]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - binutils/readelf.c
fd9722c8afc9914e64a49321913d8e09a39d66a8
[thirdparty/binutils-gdb.git] / binutils / readelf.c
1 /* readelf.c -- display contents of an ELF format file
2 Copyright (C) 1998-2025 Free Software Foundation, Inc.
3
4 Originally developed by Eric Youngdale <eric@andante.jic.com>
5 Modifications by Nick Clifton <nickc@redhat.com>
6
7 This file is part of GNU Binutils.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
22 02110-1301, USA. */
23 \f
24 /* The difference between readelf and objdump:
25
26 Both programs are capable of displaying the contents of ELF format files,
27 so why does the binutils project have two file dumpers ?
28
29 The reason is that objdump sees an ELF file through a BFD filter of the
30 world; if BFD has a bug where, say, it disagrees about a machine constant
31 in e_flags, then the odds are good that it will remain internally
32 consistent. The linker sees it the BFD way, objdump sees it the BFD way,
33 GAS sees it the BFD way. There was need for a tool to go find out what
34 the file actually says.
35
36 This is why the readelf program does not link against the BFD library - it
37 exists as an independent program to help verify the correct working of BFD.
38
39 There is also the case that readelf can provide more information about an
40 ELF file than is provided by objdump. In particular it can display DWARF
41 debugging information which (at the moment) objdump cannot. */
42 \f
43 #include "sysdep.h"
44 #include <assert.h>
45 #include <time.h>
46 #include <zlib.h>
47 #ifdef HAVE_ZSTD
48 #include <zstd.h>
49 #endif
50 #include <wchar.h>
51
52 #if defined HAVE_MSGPACK
53 #include <msgpack.h>
54 #endif
55
56 /* Define BFD64 here, even if our default architecture is 32 bit ELF
57 as this will allow us to read in and parse 64bit and 32bit ELF files. */
58 #define BFD64
59
60 #include "bfd.h"
61 #include "bucomm.h"
62 #include "elfcomm.h"
63 #include "demanguse.h"
64 #include "dwarf.h"
65 #include "ctf-api.h"
66 #include "sframe-api.h"
67 #include "demangle.h"
68
69 #include "elf/common.h"
70 #include "elf/external.h"
71 #include "elf/internal.h"
72
73
74 /* Included here, before RELOC_MACROS_GEN_FUNC is defined, so that
75 we can obtain the H8 reloc numbers. We need these for the
76 get_reloc_size() function. We include h8.h again after defining
77 RELOC_MACROS_GEN_FUNC so that we get the naming function as well. */
78
79 #include "elf/h8.h"
80 #undef _ELF_H8_H
81
82 /* Undo the effects of #including reloc-macros.h. */
83
84 #undef START_RELOC_NUMBERS
85 #undef RELOC_NUMBER
86 #undef FAKE_RELOC
87 #undef EMPTY_RELOC
88 #undef END_RELOC_NUMBERS
89 #undef _RELOC_MACROS_H
90
91 /* The following headers use the elf/reloc-macros.h file to
92 automatically generate relocation recognition functions
93 such as elf_mips_reloc_type() */
94
95 #define RELOC_MACROS_GEN_FUNC
96
97 #include "elf/aarch64.h"
98 #include "elf/alpha.h"
99 #include "elf/amdgpu.h"
100 #include "elf/arc.h"
101 #include "elf/arm.h"
102 #include "elf/avr.h"
103 #include "elf/bfin.h"
104 #include "elf/cr16.h"
105 #include "elf/cris.h"
106 #include "elf/crx.h"
107 #include "elf/csky.h"
108 #include "elf/d10v.h"
109 #include "elf/d30v.h"
110 #include "elf/dlx.h"
111 #include "elf/bpf.h"
112 #include "elf/epiphany.h"
113 #include "elf/fr30.h"
114 #include "elf/frv.h"
115 #include "elf/ft32.h"
116 #include "elf/h8.h"
117 #include "elf/hppa.h"
118 #include "elf/i386.h"
119 #include "elf/i370.h"
120 #include "elf/i860.h"
121 #include "elf/i960.h"
122 #include "elf/ia64.h"
123 #include "elf/ip2k.h"
124 #include "elf/kvx.h"
125 #include "elf/lm32.h"
126 #include "elf/iq2000.h"
127 #include "elf/m32c.h"
128 #include "elf/m32r.h"
129 #include "elf/m68k.h"
130 #include "elf/m68hc11.h"
131 #include "elf/s12z.h"
132 #include "elf/mcore.h"
133 #include "elf/mep.h"
134 #include "elf/metag.h"
135 #include "elf/microblaze.h"
136 #include "elf/mips.h"
137 #include "elf/mmix.h"
138 #include "elf/mn10200.h"
139 #include "elf/mn10300.h"
140 #include "elf/moxie.h"
141 #include "elf/mt.h"
142 #include "elf/msp430.h"
143 #include "elf/nds32.h"
144 #include "elf/nfp.h"
145 #include "elf/nios2.h"
146 #include "elf/or1k.h"
147 #include "elf/pj.h"
148 #include "elf/ppc.h"
149 #include "elf/ppc64.h"
150 #include "elf/pru.h"
151 #include "elf/riscv.h"
152 #include "elf/rl78.h"
153 #include "elf/rx.h"
154 #include "elf/s390.h"
155 #include "elf/score.h"
156 #include "elf/sh.h"
157 #include "elf/sparc.h"
158 #include "elf/spu.h"
159 #include "elf/tic6x.h"
160 #include "elf/tilegx.h"
161 #include "elf/tilepro.h"
162 #include "elf/v850.h"
163 #include "elf/vax.h"
164 #include "elf/visium.h"
165 #include "elf/wasm32.h"
166 #include "elf/x86-64.h"
167 #include "elf/xgate.h"
168 #include "elf/xstormy16.h"
169 #include "elf/xtensa.h"
170 #include "elf/z80.h"
171 #include "elf/loongarch.h"
172 #include "elf/bpf.h"
173
174 #include "getopt.h"
175 #include "libiberty.h"
176 #include "safe-ctype.h"
177 #include "filenames.h"
178
179 #ifndef offsetof
180 #define offsetof(TYPE, MEMBER) ((size_t) &(((TYPE *) 0)->MEMBER))
181 #endif
182
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 #ifdef SUPPORT_DISASSEMBLY
192 #define DISASS_DUMP (1 << 1) /* The -i command line switch. */
193 #endif
194 #define DEBUG_DUMP (1 << 2) /* The -w command line switch. */
195 #define STRING_DUMP (1 << 3) /* The -p command line switch. */
196 #define RELOC_DUMP (1 << 4) /* The -R command line switch. */
197 #define CTF_DUMP (1 << 5) /* The --ctf command line switch. */
198 #define SFRAME_DUMP (1 << 6) /* The --sframe command line switch. */
199 #define AUTO_DUMP (1 << 7) /* The -j command line switch. */
200
201 typedef unsigned char dump_type;
202
203 /* A linked list of the section names for which dumps were requested. */
204 struct dump_list_entry
205 {
206 char * name;
207 dump_type type;
208 struct dump_list_entry * next;
209 };
210
211 /* A dynamic array of flags indicating for which sections a dump
212 has been requested via command line switches. */
213 struct dump_data
214 {
215 dump_type * dump_sects;
216 unsigned int num_dump_sects;
217 };
218
219 static struct dump_data cmdline;
220
221 static struct dump_list_entry * dump_sects_byname;
222
223 char * program_name = "readelf";
224
225 static bool show_name = false;
226 static bool do_dynamic = false;
227 static bool do_syms = false;
228 static bool do_dyn_syms = false;
229 static bool do_lto_syms = false;
230 static bool do_reloc = false;
231 static bool do_sections = false;
232 static bool do_got_section_contents = false;
233 static bool do_section_groups = false;
234 static bool do_section_details = false;
235 static bool do_segments = false;
236 static bool do_unwind = false;
237 static bool do_using_dynamic = false;
238 static bool do_header = false;
239 static bool do_dump = false;
240 static bool do_version = false;
241 static bool do_histogram = false;
242 static bool do_debugging = false;
243 static bool do_ctf = false;
244 static bool do_sframe = false;
245 static bool do_arch = false;
246 static bool do_notes = false;
247 static bool do_archive_index = false;
248 static bool check_all = false;
249 static bool is_32bit_elf = false;
250 static bool decompress_dumps = false;
251 static bool do_not_show_symbol_truncation = false;
252 static bool do_demangle = false; /* Pretty print C++ symbol names. */
253 static bool process_links = false;
254 static bool dump_any_debugging = false;
255 static bool extra_sym_info = false;
256 static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
257 static int sym_base = 0;
258
259 static char *dump_ctf_parent_name;
260 static char *dump_ctf_symtab_name;
261 static char *dump_ctf_strtab_name;
262
263 struct group_list
264 {
265 struct group_list * next;
266 unsigned int section_index;
267 };
268
269 struct group
270 {
271 struct group_list * root;
272 unsigned int group_index;
273 };
274
275 typedef struct filedata
276 {
277 const char * file_name;
278 bool is_separate;
279 FILE * handle;
280 uint64_t file_size;
281 Elf_Internal_Ehdr file_header;
282 uint64_t archive_file_offset;
283 uint64_t archive_file_size;
284 /* Everything below this point is cleared out by free_filedata. */
285 Elf_Internal_Shdr * section_headers;
286 Elf_Internal_Phdr * program_headers;
287 char * string_table;
288 uint64_t string_table_length;
289 uint64_t dynamic_addr;
290 uint64_t dynamic_size;
291 uint64_t dynamic_nent;
292 Elf_Internal_Dyn * dynamic_section;
293 Elf_Internal_Shdr * dynamic_strtab_section;
294 char * dynamic_strings;
295 uint64_t dynamic_strings_length;
296 Elf_Internal_Shdr * dynamic_symtab_section;
297 uint64_t num_dynamic_syms;
298 Elf_Internal_Sym * dynamic_symbols;
299 uint64_t version_info[16];
300 unsigned int dynamic_syminfo_nent;
301 Elf_Internal_Syminfo * dynamic_syminfo;
302 uint64_t dynamic_syminfo_offset;
303 uint64_t nbuckets;
304 uint64_t nchains;
305 uint64_t * buckets;
306 uint64_t * chains;
307 uint64_t ngnubuckets;
308 uint64_t ngnuchains;
309 uint64_t * gnubuckets;
310 uint64_t * gnuchains;
311 uint64_t * mipsxlat;
312 uint64_t gnusymidx;
313 char * program_interpreter;
314 uint64_t dynamic_info[DT_RELRENT + 1];
315 uint64_t dynamic_info_DT_GNU_HASH;
316 uint64_t dynamic_info_DT_MIPS_XHASH;
317 elf_section_list * symtab_shndx_list;
318 size_t group_count;
319 struct group * section_groups;
320 struct group ** section_headers_groups;
321 /* A dynamic array of flags indicating for which sections a dump of
322 some kind has been requested. It is reset on a per-object file
323 basis and then initialised from the cmdline_dump_sects array,
324 the results of interpreting the -w switch, and the
325 dump_sects_byname list. */
326 struct dump_data dump;
327 } Filedata;
328
329 /* How to print a vma value. */
330 typedef enum print_mode
331 {
332 HEX,
333 HEX_5,
334 DEC,
335 DEC_5,
336 UNSIGNED,
337 UNSIGNED_5,
338 PREFIX_HEX,
339 PREFIX_HEX_5,
340 FULL_HEX,
341 LONG_HEX,
342 ZERO_HEX,
343 OCTAL,
344 OCTAL_5
345 }
346 print_mode;
347
348 typedef enum unicode_display_type
349 {
350 unicode_default = 0,
351 unicode_locale,
352 unicode_escape,
353 unicode_hex,
354 unicode_highlight,
355 unicode_invalid
356 } unicode_display_type;
357
358 static unicode_display_type unicode_display = unicode_default;
359
360 typedef enum
361 {
362 reltype_unknown,
363 reltype_rel,
364 reltype_rela,
365 reltype_relr
366 } relocation_type;
367
368 /* Versioned symbol info. */
369 enum versioned_symbol_info
370 {
371 symbol_undefined,
372 symbol_hidden,
373 symbol_public
374 };
375
376 /* Relocation entries */
377
378 typedef struct elf_relocation
379 {
380 bfd_vma r_offset; /* Location at which to apply the action */
381 bfd_vma r_addend; /* Constant addend used to compute value */
382 const char *r_name; /* Relocation name. */
383 char *r_symbol; /* Relocation symbol. */
384 relocation_type r_type; /* Relocation type. */
385 } elf_relocation;
386
387 static elf_relocation *all_relocations_root;
388 static elf_relocation *all_relocations;
389 static size_t all_relocations_count;
390
391 static int
392 fseek64 (FILE *stream, int64_t offset, int whence)
393 {
394 #if defined (HAVE_FSEEKO64)
395 off64_t o = offset;
396 if (o != offset)
397 {
398 errno = EINVAL;
399 return -1;
400 }
401 return fseeko64 (stream, o, whence);
402 #elif defined (HAVE_FSEEKO)
403 off_t o = offset;
404 if (o != offset)
405 {
406 errno = EINVAL;
407 return -1;
408 }
409 return fseeko (stream, o, whence);
410 #else
411 long o = offset;
412 if (o != offset)
413 {
414 errno = EINVAL;
415 return -1;
416 }
417 return fseek (stream, o, whence);
418 #endif
419 }
420
421 static const char * get_symbol_version_string
422 (Filedata *, bool, const char *, size_t, unsigned,
423 Elf_Internal_Sym *, enum versioned_symbol_info *, unsigned short *);
424
425 static bool process_notes_at
426 (Filedata *, Elf_Internal_Shdr *, uint64_t, uint64_t, uint64_t);
427
428 #define UNKNOWN -1
429
430 static inline const char *
431 section_name (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
432 {
433 return filedata->string_table + hdr->sh_name;
434 }
435
436 static inline bool
437 section_name_valid (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
438 {
439 return (filedata != NULL
440 && hdr != NULL
441 && filedata->string_table != NULL
442 && hdr->sh_name < filedata->string_table_length);
443 }
444
445 /* Returns true if the given index is real/valid. Note: "real" here
446 means "references a real section in the section header" and not
447 "is a valid section index as per the ELF standard". */
448
449 static inline bool
450 section_index_real (const Filedata *filedata, unsigned int ndx)
451 {
452 return (filedata != NULL
453 && filedata->section_headers != NULL
454 && ndx < filedata->file_header.e_shnum
455 && ndx > 0);
456 }
457
458 #define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */
459
460 static inline bool
461 valid_symbol_name (const char *strtab, size_t strtab_size, uint64_t offset)
462 {
463 return strtab != NULL && offset < strtab_size;
464 }
465
466 static inline bool
467 valid_dynamic_name (const Filedata *filedata, uint64_t offset)
468 {
469 return valid_symbol_name (filedata->dynamic_strings,
470 filedata->dynamic_strings_length, offset);
471 }
472
473 /* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
474 already been called and verified that the string exists. */
475 static inline const char *
476 get_dynamic_name (const Filedata *filedata, size_t offset)
477 {
478 return filedata->dynamic_strings + offset;
479 }
480
481 #define REMOVE_ARCH_BITS(ADDR) \
482 do \
483 { \
484 if (filedata->file_header.e_machine == EM_ARM) \
485 (ADDR) &= ~1; \
486 } \
487 while (0)
488
489 /* Get the correct GNU hash section name. */
490 #define GNU_HASH_SECTION_NAME(filedata) \
491 filedata->dynamic_info_DT_MIPS_XHASH ? ".MIPS.xhash" : ".gnu.hash"
492 \f
493 /* Retrieve NMEMB structures, each SIZE bytes long from FILEDATA starting at
494 OFFSET + the offset of the current archive member, if we are examining an
495 archive. Put the retrieved data into VAR, if it is not NULL. Otherwise
496 allocate a buffer using malloc and fill that. In either case return the
497 pointer to the start of the retrieved data or NULL if something went wrong.
498 If something does go wrong and REASON is not NULL then emit an error
499 message using REASON as part of the context. */
500
501 static void *
502 get_data (void *var,
503 Filedata *filedata,
504 uint64_t offset,
505 uint64_t size,
506 uint64_t nmemb,
507 const char *reason)
508 {
509 void * mvar;
510 uint64_t amt = size * nmemb;
511
512 if (size == 0 || nmemb == 0)
513 return NULL;
514
515 /* If size_t is smaller than uint64_t, eg because you are building
516 on a 32-bit host, then make sure that when the sizes are cast to
517 size_t no information is lost. */
518 if ((size_t) size != size
519 || (size_t) nmemb != nmemb
520 || (size_t) amt != amt
521 || amt / size != nmemb
522 || (size_t) amt + 1 == 0)
523 {
524 if (reason)
525 error (_("Size overflow prevents reading %" PRIu64
526 " elements of size %" PRIu64 " for %s\n"),
527 nmemb, size, reason);
528 return NULL;
529 }
530
531 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
532 attempting to allocate memory when the read is bound to fail. */
533 if (filedata->archive_file_offset > filedata->file_size
534 || offset > filedata->file_size - filedata->archive_file_offset
535 || amt > filedata->file_size - filedata->archive_file_offset - offset)
536 {
537 if (reason)
538 error (_("Reading %" PRIu64 " bytes extends past end of file for %s\n"),
539 amt, reason);
540 return NULL;
541 }
542
543 if (fseek64 (filedata->handle, filedata->archive_file_offset + offset,
544 SEEK_SET))
545 {
546 if (reason)
547 error (_("Unable to seek to %#" PRIx64 " for %s\n"),
548 filedata->archive_file_offset + offset, reason);
549 return NULL;
550 }
551
552 mvar = var;
553 if (mvar == NULL)
554 {
555 /* + 1 so that we can '\0' terminate invalid string table sections. */
556 mvar = malloc ((size_t) amt + 1);
557
558 if (mvar == NULL)
559 {
560 if (reason)
561 error (_("Out of memory allocating %" PRIu64 " bytes for %s\n"),
562 amt, reason);
563 return NULL;
564 }
565
566 ((char *) mvar)[amt] = '\0';
567 }
568
569 if (fread (mvar, (size_t) size, (size_t) nmemb, filedata->handle) != nmemb)
570 {
571 if (reason)
572 error (_("Unable to read in %" PRIu64 " bytes of %s\n"),
573 amt, reason);
574 if (mvar != var)
575 free (mvar);
576 return NULL;
577 }
578
579 return mvar;
580 }
581
582 /* Print a VMA value in the MODE specified.
583 Returns the number of characters displayed. */
584
585 static unsigned int
586 print_vma (uint64_t vma, print_mode mode)
587 {
588 unsigned int nc = 0;
589
590 switch (mode)
591 {
592 case FULL_HEX:
593 nc = printf ("0x");
594 /* Fall through. */
595 case LONG_HEX:
596 if (!is_32bit_elf)
597 return nc + printf ("%16.16" PRIx64, vma);
598 return nc + printf ("%8.8" PRIx64, vma);
599
600 case ZERO_HEX:
601 if (is_32bit_elf)
602 return printf ("%08" PRIx64, vma);
603 return printf ("%016" PRIx64, vma);
604
605 case DEC_5:
606 if (vma <= 99999)
607 return printf ("%5" PRId64, vma);
608 /* Fall through. */
609 case PREFIX_HEX:
610 nc = printf ("0x");
611 /* Fall through. */
612 case HEX:
613 return nc + printf ("%" PRIx64, vma);
614
615 case PREFIX_HEX_5:
616 nc = printf ("0x");
617 /* Fall through. */
618 case HEX_5:
619 return nc + printf ("%05" PRIx64, vma);
620
621 case DEC:
622 return printf ("%" PRId64, vma);
623
624 case UNSIGNED:
625 return printf ("%" PRIu64, vma);
626
627 case UNSIGNED_5:
628 return printf ("%5" PRIu64, vma);
629
630 case OCTAL:
631 return printf ("%" PRIo64, vma);
632
633 case OCTAL_5:
634 return printf ("%5" PRIo64, vma);
635
636 default:
637 /* FIXME: Report unrecognised mode ? */
638 return 0;
639 }
640 }
641
642
643 /* Display a symbol on stdout. Handles the display of control characters and
644 multibye characters (assuming the host environment supports them).
645
646 Display at most abs(WIDTH) characters, truncating as necessary,
647 unless do_wide or extra_sym_info is true.
648
649 If truncation will happen and do_not_show_symbol_truncation is FALSE then display
650 abs(WIDTH) - 5 characters followed by "[...]".
651
652 If WIDTH is negative then ensure that the output is at least (- WIDTH) characters,
653 padding as necessary.
654
655 Returns the number of emitted characters. */
656
657 static unsigned int
658 print_symbol_name (signed int width, const char * symbol)
659 {
660 bool extra_padding = false;
661 bool do_dots = false;
662 signed int num_printed = 0;
663 #ifdef HAVE_MBSTATE_T
664 mbstate_t state;
665 #endif
666 unsigned int width_remaining;
667 const void * alloced_symbol = NULL;
668
669 if (width < 0)
670 {
671 /* Keep the width positive. This helps the code below. */
672 width = - width;
673 extra_padding = true;
674 }
675 else if (width == 0)
676 return 0;
677
678 if (do_wide || extra_sym_info)
679 /* Set the remaining width to a very large value.
680 This simplifies the code below. */
681 width_remaining = INT_MAX;
682 else
683 {
684 width_remaining = width;
685
686 if (! do_not_show_symbol_truncation
687 && (int) strlen (symbol) > width)
688 {
689 width_remaining -= 5;
690 if ((int) width_remaining < 0)
691 width_remaining = 0;
692 do_dots = true;
693 }
694 }
695
696 #ifdef HAVE_MBSTATE_T
697 /* Initialise the multibyte conversion state. */
698 memset (& state, 0, sizeof (state));
699 #endif
700
701 if (do_demangle && *symbol)
702 {
703 const char * res = cplus_demangle (symbol, demangle_flags);
704
705 if (res != NULL)
706 alloced_symbol = symbol = res;
707 }
708
709 while (width_remaining)
710 {
711 size_t n;
712 const char c = *symbol++;
713
714 if (c == 0)
715 break;
716
717 if (ISPRINT (c))
718 {
719 putchar (c);
720 width_remaining --;
721 num_printed ++;
722 }
723 else if (ISCNTRL (c))
724 {
725 /* Do not print control characters directly as they can affect terminal
726 settings. Such characters usually appear in the names generated
727 by the assembler for local labels. */
728
729 if (width_remaining < 2)
730 break;
731
732 printf ("^%c", c + 0x40);
733 width_remaining -= 2;
734 num_printed += 2;
735 }
736 else if (c == 0x7f)
737 {
738 if (width_remaining < 5)
739 break;
740 printf ("<DEL>");
741 width_remaining -= 5;
742 num_printed += 5;
743 }
744 else if (unicode_display != unicode_locale
745 && unicode_display != unicode_default)
746 {
747 /* Display unicode characters as something else. */
748 unsigned char bytes[4];
749 bool is_utf8;
750 unsigned int nbytes;
751
752 bytes[0] = c;
753
754 if (bytes[0] < 0xc0)
755 {
756 nbytes = 1;
757 is_utf8 = false;
758 }
759 else
760 {
761 bytes[1] = *symbol++;
762
763 if ((bytes[1] & 0xc0) != 0x80)
764 {
765 is_utf8 = false;
766 /* Do not consume this character. It may only
767 be the first byte in the sequence that was
768 corrupt. */
769 --symbol;
770 nbytes = 1;
771 }
772 else if ((bytes[0] & 0x20) == 0)
773 {
774 is_utf8 = true;
775 nbytes = 2;
776 }
777 else
778 {
779 bytes[2] = *symbol++;
780
781 if ((bytes[2] & 0xc0) != 0x80)
782 {
783 is_utf8 = false;
784 symbol -= 2;
785 nbytes = 1;
786 }
787 else if ((bytes[0] & 0x10) == 0)
788 {
789 is_utf8 = true;
790 nbytes = 3;
791 }
792 else
793 {
794 bytes[3] = *symbol++;
795
796 nbytes = 4;
797
798 if ((bytes[3] & 0xc0) != 0x80)
799 {
800 is_utf8 = false;
801 symbol -= 3;
802 nbytes = 1;
803 }
804 else
805 is_utf8 = true;
806 }
807 }
808 }
809
810 if (unicode_display == unicode_invalid)
811 is_utf8 = false;
812
813 if (unicode_display == unicode_hex || ! is_utf8)
814 {
815 unsigned int i;
816
817 if (width_remaining < (nbytes * 2) + 2)
818 break;
819
820 putchar (is_utf8 ? '<' : '{');
821 printf ("0x");
822 for (i = 0; i < nbytes; i++)
823 printf ("%02x", bytes[i]);
824 putchar (is_utf8 ? '>' : '}');
825 }
826 else
827 {
828 if (unicode_display == unicode_highlight && isatty (1))
829 printf ("\x1B[31;47m"); /* Red. */
830
831 switch (nbytes)
832 {
833 case 2:
834 if (width_remaining < 6)
835 break;
836 printf ("\\u%02x%02x",
837 (bytes[0] & 0x1c) >> 2,
838 ((bytes[0] & 0x03) << 6) | (bytes[1] & 0x3f));
839 break;
840 case 3:
841 if (width_remaining < 6)
842 break;
843 printf ("\\u%02x%02x",
844 ((bytes[0] & 0x0f) << 4) | ((bytes[1] & 0x3c) >> 2),
845 ((bytes[1] & 0x03) << 6) | (bytes[2] & 0x3f));
846 break;
847 case 4:
848 if (width_remaining < 8)
849 break;
850 printf ("\\u%02x%02x%02x",
851 ((bytes[0] & 0x07) << 2) | ((bytes[1] & 0x30) >> 4),
852 ((bytes[1] & 0x0f) << 4) | ((bytes[2] & 0x3c) >> 2),
853 ((bytes[2] & 0x03) << 6) | (bytes[3] & 0x3f));
854
855 break;
856 default:
857 /* URG. */
858 break;
859 }
860
861 if (unicode_display == unicode_highlight && isatty (1))
862 printf ("\033[0m"); /* Default colour. */
863 }
864
865 if (bytes[nbytes - 1] == 0)
866 break;
867 }
868 else
869 {
870 #ifdef HAVE_MBSTATE_T
871 wchar_t w;
872 #endif
873 /* Let printf do the hard work of displaying multibyte characters. */
874 printf ("%.1s", symbol - 1);
875 width_remaining --;
876 num_printed ++;
877
878 #ifdef HAVE_MBSTATE_T
879 /* Try to find out how many bytes made up the character that was
880 just printed. Advance the symbol pointer past the bytes that
881 were displayed. */
882 n = mbrtowc (& w, symbol - 1, MB_CUR_MAX, & state);
883 #else
884 n = 1;
885 #endif
886 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
887 symbol += (n - 1);
888 }
889 }
890
891 if (do_dots)
892 num_printed += printf ("[...]");
893
894 if (extra_padding && num_printed < width)
895 {
896 /* Fill in the remaining spaces. */
897 printf ("%-*s", width - num_printed, " ");
898 num_printed = width;
899 }
900
901 free ((void *) alloced_symbol);
902 return num_printed;
903 }
904
905 /* Returns a pointer to a static buffer containing a printable version of
906 STRING. Uses a rotating array of static buffers, so that multiple
907 successive calls will still work. eg when used in a call to printf().
908
909 If supplied MAX_LEN is the maximum number of characters to be read
910 starting from STRING.
911
912 This function is similar to print_symbol_name(), except that it does
913 not try to print multibyte characters, it just shows them as hex values.
914
915 If the string is too long for the static buffer or if it is not
916 terminated then a truncated version of the string will be returned. */
917
918 static const char *
919 printable_string (const char * string, unsigned int max_len)
920 {
921 #define NUM_STRING_BUFS 5
922 #define MAX_STRING_LEN 256
923
924 static int string_buf_index = 0;
925 static char string_buf [NUM_STRING_BUFS][MAX_STRING_LEN + 1];
926
927 char * buf;
928 char * buf_start;
929
930 /* Select a buffer to use. */
931 buf_start = buf = string_buf[string_buf_index];
932 if (++ string_buf_index >= NUM_STRING_BUFS)
933 string_buf_index = 0;
934
935 char c;
936 unsigned int remaining = MAX_STRING_LEN;
937
938 while ((c = * string ++) != 0)
939 {
940 if (ISCNTRL (c))
941 {
942 if (remaining < 2)
943 break;
944
945 * buf ++ = '^';
946 * buf ++ = c + 0x40;
947 remaining -= 2;
948 }
949 else if (ISPRINT (c))
950 {
951 * buf ++ = c;
952 remaining -= 1;
953 }
954 else
955 {
956 static char hex[17] = "0123456789ABCDEF";
957
958 if (remaining < 4)
959 break;
960 * buf ++ = '<';
961 * buf ++ = hex[(c & 0xf0) >> 4];
962 * buf ++ = hex[c & 0x0f];
963 * buf ++ = '>';
964 remaining -= 4;
965 }
966
967 if (remaining == 0)
968 break;
969
970 if (max_len > 0)
971 {
972 max_len -= 1;
973 if (max_len == 0)
974 break;
975 }
976 }
977
978 * buf = 0;
979 return buf_start;
980 }
981
982 /* Returns a pointer to a static buffer containing a
983 printable version of the given section's name. */
984
985 static const char *
986 printable_section_name (Filedata * filedata, const Elf_Internal_Shdr * sec)
987 {
988 /* Validate the input parameters. */
989 if (filedata == NULL)
990 return _("<internal error>");
991 if (sec == NULL)
992 return _("<none>");
993 if (filedata->string_table == NULL)
994 return _("<no-strings>");
995 if (sec->sh_name >= filedata->string_table_length)
996 return _("<corrupt>");
997
998 return printable_string (section_name (filedata, sec),
999 filedata->string_table_length - sec->sh_name);
1000 }
1001
1002 /* Return TRUE if the current file is for IA-64 machine and OpenVMS ABI.
1003 This OS has so many departures from the ELF standard that we test it at
1004 many places. */
1005
1006 static inline bool
1007 is_ia64_vms (Filedata * filedata)
1008 {
1009 return filedata->file_header.e_machine == EM_IA_64
1010 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS;
1011 }
1012
1013 static const char *
1014 printable_section_name_from_index (Filedata * filedata,
1015 size_t ndx,
1016 bool * is_special)
1017 {
1018 if (is_special != NULL)
1019 * is_special = true;
1020
1021 switch (ndx)
1022 {
1023 case SHN_UNDEF: return "UND";
1024 case SHN_ABS: return "ABS";
1025 case SHN_COMMON: return "COM";
1026 break;
1027 }
1028
1029 if (filedata != NULL)
1030 {
1031 switch (filedata->file_header.e_machine)
1032 {
1033 case EM_MIPS:
1034 if (ndx == SHN_MIPS_SCOMMON)
1035 return "SCOMMON";
1036 if (ndx == SHN_MIPS_SUNDEFINED)
1037 return "SUNDEF";
1038 break;
1039
1040 case EM_TI_C6000:
1041 if (ndx == SHN_TIC6X_SCOMMON)
1042 return "SCOM";
1043 break;
1044
1045 case EM_X86_64:
1046 case EM_L1OM:
1047 case EM_K1OM:
1048 if (ndx == SHN_X86_64_LCOMMON)
1049 return "LARGE_COM";
1050 break;
1051
1052 case EM_IA_64:
1053 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
1054 && ndx == SHN_IA_64_ANSI_COMMON)
1055 return "ANSI_COM";
1056
1057 if (is_ia64_vms (filedata) && ndx == SHN_IA_64_VMS_SYMVEC)
1058 return "VMS_SYMVEC";
1059 break;
1060
1061 default:
1062 break;
1063 }
1064
1065 if (filedata->section_headers != NULL
1066 && ndx < filedata->file_header.e_shnum)
1067 {
1068 const char * res;
1069
1070 res = printable_section_name (filedata, filedata->section_headers + ndx);
1071 if (is_special != NULL)
1072 * is_special = (res[0] == '<');
1073
1074 return res;
1075 }
1076 }
1077
1078 static char name_buf[40];
1079 unsigned int short_ndx = (unsigned int) (ndx & 0xffff);
1080
1081 if (ndx >= SHN_LOPROC && ndx <= SHN_HIPROC)
1082 sprintf (name_buf, "PRC[0x%04x]", short_ndx);
1083 else if (ndx >= SHN_LOOS && ndx <= SHN_HIOS)
1084 sprintf (name_buf, "OS [0x%04x]", short_ndx);
1085 else if (ndx >= SHN_LORESERVE)
1086 sprintf (name_buf, "RSV[0x%04x]", short_ndx);
1087 else if (filedata->file_header.e_shnum != 0
1088 && ndx >= filedata->file_header.e_shnum)
1089 sprintf (name_buf, _("BAD[0x%lx]"), (long) ndx);
1090 else
1091 sprintf (name_buf, "<section 0x%lx>", (long) ndx);
1092
1093 return name_buf;
1094 }
1095
1096 /* Return a pointer to section NAME, or NULL if no such section exists. */
1097
1098 static Elf_Internal_Shdr *
1099 find_section (Filedata * filedata, const char * name)
1100 {
1101 unsigned int i;
1102
1103 if (filedata->section_headers == NULL)
1104 return NULL;
1105
1106 for (i = 0; i < filedata->file_header.e_shnum; i++)
1107 if (section_name_valid (filedata, filedata->section_headers + i)
1108 && streq (section_name (filedata, filedata->section_headers + i),
1109 name))
1110 return filedata->section_headers + i;
1111
1112 return NULL;
1113 }
1114
1115 /* Return a pointer to a section containing ADDR, or NULL if no such
1116 section exists. */
1117
1118 static Elf_Internal_Shdr *
1119 find_section_by_address (Filedata * filedata, uint64_t addr)
1120 {
1121 unsigned int i;
1122
1123 if (filedata->section_headers == NULL)
1124 return NULL;
1125
1126 for (i = 0; i < filedata->file_header.e_shnum; i++)
1127 {
1128 Elf_Internal_Shdr *sec = filedata->section_headers + i;
1129
1130 if (addr >= sec->sh_addr && addr < sec->sh_addr + sec->sh_size)
1131 return sec;
1132 }
1133
1134 return NULL;
1135 }
1136
1137 static Elf_Internal_Shdr *
1138 find_section_by_type (Filedata * filedata, unsigned int type)
1139 {
1140 unsigned int i;
1141
1142 if (filedata->section_headers == NULL)
1143 return NULL;
1144
1145 for (i = 0; i < filedata->file_header.e_shnum; i++)
1146 {
1147 Elf_Internal_Shdr *sec = filedata->section_headers + i;
1148
1149 if (sec->sh_type == type)
1150 return sec;
1151 }
1152
1153 return NULL;
1154 }
1155
1156 static Elf_Internal_Shdr *
1157 find_section_by_name (Filedata * filedata, const char * name)
1158 {
1159 unsigned int i;
1160
1161 if (filedata->section_headers == NULL || filedata->string_table_length == 0)
1162 return NULL;
1163
1164 for (i = 0; i < filedata->file_header.e_shnum; i++)
1165 {
1166 Elf_Internal_Shdr *sec = filedata->section_headers + i;
1167
1168 if (sec->sh_name < filedata->string_table_length
1169 && streq (name, filedata->string_table + sec->sh_name))
1170 return sec;
1171 }
1172
1173 return NULL;
1174 }
1175
1176 /* Return a pointer to section NAME, or NULL if no such section exists,
1177 restricted to the list of sections given in SET. */
1178
1179 static Elf_Internal_Shdr *
1180 find_section_in_set (Filedata * filedata, const char * name, unsigned int * set)
1181 {
1182 unsigned int i;
1183
1184 if (filedata->section_headers == NULL)
1185 return NULL;
1186
1187 if (set != NULL)
1188 {
1189 while ((i = *set++) > 0)
1190 {
1191 /* See PR 21156 for a reproducer. */
1192 if (i >= filedata->file_header.e_shnum)
1193 continue; /* FIXME: Should we issue an error message ? */
1194
1195 if (section_name_valid (filedata, filedata->section_headers + i)
1196 && streq (section_name (filedata, filedata->section_headers + i),
1197 name))
1198 return filedata->section_headers + i;
1199 }
1200 }
1201
1202 return find_section (filedata, name);
1203 }
1204
1205 /* Guess the relocation size commonly used by the specific machines. */
1206
1207 static bool
1208 guess_is_rela (unsigned int e_machine)
1209 {
1210 switch (e_machine)
1211 {
1212 /* Targets that use REL relocations. */
1213 case EM_386:
1214 case EM_IAMCU:
1215 case EM_960:
1216 case EM_ARM:
1217 case EM_D10V:
1218 case EM_CYGNUS_D10V:
1219 case EM_DLX:
1220 case EM_MIPS:
1221 case EM_MIPS_RS3_LE:
1222 case EM_CYGNUS_M32R:
1223 case EM_SCORE:
1224 case EM_XGATE:
1225 case EM_NFP:
1226 case EM_BPF:
1227 return false;
1228
1229 /* Targets that use RELA relocations. */
1230 case EM_68K:
1231 case EM_860:
1232 case EM_AARCH64:
1233 case EM_ADAPTEVA_EPIPHANY:
1234 case EM_ALPHA:
1235 case EM_ALTERA_NIOS2:
1236 case EM_ARC:
1237 case EM_ARC_COMPACT:
1238 case EM_ARC_COMPACT2:
1239 case EM_ARC_COMPACT3:
1240 case EM_ARC_COMPACT3_64:
1241 case EM_AVR:
1242 case EM_AVR_OLD:
1243 case EM_BLACKFIN:
1244 case EM_CR16:
1245 case EM_CRIS:
1246 case EM_CRX:
1247 case EM_CSKY:
1248 case EM_D30V:
1249 case EM_CYGNUS_D30V:
1250 case EM_FR30:
1251 case EM_FT32:
1252 case EM_CYGNUS_FR30:
1253 case EM_CYGNUS_FRV:
1254 case EM_H8S:
1255 case EM_H8_300:
1256 case EM_H8_300H:
1257 case EM_IA_64:
1258 case EM_IP2K:
1259 case EM_IP2K_OLD:
1260 case EM_IQ2000:
1261 case EM_KVX:
1262 case EM_LATTICEMICO32:
1263 case EM_M32C_OLD:
1264 case EM_M32C:
1265 case EM_M32R:
1266 case EM_MCORE:
1267 case EM_CYGNUS_MEP:
1268 case EM_METAG:
1269 case EM_MMIX:
1270 case EM_MN10200:
1271 case EM_CYGNUS_MN10200:
1272 case EM_MN10300:
1273 case EM_CYGNUS_MN10300:
1274 case EM_MOXIE:
1275 case EM_MSP430:
1276 case EM_MSP430_OLD:
1277 case EM_MT:
1278 case EM_NDS32:
1279 case EM_NIOS32:
1280 case EM_OR1K:
1281 case EM_PPC64:
1282 case EM_PPC:
1283 case EM_TI_PRU:
1284 case EM_RISCV:
1285 case EM_RL78:
1286 case EM_RX:
1287 case EM_S390:
1288 case EM_S390_OLD:
1289 case EM_SH:
1290 case EM_SPARC:
1291 case EM_SPARC32PLUS:
1292 case EM_SPARCV9:
1293 case EM_SPU:
1294 case EM_TI_C6000:
1295 case EM_TILEGX:
1296 case EM_TILEPRO:
1297 case EM_V800:
1298 case EM_V850:
1299 case EM_CYGNUS_V850:
1300 case EM_VAX:
1301 case EM_VISIUM:
1302 case EM_X86_64:
1303 case EM_L1OM:
1304 case EM_K1OM:
1305 case EM_XSTORMY16:
1306 case EM_XTENSA:
1307 case EM_XTENSA_OLD:
1308 case EM_MICROBLAZE:
1309 case EM_MICROBLAZE_OLD:
1310 case EM_WEBASSEMBLY:
1311 return true;
1312
1313 case EM_68HC05:
1314 case EM_68HC08:
1315 case EM_68HC11:
1316 case EM_68HC16:
1317 case EM_FX66:
1318 case EM_ME16:
1319 case EM_MMA:
1320 case EM_NCPU:
1321 case EM_NDR1:
1322 case EM_PCP:
1323 case EM_ST100:
1324 case EM_ST19:
1325 case EM_ST7:
1326 case EM_ST9PLUS:
1327 case EM_STARCORE:
1328 case EM_SVX:
1329 case EM_TINYJ:
1330 default:
1331 warn (_("Don't know about relocations on this machine architecture\n"));
1332 return false;
1333 }
1334 }
1335
1336 /* Load RELA type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1337 Returns TRUE upon success, FALSE otherwise. If successful then a
1338 pointer to a malloc'ed buffer containing the relocs is placed in *RELASP,
1339 and the number of relocs loaded is placed in *NRELASP. It is the caller's
1340 responsibility to free the allocated buffer. */
1341
1342 static bool
1343 slurp_rela_relocs (Filedata *filedata,
1344 uint64_t rel_offset,
1345 uint64_t rel_size,
1346 Elf_Internal_Rela **relasp,
1347 uint64_t *nrelasp)
1348 {
1349 Elf_Internal_Rela * relas;
1350 uint64_t nrelas;
1351 unsigned int i;
1352
1353 if (is_32bit_elf)
1354 {
1355 Elf32_External_Rela * erelas;
1356
1357 erelas = (Elf32_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1358 rel_size, _("32-bit relocation data"));
1359 if (!erelas)
1360 return false;
1361
1362 nrelas = rel_size / sizeof (Elf32_External_Rela);
1363
1364 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1365 sizeof (Elf_Internal_Rela));
1366
1367 if (relas == NULL)
1368 {
1369 free (erelas);
1370 error (_("out of memory parsing relocs\n"));
1371 return false;
1372 }
1373
1374 for (i = 0; i < nrelas; i++)
1375 {
1376 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1377 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1378 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1379 }
1380
1381 free (erelas);
1382 }
1383 else
1384 {
1385 Elf64_External_Rela * erelas;
1386
1387 erelas = (Elf64_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1388 rel_size, _("64-bit relocation data"));
1389 if (!erelas)
1390 return false;
1391
1392 nrelas = rel_size / sizeof (Elf64_External_Rela);
1393
1394 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1395 sizeof (Elf_Internal_Rela));
1396
1397 if (relas == NULL)
1398 {
1399 free (erelas);
1400 error (_("out of memory parsing relocs\n"));
1401 return false;
1402 }
1403
1404 for (i = 0; i < nrelas; i++)
1405 {
1406 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1407 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1408 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1409
1410 if (filedata->file_header.e_machine == EM_MIPS
1411 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1412 {
1413 /* In little-endian objects, r_info isn't really a
1414 64-bit little-endian value: it has a 32-bit
1415 little-endian symbol index followed by four
1416 individual byte fields. Reorder INFO
1417 accordingly. */
1418 uint64_t inf = relas[i].r_info;
1419 inf = (((inf & 0xffffffff) << 32)
1420 | ((inf >> 56) & 0xff)
1421 | ((inf >> 40) & 0xff00)
1422 | ((inf >> 24) & 0xff0000)
1423 | ((inf >> 8) & 0xff000000));
1424 relas[i].r_info = inf;
1425 }
1426 }
1427
1428 free (erelas);
1429 }
1430
1431 *relasp = relas;
1432 *nrelasp = nrelas;
1433 return true;
1434 }
1435
1436 /* Load REL type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1437 Returns TRUE upon success, FALSE otherwise. If successful then a
1438 pointer to a malloc'ed buffer containing the relocs is placed in *RELSP,
1439 and the number of relocs loaded is placed in *NRELSP. It is the caller's
1440 responsibility to free the allocated buffer. */
1441
1442 static bool
1443 slurp_rel_relocs (Filedata *filedata,
1444 uint64_t rel_offset,
1445 uint64_t rel_size,
1446 Elf_Internal_Rela **relsp,
1447 uint64_t *nrelsp)
1448 {
1449 Elf_Internal_Rela * rels;
1450 uint64_t nrels;
1451 unsigned int i;
1452
1453 if (is_32bit_elf)
1454 {
1455 Elf32_External_Rel * erels;
1456
1457 erels = (Elf32_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1458 rel_size, _("32-bit relocation data"));
1459 if (!erels)
1460 return false;
1461
1462 nrels = rel_size / sizeof (Elf32_External_Rel);
1463
1464 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1465
1466 if (rels == NULL)
1467 {
1468 free (erels);
1469 error (_("out of memory parsing relocs\n"));
1470 return false;
1471 }
1472
1473 for (i = 0; i < nrels; i++)
1474 {
1475 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1476 rels[i].r_info = BYTE_GET (erels[i].r_info);
1477 rels[i].r_addend = 0;
1478 }
1479
1480 free (erels);
1481 }
1482 else
1483 {
1484 Elf64_External_Rel * erels;
1485
1486 erels = (Elf64_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1487 rel_size, _("64-bit relocation data"));
1488 if (!erels)
1489 return false;
1490
1491 nrels = rel_size / sizeof (Elf64_External_Rel);
1492
1493 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1494
1495 if (rels == NULL)
1496 {
1497 free (erels);
1498 error (_("out of memory parsing relocs\n"));
1499 return false;
1500 }
1501
1502 for (i = 0; i < nrels; i++)
1503 {
1504 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1505 rels[i].r_info = BYTE_GET (erels[i].r_info);
1506 rels[i].r_addend = 0;
1507
1508 if (filedata->file_header.e_machine == EM_MIPS
1509 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1510 {
1511 /* In little-endian objects, r_info isn't really a
1512 64-bit little-endian value: it has a 32-bit
1513 little-endian symbol index followed by four
1514 individual byte fields. Reorder INFO
1515 accordingly. */
1516 uint64_t inf = rels[i].r_info;
1517 inf = (((inf & 0xffffffff) << 32)
1518 | ((inf >> 56) & 0xff)
1519 | ((inf >> 40) & 0xff00)
1520 | ((inf >> 24) & 0xff0000)
1521 | ((inf >> 8) & 0xff000000));
1522 rels[i].r_info = inf;
1523 }
1524 }
1525
1526 free (erels);
1527 }
1528
1529 *relsp = rels;
1530 *nrelsp = nrels;
1531 return true;
1532 }
1533
1534 /* Returns the reloc type extracted from the reloc info field. */
1535
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
1576 static const char *
1577 get_symbol_at (Filedata * filedata,
1578 Elf_Internal_Sym * symtab,
1579 uint64_t nsyms,
1580 char * strtab,
1581 uint64_t where,
1582 uint64_t * offset_return)
1583 {
1584 Elf_Internal_Sym * beg = symtab;
1585 Elf_Internal_Sym * end = symtab + nsyms;
1586 Elf_Internal_Sym * best = NULL;
1587 uint64_t dist = 0x100000;
1588
1589 /* FIXME: Since this function is likely to be called repeatedly with
1590 slightly increasing addresses each time, we could speed things up by
1591 caching the last returned value and starting our search from there. */
1592 while (beg < end)
1593 {
1594 Elf_Internal_Sym * sym;
1595 uint64_t value;
1596
1597 sym = beg + (end - beg) / 2;
1598
1599 value = sym->st_value;
1600
1601 if (where >= value
1602 && where - value < dist)
1603 {
1604 best = sym;
1605 dist = where - value;
1606 if (dist == 0)
1607 break;
1608 }
1609
1610 if (where < value)
1611 end = sym;
1612 else
1613 beg = sym + 1;
1614 }
1615
1616 const char *name;
1617
1618 /* If there is a section start closer than the found symbol then
1619 use that for symbolizing the address. */
1620 Elf_Internal_Shdr *sec = find_section_by_address (filedata, where);
1621 if (sec != NULL
1622 && where - sec->sh_addr < dist
1623 && section_name_valid (filedata, sec))
1624 {
1625 name = section_name (filedata, sec);
1626 dist = where - sec->sh_addr;
1627 }
1628 else if (best != NULL)
1629 name = strtab + best->st_name;
1630 else
1631 return NULL;
1632
1633 if (offset_return != NULL)
1634 * offset_return = dist;
1635
1636 return name;
1637 }
1638
1639 static void
1640 print_relr_addr_and_sym (Filedata * filedata,
1641 Elf_Internal_Sym * symtab,
1642 uint64_t nsyms,
1643 char * strtab,
1644 uint64_t where)
1645 {
1646 const char * symname = NULL;
1647 uint64_t offset = 0;
1648
1649 print_vma (where, ZERO_HEX);
1650 printf (" ");
1651
1652 symname = get_symbol_at (filedata, symtab, nsyms, strtab, where, & offset);
1653
1654 if (symname == NULL)
1655 printf ("<no sym>");
1656 else if (offset == 0)
1657 print_symbol_name (38, symname);
1658 else
1659 {
1660 print_symbol_name (28, symname);
1661 printf (" + ");
1662 print_vma (offset, PREFIX_HEX);
1663 }
1664 }
1665
1666 /* See bfd_is_aarch64_special_symbol_name. */
1667
1668 static bool
1669 is_aarch64_special_symbol_name (const char *name)
1670 {
1671 if (!name || name[0] != '$')
1672 return false;
1673 if (name[1] == 'x' || name[1] == 'd')
1674 /* Map. */;
1675 else if (name[1] == 'm' || name[1] == 'f' || name[1] == 'p')
1676 /* Tag. */;
1677 else
1678 return false;
1679 return name[2] == 0 || name[2] == '.';
1680 }
1681
1682 static bool
1683 is_special_symbol_name (Filedata * filedata, const char * s)
1684 {
1685 switch (filedata->file_header.e_machine)
1686 {
1687 case EM_AARCH64:
1688 return is_aarch64_special_symbol_name (s);
1689
1690 default:
1691 return false;
1692 }
1693 }
1694
1695 /* Allows selecting the best symbol from a set for displaying addresses.
1696 BEST is the current best or NULL if there are no good symbols yet.
1697 SYM is the next symbol to consider, if it is better than BEST then
1698 return SYM else return BEST. */
1699
1700 static Elf_Internal_Sym *
1701 select_display_sym (Filedata * filedata,
1702 char * strtab,
1703 uint64_t strtablen,
1704 Elf_Internal_Sym * best,
1705 Elf_Internal_Sym * sym)
1706 {
1707 /* Ignore empty or invalid syms. */
1708 if (sym->st_name == 0)
1709 return best;
1710 if (sym->st_name >= strtablen)
1711 return best;
1712 /* Ignore undefined or TLS syms. */
1713 if (sym->st_shndx == SHN_UNDEF)
1714 return best;
1715 if (ELF_ST_TYPE (sym->st_info) == STT_TLS)
1716 return best;
1717
1718 char *s = strtab + sym->st_name;
1719
1720 /* Don't display special symbols. */
1721 if (is_special_symbol_name (filedata, s))
1722 return best;
1723
1724 /* Here SYM is good for display. */
1725
1726 if (best == NULL)
1727 return sym;
1728
1729 char *sbest = strtab + best->st_name;
1730
1731 /* Prefer non-local symbols. */
1732 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL
1733 && ELF_ST_BIND (best->st_info) != STB_LOCAL)
1734 return best;
1735 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL
1736 && ELF_ST_BIND (best->st_info) == STB_LOCAL)
1737 return sym;
1738
1739 /* Select based on lexicographic order. */
1740 return strcmp (s, sbest) < 0 ? sym : best;
1741 }
1742
1743 /* Filter the sorted SYMTAB symbol array in-place to select at most one
1744 symbol for an address and drop symbols that are not good to display.
1745 Returns the new array length. */
1746
1747 static uint64_t
1748 filter_display_syms (Filedata * filedata,
1749 Elf_Internal_Sym * symtab,
1750 uint64_t nsyms,
1751 char * strtab,
1752 uint64_t strtablen)
1753 {
1754 Elf_Internal_Sym *r = symtab;
1755 Elf_Internal_Sym *w = symtab;
1756 Elf_Internal_Sym *best = NULL;
1757 Elf_Internal_Sym *end = symtab + nsyms;
1758 while (r < end)
1759 {
1760 /* Select the best symbol for an address. */
1761 while (r < end
1762 && (best == NULL || best->st_value == r->st_value))
1763 {
1764 best = select_display_sym (filedata, strtab, strtablen, best, r);
1765 r++;
1766 }
1767 if (best != NULL)
1768 {
1769 *w = *best;
1770 w++;
1771 best = NULL;
1772 }
1773 }
1774 return w - symtab;
1775 }
1776
1777 static /* signed */ int
1778 symcmp (const void *p, const void *q)
1779 {
1780 Elf_Internal_Sym *sp = (Elf_Internal_Sym *) p;
1781 Elf_Internal_Sym *sq = (Elf_Internal_Sym *) q;
1782
1783 return sp->st_value > sq->st_value ? 1 : (sp->st_value < sq->st_value ? -1 : 0);
1784 }
1785
1786 static void
1787 update_all_relocations (size_t nentries)
1788 {
1789 size_t sz;
1790
1791 if (!do_got_section_contents)
1792 return;
1793
1794 if (!all_relocations_root)
1795 {
1796 sz = nentries * sizeof (elf_relocation);
1797 all_relocations_root = (elf_relocation *) xmalloc (sz);
1798 all_relocations = all_relocations_root;
1799 all_relocations_count = nentries;
1800 }
1801 else
1802 {
1803 size_t orig_count = all_relocations_count;
1804 sz = (orig_count + nentries) * sizeof (elf_relocation);
1805 all_relocations_root = (elf_relocation *)
1806 xrealloc (all_relocations_root, sz);
1807 all_relocations = all_relocations_root + orig_count;
1808 all_relocations_count += nentries;
1809 }
1810 }
1811
1812 static uint64_t
1813 count_relr_relocations (Filedata * filedata,
1814 Elf_Internal_Shdr * section,
1815 uint64_t ** relrs_p)
1816 {
1817 uint64_t * relrs;
1818 uint64_t nentries;
1819 uint64_t i;
1820 uint64_t count;
1821 int entsize;
1822
1823 if (section == NULL
1824 || section->sh_type != SHT_RELR
1825 || section->sh_size == 0)
1826 return 0;
1827
1828 entsize = section->sh_entsize;
1829 if (entsize == 0)
1830 entsize = is_32bit_elf
1831 ? sizeof (Elf32_External_Relr) : sizeof (Elf64_External_Relr);
1832 else if (entsize != sizeof (Elf32_External_Relr)
1833 && entsize != sizeof (Elf64_External_Relr))
1834 return 0;
1835
1836 nentries = section->sh_size / entsize;
1837 if (nentries == 0)
1838 return 0;
1839
1840 relrs = get_data (NULL, filedata, section->sh_offset, 1,
1841 section->sh_size, _("RELR relocation data"));
1842 if (relrs == NULL)
1843 return 0;
1844
1845 for (count = i = 0; i < nentries; i++)
1846 {
1847 uint64_t entry;
1848
1849 if (entsize == sizeof (Elf32_External_Relr))
1850 entry = BYTE_GET (((Elf32_External_Relr *)relrs)[i].r_data);
1851 else
1852 entry = BYTE_GET (((Elf64_External_Relr *)relrs)[i].r_data);
1853
1854 if ((entry & 1) == 0)
1855 {
1856 ++ count;
1857 }
1858 else
1859 {
1860 if (entry == 1)
1861 continue;
1862
1863 for (; entry >>= 1;)
1864 if ((entry & 1) == 1)
1865 ++ count;
1866 }
1867 }
1868
1869 *relrs_p = relrs;
1870
1871 return count;
1872 }
1873
1874 /* If DUMP_RELOC is false, don't display RELR relocations, just collect
1875 RELR relocations for displaying GOT section contents later. */
1876
1877 static bool
1878 dump_relr_relocations (Filedata * filedata,
1879 uint64_t relr_size,
1880 int relr_entsize,
1881 uint64_t relr_offset,
1882 uint64_t * relrs,
1883 const Elf_Internal_Sym * symtab_p,
1884 uint64_t nsyms,
1885 char * strtab,
1886 uint64_t strtablen,
1887 bool dump_reloc)
1888 {
1889 uint64_t nentries, i;
1890 uint64_t where = 0;
1891 int num_bits_in_entry;
1892
1893 if (relr_entsize == 0)
1894 relr_entsize = is_32bit_elf
1895 ? sizeof (Elf32_External_Relr) : sizeof (Elf64_External_Relr);
1896
1897 nentries = relr_size / relr_entsize;
1898
1899 if (nentries == 0)
1900 return true;
1901
1902 if (relr_entsize == sizeof (Elf32_External_Relr))
1903 num_bits_in_entry = 31;
1904 else if (relr_entsize == sizeof (Elf64_External_Relr))
1905 num_bits_in_entry = 63;
1906 else
1907 {
1908 if (dump_reloc)
1909 warn (_("Unexpected entsize for RELR section\n"));
1910 return false;
1911 }
1912
1913 uint64_t *alloc_relrs = NULL;
1914 if (relrs == NULL)
1915 {
1916 relrs = get_data (NULL, filedata, relr_offset, 1, relr_size,
1917 _("RELR relocation data"));
1918 if (relrs == NULL)
1919 return false;
1920 alloc_relrs = relrs;
1921 }
1922
1923 /* Paranoia. */
1924 if (strtab == NULL)
1925 strtablen = 0;
1926 if (symtab_p == NULL)
1927 nsyms = 0;
1928
1929 const char *rtype = NULL;
1930 if (do_got_section_contents)
1931 switch (filedata->file_header.e_machine)
1932 {
1933 default:
1934 abort ();
1935
1936 case EM_386:
1937 case EM_IAMCU:
1938 rtype = "R_386_RELATIVE";
1939 break;
1940
1941 case EM_68K:
1942 rtype = "R_68K_RELATIVE";
1943 break;
1944
1945 case EM_860:
1946 rtype = "R_860_RELATIVE";
1947 break;
1948
1949 case EM_AARCH64:
1950 rtype = "R_AARCH64_RELATIVE";
1951 break;
1952
1953 case EM_AMDGPU:
1954 rtype = "R_AMDGPU_RELATIVE64";
1955 break;
1956
1957 case EM_ALPHA:
1958 rtype = "R_ALPHA_RELATIVE";
1959 break;
1960
1961 case EM_ALTERA_NIOS2:
1962 rtype = "R_NIOS2_RELATIVE";
1963 break;
1964
1965 case EM_ARM:
1966 rtype = "R_ARM_RELATIVE";
1967 break;
1968
1969 case EM_ARC:
1970 case EM_ARC_COMPACT:
1971 case EM_ARC_COMPACT2:
1972 case EM_ARC_COMPACT3:
1973 case EM_ARC_COMPACT3_64:
1974 rtype = "R_ARC_RELATIVE";
1975 break;
1976
1977 case EM_CRIS:
1978 rtype = "R_CRIS_RELATIVE";
1979 break;
1980
1981 case EM_CSKY:
1982 rtype = "R_CKCORE_RELATIVE";
1983 break;
1984
1985 case EM_KVX:
1986 rtype = "R_KVX_RELATIVE";
1987 break;
1988
1989 case EM_LATTICEMICO32:
1990 rtype = "R_LM32_RELATIVE";
1991 break;
1992
1993 case EM_LOONGARCH:
1994 rtype = "R_LARCH_RELATIVE";
1995 break;
1996
1997 case EM_M32R:
1998 case EM_CYGNUS_M32R:
1999 rtype = "R_M32R_RELATIVE";
2000 break;
2001
2002 case EM_MCORE:
2003 rtype = "R_MCORE_RELATIVE";
2004 break;
2005
2006 case EM_METAG:
2007 rtype = "R_METAG_RELATIVE";
2008 break;
2009
2010 case EM_MN10300:
2011 case EM_CYGNUS_MN10300:
2012 rtype = "R_MN10300_RELATIVE";
2013 break;
2014
2015 case EM_NDS32:
2016 rtype = "R_NDS32_RELATIVE";
2017 break;
2018
2019 case EM_OR1K:
2020 rtype = "R_OR1K_RELATIVE";
2021 break;
2022
2023 case EM_PPC:
2024 rtype = "R_PPC_RELATIVE";
2025 break;
2026
2027 case EM_PPC64:
2028 rtype = "R_PPC64_RELATIVE";
2029 break;
2030
2031 case EM_RISCV:
2032 rtype = "R_RISCV_RELATIVE";
2033 break;
2034
2035 case EM_S370:
2036 rtype = "R_I370_RELATIVE";
2037 break;
2038
2039 case EM_S390_OLD:
2040 case EM_S390:
2041 rtype = "R_390_RELATIVE";
2042 break;
2043
2044 case EM_SH:
2045 rtype = "R_SH_RELATIVE";
2046 break;
2047
2048 case EM_OLD_SPARCV9:
2049 case EM_SPARC32PLUS:
2050 case EM_SPARCV9:
2051 case EM_SPARC:
2052 rtype = "R_SPARC_RELATIVE";
2053 break;
2054
2055 case EM_TILEGX:
2056 rtype = "R_TILEGX_RELATIVE";
2057 break;
2058
2059 case EM_TILEPRO:
2060 rtype = "R_TILEPRO_RELATIVE";
2061 break;
2062
2063 case EM_V850:
2064 case EM_CYGNUS_V850:
2065 rtype = "R_V850_RELATIVE";
2066 break;
2067
2068 case EM_VAX:
2069 rtype = "R_VAX_RELATIVE";
2070 break;
2071
2072 case EM_X86_64:
2073 case EM_L1OM:
2074 case EM_K1OM:
2075 rtype = "R_X86_64_RELATIVE";
2076 break;
2077
2078 case EM_XTENSA_OLD:
2079 case EM_XTENSA:
2080 rtype = "R_XTENSA_RELATIVE";
2081 break;
2082 }
2083
2084 Elf_Internal_Sym *symtab = NULL;
2085 if (symtab_p != NULL)
2086 {
2087 /* Symbol tables are not sorted on address, but we want a quick lookup
2088 for the symbol associated with each address computed below, so sort
2089 the table then filter out unwanted entries. */
2090 size_t sz = nsyms * sizeof (*symtab);
2091 symtab = xmemdup (symtab_p, sz, sz);
2092 qsort (symtab, nsyms, sizeof (Elf_Internal_Sym), symcmp);
2093 nsyms = filter_display_syms (filedata, symtab, nsyms, strtab, strtablen);
2094 }
2095
2096 if (dump_reloc)
2097 {
2098 if (relr_entsize == sizeof (Elf32_External_Relr))
2099 printf (_ ("Index: Entry Address Symbolic Address\n"));
2100 else
2101 printf (_ ("Index: Entry Address Symbolic Address\n"));
2102 }
2103
2104 uint64_t r = 0;
2105 for (i = 0; i < nentries; i++)
2106 {
2107 uint64_t entry;
2108
2109 if (relr_entsize == sizeof (Elf32_External_Relr))
2110 entry = BYTE_GET (((Elf32_External_Relr *)relrs)[i].r_data);
2111 else
2112 entry = BYTE_GET (((Elf64_External_Relr *)relrs)[i].r_data);
2113
2114 if (dump_reloc)
2115 {
2116 /* We assume that there will never be more than 9999
2117 entries. */
2118 printf (_("%04u: "), (unsigned int) i);
2119 print_vma (entry, ZERO_HEX);
2120 printf (" ");
2121 }
2122
2123 if ((entry & 1) == 0)
2124 {
2125 where = entry;
2126 if (dump_reloc)
2127 {
2128 print_relr_addr_and_sym (filedata, symtab, nsyms, strtab,
2129 where);
2130 printf ("\n");
2131 }
2132
2133 if (do_got_section_contents)
2134 {
2135 all_relocations[r].r_offset = where;
2136 all_relocations[r].r_name = rtype;
2137 all_relocations[r].r_symbol = NULL;
2138 all_relocations[r].r_type = reltype_relr;
2139 r++;
2140 }
2141
2142 where += relr_entsize;
2143 }
2144 else
2145 {
2146 bool first = true;
2147 int j;
2148
2149 /* The least significant bit is ignored. */
2150 if (entry == 1)
2151 /* This can actually happen when the linker is allowed to shrink
2152 RELR sections. For more details see: https://reviews.llvm.org/D67164. */
2153 continue;
2154 else if (dump_reloc && i == 0)
2155 warn (_("Unusual RELR bitmap - no previous entry to set the base address\n"));
2156
2157 for (j = 0; entry >>= 1; j++)
2158 if ((entry & 1) == 1)
2159 {
2160 uint64_t addr = where + (j * relr_entsize);
2161
2162 if (dump_reloc)
2163 {
2164 if (first)
2165 {
2166 print_relr_addr_and_sym (filedata, symtab, nsyms,
2167 strtab, addr);
2168 first = false;
2169 }
2170 else
2171 {
2172 printf (_("\n%*s "),
2173 relr_entsize == 4 ? 15 : 23, " ");
2174 print_relr_addr_and_sym (filedata, symtab, nsyms,
2175 strtab, addr);
2176 }
2177 }
2178
2179 if (do_got_section_contents)
2180 {
2181 all_relocations[r].r_offset = addr;
2182 all_relocations[r].r_name = rtype;
2183 all_relocations[r].r_symbol = NULL;
2184 all_relocations[r].r_type = reltype_relr;
2185 r++;
2186 }
2187 }
2188
2189 if (dump_reloc)
2190 printf ("\n");
2191 where += num_bits_in_entry * relr_entsize;
2192 }
2193 }
2194
2195 free (symtab);
2196 free (alloc_relrs);
2197 return true;
2198 }
2199
2200 /* Display the contents of the relocation data found at the specified
2201 offset. If DUMP_RELOC is false, don't display relocations, just
2202 collect relocations for displaying GOT section contents later. */
2203
2204 static bool
2205 dump_relocations (Filedata * filedata,
2206 uint64_t rel_offset,
2207 uint64_t rel_size,
2208 Elf_Internal_Sym * symtab,
2209 uint64_t nsyms,
2210 char * strtab,
2211 uint64_t strtablen,
2212 relocation_type rel_type,
2213 bool is_dynsym,
2214 bool dump_reloc)
2215 {
2216 size_t i;
2217 Elf_Internal_Rela * rels;
2218 bool res = true;
2219
2220 if (rel_type == reltype_unknown)
2221 rel_type = guess_is_rela (filedata->file_header.e_machine) ? reltype_rela : reltype_rel;
2222
2223 if (rel_type == reltype_rela)
2224 {
2225 if (!slurp_rela_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
2226 return false;
2227 }
2228 else if (rel_type == reltype_rel)
2229 {
2230 if (!slurp_rel_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
2231 return false;
2232 }
2233 else if (rel_type == reltype_relr)
2234 {
2235 /* This should have been handled by display_relocations(). */
2236 return false;
2237 }
2238
2239 if (dump_reloc)
2240 {
2241 if (is_32bit_elf)
2242 {
2243 if (rel_type == reltype_rela)
2244 {
2245 if (do_wide)
2246 printf (_(" Offset Info Type Sym. Value Symbol's Name + Addend\n"));
2247 else
2248 printf (_(" Offset Info Type Sym.Value Sym. Name + Addend\n"));
2249 }
2250 else
2251 {
2252 if (do_wide)
2253 printf (_(" Offset Info Type Sym. Value Symbol's Name\n"));
2254 else
2255 printf (_(" Offset Info Type Sym.Value Sym. Name\n"));
2256 }
2257 }
2258 else if (rel_type == reltype_rela)
2259 {
2260 if (do_wide)
2261 printf (_(" Offset Info Type Symbol's Value Symbol's Name + Addend\n"));
2262 else
2263 printf (_(" Offset Info Type Sym. Value Sym. Name + Addend\n"));
2264 }
2265 else
2266 {
2267 if (do_wide)
2268 printf (_(" Offset Info Type Symbol's Value Symbol's Name\n"));
2269 else
2270 printf (_(" Offset Info Type Sym. Value Sym. Name\n"));
2271 }
2272 }
2273
2274 for (i = 0; i < rel_size; i++)
2275 {
2276 const char * rtype;
2277 uint64_t offset;
2278 uint64_t inf;
2279 uint64_t symtab_index;
2280 uint64_t type;
2281
2282 offset = rels[i].r_offset;
2283 inf = rels[i].r_info;
2284
2285 type = get_reloc_type (filedata, inf);
2286 symtab_index = get_reloc_symindex (inf);
2287
2288 if (dump_reloc)
2289 {
2290 if (is_32bit_elf)
2291 printf ("%8.8" PRIx32 " %8.8" PRIx32 " ",
2292 (uint32_t) offset, (uint32_t) inf);
2293 else
2294 printf (do_wide
2295 ? "%16.16" PRIx64 " %16.16" PRIx64 " "
2296 : "%12.12" PRIx64 " %12.12" PRIx64 " ",
2297 offset, inf);
2298 }
2299
2300 switch (filedata->file_header.e_machine)
2301 {
2302 default:
2303 rtype = NULL;
2304 break;
2305
2306 case EM_AARCH64:
2307 rtype = elf_aarch64_reloc_type (type);
2308 break;
2309
2310 case EM_M32R:
2311 case EM_CYGNUS_M32R:
2312 rtype = elf_m32r_reloc_type (type);
2313 break;
2314
2315 case EM_386:
2316 case EM_IAMCU:
2317 rtype = elf_i386_reloc_type (type);
2318 break;
2319
2320 case EM_68HC11:
2321 case EM_68HC12:
2322 rtype = elf_m68hc11_reloc_type (type);
2323 break;
2324
2325 case EM_S12Z:
2326 rtype = elf_s12z_reloc_type (type);
2327 break;
2328
2329 case EM_68K:
2330 rtype = elf_m68k_reloc_type (type);
2331 break;
2332
2333 case EM_960:
2334 rtype = elf_i960_reloc_type (type);
2335 break;
2336
2337 case EM_AVR:
2338 case EM_AVR_OLD:
2339 rtype = elf_avr_reloc_type (type);
2340 break;
2341
2342 case EM_OLD_SPARCV9:
2343 case EM_SPARC32PLUS:
2344 case EM_SPARCV9:
2345 case EM_SPARC:
2346 rtype = elf_sparc_reloc_type (type);
2347 break;
2348
2349 case EM_SPU:
2350 rtype = elf_spu_reloc_type (type);
2351 break;
2352
2353 case EM_V800:
2354 rtype = v800_reloc_type (type);
2355 break;
2356 case EM_V850:
2357 case EM_CYGNUS_V850:
2358 rtype = v850_reloc_type (type);
2359 break;
2360
2361 case EM_D10V:
2362 case EM_CYGNUS_D10V:
2363 rtype = elf_d10v_reloc_type (type);
2364 break;
2365
2366 case EM_D30V:
2367 case EM_CYGNUS_D30V:
2368 rtype = elf_d30v_reloc_type (type);
2369 break;
2370
2371 case EM_DLX:
2372 rtype = elf_dlx_reloc_type (type);
2373 break;
2374
2375 case EM_SH:
2376 rtype = elf_sh_reloc_type (type);
2377 break;
2378
2379 case EM_MN10300:
2380 case EM_CYGNUS_MN10300:
2381 rtype = elf_mn10300_reloc_type (type);
2382 break;
2383
2384 case EM_MN10200:
2385 case EM_CYGNUS_MN10200:
2386 rtype = elf_mn10200_reloc_type (type);
2387 break;
2388
2389 case EM_FR30:
2390 case EM_CYGNUS_FR30:
2391 rtype = elf_fr30_reloc_type (type);
2392 break;
2393
2394 case EM_CYGNUS_FRV:
2395 rtype = elf_frv_reloc_type (type);
2396 break;
2397
2398 case EM_CSKY:
2399 rtype = elf_csky_reloc_type (type);
2400 break;
2401
2402 case EM_FT32:
2403 rtype = elf_ft32_reloc_type (type);
2404 break;
2405
2406 case EM_MCORE:
2407 rtype = elf_mcore_reloc_type (type);
2408 break;
2409
2410 case EM_MMIX:
2411 rtype = elf_mmix_reloc_type (type);
2412 break;
2413
2414 case EM_MOXIE:
2415 rtype = elf_moxie_reloc_type (type);
2416 break;
2417
2418 case EM_MSP430:
2419 if (uses_msp430x_relocs (filedata))
2420 {
2421 rtype = elf_msp430x_reloc_type (type);
2422 break;
2423 }
2424 /* Fall through. */
2425 case EM_MSP430_OLD:
2426 rtype = elf_msp430_reloc_type (type);
2427 break;
2428
2429 case EM_NDS32:
2430 rtype = elf_nds32_reloc_type (type);
2431 break;
2432
2433 case EM_PPC:
2434 rtype = elf_ppc_reloc_type (type);
2435 break;
2436
2437 case EM_PPC64:
2438 rtype = elf_ppc64_reloc_type (type);
2439 break;
2440
2441 case EM_MIPS:
2442 case EM_MIPS_RS3_LE:
2443 rtype = elf_mips_reloc_type (type);
2444 break;
2445
2446 case EM_RISCV:
2447 rtype = elf_riscv_reloc_type (type);
2448 break;
2449
2450 case EM_ALPHA:
2451 rtype = elf_alpha_reloc_type (type);
2452 break;
2453
2454 case EM_ARM:
2455 rtype = elf_arm_reloc_type (type);
2456 break;
2457
2458 case EM_ARC:
2459 case EM_ARC_COMPACT:
2460 case EM_ARC_COMPACT2:
2461 case EM_ARC_COMPACT3:
2462 case EM_ARC_COMPACT3_64:
2463 rtype = elf_arc_reloc_type (type);
2464 break;
2465
2466 case EM_PARISC:
2467 rtype = elf_hppa_reloc_type (type);
2468 break;
2469
2470 case EM_H8_300:
2471 case EM_H8_300H:
2472 case EM_H8S:
2473 rtype = elf_h8_reloc_type (type);
2474 break;
2475
2476 case EM_OR1K:
2477 rtype = elf_or1k_reloc_type (type);
2478 break;
2479
2480 case EM_PJ:
2481 case EM_PJ_OLD:
2482 rtype = elf_pj_reloc_type (type);
2483 break;
2484 case EM_IA_64:
2485 rtype = elf_ia64_reloc_type (type);
2486 break;
2487
2488 case EM_KVX:
2489 rtype = elf_kvx_reloc_type (type);
2490 break;
2491
2492 case EM_CRIS:
2493 rtype = elf_cris_reloc_type (type);
2494 break;
2495
2496 case EM_860:
2497 rtype = elf_i860_reloc_type (type);
2498 break;
2499
2500 case EM_X86_64:
2501 case EM_L1OM:
2502 case EM_K1OM:
2503 rtype = elf_x86_64_reloc_type (type);
2504 break;
2505
2506 case EM_S370:
2507 rtype = i370_reloc_type (type);
2508 break;
2509
2510 case EM_S390_OLD:
2511 case EM_S390:
2512 rtype = elf_s390_reloc_type (type);
2513 break;
2514
2515 case EM_SCORE:
2516 rtype = elf_score_reloc_type (type);
2517 break;
2518
2519 case EM_XSTORMY16:
2520 rtype = elf_xstormy16_reloc_type (type);
2521 break;
2522
2523 case EM_CRX:
2524 rtype = elf_crx_reloc_type (type);
2525 break;
2526
2527 case EM_VAX:
2528 rtype = elf_vax_reloc_type (type);
2529 break;
2530
2531 case EM_VISIUM:
2532 rtype = elf_visium_reloc_type (type);
2533 break;
2534
2535 case EM_BPF:
2536 rtype = elf_bpf_reloc_type (type);
2537 break;
2538
2539 case EM_ADAPTEVA_EPIPHANY:
2540 rtype = elf_epiphany_reloc_type (type);
2541 break;
2542
2543 case EM_IP2K:
2544 case EM_IP2K_OLD:
2545 rtype = elf_ip2k_reloc_type (type);
2546 break;
2547
2548 case EM_IQ2000:
2549 rtype = elf_iq2000_reloc_type (type);
2550 break;
2551
2552 case EM_XTENSA_OLD:
2553 case EM_XTENSA:
2554 rtype = elf_xtensa_reloc_type (type);
2555 break;
2556
2557 case EM_LATTICEMICO32:
2558 rtype = elf_lm32_reloc_type (type);
2559 break;
2560
2561 case EM_M32C_OLD:
2562 case EM_M32C:
2563 rtype = elf_m32c_reloc_type (type);
2564 break;
2565
2566 case EM_MT:
2567 rtype = elf_mt_reloc_type (type);
2568 break;
2569
2570 case EM_BLACKFIN:
2571 rtype = elf_bfin_reloc_type (type);
2572 break;
2573
2574 case EM_CYGNUS_MEP:
2575 rtype = elf_mep_reloc_type (type);
2576 break;
2577
2578 case EM_CR16:
2579 rtype = elf_cr16_reloc_type (type);
2580 break;
2581
2582 case EM_MICROBLAZE:
2583 case EM_MICROBLAZE_OLD:
2584 rtype = elf_microblaze_reloc_type (type);
2585 break;
2586
2587 case EM_RL78:
2588 rtype = elf_rl78_reloc_type (type);
2589 break;
2590
2591 case EM_RX:
2592 rtype = elf_rx_reloc_type (type);
2593 break;
2594
2595 case EM_METAG:
2596 rtype = elf_metag_reloc_type (type);
2597 break;
2598
2599 case EM_TI_C6000:
2600 rtype = elf_tic6x_reloc_type (type);
2601 break;
2602
2603 case EM_TILEGX:
2604 rtype = elf_tilegx_reloc_type (type);
2605 break;
2606
2607 case EM_TILEPRO:
2608 rtype = elf_tilepro_reloc_type (type);
2609 break;
2610
2611 case EM_WEBASSEMBLY:
2612 rtype = elf_wasm32_reloc_type (type);
2613 break;
2614
2615 case EM_XGATE:
2616 rtype = elf_xgate_reloc_type (type);
2617 break;
2618
2619 case EM_ALTERA_NIOS2:
2620 rtype = elf_nios2_reloc_type (type);
2621 break;
2622
2623 case EM_TI_PRU:
2624 rtype = elf_pru_reloc_type (type);
2625 break;
2626
2627 case EM_NFP:
2628 if (EF_NFP_MACH (filedata->file_header.e_flags) == E_NFP_MACH_3200)
2629 rtype = elf_nfp3200_reloc_type (type);
2630 else
2631 rtype = elf_nfp_reloc_type (type);
2632 break;
2633
2634 case EM_Z80:
2635 rtype = elf_z80_reloc_type (type);
2636 break;
2637
2638 case EM_LOONGARCH:
2639 rtype = elf_loongarch_reloc_type (type);
2640 break;
2641
2642 case EM_AMDGPU:
2643 rtype = elf_amdgpu_reloc_type (type);
2644 break;
2645 }
2646
2647 char *symbol_name = NULL;
2648 if (dump_reloc)
2649 {
2650 if (rtype == NULL)
2651 printf (_("unrecognized: %-7lx"),
2652 (unsigned long) type & 0xffffffff);
2653 else
2654 printf (do_wide ? "%-22s" : "%-17.17s", rtype);
2655 }
2656
2657 if (filedata->file_header.e_machine == EM_ALPHA
2658 && rtype != NULL
2659 && streq (rtype, "R_ALPHA_LITUSE")
2660 && rel_type == reltype_rela)
2661 {
2662 switch (rels[i].r_addend)
2663 {
2664 case LITUSE_ALPHA_ADDR: rtype = "ADDR"; break;
2665 case LITUSE_ALPHA_BASE: rtype = "BASE"; break;
2666 case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
2667 case LITUSE_ALPHA_JSR: rtype = "JSR"; break;
2668 case LITUSE_ALPHA_TLSGD: rtype = "TLSGD"; break;
2669 case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
2670 case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
2671 default: rtype = NULL;
2672 }
2673
2674 if (dump_reloc)
2675 {
2676 if (rtype)
2677 printf (" (%s)", rtype);
2678 else
2679 {
2680 putchar (' ');
2681 printf (_("<unknown addend: %" PRIx64 ">"),
2682 rels[i].r_addend);
2683 res = false;
2684 }
2685 }
2686 }
2687 else if (symtab_index)
2688 {
2689 if (symtab == NULL || symtab_index >= nsyms)
2690 {
2691 if (dump_reloc)
2692 {
2693 error (_(" bad symbol index: %08lx in reloc\n"),
2694 (unsigned long) symtab_index);
2695 res = false;
2696 }
2697 }
2698 else
2699 {
2700 Elf_Internal_Sym * psym;
2701 const char * version_string;
2702 enum versioned_symbol_info sym_info;
2703 unsigned short vna_other;
2704
2705 psym = symtab + symtab_index;
2706
2707 version_string
2708 = get_symbol_version_string (filedata, is_dynsym,
2709 strtab, strtablen,
2710 symtab_index,
2711 psym,
2712 &sym_info,
2713 &vna_other);
2714
2715 if (dump_reloc)
2716 printf (" ");
2717
2718 if (ELF_ST_TYPE (psym->st_info) == STT_GNU_IFUNC)
2719 {
2720 const char * name;
2721 unsigned int len;
2722 unsigned int width = is_32bit_elf ? 8 : 14;
2723
2724 /* Relocations against GNU_IFUNC symbols do not use the value
2725 of the symbol as the address to relocate against. Instead
2726 they invoke the function named by the symbol and use its
2727 result as the address for relocation.
2728
2729 To indicate this to the user, do not display the value of
2730 the symbol in the "Symbols's Value" field. Instead show
2731 its name followed by () as a hint that the symbol is
2732 invoked. */
2733
2734 if (strtab == NULL
2735 || psym->st_name == 0
2736 || psym->st_name >= strtablen)
2737 name = "??";
2738 else
2739 name = strtab + psym->st_name;
2740
2741 if (do_got_section_contents)
2742 {
2743 if (version_string)
2744 symbol_name = concat (name,
2745 sym_info == symbol_public
2746 ? "@@" : "@",
2747 version_string, NULL);
2748 else
2749 symbol_name = xstrdup (name);
2750 }
2751
2752 if (dump_reloc)
2753 {
2754 len = print_symbol_name (width, name);
2755 if (version_string)
2756 printf (sym_info == symbol_public
2757 ? "@@%s" : "@%s",
2758 version_string);
2759 printf ("()%-*s",
2760 len <= width ? (width + 1) - len : 1,
2761 " ");
2762 }
2763 }
2764 else if (dump_reloc)
2765 {
2766 print_vma (psym->st_value, LONG_HEX);
2767
2768 printf (is_32bit_elf ? " " : " ");
2769 }
2770
2771 if (psym->st_name == 0)
2772 {
2773 const char * sec_name = "<null>";
2774
2775 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
2776 sec_name = printable_section_name_from_index
2777 (filedata, psym->st_shndx, NULL);
2778
2779 if (do_got_section_contents)
2780 symbol_name = xstrdup (sec_name);
2781 if (dump_reloc)
2782 print_symbol_name (22, sec_name);
2783 }
2784 else if (strtab == NULL)
2785 {
2786 if (dump_reloc)
2787 printf (_("<string table index: %3ld>"),
2788 psym->st_name);
2789 }
2790 else if (psym->st_name >= strtablen)
2791 {
2792 if (dump_reloc)
2793 {
2794 error (_("<corrupt string table index: %3ld>\n"),
2795 psym->st_name);
2796 res = false;
2797 }
2798 }
2799 else
2800 {
2801 if (dump_reloc)
2802 {
2803 print_symbol_name (22, strtab + psym->st_name);
2804 if (version_string)
2805 printf (sym_info == symbol_public
2806 ? "@@%s" : "@%s",
2807 version_string);
2808 }
2809 if (do_got_section_contents)
2810 {
2811 if (version_string)
2812 symbol_name = concat (strtab + psym->st_name,
2813 sym_info == symbol_public
2814 ? "@@" : "@",
2815 version_string, NULL);
2816 else
2817 symbol_name = xstrdup (strtab + psym->st_name);
2818 }
2819 }
2820
2821 if (dump_reloc && rel_type == reltype_rela)
2822 {
2823 uint64_t off = rels[i].r_addend;
2824
2825 if ((int64_t) off < 0)
2826 printf (" - %" PRIx64, -off);
2827 else
2828 printf (" + %" PRIx64, off);
2829 }
2830 }
2831 }
2832 else if (dump_reloc && rel_type == reltype_rela)
2833 {
2834 uint64_t off = rels[i].r_addend;
2835
2836 printf ("%*c", is_32bit_elf ? 12 : 20, ' ');
2837 if ((int64_t) off < 0)
2838 printf ("-%" PRIx64, -off);
2839 else
2840 printf ("%" PRIx64, off);
2841 }
2842
2843 if (do_got_section_contents)
2844 {
2845 all_relocations[i].r_offset = offset;
2846 all_relocations[i].r_name = rtype;
2847 all_relocations[i].r_symbol = symbol_name;
2848 all_relocations[i].r_addend = rels[i].r_addend;
2849 all_relocations[i].r_type = rel_type;
2850 }
2851
2852 if (dump_reloc)
2853 {
2854 if (filedata->file_header.e_machine == EM_SPARCV9
2855 && rtype != NULL
2856 && streq (rtype, "R_SPARC_OLO10"))
2857 printf (" + %" PRIx64, ELF64_R_TYPE_DATA (inf));
2858
2859 putchar ('\n');
2860
2861 if (! is_32bit_elf
2862 && filedata->file_header.e_machine == EM_MIPS)
2863 {
2864 uint64_t type2 = ELF64_MIPS_R_TYPE2 (inf);
2865 uint64_t type3 = ELF64_MIPS_R_TYPE3 (inf);
2866 const char * rtype2 = elf_mips_reloc_type (type2);
2867 const char * rtype3 = elf_mips_reloc_type (type3);
2868
2869 printf (" Type2: ");
2870
2871 if (rtype2 == NULL)
2872 printf (_("unrecognized: %-7lx"),
2873 (unsigned long) type2 & 0xffffffff);
2874 else
2875 printf ("%-17.17s", rtype2);
2876
2877 printf ("\n Type3: ");
2878
2879 if (rtype3 == NULL)
2880 printf (_("unrecognized: %-7lx"),
2881 (unsigned long) type3 & 0xffffffff);
2882 else
2883 printf ("%-17.17s", rtype3);
2884
2885 putchar ('\n');
2886 }
2887 }
2888 }
2889
2890 free (rels);
2891
2892 return res;
2893 }
2894
2895 static const char *
2896 get_aarch64_dynamic_type (unsigned long type)
2897 {
2898 switch (type)
2899 {
2900 case DT_AARCH64_BTI_PLT: return "AARCH64_BTI_PLT";
2901 case DT_AARCH64_PAC_PLT: return "AARCH64_PAC_PLT";
2902 case DT_AARCH64_VARIANT_PCS: return "AARCH64_VARIANT_PCS";
2903 case DT_AARCH64_MEMTAG_MODE: return "AARCH64_MEMTAG_MODE";
2904 case DT_AARCH64_MEMTAG_STACK: return "AARCH64_MEMTAG_STACK";
2905
2906 default:
2907 return NULL;
2908 }
2909 }
2910
2911 static const char *
2912 get_mips_dynamic_type (unsigned long type)
2913 {
2914 switch (type)
2915 {
2916 case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
2917 case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
2918 case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
2919 case DT_MIPS_IVERSION: return "MIPS_IVERSION";
2920 case DT_MIPS_FLAGS: return "MIPS_FLAGS";
2921 case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
2922 case DT_MIPS_MSYM: return "MIPS_MSYM";
2923 case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
2924 case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
2925 case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
2926 case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
2927 case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
2928 case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
2929 case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
2930 case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
2931 case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
2932 case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
2933 case DT_MIPS_RLD_MAP_REL: return "MIPS_RLD_MAP_REL";
2934 case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
2935 case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
2936 case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
2937 case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
2938 case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
2939 case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
2940 case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
2941 case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
2942 case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
2943 case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
2944 case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
2945 case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
2946 case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
2947 case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
2948 case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
2949 case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
2950 case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
2951 case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
2952 case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
2953 case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
2954 case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
2955 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
2956 case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
2957 case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
2958 case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
2959 case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
2960 case DT_MIPS_PLTGOT: return "MIPS_PLTGOT";
2961 case DT_MIPS_RWPLT: return "MIPS_RWPLT";
2962 case DT_MIPS_XHASH: return "MIPS_XHASH";
2963 default:
2964 return NULL;
2965 }
2966 }
2967
2968 static const char *
2969 get_sparc64_dynamic_type (unsigned long type)
2970 {
2971 switch (type)
2972 {
2973 case DT_SPARC_REGISTER: return "SPARC_REGISTER";
2974 default:
2975 return NULL;
2976 }
2977 }
2978
2979 static const char *
2980 get_ppc_dynamic_type (unsigned long type)
2981 {
2982 switch (type)
2983 {
2984 case DT_PPC_GOT: return "PPC_GOT";
2985 case DT_PPC_OPT: return "PPC_OPT";
2986 default:
2987 return NULL;
2988 }
2989 }
2990
2991 static const char *
2992 get_ppc64_dynamic_type (unsigned long type)
2993 {
2994 switch (type)
2995 {
2996 case DT_PPC64_GLINK: return "PPC64_GLINK";
2997 case DT_PPC64_OPD: return "PPC64_OPD";
2998 case DT_PPC64_OPDSZ: return "PPC64_OPDSZ";
2999 case DT_PPC64_OPT: return "PPC64_OPT";
3000 default:
3001 return NULL;
3002 }
3003 }
3004
3005 static const char *
3006 get_parisc_dynamic_type (unsigned long type)
3007 {
3008 switch (type)
3009 {
3010 case DT_HP_LOAD_MAP: return "HP_LOAD_MAP";
3011 case DT_HP_DLD_FLAGS: return "HP_DLD_FLAGS";
3012 case DT_HP_DLD_HOOK: return "HP_DLD_HOOK";
3013 case DT_HP_UX10_INIT: return "HP_UX10_INIT";
3014 case DT_HP_UX10_INITSZ: return "HP_UX10_INITSZ";
3015 case DT_HP_PREINIT: return "HP_PREINIT";
3016 case DT_HP_PREINITSZ: return "HP_PREINITSZ";
3017 case DT_HP_NEEDED: return "HP_NEEDED";
3018 case DT_HP_TIME_STAMP: return "HP_TIME_STAMP";
3019 case DT_HP_CHECKSUM: return "HP_CHECKSUM";
3020 case DT_HP_GST_SIZE: return "HP_GST_SIZE";
3021 case DT_HP_GST_VERSION: return "HP_GST_VERSION";
3022 case DT_HP_GST_HASHVAL: return "HP_GST_HASHVAL";
3023 case DT_HP_EPLTREL: return "HP_GST_EPLTREL";
3024 case DT_HP_EPLTRELSZ: return "HP_GST_EPLTRELSZ";
3025 case DT_HP_FILTERED: return "HP_FILTERED";
3026 case DT_HP_FILTER_TLS: return "HP_FILTER_TLS";
3027 case DT_HP_COMPAT_FILTERED: return "HP_COMPAT_FILTERED";
3028 case DT_HP_LAZYLOAD: return "HP_LAZYLOAD";
3029 case DT_HP_BIND_NOW_COUNT: return "HP_BIND_NOW_COUNT";
3030 case DT_PLT: return "PLT";
3031 case DT_PLT_SIZE: return "PLT_SIZE";
3032 case DT_DLT: return "DLT";
3033 case DT_DLT_SIZE: return "DLT_SIZE";
3034 default:
3035 return NULL;
3036 }
3037 }
3038
3039 static const char *
3040 get_ia64_dynamic_type (unsigned long type)
3041 {
3042 switch (type)
3043 {
3044 case DT_IA_64_PLT_RESERVE: return "IA_64_PLT_RESERVE";
3045 case DT_IA_64_VMS_SUBTYPE: return "VMS_SUBTYPE";
3046 case DT_IA_64_VMS_IMGIOCNT: return "VMS_IMGIOCNT";
3047 case DT_IA_64_VMS_LNKFLAGS: return "VMS_LNKFLAGS";
3048 case DT_IA_64_VMS_VIR_MEM_BLK_SIZ: return "VMS_VIR_MEM_BLK_SIZ";
3049 case DT_IA_64_VMS_IDENT: return "VMS_IDENT";
3050 case DT_IA_64_VMS_NEEDED_IDENT: return "VMS_NEEDED_IDENT";
3051 case DT_IA_64_VMS_IMG_RELA_CNT: return "VMS_IMG_RELA_CNT";
3052 case DT_IA_64_VMS_SEG_RELA_CNT: return "VMS_SEG_RELA_CNT";
3053 case DT_IA_64_VMS_FIXUP_RELA_CNT: return "VMS_FIXUP_RELA_CNT";
3054 case DT_IA_64_VMS_FIXUP_NEEDED: return "VMS_FIXUP_NEEDED";
3055 case DT_IA_64_VMS_SYMVEC_CNT: return "VMS_SYMVEC_CNT";
3056 case DT_IA_64_VMS_XLATED: return "VMS_XLATED";
3057 case DT_IA_64_VMS_STACKSIZE: return "VMS_STACKSIZE";
3058 case DT_IA_64_VMS_UNWINDSZ: return "VMS_UNWINDSZ";
3059 case DT_IA_64_VMS_UNWIND_CODSEG: return "VMS_UNWIND_CODSEG";
3060 case DT_IA_64_VMS_UNWIND_INFOSEG: return "VMS_UNWIND_INFOSEG";
3061 case DT_IA_64_VMS_LINKTIME: return "VMS_LINKTIME";
3062 case DT_IA_64_VMS_SEG_NO: return "VMS_SEG_NO";
3063 case DT_IA_64_VMS_SYMVEC_OFFSET: return "VMS_SYMVEC_OFFSET";
3064 case DT_IA_64_VMS_SYMVEC_SEG: return "VMS_SYMVEC_SEG";
3065 case DT_IA_64_VMS_UNWIND_OFFSET: return "VMS_UNWIND_OFFSET";
3066 case DT_IA_64_VMS_UNWIND_SEG: return "VMS_UNWIND_SEG";
3067 case DT_IA_64_VMS_STRTAB_OFFSET: return "VMS_STRTAB_OFFSET";
3068 case DT_IA_64_VMS_SYSVER_OFFSET: return "VMS_SYSVER_OFFSET";
3069 case DT_IA_64_VMS_IMG_RELA_OFF: return "VMS_IMG_RELA_OFF";
3070 case DT_IA_64_VMS_SEG_RELA_OFF: return "VMS_SEG_RELA_OFF";
3071 case DT_IA_64_VMS_FIXUP_RELA_OFF: return "VMS_FIXUP_RELA_OFF";
3072 case DT_IA_64_VMS_PLTGOT_OFFSET: return "VMS_PLTGOT_OFFSET";
3073 case DT_IA_64_VMS_PLTGOT_SEG: return "VMS_PLTGOT_SEG";
3074 case DT_IA_64_VMS_FPMODE: return "VMS_FPMODE";
3075 default:
3076 return NULL;
3077 }
3078 }
3079
3080 static const char *
3081 get_solaris_section_type (unsigned long type)
3082 {
3083 switch (type)
3084 {
3085 case SHT_SUNW_symtabnsort:
3086 return "SUNW_symtabnsort";
3087 case SHT_SUNW_ancillary:
3088 return "SUNW_ancillary";
3089 case SHT_SUNW_phname:
3090 return "SUNW_phname";
3091 case SHT_SUNW_capchain:
3092 return "SUNW_capchain";
3093 case SHT_SUNW_capinfo:
3094 return "SUNW_capinfo";
3095 case SHT_SUNW_symsort:
3096 return "SUNW_symsort";
3097 case SHT_SUNW_tlssort:
3098 return "SUNW_tlssort";
3099 case SHT_SUNW_LDYNSYM:
3100 return "SUNW_LDYNSYM";
3101 case SHT_SUNW_dof:
3102 return "SUNW_dof";
3103 case SHT_SUNW_cap:
3104 return "SUNW_cap";
3105 case SHT_SUNW_SIGNATURE:
3106 return "SUNW_SIGNATURE";
3107 case SHT_SUNW_ANNOTATE:
3108 return "SUNW_ANNOTATE";
3109 case SHT_SUNW_DEBUGSTR:
3110 return "SUNW_DEBUGSTR";
3111 case SHT_SUNW_DEBUG:
3112 return "SUNW_DEBUG";
3113 case SHT_SUNW_move:
3114 return "SUNW_move";
3115 case SHT_SUNW_COMDAT:
3116 return "SUNW_COMDAT";
3117 case SHT_SUNW_syminfo:
3118 return "SUNW_syminfo";
3119 case SHT_SUNW_verdef:
3120 return "SUNW_verdef";
3121 case SHT_SUNW_verneed:
3122 return "SUNW_verneed";
3123 case SHT_SUNW_versym:
3124 return "SUNW_versym";
3125 case 0x70000000:
3126 return "SPARC_GOTDATA";
3127 default:
3128 return NULL;
3129 }
3130 }
3131
3132 static const char *
3133 get_alpha_dynamic_type (unsigned long type)
3134 {
3135 switch (type)
3136 {
3137 case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
3138 default: return NULL;
3139 }
3140 }
3141
3142 static const char *
3143 get_score_dynamic_type (unsigned long type)
3144 {
3145 switch (type)
3146 {
3147 case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
3148 case DT_SCORE_LOCAL_GOTNO: return "SCORE_LOCAL_GOTNO";
3149 case DT_SCORE_SYMTABNO: return "SCORE_SYMTABNO";
3150 case DT_SCORE_GOTSYM: return "SCORE_GOTSYM";
3151 case DT_SCORE_UNREFEXTNO: return "SCORE_UNREFEXTNO";
3152 case DT_SCORE_HIPAGENO: return "SCORE_HIPAGENO";
3153 default: return NULL;
3154 }
3155 }
3156
3157 static const char *
3158 get_tic6x_dynamic_type (unsigned long type)
3159 {
3160 switch (type)
3161 {
3162 case DT_C6000_GSYM_OFFSET: return "C6000_GSYM_OFFSET";
3163 case DT_C6000_GSTR_OFFSET: return "C6000_GSTR_OFFSET";
3164 case DT_C6000_DSBT_BASE: return "C6000_DSBT_BASE";
3165 case DT_C6000_DSBT_SIZE: return "C6000_DSBT_SIZE";
3166 case DT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
3167 case DT_C6000_DSBT_INDEX: return "C6000_DSBT_INDEX";
3168 default: return NULL;
3169 }
3170 }
3171
3172 static const char *
3173 get_nios2_dynamic_type (unsigned long type)
3174 {
3175 switch (type)
3176 {
3177 case DT_NIOS2_GP: return "NIOS2_GP";
3178 default: return NULL;
3179 }
3180 }
3181
3182 static const char *
3183 get_solaris_dynamic_type (unsigned long type)
3184 {
3185 switch (type)
3186 {
3187 case 0x6000000d: return "SUNW_AUXILIARY";
3188 case 0x6000000e: return "SUNW_RTLDINF";
3189 case 0x6000000f: return "SUNW_FILTER";
3190 case 0x60000010: return "SUNW_CAP";
3191 case 0x60000011: return "SUNW_SYMTAB";
3192 case 0x60000012: return "SUNW_SYMSZ";
3193 case 0x60000013: return "SUNW_SORTENT";
3194 case 0x60000014: return "SUNW_SYMSORT";
3195 case 0x60000015: return "SUNW_SYMSORTSZ";
3196 case 0x60000016: return "SUNW_TLSSORT";
3197 case 0x60000017: return "SUNW_TLSSORTSZ";
3198 case 0x60000018: return "SUNW_CAPINFO";
3199 case 0x60000019: return "SUNW_STRPAD";
3200 case 0x6000001a: return "SUNW_CAPCHAIN";
3201 case 0x6000001b: return "SUNW_LDMACH";
3202 case 0x6000001d: return "SUNW_CAPCHAINENT";
3203 case 0x6000001f: return "SUNW_CAPCHAINSZ";
3204 case 0x60000021: return "SUNW_PARENT";
3205 case 0x60000023: return "SUNW_ASLR";
3206 case 0x60000025: return "SUNW_RELAX";
3207 case 0x60000029: return "SUNW_NXHEAP";
3208 case 0x6000002b: return "SUNW_NXSTACK";
3209
3210 case 0x70000001: return "SPARC_REGISTER";
3211 case 0x7ffffffd: return "AUXILIARY";
3212 case 0x7ffffffe: return "USED";
3213 case 0x7fffffff: return "FILTER";
3214
3215 default: return NULL;
3216 }
3217 }
3218
3219 static const char *
3220 get_riscv_dynamic_type (unsigned long type)
3221 {
3222 switch (type)
3223 {
3224 case DT_RISCV_VARIANT_CC: return "RISCV_VARIANT_CC";
3225 default:
3226 return NULL;
3227 }
3228 }
3229
3230 static const char *
3231 get_x86_64_dynamic_type (unsigned long type)
3232 {
3233 switch (type)
3234 {
3235 case DT_X86_64_PLT:
3236 return "DT_X86_64_PLT";
3237 case DT_X86_64_PLTSZ:
3238 return "DT_X86_64_PLTSZ";
3239 case DT_X86_64_PLTENT:
3240 return "DT_X86_64_PLTENT";
3241 default:
3242 return NULL;
3243 }
3244 }
3245
3246 static const char *
3247 get_dynamic_type (Filedata * filedata, unsigned long type)
3248 {
3249 static char buff[64];
3250
3251 switch (type)
3252 {
3253 case DT_NULL: return "NULL";
3254 case DT_NEEDED: return "NEEDED";
3255 case DT_PLTRELSZ: return "PLTRELSZ";
3256 case DT_PLTGOT: return "PLTGOT";
3257 case DT_HASH: return "HASH";
3258 case DT_STRTAB: return "STRTAB";
3259 case DT_SYMTAB: return "SYMTAB";
3260 case DT_RELA: return "RELA";
3261 case DT_RELASZ: return "RELASZ";
3262 case DT_RELAENT: return "RELAENT";
3263 case DT_STRSZ: return "STRSZ";
3264 case DT_SYMENT: return "SYMENT";
3265 case DT_INIT: return "INIT";
3266 case DT_FINI: return "FINI";
3267 case DT_SONAME: return "SONAME";
3268 case DT_RPATH: return "RPATH";
3269 case DT_SYMBOLIC: return "SYMBOLIC";
3270 case DT_REL: return "REL";
3271 case DT_RELSZ: return "RELSZ";
3272 case DT_RELENT: return "RELENT";
3273 case DT_RELR: return "RELR";
3274 case DT_RELRSZ: return "RELRSZ";
3275 case DT_RELRENT: return "RELRENT";
3276 case DT_PLTREL: return "PLTREL";
3277 case DT_DEBUG: return "DEBUG";
3278 case DT_TEXTREL: return "TEXTREL";
3279 case DT_JMPREL: return "JMPREL";
3280 case DT_BIND_NOW: return "BIND_NOW";
3281 case DT_INIT_ARRAY: return "INIT_ARRAY";
3282 case DT_FINI_ARRAY: return "FINI_ARRAY";
3283 case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
3284 case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
3285 case DT_RUNPATH: return "RUNPATH";
3286 case DT_FLAGS: return "FLAGS";
3287
3288 case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
3289 case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
3290 case DT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
3291
3292 case DT_CHECKSUM: return "CHECKSUM";
3293 case DT_PLTPADSZ: return "PLTPADSZ";
3294 case DT_MOVEENT: return "MOVEENT";
3295 case DT_MOVESZ: return "MOVESZ";
3296 case DT_FEATURE: return "FEATURE";
3297 case DT_POSFLAG_1: return "POSFLAG_1";
3298 case DT_SYMINSZ: return "SYMINSZ";
3299 case DT_SYMINENT: return "SYMINENT"; /* aka VALRNGHI */
3300
3301 case DT_ADDRRNGLO: return "ADDRRNGLO";
3302 case DT_CONFIG: return "CONFIG";
3303 case DT_DEPAUDIT: return "DEPAUDIT";
3304 case DT_AUDIT: return "AUDIT";
3305 case DT_PLTPAD: return "PLTPAD";
3306 case DT_MOVETAB: return "MOVETAB";
3307 case DT_SYMINFO: return "SYMINFO"; /* aka ADDRRNGHI */
3308
3309 case DT_VERSYM: return "VERSYM";
3310
3311 case DT_TLSDESC_GOT: return "TLSDESC_GOT";
3312 case DT_TLSDESC_PLT: return "TLSDESC_PLT";
3313 case DT_RELACOUNT: return "RELACOUNT";
3314 case DT_RELCOUNT: return "RELCOUNT";
3315 case DT_FLAGS_1: return "FLAGS_1";
3316 case DT_VERDEF: return "VERDEF";
3317 case DT_VERDEFNUM: return "VERDEFNUM";
3318 case DT_VERNEED: return "VERNEED";
3319 case DT_VERNEEDNUM: return "VERNEEDNUM";
3320
3321 case DT_AUXILIARY: return "AUXILIARY";
3322 case DT_USED: return "USED";
3323 case DT_FILTER: return "FILTER";
3324
3325 case DT_GNU_PRELINKED: return "GNU_PRELINKED";
3326 case DT_GNU_CONFLICT: return "GNU_CONFLICT";
3327 case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
3328 case DT_GNU_LIBLIST: return "GNU_LIBLIST";
3329 case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
3330 case DT_GNU_HASH: return "GNU_HASH";
3331 case DT_GNU_FLAGS_1: return "GNU_FLAGS_1";
3332
3333 default:
3334 if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
3335 {
3336 const char * result;
3337
3338 switch (filedata->file_header.e_machine)
3339 {
3340 case EM_AARCH64:
3341 result = get_aarch64_dynamic_type (type);
3342 break;
3343 case EM_MIPS:
3344 case EM_MIPS_RS3_LE:
3345 result = get_mips_dynamic_type (type);
3346 break;
3347 case EM_SPARCV9:
3348 result = get_sparc64_dynamic_type (type);
3349 break;
3350 case EM_PPC:
3351 result = get_ppc_dynamic_type (type);
3352 break;
3353 case EM_PPC64:
3354 result = get_ppc64_dynamic_type (type);
3355 break;
3356 case EM_IA_64:
3357 result = get_ia64_dynamic_type (type);
3358 break;
3359 case EM_ALPHA:
3360 result = get_alpha_dynamic_type (type);
3361 break;
3362 case EM_SCORE:
3363 result = get_score_dynamic_type (type);
3364 break;
3365 case EM_TI_C6000:
3366 result = get_tic6x_dynamic_type (type);
3367 break;
3368 case EM_ALTERA_NIOS2:
3369 result = get_nios2_dynamic_type (type);
3370 break;
3371 case EM_RISCV:
3372 result = get_riscv_dynamic_type (type);
3373 break;
3374 case EM_X86_64:
3375 result = get_x86_64_dynamic_type (type);
3376 break;
3377 default:
3378 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
3379 result = get_solaris_dynamic_type (type);
3380 else
3381 result = NULL;
3382 break;
3383 }
3384
3385 if (result != NULL)
3386 return result;
3387
3388 snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
3389 }
3390 else if (((type >= DT_LOOS) && (type <= DT_HIOS))
3391 || (filedata->file_header.e_machine == EM_PARISC
3392 && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
3393 {
3394 const char * result;
3395
3396 switch (filedata->file_header.e_machine)
3397 {
3398 case EM_PARISC:
3399 result = get_parisc_dynamic_type (type);
3400 break;
3401 case EM_IA_64:
3402 result = get_ia64_dynamic_type (type);
3403 break;
3404 default:
3405 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
3406 result = get_solaris_dynamic_type (type);
3407 else
3408 result = NULL;
3409 break;
3410 }
3411
3412 if (result != NULL)
3413 return result;
3414
3415 snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
3416 type);
3417 }
3418 else
3419 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
3420
3421 return buff;
3422 }
3423 }
3424
3425 static bool get_program_headers (Filedata *);
3426 static bool get_dynamic_section (Filedata *);
3427
3428 static void
3429 locate_dynamic_section (Filedata *filedata)
3430 {
3431 uint64_t dynamic_addr = 0;
3432 uint64_t dynamic_size = 0;
3433
3434 if (filedata->file_header.e_phnum != 0
3435 && get_program_headers (filedata))
3436 {
3437 Elf_Internal_Phdr *segment;
3438 unsigned int i;
3439
3440 for (i = 0, segment = filedata->program_headers;
3441 i < filedata->file_header.e_phnum;
3442 i++, segment++)
3443 {
3444 if (segment->p_type == PT_DYNAMIC)
3445 {
3446 dynamic_addr = segment->p_offset;
3447 dynamic_size = segment->p_filesz;
3448
3449 if (filedata->section_headers != NULL)
3450 {
3451 Elf_Internal_Shdr *sec;
3452
3453 sec = find_section (filedata, ".dynamic");
3454 if (sec != NULL)
3455 {
3456 if (sec->sh_size == 0
3457 || sec->sh_type == SHT_NOBITS)
3458 {
3459 dynamic_addr = 0;
3460 dynamic_size = 0;
3461 }
3462 else
3463 {
3464 dynamic_addr = sec->sh_offset;
3465 dynamic_size = sec->sh_size;
3466 }
3467 }
3468 }
3469
3470 if (dynamic_addr > filedata->file_size
3471 || (dynamic_size > filedata->file_size - dynamic_addr))
3472 {
3473 dynamic_addr = 0;
3474 dynamic_size = 0;
3475 }
3476 break;
3477 }
3478 }
3479 }
3480 filedata->dynamic_addr = dynamic_addr;
3481 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
3482 }
3483
3484 static bool
3485 is_pie (Filedata *filedata)
3486 {
3487 Elf_Internal_Dyn *entry;
3488
3489 if (filedata->dynamic_size == 0)
3490 locate_dynamic_section (filedata);
3491 if (filedata->dynamic_size <= 1)
3492 return false;
3493
3494 if (!get_dynamic_section (filedata))
3495 return false;
3496
3497 for (entry = filedata->dynamic_section;
3498 entry < filedata->dynamic_section + filedata->dynamic_nent;
3499 entry++)
3500 {
3501 if (entry->d_tag == DT_FLAGS_1)
3502 {
3503 if ((entry->d_un.d_val & DF_1_PIE) != 0)
3504 return true;
3505 break;
3506 }
3507 }
3508 return false;
3509 }
3510
3511 static char *
3512 get_file_type (Filedata *filedata)
3513 {
3514 unsigned e_type = filedata->file_header.e_type;
3515 static char buff[64];
3516
3517 switch (e_type)
3518 {
3519 case ET_NONE: return _("NONE (None)");
3520 case ET_REL: return _("REL (Relocatable file)");
3521 case ET_EXEC: return _("EXEC (Executable file)");
3522 case ET_DYN:
3523 if (is_pie (filedata))
3524 return _("DYN (Position-Independent Executable file)");
3525 else
3526 return _("DYN (Shared object file)");
3527 case ET_CORE: return _("CORE (Core file)");
3528
3529 default:
3530 if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
3531 snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
3532 else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
3533 snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
3534 else
3535 snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
3536 return buff;
3537 }
3538 }
3539
3540 static char *
3541 get_machine_name (unsigned e_machine)
3542 {
3543 static char buff[64]; /* XXX */
3544
3545 switch (e_machine)
3546 {
3547 /* Please keep this switch table sorted by increasing EM_ value. */
3548 /* 0 */
3549 case EM_NONE: return _("None");
3550 case EM_M32: return "WE32100";
3551 case EM_SPARC: return "Sparc";
3552 case EM_386: return "Intel 80386";
3553 case EM_68K: return "MC68000";
3554 case EM_88K: return "MC88000";
3555 case EM_IAMCU: return "Intel MCU";
3556 case EM_860: return "Intel 80860";
3557 case EM_MIPS: return "MIPS R3000";
3558 case EM_S370: return "IBM System/370";
3559 /* 10 */
3560 case EM_MIPS_RS3_LE: return "MIPS R4000 big-endian";
3561 case EM_OLD_SPARCV9: return "Sparc v9 (old)";
3562 case EM_PARISC: return "HPPA";
3563 case EM_VPP550: return "Fujitsu VPP500";
3564 case EM_SPARC32PLUS: return "Sparc v8+" ;
3565 case EM_960: return "Intel 80960";
3566 case EM_PPC: return "PowerPC";
3567 /* 20 */
3568 case EM_PPC64: return "PowerPC64";
3569 case EM_S390_OLD:
3570 case EM_S390: return "IBM S/390";
3571 case EM_SPU: return "SPU";
3572 /* 30 */
3573 case EM_V800: return "Renesas V850 (using RH850 ABI)";
3574 case EM_FR20: return "Fujitsu FR20";
3575 case EM_RH32: return "TRW RH32";
3576 case EM_MCORE: return "MCORE";
3577 /* 40 */
3578 case EM_ARM: return "ARM";
3579 case EM_OLD_ALPHA: return "Digital Alpha (old)";
3580 case EM_SH: return "Renesas / SuperH SH";
3581 case EM_SPARCV9: return "Sparc v9";
3582 case EM_TRICORE: return "Siemens Tricore";
3583 case EM_ARC: return "ARC";
3584 case EM_H8_300: return "Renesas H8/300";
3585 case EM_H8_300H: return "Renesas H8/300H";
3586 case EM_H8S: return "Renesas H8S";
3587 case EM_H8_500: return "Renesas H8/500";
3588 /* 50 */
3589 case EM_IA_64: return "Intel IA-64";
3590 case EM_MIPS_X: return "Stanford MIPS-X";
3591 case EM_COLDFIRE: return "Motorola Coldfire";
3592 case EM_68HC12: return "Motorola MC68HC12 Microcontroller";
3593 case EM_MMA: return "Fujitsu Multimedia Accelerator";
3594 case EM_PCP: return "Siemens PCP";
3595 case EM_NCPU: return "Sony nCPU embedded RISC processor";
3596 case EM_NDR1: return "Denso NDR1 microprocessor";
3597 case EM_STARCORE: return "Motorola Star*Core processor";
3598 case EM_ME16: return "Toyota ME16 processor";
3599 /* 60 */
3600 case EM_ST100: return "STMicroelectronics ST100 processor";
3601 case EM_TINYJ: return "Advanced Logic Corp. TinyJ embedded processor";
3602 case EM_X86_64: return "Advanced Micro Devices X86-64";
3603 case EM_PDSP: return "Sony DSP processor";
3604 case EM_PDP10: return "Digital Equipment Corp. PDP-10";
3605 case EM_PDP11: return "Digital Equipment Corp. PDP-11";
3606 case EM_FX66: return "Siemens FX66 microcontroller";
3607 case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 bit microcontroller";
3608 case EM_ST7: return "STMicroelectronics ST7 8-bit microcontroller";
3609 case EM_68HC16: return "Motorola MC68HC16 Microcontroller";
3610 /* 70 */
3611 case EM_68HC11: return "Motorola MC68HC11 Microcontroller";
3612 case EM_68HC08: return "Motorola MC68HC08 Microcontroller";
3613 case EM_68HC05: return "Motorola MC68HC05 Microcontroller";
3614 case EM_SVX: return "Silicon Graphics SVx";
3615 case EM_ST19: return "STMicroelectronics ST19 8-bit microcontroller";
3616 case EM_VAX: return "Digital VAX";
3617 case EM_CRIS: return "Axis Communications 32-bit embedded processor";
3618 case EM_JAVELIN: return "Infineon Technologies 32-bit embedded cpu";
3619 case EM_FIREPATH: return "Element 14 64-bit DSP processor";
3620 case EM_ZSP: return "LSI Logic's 16-bit DSP processor";
3621 /* 80 */
3622 case EM_MMIX: return "Donald Knuth's educational 64-bit processor";
3623 case EM_HUANY: return "Harvard Universitys's machine-independent object format";
3624 case EM_PRISM: return "Vitesse Prism";
3625 case EM_AVR_OLD:
3626 case EM_AVR: return "Atmel AVR 8-bit microcontroller";
3627 case EM_CYGNUS_FR30:
3628 case EM_FR30: return "Fujitsu FR30";
3629 case EM_CYGNUS_D10V:
3630 case EM_D10V: return "d10v";
3631 case EM_CYGNUS_D30V:
3632 case EM_D30V: return "d30v";
3633 case EM_CYGNUS_V850:
3634 case EM_V850: return "Renesas V850";
3635 case EM_CYGNUS_M32R:
3636 case EM_M32R: return "Renesas M32R (formerly Mitsubishi M32r)";
3637 case EM_CYGNUS_MN10300:
3638 case EM_MN10300: return "mn10300";
3639 /* 90 */
3640 case EM_CYGNUS_MN10200:
3641 case EM_MN10200: return "mn10200";
3642 case EM_PJ: return "picoJava";
3643 case EM_OR1K: return "OpenRISC 1000";
3644 case EM_ARC_COMPACT: return "ARCompact";
3645 case EM_XTENSA_OLD:
3646 case EM_XTENSA: return "Tensilica Xtensa Processor";
3647 case EM_VIDEOCORE: return "Alphamosaic VideoCore processor";
3648 case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor";
3649 case EM_NS32K: return "National Semiconductor 32000 series";
3650 case EM_TPC: return "Tenor Network TPC processor";
3651 case EM_SNP1K: return "Trebia SNP 1000 processor";
3652 /* 100 */
3653 case EM_ST200: return "STMicroelectronics ST200 microcontroller";
3654 case EM_IP2K_OLD:
3655 case EM_IP2K: return "Ubicom IP2xxx 8-bit microcontrollers";
3656 case EM_MAX: return "MAX Processor";
3657 case EM_CR: return "National Semiconductor CompactRISC";
3658 case EM_F2MC16: return "Fujitsu F2MC16";
3659 case EM_MSP430: return "Texas Instruments msp430 microcontroller";
3660 case EM_BLACKFIN: return "Analog Devices Blackfin";
3661 case EM_SE_C33: return "S1C33 Family of Seiko Epson processors";
3662 case EM_SEP: return "Sharp embedded microprocessor";
3663 case EM_ARCA: return "Arca RISC microprocessor";
3664 /* 110 */
3665 case EM_UNICORE: return "Unicore";
3666 case EM_EXCESS: return "eXcess 16/32/64-bit configurable embedded CPU";
3667 case EM_DXP: return "Icera Semiconductor Inc. Deep Execution Processor";
3668 case EM_ALTERA_NIOS2: return "Altera Nios II";
3669 case EM_CRX: return "National Semiconductor CRX microprocessor";
3670 case EM_XGATE: return "Motorola XGATE embedded processor";
3671 case EM_C166:
3672 case EM_XC16X: return "Infineon Technologies xc16x";
3673 case EM_M16C: return "Renesas M16C series microprocessors";
3674 case EM_DSPIC30F: return "Microchip Technology dsPIC30F Digital Signal Controller";
3675 case EM_CE: return "Freescale Communication Engine RISC core";
3676 /* 120 */
3677 case EM_M32C: return "Renesas M32c";
3678 /* 130 */
3679 case EM_TSK3000: return "Altium TSK3000 core";
3680 case EM_RS08: return "Freescale RS08 embedded processor";
3681 case EM_ECOG2: return "Cyan Technology eCOG2 microprocessor";
3682 case EM_SCORE: return "SUNPLUS S+Core";
3683 case EM_DSP24: return "New Japan Radio (NJR) 24-bit DSP Processor";
3684 case EM_VIDEOCORE3: return "Broadcom VideoCore III processor";
3685 case EM_LATTICEMICO32: return "Lattice Mico32";
3686 case EM_SE_C17: return "Seiko Epson C17 family";
3687 /* 140 */
3688 case EM_TI_C6000: return "Texas Instruments TMS320C6000 DSP family";
3689 case EM_TI_C2000: return "Texas Instruments TMS320C2000 DSP family";
3690 case EM_TI_C5500: return "Texas Instruments TMS320C55x DSP family";
3691 case EM_TI_PRU: return "TI PRU I/O processor";
3692 /* 160 */
3693 case EM_MMDSP_PLUS: return "STMicroelectronics 64bit VLIW Data Signal Processor";
3694 case EM_CYPRESS_M8C: return "Cypress M8C microprocessor";
3695 case EM_R32C: return "Renesas R32C series microprocessors";
3696 case EM_TRIMEDIA: return "NXP Semiconductors TriMedia architecture family";
3697 case EM_QDSP6: return "QUALCOMM DSP6 Processor";
3698 case EM_8051: return "Intel 8051 and variants";
3699 case EM_STXP7X: return "STMicroelectronics STxP7x family";
3700 case EM_NDS32: return "Andes Technology compact code size embedded RISC processor family";
3701 case EM_ECOG1X: return "Cyan Technology eCOG1X family";
3702 case EM_MAXQ30: return "Dallas Semiconductor MAXQ30 Core microcontrollers";
3703 /* 170 */
3704 case EM_XIMO16: return "New Japan Radio (NJR) 16-bit DSP Processor";
3705 case EM_MANIK: return "M2000 Reconfigurable RISC Microprocessor";
3706 case EM_CRAYNV2: return "Cray Inc. NV2 vector architecture";
3707 case EM_RX: return "Renesas RX";
3708 case EM_METAG: return "Imagination Technologies Meta processor architecture";
3709 case EM_MCST_ELBRUS: return "MCST Elbrus general purpose hardware architecture";
3710 case EM_ECOG16: return "Cyan Technology eCOG16 family";
3711 case EM_CR16:
3712 case EM_MICROBLAZE:
3713 case EM_MICROBLAZE_OLD: return "Xilinx MicroBlaze";
3714 case EM_ETPU: return "Freescale Extended Time Processing Unit";
3715 case EM_SLE9X: return "Infineon Technologies SLE9X core";
3716 /* 180 */
3717 case EM_L1OM: return "Intel L1OM";
3718 case EM_K1OM: return "Intel K1OM";
3719 case EM_INTEL182: return "Intel (reserved)";
3720 case EM_AARCH64: return "AArch64";
3721 case EM_ARM184: return "ARM (reserved)";
3722 case EM_AVR32: return "Atmel Corporation 32-bit microprocessor";
3723 case EM_STM8: return "STMicroeletronics STM8 8-bit microcontroller";
3724 case EM_TILE64: return "Tilera TILE64 multicore architecture family";
3725 case EM_TILEPRO: return "Tilera TILEPro multicore architecture family";
3726 /* 190 */
3727 case EM_CUDA: return "NVIDIA CUDA architecture";
3728 case EM_TILEGX: return "Tilera TILE-Gx multicore architecture family";
3729 case EM_CLOUDSHIELD: return "CloudShield architecture family";
3730 case EM_COREA_1ST: return "KIPO-KAIST Core-A 1st generation processor family";
3731 case EM_COREA_2ND: return "KIPO-KAIST Core-A 2nd generation processor family";
3732 case EM_ARC_COMPACT2: return "ARCv2";
3733 case EM_OPEN8: return "Open8 8-bit RISC soft processor core";
3734 case EM_RL78: return "Renesas RL78";
3735 case EM_VIDEOCORE5: return "Broadcom VideoCore V processor";
3736 case EM_78K0R: return "Renesas 78K0R";
3737 /* 200 */
3738 case EM_56800EX: return "Freescale 56800EX Digital Signal Controller (DSC)";
3739 case EM_BA1: return "Beyond BA1 CPU architecture";
3740 case EM_BA2: return "Beyond BA2 CPU architecture";
3741 case EM_XCORE: return "XMOS xCORE processor family";
3742 case EM_MCHP_PIC: return "Microchip 8-bit PIC(r) family";
3743 case EM_INTELGT: return "Intel Graphics Technology";
3744 /* 210 */
3745 case EM_KM32: return "KM211 KM32 32-bit processor";
3746 case EM_KMX32: return "KM211 KMX32 32-bit processor";
3747 case EM_KMX16: return "KM211 KMX16 16-bit processor";
3748 case EM_KMX8: return "KM211 KMX8 8-bit processor";
3749 case EM_KVARC: return "KM211 KVARC processor";
3750 case EM_CDP: return "Paneve CDP architecture family";
3751 case EM_COGE: return "Cognitive Smart Memory Processor";
3752 case EM_COOL: return "Bluechip Systems CoolEngine";
3753 case EM_NORC: return "Nanoradio Optimized RISC";
3754 case EM_CSR_KALIMBA: return "CSR Kalimba architecture family";
3755 /* 220 */
3756 case EM_Z80: return "Zilog Z80";
3757 case EM_VISIUM: return "CDS VISIUMcore processor";
3758 case EM_FT32: return "FTDI Chip FT32";
3759 case EM_MOXIE: return "Moxie";
3760 case EM_AMDGPU: return "AMD GPU";
3761 /* 230 (all reserved) */
3762 /* 240 */
3763 case EM_RISCV: return "RISC-V";
3764 case EM_LANAI: return "Lanai 32-bit processor";
3765 case EM_CEVA: return "CEVA Processor Architecture Family";
3766 case EM_CEVA_X2: return "CEVA X2 Processor Family";
3767 case EM_BPF: return "Linux BPF";
3768 case EM_GRAPHCORE_IPU: return "Graphcore Intelligent Processing Unit";
3769 case EM_IMG1: return "Imagination Technologies";
3770 /* 250 */
3771 case EM_NFP: return "Netronome Flow Processor";
3772 case EM_VE: return "NEC Vector Engine";
3773 case EM_CSKY: return "C-SKY";
3774 case EM_ARC_COMPACT3_64: return "Synopsys ARCv3 64-bit processor";
3775 case EM_MCS6502: return "MOS Technology MCS 6502 processor";
3776 case EM_ARC_COMPACT3: return "Synopsys ARCv3 32-bit processor";
3777 case EM_KVX: return "Kalray VLIW core of the MPPA processor family";
3778 case EM_65816: return "WDC 65816/65C816";
3779 case EM_LOONGARCH: return "LoongArch";
3780 case EM_KF32: return "ChipON KungFu32";
3781
3782 /* Large numbers... */
3783 case EM_MT: return "Morpho Techologies MT processor";
3784 case EM_ALPHA: return "Alpha";
3785 case EM_WEBASSEMBLY: return "Web Assembly";
3786 case EM_DLX: return "OpenDLX";
3787 case EM_XSTORMY16: return "Sanyo XStormy16 CPU core";
3788 case EM_IQ2000: return "Vitesse IQ2000";
3789 case EM_M32C_OLD:
3790 case EM_NIOS32: return "Altera Nios";
3791 case EM_CYGNUS_MEP: return "Toshiba MeP Media Engine";
3792 case EM_ADAPTEVA_EPIPHANY: return "Adapteva EPIPHANY";
3793 case EM_CYGNUS_FRV: return "Fujitsu FR-V";
3794 case EM_S12Z: return "Freescale S12Z";
3795
3796 default:
3797 snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
3798 return buff;
3799 }
3800 }
3801
3802 static char *
3803 decode_ARC_machine_flags (char *out, unsigned e_flags, unsigned e_machine)
3804 {
3805 /* ARC has two machine types EM_ARC_COMPACT and EM_ARC_COMPACT2. Some
3806 other compilers don't specify an architecture type in the e_flags, and
3807 instead use EM_ARC_COMPACT for old ARC600, ARC601, and ARC700
3808 architectures, and switch to EM_ARC_COMPACT2 for newer ARCEM and ARCHS
3809 architectures.
3810
3811 Th GNU tools follows this use of EM_ARC_COMPACT and EM_ARC_COMPACT2,
3812 but also sets a specific architecture type in the e_flags field.
3813
3814 However, when decoding the flags we don't worry if we see an
3815 unexpected pairing, for example EM_ARC_COMPACT machine type, with
3816 ARCEM architecture type. */
3817
3818 switch (e_flags & EF_ARC_MACH_MSK)
3819 {
3820 /* We only expect these to occur for EM_ARC_COMPACT2. */
3821 case EF_ARC_CPU_ARCV2EM:
3822 out = stpcpy (out, ", ARC EM");
3823 break;
3824 case EF_ARC_CPU_ARCV2HS:
3825 out = stpcpy (out, ", ARC HS");
3826 break;
3827
3828 /* We only expect these to occur for EM_ARC_COMPACT. */
3829 case E_ARC_MACH_ARC600:
3830 out = stpcpy (out, ", ARC600");
3831 break;
3832 case E_ARC_MACH_ARC601:
3833 out = stpcpy (out, ", ARC601");
3834 break;
3835 case E_ARC_MACH_ARC700:
3836 out = stpcpy (out, ", ARC700");
3837 break;
3838
3839 /* The only times we should end up here are (a) A corrupt ELF, (b) A
3840 new ELF with new architecture being read by an old version of
3841 readelf, or (c) An ELF built with non-GNU compiler that does not
3842 set the architecture in the e_flags. */
3843 default:
3844 if (e_machine == EM_ARC_COMPACT)
3845 out = stpcpy (out, ", Unknown ARCompact");
3846 else
3847 out = stpcpy (out, ", Unknown ARC");
3848 break;
3849 }
3850
3851 switch (e_flags & EF_ARC_OSABI_MSK)
3852 {
3853 case E_ARC_OSABI_ORIG:
3854 out = stpcpy (out, ", (ABI:legacy)");
3855 break;
3856 case E_ARC_OSABI_V2:
3857 out = stpcpy (out, ", (ABI:v2)");
3858 break;
3859 /* Only upstream 3.9+ kernels will support ARCv2 ISA. */
3860 case E_ARC_OSABI_V3:
3861 out = stpcpy (out, ", v3 no-legacy-syscalls ABI");
3862 break;
3863 case E_ARC_OSABI_V4:
3864 out = stpcpy (out, ", v4 ABI");
3865 break;
3866 default:
3867 out = stpcpy (out, ", unrecognised ARC OSABI flag");
3868 break;
3869 }
3870 return out;
3871 }
3872
3873 static char *
3874 decode_ARM_machine_flags (char *out, unsigned e_flags)
3875 {
3876 unsigned eabi;
3877 bool unknown_abi = false;
3878
3879 eabi = EF_ARM_EABI_VERSION (e_flags);
3880 e_flags &= ~ EF_ARM_EABIMASK;
3881
3882 /* Handle "generic" ARM flags. */
3883 if (e_flags & EF_ARM_RELEXEC)
3884 {
3885 out = stpcpy (out, ", relocatable executable");
3886 e_flags &= ~ EF_ARM_RELEXEC;
3887 }
3888
3889 if (e_flags & EF_ARM_PIC)
3890 {
3891 out = stpcpy (out, ", position independent");
3892 e_flags &= ~ EF_ARM_PIC;
3893 }
3894
3895 /* Now handle EABI specific flags. */
3896 switch (eabi)
3897 {
3898 default:
3899 out = stpcpy (out, ", <unrecognized EABI>");
3900 if (e_flags)
3901 unknown_abi = true;
3902 break;
3903
3904 case EF_ARM_EABI_VER1:
3905 out = stpcpy (out, ", Version1 EABI");
3906 while (e_flags)
3907 {
3908 unsigned flag;
3909
3910 /* Process flags one bit at a time. */
3911 flag = e_flags & - e_flags;
3912 e_flags &= ~ flag;
3913
3914 switch (flag)
3915 {
3916 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
3917 out = stpcpy (out, ", sorted symbol tables");
3918 break;
3919
3920 default:
3921 unknown_abi = true;
3922 break;
3923 }
3924 }
3925 break;
3926
3927 case EF_ARM_EABI_VER2:
3928 out = stpcpy (out, ", Version2 EABI");
3929 while (e_flags)
3930 {
3931 unsigned flag;
3932
3933 /* Process flags one bit at a time. */
3934 flag = e_flags & - e_flags;
3935 e_flags &= ~ flag;
3936
3937 switch (flag)
3938 {
3939 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
3940 out = stpcpy (out, ", sorted symbol tables");
3941 break;
3942
3943 case EF_ARM_DYNSYMSUSESEGIDX:
3944 out = stpcpy (out, ", dynamic symbols use segment index");
3945 break;
3946
3947 case EF_ARM_MAPSYMSFIRST:
3948 out = stpcpy (out, ", mapping symbols precede others");
3949 break;
3950
3951 default:
3952 unknown_abi = true;
3953 break;
3954 }
3955 }
3956 break;
3957
3958 case EF_ARM_EABI_VER3:
3959 out = stpcpy (out, ", Version3 EABI");
3960 break;
3961
3962 case EF_ARM_EABI_VER4:
3963 out = stpcpy (out, ", Version4 EABI");
3964 while (e_flags)
3965 {
3966 unsigned flag;
3967
3968 /* Process flags one bit at a time. */
3969 flag = e_flags & - e_flags;
3970 e_flags &= ~ flag;
3971
3972 switch (flag)
3973 {
3974 case EF_ARM_BE8:
3975 out = stpcpy (out, ", BE8");
3976 break;
3977
3978 case EF_ARM_LE8:
3979 out = stpcpy (out, ", LE8");
3980 break;
3981
3982 default:
3983 unknown_abi = true;
3984 break;
3985 }
3986 }
3987 break;
3988
3989 case EF_ARM_EABI_VER5:
3990 out = stpcpy (out, ", Version5 EABI");
3991 while (e_flags)
3992 {
3993 unsigned flag;
3994
3995 /* Process flags one bit at a time. */
3996 flag = e_flags & - e_flags;
3997 e_flags &= ~ flag;
3998
3999 switch (flag)
4000 {
4001 case EF_ARM_BE8:
4002 out = stpcpy (out, ", BE8");
4003 break;
4004
4005 case EF_ARM_LE8:
4006 out = stpcpy (out, ", LE8");
4007 break;
4008
4009 case EF_ARM_ABI_FLOAT_SOFT: /* Conflicts with EF_ARM_SOFT_FLOAT. */
4010 out = stpcpy (out, ", soft-float ABI");
4011 break;
4012
4013 case EF_ARM_ABI_FLOAT_HARD: /* Conflicts with EF_ARM_VFP_FLOAT. */
4014 out = stpcpy (out, ", hard-float ABI");
4015 break;
4016
4017 default:
4018 unknown_abi = true;
4019 break;
4020 }
4021 }
4022 break;
4023
4024 case EF_ARM_EABI_UNKNOWN:
4025 out = stpcpy (out, ", GNU EABI");
4026 while (e_flags)
4027 {
4028 unsigned flag;
4029
4030 /* Process flags one bit at a time. */
4031 flag = e_flags & - e_flags;
4032 e_flags &= ~ flag;
4033
4034 switch (flag)
4035 {
4036 case EF_ARM_INTERWORK:
4037 out = stpcpy (out, ", interworking enabled");
4038 break;
4039
4040 case EF_ARM_APCS_26:
4041 out = stpcpy (out, ", uses APCS/26");
4042 break;
4043
4044 case EF_ARM_APCS_FLOAT:
4045 out = stpcpy (out, ", uses APCS/float");
4046 break;
4047
4048 case EF_ARM_PIC:
4049 out = stpcpy (out, ", position independent");
4050 break;
4051
4052 case EF_ARM_ALIGN8:
4053 out = stpcpy (out, ", 8 bit structure alignment");
4054 break;
4055
4056 case EF_ARM_NEW_ABI:
4057 out = stpcpy (out, ", uses new ABI");
4058 break;
4059
4060 case EF_ARM_OLD_ABI:
4061 out = stpcpy (out, ", uses old ABI");
4062 break;
4063
4064 case EF_ARM_SOFT_FLOAT:
4065 out = stpcpy (out, ", software FP");
4066 break;
4067
4068 case EF_ARM_VFP_FLOAT:
4069 out = stpcpy (out, ", VFP");
4070 break;
4071
4072 default:
4073 unknown_abi = true;
4074 break;
4075 }
4076 }
4077 }
4078
4079 if (unknown_abi)
4080 out = stpcpy (out,_(", <unknown>"));
4081 return out;
4082 }
4083
4084 static char *
4085 decode_AVR_machine_flags (char *out, unsigned e_flags)
4086 {
4087 switch (e_flags & EF_AVR_MACH)
4088 {
4089 case E_AVR_MACH_AVR1:
4090 out = stpcpy (out, ", avr:1");
4091 break;
4092 case E_AVR_MACH_AVR2:
4093 out = stpcpy (out, ", avr:2");
4094 break;
4095 case E_AVR_MACH_AVR25:
4096 out = stpcpy (out, ", avr:25");
4097 break;
4098 case E_AVR_MACH_AVR3:
4099 out = stpcpy (out, ", avr:3");
4100 break;
4101 case E_AVR_MACH_AVR31:
4102 out = stpcpy (out, ", avr:31");
4103 break;
4104 case E_AVR_MACH_AVR35:
4105 out = stpcpy (out, ", avr:35");
4106 break;
4107 case E_AVR_MACH_AVR4:
4108 out = stpcpy (out, ", avr:4");
4109 break;
4110 case E_AVR_MACH_AVR5:
4111 out = stpcpy (out, ", avr:5");
4112 break;
4113 case E_AVR_MACH_AVR51:
4114 out = stpcpy (out, ", avr:51");
4115 break;
4116 case E_AVR_MACH_AVR6:
4117 out = stpcpy (out, ", avr:6");
4118 break;
4119 case E_AVR_MACH_AVRTINY:
4120 out = stpcpy (out, ", avr:100");
4121 break;
4122 case E_AVR_MACH_XMEGA1:
4123 out = stpcpy (out, ", avr:101");
4124 break;
4125 case E_AVR_MACH_XMEGA2:
4126 out = stpcpy (out, ", avr:102");
4127 break;
4128 case E_AVR_MACH_XMEGA3:
4129 out = stpcpy (out, ", avr:103");
4130 break;
4131 case E_AVR_MACH_XMEGA4:
4132 out = stpcpy (out, ", avr:104");
4133 break;
4134 case E_AVR_MACH_XMEGA5:
4135 out = stpcpy (out, ", avr:105");
4136 break;
4137 case E_AVR_MACH_XMEGA6:
4138 out = stpcpy (out, ", avr:106");
4139 break;
4140 case E_AVR_MACH_XMEGA7:
4141 out = stpcpy (out, ", avr:107");
4142 break;
4143 default:
4144 out = stpcpy (out, ", avr:<unknown>");
4145 break;
4146 }
4147
4148 if (e_flags & EF_AVR_LINKRELAX_PREPARED)
4149 out = stpcpy (out, ", link-relax");
4150 return out;
4151 }
4152
4153 static char *
4154 decode_BLACKFIN_machine_flags (char *out, unsigned e_flags)
4155 {
4156 if (e_flags & EF_BFIN_PIC)
4157 out = stpcpy (out, ", PIC");
4158
4159 if (e_flags & EF_BFIN_FDPIC)
4160 out = stpcpy (out, ", FDPIC");
4161
4162 if (e_flags & EF_BFIN_CODE_IN_L1)
4163 out = stpcpy (out, ", code in L1");
4164
4165 if (e_flags & EF_BFIN_DATA_IN_L1)
4166 out = stpcpy (out, ", data in L1");
4167 return out;
4168 }
4169
4170 static char *
4171 decode_FRV_machine_flags (char *out, unsigned e_flags)
4172 {
4173 switch (e_flags & EF_FRV_CPU_MASK)
4174 {
4175 case EF_FRV_CPU_GENERIC:
4176 break;
4177
4178 default:
4179 out = stpcpy (out, ", fr???");
4180 break;
4181
4182 case EF_FRV_CPU_FR300:
4183 out = stpcpy (out, ", fr300");
4184 break;
4185
4186 case EF_FRV_CPU_FR400:
4187 out = stpcpy (out, ", fr400");
4188 break;
4189 case EF_FRV_CPU_FR405:
4190 out = stpcpy (out, ", fr405");
4191 break;
4192
4193 case EF_FRV_CPU_FR450:
4194 out = stpcpy (out, ", fr450");
4195 break;
4196
4197 case EF_FRV_CPU_FR500:
4198 out = stpcpy (out, ", fr500");
4199 break;
4200 case EF_FRV_CPU_FR550:
4201 out = stpcpy (out, ", fr550");
4202 break;
4203
4204 case EF_FRV_CPU_SIMPLE:
4205 out = stpcpy (out, ", simple");
4206 break;
4207 case EF_FRV_CPU_TOMCAT:
4208 out = stpcpy (out, ", tomcat");
4209 break;
4210 }
4211 return out;
4212 }
4213
4214 static char *
4215 decode_IA64_machine_flags (char *out, unsigned e_flags, Filedata *filedata)
4216 {
4217 if ((e_flags & EF_IA_64_ABI64))
4218 out = stpcpy (out, ", 64-bit");
4219 else
4220 out = stpcpy (out, ", 32-bit");
4221 if ((e_flags & EF_IA_64_REDUCEDFP))
4222 out = stpcpy (out, ", reduced fp model");
4223 if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
4224 out = stpcpy (out, ", no function descriptors, constant gp");
4225 else if ((e_flags & EF_IA_64_CONS_GP))
4226 out = stpcpy (out, ", constant gp");
4227 if ((e_flags & EF_IA_64_ABSOLUTE))
4228 out = stpcpy (out, ", absolute");
4229 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
4230 {
4231 if ((e_flags & EF_IA_64_VMS_LINKAGES))
4232 out = stpcpy (out, ", vms_linkages");
4233 switch ((e_flags & EF_IA_64_VMS_COMCOD))
4234 {
4235 case EF_IA_64_VMS_COMCOD_SUCCESS:
4236 break;
4237 case EF_IA_64_VMS_COMCOD_WARNING:
4238 out = stpcpy (out, ", warning");
4239 break;
4240 case EF_IA_64_VMS_COMCOD_ERROR:
4241 out = stpcpy (out, ", error");
4242 break;
4243 case EF_IA_64_VMS_COMCOD_ABORT:
4244 out = stpcpy (out, ", abort");
4245 break;
4246 default:
4247 warn (_("Unrecognised IA64 VMS Command Code: %x\n"),
4248 e_flags & EF_IA_64_VMS_COMCOD);
4249 out = stpcpy (out, ", <unknown>");
4250 }
4251 }
4252 return out;
4253 }
4254
4255 static char *
4256 decode_LOONGARCH_machine_flags (char *out, unsigned int e_flags)
4257 {
4258 if (EF_LOONGARCH_IS_SOFT_FLOAT (e_flags))
4259 out = stpcpy (out, ", SOFT-FLOAT");
4260 else if (EF_LOONGARCH_IS_SINGLE_FLOAT (e_flags))
4261 out = stpcpy (out, ", SINGLE-FLOAT");
4262 else if (EF_LOONGARCH_IS_DOUBLE_FLOAT (e_flags))
4263 out = stpcpy (out, ", DOUBLE-FLOAT");
4264
4265 if (EF_LOONGARCH_IS_OBJ_V0 (e_flags))
4266 out = stpcpy (out, ", OBJ-v0");
4267 else if (EF_LOONGARCH_IS_OBJ_V1 (e_flags))
4268 out = stpcpy (out, ", OBJ-v1");
4269 return out;
4270 }
4271
4272 static char *
4273 decode_M68K_machine_flags (char *out, unsigned int e_flags)
4274 {
4275 if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
4276 out = stpcpy (out, ", m68000");
4277 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
4278 out = stpcpy (out, ", cpu32");
4279 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
4280 out = stpcpy (out, ", fido_a");
4281 else
4282 {
4283 char const *isa = _("unknown");
4284 char const *mac = _("unknown mac");
4285 char const *additional = NULL;
4286
4287 switch (e_flags & EF_M68K_CF_ISA_MASK)
4288 {
4289 case EF_M68K_CF_ISA_A_NODIV:
4290 isa = "A";
4291 additional = ", nodiv";
4292 break;
4293 case EF_M68K_CF_ISA_A:
4294 isa = "A";
4295 break;
4296 case EF_M68K_CF_ISA_A_PLUS:
4297 isa = "A+";
4298 break;
4299 case EF_M68K_CF_ISA_B_NOUSP:
4300 isa = "B";
4301 additional = ", nousp";
4302 break;
4303 case EF_M68K_CF_ISA_B:
4304 isa = "B";
4305 break;
4306 case EF_M68K_CF_ISA_C:
4307 isa = "C";
4308 break;
4309 case EF_M68K_CF_ISA_C_NODIV:
4310 isa = "C";
4311 additional = ", nodiv";
4312 break;
4313 }
4314 out = stpcpy (out, ", cf, isa ");
4315 out = stpcpy (out, isa);
4316 if (additional)
4317 out = stpcpy (out, additional);
4318 if (e_flags & EF_M68K_CF_FLOAT)
4319 out = stpcpy (out, ", float");
4320 switch (e_flags & EF_M68K_CF_MAC_MASK)
4321 {
4322 case 0:
4323 mac = NULL;
4324 break;
4325 case EF_M68K_CF_MAC:
4326 mac = "mac";
4327 break;
4328 case EF_M68K_CF_EMAC:
4329 mac = "emac";
4330 break;
4331 case EF_M68K_CF_EMAC_B:
4332 mac = "emac_b";
4333 break;
4334 }
4335 if (mac)
4336 {
4337 out = stpcpy (out, ", ");
4338 out = stpcpy (out, mac);
4339 }
4340 }
4341 return out;
4342 }
4343
4344 static char *
4345 decode_MeP_machine_flags (char *out, unsigned int e_flags)
4346 {
4347 switch (e_flags & EF_MEP_CPU_MASK)
4348 {
4349 case EF_MEP_CPU_MEP:
4350 out = stpcpy (out, ", generic MeP");
4351 break;
4352 case EF_MEP_CPU_C2:
4353 out = stpcpy (out, ", MeP C2");
4354 break;
4355 case EF_MEP_CPU_C3:
4356 out = stpcpy (out, ", MeP C3");
4357 break;
4358 case EF_MEP_CPU_C4:
4359 out = stpcpy (out, ", MeP C4");
4360 break;
4361 case EF_MEP_CPU_C5:
4362 out = stpcpy (out, ", MeP C5");
4363 break;
4364 case EF_MEP_CPU_H1:
4365 out = stpcpy (out, ", MeP H1");
4366 break;
4367 default:
4368 out = stpcpy (out, _(", <unknown MeP cpu type>"));
4369 break;
4370 }
4371
4372 switch (e_flags & EF_MEP_COP_MASK)
4373 {
4374 case EF_MEP_COP_NONE:
4375 break;
4376 case EF_MEP_COP_AVC:
4377 out = stpcpy (out, ", AVC coprocessor");
4378 break;
4379 case EF_MEP_COP_AVC2:
4380 out = stpcpy (out, ", AVC2 coprocessor");
4381 break;
4382 case EF_MEP_COP_FMAX:
4383 out = stpcpy (out, ", FMAX coprocessor");
4384 break;
4385 case EF_MEP_COP_IVC2:
4386 out = stpcpy (out, ", IVC2 coprocessor");
4387 break;
4388 default:
4389 out = stpcpy (out, _("<unknown MeP copro type>"));
4390 break;
4391 }
4392
4393 if (e_flags & EF_MEP_LIBRARY)
4394 out = stpcpy (out, ", Built for Library");
4395
4396 if (e_flags & EF_MEP_INDEX_MASK)
4397 out += sprintf (out, ", Configuration Index: %#x",
4398 e_flags & EF_MEP_INDEX_MASK);
4399
4400 if (e_flags & ~ EF_MEP_ALL_FLAGS)
4401 out += sprintf (out, _(", unknown flags bits: %#x"),
4402 e_flags & ~ EF_MEP_ALL_FLAGS);
4403 return out;
4404 }
4405
4406 static char *
4407 decode_MIPS_machine_flags (char *out, unsigned int e_flags)
4408 {
4409 if (e_flags & EF_MIPS_NOREORDER)
4410 out = stpcpy (out, ", noreorder");
4411
4412 if (e_flags & EF_MIPS_PIC)
4413 out = stpcpy (out, ", pic");
4414
4415 if (e_flags & EF_MIPS_CPIC)
4416 out = stpcpy (out, ", cpic");
4417
4418 if (e_flags & EF_MIPS_UCODE)
4419 out = stpcpy (out, ", ugen_reserved");
4420
4421 if (e_flags & EF_MIPS_ABI2)
4422 out = stpcpy (out, ", abi2");
4423
4424 if (e_flags & EF_MIPS_OPTIONS_FIRST)
4425 out = stpcpy (out, ", odk first");
4426
4427 if (e_flags & EF_MIPS_32BITMODE)
4428 out = stpcpy (out, ", 32bitmode");
4429
4430 if (e_flags & EF_MIPS_NAN2008)
4431 out = stpcpy (out, ", nan2008");
4432
4433 if (e_flags & EF_MIPS_FP64)
4434 out = stpcpy (out, ", fp64");
4435
4436 switch ((e_flags & EF_MIPS_MACH))
4437 {
4438 case EF_MIPS_MACH_3900:
4439 out = stpcpy (out, ", 3900");
4440 break;
4441 case EF_MIPS_MACH_4010:
4442 out = stpcpy (out, ", 4010");
4443 break;
4444 case EF_MIPS_MACH_4100:
4445 out = stpcpy (out, ", 4100");
4446 break;
4447 case EF_MIPS_MACH_4111:
4448 out = stpcpy (out, ", 4111");
4449 break;
4450 case EF_MIPS_MACH_4120:
4451 out = stpcpy (out, ", 4120");
4452 break;
4453 case EF_MIPS_MACH_4650:
4454 out = stpcpy (out, ", 4650");
4455 break;
4456 case EF_MIPS_MACH_5400:
4457 out = stpcpy (out, ", 5400");
4458 break;
4459 case EF_MIPS_MACH_5500:
4460 out = stpcpy (out, ", 5500");
4461 break;
4462 case EF_MIPS_MACH_5900:
4463 out = stpcpy (out, ", 5900");
4464 break;
4465 case EF_MIPS_MACH_SB1:
4466 out = stpcpy (out, ", sb1");
4467 break;
4468 case EF_MIPS_MACH_9000:
4469 out = stpcpy (out, ", 9000");
4470 break;
4471 case EF_MIPS_MACH_LS2E:
4472 out = stpcpy (out, ", loongson-2e");
4473 break;
4474 case EF_MIPS_MACH_LS2F:
4475 out = stpcpy (out, ", loongson-2f");
4476 break;
4477 case EF_MIPS_MACH_GS464:
4478 out = stpcpy (out, ", gs464");
4479 break;
4480 case EF_MIPS_MACH_GS464E:
4481 out = stpcpy (out, ", gs464e");
4482 break;
4483 case EF_MIPS_MACH_GS264E:
4484 out = stpcpy (out, ", gs264e");
4485 break;
4486 case EF_MIPS_MACH_OCTEON:
4487 out = stpcpy (out, ", octeon");
4488 break;
4489 case EF_MIPS_MACH_OCTEON2:
4490 out = stpcpy (out, ", octeon2");
4491 break;
4492 case EF_MIPS_MACH_OCTEON3:
4493 out = stpcpy (out, ", octeon3");
4494 break;
4495 case EF_MIPS_MACH_XLR:
4496 out = stpcpy (out, ", xlr");
4497 break;
4498 case EF_MIPS_MACH_IAMR2:
4499 out = stpcpy (out, ", interaptiv-mr2");
4500 break;
4501 case EF_MIPS_MACH_ALLEGREX:
4502 out = stpcpy (out, ", allegrex");
4503 break;
4504 case 0:
4505 /* We simply ignore the field in this case to avoid confusion:
4506 MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
4507 extension. */
4508 break;
4509 default:
4510 out = stpcpy (out, _(", unknown CPU"));
4511 break;
4512 }
4513
4514 switch ((e_flags & EF_MIPS_ABI))
4515 {
4516 case EF_MIPS_ABI_O32:
4517 out = stpcpy (out, ", o32");
4518 break;
4519 case EF_MIPS_ABI_O64:
4520 out = stpcpy (out, ", o64");
4521 break;
4522 case EF_MIPS_ABI_EABI32:
4523 out = stpcpy (out, ", eabi32");
4524 break;
4525 case EF_MIPS_ABI_EABI64:
4526 out = stpcpy (out, ", eabi64");
4527 break;
4528 case 0:
4529 /* We simply ignore the field in this case to avoid confusion:
4530 MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
4531 This means it is likely to be an o32 file, but not for
4532 sure. */
4533 break;
4534 default:
4535 out = stpcpy (out, _(", unknown ABI"));
4536 break;
4537 }
4538
4539 if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
4540 out = stpcpy (out, ", mdmx");
4541
4542 if (e_flags & EF_MIPS_ARCH_ASE_M16)
4543 out = stpcpy (out, ", mips16");
4544
4545 if (e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
4546 out = stpcpy (out, ", micromips");
4547
4548 switch ((e_flags & EF_MIPS_ARCH))
4549 {
4550 case EF_MIPS_ARCH_1:
4551 out = stpcpy (out, ", mips1");
4552 break;
4553 case EF_MIPS_ARCH_2:
4554 out = stpcpy (out, ", mips2");
4555 break;
4556 case EF_MIPS_ARCH_3:
4557 out = stpcpy (out, ", mips3");
4558 break;
4559 case EF_MIPS_ARCH_4:
4560 out = stpcpy (out, ", mips4");
4561 break;
4562 case EF_MIPS_ARCH_5:
4563 out = stpcpy (out, ", mips5");
4564 break;
4565 case EF_MIPS_ARCH_32:
4566 out = stpcpy (out, ", mips32");
4567 break;
4568 case EF_MIPS_ARCH_32R2:
4569 out = stpcpy (out, ", mips32r2");
4570 break;
4571 case EF_MIPS_ARCH_32R6:
4572 out = stpcpy (out, ", mips32r6");
4573 break;
4574 case EF_MIPS_ARCH_64:
4575 out = stpcpy (out, ", mips64");
4576 break;
4577 case EF_MIPS_ARCH_64R2:
4578 out = stpcpy (out, ", mips64r2");
4579 break;
4580 case EF_MIPS_ARCH_64R6:
4581 out = stpcpy (out, ", mips64r6");
4582 break;
4583 default:
4584 out = stpcpy (out, _(", unknown ISA"));
4585 break;
4586 }
4587 return out;
4588 }
4589
4590 static char *
4591 decode_MSP430_machine_flags (char *out, unsigned e_flags)
4592 {
4593 out = stpcpy (out, _(": architecture variant: "));
4594 switch (e_flags & EF_MSP430_MACH)
4595 {
4596 case E_MSP430_MACH_MSP430x11:
4597 out = stpcpy (out, "MSP430x11");
4598 break;
4599 case E_MSP430_MACH_MSP430x11x1:
4600 out = stpcpy (out, "MSP430x11x1 ");
4601 break;
4602 case E_MSP430_MACH_MSP430x12:
4603 out = stpcpy (out, "MSP430x12");
4604 break;
4605 case E_MSP430_MACH_MSP430x13:
4606 out = stpcpy (out, "MSP430x13");
4607 break;
4608 case E_MSP430_MACH_MSP430x14:
4609 out = stpcpy (out, "MSP430x14");
4610 break;
4611 case E_MSP430_MACH_MSP430x15:
4612 out = stpcpy (out, "MSP430x15");
4613 break;
4614 case E_MSP430_MACH_MSP430x16:
4615 out = stpcpy (out, "MSP430x16");
4616 break;
4617 case E_MSP430_MACH_MSP430x31:
4618 out = stpcpy (out, "MSP430x31");
4619 break;
4620 case E_MSP430_MACH_MSP430x32:
4621 out = stpcpy (out, "MSP430x32");
4622 break;
4623 case E_MSP430_MACH_MSP430x33:
4624 out = stpcpy (out, "MSP430x33");
4625 break;
4626 case E_MSP430_MACH_MSP430x41:
4627 out = stpcpy (out, "MSP430x41");
4628 break;
4629 case E_MSP430_MACH_MSP430x42:
4630 out = stpcpy (out, "MSP430x42");
4631 break;
4632 case E_MSP430_MACH_MSP430x43:
4633 out = stpcpy (out, "MSP430x43");
4634 break;
4635 case E_MSP430_MACH_MSP430x44:
4636 out = stpcpy (out, "MSP430x44");
4637 break;
4638 case E_MSP430_MACH_MSP430X :
4639 out = stpcpy (out, "MSP430X");
4640 break;
4641 default:
4642 out = stpcpy (out, _(": unknown"));
4643 break;
4644 }
4645
4646 if (e_flags & ~ EF_MSP430_MACH)
4647 out = stpcpy (out, _(": unknown extra flag bits also present"));
4648 return out;
4649 }
4650
4651 static char *
4652 decode_NDS32_machine_flags (char *out, unsigned e_flags)
4653 {
4654 unsigned abi;
4655 unsigned arch;
4656 unsigned config;
4657 unsigned version;
4658 bool has_fpu = false;
4659
4660 static const char *ABI_STRINGS[] =
4661 {
4662 "ABI v0", /* use r5 as return register; only used in N1213HC */
4663 "ABI v1", /* use r0 as return register */
4664 "ABI v2", /* use r0 as return register and don't reserve 24 bytes for arguments */
4665 "ABI v2fp", /* for FPU */
4666 "AABI",
4667 "ABI2 FP+"
4668 };
4669 static const char *VER_STRINGS[] =
4670 {
4671 "Andes ELF V1.3 or older",
4672 "Andes ELF V1.3.1",
4673 "Andes ELF V1.4"
4674 };
4675 static const char *ARCH_STRINGS[] =
4676 {
4677 "",
4678 "Andes Star v1.0",
4679 "Andes Star v2.0",
4680 "Andes Star v3.0",
4681 "Andes Star v3.0m"
4682 };
4683
4684 abi = EF_NDS_ABI & e_flags;
4685 arch = EF_NDS_ARCH & e_flags;
4686 config = EF_NDS_INST & e_flags;
4687 version = EF_NDS32_ELF_VERSION & e_flags;
4688
4689 switch (abi)
4690 {
4691 case E_NDS_ABI_V0:
4692 case E_NDS_ABI_V1:
4693 case E_NDS_ABI_V2:
4694 case E_NDS_ABI_V2FP:
4695 case E_NDS_ABI_AABI:
4696 case E_NDS_ABI_V2FP_PLUS:
4697 /* In case there are holes in the array. */
4698 out += sprintf (out, ", %s", ABI_STRINGS[abi >> EF_NDS_ABI_SHIFT]);
4699 break;
4700
4701 default:
4702 out = stpcpy (out, ", <unrecognized ABI>");
4703 break;
4704 }
4705
4706 switch (version)
4707 {
4708 case E_NDS32_ELF_VER_1_2:
4709 case E_NDS32_ELF_VER_1_3:
4710 case E_NDS32_ELF_VER_1_4:
4711 out += sprintf (out, ", %s", VER_STRINGS[version >> EF_NDS32_ELF_VERSION_SHIFT]);
4712 break;
4713
4714 default:
4715 out = stpcpy (out, ", <unrecognized ELF version number>");
4716 break;
4717 }
4718
4719 if (E_NDS_ABI_V0 == abi)
4720 {
4721 /* OLD ABI; only used in N1213HC, has performance extension 1. */
4722 out = stpcpy (out, ", Andes Star v1.0, N1213HC, MAC, PERF1");
4723 if (arch == E_NDS_ARCH_STAR_V1_0)
4724 out = stpcpy (out, ", 16b"); /* has 16-bit instructions */
4725 return out;
4726 }
4727
4728 switch (arch)
4729 {
4730 case E_NDS_ARCH_STAR_V1_0:
4731 case E_NDS_ARCH_STAR_V2_0:
4732 case E_NDS_ARCH_STAR_V3_0:
4733 case E_NDS_ARCH_STAR_V3_M:
4734 out += sprintf (out, ", %s", ARCH_STRINGS[arch >> EF_NDS_ARCH_SHIFT]);
4735 break;
4736
4737 default:
4738 out = stpcpy (out, ", <unrecognized architecture>");
4739 /* ARCH version determines how the e_flags are interpreted.
4740 If it is unknown, we cannot proceed. */
4741 return out;
4742 }
4743
4744 /* Newer ABI; Now handle architecture specific flags. */
4745 if (arch == E_NDS_ARCH_STAR_V1_0)
4746 {
4747 if (config & E_NDS32_HAS_MFUSR_PC_INST)
4748 out = stpcpy (out, ", MFUSR_PC");
4749
4750 if (!(config & E_NDS32_HAS_NO_MAC_INST))
4751 out = stpcpy (out, ", MAC");
4752
4753 if (config & E_NDS32_HAS_DIV_INST)
4754 out = stpcpy (out, ", DIV");
4755
4756 if (config & E_NDS32_HAS_16BIT_INST)
4757 out = stpcpy (out, ", 16b");
4758 }
4759 else
4760 {
4761 if (config & E_NDS32_HAS_MFUSR_PC_INST)
4762 {
4763 if (version <= E_NDS32_ELF_VER_1_3)
4764 out = stpcpy (out, ", [B8]");
4765 else
4766 out = stpcpy (out, ", EX9");
4767 }
4768
4769 if (config & E_NDS32_HAS_MAC_DX_INST)
4770 out = stpcpy (out, ", MAC_DX");
4771
4772 if (config & E_NDS32_HAS_DIV_DX_INST)
4773 out = stpcpy (out, ", DIV_DX");
4774
4775 if (config & E_NDS32_HAS_16BIT_INST)
4776 {
4777 if (version <= E_NDS32_ELF_VER_1_3)
4778 out = stpcpy (out, ", 16b");
4779 else
4780 out = stpcpy (out, ", IFC");
4781 }
4782 }
4783
4784 if (config & E_NDS32_HAS_EXT_INST)
4785 out = stpcpy (out, ", PERF1");
4786
4787 if (config & E_NDS32_HAS_EXT2_INST)
4788 out = stpcpy (out, ", PERF2");
4789
4790 if (config & E_NDS32_HAS_FPU_INST)
4791 {
4792 has_fpu = true;
4793 out = stpcpy (out, ", FPU_SP");
4794 }
4795
4796 if (config & E_NDS32_HAS_FPU_DP_INST)
4797 {
4798 has_fpu = true;
4799 out = stpcpy (out, ", FPU_DP");
4800 }
4801
4802 if (config & E_NDS32_HAS_FPU_MAC_INST)
4803 {
4804 has_fpu = true;
4805 out = stpcpy (out, ", FPU_MAC");
4806 }
4807
4808 if (has_fpu)
4809 {
4810 switch ((config & E_NDS32_FPU_REG_CONF) >> E_NDS32_FPU_REG_CONF_SHIFT)
4811 {
4812 case E_NDS32_FPU_REG_8SP_4DP:
4813 out = stpcpy (out, ", FPU_REG:8/4");
4814 break;
4815 case E_NDS32_FPU_REG_16SP_8DP:
4816 out = stpcpy (out, ", FPU_REG:16/8");
4817 break;
4818 case E_NDS32_FPU_REG_32SP_16DP:
4819 out = stpcpy (out, ", FPU_REG:32/16");
4820 break;
4821 case E_NDS32_FPU_REG_32SP_32DP:
4822 out = stpcpy (out, ", FPU_REG:32/32");
4823 break;
4824 }
4825 }
4826
4827 if (config & E_NDS32_HAS_AUDIO_INST)
4828 out = stpcpy (out, ", AUDIO");
4829
4830 if (config & E_NDS32_HAS_STRING_INST)
4831 out = stpcpy (out, ", STR");
4832
4833 if (config & E_NDS32_HAS_REDUCED_REGS)
4834 out = stpcpy (out, ", 16REG");
4835
4836 if (config & E_NDS32_HAS_VIDEO_INST)
4837 {
4838 if (version <= E_NDS32_ELF_VER_1_3)
4839 out = stpcpy (out, ", VIDEO");
4840 else
4841 out = stpcpy (out, ", SATURATION");
4842 }
4843
4844 if (config & E_NDS32_HAS_ENCRIPT_INST)
4845 out = stpcpy (out, ", ENCRP");
4846
4847 if (config & E_NDS32_HAS_L2C_INST)
4848 out = stpcpy (out, ", L2C");
4849
4850 return out;
4851 }
4852
4853 static char *
4854 decode_PARISC_machine_flags (char *out, unsigned e_flags)
4855 {
4856 switch (e_flags & EF_PARISC_ARCH)
4857 {
4858 case EFA_PARISC_1_0:
4859 out = stpcpy (out, ", PA-RISC 1.0");
4860 break;
4861 case EFA_PARISC_1_1:
4862 out = stpcpy (out, ", PA-RISC 1.1");
4863 break;
4864 case EFA_PARISC_2_0:
4865 out = stpcpy (out, ", PA-RISC 2.0");
4866 break;
4867 default:
4868 break;
4869 }
4870 if (e_flags & EF_PARISC_TRAPNIL)
4871 out = stpcpy (out, ", trapnil");
4872 if (e_flags & EF_PARISC_EXT)
4873 out = stpcpy (out, ", ext");
4874 if (e_flags & EF_PARISC_LSB)
4875 out = stpcpy (out, ", lsb");
4876 if (e_flags & EF_PARISC_WIDE)
4877 out = stpcpy (out, ", wide");
4878 if (e_flags & EF_PARISC_NO_KABP)
4879 out = stpcpy (out, ", no kabp");
4880 if (e_flags & EF_PARISC_LAZYSWAP)
4881 out = stpcpy (out, ", lazyswap");
4882 return out;
4883 }
4884
4885 static char *
4886 decode_RISCV_machine_flags (char *out, unsigned e_flags)
4887 {
4888 if (e_flags & EF_RISCV_RVC)
4889 out = stpcpy (out, ", RVC");
4890
4891 if (e_flags & EF_RISCV_RVE)
4892 out = stpcpy (out, ", RVE");
4893
4894 if (e_flags & EF_RISCV_TSO)
4895 out = stpcpy (out, ", TSO");
4896
4897 switch (e_flags & EF_RISCV_FLOAT_ABI)
4898 {
4899 case EF_RISCV_FLOAT_ABI_SOFT:
4900 out = stpcpy (out, ", soft-float ABI");
4901 break;
4902
4903 case EF_RISCV_FLOAT_ABI_SINGLE:
4904 out = stpcpy (out, ", single-float ABI");
4905 break;
4906
4907 case EF_RISCV_FLOAT_ABI_DOUBLE:
4908 out = stpcpy (out, ", double-float ABI");
4909 break;
4910
4911 case EF_RISCV_FLOAT_ABI_QUAD:
4912 out = stpcpy (out, ", quad-float ABI");
4913 break;
4914 }
4915 return out;
4916 }
4917
4918 static char *
4919 decode_RL78_machine_flags (char *out, unsigned e_flags)
4920 {
4921 switch (e_flags & E_FLAG_RL78_CPU_MASK)
4922 {
4923 case E_FLAG_RL78_ANY_CPU:
4924 break;
4925 case E_FLAG_RL78_G10:
4926 out = stpcpy (out, ", G10");
4927 break;
4928 case E_FLAG_RL78_G13:
4929 out = stpcpy (out, ", G13");
4930 break;
4931 case E_FLAG_RL78_G14:
4932 out = stpcpy (out, ", G14");
4933 break;
4934 }
4935 if (e_flags & E_FLAG_RL78_64BIT_DOUBLES)
4936 out = stpcpy (out, ", 64-bit doubles");
4937 return out;
4938 }
4939
4940 static char *
4941 decode_RX_machine_flags (char *out, unsigned e_flags)
4942 {
4943 if (e_flags & E_FLAG_RX_64BIT_DOUBLES)
4944 out = stpcpy (out, ", 64-bit doubles");
4945 if (e_flags & E_FLAG_RX_DSP)
4946 out = stpcpy (out, ", dsp");
4947 if (e_flags & E_FLAG_RX_PID)
4948 out = stpcpy (out, ", pid");
4949 if (e_flags & E_FLAG_RX_ABI)
4950 out = stpcpy (out, ", RX ABI");
4951 if (e_flags & E_FLAG_RX_SINSNS_SET)
4952 out = stpcpy (out, (e_flags & E_FLAG_RX_SINSNS_YES
4953 ? ", uses String instructions"
4954 : ", bans String instructions"));
4955 if (e_flags & E_FLAG_RX_V2)
4956 out = stpcpy (out, ", V2");
4957 if (e_flags & E_FLAG_RX_V3)
4958 out = stpcpy (out, ", V3");
4959 return out;
4960 }
4961
4962 static char *
4963 decode_SH_machine_flags (char *out, unsigned e_flags)
4964 {
4965 switch ((e_flags & EF_SH_MACH_MASK))
4966 {
4967 case EF_SH1:
4968 out = stpcpy (out, ", sh1");
4969 break;
4970 case EF_SH2:
4971 out = stpcpy (out, ", sh2");
4972 break;
4973 case EF_SH3:
4974 out = stpcpy (out, ", sh3");
4975 break;
4976 case EF_SH_DSP:
4977 out = stpcpy (out, ", sh-dsp");
4978 break;
4979 case EF_SH3_DSP:
4980 out = stpcpy (out, ", sh3-dsp");
4981 break;
4982 case EF_SH4AL_DSP:
4983 out = stpcpy (out, ", sh4al-dsp");
4984 break;
4985 case EF_SH3E:
4986 out = stpcpy (out, ", sh3e");
4987 break;
4988 case EF_SH4:
4989 out = stpcpy (out, ", sh4");
4990 break;
4991 case EF_SH5:
4992 out = stpcpy (out, ", sh5");
4993 break;
4994 case EF_SH2E:
4995 out = stpcpy (out, ", sh2e");
4996 break;
4997 case EF_SH4A:
4998 out = stpcpy (out, ", sh4a");
4999 break;
5000 case EF_SH2A:
5001 out = stpcpy (out, ", sh2a");
5002 break;
5003 case EF_SH4_NOFPU:
5004 out = stpcpy (out, ", sh4-nofpu");
5005 break;
5006 case EF_SH4A_NOFPU:
5007 out = stpcpy (out, ", sh4a-nofpu");
5008 break;
5009 case EF_SH2A_NOFPU:
5010 out = stpcpy (out, ", sh2a-nofpu");
5011 break;
5012 case EF_SH3_NOMMU:
5013 out = stpcpy (out, ", sh3-nommu");
5014 break;
5015 case EF_SH4_NOMMU_NOFPU:
5016 out = stpcpy (out, ", sh4-nommu-nofpu");
5017 break;
5018 case EF_SH2A_SH4_NOFPU:
5019 out = stpcpy (out, ", sh2a-nofpu-or-sh4-nommu-nofpu");
5020 break;
5021 case EF_SH2A_SH3_NOFPU:
5022 out = stpcpy (out, ", sh2a-nofpu-or-sh3-nommu");
5023 break;
5024 case EF_SH2A_SH4:
5025 out = stpcpy (out, ", sh2a-or-sh4");
5026 break;
5027 case EF_SH2A_SH3E:
5028 out = stpcpy (out, ", sh2a-or-sh3e");
5029 break;
5030 default:
5031 out = stpcpy (out, _(", unknown ISA"));
5032 break;
5033 }
5034
5035 if (e_flags & EF_SH_PIC)
5036 out = stpcpy (out, ", pic");
5037
5038 if (e_flags & EF_SH_FDPIC)
5039 out = stpcpy (out, ", fdpic");
5040 return out;
5041 }
5042
5043 static char *
5044 decode_SPARC_machine_flags (char *out, unsigned e_flags)
5045 {
5046 if (e_flags & EF_SPARC_32PLUS)
5047 out = stpcpy (out, ", v8+");
5048
5049 if (e_flags & EF_SPARC_SUN_US1)
5050 out = stpcpy (out, ", ultrasparcI");
5051
5052 if (e_flags & EF_SPARC_SUN_US3)
5053 out = stpcpy (out, ", ultrasparcIII");
5054
5055 if (e_flags & EF_SPARC_HAL_R1)
5056 out = stpcpy (out, ", halr1");
5057
5058 if (e_flags & EF_SPARC_LEDATA)
5059 out = stpcpy (out, ", ledata");
5060
5061 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
5062 out = stpcpy (out, ", tso");
5063
5064 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
5065 out = stpcpy (out, ", pso");
5066
5067 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
5068 out = stpcpy (out, ", rmo");
5069 return out;
5070 }
5071
5072 static char *
5073 decode_V800_machine_flags (char *out, unsigned int e_flags)
5074 {
5075 if ((e_flags & EF_RH850_ABI) == EF_RH850_ABI)
5076 out = stpcpy (out, ", RH850 ABI");
5077
5078 if (e_flags & EF_V800_850E3)
5079 out = stpcpy (out, ", V3 architecture");
5080
5081 if ((e_flags & (EF_RH850_FPU_DOUBLE | EF_RH850_FPU_SINGLE)) == 0)
5082 out = stpcpy (out, ", FPU not used");
5083
5084 if ((e_flags & (EF_RH850_REGMODE22 | EF_RH850_REGMODE32)) == 0)
5085 out = stpcpy (out, ", regmode: COMMON");
5086
5087 if ((e_flags & (EF_RH850_GP_FIX | EF_RH850_GP_NOFIX)) == 0)
5088 out = stpcpy (out, ", r4 not used");
5089
5090 if ((e_flags & (EF_RH850_EP_FIX | EF_RH850_EP_NOFIX)) == 0)
5091 out = stpcpy (out, ", r30 not used");
5092
5093 if ((e_flags & (EF_RH850_TP_FIX | EF_RH850_TP_NOFIX)) == 0)
5094 out = stpcpy (out, ", r5 not used");
5095
5096 if ((e_flags & (EF_RH850_REG2_RESERVE | EF_RH850_REG2_NORESERVE)) == 0)
5097 out = stpcpy (out, ", r2 not used");
5098
5099 for (e_flags &= 0xFFFF; e_flags; e_flags &= ~ (e_flags & - e_flags))
5100 {
5101 switch (e_flags & - e_flags)
5102 {
5103 case EF_RH850_FPU_DOUBLE:
5104 out = stpcpy (out, ", double precision FPU");
5105 break;
5106 case EF_RH850_FPU_SINGLE:
5107 out = stpcpy (out, ", single precision FPU");
5108 break;
5109 case EF_RH850_REGMODE22:
5110 out = stpcpy (out, ", regmode:22");
5111 break;
5112 case EF_RH850_REGMODE32:
5113 out = stpcpy (out, ", regmode:23");
5114 break;
5115 case EF_RH850_GP_FIX:
5116 out = stpcpy (out, ", r4 fixed");
5117 break;
5118 case EF_RH850_GP_NOFIX:
5119 out = stpcpy (out, ", r4 free");
5120 break;
5121 case EF_RH850_EP_FIX:
5122 out = stpcpy (out, ", r30 fixed");
5123 break;
5124 case EF_RH850_EP_NOFIX:
5125 out = stpcpy (out, ", r30 free");
5126 break;
5127 case EF_RH850_TP_FIX:
5128 out = stpcpy (out, ", r5 fixed");
5129 break;
5130 case EF_RH850_TP_NOFIX:
5131 out = stpcpy (out, ", r5 free");
5132 break;
5133 case EF_RH850_REG2_RESERVE:
5134 out = stpcpy (out, ", r2 fixed");
5135 break;
5136 case EF_RH850_REG2_NORESERVE:
5137 out = stpcpy (out, ", r2 free");
5138 break;
5139 default:
5140 break;
5141 }
5142 }
5143 return out;
5144 }
5145
5146 static char *
5147 decode_V850_machine_flags (char *out, unsigned int e_flags)
5148 {
5149 switch (e_flags & EF_V850_ARCH)
5150 {
5151 case E_V850E3V5_ARCH:
5152 out = stpcpy (out, ", v850e3v5");
5153 break;
5154 case E_V850E2V3_ARCH:
5155 out = stpcpy (out, ", v850e2v3");
5156 break;
5157 case E_V850E2_ARCH:
5158 out = stpcpy (out, ", v850e2");
5159 break;
5160 case E_V850E1_ARCH:
5161 out = stpcpy (out, ", v850e1");
5162 break;
5163 case E_V850E_ARCH:
5164 out = stpcpy (out, ", v850e");
5165 break;
5166 case E_V850_ARCH:
5167 out = stpcpy (out, ", v850");
5168 break;
5169 default:
5170 out = stpcpy (out, _(", unknown v850 architecture variant"));
5171 break;
5172 }
5173 return out;
5174 }
5175
5176 static char *
5177 decode_Z80_machine_flags (char *out, unsigned int e_flags)
5178 {
5179 switch (e_flags & EF_Z80_MACH_MSK)
5180 {
5181 case EF_Z80_MACH_Z80:
5182 out = stpcpy (out, ", Z80");
5183 break;
5184 case EF_Z80_MACH_Z180:
5185 out = stpcpy (out, ", Z180");
5186 break;
5187 case EF_Z80_MACH_R800:
5188 out = stpcpy (out, ", R800");
5189 break;
5190 case EF_Z80_MACH_EZ80_Z80:
5191 out = stpcpy (out, ", EZ80");
5192 break;
5193 case EF_Z80_MACH_EZ80_ADL:
5194 out = stpcpy (out, ", EZ80, ADL");
5195 break;
5196 case EF_Z80_MACH_GBZ80:
5197 out = stpcpy (out, ", GBZ80");
5198 break;
5199 case EF_Z80_MACH_Z80N:
5200 out = stpcpy (out, ", Z80N");
5201 break;
5202 default:
5203 out = stpcpy (out, _(", unknown"));
5204 break;
5205 }
5206 return out;
5207 }
5208
5209 static char *
5210 decode_AMDGPU_machine_flags (char *out, unsigned int e_flags, Filedata *filedata)
5211 {
5212 unsigned char *e_ident = filedata->file_header.e_ident;
5213 unsigned char osabi = e_ident[EI_OSABI];
5214 unsigned char abiversion = e_ident[EI_ABIVERSION];
5215 unsigned int mach;
5216
5217 /* HSA OS ABI v2 used a different encoding, but we don't need to support it,
5218 it has been deprecated for a while.
5219
5220 The PAL, MESA3D and NONE OS ABIs are not properly versioned, at the time
5221 of writing, they use the same flags as HSA v3, so the code below uses that
5222 assumption. */
5223 if (osabi == ELFOSABI_AMDGPU_HSA && abiversion < ELFABIVERSION_AMDGPU_HSA_V3)
5224 return out;
5225
5226 mach = e_flags & EF_AMDGPU_MACH;
5227 switch (mach)
5228 {
5229 #define AMDGPU_CASE(code, string) \
5230 case code: out = stpcpy (out, ", " string); break;
5231 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX600, "gfx600")
5232 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX601, "gfx601")
5233 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX700, "gfx700")
5234 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX701, "gfx701")
5235 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX702, "gfx702")
5236 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX703, "gfx703")
5237 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX704, "gfx704")
5238 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX801, "gfx801")
5239 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX802, "gfx802")
5240 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX803, "gfx803")
5241 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX810, "gfx810")
5242 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX900, "gfx900")
5243 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX902, "gfx902")
5244 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX904, "gfx904")
5245 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX906, "gfx906")
5246 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX908, "gfx908")
5247 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX909, "gfx909")
5248 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX90C, "gfx90c")
5249 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1010, "gfx1010")
5250 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1011, "gfx1011")
5251 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1012, "gfx1012")
5252 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1030, "gfx1030")
5253 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1031, "gfx1031")
5254 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1032, "gfx1032")
5255 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1033, "gfx1033")
5256 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1100, "gfx1100")
5257 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1101, "gfx1101")
5258 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1102, "gfx1102")
5259 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX602, "gfx602")
5260 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX705, "gfx705")
5261 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX805, "gfx805")
5262 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1035, "gfx1035")
5263 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1034, "gfx1034")
5264 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX90A, "gfx90a")
5265 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX940, "gfx940")
5266 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1013, "gfx1013")
5267 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1036, "gfx1036")
5268 default:
5269 out += sprintf (out, _(", <unknown AMDGPU GPU type: %#x>"), mach);
5270 break;
5271 #undef AMDGPU_CASE
5272 }
5273
5274 e_flags &= ~EF_AMDGPU_MACH;
5275
5276 if ((osabi == ELFOSABI_AMDGPU_HSA
5277 && abiversion == ELFABIVERSION_AMDGPU_HSA_V3)
5278 || osabi != ELFOSABI_AMDGPU_HSA)
5279 {
5280 /* For HSA v3 and other OS ABIs. */
5281 if (e_flags & EF_AMDGPU_FEATURE_XNACK_V3)
5282 {
5283 out = stpcpy (out, ", xnack on");
5284 e_flags &= ~EF_AMDGPU_FEATURE_XNACK_V3;
5285 }
5286
5287 if (e_flags & EF_AMDGPU_FEATURE_SRAMECC_V3)
5288 {
5289 out = stpcpy (out, ", sramecc on");
5290 e_flags &= ~EF_AMDGPU_FEATURE_SRAMECC_V3;
5291 }
5292 }
5293 else
5294 {
5295 /* For HSA v4+. */
5296 int xnack, sramecc;
5297
5298 xnack = e_flags & EF_AMDGPU_FEATURE_XNACK_V4;
5299 switch (xnack)
5300 {
5301 case EF_AMDGPU_FEATURE_XNACK_UNSUPPORTED_V4:
5302 break;
5303
5304 case EF_AMDGPU_FEATURE_XNACK_ANY_V4:
5305 out = stpcpy (out, ", xnack any");
5306 break;
5307
5308 case EF_AMDGPU_FEATURE_XNACK_OFF_V4:
5309 out = stpcpy (out, ", xnack off");
5310 break;
5311
5312 case EF_AMDGPU_FEATURE_XNACK_ON_V4:
5313 out = stpcpy (out, ", xnack on");
5314 break;
5315
5316 default:
5317 out += sprintf (out, _(", <unknown xnack value: %#x>"), xnack);
5318 break;
5319 }
5320
5321 e_flags &= ~EF_AMDGPU_FEATURE_XNACK_V4;
5322
5323 sramecc = e_flags & EF_AMDGPU_FEATURE_SRAMECC_V4;
5324 switch (sramecc)
5325 {
5326 case EF_AMDGPU_FEATURE_SRAMECC_UNSUPPORTED_V4:
5327 break;
5328
5329 case EF_AMDGPU_FEATURE_SRAMECC_ANY_V4:
5330 out = stpcpy (out, ", sramecc any");
5331 break;
5332
5333 case EF_AMDGPU_FEATURE_SRAMECC_OFF_V4:
5334 out = stpcpy (out, ", sramecc off");
5335 break;
5336
5337 case EF_AMDGPU_FEATURE_SRAMECC_ON_V4:
5338 out = stpcpy (out, ", sramecc on");
5339 break;
5340
5341 default:
5342 out += sprintf (out, _(", <unknown sramecc value: %#x>"), sramecc);
5343 break;
5344 }
5345
5346 e_flags &= ~EF_AMDGPU_FEATURE_SRAMECC_V4;
5347 }
5348
5349 if (e_flags != 0)
5350 out += sprintf (out, _(", unknown flags bits: %#x"), e_flags);
5351 return out;
5352 }
5353
5354 static char *
5355 get_machine_flags (Filedata * filedata, unsigned e_flags, unsigned e_machine)
5356 {
5357 static char buf[1024];
5358 char *out = buf;
5359
5360 buf[0] = '\0';
5361
5362 if (e_flags)
5363 {
5364 switch (e_machine)
5365 {
5366 default:
5367 break;
5368
5369 case EM_ARC_COMPACT3:
5370 out = stpcpy (out, ", HS5x");
5371 break;
5372
5373 case EM_ARC_COMPACT3_64:
5374 out = stpcpy (out, ", HS6x");
5375 break;
5376
5377 case EM_ARC_COMPACT2:
5378 case EM_ARC_COMPACT:
5379 out = decode_ARC_machine_flags (out, e_flags, e_machine);
5380 break;
5381
5382 case EM_ARM:
5383 out = decode_ARM_machine_flags (out, e_flags);
5384 break;
5385
5386 case EM_AVR:
5387 out = decode_AVR_machine_flags (out, e_flags);
5388 break;
5389
5390 case EM_BLACKFIN:
5391 out = decode_BLACKFIN_machine_flags (out, e_flags);
5392 break;
5393
5394 case EM_CYGNUS_FRV:
5395 out = decode_FRV_machine_flags (out, e_flags);
5396 break;
5397
5398 case EM_68K:
5399 out = decode_M68K_machine_flags (out, e_flags);
5400 break;
5401
5402 case EM_AMDGPU:
5403 out = decode_AMDGPU_machine_flags (out, e_flags, filedata);
5404 break;
5405
5406 case EM_CYGNUS_MEP:
5407 out = decode_MeP_machine_flags (out, e_flags);
5408 break;
5409
5410 case EM_PPC:
5411 if (e_flags & EF_PPC_EMB)
5412 out = stpcpy (out, ", emb");
5413
5414 if (e_flags & EF_PPC_RELOCATABLE)
5415 out = stpcpy (out, _(", relocatable"));
5416
5417 if (e_flags & EF_PPC_RELOCATABLE_LIB)
5418 out = stpcpy (out, _(", relocatable-lib"));
5419 break;
5420
5421 case EM_PPC64:
5422 if (e_flags & EF_PPC64_ABI)
5423 out += sprintf (out, ", abiv%d", e_flags & EF_PPC64_ABI);
5424 break;
5425
5426 case EM_V800:
5427 out = decode_V800_machine_flags (out, e_flags);
5428 break;
5429
5430 case EM_V850:
5431 case EM_CYGNUS_V850:
5432 out = decode_V850_machine_flags (out, e_flags);
5433 break;
5434
5435 case EM_M32R:
5436 case EM_CYGNUS_M32R:
5437 if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
5438 out = stpcpy (out, ", m32r");
5439 break;
5440
5441 case EM_MIPS:
5442 case EM_MIPS_RS3_LE:
5443 out = decode_MIPS_machine_flags (out, e_flags);
5444 break;
5445
5446 case EM_NDS32:
5447 out = decode_NDS32_machine_flags (out, e_flags);
5448 break;
5449
5450 case EM_NFP:
5451 switch (EF_NFP_MACH (e_flags))
5452 {
5453 case E_NFP_MACH_3200:
5454 out = stpcpy (out, ", NFP-32xx");
5455 break;
5456 case E_NFP_MACH_6000:
5457 out = stpcpy (out, ", NFP-6xxx");
5458 break;
5459 }
5460 break;
5461
5462 case EM_RISCV:
5463 out = decode_RISCV_machine_flags (out, e_flags);
5464 break;
5465
5466 case EM_SH:
5467 out = decode_SH_machine_flags (out, e_flags);
5468 break;
5469
5470 case EM_OR1K:
5471 if (e_flags & EF_OR1K_NODELAY)
5472 out = stpcpy (out, ", no delay");
5473 break;
5474
5475 case EM_BPF:
5476 out += sprintf (out, ", CPU Version: %u", e_flags & EF_BPF_CPUVER);
5477 break;
5478
5479 case EM_SPARCV9:
5480 out = decode_SPARC_machine_flags (out, e_flags);
5481 break;
5482
5483 case EM_PARISC:
5484 out = decode_PARISC_machine_flags (out, e_flags);
5485 break;
5486
5487 case EM_PJ:
5488 case EM_PJ_OLD:
5489 if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
5490 out = stpcpy (out, ", new calling convention");
5491
5492 if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
5493 out = stpcpy (out, ", gnu calling convention");
5494 break;
5495
5496 case EM_IA_64:
5497 out = decode_IA64_machine_flags (out, e_flags, filedata);
5498 break;
5499
5500 case EM_VAX:
5501 if ((e_flags & EF_VAX_NONPIC))
5502 out = stpcpy (out, ", non-PIC");
5503 if ((e_flags & EF_VAX_DFLOAT))
5504 out = stpcpy (out, ", D-Float");
5505 if ((e_flags & EF_VAX_GFLOAT))
5506 out = stpcpy (out, ", G-Float");
5507 break;
5508
5509 case EM_VISIUM:
5510 if (e_flags & EF_VISIUM_ARCH_MCM)
5511 out = stpcpy (out, ", mcm");
5512 else if (e_flags & EF_VISIUM_ARCH_MCM24)
5513 out = stpcpy (out, ", mcm24");
5514 if (e_flags & EF_VISIUM_ARCH_GR6)
5515 out = stpcpy (out, ", gr6");
5516 break;
5517
5518 case EM_RL78:
5519 out = decode_RL78_machine_flags (out, e_flags);
5520 break;
5521
5522 case EM_RX:
5523 out = decode_RX_machine_flags (out, e_flags);
5524 break;
5525
5526 case EM_S390:
5527 if (e_flags & EF_S390_HIGH_GPRS)
5528 out = stpcpy (out, ", highgprs");
5529 break;
5530
5531 case EM_TI_C6000:
5532 if ((e_flags & EF_C6000_REL))
5533 out = stpcpy (out, ", relocatable module");
5534 break;
5535
5536 case EM_KVX:
5537 if ((e_flags & (ELF_KVX_CORE_MAJOR_MASK | ELF_KVX_CORE_MINOR_MASK)) == ELF_KVX_CORE_KV3_1)
5538 strcat (buf, ", Kalray VLIW kv3-1");
5539 else if ((e_flags & (ELF_KVX_CORE_MAJOR_MASK | ELF_KVX_CORE_MINOR_MASK)) == ELF_KVX_CORE_KV3_2)
5540 strcat (buf, ", Kalray VLIW kv3-2");
5541 else if ((e_flags & (ELF_KVX_CORE_MAJOR_MASK | ELF_KVX_CORE_MINOR_MASK)) == ELF_KVX_CORE_KV4_1)
5542 strcat (buf, ", Kalray VLIW kv4-1");
5543 else
5544 strcat (buf, ", unknown KVX MPPA");
5545 break;
5546
5547 case EM_MSP430:
5548 out = decode_MSP430_machine_flags (out, e_flags);
5549 break;
5550
5551 case EM_Z80:
5552 out = decode_Z80_machine_flags (out, e_flags);
5553 break;
5554
5555 case EM_LOONGARCH:
5556 out = decode_LOONGARCH_machine_flags (out, e_flags);
5557 break;
5558 }
5559 }
5560
5561 return buf;
5562 }
5563
5564 static const char *
5565 get_osabi_name (Filedata * filedata, unsigned int osabi)
5566 {
5567 static char buff[32];
5568
5569 switch (osabi)
5570 {
5571 case ELFOSABI_NONE: return "UNIX - System V";
5572 case ELFOSABI_HPUX: return "UNIX - HP-UX";
5573 case ELFOSABI_NETBSD: return "UNIX - NetBSD";
5574 case ELFOSABI_GNU: return "UNIX - GNU";
5575 case ELFOSABI_SOLARIS: return "UNIX - Solaris";
5576 case ELFOSABI_AIX: return "UNIX - AIX";
5577 case ELFOSABI_IRIX: return "UNIX - IRIX";
5578 case ELFOSABI_FREEBSD: return "UNIX - FreeBSD";
5579 case ELFOSABI_TRU64: return "UNIX - TRU64";
5580 case ELFOSABI_MODESTO: return "Novell - Modesto";
5581 case ELFOSABI_OPENBSD: return "UNIX - OpenBSD";
5582 case ELFOSABI_OPENVMS: return "VMS - OpenVMS";
5583 case ELFOSABI_NSK: return "HP - Non-Stop Kernel";
5584 case ELFOSABI_AROS: return "AROS";
5585 case ELFOSABI_FENIXOS: return "FenixOS";
5586 case ELFOSABI_CLOUDABI: return "Nuxi CloudABI";
5587 case ELFOSABI_OPENVOS: return "Stratus Technologies OpenVOS";
5588 case ELFOSABI_CUDA: return "CUDA";
5589 default:
5590 if (osabi >= 64)
5591 switch (filedata->file_header.e_machine)
5592 {
5593 case EM_AMDGPU:
5594 switch (osabi)
5595 {
5596 case ELFOSABI_AMDGPU_HSA: return "AMD HSA";
5597 case ELFOSABI_AMDGPU_PAL: return "AMD PAL";
5598 case ELFOSABI_AMDGPU_MESA3D: return "AMD Mesa3D";
5599 default:
5600 break;
5601 }
5602 break;
5603
5604 case EM_ARM:
5605 switch (osabi)
5606 {
5607 case ELFOSABI_ARM: return "ARM";
5608 case ELFOSABI_ARM_FDPIC: return "ARM FDPIC";
5609 default:
5610 break;
5611 }
5612 break;
5613
5614 case EM_MSP430:
5615 case EM_MSP430_OLD:
5616 case EM_VISIUM:
5617 switch (osabi)
5618 {
5619 case ELFOSABI_STANDALONE: return _("Standalone App");
5620 default:
5621 break;
5622 }
5623 break;
5624
5625 case EM_TI_C6000:
5626 switch (osabi)
5627 {
5628 case ELFOSABI_C6000_ELFABI: return _("Bare-metal C6000");
5629 case ELFOSABI_C6000_LINUX: return "Linux C6000";
5630 default:
5631 break;
5632 }
5633 break;
5634
5635 default:
5636 break;
5637 }
5638 snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
5639 return buff;
5640 }
5641 }
5642
5643 static const char *
5644 get_aarch64_segment_type (unsigned long type)
5645 {
5646 switch (type)
5647 {
5648 case PT_AARCH64_ARCHEXT: return "AARCH64_ARCHEXT";
5649 case PT_AARCH64_MEMTAG_MTE: return "AARCH64_MEMTAG_MTE";
5650 default: return NULL;
5651 }
5652 }
5653
5654 static const char *
5655 get_arm_segment_type (unsigned long type)
5656 {
5657 switch (type)
5658 {
5659 case PT_ARM_ARCHEXT: return "ARM_ARCHEXT";
5660 case PT_ARM_EXIDX: return "ARM_EXIDX";
5661 default: return NULL;
5662 }
5663 }
5664
5665 static const char *
5666 get_s390_segment_type (unsigned long type)
5667 {
5668 switch (type)
5669 {
5670 case PT_S390_PGSTE: return "S390_PGSTE";
5671 default: return NULL;
5672 }
5673 }
5674
5675 static const char *
5676 get_mips_segment_type (unsigned long type)
5677 {
5678 switch (type)
5679 {
5680 case PT_MIPS_REGINFO: return "REGINFO";
5681 case PT_MIPS_RTPROC: return "RTPROC";
5682 case PT_MIPS_OPTIONS: return "OPTIONS";
5683 case PT_MIPS_ABIFLAGS: return "ABIFLAGS";
5684 default: return NULL;
5685 }
5686 }
5687
5688 static const char *
5689 get_parisc_segment_type (unsigned long type)
5690 {
5691 switch (type)
5692 {
5693 case PT_PARISC_ARCHEXT: return "PARISC_ARCHEXT";
5694 case PT_PARISC_UNWIND: return "PARISC_UNWIND";
5695 case PT_PARISC_WEAKORDER: return "PARISC_WEAKORDER";
5696 default: return NULL;
5697 }
5698 }
5699
5700 static const char *
5701 get_ia64_segment_type (unsigned long type)
5702 {
5703 switch (type)
5704 {
5705 case PT_IA_64_ARCHEXT: return "IA_64_ARCHEXT";
5706 case PT_IA_64_UNWIND: return "IA_64_UNWIND";
5707 default: return NULL;
5708 }
5709 }
5710
5711 static const char *
5712 get_tic6x_segment_type (unsigned long type)
5713 {
5714 switch (type)
5715 {
5716 case PT_C6000_PHATTR: return "C6000_PHATTR";
5717 default: return NULL;
5718 }
5719 }
5720
5721 static const char *
5722 get_riscv_segment_type (unsigned long type)
5723 {
5724 switch (type)
5725 {
5726 case PT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
5727 default: return NULL;
5728 }
5729 }
5730
5731 static const char *
5732 get_hpux_segment_type (unsigned long type, unsigned e_machine)
5733 {
5734 if (e_machine == EM_PARISC)
5735 switch (type)
5736 {
5737 case PT_HP_TLS: return "HP_TLS";
5738 case PT_HP_CORE_NONE: return "HP_CORE_NONE";
5739 case PT_HP_CORE_VERSION: return "HP_CORE_VERSION";
5740 case PT_HP_CORE_KERNEL: return "HP_CORE_KERNEL";
5741 case PT_HP_CORE_COMM: return "HP_CORE_COMM";
5742 case PT_HP_CORE_PROC: return "HP_CORE_PROC";
5743 case PT_HP_CORE_LOADABLE: return "HP_CORE_LOADABLE";
5744 case PT_HP_CORE_STACK: return "HP_CORE_STACK";
5745 case PT_HP_CORE_SHM: return "HP_CORE_SHM";
5746 case PT_HP_CORE_MMF: return "HP_CORE_MMF";
5747 case PT_HP_PARALLEL: return "HP_PARALLEL";
5748 case PT_HP_FASTBIND: return "HP_FASTBIND";
5749 case PT_HP_OPT_ANNOT: return "HP_OPT_ANNOT";
5750 case PT_HP_HSL_ANNOT: return "HP_HSL_ANNOT";
5751 case PT_HP_STACK: return "HP_STACK";
5752 case PT_HP_CORE_UTSNAME: return "HP_CORE_UTSNAME";
5753 default:
5754 break;
5755 }
5756
5757 if (e_machine == EM_IA_64)
5758 switch (type)
5759 {
5760 case PT_HP_TLS: return "HP_TLS";
5761 case PT_IA_64_HP_OPT_ANOT: return "HP_OPT_ANNOT";
5762 case PT_IA_64_HP_HSL_ANOT: return "HP_HSL_ANNOT";
5763 case PT_IA_64_HP_STACK: return "HP_STACK";
5764 default:
5765 break;
5766 }
5767
5768 return NULL;
5769 }
5770
5771 static const char *
5772 get_solaris_segment_type (unsigned long type)
5773 {
5774 switch (type)
5775 {
5776 case PT_SUNW_UNWIND: return "SUNW_UNWIND";
5777 case PT_SUNW_EH_FRAME: return "SUNW_EH_FRAME";
5778 case PT_SUNWBSS: return "SUNW_BSS";
5779 case PT_SUNWSTACK: return "SUNW_STACK";
5780 case PT_SUNWDTRACE: return "SUNW_DTRACE";
5781 case PT_SUNWCAP: return "SUNW_CAP";
5782 default: return NULL;
5783 }
5784 }
5785
5786 static const char *
5787 get_os_specific_segment_type (Filedata * filedata, unsigned long p_type)
5788 {
5789 static char buff[32];
5790 const char * result = NULL;
5791
5792 switch (filedata->file_header.e_ident[EI_OSABI])
5793 {
5794 case ELFOSABI_GNU:
5795 case ELFOSABI_FREEBSD:
5796 if (p_type >= PT_GNU_MBIND_LO && p_type <= PT_GNU_MBIND_HI)
5797 {
5798 sprintf (buff, "GNU_MBIND+%#lx", p_type - PT_GNU_MBIND_LO);
5799 result = buff;
5800 }
5801 break;
5802
5803 case ELFOSABI_HPUX:
5804 result = get_hpux_segment_type (p_type,
5805 filedata->file_header.e_machine);
5806 break;
5807
5808 case ELFOSABI_SOLARIS:
5809 result = get_solaris_segment_type (p_type);
5810 break;
5811
5812 default:
5813 break;
5814 }
5815
5816 if (result != NULL)
5817 return result;
5818
5819 switch (p_type)
5820 {
5821 case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
5822 case PT_GNU_STACK: return "GNU_STACK";
5823 case PT_GNU_RELRO: return "GNU_RELRO";
5824 case PT_GNU_PROPERTY: return "GNU_PROPERTY";
5825 case PT_GNU_SFRAME: return "GNU_SFRAME";
5826
5827 case PT_OPENBSD_MUTABLE: return "OPENBSD_MUTABLE";
5828 case PT_OPENBSD_RANDOMIZE: return "OPENBSD_RANDOMIZE";
5829 case PT_OPENBSD_WXNEEDED: return "OPENBSD_WXNEEDED";
5830 case PT_OPENBSD_NOBTCFI: return "OPENBSD_NOBTCFI";
5831 case PT_OPENBSD_SYSCALLS: return "OPENBSD_SYSCALLS";
5832 case PT_OPENBSD_BOOTDATA: return "OPENBSD_BOOTDATA";
5833
5834 default:
5835 break;
5836 }
5837
5838 sprintf (buff, "LOOS+%#lx", p_type - PT_LOOS);
5839 return buff;
5840 }
5841
5842 static const char *
5843 get_processor_specific_segment_type (Filedata * filedata, unsigned long p_type)
5844 {
5845 static char buff[32];
5846 const char * result = NULL;
5847
5848 switch (filedata->file_header.e_machine)
5849 {
5850 case EM_AARCH64:
5851 result = get_aarch64_segment_type (p_type);
5852 break;
5853
5854 case EM_ARM:
5855 result = get_arm_segment_type (p_type);
5856 break;
5857
5858 case EM_MIPS:
5859 case EM_MIPS_RS3_LE:
5860 result = get_mips_segment_type (p_type);
5861 break;
5862
5863 case EM_PARISC:
5864 result = get_parisc_segment_type (p_type);
5865 break;
5866
5867 case EM_IA_64:
5868 result = get_ia64_segment_type (p_type);
5869 break;
5870
5871 case EM_TI_C6000:
5872 result = get_tic6x_segment_type (p_type);
5873 break;
5874
5875 case EM_S390:
5876 case EM_S390_OLD:
5877 result = get_s390_segment_type (p_type);
5878 break;
5879
5880 case EM_RISCV:
5881 result = get_riscv_segment_type (p_type);
5882 break;
5883
5884 default:
5885 result = NULL;
5886 break;
5887 }
5888
5889 if (result != NULL)
5890 return result;
5891
5892 sprintf (buff, "LOPROC+%#lx", p_type - PT_LOPROC);
5893 return buff;
5894 }
5895
5896 static const char *
5897 get_segment_type (Filedata * filedata, unsigned long p_type)
5898 {
5899 static char buff[32];
5900
5901 switch (p_type)
5902 {
5903 case PT_NULL: return "NULL";
5904 case PT_LOAD: return "LOAD";
5905 case PT_DYNAMIC: return "DYNAMIC";
5906 case PT_INTERP: return "INTERP";
5907 case PT_NOTE: return "NOTE";
5908 case PT_SHLIB: return "SHLIB";
5909 case PT_PHDR: return "PHDR";
5910 case PT_TLS: return "TLS";
5911 case PT_NUM: return "NUM";
5912 }
5913
5914 if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
5915 return get_os_specific_segment_type (filedata, p_type);
5916
5917 if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
5918 return get_processor_specific_segment_type (filedata, p_type);
5919
5920 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
5921 return buff;
5922 }
5923
5924 static const char *
5925 get_arc_section_type_name (unsigned int sh_type)
5926 {
5927 switch (sh_type)
5928 {
5929 case SHT_ARC_ATTRIBUTES: return "ARC_ATTRIBUTES";
5930 default:
5931 break;
5932 }
5933 return NULL;
5934 }
5935
5936 static const char *
5937 get_mips_section_type_name (unsigned int sh_type)
5938 {
5939 switch (sh_type)
5940 {
5941 case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
5942 case SHT_MIPS_MSYM: return "MIPS_MSYM";
5943 case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
5944 case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
5945 case SHT_MIPS_UCODE: return "MIPS_UCODE";
5946 case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
5947 case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
5948 case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
5949 case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
5950 case SHT_MIPS_RELD: return "MIPS_RELD";
5951 case SHT_MIPS_IFACE: return "MIPS_IFACE";
5952 case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
5953 case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
5954 case SHT_MIPS_SHDR: return "MIPS_SHDR";
5955 case SHT_MIPS_FDESC: return "MIPS_FDESC";
5956 case SHT_MIPS_EXTSYM: return "MIPS_EXTSYM";
5957 case SHT_MIPS_DENSE: return "MIPS_DENSE";
5958 case SHT_MIPS_PDESC: return "MIPS_PDESC";
5959 case SHT_MIPS_LOCSYM: return "MIPS_LOCSYM";
5960 case SHT_MIPS_AUXSYM: return "MIPS_AUXSYM";
5961 case SHT_MIPS_OPTSYM: return "MIPS_OPTSYM";
5962 case SHT_MIPS_LOCSTR: return "MIPS_LOCSTR";
5963 case SHT_MIPS_LINE: return "MIPS_LINE";
5964 case SHT_MIPS_RFDESC: return "MIPS_RFDESC";
5965 case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
5966 case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
5967 case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
5968 case SHT_MIPS_DWARF: return "MIPS_DWARF";
5969 case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
5970 case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
5971 case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
5972 case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
5973 case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
5974 case SHT_MIPS_XLATE: return "MIPS_XLATE";
5975 case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
5976 case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
5977 case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
5978 case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
5979 case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
5980 case SHT_MIPS_ABIFLAGS: return "MIPS_ABIFLAGS";
5981 case SHT_MIPS_XHASH: return "MIPS_XHASH";
5982 default:
5983 break;
5984 }
5985 return NULL;
5986 }
5987
5988 static const char *
5989 get_parisc_section_type_name (unsigned int sh_type)
5990 {
5991 switch (sh_type)
5992 {
5993 case SHT_PARISC_EXT: return "PARISC_EXT";
5994 case SHT_PARISC_UNWIND: return "PARISC_UNWIND";
5995 case SHT_PARISC_DOC: return "PARISC_DOC";
5996 case SHT_PARISC_ANNOT: return "PARISC_ANNOT";
5997 case SHT_PARISC_DLKM: return "PARISC_DLKM";
5998 case SHT_PARISC_SYMEXTN: return "PARISC_SYMEXTN";
5999 case SHT_PARISC_STUBS: return "PARISC_STUBS";
6000 default: return NULL;
6001 }
6002 }
6003
6004 static const char *
6005 get_ia64_section_type_name (Filedata * filedata, unsigned int sh_type)
6006 {
6007 /* If the top 8 bits are 0x78 the next 8 are the os/abi ID. */
6008 if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
6009 return get_osabi_name (filedata, (sh_type & 0x00FF0000) >> 16);
6010
6011 switch (sh_type)
6012 {
6013 case SHT_IA_64_EXT: return "IA_64_EXT";
6014 case SHT_IA_64_UNWIND: return "IA_64_UNWIND";
6015 case SHT_IA_64_PRIORITY_INIT: return "IA_64_PRIORITY_INIT";
6016 default:
6017 break;
6018 }
6019 return NULL;
6020 }
6021
6022 static const char *
6023 get_vms_section_type_name (unsigned int sh_type)
6024 {
6025 switch (sh_type)
6026 {
6027 case SHT_IA_64_VMS_TRACE: return "VMS_TRACE";
6028 case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
6029 case SHT_IA_64_VMS_DEBUG: return "VMS_DEBUG";
6030 case SHT_IA_64_VMS_DEBUG_STR: return "VMS_DEBUG_STR";
6031 case SHT_IA_64_VMS_LINKAGES: return "VMS_LINKAGES";
6032 case SHT_IA_64_VMS_SYMBOL_VECTOR: return "VMS_SYMBOL_VECTOR";
6033 case SHT_IA_64_VMS_FIXUP: return "VMS_FIXUP";
6034 default:
6035 break;
6036 }
6037 return NULL;
6038 }
6039
6040 static const char *
6041 get_x86_64_section_type_name (unsigned int sh_type)
6042 {
6043 switch (sh_type)
6044 {
6045 case SHT_X86_64_UNWIND: return "X86_64_UNWIND";
6046 default: return NULL;
6047 }
6048 }
6049
6050 static const char *
6051 get_aarch64_section_type_name (unsigned int sh_type)
6052 {
6053 switch (sh_type)
6054 {
6055 case SHT_AARCH64_ATTRIBUTES:
6056 return "AARCH64_ATTRIBUTES";
6057 case SHT_AARCH64_AUTH_RELR:
6058 return "AARCH64_AUTH_RELR";
6059 case SHT_AARCH64_MEMTAG_GLOBALS_STATIC:
6060 return "AARCH64_MEMTAG_GLOBALS_STATIC";
6061 case SHT_AARCH64_MEMTAG_GLOBALS_DYNAMIC:
6062 return "AARCH64_MEMTAG_GLOBALS_DYNAMIC";
6063 default:
6064 return NULL;
6065 }
6066 }
6067
6068 static const char *
6069 get_arm_section_type_name (unsigned int sh_type)
6070 {
6071 switch (sh_type)
6072 {
6073 case SHT_ARM_EXIDX: return "ARM_EXIDX";
6074 case SHT_ARM_PREEMPTMAP: return "ARM_PREEMPTMAP";
6075 case SHT_ARM_ATTRIBUTES: return "ARM_ATTRIBUTES";
6076 case SHT_ARM_DEBUGOVERLAY: return "ARM_DEBUGOVERLAY";
6077 case SHT_ARM_OVERLAYSECTION: return "ARM_OVERLAYSECTION";
6078 default: return NULL;
6079 }
6080 }
6081
6082 static const char *
6083 get_tic6x_section_type_name (unsigned int sh_type)
6084 {
6085 switch (sh_type)
6086 {
6087 case SHT_C6000_UNWIND: return "C6000_UNWIND";
6088 case SHT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
6089 case SHT_C6000_ATTRIBUTES: return "C6000_ATTRIBUTES";
6090 case SHT_TI_ICODE: return "TI_ICODE";
6091 case SHT_TI_XREF: return "TI_XREF";
6092 case SHT_TI_HANDLER: return "TI_HANDLER";
6093 case SHT_TI_INITINFO: return "TI_INITINFO";
6094 case SHT_TI_PHATTRS: return "TI_PHATTRS";
6095 default: return NULL;
6096 }
6097 }
6098
6099 static const char *
6100 get_msp430_section_type_name (unsigned int sh_type)
6101 {
6102 switch (sh_type)
6103 {
6104 case SHT_MSP430_SEC_FLAGS: return "MSP430_SEC_FLAGS";
6105 case SHT_MSP430_SYM_ALIASES: return "MSP430_SYM_ALIASES";
6106 case SHT_MSP430_ATTRIBUTES: return "MSP430_ATTRIBUTES";
6107 default: return NULL;
6108 }
6109 }
6110
6111 static const char *
6112 get_nfp_section_type_name (unsigned int sh_type)
6113 {
6114 switch (sh_type)
6115 {
6116 case SHT_NFP_MECONFIG: return "NFP_MECONFIG";
6117 case SHT_NFP_INITREG: return "NFP_INITREG";
6118 case SHT_NFP_UDEBUG: return "NFP_UDEBUG";
6119 default: return NULL;
6120 }
6121 }
6122
6123 static const char *
6124 get_v850_section_type_name (unsigned int sh_type)
6125 {
6126 switch (sh_type)
6127 {
6128 case SHT_V850_SCOMMON: return "V850 Small Common";
6129 case SHT_V850_TCOMMON: return "V850 Tiny Common";
6130 case SHT_V850_ZCOMMON: return "V850 Zero Common";
6131 case SHT_RENESAS_IOP: return "RENESAS IOP";
6132 case SHT_RENESAS_INFO: return "RENESAS INFO";
6133 default: return NULL;
6134 }
6135 }
6136
6137 static const char *
6138 get_riscv_section_type_name (unsigned int sh_type)
6139 {
6140 switch (sh_type)
6141 {
6142 case SHT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
6143 default: return NULL;
6144 }
6145 }
6146
6147 static const char *
6148 get_csky_section_type_name (unsigned int sh_type)
6149 {
6150 switch (sh_type)
6151 {
6152 case SHT_CSKY_ATTRIBUTES: return "CSKY_ATTRIBUTES";
6153 default: return NULL;
6154 }
6155 }
6156
6157 static const char *
6158 get_powerpc_section_type_name (unsigned int sh_type)
6159 {
6160 switch (sh_type)
6161 {
6162 case SHT_ORDERED: return "ORDERED";
6163 default: return NULL;
6164 }
6165 }
6166
6167 static const char *
6168 get_alpha_section_type_name (unsigned int sh_type)
6169 {
6170 switch (sh_type)
6171 {
6172 case SHT_ALPHA_DEBUG: return "DEBUG";
6173 case SHT_ALPHA_REGINFO: return "REGINFO";
6174 default: return NULL;
6175 }
6176 }
6177
6178 static const char *
6179 get_processor_specific_section_type_name (Filedata * filedata, unsigned int sh_type)
6180 {
6181 static char buff[32];
6182 const char * result = NULL;
6183
6184 switch (filedata->file_header.e_machine)
6185 {
6186 case EM_AARCH64:
6187 result = get_aarch64_section_type_name (sh_type);
6188 break;
6189
6190 case EM_ALPHA:
6191 result = get_alpha_section_type_name (sh_type);
6192 break;
6193
6194 case EM_ARC:
6195 case EM_ARC_COMPACT:
6196 case EM_ARC_COMPACT2:
6197 case EM_ARC_COMPACT3:
6198 case EM_ARC_COMPACT3_64:
6199 result = get_arc_section_type_name (sh_type);
6200 break;
6201
6202 case EM_ARM:
6203 result = get_arm_section_type_name (sh_type);
6204 break;
6205
6206 case EM_CSKY:
6207 result = get_csky_section_type_name (sh_type);
6208 break;
6209
6210 case EM_IA_64:
6211 result = get_ia64_section_type_name (filedata, sh_type);
6212 break;
6213
6214 case EM_MIPS:
6215 case EM_MIPS_RS3_LE:
6216 result = get_mips_section_type_name (sh_type);
6217 break;
6218
6219 case EM_MSP430:
6220 result = get_msp430_section_type_name (sh_type);
6221 break;
6222
6223 case EM_NFP:
6224 result = get_nfp_section_type_name (sh_type);
6225 break;
6226
6227 case EM_PARISC:
6228 result = get_parisc_section_type_name (sh_type);
6229 break;
6230
6231 case EM_PPC64:
6232 case EM_PPC:
6233 return get_powerpc_section_type_name (sh_type);
6234 break;
6235
6236 case EM_RISCV:
6237 result = get_riscv_section_type_name (sh_type);
6238 break;
6239
6240 case EM_TI_C6000:
6241 result = get_tic6x_section_type_name (sh_type);
6242 break;
6243
6244 case EM_V800:
6245 case EM_V850:
6246 case EM_CYGNUS_V850:
6247 result = get_v850_section_type_name (sh_type);
6248 break;
6249
6250 case EM_X86_64:
6251 case EM_L1OM:
6252 case EM_K1OM:
6253 result = get_x86_64_section_type_name (sh_type);
6254 break;
6255
6256 default:
6257 break;
6258 }
6259
6260 if (result != NULL)
6261 return result;
6262
6263 switch (sh_type)
6264 {
6265 /* FIXME: Are these correct ? If so, why do they not have #define's ? */
6266 case 0x7ffffffd: return "AUXILIARY";
6267 case 0x7fffffff: return "FILTER";
6268 default:
6269 break;
6270 }
6271
6272 sprintf (buff, "LOPROC+%#x", sh_type - SHT_LOPROC);
6273 return buff;
6274 }
6275
6276 static const char *
6277 get_os_specific_section_type_name (Filedata * filedata, unsigned int sh_type)
6278 {
6279 static char buff[32];
6280 const char * result = NULL;
6281
6282 switch (filedata->file_header.e_machine)
6283 {
6284 case EM_IA_64:
6285 result = get_vms_section_type_name (sh_type);
6286 break;
6287 default:
6288 break;
6289 }
6290
6291 if (result != NULL)
6292 return result;
6293
6294 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
6295 result = get_solaris_section_type (sh_type);
6296
6297 if (result != NULL)
6298 return result;
6299
6300 switch (sh_type)
6301 {
6302 case SHT_GNU_INCREMENTAL_INPUTS: return "GNU_INCREMENTAL_INPUTS";
6303 case SHT_GNU_ATTRIBUTES: return "GNU_ATTRIBUTES";
6304 case SHT_GNU_HASH: return "GNU_HASH";
6305 case SHT_GNU_LIBLIST: return "GNU_LIBLIST";
6306 case SHT_GNU_OBJECT_ONLY: return "GNU_OBJECT_ONLY";
6307 case SHT_GNU_SFRAME: return "GNU_SFRAME";
6308
6309 case SHT_SUNW_move: return "SUNW_MOVE";
6310 case SHT_SUNW_COMDAT: return "SUNW_COMDAT";
6311 case SHT_SUNW_syminfo: return "SUNW_SYMINFO";
6312 case SHT_GNU_verdef: return "VERDEF";
6313 case SHT_GNU_verneed: return "VERNEED";
6314 case SHT_GNU_versym: return "VERSYM";
6315
6316 case SHT_LLVM_ODRTAB: return "LLVM_ODRTAB";
6317 case SHT_LLVM_LINKER_OPTIONS: return "LLVM_LINKER_OPTIONS";
6318 case SHT_LLVM_ADDRSIG: return "LLVM_ADDRSIG";
6319 case SHT_LLVM_DEPENDENT_LIBRARIES: return "LLVM_DEPENDENT_LIBRARIES";
6320 case SHT_LLVM_SYMPART: return "LLVM_SYMPART";
6321 case SHT_LLVM_PART_EHDR: return "LLVM_PART_EHDR";
6322 case SHT_LLVM_PART_PHDR: return "LLVM_PART_PHDR";
6323 case SHT_LLVM_BB_ADDR_MAP_V0: return "LLVM_BB_ADDR_MAP_V0";
6324 case SHT_LLVM_CALL_GRAPH_PROFILE: return "LLVM_CALL_GRAPH_PROFILE";
6325 case SHT_LLVM_BB_ADDR_MAP: return "LLVM_BB_ADDR_MAP";
6326 case SHT_LLVM_OFFLOADING: return "LLVM_OFFLOADING";
6327 case SHT_LLVM_LTO: return "LLVM_LTO";
6328
6329 case SHT_ANDROID_REL: return "ANDROID_REL";
6330 case SHT_ANDROID_RELA: return "ANDROID_RELA";
6331 case SHT_ANDROID_RELR: return "ANDROID_RELR";
6332
6333 case SHT_CHECKSUM: return "CHECKSUM";
6334
6335 /* FIXME: Are these correct ? If so, why do they not have #define's ? */
6336 case 0x6ffffff0: return "VERSYM";
6337
6338 default:
6339 break;
6340 }
6341
6342 sprintf (buff, "LOOS+%#x", sh_type - SHT_LOOS);
6343 return buff;
6344 }
6345
6346 static const char *
6347 get_user_specific_section_type_name (Filedata * filedata, unsigned int sh_type)
6348 {
6349 static char buff[32];
6350 const char * result;
6351
6352 switch (filedata->file_header.e_machine)
6353 {
6354 case EM_V800:
6355 case EM_V850:
6356 case EM_CYGNUS_V850:
6357 result = get_v850_section_type_name (sh_type);
6358 break;
6359
6360 default:
6361 result = NULL;
6362 break;
6363 }
6364
6365 if (result != NULL)
6366 return result;
6367
6368 sprintf (buff, "LOUSER+%#x", sh_type - SHT_LOUSER);
6369 return buff;
6370 }
6371
6372 static const char *
6373 get_section_type_name (Filedata * filedata,
6374 unsigned int sh_type)
6375 {
6376 switch (sh_type)
6377 {
6378 case SHT_NULL: return "NULL";
6379 case SHT_PROGBITS: return "PROGBITS";
6380 case SHT_SYMTAB: return "SYMTAB";
6381 case SHT_STRTAB: return "STRTAB";
6382 case SHT_RELA: return "RELA";
6383 case SHT_HASH: return "HASH";
6384 case SHT_DYNAMIC: return "DYNAMIC";
6385 case SHT_NOTE: return "NOTE";
6386 case SHT_NOBITS: return "NOBITS";
6387 case SHT_REL: return "REL";
6388 case SHT_SHLIB: return "SHLIB";
6389 case SHT_DYNSYM: return "DYNSYM";
6390 /* 12 and 13 are not defined. */
6391 case SHT_INIT_ARRAY: return "INIT_ARRAY";
6392 case SHT_FINI_ARRAY: return "FINI_ARRAY";
6393 case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
6394 case SHT_GROUP: return "GROUP";
6395 case SHT_SYMTAB_SHNDX: return "SYMTAB SECTION INDICES";
6396 case SHT_RELR: return "RELR";
6397 /* End of generic section types. */
6398
6399 default:
6400 break;
6401 }
6402
6403 if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
6404 return get_processor_specific_section_type_name (filedata, sh_type);
6405
6406 if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
6407 return get_os_specific_section_type_name (filedata, sh_type);
6408
6409 if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
6410 return get_user_specific_section_type_name (filedata, sh_type);
6411
6412 static char buff[32];
6413
6414 /* This message is probably going to be displayed in a 15
6415 character wide field, so put the hex value first. */
6416 snprintf (buff, sizeof (buff), _("%08x: <unknown>"), sh_type);
6417 return buff;
6418 }
6419
6420 enum long_option_values
6421 {
6422 OPTION_DEBUG_DUMP = 512,
6423 OPTION_DYN_SYMS,
6424 OPTION_LTO_SYMS,
6425 OPTION_DWARF_DEPTH,
6426 OPTION_DWARF_START,
6427 OPTION_DWARF_CHECK,
6428 OPTION_CTF_DUMP,
6429 OPTION_CTF_PARENT,
6430 OPTION_CTF_SYMBOLS,
6431 OPTION_CTF_STRINGS,
6432 OPTION_SFRAME_DUMP,
6433 OPTION_WITH_SYMBOL_VERSIONS,
6434 OPTION_RECURSE_LIMIT,
6435 OPTION_NO_RECURSE_LIMIT,
6436 OPTION_NO_DEMANGLING,
6437 OPTION_NO_EXTRA_SYM_INFO,
6438 OPTION_SYM_BASE,
6439 OPTION_GOT_CONTENTS
6440 };
6441
6442 static struct option options[] =
6443 {
6444 /* Note - This table is alpha-sorted on the 'val'
6445 field in order to make adding new options easier. */
6446 {"arch-specific", no_argument, 0, 'A'},
6447 {"all", no_argument, 0, 'a'},
6448 {"demangle", optional_argument, 0, 'C'},
6449 {"archive-index", no_argument, 0, 'c'},
6450 {"use-dynamic", no_argument, 0, 'D'},
6451 {"dynamic", no_argument, 0, 'd'},
6452 {"headers", no_argument, 0, 'e'},
6453 {"section-groups", no_argument, 0, 'g'},
6454 {"help", no_argument, 0, 'H'},
6455 {"file-header", no_argument, 0, 'h'},
6456 {"histogram", no_argument, 0, 'I'},
6457 {"display-section", required_argument, 0, 'j'},
6458 {"lint", no_argument, 0, 'L'},
6459 {"enable-checks", no_argument, 0, 'L'},
6460 {"program-headers", no_argument, 0, 'l'},
6461 {"segments", no_argument, 0, 'l'},
6462 {"full-section-name",no_argument, 0, 'N'},
6463 {"notes", no_argument, 0, 'n'},
6464 {"process-links", no_argument, 0, 'P'},
6465 {"string-dump", required_argument, 0, 'p'},
6466 {"relocated-dump", required_argument, 0, 'R'},
6467 {"relocs", no_argument, 0, 'r'},
6468 {"section-headers", no_argument, 0, 'S'},
6469 {"sections", no_argument, 0, 'S'},
6470 {"symbols", no_argument, 0, 's'},
6471 {"syms", no_argument, 0, 's'},
6472 {"silent-truncation",no_argument, 0, 'T'},
6473 {"section-details", no_argument, 0, 't'},
6474 {"unicode", required_argument, NULL, 'U'},
6475 {"unwind", no_argument, 0, 'u'},
6476 {"version-info", no_argument, 0, 'V'},
6477 {"version", no_argument, 0, 'v'},
6478 {"wide", no_argument, 0, 'W'},
6479 {"extra-sym-info", no_argument, 0, 'X'},
6480 {"hex-dump", required_argument, 0, 'x'},
6481 {"decompress", no_argument, 0, 'z'},
6482
6483 {"no-demangle", no_argument, 0, OPTION_NO_DEMANGLING},
6484 {"no-extra-sym-info",no_argument, 0, OPTION_NO_EXTRA_SYM_INFO},
6485 {"recurse-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
6486 {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
6487 {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
6488 {"dyn-syms", no_argument, 0, OPTION_DYN_SYMS},
6489 {"lto-syms", no_argument, 0, OPTION_LTO_SYMS},
6490 {"debug-dump", optional_argument, 0, OPTION_DEBUG_DUMP},
6491 {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
6492 {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
6493 {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
6494 #ifdef ENABLE_LIBCTF
6495 {"ctf", required_argument, 0, OPTION_CTF_DUMP},
6496 {"ctf-symbols", required_argument, 0, OPTION_CTF_SYMBOLS},
6497 {"ctf-strings", required_argument, 0, OPTION_CTF_STRINGS},
6498 {"ctf-parent", required_argument, 0, OPTION_CTF_PARENT},
6499 #endif
6500 {"sframe", optional_argument, 0, OPTION_SFRAME_DUMP},
6501 {"sym-base", optional_argument, 0, OPTION_SYM_BASE},
6502 {"got-contents", no_argument, 0, OPTION_GOT_CONTENTS},
6503
6504 {0, no_argument, 0, 0}
6505 };
6506
6507 static void
6508 usage (FILE * stream)
6509 {
6510 fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
6511 fprintf (stream, _(" Display information about the contents of ELF format files\n"));
6512 fprintf (stream, _(" Options are:\n"));
6513 fprintf (stream, _("\
6514 -a --all Equivalent to: -h -l -S -s -r -d -V -A -I --got-contents\n"));
6515 fprintf (stream, _("\
6516 -h --file-header Display the ELF file header\n"));
6517 fprintf (stream, _("\
6518 -l --program-headers Display the program headers\n"));
6519 fprintf (stream, _("\
6520 --segments An alias for --program-headers\n"));
6521 fprintf (stream, _("\
6522 -S --section-headers Display the sections' header\n"));
6523 fprintf (stream, _("\
6524 --sections An alias for --section-headers\n"));
6525 fprintf (stream, _("\
6526 -g --section-groups Display the section groups\n"));
6527 fprintf (stream, _("\
6528 -t --section-details Display the section details\n"));
6529 fprintf (stream, _("\
6530 -e --headers Equivalent to: -h -l -S\n"));
6531 fprintf (stream, _("\
6532 -s --syms Display the symbol table\n"));
6533 fprintf (stream, _("\
6534 --symbols An alias for --syms\n"));
6535 fprintf (stream, _("\
6536 --dyn-syms Display the dynamic symbol table\n"));
6537 fprintf (stream, _("\
6538 --lto-syms Display LTO symbol tables\n"));
6539 fprintf (stream, _("\
6540 --sym-base=[0|8|10|16] \n\
6541 Force base for symbol sizes. The options are \n\
6542 mixed (the default), octal, decimal, hexadecimal.\n"));
6543 fprintf (stream, _("\
6544 -C --demangle[=STYLE] Decode mangled/processed symbol names\n"));
6545 display_demangler_styles (stream, _("\
6546 STYLE can be "));
6547 fprintf (stream, _("\
6548 --no-demangle Do not demangle low-level symbol names. (default)\n"));
6549 fprintf (stream, _("\
6550 --recurse-limit Enable a demangling recursion limit. (default)\n"));
6551 fprintf (stream, _("\
6552 --no-recurse-limit Disable a demangling recursion limit\n"));
6553 fprintf (stream, _("\
6554 -U[dlexhi] --unicode=[default|locale|escape|hex|highlight|invalid]\n\
6555 Display unicode characters as determined by the current locale\n\
6556 (default), escape sequences, \"<hex sequences>\", highlighted\n\
6557 escape sequences, or treat them as invalid and display as\n\
6558 \"{hex sequences}\"\n"));
6559 fprintf (stream, _("\
6560 -X --extra-sym-info Display extra information when showing symbols\n"));
6561 fprintf (stream, _("\
6562 --no-extra-sym-info Do not display extra information when showing symbols (default)\n"));
6563 fprintf (stream, _("\
6564 -n --notes Display the contents of note sections (if present)\n"));
6565 fprintf (stream, _("\
6566 -r --relocs Display the relocations (if present)\n"));
6567 fprintf (stream, _("\
6568 -u --unwind Display the unwind info (if present)\n"));
6569 fprintf (stream, _("\
6570 -d --dynamic Display the dynamic section (if present)\n"));
6571 fprintf (stream, _("\
6572 -V --version-info Display the version sections (if present)\n"));
6573 fprintf (stream, _("\
6574 -A --arch-specific Display architecture specific information (if any)\n"));
6575 fprintf (stream, _("\
6576 -c --archive-index Display the symbol/file index in an archive\n"));
6577 fprintf (stream, _("\
6578 -D --use-dynamic Use the dynamic section info when displaying symbols\n"));
6579 fprintf (stream, _("\
6580 -L --lint|--enable-checks\n\
6581 Display warning messages for possible problems\n"));
6582 fprintf (stream, _("\
6583 -x --hex-dump=<number|name>\n\
6584 Dump the contents of section <number|name> as bytes\n"));
6585 fprintf (stream, _("\
6586 -p --string-dump=<number|name>\n\
6587 Dump the contents of section <number|name> as strings\n"));
6588 fprintf (stream, _("\
6589 -R --relocated-dump=<number|name>\n\
6590 Dump the relocated contents of section <number|name>\n"));
6591 fprintf (stream, _("\
6592 -z --decompress Decompress section before dumping it\n"));
6593 fprintf (stream, _("\n\
6594 -j --display-section=<name|number>\n\
6595 Display the contents of the indicated section. Can be repeated\n"));
6596 fprintf (stream, _("\
6597 -w --debug-dump[a/=abbrev, A/=addr, r/=aranges, c/=cu_index, L/=decodedline,\n\
6598 f/=frames, F/=frames-interp, g/=gdb_index, i/=info, o/=loc,\n\
6599 m/=macro, p/=pubnames, t/=pubtypes, R/=Ranges, l/=rawline,\n\
6600 s/=str, O/=str-offsets, u/=trace_abbrev, T/=trace_aranges,\n\
6601 U/=trace_info]\n\
6602 Display the contents of DWARF debug sections\n"));
6603 fprintf (stream, _("\
6604 -wk --debug-dump=links Display the contents of sections that link to separate\n\
6605 debuginfo files\n"));
6606 fprintf (stream, _("\
6607 -P --process-links Display the contents of non-debug sections in separate\n\
6608 debuginfo files. (Implies -wK)\n"));
6609 #if DEFAULT_FOR_FOLLOW_LINKS
6610 fprintf (stream, _("\
6611 -wK --debug-dump=follow-links\n\
6612 Follow links to separate debug info files (default)\n"));
6613 fprintf (stream, _("\
6614 -wN --debug-dump=no-follow-links\n\
6615 Do not follow links to separate debug info files\n"));
6616 #else
6617 fprintf (stream, _("\
6618 -wK --debug-dump=follow-links\n\
6619 Follow links to separate debug info files\n"));
6620 fprintf (stream, _("\
6621 -wN --debug-dump=no-follow-links\n\
6622 Do not follow links to separate debug info files\n\
6623 (default)\n"));
6624 #endif
6625 #if HAVE_LIBDEBUGINFOD
6626 fprintf (stream, _("\
6627 -wD --debug-dump=use-debuginfod\n\
6628 When following links, also query debuginfod servers (default)\n"));
6629 fprintf (stream, _("\
6630 -wE --debug-dump=do-not-use-debuginfod\n\
6631 When following links, do not query debuginfod servers\n"));
6632 #endif
6633 fprintf (stream, _("\
6634 --dwarf-depth=N Do not display DIEs at depth N or greater\n"));
6635 fprintf (stream, _("\
6636 --dwarf-start=N Display DIEs starting at offset N\n"));
6637 #ifdef ENABLE_LIBCTF
6638 fprintf (stream, _("\
6639 --ctf=<number|name> Display CTF info from section <number|name>\n"));
6640 fprintf (stream, _("\
6641 --ctf-parent=<name> Use CTF archive member <name> as the CTF parent\n"));
6642 fprintf (stream, _("\
6643 --ctf-symbols=<number|name>\n\
6644 Use section <number|name> as the CTF external symtab\n"));
6645 fprintf (stream, _("\
6646 --ctf-strings=<number|name>\n\
6647 Use section <number|name> as the CTF external strtab\n"));
6648 #endif
6649 fprintf (stream, _("\
6650 --sframe[=NAME] Display SFrame info from section NAME, (default '.sframe')\n"));
6651
6652 #ifdef SUPPORT_DISASSEMBLY
6653 fprintf (stream, _("\
6654 -i --instruction-dump=<number|name>\n\
6655 Disassemble the contents of section <number|name>\n"));
6656 #endif
6657 fprintf (stream, _("\
6658 -I --histogram Display histogram of bucket list lengths\n"));
6659 fprintf (stream, _("\
6660 --got-contents Display GOT section contents\n"));
6661 fprintf (stream, _("\
6662 -W --wide Allow output width to exceed 80 characters\n"));
6663 fprintf (stream, _("\
6664 -T --silent-truncation If a symbol name is truncated, do not add [...] suffix\n"));
6665 fprintf (stream, _("\
6666 @<file> Read options from <file>\n"));
6667 fprintf (stream, _("\
6668 -H --help Display this information\n"));
6669 fprintf (stream, _("\
6670 -v --version Display the version number of readelf\n"));
6671
6672 if (REPORT_BUGS_TO[0] && stream == stdout)
6673 fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
6674
6675 exit (stream == stdout ? 0 : 1);
6676 }
6677
6678 /* Record the fact that the user wants the contents of section number
6679 SECTION to be displayed using the method(s) encoded as flags bits
6680 in TYPE. Note, TYPE can be zero if we are creating the array for
6681 the first time. */
6682
6683 static void
6684 request_dump_bynumber (struct dump_data *dumpdata,
6685 unsigned int section, dump_type type)
6686 {
6687 if (section >= dumpdata->num_dump_sects)
6688 {
6689 dump_type * new_dump_sects;
6690
6691 new_dump_sects = (dump_type *) calloc (section + 1,
6692 sizeof (* new_dump_sects));
6693
6694 if (new_dump_sects == NULL)
6695 error (_("Out of memory allocating dump request table.\n"));
6696 else
6697 {
6698 if (dumpdata->dump_sects)
6699 {
6700 /* Copy current flag settings. */
6701 memcpy (new_dump_sects, dumpdata->dump_sects,
6702 dumpdata->num_dump_sects * sizeof (* new_dump_sects));
6703
6704 free (dumpdata->dump_sects);
6705 }
6706
6707 dumpdata->dump_sects = new_dump_sects;
6708 dumpdata->num_dump_sects = section + 1;
6709 }
6710 }
6711
6712 if (dumpdata->dump_sects)
6713 dumpdata->dump_sects[section] |= type;
6714 }
6715
6716 /* Request a dump by section name. */
6717
6718 static void
6719 request_dump_byname (const char * section, dump_type type)
6720 {
6721 struct dump_list_entry * new_request;
6722
6723 new_request = (struct dump_list_entry *)
6724 malloc (sizeof (struct dump_list_entry));
6725 if (!new_request)
6726 error (_("Out of memory allocating dump request table.\n"));
6727
6728 new_request->name = strdup (section);
6729 if (!new_request->name)
6730 error (_("Out of memory allocating dump request table.\n"));
6731
6732 new_request->type = type;
6733
6734 new_request->next = dump_sects_byname;
6735 dump_sects_byname = new_request;
6736 }
6737
6738 static inline void
6739 request_dump (struct dump_data *dumpdata, dump_type type)
6740 {
6741 int section;
6742 char * cp;
6743
6744 do_dump = true;
6745 section = strtoul (optarg, & cp, 0);
6746
6747 if (! *cp && section >= 0)
6748 request_dump_bynumber (dumpdata, section, type);
6749 else
6750 request_dump_byname (optarg, type);
6751 }
6752
6753 static void
6754 parse_args (struct dump_data *dumpdata, int argc, char ** argv)
6755 {
6756 int c;
6757
6758 if (argc < 2)
6759 usage (stderr);
6760
6761 while ((c = getopt_long
6762 (argc, argv, "ACDHILNPR:STU:VWXacdeghi:j:lnp:rstuvw::x:z", options, NULL)) != EOF)
6763 {
6764 switch (c)
6765 {
6766 case 0:
6767 /* Long options. */
6768 break;
6769 case 'H':
6770 usage (stdout);
6771 break;
6772
6773 case 'a':
6774 do_syms = true;
6775 do_reloc = true;
6776 do_unwind = true;
6777 do_dynamic = true;
6778 do_header = true;
6779 do_sections = true;
6780 do_section_groups = true;
6781 do_segments = true;
6782 do_version = true;
6783 do_histogram = true;
6784 do_arch = true;
6785 do_notes = true;
6786 do_got_section_contents = true;
6787 break;
6788
6789 case 'g':
6790 do_section_groups = true;
6791 break;
6792 case 't':
6793 case 'N':
6794 do_sections = true;
6795 do_section_details = true;
6796 break;
6797 case 'e':
6798 do_header = true;
6799 do_sections = true;
6800 do_segments = true;
6801 break;
6802 case 'A':
6803 do_arch = true;
6804 break;
6805 case 'D':
6806 do_using_dynamic = true;
6807 break;
6808 case 'r':
6809 do_reloc = true;
6810 break;
6811 case 'u':
6812 do_unwind = true;
6813 break;
6814 case 'h':
6815 do_header = true;
6816 break;
6817 case 'l':
6818 do_segments = true;
6819 break;
6820 case 's':
6821 do_syms = true;
6822 break;
6823 case 'S':
6824 do_sections = true;
6825 break;
6826 case 'd':
6827 do_dynamic = true;
6828 break;
6829 case 'I':
6830 do_histogram = true;
6831 break;
6832 case 'n':
6833 do_notes = true;
6834 break;
6835 case 'c':
6836 do_archive_index = true;
6837 break;
6838 case 'L':
6839 do_checks = true;
6840 break;
6841 case 'P':
6842 process_links = true;
6843 do_follow_links = true;
6844 dump_any_debugging = true;
6845 break;
6846 case 'j':
6847 request_dump (dumpdata, AUTO_DUMP);
6848 break;
6849 case 'x':
6850 request_dump (dumpdata, HEX_DUMP);
6851 break;
6852 case 'p':
6853 request_dump (dumpdata, STRING_DUMP);
6854 break;
6855 case 'R':
6856 request_dump (dumpdata, RELOC_DUMP);
6857 break;
6858 case 'z':
6859 decompress_dumps = true;
6860 break;
6861 case 'w':
6862 if (optarg == NULL)
6863 {
6864 do_debugging = true;
6865 do_dump = true;
6866 dump_any_debugging = true;
6867 dwarf_select_sections_all ();
6868 }
6869 else
6870 {
6871 do_debugging = false;
6872 if (dwarf_select_sections_by_letters (optarg))
6873 {
6874 do_dump = true;
6875 dump_any_debugging = true;
6876 }
6877 }
6878 break;
6879 case OPTION_DEBUG_DUMP:
6880 if (optarg == NULL)
6881 {
6882 do_dump = true;
6883 do_debugging = true;
6884 dump_any_debugging = true;
6885 dwarf_select_sections_all ();
6886 }
6887 else if (strcmp (optarg, "sframe-internal-only") == 0)
6888 warn (_("Unrecognized debug option 'sframe-internal-only'\n"));
6889 else
6890 {
6891 do_debugging = false;
6892 if (dwarf_select_sections_by_names (optarg))
6893 {
6894 do_dump = true;
6895 dump_any_debugging = true;
6896 }
6897 }
6898 break;
6899 case OPTION_DWARF_DEPTH:
6900 {
6901 char *cp;
6902
6903 dwarf_cutoff_level = strtoul (optarg, & cp, 0);
6904 }
6905 break;
6906 case OPTION_DWARF_START:
6907 {
6908 char *cp;
6909
6910 dwarf_start_die = strtoul (optarg, & cp, 0);
6911 }
6912 break;
6913 case OPTION_DWARF_CHECK:
6914 dwarf_check = true;
6915 break;
6916 case OPTION_CTF_DUMP:
6917 do_ctf = true;
6918 request_dump (dumpdata, CTF_DUMP);
6919 break;
6920 case OPTION_CTF_SYMBOLS:
6921 free (dump_ctf_symtab_name);
6922 dump_ctf_symtab_name = strdup (optarg);
6923 break;
6924 case OPTION_CTF_STRINGS:
6925 free (dump_ctf_strtab_name);
6926 dump_ctf_strtab_name = strdup (optarg);
6927 break;
6928 case OPTION_CTF_PARENT:
6929 free (dump_ctf_parent_name);
6930 dump_ctf_parent_name = strdup (optarg);
6931 break;
6932 case OPTION_SFRAME_DUMP:
6933 do_sframe = true;
6934 /* Fix PR/32589 but keep the error messaging same ? */
6935 if (optarg != NULL && strcmp (optarg, "") == 0)
6936 {
6937 do_dump = true;
6938 error (_("Section name must be provided\n"));
6939 }
6940 /* Providing section name is optional. request_dump (), however,
6941 thrives on non NULL optarg. Handle it explicitly here. */
6942 else if (optarg != NULL)
6943 request_dump (dumpdata, SFRAME_DUMP);
6944 else
6945 {
6946 do_dump = true;
6947 const char *sframe_sec_name = strdup (".sframe");
6948 request_dump_byname (sframe_sec_name, SFRAME_DUMP);
6949 }
6950 break;
6951 case OPTION_DYN_SYMS:
6952 do_dyn_syms = true;
6953 break;
6954 case OPTION_LTO_SYMS:
6955 do_lto_syms = true;
6956 break;
6957 case 'X':
6958 extra_sym_info = true;
6959 break;
6960 case OPTION_NO_EXTRA_SYM_INFO:
6961 extra_sym_info = false;
6962 break;
6963
6964 #ifdef SUPPORT_DISASSEMBLY
6965 case 'i':
6966 request_dump (dumpdata, DISASS_DUMP);
6967 break;
6968 #endif
6969 case 'v':
6970 print_version (program_name);
6971 break;
6972 case 'V':
6973 do_version = true;
6974 break;
6975 case 'W':
6976 do_wide = true;
6977 break;
6978 case 'T':
6979 do_not_show_symbol_truncation = true;
6980 break;
6981 case 'C':
6982 do_demangle = true;
6983 if (optarg != NULL)
6984 {
6985 enum demangling_styles style;
6986
6987 style = cplus_demangle_name_to_style (optarg);
6988 if (style == unknown_demangling)
6989 error (_("unknown demangling style `%s'"), optarg);
6990
6991 cplus_demangle_set_style (style);
6992 }
6993 break;
6994 case OPTION_NO_DEMANGLING:
6995 do_demangle = false;
6996 break;
6997 case OPTION_RECURSE_LIMIT:
6998 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
6999 break;
7000 case OPTION_NO_RECURSE_LIMIT:
7001 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
7002 break;
7003 case OPTION_WITH_SYMBOL_VERSIONS:
7004 /* Ignored for backward compatibility. */
7005 break;
7006
7007 case 'U':
7008 if (optarg == NULL)
7009 error (_("Missing arg to -U/--unicode")); /* Can this happen ? */
7010 else if (streq (optarg, "default") || streq (optarg, "d"))
7011 unicode_display = unicode_default;
7012 else if (streq (optarg, "locale") || streq (optarg, "l"))
7013 unicode_display = unicode_locale;
7014 else if (streq (optarg, "escape") || streq (optarg, "e"))
7015 unicode_display = unicode_escape;
7016 else if (streq (optarg, "invalid") || streq (optarg, "i"))
7017 unicode_display = unicode_invalid;
7018 else if (streq (optarg, "hex") || streq (optarg, "x"))
7019 unicode_display = unicode_hex;
7020 else if (streq (optarg, "highlight") || streq (optarg, "h"))
7021 unicode_display = unicode_highlight;
7022 else
7023 error (_("invalid argument to -U/--unicode: %s"), optarg);
7024 break;
7025
7026 case OPTION_SYM_BASE:
7027 sym_base = 0;
7028 if (optarg != NULL)
7029 {
7030 sym_base = strtoul (optarg, NULL, 0);
7031 switch (sym_base)
7032 {
7033 case 0:
7034 case 8:
7035 case 10:
7036 case 16:
7037 break;
7038
7039 default:
7040 sym_base = 0;
7041 break;
7042 }
7043 }
7044 break;
7045
7046 case OPTION_GOT_CONTENTS:
7047 do_got_section_contents = true;
7048 do_dump = true;
7049 break;
7050
7051 default:
7052 /* xgettext:c-format */
7053 error (_("Invalid option '-%c'\n"), c);
7054 /* Fall through. */
7055 case '?':
7056 usage (stderr);
7057 }
7058 }
7059
7060 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
7061 && !do_segments && !do_header && !do_dump && !do_version
7062 && !do_histogram && !do_debugging && !do_arch && !do_notes
7063 && !do_section_groups && !do_archive_index
7064 && !do_dyn_syms && !do_lto_syms)
7065 {
7066 if (do_checks)
7067 {
7068 check_all = true;
7069 do_dynamic = do_syms = do_reloc = do_unwind = do_sections = true;
7070 do_segments = do_header = do_dump = do_version = true;
7071 do_histogram = do_debugging = do_arch = do_notes = true;
7072 do_section_groups = do_archive_index = do_dyn_syms = true;
7073 do_lto_syms = true;
7074 }
7075 else
7076 usage (stderr);
7077 }
7078 }
7079
7080 static const char *
7081 get_elf_class (unsigned int elf_class)
7082 {
7083 static char buff[32];
7084
7085 switch (elf_class)
7086 {
7087 case ELFCLASSNONE: return _("none");
7088 case ELFCLASS32: return "ELF32";
7089 case ELFCLASS64: return "ELF64";
7090 default:
7091 snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
7092 return buff;
7093 }
7094 }
7095
7096 static const char *
7097 get_data_encoding (unsigned int encoding)
7098 {
7099 static char buff[32];
7100
7101 switch (encoding)
7102 {
7103 case ELFDATANONE: return _("none");
7104 case ELFDATA2LSB: return _("2's complement, little endian");
7105 case ELFDATA2MSB: return _("2's complement, big endian");
7106 default:
7107 snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
7108 return buff;
7109 }
7110 }
7111
7112 static bool
7113 check_magic_number (Filedata * filedata, Elf_Internal_Ehdr * header)
7114 {
7115 if (header->e_ident[EI_MAG0] == ELFMAG0
7116 && header->e_ident[EI_MAG1] == ELFMAG1
7117 && header->e_ident[EI_MAG2] == ELFMAG2
7118 && header->e_ident[EI_MAG3] == ELFMAG3)
7119 return true;
7120
7121 /* Some compilers produce object files that are not in the ELF file format.
7122 As an aid to users of readelf, try to identify these cases and suggest
7123 alternative tools.
7124
7125 FIXME: It is not clear if all four bytes are used as constant magic
7126 valus by all compilers. It may be necessary to recode this function if
7127 different tools use different length sequences. */
7128
7129 static struct
7130 {
7131 unsigned char magic[4];
7132 const char * obj_message;
7133 const char * ar_message;
7134 }
7135 known_magic[] =
7136 {
7137 { { 'B', 'C', 0xc0, 0xde },
7138 N_("This is a LLVM bitcode file - try using llvm-bcanalyzer\n"),
7139 N_("This is a LLVM bitcode file - try extracting and then using llvm-bcanalyzer\n")
7140 },
7141 { { 'g', 'o', ' ', 'o' },
7142 N_("This is a GO binary file - try using 'go tool objdump' or 'go tool nm'\n"),
7143 NULL
7144 }
7145 };
7146 int i;
7147
7148 for (i = ARRAY_SIZE (known_magic); i--;)
7149 {
7150 if (header->e_ident[EI_MAG0] == known_magic[i].magic[0]
7151 && header->e_ident[EI_MAG1] == known_magic[i].magic[1]
7152 && header->e_ident[EI_MAG2] == known_magic[i].magic[2]
7153 && header->e_ident[EI_MAG3] == known_magic[i].magic[3])
7154 {
7155 /* Some compiler's analyzer tools do not handle archives,
7156 so we provide two different kinds of error message. */
7157 if (filedata->archive_file_size > 0
7158 && known_magic[i].ar_message != NULL)
7159 error ("%s", known_magic[i].ar_message);
7160 else
7161 error ("%s", known_magic[i].obj_message);
7162 return false;
7163 }
7164 }
7165
7166 error (_("Not an ELF file - it has the wrong magic bytes at the start\n"));
7167 return false;
7168 }
7169
7170 /* Decode the data held in 'filedata->file_header'. */
7171
7172 static bool
7173 process_file_header (Filedata * filedata)
7174 {
7175 Elf_Internal_Ehdr * header = & filedata->file_header;
7176
7177 if (! check_magic_number (filedata, header))
7178 return false;
7179
7180 if (! filedata->is_separate)
7181 init_dwarf_by_elf_machine_code (header->e_machine);
7182
7183 if (do_header)
7184 {
7185 unsigned i;
7186
7187 if (filedata->is_separate)
7188 printf (_("ELF Header in linked file '%s':\n"),
7189 printable_string (filedata->file_name, 0));
7190 else
7191 printf (_("ELF Header:\n"));
7192 printf (_(" Magic: "));
7193 for (i = 0; i < EI_NIDENT; i++)
7194 printf ("%2.2x ", header->e_ident[i]);
7195 printf ("\n");
7196 printf (_(" Class: %s\n"),
7197 get_elf_class (header->e_ident[EI_CLASS]));
7198 printf (_(" Data: %s\n"),
7199 get_data_encoding (header->e_ident[EI_DATA]));
7200 printf (_(" Version: %d%s\n"),
7201 header->e_ident[EI_VERSION],
7202 (header->e_ident[EI_VERSION] == EV_CURRENT
7203 ? _(" (current)")
7204 : (header->e_ident[EI_VERSION] != EV_NONE
7205 ? _(" <unknown>")
7206 : "")));
7207 printf (_(" OS/ABI: %s\n"),
7208 get_osabi_name (filedata, header->e_ident[EI_OSABI]));
7209 printf (_(" ABI Version: %d\n"),
7210 header->e_ident[EI_ABIVERSION]);
7211 printf (_(" Type: %s\n"),
7212 get_file_type (filedata));
7213 printf (_(" Machine: %s\n"),
7214 get_machine_name (header->e_machine));
7215 printf (_(" Version: 0x%lx\n"),
7216 header->e_version);
7217
7218 printf (_(" Entry point address: "));
7219 print_vma (header->e_entry, PREFIX_HEX);
7220 printf (_("\n Start of program headers: "));
7221 print_vma (header->e_phoff, DEC);
7222 printf (_(" (bytes into file)\n Start of section headers: "));
7223 print_vma (header->e_shoff, DEC);
7224 printf (_(" (bytes into file)\n"));
7225
7226 printf (_(" Flags: 0x%lx%s\n"),
7227 header->e_flags,
7228 get_machine_flags (filedata, header->e_flags, header->e_machine));
7229 printf (_(" Size of this header: %u (bytes)\n"),
7230 header->e_ehsize);
7231 printf (_(" Size of program headers: %u (bytes)\n"),
7232 header->e_phentsize);
7233 printf (_(" Number of program headers: %u"),
7234 header->e_phnum);
7235 if (filedata->section_headers != NULL
7236 && header->e_phnum == PN_XNUM
7237 && filedata->section_headers[0].sh_info != 0)
7238 printf (" (%u)", filedata->section_headers[0].sh_info);
7239 putc ('\n', stdout);
7240 printf (_(" Size of section headers: %u (bytes)\n"),
7241 header->e_shentsize);
7242 printf (_(" Number of section headers: %u"),
7243 header->e_shnum);
7244 if (filedata->section_headers != NULL && header->e_shnum == SHN_UNDEF)
7245 {
7246 header->e_shnum = filedata->section_headers[0].sh_size;
7247 printf (" (%u)", header->e_shnum);
7248 }
7249 putc ('\n', stdout);
7250 printf (_(" Section header string table index: %u"),
7251 header->e_shstrndx);
7252 if (filedata->section_headers != NULL
7253 && header->e_shstrndx == (SHN_XINDEX & 0xffff))
7254 {
7255 header->e_shstrndx = filedata->section_headers[0].sh_link;
7256 printf (" (%u)", header->e_shstrndx);
7257 }
7258 if (header->e_shstrndx != SHN_UNDEF
7259 && header->e_shstrndx >= header->e_shnum)
7260 {
7261 header->e_shstrndx = SHN_UNDEF;
7262 printf (_(" <corrupt: out of range>"));
7263 }
7264 putc ('\n', stdout);
7265 }
7266
7267 if (filedata->section_headers != NULL)
7268 {
7269 if (header->e_phnum == PN_XNUM
7270 && filedata->section_headers[0].sh_info != 0)
7271 {
7272 /* Throw away any cached read of PN_XNUM headers. */
7273 free (filedata->program_headers);
7274 filedata->program_headers = NULL;
7275 header->e_phnum = filedata->section_headers[0].sh_info;
7276 }
7277 if (header->e_shnum == SHN_UNDEF)
7278 header->e_shnum = filedata->section_headers[0].sh_size;
7279 if (header->e_shstrndx == (SHN_XINDEX & 0xffff))
7280 header->e_shstrndx = filedata->section_headers[0].sh_link;
7281 if (header->e_shstrndx >= header->e_shnum)
7282 header->e_shstrndx = SHN_UNDEF;
7283 }
7284
7285 return true;
7286 }
7287
7288 /* Read in the program headers from FILEDATA and store them in PHEADERS.
7289 Returns TRUE upon success, FALSE otherwise. Loads 32-bit headers. */
7290
7291 static bool
7292 get_32bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
7293 {
7294 Elf32_External_Phdr * phdrs;
7295 Elf32_External_Phdr * external;
7296 Elf_Internal_Phdr * internal;
7297 unsigned int i;
7298 unsigned int size = filedata->file_header.e_phentsize;
7299 unsigned int num = filedata->file_header.e_phnum;
7300
7301 /* PR binutils/17531: Cope with unexpected section header sizes. */
7302 if (size == 0 || num == 0)
7303 return false;
7304 if (size < sizeof * phdrs)
7305 {
7306 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
7307 return false;
7308 }
7309 if (size > sizeof * phdrs)
7310 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
7311
7312 phdrs = (Elf32_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
7313 size, num, _("program headers"));
7314 if (phdrs == NULL)
7315 return false;
7316
7317 for (i = 0, internal = pheaders, external = phdrs;
7318 i < filedata->file_header.e_phnum;
7319 i++, internal++, external++)
7320 {
7321 internal->p_type = BYTE_GET (external->p_type);
7322 internal->p_offset = BYTE_GET (external->p_offset);
7323 internal->p_vaddr = BYTE_GET (external->p_vaddr);
7324 internal->p_paddr = BYTE_GET (external->p_paddr);
7325 internal->p_filesz = BYTE_GET (external->p_filesz);
7326 internal->p_memsz = BYTE_GET (external->p_memsz);
7327 internal->p_flags = BYTE_GET (external->p_flags);
7328 internal->p_align = BYTE_GET (external->p_align);
7329 }
7330
7331 free (phdrs);
7332 return true;
7333 }
7334
7335 /* Read in the program headers from FILEDATA and store them in PHEADERS.
7336 Returns TRUE upon success, FALSE otherwise. Loads 64-bit headers. */
7337
7338 static bool
7339 get_64bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
7340 {
7341 Elf64_External_Phdr * phdrs;
7342 Elf64_External_Phdr * external;
7343 Elf_Internal_Phdr * internal;
7344 unsigned int i;
7345 unsigned int size = filedata->file_header.e_phentsize;
7346 unsigned int num = filedata->file_header.e_phnum;
7347
7348 /* PR binutils/17531: Cope with unexpected section header sizes. */
7349 if (size == 0 || num == 0)
7350 return false;
7351 if (size < sizeof * phdrs)
7352 {
7353 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
7354 return false;
7355 }
7356 if (size > sizeof * phdrs)
7357 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
7358
7359 phdrs = (Elf64_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
7360 size, num, _("program headers"));
7361 if (!phdrs)
7362 return false;
7363
7364 for (i = 0, internal = pheaders, external = phdrs;
7365 i < filedata->file_header.e_phnum;
7366 i++, internal++, external++)
7367 {
7368 internal->p_type = BYTE_GET (external->p_type);
7369 internal->p_flags = BYTE_GET (external->p_flags);
7370 internal->p_offset = BYTE_GET (external->p_offset);
7371 internal->p_vaddr = BYTE_GET (external->p_vaddr);
7372 internal->p_paddr = BYTE_GET (external->p_paddr);
7373 internal->p_filesz = BYTE_GET (external->p_filesz);
7374 internal->p_memsz = BYTE_GET (external->p_memsz);
7375 internal->p_align = BYTE_GET (external->p_align);
7376 }
7377
7378 free (phdrs);
7379 return true;
7380 }
7381
7382 /* Returns TRUE if the program headers were read into `program_headers'. */
7383
7384 static bool
7385 get_program_headers (Filedata * filedata)
7386 {
7387 Elf_Internal_Phdr * phdrs;
7388
7389 /* Check cache of prior read. */
7390 if (filedata->program_headers != NULL)
7391 return true;
7392
7393 /* Be kind to memory checkers by looking for
7394 e_phnum values which we know must be invalid. */
7395 if (filedata->file_header.e_phnum
7396 * (is_32bit_elf ? sizeof (Elf32_External_Phdr) : sizeof (Elf64_External_Phdr))
7397 >= filedata->file_size)
7398 {
7399 error (_("Too many program headers - %#x - the file is not that big\n"),
7400 filedata->file_header.e_phnum);
7401 return false;
7402 }
7403
7404 phdrs = (Elf_Internal_Phdr *) cmalloc (filedata->file_header.e_phnum,
7405 sizeof (Elf_Internal_Phdr));
7406 if (phdrs == NULL)
7407 {
7408 error (_("Out of memory reading %u program headers\n"),
7409 filedata->file_header.e_phnum);
7410 return false;
7411 }
7412
7413 if (is_32bit_elf
7414 ? get_32bit_program_headers (filedata, phdrs)
7415 : get_64bit_program_headers (filedata, phdrs))
7416 {
7417 filedata->program_headers = phdrs;
7418 return true;
7419 }
7420
7421 free (phdrs);
7422 return false;
7423 }
7424
7425 /* Print program header info and locate dynamic section. */
7426
7427 static void
7428 process_program_headers (Filedata * filedata)
7429 {
7430 Elf_Internal_Phdr * segment;
7431 unsigned int i;
7432 Elf_Internal_Phdr * previous_load = NULL;
7433
7434 if (filedata->file_header.e_phnum == 0)
7435 {
7436 /* PR binutils/12467. */
7437 if (filedata->file_header.e_phoff != 0)
7438 warn (_("possibly corrupt ELF header - it has a non-zero program"
7439 " header offset, but no program headers\n"));
7440 else if (do_segments)
7441 {
7442 if (filedata->is_separate)
7443 printf (_("\nThere are no program headers in linked file '%s'.\n"),
7444 printable_string (filedata->file_name, 0));
7445 else
7446 printf (_("\nThere are no program headers in this file.\n"));
7447 }
7448 goto no_headers;
7449 }
7450
7451 if (do_segments && !do_header)
7452 {
7453 if (filedata->is_separate)
7454 printf ("\nIn linked file '%s' the ELF file type is %s\n",
7455 printable_string (filedata->file_name, 0),
7456 get_file_type (filedata));
7457 else
7458 printf (_("\nElf file type is %s\n"), get_file_type (filedata));
7459 printf (_("Entry point 0x%" PRIx64 "\n"),
7460 filedata->file_header.e_entry);
7461 printf (ngettext ("There is %d program header,"
7462 " starting at offset %" PRIu64 "\n",
7463 "There are %d program headers,"
7464 " starting at offset %" PRIu64 "\n",
7465 filedata->file_header.e_phnum),
7466 filedata->file_header.e_phnum,
7467 filedata->file_header.e_phoff);
7468 }
7469
7470 if (! get_program_headers (filedata))
7471 goto no_headers;
7472
7473 if (do_segments)
7474 {
7475 if (filedata->file_header.e_phnum > 1)
7476 printf (_("\nProgram Headers:\n"));
7477 else
7478 printf (_("\nProgram Headers:\n"));
7479
7480 if (is_32bit_elf)
7481 printf
7482 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
7483 else if (do_wide)
7484 printf
7485 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
7486 else
7487 {
7488 printf
7489 (_(" Type Offset VirtAddr PhysAddr\n"));
7490 printf
7491 (_(" FileSiz MemSiz Flags Align\n"));
7492 }
7493 }
7494
7495 uint64_t dynamic_addr = 0;
7496 uint64_t dynamic_size = 0;
7497 for (i = 0, segment = filedata->program_headers;
7498 i < filedata->file_header.e_phnum;
7499 i++, segment++)
7500 {
7501 if (do_segments)
7502 {
7503 printf (" %-14.14s ", get_segment_type (filedata, segment->p_type));
7504
7505 if (is_32bit_elf)
7506 {
7507 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
7508 printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
7509 printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
7510 printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
7511 printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
7512 printf ("%c%c%c ",
7513 (segment->p_flags & PF_R ? 'R' : ' '),
7514 (segment->p_flags & PF_W ? 'W' : ' '),
7515 (segment->p_flags & PF_X ? 'E' : ' '));
7516 printf ("%#lx", (unsigned long) segment->p_align);
7517 }
7518 else if (do_wide)
7519 {
7520 if ((unsigned long) segment->p_offset == segment->p_offset)
7521 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
7522 else
7523 {
7524 print_vma (segment->p_offset, FULL_HEX);
7525 putchar (' ');
7526 }
7527
7528 print_vma (segment->p_vaddr, FULL_HEX);
7529 putchar (' ');
7530 print_vma (segment->p_paddr, FULL_HEX);
7531 putchar (' ');
7532
7533 if ((unsigned long) segment->p_filesz == segment->p_filesz)
7534 printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
7535 else
7536 {
7537 print_vma (segment->p_filesz, FULL_HEX);
7538 putchar (' ');
7539 }
7540
7541 if ((unsigned long) segment->p_memsz == segment->p_memsz)
7542 printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
7543 else
7544 {
7545 print_vma (segment->p_memsz, FULL_HEX);
7546 }
7547
7548 printf (" %c%c%c ",
7549 (segment->p_flags & PF_R ? 'R' : ' '),
7550 (segment->p_flags & PF_W ? 'W' : ' '),
7551 (segment->p_flags & PF_X ? 'E' : ' '));
7552
7553 if ((unsigned long) segment->p_align == segment->p_align)
7554 printf ("%#lx", (unsigned long) segment->p_align);
7555 else
7556 {
7557 print_vma (segment->p_align, PREFIX_HEX);
7558 }
7559 }
7560 else
7561 {
7562 print_vma (segment->p_offset, FULL_HEX);
7563 putchar (' ');
7564 print_vma (segment->p_vaddr, FULL_HEX);
7565 putchar (' ');
7566 print_vma (segment->p_paddr, FULL_HEX);
7567 printf ("\n ");
7568 print_vma (segment->p_filesz, FULL_HEX);
7569 putchar (' ');
7570 print_vma (segment->p_memsz, FULL_HEX);
7571 printf (" %c%c%c ",
7572 (segment->p_flags & PF_R ? 'R' : ' '),
7573 (segment->p_flags & PF_W ? 'W' : ' '),
7574 (segment->p_flags & PF_X ? 'E' : ' '));
7575 print_vma (segment->p_align, PREFIX_HEX);
7576 }
7577
7578 putc ('\n', stdout);
7579 }
7580
7581 switch (segment->p_type)
7582 {
7583 case PT_LOAD:
7584 #if 0 /* Do not warn about out of order PT_LOAD segments. Although officially
7585 required by the ELF standard, several programs, including the Linux
7586 kernel, make use of non-ordered segments. */
7587 if (previous_load
7588 && previous_load->p_vaddr > segment->p_vaddr)
7589 error (_("LOAD segments must be sorted in order of increasing VirtAddr\n"));
7590 #endif
7591 if (segment->p_memsz < segment->p_filesz)
7592 error (_("the segment's file size is larger than its memory size\n"));
7593 previous_load = segment;
7594 break;
7595
7596 case PT_PHDR:
7597 /* PR 20815 - Verify that the program header is loaded into memory. */
7598 if (i > 0 && previous_load != NULL)
7599 error (_("the PHDR segment must occur before any LOAD segment\n"));
7600 if (filedata->file_header.e_machine != EM_PARISC)
7601 {
7602 unsigned int j;
7603
7604 for (j = 1; j < filedata->file_header.e_phnum; j++)
7605 {
7606 Elf_Internal_Phdr *load = filedata->program_headers + j;
7607 if (load->p_type == PT_LOAD
7608 && load->p_offset <= segment->p_offset
7609 && (load->p_offset + load->p_filesz
7610 >= segment->p_offset + segment->p_filesz)
7611 && load->p_vaddr <= segment->p_vaddr
7612 && (load->p_vaddr + load->p_filesz
7613 >= segment->p_vaddr + segment->p_filesz))
7614 break;
7615 }
7616 if (j == filedata->file_header.e_phnum)
7617 error (_("the PHDR segment is not covered by a LOAD segment\n"));
7618 }
7619 break;
7620
7621 case PT_DYNAMIC:
7622 if (dynamic_addr)
7623 error (_("more than one dynamic segment\n"));
7624
7625 /* By default, assume that the .dynamic section is the first
7626 section in the DYNAMIC segment. */
7627 dynamic_addr = segment->p_offset;
7628 dynamic_size = segment->p_filesz;
7629
7630 /* Try to locate the .dynamic section. If there is
7631 a section header table, we can easily locate it. */
7632 if (filedata->section_headers != NULL)
7633 {
7634 Elf_Internal_Shdr * sec;
7635
7636 sec = find_section (filedata, ".dynamic");
7637 if (sec == NULL || sec->sh_size == 0)
7638 {
7639 /* A corresponding .dynamic section is expected, but on
7640 IA-64/OpenVMS it is OK for it to be missing. */
7641 if (!is_ia64_vms (filedata))
7642 error (_("no .dynamic section in the dynamic segment\n"));
7643 break;
7644 }
7645
7646 if (sec->sh_type == SHT_NOBITS)
7647 {
7648 dynamic_addr = 0;
7649 dynamic_size = 0;
7650 break;
7651 }
7652
7653 dynamic_addr = sec->sh_offset;
7654 dynamic_size = sec->sh_size;
7655
7656 /* The PT_DYNAMIC segment, which is used by the run-time
7657 loader, should exactly match the .dynamic section. */
7658 if (do_checks
7659 && (dynamic_addr != segment->p_offset
7660 || dynamic_size != segment->p_filesz))
7661 warn (_("\
7662 the .dynamic section is not the same as the dynamic segment\n"));
7663 }
7664
7665 /* PR binutils/17512: Avoid corrupt dynamic section info in the
7666 segment. Check this after matching against the section headers
7667 so we don't warn on debuginfo file (which have NOBITS .dynamic
7668 sections). */
7669 if (dynamic_addr > filedata->file_size
7670 || (dynamic_size > filedata->file_size - dynamic_addr))
7671 {
7672 error (_("the dynamic segment offset + size exceeds the size of the file\n"));
7673 dynamic_addr = 0;
7674 dynamic_size = 0;
7675 }
7676 break;
7677
7678 case PT_INTERP:
7679 if (segment->p_offset >= filedata->file_size
7680 || segment->p_filesz > filedata->file_size - segment->p_offset
7681 || segment->p_filesz - 1 >= (size_t) -2
7682 || fseek64 (filedata->handle,
7683 filedata->archive_file_offset + segment->p_offset,
7684 SEEK_SET))
7685 error (_("Unable to find program interpreter name\n"));
7686 else
7687 {
7688 size_t len = segment->p_filesz;
7689 free (filedata->program_interpreter);
7690 filedata->program_interpreter = xmalloc (len + 1);
7691 len = fread (filedata->program_interpreter, 1, len,
7692 filedata->handle);
7693 filedata->program_interpreter[len] = 0;
7694
7695 if (do_segments)
7696 printf (_(" [Requesting program interpreter: %s]\n"),
7697 printable_string (filedata->program_interpreter, 0));
7698 }
7699 break;
7700 }
7701 }
7702
7703 if (do_segments
7704 && filedata->section_headers != NULL
7705 && filedata->string_table != NULL)
7706 {
7707 printf (_("\n Section to Segment mapping:\n"));
7708 printf (_(" Segment Sections...\n"));
7709
7710 for (i = 0; i < filedata->file_header.e_phnum; i++)
7711 {
7712 unsigned int j;
7713 Elf_Internal_Shdr * section;
7714
7715 segment = filedata->program_headers + i;
7716 section = filedata->section_headers + 1;
7717
7718 printf (" %2.2d ", i);
7719
7720 for (j = 1; j < filedata->file_header.e_shnum; j++, section++)
7721 {
7722 if (!ELF_TBSS_SPECIAL (section, segment)
7723 && ELF_SECTION_IN_SEGMENT_STRICT (section, segment))
7724 printf ("%s ", printable_section_name (filedata, section));
7725 }
7726
7727 putc ('\n',stdout);
7728 }
7729 }
7730
7731 filedata->dynamic_addr = dynamic_addr;
7732 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
7733 return;
7734
7735 no_headers:
7736 filedata->dynamic_addr = 0;
7737 filedata->dynamic_size = 1;
7738 }
7739
7740
7741 /* Find the file offset corresponding to VMA by using the program headers. */
7742
7743 static int64_t
7744 offset_from_vma (Filedata * filedata, uint64_t vma, uint64_t size)
7745 {
7746 Elf_Internal_Phdr * seg;
7747
7748 if (! get_program_headers (filedata))
7749 {
7750 warn (_("Cannot interpret virtual addresses without program headers.\n"));
7751 return (long) vma;
7752 }
7753
7754 for (seg = filedata->program_headers;
7755 seg < filedata->program_headers + filedata->file_header.e_phnum;
7756 ++seg)
7757 {
7758 if (seg->p_type != PT_LOAD)
7759 continue;
7760
7761 if (vma >= (seg->p_vaddr & -seg->p_align)
7762 && vma + size <= seg->p_vaddr + seg->p_filesz)
7763 return vma - seg->p_vaddr + seg->p_offset;
7764 }
7765
7766 warn (_("Virtual address %#" PRIx64
7767 " not located in any PT_LOAD segment.\n"), vma);
7768 return vma;
7769 }
7770
7771
7772 /* Allocate memory and load the sections headers into FILEDATA->filedata->section_headers.
7773 If PROBE is true, this is just a probe and we do not generate any error
7774 messages if the load fails. */
7775
7776 static bool
7777 get_32bit_section_headers (Filedata * filedata, bool probe)
7778 {
7779 Elf32_External_Shdr * shdrs;
7780 Elf_Internal_Shdr * internal;
7781 unsigned int i;
7782 unsigned int size = filedata->file_header.e_shentsize;
7783 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
7784
7785 /* PR binutils/17531: Cope with unexpected section header sizes. */
7786 if (size == 0 || num == 0)
7787 return false;
7788
7789 /* The section header cannot be at the start of the file - that is
7790 where the ELF file header is located. A file with absolutely no
7791 sections in it will use a shoff of 0. */
7792 if (filedata->file_header.e_shoff == 0)
7793 return false;
7794
7795 if (size < sizeof * shdrs)
7796 {
7797 if (! probe)
7798 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
7799 return false;
7800 }
7801 if (!probe && size > sizeof * shdrs)
7802 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
7803
7804 shdrs = (Elf32_External_Shdr *) get_data (NULL, filedata, filedata->file_header.e_shoff,
7805 size, num,
7806 probe ? NULL : _("section headers"));
7807 if (shdrs == NULL)
7808 return false;
7809
7810 filedata->section_headers = (Elf_Internal_Shdr *)
7811 cmalloc (num, sizeof (Elf_Internal_Shdr));
7812 if (filedata->section_headers == NULL)
7813 {
7814 if (!probe)
7815 error (_("Out of memory reading %u section headers\n"), num);
7816 free (shdrs);
7817 return false;
7818 }
7819
7820 for (i = 0, internal = filedata->section_headers;
7821 i < num;
7822 i++, internal++)
7823 {
7824 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
7825 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
7826 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
7827 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
7828 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
7829 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
7830 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
7831 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
7832 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
7833 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
7834 if (!probe && internal->sh_link > num)
7835 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
7836 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
7837 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
7838 }
7839
7840 free (shdrs);
7841 return true;
7842 }
7843
7844 /* Like get_32bit_section_headers, except that it fetches 64-bit headers. */
7845
7846 static bool
7847 get_64bit_section_headers (Filedata * filedata, bool probe)
7848 {
7849 Elf64_External_Shdr * shdrs;
7850 Elf_Internal_Shdr * internal;
7851 unsigned int i;
7852 unsigned int size = filedata->file_header.e_shentsize;
7853 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
7854
7855 /* PR binutils/17531: Cope with unexpected section header sizes. */
7856 if (size == 0 || num == 0)
7857 return false;
7858
7859 /* The section header cannot be at the start of the file - that is
7860 where the ELF file header is located. A file with absolutely no
7861 sections in it will use a shoff of 0. */
7862 if (filedata->file_header.e_shoff == 0)
7863 return false;
7864
7865 if (size < sizeof * shdrs)
7866 {
7867 if (! probe)
7868 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
7869 return false;
7870 }
7871
7872 if (! probe && size > sizeof * shdrs)
7873 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
7874
7875 shdrs = (Elf64_External_Shdr *) get_data (NULL, filedata,
7876 filedata->file_header.e_shoff,
7877 size, num,
7878 probe ? NULL : _("section headers"));
7879 if (shdrs == NULL)
7880 return false;
7881
7882 filedata->section_headers = (Elf_Internal_Shdr *)
7883 cmalloc (num, sizeof (Elf_Internal_Shdr));
7884 if (filedata->section_headers == NULL)
7885 {
7886 if (! probe)
7887 error (_("Out of memory reading %u section headers\n"), num);
7888 free (shdrs);
7889 return false;
7890 }
7891
7892 for (i = 0, internal = filedata->section_headers;
7893 i < num;
7894 i++, internal++)
7895 {
7896 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
7897 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
7898 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
7899 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
7900 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
7901 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
7902 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
7903 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
7904 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
7905 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
7906 if (!probe && internal->sh_link > num)
7907 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
7908 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
7909 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
7910 }
7911
7912 free (shdrs);
7913 return true;
7914 }
7915
7916 static bool
7917 get_section_headers (Filedata *filedata, bool probe)
7918 {
7919 if (filedata->section_headers != NULL)
7920 return true;
7921
7922 if (is_32bit_elf)
7923 return get_32bit_section_headers (filedata, probe);
7924 else
7925 return get_64bit_section_headers (filedata, probe);
7926 }
7927
7928 static Elf_Internal_Sym *
7929 get_32bit_elf_symbols (Filedata *filedata,
7930 Elf_Internal_Shdr *section,
7931 uint64_t *num_syms_return)
7932 {
7933 uint64_t number = 0;
7934 Elf32_External_Sym * esyms = NULL;
7935 Elf_External_Sym_Shndx * shndx = NULL;
7936 Elf_Internal_Sym * isyms = NULL;
7937 Elf_Internal_Sym * psym;
7938 unsigned int j;
7939 elf_section_list * entry;
7940
7941 if (section->sh_size == 0)
7942 {
7943 if (num_syms_return != NULL)
7944 * num_syms_return = 0;
7945 return NULL;
7946 }
7947
7948 /* Run some sanity checks first. */
7949 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
7950 {
7951 error (_("Section %s has an invalid sh_entsize of %#" PRIx64 "\n"),
7952 printable_section_name (filedata, section),
7953 section->sh_entsize);
7954 goto exit_point;
7955 }
7956
7957 if (section->sh_size > filedata->file_size)
7958 {
7959 error (_("Section %s has an invalid sh_size of %#" PRIx64 "\n"),
7960 printable_section_name (filedata, section),
7961 section->sh_size);
7962 goto exit_point;
7963 }
7964
7965 number = section->sh_size / section->sh_entsize;
7966
7967 if (number * sizeof (Elf32_External_Sym) > section->sh_size + 1)
7968 {
7969 error (_("Size (%#" PRIx64 ") of section %s "
7970 "is not a multiple of its sh_entsize (%#" PRIx64 ")\n"),
7971 section->sh_size,
7972 printable_section_name (filedata, section),
7973 section->sh_entsize);
7974 goto exit_point;
7975 }
7976
7977 esyms = (Elf32_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
7978 section->sh_size, _("symbols"));
7979 if (esyms == NULL)
7980 goto exit_point;
7981
7982 shndx = NULL;
7983 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
7984 {
7985 if (entry->hdr->sh_link != (size_t) (section - filedata->section_headers))
7986 continue;
7987
7988 if (shndx != NULL)
7989 {
7990 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
7991 free (shndx);
7992 }
7993
7994 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
7995 entry->hdr->sh_offset,
7996 1, entry->hdr->sh_size,
7997 _("symbol table section indices"));
7998 if (shndx == NULL)
7999 goto exit_point;
8000
8001 /* PR17531: file: heap-buffer-overflow */
8002 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
8003 {
8004 error (_("Index section %s has an sh_size of %#" PRIx64 " - expected %#" PRIx64 "\n"),
8005 printable_section_name (filedata, entry->hdr),
8006 entry->hdr->sh_size,
8007 section->sh_size);
8008 goto exit_point;
8009 }
8010 }
8011
8012 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
8013
8014 if (isyms == NULL)
8015 {
8016 error (_("Out of memory reading %" PRIu64 " symbols\n"), number);
8017 goto exit_point;
8018 }
8019
8020 for (j = 0, psym = isyms; j < number; j++, psym++)
8021 {
8022 psym->st_name = BYTE_GET (esyms[j].st_name);
8023 psym->st_value = BYTE_GET (esyms[j].st_value);
8024 psym->st_size = BYTE_GET (esyms[j].st_size);
8025 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
8026 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
8027 psym->st_shndx
8028 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
8029 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
8030 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
8031 psym->st_info = BYTE_GET (esyms[j].st_info);
8032 psym->st_other = BYTE_GET (esyms[j].st_other);
8033 }
8034
8035 exit_point:
8036 free (shndx);
8037 free (esyms);
8038
8039 if (num_syms_return != NULL)
8040 * num_syms_return = isyms == NULL ? 0 : number;
8041
8042 return isyms;
8043 }
8044
8045 static Elf_Internal_Sym *
8046 get_64bit_elf_symbols (Filedata *filedata,
8047 Elf_Internal_Shdr *section,
8048 uint64_t *num_syms_return)
8049 {
8050 uint64_t number = 0;
8051 Elf64_External_Sym * esyms = NULL;
8052 Elf_External_Sym_Shndx * shndx = NULL;
8053 Elf_Internal_Sym * isyms = NULL;
8054 Elf_Internal_Sym * psym;
8055 unsigned int j;
8056 elf_section_list * entry;
8057
8058 if (section->sh_size == 0)
8059 {
8060 if (num_syms_return != NULL)
8061 * num_syms_return = 0;
8062 return NULL;
8063 }
8064
8065 /* Run some sanity checks first. */
8066 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
8067 {
8068 error (_("Section %s has an invalid sh_entsize of %#" PRIx64 "\n"),
8069 printable_section_name (filedata, section),
8070 section->sh_entsize);
8071 goto exit_point;
8072 }
8073
8074 if (section->sh_size > filedata->file_size)
8075 {
8076 error (_("Section %s has an invalid sh_size of %#" PRIx64 "\n"),
8077 printable_section_name (filedata, section),
8078 section->sh_size);
8079 goto exit_point;
8080 }
8081
8082 number = section->sh_size / section->sh_entsize;
8083
8084 if (number * sizeof (Elf64_External_Sym) > section->sh_size + 1)
8085 {
8086 error (_("Size (%#" PRIx64 ") of section %s "
8087 "is not a multiple of its sh_entsize (%#" PRIx64 ")\n"),
8088 section->sh_size,
8089 printable_section_name (filedata, section),
8090 section->sh_entsize);
8091 goto exit_point;
8092 }
8093
8094 esyms = (Elf64_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
8095 section->sh_size, _("symbols"));
8096 if (!esyms)
8097 goto exit_point;
8098
8099 shndx = NULL;
8100 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
8101 {
8102 if (entry->hdr->sh_link != (size_t) (section - filedata->section_headers))
8103 continue;
8104
8105 if (shndx != NULL)
8106 {
8107 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
8108 free (shndx);
8109 }
8110
8111 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
8112 entry->hdr->sh_offset,
8113 1, entry->hdr->sh_size,
8114 _("symbol table section indices"));
8115 if (shndx == NULL)
8116 goto exit_point;
8117
8118 /* PR17531: file: heap-buffer-overflow */
8119 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
8120 {
8121 error (_("Index section %s has an sh_size of %#" PRIx64 " - expected %#" PRIx64 "\n"),
8122 printable_section_name (filedata, entry->hdr),
8123 entry->hdr->sh_size,
8124 section->sh_size);
8125 goto exit_point;
8126 }
8127 }
8128
8129 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
8130
8131 if (isyms == NULL)
8132 {
8133 error (_("Out of memory reading %" PRIu64 " symbols\n"), number);
8134 goto exit_point;
8135 }
8136
8137 for (j = 0, psym = isyms; j < number; j++, psym++)
8138 {
8139 psym->st_name = BYTE_GET (esyms[j].st_name);
8140 psym->st_info = BYTE_GET (esyms[j].st_info);
8141 psym->st_other = BYTE_GET (esyms[j].st_other);
8142 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
8143
8144 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
8145 psym->st_shndx
8146 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
8147 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
8148 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
8149
8150 psym->st_value = BYTE_GET (esyms[j].st_value);
8151 psym->st_size = BYTE_GET (esyms[j].st_size);
8152 }
8153
8154 exit_point:
8155 free (shndx);
8156 free (esyms);
8157
8158 if (num_syms_return != NULL)
8159 * num_syms_return = isyms == NULL ? 0 : number;
8160
8161 return isyms;
8162 }
8163
8164 static Elf_Internal_Sym *
8165 get_elf_symbols (Filedata *filedata,
8166 Elf_Internal_Shdr *section,
8167 uint64_t *num_syms_return)
8168 {
8169 if (is_32bit_elf)
8170 return get_32bit_elf_symbols (filedata, section, num_syms_return);
8171 else
8172 return get_64bit_elf_symbols (filedata, section, num_syms_return);
8173 }
8174
8175 static const char *
8176 get_elf_section_flags (Filedata * filedata, uint64_t sh_flags)
8177 {
8178 static char buff[1024];
8179 char * p = buff;
8180 unsigned int field_size = is_32bit_elf ? 8 : 16;
8181 signed int sindex;
8182 unsigned int size = sizeof (buff) - (field_size + 4 + 1);
8183 uint64_t os_flags = 0;
8184 uint64_t proc_flags = 0;
8185 uint64_t unknown_flags = 0;
8186 static const struct
8187 {
8188 const char * str;
8189 unsigned int len;
8190 }
8191 flags [] =
8192 {
8193 /* 0 */ { STRING_COMMA_LEN ("WRITE") },
8194 /* 1 */ { STRING_COMMA_LEN ("ALLOC") },
8195 /* 2 */ { STRING_COMMA_LEN ("EXEC") },
8196 /* 3 */ { STRING_COMMA_LEN ("MERGE") },
8197 /* 4 */ { STRING_COMMA_LEN ("STRINGS") },
8198 /* 5 */ { STRING_COMMA_LEN ("INFO LINK") },
8199 /* 6 */ { STRING_COMMA_LEN ("LINK ORDER") },
8200 /* 7 */ { STRING_COMMA_LEN ("OS NONCONF") },
8201 /* 8 */ { STRING_COMMA_LEN ("GROUP") },
8202 /* 9 */ { STRING_COMMA_LEN ("TLS") },
8203 /* IA-64 specific. */
8204 /* 10 */ { STRING_COMMA_LEN ("SHORT") },
8205 /* 11 */ { STRING_COMMA_LEN ("NORECOV") },
8206 /* IA-64 OpenVMS specific. */
8207 /* 12 */ { STRING_COMMA_LEN ("VMS_GLOBAL") },
8208 /* 13 */ { STRING_COMMA_LEN ("VMS_OVERLAID") },
8209 /* 14 */ { STRING_COMMA_LEN ("VMS_SHARED") },
8210 /* 15 */ { STRING_COMMA_LEN ("VMS_VECTOR") },
8211 /* 16 */ { STRING_COMMA_LEN ("VMS_ALLOC_64BIT") },
8212 /* 17 */ { STRING_COMMA_LEN ("VMS_PROTECTED") },
8213 /* Generic. */
8214 /* 18 */ { STRING_COMMA_LEN ("EXCLUDE") },
8215 /* SPARC specific. */
8216 /* 19 */ { STRING_COMMA_LEN ("ORDERED") },
8217 /* 20 */ { STRING_COMMA_LEN ("COMPRESSED") },
8218 /* ARM specific. */
8219 /* 21 */ { STRING_COMMA_LEN ("ENTRYSECT") },
8220 /* 22 */ { STRING_COMMA_LEN ("ARM_PURECODE") },
8221 /* 23 */ { STRING_COMMA_LEN ("COMDEF") },
8222 /* GNU specific. */
8223 /* 24 */ { STRING_COMMA_LEN ("GNU_MBIND") },
8224 /* VLE specific. */
8225 /* 25 */ { STRING_COMMA_LEN ("VLE") },
8226 /* GNU specific. */
8227 /* 26 */ { STRING_COMMA_LEN ("GNU_RETAIN") },
8228 };
8229
8230 if (do_section_details)
8231 p += sprintf (p, "[%*.*lx]: ",
8232 field_size, field_size, (unsigned long) sh_flags);
8233
8234 while (sh_flags)
8235 {
8236 uint64_t flag;
8237
8238 flag = sh_flags & - sh_flags;
8239 sh_flags &= ~ flag;
8240
8241 if (do_section_details)
8242 {
8243 switch (flag)
8244 {
8245 case SHF_WRITE: sindex = 0; break;
8246 case SHF_ALLOC: sindex = 1; break;
8247 case SHF_EXECINSTR: sindex = 2; break;
8248 case SHF_MERGE: sindex = 3; break;
8249 case SHF_STRINGS: sindex = 4; break;
8250 case SHF_INFO_LINK: sindex = 5; break;
8251 case SHF_LINK_ORDER: sindex = 6; break;
8252 case SHF_OS_NONCONFORMING: sindex = 7; break;
8253 case SHF_GROUP: sindex = 8; break;
8254 case SHF_TLS: sindex = 9; break;
8255 case SHF_EXCLUDE: sindex = 18; break;
8256 case SHF_COMPRESSED: sindex = 20; break;
8257
8258 default:
8259 sindex = -1;
8260 switch (filedata->file_header.e_machine)
8261 {
8262 case EM_IA_64:
8263 if (flag == SHF_IA_64_SHORT)
8264 sindex = 10;
8265 else if (flag == SHF_IA_64_NORECOV)
8266 sindex = 11;
8267 else if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
8268 switch (flag)
8269 {
8270 case SHF_IA_64_VMS_GLOBAL: sindex = 12; break;
8271 case SHF_IA_64_VMS_OVERLAID: sindex = 13; break;
8272 case SHF_IA_64_VMS_SHARED: sindex = 14; break;
8273 case SHF_IA_64_VMS_VECTOR: sindex = 15; break;
8274 case SHF_IA_64_VMS_ALLOC_64BIT: sindex = 16; break;
8275 case SHF_IA_64_VMS_PROTECTED: sindex = 17; break;
8276 default: break;
8277 }
8278 break;
8279
8280 case EM_386:
8281 case EM_IAMCU:
8282 case EM_X86_64:
8283 case EM_L1OM:
8284 case EM_K1OM:
8285 case EM_OLD_SPARCV9:
8286 case EM_SPARC32PLUS:
8287 case EM_SPARCV9:
8288 case EM_SPARC:
8289 if (flag == SHF_ORDERED)
8290 sindex = 19;
8291 break;
8292
8293 case EM_ARM:
8294 switch (flag)
8295 {
8296 case SHF_ENTRYSECT: sindex = 21; break;
8297 case SHF_ARM_PURECODE: sindex = 22; break;
8298 case SHF_COMDEF: sindex = 23; break;
8299 default: break;
8300 }
8301 break;
8302 case EM_PPC:
8303 if (flag == SHF_PPC_VLE)
8304 sindex = 25;
8305 break;
8306 default:
8307 break;
8308 }
8309
8310 switch (filedata->file_header.e_ident[EI_OSABI])
8311 {
8312 case ELFOSABI_GNU:
8313 case ELFOSABI_FREEBSD:
8314 if (flag == SHF_GNU_RETAIN)
8315 sindex = 26;
8316 /* Fall through */
8317 case ELFOSABI_NONE:
8318 if (flag == SHF_GNU_MBIND)
8319 /* We should not recognize SHF_GNU_MBIND for
8320 ELFOSABI_NONE, but binutils as of 2019-07-23 did
8321 not set the EI_OSABI header byte. */
8322 sindex = 24;
8323 break;
8324 default:
8325 break;
8326 }
8327 break;
8328 }
8329
8330 if (sindex != -1)
8331 {
8332 if (p != buff + field_size + 4)
8333 {
8334 if (size < (10 + 2))
8335 {
8336 warn (_("Internal error: not enough buffer room for section flag info"));
8337 return _("<unknown>");
8338 }
8339 size -= 2;
8340 *p++ = ',';
8341 *p++ = ' ';
8342 }
8343
8344 size -= flags [sindex].len;
8345 p = stpcpy (p, flags [sindex].str);
8346 }
8347 else if (flag & SHF_MASKOS)
8348 os_flags |= flag;
8349 else if (flag & SHF_MASKPROC)
8350 proc_flags |= flag;
8351 else
8352 unknown_flags |= flag;
8353 }
8354 else
8355 {
8356 switch (flag)
8357 {
8358 case SHF_WRITE: *p = 'W'; break;
8359 case SHF_ALLOC: *p = 'A'; break;
8360 case SHF_EXECINSTR: *p = 'X'; break;
8361 case SHF_MERGE: *p = 'M'; break;
8362 case SHF_STRINGS: *p = 'S'; break;
8363 case SHF_INFO_LINK: *p = 'I'; break;
8364 case SHF_LINK_ORDER: *p = 'L'; break;
8365 case SHF_OS_NONCONFORMING: *p = 'O'; break;
8366 case SHF_GROUP: *p = 'G'; break;
8367 case SHF_TLS: *p = 'T'; break;
8368 case SHF_EXCLUDE: *p = 'E'; break;
8369 case SHF_COMPRESSED: *p = 'C'; break;
8370
8371 default:
8372 if ((filedata->file_header.e_machine == EM_X86_64
8373 || filedata->file_header.e_machine == EM_L1OM
8374 || filedata->file_header.e_machine == EM_K1OM)
8375 && flag == SHF_X86_64_LARGE)
8376 *p = 'l';
8377 else if (filedata->file_header.e_machine == EM_ARM
8378 && flag == SHF_ARM_PURECODE)
8379 *p = 'y';
8380 else if (filedata->file_header.e_machine == EM_PPC
8381 && flag == SHF_PPC_VLE)
8382 *p = 'v';
8383 else if (flag & SHF_MASKOS)
8384 {
8385 switch (filedata->file_header.e_ident[EI_OSABI])
8386 {
8387 case ELFOSABI_GNU:
8388 case ELFOSABI_FREEBSD:
8389 if (flag == SHF_GNU_RETAIN)
8390 {
8391 *p = 'R';
8392 break;
8393 }
8394 /* Fall through */
8395 case ELFOSABI_NONE:
8396 if (flag == SHF_GNU_MBIND)
8397 {
8398 /* We should not recognize SHF_GNU_MBIND for
8399 ELFOSABI_NONE, but binutils as of 2019-07-23 did
8400 not set the EI_OSABI header byte. */
8401 *p = 'D';
8402 break;
8403 }
8404 /* Fall through */
8405 default:
8406 *p = 'o';
8407 sh_flags &= ~SHF_MASKOS;
8408 break;
8409 }
8410 }
8411 else if (flag & SHF_MASKPROC)
8412 {
8413 *p = 'p';
8414 sh_flags &= ~ SHF_MASKPROC;
8415 }
8416 else
8417 *p = 'x';
8418 break;
8419 }
8420 p++;
8421 }
8422 }
8423
8424 if (do_section_details)
8425 {
8426 if (os_flags)
8427 {
8428 if (p != buff + field_size + 4)
8429 {
8430 if (size < 2 + 5 + field_size + 1)
8431 {
8432 warn (_("Internal error: not enough buffer room for section flag info"));
8433 return _("<unknown>");
8434 }
8435 size -= 2;
8436 *p++ = ',';
8437 *p++ = ' ';
8438 }
8439 size -= 5 + field_size;
8440 p += sprintf (p, "OS (%*.*lx)", field_size, field_size,
8441 (unsigned long) os_flags);
8442 }
8443 if (proc_flags)
8444 {
8445 if (p != buff + field_size + 4)
8446 {
8447 if (size < 2 + 7 + field_size + 1)
8448 {
8449 warn (_("Internal error: not enough buffer room for section flag info"));
8450 return _("<unknown>");
8451 }
8452 size -= 2;
8453 *p++ = ',';
8454 *p++ = ' ';
8455 }
8456 size -= 7 + field_size;
8457 p += sprintf (p, "PROC (%*.*lx)", field_size, field_size,
8458 (unsigned long) proc_flags);
8459 }
8460 if (unknown_flags)
8461 {
8462 if (p != buff + field_size + 4)
8463 {
8464 if (size < 2 + 10 + field_size + 1)
8465 {
8466 warn (_("Internal error: not enough buffer room for section flag info"));
8467 return _("<unknown>");
8468 }
8469 size -= 2;
8470 *p++ = ',';
8471 *p++ = ' ';
8472 }
8473 size -= 10 + field_size;
8474 p += sprintf (p, _("UNKNOWN (%*.*lx)"), field_size, field_size,
8475 (unsigned long) unknown_flags);
8476 }
8477 }
8478
8479 *p = '\0';
8480 return buff;
8481 }
8482
8483 static unsigned int ATTRIBUTE_WARN_UNUSED_RESULT
8484 get_compression_header (Elf_Internal_Chdr *chdr, unsigned char *buf,
8485 uint64_t size)
8486 {
8487 if (is_32bit_elf)
8488 {
8489 Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) buf;
8490
8491 if (size < sizeof (* echdr))
8492 {
8493 error (_("Compressed section is too small even for a compression header\n"));
8494 return 0;
8495 }
8496
8497 chdr->ch_type = BYTE_GET (echdr->ch_type);
8498 chdr->ch_size = BYTE_GET (echdr->ch_size);
8499 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
8500 return sizeof (*echdr);
8501 }
8502 else
8503 {
8504 Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) buf;
8505
8506 if (size < sizeof (* echdr))
8507 {
8508 error (_("Compressed section is too small even for a compression header\n"));
8509 return 0;
8510 }
8511
8512 chdr->ch_type = BYTE_GET (echdr->ch_type);
8513 chdr->ch_size = BYTE_GET (echdr->ch_size);
8514 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
8515 return sizeof (*echdr);
8516 }
8517 }
8518
8519 static bool
8520 process_section_headers (Filedata * filedata)
8521 {
8522 Elf_Internal_Shdr * section;
8523 unsigned int i;
8524
8525 if (filedata->file_header.e_shnum == 0)
8526 {
8527 /* PR binutils/12467. */
8528 if (filedata->file_header.e_shoff != 0)
8529 {
8530 warn (_("possibly corrupt ELF file header - it has a non-zero"
8531 " section header offset, but no section headers\n"));
8532 return false;
8533 }
8534 else if (do_sections)
8535 printf (_("\nThere are no sections in this file.\n"));
8536
8537 return true;
8538 }
8539
8540 if (do_sections && !do_header)
8541 {
8542 if (filedata->is_separate && process_links)
8543 printf (_("In linked file '%s': "),
8544 printable_string (filedata->file_name, 0));
8545 if (! filedata->is_separate || process_links)
8546 printf (ngettext ("There is %d section header, "
8547 "starting at offset %#" PRIx64 ":\n",
8548 "There are %d section headers, "
8549 "starting at offset %#" PRIx64 ":\n",
8550 filedata->file_header.e_shnum),
8551 filedata->file_header.e_shnum,
8552 filedata->file_header.e_shoff);
8553 }
8554
8555 if (!get_section_headers (filedata, false))
8556 return false;
8557
8558 /* Read in the string table, so that we have names to display. */
8559 if (filedata->string_table == NULL
8560 && filedata->file_header.e_shstrndx != SHN_UNDEF
8561 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
8562 {
8563 section = filedata->section_headers + filedata->file_header.e_shstrndx;
8564
8565 if (section->sh_size != 0)
8566 {
8567 filedata->string_table = (char *) get_data (NULL, filedata, section->sh_offset,
8568 1, section->sh_size,
8569 _("string table"));
8570
8571 filedata->string_table_length = filedata->string_table != NULL ? section->sh_size : 0;
8572 }
8573 }
8574
8575 /* Scan the sections for the dynamic symbol table
8576 and dynamic string table and debug sections. */
8577 eh_addr_size = is_32bit_elf ? 4 : 8;
8578 switch (filedata->file_header.e_machine)
8579 {
8580 case EM_MIPS:
8581 case EM_MIPS_RS3_LE:
8582 /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
8583 FDE addresses. However, the ABI also has a semi-official ILP32
8584 variant for which the normal FDE address size rules apply.
8585
8586 GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
8587 section, where XX is the size of longs in bits. Unfortunately,
8588 earlier compilers provided no way of distinguishing ILP32 objects
8589 from LP64 objects, so if there's any doubt, we should assume that
8590 the official LP64 form is being used. */
8591 if ((filedata->file_header.e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI64
8592 && find_section (filedata, ".gcc_compiled_long32") == NULL)
8593 eh_addr_size = 8;
8594 break;
8595
8596 case EM_H8_300:
8597 case EM_H8_300H:
8598 switch (filedata->file_header.e_flags & EF_H8_MACH)
8599 {
8600 case E_H8_MACH_H8300:
8601 case E_H8_MACH_H8300HN:
8602 case E_H8_MACH_H8300SN:
8603 case E_H8_MACH_H8300SXN:
8604 eh_addr_size = 2;
8605 break;
8606 case E_H8_MACH_H8300H:
8607 case E_H8_MACH_H8300S:
8608 case E_H8_MACH_H8300SX:
8609 eh_addr_size = 4;
8610 break;
8611 }
8612 break;
8613
8614 case EM_M32C_OLD:
8615 case EM_M32C:
8616 switch (filedata->file_header.e_flags & EF_M32C_CPU_MASK)
8617 {
8618 case EF_M32C_CPU_M16C:
8619 eh_addr_size = 2;
8620 break;
8621 }
8622 break;
8623 }
8624
8625 #define CHECK_ENTSIZE_VALUES(section, i, size32, size64) \
8626 do \
8627 { \
8628 uint64_t expected_entsize = is_32bit_elf ? size32 : size64; \
8629 if (section->sh_entsize != expected_entsize) \
8630 { \
8631 error (_("Section %d has invalid sh_entsize of %" PRIx64 "\n"), \
8632 i, section->sh_entsize); \
8633 error (_("(Using the expected size of %" PRIx64 " for the rest of this dump)\n"), \
8634 expected_entsize); \
8635 section->sh_entsize = expected_entsize; \
8636 } \
8637 } \
8638 while (0)
8639
8640 #define CHECK_ENTSIZE(section, i, type) \
8641 CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type), \
8642 sizeof (Elf64_External_##type))
8643
8644 for (i = 0, section = filedata->section_headers;
8645 i < filedata->file_header.e_shnum;
8646 i++, section++)
8647 {
8648 const char *name = printable_section_name (filedata, section);
8649
8650 /* Run some sanity checks on the headers and
8651 possibly fill in some file data as well. */
8652 switch (section->sh_type)
8653 {
8654 case SHT_DYNSYM:
8655 if (filedata->dynamic_symbols != NULL)
8656 {
8657 error (_("File contains multiple dynamic symbol tables\n"));
8658 continue;
8659 }
8660
8661 CHECK_ENTSIZE (section, i, Sym);
8662 filedata->dynamic_symbols
8663 = get_elf_symbols (filedata, section, &filedata->num_dynamic_syms);
8664 filedata->dynamic_symtab_section = section;
8665 break;
8666
8667 case SHT_STRTAB:
8668 if (streq (name, ".dynstr"))
8669 {
8670 if (filedata->dynamic_strings != NULL)
8671 {
8672 error (_("File contains multiple dynamic string tables\n"));
8673 continue;
8674 }
8675
8676 filedata->dynamic_strings
8677 = (char *) get_data (NULL, filedata, section->sh_offset,
8678 1, section->sh_size, _("dynamic strings"));
8679 filedata->dynamic_strings_length
8680 = filedata->dynamic_strings == NULL ? 0 : section->sh_size;
8681 filedata->dynamic_strtab_section = section;
8682 }
8683 break;
8684
8685 case SHT_SYMTAB_SHNDX:
8686 {
8687 elf_section_list * entry = xmalloc (sizeof * entry);
8688
8689 entry->hdr = section;
8690 entry->next = filedata->symtab_shndx_list;
8691 filedata->symtab_shndx_list = entry;
8692 }
8693 break;
8694
8695 case SHT_SYMTAB:
8696 CHECK_ENTSIZE (section, i, Sym);
8697 break;
8698
8699 case SHT_GROUP:
8700 CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
8701 break;
8702
8703 case SHT_REL:
8704 CHECK_ENTSIZE (section, i, Rel);
8705 if (do_checks && section->sh_size == 0)
8706 warn (_("Section '%s': zero-sized relocation section\n"), name);
8707 break;
8708
8709 case SHT_RELA:
8710 CHECK_ENTSIZE (section, i, Rela);
8711 if (do_checks && section->sh_size == 0)
8712 warn (_("Section '%s': zero-sized relocation section\n"), name);
8713 break;
8714
8715 case SHT_RELR:
8716 CHECK_ENTSIZE (section, i, Relr);
8717 break;
8718
8719 case SHT_NOTE:
8720 case SHT_PROGBITS:
8721 case SHT_GNU_SFRAME:
8722 /* Having a zero sized section is not illegal according to the
8723 ELF standard, but it might be an indication that something
8724 is wrong. So issue a warning if we are running in lint mode. */
8725 if (do_checks && section->sh_size == 0)
8726 warn (_("Section '%s': has a size of zero - is this intended ?\n"), name);
8727 break;
8728
8729 default:
8730 break;
8731 }
8732
8733 if ((do_debugging || do_debug_info || do_debug_abbrevs
8734 || do_debug_lines || do_debug_pubnames || do_debug_pubtypes
8735 || do_debug_aranges || do_debug_frames || do_debug_macinfo
8736 || do_debug_str || do_debug_str_offsets || do_debug_loc
8737 || do_debug_ranges
8738 || do_debug_addr || do_debug_cu_index || do_debug_links)
8739 && (startswith (name, ".debug_")
8740 || startswith (name, ".zdebug_")))
8741 {
8742 if (name[1] == 'z')
8743 name += sizeof (".zdebug_") - 1;
8744 else
8745 name += sizeof (".debug_") - 1;
8746
8747 if (do_debugging
8748 || (do_debug_info && startswith (name, "info"))
8749 || (do_debug_info && startswith (name, "types"))
8750 || (do_debug_abbrevs && startswith (name, "abbrev"))
8751 || (do_debug_lines && strcmp (name, "line") == 0)
8752 || (do_debug_lines && startswith (name, "line."))
8753 || (do_debug_pubnames && startswith (name, "pubnames"))
8754 || (do_debug_pubtypes && startswith (name, "pubtypes"))
8755 || (do_debug_pubnames && startswith (name, "gnu_pubnames"))
8756 || (do_debug_pubtypes && startswith (name, "gnu_pubtypes"))
8757 || (do_debug_aranges && startswith (name, "aranges"))
8758 || (do_debug_ranges && startswith (name, "ranges"))
8759 || (do_debug_ranges && startswith (name, "rnglists"))
8760 || (do_debug_frames && startswith (name, "frame"))
8761 || (do_debug_macinfo && startswith (name, "macinfo"))
8762 || (do_debug_macinfo && startswith (name, "macro"))
8763 || (do_debug_str && startswith (name, "str"))
8764 || (do_debug_links && startswith (name, "sup"))
8765 || (do_debug_str_offsets && startswith (name, "str_offsets"))
8766 || (do_debug_loc && startswith (name, "loc"))
8767 || (do_debug_loc && startswith (name, "loclists"))
8768 || (do_debug_addr && startswith (name, "addr"))
8769 || (do_debug_cu_index && startswith (name, "cu_index"))
8770 || (do_debug_cu_index && startswith (name, "tu_index"))
8771 )
8772 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
8773 }
8774 /* Linkonce section to be combined with .debug_info at link time. */
8775 else if ((do_debugging || do_debug_info)
8776 && startswith (name, ".gnu.linkonce.wi."))
8777 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
8778 else if (do_debug_frames && streq (name, ".eh_frame"))
8779 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
8780 else if (do_debug_frames && streq (name, ".eh_frame_hdr"))
8781 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
8782 else if (do_gdb_index && (streq (name, ".gdb_index")
8783 || streq (name, ".debug_names")))
8784 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
8785 /* Trace sections for Itanium VMS. */
8786 else if ((do_debugging || do_trace_info || do_trace_abbrevs
8787 || do_trace_aranges)
8788 && startswith (name, ".trace_"))
8789 {
8790 name += sizeof (".trace_") - 1;
8791
8792 if (do_debugging
8793 || (do_trace_info && streq (name, "info"))
8794 || (do_trace_abbrevs && streq (name, "abbrev"))
8795 || (do_trace_aranges && streq (name, "aranges"))
8796 )
8797 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
8798 }
8799 else if ((do_debugging || do_debug_links)
8800 && (startswith (name, ".gnu_debuglink")
8801 || startswith (name, ".gnu_debugaltlink")))
8802 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
8803 }
8804
8805 if (! do_sections)
8806 return true;
8807
8808 if (filedata->is_separate && ! process_links)
8809 return true;
8810
8811 if (filedata->is_separate)
8812 printf (_("\nSection Headers in linked file '%s':\n"),
8813 printable_string (filedata->file_name, 0));
8814 else if (filedata->file_header.e_shnum > 1)
8815 printf (_("\nSection Headers:\n"));
8816 else
8817 printf (_("\nSection Header:\n"));
8818
8819 if (is_32bit_elf)
8820 {
8821 if (do_section_details)
8822 {
8823 printf (_(" [Nr] Name\n"));
8824 printf (_(" Type Addr Off Size ES Lk Inf Al\n"));
8825 }
8826 else
8827 printf
8828 (_(" [Nr] Name Type Addr Off Size ES Flg Lk Inf Al\n"));
8829 }
8830 else if (do_wide)
8831 {
8832 if (do_section_details)
8833 {
8834 printf (_(" [Nr] Name\n"));
8835 printf (_(" Type Address Off Size ES Lk Inf Al\n"));
8836 }
8837 else
8838 printf
8839 (_(" [Nr] Name Type Address Off Size ES Flg Lk Inf Al\n"));
8840 }
8841 else
8842 {
8843 if (do_section_details)
8844 {
8845 printf (_(" [Nr] Name\n"));
8846 printf (_(" Type Address Offset Link\n"));
8847 printf (_(" Size EntSize Info Align\n"));
8848 }
8849 else
8850 {
8851 printf (_(" [Nr] Name Type Address Offset\n"));
8852 printf (_(" Size EntSize Flags Link Info Align\n"));
8853 }
8854 }
8855
8856 if (do_section_details)
8857 printf (_(" Flags\n"));
8858
8859 for (i = 0, section = filedata->section_headers;
8860 i < filedata->file_header.e_shnum;
8861 i++, section++)
8862 {
8863 /* Run some sanity checks on the section header. */
8864
8865 /* Check the sh_link field. */
8866 switch (section->sh_type)
8867 {
8868 case SHT_REL:
8869 case SHT_RELR:
8870 case SHT_RELA:
8871 if (section->sh_link == 0
8872 && (filedata->file_header.e_type == ET_EXEC
8873 || filedata->file_header.e_type == ET_DYN))
8874 /* A dynamic relocation section where all entries use a
8875 zero symbol index need not specify a symtab section. */
8876 break;
8877 /* Fall through. */
8878 case SHT_SYMTAB_SHNDX:
8879 case SHT_GROUP:
8880 case SHT_HASH:
8881 case SHT_GNU_HASH:
8882 case SHT_GNU_versym:
8883 if (section->sh_link == 0
8884 || section->sh_link >= filedata->file_header.e_shnum
8885 || (filedata->section_headers[section->sh_link].sh_type != SHT_SYMTAB
8886 && filedata->section_headers[section->sh_link].sh_type != SHT_DYNSYM))
8887 warn (_("[%2u]: Link field (%u) should index a symtab section.\n"),
8888 i, section->sh_link);
8889 break;
8890
8891 case SHT_DYNAMIC:
8892 case SHT_SYMTAB:
8893 case SHT_DYNSYM:
8894 case SHT_GNU_verneed:
8895 case SHT_GNU_verdef:
8896 case SHT_GNU_LIBLIST:
8897 if (section->sh_link == 0
8898 || section->sh_link >= filedata->file_header.e_shnum
8899 || filedata->section_headers[section->sh_link].sh_type != SHT_STRTAB)
8900 warn (_("[%2u]: Link field (%u) should index a string section.\n"),
8901 i, section->sh_link);
8902 break;
8903
8904 case SHT_INIT_ARRAY:
8905 case SHT_FINI_ARRAY:
8906 case SHT_PREINIT_ARRAY:
8907 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
8908 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
8909 i, section->sh_link);
8910 break;
8911
8912 default:
8913 /* FIXME: Add support for target specific section types. */
8914 #if 0 /* Currently we do not check other section types as there are too
8915 many special cases. Stab sections for example have a type
8916 of SHT_PROGBITS but an sh_link field that links to the .stabstr
8917 section. */
8918 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
8919 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
8920 i, section->sh_link);
8921 #endif
8922 break;
8923 }
8924
8925 /* Check the sh_info field. */
8926 switch (section->sh_type)
8927 {
8928 case SHT_REL:
8929 case SHT_RELA:
8930 if (section->sh_info == 0
8931 && (filedata->file_header.e_type == ET_EXEC
8932 || filedata->file_header.e_type == ET_DYN))
8933 /* Dynamic relocations apply to segments, so they do not
8934 need to specify the section they relocate. */
8935 break;
8936 if (section->sh_info == 0
8937 || section->sh_info >= filedata->file_header.e_shnum
8938 || (filedata->section_headers[section->sh_info].sh_type != SHT_PROGBITS
8939 && filedata->section_headers[section->sh_info].sh_type != SHT_NOBITS
8940 && filedata->section_headers[section->sh_info].sh_type != SHT_NOTE
8941 && filedata->section_headers[section->sh_info].sh_type != SHT_INIT_ARRAY
8942 && filedata->section_headers[section->sh_info].sh_type != SHT_FINI_ARRAY
8943 && filedata->section_headers[section->sh_info].sh_type != SHT_PREINIT_ARRAY
8944 /* FIXME: Are other section types valid ? */
8945 && filedata->section_headers[section->sh_info].sh_type < SHT_LOOS))
8946 warn (_("[%2u]: Info field (%u) should index a relocatable section.\n"),
8947 i, section->sh_info);
8948 break;
8949
8950 case SHT_DYNAMIC:
8951 case SHT_HASH:
8952 case SHT_SYMTAB_SHNDX:
8953 case SHT_INIT_ARRAY:
8954 case SHT_FINI_ARRAY:
8955 case SHT_PREINIT_ARRAY:
8956 if (section->sh_info != 0)
8957 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
8958 i, section->sh_info);
8959 break;
8960
8961 case SHT_GROUP:
8962 case SHT_SYMTAB:
8963 case SHT_DYNSYM:
8964 /* A symbol index - we assume that it is valid. */
8965 break;
8966
8967 default:
8968 /* FIXME: Add support for target specific section types. */
8969 if (section->sh_type == SHT_NOBITS)
8970 /* NOBITS section headers with non-zero sh_info fields can be
8971 created when a binary is stripped of everything but its debug
8972 information. The stripped sections have their headers
8973 preserved but their types set to SHT_NOBITS. So do not check
8974 this type of section. */
8975 ;
8976 else if (section->sh_flags & SHF_INFO_LINK)
8977 {
8978 if (section->sh_info < 1 || section->sh_info >= filedata->file_header.e_shnum)
8979 warn (_("[%2u]: Expected link to another section in info field"), i);
8980 }
8981 else if (section->sh_type < SHT_LOOS
8982 && (section->sh_flags & SHF_GNU_MBIND) == 0
8983 && section->sh_info != 0)
8984 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
8985 i, section->sh_info);
8986 break;
8987 }
8988
8989 /* Check the sh_size field. */
8990 if (section->sh_size > filedata->file_size
8991 && section->sh_type != SHT_NOBITS
8992 && section->sh_type != SHT_NULL
8993 && section->sh_type < SHT_LOOS)
8994 warn (_("Size of section %u is larger than the entire file!\n"), i);
8995
8996 printf (" [%2u] ", i);
8997 if (do_section_details)
8998 printf ("%s\n ", printable_section_name (filedata, section));
8999 else
9000 print_symbol_name (-17, printable_section_name (filedata, section));
9001
9002 printf (do_wide ? " %-15s " : " %-15.15s ",
9003 get_section_type_name (filedata, section->sh_type));
9004
9005 if (is_32bit_elf)
9006 {
9007 const char * link_too_big = NULL;
9008
9009 print_vma (section->sh_addr, LONG_HEX);
9010
9011 printf ( " %6.6lx %6.6lx %2.2lx",
9012 (unsigned long) section->sh_offset,
9013 (unsigned long) section->sh_size,
9014 (unsigned long) section->sh_entsize);
9015
9016 if (do_section_details)
9017 fputs (" ", stdout);
9018 else
9019 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
9020
9021 if (section->sh_link >= filedata->file_header.e_shnum)
9022 {
9023 link_too_big = "";
9024 /* The sh_link value is out of range. Normally this indicates
9025 an error but it can have special values in Solaris binaries. */
9026 switch (filedata->file_header.e_machine)
9027 {
9028 case EM_386:
9029 case EM_IAMCU:
9030 case EM_X86_64:
9031 case EM_L1OM:
9032 case EM_K1OM:
9033 case EM_OLD_SPARCV9:
9034 case EM_SPARC32PLUS:
9035 case EM_SPARCV9:
9036 case EM_SPARC:
9037 if (section->sh_link == (SHN_BEFORE & 0xffff))
9038 link_too_big = "BEFORE";
9039 else if (section->sh_link == (SHN_AFTER & 0xffff))
9040 link_too_big = "AFTER";
9041 break;
9042 default:
9043 break;
9044 }
9045 }
9046
9047 if (do_section_details)
9048 {
9049 if (link_too_big != NULL && * link_too_big)
9050 printf ("<%s> ", link_too_big);
9051 else
9052 printf ("%2u ", section->sh_link);
9053 printf ("%3u %2lu\n", section->sh_info,
9054 (unsigned long) section->sh_addralign);
9055 }
9056 else
9057 printf ("%2u %3u %2lu\n",
9058 section->sh_link,
9059 section->sh_info,
9060 (unsigned long) section->sh_addralign);
9061
9062 if (link_too_big && ! * link_too_big)
9063 warn (_("section %u: sh_link value of %u is larger than the number of sections\n"),
9064 i, section->sh_link);
9065 }
9066 else if (do_wide)
9067 {
9068 print_vma (section->sh_addr, LONG_HEX);
9069
9070 if ((long) section->sh_offset == section->sh_offset)
9071 printf (" %6.6lx", (unsigned long) section->sh_offset);
9072 else
9073 {
9074 putchar (' ');
9075 print_vma (section->sh_offset, LONG_HEX);
9076 }
9077
9078 if ((unsigned long) section->sh_size == section->sh_size)
9079 printf (" %6.6lx", (unsigned long) section->sh_size);
9080 else
9081 {
9082 putchar (' ');
9083 print_vma (section->sh_size, LONG_HEX);
9084 }
9085
9086 if ((unsigned long) section->sh_entsize == section->sh_entsize)
9087 printf (" %2.2lx", (unsigned long) section->sh_entsize);
9088 else
9089 {
9090 putchar (' ');
9091 print_vma (section->sh_entsize, LONG_HEX);
9092 }
9093
9094 if (do_section_details)
9095 fputs (" ", stdout);
9096 else
9097 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
9098
9099 printf ("%2u %3u ", section->sh_link, section->sh_info);
9100
9101 if ((unsigned long) section->sh_addralign == section->sh_addralign)
9102 printf ("%2lu\n", (unsigned long) section->sh_addralign);
9103 else
9104 {
9105 print_vma (section->sh_addralign, DEC);
9106 putchar ('\n');
9107 }
9108 }
9109 else if (do_section_details)
9110 {
9111 putchar (' ');
9112 print_vma (section->sh_addr, LONG_HEX);
9113 if ((long) section->sh_offset == section->sh_offset)
9114 printf (" %16.16lx", (unsigned long) section->sh_offset);
9115 else
9116 {
9117 printf (" ");
9118 print_vma (section->sh_offset, LONG_HEX);
9119 }
9120 printf (" %u\n ", section->sh_link);
9121 print_vma (section->sh_size, LONG_HEX);
9122 putchar (' ');
9123 print_vma (section->sh_entsize, LONG_HEX);
9124
9125 printf (" %-16u %lu\n",
9126 section->sh_info,
9127 (unsigned long) section->sh_addralign);
9128 }
9129 else
9130 {
9131 putchar (' ');
9132 print_vma (section->sh_addr, LONG_HEX);
9133 if ((long) section->sh_offset == section->sh_offset)
9134 printf (" %8.8lx", (unsigned long) section->sh_offset);
9135 else
9136 {
9137 printf (" ");
9138 print_vma (section->sh_offset, LONG_HEX);
9139 }
9140 printf ("\n ");
9141 print_vma (section->sh_size, LONG_HEX);
9142 printf (" ");
9143 print_vma (section->sh_entsize, LONG_HEX);
9144
9145 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
9146
9147 printf (" %2u %3u %lu\n",
9148 section->sh_link,
9149 section->sh_info,
9150 (unsigned long) section->sh_addralign);
9151 }
9152
9153 if (do_section_details)
9154 {
9155 printf (" %s\n", get_elf_section_flags (filedata, section->sh_flags));
9156 if ((section->sh_flags & SHF_COMPRESSED) != 0)
9157 {
9158 /* Minimum section size is 12 bytes for 32-bit compression
9159 header + 12 bytes for compressed data header. */
9160 unsigned char buf[24];
9161
9162 assert (sizeof (buf) >= sizeof (Elf64_External_Chdr));
9163 if (get_data (&buf, filedata, section->sh_offset, 1,
9164 sizeof (buf), _("compression header")))
9165 {
9166 Elf_Internal_Chdr chdr;
9167
9168 if (get_compression_header (&chdr, buf, sizeof (buf)) == 0)
9169 printf (_(" [<corrupt>]\n"));
9170 else
9171 {
9172 if (chdr.ch_type == ch_compress_zlib)
9173 printf (" ZLIB, ");
9174 else if (chdr.ch_type == ch_compress_zstd)
9175 printf (" ZSTD, ");
9176 else
9177 printf (_(" [<unknown>: 0x%x], "),
9178 chdr.ch_type);
9179 print_vma (chdr.ch_size, LONG_HEX);
9180 printf (", %lu\n", (unsigned long) chdr.ch_addralign);
9181 }
9182 }
9183 }
9184 }
9185 }
9186
9187 if (!do_section_details)
9188 {
9189 /* The ordering of the letters shown here matches the ordering of the
9190 corresponding SHF_xxx values, and hence the order in which these
9191 letters will be displayed to the user. */
9192 printf (_("Key to Flags:\n\
9193 W (write), A (alloc), X (execute), M (merge), S (strings), I (info),\n\
9194 L (link order), O (extra OS processing required), G (group), T (TLS),\n\
9195 C (compressed), x (unknown), o (OS specific), E (exclude),\n "));
9196 switch (filedata->file_header.e_ident[EI_OSABI])
9197 {
9198 case ELFOSABI_GNU:
9199 case ELFOSABI_FREEBSD:
9200 printf (_("R (retain), "));
9201 /* Fall through */
9202 case ELFOSABI_NONE:
9203 printf (_("D (mbind), "));
9204 break;
9205 default:
9206 break;
9207 }
9208 if (filedata->file_header.e_machine == EM_X86_64
9209 || filedata->file_header.e_machine == EM_L1OM
9210 || filedata->file_header.e_machine == EM_K1OM)
9211 printf (_("l (large), "));
9212 else if (filedata->file_header.e_machine == EM_ARM)
9213 printf (_("y (purecode), "));
9214 else if (filedata->file_header.e_machine == EM_PPC)
9215 printf (_("v (VLE), "));
9216 printf ("p (processor specific)\n");
9217 }
9218
9219 return true;
9220 }
9221
9222 static bool
9223 get_symtab (Filedata * filedata,
9224 Elf_Internal_Shdr * symsec,
9225 Elf_Internal_Sym ** symtab,
9226 uint64_t * nsyms,
9227 char ** strtab,
9228 uint64_t * strtablen)
9229 {
9230 *strtab = NULL;
9231 *strtablen = 0;
9232 *symtab = get_elf_symbols (filedata, symsec, nsyms);
9233
9234 if (*symtab == NULL)
9235 return false;
9236
9237 if (symsec->sh_link != 0)
9238 {
9239 Elf_Internal_Shdr *strsec;
9240
9241 if (symsec->sh_link >= filedata->file_header.e_shnum)
9242 {
9243 error (_("Bad sh_link in symbol table section\n"));
9244 free (*symtab);
9245 *symtab = NULL;
9246 *nsyms = 0;
9247 return false;
9248 }
9249
9250 strsec = filedata->section_headers + symsec->sh_link;
9251
9252 *strtab = (char *) get_data (NULL, filedata, strsec->sh_offset,
9253 1, strsec->sh_size, _("string table"));
9254 if (*strtab == NULL)
9255 {
9256 free (*symtab);
9257 *symtab = NULL;
9258 *nsyms = 0;
9259 return false;
9260 }
9261 *strtablen = strsec->sh_size;
9262 }
9263 return true;
9264 }
9265
9266 static const char *
9267 get_group_flags (unsigned int flags)
9268 {
9269 static char buff[128];
9270
9271 if (flags == 0)
9272 return "";
9273 else if (flags == GRP_COMDAT)
9274 return "COMDAT ";
9275
9276 snprintf (buff, sizeof buff, "[0x%x: %s%s%s]",
9277 flags,
9278 flags & GRP_MASKOS ? _("<OS specific>") : "",
9279 flags & GRP_MASKPROC ? _("<PROC specific>") : "",
9280 (flags & ~(GRP_COMDAT | GRP_MASKOS | GRP_MASKPROC)
9281 ? _("<unknown>") : ""));
9282
9283 return buff;
9284 }
9285
9286 static bool
9287 process_section_groups (Filedata * filedata)
9288 {
9289 Elf_Internal_Shdr * section;
9290 unsigned int i;
9291 struct group * group;
9292 Elf_Internal_Shdr * symtab_sec;
9293 Elf_Internal_Shdr * strtab_sec;
9294 Elf_Internal_Sym * symtab;
9295 uint64_t num_syms;
9296 char * strtab;
9297 size_t strtab_size;
9298
9299 /* Don't process section groups unless needed. */
9300 if (!do_unwind && !do_section_groups)
9301 return true;
9302
9303 if (filedata->file_header.e_shnum == 0)
9304 {
9305 if (do_section_groups)
9306 {
9307 if (filedata->is_separate)
9308 printf (_("\nThere are no sections group in linked file '%s'.\n"),
9309 printable_string (filedata->file_name, 0));
9310 else
9311 printf (_("\nThere are no section groups in this file.\n"));
9312 }
9313 return true;
9314 }
9315
9316 if (filedata->section_headers == NULL)
9317 {
9318 error (_("Section headers are not available!\n"));
9319 /* PR 13622: This can happen with a corrupt ELF header. */
9320 return false;
9321 }
9322
9323 filedata->section_headers_groups
9324 = (struct group **) calloc (filedata->file_header.e_shnum,
9325 sizeof (struct group *));
9326
9327 if (filedata->section_headers_groups == NULL)
9328 {
9329 error (_("Out of memory reading %u section group headers\n"),
9330 filedata->file_header.e_shnum);
9331 return false;
9332 }
9333
9334 /* Scan the sections for the group section. */
9335 filedata->group_count = 0;
9336 for (i = 0, section = filedata->section_headers;
9337 i < filedata->file_header.e_shnum;
9338 i++, section++)
9339 if (section->sh_type == SHT_GROUP)
9340 filedata->group_count++;
9341
9342 if (filedata->group_count == 0)
9343 {
9344 if (do_section_groups)
9345 {
9346 if (filedata->is_separate)
9347 printf (_("\nThere are no section groups in linked file '%s'.\n"),
9348 printable_string (filedata->file_name, 0));
9349 else
9350 printf (_("\nThere are no section groups in this file.\n"));
9351 }
9352
9353 return true;
9354 }
9355
9356 filedata->section_groups = (struct group *) calloc (filedata->group_count,
9357 sizeof (struct group));
9358
9359 if (filedata->section_groups == NULL)
9360 {
9361 error (_("Out of memory reading %zu groups\n"), filedata->group_count);
9362 return false;
9363 }
9364
9365 symtab_sec = NULL;
9366 strtab_sec = NULL;
9367 symtab = NULL;
9368 num_syms = 0;
9369 strtab = NULL;
9370 strtab_size = 0;
9371
9372 if (filedata->is_separate)
9373 printf (_("Section groups in linked file '%s'\n"),
9374 printable_string (filedata->file_name, 0));
9375
9376 for (i = 0, section = filedata->section_headers, group = filedata->section_groups;
9377 i < filedata->file_header.e_shnum;
9378 i++, section++)
9379 {
9380 if (section->sh_type == SHT_GROUP)
9381 {
9382 const char * name = printable_section_name (filedata, section);
9383 const char * group_name;
9384 unsigned char * start;
9385 unsigned char * indices;
9386 unsigned int entry, j, size;
9387 Elf_Internal_Shdr * sec;
9388 Elf_Internal_Sym * sym;
9389
9390 /* Get the symbol table. */
9391 if (section->sh_link >= filedata->file_header.e_shnum
9392 || ((sec = filedata->section_headers + section->sh_link)->sh_type
9393 != SHT_SYMTAB))
9394 {
9395 error (_("Bad sh_link in group section `%s'\n"), name);
9396 continue;
9397 }
9398
9399 if (symtab_sec != sec)
9400 {
9401 symtab_sec = sec;
9402 free (symtab);
9403 symtab = get_elf_symbols (filedata, symtab_sec, & num_syms);
9404 }
9405
9406 if (symtab == NULL)
9407 {
9408 error (_("Corrupt header in group section `%s'\n"), name);
9409 continue;
9410 }
9411
9412 if (section->sh_info >= num_syms)
9413 {
9414 error (_("Bad sh_info in group section `%s'\n"), name);
9415 continue;
9416 }
9417
9418 sym = symtab + section->sh_info;
9419
9420 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
9421 {
9422 if (sym->st_shndx == 0
9423 || sym->st_shndx >= filedata->file_header.e_shnum)
9424 {
9425 error (_("Bad sh_info in group section `%s'\n"), name);
9426 continue;
9427 }
9428
9429 group_name = printable_section_name (filedata,
9430 filedata->section_headers
9431 + sym->st_shndx);
9432 strtab_sec = NULL;
9433 free (strtab);
9434 strtab = NULL;
9435 strtab_size = 0;
9436 }
9437 else
9438 {
9439 /* Get the string table. */
9440 if (symtab_sec->sh_link >= filedata->file_header.e_shnum)
9441 {
9442 strtab_sec = NULL;
9443 free (strtab);
9444 strtab = NULL;
9445 strtab_size = 0;
9446 }
9447 else if (strtab_sec
9448 != (sec = filedata->section_headers + symtab_sec->sh_link))
9449 {
9450 strtab_sec = sec;
9451 free (strtab);
9452
9453 strtab = (char *) get_data (NULL, filedata, strtab_sec->sh_offset,
9454 1, strtab_sec->sh_size,
9455 _("string table"));
9456 strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
9457 }
9458 group_name = sym->st_name < strtab_size
9459 ? strtab + sym->st_name : _("<corrupt>");
9460 }
9461
9462 /* PR 17531: file: loop. */
9463 if (section->sh_entsize > section->sh_size)
9464 {
9465 error (_("Section %s has sh_entsize (%#" PRIx64 ")"
9466 " which is larger than its size (%#" PRIx64 ")\n"),
9467 printable_section_name (filedata, section),
9468 section->sh_entsize,
9469 section->sh_size);
9470 continue;
9471 }
9472
9473 start = (unsigned char *) get_data (NULL, filedata, section->sh_offset,
9474 1, section->sh_size,
9475 _("section data"));
9476 if (start == NULL)
9477 continue;
9478
9479 indices = start;
9480 size = (section->sh_size / section->sh_entsize) - 1;
9481 entry = byte_get (indices, 4);
9482 indices += 4;
9483
9484 if (do_section_groups)
9485 {
9486 printf (_("\n%sgroup section [%5u] `%s' [%s] contains %u sections:\n"),
9487 get_group_flags (entry), i, name, group_name, size);
9488
9489 printf (_(" [Index] Name\n"));
9490 }
9491
9492 group->group_index = i;
9493
9494 for (j = 0; j < size; j++)
9495 {
9496 struct group_list * g;
9497
9498 entry = byte_get (indices, 4);
9499 indices += 4;
9500
9501 if (entry >= filedata->file_header.e_shnum)
9502 {
9503 static unsigned num_group_errors = 0;
9504
9505 if (num_group_errors ++ < 10)
9506 {
9507 error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
9508 entry, i, filedata->file_header.e_shnum - 1);
9509 if (num_group_errors == 10)
9510 warn (_("Further error messages about overlarge group section indices suppressed\n"));
9511 }
9512 continue;
9513 }
9514
9515 if (filedata->section_headers_groups [entry] != NULL)
9516 {
9517 if (entry)
9518 {
9519 static unsigned num_errs = 0;
9520
9521 if (num_errs ++ < 10)
9522 {
9523 error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
9524 entry, i,
9525 filedata->section_headers_groups [entry]->group_index);
9526 if (num_errs == 10)
9527 warn (_("Further error messages about already contained group sections suppressed\n"));
9528 }
9529 continue;
9530 }
9531 else
9532 {
9533 /* Intel C/C++ compiler may put section 0 in a
9534 section group. We just warn it the first time
9535 and ignore it afterwards. */
9536 static bool warned = false;
9537 if (!warned)
9538 {
9539 error (_("section 0 in group section [%5u]\n"),
9540 filedata->section_headers_groups [entry]->group_index);
9541 warned = true;
9542 }
9543 }
9544 }
9545
9546 filedata->section_headers_groups [entry] = group;
9547
9548 if (do_section_groups)
9549 {
9550 sec = filedata->section_headers + entry;
9551 printf (" [%5u] %s\n", entry, printable_section_name (filedata, sec));
9552 }
9553
9554 g = (struct group_list *) xmalloc (sizeof (struct group_list));
9555 g->section_index = entry;
9556 g->next = group->root;
9557 group->root = g;
9558 }
9559
9560 free (start);
9561
9562 group++;
9563 }
9564 }
9565
9566 free (symtab);
9567 free (strtab);
9568 return true;
9569 }
9570
9571 /* Data used to display dynamic fixups. */
9572
9573 struct ia64_vms_dynfixup
9574 {
9575 uint64_t needed_ident; /* Library ident number. */
9576 uint64_t needed; /* Index in the dstrtab of the library name. */
9577 uint64_t fixup_needed; /* Index of the library. */
9578 uint64_t fixup_rela_cnt; /* Number of fixups. */
9579 uint64_t fixup_rela_off; /* Fixups offset in the dynamic segment. */
9580 };
9581
9582 /* Data used to display dynamic relocations. */
9583
9584 struct ia64_vms_dynimgrela
9585 {
9586 uint64_t img_rela_cnt; /* Number of relocations. */
9587 uint64_t img_rela_off; /* Reloc offset in the dynamic segment. */
9588 };
9589
9590 /* Display IA-64 OpenVMS dynamic fixups (used to dynamically link a shared
9591 library). */
9592
9593 static bool
9594 dump_ia64_vms_dynamic_fixups (Filedata * filedata,
9595 struct ia64_vms_dynfixup * fixup,
9596 const char * strtab,
9597 unsigned int strtab_sz)
9598 {
9599 Elf64_External_VMS_IMAGE_FIXUP * imfs;
9600 size_t i;
9601 const char * lib_name;
9602
9603 imfs = get_data (NULL, filedata,
9604 filedata->dynamic_addr + fixup->fixup_rela_off,
9605 sizeof (*imfs), fixup->fixup_rela_cnt,
9606 _("dynamic section image fixups"));
9607 if (!imfs)
9608 return false;
9609
9610 if (fixup->needed < strtab_sz)
9611 lib_name = strtab + fixup->needed;
9612 else
9613 {
9614 warn (_("corrupt library name index of %#" PRIx64
9615 " found in dynamic entry"), fixup->needed);
9616 lib_name = "???";
9617 }
9618
9619 printf (_("\nImage fixups for needed library #%" PRId64
9620 ": %s - ident: %" PRIx64 "\n"),
9621 fixup->fixup_needed, lib_name, fixup->needed_ident);
9622 printf
9623 (_("Seg Offset Type SymVec DataType\n"));
9624
9625 for (i = 0; i < (size_t) fixup->fixup_rela_cnt; i++)
9626 {
9627 unsigned int type;
9628 const char *rtype;
9629
9630 printf ("%3u ", (unsigned) BYTE_GET (imfs [i].fixup_seg));
9631 printf ("%016" PRIx64 " ", BYTE_GET (imfs [i].fixup_offset));
9632 type = BYTE_GET (imfs [i].type);
9633 rtype = elf_ia64_reloc_type (type);
9634 if (rtype == NULL)
9635 printf ("0x%08x ", type);
9636 else
9637 printf ("%-32s ", rtype);
9638 printf ("%6u ", (unsigned) BYTE_GET (imfs [i].symvec_index));
9639 printf ("0x%08x\n", (unsigned) BYTE_GET (imfs [i].data_type));
9640 }
9641
9642 free (imfs);
9643 return true;
9644 }
9645
9646 /* Display IA-64 OpenVMS dynamic relocations (used to relocate an image). */
9647
9648 static bool
9649 dump_ia64_vms_dynamic_relocs (Filedata * filedata, struct ia64_vms_dynimgrela *imgrela)
9650 {
9651 Elf64_External_VMS_IMAGE_RELA *imrs;
9652 size_t i;
9653
9654 imrs = get_data (NULL, filedata,
9655 filedata->dynamic_addr + imgrela->img_rela_off,
9656 sizeof (*imrs), imgrela->img_rela_cnt,
9657 _("dynamic section image relocations"));
9658 if (!imrs)
9659 return false;
9660
9661 printf (_("\nImage relocs\n"));
9662 printf
9663 (_("Seg Offset Type Addend Seg Sym Off\n"));
9664
9665 for (i = 0; i < (size_t) imgrela->img_rela_cnt; i++)
9666 {
9667 unsigned int type;
9668 const char *rtype;
9669
9670 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].rela_seg));
9671 printf ("%08" PRIx64 " ", BYTE_GET (imrs [i].rela_offset));
9672 type = BYTE_GET (imrs [i].type);
9673 rtype = elf_ia64_reloc_type (type);
9674 if (rtype == NULL)
9675 printf ("0x%08x ", type);
9676 else
9677 printf ("%-31s ", rtype);
9678 print_vma (BYTE_GET (imrs [i].addend), FULL_HEX);
9679 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].sym_seg));
9680 printf ("%08" PRIx64 "\n", BYTE_GET (imrs [i].sym_offset));
9681 }
9682
9683 free (imrs);
9684 return true;
9685 }
9686
9687 /* Display IA-64 OpenVMS dynamic relocations and fixups. */
9688
9689 static bool
9690 process_ia64_vms_dynamic_relocs (Filedata * filedata)
9691 {
9692 struct ia64_vms_dynfixup fixup;
9693 struct ia64_vms_dynimgrela imgrela;
9694 Elf_Internal_Dyn *entry;
9695 uint64_t strtab_off = 0;
9696 uint64_t strtab_sz = 0;
9697 char *strtab = NULL;
9698 bool res = true;
9699
9700 memset (&fixup, 0, sizeof (fixup));
9701 memset (&imgrela, 0, sizeof (imgrela));
9702
9703 /* Note: the order of the entries is specified by the OpenVMS specs. */
9704 for (entry = filedata->dynamic_section;
9705 entry < filedata->dynamic_section + filedata->dynamic_nent;
9706 entry++)
9707 {
9708 switch (entry->d_tag)
9709 {
9710 case DT_IA_64_VMS_STRTAB_OFFSET:
9711 strtab_off = entry->d_un.d_val;
9712 break;
9713 case DT_STRSZ:
9714 strtab_sz = entry->d_un.d_val;
9715 if (strtab == NULL)
9716 strtab = get_data (NULL, filedata,
9717 filedata->dynamic_addr + strtab_off,
9718 1, strtab_sz, _("dynamic string section"));
9719 if (strtab == NULL)
9720 strtab_sz = 0;
9721 break;
9722
9723 case DT_IA_64_VMS_NEEDED_IDENT:
9724 fixup.needed_ident = entry->d_un.d_val;
9725 break;
9726 case DT_NEEDED:
9727 fixup.needed = entry->d_un.d_val;
9728 break;
9729 case DT_IA_64_VMS_FIXUP_NEEDED:
9730 fixup.fixup_needed = entry->d_un.d_val;
9731 break;
9732 case DT_IA_64_VMS_FIXUP_RELA_CNT:
9733 fixup.fixup_rela_cnt = entry->d_un.d_val;
9734 break;
9735 case DT_IA_64_VMS_FIXUP_RELA_OFF:
9736 fixup.fixup_rela_off = entry->d_un.d_val;
9737 if (! dump_ia64_vms_dynamic_fixups (filedata, &fixup, strtab, strtab_sz))
9738 res = false;
9739 break;
9740 case DT_IA_64_VMS_IMG_RELA_CNT:
9741 imgrela.img_rela_cnt = entry->d_un.d_val;
9742 break;
9743 case DT_IA_64_VMS_IMG_RELA_OFF:
9744 imgrela.img_rela_off = entry->d_un.d_val;
9745 if (! dump_ia64_vms_dynamic_relocs (filedata, &imgrela))
9746 res = false;
9747 break;
9748
9749 default:
9750 break;
9751 }
9752 }
9753
9754 free (strtab);
9755
9756 return res;
9757 }
9758
9759 static struct
9760 {
9761 const char * name;
9762 int reloc;
9763 int size;
9764 relocation_type rel_type;
9765 }
9766 dynamic_relocations [] =
9767 {
9768 { "REL", DT_REL, DT_RELSZ, reltype_rel },
9769 { "RELA", DT_RELA, DT_RELASZ, reltype_rela },
9770 { "RELR", DT_RELR, DT_RELRSZ, reltype_relr },
9771 { "PLT", DT_JMPREL, DT_PLTRELSZ, reltype_unknown }
9772 };
9773
9774 static relocation_type
9775 rel_type_from_sh_type (unsigned int sh_type)
9776 {
9777 switch (sh_type)
9778 {
9779 case SHT_RELA: return reltype_rela;
9780 case SHT_REL: return reltype_rel;
9781 case SHT_RELR: return reltype_relr;
9782 default: return reltype_unknown;
9783 }
9784 }
9785
9786 static bool
9787 display_relocations (Elf_Internal_Shdr * section,
9788 Filedata * filedata,
9789 bool dump_reloc)
9790 {
9791 relocation_type rel_type = rel_type_from_sh_type (section->sh_type);
9792
9793 if (rel_type == reltype_unknown)
9794 return false;
9795
9796 uint64_t rel_size = section->sh_size;
9797
9798 if (rel_size == 0)
9799 return false;
9800
9801 if (dump_reloc)
9802 {
9803 if (filedata->is_separate)
9804 printf (_("\nIn linked file '%s' relocation section "),
9805 printable_string (filedata->file_name, 0));
9806 else
9807 printf (_("\nRelocation section "));
9808
9809 if (filedata->string_table == NULL)
9810 printf ("%d", section->sh_name);
9811 else
9812 printf ("'%s'", printable_section_name (filedata, section));
9813 }
9814
9815 uint64_t rel_offset = section->sh_offset;
9816 uint64_t num_rela = rel_size / section->sh_entsize;
9817 uint64_t num_reloc;
9818 uint64_t *relrs = NULL;
9819
9820 if (rel_type == reltype_relr)
9821 {
9822 /* Just stating the 'number of entries' in a RELR section can be
9823 misleading, since this is not the number of locations relocated, but
9824 the number of words in the compressed RELR format. So also provide
9825 the number of locations affected. */
9826
9827 num_reloc = count_relr_relocations (filedata, section, &relrs);
9828
9829 if (dump_reloc)
9830 {
9831 printf (_(" at offset %#" PRIx64), rel_offset);
9832 printf (ngettext (" contains %" PRIu64 " entry which relocates",
9833 " contains %" PRIu64 " entries which relocate",
9834 num_rela), num_rela);
9835 printf (ngettext (" %" PRIu64 " location:\n",
9836 " %" PRIu64 " locations:\n",
9837 num_reloc), num_reloc);
9838 }
9839 }
9840 else
9841 {
9842 num_reloc = num_rela;
9843 if (dump_reloc)
9844 printf (ngettext (" at offset %#" PRIx64
9845 " contains %" PRIu64 " entry:\n",
9846 " at offset %#" PRIx64
9847 " contains %" PRIu64 " entries:\n",
9848 num_rela),
9849 rel_offset, num_rela);
9850 }
9851
9852 update_all_relocations (num_reloc);
9853
9854 Elf_Internal_Shdr * symsec;
9855 Elf_Internal_Sym * symtab = NULL;
9856 uint64_t nsyms = 0;
9857 uint64_t strtablen = 0;
9858 char * strtab = NULL;
9859
9860 if (section->sh_link == 0
9861 || section->sh_link >= filedata->file_header.e_shnum)
9862 {
9863 /* Symbol data not available.
9864 This can happen, especially with RELR relocs.
9865 See if there is a .symtab section present.
9866 If so then use it. */
9867 symsec = find_section_by_name (filedata, ".symtab");
9868 }
9869 else
9870 {
9871 symsec = filedata->section_headers + section->sh_link;
9872
9873 if (symsec->sh_type != SHT_SYMTAB
9874 && symsec->sh_type != SHT_DYNSYM)
9875 {
9876 free (relrs);
9877 return false;
9878 }
9879 }
9880
9881 if (symsec != NULL
9882 && !get_symtab (filedata, symsec, &symtab, &nsyms, &strtab, &strtablen))
9883 {
9884 free (relrs);
9885 return false;
9886 }
9887
9888 bool res;
9889
9890 if (rel_type == reltype_relr)
9891 {
9892 res = dump_relr_relocations (filedata, section->sh_size,
9893 section->sh_entsize,
9894 section->sh_offset,
9895 relrs,
9896 symtab, nsyms, strtab, strtablen,
9897 dump_reloc);
9898 free (relrs);
9899 }
9900 else
9901 res = dump_relocations (filedata, rel_offset, rel_size,
9902 symtab, nsyms, strtab, strtablen,
9903 rel_type,
9904 symsec == NULL
9905 ? false : symsec->sh_type == SHT_DYNSYM,
9906 dump_reloc);
9907 free (strtab);
9908 free (symtab);
9909
9910 return res;
9911 }
9912
9913 /* Process the reloc section. */
9914
9915 static bool
9916 process_relocs (Filedata * filedata)
9917 {
9918 uint64_t rel_size;
9919 uint64_t rel_offset;
9920 unsigned int rel_entsz;
9921
9922 if (!do_reloc && !do_got_section_contents)
9923 return true;
9924
9925 if (do_using_dynamic)
9926 {
9927 relocation_type rel_type;
9928 const char * name;
9929 const char * entsz_name;
9930 bool has_dynamic_reloc;
9931 unsigned int i;
9932
9933 has_dynamic_reloc = false;
9934
9935 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
9936 {
9937 rel_size = filedata->dynamic_info[dynamic_relocations [i].size];
9938
9939 if (!rel_size)
9940 continue;
9941
9942 has_dynamic_reloc = true;
9943
9944 rel_type = dynamic_relocations [i].rel_type;
9945 name = dynamic_relocations [i].name;
9946 rel_offset = filedata->dynamic_info[dynamic_relocations [i].reloc];
9947
9948 if (rel_type == reltype_unknown)
9949 {
9950 if (dynamic_relocations [i].reloc != DT_JMPREL)
9951 abort ();
9952 switch (filedata->dynamic_info[DT_PLTREL])
9953 {
9954 case DT_REL:
9955 rel_type = reltype_rel;
9956 break;
9957 case DT_RELA:
9958 rel_type = reltype_rela;
9959 break;
9960 }
9961 }
9962
9963 switch (rel_type)
9964 {
9965 default:
9966 abort ();
9967 case reltype_rel:
9968 rel_entsz = filedata->dynamic_info[DT_RELENT];
9969 entsz_name = "DT_RELENT";
9970 break;
9971 case reltype_rela:
9972 rel_entsz = filedata->dynamic_info[DT_RELAENT];
9973 entsz_name = "DT_RELAENT";
9974 break;
9975 case reltype_relr:
9976 rel_entsz = filedata->dynamic_info[DT_RELRENT];
9977 entsz_name = "DT_RELRENT";
9978 break;
9979 }
9980
9981 if (do_reloc)
9982 {
9983 if (filedata->is_separate)
9984 printf
9985 (_("\nIn linked file '%s' section '%s' at offset"
9986 "%#" PRIx64 " contains %" PRId64 " bytes:\n"),
9987 filedata->file_name, name, rel_offset, rel_size);
9988 else
9989 printf
9990 (_("\n'%s' relocation section at offset %#" PRIx64
9991 " contains %" PRId64 " bytes:\n"),
9992 name, rel_offset, rel_size);
9993 }
9994
9995 if (rel_type == reltype_relr)
9996 dump_relr_relocations (filedata,
9997 filedata->dynamic_info[DT_RELRSZ],
9998 filedata->dynamic_info[DT_RELRENT],
9999 filedata->dynamic_info[DT_RELR],
10000 NULL,
10001 filedata->dynamic_symbols,
10002 filedata->num_dynamic_syms,
10003 filedata->dynamic_strings,
10004 filedata->dynamic_strings_length,
10005 do_reloc);
10006 else
10007 {
10008 if (rel_entsz == 0)
10009 {
10010 printf (_("<missing or corrupt dynamic tag: %s>\n"),
10011 entsz_name);
10012 continue;
10013 }
10014
10015 update_all_relocations (rel_size / rel_entsz);
10016
10017 dump_relocations (filedata,
10018 offset_from_vma (filedata, rel_offset,
10019 rel_size),
10020 rel_size,
10021 filedata->dynamic_symbols,
10022 filedata->num_dynamic_syms,
10023 filedata->dynamic_strings,
10024 filedata->dynamic_strings_length,
10025 rel_type, true /* is_dynamic */,
10026 do_reloc);
10027 }
10028 }
10029
10030 if (is_ia64_vms (filedata)
10031 && process_ia64_vms_dynamic_relocs (filedata))
10032 has_dynamic_reloc = true;
10033
10034 if (do_reloc && ! has_dynamic_reloc)
10035 {
10036 if (filedata->is_separate)
10037 printf (_("\nThere are no dynamic relocations in linked file '%s'.\n"),
10038 filedata->file_name);
10039 else
10040 printf (_("\nThere are no dynamic relocations in this file.\n"));
10041 }
10042 }
10043 else
10044 {
10045 Elf_Internal_Shdr * section;
10046 size_t i;
10047 bool found = false;
10048
10049 for (i = 0, section = filedata->section_headers;
10050 i < filedata->file_header.e_shnum;
10051 i++, section++)
10052 {
10053 if (display_relocations (section, filedata, do_reloc))
10054 found = true;
10055 }
10056
10057 if (do_reloc && ! found)
10058 {
10059 /* Users sometimes forget the -D option, so try to be helpful. */
10060 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
10061 {
10062 if (filedata->dynamic_info[dynamic_relocations [i].size])
10063 {
10064 if (filedata->is_separate)
10065 printf (_("\nThere are no static relocations in linked file '%s'."),
10066 filedata->file_name);
10067 else
10068 printf (_("\nThere are no static relocations in this file."));
10069 printf (_("\nTo see the dynamic relocations add --use-dynamic to the command line.\n"));
10070
10071 break;
10072 }
10073 }
10074 if (i == ARRAY_SIZE (dynamic_relocations))
10075 {
10076 if (filedata->is_separate)
10077 printf (_("\nThere are no relocations in linked file '%s'.\n"),
10078 filedata->file_name);
10079 else
10080 printf (_("\nThere are no relocations in this file.\n"));
10081 }
10082 }
10083 }
10084
10085 return true;
10086 }
10087
10088 /* An absolute address consists of a section and an offset. If the
10089 section is NULL, the offset itself is the address, otherwise, the
10090 address equals to LOAD_ADDRESS(section) + offset. */
10091
10092 struct absaddr
10093 {
10094 unsigned short section;
10095 uint64_t offset;
10096 };
10097
10098 /* Find the nearest symbol at or below ADDR. Returns the symbol
10099 name, if found, and the offset from the symbol to ADDR. */
10100
10101 static void
10102 find_symbol_for_address (Filedata *filedata,
10103 Elf_Internal_Sym *symtab,
10104 uint64_t nsyms,
10105 const char *strtab,
10106 uint64_t strtab_size,
10107 struct absaddr addr,
10108 const char **symname,
10109 uint64_t *offset)
10110 {
10111 uint64_t dist = 0x100000;
10112 Elf_Internal_Sym * sym;
10113 Elf_Internal_Sym * beg;
10114 Elf_Internal_Sym * end;
10115 Elf_Internal_Sym * best = NULL;
10116
10117 REMOVE_ARCH_BITS (addr.offset);
10118 beg = symtab;
10119 end = symtab + nsyms;
10120
10121 while (beg < end)
10122 {
10123 uint64_t value;
10124
10125 sym = beg + (end - beg) / 2;
10126
10127 value = sym->st_value;
10128 REMOVE_ARCH_BITS (value);
10129
10130 if (sym->st_name != 0
10131 && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
10132 && addr.offset >= value
10133 && addr.offset - value < dist)
10134 {
10135 best = sym;
10136 dist = addr.offset - value;
10137 if (!dist)
10138 break;
10139 }
10140
10141 if (addr.offset < value)
10142 end = sym;
10143 else
10144 beg = sym + 1;
10145 }
10146
10147 if (best)
10148 {
10149 *symname = (best->st_name >= strtab_size
10150 ? _("<corrupt>") : strtab + best->st_name);
10151 *offset = dist;
10152 return;
10153 }
10154
10155 *symname = NULL;
10156 *offset = addr.offset;
10157 }
10158
10159 /* Process the unwind section. */
10160
10161 #include "unwind-ia64.h"
10162
10163 struct ia64_unw_table_entry
10164 {
10165 struct absaddr start;
10166 struct absaddr end;
10167 struct absaddr info;
10168 };
10169
10170 struct ia64_unw_aux_info
10171 {
10172 struct ia64_unw_table_entry * table; /* Unwind table. */
10173 uint64_t table_len; /* Length of unwind table. */
10174 unsigned char * info; /* Unwind info. */
10175 uint64_t info_size; /* Size of unwind info. */
10176 uint64_t info_addr; /* Starting address of unwind info. */
10177 uint64_t seg_base; /* Starting address of segment. */
10178 Elf_Internal_Sym * symtab; /* The symbol table. */
10179 uint64_t nsyms; /* Number of symbols. */
10180 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
10181 uint64_t nfuns; /* Number of entries in funtab. */
10182 char * strtab; /* The string table. */
10183 uint64_t strtab_size; /* Size of string table. */
10184 };
10185
10186 static bool
10187 dump_ia64_unwind (Filedata * filedata, struct ia64_unw_aux_info * aux)
10188 {
10189 struct ia64_unw_table_entry * tp;
10190 size_t j, nfuns;
10191 int in_body;
10192 bool res = true;
10193
10194 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
10195 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
10196 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
10197 aux->funtab[nfuns++] = aux->symtab[j];
10198 aux->nfuns = nfuns;
10199 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
10200
10201 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
10202 {
10203 uint64_t stamp;
10204 uint64_t offset;
10205 const unsigned char * dp;
10206 const unsigned char * head;
10207 const unsigned char * end;
10208 const char * procname;
10209
10210 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
10211 aux->strtab_size, tp->start, &procname, &offset);
10212
10213 fputs ("\n<", stdout);
10214
10215 if (procname)
10216 {
10217 fputs (procname, stdout);
10218
10219 if (offset)
10220 printf ("+%" PRIx64, offset);
10221 }
10222
10223 fputs (">: [", stdout);
10224 print_vma (tp->start.offset, PREFIX_HEX);
10225 fputc ('-', stdout);
10226 print_vma (tp->end.offset, PREFIX_HEX);
10227 printf ("], info at +0x%" PRIx64 "\n",
10228 tp->info.offset - aux->seg_base);
10229
10230 /* PR 17531: file: 86232b32. */
10231 if (aux->info == NULL)
10232 continue;
10233
10234 offset = tp->info.offset;
10235 if (tp->info.section)
10236 {
10237 if (tp->info.section >= filedata->file_header.e_shnum)
10238 {
10239 warn (_("Invalid section %u in table entry %td\n"),
10240 tp->info.section, tp - aux->table);
10241 res = false;
10242 continue;
10243 }
10244 offset += filedata->section_headers[tp->info.section].sh_addr;
10245 }
10246 offset -= aux->info_addr;
10247 /* PR 17531: file: 0997b4d1. */
10248 if (offset >= aux->info_size
10249 || aux->info_size - offset < 8)
10250 {
10251 warn (_("Invalid offset %" PRIx64 " in table entry %td\n"),
10252 tp->info.offset, tp - aux->table);
10253 res = false;
10254 continue;
10255 }
10256
10257 head = aux->info + offset;
10258 stamp = byte_get ((unsigned char *) head, sizeof (stamp));
10259
10260 printf (" v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
10261 (unsigned) UNW_VER (stamp),
10262 (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
10263 UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
10264 UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
10265 (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
10266
10267 if (UNW_VER (stamp) != 1)
10268 {
10269 printf (_("\tUnknown version.\n"));
10270 continue;
10271 }
10272
10273 in_body = 0;
10274 end = head + 8 + eh_addr_size * UNW_LENGTH (stamp);
10275 /* PR 17531: file: 16ceda89. */
10276 if (end > aux->info + aux->info_size)
10277 end = aux->info + aux->info_size;
10278 for (dp = head + 8; dp < end;)
10279 dp = unw_decode (dp, in_body, & in_body, end);
10280 }
10281
10282 free (aux->funtab);
10283
10284 return res;
10285 }
10286
10287 static bool
10288 slurp_ia64_unwind_table (Filedata * filedata,
10289 struct ia64_unw_aux_info * aux,
10290 Elf_Internal_Shdr * sec)
10291 {
10292 uint64_t size, nrelas, i;
10293 Elf_Internal_Phdr * seg;
10294 struct ia64_unw_table_entry * tep;
10295 Elf_Internal_Shdr * relsec;
10296 Elf_Internal_Rela * rela;
10297 Elf_Internal_Rela * rp;
10298 unsigned char * table;
10299 unsigned char * tp;
10300 Elf_Internal_Sym * sym;
10301 const char * relname;
10302
10303 aux->table_len = 0;
10304
10305 /* First, find the starting address of the segment that includes
10306 this section: */
10307
10308 if (filedata->file_header.e_phnum)
10309 {
10310 if (! get_program_headers (filedata))
10311 return false;
10312
10313 for (seg = filedata->program_headers;
10314 seg < filedata->program_headers + filedata->file_header.e_phnum;
10315 ++seg)
10316 {
10317 if (seg->p_type != PT_LOAD)
10318 continue;
10319
10320 if (sec->sh_addr >= seg->p_vaddr
10321 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
10322 {
10323 aux->seg_base = seg->p_vaddr;
10324 break;
10325 }
10326 }
10327 }
10328
10329 /* Second, build the unwind table from the contents of the unwind section: */
10330 size = sec->sh_size;
10331 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
10332 _("unwind table"));
10333 if (!table)
10334 return false;
10335
10336 aux->table_len = size / (3 * eh_addr_size);
10337 aux->table = (struct ia64_unw_table_entry *)
10338 xcmalloc (aux->table_len, sizeof (aux->table[0]));
10339 tep = aux->table;
10340
10341 for (tp = table; tp <= table + size - (3 * eh_addr_size); ++tep)
10342 {
10343 tep->start.section = SHN_UNDEF;
10344 tep->end.section = SHN_UNDEF;
10345 tep->info.section = SHN_UNDEF;
10346 tep->start.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
10347 tep->end.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
10348 tep->info.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
10349 tep->start.offset += aux->seg_base;
10350 tep->end.offset += aux->seg_base;
10351 tep->info.offset += aux->seg_base;
10352 }
10353 free (table);
10354
10355 /* Third, apply any relocations to the unwind table: */
10356 for (relsec = filedata->section_headers;
10357 relsec < filedata->section_headers + filedata->file_header.e_shnum;
10358 ++relsec)
10359 {
10360 if (relsec->sh_type != SHT_RELA
10361 || relsec->sh_info >= filedata->file_header.e_shnum
10362 || filedata->section_headers + relsec->sh_info != sec)
10363 continue;
10364
10365 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
10366 & rela, & nrelas))
10367 {
10368 free (aux->table);
10369 aux->table = NULL;
10370 aux->table_len = 0;
10371 return false;
10372 }
10373
10374 for (rp = rela; rp < rela + nrelas; ++rp)
10375 {
10376 unsigned int sym_ndx;
10377 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
10378 relname = elf_ia64_reloc_type (r_type);
10379
10380 /* PR 17531: file: 9fa67536. */
10381 if (relname == NULL)
10382 {
10383 warn (_("Skipping unknown relocation type: %u\n"), r_type);
10384 continue;
10385 }
10386
10387 if (! startswith (relname, "R_IA64_SEGREL"))
10388 {
10389 warn (_("Skipping unexpected relocation type: %s\n"), relname);
10390 continue;
10391 }
10392
10393 i = rp->r_offset / (3 * eh_addr_size);
10394
10395 /* PR 17531: file: 5bc8d9bf. */
10396 if (i >= aux->table_len)
10397 {
10398 warn (_("Skipping reloc with overlarge offset: %#" PRIx64 "\n"),
10399 i);
10400 continue;
10401 }
10402
10403 sym_ndx = get_reloc_symindex (rp->r_info);
10404 if (sym_ndx >= aux->nsyms)
10405 {
10406 warn (_("Skipping reloc with invalid symbol index: %u\n"),
10407 sym_ndx);
10408 continue;
10409 }
10410 sym = aux->symtab + sym_ndx;
10411
10412 switch (rp->r_offset / eh_addr_size % 3)
10413 {
10414 case 0:
10415 aux->table[i].start.section = sym->st_shndx;
10416 aux->table[i].start.offset = rp->r_addend + sym->st_value;
10417 break;
10418 case 1:
10419 aux->table[i].end.section = sym->st_shndx;
10420 aux->table[i].end.offset = rp->r_addend + sym->st_value;
10421 break;
10422 case 2:
10423 aux->table[i].info.section = sym->st_shndx;
10424 aux->table[i].info.offset = rp->r_addend + sym->st_value;
10425 break;
10426 default:
10427 break;
10428 }
10429 }
10430
10431 free (rela);
10432 }
10433
10434 return true;
10435 }
10436
10437 static bool
10438 ia64_process_unwind (Filedata * filedata)
10439 {
10440 Elf_Internal_Shdr * sec;
10441 Elf_Internal_Shdr * unwsec = NULL;
10442 uint64_t i, unwcount = 0, unwstart = 0;
10443 struct ia64_unw_aux_info aux;
10444 bool res = true;
10445
10446 memset (& aux, 0, sizeof (aux));
10447
10448 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
10449 {
10450 if (sec->sh_type == SHT_SYMTAB)
10451 {
10452 if (aux.symtab)
10453 {
10454 error (_("Multiple symbol tables encountered\n"));
10455 free (aux.symtab);
10456 aux.symtab = NULL;
10457 free (aux.strtab);
10458 aux.strtab = NULL;
10459 }
10460 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
10461 &aux.strtab, &aux.strtab_size))
10462 return false;
10463 }
10464 else if (sec->sh_type == SHT_IA_64_UNWIND)
10465 unwcount++;
10466 }
10467
10468 if (!unwcount)
10469 printf (_("\nThere are no unwind sections in this file.\n"));
10470
10471 while (unwcount-- > 0)
10472 {
10473 const char *suffix;
10474 size_t len, len2;
10475
10476 for (i = unwstart, sec = filedata->section_headers + unwstart, unwsec = NULL;
10477 i < filedata->file_header.e_shnum; ++i, ++sec)
10478 if (sec->sh_type == SHT_IA_64_UNWIND)
10479 {
10480 unwsec = sec;
10481 break;
10482 }
10483 /* We have already counted the number of SHT_IA64_UNWIND
10484 sections so the loop above should never fail. */
10485 assert (unwsec != NULL);
10486
10487 unwstart = i + 1;
10488 len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
10489
10490 if ((unwsec->sh_flags & SHF_GROUP) != 0)
10491 {
10492 /* We need to find which section group it is in. */
10493 struct group_list * g;
10494
10495 if (filedata->section_headers_groups == NULL
10496 || filedata->section_headers_groups[i] == NULL)
10497 i = filedata->file_header.e_shnum;
10498 else
10499 {
10500 g = filedata->section_headers_groups[i]->root;
10501
10502 for (; g != NULL; g = g->next)
10503 {
10504 sec = filedata->section_headers + g->section_index;
10505
10506 if (section_name_valid (filedata, sec)
10507 && streq (section_name (filedata, sec),
10508 ELF_STRING_ia64_unwind_info))
10509 break;
10510 }
10511
10512 if (g == NULL)
10513 i = filedata->file_header.e_shnum;
10514 }
10515 }
10516 else if (section_name_valid (filedata, unwsec)
10517 && startswith (section_name (filedata, unwsec),
10518 ELF_STRING_ia64_unwind_once))
10519 {
10520 /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO. */
10521 len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
10522 suffix = section_name (filedata, unwsec) + len;
10523 for (i = 0, sec = filedata->section_headers;
10524 i < filedata->file_header.e_shnum;
10525 ++i, ++sec)
10526 if (section_name_valid (filedata, sec)
10527 && startswith (section_name (filedata, sec),
10528 ELF_STRING_ia64_unwind_info_once)
10529 && streq (section_name (filedata, sec) + len2, suffix))
10530 break;
10531 }
10532 else
10533 {
10534 /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
10535 .IA_64.unwind or BAR -> .IA_64.unwind_info. */
10536 len = sizeof (ELF_STRING_ia64_unwind) - 1;
10537 len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
10538 suffix = "";
10539 if (section_name_valid (filedata, unwsec)
10540 && startswith (section_name (filedata, unwsec),
10541 ELF_STRING_ia64_unwind))
10542 suffix = section_name (filedata, unwsec) + len;
10543 for (i = 0, sec = filedata->section_headers;
10544 i < filedata->file_header.e_shnum;
10545 ++i, ++sec)
10546 if (section_name_valid (filedata, sec)
10547 && startswith (section_name (filedata, sec),
10548 ELF_STRING_ia64_unwind_info)
10549 && streq (section_name (filedata, sec) + len2, suffix))
10550 break;
10551 }
10552
10553 if (i == filedata->file_header.e_shnum)
10554 {
10555 printf (_("\nCould not find unwind info section for "));
10556
10557 if (filedata->string_table == NULL)
10558 printf ("%d", unwsec->sh_name);
10559 else
10560 printf ("'%s'", printable_section_name (filedata, unwsec));
10561 }
10562 else
10563 {
10564 aux.info_addr = sec->sh_addr;
10565 aux.info = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1,
10566 sec->sh_size,
10567 _("unwind info"));
10568 aux.info_size = aux.info == NULL ? 0 : sec->sh_size;
10569
10570 printf (_("\nUnwind section "));
10571
10572 if (filedata->string_table == NULL)
10573 printf ("%d", unwsec->sh_name);
10574 else
10575 printf ("'%s'", printable_section_name (filedata, unwsec));
10576
10577 printf (_(" at offset %#" PRIx64 " contains %" PRIu64 " entries:\n"),
10578 unwsec->sh_offset,
10579 unwsec->sh_size / (3 * eh_addr_size));
10580
10581 if (slurp_ia64_unwind_table (filedata, & aux, unwsec)
10582 && aux.table_len > 0)
10583 dump_ia64_unwind (filedata, & aux);
10584
10585 free ((char *) aux.table);
10586 free ((char *) aux.info);
10587 aux.table = NULL;
10588 aux.info = NULL;
10589 }
10590 }
10591
10592 free (aux.symtab);
10593 free ((char *) aux.strtab);
10594
10595 return res;
10596 }
10597
10598 struct hppa_unw_table_entry
10599 {
10600 struct absaddr start;
10601 struct absaddr end;
10602 unsigned int Cannot_unwind:1; /* 0 */
10603 unsigned int Millicode:1; /* 1 */
10604 unsigned int Millicode_save_sr0:1; /* 2 */
10605 unsigned int Region_description:2; /* 3..4 */
10606 unsigned int reserved1:1; /* 5 */
10607 unsigned int Entry_SR:1; /* 6 */
10608 unsigned int Entry_FR:4; /* Number saved 7..10 */
10609 unsigned int Entry_GR:5; /* Number saved 11..15 */
10610 unsigned int Args_stored:1; /* 16 */
10611 unsigned int Variable_Frame:1; /* 17 */
10612 unsigned int Separate_Package_Body:1; /* 18 */
10613 unsigned int Frame_Extension_Millicode:1; /* 19 */
10614 unsigned int Stack_Overflow_Check:1; /* 20 */
10615 unsigned int Two_Instruction_SP_Increment:1; /* 21 */
10616 unsigned int Ada_Region:1; /* 22 */
10617 unsigned int cxx_info:1; /* 23 */
10618 unsigned int cxx_try_catch:1; /* 24 */
10619 unsigned int sched_entry_seq:1; /* 25 */
10620 unsigned int reserved2:1; /* 26 */
10621 unsigned int Save_SP:1; /* 27 */
10622 unsigned int Save_RP:1; /* 28 */
10623 unsigned int Save_MRP_in_frame:1; /* 29 */
10624 unsigned int extn_ptr_defined:1; /* 30 */
10625 unsigned int Cleanup_defined:1; /* 31 */
10626
10627 unsigned int MPE_XL_interrupt_marker:1; /* 0 */
10628 unsigned int HP_UX_interrupt_marker:1; /* 1 */
10629 unsigned int Large_frame:1; /* 2 */
10630 unsigned int Pseudo_SP_Set:1; /* 3 */
10631 unsigned int reserved4:1; /* 4 */
10632 unsigned int Total_frame_size:27; /* 5..31 */
10633 };
10634
10635 struct hppa_unw_aux_info
10636 {
10637 struct hppa_unw_table_entry * table; /* Unwind table. */
10638 uint64_t table_len; /* Length of unwind table. */
10639 uint64_t seg_base; /* Starting address of segment. */
10640 Elf_Internal_Sym * symtab; /* The symbol table. */
10641 uint64_t nsyms; /* Number of symbols. */
10642 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
10643 uint64_t nfuns; /* Number of entries in funtab. */
10644 char * strtab; /* The string table. */
10645 uint64_t strtab_size; /* Size of string table. */
10646 };
10647
10648 static bool
10649 dump_hppa_unwind (Filedata * filedata, struct hppa_unw_aux_info * aux)
10650 {
10651 struct hppa_unw_table_entry * tp;
10652 uint64_t j, nfuns;
10653 bool res = true;
10654
10655 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
10656 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
10657 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
10658 aux->funtab[nfuns++] = aux->symtab[j];
10659 aux->nfuns = nfuns;
10660 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
10661
10662 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
10663 {
10664 uint64_t offset;
10665 const char * procname;
10666
10667 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
10668 aux->strtab_size, tp->start, &procname,
10669 &offset);
10670
10671 fputs ("\n<", stdout);
10672
10673 if (procname)
10674 {
10675 fputs (procname, stdout);
10676
10677 if (offset)
10678 printf ("+%" PRIx64, offset);
10679 }
10680
10681 fputs (">: [", stdout);
10682 print_vma (tp->start.offset, PREFIX_HEX);
10683 fputc ('-', stdout);
10684 print_vma (tp->end.offset, PREFIX_HEX);
10685 printf ("]\n\t");
10686
10687 #define PF(_m) if (tp->_m) printf (#_m " ");
10688 #define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
10689 PF(Cannot_unwind);
10690 PF(Millicode);
10691 PF(Millicode_save_sr0);
10692 /* PV(Region_description); */
10693 PF(Entry_SR);
10694 PV(Entry_FR);
10695 PV(Entry_GR);
10696 PF(Args_stored);
10697 PF(Variable_Frame);
10698 PF(Separate_Package_Body);
10699 PF(Frame_Extension_Millicode);
10700 PF(Stack_Overflow_Check);
10701 PF(Two_Instruction_SP_Increment);
10702 PF(Ada_Region);
10703 PF(cxx_info);
10704 PF(cxx_try_catch);
10705 PF(sched_entry_seq);
10706 PF(Save_SP);
10707 PF(Save_RP);
10708 PF(Save_MRP_in_frame);
10709 PF(extn_ptr_defined);
10710 PF(Cleanup_defined);
10711 PF(MPE_XL_interrupt_marker);
10712 PF(HP_UX_interrupt_marker);
10713 PF(Large_frame);
10714 PF(Pseudo_SP_Set);
10715 PV(Total_frame_size);
10716 #undef PF
10717 #undef PV
10718 }
10719
10720 printf ("\n");
10721
10722 free (aux->funtab);
10723
10724 return res;
10725 }
10726
10727 static bool
10728 slurp_hppa_unwind_table (Filedata * filedata,
10729 struct hppa_unw_aux_info * aux,
10730 Elf_Internal_Shdr * sec)
10731 {
10732 uint64_t size, unw_ent_size, nentries, nrelas, i;
10733 Elf_Internal_Phdr * seg;
10734 struct hppa_unw_table_entry * tep;
10735 Elf_Internal_Shdr * relsec;
10736 Elf_Internal_Rela * rela;
10737 Elf_Internal_Rela * rp;
10738 unsigned char * table;
10739 unsigned char * tp;
10740 Elf_Internal_Sym * sym;
10741 const char * relname;
10742
10743 /* First, find the starting address of the segment that includes
10744 this section. */
10745 if (filedata->file_header.e_phnum)
10746 {
10747 if (! get_program_headers (filedata))
10748 return false;
10749
10750 for (seg = filedata->program_headers;
10751 seg < filedata->program_headers + filedata->file_header.e_phnum;
10752 ++seg)
10753 {
10754 if (seg->p_type != PT_LOAD)
10755 continue;
10756
10757 if (sec->sh_addr >= seg->p_vaddr
10758 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
10759 {
10760 aux->seg_base = seg->p_vaddr;
10761 break;
10762 }
10763 }
10764 }
10765
10766 /* Second, build the unwind table from the contents of the unwind
10767 section. */
10768 size = sec->sh_size;
10769 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
10770 _("unwind table"));
10771 if (!table)
10772 return false;
10773
10774 unw_ent_size = 16;
10775 nentries = size / unw_ent_size;
10776 size = unw_ent_size * nentries;
10777
10778 aux->table_len = nentries;
10779 tep = aux->table = (struct hppa_unw_table_entry *)
10780 xcmalloc (nentries, sizeof (aux->table[0]));
10781
10782 for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
10783 {
10784 unsigned int tmp1, tmp2;
10785
10786 tep->start.section = SHN_UNDEF;
10787 tep->end.section = SHN_UNDEF;
10788
10789 tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
10790 tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
10791 tmp1 = byte_get ((unsigned char *) tp + 8, 4);
10792 tmp2 = byte_get ((unsigned char *) tp + 12, 4);
10793
10794 tep->start.offset += aux->seg_base;
10795 tep->end.offset += aux->seg_base;
10796
10797 tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
10798 tep->Millicode = (tmp1 >> 30) & 0x1;
10799 tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
10800 tep->Region_description = (tmp1 >> 27) & 0x3;
10801 tep->reserved1 = (tmp1 >> 26) & 0x1;
10802 tep->Entry_SR = (tmp1 >> 25) & 0x1;
10803 tep->Entry_FR = (tmp1 >> 21) & 0xf;
10804 tep->Entry_GR = (tmp1 >> 16) & 0x1f;
10805 tep->Args_stored = (tmp1 >> 15) & 0x1;
10806 tep->Variable_Frame = (tmp1 >> 14) & 0x1;
10807 tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
10808 tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
10809 tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
10810 tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
10811 tep->Ada_Region = (tmp1 >> 9) & 0x1;
10812 tep->cxx_info = (tmp1 >> 8) & 0x1;
10813 tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
10814 tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
10815 tep->reserved2 = (tmp1 >> 5) & 0x1;
10816 tep->Save_SP = (tmp1 >> 4) & 0x1;
10817 tep->Save_RP = (tmp1 >> 3) & 0x1;
10818 tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
10819 tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
10820 tep->Cleanup_defined = tmp1 & 0x1;
10821
10822 tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
10823 tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
10824 tep->Large_frame = (tmp2 >> 29) & 0x1;
10825 tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
10826 tep->reserved4 = (tmp2 >> 27) & 0x1;
10827 tep->Total_frame_size = tmp2 & 0x7ffffff;
10828 }
10829 free (table);
10830
10831 /* Third, apply any relocations to the unwind table. */
10832 for (relsec = filedata->section_headers;
10833 relsec < filedata->section_headers + filedata->file_header.e_shnum;
10834 ++relsec)
10835 {
10836 if (relsec->sh_type != SHT_RELA
10837 || relsec->sh_info >= filedata->file_header.e_shnum
10838 || filedata->section_headers + relsec->sh_info != sec)
10839 continue;
10840
10841 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
10842 & rela, & nrelas))
10843 return false;
10844
10845 for (rp = rela; rp < rela + nrelas; ++rp)
10846 {
10847 unsigned int sym_ndx;
10848 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
10849 relname = elf_hppa_reloc_type (r_type);
10850
10851 if (relname == NULL)
10852 {
10853 warn (_("Skipping unknown relocation type: %u\n"), r_type);
10854 continue;
10855 }
10856
10857 /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64. */
10858 if (! startswith (relname, "R_PARISC_SEGREL"))
10859 {
10860 warn (_("Skipping unexpected relocation type: %s\n"), relname);
10861 continue;
10862 }
10863
10864 i = rp->r_offset / unw_ent_size;
10865 if (i >= aux->table_len)
10866 {
10867 warn (_("Skipping reloc with overlarge offset: %#" PRIx64 "\n"),
10868 i);
10869 continue;
10870 }
10871
10872 sym_ndx = get_reloc_symindex (rp->r_info);
10873 if (sym_ndx >= aux->nsyms)
10874 {
10875 warn (_("Skipping reloc with invalid symbol index: %u\n"),
10876 sym_ndx);
10877 continue;
10878 }
10879 sym = aux->symtab + sym_ndx;
10880
10881 switch ((rp->r_offset % unw_ent_size) / 4)
10882 {
10883 case 0:
10884 aux->table[i].start.section = sym->st_shndx;
10885 aux->table[i].start.offset = sym->st_value + rp->r_addend;
10886 break;
10887 case 1:
10888 aux->table[i].end.section = sym->st_shndx;
10889 aux->table[i].end.offset = sym->st_value + rp->r_addend;
10890 break;
10891 default:
10892 break;
10893 }
10894 }
10895
10896 free (rela);
10897 }
10898
10899 return true;
10900 }
10901
10902 static bool
10903 hppa_process_unwind (Filedata * filedata)
10904 {
10905 struct hppa_unw_aux_info aux;
10906 Elf_Internal_Shdr * unwsec = NULL;
10907 Elf_Internal_Shdr * sec;
10908 size_t i;
10909 bool res = true;
10910
10911 if (filedata->string_table == NULL)
10912 return false;
10913
10914 memset (& aux, 0, sizeof (aux));
10915
10916 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
10917 {
10918 if (sec->sh_type == SHT_SYMTAB)
10919 {
10920 if (aux.symtab)
10921 {
10922 error (_("Multiple symbol tables encountered\n"));
10923 free (aux.symtab);
10924 aux.symtab = NULL;
10925 free (aux.strtab);
10926 aux.strtab = NULL;
10927 }
10928 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
10929 &aux.strtab, &aux.strtab_size))
10930 return false;
10931 }
10932 else if (section_name_valid (filedata, sec)
10933 && streq (section_name (filedata, sec), ".PARISC.unwind"))
10934 unwsec = sec;
10935 }
10936
10937 if (!unwsec)
10938 printf (_("\nThere are no unwind sections in this file.\n"));
10939
10940 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
10941 {
10942 if (section_name_valid (filedata, sec)
10943 && streq (section_name (filedata, sec), ".PARISC.unwind"))
10944 {
10945 uint64_t num_unwind = sec->sh_size / 16;
10946
10947 printf (ngettext ("\nUnwind section '%s' at offset %#" PRIx64 " "
10948 "contains %" PRIu64 " entry:\n",
10949 "\nUnwind section '%s' at offset %#" PRIx64 " "
10950 "contains %" PRIu64 " entries:\n",
10951 num_unwind),
10952 printable_section_name (filedata, sec),
10953 sec->sh_offset,
10954 num_unwind);
10955
10956 if (! slurp_hppa_unwind_table (filedata, &aux, sec))
10957 res = false;
10958
10959 if (res && aux.table_len > 0)
10960 {
10961 if (! dump_hppa_unwind (filedata, &aux))
10962 res = false;
10963 }
10964
10965 free ((char *) aux.table);
10966 aux.table = NULL;
10967 }
10968 }
10969
10970 free (aux.symtab);
10971 free ((char *) aux.strtab);
10972
10973 return res;
10974 }
10975
10976 struct arm_section
10977 {
10978 unsigned char * data; /* The unwind data. */
10979 Elf_Internal_Shdr * sec; /* The cached unwind section header. */
10980 Elf_Internal_Rela * rela; /* The cached relocations for this section. */
10981 uint64_t nrelas; /* The number of relocations. */
10982 unsigned int rel_type; /* REL or RELA ? */
10983 Elf_Internal_Rela * next_rela; /* Cyclic pointer to the next reloc to process. */
10984 };
10985
10986 struct arm_unw_aux_info
10987 {
10988 Filedata * filedata; /* The file containing the unwind sections. */
10989 Elf_Internal_Sym * symtab; /* The file's symbol table. */
10990 uint64_t nsyms; /* Number of symbols. */
10991 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
10992 uint64_t nfuns; /* Number of these symbols. */
10993 char * strtab; /* The file's string table. */
10994 uint64_t strtab_size; /* Size of string table. */
10995 };
10996
10997 static const char *
10998 arm_print_vma_and_name (Filedata * filedata,
10999 struct arm_unw_aux_info * aux,
11000 uint64_t fn,
11001 struct absaddr addr)
11002 {
11003 const char *procname;
11004 uint64_t sym_offset;
11005
11006 if (addr.section == SHN_UNDEF)
11007 addr.offset = fn;
11008
11009 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
11010 aux->strtab_size, addr, &procname,
11011 &sym_offset);
11012
11013 print_vma (fn, PREFIX_HEX);
11014
11015 if (procname)
11016 {
11017 fputs (" <", stdout);
11018 fputs (procname, stdout);
11019
11020 if (sym_offset)
11021 printf ("+0x%" PRIx64, sym_offset);
11022 fputc ('>', stdout);
11023 }
11024
11025 return procname;
11026 }
11027
11028 static void
11029 arm_free_section (struct arm_section *arm_sec)
11030 {
11031 free (arm_sec->data);
11032 free (arm_sec->rela);
11033 }
11034
11035 /* 1) If SEC does not match the one cached in ARM_SEC, then free the current
11036 cached section and install SEC instead.
11037 2) Locate the 32-bit word at WORD_OFFSET in unwind section SEC
11038 and return its valued in * WORDP, relocating if necessary.
11039 3) Update the NEXT_RELA field in ARM_SEC and store the section index and
11040 relocation's offset in ADDR.
11041 4) If SYM_NAME is non-NULL and a relocation was applied, record the offset
11042 into the string table of the symbol associated with the reloc. If no
11043 reloc was applied store -1 there.
11044 5) Return TRUE upon success, FALSE otherwise. */
11045
11046 static bool
11047 get_unwind_section_word (Filedata * filedata,
11048 struct arm_unw_aux_info * aux,
11049 struct arm_section * arm_sec,
11050 Elf_Internal_Shdr * sec,
11051 uint64_t word_offset,
11052 unsigned int * wordp,
11053 struct absaddr * addr,
11054 uint64_t * sym_name)
11055 {
11056 Elf_Internal_Rela *rp;
11057 Elf_Internal_Sym *sym;
11058 const char * relname;
11059 unsigned int word;
11060 bool wrapped;
11061
11062 if (sec == NULL || arm_sec == NULL)
11063 return false;
11064
11065 addr->section = SHN_UNDEF;
11066 addr->offset = 0;
11067
11068 if (sym_name != NULL)
11069 *sym_name = (uint64_t) -1;
11070
11071 /* If necessary, update the section cache. */
11072 if (sec != arm_sec->sec)
11073 {
11074 Elf_Internal_Shdr *relsec;
11075
11076 arm_free_section (arm_sec);
11077
11078 arm_sec->sec = sec;
11079 arm_sec->data = get_data (NULL, aux->filedata, sec->sh_offset, 1,
11080 sec->sh_size, _("unwind data"));
11081 arm_sec->rela = NULL;
11082 arm_sec->nrelas = 0;
11083
11084 for (relsec = filedata->section_headers;
11085 relsec < filedata->section_headers + filedata->file_header.e_shnum;
11086 ++relsec)
11087 {
11088 if (relsec->sh_info >= filedata->file_header.e_shnum
11089 || filedata->section_headers + relsec->sh_info != sec
11090 /* PR 15745: Check the section type as well. */
11091 || (relsec->sh_type != SHT_REL
11092 && relsec->sh_type != SHT_RELA))
11093 continue;
11094
11095 arm_sec->rel_type = relsec->sh_type;
11096 if (relsec->sh_type == SHT_REL)
11097 {
11098 if (!slurp_rel_relocs (aux->filedata, relsec->sh_offset,
11099 relsec->sh_size,
11100 & arm_sec->rela, & arm_sec->nrelas))
11101 return false;
11102 }
11103 else /* relsec->sh_type == SHT_RELA */
11104 {
11105 if (!slurp_rela_relocs (aux->filedata, relsec->sh_offset,
11106 relsec->sh_size,
11107 & arm_sec->rela, & arm_sec->nrelas))
11108 return false;
11109 }
11110 break;
11111 }
11112
11113 arm_sec->next_rela = arm_sec->rela;
11114 }
11115
11116 /* If there is no unwind data we can do nothing. */
11117 if (arm_sec->data == NULL)
11118 return false;
11119
11120 /* If the offset is invalid then fail. */
11121 if (/* PR 21343 *//* PR 18879 */
11122 sec->sh_size < 4
11123 || word_offset > sec->sh_size - 4)
11124 return false;
11125
11126 /* Get the word at the required offset. */
11127 word = byte_get (arm_sec->data + word_offset, 4);
11128
11129 /* PR 17531: file: id:000001,src:001266+003044,op:splice,rep:128. */
11130 if (arm_sec->rela == NULL)
11131 {
11132 * wordp = word;
11133 return true;
11134 }
11135
11136 /* Look through the relocs to find the one that applies to the provided offset. */
11137 wrapped = false;
11138 for (rp = arm_sec->next_rela; rp != arm_sec->rela + arm_sec->nrelas; rp++)
11139 {
11140 uint64_t prelval, offset;
11141
11142 if (rp->r_offset > word_offset && !wrapped)
11143 {
11144 rp = arm_sec->rela;
11145 wrapped = true;
11146 }
11147 if (rp->r_offset > word_offset)
11148 break;
11149
11150 if (rp->r_offset & 3)
11151 {
11152 warn (_("Skipping unexpected relocation at offset %#" PRIx64 "\n"),
11153 rp->r_offset);
11154 continue;
11155 }
11156
11157 if (rp->r_offset < word_offset)
11158 continue;
11159
11160 /* PR 17531: file: 027-161405-0.004 */
11161 if (aux->symtab == NULL)
11162 continue;
11163
11164 if (arm_sec->rel_type == SHT_REL)
11165 {
11166 offset = word & 0x7fffffff;
11167 if (offset & 0x40000000)
11168 offset |= ~ (uint64_t) 0x7fffffff;
11169 }
11170 else if (arm_sec->rel_type == SHT_RELA)
11171 offset = rp->r_addend;
11172 else
11173 {
11174 error (_("Unknown section relocation type %d encountered\n"),
11175 arm_sec->rel_type);
11176 break;
11177 }
11178
11179 /* PR 17531 file: 027-1241568-0.004. */
11180 if (ELF32_R_SYM (rp->r_info) >= aux->nsyms)
11181 {
11182 error (_("Bad symbol index in unwind relocation "
11183 "(%" PRIu64 " > %" PRIu64 ")\n"),
11184 ELF32_R_SYM (rp->r_info), aux->nsyms);
11185 break;
11186 }
11187
11188 sym = aux->symtab + ELF32_R_SYM (rp->r_info);
11189 offset += sym->st_value;
11190 prelval = offset - (arm_sec->sec->sh_addr + rp->r_offset);
11191
11192 /* Check that we are processing the expected reloc type. */
11193 if (filedata->file_header.e_machine == EM_ARM)
11194 {
11195 relname = elf_arm_reloc_type (ELF32_R_TYPE (rp->r_info));
11196 if (relname == NULL)
11197 {
11198 warn (_("Skipping unknown ARM relocation type: %d\n"),
11199 (int) ELF32_R_TYPE (rp->r_info));
11200 continue;
11201 }
11202
11203 if (streq (relname, "R_ARM_NONE"))
11204 continue;
11205
11206 if (! streq (relname, "R_ARM_PREL31"))
11207 {
11208 warn (_("Skipping unexpected ARM relocation type %s\n"), relname);
11209 continue;
11210 }
11211 }
11212 else if (filedata->file_header.e_machine == EM_TI_C6000)
11213 {
11214 relname = elf_tic6x_reloc_type (ELF32_R_TYPE (rp->r_info));
11215 if (relname == NULL)
11216 {
11217 warn (_("Skipping unknown C6000 relocation type: %d\n"),
11218 (int) ELF32_R_TYPE (rp->r_info));
11219 continue;
11220 }
11221
11222 if (streq (relname, "R_C6000_NONE"))
11223 continue;
11224
11225 if (! streq (relname, "R_C6000_PREL31"))
11226 {
11227 warn (_("Skipping unexpected C6000 relocation type %s\n"), relname);
11228 continue;
11229 }
11230
11231 prelval >>= 1;
11232 }
11233 else
11234 {
11235 /* This function currently only supports ARM and TI unwinders. */
11236 warn (_("Only TI and ARM unwinders are currently supported\n"));
11237 break;
11238 }
11239
11240 word = (word & ~ (uint64_t) 0x7fffffff) | (prelval & 0x7fffffff);
11241 addr->section = sym->st_shndx;
11242 addr->offset = offset;
11243
11244 if (sym_name)
11245 * sym_name = sym->st_name;
11246 break;
11247 }
11248
11249 *wordp = word;
11250 arm_sec->next_rela = rp;
11251
11252 return true;
11253 }
11254
11255 static const char *tic6x_unwind_regnames[16] =
11256 {
11257 "A15", "B15", "B14", "B13", "B12", "B11", "B10", "B3",
11258 "A14", "A13", "A12", "A11", "A10",
11259 "[invalid reg 13]", "[invalid reg 14]", "[invalid reg 15]"
11260 };
11261
11262 static void
11263 decode_tic6x_unwind_regmask (unsigned int mask)
11264 {
11265 int i;
11266
11267 for (i = 12; mask; mask >>= 1, i--)
11268 {
11269 if (mask & 1)
11270 {
11271 fputs (tic6x_unwind_regnames[i], stdout);
11272 if (mask > 1)
11273 fputs (", ", stdout);
11274 }
11275 }
11276 }
11277
11278 #define ADVANCE \
11279 if (remaining == 0 && more_words) \
11280 { \
11281 data_offset += 4; \
11282 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, \
11283 data_offset, & word, & addr, NULL)) \
11284 return false; \
11285 remaining = 4; \
11286 more_words--; \
11287 } \
11288
11289 #define GET_OP(OP) \
11290 ADVANCE; \
11291 if (remaining) \
11292 { \
11293 remaining--; \
11294 (OP) = word >> 24; \
11295 word <<= 8; \
11296 } \
11297 else \
11298 { \
11299 printf (_("[Truncated opcode]\n")); \
11300 return false; \
11301 } \
11302 printf ("0x%02x ", OP)
11303
11304 static bool
11305 decode_arm_unwind_bytecode (Filedata * filedata,
11306 struct arm_unw_aux_info * aux,
11307 unsigned int word,
11308 unsigned int remaining,
11309 unsigned int more_words,
11310 uint64_t data_offset,
11311 Elf_Internal_Shdr * data_sec,
11312 struct arm_section * data_arm_sec)
11313 {
11314 struct absaddr addr;
11315 bool res = true;
11316
11317 /* Decode the unwinding instructions. */
11318 while (1)
11319 {
11320 unsigned int op, op2;
11321
11322 ADVANCE;
11323 if (remaining == 0)
11324 break;
11325 remaining--;
11326 op = word >> 24;
11327 word <<= 8;
11328
11329 printf (" 0x%02x ", op);
11330
11331 if ((op & 0xc0) == 0x00)
11332 {
11333 int offset = ((op & 0x3f) << 2) + 4;
11334
11335 printf (" vsp = vsp + %d", offset);
11336 }
11337 else if ((op & 0xc0) == 0x40)
11338 {
11339 int offset = ((op & 0x3f) << 2) + 4;
11340
11341 printf (" vsp = vsp - %d", offset);
11342 }
11343 else if ((op & 0xf0) == 0x80)
11344 {
11345 GET_OP (op2);
11346 if (op == 0x80 && op2 == 0)
11347 printf (_("Refuse to unwind"));
11348 else
11349 {
11350 unsigned int mask = ((op & 0x0f) << 8) | op2;
11351 bool first = true;
11352 int i;
11353
11354 printf ("pop {");
11355 for (i = 0; i < 12; i++)
11356 if (mask & (1 << i))
11357 {
11358 if (first)
11359 first = false;
11360 else
11361 printf (", ");
11362 printf ("r%d", 4 + i);
11363 }
11364 printf ("}");
11365 }
11366 }
11367 else if ((op & 0xf0) == 0x90)
11368 {
11369 if (op == 0x9d || op == 0x9f)
11370 printf (_(" [Reserved]"));
11371 else
11372 printf (" vsp = r%d", op & 0x0f);
11373 }
11374 else if ((op & 0xf0) == 0xa0)
11375 {
11376 int end = 4 + (op & 0x07);
11377 bool first = true;
11378 int i;
11379
11380 printf (" pop {");
11381 for (i = 4; i <= end; i++)
11382 {
11383 if (first)
11384 first = false;
11385 else
11386 printf (", ");
11387 printf ("r%d", i);
11388 }
11389 if (op & 0x08)
11390 {
11391 if (!first)
11392 printf (", ");
11393 printf ("r14");
11394 }
11395 printf ("}");
11396 }
11397 else if (op == 0xb0)
11398 printf (_(" finish"));
11399 else if (op == 0xb1)
11400 {
11401 GET_OP (op2);
11402 if (op2 == 0 || (op2 & 0xf0) != 0)
11403 printf (_("[Spare]"));
11404 else
11405 {
11406 unsigned int mask = op2 & 0x0f;
11407 bool first = true;
11408 int i;
11409
11410 printf ("pop {");
11411 for (i = 0; i < 12; i++)
11412 if (mask & (1 << i))
11413 {
11414 if (first)
11415 first = false;
11416 else
11417 printf (", ");
11418 printf ("r%d", i);
11419 }
11420 printf ("}");
11421 }
11422 }
11423 else if (op == 0xb2)
11424 {
11425 unsigned char buf[9];
11426 unsigned int i, len;
11427 uint64_t offset;
11428
11429 for (i = 0; i < sizeof (buf); i++)
11430 {
11431 GET_OP (buf[i]);
11432 if ((buf[i] & 0x80) == 0)
11433 break;
11434 }
11435 if (i == sizeof (buf))
11436 {
11437 error (_("corrupt change to vsp\n"));
11438 res = false;
11439 }
11440 else
11441 {
11442 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
11443 assert (len == i + 1);
11444 offset = offset * 4 + 0x204;
11445 printf ("vsp = vsp + %" PRId64, offset);
11446 }
11447 }
11448 else if (op == 0xb3 || op == 0xc8 || op == 0xc9)
11449 {
11450 unsigned int first, last;
11451
11452 GET_OP (op2);
11453 first = op2 >> 4;
11454 last = op2 & 0x0f;
11455 if (op == 0xc8)
11456 first = first + 16;
11457 printf ("pop {D%d", first);
11458 if (last)
11459 printf ("-D%d", first + last);
11460 printf ("}");
11461 }
11462 else if (op == 0xb4)
11463 printf (_(" pop {ra_auth_code}"));
11464 else if (op == 0xb5)
11465 printf (_(" vsp as modifier for PAC validation"));
11466 else if ((op & 0xf8) == 0xb8 || (op & 0xf8) == 0xd0)
11467 {
11468 unsigned int count = op & 0x07;
11469
11470 printf ("pop {D8");
11471 if (count)
11472 printf ("-D%d", 8 + count);
11473 printf ("}");
11474 }
11475 else if (op >= 0xc0 && op <= 0xc5)
11476 {
11477 unsigned int count = op & 0x07;
11478
11479 printf (" pop {wR10");
11480 if (count)
11481 printf ("-wR%d", 10 + count);
11482 printf ("}");
11483 }
11484 else if (op == 0xc6)
11485 {
11486 unsigned int first, last;
11487
11488 GET_OP (op2);
11489 first = op2 >> 4;
11490 last = op2 & 0x0f;
11491 printf ("pop {wR%d", first);
11492 if (last)
11493 printf ("-wR%d", first + last);
11494 printf ("}");
11495 }
11496 else if (op == 0xc7)
11497 {
11498 GET_OP (op2);
11499 if (op2 == 0 || (op2 & 0xf0) != 0)
11500 printf (_("[Spare]"));
11501 else
11502 {
11503 unsigned int mask = op2 & 0x0f;
11504 bool first = true;
11505 int i;
11506
11507 printf ("pop {");
11508 for (i = 0; i < 4; i++)
11509 if (mask & (1 << i))
11510 {
11511 if (first)
11512 first = false;
11513 else
11514 printf (", ");
11515 printf ("wCGR%d", i);
11516 }
11517 printf ("}");
11518 }
11519 }
11520 else
11521 {
11522 printf (_(" [unsupported opcode]"));
11523 res = false;
11524 }
11525
11526 printf ("\n");
11527 }
11528
11529 return res;
11530 }
11531
11532 static bool
11533 decode_tic6x_unwind_bytecode (Filedata * filedata,
11534 struct arm_unw_aux_info * aux,
11535 unsigned int word,
11536 unsigned int remaining,
11537 unsigned int more_words,
11538 uint64_t data_offset,
11539 Elf_Internal_Shdr * data_sec,
11540 struct arm_section * data_arm_sec)
11541 {
11542 struct absaddr addr;
11543
11544 /* Decode the unwinding instructions. */
11545 while (1)
11546 {
11547 unsigned int op, op2;
11548
11549 ADVANCE;
11550 if (remaining == 0)
11551 break;
11552 remaining--;
11553 op = word >> 24;
11554 word <<= 8;
11555
11556 printf (" 0x%02x ", op);
11557
11558 if ((op & 0xc0) == 0x00)
11559 {
11560 int offset = ((op & 0x3f) << 3) + 8;
11561 printf (" sp = sp + %d", offset);
11562 }
11563 else if ((op & 0xc0) == 0x80)
11564 {
11565 GET_OP (op2);
11566 if (op == 0x80 && op2 == 0)
11567 printf (_("Refuse to unwind"));
11568 else
11569 {
11570 unsigned int mask = ((op & 0x1f) << 8) | op2;
11571 if (op & 0x20)
11572 printf ("pop compact {");
11573 else
11574 printf ("pop {");
11575
11576 decode_tic6x_unwind_regmask (mask);
11577 printf("}");
11578 }
11579 }
11580 else if ((op & 0xf0) == 0xc0)
11581 {
11582 unsigned int reg;
11583 unsigned int nregs;
11584 unsigned int i;
11585 const char *name;
11586 struct
11587 {
11588 unsigned int offset;
11589 unsigned int reg;
11590 } regpos[16];
11591
11592 /* Scan entire instruction first so that GET_OP output is not
11593 interleaved with disassembly. */
11594 nregs = 0;
11595 for (i = 0; nregs < (op & 0xf); i++)
11596 {
11597 GET_OP (op2);
11598 reg = op2 >> 4;
11599 if (reg != 0xf)
11600 {
11601 regpos[nregs].offset = i * 2;
11602 regpos[nregs].reg = reg;
11603 nregs++;
11604 }
11605
11606 reg = op2 & 0xf;
11607 if (reg != 0xf)
11608 {
11609 regpos[nregs].offset = i * 2 + 1;
11610 regpos[nregs].reg = reg;
11611 nregs++;
11612 }
11613 }
11614
11615 printf (_("pop frame {"));
11616 if (nregs == 0)
11617 {
11618 printf (_("*corrupt* - no registers specified"));
11619 }
11620 else
11621 {
11622 reg = nregs - 1;
11623 for (i = i * 2; i > 0; i--)
11624 {
11625 if (regpos[reg].offset == i - 1)
11626 {
11627 name = tic6x_unwind_regnames[regpos[reg].reg];
11628 if (reg > 0)
11629 reg--;
11630 }
11631 else
11632 name = _("[pad]");
11633
11634 fputs (name, stdout);
11635 if (i > 1)
11636 printf (", ");
11637 }
11638 }
11639
11640 printf ("}");
11641 }
11642 else if (op == 0xd0)
11643 printf (" MOV FP, SP");
11644 else if (op == 0xd1)
11645 printf (" __c6xabi_pop_rts");
11646 else if (op == 0xd2)
11647 {
11648 unsigned char buf[9];
11649 unsigned int i, len;
11650 uint64_t offset;
11651
11652 for (i = 0; i < sizeof (buf); i++)
11653 {
11654 GET_OP (buf[i]);
11655 if ((buf[i] & 0x80) == 0)
11656 break;
11657 }
11658 /* PR 17531: file: id:000001,src:001906+004739,op:splice,rep:2. */
11659 if (i == sizeof (buf))
11660 {
11661 warn (_("Corrupt stack pointer adjustment detected\n"));
11662 return false;
11663 }
11664
11665 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
11666 assert (len == i + 1);
11667 offset = offset * 8 + 0x408;
11668 printf (_("sp = sp + %" PRId64), offset);
11669 }
11670 else if ((op & 0xf0) == 0xe0)
11671 {
11672 if ((op & 0x0f) == 7)
11673 printf (" RETURN");
11674 else
11675 printf (" MV %s, B3", tic6x_unwind_regnames[op & 0x0f]);
11676 }
11677 else
11678 {
11679 printf (_(" [unsupported opcode]"));
11680 }
11681 putchar ('\n');
11682 }
11683
11684 return true;
11685 }
11686
11687 static uint64_t
11688 arm_expand_prel31 (Filedata * filedata, uint64_t word, uint64_t where)
11689 {
11690 uint64_t offset;
11691
11692 offset = word & 0x7fffffff;
11693 if (offset & 0x40000000)
11694 offset |= ~ (uint64_t) 0x7fffffff;
11695
11696 if (filedata->file_header.e_machine == EM_TI_C6000)
11697 offset <<= 1;
11698
11699 return offset + where;
11700 }
11701
11702 static bool
11703 decode_arm_unwind (Filedata * filedata,
11704 struct arm_unw_aux_info * aux,
11705 unsigned int word,
11706 unsigned int remaining,
11707 uint64_t data_offset,
11708 Elf_Internal_Shdr * data_sec,
11709 struct arm_section * data_arm_sec)
11710 {
11711 int per_index;
11712 unsigned int more_words = 0;
11713 struct absaddr addr;
11714 uint64_t sym_name = (uint64_t) -1;
11715 bool res = true;
11716
11717 if (remaining == 0)
11718 {
11719 /* Fetch the first word.
11720 Note - when decoding an object file the address extracted
11721 here will always be 0. So we also pass in the sym_name
11722 parameter so that we can find the symbol associated with
11723 the personality routine. */
11724 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, data_offset,
11725 & word, & addr, & sym_name))
11726 return false;
11727
11728 remaining = 4;
11729 }
11730 else
11731 {
11732 addr.section = SHN_UNDEF;
11733 addr.offset = 0;
11734 }
11735
11736 if ((word & 0x80000000) == 0)
11737 {
11738 /* Expand prel31 for personality routine. */
11739 uint64_t fn;
11740 const char *procname;
11741
11742 fn = arm_expand_prel31 (filedata, word, data_sec->sh_addr + data_offset);
11743 printf (_(" Personality routine: "));
11744 if (fn == 0
11745 && addr.section == SHN_UNDEF && addr.offset == 0
11746 && sym_name != (uint64_t) -1 && sym_name < aux->strtab_size)
11747 {
11748 procname = aux->strtab + sym_name;
11749 print_vma (fn, PREFIX_HEX);
11750 if (procname)
11751 {
11752 fputs (" <", stdout);
11753 fputs (procname, stdout);
11754 fputc ('>', stdout);
11755 }
11756 }
11757 else
11758 procname = arm_print_vma_and_name (filedata, aux, fn, addr);
11759 fputc ('\n', stdout);
11760
11761 /* The GCC personality routines use the standard compact
11762 encoding, starting with one byte giving the number of
11763 words. */
11764 if (procname != NULL
11765 && (startswith (procname, "__gcc_personality_v0")
11766 || startswith (procname, "__gxx_personality_v0")
11767 || startswith (procname, "__gcj_personality_v0")
11768 || startswith (procname, "__gnu_objc_personality_v0")))
11769 {
11770 remaining = 0;
11771 more_words = 1;
11772 ADVANCE;
11773 if (!remaining)
11774 {
11775 printf (_(" [Truncated data]\n"));
11776 return false;
11777 }
11778 more_words = word >> 24;
11779 word <<= 8;
11780 remaining--;
11781 per_index = -1;
11782 }
11783 else
11784 return true;
11785 }
11786 else
11787 {
11788 /* ARM EHABI Section 6.3:
11789
11790 An exception-handling table entry for the compact model looks like:
11791
11792 31 30-28 27-24 23-0
11793 -- ----- ----- ----
11794 1 0 index Data for personalityRoutine[index] */
11795
11796 if (filedata->file_header.e_machine == EM_ARM
11797 && (word & 0x70000000))
11798 {
11799 warn (_("Corrupt ARM compact model table entry: %x \n"), word);
11800 res = false;
11801 }
11802
11803 per_index = (word >> 24) & 0x7f;
11804 printf (_(" Compact model index: %d\n"), per_index);
11805 if (per_index == 0)
11806 {
11807 more_words = 0;
11808 word <<= 8;
11809 remaining--;
11810 }
11811 else if (per_index < 3)
11812 {
11813 more_words = (word >> 16) & 0xff;
11814 word <<= 16;
11815 remaining -= 2;
11816 }
11817 }
11818
11819 switch (filedata->file_header.e_machine)
11820 {
11821 case EM_ARM:
11822 if (per_index < 3)
11823 {
11824 if (! decode_arm_unwind_bytecode (filedata, aux, word, remaining, more_words,
11825 data_offset, data_sec, data_arm_sec))
11826 res = false;
11827 }
11828 else
11829 {
11830 warn (_("Unknown ARM compact model index encountered\n"));
11831 printf (_(" [reserved]\n"));
11832 res = false;
11833 }
11834 break;
11835
11836 case EM_TI_C6000:
11837 if (per_index < 3)
11838 {
11839 if (! decode_tic6x_unwind_bytecode (filedata, aux, word, remaining, more_words,
11840 data_offset, data_sec, data_arm_sec))
11841 res = false;
11842 }
11843 else if (per_index < 5)
11844 {
11845 if (((word >> 17) & 0x7f) == 0x7f)
11846 printf (_(" Restore stack from frame pointer\n"));
11847 else
11848 printf (_(" Stack increment %d\n"), (word >> 14) & 0x1fc);
11849 printf (_(" Registers restored: "));
11850 if (per_index == 4)
11851 printf (" (compact) ");
11852 decode_tic6x_unwind_regmask ((word >> 4) & 0x1fff);
11853 putchar ('\n');
11854 printf (_(" Return register: %s\n"),
11855 tic6x_unwind_regnames[word & 0xf]);
11856 }
11857 else
11858 printf (_(" [reserved (%d)]\n"), per_index);
11859 break;
11860
11861 default:
11862 error (_("Unsupported architecture type %d encountered when decoding unwind table\n"),
11863 filedata->file_header.e_machine);
11864 res = false;
11865 }
11866
11867 /* Decode the descriptors. Not implemented. */
11868
11869 return res;
11870 }
11871
11872 static bool
11873 dump_arm_unwind (Filedata * filedata,
11874 struct arm_unw_aux_info * aux,
11875 Elf_Internal_Shdr * exidx_sec)
11876 {
11877 struct arm_section exidx_arm_sec, extab_arm_sec;
11878 unsigned int i, exidx_len;
11879 uint64_t j, nfuns;
11880 bool res = true;
11881
11882 memset (&exidx_arm_sec, 0, sizeof (exidx_arm_sec));
11883 memset (&extab_arm_sec, 0, sizeof (extab_arm_sec));
11884 exidx_len = exidx_sec->sh_size / 8;
11885
11886 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
11887 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
11888 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
11889 aux->funtab[nfuns++] = aux->symtab[j];
11890 aux->nfuns = nfuns;
11891 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
11892
11893 for (i = 0; i < exidx_len; i++)
11894 {
11895 unsigned int exidx_fn, exidx_entry;
11896 struct absaddr fn_addr, entry_addr;
11897 uint64_t fn;
11898
11899 fputc ('\n', stdout);
11900
11901 if (! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
11902 8 * i, & exidx_fn, & fn_addr, NULL)
11903 || ! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
11904 8 * i + 4, & exidx_entry, & entry_addr, NULL))
11905 {
11906 free (aux->funtab);
11907 arm_free_section (& exidx_arm_sec);
11908 arm_free_section (& extab_arm_sec);
11909 return false;
11910 }
11911
11912 /* ARM EHABI, Section 5:
11913 An index table entry consists of 2 words.
11914 The first word contains a prel31 offset to the start of a function, with bit 31 clear. */
11915 if (exidx_fn & 0x80000000)
11916 {
11917 warn (_("corrupt index table entry: %x\n"), exidx_fn);
11918 res = false;
11919 }
11920
11921 fn = arm_expand_prel31 (filedata, exidx_fn, exidx_sec->sh_addr + 8 * i);
11922
11923 arm_print_vma_and_name (filedata, aux, fn, fn_addr);
11924 fputs (": ", stdout);
11925
11926 if (exidx_entry == 1)
11927 {
11928 print_vma (exidx_entry, PREFIX_HEX);
11929 fputs (" [cantunwind]\n", stdout);
11930 }
11931 else if (exidx_entry & 0x80000000)
11932 {
11933 print_vma (exidx_entry, PREFIX_HEX);
11934 fputc ('\n', stdout);
11935 decode_arm_unwind (filedata, aux, exidx_entry, 4, 0, NULL, NULL);
11936 }
11937 else
11938 {
11939 uint64_t table, table_offset = 0;
11940 Elf_Internal_Shdr *table_sec;
11941
11942 fputs ("@", stdout);
11943 table = arm_expand_prel31 (filedata, exidx_entry, exidx_sec->sh_addr + 8 * i + 4);
11944 print_vma (table, PREFIX_HEX);
11945 printf ("\n");
11946
11947 /* Locate the matching .ARM.extab. */
11948 if (entry_addr.section != SHN_UNDEF
11949 && entry_addr.section < filedata->file_header.e_shnum)
11950 {
11951 table_sec = filedata->section_headers + entry_addr.section;
11952 table_offset = entry_addr.offset;
11953 /* PR 18879 */
11954 if (table_offset > table_sec->sh_size)
11955 {
11956 warn (_("Unwind entry contains corrupt offset (%#" PRIx64 ") into section %s\n"),
11957 table_offset,
11958 printable_section_name (filedata, table_sec));
11959 res = false;
11960 continue;
11961 }
11962 }
11963 else
11964 {
11965 table_sec = find_section_by_address (filedata, table);
11966 if (table_sec != NULL)
11967 table_offset = table - table_sec->sh_addr;
11968 }
11969
11970 if (table_sec == NULL)
11971 {
11972 warn (_("Could not locate .ARM.extab section containing %#" PRIx64 ".\n"),
11973 table);
11974 res = false;
11975 continue;
11976 }
11977
11978 if (! decode_arm_unwind (filedata, aux, 0, 0, table_offset, table_sec,
11979 &extab_arm_sec))
11980 res = false;
11981 }
11982 }
11983
11984 printf ("\n");
11985
11986 free (aux->funtab);
11987 arm_free_section (&exidx_arm_sec);
11988 arm_free_section (&extab_arm_sec);
11989
11990 return res;
11991 }
11992
11993 /* Used for both ARM and C6X unwinding tables. */
11994
11995 static bool
11996 arm_process_unwind (Filedata * filedata)
11997 {
11998 struct arm_unw_aux_info aux;
11999 Elf_Internal_Shdr *unwsec = NULL;
12000 Elf_Internal_Shdr *sec;
12001 size_t i;
12002 unsigned int sec_type;
12003 bool res = true;
12004
12005 switch (filedata->file_header.e_machine)
12006 {
12007 case EM_ARM:
12008 sec_type = SHT_ARM_EXIDX;
12009 break;
12010
12011 case EM_TI_C6000:
12012 sec_type = SHT_C6000_UNWIND;
12013 break;
12014
12015 default:
12016 error (_("Unsupported architecture type %d encountered when processing unwind table\n"),
12017 filedata->file_header.e_machine);
12018 return false;
12019 }
12020
12021 if (filedata->string_table == NULL)
12022 return false;
12023
12024 memset (& aux, 0, sizeof (aux));
12025 aux.filedata = filedata;
12026
12027 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
12028 {
12029 if (sec->sh_type == SHT_SYMTAB)
12030 {
12031 if (aux.symtab)
12032 {
12033 error (_("Multiple symbol tables encountered\n"));
12034 free (aux.symtab);
12035 aux.symtab = NULL;
12036 free (aux.strtab);
12037 aux.strtab = NULL;
12038 }
12039 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
12040 &aux.strtab, &aux.strtab_size))
12041 return false;
12042 }
12043 else if (sec->sh_type == sec_type)
12044 unwsec = sec;
12045 }
12046
12047 if (unwsec == NULL)
12048 printf (_("\nThere are no unwind sections in this file.\n"));
12049 else
12050 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
12051 {
12052 if (sec->sh_type == sec_type)
12053 {
12054 uint64_t num_unwind = sec->sh_size / (2 * eh_addr_size);
12055 printf (ngettext ("\nUnwind section '%s' at offset %#" PRIx64 " "
12056 "contains %" PRIu64 " entry:\n",
12057 "\nUnwind section '%s' at offset %#" PRIx64 " "
12058 "contains %" PRIu64 " entries:\n",
12059 num_unwind),
12060 printable_section_name (filedata, sec),
12061 sec->sh_offset,
12062 num_unwind);
12063
12064 if (! dump_arm_unwind (filedata, &aux, sec))
12065 res = false;
12066 }
12067 }
12068
12069 free (aux.symtab);
12070 free ((char *) aux.strtab);
12071
12072 return res;
12073 }
12074
12075 static bool
12076 no_processor_specific_unwind (Filedata * filedata ATTRIBUTE_UNUSED)
12077 {
12078 printf (_("No processor specific unwind information to decode\n"));
12079 return true;
12080 }
12081
12082 static bool
12083 process_unwind (Filedata * filedata)
12084 {
12085 struct unwind_handler
12086 {
12087 unsigned int machtype;
12088 bool (* handler)(Filedata *);
12089 } handlers[] =
12090 {
12091 { EM_ARM, arm_process_unwind },
12092 { EM_IA_64, ia64_process_unwind },
12093 { EM_PARISC, hppa_process_unwind },
12094 { EM_TI_C6000, arm_process_unwind },
12095 { EM_386, no_processor_specific_unwind },
12096 { EM_X86_64, no_processor_specific_unwind },
12097 { 0, NULL }
12098 };
12099 int i;
12100
12101 if (!do_unwind)
12102 return true;
12103
12104 for (i = 0; handlers[i].handler != NULL; i++)
12105 if (filedata->file_header.e_machine == handlers[i].machtype)
12106 return handlers[i].handler (filedata);
12107
12108 printf (_("\nThe decoding of unwind sections for machine type %s is not currently supported.\n"),
12109 get_machine_name (filedata->file_header.e_machine));
12110 return true;
12111 }
12112
12113 static void
12114 dynamic_section_aarch64_val (Elf_Internal_Dyn * entry)
12115 {
12116 switch (entry->d_tag)
12117 {
12118 case DT_AARCH64_BTI_PLT:
12119 case DT_AARCH64_PAC_PLT:
12120 break;
12121 default:
12122 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
12123 break;
12124 }
12125 putchar ('\n');
12126 }
12127
12128 static void
12129 dynamic_section_mips_val (Filedata * filedata, Elf_Internal_Dyn * entry)
12130 {
12131 switch (entry->d_tag)
12132 {
12133 case DT_MIPS_FLAGS:
12134 if (entry->d_un.d_val == 0)
12135 printf (_("NONE"));
12136 else
12137 {
12138 static const char * opts[] =
12139 {
12140 "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
12141 "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
12142 "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
12143 "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
12144 "RLD_ORDER_SAFE"
12145 };
12146 unsigned int cnt;
12147 bool first = true;
12148
12149 for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
12150 if (entry->d_un.d_val & (1 << cnt))
12151 {
12152 printf ("%s%s", first ? "" : " ", opts[cnt]);
12153 first = false;
12154 }
12155 }
12156 break;
12157
12158 case DT_MIPS_IVERSION:
12159 if (valid_dynamic_name (filedata, entry->d_un.d_val))
12160 printf (_("Interface Version: %s"),
12161 get_dynamic_name (filedata, entry->d_un.d_val));
12162 else
12163 printf (_("Interface Version: <corrupt: %" PRIx64 ">"),
12164 entry->d_un.d_ptr);
12165 break;
12166
12167 case DT_MIPS_TIME_STAMP:
12168 {
12169 char timebuf[128];
12170 struct tm * tmp;
12171 time_t atime = entry->d_un.d_val;
12172
12173 tmp = gmtime (&atime);
12174 /* PR 17531: file: 6accc532. */
12175 if (tmp == NULL)
12176 snprintf (timebuf, sizeof (timebuf), _("<corrupt>"));
12177 else
12178 snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
12179 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
12180 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
12181 printf (_("Time Stamp: %s"), timebuf);
12182 }
12183 break;
12184
12185 case DT_MIPS_RLD_VERSION:
12186 case DT_MIPS_LOCAL_GOTNO:
12187 case DT_MIPS_CONFLICTNO:
12188 case DT_MIPS_LIBLISTNO:
12189 case DT_MIPS_SYMTABNO:
12190 case DT_MIPS_UNREFEXTNO:
12191 case DT_MIPS_HIPAGENO:
12192 case DT_MIPS_DELTA_CLASS_NO:
12193 case DT_MIPS_DELTA_INSTANCE_NO:
12194 case DT_MIPS_DELTA_RELOC_NO:
12195 case DT_MIPS_DELTA_SYM_NO:
12196 case DT_MIPS_DELTA_CLASSSYM_NO:
12197 case DT_MIPS_COMPACT_SIZE:
12198 print_vma (entry->d_un.d_val, DEC);
12199 break;
12200
12201 case DT_MIPS_XHASH:
12202 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
12203 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
12204 /* Falls through. */
12205
12206 default:
12207 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
12208 }
12209 putchar ('\n');
12210 }
12211
12212 static void
12213 dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
12214 {
12215 switch (entry->d_tag)
12216 {
12217 case DT_HP_DLD_FLAGS:
12218 {
12219 static struct
12220 {
12221 unsigned int bit;
12222 const char * str;
12223 }
12224 flags[] =
12225 {
12226 { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
12227 { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
12228 { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
12229 { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
12230 { DT_HP_BIND_NOW, "HP_BIND_NOW" },
12231 { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
12232 { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
12233 { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
12234 { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
12235 { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
12236 { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
12237 { DT_HP_GST, "HP_GST" },
12238 { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
12239 { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
12240 { DT_HP_NODELETE, "HP_NODELETE" },
12241 { DT_HP_GROUP, "HP_GROUP" },
12242 { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
12243 };
12244 bool first = true;
12245 size_t cnt;
12246 uint64_t val = entry->d_un.d_val;
12247
12248 for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
12249 if (val & flags[cnt].bit)
12250 {
12251 if (! first)
12252 putchar (' ');
12253 fputs (flags[cnt].str, stdout);
12254 first = false;
12255 val ^= flags[cnt].bit;
12256 }
12257
12258 if (val != 0 || first)
12259 {
12260 if (! first)
12261 putchar (' ');
12262 print_vma (val, HEX);
12263 }
12264 }
12265 break;
12266
12267 default:
12268 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
12269 break;
12270 }
12271 putchar ('\n');
12272 }
12273
12274 /* VMS vs Unix time offset and factor. */
12275
12276 #define VMS_EPOCH_OFFSET 35067168000000000LL
12277 #define VMS_GRANULARITY_FACTOR 10000000
12278 #ifndef INT64_MIN
12279 #define INT64_MIN (-9223372036854775807LL - 1)
12280 #endif
12281
12282 /* Display a VMS time in a human readable format. */
12283
12284 static void
12285 print_vms_time (int64_t vmstime)
12286 {
12287 struct tm *tm = NULL;
12288 time_t unxtime;
12289
12290 if (vmstime >= INT64_MIN + VMS_EPOCH_OFFSET)
12291 {
12292 vmstime = (vmstime - VMS_EPOCH_OFFSET) / VMS_GRANULARITY_FACTOR;
12293 unxtime = vmstime;
12294 if (unxtime == vmstime)
12295 tm = gmtime (&unxtime);
12296 }
12297 if (tm != NULL)
12298 printf ("%04u-%02u-%02uT%02u:%02u:%02u",
12299 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
12300 tm->tm_hour, tm->tm_min, tm->tm_sec);
12301 }
12302
12303 static void
12304 dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
12305 {
12306 switch (entry->d_tag)
12307 {
12308 case DT_IA_64_PLT_RESERVE:
12309 /* First 3 slots reserved. */
12310 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
12311 printf (" -- ");
12312 print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
12313 break;
12314
12315 case DT_IA_64_VMS_LINKTIME:
12316 print_vms_time (entry->d_un.d_val);
12317 break;
12318
12319 case DT_IA_64_VMS_LNKFLAGS:
12320 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
12321 if (entry->d_un.d_val & VMS_LF_CALL_DEBUG)
12322 printf (" CALL_DEBUG");
12323 if (entry->d_un.d_val & VMS_LF_NOP0BUFS)
12324 printf (" NOP0BUFS");
12325 if (entry->d_un.d_val & VMS_LF_P0IMAGE)
12326 printf (" P0IMAGE");
12327 if (entry->d_un.d_val & VMS_LF_MKTHREADS)
12328 printf (" MKTHREADS");
12329 if (entry->d_un.d_val & VMS_LF_UPCALLS)
12330 printf (" UPCALLS");
12331 if (entry->d_un.d_val & VMS_LF_IMGSTA)
12332 printf (" IMGSTA");
12333 if (entry->d_un.d_val & VMS_LF_INITIALIZE)
12334 printf (" INITIALIZE");
12335 if (entry->d_un.d_val & VMS_LF_MAIN)
12336 printf (" MAIN");
12337 if (entry->d_un.d_val & VMS_LF_EXE_INIT)
12338 printf (" EXE_INIT");
12339 if (entry->d_un.d_val & VMS_LF_TBK_IN_IMG)
12340 printf (" TBK_IN_IMG");
12341 if (entry->d_un.d_val & VMS_LF_DBG_IN_IMG)
12342 printf (" DBG_IN_IMG");
12343 if (entry->d_un.d_val & VMS_LF_TBK_IN_DSF)
12344 printf (" TBK_IN_DSF");
12345 if (entry->d_un.d_val & VMS_LF_DBG_IN_DSF)
12346 printf (" DBG_IN_DSF");
12347 if (entry->d_un.d_val & VMS_LF_SIGNATURES)
12348 printf (" SIGNATURES");
12349 if (entry->d_un.d_val & VMS_LF_REL_SEG_OFF)
12350 printf (" REL_SEG_OFF");
12351 break;
12352
12353 default:
12354 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
12355 break;
12356 }
12357 putchar ('\n');
12358 }
12359
12360 static bool
12361 get_32bit_dynamic_section (Filedata * filedata)
12362 {
12363 Elf32_External_Dyn * edyn;
12364 Elf32_External_Dyn * ext;
12365 Elf_Internal_Dyn * entry;
12366
12367 edyn = (Elf32_External_Dyn *) get_data (NULL, filedata,
12368 filedata->dynamic_addr, 1,
12369 filedata->dynamic_size,
12370 _("dynamic section"));
12371 if (!edyn)
12372 return false;
12373
12374 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
12375 might not have the luxury of section headers. Look for the DT_NULL
12376 terminator to determine the number of entries. */
12377 for (ext = edyn, filedata->dynamic_nent = 0;
12378 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
12379 ext++)
12380 {
12381 filedata->dynamic_nent++;
12382 if (BYTE_GET (ext->d_tag) == DT_NULL)
12383 break;
12384 }
12385
12386 filedata->dynamic_section
12387 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
12388 if (filedata->dynamic_section == NULL)
12389 {
12390 error (_("Out of memory allocating space for %" PRIu64 " dynamic entries\n"),
12391 filedata->dynamic_nent);
12392 free (edyn);
12393 return false;
12394 }
12395
12396 for (ext = edyn, entry = filedata->dynamic_section;
12397 entry < filedata->dynamic_section + filedata->dynamic_nent;
12398 ext++, entry++)
12399 {
12400 entry->d_tag = BYTE_GET (ext->d_tag);
12401 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
12402 }
12403
12404 free (edyn);
12405
12406 return true;
12407 }
12408
12409 static bool
12410 get_64bit_dynamic_section (Filedata * filedata)
12411 {
12412 Elf64_External_Dyn * edyn;
12413 Elf64_External_Dyn * ext;
12414 Elf_Internal_Dyn * entry;
12415
12416 /* Read in the data. */
12417 edyn = (Elf64_External_Dyn *) get_data (NULL, filedata,
12418 filedata->dynamic_addr, 1,
12419 filedata->dynamic_size,
12420 _("dynamic section"));
12421 if (!edyn)
12422 return false;
12423
12424 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
12425 might not have the luxury of section headers. Look for the DT_NULL
12426 terminator to determine the number of entries. */
12427 for (ext = edyn, filedata->dynamic_nent = 0;
12428 /* PR 17533 file: 033-67080-0.004 - do not read past end of buffer. */
12429 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
12430 ext++)
12431 {
12432 filedata->dynamic_nent++;
12433 if (BYTE_GET (ext->d_tag) == DT_NULL)
12434 break;
12435 }
12436
12437 filedata->dynamic_section
12438 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
12439 if (filedata->dynamic_section == NULL)
12440 {
12441 error (_("Out of memory allocating space for %" PRIu64 " dynamic entries\n"),
12442 filedata->dynamic_nent);
12443 free (edyn);
12444 return false;
12445 }
12446
12447 /* Convert from external to internal formats. */
12448 for (ext = edyn, entry = filedata->dynamic_section;
12449 entry < filedata->dynamic_section + filedata->dynamic_nent;
12450 ext++, entry++)
12451 {
12452 entry->d_tag = BYTE_GET (ext->d_tag);
12453 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
12454 }
12455
12456 free (edyn);
12457
12458 return true;
12459 }
12460
12461 static bool
12462 get_dynamic_section (Filedata *filedata)
12463 {
12464 if (filedata->dynamic_section)
12465 return true;
12466
12467 if (is_32bit_elf)
12468 return get_32bit_dynamic_section (filedata);
12469 else
12470 return get_64bit_dynamic_section (filedata);
12471 }
12472
12473 static void
12474 print_dynamic_flags (uint64_t flags)
12475 {
12476 bool first = true;
12477
12478 while (flags)
12479 {
12480 uint64_t flag;
12481
12482 flag = flags & - flags;
12483 flags &= ~ flag;
12484
12485 if (first)
12486 first = false;
12487 else
12488 putc (' ', stdout);
12489
12490 switch (flag)
12491 {
12492 case DF_ORIGIN: fputs ("ORIGIN", stdout); break;
12493 case DF_SYMBOLIC: fputs ("SYMBOLIC", stdout); break;
12494 case DF_TEXTREL: fputs ("TEXTREL", stdout); break;
12495 case DF_BIND_NOW: fputs ("BIND_NOW", stdout); break;
12496 case DF_STATIC_TLS: fputs ("STATIC_TLS", stdout); break;
12497 default: fputs (_("unknown"), stdout); break;
12498 }
12499 }
12500 puts ("");
12501 }
12502
12503 static uint64_t *
12504 get_dynamic_data (Filedata * filedata, uint64_t number, unsigned int ent_size)
12505 {
12506 unsigned char * e_data;
12507 uint64_t * i_data;
12508
12509 /* If size_t is smaller than uint64_t, eg because you are building
12510 on a 32-bit host, then make sure that when number is cast to
12511 size_t no information is lost. */
12512 if ((size_t) number != number
12513 || ent_size * number / ent_size != number)
12514 {
12515 error (_("Size overflow prevents reading %" PRIu64
12516 " elements of size %u\n"),
12517 number, ent_size);
12518 return NULL;
12519 }
12520
12521 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
12522 attempting to allocate memory when the read is bound to fail. */
12523 if (ent_size * number > filedata->file_size)
12524 {
12525 error (_("Invalid number of dynamic entries: %" PRIu64 "\n"),
12526 number);
12527 return NULL;
12528 }
12529
12530 e_data = (unsigned char *) cmalloc ((size_t) number, ent_size);
12531 if (e_data == NULL)
12532 {
12533 error (_("Out of memory reading %" PRIu64 " dynamic entries\n"),
12534 number);
12535 return NULL;
12536 }
12537
12538 if (fread (e_data, ent_size, (size_t) number, filedata->handle) != number)
12539 {
12540 error (_("Unable to read in %" PRIu64 " bytes of dynamic data\n"),
12541 number * ent_size);
12542 free (e_data);
12543 return NULL;
12544 }
12545
12546 i_data = (uint64_t *) cmalloc ((size_t) number, sizeof (*i_data));
12547 if (i_data == NULL)
12548 {
12549 error (_("Out of memory allocating space for %" PRIu64 " dynamic entries\n"),
12550 number);
12551 free (e_data);
12552 return NULL;
12553 }
12554
12555 while (number--)
12556 i_data[number] = byte_get (e_data + number * ent_size, ent_size);
12557
12558 free (e_data);
12559
12560 return i_data;
12561 }
12562
12563 static uint64_t
12564 get_num_dynamic_syms (Filedata * filedata)
12565 {
12566 uint64_t num_of_syms = 0;
12567
12568 if (!do_histogram && (!do_using_dynamic || do_dyn_syms))
12569 return num_of_syms;
12570
12571 if (filedata->dynamic_info[DT_HASH])
12572 {
12573 unsigned char nb[8];
12574 unsigned char nc[8];
12575 unsigned int hash_ent_size = 4;
12576
12577 if ((filedata->file_header.e_machine == EM_ALPHA
12578 || filedata->file_header.e_machine == EM_S390
12579 || filedata->file_header.e_machine == EM_S390_OLD)
12580 && filedata->file_header.e_ident[EI_CLASS] == ELFCLASS64)
12581 hash_ent_size = 8;
12582
12583 if (fseek64 (filedata->handle,
12584 (filedata->archive_file_offset
12585 + offset_from_vma (filedata,
12586 filedata->dynamic_info[DT_HASH],
12587 sizeof nb + sizeof nc)),
12588 SEEK_SET))
12589 {
12590 error (_("Unable to seek to start of dynamic information\n"));
12591 goto no_hash;
12592 }
12593
12594 if (fread (nb, hash_ent_size, 1, filedata->handle) != 1)
12595 {
12596 error (_("Failed to read in number of buckets\n"));
12597 goto no_hash;
12598 }
12599
12600 if (fread (nc, hash_ent_size, 1, filedata->handle) != 1)
12601 {
12602 error (_("Failed to read in number of chains\n"));
12603 goto no_hash;
12604 }
12605
12606 filedata->nbuckets = byte_get (nb, hash_ent_size);
12607 filedata->nchains = byte_get (nc, hash_ent_size);
12608
12609 if (filedata->nbuckets != 0 && filedata->nchains != 0)
12610 {
12611 filedata->buckets = get_dynamic_data (filedata, filedata->nbuckets,
12612 hash_ent_size);
12613 filedata->chains = get_dynamic_data (filedata, filedata->nchains,
12614 hash_ent_size);
12615
12616 if (filedata->buckets != NULL && filedata->chains != NULL)
12617 num_of_syms = filedata->nchains;
12618 }
12619 no_hash:
12620 if (num_of_syms == 0)
12621 {
12622 free (filedata->buckets);
12623 filedata->buckets = NULL;
12624 free (filedata->chains);
12625 filedata->chains = NULL;
12626 filedata->nbuckets = 0;
12627 }
12628 }
12629
12630 if (filedata->dynamic_info_DT_GNU_HASH)
12631 {
12632 unsigned char nb[16];
12633 uint64_t i, maxchain = 0xffffffff, bitmaskwords;
12634 uint64_t buckets_vma;
12635 uint64_t hn;
12636
12637 if (fseek64 (filedata->handle,
12638 (filedata->archive_file_offset
12639 + offset_from_vma (filedata,
12640 filedata->dynamic_info_DT_GNU_HASH,
12641 sizeof nb)),
12642 SEEK_SET))
12643 {
12644 error (_("Unable to seek to start of dynamic information\n"));
12645 goto no_gnu_hash;
12646 }
12647
12648 if (fread (nb, 16, 1, filedata->handle) != 1)
12649 {
12650 error (_("Failed to read in number of buckets\n"));
12651 goto no_gnu_hash;
12652 }
12653
12654 filedata->ngnubuckets = byte_get (nb, 4);
12655 filedata->gnusymidx = byte_get (nb + 4, 4);
12656 bitmaskwords = byte_get (nb + 8, 4);
12657 buckets_vma = filedata->dynamic_info_DT_GNU_HASH + 16;
12658 if (is_32bit_elf)
12659 buckets_vma += bitmaskwords * 4;
12660 else
12661 buckets_vma += bitmaskwords * 8;
12662
12663 if (fseek64 (filedata->handle,
12664 (filedata->archive_file_offset
12665 + offset_from_vma (filedata, buckets_vma, 4)),
12666 SEEK_SET))
12667 {
12668 error (_("Unable to seek to start of dynamic information\n"));
12669 goto no_gnu_hash;
12670 }
12671
12672 filedata->gnubuckets
12673 = get_dynamic_data (filedata, filedata->ngnubuckets, 4);
12674
12675 if (filedata->gnubuckets == NULL)
12676 goto no_gnu_hash;
12677
12678 for (i = 0; i < filedata->ngnubuckets; i++)
12679 if (filedata->gnubuckets[i] != 0)
12680 {
12681 if (filedata->gnubuckets[i] < filedata->gnusymidx)
12682 goto no_gnu_hash;
12683
12684 if (maxchain == 0xffffffff || filedata->gnubuckets[i] > maxchain)
12685 maxchain = filedata->gnubuckets[i];
12686 }
12687
12688 if (maxchain == 0xffffffff)
12689 goto no_gnu_hash;
12690
12691 maxchain -= filedata->gnusymidx;
12692
12693 if (fseek64 (filedata->handle,
12694 (filedata->archive_file_offset
12695 + offset_from_vma (filedata,
12696 buckets_vma + 4 * (filedata->ngnubuckets
12697 + maxchain),
12698 4)),
12699 SEEK_SET))
12700 {
12701 error (_("Unable to seek to start of dynamic information\n"));
12702 goto no_gnu_hash;
12703 }
12704
12705 do
12706 {
12707 if (fread (nb, 4, 1, filedata->handle) != 1)
12708 {
12709 error (_("Failed to determine last chain length\n"));
12710 goto no_gnu_hash;
12711 }
12712
12713 if (maxchain + 1 == 0)
12714 goto no_gnu_hash;
12715
12716 ++maxchain;
12717 }
12718 while ((byte_get (nb, 4) & 1) == 0);
12719
12720 if (fseek64 (filedata->handle,
12721 (filedata->archive_file_offset
12722 + offset_from_vma (filedata, (buckets_vma
12723 + 4 * filedata->ngnubuckets),
12724 4)),
12725 SEEK_SET))
12726 {
12727 error (_("Unable to seek to start of dynamic information\n"));
12728 goto no_gnu_hash;
12729 }
12730
12731 filedata->gnuchains = get_dynamic_data (filedata, maxchain, 4);
12732 filedata->ngnuchains = maxchain;
12733
12734 if (filedata->gnuchains == NULL)
12735 goto no_gnu_hash;
12736
12737 if (filedata->dynamic_info_DT_MIPS_XHASH)
12738 {
12739 if (fseek64 (filedata->handle,
12740 (filedata->archive_file_offset
12741 + offset_from_vma (filedata, (buckets_vma
12742 + 4 * (filedata->ngnubuckets
12743 + maxchain)), 4)),
12744 SEEK_SET))
12745 {
12746 error (_("Unable to seek to start of dynamic information\n"));
12747 goto no_gnu_hash;
12748 }
12749
12750 filedata->mipsxlat = get_dynamic_data (filedata, maxchain, 4);
12751 if (filedata->mipsxlat == NULL)
12752 goto no_gnu_hash;
12753 }
12754
12755 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
12756 if (filedata->gnubuckets[hn] != 0)
12757 {
12758 uint64_t si = filedata->gnubuckets[hn];
12759 uint64_t off = si - filedata->gnusymidx;
12760
12761 do
12762 {
12763 if (filedata->dynamic_info_DT_MIPS_XHASH)
12764 {
12765 if (off < filedata->ngnuchains
12766 && filedata->mipsxlat[off] >= num_of_syms)
12767 num_of_syms = filedata->mipsxlat[off] + 1;
12768 }
12769 else
12770 {
12771 if (si >= num_of_syms)
12772 num_of_syms = si + 1;
12773 }
12774 si++;
12775 }
12776 while (off < filedata->ngnuchains
12777 && (filedata->gnuchains[off++] & 1) == 0);
12778 }
12779
12780 if (num_of_syms == 0)
12781 {
12782 no_gnu_hash:
12783 free (filedata->mipsxlat);
12784 filedata->mipsxlat = NULL;
12785 free (filedata->gnuchains);
12786 filedata->gnuchains = NULL;
12787 free (filedata->gnubuckets);
12788 filedata->gnubuckets = NULL;
12789 filedata->ngnubuckets = 0;
12790 filedata->ngnuchains = 0;
12791 }
12792 }
12793
12794 return num_of_syms;
12795 }
12796
12797 /* Parse and display the contents of the dynamic section. */
12798
12799 static bool
12800 process_dynamic_section (Filedata * filedata)
12801 {
12802 Elf_Internal_Dyn * entry;
12803
12804 if (filedata->dynamic_size <= 1)
12805 {
12806 if (do_dynamic)
12807 {
12808 if (filedata->is_separate)
12809 printf (_("\nThere is no dynamic section in linked file '%s'.\n"),
12810 printable_string (filedata->file_name, 0));
12811 else
12812 printf (_("\nThere is no dynamic section in this file.\n"));
12813 }
12814
12815 return true;
12816 }
12817
12818 if (!get_dynamic_section (filedata))
12819 return false;
12820
12821 /* Find the appropriate symbol table. */
12822 if (filedata->dynamic_symbols == NULL || do_histogram)
12823 {
12824 uint64_t num_of_syms;
12825
12826 for (entry = filedata->dynamic_section;
12827 entry < filedata->dynamic_section + filedata->dynamic_nent;
12828 ++entry)
12829 if (entry->d_tag == DT_SYMTAB)
12830 filedata->dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
12831 else if (entry->d_tag == DT_SYMENT)
12832 filedata->dynamic_info[DT_SYMENT] = entry->d_un.d_val;
12833 else if (entry->d_tag == DT_HASH)
12834 filedata->dynamic_info[DT_HASH] = entry->d_un.d_val;
12835 else if (entry->d_tag == DT_GNU_HASH)
12836 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
12837 else if ((filedata->file_header.e_machine == EM_MIPS
12838 || filedata->file_header.e_machine == EM_MIPS_RS3_LE)
12839 && entry->d_tag == DT_MIPS_XHASH)
12840 {
12841 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
12842 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
12843 }
12844
12845 num_of_syms = get_num_dynamic_syms (filedata);
12846
12847 if (num_of_syms != 0
12848 && filedata->dynamic_symbols == NULL
12849 && filedata->dynamic_info[DT_SYMTAB]
12850 && filedata->dynamic_info[DT_SYMENT])
12851 {
12852 Elf_Internal_Phdr *seg;
12853 uint64_t vma = filedata->dynamic_info[DT_SYMTAB];
12854
12855 if (! get_program_headers (filedata))
12856 {
12857 error (_("Cannot interpret virtual addresses "
12858 "without program headers.\n"));
12859 return false;
12860 }
12861
12862 for (seg = filedata->program_headers;
12863 seg < filedata->program_headers + filedata->file_header.e_phnum;
12864 ++seg)
12865 {
12866 if (seg->p_type != PT_LOAD)
12867 continue;
12868
12869 if (seg->p_offset + seg->p_filesz > filedata->file_size)
12870 {
12871 /* See PR 21379 for a reproducer. */
12872 error (_("Invalid PT_LOAD entry\n"));
12873 return false;
12874 }
12875
12876 if (vma >= (seg->p_vaddr & -seg->p_align)
12877 && vma < seg->p_vaddr + seg->p_filesz)
12878 {
12879 /* Since we do not know how big the symbol table is,
12880 we default to reading in up to the end of PT_LOAD
12881 segment and processing that. This is overkill, I
12882 know, but it should work. */
12883 Elf_Internal_Shdr section;
12884 section.sh_offset = (vma - seg->p_vaddr
12885 + seg->p_offset);
12886 section.sh_size = (num_of_syms
12887 * filedata->dynamic_info[DT_SYMENT]);
12888 section.sh_entsize = filedata->dynamic_info[DT_SYMENT];
12889
12890 if (do_checks
12891 && filedata->dynamic_symtab_section != NULL
12892 && ((filedata->dynamic_symtab_section->sh_offset
12893 != section.sh_offset)
12894 || (filedata->dynamic_symtab_section->sh_size
12895 != section.sh_size)
12896 || (filedata->dynamic_symtab_section->sh_entsize
12897 != section.sh_entsize)))
12898 warn (_("\
12899 the .dynsym section doesn't match the DT_SYMTAB and DT_SYMENT tags\n"));
12900
12901 section.sh_name = filedata->string_table_length;
12902 filedata->dynamic_symbols
12903 = get_elf_symbols (filedata, &section,
12904 &filedata->num_dynamic_syms);
12905 if (filedata->dynamic_symbols == NULL
12906 || filedata->num_dynamic_syms != num_of_syms)
12907 {
12908 error (_("Corrupt DT_SYMTAB dynamic entry\n"));
12909 return false;
12910 }
12911 break;
12912 }
12913 }
12914 }
12915 }
12916
12917 /* Similarly find a string table. */
12918 if (filedata->dynamic_strings == NULL)
12919 for (entry = filedata->dynamic_section;
12920 entry < filedata->dynamic_section + filedata->dynamic_nent;
12921 ++entry)
12922 {
12923 if (entry->d_tag == DT_STRTAB)
12924 filedata->dynamic_info[DT_STRTAB] = entry->d_un.d_val;
12925
12926 if (entry->d_tag == DT_STRSZ)
12927 filedata->dynamic_info[DT_STRSZ] = entry->d_un.d_val;
12928
12929 if (filedata->dynamic_info[DT_STRTAB]
12930 && filedata->dynamic_info[DT_STRSZ])
12931 {
12932 uint64_t offset;
12933 uint64_t str_tab_len = filedata->dynamic_info[DT_STRSZ];
12934
12935 offset = offset_from_vma (filedata,
12936 filedata->dynamic_info[DT_STRTAB],
12937 str_tab_len);
12938 if (do_checks
12939 && filedata->dynamic_strtab_section
12940 && ((filedata->dynamic_strtab_section->sh_offset
12941 != (file_ptr) offset)
12942 || (filedata->dynamic_strtab_section->sh_size
12943 != str_tab_len)))
12944 warn (_("\
12945 the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
12946
12947 filedata->dynamic_strings
12948 = (char *) get_data (NULL, filedata, offset, 1, str_tab_len,
12949 _("dynamic string table"));
12950 if (filedata->dynamic_strings == NULL)
12951 {
12952 error (_("Corrupt DT_STRTAB dynamic entry\n"));
12953 break;
12954 }
12955
12956 filedata->dynamic_strings_length = str_tab_len;
12957 break;
12958 }
12959 }
12960
12961 /* And find the syminfo section if available. */
12962 if (filedata->dynamic_syminfo == NULL)
12963 {
12964 uint64_t syminsz = 0;
12965
12966 for (entry = filedata->dynamic_section;
12967 entry < filedata->dynamic_section + filedata->dynamic_nent;
12968 ++entry)
12969 {
12970 if (entry->d_tag == DT_SYMINENT)
12971 {
12972 /* Note: these braces are necessary to avoid a syntax
12973 error from the SunOS4 C compiler. */
12974 /* PR binutils/17531: A corrupt file can trigger this test.
12975 So do not use an assert, instead generate an error message. */
12976 if (sizeof (Elf_External_Syminfo) != entry->d_un.d_val)
12977 error (_("Bad value (%d) for SYMINENT entry\n"),
12978 (int) entry->d_un.d_val);
12979 }
12980 else if (entry->d_tag == DT_SYMINSZ)
12981 syminsz = entry->d_un.d_val;
12982 else if (entry->d_tag == DT_SYMINFO)
12983 filedata->dynamic_syminfo_offset
12984 = offset_from_vma (filedata, entry->d_un.d_val, syminsz);
12985 }
12986
12987 if (filedata->dynamic_syminfo_offset != 0 && syminsz != 0)
12988 {
12989 Elf_External_Syminfo * extsyminfo;
12990 Elf_External_Syminfo * extsym;
12991 Elf_Internal_Syminfo * syminfo;
12992
12993 /* There is a syminfo section. Read the data. */
12994 extsyminfo = (Elf_External_Syminfo *)
12995 get_data (NULL, filedata, filedata->dynamic_syminfo_offset,
12996 1, syminsz, _("symbol information"));
12997 if (!extsyminfo)
12998 return false;
12999
13000 if (filedata->dynamic_syminfo != NULL)
13001 {
13002 error (_("Multiple dynamic symbol information sections found\n"));
13003 free (filedata->dynamic_syminfo);
13004 }
13005 filedata->dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
13006 if (filedata->dynamic_syminfo == NULL)
13007 {
13008 error (_("Out of memory allocating %" PRIu64
13009 " bytes for dynamic symbol info\n"),
13010 syminsz);
13011 return false;
13012 }
13013
13014 filedata->dynamic_syminfo_nent
13015 = syminsz / sizeof (Elf_External_Syminfo);
13016 for (syminfo = filedata->dynamic_syminfo, extsym = extsyminfo;
13017 syminfo < (filedata->dynamic_syminfo
13018 + filedata->dynamic_syminfo_nent);
13019 ++syminfo, ++extsym)
13020 {
13021 syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
13022 syminfo->si_flags = BYTE_GET (extsym->si_flags);
13023 }
13024
13025 free (extsyminfo);
13026 }
13027 }
13028
13029 if (do_dynamic && filedata->dynamic_addr)
13030 {
13031 if (filedata->is_separate)
13032 printf (ngettext ("\nIn linked file '%s' the dynamic section at offset %#" PRIx64 " contains %" PRIu64 " entry:\n",
13033 "\nIn linked file '%s' the dynamic section at offset %#" PRIx64 " contains %" PRIu64 " entries:\n",
13034 filedata->dynamic_nent),
13035 filedata->file_name,
13036 filedata->dynamic_addr,
13037 filedata->dynamic_nent);
13038 else
13039 printf (ngettext ("\nDynamic section at offset %#" PRIx64 " contains %" PRIu64 " entry:\n",
13040 "\nDynamic section at offset %#" PRIx64 " contains %" PRIu64 " entries:\n",
13041 filedata->dynamic_nent),
13042 filedata->dynamic_addr,
13043 filedata->dynamic_nent);
13044 }
13045 if (do_dynamic)
13046 printf (_(" Tag Type Name/Value\n"));
13047
13048 for (entry = filedata->dynamic_section;
13049 entry < filedata->dynamic_section + filedata->dynamic_nent;
13050 entry++)
13051 {
13052 if (do_dynamic)
13053 {
13054 const char * dtype;
13055
13056 putchar (' ');
13057 print_vma (entry->d_tag, FULL_HEX);
13058 dtype = get_dynamic_type (filedata, entry->d_tag);
13059 printf (" (%s)%*s", dtype,
13060 ((is_32bit_elf ? 27 : 19) - (int) strlen (dtype)), " ");
13061 }
13062
13063 switch (entry->d_tag)
13064 {
13065 case DT_FLAGS:
13066 if (do_dynamic)
13067 print_dynamic_flags (entry->d_un.d_val);
13068 break;
13069
13070 case DT_AUXILIARY:
13071 case DT_FILTER:
13072 case DT_CONFIG:
13073 case DT_DEPAUDIT:
13074 case DT_AUDIT:
13075 if (do_dynamic)
13076 {
13077 switch (entry->d_tag)
13078 {
13079 case DT_AUXILIARY:
13080 printf (_("Auxiliary library"));
13081 break;
13082
13083 case DT_FILTER:
13084 printf (_("Filter library"));
13085 break;
13086
13087 case DT_CONFIG:
13088 printf (_("Configuration file"));
13089 break;
13090
13091 case DT_DEPAUDIT:
13092 printf (_("Dependency audit library"));
13093 break;
13094
13095 case DT_AUDIT:
13096 printf (_("Audit library"));
13097 break;
13098 }
13099
13100 if (valid_dynamic_name (filedata, entry->d_un.d_val))
13101 printf (": [%s]\n",
13102 get_dynamic_name (filedata, entry->d_un.d_val));
13103 else
13104 {
13105 printf (": ");
13106 print_vma (entry->d_un.d_val, PREFIX_HEX);
13107 putchar ('\n');
13108 }
13109 }
13110 break;
13111
13112 case DT_FEATURE:
13113 if (do_dynamic)
13114 {
13115 printf (_("Flags:"));
13116
13117 if (entry->d_un.d_val == 0)
13118 printf (_(" None\n"));
13119 else
13120 {
13121 uint64_t val = entry->d_un.d_val;
13122
13123 if (val & DTF_1_PARINIT)
13124 {
13125 printf (" PARINIT");
13126 val ^= DTF_1_PARINIT;
13127 }
13128 if (val & DTF_1_CONFEXP)
13129 {
13130 printf (" CONFEXP");
13131 val ^= DTF_1_CONFEXP;
13132 }
13133 if (val != 0)
13134 printf (" %" PRIx64, val);
13135 puts ("");
13136 }
13137 }
13138 break;
13139
13140 case DT_POSFLAG_1:
13141 if (do_dynamic)
13142 {
13143 printf (_("Flags:"));
13144
13145 if (entry->d_un.d_val == 0)
13146 printf (_(" None\n"));
13147 else
13148 {
13149 uint64_t val = entry->d_un.d_val;
13150
13151 if (val & DF_P1_LAZYLOAD)
13152 {
13153 printf (" LAZYLOAD");
13154 val ^= DF_P1_LAZYLOAD;
13155 }
13156 if (val & DF_P1_GROUPPERM)
13157 {
13158 printf (" GROUPPERM");
13159 val ^= DF_P1_GROUPPERM;
13160 }
13161 if (val != 0)
13162 printf (" %" PRIx64, val);
13163 puts ("");
13164 }
13165 }
13166 break;
13167
13168 case DT_FLAGS_1:
13169 if (do_dynamic)
13170 {
13171 printf (_("Flags:"));
13172 if (entry->d_un.d_val == 0)
13173 printf (_(" None\n"));
13174 else
13175 {
13176 uint64_t val = entry->d_un.d_val;
13177
13178 if (val & DF_1_NOW)
13179 {
13180 printf (" NOW");
13181 val ^= DF_1_NOW;
13182 }
13183 if (val & DF_1_GLOBAL)
13184 {
13185 printf (" GLOBAL");
13186 val ^= DF_1_GLOBAL;
13187 }
13188 if (val & DF_1_GROUP)
13189 {
13190 printf (" GROUP");
13191 val ^= DF_1_GROUP;
13192 }
13193 if (val & DF_1_NODELETE)
13194 {
13195 printf (" NODELETE");
13196 val ^= DF_1_NODELETE;
13197 }
13198 if (val & DF_1_LOADFLTR)
13199 {
13200 printf (" LOADFLTR");
13201 val ^= DF_1_LOADFLTR;
13202 }
13203 if (val & DF_1_INITFIRST)
13204 {
13205 printf (" INITFIRST");
13206 val ^= DF_1_INITFIRST;
13207 }
13208 if (val & DF_1_NOOPEN)
13209 {
13210 printf (" NOOPEN");
13211 val ^= DF_1_NOOPEN;
13212 }
13213 if (val & DF_1_ORIGIN)
13214 {
13215 printf (" ORIGIN");
13216 val ^= DF_1_ORIGIN;
13217 }
13218 if (val & DF_1_DIRECT)
13219 {
13220 printf (" DIRECT");
13221 val ^= DF_1_DIRECT;
13222 }
13223 if (val & DF_1_TRANS)
13224 {
13225 printf (" TRANS");
13226 val ^= DF_1_TRANS;
13227 }
13228 if (val & DF_1_INTERPOSE)
13229 {
13230 printf (" INTERPOSE");
13231 val ^= DF_1_INTERPOSE;
13232 }
13233 if (val & DF_1_NODEFLIB)
13234 {
13235 printf (" NODEFLIB");
13236 val ^= DF_1_NODEFLIB;
13237 }
13238 if (val & DF_1_NODUMP)
13239 {
13240 printf (" NODUMP");
13241 val ^= DF_1_NODUMP;
13242 }
13243 if (val & DF_1_CONFALT)
13244 {
13245 printf (" CONFALT");
13246 val ^= DF_1_CONFALT;
13247 }
13248 if (val & DF_1_ENDFILTEE)
13249 {
13250 printf (" ENDFILTEE");
13251 val ^= DF_1_ENDFILTEE;
13252 }
13253 if (val & DF_1_DISPRELDNE)
13254 {
13255 printf (" DISPRELDNE");
13256 val ^= DF_1_DISPRELDNE;
13257 }
13258 if (val & DF_1_DISPRELPND)
13259 {
13260 printf (" DISPRELPND");
13261 val ^= DF_1_DISPRELPND;
13262 }
13263 if (val & DF_1_NODIRECT)
13264 {
13265 printf (" NODIRECT");
13266 val ^= DF_1_NODIRECT;
13267 }
13268 if (val & DF_1_IGNMULDEF)
13269 {
13270 printf (" IGNMULDEF");
13271 val ^= DF_1_IGNMULDEF;
13272 }
13273 if (val & DF_1_NOKSYMS)
13274 {
13275 printf (" NOKSYMS");
13276 val ^= DF_1_NOKSYMS;
13277 }
13278 if (val & DF_1_NOHDR)
13279 {
13280 printf (" NOHDR");
13281 val ^= DF_1_NOHDR;
13282 }
13283 if (val & DF_1_EDITED)
13284 {
13285 printf (" EDITED");
13286 val ^= DF_1_EDITED;
13287 }
13288 if (val & DF_1_NORELOC)
13289 {
13290 printf (" NORELOC");
13291 val ^= DF_1_NORELOC;
13292 }
13293 if (val & DF_1_SYMINTPOSE)
13294 {
13295 printf (" SYMINTPOSE");
13296 val ^= DF_1_SYMINTPOSE;
13297 }
13298 if (val & DF_1_GLOBAUDIT)
13299 {
13300 printf (" GLOBAUDIT");
13301 val ^= DF_1_GLOBAUDIT;
13302 }
13303 if (val & DF_1_SINGLETON)
13304 {
13305 printf (" SINGLETON");
13306 val ^= DF_1_SINGLETON;
13307 }
13308 if (val & DF_1_STUB)
13309 {
13310 printf (" STUB");
13311 val ^= DF_1_STUB;
13312 }
13313 if (val & DF_1_PIE)
13314 {
13315 printf (" PIE");
13316 val ^= DF_1_PIE;
13317 }
13318 if (val & DF_1_KMOD)
13319 {
13320 printf (" KMOD");
13321 val ^= DF_1_KMOD;
13322 }
13323 if (val & DF_1_WEAKFILTER)
13324 {
13325 printf (" WEAKFILTER");
13326 val ^= DF_1_WEAKFILTER;
13327 }
13328 if (val & DF_1_NOCOMMON)
13329 {
13330 printf (" NOCOMMON");
13331 val ^= DF_1_NOCOMMON;
13332 }
13333 if (val != 0)
13334 printf (" %" PRIx64, val);
13335 puts ("");
13336 }
13337 }
13338 break;
13339
13340 case DT_PLTREL:
13341 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
13342 if (do_dynamic)
13343 puts (get_dynamic_type (filedata, entry->d_un.d_val));
13344 break;
13345
13346 case DT_NULL :
13347 case DT_NEEDED :
13348 case DT_PLTGOT :
13349 case DT_HASH :
13350 case DT_STRTAB :
13351 case DT_SYMTAB :
13352 case DT_RELA :
13353 case DT_INIT :
13354 case DT_FINI :
13355 case DT_SONAME :
13356 case DT_RPATH :
13357 case DT_SYMBOLIC:
13358 case DT_REL :
13359 case DT_RELR :
13360 case DT_DEBUG :
13361 case DT_TEXTREL :
13362 case DT_JMPREL :
13363 case DT_RUNPATH :
13364 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
13365
13366 if (do_dynamic)
13367 {
13368 const char *name;
13369
13370 if (valid_dynamic_name (filedata, entry->d_un.d_val))
13371 name = get_dynamic_name (filedata, entry->d_un.d_val);
13372 else
13373 name = NULL;
13374
13375 if (name)
13376 {
13377 switch (entry->d_tag)
13378 {
13379 case DT_NEEDED:
13380 printf (_("Shared library: [%s]"), name);
13381
13382 if (filedata->program_interpreter
13383 && streq (name, filedata->program_interpreter))
13384 printf (_(" program interpreter"));
13385 break;
13386
13387 case DT_SONAME:
13388 printf (_("Library soname: [%s]"), name);
13389 break;
13390
13391 case DT_RPATH:
13392 printf (_("Library rpath: [%s]"), name);
13393 break;
13394
13395 case DT_RUNPATH:
13396 printf (_("Library runpath: [%s]"), name);
13397 break;
13398
13399 default:
13400 print_vma (entry->d_un.d_val, PREFIX_HEX);
13401 break;
13402 }
13403 }
13404 else
13405 print_vma (entry->d_un.d_val, PREFIX_HEX);
13406
13407 putchar ('\n');
13408 }
13409 break;
13410
13411 case DT_PLTRELSZ:
13412 case DT_RELASZ :
13413 case DT_STRSZ :
13414 case DT_RELSZ :
13415 case DT_RELAENT :
13416 case DT_RELRENT :
13417 case DT_RELRSZ :
13418 case DT_SYMENT :
13419 case DT_RELENT :
13420 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
13421 /* Fall through. */
13422 case DT_PLTPADSZ:
13423 case DT_MOVEENT :
13424 case DT_MOVESZ :
13425 case DT_PREINIT_ARRAYSZ:
13426 case DT_INIT_ARRAYSZ:
13427 case DT_FINI_ARRAYSZ:
13428 case DT_GNU_CONFLICTSZ:
13429 case DT_GNU_LIBLISTSZ:
13430 if (do_dynamic)
13431 {
13432 print_vma (entry->d_un.d_val, UNSIGNED);
13433 printf (_(" (bytes)\n"));
13434 }
13435 break;
13436
13437 case DT_VERDEFNUM:
13438 case DT_VERNEEDNUM:
13439 case DT_RELACOUNT:
13440 case DT_RELCOUNT:
13441 if (do_dynamic)
13442 {
13443 print_vma (entry->d_un.d_val, UNSIGNED);
13444 putchar ('\n');
13445 }
13446 break;
13447
13448 case DT_SYMINSZ:
13449 case DT_SYMINENT:
13450 case DT_SYMINFO:
13451 case DT_USED:
13452 case DT_INIT_ARRAY:
13453 case DT_FINI_ARRAY:
13454 if (do_dynamic)
13455 {
13456 if (entry->d_tag == DT_USED
13457 && valid_dynamic_name (filedata, entry->d_un.d_val))
13458 {
13459 const char *name
13460 = get_dynamic_name (filedata, entry->d_un.d_val);
13461
13462 if (*name)
13463 {
13464 printf (_("Not needed object: [%s]\n"), name);
13465 break;
13466 }
13467 }
13468
13469 print_vma (entry->d_un.d_val, PREFIX_HEX);
13470 putchar ('\n');
13471 }
13472 break;
13473
13474 case DT_BIND_NOW:
13475 /* The value of this entry is ignored. */
13476 if (do_dynamic)
13477 putchar ('\n');
13478 break;
13479
13480 case DT_GNU_PRELINKED:
13481 if (do_dynamic)
13482 {
13483 struct tm * tmp;
13484 time_t atime = entry->d_un.d_val;
13485
13486 tmp = gmtime (&atime);
13487 /* PR 17533 file: 041-1244816-0.004. */
13488 if (tmp == NULL)
13489 printf (_("<corrupt time val: %" PRIx64),
13490 (uint64_t) atime);
13491 else
13492 printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
13493 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
13494 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
13495
13496 }
13497 break;
13498
13499 case DT_GNU_HASH:
13500 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
13501 if (do_dynamic)
13502 {
13503 print_vma (entry->d_un.d_val, PREFIX_HEX);
13504 putchar ('\n');
13505 }
13506 break;
13507
13508 case DT_GNU_FLAGS_1:
13509 if (do_dynamic)
13510 {
13511 printf (_("Flags:"));
13512 if (entry->d_un.d_val == 0)
13513 printf (_(" None\n"));
13514 else
13515 {
13516 uint64_t val = entry->d_un.d_val;
13517
13518 if (val & DF_GNU_1_UNIQUE)
13519 {
13520 printf (" UNIQUE");
13521 val ^= DF_GNU_1_UNIQUE;
13522 }
13523 if (val != 0)
13524 printf (" %" PRIx64, val);
13525 puts ("");
13526 }
13527 }
13528 break;
13529
13530 default:
13531 if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
13532 filedata->version_info[DT_VERSIONTAGIDX (entry->d_tag)]
13533 = entry->d_un.d_val;
13534
13535 if (do_dynamic)
13536 {
13537 switch (filedata->file_header.e_machine)
13538 {
13539 case EM_AARCH64:
13540 dynamic_section_aarch64_val (entry);
13541 break;
13542 case EM_MIPS:
13543 case EM_MIPS_RS3_LE:
13544 dynamic_section_mips_val (filedata, entry);
13545 break;
13546 case EM_PARISC:
13547 dynamic_section_parisc_val (entry);
13548 break;
13549 case EM_IA_64:
13550 dynamic_section_ia64_val (entry);
13551 break;
13552 default:
13553 print_vma (entry->d_un.d_val, PREFIX_HEX);
13554 putchar ('\n');
13555 }
13556 }
13557 break;
13558 }
13559 }
13560
13561 return true;
13562 }
13563
13564 static char *
13565 get_ver_flags (unsigned int flags)
13566 {
13567 static char buff[128];
13568
13569 buff[0] = 0;
13570
13571 if (flags == 0)
13572 return _("none");
13573
13574 if (flags & VER_FLG_BASE)
13575 strcat (buff, "BASE");
13576
13577 if (flags & VER_FLG_WEAK)
13578 {
13579 if (flags & VER_FLG_BASE)
13580 strcat (buff, " | ");
13581
13582 strcat (buff, "WEAK");
13583 }
13584
13585 if (flags & VER_FLG_INFO)
13586 {
13587 if (flags & (VER_FLG_BASE|VER_FLG_WEAK))
13588 strcat (buff, " | ");
13589
13590 strcat (buff, "INFO");
13591 }
13592
13593 if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
13594 {
13595 if (flags & (VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
13596 strcat (buff, " | ");
13597
13598 strcat (buff, _("<unknown>"));
13599 }
13600
13601 return buff;
13602 }
13603
13604 /* Display the contents of the version sections. */
13605
13606 static bool
13607 process_version_sections (Filedata * filedata)
13608 {
13609 Elf_Internal_Shdr * section;
13610 unsigned i;
13611 bool found = false;
13612
13613 if (! do_version)
13614 return true;
13615
13616 for (i = 0, section = filedata->section_headers;
13617 i < filedata->file_header.e_shnum;
13618 i++, section++)
13619 {
13620 switch (section->sh_type)
13621 {
13622 case SHT_GNU_verdef:
13623 {
13624 Elf_External_Verdef * edefs;
13625 size_t idx;
13626 size_t cnt;
13627 char * endbuf;
13628
13629 found = true;
13630
13631 if (filedata->is_separate)
13632 printf (ngettext ("\nIn linked file '%s' the version definition section '%s' contains %u entry:\n",
13633 "\nIn linked file '%s' the version definition section '%s' contains %u entries:\n",
13634 section->sh_info),
13635 filedata->file_name,
13636 printable_section_name (filedata, section),
13637 section->sh_info);
13638 else
13639 printf (ngettext ("\nVersion definition section '%s' "
13640 "contains %u entry:\n",
13641 "\nVersion definition section '%s' "
13642 "contains %u entries:\n",
13643 section->sh_info),
13644 printable_section_name (filedata, section),
13645 section->sh_info);
13646
13647 printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
13648 printf (_(" Offset: 0x%08" PRIx64 " Link: %u (%s)\n"),
13649 section->sh_offset, section->sh_link,
13650 printable_section_name_from_index (filedata, section->sh_link, NULL));
13651
13652 edefs = (Elf_External_Verdef *)
13653 get_data (NULL, filedata, section->sh_offset, 1,section->sh_size,
13654 _("version definition section"));
13655 if (!edefs)
13656 break;
13657 endbuf = (char *) edefs + section->sh_size;
13658
13659 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
13660 {
13661 char * vstart;
13662 Elf_External_Verdef * edef;
13663 Elf_Internal_Verdef ent;
13664 Elf_External_Verdaux * eaux;
13665 Elf_Internal_Verdaux aux;
13666 size_t isum;
13667 int j;
13668
13669 vstart = ((char *) edefs) + idx;
13670 if (vstart + sizeof (*edef) > endbuf)
13671 break;
13672
13673 edef = (Elf_External_Verdef *) vstart;
13674
13675 ent.vd_version = BYTE_GET (edef->vd_version);
13676 ent.vd_flags = BYTE_GET (edef->vd_flags);
13677 ent.vd_ndx = BYTE_GET (edef->vd_ndx);
13678 ent.vd_cnt = BYTE_GET (edef->vd_cnt);
13679 ent.vd_hash = BYTE_GET (edef->vd_hash);
13680 ent.vd_aux = BYTE_GET (edef->vd_aux);
13681 ent.vd_next = BYTE_GET (edef->vd_next);
13682
13683 printf (_(" %#06zx: Rev: %d Flags: %s"),
13684 idx, ent.vd_version, get_ver_flags (ent.vd_flags));
13685
13686 printf (_(" Index: %d Cnt: %d "),
13687 ent.vd_ndx, ent.vd_cnt);
13688
13689 /* Check for overflow. */
13690 if (ent.vd_aux > (size_t) (endbuf - vstart))
13691 break;
13692
13693 vstart += ent.vd_aux;
13694
13695 if (vstart + sizeof (*eaux) > endbuf)
13696 break;
13697 eaux = (Elf_External_Verdaux *) vstart;
13698
13699 aux.vda_name = BYTE_GET (eaux->vda_name);
13700 aux.vda_next = BYTE_GET (eaux->vda_next);
13701
13702 if (valid_dynamic_name (filedata, aux.vda_name))
13703 printf (_("Name: %s\n"),
13704 get_dynamic_name (filedata, aux.vda_name));
13705 else
13706 printf (_("Name index: %ld\n"), aux.vda_name);
13707
13708 isum = idx + ent.vd_aux;
13709
13710 for (j = 1; j < ent.vd_cnt; j++)
13711 {
13712 if (aux.vda_next < sizeof (*eaux)
13713 && !(j == ent.vd_cnt - 1 && aux.vda_next == 0))
13714 {
13715 warn (_("Invalid vda_next field of %lx\n"),
13716 aux.vda_next);
13717 j = ent.vd_cnt;
13718 break;
13719 }
13720 /* Check for overflow. */
13721 if (aux.vda_next > (size_t) (endbuf - vstart))
13722 break;
13723
13724 isum += aux.vda_next;
13725 vstart += aux.vda_next;
13726
13727 if (vstart + sizeof (*eaux) > endbuf)
13728 break;
13729 eaux = (Elf_External_Verdaux *) vstart;
13730
13731 aux.vda_name = BYTE_GET (eaux->vda_name);
13732 aux.vda_next = BYTE_GET (eaux->vda_next);
13733
13734 if (valid_dynamic_name (filedata, aux.vda_name))
13735 printf (_(" %#06zx: Parent %d: %s\n"),
13736 isum, j,
13737 get_dynamic_name (filedata, aux.vda_name));
13738 else
13739 printf (_(" %#06zx: Parent %d, name index: %ld\n"),
13740 isum, j, aux.vda_name);
13741 }
13742
13743 if (j < ent.vd_cnt)
13744 printf (_(" Version def aux past end of section\n"));
13745
13746 /* PR 17531:
13747 file: id:000001,src:000172+005151,op:splice,rep:2. */
13748 if (ent.vd_next < sizeof (*edef)
13749 && !(cnt == section->sh_info - 1 && ent.vd_next == 0))
13750 {
13751 warn (_("Invalid vd_next field of %lx\n"), ent.vd_next);
13752 cnt = section->sh_info;
13753 break;
13754 }
13755 if (ent.vd_next > (size_t) (endbuf - ((char *) edefs + idx)))
13756 break;
13757
13758 idx += ent.vd_next;
13759 }
13760
13761 if (cnt < section->sh_info)
13762 printf (_(" Version definition past end of section\n"));
13763
13764 free (edefs);
13765 }
13766 break;
13767
13768 case SHT_GNU_verneed:
13769 {
13770 Elf_External_Verneed * eneed;
13771 size_t idx;
13772 size_t cnt;
13773 char * endbuf;
13774
13775 found = true;
13776
13777 if (filedata->is_separate)
13778 printf (ngettext ("\nIn linked file '%s' the version needs section '%s' contains %u entry:\n",
13779 "\nIn linked file '%s' the version needs section '%s' contains %u entries:\n",
13780 section->sh_info),
13781 filedata->file_name,
13782 printable_section_name (filedata, section),
13783 section->sh_info);
13784 else
13785 printf (ngettext ("\nVersion needs section '%s' "
13786 "contains %u entry:\n",
13787 "\nVersion needs section '%s' "
13788 "contains %u entries:\n",
13789 section->sh_info),
13790 printable_section_name (filedata, section),
13791 section->sh_info);
13792
13793 printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
13794 printf (_(" Offset: 0x%08" PRIx64 " Link: %u (%s)\n"),
13795 section->sh_offset, section->sh_link,
13796 printable_section_name_from_index (filedata, section->sh_link, NULL));
13797
13798 eneed = (Elf_External_Verneed *) get_data (NULL, filedata,
13799 section->sh_offset, 1,
13800 section->sh_size,
13801 _("Version Needs section"));
13802 if (!eneed)
13803 break;
13804 endbuf = (char *) eneed + section->sh_size;
13805
13806 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
13807 {
13808 Elf_External_Verneed * entry;
13809 Elf_Internal_Verneed ent;
13810 size_t isum;
13811 int j;
13812 char * vstart;
13813
13814 vstart = ((char *) eneed) + idx;
13815 if (vstart + sizeof (*entry) > endbuf)
13816 break;
13817
13818 entry = (Elf_External_Verneed *) vstart;
13819
13820 ent.vn_version = BYTE_GET (entry->vn_version);
13821 ent.vn_cnt = BYTE_GET (entry->vn_cnt);
13822 ent.vn_file = BYTE_GET (entry->vn_file);
13823 ent.vn_aux = BYTE_GET (entry->vn_aux);
13824 ent.vn_next = BYTE_GET (entry->vn_next);
13825
13826 printf (_(" %#06zx: Version: %d"), idx, ent.vn_version);
13827
13828 if (valid_dynamic_name (filedata, ent.vn_file))
13829 printf (_(" File: %s"),
13830 get_dynamic_name (filedata, ent.vn_file));
13831 else
13832 printf (_(" File: %lx"), ent.vn_file);
13833
13834 printf (_(" Cnt: %d\n"), ent.vn_cnt);
13835
13836 /* Check for overflow. */
13837 if (ent.vn_aux > (size_t) (endbuf - vstart))
13838 break;
13839 vstart += ent.vn_aux;
13840
13841 for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
13842 {
13843 Elf_External_Vernaux * eaux;
13844 Elf_Internal_Vernaux aux;
13845
13846 if (vstart + sizeof (*eaux) > endbuf)
13847 break;
13848 eaux = (Elf_External_Vernaux *) vstart;
13849
13850 aux.vna_hash = BYTE_GET (eaux->vna_hash);
13851 aux.vna_flags = BYTE_GET (eaux->vna_flags);
13852 aux.vna_other = BYTE_GET (eaux->vna_other);
13853 aux.vna_name = BYTE_GET (eaux->vna_name);
13854 aux.vna_next = BYTE_GET (eaux->vna_next);
13855
13856 if (valid_dynamic_name (filedata, aux.vna_name))
13857 printf (_(" %#06zx: Name: %s"),
13858 isum, get_dynamic_name (filedata, aux.vna_name));
13859 else
13860 printf (_(" %#06zx: Name index: %lx"),
13861 isum, aux.vna_name);
13862
13863 printf (_(" Flags: %s Version: %d\n"),
13864 get_ver_flags (aux.vna_flags), aux.vna_other);
13865
13866 if (aux.vna_next < sizeof (*eaux)
13867 && !(j == ent.vn_cnt - 1 && aux.vna_next == 0))
13868 {
13869 warn (_("Invalid vna_next field of %lx\n"),
13870 aux.vna_next);
13871 j = ent.vn_cnt;
13872 break;
13873 }
13874 /* Check for overflow. */
13875 if (aux.vna_next > (size_t) (endbuf - vstart))
13876 break;
13877 isum += aux.vna_next;
13878 vstart += aux.vna_next;
13879 }
13880
13881 if (j < ent.vn_cnt)
13882 warn (_("Missing Version Needs auxiliary information\n"));
13883
13884 if (ent.vn_next < sizeof (*entry)
13885 && !(cnt == section->sh_info - 1 && ent.vn_next == 0))
13886 {
13887 warn (_("Invalid vn_next field of %lx\n"), ent.vn_next);
13888 cnt = section->sh_info;
13889 break;
13890 }
13891 if (ent.vn_next > (size_t) (endbuf - ((char *) eneed + idx)))
13892 break;
13893 idx += ent.vn_next;
13894 }
13895
13896 if (cnt < section->sh_info)
13897 warn (_("Missing Version Needs information\n"));
13898
13899 free (eneed);
13900 }
13901 break;
13902
13903 case SHT_GNU_versym:
13904 {
13905 Elf_Internal_Shdr * link_section;
13906 uint64_t total;
13907 unsigned int cnt;
13908 unsigned char * edata;
13909 unsigned short * data;
13910 char * strtab;
13911 Elf_Internal_Sym * symbols;
13912 Elf_Internal_Shdr * string_sec;
13913 uint64_t num_syms;
13914 uint64_t off;
13915
13916 if (section->sh_link >= filedata->file_header.e_shnum)
13917 break;
13918
13919 link_section = filedata->section_headers + section->sh_link;
13920 total = section->sh_size / sizeof (Elf_External_Versym);
13921
13922 if (link_section->sh_link >= filedata->file_header.e_shnum)
13923 break;
13924
13925 found = true;
13926
13927 symbols = get_elf_symbols (filedata, link_section, & num_syms);
13928 if (symbols == NULL)
13929 break;
13930
13931 string_sec = filedata->section_headers + link_section->sh_link;
13932
13933 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
13934 string_sec->sh_size,
13935 _("version string table"));
13936 if (!strtab)
13937 {
13938 free (symbols);
13939 break;
13940 }
13941
13942 if (filedata->is_separate)
13943 printf (ngettext ("\nIn linked file '%s' the version symbols section '%s' contains %" PRIu64 " entry:\n",
13944 "\nIn linked file '%s' the version symbols section '%s' contains %" PRIu64 " entries:\n",
13945 total),
13946 filedata->file_name,
13947 printable_section_name (filedata, section),
13948 total);
13949 else
13950 printf (ngettext ("\nVersion symbols section '%s' "
13951 "contains %" PRIu64 " entry:\n",
13952 "\nVersion symbols section '%s' "
13953 "contains %" PRIu64 " entries:\n",
13954 total),
13955 printable_section_name (filedata, section),
13956 total);
13957
13958 printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
13959 printf (_(" Offset: 0x%08" PRIx64 " Link: %u (%s)\n"),
13960 section->sh_offset, section->sh_link,
13961 printable_section_name (filedata, link_section));
13962
13963 off = offset_from_vma (filedata,
13964 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
13965 total * sizeof (short));
13966 edata = (unsigned char *) get_data (NULL, filedata, off,
13967 sizeof (short), total,
13968 _("version symbol data"));
13969 if (!edata)
13970 {
13971 free (strtab);
13972 free (symbols);
13973 break;
13974 }
13975
13976 data = (short unsigned int *) cmalloc (total, sizeof (short));
13977
13978 for (cnt = total; cnt --;)
13979 data[cnt] = byte_get (edata + cnt * sizeof (short),
13980 sizeof (short));
13981
13982 free (edata);
13983
13984 for (cnt = 0; cnt < total; cnt += 4)
13985 {
13986 int j, nn;
13987 char *name;
13988 char *invalid = _("*invalid*");
13989
13990 printf (" %03x:", cnt);
13991
13992 for (j = 0; (j < 4) && (cnt + j) < total; ++j)
13993 switch (data[cnt + j])
13994 {
13995 case 0:
13996 fputs (_(" 0 (*local*) "), stdout);
13997 break;
13998
13999 case 1:
14000 fputs (_(" 1 (*global*) "), stdout);
14001 break;
14002
14003 default:
14004 nn = printf ("%4x%c", data[cnt + j] & VERSYM_VERSION,
14005 data[cnt + j] & VERSYM_HIDDEN ? 'h' : ' ');
14006
14007 /* If this index value is greater than the size of the symbols
14008 array, break to avoid an out-of-bounds read. */
14009 if (cnt + j >= num_syms)
14010 {
14011 warn (_("invalid index into symbol array\n"));
14012 break;
14013 }
14014
14015 name = NULL;
14016 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
14017 {
14018 Elf_Internal_Verneed ivn;
14019 uint64_t offset;
14020
14021 offset = offset_from_vma
14022 (filedata,
14023 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
14024 sizeof (Elf_External_Verneed));
14025
14026 do
14027 {
14028 Elf_Internal_Vernaux ivna;
14029 Elf_External_Verneed evn;
14030 Elf_External_Vernaux evna;
14031 uint64_t a_off;
14032
14033 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
14034 _("version need")) == NULL)
14035 break;
14036
14037 ivn.vn_aux = BYTE_GET (evn.vn_aux);
14038 ivn.vn_next = BYTE_GET (evn.vn_next);
14039
14040 a_off = offset + ivn.vn_aux;
14041
14042 do
14043 {
14044 if (get_data (&evna, filedata, a_off, sizeof (evna),
14045 1, _("version need aux (2)")) == NULL)
14046 {
14047 ivna.vna_next = 0;
14048 ivna.vna_other = 0;
14049 }
14050 else
14051 {
14052 ivna.vna_next = BYTE_GET (evna.vna_next);
14053 ivna.vna_other = BYTE_GET (evna.vna_other);
14054 }
14055
14056 a_off += ivna.vna_next;
14057 }
14058 while (ivna.vna_other != data[cnt + j]
14059 && ivna.vna_next != 0);
14060
14061 if (ivna.vna_other == data[cnt + j])
14062 {
14063 ivna.vna_name = BYTE_GET (evna.vna_name);
14064
14065 if (ivna.vna_name >= string_sec->sh_size)
14066 name = invalid;
14067 else
14068 name = strtab + ivna.vna_name;
14069 break;
14070 }
14071
14072 offset += ivn.vn_next;
14073 }
14074 while (ivn.vn_next);
14075 }
14076
14077 if (data[cnt + j] != 0x8001
14078 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
14079 {
14080 Elf_Internal_Verdef ivd;
14081 Elf_External_Verdef evd;
14082 uint64_t offset;
14083
14084 offset = offset_from_vma
14085 (filedata,
14086 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
14087 sizeof evd);
14088
14089 do
14090 {
14091 if (get_data (&evd, filedata, offset, sizeof (evd), 1,
14092 _("version def")) == NULL)
14093 {
14094 ivd.vd_next = 0;
14095 /* PR 17531: file: 046-1082287-0.004. */
14096 ivd.vd_ndx = (data[cnt + j] & VERSYM_VERSION) + 1;
14097 break;
14098 }
14099 else
14100 {
14101 ivd.vd_next = BYTE_GET (evd.vd_next);
14102 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
14103 }
14104
14105 offset += ivd.vd_next;
14106 }
14107 while (ivd.vd_ndx != (data[cnt + j] & VERSYM_VERSION)
14108 && ivd.vd_next != 0);
14109
14110 if (ivd.vd_ndx == (data[cnt + j] & VERSYM_VERSION))
14111 {
14112 Elf_External_Verdaux evda;
14113 Elf_Internal_Verdaux ivda;
14114
14115 ivd.vd_aux = BYTE_GET (evd.vd_aux);
14116
14117 if (get_data (&evda, filedata,
14118 offset - ivd.vd_next + ivd.vd_aux,
14119 sizeof (evda), 1,
14120 _("version def aux")) == NULL)
14121 break;
14122
14123 ivda.vda_name = BYTE_GET (evda.vda_name);
14124
14125 if (ivda.vda_name >= string_sec->sh_size)
14126 name = invalid;
14127 else if (name != NULL && name != invalid)
14128 name = _("*both*");
14129 else
14130 name = strtab + ivda.vda_name;
14131 }
14132 }
14133 if (name != NULL)
14134 nn += printf ("(%s%-*s",
14135 name,
14136 12 - (int) strlen (name),
14137 ")");
14138
14139 if (nn < 18)
14140 printf ("%*c", 18 - nn, ' ');
14141 }
14142
14143 putchar ('\n');
14144 }
14145
14146 free (data);
14147 free (strtab);
14148 free (symbols);
14149 }
14150 break;
14151
14152 default:
14153 break;
14154 }
14155 }
14156
14157 if (! found)
14158 {
14159 if (filedata->is_separate)
14160 printf (_("\nNo version information found in linked file '%s'.\n"),
14161 filedata->file_name);
14162 else
14163 printf (_("\nNo version information found in this file.\n"));
14164 }
14165
14166 return true;
14167 }
14168
14169 static const char *
14170 get_symbol_binding (Filedata * filedata, unsigned int binding)
14171 {
14172 static char buff[64];
14173
14174 switch (binding)
14175 {
14176 case STB_LOCAL: return "LOCAL";
14177 case STB_GLOBAL: return "GLOBAL";
14178 case STB_WEAK: return "WEAK";
14179 default:
14180 if (binding >= STB_LOPROC && binding <= STB_HIPROC)
14181 snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
14182 binding);
14183 else if (binding >= STB_LOOS && binding <= STB_HIOS)
14184 {
14185 if (binding == STB_GNU_UNIQUE
14186 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU)
14187 return "UNIQUE";
14188 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
14189 }
14190 else
14191 snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
14192 return buff;
14193 }
14194 }
14195
14196 static const char *
14197 get_symbol_type (Filedata * filedata, unsigned int type)
14198 {
14199 static char buff[64];
14200
14201 switch (type)
14202 {
14203 case STT_NOTYPE: return "NOTYPE";
14204 case STT_OBJECT: return "OBJECT";
14205 case STT_FUNC: return "FUNC";
14206 case STT_SECTION: return "SECTION";
14207 case STT_FILE: return "FILE";
14208 case STT_COMMON: return "COMMON";
14209 case STT_TLS: return "TLS";
14210 case STT_RELC: return "RELC";
14211 case STT_SRELC: return "SRELC";
14212 default:
14213 if (type >= STT_LOPROC && type <= STT_HIPROC)
14214 {
14215 if (filedata->file_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
14216 return "THUMB_FUNC";
14217
14218 if (filedata->file_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
14219 return "REGISTER";
14220
14221 if (filedata->file_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
14222 return "PARISC_MILLI";
14223
14224 snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
14225 }
14226 else if (type >= STT_LOOS && type <= STT_HIOS)
14227 {
14228 if (filedata->file_header.e_machine == EM_PARISC)
14229 {
14230 if (type == STT_HP_OPAQUE)
14231 return "HP_OPAQUE";
14232 if (type == STT_HP_STUB)
14233 return "HP_STUB";
14234 }
14235
14236 if (type == STT_GNU_IFUNC
14237 && (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU
14238 || filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_FREEBSD))
14239 return "IFUNC";
14240
14241 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
14242 }
14243 else
14244 snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
14245 return buff;
14246 }
14247 }
14248
14249 static const char *
14250 get_symbol_visibility (unsigned int visibility)
14251 {
14252 switch (visibility)
14253 {
14254 case STV_DEFAULT: return "DEFAULT";
14255 case STV_INTERNAL: return "INTERNAL";
14256 case STV_HIDDEN: return "HIDDEN";
14257 case STV_PROTECTED: return "PROTECTED";
14258 default:
14259 error (_("Unrecognized visibility value: %u\n"), visibility);
14260 return _("<unknown>");
14261 }
14262 }
14263
14264 static const char *
14265 get_alpha_symbol_other (unsigned int other)
14266 {
14267 switch (other)
14268 {
14269 case STO_ALPHA_NOPV: return "NOPV";
14270 case STO_ALPHA_STD_GPLOAD: return "STD GPLOAD";
14271 default:
14272 error (_("Unrecognized alpha specific other value: %u\n"), other);
14273 return _("<unknown>");
14274 }
14275 }
14276
14277 static const char *
14278 get_solaris_symbol_visibility (unsigned int visibility)
14279 {
14280 switch (visibility)
14281 {
14282 case 4: return "EXPORTED";
14283 case 5: return "SINGLETON";
14284 case 6: return "ELIMINATE";
14285 default: return get_symbol_visibility (visibility);
14286 }
14287 }
14288
14289 static const char *
14290 get_aarch64_symbol_other (unsigned int other)
14291 {
14292 static char buf[32];
14293
14294 if (other & STO_AARCH64_VARIANT_PCS)
14295 {
14296 other &= ~STO_AARCH64_VARIANT_PCS;
14297 if (other == 0)
14298 return "VARIANT_PCS";
14299 snprintf (buf, sizeof buf, "VARIANT_PCS | %x", other);
14300 return buf;
14301 }
14302 return NULL;
14303 }
14304
14305 static const char *
14306 get_mips_symbol_other (unsigned int other)
14307 {
14308 switch (other)
14309 {
14310 case STO_OPTIONAL: return "OPTIONAL";
14311 case STO_MIPS_PLT: return "MIPS PLT";
14312 case STO_MIPS_PIC: return "MIPS PIC";
14313 case STO_MICROMIPS: return "MICROMIPS";
14314 case STO_MICROMIPS | STO_MIPS_PIC: return "MICROMIPS, MIPS PIC";
14315 case STO_MIPS16: return "MIPS16";
14316 default: return NULL;
14317 }
14318 }
14319
14320 static const char *
14321 get_ia64_symbol_other (Filedata * filedata, unsigned int other)
14322 {
14323 if (is_ia64_vms (filedata))
14324 {
14325 static char res[32];
14326
14327 res[0] = 0;
14328
14329 /* Function types is for images and .STB files only. */
14330 switch (filedata->file_header.e_type)
14331 {
14332 case ET_DYN:
14333 case ET_EXEC:
14334 switch (VMS_ST_FUNC_TYPE (other))
14335 {
14336 case VMS_SFT_CODE_ADDR:
14337 strcat (res, " CA");
14338 break;
14339 case VMS_SFT_SYMV_IDX:
14340 strcat (res, " VEC");
14341 break;
14342 case VMS_SFT_FD:
14343 strcat (res, " FD");
14344 break;
14345 case VMS_SFT_RESERVE:
14346 strcat (res, " RSV");
14347 break;
14348 default:
14349 warn (_("Unrecognized IA64 VMS ST Function type: %d\n"),
14350 VMS_ST_FUNC_TYPE (other));
14351 strcat (res, " <unknown>");
14352 break;
14353 }
14354 break;
14355 default:
14356 break;
14357 }
14358 switch (VMS_ST_LINKAGE (other))
14359 {
14360 case VMS_STL_IGNORE:
14361 strcat (res, " IGN");
14362 break;
14363 case VMS_STL_RESERVE:
14364 strcat (res, " RSV");
14365 break;
14366 case VMS_STL_STD:
14367 strcat (res, " STD");
14368 break;
14369 case VMS_STL_LNK:
14370 strcat (res, " LNK");
14371 break;
14372 default:
14373 warn (_("Unrecognized IA64 VMS ST Linkage: %d\n"),
14374 VMS_ST_LINKAGE (other));
14375 strcat (res, " <unknown>");
14376 break;
14377 }
14378
14379 if (res[0] != 0)
14380 return res + 1;
14381 else
14382 return res;
14383 }
14384 return NULL;
14385 }
14386
14387 static const char *
14388 get_ppc64_symbol_other (unsigned int other)
14389 {
14390 if ((other & ~STO_PPC64_LOCAL_MASK) != 0)
14391 return NULL;
14392
14393 other >>= STO_PPC64_LOCAL_BIT;
14394 if (other <= 6)
14395 {
14396 static char buf[64];
14397 if (other >= 2)
14398 other = ppc64_decode_local_entry (other);
14399 snprintf (buf, sizeof buf, _("<localentry>: %d"), other);
14400 return buf;
14401 }
14402 return NULL;
14403 }
14404
14405 static const char *
14406 get_riscv_symbol_other (unsigned int other)
14407 {
14408 static char buf[32];
14409 buf[0] = 0;
14410
14411 if (other & STO_RISCV_VARIANT_CC)
14412 {
14413 strcat (buf, _(" VARIANT_CC"));
14414 other &= ~STO_RISCV_VARIANT_CC;
14415 }
14416
14417 if (other != 0)
14418 snprintf (buf, sizeof buf, " %x", other);
14419
14420
14421 if (buf[0] != 0)
14422 return buf + 1;
14423 else
14424 return buf;
14425 }
14426
14427 static const char *
14428 get_symbol_other (Filedata * filedata, unsigned int other)
14429 {
14430 const char * result = NULL;
14431 static char buff [64];
14432
14433 if (other == 0)
14434 return "";
14435
14436 switch (filedata->file_header.e_machine)
14437 {
14438 case EM_ALPHA:
14439 result = get_alpha_symbol_other (other);
14440 break;
14441 case EM_AARCH64:
14442 result = get_aarch64_symbol_other (other);
14443 break;
14444 case EM_MIPS:
14445 result = get_mips_symbol_other (other);
14446 break;
14447 case EM_IA_64:
14448 result = get_ia64_symbol_other (filedata, other);
14449 break;
14450 case EM_PPC64:
14451 result = get_ppc64_symbol_other (other);
14452 break;
14453 case EM_RISCV:
14454 result = get_riscv_symbol_other (other);
14455 break;
14456 default:
14457 result = NULL;
14458 break;
14459 }
14460
14461 if (result)
14462 return result;
14463
14464 snprintf (buff, sizeof buff, _("<other>: %x"), other);
14465 return buff;
14466 }
14467
14468 static const char *
14469 get_symbol_version_string (Filedata *filedata,
14470 bool is_dynsym,
14471 const char *strtab,
14472 size_t strtab_size,
14473 unsigned int si,
14474 Elf_Internal_Sym *psym,
14475 enum versioned_symbol_info *sym_info,
14476 unsigned short *vna_other)
14477 {
14478 unsigned char data[2];
14479 unsigned short vers_data;
14480 uint64_t offset;
14481 unsigned short max_vd_ndx;
14482
14483 if (!is_dynsym
14484 || filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)] == 0)
14485 return NULL;
14486
14487 offset = offset_from_vma (filedata,
14488 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
14489 sizeof data + si * sizeof (vers_data));
14490
14491 if (get_data (&data, filedata, offset + si * sizeof (vers_data),
14492 sizeof (data), 1, _("version data")) == NULL)
14493 return NULL;
14494
14495 vers_data = byte_get (data, 2);
14496
14497 if ((vers_data & VERSYM_HIDDEN) == 0 && vers_data == 0)
14498 return NULL;
14499
14500 *sym_info = (vers_data & VERSYM_HIDDEN) != 0 ? symbol_hidden : symbol_public;
14501 max_vd_ndx = 0;
14502
14503 /* Usually we'd only see verdef for defined symbols, and verneed for
14504 undefined symbols. However, symbols defined by the linker in
14505 .dynbss for variables copied from a shared library in order to
14506 avoid text relocations are defined yet have verneed. We could
14507 use a heuristic to detect the special case, for example, check
14508 for verneed first on symbols defined in SHT_NOBITS sections, but
14509 it is simpler and more reliable to just look for both verdef and
14510 verneed. .dynbss might not be mapped to a SHT_NOBITS section. */
14511
14512 if (psym->st_shndx != SHN_UNDEF
14513 && vers_data != 0x8001
14514 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
14515 {
14516 Elf_Internal_Verdef ivd;
14517 Elf_Internal_Verdaux ivda;
14518 Elf_External_Verdaux evda;
14519 uint64_t off;
14520
14521 off = offset_from_vma (filedata,
14522 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
14523 sizeof (Elf_External_Verdef));
14524
14525 do
14526 {
14527 Elf_External_Verdef evd;
14528
14529 if (get_data (&evd, filedata, off, sizeof (evd), 1,
14530 _("version def")) == NULL)
14531 {
14532 ivd.vd_ndx = 0;
14533 ivd.vd_aux = 0;
14534 ivd.vd_next = 0;
14535 ivd.vd_flags = 0;
14536 }
14537 else
14538 {
14539 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
14540 ivd.vd_aux = BYTE_GET (evd.vd_aux);
14541 ivd.vd_next = BYTE_GET (evd.vd_next);
14542 ivd.vd_flags = BYTE_GET (evd.vd_flags);
14543 }
14544
14545 if ((ivd.vd_ndx & VERSYM_VERSION) > max_vd_ndx)
14546 max_vd_ndx = ivd.vd_ndx & VERSYM_VERSION;
14547
14548 off += ivd.vd_next;
14549 }
14550 while (ivd.vd_ndx != (vers_data & VERSYM_VERSION) && ivd.vd_next != 0);
14551
14552 if (ivd.vd_ndx == (vers_data & VERSYM_VERSION))
14553 {
14554 if (ivd.vd_ndx == 1 && ivd.vd_flags == VER_FLG_BASE)
14555 return NULL;
14556
14557 off -= ivd.vd_next;
14558 off += ivd.vd_aux;
14559
14560 if (get_data (&evda, filedata, off, sizeof (evda), 1,
14561 _("version def aux")) != NULL)
14562 {
14563 ivda.vda_name = BYTE_GET (evda.vda_name);
14564
14565 if (psym->st_name != ivda.vda_name)
14566 return (ivda.vda_name < strtab_size
14567 ? strtab + ivda.vda_name : _("<corrupt>"));
14568 }
14569 }
14570 }
14571
14572 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
14573 {
14574 Elf_External_Verneed evn;
14575 Elf_Internal_Verneed ivn;
14576 Elf_Internal_Vernaux ivna;
14577
14578 offset = offset_from_vma (filedata,
14579 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
14580 sizeof evn);
14581 do
14582 {
14583 uint64_t vna_off;
14584
14585 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
14586 _("version need")) == NULL)
14587 {
14588 ivna.vna_next = 0;
14589 ivna.vna_other = 0;
14590 ivna.vna_name = 0;
14591 break;
14592 }
14593
14594 ivn.vn_aux = BYTE_GET (evn.vn_aux);
14595 ivn.vn_next = BYTE_GET (evn.vn_next);
14596
14597 vna_off = offset + ivn.vn_aux;
14598
14599 do
14600 {
14601 Elf_External_Vernaux evna;
14602
14603 if (get_data (&evna, filedata, vna_off, sizeof (evna), 1,
14604 _("version need aux (3)")) == NULL)
14605 {
14606 ivna.vna_next = 0;
14607 ivna.vna_other = 0;
14608 ivna.vna_name = 0;
14609 }
14610 else
14611 {
14612 ivna.vna_other = BYTE_GET (evna.vna_other);
14613 ivna.vna_next = BYTE_GET (evna.vna_next);
14614 ivna.vna_name = BYTE_GET (evna.vna_name);
14615 }
14616
14617 vna_off += ivna.vna_next;
14618 }
14619 while (ivna.vna_other != vers_data && ivna.vna_next != 0);
14620
14621 if (ivna.vna_other == vers_data)
14622 break;
14623
14624 offset += ivn.vn_next;
14625 }
14626 while (ivn.vn_next != 0);
14627
14628 if (ivna.vna_other == vers_data)
14629 {
14630 *sym_info = symbol_undefined;
14631 *vna_other = ivna.vna_other;
14632 return (ivna.vna_name < strtab_size
14633 ? strtab + ivna.vna_name : _("<corrupt>"));
14634 }
14635 else if ((max_vd_ndx || (vers_data & VERSYM_VERSION) != 1)
14636 && (vers_data & VERSYM_VERSION) > max_vd_ndx)
14637 return _("<corrupt>");
14638 }
14639 return NULL;
14640 }
14641
14642 /* Display a symbol size on stdout. Format is based on --sym-base setting. */
14643
14644 static unsigned int
14645 print_symbol_size (uint64_t vma, int base)
14646 {
14647 switch (base)
14648 {
14649 case 8:
14650 return print_vma (vma, OCTAL_5);
14651
14652 case 10:
14653 return print_vma (vma, UNSIGNED_5);
14654
14655 case 16:
14656 return print_vma (vma, PREFIX_HEX_5);
14657
14658 case 0:
14659 default:
14660 return print_vma (vma, DEC_5);
14661 }
14662 }
14663
14664 /* Print information on a single symbol. */
14665
14666 static void
14667 print_symbol (Filedata * filedata,
14668 uint64_t symbol_index,
14669 Elf_Internal_Sym * symtab,
14670 Elf_Internal_Shdr * section,
14671 char * strtab,
14672 size_t strtab_size)
14673 {
14674 const char *version_string;
14675 enum versioned_symbol_info sym_info;
14676 unsigned short vna_other;
14677 const char * sstr;
14678 Elf_Internal_Sym *psym = symtab + symbol_index;
14679
14680 /* FIXME: We should have a table of field widths,
14681 rather than using hard coded constants. */
14682 printf ("%6" PRId64 ": ", symbol_index);
14683 print_vma (psym->st_value, LONG_HEX);
14684 putchar (' ');
14685 print_symbol_size (psym->st_size, sym_base);
14686 printf (" %-7s", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
14687 printf (" %-6s", get_symbol_binding (filedata, ELF_ST_BIND (psym->st_info)));
14688 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
14689 printf (" %-7s", get_solaris_symbol_visibility (psym->st_other));
14690 else
14691 {
14692 unsigned int vis = ELF_ST_VISIBILITY (psym->st_other);
14693
14694 printf (" %-7s", get_symbol_visibility (vis));
14695
14696 /* Check to see if any other bits in the st_other field are set.
14697 FIXME: Displaying this information here disrupts the layout
14698 of the table being generated. */
14699 if (psym->st_other ^ vis)
14700 printf (" [%s] ", get_symbol_other (filedata, psym->st_other ^ vis));
14701 }
14702
14703 bool is_special;
14704
14705 sstr = printable_section_name_from_index (filedata, psym->st_shndx, & is_special);
14706
14707 /* Print the symbol's section index. If the index is special
14708 then print the index's name rather than its number. */
14709 if (is_special)
14710 {
14711 int printed;
14712
14713 /* Special case: If there are no section headers, and the printable
14714 name is "<section 0x...." then just display the section number
14715 as a decimal. This happens when objcopy --strip -section-headers
14716 is used. */
14717 if (filedata->file_header.e_shnum == 0 && startswith (sstr, "<section"))
14718 printed = printf (" %4d ", psym->st_shndx);
14719 else
14720 printed = printf (" %4s ", sstr);
14721
14722 if (extra_sym_info && printed < 16)
14723 printf ("%*s", 16 - printed, "");
14724 }
14725 else
14726 {
14727 printf (" %4u ", psym->st_shndx);
14728
14729 if (extra_sym_info)
14730 {
14731 /* Display the section name referenced by the section index. */
14732 int printed = printf ("(%s) ", sstr);
14733 if (printed < 10)
14734 printf ("%*s", 10 - printed, "");
14735 }
14736 }
14737
14738 /* Get the symbol's name. For section symbols without a
14739 specific name use the (already computed) section name. */
14740 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION
14741 && section_index_real (filedata, psym->st_shndx)
14742 && psym->st_name == 0)
14743 {
14744 ;
14745 }
14746 else
14747 {
14748 bool is_valid;
14749
14750 is_valid = valid_symbol_name (strtab, strtab_size, psym->st_name);
14751 sstr = is_valid ? strtab + psym->st_name : _("<corrupt>");
14752 }
14753
14754 version_string
14755 = get_symbol_version_string (filedata,
14756 (section == NULL
14757 || section->sh_type == SHT_DYNSYM),
14758 strtab, strtab_size, symbol_index,
14759 psym, &sym_info, &vna_other);
14760
14761 int len_avail = 21;
14762 if (! do_wide && version_string != NULL)
14763 {
14764 char buffer[16];
14765
14766 len_avail -= 1 + strlen (version_string);
14767
14768 if (sym_info == symbol_undefined)
14769 len_avail -= sprintf (buffer," (%d)", vna_other);
14770 else if (sym_info != symbol_hidden)
14771 len_avail -= 1;
14772 }
14773
14774 print_symbol_name (len_avail, sstr);
14775
14776 if (version_string)
14777 {
14778 if (sym_info == symbol_undefined)
14779 printf ("@%s (%d)", version_string, vna_other);
14780 else
14781 printf (sym_info == symbol_hidden ? "@%s" : "@@%s",
14782 version_string);
14783 }
14784
14785 putchar ('\n');
14786
14787 if (ELF_ST_BIND (psym->st_info) == STB_LOCAL
14788 && section != NULL
14789 && symbol_index >= section->sh_info
14790 /* Irix 5 and 6 MIPS binaries are known to ignore this requirement. */
14791 && filedata->file_header.e_machine != EM_MIPS
14792 /* Solaris binaries have been found to violate this requirement as
14793 well. Not sure if this is a bug or an ABI requirement. */
14794 && filedata->file_header.e_ident[EI_OSABI] != ELFOSABI_SOLARIS)
14795 warn (_("local symbol %" PRIu64 " found at index >= %s's sh_info value of %u\n"),
14796 symbol_index, printable_section_name (filedata, section), section->sh_info);
14797 }
14798
14799 static const char *
14800 get_lto_kind (unsigned int kind)
14801 {
14802 switch (kind)
14803 {
14804 case 0: return "DEF";
14805 case 1: return "WEAKDEF";
14806 case 2: return "UNDEF";
14807 case 3: return "WEAKUNDEF";
14808 case 4: return "COMMON";
14809 default:
14810 break;
14811 }
14812
14813 static char buffer[30];
14814 error (_("Unknown LTO symbol definition encountered: %u\n"), kind);
14815 sprintf (buffer, "<unknown: %u>", kind);
14816 return buffer;
14817 }
14818
14819 static const char *
14820 get_lto_visibility (unsigned int visibility)
14821 {
14822 switch (visibility)
14823 {
14824 case 0: return "DEFAULT";
14825 case 1: return "PROTECTED";
14826 case 2: return "INTERNAL";
14827 case 3: return "HIDDEN";
14828 default:
14829 break;
14830 }
14831
14832 static char buffer[30];
14833 error (_("Unknown LTO symbol visibility encountered: %u\n"), visibility);
14834 sprintf (buffer, "<unknown: %u>", visibility);
14835 return buffer;
14836 }
14837
14838 static const char *
14839 get_lto_sym_type (unsigned int sym_type)
14840 {
14841 switch (sym_type)
14842 {
14843 case 0: return "UNKNOWN";
14844 case 1: return "FUNCTION";
14845 case 2: return "VARIABLE";
14846 default:
14847 break;
14848 }
14849
14850 static char buffer[30];
14851 error (_("Unknown LTO symbol type encountered: %u\n"), sym_type);
14852 sprintf (buffer, "<unknown: %u>", sym_type);
14853 return buffer;
14854 }
14855
14856 /* Display an LTO format symbol table.
14857 FIXME: The format of LTO symbol tables is not formalized.
14858 So this code could need changing in the future. */
14859
14860 static bool
14861 display_lto_symtab (Filedata * filedata,
14862 Elf_Internal_Shdr * section)
14863 {
14864 if (section->sh_size == 0)
14865 {
14866 if (filedata->is_separate)
14867 printf (_("\nThe LTO Symbol table section '%s' in linked file '%s' is empty!\n"),
14868 printable_section_name (filedata, section),
14869 filedata->file_name);
14870 else
14871 printf (_("\nLTO Symbol table '%s' is empty!\n"),
14872 printable_section_name (filedata, section));
14873
14874 return true;
14875 }
14876
14877 if (section->sh_size > filedata->file_size)
14878 {
14879 error (_("Section %s has an invalid sh_size of %#" PRIx64 "\n"),
14880 printable_section_name (filedata, section),
14881 section->sh_size);
14882 return false;
14883 }
14884
14885 void * alloced_data = get_data (NULL, filedata, section->sh_offset,
14886 section->sh_size, 1, _("LTO symbols"));
14887 if (alloced_data == NULL)
14888 return false;
14889
14890 /* Look for extended data for the symbol table. */
14891 void * ext_data_orig = NULL;
14892 char * ext_data = NULL;
14893 char * ext_data_end = NULL;
14894 char *ext_name = xasprintf (".gnu.lto_.ext_symtab.%s",
14895 (section_name (filedata, section)
14896 + sizeof (".gnu.lto_.symtab.")));
14897 Elf_Internal_Shdr *ext = find_section (filedata, ext_name);
14898 if (ext != NULL)
14899 {
14900 if (ext->sh_size < 3)
14901 error (_("LTO Symbol extension table '%s' is empty!\n"),
14902 printable_section_name (filedata, ext));
14903 else
14904 {
14905 ext_data_orig = ext_data = get_data (NULL, filedata, ext->sh_offset,
14906 ext->sh_size, 1,
14907 _("LTO ext symbol data"));
14908 if (ext_data != NULL)
14909 {
14910 ext_data_end = ext_data + ext->sh_size;
14911 if (* ext_data++ != 1)
14912 error (_("Unexpected version number in symbol extension table\n"));
14913 }
14914 }
14915 }
14916
14917 const unsigned char * data = (const unsigned char *) alloced_data;
14918 const unsigned char * end = data + section->sh_size;
14919
14920 if (filedata->is_separate)
14921 printf (_("\nIn linked file '%s': "), filedata->file_name);
14922 else
14923 printf ("\n");
14924
14925 if (ext_data_orig != NULL)
14926 {
14927 if (do_wide)
14928 printf (_("LTO Symbol table '%s' and extension table '%s' contain:\n"),
14929 printable_section_name (filedata, section),
14930 printable_section_name (filedata, ext));
14931 else
14932 {
14933 printf (_("LTO Symbol table '%s'\n"),
14934 printable_section_name (filedata, section));
14935 printf (_(" and extension table '%s' contain:\n"),
14936 printable_section_name (filedata, ext));
14937 }
14938 }
14939 else
14940 printf (_("LTO Symbol table '%s' contains:\n"),
14941 printable_section_name (filedata, section));
14942
14943 /* FIXME: Add a wide version. */
14944 if (ext_data_orig != NULL)
14945 printf (_(" Comdat_Key Kind Visibility Size Slot Type Section Name\n"));
14946 else
14947 printf (_(" Comdat_Key Kind Visibility Size Slot Name\n"));
14948
14949 /* FIXME: We do not handle style prefixes. */
14950
14951 while (data < end)
14952 {
14953 const unsigned char * sym_name = data;
14954 data += strnlen ((const char *) sym_name, end - data) + 1;
14955 if (data >= end)
14956 goto fail;
14957
14958 const unsigned char * comdat_key = data;
14959 data += strnlen ((const char *) comdat_key, end - data) + 1;
14960 if (data >= end)
14961 goto fail;
14962
14963 if (data + 2 + 8 + 4 > end)
14964 goto fail;
14965
14966 unsigned int kind = *data++;
14967 unsigned int visibility = *data++;
14968
14969 uint64_t size = byte_get (data, 8);
14970 data += 8;
14971
14972 uint64_t slot = byte_get (data, 4);
14973 data += 4;
14974
14975 if (ext_data != NULL)
14976 {
14977 if (ext_data < (ext_data_end - 1))
14978 {
14979 unsigned int sym_type = * ext_data ++;
14980 unsigned int sec_kind = * ext_data ++;
14981
14982 printf (" %10s %10s %11s %08" PRIx64 " %08" PRIx64 " %9s %08x _",
14983 * comdat_key == 0 ? "-" : (char *) comdat_key,
14984 get_lto_kind (kind),
14985 get_lto_visibility (visibility),
14986 size,
14987 slot,
14988 get_lto_sym_type (sym_type),
14989 sec_kind);
14990 print_symbol_name (6, (const char *) sym_name);
14991 }
14992 else
14993 {
14994 error (_("Ran out of LTO symbol extension data\n"));
14995 ext_data = NULL;
14996 /* FIXME: return FAIL result ? */
14997 }
14998 }
14999 else
15000 {
15001 printf (" %10s %10s %11s %08" PRIx64 " %08" PRIx64 " _",
15002 * comdat_key == 0 ? "-" : (char *) comdat_key,
15003 get_lto_kind (kind),
15004 get_lto_visibility (visibility),
15005 size,
15006 slot);
15007 print_symbol_name (21, (const char *) sym_name);
15008 }
15009 putchar ('\n');
15010 }
15011
15012 if (ext_data != NULL && ext_data < ext_data_end)
15013 {
15014 error (_("Data remains in the LTO symbol extension table\n"));
15015 goto fail;
15016 }
15017
15018 free (alloced_data);
15019 free (ext_data_orig);
15020 free (ext_name);
15021 return true;
15022
15023 fail:
15024 error (_("Buffer overrun encountered whilst decoding LTO symbol table\n"));
15025 free (alloced_data);
15026 free (ext_data_orig);
15027 free (ext_name);
15028 return false;
15029 }
15030
15031 /* Display LTO symbol tables. */
15032
15033 static bool
15034 process_lto_symbol_tables (Filedata * filedata)
15035 {
15036 Elf_Internal_Shdr * section;
15037 unsigned int i;
15038 bool res = true;
15039
15040 if (!do_lto_syms)
15041 return true;
15042
15043 if (filedata->section_headers == NULL)
15044 return true;
15045
15046 for (i = 0, section = filedata->section_headers;
15047 i < filedata->file_header.e_shnum;
15048 i++, section++)
15049 if (section_name_valid (filedata, section)
15050 && startswith (section_name (filedata, section), ".gnu.lto_.symtab."))
15051 res &= display_lto_symtab (filedata, section);
15052
15053 return res;
15054 }
15055
15056 static void
15057 print_symbol_table_heading (void)
15058 {
15059 /* FIXME: We should store the size of each field in the display in a table and
15060 then use the values inside print_symbol(), instead of that function using
15061 hard coded constants. */
15062 if (is_32bit_elf)
15063 {
15064 if (extra_sym_info)
15065 {
15066 printf (_(" Num: Value Size Type Bind Vis+Other Ndx(SecName) Name [+ Version Info]\n"));
15067 /* |--6-|: |--8---| |-5-| |--7--| |-6--| |--7--| |---8--| |----13.....| |........... */
15068 /* eg: 5: 00000000 14 FUNC LOCAL DEFAULT 1 (.text) get_sections */
15069 }
15070 else if (do_wide)
15071 {
15072 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
15073 /* |--6-|: |--8---| |-5-| |--7--| |-6--| |--7--| |-4| |........... */
15074 /* eg: 5: 00000000 14 FUNC LOCAL DEFAULT 1 get_sections */
15075 }
15076 else
15077 {
15078 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
15079 /* |--6-|: |--8---| |-5-| |--7--| |-6--| |--7--| |-4| |------------29-------------| */
15080 /* eg: 5: 00000000 14 FUNC LOCAL DEFAULT 1 get_sections */
15081 }
15082 }
15083 else
15084 {
15085 if (extra_sym_info)
15086 {
15087 printf (_(" Num: Value Size Type Bind Vis+Other Ndx(SecName) Name [+ Version Info]\n"));
15088 /* |--6-|: |------16------| |-5-| |--7--| |-6--| |--7--| |-------14---| |..... */
15089 /* eg: 2: 0000000000000000 0 FUNC LOCAL DEFAULT 1 (.text) .very_long_function_name */
15090
15091 }
15092 else if (do_wide)
15093 {
15094 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
15095 /* |--6-|: |------16------| |-5-| |--7--| |-6--| |--7--| |-4| |........... */
15096 /* eg: 5: 0000000000000000 14 FUNC LOCAL DEFAULT 1 very_long_function_name */
15097 }
15098 else
15099 {
15100 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
15101 /* |--6-|: |------16------| |-5-| |--7--| |-6--| |--7--| |-4| |--------21---------| */
15102 /* eg: 5: 0000000000000000 14 FUNC LOCAL DEFAULT 1 very_long_functi[...] */
15103 }
15104 }
15105 }
15106
15107 static bool
15108 dump_symbol_section (Elf_Internal_Shdr * section,
15109 Filedata * filedata)
15110 {
15111 if (section->sh_entsize == 0)
15112 {
15113 printf (_("\nSymbol table '%s' has a sh_entsize of zero!\n"),
15114 printable_section_name (filedata, section));
15115 return false;
15116 }
15117
15118 uint64_t num_syms = section->sh_size / section->sh_entsize;
15119
15120 if (filedata->is_separate)
15121 printf (ngettext ("\nIn linked file '%s' symbol section '%s'"
15122 " contains %" PRIu64 " entry:\n",
15123 "\nIn linked file '%s' symbol section '%s'"
15124 " contains %" PRIu64 " entries:\n",
15125 num_syms),
15126 filedata->file_name,
15127 printable_section_name (filedata, section),
15128 num_syms);
15129 else
15130 printf (ngettext ("\nSymbol table '%s' contains %" PRIu64
15131 " entry:\n",
15132 "\nSymbol table '%s' contains %" PRIu64
15133 " entries:\n",
15134 num_syms),
15135 printable_section_name (filedata, section),
15136 num_syms);
15137
15138 print_symbol_table_heading ();
15139
15140 Elf_Internal_Sym * symtab = get_elf_symbols (filedata, section, & num_syms);
15141 if (symtab == NULL)
15142 /* An error message will have already been displayed. */
15143 return false;
15144
15145 char * strtab = NULL;
15146 uint64_t strtab_size = 0;
15147
15148 if (section->sh_link == filedata->file_header.e_shstrndx)
15149 {
15150 strtab = filedata->string_table;
15151 strtab_size = filedata->string_table_length;
15152 }
15153 else if (section->sh_link < filedata->file_header.e_shnum)
15154 {
15155 Elf_Internal_Shdr * string_sec;
15156
15157 string_sec = filedata->section_headers + section->sh_link;
15158
15159 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset,
15160 1, string_sec->sh_size,
15161 _("string table"));
15162 strtab_size = strtab != NULL ? string_sec->sh_size : 0;
15163 }
15164
15165 uint64_t si;
15166
15167 for (si = 0; si < num_syms; si++)
15168 print_symbol (filedata, si, symtab, section, strtab, strtab_size);
15169
15170 free (symtab);
15171
15172 if (strtab != filedata->string_table)
15173 free (strtab);
15174
15175 return true;
15176 }
15177
15178 /* Dump the symbol table. */
15179
15180 static bool
15181 process_symbol_table (Filedata * filedata)
15182 {
15183 Elf_Internal_Shdr * section;
15184
15185 if (!do_syms && !do_dyn_syms && !do_histogram)
15186 return true;
15187
15188 if ((filedata->dynamic_info[DT_HASH] || filedata->dynamic_info_DT_GNU_HASH)
15189 && do_syms
15190 && do_using_dynamic
15191 && filedata->dynamic_strings != NULL
15192 && filedata->dynamic_symbols != NULL)
15193 {
15194 uint64_t si;
15195
15196 if (filedata->is_separate)
15197 {
15198 printf (ngettext ("\nIn linked file '%s' the dynamic symbol table"
15199 " contains %" PRIu64 " entry:\n",
15200 "\nIn linked file '%s' the dynamic symbol table"
15201 " contains %" PRIu64 " entries:\n",
15202 filedata->num_dynamic_syms),
15203 filedata->file_name,
15204 filedata->num_dynamic_syms);
15205 }
15206 else
15207 {
15208 printf (ngettext ("\nSymbol table for image contains %" PRIu64
15209 " entry:\n",
15210 "\nSymbol table for image contains %" PRIu64
15211 " entries:\n",
15212 filedata->num_dynamic_syms),
15213 filedata->num_dynamic_syms);
15214 }
15215
15216 print_symbol_table_heading ();
15217
15218 for (si = 0; si < filedata->num_dynamic_syms; si++)
15219 print_symbol (filedata, si, filedata->dynamic_symbols, NULL,
15220 filedata->dynamic_strings,
15221 filedata->dynamic_strings_length);
15222 }
15223 else if ((do_dyn_syms || (do_syms && !do_using_dynamic))
15224 && filedata->section_headers != NULL)
15225 {
15226 unsigned int i;
15227
15228 for (i = 0, section = filedata->section_headers;
15229 i < filedata->file_header.e_shnum;
15230 i++, section++)
15231 {
15232 if ((section->sh_type != SHT_SYMTAB
15233 && section->sh_type != SHT_DYNSYM)
15234 || (!do_syms
15235 && section->sh_type == SHT_SYMTAB))
15236 continue;
15237
15238 dump_symbol_section (section, filedata);
15239 }
15240 }
15241 else if (do_syms)
15242 printf
15243 (_("\nDynamic symbol information is not available for displaying symbols.\n"));
15244
15245 if (do_histogram && filedata->buckets != NULL)
15246 {
15247 uint64_t *lengths;
15248 uint64_t *counts;
15249 uint64_t hn;
15250 uint64_t si;
15251 uint64_t maxlength = 0;
15252 uint64_t nzero_counts = 0;
15253 uint64_t nsyms = 0;
15254 char *visited;
15255
15256 printf (ngettext ("\nHistogram for bucket list length "
15257 "(total of %" PRIu64 " bucket):\n",
15258 "\nHistogram for bucket list length "
15259 "(total of %" PRIu64 " buckets):\n",
15260 filedata->nbuckets),
15261 filedata->nbuckets);
15262
15263 lengths = calloc (filedata->nbuckets, sizeof (*lengths));
15264 if (lengths == NULL)
15265 {
15266 error (_("Out of memory allocating space for histogram buckets\n"));
15267 goto err_out;
15268 }
15269 visited = xcmalloc (filedata->nchains, 1);
15270 memset (visited, 0, filedata->nchains);
15271
15272 printf (_(" Length Number %% of total Coverage\n"));
15273 for (hn = 0; hn < filedata->nbuckets; ++hn)
15274 {
15275 for (si = filedata->buckets[hn]; si > 0; si = filedata->chains[si])
15276 {
15277 ++nsyms;
15278 if (maxlength < ++lengths[hn])
15279 ++maxlength;
15280 if (si >= filedata->nchains || visited[si])
15281 {
15282 error (_("histogram chain is corrupt\n"));
15283 break;
15284 }
15285 visited[si] = 1;
15286 }
15287 }
15288 free (visited);
15289
15290 counts = calloc (maxlength + 1, sizeof (*counts));
15291 if (counts == NULL)
15292 {
15293 free (lengths);
15294 error (_("Out of memory allocating space for histogram counts\n"));
15295 goto err_out;
15296 }
15297
15298 for (hn = 0; hn < filedata->nbuckets; ++hn)
15299 ++counts[lengths[hn]];
15300
15301 if (filedata->nbuckets > 0)
15302 {
15303 uint64_t i;
15304 printf (" 0 %-10" PRIu64 " (%5.1f%%)\n",
15305 counts[0], (counts[0] * 100.0) / filedata->nbuckets);
15306 for (i = 1; i <= maxlength; ++i)
15307 {
15308 nzero_counts += counts[i] * i;
15309 printf ("%7" PRIu64 " %-10" PRIu64 " (%5.1f%%) %5.1f%%\n",
15310 i, counts[i], (counts[i] * 100.0) / filedata->nbuckets,
15311 (nzero_counts * 100.0) / nsyms);
15312 }
15313 }
15314
15315 free (counts);
15316 free (lengths);
15317 }
15318
15319 free (filedata->buckets);
15320 filedata->buckets = NULL;
15321 filedata->nbuckets = 0;
15322 free (filedata->chains);
15323 filedata->chains = NULL;
15324
15325 if (do_histogram && filedata->gnubuckets != NULL)
15326 {
15327 uint64_t *lengths;
15328 uint64_t *counts;
15329 uint64_t hn;
15330 uint64_t maxlength = 0;
15331 uint64_t nzero_counts = 0;
15332 uint64_t nsyms = 0;
15333
15334 printf (ngettext ("\nHistogram for `%s' bucket list length "
15335 "(total of %" PRIu64 " bucket):\n",
15336 "\nHistogram for `%s' bucket list length "
15337 "(total of %" PRIu64 " buckets):\n",
15338 filedata->ngnubuckets),
15339 GNU_HASH_SECTION_NAME (filedata),
15340 filedata->ngnubuckets);
15341
15342 lengths = calloc (filedata->ngnubuckets, sizeof (*lengths));
15343 if (lengths == NULL)
15344 {
15345 error (_("Out of memory allocating space for gnu histogram buckets\n"));
15346 goto err_out;
15347 }
15348
15349 printf (_(" Length Number %% of total Coverage\n"));
15350
15351 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
15352 if (filedata->gnubuckets[hn] != 0)
15353 {
15354 uint64_t off, length = 1;
15355
15356 for (off = filedata->gnubuckets[hn] - filedata->gnusymidx;
15357 /* PR 17531 file: 010-77222-0.004. */
15358 off < filedata->ngnuchains
15359 && (filedata->gnuchains[off] & 1) == 0;
15360 ++off)
15361 ++length;
15362 lengths[hn] = length;
15363 if (length > maxlength)
15364 maxlength = length;
15365 nsyms += length;
15366 }
15367
15368 counts = calloc (maxlength + 1, sizeof (*counts));
15369 if (counts == NULL)
15370 {
15371 free (lengths);
15372 error (_("Out of memory allocating space for gnu histogram counts\n"));
15373 goto err_out;
15374 }
15375
15376 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
15377 ++counts[lengths[hn]];
15378
15379 if (filedata->ngnubuckets > 0)
15380 {
15381 uint64_t j;
15382 printf (" 0 %-10" PRIu64 " (%5.1f%%)\n",
15383 counts[0], (counts[0] * 100.0) / filedata->ngnubuckets);
15384 for (j = 1; j <= maxlength; ++j)
15385 {
15386 nzero_counts += counts[j] * j;
15387 printf ("%7" PRIu64 " %-10" PRIu64 " (%5.1f%%) %5.1f%%\n",
15388 j, counts[j], (counts[j] * 100.0) / filedata->ngnubuckets,
15389 (nzero_counts * 100.0) / nsyms);
15390 }
15391 }
15392
15393 free (counts);
15394 free (lengths);
15395 }
15396 free (filedata->gnubuckets);
15397 filedata->gnubuckets = NULL;
15398 filedata->ngnubuckets = 0;
15399 free (filedata->gnuchains);
15400 filedata->gnuchains = NULL;
15401 filedata->ngnuchains = 0;
15402 free (filedata->mipsxlat);
15403 filedata->mipsxlat = NULL;
15404 return true;
15405
15406 err_out:
15407 free (filedata->gnubuckets);
15408 filedata->gnubuckets = NULL;
15409 filedata->ngnubuckets = 0;
15410 free (filedata->gnuchains);
15411 filedata->gnuchains = NULL;
15412 filedata->ngnuchains = 0;
15413 free (filedata->mipsxlat);
15414 filedata->mipsxlat = NULL;
15415 free (filedata->buckets);
15416 filedata->buckets = NULL;
15417 filedata->nbuckets = 0;
15418 free (filedata->chains);
15419 filedata->chains = NULL;
15420 return false;
15421 }
15422
15423 static bool
15424 process_syminfo (Filedata * filedata)
15425 {
15426 unsigned int i;
15427
15428 if (filedata->dynamic_syminfo == NULL
15429 || !do_dynamic)
15430 /* No syminfo, this is ok. */
15431 return true;
15432
15433 /* There better should be a dynamic symbol section. */
15434 if (filedata->dynamic_symbols == NULL || filedata->dynamic_strings == NULL)
15435 return false;
15436
15437 if (filedata->is_separate)
15438 printf (ngettext ("\nIn linked file '%s: the dynamic info segment at offset %#" PRIx64 " contains %d entry:\n",
15439 "\nIn linked file '%s: the dynamic info segment at offset %#" PRIx64 " contains %d entries:\n",
15440 filedata->dynamic_syminfo_nent),
15441 filedata->file_name,
15442 filedata->dynamic_syminfo_offset,
15443 filedata->dynamic_syminfo_nent);
15444 else
15445 printf (ngettext ("\nDynamic info segment at offset %#" PRIx64
15446 " contains %d entry:\n",
15447 "\nDynamic info segment at offset %#" PRIx64
15448 " contains %d entries:\n",
15449 filedata->dynamic_syminfo_nent),
15450 filedata->dynamic_syminfo_offset,
15451 filedata->dynamic_syminfo_nent);
15452
15453 printf (_(" Num: Name BoundTo Flags\n"));
15454 for (i = 0; i < filedata->dynamic_syminfo_nent; ++i)
15455 {
15456 unsigned short int flags = filedata->dynamic_syminfo[i].si_flags;
15457
15458 printf ("%4d: ", i);
15459 if (i >= filedata->num_dynamic_syms)
15460 printf (_("<corrupt index>"));
15461 else if (valid_dynamic_name (filedata, filedata->dynamic_symbols[i].st_name))
15462 print_symbol_name (30, get_dynamic_name (filedata,
15463 filedata->dynamic_symbols[i].st_name));
15464 else
15465 printf (_("<corrupt: %19ld>"), filedata->dynamic_symbols[i].st_name);
15466 putchar (' ');
15467
15468 switch (filedata->dynamic_syminfo[i].si_boundto)
15469 {
15470 case SYMINFO_BT_SELF:
15471 fputs ("SELF ", stdout);
15472 break;
15473 case SYMINFO_BT_PARENT:
15474 fputs ("PARENT ", stdout);
15475 break;
15476 default:
15477 if (filedata->dynamic_syminfo[i].si_boundto > 0
15478 && filedata->dynamic_syminfo[i].si_boundto < filedata->dynamic_nent
15479 && valid_dynamic_name (filedata,
15480 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val))
15481 {
15482 print_symbol_name (10, get_dynamic_name (filedata,
15483 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val));
15484 putchar (' ' );
15485 }
15486 else
15487 printf ("%-10d ", filedata->dynamic_syminfo[i].si_boundto);
15488 break;
15489 }
15490
15491 if (flags & SYMINFO_FLG_DIRECT)
15492 printf (" DIRECT");
15493 if (flags & SYMINFO_FLG_PASSTHRU)
15494 printf (" PASSTHRU");
15495 if (flags & SYMINFO_FLG_COPY)
15496 printf (" COPY");
15497 if (flags & SYMINFO_FLG_LAZYLOAD)
15498 printf (" LAZYLOAD");
15499
15500 puts ("");
15501 }
15502
15503 return true;
15504 }
15505
15506 /* A macro which evaluates to TRUE if the region ADDR .. ADDR + NELEM
15507 is contained by the region START .. END. The types of ADDR, START
15508 and END should all be the same. Note both ADDR + NELEM and END
15509 point to just beyond the end of the regions that are being tested. */
15510 #define IN_RANGE(START,END,ADDR,NELEM) \
15511 (((ADDR) >= (START)) && ((ADDR) < (END)) && ((ADDR) + (NELEM) <= (END)))
15512
15513 /* Check to see if the given reloc needs to be handled in a target specific
15514 manner. If so then process the reloc and return TRUE otherwise return
15515 FALSE.
15516
15517 If called with reloc == NULL, then this is a signal that reloc processing
15518 for the current section has finished, and any saved state should be
15519 discarded. */
15520
15521 static bool
15522 target_specific_reloc_handling (Filedata *filedata,
15523 Elf_Internal_Rela *reloc,
15524 unsigned char *start,
15525 unsigned char *end,
15526 Elf_Internal_Sym *symtab,
15527 uint64_t num_syms)
15528 {
15529 unsigned int reloc_type = 0;
15530 uint64_t sym_index = 0;
15531
15532 if (reloc)
15533 {
15534 reloc_type = get_reloc_type (filedata, reloc->r_info);
15535 sym_index = get_reloc_symindex (reloc->r_info);
15536 }
15537
15538 switch (filedata->file_header.e_machine)
15539 {
15540 case EM_LOONGARCH:
15541 {
15542 switch (reloc_type)
15543 {
15544 /* For .uleb128 .LFE1-.LFB1, loongarch write 0 to object file
15545 at assembly time. */
15546 case 107: /* R_LARCH_ADD_ULEB128. */
15547 case 108: /* R_LARCH_SUB_ULEB128. */
15548 {
15549 uint64_t value = 0;
15550 unsigned int reloc_size = 0;
15551 int leb_ret = 0;
15552
15553 if (reloc->r_offset < (size_t) (end - start))
15554 value = read_leb128 (start + reloc->r_offset, end, false,
15555 &reloc_size, &leb_ret);
15556 if (leb_ret != 0 || reloc_size == 0 || reloc_size > 8)
15557 error (_("LoongArch ULEB128 field at 0x%lx contains invalid "
15558 "ULEB128 value\n"),
15559 (long) reloc->r_offset);
15560
15561 else if (sym_index >= num_syms)
15562 error (_("%s reloc contains invalid symbol index "
15563 "%" PRIu64 "\n"),
15564 (reloc_type == 107
15565 ? "R_LARCH_ADD_ULEB128"
15566 : "R_LARCH_SUB_ULEB128"),
15567 sym_index);
15568 else
15569 {
15570 if (reloc_type == 107)
15571 value += reloc->r_addend + symtab[sym_index].st_value;
15572 else
15573 value -= reloc->r_addend + symtab[sym_index].st_value;
15574
15575 /* Write uleb128 value to p. */
15576 bfd_byte *p = start + reloc->r_offset;
15577 do
15578 {
15579 bfd_byte c = value & 0x7f;
15580 value >>= 7;
15581 if (--reloc_size != 0)
15582 c |= 0x80;
15583 *p++ = c;
15584 }
15585 while (reloc_size);
15586 }
15587
15588 return true;
15589 }
15590 }
15591 break;
15592 }
15593
15594 case EM_MSP430:
15595 case EM_MSP430_OLD:
15596 {
15597 static Elf_Internal_Sym * saved_sym = NULL;
15598
15599 if (reloc == NULL)
15600 {
15601 saved_sym = NULL;
15602 return true;
15603 }
15604
15605 switch (reloc_type)
15606 {
15607 case 10: /* R_MSP430_SYM_DIFF */
15608 case 12: /* R_MSP430_GNU_SUB_ULEB128 */
15609 if (uses_msp430x_relocs (filedata))
15610 break;
15611 /* Fall through. */
15612 case 21: /* R_MSP430X_SYM_DIFF */
15613 case 23: /* R_MSP430X_GNU_SUB_ULEB128 */
15614 /* PR 21139. */
15615 if (sym_index >= num_syms)
15616 error (_("%s reloc contains invalid symbol index "
15617 "%" PRIu64 "\n"), "MSP430 SYM_DIFF", sym_index);
15618 else
15619 saved_sym = symtab + sym_index;
15620 return true;
15621
15622 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
15623 case 3: /* R_MSP430_16 or R_MSP430_ABS8 */
15624 goto handle_sym_diff;
15625
15626 case 5: /* R_MSP430_16_BYTE */
15627 case 9: /* R_MSP430_8 */
15628 case 11: /* R_MSP430_GNU_SET_ULEB128 */
15629 if (uses_msp430x_relocs (filedata))
15630 break;
15631 goto handle_sym_diff;
15632
15633 case 2: /* R_MSP430_ABS16 */
15634 case 15: /* R_MSP430X_ABS16 */
15635 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
15636 if (! uses_msp430x_relocs (filedata))
15637 break;
15638 goto handle_sym_diff;
15639
15640 handle_sym_diff:
15641 if (saved_sym != NULL)
15642 {
15643 uint64_t value;
15644 unsigned int reloc_size = 0;
15645 int leb_ret = 0;
15646 switch (reloc_type)
15647 {
15648 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
15649 reloc_size = 4;
15650 break;
15651 case 11: /* R_MSP430_GNU_SET_ULEB128 */
15652 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
15653 if (reloc->r_offset < (size_t) (end - start))
15654 read_leb128 (start + reloc->r_offset, end, false,
15655 &reloc_size, &leb_ret);
15656 break;
15657 default:
15658 reloc_size = 2;
15659 break;
15660 }
15661
15662 if (leb_ret != 0 || reloc_size == 0 || reloc_size > 8)
15663 error (_("MSP430 ULEB128 field at %#" PRIx64
15664 " contains invalid ULEB128 value\n"),
15665 reloc->r_offset);
15666 else if (sym_index >= num_syms)
15667 error (_("%s reloc contains invalid symbol index "
15668 "%" PRIu64 "\n"), "MSP430", sym_index);
15669 else
15670 {
15671 value = reloc->r_addend + (symtab[sym_index].st_value
15672 - saved_sym->st_value);
15673
15674 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
15675 byte_put (start + reloc->r_offset, value, reloc_size);
15676 else
15677 /* PR 21137 */
15678 error (_("MSP430 sym diff reloc contains invalid offset: "
15679 "%#" PRIx64 "\n"),
15680 reloc->r_offset);
15681 }
15682
15683 saved_sym = NULL;
15684 return true;
15685 }
15686 break;
15687
15688 default:
15689 if (saved_sym != NULL)
15690 error (_("Unhandled MSP430 reloc type found after SYM_DIFF reloc\n"));
15691 break;
15692 }
15693 break;
15694 }
15695
15696 case EM_MN10300:
15697 case EM_CYGNUS_MN10300:
15698 {
15699 static Elf_Internal_Sym * saved_sym = NULL;
15700
15701 if (reloc == NULL)
15702 {
15703 saved_sym = NULL;
15704 return true;
15705 }
15706
15707 switch (reloc_type)
15708 {
15709 case 34: /* R_MN10300_ALIGN */
15710 return true;
15711 case 33: /* R_MN10300_SYM_DIFF */
15712 if (sym_index >= num_syms)
15713 error (_("%s reloc contains invalid symbol index "
15714 "%" PRIu64 "\n"), "MN10300_SYM_DIFF", sym_index);
15715 else
15716 saved_sym = symtab + sym_index;
15717 return true;
15718
15719 case 1: /* R_MN10300_32 */
15720 case 2: /* R_MN10300_16 */
15721 if (saved_sym != NULL)
15722 {
15723 int reloc_size = reloc_type == 1 ? 4 : 2;
15724 uint64_t value;
15725
15726 if (sym_index >= num_syms)
15727 error (_("%s reloc contains invalid symbol index "
15728 "%" PRIu64 "\n"), "MN10300", sym_index);
15729 else
15730 {
15731 value = reloc->r_addend + (symtab[sym_index].st_value
15732 - saved_sym->st_value);
15733
15734 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
15735 byte_put (start + reloc->r_offset, value, reloc_size);
15736 else
15737 error (_("MN10300 sym diff reloc contains invalid offset:"
15738 " %#" PRIx64 "\n"),
15739 reloc->r_offset);
15740 }
15741
15742 saved_sym = NULL;
15743 return true;
15744 }
15745 break;
15746 default:
15747 if (saved_sym != NULL)
15748 error (_("Unhandled MN10300 reloc type found after SYM_DIFF reloc\n"));
15749 break;
15750 }
15751 break;
15752 }
15753
15754 case EM_RL78:
15755 {
15756 static uint64_t saved_sym1 = 0;
15757 static uint64_t saved_sym2 = 0;
15758 static uint64_t value;
15759
15760 if (reloc == NULL)
15761 {
15762 saved_sym1 = saved_sym2 = 0;
15763 return true;
15764 }
15765
15766 switch (reloc_type)
15767 {
15768 case 0x80: /* R_RL78_SYM. */
15769 saved_sym1 = saved_sym2;
15770 if (sym_index >= num_syms)
15771 error (_("%s reloc contains invalid symbol index "
15772 "%" PRIu64 "\n"), "RL78_SYM", sym_index);
15773 else
15774 {
15775 saved_sym2 = symtab[sym_index].st_value;
15776 saved_sym2 += reloc->r_addend;
15777 }
15778 return true;
15779
15780 case 0x83: /* R_RL78_OPsub. */
15781 value = saved_sym1 - saved_sym2;
15782 saved_sym2 = saved_sym1 = 0;
15783 return true;
15784 break;
15785
15786 case 0x41: /* R_RL78_ABS32. */
15787 if (IN_RANGE (start, end, start + reloc->r_offset, 4))
15788 byte_put (start + reloc->r_offset, value, 4);
15789 else
15790 error (_("RL78 sym diff reloc contains invalid offset: "
15791 "%#" PRIx64 "\n"),
15792 reloc->r_offset);
15793 value = 0;
15794 return true;
15795
15796 case 0x43: /* R_RL78_ABS16. */
15797 if (IN_RANGE (start, end, start + reloc->r_offset, 2))
15798 byte_put (start + reloc->r_offset, value, 2);
15799 else
15800 error (_("RL78 sym diff reloc contains invalid offset: "
15801 "%#" PRIx64 "\n"),
15802 reloc->r_offset);
15803 value = 0;
15804 return true;
15805
15806 default:
15807 break;
15808 }
15809 break;
15810 }
15811 }
15812
15813 return false;
15814 }
15815
15816 /* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
15817 DWARF debug sections. This is a target specific test. Note - we do not
15818 go through the whole including-target-headers-multiple-times route, (as
15819 we have already done with <elf/h8.h>) because this would become very
15820 messy and even then this function would have to contain target specific
15821 information (the names of the relocs instead of their numeric values).
15822 FIXME: This is not the correct way to solve this problem. The proper way
15823 is to have target specific reloc sizing and typing functions created by
15824 the reloc-macros.h header, in the same way that it already creates the
15825 reloc naming functions. */
15826
15827 static bool
15828 is_32bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
15829 {
15830 /* Please keep this table alpha-sorted for ease of visual lookup. */
15831 switch (filedata->file_header.e_machine)
15832 {
15833 case EM_386:
15834 case EM_IAMCU:
15835 return reloc_type == 1; /* R_386_32. */
15836 case EM_68K:
15837 return reloc_type == 1; /* R_68K_32. */
15838 case EM_860:
15839 return reloc_type == 1; /* R_860_32. */
15840 case EM_960:
15841 return reloc_type == 2; /* R_960_32. */
15842 case EM_AARCH64:
15843 return (reloc_type == 258
15844 || reloc_type == 1); /* R_AARCH64_ABS32 || R_AARCH64_P32_ABS32 */
15845 case EM_BPF:
15846 return reloc_type == 11; /* R_BPF_DATA_32 */
15847 case EM_ADAPTEVA_EPIPHANY:
15848 return reloc_type == 3;
15849 case EM_ALPHA:
15850 return reloc_type == 1; /* R_ALPHA_REFLONG. */
15851 case EM_ARC:
15852 return reloc_type == 1; /* R_ARC_32. */
15853 case EM_ARC_COMPACT:
15854 case EM_ARC_COMPACT2:
15855 case EM_ARC_COMPACT3:
15856 case EM_ARC_COMPACT3_64:
15857 return reloc_type == 4; /* R_ARC_32. */
15858 case EM_ARM:
15859 return reloc_type == 2; /* R_ARM_ABS32 */
15860 case EM_AVR_OLD:
15861 case EM_AVR:
15862 return reloc_type == 1;
15863 case EM_BLACKFIN:
15864 return reloc_type == 0x12; /* R_byte4_data. */
15865 case EM_CRIS:
15866 return reloc_type == 3; /* R_CRIS_32. */
15867 case EM_CR16:
15868 return reloc_type == 3; /* R_CR16_NUM32. */
15869 case EM_CRX:
15870 return reloc_type == 15; /* R_CRX_NUM32. */
15871 case EM_CSKY:
15872 return reloc_type == 1; /* R_CKCORE_ADDR32. */
15873 case EM_CYGNUS_FRV:
15874 return reloc_type == 1;
15875 case EM_CYGNUS_D10V:
15876 case EM_D10V:
15877 return reloc_type == 6; /* R_D10V_32. */
15878 case EM_CYGNUS_D30V:
15879 case EM_D30V:
15880 return reloc_type == 12; /* R_D30V_32_NORMAL. */
15881 case EM_DLX:
15882 return reloc_type == 3; /* R_DLX_RELOC_32. */
15883 case EM_CYGNUS_FR30:
15884 case EM_FR30:
15885 return reloc_type == 3; /* R_FR30_32. */
15886 case EM_FT32:
15887 return reloc_type == 1; /* R_FT32_32. */
15888 case EM_H8S:
15889 case EM_H8_300:
15890 case EM_H8_300H:
15891 return reloc_type == 1; /* R_H8_DIR32. */
15892 case EM_IA_64:
15893 return (reloc_type == 0x64 /* R_IA64_SECREL32MSB. */
15894 || reloc_type == 0x65 /* R_IA64_SECREL32LSB. */
15895 || reloc_type == 0x24 /* R_IA64_DIR32MSB. */
15896 || reloc_type == 0x25 /* R_IA64_DIR32LSB. */);
15897 case EM_IP2K_OLD:
15898 case EM_IP2K:
15899 return reloc_type == 2; /* R_IP2K_32. */
15900 case EM_IQ2000:
15901 return reloc_type == 2; /* R_IQ2000_32. */
15902 case EM_KVX:
15903 return reloc_type == 2; /* R_KVX_32. */
15904 case EM_LATTICEMICO32:
15905 return reloc_type == 3; /* R_LM32_32. */
15906 case EM_LOONGARCH:
15907 return reloc_type == 1; /* R_LARCH_32. */
15908 case EM_M32C_OLD:
15909 case EM_M32C:
15910 return reloc_type == 3; /* R_M32C_32. */
15911 case EM_M32R:
15912 return reloc_type == 34; /* R_M32R_32_RELA. */
15913 case EM_68HC11:
15914 case EM_68HC12:
15915 return reloc_type == 6; /* R_M68HC11_32. */
15916 case EM_S12Z:
15917 return reloc_type == 7 || /* R_S12Z_EXT32 */
15918 reloc_type == 6; /* R_S12Z_CW32. */
15919 case EM_MCORE:
15920 return reloc_type == 1; /* R_MCORE_ADDR32. */
15921 case EM_CYGNUS_MEP:
15922 return reloc_type == 4; /* R_MEP_32. */
15923 case EM_METAG:
15924 return reloc_type == 2; /* R_METAG_ADDR32. */
15925 case EM_MICROBLAZE:
15926 return reloc_type == 1; /* R_MICROBLAZE_32. */
15927 case EM_MIPS:
15928 return reloc_type == 2; /* R_MIPS_32. */
15929 case EM_MMIX:
15930 return reloc_type == 4; /* R_MMIX_32. */
15931 case EM_CYGNUS_MN10200:
15932 case EM_MN10200:
15933 return reloc_type == 1; /* R_MN10200_32. */
15934 case EM_CYGNUS_MN10300:
15935 case EM_MN10300:
15936 return reloc_type == 1; /* R_MN10300_32. */
15937 case EM_MOXIE:
15938 return reloc_type == 1; /* R_MOXIE_32. */
15939 case EM_MSP430_OLD:
15940 case EM_MSP430:
15941 return reloc_type == 1; /* R_MSP430_32 or R_MSP320_ABS32. */
15942 case EM_MT:
15943 return reloc_type == 2; /* R_MT_32. */
15944 case EM_NDS32:
15945 return reloc_type == 20; /* R_NDS32_32_RELA. */
15946 case EM_ALTERA_NIOS2:
15947 return reloc_type == 12; /* R_NIOS2_BFD_RELOC_32. */
15948 case EM_NIOS32:
15949 return reloc_type == 1; /* R_NIOS_32. */
15950 case EM_OR1K:
15951 return reloc_type == 1; /* R_OR1K_32. */
15952 case EM_PARISC:
15953 return (reloc_type == 1 /* R_PARISC_DIR32. */
15954 || reloc_type == 2 /* R_PARISC_DIR21L. */
15955 || reloc_type == 41); /* R_PARISC_SECREL32. */
15956 case EM_PJ:
15957 case EM_PJ_OLD:
15958 return reloc_type == 1; /* R_PJ_DATA_DIR32. */
15959 case EM_PPC64:
15960 return reloc_type == 1; /* R_PPC64_ADDR32. */
15961 case EM_PPC:
15962 return reloc_type == 1; /* R_PPC_ADDR32. */
15963 case EM_TI_PRU:
15964 return reloc_type == 11; /* R_PRU_BFD_RELOC_32. */
15965 case EM_RISCV:
15966 return reloc_type == 1; /* R_RISCV_32. */
15967 case EM_RL78:
15968 return reloc_type == 1; /* R_RL78_DIR32. */
15969 case EM_RX:
15970 return reloc_type == 1; /* R_RX_DIR32. */
15971 case EM_S370:
15972 return reloc_type == 1; /* R_I370_ADDR31. */
15973 case EM_S390_OLD:
15974 case EM_S390:
15975 return reloc_type == 4; /* R_S390_32. */
15976 case EM_SCORE:
15977 return reloc_type == 8; /* R_SCORE_ABS32. */
15978 case EM_SH:
15979 return reloc_type == 1; /* R_SH_DIR32. */
15980 case EM_SPARC32PLUS:
15981 case EM_SPARCV9:
15982 case EM_SPARC:
15983 return reloc_type == 3 /* R_SPARC_32. */
15984 || reloc_type == 23; /* R_SPARC_UA32. */
15985 case EM_SPU:
15986 return reloc_type == 6; /* R_SPU_ADDR32 */
15987 case EM_TI_C6000:
15988 return reloc_type == 1; /* R_C6000_ABS32. */
15989 case EM_TILEGX:
15990 return reloc_type == 2; /* R_TILEGX_32. */
15991 case EM_TILEPRO:
15992 return reloc_type == 1; /* R_TILEPRO_32. */
15993 case EM_CYGNUS_V850:
15994 case EM_V850:
15995 return reloc_type == 6; /* R_V850_ABS32. */
15996 case EM_V800:
15997 return reloc_type == 0x33; /* R_V810_WORD. */
15998 case EM_VAX:
15999 return reloc_type == 1; /* R_VAX_32. */
16000 case EM_VISIUM:
16001 return reloc_type == 3; /* R_VISIUM_32. */
16002 case EM_WEBASSEMBLY:
16003 return reloc_type == 1; /* R_WASM32_32. */
16004 case EM_X86_64:
16005 case EM_L1OM:
16006 case EM_K1OM:
16007 return reloc_type == 10; /* R_X86_64_32. */
16008 case EM_XGATE:
16009 return reloc_type == 4; /* R_XGATE_32. */
16010 case EM_XSTORMY16:
16011 return reloc_type == 1; /* R_XSTROMY16_32. */
16012 case EM_XTENSA_OLD:
16013 case EM_XTENSA:
16014 return reloc_type == 1; /* R_XTENSA_32. */
16015 case EM_Z80:
16016 return reloc_type == 6; /* R_Z80_32. */
16017 default:
16018 {
16019 static unsigned int prev_warn = 0;
16020
16021 /* Avoid repeating the same warning multiple times. */
16022 if (prev_warn != filedata->file_header.e_machine)
16023 error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
16024 filedata->file_header.e_machine);
16025 prev_warn = filedata->file_header.e_machine;
16026 return false;
16027 }
16028 }
16029 }
16030
16031 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
16032 a 32-bit pc-relative RELA relocation used in DWARF debug sections. */
16033
16034 static bool
16035 is_32bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
16036 {
16037 switch (filedata->file_header.e_machine)
16038 /* Please keep this table alpha-sorted for ease of visual lookup. */
16039 {
16040 case EM_386:
16041 case EM_IAMCU:
16042 return reloc_type == 2; /* R_386_PC32. */
16043 case EM_68K:
16044 return reloc_type == 4; /* R_68K_PC32. */
16045 case EM_AARCH64:
16046 return reloc_type == 261; /* R_AARCH64_PREL32 */
16047 case EM_ADAPTEVA_EPIPHANY:
16048 return reloc_type == 6;
16049 case EM_ALPHA:
16050 return reloc_type == 10; /* R_ALPHA_SREL32. */
16051 case EM_ARC_COMPACT:
16052 case EM_ARC_COMPACT2:
16053 case EM_ARC_COMPACT3:
16054 case EM_ARC_COMPACT3_64:
16055 return reloc_type == 49; /* R_ARC_32_PCREL. */
16056 case EM_ARM:
16057 return reloc_type == 3; /* R_ARM_REL32 */
16058 case EM_AVR_OLD:
16059 case EM_AVR:
16060 return reloc_type == 36; /* R_AVR_32_PCREL. */
16061 case EM_LOONGARCH:
16062 return reloc_type == 99; /* R_LARCH_32_PCREL. */
16063 case EM_MICROBLAZE:
16064 return reloc_type == 2; /* R_MICROBLAZE_32_PCREL. */
16065 case EM_OR1K:
16066 return reloc_type == 9; /* R_OR1K_32_PCREL. */
16067 case EM_PARISC:
16068 return reloc_type == 9; /* R_PARISC_PCREL32. */
16069 case EM_PPC:
16070 return reloc_type == 26; /* R_PPC_REL32. */
16071 case EM_PPC64:
16072 return reloc_type == 26; /* R_PPC64_REL32. */
16073 case EM_RISCV:
16074 return reloc_type == 57; /* R_RISCV_32_PCREL. */
16075 case EM_S390_OLD:
16076 case EM_S390:
16077 return reloc_type == 5; /* R_390_PC32. */
16078 case EM_SH:
16079 return reloc_type == 2; /* R_SH_REL32. */
16080 case EM_SPARC32PLUS:
16081 case EM_SPARCV9:
16082 case EM_SPARC:
16083 return reloc_type == 6; /* R_SPARC_DISP32. */
16084 case EM_SPU:
16085 return reloc_type == 13; /* R_SPU_REL32. */
16086 case EM_TILEGX:
16087 return reloc_type == 6; /* R_TILEGX_32_PCREL. */
16088 case EM_TILEPRO:
16089 return reloc_type == 4; /* R_TILEPRO_32_PCREL. */
16090 case EM_VISIUM:
16091 return reloc_type == 6; /* R_VISIUM_32_PCREL */
16092 case EM_X86_64:
16093 case EM_L1OM:
16094 case EM_K1OM:
16095 return reloc_type == 2; /* R_X86_64_PC32. */
16096 case EM_VAX:
16097 return reloc_type == 4; /* R_VAX_PCREL32. */
16098 case EM_XTENSA_OLD:
16099 case EM_XTENSA:
16100 return reloc_type == 14; /* R_XTENSA_32_PCREL. */
16101 case EM_KVX:
16102 return reloc_type == 7; /* R_KVX_32_PCREL */
16103 default:
16104 /* Do not abort or issue an error message here. Not all targets use
16105 pc-relative 32-bit relocs in their DWARF debug information and we
16106 have already tested for target coverage in is_32bit_abs_reloc. A
16107 more helpful warning message will be generated by apply_relocations
16108 anyway, so just return. */
16109 return false;
16110 }
16111 }
16112
16113 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
16114 a 64-bit absolute RELA relocation used in DWARF debug sections. */
16115
16116 static bool
16117 is_64bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
16118 {
16119 switch (filedata->file_header.e_machine)
16120 {
16121 case EM_AARCH64:
16122 return reloc_type == 257; /* R_AARCH64_ABS64. */
16123 case EM_ARC_COMPACT3_64:
16124 return reloc_type == 5; /* R_ARC_64. */
16125 case EM_ALPHA:
16126 return reloc_type == 2; /* R_ALPHA_REFQUAD. */
16127 case EM_IA_64:
16128 return (reloc_type == 0x26 /* R_IA64_DIR64MSB. */
16129 || reloc_type == 0x27 /* R_IA64_DIR64LSB. */);
16130 case EM_LOONGARCH:
16131 return reloc_type == 2; /* R_LARCH_64 */
16132 case EM_PARISC:
16133 return reloc_type == 80; /* R_PARISC_DIR64. */
16134 case EM_PPC64:
16135 return reloc_type == 38; /* R_PPC64_ADDR64. */
16136 case EM_RISCV:
16137 return reloc_type == 2; /* R_RISCV_64. */
16138 case EM_SPARC32PLUS:
16139 case EM_SPARCV9:
16140 case EM_SPARC:
16141 return reloc_type == 32 /* R_SPARC_64. */
16142 || reloc_type == 54; /* R_SPARC_UA64. */
16143 case EM_X86_64:
16144 case EM_L1OM:
16145 case EM_K1OM:
16146 return reloc_type == 1; /* R_X86_64_64. */
16147 case EM_S390_OLD:
16148 case EM_S390:
16149 return reloc_type == 22; /* R_S390_64. */
16150 case EM_TILEGX:
16151 return reloc_type == 1; /* R_TILEGX_64. */
16152 case EM_MIPS:
16153 return reloc_type == 18; /* R_MIPS_64. */
16154 case EM_KVX:
16155 return reloc_type == 3; /* R_KVX_64 */
16156 default:
16157 return false;
16158 }
16159 }
16160
16161 /* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
16162 a 64-bit pc-relative RELA relocation used in DWARF debug sections. */
16163
16164 static bool
16165 is_64bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
16166 {
16167 switch (filedata->file_header.e_machine)
16168 {
16169 case EM_AARCH64:
16170 return reloc_type == 260; /* R_AARCH64_PREL64. */
16171 case EM_ALPHA:
16172 return reloc_type == 11; /* R_ALPHA_SREL64. */
16173 case EM_IA_64:
16174 return (reloc_type == 0x4e /* R_IA64_PCREL64MSB. */
16175 || reloc_type == 0x4f /* R_IA64_PCREL64LSB. */);
16176 case EM_PARISC:
16177 return reloc_type == 72; /* R_PARISC_PCREL64. */
16178 case EM_PPC64:
16179 return reloc_type == 44; /* R_PPC64_REL64. */
16180 case EM_SPARC32PLUS:
16181 case EM_SPARCV9:
16182 case EM_SPARC:
16183 return reloc_type == 46; /* R_SPARC_DISP64. */
16184 case EM_X86_64:
16185 case EM_L1OM:
16186 case EM_K1OM:
16187 return reloc_type == 24; /* R_X86_64_PC64. */
16188 case EM_S390_OLD:
16189 case EM_S390:
16190 return reloc_type == 23; /* R_S390_PC64. */
16191 case EM_TILEGX:
16192 return reloc_type == 5; /* R_TILEGX_64_PCREL. */
16193 default:
16194 return false;
16195 }
16196 }
16197
16198 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
16199 a 24-bit absolute RELA relocation used in DWARF debug sections. */
16200
16201 static bool
16202 is_24bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
16203 {
16204 switch (filedata->file_header.e_machine)
16205 {
16206 case EM_CYGNUS_MN10200:
16207 case EM_MN10200:
16208 return reloc_type == 4; /* R_MN10200_24. */
16209 case EM_FT32:
16210 return reloc_type == 5; /* R_FT32_20. */
16211 case EM_Z80:
16212 return reloc_type == 5; /* R_Z80_24. */
16213 default:
16214 return false;
16215 }
16216 }
16217
16218 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
16219 a 16-bit absolute RELA relocation used in DWARF debug sections. */
16220
16221 static bool
16222 is_16bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
16223 {
16224 /* Please keep this table alpha-sorted for ease of visual lookup. */
16225 switch (filedata->file_header.e_machine)
16226 {
16227 case EM_ARC:
16228 case EM_ARC_COMPACT:
16229 case EM_ARC_COMPACT2:
16230 case EM_ARC_COMPACT3:
16231 case EM_ARC_COMPACT3_64:
16232 return reloc_type == 2; /* R_ARC_16. */
16233 case EM_ADAPTEVA_EPIPHANY:
16234 return reloc_type == 5;
16235 case EM_AVR_OLD:
16236 case EM_AVR:
16237 return reloc_type == 4; /* R_AVR_16. */
16238 case EM_CYGNUS_D10V:
16239 case EM_D10V:
16240 return reloc_type == 3; /* R_D10V_16. */
16241 case EM_FT32:
16242 return reloc_type == 2; /* R_FT32_16. */
16243 case EM_H8S:
16244 case EM_H8_300:
16245 case EM_H8_300H:
16246 return reloc_type == R_H8_DIR16;
16247 case EM_IP2K_OLD:
16248 case EM_IP2K:
16249 return reloc_type == 1; /* R_IP2K_16. */
16250 case EM_M32C_OLD:
16251 case EM_M32C:
16252 return reloc_type == 1; /* R_M32C_16 */
16253 case EM_CYGNUS_MN10200:
16254 case EM_MN10200:
16255 return reloc_type == 2; /* R_MN10200_16. */
16256 case EM_CYGNUS_MN10300:
16257 case EM_MN10300:
16258 return reloc_type == 2; /* R_MN10300_16. */
16259 case EM_KVX:
16260 return reloc_type == 1; /* R_KVX_16 */
16261 case EM_MSP430:
16262 if (uses_msp430x_relocs (filedata))
16263 return reloc_type == 2; /* R_MSP430_ABS16. */
16264 /* Fall through. */
16265 case EM_MSP430_OLD:
16266 return reloc_type == 5; /* R_MSP430_16_BYTE. */
16267 case EM_NDS32:
16268 return reloc_type == 19; /* R_NDS32_16_RELA. */
16269 case EM_ALTERA_NIOS2:
16270 return reloc_type == 13; /* R_NIOS2_BFD_RELOC_16. */
16271 case EM_NIOS32:
16272 return reloc_type == 9; /* R_NIOS_16. */
16273 case EM_OR1K:
16274 return reloc_type == 2; /* R_OR1K_16. */
16275 case EM_RISCV:
16276 return reloc_type == 55; /* R_RISCV_SET16. */
16277 case EM_TI_PRU:
16278 return reloc_type == 8; /* R_PRU_BFD_RELOC_16. */
16279 case EM_TI_C6000:
16280 return reloc_type == 2; /* R_C6000_ABS16. */
16281 case EM_VISIUM:
16282 return reloc_type == 2; /* R_VISIUM_16. */
16283 case EM_XGATE:
16284 return reloc_type == 3; /* R_XGATE_16. */
16285 case EM_Z80:
16286 return reloc_type == 4; /* R_Z80_16. */
16287 default:
16288 return false;
16289 }
16290 }
16291
16292 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
16293 a 8-bit absolute RELA relocation used in DWARF debug sections. */
16294
16295 static bool
16296 is_8bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
16297 {
16298 switch (filedata->file_header.e_machine)
16299 {
16300 case EM_RISCV:
16301 return reloc_type == 54; /* R_RISCV_SET8. */
16302 case EM_Z80:
16303 return reloc_type == 1; /* R_Z80_8. */
16304 case EM_MICROBLAZE:
16305 return (reloc_type == 33 /* R_MICROBLAZE_32_NONE. */
16306 || reloc_type == 0 /* R_MICROBLAZE_NONE. */
16307 || reloc_type == 9 /* R_MICROBLAZE_64_NONE. */);
16308 default:
16309 return false;
16310 }
16311 }
16312
16313 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
16314 a 6-bit absolute RELA relocation used in DWARF debug sections. */
16315
16316 static bool
16317 is_6bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
16318 {
16319 switch (filedata->file_header.e_machine)
16320 {
16321 case EM_RISCV:
16322 return reloc_type == 53; /* R_RISCV_SET6. */
16323 default:
16324 return false;
16325 }
16326 }
16327
16328 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
16329 a 32-bit inplace add RELA relocation used in DWARF debug sections. */
16330
16331 static bool
16332 is_32bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
16333 {
16334 /* Please keep this table alpha-sorted for ease of visual lookup. */
16335 switch (filedata->file_header.e_machine)
16336 {
16337 case EM_LOONGARCH:
16338 return reloc_type == 50; /* R_LARCH_ADD32. */
16339 case EM_RISCV:
16340 return reloc_type == 35; /* R_RISCV_ADD32. */
16341 default:
16342 return false;
16343 }
16344 }
16345
16346 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
16347 a 32-bit inplace sub RELA relocation used in DWARF debug sections. */
16348
16349 static bool
16350 is_32bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
16351 {
16352 /* Please keep this table alpha-sorted for ease of visual lookup. */
16353 switch (filedata->file_header.e_machine)
16354 {
16355 case EM_LOONGARCH:
16356 return reloc_type == 55; /* R_LARCH_SUB32. */
16357 case EM_RISCV:
16358 return reloc_type == 39; /* R_RISCV_SUB32. */
16359 default:
16360 return false;
16361 }
16362 }
16363
16364 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
16365 a 64-bit inplace add RELA relocation used in DWARF debug sections. */
16366
16367 static bool
16368 is_64bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
16369 {
16370 /* Please keep this table alpha-sorted for ease of visual lookup. */
16371 switch (filedata->file_header.e_machine)
16372 {
16373 case EM_LOONGARCH:
16374 return reloc_type == 51; /* R_LARCH_ADD64. */
16375 case EM_RISCV:
16376 return reloc_type == 36; /* R_RISCV_ADD64. */
16377 default:
16378 return false;
16379 }
16380 }
16381
16382 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
16383 a 64-bit inplace sub RELA relocation used in DWARF debug sections. */
16384
16385 static bool
16386 is_64bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
16387 {
16388 /* Please keep this table alpha-sorted for ease of visual lookup. */
16389 switch (filedata->file_header.e_machine)
16390 {
16391 case EM_LOONGARCH:
16392 return reloc_type == 56; /* R_LARCH_SUB64. */
16393 case EM_RISCV:
16394 return reloc_type == 40; /* R_RISCV_SUB64. */
16395 default:
16396 return false;
16397 }
16398 }
16399
16400 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
16401 a 16-bit inplace add RELA relocation used in DWARF debug sections. */
16402
16403 static bool
16404 is_16bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
16405 {
16406 /* Please keep this table alpha-sorted for ease of visual lookup. */
16407 switch (filedata->file_header.e_machine)
16408 {
16409 case EM_LOONGARCH:
16410 return reloc_type == 48; /* R_LARCH_ADD16. */
16411 case EM_RISCV:
16412 return reloc_type == 34; /* R_RISCV_ADD16. */
16413 default:
16414 return false;
16415 }
16416 }
16417
16418 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
16419 a 16-bit inplace sub RELA relocation used in DWARF debug sections. */
16420
16421 static bool
16422 is_16bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
16423 {
16424 /* Please keep this table alpha-sorted for ease of visual lookup. */
16425 switch (filedata->file_header.e_machine)
16426 {
16427 case EM_LOONGARCH:
16428 return reloc_type == 53; /* R_LARCH_SUB16. */
16429 case EM_RISCV:
16430 return reloc_type == 38; /* R_RISCV_SUB16. */
16431 default:
16432 return false;
16433 }
16434 }
16435
16436 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
16437 a 8-bit inplace add RELA relocation used in DWARF debug sections. */
16438
16439 static bool
16440 is_8bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
16441 {
16442 /* Please keep this table alpha-sorted for ease of visual lookup. */
16443 switch (filedata->file_header.e_machine)
16444 {
16445 case EM_LOONGARCH:
16446 return reloc_type == 47; /* R_LARCH_ADD8. */
16447 case EM_RISCV:
16448 return reloc_type == 33; /* R_RISCV_ADD8. */
16449 default:
16450 return false;
16451 }
16452 }
16453
16454 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
16455 a 8-bit inplace sub RELA relocation used in DWARF debug sections. */
16456
16457 static bool
16458 is_8bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
16459 {
16460 /* Please keep this table alpha-sorted for ease of visual lookup. */
16461 switch (filedata->file_header.e_machine)
16462 {
16463 case EM_LOONGARCH:
16464 return reloc_type == 52; /* R_LARCH_SUB8. */
16465 case EM_RISCV:
16466 return reloc_type == 37; /* R_RISCV_SUB8. */
16467 default:
16468 return false;
16469 }
16470 }
16471
16472 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
16473 a 6-bit inplace add RELA relocation used in DWARF debug sections. */
16474
16475 static bool
16476 is_6bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
16477 {
16478 switch (filedata->file_header.e_machine)
16479 {
16480 case EM_LOONGARCH:
16481 return reloc_type == 105; /* R_LARCH_ADD6. */
16482 default:
16483 return false;
16484 }
16485 }
16486
16487 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
16488 a 6-bit inplace sub RELA relocation used in DWARF debug sections. */
16489
16490 static bool
16491 is_6bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
16492 {
16493 switch (filedata->file_header.e_machine)
16494 {
16495 case EM_LOONGARCH:
16496 return reloc_type == 106; /* R_LARCH_SUB6. */
16497 case EM_RISCV:
16498 return reloc_type == 52; /* R_RISCV_SUB6. */
16499 default:
16500 return false;
16501 }
16502 }
16503
16504 /* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
16505 relocation entries (possibly formerly used for SHT_GROUP sections). */
16506
16507 static bool
16508 is_none_reloc (Filedata * filedata, unsigned int reloc_type)
16509 {
16510 switch (filedata->file_header.e_machine)
16511 {
16512 case EM_386: /* R_386_NONE. */
16513 case EM_68K: /* R_68K_NONE. */
16514 case EM_ADAPTEVA_EPIPHANY:
16515 case EM_ALPHA: /* R_ALPHA_NONE. */
16516 case EM_ALTERA_NIOS2: /* R_NIOS2_NONE. */
16517 case EM_ARC: /* R_ARC_NONE. */
16518 case EM_ARC_COMPACT2: /* R_ARC_NONE. */
16519 case EM_ARC_COMPACT: /* R_ARC_NONE. */
16520 case EM_ARC_COMPACT3: /* R_ARC_NONE. */
16521 case EM_ARC_COMPACT3_64: /* R_ARC_NONE. */
16522 case EM_ARM: /* R_ARM_NONE. */
16523 case EM_CRIS: /* R_CRIS_NONE. */
16524 case EM_FT32: /* R_FT32_NONE. */
16525 case EM_IA_64: /* R_IA64_NONE. */
16526 case EM_K1OM: /* R_X86_64_NONE. */
16527 case EM_KVX: /* R_KVX_NONE. */
16528 case EM_L1OM: /* R_X86_64_NONE. */
16529 case EM_M32R: /* R_M32R_NONE. */
16530 case EM_MIPS: /* R_MIPS_NONE. */
16531 case EM_MN10300: /* R_MN10300_NONE. */
16532 case EM_MOXIE: /* R_MOXIE_NONE. */
16533 case EM_NIOS32: /* R_NIOS_NONE. */
16534 case EM_OR1K: /* R_OR1K_NONE. */
16535 case EM_PARISC: /* R_PARISC_NONE. */
16536 case EM_PPC64: /* R_PPC64_NONE. */
16537 case EM_PPC: /* R_PPC_NONE. */
16538 case EM_RISCV: /* R_RISCV_NONE. */
16539 case EM_S390: /* R_390_NONE. */
16540 case EM_S390_OLD:
16541 case EM_SH: /* R_SH_NONE. */
16542 case EM_SPARC32PLUS:
16543 case EM_SPARC: /* R_SPARC_NONE. */
16544 case EM_SPARCV9:
16545 case EM_TILEGX: /* R_TILEGX_NONE. */
16546 case EM_TILEPRO: /* R_TILEPRO_NONE. */
16547 case EM_TI_C6000:/* R_C6000_NONE. */
16548 case EM_X86_64: /* R_X86_64_NONE. */
16549 case EM_Z80: /* R_Z80_NONE. */
16550 case EM_WEBASSEMBLY: /* R_WASM32_NONE. */
16551 return reloc_type == 0;
16552
16553 case EM_AARCH64:
16554 return reloc_type == 0 || reloc_type == 256;
16555 case EM_AVR_OLD:
16556 case EM_AVR:
16557 return (reloc_type == 0 /* R_AVR_NONE. */
16558 || reloc_type == 30 /* R_AVR_DIFF8. */
16559 || reloc_type == 31 /* R_AVR_DIFF16. */
16560 || reloc_type == 32 /* R_AVR_DIFF32. */);
16561 case EM_METAG:
16562 return reloc_type == 3; /* R_METAG_NONE. */
16563 case EM_NDS32:
16564 return (reloc_type == 0 /* R_NDS32_NONE. */
16565 || reloc_type == 205 /* R_NDS32_DIFF8. */
16566 || reloc_type == 206 /* R_NDS32_DIFF16. */
16567 || reloc_type == 207 /* R_NDS32_DIFF32. */
16568 || reloc_type == 208 /* R_NDS32_DIFF_ULEB128. */);
16569 case EM_TI_PRU:
16570 return (reloc_type == 0 /* R_PRU_NONE. */
16571 || reloc_type == 65 /* R_PRU_DIFF8. */
16572 || reloc_type == 66 /* R_PRU_DIFF16. */
16573 || reloc_type == 67 /* R_PRU_DIFF32. */);
16574 case EM_XTENSA_OLD:
16575 case EM_XTENSA:
16576 return (reloc_type == 0 /* R_XTENSA_NONE. */
16577 || reloc_type == 17 /* R_XTENSA_DIFF8. */
16578 || reloc_type == 18 /* R_XTENSA_DIFF16. */
16579 || reloc_type == 19 /* R_XTENSA_DIFF32. */
16580 || reloc_type == 57 /* R_XTENSA_PDIFF8. */
16581 || reloc_type == 58 /* R_XTENSA_PDIFF16. */
16582 || reloc_type == 59 /* R_XTENSA_PDIFF32. */
16583 || reloc_type == 60 /* R_XTENSA_NDIFF8. */
16584 || reloc_type == 61 /* R_XTENSA_NDIFF16. */
16585 || reloc_type == 62 /* R_XTENSA_NDIFF32. */);
16586 }
16587 return false;
16588 }
16589
16590 /* Returns TRUE if there is a relocation against
16591 section NAME at OFFSET bytes. */
16592
16593 bool
16594 reloc_at (struct dwarf_section * dsec, uint64_t offset)
16595 {
16596 Elf_Internal_Rela * relocs;
16597 Elf_Internal_Rela * rp;
16598
16599 if (dsec == NULL || dsec->reloc_info == NULL)
16600 return false;
16601
16602 relocs = (Elf_Internal_Rela *) dsec->reloc_info;
16603
16604 for (rp = relocs; rp < relocs + dsec->num_relocs; ++rp)
16605 if (rp->r_offset == offset)
16606 return true;
16607
16608 return false;
16609 }
16610
16611 /* Apply relocations to a section.
16612 Returns TRUE upon success, FALSE otherwise.
16613 If RELOCS_RETURN is non-NULL then it is set to point to the loaded relocs.
16614 It is then the caller's responsibility to free them. NUM_RELOCS_RETURN
16615 will be set to the number of relocs loaded.
16616
16617 Note: So far support has been added only for those relocations
16618 which can be found in debug sections. FIXME: Add support for
16619 more relocations ? */
16620
16621 static bool
16622 apply_relocations (Filedata *filedata,
16623 const Elf_Internal_Shdr *section,
16624 unsigned char *start,
16625 size_t size,
16626 void **relocs_return,
16627 uint64_t *num_relocs_return)
16628 {
16629 Elf_Internal_Shdr * relsec;
16630 unsigned char * end = start + size;
16631
16632 if (relocs_return != NULL)
16633 {
16634 * (Elf_Internal_Rela **) relocs_return = NULL;
16635 * num_relocs_return = 0;
16636 }
16637
16638 if (filedata->file_header.e_type != ET_REL)
16639 /* No relocs to apply. */
16640 return true;
16641
16642 /* Find the reloc section associated with the section. */
16643 for (relsec = filedata->section_headers;
16644 relsec < filedata->section_headers + filedata->file_header.e_shnum;
16645 ++relsec)
16646 {
16647 bool is_rela;
16648 uint64_t num_relocs;
16649 Elf_Internal_Rela * relocs;
16650 Elf_Internal_Rela * rp;
16651 Elf_Internal_Shdr * symsec;
16652 Elf_Internal_Sym * symtab;
16653 uint64_t num_syms;
16654 Elf_Internal_Sym * sym;
16655
16656 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
16657 || relsec->sh_info >= filedata->file_header.e_shnum
16658 || filedata->section_headers + relsec->sh_info != section
16659 || relsec->sh_size == 0
16660 || relsec->sh_link >= filedata->file_header.e_shnum)
16661 continue;
16662
16663 symsec = filedata->section_headers + relsec->sh_link;
16664 if (symsec->sh_type != SHT_SYMTAB
16665 && symsec->sh_type != SHT_DYNSYM)
16666 return false;
16667
16668 is_rela = relsec->sh_type == SHT_RELA;
16669
16670 if (is_rela)
16671 {
16672 if (!slurp_rela_relocs (filedata, relsec->sh_offset,
16673 relsec->sh_size, & relocs, & num_relocs))
16674 return false;
16675 }
16676 else
16677 {
16678 if (!slurp_rel_relocs (filedata, relsec->sh_offset,
16679 relsec->sh_size, & relocs, & num_relocs))
16680 return false;
16681 }
16682
16683 /* SH uses RELA but uses in place value instead of the addend field. */
16684 if (filedata->file_header.e_machine == EM_SH)
16685 is_rela = false;
16686
16687 symtab = get_elf_symbols (filedata, symsec, & num_syms);
16688
16689 for (rp = relocs; rp < relocs + num_relocs; ++rp)
16690 {
16691 uint64_t addend;
16692 unsigned int reloc_type;
16693 unsigned int reloc_size;
16694 bool reloc_inplace = false;
16695 bool reloc_subtract = false;
16696 unsigned char *rloc;
16697 uint64_t sym_index;
16698
16699 reloc_type = get_reloc_type (filedata, rp->r_info);
16700
16701 if (target_specific_reloc_handling (filedata, rp, start, end, symtab, num_syms))
16702 continue;
16703 else if (is_none_reloc (filedata, reloc_type))
16704 continue;
16705 else if (is_32bit_abs_reloc (filedata, reloc_type)
16706 || is_32bit_pcrel_reloc (filedata, reloc_type))
16707 reloc_size = 4;
16708 else if (is_64bit_abs_reloc (filedata, reloc_type)
16709 || is_64bit_pcrel_reloc (filedata, reloc_type))
16710 reloc_size = 8;
16711 else if (is_24bit_abs_reloc (filedata, reloc_type))
16712 reloc_size = 3;
16713 else if (is_16bit_abs_reloc (filedata, reloc_type))
16714 reloc_size = 2;
16715 else if (is_8bit_abs_reloc (filedata, reloc_type)
16716 || is_6bit_abs_reloc (filedata, reloc_type))
16717 reloc_size = 1;
16718 else if ((reloc_subtract = is_32bit_inplace_sub_reloc (filedata,
16719 reloc_type))
16720 || is_32bit_inplace_add_reloc (filedata, reloc_type))
16721 {
16722 reloc_size = 4;
16723 reloc_inplace = true;
16724 }
16725 else if ((reloc_subtract = is_64bit_inplace_sub_reloc (filedata,
16726 reloc_type))
16727 || is_64bit_inplace_add_reloc (filedata, reloc_type))
16728 {
16729 reloc_size = 8;
16730 reloc_inplace = true;
16731 }
16732 else if ((reloc_subtract = is_16bit_inplace_sub_reloc (filedata,
16733 reloc_type))
16734 || is_16bit_inplace_add_reloc (filedata, reloc_type))
16735 {
16736 reloc_size = 2;
16737 reloc_inplace = true;
16738 }
16739 else if ((reloc_subtract = is_8bit_inplace_sub_reloc (filedata,
16740 reloc_type))
16741 || is_8bit_inplace_add_reloc (filedata, reloc_type))
16742 {
16743 reloc_size = 1;
16744 reloc_inplace = true;
16745 }
16746 else if ((reloc_subtract = is_6bit_inplace_sub_reloc (filedata,
16747 reloc_type))
16748 || is_6bit_inplace_add_reloc (filedata, reloc_type))
16749 {
16750 reloc_size = 1;
16751 reloc_inplace = true;
16752 }
16753 else
16754 {
16755 static unsigned int prev_reloc = 0;
16756
16757 if (reloc_type != prev_reloc)
16758 warn (_("unable to apply unsupported reloc type %d to section %s\n"),
16759 reloc_type, printable_section_name (filedata, section));
16760 prev_reloc = reloc_type;
16761 continue;
16762 }
16763
16764 rloc = start + rp->r_offset;
16765 if (!IN_RANGE (start, end, rloc, reloc_size))
16766 {
16767 warn (_("skipping invalid relocation offset %#" PRIx64
16768 " in section %s\n"),
16769 rp->r_offset,
16770 printable_section_name (filedata, section));
16771 continue;
16772 }
16773
16774 sym_index = get_reloc_symindex (rp->r_info);
16775 if (sym_index >= num_syms)
16776 {
16777 warn (_("skipping invalid relocation symbol index %#" PRIx64
16778 " in section %s\n"),
16779 sym_index, printable_section_name (filedata, section));
16780 continue;
16781 }
16782 sym = symtab + sym_index;
16783
16784 /* If the reloc has a symbol associated with it,
16785 make sure that it is of an appropriate type.
16786
16787 Relocations against symbols without type can happen.
16788 Gcc -feliminate-dwarf2-dups may generate symbols
16789 without type for debug info.
16790
16791 Icc generates relocations against function symbols
16792 instead of local labels.
16793
16794 Relocations against object symbols can happen, eg when
16795 referencing a global array. For an example of this see
16796 the _clz.o binary in libgcc.a. */
16797 if (sym != symtab
16798 && ELF_ST_TYPE (sym->st_info) != STT_COMMON
16799 && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
16800 {
16801 warn (_("skipping unexpected symbol type %s in section %s relocation %tu\n"),
16802 get_symbol_type (filedata, ELF_ST_TYPE (sym->st_info)),
16803 printable_section_name (filedata, relsec),
16804 rp - relocs);
16805 continue;
16806 }
16807
16808 addend = 0;
16809 if (is_rela)
16810 addend += rp->r_addend;
16811 /* R_XTENSA_32, R_PJ_DATA_DIR32 and R_D30V_32_NORMAL are
16812 partial_inplace. */
16813 if (!is_rela
16814 || (filedata->file_header.e_machine == EM_XTENSA
16815 && reloc_type == 1)
16816 || ((filedata->file_header.e_machine == EM_PJ
16817 || filedata->file_header.e_machine == EM_PJ_OLD)
16818 && reloc_type == 1)
16819 || ((filedata->file_header.e_machine == EM_D30V
16820 || filedata->file_header.e_machine == EM_CYGNUS_D30V)
16821 && reloc_type == 12)
16822 || reloc_inplace)
16823 {
16824 if (is_6bit_inplace_sub_reloc (filedata, reloc_type))
16825 addend += byte_get (rloc, reloc_size) & 0x3f;
16826 else
16827 addend += byte_get (rloc, reloc_size);
16828 }
16829
16830 if (is_32bit_pcrel_reloc (filedata, reloc_type)
16831 || is_64bit_pcrel_reloc (filedata, reloc_type))
16832 {
16833 /* On HPPA, all pc-relative relocations are biased by 8. */
16834 if (filedata->file_header.e_machine == EM_PARISC)
16835 addend -= 8;
16836 byte_put (rloc, (addend + sym->st_value) - rp->r_offset,
16837 reloc_size);
16838 }
16839 else if (is_6bit_abs_reloc (filedata, reloc_type)
16840 || is_6bit_inplace_sub_reloc (filedata, reloc_type)
16841 || is_6bit_inplace_add_reloc (filedata, reloc_type))
16842 {
16843 if (reloc_subtract)
16844 addend -= sym->st_value;
16845 else
16846 addend += sym->st_value;
16847 addend = (addend & 0x3f) | (byte_get (rloc, reloc_size) & 0xc0);
16848 byte_put (rloc, addend, reloc_size);
16849 }
16850 else if (reloc_subtract)
16851 byte_put (rloc, addend - sym->st_value, reloc_size);
16852 else
16853 byte_put (rloc, addend + sym->st_value, reloc_size);
16854 }
16855
16856 free (symtab);
16857 /* Let the target specific reloc processing code know that
16858 we have finished with these relocs. */
16859 target_specific_reloc_handling (filedata, NULL, NULL, NULL, NULL, 0);
16860
16861 if (relocs_return)
16862 {
16863 * (Elf_Internal_Rela **) relocs_return = relocs;
16864 * num_relocs_return = num_relocs;
16865 }
16866 else
16867 free (relocs);
16868
16869 break;
16870 }
16871
16872 return true;
16873 }
16874
16875 #ifdef SUPPORT_DISASSEMBLY
16876 static bool
16877 disassemble_section (Elf_Internal_Shdr * section, Filedata * filedata)
16878 {
16879 printf (_("\nAssembly dump of section %s\n"), printable_section_name (filedata, section));
16880
16881 /* FIXME: XXX -- to be done --- XXX */
16882
16883 return true;
16884 }
16885 #endif
16886
16887 /* Reads in the contents of SECTION from FILE, returning a pointer
16888 to a malloc'ed buffer or NULL if something went wrong. */
16889
16890 static char *
16891 get_section_contents (Elf_Internal_Shdr * section, Filedata * filedata)
16892 {
16893 uint64_t num_bytes = section->sh_size;
16894
16895 if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
16896 {
16897 printf (_("Section '%s' has no data to dump.\n"),
16898 printable_section_name (filedata, section));
16899 return NULL;
16900 }
16901
16902 return (char *) get_data (NULL, filedata, section->sh_offset, 1, num_bytes,
16903 _("section contents"));
16904 }
16905
16906 /* Uncompresses a section that was compressed using zlib/zstd, in place. */
16907
16908 static bool
16909 uncompress_section_contents (bool is_zstd,
16910 unsigned char ** buffer,
16911 uint64_t uncompressed_size,
16912 uint64_t * size,
16913 uint64_t file_size)
16914 {
16915 uint64_t compressed_size = *size;
16916 unsigned char *compressed_buffer = *buffer;
16917 unsigned char *uncompressed_buffer = NULL;
16918 z_stream strm;
16919 int rc;
16920
16921 /* Similar to bfd_section_size_insane() in the BFD library we expect an
16922 upper limit of ~10x compression. Any compression larger than that is
16923 thought to be due to fuzzing of the compression header. */
16924 if (uncompressed_size > file_size * 10)
16925 {
16926 error (_("Uncompressed section size is suspiciously large: 0x%" PRIu64 "\n"),
16927 uncompressed_size);
16928 goto fail;
16929 }
16930
16931 uncompressed_buffer = xmalloc (uncompressed_size);
16932
16933 if (is_zstd)
16934 {
16935 #ifdef HAVE_ZSTD
16936 size_t ret = ZSTD_decompress (uncompressed_buffer, uncompressed_size,
16937 compressed_buffer, compressed_size);
16938 if (ZSTD_isError (ret))
16939 goto fail;
16940 #endif
16941 }
16942 else
16943 {
16944 /* It is possible the section consists of several compressed
16945 buffers concatenated together, so we uncompress in a loop. */
16946 /* PR 18313: The state field in the z_stream structure is supposed
16947 to be invisible to the user (ie us), but some compilers will
16948 still complain about it being used without initialisation. So
16949 we first zero the entire z_stream structure and then set the fields
16950 that we need. */
16951 memset (&strm, 0, sizeof strm);
16952 strm.avail_in = compressed_size;
16953 strm.next_in = (Bytef *)compressed_buffer;
16954 strm.avail_out = uncompressed_size;
16955
16956 rc = inflateInit (&strm);
16957 while (strm.avail_in > 0)
16958 {
16959 if (rc != Z_OK)
16960 break;
16961 strm.next_out = ((Bytef *)uncompressed_buffer
16962 + (uncompressed_size - strm.avail_out));
16963 rc = inflate (&strm, Z_FINISH);
16964 if (rc != Z_STREAM_END)
16965 break;
16966 rc = inflateReset (&strm);
16967 }
16968 if (inflateEnd (&strm) != Z_OK || rc != Z_OK || strm.avail_out != 0)
16969 goto fail;
16970 }
16971
16972 *buffer = uncompressed_buffer;
16973 *size = uncompressed_size;
16974 return true;
16975
16976 fail:
16977 free (uncompressed_buffer);
16978 /* Indicate decompression failure. */
16979 *buffer = NULL;
16980 return false;
16981 }
16982
16983 static uint64_t
16984 maybe_expand_or_relocate_section (Elf_Internal_Shdr * section,
16985 Filedata * filedata,
16986 unsigned char ** start_ptr,
16987 unsigned char ** decomp_buf,
16988 bool relocate)
16989 {
16990 uint64_t section_size = section->sh_size;
16991 unsigned char * start = * start_ptr;
16992
16993 if (decompress_dumps)
16994 {
16995 uint64_t new_size = section_size;
16996 uint64_t uncompressed_size = 0;
16997 bool is_zstd = false;
16998
16999 if ((section->sh_flags & SHF_COMPRESSED) != 0)
17000 {
17001 Elf_Internal_Chdr chdr;
17002 unsigned int compression_header_size
17003 = get_compression_header (& chdr, start, section_size);
17004
17005 if (compression_header_size == 0)
17006 /* An error message will have already been generated
17007 by get_compression_header. */
17008 return (uint64_t) -1;
17009
17010 if (chdr.ch_type == ch_compress_zlib)
17011 ;
17012 #ifdef HAVE_ZSTD
17013 else if (chdr.ch_type == ch_compress_zstd)
17014 is_zstd = true;
17015 #endif
17016 else
17017 {
17018 warn (_("section '%s' has unsupported compress type: %d\n"),
17019 printable_section_name (filedata, section), chdr.ch_type);
17020 return (uint64_t) -1;
17021 }
17022
17023 uncompressed_size = chdr.ch_size;
17024 start += compression_header_size;
17025 new_size -= compression_header_size;
17026 }
17027 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
17028 {
17029 /* Read the zlib header. In this case, it should be "ZLIB"
17030 followed by the uncompressed section size, 8 bytes in
17031 big-endian order. */
17032 uncompressed_size = start[4]; uncompressed_size <<= 8;
17033 uncompressed_size += start[5]; uncompressed_size <<= 8;
17034 uncompressed_size += start[6]; uncompressed_size <<= 8;
17035 uncompressed_size += start[7]; uncompressed_size <<= 8;
17036 uncompressed_size += start[8]; uncompressed_size <<= 8;
17037 uncompressed_size += start[9]; uncompressed_size <<= 8;
17038 uncompressed_size += start[10]; uncompressed_size <<= 8;
17039 uncompressed_size += start[11];
17040 start += 12;
17041 new_size -= 12;
17042 }
17043
17044 if (uncompressed_size)
17045 {
17046 if (uncompress_section_contents (is_zstd, &start, uncompressed_size,
17047 &new_size, filedata->file_size))
17048 {
17049 *decomp_buf = start;
17050 section_size = new_size;
17051 }
17052 else
17053 {
17054 error (_("Unable to decompress section %s\n"),
17055 printable_section_name (filedata, section));
17056 return (uint64_t) -1;
17057 }
17058 }
17059 else
17060 start = * start_ptr;
17061 }
17062 else if (((section->sh_flags & SHF_COMPRESSED) != 0)
17063 || (section_size > 12 && streq ((char *) start, "ZLIB")))
17064 {
17065 printf (_(" NOTE: This section is compressed, but its contents have NOT been expanded for this dump.\n"));
17066 }
17067
17068 if (relocate)
17069 {
17070 if (! apply_relocations (filedata, section, start, section_size, NULL, NULL))
17071 return (uint64_t) -1;
17072 }
17073 else
17074 {
17075 Elf_Internal_Shdr *relsec;
17076
17077 /* If the section being dumped has relocations against it the user might
17078 be expecting these relocations to have been applied. Check for this
17079 case and issue a warning message in order to avoid confusion.
17080 FIXME: Maybe we ought to have an option that dumps a section with
17081 relocs applied ? */
17082 for (relsec = filedata->section_headers;
17083 relsec < filedata->section_headers + filedata->file_header.e_shnum;
17084 ++relsec)
17085 {
17086 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
17087 || relsec->sh_info >= filedata->file_header.e_shnum
17088 || filedata->section_headers + relsec->sh_info != section
17089 || relsec->sh_size == 0
17090 || relsec->sh_link >= filedata->file_header.e_shnum)
17091 continue;
17092
17093 printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
17094 break;
17095 }
17096 }
17097
17098 * start_ptr = start;
17099 return section_size;
17100 }
17101
17102 static bool
17103 dump_section_as_strings (Elf_Internal_Shdr * section, Filedata * filedata)
17104 {
17105 uint64_t num_bytes;
17106 unsigned char *data;
17107 unsigned char *end;
17108 unsigned char *real_start;
17109 unsigned char *start;
17110 unsigned char *decomp_buf;
17111 bool some_strings_shown;
17112
17113 real_start = start = (unsigned char *) get_section_contents (section, filedata);
17114 if (start == NULL)
17115 /* PR 21820: Do not fail if the section was empty. */
17116 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
17117
17118 num_bytes = section->sh_size;
17119
17120 if (filedata->is_separate)
17121 printf (_("\nString dump of section '%s' in linked file %s:\n"),
17122 printable_section_name (filedata, section),
17123 filedata->file_name);
17124 else
17125 printf (_("\nString dump of section '%s':\n"),
17126 printable_section_name (filedata, section));
17127
17128 decomp_buf = NULL;
17129 num_bytes = maybe_expand_or_relocate_section (section, filedata, &start,
17130 &decomp_buf, false);
17131 if (num_bytes == (uint64_t) -1)
17132 goto error_out;
17133
17134 data = start;
17135 end = start + num_bytes;
17136 some_strings_shown = false;
17137
17138 #ifdef HAVE_MBSTATE_T
17139 mbstate_t state;
17140 /* Initialise the multibyte conversion state. */
17141 memset (& state, 0, sizeof (state));
17142 #endif
17143
17144 bool continuing = false;
17145
17146 while (data < end)
17147 {
17148 while (!ISPRINT (* data))
17149 if (++ data >= end)
17150 break;
17151
17152 if (data < end)
17153 {
17154 size_t maxlen = end - data;
17155
17156 if (continuing)
17157 {
17158 printf (" ");
17159 continuing = false;
17160 }
17161 else
17162 {
17163 printf (" [%6tx] ", data - start);
17164 }
17165
17166 if (maxlen > 0)
17167 {
17168 char c = 0;
17169
17170 while (maxlen)
17171 {
17172 c = *data++;
17173
17174 if (c == 0)
17175 break;
17176
17177 /* PR 25543: Treat new-lines as string-ending characters. */
17178 if (c == '\n')
17179 {
17180 printf ("\\n\n");
17181 if (*data != 0)
17182 continuing = true;
17183 break;
17184 }
17185
17186 /* Do not print control characters directly as they can affect terminal
17187 settings. Such characters usually appear in the names generated
17188 by the assembler for local labels. */
17189 if (ISCNTRL (c))
17190 {
17191 printf ("^%c", c + 0x40);
17192 }
17193 else if (ISPRINT (c))
17194 {
17195 putchar (c);
17196 }
17197 else
17198 {
17199 size_t n;
17200 #ifdef HAVE_MBSTATE_T
17201 wchar_t w;
17202 #endif
17203 /* Let printf do the hard work of displaying multibyte characters. */
17204 printf ("%.1s", data - 1);
17205 #ifdef HAVE_MBSTATE_T
17206 /* Try to find out how many bytes made up the character that was
17207 just printed. Advance the symbol pointer past the bytes that
17208 were displayed. */
17209 n = mbrtowc (& w, (char *)(data - 1), MB_CUR_MAX, & state);
17210 #else
17211 n = 1;
17212 #endif
17213 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
17214 data += (n - 1);
17215 }
17216 }
17217
17218 if (c != '\n')
17219 putchar ('\n');
17220 }
17221 else
17222 {
17223 printf (_("<corrupt>\n"));
17224 data = end;
17225 }
17226 some_strings_shown = true;
17227 }
17228 }
17229
17230 if (! some_strings_shown)
17231 printf (_(" No strings found in this section."));
17232
17233 free (decomp_buf);
17234 free (real_start);
17235
17236 putchar ('\n');
17237 return true;
17238
17239 error_out:
17240 free (decomp_buf);
17241 free (real_start);
17242 return false;
17243 }
17244
17245 static bool
17246 dump_section_as_bytes (Elf_Internal_Shdr *section,
17247 Filedata *filedata,
17248 bool relocate)
17249 {
17250 size_t bytes;
17251 uint64_t section_size;
17252 uint64_t addr;
17253 unsigned char *data;
17254 unsigned char *real_start;
17255 unsigned char *start;
17256 unsigned char *decomp_buf;
17257
17258 real_start = start = (unsigned char *) get_section_contents (section, filedata);
17259 if (start == NULL)
17260 /* PR 21820: Do not fail if the section was empty. */
17261 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
17262
17263 section_size = section->sh_size;
17264
17265 if (filedata->is_separate)
17266 printf (_("\nHex dump of section '%s' in linked file %s:\n"),
17267 printable_section_name (filedata, section),
17268 filedata->file_name);
17269 else
17270 printf (_("\nHex dump of section '%s':\n"),
17271 printable_section_name (filedata, section));
17272
17273 decomp_buf = NULL;
17274 section_size = maybe_expand_or_relocate_section (section, filedata, &start,
17275 &decomp_buf, relocate);
17276 if (section_size == (uint64_t) -1)
17277 goto error_out;
17278
17279 addr = section->sh_addr;
17280 bytes = section_size;
17281 data = start;
17282
17283 while (bytes)
17284 {
17285 int j;
17286 int k;
17287 int lbytes;
17288
17289 lbytes = (bytes > 16 ? 16 : bytes);
17290
17291 printf (" 0x%8.8" PRIx64 " ", addr);
17292
17293 for (j = 0; j < 16; j++)
17294 {
17295 if (j < lbytes)
17296 printf ("%2.2x", data[j]);
17297 else
17298 printf (" ");
17299
17300 if ((j & 3) == 3)
17301 printf (" ");
17302 }
17303
17304 for (j = 0; j < lbytes; j++)
17305 {
17306 k = data[j];
17307 if (k >= ' ' && k < 0x7f)
17308 printf ("%c", k);
17309 else
17310 printf (".");
17311 }
17312
17313 putchar ('\n');
17314
17315 data += lbytes;
17316 addr += lbytes;
17317 bytes -= lbytes;
17318 }
17319
17320 free (decomp_buf);
17321 free (real_start);
17322
17323 putchar ('\n');
17324 return true;
17325
17326 error_out:
17327 free (decomp_buf);
17328 free (real_start);
17329 return false;
17330 }
17331
17332 #ifdef ENABLE_LIBCTF
17333 static ctf_sect_t *
17334 shdr_to_ctf_sect (ctf_sect_t *buf, Elf_Internal_Shdr *shdr, Filedata *filedata)
17335 {
17336 buf->cts_name = printable_section_name (filedata, shdr);
17337 buf->cts_size = shdr->sh_size;
17338 buf->cts_entsize = shdr->sh_entsize;
17339
17340 return buf;
17341 }
17342
17343 /* Formatting callback function passed to ctf_dump. Returns either the pointer
17344 it is passed, or a pointer to newly-allocated storage, in which case
17345 dump_ctf() will free it when it no longer needs it. */
17346
17347 static char *
17348 dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
17349 char *s, void *arg)
17350 {
17351 const char *blanks = arg;
17352 return xasprintf ("%s%s", blanks, s);
17353 }
17354
17355 /* Dump CTF errors/warnings. */
17356 static void
17357 dump_ctf_errs (ctf_dict_t *fp)
17358 {
17359 ctf_next_t *it = NULL;
17360 char *errtext;
17361 int is_warning;
17362 int err;
17363
17364 /* Dump accumulated errors and warnings. */
17365 while ((errtext = ctf_errwarning_next (fp, &it, &is_warning, &err)) != NULL)
17366 {
17367 error (_("%s: %s"), is_warning ? _("warning"): _("error"),
17368 errtext);
17369 free (errtext);
17370 }
17371 if (err != ECTF_NEXT_END)
17372 error (_("CTF error: cannot get CTF errors: `%s'"), ctf_errmsg (err));
17373 }
17374
17375 /* Dump one CTF archive member. */
17376
17377 static void
17378 dump_ctf_archive_member (ctf_dict_t *ctf, const char *name, ctf_dict_t *parent,
17379 size_t member)
17380 {
17381 const char *things[] = {"Header", "Labels", "Data objects",
17382 "Function objects", "Variables", "Types", "Strings",
17383 ""};
17384 const char **thing;
17385 size_t i;
17386
17387 /* Don't print out the name of the default-named archive member if it appears
17388 first in the list. The name .ctf appears everywhere, even for things that
17389 aren't really archives, so printing it out is liable to be confusing; also,
17390 the common case by far is for only one archive member to exist, and hiding
17391 it in that case seems worthwhile. */
17392
17393 if (strcmp (name, ".ctf") != 0 || member != 0)
17394 printf (_("\nCTF archive member: %s:\n"), name);
17395
17396 if (ctf_parent_name (ctf) != NULL)
17397 ctf_import (ctf, parent);
17398
17399 for (i = 0, thing = things; *thing[0]; thing++, i++)
17400 {
17401 ctf_dump_state_t *s = NULL;
17402 char *item;
17403
17404 printf ("\n %s:\n", *thing);
17405 while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
17406 (void *) " ")) != NULL)
17407 {
17408 printf ("%s\n", item);
17409 free (item);
17410 }
17411
17412 if (ctf_errno (ctf))
17413 {
17414 error (_("Iteration failed: %s, %s\n"), *thing,
17415 ctf_errmsg (ctf_errno (ctf)));
17416 break;
17417 }
17418 }
17419
17420 dump_ctf_errs (ctf);
17421 }
17422
17423 static bool
17424 dump_section_as_ctf (Elf_Internal_Shdr * section, Filedata * filedata)
17425 {
17426 Elf_Internal_Shdr * symtab_sec = NULL;
17427 Elf_Internal_Shdr * strtab_sec = NULL;
17428 void * data = NULL;
17429 void * symdata = NULL;
17430 void * strdata = NULL;
17431 ctf_sect_t ctfsect, symsect, strsect;
17432 ctf_sect_t * symsectp = NULL;
17433 ctf_sect_t * strsectp = NULL;
17434 ctf_archive_t * ctfa = NULL;
17435 ctf_dict_t * parent = NULL;
17436 ctf_dict_t * fp;
17437
17438 ctf_next_t *i = NULL;
17439 const char *name;
17440 size_t member = 0;
17441 int err;
17442 bool ret = false;
17443
17444 shdr_to_ctf_sect (&ctfsect, section, filedata);
17445 data = get_section_contents (section, filedata);
17446 ctfsect.cts_data = data;
17447
17448 if (!dump_ctf_symtab_name)
17449 dump_ctf_symtab_name = strdup (".dynsym");
17450
17451 if (!dump_ctf_strtab_name)
17452 dump_ctf_strtab_name = strdup (".dynstr");
17453
17454 if (dump_ctf_symtab_name && dump_ctf_symtab_name[0] != 0)
17455 {
17456 if ((symtab_sec = find_section (filedata, dump_ctf_symtab_name)) == NULL)
17457 {
17458 error (_("No symbol section named %s\n"), dump_ctf_symtab_name);
17459 goto fail;
17460 }
17461 if ((symdata = (void *) get_data (NULL, filedata,
17462 symtab_sec->sh_offset, 1,
17463 symtab_sec->sh_size,
17464 _("symbols"))) == NULL)
17465 goto fail;
17466 symsectp = shdr_to_ctf_sect (&symsect, symtab_sec, filedata);
17467 symsect.cts_data = symdata;
17468 }
17469
17470 if (dump_ctf_strtab_name && dump_ctf_strtab_name[0] != 0)
17471 {
17472 if ((strtab_sec = find_section (filedata, dump_ctf_strtab_name)) == NULL)
17473 {
17474 error (_("No string table section named %s\n"),
17475 dump_ctf_strtab_name);
17476 goto fail;
17477 }
17478 if ((strdata = (void *) get_data (NULL, filedata,
17479 strtab_sec->sh_offset, 1,
17480 strtab_sec->sh_size,
17481 _("strings"))) == NULL)
17482 goto fail;
17483 strsectp = shdr_to_ctf_sect (&strsect, strtab_sec, filedata);
17484 strsect.cts_data = strdata;
17485 }
17486
17487 /* Load the CTF file and dump it. It may be a raw CTF section, or an archive:
17488 libctf papers over the difference, so we can pretend it is always an
17489 archive. */
17490
17491 if ((ctfa = ctf_arc_bufopen (&ctfsect, symsectp, strsectp, &err)) == NULL)
17492 {
17493 dump_ctf_errs (NULL);
17494 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
17495 goto fail;
17496 }
17497
17498 ctf_arc_symsect_endianness (ctfa, filedata->file_header.e_ident[EI_DATA]
17499 != ELFDATA2MSB);
17500
17501 /* Preload the parent dict, since it will need to be imported into every
17502 child in turn. */
17503 if ((parent = ctf_dict_open (ctfa, dump_ctf_parent_name, &err)) == NULL)
17504 {
17505 dump_ctf_errs (NULL);
17506 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
17507 goto fail;
17508 }
17509
17510 ret = true;
17511
17512 if (filedata->is_separate)
17513 printf (_("\nDump of CTF section '%s' in linked file %s:\n"),
17514 printable_section_name (filedata, section),
17515 filedata->file_name);
17516 else
17517 printf (_("\nDump of CTF section '%s':\n"),
17518 printable_section_name (filedata, section));
17519
17520 while ((fp = ctf_archive_next (ctfa, &i, &name, 0, &err)) != NULL)
17521 {
17522 dump_ctf_archive_member (fp, name, parent, member++);
17523 ctf_dict_close (fp);
17524 }
17525 if (err != ECTF_NEXT_END)
17526 {
17527 dump_ctf_errs (NULL);
17528 error (_("CTF member open failure: %s\n"), ctf_errmsg (err));
17529 ret = false;
17530 }
17531
17532 fail:
17533 ctf_dict_close (parent);
17534 ctf_close (ctfa);
17535 free (data);
17536 free (symdata);
17537 free (strdata);
17538 return ret;
17539 }
17540 #endif
17541
17542 static bool
17543 load_specific_debug_section (enum dwarf_section_display_enum debug,
17544 const Elf_Internal_Shdr * sec,
17545 void * data)
17546 {
17547 struct dwarf_section * section = &debug_displays [debug].section;
17548 char buf [64];
17549 Filedata * filedata = (Filedata *) data;
17550
17551 if (section->start != NULL)
17552 {
17553 /* If it is already loaded, do nothing. */
17554 if (streq (section->filename, filedata->file_name))
17555 return true;
17556 free (section->start);
17557 }
17558
17559 snprintf (buf, sizeof (buf), _("%s section data"), section->name);
17560 section->address = sec->sh_addr;
17561 section->filename = filedata->file_name;
17562 section->start = (unsigned char *) get_data (NULL, filedata,
17563 sec->sh_offset, 1,
17564 sec->sh_size, buf);
17565 if (section->start == NULL)
17566 section->size = 0;
17567 else
17568 {
17569 unsigned char *start = section->start;
17570 uint64_t size = sec->sh_size;
17571 uint64_t uncompressed_size = 0;
17572 bool is_zstd = false;
17573
17574 if ((sec->sh_flags & SHF_COMPRESSED) != 0)
17575 {
17576 Elf_Internal_Chdr chdr;
17577 unsigned int compression_header_size;
17578
17579 if (size < (is_32bit_elf
17580 ? sizeof (Elf32_External_Chdr)
17581 : sizeof (Elf64_External_Chdr)))
17582 {
17583 warn (_("compressed section %s is too small to contain a compression header\n"),
17584 section->name);
17585 return false;
17586 }
17587
17588 compression_header_size = get_compression_header (&chdr, start, size);
17589 if (compression_header_size == 0)
17590 /* An error message will have already been generated
17591 by get_compression_header. */
17592 return false;
17593
17594 if (chdr.ch_type == ch_compress_zlib)
17595 ;
17596 #ifdef HAVE_ZSTD
17597 else if (chdr.ch_type == ch_compress_zstd)
17598 is_zstd = true;
17599 #endif
17600 else
17601 {
17602 warn (_("section '%s' has unsupported compress type: %d\n"),
17603 section->name, chdr.ch_type);
17604 return false;
17605 }
17606 uncompressed_size = chdr.ch_size;
17607 start += compression_header_size;
17608 size -= compression_header_size;
17609 }
17610 else if (size > 12 && streq ((char *) start, "ZLIB"))
17611 {
17612 /* Read the zlib header. In this case, it should be "ZLIB"
17613 followed by the uncompressed section size, 8 bytes in
17614 big-endian order. */
17615 uncompressed_size = start[4]; uncompressed_size <<= 8;
17616 uncompressed_size += start[5]; uncompressed_size <<= 8;
17617 uncompressed_size += start[6]; uncompressed_size <<= 8;
17618 uncompressed_size += start[7]; uncompressed_size <<= 8;
17619 uncompressed_size += start[8]; uncompressed_size <<= 8;
17620 uncompressed_size += start[9]; uncompressed_size <<= 8;
17621 uncompressed_size += start[10]; uncompressed_size <<= 8;
17622 uncompressed_size += start[11];
17623 start += 12;
17624 size -= 12;
17625 }
17626
17627 if (uncompressed_size)
17628 {
17629 if (uncompress_section_contents (is_zstd, &start, uncompressed_size,
17630 &size, filedata->file_size))
17631 {
17632 /* Free the compressed buffer, update the section buffer
17633 and the section size if uncompress is successful. */
17634 free (section->start);
17635 section->start = start;
17636 }
17637 else
17638 {
17639 error (_("Unable to decompress section %s\n"),
17640 printable_section_name (filedata, sec));
17641 return false;
17642 }
17643 }
17644
17645 section->size = size;
17646 }
17647
17648 if (section->start == NULL)
17649 return false;
17650
17651 if (debug_displays [debug].relocate)
17652 {
17653 if (! apply_relocations (filedata, sec, section->start, section->size,
17654 & section->reloc_info, & section->num_relocs))
17655 return false;
17656 }
17657 else
17658 {
17659 section->reloc_info = NULL;
17660 section->num_relocs = 0;
17661 }
17662
17663 return true;
17664 }
17665
17666 #if HAVE_LIBDEBUGINFOD
17667 /* Return a hex string representation of the build-id. */
17668 unsigned char *
17669 get_build_id (void * data)
17670 {
17671 Filedata * filedata = (Filedata *) data;
17672 Elf_Internal_Shdr * shdr;
17673 size_t i;
17674
17675 /* Iterate through notes to find note.gnu.build-id.
17676 FIXME: Only the first note in any note section is examined. */
17677 for (i = 0, shdr = filedata->section_headers;
17678 i < filedata->file_header.e_shnum && shdr != NULL;
17679 i++, shdr++)
17680 {
17681 if (shdr->sh_type != SHT_NOTE)
17682 continue;
17683
17684 char * next;
17685 char * end;
17686 size_t data_remaining;
17687 size_t min_notesz;
17688 Elf_External_Note * enote;
17689 Elf_Internal_Note inote;
17690
17691 uint64_t offset = shdr->sh_offset;
17692 uint64_t align = shdr->sh_addralign;
17693 uint64_t length = shdr->sh_size;
17694
17695 enote = (Elf_External_Note *) get_section_contents (shdr, filedata);
17696 if (enote == NULL)
17697 continue;
17698
17699 if (align < 4)
17700 align = 4;
17701 else if (align != 4 && align != 8)
17702 {
17703 free (enote);
17704 continue;
17705 }
17706
17707 end = (char *) enote + length;
17708 data_remaining = end - (char *) enote;
17709
17710 if (!is_ia64_vms (filedata))
17711 {
17712 min_notesz = offsetof (Elf_External_Note, name);
17713 if (data_remaining < min_notesz)
17714 {
17715 warn (_("\
17716 malformed note encountered in section %s whilst scanning for build-id note\n"),
17717 printable_section_name (filedata, shdr));
17718 free (enote);
17719 continue;
17720 }
17721 data_remaining -= min_notesz;
17722
17723 inote.type = BYTE_GET (enote->type);
17724 inote.namesz = BYTE_GET (enote->namesz);
17725 inote.namedata = enote->name;
17726 inote.descsz = BYTE_GET (enote->descsz);
17727 inote.descdata = ((char *) enote
17728 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
17729 inote.descpos = offset + (inote.descdata - (char *) enote);
17730 next = ((char *) enote
17731 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
17732 }
17733 else
17734 {
17735 Elf64_External_VMS_Note *vms_enote;
17736
17737 /* PR binutils/15191
17738 Make sure that there is enough data to read. */
17739 min_notesz = offsetof (Elf64_External_VMS_Note, name);
17740 if (data_remaining < min_notesz)
17741 {
17742 warn (_("\
17743 malformed note encountered in section %s whilst scanning for build-id note\n"),
17744 printable_section_name (filedata, shdr));
17745 free (enote);
17746 continue;
17747 }
17748 data_remaining -= min_notesz;
17749
17750 vms_enote = (Elf64_External_VMS_Note *) enote;
17751 inote.type = BYTE_GET (vms_enote->type);
17752 inote.namesz = BYTE_GET (vms_enote->namesz);
17753 inote.namedata = vms_enote->name;
17754 inote.descsz = BYTE_GET (vms_enote->descsz);
17755 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
17756 inote.descpos = offset + (inote.descdata - (char *) enote);
17757 next = inote.descdata + align_power (inote.descsz, 3);
17758 }
17759
17760 /* Skip malformed notes. */
17761 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
17762 || (size_t) (inote.descdata - inote.namedata) > data_remaining
17763 || (size_t) (next - inote.descdata) < inote.descsz
17764 || ((size_t) (next - inote.descdata)
17765 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
17766 {
17767 warn (_("\
17768 malformed note encountered in section %s whilst scanning for build-id note\n"),
17769 printable_section_name (filedata, shdr));
17770 free (enote);
17771 continue;
17772 }
17773
17774 /* Check if this is the build-id note. If so then convert the build-id
17775 bytes to a hex string. */
17776 if (inote.namesz > 0
17777 && startswith (inote.namedata, "GNU")
17778 && inote.type == NT_GNU_BUILD_ID)
17779 {
17780 size_t j;
17781 char * build_id;
17782
17783 build_id = malloc (inote.descsz * 2 + 1);
17784 if (build_id == NULL)
17785 {
17786 free (enote);
17787 return NULL;
17788 }
17789
17790 for (j = 0; j < inote.descsz; ++j)
17791 sprintf (build_id + (j * 2), "%02x", inote.descdata[j] & 0xff);
17792 build_id[inote.descsz * 2] = '\0';
17793 free (enote);
17794
17795 return (unsigned char *) build_id;
17796 }
17797 free (enote);
17798 }
17799
17800 return NULL;
17801 }
17802 #endif /* HAVE_LIBDEBUGINFOD */
17803
17804 /* If this is not NULL, load_debug_section will only look for sections
17805 within the list of sections given here. */
17806 static unsigned int * section_subset = NULL;
17807
17808 bool
17809 load_debug_section (enum dwarf_section_display_enum debug, void * data)
17810 {
17811 struct dwarf_section * section = &debug_displays [debug].section;
17812 Elf_Internal_Shdr * sec;
17813 Filedata * filedata = (Filedata *) data;
17814
17815 if (!dump_any_debugging)
17816 return false;
17817
17818 /* Without section headers we cannot find any sections. */
17819 if (filedata->section_headers == NULL)
17820 return false;
17821
17822 if (filedata->string_table == NULL
17823 && filedata->file_header.e_shstrndx != SHN_UNDEF
17824 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
17825 {
17826 Elf_Internal_Shdr * strs;
17827
17828 /* Read in the string table, so that we have section names to scan. */
17829 strs = filedata->section_headers + filedata->file_header.e_shstrndx;
17830
17831 if (strs != NULL && strs->sh_size != 0)
17832 {
17833 filedata->string_table
17834 = (char *) get_data (NULL, filedata, strs->sh_offset,
17835 1, strs->sh_size, _("string table"));
17836
17837 filedata->string_table_length
17838 = filedata->string_table != NULL ? strs->sh_size : 0;
17839 }
17840 }
17841
17842 /* Locate the debug section. */
17843 sec = find_section_in_set (filedata, section->uncompressed_name, section_subset);
17844 if (sec != NULL)
17845 section->name = section->uncompressed_name;
17846 else
17847 {
17848 sec = find_section_in_set (filedata, section->compressed_name, section_subset);
17849 if (sec != NULL)
17850 section->name = section->compressed_name;
17851 }
17852 if (sec == NULL)
17853 return false;
17854
17855 /* If we're loading from a subset of sections, and we've loaded
17856 a section matching this name before, it's likely that it's a
17857 different one. */
17858 if (section_subset != NULL)
17859 free_debug_section (debug);
17860
17861 return load_specific_debug_section (debug, sec, data);
17862 }
17863
17864 void
17865 free_debug_section (enum dwarf_section_display_enum debug)
17866 {
17867 struct dwarf_section * section = &debug_displays [debug].section;
17868
17869 if (section->start == NULL)
17870 return;
17871
17872 free ((char *) section->start);
17873 section->start = NULL;
17874 section->address = 0;
17875 section->size = 0;
17876
17877 free (section->reloc_info);
17878 section->reloc_info = NULL;
17879 section->num_relocs = 0;
17880 }
17881
17882 static bool
17883 display_debug_section (int shndx, Elf_Internal_Shdr * section, Filedata * filedata)
17884 {
17885 const char *name = (section_name_valid (filedata, section)
17886 ? section_name (filedata, section) : "");
17887 const char *print_name = printable_section_name (filedata, section);
17888 uint64_t length;
17889 bool result = true;
17890 int i;
17891
17892 length = section->sh_size;
17893 if (length == 0)
17894 {
17895 printf (_("\nSection '%s' has no debugging data.\n"), print_name);
17896 return true;
17897 }
17898 if (section->sh_type == SHT_NOBITS)
17899 {
17900 /* There is no point in dumping the contents of a debugging section
17901 which has the NOBITS type - the bits in the file will be random.
17902 This can happen when a file containing a .eh_frame section is
17903 stripped with the --only-keep-debug command line option. */
17904 printf (_("section '%s' has the NOBITS type - its contents are unreliable.\n"),
17905 print_name);
17906 return false;
17907 }
17908
17909 if (startswith (name, ".gnu.linkonce.wi."))
17910 name = ".debug_info";
17911
17912 /* See if we know how to display the contents of this section. */
17913 for (i = 0; i < max; i++)
17914 {
17915 enum dwarf_section_display_enum id = (enum dwarf_section_display_enum) i;
17916 struct dwarf_section_display * display = debug_displays + i;
17917 struct dwarf_section * sec = & display->section;
17918
17919 if (streq (sec->uncompressed_name, name)
17920 || (id == line && startswith (name, ".debug_line."))
17921 || (id == sframe && section->sh_type == SHT_GNU_SFRAME)
17922 || streq (sec->compressed_name, name))
17923 {
17924 bool secondary = (section != find_section (filedata, name));
17925
17926 if (secondary)
17927 free_debug_section (id);
17928
17929 if (i == line && startswith (name, ".debug_line."))
17930 sec->name = name;
17931 else if (id == sframe && section->sh_type == SHT_GNU_SFRAME)
17932 sec->name = name;
17933 else if (streq (sec->uncompressed_name, name))
17934 sec->name = sec->uncompressed_name;
17935 else
17936 sec->name = sec->compressed_name;
17937
17938 if (load_specific_debug_section (id, section, filedata))
17939 {
17940 /* If this debug section is part of a CU/TU set in a .dwp file,
17941 restrict load_debug_section to the sections in that set. */
17942 section_subset = find_cu_tu_set (filedata, shndx);
17943
17944 result &= display->display (sec, filedata);
17945
17946 section_subset = NULL;
17947
17948 if (secondary || (id != info && id != abbrev && id != debug_addr))
17949 free_debug_section (id);
17950 }
17951 break;
17952 }
17953 }
17954
17955 if (i == max)
17956 {
17957 printf (_("Unrecognized debug section: %s\n"), print_name);
17958 result = false;
17959 }
17960
17961 return result;
17962 }
17963
17964 /* Set DUMP_SECTS for all sections where dumps were requested
17965 based on section name. */
17966
17967 static void
17968 initialise_dumps_byname (Filedata * filedata)
17969 {
17970 struct dump_list_entry * cur;
17971
17972 for (cur = dump_sects_byname; cur; cur = cur->next)
17973 {
17974 unsigned int i;
17975 bool any = false;
17976
17977 for (i = 0; i < filedata->file_header.e_shnum; i++)
17978 if (section_name_valid (filedata, filedata->section_headers + i)
17979 && streq (section_name (filedata, filedata->section_headers + i),
17980 cur->name))
17981 {
17982 request_dump_bynumber (&filedata->dump, i, cur->type);
17983 any = true;
17984 }
17985
17986 if (!any && !filedata->is_separate)
17987 warn (_("Section '%s' was not dumped because it does not exist\n"),
17988 cur->name);
17989 }
17990 }
17991
17992 static bool
17993 process_section_contents (Filedata * filedata)
17994 {
17995 Elf_Internal_Shdr * section;
17996 unsigned int i;
17997 bool res = true;
17998
17999 if (! do_dump)
18000 return true;
18001
18002 initialise_dumps_byname (filedata);
18003
18004 for (i = 0, section = filedata->section_headers;
18005 i < filedata->file_header.e_shnum && i < filedata->dump.num_dump_sects;
18006 i++, section++)
18007 {
18008 dump_type dump = filedata->dump.dump_sects[i];
18009
18010 if (filedata->is_separate && ! process_links)
18011 dump &= DEBUG_DUMP;
18012
18013 if (dump & AUTO_DUMP)
18014 {
18015 switch (section->sh_type)
18016 {
18017 case SHT_PROGBITS:
18018 /* FIXME: There are lots of different type of section that have
18019 SHT_PROGBITS set in their header - code, debug info, etc. So
18020 we should check the section's name and interpret its contents
18021 that way, rather than just defaulting to a byte dump. */
18022 #ifdef SUPPORT_DISASSEMBLY
18023 res &= disassemble_section (section, filedata);
18024 #else
18025 res &= dump_section_as_bytes (section, filedata, false);
18026 #endif
18027 break;
18028
18029 case SHT_DYNSYM:
18030 case SHT_SYMTAB:
18031 res &= dump_symbol_section (section, filedata);
18032 break;
18033
18034 case SHT_STRTAB:
18035 res &= dump_section_as_strings (section, filedata);
18036 break;
18037
18038 case SHT_RELA:
18039 case SHT_REL:
18040 case SHT_RELR:
18041 res &= display_relocations (section, filedata, true);
18042 break;
18043
18044 case SHT_NOTE:
18045 res &= process_notes_at (filedata, section, section->sh_offset,
18046 section->sh_size, section->sh_addralign);
18047 break;
18048
18049 case SHT_NULL:
18050 inform (_("Unable to display section %d - it has a NULL type\n"), i);
18051 break;
18052
18053 case SHT_NOBITS:
18054 inform (_("Unable to display section %d - it has no contents\n"), i);
18055 break;
18056
18057 case SHT_HASH:
18058 case SHT_DYNAMIC:
18059 case SHT_GROUP:
18060 case SHT_GNU_ATTRIBUTES:
18061 /* FIXME: Implement these. */
18062 /* Fall through. */
18063 default:
18064 /* FIXME: Add Proc and OS specific section types ? */
18065 warn (_("Unable to determine how to dump section %d (type %#x)\n"),
18066 i, section->sh_type);
18067 res = false;
18068 break;
18069 }
18070 }
18071
18072 #ifdef SUPPORT_DISASSEMBLY
18073 if (dump & DISASS_DUMP)
18074 {
18075 if (! disassemble_section (section, filedata))
18076 res = false;
18077 }
18078 #endif
18079 if (dump & HEX_DUMP)
18080 {
18081 if (! dump_section_as_bytes (section, filedata, false))
18082 res = false;
18083 }
18084
18085 if (dump & RELOC_DUMP)
18086 {
18087 if (! dump_section_as_bytes (section, filedata, true))
18088 res = false;
18089 }
18090
18091 if (dump & STRING_DUMP)
18092 {
18093 if (! dump_section_as_strings (section, filedata))
18094 res = false;
18095 }
18096
18097 if (dump & DEBUG_DUMP)
18098 {
18099 if (! display_debug_section (i, section, filedata))
18100 res = false;
18101 }
18102
18103 #ifdef ENABLE_LIBCTF
18104 if (dump & CTF_DUMP)
18105 {
18106 if (! dump_section_as_ctf (section, filedata))
18107 res = false;
18108 }
18109 #endif
18110 if (dump & SFRAME_DUMP)
18111 {
18112 if (! display_debug_section (i, section, filedata))
18113 res = false;
18114 }
18115 }
18116
18117 if (! filedata->is_separate)
18118 {
18119 /* Check to see if the user requested a
18120 dump of a section that does not exist. */
18121 for (; i < filedata->dump.num_dump_sects; i++)
18122 if (filedata->dump.dump_sects[i])
18123 {
18124 warn (_("Section %d was not dumped because it does not exist!\n"), i);
18125 res = false;
18126 }
18127 }
18128
18129 return res;
18130 }
18131
18132 static void
18133 process_mips_fpe_exception (int mask)
18134 {
18135 if (mask)
18136 {
18137 bool first = true;
18138
18139 if (mask & OEX_FPU_INEX)
18140 fputs ("INEX", stdout), first = false;
18141 if (mask & OEX_FPU_UFLO)
18142 printf ("%sUFLO", first ? "" : "|"), first = false;
18143 if (mask & OEX_FPU_OFLO)
18144 printf ("%sOFLO", first ? "" : "|"), first = false;
18145 if (mask & OEX_FPU_DIV0)
18146 printf ("%sDIV0", first ? "" : "|"), first = false;
18147 if (mask & OEX_FPU_INVAL)
18148 printf ("%sINVAL", first ? "" : "|");
18149 }
18150 else
18151 fputs ("0", stdout);
18152 }
18153
18154 /* Display's the value of TAG at location P. If TAG is
18155 greater than 0 it is assumed to be an unknown tag, and
18156 a message is printed to this effect. Otherwise it is
18157 assumed that a message has already been printed.
18158
18159 If the bottom bit of TAG is set it assumed to have a
18160 string value, otherwise it is assumed to have an integer
18161 value.
18162
18163 Returns an updated P pointing to the first unread byte
18164 beyond the end of TAG's value.
18165
18166 Reads at or beyond END will not be made. */
18167
18168 static unsigned char *
18169 display_tag_value (signed int tag,
18170 unsigned char * p,
18171 const unsigned char * const end)
18172 {
18173 uint64_t val;
18174
18175 if (tag > 0)
18176 printf (" Tag_unknown_%d: ", tag);
18177
18178 if (p >= end)
18179 {
18180 warn (_("<corrupt tag>\n"));
18181 }
18182 else if (tag & 1)
18183 {
18184 /* PR 17531 file: 027-19978-0.004. */
18185 size_t maxlen = end - p;
18186
18187 putchar ('"');
18188 if (maxlen > 0)
18189 {
18190 maxlen -= 1; /* Remove \0 from the character count. */
18191 print_symbol_name ((int) maxlen, (const char *) p);
18192 size_t len = strnlen ((char *) p, maxlen);
18193 if (len == maxlen && p[maxlen] != '\0')
18194 printf (_("<corrupt string tag>"));
18195 p += len + 1;
18196 }
18197 else
18198 {
18199 printf (_("<corrupt string tag>"));
18200 p = (unsigned char *) end;
18201 }
18202 printf ("\"\n");
18203 }
18204 else
18205 {
18206 READ_ULEB (val, p, end);
18207 printf ("%" PRId64 " (0x%" PRIx64 ")\n", val, val);
18208 }
18209
18210 assert (p <= end);
18211 return p;
18212 }
18213
18214 /* ARC ABI attributes section. */
18215
18216 static unsigned char *
18217 display_arc_attribute (unsigned char * p,
18218 const unsigned char * const end)
18219 {
18220 unsigned int tag;
18221 unsigned int val;
18222
18223 READ_ULEB (tag, p, end);
18224
18225 switch (tag)
18226 {
18227 case Tag_ARC_PCS_config:
18228 READ_ULEB (val, p, end);
18229 printf (" Tag_ARC_PCS_config: ");
18230 switch (val)
18231 {
18232 case 0:
18233 printf (_("Absent/Non standard\n"));
18234 break;
18235 case 1:
18236 printf (_("Bare metal/mwdt\n"));
18237 break;
18238 case 2:
18239 printf (_("Bare metal/newlib\n"));
18240 break;
18241 case 3:
18242 printf (_("Linux/uclibc\n"));
18243 break;
18244 case 4:
18245 printf (_("Linux/glibc\n"));
18246 break;
18247 default:
18248 printf (_("Unknown\n"));
18249 break;
18250 }
18251 break;
18252
18253 case Tag_ARC_CPU_base:
18254 READ_ULEB (val, p, end);
18255 printf (" Tag_ARC_CPU_base: ");
18256 switch (val)
18257 {
18258 default:
18259 case TAG_CPU_NONE:
18260 printf (_("Absent\n"));
18261 break;
18262 case TAG_CPU_ARC6xx:
18263 printf ("ARC6xx\n");
18264 break;
18265 case TAG_CPU_ARC7xx:
18266 printf ("ARC7xx\n");
18267 break;
18268 case TAG_CPU_ARCEM:
18269 printf ("ARCEM\n");
18270 break;
18271 case TAG_CPU_ARCHS:
18272 printf ("ARCHS\n");
18273 break;
18274 }
18275 break;
18276
18277 case Tag_ARC_CPU_variation:
18278 READ_ULEB (val, p, end);
18279 printf (" Tag_ARC_CPU_variation: ");
18280 switch (val)
18281 {
18282 default:
18283 if (val > 0 && val < 16)
18284 printf ("Core%d\n", val);
18285 else
18286 printf ("Unknown\n");
18287 break;
18288
18289 case 0:
18290 printf (_("Absent\n"));
18291 break;
18292 }
18293 break;
18294
18295 case Tag_ARC_CPU_name:
18296 printf (" Tag_ARC_CPU_name: ");
18297 p = display_tag_value (-1, p, end);
18298 break;
18299
18300 case Tag_ARC_ABI_rf16:
18301 READ_ULEB (val, p, end);
18302 printf (" Tag_ARC_ABI_rf16: %s\n", val ? _("yes") : _("no"));
18303 break;
18304
18305 case Tag_ARC_ABI_osver:
18306 READ_ULEB (val, p, end);
18307 printf (" Tag_ARC_ABI_osver: v%d\n", val);
18308 break;
18309
18310 case Tag_ARC_ABI_pic:
18311 case Tag_ARC_ABI_sda:
18312 READ_ULEB (val, p, end);
18313 printf (tag == Tag_ARC_ABI_sda ? " Tag_ARC_ABI_sda: "
18314 : " Tag_ARC_ABI_pic: ");
18315 switch (val)
18316 {
18317 case 0:
18318 printf (_("Absent\n"));
18319 break;
18320 case 1:
18321 printf ("MWDT\n");
18322 break;
18323 case 2:
18324 printf ("GNU\n");
18325 break;
18326 default:
18327 printf (_("Unknown\n"));
18328 break;
18329 }
18330 break;
18331
18332 case Tag_ARC_ABI_tls:
18333 READ_ULEB (val, p, end);
18334 printf (" Tag_ARC_ABI_tls: %s\n", val ? "r25": "none");
18335 break;
18336
18337 case Tag_ARC_ABI_enumsize:
18338 READ_ULEB (val, p, end);
18339 printf (" Tag_ARC_ABI_enumsize: %s\n", val ? _("default") :
18340 _("smallest"));
18341 break;
18342
18343 case Tag_ARC_ABI_exceptions:
18344 READ_ULEB (val, p, end);
18345 printf (" Tag_ARC_ABI_exceptions: %s\n", val ? _("OPTFP")
18346 : _("default"));
18347 break;
18348
18349 case Tag_ARC_ABI_double_size:
18350 READ_ULEB (val, p, end);
18351 printf (" Tag_ARC_ABI_double_size: %d\n", val);
18352 break;
18353
18354 case Tag_ARC_ISA_config:
18355 printf (" Tag_ARC_ISA_config: ");
18356 p = display_tag_value (-1, p, end);
18357 break;
18358
18359 case Tag_ARC_ISA_apex:
18360 printf (" Tag_ARC_ISA_apex: ");
18361 p = display_tag_value (-1, p, end);
18362 break;
18363
18364 case Tag_ARC_ISA_mpy_option:
18365 READ_ULEB (val, p, end);
18366 printf (" Tag_ARC_ISA_mpy_option: %d\n", val);
18367 break;
18368
18369 case Tag_ARC_ATR_version:
18370 READ_ULEB (val, p, end);
18371 printf (" Tag_ARC_ATR_version: %d\n", val);
18372 break;
18373
18374 default:
18375 return display_tag_value (tag & 1, p, end);
18376 }
18377
18378 return p;
18379 }
18380
18381 /* ARM EABI attributes section. */
18382 typedef struct
18383 {
18384 unsigned int tag;
18385 const char * name;
18386 /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup. */
18387 unsigned int type;
18388 const char *const *table;
18389 } arm_attr_public_tag;
18390
18391 static const char *const arm_attr_tag_CPU_arch[] =
18392 {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
18393 "v6K", "v7", "v6-M", "v6S-M", "v7E-M", "v8", "v8-R", "v8-M.baseline",
18394 "v8-M.mainline", "v8.1-A", "v8.2-A", "v8.3-A",
18395 "v8.1-M.mainline", "v9"};
18396 static const char *const arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
18397 static const char *const arm_attr_tag_THUMB_ISA_use[] =
18398 {"No", "Thumb-1", "Thumb-2", "Yes"};
18399 static const char *const arm_attr_tag_FP_arch[] =
18400 {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16", "VFPv4", "VFPv4-D16",
18401 "FP for ARMv8", "FPv5/FP-D16 for ARMv8"};
18402 static const char *const arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
18403 static const char *const arm_attr_tag_Advanced_SIMD_arch[] =
18404 {"No", "NEONv1", "NEONv1 with Fused-MAC", "NEON for ARMv8",
18405 "NEON for ARMv8.1"};
18406 static const char *const arm_attr_tag_PCS_config[] =
18407 {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
18408 "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
18409 static const char *const arm_attr_tag_ABI_PCS_R9_use[] =
18410 {"V6", "SB", "TLS", "Unused"};
18411 static const char *const arm_attr_tag_ABI_PCS_RW_data[] =
18412 {"Absolute", "PC-relative", "SB-relative", "None"};
18413 static const char *const arm_attr_tag_ABI_PCS_RO_data[] =
18414 {"Absolute", "PC-relative", "None"};
18415 static const char *const arm_attr_tag_ABI_PCS_GOT_use[] =
18416 {"None", "direct", "GOT-indirect"};
18417 static const char *const arm_attr_tag_ABI_PCS_wchar_t[] =
18418 {"None", "??? 1", "2", "??? 3", "4"};
18419 static const char *const arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
18420 static const char *const arm_attr_tag_ABI_FP_denormal[] =
18421 {"Unused", "Needed", "Sign only"};
18422 static const char *const arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
18423 static const char *const arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
18424 static const char *const arm_attr_tag_ABI_FP_number_model[] =
18425 {"Unused", "Finite", "RTABI", "IEEE 754"};
18426 static const char *const arm_attr_tag_ABI_enum_size[] =
18427 {"Unused", "small", "int", "forced to int"};
18428 static const char *const arm_attr_tag_ABI_HardFP_use[] =
18429 {"As Tag_FP_arch", "SP only", "Reserved", "Deprecated"};
18430 static const char *const arm_attr_tag_ABI_VFP_args[] =
18431 {"AAPCS", "VFP registers", "custom", "compatible"};
18432 static const char *const arm_attr_tag_ABI_WMMX_args[] =
18433 {"AAPCS", "WMMX registers", "custom"};
18434 static const char *const arm_attr_tag_ABI_optimization_goals[] =
18435 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
18436 "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
18437 static const char *const arm_attr_tag_ABI_FP_optimization_goals[] =
18438 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
18439 "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
18440 static const char *const arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
18441 static const char *const arm_attr_tag_FP_HP_extension[] =
18442 {"Not Allowed", "Allowed"};
18443 static const char *const arm_attr_tag_ABI_FP_16bit_format[] =
18444 {"None", "IEEE 754", "Alternative Format"};
18445 static const char *const arm_attr_tag_DSP_extension[] =
18446 {"Follow architecture", "Allowed"};
18447 static const char *const arm_attr_tag_MPextension_use[] =
18448 {"Not Allowed", "Allowed"};
18449 static const char *const arm_attr_tag_DIV_use[] =
18450 {"Allowed in Thumb-ISA, v7-R or v7-M", "Not allowed",
18451 "Allowed in v7-A with integer division extension"};
18452 static const char *const arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
18453 static const char *const arm_attr_tag_Virtualization_use[] =
18454 {"Not Allowed", "TrustZone", "Virtualization Extensions",
18455 "TrustZone and Virtualization Extensions"};
18456 static const char *const arm_attr_tag_MPextension_use_legacy[] =
18457 {"Not Allowed", "Allowed"};
18458
18459 static const char *const arm_attr_tag_MVE_arch[] =
18460 {"No MVE", "MVE Integer only", "MVE Integer and FP"};
18461
18462 static const char * arm_attr_tag_PAC_extension[] =
18463 {"No PAC/AUT instructions",
18464 "PAC/AUT instructions permitted in the NOP space",
18465 "PAC/AUT instructions permitted in the NOP and in the non-NOP space"};
18466
18467 static const char * arm_attr_tag_BTI_extension[] =
18468 {"BTI instructions not permitted",
18469 "BTI instructions permitted in the NOP space",
18470 "BTI instructions permitted in the NOP and in the non-NOP space"};
18471
18472 static const char * arm_attr_tag_BTI_use[] =
18473 {"Compiled without branch target enforcement",
18474 "Compiled with branch target enforcement"};
18475
18476 static const char * arm_attr_tag_PACRET_use[] =
18477 {"Compiled without return address signing and authentication",
18478 "Compiled with return address signing and authentication"};
18479
18480 #define LOOKUP(id, name) \
18481 {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
18482 static arm_attr_public_tag arm_attr_public_tags[] =
18483 {
18484 {4, "CPU_raw_name", 1, NULL},
18485 {5, "CPU_name", 1, NULL},
18486 LOOKUP(6, CPU_arch),
18487 {7, "CPU_arch_profile", 0, NULL},
18488 LOOKUP(8, ARM_ISA_use),
18489 LOOKUP(9, THUMB_ISA_use),
18490 LOOKUP(10, FP_arch),
18491 LOOKUP(11, WMMX_arch),
18492 LOOKUP(12, Advanced_SIMD_arch),
18493 LOOKUP(13, PCS_config),
18494 LOOKUP(14, ABI_PCS_R9_use),
18495 LOOKUP(15, ABI_PCS_RW_data),
18496 LOOKUP(16, ABI_PCS_RO_data),
18497 LOOKUP(17, ABI_PCS_GOT_use),
18498 LOOKUP(18, ABI_PCS_wchar_t),
18499 LOOKUP(19, ABI_FP_rounding),
18500 LOOKUP(20, ABI_FP_denormal),
18501 LOOKUP(21, ABI_FP_exceptions),
18502 LOOKUP(22, ABI_FP_user_exceptions),
18503 LOOKUP(23, ABI_FP_number_model),
18504 {24, "ABI_align_needed", 0, NULL},
18505 {25, "ABI_align_preserved", 0, NULL},
18506 LOOKUP(26, ABI_enum_size),
18507 LOOKUP(27, ABI_HardFP_use),
18508 LOOKUP(28, ABI_VFP_args),
18509 LOOKUP(29, ABI_WMMX_args),
18510 LOOKUP(30, ABI_optimization_goals),
18511 LOOKUP(31, ABI_FP_optimization_goals),
18512 {32, "compatibility", 0, NULL},
18513 LOOKUP(34, CPU_unaligned_access),
18514 LOOKUP(36, FP_HP_extension),
18515 LOOKUP(38, ABI_FP_16bit_format),
18516 LOOKUP(42, MPextension_use),
18517 LOOKUP(44, DIV_use),
18518 LOOKUP(46, DSP_extension),
18519 LOOKUP(48, MVE_arch),
18520 LOOKUP(50, PAC_extension),
18521 LOOKUP(52, BTI_extension),
18522 LOOKUP(74, BTI_use),
18523 LOOKUP(76, PACRET_use),
18524 {64, "nodefaults", 0, NULL},
18525 {65, "also_compatible_with", 0, NULL},
18526 LOOKUP(66, T2EE_use),
18527 {67, "conformance", 1, NULL},
18528 LOOKUP(68, Virtualization_use),
18529 LOOKUP(70, MPextension_use_legacy)
18530 };
18531 #undef LOOKUP
18532
18533 static unsigned char *
18534 display_arm_attribute (unsigned char * p,
18535 const unsigned char * const end)
18536 {
18537 unsigned int tag;
18538 unsigned int val;
18539 arm_attr_public_tag * attr;
18540 unsigned i;
18541 unsigned int type;
18542
18543 READ_ULEB (tag, p, end);
18544 attr = NULL;
18545 for (i = 0; i < ARRAY_SIZE (arm_attr_public_tags); i++)
18546 {
18547 if (arm_attr_public_tags[i].tag == tag)
18548 {
18549 attr = &arm_attr_public_tags[i];
18550 break;
18551 }
18552 }
18553
18554 if (attr)
18555 {
18556 printf (" Tag_%s: ", attr->name);
18557 switch (attr->type)
18558 {
18559 case 0:
18560 switch (tag)
18561 {
18562 case 7: /* Tag_CPU_arch_profile. */
18563 READ_ULEB (val, p, end);
18564 switch (val)
18565 {
18566 case 0: printf (_("None\n")); break;
18567 case 'A': printf (_("Application\n")); break;
18568 case 'R': printf (_("Realtime\n")); break;
18569 case 'M': printf (_("Microcontroller\n")); break;
18570 case 'S': printf (_("Application or Realtime\n")); break;
18571 default: printf ("??? (%d)\n", val); break;
18572 }
18573 break;
18574
18575 case 24: /* Tag_align_needed. */
18576 READ_ULEB (val, p, end);
18577 switch (val)
18578 {
18579 case 0: printf (_("None\n")); break;
18580 case 1: printf (_("8-byte\n")); break;
18581 case 2: printf (_("4-byte\n")); break;
18582 case 3: printf ("??? 3\n"); break;
18583 default:
18584 if (val <= 12)
18585 printf (_("8-byte and up to %d-byte extended\n"),
18586 1 << val);
18587 else
18588 printf ("??? (%d)\n", val);
18589 break;
18590 }
18591 break;
18592
18593 case 25: /* Tag_align_preserved. */
18594 READ_ULEB (val, p, end);
18595 switch (val)
18596 {
18597 case 0: printf (_("None\n")); break;
18598 case 1: printf (_("8-byte, except leaf SP\n")); break;
18599 case 2: printf (_("8-byte\n")); break;
18600 case 3: printf ("??? 3\n"); break;
18601 default:
18602 if (val <= 12)
18603 printf (_("8-byte and up to %d-byte extended\n"),
18604 1 << val);
18605 else
18606 printf ("??? (%d)\n", val);
18607 break;
18608 }
18609 break;
18610
18611 case 32: /* Tag_compatibility. */
18612 {
18613 READ_ULEB (val, p, end);
18614 printf (_("flag = %d, vendor = "), val);
18615 if (p < end - 1)
18616 {
18617 size_t maxlen = (end - p) - 1;
18618
18619 print_symbol_name ((int) maxlen, (const char *) p);
18620 p += strnlen ((char *) p, maxlen) + 1;
18621 }
18622 else
18623 {
18624 printf (_("<corrupt>"));
18625 p = (unsigned char *) end;
18626 }
18627 putchar ('\n');
18628 }
18629 break;
18630
18631 case 64: /* Tag_nodefaults. */
18632 /* PR 17531: file: 001-505008-0.01. */
18633 if (p < end)
18634 p++;
18635 printf (_("True\n"));
18636 break;
18637
18638 case 65: /* Tag_also_compatible_with. */
18639 READ_ULEB (val, p, end);
18640 if (val == 6 /* Tag_CPU_arch. */)
18641 {
18642 READ_ULEB (val, p, end);
18643 if ((unsigned int) val >= ARRAY_SIZE (arm_attr_tag_CPU_arch))
18644 printf ("??? (%d)\n", val);
18645 else
18646 printf ("%s\n", arm_attr_tag_CPU_arch[val]);
18647 }
18648 else
18649 printf ("???\n");
18650 while (p < end && *(p++) != '\0' /* NUL terminator. */)
18651 ;
18652 break;
18653
18654 default:
18655 printf (_("<unknown: %d>\n"), tag);
18656 break;
18657 }
18658 return p;
18659
18660 case 1:
18661 return display_tag_value (-1, p, end);
18662 case 2:
18663 return display_tag_value (0, p, end);
18664
18665 default:
18666 assert (attr->type & 0x80);
18667 READ_ULEB (val, p, end);
18668 type = attr->type & 0x7f;
18669 if (val >= type)
18670 printf ("??? (%d)\n", val);
18671 else
18672 printf ("%s\n", attr->table[val]);
18673 return p;
18674 }
18675 }
18676
18677 return display_tag_value (tag, p, end);
18678 }
18679
18680 static unsigned char *
18681 display_gnu_attribute (unsigned char * p,
18682 unsigned char * (* display_proc_gnu_attribute)
18683 (unsigned char *, unsigned int, const unsigned char * const),
18684 const unsigned char * const end)
18685 {
18686 unsigned int tag;
18687 unsigned int val;
18688
18689 READ_ULEB (tag, p, end);
18690
18691 /* Tag_compatibility is the only generic GNU attribute defined at
18692 present. */
18693 if (tag == 32)
18694 {
18695 READ_ULEB (val, p, end);
18696
18697 printf (_("flag = %d, vendor = "), val);
18698 if (p == end)
18699 {
18700 printf (_("<corrupt>\n"));
18701 warn (_("corrupt vendor attribute\n"));
18702 }
18703 else
18704 {
18705 if (p < end - 1)
18706 {
18707 size_t maxlen = (end - p) - 1;
18708
18709 print_symbol_name ((int) maxlen, (const char *) p);
18710 p += strnlen ((char *) p, maxlen) + 1;
18711 }
18712 else
18713 {
18714 printf (_("<corrupt>"));
18715 p = (unsigned char *) end;
18716 }
18717 putchar ('\n');
18718 }
18719 return p;
18720 }
18721
18722 if ((tag & 2) == 0 && display_proc_gnu_attribute)
18723 return display_proc_gnu_attribute (p, tag, end);
18724
18725 return display_tag_value (tag, p, end);
18726 }
18727
18728 static unsigned char *
18729 display_m68k_gnu_attribute (unsigned char * p,
18730 unsigned int tag,
18731 const unsigned char * const end)
18732 {
18733 unsigned int val;
18734
18735 if (tag == Tag_GNU_M68K_ABI_FP)
18736 {
18737 printf (" Tag_GNU_M68K_ABI_FP: ");
18738 if (p == end)
18739 {
18740 printf (_("<corrupt>\n"));
18741 return p;
18742 }
18743 READ_ULEB (val, p, end);
18744
18745 if (val > 3)
18746 printf ("(%#x), ", val);
18747
18748 switch (val & 3)
18749 {
18750 case 0:
18751 printf (_("unspecified hard/soft float\n"));
18752 break;
18753 case 1:
18754 printf (_("hard float\n"));
18755 break;
18756 case 2:
18757 printf (_("soft float\n"));
18758 break;
18759 }
18760 return p;
18761 }
18762
18763 return display_tag_value (tag & 1, p, end);
18764 }
18765
18766 static unsigned char *
18767 display_power_gnu_attribute (unsigned char * p,
18768 unsigned int tag,
18769 const unsigned char * const end)
18770 {
18771 unsigned int val;
18772
18773 if (tag == Tag_GNU_Power_ABI_FP)
18774 {
18775 printf (" Tag_GNU_Power_ABI_FP: ");
18776 if (p == end)
18777 {
18778 printf (_("<corrupt>\n"));
18779 return p;
18780 }
18781 READ_ULEB (val, p, end);
18782
18783 if (val > 15)
18784 printf ("(%#x), ", val);
18785
18786 switch (val & 3)
18787 {
18788 case 0:
18789 printf (_("unspecified hard/soft float, "));
18790 break;
18791 case 1:
18792 printf (_("hard float, "));
18793 break;
18794 case 2:
18795 printf (_("soft float, "));
18796 break;
18797 case 3:
18798 printf (_("single-precision hard float, "));
18799 break;
18800 }
18801
18802 switch (val & 0xC)
18803 {
18804 case 0:
18805 printf (_("unspecified long double\n"));
18806 break;
18807 case 4:
18808 printf (_("128-bit IBM long double\n"));
18809 break;
18810 case 8:
18811 printf (_("64-bit long double\n"));
18812 break;
18813 case 12:
18814 printf (_("128-bit IEEE long double\n"));
18815 break;
18816 }
18817 return p;
18818 }
18819
18820 if (tag == Tag_GNU_Power_ABI_Vector)
18821 {
18822 printf (" Tag_GNU_Power_ABI_Vector: ");
18823 if (p == end)
18824 {
18825 printf (_("<corrupt>\n"));
18826 return p;
18827 }
18828 READ_ULEB (val, p, end);
18829
18830 if (val > 3)
18831 printf ("(%#x), ", val);
18832
18833 switch (val & 3)
18834 {
18835 case 0:
18836 printf (_("unspecified\n"));
18837 break;
18838 case 1:
18839 printf (_("generic\n"));
18840 break;
18841 case 2:
18842 printf ("AltiVec\n");
18843 break;
18844 case 3:
18845 printf ("SPE\n");
18846 break;
18847 }
18848 return p;
18849 }
18850
18851 if (tag == Tag_GNU_Power_ABI_Struct_Return)
18852 {
18853 printf (" Tag_GNU_Power_ABI_Struct_Return: ");
18854 if (p == end)
18855 {
18856 printf (_("<corrupt>\n"));
18857 return p;
18858 }
18859 READ_ULEB (val, p, end);
18860
18861 if (val > 2)
18862 printf ("(%#x), ", val);
18863
18864 switch (val & 3)
18865 {
18866 case 0:
18867 printf (_("unspecified\n"));
18868 break;
18869 case 1:
18870 printf ("r3/r4\n");
18871 break;
18872 case 2:
18873 printf (_("memory\n"));
18874 break;
18875 case 3:
18876 printf ("???\n");
18877 break;
18878 }
18879 return p;
18880 }
18881
18882 return display_tag_value (tag & 1, p, end);
18883 }
18884
18885 static unsigned char *
18886 display_s390_gnu_attribute (unsigned char * p,
18887 unsigned int tag,
18888 const unsigned char * const end)
18889 {
18890 unsigned int val;
18891
18892 if (tag == Tag_GNU_S390_ABI_Vector)
18893 {
18894 printf (" Tag_GNU_S390_ABI_Vector: ");
18895 READ_ULEB (val, p, end);
18896
18897 switch (val)
18898 {
18899 case 0:
18900 printf (_("any\n"));
18901 break;
18902 case 1:
18903 printf (_("software\n"));
18904 break;
18905 case 2:
18906 printf (_("hardware\n"));
18907 break;
18908 default:
18909 printf ("??? (%d)\n", val);
18910 break;
18911 }
18912 return p;
18913 }
18914
18915 return display_tag_value (tag & 1, p, end);
18916 }
18917
18918 static void
18919 display_sparc_hwcaps (unsigned int mask)
18920 {
18921 if (mask)
18922 {
18923 bool first = true;
18924
18925 if (mask & ELF_SPARC_HWCAP_MUL32)
18926 fputs ("mul32", stdout), first = false;
18927 if (mask & ELF_SPARC_HWCAP_DIV32)
18928 printf ("%sdiv32", first ? "" : "|"), first = false;
18929 if (mask & ELF_SPARC_HWCAP_FSMULD)
18930 printf ("%sfsmuld", first ? "" : "|"), first = false;
18931 if (mask & ELF_SPARC_HWCAP_V8PLUS)
18932 printf ("%sv8plus", first ? "" : "|"), first = false;
18933 if (mask & ELF_SPARC_HWCAP_POPC)
18934 printf ("%spopc", first ? "" : "|"), first = false;
18935 if (mask & ELF_SPARC_HWCAP_VIS)
18936 printf ("%svis", first ? "" : "|"), first = false;
18937 if (mask & ELF_SPARC_HWCAP_VIS2)
18938 printf ("%svis2", first ? "" : "|"), first = false;
18939 if (mask & ELF_SPARC_HWCAP_ASI_BLK_INIT)
18940 printf ("%sASIBlkInit", first ? "" : "|"), first = false;
18941 if (mask & ELF_SPARC_HWCAP_FMAF)
18942 printf ("%sfmaf", first ? "" : "|"), first = false;
18943 if (mask & ELF_SPARC_HWCAP_VIS3)
18944 printf ("%svis3", first ? "" : "|"), first = false;
18945 if (mask & ELF_SPARC_HWCAP_HPC)
18946 printf ("%shpc", first ? "" : "|"), first = false;
18947 if (mask & ELF_SPARC_HWCAP_RANDOM)
18948 printf ("%srandom", first ? "" : "|"), first = false;
18949 if (mask & ELF_SPARC_HWCAP_TRANS)
18950 printf ("%strans", first ? "" : "|"), first = false;
18951 if (mask & ELF_SPARC_HWCAP_FJFMAU)
18952 printf ("%sfjfmau", first ? "" : "|"), first = false;
18953 if (mask & ELF_SPARC_HWCAP_IMA)
18954 printf ("%sima", first ? "" : "|"), first = false;
18955 if (mask & ELF_SPARC_HWCAP_ASI_CACHE_SPARING)
18956 printf ("%scspare", first ? "" : "|"), first = false;
18957 }
18958 else
18959 fputc ('0', stdout);
18960 fputc ('\n', stdout);
18961 }
18962
18963 static void
18964 display_sparc_hwcaps2 (unsigned int mask)
18965 {
18966 if (mask)
18967 {
18968 bool first = true;
18969
18970 if (mask & ELF_SPARC_HWCAP2_FJATHPLUS)
18971 fputs ("fjathplus", stdout), first = false;
18972 if (mask & ELF_SPARC_HWCAP2_VIS3B)
18973 printf ("%svis3b", first ? "" : "|"), first = false;
18974 if (mask & ELF_SPARC_HWCAP2_ADP)
18975 printf ("%sadp", first ? "" : "|"), first = false;
18976 if (mask & ELF_SPARC_HWCAP2_SPARC5)
18977 printf ("%ssparc5", first ? "" : "|"), first = false;
18978 if (mask & ELF_SPARC_HWCAP2_MWAIT)
18979 printf ("%smwait", first ? "" : "|"), first = false;
18980 if (mask & ELF_SPARC_HWCAP2_XMPMUL)
18981 printf ("%sxmpmul", first ? "" : "|"), first = false;
18982 if (mask & ELF_SPARC_HWCAP2_XMONT)
18983 printf ("%sxmont2", first ? "" : "|"), first = false;
18984 if (mask & ELF_SPARC_HWCAP2_NSEC)
18985 printf ("%snsec", first ? "" : "|"), first = false;
18986 if (mask & ELF_SPARC_HWCAP2_FJATHHPC)
18987 printf ("%sfjathhpc", first ? "" : "|"), first = false;
18988 if (mask & ELF_SPARC_HWCAP2_FJDES)
18989 printf ("%sfjdes", first ? "" : "|"), first = false;
18990 if (mask & ELF_SPARC_HWCAP2_FJAES)
18991 printf ("%sfjaes", first ? "" : "|"), first = false;
18992 }
18993 else
18994 fputc ('0', stdout);
18995 fputc ('\n', stdout);
18996 }
18997
18998 static unsigned char *
18999 display_sparc_gnu_attribute (unsigned char * p,
19000 unsigned int tag,
19001 const unsigned char * const end)
19002 {
19003 unsigned int val;
19004
19005 if (tag == Tag_GNU_Sparc_HWCAPS)
19006 {
19007 READ_ULEB (val, p, end);
19008 printf (" Tag_GNU_Sparc_HWCAPS: ");
19009 display_sparc_hwcaps (val);
19010 return p;
19011 }
19012 if (tag == Tag_GNU_Sparc_HWCAPS2)
19013 {
19014 READ_ULEB (val, p, end);
19015 printf (" Tag_GNU_Sparc_HWCAPS2: ");
19016 display_sparc_hwcaps2 (val);
19017 return p;
19018 }
19019
19020 return display_tag_value (tag, p, end);
19021 }
19022
19023 static void
19024 print_mips_fp_abi_value (unsigned int val)
19025 {
19026 switch (val)
19027 {
19028 case Val_GNU_MIPS_ABI_FP_ANY:
19029 printf (_("Hard or soft float\n"));
19030 break;
19031 case Val_GNU_MIPS_ABI_FP_DOUBLE:
19032 printf (_("Hard float (double precision)\n"));
19033 break;
19034 case Val_GNU_MIPS_ABI_FP_SINGLE:
19035 printf (_("Hard float (single precision)\n"));
19036 break;
19037 case Val_GNU_MIPS_ABI_FP_SOFT:
19038 printf (_("Soft float\n"));
19039 break;
19040 case Val_GNU_MIPS_ABI_FP_OLD_64:
19041 printf (_("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
19042 break;
19043 case Val_GNU_MIPS_ABI_FP_XX:
19044 printf (_("Hard float (32-bit CPU, Any FPU)\n"));
19045 break;
19046 case Val_GNU_MIPS_ABI_FP_64:
19047 printf (_("Hard float (32-bit CPU, 64-bit FPU)\n"));
19048 break;
19049 case Val_GNU_MIPS_ABI_FP_64A:
19050 printf (_("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
19051 break;
19052 case Val_GNU_MIPS_ABI_FP_NAN2008:
19053 printf (_("NaN 2008 compatibility\n"));
19054 break;
19055 default:
19056 printf ("??? (%d)\n", val);
19057 break;
19058 }
19059 }
19060
19061 static unsigned char *
19062 display_mips_gnu_attribute (unsigned char * p,
19063 unsigned int tag,
19064 const unsigned char * const end)
19065 {
19066 if (tag == Tag_GNU_MIPS_ABI_FP)
19067 {
19068 unsigned int val;
19069
19070 printf (" Tag_GNU_MIPS_ABI_FP: ");
19071 READ_ULEB (val, p, end);
19072 print_mips_fp_abi_value (val);
19073 return p;
19074 }
19075
19076 if (tag == Tag_GNU_MIPS_ABI_MSA)
19077 {
19078 unsigned int val;
19079
19080 printf (" Tag_GNU_MIPS_ABI_MSA: ");
19081 READ_ULEB (val, p, end);
19082
19083 switch (val)
19084 {
19085 case Val_GNU_MIPS_ABI_MSA_ANY:
19086 printf (_("Any MSA or not\n"));
19087 break;
19088 case Val_GNU_MIPS_ABI_MSA_128:
19089 printf (_("128-bit MSA\n"));
19090 break;
19091 default:
19092 printf ("??? (%d)\n", val);
19093 break;
19094 }
19095 return p;
19096 }
19097
19098 return display_tag_value (tag & 1, p, end);
19099 }
19100
19101 static unsigned char *
19102 display_tic6x_attribute (unsigned char * p,
19103 const unsigned char * const end)
19104 {
19105 unsigned int tag;
19106 unsigned int val;
19107
19108 READ_ULEB (tag, p, end);
19109
19110 switch (tag)
19111 {
19112 case Tag_ISA:
19113 printf (" Tag_ISA: ");
19114 READ_ULEB (val, p, end);
19115
19116 switch (val)
19117 {
19118 case C6XABI_Tag_ISA_none:
19119 printf (_("None\n"));
19120 break;
19121 case C6XABI_Tag_ISA_C62X:
19122 printf ("C62x\n");
19123 break;
19124 case C6XABI_Tag_ISA_C67X:
19125 printf ("C67x\n");
19126 break;
19127 case C6XABI_Tag_ISA_C67XP:
19128 printf ("C67x+\n");
19129 break;
19130 case C6XABI_Tag_ISA_C64X:
19131 printf ("C64x\n");
19132 break;
19133 case C6XABI_Tag_ISA_C64XP:
19134 printf ("C64x+\n");
19135 break;
19136 case C6XABI_Tag_ISA_C674X:
19137 printf ("C674x\n");
19138 break;
19139 default:
19140 printf ("??? (%d)\n", val);
19141 break;
19142 }
19143 return p;
19144
19145 case Tag_ABI_wchar_t:
19146 printf (" Tag_ABI_wchar_t: ");
19147 READ_ULEB (val, p, end);
19148 switch (val)
19149 {
19150 case 0:
19151 printf (_("Not used\n"));
19152 break;
19153 case 1:
19154 printf (_("2 bytes\n"));
19155 break;
19156 case 2:
19157 printf (_("4 bytes\n"));
19158 break;
19159 default:
19160 printf ("??? (%d)\n", val);
19161 break;
19162 }
19163 return p;
19164
19165 case Tag_ABI_stack_align_needed:
19166 printf (" Tag_ABI_stack_align_needed: ");
19167 READ_ULEB (val, p, end);
19168 switch (val)
19169 {
19170 case 0:
19171 printf (_("8-byte\n"));
19172 break;
19173 case 1:
19174 printf (_("16-byte\n"));
19175 break;
19176 default:
19177 printf ("??? (%d)\n", val);
19178 break;
19179 }
19180 return p;
19181
19182 case Tag_ABI_stack_align_preserved:
19183 READ_ULEB (val, p, end);
19184 printf (" Tag_ABI_stack_align_preserved: ");
19185 switch (val)
19186 {
19187 case 0:
19188 printf (_("8-byte\n"));
19189 break;
19190 case 1:
19191 printf (_("16-byte\n"));
19192 break;
19193 default:
19194 printf ("??? (%d)\n", val);
19195 break;
19196 }
19197 return p;
19198
19199 case Tag_ABI_DSBT:
19200 READ_ULEB (val, p, end);
19201 printf (" Tag_ABI_DSBT: ");
19202 switch (val)
19203 {
19204 case 0:
19205 printf (_("DSBT addressing not used\n"));
19206 break;
19207 case 1:
19208 printf (_("DSBT addressing used\n"));
19209 break;
19210 default:
19211 printf ("??? (%d)\n", val);
19212 break;
19213 }
19214 return p;
19215
19216 case Tag_ABI_PID:
19217 READ_ULEB (val, p, end);
19218 printf (" Tag_ABI_PID: ");
19219 switch (val)
19220 {
19221 case 0:
19222 printf (_("Data addressing position-dependent\n"));
19223 break;
19224 case 1:
19225 printf (_("Data addressing position-independent, GOT near DP\n"));
19226 break;
19227 case 2:
19228 printf (_("Data addressing position-independent, GOT far from DP\n"));
19229 break;
19230 default:
19231 printf ("??? (%d)\n", val);
19232 break;
19233 }
19234 return p;
19235
19236 case Tag_ABI_PIC:
19237 READ_ULEB (val, p, end);
19238 printf (" Tag_ABI_PIC: ");
19239 switch (val)
19240 {
19241 case 0:
19242 printf (_("Code addressing position-dependent\n"));
19243 break;
19244 case 1:
19245 printf (_("Code addressing position-independent\n"));
19246 break;
19247 default:
19248 printf ("??? (%d)\n", val);
19249 break;
19250 }
19251 return p;
19252
19253 case Tag_ABI_array_object_alignment:
19254 READ_ULEB (val, p, end);
19255 printf (" Tag_ABI_array_object_alignment: ");
19256 switch (val)
19257 {
19258 case 0:
19259 printf (_("8-byte\n"));
19260 break;
19261 case 1:
19262 printf (_("4-byte\n"));
19263 break;
19264 case 2:
19265 printf (_("16-byte\n"));
19266 break;
19267 default:
19268 printf ("??? (%d)\n", val);
19269 break;
19270 }
19271 return p;
19272
19273 case Tag_ABI_array_object_align_expected:
19274 READ_ULEB (val, p, end);
19275 printf (" Tag_ABI_array_object_align_expected: ");
19276 switch (val)
19277 {
19278 case 0:
19279 printf (_("8-byte\n"));
19280 break;
19281 case 1:
19282 printf (_("4-byte\n"));
19283 break;
19284 case 2:
19285 printf (_("16-byte\n"));
19286 break;
19287 default:
19288 printf ("??? (%d)\n", val);
19289 break;
19290 }
19291 return p;
19292
19293 case Tag_ABI_compatibility:
19294 {
19295 READ_ULEB (val, p, end);
19296 printf (" Tag_ABI_compatibility: ");
19297 printf (_("flag = %d, vendor = "), val);
19298 if (p < end - 1)
19299 {
19300 size_t maxlen = (end - p) - 1;
19301
19302 print_symbol_name ((int) maxlen, (const char *) p);
19303 p += strnlen ((char *) p, maxlen) + 1;
19304 }
19305 else
19306 {
19307 printf (_("<corrupt>"));
19308 p = (unsigned char *) end;
19309 }
19310 putchar ('\n');
19311 return p;
19312 }
19313
19314 case Tag_ABI_conformance:
19315 {
19316 printf (" Tag_ABI_conformance: \"");
19317 if (p < end - 1)
19318 {
19319 size_t maxlen = (end - p) - 1;
19320
19321 print_symbol_name ((int) maxlen, (const char *) p);
19322 p += strnlen ((char *) p, maxlen) + 1;
19323 }
19324 else
19325 {
19326 printf (_("<corrupt>"));
19327 p = (unsigned char *) end;
19328 }
19329 printf ("\"\n");
19330 return p;
19331 }
19332 }
19333
19334 return display_tag_value (tag, p, end);
19335 }
19336
19337 static void
19338 display_raw_attribute (unsigned char * p, unsigned char const * const end)
19339 {
19340 uint64_t addr = 0;
19341 size_t bytes = end - p;
19342
19343 assert (end >= p);
19344 while (bytes)
19345 {
19346 int j;
19347 int k;
19348 int lbytes = (bytes > 16 ? 16 : bytes);
19349
19350 printf (" 0x%8.8" PRIx64 " ", addr);
19351
19352 for (j = 0; j < 16; j++)
19353 {
19354 if (j < lbytes)
19355 printf ("%2.2x", p[j]);
19356 else
19357 printf (" ");
19358
19359 if ((j & 3) == 3)
19360 printf (" ");
19361 }
19362
19363 for (j = 0; j < lbytes; j++)
19364 {
19365 k = p[j];
19366 if (k >= ' ' && k < 0x7f)
19367 printf ("%c", k);
19368 else
19369 printf (".");
19370 }
19371
19372 putchar ('\n');
19373
19374 p += lbytes;
19375 bytes -= lbytes;
19376 addr += lbytes;
19377 }
19378
19379 putchar ('\n');
19380 }
19381
19382 static unsigned char *
19383 display_msp430_attribute (unsigned char * p,
19384 const unsigned char * const end)
19385 {
19386 uint64_t val;
19387 uint64_t tag;
19388
19389 READ_ULEB (tag, p, end);
19390
19391 switch (tag)
19392 {
19393 case OFBA_MSPABI_Tag_ISA:
19394 printf (" Tag_ISA: ");
19395 READ_ULEB (val, p, end);
19396 switch (val)
19397 {
19398 case 0: printf (_("None\n")); break;
19399 case 1: printf (_("MSP430\n")); break;
19400 case 2: printf (_("MSP430X\n")); break;
19401 default: printf ("??? (%" PRId64 ")\n", val); break;
19402 }
19403 break;
19404
19405 case OFBA_MSPABI_Tag_Code_Model:
19406 printf (" Tag_Code_Model: ");
19407 READ_ULEB (val, p, end);
19408 switch (val)
19409 {
19410 case 0: printf (_("None\n")); break;
19411 case 1: printf (_("Small\n")); break;
19412 case 2: printf (_("Large\n")); break;
19413 default: printf ("??? (%" PRId64 ")\n", val); break;
19414 }
19415 break;
19416
19417 case OFBA_MSPABI_Tag_Data_Model:
19418 printf (" Tag_Data_Model: ");
19419 READ_ULEB (val, p, end);
19420 switch (val)
19421 {
19422 case 0: printf (_("None\n")); break;
19423 case 1: printf (_("Small\n")); break;
19424 case 2: printf (_("Large\n")); break;
19425 case 3: printf (_("Restricted Large\n")); break;
19426 default: printf ("??? (%" PRId64 ")\n", val); break;
19427 }
19428 break;
19429
19430 default:
19431 printf (_(" <unknown tag %" PRId64 ">: "), tag);
19432
19433 if (tag & 1)
19434 {
19435 putchar ('"');
19436 if (p < end - 1)
19437 {
19438 size_t maxlen = (end - p) - 1;
19439
19440 print_symbol_name ((int) maxlen, (const char *) p);
19441 p += strnlen ((char *) p, maxlen) + 1;
19442 }
19443 else
19444 {
19445 printf (_("<corrupt>"));
19446 p = (unsigned char *) end;
19447 }
19448 printf ("\"\n");
19449 }
19450 else
19451 {
19452 READ_ULEB (val, p, end);
19453 printf ("%" PRId64 " (0x%" PRIx64 ")\n", val, val);
19454 }
19455 break;
19456 }
19457
19458 assert (p <= end);
19459 return p;
19460 }
19461
19462 static unsigned char *
19463 display_msp430_gnu_attribute (unsigned char * p,
19464 unsigned int tag,
19465 const unsigned char * const end)
19466 {
19467 if (tag == Tag_GNU_MSP430_Data_Region)
19468 {
19469 uint64_t val;
19470
19471 printf (" Tag_GNU_MSP430_Data_Region: ");
19472 READ_ULEB (val, p, end);
19473
19474 switch (val)
19475 {
19476 case Val_GNU_MSP430_Data_Region_Any:
19477 printf (_("Any Region\n"));
19478 break;
19479 case Val_GNU_MSP430_Data_Region_Lower:
19480 printf (_("Lower Region Only\n"));
19481 break;
19482 default:
19483 printf ("??? (%" PRIu64 ")\n", val);
19484 }
19485 return p;
19486 }
19487 return display_tag_value (tag & 1, p, end);
19488 }
19489
19490 struct riscv_attr_tag_t {
19491 const char *name;
19492 unsigned int tag;
19493 };
19494
19495 static struct riscv_attr_tag_t riscv_attr_tag[] =
19496 {
19497 #define T(tag) {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
19498 T(arch),
19499 T(priv_spec),
19500 T(priv_spec_minor),
19501 T(priv_spec_revision),
19502 T(unaligned_access),
19503 T(stack_align),
19504 #undef T
19505 };
19506
19507 static unsigned char *
19508 display_riscv_attribute (unsigned char *p,
19509 const unsigned char * const end)
19510 {
19511 uint64_t val;
19512 uint64_t tag;
19513 struct riscv_attr_tag_t *attr = NULL;
19514 unsigned i;
19515
19516 READ_ULEB (tag, p, end);
19517
19518 /* Find the name of attribute. */
19519 for (i = 0; i < ARRAY_SIZE (riscv_attr_tag); i++)
19520 {
19521 if (riscv_attr_tag[i].tag == tag)
19522 {
19523 attr = &riscv_attr_tag[i];
19524 break;
19525 }
19526 }
19527
19528 if (attr)
19529 printf (" %s: ", attr->name);
19530 else
19531 return display_tag_value (tag, p, end);
19532
19533 switch (tag)
19534 {
19535 case Tag_RISCV_priv_spec:
19536 case Tag_RISCV_priv_spec_minor:
19537 case Tag_RISCV_priv_spec_revision:
19538 READ_ULEB (val, p, end);
19539 printf ("%" PRIu64 "\n", val);
19540 break;
19541 case Tag_RISCV_unaligned_access:
19542 READ_ULEB (val, p, end);
19543 switch (val)
19544 {
19545 case 0:
19546 printf (_("No unaligned access\n"));
19547 break;
19548 case 1:
19549 printf (_("Unaligned access\n"));
19550 break;
19551 }
19552 break;
19553 case Tag_RISCV_stack_align:
19554 READ_ULEB (val, p, end);
19555 printf (_("%" PRIu64 "-bytes\n"), val);
19556 break;
19557 case Tag_RISCV_arch:
19558 p = display_tag_value (-1, p, end);
19559 break;
19560 default:
19561 return display_tag_value (tag, p, end);
19562 }
19563
19564 return p;
19565 }
19566
19567 static unsigned char *
19568 display_csky_attribute (unsigned char * p,
19569 const unsigned char * const end)
19570 {
19571 uint64_t tag;
19572 uint64_t val;
19573 READ_ULEB (tag, p, end);
19574
19575 if (tag >= Tag_CSKY_MAX)
19576 {
19577 return display_tag_value (-1, p, end);
19578 }
19579
19580 switch (tag)
19581 {
19582 case Tag_CSKY_ARCH_NAME:
19583 printf (" Tag_CSKY_ARCH_NAME:\t\t");
19584 return display_tag_value (-1, p, end);
19585 case Tag_CSKY_CPU_NAME:
19586 printf (" Tag_CSKY_CPU_NAME:\t\t");
19587 return display_tag_value (-1, p, end);
19588
19589 case Tag_CSKY_ISA_FLAGS:
19590 printf (" Tag_CSKY_ISA_FLAGS:\t\t");
19591 return display_tag_value (0, p, end);
19592 case Tag_CSKY_ISA_EXT_FLAGS:
19593 printf (" Tag_CSKY_ISA_EXT_FLAGS:\t");
19594 return display_tag_value (0, p, end);
19595
19596 case Tag_CSKY_DSP_VERSION:
19597 printf (" Tag_CSKY_DSP_VERSION:\t\t");
19598 READ_ULEB (val, p, end);
19599 if (val == VAL_CSKY_DSP_VERSION_EXTENSION)
19600 printf ("DSP Extension\n");
19601 else if (val == VAL_CSKY_DSP_VERSION_2)
19602 printf ("DSP 2.0\n");
19603 break;
19604
19605 case Tag_CSKY_VDSP_VERSION:
19606 printf (" Tag_CSKY_VDSP_VERSION:\t");
19607 READ_ULEB (val, p, end);
19608 printf ("VDSP Version %" PRId64 "\n", val);
19609 break;
19610
19611 case Tag_CSKY_FPU_VERSION:
19612 printf (" Tag_CSKY_FPU_VERSION:\t\t");
19613 READ_ULEB (val, p, end);
19614 if (val == VAL_CSKY_FPU_VERSION_1)
19615 printf ("ABIV1 FPU Version 1\n");
19616 else if (val == VAL_CSKY_FPU_VERSION_2)
19617 printf ("FPU Version 2\n");
19618 break;
19619
19620 case Tag_CSKY_FPU_ABI:
19621 printf (" Tag_CSKY_FPU_ABI:\t\t");
19622 READ_ULEB (val, p, end);
19623 if (val == VAL_CSKY_FPU_ABI_HARD)
19624 printf ("Hard\n");
19625 else if (val == VAL_CSKY_FPU_ABI_SOFTFP)
19626 printf ("SoftFP\n");
19627 else if (val == VAL_CSKY_FPU_ABI_SOFT)
19628 printf ("Soft\n");
19629 break;
19630 case Tag_CSKY_FPU_ROUNDING:
19631 READ_ULEB (val, p, end);
19632 if (val == 1)
19633 {
19634 printf (" Tag_CSKY_FPU_ROUNDING:\t");
19635 printf ("Needed\n");
19636 }
19637 break;
19638 case Tag_CSKY_FPU_DENORMAL:
19639 READ_ULEB (val, p, end);
19640 if (val == 1)
19641 {
19642 printf (" Tag_CSKY_FPU_DENORMAL:\t");
19643 printf ("Needed\n");
19644 }
19645 break;
19646 case Tag_CSKY_FPU_Exception:
19647 READ_ULEB (val, p, end);
19648 if (val == 1)
19649 {
19650 printf (" Tag_CSKY_FPU_Exception:\t");
19651 printf ("Needed\n");
19652 }
19653 break;
19654 case Tag_CSKY_FPU_NUMBER_MODULE:
19655 printf (" Tag_CSKY_FPU_NUMBER_MODULE:\t");
19656 return display_tag_value (-1, p, end);
19657 case Tag_CSKY_FPU_HARDFP:
19658 printf (" Tag_CSKY_FPU_HARDFP:\t\t");
19659 READ_ULEB (val, p, end);
19660 if (val & VAL_CSKY_FPU_HARDFP_HALF)
19661 printf (" Half");
19662 if (val & VAL_CSKY_FPU_HARDFP_SINGLE)
19663 printf (" Single");
19664 if (val & VAL_CSKY_FPU_HARDFP_DOUBLE)
19665 printf (" Double");
19666 printf ("\n");
19667 break;
19668 default:
19669 return display_tag_value (tag, p, end);
19670 }
19671 return p;
19672 }
19673
19674 static bool
19675 process_attributes (Filedata * filedata,
19676 const char * public_name,
19677 unsigned int proc_type,
19678 unsigned char * (* display_pub_attribute) (unsigned char *, const unsigned char * const),
19679 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const))
19680 {
19681 /* Find the section header so that we get the size. */
19682 Elf_Internal_Shdr * sect = find_section_by_type (filedata, proc_type);
19683 if (sect == NULL)
19684 sect = find_section_by_type (filedata, SHT_GNU_ATTRIBUTES);
19685
19686 if (sect == NULL)
19687 /* No section, exit without error. */
19688 return true;
19689
19690 unsigned char * contents = (unsigned char *)
19691 get_data (NULL, filedata, sect->sh_offset, 1, sect->sh_size, _("attributes"));
19692 if (contents == NULL)
19693 return false;
19694
19695 bool res = true;
19696 unsigned char * p = contents;
19697 /* The first character is the version of the attributes.
19698 Currently only version 1, (aka 'A') is recognised here. */
19699 if (*p != 'A')
19700 {
19701 printf (_("Unknown attributes version '%c'(%d) - expecting 'A'\n"), *p, *p);
19702 res = false;
19703 goto free_data;
19704 }
19705
19706 uint64_t section_len = sect->sh_size - 1;
19707 p++;
19708
19709 while (section_len > 0)
19710 {
19711 uint64_t attr_len;
19712 unsigned int namelen;
19713 bool public_section;
19714 bool gnu_section;
19715
19716 if (section_len <= 4)
19717 {
19718 error (_("Tag section ends prematurely\n"));
19719 res = false;
19720 break;
19721 }
19722 attr_len = byte_get (p, 4);
19723 p += 4;
19724
19725 if (attr_len > section_len)
19726 {
19727 error (_("Bad attribute length (%u > %u)\n"),
19728 (unsigned) attr_len, (unsigned) section_len);
19729 attr_len = section_len;
19730 res = false;
19731 }
19732 /* PR 17531: file: 001-101425-0.004 */
19733 else if (attr_len < 5)
19734 {
19735 error (_("Attribute length of %u is too small\n"), (unsigned) attr_len);
19736 res = false;
19737 break;
19738 }
19739
19740 section_len -= attr_len;
19741 attr_len -= 4;
19742
19743 namelen = strnlen ((char *) p, attr_len) + 1;
19744 if (namelen == 0 || namelen >= attr_len)
19745 {
19746 error (_("Corrupt attribute section name\n"));
19747 res = false;
19748 break;
19749 }
19750
19751 printf (_("Attribute Section: "));
19752 print_symbol_name (INT_MAX, (const char *) p);
19753 putchar ('\n');
19754
19755 if (public_name && streq ((char *) p, public_name))
19756 public_section = true;
19757 else
19758 public_section = false;
19759
19760 if (streq ((char *) p, "gnu"))
19761 gnu_section = true;
19762 else
19763 gnu_section = false;
19764
19765 p += namelen;
19766 attr_len -= namelen;
19767
19768 while (attr_len > 0 && p < contents + sect->sh_size)
19769 {
19770 int tag;
19771 unsigned int val;
19772 uint64_t size;
19773 unsigned char * end;
19774
19775 /* PR binutils/17531: Safe handling of corrupt files. */
19776 if (attr_len < 6)
19777 {
19778 error (_("Unused bytes at end of section\n"));
19779 res = false;
19780 section_len = 0;
19781 break;
19782 }
19783
19784 tag = *(p++);
19785 size = byte_get (p, 4);
19786 if (size > attr_len)
19787 {
19788 error (_("Bad subsection length (%u > %u)\n"),
19789 (unsigned) size, (unsigned) attr_len);
19790 res = false;
19791 size = attr_len;
19792 }
19793 /* PR binutils/17531: Safe handling of corrupt files. */
19794 if (size < 6)
19795 {
19796 error (_("Bad subsection length (%u < 6)\n"),
19797 (unsigned) size);
19798 res = false;
19799 section_len = 0;
19800 break;
19801 }
19802
19803 attr_len -= size;
19804 end = p + size - 1;
19805 assert (end <= contents + sect->sh_size);
19806 p += 4;
19807
19808 switch (tag)
19809 {
19810 case 1:
19811 printf (_("File Attributes\n"));
19812 break;
19813 case 2:
19814 printf (_("Section Attributes:"));
19815 goto do_numlist;
19816 case 3:
19817 printf (_("Symbol Attributes:"));
19818 /* Fall through. */
19819 do_numlist:
19820 for (;;)
19821 {
19822 READ_ULEB (val, p, end);
19823 if (val == 0)
19824 break;
19825 printf (" %d", val);
19826 }
19827 printf ("\n");
19828 break;
19829 default:
19830 printf (_("Unknown tag: %d\n"), tag);
19831 public_section = false;
19832 break;
19833 }
19834
19835 if (public_section && display_pub_attribute != NULL)
19836 {
19837 while (p < end)
19838 p = display_pub_attribute (p, end);
19839 assert (p == end);
19840 }
19841 else if (gnu_section && display_proc_gnu_attribute != NULL)
19842 {
19843 while (p < end)
19844 p = display_gnu_attribute (p,
19845 display_proc_gnu_attribute,
19846 end);
19847 assert (p == end);
19848 }
19849 else if (p < end)
19850 {
19851 printf (_(" Unknown attribute:\n"));
19852 display_raw_attribute (p, end);
19853 p = end;
19854 }
19855 else
19856 attr_len = 0;
19857 }
19858 }
19859
19860 free_data:
19861 free (contents);
19862
19863 return res;
19864 }
19865
19866 /* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
19867 Print the Address, Access and Initial fields of an entry at VMA ADDR
19868 and return the VMA of the next entry, or -1 if there was a problem.
19869 Does not read from DATA_END or beyond. */
19870
19871 static uint64_t
19872 print_mips_got_entry (unsigned char * data, uint64_t pltgot, uint64_t addr,
19873 unsigned char * data_end)
19874 {
19875 printf (" ");
19876 print_vma (addr, LONG_HEX);
19877 printf (" ");
19878 if (addr < pltgot + 0xfff0)
19879 printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
19880 else
19881 printf ("%10s", "");
19882 printf (" ");
19883 if (data == NULL)
19884 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
19885 else
19886 {
19887 uint64_t entry;
19888 unsigned char * from = data + addr - pltgot;
19889
19890 if (from + (is_32bit_elf ? 4 : 8) > data_end)
19891 {
19892 warn (_("MIPS GOT entry extends beyond the end of available data\n"));
19893 printf ("%*s", is_32bit_elf ? 8 : 16, _("<corrupt>"));
19894 return (uint64_t) -1;
19895 }
19896 else
19897 {
19898 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
19899 print_vma (entry, LONG_HEX);
19900 }
19901 }
19902 return addr + (is_32bit_elf ? 4 : 8);
19903 }
19904
19905 /* DATA points to the contents of a MIPS PLT GOT that starts at VMA
19906 PLTGOT. Print the Address and Initial fields of an entry at VMA
19907 ADDR and return the VMA of the next entry. */
19908
19909 static uint64_t
19910 print_mips_pltgot_entry (unsigned char * data, uint64_t pltgot, uint64_t addr)
19911 {
19912 printf (" ");
19913 print_vma (addr, LONG_HEX);
19914 printf (" ");
19915 if (data == NULL)
19916 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
19917 else
19918 {
19919 uint64_t entry;
19920
19921 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
19922 print_vma (entry, LONG_HEX);
19923 }
19924 return addr + (is_32bit_elf ? 4 : 8);
19925 }
19926
19927 static void
19928 print_mips_ases (unsigned int mask)
19929 {
19930 if (mask & AFL_ASE_DSP)
19931 fputs ("\n\tDSP ASE", stdout);
19932 if (mask & AFL_ASE_DSPR2)
19933 fputs ("\n\tDSP R2 ASE", stdout);
19934 if (mask & AFL_ASE_DSPR3)
19935 fputs ("\n\tDSP R3 ASE", stdout);
19936 if (mask & AFL_ASE_EVA)
19937 fputs ("\n\tEnhanced VA Scheme", stdout);
19938 if (mask & AFL_ASE_MCU)
19939 fputs ("\n\tMCU (MicroController) ASE", stdout);
19940 if (mask & AFL_ASE_MDMX)
19941 fputs ("\n\tMDMX ASE", stdout);
19942 if (mask & AFL_ASE_MIPS3D)
19943 fputs ("\n\tMIPS-3D ASE", stdout);
19944 if (mask & AFL_ASE_MT)
19945 fputs ("\n\tMT ASE", stdout);
19946 if (mask & AFL_ASE_SMARTMIPS)
19947 fputs ("\n\tSmartMIPS ASE", stdout);
19948 if (mask & AFL_ASE_VIRT)
19949 fputs ("\n\tVZ ASE", stdout);
19950 if (mask & AFL_ASE_MSA)
19951 fputs ("\n\tMSA ASE", stdout);
19952 if (mask & AFL_ASE_MIPS16)
19953 fputs ("\n\tMIPS16 ASE", stdout);
19954 if (mask & AFL_ASE_MICROMIPS)
19955 fputs ("\n\tMICROMIPS ASE", stdout);
19956 if (mask & AFL_ASE_XPA)
19957 fputs ("\n\tXPA ASE", stdout);
19958 if (mask & AFL_ASE_MIPS16E2)
19959 fputs ("\n\tMIPS16e2 ASE", stdout);
19960 if (mask & AFL_ASE_CRC)
19961 fputs ("\n\tCRC ASE", stdout);
19962 if (mask & AFL_ASE_GINV)
19963 fputs ("\n\tGINV ASE", stdout);
19964 if (mask & AFL_ASE_LOONGSON_MMI)
19965 fputs ("\n\tLoongson MMI ASE", stdout);
19966 if (mask & AFL_ASE_LOONGSON_CAM)
19967 fputs ("\n\tLoongson CAM ASE", stdout);
19968 if (mask & AFL_ASE_LOONGSON_EXT)
19969 fputs ("\n\tLoongson EXT ASE", stdout);
19970 if (mask & AFL_ASE_LOONGSON_EXT2)
19971 fputs ("\n\tLoongson EXT2 ASE", stdout);
19972 if (mask == 0)
19973 fprintf (stdout, "\n\t%s", _("None"));
19974 else if ((mask & ~AFL_ASE_MASK) != 0)
19975 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
19976 }
19977
19978 static void
19979 print_mips_isa_ext (unsigned int isa_ext)
19980 {
19981 switch (isa_ext)
19982 {
19983 case 0:
19984 fputs (_("None"), stdout);
19985 break;
19986 case AFL_EXT_XLR:
19987 fputs ("RMI XLR", stdout);
19988 break;
19989 case AFL_EXT_OCTEON3:
19990 fputs ("Cavium Networks Octeon3", stdout);
19991 break;
19992 case AFL_EXT_OCTEON2:
19993 fputs ("Cavium Networks Octeon2", stdout);
19994 break;
19995 case AFL_EXT_OCTEONP:
19996 fputs ("Cavium Networks OcteonP", stdout);
19997 break;
19998 case AFL_EXT_OCTEON:
19999 fputs ("Cavium Networks Octeon", stdout);
20000 break;
20001 case AFL_EXT_5900:
20002 fputs ("Toshiba R5900", stdout);
20003 break;
20004 case AFL_EXT_4650:
20005 fputs ("MIPS R4650", stdout);
20006 break;
20007 case AFL_EXT_4010:
20008 fputs ("LSI R4010", stdout);
20009 break;
20010 case AFL_EXT_4100:
20011 fputs ("NEC VR4100", stdout);
20012 break;
20013 case AFL_EXT_3900:
20014 fputs ("Toshiba R3900", stdout);
20015 break;
20016 case AFL_EXT_10000:
20017 fputs ("MIPS R10000", stdout);
20018 break;
20019 case AFL_EXT_SB1:
20020 fputs ("Broadcom SB-1", stdout);
20021 break;
20022 case AFL_EXT_4111:
20023 fputs ("NEC VR4111/VR4181", stdout);
20024 break;
20025 case AFL_EXT_4120:
20026 fputs ("NEC VR4120", stdout);
20027 break;
20028 case AFL_EXT_5400:
20029 fputs ("NEC VR5400", stdout);
20030 break;
20031 case AFL_EXT_5500:
20032 fputs ("NEC VR5500", stdout);
20033 break;
20034 case AFL_EXT_LOONGSON_2E:
20035 fputs ("ST Microelectronics Loongson 2E", stdout);
20036 break;
20037 case AFL_EXT_LOONGSON_2F:
20038 fputs ("ST Microelectronics Loongson 2F", stdout);
20039 break;
20040 case AFL_EXT_INTERAPTIV_MR2:
20041 fputs ("Imagination interAptiv MR2", stdout);
20042 break;
20043 default:
20044 fprintf (stdout, "%s (%d)", _("Unknown"), isa_ext);
20045 }
20046 }
20047
20048 static signed int
20049 get_mips_reg_size (int reg_size)
20050 {
20051 return (reg_size == AFL_REG_NONE) ? 0
20052 : (reg_size == AFL_REG_32) ? 32
20053 : (reg_size == AFL_REG_64) ? 64
20054 : (reg_size == AFL_REG_128) ? 128
20055 : -1;
20056 }
20057
20058 /* If DUMP_GOT is true, display only the GOT related contents.
20059 Otherwise, display all MIPS specific information. */
20060
20061 static bool
20062 process_mips_specific (Filedata * filedata, bool dump_got)
20063 {
20064 Elf_Internal_Dyn * entry;
20065 Elf_Internal_Shdr *sect = NULL;
20066 size_t liblist_offset = 0;
20067 size_t liblistno = 0;
20068 size_t conflictsno = 0;
20069 size_t options_offset = 0;
20070 size_t conflicts_offset = 0;
20071 size_t pltrelsz = 0;
20072 size_t pltrel = 0;
20073 uint64_t pltgot = 0;
20074 uint64_t mips_pltgot = 0;
20075 uint64_t jmprel = 0;
20076 uint64_t local_gotno = 0;
20077 uint64_t gotsym = 0;
20078 uint64_t symtabno = 0;
20079 bool res = true;
20080
20081 if (!dump_got)
20082 {
20083 if (! process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
20084 display_mips_gnu_attribute))
20085 res = false;
20086
20087 sect = find_section (filedata, ".MIPS.abiflags");
20088
20089 if (sect != NULL)
20090 {
20091 Elf_External_ABIFlags_v0 *abiflags_ext;
20092 Elf_Internal_ABIFlags_v0 abiflags_in;
20093
20094 if (sizeof (Elf_External_ABIFlags_v0) != sect->sh_size)
20095 {
20096 error (_("Corrupt MIPS ABI Flags section.\n"));
20097 res = false;
20098 }
20099 else
20100 {
20101 abiflags_ext = get_data (NULL, filedata, sect->sh_offset, 1,
20102 sect->sh_size, _("MIPS ABI Flags section"));
20103 if (abiflags_ext)
20104 {
20105 abiflags_in.version = BYTE_GET (abiflags_ext->version);
20106 abiflags_in.isa_level = BYTE_GET (abiflags_ext->isa_level);
20107 abiflags_in.isa_rev = BYTE_GET (abiflags_ext->isa_rev);
20108 abiflags_in.gpr_size = BYTE_GET (abiflags_ext->gpr_size);
20109 abiflags_in.cpr1_size = BYTE_GET (abiflags_ext->cpr1_size);
20110 abiflags_in.cpr2_size = BYTE_GET (abiflags_ext->cpr2_size);
20111 abiflags_in.fp_abi = BYTE_GET (abiflags_ext->fp_abi);
20112 abiflags_in.isa_ext = BYTE_GET (abiflags_ext->isa_ext);
20113 abiflags_in.ases = BYTE_GET (abiflags_ext->ases);
20114 abiflags_in.flags1 = BYTE_GET (abiflags_ext->flags1);
20115 abiflags_in.flags2 = BYTE_GET (abiflags_ext->flags2);
20116
20117 printf ("\nMIPS ABI Flags Version: %d\n", abiflags_in.version);
20118 printf ("\nISA: MIPS%d", abiflags_in.isa_level);
20119 if (abiflags_in.isa_rev > 1)
20120 printf ("r%d", abiflags_in.isa_rev);
20121 printf ("\nGPR size: %d",
20122 get_mips_reg_size (abiflags_in.gpr_size));
20123 printf ("\nCPR1 size: %d",
20124 get_mips_reg_size (abiflags_in.cpr1_size));
20125 printf ("\nCPR2 size: %d",
20126 get_mips_reg_size (abiflags_in.cpr2_size));
20127 fputs ("\nFP ABI: ", stdout);
20128 print_mips_fp_abi_value (abiflags_in.fp_abi);
20129 fputs ("ISA Extension: ", stdout);
20130 print_mips_isa_ext (abiflags_in.isa_ext);
20131 fputs ("\nASEs:", stdout);
20132 print_mips_ases (abiflags_in.ases);
20133 printf ("\nFLAGS 1: %8.8lx", abiflags_in.flags1);
20134 printf ("\nFLAGS 2: %8.8lx", abiflags_in.flags2);
20135 fputc ('\n', stdout);
20136 free (abiflags_ext);
20137 }
20138 }
20139 }
20140 }
20141
20142 /* We have a lot of special sections. Thanks SGI! */
20143 if (filedata->dynamic_section == NULL)
20144 {
20145 /* No dynamic information available. See if there is static GOT. */
20146 sect = find_section (filedata, ".got");
20147 if (sect != NULL)
20148 {
20149 unsigned char *data_end;
20150 unsigned char *data;
20151 uint64_t ent, end;
20152 int addr_size;
20153
20154 pltgot = sect->sh_addr;
20155
20156 ent = pltgot;
20157 addr_size = (is_32bit_elf ? 4 : 8);
20158 end = pltgot + sect->sh_size;
20159
20160 data = (unsigned char *) get_data (NULL, filedata, sect->sh_offset,
20161 end - pltgot, 1,
20162 _("Global Offset Table data"));
20163 /* PR 12855: Null data is handled gracefully throughout. */
20164 data_end = data + (end - pltgot);
20165
20166 printf (_("\nStatic GOT:\n"));
20167 printf (_(" Canonical gp value: "));
20168 print_vma (ent + 0x7ff0, LONG_HEX);
20169 printf ("\n\n");
20170
20171 /* In a dynamic binary GOT[0] is reserved for the dynamic
20172 loader to store the lazy resolver pointer, however in
20173 a static binary it may well have been omitted and GOT
20174 reduced to a table of addresses.
20175 PR 21344: Check for the entry being fully available
20176 before fetching it. */
20177 if (data
20178 && data + ent - pltgot + addr_size <= data_end
20179 && byte_get (data + ent - pltgot, addr_size) == 0)
20180 {
20181 printf (_(" Reserved entries:\n"));
20182 printf (_(" %*s %10s %*s\n"),
20183 addr_size * 2, _("Address"), _("Access"),
20184 addr_size * 2, _("Value"));
20185 ent = print_mips_got_entry (data, pltgot, ent, data_end);
20186 printf ("\n");
20187 if (ent == (uint64_t) -1)
20188 goto sgot_print_fail;
20189
20190 /* Check for the MSB of GOT[1] being set, identifying a
20191 GNU object. This entry will be used by some runtime
20192 loaders, to store the module pointer. Otherwise this
20193 is an ordinary local entry.
20194 PR 21344: Check for the entry being fully available
20195 before fetching it. */
20196 if (data
20197 && data + ent - pltgot + addr_size <= data_end
20198 && (byte_get (data + ent - pltgot, addr_size)
20199 >> (addr_size * 8 - 1)) != 0)
20200 {
20201 ent = print_mips_got_entry (data, pltgot, ent, data_end);
20202 printf ("\n");
20203 if (ent == (uint64_t) -1)
20204 goto sgot_print_fail;
20205 }
20206 printf ("\n");
20207 }
20208
20209 if (data != NULL && ent < end)
20210 {
20211 printf (_(" Local entries:\n"));
20212 printf (" %*s %10s %*s\n",
20213 addr_size * 2, _("Address"), _("Access"),
20214 addr_size * 2, _("Value"));
20215 while (ent < end)
20216 {
20217 ent = print_mips_got_entry (data, pltgot, ent, data_end);
20218 printf ("\n");
20219 if (ent == (uint64_t) -1)
20220 goto sgot_print_fail;
20221 }
20222 printf ("\n");
20223 }
20224
20225 sgot_print_fail:
20226 free (data);
20227 }
20228 return res;
20229 }
20230
20231 for (entry = filedata->dynamic_section;
20232 /* PR 17531 file: 012-50589-0.004. */
20233 (entry < filedata->dynamic_section + filedata->dynamic_nent
20234 && entry->d_tag != DT_NULL);
20235 ++entry)
20236 switch (entry->d_tag)
20237 {
20238 case DT_MIPS_LIBLIST:
20239 liblist_offset
20240 = offset_from_vma (filedata, entry->d_un.d_val,
20241 liblistno * sizeof (Elf32_External_Lib));
20242 break;
20243 case DT_MIPS_LIBLISTNO:
20244 liblistno = entry->d_un.d_val;
20245 break;
20246 case DT_MIPS_OPTIONS:
20247 options_offset = offset_from_vma (filedata, entry->d_un.d_val, 0);
20248 break;
20249 case DT_MIPS_CONFLICT:
20250 conflicts_offset
20251 = offset_from_vma (filedata, entry->d_un.d_val,
20252 conflictsno * sizeof (Elf32_External_Conflict));
20253 break;
20254 case DT_MIPS_CONFLICTNO:
20255 conflictsno = entry->d_un.d_val;
20256 break;
20257 case DT_PLTGOT:
20258 pltgot = entry->d_un.d_ptr;
20259 break;
20260 case DT_MIPS_LOCAL_GOTNO:
20261 local_gotno = entry->d_un.d_val;
20262 break;
20263 case DT_MIPS_GOTSYM:
20264 gotsym = entry->d_un.d_val;
20265 break;
20266 case DT_MIPS_SYMTABNO:
20267 symtabno = entry->d_un.d_val;
20268 break;
20269 case DT_MIPS_PLTGOT:
20270 mips_pltgot = entry->d_un.d_ptr;
20271 break;
20272 case DT_PLTREL:
20273 pltrel = entry->d_un.d_val;
20274 break;
20275 case DT_PLTRELSZ:
20276 pltrelsz = entry->d_un.d_val;
20277 break;
20278 case DT_JMPREL:
20279 jmprel = entry->d_un.d_ptr;
20280 break;
20281 default:
20282 break;
20283 }
20284
20285 if (!dump_got && liblist_offset != 0 && liblistno != 0 && do_dynamic)
20286 {
20287 Elf32_External_Lib * elib;
20288 size_t cnt;
20289
20290 elib = (Elf32_External_Lib *) get_data (NULL, filedata, liblist_offset,
20291 sizeof (Elf32_External_Lib),
20292 liblistno,
20293 _("liblist section data"));
20294 if (elib)
20295 {
20296 printf (ngettext ("\nSection '.liblist' contains %zu entry:\n",
20297 "\nSection '.liblist' contains %zu entries:\n",
20298 liblistno),
20299 liblistno);
20300 fputs (_(" Library Time Stamp Checksum Version Flags\n"),
20301 stdout);
20302
20303 for (cnt = 0; cnt < liblistno; ++cnt)
20304 {
20305 Elf32_Lib liblist;
20306 time_t atime;
20307 char timebuf[128];
20308 struct tm * tmp;
20309
20310 liblist.l_name = BYTE_GET (elib[cnt].l_name);
20311 atime = BYTE_GET (elib[cnt].l_time_stamp);
20312 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
20313 liblist.l_version = BYTE_GET (elib[cnt].l_version);
20314 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
20315
20316 tmp = gmtime (&atime);
20317 snprintf (timebuf, sizeof (timebuf),
20318 "%04u-%02u-%02uT%02u:%02u:%02u",
20319 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
20320 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
20321
20322 printf ("%3zu: ", cnt);
20323 if (valid_dynamic_name (filedata, liblist.l_name))
20324 print_symbol_name (20, get_dynamic_name (filedata, liblist.l_name));
20325 else
20326 printf (_("<corrupt: %9ld>"), liblist.l_name);
20327 printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
20328 liblist.l_version);
20329
20330 if (liblist.l_flags == 0)
20331 puts (_(" NONE"));
20332 else
20333 {
20334 static const struct
20335 {
20336 const char * name;
20337 int bit;
20338 }
20339 l_flags_vals[] =
20340 {
20341 { " EXACT_MATCH", LL_EXACT_MATCH },
20342 { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
20343 { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
20344 { " EXPORTS", LL_EXPORTS },
20345 { " DELAY_LOAD", LL_DELAY_LOAD },
20346 { " DELTA", LL_DELTA }
20347 };
20348 int flags = liblist.l_flags;
20349 size_t fcnt;
20350
20351 for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
20352 if ((flags & l_flags_vals[fcnt].bit) != 0)
20353 {
20354 fputs (l_flags_vals[fcnt].name, stdout);
20355 flags ^= l_flags_vals[fcnt].bit;
20356 }
20357 if (flags != 0)
20358 printf (" %#x", (unsigned int) flags);
20359
20360 puts ("");
20361 }
20362 }
20363
20364 free (elib);
20365 }
20366 else
20367 res = false;
20368 }
20369
20370 if (!dump_got && options_offset != 0)
20371 {
20372 Elf_External_Options * eopt;
20373 size_t offset;
20374 int cnt;
20375
20376 /* Find the section header so that we get the size. */
20377 sect = find_section_by_type (filedata, SHT_MIPS_OPTIONS);
20378 /* PR 17533 file: 012-277276-0.004. */
20379 if (sect == NULL)
20380 {
20381 error (_("No MIPS_OPTIONS header found\n"));
20382 return false;
20383 }
20384 /* PR 24243 */
20385 if (sect->sh_size < sizeof (* eopt))
20386 {
20387 error (_("The MIPS options section is too small.\n"));
20388 return false;
20389 }
20390
20391 eopt = (Elf_External_Options *) get_data (NULL, filedata, options_offset, 1,
20392 sect->sh_size, _("options"));
20393 if (eopt)
20394 {
20395 Elf_Internal_Options option;
20396
20397 offset = cnt = 0;
20398 while (offset <= sect->sh_size - sizeof (* eopt))
20399 {
20400 Elf_External_Options * eoption;
20401 unsigned int optsize;
20402
20403 eoption = (Elf_External_Options *) ((char *) eopt + offset);
20404
20405 optsize = BYTE_GET (eoption->size);
20406
20407 /* PR 17531: file: ffa0fa3b. */
20408 if (optsize < sizeof (* eopt)
20409 || optsize > sect->sh_size - offset)
20410 {
20411 error (_("Invalid size (%u) for MIPS option\n"),
20412 optsize);
20413 free (eopt);
20414 return false;
20415 }
20416 offset += optsize;
20417 ++cnt;
20418 }
20419
20420 printf (ngettext ("\nSection '%s' contains %d entry:\n",
20421 "\nSection '%s' contains %d entries:\n",
20422 cnt),
20423 printable_section_name (filedata, sect), cnt);
20424
20425 offset = 0;
20426 while (cnt-- > 0)
20427 {
20428 size_t len;
20429 Elf_External_Options * eoption;
20430
20431 eoption = (Elf_External_Options *) ((char *) eopt + offset);
20432
20433 option.kind = BYTE_GET (eoption->kind);
20434 option.size = BYTE_GET (eoption->size);
20435 option.section = BYTE_GET (eoption->section);
20436 option.info = BYTE_GET (eoption->info);
20437
20438 switch (option.kind)
20439 {
20440 case ODK_NULL:
20441 /* This shouldn't happen. */
20442 printf (" NULL %" PRId16 " %" PRIx32,
20443 option.section, option.info);
20444 break;
20445
20446 case ODK_REGINFO:
20447 printf (" REGINFO ");
20448 if (filedata->file_header.e_machine == EM_MIPS)
20449 {
20450 Elf32_External_RegInfo * ereg;
20451 Elf32_RegInfo reginfo;
20452
20453 /* 32bit form. */
20454 if (option.size < (sizeof (Elf_External_Options)
20455 + sizeof (Elf32_External_RegInfo)))
20456 {
20457 printf (_("<corrupt>\n"));
20458 error (_("Truncated MIPS REGINFO option\n"));
20459 cnt = 0;
20460 break;
20461 }
20462
20463 ereg = (Elf32_External_RegInfo *) (eoption + 1);
20464
20465 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
20466 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
20467 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
20468 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
20469 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
20470 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
20471
20472 printf ("GPR %08" PRIx32 " GP 0x%" PRIx32 "\n",
20473 reginfo.ri_gprmask, reginfo.ri_gp_value);
20474 printf (" "
20475 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
20476 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
20477 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
20478 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
20479 }
20480 else
20481 {
20482 /* 64 bit form. */
20483 Elf64_External_RegInfo * ereg;
20484 Elf64_Internal_RegInfo reginfo;
20485
20486 if (option.size < (sizeof (Elf_External_Options)
20487 + sizeof (Elf64_External_RegInfo)))
20488 {
20489 printf (_("<corrupt>\n"));
20490 error (_("Truncated MIPS REGINFO option\n"));
20491 cnt = 0;
20492 break;
20493 }
20494
20495 ereg = (Elf64_External_RegInfo *) (eoption + 1);
20496 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
20497 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
20498 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
20499 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
20500 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
20501 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
20502
20503 printf ("GPR %08" PRIx32 " GP 0x%" PRIx64 "\n",
20504 reginfo.ri_gprmask, reginfo.ri_gp_value);
20505 printf (" "
20506 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
20507 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
20508 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
20509 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
20510 }
20511 offset += option.size;
20512 continue;
20513
20514 case ODK_EXCEPTIONS:
20515 fputs (" EXCEPTIONS fpe_min(", stdout);
20516 process_mips_fpe_exception (option.info & OEX_FPU_MIN);
20517 fputs (") fpe_max(", stdout);
20518 process_mips_fpe_exception ((option.info & OEX_FPU_MAX) >> 8);
20519 fputs (")", stdout);
20520
20521 if (option.info & OEX_PAGE0)
20522 fputs (" PAGE0", stdout);
20523 if (option.info & OEX_SMM)
20524 fputs (" SMM", stdout);
20525 if (option.info & OEX_FPDBUG)
20526 fputs (" FPDBUG", stdout);
20527 if (option.info & OEX_DISMISS)
20528 fputs (" DISMISS", stdout);
20529 break;
20530
20531 case ODK_PAD:
20532 fputs (" PAD ", stdout);
20533 if (option.info & OPAD_PREFIX)
20534 fputs (" PREFIX", stdout);
20535 if (option.info & OPAD_POSTFIX)
20536 fputs (" POSTFIX", stdout);
20537 if (option.info & OPAD_SYMBOL)
20538 fputs (" SYMBOL", stdout);
20539 break;
20540
20541 case ODK_HWPATCH:
20542 fputs (" HWPATCH ", stdout);
20543 if (option.info & OHW_R4KEOP)
20544 fputs (" R4KEOP", stdout);
20545 if (option.info & OHW_R8KPFETCH)
20546 fputs (" R8KPFETCH", stdout);
20547 if (option.info & OHW_R5KEOP)
20548 fputs (" R5KEOP", stdout);
20549 if (option.info & OHW_R5KCVTL)
20550 fputs (" R5KCVTL", stdout);
20551 break;
20552
20553 case ODK_FILL:
20554 fputs (" FILL ", stdout);
20555 /* XXX Print content of info word? */
20556 break;
20557
20558 case ODK_TAGS:
20559 fputs (" TAGS ", stdout);
20560 /* XXX Print content of info word? */
20561 break;
20562
20563 case ODK_HWAND:
20564 fputs (" HWAND ", stdout);
20565 if (option.info & OHWA0_R4KEOP_CHECKED)
20566 fputs (" R4KEOP_CHECKED", stdout);
20567 if (option.info & OHWA0_R4KEOP_CLEAN)
20568 fputs (" R4KEOP_CLEAN", stdout);
20569 break;
20570
20571 case ODK_HWOR:
20572 fputs (" HWOR ", stdout);
20573 if (option.info & OHWA0_R4KEOP_CHECKED)
20574 fputs (" R4KEOP_CHECKED", stdout);
20575 if (option.info & OHWA0_R4KEOP_CLEAN)
20576 fputs (" R4KEOP_CLEAN", stdout);
20577 break;
20578
20579 case ODK_GP_GROUP:
20580 printf (" GP_GROUP %#06x self-contained %#06x",
20581 option.info & OGP_GROUP,
20582 (option.info & OGP_SELF) >> 16);
20583 break;
20584
20585 case ODK_IDENT:
20586 printf (" IDENT %#06x self-contained %#06x",
20587 option.info & OGP_GROUP,
20588 (option.info & OGP_SELF) >> 16);
20589 break;
20590
20591 default:
20592 /* This shouldn't happen. */
20593 printf (" %3d ??? %" PRId16 " %" PRIx32,
20594 option.kind, option.section, option.info);
20595 break;
20596 }
20597
20598 len = sizeof (* eopt);
20599 while (len < option.size)
20600 {
20601 unsigned char datum = *((unsigned char *) eoption + len);
20602
20603 if (ISPRINT (datum))
20604 printf ("%c", datum);
20605 else
20606 printf ("\\%03o", datum);
20607 len ++;
20608 }
20609 fputs ("\n", stdout);
20610
20611 offset += option.size;
20612 }
20613 free (eopt);
20614 }
20615 else
20616 res = false;
20617 }
20618
20619 if (!dump_got && conflicts_offset != 0 && conflictsno != 0)
20620 {
20621 Elf32_Conflict * iconf;
20622 size_t cnt;
20623
20624 if (filedata->dynamic_symbols == NULL)
20625 {
20626 error (_("conflict list found without a dynamic symbol table\n"));
20627 return false;
20628 }
20629
20630 /* PR 21345 - print a slightly more helpful error message
20631 if we are sure that the cmalloc will fail. */
20632 if (conflictsno > filedata->file_size / sizeof (* iconf))
20633 {
20634 error (_("Overlarge number of conflicts detected: %zx\n"),
20635 conflictsno);
20636 return false;
20637 }
20638
20639 iconf = (Elf32_Conflict *) cmalloc (conflictsno, sizeof (* iconf));
20640 if (iconf == NULL)
20641 {
20642 error (_("Out of memory allocating space for dynamic conflicts\n"));
20643 return false;
20644 }
20645
20646 if (is_32bit_elf)
20647 {
20648 Elf32_External_Conflict * econf32;
20649
20650 econf32 = (Elf32_External_Conflict *)
20651 get_data (NULL, filedata, conflicts_offset,
20652 sizeof (*econf32), conflictsno, _("conflict"));
20653 if (!econf32)
20654 {
20655 free (iconf);
20656 return false;
20657 }
20658
20659 for (cnt = 0; cnt < conflictsno; ++cnt)
20660 iconf[cnt] = BYTE_GET (econf32[cnt]);
20661
20662 free (econf32);
20663 }
20664 else
20665 {
20666 Elf64_External_Conflict * econf64;
20667
20668 econf64 = (Elf64_External_Conflict *)
20669 get_data (NULL, filedata, conflicts_offset,
20670 sizeof (*econf64), conflictsno, _("conflict"));
20671 if (!econf64)
20672 {
20673 free (iconf);
20674 return false;
20675 }
20676
20677 for (cnt = 0; cnt < conflictsno; ++cnt)
20678 iconf[cnt] = BYTE_GET (econf64[cnt]);
20679
20680 free (econf64);
20681 }
20682
20683 printf (ngettext ("\nSection '.conflict' contains %zu entry:\n",
20684 "\nSection '.conflict' contains %zu entries:\n",
20685 conflictsno),
20686 conflictsno);
20687 puts (_(" Num: Index Value Name"));
20688
20689 for (cnt = 0; cnt < conflictsno; ++cnt)
20690 {
20691 printf ("%5zu: %8lu ", cnt, iconf[cnt]);
20692
20693 if (iconf[cnt] >= filedata->num_dynamic_syms)
20694 printf (_("<corrupt symbol index>"));
20695 else
20696 {
20697 Elf_Internal_Sym * psym;
20698
20699 psym = & filedata->dynamic_symbols[iconf[cnt]];
20700 print_vma (psym->st_value, FULL_HEX);
20701 putchar (' ');
20702 if (valid_dynamic_name (filedata, psym->st_name))
20703 print_symbol_name (25, get_dynamic_name (filedata, psym->st_name));
20704 else
20705 printf (_("<corrupt: %14ld>"), psym->st_name);
20706 }
20707 putchar ('\n');
20708 }
20709
20710 free (iconf);
20711 }
20712
20713 if (pltgot != 0 && local_gotno != 0)
20714 {
20715 uint64_t ent, local_end, global_end;
20716 size_t i, offset;
20717 unsigned char * data;
20718 unsigned char * data_end;
20719 int addr_size;
20720
20721 ent = pltgot;
20722 addr_size = (is_32bit_elf ? 4 : 8);
20723 local_end = pltgot + local_gotno * addr_size;
20724
20725 /* PR binutils/17533 file: 012-111227-0.004 */
20726 if (symtabno < gotsym)
20727 {
20728 error (_("The GOT symbol offset (%" PRIu64
20729 ") is greater than the symbol table size (%" PRIu64 ")\n"),
20730 gotsym, symtabno);
20731 return false;
20732 }
20733
20734 global_end = local_end + (symtabno - gotsym) * addr_size;
20735 /* PR 17531: file: 54c91a34. */
20736 if (global_end < local_end)
20737 {
20738 error (_("Too many GOT symbols: %" PRIu64 "\n"), symtabno);
20739 return false;
20740 }
20741
20742 offset = offset_from_vma (filedata, pltgot, global_end - pltgot);
20743 data = (unsigned char *) get_data (NULL, filedata, offset,
20744 global_end - pltgot, 1,
20745 _("Global Offset Table data"));
20746 /* PR 12855: Null data is handled gracefully throughout. */
20747 data_end = data + (global_end - pltgot);
20748
20749 printf (_("\nPrimary GOT:\n"));
20750 printf (_(" Canonical gp value: "));
20751 print_vma (pltgot + 0x7ff0, LONG_HEX);
20752 printf ("\n\n");
20753
20754 printf (_(" Reserved entries:\n"));
20755 printf (_(" %*s %10s %*s Purpose\n"),
20756 addr_size * 2, _("Address"), _("Access"),
20757 addr_size * 2, _("Initial"));
20758 ent = print_mips_got_entry (data, pltgot, ent, data_end);
20759 printf (_(" Lazy resolver\n"));
20760 if (ent == (uint64_t) -1)
20761 goto got_print_fail;
20762
20763 /* Check for the MSB of GOT[1] being set, denoting a GNU object.
20764 This entry will be used by some runtime loaders, to store the
20765 module pointer. Otherwise this is an ordinary local entry.
20766 PR 21344: Check for the entry being fully available before
20767 fetching it. */
20768 if (data
20769 && data + ent - pltgot + addr_size <= data_end
20770 && (byte_get (data + ent - pltgot, addr_size)
20771 >> (addr_size * 8 - 1)) != 0)
20772 {
20773 ent = print_mips_got_entry (data, pltgot, ent, data_end);
20774 printf (_(" Module pointer (GNU extension)\n"));
20775 if (ent == (uint64_t) -1)
20776 goto got_print_fail;
20777 }
20778 printf ("\n");
20779
20780 if (data != NULL && ent < local_end)
20781 {
20782 printf (_(" Local entries:\n"));
20783 printf (" %*s %10s %*s\n",
20784 addr_size * 2, _("Address"), _("Access"),
20785 addr_size * 2, _("Initial"));
20786 while (ent < local_end)
20787 {
20788 ent = print_mips_got_entry (data, pltgot, ent, data_end);
20789 printf ("\n");
20790 if (ent == (uint64_t) -1)
20791 goto got_print_fail;
20792 }
20793 printf ("\n");
20794 }
20795
20796 if (data != NULL && gotsym < symtabno)
20797 {
20798 int sym_width;
20799
20800 printf (_(" Global entries:\n"));
20801 printf (" %*s %10s %*s %*s %-7s %3s %s\n",
20802 addr_size * 2, _("Address"),
20803 _("Access"),
20804 addr_size * 2, _("Initial"),
20805 addr_size * 2, _("Sym.Val."),
20806 _("Type"),
20807 /* Note for translators: "Ndx" = abbreviated form of "Index". */
20808 _("Ndx"), _("Name"));
20809
20810 sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
20811
20812 for (i = gotsym; i < symtabno; i++)
20813 {
20814 ent = print_mips_got_entry (data, pltgot, ent, data_end);
20815 printf (" ");
20816
20817 if (filedata->dynamic_symbols == NULL)
20818 printf (_("<no dynamic symbols>"));
20819 else if (i < filedata->num_dynamic_syms)
20820 {
20821 Elf_Internal_Sym * psym = filedata->dynamic_symbols + i;
20822
20823 print_vma (psym->st_value, LONG_HEX);
20824 printf (" %-7s ", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
20825
20826 bool is_special;
20827 const char * s = printable_section_name_from_index (filedata, psym->st_shndx, & is_special);
20828 if (is_special)
20829 printf ("%3s ", s);
20830 else
20831 printf ("%3u ", psym->st_shndx);
20832
20833 if (valid_dynamic_name (filedata, psym->st_name))
20834 print_symbol_name (sym_width,
20835 get_dynamic_name (filedata, psym->st_name));
20836 else
20837 printf (_("<corrupt: %14ld>"), psym->st_name);
20838 }
20839 else
20840 printf (_("<symbol index %zu exceeds number of dynamic symbols>"),
20841 i);
20842
20843 printf ("\n");
20844 if (ent == (uint64_t) -1)
20845 break;
20846 }
20847 printf ("\n");
20848 }
20849
20850 got_print_fail:
20851 free (data);
20852 }
20853
20854 if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
20855 {
20856 uint64_t ent, end;
20857 uint64_t offset, rel_offset;
20858 uint64_t count, i;
20859 unsigned char * data;
20860 int addr_size, sym_width;
20861 Elf_Internal_Rela * rels;
20862
20863 rel_offset = offset_from_vma (filedata, jmprel, pltrelsz);
20864 if (pltrel == DT_RELA)
20865 {
20866 if (!slurp_rela_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
20867 return false;
20868 }
20869 else
20870 {
20871 if (!slurp_rel_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
20872 return false;
20873 }
20874
20875 ent = mips_pltgot;
20876 addr_size = (is_32bit_elf ? 4 : 8);
20877 end = mips_pltgot + (2 + count) * addr_size;
20878
20879 offset = offset_from_vma (filedata, mips_pltgot, end - mips_pltgot);
20880 data = (unsigned char *) get_data (NULL, filedata, offset, end - mips_pltgot,
20881 1, _("Procedure Linkage Table data"));
20882 if (data == NULL)
20883 {
20884 free (rels);
20885 return false;
20886 }
20887
20888 printf ("\nPLT GOT:\n\n");
20889 printf (_(" Reserved entries:\n"));
20890 printf (_(" %*s %*s Purpose\n"),
20891 addr_size * 2, _("Address"), addr_size * 2, _("Initial"));
20892 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
20893 printf (_(" PLT lazy resolver\n"));
20894 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
20895 printf (_(" Module pointer\n"));
20896 printf ("\n");
20897
20898 printf (_(" Entries:\n"));
20899 printf (" %*s %*s %*s %-7s %3s %s\n",
20900 addr_size * 2, _("Address"),
20901 addr_size * 2, _("Initial"),
20902 addr_size * 2, _("Sym.Val."), _("Type"), _("Ndx"), _("Name"));
20903 sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
20904 for (i = 0; i < count; i++)
20905 {
20906 uint64_t idx = get_reloc_symindex (rels[i].r_info);
20907
20908 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
20909 printf (" ");
20910
20911 if (idx >= filedata->num_dynamic_syms)
20912 printf (_("<corrupt symbol index: %" PRIu64 ">"), idx);
20913 else
20914 {
20915 Elf_Internal_Sym * psym = filedata->dynamic_symbols + idx;
20916
20917 print_vma (psym->st_value, LONG_HEX);
20918 printf (" %-7s %3s ",
20919 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
20920 printable_section_name_from_index (filedata, psym->st_shndx, NULL));
20921 if (valid_dynamic_name (filedata, psym->st_name))
20922 print_symbol_name (sym_width,
20923 get_dynamic_name (filedata, psym->st_name));
20924 else
20925 printf (_("<corrupt: %14ld>"), psym->st_name);
20926 }
20927 printf ("\n");
20928 }
20929 printf ("\n");
20930
20931 free (data);
20932 free (rels);
20933 }
20934
20935 return res;
20936 }
20937
20938 static bool
20939 process_nds32_specific (Filedata * filedata)
20940 {
20941 Elf_Internal_Shdr *sect = NULL;
20942
20943 sect = find_section (filedata, ".nds32_e_flags");
20944 if (sect != NULL && sect->sh_size >= 4)
20945 {
20946 unsigned char *buf;
20947 unsigned int flag;
20948
20949 printf ("\nNDS32 elf flags section:\n");
20950 buf = get_data (NULL, filedata, sect->sh_offset, 1, 4,
20951 _("NDS32 elf flags section"));
20952
20953 if (buf == NULL)
20954 return false;
20955
20956 flag = byte_get (buf, 4);
20957 free (buf);
20958 switch (flag & 0x3)
20959 {
20960 case 0:
20961 printf ("(VEC_SIZE):\tNo entry.\n");
20962 break;
20963 case 1:
20964 printf ("(VEC_SIZE):\t4 bytes\n");
20965 break;
20966 case 2:
20967 printf ("(VEC_SIZE):\t16 bytes\n");
20968 break;
20969 case 3:
20970 printf ("(VEC_SIZE):\treserved\n");
20971 break;
20972 }
20973 }
20974
20975 return true;
20976 }
20977
20978 static int
20979 elf_relocation_cmp (const void *p, const void *q)
20980 {
20981 const elf_relocation *rp = (const elf_relocation *) p;
20982 const elf_relocation *rq = (const elf_relocation *) q;
20983
20984 return (rp->r_offset > rq->r_offset
20985 ? 1
20986 : (rp->r_offset < rq->r_offset ? -1 : 0));
20987 }
20988
20989 static void
20990 display_elf_relocation_at (uint64_t offset, uint64_t g)
20991 {
20992 bool matched = false;
20993
20994 for (size_t i = 0; i < all_relocations_count; i++)
20995 if (all_relocations_root[i].r_offset == offset)
20996 {
20997 if (do_wide)
20998 printf (" %-22s", all_relocations_root[i].r_name);
20999 else
21000 printf (" %-17.17s", all_relocations_root[i].r_name);
21001
21002 uint64_t off;
21003 switch (all_relocations_root[i].r_type)
21004 {
21005 default:
21006 abort ();
21007 case reltype_rel:
21008 case reltype_relr:
21009 off = g;
21010 break;
21011 case reltype_rela:
21012 off = all_relocations_root[i].r_addend;
21013 break;
21014 }
21015
21016 if (all_relocations_root[i].r_symbol)
21017 {
21018 printf (" %s", all_relocations_root[i].r_symbol);
21019 if ((int64_t) off < 0)
21020 printf (" - %" PRIx64, -off);
21021 else
21022 printf (" + %" PRIx64, off);
21023 }
21024 else
21025 {
21026 if ((int64_t) off < 0)
21027 printf (" -%" PRIx64, -off);
21028 else
21029 printf (" %" PRIx64, off);
21030 }
21031
21032 matched = true;
21033 break;
21034 }
21035 else if (all_relocations_root[i].r_offset > offset)
21036 break;
21037
21038 if (!matched)
21039 {
21040 if (do_wide)
21041 printf ("%*c", 24, ' ');
21042 else
21043 printf ("%*c", 19, ' ');
21044 printf ("%" PRIx64, g);
21045 }
21046 }
21047
21048 static bool
21049 process_got_section_contents (Filedata * filedata)
21050 {
21051 Elf_Internal_Shdr * section;
21052 unsigned int i;
21053 uint64_t entries;
21054 unsigned char *data;
21055 bool res = true;
21056 bool found = false;
21057
21058 if (!do_got_section_contents)
21059 return res;
21060
21061 switch (filedata->file_header.e_type)
21062 {
21063 case ET_DYN:
21064 case ET_EXEC:
21065 break;
21066 default:
21067 goto out;
21068 }
21069
21070 switch (filedata->file_header.e_machine)
21071 {
21072 case EM_MIPS:
21073 case EM_MIPS_RS3_LE:
21074 /* process_mips_specific also displays GOT related contents. */
21075 if (!do_arch)
21076 res = process_mips_specific (filedata, true);
21077 found = true;
21078 goto out;
21079 }
21080
21081 if (all_relocations_count > 1)
21082 qsort (all_relocations_root, all_relocations_count,
21083 sizeof (elf_relocation), elf_relocation_cmp);
21084
21085 initialise_dumps_byname (filedata);
21086
21087 for (i = 0, section = filedata->section_headers;
21088 i < filedata->file_header.e_shnum;
21089 i++, section++)
21090 if (section->sh_type == SHT_PROGBITS
21091 && section->sh_size != 0)
21092 {
21093 const char *name = printable_section_name (filedata, section);
21094
21095 if (!startswith (name, ".got"))
21096 continue;
21097
21098 found = true;
21099
21100 data = (unsigned char *) get_section_contents (section,
21101 filedata);
21102 if (data == NULL)
21103 {
21104 res = false;
21105 goto out;
21106 }
21107
21108 uint32_t entsz = section->sh_entsize;
21109 /* NB: Gold, lld and mold set sh_entsize to 0 on .got and
21110 .got.plt sections. */
21111 if (entsz == 0)
21112 {
21113 if (is_32bit_elf)
21114 switch (filedata->file_header.e_machine)
21115 {
21116 default:
21117 entsz = 4;
21118 break;
21119 case EM_X86_64:
21120 /* x32 uses 8 byte GOT sh_entsize. */
21121 entsz = 8;
21122 break;
21123 }
21124 else
21125 entsz = 8;
21126 }
21127
21128 entries = section->sh_size / entsz;
21129 if (entries == 1)
21130 printf (_("\nGlobal Offset Table '%s' contains 1 entry:\n"),
21131 name);
21132 else
21133 printf (_("\nGlobal Offset Table '%s' contains %" PRIu64
21134 " entries:\n"), name, entries);
21135
21136 uint64_t g;
21137
21138 if (is_32bit_elf)
21139 {
21140 uint32_t j, n = entries;
21141 uint32_t addr;
21142 struct got32
21143 {
21144 unsigned char bytes[4];
21145 } *got;
21146
21147 if (do_wide)
21148 printf (_(" Index: Address Reloc Sym. Name + Addend/Value\n"));
21149 /* |---9---| |---8--| |---------22---------| |........... */
21150 else
21151 printf (_(" Index: Address Reloc Sym. Name + Addend/Value\n"));
21152 /* |--7--| |---8--| |-------17------| |........... */
21153
21154 addr = section->sh_addr;
21155 got = (struct got32 *) data;
21156 for (j = 0; j < n; j++)
21157 {
21158 g = BYTE_GET (got[j].bytes);
21159 if (do_wide)
21160 printf ("%8" PRIu32 ": %8.8" PRIx32, j, addr);
21161 else
21162 printf ("%6" PRIu32 ": %8.8" PRIx32, j, addr);
21163 display_elf_relocation_at (addr, g);
21164 putchar ('\n');
21165 addr += entsz;
21166 }
21167 }
21168 else
21169 {
21170 uint64_t j, addr;
21171 struct got64
21172 {
21173 unsigned char bytes[4];
21174 } *got;
21175
21176 if (do_wide)
21177 printf (_(" Index: Address Reloc Sym. Name + Addend/Value\n"));
21178 /* |---9---| |------16------| |---------22---------| |........... */
21179 else
21180 printf (_(" Index: Address Reloc Sym. Name + Addend/Value\n"));
21181 /* |--7--| |----12----| |------17-------| |........... */
21182
21183 addr = section->sh_addr;
21184 got = (struct got64 *) data;
21185 for (j = 0; j < entries; j++)
21186 {
21187 g = BYTE_GET (got[j].bytes);
21188 if (do_wide)
21189 printf ("%8" PRIu64 ": %16.16" PRIx64, j, addr);
21190 else
21191 printf ("%6" PRIu64 ": %12.12" PRIx64, j, addr);
21192 display_elf_relocation_at (addr, g);
21193 putchar ('\n');
21194 addr += entsz;
21195 }
21196 }
21197
21198 free (data);
21199 }
21200
21201 out:
21202 if (! found)
21203 {
21204 if (filedata->is_separate)
21205 printf (_("\nThere is no GOT section in linked file '%s'.\n"),
21206 filedata->file_name);
21207 else
21208 printf (_("\nThere is no GOT section in this file.\n"));
21209 }
21210
21211 for (size_t j = 0; j < all_relocations_count; j++)
21212 free (all_relocations_root[j].r_symbol);
21213 free (all_relocations_root);
21214 all_relocations_root = NULL;
21215 all_relocations = NULL;
21216 all_relocations_count = 0;
21217 return res;
21218 }
21219
21220 static bool
21221 process_gnu_liblist (Filedata * filedata)
21222 {
21223 Elf_Internal_Shdr * section;
21224 Elf_Internal_Shdr * string_sec;
21225 Elf32_External_Lib * elib;
21226 char * strtab;
21227 size_t strtab_size;
21228 size_t cnt;
21229 uint64_t num_liblist;
21230 unsigned i;
21231 bool res = true;
21232
21233 if (! do_arch)
21234 return true;
21235
21236 for (i = 0, section = filedata->section_headers;
21237 i < filedata->file_header.e_shnum;
21238 i++, section++)
21239 {
21240 switch (section->sh_type)
21241 {
21242 case SHT_GNU_LIBLIST:
21243 if (section->sh_link >= filedata->file_header.e_shnum)
21244 break;
21245
21246 elib = (Elf32_External_Lib *)
21247 get_data (NULL, filedata, section->sh_offset, 1, section->sh_size,
21248 _("liblist section data"));
21249
21250 if (elib == NULL)
21251 {
21252 res = false;
21253 break;
21254 }
21255
21256 string_sec = filedata->section_headers + section->sh_link;
21257 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
21258 string_sec->sh_size,
21259 _("liblist string table"));
21260 if (strtab == NULL
21261 || section->sh_entsize != sizeof (Elf32_External_Lib))
21262 {
21263 free (elib);
21264 free (strtab);
21265 res = false;
21266 break;
21267 }
21268 strtab_size = string_sec->sh_size;
21269
21270 num_liblist = section->sh_size / sizeof (Elf32_External_Lib);
21271 printf (ngettext ("\nLibrary list section '%s' contains %" PRIu64
21272 " entries:\n",
21273 "\nLibrary list section '%s' contains %" PRIu64
21274 " entries:\n",
21275 num_liblist),
21276 printable_section_name (filedata, section),
21277 num_liblist);
21278
21279 puts (_(" Library Time Stamp Checksum Version Flags"));
21280
21281 for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
21282 ++cnt)
21283 {
21284 Elf32_Lib liblist;
21285 time_t atime;
21286 char timebuf[128];
21287 struct tm * tmp;
21288
21289 liblist.l_name = BYTE_GET (elib[cnt].l_name);
21290 atime = BYTE_GET (elib[cnt].l_time_stamp);
21291 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
21292 liblist.l_version = BYTE_GET (elib[cnt].l_version);
21293 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
21294
21295 tmp = gmtime (&atime);
21296 snprintf (timebuf, sizeof (timebuf),
21297 "%04u-%02u-%02uT%02u:%02u:%02u",
21298 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
21299 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
21300
21301 printf ("%3zu: ", cnt);
21302 if (do_wide)
21303 printf ("%-20s", liblist.l_name < strtab_size
21304 ? strtab + liblist.l_name : _("<corrupt>"));
21305 else
21306 printf ("%-20.20s", liblist.l_name < strtab_size
21307 ? strtab + liblist.l_name : _("<corrupt>"));
21308 printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
21309 liblist.l_version, liblist.l_flags);
21310 }
21311
21312 free (elib);
21313 free (strtab);
21314 }
21315 }
21316
21317 return res;
21318 }
21319
21320 static const char *
21321 get_note_type (Filedata * filedata, unsigned e_type)
21322 {
21323 static char buff[64];
21324
21325 if (filedata->file_header.e_type == ET_CORE)
21326 switch (e_type)
21327 {
21328 case NT_AUXV:
21329 return _("NT_AUXV (auxiliary vector)");
21330 case NT_PRSTATUS:
21331 return _("NT_PRSTATUS (prstatus structure)");
21332 case NT_FPREGSET:
21333 return _("NT_FPREGSET (floating point registers)");
21334 case NT_PRPSINFO:
21335 return _("NT_PRPSINFO (prpsinfo structure)");
21336 case NT_TASKSTRUCT:
21337 return _("NT_TASKSTRUCT (task structure)");
21338 case NT_GDB_TDESC:
21339 return _("NT_GDB_TDESC (GDB XML target description)");
21340 case NT_PRXFPREG:
21341 return _("NT_PRXFPREG (user_xfpregs structure)");
21342 case NT_PPC_VMX:
21343 return _("NT_PPC_VMX (ppc Altivec registers)");
21344 case NT_PPC_VSX:
21345 return _("NT_PPC_VSX (ppc VSX registers)");
21346 case NT_PPC_TAR:
21347 return _("NT_PPC_TAR (ppc TAR register)");
21348 case NT_PPC_PPR:
21349 return _("NT_PPC_PPR (ppc PPR register)");
21350 case NT_PPC_DSCR:
21351 return _("NT_PPC_DSCR (ppc DSCR register)");
21352 case NT_PPC_EBB:
21353 return _("NT_PPC_EBB (ppc EBB registers)");
21354 case NT_PPC_PMU:
21355 return _("NT_PPC_PMU (ppc PMU registers)");
21356 case NT_PPC_TM_CGPR:
21357 return _("NT_PPC_TM_CGPR (ppc checkpointed GPR registers)");
21358 case NT_PPC_TM_CFPR:
21359 return _("NT_PPC_TM_CFPR (ppc checkpointed floating point registers)");
21360 case NT_PPC_TM_CVMX:
21361 return _("NT_PPC_TM_CVMX (ppc checkpointed Altivec registers)");
21362 case NT_PPC_TM_CVSX:
21363 return _("NT_PPC_TM_CVSX (ppc checkpointed VSX registers)");
21364 case NT_PPC_TM_SPR:
21365 return _("NT_PPC_TM_SPR (ppc TM special purpose registers)");
21366 case NT_PPC_TM_CTAR:
21367 return _("NT_PPC_TM_CTAR (ppc checkpointed TAR register)");
21368 case NT_PPC_TM_CPPR:
21369 return _("NT_PPC_TM_CPPR (ppc checkpointed PPR register)");
21370 case NT_PPC_TM_CDSCR:
21371 return _("NT_PPC_TM_CDSCR (ppc checkpointed DSCR register)");
21372 case NT_386_TLS:
21373 return _("NT_386_TLS (x86 TLS information)");
21374 case NT_386_IOPERM:
21375 return _("NT_386_IOPERM (x86 I/O permissions)");
21376 case NT_X86_XSTATE:
21377 return _("NT_X86_XSTATE (x86 XSAVE extended state)");
21378 case NT_X86_CET:
21379 return _("NT_X86_CET (x86 CET state)");
21380 case NT_X86_SHSTK:
21381 return _("NT_X86_SHSTK (x86 SHSTK state)");
21382 case NT_S390_HIGH_GPRS:
21383 return _("NT_S390_HIGH_GPRS (s390 upper register halves)");
21384 case NT_S390_TIMER:
21385 return _("NT_S390_TIMER (s390 timer register)");
21386 case NT_S390_TODCMP:
21387 return _("NT_S390_TODCMP (s390 TOD comparator register)");
21388 case NT_S390_TODPREG:
21389 return _("NT_S390_TODPREG (s390 TOD programmable register)");
21390 case NT_S390_CTRS:
21391 return _("NT_S390_CTRS (s390 control registers)");
21392 case NT_S390_PREFIX:
21393 return _("NT_S390_PREFIX (s390 prefix register)");
21394 case NT_S390_LAST_BREAK:
21395 return _("NT_S390_LAST_BREAK (s390 last breaking event address)");
21396 case NT_S390_SYSTEM_CALL:
21397 return _("NT_S390_SYSTEM_CALL (s390 system call restart data)");
21398 case NT_S390_TDB:
21399 return _("NT_S390_TDB (s390 transaction diagnostic block)");
21400 case NT_S390_VXRS_LOW:
21401 return _("NT_S390_VXRS_LOW (s390 vector registers 0-15 upper half)");
21402 case NT_S390_VXRS_HIGH:
21403 return _("NT_S390_VXRS_HIGH (s390 vector registers 16-31)");
21404 case NT_S390_GS_CB:
21405 return _("NT_S390_GS_CB (s390 guarded-storage registers)");
21406 case NT_S390_GS_BC:
21407 return _("NT_S390_GS_BC (s390 guarded-storage broadcast control)");
21408 case NT_ARM_VFP:
21409 return _("NT_ARM_VFP (arm VFP registers)");
21410 case NT_ARM_TLS:
21411 return _("NT_ARM_TLS (AArch TLS registers)");
21412 case NT_ARM_HW_BREAK:
21413 return _("NT_ARM_HW_BREAK (AArch hardware breakpoint registers)");
21414 case NT_ARM_HW_WATCH:
21415 return _("NT_ARM_HW_WATCH (AArch hardware watchpoint registers)");
21416 case NT_ARM_SYSTEM_CALL:
21417 return _("NT_ARM_SYSTEM_CALL (AArch system call number)");
21418 case NT_ARM_SVE:
21419 return _("NT_ARM_SVE (AArch SVE registers)");
21420 case NT_ARM_PAC_MASK:
21421 return _("NT_ARM_PAC_MASK (AArch pointer authentication code masks)");
21422 case NT_ARM_PACA_KEYS:
21423 return _("NT_ARM_PACA_KEYS (ARM pointer authentication address keys)");
21424 case NT_ARM_PACG_KEYS:
21425 return _("NT_ARM_PACG_KEYS (ARM pointer authentication generic keys)");
21426 case NT_ARM_TAGGED_ADDR_CTRL:
21427 return _("NT_ARM_TAGGED_ADDR_CTRL (AArch tagged address control)");
21428 case NT_ARM_SSVE:
21429 return _("NT_ARM_SSVE (AArch64 streaming SVE registers)");
21430 case NT_ARM_ZA:
21431 return _("NT_ARM_ZA (AArch64 SME ZA register)");
21432 case NT_ARM_ZT:
21433 return _("NT_ARM_ZT (AArch64 SME2 ZT registers)");
21434 case NT_ARM_PAC_ENABLED_KEYS:
21435 return _("NT_ARM_PAC_ENABLED_KEYS (AArch64 pointer authentication enabled keys)");
21436 case NT_ARC_V2:
21437 return _("NT_ARC_V2 (ARC HS accumulator/extra registers)");
21438 case NT_RISCV_CSR:
21439 return _("NT_RISCV_CSR (RISC-V control and status registers)");
21440 case NT_PSTATUS:
21441 return _("NT_PSTATUS (pstatus structure)");
21442 case NT_FPREGS:
21443 return _("NT_FPREGS (floating point registers)");
21444 case NT_PSINFO:
21445 return _("NT_PSINFO (psinfo structure)");
21446 case NT_LWPSTATUS:
21447 return _("NT_LWPSTATUS (lwpstatus_t structure)");
21448 case NT_LWPSINFO:
21449 return _("NT_LWPSINFO (lwpsinfo_t structure)");
21450 case NT_WIN32PSTATUS:
21451 return _("NT_WIN32PSTATUS (win32_pstatus structure)");
21452 case NT_SIGINFO:
21453 return _("NT_SIGINFO (siginfo_t data)");
21454 case NT_FILE:
21455 return _("NT_FILE (mapped files)");
21456 default:
21457 break;
21458 }
21459 else
21460 switch (e_type)
21461 {
21462 case NT_VERSION:
21463 return _("NT_VERSION (version)");
21464 case NT_ARCH:
21465 return _("NT_ARCH (architecture)");
21466 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
21467 return _("OPEN");
21468 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
21469 return _("func");
21470 case NT_GO_BUILDID:
21471 return _("GO BUILDID");
21472 case FDO_PACKAGING_METADATA:
21473 return _("FDO_PACKAGING_METADATA");
21474 case FDO_DLOPEN_METADATA:
21475 return _("FDO_DLOPEN_METADATA");
21476 default:
21477 break;
21478 }
21479
21480 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
21481 return buff;
21482 }
21483
21484 static bool
21485 print_core_note (Elf_Internal_Note *pnote)
21486 {
21487 unsigned int addr_size = is_32bit_elf ? 4 : 8;
21488 uint64_t count, page_size;
21489 unsigned char *descdata, *filenames, *descend;
21490
21491 if (pnote->type != NT_FILE)
21492 {
21493 if (do_wide)
21494 printf ("\n");
21495 return true;
21496 }
21497
21498 if (pnote->descsz < 2 * addr_size)
21499 {
21500 error (_(" Malformed note - too short for header\n"));
21501 return false;
21502 }
21503
21504 descdata = (unsigned char *) pnote->descdata;
21505 descend = descdata + pnote->descsz;
21506
21507 if (descdata[pnote->descsz - 1] != '\0')
21508 {
21509 error (_(" Malformed note - does not end with \\0\n"));
21510 return false;
21511 }
21512
21513 count = byte_get (descdata, addr_size);
21514 descdata += addr_size;
21515
21516 page_size = byte_get (descdata, addr_size);
21517 descdata += addr_size;
21518
21519 if (count > ((uint64_t) -1 - 2 * addr_size) / (3 * addr_size)
21520 || pnote->descsz < 2 * addr_size + count * 3 * addr_size)
21521 {
21522 error (_(" Malformed note - too short for supplied file count\n"));
21523 return false;
21524 }
21525
21526 printf (_(" Page size: "));
21527 print_vma (page_size, DEC);
21528 printf ("\n");
21529
21530 printf (_(" %*s%*s%*s\n"),
21531 (int) (2 + 2 * addr_size), _("Start"),
21532 (int) (4 + 2 * addr_size), _("End"),
21533 (int) (4 + 2 * addr_size), _("Page Offset"));
21534 filenames = descdata + count * 3 * addr_size;
21535 while (count-- > 0)
21536 {
21537 uint64_t start, end, file_ofs;
21538
21539 if (filenames == descend)
21540 {
21541 error (_(" Malformed note - filenames end too early\n"));
21542 return false;
21543 }
21544
21545 start = byte_get (descdata, addr_size);
21546 descdata += addr_size;
21547 end = byte_get (descdata, addr_size);
21548 descdata += addr_size;
21549 file_ofs = byte_get (descdata, addr_size);
21550 descdata += addr_size;
21551
21552 printf (" ");
21553 print_vma (start, FULL_HEX);
21554 printf (" ");
21555 print_vma (end, FULL_HEX);
21556 printf (" ");
21557 print_vma (file_ofs, FULL_HEX);
21558 printf ("\n %s\n", filenames);
21559
21560 filenames += 1 + strlen ((char *) filenames);
21561 }
21562
21563 return true;
21564 }
21565
21566 static const char *
21567 get_gnu_elf_note_type (unsigned e_type)
21568 {
21569 /* NB/ Keep this switch statement in sync with print_gnu_note (). */
21570 switch (e_type)
21571 {
21572 case NT_GNU_ABI_TAG:
21573 return _("NT_GNU_ABI_TAG (ABI version tag)");
21574 case NT_GNU_HWCAP:
21575 return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
21576 case NT_GNU_BUILD_ID:
21577 return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
21578 case NT_GNU_GOLD_VERSION:
21579 return _("NT_GNU_GOLD_VERSION (gold version)");
21580 case NT_GNU_PROPERTY_TYPE_0:
21581 return _("NT_GNU_PROPERTY_TYPE_0");
21582 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
21583 return _("NT_GNU_BUILD_ATTRIBUTE_OPEN");
21584 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
21585 return _("NT_GNU_BUILD_ATTRIBUTE_FUNC");
21586 default:
21587 {
21588 static char buff[64];
21589
21590 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
21591 return buff;
21592 }
21593 }
21594 }
21595
21596 static void
21597 decode_x86_compat_isa (unsigned int bitmask)
21598 {
21599 while (bitmask)
21600 {
21601 unsigned int bit = bitmask & (- bitmask);
21602
21603 bitmask &= ~ bit;
21604 switch (bit)
21605 {
21606 case GNU_PROPERTY_X86_COMPAT_ISA_1_486:
21607 printf ("i486");
21608 break;
21609 case GNU_PROPERTY_X86_COMPAT_ISA_1_586:
21610 printf ("586");
21611 break;
21612 case GNU_PROPERTY_X86_COMPAT_ISA_1_686:
21613 printf ("686");
21614 break;
21615 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE:
21616 printf ("SSE");
21617 break;
21618 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE2:
21619 printf ("SSE2");
21620 break;
21621 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE3:
21622 printf ("SSE3");
21623 break;
21624 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSSE3:
21625 printf ("SSSE3");
21626 break;
21627 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_1:
21628 printf ("SSE4_1");
21629 break;
21630 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_2:
21631 printf ("SSE4_2");
21632 break;
21633 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX:
21634 printf ("AVX");
21635 break;
21636 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX2:
21637 printf ("AVX2");
21638 break;
21639 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512F:
21640 printf ("AVX512F");
21641 break;
21642 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512CD:
21643 printf ("AVX512CD");
21644 break;
21645 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512ER:
21646 printf ("AVX512ER");
21647 break;
21648 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512PF:
21649 printf ("AVX512PF");
21650 break;
21651 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512VL:
21652 printf ("AVX512VL");
21653 break;
21654 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512DQ:
21655 printf ("AVX512DQ");
21656 break;
21657 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512BW:
21658 printf ("AVX512BW");
21659 break;
21660 default:
21661 printf (_("<unknown: %x>"), bit);
21662 break;
21663 }
21664 if (bitmask)
21665 printf (", ");
21666 }
21667 }
21668
21669 static void
21670 decode_x86_compat_2_isa (unsigned int bitmask)
21671 {
21672 if (!bitmask)
21673 {
21674 printf (_("<None>"));
21675 return;
21676 }
21677
21678 while (bitmask)
21679 {
21680 unsigned int bit = bitmask & (- bitmask);
21681
21682 bitmask &= ~ bit;
21683 switch (bit)
21684 {
21685 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_CMOV:
21686 printf ("CMOV");
21687 break;
21688 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE:
21689 printf ("SSE");
21690 break;
21691 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE2:
21692 printf ("SSE2");
21693 break;
21694 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE3:
21695 printf ("SSE3");
21696 break;
21697 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSSE3:
21698 printf ("SSSE3");
21699 break;
21700 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_1:
21701 printf ("SSE4_1");
21702 break;
21703 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_2:
21704 printf ("SSE4_2");
21705 break;
21706 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX:
21707 printf ("AVX");
21708 break;
21709 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX2:
21710 printf ("AVX2");
21711 break;
21712 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_FMA:
21713 printf ("FMA");
21714 break;
21715 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512F:
21716 printf ("AVX512F");
21717 break;
21718 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512CD:
21719 printf ("AVX512CD");
21720 break;
21721 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512ER:
21722 printf ("AVX512ER");
21723 break;
21724 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512PF:
21725 printf ("AVX512PF");
21726 break;
21727 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512VL:
21728 printf ("AVX512VL");
21729 break;
21730 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512DQ:
21731 printf ("AVX512DQ");
21732 break;
21733 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512BW:
21734 printf ("AVX512BW");
21735 break;
21736 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4FMAPS:
21737 printf ("AVX512_4FMAPS");
21738 break;
21739 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4VNNIW:
21740 printf ("AVX512_4VNNIW");
21741 break;
21742 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BITALG:
21743 printf ("AVX512_BITALG");
21744 break;
21745 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_IFMA:
21746 printf ("AVX512_IFMA");
21747 break;
21748 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI:
21749 printf ("AVX512_VBMI");
21750 break;
21751 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI2:
21752 printf ("AVX512_VBMI2");
21753 break;
21754 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VNNI:
21755 printf ("AVX512_VNNI");
21756 break;
21757 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BF16:
21758 printf ("AVX512_BF16");
21759 break;
21760 default:
21761 printf (_("<unknown: %x>"), bit);
21762 break;
21763 }
21764 if (bitmask)
21765 printf (", ");
21766 }
21767 }
21768
21769 static const char *
21770 get_amdgpu_elf_note_type (unsigned int e_type)
21771 {
21772 switch (e_type)
21773 {
21774 case NT_AMDGPU_METADATA:
21775 return _("NT_AMDGPU_METADATA (code object metadata)");
21776 default:
21777 {
21778 static char buf[64];
21779 snprintf (buf, sizeof (buf), _("Unknown note type: (0x%08x)"), e_type);
21780 return buf;
21781 }
21782 }
21783 }
21784
21785 static void
21786 decode_x86_isa (unsigned int bitmask)
21787 {
21788 while (bitmask)
21789 {
21790 unsigned int bit = bitmask & (- bitmask);
21791
21792 bitmask &= ~ bit;
21793 switch (bit)
21794 {
21795 case GNU_PROPERTY_X86_ISA_1_BASELINE:
21796 printf ("x86-64-baseline");
21797 break;
21798 case GNU_PROPERTY_X86_ISA_1_V2:
21799 printf ("x86-64-v2");
21800 break;
21801 case GNU_PROPERTY_X86_ISA_1_V3:
21802 printf ("x86-64-v3");
21803 break;
21804 case GNU_PROPERTY_X86_ISA_1_V4:
21805 printf ("x86-64-v4");
21806 break;
21807 default:
21808 printf (_("<unknown: %x>"), bit);
21809 break;
21810 }
21811 if (bitmask)
21812 printf (", ");
21813 }
21814 }
21815
21816 static void
21817 decode_x86_feature_1 (unsigned int bitmask)
21818 {
21819 if (!bitmask)
21820 {
21821 printf (_("<None>"));
21822 return;
21823 }
21824
21825 while (bitmask)
21826 {
21827 unsigned int bit = bitmask & (- bitmask);
21828
21829 bitmask &= ~ bit;
21830 switch (bit)
21831 {
21832 case GNU_PROPERTY_X86_FEATURE_1_IBT:
21833 printf ("IBT");
21834 break;
21835 case GNU_PROPERTY_X86_FEATURE_1_SHSTK:
21836 printf ("SHSTK");
21837 break;
21838 case GNU_PROPERTY_X86_FEATURE_1_LAM_U48:
21839 printf ("LAM_U48");
21840 break;
21841 case GNU_PROPERTY_X86_FEATURE_1_LAM_U57:
21842 printf ("LAM_U57");
21843 break;
21844 default:
21845 printf (_("<unknown: %x>"), bit);
21846 break;
21847 }
21848 if (bitmask)
21849 printf (", ");
21850 }
21851 }
21852
21853 static void
21854 decode_x86_feature_2 (unsigned int bitmask)
21855 {
21856 if (!bitmask)
21857 {
21858 printf (_("<None>"));
21859 return;
21860 }
21861
21862 while (bitmask)
21863 {
21864 unsigned int bit = bitmask & (- bitmask);
21865
21866 bitmask &= ~ bit;
21867 switch (bit)
21868 {
21869 case GNU_PROPERTY_X86_FEATURE_2_X86:
21870 printf ("x86");
21871 break;
21872 case GNU_PROPERTY_X86_FEATURE_2_X87:
21873 printf ("x87");
21874 break;
21875 case GNU_PROPERTY_X86_FEATURE_2_MMX:
21876 printf ("MMX");
21877 break;
21878 case GNU_PROPERTY_X86_FEATURE_2_XMM:
21879 printf ("XMM");
21880 break;
21881 case GNU_PROPERTY_X86_FEATURE_2_YMM:
21882 printf ("YMM");
21883 break;
21884 case GNU_PROPERTY_X86_FEATURE_2_ZMM:
21885 printf ("ZMM");
21886 break;
21887 case GNU_PROPERTY_X86_FEATURE_2_TMM:
21888 printf ("TMM");
21889 break;
21890 case GNU_PROPERTY_X86_FEATURE_2_MASK:
21891 printf ("MASK");
21892 break;
21893 case GNU_PROPERTY_X86_FEATURE_2_FXSR:
21894 printf ("FXSR");
21895 break;
21896 case GNU_PROPERTY_X86_FEATURE_2_XSAVE:
21897 printf ("XSAVE");
21898 break;
21899 case GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT:
21900 printf ("XSAVEOPT");
21901 break;
21902 case GNU_PROPERTY_X86_FEATURE_2_XSAVEC:
21903 printf ("XSAVEC");
21904 break;
21905 default:
21906 printf (_("<unknown: %x>"), bit);
21907 break;
21908 }
21909 if (bitmask)
21910 printf (", ");
21911 }
21912 }
21913
21914 static void
21915 decode_aarch64_feature_1_and (unsigned int bitmask)
21916 {
21917 while (bitmask)
21918 {
21919 unsigned int bit = bitmask & (- bitmask);
21920
21921 bitmask &= ~ bit;
21922 switch (bit)
21923 {
21924 case GNU_PROPERTY_AARCH64_FEATURE_1_BTI:
21925 printf ("BTI");
21926 break;
21927
21928 case GNU_PROPERTY_AARCH64_FEATURE_1_PAC:
21929 printf ("PAC");
21930 break;
21931
21932 case GNU_PROPERTY_AARCH64_FEATURE_1_GCS:
21933 printf ("GCS");
21934 break;
21935
21936 default:
21937 printf (_("<unknown: %x>"), bit);
21938 break;
21939 }
21940 if (bitmask)
21941 printf (", ");
21942 }
21943 }
21944
21945 static void
21946 decode_riscv_feature_1_and (unsigned int bitmask)
21947 {
21948 while (bitmask)
21949 {
21950 unsigned int bit = bitmask & (- bitmask);
21951
21952 bitmask &= ~ bit;
21953 switch (bit)
21954 {
21955 case GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_UNLABELED:
21956 printf ("CFI_LP_UNLABELED");
21957 break;
21958
21959 case GNU_PROPERTY_RISCV_FEATURE_1_CFI_SS:
21960 printf ("CFI_SS");
21961 break;
21962
21963 default:
21964 printf (_("<unknown: %x>"), bit);
21965 break;
21966 }
21967 if (bitmask)
21968 printf (", ");
21969 }
21970 }
21971
21972 static void
21973 decode_1_needed (unsigned int bitmask)
21974 {
21975 while (bitmask)
21976 {
21977 unsigned int bit = bitmask & (- bitmask);
21978
21979 bitmask &= ~ bit;
21980 switch (bit)
21981 {
21982 case GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS:
21983 printf ("indirect external access");
21984 break;
21985 default:
21986 printf (_("<unknown: %x>"), bit);
21987 break;
21988 }
21989 if (bitmask)
21990 printf (", ");
21991 }
21992 }
21993
21994 static void
21995 print_gnu_property_note (Filedata * filedata, Elf_Internal_Note * pnote)
21996 {
21997 unsigned char * ptr = (unsigned char *) pnote->descdata;
21998 unsigned char * ptr_end = ptr + pnote->descsz;
21999 unsigned int size = is_32bit_elf ? 4 : 8;
22000
22001 printf (_(" Properties: "));
22002
22003 if (pnote->descsz < 8 || (pnote->descsz % size) != 0)
22004 {
22005 printf (_("<corrupt GNU_PROPERTY_TYPE, size = %#lx>\n"), pnote->descsz);
22006 return;
22007 }
22008
22009 while (ptr < ptr_end)
22010 {
22011 unsigned int j;
22012 unsigned int type;
22013 unsigned int datasz;
22014
22015 if ((size_t) (ptr_end - ptr) < 8)
22016 {
22017 printf (_("<corrupt descsz: %#lx>\n"), pnote->descsz);
22018 break;
22019 }
22020
22021 type = byte_get (ptr, 4);
22022 datasz = byte_get (ptr + 4, 4);
22023
22024 ptr += 8;
22025
22026 if (datasz > (size_t) (ptr_end - ptr))
22027 {
22028 printf (_("<corrupt type (%#x) datasz: %#x>\n"),
22029 type, datasz);
22030 break;
22031 }
22032
22033 if (type >= GNU_PROPERTY_LOPROC && type <= GNU_PROPERTY_HIPROC)
22034 {
22035 if (filedata->file_header.e_machine == EM_X86_64
22036 || filedata->file_header.e_machine == EM_IAMCU
22037 || filedata->file_header.e_machine == EM_386)
22038 {
22039 unsigned int bitmask;
22040
22041 if (datasz == 4)
22042 bitmask = byte_get (ptr, 4);
22043 else
22044 bitmask = 0;
22045
22046 switch (type)
22047 {
22048 case GNU_PROPERTY_X86_ISA_1_USED:
22049 if (datasz != 4)
22050 printf (_("x86 ISA used: <corrupt length: %#x> "),
22051 datasz);
22052 else
22053 {
22054 printf ("x86 ISA used: ");
22055 decode_x86_isa (bitmask);
22056 }
22057 goto next;
22058
22059 case GNU_PROPERTY_X86_ISA_1_NEEDED:
22060 if (datasz != 4)
22061 printf (_("x86 ISA needed: <corrupt length: %#x> "),
22062 datasz);
22063 else
22064 {
22065 printf ("x86 ISA needed: ");
22066 decode_x86_isa (bitmask);
22067 }
22068 goto next;
22069
22070 case GNU_PROPERTY_X86_FEATURE_1_AND:
22071 if (datasz != 4)
22072 printf (_("x86 feature: <corrupt length: %#x> "),
22073 datasz);
22074 else
22075 {
22076 printf ("x86 feature: ");
22077 decode_x86_feature_1 (bitmask);
22078 }
22079 goto next;
22080
22081 case GNU_PROPERTY_X86_FEATURE_2_USED:
22082 if (datasz != 4)
22083 printf (_("x86 feature used: <corrupt length: %#x> "),
22084 datasz);
22085 else
22086 {
22087 printf ("x86 feature used: ");
22088 decode_x86_feature_2 (bitmask);
22089 }
22090 goto next;
22091
22092 case GNU_PROPERTY_X86_FEATURE_2_NEEDED:
22093 if (datasz != 4)
22094 printf (_("x86 feature needed: <corrupt length: %#x> "), datasz);
22095 else
22096 {
22097 printf ("x86 feature needed: ");
22098 decode_x86_feature_2 (bitmask);
22099 }
22100 goto next;
22101
22102 case GNU_PROPERTY_X86_COMPAT_ISA_1_USED:
22103 if (datasz != 4)
22104 printf (_("x86 ISA used: <corrupt length: %#x> "),
22105 datasz);
22106 else
22107 {
22108 printf ("x86 ISA used: ");
22109 decode_x86_compat_isa (bitmask);
22110 }
22111 goto next;
22112
22113 case GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED:
22114 if (datasz != 4)
22115 printf (_("x86 ISA needed: <corrupt length: %#x> "),
22116 datasz);
22117 else
22118 {
22119 printf ("x86 ISA needed: ");
22120 decode_x86_compat_isa (bitmask);
22121 }
22122 goto next;
22123
22124 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_USED:
22125 if (datasz != 4)
22126 printf (_("x86 ISA used: <corrupt length: %#x> "),
22127 datasz);
22128 else
22129 {
22130 printf ("x86 ISA used: ");
22131 decode_x86_compat_2_isa (bitmask);
22132 }
22133 goto next;
22134
22135 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_NEEDED:
22136 if (datasz != 4)
22137 printf (_("x86 ISA needed: <corrupt length: %#x> "),
22138 datasz);
22139 else
22140 {
22141 printf ("x86 ISA needed: ");
22142 decode_x86_compat_2_isa (bitmask);
22143 }
22144 goto next;
22145
22146 default:
22147 break;
22148 }
22149 }
22150 else if (filedata->file_header.e_machine == EM_AARCH64)
22151 {
22152 if (type == GNU_PROPERTY_AARCH64_FEATURE_1_AND)
22153 {
22154 printf ("AArch64 feature: ");
22155 if (datasz != 4)
22156 printf (_("<corrupt length: %#x> "), datasz);
22157 else
22158 decode_aarch64_feature_1_and (byte_get (ptr, 4));
22159 goto next;
22160 }
22161 }
22162 else if (filedata->file_header.e_machine == EM_RISCV)
22163 {
22164 if (type == GNU_PROPERTY_RISCV_FEATURE_1_AND)
22165 {
22166 printf ("RISC-V AND feature: ");
22167 if (datasz != 4)
22168 printf (_("<corrupt length: %#x> "), datasz);
22169 else
22170 decode_riscv_feature_1_and (byte_get (ptr, 4));
22171 goto next;
22172 }
22173 }
22174 }
22175 else
22176 {
22177 switch (type)
22178 {
22179 case GNU_PROPERTY_STACK_SIZE:
22180 printf (_("stack size: "));
22181 if (datasz != size)
22182 printf (_("<corrupt length: %#x> "), datasz);
22183 else
22184 printf ("%#" PRIx64, byte_get (ptr, size));
22185 goto next;
22186
22187 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
22188 printf ("no copy on protected ");
22189 if (datasz)
22190 printf (_("<corrupt length: %#x> "), datasz);
22191 goto next;
22192
22193 case GNU_PROPERTY_MEMORY_SEAL:
22194 printf ("memory seal ");
22195 if (datasz)
22196 printf (_("<corrupt length: %#x> "), datasz);
22197 goto next;
22198
22199 default:
22200 if ((type >= GNU_PROPERTY_UINT32_AND_LO
22201 && type <= GNU_PROPERTY_UINT32_AND_HI)
22202 || (type >= GNU_PROPERTY_UINT32_OR_LO
22203 && type <= GNU_PROPERTY_UINT32_OR_HI))
22204 {
22205 switch (type)
22206 {
22207 case GNU_PROPERTY_1_NEEDED:
22208 if (datasz != 4)
22209 printf (_("1_needed: <corrupt length: %#x> "),
22210 datasz);
22211 else
22212 {
22213 unsigned int bitmask = byte_get (ptr, 4);
22214 printf ("1_needed: ");
22215 decode_1_needed (bitmask);
22216 }
22217 goto next;
22218
22219 default:
22220 break;
22221 }
22222 if (type <= GNU_PROPERTY_UINT32_AND_HI)
22223 printf (_("UINT32_AND (%#x): "), type);
22224 else
22225 printf (_("UINT32_OR (%#x): "), type);
22226 if (datasz != 4)
22227 printf (_("<corrupt length: %#x> "), datasz);
22228 else
22229 printf ("%#x", (unsigned int) byte_get (ptr, 4));
22230 goto next;
22231 }
22232 break;
22233 }
22234 }
22235
22236 if (type < GNU_PROPERTY_LOPROC)
22237 printf (_("<unknown type %#x data: "), type);
22238 else if (type < GNU_PROPERTY_LOUSER)
22239 printf (_("<processor-specific type %#x data: "), type);
22240 else
22241 printf (_("<application-specific type %#x data: "), type);
22242 for (j = 0; j < datasz; ++j)
22243 printf ("%02x ", ptr[j] & 0xff);
22244 printf (">");
22245
22246 next:
22247 ptr += ((datasz + (size - 1)) & ~ (size - 1));
22248 if (ptr == ptr_end)
22249 break;
22250
22251 if (do_wide)
22252 printf (", ");
22253 else
22254 printf ("\n\t");
22255 }
22256
22257 printf ("\n");
22258 }
22259
22260 static bool
22261 print_gnu_note (Filedata * filedata, Elf_Internal_Note *pnote)
22262 {
22263 /* NB/ Keep this switch statement in sync with get_gnu_elf_note_type (). */
22264 switch (pnote->type)
22265 {
22266 case NT_GNU_BUILD_ID:
22267 {
22268 size_t i;
22269
22270 printf (_(" Build ID: "));
22271 for (i = 0; i < pnote->descsz; ++i)
22272 printf ("%02x", pnote->descdata[i] & 0xff);
22273 printf ("\n");
22274 }
22275 break;
22276
22277 case NT_GNU_ABI_TAG:
22278 {
22279 unsigned int os, major, minor, subminor;
22280 const char *osname;
22281
22282 /* PR 17531: file: 030-599401-0.004. */
22283 if (pnote->descsz < 16)
22284 {
22285 printf (_(" <corrupt GNU_ABI_TAG>\n"));
22286 break;
22287 }
22288
22289 os = byte_get ((unsigned char *) pnote->descdata, 4);
22290 major = byte_get ((unsigned char *) pnote->descdata + 4, 4);
22291 minor = byte_get ((unsigned char *) pnote->descdata + 8, 4);
22292 subminor = byte_get ((unsigned char *) pnote->descdata + 12, 4);
22293
22294 switch (os)
22295 {
22296 case GNU_ABI_TAG_LINUX:
22297 osname = "Linux";
22298 break;
22299 case GNU_ABI_TAG_HURD:
22300 osname = "Hurd";
22301 break;
22302 case GNU_ABI_TAG_SOLARIS:
22303 osname = "Solaris";
22304 break;
22305 case GNU_ABI_TAG_FREEBSD:
22306 osname = "FreeBSD";
22307 break;
22308 case GNU_ABI_TAG_NETBSD:
22309 osname = "NetBSD";
22310 break;
22311 case GNU_ABI_TAG_SYLLABLE:
22312 osname = "Syllable";
22313 break;
22314 case GNU_ABI_TAG_NACL:
22315 osname = "NaCl";
22316 break;
22317 default:
22318 osname = "Unknown";
22319 break;
22320 }
22321
22322 printf (_(" OS: %s, ABI: %d.%d.%d\n"), osname,
22323 major, minor, subminor);
22324 }
22325 break;
22326
22327 case NT_GNU_GOLD_VERSION:
22328 {
22329 size_t i;
22330
22331 printf (_(" Version: "));
22332 for (i = 0; i < pnote->descsz && pnote->descdata[i] != '\0'; ++i)
22333 printf ("%c", pnote->descdata[i]);
22334 printf ("\n");
22335 }
22336 break;
22337
22338 case NT_GNU_HWCAP:
22339 {
22340 unsigned int num_entries, mask;
22341
22342 /* Hardware capabilities information. Word 0 is the number of entries.
22343 Word 1 is a bitmask of enabled entries. The rest of the descriptor
22344 is a series of entries, where each entry is a single byte followed
22345 by a nul terminated string. The byte gives the bit number to test
22346 if enabled in the bitmask. */
22347 printf (_(" Hardware Capabilities: "));
22348 if (pnote->descsz < 8)
22349 {
22350 error (_("<corrupt GNU_HWCAP>\n"));
22351 return false;
22352 }
22353 num_entries = byte_get ((unsigned char *) pnote->descdata, 4);
22354 mask = byte_get ((unsigned char *) pnote->descdata + 4, 4);
22355 printf (_("num entries: %d, enabled mask: %x\n"), num_entries, mask);
22356 /* FIXME: Add code to display the entries... */
22357 }
22358 break;
22359
22360 case NT_GNU_PROPERTY_TYPE_0:
22361 print_gnu_property_note (filedata, pnote);
22362 break;
22363
22364 default:
22365 /* Handle unrecognised types. An error message should have already been
22366 created by get_gnu_elf_note_type(), so all that we need to do is to
22367 display the data. */
22368 {
22369 size_t i;
22370
22371 printf (_(" Description data: "));
22372 for (i = 0; i < pnote->descsz; ++i)
22373 printf ("%02x ", pnote->descdata[i] & 0xff);
22374 printf ("\n");
22375 }
22376 break;
22377 }
22378
22379 return true;
22380 }
22381
22382 static const char *
22383 get_v850_elf_note_type (enum v850_notes n_type)
22384 {
22385 static char buff[64];
22386
22387 switch (n_type)
22388 {
22389 case V850_NOTE_ALIGNMENT: return _("Alignment of 8-byte objects");
22390 case V850_NOTE_DATA_SIZE: return _("Sizeof double and long double");
22391 case V850_NOTE_FPU_INFO: return _("Type of FPU support needed");
22392 case V850_NOTE_SIMD_INFO: return _("Use of SIMD instructions");
22393 case V850_NOTE_CACHE_INFO: return _("Use of cache");
22394 case V850_NOTE_MMU_INFO: return _("Use of MMU");
22395 default:
22396 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), n_type);
22397 return buff;
22398 }
22399 }
22400
22401 static bool
22402 print_v850_note (Elf_Internal_Note * pnote)
22403 {
22404 unsigned int val;
22405
22406 printf (" %s: ", get_v850_elf_note_type (pnote->type));
22407
22408 if (pnote->descsz != 4)
22409 {
22410 printf ("<corrupt descsz: %#lx>\n", pnote->descsz);
22411 return false;
22412 }
22413
22414 val = byte_get ((unsigned char *) pnote->descdata, pnote->descsz);
22415
22416 if (val == 0)
22417 {
22418 printf (_("not set\n"));
22419 return true;
22420 }
22421
22422 switch (pnote->type)
22423 {
22424 case V850_NOTE_ALIGNMENT:
22425 switch (val)
22426 {
22427 case EF_RH850_DATA_ALIGN4: printf (_("4-byte\n")); return true;
22428 case EF_RH850_DATA_ALIGN8: printf (_("8-byte\n")); return true;
22429 }
22430 break;
22431
22432 case V850_NOTE_DATA_SIZE:
22433 switch (val)
22434 {
22435 case EF_RH850_DOUBLE32: printf (_("4-bytes\n")); return true;
22436 case EF_RH850_DOUBLE64: printf (_("8-bytes\n")); return true;
22437 }
22438 break;
22439
22440 case V850_NOTE_FPU_INFO:
22441 switch (val)
22442 {
22443 case EF_RH850_FPU20: printf (_("FPU-2.0\n")); return true;
22444 case EF_RH850_FPU30: printf (_("FPU-3.0\n")); return true;
22445 }
22446 break;
22447
22448 case V850_NOTE_MMU_INFO:
22449 case V850_NOTE_CACHE_INFO:
22450 case V850_NOTE_SIMD_INFO:
22451 if (val == EF_RH850_SIMD)
22452 {
22453 printf (_("yes\n"));
22454 return true;
22455 }
22456 break;
22457
22458 default:
22459 /* An 'unknown note type' message will already have been displayed. */
22460 break;
22461 }
22462
22463 printf (_("unknown value: %x\n"), val);
22464 return false;
22465 }
22466
22467 static bool
22468 process_netbsd_elf_note (Elf_Internal_Note * pnote)
22469 {
22470 unsigned int version;
22471
22472 switch (pnote->type)
22473 {
22474 case NT_NETBSD_IDENT:
22475 if (pnote->descsz < 1)
22476 break;
22477 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
22478 if ((version / 10000) % 100)
22479 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u%s%c)\n", pnote->descsz,
22480 version, version / 100000000, (version / 1000000) % 100,
22481 (version / 10000) % 100 > 26 ? "Z" : "",
22482 'A' + (version / 10000) % 26);
22483 else
22484 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u.%u)\n", pnote->descsz,
22485 version, version / 100000000, (version / 1000000) % 100,
22486 (version / 100) % 100);
22487 return true;
22488
22489 case NT_NETBSD_MARCH:
22490 printf (" NetBSD\t\t0x%08lx\tMARCH <%s>\n", pnote->descsz,
22491 pnote->descdata);
22492 return true;
22493
22494 case NT_NETBSD_PAX:
22495 if (pnote->descsz < 1)
22496 break;
22497 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
22498 printf (" NetBSD\t\t0x%08lx\tPaX <%s%s%s%s%s%s>\n", pnote->descsz,
22499 ((version & NT_NETBSD_PAX_MPROTECT) ? "+mprotect" : ""),
22500 ((version & NT_NETBSD_PAX_NOMPROTECT) ? "-mprotect" : ""),
22501 ((version & NT_NETBSD_PAX_GUARD) ? "+guard" : ""),
22502 ((version & NT_NETBSD_PAX_NOGUARD) ? "-guard" : ""),
22503 ((version & NT_NETBSD_PAX_ASLR) ? "+ASLR" : ""),
22504 ((version & NT_NETBSD_PAX_NOASLR) ? "-ASLR" : ""));
22505 return true;
22506 }
22507
22508 printf (" NetBSD\t0x%08lx\tUnknown note type: (0x%08lx)\n",
22509 pnote->descsz, pnote->type);
22510 return false;
22511 }
22512
22513 static const char *
22514 get_freebsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
22515 {
22516 switch (e_type)
22517 {
22518 case NT_FREEBSD_THRMISC:
22519 return _("NT_THRMISC (thrmisc structure)");
22520 case NT_FREEBSD_PROCSTAT_PROC:
22521 return _("NT_PROCSTAT_PROC (proc data)");
22522 case NT_FREEBSD_PROCSTAT_FILES:
22523 return _("NT_PROCSTAT_FILES (files data)");
22524 case NT_FREEBSD_PROCSTAT_VMMAP:
22525 return _("NT_PROCSTAT_VMMAP (vmmap data)");
22526 case NT_FREEBSD_PROCSTAT_GROUPS:
22527 return _("NT_PROCSTAT_GROUPS (groups data)");
22528 case NT_FREEBSD_PROCSTAT_UMASK:
22529 return _("NT_PROCSTAT_UMASK (umask data)");
22530 case NT_FREEBSD_PROCSTAT_RLIMIT:
22531 return _("NT_PROCSTAT_RLIMIT (rlimit data)");
22532 case NT_FREEBSD_PROCSTAT_OSREL:
22533 return _("NT_PROCSTAT_OSREL (osreldate data)");
22534 case NT_FREEBSD_PROCSTAT_PSSTRINGS:
22535 return _("NT_PROCSTAT_PSSTRINGS (ps_strings data)");
22536 case NT_FREEBSD_PROCSTAT_AUXV:
22537 return _("NT_PROCSTAT_AUXV (auxv data)");
22538 case NT_FREEBSD_PTLWPINFO:
22539 return _("NT_PTLWPINFO (ptrace_lwpinfo structure)");
22540 case NT_FREEBSD_X86_SEGBASES:
22541 return _("NT_X86_SEGBASES (x86 segment base registers)");
22542 }
22543 return get_note_type (filedata, e_type);
22544 }
22545
22546 static const char *
22547 get_netbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
22548 {
22549 static char buff[64];
22550
22551 switch (e_type)
22552 {
22553 case NT_NETBSDCORE_PROCINFO:
22554 /* NetBSD core "procinfo" structure. */
22555 return _("NetBSD procinfo structure");
22556
22557 case NT_NETBSDCORE_AUXV:
22558 return _("NetBSD ELF auxiliary vector data");
22559
22560 case NT_NETBSDCORE_LWPSTATUS:
22561 return _("PT_LWPSTATUS (ptrace_lwpstatus structure)");
22562
22563 default:
22564 /* As of Jan 2020 there are no other machine-independent notes
22565 defined for NetBSD core files. If the note type is less
22566 than the start of the machine-dependent note types, we don't
22567 understand it. */
22568
22569 if (e_type < NT_NETBSDCORE_FIRSTMACH)
22570 {
22571 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
22572 return buff;
22573 }
22574 break;
22575 }
22576
22577 switch (filedata->file_header.e_machine)
22578 {
22579 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
22580 and PT_GETFPREGS == mach+2. */
22581
22582 case EM_OLD_ALPHA:
22583 case EM_ALPHA:
22584 case EM_SPARC:
22585 case EM_SPARC32PLUS:
22586 case EM_SPARCV9:
22587 switch (e_type)
22588 {
22589 case NT_NETBSDCORE_FIRSTMACH + 0:
22590 return _("PT_GETREGS (reg structure)");
22591 case NT_NETBSDCORE_FIRSTMACH + 2:
22592 return _("PT_GETFPREGS (fpreg structure)");
22593 default:
22594 break;
22595 }
22596 break;
22597
22598 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
22599 There's also old PT___GETREGS40 == mach + 1 for old reg
22600 structure which lacks GBR. */
22601 case EM_SH:
22602 switch (e_type)
22603 {
22604 case NT_NETBSDCORE_FIRSTMACH + 1:
22605 return _("PT___GETREGS40 (old reg structure)");
22606 case NT_NETBSDCORE_FIRSTMACH + 3:
22607 return _("PT_GETREGS (reg structure)");
22608 case NT_NETBSDCORE_FIRSTMACH + 5:
22609 return _("PT_GETFPREGS (fpreg structure)");
22610 default:
22611 break;
22612 }
22613 break;
22614
22615 /* On all other arch's, PT_GETREGS == mach+1 and
22616 PT_GETFPREGS == mach+3. */
22617 default:
22618 switch (e_type)
22619 {
22620 case NT_NETBSDCORE_FIRSTMACH + 1:
22621 return _("PT_GETREGS (reg structure)");
22622 case NT_NETBSDCORE_FIRSTMACH + 3:
22623 return _("PT_GETFPREGS (fpreg structure)");
22624 default:
22625 break;
22626 }
22627 }
22628
22629 snprintf (buff, sizeof (buff), "PT_FIRSTMACH+%d",
22630 e_type - NT_NETBSDCORE_FIRSTMACH);
22631 return buff;
22632 }
22633
22634 static const char *
22635 get_openbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
22636 {
22637 switch (e_type)
22638 {
22639 case NT_OPENBSD_PROCINFO:
22640 return _("OpenBSD procinfo structure");
22641 case NT_OPENBSD_AUXV:
22642 return _("OpenBSD ELF auxiliary vector data");
22643 case NT_OPENBSD_REGS:
22644 return _("OpenBSD regular registers");
22645 case NT_OPENBSD_FPREGS:
22646 return _("OpenBSD floating point registers");
22647 case NT_OPENBSD_WCOOKIE:
22648 return _("OpenBSD window cookie");
22649 }
22650
22651 return get_note_type (filedata, e_type);
22652 }
22653
22654 static const char *
22655 get_qnx_elfcore_note_type (Filedata * filedata, unsigned e_type)
22656 {
22657 switch (e_type)
22658 {
22659 case QNT_DEBUG_FULLPATH:
22660 return _("QNX debug fullpath");
22661 case QNT_DEBUG_RELOC:
22662 return _("QNX debug relocation");
22663 case QNT_STACK:
22664 return _("QNX stack");
22665 case QNT_GENERATOR:
22666 return _("QNX generator");
22667 case QNT_DEFAULT_LIB:
22668 return _("QNX default library");
22669 case QNT_CORE_SYSINFO:
22670 return _("QNX core sysinfo");
22671 case QNT_CORE_INFO:
22672 return _("QNX core info");
22673 case QNT_CORE_STATUS:
22674 return _("QNX core status");
22675 case QNT_CORE_GREG:
22676 return _("QNX general registers");
22677 case QNT_CORE_FPREG:
22678 return _("QNX floating point registers");
22679 case QNT_LINK_MAP:
22680 return _("QNX link map");
22681 }
22682
22683 return get_note_type (filedata, e_type);
22684 }
22685
22686 static const char *
22687 get_stapsdt_note_type (unsigned e_type)
22688 {
22689 static char buff[64];
22690
22691 switch (e_type)
22692 {
22693 case NT_STAPSDT:
22694 return _("NT_STAPSDT (SystemTap probe descriptors)");
22695
22696 default:
22697 break;
22698 }
22699
22700 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
22701 return buff;
22702 }
22703
22704 static bool
22705 print_stapsdt_note (Elf_Internal_Note *pnote)
22706 {
22707 size_t len, maxlen;
22708 size_t addr_size = is_32bit_elf ? 4 : 8;
22709 char *data = pnote->descdata;
22710 char *data_end = pnote->descdata + pnote->descsz;
22711 uint64_t pc, base_addr, semaphore;
22712 char *provider, *probe, *arg_fmt;
22713
22714 if (pnote->descsz < (addr_size * 3))
22715 goto stapdt_note_too_small;
22716
22717 pc = byte_get ((unsigned char *) data, addr_size);
22718 data += addr_size;
22719
22720 base_addr = byte_get ((unsigned char *) data, addr_size);
22721 data += addr_size;
22722
22723 semaphore = byte_get ((unsigned char *) data, addr_size);
22724 data += addr_size;
22725
22726 if (data >= data_end)
22727 goto stapdt_note_too_small;
22728 maxlen = data_end - data;
22729 len = strnlen (data, maxlen);
22730 if (len < maxlen)
22731 {
22732 provider = data;
22733 data += len + 1;
22734 }
22735 else
22736 goto stapdt_note_too_small;
22737
22738 if (data >= data_end)
22739 goto stapdt_note_too_small;
22740 maxlen = data_end - data;
22741 len = strnlen (data, maxlen);
22742 if (len < maxlen)
22743 {
22744 probe = data;
22745 data += len + 1;
22746 }
22747 else
22748 goto stapdt_note_too_small;
22749
22750 if (data >= data_end)
22751 goto stapdt_note_too_small;
22752 maxlen = data_end - data;
22753 len = strnlen (data, maxlen);
22754 if (len < maxlen)
22755 {
22756 arg_fmt = data;
22757 data += len + 1;
22758 }
22759 else
22760 goto stapdt_note_too_small;
22761
22762 printf (_(" Provider: %s\n"), provider);
22763 printf (_(" Name: %s\n"), probe);
22764 printf (_(" Location: "));
22765 print_vma (pc, FULL_HEX);
22766 printf (_(", Base: "));
22767 print_vma (base_addr, FULL_HEX);
22768 printf (_(", Semaphore: "));
22769 print_vma (semaphore, FULL_HEX);
22770 printf ("\n");
22771 printf (_(" Arguments: %s\n"), arg_fmt);
22772
22773 return data == data_end;
22774
22775 stapdt_note_too_small:
22776 printf (_(" <corrupt - note is too small>\n"));
22777 error (_("corrupt stapdt note - the data size is too small\n"));
22778 return false;
22779 }
22780
22781 static bool
22782 print_fdo_note (Elf_Internal_Note * pnote)
22783 {
22784 if (pnote->descsz > 0 && pnote->type == FDO_PACKAGING_METADATA)
22785 {
22786 printf (_(" Packaging Metadata: %.*s\n"), (int) pnote->descsz, pnote->descdata);
22787 return true;
22788 }
22789 if (pnote->descsz > 0 && pnote->type == FDO_DLOPEN_METADATA)
22790 {
22791 printf (_(" Dlopen Metadata: %.*s\n"), (int) pnote->descsz, pnote->descdata);
22792 return true;
22793 }
22794 return false;
22795 }
22796
22797 static const char *
22798 get_ia64_vms_note_type (unsigned e_type)
22799 {
22800 static char buff[64];
22801
22802 switch (e_type)
22803 {
22804 case NT_VMS_MHD:
22805 return _("NT_VMS_MHD (module header)");
22806 case NT_VMS_LNM:
22807 return _("NT_VMS_LNM (language name)");
22808 case NT_VMS_SRC:
22809 return _("NT_VMS_SRC (source files)");
22810 case NT_VMS_TITLE:
22811 return "NT_VMS_TITLE";
22812 case NT_VMS_EIDC:
22813 return _("NT_VMS_EIDC (consistency check)");
22814 case NT_VMS_FPMODE:
22815 return _("NT_VMS_FPMODE (FP mode)");
22816 case NT_VMS_LINKTIME:
22817 return "NT_VMS_LINKTIME";
22818 case NT_VMS_IMGNAM:
22819 return _("NT_VMS_IMGNAM (image name)");
22820 case NT_VMS_IMGID:
22821 return _("NT_VMS_IMGID (image id)");
22822 case NT_VMS_LINKID:
22823 return _("NT_VMS_LINKID (link id)");
22824 case NT_VMS_IMGBID:
22825 return _("NT_VMS_IMGBID (build id)");
22826 case NT_VMS_GSTNAM:
22827 return _("NT_VMS_GSTNAM (sym table name)");
22828 case NT_VMS_ORIG_DYN:
22829 return "NT_VMS_ORIG_DYN";
22830 case NT_VMS_PATCHTIME:
22831 return "NT_VMS_PATCHTIME";
22832 default:
22833 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
22834 return buff;
22835 }
22836 }
22837
22838 static bool
22839 print_ia64_vms_note (Elf_Internal_Note * pnote)
22840 {
22841 unsigned int maxlen = pnote->descsz;
22842
22843 if (maxlen < 2 || maxlen != pnote->descsz)
22844 goto desc_size_fail;
22845
22846 switch (pnote->type)
22847 {
22848 case NT_VMS_MHD:
22849 if (maxlen <= 36)
22850 goto desc_size_fail;
22851
22852 size_t l = strnlen (pnote->descdata + 34, maxlen - 34);
22853
22854 printf (_(" Creation date : %.17s\n"), pnote->descdata);
22855 printf (_(" Last patch date: %.17s\n"), pnote->descdata + 17);
22856 if (l + 34 < maxlen)
22857 {
22858 printf (_(" Module name : %s\n"), pnote->descdata + 34);
22859 if (l + 35 < maxlen)
22860 printf (_(" Module version : %s\n"), pnote->descdata + 34 + l + 1);
22861 else
22862 printf (_(" Module version : <missing>\n"));
22863 }
22864 else
22865 {
22866 printf (_(" Module name : <missing>\n"));
22867 printf (_(" Module version : <missing>\n"));
22868 }
22869 break;
22870
22871 case NT_VMS_LNM:
22872 printf (_(" Language: %.*s\n"), maxlen, pnote->descdata);
22873 break;
22874
22875 case NT_VMS_FPMODE:
22876 printf (_(" Floating Point mode: "));
22877 if (maxlen < 8)
22878 goto desc_size_fail;
22879 /* FIXME: Generate an error if descsz > 8 ? */
22880
22881 printf ("0x%016" PRIx64 "\n",
22882 byte_get ((unsigned char *) pnote->descdata, 8));
22883 break;
22884
22885 case NT_VMS_LINKTIME:
22886 printf (_(" Link time: "));
22887 if (maxlen < 8)
22888 goto desc_size_fail;
22889 /* FIXME: Generate an error if descsz > 8 ? */
22890
22891 print_vms_time (byte_get ((unsigned char *) pnote->descdata, 8));
22892 printf ("\n");
22893 break;
22894
22895 case NT_VMS_PATCHTIME:
22896 printf (_(" Patch time: "));
22897 if (maxlen < 8)
22898 goto desc_size_fail;
22899 /* FIXME: Generate an error if descsz > 8 ? */
22900
22901 print_vms_time (byte_get ((unsigned char *) pnote->descdata, 8));
22902 printf ("\n");
22903 break;
22904
22905 case NT_VMS_ORIG_DYN:
22906 if (maxlen < 34)
22907 goto desc_size_fail;
22908
22909 printf (_(" Major id: %u, minor id: %u\n"),
22910 (unsigned) byte_get ((unsigned char *) pnote->descdata, 4),
22911 (unsigned) byte_get ((unsigned char *) pnote->descdata + 4, 4));
22912 printf (_(" Last modified : "));
22913 print_vms_time (byte_get ((unsigned char *) pnote->descdata + 8, 8));
22914 printf (_("\n Link flags : "));
22915 printf ("0x%016" PRIx64 "\n",
22916 byte_get ((unsigned char *) pnote->descdata + 16, 8));
22917 printf (_(" Header flags: 0x%08x\n"),
22918 (unsigned) byte_get ((unsigned char *) pnote->descdata + 24, 4));
22919 printf (_(" Image id : %.*s\n"), maxlen - 32, pnote->descdata + 32);
22920 break;
22921
22922 case NT_VMS_IMGNAM:
22923 printf (_(" Image name: %.*s\n"), maxlen, pnote->descdata);
22924 break;
22925
22926 case NT_VMS_GSTNAM:
22927 printf (_(" Global symbol table name: %.*s\n"), maxlen, pnote->descdata);
22928 break;
22929
22930 case NT_VMS_IMGID:
22931 printf (_(" Image id: %.*s\n"), maxlen, pnote->descdata);
22932 break;
22933
22934 case NT_VMS_LINKID:
22935 printf (_(" Linker id: %.*s\n"), maxlen, pnote->descdata);
22936 break;
22937
22938 default:
22939 return false;
22940 }
22941
22942 return true;
22943
22944 desc_size_fail:
22945 printf (_(" <corrupt - data size is too small>\n"));
22946 error (_("corrupt IA64 note: data size is too small\n"));
22947 return false;
22948 }
22949
22950 struct build_attr_cache {
22951 Filedata *filedata;
22952 char *strtab;
22953 uint64_t strtablen;
22954 Elf_Internal_Sym *symtab;
22955 uint64_t nsyms;
22956 } ba_cache;
22957
22958 /* Find the symbol associated with a build attribute that is attached
22959 to address OFFSET. If PNAME is non-NULL then store the name of
22960 the symbol (if found) in the provided pointer, Returns NULL if a
22961 symbol could not be found. */
22962
22963 static Elf_Internal_Sym *
22964 get_symbol_for_build_attribute (Filedata *filedata,
22965 uint64_t offset,
22966 bool is_open_attr,
22967 const char **pname)
22968 {
22969 Elf_Internal_Sym *saved_sym = NULL;
22970 Elf_Internal_Sym *sym;
22971
22972 if (filedata->section_headers != NULL
22973 && (ba_cache.filedata == NULL || filedata != ba_cache.filedata))
22974 {
22975 Elf_Internal_Shdr * symsec;
22976
22977 free (ba_cache.strtab);
22978 ba_cache.strtab = NULL;
22979 free (ba_cache.symtab);
22980 ba_cache.symtab = NULL;
22981
22982 /* Load the symbol and string sections. */
22983 for (symsec = filedata->section_headers;
22984 symsec < filedata->section_headers + filedata->file_header.e_shnum;
22985 symsec ++)
22986 {
22987 if (symsec->sh_type == SHT_SYMTAB
22988 && get_symtab (filedata, symsec,
22989 &ba_cache.symtab, &ba_cache.nsyms,
22990 &ba_cache.strtab, &ba_cache.strtablen))
22991 break;
22992 }
22993 ba_cache.filedata = filedata;
22994 }
22995
22996 if (ba_cache.symtab == NULL)
22997 return NULL;
22998
22999 /* Find a symbol whose value matches offset. */
23000 for (sym = ba_cache.symtab; sym < ba_cache.symtab + ba_cache.nsyms; sym ++)
23001 if (sym->st_value == offset)
23002 {
23003 if (sym->st_name >= ba_cache.strtablen)
23004 /* Huh ? This should not happen. */
23005 continue;
23006
23007 if (ba_cache.strtab[sym->st_name] == 0)
23008 continue;
23009
23010 /* The AArch64, ARM and RISC-V architectures define mapping symbols
23011 (eg $d, $x, $t) which we want to ignore. */
23012 if (ba_cache.strtab[sym->st_name] == '$'
23013 && ba_cache.strtab[sym->st_name + 1] != 0
23014 && ba_cache.strtab[sym->st_name + 2] == 0)
23015 continue;
23016
23017 if (is_open_attr)
23018 {
23019 /* For OPEN attributes we prefer GLOBAL over LOCAL symbols
23020 and FILE or OBJECT symbols over NOTYPE symbols. We skip
23021 FUNC symbols entirely. */
23022 switch (ELF_ST_TYPE (sym->st_info))
23023 {
23024 case STT_OBJECT:
23025 case STT_FILE:
23026 saved_sym = sym;
23027 if (sym->st_size)
23028 {
23029 /* If the symbol has a size associated
23030 with it then we can stop searching. */
23031 sym = ba_cache.symtab + ba_cache.nsyms;
23032 }
23033 continue;
23034
23035 case STT_FUNC:
23036 /* Ignore function symbols. */
23037 continue;
23038
23039 default:
23040 break;
23041 }
23042
23043 switch (ELF_ST_BIND (sym->st_info))
23044 {
23045 case STB_GLOBAL:
23046 if (saved_sym == NULL
23047 || ELF_ST_TYPE (saved_sym->st_info) != STT_OBJECT)
23048 saved_sym = sym;
23049 break;
23050
23051 case STB_LOCAL:
23052 if (saved_sym == NULL)
23053 saved_sym = sym;
23054 break;
23055
23056 default:
23057 break;
23058 }
23059 }
23060 else
23061 {
23062 if (ELF_ST_TYPE (sym->st_info) != STT_FUNC)
23063 continue;
23064
23065 saved_sym = sym;
23066 break;
23067 }
23068 }
23069
23070 if (saved_sym && pname)
23071 * pname = ba_cache.strtab + saved_sym->st_name;
23072
23073 return saved_sym;
23074 }
23075
23076 /* Returns true iff addr1 and addr2 are in the same section. */
23077
23078 static bool
23079 same_section (Filedata * filedata, uint64_t addr1, uint64_t addr2)
23080 {
23081 Elf_Internal_Shdr * a1;
23082 Elf_Internal_Shdr * a2;
23083
23084 a1 = find_section_by_address (filedata, addr1);
23085 a2 = find_section_by_address (filedata, addr2);
23086
23087 return a1 == a2 && a1 != NULL;
23088 }
23089
23090 static bool
23091 print_gnu_build_attribute_description (Elf_Internal_Note * pnote,
23092 Filedata * filedata)
23093 {
23094 static uint64_t global_offset = 0;
23095 static uint64_t global_end = 0;
23096 static uint64_t func_offset = 0;
23097 static uint64_t func_end = 0;
23098
23099 Elf_Internal_Sym *sym;
23100 const char *name;
23101 uint64_t start;
23102 uint64_t end;
23103 bool is_open_attr = pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN;
23104
23105 switch (pnote->descsz)
23106 {
23107 case 0:
23108 /* A zero-length description means that the range of
23109 the previous note of the same type should be used. */
23110 if (is_open_attr)
23111 {
23112 if (global_end > global_offset)
23113 printf (_(" Applies to region from %#" PRIx64
23114 " to %#" PRIx64 "\n"), global_offset, global_end);
23115 else
23116 printf (_(" Applies to region from %#" PRIx64
23117 "\n"), global_offset);
23118 }
23119 else
23120 {
23121 if (func_end > func_offset)
23122 printf (_(" Applies to region from %#" PRIx64
23123 " to %#" PRIx64 "\n"), func_offset, func_end);
23124 else
23125 printf (_(" Applies to region from %#" PRIx64
23126 "\n"), func_offset);
23127 }
23128 return true;
23129
23130 case 4:
23131 start = byte_get ((unsigned char *) pnote->descdata, 4);
23132 end = 0;
23133 break;
23134
23135 case 8:
23136 start = byte_get ((unsigned char *) pnote->descdata, 4);
23137 end = byte_get ((unsigned char *) pnote->descdata + 4, 4);
23138 break;
23139
23140 case 16:
23141 start = byte_get ((unsigned char *) pnote->descdata, 8);
23142 end = byte_get ((unsigned char *) pnote->descdata + 8, 8);
23143 break;
23144
23145 default:
23146 error (_(" <invalid description size: %lx>\n"), pnote->descsz);
23147 printf (_(" <invalid descsz>"));
23148 return false;
23149 }
23150
23151 name = NULL;
23152 sym = get_symbol_for_build_attribute (filedata, start, is_open_attr, & name);
23153 /* As of version 5 of the annobin plugin, filename symbols are biased by 2
23154 in order to avoid them being confused with the start address of the
23155 first function in the file... */
23156 if (sym == NULL && is_open_attr)
23157 sym = get_symbol_for_build_attribute (filedata, start + 2, is_open_attr,
23158 & name);
23159
23160 if (end == 0 && sym != NULL && sym->st_size > 0)
23161 end = start + sym->st_size;
23162
23163 if (is_open_attr)
23164 {
23165 /* FIXME: Need to properly allow for section alignment.
23166 16 is just the alignment used on x86_64. */
23167 if (global_end > 0
23168 && start > BFD_ALIGN (global_end, 16)
23169 /* Build notes are not guaranteed to be organised in order of
23170 increasing address, but we should find the all of the notes
23171 for one section in the same place. */
23172 && same_section (filedata, start, global_end))
23173 warn (_("Gap in build notes detected from %#" PRIx64
23174 " to %#" PRIx64 "\n"),
23175 global_end + 1, start - 1);
23176
23177 printf (_(" Applies to region from %#" PRIx64), start);
23178 global_offset = start;
23179
23180 if (end)
23181 {
23182 printf (_(" to %#" PRIx64), end);
23183 global_end = end;
23184 }
23185 }
23186 else
23187 {
23188 printf (_(" Applies to region from %#" PRIx64), start);
23189 func_offset = start;
23190
23191 if (end)
23192 {
23193 printf (_(" to %#" PRIx64), end);
23194 func_end = end;
23195 }
23196 }
23197
23198 if (sym && name)
23199 printf (_(" (%s)"), name);
23200
23201 printf ("\n");
23202 return true;
23203 }
23204
23205 static bool
23206 print_gnu_build_attribute_name (Elf_Internal_Note * pnote)
23207 {
23208 static const char string_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_STRING, 0 };
23209 static const char number_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC, 0 };
23210 static const char bool_expected [3] = { GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE, GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE, 0 };
23211 char name_type;
23212 char name_attribute;
23213 const char * expected_types;
23214 const char * name = pnote->namedata;
23215 const char * text;
23216 signed int left;
23217
23218 if (name == NULL || pnote->namesz < 2)
23219 {
23220 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
23221 print_symbol_name (-20, _(" <corrupt name>"));
23222 return false;
23223 }
23224
23225 if (do_wide)
23226 left = 28;
23227 else
23228 left = 20;
23229
23230 /* Version 2 of the spec adds a "GA" prefix to the name field. */
23231 if (name[0] == 'G' && name[1] == 'A')
23232 {
23233 if (pnote->namesz < 4)
23234 {
23235 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
23236 print_symbol_name (-20, _(" <corrupt name>"));
23237 return false;
23238 }
23239
23240 printf ("GA");
23241 name += 2;
23242 left -= 2;
23243 }
23244
23245 switch ((name_type = * name))
23246 {
23247 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
23248 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
23249 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
23250 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
23251 printf ("%c", * name);
23252 left --;
23253 break;
23254 default:
23255 error (_("unrecognised attribute type in name field: %d\n"), name_type);
23256 print_symbol_name (-20, _("<unknown name type>"));
23257 return false;
23258 }
23259
23260 ++ name;
23261 text = NULL;
23262
23263 switch ((name_attribute = * name))
23264 {
23265 case GNU_BUILD_ATTRIBUTE_VERSION:
23266 text = _("<version>");
23267 expected_types = string_expected;
23268 ++ name;
23269 break;
23270 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
23271 text = _("<stack prot>");
23272 expected_types = "!+*";
23273 ++ name;
23274 break;
23275 case GNU_BUILD_ATTRIBUTE_RELRO:
23276 text = _("<relro>");
23277 expected_types = bool_expected;
23278 ++ name;
23279 break;
23280 case GNU_BUILD_ATTRIBUTE_STACK_SIZE:
23281 text = _("<stack size>");
23282 expected_types = number_expected;
23283 ++ name;
23284 break;
23285 case GNU_BUILD_ATTRIBUTE_TOOL:
23286 text = _("<tool>");
23287 expected_types = string_expected;
23288 ++ name;
23289 break;
23290 case GNU_BUILD_ATTRIBUTE_ABI:
23291 text = _("<ABI>");
23292 expected_types = "$*";
23293 ++ name;
23294 break;
23295 case GNU_BUILD_ATTRIBUTE_PIC:
23296 text = _("<PIC>");
23297 expected_types = number_expected;
23298 ++ name;
23299 break;
23300 case GNU_BUILD_ATTRIBUTE_SHORT_ENUM:
23301 text = _("<short enum>");
23302 expected_types = bool_expected;
23303 ++ name;
23304 break;
23305 default:
23306 if (ISPRINT (* name))
23307 {
23308 int len = strnlen (name, pnote->namesz - (name - pnote->namedata)) + 1;
23309
23310 if (len > left && ! do_wide)
23311 len = left;
23312 printf ("%.*s:", len, name);
23313 left -= len;
23314 name += len;
23315 }
23316 else
23317 {
23318 static char tmpbuf [128];
23319
23320 error (_("unrecognised byte in name field: %d\n"), * name);
23321 sprintf (tmpbuf, _("<unknown:_%d>"), * name);
23322 text = tmpbuf;
23323 name ++;
23324 }
23325 expected_types = "*$!+";
23326 break;
23327 }
23328
23329 if (text)
23330 left -= printf ("%s", text);
23331
23332 if (strchr (expected_types, name_type) == NULL)
23333 warn (_("attribute does not have an expected type (%c)\n"), name_type);
23334
23335 if ((size_t) (name - pnote->namedata) > pnote->namesz)
23336 {
23337 error (_("corrupt name field: namesz: %lu but parsing gets to %td\n"),
23338 pnote->namesz,
23339 name - pnote->namedata);
23340 return false;
23341 }
23342
23343 if (left < 1 && ! do_wide)
23344 return true;
23345
23346 switch (name_type)
23347 {
23348 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
23349 {
23350 unsigned int bytes;
23351 uint64_t val = 0;
23352 unsigned int shift = 0;
23353 char *decoded = NULL;
23354
23355 bytes = pnote->namesz - (name - pnote->namedata);
23356 if (bytes > 0)
23357 /* The -1 is because the name field is always 0 terminated, and we
23358 want to be able to ensure that the shift in the while loop below
23359 will not overflow. */
23360 -- bytes;
23361
23362 if (bytes > sizeof (val))
23363 {
23364 error (_("corrupt numeric name field: too many bytes in the value: %x\n"),
23365 bytes);
23366 bytes = sizeof (val);
23367 }
23368 /* We do not bother to warn if bytes == 0 as this can
23369 happen with some early versions of the gcc plugin. */
23370
23371 while (bytes --)
23372 {
23373 uint64_t byte = *name++ & 0xff;
23374
23375 val |= byte << shift;
23376 shift += 8;
23377 }
23378
23379 switch (name_attribute)
23380 {
23381 case GNU_BUILD_ATTRIBUTE_PIC:
23382 switch (val)
23383 {
23384 case 0: decoded = "static"; break;
23385 case 1: decoded = "pic"; break;
23386 case 2: decoded = "PIC"; break;
23387 case 3: decoded = "pie"; break;
23388 case 4: decoded = "PIE"; break;
23389 default: break;
23390 }
23391 break;
23392 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
23393 switch (val)
23394 {
23395 /* Based upon the SPCT_FLAG_xxx enum values in gcc/cfgexpand.c. */
23396 case 0: decoded = "off"; break;
23397 case 1: decoded = "on"; break;
23398 case 2: decoded = "all"; break;
23399 case 3: decoded = "strong"; break;
23400 case 4: decoded = "explicit"; break;
23401 default: break;
23402 }
23403 break;
23404 default:
23405 break;
23406 }
23407
23408 if (decoded != NULL)
23409 {
23410 print_symbol_name (-left, decoded);
23411 left = 0;
23412 }
23413 else if (val == 0)
23414 {
23415 printf ("0x0");
23416 left -= 3;
23417 }
23418 else
23419 {
23420 if (do_wide)
23421 left -= printf ("0x%" PRIx64, val);
23422 else
23423 left -= printf ("0x%-.*" PRIx64, left, val);
23424 }
23425 }
23426 break;
23427 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
23428 left -= print_symbol_name (- left, name);
23429 break;
23430 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
23431 left -= print_symbol_name (- left, "true");
23432 break;
23433 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
23434 left -= print_symbol_name (- left, "false");
23435 break;
23436 }
23437
23438 if (do_wide && left > 0)
23439 printf ("%-*s", left, " ");
23440
23441 return true;
23442 }
23443
23444 /* Print the contents of PNOTE as hex. */
23445
23446 static void
23447 print_note_contents_hex (Elf_Internal_Note *pnote)
23448 {
23449 if (pnote->descsz)
23450 {
23451 size_t i;
23452
23453 printf (_(" description data: "));
23454 for (i = 0; i < pnote->descsz; i++)
23455 printf ("%02x ", pnote->descdata[i] & 0xff);
23456 if (!do_wide)
23457 printf ("\n");
23458 }
23459
23460 if (do_wide)
23461 printf ("\n");
23462 }
23463
23464 #if defined HAVE_MSGPACK
23465
23466 static void
23467 print_indents (int n)
23468 {
23469 printf (" ");
23470
23471 for (int i = 0; i < n; i++)
23472 printf (" ");
23473 }
23474
23475 /* Print OBJ in human-readable form. */
23476
23477 static void
23478 dump_msgpack_obj (const msgpack_object *obj, int indent)
23479 {
23480 switch (obj->type)
23481 {
23482 case MSGPACK_OBJECT_NIL:
23483 printf ("(nil)");
23484 break;
23485
23486 case MSGPACK_OBJECT_BOOLEAN:
23487 printf ("%s", obj->via.boolean ? "true" : "false");
23488 break;
23489
23490 case MSGPACK_OBJECT_POSITIVE_INTEGER:
23491 printf ("%" PRIu64, obj->via.u64);
23492 break;
23493
23494 case MSGPACK_OBJECT_NEGATIVE_INTEGER:
23495 printf ("%" PRIi64, obj->via.i64);
23496 break;
23497
23498 case MSGPACK_OBJECT_FLOAT32:
23499 case MSGPACK_OBJECT_FLOAT64:
23500 printf ("%f", obj->via.f64);
23501 break;
23502
23503 case MSGPACK_OBJECT_STR:
23504 printf ("\"%.*s\"", obj->via.str.size, obj->via.str.ptr);
23505 break;
23506
23507 case MSGPACK_OBJECT_ARRAY:
23508 {
23509 const msgpack_object_array *array = &obj->via.array;
23510
23511 printf ("[\n");
23512 ++indent;
23513
23514 for (uint32_t i = 0; i < array->size; ++i)
23515 {
23516 const msgpack_object *item = &array->ptr[i];
23517
23518 print_indents (indent);
23519 dump_msgpack_obj (item, indent);
23520 printf (",\n");
23521 }
23522
23523 --indent;
23524 print_indents (indent);
23525 printf ("]");
23526 break;
23527 }
23528 break;
23529
23530 case MSGPACK_OBJECT_MAP:
23531 {
23532 const msgpack_object_map *map = &obj->via.map;
23533
23534 printf ("{\n");
23535 ++indent;
23536
23537 for (uint32_t i = 0; i < map->size; ++i)
23538 {
23539 const msgpack_object_kv *kv = &map->ptr[i];
23540 const msgpack_object *key = &kv->key;
23541 const msgpack_object *val = &kv->val;
23542
23543 print_indents (indent);
23544 dump_msgpack_obj (key, indent);
23545 printf (": ");
23546 dump_msgpack_obj (val, indent);
23547
23548 printf (",\n");
23549 }
23550
23551 --indent;
23552 print_indents (indent);
23553 printf ("}");
23554
23555 break;
23556 }
23557
23558 case MSGPACK_OBJECT_BIN:
23559 printf ("(bin)");
23560 break;
23561
23562 case MSGPACK_OBJECT_EXT:
23563 printf ("(ext)");
23564 break;
23565 }
23566 }
23567
23568 static void
23569 dump_msgpack (const msgpack_unpacked *msg)
23570 {
23571 print_indents (0);
23572 dump_msgpack_obj (&msg->data, 0);
23573 printf ("\n");
23574 }
23575
23576 #endif /* defined HAVE_MSGPACK */
23577
23578 static bool
23579 print_amdgpu_note (Elf_Internal_Note *pnote)
23580 {
23581 #if defined HAVE_MSGPACK
23582 /* If msgpack is available, decode and dump the note's content. */
23583 bool ret;
23584 msgpack_unpacked msg;
23585 msgpack_unpack_return msgpack_ret;
23586
23587 assert (pnote->type == NT_AMDGPU_METADATA);
23588
23589 msgpack_unpacked_init (&msg);
23590 msgpack_ret = msgpack_unpack_next (&msg, pnote->descdata, pnote->descsz,
23591 NULL);
23592
23593 switch (msgpack_ret)
23594 {
23595 case MSGPACK_UNPACK_SUCCESS:
23596 dump_msgpack (&msg);
23597 ret = true;
23598 break;
23599
23600 default:
23601 error (_("failed to unpack msgpack contents in NT_AMDGPU_METADATA note"));
23602 ret = false;
23603 break;
23604 }
23605
23606 msgpack_unpacked_destroy (&msg);
23607 return ret;
23608 #else
23609 /* msgpack is not available, dump contents as hex. */
23610 print_note_contents_hex (pnote);
23611 return true;
23612 #endif
23613 }
23614
23615 static bool
23616 print_qnx_note (Elf_Internal_Note *pnote)
23617 {
23618 switch (pnote->type)
23619 {
23620 case QNT_STACK:
23621 if (pnote->descsz != 12)
23622 goto desc_size_fail;
23623
23624 printf (_(" Stack Size: 0x%" PRIx32 "\n"),
23625 (unsigned) byte_get ((unsigned char *) pnote->descdata, 4));
23626 printf (_(" Stack allocated: %" PRIx32 "\n"),
23627 (unsigned) byte_get ((unsigned char *) pnote->descdata + 4, 4));
23628 printf (_(" Executable: %s\n"),
23629 ((unsigned) byte_get ((unsigned char *) pnote->descdata + 8, 1)) ? "no": "yes");
23630 break;
23631
23632 default:
23633 print_note_contents_hex(pnote);
23634 }
23635 return true;
23636
23637 desc_size_fail:
23638 printf (_(" <corrupt - data size is too small>\n"));
23639 error (_("corrupt QNX note: data size is too small\n"));
23640 return false;
23641 }
23642
23643
23644 /* Note that by the ELF standard, the name field is already null byte
23645 terminated, and namesz includes the terminating null byte.
23646 I.E. the value of namesz for the name "FSF" is 4.
23647
23648 If the value of namesz is zero, there is no name present. */
23649
23650 static bool
23651 process_note (Elf_Internal_Note * pnote,
23652 Filedata * filedata)
23653 {
23654 const char * name = pnote->namesz ? pnote->namedata : "(NONE)";
23655 const char * nt;
23656
23657 if (pnote->namesz == 0)
23658 /* If there is no note name, then use the default set of
23659 note type strings. */
23660 nt = get_note_type (filedata, pnote->type);
23661
23662 else if (startswith (pnote->namedata, "GNU"))
23663 /* GNU-specific object file notes. */
23664 nt = get_gnu_elf_note_type (pnote->type);
23665
23666 else if (startswith (pnote->namedata, "AMDGPU"))
23667 /* AMDGPU-specific object file notes. */
23668 nt = get_amdgpu_elf_note_type (pnote->type);
23669
23670 else if (startswith (pnote->namedata, "FreeBSD"))
23671 /* FreeBSD-specific core file notes. */
23672 nt = get_freebsd_elfcore_note_type (filedata, pnote->type);
23673
23674 else if (startswith (pnote->namedata, "NetBSD-CORE"))
23675 /* NetBSD-specific core file notes. */
23676 nt = get_netbsd_elfcore_note_type (filedata, pnote->type);
23677
23678 else if (startswith (pnote->namedata, "NetBSD"))
23679 /* NetBSD-specific core file notes. */
23680 return process_netbsd_elf_note (pnote);
23681
23682 else if (startswith (pnote->namedata, "PaX"))
23683 /* NetBSD-specific core file notes. */
23684 return process_netbsd_elf_note (pnote);
23685
23686 else if (startswith (pnote->namedata, "OpenBSD"))
23687 /* OpenBSD-specific core file notes. */
23688 nt = get_openbsd_elfcore_note_type (filedata, pnote->type);
23689
23690 else if (startswith (pnote->namedata, "QNX"))
23691 /* QNX-specific core file notes. */
23692 nt = get_qnx_elfcore_note_type (filedata, pnote->type);
23693
23694 else if (startswith (pnote->namedata, "SPU/"))
23695 {
23696 /* SPU-specific core file notes. */
23697 nt = pnote->namedata + 4;
23698 name = "SPU";
23699 }
23700
23701 else if (startswith (pnote->namedata, "IPF/VMS"))
23702 /* VMS/ia64-specific file notes. */
23703 nt = get_ia64_vms_note_type (pnote->type);
23704
23705 else if (startswith (pnote->namedata, "stapsdt"))
23706 nt = get_stapsdt_note_type (pnote->type);
23707
23708 else
23709 /* Don't recognize this note name; just use the default set of
23710 note type strings. */
23711 nt = get_note_type (filedata, pnote->type);
23712
23713 printf (" ");
23714
23715 if (((startswith (pnote->namedata, "GA")
23716 && strchr ("*$!+", pnote->namedata[2]) != NULL)
23717 || strchr ("*$!+", pnote->namedata[0]) != NULL)
23718 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
23719 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
23720 print_gnu_build_attribute_name (pnote);
23721 else
23722 print_symbol_name (-20, name);
23723
23724 if (do_wide)
23725 printf (" 0x%08lx\t%s\t", pnote->descsz, nt);
23726 else
23727 printf (" 0x%08lx\t%s\n", pnote->descsz, nt);
23728
23729 if (startswith (pnote->namedata, "IPF/VMS"))
23730 return print_ia64_vms_note (pnote);
23731 else if (startswith (pnote->namedata, "GNU"))
23732 return print_gnu_note (filedata, pnote);
23733 else if (startswith (pnote->namedata, "stapsdt"))
23734 return print_stapsdt_note (pnote);
23735 else if (startswith (pnote->namedata, "CORE"))
23736 return print_core_note (pnote);
23737 else if (startswith (pnote->namedata, "FDO"))
23738 return print_fdo_note (pnote);
23739 else if (((startswith (pnote->namedata, "GA")
23740 && strchr ("*$!+", pnote->namedata[2]) != NULL)
23741 || strchr ("*$!+", pnote->namedata[0]) != NULL)
23742 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
23743 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
23744 return print_gnu_build_attribute_description (pnote, filedata);
23745 else if (startswith (pnote->namedata, "AMDGPU")
23746 && pnote->type == NT_AMDGPU_METADATA)
23747 return print_amdgpu_note (pnote);
23748 else if (startswith (pnote->namedata, "QNX"))
23749 return print_qnx_note (pnote);
23750
23751 print_note_contents_hex (pnote);
23752 return true;
23753 }
23754
23755 static bool
23756 process_notes_at (Filedata * filedata,
23757 Elf_Internal_Shdr * section,
23758 uint64_t offset,
23759 uint64_t length,
23760 uint64_t align)
23761 {
23762 Elf_External_Note *pnotes;
23763 Elf_External_Note *external;
23764 char *end;
23765 bool res = true;
23766
23767 if (length <= 0)
23768 return false;
23769
23770 if (section)
23771 {
23772 pnotes = (Elf_External_Note *) get_section_contents (section, filedata);
23773 if (pnotes)
23774 {
23775 if (! apply_relocations (filedata, section, (unsigned char *) pnotes, length, NULL, NULL))
23776 {
23777 free (pnotes);
23778 return false;
23779 }
23780 }
23781 }
23782 else
23783 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
23784 _("notes"));
23785
23786 if (pnotes == NULL)
23787 return false;
23788
23789 external = pnotes;
23790
23791 if (filedata->is_separate)
23792 printf (_("In linked file '%s': "), filedata->file_name);
23793 else
23794 printf ("\n");
23795 if (section)
23796 printf (_("Displaying notes found in: %s\n"), printable_section_name (filedata, section));
23797 else
23798 printf (_("Displaying notes found at file offset 0x%08" PRIx64
23799 " with length 0x%08" PRIx64 ":\n"),
23800 offset, length);
23801
23802 /* NB: Some note sections may have alignment value of 0 or 1. gABI
23803 specifies that notes should be aligned to 4 bytes in 32-bit
23804 objects and to 8 bytes in 64-bit objects. As a Linux extension,
23805 we also support 4 byte alignment in 64-bit objects. If section
23806 alignment is less than 4, we treate alignment as 4 bytes. */
23807 if (align < 4)
23808 align = 4;
23809 else if (align != 4 && align != 8)
23810 {
23811 warn (_("Corrupt note: alignment %" PRId64 ", expecting 4 or 8\n"),
23812 align);
23813 free (pnotes);
23814 return false;
23815 }
23816
23817 printf (_(" %-20s %-10s\tDescription\n"), _("Owner"), _("Data size"));
23818
23819 end = (char *) pnotes + length;
23820 while ((char *) external < end)
23821 {
23822 Elf_Internal_Note inote;
23823 size_t min_notesz;
23824 char * next;
23825 char * temp = NULL;
23826 size_t data_remaining = end - (char *) external;
23827
23828 if (!is_ia64_vms (filedata))
23829 {
23830 /* PR binutils/15191
23831 Make sure that there is enough data to read. */
23832 min_notesz = offsetof (Elf_External_Note, name);
23833 if (data_remaining < min_notesz)
23834 {
23835 warn (ngettext ("Corrupt note: only %zd byte remains, "
23836 "not enough for a full note\n",
23837 "Corrupt note: only %zd bytes remain, "
23838 "not enough for a full note\n",
23839 data_remaining),
23840 data_remaining);
23841 break;
23842 }
23843 data_remaining -= min_notesz;
23844
23845 inote.type = BYTE_GET (external->type);
23846 inote.namesz = BYTE_GET (external->namesz);
23847 inote.namedata = external->name;
23848 inote.descsz = BYTE_GET (external->descsz);
23849 inote.descdata = ((char *) external
23850 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
23851 inote.descpos = offset + (inote.descdata - (char *) pnotes);
23852 next = ((char *) external
23853 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
23854 }
23855 else
23856 {
23857 Elf64_External_VMS_Note *vms_external;
23858
23859 /* PR binutils/15191
23860 Make sure that there is enough data to read. */
23861 min_notesz = offsetof (Elf64_External_VMS_Note, name);
23862 if (data_remaining < min_notesz)
23863 {
23864 warn (ngettext ("Corrupt note: only %zd byte remains, "
23865 "not enough for a full note\n",
23866 "Corrupt note: only %zd bytes remain, "
23867 "not enough for a full note\n",
23868 data_remaining),
23869 data_remaining);
23870 break;
23871 }
23872 data_remaining -= min_notesz;
23873
23874 vms_external = (Elf64_External_VMS_Note *) external;
23875 inote.type = BYTE_GET (vms_external->type);
23876 inote.namesz = BYTE_GET (vms_external->namesz);
23877 inote.namedata = vms_external->name;
23878 inote.descsz = BYTE_GET (vms_external->descsz);
23879 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
23880 inote.descpos = offset + (inote.descdata - (char *) pnotes);
23881 next = inote.descdata + align_power (inote.descsz, 3);
23882 }
23883
23884 /* PR 17531: file: 3443835e. */
23885 /* PR 17531: file: id:000000,sig:11,src:006986,op:havoc,rep:4. */
23886 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
23887 || (size_t) (inote.descdata - inote.namedata) > data_remaining
23888 || (size_t) (next - inote.descdata) < inote.descsz
23889 || ((size_t) (next - inote.descdata)
23890 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
23891 {
23892 warn (_("note with invalid namesz and/or descsz found at offset %#tx\n"),
23893 (char *) external - (char *) pnotes);
23894 warn (_(" type: %#lx, namesize: %#lx, descsize: %#lx, alignment: %u\n"),
23895 inote.type, inote.namesz, inote.descsz, (int) align);
23896 break;
23897 }
23898
23899 external = (Elf_External_Note *) next;
23900
23901 /* Verify that name is null terminated. It appears that at least
23902 one version of Linux (RedHat 6.0) generates corefiles that don't
23903 comply with the ELF spec by failing to include the null byte in
23904 namesz. */
23905 if (inote.namesz > 0 && inote.namedata[inote.namesz - 1] != '\0')
23906 {
23907 if ((size_t) (inote.descdata - inote.namedata) == inote.namesz)
23908 {
23909 temp = (char *) malloc (inote.namesz + 1);
23910 if (temp == NULL)
23911 {
23912 error (_("Out of memory allocating space for inote name\n"));
23913 res = false;
23914 break;
23915 }
23916
23917 memcpy (temp, inote.namedata, inote.namesz);
23918 inote.namedata = temp;
23919 }
23920 inote.namedata[inote.namesz] = 0;
23921 }
23922
23923 if (! process_note (& inote, filedata))
23924 res = false;
23925
23926 free (temp);
23927 temp = NULL;
23928 }
23929
23930 free (pnotes);
23931
23932 return res;
23933 }
23934
23935 static bool
23936 process_corefile_note_segments (Filedata * filedata)
23937 {
23938 Elf_Internal_Phdr *segment;
23939 unsigned int i;
23940 bool res = true;
23941
23942 if (! get_program_headers (filedata))
23943 return true;
23944
23945 for (i = 0, segment = filedata->program_headers;
23946 i < filedata->file_header.e_phnum;
23947 i++, segment++)
23948 {
23949 if (segment->p_type == PT_NOTE)
23950 if (! process_notes_at (filedata, NULL, segment->p_offset,
23951 segment->p_filesz, segment->p_align))
23952 res = false;
23953 }
23954
23955 return res;
23956 }
23957
23958 static bool
23959 process_v850_notes (Filedata * filedata, uint64_t offset, uint64_t length)
23960 {
23961 Elf_External_Note * pnotes;
23962 Elf_External_Note * external;
23963 char * end;
23964 bool res = true;
23965
23966 if (length <= 0)
23967 return false;
23968
23969 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
23970 _("v850 notes"));
23971 if (pnotes == NULL)
23972 return false;
23973
23974 external = pnotes;
23975 end = (char*) pnotes + length;
23976
23977 printf (_("\nDisplaying contents of Renesas V850 notes section at offset"
23978 " %#" PRIx64 " with length %#" PRIx64 ":\n"),
23979 offset, length);
23980
23981 while ((char *) external < end)
23982 {
23983 char *next;
23984 Elf_Internal_Note inote;
23985 size_t data_remaining = end - (char *) external;
23986
23987 if (data_remaining < offsetof (Elf_External_Note, name))
23988 break;
23989 data_remaining -= offsetof (Elf_External_Note, name);
23990
23991 inote.type = BYTE_GET (external->type);
23992 inote.namesz = BYTE_GET (external->namesz);
23993 inote.namedata = external->name;
23994 inote.descsz = BYTE_GET (external->descsz);
23995 inote.descdata = inote.namedata + align_power (inote.namesz, 2);
23996 inote.descpos = offset + (inote.descdata - (char *) pnotes);
23997 next = inote.descdata + align_power (inote.descsz, 2);
23998
23999 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
24000 || (size_t) (inote.descdata - inote.namedata) > data_remaining
24001 || (size_t) (next - inote.descdata) < inote.descsz
24002 || ((size_t) (next - inote.descdata)
24003 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
24004 {
24005 warn (_("note with invalid namesz and/or descsz found at offset %#tx\n"),
24006 (char *) external - (char *) pnotes);
24007 warn (_(" type: %#lx, namesize: %#lx, descsize: %#lx, alignment: %u\n"),
24008 inote.type, inote.namesz, inote.descsz, 2);
24009 break;
24010 }
24011
24012 external = (Elf_External_Note *) next;
24013
24014 if (! print_v850_note (& inote))
24015 res = false;
24016 }
24017
24018 free (pnotes);
24019
24020 return res;
24021 }
24022
24023 static bool
24024 process_note_sections (Filedata * filedata)
24025 {
24026 Elf_Internal_Shdr *section;
24027 size_t i;
24028 unsigned int n = 0;
24029 bool res = true;
24030
24031 for (i = 0, section = filedata->section_headers;
24032 i < filedata->file_header.e_shnum && section != NULL;
24033 i++, section++)
24034 {
24035 if (section->sh_type == SHT_NOTE)
24036 {
24037 if (! process_notes_at (filedata, section, section->sh_offset,
24038 section->sh_size, section->sh_addralign))
24039 res = false;
24040 n++;
24041 }
24042
24043 if (( filedata->file_header.e_machine == EM_V800
24044 || filedata->file_header.e_machine == EM_V850
24045 || filedata->file_header.e_machine == EM_CYGNUS_V850)
24046 && section->sh_type == SHT_RENESAS_INFO)
24047 {
24048 if (! process_v850_notes (filedata, section->sh_offset,
24049 section->sh_size))
24050 res = false;
24051 n++;
24052 }
24053 }
24054
24055 if (n == 0)
24056 /* Try processing NOTE segments instead. */
24057 return process_corefile_note_segments (filedata);
24058
24059 return res;
24060 }
24061
24062 static bool
24063 process_notes (Filedata * filedata)
24064 {
24065 /* If we have not been asked to display the notes then do nothing. */
24066 if (! do_notes)
24067 return true;
24068
24069 if (filedata->file_header.e_type != ET_CORE)
24070 return process_note_sections (filedata);
24071
24072 /* No program headers means no NOTE segment. */
24073 if (filedata->file_header.e_phnum > 0)
24074 return process_corefile_note_segments (filedata);
24075
24076 if (filedata->is_separate)
24077 printf (_("No notes found in linked file '%s'.\n"),
24078 filedata->file_name);
24079 else
24080 printf (_("No notes found file.\n"));
24081
24082 return true;
24083 }
24084
24085 static unsigned char *
24086 display_public_gnu_attributes (unsigned char * start,
24087 const unsigned char * const end)
24088 {
24089 printf (_(" Unknown GNU attribute: %s\n"), start);
24090
24091 start += strnlen ((char *) start, end - start);
24092 display_raw_attribute (start, end);
24093
24094 return (unsigned char *) end;
24095 }
24096
24097 static unsigned char *
24098 display_generic_attribute (unsigned char * start,
24099 unsigned int tag,
24100 const unsigned char * const end)
24101 {
24102 if (tag == 0)
24103 return (unsigned char *) end;
24104
24105 return display_tag_value (tag, start, end);
24106 }
24107
24108 static bool
24109 process_arch_specific (Filedata * filedata)
24110 {
24111 if (! do_arch)
24112 return true;
24113
24114 switch (filedata->file_header.e_machine)
24115 {
24116 case EM_ARC:
24117 case EM_ARC_COMPACT:
24118 case EM_ARC_COMPACT2:
24119 case EM_ARC_COMPACT3:
24120 case EM_ARC_COMPACT3_64:
24121 return process_attributes (filedata, "ARC", SHT_ARC_ATTRIBUTES,
24122 display_arc_attribute,
24123 display_generic_attribute);
24124 case EM_ARM:
24125 return process_attributes (filedata, "aeabi", SHT_ARM_ATTRIBUTES,
24126 display_arm_attribute,
24127 display_generic_attribute);
24128
24129 case EM_MIPS:
24130 case EM_MIPS_RS3_LE:
24131 return process_mips_specific (filedata, false);
24132
24133 case EM_MSP430:
24134 return process_attributes (filedata, "mspabi", SHT_MSP430_ATTRIBUTES,
24135 display_msp430_attribute,
24136 display_msp430_gnu_attribute);
24137
24138 case EM_RISCV:
24139 return process_attributes (filedata, "riscv", SHT_RISCV_ATTRIBUTES,
24140 display_riscv_attribute,
24141 display_generic_attribute);
24142
24143 case EM_NDS32:
24144 return process_nds32_specific (filedata);
24145
24146 case EM_68K:
24147 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
24148 display_m68k_gnu_attribute);
24149
24150 case EM_PPC:
24151 case EM_PPC64:
24152 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
24153 display_power_gnu_attribute);
24154
24155 case EM_S390:
24156 case EM_S390_OLD:
24157 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
24158 display_s390_gnu_attribute);
24159
24160 case EM_SPARC:
24161 case EM_SPARC32PLUS:
24162 case EM_SPARCV9:
24163 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
24164 display_sparc_gnu_attribute);
24165
24166 case EM_TI_C6000:
24167 return process_attributes (filedata, "c6xabi", SHT_C6000_ATTRIBUTES,
24168 display_tic6x_attribute,
24169 display_generic_attribute);
24170
24171 case EM_CSKY:
24172 return process_attributes (filedata, "csky", SHT_CSKY_ATTRIBUTES,
24173 display_csky_attribute, NULL);
24174
24175 default:
24176 return process_attributes (filedata, "gnu", SHT_GNU_ATTRIBUTES,
24177 display_public_gnu_attributes,
24178 display_generic_attribute);
24179 }
24180 }
24181
24182 static bool
24183 get_file_header (Filedata * filedata)
24184 {
24185 /* Read in the identity array. */
24186 if (fread (filedata->file_header.e_ident, EI_NIDENT, 1, filedata->handle) != 1)
24187 return false;
24188
24189 /* Determine how to read the rest of the header. */
24190 switch (filedata->file_header.e_ident[EI_DATA])
24191 {
24192 default:
24193 case ELFDATANONE:
24194 case ELFDATA2LSB:
24195 byte_get = byte_get_little_endian;
24196 byte_put = byte_put_little_endian;
24197 break;
24198 case ELFDATA2MSB:
24199 byte_get = byte_get_big_endian;
24200 byte_put = byte_put_big_endian;
24201 break;
24202 }
24203
24204 /* For now we only support 32 bit and 64 bit ELF files. */
24205 is_32bit_elf = (filedata->file_header.e_ident[EI_CLASS] != ELFCLASS64);
24206
24207 /* Read in the rest of the header. */
24208 if (is_32bit_elf)
24209 {
24210 Elf32_External_Ehdr ehdr32;
24211
24212 if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, filedata->handle) != 1)
24213 return false;
24214
24215 filedata->file_header.e_type = BYTE_GET (ehdr32.e_type);
24216 filedata->file_header.e_machine = BYTE_GET (ehdr32.e_machine);
24217 filedata->file_header.e_version = BYTE_GET (ehdr32.e_version);
24218 filedata->file_header.e_entry = BYTE_GET (ehdr32.e_entry);
24219 filedata->file_header.e_phoff = BYTE_GET (ehdr32.e_phoff);
24220 filedata->file_header.e_shoff = BYTE_GET (ehdr32.e_shoff);
24221 filedata->file_header.e_flags = BYTE_GET (ehdr32.e_flags);
24222 filedata->file_header.e_ehsize = BYTE_GET (ehdr32.e_ehsize);
24223 filedata->file_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
24224 filedata->file_header.e_phnum = BYTE_GET (ehdr32.e_phnum);
24225 filedata->file_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
24226 filedata->file_header.e_shnum = BYTE_GET (ehdr32.e_shnum);
24227 filedata->file_header.e_shstrndx = BYTE_GET (ehdr32.e_shstrndx);
24228 }
24229 else
24230 {
24231 Elf64_External_Ehdr ehdr64;
24232
24233 if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, filedata->handle) != 1)
24234 return false;
24235
24236 filedata->file_header.e_type = BYTE_GET (ehdr64.e_type);
24237 filedata->file_header.e_machine = BYTE_GET (ehdr64.e_machine);
24238 filedata->file_header.e_version = BYTE_GET (ehdr64.e_version);
24239 filedata->file_header.e_entry = BYTE_GET (ehdr64.e_entry);
24240 filedata->file_header.e_phoff = BYTE_GET (ehdr64.e_phoff);
24241 filedata->file_header.e_shoff = BYTE_GET (ehdr64.e_shoff);
24242 filedata->file_header.e_flags = BYTE_GET (ehdr64.e_flags);
24243 filedata->file_header.e_ehsize = BYTE_GET (ehdr64.e_ehsize);
24244 filedata->file_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
24245 filedata->file_header.e_phnum = BYTE_GET (ehdr64.e_phnum);
24246 filedata->file_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
24247 filedata->file_header.e_shnum = BYTE_GET (ehdr64.e_shnum);
24248 filedata->file_header.e_shstrndx = BYTE_GET (ehdr64.e_shstrndx);
24249 }
24250
24251 return true;
24252 }
24253
24254 static void
24255 free_filedata (Filedata *filedata)
24256 {
24257 free (filedata->program_interpreter);
24258 free (filedata->program_headers);
24259 free (filedata->section_headers);
24260 free (filedata->string_table);
24261 free (filedata->dump.dump_sects);
24262 free (filedata->dynamic_strings);
24263 free (filedata->dynamic_symbols);
24264 free (filedata->dynamic_syminfo);
24265 free (filedata->dynamic_section);
24266
24267 while (filedata->symtab_shndx_list != NULL)
24268 {
24269 elf_section_list *next = filedata->symtab_shndx_list->next;
24270 free (filedata->symtab_shndx_list);
24271 filedata->symtab_shndx_list = next;
24272 }
24273
24274 free (filedata->section_headers_groups);
24275
24276 if (filedata->section_groups)
24277 {
24278 size_t i;
24279 struct group_list * g;
24280 struct group_list * next;
24281
24282 for (i = 0; i < filedata->group_count; i++)
24283 {
24284 for (g = filedata->section_groups [i].root; g != NULL; g = next)
24285 {
24286 next = g->next;
24287 free (g);
24288 }
24289 }
24290
24291 free (filedata->section_groups);
24292 }
24293 memset (&filedata->section_headers, 0,
24294 sizeof (Filedata) - offsetof (Filedata, section_headers));
24295 }
24296
24297 static void
24298 close_file (Filedata * filedata)
24299 {
24300 if (filedata)
24301 {
24302 if (filedata->handle)
24303 fclose (filedata->handle);
24304 free (filedata);
24305 }
24306 }
24307
24308 void
24309 close_debug_file (void * data)
24310 {
24311 free_filedata ((Filedata *) data);
24312 close_file ((Filedata *) data);
24313 }
24314
24315 static Filedata *
24316 open_file (const char * pathname, bool is_separate)
24317 {
24318 struct stat statbuf;
24319 Filedata * filedata = NULL;
24320
24321 if (stat (pathname, & statbuf) < 0
24322 || ! S_ISREG (statbuf.st_mode))
24323 goto fail;
24324
24325 filedata = calloc (1, sizeof * filedata);
24326 if (filedata == NULL)
24327 goto fail;
24328
24329 filedata->handle = fopen (pathname, "rb");
24330 if (filedata->handle == NULL)
24331 goto fail;
24332
24333 filedata->file_size = statbuf.st_size;
24334 filedata->file_name = pathname;
24335 filedata->is_separate = is_separate;
24336
24337 if (! get_file_header (filedata))
24338 goto fail;
24339
24340 if (!get_section_headers (filedata, false))
24341 goto fail;
24342
24343 return filedata;
24344
24345 fail:
24346 if (filedata)
24347 {
24348 if (filedata->handle)
24349 fclose (filedata->handle);
24350 free (filedata);
24351 }
24352 return NULL;
24353 }
24354
24355 void *
24356 open_debug_file (const char * pathname)
24357 {
24358 return open_file (pathname, true);
24359 }
24360
24361 static void
24362 initialise_dump_sects (Filedata * filedata)
24363 {
24364 /* Initialise the dump_sects array from the cmdline_dump_sects array.
24365 Note we do this even if cmdline_dump_sects is empty because we
24366 must make sure that the dump_sets array is zeroed out before each
24367 object file is processed. */
24368 if (filedata->dump.num_dump_sects > cmdline.num_dump_sects)
24369 memset (filedata->dump.dump_sects, 0,
24370 filedata->dump.num_dump_sects * sizeof (*filedata->dump.dump_sects));
24371
24372 if (cmdline.num_dump_sects > 0)
24373 {
24374 if (filedata->dump.num_dump_sects == 0)
24375 /* A sneaky way of allocating the dump_sects array. */
24376 request_dump_bynumber (&filedata->dump, cmdline.num_dump_sects, 0);
24377
24378 assert (filedata->dump.num_dump_sects >= cmdline.num_dump_sects);
24379 memcpy (filedata->dump.dump_sects, cmdline.dump_sects,
24380 cmdline.num_dump_sects * sizeof (*filedata->dump.dump_sects));
24381 }
24382 }
24383
24384 static bool
24385 might_need_separate_debug_info (Filedata * filedata)
24386 {
24387 /* Debuginfo files do not need further separate file loading. */
24388 if (filedata->file_header.e_shstrndx == SHN_UNDEF)
24389 return false;
24390
24391 /* Since do_follow_links might be enabled by default, only treat it as an
24392 indication that separate files should be loaded if setting it was a
24393 deliberate user action. */
24394 if (DEFAULT_FOR_FOLLOW_LINKS == 0 && do_follow_links)
24395 return true;
24396
24397 if (process_links || do_syms || do_unwind
24398 || dump_any_debugging || do_dump || do_debugging)
24399 return true;
24400
24401 return false;
24402 }
24403
24404 /* Process one ELF object file according to the command line options.
24405 This file may actually be stored in an archive. The file is
24406 positioned at the start of the ELF object. Returns TRUE if no
24407 problems were encountered, FALSE otherwise. */
24408
24409 static bool
24410 process_object (Filedata * filedata)
24411 {
24412 bool have_separate_files;
24413 unsigned int i;
24414 bool res;
24415
24416 if (! get_file_header (filedata))
24417 {
24418 error (_("%s: Failed to read file header\n"), filedata->file_name);
24419 return false;
24420 }
24421
24422 /* Initialise per file variables. */
24423 for (i = ARRAY_SIZE (filedata->version_info); i--;)
24424 filedata->version_info[i] = 0;
24425
24426 for (i = ARRAY_SIZE (filedata->dynamic_info); i--;)
24427 filedata->dynamic_info[i] = 0;
24428 filedata->dynamic_info_DT_GNU_HASH = 0;
24429 filedata->dynamic_info_DT_MIPS_XHASH = 0;
24430
24431 /* Process the file. */
24432 if (show_name)
24433 printf (_("\nFile: %s\n"), filedata->file_name);
24434
24435 initialise_dump_sects (filedata);
24436
24437 /* There may be some extensions in the first section header. Don't
24438 bomb if we can't read it. */
24439 get_section_headers (filedata, true);
24440
24441 if (! process_file_header (filedata))
24442 {
24443 res = false;
24444 goto out;
24445 }
24446
24447 /* Throw away the single section header read above, so that we
24448 re-read the entire set. */
24449 free (filedata->section_headers);
24450 filedata->section_headers = NULL;
24451
24452 if (! process_section_headers (filedata))
24453 {
24454 /* Without loaded section headers we cannot process lots of things. */
24455 do_unwind = do_version = do_dump = do_arch = false;
24456
24457 if (! do_using_dynamic)
24458 do_syms = do_dyn_syms = do_reloc = false;
24459 }
24460
24461 if (! process_section_groups (filedata))
24462 /* Without loaded section groups we cannot process unwind. */
24463 do_unwind = false;
24464
24465 process_program_headers (filedata);
24466
24467 res = process_dynamic_section (filedata);
24468
24469 if (! process_relocs (filedata))
24470 res = false;
24471
24472 if (! process_unwind (filedata))
24473 res = false;
24474
24475 if (! process_symbol_table (filedata))
24476 res = false;
24477
24478 if (! process_lto_symbol_tables (filedata))
24479 res = false;
24480
24481 if (! process_syminfo (filedata))
24482 res = false;
24483
24484 if (! process_version_sections (filedata))
24485 res = false;
24486
24487 if (might_need_separate_debug_info (filedata))
24488 have_separate_files = load_separate_debug_files (filedata, filedata->file_name);
24489 else
24490 have_separate_files = false;
24491
24492 if (! process_section_contents (filedata))
24493 res = false;
24494
24495 if (! process_got_section_contents (filedata))
24496 res = false;
24497
24498 if (have_separate_files)
24499 {
24500 separate_info * d;
24501
24502 for (d = first_separate_info; d != NULL; d = d->next)
24503 {
24504 initialise_dump_sects (d->handle);
24505
24506 if (process_links && ! process_file_header (d->handle))
24507 res = false;
24508 else if (! process_section_headers (d->handle))
24509 res = false;
24510 else if (! process_section_contents (d->handle))
24511 res = false;
24512 else if (process_links)
24513 {
24514 if (! process_section_groups (d->handle))
24515 res = false;
24516 process_program_headers (d->handle);
24517 if (! process_dynamic_section (d->handle))
24518 res = false;
24519 if (! process_relocs (d->handle))
24520 res = false;
24521 if (! process_unwind (d->handle))
24522 res = false;
24523 if (! process_symbol_table (d->handle))
24524 res = false;
24525 if (! process_lto_symbol_tables (d->handle))
24526 res = false;
24527 if (! process_syminfo (d->handle))
24528 res = false;
24529 if (! process_version_sections (d->handle))
24530 res = false;
24531 if (! process_notes (d->handle))
24532 res = false;
24533 }
24534 }
24535
24536 /* The file handles are closed by the call to free_debug_memory() below. */
24537 }
24538
24539 if (! process_notes (filedata))
24540 res = false;
24541
24542 if (! process_gnu_liblist (filedata))
24543 res = false;
24544
24545 if (! process_arch_specific (filedata))
24546 res = false;
24547
24548 out:
24549 free_filedata (filedata);
24550
24551 free_debug_memory ();
24552
24553 return res;
24554 }
24555
24556 /* Process an ELF archive.
24557 On entry the file is positioned just after the ARMAG string.
24558 Returns TRUE upon success, FALSE otherwise. */
24559
24560 static bool
24561 process_archive (Filedata * filedata, bool is_thin_archive)
24562 {
24563 struct archive_info arch;
24564 struct archive_info nested_arch;
24565 size_t got;
24566 bool ret = true;
24567
24568 show_name = true;
24569
24570 /* The ARCH structure is used to hold information about this archive. */
24571 arch.file_name = NULL;
24572 arch.file = NULL;
24573 arch.index_array = NULL;
24574 arch.sym_table = NULL;
24575 arch.longnames = NULL;
24576
24577 /* The NESTED_ARCH structure is used as a single-item cache of information
24578 about a nested archive (when members of a thin archive reside within
24579 another regular archive file). */
24580 nested_arch.file_name = NULL;
24581 nested_arch.file = NULL;
24582 nested_arch.index_array = NULL;
24583 nested_arch.sym_table = NULL;
24584 nested_arch.longnames = NULL;
24585
24586 if (setup_archive (&arch, filedata->file_name, filedata->handle,
24587 filedata->file_size, is_thin_archive,
24588 do_archive_index) != 0)
24589 {
24590 ret = false;
24591 goto out;
24592 }
24593
24594 if (do_archive_index)
24595 {
24596 if (arch.sym_table == NULL)
24597 error (_("%s: unable to dump the index as none was found\n"),
24598 filedata->file_name);
24599 else
24600 {
24601 uint64_t i, l;
24602 uint64_t current_pos;
24603
24604 printf (_("Index of archive %s: (%" PRIu64 " entries,"
24605 " %#" PRIx64 " bytes in the symbol table)\n"),
24606 filedata->file_name, arch.index_num,
24607 arch.sym_size);
24608
24609 current_pos = ftell (filedata->handle);
24610
24611 for (i = l = 0; i < arch.index_num; i++)
24612 {
24613 if (i == 0
24614 || (i > 0 && arch.index_array[i] != arch.index_array[i - 1]))
24615 {
24616 char * member_name
24617 = get_archive_member_name_at (&arch, arch.index_array[i],
24618 &nested_arch);
24619
24620 if (member_name != NULL)
24621 {
24622 char * qualified_name
24623 = make_qualified_name (&arch, &nested_arch,
24624 member_name);
24625
24626 if (qualified_name != NULL)
24627 {
24628 printf (_("Contents of binary %s at offset "),
24629 qualified_name);
24630 (void) print_vma (arch.index_array[i], PREFIX_HEX);
24631 putchar ('\n');
24632 free (qualified_name);
24633 }
24634 free (member_name);
24635 }
24636 }
24637
24638 if (l >= arch.sym_size)
24639 {
24640 error (_("%s: end of the symbol table reached "
24641 "before the end of the index\n"),
24642 filedata->file_name);
24643 ret = false;
24644 break;
24645 }
24646 /* PR 17531: file: 0b6630b2. */
24647 printf ("\t%.*s\n",
24648 (int) (arch.sym_size - l), arch.sym_table + l);
24649 l += strnlen (arch.sym_table + l, arch.sym_size - l) + 1;
24650 }
24651
24652 if (arch.uses_64bit_indices)
24653 l = (l + 7) & ~ 7;
24654 else
24655 l += l & 1;
24656
24657 if (l < arch.sym_size)
24658 {
24659 error (ngettext ("%s: %" PRId64 " byte remains in the symbol table, "
24660 "but without corresponding entries in "
24661 "the index table\n",
24662 "%s: %" PRId64 " bytes remain in the symbol table, "
24663 "but without corresponding entries in "
24664 "the index table\n",
24665 arch.sym_size - l),
24666 filedata->file_name, arch.sym_size - l);
24667 ret = false;
24668 }
24669
24670 if (fseek64 (filedata->handle, current_pos, SEEK_SET) != 0)
24671 {
24672 error (_("%s: failed to seek back to start of object files "
24673 "in the archive\n"),
24674 filedata->file_name);
24675 ret = false;
24676 goto out;
24677 }
24678 }
24679
24680 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
24681 && !do_segments && !do_header && !do_dump && !do_version
24682 && !do_histogram && !do_debugging && !do_arch && !do_notes
24683 && !do_section_groups && !do_dyn_syms)
24684 {
24685 ret = true; /* Archive index only. */
24686 goto out;
24687 }
24688 }
24689
24690 while (1)
24691 {
24692 char * name;
24693 size_t namelen;
24694 char * qualified_name;
24695
24696 /* Read the next archive header. */
24697 if (fseek64 (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0)
24698 {
24699 error (_("%s: failed to seek to next archive header\n"),
24700 arch.file_name);
24701 ret = false;
24702 break;
24703 }
24704 got = fread (&arch.arhdr, 1, sizeof arch.arhdr, filedata->handle);
24705 if (got != sizeof arch.arhdr)
24706 {
24707 if (got == 0)
24708 break;
24709 /* PR 24049 - we cannot use filedata->file_name as this will
24710 have already been freed. */
24711 error (_("%s: failed to read archive header\n"), arch.file_name);
24712
24713 ret = false;
24714 break;
24715 }
24716 if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
24717 {
24718 error (_("%s: did not find a valid archive header\n"),
24719 arch.file_name);
24720 ret = false;
24721 break;
24722 }
24723
24724 arch.next_arhdr_offset += sizeof arch.arhdr;
24725
24726 filedata->archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
24727
24728 name = get_archive_member_name (&arch, &nested_arch);
24729 if (name == NULL)
24730 {
24731 error (_("%s: bad archive file name\n"), arch.file_name);
24732 ret = false;
24733 break;
24734 }
24735 namelen = strlen (name);
24736
24737 qualified_name = make_qualified_name (&arch, &nested_arch, name);
24738 if (qualified_name == NULL)
24739 {
24740 error (_("%s: bad archive file name\n"), arch.file_name);
24741 free (name);
24742 ret = false;
24743 break;
24744 }
24745
24746 if (is_thin_archive && arch.nested_member_origin == 0)
24747 {
24748 /* This is a proxy for an external member of a thin archive. */
24749 Filedata * member_filedata;
24750 char * member_file_name = adjust_relative_path
24751 (filedata->file_name, name, namelen);
24752
24753 free (name);
24754 if (member_file_name == NULL)
24755 {
24756 free (qualified_name);
24757 ret = false;
24758 break;
24759 }
24760
24761 member_filedata = open_file (member_file_name, false);
24762 if (member_filedata == NULL)
24763 {
24764 error (_("Input file '%s' is not readable.\n"), member_file_name);
24765 free (member_file_name);
24766 free (qualified_name);
24767 ret = false;
24768 break;
24769 }
24770
24771 filedata->archive_file_offset = arch.nested_member_origin;
24772 member_filedata->file_name = qualified_name;
24773
24774 /* The call to process_object() expects the file to be at the beginning. */
24775 rewind (member_filedata->handle);
24776
24777 if (! process_object (member_filedata))
24778 ret = false;
24779
24780 close_file (member_filedata);
24781 free (member_file_name);
24782 }
24783 else if (is_thin_archive)
24784 {
24785 Filedata thin_filedata;
24786
24787 memset (&thin_filedata, 0, sizeof (thin_filedata));
24788
24789 /* PR 15140: Allow for corrupt thin archives. */
24790 if (nested_arch.file == NULL)
24791 {
24792 error (_("%s: contains corrupt thin archive: %s\n"),
24793 qualified_name, name);
24794 free (qualified_name);
24795 free (name);
24796 ret = false;
24797 break;
24798 }
24799 free (name);
24800
24801 /* This is a proxy for a member of a nested archive. */
24802 filedata->archive_file_offset
24803 = arch.nested_member_origin + sizeof arch.arhdr;
24804
24805 /* The nested archive file will have been opened and setup by
24806 get_archive_member_name. */
24807 if (fseek64 (nested_arch.file, filedata->archive_file_offset,
24808 SEEK_SET) != 0)
24809 {
24810 error (_("%s: failed to seek to archive member.\n"),
24811 nested_arch.file_name);
24812 free (qualified_name);
24813 ret = false;
24814 break;
24815 }
24816
24817 thin_filedata.handle = nested_arch.file;
24818 thin_filedata.file_name = qualified_name;
24819
24820 if (! process_object (& thin_filedata))
24821 ret = false;
24822 }
24823 else
24824 {
24825 free (name);
24826 filedata->archive_file_offset = arch.next_arhdr_offset;
24827 filedata->file_name = qualified_name;
24828 if (! process_object (filedata))
24829 ret = false;
24830 arch.next_arhdr_offset += (filedata->archive_file_size + 1) & -2;
24831 /* Stop looping with "negative" archive_file_size. */
24832 if (arch.next_arhdr_offset < filedata->archive_file_size)
24833 arch.next_arhdr_offset = -1ul;
24834 }
24835
24836 free (qualified_name);
24837 }
24838
24839 out:
24840 if (nested_arch.file != NULL)
24841 fclose (nested_arch.file);
24842 release_archive (&nested_arch);
24843 release_archive (&arch);
24844
24845 return ret;
24846 }
24847
24848 static bool
24849 process_file (char * file_name)
24850 {
24851 Filedata * filedata = NULL;
24852 struct stat statbuf;
24853 char armag[SARMAG];
24854 bool ret = true;
24855
24856 if (stat (file_name, &statbuf) < 0)
24857 {
24858 if (errno == ENOENT)
24859 error (_("'%s': No such file\n"), file_name);
24860 else
24861 error (_("Could not locate '%s'. System error message: %s\n"),
24862 file_name, strerror (errno));
24863 return false;
24864 }
24865
24866 if (! S_ISREG (statbuf.st_mode))
24867 {
24868 error (_("'%s' is not an ordinary file\n"), file_name);
24869 return false;
24870 }
24871
24872 filedata = calloc (1, sizeof * filedata);
24873 if (filedata == NULL)
24874 {
24875 error (_("Out of memory allocating file data structure\n"));
24876 return false;
24877 }
24878
24879 filedata->file_name = file_name;
24880 filedata->handle = fopen (file_name, "rb");
24881 if (filedata->handle == NULL)
24882 {
24883 error (_("Input file '%s' is not readable.\n"), file_name);
24884 free (filedata);
24885 return false;
24886 }
24887
24888 if (fread (armag, SARMAG, 1, filedata->handle) != 1)
24889 {
24890 error (_("%s: Failed to read file's magic number\n"), file_name);
24891 fclose (filedata->handle);
24892 free (filedata);
24893 return false;
24894 }
24895
24896 filedata->file_size = statbuf.st_size;
24897 filedata->is_separate = false;
24898
24899 if (memcmp (armag, ARMAG, SARMAG) == 0)
24900 {
24901 if (! process_archive (filedata, false))
24902 ret = false;
24903 }
24904 else if (memcmp (armag, ARMAGT, SARMAG) == 0)
24905 {
24906 if ( ! process_archive (filedata, true))
24907 ret = false;
24908 }
24909 else
24910 {
24911 if (do_archive_index && !check_all)
24912 error (_("File %s is not an archive so its index cannot be displayed.\n"),
24913 file_name);
24914
24915 rewind (filedata->handle);
24916 filedata->archive_file_size = filedata->archive_file_offset = 0;
24917
24918 if (! process_object (filedata))
24919 ret = false;
24920 }
24921
24922 close_debug_file (filedata);
24923
24924 free (ba_cache.strtab);
24925 ba_cache.strtab = NULL;
24926 free (ba_cache.symtab);
24927 ba_cache.symtab = NULL;
24928 ba_cache.filedata = NULL;
24929
24930 return ret;
24931 }
24932
24933 #ifdef SUPPORT_DISASSEMBLY
24934 /* Needed by the i386 disassembler. For extra credit, someone could
24935 fix this so that we insert symbolic addresses here, esp for GOT/PLT
24936 symbols. */
24937
24938 void
24939 print_address (unsigned int addr, FILE * outfile)
24940 {
24941 fprintf (outfile,"0x%8.8x", addr);
24942 }
24943
24944 /* Needed by the i386 disassembler. */
24945
24946 void
24947 db_task_printsym (unsigned int addr)
24948 {
24949 print_address (addr, stderr);
24950 }
24951 #endif
24952
24953 int
24954 main (int argc, char ** argv)
24955 {
24956 int err;
24957
24958 #ifdef HAVE_LC_MESSAGES
24959 setlocale (LC_MESSAGES, "");
24960 #endif
24961 setlocale (LC_CTYPE, "");
24962 bindtextdomain (PACKAGE, LOCALEDIR);
24963 textdomain (PACKAGE);
24964
24965 expandargv (&argc, &argv);
24966
24967 parse_args (& cmdline, argc, argv);
24968
24969 if (optind < (argc - 1))
24970 /* When displaying information for more than one file,
24971 prefix the information with the file name. */
24972 show_name = true;
24973 else if (optind >= argc)
24974 {
24975 /* Ensure that the warning is always displayed. */
24976 do_checks = true;
24977
24978 warn (_("Nothing to do.\n"));
24979 usage (stderr);
24980 }
24981
24982 err = false;
24983 while (optind < argc)
24984 if (! process_file (argv[optind++]))
24985 err = true;
24986
24987 free (cmdline.dump_sects);
24988
24989 free (dump_ctf_symtab_name);
24990 free (dump_ctf_strtab_name);
24991 free (dump_ctf_parent_name);
24992
24993 return err ? EXIT_FAILURE : EXIT_SUCCESS;
24994 }