]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - binutils/readelf.c
readelf: DO not load section headers from file offset zero
[thirdparty/binutils-gdb.git] / binutils / readelf.c
1 /* readelf.c -- display contents of an ELF format file
2 Copyright (C) 1998-2022 Free Software Foundation, Inc.
3
4 Originally developed by Eric Youngdale <eric@andante.jic.com>
5 Modifications by Nick Clifton <nickc@redhat.com>
6
7 This file is part of GNU Binutils.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
22 02110-1301, USA. */
23 \f
24 /* The difference between readelf and objdump:
25
26 Both programs are capable of displaying the contents of ELF format files,
27 so why does the binutils project have two file dumpers ?
28
29 The reason is that objdump sees an ELF file through a BFD filter of the
30 world; if BFD has a bug where, say, it disagrees about a machine constant
31 in e_flags, then the odds are good that it will remain internally
32 consistent. The linker sees it the BFD way, objdump sees it the BFD way,
33 GAS sees it the BFD way. There was need for a tool to go find out what
34 the file actually says.
35
36 This is why the readelf program does not link against the BFD library - it
37 exists as an independent program to help verify the correct working of BFD.
38
39 There is also the case that readelf can provide more information about an
40 ELF file than is provided by objdump. In particular it can display DWARF
41 debugging information which (at the moment) objdump cannot. */
42 \f
43 #include "sysdep.h"
44 #include <assert.h>
45 #include <time.h>
46 #include <zlib.h>
47 #include <wchar.h>
48
49 #if defined HAVE_MSGPACK
50 #include <msgpack.h>
51 #endif
52
53 /* Define BFD64 here, even if our default architecture is 32 bit ELF
54 as this will allow us to read in and parse 64bit and 32bit ELF files. */
55 #define BFD64
56
57 #include "bfd.h"
58 #include "bucomm.h"
59 #include "elfcomm.h"
60 #include "demanguse.h"
61 #include "dwarf.h"
62 #include "ctf-api.h"
63 #include "demangle.h"
64
65 #include "elf/common.h"
66 #include "elf/external.h"
67 #include "elf/internal.h"
68
69
70 /* Included here, before RELOC_MACROS_GEN_FUNC is defined, so that
71 we can obtain the H8 reloc numbers. We need these for the
72 get_reloc_size() function. We include h8.h again after defining
73 RELOC_MACROS_GEN_FUNC so that we get the naming function as well. */
74
75 #include "elf/h8.h"
76 #undef _ELF_H8_H
77
78 /* Undo the effects of #including reloc-macros.h. */
79
80 #undef START_RELOC_NUMBERS
81 #undef RELOC_NUMBER
82 #undef FAKE_RELOC
83 #undef EMPTY_RELOC
84 #undef END_RELOC_NUMBERS
85 #undef _RELOC_MACROS_H
86
87 /* The following headers use the elf/reloc-macros.h file to
88 automatically generate relocation recognition functions
89 such as elf_mips_reloc_type() */
90
91 #define RELOC_MACROS_GEN_FUNC
92
93 #include "elf/aarch64.h"
94 #include "elf/alpha.h"
95 #include "elf/amdgpu.h"
96 #include "elf/arc.h"
97 #include "elf/arm.h"
98 #include "elf/avr.h"
99 #include "elf/bfin.h"
100 #include "elf/cr16.h"
101 #include "elf/cris.h"
102 #include "elf/crx.h"
103 #include "elf/csky.h"
104 #include "elf/d10v.h"
105 #include "elf/d30v.h"
106 #include "elf/dlx.h"
107 #include "elf/bpf.h"
108 #include "elf/epiphany.h"
109 #include "elf/fr30.h"
110 #include "elf/frv.h"
111 #include "elf/ft32.h"
112 #include "elf/h8.h"
113 #include "elf/hppa.h"
114 #include "elf/i386.h"
115 #include "elf/i370.h"
116 #include "elf/i860.h"
117 #include "elf/i960.h"
118 #include "elf/ia64.h"
119 #include "elf/ip2k.h"
120 #include "elf/lm32.h"
121 #include "elf/iq2000.h"
122 #include "elf/m32c.h"
123 #include "elf/m32r.h"
124 #include "elf/m68k.h"
125 #include "elf/m68hc11.h"
126 #include "elf/s12z.h"
127 #include "elf/mcore.h"
128 #include "elf/mep.h"
129 #include "elf/metag.h"
130 #include "elf/microblaze.h"
131 #include "elf/mips.h"
132 #include "elf/mmix.h"
133 #include "elf/mn10200.h"
134 #include "elf/mn10300.h"
135 #include "elf/moxie.h"
136 #include "elf/mt.h"
137 #include "elf/msp430.h"
138 #include "elf/nds32.h"
139 #include "elf/nfp.h"
140 #include "elf/nios2.h"
141 #include "elf/or1k.h"
142 #include "elf/pj.h"
143 #include "elf/ppc.h"
144 #include "elf/ppc64.h"
145 #include "elf/pru.h"
146 #include "elf/riscv.h"
147 #include "elf/rl78.h"
148 #include "elf/rx.h"
149 #include "elf/s390.h"
150 #include "elf/score.h"
151 #include "elf/sh.h"
152 #include "elf/sparc.h"
153 #include "elf/spu.h"
154 #include "elf/tic6x.h"
155 #include "elf/tilegx.h"
156 #include "elf/tilepro.h"
157 #include "elf/v850.h"
158 #include "elf/vax.h"
159 #include "elf/visium.h"
160 #include "elf/wasm32.h"
161 #include "elf/x86-64.h"
162 #include "elf/xgate.h"
163 #include "elf/xstormy16.h"
164 #include "elf/xtensa.h"
165 #include "elf/z80.h"
166 #include "elf/loongarch.h"
167
168 #include "getopt.h"
169 #include "libiberty.h"
170 #include "safe-ctype.h"
171 #include "filenames.h"
172
173 #ifndef offsetof
174 #define offsetof(TYPE, MEMBER) ((size_t) &(((TYPE *) 0)->MEMBER))
175 #endif
176
177 typedef struct elf_section_list
178 {
179 Elf_Internal_Shdr * hdr;
180 struct elf_section_list * next;
181 } elf_section_list;
182
183 /* Flag bits indicating particular types of dump. */
184 #define HEX_DUMP (1 << 0) /* The -x command line switch. */
185 #define DISASS_DUMP (1 << 1) /* The -i command line switch. */
186 #define DEBUG_DUMP (1 << 2) /* The -w command line switch. */
187 #define STRING_DUMP (1 << 3) /* The -p command line switch. */
188 #define RELOC_DUMP (1 << 4) /* The -R command line switch. */
189 #define CTF_DUMP (1 << 5) /* The --ctf command line switch. */
190
191 typedef unsigned char dump_type;
192
193 /* A linked list of the section names for which dumps were requested. */
194 struct dump_list_entry
195 {
196 char * name;
197 dump_type type;
198 struct dump_list_entry * next;
199 };
200
201 /* A dynamic array of flags indicating for which sections a dump
202 has been requested via command line switches. */
203 struct dump_data
204 {
205 dump_type * dump_sects;
206 unsigned int num_dump_sects;
207 };
208
209 static struct dump_data cmdline;
210
211 static struct dump_list_entry * dump_sects_byname;
212
213 char * program_name = "readelf";
214
215 static bool show_name = false;
216 static bool do_dynamic = false;
217 static bool do_syms = false;
218 static bool do_dyn_syms = false;
219 static bool do_lto_syms = false;
220 static bool do_reloc = false;
221 static bool do_sections = false;
222 static bool do_section_groups = false;
223 static bool do_section_details = false;
224 static bool do_segments = false;
225 static bool do_unwind = false;
226 static bool do_using_dynamic = false;
227 static bool do_header = false;
228 static bool do_dump = false;
229 static bool do_version = false;
230 static bool do_histogram = false;
231 static bool do_debugging = false;
232 static bool do_ctf = false;
233 static bool do_arch = false;
234 static bool do_notes = false;
235 static bool do_archive_index = false;
236 static bool check_all = false;
237 static bool is_32bit_elf = false;
238 static bool decompress_dumps = false;
239 static bool do_not_show_symbol_truncation = false;
240 static bool do_demangle = false; /* Pretty print C++ symbol names. */
241 static bool process_links = false;
242 static bool dump_any_debugging = false;
243 static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
244 static int sym_base = 0;
245
246 static char *dump_ctf_parent_name;
247 static char *dump_ctf_symtab_name;
248 static char *dump_ctf_strtab_name;
249
250 struct group_list
251 {
252 struct group_list * next;
253 unsigned int section_index;
254 };
255
256 struct group
257 {
258 struct group_list * root;
259 unsigned int group_index;
260 };
261
262 typedef struct filedata
263 {
264 const char * file_name;
265 bool is_separate;
266 FILE * handle;
267 uint64_t file_size;
268 Elf_Internal_Ehdr file_header;
269 unsigned long archive_file_offset;
270 unsigned long archive_file_size;
271 /* Everything below this point is cleared out by free_filedata. */
272 Elf_Internal_Shdr * section_headers;
273 Elf_Internal_Phdr * program_headers;
274 char * string_table;
275 unsigned long string_table_length;
276 unsigned long dynamic_addr;
277 uint64_t dynamic_size;
278 size_t dynamic_nent;
279 Elf_Internal_Dyn * dynamic_section;
280 Elf_Internal_Shdr * dynamic_strtab_section;
281 char * dynamic_strings;
282 unsigned long dynamic_strings_length;
283 Elf_Internal_Shdr * dynamic_symtab_section;
284 unsigned long num_dynamic_syms;
285 Elf_Internal_Sym * dynamic_symbols;
286 uint64_t version_info[16];
287 unsigned int dynamic_syminfo_nent;
288 Elf_Internal_Syminfo * dynamic_syminfo;
289 unsigned long dynamic_syminfo_offset;
290 uint64_t nbuckets;
291 uint64_t nchains;
292 uint64_t * buckets;
293 uint64_t * chains;
294 uint64_t ngnubuckets;
295 uint64_t ngnuchains;
296 uint64_t * gnubuckets;
297 uint64_t * gnuchains;
298 uint64_t * mipsxlat;
299 uint64_t gnusymidx;
300 char * program_interpreter;
301 uint64_t dynamic_info[DT_ENCODING];
302 uint64_t dynamic_info_DT_GNU_HASH;
303 uint64_t dynamic_info_DT_MIPS_XHASH;
304 elf_section_list * symtab_shndx_list;
305 size_t group_count;
306 struct group * section_groups;
307 struct group ** section_headers_groups;
308 /* A dynamic array of flags indicating for which sections a dump of
309 some kind has been requested. It is reset on a per-object file
310 basis and then initialised from the cmdline_dump_sects array,
311 the results of interpreting the -w switch, and the
312 dump_sects_byname list. */
313 struct dump_data dump;
314 } Filedata;
315
316 /* How to print a vma value. */
317 typedef enum print_mode
318 {
319 HEX,
320 HEX_5,
321 DEC,
322 DEC_5,
323 UNSIGNED,
324 UNSIGNED_5,
325 PREFIX_HEX,
326 PREFIX_HEX_5,
327 FULL_HEX,
328 LONG_HEX,
329 OCTAL,
330 OCTAL_5
331 }
332 print_mode;
333
334 typedef enum unicode_display_type
335 {
336 unicode_default = 0,
337 unicode_locale,
338 unicode_escape,
339 unicode_hex,
340 unicode_highlight,
341 unicode_invalid
342 } unicode_display_type;
343
344 static unicode_display_type unicode_display = unicode_default;
345
346 typedef enum
347 {
348 reltype_unknown,
349 reltype_rel,
350 reltype_rela,
351 reltype_relr
352 } relocation_type;
353
354 /* Versioned symbol info. */
355 enum versioned_symbol_info
356 {
357 symbol_undefined,
358 symbol_hidden,
359 symbol_public
360 };
361
362 static const char * get_symbol_version_string
363 (Filedata *, bool, const char *, unsigned long, unsigned,
364 Elf_Internal_Sym *, enum versioned_symbol_info *, unsigned short *);
365
366 #define UNKNOWN -1
367
368 static inline const char *
369 section_name (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
370 {
371 return filedata->string_table + hdr->sh_name;
372 }
373
374 static inline bool
375 section_name_valid (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
376 {
377 return (hdr != NULL
378 && filedata->string_table != NULL
379 && hdr->sh_name < filedata->string_table_length);
380 }
381
382 static inline const char *
383 section_name_print (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
384 {
385 if (hdr == NULL)
386 return _("<none>");
387 if (filedata->string_table == NULL)
388 return _("<no-strings>");
389 if (hdr->sh_name >= filedata->string_table_length)
390 return _("<corrupt>");
391 return section_name (filedata, hdr);
392 }
393
394 #define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */
395
396 static inline bool
397 valid_symbol_name (const char *strtab, size_t strtab_size, uint64_t offset)
398 {
399 return strtab != NULL && offset < strtab_size;
400 }
401
402 static inline bool
403 valid_dynamic_name (const Filedata *filedata, uint64_t offset)
404 {
405 return valid_symbol_name (filedata->dynamic_strings,
406 filedata->dynamic_strings_length, offset);
407 }
408
409 /* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
410 already been called and verified that the string exists. */
411 static inline const char *
412 get_dynamic_name (const Filedata *filedata, size_t offset)
413 {
414 return filedata->dynamic_strings + offset;
415 }
416
417 #define REMOVE_ARCH_BITS(ADDR) \
418 do \
419 { \
420 if (filedata->file_header.e_machine == EM_ARM) \
421 (ADDR) &= ~1; \
422 } \
423 while (0)
424
425 /* Get the correct GNU hash section name. */
426 #define GNU_HASH_SECTION_NAME(filedata) \
427 filedata->dynamic_info_DT_MIPS_XHASH ? ".MIPS.xhash" : ".gnu.hash"
428 \f
429 /* Retrieve NMEMB structures, each SIZE bytes long from FILEDATA starting at
430 OFFSET + the offset of the current archive member, if we are examining an
431 archive. Put the retrieved data into VAR, if it is not NULL. Otherwise
432 allocate a buffer using malloc and fill that. In either case return the
433 pointer to the start of the retrieved data or NULL if something went wrong.
434 If something does go wrong and REASON is not NULL then emit an error
435 message using REASON as part of the context. */
436
437 static void *
438 get_data (void *var,
439 Filedata *filedata,
440 unsigned long offset,
441 uint64_t size,
442 uint64_t nmemb,
443 const char *reason)
444 {
445 void * mvar;
446 uint64_t amt = size * nmemb;
447
448 if (size == 0 || nmemb == 0)
449 return NULL;
450
451 /* If size_t is smaller than uint64_t, eg because you are building
452 on a 32-bit host, then make sure that when the sizes are cast to
453 size_t no information is lost. */
454 if ((size_t) size != size
455 || (size_t) nmemb != nmemb
456 || (size_t) amt != amt
457 || amt / size != nmemb
458 || (size_t) amt + 1 == 0)
459 {
460 if (reason)
461 error (_("Size overflow prevents reading %" PRIu64
462 " elements of size %" PRIu64 " for %s\n"),
463 nmemb, size, reason);
464 return NULL;
465 }
466
467 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
468 attempting to allocate memory when the read is bound to fail. */
469 if (filedata->archive_file_offset > filedata->file_size
470 || offset > filedata->file_size - filedata->archive_file_offset
471 || amt > filedata->file_size - filedata->archive_file_offset - offset)
472 {
473 if (reason)
474 error (_("Reading %" PRIu64 " bytes extends past end of file for %s\n"),
475 amt, reason);
476 return NULL;
477 }
478
479 if (fseek (filedata->handle, filedata->archive_file_offset + offset,
480 SEEK_SET))
481 {
482 if (reason)
483 error (_("Unable to seek to 0x%lx for %s\n"),
484 filedata->archive_file_offset + offset, reason);
485 return NULL;
486 }
487
488 mvar = var;
489 if (mvar == NULL)
490 {
491 /* + 1 so that we can '\0' terminate invalid string table sections. */
492 mvar = malloc ((size_t) amt + 1);
493
494 if (mvar == NULL)
495 {
496 if (reason)
497 error (_("Out of memory allocating %" PRIu64 " bytes for %s\n"),
498 amt, reason);
499 return NULL;
500 }
501
502 ((char *) mvar)[amt] = '\0';
503 }
504
505 if (fread (mvar, (size_t) size, (size_t) nmemb, filedata->handle) != nmemb)
506 {
507 if (reason)
508 error (_("Unable to read in %" PRIu64 " bytes of %s\n"),
509 amt, reason);
510 if (mvar != var)
511 free (mvar);
512 return NULL;
513 }
514
515 return mvar;
516 }
517
518 /* Print a VMA value in the MODE specified.
519 Returns the number of characters displayed. */
520
521 static unsigned int
522 print_vma (uint64_t vma, print_mode mode)
523 {
524 unsigned int nc = 0;
525
526 switch (mode)
527 {
528 case FULL_HEX:
529 nc = printf ("0x");
530 /* Fall through. */
531 case LONG_HEX:
532 if (!is_32bit_elf)
533 return nc + printf ("%16.16" PRIx64, vma);
534 return nc + printf ("%8.8" PRIx64, vma);
535
536 case DEC_5:
537 if (vma <= 99999)
538 return printf ("%5" PRId64, vma);
539 /* Fall through. */
540 case PREFIX_HEX:
541 nc = printf ("0x");
542 /* Fall through. */
543 case HEX:
544 return nc + printf ("%" PRIx64, vma);
545
546 case PREFIX_HEX_5:
547 nc = printf ("0x");
548 /* Fall through. */
549 case HEX_5:
550 return nc + printf ("%05" PRIx64, vma);
551
552 case DEC:
553 return printf ("%" PRId64, vma);
554
555 case UNSIGNED:
556 return printf ("%" PRIu64, vma);
557
558 case UNSIGNED_5:
559 return printf ("%5" PRIu64, vma);
560
561 case OCTAL:
562 return printf ("%" PRIo64, vma);
563
564 case OCTAL_5:
565 return printf ("%5" PRIo64, vma);
566
567 default:
568 /* FIXME: Report unrecognised mode ? */
569 return 0;
570 }
571 }
572
573
574 /* Display a symbol on stdout. Handles the display of control characters and
575 multibye characters (assuming the host environment supports them).
576
577 Display at most abs(WIDTH) characters, truncating as necessary, unless do_wide is true.
578
579 If truncation will happen and do_not_show_symbol_truncation is FALSE then display
580 abs(WIDTH) - 5 characters followed by "[...]".
581
582 If WIDTH is negative then ensure that the output is at least (- WIDTH) characters,
583 padding as necessary.
584
585 Returns the number of emitted characters. */
586
587 static unsigned int
588 print_symbol (signed int width, const char * symbol)
589 {
590 bool extra_padding = false;
591 bool do_dots = false;
592 signed int num_printed = 0;
593 #ifdef HAVE_MBSTATE_T
594 mbstate_t state;
595 #endif
596 unsigned int width_remaining;
597 const void * alloced_symbol = NULL;
598
599 if (width < 0)
600 {
601 /* Keep the width positive. This helps the code below. */
602 width = - width;
603 extra_padding = true;
604 }
605 else if (width == 0)
606 return 0;
607
608 if (do_wide)
609 /* Set the remaining width to a very large value.
610 This simplifies the code below. */
611 width_remaining = INT_MAX;
612 else
613 {
614 width_remaining = width;
615 if (! do_not_show_symbol_truncation
616 && (int) strlen (symbol) > width)
617 {
618 width_remaining -= 5;
619 if ((int) width_remaining < 0)
620 width_remaining = 0;
621 do_dots = true;
622 }
623 }
624
625 #ifdef HAVE_MBSTATE_T
626 /* Initialise the multibyte conversion state. */
627 memset (& state, 0, sizeof (state));
628 #endif
629
630 if (do_demangle && *symbol)
631 {
632 const char * res = cplus_demangle (symbol, demangle_flags);
633
634 if (res != NULL)
635 alloced_symbol = symbol = res;
636 }
637
638 while (width_remaining)
639 {
640 size_t n;
641 const char c = *symbol++;
642
643 if (c == 0)
644 break;
645
646 if (ISPRINT (c))
647 {
648 putchar (c);
649 width_remaining --;
650 num_printed ++;
651 }
652 else if (ISCNTRL (c))
653 {
654 /* Do not print control characters directly as they can affect terminal
655 settings. Such characters usually appear in the names generated
656 by the assembler for local labels. */
657
658 if (width_remaining < 2)
659 break;
660
661 printf ("^%c", c + 0x40);
662 width_remaining -= 2;
663 num_printed += 2;
664 }
665 else if (c == 0x7f)
666 {
667 if (width_remaining < 5)
668 break;
669 printf ("<DEL>");
670 width_remaining -= 5;
671 num_printed += 5;
672 }
673 else if (unicode_display != unicode_locale
674 && unicode_display != unicode_default)
675 {
676 /* Display unicode characters as something else. */
677 unsigned char bytes[4];
678 bool is_utf8;
679 unsigned int nbytes;
680
681 bytes[0] = c;
682
683 if (bytes[0] < 0xc0)
684 {
685 nbytes = 1;
686 is_utf8 = false;
687 }
688 else
689 {
690 bytes[1] = *symbol++;
691
692 if ((bytes[1] & 0xc0) != 0x80)
693 {
694 is_utf8 = false;
695 /* Do not consume this character. It may only
696 be the first byte in the sequence that was
697 corrupt. */
698 --symbol;
699 nbytes = 1;
700 }
701 else if ((bytes[0] & 0x20) == 0)
702 {
703 is_utf8 = true;
704 nbytes = 2;
705 }
706 else
707 {
708 bytes[2] = *symbol++;
709
710 if ((bytes[2] & 0xc0) != 0x80)
711 {
712 is_utf8 = false;
713 symbol -= 2;
714 nbytes = 1;
715 }
716 else if ((bytes[0] & 0x10) == 0)
717 {
718 is_utf8 = true;
719 nbytes = 3;
720 }
721 else
722 {
723 bytes[3] = *symbol++;
724
725 nbytes = 4;
726
727 if ((bytes[3] & 0xc0) != 0x80)
728 {
729 is_utf8 = false;
730 symbol -= 3;
731 nbytes = 1;
732 }
733 else
734 is_utf8 = true;
735 }
736 }
737 }
738
739 if (unicode_display == unicode_invalid)
740 is_utf8 = false;
741
742 if (unicode_display == unicode_hex || ! is_utf8)
743 {
744 unsigned int i;
745
746 if (width_remaining < (nbytes * 2) + 2)
747 break;
748
749 putchar (is_utf8 ? '<' : '{');
750 printf ("0x");
751 for (i = 0; i < nbytes; i++)
752 printf ("%02x", bytes[i]);
753 putchar (is_utf8 ? '>' : '}');
754 }
755 else
756 {
757 if (unicode_display == unicode_highlight && isatty (1))
758 printf ("\x1B[31;47m"); /* Red. */
759
760 switch (nbytes)
761 {
762 case 2:
763 if (width_remaining < 6)
764 break;
765 printf ("\\u%02x%02x",
766 (bytes[0] & 0x1c) >> 2,
767 ((bytes[0] & 0x03) << 6) | (bytes[1] & 0x3f));
768 break;
769 case 3:
770 if (width_remaining < 6)
771 break;
772 printf ("\\u%02x%02x",
773 ((bytes[0] & 0x0f) << 4) | ((bytes[1] & 0x3c) >> 2),
774 ((bytes[1] & 0x03) << 6) | (bytes[2] & 0x3f));
775 break;
776 case 4:
777 if (width_remaining < 8)
778 break;
779 printf ("\\u%02x%02x%02x",
780 ((bytes[0] & 0x07) << 6) | ((bytes[1] & 0x3c) >> 2),
781 ((bytes[1] & 0x03) << 6) | ((bytes[2] & 0x3c) >> 2),
782 ((bytes[2] & 0x03) << 6) | (bytes[3] & 0x3f));
783
784 break;
785 default:
786 /* URG. */
787 break;
788 }
789
790 if (unicode_display == unicode_highlight && isatty (1))
791 printf ("\033[0m"); /* Default colour. */
792 }
793
794 if (bytes[nbytes - 1] == 0)
795 break;
796 }
797 else
798 {
799 #ifdef HAVE_MBSTATE_T
800 wchar_t w;
801 #endif
802 /* Let printf do the hard work of displaying multibyte characters. */
803 printf ("%.1s", symbol - 1);
804 width_remaining --;
805 num_printed ++;
806
807 #ifdef HAVE_MBSTATE_T
808 /* Try to find out how many bytes made up the character that was
809 just printed. Advance the symbol pointer past the bytes that
810 were displayed. */
811 n = mbrtowc (& w, symbol - 1, MB_CUR_MAX, & state);
812 #else
813 n = 1;
814 #endif
815 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
816 symbol += (n - 1);
817 }
818 }
819
820 if (do_dots)
821 num_printed += printf ("[...]");
822
823 if (extra_padding && num_printed < width)
824 {
825 /* Fill in the remaining spaces. */
826 printf ("%-*s", width - num_printed, " ");
827 num_printed = width;
828 }
829
830 free ((void *) alloced_symbol);
831 return num_printed;
832 }
833
834 /* Returns a pointer to a static buffer containing a printable version of
835 the given section's name. Like print_symbol, except that it does not try
836 to print multibyte characters, it just interprets them as hex values. */
837
838 static const char *
839 printable_section_name (Filedata * filedata, const Elf_Internal_Shdr * sec)
840 {
841 #define MAX_PRINT_SEC_NAME_LEN 256
842 static char sec_name_buf [MAX_PRINT_SEC_NAME_LEN + 1];
843 const char * name = section_name_print (filedata, sec);
844 char * buf = sec_name_buf;
845 char c;
846 unsigned int remaining = MAX_PRINT_SEC_NAME_LEN;
847
848 while ((c = * name ++) != 0)
849 {
850 if (ISCNTRL (c))
851 {
852 if (remaining < 2)
853 break;
854
855 * buf ++ = '^';
856 * buf ++ = c + 0x40;
857 remaining -= 2;
858 }
859 else if (ISPRINT (c))
860 {
861 * buf ++ = c;
862 remaining -= 1;
863 }
864 else
865 {
866 static char hex[17] = "0123456789ABCDEF";
867
868 if (remaining < 4)
869 break;
870 * buf ++ = '<';
871 * buf ++ = hex[(c & 0xf0) >> 4];
872 * buf ++ = hex[c & 0x0f];
873 * buf ++ = '>';
874 remaining -= 4;
875 }
876
877 if (remaining == 0)
878 break;
879 }
880
881 * buf = 0;
882 return sec_name_buf;
883 }
884
885 static const char *
886 printable_section_name_from_index (Filedata * filedata, unsigned long ndx)
887 {
888 if (ndx >= filedata->file_header.e_shnum)
889 return _("<corrupt>");
890
891 return printable_section_name (filedata, filedata->section_headers + ndx);
892 }
893
894 /* Return a pointer to section NAME, or NULL if no such section exists. */
895
896 static Elf_Internal_Shdr *
897 find_section (Filedata * filedata, const char * name)
898 {
899 unsigned int i;
900
901 if (filedata->section_headers == NULL)
902 return NULL;
903
904 for (i = 0; i < filedata->file_header.e_shnum; i++)
905 if (section_name_valid (filedata, filedata->section_headers + i)
906 && streq (section_name (filedata, filedata->section_headers + i),
907 name))
908 return filedata->section_headers + i;
909
910 return NULL;
911 }
912
913 /* Return a pointer to a section containing ADDR, or NULL if no such
914 section exists. */
915
916 static Elf_Internal_Shdr *
917 find_section_by_address (Filedata * filedata, uint64_t addr)
918 {
919 unsigned int i;
920
921 if (filedata->section_headers == NULL)
922 return NULL;
923
924 for (i = 0; i < filedata->file_header.e_shnum; i++)
925 {
926 Elf_Internal_Shdr *sec = filedata->section_headers + i;
927
928 if (addr >= sec->sh_addr && addr < sec->sh_addr + sec->sh_size)
929 return sec;
930 }
931
932 return NULL;
933 }
934
935 static Elf_Internal_Shdr *
936 find_section_by_type (Filedata * filedata, unsigned int type)
937 {
938 unsigned int i;
939
940 if (filedata->section_headers == NULL)
941 return NULL;
942
943 for (i = 0; i < filedata->file_header.e_shnum; i++)
944 {
945 Elf_Internal_Shdr *sec = filedata->section_headers + i;
946
947 if (sec->sh_type == type)
948 return sec;
949 }
950
951 return NULL;
952 }
953
954 /* Return a pointer to section NAME, or NULL if no such section exists,
955 restricted to the list of sections given in SET. */
956
957 static Elf_Internal_Shdr *
958 find_section_in_set (Filedata * filedata, const char * name, unsigned int * set)
959 {
960 unsigned int i;
961
962 if (filedata->section_headers == NULL)
963 return NULL;
964
965 if (set != NULL)
966 {
967 while ((i = *set++) > 0)
968 {
969 /* See PR 21156 for a reproducer. */
970 if (i >= filedata->file_header.e_shnum)
971 continue; /* FIXME: Should we issue an error message ? */
972
973 if (section_name_valid (filedata, filedata->section_headers + i)
974 && streq (section_name (filedata, filedata->section_headers + i),
975 name))
976 return filedata->section_headers + i;
977 }
978 }
979
980 return find_section (filedata, name);
981 }
982
983 /* Return TRUE if the current file is for IA-64 machine and OpenVMS ABI.
984 This OS has so many departures from the ELF standard that we test it at
985 many places. */
986
987 static inline bool
988 is_ia64_vms (Filedata * filedata)
989 {
990 return filedata->file_header.e_machine == EM_IA_64
991 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS;
992 }
993
994 /* Guess the relocation size commonly used by the specific machines. */
995
996 static bool
997 guess_is_rela (unsigned int e_machine)
998 {
999 switch (e_machine)
1000 {
1001 /* Targets that use REL relocations. */
1002 case EM_386:
1003 case EM_IAMCU:
1004 case EM_960:
1005 case EM_ARM:
1006 case EM_D10V:
1007 case EM_CYGNUS_D10V:
1008 case EM_DLX:
1009 case EM_MIPS:
1010 case EM_MIPS_RS3_LE:
1011 case EM_CYGNUS_M32R:
1012 case EM_SCORE:
1013 case EM_XGATE:
1014 case EM_NFP:
1015 case EM_BPF:
1016 return false;
1017
1018 /* Targets that use RELA relocations. */
1019 case EM_68K:
1020 case EM_860:
1021 case EM_AARCH64:
1022 case EM_ADAPTEVA_EPIPHANY:
1023 case EM_ALPHA:
1024 case EM_ALTERA_NIOS2:
1025 case EM_ARC:
1026 case EM_ARC_COMPACT:
1027 case EM_ARC_COMPACT2:
1028 case EM_AVR:
1029 case EM_AVR_OLD:
1030 case EM_BLACKFIN:
1031 case EM_CR16:
1032 case EM_CRIS:
1033 case EM_CRX:
1034 case EM_CSKY:
1035 case EM_D30V:
1036 case EM_CYGNUS_D30V:
1037 case EM_FR30:
1038 case EM_FT32:
1039 case EM_CYGNUS_FR30:
1040 case EM_CYGNUS_FRV:
1041 case EM_H8S:
1042 case EM_H8_300:
1043 case EM_H8_300H:
1044 case EM_IA_64:
1045 case EM_IP2K:
1046 case EM_IP2K_OLD:
1047 case EM_IQ2000:
1048 case EM_LATTICEMICO32:
1049 case EM_M32C_OLD:
1050 case EM_M32C:
1051 case EM_M32R:
1052 case EM_MCORE:
1053 case EM_CYGNUS_MEP:
1054 case EM_METAG:
1055 case EM_MMIX:
1056 case EM_MN10200:
1057 case EM_CYGNUS_MN10200:
1058 case EM_MN10300:
1059 case EM_CYGNUS_MN10300:
1060 case EM_MOXIE:
1061 case EM_MSP430:
1062 case EM_MSP430_OLD:
1063 case EM_MT:
1064 case EM_NDS32:
1065 case EM_NIOS32:
1066 case EM_OR1K:
1067 case EM_PPC64:
1068 case EM_PPC:
1069 case EM_TI_PRU:
1070 case EM_RISCV:
1071 case EM_RL78:
1072 case EM_RX:
1073 case EM_S390:
1074 case EM_S390_OLD:
1075 case EM_SH:
1076 case EM_SPARC:
1077 case EM_SPARC32PLUS:
1078 case EM_SPARCV9:
1079 case EM_SPU:
1080 case EM_TI_C6000:
1081 case EM_TILEGX:
1082 case EM_TILEPRO:
1083 case EM_V800:
1084 case EM_V850:
1085 case EM_CYGNUS_V850:
1086 case EM_VAX:
1087 case EM_VISIUM:
1088 case EM_X86_64:
1089 case EM_L1OM:
1090 case EM_K1OM:
1091 case EM_XSTORMY16:
1092 case EM_XTENSA:
1093 case EM_XTENSA_OLD:
1094 case EM_MICROBLAZE:
1095 case EM_MICROBLAZE_OLD:
1096 case EM_WEBASSEMBLY:
1097 return true;
1098
1099 case EM_68HC05:
1100 case EM_68HC08:
1101 case EM_68HC11:
1102 case EM_68HC16:
1103 case EM_FX66:
1104 case EM_ME16:
1105 case EM_MMA:
1106 case EM_NCPU:
1107 case EM_NDR1:
1108 case EM_PCP:
1109 case EM_ST100:
1110 case EM_ST19:
1111 case EM_ST7:
1112 case EM_ST9PLUS:
1113 case EM_STARCORE:
1114 case EM_SVX:
1115 case EM_TINYJ:
1116 default:
1117 warn (_("Don't know about relocations on this machine architecture\n"));
1118 return false;
1119 }
1120 }
1121
1122 /* Load RELA type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1123 Returns TRUE upon success, FALSE otherwise. If successful then a
1124 pointer to a malloc'ed buffer containing the relocs is placed in *RELASP,
1125 and the number of relocs loaded is placed in *NRELASP. It is the caller's
1126 responsibility to free the allocated buffer. */
1127
1128 static bool
1129 slurp_rela_relocs (Filedata * filedata,
1130 unsigned long rel_offset,
1131 unsigned long rel_size,
1132 Elf_Internal_Rela ** relasp,
1133 unsigned long * nrelasp)
1134 {
1135 Elf_Internal_Rela * relas;
1136 size_t nrelas;
1137 unsigned int i;
1138
1139 if (is_32bit_elf)
1140 {
1141 Elf32_External_Rela * erelas;
1142
1143 erelas = (Elf32_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1144 rel_size, _("32-bit relocation data"));
1145 if (!erelas)
1146 return false;
1147
1148 nrelas = rel_size / sizeof (Elf32_External_Rela);
1149
1150 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1151 sizeof (Elf_Internal_Rela));
1152
1153 if (relas == NULL)
1154 {
1155 free (erelas);
1156 error (_("out of memory parsing relocs\n"));
1157 return false;
1158 }
1159
1160 for (i = 0; i < nrelas; i++)
1161 {
1162 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1163 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1164 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1165 }
1166
1167 free (erelas);
1168 }
1169 else
1170 {
1171 Elf64_External_Rela * erelas;
1172
1173 erelas = (Elf64_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1174 rel_size, _("64-bit relocation data"));
1175 if (!erelas)
1176 return false;
1177
1178 nrelas = rel_size / sizeof (Elf64_External_Rela);
1179
1180 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1181 sizeof (Elf_Internal_Rela));
1182
1183 if (relas == NULL)
1184 {
1185 free (erelas);
1186 error (_("out of memory parsing relocs\n"));
1187 return false;
1188 }
1189
1190 for (i = 0; i < nrelas; i++)
1191 {
1192 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1193 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1194 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1195
1196 if (filedata->file_header.e_machine == EM_MIPS
1197 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1198 {
1199 /* In little-endian objects, r_info isn't really a
1200 64-bit little-endian value: it has a 32-bit
1201 little-endian symbol index followed by four
1202 individual byte fields. Reorder INFO
1203 accordingly. */
1204 uint64_t inf = relas[i].r_info;
1205 inf = (((inf & 0xffffffff) << 32)
1206 | ((inf >> 56) & 0xff)
1207 | ((inf >> 40) & 0xff00)
1208 | ((inf >> 24) & 0xff0000)
1209 | ((inf >> 8) & 0xff000000));
1210 relas[i].r_info = inf;
1211 }
1212 }
1213
1214 free (erelas);
1215 }
1216
1217 *relasp = relas;
1218 *nrelasp = nrelas;
1219 return true;
1220 }
1221
1222 /* Load REL type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1223 Returns TRUE upon success, FALSE otherwise. If successful then a
1224 pointer to a malloc'ed buffer containing the relocs is placed in *RELSP,
1225 and the number of relocs loaded is placed in *NRELSP. It is the caller's
1226 responsibility to free the allocated buffer. */
1227
1228 static bool
1229 slurp_rel_relocs (Filedata * filedata,
1230 unsigned long rel_offset,
1231 unsigned long rel_size,
1232 Elf_Internal_Rela ** relsp,
1233 unsigned long * nrelsp)
1234 {
1235 Elf_Internal_Rela * rels;
1236 size_t nrels;
1237 unsigned int i;
1238
1239 if (is_32bit_elf)
1240 {
1241 Elf32_External_Rel * erels;
1242
1243 erels = (Elf32_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1244 rel_size, _("32-bit relocation data"));
1245 if (!erels)
1246 return false;
1247
1248 nrels = rel_size / sizeof (Elf32_External_Rel);
1249
1250 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1251
1252 if (rels == NULL)
1253 {
1254 free (erels);
1255 error (_("out of memory parsing relocs\n"));
1256 return false;
1257 }
1258
1259 for (i = 0; i < nrels; i++)
1260 {
1261 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1262 rels[i].r_info = BYTE_GET (erels[i].r_info);
1263 rels[i].r_addend = 0;
1264 }
1265
1266 free (erels);
1267 }
1268 else
1269 {
1270 Elf64_External_Rel * erels;
1271
1272 erels = (Elf64_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1273 rel_size, _("64-bit relocation data"));
1274 if (!erels)
1275 return false;
1276
1277 nrels = rel_size / sizeof (Elf64_External_Rel);
1278
1279 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1280
1281 if (rels == NULL)
1282 {
1283 free (erels);
1284 error (_("out of memory parsing relocs\n"));
1285 return false;
1286 }
1287
1288 for (i = 0; i < nrels; i++)
1289 {
1290 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1291 rels[i].r_info = BYTE_GET (erels[i].r_info);
1292 rels[i].r_addend = 0;
1293
1294 if (filedata->file_header.e_machine == EM_MIPS
1295 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1296 {
1297 /* In little-endian objects, r_info isn't really a
1298 64-bit little-endian value: it has a 32-bit
1299 little-endian symbol index followed by four
1300 individual byte fields. Reorder INFO
1301 accordingly. */
1302 uint64_t inf = rels[i].r_info;
1303 inf = (((inf & 0xffffffff) << 32)
1304 | ((inf >> 56) & 0xff)
1305 | ((inf >> 40) & 0xff00)
1306 | ((inf >> 24) & 0xff0000)
1307 | ((inf >> 8) & 0xff000000));
1308 rels[i].r_info = inf;
1309 }
1310 }
1311
1312 free (erels);
1313 }
1314
1315 *relsp = rels;
1316 *nrelsp = nrels;
1317 return true;
1318 }
1319
1320 static bool
1321 slurp_relr_relocs (Filedata * filedata,
1322 unsigned long relr_offset,
1323 unsigned long relr_size,
1324 uint64_t ** relrsp,
1325 unsigned long * nrelrsp)
1326 {
1327 void *relrs;
1328 size_t size = 0, nentries, i;
1329 uint64_t base = 0, addr, entry;
1330
1331 relrs = get_data (NULL, filedata, relr_offset, 1, relr_size,
1332 _("RELR relocation data"));
1333 if (!relrs)
1334 return false;
1335
1336 if (is_32bit_elf)
1337 nentries = relr_size / sizeof (Elf32_External_Relr);
1338 else
1339 nentries = relr_size / sizeof (Elf64_External_Relr);
1340 for (i = 0; i < nentries; i++)
1341 {
1342 if (is_32bit_elf)
1343 entry = BYTE_GET (((Elf32_External_Relr *)relrs)[i].r_data);
1344 else
1345 entry = BYTE_GET (((Elf64_External_Relr *)relrs)[i].r_data);
1346 if ((entry & 1) == 0)
1347 size++;
1348 else
1349 while ((entry >>= 1) != 0)
1350 if ((entry & 1) == 1)
1351 size++;
1352 }
1353
1354 *relrsp = malloc (size * sizeof (**relrsp));
1355 if (*relrsp == NULL)
1356 {
1357 free (relrs);
1358 error (_("out of memory parsing relocs\n"));
1359 return false;
1360 }
1361
1362 size = 0;
1363 for (i = 0; i < nentries; i++)
1364 {
1365 const uint64_t entry_bytes = is_32bit_elf ? 4 : 8;
1366
1367 if (is_32bit_elf)
1368 entry = BYTE_GET (((Elf32_External_Relr *)relrs)[i].r_data);
1369 else
1370 entry = BYTE_GET (((Elf64_External_Relr *)relrs)[i].r_data);
1371 if ((entry & 1) == 0)
1372 {
1373 (*relrsp)[size++] = entry;
1374 base = entry + entry_bytes;
1375 }
1376 else
1377 {
1378 for (addr = base; (entry >>= 1) != 0; addr += entry_bytes)
1379 if ((entry & 1) != 0)
1380 (*relrsp)[size++] = addr;
1381 base += entry_bytes * (entry_bytes * CHAR_BIT - 1);
1382 }
1383 }
1384
1385 *nrelrsp = size;
1386 free (relrs);
1387 return true;
1388 }
1389
1390 /* Returns the reloc type extracted from the reloc info field. */
1391
1392 static unsigned int
1393 get_reloc_type (Filedata * filedata, uint64_t reloc_info)
1394 {
1395 if (is_32bit_elf)
1396 return ELF32_R_TYPE (reloc_info);
1397
1398 switch (filedata->file_header.e_machine)
1399 {
1400 case EM_MIPS:
1401 /* Note: We assume that reloc_info has already been adjusted for us. */
1402 return ELF64_MIPS_R_TYPE (reloc_info);
1403
1404 case EM_SPARCV9:
1405 return ELF64_R_TYPE_ID (reloc_info);
1406
1407 default:
1408 return ELF64_R_TYPE (reloc_info);
1409 }
1410 }
1411
1412 /* Return the symbol index extracted from the reloc info field. */
1413
1414 static uint64_t
1415 get_reloc_symindex (uint64_t reloc_info)
1416 {
1417 return is_32bit_elf ? ELF32_R_SYM (reloc_info) : ELF64_R_SYM (reloc_info);
1418 }
1419
1420 static inline bool
1421 uses_msp430x_relocs (Filedata * filedata)
1422 {
1423 return
1424 filedata->file_header.e_machine == EM_MSP430 /* Paranoia. */
1425 /* GCC uses osabi == ELFOSBI_STANDALONE. */
1426 && (((filedata->file_header.e_flags & EF_MSP430_MACH) == E_MSP430_MACH_MSP430X)
1427 /* TI compiler uses ELFOSABI_NONE. */
1428 || (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_NONE));
1429 }
1430
1431 /* Display the contents of the relocation data found at the specified
1432 offset. */
1433
1434 static bool
1435 dump_relocations (Filedata * filedata,
1436 unsigned long rel_offset,
1437 unsigned long rel_size,
1438 Elf_Internal_Sym * symtab,
1439 unsigned long nsyms,
1440 char * strtab,
1441 unsigned long strtablen,
1442 relocation_type rel_type,
1443 bool is_dynsym)
1444 {
1445 unsigned long i;
1446 Elf_Internal_Rela * rels;
1447 bool res = true;
1448
1449 if (rel_type == reltype_unknown)
1450 rel_type = guess_is_rela (filedata->file_header.e_machine) ? reltype_rela : reltype_rel;
1451
1452 if (rel_type == reltype_rela)
1453 {
1454 if (!slurp_rela_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1455 return false;
1456 }
1457 else if (rel_type == reltype_rel)
1458 {
1459 if (!slurp_rel_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1460 return false;
1461 }
1462 else if (rel_type == reltype_relr)
1463 {
1464 uint64_t * relrs;
1465 const char *format
1466 = is_32bit_elf ? "%08" PRIx64 "\n" : "%016" PRIx64 "\n";
1467
1468 if (!slurp_relr_relocs (filedata, rel_offset, rel_size, &relrs,
1469 &rel_size))
1470 return false;
1471
1472 printf (ngettext (" %lu offset\n", " %lu offsets\n", rel_size),
1473 rel_size);
1474 for (i = 0; i < rel_size; i++)
1475 printf (format, relrs[i]);
1476 free (relrs);
1477 return true;
1478 }
1479
1480 if (is_32bit_elf)
1481 {
1482 if (rel_type == reltype_rela)
1483 {
1484 if (do_wide)
1485 printf (_(" Offset Info Type Sym. Value Symbol's Name + Addend\n"));
1486 else
1487 printf (_(" Offset Info Type Sym.Value Sym. Name + Addend\n"));
1488 }
1489 else
1490 {
1491 if (do_wide)
1492 printf (_(" Offset Info Type Sym. Value Symbol's Name\n"));
1493 else
1494 printf (_(" Offset Info Type Sym.Value Sym. Name\n"));
1495 }
1496 }
1497 else
1498 {
1499 if (rel_type == reltype_rela)
1500 {
1501 if (do_wide)
1502 printf (_(" Offset Info Type Symbol's Value Symbol's Name + Addend\n"));
1503 else
1504 printf (_(" Offset Info Type Sym. Value Sym. Name + Addend\n"));
1505 }
1506 else
1507 {
1508 if (do_wide)
1509 printf (_(" Offset Info Type Symbol's Value Symbol's Name\n"));
1510 else
1511 printf (_(" Offset Info Type Sym. Value Sym. Name\n"));
1512 }
1513 }
1514
1515 for (i = 0; i < rel_size; i++)
1516 {
1517 const char * rtype;
1518 uint64_t offset;
1519 uint64_t inf;
1520 uint64_t symtab_index;
1521 uint64_t type;
1522
1523 offset = rels[i].r_offset;
1524 inf = rels[i].r_info;
1525
1526 type = get_reloc_type (filedata, inf);
1527 symtab_index = get_reloc_symindex (inf);
1528
1529 if (is_32bit_elf)
1530 {
1531 printf ("%8.8lx %8.8lx ",
1532 (unsigned long) offset & 0xffffffff,
1533 (unsigned long) inf & 0xffffffff);
1534 }
1535 else
1536 {
1537 printf (do_wide
1538 ? "%16.16" PRIx64 " %16.16" PRIx64 " "
1539 : "%12.12" PRIx64 " %12.12" PRIx64 " ",
1540 offset, inf);
1541 }
1542
1543 switch (filedata->file_header.e_machine)
1544 {
1545 default:
1546 rtype = NULL;
1547 break;
1548
1549 case EM_AARCH64:
1550 rtype = elf_aarch64_reloc_type (type);
1551 break;
1552
1553 case EM_M32R:
1554 case EM_CYGNUS_M32R:
1555 rtype = elf_m32r_reloc_type (type);
1556 break;
1557
1558 case EM_386:
1559 case EM_IAMCU:
1560 rtype = elf_i386_reloc_type (type);
1561 break;
1562
1563 case EM_68HC11:
1564 case EM_68HC12:
1565 rtype = elf_m68hc11_reloc_type (type);
1566 break;
1567
1568 case EM_S12Z:
1569 rtype = elf_s12z_reloc_type (type);
1570 break;
1571
1572 case EM_68K:
1573 rtype = elf_m68k_reloc_type (type);
1574 break;
1575
1576 case EM_960:
1577 rtype = elf_i960_reloc_type (type);
1578 break;
1579
1580 case EM_AVR:
1581 case EM_AVR_OLD:
1582 rtype = elf_avr_reloc_type (type);
1583 break;
1584
1585 case EM_OLD_SPARCV9:
1586 case EM_SPARC32PLUS:
1587 case EM_SPARCV9:
1588 case EM_SPARC:
1589 rtype = elf_sparc_reloc_type (type);
1590 break;
1591
1592 case EM_SPU:
1593 rtype = elf_spu_reloc_type (type);
1594 break;
1595
1596 case EM_V800:
1597 rtype = v800_reloc_type (type);
1598 break;
1599 case EM_V850:
1600 case EM_CYGNUS_V850:
1601 rtype = v850_reloc_type (type);
1602 break;
1603
1604 case EM_D10V:
1605 case EM_CYGNUS_D10V:
1606 rtype = elf_d10v_reloc_type (type);
1607 break;
1608
1609 case EM_D30V:
1610 case EM_CYGNUS_D30V:
1611 rtype = elf_d30v_reloc_type (type);
1612 break;
1613
1614 case EM_DLX:
1615 rtype = elf_dlx_reloc_type (type);
1616 break;
1617
1618 case EM_SH:
1619 rtype = elf_sh_reloc_type (type);
1620 break;
1621
1622 case EM_MN10300:
1623 case EM_CYGNUS_MN10300:
1624 rtype = elf_mn10300_reloc_type (type);
1625 break;
1626
1627 case EM_MN10200:
1628 case EM_CYGNUS_MN10200:
1629 rtype = elf_mn10200_reloc_type (type);
1630 break;
1631
1632 case EM_FR30:
1633 case EM_CYGNUS_FR30:
1634 rtype = elf_fr30_reloc_type (type);
1635 break;
1636
1637 case EM_CYGNUS_FRV:
1638 rtype = elf_frv_reloc_type (type);
1639 break;
1640
1641 case EM_CSKY:
1642 rtype = elf_csky_reloc_type (type);
1643 break;
1644
1645 case EM_FT32:
1646 rtype = elf_ft32_reloc_type (type);
1647 break;
1648
1649 case EM_MCORE:
1650 rtype = elf_mcore_reloc_type (type);
1651 break;
1652
1653 case EM_MMIX:
1654 rtype = elf_mmix_reloc_type (type);
1655 break;
1656
1657 case EM_MOXIE:
1658 rtype = elf_moxie_reloc_type (type);
1659 break;
1660
1661 case EM_MSP430:
1662 if (uses_msp430x_relocs (filedata))
1663 {
1664 rtype = elf_msp430x_reloc_type (type);
1665 break;
1666 }
1667 /* Fall through. */
1668 case EM_MSP430_OLD:
1669 rtype = elf_msp430_reloc_type (type);
1670 break;
1671
1672 case EM_NDS32:
1673 rtype = elf_nds32_reloc_type (type);
1674 break;
1675
1676 case EM_PPC:
1677 rtype = elf_ppc_reloc_type (type);
1678 break;
1679
1680 case EM_PPC64:
1681 rtype = elf_ppc64_reloc_type (type);
1682 break;
1683
1684 case EM_MIPS:
1685 case EM_MIPS_RS3_LE:
1686 rtype = elf_mips_reloc_type (type);
1687 break;
1688
1689 case EM_RISCV:
1690 rtype = elf_riscv_reloc_type (type);
1691 break;
1692
1693 case EM_ALPHA:
1694 rtype = elf_alpha_reloc_type (type);
1695 break;
1696
1697 case EM_ARM:
1698 rtype = elf_arm_reloc_type (type);
1699 break;
1700
1701 case EM_ARC:
1702 case EM_ARC_COMPACT:
1703 case EM_ARC_COMPACT2:
1704 rtype = elf_arc_reloc_type (type);
1705 break;
1706
1707 case EM_PARISC:
1708 rtype = elf_hppa_reloc_type (type);
1709 break;
1710
1711 case EM_H8_300:
1712 case EM_H8_300H:
1713 case EM_H8S:
1714 rtype = elf_h8_reloc_type (type);
1715 break;
1716
1717 case EM_OR1K:
1718 rtype = elf_or1k_reloc_type (type);
1719 break;
1720
1721 case EM_PJ:
1722 case EM_PJ_OLD:
1723 rtype = elf_pj_reloc_type (type);
1724 break;
1725 case EM_IA_64:
1726 rtype = elf_ia64_reloc_type (type);
1727 break;
1728
1729 case EM_CRIS:
1730 rtype = elf_cris_reloc_type (type);
1731 break;
1732
1733 case EM_860:
1734 rtype = elf_i860_reloc_type (type);
1735 break;
1736
1737 case EM_X86_64:
1738 case EM_L1OM:
1739 case EM_K1OM:
1740 rtype = elf_x86_64_reloc_type (type);
1741 break;
1742
1743 case EM_S370:
1744 rtype = i370_reloc_type (type);
1745 break;
1746
1747 case EM_S390_OLD:
1748 case EM_S390:
1749 rtype = elf_s390_reloc_type (type);
1750 break;
1751
1752 case EM_SCORE:
1753 rtype = elf_score_reloc_type (type);
1754 break;
1755
1756 case EM_XSTORMY16:
1757 rtype = elf_xstormy16_reloc_type (type);
1758 break;
1759
1760 case EM_CRX:
1761 rtype = elf_crx_reloc_type (type);
1762 break;
1763
1764 case EM_VAX:
1765 rtype = elf_vax_reloc_type (type);
1766 break;
1767
1768 case EM_VISIUM:
1769 rtype = elf_visium_reloc_type (type);
1770 break;
1771
1772 case EM_BPF:
1773 rtype = elf_bpf_reloc_type (type);
1774 break;
1775
1776 case EM_ADAPTEVA_EPIPHANY:
1777 rtype = elf_epiphany_reloc_type (type);
1778 break;
1779
1780 case EM_IP2K:
1781 case EM_IP2K_OLD:
1782 rtype = elf_ip2k_reloc_type (type);
1783 break;
1784
1785 case EM_IQ2000:
1786 rtype = elf_iq2000_reloc_type (type);
1787 break;
1788
1789 case EM_XTENSA_OLD:
1790 case EM_XTENSA:
1791 rtype = elf_xtensa_reloc_type (type);
1792 break;
1793
1794 case EM_LATTICEMICO32:
1795 rtype = elf_lm32_reloc_type (type);
1796 break;
1797
1798 case EM_M32C_OLD:
1799 case EM_M32C:
1800 rtype = elf_m32c_reloc_type (type);
1801 break;
1802
1803 case EM_MT:
1804 rtype = elf_mt_reloc_type (type);
1805 break;
1806
1807 case EM_BLACKFIN:
1808 rtype = elf_bfin_reloc_type (type);
1809 break;
1810
1811 case EM_CYGNUS_MEP:
1812 rtype = elf_mep_reloc_type (type);
1813 break;
1814
1815 case EM_CR16:
1816 rtype = elf_cr16_reloc_type (type);
1817 break;
1818
1819 case EM_MICROBLAZE:
1820 case EM_MICROBLAZE_OLD:
1821 rtype = elf_microblaze_reloc_type (type);
1822 break;
1823
1824 case EM_RL78:
1825 rtype = elf_rl78_reloc_type (type);
1826 break;
1827
1828 case EM_RX:
1829 rtype = elf_rx_reloc_type (type);
1830 break;
1831
1832 case EM_METAG:
1833 rtype = elf_metag_reloc_type (type);
1834 break;
1835
1836 case EM_TI_C6000:
1837 rtype = elf_tic6x_reloc_type (type);
1838 break;
1839
1840 case EM_TILEGX:
1841 rtype = elf_tilegx_reloc_type (type);
1842 break;
1843
1844 case EM_TILEPRO:
1845 rtype = elf_tilepro_reloc_type (type);
1846 break;
1847
1848 case EM_WEBASSEMBLY:
1849 rtype = elf_wasm32_reloc_type (type);
1850 break;
1851
1852 case EM_XGATE:
1853 rtype = elf_xgate_reloc_type (type);
1854 break;
1855
1856 case EM_ALTERA_NIOS2:
1857 rtype = elf_nios2_reloc_type (type);
1858 break;
1859
1860 case EM_TI_PRU:
1861 rtype = elf_pru_reloc_type (type);
1862 break;
1863
1864 case EM_NFP:
1865 if (EF_NFP_MACH (filedata->file_header.e_flags) == E_NFP_MACH_3200)
1866 rtype = elf_nfp3200_reloc_type (type);
1867 else
1868 rtype = elf_nfp_reloc_type (type);
1869 break;
1870
1871 case EM_Z80:
1872 rtype = elf_z80_reloc_type (type);
1873 break;
1874
1875 case EM_LOONGARCH:
1876 rtype = elf_loongarch_reloc_type (type);
1877 break;
1878
1879 case EM_AMDGPU:
1880 rtype = elf_amdgpu_reloc_type (type);
1881 break;
1882 }
1883
1884 if (rtype == NULL)
1885 printf (_("unrecognized: %-7lx"), (unsigned long) type & 0xffffffff);
1886 else
1887 printf (do_wide ? "%-22s" : "%-17.17s", rtype);
1888
1889 if (filedata->file_header.e_machine == EM_ALPHA
1890 && rtype != NULL
1891 && streq (rtype, "R_ALPHA_LITUSE")
1892 && rel_type == reltype_rela)
1893 {
1894 switch (rels[i].r_addend)
1895 {
1896 case LITUSE_ALPHA_ADDR: rtype = "ADDR"; break;
1897 case LITUSE_ALPHA_BASE: rtype = "BASE"; break;
1898 case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
1899 case LITUSE_ALPHA_JSR: rtype = "JSR"; break;
1900 case LITUSE_ALPHA_TLSGD: rtype = "TLSGD"; break;
1901 case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
1902 case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
1903 default: rtype = NULL;
1904 }
1905
1906 if (rtype)
1907 printf (" (%s)", rtype);
1908 else
1909 {
1910 putchar (' ');
1911 printf (_("<unknown addend: %lx>"),
1912 (unsigned long) rels[i].r_addend);
1913 res = false;
1914 }
1915 }
1916 else if (symtab_index)
1917 {
1918 if (symtab == NULL || symtab_index >= nsyms)
1919 {
1920 error (_(" bad symbol index: %08lx in reloc\n"),
1921 (unsigned long) symtab_index);
1922 res = false;
1923 }
1924 else
1925 {
1926 Elf_Internal_Sym * psym;
1927 const char * version_string;
1928 enum versioned_symbol_info sym_info;
1929 unsigned short vna_other;
1930
1931 psym = symtab + symtab_index;
1932
1933 version_string
1934 = get_symbol_version_string (filedata, is_dynsym,
1935 strtab, strtablen,
1936 symtab_index,
1937 psym,
1938 &sym_info,
1939 &vna_other);
1940
1941 printf (" ");
1942
1943 if (ELF_ST_TYPE (psym->st_info) == STT_GNU_IFUNC)
1944 {
1945 const char * name;
1946 unsigned int len;
1947 unsigned int width = is_32bit_elf ? 8 : 14;
1948
1949 /* Relocations against GNU_IFUNC symbols do not use the value
1950 of the symbol as the address to relocate against. Instead
1951 they invoke the function named by the symbol and use its
1952 result as the address for relocation.
1953
1954 To indicate this to the user, do not display the value of
1955 the symbol in the "Symbols's Value" field. Instead show
1956 its name followed by () as a hint that the symbol is
1957 invoked. */
1958
1959 if (strtab == NULL
1960 || psym->st_name == 0
1961 || psym->st_name >= strtablen)
1962 name = "??";
1963 else
1964 name = strtab + psym->st_name;
1965
1966 len = print_symbol (width, name);
1967 if (version_string)
1968 printf (sym_info == symbol_public ? "@@%s" : "@%s",
1969 version_string);
1970 printf ("()%-*s", len <= width ? (width + 1) - len : 1, " ");
1971 }
1972 else
1973 {
1974 print_vma (psym->st_value, LONG_HEX);
1975
1976 printf (is_32bit_elf ? " " : " ");
1977 }
1978
1979 if (psym->st_name == 0)
1980 {
1981 const char * sec_name = "<null>";
1982 char name_buf[40];
1983
1984 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
1985 {
1986 if (psym->st_shndx < filedata->file_header.e_shnum
1987 && filedata->section_headers != NULL)
1988 sec_name = section_name_print (filedata,
1989 filedata->section_headers
1990 + psym->st_shndx);
1991 else if (psym->st_shndx == SHN_ABS)
1992 sec_name = "ABS";
1993 else if (psym->st_shndx == SHN_COMMON)
1994 sec_name = "COMMON";
1995 else if ((filedata->file_header.e_machine == EM_MIPS
1996 && psym->st_shndx == SHN_MIPS_SCOMMON)
1997 || (filedata->file_header.e_machine == EM_TI_C6000
1998 && psym->st_shndx == SHN_TIC6X_SCOMMON))
1999 sec_name = "SCOMMON";
2000 else if (filedata->file_header.e_machine == EM_MIPS
2001 && psym->st_shndx == SHN_MIPS_SUNDEFINED)
2002 sec_name = "SUNDEF";
2003 else if ((filedata->file_header.e_machine == EM_X86_64
2004 || filedata->file_header.e_machine == EM_L1OM
2005 || filedata->file_header.e_machine == EM_K1OM)
2006 && psym->st_shndx == SHN_X86_64_LCOMMON)
2007 sec_name = "LARGE_COMMON";
2008 else if (filedata->file_header.e_machine == EM_IA_64
2009 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
2010 && psym->st_shndx == SHN_IA_64_ANSI_COMMON)
2011 sec_name = "ANSI_COM";
2012 else if (is_ia64_vms (filedata)
2013 && psym->st_shndx == SHN_IA_64_VMS_SYMVEC)
2014 sec_name = "VMS_SYMVEC";
2015 else
2016 {
2017 sprintf (name_buf, "<section 0x%x>",
2018 (unsigned int) psym->st_shndx);
2019 sec_name = name_buf;
2020 }
2021 }
2022 print_symbol (22, sec_name);
2023 }
2024 else if (strtab == NULL)
2025 printf (_("<string table index: %3ld>"), psym->st_name);
2026 else if (psym->st_name >= strtablen)
2027 {
2028 error (_("<corrupt string table index: %3ld>\n"),
2029 psym->st_name);
2030 res = false;
2031 }
2032 else
2033 {
2034 print_symbol (22, strtab + psym->st_name);
2035 if (version_string)
2036 printf (sym_info == symbol_public ? "@@%s" : "@%s",
2037 version_string);
2038 }
2039
2040 if (rel_type == reltype_rela)
2041 {
2042 uint64_t off = rels[i].r_addend;
2043
2044 if ((int64_t) off < 0)
2045 printf (" - %" PRIx64, -off);
2046 else
2047 printf (" + %" PRIx64, off);
2048 }
2049 }
2050 }
2051 else if (rel_type == reltype_rela)
2052 {
2053 uint64_t off = rels[i].r_addend;
2054
2055 printf ("%*c", is_32bit_elf ? 12 : 20, ' ');
2056 if ((int64_t) off < 0)
2057 printf ("-%" PRIx64, -off);
2058 else
2059 printf ("%" PRIx64, off);
2060 }
2061
2062 if (filedata->file_header.e_machine == EM_SPARCV9
2063 && rtype != NULL
2064 && streq (rtype, "R_SPARC_OLO10"))
2065 printf (" + %lx", (unsigned long) ELF64_R_TYPE_DATA (inf));
2066
2067 putchar ('\n');
2068
2069 if (! is_32bit_elf && filedata->file_header.e_machine == EM_MIPS)
2070 {
2071 uint64_t type2 = ELF64_MIPS_R_TYPE2 (inf);
2072 uint64_t type3 = ELF64_MIPS_R_TYPE3 (inf);
2073 const char * rtype2 = elf_mips_reloc_type (type2);
2074 const char * rtype3 = elf_mips_reloc_type (type3);
2075
2076 printf (" Type2: ");
2077
2078 if (rtype2 == NULL)
2079 printf (_("unrecognized: %-7lx"),
2080 (unsigned long) type2 & 0xffffffff);
2081 else
2082 printf ("%-17.17s", rtype2);
2083
2084 printf ("\n Type3: ");
2085
2086 if (rtype3 == NULL)
2087 printf (_("unrecognized: %-7lx"),
2088 (unsigned long) type3 & 0xffffffff);
2089 else
2090 printf ("%-17.17s", rtype3);
2091
2092 putchar ('\n');
2093 }
2094 }
2095
2096 free (rels);
2097
2098 return res;
2099 }
2100
2101 static const char *
2102 get_aarch64_dynamic_type (unsigned long type)
2103 {
2104 switch (type)
2105 {
2106 case DT_AARCH64_BTI_PLT: return "AARCH64_BTI_PLT";
2107 case DT_AARCH64_PAC_PLT: return "AARCH64_PAC_PLT";
2108 case DT_AARCH64_VARIANT_PCS: return "AARCH64_VARIANT_PCS";
2109 default:
2110 return NULL;
2111 }
2112 }
2113
2114 static const char *
2115 get_mips_dynamic_type (unsigned long type)
2116 {
2117 switch (type)
2118 {
2119 case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
2120 case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
2121 case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
2122 case DT_MIPS_IVERSION: return "MIPS_IVERSION";
2123 case DT_MIPS_FLAGS: return "MIPS_FLAGS";
2124 case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
2125 case DT_MIPS_MSYM: return "MIPS_MSYM";
2126 case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
2127 case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
2128 case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
2129 case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
2130 case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
2131 case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
2132 case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
2133 case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
2134 case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
2135 case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
2136 case DT_MIPS_RLD_MAP_REL: return "MIPS_RLD_MAP_REL";
2137 case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
2138 case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
2139 case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
2140 case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
2141 case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
2142 case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
2143 case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
2144 case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
2145 case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
2146 case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
2147 case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
2148 case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
2149 case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
2150 case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
2151 case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
2152 case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
2153 case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
2154 case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
2155 case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
2156 case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
2157 case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
2158 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
2159 case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
2160 case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
2161 case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
2162 case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
2163 case DT_MIPS_PLTGOT: return "MIPS_PLTGOT";
2164 case DT_MIPS_RWPLT: return "MIPS_RWPLT";
2165 case DT_MIPS_XHASH: return "MIPS_XHASH";
2166 default:
2167 return NULL;
2168 }
2169 }
2170
2171 static const char *
2172 get_sparc64_dynamic_type (unsigned long type)
2173 {
2174 switch (type)
2175 {
2176 case DT_SPARC_REGISTER: return "SPARC_REGISTER";
2177 default:
2178 return NULL;
2179 }
2180 }
2181
2182 static const char *
2183 get_ppc_dynamic_type (unsigned long type)
2184 {
2185 switch (type)
2186 {
2187 case DT_PPC_GOT: return "PPC_GOT";
2188 case DT_PPC_OPT: return "PPC_OPT";
2189 default:
2190 return NULL;
2191 }
2192 }
2193
2194 static const char *
2195 get_ppc64_dynamic_type (unsigned long type)
2196 {
2197 switch (type)
2198 {
2199 case DT_PPC64_GLINK: return "PPC64_GLINK";
2200 case DT_PPC64_OPD: return "PPC64_OPD";
2201 case DT_PPC64_OPDSZ: return "PPC64_OPDSZ";
2202 case DT_PPC64_OPT: return "PPC64_OPT";
2203 default:
2204 return NULL;
2205 }
2206 }
2207
2208 static const char *
2209 get_parisc_dynamic_type (unsigned long type)
2210 {
2211 switch (type)
2212 {
2213 case DT_HP_LOAD_MAP: return "HP_LOAD_MAP";
2214 case DT_HP_DLD_FLAGS: return "HP_DLD_FLAGS";
2215 case DT_HP_DLD_HOOK: return "HP_DLD_HOOK";
2216 case DT_HP_UX10_INIT: return "HP_UX10_INIT";
2217 case DT_HP_UX10_INITSZ: return "HP_UX10_INITSZ";
2218 case DT_HP_PREINIT: return "HP_PREINIT";
2219 case DT_HP_PREINITSZ: return "HP_PREINITSZ";
2220 case DT_HP_NEEDED: return "HP_NEEDED";
2221 case DT_HP_TIME_STAMP: return "HP_TIME_STAMP";
2222 case DT_HP_CHECKSUM: return "HP_CHECKSUM";
2223 case DT_HP_GST_SIZE: return "HP_GST_SIZE";
2224 case DT_HP_GST_VERSION: return "HP_GST_VERSION";
2225 case DT_HP_GST_HASHVAL: return "HP_GST_HASHVAL";
2226 case DT_HP_EPLTREL: return "HP_GST_EPLTREL";
2227 case DT_HP_EPLTRELSZ: return "HP_GST_EPLTRELSZ";
2228 case DT_HP_FILTERED: return "HP_FILTERED";
2229 case DT_HP_FILTER_TLS: return "HP_FILTER_TLS";
2230 case DT_HP_COMPAT_FILTERED: return "HP_COMPAT_FILTERED";
2231 case DT_HP_LAZYLOAD: return "HP_LAZYLOAD";
2232 case DT_HP_BIND_NOW_COUNT: return "HP_BIND_NOW_COUNT";
2233 case DT_PLT: return "PLT";
2234 case DT_PLT_SIZE: return "PLT_SIZE";
2235 case DT_DLT: return "DLT";
2236 case DT_DLT_SIZE: return "DLT_SIZE";
2237 default:
2238 return NULL;
2239 }
2240 }
2241
2242 static const char *
2243 get_ia64_dynamic_type (unsigned long type)
2244 {
2245 switch (type)
2246 {
2247 case DT_IA_64_PLT_RESERVE: return "IA_64_PLT_RESERVE";
2248 case DT_IA_64_VMS_SUBTYPE: return "VMS_SUBTYPE";
2249 case DT_IA_64_VMS_IMGIOCNT: return "VMS_IMGIOCNT";
2250 case DT_IA_64_VMS_LNKFLAGS: return "VMS_LNKFLAGS";
2251 case DT_IA_64_VMS_VIR_MEM_BLK_SIZ: return "VMS_VIR_MEM_BLK_SIZ";
2252 case DT_IA_64_VMS_IDENT: return "VMS_IDENT";
2253 case DT_IA_64_VMS_NEEDED_IDENT: return "VMS_NEEDED_IDENT";
2254 case DT_IA_64_VMS_IMG_RELA_CNT: return "VMS_IMG_RELA_CNT";
2255 case DT_IA_64_VMS_SEG_RELA_CNT: return "VMS_SEG_RELA_CNT";
2256 case DT_IA_64_VMS_FIXUP_RELA_CNT: return "VMS_FIXUP_RELA_CNT";
2257 case DT_IA_64_VMS_FIXUP_NEEDED: return "VMS_FIXUP_NEEDED";
2258 case DT_IA_64_VMS_SYMVEC_CNT: return "VMS_SYMVEC_CNT";
2259 case DT_IA_64_VMS_XLATED: return "VMS_XLATED";
2260 case DT_IA_64_VMS_STACKSIZE: return "VMS_STACKSIZE";
2261 case DT_IA_64_VMS_UNWINDSZ: return "VMS_UNWINDSZ";
2262 case DT_IA_64_VMS_UNWIND_CODSEG: return "VMS_UNWIND_CODSEG";
2263 case DT_IA_64_VMS_UNWIND_INFOSEG: return "VMS_UNWIND_INFOSEG";
2264 case DT_IA_64_VMS_LINKTIME: return "VMS_LINKTIME";
2265 case DT_IA_64_VMS_SEG_NO: return "VMS_SEG_NO";
2266 case DT_IA_64_VMS_SYMVEC_OFFSET: return "VMS_SYMVEC_OFFSET";
2267 case DT_IA_64_VMS_SYMVEC_SEG: return "VMS_SYMVEC_SEG";
2268 case DT_IA_64_VMS_UNWIND_OFFSET: return "VMS_UNWIND_OFFSET";
2269 case DT_IA_64_VMS_UNWIND_SEG: return "VMS_UNWIND_SEG";
2270 case DT_IA_64_VMS_STRTAB_OFFSET: return "VMS_STRTAB_OFFSET";
2271 case DT_IA_64_VMS_SYSVER_OFFSET: return "VMS_SYSVER_OFFSET";
2272 case DT_IA_64_VMS_IMG_RELA_OFF: return "VMS_IMG_RELA_OFF";
2273 case DT_IA_64_VMS_SEG_RELA_OFF: return "VMS_SEG_RELA_OFF";
2274 case DT_IA_64_VMS_FIXUP_RELA_OFF: return "VMS_FIXUP_RELA_OFF";
2275 case DT_IA_64_VMS_PLTGOT_OFFSET: return "VMS_PLTGOT_OFFSET";
2276 case DT_IA_64_VMS_PLTGOT_SEG: return "VMS_PLTGOT_SEG";
2277 case DT_IA_64_VMS_FPMODE: return "VMS_FPMODE";
2278 default:
2279 return NULL;
2280 }
2281 }
2282
2283 static const char *
2284 get_solaris_section_type (unsigned long type)
2285 {
2286 switch (type)
2287 {
2288 case 0x6fffffee: return "SUNW_ancillary";
2289 case 0x6fffffef: return "SUNW_capchain";
2290 case 0x6ffffff0: return "SUNW_capinfo";
2291 case 0x6ffffff1: return "SUNW_symsort";
2292 case 0x6ffffff2: return "SUNW_tlssort";
2293 case 0x6ffffff3: return "SUNW_LDYNSYM";
2294 case 0x6ffffff4: return "SUNW_dof";
2295 case 0x6ffffff5: return "SUNW_cap";
2296 case 0x6ffffff6: return "SUNW_SIGNATURE";
2297 case 0x6ffffff7: return "SUNW_ANNOTATE";
2298 case 0x6ffffff8: return "SUNW_DEBUGSTR";
2299 case 0x6ffffff9: return "SUNW_DEBUG";
2300 case 0x6ffffffa: return "SUNW_move";
2301 case 0x6ffffffb: return "SUNW_COMDAT";
2302 case 0x6ffffffc: return "SUNW_syminfo";
2303 case 0x6ffffffd: return "SUNW_verdef";
2304 case 0x6ffffffe: return "SUNW_verneed";
2305 case 0x6fffffff: return "SUNW_versym";
2306 case 0x70000000: return "SPARC_GOTDATA";
2307 default: return NULL;
2308 }
2309 }
2310
2311 static const char *
2312 get_alpha_dynamic_type (unsigned long type)
2313 {
2314 switch (type)
2315 {
2316 case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
2317 default: return NULL;
2318 }
2319 }
2320
2321 static const char *
2322 get_score_dynamic_type (unsigned long type)
2323 {
2324 switch (type)
2325 {
2326 case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
2327 case DT_SCORE_LOCAL_GOTNO: return "SCORE_LOCAL_GOTNO";
2328 case DT_SCORE_SYMTABNO: return "SCORE_SYMTABNO";
2329 case DT_SCORE_GOTSYM: return "SCORE_GOTSYM";
2330 case DT_SCORE_UNREFEXTNO: return "SCORE_UNREFEXTNO";
2331 case DT_SCORE_HIPAGENO: return "SCORE_HIPAGENO";
2332 default: return NULL;
2333 }
2334 }
2335
2336 static const char *
2337 get_tic6x_dynamic_type (unsigned long type)
2338 {
2339 switch (type)
2340 {
2341 case DT_C6000_GSYM_OFFSET: return "C6000_GSYM_OFFSET";
2342 case DT_C6000_GSTR_OFFSET: return "C6000_GSTR_OFFSET";
2343 case DT_C6000_DSBT_BASE: return "C6000_DSBT_BASE";
2344 case DT_C6000_DSBT_SIZE: return "C6000_DSBT_SIZE";
2345 case DT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
2346 case DT_C6000_DSBT_INDEX: return "C6000_DSBT_INDEX";
2347 default: return NULL;
2348 }
2349 }
2350
2351 static const char *
2352 get_nios2_dynamic_type (unsigned long type)
2353 {
2354 switch (type)
2355 {
2356 case DT_NIOS2_GP: return "NIOS2_GP";
2357 default: return NULL;
2358 }
2359 }
2360
2361 static const char *
2362 get_solaris_dynamic_type (unsigned long type)
2363 {
2364 switch (type)
2365 {
2366 case 0x6000000d: return "SUNW_AUXILIARY";
2367 case 0x6000000e: return "SUNW_RTLDINF";
2368 case 0x6000000f: return "SUNW_FILTER";
2369 case 0x60000010: return "SUNW_CAP";
2370 case 0x60000011: return "SUNW_SYMTAB";
2371 case 0x60000012: return "SUNW_SYMSZ";
2372 case 0x60000013: return "SUNW_SORTENT";
2373 case 0x60000014: return "SUNW_SYMSORT";
2374 case 0x60000015: return "SUNW_SYMSORTSZ";
2375 case 0x60000016: return "SUNW_TLSSORT";
2376 case 0x60000017: return "SUNW_TLSSORTSZ";
2377 case 0x60000018: return "SUNW_CAPINFO";
2378 case 0x60000019: return "SUNW_STRPAD";
2379 case 0x6000001a: return "SUNW_CAPCHAIN";
2380 case 0x6000001b: return "SUNW_LDMACH";
2381 case 0x6000001d: return "SUNW_CAPCHAINENT";
2382 case 0x6000001f: return "SUNW_CAPCHAINSZ";
2383 case 0x60000021: return "SUNW_PARENT";
2384 case 0x60000023: return "SUNW_ASLR";
2385 case 0x60000025: return "SUNW_RELAX";
2386 case 0x60000029: return "SUNW_NXHEAP";
2387 case 0x6000002b: return "SUNW_NXSTACK";
2388
2389 case 0x70000001: return "SPARC_REGISTER";
2390 case 0x7ffffffd: return "AUXILIARY";
2391 case 0x7ffffffe: return "USED";
2392 case 0x7fffffff: return "FILTER";
2393
2394 default: return NULL;
2395 }
2396 }
2397
2398 static const char *
2399 get_riscv_dynamic_type (unsigned long type)
2400 {
2401 switch (type)
2402 {
2403 case DT_RISCV_VARIANT_CC: return "RISCV_VARIANT_CC";
2404 default:
2405 return NULL;
2406 }
2407 }
2408
2409 static const char *
2410 get_dynamic_type (Filedata * filedata, unsigned long type)
2411 {
2412 static char buff[64];
2413
2414 switch (type)
2415 {
2416 case DT_NULL: return "NULL";
2417 case DT_NEEDED: return "NEEDED";
2418 case DT_PLTRELSZ: return "PLTRELSZ";
2419 case DT_PLTGOT: return "PLTGOT";
2420 case DT_HASH: return "HASH";
2421 case DT_STRTAB: return "STRTAB";
2422 case DT_SYMTAB: return "SYMTAB";
2423 case DT_RELA: return "RELA";
2424 case DT_RELASZ: return "RELASZ";
2425 case DT_RELAENT: return "RELAENT";
2426 case DT_STRSZ: return "STRSZ";
2427 case DT_SYMENT: return "SYMENT";
2428 case DT_INIT: return "INIT";
2429 case DT_FINI: return "FINI";
2430 case DT_SONAME: return "SONAME";
2431 case DT_RPATH: return "RPATH";
2432 case DT_SYMBOLIC: return "SYMBOLIC";
2433 case DT_REL: return "REL";
2434 case DT_RELSZ: return "RELSZ";
2435 case DT_RELENT: return "RELENT";
2436 case DT_RELR: return "RELR";
2437 case DT_RELRSZ: return "RELRSZ";
2438 case DT_RELRENT: return "RELRENT";
2439 case DT_PLTREL: return "PLTREL";
2440 case DT_DEBUG: return "DEBUG";
2441 case DT_TEXTREL: return "TEXTREL";
2442 case DT_JMPREL: return "JMPREL";
2443 case DT_BIND_NOW: return "BIND_NOW";
2444 case DT_INIT_ARRAY: return "INIT_ARRAY";
2445 case DT_FINI_ARRAY: return "FINI_ARRAY";
2446 case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
2447 case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
2448 case DT_RUNPATH: return "RUNPATH";
2449 case DT_FLAGS: return "FLAGS";
2450
2451 case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
2452 case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
2453 case DT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
2454
2455 case DT_CHECKSUM: return "CHECKSUM";
2456 case DT_PLTPADSZ: return "PLTPADSZ";
2457 case DT_MOVEENT: return "MOVEENT";
2458 case DT_MOVESZ: return "MOVESZ";
2459 case DT_FEATURE: return "FEATURE";
2460 case DT_POSFLAG_1: return "POSFLAG_1";
2461 case DT_SYMINSZ: return "SYMINSZ";
2462 case DT_SYMINENT: return "SYMINENT"; /* aka VALRNGHI */
2463
2464 case DT_ADDRRNGLO: return "ADDRRNGLO";
2465 case DT_CONFIG: return "CONFIG";
2466 case DT_DEPAUDIT: return "DEPAUDIT";
2467 case DT_AUDIT: return "AUDIT";
2468 case DT_PLTPAD: return "PLTPAD";
2469 case DT_MOVETAB: return "MOVETAB";
2470 case DT_SYMINFO: return "SYMINFO"; /* aka ADDRRNGHI */
2471
2472 case DT_VERSYM: return "VERSYM";
2473
2474 case DT_TLSDESC_GOT: return "TLSDESC_GOT";
2475 case DT_TLSDESC_PLT: return "TLSDESC_PLT";
2476 case DT_RELACOUNT: return "RELACOUNT";
2477 case DT_RELCOUNT: return "RELCOUNT";
2478 case DT_FLAGS_1: return "FLAGS_1";
2479 case DT_VERDEF: return "VERDEF";
2480 case DT_VERDEFNUM: return "VERDEFNUM";
2481 case DT_VERNEED: return "VERNEED";
2482 case DT_VERNEEDNUM: return "VERNEEDNUM";
2483
2484 case DT_AUXILIARY: return "AUXILIARY";
2485 case DT_USED: return "USED";
2486 case DT_FILTER: return "FILTER";
2487
2488 case DT_GNU_PRELINKED: return "GNU_PRELINKED";
2489 case DT_GNU_CONFLICT: return "GNU_CONFLICT";
2490 case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
2491 case DT_GNU_LIBLIST: return "GNU_LIBLIST";
2492 case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
2493 case DT_GNU_HASH: return "GNU_HASH";
2494 case DT_GNU_FLAGS_1: return "GNU_FLAGS_1";
2495
2496 default:
2497 if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
2498 {
2499 const char * result;
2500
2501 switch (filedata->file_header.e_machine)
2502 {
2503 case EM_AARCH64:
2504 result = get_aarch64_dynamic_type (type);
2505 break;
2506 case EM_MIPS:
2507 case EM_MIPS_RS3_LE:
2508 result = get_mips_dynamic_type (type);
2509 break;
2510 case EM_SPARCV9:
2511 result = get_sparc64_dynamic_type (type);
2512 break;
2513 case EM_PPC:
2514 result = get_ppc_dynamic_type (type);
2515 break;
2516 case EM_PPC64:
2517 result = get_ppc64_dynamic_type (type);
2518 break;
2519 case EM_IA_64:
2520 result = get_ia64_dynamic_type (type);
2521 break;
2522 case EM_ALPHA:
2523 result = get_alpha_dynamic_type (type);
2524 break;
2525 case EM_SCORE:
2526 result = get_score_dynamic_type (type);
2527 break;
2528 case EM_TI_C6000:
2529 result = get_tic6x_dynamic_type (type);
2530 break;
2531 case EM_ALTERA_NIOS2:
2532 result = get_nios2_dynamic_type (type);
2533 break;
2534 case EM_RISCV:
2535 result = get_riscv_dynamic_type (type);
2536 break;
2537 default:
2538 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2539 result = get_solaris_dynamic_type (type);
2540 else
2541 result = NULL;
2542 break;
2543 }
2544
2545 if (result != NULL)
2546 return result;
2547
2548 snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
2549 }
2550 else if (((type >= DT_LOOS) && (type <= DT_HIOS))
2551 || (filedata->file_header.e_machine == EM_PARISC
2552 && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
2553 {
2554 const char * result;
2555
2556 switch (filedata->file_header.e_machine)
2557 {
2558 case EM_PARISC:
2559 result = get_parisc_dynamic_type (type);
2560 break;
2561 case EM_IA_64:
2562 result = get_ia64_dynamic_type (type);
2563 break;
2564 default:
2565 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2566 result = get_solaris_dynamic_type (type);
2567 else
2568 result = NULL;
2569 break;
2570 }
2571
2572 if (result != NULL)
2573 return result;
2574
2575 snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
2576 type);
2577 }
2578 else
2579 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
2580
2581 return buff;
2582 }
2583 }
2584
2585 static bool get_program_headers (Filedata *);
2586 static bool get_dynamic_section (Filedata *);
2587
2588 static void
2589 locate_dynamic_section (Filedata *filedata)
2590 {
2591 unsigned long dynamic_addr = 0;
2592 uint64_t dynamic_size = 0;
2593
2594 if (filedata->file_header.e_phnum != 0
2595 && get_program_headers (filedata))
2596 {
2597 Elf_Internal_Phdr *segment;
2598 unsigned int i;
2599
2600 for (i = 0, segment = filedata->program_headers;
2601 i < filedata->file_header.e_phnum;
2602 i++, segment++)
2603 {
2604 if (segment->p_type == PT_DYNAMIC)
2605 {
2606 dynamic_addr = segment->p_offset;
2607 dynamic_size = segment->p_filesz;
2608
2609 if (filedata->section_headers != NULL)
2610 {
2611 Elf_Internal_Shdr *sec;
2612
2613 sec = find_section (filedata, ".dynamic");
2614 if (sec != NULL)
2615 {
2616 if (sec->sh_size == 0
2617 || sec->sh_type == SHT_NOBITS)
2618 {
2619 dynamic_addr = 0;
2620 dynamic_size = 0;
2621 }
2622 else
2623 {
2624 dynamic_addr = sec->sh_offset;
2625 dynamic_size = sec->sh_size;
2626 }
2627 }
2628 }
2629
2630 if (dynamic_addr > filedata->file_size
2631 || (dynamic_size > filedata->file_size - dynamic_addr))
2632 {
2633 dynamic_addr = 0;
2634 dynamic_size = 0;
2635 }
2636 break;
2637 }
2638 }
2639 }
2640 filedata->dynamic_addr = dynamic_addr;
2641 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
2642 }
2643
2644 static bool
2645 is_pie (Filedata *filedata)
2646 {
2647 Elf_Internal_Dyn *entry;
2648
2649 if (filedata->dynamic_size == 0)
2650 locate_dynamic_section (filedata);
2651 if (filedata->dynamic_size <= 1)
2652 return false;
2653
2654 if (!get_dynamic_section (filedata))
2655 return false;
2656
2657 for (entry = filedata->dynamic_section;
2658 entry < filedata->dynamic_section + filedata->dynamic_nent;
2659 entry++)
2660 {
2661 if (entry->d_tag == DT_FLAGS_1)
2662 {
2663 if ((entry->d_un.d_val & DF_1_PIE) != 0)
2664 return true;
2665 break;
2666 }
2667 }
2668 return false;
2669 }
2670
2671 static char *
2672 get_file_type (Filedata *filedata)
2673 {
2674 unsigned e_type = filedata->file_header.e_type;
2675 static char buff[64];
2676
2677 switch (e_type)
2678 {
2679 case ET_NONE: return _("NONE (None)");
2680 case ET_REL: return _("REL (Relocatable file)");
2681 case ET_EXEC: return _("EXEC (Executable file)");
2682 case ET_DYN:
2683 if (is_pie (filedata))
2684 return _("DYN (Position-Independent Executable file)");
2685 else
2686 return _("DYN (Shared object file)");
2687 case ET_CORE: return _("CORE (Core file)");
2688
2689 default:
2690 if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
2691 snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
2692 else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
2693 snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
2694 else
2695 snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
2696 return buff;
2697 }
2698 }
2699
2700 static char *
2701 get_machine_name (unsigned e_machine)
2702 {
2703 static char buff[64]; /* XXX */
2704
2705 switch (e_machine)
2706 {
2707 /* Please keep this switch table sorted by increasing EM_ value. */
2708 /* 0 */
2709 case EM_NONE: return _("None");
2710 case EM_M32: return "WE32100";
2711 case EM_SPARC: return "Sparc";
2712 case EM_386: return "Intel 80386";
2713 case EM_68K: return "MC68000";
2714 case EM_88K: return "MC88000";
2715 case EM_IAMCU: return "Intel MCU";
2716 case EM_860: return "Intel 80860";
2717 case EM_MIPS: return "MIPS R3000";
2718 case EM_S370: return "IBM System/370";
2719 /* 10 */
2720 case EM_MIPS_RS3_LE: return "MIPS R4000 big-endian";
2721 case EM_OLD_SPARCV9: return "Sparc v9 (old)";
2722 case EM_PARISC: return "HPPA";
2723 case EM_VPP550: return "Fujitsu VPP500";
2724 case EM_SPARC32PLUS: return "Sparc v8+" ;
2725 case EM_960: return "Intel 80960";
2726 case EM_PPC: return "PowerPC";
2727 /* 20 */
2728 case EM_PPC64: return "PowerPC64";
2729 case EM_S390_OLD:
2730 case EM_S390: return "IBM S/390";
2731 case EM_SPU: return "SPU";
2732 /* 30 */
2733 case EM_V800: return "Renesas V850 (using RH850 ABI)";
2734 case EM_FR20: return "Fujitsu FR20";
2735 case EM_RH32: return "TRW RH32";
2736 case EM_MCORE: return "MCORE";
2737 /* 40 */
2738 case EM_ARM: return "ARM";
2739 case EM_OLD_ALPHA: return "Digital Alpha (old)";
2740 case EM_SH: return "Renesas / SuperH SH";
2741 case EM_SPARCV9: return "Sparc v9";
2742 case EM_TRICORE: return "Siemens Tricore";
2743 case EM_ARC: return "ARC";
2744 case EM_H8_300: return "Renesas H8/300";
2745 case EM_H8_300H: return "Renesas H8/300H";
2746 case EM_H8S: return "Renesas H8S";
2747 case EM_H8_500: return "Renesas H8/500";
2748 /* 50 */
2749 case EM_IA_64: return "Intel IA-64";
2750 case EM_MIPS_X: return "Stanford MIPS-X";
2751 case EM_COLDFIRE: return "Motorola Coldfire";
2752 case EM_68HC12: return "Motorola MC68HC12 Microcontroller";
2753 case EM_MMA: return "Fujitsu Multimedia Accelerator";
2754 case EM_PCP: return "Siemens PCP";
2755 case EM_NCPU: return "Sony nCPU embedded RISC processor";
2756 case EM_NDR1: return "Denso NDR1 microprocesspr";
2757 case EM_STARCORE: return "Motorola Star*Core processor";
2758 case EM_ME16: return "Toyota ME16 processor";
2759 /* 60 */
2760 case EM_ST100: return "STMicroelectronics ST100 processor";
2761 case EM_TINYJ: return "Advanced Logic Corp. TinyJ embedded processor";
2762 case EM_X86_64: return "Advanced Micro Devices X86-64";
2763 case EM_PDSP: return "Sony DSP processor";
2764 case EM_PDP10: return "Digital Equipment Corp. PDP-10";
2765 case EM_PDP11: return "Digital Equipment Corp. PDP-11";
2766 case EM_FX66: return "Siemens FX66 microcontroller";
2767 case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 bit microcontroller";
2768 case EM_ST7: return "STMicroelectronics ST7 8-bit microcontroller";
2769 case EM_68HC16: return "Motorola MC68HC16 Microcontroller";
2770 /* 70 */
2771 case EM_68HC11: return "Motorola MC68HC11 Microcontroller";
2772 case EM_68HC08: return "Motorola MC68HC08 Microcontroller";
2773 case EM_68HC05: return "Motorola MC68HC05 Microcontroller";
2774 case EM_SVX: return "Silicon Graphics SVx";
2775 case EM_ST19: return "STMicroelectronics ST19 8-bit microcontroller";
2776 case EM_VAX: return "Digital VAX";
2777 case EM_CRIS: return "Axis Communications 32-bit embedded processor";
2778 case EM_JAVELIN: return "Infineon Technologies 32-bit embedded cpu";
2779 case EM_FIREPATH: return "Element 14 64-bit DSP processor";
2780 case EM_ZSP: return "LSI Logic's 16-bit DSP processor";
2781 /* 80 */
2782 case EM_MMIX: return "Donald Knuth's educational 64-bit processor";
2783 case EM_HUANY: return "Harvard Universitys's machine-independent object format";
2784 case EM_PRISM: return "Vitesse Prism";
2785 case EM_AVR_OLD:
2786 case EM_AVR: return "Atmel AVR 8-bit microcontroller";
2787 case EM_CYGNUS_FR30:
2788 case EM_FR30: return "Fujitsu FR30";
2789 case EM_CYGNUS_D10V:
2790 case EM_D10V: return "d10v";
2791 case EM_CYGNUS_D30V:
2792 case EM_D30V: return "d30v";
2793 case EM_CYGNUS_V850:
2794 case EM_V850: return "Renesas V850";
2795 case EM_CYGNUS_M32R:
2796 case EM_M32R: return "Renesas M32R (formerly Mitsubishi M32r)";
2797 case EM_CYGNUS_MN10300:
2798 case EM_MN10300: return "mn10300";
2799 /* 90 */
2800 case EM_CYGNUS_MN10200:
2801 case EM_MN10200: return "mn10200";
2802 case EM_PJ: return "picoJava";
2803 case EM_OR1K: return "OpenRISC 1000";
2804 case EM_ARC_COMPACT: return "ARCompact";
2805 case EM_XTENSA_OLD:
2806 case EM_XTENSA: return "Tensilica Xtensa Processor";
2807 case EM_VIDEOCORE: return "Alphamosaic VideoCore processor";
2808 case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor";
2809 case EM_NS32K: return "National Semiconductor 32000 series";
2810 case EM_TPC: return "Tenor Network TPC processor";
2811 case EM_SNP1K: return "Trebia SNP 1000 processor";
2812 /* 100 */
2813 case EM_ST200: return "STMicroelectronics ST200 microcontroller";
2814 case EM_IP2K_OLD:
2815 case EM_IP2K: return "Ubicom IP2xxx 8-bit microcontrollers";
2816 case EM_MAX: return "MAX Processor";
2817 case EM_CR: return "National Semiconductor CompactRISC";
2818 case EM_F2MC16: return "Fujitsu F2MC16";
2819 case EM_MSP430: return "Texas Instruments msp430 microcontroller";
2820 case EM_BLACKFIN: return "Analog Devices Blackfin";
2821 case EM_SE_C33: return "S1C33 Family of Seiko Epson processors";
2822 case EM_SEP: return "Sharp embedded microprocessor";
2823 case EM_ARCA: return "Arca RISC microprocessor";
2824 /* 110 */
2825 case EM_UNICORE: return "Unicore";
2826 case EM_EXCESS: return "eXcess 16/32/64-bit configurable embedded CPU";
2827 case EM_DXP: return "Icera Semiconductor Inc. Deep Execution Processor";
2828 case EM_ALTERA_NIOS2: return "Altera Nios II";
2829 case EM_CRX: return "National Semiconductor CRX microprocessor";
2830 case EM_XGATE: return "Motorola XGATE embedded processor";
2831 case EM_C166:
2832 case EM_XC16X: return "Infineon Technologies xc16x";
2833 case EM_M16C: return "Renesas M16C series microprocessors";
2834 case EM_DSPIC30F: return "Microchip Technology dsPIC30F Digital Signal Controller";
2835 case EM_CE: return "Freescale Communication Engine RISC core";
2836 /* 120 */
2837 case EM_M32C: return "Renesas M32c";
2838 /* 130 */
2839 case EM_TSK3000: return "Altium TSK3000 core";
2840 case EM_RS08: return "Freescale RS08 embedded processor";
2841 case EM_ECOG2: return "Cyan Technology eCOG2 microprocessor";
2842 case EM_SCORE: return "SUNPLUS S+Core";
2843 case EM_DSP24: return "New Japan Radio (NJR) 24-bit DSP Processor";
2844 case EM_VIDEOCORE3: return "Broadcom VideoCore III processor";
2845 case EM_LATTICEMICO32: return "Lattice Mico32";
2846 case EM_SE_C17: return "Seiko Epson C17 family";
2847 /* 140 */
2848 case EM_TI_C6000: return "Texas Instruments TMS320C6000 DSP family";
2849 case EM_TI_C2000: return "Texas Instruments TMS320C2000 DSP family";
2850 case EM_TI_C5500: return "Texas Instruments TMS320C55x DSP family";
2851 case EM_TI_PRU: return "TI PRU I/O processor";
2852 /* 160 */
2853 case EM_MMDSP_PLUS: return "STMicroelectronics 64bit VLIW Data Signal Processor";
2854 case EM_CYPRESS_M8C: return "Cypress M8C microprocessor";
2855 case EM_R32C: return "Renesas R32C series microprocessors";
2856 case EM_TRIMEDIA: return "NXP Semiconductors TriMedia architecture family";
2857 case EM_QDSP6: return "QUALCOMM DSP6 Processor";
2858 case EM_8051: return "Intel 8051 and variants";
2859 case EM_STXP7X: return "STMicroelectronics STxP7x family";
2860 case EM_NDS32: return "Andes Technology compact code size embedded RISC processor family";
2861 case EM_ECOG1X: return "Cyan Technology eCOG1X family";
2862 case EM_MAXQ30: return "Dallas Semiconductor MAXQ30 Core microcontrollers";
2863 /* 170 */
2864 case EM_XIMO16: return "New Japan Radio (NJR) 16-bit DSP Processor";
2865 case EM_MANIK: return "M2000 Reconfigurable RISC Microprocessor";
2866 case EM_CRAYNV2: return "Cray Inc. NV2 vector architecture";
2867 case EM_RX: return "Renesas RX";
2868 case EM_METAG: return "Imagination Technologies Meta processor architecture";
2869 case EM_MCST_ELBRUS: return "MCST Elbrus general purpose hardware architecture";
2870 case EM_ECOG16: return "Cyan Technology eCOG16 family";
2871 case EM_CR16:
2872 case EM_MICROBLAZE:
2873 case EM_MICROBLAZE_OLD: return "Xilinx MicroBlaze";
2874 case EM_ETPU: return "Freescale Extended Time Processing Unit";
2875 case EM_SLE9X: return "Infineon Technologies SLE9X core";
2876 /* 180 */
2877 case EM_L1OM: return "Intel L1OM";
2878 case EM_K1OM: return "Intel K1OM";
2879 case EM_INTEL182: return "Intel (reserved)";
2880 case EM_AARCH64: return "AArch64";
2881 case EM_ARM184: return "ARM (reserved)";
2882 case EM_AVR32: return "Atmel Corporation 32-bit microprocessor";
2883 case EM_STM8: return "STMicroeletronics STM8 8-bit microcontroller";
2884 case EM_TILE64: return "Tilera TILE64 multicore architecture family";
2885 case EM_TILEPRO: return "Tilera TILEPro multicore architecture family";
2886 /* 190 */
2887 case EM_CUDA: return "NVIDIA CUDA architecture";
2888 case EM_TILEGX: return "Tilera TILE-Gx multicore architecture family";
2889 case EM_CLOUDSHIELD: return "CloudShield architecture family";
2890 case EM_COREA_1ST: return "KIPO-KAIST Core-A 1st generation processor family";
2891 case EM_COREA_2ND: return "KIPO-KAIST Core-A 2nd generation processor family";
2892 case EM_ARC_COMPACT2: return "ARCv2";
2893 case EM_OPEN8: return "Open8 8-bit RISC soft processor core";
2894 case EM_RL78: return "Renesas RL78";
2895 case EM_VIDEOCORE5: return "Broadcom VideoCore V processor";
2896 case EM_78K0R: return "Renesas 78K0R";
2897 /* 200 */
2898 case EM_56800EX: return "Freescale 56800EX Digital Signal Controller (DSC)";
2899 case EM_BA1: return "Beyond BA1 CPU architecture";
2900 case EM_BA2: return "Beyond BA2 CPU architecture";
2901 case EM_XCORE: return "XMOS xCORE processor family";
2902 case EM_MCHP_PIC: return "Microchip 8-bit PIC(r) family";
2903 case EM_INTELGT: return "Intel Graphics Technology";
2904 /* 210 */
2905 case EM_KM32: return "KM211 KM32 32-bit processor";
2906 case EM_KMX32: return "KM211 KMX32 32-bit processor";
2907 case EM_KMX16: return "KM211 KMX16 16-bit processor";
2908 case EM_KMX8: return "KM211 KMX8 8-bit processor";
2909 case EM_KVARC: return "KM211 KVARC processor";
2910 case EM_CDP: return "Paneve CDP architecture family";
2911 case EM_COGE: return "Cognitive Smart Memory Processor";
2912 case EM_COOL: return "Bluechip Systems CoolEngine";
2913 case EM_NORC: return "Nanoradio Optimized RISC";
2914 case EM_CSR_KALIMBA: return "CSR Kalimba architecture family";
2915 /* 220 */
2916 case EM_Z80: return "Zilog Z80";
2917 case EM_VISIUM: return "CDS VISIUMcore processor";
2918 case EM_FT32: return "FTDI Chip FT32";
2919 case EM_MOXIE: return "Moxie";
2920 case EM_AMDGPU: return "AMD GPU";
2921 /* 230 (all reserved) */
2922 /* 240 */
2923 case EM_RISCV: return "RISC-V";
2924 case EM_LANAI: return "Lanai 32-bit processor";
2925 case EM_CEVA: return "CEVA Processor Architecture Family";
2926 case EM_CEVA_X2: return "CEVA X2 Processor Family";
2927 case EM_BPF: return "Linux BPF";
2928 case EM_GRAPHCORE_IPU: return "Graphcore Intelligent Processing Unit";
2929 case EM_IMG1: return "Imagination Technologies";
2930 /* 250 */
2931 case EM_NFP: return "Netronome Flow Processor";
2932 case EM_VE: return "NEC Vector Engine";
2933 case EM_CSKY: return "C-SKY";
2934 case EM_ARC_COMPACT3_64: return "Synopsys ARCv2.3 64-bit";
2935 case EM_MCS6502: return "MOS Technology MCS 6502 processor";
2936 case EM_ARC_COMPACT3: return "Synopsys ARCv2.3 32-bit";
2937 case EM_KVX: return "Kalray VLIW core of the MPPA processor family";
2938 case EM_65816: return "WDC 65816/65C816";
2939 case EM_LOONGARCH: return "LoongArch";
2940 case EM_KF32: return "ChipON KungFu32";
2941
2942 /* Large numbers... */
2943 case EM_MT: return "Morpho Techologies MT processor";
2944 case EM_ALPHA: return "Alpha";
2945 case EM_WEBASSEMBLY: return "Web Assembly";
2946 case EM_DLX: return "OpenDLX";
2947 case EM_XSTORMY16: return "Sanyo XStormy16 CPU core";
2948 case EM_IQ2000: return "Vitesse IQ2000";
2949 case EM_M32C_OLD:
2950 case EM_NIOS32: return "Altera Nios";
2951 case EM_CYGNUS_MEP: return "Toshiba MeP Media Engine";
2952 case EM_ADAPTEVA_EPIPHANY: return "Adapteva EPIPHANY";
2953 case EM_CYGNUS_FRV: return "Fujitsu FR-V";
2954 case EM_S12Z: return "Freescale S12Z";
2955
2956 default:
2957 snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
2958 return buff;
2959 }
2960 }
2961
2962 static void
2963 decode_ARC_machine_flags (unsigned e_flags, unsigned e_machine, char buf[])
2964 {
2965 /* ARC has two machine types EM_ARC_COMPACT and EM_ARC_COMPACT2. Some
2966 other compilers don't specify an architecture type in the e_flags, and
2967 instead use EM_ARC_COMPACT for old ARC600, ARC601, and ARC700
2968 architectures, and switch to EM_ARC_COMPACT2 for newer ARCEM and ARCHS
2969 architectures.
2970
2971 Th GNU tools follows this use of EM_ARC_COMPACT and EM_ARC_COMPACT2,
2972 but also sets a specific architecture type in the e_flags field.
2973
2974 However, when decoding the flags we don't worry if we see an
2975 unexpected pairing, for example EM_ARC_COMPACT machine type, with
2976 ARCEM architecture type. */
2977
2978 switch (e_flags & EF_ARC_MACH_MSK)
2979 {
2980 /* We only expect these to occur for EM_ARC_COMPACT2. */
2981 case EF_ARC_CPU_ARCV2EM:
2982 strcat (buf, ", ARC EM");
2983 break;
2984 case EF_ARC_CPU_ARCV2HS:
2985 strcat (buf, ", ARC HS");
2986 break;
2987
2988 /* We only expect these to occur for EM_ARC_COMPACT. */
2989 case E_ARC_MACH_ARC600:
2990 strcat (buf, ", ARC600");
2991 break;
2992 case E_ARC_MACH_ARC601:
2993 strcat (buf, ", ARC601");
2994 break;
2995 case E_ARC_MACH_ARC700:
2996 strcat (buf, ", ARC700");
2997 break;
2998
2999 /* The only times we should end up here are (a) A corrupt ELF, (b) A
3000 new ELF with new architecture being read by an old version of
3001 readelf, or (c) An ELF built with non-GNU compiler that does not
3002 set the architecture in the e_flags. */
3003 default:
3004 if (e_machine == EM_ARC_COMPACT)
3005 strcat (buf, ", Unknown ARCompact");
3006 else
3007 strcat (buf, ", Unknown ARC");
3008 break;
3009 }
3010
3011 switch (e_flags & EF_ARC_OSABI_MSK)
3012 {
3013 case E_ARC_OSABI_ORIG:
3014 strcat (buf, ", (ABI:legacy)");
3015 break;
3016 case E_ARC_OSABI_V2:
3017 strcat (buf, ", (ABI:v2)");
3018 break;
3019 /* Only upstream 3.9+ kernels will support ARCv2 ISA. */
3020 case E_ARC_OSABI_V3:
3021 strcat (buf, ", v3 no-legacy-syscalls ABI");
3022 break;
3023 case E_ARC_OSABI_V4:
3024 strcat (buf, ", v4 ABI");
3025 break;
3026 default:
3027 strcat (buf, ", unrecognised ARC OSABI flag");
3028 break;
3029 }
3030 }
3031
3032 static void
3033 decode_ARM_machine_flags (unsigned e_flags, char buf[])
3034 {
3035 unsigned eabi;
3036 bool unknown = false;
3037
3038 eabi = EF_ARM_EABI_VERSION (e_flags);
3039 e_flags &= ~ EF_ARM_EABIMASK;
3040
3041 /* Handle "generic" ARM flags. */
3042 if (e_flags & EF_ARM_RELEXEC)
3043 {
3044 strcat (buf, ", relocatable executable");
3045 e_flags &= ~ EF_ARM_RELEXEC;
3046 }
3047
3048 if (e_flags & EF_ARM_PIC)
3049 {
3050 strcat (buf, ", position independent");
3051 e_flags &= ~ EF_ARM_PIC;
3052 }
3053
3054 /* Now handle EABI specific flags. */
3055 switch (eabi)
3056 {
3057 default:
3058 strcat (buf, ", <unrecognized EABI>");
3059 if (e_flags)
3060 unknown = true;
3061 break;
3062
3063 case EF_ARM_EABI_VER1:
3064 strcat (buf, ", Version1 EABI");
3065 while (e_flags)
3066 {
3067 unsigned flag;
3068
3069 /* Process flags one bit at a time. */
3070 flag = e_flags & - e_flags;
3071 e_flags &= ~ flag;
3072
3073 switch (flag)
3074 {
3075 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
3076 strcat (buf, ", sorted symbol tables");
3077 break;
3078
3079 default:
3080 unknown = true;
3081 break;
3082 }
3083 }
3084 break;
3085
3086 case EF_ARM_EABI_VER2:
3087 strcat (buf, ", Version2 EABI");
3088 while (e_flags)
3089 {
3090 unsigned flag;
3091
3092 /* Process flags one bit at a time. */
3093 flag = e_flags & - e_flags;
3094 e_flags &= ~ flag;
3095
3096 switch (flag)
3097 {
3098 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
3099 strcat (buf, ", sorted symbol tables");
3100 break;
3101
3102 case EF_ARM_DYNSYMSUSESEGIDX:
3103 strcat (buf, ", dynamic symbols use segment index");
3104 break;
3105
3106 case EF_ARM_MAPSYMSFIRST:
3107 strcat (buf, ", mapping symbols precede others");
3108 break;
3109
3110 default:
3111 unknown = true;
3112 break;
3113 }
3114 }
3115 break;
3116
3117 case EF_ARM_EABI_VER3:
3118 strcat (buf, ", Version3 EABI");
3119 break;
3120
3121 case EF_ARM_EABI_VER4:
3122 strcat (buf, ", Version4 EABI");
3123 while (e_flags)
3124 {
3125 unsigned flag;
3126
3127 /* Process flags one bit at a time. */
3128 flag = e_flags & - e_flags;
3129 e_flags &= ~ flag;
3130
3131 switch (flag)
3132 {
3133 case EF_ARM_BE8:
3134 strcat (buf, ", BE8");
3135 break;
3136
3137 case EF_ARM_LE8:
3138 strcat (buf, ", LE8");
3139 break;
3140
3141 default:
3142 unknown = true;
3143 break;
3144 }
3145 }
3146 break;
3147
3148 case EF_ARM_EABI_VER5:
3149 strcat (buf, ", Version5 EABI");
3150 while (e_flags)
3151 {
3152 unsigned flag;
3153
3154 /* Process flags one bit at a time. */
3155 flag = e_flags & - e_flags;
3156 e_flags &= ~ flag;
3157
3158 switch (flag)
3159 {
3160 case EF_ARM_BE8:
3161 strcat (buf, ", BE8");
3162 break;
3163
3164 case EF_ARM_LE8:
3165 strcat (buf, ", LE8");
3166 break;
3167
3168 case EF_ARM_ABI_FLOAT_SOFT: /* Conflicts with EF_ARM_SOFT_FLOAT. */
3169 strcat (buf, ", soft-float ABI");
3170 break;
3171
3172 case EF_ARM_ABI_FLOAT_HARD: /* Conflicts with EF_ARM_VFP_FLOAT. */
3173 strcat (buf, ", hard-float ABI");
3174 break;
3175
3176 default:
3177 unknown = true;
3178 break;
3179 }
3180 }
3181 break;
3182
3183 case EF_ARM_EABI_UNKNOWN:
3184 strcat (buf, ", GNU EABI");
3185 while (e_flags)
3186 {
3187 unsigned flag;
3188
3189 /* Process flags one bit at a time. */
3190 flag = e_flags & - e_flags;
3191 e_flags &= ~ flag;
3192
3193 switch (flag)
3194 {
3195 case EF_ARM_INTERWORK:
3196 strcat (buf, ", interworking enabled");
3197 break;
3198
3199 case EF_ARM_APCS_26:
3200 strcat (buf, ", uses APCS/26");
3201 break;
3202
3203 case EF_ARM_APCS_FLOAT:
3204 strcat (buf, ", uses APCS/float");
3205 break;
3206
3207 case EF_ARM_PIC:
3208 strcat (buf, ", position independent");
3209 break;
3210
3211 case EF_ARM_ALIGN8:
3212 strcat (buf, ", 8 bit structure alignment");
3213 break;
3214
3215 case EF_ARM_NEW_ABI:
3216 strcat (buf, ", uses new ABI");
3217 break;
3218
3219 case EF_ARM_OLD_ABI:
3220 strcat (buf, ", uses old ABI");
3221 break;
3222
3223 case EF_ARM_SOFT_FLOAT:
3224 strcat (buf, ", software FP");
3225 break;
3226
3227 case EF_ARM_VFP_FLOAT:
3228 strcat (buf, ", VFP");
3229 break;
3230
3231 case EF_ARM_MAVERICK_FLOAT:
3232 strcat (buf, ", Maverick FP");
3233 break;
3234
3235 default:
3236 unknown = true;
3237 break;
3238 }
3239 }
3240 }
3241
3242 if (unknown)
3243 strcat (buf,_(", <unknown>"));
3244 }
3245
3246 static void
3247 decode_AVR_machine_flags (unsigned e_flags, char buf[], size_t size)
3248 {
3249 --size; /* Leave space for null terminator. */
3250
3251 switch (e_flags & EF_AVR_MACH)
3252 {
3253 case E_AVR_MACH_AVR1:
3254 strncat (buf, ", avr:1", size);
3255 break;
3256 case E_AVR_MACH_AVR2:
3257 strncat (buf, ", avr:2", size);
3258 break;
3259 case E_AVR_MACH_AVR25:
3260 strncat (buf, ", avr:25", size);
3261 break;
3262 case E_AVR_MACH_AVR3:
3263 strncat (buf, ", avr:3", size);
3264 break;
3265 case E_AVR_MACH_AVR31:
3266 strncat (buf, ", avr:31", size);
3267 break;
3268 case E_AVR_MACH_AVR35:
3269 strncat (buf, ", avr:35", size);
3270 break;
3271 case E_AVR_MACH_AVR4:
3272 strncat (buf, ", avr:4", size);
3273 break;
3274 case E_AVR_MACH_AVR5:
3275 strncat (buf, ", avr:5", size);
3276 break;
3277 case E_AVR_MACH_AVR51:
3278 strncat (buf, ", avr:51", size);
3279 break;
3280 case E_AVR_MACH_AVR6:
3281 strncat (buf, ", avr:6", size);
3282 break;
3283 case E_AVR_MACH_AVRTINY:
3284 strncat (buf, ", avr:100", size);
3285 break;
3286 case E_AVR_MACH_XMEGA1:
3287 strncat (buf, ", avr:101", size);
3288 break;
3289 case E_AVR_MACH_XMEGA2:
3290 strncat (buf, ", avr:102", size);
3291 break;
3292 case E_AVR_MACH_XMEGA3:
3293 strncat (buf, ", avr:103", size);
3294 break;
3295 case E_AVR_MACH_XMEGA4:
3296 strncat (buf, ", avr:104", size);
3297 break;
3298 case E_AVR_MACH_XMEGA5:
3299 strncat (buf, ", avr:105", size);
3300 break;
3301 case E_AVR_MACH_XMEGA6:
3302 strncat (buf, ", avr:106", size);
3303 break;
3304 case E_AVR_MACH_XMEGA7:
3305 strncat (buf, ", avr:107", size);
3306 break;
3307 default:
3308 strncat (buf, ", avr:<unknown>", size);
3309 break;
3310 }
3311
3312 size -= strlen (buf);
3313 if (e_flags & EF_AVR_LINKRELAX_PREPARED)
3314 strncat (buf, ", link-relax", size);
3315 }
3316
3317 static void
3318 decode_NDS32_machine_flags (unsigned e_flags, char buf[], size_t size)
3319 {
3320 unsigned abi;
3321 unsigned arch;
3322 unsigned config;
3323 unsigned version;
3324 bool has_fpu = false;
3325 unsigned int r = 0;
3326
3327 static const char *ABI_STRINGS[] =
3328 {
3329 "ABI v0", /* use r5 as return register; only used in N1213HC */
3330 "ABI v1", /* use r0 as return register */
3331 "ABI v2", /* use r0 as return register and don't reserve 24 bytes for arguments */
3332 "ABI v2fp", /* for FPU */
3333 "AABI",
3334 "ABI2 FP+"
3335 };
3336 static const char *VER_STRINGS[] =
3337 {
3338 "Andes ELF V1.3 or older",
3339 "Andes ELF V1.3.1",
3340 "Andes ELF V1.4"
3341 };
3342 static const char *ARCH_STRINGS[] =
3343 {
3344 "",
3345 "Andes Star v1.0",
3346 "Andes Star v2.0",
3347 "Andes Star v3.0",
3348 "Andes Star v3.0m"
3349 };
3350
3351 abi = EF_NDS_ABI & e_flags;
3352 arch = EF_NDS_ARCH & e_flags;
3353 config = EF_NDS_INST & e_flags;
3354 version = EF_NDS32_ELF_VERSION & e_flags;
3355
3356 memset (buf, 0, size);
3357
3358 switch (abi)
3359 {
3360 case E_NDS_ABI_V0:
3361 case E_NDS_ABI_V1:
3362 case E_NDS_ABI_V2:
3363 case E_NDS_ABI_V2FP:
3364 case E_NDS_ABI_AABI:
3365 case E_NDS_ABI_V2FP_PLUS:
3366 /* In case there are holes in the array. */
3367 r += snprintf (buf + r, size - r, ", %s", ABI_STRINGS[abi >> EF_NDS_ABI_SHIFT]);
3368 break;
3369
3370 default:
3371 r += snprintf (buf + r, size - r, ", <unrecognized ABI>");
3372 break;
3373 }
3374
3375 switch (version)
3376 {
3377 case E_NDS32_ELF_VER_1_2:
3378 case E_NDS32_ELF_VER_1_3:
3379 case E_NDS32_ELF_VER_1_4:
3380 r += snprintf (buf + r, size - r, ", %s", VER_STRINGS[version >> EF_NDS32_ELF_VERSION_SHIFT]);
3381 break;
3382
3383 default:
3384 r += snprintf (buf + r, size - r, ", <unrecognized ELF version number>");
3385 break;
3386 }
3387
3388 if (E_NDS_ABI_V0 == abi)
3389 {
3390 /* OLD ABI; only used in N1213HC, has performance extension 1. */
3391 r += snprintf (buf + r, size - r, ", Andes Star v1.0, N1213HC, MAC, PERF1");
3392 if (arch == E_NDS_ARCH_STAR_V1_0)
3393 r += snprintf (buf + r, size -r, ", 16b"); /* has 16-bit instructions */
3394 return;
3395 }
3396
3397 switch (arch)
3398 {
3399 case E_NDS_ARCH_STAR_V1_0:
3400 case E_NDS_ARCH_STAR_V2_0:
3401 case E_NDS_ARCH_STAR_V3_0:
3402 case E_NDS_ARCH_STAR_V3_M:
3403 r += snprintf (buf + r, size - r, ", %s", ARCH_STRINGS[arch >> EF_NDS_ARCH_SHIFT]);
3404 break;
3405
3406 default:
3407 r += snprintf (buf + r, size - r, ", <unrecognized architecture>");
3408 /* ARCH version determines how the e_flags are interpreted.
3409 If it is unknown, we cannot proceed. */
3410 return;
3411 }
3412
3413 /* Newer ABI; Now handle architecture specific flags. */
3414 if (arch == E_NDS_ARCH_STAR_V1_0)
3415 {
3416 if (config & E_NDS32_HAS_MFUSR_PC_INST)
3417 r += snprintf (buf + r, size -r, ", MFUSR_PC");
3418
3419 if (!(config & E_NDS32_HAS_NO_MAC_INST))
3420 r += snprintf (buf + r, size -r, ", MAC");
3421
3422 if (config & E_NDS32_HAS_DIV_INST)
3423 r += snprintf (buf + r, size -r, ", DIV");
3424
3425 if (config & E_NDS32_HAS_16BIT_INST)
3426 r += snprintf (buf + r, size -r, ", 16b");
3427 }
3428 else
3429 {
3430 if (config & E_NDS32_HAS_MFUSR_PC_INST)
3431 {
3432 if (version <= E_NDS32_ELF_VER_1_3)
3433 r += snprintf (buf + r, size -r, ", [B8]");
3434 else
3435 r += snprintf (buf + r, size -r, ", EX9");
3436 }
3437
3438 if (config & E_NDS32_HAS_MAC_DX_INST)
3439 r += snprintf (buf + r, size -r, ", MAC_DX");
3440
3441 if (config & E_NDS32_HAS_DIV_DX_INST)
3442 r += snprintf (buf + r, size -r, ", DIV_DX");
3443
3444 if (config & E_NDS32_HAS_16BIT_INST)
3445 {
3446 if (version <= E_NDS32_ELF_VER_1_3)
3447 r += snprintf (buf + r, size -r, ", 16b");
3448 else
3449 r += snprintf (buf + r, size -r, ", IFC");
3450 }
3451 }
3452
3453 if (config & E_NDS32_HAS_EXT_INST)
3454 r += snprintf (buf + r, size -r, ", PERF1");
3455
3456 if (config & E_NDS32_HAS_EXT2_INST)
3457 r += snprintf (buf + r, size -r, ", PERF2");
3458
3459 if (config & E_NDS32_HAS_FPU_INST)
3460 {
3461 has_fpu = true;
3462 r += snprintf (buf + r, size -r, ", FPU_SP");
3463 }
3464
3465 if (config & E_NDS32_HAS_FPU_DP_INST)
3466 {
3467 has_fpu = true;
3468 r += snprintf (buf + r, size -r, ", FPU_DP");
3469 }
3470
3471 if (config & E_NDS32_HAS_FPU_MAC_INST)
3472 {
3473 has_fpu = true;
3474 r += snprintf (buf + r, size -r, ", FPU_MAC");
3475 }
3476
3477 if (has_fpu)
3478 {
3479 switch ((config & E_NDS32_FPU_REG_CONF) >> E_NDS32_FPU_REG_CONF_SHIFT)
3480 {
3481 case E_NDS32_FPU_REG_8SP_4DP:
3482 r += snprintf (buf + r, size -r, ", FPU_REG:8/4");
3483 break;
3484 case E_NDS32_FPU_REG_16SP_8DP:
3485 r += snprintf (buf + r, size -r, ", FPU_REG:16/8");
3486 break;
3487 case E_NDS32_FPU_REG_32SP_16DP:
3488 r += snprintf (buf + r, size -r, ", FPU_REG:32/16");
3489 break;
3490 case E_NDS32_FPU_REG_32SP_32DP:
3491 r += snprintf (buf + r, size -r, ", FPU_REG:32/32");
3492 break;
3493 }
3494 }
3495
3496 if (config & E_NDS32_HAS_AUDIO_INST)
3497 r += snprintf (buf + r, size -r, ", AUDIO");
3498
3499 if (config & E_NDS32_HAS_STRING_INST)
3500 r += snprintf (buf + r, size -r, ", STR");
3501
3502 if (config & E_NDS32_HAS_REDUCED_REGS)
3503 r += snprintf (buf + r, size -r, ", 16REG");
3504
3505 if (config & E_NDS32_HAS_VIDEO_INST)
3506 {
3507 if (version <= E_NDS32_ELF_VER_1_3)
3508 r += snprintf (buf + r, size -r, ", VIDEO");
3509 else
3510 r += snprintf (buf + r, size -r, ", SATURATION");
3511 }
3512
3513 if (config & E_NDS32_HAS_ENCRIPT_INST)
3514 r += snprintf (buf + r, size -r, ", ENCRP");
3515
3516 if (config & E_NDS32_HAS_L2C_INST)
3517 r += snprintf (buf + r, size -r, ", L2C");
3518 }
3519
3520 static void
3521 decode_AMDGPU_machine_flags (Filedata *filedata, unsigned int e_flags,
3522 char *buf)
3523 {
3524 unsigned char *e_ident = filedata->file_header.e_ident;
3525 unsigned char osabi = e_ident[EI_OSABI];
3526 unsigned char abiversion = e_ident[EI_ABIVERSION];
3527 unsigned int mach;
3528
3529 /* HSA OS ABI v2 used a different encoding, but we don't need to support it,
3530 it has been deprecated for a while.
3531
3532 The PAL, MESA3D and NONE OS ABIs are not properly versioned, at the time
3533 of writing, they use the same flags as HSA v3, so the code below uses that
3534 assumption. */
3535 if (osabi == ELFOSABI_AMDGPU_HSA && abiversion < ELFABIVERSION_AMDGPU_HSA_V3)
3536 return;
3537
3538 mach = e_flags & EF_AMDGPU_MACH;
3539 switch (mach)
3540 {
3541 #define AMDGPU_CASE(code, string) \
3542 case code: strcat (buf, ", " string); break;
3543 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX600, "gfx600")
3544 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX601, "gfx601")
3545 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX700, "gfx700")
3546 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX701, "gfx701")
3547 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX702, "gfx702")
3548 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX703, "gfx703")
3549 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX704, "gfx704")
3550 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX801, "gfx801")
3551 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX802, "gfx802")
3552 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX803, "gfx803")
3553 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX810, "gfx810")
3554 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX900, "gfx900")
3555 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX902, "gfx902")
3556 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX904, "gfx904")
3557 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX906, "gfx906")
3558 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX908, "gfx908")
3559 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX909, "gfx909")
3560 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX90C, "gfx90c")
3561 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1010, "gfx1010")
3562 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1011, "gfx1011")
3563 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1012, "gfx1012")
3564 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1030, "gfx1030")
3565 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1031, "gfx1031")
3566 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1032, "gfx1032")
3567 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1033, "gfx1033")
3568 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX602, "gfx602")
3569 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX705, "gfx705")
3570 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX805, "gfx805")
3571 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1035, "gfx1035")
3572 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1034, "gfx1034")
3573 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX90A, "gfx90a")
3574 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX940, "gfx940")
3575 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1013, "gfx1013")
3576 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1036, "gfx1036")
3577 default:
3578 sprintf (buf, _(", <unknown AMDGPU GPU type: %#x>"), mach);
3579 break;
3580 #undef AMDGPU_CASE
3581 }
3582
3583 buf += strlen (buf);
3584 e_flags &= ~EF_AMDGPU_MACH;
3585
3586 if ((osabi == ELFOSABI_AMDGPU_HSA
3587 && abiversion == ELFABIVERSION_AMDGPU_HSA_V3)
3588 || osabi != ELFOSABI_AMDGPU_HSA)
3589 {
3590 /* For HSA v3 and other OS ABIs. */
3591 if (e_flags & EF_AMDGPU_FEATURE_XNACK_V3)
3592 {
3593 strcat (buf, ", xnack on");
3594 buf += strlen (buf);
3595 e_flags &= ~EF_AMDGPU_FEATURE_XNACK_V3;
3596 }
3597
3598 if (e_flags & EF_AMDGPU_FEATURE_SRAMECC_V3)
3599 {
3600 strcat (buf, ", sramecc on");
3601 buf += strlen (buf);
3602 e_flags &= ~EF_AMDGPU_FEATURE_SRAMECC_V3;
3603 }
3604 }
3605 else
3606 {
3607 /* For HSA v4+. */
3608 int xnack, sramecc;
3609
3610 xnack = e_flags & EF_AMDGPU_FEATURE_XNACK_V4;
3611 switch (xnack)
3612 {
3613 case EF_AMDGPU_FEATURE_XNACK_UNSUPPORTED_V4:
3614 break;
3615
3616 case EF_AMDGPU_FEATURE_XNACK_ANY_V4:
3617 strcat (buf, ", xnack any");
3618 break;
3619
3620 case EF_AMDGPU_FEATURE_XNACK_OFF_V4:
3621 strcat (buf, ", xnack off");
3622 break;
3623
3624 case EF_AMDGPU_FEATURE_XNACK_ON_V4:
3625 strcat (buf, ", xnack on");
3626 break;
3627
3628 default:
3629 sprintf (buf, _(", <unknown xnack value: %#x>"), xnack);
3630 break;
3631 }
3632
3633 buf += strlen (buf);
3634 e_flags &= ~EF_AMDGPU_FEATURE_XNACK_V4;
3635
3636 sramecc = e_flags & EF_AMDGPU_FEATURE_SRAMECC_V4;
3637 switch (sramecc)
3638 {
3639 case EF_AMDGPU_FEATURE_SRAMECC_UNSUPPORTED_V4:
3640 break;
3641
3642 case EF_AMDGPU_FEATURE_SRAMECC_ANY_V4:
3643 strcat (buf, ", sramecc any");
3644 break;
3645
3646 case EF_AMDGPU_FEATURE_SRAMECC_OFF_V4:
3647 strcat (buf, ", sramecc off");
3648 break;
3649
3650 case EF_AMDGPU_FEATURE_SRAMECC_ON_V4:
3651 strcat (buf, ", sramecc on");
3652 break;
3653
3654 default:
3655 sprintf (buf, _(", <unknown sramecc value: %#x>"), sramecc);
3656 break;
3657 }
3658
3659 buf += strlen (buf);
3660 e_flags &= ~EF_AMDGPU_FEATURE_SRAMECC_V4;
3661 }
3662
3663 if (e_flags != 0)
3664 sprintf (buf, _(", unknown flags bits: %#x"), e_flags);
3665 }
3666
3667 static char *
3668 get_machine_flags (Filedata * filedata, unsigned e_flags, unsigned e_machine)
3669 {
3670 static char buf[1024];
3671
3672 buf[0] = '\0';
3673
3674 if (e_flags)
3675 {
3676 switch (e_machine)
3677 {
3678 default:
3679 break;
3680
3681 case EM_ARC_COMPACT2:
3682 case EM_ARC_COMPACT:
3683 decode_ARC_machine_flags (e_flags, e_machine, buf);
3684 break;
3685
3686 case EM_ARM:
3687 decode_ARM_machine_flags (e_flags, buf);
3688 break;
3689
3690 case EM_AVR:
3691 decode_AVR_machine_flags (e_flags, buf, sizeof buf);
3692 break;
3693
3694 case EM_BLACKFIN:
3695 if (e_flags & EF_BFIN_PIC)
3696 strcat (buf, ", PIC");
3697
3698 if (e_flags & EF_BFIN_FDPIC)
3699 strcat (buf, ", FDPIC");
3700
3701 if (e_flags & EF_BFIN_CODE_IN_L1)
3702 strcat (buf, ", code in L1");
3703
3704 if (e_flags & EF_BFIN_DATA_IN_L1)
3705 strcat (buf, ", data in L1");
3706
3707 break;
3708
3709 case EM_CYGNUS_FRV:
3710 switch (e_flags & EF_FRV_CPU_MASK)
3711 {
3712 case EF_FRV_CPU_GENERIC:
3713 break;
3714
3715 default:
3716 strcat (buf, ", fr???");
3717 break;
3718
3719 case EF_FRV_CPU_FR300:
3720 strcat (buf, ", fr300");
3721 break;
3722
3723 case EF_FRV_CPU_FR400:
3724 strcat (buf, ", fr400");
3725 break;
3726 case EF_FRV_CPU_FR405:
3727 strcat (buf, ", fr405");
3728 break;
3729
3730 case EF_FRV_CPU_FR450:
3731 strcat (buf, ", fr450");
3732 break;
3733
3734 case EF_FRV_CPU_FR500:
3735 strcat (buf, ", fr500");
3736 break;
3737 case EF_FRV_CPU_FR550:
3738 strcat (buf, ", fr550");
3739 break;
3740
3741 case EF_FRV_CPU_SIMPLE:
3742 strcat (buf, ", simple");
3743 break;
3744 case EF_FRV_CPU_TOMCAT:
3745 strcat (buf, ", tomcat");
3746 break;
3747 }
3748 break;
3749
3750 case EM_68K:
3751 if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
3752 strcat (buf, ", m68000");
3753 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
3754 strcat (buf, ", cpu32");
3755 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
3756 strcat (buf, ", fido_a");
3757 else
3758 {
3759 char const * isa = _("unknown");
3760 char const * mac = _("unknown mac");
3761 char const * additional = NULL;
3762
3763 switch (e_flags & EF_M68K_CF_ISA_MASK)
3764 {
3765 case EF_M68K_CF_ISA_A_NODIV:
3766 isa = "A";
3767 additional = ", nodiv";
3768 break;
3769 case EF_M68K_CF_ISA_A:
3770 isa = "A";
3771 break;
3772 case EF_M68K_CF_ISA_A_PLUS:
3773 isa = "A+";
3774 break;
3775 case EF_M68K_CF_ISA_B_NOUSP:
3776 isa = "B";
3777 additional = ", nousp";
3778 break;
3779 case EF_M68K_CF_ISA_B:
3780 isa = "B";
3781 break;
3782 case EF_M68K_CF_ISA_C:
3783 isa = "C";
3784 break;
3785 case EF_M68K_CF_ISA_C_NODIV:
3786 isa = "C";
3787 additional = ", nodiv";
3788 break;
3789 }
3790 strcat (buf, ", cf, isa ");
3791 strcat (buf, isa);
3792 if (additional)
3793 strcat (buf, additional);
3794 if (e_flags & EF_M68K_CF_FLOAT)
3795 strcat (buf, ", float");
3796 switch (e_flags & EF_M68K_CF_MAC_MASK)
3797 {
3798 case 0:
3799 mac = NULL;
3800 break;
3801 case EF_M68K_CF_MAC:
3802 mac = "mac";
3803 break;
3804 case EF_M68K_CF_EMAC:
3805 mac = "emac";
3806 break;
3807 case EF_M68K_CF_EMAC_B:
3808 mac = "emac_b";
3809 break;
3810 }
3811 if (mac)
3812 {
3813 strcat (buf, ", ");
3814 strcat (buf, mac);
3815 }
3816 }
3817 break;
3818
3819 case EM_AMDGPU:
3820 decode_AMDGPU_machine_flags (filedata, e_flags, buf);
3821 break;
3822
3823 case EM_CYGNUS_MEP:
3824 switch (e_flags & EF_MEP_CPU_MASK)
3825 {
3826 case EF_MEP_CPU_MEP: strcat (buf, ", generic MeP"); break;
3827 case EF_MEP_CPU_C2: strcat (buf, ", MeP C2"); break;
3828 case EF_MEP_CPU_C3: strcat (buf, ", MeP C3"); break;
3829 case EF_MEP_CPU_C4: strcat (buf, ", MeP C4"); break;
3830 case EF_MEP_CPU_C5: strcat (buf, ", MeP C5"); break;
3831 case EF_MEP_CPU_H1: strcat (buf, ", MeP H1"); break;
3832 default: strcat (buf, _(", <unknown MeP cpu type>")); break;
3833 }
3834
3835 switch (e_flags & EF_MEP_COP_MASK)
3836 {
3837 case EF_MEP_COP_NONE: break;
3838 case EF_MEP_COP_AVC: strcat (buf, ", AVC coprocessor"); break;
3839 case EF_MEP_COP_AVC2: strcat (buf, ", AVC2 coprocessor"); break;
3840 case EF_MEP_COP_FMAX: strcat (buf, ", FMAX coprocessor"); break;
3841 case EF_MEP_COP_IVC2: strcat (buf, ", IVC2 coprocessor"); break;
3842 default: strcat (buf, _("<unknown MeP copro type>")); break;
3843 }
3844
3845 if (e_flags & EF_MEP_LIBRARY)
3846 strcat (buf, ", Built for Library");
3847
3848 if (e_flags & EF_MEP_INDEX_MASK)
3849 sprintf (buf + strlen (buf), ", Configuration Index: %#x",
3850 e_flags & EF_MEP_INDEX_MASK);
3851
3852 if (e_flags & ~ EF_MEP_ALL_FLAGS)
3853 sprintf (buf + strlen (buf), _(", unknown flags bits: %#x"),
3854 e_flags & ~ EF_MEP_ALL_FLAGS);
3855 break;
3856
3857 case EM_PPC:
3858 if (e_flags & EF_PPC_EMB)
3859 strcat (buf, ", emb");
3860
3861 if (e_flags & EF_PPC_RELOCATABLE)
3862 strcat (buf, _(", relocatable"));
3863
3864 if (e_flags & EF_PPC_RELOCATABLE_LIB)
3865 strcat (buf, _(", relocatable-lib"));
3866 break;
3867
3868 case EM_PPC64:
3869 if (e_flags & EF_PPC64_ABI)
3870 {
3871 char abi[] = ", abiv0";
3872
3873 abi[6] += e_flags & EF_PPC64_ABI;
3874 strcat (buf, abi);
3875 }
3876 break;
3877
3878 case EM_V800:
3879 if ((e_flags & EF_RH850_ABI) == EF_RH850_ABI)
3880 strcat (buf, ", RH850 ABI");
3881
3882 if (e_flags & EF_V800_850E3)
3883 strcat (buf, ", V3 architecture");
3884
3885 if ((e_flags & (EF_RH850_FPU_DOUBLE | EF_RH850_FPU_SINGLE)) == 0)
3886 strcat (buf, ", FPU not used");
3887
3888 if ((e_flags & (EF_RH850_REGMODE22 | EF_RH850_REGMODE32)) == 0)
3889 strcat (buf, ", regmode: COMMON");
3890
3891 if ((e_flags & (EF_RH850_GP_FIX | EF_RH850_GP_NOFIX)) == 0)
3892 strcat (buf, ", r4 not used");
3893
3894 if ((e_flags & (EF_RH850_EP_FIX | EF_RH850_EP_NOFIX)) == 0)
3895 strcat (buf, ", r30 not used");
3896
3897 if ((e_flags & (EF_RH850_TP_FIX | EF_RH850_TP_NOFIX)) == 0)
3898 strcat (buf, ", r5 not used");
3899
3900 if ((e_flags & (EF_RH850_REG2_RESERVE | EF_RH850_REG2_NORESERVE)) == 0)
3901 strcat (buf, ", r2 not used");
3902
3903 for (e_flags &= 0xFFFF; e_flags; e_flags &= ~ (e_flags & - e_flags))
3904 {
3905 switch (e_flags & - e_flags)
3906 {
3907 case EF_RH850_FPU_DOUBLE: strcat (buf, ", double precision FPU"); break;
3908 case EF_RH850_FPU_SINGLE: strcat (buf, ", single precision FPU"); break;
3909 case EF_RH850_REGMODE22: strcat (buf, ", regmode:22"); break;
3910 case EF_RH850_REGMODE32: strcat (buf, ", regmode:23"); break;
3911 case EF_RH850_GP_FIX: strcat (buf, ", r4 fixed"); break;
3912 case EF_RH850_GP_NOFIX: strcat (buf, ", r4 free"); break;
3913 case EF_RH850_EP_FIX: strcat (buf, ", r30 fixed"); break;
3914 case EF_RH850_EP_NOFIX: strcat (buf, ", r30 free"); break;
3915 case EF_RH850_TP_FIX: strcat (buf, ", r5 fixed"); break;
3916 case EF_RH850_TP_NOFIX: strcat (buf, ", r5 free"); break;
3917 case EF_RH850_REG2_RESERVE: strcat (buf, ", r2 fixed"); break;
3918 case EF_RH850_REG2_NORESERVE: strcat (buf, ", r2 free"); break;
3919 default: break;
3920 }
3921 }
3922 break;
3923
3924 case EM_V850:
3925 case EM_CYGNUS_V850:
3926 switch (e_flags & EF_V850_ARCH)
3927 {
3928 case E_V850E3V5_ARCH:
3929 strcat (buf, ", v850e3v5");
3930 break;
3931 case E_V850E2V3_ARCH:
3932 strcat (buf, ", v850e2v3");
3933 break;
3934 case E_V850E2_ARCH:
3935 strcat (buf, ", v850e2");
3936 break;
3937 case E_V850E1_ARCH:
3938 strcat (buf, ", v850e1");
3939 break;
3940 case E_V850E_ARCH:
3941 strcat (buf, ", v850e");
3942 break;
3943 case E_V850_ARCH:
3944 strcat (buf, ", v850");
3945 break;
3946 default:
3947 strcat (buf, _(", unknown v850 architecture variant"));
3948 break;
3949 }
3950 break;
3951
3952 case EM_M32R:
3953 case EM_CYGNUS_M32R:
3954 if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
3955 strcat (buf, ", m32r");
3956 break;
3957
3958 case EM_MIPS:
3959 case EM_MIPS_RS3_LE:
3960 if (e_flags & EF_MIPS_NOREORDER)
3961 strcat (buf, ", noreorder");
3962
3963 if (e_flags & EF_MIPS_PIC)
3964 strcat (buf, ", pic");
3965
3966 if (e_flags & EF_MIPS_CPIC)
3967 strcat (buf, ", cpic");
3968
3969 if (e_flags & EF_MIPS_UCODE)
3970 strcat (buf, ", ugen_reserved");
3971
3972 if (e_flags & EF_MIPS_ABI2)
3973 strcat (buf, ", abi2");
3974
3975 if (e_flags & EF_MIPS_OPTIONS_FIRST)
3976 strcat (buf, ", odk first");
3977
3978 if (e_flags & EF_MIPS_32BITMODE)
3979 strcat (buf, ", 32bitmode");
3980
3981 if (e_flags & EF_MIPS_NAN2008)
3982 strcat (buf, ", nan2008");
3983
3984 if (e_flags & EF_MIPS_FP64)
3985 strcat (buf, ", fp64");
3986
3987 switch ((e_flags & EF_MIPS_MACH))
3988 {
3989 case E_MIPS_MACH_3900: strcat (buf, ", 3900"); break;
3990 case E_MIPS_MACH_4010: strcat (buf, ", 4010"); break;
3991 case E_MIPS_MACH_4100: strcat (buf, ", 4100"); break;
3992 case E_MIPS_MACH_4111: strcat (buf, ", 4111"); break;
3993 case E_MIPS_MACH_4120: strcat (buf, ", 4120"); break;
3994 case E_MIPS_MACH_4650: strcat (buf, ", 4650"); break;
3995 case E_MIPS_MACH_5400: strcat (buf, ", 5400"); break;
3996 case E_MIPS_MACH_5500: strcat (buf, ", 5500"); break;
3997 case E_MIPS_MACH_5900: strcat (buf, ", 5900"); break;
3998 case E_MIPS_MACH_SB1: strcat (buf, ", sb1"); break;
3999 case E_MIPS_MACH_9000: strcat (buf, ", 9000"); break;
4000 case E_MIPS_MACH_LS2E: strcat (buf, ", loongson-2e"); break;
4001 case E_MIPS_MACH_LS2F: strcat (buf, ", loongson-2f"); break;
4002 case E_MIPS_MACH_GS464: strcat (buf, ", gs464"); break;
4003 case E_MIPS_MACH_GS464E: strcat (buf, ", gs464e"); break;
4004 case E_MIPS_MACH_GS264E: strcat (buf, ", gs264e"); break;
4005 case E_MIPS_MACH_OCTEON: strcat (buf, ", octeon"); break;
4006 case E_MIPS_MACH_OCTEON2: strcat (buf, ", octeon2"); break;
4007 case E_MIPS_MACH_OCTEON3: strcat (buf, ", octeon3"); break;
4008 case E_MIPS_MACH_XLR: strcat (buf, ", xlr"); break;
4009 case E_MIPS_MACH_IAMR2: strcat (buf, ", interaptiv-mr2"); break;
4010 case 0:
4011 /* We simply ignore the field in this case to avoid confusion:
4012 MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
4013 extension. */
4014 break;
4015 default: strcat (buf, _(", unknown CPU")); break;
4016 }
4017
4018 switch ((e_flags & EF_MIPS_ABI))
4019 {
4020 case E_MIPS_ABI_O32: strcat (buf, ", o32"); break;
4021 case E_MIPS_ABI_O64: strcat (buf, ", o64"); break;
4022 case E_MIPS_ABI_EABI32: strcat (buf, ", eabi32"); break;
4023 case E_MIPS_ABI_EABI64: strcat (buf, ", eabi64"); break;
4024 case 0:
4025 /* We simply ignore the field in this case to avoid confusion:
4026 MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
4027 This means it is likely to be an o32 file, but not for
4028 sure. */
4029 break;
4030 default: strcat (buf, _(", unknown ABI")); break;
4031 }
4032
4033 if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
4034 strcat (buf, ", mdmx");
4035
4036 if (e_flags & EF_MIPS_ARCH_ASE_M16)
4037 strcat (buf, ", mips16");
4038
4039 if (e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
4040 strcat (buf, ", micromips");
4041
4042 switch ((e_flags & EF_MIPS_ARCH))
4043 {
4044 case E_MIPS_ARCH_1: strcat (buf, ", mips1"); break;
4045 case E_MIPS_ARCH_2: strcat (buf, ", mips2"); break;
4046 case E_MIPS_ARCH_3: strcat (buf, ", mips3"); break;
4047 case E_MIPS_ARCH_4: strcat (buf, ", mips4"); break;
4048 case E_MIPS_ARCH_5: strcat (buf, ", mips5"); break;
4049 case E_MIPS_ARCH_32: strcat (buf, ", mips32"); break;
4050 case E_MIPS_ARCH_32R2: strcat (buf, ", mips32r2"); break;
4051 case E_MIPS_ARCH_32R6: strcat (buf, ", mips32r6"); break;
4052 case E_MIPS_ARCH_64: strcat (buf, ", mips64"); break;
4053 case E_MIPS_ARCH_64R2: strcat (buf, ", mips64r2"); break;
4054 case E_MIPS_ARCH_64R6: strcat (buf, ", mips64r6"); break;
4055 default: strcat (buf, _(", unknown ISA")); break;
4056 }
4057 break;
4058
4059 case EM_NDS32:
4060 decode_NDS32_machine_flags (e_flags, buf, sizeof buf);
4061 break;
4062
4063 case EM_NFP:
4064 switch (EF_NFP_MACH (e_flags))
4065 {
4066 case E_NFP_MACH_3200:
4067 strcat (buf, ", NFP-32xx");
4068 break;
4069 case E_NFP_MACH_6000:
4070 strcat (buf, ", NFP-6xxx");
4071 break;
4072 }
4073 break;
4074
4075 case EM_RISCV:
4076 if (e_flags & EF_RISCV_RVC)
4077 strcat (buf, ", RVC");
4078
4079 if (e_flags & EF_RISCV_RVE)
4080 strcat (buf, ", RVE");
4081
4082 if (e_flags & EF_RISCV_TSO)
4083 strcat (buf, ", TSO");
4084
4085 switch (e_flags & EF_RISCV_FLOAT_ABI)
4086 {
4087 case EF_RISCV_FLOAT_ABI_SOFT:
4088 strcat (buf, ", soft-float ABI");
4089 break;
4090
4091 case EF_RISCV_FLOAT_ABI_SINGLE:
4092 strcat (buf, ", single-float ABI");
4093 break;
4094
4095 case EF_RISCV_FLOAT_ABI_DOUBLE:
4096 strcat (buf, ", double-float ABI");
4097 break;
4098
4099 case EF_RISCV_FLOAT_ABI_QUAD:
4100 strcat (buf, ", quad-float ABI");
4101 break;
4102 }
4103 break;
4104
4105 case EM_SH:
4106 switch ((e_flags & EF_SH_MACH_MASK))
4107 {
4108 case EF_SH1: strcat (buf, ", sh1"); break;
4109 case EF_SH2: strcat (buf, ", sh2"); break;
4110 case EF_SH3: strcat (buf, ", sh3"); break;
4111 case EF_SH_DSP: strcat (buf, ", sh-dsp"); break;
4112 case EF_SH3_DSP: strcat (buf, ", sh3-dsp"); break;
4113 case EF_SH4AL_DSP: strcat (buf, ", sh4al-dsp"); break;
4114 case EF_SH3E: strcat (buf, ", sh3e"); break;
4115 case EF_SH4: strcat (buf, ", sh4"); break;
4116 case EF_SH5: strcat (buf, ", sh5"); break;
4117 case EF_SH2E: strcat (buf, ", sh2e"); break;
4118 case EF_SH4A: strcat (buf, ", sh4a"); break;
4119 case EF_SH2A: strcat (buf, ", sh2a"); break;
4120 case EF_SH4_NOFPU: strcat (buf, ", sh4-nofpu"); break;
4121 case EF_SH4A_NOFPU: strcat (buf, ", sh4a-nofpu"); break;
4122 case EF_SH2A_NOFPU: strcat (buf, ", sh2a-nofpu"); break;
4123 case EF_SH3_NOMMU: strcat (buf, ", sh3-nommu"); break;
4124 case EF_SH4_NOMMU_NOFPU: strcat (buf, ", sh4-nommu-nofpu"); break;
4125 case EF_SH2A_SH4_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh4-nommu-nofpu"); break;
4126 case EF_SH2A_SH3_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh3-nommu"); break;
4127 case EF_SH2A_SH4: strcat (buf, ", sh2a-or-sh4"); break;
4128 case EF_SH2A_SH3E: strcat (buf, ", sh2a-or-sh3e"); break;
4129 default: strcat (buf, _(", unknown ISA")); break;
4130 }
4131
4132 if (e_flags & EF_SH_PIC)
4133 strcat (buf, ", pic");
4134
4135 if (e_flags & EF_SH_FDPIC)
4136 strcat (buf, ", fdpic");
4137 break;
4138
4139 case EM_OR1K:
4140 if (e_flags & EF_OR1K_NODELAY)
4141 strcat (buf, ", no delay");
4142 break;
4143
4144 case EM_SPARCV9:
4145 if (e_flags & EF_SPARC_32PLUS)
4146 strcat (buf, ", v8+");
4147
4148 if (e_flags & EF_SPARC_SUN_US1)
4149 strcat (buf, ", ultrasparcI");
4150
4151 if (e_flags & EF_SPARC_SUN_US3)
4152 strcat (buf, ", ultrasparcIII");
4153
4154 if (e_flags & EF_SPARC_HAL_R1)
4155 strcat (buf, ", halr1");
4156
4157 if (e_flags & EF_SPARC_LEDATA)
4158 strcat (buf, ", ledata");
4159
4160 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
4161 strcat (buf, ", tso");
4162
4163 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
4164 strcat (buf, ", pso");
4165
4166 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
4167 strcat (buf, ", rmo");
4168 break;
4169
4170 case EM_PARISC:
4171 switch (e_flags & EF_PARISC_ARCH)
4172 {
4173 case EFA_PARISC_1_0:
4174 strcpy (buf, ", PA-RISC 1.0");
4175 break;
4176 case EFA_PARISC_1_1:
4177 strcpy (buf, ", PA-RISC 1.1");
4178 break;
4179 case EFA_PARISC_2_0:
4180 strcpy (buf, ", PA-RISC 2.0");
4181 break;
4182 default:
4183 break;
4184 }
4185 if (e_flags & EF_PARISC_TRAPNIL)
4186 strcat (buf, ", trapnil");
4187 if (e_flags & EF_PARISC_EXT)
4188 strcat (buf, ", ext");
4189 if (e_flags & EF_PARISC_LSB)
4190 strcat (buf, ", lsb");
4191 if (e_flags & EF_PARISC_WIDE)
4192 strcat (buf, ", wide");
4193 if (e_flags & EF_PARISC_NO_KABP)
4194 strcat (buf, ", no kabp");
4195 if (e_flags & EF_PARISC_LAZYSWAP)
4196 strcat (buf, ", lazyswap");
4197 break;
4198
4199 case EM_PJ:
4200 case EM_PJ_OLD:
4201 if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
4202 strcat (buf, ", new calling convention");
4203
4204 if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
4205 strcat (buf, ", gnu calling convention");
4206 break;
4207
4208 case EM_IA_64:
4209 if ((e_flags & EF_IA_64_ABI64))
4210 strcat (buf, ", 64-bit");
4211 else
4212 strcat (buf, ", 32-bit");
4213 if ((e_flags & EF_IA_64_REDUCEDFP))
4214 strcat (buf, ", reduced fp model");
4215 if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
4216 strcat (buf, ", no function descriptors, constant gp");
4217 else if ((e_flags & EF_IA_64_CONS_GP))
4218 strcat (buf, ", constant gp");
4219 if ((e_flags & EF_IA_64_ABSOLUTE))
4220 strcat (buf, ", absolute");
4221 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
4222 {
4223 if ((e_flags & EF_IA_64_VMS_LINKAGES))
4224 strcat (buf, ", vms_linkages");
4225 switch ((e_flags & EF_IA_64_VMS_COMCOD))
4226 {
4227 case EF_IA_64_VMS_COMCOD_SUCCESS:
4228 break;
4229 case EF_IA_64_VMS_COMCOD_WARNING:
4230 strcat (buf, ", warning");
4231 break;
4232 case EF_IA_64_VMS_COMCOD_ERROR:
4233 strcat (buf, ", error");
4234 break;
4235 case EF_IA_64_VMS_COMCOD_ABORT:
4236 strcat (buf, ", abort");
4237 break;
4238 default:
4239 warn (_("Unrecognised IA64 VMS Command Code: %x\n"),
4240 e_flags & EF_IA_64_VMS_COMCOD);
4241 strcat (buf, ", <unknown>");
4242 }
4243 }
4244 break;
4245
4246 case EM_VAX:
4247 if ((e_flags & EF_VAX_NONPIC))
4248 strcat (buf, ", non-PIC");
4249 if ((e_flags & EF_VAX_DFLOAT))
4250 strcat (buf, ", D-Float");
4251 if ((e_flags & EF_VAX_GFLOAT))
4252 strcat (buf, ", G-Float");
4253 break;
4254
4255 case EM_VISIUM:
4256 if (e_flags & EF_VISIUM_ARCH_MCM)
4257 strcat (buf, ", mcm");
4258 else if (e_flags & EF_VISIUM_ARCH_MCM24)
4259 strcat (buf, ", mcm24");
4260 if (e_flags & EF_VISIUM_ARCH_GR6)
4261 strcat (buf, ", gr6");
4262 break;
4263
4264 case EM_RL78:
4265 switch (e_flags & E_FLAG_RL78_CPU_MASK)
4266 {
4267 case E_FLAG_RL78_ANY_CPU: break;
4268 case E_FLAG_RL78_G10: strcat (buf, ", G10"); break;
4269 case E_FLAG_RL78_G13: strcat (buf, ", G13"); break;
4270 case E_FLAG_RL78_G14: strcat (buf, ", G14"); break;
4271 }
4272 if (e_flags & E_FLAG_RL78_64BIT_DOUBLES)
4273 strcat (buf, ", 64-bit doubles");
4274 break;
4275
4276 case EM_RX:
4277 if (e_flags & E_FLAG_RX_64BIT_DOUBLES)
4278 strcat (buf, ", 64-bit doubles");
4279 if (e_flags & E_FLAG_RX_DSP)
4280 strcat (buf, ", dsp");
4281 if (e_flags & E_FLAG_RX_PID)
4282 strcat (buf, ", pid");
4283 if (e_flags & E_FLAG_RX_ABI)
4284 strcat (buf, ", RX ABI");
4285 if (e_flags & E_FLAG_RX_SINSNS_SET)
4286 strcat (buf, e_flags & E_FLAG_RX_SINSNS_YES
4287 ? ", uses String instructions" : ", bans String instructions");
4288 if (e_flags & E_FLAG_RX_V2)
4289 strcat (buf, ", V2");
4290 if (e_flags & E_FLAG_RX_V3)
4291 strcat (buf, ", V3");
4292 break;
4293
4294 case EM_S390:
4295 if (e_flags & EF_S390_HIGH_GPRS)
4296 strcat (buf, ", highgprs");
4297 break;
4298
4299 case EM_TI_C6000:
4300 if ((e_flags & EF_C6000_REL))
4301 strcat (buf, ", relocatable module");
4302 break;
4303
4304 case EM_MSP430:
4305 strcat (buf, _(": architecture variant: "));
4306 switch (e_flags & EF_MSP430_MACH)
4307 {
4308 case E_MSP430_MACH_MSP430x11: strcat (buf, "MSP430x11"); break;
4309 case E_MSP430_MACH_MSP430x11x1 : strcat (buf, "MSP430x11x1 "); break;
4310 case E_MSP430_MACH_MSP430x12: strcat (buf, "MSP430x12"); break;
4311 case E_MSP430_MACH_MSP430x13: strcat (buf, "MSP430x13"); break;
4312 case E_MSP430_MACH_MSP430x14: strcat (buf, "MSP430x14"); break;
4313 case E_MSP430_MACH_MSP430x15: strcat (buf, "MSP430x15"); break;
4314 case E_MSP430_MACH_MSP430x16: strcat (buf, "MSP430x16"); break;
4315 case E_MSP430_MACH_MSP430x31: strcat (buf, "MSP430x31"); break;
4316 case E_MSP430_MACH_MSP430x32: strcat (buf, "MSP430x32"); break;
4317 case E_MSP430_MACH_MSP430x33: strcat (buf, "MSP430x33"); break;
4318 case E_MSP430_MACH_MSP430x41: strcat (buf, "MSP430x41"); break;
4319 case E_MSP430_MACH_MSP430x42: strcat (buf, "MSP430x42"); break;
4320 case E_MSP430_MACH_MSP430x43: strcat (buf, "MSP430x43"); break;
4321 case E_MSP430_MACH_MSP430x44: strcat (buf, "MSP430x44"); break;
4322 case E_MSP430_MACH_MSP430X : strcat (buf, "MSP430X"); break;
4323 default:
4324 strcat (buf, _(": unknown")); break;
4325 }
4326
4327 if (e_flags & ~ EF_MSP430_MACH)
4328 strcat (buf, _(": unknown extra flag bits also present"));
4329 break;
4330
4331 case EM_Z80:
4332 switch (e_flags & EF_Z80_MACH_MSK)
4333 {
4334 case EF_Z80_MACH_Z80: strcat (buf, ", Z80"); break;
4335 case EF_Z80_MACH_Z180: strcat (buf, ", Z180"); break;
4336 case EF_Z80_MACH_R800: strcat (buf, ", R800"); break;
4337 case EF_Z80_MACH_EZ80_Z80: strcat (buf, ", EZ80"); break;
4338 case EF_Z80_MACH_EZ80_ADL: strcat (buf, ", EZ80, ADL"); break;
4339 case EF_Z80_MACH_GBZ80: strcat (buf, ", GBZ80"); break;
4340 case EF_Z80_MACH_Z80N: strcat (buf, ", Z80N"); break;
4341 default:
4342 strcat (buf, _(", unknown")); break;
4343 }
4344 break;
4345 case EM_LOONGARCH:
4346 if (EF_LOONGARCH_IS_SOFT_FLOAT (e_flags))
4347 strcat (buf, ", SOFT-FLOAT");
4348 else if (EF_LOONGARCH_IS_SINGLE_FLOAT (e_flags))
4349 strcat (buf, ", SINGLE-FLOAT");
4350 else if (EF_LOONGARCH_IS_DOUBLE_FLOAT (e_flags))
4351 strcat (buf, ", DOUBLE-FLOAT");
4352
4353 if (EF_LOONGARCH_IS_OBJ_V0 (e_flags))
4354 strcat (buf, ", OBJ-v0");
4355 else if (EF_LOONGARCH_IS_OBJ_V1 (e_flags))
4356 strcat (buf, ", OBJ-v1");
4357
4358 break;
4359 }
4360 }
4361
4362 return buf;
4363 }
4364
4365 static const char *
4366 get_osabi_name (Filedata * filedata, unsigned int osabi)
4367 {
4368 static char buff[32];
4369
4370 switch (osabi)
4371 {
4372 case ELFOSABI_NONE: return "UNIX - System V";
4373 case ELFOSABI_HPUX: return "UNIX - HP-UX";
4374 case ELFOSABI_NETBSD: return "UNIX - NetBSD";
4375 case ELFOSABI_GNU: return "UNIX - GNU";
4376 case ELFOSABI_SOLARIS: return "UNIX - Solaris";
4377 case ELFOSABI_AIX: return "UNIX - AIX";
4378 case ELFOSABI_IRIX: return "UNIX - IRIX";
4379 case ELFOSABI_FREEBSD: return "UNIX - FreeBSD";
4380 case ELFOSABI_TRU64: return "UNIX - TRU64";
4381 case ELFOSABI_MODESTO: return "Novell - Modesto";
4382 case ELFOSABI_OPENBSD: return "UNIX - OpenBSD";
4383 case ELFOSABI_OPENVMS: return "VMS - OpenVMS";
4384 case ELFOSABI_NSK: return "HP - Non-Stop Kernel";
4385 case ELFOSABI_AROS: return "AROS";
4386 case ELFOSABI_FENIXOS: return "FenixOS";
4387 case ELFOSABI_CLOUDABI: return "Nuxi CloudABI";
4388 case ELFOSABI_OPENVOS: return "Stratus Technologies OpenVOS";
4389 default:
4390 if (osabi >= 64)
4391 switch (filedata->file_header.e_machine)
4392 {
4393 case EM_AMDGPU:
4394 switch (osabi)
4395 {
4396 case ELFOSABI_AMDGPU_HSA: return "AMD HSA";
4397 case ELFOSABI_AMDGPU_PAL: return "AMD PAL";
4398 case ELFOSABI_AMDGPU_MESA3D: return "AMD Mesa3D";
4399 default:
4400 break;
4401 }
4402 break;
4403
4404 case EM_ARM:
4405 switch (osabi)
4406 {
4407 case ELFOSABI_ARM: return "ARM";
4408 case ELFOSABI_ARM_FDPIC: return "ARM FDPIC";
4409 default:
4410 break;
4411 }
4412 break;
4413
4414 case EM_MSP430:
4415 case EM_MSP430_OLD:
4416 case EM_VISIUM:
4417 switch (osabi)
4418 {
4419 case ELFOSABI_STANDALONE: return _("Standalone App");
4420 default:
4421 break;
4422 }
4423 break;
4424
4425 case EM_TI_C6000:
4426 switch (osabi)
4427 {
4428 case ELFOSABI_C6000_ELFABI: return _("Bare-metal C6000");
4429 case ELFOSABI_C6000_LINUX: return "Linux C6000";
4430 default:
4431 break;
4432 }
4433 break;
4434
4435 default:
4436 break;
4437 }
4438 snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
4439 return buff;
4440 }
4441 }
4442
4443 static const char *
4444 get_aarch64_segment_type (unsigned long type)
4445 {
4446 switch (type)
4447 {
4448 case PT_AARCH64_ARCHEXT: return "AARCH64_ARCHEXT";
4449 case PT_AARCH64_MEMTAG_MTE: return "AARCH64_MEMTAG_MTE";
4450 default: return NULL;
4451 }
4452 }
4453
4454 static const char *
4455 get_arm_segment_type (unsigned long type)
4456 {
4457 switch (type)
4458 {
4459 case PT_ARM_EXIDX: return "EXIDX";
4460 default: return NULL;
4461 }
4462 }
4463
4464 static const char *
4465 get_s390_segment_type (unsigned long type)
4466 {
4467 switch (type)
4468 {
4469 case PT_S390_PGSTE: return "S390_PGSTE";
4470 default: return NULL;
4471 }
4472 }
4473
4474 static const char *
4475 get_mips_segment_type (unsigned long type)
4476 {
4477 switch (type)
4478 {
4479 case PT_MIPS_REGINFO: return "REGINFO";
4480 case PT_MIPS_RTPROC: return "RTPROC";
4481 case PT_MIPS_OPTIONS: return "OPTIONS";
4482 case PT_MIPS_ABIFLAGS: return "ABIFLAGS";
4483 default: return NULL;
4484 }
4485 }
4486
4487 static const char *
4488 get_parisc_segment_type (unsigned long type)
4489 {
4490 switch (type)
4491 {
4492 case PT_PARISC_ARCHEXT: return "PARISC_ARCHEXT";
4493 case PT_PARISC_UNWIND: return "PARISC_UNWIND";
4494 case PT_PARISC_WEAKORDER: return "PARISC_WEAKORDER";
4495 default: return NULL;
4496 }
4497 }
4498
4499 static const char *
4500 get_ia64_segment_type (unsigned long type)
4501 {
4502 switch (type)
4503 {
4504 case PT_IA_64_ARCHEXT: return "IA_64_ARCHEXT";
4505 case PT_IA_64_UNWIND: return "IA_64_UNWIND";
4506 default: return NULL;
4507 }
4508 }
4509
4510 static const char *
4511 get_tic6x_segment_type (unsigned long type)
4512 {
4513 switch (type)
4514 {
4515 case PT_C6000_PHATTR: return "C6000_PHATTR";
4516 default: return NULL;
4517 }
4518 }
4519
4520 static const char *
4521 get_riscv_segment_type (unsigned long type)
4522 {
4523 switch (type)
4524 {
4525 case PT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
4526 default: return NULL;
4527 }
4528 }
4529
4530 static const char *
4531 get_hpux_segment_type (unsigned long type, unsigned e_machine)
4532 {
4533 if (e_machine == EM_PARISC)
4534 switch (type)
4535 {
4536 case PT_HP_TLS: return "HP_TLS";
4537 case PT_HP_CORE_NONE: return "HP_CORE_NONE";
4538 case PT_HP_CORE_VERSION: return "HP_CORE_VERSION";
4539 case PT_HP_CORE_KERNEL: return "HP_CORE_KERNEL";
4540 case PT_HP_CORE_COMM: return "HP_CORE_COMM";
4541 case PT_HP_CORE_PROC: return "HP_CORE_PROC";
4542 case PT_HP_CORE_LOADABLE: return "HP_CORE_LOADABLE";
4543 case PT_HP_CORE_STACK: return "HP_CORE_STACK";
4544 case PT_HP_CORE_SHM: return "HP_CORE_SHM";
4545 case PT_HP_CORE_MMF: return "HP_CORE_MMF";
4546 case PT_HP_PARALLEL: return "HP_PARALLEL";
4547 case PT_HP_FASTBIND: return "HP_FASTBIND";
4548 case PT_HP_OPT_ANNOT: return "HP_OPT_ANNOT";
4549 case PT_HP_HSL_ANNOT: return "HP_HSL_ANNOT";
4550 case PT_HP_STACK: return "HP_STACK";
4551 case PT_HP_CORE_UTSNAME: return "HP_CORE_UTSNAME";
4552 default: return NULL;
4553 }
4554
4555 if (e_machine == EM_IA_64)
4556 switch (type)
4557 {
4558 case PT_HP_TLS: return "HP_TLS";
4559 case PT_IA_64_HP_OPT_ANOT: return "HP_OPT_ANNOT";
4560 case PT_IA_64_HP_HSL_ANOT: return "HP_HSL_ANNOT";
4561 case PT_IA_64_HP_STACK: return "HP_STACK";
4562 default: return NULL;
4563 }
4564
4565 return NULL;
4566 }
4567
4568 static const char *
4569 get_solaris_segment_type (unsigned long type)
4570 {
4571 switch (type)
4572 {
4573 case 0x6464e550: return "PT_SUNW_UNWIND";
4574 case 0x6474e550: return "PT_SUNW_EH_FRAME";
4575 case 0x6ffffff7: return "PT_LOSUNW";
4576 case 0x6ffffffa: return "PT_SUNWBSS";
4577 case 0x6ffffffb: return "PT_SUNWSTACK";
4578 case 0x6ffffffc: return "PT_SUNWDTRACE";
4579 case 0x6ffffffd: return "PT_SUNWCAP";
4580 case 0x6fffffff: return "PT_HISUNW";
4581 default: return NULL;
4582 }
4583 }
4584
4585 static const char *
4586 get_segment_type (Filedata * filedata, unsigned long p_type)
4587 {
4588 static char buff[32];
4589
4590 switch (p_type)
4591 {
4592 case PT_NULL: return "NULL";
4593 case PT_LOAD: return "LOAD";
4594 case PT_DYNAMIC: return "DYNAMIC";
4595 case PT_INTERP: return "INTERP";
4596 case PT_NOTE: return "NOTE";
4597 case PT_SHLIB: return "SHLIB";
4598 case PT_PHDR: return "PHDR";
4599 case PT_TLS: return "TLS";
4600 case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
4601 case PT_GNU_STACK: return "GNU_STACK";
4602 case PT_GNU_RELRO: return "GNU_RELRO";
4603 case PT_GNU_PROPERTY: return "GNU_PROPERTY";
4604
4605 case PT_OPENBSD_RANDOMIZE: return "OPENBSD_RANDOMIZE";
4606 case PT_OPENBSD_WXNEEDED: return "OPENBSD_WXNEEDED";
4607 case PT_OPENBSD_BOOTDATA: return "OPENBSD_BOOTDATA";
4608
4609 default:
4610 if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
4611 {
4612 const char * result;
4613
4614 switch (filedata->file_header.e_machine)
4615 {
4616 case EM_AARCH64:
4617 result = get_aarch64_segment_type (p_type);
4618 break;
4619 case EM_ARM:
4620 result = get_arm_segment_type (p_type);
4621 break;
4622 case EM_MIPS:
4623 case EM_MIPS_RS3_LE:
4624 result = get_mips_segment_type (p_type);
4625 break;
4626 case EM_PARISC:
4627 result = get_parisc_segment_type (p_type);
4628 break;
4629 case EM_IA_64:
4630 result = get_ia64_segment_type (p_type);
4631 break;
4632 case EM_TI_C6000:
4633 result = get_tic6x_segment_type (p_type);
4634 break;
4635 case EM_S390:
4636 case EM_S390_OLD:
4637 result = get_s390_segment_type (p_type);
4638 break;
4639 case EM_RISCV:
4640 result = get_riscv_segment_type (p_type);
4641 break;
4642 default:
4643 result = NULL;
4644 break;
4645 }
4646
4647 if (result != NULL)
4648 return result;
4649
4650 sprintf (buff, "LOPROC+%#lx", p_type - PT_LOPROC);
4651 }
4652 else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
4653 {
4654 const char * result = NULL;
4655
4656 switch (filedata->file_header.e_ident[EI_OSABI])
4657 {
4658 case ELFOSABI_GNU:
4659 case ELFOSABI_FREEBSD:
4660 if (p_type >= PT_GNU_MBIND_LO && p_type <= PT_GNU_MBIND_HI)
4661 {
4662 sprintf (buff, "GNU_MBIND+%#lx", p_type - PT_GNU_MBIND_LO);
4663 result = buff;
4664 }
4665 break;
4666 case ELFOSABI_HPUX:
4667 result = get_hpux_segment_type (p_type,
4668 filedata->file_header.e_machine);
4669 break;
4670 case ELFOSABI_SOLARIS:
4671 result = get_solaris_segment_type (p_type);
4672 break;
4673 default:
4674 break;
4675 }
4676 if (result != NULL)
4677 return result;
4678
4679 sprintf (buff, "LOOS+%#lx", p_type - PT_LOOS);
4680 }
4681 else
4682 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
4683
4684 return buff;
4685 }
4686 }
4687
4688 static const char *
4689 get_arc_section_type_name (unsigned int sh_type)
4690 {
4691 switch (sh_type)
4692 {
4693 case SHT_ARC_ATTRIBUTES: return "ARC_ATTRIBUTES";
4694 default:
4695 break;
4696 }
4697 return NULL;
4698 }
4699
4700 static const char *
4701 get_mips_section_type_name (unsigned int sh_type)
4702 {
4703 switch (sh_type)
4704 {
4705 case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
4706 case SHT_MIPS_MSYM: return "MIPS_MSYM";
4707 case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
4708 case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
4709 case SHT_MIPS_UCODE: return "MIPS_UCODE";
4710 case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
4711 case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
4712 case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
4713 case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
4714 case SHT_MIPS_RELD: return "MIPS_RELD";
4715 case SHT_MIPS_IFACE: return "MIPS_IFACE";
4716 case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
4717 case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
4718 case SHT_MIPS_SHDR: return "MIPS_SHDR";
4719 case SHT_MIPS_FDESC: return "MIPS_FDESC";
4720 case SHT_MIPS_EXTSYM: return "MIPS_EXTSYM";
4721 case SHT_MIPS_DENSE: return "MIPS_DENSE";
4722 case SHT_MIPS_PDESC: return "MIPS_PDESC";
4723 case SHT_MIPS_LOCSYM: return "MIPS_LOCSYM";
4724 case SHT_MIPS_AUXSYM: return "MIPS_AUXSYM";
4725 case SHT_MIPS_OPTSYM: return "MIPS_OPTSYM";
4726 case SHT_MIPS_LOCSTR: return "MIPS_LOCSTR";
4727 case SHT_MIPS_LINE: return "MIPS_LINE";
4728 case SHT_MIPS_RFDESC: return "MIPS_RFDESC";
4729 case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
4730 case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
4731 case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
4732 case SHT_MIPS_DWARF: return "MIPS_DWARF";
4733 case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
4734 case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
4735 case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
4736 case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
4737 case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
4738 case SHT_MIPS_XLATE: return "MIPS_XLATE";
4739 case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
4740 case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
4741 case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
4742 case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
4743 case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
4744 case SHT_MIPS_ABIFLAGS: return "MIPS_ABIFLAGS";
4745 case SHT_MIPS_XHASH: return "MIPS_XHASH";
4746 default:
4747 break;
4748 }
4749 return NULL;
4750 }
4751
4752 static const char *
4753 get_parisc_section_type_name (unsigned int sh_type)
4754 {
4755 switch (sh_type)
4756 {
4757 case SHT_PARISC_EXT: return "PARISC_EXT";
4758 case SHT_PARISC_UNWIND: return "PARISC_UNWIND";
4759 case SHT_PARISC_DOC: return "PARISC_DOC";
4760 case SHT_PARISC_ANNOT: return "PARISC_ANNOT";
4761 case SHT_PARISC_SYMEXTN: return "PARISC_SYMEXTN";
4762 case SHT_PARISC_STUBS: return "PARISC_STUBS";
4763 case SHT_PARISC_DLKM: return "PARISC_DLKM";
4764 default: return NULL;
4765 }
4766 }
4767
4768 static const char *
4769 get_ia64_section_type_name (Filedata * filedata, unsigned int sh_type)
4770 {
4771 /* If the top 8 bits are 0x78 the next 8 are the os/abi ID. */
4772 if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
4773 return get_osabi_name (filedata, (sh_type & 0x00FF0000) >> 16);
4774
4775 switch (sh_type)
4776 {
4777 case SHT_IA_64_EXT: return "IA_64_EXT";
4778 case SHT_IA_64_UNWIND: return "IA_64_UNWIND";
4779 case SHT_IA_64_PRIORITY_INIT: return "IA_64_PRIORITY_INIT";
4780 case SHT_IA_64_VMS_TRACE: return "VMS_TRACE";
4781 case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
4782 case SHT_IA_64_VMS_DEBUG: return "VMS_DEBUG";
4783 case SHT_IA_64_VMS_DEBUG_STR: return "VMS_DEBUG_STR";
4784 case SHT_IA_64_VMS_LINKAGES: return "VMS_LINKAGES";
4785 case SHT_IA_64_VMS_SYMBOL_VECTOR: return "VMS_SYMBOL_VECTOR";
4786 case SHT_IA_64_VMS_FIXUP: return "VMS_FIXUP";
4787 default:
4788 break;
4789 }
4790 return NULL;
4791 }
4792
4793 static const char *
4794 get_x86_64_section_type_name (unsigned int sh_type)
4795 {
4796 switch (sh_type)
4797 {
4798 case SHT_X86_64_UNWIND: return "X86_64_UNWIND";
4799 default: return NULL;
4800 }
4801 }
4802
4803 static const char *
4804 get_aarch64_section_type_name (unsigned int sh_type)
4805 {
4806 switch (sh_type)
4807 {
4808 case SHT_AARCH64_ATTRIBUTES: return "AARCH64_ATTRIBUTES";
4809 default: return NULL;
4810 }
4811 }
4812
4813 static const char *
4814 get_arm_section_type_name (unsigned int sh_type)
4815 {
4816 switch (sh_type)
4817 {
4818 case SHT_ARM_EXIDX: return "ARM_EXIDX";
4819 case SHT_ARM_PREEMPTMAP: return "ARM_PREEMPTMAP";
4820 case SHT_ARM_ATTRIBUTES: return "ARM_ATTRIBUTES";
4821 case SHT_ARM_DEBUGOVERLAY: return "ARM_DEBUGOVERLAY";
4822 case SHT_ARM_OVERLAYSECTION: return "ARM_OVERLAYSECTION";
4823 default: return NULL;
4824 }
4825 }
4826
4827 static const char *
4828 get_tic6x_section_type_name (unsigned int sh_type)
4829 {
4830 switch (sh_type)
4831 {
4832 case SHT_C6000_UNWIND: return "C6000_UNWIND";
4833 case SHT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
4834 case SHT_C6000_ATTRIBUTES: return "C6000_ATTRIBUTES";
4835 case SHT_TI_ICODE: return "TI_ICODE";
4836 case SHT_TI_XREF: return "TI_XREF";
4837 case SHT_TI_HANDLER: return "TI_HANDLER";
4838 case SHT_TI_INITINFO: return "TI_INITINFO";
4839 case SHT_TI_PHATTRS: return "TI_PHATTRS";
4840 default: return NULL;
4841 }
4842 }
4843
4844 static const char *
4845 get_msp430_section_type_name (unsigned int sh_type)
4846 {
4847 switch (sh_type)
4848 {
4849 case SHT_MSP430_SEC_FLAGS: return "MSP430_SEC_FLAGS";
4850 case SHT_MSP430_SYM_ALIASES: return "MSP430_SYM_ALIASES";
4851 case SHT_MSP430_ATTRIBUTES: return "MSP430_ATTRIBUTES";
4852 default: return NULL;
4853 }
4854 }
4855
4856 static const char *
4857 get_nfp_section_type_name (unsigned int sh_type)
4858 {
4859 switch (sh_type)
4860 {
4861 case SHT_NFP_MECONFIG: return "NFP_MECONFIG";
4862 case SHT_NFP_INITREG: return "NFP_INITREG";
4863 case SHT_NFP_UDEBUG: return "NFP_UDEBUG";
4864 default: return NULL;
4865 }
4866 }
4867
4868 static const char *
4869 get_v850_section_type_name (unsigned int sh_type)
4870 {
4871 switch (sh_type)
4872 {
4873 case SHT_V850_SCOMMON: return "V850 Small Common";
4874 case SHT_V850_TCOMMON: return "V850 Tiny Common";
4875 case SHT_V850_ZCOMMON: return "V850 Zero Common";
4876 case SHT_RENESAS_IOP: return "RENESAS IOP";
4877 case SHT_RENESAS_INFO: return "RENESAS INFO";
4878 default: return NULL;
4879 }
4880 }
4881
4882 static const char *
4883 get_riscv_section_type_name (unsigned int sh_type)
4884 {
4885 switch (sh_type)
4886 {
4887 case SHT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
4888 default: return NULL;
4889 }
4890 }
4891
4892 static const char *
4893 get_csky_section_type_name (unsigned int sh_type)
4894 {
4895 switch (sh_type)
4896 {
4897 case SHT_CSKY_ATTRIBUTES: return "CSKY_ATTRIBUTES";
4898 default: return NULL;
4899 }
4900 }
4901
4902 static const char *
4903 get_section_type_name (Filedata * filedata, unsigned int sh_type)
4904 {
4905 static char buff[32];
4906 const char * result;
4907
4908 switch (sh_type)
4909 {
4910 case SHT_NULL: return "NULL";
4911 case SHT_PROGBITS: return "PROGBITS";
4912 case SHT_SYMTAB: return "SYMTAB";
4913 case SHT_STRTAB: return "STRTAB";
4914 case SHT_RELA: return "RELA";
4915 case SHT_RELR: return "RELR";
4916 case SHT_HASH: return "HASH";
4917 case SHT_DYNAMIC: return "DYNAMIC";
4918 case SHT_NOTE: return "NOTE";
4919 case SHT_NOBITS: return "NOBITS";
4920 case SHT_REL: return "REL";
4921 case SHT_SHLIB: return "SHLIB";
4922 case SHT_DYNSYM: return "DYNSYM";
4923 case SHT_INIT_ARRAY: return "INIT_ARRAY";
4924 case SHT_FINI_ARRAY: return "FINI_ARRAY";
4925 case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
4926 case SHT_GNU_HASH: return "GNU_HASH";
4927 case SHT_GROUP: return "GROUP";
4928 case SHT_SYMTAB_SHNDX: return "SYMTAB SECTION INDICES";
4929 case SHT_GNU_verdef: return "VERDEF";
4930 case SHT_GNU_verneed: return "VERNEED";
4931 case SHT_GNU_versym: return "VERSYM";
4932 case 0x6ffffff0: return "VERSYM";
4933 case 0x6ffffffc: return "VERDEF";
4934 case 0x7ffffffd: return "AUXILIARY";
4935 case 0x7fffffff: return "FILTER";
4936 case SHT_GNU_LIBLIST: return "GNU_LIBLIST";
4937
4938 default:
4939 if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
4940 {
4941 switch (filedata->file_header.e_machine)
4942 {
4943 case EM_ARC:
4944 case EM_ARC_COMPACT:
4945 case EM_ARC_COMPACT2:
4946 result = get_arc_section_type_name (sh_type);
4947 break;
4948 case EM_MIPS:
4949 case EM_MIPS_RS3_LE:
4950 result = get_mips_section_type_name (sh_type);
4951 break;
4952 case EM_PARISC:
4953 result = get_parisc_section_type_name (sh_type);
4954 break;
4955 case EM_IA_64:
4956 result = get_ia64_section_type_name (filedata, sh_type);
4957 break;
4958 case EM_X86_64:
4959 case EM_L1OM:
4960 case EM_K1OM:
4961 result = get_x86_64_section_type_name (sh_type);
4962 break;
4963 case EM_AARCH64:
4964 result = get_aarch64_section_type_name (sh_type);
4965 break;
4966 case EM_ARM:
4967 result = get_arm_section_type_name (sh_type);
4968 break;
4969 case EM_TI_C6000:
4970 result = get_tic6x_section_type_name (sh_type);
4971 break;
4972 case EM_MSP430:
4973 result = get_msp430_section_type_name (sh_type);
4974 break;
4975 case EM_NFP:
4976 result = get_nfp_section_type_name (sh_type);
4977 break;
4978 case EM_V800:
4979 case EM_V850:
4980 case EM_CYGNUS_V850:
4981 result = get_v850_section_type_name (sh_type);
4982 break;
4983 case EM_RISCV:
4984 result = get_riscv_section_type_name (sh_type);
4985 break;
4986 case EM_CSKY:
4987 result = get_csky_section_type_name (sh_type);
4988 break;
4989 default:
4990 result = NULL;
4991 break;
4992 }
4993
4994 if (result != NULL)
4995 return result;
4996
4997 sprintf (buff, "LOPROC+%#x", sh_type - SHT_LOPROC);
4998 }
4999 else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
5000 {
5001 switch (filedata->file_header.e_machine)
5002 {
5003 case EM_IA_64:
5004 result = get_ia64_section_type_name (filedata, sh_type);
5005 break;
5006 default:
5007 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
5008 result = get_solaris_section_type (sh_type);
5009 else
5010 {
5011 switch (sh_type)
5012 {
5013 case SHT_GNU_INCREMENTAL_INPUTS: result = "GNU_INCREMENTAL_INPUTS"; break;
5014 case SHT_GNU_ATTRIBUTES: result = "GNU_ATTRIBUTES"; break;
5015 case SHT_GNU_HASH: result = "GNU_HASH"; break;
5016 case SHT_GNU_LIBLIST: result = "GNU_LIBLIST"; break;
5017 default:
5018 result = NULL;
5019 break;
5020 }
5021 }
5022 break;
5023 }
5024
5025 if (result != NULL)
5026 return result;
5027
5028 sprintf (buff, "LOOS+%#x", sh_type - SHT_LOOS);
5029 }
5030 else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
5031 {
5032 switch (filedata->file_header.e_machine)
5033 {
5034 case EM_V800:
5035 case EM_V850:
5036 case EM_CYGNUS_V850:
5037 result = get_v850_section_type_name (sh_type);
5038 break;
5039 default:
5040 result = NULL;
5041 break;
5042 }
5043
5044 if (result != NULL)
5045 return result;
5046
5047 sprintf (buff, "LOUSER+%#x", sh_type - SHT_LOUSER);
5048 }
5049 else
5050 /* This message is probably going to be displayed in a 15
5051 character wide field, so put the hex value first. */
5052 snprintf (buff, sizeof (buff), _("%08x: <unknown>"), sh_type);
5053
5054 return buff;
5055 }
5056 }
5057
5058 enum long_option_values
5059 {
5060 OPTION_DEBUG_DUMP = 512,
5061 OPTION_DYN_SYMS,
5062 OPTION_LTO_SYMS,
5063 OPTION_DWARF_DEPTH,
5064 OPTION_DWARF_START,
5065 OPTION_DWARF_CHECK,
5066 OPTION_CTF_DUMP,
5067 OPTION_CTF_PARENT,
5068 OPTION_CTF_SYMBOLS,
5069 OPTION_CTF_STRINGS,
5070 OPTION_WITH_SYMBOL_VERSIONS,
5071 OPTION_RECURSE_LIMIT,
5072 OPTION_NO_RECURSE_LIMIT,
5073 OPTION_NO_DEMANGLING,
5074 OPTION_SYM_BASE
5075 };
5076
5077 static struct option options[] =
5078 {
5079 /* Note - This table is alpha-sorted on the 'val'
5080 field in order to make adding new options easier. */
5081 {"arch-specific", no_argument, 0, 'A'},
5082 {"all", no_argument, 0, 'a'},
5083 {"demangle", optional_argument, 0, 'C'},
5084 {"archive-index", no_argument, 0, 'c'},
5085 {"use-dynamic", no_argument, 0, 'D'},
5086 {"dynamic", no_argument, 0, 'd'},
5087 {"headers", no_argument, 0, 'e'},
5088 {"section-groups", no_argument, 0, 'g'},
5089 {"help", no_argument, 0, 'H'},
5090 {"file-header", no_argument, 0, 'h'},
5091 {"histogram", no_argument, 0, 'I'},
5092 {"lint", no_argument, 0, 'L'},
5093 {"enable-checks", no_argument, 0, 'L'},
5094 {"program-headers", no_argument, 0, 'l'},
5095 {"segments", no_argument, 0, 'l'},
5096 {"full-section-name",no_argument, 0, 'N'},
5097 {"notes", no_argument, 0, 'n'},
5098 {"process-links", no_argument, 0, 'P'},
5099 {"string-dump", required_argument, 0, 'p'},
5100 {"relocated-dump", required_argument, 0, 'R'},
5101 {"relocs", no_argument, 0, 'r'},
5102 {"section-headers", no_argument, 0, 'S'},
5103 {"sections", no_argument, 0, 'S'},
5104 {"symbols", no_argument, 0, 's'},
5105 {"syms", no_argument, 0, 's'},
5106 {"silent-truncation",no_argument, 0, 'T'},
5107 {"section-details", no_argument, 0, 't'},
5108 {"unicode", required_argument, NULL, 'U'},
5109 {"unwind", no_argument, 0, 'u'},
5110 {"version-info", no_argument, 0, 'V'},
5111 {"version", no_argument, 0, 'v'},
5112 {"wide", no_argument, 0, 'W'},
5113 {"hex-dump", required_argument, 0, 'x'},
5114 {"decompress", no_argument, 0, 'z'},
5115
5116 {"no-demangle", no_argument, 0, OPTION_NO_DEMANGLING},
5117 {"recurse-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
5118 {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
5119 {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
5120 {"dyn-syms", no_argument, 0, OPTION_DYN_SYMS},
5121 {"lto-syms", no_argument, 0, OPTION_LTO_SYMS},
5122 {"debug-dump", optional_argument, 0, OPTION_DEBUG_DUMP},
5123 {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
5124 {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
5125 {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
5126 #ifdef ENABLE_LIBCTF
5127 {"ctf", required_argument, 0, OPTION_CTF_DUMP},
5128 {"ctf-symbols", required_argument, 0, OPTION_CTF_SYMBOLS},
5129 {"ctf-strings", required_argument, 0, OPTION_CTF_STRINGS},
5130 {"ctf-parent", required_argument, 0, OPTION_CTF_PARENT},
5131 #endif
5132 {"sym-base", optional_argument, 0, OPTION_SYM_BASE},
5133
5134 {0, no_argument, 0, 0}
5135 };
5136
5137 static void
5138 usage (FILE * stream)
5139 {
5140 fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
5141 fprintf (stream, _(" Display information about the contents of ELF format files\n"));
5142 fprintf (stream, _(" Options are:\n"));
5143 fprintf (stream, _("\
5144 -a --all Equivalent to: -h -l -S -s -r -d -V -A -I\n"));
5145 fprintf (stream, _("\
5146 -h --file-header Display the ELF file header\n"));
5147 fprintf (stream, _("\
5148 -l --program-headers Display the program headers\n"));
5149 fprintf (stream, _("\
5150 --segments An alias for --program-headers\n"));
5151 fprintf (stream, _("\
5152 -S --section-headers Display the sections' header\n"));
5153 fprintf (stream, _("\
5154 --sections An alias for --section-headers\n"));
5155 fprintf (stream, _("\
5156 -g --section-groups Display the section groups\n"));
5157 fprintf (stream, _("\
5158 -t --section-details Display the section details\n"));
5159 fprintf (stream, _("\
5160 -e --headers Equivalent to: -h -l -S\n"));
5161 fprintf (stream, _("\
5162 -s --syms Display the symbol table\n"));
5163 fprintf (stream, _("\
5164 --symbols An alias for --syms\n"));
5165 fprintf (stream, _("\
5166 --dyn-syms Display the dynamic symbol table\n"));
5167 fprintf (stream, _("\
5168 --lto-syms Display LTO symbol tables\n"));
5169 fprintf (stream, _("\
5170 --sym-base=[0|8|10|16] \n\
5171 Force base for symbol sizes. The options are \n\
5172 mixed (the default), octal, decimal, hexadecimal.\n"));
5173 fprintf (stream, _("\
5174 -C --demangle[=STYLE] Decode mangled/processed symbol names\n"));
5175 display_demangler_styles (stream, _("\
5176 STYLE can be "));
5177 fprintf (stream, _("\
5178 --no-demangle Do not demangle low-level symbol names. (default)\n"));
5179 fprintf (stream, _("\
5180 --recurse-limit Enable a demangling recursion limit. (default)\n"));
5181 fprintf (stream, _("\
5182 --no-recurse-limit Disable a demangling recursion limit\n"));
5183 fprintf (stream, _("\
5184 -U[dlexhi] --unicode=[default|locale|escape|hex|highlight|invalid]\n\
5185 Display unicode characters as determined by the current locale\n\
5186 (default), escape sequences, \"<hex sequences>\", highlighted\n\
5187 escape sequences, or treat them as invalid and display as\n\
5188 \"{hex sequences}\"\n"));
5189 fprintf (stream, _("\
5190 -n --notes Display the core notes (if present)\n"));
5191 fprintf (stream, _("\
5192 -r --relocs Display the relocations (if present)\n"));
5193 fprintf (stream, _("\
5194 -u --unwind Display the unwind info (if present)\n"));
5195 fprintf (stream, _("\
5196 -d --dynamic Display the dynamic section (if present)\n"));
5197 fprintf (stream, _("\
5198 -V --version-info Display the version sections (if present)\n"));
5199 fprintf (stream, _("\
5200 -A --arch-specific Display architecture specific information (if any)\n"));
5201 fprintf (stream, _("\
5202 -c --archive-index Display the symbol/file index in an archive\n"));
5203 fprintf (stream, _("\
5204 -D --use-dynamic Use the dynamic section info when displaying symbols\n"));
5205 fprintf (stream, _("\
5206 -L --lint|--enable-checks\n\
5207 Display warning messages for possible problems\n"));
5208 fprintf (stream, _("\
5209 -x --hex-dump=<number|name>\n\
5210 Dump the contents of section <number|name> as bytes\n"));
5211 fprintf (stream, _("\
5212 -p --string-dump=<number|name>\n\
5213 Dump the contents of section <number|name> as strings\n"));
5214 fprintf (stream, _("\
5215 -R --relocated-dump=<number|name>\n\
5216 Dump the relocated contents of section <number|name>\n"));
5217 fprintf (stream, _("\
5218 -z --decompress Decompress section before dumping it\n"));
5219 fprintf (stream, _("\
5220 -w --debug-dump[a/=abbrev, A/=addr, r/=aranges, c/=cu_index, L/=decodedline,\n\
5221 f/=frames, F/=frames-interp, g/=gdb_index, i/=info, o/=loc,\n\
5222 m/=macro, p/=pubnames, t/=pubtypes, R/=Ranges, l/=rawline,\n\
5223 s/=str, O/=str-offsets, u/=trace_abbrev, T/=trace_aranges,\n\
5224 U/=trace_info]\n\
5225 Display the contents of DWARF debug sections\n"));
5226 fprintf (stream, _("\
5227 -wk --debug-dump=links Display the contents of sections that link to separate\n\
5228 debuginfo files\n"));
5229 fprintf (stream, _("\
5230 -P --process-links Display the contents of non-debug sections in separate\n\
5231 debuginfo files. (Implies -wK)\n"));
5232 #if DEFAULT_FOR_FOLLOW_LINKS
5233 fprintf (stream, _("\
5234 -wK --debug-dump=follow-links\n\
5235 Follow links to separate debug info files (default)\n"));
5236 fprintf (stream, _("\
5237 -wN --debug-dump=no-follow-links\n\
5238 Do not follow links to separate debug info files\n"));
5239 #else
5240 fprintf (stream, _("\
5241 -wK --debug-dump=follow-links\n\
5242 Follow links to separate debug info files\n"));
5243 fprintf (stream, _("\
5244 -wN --debug-dump=no-follow-links\n\
5245 Do not follow links to separate debug info files\n\
5246 (default)\n"));
5247 #endif
5248 #if HAVE_LIBDEBUGINFOD
5249 fprintf (stream, _("\
5250 -wD --debug-dump=use-debuginfod\n\
5251 When following links, also query debuginfod servers (default)\n"));
5252 fprintf (stream, _("\
5253 -wE --debug-dump=do-not-use-debuginfod\n\
5254 When following links, do not query debuginfod servers\n"));
5255 #endif
5256 fprintf (stream, _("\
5257 --dwarf-depth=N Do not display DIEs at depth N or greater\n"));
5258 fprintf (stream, _("\
5259 --dwarf-start=N Display DIEs starting at offset N\n"));
5260 #ifdef ENABLE_LIBCTF
5261 fprintf (stream, _("\
5262 --ctf=<number|name> Display CTF info from section <number|name>\n"));
5263 fprintf (stream, _("\
5264 --ctf-parent=<name> Use CTF archive member <name> as the CTF parent\n"));
5265 fprintf (stream, _("\
5266 --ctf-symbols=<number|name>\n\
5267 Use section <number|name> as the CTF external symtab\n"));
5268 fprintf (stream, _("\
5269 --ctf-strings=<number|name>\n\
5270 Use section <number|name> as the CTF external strtab\n"));
5271 #endif
5272
5273 #ifdef SUPPORT_DISASSEMBLY
5274 fprintf (stream, _("\
5275 -i --instruction-dump=<number|name>\n\
5276 Disassemble the contents of section <number|name>\n"));
5277 #endif
5278 fprintf (stream, _("\
5279 -I --histogram Display histogram of bucket list lengths\n"));
5280 fprintf (stream, _("\
5281 -W --wide Allow output width to exceed 80 characters\n"));
5282 fprintf (stream, _("\
5283 -T --silent-truncation If a symbol name is truncated, do not add [...] suffix\n"));
5284 fprintf (stream, _("\
5285 @<file> Read options from <file>\n"));
5286 fprintf (stream, _("\
5287 -H --help Display this information\n"));
5288 fprintf (stream, _("\
5289 -v --version Display the version number of readelf\n"));
5290
5291 if (REPORT_BUGS_TO[0] && stream == stdout)
5292 fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
5293
5294 exit (stream == stdout ? 0 : 1);
5295 }
5296
5297 /* Record the fact that the user wants the contents of section number
5298 SECTION to be displayed using the method(s) encoded as flags bits
5299 in TYPE. Note, TYPE can be zero if we are creating the array for
5300 the first time. */
5301
5302 static void
5303 request_dump_bynumber (struct dump_data *dumpdata,
5304 unsigned int section, dump_type type)
5305 {
5306 if (section >= dumpdata->num_dump_sects)
5307 {
5308 dump_type * new_dump_sects;
5309
5310 new_dump_sects = (dump_type *) calloc (section + 1,
5311 sizeof (* new_dump_sects));
5312
5313 if (new_dump_sects == NULL)
5314 error (_("Out of memory allocating dump request table.\n"));
5315 else
5316 {
5317 if (dumpdata->dump_sects)
5318 {
5319 /* Copy current flag settings. */
5320 memcpy (new_dump_sects, dumpdata->dump_sects,
5321 dumpdata->num_dump_sects * sizeof (* new_dump_sects));
5322
5323 free (dumpdata->dump_sects);
5324 }
5325
5326 dumpdata->dump_sects = new_dump_sects;
5327 dumpdata->num_dump_sects = section + 1;
5328 }
5329 }
5330
5331 if (dumpdata->dump_sects)
5332 dumpdata->dump_sects[section] |= type;
5333 }
5334
5335 /* Request a dump by section name. */
5336
5337 static void
5338 request_dump_byname (const char * section, dump_type type)
5339 {
5340 struct dump_list_entry * new_request;
5341
5342 new_request = (struct dump_list_entry *)
5343 malloc (sizeof (struct dump_list_entry));
5344 if (!new_request)
5345 error (_("Out of memory allocating dump request table.\n"));
5346
5347 new_request->name = strdup (section);
5348 if (!new_request->name)
5349 error (_("Out of memory allocating dump request table.\n"));
5350
5351 new_request->type = type;
5352
5353 new_request->next = dump_sects_byname;
5354 dump_sects_byname = new_request;
5355 }
5356
5357 static inline void
5358 request_dump (struct dump_data *dumpdata, dump_type type)
5359 {
5360 int section;
5361 char * cp;
5362
5363 do_dump = true;
5364 section = strtoul (optarg, & cp, 0);
5365
5366 if (! *cp && section >= 0)
5367 request_dump_bynumber (dumpdata, section, type);
5368 else
5369 request_dump_byname (optarg, type);
5370 }
5371
5372 static void
5373 parse_args (struct dump_data *dumpdata, int argc, char ** argv)
5374 {
5375 int c;
5376
5377 if (argc < 2)
5378 usage (stderr);
5379
5380 while ((c = getopt_long
5381 (argc, argv, "ACDHILNPR:STU:VWacdeghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
5382 {
5383 switch (c)
5384 {
5385 case 0:
5386 /* Long options. */
5387 break;
5388 case 'H':
5389 usage (stdout);
5390 break;
5391
5392 case 'a':
5393 do_syms = true;
5394 do_reloc = true;
5395 do_unwind = true;
5396 do_dynamic = true;
5397 do_header = true;
5398 do_sections = true;
5399 do_section_groups = true;
5400 do_segments = true;
5401 do_version = true;
5402 do_histogram = true;
5403 do_arch = true;
5404 do_notes = true;
5405 break;
5406
5407 case 'g':
5408 do_section_groups = true;
5409 break;
5410 case 't':
5411 case 'N':
5412 do_sections = true;
5413 do_section_details = true;
5414 break;
5415 case 'e':
5416 do_header = true;
5417 do_sections = true;
5418 do_segments = true;
5419 break;
5420 case 'A':
5421 do_arch = true;
5422 break;
5423 case 'D':
5424 do_using_dynamic = true;
5425 break;
5426 case 'r':
5427 do_reloc = true;
5428 break;
5429 case 'u':
5430 do_unwind = true;
5431 break;
5432 case 'h':
5433 do_header = true;
5434 break;
5435 case 'l':
5436 do_segments = true;
5437 break;
5438 case 's':
5439 do_syms = true;
5440 break;
5441 case 'S':
5442 do_sections = true;
5443 break;
5444 case 'd':
5445 do_dynamic = true;
5446 break;
5447 case 'I':
5448 do_histogram = true;
5449 break;
5450 case 'n':
5451 do_notes = true;
5452 break;
5453 case 'c':
5454 do_archive_index = true;
5455 break;
5456 case 'L':
5457 do_checks = true;
5458 break;
5459 case 'P':
5460 process_links = true;
5461 do_follow_links = true;
5462 dump_any_debugging = true;
5463 break;
5464 case 'x':
5465 request_dump (dumpdata, HEX_DUMP);
5466 break;
5467 case 'p':
5468 request_dump (dumpdata, STRING_DUMP);
5469 break;
5470 case 'R':
5471 request_dump (dumpdata, RELOC_DUMP);
5472 break;
5473 case 'z':
5474 decompress_dumps = true;
5475 break;
5476 case 'w':
5477 if (optarg == NULL)
5478 {
5479 do_debugging = true;
5480 do_dump = true;
5481 dump_any_debugging = true;
5482 dwarf_select_sections_all ();
5483 }
5484 else
5485 {
5486 do_debugging = false;
5487 if (dwarf_select_sections_by_letters (optarg))
5488 {
5489 do_dump = true;
5490 dump_any_debugging = true;
5491 }
5492 }
5493 break;
5494 case OPTION_DEBUG_DUMP:
5495 if (optarg == NULL)
5496 {
5497 do_dump = true;
5498 do_debugging = true;
5499 dump_any_debugging = true;
5500 dwarf_select_sections_all ();
5501 }
5502 else
5503 {
5504 do_debugging = false;
5505 if (dwarf_select_sections_by_names (optarg))
5506 {
5507 do_dump = true;
5508 dump_any_debugging = true;
5509 }
5510 }
5511 break;
5512 case OPTION_DWARF_DEPTH:
5513 {
5514 char *cp;
5515
5516 dwarf_cutoff_level = strtoul (optarg, & cp, 0);
5517 }
5518 break;
5519 case OPTION_DWARF_START:
5520 {
5521 char *cp;
5522
5523 dwarf_start_die = strtoul (optarg, & cp, 0);
5524 }
5525 break;
5526 case OPTION_DWARF_CHECK:
5527 dwarf_check = true;
5528 break;
5529 case OPTION_CTF_DUMP:
5530 do_ctf = true;
5531 request_dump (dumpdata, CTF_DUMP);
5532 break;
5533 case OPTION_CTF_SYMBOLS:
5534 free (dump_ctf_symtab_name);
5535 dump_ctf_symtab_name = strdup (optarg);
5536 break;
5537 case OPTION_CTF_STRINGS:
5538 free (dump_ctf_strtab_name);
5539 dump_ctf_strtab_name = strdup (optarg);
5540 break;
5541 case OPTION_CTF_PARENT:
5542 free (dump_ctf_parent_name);
5543 dump_ctf_parent_name = strdup (optarg);
5544 break;
5545 case OPTION_DYN_SYMS:
5546 do_dyn_syms = true;
5547 break;
5548 case OPTION_LTO_SYMS:
5549 do_lto_syms = true;
5550 break;
5551 #ifdef SUPPORT_DISASSEMBLY
5552 case 'i':
5553 request_dump (dumpdata, DISASS_DUMP);
5554 break;
5555 #endif
5556 case 'v':
5557 print_version (program_name);
5558 break;
5559 case 'V':
5560 do_version = true;
5561 break;
5562 case 'W':
5563 do_wide = true;
5564 break;
5565 case 'T':
5566 do_not_show_symbol_truncation = true;
5567 break;
5568 case 'C':
5569 do_demangle = true;
5570 if (optarg != NULL)
5571 {
5572 enum demangling_styles style;
5573
5574 style = cplus_demangle_name_to_style (optarg);
5575 if (style == unknown_demangling)
5576 error (_("unknown demangling style `%s'"), optarg);
5577
5578 cplus_demangle_set_style (style);
5579 }
5580 break;
5581 case OPTION_NO_DEMANGLING:
5582 do_demangle = false;
5583 break;
5584 case OPTION_RECURSE_LIMIT:
5585 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
5586 break;
5587 case OPTION_NO_RECURSE_LIMIT:
5588 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
5589 break;
5590 case OPTION_WITH_SYMBOL_VERSIONS:
5591 /* Ignored for backward compatibility. */
5592 break;
5593
5594 case 'U':
5595 if (optarg == NULL)
5596 error (_("Missing arg to -U/--unicode")); /* Can this happen ? */
5597 else if (streq (optarg, "default") || streq (optarg, "d"))
5598 unicode_display = unicode_default;
5599 else if (streq (optarg, "locale") || streq (optarg, "l"))
5600 unicode_display = unicode_locale;
5601 else if (streq (optarg, "escape") || streq (optarg, "e"))
5602 unicode_display = unicode_escape;
5603 else if (streq (optarg, "invalid") || streq (optarg, "i"))
5604 unicode_display = unicode_invalid;
5605 else if (streq (optarg, "hex") || streq (optarg, "x"))
5606 unicode_display = unicode_hex;
5607 else if (streq (optarg, "highlight") || streq (optarg, "h"))
5608 unicode_display = unicode_highlight;
5609 else
5610 error (_("invalid argument to -U/--unicode: %s"), optarg);
5611 break;
5612
5613 case OPTION_SYM_BASE:
5614 sym_base = 0;
5615 if (optarg != NULL)
5616 {
5617 sym_base = strtoul (optarg, NULL, 0);
5618 switch (sym_base)
5619 {
5620 case 0:
5621 case 8:
5622 case 10:
5623 case 16:
5624 break;
5625
5626 default:
5627 sym_base = 0;
5628 break;
5629 }
5630 }
5631 break;
5632
5633 default:
5634 /* xgettext:c-format */
5635 error (_("Invalid option '-%c'\n"), c);
5636 /* Fall through. */
5637 case '?':
5638 usage (stderr);
5639 }
5640 }
5641
5642 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
5643 && !do_segments && !do_header && !do_dump && !do_version
5644 && !do_histogram && !do_debugging && !do_arch && !do_notes
5645 && !do_section_groups && !do_archive_index
5646 && !do_dyn_syms && !do_lto_syms)
5647 {
5648 if (do_checks)
5649 {
5650 check_all = true;
5651 do_dynamic = do_syms = do_reloc = do_unwind = do_sections = true;
5652 do_segments = do_header = do_dump = do_version = true;
5653 do_histogram = do_debugging = do_arch = do_notes = true;
5654 do_section_groups = do_archive_index = do_dyn_syms = true;
5655 do_lto_syms = true;
5656 }
5657 else
5658 usage (stderr);
5659 }
5660 }
5661
5662 static const char *
5663 get_elf_class (unsigned int elf_class)
5664 {
5665 static char buff[32];
5666
5667 switch (elf_class)
5668 {
5669 case ELFCLASSNONE: return _("none");
5670 case ELFCLASS32: return "ELF32";
5671 case ELFCLASS64: return "ELF64";
5672 default:
5673 snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
5674 return buff;
5675 }
5676 }
5677
5678 static const char *
5679 get_data_encoding (unsigned int encoding)
5680 {
5681 static char buff[32];
5682
5683 switch (encoding)
5684 {
5685 case ELFDATANONE: return _("none");
5686 case ELFDATA2LSB: return _("2's complement, little endian");
5687 case ELFDATA2MSB: return _("2's complement, big endian");
5688 default:
5689 snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
5690 return buff;
5691 }
5692 }
5693
5694 static bool
5695 check_magic_number (Filedata * filedata, Elf_Internal_Ehdr * header)
5696 {
5697 if (header->e_ident[EI_MAG0] == ELFMAG0
5698 && header->e_ident[EI_MAG1] == ELFMAG1
5699 && header->e_ident[EI_MAG2] == ELFMAG2
5700 && header->e_ident[EI_MAG3] == ELFMAG3)
5701 return true;
5702
5703 /* Some compilers produce object files that are not in the ELF file format.
5704 As an aid to users of readelf, try to identify these cases and suggest
5705 alternative tools.
5706
5707 FIXME: It is not clear if all four bytes are used as constant magic
5708 valus by all compilers. It may be necessary to recode this function if
5709 different tools use different length sequences. */
5710
5711 static struct
5712 {
5713 unsigned char magic[4];
5714 const char * obj_message;
5715 const char * ar_message;
5716 }
5717 known_magic[] =
5718 {
5719 { { 'B', 'C', 0xc0, 0xde },
5720 N_("This is a LLVM bitcode file - try using llvm-bcanalyzer\n"),
5721 N_("This is a LLVM bitcode file - try extracing and then using llvm-bcanalyzer\n")
5722 },
5723 { { 'g', 'o', ' ', 'o' },
5724 N_("This is a GO binary file - try using 'go tool objdump' or 'go tool nm'\n"),
5725 NULL
5726 }
5727 };
5728 int i;
5729
5730 for (i = ARRAY_SIZE (known_magic); i--;)
5731 {
5732 if (header->e_ident[EI_MAG0] == known_magic[i].magic[0]
5733 && header->e_ident[EI_MAG1] == known_magic[i].magic[1]
5734 && header->e_ident[EI_MAG2] == known_magic[i].magic[2]
5735 && header->e_ident[EI_MAG3] == known_magic[i].magic[3])
5736 {
5737 /* Some compiler's analyzer tools do not handle archives,
5738 so we provide two different kinds of error message. */
5739 if (filedata->archive_file_size > 0
5740 && known_magic[i].ar_message != NULL)
5741 error ("%s", known_magic[i].ar_message);
5742 else
5743 error ("%s", known_magic[i].obj_message);
5744 return false;
5745 }
5746 }
5747
5748 error (_("Not an ELF file - it has the wrong magic bytes at the start\n"));
5749 return false;
5750 }
5751
5752 /* Decode the data held in 'filedata->file_header'. */
5753
5754 static bool
5755 process_file_header (Filedata * filedata)
5756 {
5757 Elf_Internal_Ehdr * header = & filedata->file_header;
5758
5759 if (! check_magic_number (filedata, header))
5760 return false;
5761
5762 if (! filedata->is_separate)
5763 init_dwarf_regnames_by_elf_machine_code (header->e_machine);
5764
5765 if (do_header)
5766 {
5767 unsigned i;
5768
5769 if (filedata->is_separate)
5770 printf (_("ELF Header in linked file '%s':\n"), filedata->file_name);
5771 else
5772 printf (_("ELF Header:\n"));
5773 printf (_(" Magic: "));
5774 for (i = 0; i < EI_NIDENT; i++)
5775 printf ("%2.2x ", header->e_ident[i]);
5776 printf ("\n");
5777 printf (_(" Class: %s\n"),
5778 get_elf_class (header->e_ident[EI_CLASS]));
5779 printf (_(" Data: %s\n"),
5780 get_data_encoding (header->e_ident[EI_DATA]));
5781 printf (_(" Version: %d%s\n"),
5782 header->e_ident[EI_VERSION],
5783 (header->e_ident[EI_VERSION] == EV_CURRENT
5784 ? _(" (current)")
5785 : (header->e_ident[EI_VERSION] != EV_NONE
5786 ? _(" <unknown>")
5787 : "")));
5788 printf (_(" OS/ABI: %s\n"),
5789 get_osabi_name (filedata, header->e_ident[EI_OSABI]));
5790 printf (_(" ABI Version: %d\n"),
5791 header->e_ident[EI_ABIVERSION]);
5792 printf (_(" Type: %s\n"),
5793 get_file_type (filedata));
5794 printf (_(" Machine: %s\n"),
5795 get_machine_name (header->e_machine));
5796 printf (_(" Version: 0x%lx\n"),
5797 header->e_version);
5798
5799 printf (_(" Entry point address: "));
5800 print_vma (header->e_entry, PREFIX_HEX);
5801 printf (_("\n Start of program headers: "));
5802 print_vma (header->e_phoff, DEC);
5803 printf (_(" (bytes into file)\n Start of section headers: "));
5804 print_vma (header->e_shoff, DEC);
5805 printf (_(" (bytes into file)\n"));
5806
5807 printf (_(" Flags: 0x%lx%s\n"),
5808 header->e_flags,
5809 get_machine_flags (filedata, header->e_flags, header->e_machine));
5810 printf (_(" Size of this header: %u (bytes)\n"),
5811 header->e_ehsize);
5812 printf (_(" Size of program headers: %u (bytes)\n"),
5813 header->e_phentsize);
5814 printf (_(" Number of program headers: %u"),
5815 header->e_phnum);
5816 if (filedata->section_headers != NULL
5817 && header->e_phnum == PN_XNUM
5818 && filedata->section_headers[0].sh_info != 0)
5819 printf (" (%u)", filedata->section_headers[0].sh_info);
5820 putc ('\n', stdout);
5821 printf (_(" Size of section headers: %u (bytes)\n"),
5822 header->e_shentsize);
5823 printf (_(" Number of section headers: %u"),
5824 header->e_shnum);
5825 if (filedata->section_headers != NULL && header->e_shnum == SHN_UNDEF)
5826 {
5827 header->e_shnum = filedata->section_headers[0].sh_size;
5828 printf (" (%u)", header->e_shnum);
5829 }
5830 putc ('\n', stdout);
5831 printf (_(" Section header string table index: %u"),
5832 header->e_shstrndx);
5833 if (filedata->section_headers != NULL
5834 && header->e_shstrndx == (SHN_XINDEX & 0xffff))
5835 {
5836 header->e_shstrndx = filedata->section_headers[0].sh_link;
5837 printf (" (%u)", header->e_shstrndx);
5838 }
5839 if (header->e_shstrndx != SHN_UNDEF
5840 && header->e_shstrndx >= header->e_shnum)
5841 {
5842 header->e_shstrndx = SHN_UNDEF;
5843 printf (_(" <corrupt: out of range>"));
5844 }
5845 putc ('\n', stdout);
5846 }
5847
5848 if (filedata->section_headers != NULL)
5849 {
5850 if (header->e_phnum == PN_XNUM
5851 && filedata->section_headers[0].sh_info != 0)
5852 {
5853 /* Throw away any cached read of PN_XNUM headers. */
5854 free (filedata->program_headers);
5855 filedata->program_headers = NULL;
5856 header->e_phnum = filedata->section_headers[0].sh_info;
5857 }
5858 if (header->e_shnum == SHN_UNDEF)
5859 header->e_shnum = filedata->section_headers[0].sh_size;
5860 if (header->e_shstrndx == (SHN_XINDEX & 0xffff))
5861 header->e_shstrndx = filedata->section_headers[0].sh_link;
5862 if (header->e_shstrndx >= header->e_shnum)
5863 header->e_shstrndx = SHN_UNDEF;
5864 }
5865
5866 return true;
5867 }
5868
5869 /* Read in the program headers from FILEDATA and store them in PHEADERS.
5870 Returns TRUE upon success, FALSE otherwise. Loads 32-bit headers. */
5871
5872 static bool
5873 get_32bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
5874 {
5875 Elf32_External_Phdr * phdrs;
5876 Elf32_External_Phdr * external;
5877 Elf_Internal_Phdr * internal;
5878 unsigned int i;
5879 unsigned int size = filedata->file_header.e_phentsize;
5880 unsigned int num = filedata->file_header.e_phnum;
5881
5882 /* PR binutils/17531: Cope with unexpected section header sizes. */
5883 if (size == 0 || num == 0)
5884 return false;
5885 if (size < sizeof * phdrs)
5886 {
5887 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
5888 return false;
5889 }
5890 if (size > sizeof * phdrs)
5891 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
5892
5893 phdrs = (Elf32_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
5894 size, num, _("program headers"));
5895 if (phdrs == NULL)
5896 return false;
5897
5898 for (i = 0, internal = pheaders, external = phdrs;
5899 i < filedata->file_header.e_phnum;
5900 i++, internal++, external++)
5901 {
5902 internal->p_type = BYTE_GET (external->p_type);
5903 internal->p_offset = BYTE_GET (external->p_offset);
5904 internal->p_vaddr = BYTE_GET (external->p_vaddr);
5905 internal->p_paddr = BYTE_GET (external->p_paddr);
5906 internal->p_filesz = BYTE_GET (external->p_filesz);
5907 internal->p_memsz = BYTE_GET (external->p_memsz);
5908 internal->p_flags = BYTE_GET (external->p_flags);
5909 internal->p_align = BYTE_GET (external->p_align);
5910 }
5911
5912 free (phdrs);
5913 return true;
5914 }
5915
5916 /* Read in the program headers from FILEDATA and store them in PHEADERS.
5917 Returns TRUE upon success, FALSE otherwise. Loads 64-bit headers. */
5918
5919 static bool
5920 get_64bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
5921 {
5922 Elf64_External_Phdr * phdrs;
5923 Elf64_External_Phdr * external;
5924 Elf_Internal_Phdr * internal;
5925 unsigned int i;
5926 unsigned int size = filedata->file_header.e_phentsize;
5927 unsigned int num = filedata->file_header.e_phnum;
5928
5929 /* PR binutils/17531: Cope with unexpected section header sizes. */
5930 if (size == 0 || num == 0)
5931 return false;
5932 if (size < sizeof * phdrs)
5933 {
5934 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
5935 return false;
5936 }
5937 if (size > sizeof * phdrs)
5938 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
5939
5940 phdrs = (Elf64_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
5941 size, num, _("program headers"));
5942 if (!phdrs)
5943 return false;
5944
5945 for (i = 0, internal = pheaders, external = phdrs;
5946 i < filedata->file_header.e_phnum;
5947 i++, internal++, external++)
5948 {
5949 internal->p_type = BYTE_GET (external->p_type);
5950 internal->p_flags = BYTE_GET (external->p_flags);
5951 internal->p_offset = BYTE_GET (external->p_offset);
5952 internal->p_vaddr = BYTE_GET (external->p_vaddr);
5953 internal->p_paddr = BYTE_GET (external->p_paddr);
5954 internal->p_filesz = BYTE_GET (external->p_filesz);
5955 internal->p_memsz = BYTE_GET (external->p_memsz);
5956 internal->p_align = BYTE_GET (external->p_align);
5957 }
5958
5959 free (phdrs);
5960 return true;
5961 }
5962
5963 /* Returns TRUE if the program headers were read into `program_headers'. */
5964
5965 static bool
5966 get_program_headers (Filedata * filedata)
5967 {
5968 Elf_Internal_Phdr * phdrs;
5969
5970 /* Check cache of prior read. */
5971 if (filedata->program_headers != NULL)
5972 return true;
5973
5974 /* Be kind to memory checkers by looking for
5975 e_phnum values which we know must be invalid. */
5976 if (filedata->file_header.e_phnum
5977 * (is_32bit_elf ? sizeof (Elf32_External_Phdr) : sizeof (Elf64_External_Phdr))
5978 >= filedata->file_size)
5979 {
5980 error (_("Too many program headers - %#x - the file is not that big\n"),
5981 filedata->file_header.e_phnum);
5982 return false;
5983 }
5984
5985 phdrs = (Elf_Internal_Phdr *) cmalloc (filedata->file_header.e_phnum,
5986 sizeof (Elf_Internal_Phdr));
5987 if (phdrs == NULL)
5988 {
5989 error (_("Out of memory reading %u program headers\n"),
5990 filedata->file_header.e_phnum);
5991 return false;
5992 }
5993
5994 if (is_32bit_elf
5995 ? get_32bit_program_headers (filedata, phdrs)
5996 : get_64bit_program_headers (filedata, phdrs))
5997 {
5998 filedata->program_headers = phdrs;
5999 return true;
6000 }
6001
6002 free (phdrs);
6003 return false;
6004 }
6005
6006 /* Print program header info and locate dynamic section. */
6007
6008 static void
6009 process_program_headers (Filedata * filedata)
6010 {
6011 Elf_Internal_Phdr * segment;
6012 unsigned int i;
6013 Elf_Internal_Phdr * previous_load = NULL;
6014
6015 if (filedata->file_header.e_phnum == 0)
6016 {
6017 /* PR binutils/12467. */
6018 if (filedata->file_header.e_phoff != 0)
6019 warn (_("possibly corrupt ELF header - it has a non-zero program"
6020 " header offset, but no program headers\n"));
6021 else if (do_segments)
6022 {
6023 if (filedata->is_separate)
6024 printf (_("\nThere are no program headers in linked file '%s'.\n"),
6025 filedata->file_name);
6026 else
6027 printf (_("\nThere are no program headers in this file.\n"));
6028 }
6029 goto no_headers;
6030 }
6031
6032 if (do_segments && !do_header)
6033 {
6034 if (filedata->is_separate)
6035 printf ("\nIn linked file '%s' the ELF file type is %s\n",
6036 filedata->file_name, get_file_type (filedata));
6037 else
6038 printf (_("\nElf file type is %s\n"), get_file_type (filedata));
6039 printf (_("Entry point 0x%" PRIx64 "\n"),
6040 filedata->file_header.e_entry);
6041 printf (ngettext ("There is %d program header,"
6042 " starting at offset %" PRIu64 "\n",
6043 "There are %d program headers,"
6044 " starting at offset %" PRIu64 "\n",
6045 filedata->file_header.e_phnum),
6046 filedata->file_header.e_phnum,
6047 filedata->file_header.e_phoff);
6048 }
6049
6050 if (! get_program_headers (filedata))
6051 goto no_headers;
6052
6053 if (do_segments)
6054 {
6055 if (filedata->file_header.e_phnum > 1)
6056 printf (_("\nProgram Headers:\n"));
6057 else
6058 printf (_("\nProgram Headers:\n"));
6059
6060 if (is_32bit_elf)
6061 printf
6062 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
6063 else if (do_wide)
6064 printf
6065 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
6066 else
6067 {
6068 printf
6069 (_(" Type Offset VirtAddr PhysAddr\n"));
6070 printf
6071 (_(" FileSiz MemSiz Flags Align\n"));
6072 }
6073 }
6074
6075 unsigned long dynamic_addr = 0;
6076 uint64_t dynamic_size = 0;
6077 for (i = 0, segment = filedata->program_headers;
6078 i < filedata->file_header.e_phnum;
6079 i++, segment++)
6080 {
6081 if (do_segments)
6082 {
6083 printf (" %-14.14s ", get_segment_type (filedata, segment->p_type));
6084
6085 if (is_32bit_elf)
6086 {
6087 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
6088 printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
6089 printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
6090 printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
6091 printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
6092 printf ("%c%c%c ",
6093 (segment->p_flags & PF_R ? 'R' : ' '),
6094 (segment->p_flags & PF_W ? 'W' : ' '),
6095 (segment->p_flags & PF_X ? 'E' : ' '));
6096 printf ("%#lx", (unsigned long) segment->p_align);
6097 }
6098 else if (do_wide)
6099 {
6100 if ((unsigned long) segment->p_offset == segment->p_offset)
6101 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
6102 else
6103 {
6104 print_vma (segment->p_offset, FULL_HEX);
6105 putchar (' ');
6106 }
6107
6108 print_vma (segment->p_vaddr, FULL_HEX);
6109 putchar (' ');
6110 print_vma (segment->p_paddr, FULL_HEX);
6111 putchar (' ');
6112
6113 if ((unsigned long) segment->p_filesz == segment->p_filesz)
6114 printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
6115 else
6116 {
6117 print_vma (segment->p_filesz, FULL_HEX);
6118 putchar (' ');
6119 }
6120
6121 if ((unsigned long) segment->p_memsz == segment->p_memsz)
6122 printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
6123 else
6124 {
6125 print_vma (segment->p_memsz, FULL_HEX);
6126 }
6127
6128 printf (" %c%c%c ",
6129 (segment->p_flags & PF_R ? 'R' : ' '),
6130 (segment->p_flags & PF_W ? 'W' : ' '),
6131 (segment->p_flags & PF_X ? 'E' : ' '));
6132
6133 if ((unsigned long) segment->p_align == segment->p_align)
6134 printf ("%#lx", (unsigned long) segment->p_align);
6135 else
6136 {
6137 print_vma (segment->p_align, PREFIX_HEX);
6138 }
6139 }
6140 else
6141 {
6142 print_vma (segment->p_offset, FULL_HEX);
6143 putchar (' ');
6144 print_vma (segment->p_vaddr, FULL_HEX);
6145 putchar (' ');
6146 print_vma (segment->p_paddr, FULL_HEX);
6147 printf ("\n ");
6148 print_vma (segment->p_filesz, FULL_HEX);
6149 putchar (' ');
6150 print_vma (segment->p_memsz, FULL_HEX);
6151 printf (" %c%c%c ",
6152 (segment->p_flags & PF_R ? 'R' : ' '),
6153 (segment->p_flags & PF_W ? 'W' : ' '),
6154 (segment->p_flags & PF_X ? 'E' : ' '));
6155 print_vma (segment->p_align, PREFIX_HEX);
6156 }
6157
6158 putc ('\n', stdout);
6159 }
6160
6161 switch (segment->p_type)
6162 {
6163 case PT_LOAD:
6164 #if 0 /* Do not warn about out of order PT_LOAD segments. Although officially
6165 required by the ELF standard, several programs, including the Linux
6166 kernel, make use of non-ordered segments. */
6167 if (previous_load
6168 && previous_load->p_vaddr > segment->p_vaddr)
6169 error (_("LOAD segments must be sorted in order of increasing VirtAddr\n"));
6170 #endif
6171 if (segment->p_memsz < segment->p_filesz)
6172 error (_("the segment's file size is larger than its memory size\n"));
6173 previous_load = segment;
6174 break;
6175
6176 case PT_PHDR:
6177 /* PR 20815 - Verify that the program header is loaded into memory. */
6178 if (i > 0 && previous_load != NULL)
6179 error (_("the PHDR segment must occur before any LOAD segment\n"));
6180 if (filedata->file_header.e_machine != EM_PARISC)
6181 {
6182 unsigned int j;
6183
6184 for (j = 1; j < filedata->file_header.e_phnum; j++)
6185 {
6186 Elf_Internal_Phdr *load = filedata->program_headers + j;
6187 if (load->p_type == PT_LOAD
6188 && load->p_offset <= segment->p_offset
6189 && (load->p_offset + load->p_filesz
6190 >= segment->p_offset + segment->p_filesz)
6191 && load->p_vaddr <= segment->p_vaddr
6192 && (load->p_vaddr + load->p_filesz
6193 >= segment->p_vaddr + segment->p_filesz))
6194 break;
6195 }
6196 if (j == filedata->file_header.e_phnum)
6197 error (_("the PHDR segment is not covered by a LOAD segment\n"));
6198 }
6199 break;
6200
6201 case PT_DYNAMIC:
6202 if (dynamic_addr)
6203 error (_("more than one dynamic segment\n"));
6204
6205 /* By default, assume that the .dynamic section is the first
6206 section in the DYNAMIC segment. */
6207 dynamic_addr = segment->p_offset;
6208 dynamic_size = segment->p_filesz;
6209
6210 /* Try to locate the .dynamic section. If there is
6211 a section header table, we can easily locate it. */
6212 if (filedata->section_headers != NULL)
6213 {
6214 Elf_Internal_Shdr * sec;
6215
6216 sec = find_section (filedata, ".dynamic");
6217 if (sec == NULL || sec->sh_size == 0)
6218 {
6219 /* A corresponding .dynamic section is expected, but on
6220 IA-64/OpenVMS it is OK for it to be missing. */
6221 if (!is_ia64_vms (filedata))
6222 error (_("no .dynamic section in the dynamic segment\n"));
6223 break;
6224 }
6225
6226 if (sec->sh_type == SHT_NOBITS)
6227 {
6228 dynamic_addr = 0;
6229 dynamic_size = 0;
6230 break;
6231 }
6232
6233 dynamic_addr = sec->sh_offset;
6234 dynamic_size = sec->sh_size;
6235
6236 /* The PT_DYNAMIC segment, which is used by the run-time
6237 loader, should exactly match the .dynamic section. */
6238 if (do_checks
6239 && (dynamic_addr != segment->p_offset
6240 || dynamic_size != segment->p_filesz))
6241 warn (_("\
6242 the .dynamic section is not the same as the dynamic segment\n"));
6243 }
6244
6245 /* PR binutils/17512: Avoid corrupt dynamic section info in the
6246 segment. Check this after matching against the section headers
6247 so we don't warn on debuginfo file (which have NOBITS .dynamic
6248 sections). */
6249 if (dynamic_addr > filedata->file_size
6250 || (dynamic_size > filedata->file_size - dynamic_addr))
6251 {
6252 error (_("the dynamic segment offset + size exceeds the size of the file\n"));
6253 dynamic_addr = 0;
6254 dynamic_size = 0;
6255 }
6256 break;
6257
6258 case PT_INTERP:
6259 if (segment->p_offset >= filedata->file_size
6260 || segment->p_filesz > filedata->file_size - segment->p_offset
6261 || segment->p_filesz - 1 >= (size_t) -2
6262 || fseek (filedata->handle,
6263 filedata->archive_file_offset + (long) segment->p_offset,
6264 SEEK_SET))
6265 error (_("Unable to find program interpreter name\n"));
6266 else
6267 {
6268 size_t len = segment->p_filesz;
6269 free (filedata->program_interpreter);
6270 filedata->program_interpreter = xmalloc (len + 1);
6271 len = fread (filedata->program_interpreter, 1, len,
6272 filedata->handle);
6273 filedata->program_interpreter[len] = 0;
6274
6275 if (do_segments)
6276 printf (_(" [Requesting program interpreter: %s]\n"),
6277 filedata->program_interpreter);
6278 }
6279 break;
6280 }
6281 }
6282
6283 if (do_segments
6284 && filedata->section_headers != NULL
6285 && filedata->string_table != NULL)
6286 {
6287 printf (_("\n Section to Segment mapping:\n"));
6288 printf (_(" Segment Sections...\n"));
6289
6290 for (i = 0; i < filedata->file_header.e_phnum; i++)
6291 {
6292 unsigned int j;
6293 Elf_Internal_Shdr * section;
6294
6295 segment = filedata->program_headers + i;
6296 section = filedata->section_headers + 1;
6297
6298 printf (" %2.2d ", i);
6299
6300 for (j = 1; j < filedata->file_header.e_shnum; j++, section++)
6301 {
6302 if (!ELF_TBSS_SPECIAL (section, segment)
6303 && ELF_SECTION_IN_SEGMENT_STRICT (section, segment))
6304 printf ("%s ", printable_section_name (filedata, section));
6305 }
6306
6307 putc ('\n',stdout);
6308 }
6309 }
6310
6311 filedata->dynamic_addr = dynamic_addr;
6312 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
6313 return;
6314
6315 no_headers:
6316 filedata->dynamic_addr = 0;
6317 filedata->dynamic_size = 1;
6318 }
6319
6320
6321 /* Find the file offset corresponding to VMA by using the program headers. */
6322
6323 static long
6324 offset_from_vma (Filedata * filedata, uint64_t vma, uint64_t size)
6325 {
6326 Elf_Internal_Phdr * seg;
6327
6328 if (! get_program_headers (filedata))
6329 {
6330 warn (_("Cannot interpret virtual addresses without program headers.\n"));
6331 return (long) vma;
6332 }
6333
6334 for (seg = filedata->program_headers;
6335 seg < filedata->program_headers + filedata->file_header.e_phnum;
6336 ++seg)
6337 {
6338 if (seg->p_type != PT_LOAD)
6339 continue;
6340
6341 if (vma >= (seg->p_vaddr & -seg->p_align)
6342 && vma + size <= seg->p_vaddr + seg->p_filesz)
6343 return vma - seg->p_vaddr + seg->p_offset;
6344 }
6345
6346 warn (_("Virtual address 0x%lx not located in any PT_LOAD segment.\n"),
6347 (unsigned long) vma);
6348 return (long) vma;
6349 }
6350
6351
6352 /* Allocate memory and load the sections headers into FILEDATA->filedata->section_headers.
6353 If PROBE is true, this is just a probe and we do not generate any error
6354 messages if the load fails. */
6355
6356 static bool
6357 get_32bit_section_headers (Filedata * filedata, bool probe)
6358 {
6359 Elf32_External_Shdr * shdrs;
6360 Elf_Internal_Shdr * internal;
6361 unsigned int i;
6362 unsigned int size = filedata->file_header.e_shentsize;
6363 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
6364
6365 /* PR binutils/17531: Cope with unexpected section header sizes. */
6366 if (size == 0 || num == 0)
6367 return false;
6368
6369 /* The section header cannot be at the start of the file - that is
6370 where the ELF file header is located. A file with absolutely no
6371 sections in it will use a shoff of 0. */
6372 if (filedata->file_header.e_shoff == 0)
6373 return false;
6374
6375 if (size < sizeof * shdrs)
6376 {
6377 if (! probe)
6378 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
6379 return false;
6380 }
6381 if (!probe && size > sizeof * shdrs)
6382 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
6383
6384 shdrs = (Elf32_External_Shdr *) get_data (NULL, filedata, filedata->file_header.e_shoff,
6385 size, num,
6386 probe ? NULL : _("section headers"));
6387 if (shdrs == NULL)
6388 return false;
6389
6390 filedata->section_headers = (Elf_Internal_Shdr *)
6391 cmalloc (num, sizeof (Elf_Internal_Shdr));
6392 if (filedata->section_headers == NULL)
6393 {
6394 if (!probe)
6395 error (_("Out of memory reading %u section headers\n"), num);
6396 free (shdrs);
6397 return false;
6398 }
6399
6400 for (i = 0, internal = filedata->section_headers;
6401 i < num;
6402 i++, internal++)
6403 {
6404 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
6405 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
6406 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
6407 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
6408 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
6409 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
6410 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
6411 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
6412 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
6413 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
6414 if (!probe && internal->sh_link > num)
6415 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
6416 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
6417 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
6418 }
6419
6420 free (shdrs);
6421 return true;
6422 }
6423
6424 /* Like get_32bit_section_headers, except that it fetches 64-bit headers. */
6425
6426 static bool
6427 get_64bit_section_headers (Filedata * filedata, bool probe)
6428 {
6429 Elf64_External_Shdr * shdrs;
6430 Elf_Internal_Shdr * internal;
6431 unsigned int i;
6432 unsigned int size = filedata->file_header.e_shentsize;
6433 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
6434
6435 /* PR binutils/17531: Cope with unexpected section header sizes. */
6436 if (size == 0 || num == 0)
6437 return false;
6438
6439 /* The section header cannot be at the start of the file - that is
6440 where the ELF file header is located. A file with absolutely no
6441 sections in it will use a shoff of 0. */
6442 if (filedata->file_header.e_shoff == 0)
6443 return false;
6444
6445 if (size < sizeof * shdrs)
6446 {
6447 if (! probe)
6448 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
6449 return false;
6450 }
6451
6452 if (! probe && size > sizeof * shdrs)
6453 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
6454
6455 shdrs = (Elf64_External_Shdr *) get_data (NULL, filedata,
6456 filedata->file_header.e_shoff,
6457 size, num,
6458 probe ? NULL : _("section headers"));
6459 if (shdrs == NULL)
6460 return false;
6461
6462 filedata->section_headers = (Elf_Internal_Shdr *)
6463 cmalloc (num, sizeof (Elf_Internal_Shdr));
6464 if (filedata->section_headers == NULL)
6465 {
6466 if (! probe)
6467 error (_("Out of memory reading %u section headers\n"), num);
6468 free (shdrs);
6469 return false;
6470 }
6471
6472 for (i = 0, internal = filedata->section_headers;
6473 i < num;
6474 i++, internal++)
6475 {
6476 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
6477 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
6478 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
6479 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
6480 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
6481 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
6482 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
6483 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
6484 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
6485 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
6486 if (!probe && internal->sh_link > num)
6487 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
6488 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
6489 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
6490 }
6491
6492 free (shdrs);
6493 return true;
6494 }
6495
6496 static bool
6497 get_section_headers (Filedata *filedata, bool probe)
6498 {
6499 if (filedata->section_headers != NULL)
6500 return true;
6501
6502 if (is_32bit_elf)
6503 return get_32bit_section_headers (filedata, probe);
6504 else
6505 return get_64bit_section_headers (filedata, probe);
6506 }
6507
6508 static Elf_Internal_Sym *
6509 get_32bit_elf_symbols (Filedata * filedata,
6510 Elf_Internal_Shdr * section,
6511 unsigned long * num_syms_return)
6512 {
6513 unsigned long number = 0;
6514 Elf32_External_Sym * esyms = NULL;
6515 Elf_External_Sym_Shndx * shndx = NULL;
6516 Elf_Internal_Sym * isyms = NULL;
6517 Elf_Internal_Sym * psym;
6518 unsigned int j;
6519 elf_section_list * entry;
6520
6521 if (section->sh_size == 0)
6522 {
6523 if (num_syms_return != NULL)
6524 * num_syms_return = 0;
6525 return NULL;
6526 }
6527
6528 /* Run some sanity checks first. */
6529 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
6530 {
6531 error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
6532 printable_section_name (filedata, section),
6533 (unsigned long) section->sh_entsize);
6534 goto exit_point;
6535 }
6536
6537 if (section->sh_size > filedata->file_size)
6538 {
6539 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
6540 printable_section_name (filedata, section),
6541 (unsigned long) section->sh_size);
6542 goto exit_point;
6543 }
6544
6545 number = section->sh_size / section->sh_entsize;
6546
6547 if (number * sizeof (Elf32_External_Sym) > section->sh_size + 1)
6548 {
6549 error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
6550 (unsigned long) section->sh_size,
6551 printable_section_name (filedata, section),
6552 (unsigned long) section->sh_entsize);
6553 goto exit_point;
6554 }
6555
6556 esyms = (Elf32_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
6557 section->sh_size, _("symbols"));
6558 if (esyms == NULL)
6559 goto exit_point;
6560
6561 shndx = NULL;
6562 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
6563 {
6564 if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
6565 continue;
6566
6567 if (shndx != NULL)
6568 {
6569 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
6570 free (shndx);
6571 }
6572
6573 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
6574 entry->hdr->sh_offset,
6575 1, entry->hdr->sh_size,
6576 _("symbol table section indices"));
6577 if (shndx == NULL)
6578 goto exit_point;
6579
6580 /* PR17531: file: heap-buffer-overflow */
6581 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
6582 {
6583 error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
6584 printable_section_name (filedata, entry->hdr),
6585 (unsigned long) entry->hdr->sh_size,
6586 (unsigned long) section->sh_size);
6587 goto exit_point;
6588 }
6589 }
6590
6591 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
6592
6593 if (isyms == NULL)
6594 {
6595 error (_("Out of memory reading %lu symbols\n"),
6596 (unsigned long) number);
6597 goto exit_point;
6598 }
6599
6600 for (j = 0, psym = isyms; j < number; j++, psym++)
6601 {
6602 psym->st_name = BYTE_GET (esyms[j].st_name);
6603 psym->st_value = BYTE_GET (esyms[j].st_value);
6604 psym->st_size = BYTE_GET (esyms[j].st_size);
6605 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
6606 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
6607 psym->st_shndx
6608 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
6609 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
6610 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
6611 psym->st_info = BYTE_GET (esyms[j].st_info);
6612 psym->st_other = BYTE_GET (esyms[j].st_other);
6613 }
6614
6615 exit_point:
6616 free (shndx);
6617 free (esyms);
6618
6619 if (num_syms_return != NULL)
6620 * num_syms_return = isyms == NULL ? 0 : number;
6621
6622 return isyms;
6623 }
6624
6625 static Elf_Internal_Sym *
6626 get_64bit_elf_symbols (Filedata * filedata,
6627 Elf_Internal_Shdr * section,
6628 unsigned long * num_syms_return)
6629 {
6630 unsigned long number = 0;
6631 Elf64_External_Sym * esyms = NULL;
6632 Elf_External_Sym_Shndx * shndx = NULL;
6633 Elf_Internal_Sym * isyms = NULL;
6634 Elf_Internal_Sym * psym;
6635 unsigned int j;
6636 elf_section_list * entry;
6637
6638 if (section->sh_size == 0)
6639 {
6640 if (num_syms_return != NULL)
6641 * num_syms_return = 0;
6642 return NULL;
6643 }
6644
6645 /* Run some sanity checks first. */
6646 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
6647 {
6648 error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
6649 printable_section_name (filedata, section),
6650 (unsigned long) section->sh_entsize);
6651 goto exit_point;
6652 }
6653
6654 if (section->sh_size > filedata->file_size)
6655 {
6656 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
6657 printable_section_name (filedata, section),
6658 (unsigned long) section->sh_size);
6659 goto exit_point;
6660 }
6661
6662 number = section->sh_size / section->sh_entsize;
6663
6664 if (number * sizeof (Elf64_External_Sym) > section->sh_size + 1)
6665 {
6666 error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
6667 (unsigned long) section->sh_size,
6668 printable_section_name (filedata, section),
6669 (unsigned long) section->sh_entsize);
6670 goto exit_point;
6671 }
6672
6673 esyms = (Elf64_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
6674 section->sh_size, _("symbols"));
6675 if (!esyms)
6676 goto exit_point;
6677
6678 shndx = NULL;
6679 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
6680 {
6681 if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
6682 continue;
6683
6684 if (shndx != NULL)
6685 {
6686 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
6687 free (shndx);
6688 }
6689
6690 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
6691 entry->hdr->sh_offset,
6692 1, entry->hdr->sh_size,
6693 _("symbol table section indices"));
6694 if (shndx == NULL)
6695 goto exit_point;
6696
6697 /* PR17531: file: heap-buffer-overflow */
6698 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
6699 {
6700 error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
6701 printable_section_name (filedata, entry->hdr),
6702 (unsigned long) entry->hdr->sh_size,
6703 (unsigned long) section->sh_size);
6704 goto exit_point;
6705 }
6706 }
6707
6708 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
6709
6710 if (isyms == NULL)
6711 {
6712 error (_("Out of memory reading %lu symbols\n"),
6713 (unsigned long) number);
6714 goto exit_point;
6715 }
6716
6717 for (j = 0, psym = isyms; j < number; j++, psym++)
6718 {
6719 psym->st_name = BYTE_GET (esyms[j].st_name);
6720 psym->st_info = BYTE_GET (esyms[j].st_info);
6721 psym->st_other = BYTE_GET (esyms[j].st_other);
6722 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
6723
6724 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
6725 psym->st_shndx
6726 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
6727 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
6728 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
6729
6730 psym->st_value = BYTE_GET (esyms[j].st_value);
6731 psym->st_size = BYTE_GET (esyms[j].st_size);
6732 }
6733
6734 exit_point:
6735 free (shndx);
6736 free (esyms);
6737
6738 if (num_syms_return != NULL)
6739 * num_syms_return = isyms == NULL ? 0 : number;
6740
6741 return isyms;
6742 }
6743
6744 static Elf_Internal_Sym *
6745 get_elf_symbols (Filedata *filedata,
6746 Elf_Internal_Shdr *section,
6747 unsigned long *num_syms_return)
6748 {
6749 if (is_32bit_elf)
6750 return get_32bit_elf_symbols (filedata, section, num_syms_return);
6751 else
6752 return get_64bit_elf_symbols (filedata, section, num_syms_return);
6753 }
6754
6755 static const char *
6756 get_elf_section_flags (Filedata * filedata, uint64_t sh_flags)
6757 {
6758 static char buff[1024];
6759 char * p = buff;
6760 unsigned int field_size = is_32bit_elf ? 8 : 16;
6761 signed int sindex;
6762 unsigned int size = sizeof (buff) - (field_size + 4 + 1);
6763 uint64_t os_flags = 0;
6764 uint64_t proc_flags = 0;
6765 uint64_t unknown_flags = 0;
6766 static const struct
6767 {
6768 const char * str;
6769 unsigned int len;
6770 }
6771 flags [] =
6772 {
6773 /* 0 */ { STRING_COMMA_LEN ("WRITE") },
6774 /* 1 */ { STRING_COMMA_LEN ("ALLOC") },
6775 /* 2 */ { STRING_COMMA_LEN ("EXEC") },
6776 /* 3 */ { STRING_COMMA_LEN ("MERGE") },
6777 /* 4 */ { STRING_COMMA_LEN ("STRINGS") },
6778 /* 5 */ { STRING_COMMA_LEN ("INFO LINK") },
6779 /* 6 */ { STRING_COMMA_LEN ("LINK ORDER") },
6780 /* 7 */ { STRING_COMMA_LEN ("OS NONCONF") },
6781 /* 8 */ { STRING_COMMA_LEN ("GROUP") },
6782 /* 9 */ { STRING_COMMA_LEN ("TLS") },
6783 /* IA-64 specific. */
6784 /* 10 */ { STRING_COMMA_LEN ("SHORT") },
6785 /* 11 */ { STRING_COMMA_LEN ("NORECOV") },
6786 /* IA-64 OpenVMS specific. */
6787 /* 12 */ { STRING_COMMA_LEN ("VMS_GLOBAL") },
6788 /* 13 */ { STRING_COMMA_LEN ("VMS_OVERLAID") },
6789 /* 14 */ { STRING_COMMA_LEN ("VMS_SHARED") },
6790 /* 15 */ { STRING_COMMA_LEN ("VMS_VECTOR") },
6791 /* 16 */ { STRING_COMMA_LEN ("VMS_ALLOC_64BIT") },
6792 /* 17 */ { STRING_COMMA_LEN ("VMS_PROTECTED") },
6793 /* Generic. */
6794 /* 18 */ { STRING_COMMA_LEN ("EXCLUDE") },
6795 /* SPARC specific. */
6796 /* 19 */ { STRING_COMMA_LEN ("ORDERED") },
6797 /* 20 */ { STRING_COMMA_LEN ("COMPRESSED") },
6798 /* ARM specific. */
6799 /* 21 */ { STRING_COMMA_LEN ("ENTRYSECT") },
6800 /* 22 */ { STRING_COMMA_LEN ("ARM_PURECODE") },
6801 /* 23 */ { STRING_COMMA_LEN ("COMDEF") },
6802 /* GNU specific. */
6803 /* 24 */ { STRING_COMMA_LEN ("GNU_MBIND") },
6804 /* VLE specific. */
6805 /* 25 */ { STRING_COMMA_LEN ("VLE") },
6806 /* GNU specific. */
6807 /* 26 */ { STRING_COMMA_LEN ("GNU_RETAIN") },
6808 };
6809
6810 if (do_section_details)
6811 {
6812 sprintf (buff, "[%*.*lx]: ",
6813 field_size, field_size, (unsigned long) sh_flags);
6814 p += field_size + 4;
6815 }
6816
6817 while (sh_flags)
6818 {
6819 uint64_t flag;
6820
6821 flag = sh_flags & - sh_flags;
6822 sh_flags &= ~ flag;
6823
6824 if (do_section_details)
6825 {
6826 switch (flag)
6827 {
6828 case SHF_WRITE: sindex = 0; break;
6829 case SHF_ALLOC: sindex = 1; break;
6830 case SHF_EXECINSTR: sindex = 2; break;
6831 case SHF_MERGE: sindex = 3; break;
6832 case SHF_STRINGS: sindex = 4; break;
6833 case SHF_INFO_LINK: sindex = 5; break;
6834 case SHF_LINK_ORDER: sindex = 6; break;
6835 case SHF_OS_NONCONFORMING: sindex = 7; break;
6836 case SHF_GROUP: sindex = 8; break;
6837 case SHF_TLS: sindex = 9; break;
6838 case SHF_EXCLUDE: sindex = 18; break;
6839 case SHF_COMPRESSED: sindex = 20; break;
6840
6841 default:
6842 sindex = -1;
6843 switch (filedata->file_header.e_machine)
6844 {
6845 case EM_IA_64:
6846 if (flag == SHF_IA_64_SHORT)
6847 sindex = 10;
6848 else if (flag == SHF_IA_64_NORECOV)
6849 sindex = 11;
6850 else if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
6851 switch (flag)
6852 {
6853 case SHF_IA_64_VMS_GLOBAL: sindex = 12; break;
6854 case SHF_IA_64_VMS_OVERLAID: sindex = 13; break;
6855 case SHF_IA_64_VMS_SHARED: sindex = 14; break;
6856 case SHF_IA_64_VMS_VECTOR: sindex = 15; break;
6857 case SHF_IA_64_VMS_ALLOC_64BIT: sindex = 16; break;
6858 case SHF_IA_64_VMS_PROTECTED: sindex = 17; break;
6859 default: break;
6860 }
6861 break;
6862
6863 case EM_386:
6864 case EM_IAMCU:
6865 case EM_X86_64:
6866 case EM_L1OM:
6867 case EM_K1OM:
6868 case EM_OLD_SPARCV9:
6869 case EM_SPARC32PLUS:
6870 case EM_SPARCV9:
6871 case EM_SPARC:
6872 if (flag == SHF_ORDERED)
6873 sindex = 19;
6874 break;
6875
6876 case EM_ARM:
6877 switch (flag)
6878 {
6879 case SHF_ENTRYSECT: sindex = 21; break;
6880 case SHF_ARM_PURECODE: sindex = 22; break;
6881 case SHF_COMDEF: sindex = 23; break;
6882 default: break;
6883 }
6884 break;
6885 case EM_PPC:
6886 if (flag == SHF_PPC_VLE)
6887 sindex = 25;
6888 break;
6889 default:
6890 break;
6891 }
6892
6893 switch (filedata->file_header.e_ident[EI_OSABI])
6894 {
6895 case ELFOSABI_GNU:
6896 case ELFOSABI_FREEBSD:
6897 if (flag == SHF_GNU_RETAIN)
6898 sindex = 26;
6899 /* Fall through */
6900 case ELFOSABI_NONE:
6901 if (flag == SHF_GNU_MBIND)
6902 /* We should not recognize SHF_GNU_MBIND for
6903 ELFOSABI_NONE, but binutils as of 2019-07-23 did
6904 not set the EI_OSABI header byte. */
6905 sindex = 24;
6906 break;
6907 default:
6908 break;
6909 }
6910 break;
6911 }
6912
6913 if (sindex != -1)
6914 {
6915 if (p != buff + field_size + 4)
6916 {
6917 if (size < (10 + 2))
6918 {
6919 warn (_("Internal error: not enough buffer room for section flag info"));
6920 return _("<unknown>");
6921 }
6922 size -= 2;
6923 *p++ = ',';
6924 *p++ = ' ';
6925 }
6926
6927 size -= flags [sindex].len;
6928 p = stpcpy (p, flags [sindex].str);
6929 }
6930 else if (flag & SHF_MASKOS)
6931 os_flags |= flag;
6932 else if (flag & SHF_MASKPROC)
6933 proc_flags |= flag;
6934 else
6935 unknown_flags |= flag;
6936 }
6937 else
6938 {
6939 switch (flag)
6940 {
6941 case SHF_WRITE: *p = 'W'; break;
6942 case SHF_ALLOC: *p = 'A'; break;
6943 case SHF_EXECINSTR: *p = 'X'; break;
6944 case SHF_MERGE: *p = 'M'; break;
6945 case SHF_STRINGS: *p = 'S'; break;
6946 case SHF_INFO_LINK: *p = 'I'; break;
6947 case SHF_LINK_ORDER: *p = 'L'; break;
6948 case SHF_OS_NONCONFORMING: *p = 'O'; break;
6949 case SHF_GROUP: *p = 'G'; break;
6950 case SHF_TLS: *p = 'T'; break;
6951 case SHF_EXCLUDE: *p = 'E'; break;
6952 case SHF_COMPRESSED: *p = 'C'; break;
6953
6954 default:
6955 if ((filedata->file_header.e_machine == EM_X86_64
6956 || filedata->file_header.e_machine == EM_L1OM
6957 || filedata->file_header.e_machine == EM_K1OM)
6958 && flag == SHF_X86_64_LARGE)
6959 *p = 'l';
6960 else if (filedata->file_header.e_machine == EM_ARM
6961 && flag == SHF_ARM_PURECODE)
6962 *p = 'y';
6963 else if (filedata->file_header.e_machine == EM_PPC
6964 && flag == SHF_PPC_VLE)
6965 *p = 'v';
6966 else if (flag & SHF_MASKOS)
6967 {
6968 switch (filedata->file_header.e_ident[EI_OSABI])
6969 {
6970 case ELFOSABI_GNU:
6971 case ELFOSABI_FREEBSD:
6972 if (flag == SHF_GNU_RETAIN)
6973 {
6974 *p = 'R';
6975 break;
6976 }
6977 /* Fall through */
6978 case ELFOSABI_NONE:
6979 if (flag == SHF_GNU_MBIND)
6980 {
6981 /* We should not recognize SHF_GNU_MBIND for
6982 ELFOSABI_NONE, but binutils as of 2019-07-23 did
6983 not set the EI_OSABI header byte. */
6984 *p = 'D';
6985 break;
6986 }
6987 /* Fall through */
6988 default:
6989 *p = 'o';
6990 sh_flags &= ~SHF_MASKOS;
6991 break;
6992 }
6993 }
6994 else if (flag & SHF_MASKPROC)
6995 {
6996 *p = 'p';
6997 sh_flags &= ~ SHF_MASKPROC;
6998 }
6999 else
7000 *p = 'x';
7001 break;
7002 }
7003 p++;
7004 }
7005 }
7006
7007 if (do_section_details)
7008 {
7009 if (os_flags)
7010 {
7011 size -= 5 + field_size;
7012 if (p != buff + field_size + 4)
7013 {
7014 if (size < (2 + 1))
7015 {
7016 warn (_("Internal error: not enough buffer room for section flag info"));
7017 return _("<unknown>");
7018 }
7019 size -= 2;
7020 *p++ = ',';
7021 *p++ = ' ';
7022 }
7023 sprintf (p, "OS (%*.*lx)", field_size, field_size,
7024 (unsigned long) os_flags);
7025 p += 5 + field_size;
7026 }
7027 if (proc_flags)
7028 {
7029 size -= 7 + field_size;
7030 if (p != buff + field_size + 4)
7031 {
7032 if (size < (2 + 1))
7033 {
7034 warn (_("Internal error: not enough buffer room for section flag info"));
7035 return _("<unknown>");
7036 }
7037 size -= 2;
7038 *p++ = ',';
7039 *p++ = ' ';
7040 }
7041 sprintf (p, "PROC (%*.*lx)", field_size, field_size,
7042 (unsigned long) proc_flags);
7043 p += 7 + field_size;
7044 }
7045 if (unknown_flags)
7046 {
7047 size -= 10 + field_size;
7048 if (p != buff + field_size + 4)
7049 {
7050 if (size < (2 + 1))
7051 {
7052 warn (_("Internal error: not enough buffer room for section flag info"));
7053 return _("<unknown>");
7054 }
7055 size -= 2;
7056 *p++ = ',';
7057 *p++ = ' ';
7058 }
7059 sprintf (p, _("UNKNOWN (%*.*lx)"), field_size, field_size,
7060 (unsigned long) unknown_flags);
7061 p += 10 + field_size;
7062 }
7063 }
7064
7065 *p = '\0';
7066 return buff;
7067 }
7068
7069 static unsigned int ATTRIBUTE_WARN_UNUSED_RESULT
7070 get_compression_header (Elf_Internal_Chdr *chdr, unsigned char *buf,
7071 uint64_t size)
7072 {
7073 if (is_32bit_elf)
7074 {
7075 Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) buf;
7076
7077 if (size < sizeof (* echdr))
7078 {
7079 error (_("Compressed section is too small even for a compression header\n"));
7080 return 0;
7081 }
7082
7083 chdr->ch_type = BYTE_GET (echdr->ch_type);
7084 chdr->ch_size = BYTE_GET (echdr->ch_size);
7085 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
7086 return sizeof (*echdr);
7087 }
7088 else
7089 {
7090 Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) buf;
7091
7092 if (size < sizeof (* echdr))
7093 {
7094 error (_("Compressed section is too small even for a compression header\n"));
7095 return 0;
7096 }
7097
7098 chdr->ch_type = BYTE_GET (echdr->ch_type);
7099 chdr->ch_size = BYTE_GET (echdr->ch_size);
7100 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
7101 return sizeof (*echdr);
7102 }
7103 }
7104
7105 static bool
7106 process_section_headers (Filedata * filedata)
7107 {
7108 Elf_Internal_Shdr * section;
7109 unsigned int i;
7110
7111 if (filedata->file_header.e_shnum == 0)
7112 {
7113 /* PR binutils/12467. */
7114 if (filedata->file_header.e_shoff != 0)
7115 {
7116 warn (_("possibly corrupt ELF file header - it has a non-zero"
7117 " section header offset, but no section headers\n"));
7118 return false;
7119 }
7120 else if (do_sections)
7121 printf (_("\nThere are no sections in this file.\n"));
7122
7123 return true;
7124 }
7125
7126 if (do_sections && !do_header)
7127 {
7128 if (filedata->is_separate && process_links)
7129 printf (_("In linked file '%s': "), filedata->file_name);
7130 if (! filedata->is_separate || process_links)
7131 printf (ngettext ("There is %d section header, "
7132 "starting at offset 0x%lx:\n",
7133 "There are %d section headers, "
7134 "starting at offset 0x%lx:\n",
7135 filedata->file_header.e_shnum),
7136 filedata->file_header.e_shnum,
7137 (unsigned long) filedata->file_header.e_shoff);
7138 }
7139
7140 if (!get_section_headers (filedata, false))
7141 return false;
7142
7143 /* Read in the string table, so that we have names to display. */
7144 if (filedata->file_header.e_shstrndx != SHN_UNDEF
7145 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
7146 {
7147 section = filedata->section_headers + filedata->file_header.e_shstrndx;
7148
7149 if (section->sh_size != 0)
7150 {
7151 filedata->string_table = (char *) get_data (NULL, filedata, section->sh_offset,
7152 1, section->sh_size,
7153 _("string table"));
7154
7155 filedata->string_table_length = filedata->string_table != NULL ? section->sh_size : 0;
7156 }
7157 }
7158
7159 /* Scan the sections for the dynamic symbol table
7160 and dynamic string table and debug sections. */
7161 eh_addr_size = is_32bit_elf ? 4 : 8;
7162 switch (filedata->file_header.e_machine)
7163 {
7164 case EM_MIPS:
7165 case EM_MIPS_RS3_LE:
7166 /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
7167 FDE addresses. However, the ABI also has a semi-official ILP32
7168 variant for which the normal FDE address size rules apply.
7169
7170 GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
7171 section, where XX is the size of longs in bits. Unfortunately,
7172 earlier compilers provided no way of distinguishing ILP32 objects
7173 from LP64 objects, so if there's any doubt, we should assume that
7174 the official LP64 form is being used. */
7175 if ((filedata->file_header.e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64
7176 && find_section (filedata, ".gcc_compiled_long32") == NULL)
7177 eh_addr_size = 8;
7178 break;
7179
7180 case EM_H8_300:
7181 case EM_H8_300H:
7182 switch (filedata->file_header.e_flags & EF_H8_MACH)
7183 {
7184 case E_H8_MACH_H8300:
7185 case E_H8_MACH_H8300HN:
7186 case E_H8_MACH_H8300SN:
7187 case E_H8_MACH_H8300SXN:
7188 eh_addr_size = 2;
7189 break;
7190 case E_H8_MACH_H8300H:
7191 case E_H8_MACH_H8300S:
7192 case E_H8_MACH_H8300SX:
7193 eh_addr_size = 4;
7194 break;
7195 }
7196 break;
7197
7198 case EM_M32C_OLD:
7199 case EM_M32C:
7200 switch (filedata->file_header.e_flags & EF_M32C_CPU_MASK)
7201 {
7202 case EF_M32C_CPU_M16C:
7203 eh_addr_size = 2;
7204 break;
7205 }
7206 break;
7207 }
7208
7209 #define CHECK_ENTSIZE_VALUES(section, i, size32, size64) \
7210 do \
7211 { \
7212 uint64_t expected_entsize = is_32bit_elf ? size32 : size64; \
7213 if (section->sh_entsize != expected_entsize) \
7214 { \
7215 error (_("Section %d has invalid sh_entsize of %" PRIx64 "\n"), \
7216 i, section->sh_entsize); \
7217 error (_("(Using the expected size of %" PRIx64 " for the rest of this dump)\n"), \
7218 expected_entsize); \
7219 section->sh_entsize = expected_entsize; \
7220 } \
7221 } \
7222 while (0)
7223
7224 #define CHECK_ENTSIZE(section, i, type) \
7225 CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type), \
7226 sizeof (Elf64_External_##type))
7227
7228 for (i = 0, section = filedata->section_headers;
7229 i < filedata->file_header.e_shnum;
7230 i++, section++)
7231 {
7232 const char *name = section_name_print (filedata, section);
7233
7234 /* Run some sanity checks on the headers and
7235 possibly fill in some file data as well. */
7236 switch (section->sh_type)
7237 {
7238 case SHT_DYNSYM:
7239 if (filedata->dynamic_symbols != NULL)
7240 {
7241 error (_("File contains multiple dynamic symbol tables\n"));
7242 continue;
7243 }
7244
7245 CHECK_ENTSIZE (section, i, Sym);
7246 filedata->dynamic_symbols
7247 = get_elf_symbols (filedata, section, &filedata->num_dynamic_syms);
7248 filedata->dynamic_symtab_section = section;
7249 break;
7250
7251 case SHT_STRTAB:
7252 if (streq (name, ".dynstr"))
7253 {
7254 if (filedata->dynamic_strings != NULL)
7255 {
7256 error (_("File contains multiple dynamic string tables\n"));
7257 continue;
7258 }
7259
7260 filedata->dynamic_strings
7261 = (char *) get_data (NULL, filedata, section->sh_offset,
7262 1, section->sh_size, _("dynamic strings"));
7263 filedata->dynamic_strings_length
7264 = filedata->dynamic_strings == NULL ? 0 : section->sh_size;
7265 filedata->dynamic_strtab_section = section;
7266 }
7267 break;
7268
7269 case SHT_SYMTAB_SHNDX:
7270 {
7271 elf_section_list * entry = xmalloc (sizeof * entry);
7272
7273 entry->hdr = section;
7274 entry->next = filedata->symtab_shndx_list;
7275 filedata->symtab_shndx_list = entry;
7276 }
7277 break;
7278
7279 case SHT_SYMTAB:
7280 CHECK_ENTSIZE (section, i, Sym);
7281 break;
7282
7283 case SHT_GROUP:
7284 CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
7285 break;
7286
7287 case SHT_REL:
7288 CHECK_ENTSIZE (section, i, Rel);
7289 if (do_checks && section->sh_size == 0)
7290 warn (_("Section '%s': zero-sized relocation section\n"), name);
7291 break;
7292
7293 case SHT_RELA:
7294 CHECK_ENTSIZE (section, i, Rela);
7295 if (do_checks && section->sh_size == 0)
7296 warn (_("Section '%s': zero-sized relocation section\n"), name);
7297 break;
7298
7299 case SHT_RELR:
7300 CHECK_ENTSIZE (section, i, Relr);
7301 break;
7302
7303 case SHT_NOTE:
7304 case SHT_PROGBITS:
7305 /* Having a zero sized section is not illegal according to the
7306 ELF standard, but it might be an indication that something
7307 is wrong. So issue a warning if we are running in lint mode. */
7308 if (do_checks && section->sh_size == 0)
7309 warn (_("Section '%s': has a size of zero - is this intended ?\n"), name);
7310 break;
7311
7312 default:
7313 break;
7314 }
7315
7316 if ((do_debugging || do_debug_info || do_debug_abbrevs
7317 || do_debug_lines || do_debug_pubnames || do_debug_pubtypes
7318 || do_debug_aranges || do_debug_frames || do_debug_macinfo
7319 || do_debug_str || do_debug_str_offsets || do_debug_loc
7320 || do_debug_ranges
7321 || do_debug_addr || do_debug_cu_index || do_debug_links)
7322 && (startswith (name, ".debug_")
7323 || startswith (name, ".zdebug_")))
7324 {
7325 if (name[1] == 'z')
7326 name += sizeof (".zdebug_") - 1;
7327 else
7328 name += sizeof (".debug_") - 1;
7329
7330 if (do_debugging
7331 || (do_debug_info && startswith (name, "info"))
7332 || (do_debug_info && startswith (name, "types"))
7333 || (do_debug_abbrevs && startswith (name, "abbrev"))
7334 || (do_debug_lines && strcmp (name, "line") == 0)
7335 || (do_debug_lines && startswith (name, "line."))
7336 || (do_debug_pubnames && startswith (name, "pubnames"))
7337 || (do_debug_pubtypes && startswith (name, "pubtypes"))
7338 || (do_debug_pubnames && startswith (name, "gnu_pubnames"))
7339 || (do_debug_pubtypes && startswith (name, "gnu_pubtypes"))
7340 || (do_debug_aranges && startswith (name, "aranges"))
7341 || (do_debug_ranges && startswith (name, "ranges"))
7342 || (do_debug_ranges && startswith (name, "rnglists"))
7343 || (do_debug_frames && startswith (name, "frame"))
7344 || (do_debug_macinfo && startswith (name, "macinfo"))
7345 || (do_debug_macinfo && startswith (name, "macro"))
7346 || (do_debug_str && startswith (name, "str"))
7347 || (do_debug_links && startswith (name, "sup"))
7348 || (do_debug_str_offsets && startswith (name, "str_offsets"))
7349 || (do_debug_loc && startswith (name, "loc"))
7350 || (do_debug_loc && startswith (name, "loclists"))
7351 || (do_debug_addr && startswith (name, "addr"))
7352 || (do_debug_cu_index && startswith (name, "cu_index"))
7353 || (do_debug_cu_index && startswith (name, "tu_index"))
7354 )
7355 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7356 }
7357 /* Linkonce section to be combined with .debug_info at link time. */
7358 else if ((do_debugging || do_debug_info)
7359 && startswith (name, ".gnu.linkonce.wi."))
7360 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7361 else if (do_debug_frames && streq (name, ".eh_frame"))
7362 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7363 else if (do_gdb_index && (streq (name, ".gdb_index")
7364 || streq (name, ".debug_names")))
7365 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7366 /* Trace sections for Itanium VMS. */
7367 else if ((do_debugging || do_trace_info || do_trace_abbrevs
7368 || do_trace_aranges)
7369 && startswith (name, ".trace_"))
7370 {
7371 name += sizeof (".trace_") - 1;
7372
7373 if (do_debugging
7374 || (do_trace_info && streq (name, "info"))
7375 || (do_trace_abbrevs && streq (name, "abbrev"))
7376 || (do_trace_aranges && streq (name, "aranges"))
7377 )
7378 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7379 }
7380 else if ((do_debugging || do_debug_links)
7381 && (startswith (name, ".gnu_debuglink")
7382 || startswith (name, ".gnu_debugaltlink")))
7383 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7384 }
7385
7386 if (! do_sections)
7387 return true;
7388
7389 if (filedata->is_separate && ! process_links)
7390 return true;
7391
7392 if (filedata->is_separate)
7393 printf (_("\nSection Headers in linked file '%s':\n"), filedata->file_name);
7394 else if (filedata->file_header.e_shnum > 1)
7395 printf (_("\nSection Headers:\n"));
7396 else
7397 printf (_("\nSection Header:\n"));
7398
7399 if (is_32bit_elf)
7400 {
7401 if (do_section_details)
7402 {
7403 printf (_(" [Nr] Name\n"));
7404 printf (_(" Type Addr Off Size ES Lk Inf Al\n"));
7405 }
7406 else
7407 printf
7408 (_(" [Nr] Name Type Addr Off Size ES Flg Lk Inf Al\n"));
7409 }
7410 else if (do_wide)
7411 {
7412 if (do_section_details)
7413 {
7414 printf (_(" [Nr] Name\n"));
7415 printf (_(" Type Address Off Size ES Lk Inf Al\n"));
7416 }
7417 else
7418 printf
7419 (_(" [Nr] Name Type Address Off Size ES Flg Lk Inf Al\n"));
7420 }
7421 else
7422 {
7423 if (do_section_details)
7424 {
7425 printf (_(" [Nr] Name\n"));
7426 printf (_(" Type Address Offset Link\n"));
7427 printf (_(" Size EntSize Info Align\n"));
7428 }
7429 else
7430 {
7431 printf (_(" [Nr] Name Type Address Offset\n"));
7432 printf (_(" Size EntSize Flags Link Info Align\n"));
7433 }
7434 }
7435
7436 if (do_section_details)
7437 printf (_(" Flags\n"));
7438
7439 for (i = 0, section = filedata->section_headers;
7440 i < filedata->file_header.e_shnum;
7441 i++, section++)
7442 {
7443 /* Run some sanity checks on the section header. */
7444
7445 /* Check the sh_link field. */
7446 switch (section->sh_type)
7447 {
7448 case SHT_REL:
7449 case SHT_RELA:
7450 if (section->sh_link == 0
7451 && (filedata->file_header.e_type == ET_EXEC
7452 || filedata->file_header.e_type == ET_DYN))
7453 /* A dynamic relocation section where all entries use a
7454 zero symbol index need not specify a symtab section. */
7455 break;
7456 /* Fall through. */
7457 case SHT_SYMTAB_SHNDX:
7458 case SHT_GROUP:
7459 case SHT_HASH:
7460 case SHT_GNU_HASH:
7461 case SHT_GNU_versym:
7462 if (section->sh_link == 0
7463 || section->sh_link >= filedata->file_header.e_shnum
7464 || (filedata->section_headers[section->sh_link].sh_type != SHT_SYMTAB
7465 && filedata->section_headers[section->sh_link].sh_type != SHT_DYNSYM))
7466 warn (_("[%2u]: Link field (%u) should index a symtab section.\n"),
7467 i, section->sh_link);
7468 break;
7469
7470 case SHT_DYNAMIC:
7471 case SHT_SYMTAB:
7472 case SHT_DYNSYM:
7473 case SHT_GNU_verneed:
7474 case SHT_GNU_verdef:
7475 case SHT_GNU_LIBLIST:
7476 if (section->sh_link == 0
7477 || section->sh_link >= filedata->file_header.e_shnum
7478 || filedata->section_headers[section->sh_link].sh_type != SHT_STRTAB)
7479 warn (_("[%2u]: Link field (%u) should index a string section.\n"),
7480 i, section->sh_link);
7481 break;
7482
7483 case SHT_INIT_ARRAY:
7484 case SHT_FINI_ARRAY:
7485 case SHT_PREINIT_ARRAY:
7486 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
7487 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
7488 i, section->sh_link);
7489 break;
7490
7491 default:
7492 /* FIXME: Add support for target specific section types. */
7493 #if 0 /* Currently we do not check other section types as there are too
7494 many special cases. Stab sections for example have a type
7495 of SHT_PROGBITS but an sh_link field that links to the .stabstr
7496 section. */
7497 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
7498 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
7499 i, section->sh_link);
7500 #endif
7501 break;
7502 }
7503
7504 /* Check the sh_info field. */
7505 switch (section->sh_type)
7506 {
7507 case SHT_REL:
7508 case SHT_RELA:
7509 if (section->sh_info == 0
7510 && (filedata->file_header.e_type == ET_EXEC
7511 || filedata->file_header.e_type == ET_DYN))
7512 /* Dynamic relocations apply to segments, so they do not
7513 need to specify the section they relocate. */
7514 break;
7515 if (section->sh_info == 0
7516 || section->sh_info >= filedata->file_header.e_shnum
7517 || (filedata->section_headers[section->sh_info].sh_type != SHT_PROGBITS
7518 && filedata->section_headers[section->sh_info].sh_type != SHT_NOBITS
7519 && filedata->section_headers[section->sh_info].sh_type != SHT_NOTE
7520 && filedata->section_headers[section->sh_info].sh_type != SHT_INIT_ARRAY
7521 && filedata->section_headers[section->sh_info].sh_type != SHT_FINI_ARRAY
7522 && filedata->section_headers[section->sh_info].sh_type != SHT_PREINIT_ARRAY
7523 /* FIXME: Are other section types valid ? */
7524 && filedata->section_headers[section->sh_info].sh_type < SHT_LOOS))
7525 warn (_("[%2u]: Info field (%u) should index a relocatable section.\n"),
7526 i, section->sh_info);
7527 break;
7528
7529 case SHT_DYNAMIC:
7530 case SHT_HASH:
7531 case SHT_SYMTAB_SHNDX:
7532 case SHT_INIT_ARRAY:
7533 case SHT_FINI_ARRAY:
7534 case SHT_PREINIT_ARRAY:
7535 if (section->sh_info != 0)
7536 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
7537 i, section->sh_info);
7538 break;
7539
7540 case SHT_GROUP:
7541 case SHT_SYMTAB:
7542 case SHT_DYNSYM:
7543 /* A symbol index - we assume that it is valid. */
7544 break;
7545
7546 default:
7547 /* FIXME: Add support for target specific section types. */
7548 if (section->sh_type == SHT_NOBITS)
7549 /* NOBITS section headers with non-zero sh_info fields can be
7550 created when a binary is stripped of everything but its debug
7551 information. The stripped sections have their headers
7552 preserved but their types set to SHT_NOBITS. So do not check
7553 this type of section. */
7554 ;
7555 else if (section->sh_flags & SHF_INFO_LINK)
7556 {
7557 if (section->sh_info < 1 || section->sh_info >= filedata->file_header.e_shnum)
7558 warn (_("[%2u]: Expected link to another section in info field"), i);
7559 }
7560 else if (section->sh_type < SHT_LOOS
7561 && (section->sh_flags & SHF_GNU_MBIND) == 0
7562 && section->sh_info != 0)
7563 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
7564 i, section->sh_info);
7565 break;
7566 }
7567
7568 /* Check the sh_size field. */
7569 if (section->sh_size > filedata->file_size
7570 && section->sh_type != SHT_NOBITS
7571 && section->sh_type != SHT_NULL
7572 && section->sh_type < SHT_LOOS)
7573 warn (_("Size of section %u is larger than the entire file!\n"), i);
7574
7575 printf (" [%2u] ", i);
7576 if (do_section_details)
7577 printf ("%s\n ", printable_section_name (filedata, section));
7578 else
7579 print_symbol (-17, section_name_print (filedata, section));
7580
7581 printf (do_wide ? " %-15s " : " %-15.15s ",
7582 get_section_type_name (filedata, section->sh_type));
7583
7584 if (is_32bit_elf)
7585 {
7586 const char * link_too_big = NULL;
7587
7588 print_vma (section->sh_addr, LONG_HEX);
7589
7590 printf ( " %6.6lx %6.6lx %2.2lx",
7591 (unsigned long) section->sh_offset,
7592 (unsigned long) section->sh_size,
7593 (unsigned long) section->sh_entsize);
7594
7595 if (do_section_details)
7596 fputs (" ", stdout);
7597 else
7598 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7599
7600 if (section->sh_link >= filedata->file_header.e_shnum)
7601 {
7602 link_too_big = "";
7603 /* The sh_link value is out of range. Normally this indicates
7604 an error but it can have special values in Solaris binaries. */
7605 switch (filedata->file_header.e_machine)
7606 {
7607 case EM_386:
7608 case EM_IAMCU:
7609 case EM_X86_64:
7610 case EM_L1OM:
7611 case EM_K1OM:
7612 case EM_OLD_SPARCV9:
7613 case EM_SPARC32PLUS:
7614 case EM_SPARCV9:
7615 case EM_SPARC:
7616 if (section->sh_link == (SHN_BEFORE & 0xffff))
7617 link_too_big = "BEFORE";
7618 else if (section->sh_link == (SHN_AFTER & 0xffff))
7619 link_too_big = "AFTER";
7620 break;
7621 default:
7622 break;
7623 }
7624 }
7625
7626 if (do_section_details)
7627 {
7628 if (link_too_big != NULL && * link_too_big)
7629 printf ("<%s> ", link_too_big);
7630 else
7631 printf ("%2u ", section->sh_link);
7632 printf ("%3u %2lu\n", section->sh_info,
7633 (unsigned long) section->sh_addralign);
7634 }
7635 else
7636 printf ("%2u %3u %2lu\n",
7637 section->sh_link,
7638 section->sh_info,
7639 (unsigned long) section->sh_addralign);
7640
7641 if (link_too_big && ! * link_too_big)
7642 warn (_("section %u: sh_link value of %u is larger than the number of sections\n"),
7643 i, section->sh_link);
7644 }
7645 else if (do_wide)
7646 {
7647 print_vma (section->sh_addr, LONG_HEX);
7648
7649 if ((long) section->sh_offset == section->sh_offset)
7650 printf (" %6.6lx", (unsigned long) section->sh_offset);
7651 else
7652 {
7653 putchar (' ');
7654 print_vma (section->sh_offset, LONG_HEX);
7655 }
7656
7657 if ((unsigned long) section->sh_size == section->sh_size)
7658 printf (" %6.6lx", (unsigned long) section->sh_size);
7659 else
7660 {
7661 putchar (' ');
7662 print_vma (section->sh_size, LONG_HEX);
7663 }
7664
7665 if ((unsigned long) section->sh_entsize == section->sh_entsize)
7666 printf (" %2.2lx", (unsigned long) section->sh_entsize);
7667 else
7668 {
7669 putchar (' ');
7670 print_vma (section->sh_entsize, LONG_HEX);
7671 }
7672
7673 if (do_section_details)
7674 fputs (" ", stdout);
7675 else
7676 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7677
7678 printf ("%2u %3u ", section->sh_link, section->sh_info);
7679
7680 if ((unsigned long) section->sh_addralign == section->sh_addralign)
7681 printf ("%2lu\n", (unsigned long) section->sh_addralign);
7682 else
7683 {
7684 print_vma (section->sh_addralign, DEC);
7685 putchar ('\n');
7686 }
7687 }
7688 else if (do_section_details)
7689 {
7690 putchar (' ');
7691 print_vma (section->sh_addr, LONG_HEX);
7692 if ((long) section->sh_offset == section->sh_offset)
7693 printf (" %16.16lx", (unsigned long) section->sh_offset);
7694 else
7695 {
7696 printf (" ");
7697 print_vma (section->sh_offset, LONG_HEX);
7698 }
7699 printf (" %u\n ", section->sh_link);
7700 print_vma (section->sh_size, LONG_HEX);
7701 putchar (' ');
7702 print_vma (section->sh_entsize, LONG_HEX);
7703
7704 printf (" %-16u %lu\n",
7705 section->sh_info,
7706 (unsigned long) section->sh_addralign);
7707 }
7708 else
7709 {
7710 putchar (' ');
7711 print_vma (section->sh_addr, LONG_HEX);
7712 if ((long) section->sh_offset == section->sh_offset)
7713 printf (" %8.8lx", (unsigned long) section->sh_offset);
7714 else
7715 {
7716 printf (" ");
7717 print_vma (section->sh_offset, LONG_HEX);
7718 }
7719 printf ("\n ");
7720 print_vma (section->sh_size, LONG_HEX);
7721 printf (" ");
7722 print_vma (section->sh_entsize, LONG_HEX);
7723
7724 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7725
7726 printf (" %2u %3u %lu\n",
7727 section->sh_link,
7728 section->sh_info,
7729 (unsigned long) section->sh_addralign);
7730 }
7731
7732 if (do_section_details)
7733 {
7734 printf (" %s\n", get_elf_section_flags (filedata, section->sh_flags));
7735 if ((section->sh_flags & SHF_COMPRESSED) != 0)
7736 {
7737 /* Minimum section size is 12 bytes for 32-bit compression
7738 header + 12 bytes for compressed data header. */
7739 unsigned char buf[24];
7740
7741 assert (sizeof (buf) >= sizeof (Elf64_External_Chdr));
7742 if (get_data (&buf, filedata, section->sh_offset, 1,
7743 sizeof (buf), _("compression header")))
7744 {
7745 Elf_Internal_Chdr chdr;
7746
7747 if (get_compression_header (&chdr, buf, sizeof (buf)) == 0)
7748 printf (_(" [<corrupt>]\n"));
7749 else
7750 {
7751 if (chdr.ch_type == ELFCOMPRESS_ZLIB)
7752 printf (" ZLIB, ");
7753 else if (chdr.ch_type == ELFCOMPRESS_ZSTD)
7754 printf (" ZSTD, ");
7755 else
7756 printf (_(" [<unknown>: 0x%x], "),
7757 chdr.ch_type);
7758 print_vma (chdr.ch_size, LONG_HEX);
7759 printf (", %lu\n", (unsigned long) chdr.ch_addralign);
7760 }
7761 }
7762 }
7763 }
7764 }
7765
7766 if (!do_section_details)
7767 {
7768 /* The ordering of the letters shown here matches the ordering of the
7769 corresponding SHF_xxx values, and hence the order in which these
7770 letters will be displayed to the user. */
7771 printf (_("Key to Flags:\n\
7772 W (write), A (alloc), X (execute), M (merge), S (strings), I (info),\n\
7773 L (link order), O (extra OS processing required), G (group), T (TLS),\n\
7774 C (compressed), x (unknown), o (OS specific), E (exclude),\n "));
7775 switch (filedata->file_header.e_ident[EI_OSABI])
7776 {
7777 case ELFOSABI_GNU:
7778 case ELFOSABI_FREEBSD:
7779 printf (_("R (retain), "));
7780 /* Fall through */
7781 case ELFOSABI_NONE:
7782 printf (_("D (mbind), "));
7783 break;
7784 default:
7785 break;
7786 }
7787 if (filedata->file_header.e_machine == EM_X86_64
7788 || filedata->file_header.e_machine == EM_L1OM
7789 || filedata->file_header.e_machine == EM_K1OM)
7790 printf (_("l (large), "));
7791 else if (filedata->file_header.e_machine == EM_ARM)
7792 printf (_("y (purecode), "));
7793 else if (filedata->file_header.e_machine == EM_PPC)
7794 printf (_("v (VLE), "));
7795 printf ("p (processor specific)\n");
7796 }
7797
7798 return true;
7799 }
7800
7801 static bool
7802 get_symtab (Filedata *filedata, Elf_Internal_Shdr *symsec,
7803 Elf_Internal_Sym **symtab, unsigned long *nsyms,
7804 char **strtab, unsigned long *strtablen)
7805 {
7806 *strtab = NULL;
7807 *strtablen = 0;
7808 *symtab = get_elf_symbols (filedata, symsec, nsyms);
7809
7810 if (*symtab == NULL)
7811 return false;
7812
7813 if (symsec->sh_link != 0)
7814 {
7815 Elf_Internal_Shdr *strsec;
7816
7817 if (symsec->sh_link >= filedata->file_header.e_shnum)
7818 {
7819 error (_("Bad sh_link in symbol table section\n"));
7820 free (*symtab);
7821 *symtab = NULL;
7822 *nsyms = 0;
7823 return false;
7824 }
7825
7826 strsec = filedata->section_headers + symsec->sh_link;
7827
7828 *strtab = (char *) get_data (NULL, filedata, strsec->sh_offset,
7829 1, strsec->sh_size, _("string table"));
7830 if (*strtab == NULL)
7831 {
7832 free (*symtab);
7833 *symtab = NULL;
7834 *nsyms = 0;
7835 return false;
7836 }
7837 *strtablen = strsec->sh_size;
7838 }
7839 return true;
7840 }
7841
7842 static const char *
7843 get_group_flags (unsigned int flags)
7844 {
7845 static char buff[128];
7846
7847 if (flags == 0)
7848 return "";
7849 else if (flags == GRP_COMDAT)
7850 return "COMDAT ";
7851
7852 snprintf (buff, sizeof buff, "[0x%x: %s%s%s]",
7853 flags,
7854 flags & GRP_MASKOS ? _("<OS specific>") : "",
7855 flags & GRP_MASKPROC ? _("<PROC specific>") : "",
7856 (flags & ~(GRP_COMDAT | GRP_MASKOS | GRP_MASKPROC)
7857 ? _("<unknown>") : ""));
7858
7859 return buff;
7860 }
7861
7862 static bool
7863 process_section_groups (Filedata * filedata)
7864 {
7865 Elf_Internal_Shdr * section;
7866 unsigned int i;
7867 struct group * group;
7868 Elf_Internal_Shdr * symtab_sec;
7869 Elf_Internal_Shdr * strtab_sec;
7870 Elf_Internal_Sym * symtab;
7871 unsigned long num_syms;
7872 char * strtab;
7873 size_t strtab_size;
7874
7875 /* Don't process section groups unless needed. */
7876 if (!do_unwind && !do_section_groups)
7877 return true;
7878
7879 if (filedata->file_header.e_shnum == 0)
7880 {
7881 if (do_section_groups)
7882 {
7883 if (filedata->is_separate)
7884 printf (_("\nThere are no sections group in linked file '%s'.\n"),
7885 filedata->file_name);
7886 else
7887 printf (_("\nThere are no section groups in this file.\n"));
7888 }
7889 return true;
7890 }
7891
7892 if (filedata->section_headers == NULL)
7893 {
7894 error (_("Section headers are not available!\n"));
7895 /* PR 13622: This can happen with a corrupt ELF header. */
7896 return false;
7897 }
7898
7899 filedata->section_headers_groups
7900 = (struct group **) calloc (filedata->file_header.e_shnum,
7901 sizeof (struct group *));
7902
7903 if (filedata->section_headers_groups == NULL)
7904 {
7905 error (_("Out of memory reading %u section group headers\n"),
7906 filedata->file_header.e_shnum);
7907 return false;
7908 }
7909
7910 /* Scan the sections for the group section. */
7911 filedata->group_count = 0;
7912 for (i = 0, section = filedata->section_headers;
7913 i < filedata->file_header.e_shnum;
7914 i++, section++)
7915 if (section->sh_type == SHT_GROUP)
7916 filedata->group_count++;
7917
7918 if (filedata->group_count == 0)
7919 {
7920 if (do_section_groups)
7921 {
7922 if (filedata->is_separate)
7923 printf (_("\nThere are no section groups in linked file '%s'.\n"),
7924 filedata->file_name);
7925 else
7926 printf (_("\nThere are no section groups in this file.\n"));
7927 }
7928
7929 return true;
7930 }
7931
7932 filedata->section_groups = (struct group *) calloc (filedata->group_count,
7933 sizeof (struct group));
7934
7935 if (filedata->section_groups == NULL)
7936 {
7937 error (_("Out of memory reading %lu groups\n"),
7938 (unsigned long) filedata->group_count);
7939 return false;
7940 }
7941
7942 symtab_sec = NULL;
7943 strtab_sec = NULL;
7944 symtab = NULL;
7945 num_syms = 0;
7946 strtab = NULL;
7947 strtab_size = 0;
7948
7949 if (filedata->is_separate)
7950 printf (_("Section groups in linked file '%s'\n"), filedata->file_name);
7951
7952 for (i = 0, section = filedata->section_headers, group = filedata->section_groups;
7953 i < filedata->file_header.e_shnum;
7954 i++, section++)
7955 {
7956 if (section->sh_type == SHT_GROUP)
7957 {
7958 const char * name = printable_section_name (filedata, section);
7959 const char * group_name;
7960 unsigned char * start;
7961 unsigned char * indices;
7962 unsigned int entry, j, size;
7963 Elf_Internal_Shdr * sec;
7964 Elf_Internal_Sym * sym;
7965
7966 /* Get the symbol table. */
7967 if (section->sh_link >= filedata->file_header.e_shnum
7968 || ((sec = filedata->section_headers + section->sh_link)->sh_type
7969 != SHT_SYMTAB))
7970 {
7971 error (_("Bad sh_link in group section `%s'\n"), name);
7972 continue;
7973 }
7974
7975 if (symtab_sec != sec)
7976 {
7977 symtab_sec = sec;
7978 free (symtab);
7979 symtab = get_elf_symbols (filedata, symtab_sec, & num_syms);
7980 }
7981
7982 if (symtab == NULL)
7983 {
7984 error (_("Corrupt header in group section `%s'\n"), name);
7985 continue;
7986 }
7987
7988 if (section->sh_info >= num_syms)
7989 {
7990 error (_("Bad sh_info in group section `%s'\n"), name);
7991 continue;
7992 }
7993
7994 sym = symtab + section->sh_info;
7995
7996 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
7997 {
7998 if (sym->st_shndx == 0
7999 || sym->st_shndx >= filedata->file_header.e_shnum)
8000 {
8001 error (_("Bad sh_info in group section `%s'\n"), name);
8002 continue;
8003 }
8004
8005 group_name = section_name_print (filedata,
8006 filedata->section_headers
8007 + sym->st_shndx);
8008 strtab_sec = NULL;
8009 free (strtab);
8010 strtab = NULL;
8011 strtab_size = 0;
8012 }
8013 else
8014 {
8015 /* Get the string table. */
8016 if (symtab_sec->sh_link >= filedata->file_header.e_shnum)
8017 {
8018 strtab_sec = NULL;
8019 free (strtab);
8020 strtab = NULL;
8021 strtab_size = 0;
8022 }
8023 else if (strtab_sec
8024 != (sec = filedata->section_headers + symtab_sec->sh_link))
8025 {
8026 strtab_sec = sec;
8027 free (strtab);
8028
8029 strtab = (char *) get_data (NULL, filedata, strtab_sec->sh_offset,
8030 1, strtab_sec->sh_size,
8031 _("string table"));
8032 strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
8033 }
8034 group_name = sym->st_name < strtab_size
8035 ? strtab + sym->st_name : _("<corrupt>");
8036 }
8037
8038 /* PR 17531: file: loop. */
8039 if (section->sh_entsize > section->sh_size)
8040 {
8041 error (_("Section %s has sh_entsize (0x%lx) which is larger than its size (0x%lx)\n"),
8042 printable_section_name (filedata, section),
8043 (unsigned long) section->sh_entsize,
8044 (unsigned long) section->sh_size);
8045 continue;
8046 }
8047
8048 start = (unsigned char *) get_data (NULL, filedata, section->sh_offset,
8049 1, section->sh_size,
8050 _("section data"));
8051 if (start == NULL)
8052 continue;
8053
8054 indices = start;
8055 size = (section->sh_size / section->sh_entsize) - 1;
8056 entry = byte_get (indices, 4);
8057 indices += 4;
8058
8059 if (do_section_groups)
8060 {
8061 printf (_("\n%sgroup section [%5u] `%s' [%s] contains %u sections:\n"),
8062 get_group_flags (entry), i, name, group_name, size);
8063
8064 printf (_(" [Index] Name\n"));
8065 }
8066
8067 group->group_index = i;
8068
8069 for (j = 0; j < size; j++)
8070 {
8071 struct group_list * g;
8072
8073 entry = byte_get (indices, 4);
8074 indices += 4;
8075
8076 if (entry >= filedata->file_header.e_shnum)
8077 {
8078 static unsigned num_group_errors = 0;
8079
8080 if (num_group_errors ++ < 10)
8081 {
8082 error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
8083 entry, i, filedata->file_header.e_shnum - 1);
8084 if (num_group_errors == 10)
8085 warn (_("Further error messages about overlarge group section indices suppressed\n"));
8086 }
8087 continue;
8088 }
8089
8090 if (filedata->section_headers_groups [entry] != NULL)
8091 {
8092 if (entry)
8093 {
8094 static unsigned num_errs = 0;
8095
8096 if (num_errs ++ < 10)
8097 {
8098 error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
8099 entry, i,
8100 filedata->section_headers_groups [entry]->group_index);
8101 if (num_errs == 10)
8102 warn (_("Further error messages about already contained group sections suppressed\n"));
8103 }
8104 continue;
8105 }
8106 else
8107 {
8108 /* Intel C/C++ compiler may put section 0 in a
8109 section group. We just warn it the first time
8110 and ignore it afterwards. */
8111 static bool warned = false;
8112 if (!warned)
8113 {
8114 error (_("section 0 in group section [%5u]\n"),
8115 filedata->section_headers_groups [entry]->group_index);
8116 warned = true;
8117 }
8118 }
8119 }
8120
8121 filedata->section_headers_groups [entry] = group;
8122
8123 if (do_section_groups)
8124 {
8125 sec = filedata->section_headers + entry;
8126 printf (" [%5u] %s\n", entry, printable_section_name (filedata, sec));
8127 }
8128
8129 g = (struct group_list *) xmalloc (sizeof (struct group_list));
8130 g->section_index = entry;
8131 g->next = group->root;
8132 group->root = g;
8133 }
8134
8135 free (start);
8136
8137 group++;
8138 }
8139 }
8140
8141 free (symtab);
8142 free (strtab);
8143 return true;
8144 }
8145
8146 /* Data used to display dynamic fixups. */
8147
8148 struct ia64_vms_dynfixup
8149 {
8150 uint64_t needed_ident; /* Library ident number. */
8151 uint64_t needed; /* Index in the dstrtab of the library name. */
8152 uint64_t fixup_needed; /* Index of the library. */
8153 uint64_t fixup_rela_cnt; /* Number of fixups. */
8154 uint64_t fixup_rela_off; /* Fixups offset in the dynamic segment. */
8155 };
8156
8157 /* Data used to display dynamic relocations. */
8158
8159 struct ia64_vms_dynimgrela
8160 {
8161 uint64_t img_rela_cnt; /* Number of relocations. */
8162 uint64_t img_rela_off; /* Reloc offset in the dynamic segment. */
8163 };
8164
8165 /* Display IA-64 OpenVMS dynamic fixups (used to dynamically link a shared
8166 library). */
8167
8168 static bool
8169 dump_ia64_vms_dynamic_fixups (Filedata * filedata,
8170 struct ia64_vms_dynfixup * fixup,
8171 const char * strtab,
8172 unsigned int strtab_sz)
8173 {
8174 Elf64_External_VMS_IMAGE_FIXUP * imfs;
8175 long i;
8176 const char * lib_name;
8177
8178 imfs = get_data (NULL, filedata,
8179 filedata->dynamic_addr + fixup->fixup_rela_off,
8180 sizeof (*imfs), fixup->fixup_rela_cnt,
8181 _("dynamic section image fixups"));
8182 if (!imfs)
8183 return false;
8184
8185 if (fixup->needed < strtab_sz)
8186 lib_name = strtab + fixup->needed;
8187 else
8188 {
8189 warn (_("corrupt library name index of 0x%lx found in dynamic entry"),
8190 (unsigned long) fixup->needed);
8191 lib_name = "???";
8192 }
8193
8194 printf (_("\nImage fixups for needed library #%d: %s - ident: %lx\n"),
8195 (int) fixup->fixup_needed, lib_name, (long) fixup->needed_ident);
8196 printf
8197 (_("Seg Offset Type SymVec DataType\n"));
8198
8199 for (i = 0; i < (long) fixup->fixup_rela_cnt; i++)
8200 {
8201 unsigned int type;
8202 const char *rtype;
8203
8204 printf ("%3u ", (unsigned) BYTE_GET (imfs [i].fixup_seg));
8205 printf ("%016" PRIx64 " ", BYTE_GET (imfs [i].fixup_offset));
8206 type = BYTE_GET (imfs [i].type);
8207 rtype = elf_ia64_reloc_type (type);
8208 if (rtype == NULL)
8209 printf ("0x%08x ", type);
8210 else
8211 printf ("%-32s ", rtype);
8212 printf ("%6u ", (unsigned) BYTE_GET (imfs [i].symvec_index));
8213 printf ("0x%08x\n", (unsigned) BYTE_GET (imfs [i].data_type));
8214 }
8215
8216 free (imfs);
8217 return true;
8218 }
8219
8220 /* Display IA-64 OpenVMS dynamic relocations (used to relocate an image). */
8221
8222 static bool
8223 dump_ia64_vms_dynamic_relocs (Filedata * filedata, struct ia64_vms_dynimgrela *imgrela)
8224 {
8225 Elf64_External_VMS_IMAGE_RELA *imrs;
8226 long i;
8227
8228 imrs = get_data (NULL, filedata,
8229 filedata->dynamic_addr + imgrela->img_rela_off,
8230 sizeof (*imrs), imgrela->img_rela_cnt,
8231 _("dynamic section image relocations"));
8232 if (!imrs)
8233 return false;
8234
8235 printf (_("\nImage relocs\n"));
8236 printf
8237 (_("Seg Offset Type Addend Seg Sym Off\n"));
8238
8239 for (i = 0; i < (long) imgrela->img_rela_cnt; i++)
8240 {
8241 unsigned int type;
8242 const char *rtype;
8243
8244 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].rela_seg));
8245 printf ("%08" PRIx64 " ", BYTE_GET (imrs [i].rela_offset));
8246 type = BYTE_GET (imrs [i].type);
8247 rtype = elf_ia64_reloc_type (type);
8248 if (rtype == NULL)
8249 printf ("0x%08x ", type);
8250 else
8251 printf ("%-31s ", rtype);
8252 print_vma (BYTE_GET (imrs [i].addend), FULL_HEX);
8253 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].sym_seg));
8254 printf ("%08" PRIx64 "\n", BYTE_GET (imrs [i].sym_offset));
8255 }
8256
8257 free (imrs);
8258 return true;
8259 }
8260
8261 /* Display IA-64 OpenVMS dynamic relocations and fixups. */
8262
8263 static bool
8264 process_ia64_vms_dynamic_relocs (Filedata * filedata)
8265 {
8266 struct ia64_vms_dynfixup fixup;
8267 struct ia64_vms_dynimgrela imgrela;
8268 Elf_Internal_Dyn *entry;
8269 uint64_t strtab_off = 0;
8270 uint64_t strtab_sz = 0;
8271 char *strtab = NULL;
8272 bool res = true;
8273
8274 memset (&fixup, 0, sizeof (fixup));
8275 memset (&imgrela, 0, sizeof (imgrela));
8276
8277 /* Note: the order of the entries is specified by the OpenVMS specs. */
8278 for (entry = filedata->dynamic_section;
8279 entry < filedata->dynamic_section + filedata->dynamic_nent;
8280 entry++)
8281 {
8282 switch (entry->d_tag)
8283 {
8284 case DT_IA_64_VMS_STRTAB_OFFSET:
8285 strtab_off = entry->d_un.d_val;
8286 break;
8287 case DT_STRSZ:
8288 strtab_sz = entry->d_un.d_val;
8289 if (strtab == NULL)
8290 strtab = get_data (NULL, filedata,
8291 filedata->dynamic_addr + strtab_off,
8292 1, strtab_sz, _("dynamic string section"));
8293 if (strtab == NULL)
8294 strtab_sz = 0;
8295 break;
8296
8297 case DT_IA_64_VMS_NEEDED_IDENT:
8298 fixup.needed_ident = entry->d_un.d_val;
8299 break;
8300 case DT_NEEDED:
8301 fixup.needed = entry->d_un.d_val;
8302 break;
8303 case DT_IA_64_VMS_FIXUP_NEEDED:
8304 fixup.fixup_needed = entry->d_un.d_val;
8305 break;
8306 case DT_IA_64_VMS_FIXUP_RELA_CNT:
8307 fixup.fixup_rela_cnt = entry->d_un.d_val;
8308 break;
8309 case DT_IA_64_VMS_FIXUP_RELA_OFF:
8310 fixup.fixup_rela_off = entry->d_un.d_val;
8311 if (! dump_ia64_vms_dynamic_fixups (filedata, &fixup, strtab, strtab_sz))
8312 res = false;
8313 break;
8314 case DT_IA_64_VMS_IMG_RELA_CNT:
8315 imgrela.img_rela_cnt = entry->d_un.d_val;
8316 break;
8317 case DT_IA_64_VMS_IMG_RELA_OFF:
8318 imgrela.img_rela_off = entry->d_un.d_val;
8319 if (! dump_ia64_vms_dynamic_relocs (filedata, &imgrela))
8320 res = false;
8321 break;
8322
8323 default:
8324 break;
8325 }
8326 }
8327
8328 free (strtab);
8329
8330 return res;
8331 }
8332
8333 static struct
8334 {
8335 const char * name;
8336 int reloc;
8337 int size;
8338 relocation_type rel_type;
8339 }
8340 dynamic_relocations [] =
8341 {
8342 { "REL", DT_REL, DT_RELSZ, reltype_rel },
8343 { "RELA", DT_RELA, DT_RELASZ, reltype_rela },
8344 { "RELR", DT_RELR, DT_RELRSZ, reltype_relr },
8345 { "PLT", DT_JMPREL, DT_PLTRELSZ, reltype_unknown }
8346 };
8347
8348 /* Process the reloc section. */
8349
8350 static bool
8351 process_relocs (Filedata * filedata)
8352 {
8353 unsigned long rel_size;
8354 unsigned long rel_offset;
8355
8356 if (!do_reloc)
8357 return true;
8358
8359 if (do_using_dynamic)
8360 {
8361 relocation_type rel_type;
8362 const char * name;
8363 bool has_dynamic_reloc;
8364 unsigned int i;
8365
8366 has_dynamic_reloc = false;
8367
8368 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
8369 {
8370 rel_type = dynamic_relocations [i].rel_type;
8371 name = dynamic_relocations [i].name;
8372 rel_size = filedata->dynamic_info[dynamic_relocations [i].size];
8373 rel_offset = filedata->dynamic_info[dynamic_relocations [i].reloc];
8374
8375 if (rel_size)
8376 has_dynamic_reloc = true;
8377
8378 if (rel_type == reltype_unknown)
8379 {
8380 if (dynamic_relocations [i].reloc == DT_JMPREL)
8381 switch (filedata->dynamic_info[DT_PLTREL])
8382 {
8383 case DT_REL:
8384 rel_type = reltype_rel;
8385 break;
8386 case DT_RELA:
8387 rel_type = reltype_rela;
8388 break;
8389 }
8390 }
8391
8392 if (rel_size)
8393 {
8394 if (filedata->is_separate)
8395 printf
8396 (_("\nIn linked file '%s' section '%s' at offset 0x%lx contains %ld bytes:\n"),
8397 filedata->file_name, name, rel_offset, rel_size);
8398 else
8399 printf
8400 (_("\n'%s' relocation section at offset 0x%lx contains %ld bytes:\n"),
8401 name, rel_offset, rel_size);
8402
8403 dump_relocations (filedata,
8404 offset_from_vma (filedata, rel_offset, rel_size),
8405 rel_size,
8406 filedata->dynamic_symbols,
8407 filedata->num_dynamic_syms,
8408 filedata->dynamic_strings,
8409 filedata->dynamic_strings_length,
8410 rel_type, true /* is_dynamic */);
8411 }
8412 }
8413
8414 if (is_ia64_vms (filedata))
8415 if (process_ia64_vms_dynamic_relocs (filedata))
8416 has_dynamic_reloc = true;
8417
8418 if (! has_dynamic_reloc)
8419 {
8420 if (filedata->is_separate)
8421 printf (_("\nThere are no dynamic relocations in linked file '%s'.\n"),
8422 filedata->file_name);
8423 else
8424 printf (_("\nThere are no dynamic relocations in this file.\n"));
8425 }
8426 }
8427 else
8428 {
8429 Elf_Internal_Shdr * section;
8430 unsigned long i;
8431 bool found = false;
8432
8433 for (i = 0, section = filedata->section_headers;
8434 i < filedata->file_header.e_shnum;
8435 i++, section++)
8436 {
8437 if ( section->sh_type != SHT_RELA
8438 && section->sh_type != SHT_REL
8439 && section->sh_type != SHT_RELR)
8440 continue;
8441
8442 rel_offset = section->sh_offset;
8443 rel_size = section->sh_size;
8444
8445 if (rel_size)
8446 {
8447 relocation_type rel_type;
8448 unsigned long num_rela;
8449
8450 if (filedata->is_separate)
8451 printf (_("\nIn linked file '%s' relocation section "),
8452 filedata->file_name);
8453 else
8454 printf (_("\nRelocation section "));
8455
8456 if (filedata->string_table == NULL)
8457 printf ("%d", section->sh_name);
8458 else
8459 printf ("'%s'", printable_section_name (filedata, section));
8460
8461 num_rela = rel_size / section->sh_entsize;
8462 printf (ngettext (" at offset 0x%lx contains %lu entry:\n",
8463 " at offset 0x%lx contains %lu entries:\n",
8464 num_rela),
8465 rel_offset, num_rela);
8466
8467 rel_type = section->sh_type == SHT_RELA ? reltype_rela :
8468 section->sh_type == SHT_REL ? reltype_rel : reltype_relr;
8469
8470 if (section->sh_link != 0
8471 && section->sh_link < filedata->file_header.e_shnum)
8472 {
8473 Elf_Internal_Shdr * symsec;
8474 Elf_Internal_Sym * symtab;
8475 unsigned long nsyms;
8476 unsigned long strtablen = 0;
8477 char * strtab = NULL;
8478
8479 symsec = filedata->section_headers + section->sh_link;
8480 if (symsec->sh_type != SHT_SYMTAB
8481 && symsec->sh_type != SHT_DYNSYM)
8482 continue;
8483
8484 if (!get_symtab (filedata, symsec,
8485 &symtab, &nsyms, &strtab, &strtablen))
8486 continue;
8487
8488 dump_relocations (filedata, rel_offset, rel_size,
8489 symtab, nsyms, strtab, strtablen,
8490 rel_type,
8491 symsec->sh_type == SHT_DYNSYM);
8492 free (strtab);
8493 free (symtab);
8494 }
8495 else
8496 dump_relocations (filedata, rel_offset, rel_size,
8497 NULL, 0, NULL, 0, rel_type, false /* is_dynamic */);
8498
8499 found = true;
8500 }
8501 }
8502
8503 if (! found)
8504 {
8505 /* Users sometimes forget the -D option, so try to be helpful. */
8506 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
8507 {
8508 if (filedata->dynamic_info[dynamic_relocations [i].size])
8509 {
8510 if (filedata->is_separate)
8511 printf (_("\nThere are no static relocations in linked file '%s'."),
8512 filedata->file_name);
8513 else
8514 printf (_("\nThere are no static relocations in this file."));
8515 printf (_("\nTo see the dynamic relocations add --use-dynamic to the command line.\n"));
8516
8517 break;
8518 }
8519 }
8520 if (i == ARRAY_SIZE (dynamic_relocations))
8521 {
8522 if (filedata->is_separate)
8523 printf (_("\nThere are no relocations in linked file '%s'.\n"),
8524 filedata->file_name);
8525 else
8526 printf (_("\nThere are no relocations in this file.\n"));
8527 }
8528 }
8529 }
8530
8531 return true;
8532 }
8533
8534 /* An absolute address consists of a section and an offset. If the
8535 section is NULL, the offset itself is the address, otherwise, the
8536 address equals to LOAD_ADDRESS(section) + offset. */
8537
8538 struct absaddr
8539 {
8540 unsigned short section;
8541 uint64_t offset;
8542 };
8543
8544 /* Find the nearest symbol at or below ADDR. Returns the symbol
8545 name, if found, and the offset from the symbol to ADDR. */
8546
8547 static void
8548 find_symbol_for_address (Filedata * filedata,
8549 Elf_Internal_Sym * symtab,
8550 unsigned long nsyms,
8551 const char * strtab,
8552 unsigned long strtab_size,
8553 struct absaddr addr,
8554 const char ** symname,
8555 uint64_t * offset)
8556 {
8557 uint64_t dist = 0x100000;
8558 Elf_Internal_Sym * sym;
8559 Elf_Internal_Sym * beg;
8560 Elf_Internal_Sym * end;
8561 Elf_Internal_Sym * best = NULL;
8562
8563 REMOVE_ARCH_BITS (addr.offset);
8564 beg = symtab;
8565 end = symtab + nsyms;
8566
8567 while (beg < end)
8568 {
8569 uint64_t value;
8570
8571 sym = beg + (end - beg) / 2;
8572
8573 value = sym->st_value;
8574 REMOVE_ARCH_BITS (value);
8575
8576 if (sym->st_name != 0
8577 && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
8578 && addr.offset >= value
8579 && addr.offset - value < dist)
8580 {
8581 best = sym;
8582 dist = addr.offset - value;
8583 if (!dist)
8584 break;
8585 }
8586
8587 if (addr.offset < value)
8588 end = sym;
8589 else
8590 beg = sym + 1;
8591 }
8592
8593 if (best)
8594 {
8595 *symname = (best->st_name >= strtab_size
8596 ? _("<corrupt>") : strtab + best->st_name);
8597 *offset = dist;
8598 return;
8599 }
8600
8601 *symname = NULL;
8602 *offset = addr.offset;
8603 }
8604
8605 static /* signed */ int
8606 symcmp (const void *p, const void *q)
8607 {
8608 Elf_Internal_Sym *sp = (Elf_Internal_Sym *) p;
8609 Elf_Internal_Sym *sq = (Elf_Internal_Sym *) q;
8610
8611 return sp->st_value > sq->st_value ? 1 : (sp->st_value < sq->st_value ? -1 : 0);
8612 }
8613
8614 /* Process the unwind section. */
8615
8616 #include "unwind-ia64.h"
8617
8618 struct ia64_unw_table_entry
8619 {
8620 struct absaddr start;
8621 struct absaddr end;
8622 struct absaddr info;
8623 };
8624
8625 struct ia64_unw_aux_info
8626 {
8627 struct ia64_unw_table_entry * table; /* Unwind table. */
8628 unsigned long table_len; /* Length of unwind table. */
8629 unsigned char * info; /* Unwind info. */
8630 unsigned long info_size; /* Size of unwind info. */
8631 uint64_t info_addr; /* Starting address of unwind info. */
8632 uint64_t seg_base; /* Starting address of segment. */
8633 Elf_Internal_Sym * symtab; /* The symbol table. */
8634 unsigned long nsyms; /* Number of symbols. */
8635 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
8636 unsigned long nfuns; /* Number of entries in funtab. */
8637 char * strtab; /* The string table. */
8638 unsigned long strtab_size; /* Size of string table. */
8639 };
8640
8641 static bool
8642 dump_ia64_unwind (Filedata * filedata, struct ia64_unw_aux_info * aux)
8643 {
8644 struct ia64_unw_table_entry * tp;
8645 unsigned long j, nfuns;
8646 int in_body;
8647 bool res = true;
8648
8649 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
8650 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
8651 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
8652 aux->funtab[nfuns++] = aux->symtab[j];
8653 aux->nfuns = nfuns;
8654 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
8655
8656 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
8657 {
8658 uint64_t stamp;
8659 uint64_t offset;
8660 const unsigned char * dp;
8661 const unsigned char * head;
8662 const unsigned char * end;
8663 const char * procname;
8664
8665 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
8666 aux->strtab_size, tp->start, &procname, &offset);
8667
8668 fputs ("\n<", stdout);
8669
8670 if (procname)
8671 {
8672 fputs (procname, stdout);
8673
8674 if (offset)
8675 printf ("+%lx", (unsigned long) offset);
8676 }
8677
8678 fputs (">: [", stdout);
8679 print_vma (tp->start.offset, PREFIX_HEX);
8680 fputc ('-', stdout);
8681 print_vma (tp->end.offset, PREFIX_HEX);
8682 printf ("], info at +0x%lx\n",
8683 (unsigned long) (tp->info.offset - aux->seg_base));
8684
8685 /* PR 17531: file: 86232b32. */
8686 if (aux->info == NULL)
8687 continue;
8688
8689 offset = tp->info.offset;
8690 if (tp->info.section)
8691 {
8692 if (tp->info.section >= filedata->file_header.e_shnum)
8693 {
8694 warn (_("Invalid section %u in table entry %ld\n"),
8695 tp->info.section, (long) (tp - aux->table));
8696 res = false;
8697 continue;
8698 }
8699 offset += filedata->section_headers[tp->info.section].sh_addr;
8700 }
8701 offset -= aux->info_addr;
8702 /* PR 17531: file: 0997b4d1. */
8703 if (offset >= aux->info_size
8704 || aux->info_size - offset < 8)
8705 {
8706 warn (_("Invalid offset %lx in table entry %ld\n"),
8707 (long) tp->info.offset, (long) (tp - aux->table));
8708 res = false;
8709 continue;
8710 }
8711
8712 head = aux->info + offset;
8713 stamp = byte_get ((unsigned char *) head, sizeof (stamp));
8714
8715 printf (" v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
8716 (unsigned) UNW_VER (stamp),
8717 (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
8718 UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
8719 UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
8720 (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
8721
8722 if (UNW_VER (stamp) != 1)
8723 {
8724 printf (_("\tUnknown version.\n"));
8725 continue;
8726 }
8727
8728 in_body = 0;
8729 end = head + 8 + eh_addr_size * UNW_LENGTH (stamp);
8730 /* PR 17531: file: 16ceda89. */
8731 if (end > aux->info + aux->info_size)
8732 end = aux->info + aux->info_size;
8733 for (dp = head + 8; dp < end;)
8734 dp = unw_decode (dp, in_body, & in_body, end);
8735 }
8736
8737 free (aux->funtab);
8738
8739 return res;
8740 }
8741
8742 static bool
8743 slurp_ia64_unwind_table (Filedata * filedata,
8744 struct ia64_unw_aux_info * aux,
8745 Elf_Internal_Shdr * sec)
8746 {
8747 unsigned long size, nrelas, i;
8748 Elf_Internal_Phdr * seg;
8749 struct ia64_unw_table_entry * tep;
8750 Elf_Internal_Shdr * relsec;
8751 Elf_Internal_Rela * rela;
8752 Elf_Internal_Rela * rp;
8753 unsigned char * table;
8754 unsigned char * tp;
8755 Elf_Internal_Sym * sym;
8756 const char * relname;
8757
8758 aux->table_len = 0;
8759
8760 /* First, find the starting address of the segment that includes
8761 this section: */
8762
8763 if (filedata->file_header.e_phnum)
8764 {
8765 if (! get_program_headers (filedata))
8766 return false;
8767
8768 for (seg = filedata->program_headers;
8769 seg < filedata->program_headers + filedata->file_header.e_phnum;
8770 ++seg)
8771 {
8772 if (seg->p_type != PT_LOAD)
8773 continue;
8774
8775 if (sec->sh_addr >= seg->p_vaddr
8776 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
8777 {
8778 aux->seg_base = seg->p_vaddr;
8779 break;
8780 }
8781 }
8782 }
8783
8784 /* Second, build the unwind table from the contents of the unwind section: */
8785 size = sec->sh_size;
8786 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
8787 _("unwind table"));
8788 if (!table)
8789 return false;
8790
8791 aux->table_len = size / (3 * eh_addr_size);
8792 aux->table = (struct ia64_unw_table_entry *)
8793 xcmalloc (aux->table_len, sizeof (aux->table[0]));
8794 tep = aux->table;
8795
8796 for (tp = table; tp <= table + size - (3 * eh_addr_size); ++tep)
8797 {
8798 tep->start.section = SHN_UNDEF;
8799 tep->end.section = SHN_UNDEF;
8800 tep->info.section = SHN_UNDEF;
8801 tep->start.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8802 tep->end.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8803 tep->info.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8804 tep->start.offset += aux->seg_base;
8805 tep->end.offset += aux->seg_base;
8806 tep->info.offset += aux->seg_base;
8807 }
8808 free (table);
8809
8810 /* Third, apply any relocations to the unwind table: */
8811 for (relsec = filedata->section_headers;
8812 relsec < filedata->section_headers + filedata->file_header.e_shnum;
8813 ++relsec)
8814 {
8815 if (relsec->sh_type != SHT_RELA
8816 || relsec->sh_info >= filedata->file_header.e_shnum
8817 || filedata->section_headers + relsec->sh_info != sec)
8818 continue;
8819
8820 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
8821 & rela, & nrelas))
8822 {
8823 free (aux->table);
8824 aux->table = NULL;
8825 aux->table_len = 0;
8826 return false;
8827 }
8828
8829 for (rp = rela; rp < rela + nrelas; ++rp)
8830 {
8831 unsigned int sym_ndx;
8832 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
8833 relname = elf_ia64_reloc_type (r_type);
8834
8835 /* PR 17531: file: 9fa67536. */
8836 if (relname == NULL)
8837 {
8838 warn (_("Skipping unknown relocation type: %u\n"), r_type);
8839 continue;
8840 }
8841
8842 if (! startswith (relname, "R_IA64_SEGREL"))
8843 {
8844 warn (_("Skipping unexpected relocation type: %s\n"), relname);
8845 continue;
8846 }
8847
8848 i = rp->r_offset / (3 * eh_addr_size);
8849
8850 /* PR 17531: file: 5bc8d9bf. */
8851 if (i >= aux->table_len)
8852 {
8853 warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
8854 continue;
8855 }
8856
8857 sym_ndx = get_reloc_symindex (rp->r_info);
8858 if (sym_ndx >= aux->nsyms)
8859 {
8860 warn (_("Skipping reloc with invalid symbol index: %u\n"),
8861 sym_ndx);
8862 continue;
8863 }
8864 sym = aux->symtab + sym_ndx;
8865
8866 switch (rp->r_offset / eh_addr_size % 3)
8867 {
8868 case 0:
8869 aux->table[i].start.section = sym->st_shndx;
8870 aux->table[i].start.offset = rp->r_addend + sym->st_value;
8871 break;
8872 case 1:
8873 aux->table[i].end.section = sym->st_shndx;
8874 aux->table[i].end.offset = rp->r_addend + sym->st_value;
8875 break;
8876 case 2:
8877 aux->table[i].info.section = sym->st_shndx;
8878 aux->table[i].info.offset = rp->r_addend + sym->st_value;
8879 break;
8880 default:
8881 break;
8882 }
8883 }
8884
8885 free (rela);
8886 }
8887
8888 return true;
8889 }
8890
8891 static bool
8892 ia64_process_unwind (Filedata * filedata)
8893 {
8894 Elf_Internal_Shdr * sec;
8895 Elf_Internal_Shdr * unwsec = NULL;
8896 unsigned long i, unwcount = 0, unwstart = 0;
8897 struct ia64_unw_aux_info aux;
8898 bool res = true;
8899
8900 memset (& aux, 0, sizeof (aux));
8901
8902 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
8903 {
8904 if (sec->sh_type == SHT_SYMTAB)
8905 {
8906 if (aux.symtab)
8907 {
8908 error (_("Multiple symbol tables encountered\n"));
8909 free (aux.symtab);
8910 aux.symtab = NULL;
8911 free (aux.strtab);
8912 aux.strtab = NULL;
8913 }
8914 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
8915 &aux.strtab, &aux.strtab_size))
8916 return false;
8917 }
8918 else if (sec->sh_type == SHT_IA_64_UNWIND)
8919 unwcount++;
8920 }
8921
8922 if (!unwcount)
8923 printf (_("\nThere are no unwind sections in this file.\n"));
8924
8925 while (unwcount-- > 0)
8926 {
8927 const char *suffix;
8928 size_t len, len2;
8929
8930 for (i = unwstart, sec = filedata->section_headers + unwstart, unwsec = NULL;
8931 i < filedata->file_header.e_shnum; ++i, ++sec)
8932 if (sec->sh_type == SHT_IA_64_UNWIND)
8933 {
8934 unwsec = sec;
8935 break;
8936 }
8937 /* We have already counted the number of SHT_IA64_UNWIND
8938 sections so the loop above should never fail. */
8939 assert (unwsec != NULL);
8940
8941 unwstart = i + 1;
8942 len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
8943
8944 if ((unwsec->sh_flags & SHF_GROUP) != 0)
8945 {
8946 /* We need to find which section group it is in. */
8947 struct group_list * g;
8948
8949 if (filedata->section_headers_groups == NULL
8950 || filedata->section_headers_groups[i] == NULL)
8951 i = filedata->file_header.e_shnum;
8952 else
8953 {
8954 g = filedata->section_headers_groups[i]->root;
8955
8956 for (; g != NULL; g = g->next)
8957 {
8958 sec = filedata->section_headers + g->section_index;
8959
8960 if (section_name_valid (filedata, sec)
8961 && streq (section_name (filedata, sec),
8962 ELF_STRING_ia64_unwind_info))
8963 break;
8964 }
8965
8966 if (g == NULL)
8967 i = filedata->file_header.e_shnum;
8968 }
8969 }
8970 else if (section_name_valid (filedata, unwsec)
8971 && startswith (section_name (filedata, unwsec),
8972 ELF_STRING_ia64_unwind_once))
8973 {
8974 /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO. */
8975 len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
8976 suffix = section_name (filedata, unwsec) + len;
8977 for (i = 0, sec = filedata->section_headers;
8978 i < filedata->file_header.e_shnum;
8979 ++i, ++sec)
8980 if (section_name_valid (filedata, sec)
8981 && startswith (section_name (filedata, sec),
8982 ELF_STRING_ia64_unwind_info_once)
8983 && streq (section_name (filedata, sec) + len2, suffix))
8984 break;
8985 }
8986 else
8987 {
8988 /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
8989 .IA_64.unwind or BAR -> .IA_64.unwind_info. */
8990 len = sizeof (ELF_STRING_ia64_unwind) - 1;
8991 len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
8992 suffix = "";
8993 if (section_name_valid (filedata, unwsec)
8994 && startswith (section_name (filedata, unwsec),
8995 ELF_STRING_ia64_unwind))
8996 suffix = section_name (filedata, unwsec) + len;
8997 for (i = 0, sec = filedata->section_headers;
8998 i < filedata->file_header.e_shnum;
8999 ++i, ++sec)
9000 if (section_name_valid (filedata, sec)
9001 && startswith (section_name (filedata, sec),
9002 ELF_STRING_ia64_unwind_info)
9003 && streq (section_name (filedata, sec) + len2, suffix))
9004 break;
9005 }
9006
9007 if (i == filedata->file_header.e_shnum)
9008 {
9009 printf (_("\nCould not find unwind info section for "));
9010
9011 if (filedata->string_table == NULL)
9012 printf ("%d", unwsec->sh_name);
9013 else
9014 printf ("'%s'", printable_section_name (filedata, unwsec));
9015 }
9016 else
9017 {
9018 aux.info_addr = sec->sh_addr;
9019 aux.info = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1,
9020 sec->sh_size,
9021 _("unwind info"));
9022 aux.info_size = aux.info == NULL ? 0 : sec->sh_size;
9023
9024 printf (_("\nUnwind section "));
9025
9026 if (filedata->string_table == NULL)
9027 printf ("%d", unwsec->sh_name);
9028 else
9029 printf ("'%s'", printable_section_name (filedata, unwsec));
9030
9031 printf (_(" at offset 0x%lx contains %lu entries:\n"),
9032 (unsigned long) unwsec->sh_offset,
9033 (unsigned long) (unwsec->sh_size / (3 * eh_addr_size)));
9034
9035 if (slurp_ia64_unwind_table (filedata, & aux, unwsec)
9036 && aux.table_len > 0)
9037 dump_ia64_unwind (filedata, & aux);
9038
9039 free ((char *) aux.table);
9040 free ((char *) aux.info);
9041 aux.table = NULL;
9042 aux.info = NULL;
9043 }
9044 }
9045
9046 free (aux.symtab);
9047 free ((char *) aux.strtab);
9048
9049 return res;
9050 }
9051
9052 struct hppa_unw_table_entry
9053 {
9054 struct absaddr start;
9055 struct absaddr end;
9056 unsigned int Cannot_unwind:1; /* 0 */
9057 unsigned int Millicode:1; /* 1 */
9058 unsigned int Millicode_save_sr0:1; /* 2 */
9059 unsigned int Region_description:2; /* 3..4 */
9060 unsigned int reserved1:1; /* 5 */
9061 unsigned int Entry_SR:1; /* 6 */
9062 unsigned int Entry_FR:4; /* Number saved 7..10 */
9063 unsigned int Entry_GR:5; /* Number saved 11..15 */
9064 unsigned int Args_stored:1; /* 16 */
9065 unsigned int Variable_Frame:1; /* 17 */
9066 unsigned int Separate_Package_Body:1; /* 18 */
9067 unsigned int Frame_Extension_Millicode:1; /* 19 */
9068 unsigned int Stack_Overflow_Check:1; /* 20 */
9069 unsigned int Two_Instruction_SP_Increment:1; /* 21 */
9070 unsigned int Ada_Region:1; /* 22 */
9071 unsigned int cxx_info:1; /* 23 */
9072 unsigned int cxx_try_catch:1; /* 24 */
9073 unsigned int sched_entry_seq:1; /* 25 */
9074 unsigned int reserved2:1; /* 26 */
9075 unsigned int Save_SP:1; /* 27 */
9076 unsigned int Save_RP:1; /* 28 */
9077 unsigned int Save_MRP_in_frame:1; /* 29 */
9078 unsigned int extn_ptr_defined:1; /* 30 */
9079 unsigned int Cleanup_defined:1; /* 31 */
9080
9081 unsigned int MPE_XL_interrupt_marker:1; /* 0 */
9082 unsigned int HP_UX_interrupt_marker:1; /* 1 */
9083 unsigned int Large_frame:1; /* 2 */
9084 unsigned int Pseudo_SP_Set:1; /* 3 */
9085 unsigned int reserved4:1; /* 4 */
9086 unsigned int Total_frame_size:27; /* 5..31 */
9087 };
9088
9089 struct hppa_unw_aux_info
9090 {
9091 struct hppa_unw_table_entry * table; /* Unwind table. */
9092 unsigned long table_len; /* Length of unwind table. */
9093 uint64_t seg_base; /* Starting address of segment. */
9094 Elf_Internal_Sym * symtab; /* The symbol table. */
9095 unsigned long nsyms; /* Number of symbols. */
9096 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
9097 unsigned long nfuns; /* Number of entries in funtab. */
9098 char * strtab; /* The string table. */
9099 unsigned long strtab_size; /* Size of string table. */
9100 };
9101
9102 static bool
9103 dump_hppa_unwind (Filedata * filedata, struct hppa_unw_aux_info * aux)
9104 {
9105 struct hppa_unw_table_entry * tp;
9106 unsigned long j, nfuns;
9107 bool res = true;
9108
9109 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
9110 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
9111 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
9112 aux->funtab[nfuns++] = aux->symtab[j];
9113 aux->nfuns = nfuns;
9114 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
9115
9116 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
9117 {
9118 uint64_t offset;
9119 const char * procname;
9120
9121 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
9122 aux->strtab_size, tp->start, &procname,
9123 &offset);
9124
9125 fputs ("\n<", stdout);
9126
9127 if (procname)
9128 {
9129 fputs (procname, stdout);
9130
9131 if (offset)
9132 printf ("+%lx", (unsigned long) offset);
9133 }
9134
9135 fputs (">: [", stdout);
9136 print_vma (tp->start.offset, PREFIX_HEX);
9137 fputc ('-', stdout);
9138 print_vma (tp->end.offset, PREFIX_HEX);
9139 printf ("]\n\t");
9140
9141 #define PF(_m) if (tp->_m) printf (#_m " ");
9142 #define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
9143 PF(Cannot_unwind);
9144 PF(Millicode);
9145 PF(Millicode_save_sr0);
9146 /* PV(Region_description); */
9147 PF(Entry_SR);
9148 PV(Entry_FR);
9149 PV(Entry_GR);
9150 PF(Args_stored);
9151 PF(Variable_Frame);
9152 PF(Separate_Package_Body);
9153 PF(Frame_Extension_Millicode);
9154 PF(Stack_Overflow_Check);
9155 PF(Two_Instruction_SP_Increment);
9156 PF(Ada_Region);
9157 PF(cxx_info);
9158 PF(cxx_try_catch);
9159 PF(sched_entry_seq);
9160 PF(Save_SP);
9161 PF(Save_RP);
9162 PF(Save_MRP_in_frame);
9163 PF(extn_ptr_defined);
9164 PF(Cleanup_defined);
9165 PF(MPE_XL_interrupt_marker);
9166 PF(HP_UX_interrupt_marker);
9167 PF(Large_frame);
9168 PF(Pseudo_SP_Set);
9169 PV(Total_frame_size);
9170 #undef PF
9171 #undef PV
9172 }
9173
9174 printf ("\n");
9175
9176 free (aux->funtab);
9177
9178 return res;
9179 }
9180
9181 static bool
9182 slurp_hppa_unwind_table (Filedata * filedata,
9183 struct hppa_unw_aux_info * aux,
9184 Elf_Internal_Shdr * sec)
9185 {
9186 unsigned long size, unw_ent_size, nentries, nrelas, i;
9187 Elf_Internal_Phdr * seg;
9188 struct hppa_unw_table_entry * tep;
9189 Elf_Internal_Shdr * relsec;
9190 Elf_Internal_Rela * rela;
9191 Elf_Internal_Rela * rp;
9192 unsigned char * table;
9193 unsigned char * tp;
9194 Elf_Internal_Sym * sym;
9195 const char * relname;
9196
9197 /* First, find the starting address of the segment that includes
9198 this section. */
9199 if (filedata->file_header.e_phnum)
9200 {
9201 if (! get_program_headers (filedata))
9202 return false;
9203
9204 for (seg = filedata->program_headers;
9205 seg < filedata->program_headers + filedata->file_header.e_phnum;
9206 ++seg)
9207 {
9208 if (seg->p_type != PT_LOAD)
9209 continue;
9210
9211 if (sec->sh_addr >= seg->p_vaddr
9212 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
9213 {
9214 aux->seg_base = seg->p_vaddr;
9215 break;
9216 }
9217 }
9218 }
9219
9220 /* Second, build the unwind table from the contents of the unwind
9221 section. */
9222 size = sec->sh_size;
9223 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
9224 _("unwind table"));
9225 if (!table)
9226 return false;
9227
9228 unw_ent_size = 16;
9229 nentries = size / unw_ent_size;
9230 size = unw_ent_size * nentries;
9231
9232 aux->table_len = nentries;
9233 tep = aux->table = (struct hppa_unw_table_entry *)
9234 xcmalloc (nentries, sizeof (aux->table[0]));
9235
9236 for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
9237 {
9238 unsigned int tmp1, tmp2;
9239
9240 tep->start.section = SHN_UNDEF;
9241 tep->end.section = SHN_UNDEF;
9242
9243 tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
9244 tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
9245 tmp1 = byte_get ((unsigned char *) tp + 8, 4);
9246 tmp2 = byte_get ((unsigned char *) tp + 12, 4);
9247
9248 tep->start.offset += aux->seg_base;
9249 tep->end.offset += aux->seg_base;
9250
9251 tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
9252 tep->Millicode = (tmp1 >> 30) & 0x1;
9253 tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
9254 tep->Region_description = (tmp1 >> 27) & 0x3;
9255 tep->reserved1 = (tmp1 >> 26) & 0x1;
9256 tep->Entry_SR = (tmp1 >> 25) & 0x1;
9257 tep->Entry_FR = (tmp1 >> 21) & 0xf;
9258 tep->Entry_GR = (tmp1 >> 16) & 0x1f;
9259 tep->Args_stored = (tmp1 >> 15) & 0x1;
9260 tep->Variable_Frame = (tmp1 >> 14) & 0x1;
9261 tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
9262 tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
9263 tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
9264 tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
9265 tep->Ada_Region = (tmp1 >> 9) & 0x1;
9266 tep->cxx_info = (tmp1 >> 8) & 0x1;
9267 tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
9268 tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
9269 tep->reserved2 = (tmp1 >> 5) & 0x1;
9270 tep->Save_SP = (tmp1 >> 4) & 0x1;
9271 tep->Save_RP = (tmp1 >> 3) & 0x1;
9272 tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
9273 tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
9274 tep->Cleanup_defined = tmp1 & 0x1;
9275
9276 tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
9277 tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
9278 tep->Large_frame = (tmp2 >> 29) & 0x1;
9279 tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
9280 tep->reserved4 = (tmp2 >> 27) & 0x1;
9281 tep->Total_frame_size = tmp2 & 0x7ffffff;
9282 }
9283 free (table);
9284
9285 /* Third, apply any relocations to the unwind table. */
9286 for (relsec = filedata->section_headers;
9287 relsec < filedata->section_headers + filedata->file_header.e_shnum;
9288 ++relsec)
9289 {
9290 if (relsec->sh_type != SHT_RELA
9291 || relsec->sh_info >= filedata->file_header.e_shnum
9292 || filedata->section_headers + relsec->sh_info != sec)
9293 continue;
9294
9295 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
9296 & rela, & nrelas))
9297 return false;
9298
9299 for (rp = rela; rp < rela + nrelas; ++rp)
9300 {
9301 unsigned int sym_ndx;
9302 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
9303 relname = elf_hppa_reloc_type (r_type);
9304
9305 if (relname == NULL)
9306 {
9307 warn (_("Skipping unknown relocation type: %u\n"), r_type);
9308 continue;
9309 }
9310
9311 /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64. */
9312 if (! startswith (relname, "R_PARISC_SEGREL"))
9313 {
9314 warn (_("Skipping unexpected relocation type: %s\n"), relname);
9315 continue;
9316 }
9317
9318 i = rp->r_offset / unw_ent_size;
9319 if (i >= aux->table_len)
9320 {
9321 warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
9322 continue;
9323 }
9324
9325 sym_ndx = get_reloc_symindex (rp->r_info);
9326 if (sym_ndx >= aux->nsyms)
9327 {
9328 warn (_("Skipping reloc with invalid symbol index: %u\n"),
9329 sym_ndx);
9330 continue;
9331 }
9332 sym = aux->symtab + sym_ndx;
9333
9334 switch ((rp->r_offset % unw_ent_size) / 4)
9335 {
9336 case 0:
9337 aux->table[i].start.section = sym->st_shndx;
9338 aux->table[i].start.offset = sym->st_value + rp->r_addend;
9339 break;
9340 case 1:
9341 aux->table[i].end.section = sym->st_shndx;
9342 aux->table[i].end.offset = sym->st_value + rp->r_addend;
9343 break;
9344 default:
9345 break;
9346 }
9347 }
9348
9349 free (rela);
9350 }
9351
9352 return true;
9353 }
9354
9355 static bool
9356 hppa_process_unwind (Filedata * filedata)
9357 {
9358 struct hppa_unw_aux_info aux;
9359 Elf_Internal_Shdr * unwsec = NULL;
9360 Elf_Internal_Shdr * sec;
9361 unsigned long i;
9362 bool res = true;
9363
9364 if (filedata->string_table == NULL)
9365 return false;
9366
9367 memset (& aux, 0, sizeof (aux));
9368
9369 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9370 {
9371 if (sec->sh_type == SHT_SYMTAB)
9372 {
9373 if (aux.symtab)
9374 {
9375 error (_("Multiple symbol tables encountered\n"));
9376 free (aux.symtab);
9377 aux.symtab = NULL;
9378 free (aux.strtab);
9379 aux.strtab = NULL;
9380 }
9381 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
9382 &aux.strtab, &aux.strtab_size))
9383 return false;
9384 }
9385 else if (section_name_valid (filedata, sec)
9386 && streq (section_name (filedata, sec), ".PARISC.unwind"))
9387 unwsec = sec;
9388 }
9389
9390 if (!unwsec)
9391 printf (_("\nThere are no unwind sections in this file.\n"));
9392
9393 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9394 {
9395 if (section_name_valid (filedata, sec)
9396 && streq (section_name (filedata, sec), ".PARISC.unwind"))
9397 {
9398 unsigned long num_unwind = sec->sh_size / 16;
9399
9400 printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
9401 "contains %lu entry:\n",
9402 "\nUnwind section '%s' at offset 0x%lx "
9403 "contains %lu entries:\n",
9404 num_unwind),
9405 printable_section_name (filedata, sec),
9406 (unsigned long) sec->sh_offset,
9407 num_unwind);
9408
9409 if (! slurp_hppa_unwind_table (filedata, &aux, sec))
9410 res = false;
9411
9412 if (res && aux.table_len > 0)
9413 {
9414 if (! dump_hppa_unwind (filedata, &aux))
9415 res = false;
9416 }
9417
9418 free ((char *) aux.table);
9419 aux.table = NULL;
9420 }
9421 }
9422
9423 free (aux.symtab);
9424 free ((char *) aux.strtab);
9425
9426 return res;
9427 }
9428
9429 struct arm_section
9430 {
9431 unsigned char * data; /* The unwind data. */
9432 Elf_Internal_Shdr * sec; /* The cached unwind section header. */
9433 Elf_Internal_Rela * rela; /* The cached relocations for this section. */
9434 unsigned long nrelas; /* The number of relocations. */
9435 unsigned int rel_type; /* REL or RELA ? */
9436 Elf_Internal_Rela * next_rela; /* Cyclic pointer to the next reloc to process. */
9437 };
9438
9439 struct arm_unw_aux_info
9440 {
9441 Filedata * filedata; /* The file containing the unwind sections. */
9442 Elf_Internal_Sym * symtab; /* The file's symbol table. */
9443 unsigned long nsyms; /* Number of symbols. */
9444 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
9445 unsigned long nfuns; /* Number of these symbols. */
9446 char * strtab; /* The file's string table. */
9447 unsigned long strtab_size; /* Size of string table. */
9448 };
9449
9450 static const char *
9451 arm_print_vma_and_name (Filedata * filedata,
9452 struct arm_unw_aux_info * aux,
9453 uint64_t fn,
9454 struct absaddr addr)
9455 {
9456 const char *procname;
9457 uint64_t sym_offset;
9458
9459 if (addr.section == SHN_UNDEF)
9460 addr.offset = fn;
9461
9462 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
9463 aux->strtab_size, addr, &procname,
9464 &sym_offset);
9465
9466 print_vma (fn, PREFIX_HEX);
9467
9468 if (procname)
9469 {
9470 fputs (" <", stdout);
9471 fputs (procname, stdout);
9472
9473 if (sym_offset)
9474 printf ("+0x%lx", (unsigned long) sym_offset);
9475 fputc ('>', stdout);
9476 }
9477
9478 return procname;
9479 }
9480
9481 static void
9482 arm_free_section (struct arm_section *arm_sec)
9483 {
9484 free (arm_sec->data);
9485 free (arm_sec->rela);
9486 }
9487
9488 /* 1) If SEC does not match the one cached in ARM_SEC, then free the current
9489 cached section and install SEC instead.
9490 2) Locate the 32-bit word at WORD_OFFSET in unwind section SEC
9491 and return its valued in * WORDP, relocating if necessary.
9492 3) Update the NEXT_RELA field in ARM_SEC and store the section index and
9493 relocation's offset in ADDR.
9494 4) If SYM_NAME is non-NULL and a relocation was applied, record the offset
9495 into the string table of the symbol associated with the reloc. If no
9496 reloc was applied store -1 there.
9497 5) Return TRUE upon success, FALSE otherwise. */
9498
9499 static bool
9500 get_unwind_section_word (Filedata * filedata,
9501 struct arm_unw_aux_info * aux,
9502 struct arm_section * arm_sec,
9503 Elf_Internal_Shdr * sec,
9504 uint64_t word_offset,
9505 unsigned int * wordp,
9506 struct absaddr * addr,
9507 uint64_t * sym_name)
9508 {
9509 Elf_Internal_Rela *rp;
9510 Elf_Internal_Sym *sym;
9511 const char * relname;
9512 unsigned int word;
9513 bool wrapped;
9514
9515 if (sec == NULL || arm_sec == NULL)
9516 return false;
9517
9518 addr->section = SHN_UNDEF;
9519 addr->offset = 0;
9520
9521 if (sym_name != NULL)
9522 *sym_name = (uint64_t) -1;
9523
9524 /* If necessary, update the section cache. */
9525 if (sec != arm_sec->sec)
9526 {
9527 Elf_Internal_Shdr *relsec;
9528
9529 arm_free_section (arm_sec);
9530
9531 arm_sec->sec = sec;
9532 arm_sec->data = get_data (NULL, aux->filedata, sec->sh_offset, 1,
9533 sec->sh_size, _("unwind data"));
9534 arm_sec->rela = NULL;
9535 arm_sec->nrelas = 0;
9536
9537 for (relsec = filedata->section_headers;
9538 relsec < filedata->section_headers + filedata->file_header.e_shnum;
9539 ++relsec)
9540 {
9541 if (relsec->sh_info >= filedata->file_header.e_shnum
9542 || filedata->section_headers + relsec->sh_info != sec
9543 /* PR 15745: Check the section type as well. */
9544 || (relsec->sh_type != SHT_REL
9545 && relsec->sh_type != SHT_RELA))
9546 continue;
9547
9548 arm_sec->rel_type = relsec->sh_type;
9549 if (relsec->sh_type == SHT_REL)
9550 {
9551 if (!slurp_rel_relocs (aux->filedata, relsec->sh_offset,
9552 relsec->sh_size,
9553 & arm_sec->rela, & arm_sec->nrelas))
9554 return false;
9555 }
9556 else /* relsec->sh_type == SHT_RELA */
9557 {
9558 if (!slurp_rela_relocs (aux->filedata, relsec->sh_offset,
9559 relsec->sh_size,
9560 & arm_sec->rela, & arm_sec->nrelas))
9561 return false;
9562 }
9563 break;
9564 }
9565
9566 arm_sec->next_rela = arm_sec->rela;
9567 }
9568
9569 /* If there is no unwind data we can do nothing. */
9570 if (arm_sec->data == NULL)
9571 return false;
9572
9573 /* If the offset is invalid then fail. */
9574 if (/* PR 21343 *//* PR 18879 */
9575 sec->sh_size < 4
9576 || word_offset > sec->sh_size - 4)
9577 return false;
9578
9579 /* Get the word at the required offset. */
9580 word = byte_get (arm_sec->data + word_offset, 4);
9581
9582 /* PR 17531: file: id:000001,src:001266+003044,op:splice,rep:128. */
9583 if (arm_sec->rela == NULL)
9584 {
9585 * wordp = word;
9586 return true;
9587 }
9588
9589 /* Look through the relocs to find the one that applies to the provided offset. */
9590 wrapped = false;
9591 for (rp = arm_sec->next_rela; rp != arm_sec->rela + arm_sec->nrelas; rp++)
9592 {
9593 uint64_t prelval, offset;
9594
9595 if (rp->r_offset > word_offset && !wrapped)
9596 {
9597 rp = arm_sec->rela;
9598 wrapped = true;
9599 }
9600 if (rp->r_offset > word_offset)
9601 break;
9602
9603 if (rp->r_offset & 3)
9604 {
9605 warn (_("Skipping unexpected relocation at offset 0x%lx\n"),
9606 (unsigned long) rp->r_offset);
9607 continue;
9608 }
9609
9610 if (rp->r_offset < word_offset)
9611 continue;
9612
9613 /* PR 17531: file: 027-161405-0.004 */
9614 if (aux->symtab == NULL)
9615 continue;
9616
9617 if (arm_sec->rel_type == SHT_REL)
9618 {
9619 offset = word & 0x7fffffff;
9620 if (offset & 0x40000000)
9621 offset |= ~ (uint64_t) 0x7fffffff;
9622 }
9623 else if (arm_sec->rel_type == SHT_RELA)
9624 offset = rp->r_addend;
9625 else
9626 {
9627 error (_("Unknown section relocation type %d encountered\n"),
9628 arm_sec->rel_type);
9629 break;
9630 }
9631
9632 /* PR 17531 file: 027-1241568-0.004. */
9633 if (ELF32_R_SYM (rp->r_info) >= aux->nsyms)
9634 {
9635 error (_("Bad symbol index in unwind relocation (%lu > %lu)\n"),
9636 (unsigned long) ELF32_R_SYM (rp->r_info), aux->nsyms);
9637 break;
9638 }
9639
9640 sym = aux->symtab + ELF32_R_SYM (rp->r_info);
9641 offset += sym->st_value;
9642 prelval = offset - (arm_sec->sec->sh_addr + rp->r_offset);
9643
9644 /* Check that we are processing the expected reloc type. */
9645 if (filedata->file_header.e_machine == EM_ARM)
9646 {
9647 relname = elf_arm_reloc_type (ELF32_R_TYPE (rp->r_info));
9648 if (relname == NULL)
9649 {
9650 warn (_("Skipping unknown ARM relocation type: %d\n"),
9651 (int) ELF32_R_TYPE (rp->r_info));
9652 continue;
9653 }
9654
9655 if (streq (relname, "R_ARM_NONE"))
9656 continue;
9657
9658 if (! streq (relname, "R_ARM_PREL31"))
9659 {
9660 warn (_("Skipping unexpected ARM relocation type %s\n"), relname);
9661 continue;
9662 }
9663 }
9664 else if (filedata->file_header.e_machine == EM_TI_C6000)
9665 {
9666 relname = elf_tic6x_reloc_type (ELF32_R_TYPE (rp->r_info));
9667 if (relname == NULL)
9668 {
9669 warn (_("Skipping unknown C6000 relocation type: %d\n"),
9670 (int) ELF32_R_TYPE (rp->r_info));
9671 continue;
9672 }
9673
9674 if (streq (relname, "R_C6000_NONE"))
9675 continue;
9676
9677 if (! streq (relname, "R_C6000_PREL31"))
9678 {
9679 warn (_("Skipping unexpected C6000 relocation type %s\n"), relname);
9680 continue;
9681 }
9682
9683 prelval >>= 1;
9684 }
9685 else
9686 {
9687 /* This function currently only supports ARM and TI unwinders. */
9688 warn (_("Only TI and ARM unwinders are currently supported\n"));
9689 break;
9690 }
9691
9692 word = (word & ~ (uint64_t) 0x7fffffff) | (prelval & 0x7fffffff);
9693 addr->section = sym->st_shndx;
9694 addr->offset = offset;
9695
9696 if (sym_name)
9697 * sym_name = sym->st_name;
9698 break;
9699 }
9700
9701 *wordp = word;
9702 arm_sec->next_rela = rp;
9703
9704 return true;
9705 }
9706
9707 static const char *tic6x_unwind_regnames[16] =
9708 {
9709 "A15", "B15", "B14", "B13", "B12", "B11", "B10", "B3",
9710 "A14", "A13", "A12", "A11", "A10",
9711 "[invalid reg 13]", "[invalid reg 14]", "[invalid reg 15]"
9712 };
9713
9714 static void
9715 decode_tic6x_unwind_regmask (unsigned int mask)
9716 {
9717 int i;
9718
9719 for (i = 12; mask; mask >>= 1, i--)
9720 {
9721 if (mask & 1)
9722 {
9723 fputs (tic6x_unwind_regnames[i], stdout);
9724 if (mask > 1)
9725 fputs (", ", stdout);
9726 }
9727 }
9728 }
9729
9730 #define ADVANCE \
9731 if (remaining == 0 && more_words) \
9732 { \
9733 data_offset += 4; \
9734 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, \
9735 data_offset, & word, & addr, NULL)) \
9736 return false; \
9737 remaining = 4; \
9738 more_words--; \
9739 } \
9740
9741 #define GET_OP(OP) \
9742 ADVANCE; \
9743 if (remaining) \
9744 { \
9745 remaining--; \
9746 (OP) = word >> 24; \
9747 word <<= 8; \
9748 } \
9749 else \
9750 { \
9751 printf (_("[Truncated opcode]\n")); \
9752 return false; \
9753 } \
9754 printf ("0x%02x ", OP)
9755
9756 static bool
9757 decode_arm_unwind_bytecode (Filedata * filedata,
9758 struct arm_unw_aux_info * aux,
9759 unsigned int word,
9760 unsigned int remaining,
9761 unsigned int more_words,
9762 uint64_t data_offset,
9763 Elf_Internal_Shdr * data_sec,
9764 struct arm_section * data_arm_sec)
9765 {
9766 struct absaddr addr;
9767 bool res = true;
9768
9769 /* Decode the unwinding instructions. */
9770 while (1)
9771 {
9772 unsigned int op, op2;
9773
9774 ADVANCE;
9775 if (remaining == 0)
9776 break;
9777 remaining--;
9778 op = word >> 24;
9779 word <<= 8;
9780
9781 printf (" 0x%02x ", op);
9782
9783 if ((op & 0xc0) == 0x00)
9784 {
9785 int offset = ((op & 0x3f) << 2) + 4;
9786
9787 printf (" vsp = vsp + %d", offset);
9788 }
9789 else if ((op & 0xc0) == 0x40)
9790 {
9791 int offset = ((op & 0x3f) << 2) + 4;
9792
9793 printf (" vsp = vsp - %d", offset);
9794 }
9795 else if ((op & 0xf0) == 0x80)
9796 {
9797 GET_OP (op2);
9798 if (op == 0x80 && op2 == 0)
9799 printf (_("Refuse to unwind"));
9800 else
9801 {
9802 unsigned int mask = ((op & 0x0f) << 8) | op2;
9803 bool first = true;
9804 int i;
9805
9806 printf ("pop {");
9807 for (i = 0; i < 12; i++)
9808 if (mask & (1 << i))
9809 {
9810 if (first)
9811 first = false;
9812 else
9813 printf (", ");
9814 printf ("r%d", 4 + i);
9815 }
9816 printf ("}");
9817 }
9818 }
9819 else if ((op & 0xf0) == 0x90)
9820 {
9821 if (op == 0x9d || op == 0x9f)
9822 printf (_(" [Reserved]"));
9823 else
9824 printf (" vsp = r%d", op & 0x0f);
9825 }
9826 else if ((op & 0xf0) == 0xa0)
9827 {
9828 int end = 4 + (op & 0x07);
9829 bool first = true;
9830 int i;
9831
9832 printf (" pop {");
9833 for (i = 4; i <= end; i++)
9834 {
9835 if (first)
9836 first = false;
9837 else
9838 printf (", ");
9839 printf ("r%d", i);
9840 }
9841 if (op & 0x08)
9842 {
9843 if (!first)
9844 printf (", ");
9845 printf ("r14");
9846 }
9847 printf ("}");
9848 }
9849 else if (op == 0xb0)
9850 printf (_(" finish"));
9851 else if (op == 0xb1)
9852 {
9853 GET_OP (op2);
9854 if (op2 == 0 || (op2 & 0xf0) != 0)
9855 printf (_("[Spare]"));
9856 else
9857 {
9858 unsigned int mask = op2 & 0x0f;
9859 bool first = true;
9860 int i;
9861
9862 printf ("pop {");
9863 for (i = 0; i < 12; i++)
9864 if (mask & (1 << i))
9865 {
9866 if (first)
9867 first = false;
9868 else
9869 printf (", ");
9870 printf ("r%d", i);
9871 }
9872 printf ("}");
9873 }
9874 }
9875 else if (op == 0xb2)
9876 {
9877 unsigned char buf[9];
9878 unsigned int i, len;
9879 unsigned long offset;
9880
9881 for (i = 0; i < sizeof (buf); i++)
9882 {
9883 GET_OP (buf[i]);
9884 if ((buf[i] & 0x80) == 0)
9885 break;
9886 }
9887 if (i == sizeof (buf))
9888 {
9889 error (_("corrupt change to vsp\n"));
9890 res = false;
9891 }
9892 else
9893 {
9894 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
9895 assert (len == i + 1);
9896 offset = offset * 4 + 0x204;
9897 printf ("vsp = vsp + %ld", offset);
9898 }
9899 }
9900 else if (op == 0xb3 || op == 0xc8 || op == 0xc9)
9901 {
9902 unsigned int first, last;
9903
9904 GET_OP (op2);
9905 first = op2 >> 4;
9906 last = op2 & 0x0f;
9907 if (op == 0xc8)
9908 first = first + 16;
9909 printf ("pop {D%d", first);
9910 if (last)
9911 printf ("-D%d", first + last);
9912 printf ("}");
9913 }
9914 else if (op == 0xb4)
9915 printf (_(" pop {ra_auth_code}"));
9916 else if ((op & 0xf8) == 0xb8 || (op & 0xf8) == 0xd0)
9917 {
9918 unsigned int count = op & 0x07;
9919
9920 printf ("pop {D8");
9921 if (count)
9922 printf ("-D%d", 8 + count);
9923 printf ("}");
9924 }
9925 else if (op >= 0xc0 && op <= 0xc5)
9926 {
9927 unsigned int count = op & 0x07;
9928
9929 printf (" pop {wR10");
9930 if (count)
9931 printf ("-wR%d", 10 + count);
9932 printf ("}");
9933 }
9934 else if (op == 0xc6)
9935 {
9936 unsigned int first, last;
9937
9938 GET_OP (op2);
9939 first = op2 >> 4;
9940 last = op2 & 0x0f;
9941 printf ("pop {wR%d", first);
9942 if (last)
9943 printf ("-wR%d", first + last);
9944 printf ("}");
9945 }
9946 else if (op == 0xc7)
9947 {
9948 GET_OP (op2);
9949 if (op2 == 0 || (op2 & 0xf0) != 0)
9950 printf (_("[Spare]"));
9951 else
9952 {
9953 unsigned int mask = op2 & 0x0f;
9954 bool first = true;
9955 int i;
9956
9957 printf ("pop {");
9958 for (i = 0; i < 4; i++)
9959 if (mask & (1 << i))
9960 {
9961 if (first)
9962 first = false;
9963 else
9964 printf (", ");
9965 printf ("wCGR%d", i);
9966 }
9967 printf ("}");
9968 }
9969 }
9970 else
9971 {
9972 printf (_(" [unsupported opcode]"));
9973 res = false;
9974 }
9975
9976 printf ("\n");
9977 }
9978
9979 return res;
9980 }
9981
9982 static bool
9983 decode_tic6x_unwind_bytecode (Filedata * filedata,
9984 struct arm_unw_aux_info * aux,
9985 unsigned int word,
9986 unsigned int remaining,
9987 unsigned int more_words,
9988 uint64_t data_offset,
9989 Elf_Internal_Shdr * data_sec,
9990 struct arm_section * data_arm_sec)
9991 {
9992 struct absaddr addr;
9993
9994 /* Decode the unwinding instructions. */
9995 while (1)
9996 {
9997 unsigned int op, op2;
9998
9999 ADVANCE;
10000 if (remaining == 0)
10001 break;
10002 remaining--;
10003 op = word >> 24;
10004 word <<= 8;
10005
10006 printf (" 0x%02x ", op);
10007
10008 if ((op & 0xc0) == 0x00)
10009 {
10010 int offset = ((op & 0x3f) << 3) + 8;
10011 printf (" sp = sp + %d", offset);
10012 }
10013 else if ((op & 0xc0) == 0x80)
10014 {
10015 GET_OP (op2);
10016 if (op == 0x80 && op2 == 0)
10017 printf (_("Refuse to unwind"));
10018 else
10019 {
10020 unsigned int mask = ((op & 0x1f) << 8) | op2;
10021 if (op & 0x20)
10022 printf ("pop compact {");
10023 else
10024 printf ("pop {");
10025
10026 decode_tic6x_unwind_regmask (mask);
10027 printf("}");
10028 }
10029 }
10030 else if ((op & 0xf0) == 0xc0)
10031 {
10032 unsigned int reg;
10033 unsigned int nregs;
10034 unsigned int i;
10035 const char *name;
10036 struct
10037 {
10038 unsigned int offset;
10039 unsigned int reg;
10040 } regpos[16];
10041
10042 /* Scan entire instruction first so that GET_OP output is not
10043 interleaved with disassembly. */
10044 nregs = 0;
10045 for (i = 0; nregs < (op & 0xf); i++)
10046 {
10047 GET_OP (op2);
10048 reg = op2 >> 4;
10049 if (reg != 0xf)
10050 {
10051 regpos[nregs].offset = i * 2;
10052 regpos[nregs].reg = reg;
10053 nregs++;
10054 }
10055
10056 reg = op2 & 0xf;
10057 if (reg != 0xf)
10058 {
10059 regpos[nregs].offset = i * 2 + 1;
10060 regpos[nregs].reg = reg;
10061 nregs++;
10062 }
10063 }
10064
10065 printf (_("pop frame {"));
10066 if (nregs == 0)
10067 {
10068 printf (_("*corrupt* - no registers specified"));
10069 }
10070 else
10071 {
10072 reg = nregs - 1;
10073 for (i = i * 2; i > 0; i--)
10074 {
10075 if (regpos[reg].offset == i - 1)
10076 {
10077 name = tic6x_unwind_regnames[regpos[reg].reg];
10078 if (reg > 0)
10079 reg--;
10080 }
10081 else
10082 name = _("[pad]");
10083
10084 fputs (name, stdout);
10085 if (i > 1)
10086 printf (", ");
10087 }
10088 }
10089
10090 printf ("}");
10091 }
10092 else if (op == 0xd0)
10093 printf (" MOV FP, SP");
10094 else if (op == 0xd1)
10095 printf (" __c6xabi_pop_rts");
10096 else if (op == 0xd2)
10097 {
10098 unsigned char buf[9];
10099 unsigned int i, len;
10100 unsigned long offset;
10101
10102 for (i = 0; i < sizeof (buf); i++)
10103 {
10104 GET_OP (buf[i]);
10105 if ((buf[i] & 0x80) == 0)
10106 break;
10107 }
10108 /* PR 17531: file: id:000001,src:001906+004739,op:splice,rep:2. */
10109 if (i == sizeof (buf))
10110 {
10111 warn (_("Corrupt stack pointer adjustment detected\n"));
10112 return false;
10113 }
10114
10115 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
10116 assert (len == i + 1);
10117 offset = offset * 8 + 0x408;
10118 printf (_("sp = sp + %ld"), offset);
10119 }
10120 else if ((op & 0xf0) == 0xe0)
10121 {
10122 if ((op & 0x0f) == 7)
10123 printf (" RETURN");
10124 else
10125 printf (" MV %s, B3", tic6x_unwind_regnames[op & 0x0f]);
10126 }
10127 else
10128 {
10129 printf (_(" [unsupported opcode]"));
10130 }
10131 putchar ('\n');
10132 }
10133
10134 return true;
10135 }
10136
10137 static uint64_t
10138 arm_expand_prel31 (Filedata * filedata, uint64_t word, uint64_t where)
10139 {
10140 uint64_t offset;
10141
10142 offset = word & 0x7fffffff;
10143 if (offset & 0x40000000)
10144 offset |= ~ (uint64_t) 0x7fffffff;
10145
10146 if (filedata->file_header.e_machine == EM_TI_C6000)
10147 offset <<= 1;
10148
10149 return offset + where;
10150 }
10151
10152 static bool
10153 decode_arm_unwind (Filedata * filedata,
10154 struct arm_unw_aux_info * aux,
10155 unsigned int word,
10156 unsigned int remaining,
10157 uint64_t data_offset,
10158 Elf_Internal_Shdr * data_sec,
10159 struct arm_section * data_arm_sec)
10160 {
10161 int per_index;
10162 unsigned int more_words = 0;
10163 struct absaddr addr;
10164 uint64_t sym_name = (uint64_t) -1;
10165 bool res = true;
10166
10167 if (remaining == 0)
10168 {
10169 /* Fetch the first word.
10170 Note - when decoding an object file the address extracted
10171 here will always be 0. So we also pass in the sym_name
10172 parameter so that we can find the symbol associated with
10173 the personality routine. */
10174 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, data_offset,
10175 & word, & addr, & sym_name))
10176 return false;
10177
10178 remaining = 4;
10179 }
10180 else
10181 {
10182 addr.section = SHN_UNDEF;
10183 addr.offset = 0;
10184 }
10185
10186 if ((word & 0x80000000) == 0)
10187 {
10188 /* Expand prel31 for personality routine. */
10189 uint64_t fn;
10190 const char *procname;
10191
10192 fn = arm_expand_prel31 (filedata, word, data_sec->sh_addr + data_offset);
10193 printf (_(" Personality routine: "));
10194 if (fn == 0
10195 && addr.section == SHN_UNDEF && addr.offset == 0
10196 && sym_name != (uint64_t) -1 && sym_name < aux->strtab_size)
10197 {
10198 procname = aux->strtab + sym_name;
10199 print_vma (fn, PREFIX_HEX);
10200 if (procname)
10201 {
10202 fputs (" <", stdout);
10203 fputs (procname, stdout);
10204 fputc ('>', stdout);
10205 }
10206 }
10207 else
10208 procname = arm_print_vma_and_name (filedata, aux, fn, addr);
10209 fputc ('\n', stdout);
10210
10211 /* The GCC personality routines use the standard compact
10212 encoding, starting with one byte giving the number of
10213 words. */
10214 if (procname != NULL
10215 && (startswith (procname, "__gcc_personality_v0")
10216 || startswith (procname, "__gxx_personality_v0")
10217 || startswith (procname, "__gcj_personality_v0")
10218 || startswith (procname, "__gnu_objc_personality_v0")))
10219 {
10220 remaining = 0;
10221 more_words = 1;
10222 ADVANCE;
10223 if (!remaining)
10224 {
10225 printf (_(" [Truncated data]\n"));
10226 return false;
10227 }
10228 more_words = word >> 24;
10229 word <<= 8;
10230 remaining--;
10231 per_index = -1;
10232 }
10233 else
10234 return true;
10235 }
10236 else
10237 {
10238 /* ARM EHABI Section 6.3:
10239
10240 An exception-handling table entry for the compact model looks like:
10241
10242 31 30-28 27-24 23-0
10243 -- ----- ----- ----
10244 1 0 index Data for personalityRoutine[index] */
10245
10246 if (filedata->file_header.e_machine == EM_ARM
10247 && (word & 0x70000000))
10248 {
10249 warn (_("Corrupt ARM compact model table entry: %x \n"), word);
10250 res = false;
10251 }
10252
10253 per_index = (word >> 24) & 0x7f;
10254 printf (_(" Compact model index: %d\n"), per_index);
10255 if (per_index == 0)
10256 {
10257 more_words = 0;
10258 word <<= 8;
10259 remaining--;
10260 }
10261 else if (per_index < 3)
10262 {
10263 more_words = (word >> 16) & 0xff;
10264 word <<= 16;
10265 remaining -= 2;
10266 }
10267 }
10268
10269 switch (filedata->file_header.e_machine)
10270 {
10271 case EM_ARM:
10272 if (per_index < 3)
10273 {
10274 if (! decode_arm_unwind_bytecode (filedata, aux, word, remaining, more_words,
10275 data_offset, data_sec, data_arm_sec))
10276 res = false;
10277 }
10278 else
10279 {
10280 warn (_("Unknown ARM compact model index encountered\n"));
10281 printf (_(" [reserved]\n"));
10282 res = false;
10283 }
10284 break;
10285
10286 case EM_TI_C6000:
10287 if (per_index < 3)
10288 {
10289 if (! decode_tic6x_unwind_bytecode (filedata, aux, word, remaining, more_words,
10290 data_offset, data_sec, data_arm_sec))
10291 res = false;
10292 }
10293 else if (per_index < 5)
10294 {
10295 if (((word >> 17) & 0x7f) == 0x7f)
10296 printf (_(" Restore stack from frame pointer\n"));
10297 else
10298 printf (_(" Stack increment %d\n"), (word >> 14) & 0x1fc);
10299 printf (_(" Registers restored: "));
10300 if (per_index == 4)
10301 printf (" (compact) ");
10302 decode_tic6x_unwind_regmask ((word >> 4) & 0x1fff);
10303 putchar ('\n');
10304 printf (_(" Return register: %s\n"),
10305 tic6x_unwind_regnames[word & 0xf]);
10306 }
10307 else
10308 printf (_(" [reserved (%d)]\n"), per_index);
10309 break;
10310
10311 default:
10312 error (_("Unsupported architecture type %d encountered when decoding unwind table\n"),
10313 filedata->file_header.e_machine);
10314 res = false;
10315 }
10316
10317 /* Decode the descriptors. Not implemented. */
10318
10319 return res;
10320 }
10321
10322 static bool
10323 dump_arm_unwind (Filedata * filedata,
10324 struct arm_unw_aux_info * aux,
10325 Elf_Internal_Shdr * exidx_sec)
10326 {
10327 struct arm_section exidx_arm_sec, extab_arm_sec;
10328 unsigned int i, exidx_len;
10329 unsigned long j, nfuns;
10330 bool res = true;
10331
10332 memset (&exidx_arm_sec, 0, sizeof (exidx_arm_sec));
10333 memset (&extab_arm_sec, 0, sizeof (extab_arm_sec));
10334 exidx_len = exidx_sec->sh_size / 8;
10335
10336 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
10337 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
10338 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
10339 aux->funtab[nfuns++] = aux->symtab[j];
10340 aux->nfuns = nfuns;
10341 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
10342
10343 for (i = 0; i < exidx_len; i++)
10344 {
10345 unsigned int exidx_fn, exidx_entry;
10346 struct absaddr fn_addr, entry_addr;
10347 uint64_t fn;
10348
10349 fputc ('\n', stdout);
10350
10351 if (! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
10352 8 * i, & exidx_fn, & fn_addr, NULL)
10353 || ! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
10354 8 * i + 4, & exidx_entry, & entry_addr, NULL))
10355 {
10356 free (aux->funtab);
10357 arm_free_section (& exidx_arm_sec);
10358 arm_free_section (& extab_arm_sec);
10359 return false;
10360 }
10361
10362 /* ARM EHABI, Section 5:
10363 An index table entry consists of 2 words.
10364 The first word contains a prel31 offset to the start of a function, with bit 31 clear. */
10365 if (exidx_fn & 0x80000000)
10366 {
10367 warn (_("corrupt index table entry: %x\n"), exidx_fn);
10368 res = false;
10369 }
10370
10371 fn = arm_expand_prel31 (filedata, exidx_fn, exidx_sec->sh_addr + 8 * i);
10372
10373 arm_print_vma_and_name (filedata, aux, fn, fn_addr);
10374 fputs (": ", stdout);
10375
10376 if (exidx_entry == 1)
10377 {
10378 print_vma (exidx_entry, PREFIX_HEX);
10379 fputs (" [cantunwind]\n", stdout);
10380 }
10381 else if (exidx_entry & 0x80000000)
10382 {
10383 print_vma (exidx_entry, PREFIX_HEX);
10384 fputc ('\n', stdout);
10385 decode_arm_unwind (filedata, aux, exidx_entry, 4, 0, NULL, NULL);
10386 }
10387 else
10388 {
10389 uint64_t table, table_offset = 0;
10390 Elf_Internal_Shdr *table_sec;
10391
10392 fputs ("@", stdout);
10393 table = arm_expand_prel31 (filedata, exidx_entry, exidx_sec->sh_addr + 8 * i + 4);
10394 print_vma (table, PREFIX_HEX);
10395 printf ("\n");
10396
10397 /* Locate the matching .ARM.extab. */
10398 if (entry_addr.section != SHN_UNDEF
10399 && entry_addr.section < filedata->file_header.e_shnum)
10400 {
10401 table_sec = filedata->section_headers + entry_addr.section;
10402 table_offset = entry_addr.offset;
10403 /* PR 18879 */
10404 if (table_offset > table_sec->sh_size)
10405 {
10406 warn (_("Unwind entry contains corrupt offset (0x%lx) into section %s\n"),
10407 (unsigned long) table_offset,
10408 printable_section_name (filedata, table_sec));
10409 res = false;
10410 continue;
10411 }
10412 }
10413 else
10414 {
10415 table_sec = find_section_by_address (filedata, table);
10416 if (table_sec != NULL)
10417 table_offset = table - table_sec->sh_addr;
10418 }
10419
10420 if (table_sec == NULL)
10421 {
10422 warn (_("Could not locate .ARM.extab section containing 0x%lx.\n"),
10423 (unsigned long) table);
10424 res = false;
10425 continue;
10426 }
10427
10428 if (! decode_arm_unwind (filedata, aux, 0, 0, table_offset, table_sec,
10429 &extab_arm_sec))
10430 res = false;
10431 }
10432 }
10433
10434 printf ("\n");
10435
10436 free (aux->funtab);
10437 arm_free_section (&exidx_arm_sec);
10438 arm_free_section (&extab_arm_sec);
10439
10440 return res;
10441 }
10442
10443 /* Used for both ARM and C6X unwinding tables. */
10444
10445 static bool
10446 arm_process_unwind (Filedata * filedata)
10447 {
10448 struct arm_unw_aux_info aux;
10449 Elf_Internal_Shdr *unwsec = NULL;
10450 Elf_Internal_Shdr *sec;
10451 unsigned long i;
10452 unsigned int sec_type;
10453 bool res = true;
10454
10455 switch (filedata->file_header.e_machine)
10456 {
10457 case EM_ARM:
10458 sec_type = SHT_ARM_EXIDX;
10459 break;
10460
10461 case EM_TI_C6000:
10462 sec_type = SHT_C6000_UNWIND;
10463 break;
10464
10465 default:
10466 error (_("Unsupported architecture type %d encountered when processing unwind table\n"),
10467 filedata->file_header.e_machine);
10468 return false;
10469 }
10470
10471 if (filedata->string_table == NULL)
10472 return false;
10473
10474 memset (& aux, 0, sizeof (aux));
10475 aux.filedata = filedata;
10476
10477 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
10478 {
10479 if (sec->sh_type == SHT_SYMTAB)
10480 {
10481 if (aux.symtab)
10482 {
10483 error (_("Multiple symbol tables encountered\n"));
10484 free (aux.symtab);
10485 aux.symtab = NULL;
10486 free (aux.strtab);
10487 aux.strtab = NULL;
10488 }
10489 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
10490 &aux.strtab, &aux.strtab_size))
10491 return false;
10492 }
10493 else if (sec->sh_type == sec_type)
10494 unwsec = sec;
10495 }
10496
10497 if (unwsec == NULL)
10498 printf (_("\nThere are no unwind sections in this file.\n"));
10499 else
10500 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
10501 {
10502 if (sec->sh_type == sec_type)
10503 {
10504 unsigned long num_unwind = sec->sh_size / (2 * eh_addr_size);
10505 printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
10506 "contains %lu entry:\n",
10507 "\nUnwind section '%s' at offset 0x%lx "
10508 "contains %lu entries:\n",
10509 num_unwind),
10510 printable_section_name (filedata, sec),
10511 (unsigned long) sec->sh_offset,
10512 num_unwind);
10513
10514 if (! dump_arm_unwind (filedata, &aux, sec))
10515 res = false;
10516 }
10517 }
10518
10519 free (aux.symtab);
10520 free ((char *) aux.strtab);
10521
10522 return res;
10523 }
10524
10525 static bool
10526 no_processor_specific_unwind (Filedata * filedata ATTRIBUTE_UNUSED)
10527 {
10528 printf (_("No processor specific unwind information to decode\n"));
10529 return true;
10530 }
10531
10532 static bool
10533 process_unwind (Filedata * filedata)
10534 {
10535 struct unwind_handler
10536 {
10537 unsigned int machtype;
10538 bool (* handler)(Filedata *);
10539 } handlers[] =
10540 {
10541 { EM_ARM, arm_process_unwind },
10542 { EM_IA_64, ia64_process_unwind },
10543 { EM_PARISC, hppa_process_unwind },
10544 { EM_TI_C6000, arm_process_unwind },
10545 { EM_386, no_processor_specific_unwind },
10546 { EM_X86_64, no_processor_specific_unwind },
10547 { 0, NULL }
10548 };
10549 int i;
10550
10551 if (!do_unwind)
10552 return true;
10553
10554 for (i = 0; handlers[i].handler != NULL; i++)
10555 if (filedata->file_header.e_machine == handlers[i].machtype)
10556 return handlers[i].handler (filedata);
10557
10558 printf (_("\nThe decoding of unwind sections for machine type %s is not currently supported.\n"),
10559 get_machine_name (filedata->file_header.e_machine));
10560 return true;
10561 }
10562
10563 static void
10564 dynamic_section_aarch64_val (Elf_Internal_Dyn * entry)
10565 {
10566 switch (entry->d_tag)
10567 {
10568 case DT_AARCH64_BTI_PLT:
10569 case DT_AARCH64_PAC_PLT:
10570 break;
10571 default:
10572 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10573 break;
10574 }
10575 putchar ('\n');
10576 }
10577
10578 static void
10579 dynamic_section_mips_val (Filedata * filedata, Elf_Internal_Dyn * entry)
10580 {
10581 switch (entry->d_tag)
10582 {
10583 case DT_MIPS_FLAGS:
10584 if (entry->d_un.d_val == 0)
10585 printf (_("NONE"));
10586 else
10587 {
10588 static const char * opts[] =
10589 {
10590 "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
10591 "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
10592 "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
10593 "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
10594 "RLD_ORDER_SAFE"
10595 };
10596 unsigned int cnt;
10597 bool first = true;
10598
10599 for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
10600 if (entry->d_un.d_val & (1 << cnt))
10601 {
10602 printf ("%s%s", first ? "" : " ", opts[cnt]);
10603 first = false;
10604 }
10605 }
10606 break;
10607
10608 case DT_MIPS_IVERSION:
10609 if (valid_dynamic_name (filedata, entry->d_un.d_val))
10610 printf (_("Interface Version: %s"),
10611 get_dynamic_name (filedata, entry->d_un.d_val));
10612 else
10613 printf (_("Interface Version: <corrupt: %" PRIx64 ">"),
10614 entry->d_un.d_ptr);
10615 break;
10616
10617 case DT_MIPS_TIME_STAMP:
10618 {
10619 char timebuf[128];
10620 struct tm * tmp;
10621 time_t atime = entry->d_un.d_val;
10622
10623 tmp = gmtime (&atime);
10624 /* PR 17531: file: 6accc532. */
10625 if (tmp == NULL)
10626 snprintf (timebuf, sizeof (timebuf), _("<corrupt>"));
10627 else
10628 snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
10629 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
10630 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
10631 printf (_("Time Stamp: %s"), timebuf);
10632 }
10633 break;
10634
10635 case DT_MIPS_RLD_VERSION:
10636 case DT_MIPS_LOCAL_GOTNO:
10637 case DT_MIPS_CONFLICTNO:
10638 case DT_MIPS_LIBLISTNO:
10639 case DT_MIPS_SYMTABNO:
10640 case DT_MIPS_UNREFEXTNO:
10641 case DT_MIPS_HIPAGENO:
10642 case DT_MIPS_DELTA_CLASS_NO:
10643 case DT_MIPS_DELTA_INSTANCE_NO:
10644 case DT_MIPS_DELTA_RELOC_NO:
10645 case DT_MIPS_DELTA_SYM_NO:
10646 case DT_MIPS_DELTA_CLASSSYM_NO:
10647 case DT_MIPS_COMPACT_SIZE:
10648 print_vma (entry->d_un.d_val, DEC);
10649 break;
10650
10651 case DT_MIPS_XHASH:
10652 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
10653 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10654 /* Falls through. */
10655
10656 default:
10657 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10658 }
10659 putchar ('\n');
10660 }
10661
10662 static void
10663 dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
10664 {
10665 switch (entry->d_tag)
10666 {
10667 case DT_HP_DLD_FLAGS:
10668 {
10669 static struct
10670 {
10671 long int bit;
10672 const char * str;
10673 }
10674 flags[] =
10675 {
10676 { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
10677 { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
10678 { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
10679 { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
10680 { DT_HP_BIND_NOW, "HP_BIND_NOW" },
10681 { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
10682 { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
10683 { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
10684 { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
10685 { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
10686 { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
10687 { DT_HP_GST, "HP_GST" },
10688 { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
10689 { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
10690 { DT_HP_NODELETE, "HP_NODELETE" },
10691 { DT_HP_GROUP, "HP_GROUP" },
10692 { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
10693 };
10694 bool first = true;
10695 size_t cnt;
10696 uint64_t val = entry->d_un.d_val;
10697
10698 for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
10699 if (val & flags[cnt].bit)
10700 {
10701 if (! first)
10702 putchar (' ');
10703 fputs (flags[cnt].str, stdout);
10704 first = false;
10705 val ^= flags[cnt].bit;
10706 }
10707
10708 if (val != 0 || first)
10709 {
10710 if (! first)
10711 putchar (' ');
10712 print_vma (val, HEX);
10713 }
10714 }
10715 break;
10716
10717 default:
10718 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10719 break;
10720 }
10721 putchar ('\n');
10722 }
10723
10724 /* VMS vs Unix time offset and factor. */
10725
10726 #define VMS_EPOCH_OFFSET 35067168000000000LL
10727 #define VMS_GRANULARITY_FACTOR 10000000
10728 #ifndef INT64_MIN
10729 #define INT64_MIN (-9223372036854775807LL - 1)
10730 #endif
10731
10732 /* Display a VMS time in a human readable format. */
10733
10734 static void
10735 print_vms_time (int64_t vmstime)
10736 {
10737 struct tm *tm = NULL;
10738 time_t unxtime;
10739
10740 if (vmstime >= INT64_MIN + VMS_EPOCH_OFFSET)
10741 {
10742 vmstime = (vmstime - VMS_EPOCH_OFFSET) / VMS_GRANULARITY_FACTOR;
10743 unxtime = vmstime;
10744 if (unxtime == vmstime)
10745 tm = gmtime (&unxtime);
10746 }
10747 if (tm != NULL)
10748 printf ("%04u-%02u-%02uT%02u:%02u:%02u",
10749 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
10750 tm->tm_hour, tm->tm_min, tm->tm_sec);
10751 }
10752
10753 static void
10754 dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
10755 {
10756 switch (entry->d_tag)
10757 {
10758 case DT_IA_64_PLT_RESERVE:
10759 /* First 3 slots reserved. */
10760 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10761 printf (" -- ");
10762 print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
10763 break;
10764
10765 case DT_IA_64_VMS_LINKTIME:
10766 print_vms_time (entry->d_un.d_val);
10767 break;
10768
10769 case DT_IA_64_VMS_LNKFLAGS:
10770 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10771 if (entry->d_un.d_val & VMS_LF_CALL_DEBUG)
10772 printf (" CALL_DEBUG");
10773 if (entry->d_un.d_val & VMS_LF_NOP0BUFS)
10774 printf (" NOP0BUFS");
10775 if (entry->d_un.d_val & VMS_LF_P0IMAGE)
10776 printf (" P0IMAGE");
10777 if (entry->d_un.d_val & VMS_LF_MKTHREADS)
10778 printf (" MKTHREADS");
10779 if (entry->d_un.d_val & VMS_LF_UPCALLS)
10780 printf (" UPCALLS");
10781 if (entry->d_un.d_val & VMS_LF_IMGSTA)
10782 printf (" IMGSTA");
10783 if (entry->d_un.d_val & VMS_LF_INITIALIZE)
10784 printf (" INITIALIZE");
10785 if (entry->d_un.d_val & VMS_LF_MAIN)
10786 printf (" MAIN");
10787 if (entry->d_un.d_val & VMS_LF_EXE_INIT)
10788 printf (" EXE_INIT");
10789 if (entry->d_un.d_val & VMS_LF_TBK_IN_IMG)
10790 printf (" TBK_IN_IMG");
10791 if (entry->d_un.d_val & VMS_LF_DBG_IN_IMG)
10792 printf (" DBG_IN_IMG");
10793 if (entry->d_un.d_val & VMS_LF_TBK_IN_DSF)
10794 printf (" TBK_IN_DSF");
10795 if (entry->d_un.d_val & VMS_LF_DBG_IN_DSF)
10796 printf (" DBG_IN_DSF");
10797 if (entry->d_un.d_val & VMS_LF_SIGNATURES)
10798 printf (" SIGNATURES");
10799 if (entry->d_un.d_val & VMS_LF_REL_SEG_OFF)
10800 printf (" REL_SEG_OFF");
10801 break;
10802
10803 default:
10804 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10805 break;
10806 }
10807 putchar ('\n');
10808 }
10809
10810 static bool
10811 get_32bit_dynamic_section (Filedata * filedata)
10812 {
10813 Elf32_External_Dyn * edyn;
10814 Elf32_External_Dyn * ext;
10815 Elf_Internal_Dyn * entry;
10816
10817 edyn = (Elf32_External_Dyn *) get_data (NULL, filedata,
10818 filedata->dynamic_addr, 1,
10819 filedata->dynamic_size,
10820 _("dynamic section"));
10821 if (!edyn)
10822 return false;
10823
10824 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
10825 might not have the luxury of section headers. Look for the DT_NULL
10826 terminator to determine the number of entries. */
10827 for (ext = edyn, filedata->dynamic_nent = 0;
10828 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
10829 ext++)
10830 {
10831 filedata->dynamic_nent++;
10832 if (BYTE_GET (ext->d_tag) == DT_NULL)
10833 break;
10834 }
10835
10836 filedata->dynamic_section
10837 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
10838 if (filedata->dynamic_section == NULL)
10839 {
10840 error (_("Out of memory allocating space for %lu dynamic entries\n"),
10841 (unsigned long) filedata->dynamic_nent);
10842 free (edyn);
10843 return false;
10844 }
10845
10846 for (ext = edyn, entry = filedata->dynamic_section;
10847 entry < filedata->dynamic_section + filedata->dynamic_nent;
10848 ext++, entry++)
10849 {
10850 entry->d_tag = BYTE_GET (ext->d_tag);
10851 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
10852 }
10853
10854 free (edyn);
10855
10856 return true;
10857 }
10858
10859 static bool
10860 get_64bit_dynamic_section (Filedata * filedata)
10861 {
10862 Elf64_External_Dyn * edyn;
10863 Elf64_External_Dyn * ext;
10864 Elf_Internal_Dyn * entry;
10865
10866 /* Read in the data. */
10867 edyn = (Elf64_External_Dyn *) get_data (NULL, filedata,
10868 filedata->dynamic_addr, 1,
10869 filedata->dynamic_size,
10870 _("dynamic section"));
10871 if (!edyn)
10872 return false;
10873
10874 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
10875 might not have the luxury of section headers. Look for the DT_NULL
10876 terminator to determine the number of entries. */
10877 for (ext = edyn, filedata->dynamic_nent = 0;
10878 /* PR 17533 file: 033-67080-0.004 - do not read past end of buffer. */
10879 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
10880 ext++)
10881 {
10882 filedata->dynamic_nent++;
10883 if (BYTE_GET (ext->d_tag) == DT_NULL)
10884 break;
10885 }
10886
10887 filedata->dynamic_section
10888 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
10889 if (filedata->dynamic_section == NULL)
10890 {
10891 error (_("Out of memory allocating space for %lu dynamic entries\n"),
10892 (unsigned long) filedata->dynamic_nent);
10893 free (edyn);
10894 return false;
10895 }
10896
10897 /* Convert from external to internal formats. */
10898 for (ext = edyn, entry = filedata->dynamic_section;
10899 entry < filedata->dynamic_section + filedata->dynamic_nent;
10900 ext++, entry++)
10901 {
10902 entry->d_tag = BYTE_GET (ext->d_tag);
10903 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
10904 }
10905
10906 free (edyn);
10907
10908 return true;
10909 }
10910
10911 static bool
10912 get_dynamic_section (Filedata *filedata)
10913 {
10914 if (filedata->dynamic_section)
10915 return true;
10916
10917 if (is_32bit_elf)
10918 return get_32bit_dynamic_section (filedata);
10919 else
10920 return get_64bit_dynamic_section (filedata);
10921 }
10922
10923 static void
10924 print_dynamic_flags (uint64_t flags)
10925 {
10926 bool first = true;
10927
10928 while (flags)
10929 {
10930 uint64_t flag;
10931
10932 flag = flags & - flags;
10933 flags &= ~ flag;
10934
10935 if (first)
10936 first = false;
10937 else
10938 putc (' ', stdout);
10939
10940 switch (flag)
10941 {
10942 case DF_ORIGIN: fputs ("ORIGIN", stdout); break;
10943 case DF_SYMBOLIC: fputs ("SYMBOLIC", stdout); break;
10944 case DF_TEXTREL: fputs ("TEXTREL", stdout); break;
10945 case DF_BIND_NOW: fputs ("BIND_NOW", stdout); break;
10946 case DF_STATIC_TLS: fputs ("STATIC_TLS", stdout); break;
10947 default: fputs (_("unknown"), stdout); break;
10948 }
10949 }
10950 puts ("");
10951 }
10952
10953 static uint64_t *
10954 get_dynamic_data (Filedata * filedata, uint64_t number, unsigned int ent_size)
10955 {
10956 unsigned char * e_data;
10957 uint64_t * i_data;
10958
10959 /* If size_t is smaller than uint64_t, eg because you are building
10960 on a 32-bit host, then make sure that when number is cast to
10961 size_t no information is lost. */
10962 if ((size_t) number != number
10963 || ent_size * number / ent_size != number)
10964 {
10965 error (_("Size overflow prevents reading %" PRIu64
10966 " elements of size %u\n"),
10967 number, ent_size);
10968 return NULL;
10969 }
10970
10971 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
10972 attempting to allocate memory when the read is bound to fail. */
10973 if (ent_size * number > filedata->file_size)
10974 {
10975 error (_("Invalid number of dynamic entries: %" PRIu64 "\n"),
10976 number);
10977 return NULL;
10978 }
10979
10980 e_data = (unsigned char *) cmalloc ((size_t) number, ent_size);
10981 if (e_data == NULL)
10982 {
10983 error (_("Out of memory reading %" PRIu64 " dynamic entries\n"),
10984 number);
10985 return NULL;
10986 }
10987
10988 if (fread (e_data, ent_size, (size_t) number, filedata->handle) != number)
10989 {
10990 error (_("Unable to read in %" PRIu64 " bytes of dynamic data\n"),
10991 number * ent_size);
10992 free (e_data);
10993 return NULL;
10994 }
10995
10996 i_data = (uint64_t *) cmalloc ((size_t) number, sizeof (*i_data));
10997 if (i_data == NULL)
10998 {
10999 error (_("Out of memory allocating space for %" PRIu64 " dynamic entries\n"),
11000 number);
11001 free (e_data);
11002 return NULL;
11003 }
11004
11005 while (number--)
11006 i_data[number] = byte_get (e_data + number * ent_size, ent_size);
11007
11008 free (e_data);
11009
11010 return i_data;
11011 }
11012
11013 static unsigned long
11014 get_num_dynamic_syms (Filedata * filedata)
11015 {
11016 unsigned long num_of_syms = 0;
11017
11018 if (!do_histogram && (!do_using_dynamic || do_dyn_syms))
11019 return num_of_syms;
11020
11021 if (filedata->dynamic_info[DT_HASH])
11022 {
11023 unsigned char nb[8];
11024 unsigned char nc[8];
11025 unsigned int hash_ent_size = 4;
11026
11027 if ((filedata->file_header.e_machine == EM_ALPHA
11028 || filedata->file_header.e_machine == EM_S390
11029 || filedata->file_header.e_machine == EM_S390_OLD)
11030 && filedata->file_header.e_ident[EI_CLASS] == ELFCLASS64)
11031 hash_ent_size = 8;
11032
11033 if (fseek (filedata->handle,
11034 (filedata->archive_file_offset
11035 + offset_from_vma (filedata, filedata->dynamic_info[DT_HASH],
11036 sizeof nb + sizeof nc)),
11037 SEEK_SET))
11038 {
11039 error (_("Unable to seek to start of dynamic information\n"));
11040 goto no_hash;
11041 }
11042
11043 if (fread (nb, hash_ent_size, 1, filedata->handle) != 1)
11044 {
11045 error (_("Failed to read in number of buckets\n"));
11046 goto no_hash;
11047 }
11048
11049 if (fread (nc, hash_ent_size, 1, filedata->handle) != 1)
11050 {
11051 error (_("Failed to read in number of chains\n"));
11052 goto no_hash;
11053 }
11054
11055 filedata->nbuckets = byte_get (nb, hash_ent_size);
11056 filedata->nchains = byte_get (nc, hash_ent_size);
11057
11058 if (filedata->nbuckets != 0 && filedata->nchains != 0)
11059 {
11060 filedata->buckets = get_dynamic_data (filedata, filedata->nbuckets,
11061 hash_ent_size);
11062 filedata->chains = get_dynamic_data (filedata, filedata->nchains,
11063 hash_ent_size);
11064
11065 if (filedata->buckets != NULL && filedata->chains != NULL)
11066 num_of_syms = filedata->nchains;
11067 }
11068 no_hash:
11069 if (num_of_syms == 0)
11070 {
11071 free (filedata->buckets);
11072 filedata->buckets = NULL;
11073 free (filedata->chains);
11074 filedata->chains = NULL;
11075 filedata->nbuckets = 0;
11076 }
11077 }
11078
11079 if (filedata->dynamic_info_DT_GNU_HASH)
11080 {
11081 unsigned char nb[16];
11082 uint64_t i, maxchain = 0xffffffff, bitmaskwords;
11083 uint64_t buckets_vma;
11084 unsigned long hn;
11085
11086 if (fseek (filedata->handle,
11087 (filedata->archive_file_offset
11088 + offset_from_vma (filedata,
11089 filedata->dynamic_info_DT_GNU_HASH,
11090 sizeof nb)),
11091 SEEK_SET))
11092 {
11093 error (_("Unable to seek to start of dynamic information\n"));
11094 goto no_gnu_hash;
11095 }
11096
11097 if (fread (nb, 16, 1, filedata->handle) != 1)
11098 {
11099 error (_("Failed to read in number of buckets\n"));
11100 goto no_gnu_hash;
11101 }
11102
11103 filedata->ngnubuckets = byte_get (nb, 4);
11104 filedata->gnusymidx = byte_get (nb + 4, 4);
11105 bitmaskwords = byte_get (nb + 8, 4);
11106 buckets_vma = filedata->dynamic_info_DT_GNU_HASH + 16;
11107 if (is_32bit_elf)
11108 buckets_vma += bitmaskwords * 4;
11109 else
11110 buckets_vma += bitmaskwords * 8;
11111
11112 if (fseek (filedata->handle,
11113 (filedata->archive_file_offset
11114 + offset_from_vma (filedata, buckets_vma, 4)),
11115 SEEK_SET))
11116 {
11117 error (_("Unable to seek to start of dynamic information\n"));
11118 goto no_gnu_hash;
11119 }
11120
11121 filedata->gnubuckets
11122 = get_dynamic_data (filedata, filedata->ngnubuckets, 4);
11123
11124 if (filedata->gnubuckets == NULL)
11125 goto no_gnu_hash;
11126
11127 for (i = 0; i < filedata->ngnubuckets; i++)
11128 if (filedata->gnubuckets[i] != 0)
11129 {
11130 if (filedata->gnubuckets[i] < filedata->gnusymidx)
11131 goto no_gnu_hash;
11132
11133 if (maxchain == 0xffffffff || filedata->gnubuckets[i] > maxchain)
11134 maxchain = filedata->gnubuckets[i];
11135 }
11136
11137 if (maxchain == 0xffffffff)
11138 goto no_gnu_hash;
11139
11140 maxchain -= filedata->gnusymidx;
11141
11142 if (fseek (filedata->handle,
11143 (filedata->archive_file_offset
11144 + offset_from_vma (filedata,
11145 buckets_vma + 4 * (filedata->ngnubuckets
11146 + maxchain),
11147 4)),
11148 SEEK_SET))
11149 {
11150 error (_("Unable to seek to start of dynamic information\n"));
11151 goto no_gnu_hash;
11152 }
11153
11154 do
11155 {
11156 if (fread (nb, 4, 1, filedata->handle) != 1)
11157 {
11158 error (_("Failed to determine last chain length\n"));
11159 goto no_gnu_hash;
11160 }
11161
11162 if (maxchain + 1 == 0)
11163 goto no_gnu_hash;
11164
11165 ++maxchain;
11166 }
11167 while ((byte_get (nb, 4) & 1) == 0);
11168
11169 if (fseek (filedata->handle,
11170 (filedata->archive_file_offset
11171 + offset_from_vma (filedata, (buckets_vma
11172 + 4 * filedata->ngnubuckets),
11173 4)),
11174 SEEK_SET))
11175 {
11176 error (_("Unable to seek to start of dynamic information\n"));
11177 goto no_gnu_hash;
11178 }
11179
11180 filedata->gnuchains = get_dynamic_data (filedata, maxchain, 4);
11181 filedata->ngnuchains = maxchain;
11182
11183 if (filedata->gnuchains == NULL)
11184 goto no_gnu_hash;
11185
11186 if (filedata->dynamic_info_DT_MIPS_XHASH)
11187 {
11188 if (fseek (filedata->handle,
11189 (filedata->archive_file_offset
11190 + offset_from_vma (filedata, (buckets_vma
11191 + 4 * (filedata->ngnubuckets
11192 + maxchain)), 4)),
11193 SEEK_SET))
11194 {
11195 error (_("Unable to seek to start of dynamic information\n"));
11196 goto no_gnu_hash;
11197 }
11198
11199 filedata->mipsxlat = get_dynamic_data (filedata, maxchain, 4);
11200 if (filedata->mipsxlat == NULL)
11201 goto no_gnu_hash;
11202 }
11203
11204 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
11205 if (filedata->gnubuckets[hn] != 0)
11206 {
11207 uint64_t si = filedata->gnubuckets[hn];
11208 uint64_t off = si - filedata->gnusymidx;
11209
11210 do
11211 {
11212 if (filedata->dynamic_info_DT_MIPS_XHASH)
11213 {
11214 if (off < filedata->ngnuchains
11215 && filedata->mipsxlat[off] >= num_of_syms)
11216 num_of_syms = filedata->mipsxlat[off] + 1;
11217 }
11218 else
11219 {
11220 if (si >= num_of_syms)
11221 num_of_syms = si + 1;
11222 }
11223 si++;
11224 }
11225 while (off < filedata->ngnuchains
11226 && (filedata->gnuchains[off++] & 1) == 0);
11227 }
11228
11229 if (num_of_syms == 0)
11230 {
11231 no_gnu_hash:
11232 free (filedata->mipsxlat);
11233 filedata->mipsxlat = NULL;
11234 free (filedata->gnuchains);
11235 filedata->gnuchains = NULL;
11236 free (filedata->gnubuckets);
11237 filedata->gnubuckets = NULL;
11238 filedata->ngnubuckets = 0;
11239 filedata->ngnuchains = 0;
11240 }
11241 }
11242
11243 return num_of_syms;
11244 }
11245
11246 /* Parse and display the contents of the dynamic section. */
11247
11248 static bool
11249 process_dynamic_section (Filedata * filedata)
11250 {
11251 Elf_Internal_Dyn * entry;
11252
11253 if (filedata->dynamic_size <= 1)
11254 {
11255 if (do_dynamic)
11256 {
11257 if (filedata->is_separate)
11258 printf (_("\nThere is no dynamic section in linked file '%s'.\n"),
11259 filedata->file_name);
11260 else
11261 printf (_("\nThere is no dynamic section in this file.\n"));
11262 }
11263
11264 return true;
11265 }
11266
11267 if (!get_dynamic_section (filedata))
11268 return false;
11269
11270 /* Find the appropriate symbol table. */
11271 if (filedata->dynamic_symbols == NULL || do_histogram)
11272 {
11273 unsigned long num_of_syms;
11274
11275 for (entry = filedata->dynamic_section;
11276 entry < filedata->dynamic_section + filedata->dynamic_nent;
11277 ++entry)
11278 if (entry->d_tag == DT_SYMTAB)
11279 filedata->dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
11280 else if (entry->d_tag == DT_SYMENT)
11281 filedata->dynamic_info[DT_SYMENT] = entry->d_un.d_val;
11282 else if (entry->d_tag == DT_HASH)
11283 filedata->dynamic_info[DT_HASH] = entry->d_un.d_val;
11284 else if (entry->d_tag == DT_GNU_HASH)
11285 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11286 else if ((filedata->file_header.e_machine == EM_MIPS
11287 || filedata->file_header.e_machine == EM_MIPS_RS3_LE)
11288 && entry->d_tag == DT_MIPS_XHASH)
11289 {
11290 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
11291 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11292 }
11293
11294 num_of_syms = get_num_dynamic_syms (filedata);
11295
11296 if (num_of_syms != 0
11297 && filedata->dynamic_symbols == NULL
11298 && filedata->dynamic_info[DT_SYMTAB]
11299 && filedata->dynamic_info[DT_SYMENT])
11300 {
11301 Elf_Internal_Phdr *seg;
11302 uint64_t vma = filedata->dynamic_info[DT_SYMTAB];
11303
11304 if (! get_program_headers (filedata))
11305 {
11306 error (_("Cannot interpret virtual addresses "
11307 "without program headers.\n"));
11308 return false;
11309 }
11310
11311 for (seg = filedata->program_headers;
11312 seg < filedata->program_headers + filedata->file_header.e_phnum;
11313 ++seg)
11314 {
11315 if (seg->p_type != PT_LOAD)
11316 continue;
11317
11318 if (seg->p_offset + seg->p_filesz > filedata->file_size)
11319 {
11320 /* See PR 21379 for a reproducer. */
11321 error (_("Invalid PT_LOAD entry\n"));
11322 return false;
11323 }
11324
11325 if (vma >= (seg->p_vaddr & -seg->p_align)
11326 && vma < seg->p_vaddr + seg->p_filesz)
11327 {
11328 /* Since we do not know how big the symbol table is,
11329 we default to reading in up to the end of PT_LOAD
11330 segment and processing that. This is overkill, I
11331 know, but it should work. */
11332 Elf_Internal_Shdr section;
11333 section.sh_offset = (vma - seg->p_vaddr
11334 + seg->p_offset);
11335 section.sh_size = (num_of_syms
11336 * filedata->dynamic_info[DT_SYMENT]);
11337 section.sh_entsize = filedata->dynamic_info[DT_SYMENT];
11338
11339 if (do_checks
11340 && filedata->dynamic_symtab_section != NULL
11341 && ((filedata->dynamic_symtab_section->sh_offset
11342 != section.sh_offset)
11343 || (filedata->dynamic_symtab_section->sh_size
11344 != section.sh_size)
11345 || (filedata->dynamic_symtab_section->sh_entsize
11346 != section.sh_entsize)))
11347 warn (_("\
11348 the .dynsym section doesn't match the DT_SYMTAB and DT_SYMENT tags\n"));
11349
11350 section.sh_name = filedata->string_table_length;
11351 filedata->dynamic_symbols
11352 = get_elf_symbols (filedata, &section,
11353 &filedata->num_dynamic_syms);
11354 if (filedata->dynamic_symbols == NULL
11355 || filedata->num_dynamic_syms != num_of_syms)
11356 {
11357 error (_("Corrupt DT_SYMTAB dynamic entry\n"));
11358 return false;
11359 }
11360 break;
11361 }
11362 }
11363 }
11364 }
11365
11366 /* Similarly find a string table. */
11367 if (filedata->dynamic_strings == NULL)
11368 for (entry = filedata->dynamic_section;
11369 entry < filedata->dynamic_section + filedata->dynamic_nent;
11370 ++entry)
11371 {
11372 if (entry->d_tag == DT_STRTAB)
11373 filedata->dynamic_info[DT_STRTAB] = entry->d_un.d_val;
11374
11375 if (entry->d_tag == DT_STRSZ)
11376 filedata->dynamic_info[DT_STRSZ] = entry->d_un.d_val;
11377
11378 if (filedata->dynamic_info[DT_STRTAB]
11379 && filedata->dynamic_info[DT_STRSZ])
11380 {
11381 unsigned long offset;
11382 uint64_t str_tab_len = filedata->dynamic_info[DT_STRSZ];
11383
11384 offset = offset_from_vma (filedata,
11385 filedata->dynamic_info[DT_STRTAB],
11386 str_tab_len);
11387 if (do_checks
11388 && filedata->dynamic_strtab_section
11389 && ((filedata->dynamic_strtab_section->sh_offset
11390 != (file_ptr) offset)
11391 || (filedata->dynamic_strtab_section->sh_size
11392 != str_tab_len)))
11393 warn (_("\
11394 the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
11395
11396 filedata->dynamic_strings
11397 = (char *) get_data (NULL, filedata, offset, 1, str_tab_len,
11398 _("dynamic string table"));
11399 if (filedata->dynamic_strings == NULL)
11400 {
11401 error (_("Corrupt DT_STRTAB dynamic entry\n"));
11402 break;
11403 }
11404
11405 filedata->dynamic_strings_length = str_tab_len;
11406 break;
11407 }
11408 }
11409
11410 /* And find the syminfo section if available. */
11411 if (filedata->dynamic_syminfo == NULL)
11412 {
11413 unsigned long syminsz = 0;
11414
11415 for (entry = filedata->dynamic_section;
11416 entry < filedata->dynamic_section + filedata->dynamic_nent;
11417 ++entry)
11418 {
11419 if (entry->d_tag == DT_SYMINENT)
11420 {
11421 /* Note: these braces are necessary to avoid a syntax
11422 error from the SunOS4 C compiler. */
11423 /* PR binutils/17531: A corrupt file can trigger this test.
11424 So do not use an assert, instead generate an error message. */
11425 if (sizeof (Elf_External_Syminfo) != entry->d_un.d_val)
11426 error (_("Bad value (%d) for SYMINENT entry\n"),
11427 (int) entry->d_un.d_val);
11428 }
11429 else if (entry->d_tag == DT_SYMINSZ)
11430 syminsz = entry->d_un.d_val;
11431 else if (entry->d_tag == DT_SYMINFO)
11432 filedata->dynamic_syminfo_offset
11433 = offset_from_vma (filedata, entry->d_un.d_val, syminsz);
11434 }
11435
11436 if (filedata->dynamic_syminfo_offset != 0 && syminsz != 0)
11437 {
11438 Elf_External_Syminfo * extsyminfo;
11439 Elf_External_Syminfo * extsym;
11440 Elf_Internal_Syminfo * syminfo;
11441
11442 /* There is a syminfo section. Read the data. */
11443 extsyminfo = (Elf_External_Syminfo *)
11444 get_data (NULL, filedata, filedata->dynamic_syminfo_offset,
11445 1, syminsz, _("symbol information"));
11446 if (!extsyminfo)
11447 return false;
11448
11449 if (filedata->dynamic_syminfo != NULL)
11450 {
11451 error (_("Multiple dynamic symbol information sections found\n"));
11452 free (filedata->dynamic_syminfo);
11453 }
11454 filedata->dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
11455 if (filedata->dynamic_syminfo == NULL)
11456 {
11457 error (_("Out of memory allocating %lu bytes "
11458 "for dynamic symbol info\n"),
11459 (unsigned long) syminsz);
11460 return false;
11461 }
11462
11463 filedata->dynamic_syminfo_nent
11464 = syminsz / sizeof (Elf_External_Syminfo);
11465 for (syminfo = filedata->dynamic_syminfo, extsym = extsyminfo;
11466 syminfo < (filedata->dynamic_syminfo
11467 + filedata->dynamic_syminfo_nent);
11468 ++syminfo, ++extsym)
11469 {
11470 syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
11471 syminfo->si_flags = BYTE_GET (extsym->si_flags);
11472 }
11473
11474 free (extsyminfo);
11475 }
11476 }
11477
11478 if (do_dynamic && filedata->dynamic_addr)
11479 {
11480 if (filedata->is_separate)
11481 printf (ngettext ("\nIn linked file '%s' the dynamic section at offset 0x%lx contains %lu entry:\n",
11482 "\nIn linked file '%s' the dynamic section at offset 0x%lx contains %lu entries:\n",
11483 (unsigned long) filedata->dynamic_nent),
11484 filedata->file_name,
11485 filedata->dynamic_addr,
11486 (unsigned long) filedata->dynamic_nent);
11487 else
11488 printf (ngettext ("\nDynamic section at offset 0x%lx contains %lu entry:\n",
11489 "\nDynamic section at offset 0x%lx contains %lu entries:\n",
11490 (unsigned long) filedata->dynamic_nent),
11491 filedata->dynamic_addr,
11492 (unsigned long) filedata->dynamic_nent);
11493 }
11494 if (do_dynamic)
11495 printf (_(" Tag Type Name/Value\n"));
11496
11497 for (entry = filedata->dynamic_section;
11498 entry < filedata->dynamic_section + filedata->dynamic_nent;
11499 entry++)
11500 {
11501 if (do_dynamic)
11502 {
11503 const char * dtype;
11504
11505 putchar (' ');
11506 print_vma (entry->d_tag, FULL_HEX);
11507 dtype = get_dynamic_type (filedata, entry->d_tag);
11508 printf (" (%s)%*s", dtype,
11509 ((is_32bit_elf ? 27 : 19) - (int) strlen (dtype)), " ");
11510 }
11511
11512 switch (entry->d_tag)
11513 {
11514 case DT_FLAGS:
11515 if (do_dynamic)
11516 print_dynamic_flags (entry->d_un.d_val);
11517 break;
11518
11519 case DT_AUXILIARY:
11520 case DT_FILTER:
11521 case DT_CONFIG:
11522 case DT_DEPAUDIT:
11523 case DT_AUDIT:
11524 if (do_dynamic)
11525 {
11526 switch (entry->d_tag)
11527 {
11528 case DT_AUXILIARY:
11529 printf (_("Auxiliary library"));
11530 break;
11531
11532 case DT_FILTER:
11533 printf (_("Filter library"));
11534 break;
11535
11536 case DT_CONFIG:
11537 printf (_("Configuration file"));
11538 break;
11539
11540 case DT_DEPAUDIT:
11541 printf (_("Dependency audit library"));
11542 break;
11543
11544 case DT_AUDIT:
11545 printf (_("Audit library"));
11546 break;
11547 }
11548
11549 if (valid_dynamic_name (filedata, entry->d_un.d_val))
11550 printf (": [%s]\n",
11551 get_dynamic_name (filedata, entry->d_un.d_val));
11552 else
11553 {
11554 printf (": ");
11555 print_vma (entry->d_un.d_val, PREFIX_HEX);
11556 putchar ('\n');
11557 }
11558 }
11559 break;
11560
11561 case DT_FEATURE:
11562 if (do_dynamic)
11563 {
11564 printf (_("Flags:"));
11565
11566 if (entry->d_un.d_val == 0)
11567 printf (_(" None\n"));
11568 else
11569 {
11570 unsigned long int val = entry->d_un.d_val;
11571
11572 if (val & DTF_1_PARINIT)
11573 {
11574 printf (" PARINIT");
11575 val ^= DTF_1_PARINIT;
11576 }
11577 if (val & DTF_1_CONFEXP)
11578 {
11579 printf (" CONFEXP");
11580 val ^= DTF_1_CONFEXP;
11581 }
11582 if (val != 0)
11583 printf (" %lx", val);
11584 puts ("");
11585 }
11586 }
11587 break;
11588
11589 case DT_POSFLAG_1:
11590 if (do_dynamic)
11591 {
11592 printf (_("Flags:"));
11593
11594 if (entry->d_un.d_val == 0)
11595 printf (_(" None\n"));
11596 else
11597 {
11598 unsigned long int val = entry->d_un.d_val;
11599
11600 if (val & DF_P1_LAZYLOAD)
11601 {
11602 printf (" LAZYLOAD");
11603 val ^= DF_P1_LAZYLOAD;
11604 }
11605 if (val & DF_P1_GROUPPERM)
11606 {
11607 printf (" GROUPPERM");
11608 val ^= DF_P1_GROUPPERM;
11609 }
11610 if (val != 0)
11611 printf (" %lx", val);
11612 puts ("");
11613 }
11614 }
11615 break;
11616
11617 case DT_FLAGS_1:
11618 if (do_dynamic)
11619 {
11620 printf (_("Flags:"));
11621 if (entry->d_un.d_val == 0)
11622 printf (_(" None\n"));
11623 else
11624 {
11625 unsigned long int val = entry->d_un.d_val;
11626
11627 if (val & DF_1_NOW)
11628 {
11629 printf (" NOW");
11630 val ^= DF_1_NOW;
11631 }
11632 if (val & DF_1_GLOBAL)
11633 {
11634 printf (" GLOBAL");
11635 val ^= DF_1_GLOBAL;
11636 }
11637 if (val & DF_1_GROUP)
11638 {
11639 printf (" GROUP");
11640 val ^= DF_1_GROUP;
11641 }
11642 if (val & DF_1_NODELETE)
11643 {
11644 printf (" NODELETE");
11645 val ^= DF_1_NODELETE;
11646 }
11647 if (val & DF_1_LOADFLTR)
11648 {
11649 printf (" LOADFLTR");
11650 val ^= DF_1_LOADFLTR;
11651 }
11652 if (val & DF_1_INITFIRST)
11653 {
11654 printf (" INITFIRST");
11655 val ^= DF_1_INITFIRST;
11656 }
11657 if (val & DF_1_NOOPEN)
11658 {
11659 printf (" NOOPEN");
11660 val ^= DF_1_NOOPEN;
11661 }
11662 if (val & DF_1_ORIGIN)
11663 {
11664 printf (" ORIGIN");
11665 val ^= DF_1_ORIGIN;
11666 }
11667 if (val & DF_1_DIRECT)
11668 {
11669 printf (" DIRECT");
11670 val ^= DF_1_DIRECT;
11671 }
11672 if (val & DF_1_TRANS)
11673 {
11674 printf (" TRANS");
11675 val ^= DF_1_TRANS;
11676 }
11677 if (val & DF_1_INTERPOSE)
11678 {
11679 printf (" INTERPOSE");
11680 val ^= DF_1_INTERPOSE;
11681 }
11682 if (val & DF_1_NODEFLIB)
11683 {
11684 printf (" NODEFLIB");
11685 val ^= DF_1_NODEFLIB;
11686 }
11687 if (val & DF_1_NODUMP)
11688 {
11689 printf (" NODUMP");
11690 val ^= DF_1_NODUMP;
11691 }
11692 if (val & DF_1_CONFALT)
11693 {
11694 printf (" CONFALT");
11695 val ^= DF_1_CONFALT;
11696 }
11697 if (val & DF_1_ENDFILTEE)
11698 {
11699 printf (" ENDFILTEE");
11700 val ^= DF_1_ENDFILTEE;
11701 }
11702 if (val & DF_1_DISPRELDNE)
11703 {
11704 printf (" DISPRELDNE");
11705 val ^= DF_1_DISPRELDNE;
11706 }
11707 if (val & DF_1_DISPRELPND)
11708 {
11709 printf (" DISPRELPND");
11710 val ^= DF_1_DISPRELPND;
11711 }
11712 if (val & DF_1_NODIRECT)
11713 {
11714 printf (" NODIRECT");
11715 val ^= DF_1_NODIRECT;
11716 }
11717 if (val & DF_1_IGNMULDEF)
11718 {
11719 printf (" IGNMULDEF");
11720 val ^= DF_1_IGNMULDEF;
11721 }
11722 if (val & DF_1_NOKSYMS)
11723 {
11724 printf (" NOKSYMS");
11725 val ^= DF_1_NOKSYMS;
11726 }
11727 if (val & DF_1_NOHDR)
11728 {
11729 printf (" NOHDR");
11730 val ^= DF_1_NOHDR;
11731 }
11732 if (val & DF_1_EDITED)
11733 {
11734 printf (" EDITED");
11735 val ^= DF_1_EDITED;
11736 }
11737 if (val & DF_1_NORELOC)
11738 {
11739 printf (" NORELOC");
11740 val ^= DF_1_NORELOC;
11741 }
11742 if (val & DF_1_SYMINTPOSE)
11743 {
11744 printf (" SYMINTPOSE");
11745 val ^= DF_1_SYMINTPOSE;
11746 }
11747 if (val & DF_1_GLOBAUDIT)
11748 {
11749 printf (" GLOBAUDIT");
11750 val ^= DF_1_GLOBAUDIT;
11751 }
11752 if (val & DF_1_SINGLETON)
11753 {
11754 printf (" SINGLETON");
11755 val ^= DF_1_SINGLETON;
11756 }
11757 if (val & DF_1_STUB)
11758 {
11759 printf (" STUB");
11760 val ^= DF_1_STUB;
11761 }
11762 if (val & DF_1_PIE)
11763 {
11764 printf (" PIE");
11765 val ^= DF_1_PIE;
11766 }
11767 if (val & DF_1_KMOD)
11768 {
11769 printf (" KMOD");
11770 val ^= DF_1_KMOD;
11771 }
11772 if (val & DF_1_WEAKFILTER)
11773 {
11774 printf (" WEAKFILTER");
11775 val ^= DF_1_WEAKFILTER;
11776 }
11777 if (val & DF_1_NOCOMMON)
11778 {
11779 printf (" NOCOMMON");
11780 val ^= DF_1_NOCOMMON;
11781 }
11782 if (val != 0)
11783 printf (" %lx", val);
11784 puts ("");
11785 }
11786 }
11787 break;
11788
11789 case DT_PLTREL:
11790 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11791 if (do_dynamic)
11792 puts (get_dynamic_type (filedata, entry->d_un.d_val));
11793 break;
11794
11795 case DT_NULL :
11796 case DT_NEEDED :
11797 case DT_PLTGOT :
11798 case DT_HASH :
11799 case DT_STRTAB :
11800 case DT_SYMTAB :
11801 case DT_RELA :
11802 case DT_INIT :
11803 case DT_FINI :
11804 case DT_SONAME :
11805 case DT_RPATH :
11806 case DT_SYMBOLIC:
11807 case DT_REL :
11808 case DT_RELR :
11809 case DT_DEBUG :
11810 case DT_TEXTREL :
11811 case DT_JMPREL :
11812 case DT_RUNPATH :
11813 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11814
11815 if (do_dynamic)
11816 {
11817 const char *name;
11818
11819 if (valid_dynamic_name (filedata, entry->d_un.d_val))
11820 name = get_dynamic_name (filedata, entry->d_un.d_val);
11821 else
11822 name = NULL;
11823
11824 if (name)
11825 {
11826 switch (entry->d_tag)
11827 {
11828 case DT_NEEDED:
11829 printf (_("Shared library: [%s]"), name);
11830
11831 if (filedata->program_interpreter
11832 && streq (name, filedata->program_interpreter))
11833 printf (_(" program interpreter"));
11834 break;
11835
11836 case DT_SONAME:
11837 printf (_("Library soname: [%s]"), name);
11838 break;
11839
11840 case DT_RPATH:
11841 printf (_("Library rpath: [%s]"), name);
11842 break;
11843
11844 case DT_RUNPATH:
11845 printf (_("Library runpath: [%s]"), name);
11846 break;
11847
11848 default:
11849 print_vma (entry->d_un.d_val, PREFIX_HEX);
11850 break;
11851 }
11852 }
11853 else
11854 print_vma (entry->d_un.d_val, PREFIX_HEX);
11855
11856 putchar ('\n');
11857 }
11858 break;
11859
11860 case DT_PLTRELSZ:
11861 case DT_RELASZ :
11862 case DT_STRSZ :
11863 case DT_RELSZ :
11864 case DT_RELAENT :
11865 case DT_RELRENT :
11866 case DT_RELRSZ :
11867 case DT_SYMENT :
11868 case DT_RELENT :
11869 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11870 /* Fall through. */
11871 case DT_PLTPADSZ:
11872 case DT_MOVEENT :
11873 case DT_MOVESZ :
11874 case DT_PREINIT_ARRAYSZ:
11875 case DT_INIT_ARRAYSZ:
11876 case DT_FINI_ARRAYSZ:
11877 case DT_GNU_CONFLICTSZ:
11878 case DT_GNU_LIBLISTSZ:
11879 if (do_dynamic)
11880 {
11881 print_vma (entry->d_un.d_val, UNSIGNED);
11882 printf (_(" (bytes)\n"));
11883 }
11884 break;
11885
11886 case DT_VERDEFNUM:
11887 case DT_VERNEEDNUM:
11888 case DT_RELACOUNT:
11889 case DT_RELCOUNT:
11890 if (do_dynamic)
11891 {
11892 print_vma (entry->d_un.d_val, UNSIGNED);
11893 putchar ('\n');
11894 }
11895 break;
11896
11897 case DT_SYMINSZ:
11898 case DT_SYMINENT:
11899 case DT_SYMINFO:
11900 case DT_USED:
11901 case DT_INIT_ARRAY:
11902 case DT_FINI_ARRAY:
11903 if (do_dynamic)
11904 {
11905 if (entry->d_tag == DT_USED
11906 && valid_dynamic_name (filedata, entry->d_un.d_val))
11907 {
11908 const char *name
11909 = get_dynamic_name (filedata, entry->d_un.d_val);
11910
11911 if (*name)
11912 {
11913 printf (_("Not needed object: [%s]\n"), name);
11914 break;
11915 }
11916 }
11917
11918 print_vma (entry->d_un.d_val, PREFIX_HEX);
11919 putchar ('\n');
11920 }
11921 break;
11922
11923 case DT_BIND_NOW:
11924 /* The value of this entry is ignored. */
11925 if (do_dynamic)
11926 putchar ('\n');
11927 break;
11928
11929 case DT_GNU_PRELINKED:
11930 if (do_dynamic)
11931 {
11932 struct tm * tmp;
11933 time_t atime = entry->d_un.d_val;
11934
11935 tmp = gmtime (&atime);
11936 /* PR 17533 file: 041-1244816-0.004. */
11937 if (tmp == NULL)
11938 printf (_("<corrupt time val: %lx"),
11939 (unsigned long) atime);
11940 else
11941 printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
11942 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
11943 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
11944
11945 }
11946 break;
11947
11948 case DT_GNU_HASH:
11949 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11950 if (do_dynamic)
11951 {
11952 print_vma (entry->d_un.d_val, PREFIX_HEX);
11953 putchar ('\n');
11954 }
11955 break;
11956
11957 case DT_GNU_FLAGS_1:
11958 if (do_dynamic)
11959 {
11960 printf (_("Flags:"));
11961 if (entry->d_un.d_val == 0)
11962 printf (_(" None\n"));
11963 else
11964 {
11965 unsigned long int val = entry->d_un.d_val;
11966
11967 if (val & DF_GNU_1_UNIQUE)
11968 {
11969 printf (" UNIQUE");
11970 val ^= DF_GNU_1_UNIQUE;
11971 }
11972 if (val != 0)
11973 printf (" %lx", val);
11974 puts ("");
11975 }
11976 }
11977 break;
11978
11979 default:
11980 if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
11981 filedata->version_info[DT_VERSIONTAGIDX (entry->d_tag)]
11982 = entry->d_un.d_val;
11983
11984 if (do_dynamic)
11985 {
11986 switch (filedata->file_header.e_machine)
11987 {
11988 case EM_AARCH64:
11989 dynamic_section_aarch64_val (entry);
11990 break;
11991 case EM_MIPS:
11992 case EM_MIPS_RS3_LE:
11993 dynamic_section_mips_val (filedata, entry);
11994 break;
11995 case EM_PARISC:
11996 dynamic_section_parisc_val (entry);
11997 break;
11998 case EM_IA_64:
11999 dynamic_section_ia64_val (entry);
12000 break;
12001 default:
12002 print_vma (entry->d_un.d_val, PREFIX_HEX);
12003 putchar ('\n');
12004 }
12005 }
12006 break;
12007 }
12008 }
12009
12010 return true;
12011 }
12012
12013 static char *
12014 get_ver_flags (unsigned int flags)
12015 {
12016 static char buff[128];
12017
12018 buff[0] = 0;
12019
12020 if (flags == 0)
12021 return _("none");
12022
12023 if (flags & VER_FLG_BASE)
12024 strcat (buff, "BASE");
12025
12026 if (flags & VER_FLG_WEAK)
12027 {
12028 if (flags & VER_FLG_BASE)
12029 strcat (buff, " | ");
12030
12031 strcat (buff, "WEAK");
12032 }
12033
12034 if (flags & VER_FLG_INFO)
12035 {
12036 if (flags & (VER_FLG_BASE|VER_FLG_WEAK))
12037 strcat (buff, " | ");
12038
12039 strcat (buff, "INFO");
12040 }
12041
12042 if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
12043 {
12044 if (flags & (VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
12045 strcat (buff, " | ");
12046
12047 strcat (buff, _("<unknown>"));
12048 }
12049
12050 return buff;
12051 }
12052
12053 /* Display the contents of the version sections. */
12054
12055 static bool
12056 process_version_sections (Filedata * filedata)
12057 {
12058 Elf_Internal_Shdr * section;
12059 unsigned i;
12060 bool found = false;
12061
12062 if (! do_version)
12063 return true;
12064
12065 for (i = 0, section = filedata->section_headers;
12066 i < filedata->file_header.e_shnum;
12067 i++, section++)
12068 {
12069 switch (section->sh_type)
12070 {
12071 case SHT_GNU_verdef:
12072 {
12073 Elf_External_Verdef * edefs;
12074 unsigned long idx;
12075 unsigned long cnt;
12076 char * endbuf;
12077
12078 found = true;
12079
12080 if (filedata->is_separate)
12081 printf (ngettext ("\nIn linked file '%s' the version definition section '%s' contains %u entry:\n",
12082 "\nIn linked file '%s' the version definition section '%s' contains %u entries:\n",
12083 section->sh_info),
12084 filedata->file_name,
12085 printable_section_name (filedata, section),
12086 section->sh_info);
12087 else
12088 printf (ngettext ("\nVersion definition section '%s' "
12089 "contains %u entry:\n",
12090 "\nVersion definition section '%s' "
12091 "contains %u entries:\n",
12092 section->sh_info),
12093 printable_section_name (filedata, section),
12094 section->sh_info);
12095
12096 printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
12097 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
12098 (unsigned long) section->sh_offset, section->sh_link,
12099 printable_section_name_from_index (filedata, section->sh_link));
12100
12101 edefs = (Elf_External_Verdef *)
12102 get_data (NULL, filedata, section->sh_offset, 1,section->sh_size,
12103 _("version definition section"));
12104 if (!edefs)
12105 break;
12106 endbuf = (char *) edefs + section->sh_size;
12107
12108 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
12109 {
12110 char * vstart;
12111 Elf_External_Verdef * edef;
12112 Elf_Internal_Verdef ent;
12113 Elf_External_Verdaux * eaux;
12114 Elf_Internal_Verdaux aux;
12115 unsigned long isum;
12116 int j;
12117
12118 vstart = ((char *) edefs) + idx;
12119 if (vstart + sizeof (*edef) > endbuf)
12120 break;
12121
12122 edef = (Elf_External_Verdef *) vstart;
12123
12124 ent.vd_version = BYTE_GET (edef->vd_version);
12125 ent.vd_flags = BYTE_GET (edef->vd_flags);
12126 ent.vd_ndx = BYTE_GET (edef->vd_ndx);
12127 ent.vd_cnt = BYTE_GET (edef->vd_cnt);
12128 ent.vd_hash = BYTE_GET (edef->vd_hash);
12129 ent.vd_aux = BYTE_GET (edef->vd_aux);
12130 ent.vd_next = BYTE_GET (edef->vd_next);
12131
12132 printf (_(" %#06lx: Rev: %d Flags: %s"),
12133 idx, ent.vd_version, get_ver_flags (ent.vd_flags));
12134
12135 printf (_(" Index: %d Cnt: %d "),
12136 ent.vd_ndx, ent.vd_cnt);
12137
12138 /* Check for overflow. */
12139 if (ent.vd_aux > (size_t) (endbuf - vstart))
12140 break;
12141
12142 vstart += ent.vd_aux;
12143
12144 if (vstart + sizeof (*eaux) > endbuf)
12145 break;
12146 eaux = (Elf_External_Verdaux *) vstart;
12147
12148 aux.vda_name = BYTE_GET (eaux->vda_name);
12149 aux.vda_next = BYTE_GET (eaux->vda_next);
12150
12151 if (valid_dynamic_name (filedata, aux.vda_name))
12152 printf (_("Name: %s\n"),
12153 get_dynamic_name (filedata, aux.vda_name));
12154 else
12155 printf (_("Name index: %ld\n"), aux.vda_name);
12156
12157 isum = idx + ent.vd_aux;
12158
12159 for (j = 1; j < ent.vd_cnt; j++)
12160 {
12161 if (aux.vda_next < sizeof (*eaux)
12162 && !(j == ent.vd_cnt - 1 && aux.vda_next == 0))
12163 {
12164 warn (_("Invalid vda_next field of %lx\n"),
12165 aux.vda_next);
12166 j = ent.vd_cnt;
12167 break;
12168 }
12169 /* Check for overflow. */
12170 if (aux.vda_next > (size_t) (endbuf - vstart))
12171 break;
12172
12173 isum += aux.vda_next;
12174 vstart += aux.vda_next;
12175
12176 if (vstart + sizeof (*eaux) > endbuf)
12177 break;
12178 eaux = (Elf_External_Verdaux *) vstart;
12179
12180 aux.vda_name = BYTE_GET (eaux->vda_name);
12181 aux.vda_next = BYTE_GET (eaux->vda_next);
12182
12183 if (valid_dynamic_name (filedata, aux.vda_name))
12184 printf (_(" %#06lx: Parent %d: %s\n"),
12185 isum, j,
12186 get_dynamic_name (filedata, aux.vda_name));
12187 else
12188 printf (_(" %#06lx: Parent %d, name index: %ld\n"),
12189 isum, j, aux.vda_name);
12190 }
12191
12192 if (j < ent.vd_cnt)
12193 printf (_(" Version def aux past end of section\n"));
12194
12195 /* PR 17531:
12196 file: id:000001,src:000172+005151,op:splice,rep:2. */
12197 if (ent.vd_next < sizeof (*edef)
12198 && !(cnt == section->sh_info - 1 && ent.vd_next == 0))
12199 {
12200 warn (_("Invalid vd_next field of %lx\n"), ent.vd_next);
12201 cnt = section->sh_info;
12202 break;
12203 }
12204 if (ent.vd_next > (size_t) (endbuf - ((char *) edefs + idx)))
12205 break;
12206
12207 idx += ent.vd_next;
12208 }
12209
12210 if (cnt < section->sh_info)
12211 printf (_(" Version definition past end of section\n"));
12212
12213 free (edefs);
12214 }
12215 break;
12216
12217 case SHT_GNU_verneed:
12218 {
12219 Elf_External_Verneed * eneed;
12220 unsigned long idx;
12221 unsigned long cnt;
12222 char * endbuf;
12223
12224 found = true;
12225
12226 if (filedata->is_separate)
12227 printf (ngettext ("\nIn linked file '%s' the version needs section '%s' contains %u entry:\n",
12228 "\nIn linked file '%s' the version needs section '%s' contains %u entries:\n",
12229 section->sh_info),
12230 filedata->file_name,
12231 printable_section_name (filedata, section),
12232 section->sh_info);
12233 else
12234 printf (ngettext ("\nVersion needs section '%s' "
12235 "contains %u entry:\n",
12236 "\nVersion needs section '%s' "
12237 "contains %u entries:\n",
12238 section->sh_info),
12239 printable_section_name (filedata, section),
12240 section->sh_info);
12241
12242 printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
12243 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
12244 (unsigned long) section->sh_offset, section->sh_link,
12245 printable_section_name_from_index (filedata, section->sh_link));
12246
12247 eneed = (Elf_External_Verneed *) get_data (NULL, filedata,
12248 section->sh_offset, 1,
12249 section->sh_size,
12250 _("Version Needs section"));
12251 if (!eneed)
12252 break;
12253 endbuf = (char *) eneed + section->sh_size;
12254
12255 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
12256 {
12257 Elf_External_Verneed * entry;
12258 Elf_Internal_Verneed ent;
12259 unsigned long isum;
12260 int j;
12261 char * vstart;
12262
12263 vstart = ((char *) eneed) + idx;
12264 if (vstart + sizeof (*entry) > endbuf)
12265 break;
12266
12267 entry = (Elf_External_Verneed *) vstart;
12268
12269 ent.vn_version = BYTE_GET (entry->vn_version);
12270 ent.vn_cnt = BYTE_GET (entry->vn_cnt);
12271 ent.vn_file = BYTE_GET (entry->vn_file);
12272 ent.vn_aux = BYTE_GET (entry->vn_aux);
12273 ent.vn_next = BYTE_GET (entry->vn_next);
12274
12275 printf (_(" %#06lx: Version: %d"), idx, ent.vn_version);
12276
12277 if (valid_dynamic_name (filedata, ent.vn_file))
12278 printf (_(" File: %s"),
12279 get_dynamic_name (filedata, ent.vn_file));
12280 else
12281 printf (_(" File: %lx"), ent.vn_file);
12282
12283 printf (_(" Cnt: %d\n"), ent.vn_cnt);
12284
12285 /* Check for overflow. */
12286 if (ent.vn_aux > (size_t) (endbuf - vstart))
12287 break;
12288 vstart += ent.vn_aux;
12289
12290 for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
12291 {
12292 Elf_External_Vernaux * eaux;
12293 Elf_Internal_Vernaux aux;
12294
12295 if (vstart + sizeof (*eaux) > endbuf)
12296 break;
12297 eaux = (Elf_External_Vernaux *) vstart;
12298
12299 aux.vna_hash = BYTE_GET (eaux->vna_hash);
12300 aux.vna_flags = BYTE_GET (eaux->vna_flags);
12301 aux.vna_other = BYTE_GET (eaux->vna_other);
12302 aux.vna_name = BYTE_GET (eaux->vna_name);
12303 aux.vna_next = BYTE_GET (eaux->vna_next);
12304
12305 if (valid_dynamic_name (filedata, aux.vna_name))
12306 printf (_(" %#06lx: Name: %s"),
12307 isum, get_dynamic_name (filedata, aux.vna_name));
12308 else
12309 printf (_(" %#06lx: Name index: %lx"),
12310 isum, aux.vna_name);
12311
12312 printf (_(" Flags: %s Version: %d\n"),
12313 get_ver_flags (aux.vna_flags), aux.vna_other);
12314
12315 if (aux.vna_next < sizeof (*eaux)
12316 && !(j == ent.vn_cnt - 1 && aux.vna_next == 0))
12317 {
12318 warn (_("Invalid vna_next field of %lx\n"),
12319 aux.vna_next);
12320 j = ent.vn_cnt;
12321 break;
12322 }
12323 /* Check for overflow. */
12324 if (aux.vna_next > (size_t) (endbuf - vstart))
12325 break;
12326 isum += aux.vna_next;
12327 vstart += aux.vna_next;
12328 }
12329
12330 if (j < ent.vn_cnt)
12331 warn (_("Missing Version Needs auxiliary information\n"));
12332
12333 if (ent.vn_next < sizeof (*entry)
12334 && !(cnt == section->sh_info - 1 && ent.vn_next == 0))
12335 {
12336 warn (_("Invalid vn_next field of %lx\n"), ent.vn_next);
12337 cnt = section->sh_info;
12338 break;
12339 }
12340 if (ent.vn_next > (size_t) (endbuf - ((char *) eneed + idx)))
12341 break;
12342 idx += ent.vn_next;
12343 }
12344
12345 if (cnt < section->sh_info)
12346 warn (_("Missing Version Needs information\n"));
12347
12348 free (eneed);
12349 }
12350 break;
12351
12352 case SHT_GNU_versym:
12353 {
12354 Elf_Internal_Shdr * link_section;
12355 size_t total;
12356 unsigned int cnt;
12357 unsigned char * edata;
12358 unsigned short * data;
12359 char * strtab;
12360 Elf_Internal_Sym * symbols;
12361 Elf_Internal_Shdr * string_sec;
12362 unsigned long num_syms;
12363 long off;
12364
12365 if (section->sh_link >= filedata->file_header.e_shnum)
12366 break;
12367
12368 link_section = filedata->section_headers + section->sh_link;
12369 total = section->sh_size / sizeof (Elf_External_Versym);
12370
12371 if (link_section->sh_link >= filedata->file_header.e_shnum)
12372 break;
12373
12374 found = true;
12375
12376 symbols = get_elf_symbols (filedata, link_section, & num_syms);
12377 if (symbols == NULL)
12378 break;
12379
12380 string_sec = filedata->section_headers + link_section->sh_link;
12381
12382 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
12383 string_sec->sh_size,
12384 _("version string table"));
12385 if (!strtab)
12386 {
12387 free (symbols);
12388 break;
12389 }
12390
12391 if (filedata->is_separate)
12392 printf (ngettext ("\nIn linked file '%s' the version symbols section '%s' contains %lu entry:\n",
12393 "\nIn linked file '%s' the version symbols section '%s' contains %lu entries:\n",
12394 total),
12395 filedata->file_name,
12396 printable_section_name (filedata, section),
12397 (unsigned long) total);
12398 else
12399 printf (ngettext ("\nVersion symbols section '%s' "
12400 "contains %lu entry:\n",
12401 "\nVersion symbols section '%s' "
12402 "contains %lu entries:\n",
12403 total),
12404 printable_section_name (filedata, section),
12405 (unsigned long) total);
12406
12407 printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
12408 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
12409 (unsigned long) section->sh_offset, section->sh_link,
12410 printable_section_name (filedata, link_section));
12411
12412 off = offset_from_vma (filedata,
12413 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
12414 total * sizeof (short));
12415 edata = (unsigned char *) get_data (NULL, filedata, off,
12416 sizeof (short), total,
12417 _("version symbol data"));
12418 if (!edata)
12419 {
12420 free (strtab);
12421 free (symbols);
12422 break;
12423 }
12424
12425 data = (short unsigned int *) cmalloc (total, sizeof (short));
12426
12427 for (cnt = total; cnt --;)
12428 data[cnt] = byte_get (edata + cnt * sizeof (short),
12429 sizeof (short));
12430
12431 free (edata);
12432
12433 for (cnt = 0; cnt < total; cnt += 4)
12434 {
12435 int j, nn;
12436 char *name;
12437 char *invalid = _("*invalid*");
12438
12439 printf (" %03x:", cnt);
12440
12441 for (j = 0; (j < 4) && (cnt + j) < total; ++j)
12442 switch (data[cnt + j])
12443 {
12444 case 0:
12445 fputs (_(" 0 (*local*) "), stdout);
12446 break;
12447
12448 case 1:
12449 fputs (_(" 1 (*global*) "), stdout);
12450 break;
12451
12452 default:
12453 nn = printf ("%4x%c", data[cnt + j] & VERSYM_VERSION,
12454 data[cnt + j] & VERSYM_HIDDEN ? 'h' : ' ');
12455
12456 /* If this index value is greater than the size of the symbols
12457 array, break to avoid an out-of-bounds read. */
12458 if ((unsigned long)(cnt + j) >= num_syms)
12459 {
12460 warn (_("invalid index into symbol array\n"));
12461 break;
12462 }
12463
12464 name = NULL;
12465 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
12466 {
12467 Elf_Internal_Verneed ivn;
12468 unsigned long offset;
12469
12470 offset = offset_from_vma
12471 (filedata,
12472 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
12473 sizeof (Elf_External_Verneed));
12474
12475 do
12476 {
12477 Elf_Internal_Vernaux ivna;
12478 Elf_External_Verneed evn;
12479 Elf_External_Vernaux evna;
12480 unsigned long a_off;
12481
12482 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
12483 _("version need")) == NULL)
12484 break;
12485
12486 ivn.vn_aux = BYTE_GET (evn.vn_aux);
12487 ivn.vn_next = BYTE_GET (evn.vn_next);
12488
12489 a_off = offset + ivn.vn_aux;
12490
12491 do
12492 {
12493 if (get_data (&evna, filedata, a_off, sizeof (evna),
12494 1, _("version need aux (2)")) == NULL)
12495 {
12496 ivna.vna_next = 0;
12497 ivna.vna_other = 0;
12498 }
12499 else
12500 {
12501 ivna.vna_next = BYTE_GET (evna.vna_next);
12502 ivna.vna_other = BYTE_GET (evna.vna_other);
12503 }
12504
12505 a_off += ivna.vna_next;
12506 }
12507 while (ivna.vna_other != data[cnt + j]
12508 && ivna.vna_next != 0);
12509
12510 if (ivna.vna_other == data[cnt + j])
12511 {
12512 ivna.vna_name = BYTE_GET (evna.vna_name);
12513
12514 if (ivna.vna_name >= string_sec->sh_size)
12515 name = invalid;
12516 else
12517 name = strtab + ivna.vna_name;
12518 break;
12519 }
12520
12521 offset += ivn.vn_next;
12522 }
12523 while (ivn.vn_next);
12524 }
12525
12526 if (data[cnt + j] != 0x8001
12527 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
12528 {
12529 Elf_Internal_Verdef ivd;
12530 Elf_External_Verdef evd;
12531 unsigned long offset;
12532
12533 offset = offset_from_vma
12534 (filedata,
12535 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
12536 sizeof evd);
12537
12538 do
12539 {
12540 if (get_data (&evd, filedata, offset, sizeof (evd), 1,
12541 _("version def")) == NULL)
12542 {
12543 ivd.vd_next = 0;
12544 /* PR 17531: file: 046-1082287-0.004. */
12545 ivd.vd_ndx = (data[cnt + j] & VERSYM_VERSION) + 1;
12546 break;
12547 }
12548 else
12549 {
12550 ivd.vd_next = BYTE_GET (evd.vd_next);
12551 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
12552 }
12553
12554 offset += ivd.vd_next;
12555 }
12556 while (ivd.vd_ndx != (data[cnt + j] & VERSYM_VERSION)
12557 && ivd.vd_next != 0);
12558
12559 if (ivd.vd_ndx == (data[cnt + j] & VERSYM_VERSION))
12560 {
12561 Elf_External_Verdaux evda;
12562 Elf_Internal_Verdaux ivda;
12563
12564 ivd.vd_aux = BYTE_GET (evd.vd_aux);
12565
12566 if (get_data (&evda, filedata,
12567 offset - ivd.vd_next + ivd.vd_aux,
12568 sizeof (evda), 1,
12569 _("version def aux")) == NULL)
12570 break;
12571
12572 ivda.vda_name = BYTE_GET (evda.vda_name);
12573
12574 if (ivda.vda_name >= string_sec->sh_size)
12575 name = invalid;
12576 else if (name != NULL && name != invalid)
12577 name = _("*both*");
12578 else
12579 name = strtab + ivda.vda_name;
12580 }
12581 }
12582 if (name != NULL)
12583 nn += printf ("(%s%-*s",
12584 name,
12585 12 - (int) strlen (name),
12586 ")");
12587
12588 if (nn < 18)
12589 printf ("%*c", 18 - nn, ' ');
12590 }
12591
12592 putchar ('\n');
12593 }
12594
12595 free (data);
12596 free (strtab);
12597 free (symbols);
12598 }
12599 break;
12600
12601 default:
12602 break;
12603 }
12604 }
12605
12606 if (! found)
12607 {
12608 if (filedata->is_separate)
12609 printf (_("\nNo version information found in linked file '%s'.\n"),
12610 filedata->file_name);
12611 else
12612 printf (_("\nNo version information found in this file.\n"));
12613 }
12614
12615 return true;
12616 }
12617
12618 static const char *
12619 get_symbol_binding (Filedata * filedata, unsigned int binding)
12620 {
12621 static char buff[64];
12622
12623 switch (binding)
12624 {
12625 case STB_LOCAL: return "LOCAL";
12626 case STB_GLOBAL: return "GLOBAL";
12627 case STB_WEAK: return "WEAK";
12628 default:
12629 if (binding >= STB_LOPROC && binding <= STB_HIPROC)
12630 snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
12631 binding);
12632 else if (binding >= STB_LOOS && binding <= STB_HIOS)
12633 {
12634 if (binding == STB_GNU_UNIQUE
12635 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU)
12636 return "UNIQUE";
12637 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
12638 }
12639 else
12640 snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
12641 return buff;
12642 }
12643 }
12644
12645 static const char *
12646 get_symbol_type (Filedata * filedata, unsigned int type)
12647 {
12648 static char buff[64];
12649
12650 switch (type)
12651 {
12652 case STT_NOTYPE: return "NOTYPE";
12653 case STT_OBJECT: return "OBJECT";
12654 case STT_FUNC: return "FUNC";
12655 case STT_SECTION: return "SECTION";
12656 case STT_FILE: return "FILE";
12657 case STT_COMMON: return "COMMON";
12658 case STT_TLS: return "TLS";
12659 case STT_RELC: return "RELC";
12660 case STT_SRELC: return "SRELC";
12661 default:
12662 if (type >= STT_LOPROC && type <= STT_HIPROC)
12663 {
12664 if (filedata->file_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
12665 return "THUMB_FUNC";
12666
12667 if (filedata->file_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
12668 return "REGISTER";
12669
12670 if (filedata->file_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
12671 return "PARISC_MILLI";
12672
12673 snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
12674 }
12675 else if (type >= STT_LOOS && type <= STT_HIOS)
12676 {
12677 if (filedata->file_header.e_machine == EM_PARISC)
12678 {
12679 if (type == STT_HP_OPAQUE)
12680 return "HP_OPAQUE";
12681 if (type == STT_HP_STUB)
12682 return "HP_STUB";
12683 }
12684
12685 if (type == STT_GNU_IFUNC
12686 && (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU
12687 || filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_FREEBSD))
12688 return "IFUNC";
12689
12690 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
12691 }
12692 else
12693 snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
12694 return buff;
12695 }
12696 }
12697
12698 static const char *
12699 get_symbol_visibility (unsigned int visibility)
12700 {
12701 switch (visibility)
12702 {
12703 case STV_DEFAULT: return "DEFAULT";
12704 case STV_INTERNAL: return "INTERNAL";
12705 case STV_HIDDEN: return "HIDDEN";
12706 case STV_PROTECTED: return "PROTECTED";
12707 default:
12708 error (_("Unrecognized visibility value: %u\n"), visibility);
12709 return _("<unknown>");
12710 }
12711 }
12712
12713 static const char *
12714 get_alpha_symbol_other (unsigned int other)
12715 {
12716 switch (other)
12717 {
12718 case STO_ALPHA_NOPV: return "NOPV";
12719 case STO_ALPHA_STD_GPLOAD: return "STD GPLOAD";
12720 default:
12721 error (_("Unrecognized alpha specific other value: %u\n"), other);
12722 return _("<unknown>");
12723 }
12724 }
12725
12726 static const char *
12727 get_solaris_symbol_visibility (unsigned int visibility)
12728 {
12729 switch (visibility)
12730 {
12731 case 4: return "EXPORTED";
12732 case 5: return "SINGLETON";
12733 case 6: return "ELIMINATE";
12734 default: return get_symbol_visibility (visibility);
12735 }
12736 }
12737
12738 static const char *
12739 get_aarch64_symbol_other (unsigned int other)
12740 {
12741 static char buf[32];
12742
12743 if (other & STO_AARCH64_VARIANT_PCS)
12744 {
12745 other &= ~STO_AARCH64_VARIANT_PCS;
12746 if (other == 0)
12747 return "VARIANT_PCS";
12748 snprintf (buf, sizeof buf, "VARIANT_PCS | %x", other);
12749 return buf;
12750 }
12751 return NULL;
12752 }
12753
12754 static const char *
12755 get_mips_symbol_other (unsigned int other)
12756 {
12757 switch (other)
12758 {
12759 case STO_OPTIONAL: return "OPTIONAL";
12760 case STO_MIPS_PLT: return "MIPS PLT";
12761 case STO_MIPS_PIC: return "MIPS PIC";
12762 case STO_MICROMIPS: return "MICROMIPS";
12763 case STO_MICROMIPS | STO_MIPS_PIC: return "MICROMIPS, MIPS PIC";
12764 case STO_MIPS16: return "MIPS16";
12765 default: return NULL;
12766 }
12767 }
12768
12769 static const char *
12770 get_ia64_symbol_other (Filedata * filedata, unsigned int other)
12771 {
12772 if (is_ia64_vms (filedata))
12773 {
12774 static char res[32];
12775
12776 res[0] = 0;
12777
12778 /* Function types is for images and .STB files only. */
12779 switch (filedata->file_header.e_type)
12780 {
12781 case ET_DYN:
12782 case ET_EXEC:
12783 switch (VMS_ST_FUNC_TYPE (other))
12784 {
12785 case VMS_SFT_CODE_ADDR:
12786 strcat (res, " CA");
12787 break;
12788 case VMS_SFT_SYMV_IDX:
12789 strcat (res, " VEC");
12790 break;
12791 case VMS_SFT_FD:
12792 strcat (res, " FD");
12793 break;
12794 case VMS_SFT_RESERVE:
12795 strcat (res, " RSV");
12796 break;
12797 default:
12798 warn (_("Unrecognized IA64 VMS ST Function type: %d\n"),
12799 VMS_ST_FUNC_TYPE (other));
12800 strcat (res, " <unknown>");
12801 break;
12802 }
12803 break;
12804 default:
12805 break;
12806 }
12807 switch (VMS_ST_LINKAGE (other))
12808 {
12809 case VMS_STL_IGNORE:
12810 strcat (res, " IGN");
12811 break;
12812 case VMS_STL_RESERVE:
12813 strcat (res, " RSV");
12814 break;
12815 case VMS_STL_STD:
12816 strcat (res, " STD");
12817 break;
12818 case VMS_STL_LNK:
12819 strcat (res, " LNK");
12820 break;
12821 default:
12822 warn (_("Unrecognized IA64 VMS ST Linkage: %d\n"),
12823 VMS_ST_LINKAGE (other));
12824 strcat (res, " <unknown>");
12825 break;
12826 }
12827
12828 if (res[0] != 0)
12829 return res + 1;
12830 else
12831 return res;
12832 }
12833 return NULL;
12834 }
12835
12836 static const char *
12837 get_ppc64_symbol_other (unsigned int other)
12838 {
12839 if ((other & ~STO_PPC64_LOCAL_MASK) != 0)
12840 return NULL;
12841
12842 other >>= STO_PPC64_LOCAL_BIT;
12843 if (other <= 6)
12844 {
12845 static char buf[64];
12846 if (other >= 2)
12847 other = ppc64_decode_local_entry (other);
12848 snprintf (buf, sizeof buf, _("<localentry>: %d"), other);
12849 return buf;
12850 }
12851 return NULL;
12852 }
12853
12854 static const char *
12855 get_riscv_symbol_other (unsigned int other)
12856 {
12857 static char buf[32];
12858 buf[0] = 0;
12859
12860 if (other & STO_RISCV_VARIANT_CC)
12861 {
12862 strcat (buf, _(" VARIANT_CC"));
12863 other &= ~STO_RISCV_VARIANT_CC;
12864 }
12865
12866 if (other != 0)
12867 snprintf (buf, sizeof buf, " %x", other);
12868
12869
12870 if (buf[0] != 0)
12871 return buf + 1;
12872 else
12873 return buf;
12874 }
12875
12876 static const char *
12877 get_symbol_other (Filedata * filedata, unsigned int other)
12878 {
12879 const char * result = NULL;
12880 static char buff [64];
12881
12882 if (other == 0)
12883 return "";
12884
12885 switch (filedata->file_header.e_machine)
12886 {
12887 case EM_ALPHA:
12888 result = get_alpha_symbol_other (other);
12889 break;
12890 case EM_AARCH64:
12891 result = get_aarch64_symbol_other (other);
12892 break;
12893 case EM_MIPS:
12894 result = get_mips_symbol_other (other);
12895 break;
12896 case EM_IA_64:
12897 result = get_ia64_symbol_other (filedata, other);
12898 break;
12899 case EM_PPC64:
12900 result = get_ppc64_symbol_other (other);
12901 break;
12902 case EM_RISCV:
12903 result = get_riscv_symbol_other (other);
12904 break;
12905 default:
12906 result = NULL;
12907 break;
12908 }
12909
12910 if (result)
12911 return result;
12912
12913 snprintf (buff, sizeof buff, _("<other>: %x"), other);
12914 return buff;
12915 }
12916
12917 static const char *
12918 get_symbol_index_type (Filedata * filedata, unsigned int type)
12919 {
12920 static char buff[32];
12921
12922 switch (type)
12923 {
12924 case SHN_UNDEF: return "UND";
12925 case SHN_ABS: return "ABS";
12926 case SHN_COMMON: return "COM";
12927 default:
12928 if (type == SHN_IA_64_ANSI_COMMON
12929 && filedata->file_header.e_machine == EM_IA_64
12930 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX)
12931 return "ANSI_COM";
12932 else if ((filedata->file_header.e_machine == EM_X86_64
12933 || filedata->file_header.e_machine == EM_L1OM
12934 || filedata->file_header.e_machine == EM_K1OM)
12935 && type == SHN_X86_64_LCOMMON)
12936 return "LARGE_COM";
12937 else if ((type == SHN_MIPS_SCOMMON
12938 && filedata->file_header.e_machine == EM_MIPS)
12939 || (type == SHN_TIC6X_SCOMMON
12940 && filedata->file_header.e_machine == EM_TI_C6000))
12941 return "SCOM";
12942 else if (type == SHN_MIPS_SUNDEFINED
12943 && filedata->file_header.e_machine == EM_MIPS)
12944 return "SUND";
12945 else if (type >= SHN_LOPROC && type <= SHN_HIPROC)
12946 sprintf (buff, "PRC[0x%04x]", type & 0xffff);
12947 else if (type >= SHN_LOOS && type <= SHN_HIOS)
12948 sprintf (buff, "OS [0x%04x]", type & 0xffff);
12949 else if (type >= SHN_LORESERVE)
12950 sprintf (buff, "RSV[0x%04x]", type & 0xffff);
12951 else if (filedata->file_header.e_shnum != 0
12952 && type >= filedata->file_header.e_shnum)
12953 sprintf (buff, _("bad section index[%3d]"), type);
12954 else
12955 sprintf (buff, "%3d", type);
12956 break;
12957 }
12958
12959 return buff;
12960 }
12961
12962 static const char *
12963 get_symbol_version_string (Filedata * filedata,
12964 bool is_dynsym,
12965 const char * strtab,
12966 unsigned long int strtab_size,
12967 unsigned int si,
12968 Elf_Internal_Sym * psym,
12969 enum versioned_symbol_info * sym_info,
12970 unsigned short * vna_other)
12971 {
12972 unsigned char data[2];
12973 unsigned short vers_data;
12974 unsigned long offset;
12975 unsigned short max_vd_ndx;
12976
12977 if (!is_dynsym
12978 || filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)] == 0)
12979 return NULL;
12980
12981 offset = offset_from_vma (filedata,
12982 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
12983 sizeof data + si * sizeof (vers_data));
12984
12985 if (get_data (&data, filedata, offset + si * sizeof (vers_data),
12986 sizeof (data), 1, _("version data")) == NULL)
12987 return NULL;
12988
12989 vers_data = byte_get (data, 2);
12990
12991 if ((vers_data & VERSYM_HIDDEN) == 0 && vers_data == 0)
12992 return NULL;
12993
12994 *sym_info = (vers_data & VERSYM_HIDDEN) != 0 ? symbol_hidden : symbol_public;
12995 max_vd_ndx = 0;
12996
12997 /* Usually we'd only see verdef for defined symbols, and verneed for
12998 undefined symbols. However, symbols defined by the linker in
12999 .dynbss for variables copied from a shared library in order to
13000 avoid text relocations are defined yet have verneed. We could
13001 use a heuristic to detect the special case, for example, check
13002 for verneed first on symbols defined in SHT_NOBITS sections, but
13003 it is simpler and more reliable to just look for both verdef and
13004 verneed. .dynbss might not be mapped to a SHT_NOBITS section. */
13005
13006 if (psym->st_shndx != SHN_UNDEF
13007 && vers_data != 0x8001
13008 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
13009 {
13010 Elf_Internal_Verdef ivd;
13011 Elf_Internal_Verdaux ivda;
13012 Elf_External_Verdaux evda;
13013 unsigned long off;
13014
13015 off = offset_from_vma (filedata,
13016 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
13017 sizeof (Elf_External_Verdef));
13018
13019 do
13020 {
13021 Elf_External_Verdef evd;
13022
13023 if (get_data (&evd, filedata, off, sizeof (evd), 1,
13024 _("version def")) == NULL)
13025 {
13026 ivd.vd_ndx = 0;
13027 ivd.vd_aux = 0;
13028 ivd.vd_next = 0;
13029 ivd.vd_flags = 0;
13030 }
13031 else
13032 {
13033 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
13034 ivd.vd_aux = BYTE_GET (evd.vd_aux);
13035 ivd.vd_next = BYTE_GET (evd.vd_next);
13036 ivd.vd_flags = BYTE_GET (evd.vd_flags);
13037 }
13038
13039 if ((ivd.vd_ndx & VERSYM_VERSION) > max_vd_ndx)
13040 max_vd_ndx = ivd.vd_ndx & VERSYM_VERSION;
13041
13042 off += ivd.vd_next;
13043 }
13044 while (ivd.vd_ndx != (vers_data & VERSYM_VERSION) && ivd.vd_next != 0);
13045
13046 if (ivd.vd_ndx == (vers_data & VERSYM_VERSION))
13047 {
13048 if (ivd.vd_ndx == 1 && ivd.vd_flags == VER_FLG_BASE)
13049 return NULL;
13050
13051 off -= ivd.vd_next;
13052 off += ivd.vd_aux;
13053
13054 if (get_data (&evda, filedata, off, sizeof (evda), 1,
13055 _("version def aux")) != NULL)
13056 {
13057 ivda.vda_name = BYTE_GET (evda.vda_name);
13058
13059 if (psym->st_name != ivda.vda_name)
13060 return (ivda.vda_name < strtab_size
13061 ? strtab + ivda.vda_name : _("<corrupt>"));
13062 }
13063 }
13064 }
13065
13066 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
13067 {
13068 Elf_External_Verneed evn;
13069 Elf_Internal_Verneed ivn;
13070 Elf_Internal_Vernaux ivna;
13071
13072 offset = offset_from_vma (filedata,
13073 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
13074 sizeof evn);
13075 do
13076 {
13077 unsigned long vna_off;
13078
13079 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
13080 _("version need")) == NULL)
13081 {
13082 ivna.vna_next = 0;
13083 ivna.vna_other = 0;
13084 ivna.vna_name = 0;
13085 break;
13086 }
13087
13088 ivn.vn_aux = BYTE_GET (evn.vn_aux);
13089 ivn.vn_next = BYTE_GET (evn.vn_next);
13090
13091 vna_off = offset + ivn.vn_aux;
13092
13093 do
13094 {
13095 Elf_External_Vernaux evna;
13096
13097 if (get_data (&evna, filedata, vna_off, sizeof (evna), 1,
13098 _("version need aux (3)")) == NULL)
13099 {
13100 ivna.vna_next = 0;
13101 ivna.vna_other = 0;
13102 ivna.vna_name = 0;
13103 }
13104 else
13105 {
13106 ivna.vna_other = BYTE_GET (evna.vna_other);
13107 ivna.vna_next = BYTE_GET (evna.vna_next);
13108 ivna.vna_name = BYTE_GET (evna.vna_name);
13109 }
13110
13111 vna_off += ivna.vna_next;
13112 }
13113 while (ivna.vna_other != vers_data && ivna.vna_next != 0);
13114
13115 if (ivna.vna_other == vers_data)
13116 break;
13117
13118 offset += ivn.vn_next;
13119 }
13120 while (ivn.vn_next != 0);
13121
13122 if (ivna.vna_other == vers_data)
13123 {
13124 *sym_info = symbol_undefined;
13125 *vna_other = ivna.vna_other;
13126 return (ivna.vna_name < strtab_size
13127 ? strtab + ivna.vna_name : _("<corrupt>"));
13128 }
13129 else if ((max_vd_ndx || (vers_data & VERSYM_VERSION) != 1)
13130 && (vers_data & VERSYM_VERSION) > max_vd_ndx)
13131 return _("<corrupt>");
13132 }
13133 return NULL;
13134 }
13135
13136 /* Display a symbol size on stdout. Format is based on --sym-base setting. */
13137
13138 static unsigned int
13139 print_dynamic_symbol_size (uint64_t vma, int base)
13140 {
13141 switch (base)
13142 {
13143 case 8:
13144 return print_vma (vma, OCTAL_5);
13145
13146 case 10:
13147 return print_vma (vma, UNSIGNED_5);
13148
13149 case 16:
13150 return print_vma (vma, PREFIX_HEX_5);
13151
13152 case 0:
13153 default:
13154 return print_vma (vma, DEC_5);
13155 }
13156 }
13157
13158 static void
13159 print_dynamic_symbol (Filedata *filedata, unsigned long si,
13160 Elf_Internal_Sym *symtab,
13161 Elf_Internal_Shdr *section,
13162 char *strtab, size_t strtab_size)
13163 {
13164 const char *version_string;
13165 enum versioned_symbol_info sym_info;
13166 unsigned short vna_other;
13167 bool is_valid;
13168 const char * sstr;
13169 Elf_Internal_Sym *psym = symtab + si;
13170
13171 printf ("%6ld: ", si);
13172 print_vma (psym->st_value, LONG_HEX);
13173 putchar (' ');
13174 print_dynamic_symbol_size (psym->st_size, sym_base);
13175 printf (" %-7s", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
13176 printf (" %-6s", get_symbol_binding (filedata, ELF_ST_BIND (psym->st_info)));
13177 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
13178 printf (" %-7s", get_solaris_symbol_visibility (psym->st_other));
13179 else
13180 {
13181 unsigned int vis = ELF_ST_VISIBILITY (psym->st_other);
13182
13183 printf (" %-7s", get_symbol_visibility (vis));
13184 /* Check to see if any other bits in the st_other field are set.
13185 Note - displaying this information disrupts the layout of the
13186 table being generated, but for the moment this case is very rare. */
13187 if (psym->st_other ^ vis)
13188 printf (" [%s] ", get_symbol_other (filedata, psym->st_other ^ vis));
13189 }
13190 printf (" %4s ", get_symbol_index_type (filedata, psym->st_shndx));
13191
13192 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION
13193 && psym->st_shndx < filedata->file_header.e_shnum
13194 && filedata->section_headers != NULL
13195 && psym->st_name == 0)
13196 {
13197 is_valid
13198 = section_name_valid (filedata,
13199 filedata->section_headers + psym->st_shndx);
13200 sstr = is_valid ?
13201 section_name_print (filedata,
13202 filedata->section_headers + psym->st_shndx)
13203 : _("<corrupt>");
13204 }
13205 else
13206 {
13207 is_valid = valid_symbol_name (strtab, strtab_size, psym->st_name);
13208 sstr = is_valid ? strtab + psym->st_name : _("<corrupt>");
13209 }
13210
13211 version_string
13212 = get_symbol_version_string (filedata,
13213 (section == NULL
13214 || section->sh_type == SHT_DYNSYM),
13215 strtab, strtab_size, si,
13216 psym, &sym_info, &vna_other);
13217
13218 int len_avail = 21;
13219 if (! do_wide && version_string != NULL)
13220 {
13221 char buffer[16];
13222
13223 len_avail -= 1 + strlen (version_string);
13224
13225 if (sym_info == symbol_undefined)
13226 len_avail -= sprintf (buffer," (%d)", vna_other);
13227 else if (sym_info != symbol_hidden)
13228 len_avail -= 1;
13229 }
13230
13231 print_symbol (len_avail, sstr);
13232
13233 if (version_string)
13234 {
13235 if (sym_info == symbol_undefined)
13236 printf ("@%s (%d)", version_string, vna_other);
13237 else
13238 printf (sym_info == symbol_hidden ? "@%s" : "@@%s",
13239 version_string);
13240 }
13241
13242 putchar ('\n');
13243
13244 if (ELF_ST_BIND (psym->st_info) == STB_LOCAL
13245 && section != NULL
13246 && si >= section->sh_info
13247 /* Irix 5 and 6 MIPS binaries are known to ignore this requirement. */
13248 && filedata->file_header.e_machine != EM_MIPS
13249 /* Solaris binaries have been found to violate this requirement as
13250 well. Not sure if this is a bug or an ABI requirement. */
13251 && filedata->file_header.e_ident[EI_OSABI] != ELFOSABI_SOLARIS)
13252 warn (_("local symbol %lu found at index >= %s's sh_info value of %u\n"),
13253 si, printable_section_name (filedata, section), section->sh_info);
13254 }
13255
13256 static const char *
13257 get_lto_kind (unsigned int kind)
13258 {
13259 switch (kind)
13260 {
13261 case 0: return "DEF";
13262 case 1: return "WEAKDEF";
13263 case 2: return "UNDEF";
13264 case 3: return "WEAKUNDEF";
13265 case 4: return "COMMON";
13266 default:
13267 break;
13268 }
13269
13270 static char buffer[30];
13271 error (_("Unknown LTO symbol definition encountered: %u\n"), kind);
13272 sprintf (buffer, "<unknown: %u>", kind);
13273 return buffer;
13274 }
13275
13276 static const char *
13277 get_lto_visibility (unsigned int visibility)
13278 {
13279 switch (visibility)
13280 {
13281 case 0: return "DEFAULT";
13282 case 1: return "PROTECTED";
13283 case 2: return "INTERNAL";
13284 case 3: return "HIDDEN";
13285 default:
13286 break;
13287 }
13288
13289 static char buffer[30];
13290 error (_("Unknown LTO symbol visibility encountered: %u\n"), visibility);
13291 sprintf (buffer, "<unknown: %u>", visibility);
13292 return buffer;
13293 }
13294
13295 static const char *
13296 get_lto_sym_type (unsigned int sym_type)
13297 {
13298 switch (sym_type)
13299 {
13300 case 0: return "UNKNOWN";
13301 case 1: return "FUNCTION";
13302 case 2: return "VARIABLE";
13303 default:
13304 break;
13305 }
13306
13307 static char buffer[30];
13308 error (_("Unknown LTO symbol type encountered: %u\n"), sym_type);
13309 sprintf (buffer, "<unknown: %u>", sym_type);
13310 return buffer;
13311 }
13312
13313 /* Display an LTO format symbol table.
13314 FIXME: The format of LTO symbol tables is not formalized.
13315 So this code could need changing in the future. */
13316
13317 static bool
13318 display_lto_symtab (Filedata * filedata,
13319 Elf_Internal_Shdr * section)
13320 {
13321 if (section->sh_size == 0)
13322 {
13323 if (filedata->is_separate)
13324 printf (_("\nThe LTO Symbol table section '%s' in linked file '%s' is empty!\n"),
13325 printable_section_name (filedata, section),
13326 filedata->file_name);
13327 else
13328 printf (_("\nLTO Symbol table '%s' is empty!\n"),
13329 printable_section_name (filedata, section));
13330
13331 return true;
13332 }
13333
13334 if (section->sh_size > filedata->file_size)
13335 {
13336 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
13337 printable_section_name (filedata, section),
13338 (unsigned long) section->sh_size);
13339 return false;
13340 }
13341
13342 void * alloced_data = get_data (NULL, filedata, section->sh_offset,
13343 section->sh_size, 1, _("LTO symbols"));
13344 if (alloced_data == NULL)
13345 return false;
13346
13347 /* Look for extended data for the symbol table. */
13348 Elf_Internal_Shdr * ext;
13349 void * ext_data_orig = NULL;
13350 char * ext_data = NULL;
13351 char * ext_data_end = NULL;
13352 char * ext_name = NULL;
13353
13354 if (asprintf (& ext_name, ".gnu.lto_.ext_symtab.%s",
13355 (section_name (filedata, section)
13356 + sizeof (".gnu.lto_.symtab.") - 1)) > 0
13357 && ext_name != NULL /* Paranoia. */
13358 && (ext = find_section (filedata, ext_name)) != NULL)
13359 {
13360 if (ext->sh_size < 3)
13361 error (_("LTO Symbol extension table '%s' is empty!\n"),
13362 printable_section_name (filedata, ext));
13363 else
13364 {
13365 ext_data_orig = ext_data = get_data (NULL, filedata, ext->sh_offset,
13366 ext->sh_size, 1,
13367 _("LTO ext symbol data"));
13368 if (ext_data != NULL)
13369 {
13370 ext_data_end = ext_data + ext->sh_size;
13371 if (* ext_data++ != 1)
13372 error (_("Unexpected version number in symbol extension table\n"));
13373 }
13374 }
13375 }
13376
13377 const unsigned char * data = (const unsigned char *) alloced_data;
13378 const unsigned char * end = data + section->sh_size;
13379
13380 if (filedata->is_separate)
13381 printf (_("\nIn linked file '%s': "), filedata->file_name);
13382 else
13383 printf ("\n");
13384
13385 if (ext_data_orig != NULL)
13386 {
13387 if (do_wide)
13388 printf (_("LTO Symbol table '%s' and extension table '%s' contain:\n"),
13389 printable_section_name (filedata, section),
13390 printable_section_name (filedata, ext));
13391 else
13392 {
13393 printf (_("LTO Symbol table '%s'\n"),
13394 printable_section_name (filedata, section));
13395 printf (_(" and extension table '%s' contain:\n"),
13396 printable_section_name (filedata, ext));
13397 }
13398 }
13399 else
13400 printf (_("LTO Symbol table '%s' contains:\n"),
13401 printable_section_name (filedata, section));
13402
13403 /* FIXME: Add a wide version. */
13404 if (ext_data_orig != NULL)
13405 printf (_(" Comdat_Key Kind Visibility Size Slot Type Section Name\n"));
13406 else
13407 printf (_(" Comdat_Key Kind Visibility Size Slot Name\n"));
13408
13409 /* FIXME: We do not handle style prefixes. */
13410
13411 while (data < end)
13412 {
13413 const unsigned char * sym_name = data;
13414 data += strnlen ((const char *) sym_name, end - data) + 1;
13415 if (data >= end)
13416 goto fail;
13417
13418 const unsigned char * comdat_key = data;
13419 data += strnlen ((const char *) comdat_key, end - data) + 1;
13420 if (data >= end)
13421 goto fail;
13422
13423 if (data + 2 + 8 + 4 > end)
13424 goto fail;
13425
13426 unsigned int kind = *data++;
13427 unsigned int visibility = *data++;
13428
13429 uint64_t size = byte_get (data, 8);
13430 data += 8;
13431
13432 uint64_t slot = byte_get (data, 4);
13433 data += 4;
13434
13435 if (ext_data != NULL)
13436 {
13437 if (ext_data < (ext_data_end - 1))
13438 {
13439 unsigned int sym_type = * ext_data ++;
13440 unsigned int sec_kind = * ext_data ++;
13441
13442 printf (" %10s %10s %11s %08" PRIx64 " %08" PRIx64 " %9s %08x _",
13443 * comdat_key == 0 ? "-" : (char *) comdat_key,
13444 get_lto_kind (kind),
13445 get_lto_visibility (visibility),
13446 size,
13447 slot,
13448 get_lto_sym_type (sym_type),
13449 sec_kind);
13450 print_symbol (6, (const char *) sym_name);
13451 }
13452 else
13453 {
13454 error (_("Ran out of LTO symbol extension data\n"));
13455 ext_data = NULL;
13456 /* FIXME: return FAIL result ? */
13457 }
13458 }
13459 else
13460 {
13461 printf (" %10s %10s %11s %08" PRIx64 " %08" PRIx64 " _",
13462 * comdat_key == 0 ? "-" : (char *) comdat_key,
13463 get_lto_kind (kind),
13464 get_lto_visibility (visibility),
13465 size,
13466 slot);
13467 print_symbol (21, (const char *) sym_name);
13468 }
13469 putchar ('\n');
13470 }
13471
13472 if (ext_data != NULL && ext_data < ext_data_end)
13473 {
13474 error (_("Data remains in the LTO symbol extension table\n"));
13475 goto fail;
13476 }
13477
13478 free (alloced_data);
13479 free (ext_data_orig);
13480 free (ext_name);
13481 return true;
13482
13483 fail:
13484 error (_("Buffer overrun encountered whilst decoding LTO symbol table\n"));
13485 free (alloced_data);
13486 free (ext_data_orig);
13487 free (ext_name);
13488 return false;
13489 }
13490
13491 /* Display LTO symbol tables. */
13492
13493 static bool
13494 process_lto_symbol_tables (Filedata * filedata)
13495 {
13496 Elf_Internal_Shdr * section;
13497 unsigned int i;
13498 bool res = true;
13499
13500 if (!do_lto_syms)
13501 return true;
13502
13503 if (filedata->section_headers == NULL)
13504 return true;
13505
13506 for (i = 0, section = filedata->section_headers;
13507 i < filedata->file_header.e_shnum;
13508 i++, section++)
13509 if (section_name_valid (filedata, section)
13510 && startswith (section_name (filedata, section), ".gnu.lto_.symtab."))
13511 res &= display_lto_symtab (filedata, section);
13512
13513 return res;
13514 }
13515
13516 /* Dump the symbol table. */
13517
13518 static bool
13519 process_symbol_table (Filedata * filedata)
13520 {
13521 Elf_Internal_Shdr * section;
13522
13523 if (!do_syms && !do_dyn_syms && !do_histogram)
13524 return true;
13525
13526 if ((filedata->dynamic_info[DT_HASH] || filedata->dynamic_info_DT_GNU_HASH)
13527 && do_syms
13528 && do_using_dynamic
13529 && filedata->dynamic_strings != NULL
13530 && filedata->dynamic_symbols != NULL)
13531 {
13532 unsigned long si;
13533
13534 if (filedata->is_separate)
13535 {
13536 printf (ngettext ("\nIn linked file '%s' the dynamic symbol table contains %lu entry:\n",
13537 "\nIn linked file '%s' the dynamic symbol table contains %lu entries:\n",
13538 filedata->num_dynamic_syms),
13539 filedata->file_name,
13540 filedata->num_dynamic_syms);
13541 }
13542 else
13543 {
13544 printf (ngettext ("\nSymbol table for image contains %lu entry:\n",
13545 "\nSymbol table for image contains %lu entries:\n",
13546 filedata->num_dynamic_syms),
13547 filedata->num_dynamic_syms);
13548 }
13549 if (is_32bit_elf)
13550 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13551 else
13552 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13553
13554 for (si = 0; si < filedata->num_dynamic_syms; si++)
13555 print_dynamic_symbol (filedata, si, filedata->dynamic_symbols, NULL,
13556 filedata->dynamic_strings,
13557 filedata->dynamic_strings_length);
13558 }
13559 else if ((do_dyn_syms || (do_syms && !do_using_dynamic))
13560 && filedata->section_headers != NULL)
13561 {
13562 unsigned int i;
13563
13564 for (i = 0, section = filedata->section_headers;
13565 i < filedata->file_header.e_shnum;
13566 i++, section++)
13567 {
13568 char * strtab = NULL;
13569 unsigned long int strtab_size = 0;
13570 Elf_Internal_Sym * symtab;
13571 unsigned long si, num_syms;
13572
13573 if ((section->sh_type != SHT_SYMTAB
13574 && section->sh_type != SHT_DYNSYM)
13575 || (!do_syms
13576 && section->sh_type == SHT_SYMTAB))
13577 continue;
13578
13579 if (section->sh_entsize == 0)
13580 {
13581 printf (_("\nSymbol table '%s' has a sh_entsize of zero!\n"),
13582 printable_section_name (filedata, section));
13583 continue;
13584 }
13585
13586 num_syms = section->sh_size / section->sh_entsize;
13587
13588 if (filedata->is_separate)
13589 printf (ngettext ("\nIn linked file '%s' symbol section '%s' contains %lu entry:\n",
13590 "\nIn linked file '%s' symbol section '%s' contains %lu entries:\n",
13591 num_syms),
13592 filedata->file_name,
13593 printable_section_name (filedata, section),
13594 num_syms);
13595 else
13596 printf (ngettext ("\nSymbol table '%s' contains %lu entry:\n",
13597 "\nSymbol table '%s' contains %lu entries:\n",
13598 num_syms),
13599 printable_section_name (filedata, section),
13600 num_syms);
13601
13602 if (is_32bit_elf)
13603 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13604 else
13605 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13606
13607 symtab = get_elf_symbols (filedata, section, & num_syms);
13608 if (symtab == NULL)
13609 continue;
13610
13611 if (section->sh_link == filedata->file_header.e_shstrndx)
13612 {
13613 strtab = filedata->string_table;
13614 strtab_size = filedata->string_table_length;
13615 }
13616 else if (section->sh_link < filedata->file_header.e_shnum)
13617 {
13618 Elf_Internal_Shdr * string_sec;
13619
13620 string_sec = filedata->section_headers + section->sh_link;
13621
13622 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset,
13623 1, string_sec->sh_size,
13624 _("string table"));
13625 strtab_size = strtab != NULL ? string_sec->sh_size : 0;
13626 }
13627
13628 for (si = 0; si < num_syms; si++)
13629 print_dynamic_symbol (filedata, si, symtab, section,
13630 strtab, strtab_size);
13631
13632 free (symtab);
13633 if (strtab != filedata->string_table)
13634 free (strtab);
13635 }
13636 }
13637 else if (do_syms)
13638 printf
13639 (_("\nDynamic symbol information is not available for displaying symbols.\n"));
13640
13641 if (do_histogram && filedata->buckets != NULL)
13642 {
13643 unsigned long * lengths;
13644 unsigned long * counts;
13645 unsigned long hn;
13646 uint64_t si;
13647 unsigned long maxlength = 0;
13648 unsigned long nzero_counts = 0;
13649 unsigned long nsyms = 0;
13650 char *visited;
13651
13652 printf (ngettext ("\nHistogram for bucket list length "
13653 "(total of %lu bucket):\n",
13654 "\nHistogram for bucket list length "
13655 "(total of %lu buckets):\n",
13656 (unsigned long) filedata->nbuckets),
13657 (unsigned long) filedata->nbuckets);
13658
13659 lengths = (unsigned long *) calloc (filedata->nbuckets,
13660 sizeof (*lengths));
13661 if (lengths == NULL)
13662 {
13663 error (_("Out of memory allocating space for histogram buckets\n"));
13664 goto err_out;
13665 }
13666 visited = xcmalloc (filedata->nchains, 1);
13667 memset (visited, 0, filedata->nchains);
13668
13669 printf (_(" Length Number %% of total Coverage\n"));
13670 for (hn = 0; hn < filedata->nbuckets; ++hn)
13671 {
13672 for (si = filedata->buckets[hn]; si > 0; si = filedata->chains[si])
13673 {
13674 ++nsyms;
13675 if (maxlength < ++lengths[hn])
13676 ++maxlength;
13677 if (si >= filedata->nchains || visited[si])
13678 {
13679 error (_("histogram chain is corrupt\n"));
13680 break;
13681 }
13682 visited[si] = 1;
13683 }
13684 }
13685 free (visited);
13686
13687 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
13688 if (counts == NULL)
13689 {
13690 free (lengths);
13691 error (_("Out of memory allocating space for histogram counts\n"));
13692 goto err_out;
13693 }
13694
13695 for (hn = 0; hn < filedata->nbuckets; ++hn)
13696 ++counts[lengths[hn]];
13697
13698 if (filedata->nbuckets > 0)
13699 {
13700 unsigned long i;
13701 printf (" 0 %-10lu (%5.1f%%)\n",
13702 counts[0], (counts[0] * 100.0) / filedata->nbuckets);
13703 for (i = 1; i <= maxlength; ++i)
13704 {
13705 nzero_counts += counts[i] * i;
13706 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
13707 i, counts[i], (counts[i] * 100.0) / filedata->nbuckets,
13708 (nzero_counts * 100.0) / nsyms);
13709 }
13710 }
13711
13712 free (counts);
13713 free (lengths);
13714 }
13715
13716 free (filedata->buckets);
13717 filedata->buckets = NULL;
13718 filedata->nbuckets = 0;
13719 free (filedata->chains);
13720 filedata->chains = NULL;
13721
13722 if (do_histogram && filedata->gnubuckets != NULL)
13723 {
13724 unsigned long * lengths;
13725 unsigned long * counts;
13726 unsigned long hn;
13727 unsigned long maxlength = 0;
13728 unsigned long nzero_counts = 0;
13729 unsigned long nsyms = 0;
13730
13731 printf (ngettext ("\nHistogram for `%s' bucket list length "
13732 "(total of %lu bucket):\n",
13733 "\nHistogram for `%s' bucket list length "
13734 "(total of %lu buckets):\n",
13735 (unsigned long) filedata->ngnubuckets),
13736 GNU_HASH_SECTION_NAME (filedata),
13737 (unsigned long) filedata->ngnubuckets);
13738
13739 lengths = (unsigned long *) calloc (filedata->ngnubuckets,
13740 sizeof (*lengths));
13741 if (lengths == NULL)
13742 {
13743 error (_("Out of memory allocating space for gnu histogram buckets\n"));
13744 goto err_out;
13745 }
13746
13747 printf (_(" Length Number %% of total Coverage\n"));
13748
13749 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
13750 if (filedata->gnubuckets[hn] != 0)
13751 {
13752 uint64_t off, length = 1;
13753
13754 for (off = filedata->gnubuckets[hn] - filedata->gnusymidx;
13755 /* PR 17531 file: 010-77222-0.004. */
13756 off < filedata->ngnuchains
13757 && (filedata->gnuchains[off] & 1) == 0;
13758 ++off)
13759 ++length;
13760 lengths[hn] = length;
13761 if (length > maxlength)
13762 maxlength = length;
13763 nsyms += length;
13764 }
13765
13766 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
13767 if (counts == NULL)
13768 {
13769 free (lengths);
13770 error (_("Out of memory allocating space for gnu histogram counts\n"));
13771 goto err_out;
13772 }
13773
13774 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
13775 ++counts[lengths[hn]];
13776
13777 if (filedata->ngnubuckets > 0)
13778 {
13779 unsigned long j;
13780 printf (" 0 %-10lu (%5.1f%%)\n",
13781 counts[0], (counts[0] * 100.0) / filedata->ngnubuckets);
13782 for (j = 1; j <= maxlength; ++j)
13783 {
13784 nzero_counts += counts[j] * j;
13785 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
13786 j, counts[j], (counts[j] * 100.0) / filedata->ngnubuckets,
13787 (nzero_counts * 100.0) / nsyms);
13788 }
13789 }
13790
13791 free (counts);
13792 free (lengths);
13793 }
13794 free (filedata->gnubuckets);
13795 filedata->gnubuckets = NULL;
13796 filedata->ngnubuckets = 0;
13797 free (filedata->gnuchains);
13798 filedata->gnuchains = NULL;
13799 filedata->ngnuchains = 0;
13800 free (filedata->mipsxlat);
13801 filedata->mipsxlat = NULL;
13802 return true;
13803
13804 err_out:
13805 free (filedata->gnubuckets);
13806 filedata->gnubuckets = NULL;
13807 filedata->ngnubuckets = 0;
13808 free (filedata->gnuchains);
13809 filedata->gnuchains = NULL;
13810 filedata->ngnuchains = 0;
13811 free (filedata->mipsxlat);
13812 filedata->mipsxlat = NULL;
13813 free (filedata->buckets);
13814 filedata->buckets = NULL;
13815 filedata->nbuckets = 0;
13816 free (filedata->chains);
13817 filedata->chains = NULL;
13818 return false;
13819 }
13820
13821 static bool
13822 process_syminfo (Filedata * filedata)
13823 {
13824 unsigned int i;
13825
13826 if (filedata->dynamic_syminfo == NULL
13827 || !do_dynamic)
13828 /* No syminfo, this is ok. */
13829 return true;
13830
13831 /* There better should be a dynamic symbol section. */
13832 if (filedata->dynamic_symbols == NULL || filedata->dynamic_strings == NULL)
13833 return false;
13834
13835 if (filedata->is_separate)
13836 printf (ngettext ("\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entry:\n",
13837 "\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entries:\n",
13838 filedata->dynamic_syminfo_nent),
13839 filedata->file_name,
13840 filedata->dynamic_syminfo_offset,
13841 filedata->dynamic_syminfo_nent);
13842 else
13843 printf (ngettext ("\nDynamic info segment at offset 0x%lx "
13844 "contains %d entry:\n",
13845 "\nDynamic info segment at offset 0x%lx "
13846 "contains %d entries:\n",
13847 filedata->dynamic_syminfo_nent),
13848 filedata->dynamic_syminfo_offset,
13849 filedata->dynamic_syminfo_nent);
13850
13851 printf (_(" Num: Name BoundTo Flags\n"));
13852 for (i = 0; i < filedata->dynamic_syminfo_nent; ++i)
13853 {
13854 unsigned short int flags = filedata->dynamic_syminfo[i].si_flags;
13855
13856 printf ("%4d: ", i);
13857 if (i >= filedata->num_dynamic_syms)
13858 printf (_("<corrupt index>"));
13859 else if (valid_dynamic_name (filedata, filedata->dynamic_symbols[i].st_name))
13860 print_symbol (30, get_dynamic_name (filedata,
13861 filedata->dynamic_symbols[i].st_name));
13862 else
13863 printf (_("<corrupt: %19ld>"), filedata->dynamic_symbols[i].st_name);
13864 putchar (' ');
13865
13866 switch (filedata->dynamic_syminfo[i].si_boundto)
13867 {
13868 case SYMINFO_BT_SELF:
13869 fputs ("SELF ", stdout);
13870 break;
13871 case SYMINFO_BT_PARENT:
13872 fputs ("PARENT ", stdout);
13873 break;
13874 default:
13875 if (filedata->dynamic_syminfo[i].si_boundto > 0
13876 && filedata->dynamic_syminfo[i].si_boundto < filedata->dynamic_nent
13877 && valid_dynamic_name (filedata,
13878 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val))
13879 {
13880 print_symbol (10, get_dynamic_name (filedata,
13881 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val));
13882 putchar (' ' );
13883 }
13884 else
13885 printf ("%-10d ", filedata->dynamic_syminfo[i].si_boundto);
13886 break;
13887 }
13888
13889 if (flags & SYMINFO_FLG_DIRECT)
13890 printf (" DIRECT");
13891 if (flags & SYMINFO_FLG_PASSTHRU)
13892 printf (" PASSTHRU");
13893 if (flags & SYMINFO_FLG_COPY)
13894 printf (" COPY");
13895 if (flags & SYMINFO_FLG_LAZYLOAD)
13896 printf (" LAZYLOAD");
13897
13898 puts ("");
13899 }
13900
13901 return true;
13902 }
13903
13904 /* A macro which evaluates to TRUE if the region ADDR .. ADDR + NELEM
13905 is contained by the region START .. END. The types of ADDR, START
13906 and END should all be the same. Note both ADDR + NELEM and END
13907 point to just beyond the end of the regions that are being tested. */
13908 #define IN_RANGE(START,END,ADDR,NELEM) \
13909 (((ADDR) >= (START)) && ((ADDR) < (END)) && ((ADDR) + (NELEM) <= (END)))
13910
13911 /* Check to see if the given reloc needs to be handled in a target specific
13912 manner. If so then process the reloc and return TRUE otherwise return
13913 FALSE.
13914
13915 If called with reloc == NULL, then this is a signal that reloc processing
13916 for the current section has finished, and any saved state should be
13917 discarded. */
13918
13919 static bool
13920 target_specific_reloc_handling (Filedata * filedata,
13921 Elf_Internal_Rela * reloc,
13922 unsigned char * start,
13923 unsigned char * end,
13924 Elf_Internal_Sym * symtab,
13925 unsigned long num_syms)
13926 {
13927 unsigned int reloc_type = 0;
13928 unsigned long sym_index = 0;
13929
13930 if (reloc)
13931 {
13932 reloc_type = get_reloc_type (filedata, reloc->r_info);
13933 sym_index = get_reloc_symindex (reloc->r_info);
13934 }
13935
13936 switch (filedata->file_header.e_machine)
13937 {
13938 case EM_MSP430:
13939 case EM_MSP430_OLD:
13940 {
13941 static Elf_Internal_Sym * saved_sym = NULL;
13942
13943 if (reloc == NULL)
13944 {
13945 saved_sym = NULL;
13946 return true;
13947 }
13948
13949 switch (reloc_type)
13950 {
13951 case 10: /* R_MSP430_SYM_DIFF */
13952 case 12: /* R_MSP430_GNU_SUB_ULEB128 */
13953 if (uses_msp430x_relocs (filedata))
13954 break;
13955 /* Fall through. */
13956 case 21: /* R_MSP430X_SYM_DIFF */
13957 case 23: /* R_MSP430X_GNU_SUB_ULEB128 */
13958 /* PR 21139. */
13959 if (sym_index >= num_syms)
13960 error (_("MSP430 SYM_DIFF reloc contains invalid symbol index %lu\n"),
13961 sym_index);
13962 else
13963 saved_sym = symtab + sym_index;
13964 return true;
13965
13966 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
13967 case 3: /* R_MSP430_16 or R_MSP430_ABS8 */
13968 goto handle_sym_diff;
13969
13970 case 5: /* R_MSP430_16_BYTE */
13971 case 9: /* R_MSP430_8 */
13972 case 11: /* R_MSP430_GNU_SET_ULEB128 */
13973 if (uses_msp430x_relocs (filedata))
13974 break;
13975 goto handle_sym_diff;
13976
13977 case 2: /* R_MSP430_ABS16 */
13978 case 15: /* R_MSP430X_ABS16 */
13979 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
13980 if (! uses_msp430x_relocs (filedata))
13981 break;
13982 goto handle_sym_diff;
13983
13984 handle_sym_diff:
13985 if (saved_sym != NULL)
13986 {
13987 uint64_t value;
13988 unsigned int reloc_size = 0;
13989 int leb_ret = 0;
13990 switch (reloc_type)
13991 {
13992 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
13993 reloc_size = 4;
13994 break;
13995 case 11: /* R_MSP430_GNU_SET_ULEB128 */
13996 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
13997 if (reloc->r_offset < (size_t) (end - start))
13998 read_leb128 (start + reloc->r_offset, end, false,
13999 &reloc_size, &leb_ret);
14000 break;
14001 default:
14002 reloc_size = 2;
14003 break;
14004 }
14005
14006 if (leb_ret != 0 || reloc_size == 0 || reloc_size > 8)
14007 error (_("MSP430 ULEB128 field at 0x%lx contains invalid "
14008 "ULEB128 value\n"),
14009 (long) reloc->r_offset);
14010 else if (sym_index >= num_syms)
14011 error (_("MSP430 reloc contains invalid symbol index %lu\n"),
14012 sym_index);
14013 else
14014 {
14015 value = reloc->r_addend + (symtab[sym_index].st_value
14016 - saved_sym->st_value);
14017
14018 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
14019 byte_put (start + reloc->r_offset, value, reloc_size);
14020 else
14021 /* PR 21137 */
14022 error (_("MSP430 sym diff reloc contains invalid offset: 0x%lx\n"),
14023 (long) reloc->r_offset);
14024 }
14025
14026 saved_sym = NULL;
14027 return true;
14028 }
14029 break;
14030
14031 default:
14032 if (saved_sym != NULL)
14033 error (_("Unhandled MSP430 reloc type found after SYM_DIFF reloc\n"));
14034 break;
14035 }
14036 break;
14037 }
14038
14039 case EM_MN10300:
14040 case EM_CYGNUS_MN10300:
14041 {
14042 static Elf_Internal_Sym * saved_sym = NULL;
14043
14044 if (reloc == NULL)
14045 {
14046 saved_sym = NULL;
14047 return true;
14048 }
14049
14050 switch (reloc_type)
14051 {
14052 case 34: /* R_MN10300_ALIGN */
14053 return true;
14054 case 33: /* R_MN10300_SYM_DIFF */
14055 if (sym_index >= num_syms)
14056 error (_("MN10300_SYM_DIFF reloc contains invalid symbol index %lu\n"),
14057 sym_index);
14058 else
14059 saved_sym = symtab + sym_index;
14060 return true;
14061
14062 case 1: /* R_MN10300_32 */
14063 case 2: /* R_MN10300_16 */
14064 if (saved_sym != NULL)
14065 {
14066 int reloc_size = reloc_type == 1 ? 4 : 2;
14067 uint64_t value;
14068
14069 if (sym_index >= num_syms)
14070 error (_("MN10300 reloc contains invalid symbol index %lu\n"),
14071 sym_index);
14072 else
14073 {
14074 value = reloc->r_addend + (symtab[sym_index].st_value
14075 - saved_sym->st_value);
14076
14077 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
14078 byte_put (start + reloc->r_offset, value, reloc_size);
14079 else
14080 error (_("MN10300 sym diff reloc contains invalid offset: 0x%lx\n"),
14081 (long) reloc->r_offset);
14082 }
14083
14084 saved_sym = NULL;
14085 return true;
14086 }
14087 break;
14088 default:
14089 if (saved_sym != NULL)
14090 error (_("Unhandled MN10300 reloc type found after SYM_DIFF reloc\n"));
14091 break;
14092 }
14093 break;
14094 }
14095
14096 case EM_RL78:
14097 {
14098 static uint64_t saved_sym1 = 0;
14099 static uint64_t saved_sym2 = 0;
14100 static uint64_t value;
14101
14102 if (reloc == NULL)
14103 {
14104 saved_sym1 = saved_sym2 = 0;
14105 return true;
14106 }
14107
14108 switch (reloc_type)
14109 {
14110 case 0x80: /* R_RL78_SYM. */
14111 saved_sym1 = saved_sym2;
14112 if (sym_index >= num_syms)
14113 error (_("RL78_SYM reloc contains invalid symbol index %lu\n"),
14114 sym_index);
14115 else
14116 {
14117 saved_sym2 = symtab[sym_index].st_value;
14118 saved_sym2 += reloc->r_addend;
14119 }
14120 return true;
14121
14122 case 0x83: /* R_RL78_OPsub. */
14123 value = saved_sym1 - saved_sym2;
14124 saved_sym2 = saved_sym1 = 0;
14125 return true;
14126 break;
14127
14128 case 0x41: /* R_RL78_ABS32. */
14129 if (IN_RANGE (start, end, start + reloc->r_offset, 4))
14130 byte_put (start + reloc->r_offset, value, 4);
14131 else
14132 error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
14133 (long) reloc->r_offset);
14134 value = 0;
14135 return true;
14136
14137 case 0x43: /* R_RL78_ABS16. */
14138 if (IN_RANGE (start, end, start + reloc->r_offset, 2))
14139 byte_put (start + reloc->r_offset, value, 2);
14140 else
14141 error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
14142 (long) reloc->r_offset);
14143 value = 0;
14144 return true;
14145
14146 default:
14147 break;
14148 }
14149 break;
14150 }
14151 }
14152
14153 return false;
14154 }
14155
14156 /* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
14157 DWARF debug sections. This is a target specific test. Note - we do not
14158 go through the whole including-target-headers-multiple-times route, (as
14159 we have already done with <elf/h8.h>) because this would become very
14160 messy and even then this function would have to contain target specific
14161 information (the names of the relocs instead of their numeric values).
14162 FIXME: This is not the correct way to solve this problem. The proper way
14163 is to have target specific reloc sizing and typing functions created by
14164 the reloc-macros.h header, in the same way that it already creates the
14165 reloc naming functions. */
14166
14167 static bool
14168 is_32bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14169 {
14170 /* Please keep this table alpha-sorted for ease of visual lookup. */
14171 switch (filedata->file_header.e_machine)
14172 {
14173 case EM_386:
14174 case EM_IAMCU:
14175 return reloc_type == 1; /* R_386_32. */
14176 case EM_68K:
14177 return reloc_type == 1; /* R_68K_32. */
14178 case EM_860:
14179 return reloc_type == 1; /* R_860_32. */
14180 case EM_960:
14181 return reloc_type == 2; /* R_960_32. */
14182 case EM_AARCH64:
14183 return (reloc_type == 258
14184 || reloc_type == 1); /* R_AARCH64_ABS32 || R_AARCH64_P32_ABS32 */
14185 case EM_BPF:
14186 return reloc_type == 11; /* R_BPF_DATA_32 */
14187 case EM_ADAPTEVA_EPIPHANY:
14188 return reloc_type == 3;
14189 case EM_ALPHA:
14190 return reloc_type == 1; /* R_ALPHA_REFLONG. */
14191 case EM_ARC:
14192 return reloc_type == 1; /* R_ARC_32. */
14193 case EM_ARC_COMPACT:
14194 case EM_ARC_COMPACT2:
14195 return reloc_type == 4; /* R_ARC_32. */
14196 case EM_ARM:
14197 return reloc_type == 2; /* R_ARM_ABS32 */
14198 case EM_AVR_OLD:
14199 case EM_AVR:
14200 return reloc_type == 1;
14201 case EM_BLACKFIN:
14202 return reloc_type == 0x12; /* R_byte4_data. */
14203 case EM_CRIS:
14204 return reloc_type == 3; /* R_CRIS_32. */
14205 case EM_CR16:
14206 return reloc_type == 3; /* R_CR16_NUM32. */
14207 case EM_CRX:
14208 return reloc_type == 15; /* R_CRX_NUM32. */
14209 case EM_CSKY:
14210 return reloc_type == 1; /* R_CKCORE_ADDR32. */
14211 case EM_CYGNUS_FRV:
14212 return reloc_type == 1;
14213 case EM_CYGNUS_D10V:
14214 case EM_D10V:
14215 return reloc_type == 6; /* R_D10V_32. */
14216 case EM_CYGNUS_D30V:
14217 case EM_D30V:
14218 return reloc_type == 12; /* R_D30V_32_NORMAL. */
14219 case EM_DLX:
14220 return reloc_type == 3; /* R_DLX_RELOC_32. */
14221 case EM_CYGNUS_FR30:
14222 case EM_FR30:
14223 return reloc_type == 3; /* R_FR30_32. */
14224 case EM_FT32:
14225 return reloc_type == 1; /* R_FT32_32. */
14226 case EM_H8S:
14227 case EM_H8_300:
14228 case EM_H8_300H:
14229 return reloc_type == 1; /* R_H8_DIR32. */
14230 case EM_IA_64:
14231 return (reloc_type == 0x64 /* R_IA64_SECREL32MSB. */
14232 || reloc_type == 0x65 /* R_IA64_SECREL32LSB. */
14233 || reloc_type == 0x24 /* R_IA64_DIR32MSB. */
14234 || reloc_type == 0x25 /* R_IA64_DIR32LSB. */);
14235 case EM_IP2K_OLD:
14236 case EM_IP2K:
14237 return reloc_type == 2; /* R_IP2K_32. */
14238 case EM_IQ2000:
14239 return reloc_type == 2; /* R_IQ2000_32. */
14240 case EM_LATTICEMICO32:
14241 return reloc_type == 3; /* R_LM32_32. */
14242 case EM_LOONGARCH:
14243 return reloc_type == 1; /* R_LARCH_32. */
14244 case EM_M32C_OLD:
14245 case EM_M32C:
14246 return reloc_type == 3; /* R_M32C_32. */
14247 case EM_M32R:
14248 return reloc_type == 34; /* R_M32R_32_RELA. */
14249 case EM_68HC11:
14250 case EM_68HC12:
14251 return reloc_type == 6; /* R_M68HC11_32. */
14252 case EM_S12Z:
14253 return reloc_type == 7 || /* R_S12Z_EXT32 */
14254 reloc_type == 6; /* R_S12Z_CW32. */
14255 case EM_MCORE:
14256 return reloc_type == 1; /* R_MCORE_ADDR32. */
14257 case EM_CYGNUS_MEP:
14258 return reloc_type == 4; /* R_MEP_32. */
14259 case EM_METAG:
14260 return reloc_type == 2; /* R_METAG_ADDR32. */
14261 case EM_MICROBLAZE:
14262 return reloc_type == 1; /* R_MICROBLAZE_32. */
14263 case EM_MIPS:
14264 return reloc_type == 2; /* R_MIPS_32. */
14265 case EM_MMIX:
14266 return reloc_type == 4; /* R_MMIX_32. */
14267 case EM_CYGNUS_MN10200:
14268 case EM_MN10200:
14269 return reloc_type == 1; /* R_MN10200_32. */
14270 case EM_CYGNUS_MN10300:
14271 case EM_MN10300:
14272 return reloc_type == 1; /* R_MN10300_32. */
14273 case EM_MOXIE:
14274 return reloc_type == 1; /* R_MOXIE_32. */
14275 case EM_MSP430_OLD:
14276 case EM_MSP430:
14277 return reloc_type == 1; /* R_MSP430_32 or R_MSP320_ABS32. */
14278 case EM_MT:
14279 return reloc_type == 2; /* R_MT_32. */
14280 case EM_NDS32:
14281 return reloc_type == 20; /* R_NDS32_32_RELA. */
14282 case EM_ALTERA_NIOS2:
14283 return reloc_type == 12; /* R_NIOS2_BFD_RELOC_32. */
14284 case EM_NIOS32:
14285 return reloc_type == 1; /* R_NIOS_32. */
14286 case EM_OR1K:
14287 return reloc_type == 1; /* R_OR1K_32. */
14288 case EM_PARISC:
14289 return (reloc_type == 1 /* R_PARISC_DIR32. */
14290 || reloc_type == 2 /* R_PARISC_DIR21L. */
14291 || reloc_type == 41); /* R_PARISC_SECREL32. */
14292 case EM_PJ:
14293 case EM_PJ_OLD:
14294 return reloc_type == 1; /* R_PJ_DATA_DIR32. */
14295 case EM_PPC64:
14296 return reloc_type == 1; /* R_PPC64_ADDR32. */
14297 case EM_PPC:
14298 return reloc_type == 1; /* R_PPC_ADDR32. */
14299 case EM_TI_PRU:
14300 return reloc_type == 11; /* R_PRU_BFD_RELOC_32. */
14301 case EM_RISCV:
14302 return reloc_type == 1; /* R_RISCV_32. */
14303 case EM_RL78:
14304 return reloc_type == 1; /* R_RL78_DIR32. */
14305 case EM_RX:
14306 return reloc_type == 1; /* R_RX_DIR32. */
14307 case EM_S370:
14308 return reloc_type == 1; /* R_I370_ADDR31. */
14309 case EM_S390_OLD:
14310 case EM_S390:
14311 return reloc_type == 4; /* R_S390_32. */
14312 case EM_SCORE:
14313 return reloc_type == 8; /* R_SCORE_ABS32. */
14314 case EM_SH:
14315 return reloc_type == 1; /* R_SH_DIR32. */
14316 case EM_SPARC32PLUS:
14317 case EM_SPARCV9:
14318 case EM_SPARC:
14319 return reloc_type == 3 /* R_SPARC_32. */
14320 || reloc_type == 23; /* R_SPARC_UA32. */
14321 case EM_SPU:
14322 return reloc_type == 6; /* R_SPU_ADDR32 */
14323 case EM_TI_C6000:
14324 return reloc_type == 1; /* R_C6000_ABS32. */
14325 case EM_TILEGX:
14326 return reloc_type == 2; /* R_TILEGX_32. */
14327 case EM_TILEPRO:
14328 return reloc_type == 1; /* R_TILEPRO_32. */
14329 case EM_CYGNUS_V850:
14330 case EM_V850:
14331 return reloc_type == 6; /* R_V850_ABS32. */
14332 case EM_V800:
14333 return reloc_type == 0x33; /* R_V810_WORD. */
14334 case EM_VAX:
14335 return reloc_type == 1; /* R_VAX_32. */
14336 case EM_VISIUM:
14337 return reloc_type == 3; /* R_VISIUM_32. */
14338 case EM_WEBASSEMBLY:
14339 return reloc_type == 1; /* R_WASM32_32. */
14340 case EM_X86_64:
14341 case EM_L1OM:
14342 case EM_K1OM:
14343 return reloc_type == 10; /* R_X86_64_32. */
14344 case EM_XGATE:
14345 return reloc_type == 4; /* R_XGATE_32. */
14346 case EM_XSTORMY16:
14347 return reloc_type == 1; /* R_XSTROMY16_32. */
14348 case EM_XTENSA_OLD:
14349 case EM_XTENSA:
14350 return reloc_type == 1; /* R_XTENSA_32. */
14351 case EM_Z80:
14352 return reloc_type == 6; /* R_Z80_32. */
14353 default:
14354 {
14355 static unsigned int prev_warn = 0;
14356
14357 /* Avoid repeating the same warning multiple times. */
14358 if (prev_warn != filedata->file_header.e_machine)
14359 error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
14360 filedata->file_header.e_machine);
14361 prev_warn = filedata->file_header.e_machine;
14362 return false;
14363 }
14364 }
14365 }
14366
14367 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14368 a 32-bit pc-relative RELA relocation used in DWARF debug sections. */
14369
14370 static bool
14371 is_32bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
14372 {
14373 switch (filedata->file_header.e_machine)
14374 /* Please keep this table alpha-sorted for ease of visual lookup. */
14375 {
14376 case EM_386:
14377 case EM_IAMCU:
14378 return reloc_type == 2; /* R_386_PC32. */
14379 case EM_68K:
14380 return reloc_type == 4; /* R_68K_PC32. */
14381 case EM_AARCH64:
14382 return reloc_type == 261; /* R_AARCH64_PREL32 */
14383 case EM_ADAPTEVA_EPIPHANY:
14384 return reloc_type == 6;
14385 case EM_ALPHA:
14386 return reloc_type == 10; /* R_ALPHA_SREL32. */
14387 case EM_ARC_COMPACT:
14388 case EM_ARC_COMPACT2:
14389 return reloc_type == 49; /* R_ARC_32_PCREL. */
14390 case EM_ARM:
14391 return reloc_type == 3; /* R_ARM_REL32 */
14392 case EM_AVR_OLD:
14393 case EM_AVR:
14394 return reloc_type == 36; /* R_AVR_32_PCREL. */
14395 case EM_LOONGARCH:
14396 return reloc_type == 99; /* R_LARCH_32_PCREL. */
14397 case EM_MICROBLAZE:
14398 return reloc_type == 2; /* R_MICROBLAZE_32_PCREL. */
14399 case EM_OR1K:
14400 return reloc_type == 9; /* R_OR1K_32_PCREL. */
14401 case EM_PARISC:
14402 return reloc_type == 9; /* R_PARISC_PCREL32. */
14403 case EM_PPC:
14404 return reloc_type == 26; /* R_PPC_REL32. */
14405 case EM_PPC64:
14406 return reloc_type == 26; /* R_PPC64_REL32. */
14407 case EM_RISCV:
14408 return reloc_type == 57; /* R_RISCV_32_PCREL. */
14409 case EM_S390_OLD:
14410 case EM_S390:
14411 return reloc_type == 5; /* R_390_PC32. */
14412 case EM_SH:
14413 return reloc_type == 2; /* R_SH_REL32. */
14414 case EM_SPARC32PLUS:
14415 case EM_SPARCV9:
14416 case EM_SPARC:
14417 return reloc_type == 6; /* R_SPARC_DISP32. */
14418 case EM_SPU:
14419 return reloc_type == 13; /* R_SPU_REL32. */
14420 case EM_TILEGX:
14421 return reloc_type == 6; /* R_TILEGX_32_PCREL. */
14422 case EM_TILEPRO:
14423 return reloc_type == 4; /* R_TILEPRO_32_PCREL. */
14424 case EM_VISIUM:
14425 return reloc_type == 6; /* R_VISIUM_32_PCREL */
14426 case EM_X86_64:
14427 case EM_L1OM:
14428 case EM_K1OM:
14429 return reloc_type == 2; /* R_X86_64_PC32. */
14430 case EM_VAX:
14431 return reloc_type == 4; /* R_VAX_PCREL32. */
14432 case EM_XTENSA_OLD:
14433 case EM_XTENSA:
14434 return reloc_type == 14; /* R_XTENSA_32_PCREL. */
14435 default:
14436 /* Do not abort or issue an error message here. Not all targets use
14437 pc-relative 32-bit relocs in their DWARF debug information and we
14438 have already tested for target coverage in is_32bit_abs_reloc. A
14439 more helpful warning message will be generated by apply_relocations
14440 anyway, so just return. */
14441 return false;
14442 }
14443 }
14444
14445 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14446 a 64-bit absolute RELA relocation used in DWARF debug sections. */
14447
14448 static bool
14449 is_64bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14450 {
14451 switch (filedata->file_header.e_machine)
14452 {
14453 case EM_AARCH64:
14454 return reloc_type == 257; /* R_AARCH64_ABS64. */
14455 case EM_ALPHA:
14456 return reloc_type == 2; /* R_ALPHA_REFQUAD. */
14457 case EM_IA_64:
14458 return (reloc_type == 0x26 /* R_IA64_DIR64MSB. */
14459 || reloc_type == 0x27 /* R_IA64_DIR64LSB. */);
14460 case EM_LOONGARCH:
14461 return reloc_type == 2; /* R_LARCH_64 */
14462 case EM_PARISC:
14463 return reloc_type == 80; /* R_PARISC_DIR64. */
14464 case EM_PPC64:
14465 return reloc_type == 38; /* R_PPC64_ADDR64. */
14466 case EM_RISCV:
14467 return reloc_type == 2; /* R_RISCV_64. */
14468 case EM_SPARC32PLUS:
14469 case EM_SPARCV9:
14470 case EM_SPARC:
14471 return reloc_type == 32 /* R_SPARC_64. */
14472 || reloc_type == 54; /* R_SPARC_UA64. */
14473 case EM_X86_64:
14474 case EM_L1OM:
14475 case EM_K1OM:
14476 return reloc_type == 1; /* R_X86_64_64. */
14477 case EM_S390_OLD:
14478 case EM_S390:
14479 return reloc_type == 22; /* R_S390_64. */
14480 case EM_TILEGX:
14481 return reloc_type == 1; /* R_TILEGX_64. */
14482 case EM_MIPS:
14483 return reloc_type == 18; /* R_MIPS_64. */
14484 default:
14485 return false;
14486 }
14487 }
14488
14489 /* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
14490 a 64-bit pc-relative RELA relocation used in DWARF debug sections. */
14491
14492 static bool
14493 is_64bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
14494 {
14495 switch (filedata->file_header.e_machine)
14496 {
14497 case EM_AARCH64:
14498 return reloc_type == 260; /* R_AARCH64_PREL64. */
14499 case EM_ALPHA:
14500 return reloc_type == 11; /* R_ALPHA_SREL64. */
14501 case EM_IA_64:
14502 return (reloc_type == 0x4e /* R_IA64_PCREL64MSB. */
14503 || reloc_type == 0x4f /* R_IA64_PCREL64LSB. */);
14504 case EM_PARISC:
14505 return reloc_type == 72; /* R_PARISC_PCREL64. */
14506 case EM_PPC64:
14507 return reloc_type == 44; /* R_PPC64_REL64. */
14508 case EM_SPARC32PLUS:
14509 case EM_SPARCV9:
14510 case EM_SPARC:
14511 return reloc_type == 46; /* R_SPARC_DISP64. */
14512 case EM_X86_64:
14513 case EM_L1OM:
14514 case EM_K1OM:
14515 return reloc_type == 24; /* R_X86_64_PC64. */
14516 case EM_S390_OLD:
14517 case EM_S390:
14518 return reloc_type == 23; /* R_S390_PC64. */
14519 case EM_TILEGX:
14520 return reloc_type == 5; /* R_TILEGX_64_PCREL. */
14521 default:
14522 return false;
14523 }
14524 }
14525
14526 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14527 a 24-bit absolute RELA relocation used in DWARF debug sections. */
14528
14529 static bool
14530 is_24bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14531 {
14532 switch (filedata->file_header.e_machine)
14533 {
14534 case EM_CYGNUS_MN10200:
14535 case EM_MN10200:
14536 return reloc_type == 4; /* R_MN10200_24. */
14537 case EM_FT32:
14538 return reloc_type == 5; /* R_FT32_20. */
14539 case EM_Z80:
14540 return reloc_type == 5; /* R_Z80_24. */
14541 default:
14542 return false;
14543 }
14544 }
14545
14546 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14547 a 16-bit absolute RELA relocation used in DWARF debug sections. */
14548
14549 static bool
14550 is_16bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14551 {
14552 /* Please keep this table alpha-sorted for ease of visual lookup. */
14553 switch (filedata->file_header.e_machine)
14554 {
14555 case EM_ARC:
14556 case EM_ARC_COMPACT:
14557 case EM_ARC_COMPACT2:
14558 return reloc_type == 2; /* R_ARC_16. */
14559 case EM_ADAPTEVA_EPIPHANY:
14560 return reloc_type == 5;
14561 case EM_AVR_OLD:
14562 case EM_AVR:
14563 return reloc_type == 4; /* R_AVR_16. */
14564 case EM_CYGNUS_D10V:
14565 case EM_D10V:
14566 return reloc_type == 3; /* R_D10V_16. */
14567 case EM_FT32:
14568 return reloc_type == 2; /* R_FT32_16. */
14569 case EM_H8S:
14570 case EM_H8_300:
14571 case EM_H8_300H:
14572 return reloc_type == R_H8_DIR16;
14573 case EM_IP2K_OLD:
14574 case EM_IP2K:
14575 return reloc_type == 1; /* R_IP2K_16. */
14576 case EM_M32C_OLD:
14577 case EM_M32C:
14578 return reloc_type == 1; /* R_M32C_16 */
14579 case EM_CYGNUS_MN10200:
14580 case EM_MN10200:
14581 return reloc_type == 2; /* R_MN10200_16. */
14582 case EM_CYGNUS_MN10300:
14583 case EM_MN10300:
14584 return reloc_type == 2; /* R_MN10300_16. */
14585 case EM_MSP430:
14586 if (uses_msp430x_relocs (filedata))
14587 return reloc_type == 2; /* R_MSP430_ABS16. */
14588 /* Fall through. */
14589 case EM_MSP430_OLD:
14590 return reloc_type == 5; /* R_MSP430_16_BYTE. */
14591 case EM_NDS32:
14592 return reloc_type == 19; /* R_NDS32_16_RELA. */
14593 case EM_ALTERA_NIOS2:
14594 return reloc_type == 13; /* R_NIOS2_BFD_RELOC_16. */
14595 case EM_NIOS32:
14596 return reloc_type == 9; /* R_NIOS_16. */
14597 case EM_OR1K:
14598 return reloc_type == 2; /* R_OR1K_16. */
14599 case EM_RISCV:
14600 return reloc_type == 55; /* R_RISCV_SET16. */
14601 case EM_TI_PRU:
14602 return reloc_type == 8; /* R_PRU_BFD_RELOC_16. */
14603 case EM_TI_C6000:
14604 return reloc_type == 2; /* R_C6000_ABS16. */
14605 case EM_VISIUM:
14606 return reloc_type == 2; /* R_VISIUM_16. */
14607 case EM_XGATE:
14608 return reloc_type == 3; /* R_XGATE_16. */
14609 case EM_Z80:
14610 return reloc_type == 4; /* R_Z80_16. */
14611 default:
14612 return false;
14613 }
14614 }
14615
14616 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14617 a 8-bit absolute RELA relocation used in DWARF debug sections. */
14618
14619 static bool
14620 is_8bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14621 {
14622 switch (filedata->file_header.e_machine)
14623 {
14624 case EM_RISCV:
14625 return reloc_type == 54; /* R_RISCV_SET8. */
14626 case EM_Z80:
14627 return reloc_type == 1; /* R_Z80_8. */
14628 default:
14629 return false;
14630 }
14631 }
14632
14633 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14634 a 6-bit absolute RELA relocation used in DWARF debug sections. */
14635
14636 static bool
14637 is_6bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14638 {
14639 switch (filedata->file_header.e_machine)
14640 {
14641 case EM_RISCV:
14642 return reloc_type == 53; /* R_RISCV_SET6. */
14643 default:
14644 return false;
14645 }
14646 }
14647
14648 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14649 a 32-bit inplace add RELA relocation used in DWARF debug sections. */
14650
14651 static bool
14652 is_32bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14653 {
14654 /* Please keep this table alpha-sorted for ease of visual lookup. */
14655 switch (filedata->file_header.e_machine)
14656 {
14657 case EM_RISCV:
14658 return reloc_type == 35; /* R_RISCV_ADD32. */
14659 default:
14660 return false;
14661 }
14662 }
14663
14664 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14665 a 32-bit inplace sub RELA relocation used in DWARF debug sections. */
14666
14667 static bool
14668 is_32bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14669 {
14670 /* Please keep this table alpha-sorted for ease of visual lookup. */
14671 switch (filedata->file_header.e_machine)
14672 {
14673 case EM_RISCV:
14674 return reloc_type == 39; /* R_RISCV_SUB32. */
14675 default:
14676 return false;
14677 }
14678 }
14679
14680 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14681 a 64-bit inplace add RELA relocation used in DWARF debug sections. */
14682
14683 static bool
14684 is_64bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14685 {
14686 /* Please keep this table alpha-sorted for ease of visual lookup. */
14687 switch (filedata->file_header.e_machine)
14688 {
14689 case EM_RISCV:
14690 return reloc_type == 36; /* R_RISCV_ADD64. */
14691 default:
14692 return false;
14693 }
14694 }
14695
14696 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14697 a 64-bit inplace sub RELA relocation used in DWARF debug sections. */
14698
14699 static bool
14700 is_64bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14701 {
14702 /* Please keep this table alpha-sorted for ease of visual lookup. */
14703 switch (filedata->file_header.e_machine)
14704 {
14705 case EM_RISCV:
14706 return reloc_type == 40; /* R_RISCV_SUB64. */
14707 default:
14708 return false;
14709 }
14710 }
14711
14712 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14713 a 16-bit inplace add RELA relocation used in DWARF debug sections. */
14714
14715 static bool
14716 is_16bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14717 {
14718 /* Please keep this table alpha-sorted for ease of visual lookup. */
14719 switch (filedata->file_header.e_machine)
14720 {
14721 case EM_RISCV:
14722 return reloc_type == 34; /* R_RISCV_ADD16. */
14723 default:
14724 return false;
14725 }
14726 }
14727
14728 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14729 a 16-bit inplace sub RELA relocation used in DWARF debug sections. */
14730
14731 static bool
14732 is_16bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14733 {
14734 /* Please keep this table alpha-sorted for ease of visual lookup. */
14735 switch (filedata->file_header.e_machine)
14736 {
14737 case EM_RISCV:
14738 return reloc_type == 38; /* R_RISCV_SUB16. */
14739 default:
14740 return false;
14741 }
14742 }
14743
14744 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14745 a 8-bit inplace add RELA relocation used in DWARF debug sections. */
14746
14747 static bool
14748 is_8bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14749 {
14750 /* Please keep this table alpha-sorted for ease of visual lookup. */
14751 switch (filedata->file_header.e_machine)
14752 {
14753 case EM_RISCV:
14754 return reloc_type == 33; /* R_RISCV_ADD8. */
14755 default:
14756 return false;
14757 }
14758 }
14759
14760 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14761 a 8-bit inplace sub RELA relocation used in DWARF debug sections. */
14762
14763 static bool
14764 is_8bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14765 {
14766 /* Please keep this table alpha-sorted for ease of visual lookup. */
14767 switch (filedata->file_header.e_machine)
14768 {
14769 case EM_RISCV:
14770 return reloc_type == 37; /* R_RISCV_SUB8. */
14771 default:
14772 return false;
14773 }
14774 }
14775
14776 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14777 a 6-bit inplace sub RELA relocation used in DWARF debug sections. */
14778
14779 static bool
14780 is_6bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14781 {
14782 switch (filedata->file_header.e_machine)
14783 {
14784 case EM_RISCV:
14785 return reloc_type == 52; /* R_RISCV_SUB6. */
14786 default:
14787 return false;
14788 }
14789 }
14790
14791 /* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
14792 relocation entries (possibly formerly used for SHT_GROUP sections). */
14793
14794 static bool
14795 is_none_reloc (Filedata * filedata, unsigned int reloc_type)
14796 {
14797 switch (filedata->file_header.e_machine)
14798 {
14799 case EM_386: /* R_386_NONE. */
14800 case EM_68K: /* R_68K_NONE. */
14801 case EM_ADAPTEVA_EPIPHANY:
14802 case EM_ALPHA: /* R_ALPHA_NONE. */
14803 case EM_ALTERA_NIOS2: /* R_NIOS2_NONE. */
14804 case EM_ARC: /* R_ARC_NONE. */
14805 case EM_ARC_COMPACT2: /* R_ARC_NONE. */
14806 case EM_ARC_COMPACT: /* R_ARC_NONE. */
14807 case EM_ARM: /* R_ARM_NONE. */
14808 case EM_CRIS: /* R_CRIS_NONE. */
14809 case EM_FT32: /* R_FT32_NONE. */
14810 case EM_IA_64: /* R_IA64_NONE. */
14811 case EM_K1OM: /* R_X86_64_NONE. */
14812 case EM_L1OM: /* R_X86_64_NONE. */
14813 case EM_M32R: /* R_M32R_NONE. */
14814 case EM_MIPS: /* R_MIPS_NONE. */
14815 case EM_MN10300: /* R_MN10300_NONE. */
14816 case EM_MOXIE: /* R_MOXIE_NONE. */
14817 case EM_NIOS32: /* R_NIOS_NONE. */
14818 case EM_OR1K: /* R_OR1K_NONE. */
14819 case EM_PARISC: /* R_PARISC_NONE. */
14820 case EM_PPC64: /* R_PPC64_NONE. */
14821 case EM_PPC: /* R_PPC_NONE. */
14822 case EM_RISCV: /* R_RISCV_NONE. */
14823 case EM_S390: /* R_390_NONE. */
14824 case EM_S390_OLD:
14825 case EM_SH: /* R_SH_NONE. */
14826 case EM_SPARC32PLUS:
14827 case EM_SPARC: /* R_SPARC_NONE. */
14828 case EM_SPARCV9:
14829 case EM_TILEGX: /* R_TILEGX_NONE. */
14830 case EM_TILEPRO: /* R_TILEPRO_NONE. */
14831 case EM_TI_C6000:/* R_C6000_NONE. */
14832 case EM_X86_64: /* R_X86_64_NONE. */
14833 case EM_Z80: /* R_Z80_NONE. */
14834 case EM_WEBASSEMBLY: /* R_WASM32_NONE. */
14835 return reloc_type == 0;
14836
14837 case EM_AARCH64:
14838 return reloc_type == 0 || reloc_type == 256;
14839 case EM_AVR_OLD:
14840 case EM_AVR:
14841 return (reloc_type == 0 /* R_AVR_NONE. */
14842 || reloc_type == 30 /* R_AVR_DIFF8. */
14843 || reloc_type == 31 /* R_AVR_DIFF16. */
14844 || reloc_type == 32 /* R_AVR_DIFF32. */);
14845 case EM_METAG:
14846 return reloc_type == 3; /* R_METAG_NONE. */
14847 case EM_NDS32:
14848 return (reloc_type == 0 /* R_NDS32_NONE. */
14849 || reloc_type == 205 /* R_NDS32_DIFF8. */
14850 || reloc_type == 206 /* R_NDS32_DIFF16. */
14851 || reloc_type == 207 /* R_NDS32_DIFF32. */
14852 || reloc_type == 208 /* R_NDS32_DIFF_ULEB128. */);
14853 case EM_TI_PRU:
14854 return (reloc_type == 0 /* R_PRU_NONE. */
14855 || reloc_type == 65 /* R_PRU_DIFF8. */
14856 || reloc_type == 66 /* R_PRU_DIFF16. */
14857 || reloc_type == 67 /* R_PRU_DIFF32. */);
14858 case EM_XTENSA_OLD:
14859 case EM_XTENSA:
14860 return (reloc_type == 0 /* R_XTENSA_NONE. */
14861 || reloc_type == 17 /* R_XTENSA_DIFF8. */
14862 || reloc_type == 18 /* R_XTENSA_DIFF16. */
14863 || reloc_type == 19 /* R_XTENSA_DIFF32. */
14864 || reloc_type == 57 /* R_XTENSA_PDIFF8. */
14865 || reloc_type == 58 /* R_XTENSA_PDIFF16. */
14866 || reloc_type == 59 /* R_XTENSA_PDIFF32. */
14867 || reloc_type == 60 /* R_XTENSA_NDIFF8. */
14868 || reloc_type == 61 /* R_XTENSA_NDIFF16. */
14869 || reloc_type == 62 /* R_XTENSA_NDIFF32. */);
14870 }
14871 return false;
14872 }
14873
14874 /* Returns TRUE if there is a relocation against
14875 section NAME at OFFSET bytes. */
14876
14877 bool
14878 reloc_at (struct dwarf_section * dsec, uint64_t offset)
14879 {
14880 Elf_Internal_Rela * relocs;
14881 Elf_Internal_Rela * rp;
14882
14883 if (dsec == NULL || dsec->reloc_info == NULL)
14884 return false;
14885
14886 relocs = (Elf_Internal_Rela *) dsec->reloc_info;
14887
14888 for (rp = relocs; rp < relocs + dsec->num_relocs; ++rp)
14889 if (rp->r_offset == offset)
14890 return true;
14891
14892 return false;
14893 }
14894
14895 /* Apply relocations to a section.
14896 Returns TRUE upon success, FALSE otherwise.
14897 If RELOCS_RETURN is non-NULL then it is set to point to the loaded relocs.
14898 It is then the caller's responsibility to free them. NUM_RELOCS_RETURN
14899 will be set to the number of relocs loaded.
14900
14901 Note: So far support has been added only for those relocations
14902 which can be found in debug sections. FIXME: Add support for
14903 more relocations ? */
14904
14905 static bool
14906 apply_relocations (Filedata *filedata,
14907 const Elf_Internal_Shdr *section,
14908 unsigned char *start,
14909 size_t size,
14910 void **relocs_return,
14911 unsigned long *num_relocs_return)
14912 {
14913 Elf_Internal_Shdr * relsec;
14914 unsigned char * end = start + size;
14915
14916 if (relocs_return != NULL)
14917 {
14918 * (Elf_Internal_Rela **) relocs_return = NULL;
14919 * num_relocs_return = 0;
14920 }
14921
14922 if (filedata->file_header.e_type != ET_REL)
14923 /* No relocs to apply. */
14924 return true;
14925
14926 /* Find the reloc section associated with the section. */
14927 for (relsec = filedata->section_headers;
14928 relsec < filedata->section_headers + filedata->file_header.e_shnum;
14929 ++relsec)
14930 {
14931 bool is_rela;
14932 unsigned long num_relocs;
14933 Elf_Internal_Rela * relocs;
14934 Elf_Internal_Rela * rp;
14935 Elf_Internal_Shdr * symsec;
14936 Elf_Internal_Sym * symtab;
14937 unsigned long num_syms;
14938 Elf_Internal_Sym * sym;
14939
14940 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
14941 || relsec->sh_info >= filedata->file_header.e_shnum
14942 || filedata->section_headers + relsec->sh_info != section
14943 || relsec->sh_size == 0
14944 || relsec->sh_link >= filedata->file_header.e_shnum)
14945 continue;
14946
14947 symsec = filedata->section_headers + relsec->sh_link;
14948 if (symsec->sh_type != SHT_SYMTAB
14949 && symsec->sh_type != SHT_DYNSYM)
14950 return false;
14951
14952 is_rela = relsec->sh_type == SHT_RELA;
14953
14954 if (is_rela)
14955 {
14956 if (!slurp_rela_relocs (filedata, relsec->sh_offset,
14957 relsec->sh_size, & relocs, & num_relocs))
14958 return false;
14959 }
14960 else
14961 {
14962 if (!slurp_rel_relocs (filedata, relsec->sh_offset,
14963 relsec->sh_size, & relocs, & num_relocs))
14964 return false;
14965 }
14966
14967 /* SH uses RELA but uses in place value instead of the addend field. */
14968 if (filedata->file_header.e_machine == EM_SH)
14969 is_rela = false;
14970
14971 symtab = get_elf_symbols (filedata, symsec, & num_syms);
14972
14973 for (rp = relocs; rp < relocs + num_relocs; ++rp)
14974 {
14975 uint64_t addend;
14976 unsigned int reloc_type;
14977 unsigned int reloc_size;
14978 bool reloc_inplace = false;
14979 bool reloc_subtract = false;
14980 unsigned char *rloc;
14981 unsigned long sym_index;
14982
14983 reloc_type = get_reloc_type (filedata, rp->r_info);
14984
14985 if (target_specific_reloc_handling (filedata, rp, start, end, symtab, num_syms))
14986 continue;
14987 else if (is_none_reloc (filedata, reloc_type))
14988 continue;
14989 else if (is_32bit_abs_reloc (filedata, reloc_type)
14990 || is_32bit_pcrel_reloc (filedata, reloc_type))
14991 reloc_size = 4;
14992 else if (is_64bit_abs_reloc (filedata, reloc_type)
14993 || is_64bit_pcrel_reloc (filedata, reloc_type))
14994 reloc_size = 8;
14995 else if (is_24bit_abs_reloc (filedata, reloc_type))
14996 reloc_size = 3;
14997 else if (is_16bit_abs_reloc (filedata, reloc_type))
14998 reloc_size = 2;
14999 else if (is_8bit_abs_reloc (filedata, reloc_type)
15000 || is_6bit_abs_reloc (filedata, reloc_type))
15001 reloc_size = 1;
15002 else if ((reloc_subtract = is_32bit_inplace_sub_reloc (filedata,
15003 reloc_type))
15004 || is_32bit_inplace_add_reloc (filedata, reloc_type))
15005 {
15006 reloc_size = 4;
15007 reloc_inplace = true;
15008 }
15009 else if ((reloc_subtract = is_64bit_inplace_sub_reloc (filedata,
15010 reloc_type))
15011 || is_64bit_inplace_add_reloc (filedata, reloc_type))
15012 {
15013 reloc_size = 8;
15014 reloc_inplace = true;
15015 }
15016 else if ((reloc_subtract = is_16bit_inplace_sub_reloc (filedata,
15017 reloc_type))
15018 || is_16bit_inplace_add_reloc (filedata, reloc_type))
15019 {
15020 reloc_size = 2;
15021 reloc_inplace = true;
15022 }
15023 else if ((reloc_subtract = is_8bit_inplace_sub_reloc (filedata,
15024 reloc_type))
15025 || is_8bit_inplace_add_reloc (filedata, reloc_type))
15026 {
15027 reloc_size = 1;
15028 reloc_inplace = true;
15029 }
15030 else if ((reloc_subtract = is_6bit_inplace_sub_reloc (filedata,
15031 reloc_type)))
15032 {
15033 reloc_size = 1;
15034 reloc_inplace = true;
15035 }
15036 else
15037 {
15038 static unsigned int prev_reloc = 0;
15039
15040 if (reloc_type != prev_reloc)
15041 warn (_("unable to apply unsupported reloc type %d to section %s\n"),
15042 reloc_type, printable_section_name (filedata, section));
15043 prev_reloc = reloc_type;
15044 continue;
15045 }
15046
15047 rloc = start + rp->r_offset;
15048 if (!IN_RANGE (start, end, rloc, reloc_size))
15049 {
15050 warn (_("skipping invalid relocation offset 0x%lx in section %s\n"),
15051 (unsigned long) rp->r_offset,
15052 printable_section_name (filedata, section));
15053 continue;
15054 }
15055
15056 sym_index = (unsigned long) get_reloc_symindex (rp->r_info);
15057 if (sym_index >= num_syms)
15058 {
15059 warn (_("skipping invalid relocation symbol index 0x%lx in section %s\n"),
15060 sym_index, printable_section_name (filedata, section));
15061 continue;
15062 }
15063 sym = symtab + sym_index;
15064
15065 /* If the reloc has a symbol associated with it,
15066 make sure that it is of an appropriate type.
15067
15068 Relocations against symbols without type can happen.
15069 Gcc -feliminate-dwarf2-dups may generate symbols
15070 without type for debug info.
15071
15072 Icc generates relocations against function symbols
15073 instead of local labels.
15074
15075 Relocations against object symbols can happen, eg when
15076 referencing a global array. For an example of this see
15077 the _clz.o binary in libgcc.a. */
15078 if (sym != symtab
15079 && ELF_ST_TYPE (sym->st_info) != STT_COMMON
15080 && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
15081 {
15082 warn (_("skipping unexpected symbol type %s in section %s relocation %ld\n"),
15083 get_symbol_type (filedata, ELF_ST_TYPE (sym->st_info)),
15084 printable_section_name (filedata, relsec),
15085 (long int)(rp - relocs));
15086 continue;
15087 }
15088
15089 addend = 0;
15090 if (is_rela)
15091 addend += rp->r_addend;
15092 /* R_XTENSA_32, R_PJ_DATA_DIR32 and R_D30V_32_NORMAL are
15093 partial_inplace. */
15094 if (!is_rela
15095 || (filedata->file_header.e_machine == EM_XTENSA
15096 && reloc_type == 1)
15097 || ((filedata->file_header.e_machine == EM_PJ
15098 || filedata->file_header.e_machine == EM_PJ_OLD)
15099 && reloc_type == 1)
15100 || ((filedata->file_header.e_machine == EM_D30V
15101 || filedata->file_header.e_machine == EM_CYGNUS_D30V)
15102 && reloc_type == 12)
15103 || reloc_inplace)
15104 {
15105 if (is_6bit_inplace_sub_reloc (filedata, reloc_type))
15106 addend += byte_get (rloc, reloc_size) & 0x3f;
15107 else
15108 addend += byte_get (rloc, reloc_size);
15109 }
15110
15111 if (is_32bit_pcrel_reloc (filedata, reloc_type)
15112 || is_64bit_pcrel_reloc (filedata, reloc_type))
15113 {
15114 /* On HPPA, all pc-relative relocations are biased by 8. */
15115 if (filedata->file_header.e_machine == EM_PARISC)
15116 addend -= 8;
15117 byte_put (rloc, (addend + sym->st_value) - rp->r_offset,
15118 reloc_size);
15119 }
15120 else if (is_6bit_abs_reloc (filedata, reloc_type)
15121 || is_6bit_inplace_sub_reloc (filedata, reloc_type))
15122 {
15123 if (reloc_subtract)
15124 addend -= sym->st_value;
15125 else
15126 addend += sym->st_value;
15127 addend = (addend & 0x3f) | (byte_get (rloc, reloc_size) & 0xc0);
15128 byte_put (rloc, addend, reloc_size);
15129 }
15130 else if (reloc_subtract)
15131 byte_put (rloc, addend - sym->st_value, reloc_size);
15132 else
15133 byte_put (rloc, addend + sym->st_value, reloc_size);
15134 }
15135
15136 free (symtab);
15137 /* Let the target specific reloc processing code know that
15138 we have finished with these relocs. */
15139 target_specific_reloc_handling (filedata, NULL, NULL, NULL, NULL, 0);
15140
15141 if (relocs_return)
15142 {
15143 * (Elf_Internal_Rela **) relocs_return = relocs;
15144 * num_relocs_return = num_relocs;
15145 }
15146 else
15147 free (relocs);
15148
15149 break;
15150 }
15151
15152 return true;
15153 }
15154
15155 #ifdef SUPPORT_DISASSEMBLY
15156 static bool
15157 disassemble_section (Elf_Internal_Shdr * section, Filedata * filedata)
15158 {
15159 printf (_("\nAssembly dump of section %s\n"), printable_section_name (filedata, section));
15160
15161 /* FIXME: XXX -- to be done --- XXX */
15162
15163 return true;
15164 }
15165 #endif
15166
15167 /* Reads in the contents of SECTION from FILE, returning a pointer
15168 to a malloc'ed buffer or NULL if something went wrong. */
15169
15170 static char *
15171 get_section_contents (Elf_Internal_Shdr * section, Filedata * filedata)
15172 {
15173 uint64_t num_bytes = section->sh_size;
15174
15175 if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
15176 {
15177 printf (_("Section '%s' has no data to dump.\n"),
15178 printable_section_name (filedata, section));
15179 return NULL;
15180 }
15181
15182 return (char *) get_data (NULL, filedata, section->sh_offset, 1, num_bytes,
15183 _("section contents"));
15184 }
15185
15186 /* Uncompresses a section that was compressed using zlib, in place. */
15187
15188 static bool
15189 uncompress_section_contents (unsigned char **buffer,
15190 uint64_t uncompressed_size,
15191 uint64_t *size)
15192 {
15193 uint64_t compressed_size = *size;
15194 unsigned char *compressed_buffer = *buffer;
15195 unsigned char *uncompressed_buffer;
15196 z_stream strm;
15197 int rc;
15198
15199 /* It is possible the section consists of several compressed
15200 buffers concatenated together, so we uncompress in a loop. */
15201 /* PR 18313: The state field in the z_stream structure is supposed
15202 to be invisible to the user (ie us), but some compilers will
15203 still complain about it being used without initialisation. So
15204 we first zero the entire z_stream structure and then set the fields
15205 that we need. */
15206 memset (& strm, 0, sizeof strm);
15207 strm.avail_in = compressed_size;
15208 strm.next_in = (Bytef *) compressed_buffer;
15209 strm.avail_out = uncompressed_size;
15210 uncompressed_buffer = (unsigned char *) xmalloc (uncompressed_size);
15211
15212 rc = inflateInit (& strm);
15213 while (strm.avail_in > 0)
15214 {
15215 if (rc != Z_OK)
15216 break;
15217 strm.next_out = ((Bytef *) uncompressed_buffer
15218 + (uncompressed_size - strm.avail_out));
15219 rc = inflate (&strm, Z_FINISH);
15220 if (rc != Z_STREAM_END)
15221 break;
15222 rc = inflateReset (& strm);
15223 }
15224 if (inflateEnd (& strm) != Z_OK
15225 || rc != Z_OK
15226 || strm.avail_out != 0)
15227 goto fail;
15228
15229 *buffer = uncompressed_buffer;
15230 *size = uncompressed_size;
15231 return true;
15232
15233 fail:
15234 free (uncompressed_buffer);
15235 /* Indicate decompression failure. */
15236 *buffer = NULL;
15237 return false;
15238 }
15239
15240 static bool
15241 dump_section_as_strings (Elf_Internal_Shdr * section, Filedata * filedata)
15242 {
15243 Elf_Internal_Shdr *relsec;
15244 uint64_t num_bytes;
15245 unsigned char *data;
15246 unsigned char *end;
15247 unsigned char *real_start;
15248 unsigned char *start;
15249 bool some_strings_shown;
15250
15251 real_start = start = (unsigned char *) get_section_contents (section, filedata);
15252 if (start == NULL)
15253 /* PR 21820: Do not fail if the section was empty. */
15254 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
15255
15256 num_bytes = section->sh_size;
15257
15258 if (filedata->is_separate)
15259 printf (_("\nString dump of section '%s' in linked file %s:\n"),
15260 printable_section_name (filedata, section),
15261 filedata->file_name);
15262 else
15263 printf (_("\nString dump of section '%s':\n"),
15264 printable_section_name (filedata, section));
15265
15266 if (decompress_dumps)
15267 {
15268 uint64_t new_size = num_bytes;
15269 uint64_t uncompressed_size = 0;
15270
15271 if ((section->sh_flags & SHF_COMPRESSED) != 0)
15272 {
15273 Elf_Internal_Chdr chdr;
15274 unsigned int compression_header_size
15275 = get_compression_header (& chdr, (unsigned char *) start,
15276 num_bytes);
15277 if (compression_header_size == 0)
15278 /* An error message will have already been generated
15279 by get_compression_header. */
15280 goto error_out;
15281
15282 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
15283 {
15284 warn (_("section '%s' has unsupported compress type: %d\n"),
15285 printable_section_name (filedata, section), chdr.ch_type);
15286 goto error_out;
15287 }
15288 uncompressed_size = chdr.ch_size;
15289 start += compression_header_size;
15290 new_size -= compression_header_size;
15291 }
15292 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
15293 {
15294 /* Read the zlib header. In this case, it should be "ZLIB"
15295 followed by the uncompressed section size, 8 bytes in
15296 big-endian order. */
15297 uncompressed_size = start[4]; uncompressed_size <<= 8;
15298 uncompressed_size += start[5]; uncompressed_size <<= 8;
15299 uncompressed_size += start[6]; uncompressed_size <<= 8;
15300 uncompressed_size += start[7]; uncompressed_size <<= 8;
15301 uncompressed_size += start[8]; uncompressed_size <<= 8;
15302 uncompressed_size += start[9]; uncompressed_size <<= 8;
15303 uncompressed_size += start[10]; uncompressed_size <<= 8;
15304 uncompressed_size += start[11];
15305 start += 12;
15306 new_size -= 12;
15307 }
15308
15309 if (uncompressed_size)
15310 {
15311 if (uncompress_section_contents (& start,
15312 uncompressed_size, & new_size))
15313 num_bytes = new_size;
15314 else
15315 {
15316 error (_("Unable to decompress section %s\n"),
15317 printable_section_name (filedata, section));
15318 goto error_out;
15319 }
15320 }
15321 else
15322 start = real_start;
15323 }
15324
15325 /* If the section being dumped has relocations against it the user might
15326 be expecting these relocations to have been applied. Check for this
15327 case and issue a warning message in order to avoid confusion.
15328 FIXME: Maybe we ought to have an option that dumps a section with
15329 relocs applied ? */
15330 for (relsec = filedata->section_headers;
15331 relsec < filedata->section_headers + filedata->file_header.e_shnum;
15332 ++relsec)
15333 {
15334 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
15335 || relsec->sh_info >= filedata->file_header.e_shnum
15336 || filedata->section_headers + relsec->sh_info != section
15337 || relsec->sh_size == 0
15338 || relsec->sh_link >= filedata->file_header.e_shnum)
15339 continue;
15340
15341 printf (_(" Note: This section has relocations against it, but these have NOT been applied to this dump.\n"));
15342 break;
15343 }
15344
15345 data = start;
15346 end = start + num_bytes;
15347 some_strings_shown = false;
15348
15349 #ifdef HAVE_MBSTATE_T
15350 mbstate_t state;
15351 /* Initialise the multibyte conversion state. */
15352 memset (& state, 0, sizeof (state));
15353 #endif
15354
15355 bool continuing = false;
15356
15357 while (data < end)
15358 {
15359 while (!ISPRINT (* data))
15360 if (++ data >= end)
15361 break;
15362
15363 if (data < end)
15364 {
15365 size_t maxlen = end - data;
15366
15367 if (continuing)
15368 {
15369 printf (" ");
15370 continuing = false;
15371 }
15372 else
15373 {
15374 printf (" [%6lx] ", (unsigned long) (data - start));
15375 }
15376
15377 if (maxlen > 0)
15378 {
15379 char c = 0;
15380
15381 while (maxlen)
15382 {
15383 c = *data++;
15384
15385 if (c == 0)
15386 break;
15387
15388 /* PR 25543: Treat new-lines as string-ending characters. */
15389 if (c == '\n')
15390 {
15391 printf ("\\n\n");
15392 if (*data != 0)
15393 continuing = true;
15394 break;
15395 }
15396
15397 /* Do not print control characters directly as they can affect terminal
15398 settings. Such characters usually appear in the names generated
15399 by the assembler for local labels. */
15400 if (ISCNTRL (c))
15401 {
15402 printf ("^%c", c + 0x40);
15403 }
15404 else if (ISPRINT (c))
15405 {
15406 putchar (c);
15407 }
15408 else
15409 {
15410 size_t n;
15411 #ifdef HAVE_MBSTATE_T
15412 wchar_t w;
15413 #endif
15414 /* Let printf do the hard work of displaying multibyte characters. */
15415 printf ("%.1s", data - 1);
15416 #ifdef HAVE_MBSTATE_T
15417 /* Try to find out how many bytes made up the character that was
15418 just printed. Advance the symbol pointer past the bytes that
15419 were displayed. */
15420 n = mbrtowc (& w, (char *)(data - 1), MB_CUR_MAX, & state);
15421 #else
15422 n = 1;
15423 #endif
15424 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
15425 data += (n - 1);
15426 }
15427 }
15428
15429 if (c != '\n')
15430 putchar ('\n');
15431 }
15432 else
15433 {
15434 printf (_("<corrupt>\n"));
15435 data = end;
15436 }
15437 some_strings_shown = true;
15438 }
15439 }
15440
15441 if (! some_strings_shown)
15442 printf (_(" No strings found in this section."));
15443
15444 free (real_start);
15445
15446 putchar ('\n');
15447 return true;
15448
15449 error_out:
15450 free (real_start);
15451 return false;
15452 }
15453
15454 static bool
15455 dump_section_as_bytes (Elf_Internal_Shdr *section,
15456 Filedata *filedata,
15457 bool relocate)
15458 {
15459 Elf_Internal_Shdr *relsec;
15460 size_t bytes;
15461 uint64_t section_size;
15462 uint64_t addr;
15463 unsigned char *data;
15464 unsigned char *real_start;
15465 unsigned char *start;
15466
15467 real_start = start = (unsigned char *) get_section_contents (section, filedata);
15468 if (start == NULL)
15469 /* PR 21820: Do not fail if the section was empty. */
15470 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
15471
15472 section_size = section->sh_size;
15473
15474 if (filedata->is_separate)
15475 printf (_("\nHex dump of section '%s' in linked file %s:\n"),
15476 printable_section_name (filedata, section),
15477 filedata->file_name);
15478 else
15479 printf (_("\nHex dump of section '%s':\n"),
15480 printable_section_name (filedata, section));
15481
15482 if (decompress_dumps)
15483 {
15484 uint64_t new_size = section_size;
15485 uint64_t uncompressed_size = 0;
15486
15487 if ((section->sh_flags & SHF_COMPRESSED) != 0)
15488 {
15489 Elf_Internal_Chdr chdr;
15490 unsigned int compression_header_size
15491 = get_compression_header (& chdr, start, section_size);
15492
15493 if (compression_header_size == 0)
15494 /* An error message will have already been generated
15495 by get_compression_header. */
15496 goto error_out;
15497
15498 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
15499 {
15500 warn (_("section '%s' has unsupported compress type: %d\n"),
15501 printable_section_name (filedata, section), chdr.ch_type);
15502 goto error_out;
15503 }
15504 uncompressed_size = chdr.ch_size;
15505 start += compression_header_size;
15506 new_size -= compression_header_size;
15507 }
15508 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
15509 {
15510 /* Read the zlib header. In this case, it should be "ZLIB"
15511 followed by the uncompressed section size, 8 bytes in
15512 big-endian order. */
15513 uncompressed_size = start[4]; uncompressed_size <<= 8;
15514 uncompressed_size += start[5]; uncompressed_size <<= 8;
15515 uncompressed_size += start[6]; uncompressed_size <<= 8;
15516 uncompressed_size += start[7]; uncompressed_size <<= 8;
15517 uncompressed_size += start[8]; uncompressed_size <<= 8;
15518 uncompressed_size += start[9]; uncompressed_size <<= 8;
15519 uncompressed_size += start[10]; uncompressed_size <<= 8;
15520 uncompressed_size += start[11];
15521 start += 12;
15522 new_size -= 12;
15523 }
15524
15525 if (uncompressed_size)
15526 {
15527 if (uncompress_section_contents (& start, uncompressed_size,
15528 & new_size))
15529 {
15530 section_size = new_size;
15531 }
15532 else
15533 {
15534 error (_("Unable to decompress section %s\n"),
15535 printable_section_name (filedata, section));
15536 /* FIXME: Print the section anyway ? */
15537 goto error_out;
15538 }
15539 }
15540 else
15541 start = real_start;
15542 }
15543
15544 if (relocate)
15545 {
15546 if (! apply_relocations (filedata, section, start, section_size, NULL, NULL))
15547 goto error_out;
15548 }
15549 else
15550 {
15551 /* If the section being dumped has relocations against it the user might
15552 be expecting these relocations to have been applied. Check for this
15553 case and issue a warning message in order to avoid confusion.
15554 FIXME: Maybe we ought to have an option that dumps a section with
15555 relocs applied ? */
15556 for (relsec = filedata->section_headers;
15557 relsec < filedata->section_headers + filedata->file_header.e_shnum;
15558 ++relsec)
15559 {
15560 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
15561 || relsec->sh_info >= filedata->file_header.e_shnum
15562 || filedata->section_headers + relsec->sh_info != section
15563 || relsec->sh_size == 0
15564 || relsec->sh_link >= filedata->file_header.e_shnum)
15565 continue;
15566
15567 printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
15568 break;
15569 }
15570 }
15571
15572 addr = section->sh_addr;
15573 bytes = section_size;
15574 data = start;
15575
15576 while (bytes)
15577 {
15578 int j;
15579 int k;
15580 int lbytes;
15581
15582 lbytes = (bytes > 16 ? 16 : bytes);
15583
15584 printf (" 0x%8.8lx ", (unsigned long) addr);
15585
15586 for (j = 0; j < 16; j++)
15587 {
15588 if (j < lbytes)
15589 printf ("%2.2x", data[j]);
15590 else
15591 printf (" ");
15592
15593 if ((j & 3) == 3)
15594 printf (" ");
15595 }
15596
15597 for (j = 0; j < lbytes; j++)
15598 {
15599 k = data[j];
15600 if (k >= ' ' && k < 0x7f)
15601 printf ("%c", k);
15602 else
15603 printf (".");
15604 }
15605
15606 putchar ('\n');
15607
15608 data += lbytes;
15609 addr += lbytes;
15610 bytes -= lbytes;
15611 }
15612
15613 free (real_start);
15614
15615 putchar ('\n');
15616 return true;
15617
15618 error_out:
15619 free (real_start);
15620 return false;
15621 }
15622
15623 #ifdef ENABLE_LIBCTF
15624 static ctf_sect_t *
15625 shdr_to_ctf_sect (ctf_sect_t *buf, Elf_Internal_Shdr *shdr, Filedata *filedata)
15626 {
15627 buf->cts_name = section_name_print (filedata, shdr);
15628 buf->cts_size = shdr->sh_size;
15629 buf->cts_entsize = shdr->sh_entsize;
15630
15631 return buf;
15632 }
15633
15634 /* Formatting callback function passed to ctf_dump. Returns either the pointer
15635 it is passed, or a pointer to newly-allocated storage, in which case
15636 dump_ctf() will free it when it no longer needs it. */
15637
15638 static char *
15639 dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
15640 char *s, void *arg)
15641 {
15642 const char *blanks = arg;
15643 char *new_s;
15644
15645 if (asprintf (&new_s, "%s%s", blanks, s) < 0)
15646 return s;
15647 return new_s;
15648 }
15649
15650 /* Dump CTF errors/warnings. */
15651 static void
15652 dump_ctf_errs (ctf_dict_t *fp)
15653 {
15654 ctf_next_t *it = NULL;
15655 char *errtext;
15656 int is_warning;
15657 int err;
15658
15659 /* Dump accumulated errors and warnings. */
15660 while ((errtext = ctf_errwarning_next (fp, &it, &is_warning, &err)) != NULL)
15661 {
15662 error (_("%s: %s"), is_warning ? _("warning"): _("error"),
15663 errtext);
15664 free (errtext);
15665 }
15666 if (err != ECTF_NEXT_END)
15667 error (_("CTF error: cannot get CTF errors: `%s'"), ctf_errmsg (err));
15668 }
15669
15670 /* Dump one CTF archive member. */
15671
15672 static void
15673 dump_ctf_archive_member (ctf_dict_t *ctf, const char *name, ctf_dict_t *parent,
15674 size_t member)
15675 {
15676 const char *things[] = {"Header", "Labels", "Data objects",
15677 "Function objects", "Variables", "Types", "Strings",
15678 ""};
15679 const char **thing;
15680 size_t i;
15681
15682 /* Don't print out the name of the default-named archive member if it appears
15683 first in the list. The name .ctf appears everywhere, even for things that
15684 aren't really archives, so printing it out is liable to be confusing; also,
15685 the common case by far is for only one archive member to exist, and hiding
15686 it in that case seems worthwhile. */
15687
15688 if (strcmp (name, ".ctf") != 0 || member != 0)
15689 printf (_("\nCTF archive member: %s:\n"), name);
15690
15691 if (ctf_parent_name (ctf) != NULL)
15692 ctf_import (ctf, parent);
15693
15694 for (i = 0, thing = things; *thing[0]; thing++, i++)
15695 {
15696 ctf_dump_state_t *s = NULL;
15697 char *item;
15698
15699 printf ("\n %s:\n", *thing);
15700 while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
15701 (void *) " ")) != NULL)
15702 {
15703 printf ("%s\n", item);
15704 free (item);
15705 }
15706
15707 if (ctf_errno (ctf))
15708 {
15709 error (_("Iteration failed: %s, %s\n"), *thing,
15710 ctf_errmsg (ctf_errno (ctf)));
15711 break;
15712 }
15713 }
15714
15715 dump_ctf_errs (ctf);
15716 }
15717
15718 static bool
15719 dump_section_as_ctf (Elf_Internal_Shdr * section, Filedata * filedata)
15720 {
15721 Elf_Internal_Shdr * symtab_sec = NULL;
15722 Elf_Internal_Shdr * strtab_sec = NULL;
15723 void * data = NULL;
15724 void * symdata = NULL;
15725 void * strdata = NULL;
15726 ctf_sect_t ctfsect, symsect, strsect;
15727 ctf_sect_t * symsectp = NULL;
15728 ctf_sect_t * strsectp = NULL;
15729 ctf_archive_t * ctfa = NULL;
15730 ctf_dict_t * parent = NULL;
15731 ctf_dict_t * fp;
15732
15733 ctf_next_t *i = NULL;
15734 const char *name;
15735 size_t member = 0;
15736 int err;
15737 bool ret = false;
15738
15739 shdr_to_ctf_sect (&ctfsect, section, filedata);
15740 data = get_section_contents (section, filedata);
15741 ctfsect.cts_data = data;
15742
15743 if (!dump_ctf_symtab_name)
15744 dump_ctf_symtab_name = strdup (".dynsym");
15745
15746 if (!dump_ctf_strtab_name)
15747 dump_ctf_strtab_name = strdup (".dynstr");
15748
15749 if (dump_ctf_symtab_name && dump_ctf_symtab_name[0] != 0)
15750 {
15751 if ((symtab_sec = find_section (filedata, dump_ctf_symtab_name)) == NULL)
15752 {
15753 error (_("No symbol section named %s\n"), dump_ctf_symtab_name);
15754 goto fail;
15755 }
15756 if ((symdata = (void *) get_data (NULL, filedata,
15757 symtab_sec->sh_offset, 1,
15758 symtab_sec->sh_size,
15759 _("symbols"))) == NULL)
15760 goto fail;
15761 symsectp = shdr_to_ctf_sect (&symsect, symtab_sec, filedata);
15762 symsect.cts_data = symdata;
15763 }
15764
15765 if (dump_ctf_strtab_name && dump_ctf_strtab_name[0] != 0)
15766 {
15767 if ((strtab_sec = find_section (filedata, dump_ctf_strtab_name)) == NULL)
15768 {
15769 error (_("No string table section named %s\n"),
15770 dump_ctf_strtab_name);
15771 goto fail;
15772 }
15773 if ((strdata = (void *) get_data (NULL, filedata,
15774 strtab_sec->sh_offset, 1,
15775 strtab_sec->sh_size,
15776 _("strings"))) == NULL)
15777 goto fail;
15778 strsectp = shdr_to_ctf_sect (&strsect, strtab_sec, filedata);
15779 strsect.cts_data = strdata;
15780 }
15781
15782 /* Load the CTF file and dump it. It may be a raw CTF section, or an archive:
15783 libctf papers over the difference, so we can pretend it is always an
15784 archive. */
15785
15786 if ((ctfa = ctf_arc_bufopen (&ctfsect, symsectp, strsectp, &err)) == NULL)
15787 {
15788 dump_ctf_errs (NULL);
15789 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15790 goto fail;
15791 }
15792
15793 ctf_arc_symsect_endianness (ctfa, filedata->file_header.e_ident[EI_DATA]
15794 != ELFDATA2MSB);
15795
15796 /* Preload the parent dict, since it will need to be imported into every
15797 child in turn. */
15798 if ((parent = ctf_dict_open (ctfa, dump_ctf_parent_name, &err)) == NULL)
15799 {
15800 dump_ctf_errs (NULL);
15801 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15802 goto fail;
15803 }
15804
15805 ret = true;
15806
15807 if (filedata->is_separate)
15808 printf (_("\nDump of CTF section '%s' in linked file %s:\n"),
15809 printable_section_name (filedata, section),
15810 filedata->file_name);
15811 else
15812 printf (_("\nDump of CTF section '%s':\n"),
15813 printable_section_name (filedata, section));
15814
15815 while ((fp = ctf_archive_next (ctfa, &i, &name, 0, &err)) != NULL)
15816 dump_ctf_archive_member (fp, name, parent, member++);
15817 if (err != ECTF_NEXT_END)
15818 {
15819 dump_ctf_errs (NULL);
15820 error (_("CTF member open failure: %s\n"), ctf_errmsg (err));
15821 ret = false;
15822 }
15823
15824 fail:
15825 ctf_dict_close (parent);
15826 ctf_close (ctfa);
15827 free (data);
15828 free (symdata);
15829 free (strdata);
15830 return ret;
15831 }
15832 #endif
15833
15834 static bool
15835 load_specific_debug_section (enum dwarf_section_display_enum debug,
15836 const Elf_Internal_Shdr * sec,
15837 void * data)
15838 {
15839 struct dwarf_section * section = &debug_displays [debug].section;
15840 char buf [64];
15841 Filedata * filedata = (Filedata *) data;
15842
15843 if (section->start != NULL)
15844 {
15845 /* If it is already loaded, do nothing. */
15846 if (streq (section->filename, filedata->file_name))
15847 return true;
15848 free (section->start);
15849 }
15850
15851 snprintf (buf, sizeof (buf), _("%s section data"), section->name);
15852 section->address = sec->sh_addr;
15853 section->filename = filedata->file_name;
15854 section->start = (unsigned char *) get_data (NULL, filedata,
15855 sec->sh_offset, 1,
15856 sec->sh_size, buf);
15857 if (section->start == NULL)
15858 section->size = 0;
15859 else
15860 {
15861 unsigned char *start = section->start;
15862 uint64_t size = sec->sh_size;
15863 uint64_t uncompressed_size = 0;
15864
15865 if ((sec->sh_flags & SHF_COMPRESSED) != 0)
15866 {
15867 Elf_Internal_Chdr chdr;
15868 unsigned int compression_header_size;
15869
15870 if (size < (is_32bit_elf
15871 ? sizeof (Elf32_External_Chdr)
15872 : sizeof (Elf64_External_Chdr)))
15873 {
15874 warn (_("compressed section %s is too small to contain a compression header\n"),
15875 section->name);
15876 return false;
15877 }
15878
15879 compression_header_size = get_compression_header (&chdr, start, size);
15880 if (compression_header_size == 0)
15881 /* An error message will have already been generated
15882 by get_compression_header. */
15883 return false;
15884
15885 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
15886 {
15887 warn (_("section '%s' has unsupported compress type: %d\n"),
15888 section->name, chdr.ch_type);
15889 return false;
15890 }
15891 uncompressed_size = chdr.ch_size;
15892 start += compression_header_size;
15893 size -= compression_header_size;
15894 }
15895 else if (size > 12 && streq ((char *) start, "ZLIB"))
15896 {
15897 /* Read the zlib header. In this case, it should be "ZLIB"
15898 followed by the uncompressed section size, 8 bytes in
15899 big-endian order. */
15900 uncompressed_size = start[4]; uncompressed_size <<= 8;
15901 uncompressed_size += start[5]; uncompressed_size <<= 8;
15902 uncompressed_size += start[6]; uncompressed_size <<= 8;
15903 uncompressed_size += start[7]; uncompressed_size <<= 8;
15904 uncompressed_size += start[8]; uncompressed_size <<= 8;
15905 uncompressed_size += start[9]; uncompressed_size <<= 8;
15906 uncompressed_size += start[10]; uncompressed_size <<= 8;
15907 uncompressed_size += start[11];
15908 start += 12;
15909 size -= 12;
15910 }
15911
15912 if (uncompressed_size)
15913 {
15914 if (uncompress_section_contents (&start, uncompressed_size,
15915 &size))
15916 {
15917 /* Free the compressed buffer, update the section buffer
15918 and the section size if uncompress is successful. */
15919 free (section->start);
15920 section->start = start;
15921 }
15922 else
15923 {
15924 error (_("Unable to decompress section %s\n"),
15925 printable_section_name (filedata, sec));
15926 return false;
15927 }
15928 }
15929
15930 section->size = size;
15931 }
15932
15933 if (section->start == NULL)
15934 return false;
15935
15936 if (debug_displays [debug].relocate)
15937 {
15938 if (! apply_relocations (filedata, sec, section->start, section->size,
15939 & section->reloc_info, & section->num_relocs))
15940 return false;
15941 }
15942 else
15943 {
15944 section->reloc_info = NULL;
15945 section->num_relocs = 0;
15946 }
15947
15948 return true;
15949 }
15950
15951 #if HAVE_LIBDEBUGINFOD
15952 /* Return a hex string representation of the build-id. */
15953 unsigned char *
15954 get_build_id (void * data)
15955 {
15956 Filedata * filedata = (Filedata *) data;
15957 Elf_Internal_Shdr * shdr;
15958 unsigned long i;
15959
15960 /* Iterate through notes to find note.gnu.build-id.
15961 FIXME: Only the first note in any note section is examined. */
15962 for (i = 0, shdr = filedata->section_headers;
15963 i < filedata->file_header.e_shnum && shdr != NULL;
15964 i++, shdr++)
15965 {
15966 if (shdr->sh_type != SHT_NOTE)
15967 continue;
15968
15969 char * next;
15970 char * end;
15971 size_t data_remaining;
15972 size_t min_notesz;
15973 Elf_External_Note * enote;
15974 Elf_Internal_Note inote;
15975
15976 uint64_t offset = shdr->sh_offset;
15977 uint64_t align = shdr->sh_addralign;
15978 uint64_t length = shdr->sh_size;
15979
15980 enote = (Elf_External_Note *) get_section_contents (shdr, filedata);
15981 if (enote == NULL)
15982 continue;
15983
15984 if (align < 4)
15985 align = 4;
15986 else if (align != 4 && align != 8)
15987 {
15988 free (enote);
15989 continue;
15990 }
15991
15992 end = (char *) enote + length;
15993 data_remaining = end - (char *) enote;
15994
15995 if (!is_ia64_vms (filedata))
15996 {
15997 min_notesz = offsetof (Elf_External_Note, name);
15998 if (data_remaining < min_notesz)
15999 {
16000 warn (_("\
16001 malformed note encountered in section %s whilst scanning for build-id note\n"),
16002 printable_section_name (filedata, shdr));
16003 free (enote);
16004 continue;
16005 }
16006 data_remaining -= min_notesz;
16007
16008 inote.type = BYTE_GET (enote->type);
16009 inote.namesz = BYTE_GET (enote->namesz);
16010 inote.namedata = enote->name;
16011 inote.descsz = BYTE_GET (enote->descsz);
16012 inote.descdata = ((char *) enote
16013 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
16014 inote.descpos = offset + (inote.descdata - (char *) enote);
16015 next = ((char *) enote
16016 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
16017 }
16018 else
16019 {
16020 Elf64_External_VMS_Note *vms_enote;
16021
16022 /* PR binutils/15191
16023 Make sure that there is enough data to read. */
16024 min_notesz = offsetof (Elf64_External_VMS_Note, name);
16025 if (data_remaining < min_notesz)
16026 {
16027 warn (_("\
16028 malformed note encountered in section %s whilst scanning for build-id note\n"),
16029 printable_section_name (filedata, shdr));
16030 free (enote);
16031 continue;
16032 }
16033 data_remaining -= min_notesz;
16034
16035 vms_enote = (Elf64_External_VMS_Note *) enote;
16036 inote.type = BYTE_GET (vms_enote->type);
16037 inote.namesz = BYTE_GET (vms_enote->namesz);
16038 inote.namedata = vms_enote->name;
16039 inote.descsz = BYTE_GET (vms_enote->descsz);
16040 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
16041 inote.descpos = offset + (inote.descdata - (char *) enote);
16042 next = inote.descdata + align_power (inote.descsz, 3);
16043 }
16044
16045 /* Skip malformed notes. */
16046 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
16047 || (size_t) (inote.descdata - inote.namedata) > data_remaining
16048 || (size_t) (next - inote.descdata) < inote.descsz
16049 || ((size_t) (next - inote.descdata)
16050 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
16051 {
16052 warn (_("\
16053 malformed note encountered in section %s whilst scanning for build-id note\n"),
16054 printable_section_name (filedata, shdr));
16055 free (enote);
16056 continue;
16057 }
16058
16059 /* Check if this is the build-id note. If so then convert the build-id
16060 bytes to a hex string. */
16061 if (inote.namesz > 0
16062 && startswith (inote.namedata, "GNU")
16063 && inote.type == NT_GNU_BUILD_ID)
16064 {
16065 unsigned long j;
16066 char * build_id;
16067
16068 build_id = malloc (inote.descsz * 2 + 1);
16069 if (build_id == NULL)
16070 {
16071 free (enote);
16072 return NULL;
16073 }
16074
16075 for (j = 0; j < inote.descsz; ++j)
16076 sprintf (build_id + (j * 2), "%02x", inote.descdata[j] & 0xff);
16077 build_id[inote.descsz * 2] = '\0';
16078 free (enote);
16079
16080 return (unsigned char *) build_id;
16081 }
16082 free (enote);
16083 }
16084
16085 return NULL;
16086 }
16087 #endif /* HAVE_LIBDEBUGINFOD */
16088
16089 /* If this is not NULL, load_debug_section will only look for sections
16090 within the list of sections given here. */
16091 static unsigned int * section_subset = NULL;
16092
16093 bool
16094 load_debug_section (enum dwarf_section_display_enum debug, void * data)
16095 {
16096 struct dwarf_section * section = &debug_displays [debug].section;
16097 Elf_Internal_Shdr * sec;
16098 Filedata * filedata = (Filedata *) data;
16099
16100 if (!dump_any_debugging)
16101 return false;
16102
16103 /* Without section headers we cannot find any sections. */
16104 if (filedata->section_headers == NULL)
16105 return false;
16106
16107 if (filedata->string_table == NULL
16108 && filedata->file_header.e_shstrndx != SHN_UNDEF
16109 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
16110 {
16111 Elf_Internal_Shdr * strs;
16112
16113 /* Read in the string table, so that we have section names to scan. */
16114 strs = filedata->section_headers + filedata->file_header.e_shstrndx;
16115
16116 if (strs != NULL && strs->sh_size != 0)
16117 {
16118 filedata->string_table
16119 = (char *) get_data (NULL, filedata, strs->sh_offset,
16120 1, strs->sh_size, _("string table"));
16121
16122 filedata->string_table_length
16123 = filedata->string_table != NULL ? strs->sh_size : 0;
16124 }
16125 }
16126
16127 /* Locate the debug section. */
16128 sec = find_section_in_set (filedata, section->uncompressed_name, section_subset);
16129 if (sec != NULL)
16130 section->name = section->uncompressed_name;
16131 else
16132 {
16133 sec = find_section_in_set (filedata, section->compressed_name, section_subset);
16134 if (sec != NULL)
16135 section->name = section->compressed_name;
16136 }
16137 if (sec == NULL)
16138 return false;
16139
16140 /* If we're loading from a subset of sections, and we've loaded
16141 a section matching this name before, it's likely that it's a
16142 different one. */
16143 if (section_subset != NULL)
16144 free_debug_section (debug);
16145
16146 return load_specific_debug_section (debug, sec, data);
16147 }
16148
16149 void
16150 free_debug_section (enum dwarf_section_display_enum debug)
16151 {
16152 struct dwarf_section * section = &debug_displays [debug].section;
16153
16154 if (section->start == NULL)
16155 return;
16156
16157 free ((char *) section->start);
16158 section->start = NULL;
16159 section->address = 0;
16160 section->size = 0;
16161
16162 free (section->reloc_info);
16163 section->reloc_info = NULL;
16164 section->num_relocs = 0;
16165 }
16166
16167 static bool
16168 display_debug_section (int shndx, Elf_Internal_Shdr * section, Filedata * filedata)
16169 {
16170 const char *name = (section_name_valid (filedata, section)
16171 ? section_name (filedata, section) : "");
16172 const char *print_name = printable_section_name (filedata, section);
16173 uint64_t length;
16174 bool result = true;
16175 int i;
16176
16177 length = section->sh_size;
16178 if (length == 0)
16179 {
16180 printf (_("\nSection '%s' has no debugging data.\n"), print_name);
16181 return true;
16182 }
16183 if (section->sh_type == SHT_NOBITS)
16184 {
16185 /* There is no point in dumping the contents of a debugging section
16186 which has the NOBITS type - the bits in the file will be random.
16187 This can happen when a file containing a .eh_frame section is
16188 stripped with the --only-keep-debug command line option. */
16189 printf (_("section '%s' has the NOBITS type - its contents are unreliable.\n"),
16190 print_name);
16191 return false;
16192 }
16193
16194 if (startswith (name, ".gnu.linkonce.wi."))
16195 name = ".debug_info";
16196
16197 /* See if we know how to display the contents of this section. */
16198 for (i = 0; i < max; i++)
16199 {
16200 enum dwarf_section_display_enum id = (enum dwarf_section_display_enum) i;
16201 struct dwarf_section_display * display = debug_displays + i;
16202 struct dwarf_section * sec = & display->section;
16203
16204 if (streq (sec->uncompressed_name, name)
16205 || (id == line && startswith (name, ".debug_line."))
16206 || streq (sec->compressed_name, name))
16207 {
16208 bool secondary = (section != find_section (filedata, name));
16209
16210 if (secondary)
16211 free_debug_section (id);
16212
16213 if (i == line && startswith (name, ".debug_line."))
16214 sec->name = name;
16215 else if (streq (sec->uncompressed_name, name))
16216 sec->name = sec->uncompressed_name;
16217 else
16218 sec->name = sec->compressed_name;
16219
16220 if (load_specific_debug_section (id, section, filedata))
16221 {
16222 /* If this debug section is part of a CU/TU set in a .dwp file,
16223 restrict load_debug_section to the sections in that set. */
16224 section_subset = find_cu_tu_set (filedata, shndx);
16225
16226 result &= display->display (sec, filedata);
16227
16228 section_subset = NULL;
16229
16230 if (secondary || (id != info && id != abbrev && id != debug_addr))
16231 free_debug_section (id);
16232 }
16233 break;
16234 }
16235 }
16236
16237 if (i == max)
16238 {
16239 printf (_("Unrecognized debug section: %s\n"), print_name);
16240 result = false;
16241 }
16242
16243 return result;
16244 }
16245
16246 /* Set DUMP_SECTS for all sections where dumps were requested
16247 based on section name. */
16248
16249 static void
16250 initialise_dumps_byname (Filedata * filedata)
16251 {
16252 struct dump_list_entry * cur;
16253
16254 for (cur = dump_sects_byname; cur; cur = cur->next)
16255 {
16256 unsigned int i;
16257 bool any = false;
16258
16259 for (i = 0; i < filedata->file_header.e_shnum; i++)
16260 if (section_name_valid (filedata, filedata->section_headers + i)
16261 && streq (section_name (filedata, filedata->section_headers + i),
16262 cur->name))
16263 {
16264 request_dump_bynumber (&filedata->dump, i, cur->type);
16265 any = true;
16266 }
16267
16268 if (!any && !filedata->is_separate)
16269 warn (_("Section '%s' was not dumped because it does not exist\n"),
16270 cur->name);
16271 }
16272 }
16273
16274 static bool
16275 process_section_contents (Filedata * filedata)
16276 {
16277 Elf_Internal_Shdr * section;
16278 unsigned int i;
16279 bool res = true;
16280
16281 if (! do_dump)
16282 return true;
16283
16284 initialise_dumps_byname (filedata);
16285
16286 for (i = 0, section = filedata->section_headers;
16287 i < filedata->file_header.e_shnum && i < filedata->dump.num_dump_sects;
16288 i++, section++)
16289 {
16290 dump_type dump = filedata->dump.dump_sects[i];
16291
16292 if (filedata->is_separate && ! process_links)
16293 dump &= DEBUG_DUMP;
16294
16295 #ifdef SUPPORT_DISASSEMBLY
16296 if (dump & DISASS_DUMP)
16297 {
16298 if (! disassemble_section (section, filedata))
16299 res = false;
16300 }
16301 #endif
16302 if (dump & HEX_DUMP)
16303 {
16304 if (! dump_section_as_bytes (section, filedata, false))
16305 res = false;
16306 }
16307
16308 if (dump & RELOC_DUMP)
16309 {
16310 if (! dump_section_as_bytes (section, filedata, true))
16311 res = false;
16312 }
16313
16314 if (dump & STRING_DUMP)
16315 {
16316 if (! dump_section_as_strings (section, filedata))
16317 res = false;
16318 }
16319
16320 if (dump & DEBUG_DUMP)
16321 {
16322 if (! display_debug_section (i, section, filedata))
16323 res = false;
16324 }
16325
16326 #ifdef ENABLE_LIBCTF
16327 if (dump & CTF_DUMP)
16328 {
16329 if (! dump_section_as_ctf (section, filedata))
16330 res = false;
16331 }
16332 #endif
16333 }
16334
16335 if (! filedata->is_separate)
16336 {
16337 /* Check to see if the user requested a
16338 dump of a section that does not exist. */
16339 for (; i < filedata->dump.num_dump_sects; i++)
16340 if (filedata->dump.dump_sects[i])
16341 {
16342 warn (_("Section %d was not dumped because it does not exist!\n"), i);
16343 res = false;
16344 }
16345 }
16346
16347 return res;
16348 }
16349
16350 static void
16351 process_mips_fpe_exception (int mask)
16352 {
16353 if (mask)
16354 {
16355 bool first = true;
16356
16357 if (mask & OEX_FPU_INEX)
16358 fputs ("INEX", stdout), first = false;
16359 if (mask & OEX_FPU_UFLO)
16360 printf ("%sUFLO", first ? "" : "|"), first = false;
16361 if (mask & OEX_FPU_OFLO)
16362 printf ("%sOFLO", first ? "" : "|"), first = false;
16363 if (mask & OEX_FPU_DIV0)
16364 printf ("%sDIV0", first ? "" : "|"), first = false;
16365 if (mask & OEX_FPU_INVAL)
16366 printf ("%sINVAL", first ? "" : "|");
16367 }
16368 else
16369 fputs ("0", stdout);
16370 }
16371
16372 /* Display's the value of TAG at location P. If TAG is
16373 greater than 0 it is assumed to be an unknown tag, and
16374 a message is printed to this effect. Otherwise it is
16375 assumed that a message has already been printed.
16376
16377 If the bottom bit of TAG is set it assumed to have a
16378 string value, otherwise it is assumed to have an integer
16379 value.
16380
16381 Returns an updated P pointing to the first unread byte
16382 beyond the end of TAG's value.
16383
16384 Reads at or beyond END will not be made. */
16385
16386 static unsigned char *
16387 display_tag_value (signed int tag,
16388 unsigned char * p,
16389 const unsigned char * const end)
16390 {
16391 unsigned long val;
16392
16393 if (tag > 0)
16394 printf (" Tag_unknown_%d: ", tag);
16395
16396 if (p >= end)
16397 {
16398 warn (_("<corrupt tag>\n"));
16399 }
16400 else if (tag & 1)
16401 {
16402 /* PR 17531 file: 027-19978-0.004. */
16403 size_t maxlen = (end - p) - 1;
16404
16405 putchar ('"');
16406 if (maxlen > 0)
16407 {
16408 print_symbol ((int) maxlen, (const char *) p);
16409 p += strnlen ((char *) p, maxlen) + 1;
16410 }
16411 else
16412 {
16413 printf (_("<corrupt string tag>"));
16414 p = (unsigned char *) end;
16415 }
16416 printf ("\"\n");
16417 }
16418 else
16419 {
16420 READ_ULEB (val, p, end);
16421 printf ("%ld (0x%lx)\n", val, val);
16422 }
16423
16424 assert (p <= end);
16425 return p;
16426 }
16427
16428 /* ARC ABI attributes section. */
16429
16430 static unsigned char *
16431 display_arc_attribute (unsigned char * p,
16432 const unsigned char * const end)
16433 {
16434 unsigned int tag;
16435 unsigned int val;
16436
16437 READ_ULEB (tag, p, end);
16438
16439 switch (tag)
16440 {
16441 case Tag_ARC_PCS_config:
16442 READ_ULEB (val, p, end);
16443 printf (" Tag_ARC_PCS_config: ");
16444 switch (val)
16445 {
16446 case 0:
16447 printf (_("Absent/Non standard\n"));
16448 break;
16449 case 1:
16450 printf (_("Bare metal/mwdt\n"));
16451 break;
16452 case 2:
16453 printf (_("Bare metal/newlib\n"));
16454 break;
16455 case 3:
16456 printf (_("Linux/uclibc\n"));
16457 break;
16458 case 4:
16459 printf (_("Linux/glibc\n"));
16460 break;
16461 default:
16462 printf (_("Unknown\n"));
16463 break;
16464 }
16465 break;
16466
16467 case Tag_ARC_CPU_base:
16468 READ_ULEB (val, p, end);
16469 printf (" Tag_ARC_CPU_base: ");
16470 switch (val)
16471 {
16472 default:
16473 case TAG_CPU_NONE:
16474 printf (_("Absent\n"));
16475 break;
16476 case TAG_CPU_ARC6xx:
16477 printf ("ARC6xx\n");
16478 break;
16479 case TAG_CPU_ARC7xx:
16480 printf ("ARC7xx\n");
16481 break;
16482 case TAG_CPU_ARCEM:
16483 printf ("ARCEM\n");
16484 break;
16485 case TAG_CPU_ARCHS:
16486 printf ("ARCHS\n");
16487 break;
16488 }
16489 break;
16490
16491 case Tag_ARC_CPU_variation:
16492 READ_ULEB (val, p, end);
16493 printf (" Tag_ARC_CPU_variation: ");
16494 switch (val)
16495 {
16496 default:
16497 if (val > 0 && val < 16)
16498 printf ("Core%d\n", val);
16499 else
16500 printf ("Unknown\n");
16501 break;
16502
16503 case 0:
16504 printf (_("Absent\n"));
16505 break;
16506 }
16507 break;
16508
16509 case Tag_ARC_CPU_name:
16510 printf (" Tag_ARC_CPU_name: ");
16511 p = display_tag_value (-1, p, end);
16512 break;
16513
16514 case Tag_ARC_ABI_rf16:
16515 READ_ULEB (val, p, end);
16516 printf (" Tag_ARC_ABI_rf16: %s\n", val ? _("yes") : _("no"));
16517 break;
16518
16519 case Tag_ARC_ABI_osver:
16520 READ_ULEB (val, p, end);
16521 printf (" Tag_ARC_ABI_osver: v%d\n", val);
16522 break;
16523
16524 case Tag_ARC_ABI_pic:
16525 case Tag_ARC_ABI_sda:
16526 READ_ULEB (val, p, end);
16527 printf (tag == Tag_ARC_ABI_sda ? " Tag_ARC_ABI_sda: "
16528 : " Tag_ARC_ABI_pic: ");
16529 switch (val)
16530 {
16531 case 0:
16532 printf (_("Absent\n"));
16533 break;
16534 case 1:
16535 printf ("MWDT\n");
16536 break;
16537 case 2:
16538 printf ("GNU\n");
16539 break;
16540 default:
16541 printf (_("Unknown\n"));
16542 break;
16543 }
16544 break;
16545
16546 case Tag_ARC_ABI_tls:
16547 READ_ULEB (val, p, end);
16548 printf (" Tag_ARC_ABI_tls: %s\n", val ? "r25": "none");
16549 break;
16550
16551 case Tag_ARC_ABI_enumsize:
16552 READ_ULEB (val, p, end);
16553 printf (" Tag_ARC_ABI_enumsize: %s\n", val ? _("default") :
16554 _("smallest"));
16555 break;
16556
16557 case Tag_ARC_ABI_exceptions:
16558 READ_ULEB (val, p, end);
16559 printf (" Tag_ARC_ABI_exceptions: %s\n", val ? _("OPTFP")
16560 : _("default"));
16561 break;
16562
16563 case Tag_ARC_ABI_double_size:
16564 READ_ULEB (val, p, end);
16565 printf (" Tag_ARC_ABI_double_size: %d\n", val);
16566 break;
16567
16568 case Tag_ARC_ISA_config:
16569 printf (" Tag_ARC_ISA_config: ");
16570 p = display_tag_value (-1, p, end);
16571 break;
16572
16573 case Tag_ARC_ISA_apex:
16574 printf (" Tag_ARC_ISA_apex: ");
16575 p = display_tag_value (-1, p, end);
16576 break;
16577
16578 case Tag_ARC_ISA_mpy_option:
16579 READ_ULEB (val, p, end);
16580 printf (" Tag_ARC_ISA_mpy_option: %d\n", val);
16581 break;
16582
16583 case Tag_ARC_ATR_version:
16584 READ_ULEB (val, p, end);
16585 printf (" Tag_ARC_ATR_version: %d\n", val);
16586 break;
16587
16588 default:
16589 return display_tag_value (tag & 1, p, end);
16590 }
16591
16592 return p;
16593 }
16594
16595 /* ARM EABI attributes section. */
16596 typedef struct
16597 {
16598 unsigned int tag;
16599 const char * name;
16600 /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup. */
16601 unsigned int type;
16602 const char *const *table;
16603 } arm_attr_public_tag;
16604
16605 static const char *const arm_attr_tag_CPU_arch[] =
16606 {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
16607 "v6K", "v7", "v6-M", "v6S-M", "v7E-M", "v8", "v8-R", "v8-M.baseline",
16608 "v8-M.mainline", "v8.1-A", "v8.2-A", "v8.3-A",
16609 "v8.1-M.mainline", "v9"};
16610 static const char *const arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
16611 static const char *const arm_attr_tag_THUMB_ISA_use[] =
16612 {"No", "Thumb-1", "Thumb-2", "Yes"};
16613 static const char *const arm_attr_tag_FP_arch[] =
16614 {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16", "VFPv4", "VFPv4-D16",
16615 "FP for ARMv8", "FPv5/FP-D16 for ARMv8"};
16616 static const char *const arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
16617 static const char *const arm_attr_tag_Advanced_SIMD_arch[] =
16618 {"No", "NEONv1", "NEONv1 with Fused-MAC", "NEON for ARMv8",
16619 "NEON for ARMv8.1"};
16620 static const char *const arm_attr_tag_PCS_config[] =
16621 {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
16622 "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
16623 static const char *const arm_attr_tag_ABI_PCS_R9_use[] =
16624 {"V6", "SB", "TLS", "Unused"};
16625 static const char *const arm_attr_tag_ABI_PCS_RW_data[] =
16626 {"Absolute", "PC-relative", "SB-relative", "None"};
16627 static const char *const arm_attr_tag_ABI_PCS_RO_data[] =
16628 {"Absolute", "PC-relative", "None"};
16629 static const char *const arm_attr_tag_ABI_PCS_GOT_use[] =
16630 {"None", "direct", "GOT-indirect"};
16631 static const char *const arm_attr_tag_ABI_PCS_wchar_t[] =
16632 {"None", "??? 1", "2", "??? 3", "4"};
16633 static const char *const arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
16634 static const char *const arm_attr_tag_ABI_FP_denormal[] =
16635 {"Unused", "Needed", "Sign only"};
16636 static const char *const arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
16637 static const char *const arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
16638 static const char *const arm_attr_tag_ABI_FP_number_model[] =
16639 {"Unused", "Finite", "RTABI", "IEEE 754"};
16640 static const char *const arm_attr_tag_ABI_enum_size[] =
16641 {"Unused", "small", "int", "forced to int"};
16642 static const char *const arm_attr_tag_ABI_HardFP_use[] =
16643 {"As Tag_FP_arch", "SP only", "Reserved", "Deprecated"};
16644 static const char *const arm_attr_tag_ABI_VFP_args[] =
16645 {"AAPCS", "VFP registers", "custom", "compatible"};
16646 static const char *const arm_attr_tag_ABI_WMMX_args[] =
16647 {"AAPCS", "WMMX registers", "custom"};
16648 static const char *const arm_attr_tag_ABI_optimization_goals[] =
16649 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
16650 "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
16651 static const char *const arm_attr_tag_ABI_FP_optimization_goals[] =
16652 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
16653 "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
16654 static const char *const arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
16655 static const char *const arm_attr_tag_FP_HP_extension[] =
16656 {"Not Allowed", "Allowed"};
16657 static const char *const arm_attr_tag_ABI_FP_16bit_format[] =
16658 {"None", "IEEE 754", "Alternative Format"};
16659 static const char *const arm_attr_tag_DSP_extension[] =
16660 {"Follow architecture", "Allowed"};
16661 static const char *const arm_attr_tag_MPextension_use[] =
16662 {"Not Allowed", "Allowed"};
16663 static const char *const arm_attr_tag_DIV_use[] =
16664 {"Allowed in Thumb-ISA, v7-R or v7-M", "Not allowed",
16665 "Allowed in v7-A with integer division extension"};
16666 static const char *const arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
16667 static const char *const arm_attr_tag_Virtualization_use[] =
16668 {"Not Allowed", "TrustZone", "Virtualization Extensions",
16669 "TrustZone and Virtualization Extensions"};
16670 static const char *const arm_attr_tag_MPextension_use_legacy[] =
16671 {"Not Allowed", "Allowed"};
16672
16673 static const char *const arm_attr_tag_MVE_arch[] =
16674 {"No MVE", "MVE Integer only", "MVE Integer and FP"};
16675
16676 static const char * arm_attr_tag_PAC_extension[] =
16677 {"No PAC/AUT instructions",
16678 "PAC/AUT instructions permitted in the NOP space",
16679 "PAC/AUT instructions permitted in the NOP and in the non-NOP space"};
16680
16681 static const char * arm_attr_tag_BTI_extension[] =
16682 {"BTI instructions not permitted",
16683 "BTI instructions permitted in the NOP space",
16684 "BTI instructions permitted in the NOP and in the non-NOP space"};
16685
16686 static const char * arm_attr_tag_BTI_use[] =
16687 {"Compiled without branch target enforcement",
16688 "Compiled with branch target enforcement"};
16689
16690 static const char * arm_attr_tag_PACRET_use[] =
16691 {"Compiled without return address signing and authentication",
16692 "Compiled with return address signing and authentication"};
16693
16694 #define LOOKUP(id, name) \
16695 {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
16696 static arm_attr_public_tag arm_attr_public_tags[] =
16697 {
16698 {4, "CPU_raw_name", 1, NULL},
16699 {5, "CPU_name", 1, NULL},
16700 LOOKUP(6, CPU_arch),
16701 {7, "CPU_arch_profile", 0, NULL},
16702 LOOKUP(8, ARM_ISA_use),
16703 LOOKUP(9, THUMB_ISA_use),
16704 LOOKUP(10, FP_arch),
16705 LOOKUP(11, WMMX_arch),
16706 LOOKUP(12, Advanced_SIMD_arch),
16707 LOOKUP(13, PCS_config),
16708 LOOKUP(14, ABI_PCS_R9_use),
16709 LOOKUP(15, ABI_PCS_RW_data),
16710 LOOKUP(16, ABI_PCS_RO_data),
16711 LOOKUP(17, ABI_PCS_GOT_use),
16712 LOOKUP(18, ABI_PCS_wchar_t),
16713 LOOKUP(19, ABI_FP_rounding),
16714 LOOKUP(20, ABI_FP_denormal),
16715 LOOKUP(21, ABI_FP_exceptions),
16716 LOOKUP(22, ABI_FP_user_exceptions),
16717 LOOKUP(23, ABI_FP_number_model),
16718 {24, "ABI_align_needed", 0, NULL},
16719 {25, "ABI_align_preserved", 0, NULL},
16720 LOOKUP(26, ABI_enum_size),
16721 LOOKUP(27, ABI_HardFP_use),
16722 LOOKUP(28, ABI_VFP_args),
16723 LOOKUP(29, ABI_WMMX_args),
16724 LOOKUP(30, ABI_optimization_goals),
16725 LOOKUP(31, ABI_FP_optimization_goals),
16726 {32, "compatibility", 0, NULL},
16727 LOOKUP(34, CPU_unaligned_access),
16728 LOOKUP(36, FP_HP_extension),
16729 LOOKUP(38, ABI_FP_16bit_format),
16730 LOOKUP(42, MPextension_use),
16731 LOOKUP(44, DIV_use),
16732 LOOKUP(46, DSP_extension),
16733 LOOKUP(48, MVE_arch),
16734 LOOKUP(50, PAC_extension),
16735 LOOKUP(52, BTI_extension),
16736 LOOKUP(74, BTI_use),
16737 LOOKUP(76, PACRET_use),
16738 {64, "nodefaults", 0, NULL},
16739 {65, "also_compatible_with", 0, NULL},
16740 LOOKUP(66, T2EE_use),
16741 {67, "conformance", 1, NULL},
16742 LOOKUP(68, Virtualization_use),
16743 LOOKUP(70, MPextension_use_legacy)
16744 };
16745 #undef LOOKUP
16746
16747 static unsigned char *
16748 display_arm_attribute (unsigned char * p,
16749 const unsigned char * const end)
16750 {
16751 unsigned int tag;
16752 unsigned int val;
16753 arm_attr_public_tag * attr;
16754 unsigned i;
16755 unsigned int type;
16756
16757 READ_ULEB (tag, p, end);
16758 attr = NULL;
16759 for (i = 0; i < ARRAY_SIZE (arm_attr_public_tags); i++)
16760 {
16761 if (arm_attr_public_tags[i].tag == tag)
16762 {
16763 attr = &arm_attr_public_tags[i];
16764 break;
16765 }
16766 }
16767
16768 if (attr)
16769 {
16770 printf (" Tag_%s: ", attr->name);
16771 switch (attr->type)
16772 {
16773 case 0:
16774 switch (tag)
16775 {
16776 case 7: /* Tag_CPU_arch_profile. */
16777 READ_ULEB (val, p, end);
16778 switch (val)
16779 {
16780 case 0: printf (_("None\n")); break;
16781 case 'A': printf (_("Application\n")); break;
16782 case 'R': printf (_("Realtime\n")); break;
16783 case 'M': printf (_("Microcontroller\n")); break;
16784 case 'S': printf (_("Application or Realtime\n")); break;
16785 default: printf ("??? (%d)\n", val); break;
16786 }
16787 break;
16788
16789 case 24: /* Tag_align_needed. */
16790 READ_ULEB (val, p, end);
16791 switch (val)
16792 {
16793 case 0: printf (_("None\n")); break;
16794 case 1: printf (_("8-byte\n")); break;
16795 case 2: printf (_("4-byte\n")); break;
16796 case 3: printf ("??? 3\n"); break;
16797 default:
16798 if (val <= 12)
16799 printf (_("8-byte and up to %d-byte extended\n"),
16800 1 << val);
16801 else
16802 printf ("??? (%d)\n", val);
16803 break;
16804 }
16805 break;
16806
16807 case 25: /* Tag_align_preserved. */
16808 READ_ULEB (val, p, end);
16809 switch (val)
16810 {
16811 case 0: printf (_("None\n")); break;
16812 case 1: printf (_("8-byte, except leaf SP\n")); break;
16813 case 2: printf (_("8-byte\n")); break;
16814 case 3: printf ("??? 3\n"); break;
16815 default:
16816 if (val <= 12)
16817 printf (_("8-byte and up to %d-byte extended\n"),
16818 1 << val);
16819 else
16820 printf ("??? (%d)\n", val);
16821 break;
16822 }
16823 break;
16824
16825 case 32: /* Tag_compatibility. */
16826 {
16827 READ_ULEB (val, p, end);
16828 printf (_("flag = %d, vendor = "), val);
16829 if (p < end - 1)
16830 {
16831 size_t maxlen = (end - p) - 1;
16832
16833 print_symbol ((int) maxlen, (const char *) p);
16834 p += strnlen ((char *) p, maxlen) + 1;
16835 }
16836 else
16837 {
16838 printf (_("<corrupt>"));
16839 p = (unsigned char *) end;
16840 }
16841 putchar ('\n');
16842 }
16843 break;
16844
16845 case 64: /* Tag_nodefaults. */
16846 /* PR 17531: file: 001-505008-0.01. */
16847 if (p < end)
16848 p++;
16849 printf (_("True\n"));
16850 break;
16851
16852 case 65: /* Tag_also_compatible_with. */
16853 READ_ULEB (val, p, end);
16854 if (val == 6 /* Tag_CPU_arch. */)
16855 {
16856 READ_ULEB (val, p, end);
16857 if ((unsigned int) val >= ARRAY_SIZE (arm_attr_tag_CPU_arch))
16858 printf ("??? (%d)\n", val);
16859 else
16860 printf ("%s\n", arm_attr_tag_CPU_arch[val]);
16861 }
16862 else
16863 printf ("???\n");
16864 while (p < end && *(p++) != '\0' /* NUL terminator. */)
16865 ;
16866 break;
16867
16868 default:
16869 printf (_("<unknown: %d>\n"), tag);
16870 break;
16871 }
16872 return p;
16873
16874 case 1:
16875 return display_tag_value (-1, p, end);
16876 case 2:
16877 return display_tag_value (0, p, end);
16878
16879 default:
16880 assert (attr->type & 0x80);
16881 READ_ULEB (val, p, end);
16882 type = attr->type & 0x7f;
16883 if (val >= type)
16884 printf ("??? (%d)\n", val);
16885 else
16886 printf ("%s\n", attr->table[val]);
16887 return p;
16888 }
16889 }
16890
16891 return display_tag_value (tag, p, end);
16892 }
16893
16894 static unsigned char *
16895 display_gnu_attribute (unsigned char * p,
16896 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const),
16897 const unsigned char * const end)
16898 {
16899 unsigned int tag;
16900 unsigned int val;
16901
16902 READ_ULEB (tag, p, end);
16903
16904 /* Tag_compatibility is the only generic GNU attribute defined at
16905 present. */
16906 if (tag == 32)
16907 {
16908 READ_ULEB (val, p, end);
16909
16910 printf (_("flag = %d, vendor = "), val);
16911 if (p == end)
16912 {
16913 printf (_("<corrupt>\n"));
16914 warn (_("corrupt vendor attribute\n"));
16915 }
16916 else
16917 {
16918 if (p < end - 1)
16919 {
16920 size_t maxlen = (end - p) - 1;
16921
16922 print_symbol ((int) maxlen, (const char *) p);
16923 p += strnlen ((char *) p, maxlen) + 1;
16924 }
16925 else
16926 {
16927 printf (_("<corrupt>"));
16928 p = (unsigned char *) end;
16929 }
16930 putchar ('\n');
16931 }
16932 return p;
16933 }
16934
16935 if ((tag & 2) == 0 && display_proc_gnu_attribute)
16936 return display_proc_gnu_attribute (p, tag, end);
16937
16938 return display_tag_value (tag, p, end);
16939 }
16940
16941 static unsigned char *
16942 display_m68k_gnu_attribute (unsigned char * p,
16943 unsigned int tag,
16944 const unsigned char * const end)
16945 {
16946 unsigned int val;
16947
16948 if (tag == Tag_GNU_M68K_ABI_FP)
16949 {
16950 printf (" Tag_GNU_M68K_ABI_FP: ");
16951 if (p == end)
16952 {
16953 printf (_("<corrupt>\n"));
16954 return p;
16955 }
16956 READ_ULEB (val, p, end);
16957
16958 if (val > 3)
16959 printf ("(%#x), ", val);
16960
16961 switch (val & 3)
16962 {
16963 case 0:
16964 printf (_("unspecified hard/soft float\n"));
16965 break;
16966 case 1:
16967 printf (_("hard float\n"));
16968 break;
16969 case 2:
16970 printf (_("soft float\n"));
16971 break;
16972 }
16973 return p;
16974 }
16975
16976 return display_tag_value (tag & 1, p, end);
16977 }
16978
16979 static unsigned char *
16980 display_power_gnu_attribute (unsigned char * p,
16981 unsigned int tag,
16982 const unsigned char * const end)
16983 {
16984 unsigned int val;
16985
16986 if (tag == Tag_GNU_Power_ABI_FP)
16987 {
16988 printf (" Tag_GNU_Power_ABI_FP: ");
16989 if (p == end)
16990 {
16991 printf (_("<corrupt>\n"));
16992 return p;
16993 }
16994 READ_ULEB (val, p, end);
16995
16996 if (val > 15)
16997 printf ("(%#x), ", val);
16998
16999 switch (val & 3)
17000 {
17001 case 0:
17002 printf (_("unspecified hard/soft float, "));
17003 break;
17004 case 1:
17005 printf (_("hard float, "));
17006 break;
17007 case 2:
17008 printf (_("soft float, "));
17009 break;
17010 case 3:
17011 printf (_("single-precision hard float, "));
17012 break;
17013 }
17014
17015 switch (val & 0xC)
17016 {
17017 case 0:
17018 printf (_("unspecified long double\n"));
17019 break;
17020 case 4:
17021 printf (_("128-bit IBM long double\n"));
17022 break;
17023 case 8:
17024 printf (_("64-bit long double\n"));
17025 break;
17026 case 12:
17027 printf (_("128-bit IEEE long double\n"));
17028 break;
17029 }
17030 return p;
17031 }
17032
17033 if (tag == Tag_GNU_Power_ABI_Vector)
17034 {
17035 printf (" Tag_GNU_Power_ABI_Vector: ");
17036 if (p == end)
17037 {
17038 printf (_("<corrupt>\n"));
17039 return p;
17040 }
17041 READ_ULEB (val, p, end);
17042
17043 if (val > 3)
17044 printf ("(%#x), ", val);
17045
17046 switch (val & 3)
17047 {
17048 case 0:
17049 printf (_("unspecified\n"));
17050 break;
17051 case 1:
17052 printf (_("generic\n"));
17053 break;
17054 case 2:
17055 printf ("AltiVec\n");
17056 break;
17057 case 3:
17058 printf ("SPE\n");
17059 break;
17060 }
17061 return p;
17062 }
17063
17064 if (tag == Tag_GNU_Power_ABI_Struct_Return)
17065 {
17066 printf (" Tag_GNU_Power_ABI_Struct_Return: ");
17067 if (p == end)
17068 {
17069 printf (_("<corrupt>\n"));
17070 return p;
17071 }
17072 READ_ULEB (val, p, end);
17073
17074 if (val > 2)
17075 printf ("(%#x), ", val);
17076
17077 switch (val & 3)
17078 {
17079 case 0:
17080 printf (_("unspecified\n"));
17081 break;
17082 case 1:
17083 printf ("r3/r4\n");
17084 break;
17085 case 2:
17086 printf (_("memory\n"));
17087 break;
17088 case 3:
17089 printf ("???\n");
17090 break;
17091 }
17092 return p;
17093 }
17094
17095 return display_tag_value (tag & 1, p, end);
17096 }
17097
17098 static unsigned char *
17099 display_s390_gnu_attribute (unsigned char * p,
17100 unsigned int tag,
17101 const unsigned char * const end)
17102 {
17103 unsigned int val;
17104
17105 if (tag == Tag_GNU_S390_ABI_Vector)
17106 {
17107 printf (" Tag_GNU_S390_ABI_Vector: ");
17108 READ_ULEB (val, p, end);
17109
17110 switch (val)
17111 {
17112 case 0:
17113 printf (_("any\n"));
17114 break;
17115 case 1:
17116 printf (_("software\n"));
17117 break;
17118 case 2:
17119 printf (_("hardware\n"));
17120 break;
17121 default:
17122 printf ("??? (%d)\n", val);
17123 break;
17124 }
17125 return p;
17126 }
17127
17128 return display_tag_value (tag & 1, p, end);
17129 }
17130
17131 static void
17132 display_sparc_hwcaps (unsigned int mask)
17133 {
17134 if (mask)
17135 {
17136 bool first = true;
17137
17138 if (mask & ELF_SPARC_HWCAP_MUL32)
17139 fputs ("mul32", stdout), first = false;
17140 if (mask & ELF_SPARC_HWCAP_DIV32)
17141 printf ("%sdiv32", first ? "" : "|"), first = false;
17142 if (mask & ELF_SPARC_HWCAP_FSMULD)
17143 printf ("%sfsmuld", first ? "" : "|"), first = false;
17144 if (mask & ELF_SPARC_HWCAP_V8PLUS)
17145 printf ("%sv8plus", first ? "" : "|"), first = false;
17146 if (mask & ELF_SPARC_HWCAP_POPC)
17147 printf ("%spopc", first ? "" : "|"), first = false;
17148 if (mask & ELF_SPARC_HWCAP_VIS)
17149 printf ("%svis", first ? "" : "|"), first = false;
17150 if (mask & ELF_SPARC_HWCAP_VIS2)
17151 printf ("%svis2", first ? "" : "|"), first = false;
17152 if (mask & ELF_SPARC_HWCAP_ASI_BLK_INIT)
17153 printf ("%sASIBlkInit", first ? "" : "|"), first = false;
17154 if (mask & ELF_SPARC_HWCAP_FMAF)
17155 printf ("%sfmaf", first ? "" : "|"), first = false;
17156 if (mask & ELF_SPARC_HWCAP_VIS3)
17157 printf ("%svis3", first ? "" : "|"), first = false;
17158 if (mask & ELF_SPARC_HWCAP_HPC)
17159 printf ("%shpc", first ? "" : "|"), first = false;
17160 if (mask & ELF_SPARC_HWCAP_RANDOM)
17161 printf ("%srandom", first ? "" : "|"), first = false;
17162 if (mask & ELF_SPARC_HWCAP_TRANS)
17163 printf ("%strans", first ? "" : "|"), first = false;
17164 if (mask & ELF_SPARC_HWCAP_FJFMAU)
17165 printf ("%sfjfmau", first ? "" : "|"), first = false;
17166 if (mask & ELF_SPARC_HWCAP_IMA)
17167 printf ("%sima", first ? "" : "|"), first = false;
17168 if (mask & ELF_SPARC_HWCAP_ASI_CACHE_SPARING)
17169 printf ("%scspare", first ? "" : "|"), first = false;
17170 }
17171 else
17172 fputc ('0', stdout);
17173 fputc ('\n', stdout);
17174 }
17175
17176 static void
17177 display_sparc_hwcaps2 (unsigned int mask)
17178 {
17179 if (mask)
17180 {
17181 bool first = true;
17182
17183 if (mask & ELF_SPARC_HWCAP2_FJATHPLUS)
17184 fputs ("fjathplus", stdout), first = false;
17185 if (mask & ELF_SPARC_HWCAP2_VIS3B)
17186 printf ("%svis3b", first ? "" : "|"), first = false;
17187 if (mask & ELF_SPARC_HWCAP2_ADP)
17188 printf ("%sadp", first ? "" : "|"), first = false;
17189 if (mask & ELF_SPARC_HWCAP2_SPARC5)
17190 printf ("%ssparc5", first ? "" : "|"), first = false;
17191 if (mask & ELF_SPARC_HWCAP2_MWAIT)
17192 printf ("%smwait", first ? "" : "|"), first = false;
17193 if (mask & ELF_SPARC_HWCAP2_XMPMUL)
17194 printf ("%sxmpmul", first ? "" : "|"), first = false;
17195 if (mask & ELF_SPARC_HWCAP2_XMONT)
17196 printf ("%sxmont2", first ? "" : "|"), first = false;
17197 if (mask & ELF_SPARC_HWCAP2_NSEC)
17198 printf ("%snsec", first ? "" : "|"), first = false;
17199 if (mask & ELF_SPARC_HWCAP2_FJATHHPC)
17200 printf ("%sfjathhpc", first ? "" : "|"), first = false;
17201 if (mask & ELF_SPARC_HWCAP2_FJDES)
17202 printf ("%sfjdes", first ? "" : "|"), first = false;
17203 if (mask & ELF_SPARC_HWCAP2_FJAES)
17204 printf ("%sfjaes", first ? "" : "|"), first = false;
17205 }
17206 else
17207 fputc ('0', stdout);
17208 fputc ('\n', stdout);
17209 }
17210
17211 static unsigned char *
17212 display_sparc_gnu_attribute (unsigned char * p,
17213 unsigned int tag,
17214 const unsigned char * const end)
17215 {
17216 unsigned int val;
17217
17218 if (tag == Tag_GNU_Sparc_HWCAPS)
17219 {
17220 READ_ULEB (val, p, end);
17221 printf (" Tag_GNU_Sparc_HWCAPS: ");
17222 display_sparc_hwcaps (val);
17223 return p;
17224 }
17225 if (tag == Tag_GNU_Sparc_HWCAPS2)
17226 {
17227 READ_ULEB (val, p, end);
17228 printf (" Tag_GNU_Sparc_HWCAPS2: ");
17229 display_sparc_hwcaps2 (val);
17230 return p;
17231 }
17232
17233 return display_tag_value (tag, p, end);
17234 }
17235
17236 static void
17237 print_mips_fp_abi_value (unsigned int val)
17238 {
17239 switch (val)
17240 {
17241 case Val_GNU_MIPS_ABI_FP_ANY:
17242 printf (_("Hard or soft float\n"));
17243 break;
17244 case Val_GNU_MIPS_ABI_FP_DOUBLE:
17245 printf (_("Hard float (double precision)\n"));
17246 break;
17247 case Val_GNU_MIPS_ABI_FP_SINGLE:
17248 printf (_("Hard float (single precision)\n"));
17249 break;
17250 case Val_GNU_MIPS_ABI_FP_SOFT:
17251 printf (_("Soft float\n"));
17252 break;
17253 case Val_GNU_MIPS_ABI_FP_OLD_64:
17254 printf (_("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
17255 break;
17256 case Val_GNU_MIPS_ABI_FP_XX:
17257 printf (_("Hard float (32-bit CPU, Any FPU)\n"));
17258 break;
17259 case Val_GNU_MIPS_ABI_FP_64:
17260 printf (_("Hard float (32-bit CPU, 64-bit FPU)\n"));
17261 break;
17262 case Val_GNU_MIPS_ABI_FP_64A:
17263 printf (_("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
17264 break;
17265 case Val_GNU_MIPS_ABI_FP_NAN2008:
17266 printf (_("NaN 2008 compatibility\n"));
17267 break;
17268 default:
17269 printf ("??? (%d)\n", val);
17270 break;
17271 }
17272 }
17273
17274 static unsigned char *
17275 display_mips_gnu_attribute (unsigned char * p,
17276 unsigned int tag,
17277 const unsigned char * const end)
17278 {
17279 if (tag == Tag_GNU_MIPS_ABI_FP)
17280 {
17281 unsigned int val;
17282
17283 printf (" Tag_GNU_MIPS_ABI_FP: ");
17284 READ_ULEB (val, p, end);
17285 print_mips_fp_abi_value (val);
17286 return p;
17287 }
17288
17289 if (tag == Tag_GNU_MIPS_ABI_MSA)
17290 {
17291 unsigned int val;
17292
17293 printf (" Tag_GNU_MIPS_ABI_MSA: ");
17294 READ_ULEB (val, p, end);
17295
17296 switch (val)
17297 {
17298 case Val_GNU_MIPS_ABI_MSA_ANY:
17299 printf (_("Any MSA or not\n"));
17300 break;
17301 case Val_GNU_MIPS_ABI_MSA_128:
17302 printf (_("128-bit MSA\n"));
17303 break;
17304 default:
17305 printf ("??? (%d)\n", val);
17306 break;
17307 }
17308 return p;
17309 }
17310
17311 return display_tag_value (tag & 1, p, end);
17312 }
17313
17314 static unsigned char *
17315 display_tic6x_attribute (unsigned char * p,
17316 const unsigned char * const end)
17317 {
17318 unsigned int tag;
17319 unsigned int val;
17320
17321 READ_ULEB (tag, p, end);
17322
17323 switch (tag)
17324 {
17325 case Tag_ISA:
17326 printf (" Tag_ISA: ");
17327 READ_ULEB (val, p, end);
17328
17329 switch (val)
17330 {
17331 case C6XABI_Tag_ISA_none:
17332 printf (_("None\n"));
17333 break;
17334 case C6XABI_Tag_ISA_C62X:
17335 printf ("C62x\n");
17336 break;
17337 case C6XABI_Tag_ISA_C67X:
17338 printf ("C67x\n");
17339 break;
17340 case C6XABI_Tag_ISA_C67XP:
17341 printf ("C67x+\n");
17342 break;
17343 case C6XABI_Tag_ISA_C64X:
17344 printf ("C64x\n");
17345 break;
17346 case C6XABI_Tag_ISA_C64XP:
17347 printf ("C64x+\n");
17348 break;
17349 case C6XABI_Tag_ISA_C674X:
17350 printf ("C674x\n");
17351 break;
17352 default:
17353 printf ("??? (%d)\n", val);
17354 break;
17355 }
17356 return p;
17357
17358 case Tag_ABI_wchar_t:
17359 printf (" Tag_ABI_wchar_t: ");
17360 READ_ULEB (val, p, end);
17361 switch (val)
17362 {
17363 case 0:
17364 printf (_("Not used\n"));
17365 break;
17366 case 1:
17367 printf (_("2 bytes\n"));
17368 break;
17369 case 2:
17370 printf (_("4 bytes\n"));
17371 break;
17372 default:
17373 printf ("??? (%d)\n", val);
17374 break;
17375 }
17376 return p;
17377
17378 case Tag_ABI_stack_align_needed:
17379 printf (" Tag_ABI_stack_align_needed: ");
17380 READ_ULEB (val, p, end);
17381 switch (val)
17382 {
17383 case 0:
17384 printf (_("8-byte\n"));
17385 break;
17386 case 1:
17387 printf (_("16-byte\n"));
17388 break;
17389 default:
17390 printf ("??? (%d)\n", val);
17391 break;
17392 }
17393 return p;
17394
17395 case Tag_ABI_stack_align_preserved:
17396 READ_ULEB (val, p, end);
17397 printf (" Tag_ABI_stack_align_preserved: ");
17398 switch (val)
17399 {
17400 case 0:
17401 printf (_("8-byte\n"));
17402 break;
17403 case 1:
17404 printf (_("16-byte\n"));
17405 break;
17406 default:
17407 printf ("??? (%d)\n", val);
17408 break;
17409 }
17410 return p;
17411
17412 case Tag_ABI_DSBT:
17413 READ_ULEB (val, p, end);
17414 printf (" Tag_ABI_DSBT: ");
17415 switch (val)
17416 {
17417 case 0:
17418 printf (_("DSBT addressing not used\n"));
17419 break;
17420 case 1:
17421 printf (_("DSBT addressing used\n"));
17422 break;
17423 default:
17424 printf ("??? (%d)\n", val);
17425 break;
17426 }
17427 return p;
17428
17429 case Tag_ABI_PID:
17430 READ_ULEB (val, p, end);
17431 printf (" Tag_ABI_PID: ");
17432 switch (val)
17433 {
17434 case 0:
17435 printf (_("Data addressing position-dependent\n"));
17436 break;
17437 case 1:
17438 printf (_("Data addressing position-independent, GOT near DP\n"));
17439 break;
17440 case 2:
17441 printf (_("Data addressing position-independent, GOT far from DP\n"));
17442 break;
17443 default:
17444 printf ("??? (%d)\n", val);
17445 break;
17446 }
17447 return p;
17448
17449 case Tag_ABI_PIC:
17450 READ_ULEB (val, p, end);
17451 printf (" Tag_ABI_PIC: ");
17452 switch (val)
17453 {
17454 case 0:
17455 printf (_("Code addressing position-dependent\n"));
17456 break;
17457 case 1:
17458 printf (_("Code addressing position-independent\n"));
17459 break;
17460 default:
17461 printf ("??? (%d)\n", val);
17462 break;
17463 }
17464 return p;
17465
17466 case Tag_ABI_array_object_alignment:
17467 READ_ULEB (val, p, end);
17468 printf (" Tag_ABI_array_object_alignment: ");
17469 switch (val)
17470 {
17471 case 0:
17472 printf (_("8-byte\n"));
17473 break;
17474 case 1:
17475 printf (_("4-byte\n"));
17476 break;
17477 case 2:
17478 printf (_("16-byte\n"));
17479 break;
17480 default:
17481 printf ("??? (%d)\n", val);
17482 break;
17483 }
17484 return p;
17485
17486 case Tag_ABI_array_object_align_expected:
17487 READ_ULEB (val, p, end);
17488 printf (" Tag_ABI_array_object_align_expected: ");
17489 switch (val)
17490 {
17491 case 0:
17492 printf (_("8-byte\n"));
17493 break;
17494 case 1:
17495 printf (_("4-byte\n"));
17496 break;
17497 case 2:
17498 printf (_("16-byte\n"));
17499 break;
17500 default:
17501 printf ("??? (%d)\n", val);
17502 break;
17503 }
17504 return p;
17505
17506 case Tag_ABI_compatibility:
17507 {
17508 READ_ULEB (val, p, end);
17509 printf (" Tag_ABI_compatibility: ");
17510 printf (_("flag = %d, vendor = "), val);
17511 if (p < end - 1)
17512 {
17513 size_t maxlen = (end - p) - 1;
17514
17515 print_symbol ((int) maxlen, (const char *) p);
17516 p += strnlen ((char *) p, maxlen) + 1;
17517 }
17518 else
17519 {
17520 printf (_("<corrupt>"));
17521 p = (unsigned char *) end;
17522 }
17523 putchar ('\n');
17524 return p;
17525 }
17526
17527 case Tag_ABI_conformance:
17528 {
17529 printf (" Tag_ABI_conformance: \"");
17530 if (p < end - 1)
17531 {
17532 size_t maxlen = (end - p) - 1;
17533
17534 print_symbol ((int) maxlen, (const char *) p);
17535 p += strnlen ((char *) p, maxlen) + 1;
17536 }
17537 else
17538 {
17539 printf (_("<corrupt>"));
17540 p = (unsigned char *) end;
17541 }
17542 printf ("\"\n");
17543 return p;
17544 }
17545 }
17546
17547 return display_tag_value (tag, p, end);
17548 }
17549
17550 static void
17551 display_raw_attribute (unsigned char * p, unsigned char const * const end)
17552 {
17553 unsigned long addr = 0;
17554 size_t bytes = end - p;
17555
17556 assert (end >= p);
17557 while (bytes)
17558 {
17559 int j;
17560 int k;
17561 int lbytes = (bytes > 16 ? 16 : bytes);
17562
17563 printf (" 0x%8.8lx ", addr);
17564
17565 for (j = 0; j < 16; j++)
17566 {
17567 if (j < lbytes)
17568 printf ("%2.2x", p[j]);
17569 else
17570 printf (" ");
17571
17572 if ((j & 3) == 3)
17573 printf (" ");
17574 }
17575
17576 for (j = 0; j < lbytes; j++)
17577 {
17578 k = p[j];
17579 if (k >= ' ' && k < 0x7f)
17580 printf ("%c", k);
17581 else
17582 printf (".");
17583 }
17584
17585 putchar ('\n');
17586
17587 p += lbytes;
17588 bytes -= lbytes;
17589 addr += lbytes;
17590 }
17591
17592 putchar ('\n');
17593 }
17594
17595 static unsigned char *
17596 display_msp430_attribute (unsigned char * p,
17597 const unsigned char * const end)
17598 {
17599 unsigned int val;
17600 unsigned int tag;
17601
17602 READ_ULEB (tag, p, end);
17603
17604 switch (tag)
17605 {
17606 case OFBA_MSPABI_Tag_ISA:
17607 printf (" Tag_ISA: ");
17608 READ_ULEB (val, p, end);
17609 switch (val)
17610 {
17611 case 0: printf (_("None\n")); break;
17612 case 1: printf (_("MSP430\n")); break;
17613 case 2: printf (_("MSP430X\n")); break;
17614 default: printf ("??? (%d)\n", val); break;
17615 }
17616 break;
17617
17618 case OFBA_MSPABI_Tag_Code_Model:
17619 printf (" Tag_Code_Model: ");
17620 READ_ULEB (val, p, end);
17621 switch (val)
17622 {
17623 case 0: printf (_("None\n")); break;
17624 case 1: printf (_("Small\n")); break;
17625 case 2: printf (_("Large\n")); break;
17626 default: printf ("??? (%d)\n", val); break;
17627 }
17628 break;
17629
17630 case OFBA_MSPABI_Tag_Data_Model:
17631 printf (" Tag_Data_Model: ");
17632 READ_ULEB (val, p, end);
17633 switch (val)
17634 {
17635 case 0: printf (_("None\n")); break;
17636 case 1: printf (_("Small\n")); break;
17637 case 2: printf (_("Large\n")); break;
17638 case 3: printf (_("Restricted Large\n")); break;
17639 default: printf ("??? (%d)\n", val); break;
17640 }
17641 break;
17642
17643 default:
17644 printf (_(" <unknown tag %d>: "), tag);
17645
17646 if (tag & 1)
17647 {
17648 putchar ('"');
17649 if (p < end - 1)
17650 {
17651 size_t maxlen = (end - p) - 1;
17652
17653 print_symbol ((int) maxlen, (const char *) p);
17654 p += strnlen ((char *) p, maxlen) + 1;
17655 }
17656 else
17657 {
17658 printf (_("<corrupt>"));
17659 p = (unsigned char *) end;
17660 }
17661 printf ("\"\n");
17662 }
17663 else
17664 {
17665 READ_ULEB (val, p, end);
17666 printf ("%d (0x%x)\n", val, val);
17667 }
17668 break;
17669 }
17670
17671 assert (p <= end);
17672 return p;
17673 }
17674
17675 static unsigned char *
17676 display_msp430_gnu_attribute (unsigned char * p,
17677 unsigned int tag,
17678 const unsigned char * const end)
17679 {
17680 if (tag == Tag_GNU_MSP430_Data_Region)
17681 {
17682 unsigned int val;
17683
17684 printf (" Tag_GNU_MSP430_Data_Region: ");
17685 READ_ULEB (val, p, end);
17686
17687 switch (val)
17688 {
17689 case Val_GNU_MSP430_Data_Region_Any:
17690 printf (_("Any Region\n"));
17691 break;
17692 case Val_GNU_MSP430_Data_Region_Lower:
17693 printf (_("Lower Region Only\n"));
17694 break;
17695 default:
17696 printf ("??? (%u)\n", val);
17697 }
17698 return p;
17699 }
17700 return display_tag_value (tag & 1, p, end);
17701 }
17702
17703 struct riscv_attr_tag_t {
17704 const char *name;
17705 unsigned int tag;
17706 };
17707
17708 static struct riscv_attr_tag_t riscv_attr_tag[] =
17709 {
17710 #define T(tag) {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
17711 T(arch),
17712 T(priv_spec),
17713 T(priv_spec_minor),
17714 T(priv_spec_revision),
17715 T(unaligned_access),
17716 T(stack_align),
17717 #undef T
17718 };
17719
17720 static unsigned char *
17721 display_riscv_attribute (unsigned char *p,
17722 const unsigned char * const end)
17723 {
17724 unsigned int val;
17725 unsigned int tag;
17726 struct riscv_attr_tag_t *attr = NULL;
17727 unsigned i;
17728
17729 READ_ULEB (tag, p, end);
17730
17731 /* Find the name of attribute. */
17732 for (i = 0; i < ARRAY_SIZE (riscv_attr_tag); i++)
17733 {
17734 if (riscv_attr_tag[i].tag == tag)
17735 {
17736 attr = &riscv_attr_tag[i];
17737 break;
17738 }
17739 }
17740
17741 if (attr)
17742 printf (" %s: ", attr->name);
17743 else
17744 return display_tag_value (tag, p, end);
17745
17746 switch (tag)
17747 {
17748 case Tag_RISCV_priv_spec:
17749 case Tag_RISCV_priv_spec_minor:
17750 case Tag_RISCV_priv_spec_revision:
17751 READ_ULEB (val, p, end);
17752 printf (_("%u\n"), val);
17753 break;
17754 case Tag_RISCV_unaligned_access:
17755 READ_ULEB (val, p, end);
17756 switch (val)
17757 {
17758 case 0:
17759 printf (_("No unaligned access\n"));
17760 break;
17761 case 1:
17762 printf (_("Unaligned access\n"));
17763 break;
17764 }
17765 break;
17766 case Tag_RISCV_stack_align:
17767 READ_ULEB (val, p, end);
17768 printf (_("%u-bytes\n"), val);
17769 break;
17770 case Tag_RISCV_arch:
17771 p = display_tag_value (-1, p, end);
17772 break;
17773 default:
17774 return display_tag_value (tag, p, end);
17775 }
17776
17777 return p;
17778 }
17779
17780 static unsigned char *
17781 display_csky_attribute (unsigned char * p,
17782 const unsigned char * const end)
17783 {
17784 unsigned int tag;
17785 unsigned int val;
17786 READ_ULEB (tag, p, end);
17787
17788 if (tag >= Tag_CSKY_MAX)
17789 {
17790 return display_tag_value (-1, p, end);
17791 }
17792
17793 switch (tag)
17794 {
17795 case Tag_CSKY_ARCH_NAME:
17796 printf (" Tag_CSKY_ARCH_NAME:\t\t");
17797 return display_tag_value (-1, p, end);
17798 case Tag_CSKY_CPU_NAME:
17799 printf (" Tag_CSKY_CPU_NAME:\t\t");
17800 return display_tag_value (-1, p, end);
17801
17802 case Tag_CSKY_ISA_FLAGS:
17803 printf (" Tag_CSKY_ISA_FLAGS:\t\t");
17804 return display_tag_value (0, p, end);
17805 case Tag_CSKY_ISA_EXT_FLAGS:
17806 printf (" Tag_CSKY_ISA_EXT_FLAGS:\t");
17807 return display_tag_value (0, p, end);
17808
17809 case Tag_CSKY_DSP_VERSION:
17810 printf (" Tag_CSKY_DSP_VERSION:\t\t");
17811 READ_ULEB (val, p, end);
17812 if (val == VAL_CSKY_DSP_VERSION_EXTENSION)
17813 printf ("DSP Extension\n");
17814 else if (val == VAL_CSKY_DSP_VERSION_2)
17815 printf ("DSP 2.0\n");
17816 break;
17817
17818 case Tag_CSKY_VDSP_VERSION:
17819 printf (" Tag_CSKY_VDSP_VERSION:\t");
17820 READ_ULEB (val, p, end);
17821 printf ("VDSP Version %d\n", val);
17822 break;
17823
17824 case Tag_CSKY_FPU_VERSION:
17825 printf (" Tag_CSKY_FPU_VERSION:\t\t");
17826 READ_ULEB (val, p, end);
17827 if (val == VAL_CSKY_FPU_VERSION_1)
17828 printf ("ABIV1 FPU Version 1\n");
17829 else if (val == VAL_CSKY_FPU_VERSION_2)
17830 printf ("FPU Version 2\n");
17831 break;
17832
17833 case Tag_CSKY_FPU_ABI:
17834 printf (" Tag_CSKY_FPU_ABI:\t\t");
17835 READ_ULEB (val, p, end);
17836 if (val == VAL_CSKY_FPU_ABI_HARD)
17837 printf ("Hard\n");
17838 else if (val == VAL_CSKY_FPU_ABI_SOFTFP)
17839 printf ("SoftFP\n");
17840 else if (val == VAL_CSKY_FPU_ABI_SOFT)
17841 printf ("Soft\n");
17842 break;
17843 case Tag_CSKY_FPU_ROUNDING:
17844 READ_ULEB (val, p, end);
17845 if (val == 1)
17846 {
17847 printf (" Tag_CSKY_FPU_ROUNDING:\t");
17848 printf ("Needed\n");
17849 }
17850 break;
17851 case Tag_CSKY_FPU_DENORMAL:
17852 READ_ULEB (val, p, end);
17853 if (val == 1)
17854 {
17855 printf (" Tag_CSKY_FPU_DENORMAL:\t");
17856 printf ("Needed\n");
17857 }
17858 break;
17859 case Tag_CSKY_FPU_Exception:
17860 READ_ULEB (val, p, end);
17861 if (val == 1)
17862 {
17863 printf (" Tag_CSKY_FPU_Exception:\t");
17864 printf ("Needed\n");
17865 }
17866 break;
17867 case Tag_CSKY_FPU_NUMBER_MODULE:
17868 printf (" Tag_CSKY_FPU_NUMBER_MODULE:\t");
17869 return display_tag_value (-1, p, end);
17870 case Tag_CSKY_FPU_HARDFP:
17871 printf (" Tag_CSKY_FPU_HARDFP:\t\t");
17872 READ_ULEB (val, p, end);
17873 if (val & VAL_CSKY_FPU_HARDFP_HALF)
17874 printf (" Half");
17875 if (val & VAL_CSKY_FPU_HARDFP_SINGLE)
17876 printf (" Single");
17877 if (val & VAL_CSKY_FPU_HARDFP_DOUBLE)
17878 printf (" Double");
17879 printf ("\n");
17880 break;
17881 default:
17882 return display_tag_value (tag, p, end);
17883 }
17884 return p;
17885 }
17886
17887 static bool
17888 process_attributes (Filedata * filedata,
17889 const char * public_name,
17890 unsigned int proc_type,
17891 unsigned char * (* display_pub_attribute) (unsigned char *, const unsigned char * const),
17892 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const))
17893 {
17894 Elf_Internal_Shdr * sect;
17895 unsigned i;
17896 bool res = true;
17897
17898 /* Find the section header so that we get the size. */
17899 for (i = 0, sect = filedata->section_headers;
17900 i < filedata->file_header.e_shnum;
17901 i++, sect++)
17902 {
17903 unsigned char * contents;
17904 unsigned char * p;
17905
17906 if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
17907 continue;
17908
17909 contents = (unsigned char *) get_data (NULL, filedata, sect->sh_offset, 1,
17910 sect->sh_size, _("attributes"));
17911 if (contents == NULL)
17912 {
17913 res = false;
17914 continue;
17915 }
17916
17917 p = contents;
17918 /* The first character is the version of the attributes.
17919 Currently only version 1, (aka 'A') is recognised here. */
17920 if (*p != 'A')
17921 {
17922 printf (_("Unknown attributes version '%c'(%d) - expecting 'A'\n"), *p, *p);
17923 res = false;
17924 }
17925 else
17926 {
17927 uint64_t section_len;
17928
17929 section_len = sect->sh_size - 1;
17930 p++;
17931
17932 while (section_len > 0)
17933 {
17934 uint64_t attr_len;
17935 unsigned int namelen;
17936 bool public_section;
17937 bool gnu_section;
17938
17939 if (section_len <= 4)
17940 {
17941 error (_("Tag section ends prematurely\n"));
17942 res = false;
17943 break;
17944 }
17945 attr_len = byte_get (p, 4);
17946 p += 4;
17947
17948 if (attr_len > section_len)
17949 {
17950 error (_("Bad attribute length (%u > %u)\n"),
17951 (unsigned) attr_len, (unsigned) section_len);
17952 attr_len = section_len;
17953 res = false;
17954 }
17955 /* PR 17531: file: 001-101425-0.004 */
17956 else if (attr_len < 5)
17957 {
17958 error (_("Attribute length of %u is too small\n"), (unsigned) attr_len);
17959 res = false;
17960 break;
17961 }
17962
17963 section_len -= attr_len;
17964 attr_len -= 4;
17965
17966 namelen = strnlen ((char *) p, attr_len) + 1;
17967 if (namelen == 0 || namelen >= attr_len)
17968 {
17969 error (_("Corrupt attribute section name\n"));
17970 res = false;
17971 break;
17972 }
17973
17974 printf (_("Attribute Section: "));
17975 print_symbol (INT_MAX, (const char *) p);
17976 putchar ('\n');
17977
17978 if (public_name && streq ((char *) p, public_name))
17979 public_section = true;
17980 else
17981 public_section = false;
17982
17983 if (streq ((char *) p, "gnu"))
17984 gnu_section = true;
17985 else
17986 gnu_section = false;
17987
17988 p += namelen;
17989 attr_len -= namelen;
17990
17991 while (attr_len > 0 && p < contents + sect->sh_size)
17992 {
17993 int tag;
17994 unsigned int val;
17995 uint64_t size;
17996 unsigned char * end;
17997
17998 /* PR binutils/17531: Safe handling of corrupt files. */
17999 if (attr_len < 6)
18000 {
18001 error (_("Unused bytes at end of section\n"));
18002 res = false;
18003 section_len = 0;
18004 break;
18005 }
18006
18007 tag = *(p++);
18008 size = byte_get (p, 4);
18009 if (size > attr_len)
18010 {
18011 error (_("Bad subsection length (%u > %u)\n"),
18012 (unsigned) size, (unsigned) attr_len);
18013 res = false;
18014 size = attr_len;
18015 }
18016 /* PR binutils/17531: Safe handling of corrupt files. */
18017 if (size < 6)
18018 {
18019 error (_("Bad subsection length (%u < 6)\n"),
18020 (unsigned) size);
18021 res = false;
18022 section_len = 0;
18023 break;
18024 }
18025
18026 attr_len -= size;
18027 end = p + size - 1;
18028 assert (end <= contents + sect->sh_size);
18029 p += 4;
18030
18031 switch (tag)
18032 {
18033 case 1:
18034 printf (_("File Attributes\n"));
18035 break;
18036 case 2:
18037 printf (_("Section Attributes:"));
18038 goto do_numlist;
18039 case 3:
18040 printf (_("Symbol Attributes:"));
18041 /* Fall through. */
18042 do_numlist:
18043 for (;;)
18044 {
18045 READ_ULEB (val, p, end);
18046 if (val == 0)
18047 break;
18048 printf (" %d", val);
18049 }
18050 printf ("\n");
18051 break;
18052 default:
18053 printf (_("Unknown tag: %d\n"), tag);
18054 public_section = false;
18055 break;
18056 }
18057
18058 if (public_section && display_pub_attribute != NULL)
18059 {
18060 while (p < end)
18061 p = display_pub_attribute (p, end);
18062 assert (p == end);
18063 }
18064 else if (gnu_section && display_proc_gnu_attribute != NULL)
18065 {
18066 while (p < end)
18067 p = display_gnu_attribute (p,
18068 display_proc_gnu_attribute,
18069 end);
18070 assert (p == end);
18071 }
18072 else if (p < end)
18073 {
18074 printf (_(" Unknown attribute:\n"));
18075 display_raw_attribute (p, end);
18076 p = end;
18077 }
18078 else
18079 attr_len = 0;
18080 }
18081 }
18082 }
18083
18084 free (contents);
18085 }
18086
18087 return res;
18088 }
18089
18090 /* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
18091 Print the Address, Access and Initial fields of an entry at VMA ADDR
18092 and return the VMA of the next entry, or -1 if there was a problem.
18093 Does not read from DATA_END or beyond. */
18094
18095 static uint64_t
18096 print_mips_got_entry (unsigned char * data, uint64_t pltgot, uint64_t addr,
18097 unsigned char * data_end)
18098 {
18099 printf (" ");
18100 print_vma (addr, LONG_HEX);
18101 printf (" ");
18102 if (addr < pltgot + 0xfff0)
18103 printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
18104 else
18105 printf ("%10s", "");
18106 printf (" ");
18107 if (data == NULL)
18108 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
18109 else
18110 {
18111 uint64_t entry;
18112 unsigned char * from = data + addr - pltgot;
18113
18114 if (from + (is_32bit_elf ? 4 : 8) > data_end)
18115 {
18116 warn (_("MIPS GOT entry extends beyond the end of available data\n"));
18117 printf ("%*s", is_32bit_elf ? 8 : 16, _("<corrupt>"));
18118 return (uint64_t) -1;
18119 }
18120 else
18121 {
18122 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
18123 print_vma (entry, LONG_HEX);
18124 }
18125 }
18126 return addr + (is_32bit_elf ? 4 : 8);
18127 }
18128
18129 /* DATA points to the contents of a MIPS PLT GOT that starts at VMA
18130 PLTGOT. Print the Address and Initial fields of an entry at VMA
18131 ADDR and return the VMA of the next entry. */
18132
18133 static uint64_t
18134 print_mips_pltgot_entry (unsigned char * data, uint64_t pltgot, uint64_t addr)
18135 {
18136 printf (" ");
18137 print_vma (addr, LONG_HEX);
18138 printf (" ");
18139 if (data == NULL)
18140 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
18141 else
18142 {
18143 uint64_t entry;
18144
18145 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
18146 print_vma (entry, LONG_HEX);
18147 }
18148 return addr + (is_32bit_elf ? 4 : 8);
18149 }
18150
18151 static void
18152 print_mips_ases (unsigned int mask)
18153 {
18154 if (mask & AFL_ASE_DSP)
18155 fputs ("\n\tDSP ASE", stdout);
18156 if (mask & AFL_ASE_DSPR2)
18157 fputs ("\n\tDSP R2 ASE", stdout);
18158 if (mask & AFL_ASE_DSPR3)
18159 fputs ("\n\tDSP R3 ASE", stdout);
18160 if (mask & AFL_ASE_EVA)
18161 fputs ("\n\tEnhanced VA Scheme", stdout);
18162 if (mask & AFL_ASE_MCU)
18163 fputs ("\n\tMCU (MicroController) ASE", stdout);
18164 if (mask & AFL_ASE_MDMX)
18165 fputs ("\n\tMDMX ASE", stdout);
18166 if (mask & AFL_ASE_MIPS3D)
18167 fputs ("\n\tMIPS-3D ASE", stdout);
18168 if (mask & AFL_ASE_MT)
18169 fputs ("\n\tMT ASE", stdout);
18170 if (mask & AFL_ASE_SMARTMIPS)
18171 fputs ("\n\tSmartMIPS ASE", stdout);
18172 if (mask & AFL_ASE_VIRT)
18173 fputs ("\n\tVZ ASE", stdout);
18174 if (mask & AFL_ASE_MSA)
18175 fputs ("\n\tMSA ASE", stdout);
18176 if (mask & AFL_ASE_MIPS16)
18177 fputs ("\n\tMIPS16 ASE", stdout);
18178 if (mask & AFL_ASE_MICROMIPS)
18179 fputs ("\n\tMICROMIPS ASE", stdout);
18180 if (mask & AFL_ASE_XPA)
18181 fputs ("\n\tXPA ASE", stdout);
18182 if (mask & AFL_ASE_MIPS16E2)
18183 fputs ("\n\tMIPS16e2 ASE", stdout);
18184 if (mask & AFL_ASE_CRC)
18185 fputs ("\n\tCRC ASE", stdout);
18186 if (mask & AFL_ASE_GINV)
18187 fputs ("\n\tGINV ASE", stdout);
18188 if (mask & AFL_ASE_LOONGSON_MMI)
18189 fputs ("\n\tLoongson MMI ASE", stdout);
18190 if (mask & AFL_ASE_LOONGSON_CAM)
18191 fputs ("\n\tLoongson CAM ASE", stdout);
18192 if (mask & AFL_ASE_LOONGSON_EXT)
18193 fputs ("\n\tLoongson EXT ASE", stdout);
18194 if (mask & AFL_ASE_LOONGSON_EXT2)
18195 fputs ("\n\tLoongson EXT2 ASE", stdout);
18196 if (mask == 0)
18197 fprintf (stdout, "\n\t%s", _("None"));
18198 else if ((mask & ~AFL_ASE_MASK) != 0)
18199 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
18200 }
18201
18202 static void
18203 print_mips_isa_ext (unsigned int isa_ext)
18204 {
18205 switch (isa_ext)
18206 {
18207 case 0:
18208 fputs (_("None"), stdout);
18209 break;
18210 case AFL_EXT_XLR:
18211 fputs ("RMI XLR", stdout);
18212 break;
18213 case AFL_EXT_OCTEON3:
18214 fputs ("Cavium Networks Octeon3", stdout);
18215 break;
18216 case AFL_EXT_OCTEON2:
18217 fputs ("Cavium Networks Octeon2", stdout);
18218 break;
18219 case AFL_EXT_OCTEONP:
18220 fputs ("Cavium Networks OcteonP", stdout);
18221 break;
18222 case AFL_EXT_OCTEON:
18223 fputs ("Cavium Networks Octeon", stdout);
18224 break;
18225 case AFL_EXT_5900:
18226 fputs ("Toshiba R5900", stdout);
18227 break;
18228 case AFL_EXT_4650:
18229 fputs ("MIPS R4650", stdout);
18230 break;
18231 case AFL_EXT_4010:
18232 fputs ("LSI R4010", stdout);
18233 break;
18234 case AFL_EXT_4100:
18235 fputs ("NEC VR4100", stdout);
18236 break;
18237 case AFL_EXT_3900:
18238 fputs ("Toshiba R3900", stdout);
18239 break;
18240 case AFL_EXT_10000:
18241 fputs ("MIPS R10000", stdout);
18242 break;
18243 case AFL_EXT_SB1:
18244 fputs ("Broadcom SB-1", stdout);
18245 break;
18246 case AFL_EXT_4111:
18247 fputs ("NEC VR4111/VR4181", stdout);
18248 break;
18249 case AFL_EXT_4120:
18250 fputs ("NEC VR4120", stdout);
18251 break;
18252 case AFL_EXT_5400:
18253 fputs ("NEC VR5400", stdout);
18254 break;
18255 case AFL_EXT_5500:
18256 fputs ("NEC VR5500", stdout);
18257 break;
18258 case AFL_EXT_LOONGSON_2E:
18259 fputs ("ST Microelectronics Loongson 2E", stdout);
18260 break;
18261 case AFL_EXT_LOONGSON_2F:
18262 fputs ("ST Microelectronics Loongson 2F", stdout);
18263 break;
18264 case AFL_EXT_INTERAPTIV_MR2:
18265 fputs ("Imagination interAptiv MR2", stdout);
18266 break;
18267 default:
18268 fprintf (stdout, "%s (%d)", _("Unknown"), isa_ext);
18269 }
18270 }
18271
18272 static signed int
18273 get_mips_reg_size (int reg_size)
18274 {
18275 return (reg_size == AFL_REG_NONE) ? 0
18276 : (reg_size == AFL_REG_32) ? 32
18277 : (reg_size == AFL_REG_64) ? 64
18278 : (reg_size == AFL_REG_128) ? 128
18279 : -1;
18280 }
18281
18282 static bool
18283 process_mips_specific (Filedata * filedata)
18284 {
18285 Elf_Internal_Dyn * entry;
18286 Elf_Internal_Shdr *sect = NULL;
18287 size_t liblist_offset = 0;
18288 size_t liblistno = 0;
18289 size_t conflictsno = 0;
18290 size_t options_offset = 0;
18291 size_t conflicts_offset = 0;
18292 size_t pltrelsz = 0;
18293 size_t pltrel = 0;
18294 uint64_t pltgot = 0;
18295 uint64_t mips_pltgot = 0;
18296 uint64_t jmprel = 0;
18297 uint64_t local_gotno = 0;
18298 uint64_t gotsym = 0;
18299 uint64_t symtabno = 0;
18300 bool res = true;
18301
18302 if (! process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
18303 display_mips_gnu_attribute))
18304 res = false;
18305
18306 sect = find_section (filedata, ".MIPS.abiflags");
18307
18308 if (sect != NULL)
18309 {
18310 Elf_External_ABIFlags_v0 *abiflags_ext;
18311 Elf_Internal_ABIFlags_v0 abiflags_in;
18312
18313 if (sizeof (Elf_External_ABIFlags_v0) != sect->sh_size)
18314 {
18315 error (_("Corrupt MIPS ABI Flags section.\n"));
18316 res = false;
18317 }
18318 else
18319 {
18320 abiflags_ext = get_data (NULL, filedata, sect->sh_offset, 1,
18321 sect->sh_size, _("MIPS ABI Flags section"));
18322 if (abiflags_ext)
18323 {
18324 abiflags_in.version = BYTE_GET (abiflags_ext->version);
18325 abiflags_in.isa_level = BYTE_GET (abiflags_ext->isa_level);
18326 abiflags_in.isa_rev = BYTE_GET (abiflags_ext->isa_rev);
18327 abiflags_in.gpr_size = BYTE_GET (abiflags_ext->gpr_size);
18328 abiflags_in.cpr1_size = BYTE_GET (abiflags_ext->cpr1_size);
18329 abiflags_in.cpr2_size = BYTE_GET (abiflags_ext->cpr2_size);
18330 abiflags_in.fp_abi = BYTE_GET (abiflags_ext->fp_abi);
18331 abiflags_in.isa_ext = BYTE_GET (abiflags_ext->isa_ext);
18332 abiflags_in.ases = BYTE_GET (abiflags_ext->ases);
18333 abiflags_in.flags1 = BYTE_GET (abiflags_ext->flags1);
18334 abiflags_in.flags2 = BYTE_GET (abiflags_ext->flags2);
18335
18336 printf ("\nMIPS ABI Flags Version: %d\n", abiflags_in.version);
18337 printf ("\nISA: MIPS%d", abiflags_in.isa_level);
18338 if (abiflags_in.isa_rev > 1)
18339 printf ("r%d", abiflags_in.isa_rev);
18340 printf ("\nGPR size: %d",
18341 get_mips_reg_size (abiflags_in.gpr_size));
18342 printf ("\nCPR1 size: %d",
18343 get_mips_reg_size (abiflags_in.cpr1_size));
18344 printf ("\nCPR2 size: %d",
18345 get_mips_reg_size (abiflags_in.cpr2_size));
18346 fputs ("\nFP ABI: ", stdout);
18347 print_mips_fp_abi_value (abiflags_in.fp_abi);
18348 fputs ("ISA Extension: ", stdout);
18349 print_mips_isa_ext (abiflags_in.isa_ext);
18350 fputs ("\nASEs:", stdout);
18351 print_mips_ases (abiflags_in.ases);
18352 printf ("\nFLAGS 1: %8.8lx", abiflags_in.flags1);
18353 printf ("\nFLAGS 2: %8.8lx", abiflags_in.flags2);
18354 fputc ('\n', stdout);
18355 free (abiflags_ext);
18356 }
18357 }
18358 }
18359
18360 /* We have a lot of special sections. Thanks SGI! */
18361 if (filedata->dynamic_section == NULL)
18362 {
18363 /* No dynamic information available. See if there is static GOT. */
18364 sect = find_section (filedata, ".got");
18365 if (sect != NULL)
18366 {
18367 unsigned char *data_end;
18368 unsigned char *data;
18369 uint64_t ent, end;
18370 int addr_size;
18371
18372 pltgot = sect->sh_addr;
18373
18374 ent = pltgot;
18375 addr_size = (is_32bit_elf ? 4 : 8);
18376 end = pltgot + sect->sh_size;
18377
18378 data = (unsigned char *) get_data (NULL, filedata, sect->sh_offset,
18379 end - pltgot, 1,
18380 _("Global Offset Table data"));
18381 /* PR 12855: Null data is handled gracefully throughout. */
18382 data_end = data + (end - pltgot);
18383
18384 printf (_("\nStatic GOT:\n"));
18385 printf (_(" Canonical gp value: "));
18386 print_vma (ent + 0x7ff0, LONG_HEX);
18387 printf ("\n\n");
18388
18389 /* In a dynamic binary GOT[0] is reserved for the dynamic
18390 loader to store the lazy resolver pointer, however in
18391 a static binary it may well have been omitted and GOT
18392 reduced to a table of addresses.
18393 PR 21344: Check for the entry being fully available
18394 before fetching it. */
18395 if (data
18396 && data + ent - pltgot + addr_size <= data_end
18397 && byte_get (data + ent - pltgot, addr_size) == 0)
18398 {
18399 printf (_(" Reserved entries:\n"));
18400 printf (_(" %*s %10s %*s\n"),
18401 addr_size * 2, _("Address"), _("Access"),
18402 addr_size * 2, _("Value"));
18403 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18404 printf ("\n");
18405 if (ent == (uint64_t) -1)
18406 goto sgot_print_fail;
18407
18408 /* Check for the MSB of GOT[1] being set, identifying a
18409 GNU object. This entry will be used by some runtime
18410 loaders, to store the module pointer. Otherwise this
18411 is an ordinary local entry.
18412 PR 21344: Check for the entry being fully available
18413 before fetching it. */
18414 if (data
18415 && data + ent - pltgot + addr_size <= data_end
18416 && (byte_get (data + ent - pltgot, addr_size)
18417 >> (addr_size * 8 - 1)) != 0)
18418 {
18419 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18420 printf ("\n");
18421 if (ent == (uint64_t) -1)
18422 goto sgot_print_fail;
18423 }
18424 printf ("\n");
18425 }
18426
18427 if (data != NULL && ent < end)
18428 {
18429 printf (_(" Local entries:\n"));
18430 printf (" %*s %10s %*s\n",
18431 addr_size * 2, _("Address"), _("Access"),
18432 addr_size * 2, _("Value"));
18433 while (ent < end)
18434 {
18435 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18436 printf ("\n");
18437 if (ent == (uint64_t) -1)
18438 goto sgot_print_fail;
18439 }
18440 printf ("\n");
18441 }
18442
18443 sgot_print_fail:
18444 free (data);
18445 }
18446 return res;
18447 }
18448
18449 for (entry = filedata->dynamic_section;
18450 /* PR 17531 file: 012-50589-0.004. */
18451 (entry < filedata->dynamic_section + filedata->dynamic_nent
18452 && entry->d_tag != DT_NULL);
18453 ++entry)
18454 switch (entry->d_tag)
18455 {
18456 case DT_MIPS_LIBLIST:
18457 liblist_offset
18458 = offset_from_vma (filedata, entry->d_un.d_val,
18459 liblistno * sizeof (Elf32_External_Lib));
18460 break;
18461 case DT_MIPS_LIBLISTNO:
18462 liblistno = entry->d_un.d_val;
18463 break;
18464 case DT_MIPS_OPTIONS:
18465 options_offset = offset_from_vma (filedata, entry->d_un.d_val, 0);
18466 break;
18467 case DT_MIPS_CONFLICT:
18468 conflicts_offset
18469 = offset_from_vma (filedata, entry->d_un.d_val,
18470 conflictsno * sizeof (Elf32_External_Conflict));
18471 break;
18472 case DT_MIPS_CONFLICTNO:
18473 conflictsno = entry->d_un.d_val;
18474 break;
18475 case DT_PLTGOT:
18476 pltgot = entry->d_un.d_ptr;
18477 break;
18478 case DT_MIPS_LOCAL_GOTNO:
18479 local_gotno = entry->d_un.d_val;
18480 break;
18481 case DT_MIPS_GOTSYM:
18482 gotsym = entry->d_un.d_val;
18483 break;
18484 case DT_MIPS_SYMTABNO:
18485 symtabno = entry->d_un.d_val;
18486 break;
18487 case DT_MIPS_PLTGOT:
18488 mips_pltgot = entry->d_un.d_ptr;
18489 break;
18490 case DT_PLTREL:
18491 pltrel = entry->d_un.d_val;
18492 break;
18493 case DT_PLTRELSZ:
18494 pltrelsz = entry->d_un.d_val;
18495 break;
18496 case DT_JMPREL:
18497 jmprel = entry->d_un.d_ptr;
18498 break;
18499 default:
18500 break;
18501 }
18502
18503 if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
18504 {
18505 Elf32_External_Lib * elib;
18506 size_t cnt;
18507
18508 elib = (Elf32_External_Lib *) get_data (NULL, filedata, liblist_offset,
18509 sizeof (Elf32_External_Lib),
18510 liblistno,
18511 _("liblist section data"));
18512 if (elib)
18513 {
18514 printf (ngettext ("\nSection '.liblist' contains %lu entry:\n",
18515 "\nSection '.liblist' contains %lu entries:\n",
18516 (unsigned long) liblistno),
18517 (unsigned long) liblistno);
18518 fputs (_(" Library Time Stamp Checksum Version Flags\n"),
18519 stdout);
18520
18521 for (cnt = 0; cnt < liblistno; ++cnt)
18522 {
18523 Elf32_Lib liblist;
18524 time_t atime;
18525 char timebuf[128];
18526 struct tm * tmp;
18527
18528 liblist.l_name = BYTE_GET (elib[cnt].l_name);
18529 atime = BYTE_GET (elib[cnt].l_time_stamp);
18530 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
18531 liblist.l_version = BYTE_GET (elib[cnt].l_version);
18532 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
18533
18534 tmp = gmtime (&atime);
18535 snprintf (timebuf, sizeof (timebuf),
18536 "%04u-%02u-%02uT%02u:%02u:%02u",
18537 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
18538 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
18539
18540 printf ("%3lu: ", (unsigned long) cnt);
18541 if (valid_dynamic_name (filedata, liblist.l_name))
18542 print_symbol (20, get_dynamic_name (filedata, liblist.l_name));
18543 else
18544 printf (_("<corrupt: %9ld>"), liblist.l_name);
18545 printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
18546 liblist.l_version);
18547
18548 if (liblist.l_flags == 0)
18549 puts (_(" NONE"));
18550 else
18551 {
18552 static const struct
18553 {
18554 const char * name;
18555 int bit;
18556 }
18557 l_flags_vals[] =
18558 {
18559 { " EXACT_MATCH", LL_EXACT_MATCH },
18560 { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
18561 { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
18562 { " EXPORTS", LL_EXPORTS },
18563 { " DELAY_LOAD", LL_DELAY_LOAD },
18564 { " DELTA", LL_DELTA }
18565 };
18566 int flags = liblist.l_flags;
18567 size_t fcnt;
18568
18569 for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
18570 if ((flags & l_flags_vals[fcnt].bit) != 0)
18571 {
18572 fputs (l_flags_vals[fcnt].name, stdout);
18573 flags ^= l_flags_vals[fcnt].bit;
18574 }
18575 if (flags != 0)
18576 printf (" %#x", (unsigned int) flags);
18577
18578 puts ("");
18579 }
18580 }
18581
18582 free (elib);
18583 }
18584 else
18585 res = false;
18586 }
18587
18588 if (options_offset != 0)
18589 {
18590 Elf_External_Options * eopt;
18591 size_t offset;
18592 int cnt;
18593
18594 /* Find the section header so that we get the size. */
18595 sect = find_section_by_type (filedata, SHT_MIPS_OPTIONS);
18596 /* PR 17533 file: 012-277276-0.004. */
18597 if (sect == NULL)
18598 {
18599 error (_("No MIPS_OPTIONS header found\n"));
18600 return false;
18601 }
18602 /* PR 24243 */
18603 if (sect->sh_size < sizeof (* eopt))
18604 {
18605 error (_("The MIPS options section is too small.\n"));
18606 return false;
18607 }
18608
18609 eopt = (Elf_External_Options *) get_data (NULL, filedata, options_offset, 1,
18610 sect->sh_size, _("options"));
18611 if (eopt)
18612 {
18613 Elf_Internal_Options option;
18614
18615 offset = cnt = 0;
18616 while (offset <= sect->sh_size - sizeof (* eopt))
18617 {
18618 Elf_External_Options * eoption;
18619 unsigned int optsize;
18620
18621 eoption = (Elf_External_Options *) ((char *) eopt + offset);
18622
18623 optsize = BYTE_GET (eoption->size);
18624
18625 /* PR 17531: file: ffa0fa3b. */
18626 if (optsize < sizeof (* eopt)
18627 || optsize > sect->sh_size - offset)
18628 {
18629 error (_("Invalid size (%u) for MIPS option\n"),
18630 optsize);
18631 free (eopt);
18632 return false;
18633 }
18634 offset += optsize;
18635 ++cnt;
18636 }
18637
18638 printf (ngettext ("\nSection '%s' contains %d entry:\n",
18639 "\nSection '%s' contains %d entries:\n",
18640 cnt),
18641 printable_section_name (filedata, sect), cnt);
18642
18643 offset = 0;
18644 while (cnt-- > 0)
18645 {
18646 size_t len;
18647 Elf_External_Options * eoption;
18648
18649 eoption = (Elf_External_Options *) ((char *) eopt + offset);
18650
18651 option.kind = BYTE_GET (eoption->kind);
18652 option.size = BYTE_GET (eoption->size);
18653 option.section = BYTE_GET (eoption->section);
18654 option.info = BYTE_GET (eoption->info);
18655
18656 switch (option.kind)
18657 {
18658 case ODK_NULL:
18659 /* This shouldn't happen. */
18660 printf (" NULL %" PRId16 " %" PRIx32,
18661 option.section, option.info);
18662 break;
18663
18664 case ODK_REGINFO:
18665 printf (" REGINFO ");
18666 if (filedata->file_header.e_machine == EM_MIPS)
18667 {
18668 Elf32_External_RegInfo * ereg;
18669 Elf32_RegInfo reginfo;
18670
18671 /* 32bit form. */
18672 if (option.size < (sizeof (Elf_External_Options)
18673 + sizeof (Elf32_External_RegInfo)))
18674 {
18675 printf (_("<corrupt>\n"));
18676 error (_("Truncated MIPS REGINFO option\n"));
18677 cnt = 0;
18678 break;
18679 }
18680
18681 ereg = (Elf32_External_RegInfo *) (eoption + 1);
18682
18683 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
18684 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
18685 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
18686 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
18687 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
18688 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
18689
18690 printf ("GPR %08" PRIx32 " GP 0x%" PRIx32 "\n",
18691 reginfo.ri_gprmask, reginfo.ri_gp_value);
18692 printf (" "
18693 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
18694 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
18695 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
18696 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
18697 }
18698 else
18699 {
18700 /* 64 bit form. */
18701 Elf64_External_RegInfo * ereg;
18702 Elf64_Internal_RegInfo reginfo;
18703
18704 if (option.size < (sizeof (Elf_External_Options)
18705 + sizeof (Elf64_External_RegInfo)))
18706 {
18707 printf (_("<corrupt>\n"));
18708 error (_("Truncated MIPS REGINFO option\n"));
18709 cnt = 0;
18710 break;
18711 }
18712
18713 ereg = (Elf64_External_RegInfo *) (eoption + 1);
18714 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
18715 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
18716 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
18717 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
18718 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
18719 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
18720
18721 printf ("GPR %08" PRIx32 " GP 0x%" PRIx64 "\n",
18722 reginfo.ri_gprmask, reginfo.ri_gp_value);
18723 printf (" "
18724 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
18725 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
18726 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
18727 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
18728 }
18729 offset += option.size;
18730 continue;
18731
18732 case ODK_EXCEPTIONS:
18733 fputs (" EXCEPTIONS fpe_min(", stdout);
18734 process_mips_fpe_exception (option.info & OEX_FPU_MIN);
18735 fputs (") fpe_max(", stdout);
18736 process_mips_fpe_exception ((option.info & OEX_FPU_MAX) >> 8);
18737 fputs (")", stdout);
18738
18739 if (option.info & OEX_PAGE0)
18740 fputs (" PAGE0", stdout);
18741 if (option.info & OEX_SMM)
18742 fputs (" SMM", stdout);
18743 if (option.info & OEX_FPDBUG)
18744 fputs (" FPDBUG", stdout);
18745 if (option.info & OEX_DISMISS)
18746 fputs (" DISMISS", stdout);
18747 break;
18748
18749 case ODK_PAD:
18750 fputs (" PAD ", stdout);
18751 if (option.info & OPAD_PREFIX)
18752 fputs (" PREFIX", stdout);
18753 if (option.info & OPAD_POSTFIX)
18754 fputs (" POSTFIX", stdout);
18755 if (option.info & OPAD_SYMBOL)
18756 fputs (" SYMBOL", stdout);
18757 break;
18758
18759 case ODK_HWPATCH:
18760 fputs (" HWPATCH ", stdout);
18761 if (option.info & OHW_R4KEOP)
18762 fputs (" R4KEOP", stdout);
18763 if (option.info & OHW_R8KPFETCH)
18764 fputs (" R8KPFETCH", stdout);
18765 if (option.info & OHW_R5KEOP)
18766 fputs (" R5KEOP", stdout);
18767 if (option.info & OHW_R5KCVTL)
18768 fputs (" R5KCVTL", stdout);
18769 break;
18770
18771 case ODK_FILL:
18772 fputs (" FILL ", stdout);
18773 /* XXX Print content of info word? */
18774 break;
18775
18776 case ODK_TAGS:
18777 fputs (" TAGS ", stdout);
18778 /* XXX Print content of info word? */
18779 break;
18780
18781 case ODK_HWAND:
18782 fputs (" HWAND ", stdout);
18783 if (option.info & OHWA0_R4KEOP_CHECKED)
18784 fputs (" R4KEOP_CHECKED", stdout);
18785 if (option.info & OHWA0_R4KEOP_CLEAN)
18786 fputs (" R4KEOP_CLEAN", stdout);
18787 break;
18788
18789 case ODK_HWOR:
18790 fputs (" HWOR ", stdout);
18791 if (option.info & OHWA0_R4KEOP_CHECKED)
18792 fputs (" R4KEOP_CHECKED", stdout);
18793 if (option.info & OHWA0_R4KEOP_CLEAN)
18794 fputs (" R4KEOP_CLEAN", stdout);
18795 break;
18796
18797 case ODK_GP_GROUP:
18798 printf (" GP_GROUP %#06x self-contained %#06x",
18799 option.info & OGP_GROUP,
18800 (option.info & OGP_SELF) >> 16);
18801 break;
18802
18803 case ODK_IDENT:
18804 printf (" IDENT %#06x self-contained %#06x",
18805 option.info & OGP_GROUP,
18806 (option.info & OGP_SELF) >> 16);
18807 break;
18808
18809 default:
18810 /* This shouldn't happen. */
18811 printf (" %3d ??? %" PRId16 " %" PRIx32,
18812 option.kind, option.section, option.info);
18813 break;
18814 }
18815
18816 len = sizeof (* eopt);
18817 while (len < option.size)
18818 {
18819 unsigned char datum = *((unsigned char *) eoption + len);
18820
18821 if (ISPRINT (datum))
18822 printf ("%c", datum);
18823 else
18824 printf ("\\%03o", datum);
18825 len ++;
18826 }
18827 fputs ("\n", stdout);
18828
18829 offset += option.size;
18830 }
18831 free (eopt);
18832 }
18833 else
18834 res = false;
18835 }
18836
18837 if (conflicts_offset != 0 && conflictsno != 0)
18838 {
18839 Elf32_Conflict * iconf;
18840 size_t cnt;
18841
18842 if (filedata->dynamic_symbols == NULL)
18843 {
18844 error (_("conflict list found without a dynamic symbol table\n"));
18845 return false;
18846 }
18847
18848 /* PR 21345 - print a slightly more helpful error message
18849 if we are sure that the cmalloc will fail. */
18850 if (conflictsno > filedata->file_size / sizeof (* iconf))
18851 {
18852 error (_("Overlarge number of conflicts detected: %lx\n"),
18853 (long) conflictsno);
18854 return false;
18855 }
18856
18857 iconf = (Elf32_Conflict *) cmalloc (conflictsno, sizeof (* iconf));
18858 if (iconf == NULL)
18859 {
18860 error (_("Out of memory allocating space for dynamic conflicts\n"));
18861 return false;
18862 }
18863
18864 if (is_32bit_elf)
18865 {
18866 Elf32_External_Conflict * econf32;
18867
18868 econf32 = (Elf32_External_Conflict *)
18869 get_data (NULL, filedata, conflicts_offset,
18870 sizeof (*econf32), conflictsno, _("conflict"));
18871 if (!econf32)
18872 {
18873 free (iconf);
18874 return false;
18875 }
18876
18877 for (cnt = 0; cnt < conflictsno; ++cnt)
18878 iconf[cnt] = BYTE_GET (econf32[cnt]);
18879
18880 free (econf32);
18881 }
18882 else
18883 {
18884 Elf64_External_Conflict * econf64;
18885
18886 econf64 = (Elf64_External_Conflict *)
18887 get_data (NULL, filedata, conflicts_offset,
18888 sizeof (*econf64), conflictsno, _("conflict"));
18889 if (!econf64)
18890 {
18891 free (iconf);
18892 return false;
18893 }
18894
18895 for (cnt = 0; cnt < conflictsno; ++cnt)
18896 iconf[cnt] = BYTE_GET (econf64[cnt]);
18897
18898 free (econf64);
18899 }
18900
18901 printf (ngettext ("\nSection '.conflict' contains %lu entry:\n",
18902 "\nSection '.conflict' contains %lu entries:\n",
18903 (unsigned long) conflictsno),
18904 (unsigned long) conflictsno);
18905 puts (_(" Num: Index Value Name"));
18906
18907 for (cnt = 0; cnt < conflictsno; ++cnt)
18908 {
18909 printf ("%5lu: %8lu ", (unsigned long) cnt, iconf[cnt]);
18910
18911 if (iconf[cnt] >= filedata->num_dynamic_syms)
18912 printf (_("<corrupt symbol index>"));
18913 else
18914 {
18915 Elf_Internal_Sym * psym;
18916
18917 psym = & filedata->dynamic_symbols[iconf[cnt]];
18918 print_vma (psym->st_value, FULL_HEX);
18919 putchar (' ');
18920 if (valid_dynamic_name (filedata, psym->st_name))
18921 print_symbol (25, get_dynamic_name (filedata, psym->st_name));
18922 else
18923 printf (_("<corrupt: %14ld>"), psym->st_name);
18924 }
18925 putchar ('\n');
18926 }
18927
18928 free (iconf);
18929 }
18930
18931 if (pltgot != 0 && local_gotno != 0)
18932 {
18933 uint64_t ent, local_end, global_end;
18934 size_t i, offset;
18935 unsigned char * data;
18936 unsigned char * data_end;
18937 int addr_size;
18938
18939 ent = pltgot;
18940 addr_size = (is_32bit_elf ? 4 : 8);
18941 local_end = pltgot + local_gotno * addr_size;
18942
18943 /* PR binutils/17533 file: 012-111227-0.004 */
18944 if (symtabno < gotsym)
18945 {
18946 error (_("The GOT symbol offset (%lu) is greater than the symbol table size (%lu)\n"),
18947 (unsigned long) gotsym, (unsigned long) symtabno);
18948 return false;
18949 }
18950
18951 global_end = local_end + (symtabno - gotsym) * addr_size;
18952 /* PR 17531: file: 54c91a34. */
18953 if (global_end < local_end)
18954 {
18955 error (_("Too many GOT symbols: %lu\n"), (unsigned long) symtabno);
18956 return false;
18957 }
18958
18959 offset = offset_from_vma (filedata, pltgot, global_end - pltgot);
18960 data = (unsigned char *) get_data (NULL, filedata, offset,
18961 global_end - pltgot, 1,
18962 _("Global Offset Table data"));
18963 /* PR 12855: Null data is handled gracefully throughout. */
18964 data_end = data + (global_end - pltgot);
18965
18966 printf (_("\nPrimary GOT:\n"));
18967 printf (_(" Canonical gp value: "));
18968 print_vma (pltgot + 0x7ff0, LONG_HEX);
18969 printf ("\n\n");
18970
18971 printf (_(" Reserved entries:\n"));
18972 printf (_(" %*s %10s %*s Purpose\n"),
18973 addr_size * 2, _("Address"), _("Access"),
18974 addr_size * 2, _("Initial"));
18975 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18976 printf (_(" Lazy resolver\n"));
18977 if (ent == (uint64_t) -1)
18978 goto got_print_fail;
18979
18980 /* Check for the MSB of GOT[1] being set, denoting a GNU object.
18981 This entry will be used by some runtime loaders, to store the
18982 module pointer. Otherwise this is an ordinary local entry.
18983 PR 21344: Check for the entry being fully available before
18984 fetching it. */
18985 if (data
18986 && data + ent - pltgot + addr_size <= data_end
18987 && (byte_get (data + ent - pltgot, addr_size)
18988 >> (addr_size * 8 - 1)) != 0)
18989 {
18990 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18991 printf (_(" Module pointer (GNU extension)\n"));
18992 if (ent == (uint64_t) -1)
18993 goto got_print_fail;
18994 }
18995 printf ("\n");
18996
18997 if (data != NULL && ent < local_end)
18998 {
18999 printf (_(" Local entries:\n"));
19000 printf (" %*s %10s %*s\n",
19001 addr_size * 2, _("Address"), _("Access"),
19002 addr_size * 2, _("Initial"));
19003 while (ent < local_end)
19004 {
19005 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19006 printf ("\n");
19007 if (ent == (uint64_t) -1)
19008 goto got_print_fail;
19009 }
19010 printf ("\n");
19011 }
19012
19013 if (data != NULL && gotsym < symtabno)
19014 {
19015 int sym_width;
19016
19017 printf (_(" Global entries:\n"));
19018 printf (" %*s %10s %*s %*s %-7s %3s %s\n",
19019 addr_size * 2, _("Address"),
19020 _("Access"),
19021 addr_size * 2, _("Initial"),
19022 addr_size * 2, _("Sym.Val."),
19023 _("Type"),
19024 /* Note for translators: "Ndx" = abbreviated form of "Index". */
19025 _("Ndx"), _("Name"));
19026
19027 sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
19028
19029 for (i = gotsym; i < symtabno; i++)
19030 {
19031 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19032 printf (" ");
19033
19034 if (filedata->dynamic_symbols == NULL)
19035 printf (_("<no dynamic symbols>"));
19036 else if (i < filedata->num_dynamic_syms)
19037 {
19038 Elf_Internal_Sym * psym = filedata->dynamic_symbols + i;
19039
19040 print_vma (psym->st_value, LONG_HEX);
19041 printf (" %-7s %3s ",
19042 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
19043 get_symbol_index_type (filedata, psym->st_shndx));
19044
19045 if (valid_dynamic_name (filedata, psym->st_name))
19046 print_symbol (sym_width,
19047 get_dynamic_name (filedata, psym->st_name));
19048 else
19049 printf (_("<corrupt: %14ld>"), psym->st_name);
19050 }
19051 else
19052 printf (_("<symbol index %lu exceeds number of dynamic symbols>"),
19053 (unsigned long) i);
19054
19055 printf ("\n");
19056 if (ent == (uint64_t) -1)
19057 break;
19058 }
19059 printf ("\n");
19060 }
19061
19062 got_print_fail:
19063 free (data);
19064 }
19065
19066 if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
19067 {
19068 uint64_t ent, end;
19069 size_t offset, rel_offset;
19070 unsigned long count, i;
19071 unsigned char * data;
19072 int addr_size, sym_width;
19073 Elf_Internal_Rela * rels;
19074
19075 rel_offset = offset_from_vma (filedata, jmprel, pltrelsz);
19076 if (pltrel == DT_RELA)
19077 {
19078 if (!slurp_rela_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
19079 return false;
19080 }
19081 else
19082 {
19083 if (!slurp_rel_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
19084 return false;
19085 }
19086
19087 ent = mips_pltgot;
19088 addr_size = (is_32bit_elf ? 4 : 8);
19089 end = mips_pltgot + (2 + count) * addr_size;
19090
19091 offset = offset_from_vma (filedata, mips_pltgot, end - mips_pltgot);
19092 data = (unsigned char *) get_data (NULL, filedata, offset, end - mips_pltgot,
19093 1, _("Procedure Linkage Table data"));
19094 if (data == NULL)
19095 {
19096 free (rels);
19097 return false;
19098 }
19099
19100 printf ("\nPLT GOT:\n\n");
19101 printf (_(" Reserved entries:\n"));
19102 printf (_(" %*s %*s Purpose\n"),
19103 addr_size * 2, _("Address"), addr_size * 2, _("Initial"));
19104 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
19105 printf (_(" PLT lazy resolver\n"));
19106 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
19107 printf (_(" Module pointer\n"));
19108 printf ("\n");
19109
19110 printf (_(" Entries:\n"));
19111 printf (" %*s %*s %*s %-7s %3s %s\n",
19112 addr_size * 2, _("Address"),
19113 addr_size * 2, _("Initial"),
19114 addr_size * 2, _("Sym.Val."), _("Type"), _("Ndx"), _("Name"));
19115 sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
19116 for (i = 0; i < count; i++)
19117 {
19118 unsigned long idx = get_reloc_symindex (rels[i].r_info);
19119
19120 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
19121 printf (" ");
19122
19123 if (idx >= filedata->num_dynamic_syms)
19124 printf (_("<corrupt symbol index: %lu>"), idx);
19125 else
19126 {
19127 Elf_Internal_Sym * psym = filedata->dynamic_symbols + idx;
19128
19129 print_vma (psym->st_value, LONG_HEX);
19130 printf (" %-7s %3s ",
19131 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
19132 get_symbol_index_type (filedata, psym->st_shndx));
19133 if (valid_dynamic_name (filedata, psym->st_name))
19134 print_symbol (sym_width,
19135 get_dynamic_name (filedata, psym->st_name));
19136 else
19137 printf (_("<corrupt: %14ld>"), psym->st_name);
19138 }
19139 printf ("\n");
19140 }
19141 printf ("\n");
19142
19143 free (data);
19144 free (rels);
19145 }
19146
19147 return res;
19148 }
19149
19150 static bool
19151 process_nds32_specific (Filedata * filedata)
19152 {
19153 Elf_Internal_Shdr *sect = NULL;
19154
19155 sect = find_section (filedata, ".nds32_e_flags");
19156 if (sect != NULL && sect->sh_size >= 4)
19157 {
19158 unsigned char *buf;
19159 unsigned int flag;
19160
19161 printf ("\nNDS32 elf flags section:\n");
19162 buf = get_data (NULL, filedata, sect->sh_offset, 1, 4,
19163 _("NDS32 elf flags section"));
19164
19165 if (buf == NULL)
19166 return false;
19167
19168 flag = byte_get (buf, 4);
19169 free (buf);
19170 switch (flag & 0x3)
19171 {
19172 case 0:
19173 printf ("(VEC_SIZE):\tNo entry.\n");
19174 break;
19175 case 1:
19176 printf ("(VEC_SIZE):\t4 bytes\n");
19177 break;
19178 case 2:
19179 printf ("(VEC_SIZE):\t16 bytes\n");
19180 break;
19181 case 3:
19182 printf ("(VEC_SIZE):\treserved\n");
19183 break;
19184 }
19185 }
19186
19187 return true;
19188 }
19189
19190 static bool
19191 process_gnu_liblist (Filedata * filedata)
19192 {
19193 Elf_Internal_Shdr * section;
19194 Elf_Internal_Shdr * string_sec;
19195 Elf32_External_Lib * elib;
19196 char * strtab;
19197 size_t strtab_size;
19198 size_t cnt;
19199 unsigned long num_liblist;
19200 unsigned i;
19201 bool res = true;
19202
19203 if (! do_arch)
19204 return true;
19205
19206 for (i = 0, section = filedata->section_headers;
19207 i < filedata->file_header.e_shnum;
19208 i++, section++)
19209 {
19210 switch (section->sh_type)
19211 {
19212 case SHT_GNU_LIBLIST:
19213 if (section->sh_link >= filedata->file_header.e_shnum)
19214 break;
19215
19216 elib = (Elf32_External_Lib *)
19217 get_data (NULL, filedata, section->sh_offset, 1, section->sh_size,
19218 _("liblist section data"));
19219
19220 if (elib == NULL)
19221 {
19222 res = false;
19223 break;
19224 }
19225
19226 string_sec = filedata->section_headers + section->sh_link;
19227 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
19228 string_sec->sh_size,
19229 _("liblist string table"));
19230 if (strtab == NULL
19231 || section->sh_entsize != sizeof (Elf32_External_Lib))
19232 {
19233 free (elib);
19234 free (strtab);
19235 res = false;
19236 break;
19237 }
19238 strtab_size = string_sec->sh_size;
19239
19240 num_liblist = section->sh_size / sizeof (Elf32_External_Lib);
19241 printf (ngettext ("\nLibrary list section '%s' contains %lu entries:\n",
19242 "\nLibrary list section '%s' contains %lu entries:\n",
19243 num_liblist),
19244 printable_section_name (filedata, section),
19245 num_liblist);
19246
19247 puts (_(" Library Time Stamp Checksum Version Flags"));
19248
19249 for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
19250 ++cnt)
19251 {
19252 Elf32_Lib liblist;
19253 time_t atime;
19254 char timebuf[128];
19255 struct tm * tmp;
19256
19257 liblist.l_name = BYTE_GET (elib[cnt].l_name);
19258 atime = BYTE_GET (elib[cnt].l_time_stamp);
19259 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
19260 liblist.l_version = BYTE_GET (elib[cnt].l_version);
19261 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
19262
19263 tmp = gmtime (&atime);
19264 snprintf (timebuf, sizeof (timebuf),
19265 "%04u-%02u-%02uT%02u:%02u:%02u",
19266 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
19267 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
19268
19269 printf ("%3lu: ", (unsigned long) cnt);
19270 if (do_wide)
19271 printf ("%-20s", liblist.l_name < strtab_size
19272 ? strtab + liblist.l_name : _("<corrupt>"));
19273 else
19274 printf ("%-20.20s", liblist.l_name < strtab_size
19275 ? strtab + liblist.l_name : _("<corrupt>"));
19276 printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
19277 liblist.l_version, liblist.l_flags);
19278 }
19279
19280 free (elib);
19281 free (strtab);
19282 }
19283 }
19284
19285 return res;
19286 }
19287
19288 static const char *
19289 get_note_type (Filedata * filedata, unsigned e_type)
19290 {
19291 static char buff[64];
19292
19293 if (filedata->file_header.e_type == ET_CORE)
19294 switch (e_type)
19295 {
19296 case NT_AUXV:
19297 return _("NT_AUXV (auxiliary vector)");
19298 case NT_PRSTATUS:
19299 return _("NT_PRSTATUS (prstatus structure)");
19300 case NT_FPREGSET:
19301 return _("NT_FPREGSET (floating point registers)");
19302 case NT_PRPSINFO:
19303 return _("NT_PRPSINFO (prpsinfo structure)");
19304 case NT_TASKSTRUCT:
19305 return _("NT_TASKSTRUCT (task structure)");
19306 case NT_GDB_TDESC:
19307 return _("NT_GDB_TDESC (GDB XML target description)");
19308 case NT_PRXFPREG:
19309 return _("NT_PRXFPREG (user_xfpregs structure)");
19310 case NT_PPC_VMX:
19311 return _("NT_PPC_VMX (ppc Altivec registers)");
19312 case NT_PPC_VSX:
19313 return _("NT_PPC_VSX (ppc VSX registers)");
19314 case NT_PPC_TAR:
19315 return _("NT_PPC_TAR (ppc TAR register)");
19316 case NT_PPC_PPR:
19317 return _("NT_PPC_PPR (ppc PPR register)");
19318 case NT_PPC_DSCR:
19319 return _("NT_PPC_DSCR (ppc DSCR register)");
19320 case NT_PPC_EBB:
19321 return _("NT_PPC_EBB (ppc EBB registers)");
19322 case NT_PPC_PMU:
19323 return _("NT_PPC_PMU (ppc PMU registers)");
19324 case NT_PPC_TM_CGPR:
19325 return _("NT_PPC_TM_CGPR (ppc checkpointed GPR registers)");
19326 case NT_PPC_TM_CFPR:
19327 return _("NT_PPC_TM_CFPR (ppc checkpointed floating point registers)");
19328 case NT_PPC_TM_CVMX:
19329 return _("NT_PPC_TM_CVMX (ppc checkpointed Altivec registers)");
19330 case NT_PPC_TM_CVSX:
19331 return _("NT_PPC_TM_CVSX (ppc checkpointed VSX registers)");
19332 case NT_PPC_TM_SPR:
19333 return _("NT_PPC_TM_SPR (ppc TM special purpose registers)");
19334 case NT_PPC_TM_CTAR:
19335 return _("NT_PPC_TM_CTAR (ppc checkpointed TAR register)");
19336 case NT_PPC_TM_CPPR:
19337 return _("NT_PPC_TM_CPPR (ppc checkpointed PPR register)");
19338 case NT_PPC_TM_CDSCR:
19339 return _("NT_PPC_TM_CDSCR (ppc checkpointed DSCR register)");
19340 case NT_386_TLS:
19341 return _("NT_386_TLS (x86 TLS information)");
19342 case NT_386_IOPERM:
19343 return _("NT_386_IOPERM (x86 I/O permissions)");
19344 case NT_X86_XSTATE:
19345 return _("NT_X86_XSTATE (x86 XSAVE extended state)");
19346 case NT_X86_CET:
19347 return _("NT_X86_CET (x86 CET state)");
19348 case NT_S390_HIGH_GPRS:
19349 return _("NT_S390_HIGH_GPRS (s390 upper register halves)");
19350 case NT_S390_TIMER:
19351 return _("NT_S390_TIMER (s390 timer register)");
19352 case NT_S390_TODCMP:
19353 return _("NT_S390_TODCMP (s390 TOD comparator register)");
19354 case NT_S390_TODPREG:
19355 return _("NT_S390_TODPREG (s390 TOD programmable register)");
19356 case NT_S390_CTRS:
19357 return _("NT_S390_CTRS (s390 control registers)");
19358 case NT_S390_PREFIX:
19359 return _("NT_S390_PREFIX (s390 prefix register)");
19360 case NT_S390_LAST_BREAK:
19361 return _("NT_S390_LAST_BREAK (s390 last breaking event address)");
19362 case NT_S390_SYSTEM_CALL:
19363 return _("NT_S390_SYSTEM_CALL (s390 system call restart data)");
19364 case NT_S390_TDB:
19365 return _("NT_S390_TDB (s390 transaction diagnostic block)");
19366 case NT_S390_VXRS_LOW:
19367 return _("NT_S390_VXRS_LOW (s390 vector registers 0-15 upper half)");
19368 case NT_S390_VXRS_HIGH:
19369 return _("NT_S390_VXRS_HIGH (s390 vector registers 16-31)");
19370 case NT_S390_GS_CB:
19371 return _("NT_S390_GS_CB (s390 guarded-storage registers)");
19372 case NT_S390_GS_BC:
19373 return _("NT_S390_GS_BC (s390 guarded-storage broadcast control)");
19374 case NT_ARM_VFP:
19375 return _("NT_ARM_VFP (arm VFP registers)");
19376 case NT_ARM_TLS:
19377 return _("NT_ARM_TLS (AArch TLS registers)");
19378 case NT_ARM_HW_BREAK:
19379 return _("NT_ARM_HW_BREAK (AArch hardware breakpoint registers)");
19380 case NT_ARM_HW_WATCH:
19381 return _("NT_ARM_HW_WATCH (AArch hardware watchpoint registers)");
19382 case NT_ARM_SYSTEM_CALL:
19383 return _("NT_ARM_SYSTEM_CALL (AArch system call number)");
19384 case NT_ARM_SVE:
19385 return _("NT_ARM_SVE (AArch SVE registers)");
19386 case NT_ARM_PAC_MASK:
19387 return _("NT_ARM_PAC_MASK (AArch pointer authentication code masks)");
19388 case NT_ARM_PACA_KEYS:
19389 return _("NT_ARM_PACA_KEYS (ARM pointer authentication address keys)");
19390 case NT_ARM_PACG_KEYS:
19391 return _("NT_ARM_PACG_KEYS (ARM pointer authentication generic keys)");
19392 case NT_ARM_TAGGED_ADDR_CTRL:
19393 return _("NT_ARM_TAGGED_ADDR_CTRL (AArch tagged address control)");
19394 case NT_ARM_PAC_ENABLED_KEYS:
19395 return _("NT_ARM_PAC_ENABLED_KEYS (AArch64 pointer authentication enabled keys)");
19396 case NT_ARC_V2:
19397 return _("NT_ARC_V2 (ARC HS accumulator/extra registers)");
19398 case NT_RISCV_CSR:
19399 return _("NT_RISCV_CSR (RISC-V control and status registers)");
19400 case NT_PSTATUS:
19401 return _("NT_PSTATUS (pstatus structure)");
19402 case NT_FPREGS:
19403 return _("NT_FPREGS (floating point registers)");
19404 case NT_PSINFO:
19405 return _("NT_PSINFO (psinfo structure)");
19406 case NT_LWPSTATUS:
19407 return _("NT_LWPSTATUS (lwpstatus_t structure)");
19408 case NT_LWPSINFO:
19409 return _("NT_LWPSINFO (lwpsinfo_t structure)");
19410 case NT_WIN32PSTATUS:
19411 return _("NT_WIN32PSTATUS (win32_pstatus structure)");
19412 case NT_SIGINFO:
19413 return _("NT_SIGINFO (siginfo_t data)");
19414 case NT_FILE:
19415 return _("NT_FILE (mapped files)");
19416 default:
19417 break;
19418 }
19419 else
19420 switch (e_type)
19421 {
19422 case NT_VERSION:
19423 return _("NT_VERSION (version)");
19424 case NT_ARCH:
19425 return _("NT_ARCH (architecture)");
19426 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
19427 return _("OPEN");
19428 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
19429 return _("func");
19430 case NT_GO_BUILDID:
19431 return _("GO BUILDID");
19432 case FDO_PACKAGING_METADATA:
19433 return _("FDO_PACKAGING_METADATA");
19434 default:
19435 break;
19436 }
19437
19438 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19439 return buff;
19440 }
19441
19442 static bool
19443 print_core_note (Elf_Internal_Note *pnote)
19444 {
19445 unsigned int addr_size = is_32bit_elf ? 4 : 8;
19446 uint64_t count, page_size;
19447 unsigned char *descdata, *filenames, *descend;
19448
19449 if (pnote->type != NT_FILE)
19450 {
19451 if (do_wide)
19452 printf ("\n");
19453 return true;
19454 }
19455
19456 if (!is_32bit_elf)
19457 {
19458 printf (_(" Cannot decode 64-bit note in 32-bit build\n"));
19459 /* Still "successful". */
19460 return true;
19461 }
19462
19463 if (pnote->descsz < 2 * addr_size)
19464 {
19465 error (_(" Malformed note - too short for header\n"));
19466 return false;
19467 }
19468
19469 descdata = (unsigned char *) pnote->descdata;
19470 descend = descdata + pnote->descsz;
19471
19472 if (descdata[pnote->descsz - 1] != '\0')
19473 {
19474 error (_(" Malformed note - does not end with \\0\n"));
19475 return false;
19476 }
19477
19478 count = byte_get (descdata, addr_size);
19479 descdata += addr_size;
19480
19481 page_size = byte_get (descdata, addr_size);
19482 descdata += addr_size;
19483
19484 if (count > ((uint64_t) -1 - 2 * addr_size) / (3 * addr_size)
19485 || pnote->descsz < 2 * addr_size + count * 3 * addr_size)
19486 {
19487 error (_(" Malformed note - too short for supplied file count\n"));
19488 return false;
19489 }
19490
19491 printf (_(" Page size: "));
19492 print_vma (page_size, DEC);
19493 printf ("\n");
19494
19495 printf (_(" %*s%*s%*s\n"),
19496 (int) (2 + 2 * addr_size), _("Start"),
19497 (int) (4 + 2 * addr_size), _("End"),
19498 (int) (4 + 2 * addr_size), _("Page Offset"));
19499 filenames = descdata + count * 3 * addr_size;
19500 while (count-- > 0)
19501 {
19502 uint64_t start, end, file_ofs;
19503
19504 if (filenames == descend)
19505 {
19506 error (_(" Malformed note - filenames end too early\n"));
19507 return false;
19508 }
19509
19510 start = byte_get (descdata, addr_size);
19511 descdata += addr_size;
19512 end = byte_get (descdata, addr_size);
19513 descdata += addr_size;
19514 file_ofs = byte_get (descdata, addr_size);
19515 descdata += addr_size;
19516
19517 printf (" ");
19518 print_vma (start, FULL_HEX);
19519 printf (" ");
19520 print_vma (end, FULL_HEX);
19521 printf (" ");
19522 print_vma (file_ofs, FULL_HEX);
19523 printf ("\n %s\n", filenames);
19524
19525 filenames += 1 + strlen ((char *) filenames);
19526 }
19527
19528 return true;
19529 }
19530
19531 static const char *
19532 get_gnu_elf_note_type (unsigned e_type)
19533 {
19534 /* NB/ Keep this switch statement in sync with print_gnu_note (). */
19535 switch (e_type)
19536 {
19537 case NT_GNU_ABI_TAG:
19538 return _("NT_GNU_ABI_TAG (ABI version tag)");
19539 case NT_GNU_HWCAP:
19540 return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
19541 case NT_GNU_BUILD_ID:
19542 return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
19543 case NT_GNU_GOLD_VERSION:
19544 return _("NT_GNU_GOLD_VERSION (gold version)");
19545 case NT_GNU_PROPERTY_TYPE_0:
19546 return _("NT_GNU_PROPERTY_TYPE_0");
19547 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
19548 return _("NT_GNU_BUILD_ATTRIBUTE_OPEN");
19549 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
19550 return _("NT_GNU_BUILD_ATTRIBUTE_FUNC");
19551 default:
19552 {
19553 static char buff[64];
19554
19555 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19556 return buff;
19557 }
19558 }
19559 }
19560
19561 static void
19562 decode_x86_compat_isa (unsigned int bitmask)
19563 {
19564 while (bitmask)
19565 {
19566 unsigned int bit = bitmask & (- bitmask);
19567
19568 bitmask &= ~ bit;
19569 switch (bit)
19570 {
19571 case GNU_PROPERTY_X86_COMPAT_ISA_1_486:
19572 printf ("i486");
19573 break;
19574 case GNU_PROPERTY_X86_COMPAT_ISA_1_586:
19575 printf ("586");
19576 break;
19577 case GNU_PROPERTY_X86_COMPAT_ISA_1_686:
19578 printf ("686");
19579 break;
19580 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE:
19581 printf ("SSE");
19582 break;
19583 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE2:
19584 printf ("SSE2");
19585 break;
19586 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE3:
19587 printf ("SSE3");
19588 break;
19589 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSSE3:
19590 printf ("SSSE3");
19591 break;
19592 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_1:
19593 printf ("SSE4_1");
19594 break;
19595 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_2:
19596 printf ("SSE4_2");
19597 break;
19598 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX:
19599 printf ("AVX");
19600 break;
19601 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX2:
19602 printf ("AVX2");
19603 break;
19604 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512F:
19605 printf ("AVX512F");
19606 break;
19607 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512CD:
19608 printf ("AVX512CD");
19609 break;
19610 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512ER:
19611 printf ("AVX512ER");
19612 break;
19613 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512PF:
19614 printf ("AVX512PF");
19615 break;
19616 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512VL:
19617 printf ("AVX512VL");
19618 break;
19619 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512DQ:
19620 printf ("AVX512DQ");
19621 break;
19622 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512BW:
19623 printf ("AVX512BW");
19624 break;
19625 default:
19626 printf (_("<unknown: %x>"), bit);
19627 break;
19628 }
19629 if (bitmask)
19630 printf (", ");
19631 }
19632 }
19633
19634 static void
19635 decode_x86_compat_2_isa (unsigned int bitmask)
19636 {
19637 if (!bitmask)
19638 {
19639 printf (_("<None>"));
19640 return;
19641 }
19642
19643 while (bitmask)
19644 {
19645 unsigned int bit = bitmask & (- bitmask);
19646
19647 bitmask &= ~ bit;
19648 switch (bit)
19649 {
19650 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_CMOV:
19651 printf ("CMOV");
19652 break;
19653 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE:
19654 printf ("SSE");
19655 break;
19656 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE2:
19657 printf ("SSE2");
19658 break;
19659 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE3:
19660 printf ("SSE3");
19661 break;
19662 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSSE3:
19663 printf ("SSSE3");
19664 break;
19665 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_1:
19666 printf ("SSE4_1");
19667 break;
19668 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_2:
19669 printf ("SSE4_2");
19670 break;
19671 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX:
19672 printf ("AVX");
19673 break;
19674 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX2:
19675 printf ("AVX2");
19676 break;
19677 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_FMA:
19678 printf ("FMA");
19679 break;
19680 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512F:
19681 printf ("AVX512F");
19682 break;
19683 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512CD:
19684 printf ("AVX512CD");
19685 break;
19686 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512ER:
19687 printf ("AVX512ER");
19688 break;
19689 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512PF:
19690 printf ("AVX512PF");
19691 break;
19692 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512VL:
19693 printf ("AVX512VL");
19694 break;
19695 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512DQ:
19696 printf ("AVX512DQ");
19697 break;
19698 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512BW:
19699 printf ("AVX512BW");
19700 break;
19701 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4FMAPS:
19702 printf ("AVX512_4FMAPS");
19703 break;
19704 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4VNNIW:
19705 printf ("AVX512_4VNNIW");
19706 break;
19707 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BITALG:
19708 printf ("AVX512_BITALG");
19709 break;
19710 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_IFMA:
19711 printf ("AVX512_IFMA");
19712 break;
19713 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI:
19714 printf ("AVX512_VBMI");
19715 break;
19716 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI2:
19717 printf ("AVX512_VBMI2");
19718 break;
19719 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VNNI:
19720 printf ("AVX512_VNNI");
19721 break;
19722 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BF16:
19723 printf ("AVX512_BF16");
19724 break;
19725 default:
19726 printf (_("<unknown: %x>"), bit);
19727 break;
19728 }
19729 if (bitmask)
19730 printf (", ");
19731 }
19732 }
19733
19734 static const char *
19735 get_amdgpu_elf_note_type (unsigned int e_type)
19736 {
19737 switch (e_type)
19738 {
19739 case NT_AMDGPU_METADATA:
19740 return _("NT_AMDGPU_METADATA (code object metadata)");
19741 default:
19742 {
19743 static char buf[64];
19744 snprintf (buf, sizeof (buf), _("Unknown note type: (0x%08x)"), e_type);
19745 return buf;
19746 }
19747 }
19748 }
19749
19750 static void
19751 decode_x86_isa (unsigned int bitmask)
19752 {
19753 while (bitmask)
19754 {
19755 unsigned int bit = bitmask & (- bitmask);
19756
19757 bitmask &= ~ bit;
19758 switch (bit)
19759 {
19760 case GNU_PROPERTY_X86_ISA_1_BASELINE:
19761 printf ("x86-64-baseline");
19762 break;
19763 case GNU_PROPERTY_X86_ISA_1_V2:
19764 printf ("x86-64-v2");
19765 break;
19766 case GNU_PROPERTY_X86_ISA_1_V3:
19767 printf ("x86-64-v3");
19768 break;
19769 case GNU_PROPERTY_X86_ISA_1_V4:
19770 printf ("x86-64-v4");
19771 break;
19772 default:
19773 printf (_("<unknown: %x>"), bit);
19774 break;
19775 }
19776 if (bitmask)
19777 printf (", ");
19778 }
19779 }
19780
19781 static void
19782 decode_x86_feature_1 (unsigned int bitmask)
19783 {
19784 if (!bitmask)
19785 {
19786 printf (_("<None>"));
19787 return;
19788 }
19789
19790 while (bitmask)
19791 {
19792 unsigned int bit = bitmask & (- bitmask);
19793
19794 bitmask &= ~ bit;
19795 switch (bit)
19796 {
19797 case GNU_PROPERTY_X86_FEATURE_1_IBT:
19798 printf ("IBT");
19799 break;
19800 case GNU_PROPERTY_X86_FEATURE_1_SHSTK:
19801 printf ("SHSTK");
19802 break;
19803 case GNU_PROPERTY_X86_FEATURE_1_LAM_U48:
19804 printf ("LAM_U48");
19805 break;
19806 case GNU_PROPERTY_X86_FEATURE_1_LAM_U57:
19807 printf ("LAM_U57");
19808 break;
19809 default:
19810 printf (_("<unknown: %x>"), bit);
19811 break;
19812 }
19813 if (bitmask)
19814 printf (", ");
19815 }
19816 }
19817
19818 static void
19819 decode_x86_feature_2 (unsigned int bitmask)
19820 {
19821 if (!bitmask)
19822 {
19823 printf (_("<None>"));
19824 return;
19825 }
19826
19827 while (bitmask)
19828 {
19829 unsigned int bit = bitmask & (- bitmask);
19830
19831 bitmask &= ~ bit;
19832 switch (bit)
19833 {
19834 case GNU_PROPERTY_X86_FEATURE_2_X86:
19835 printf ("x86");
19836 break;
19837 case GNU_PROPERTY_X86_FEATURE_2_X87:
19838 printf ("x87");
19839 break;
19840 case GNU_PROPERTY_X86_FEATURE_2_MMX:
19841 printf ("MMX");
19842 break;
19843 case GNU_PROPERTY_X86_FEATURE_2_XMM:
19844 printf ("XMM");
19845 break;
19846 case GNU_PROPERTY_X86_FEATURE_2_YMM:
19847 printf ("YMM");
19848 break;
19849 case GNU_PROPERTY_X86_FEATURE_2_ZMM:
19850 printf ("ZMM");
19851 break;
19852 case GNU_PROPERTY_X86_FEATURE_2_TMM:
19853 printf ("TMM");
19854 break;
19855 case GNU_PROPERTY_X86_FEATURE_2_MASK:
19856 printf ("MASK");
19857 break;
19858 case GNU_PROPERTY_X86_FEATURE_2_FXSR:
19859 printf ("FXSR");
19860 break;
19861 case GNU_PROPERTY_X86_FEATURE_2_XSAVE:
19862 printf ("XSAVE");
19863 break;
19864 case GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT:
19865 printf ("XSAVEOPT");
19866 break;
19867 case GNU_PROPERTY_X86_FEATURE_2_XSAVEC:
19868 printf ("XSAVEC");
19869 break;
19870 default:
19871 printf (_("<unknown: %x>"), bit);
19872 break;
19873 }
19874 if (bitmask)
19875 printf (", ");
19876 }
19877 }
19878
19879 static void
19880 decode_aarch64_feature_1_and (unsigned int bitmask)
19881 {
19882 while (bitmask)
19883 {
19884 unsigned int bit = bitmask & (- bitmask);
19885
19886 bitmask &= ~ bit;
19887 switch (bit)
19888 {
19889 case GNU_PROPERTY_AARCH64_FEATURE_1_BTI:
19890 printf ("BTI");
19891 break;
19892
19893 case GNU_PROPERTY_AARCH64_FEATURE_1_PAC:
19894 printf ("PAC");
19895 break;
19896
19897 default:
19898 printf (_("<unknown: %x>"), bit);
19899 break;
19900 }
19901 if (bitmask)
19902 printf (", ");
19903 }
19904 }
19905
19906 static void
19907 decode_1_needed (unsigned int bitmask)
19908 {
19909 while (bitmask)
19910 {
19911 unsigned int bit = bitmask & (- bitmask);
19912
19913 bitmask &= ~ bit;
19914 switch (bit)
19915 {
19916 case GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS:
19917 printf ("indirect external access");
19918 break;
19919 default:
19920 printf (_("<unknown: %x>"), bit);
19921 break;
19922 }
19923 if (bitmask)
19924 printf (", ");
19925 }
19926 }
19927
19928 static void
19929 print_gnu_property_note (Filedata * filedata, Elf_Internal_Note * pnote)
19930 {
19931 unsigned char * ptr = (unsigned char *) pnote->descdata;
19932 unsigned char * ptr_end = ptr + pnote->descsz;
19933 unsigned int size = is_32bit_elf ? 4 : 8;
19934
19935 printf (_(" Properties: "));
19936
19937 if (pnote->descsz < 8 || (pnote->descsz % size) != 0)
19938 {
19939 printf (_("<corrupt GNU_PROPERTY_TYPE, size = %#lx>\n"), pnote->descsz);
19940 return;
19941 }
19942
19943 while (ptr < ptr_end)
19944 {
19945 unsigned int j;
19946 unsigned int type;
19947 unsigned int datasz;
19948
19949 if ((size_t) (ptr_end - ptr) < 8)
19950 {
19951 printf (_("<corrupt descsz: %#lx>\n"), pnote->descsz);
19952 break;
19953 }
19954
19955 type = byte_get (ptr, 4);
19956 datasz = byte_get (ptr + 4, 4);
19957
19958 ptr += 8;
19959
19960 if (datasz > (size_t) (ptr_end - ptr))
19961 {
19962 printf (_("<corrupt type (%#x) datasz: %#x>\n"),
19963 type, datasz);
19964 break;
19965 }
19966
19967 if (type >= GNU_PROPERTY_LOPROC && type <= GNU_PROPERTY_HIPROC)
19968 {
19969 if (filedata->file_header.e_machine == EM_X86_64
19970 || filedata->file_header.e_machine == EM_IAMCU
19971 || filedata->file_header.e_machine == EM_386)
19972 {
19973 unsigned int bitmask;
19974
19975 if (datasz == 4)
19976 bitmask = byte_get (ptr, 4);
19977 else
19978 bitmask = 0;
19979
19980 switch (type)
19981 {
19982 case GNU_PROPERTY_X86_ISA_1_USED:
19983 if (datasz != 4)
19984 printf (_("x86 ISA used: <corrupt length: %#x> "),
19985 datasz);
19986 else
19987 {
19988 printf ("x86 ISA used: ");
19989 decode_x86_isa (bitmask);
19990 }
19991 goto next;
19992
19993 case GNU_PROPERTY_X86_ISA_1_NEEDED:
19994 if (datasz != 4)
19995 printf (_("x86 ISA needed: <corrupt length: %#x> "),
19996 datasz);
19997 else
19998 {
19999 printf ("x86 ISA needed: ");
20000 decode_x86_isa (bitmask);
20001 }
20002 goto next;
20003
20004 case GNU_PROPERTY_X86_FEATURE_1_AND:
20005 if (datasz != 4)
20006 printf (_("x86 feature: <corrupt length: %#x> "),
20007 datasz);
20008 else
20009 {
20010 printf ("x86 feature: ");
20011 decode_x86_feature_1 (bitmask);
20012 }
20013 goto next;
20014
20015 case GNU_PROPERTY_X86_FEATURE_2_USED:
20016 if (datasz != 4)
20017 printf (_("x86 feature used: <corrupt length: %#x> "),
20018 datasz);
20019 else
20020 {
20021 printf ("x86 feature used: ");
20022 decode_x86_feature_2 (bitmask);
20023 }
20024 goto next;
20025
20026 case GNU_PROPERTY_X86_FEATURE_2_NEEDED:
20027 if (datasz != 4)
20028 printf (_("x86 feature needed: <corrupt length: %#x> "), datasz);
20029 else
20030 {
20031 printf ("x86 feature needed: ");
20032 decode_x86_feature_2 (bitmask);
20033 }
20034 goto next;
20035
20036 case GNU_PROPERTY_X86_COMPAT_ISA_1_USED:
20037 if (datasz != 4)
20038 printf (_("x86 ISA used: <corrupt length: %#x> "),
20039 datasz);
20040 else
20041 {
20042 printf ("x86 ISA used: ");
20043 decode_x86_compat_isa (bitmask);
20044 }
20045 goto next;
20046
20047 case GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED:
20048 if (datasz != 4)
20049 printf (_("x86 ISA needed: <corrupt length: %#x> "),
20050 datasz);
20051 else
20052 {
20053 printf ("x86 ISA needed: ");
20054 decode_x86_compat_isa (bitmask);
20055 }
20056 goto next;
20057
20058 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_USED:
20059 if (datasz != 4)
20060 printf (_("x86 ISA used: <corrupt length: %#x> "),
20061 datasz);
20062 else
20063 {
20064 printf ("x86 ISA used: ");
20065 decode_x86_compat_2_isa (bitmask);
20066 }
20067 goto next;
20068
20069 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_NEEDED:
20070 if (datasz != 4)
20071 printf (_("x86 ISA needed: <corrupt length: %#x> "),
20072 datasz);
20073 else
20074 {
20075 printf ("x86 ISA needed: ");
20076 decode_x86_compat_2_isa (bitmask);
20077 }
20078 goto next;
20079
20080 default:
20081 break;
20082 }
20083 }
20084 else if (filedata->file_header.e_machine == EM_AARCH64)
20085 {
20086 if (type == GNU_PROPERTY_AARCH64_FEATURE_1_AND)
20087 {
20088 printf ("AArch64 feature: ");
20089 if (datasz != 4)
20090 printf (_("<corrupt length: %#x> "), datasz);
20091 else
20092 decode_aarch64_feature_1_and (byte_get (ptr, 4));
20093 goto next;
20094 }
20095 }
20096 }
20097 else
20098 {
20099 switch (type)
20100 {
20101 case GNU_PROPERTY_STACK_SIZE:
20102 printf (_("stack size: "));
20103 if (datasz != size)
20104 printf (_("<corrupt length: %#x> "), datasz);
20105 else
20106 printf ("%#lx", (unsigned long) byte_get (ptr, size));
20107 goto next;
20108
20109 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
20110 printf ("no copy on protected ");
20111 if (datasz)
20112 printf (_("<corrupt length: %#x> "), datasz);
20113 goto next;
20114
20115 default:
20116 if ((type >= GNU_PROPERTY_UINT32_AND_LO
20117 && type <= GNU_PROPERTY_UINT32_AND_HI)
20118 || (type >= GNU_PROPERTY_UINT32_OR_LO
20119 && type <= GNU_PROPERTY_UINT32_OR_HI))
20120 {
20121 switch (type)
20122 {
20123 case GNU_PROPERTY_1_NEEDED:
20124 if (datasz != 4)
20125 printf (_("1_needed: <corrupt length: %#x> "),
20126 datasz);
20127 else
20128 {
20129 unsigned int bitmask = byte_get (ptr, 4);
20130 printf ("1_needed: ");
20131 decode_1_needed (bitmask);
20132 }
20133 goto next;
20134
20135 default:
20136 break;
20137 }
20138 if (type <= GNU_PROPERTY_UINT32_AND_HI)
20139 printf (_("UINT32_AND (%#x): "), type);
20140 else
20141 printf (_("UINT32_OR (%#x): "), type);
20142 if (datasz != 4)
20143 printf (_("<corrupt length: %#x> "), datasz);
20144 else
20145 printf ("%#x", (unsigned int) byte_get (ptr, 4));
20146 goto next;
20147 }
20148 break;
20149 }
20150 }
20151
20152 if (type < GNU_PROPERTY_LOPROC)
20153 printf (_("<unknown type %#x data: "), type);
20154 else if (type < GNU_PROPERTY_LOUSER)
20155 printf (_("<processor-specific type %#x data: "), type);
20156 else
20157 printf (_("<application-specific type %#x data: "), type);
20158 for (j = 0; j < datasz; ++j)
20159 printf ("%02x ", ptr[j] & 0xff);
20160 printf (">");
20161
20162 next:
20163 ptr += ((datasz + (size - 1)) & ~ (size - 1));
20164 if (ptr == ptr_end)
20165 break;
20166
20167 if (do_wide)
20168 printf (", ");
20169 else
20170 printf ("\n\t");
20171 }
20172
20173 printf ("\n");
20174 }
20175
20176 static bool
20177 print_gnu_note (Filedata * filedata, Elf_Internal_Note *pnote)
20178 {
20179 /* NB/ Keep this switch statement in sync with get_gnu_elf_note_type (). */
20180 switch (pnote->type)
20181 {
20182 case NT_GNU_BUILD_ID:
20183 {
20184 unsigned long i;
20185
20186 printf (_(" Build ID: "));
20187 for (i = 0; i < pnote->descsz; ++i)
20188 printf ("%02x", pnote->descdata[i] & 0xff);
20189 printf ("\n");
20190 }
20191 break;
20192
20193 case NT_GNU_ABI_TAG:
20194 {
20195 unsigned long os, major, minor, subminor;
20196 const char *osname;
20197
20198 /* PR 17531: file: 030-599401-0.004. */
20199 if (pnote->descsz < 16)
20200 {
20201 printf (_(" <corrupt GNU_ABI_TAG>\n"));
20202 break;
20203 }
20204
20205 os = byte_get ((unsigned char *) pnote->descdata, 4);
20206 major = byte_get ((unsigned char *) pnote->descdata + 4, 4);
20207 minor = byte_get ((unsigned char *) pnote->descdata + 8, 4);
20208 subminor = byte_get ((unsigned char *) pnote->descdata + 12, 4);
20209
20210 switch (os)
20211 {
20212 case GNU_ABI_TAG_LINUX:
20213 osname = "Linux";
20214 break;
20215 case GNU_ABI_TAG_HURD:
20216 osname = "Hurd";
20217 break;
20218 case GNU_ABI_TAG_SOLARIS:
20219 osname = "Solaris";
20220 break;
20221 case GNU_ABI_TAG_FREEBSD:
20222 osname = "FreeBSD";
20223 break;
20224 case GNU_ABI_TAG_NETBSD:
20225 osname = "NetBSD";
20226 break;
20227 case GNU_ABI_TAG_SYLLABLE:
20228 osname = "Syllable";
20229 break;
20230 case GNU_ABI_TAG_NACL:
20231 osname = "NaCl";
20232 break;
20233 default:
20234 osname = "Unknown";
20235 break;
20236 }
20237
20238 printf (_(" OS: %s, ABI: %ld.%ld.%ld\n"), osname,
20239 major, minor, subminor);
20240 }
20241 break;
20242
20243 case NT_GNU_GOLD_VERSION:
20244 {
20245 unsigned long i;
20246
20247 printf (_(" Version: "));
20248 for (i = 0; i < pnote->descsz && pnote->descdata[i] != '\0'; ++i)
20249 printf ("%c", pnote->descdata[i]);
20250 printf ("\n");
20251 }
20252 break;
20253
20254 case NT_GNU_HWCAP:
20255 {
20256 unsigned long num_entries, mask;
20257
20258 /* Hardware capabilities information. Word 0 is the number of entries.
20259 Word 1 is a bitmask of enabled entries. The rest of the descriptor
20260 is a series of entries, where each entry is a single byte followed
20261 by a nul terminated string. The byte gives the bit number to test
20262 if enabled in the bitmask. */
20263 printf (_(" Hardware Capabilities: "));
20264 if (pnote->descsz < 8)
20265 {
20266 error (_("<corrupt GNU_HWCAP>\n"));
20267 return false;
20268 }
20269 num_entries = byte_get ((unsigned char *) pnote->descdata, 4);
20270 mask = byte_get ((unsigned char *) pnote->descdata + 4, 4);
20271 printf (_("num entries: %ld, enabled mask: %lx\n"), num_entries, mask);
20272 /* FIXME: Add code to display the entries... */
20273 }
20274 break;
20275
20276 case NT_GNU_PROPERTY_TYPE_0:
20277 print_gnu_property_note (filedata, pnote);
20278 break;
20279
20280 default:
20281 /* Handle unrecognised types. An error message should have already been
20282 created by get_gnu_elf_note_type(), so all that we need to do is to
20283 display the data. */
20284 {
20285 unsigned long i;
20286
20287 printf (_(" Description data: "));
20288 for (i = 0; i < pnote->descsz; ++i)
20289 printf ("%02x ", pnote->descdata[i] & 0xff);
20290 printf ("\n");
20291 }
20292 break;
20293 }
20294
20295 return true;
20296 }
20297
20298 static const char *
20299 get_v850_elf_note_type (enum v850_notes n_type)
20300 {
20301 static char buff[64];
20302
20303 switch (n_type)
20304 {
20305 case V850_NOTE_ALIGNMENT: return _("Alignment of 8-byte objects");
20306 case V850_NOTE_DATA_SIZE: return _("Sizeof double and long double");
20307 case V850_NOTE_FPU_INFO: return _("Type of FPU support needed");
20308 case V850_NOTE_SIMD_INFO: return _("Use of SIMD instructions");
20309 case V850_NOTE_CACHE_INFO: return _("Use of cache");
20310 case V850_NOTE_MMU_INFO: return _("Use of MMU");
20311 default:
20312 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), n_type);
20313 return buff;
20314 }
20315 }
20316
20317 static bool
20318 print_v850_note (Elf_Internal_Note * pnote)
20319 {
20320 unsigned int val;
20321
20322 if (pnote->descsz != 4)
20323 return false;
20324
20325 val = byte_get ((unsigned char *) pnote->descdata, pnote->descsz);
20326
20327 if (val == 0)
20328 {
20329 printf (_("not set\n"));
20330 return true;
20331 }
20332
20333 switch (pnote->type)
20334 {
20335 case V850_NOTE_ALIGNMENT:
20336 switch (val)
20337 {
20338 case EF_RH850_DATA_ALIGN4: printf (_("4-byte\n")); return true;
20339 case EF_RH850_DATA_ALIGN8: printf (_("8-byte\n")); return true;
20340 }
20341 break;
20342
20343 case V850_NOTE_DATA_SIZE:
20344 switch (val)
20345 {
20346 case EF_RH850_DOUBLE32: printf (_("4-bytes\n")); return true;
20347 case EF_RH850_DOUBLE64: printf (_("8-bytes\n")); return true;
20348 }
20349 break;
20350
20351 case V850_NOTE_FPU_INFO:
20352 switch (val)
20353 {
20354 case EF_RH850_FPU20: printf (_("FPU-2.0\n")); return true;
20355 case EF_RH850_FPU30: printf (_("FPU-3.0\n")); return true;
20356 }
20357 break;
20358
20359 case V850_NOTE_MMU_INFO:
20360 case V850_NOTE_CACHE_INFO:
20361 case V850_NOTE_SIMD_INFO:
20362 if (val == EF_RH850_SIMD)
20363 {
20364 printf (_("yes\n"));
20365 return true;
20366 }
20367 break;
20368
20369 default:
20370 /* An 'unknown note type' message will already have been displayed. */
20371 break;
20372 }
20373
20374 printf (_("unknown value: %x\n"), val);
20375 return false;
20376 }
20377
20378 static bool
20379 process_netbsd_elf_note (Elf_Internal_Note * pnote)
20380 {
20381 unsigned int version;
20382
20383 switch (pnote->type)
20384 {
20385 case NT_NETBSD_IDENT:
20386 if (pnote->descsz < 1)
20387 break;
20388 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
20389 if ((version / 10000) % 100)
20390 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u%s%c)\n", pnote->descsz,
20391 version, version / 100000000, (version / 1000000) % 100,
20392 (version / 10000) % 100 > 26 ? "Z" : "",
20393 'A' + (version / 10000) % 26);
20394 else
20395 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u.%u)\n", pnote->descsz,
20396 version, version / 100000000, (version / 1000000) % 100,
20397 (version / 100) % 100);
20398 return true;
20399
20400 case NT_NETBSD_MARCH:
20401 printf (" NetBSD\t\t0x%08lx\tMARCH <%s>\n", pnote->descsz,
20402 pnote->descdata);
20403 return true;
20404
20405 case NT_NETBSD_PAX:
20406 if (pnote->descsz < 1)
20407 break;
20408 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
20409 printf (" NetBSD\t\t0x%08lx\tPaX <%s%s%s%s%s%s>\n", pnote->descsz,
20410 ((version & NT_NETBSD_PAX_MPROTECT) ? "+mprotect" : ""),
20411 ((version & NT_NETBSD_PAX_NOMPROTECT) ? "-mprotect" : ""),
20412 ((version & NT_NETBSD_PAX_GUARD) ? "+guard" : ""),
20413 ((version & NT_NETBSD_PAX_NOGUARD) ? "-guard" : ""),
20414 ((version & NT_NETBSD_PAX_ASLR) ? "+ASLR" : ""),
20415 ((version & NT_NETBSD_PAX_NOASLR) ? "-ASLR" : ""));
20416 return true;
20417 }
20418
20419 printf (" NetBSD\t0x%08lx\tUnknown note type: (0x%08lx)\n",
20420 pnote->descsz, pnote->type);
20421 return false;
20422 }
20423
20424 static const char *
20425 get_freebsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
20426 {
20427 switch (e_type)
20428 {
20429 case NT_FREEBSD_THRMISC:
20430 return _("NT_THRMISC (thrmisc structure)");
20431 case NT_FREEBSD_PROCSTAT_PROC:
20432 return _("NT_PROCSTAT_PROC (proc data)");
20433 case NT_FREEBSD_PROCSTAT_FILES:
20434 return _("NT_PROCSTAT_FILES (files data)");
20435 case NT_FREEBSD_PROCSTAT_VMMAP:
20436 return _("NT_PROCSTAT_VMMAP (vmmap data)");
20437 case NT_FREEBSD_PROCSTAT_GROUPS:
20438 return _("NT_PROCSTAT_GROUPS (groups data)");
20439 case NT_FREEBSD_PROCSTAT_UMASK:
20440 return _("NT_PROCSTAT_UMASK (umask data)");
20441 case NT_FREEBSD_PROCSTAT_RLIMIT:
20442 return _("NT_PROCSTAT_RLIMIT (rlimit data)");
20443 case NT_FREEBSD_PROCSTAT_OSREL:
20444 return _("NT_PROCSTAT_OSREL (osreldate data)");
20445 case NT_FREEBSD_PROCSTAT_PSSTRINGS:
20446 return _("NT_PROCSTAT_PSSTRINGS (ps_strings data)");
20447 case NT_FREEBSD_PROCSTAT_AUXV:
20448 return _("NT_PROCSTAT_AUXV (auxv data)");
20449 case NT_FREEBSD_PTLWPINFO:
20450 return _("NT_PTLWPINFO (ptrace_lwpinfo structure)");
20451 case NT_FREEBSD_X86_SEGBASES:
20452 return _("NT_X86_SEGBASES (x86 segment base registers)");
20453 }
20454 return get_note_type (filedata, e_type);
20455 }
20456
20457 static const char *
20458 get_netbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
20459 {
20460 static char buff[64];
20461
20462 switch (e_type)
20463 {
20464 case NT_NETBSDCORE_PROCINFO:
20465 /* NetBSD core "procinfo" structure. */
20466 return _("NetBSD procinfo structure");
20467
20468 case NT_NETBSDCORE_AUXV:
20469 return _("NetBSD ELF auxiliary vector data");
20470
20471 case NT_NETBSDCORE_LWPSTATUS:
20472 return _("PT_LWPSTATUS (ptrace_lwpstatus structure)");
20473
20474 default:
20475 /* As of Jan 2020 there are no other machine-independent notes
20476 defined for NetBSD core files. If the note type is less
20477 than the start of the machine-dependent note types, we don't
20478 understand it. */
20479
20480 if (e_type < NT_NETBSDCORE_FIRSTMACH)
20481 {
20482 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20483 return buff;
20484 }
20485 break;
20486 }
20487
20488 switch (filedata->file_header.e_machine)
20489 {
20490 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
20491 and PT_GETFPREGS == mach+2. */
20492
20493 case EM_OLD_ALPHA:
20494 case EM_ALPHA:
20495 case EM_SPARC:
20496 case EM_SPARC32PLUS:
20497 case EM_SPARCV9:
20498 switch (e_type)
20499 {
20500 case NT_NETBSDCORE_FIRSTMACH + 0:
20501 return _("PT_GETREGS (reg structure)");
20502 case NT_NETBSDCORE_FIRSTMACH + 2:
20503 return _("PT_GETFPREGS (fpreg structure)");
20504 default:
20505 break;
20506 }
20507 break;
20508
20509 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
20510 There's also old PT___GETREGS40 == mach + 1 for old reg
20511 structure which lacks GBR. */
20512 case EM_SH:
20513 switch (e_type)
20514 {
20515 case NT_NETBSDCORE_FIRSTMACH + 1:
20516 return _("PT___GETREGS40 (old reg structure)");
20517 case NT_NETBSDCORE_FIRSTMACH + 3:
20518 return _("PT_GETREGS (reg structure)");
20519 case NT_NETBSDCORE_FIRSTMACH + 5:
20520 return _("PT_GETFPREGS (fpreg structure)");
20521 default:
20522 break;
20523 }
20524 break;
20525
20526 /* On all other arch's, PT_GETREGS == mach+1 and
20527 PT_GETFPREGS == mach+3. */
20528 default:
20529 switch (e_type)
20530 {
20531 case NT_NETBSDCORE_FIRSTMACH + 1:
20532 return _("PT_GETREGS (reg structure)");
20533 case NT_NETBSDCORE_FIRSTMACH + 3:
20534 return _("PT_GETFPREGS (fpreg structure)");
20535 default:
20536 break;
20537 }
20538 }
20539
20540 snprintf (buff, sizeof (buff), "PT_FIRSTMACH+%d",
20541 e_type - NT_NETBSDCORE_FIRSTMACH);
20542 return buff;
20543 }
20544
20545 static const char *
20546 get_openbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
20547 {
20548 switch (e_type)
20549 {
20550 case NT_OPENBSD_PROCINFO:
20551 return _("OpenBSD procinfo structure");
20552 case NT_OPENBSD_AUXV:
20553 return _("OpenBSD ELF auxiliary vector data");
20554 case NT_OPENBSD_REGS:
20555 return _("OpenBSD regular registers");
20556 case NT_OPENBSD_FPREGS:
20557 return _("OpenBSD floating point registers");
20558 case NT_OPENBSD_WCOOKIE:
20559 return _("OpenBSD window cookie");
20560 }
20561
20562 return get_note_type (filedata, e_type);
20563 }
20564
20565 static const char *
20566 get_stapsdt_note_type (unsigned e_type)
20567 {
20568 static char buff[64];
20569
20570 switch (e_type)
20571 {
20572 case NT_STAPSDT:
20573 return _("NT_STAPSDT (SystemTap probe descriptors)");
20574
20575 default:
20576 break;
20577 }
20578
20579 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20580 return buff;
20581 }
20582
20583 static bool
20584 print_stapsdt_note (Elf_Internal_Note *pnote)
20585 {
20586 size_t len, maxlen;
20587 unsigned long addr_size = is_32bit_elf ? 4 : 8;
20588 char *data = pnote->descdata;
20589 char *data_end = pnote->descdata + pnote->descsz;
20590 uint64_t pc, base_addr, semaphore;
20591 char *provider, *probe, *arg_fmt;
20592
20593 if (pnote->descsz < (addr_size * 3))
20594 goto stapdt_note_too_small;
20595
20596 pc = byte_get ((unsigned char *) data, addr_size);
20597 data += addr_size;
20598
20599 base_addr = byte_get ((unsigned char *) data, addr_size);
20600 data += addr_size;
20601
20602 semaphore = byte_get ((unsigned char *) data, addr_size);
20603 data += addr_size;
20604
20605 if (data >= data_end)
20606 goto stapdt_note_too_small;
20607 maxlen = data_end - data;
20608 len = strnlen (data, maxlen);
20609 if (len < maxlen)
20610 {
20611 provider = data;
20612 data += len + 1;
20613 }
20614 else
20615 goto stapdt_note_too_small;
20616
20617 if (data >= data_end)
20618 goto stapdt_note_too_small;
20619 maxlen = data_end - data;
20620 len = strnlen (data, maxlen);
20621 if (len < maxlen)
20622 {
20623 probe = data;
20624 data += len + 1;
20625 }
20626 else
20627 goto stapdt_note_too_small;
20628
20629 if (data >= data_end)
20630 goto stapdt_note_too_small;
20631 maxlen = data_end - data;
20632 len = strnlen (data, maxlen);
20633 if (len < maxlen)
20634 {
20635 arg_fmt = data;
20636 data += len + 1;
20637 }
20638 else
20639 goto stapdt_note_too_small;
20640
20641 printf (_(" Provider: %s\n"), provider);
20642 printf (_(" Name: %s\n"), probe);
20643 printf (_(" Location: "));
20644 print_vma (pc, FULL_HEX);
20645 printf (_(", Base: "));
20646 print_vma (base_addr, FULL_HEX);
20647 printf (_(", Semaphore: "));
20648 print_vma (semaphore, FULL_HEX);
20649 printf ("\n");
20650 printf (_(" Arguments: %s\n"), arg_fmt);
20651
20652 return data == data_end;
20653
20654 stapdt_note_too_small:
20655 printf (_(" <corrupt - note is too small>\n"));
20656 error (_("corrupt stapdt note - the data size is too small\n"));
20657 return false;
20658 }
20659
20660 static bool
20661 print_fdo_note (Elf_Internal_Note * pnote)
20662 {
20663 if (pnote->descsz > 0 && pnote->type == FDO_PACKAGING_METADATA)
20664 {
20665 printf (_(" Packaging Metadata: %.*s\n"), (int) pnote->descsz, pnote->descdata);
20666 return true;
20667 }
20668 return false;
20669 }
20670
20671 static const char *
20672 get_ia64_vms_note_type (unsigned e_type)
20673 {
20674 static char buff[64];
20675
20676 switch (e_type)
20677 {
20678 case NT_VMS_MHD:
20679 return _("NT_VMS_MHD (module header)");
20680 case NT_VMS_LNM:
20681 return _("NT_VMS_LNM (language name)");
20682 case NT_VMS_SRC:
20683 return _("NT_VMS_SRC (source files)");
20684 case NT_VMS_TITLE:
20685 return "NT_VMS_TITLE";
20686 case NT_VMS_EIDC:
20687 return _("NT_VMS_EIDC (consistency check)");
20688 case NT_VMS_FPMODE:
20689 return _("NT_VMS_FPMODE (FP mode)");
20690 case NT_VMS_LINKTIME:
20691 return "NT_VMS_LINKTIME";
20692 case NT_VMS_IMGNAM:
20693 return _("NT_VMS_IMGNAM (image name)");
20694 case NT_VMS_IMGID:
20695 return _("NT_VMS_IMGID (image id)");
20696 case NT_VMS_LINKID:
20697 return _("NT_VMS_LINKID (link id)");
20698 case NT_VMS_IMGBID:
20699 return _("NT_VMS_IMGBID (build id)");
20700 case NT_VMS_GSTNAM:
20701 return _("NT_VMS_GSTNAM (sym table name)");
20702 case NT_VMS_ORIG_DYN:
20703 return "NT_VMS_ORIG_DYN";
20704 case NT_VMS_PATCHTIME:
20705 return "NT_VMS_PATCHTIME";
20706 default:
20707 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20708 return buff;
20709 }
20710 }
20711
20712 static bool
20713 print_ia64_vms_note (Elf_Internal_Note * pnote)
20714 {
20715 int maxlen = pnote->descsz;
20716
20717 if (maxlen < 2 || (unsigned long) maxlen != pnote->descsz)
20718 goto desc_size_fail;
20719
20720 switch (pnote->type)
20721 {
20722 case NT_VMS_MHD:
20723 if (maxlen <= 36)
20724 goto desc_size_fail;
20725
20726 int l = (int) strnlen (pnote->descdata + 34, maxlen - 34);
20727
20728 printf (_(" Creation date : %.17s\n"), pnote->descdata);
20729 printf (_(" Last patch date: %.17s\n"), pnote->descdata + 17);
20730 if (l + 34 < maxlen)
20731 {
20732 printf (_(" Module name : %s\n"), pnote->descdata + 34);
20733 if (l + 35 < maxlen)
20734 printf (_(" Module version : %s\n"), pnote->descdata + 34 + l + 1);
20735 else
20736 printf (_(" Module version : <missing>\n"));
20737 }
20738 else
20739 {
20740 printf (_(" Module name : <missing>\n"));
20741 printf (_(" Module version : <missing>\n"));
20742 }
20743 break;
20744
20745 case NT_VMS_LNM:
20746 printf (_(" Language: %.*s\n"), maxlen, pnote->descdata);
20747 break;
20748
20749 case NT_VMS_FPMODE:
20750 printf (_(" Floating Point mode: "));
20751 if (maxlen < 8)
20752 goto desc_size_fail;
20753 /* FIXME: Generate an error if descsz > 8 ? */
20754
20755 printf ("0x%016" PRIx64 "\n",
20756 byte_get ((unsigned char *) pnote->descdata, 8));
20757 break;
20758
20759 case NT_VMS_LINKTIME:
20760 printf (_(" Link time: "));
20761 if (maxlen < 8)
20762 goto desc_size_fail;
20763 /* FIXME: Generate an error if descsz > 8 ? */
20764
20765 print_vms_time (byte_get ((unsigned char *) pnote->descdata, 8));
20766 printf ("\n");
20767 break;
20768
20769 case NT_VMS_PATCHTIME:
20770 printf (_(" Patch time: "));
20771 if (maxlen < 8)
20772 goto desc_size_fail;
20773 /* FIXME: Generate an error if descsz > 8 ? */
20774
20775 print_vms_time (byte_get ((unsigned char *) pnote->descdata, 8));
20776 printf ("\n");
20777 break;
20778
20779 case NT_VMS_ORIG_DYN:
20780 if (maxlen < 34)
20781 goto desc_size_fail;
20782
20783 printf (_(" Major id: %u, minor id: %u\n"),
20784 (unsigned) byte_get ((unsigned char *) pnote->descdata, 4),
20785 (unsigned) byte_get ((unsigned char *) pnote->descdata + 4, 4));
20786 printf (_(" Last modified : "));
20787 print_vms_time (byte_get ((unsigned char *) pnote->descdata + 8, 8));
20788 printf (_("\n Link flags : "));
20789 printf ("0x%016" PRIx64 "\n",
20790 byte_get ((unsigned char *) pnote->descdata + 16, 8));
20791 printf (_(" Header flags: 0x%08x\n"),
20792 (unsigned) byte_get ((unsigned char *) pnote->descdata + 24, 4));
20793 printf (_(" Image id : %.*s\n"), maxlen - 32, pnote->descdata + 32);
20794 break;
20795
20796 case NT_VMS_IMGNAM:
20797 printf (_(" Image name: %.*s\n"), maxlen, pnote->descdata);
20798 break;
20799
20800 case NT_VMS_GSTNAM:
20801 printf (_(" Global symbol table name: %.*s\n"), maxlen, pnote->descdata);
20802 break;
20803
20804 case NT_VMS_IMGID:
20805 printf (_(" Image id: %.*s\n"), maxlen, pnote->descdata);
20806 break;
20807
20808 case NT_VMS_LINKID:
20809 printf (_(" Linker id: %.*s\n"), maxlen, pnote->descdata);
20810 break;
20811
20812 default:
20813 return false;
20814 }
20815
20816 return true;
20817
20818 desc_size_fail:
20819 printf (_(" <corrupt - data size is too small>\n"));
20820 error (_("corrupt IA64 note: data size is too small\n"));
20821 return false;
20822 }
20823
20824 struct build_attr_cache {
20825 Filedata *filedata;
20826 char *strtab;
20827 unsigned long strtablen;
20828 Elf_Internal_Sym *symtab;
20829 unsigned long nsyms;
20830 } ba_cache;
20831
20832 /* Find the symbol associated with a build attribute that is attached
20833 to address OFFSET. If PNAME is non-NULL then store the name of
20834 the symbol (if found) in the provided pointer, Returns NULL if a
20835 symbol could not be found. */
20836
20837 static Elf_Internal_Sym *
20838 get_symbol_for_build_attribute (Filedata *filedata,
20839 unsigned long offset,
20840 bool is_open_attr,
20841 const char **pname)
20842 {
20843 Elf_Internal_Sym *saved_sym = NULL;
20844 Elf_Internal_Sym *sym;
20845
20846 if (filedata->section_headers != NULL
20847 && (ba_cache.filedata == NULL || filedata != ba_cache.filedata))
20848 {
20849 Elf_Internal_Shdr * symsec;
20850
20851 free (ba_cache.strtab);
20852 ba_cache.strtab = NULL;
20853 free (ba_cache.symtab);
20854 ba_cache.symtab = NULL;
20855
20856 /* Load the symbol and string sections. */
20857 for (symsec = filedata->section_headers;
20858 symsec < filedata->section_headers + filedata->file_header.e_shnum;
20859 symsec ++)
20860 {
20861 if (symsec->sh_type == SHT_SYMTAB
20862 && get_symtab (filedata, symsec,
20863 &ba_cache.symtab, &ba_cache.nsyms,
20864 &ba_cache.strtab, &ba_cache.strtablen))
20865 break;
20866 }
20867 ba_cache.filedata = filedata;
20868 }
20869
20870 if (ba_cache.symtab == NULL)
20871 return NULL;
20872
20873 /* Find a symbol whose value matches offset. */
20874 for (sym = ba_cache.symtab; sym < ba_cache.symtab + ba_cache.nsyms; sym ++)
20875 if (sym->st_value == offset)
20876 {
20877 if (sym->st_name >= ba_cache.strtablen)
20878 /* Huh ? This should not happen. */
20879 continue;
20880
20881 if (ba_cache.strtab[sym->st_name] == 0)
20882 continue;
20883
20884 /* The AArch64, ARM and RISC-V architectures define mapping symbols
20885 (eg $d, $x, $t) which we want to ignore. */
20886 if (ba_cache.strtab[sym->st_name] == '$'
20887 && ba_cache.strtab[sym->st_name + 1] != 0
20888 && ba_cache.strtab[sym->st_name + 2] == 0)
20889 continue;
20890
20891 if (is_open_attr)
20892 {
20893 /* For OPEN attributes we prefer GLOBAL over LOCAL symbols
20894 and FILE or OBJECT symbols over NOTYPE symbols. We skip
20895 FUNC symbols entirely. */
20896 switch (ELF_ST_TYPE (sym->st_info))
20897 {
20898 case STT_OBJECT:
20899 case STT_FILE:
20900 saved_sym = sym;
20901 if (sym->st_size)
20902 {
20903 /* If the symbol has a size associated
20904 with it then we can stop searching. */
20905 sym = ba_cache.symtab + ba_cache.nsyms;
20906 }
20907 continue;
20908
20909 case STT_FUNC:
20910 /* Ignore function symbols. */
20911 continue;
20912
20913 default:
20914 break;
20915 }
20916
20917 switch (ELF_ST_BIND (sym->st_info))
20918 {
20919 case STB_GLOBAL:
20920 if (saved_sym == NULL
20921 || ELF_ST_TYPE (saved_sym->st_info) != STT_OBJECT)
20922 saved_sym = sym;
20923 break;
20924
20925 case STB_LOCAL:
20926 if (saved_sym == NULL)
20927 saved_sym = sym;
20928 break;
20929
20930 default:
20931 break;
20932 }
20933 }
20934 else
20935 {
20936 if (ELF_ST_TYPE (sym->st_info) != STT_FUNC)
20937 continue;
20938
20939 saved_sym = sym;
20940 break;
20941 }
20942 }
20943
20944 if (saved_sym && pname)
20945 * pname = ba_cache.strtab + saved_sym->st_name;
20946
20947 return saved_sym;
20948 }
20949
20950 /* Returns true iff addr1 and addr2 are in the same section. */
20951
20952 static bool
20953 same_section (Filedata * filedata, unsigned long addr1, unsigned long addr2)
20954 {
20955 Elf_Internal_Shdr * a1;
20956 Elf_Internal_Shdr * a2;
20957
20958 a1 = find_section_by_address (filedata, addr1);
20959 a2 = find_section_by_address (filedata, addr2);
20960
20961 return a1 == a2 && a1 != NULL;
20962 }
20963
20964 static bool
20965 print_gnu_build_attribute_description (Elf_Internal_Note * pnote,
20966 Filedata * filedata)
20967 {
20968 static unsigned long global_offset = 0;
20969 static unsigned long global_end = 0;
20970 static unsigned long func_offset = 0;
20971 static unsigned long func_end = 0;
20972
20973 Elf_Internal_Sym *sym;
20974 const char *name;
20975 unsigned long start;
20976 unsigned long end;
20977 bool is_open_attr = pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN;
20978
20979 switch (pnote->descsz)
20980 {
20981 case 0:
20982 /* A zero-length description means that the range of
20983 the previous note of the same type should be used. */
20984 if (is_open_attr)
20985 {
20986 if (global_end > global_offset)
20987 printf (_(" Applies to region from %#lx to %#lx\n"),
20988 global_offset, global_end);
20989 else
20990 printf (_(" Applies to region from %#lx\n"), global_offset);
20991 }
20992 else
20993 {
20994 if (func_end > func_offset)
20995 printf (_(" Applies to region from %#lx to %#lx\n"), func_offset, func_end);
20996 else
20997 printf (_(" Applies to region from %#lx\n"), func_offset);
20998 }
20999 return true;
21000
21001 case 4:
21002 start = byte_get ((unsigned char *) pnote->descdata, 4);
21003 end = 0;
21004 break;
21005
21006 case 8:
21007 start = byte_get ((unsigned char *) pnote->descdata, 4);
21008 end = byte_get ((unsigned char *) pnote->descdata + 4, 4);
21009 break;
21010
21011 case 16:
21012 start = byte_get ((unsigned char *) pnote->descdata, 8);
21013 end = byte_get ((unsigned char *) pnote->descdata + 8, 8);
21014 break;
21015
21016 default:
21017 error (_(" <invalid description size: %lx>\n"), pnote->descsz);
21018 printf (_(" <invalid descsz>"));
21019 return false;
21020 }
21021
21022 name = NULL;
21023 sym = get_symbol_for_build_attribute (filedata, start, is_open_attr, & name);
21024 /* As of version 5 of the annobin plugin, filename symbols are biased by 2
21025 in order to avoid them being confused with the start address of the
21026 first function in the file... */
21027 if (sym == NULL && is_open_attr)
21028 sym = get_symbol_for_build_attribute (filedata, start + 2, is_open_attr,
21029 & name);
21030
21031 if (end == 0 && sym != NULL && sym->st_size > 0)
21032 end = start + sym->st_size;
21033
21034 if (is_open_attr)
21035 {
21036 /* FIXME: Need to properly allow for section alignment.
21037 16 is just the alignment used on x86_64. */
21038 if (global_end > 0
21039 && start > BFD_ALIGN (global_end, 16)
21040 /* Build notes are not guaranteed to be organised in order of
21041 increasing address, but we should find the all of the notes
21042 for one section in the same place. */
21043 && same_section (filedata, start, global_end))
21044 warn (_("Gap in build notes detected from %#lx to %#lx\n"),
21045 global_end + 1, start - 1);
21046
21047 printf (_(" Applies to region from %#lx"), start);
21048 global_offset = start;
21049
21050 if (end)
21051 {
21052 printf (_(" to %#lx"), end);
21053 global_end = end;
21054 }
21055 }
21056 else
21057 {
21058 printf (_(" Applies to region from %#lx"), start);
21059 func_offset = start;
21060
21061 if (end)
21062 {
21063 printf (_(" to %#lx"), end);
21064 func_end = end;
21065 }
21066 }
21067
21068 if (sym && name)
21069 printf (_(" (%s)"), name);
21070
21071 printf ("\n");
21072 return true;
21073 }
21074
21075 static bool
21076 print_gnu_build_attribute_name (Elf_Internal_Note * pnote)
21077 {
21078 static const char string_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_STRING, 0 };
21079 static const char number_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC, 0 };
21080 static const char bool_expected [3] = { GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE, GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE, 0 };
21081 char name_type;
21082 char name_attribute;
21083 const char * expected_types;
21084 const char * name = pnote->namedata;
21085 const char * text;
21086 signed int left;
21087
21088 if (name == NULL || pnote->namesz < 2)
21089 {
21090 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
21091 print_symbol (-20, _(" <corrupt name>"));
21092 return false;
21093 }
21094
21095 if (do_wide)
21096 left = 28;
21097 else
21098 left = 20;
21099
21100 /* Version 2 of the spec adds a "GA" prefix to the name field. */
21101 if (name[0] == 'G' && name[1] == 'A')
21102 {
21103 if (pnote->namesz < 4)
21104 {
21105 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
21106 print_symbol (-20, _(" <corrupt name>"));
21107 return false;
21108 }
21109
21110 printf ("GA");
21111 name += 2;
21112 left -= 2;
21113 }
21114
21115 switch ((name_type = * name))
21116 {
21117 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
21118 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
21119 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
21120 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
21121 printf ("%c", * name);
21122 left --;
21123 break;
21124 default:
21125 error (_("unrecognised attribute type in name field: %d\n"), name_type);
21126 print_symbol (-20, _("<unknown name type>"));
21127 return false;
21128 }
21129
21130 ++ name;
21131 text = NULL;
21132
21133 switch ((name_attribute = * name))
21134 {
21135 case GNU_BUILD_ATTRIBUTE_VERSION:
21136 text = _("<version>");
21137 expected_types = string_expected;
21138 ++ name;
21139 break;
21140 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
21141 text = _("<stack prot>");
21142 expected_types = "!+*";
21143 ++ name;
21144 break;
21145 case GNU_BUILD_ATTRIBUTE_RELRO:
21146 text = _("<relro>");
21147 expected_types = bool_expected;
21148 ++ name;
21149 break;
21150 case GNU_BUILD_ATTRIBUTE_STACK_SIZE:
21151 text = _("<stack size>");
21152 expected_types = number_expected;
21153 ++ name;
21154 break;
21155 case GNU_BUILD_ATTRIBUTE_TOOL:
21156 text = _("<tool>");
21157 expected_types = string_expected;
21158 ++ name;
21159 break;
21160 case GNU_BUILD_ATTRIBUTE_ABI:
21161 text = _("<ABI>");
21162 expected_types = "$*";
21163 ++ name;
21164 break;
21165 case GNU_BUILD_ATTRIBUTE_PIC:
21166 text = _("<PIC>");
21167 expected_types = number_expected;
21168 ++ name;
21169 break;
21170 case GNU_BUILD_ATTRIBUTE_SHORT_ENUM:
21171 text = _("<short enum>");
21172 expected_types = bool_expected;
21173 ++ name;
21174 break;
21175 default:
21176 if (ISPRINT (* name))
21177 {
21178 int len = strnlen (name, pnote->namesz - (name - pnote->namedata)) + 1;
21179
21180 if (len > left && ! do_wide)
21181 len = left;
21182 printf ("%.*s:", len, name);
21183 left -= len;
21184 name += len;
21185 }
21186 else
21187 {
21188 static char tmpbuf [128];
21189
21190 error (_("unrecognised byte in name field: %d\n"), * name);
21191 sprintf (tmpbuf, _("<unknown:_%d>"), * name);
21192 text = tmpbuf;
21193 name ++;
21194 }
21195 expected_types = "*$!+";
21196 break;
21197 }
21198
21199 if (text)
21200 left -= printf ("%s", text);
21201
21202 if (strchr (expected_types, name_type) == NULL)
21203 warn (_("attribute does not have an expected type (%c)\n"), name_type);
21204
21205 if ((unsigned long)(name - pnote->namedata) > pnote->namesz)
21206 {
21207 error (_("corrupt name field: namesz: %lu but parsing gets to %ld\n"),
21208 (unsigned long) pnote->namesz,
21209 (long) (name - pnote->namedata));
21210 return false;
21211 }
21212
21213 if (left < 1 && ! do_wide)
21214 return true;
21215
21216 switch (name_type)
21217 {
21218 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
21219 {
21220 unsigned int bytes;
21221 unsigned long long val = 0;
21222 unsigned int shift = 0;
21223 char * decoded = NULL;
21224
21225 bytes = pnote->namesz - (name - pnote->namedata);
21226 if (bytes > 0)
21227 /* The -1 is because the name field is always 0 terminated, and we
21228 want to be able to ensure that the shift in the while loop below
21229 will not overflow. */
21230 -- bytes;
21231
21232 if (bytes > sizeof (val))
21233 {
21234 error (_("corrupt numeric name field: too many bytes in the value: %x\n"),
21235 bytes);
21236 bytes = sizeof (val);
21237 }
21238 /* We do not bother to warn if bytes == 0 as this can
21239 happen with some early versions of the gcc plugin. */
21240
21241 while (bytes --)
21242 {
21243 unsigned long long byte = *name++ & 0xff;
21244
21245 val |= byte << shift;
21246 shift += 8;
21247 }
21248
21249 switch (name_attribute)
21250 {
21251 case GNU_BUILD_ATTRIBUTE_PIC:
21252 switch (val)
21253 {
21254 case 0: decoded = "static"; break;
21255 case 1: decoded = "pic"; break;
21256 case 2: decoded = "PIC"; break;
21257 case 3: decoded = "pie"; break;
21258 case 4: decoded = "PIE"; break;
21259 default: break;
21260 }
21261 break;
21262 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
21263 switch (val)
21264 {
21265 /* Based upon the SPCT_FLAG_xxx enum values in gcc/cfgexpand.c. */
21266 case 0: decoded = "off"; break;
21267 case 1: decoded = "on"; break;
21268 case 2: decoded = "all"; break;
21269 case 3: decoded = "strong"; break;
21270 case 4: decoded = "explicit"; break;
21271 default: break;
21272 }
21273 break;
21274 default:
21275 break;
21276 }
21277
21278 if (decoded != NULL)
21279 {
21280 print_symbol (-left, decoded);
21281 left = 0;
21282 }
21283 else if (val == 0)
21284 {
21285 printf ("0x0");
21286 left -= 3;
21287 }
21288 else
21289 {
21290 if (do_wide)
21291 left -= printf ("0x%llx", val);
21292 else
21293 left -= printf ("0x%-.*llx", left, val);
21294 }
21295 }
21296 break;
21297 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
21298 left -= print_symbol (- left, name);
21299 break;
21300 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
21301 left -= print_symbol (- left, "true");
21302 break;
21303 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
21304 left -= print_symbol (- left, "false");
21305 break;
21306 }
21307
21308 if (do_wide && left > 0)
21309 printf ("%-*s", left, " ");
21310
21311 return true;
21312 }
21313
21314 /* Print the contents of PNOTE as hex. */
21315
21316 static void
21317 print_note_contents_hex (Elf_Internal_Note *pnote)
21318 {
21319 if (pnote->descsz)
21320 {
21321 unsigned long i;
21322
21323 printf (_(" description data: "));
21324 for (i = 0; i < pnote->descsz; i++)
21325 printf ("%02x ", pnote->descdata[i] & 0xff);
21326 if (!do_wide)
21327 printf ("\n");
21328 }
21329
21330 if (do_wide)
21331 printf ("\n");
21332 }
21333
21334 #if defined HAVE_MSGPACK
21335
21336 static void
21337 print_indents (int n)
21338 {
21339 printf (" ");
21340
21341 for (int i = 0; i < n; i++)
21342 printf (" ");
21343 }
21344
21345 /* Print OBJ in human-readable form. */
21346
21347 static void
21348 dump_msgpack_obj (const msgpack_object *obj, int indent)
21349 {
21350 switch (obj->type)
21351 {
21352 case MSGPACK_OBJECT_NIL:
21353 printf ("(nil)");
21354 break;
21355
21356 case MSGPACK_OBJECT_BOOLEAN:
21357 printf ("%s", obj->via.boolean ? "true" : "false");
21358 break;
21359
21360 case MSGPACK_OBJECT_POSITIVE_INTEGER:
21361 printf ("%" PRIu64, obj->via.u64);
21362 break;
21363
21364 case MSGPACK_OBJECT_NEGATIVE_INTEGER:
21365 printf ("%" PRIi64, obj->via.i64);
21366 break;
21367
21368 case MSGPACK_OBJECT_FLOAT32:
21369 case MSGPACK_OBJECT_FLOAT64:
21370 printf ("%f", obj->via.f64);
21371 break;
21372
21373 case MSGPACK_OBJECT_STR:
21374 printf ("\"%.*s\"", obj->via.str.size, obj->via.str.ptr);
21375 break;
21376
21377 case MSGPACK_OBJECT_ARRAY:
21378 {
21379 const msgpack_object_array *array = &obj->via.array;
21380
21381 printf ("[\n");
21382 ++indent;
21383
21384 for (uint32_t i = 0; i < array->size; ++i)
21385 {
21386 const msgpack_object *item = &array->ptr[i];
21387
21388 print_indents (indent);
21389 dump_msgpack_obj (item, indent);
21390 printf (",\n");
21391 }
21392
21393 --indent;
21394 print_indents (indent);
21395 printf ("]");
21396 break;
21397 }
21398 break;
21399
21400 case MSGPACK_OBJECT_MAP:
21401 {
21402 const msgpack_object_map *map = &obj->via.map;
21403
21404 printf ("{\n");
21405 ++indent;
21406
21407 for (uint32_t i = 0; i < map->size; ++i)
21408 {
21409 const msgpack_object_kv *kv = &map->ptr[i];
21410 const msgpack_object *key = &kv->key;
21411 const msgpack_object *val = &kv->val;
21412
21413 print_indents (indent);
21414 dump_msgpack_obj (key, indent);
21415 printf (": ");
21416 dump_msgpack_obj (val, indent);
21417
21418 printf (",\n");
21419 }
21420
21421 --indent;
21422 print_indents (indent);
21423 printf ("}");
21424
21425 break;
21426 }
21427
21428 case MSGPACK_OBJECT_BIN:
21429 printf ("(bin)");
21430 break;
21431
21432 case MSGPACK_OBJECT_EXT:
21433 printf ("(ext)");
21434 break;
21435 }
21436 }
21437
21438 static void
21439 dump_msgpack (const msgpack_unpacked *msg)
21440 {
21441 print_indents (0);
21442 dump_msgpack_obj (&msg->data, 0);
21443 printf ("\n");
21444 }
21445
21446 #endif /* defined HAVE_MSGPACK */
21447
21448 static bool
21449 print_amdgpu_note (Elf_Internal_Note *pnote)
21450 {
21451 #if defined HAVE_MSGPACK
21452 /* If msgpack is available, decode and dump the note's content. */
21453 bool ret;
21454 msgpack_unpacked msg;
21455 msgpack_unpack_return msgpack_ret;
21456
21457 assert (pnote->type == NT_AMDGPU_METADATA);
21458
21459 msgpack_unpacked_init (&msg);
21460 msgpack_ret = msgpack_unpack_next (&msg, pnote->descdata, pnote->descsz,
21461 NULL);
21462
21463 switch (msgpack_ret)
21464 {
21465 case MSGPACK_UNPACK_SUCCESS:
21466 dump_msgpack (&msg);
21467 ret = true;
21468 break;
21469
21470 default:
21471 error (_("failed to unpack msgpack contents in NT_AMDGPU_METADATA note"));
21472 ret = false;
21473 break;
21474 }
21475
21476 msgpack_unpacked_destroy (&msg);
21477 return ret;
21478 #else
21479 /* msgpack is not available, dump contents as hex. */
21480 print_note_contents_hex (pnote);
21481 return true;
21482 #endif
21483 }
21484
21485 /* Note that by the ELF standard, the name field is already null byte
21486 terminated, and namesz includes the terminating null byte.
21487 I.E. the value of namesz for the name "FSF" is 4.
21488
21489 If the value of namesz is zero, there is no name present. */
21490
21491 static bool
21492 process_note (Elf_Internal_Note * pnote,
21493 Filedata * filedata)
21494 {
21495 const char * name = pnote->namesz ? pnote->namedata : "(NONE)";
21496 const char * nt;
21497
21498 if (pnote->namesz == 0)
21499 /* If there is no note name, then use the default set of
21500 note type strings. */
21501 nt = get_note_type (filedata, pnote->type);
21502
21503 else if (startswith (pnote->namedata, "GNU"))
21504 /* GNU-specific object file notes. */
21505 nt = get_gnu_elf_note_type (pnote->type);
21506
21507 else if (startswith (pnote->namedata, "AMDGPU"))
21508 /* AMDGPU-specific object file notes. */
21509 nt = get_amdgpu_elf_note_type (pnote->type);
21510
21511 else if (startswith (pnote->namedata, "FreeBSD"))
21512 /* FreeBSD-specific core file notes. */
21513 nt = get_freebsd_elfcore_note_type (filedata, pnote->type);
21514
21515 else if (startswith (pnote->namedata, "NetBSD-CORE"))
21516 /* NetBSD-specific core file notes. */
21517 nt = get_netbsd_elfcore_note_type (filedata, pnote->type);
21518
21519 else if (startswith (pnote->namedata, "NetBSD"))
21520 /* NetBSD-specific core file notes. */
21521 return process_netbsd_elf_note (pnote);
21522
21523 else if (startswith (pnote->namedata, "PaX"))
21524 /* NetBSD-specific core file notes. */
21525 return process_netbsd_elf_note (pnote);
21526
21527 else if (startswith (pnote->namedata, "OpenBSD"))
21528 /* OpenBSD-specific core file notes. */
21529 nt = get_openbsd_elfcore_note_type (filedata, pnote->type);
21530
21531 else if (startswith (pnote->namedata, "SPU/"))
21532 {
21533 /* SPU-specific core file notes. */
21534 nt = pnote->namedata + 4;
21535 name = "SPU";
21536 }
21537
21538 else if (startswith (pnote->namedata, "IPF/VMS"))
21539 /* VMS/ia64-specific file notes. */
21540 nt = get_ia64_vms_note_type (pnote->type);
21541
21542 else if (startswith (pnote->namedata, "stapsdt"))
21543 nt = get_stapsdt_note_type (pnote->type);
21544
21545 else
21546 /* Don't recognize this note name; just use the default set of
21547 note type strings. */
21548 nt = get_note_type (filedata, pnote->type);
21549
21550 printf (" ");
21551
21552 if (((startswith (pnote->namedata, "GA")
21553 && strchr ("*$!+", pnote->namedata[2]) != NULL)
21554 || strchr ("*$!+", pnote->namedata[0]) != NULL)
21555 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
21556 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
21557 print_gnu_build_attribute_name (pnote);
21558 else
21559 print_symbol (-20, name);
21560
21561 if (do_wide)
21562 printf (" 0x%08lx\t%s\t", pnote->descsz, nt);
21563 else
21564 printf (" 0x%08lx\t%s\n", pnote->descsz, nt);
21565
21566 if (startswith (pnote->namedata, "IPF/VMS"))
21567 return print_ia64_vms_note (pnote);
21568 else if (startswith (pnote->namedata, "GNU"))
21569 return print_gnu_note (filedata, pnote);
21570 else if (startswith (pnote->namedata, "stapsdt"))
21571 return print_stapsdt_note (pnote);
21572 else if (startswith (pnote->namedata, "CORE"))
21573 return print_core_note (pnote);
21574 else if (startswith (pnote->namedata, "FDO"))
21575 return print_fdo_note (pnote);
21576 else if (((startswith (pnote->namedata, "GA")
21577 && strchr ("*$!+", pnote->namedata[2]) != NULL)
21578 || strchr ("*$!+", pnote->namedata[0]) != NULL)
21579 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
21580 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
21581 return print_gnu_build_attribute_description (pnote, filedata);
21582 else if (startswith (pnote->namedata, "AMDGPU")
21583 && pnote->type == NT_AMDGPU_METADATA)
21584 return print_amdgpu_note (pnote);
21585
21586 print_note_contents_hex (pnote);
21587 return true;
21588 }
21589
21590 static bool
21591 process_notes_at (Filedata * filedata,
21592 Elf_Internal_Shdr * section,
21593 uint64_t offset,
21594 uint64_t length,
21595 uint64_t align)
21596 {
21597 Elf_External_Note *pnotes;
21598 Elf_External_Note *external;
21599 char *end;
21600 bool res = true;
21601
21602 if (length <= 0)
21603 return false;
21604
21605 if (section)
21606 {
21607 pnotes = (Elf_External_Note *) get_section_contents (section, filedata);
21608 if (pnotes)
21609 {
21610 if (! apply_relocations (filedata, section, (unsigned char *) pnotes, length, NULL, NULL))
21611 {
21612 free (pnotes);
21613 return false;
21614 }
21615 }
21616 }
21617 else
21618 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
21619 _("notes"));
21620
21621 if (pnotes == NULL)
21622 return false;
21623
21624 external = pnotes;
21625
21626 if (filedata->is_separate)
21627 printf (_("In linked file '%s': "), filedata->file_name);
21628 else
21629 printf ("\n");
21630 if (section)
21631 printf (_("Displaying notes found in: %s\n"), printable_section_name (filedata, section));
21632 else
21633 printf (_("Displaying notes found at file offset 0x%08lx with length 0x%08lx:\n"),
21634 (unsigned long) offset, (unsigned long) length);
21635
21636 /* NB: Some note sections may have alignment value of 0 or 1. gABI
21637 specifies that notes should be aligned to 4 bytes in 32-bit
21638 objects and to 8 bytes in 64-bit objects. As a Linux extension,
21639 we also support 4 byte alignment in 64-bit objects. If section
21640 alignment is less than 4, we treate alignment as 4 bytes. */
21641 if (align < 4)
21642 align = 4;
21643 else if (align != 4 && align != 8)
21644 {
21645 warn (_("Corrupt note: alignment %ld, expecting 4 or 8\n"),
21646 (long) align);
21647 free (pnotes);
21648 return false;
21649 }
21650
21651 printf (_(" %-20s %-10s\tDescription\n"), _("Owner"), _("Data size"));
21652
21653 end = (char *) pnotes + length;
21654 while ((char *) external < end)
21655 {
21656 Elf_Internal_Note inote;
21657 size_t min_notesz;
21658 char * next;
21659 char * temp = NULL;
21660 size_t data_remaining = end - (char *) external;
21661
21662 if (!is_ia64_vms (filedata))
21663 {
21664 /* PR binutils/15191
21665 Make sure that there is enough data to read. */
21666 min_notesz = offsetof (Elf_External_Note, name);
21667 if (data_remaining < min_notesz)
21668 {
21669 warn (ngettext ("Corrupt note: only %ld byte remains, "
21670 "not enough for a full note\n",
21671 "Corrupt note: only %ld bytes remain, "
21672 "not enough for a full note\n",
21673 data_remaining),
21674 (long) data_remaining);
21675 break;
21676 }
21677 data_remaining -= min_notesz;
21678
21679 inote.type = BYTE_GET (external->type);
21680 inote.namesz = BYTE_GET (external->namesz);
21681 inote.namedata = external->name;
21682 inote.descsz = BYTE_GET (external->descsz);
21683 inote.descdata = ((char *) external
21684 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
21685 inote.descpos = offset + (inote.descdata - (char *) pnotes);
21686 next = ((char *) external
21687 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
21688 }
21689 else
21690 {
21691 Elf64_External_VMS_Note *vms_external;
21692
21693 /* PR binutils/15191
21694 Make sure that there is enough data to read. */
21695 min_notesz = offsetof (Elf64_External_VMS_Note, name);
21696 if (data_remaining < min_notesz)
21697 {
21698 warn (ngettext ("Corrupt note: only %ld byte remains, "
21699 "not enough for a full note\n",
21700 "Corrupt note: only %ld bytes remain, "
21701 "not enough for a full note\n",
21702 data_remaining),
21703 (long) data_remaining);
21704 break;
21705 }
21706 data_remaining -= min_notesz;
21707
21708 vms_external = (Elf64_External_VMS_Note *) external;
21709 inote.type = BYTE_GET (vms_external->type);
21710 inote.namesz = BYTE_GET (vms_external->namesz);
21711 inote.namedata = vms_external->name;
21712 inote.descsz = BYTE_GET (vms_external->descsz);
21713 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
21714 inote.descpos = offset + (inote.descdata - (char *) pnotes);
21715 next = inote.descdata + align_power (inote.descsz, 3);
21716 }
21717
21718 /* PR 17531: file: 3443835e. */
21719 /* PR 17531: file: id:000000,sig:11,src:006986,op:havoc,rep:4. */
21720 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
21721 || (size_t) (inote.descdata - inote.namedata) > data_remaining
21722 || (size_t) (next - inote.descdata) < inote.descsz
21723 || ((size_t) (next - inote.descdata)
21724 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
21725 {
21726 warn (_("note with invalid namesz and/or descsz found at offset 0x%lx\n"),
21727 (unsigned long) ((char *) external - (char *) pnotes));
21728 warn (_(" type: 0x%lx, namesize: 0x%08lx, descsize: 0x%08lx, alignment: %u\n"),
21729 inote.type, inote.namesz, inote.descsz, (int) align);
21730 break;
21731 }
21732
21733 external = (Elf_External_Note *) next;
21734
21735 /* Verify that name is null terminated. It appears that at least
21736 one version of Linux (RedHat 6.0) generates corefiles that don't
21737 comply with the ELF spec by failing to include the null byte in
21738 namesz. */
21739 if (inote.namesz > 0 && inote.namedata[inote.namesz - 1] != '\0')
21740 {
21741 if ((size_t) (inote.descdata - inote.namedata) == inote.namesz)
21742 {
21743 temp = (char *) malloc (inote.namesz + 1);
21744 if (temp == NULL)
21745 {
21746 error (_("Out of memory allocating space for inote name\n"));
21747 res = false;
21748 break;
21749 }
21750
21751 memcpy (temp, inote.namedata, inote.namesz);
21752 inote.namedata = temp;
21753 }
21754 inote.namedata[inote.namesz] = 0;
21755 }
21756
21757 if (! process_note (& inote, filedata))
21758 res = false;
21759
21760 free (temp);
21761 temp = NULL;
21762 }
21763
21764 free (pnotes);
21765
21766 return res;
21767 }
21768
21769 static bool
21770 process_corefile_note_segments (Filedata * filedata)
21771 {
21772 Elf_Internal_Phdr *segment;
21773 unsigned int i;
21774 bool res = true;
21775
21776 if (! get_program_headers (filedata))
21777 return true;
21778
21779 for (i = 0, segment = filedata->program_headers;
21780 i < filedata->file_header.e_phnum;
21781 i++, segment++)
21782 {
21783 if (segment->p_type == PT_NOTE)
21784 if (! process_notes_at (filedata, NULL, segment->p_offset,
21785 segment->p_filesz, segment->p_align))
21786 res = false;
21787 }
21788
21789 return res;
21790 }
21791
21792 static bool
21793 process_v850_notes (Filedata * filedata, uint64_t offset, uint64_t length)
21794 {
21795 Elf_External_Note * pnotes;
21796 Elf_External_Note * external;
21797 char * end;
21798 bool res = true;
21799
21800 if (length <= 0)
21801 return false;
21802
21803 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
21804 _("v850 notes"));
21805 if (pnotes == NULL)
21806 return false;
21807
21808 external = pnotes;
21809 end = (char*) pnotes + length;
21810
21811 printf (_("\nDisplaying contents of Renesas V850 notes section at offset 0x%lx with length 0x%lx:\n"),
21812 (unsigned long) offset, (unsigned long) length);
21813
21814 while ((char *) external + sizeof (Elf_External_Note) < end)
21815 {
21816 Elf_External_Note * next;
21817 Elf_Internal_Note inote;
21818
21819 inote.type = BYTE_GET (external->type);
21820 inote.namesz = BYTE_GET (external->namesz);
21821 inote.namedata = external->name;
21822 inote.descsz = BYTE_GET (external->descsz);
21823 inote.descdata = inote.namedata + align_power (inote.namesz, 2);
21824 inote.descpos = offset + (inote.descdata - (char *) pnotes);
21825
21826 if (inote.descdata < (char *) pnotes || inote.descdata >= end)
21827 {
21828 warn (_("Corrupt note: name size is too big: %lx\n"), inote.namesz);
21829 inote.descdata = inote.namedata;
21830 inote.namesz = 0;
21831 }
21832
21833 next = (Elf_External_Note *) (inote.descdata + align_power (inote.descsz, 2));
21834
21835 if ( ((char *) next > end)
21836 || ((char *) next < (char *) pnotes))
21837 {
21838 warn (_("corrupt descsz found in note at offset 0x%lx\n"),
21839 (unsigned long) ((char *) external - (char *) pnotes));
21840 warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
21841 inote.type, inote.namesz, inote.descsz);
21842 break;
21843 }
21844
21845 external = next;
21846
21847 /* Prevent out-of-bounds indexing. */
21848 if ( inote.namedata + inote.namesz > end
21849 || inote.namedata + inote.namesz < inote.namedata)
21850 {
21851 warn (_("corrupt namesz found in note at offset 0x%lx\n"),
21852 (unsigned long) ((char *) external - (char *) pnotes));
21853 warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
21854 inote.type, inote.namesz, inote.descsz);
21855 break;
21856 }
21857
21858 printf (" %s: ", get_v850_elf_note_type (inote.type));
21859
21860 if (! print_v850_note (& inote))
21861 {
21862 res = false;
21863 printf ("<corrupt sizes: namesz: %lx, descsz: %lx>\n",
21864 inote.namesz, inote.descsz);
21865 }
21866 }
21867
21868 free (pnotes);
21869
21870 return res;
21871 }
21872
21873 static bool
21874 process_note_sections (Filedata * filedata)
21875 {
21876 Elf_Internal_Shdr *section;
21877 unsigned long i;
21878 unsigned int n = 0;
21879 bool res = true;
21880
21881 for (i = 0, section = filedata->section_headers;
21882 i < filedata->file_header.e_shnum && section != NULL;
21883 i++, section++)
21884 {
21885 if (section->sh_type == SHT_NOTE)
21886 {
21887 if (! process_notes_at (filedata, section, section->sh_offset,
21888 section->sh_size, section->sh_addralign))
21889 res = false;
21890 n++;
21891 }
21892
21893 if (( filedata->file_header.e_machine == EM_V800
21894 || filedata->file_header.e_machine == EM_V850
21895 || filedata->file_header.e_machine == EM_CYGNUS_V850)
21896 && section->sh_type == SHT_RENESAS_INFO)
21897 {
21898 if (! process_v850_notes (filedata, section->sh_offset,
21899 section->sh_size))
21900 res = false;
21901 n++;
21902 }
21903 }
21904
21905 if (n == 0)
21906 /* Try processing NOTE segments instead. */
21907 return process_corefile_note_segments (filedata);
21908
21909 return res;
21910 }
21911
21912 static bool
21913 process_notes (Filedata * filedata)
21914 {
21915 /* If we have not been asked to display the notes then do nothing. */
21916 if (! do_notes)
21917 return true;
21918
21919 if (filedata->file_header.e_type != ET_CORE)
21920 return process_note_sections (filedata);
21921
21922 /* No program headers means no NOTE segment. */
21923 if (filedata->file_header.e_phnum > 0)
21924 return process_corefile_note_segments (filedata);
21925
21926 if (filedata->is_separate)
21927 printf (_("No notes found in linked file '%s'.\n"),
21928 filedata->file_name);
21929 else
21930 printf (_("No notes found file.\n"));
21931
21932 return true;
21933 }
21934
21935 static unsigned char *
21936 display_public_gnu_attributes (unsigned char * start,
21937 const unsigned char * const end)
21938 {
21939 printf (_(" Unknown GNU attribute: %s\n"), start);
21940
21941 start += strnlen ((char *) start, end - start);
21942 display_raw_attribute (start, end);
21943
21944 return (unsigned char *) end;
21945 }
21946
21947 static unsigned char *
21948 display_generic_attribute (unsigned char * start,
21949 unsigned int tag,
21950 const unsigned char * const end)
21951 {
21952 if (tag == 0)
21953 return (unsigned char *) end;
21954
21955 return display_tag_value (tag, start, end);
21956 }
21957
21958 static bool
21959 process_arch_specific (Filedata * filedata)
21960 {
21961 if (! do_arch)
21962 return true;
21963
21964 switch (filedata->file_header.e_machine)
21965 {
21966 case EM_ARC:
21967 case EM_ARC_COMPACT:
21968 case EM_ARC_COMPACT2:
21969 return process_attributes (filedata, "ARC", SHT_ARC_ATTRIBUTES,
21970 display_arc_attribute,
21971 display_generic_attribute);
21972 case EM_ARM:
21973 return process_attributes (filedata, "aeabi", SHT_ARM_ATTRIBUTES,
21974 display_arm_attribute,
21975 display_generic_attribute);
21976
21977 case EM_MIPS:
21978 case EM_MIPS_RS3_LE:
21979 return process_mips_specific (filedata);
21980
21981 case EM_MSP430:
21982 return process_attributes (filedata, "mspabi", SHT_MSP430_ATTRIBUTES,
21983 display_msp430_attribute,
21984 display_msp430_gnu_attribute);
21985
21986 case EM_RISCV:
21987 return process_attributes (filedata, "riscv", SHT_RISCV_ATTRIBUTES,
21988 display_riscv_attribute,
21989 display_generic_attribute);
21990
21991 case EM_NDS32:
21992 return process_nds32_specific (filedata);
21993
21994 case EM_68K:
21995 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
21996 display_m68k_gnu_attribute);
21997
21998 case EM_PPC:
21999 case EM_PPC64:
22000 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22001 display_power_gnu_attribute);
22002
22003 case EM_S390:
22004 case EM_S390_OLD:
22005 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22006 display_s390_gnu_attribute);
22007
22008 case EM_SPARC:
22009 case EM_SPARC32PLUS:
22010 case EM_SPARCV9:
22011 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22012 display_sparc_gnu_attribute);
22013
22014 case EM_TI_C6000:
22015 return process_attributes (filedata, "c6xabi", SHT_C6000_ATTRIBUTES,
22016 display_tic6x_attribute,
22017 display_generic_attribute);
22018
22019 case EM_CSKY:
22020 return process_attributes (filedata, "csky", SHT_CSKY_ATTRIBUTES,
22021 display_csky_attribute, NULL);
22022
22023 default:
22024 return process_attributes (filedata, "gnu", SHT_GNU_ATTRIBUTES,
22025 display_public_gnu_attributes,
22026 display_generic_attribute);
22027 }
22028 }
22029
22030 static bool
22031 get_file_header (Filedata * filedata)
22032 {
22033 /* Read in the identity array. */
22034 if (fread (filedata->file_header.e_ident, EI_NIDENT, 1, filedata->handle) != 1)
22035 return false;
22036
22037 /* Determine how to read the rest of the header. */
22038 switch (filedata->file_header.e_ident[EI_DATA])
22039 {
22040 default:
22041 case ELFDATANONE:
22042 case ELFDATA2LSB:
22043 byte_get = byte_get_little_endian;
22044 byte_put = byte_put_little_endian;
22045 break;
22046 case ELFDATA2MSB:
22047 byte_get = byte_get_big_endian;
22048 byte_put = byte_put_big_endian;
22049 break;
22050 }
22051
22052 /* For now we only support 32 bit and 64 bit ELF files. */
22053 is_32bit_elf = (filedata->file_header.e_ident[EI_CLASS] != ELFCLASS64);
22054
22055 /* Read in the rest of the header. */
22056 if (is_32bit_elf)
22057 {
22058 Elf32_External_Ehdr ehdr32;
22059
22060 if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, filedata->handle) != 1)
22061 return false;
22062
22063 filedata->file_header.e_type = BYTE_GET (ehdr32.e_type);
22064 filedata->file_header.e_machine = BYTE_GET (ehdr32.e_machine);
22065 filedata->file_header.e_version = BYTE_GET (ehdr32.e_version);
22066 filedata->file_header.e_entry = BYTE_GET (ehdr32.e_entry);
22067 filedata->file_header.e_phoff = BYTE_GET (ehdr32.e_phoff);
22068 filedata->file_header.e_shoff = BYTE_GET (ehdr32.e_shoff);
22069 filedata->file_header.e_flags = BYTE_GET (ehdr32.e_flags);
22070 filedata->file_header.e_ehsize = BYTE_GET (ehdr32.e_ehsize);
22071 filedata->file_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
22072 filedata->file_header.e_phnum = BYTE_GET (ehdr32.e_phnum);
22073 filedata->file_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
22074 filedata->file_header.e_shnum = BYTE_GET (ehdr32.e_shnum);
22075 filedata->file_header.e_shstrndx = BYTE_GET (ehdr32.e_shstrndx);
22076 }
22077 else
22078 {
22079 Elf64_External_Ehdr ehdr64;
22080
22081 if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, filedata->handle) != 1)
22082 return false;
22083
22084 filedata->file_header.e_type = BYTE_GET (ehdr64.e_type);
22085 filedata->file_header.e_machine = BYTE_GET (ehdr64.e_machine);
22086 filedata->file_header.e_version = BYTE_GET (ehdr64.e_version);
22087 filedata->file_header.e_entry = BYTE_GET (ehdr64.e_entry);
22088 filedata->file_header.e_phoff = BYTE_GET (ehdr64.e_phoff);
22089 filedata->file_header.e_shoff = BYTE_GET (ehdr64.e_shoff);
22090 filedata->file_header.e_flags = BYTE_GET (ehdr64.e_flags);
22091 filedata->file_header.e_ehsize = BYTE_GET (ehdr64.e_ehsize);
22092 filedata->file_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
22093 filedata->file_header.e_phnum = BYTE_GET (ehdr64.e_phnum);
22094 filedata->file_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
22095 filedata->file_header.e_shnum = BYTE_GET (ehdr64.e_shnum);
22096 filedata->file_header.e_shstrndx = BYTE_GET (ehdr64.e_shstrndx);
22097 }
22098
22099 return true;
22100 }
22101
22102 static void
22103 free_filedata (Filedata *filedata)
22104 {
22105 free (filedata->program_interpreter);
22106 free (filedata->program_headers);
22107 free (filedata->section_headers);
22108 free (filedata->string_table);
22109 free (filedata->dump.dump_sects);
22110 free (filedata->dynamic_strings);
22111 free (filedata->dynamic_symbols);
22112 free (filedata->dynamic_syminfo);
22113 free (filedata->dynamic_section);
22114
22115 while (filedata->symtab_shndx_list != NULL)
22116 {
22117 elf_section_list *next = filedata->symtab_shndx_list->next;
22118 free (filedata->symtab_shndx_list);
22119 filedata->symtab_shndx_list = next;
22120 }
22121
22122 free (filedata->section_headers_groups);
22123
22124 if (filedata->section_groups)
22125 {
22126 size_t i;
22127 struct group_list * g;
22128 struct group_list * next;
22129
22130 for (i = 0; i < filedata->group_count; i++)
22131 {
22132 for (g = filedata->section_groups [i].root; g != NULL; g = next)
22133 {
22134 next = g->next;
22135 free (g);
22136 }
22137 }
22138
22139 free (filedata->section_groups);
22140 }
22141 memset (&filedata->section_headers, 0,
22142 sizeof (Filedata) - offsetof (Filedata, section_headers));
22143 }
22144
22145 static void
22146 close_file (Filedata * filedata)
22147 {
22148 if (filedata)
22149 {
22150 if (filedata->handle)
22151 fclose (filedata->handle);
22152 free (filedata);
22153 }
22154 }
22155
22156 void
22157 close_debug_file (void * data)
22158 {
22159 free_filedata ((Filedata *) data);
22160 close_file ((Filedata *) data);
22161 }
22162
22163 static Filedata *
22164 open_file (const char * pathname, bool is_separate)
22165 {
22166 struct stat statbuf;
22167 Filedata * filedata = NULL;
22168
22169 if (stat (pathname, & statbuf) < 0
22170 || ! S_ISREG (statbuf.st_mode))
22171 goto fail;
22172
22173 filedata = calloc (1, sizeof * filedata);
22174 if (filedata == NULL)
22175 goto fail;
22176
22177 filedata->handle = fopen (pathname, "rb");
22178 if (filedata->handle == NULL)
22179 goto fail;
22180
22181 filedata->file_size = statbuf.st_size;
22182 filedata->file_name = pathname;
22183 filedata->is_separate = is_separate;
22184
22185 if (! get_file_header (filedata))
22186 goto fail;
22187
22188 if (!get_section_headers (filedata, false))
22189 goto fail;
22190
22191 return filedata;
22192
22193 fail:
22194 if (filedata)
22195 {
22196 if (filedata->handle)
22197 fclose (filedata->handle);
22198 free (filedata);
22199 }
22200 return NULL;
22201 }
22202
22203 void *
22204 open_debug_file (const char * pathname)
22205 {
22206 return open_file (pathname, true);
22207 }
22208
22209 static void
22210 initialise_dump_sects (Filedata * filedata)
22211 {
22212 /* Initialise the dump_sects array from the cmdline_dump_sects array.
22213 Note we do this even if cmdline_dump_sects is empty because we
22214 must make sure that the dump_sets array is zeroed out before each
22215 object file is processed. */
22216 if (filedata->dump.num_dump_sects > cmdline.num_dump_sects)
22217 memset (filedata->dump.dump_sects, 0,
22218 filedata->dump.num_dump_sects * sizeof (*filedata->dump.dump_sects));
22219
22220 if (cmdline.num_dump_sects > 0)
22221 {
22222 if (filedata->dump.num_dump_sects == 0)
22223 /* A sneaky way of allocating the dump_sects array. */
22224 request_dump_bynumber (&filedata->dump, cmdline.num_dump_sects, 0);
22225
22226 assert (filedata->dump.num_dump_sects >= cmdline.num_dump_sects);
22227 memcpy (filedata->dump.dump_sects, cmdline.dump_sects,
22228 cmdline.num_dump_sects * sizeof (*filedata->dump.dump_sects));
22229 }
22230 }
22231
22232 static bool
22233 might_need_separate_debug_info (Filedata * filedata)
22234 {
22235 /* Debuginfo files do not need further separate file loading. */
22236 if (filedata->file_header.e_shstrndx == SHN_UNDEF)
22237 return false;
22238
22239 /* Since do_follow_links might be enabled by default, only treat it as an
22240 indication that separate files should be loaded if setting it was a
22241 deliberate user action. */
22242 if (DEFAULT_FOR_FOLLOW_LINKS == 0 && do_follow_links)
22243 return true;
22244
22245 if (process_links || do_syms || do_unwind
22246 || dump_any_debugging || do_dump || do_debugging)
22247 return true;
22248
22249 return false;
22250 }
22251
22252 /* Process one ELF object file according to the command line options.
22253 This file may actually be stored in an archive. The file is
22254 positioned at the start of the ELF object. Returns TRUE if no
22255 problems were encountered, FALSE otherwise. */
22256
22257 static bool
22258 process_object (Filedata * filedata)
22259 {
22260 bool have_separate_files;
22261 unsigned int i;
22262 bool res;
22263
22264 if (! get_file_header (filedata))
22265 {
22266 error (_("%s: Failed to read file header\n"), filedata->file_name);
22267 return false;
22268 }
22269
22270 /* Initialise per file variables. */
22271 for (i = ARRAY_SIZE (filedata->version_info); i--;)
22272 filedata->version_info[i] = 0;
22273
22274 for (i = ARRAY_SIZE (filedata->dynamic_info); i--;)
22275 filedata->dynamic_info[i] = 0;
22276 filedata->dynamic_info_DT_GNU_HASH = 0;
22277 filedata->dynamic_info_DT_MIPS_XHASH = 0;
22278
22279 /* Process the file. */
22280 if (show_name)
22281 printf (_("\nFile: %s\n"), filedata->file_name);
22282
22283 initialise_dump_sects (filedata);
22284
22285 /* There may be some extensions in the first section header. Don't
22286 bomb if we can't read it. */
22287 get_section_headers (filedata, true);
22288
22289 if (! process_file_header (filedata))
22290 {
22291 res = false;
22292 goto out;
22293 }
22294
22295 /* Throw away the single section header read above, so that we
22296 re-read the entire set. */
22297 free (filedata->section_headers);
22298 filedata->section_headers = NULL;
22299
22300 if (! process_section_headers (filedata))
22301 {
22302 /* Without loaded section headers we cannot process lots of things. */
22303 do_unwind = do_version = do_dump = do_arch = false;
22304
22305 if (! do_using_dynamic)
22306 do_syms = do_dyn_syms = do_reloc = false;
22307 }
22308
22309 if (! process_section_groups (filedata))
22310 /* Without loaded section groups we cannot process unwind. */
22311 do_unwind = false;
22312
22313 process_program_headers (filedata);
22314
22315 res = process_dynamic_section (filedata);
22316
22317 if (! process_relocs (filedata))
22318 res = false;
22319
22320 if (! process_unwind (filedata))
22321 res = false;
22322
22323 if (! process_symbol_table (filedata))
22324 res = false;
22325
22326 if (! process_lto_symbol_tables (filedata))
22327 res = false;
22328
22329 if (! process_syminfo (filedata))
22330 res = false;
22331
22332 if (! process_version_sections (filedata))
22333 res = false;
22334
22335 if (might_need_separate_debug_info (filedata))
22336 have_separate_files = load_separate_debug_files (filedata, filedata->file_name);
22337 else
22338 have_separate_files = false;
22339
22340 if (! process_section_contents (filedata))
22341 res = false;
22342
22343 if (have_separate_files)
22344 {
22345 separate_info * d;
22346
22347 for (d = first_separate_info; d != NULL; d = d->next)
22348 {
22349 initialise_dump_sects (d->handle);
22350
22351 if (process_links && ! process_file_header (d->handle))
22352 res = false;
22353 else if (! process_section_headers (d->handle))
22354 res = false;
22355 else if (! process_section_contents (d->handle))
22356 res = false;
22357 else if (process_links)
22358 {
22359 if (! process_section_groups (d->handle))
22360 res = false;
22361 process_program_headers (d->handle);
22362 if (! process_dynamic_section (d->handle))
22363 res = false;
22364 if (! process_relocs (d->handle))
22365 res = false;
22366 if (! process_unwind (d->handle))
22367 res = false;
22368 if (! process_symbol_table (d->handle))
22369 res = false;
22370 if (! process_lto_symbol_tables (d->handle))
22371 res = false;
22372 if (! process_syminfo (d->handle))
22373 res = false;
22374 if (! process_version_sections (d->handle))
22375 res = false;
22376 if (! process_notes (d->handle))
22377 res = false;
22378 }
22379 }
22380
22381 /* The file handles are closed by the call to free_debug_memory() below. */
22382 }
22383
22384 if (! process_notes (filedata))
22385 res = false;
22386
22387 if (! process_gnu_liblist (filedata))
22388 res = false;
22389
22390 if (! process_arch_specific (filedata))
22391 res = false;
22392
22393 out:
22394 free_filedata (filedata);
22395
22396 free_debug_memory ();
22397
22398 return res;
22399 }
22400
22401 /* Process an ELF archive.
22402 On entry the file is positioned just after the ARMAG string.
22403 Returns TRUE upon success, FALSE otherwise. */
22404
22405 static bool
22406 process_archive (Filedata * filedata, bool is_thin_archive)
22407 {
22408 struct archive_info arch;
22409 struct archive_info nested_arch;
22410 size_t got;
22411 bool ret = true;
22412
22413 show_name = true;
22414
22415 /* The ARCH structure is used to hold information about this archive. */
22416 arch.file_name = NULL;
22417 arch.file = NULL;
22418 arch.index_array = NULL;
22419 arch.sym_table = NULL;
22420 arch.longnames = NULL;
22421
22422 /* The NESTED_ARCH structure is used as a single-item cache of information
22423 about a nested archive (when members of a thin archive reside within
22424 another regular archive file). */
22425 nested_arch.file_name = NULL;
22426 nested_arch.file = NULL;
22427 nested_arch.index_array = NULL;
22428 nested_arch.sym_table = NULL;
22429 nested_arch.longnames = NULL;
22430
22431 if (setup_archive (&arch, filedata->file_name, filedata->handle,
22432 filedata->file_size, is_thin_archive,
22433 do_archive_index) != 0)
22434 {
22435 ret = false;
22436 goto out;
22437 }
22438
22439 if (do_archive_index)
22440 {
22441 if (arch.sym_table == NULL)
22442 error (_("%s: unable to dump the index as none was found\n"),
22443 filedata->file_name);
22444 else
22445 {
22446 unsigned long i, l;
22447 unsigned long current_pos;
22448
22449 printf (_("Index of archive %s: (%lu entries, 0x%lx bytes "
22450 "in the symbol table)\n"),
22451 filedata->file_name, (unsigned long) arch.index_num,
22452 arch.sym_size);
22453
22454 current_pos = ftell (filedata->handle);
22455
22456 for (i = l = 0; i < arch.index_num; i++)
22457 {
22458 if (i == 0
22459 || (i > 0 && arch.index_array[i] != arch.index_array[i - 1]))
22460 {
22461 char * member_name
22462 = get_archive_member_name_at (&arch, arch.index_array[i],
22463 &nested_arch);
22464
22465 if (member_name != NULL)
22466 {
22467 char * qualified_name
22468 = make_qualified_name (&arch, &nested_arch,
22469 member_name);
22470
22471 if (qualified_name != NULL)
22472 {
22473 printf (_("Contents of binary %s at offset "),
22474 qualified_name);
22475 (void) print_vma (arch.index_array[i], PREFIX_HEX);
22476 putchar ('\n');
22477 free (qualified_name);
22478 }
22479 free (member_name);
22480 }
22481 }
22482
22483 if (l >= arch.sym_size)
22484 {
22485 error (_("%s: end of the symbol table reached "
22486 "before the end of the index\n"),
22487 filedata->file_name);
22488 ret = false;
22489 break;
22490 }
22491 /* PR 17531: file: 0b6630b2. */
22492 printf ("\t%.*s\n",
22493 (int) (arch.sym_size - l), arch.sym_table + l);
22494 l += strnlen (arch.sym_table + l, arch.sym_size - l) + 1;
22495 }
22496
22497 if (arch.uses_64bit_indices)
22498 l = (l + 7) & ~ 7;
22499 else
22500 l += l & 1;
22501
22502 if (l < arch.sym_size)
22503 {
22504 error (ngettext ("%s: %ld byte remains in the symbol table, "
22505 "but without corresponding entries in "
22506 "the index table\n",
22507 "%s: %ld bytes remain in the symbol table, "
22508 "but without corresponding entries in "
22509 "the index table\n",
22510 arch.sym_size - l),
22511 filedata->file_name, arch.sym_size - l);
22512 ret = false;
22513 }
22514
22515 if (fseek (filedata->handle, current_pos, SEEK_SET) != 0)
22516 {
22517 error (_("%s: failed to seek back to start of object files "
22518 "in the archive\n"),
22519 filedata->file_name);
22520 ret = false;
22521 goto out;
22522 }
22523 }
22524
22525 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
22526 && !do_segments && !do_header && !do_dump && !do_version
22527 && !do_histogram && !do_debugging && !do_arch && !do_notes
22528 && !do_section_groups && !do_dyn_syms)
22529 {
22530 ret = true; /* Archive index only. */
22531 goto out;
22532 }
22533 }
22534
22535 while (1)
22536 {
22537 char * name;
22538 size_t namelen;
22539 char * qualified_name;
22540
22541 /* Read the next archive header. */
22542 if (fseek (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0)
22543 {
22544 error (_("%s: failed to seek to next archive header\n"),
22545 arch.file_name);
22546 ret = false;
22547 break;
22548 }
22549 got = fread (&arch.arhdr, 1, sizeof arch.arhdr, filedata->handle);
22550 if (got != sizeof arch.arhdr)
22551 {
22552 if (got == 0)
22553 break;
22554 /* PR 24049 - we cannot use filedata->file_name as this will
22555 have already been freed. */
22556 error (_("%s: failed to read archive header\n"), arch.file_name);
22557
22558 ret = false;
22559 break;
22560 }
22561 if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
22562 {
22563 error (_("%s: did not find a valid archive header\n"),
22564 arch.file_name);
22565 ret = false;
22566 break;
22567 }
22568
22569 arch.next_arhdr_offset += sizeof arch.arhdr;
22570
22571 filedata->archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
22572
22573 name = get_archive_member_name (&arch, &nested_arch);
22574 if (name == NULL)
22575 {
22576 error (_("%s: bad archive file name\n"), arch.file_name);
22577 ret = false;
22578 break;
22579 }
22580 namelen = strlen (name);
22581
22582 qualified_name = make_qualified_name (&arch, &nested_arch, name);
22583 if (qualified_name == NULL)
22584 {
22585 error (_("%s: bad archive file name\n"), arch.file_name);
22586 free (name);
22587 ret = false;
22588 break;
22589 }
22590
22591 if (is_thin_archive && arch.nested_member_origin == 0)
22592 {
22593 /* This is a proxy for an external member of a thin archive. */
22594 Filedata * member_filedata;
22595 char * member_file_name = adjust_relative_path
22596 (filedata->file_name, name, namelen);
22597
22598 free (name);
22599 if (member_file_name == NULL)
22600 {
22601 free (qualified_name);
22602 ret = false;
22603 break;
22604 }
22605
22606 member_filedata = open_file (member_file_name, false);
22607 if (member_filedata == NULL)
22608 {
22609 error (_("Input file '%s' is not readable.\n"), member_file_name);
22610 free (member_file_name);
22611 free (qualified_name);
22612 ret = false;
22613 break;
22614 }
22615
22616 filedata->archive_file_offset = arch.nested_member_origin;
22617 member_filedata->file_name = qualified_name;
22618
22619 /* The call to process_object() expects the file to be at the beginning. */
22620 rewind (member_filedata->handle);
22621
22622 if (! process_object (member_filedata))
22623 ret = false;
22624
22625 close_file (member_filedata);
22626 free (member_file_name);
22627 }
22628 else if (is_thin_archive)
22629 {
22630 Filedata thin_filedata;
22631
22632 memset (&thin_filedata, 0, sizeof (thin_filedata));
22633
22634 /* PR 15140: Allow for corrupt thin archives. */
22635 if (nested_arch.file == NULL)
22636 {
22637 error (_("%s: contains corrupt thin archive: %s\n"),
22638 qualified_name, name);
22639 free (qualified_name);
22640 free (name);
22641 ret = false;
22642 break;
22643 }
22644 free (name);
22645
22646 /* This is a proxy for a member of a nested archive. */
22647 filedata->archive_file_offset
22648 = arch.nested_member_origin + sizeof arch.arhdr;
22649
22650 /* The nested archive file will have been opened and setup by
22651 get_archive_member_name. */
22652 if (fseek (nested_arch.file, filedata->archive_file_offset,
22653 SEEK_SET) != 0)
22654 {
22655 error (_("%s: failed to seek to archive member.\n"),
22656 nested_arch.file_name);
22657 free (qualified_name);
22658 ret = false;
22659 break;
22660 }
22661
22662 thin_filedata.handle = nested_arch.file;
22663 thin_filedata.file_name = qualified_name;
22664
22665 if (! process_object (& thin_filedata))
22666 ret = false;
22667 }
22668 else
22669 {
22670 free (name);
22671 filedata->archive_file_offset = arch.next_arhdr_offset;
22672 filedata->file_name = qualified_name;
22673 if (! process_object (filedata))
22674 ret = false;
22675 arch.next_arhdr_offset += (filedata->archive_file_size + 1) & -2;
22676 /* Stop looping with "negative" archive_file_size. */
22677 if (arch.next_arhdr_offset < filedata->archive_file_size)
22678 arch.next_arhdr_offset = -1ul;
22679 }
22680
22681 free (qualified_name);
22682 }
22683
22684 out:
22685 if (nested_arch.file != NULL)
22686 fclose (nested_arch.file);
22687 release_archive (&nested_arch);
22688 release_archive (&arch);
22689
22690 return ret;
22691 }
22692
22693 static bool
22694 process_file (char * file_name)
22695 {
22696 Filedata * filedata = NULL;
22697 struct stat statbuf;
22698 char armag[SARMAG];
22699 bool ret = true;
22700
22701 if (stat (file_name, &statbuf) < 0)
22702 {
22703 if (errno == ENOENT)
22704 error (_("'%s': No such file\n"), file_name);
22705 else
22706 error (_("Could not locate '%s'. System error message: %s\n"),
22707 file_name, strerror (errno));
22708 return false;
22709 }
22710
22711 if (! S_ISREG (statbuf.st_mode))
22712 {
22713 error (_("'%s' is not an ordinary file\n"), file_name);
22714 return false;
22715 }
22716
22717 filedata = calloc (1, sizeof * filedata);
22718 if (filedata == NULL)
22719 {
22720 error (_("Out of memory allocating file data structure\n"));
22721 return false;
22722 }
22723
22724 filedata->file_name = file_name;
22725 filedata->handle = fopen (file_name, "rb");
22726 if (filedata->handle == NULL)
22727 {
22728 error (_("Input file '%s' is not readable.\n"), file_name);
22729 free (filedata);
22730 return false;
22731 }
22732
22733 if (fread (armag, SARMAG, 1, filedata->handle) != 1)
22734 {
22735 error (_("%s: Failed to read file's magic number\n"), file_name);
22736 fclose (filedata->handle);
22737 free (filedata);
22738 return false;
22739 }
22740
22741 filedata->file_size = statbuf.st_size;
22742 filedata->is_separate = false;
22743
22744 if (memcmp (armag, ARMAG, SARMAG) == 0)
22745 {
22746 if (! process_archive (filedata, false))
22747 ret = false;
22748 }
22749 else if (memcmp (armag, ARMAGT, SARMAG) == 0)
22750 {
22751 if ( ! process_archive (filedata, true))
22752 ret = false;
22753 }
22754 else
22755 {
22756 if (do_archive_index && !check_all)
22757 error (_("File %s is not an archive so its index cannot be displayed.\n"),
22758 file_name);
22759
22760 rewind (filedata->handle);
22761 filedata->archive_file_size = filedata->archive_file_offset = 0;
22762
22763 if (! process_object (filedata))
22764 ret = false;
22765 }
22766
22767 fclose (filedata->handle);
22768 free (filedata->section_headers);
22769 free (filedata->program_headers);
22770 free (filedata->string_table);
22771 free (filedata->dump.dump_sects);
22772 free (filedata);
22773
22774 free (ba_cache.strtab);
22775 ba_cache.strtab = NULL;
22776 free (ba_cache.symtab);
22777 ba_cache.symtab = NULL;
22778 ba_cache.filedata = NULL;
22779
22780 return ret;
22781 }
22782
22783 #ifdef SUPPORT_DISASSEMBLY
22784 /* Needed by the i386 disassembler. For extra credit, someone could
22785 fix this so that we insert symbolic addresses here, esp for GOT/PLT
22786 symbols. */
22787
22788 void
22789 print_address (unsigned int addr, FILE * outfile)
22790 {
22791 fprintf (outfile,"0x%8.8x", addr);
22792 }
22793
22794 /* Needed by the i386 disassembler. */
22795
22796 void
22797 db_task_printsym (unsigned int addr)
22798 {
22799 print_address (addr, stderr);
22800 }
22801 #endif
22802
22803 int
22804 main (int argc, char ** argv)
22805 {
22806 int err;
22807
22808 #ifdef HAVE_LC_MESSAGES
22809 setlocale (LC_MESSAGES, "");
22810 #endif
22811 setlocale (LC_CTYPE, "");
22812 bindtextdomain (PACKAGE, LOCALEDIR);
22813 textdomain (PACKAGE);
22814
22815 expandargv (&argc, &argv);
22816
22817 parse_args (& cmdline, argc, argv);
22818
22819 if (optind < (argc - 1))
22820 /* When displaying information for more than one file,
22821 prefix the information with the file name. */
22822 show_name = true;
22823 else if (optind >= argc)
22824 {
22825 /* Ensure that the warning is always displayed. */
22826 do_checks = true;
22827
22828 warn (_("Nothing to do.\n"));
22829 usage (stderr);
22830 }
22831
22832 err = false;
22833 while (optind < argc)
22834 if (! process_file (argv[optind++]))
22835 err = true;
22836
22837 free (cmdline.dump_sects);
22838
22839 free (dump_ctf_symtab_name);
22840 free (dump_ctf_strtab_name);
22841 free (dump_ctf_parent_name);
22842
22843 return err ? EXIT_FAILURE : EXIT_SUCCESS;
22844 }