objdump: Add configure time option to enable colored disassembly output by default.
[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 #ifdef HAVE_ZSTD
48 #include <zstd.h>
49 #endif
50 #include <wchar.h>
51
52 #if defined HAVE_MSGPACK
53 #include <msgpack.h>
54 #endif
55
56 /* Define BFD64 here, even if our default architecture is 32 bit ELF
57 as this will allow us to read in and parse 64bit and 32bit ELF files. */
58 #define BFD64
59
60 #include "bfd.h"
61 #include "bucomm.h"
62 #include "elfcomm.h"
63 #include "demanguse.h"
64 #include "dwarf.h"
65 #include "ctf-api.h"
66 #include "demangle.h"
67
68 #include "elf/common.h"
69 #include "elf/external.h"
70 #include "elf/internal.h"
71
72
73 /* Included here, before RELOC_MACROS_GEN_FUNC is defined, so that
74 we can obtain the H8 reloc numbers. We need these for the
75 get_reloc_size() function. We include h8.h again after defining
76 RELOC_MACROS_GEN_FUNC so that we get the naming function as well. */
77
78 #include "elf/h8.h"
79 #undef _ELF_H8_H
80
81 /* Undo the effects of #including reloc-macros.h. */
82
83 #undef START_RELOC_NUMBERS
84 #undef RELOC_NUMBER
85 #undef FAKE_RELOC
86 #undef EMPTY_RELOC
87 #undef END_RELOC_NUMBERS
88 #undef _RELOC_MACROS_H
89
90 /* The following headers use the elf/reloc-macros.h file to
91 automatically generate relocation recognition functions
92 such as elf_mips_reloc_type() */
93
94 #define RELOC_MACROS_GEN_FUNC
95
96 #include "elf/aarch64.h"
97 #include "elf/alpha.h"
98 #include "elf/amdgpu.h"
99 #include "elf/arc.h"
100 #include "elf/arm.h"
101 #include "elf/avr.h"
102 #include "elf/bfin.h"
103 #include "elf/cr16.h"
104 #include "elf/cris.h"
105 #include "elf/crx.h"
106 #include "elf/csky.h"
107 #include "elf/d10v.h"
108 #include "elf/d30v.h"
109 #include "elf/dlx.h"
110 #include "elf/bpf.h"
111 #include "elf/epiphany.h"
112 #include "elf/fr30.h"
113 #include "elf/frv.h"
114 #include "elf/ft32.h"
115 #include "elf/h8.h"
116 #include "elf/hppa.h"
117 #include "elf/i386.h"
118 #include "elf/i370.h"
119 #include "elf/i860.h"
120 #include "elf/i960.h"
121 #include "elf/ia64.h"
122 #include "elf/ip2k.h"
123 #include "elf/lm32.h"
124 #include "elf/iq2000.h"
125 #include "elf/m32c.h"
126 #include "elf/m32r.h"
127 #include "elf/m68k.h"
128 #include "elf/m68hc11.h"
129 #include "elf/s12z.h"
130 #include "elf/mcore.h"
131 #include "elf/mep.h"
132 #include "elf/metag.h"
133 #include "elf/microblaze.h"
134 #include "elf/mips.h"
135 #include "elf/mmix.h"
136 #include "elf/mn10200.h"
137 #include "elf/mn10300.h"
138 #include "elf/moxie.h"
139 #include "elf/mt.h"
140 #include "elf/msp430.h"
141 #include "elf/nds32.h"
142 #include "elf/nfp.h"
143 #include "elf/nios2.h"
144 #include "elf/or1k.h"
145 #include "elf/pj.h"
146 #include "elf/ppc.h"
147 #include "elf/ppc64.h"
148 #include "elf/pru.h"
149 #include "elf/riscv.h"
150 #include "elf/rl78.h"
151 #include "elf/rx.h"
152 #include "elf/s390.h"
153 #include "elf/score.h"
154 #include "elf/sh.h"
155 #include "elf/sparc.h"
156 #include "elf/spu.h"
157 #include "elf/tic6x.h"
158 #include "elf/tilegx.h"
159 #include "elf/tilepro.h"
160 #include "elf/v850.h"
161 #include "elf/vax.h"
162 #include "elf/visium.h"
163 #include "elf/wasm32.h"
164 #include "elf/x86-64.h"
165 #include "elf/xgate.h"
166 #include "elf/xstormy16.h"
167 #include "elf/xtensa.h"
168 #include "elf/z80.h"
169 #include "elf/loongarch.h"
170
171 #include "getopt.h"
172 #include "libiberty.h"
173 #include "safe-ctype.h"
174 #include "filenames.h"
175
176 #ifndef offsetof
177 #define offsetof(TYPE, MEMBER) ((size_t) &(((TYPE *) 0)->MEMBER))
178 #endif
179
180 typedef struct elf_section_list
181 {
182 Elf_Internal_Shdr * hdr;
183 struct elf_section_list * next;
184 } elf_section_list;
185
186 /* Flag bits indicating particular types of dump. */
187 #define HEX_DUMP (1 << 0) /* The -x command line switch. */
188 #define DISASS_DUMP (1 << 1) /* The -i command line switch. */
189 #define DEBUG_DUMP (1 << 2) /* The -w command line switch. */
190 #define STRING_DUMP (1 << 3) /* The -p command line switch. */
191 #define RELOC_DUMP (1 << 4) /* The -R command line switch. */
192 #define CTF_DUMP (1 << 5) /* The --ctf command line switch. */
193
194 typedef unsigned char dump_type;
195
196 /* A linked list of the section names for which dumps were requested. */
197 struct dump_list_entry
198 {
199 char * name;
200 dump_type type;
201 struct dump_list_entry * next;
202 };
203
204 /* A dynamic array of flags indicating for which sections a dump
205 has been requested via command line switches. */
206 struct dump_data
207 {
208 dump_type * dump_sects;
209 unsigned int num_dump_sects;
210 };
211
212 static struct dump_data cmdline;
213
214 static struct dump_list_entry * dump_sects_byname;
215
216 char * program_name = "readelf";
217
218 static bool show_name = false;
219 static bool do_dynamic = false;
220 static bool do_syms = false;
221 static bool do_dyn_syms = false;
222 static bool do_lto_syms = false;
223 static bool do_reloc = false;
224 static bool do_sections = false;
225 static bool do_section_groups = false;
226 static bool do_section_details = false;
227 static bool do_segments = false;
228 static bool do_unwind = false;
229 static bool do_using_dynamic = false;
230 static bool do_header = false;
231 static bool do_dump = false;
232 static bool do_version = false;
233 static bool do_histogram = false;
234 static bool do_debugging = false;
235 static bool do_ctf = false;
236 static bool do_arch = false;
237 static bool do_notes = false;
238 static bool do_archive_index = false;
239 static bool check_all = false;
240 static bool is_32bit_elf = false;
241 static bool decompress_dumps = false;
242 static bool do_not_show_symbol_truncation = false;
243 static bool do_demangle = false; /* Pretty print C++ symbol names. */
244 static bool process_links = false;
245 static bool dump_any_debugging = false;
246 static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
247 static int sym_base = 0;
248
249 static char *dump_ctf_parent_name;
250 static char *dump_ctf_symtab_name;
251 static char *dump_ctf_strtab_name;
252
253 struct group_list
254 {
255 struct group_list * next;
256 unsigned int section_index;
257 };
258
259 struct group
260 {
261 struct group_list * root;
262 unsigned int group_index;
263 };
264
265 typedef struct filedata
266 {
267 const char * file_name;
268 bool is_separate;
269 FILE * handle;
270 uint64_t file_size;
271 Elf_Internal_Ehdr file_header;
272 unsigned long archive_file_offset;
273 unsigned long archive_file_size;
274 /* Everything below this point is cleared out by free_filedata. */
275 Elf_Internal_Shdr * section_headers;
276 Elf_Internal_Phdr * program_headers;
277 char * string_table;
278 unsigned long string_table_length;
279 unsigned long dynamic_addr;
280 uint64_t dynamic_size;
281 size_t dynamic_nent;
282 Elf_Internal_Dyn * dynamic_section;
283 Elf_Internal_Shdr * dynamic_strtab_section;
284 char * dynamic_strings;
285 unsigned long dynamic_strings_length;
286 Elf_Internal_Shdr * dynamic_symtab_section;
287 unsigned long num_dynamic_syms;
288 Elf_Internal_Sym * dynamic_symbols;
289 uint64_t version_info[16];
290 unsigned int dynamic_syminfo_nent;
291 Elf_Internal_Syminfo * dynamic_syminfo;
292 unsigned long dynamic_syminfo_offset;
293 uint64_t nbuckets;
294 uint64_t nchains;
295 uint64_t * buckets;
296 uint64_t * chains;
297 uint64_t ngnubuckets;
298 uint64_t ngnuchains;
299 uint64_t * gnubuckets;
300 uint64_t * gnuchains;
301 uint64_t * mipsxlat;
302 uint64_t gnusymidx;
303 char * program_interpreter;
304 uint64_t dynamic_info[DT_ENCODING];
305 uint64_t dynamic_info_DT_GNU_HASH;
306 uint64_t dynamic_info_DT_MIPS_XHASH;
307 elf_section_list * symtab_shndx_list;
308 size_t group_count;
309 struct group * section_groups;
310 struct group ** section_headers_groups;
311 /* A dynamic array of flags indicating for which sections a dump of
312 some kind has been requested. It is reset on a per-object file
313 basis and then initialised from the cmdline_dump_sects array,
314 the results of interpreting the -w switch, and the
315 dump_sects_byname list. */
316 struct dump_data dump;
317 } Filedata;
318
319 /* How to print a vma value. */
320 typedef enum print_mode
321 {
322 HEX,
323 HEX_5,
324 DEC,
325 DEC_5,
326 UNSIGNED,
327 UNSIGNED_5,
328 PREFIX_HEX,
329 PREFIX_HEX_5,
330 FULL_HEX,
331 LONG_HEX,
332 OCTAL,
333 OCTAL_5
334 }
335 print_mode;
336
337 typedef enum unicode_display_type
338 {
339 unicode_default = 0,
340 unicode_locale,
341 unicode_escape,
342 unicode_hex,
343 unicode_highlight,
344 unicode_invalid
345 } unicode_display_type;
346
347 static unicode_display_type unicode_display = unicode_default;
348
349 typedef enum
350 {
351 reltype_unknown,
352 reltype_rel,
353 reltype_rela,
354 reltype_relr
355 } relocation_type;
356
357 /* Versioned symbol info. */
358 enum versioned_symbol_info
359 {
360 symbol_undefined,
361 symbol_hidden,
362 symbol_public
363 };
364
365 static const char * get_symbol_version_string
366 (Filedata *, bool, const char *, unsigned long, unsigned,
367 Elf_Internal_Sym *, enum versioned_symbol_info *, unsigned short *);
368
369 #define UNKNOWN -1
370
371 static inline const char *
372 section_name (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
373 {
374 return filedata->string_table + hdr->sh_name;
375 }
376
377 static inline bool
378 section_name_valid (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
379 {
380 return (hdr != NULL
381 && filedata->string_table != NULL
382 && hdr->sh_name < filedata->string_table_length);
383 }
384
385 static inline const char *
386 section_name_print (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
387 {
388 if (hdr == NULL)
389 return _("<none>");
390 if (filedata->string_table == NULL)
391 return _("<no-strings>");
392 if (hdr->sh_name >= filedata->string_table_length)
393 return _("<corrupt>");
394 return section_name (filedata, hdr);
395 }
396
397 #define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */
398
399 static inline bool
400 valid_symbol_name (const char *strtab, size_t strtab_size, uint64_t offset)
401 {
402 return strtab != NULL && offset < strtab_size;
403 }
404
405 static inline bool
406 valid_dynamic_name (const Filedata *filedata, uint64_t offset)
407 {
408 return valid_symbol_name (filedata->dynamic_strings,
409 filedata->dynamic_strings_length, offset);
410 }
411
412 /* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
413 already been called and verified that the string exists. */
414 static inline const char *
415 get_dynamic_name (const Filedata *filedata, size_t offset)
416 {
417 return filedata->dynamic_strings + offset;
418 }
419
420 #define REMOVE_ARCH_BITS(ADDR) \
421 do \
422 { \
423 if (filedata->file_header.e_machine == EM_ARM) \
424 (ADDR) &= ~1; \
425 } \
426 while (0)
427
428 /* Get the correct GNU hash section name. */
429 #define GNU_HASH_SECTION_NAME(filedata) \
430 filedata->dynamic_info_DT_MIPS_XHASH ? ".MIPS.xhash" : ".gnu.hash"
431 \f
432 /* Retrieve NMEMB structures, each SIZE bytes long from FILEDATA starting at
433 OFFSET + the offset of the current archive member, if we are examining an
434 archive. Put the retrieved data into VAR, if it is not NULL. Otherwise
435 allocate a buffer using malloc and fill that. In either case return the
436 pointer to the start of the retrieved data or NULL if something went wrong.
437 If something does go wrong and REASON is not NULL then emit an error
438 message using REASON as part of the context. */
439
440 static void *
441 get_data (void *var,
442 Filedata *filedata,
443 unsigned long offset,
444 uint64_t size,
445 uint64_t nmemb,
446 const char *reason)
447 {
448 void * mvar;
449 uint64_t amt = size * nmemb;
450
451 if (size == 0 || nmemb == 0)
452 return NULL;
453
454 /* If size_t is smaller than uint64_t, eg because you are building
455 on a 32-bit host, then make sure that when the sizes are cast to
456 size_t no information is lost. */
457 if ((size_t) size != size
458 || (size_t) nmemb != nmemb
459 || (size_t) amt != amt
460 || amt / size != nmemb
461 || (size_t) amt + 1 == 0)
462 {
463 if (reason)
464 error (_("Size overflow prevents reading %" PRIu64
465 " elements of size %" PRIu64 " for %s\n"),
466 nmemb, size, reason);
467 return NULL;
468 }
469
470 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
471 attempting to allocate memory when the read is bound to fail. */
472 if (filedata->archive_file_offset > filedata->file_size
473 || offset > filedata->file_size - filedata->archive_file_offset
474 || amt > filedata->file_size - filedata->archive_file_offset - offset)
475 {
476 if (reason)
477 error (_("Reading %" PRIu64 " bytes extends past end of file for %s\n"),
478 amt, reason);
479 return NULL;
480 }
481
482 if (fseek (filedata->handle, filedata->archive_file_offset + offset,
483 SEEK_SET))
484 {
485 if (reason)
486 error (_("Unable to seek to 0x%lx for %s\n"),
487 filedata->archive_file_offset + offset, reason);
488 return NULL;
489 }
490
491 mvar = var;
492 if (mvar == NULL)
493 {
494 /* + 1 so that we can '\0' terminate invalid string table sections. */
495 mvar = malloc ((size_t) amt + 1);
496
497 if (mvar == NULL)
498 {
499 if (reason)
500 error (_("Out of memory allocating %" PRIu64 " bytes for %s\n"),
501 amt, reason);
502 return NULL;
503 }
504
505 ((char *) mvar)[amt] = '\0';
506 }
507
508 if (fread (mvar, (size_t) size, (size_t) nmemb, filedata->handle) != nmemb)
509 {
510 if (reason)
511 error (_("Unable to read in %" PRIu64 " bytes of %s\n"),
512 amt, reason);
513 if (mvar != var)
514 free (mvar);
515 return NULL;
516 }
517
518 return mvar;
519 }
520
521 /* Print a VMA value in the MODE specified.
522 Returns the number of characters displayed. */
523
524 static unsigned int
525 print_vma (uint64_t vma, print_mode mode)
526 {
527 unsigned int nc = 0;
528
529 switch (mode)
530 {
531 case FULL_HEX:
532 nc = printf ("0x");
533 /* Fall through. */
534 case LONG_HEX:
535 if (!is_32bit_elf)
536 return nc + printf ("%16.16" PRIx64, vma);
537 return nc + printf ("%8.8" PRIx64, vma);
538
539 case DEC_5:
540 if (vma <= 99999)
541 return printf ("%5" PRId64, vma);
542 /* Fall through. */
543 case PREFIX_HEX:
544 nc = printf ("0x");
545 /* Fall through. */
546 case HEX:
547 return nc + printf ("%" PRIx64, vma);
548
549 case PREFIX_HEX_5:
550 nc = printf ("0x");
551 /* Fall through. */
552 case HEX_5:
553 return nc + printf ("%05" PRIx64, vma);
554
555 case DEC:
556 return printf ("%" PRId64, vma);
557
558 case UNSIGNED:
559 return printf ("%" PRIu64, vma);
560
561 case UNSIGNED_5:
562 return printf ("%5" PRIu64, vma);
563
564 case OCTAL:
565 return printf ("%" PRIo64, vma);
566
567 case OCTAL_5:
568 return printf ("%5" PRIo64, vma);
569
570 default:
571 /* FIXME: Report unrecognised mode ? */
572 return 0;
573 }
574 }
575
576
577 /* Display a symbol on stdout. Handles the display of control characters and
578 multibye characters (assuming the host environment supports them).
579
580 Display at most abs(WIDTH) characters, truncating as necessary, unless do_wide is true.
581
582 If truncation will happen and do_not_show_symbol_truncation is FALSE then display
583 abs(WIDTH) - 5 characters followed by "[...]".
584
585 If WIDTH is negative then ensure that the output is at least (- WIDTH) characters,
586 padding as necessary.
587
588 Returns the number of emitted characters. */
589
590 static unsigned int
591 print_symbol (signed int width, const char * symbol)
592 {
593 bool extra_padding = false;
594 bool do_dots = false;
595 signed int num_printed = 0;
596 #ifdef HAVE_MBSTATE_T
597 mbstate_t state;
598 #endif
599 unsigned int width_remaining;
600 const void * alloced_symbol = NULL;
601
602 if (width < 0)
603 {
604 /* Keep the width positive. This helps the code below. */
605 width = - width;
606 extra_padding = true;
607 }
608 else if (width == 0)
609 return 0;
610
611 if (do_wide)
612 /* Set the remaining width to a very large value.
613 This simplifies the code below. */
614 width_remaining = INT_MAX;
615 else
616 {
617 width_remaining = width;
618 if (! do_not_show_symbol_truncation
619 && (int) strlen (symbol) > width)
620 {
621 width_remaining -= 5;
622 if ((int) width_remaining < 0)
623 width_remaining = 0;
624 do_dots = true;
625 }
626 }
627
628 #ifdef HAVE_MBSTATE_T
629 /* Initialise the multibyte conversion state. */
630 memset (& state, 0, sizeof (state));
631 #endif
632
633 if (do_demangle && *symbol)
634 {
635 const char * res = cplus_demangle (symbol, demangle_flags);
636
637 if (res != NULL)
638 alloced_symbol = symbol = res;
639 }
640
641 while (width_remaining)
642 {
643 size_t n;
644 const char c = *symbol++;
645
646 if (c == 0)
647 break;
648
649 if (ISPRINT (c))
650 {
651 putchar (c);
652 width_remaining --;
653 num_printed ++;
654 }
655 else if (ISCNTRL (c))
656 {
657 /* Do not print control characters directly as they can affect terminal
658 settings. Such characters usually appear in the names generated
659 by the assembler for local labels. */
660
661 if (width_remaining < 2)
662 break;
663
664 printf ("^%c", c + 0x40);
665 width_remaining -= 2;
666 num_printed += 2;
667 }
668 else if (c == 0x7f)
669 {
670 if (width_remaining < 5)
671 break;
672 printf ("<DEL>");
673 width_remaining -= 5;
674 num_printed += 5;
675 }
676 else if (unicode_display != unicode_locale
677 && unicode_display != unicode_default)
678 {
679 /* Display unicode characters as something else. */
680 unsigned char bytes[4];
681 bool is_utf8;
682 unsigned int nbytes;
683
684 bytes[0] = c;
685
686 if (bytes[0] < 0xc0)
687 {
688 nbytes = 1;
689 is_utf8 = false;
690 }
691 else
692 {
693 bytes[1] = *symbol++;
694
695 if ((bytes[1] & 0xc0) != 0x80)
696 {
697 is_utf8 = false;
698 /* Do not consume this character. It may only
699 be the first byte in the sequence that was
700 corrupt. */
701 --symbol;
702 nbytes = 1;
703 }
704 else if ((bytes[0] & 0x20) == 0)
705 {
706 is_utf8 = true;
707 nbytes = 2;
708 }
709 else
710 {
711 bytes[2] = *symbol++;
712
713 if ((bytes[2] & 0xc0) != 0x80)
714 {
715 is_utf8 = false;
716 symbol -= 2;
717 nbytes = 1;
718 }
719 else if ((bytes[0] & 0x10) == 0)
720 {
721 is_utf8 = true;
722 nbytes = 3;
723 }
724 else
725 {
726 bytes[3] = *symbol++;
727
728 nbytes = 4;
729
730 if ((bytes[3] & 0xc0) != 0x80)
731 {
732 is_utf8 = false;
733 symbol -= 3;
734 nbytes = 1;
735 }
736 else
737 is_utf8 = true;
738 }
739 }
740 }
741
742 if (unicode_display == unicode_invalid)
743 is_utf8 = false;
744
745 if (unicode_display == unicode_hex || ! is_utf8)
746 {
747 unsigned int i;
748
749 if (width_remaining < (nbytes * 2) + 2)
750 break;
751
752 putchar (is_utf8 ? '<' : '{');
753 printf ("0x");
754 for (i = 0; i < nbytes; i++)
755 printf ("%02x", bytes[i]);
756 putchar (is_utf8 ? '>' : '}');
757 }
758 else
759 {
760 if (unicode_display == unicode_highlight && isatty (1))
761 printf ("\x1B[31;47m"); /* Red. */
762
763 switch (nbytes)
764 {
765 case 2:
766 if (width_remaining < 6)
767 break;
768 printf ("\\u%02x%02x",
769 (bytes[0] & 0x1c) >> 2,
770 ((bytes[0] & 0x03) << 6) | (bytes[1] & 0x3f));
771 break;
772 case 3:
773 if (width_remaining < 6)
774 break;
775 printf ("\\u%02x%02x",
776 ((bytes[0] & 0x0f) << 4) | ((bytes[1] & 0x3c) >> 2),
777 ((bytes[1] & 0x03) << 6) | (bytes[2] & 0x3f));
778 break;
779 case 4:
780 if (width_remaining < 8)
781 break;
782 printf ("\\u%02x%02x%02x",
783 ((bytes[0] & 0x07) << 6) | ((bytes[1] & 0x3c) >> 2),
784 ((bytes[1] & 0x03) << 6) | ((bytes[2] & 0x3c) >> 2),
785 ((bytes[2] & 0x03) << 6) | (bytes[3] & 0x3f));
786
787 break;
788 default:
789 /* URG. */
790 break;
791 }
792
793 if (unicode_display == unicode_highlight && isatty (1))
794 printf ("\033[0m"); /* Default colour. */
795 }
796
797 if (bytes[nbytes - 1] == 0)
798 break;
799 }
800 else
801 {
802 #ifdef HAVE_MBSTATE_T
803 wchar_t w;
804 #endif
805 /* Let printf do the hard work of displaying multibyte characters. */
806 printf ("%.1s", symbol - 1);
807 width_remaining --;
808 num_printed ++;
809
810 #ifdef HAVE_MBSTATE_T
811 /* Try to find out how many bytes made up the character that was
812 just printed. Advance the symbol pointer past the bytes that
813 were displayed. */
814 n = mbrtowc (& w, symbol - 1, MB_CUR_MAX, & state);
815 #else
816 n = 1;
817 #endif
818 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
819 symbol += (n - 1);
820 }
821 }
822
823 if (do_dots)
824 num_printed += printf ("[...]");
825
826 if (extra_padding && num_printed < width)
827 {
828 /* Fill in the remaining spaces. */
829 printf ("%-*s", width - num_printed, " ");
830 num_printed = width;
831 }
832
833 free ((void *) alloced_symbol);
834 return num_printed;
835 }
836
837 /* Returns a pointer to a static buffer containing a printable version of
838 the given section's name. Like print_symbol, except that it does not try
839 to print multibyte characters, it just interprets them as hex values. */
840
841 static const char *
842 printable_section_name (Filedata * filedata, const Elf_Internal_Shdr * sec)
843 {
844 #define MAX_PRINT_SEC_NAME_LEN 256
845 static char sec_name_buf [MAX_PRINT_SEC_NAME_LEN + 1];
846 const char * name = section_name_print (filedata, sec);
847 char * buf = sec_name_buf;
848 char c;
849 unsigned int remaining = MAX_PRINT_SEC_NAME_LEN;
850
851 while ((c = * name ++) != 0)
852 {
853 if (ISCNTRL (c))
854 {
855 if (remaining < 2)
856 break;
857
858 * buf ++ = '^';
859 * buf ++ = c + 0x40;
860 remaining -= 2;
861 }
862 else if (ISPRINT (c))
863 {
864 * buf ++ = c;
865 remaining -= 1;
866 }
867 else
868 {
869 static char hex[17] = "0123456789ABCDEF";
870
871 if (remaining < 4)
872 break;
873 * buf ++ = '<';
874 * buf ++ = hex[(c & 0xf0) >> 4];
875 * buf ++ = hex[c & 0x0f];
876 * buf ++ = '>';
877 remaining -= 4;
878 }
879
880 if (remaining == 0)
881 break;
882 }
883
884 * buf = 0;
885 return sec_name_buf;
886 }
887
888 static const char *
889 printable_section_name_from_index (Filedata * filedata, unsigned long ndx)
890 {
891 if (ndx >= filedata->file_header.e_shnum)
892 return _("<corrupt>");
893
894 return printable_section_name (filedata, filedata->section_headers + ndx);
895 }
896
897 /* Return a pointer to section NAME, or NULL if no such section exists. */
898
899 static Elf_Internal_Shdr *
900 find_section (Filedata * filedata, const char * name)
901 {
902 unsigned int i;
903
904 if (filedata->section_headers == NULL)
905 return NULL;
906
907 for (i = 0; i < filedata->file_header.e_shnum; i++)
908 if (section_name_valid (filedata, filedata->section_headers + i)
909 && streq (section_name (filedata, filedata->section_headers + i),
910 name))
911 return filedata->section_headers + i;
912
913 return NULL;
914 }
915
916 /* Return a pointer to a section containing ADDR, or NULL if no such
917 section exists. */
918
919 static Elf_Internal_Shdr *
920 find_section_by_address (Filedata * filedata, uint64_t addr)
921 {
922 unsigned int i;
923
924 if (filedata->section_headers == NULL)
925 return NULL;
926
927 for (i = 0; i < filedata->file_header.e_shnum; i++)
928 {
929 Elf_Internal_Shdr *sec = filedata->section_headers + i;
930
931 if (addr >= sec->sh_addr && addr < sec->sh_addr + sec->sh_size)
932 return sec;
933 }
934
935 return NULL;
936 }
937
938 static Elf_Internal_Shdr *
939 find_section_by_type (Filedata * filedata, unsigned int type)
940 {
941 unsigned int i;
942
943 if (filedata->section_headers == NULL)
944 return NULL;
945
946 for (i = 0; i < filedata->file_header.e_shnum; i++)
947 {
948 Elf_Internal_Shdr *sec = filedata->section_headers + i;
949
950 if (sec->sh_type == type)
951 return sec;
952 }
953
954 return NULL;
955 }
956
957 /* Return a pointer to section NAME, or NULL if no such section exists,
958 restricted to the list of sections given in SET. */
959
960 static Elf_Internal_Shdr *
961 find_section_in_set (Filedata * filedata, const char * name, unsigned int * set)
962 {
963 unsigned int i;
964
965 if (filedata->section_headers == NULL)
966 return NULL;
967
968 if (set != NULL)
969 {
970 while ((i = *set++) > 0)
971 {
972 /* See PR 21156 for a reproducer. */
973 if (i >= filedata->file_header.e_shnum)
974 continue; /* FIXME: Should we issue an error message ? */
975
976 if (section_name_valid (filedata, filedata->section_headers + i)
977 && streq (section_name (filedata, filedata->section_headers + i),
978 name))
979 return filedata->section_headers + i;
980 }
981 }
982
983 return find_section (filedata, name);
984 }
985
986 /* Return TRUE if the current file is for IA-64 machine and OpenVMS ABI.
987 This OS has so many departures from the ELF standard that we test it at
988 many places. */
989
990 static inline bool
991 is_ia64_vms (Filedata * filedata)
992 {
993 return filedata->file_header.e_machine == EM_IA_64
994 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS;
995 }
996
997 /* Guess the relocation size commonly used by the specific machines. */
998
999 static bool
1000 guess_is_rela (unsigned int e_machine)
1001 {
1002 switch (e_machine)
1003 {
1004 /* Targets that use REL relocations. */
1005 case EM_386:
1006 case EM_IAMCU:
1007 case EM_960:
1008 case EM_ARM:
1009 case EM_D10V:
1010 case EM_CYGNUS_D10V:
1011 case EM_DLX:
1012 case EM_MIPS:
1013 case EM_MIPS_RS3_LE:
1014 case EM_CYGNUS_M32R:
1015 case EM_SCORE:
1016 case EM_XGATE:
1017 case EM_NFP:
1018 case EM_BPF:
1019 return false;
1020
1021 /* Targets that use RELA relocations. */
1022 case EM_68K:
1023 case EM_860:
1024 case EM_AARCH64:
1025 case EM_ADAPTEVA_EPIPHANY:
1026 case EM_ALPHA:
1027 case EM_ALTERA_NIOS2:
1028 case EM_ARC:
1029 case EM_ARC_COMPACT:
1030 case EM_ARC_COMPACT2:
1031 case EM_AVR:
1032 case EM_AVR_OLD:
1033 case EM_BLACKFIN:
1034 case EM_CR16:
1035 case EM_CRIS:
1036 case EM_CRX:
1037 case EM_CSKY:
1038 case EM_D30V:
1039 case EM_CYGNUS_D30V:
1040 case EM_FR30:
1041 case EM_FT32:
1042 case EM_CYGNUS_FR30:
1043 case EM_CYGNUS_FRV:
1044 case EM_H8S:
1045 case EM_H8_300:
1046 case EM_H8_300H:
1047 case EM_IA_64:
1048 case EM_IP2K:
1049 case EM_IP2K_OLD:
1050 case EM_IQ2000:
1051 case EM_LATTICEMICO32:
1052 case EM_M32C_OLD:
1053 case EM_M32C:
1054 case EM_M32R:
1055 case EM_MCORE:
1056 case EM_CYGNUS_MEP:
1057 case EM_METAG:
1058 case EM_MMIX:
1059 case EM_MN10200:
1060 case EM_CYGNUS_MN10200:
1061 case EM_MN10300:
1062 case EM_CYGNUS_MN10300:
1063 case EM_MOXIE:
1064 case EM_MSP430:
1065 case EM_MSP430_OLD:
1066 case EM_MT:
1067 case EM_NDS32:
1068 case EM_NIOS32:
1069 case EM_OR1K:
1070 case EM_PPC64:
1071 case EM_PPC:
1072 case EM_TI_PRU:
1073 case EM_RISCV:
1074 case EM_RL78:
1075 case EM_RX:
1076 case EM_S390:
1077 case EM_S390_OLD:
1078 case EM_SH:
1079 case EM_SPARC:
1080 case EM_SPARC32PLUS:
1081 case EM_SPARCV9:
1082 case EM_SPU:
1083 case EM_TI_C6000:
1084 case EM_TILEGX:
1085 case EM_TILEPRO:
1086 case EM_V800:
1087 case EM_V850:
1088 case EM_CYGNUS_V850:
1089 case EM_VAX:
1090 case EM_VISIUM:
1091 case EM_X86_64:
1092 case EM_L1OM:
1093 case EM_K1OM:
1094 case EM_XSTORMY16:
1095 case EM_XTENSA:
1096 case EM_XTENSA_OLD:
1097 case EM_MICROBLAZE:
1098 case EM_MICROBLAZE_OLD:
1099 case EM_WEBASSEMBLY:
1100 return true;
1101
1102 case EM_68HC05:
1103 case EM_68HC08:
1104 case EM_68HC11:
1105 case EM_68HC16:
1106 case EM_FX66:
1107 case EM_ME16:
1108 case EM_MMA:
1109 case EM_NCPU:
1110 case EM_NDR1:
1111 case EM_PCP:
1112 case EM_ST100:
1113 case EM_ST19:
1114 case EM_ST7:
1115 case EM_ST9PLUS:
1116 case EM_STARCORE:
1117 case EM_SVX:
1118 case EM_TINYJ:
1119 default:
1120 warn (_("Don't know about relocations on this machine architecture\n"));
1121 return false;
1122 }
1123 }
1124
1125 /* Load RELA type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1126 Returns TRUE upon success, FALSE otherwise. If successful then a
1127 pointer to a malloc'ed buffer containing the relocs is placed in *RELASP,
1128 and the number of relocs loaded is placed in *NRELASP. It is the caller's
1129 responsibility to free the allocated buffer. */
1130
1131 static bool
1132 slurp_rela_relocs (Filedata * filedata,
1133 unsigned long rel_offset,
1134 unsigned long rel_size,
1135 Elf_Internal_Rela ** relasp,
1136 unsigned long * nrelasp)
1137 {
1138 Elf_Internal_Rela * relas;
1139 size_t nrelas;
1140 unsigned int i;
1141
1142 if (is_32bit_elf)
1143 {
1144 Elf32_External_Rela * erelas;
1145
1146 erelas = (Elf32_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1147 rel_size, _("32-bit relocation data"));
1148 if (!erelas)
1149 return false;
1150
1151 nrelas = rel_size / sizeof (Elf32_External_Rela);
1152
1153 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1154 sizeof (Elf_Internal_Rela));
1155
1156 if (relas == NULL)
1157 {
1158 free (erelas);
1159 error (_("out of memory parsing relocs\n"));
1160 return false;
1161 }
1162
1163 for (i = 0; i < nrelas; i++)
1164 {
1165 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1166 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1167 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1168 }
1169
1170 free (erelas);
1171 }
1172 else
1173 {
1174 Elf64_External_Rela * erelas;
1175
1176 erelas = (Elf64_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1177 rel_size, _("64-bit relocation data"));
1178 if (!erelas)
1179 return false;
1180
1181 nrelas = rel_size / sizeof (Elf64_External_Rela);
1182
1183 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1184 sizeof (Elf_Internal_Rela));
1185
1186 if (relas == NULL)
1187 {
1188 free (erelas);
1189 error (_("out of memory parsing relocs\n"));
1190 return false;
1191 }
1192
1193 for (i = 0; i < nrelas; i++)
1194 {
1195 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1196 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1197 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1198
1199 if (filedata->file_header.e_machine == EM_MIPS
1200 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1201 {
1202 /* In little-endian objects, r_info isn't really a
1203 64-bit little-endian value: it has a 32-bit
1204 little-endian symbol index followed by four
1205 individual byte fields. Reorder INFO
1206 accordingly. */
1207 uint64_t inf = relas[i].r_info;
1208 inf = (((inf & 0xffffffff) << 32)
1209 | ((inf >> 56) & 0xff)
1210 | ((inf >> 40) & 0xff00)
1211 | ((inf >> 24) & 0xff0000)
1212 | ((inf >> 8) & 0xff000000));
1213 relas[i].r_info = inf;
1214 }
1215 }
1216
1217 free (erelas);
1218 }
1219
1220 *relasp = relas;
1221 *nrelasp = nrelas;
1222 return true;
1223 }
1224
1225 /* Load REL type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1226 Returns TRUE upon success, FALSE otherwise. If successful then a
1227 pointer to a malloc'ed buffer containing the relocs is placed in *RELSP,
1228 and the number of relocs loaded is placed in *NRELSP. It is the caller's
1229 responsibility to free the allocated buffer. */
1230
1231 static bool
1232 slurp_rel_relocs (Filedata * filedata,
1233 unsigned long rel_offset,
1234 unsigned long rel_size,
1235 Elf_Internal_Rela ** relsp,
1236 unsigned long * nrelsp)
1237 {
1238 Elf_Internal_Rela * rels;
1239 size_t nrels;
1240 unsigned int i;
1241
1242 if (is_32bit_elf)
1243 {
1244 Elf32_External_Rel * erels;
1245
1246 erels = (Elf32_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1247 rel_size, _("32-bit relocation data"));
1248 if (!erels)
1249 return false;
1250
1251 nrels = rel_size / sizeof (Elf32_External_Rel);
1252
1253 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1254
1255 if (rels == NULL)
1256 {
1257 free (erels);
1258 error (_("out of memory parsing relocs\n"));
1259 return false;
1260 }
1261
1262 for (i = 0; i < nrels; i++)
1263 {
1264 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1265 rels[i].r_info = BYTE_GET (erels[i].r_info);
1266 rels[i].r_addend = 0;
1267 }
1268
1269 free (erels);
1270 }
1271 else
1272 {
1273 Elf64_External_Rel * erels;
1274
1275 erels = (Elf64_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1276 rel_size, _("64-bit relocation data"));
1277 if (!erels)
1278 return false;
1279
1280 nrels = rel_size / sizeof (Elf64_External_Rel);
1281
1282 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1283
1284 if (rels == NULL)
1285 {
1286 free (erels);
1287 error (_("out of memory parsing relocs\n"));
1288 return false;
1289 }
1290
1291 for (i = 0; i < nrels; i++)
1292 {
1293 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1294 rels[i].r_info = BYTE_GET (erels[i].r_info);
1295 rels[i].r_addend = 0;
1296
1297 if (filedata->file_header.e_machine == EM_MIPS
1298 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1299 {
1300 /* In little-endian objects, r_info isn't really a
1301 64-bit little-endian value: it has a 32-bit
1302 little-endian symbol index followed by four
1303 individual byte fields. Reorder INFO
1304 accordingly. */
1305 uint64_t inf = rels[i].r_info;
1306 inf = (((inf & 0xffffffff) << 32)
1307 | ((inf >> 56) & 0xff)
1308 | ((inf >> 40) & 0xff00)
1309 | ((inf >> 24) & 0xff0000)
1310 | ((inf >> 8) & 0xff000000));
1311 rels[i].r_info = inf;
1312 }
1313 }
1314
1315 free (erels);
1316 }
1317
1318 *relsp = rels;
1319 *nrelsp = nrels;
1320 return true;
1321 }
1322
1323 static bool
1324 slurp_relr_relocs (Filedata * filedata,
1325 unsigned long relr_offset,
1326 unsigned long relr_size,
1327 uint64_t ** relrsp,
1328 unsigned long * nrelrsp)
1329 {
1330 void *relrs;
1331 size_t size = 0, nentries, i;
1332 uint64_t base = 0, addr, entry;
1333
1334 relrs = get_data (NULL, filedata, relr_offset, 1, relr_size,
1335 _("RELR relocation data"));
1336 if (!relrs)
1337 return false;
1338
1339 if (is_32bit_elf)
1340 nentries = relr_size / sizeof (Elf32_External_Relr);
1341 else
1342 nentries = relr_size / sizeof (Elf64_External_Relr);
1343 for (i = 0; i < nentries; i++)
1344 {
1345 if (is_32bit_elf)
1346 entry = BYTE_GET (((Elf32_External_Relr *)relrs)[i].r_data);
1347 else
1348 entry = BYTE_GET (((Elf64_External_Relr *)relrs)[i].r_data);
1349 if ((entry & 1) == 0)
1350 size++;
1351 else
1352 while ((entry >>= 1) != 0)
1353 if ((entry & 1) == 1)
1354 size++;
1355 }
1356
1357 *relrsp = malloc (size * sizeof (**relrsp));
1358 if (*relrsp == NULL)
1359 {
1360 free (relrs);
1361 error (_("out of memory parsing relocs\n"));
1362 return false;
1363 }
1364
1365 size = 0;
1366 for (i = 0; i < nentries; i++)
1367 {
1368 const uint64_t entry_bytes = is_32bit_elf ? 4 : 8;
1369
1370 if (is_32bit_elf)
1371 entry = BYTE_GET (((Elf32_External_Relr *)relrs)[i].r_data);
1372 else
1373 entry = BYTE_GET (((Elf64_External_Relr *)relrs)[i].r_data);
1374 if ((entry & 1) == 0)
1375 {
1376 (*relrsp)[size++] = entry;
1377 base = entry + entry_bytes;
1378 }
1379 else
1380 {
1381 for (addr = base; (entry >>= 1) != 0; addr += entry_bytes)
1382 if ((entry & 1) != 0)
1383 (*relrsp)[size++] = addr;
1384 base += entry_bytes * (entry_bytes * CHAR_BIT - 1);
1385 }
1386 }
1387
1388 *nrelrsp = size;
1389 free (relrs);
1390 return true;
1391 }
1392
1393 /* Returns the reloc type extracted from the reloc info field. */
1394
1395 static unsigned int
1396 get_reloc_type (Filedata * filedata, uint64_t reloc_info)
1397 {
1398 if (is_32bit_elf)
1399 return ELF32_R_TYPE (reloc_info);
1400
1401 switch (filedata->file_header.e_machine)
1402 {
1403 case EM_MIPS:
1404 /* Note: We assume that reloc_info has already been adjusted for us. */
1405 return ELF64_MIPS_R_TYPE (reloc_info);
1406
1407 case EM_SPARCV9:
1408 return ELF64_R_TYPE_ID (reloc_info);
1409
1410 default:
1411 return ELF64_R_TYPE (reloc_info);
1412 }
1413 }
1414
1415 /* Return the symbol index extracted from the reloc info field. */
1416
1417 static uint64_t
1418 get_reloc_symindex (uint64_t reloc_info)
1419 {
1420 return is_32bit_elf ? ELF32_R_SYM (reloc_info) : ELF64_R_SYM (reloc_info);
1421 }
1422
1423 static inline bool
1424 uses_msp430x_relocs (Filedata * filedata)
1425 {
1426 return
1427 filedata->file_header.e_machine == EM_MSP430 /* Paranoia. */
1428 /* GCC uses osabi == ELFOSBI_STANDALONE. */
1429 && (((filedata->file_header.e_flags & EF_MSP430_MACH) == E_MSP430_MACH_MSP430X)
1430 /* TI compiler uses ELFOSABI_NONE. */
1431 || (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_NONE));
1432 }
1433
1434 /* Display the contents of the relocation data found at the specified
1435 offset. */
1436
1437 static bool
1438 dump_relocations (Filedata * filedata,
1439 unsigned long rel_offset,
1440 unsigned long rel_size,
1441 Elf_Internal_Sym * symtab,
1442 unsigned long nsyms,
1443 char * strtab,
1444 unsigned long strtablen,
1445 relocation_type rel_type,
1446 bool is_dynsym)
1447 {
1448 unsigned long i;
1449 Elf_Internal_Rela * rels;
1450 bool res = true;
1451
1452 if (rel_type == reltype_unknown)
1453 rel_type = guess_is_rela (filedata->file_header.e_machine) ? reltype_rela : reltype_rel;
1454
1455 if (rel_type == reltype_rela)
1456 {
1457 if (!slurp_rela_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1458 return false;
1459 }
1460 else if (rel_type == reltype_rel)
1461 {
1462 if (!slurp_rel_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1463 return false;
1464 }
1465 else if (rel_type == reltype_relr)
1466 {
1467 uint64_t * relrs;
1468 const char *format
1469 = is_32bit_elf ? "%08" PRIx64 "\n" : "%016" PRIx64 "\n";
1470
1471 if (!slurp_relr_relocs (filedata, rel_offset, rel_size, &relrs,
1472 &rel_size))
1473 return false;
1474
1475 printf (ngettext (" %lu offset\n", " %lu offsets\n", rel_size),
1476 rel_size);
1477 for (i = 0; i < rel_size; i++)
1478 printf (format, relrs[i]);
1479 free (relrs);
1480 return true;
1481 }
1482
1483 if (is_32bit_elf)
1484 {
1485 if (rel_type == reltype_rela)
1486 {
1487 if (do_wide)
1488 printf (_(" Offset Info Type Sym. Value Symbol's Name + Addend\n"));
1489 else
1490 printf (_(" Offset Info Type Sym.Value Sym. Name + Addend\n"));
1491 }
1492 else
1493 {
1494 if (do_wide)
1495 printf (_(" Offset Info Type Sym. Value Symbol's Name\n"));
1496 else
1497 printf (_(" Offset Info Type Sym.Value Sym. Name\n"));
1498 }
1499 }
1500 else
1501 {
1502 if (rel_type == reltype_rela)
1503 {
1504 if (do_wide)
1505 printf (_(" Offset Info Type Symbol's Value Symbol's Name + Addend\n"));
1506 else
1507 printf (_(" Offset Info Type Sym. Value Sym. Name + Addend\n"));
1508 }
1509 else
1510 {
1511 if (do_wide)
1512 printf (_(" Offset Info Type Symbol's Value Symbol's Name\n"));
1513 else
1514 printf (_(" Offset Info Type Sym. Value Sym. Name\n"));
1515 }
1516 }
1517
1518 for (i = 0; i < rel_size; i++)
1519 {
1520 const char * rtype;
1521 uint64_t offset;
1522 uint64_t inf;
1523 uint64_t symtab_index;
1524 uint64_t type;
1525
1526 offset = rels[i].r_offset;
1527 inf = rels[i].r_info;
1528
1529 type = get_reloc_type (filedata, inf);
1530 symtab_index = get_reloc_symindex (inf);
1531
1532 if (is_32bit_elf)
1533 {
1534 printf ("%8.8lx %8.8lx ",
1535 (unsigned long) offset & 0xffffffff,
1536 (unsigned long) inf & 0xffffffff);
1537 }
1538 else
1539 {
1540 printf (do_wide
1541 ? "%16.16" PRIx64 " %16.16" PRIx64 " "
1542 : "%12.12" PRIx64 " %12.12" PRIx64 " ",
1543 offset, inf);
1544 }
1545
1546 switch (filedata->file_header.e_machine)
1547 {
1548 default:
1549 rtype = NULL;
1550 break;
1551
1552 case EM_AARCH64:
1553 rtype = elf_aarch64_reloc_type (type);
1554 break;
1555
1556 case EM_M32R:
1557 case EM_CYGNUS_M32R:
1558 rtype = elf_m32r_reloc_type (type);
1559 break;
1560
1561 case EM_386:
1562 case EM_IAMCU:
1563 rtype = elf_i386_reloc_type (type);
1564 break;
1565
1566 case EM_68HC11:
1567 case EM_68HC12:
1568 rtype = elf_m68hc11_reloc_type (type);
1569 break;
1570
1571 case EM_S12Z:
1572 rtype = elf_s12z_reloc_type (type);
1573 break;
1574
1575 case EM_68K:
1576 rtype = elf_m68k_reloc_type (type);
1577 break;
1578
1579 case EM_960:
1580 rtype = elf_i960_reloc_type (type);
1581 break;
1582
1583 case EM_AVR:
1584 case EM_AVR_OLD:
1585 rtype = elf_avr_reloc_type (type);
1586 break;
1587
1588 case EM_OLD_SPARCV9:
1589 case EM_SPARC32PLUS:
1590 case EM_SPARCV9:
1591 case EM_SPARC:
1592 rtype = elf_sparc_reloc_type (type);
1593 break;
1594
1595 case EM_SPU:
1596 rtype = elf_spu_reloc_type (type);
1597 break;
1598
1599 case EM_V800:
1600 rtype = v800_reloc_type (type);
1601 break;
1602 case EM_V850:
1603 case EM_CYGNUS_V850:
1604 rtype = v850_reloc_type (type);
1605 break;
1606
1607 case EM_D10V:
1608 case EM_CYGNUS_D10V:
1609 rtype = elf_d10v_reloc_type (type);
1610 break;
1611
1612 case EM_D30V:
1613 case EM_CYGNUS_D30V:
1614 rtype = elf_d30v_reloc_type (type);
1615 break;
1616
1617 case EM_DLX:
1618 rtype = elf_dlx_reloc_type (type);
1619 break;
1620
1621 case EM_SH:
1622 rtype = elf_sh_reloc_type (type);
1623 break;
1624
1625 case EM_MN10300:
1626 case EM_CYGNUS_MN10300:
1627 rtype = elf_mn10300_reloc_type (type);
1628 break;
1629
1630 case EM_MN10200:
1631 case EM_CYGNUS_MN10200:
1632 rtype = elf_mn10200_reloc_type (type);
1633 break;
1634
1635 case EM_FR30:
1636 case EM_CYGNUS_FR30:
1637 rtype = elf_fr30_reloc_type (type);
1638 break;
1639
1640 case EM_CYGNUS_FRV:
1641 rtype = elf_frv_reloc_type (type);
1642 break;
1643
1644 case EM_CSKY:
1645 rtype = elf_csky_reloc_type (type);
1646 break;
1647
1648 case EM_FT32:
1649 rtype = elf_ft32_reloc_type (type);
1650 break;
1651
1652 case EM_MCORE:
1653 rtype = elf_mcore_reloc_type (type);
1654 break;
1655
1656 case EM_MMIX:
1657 rtype = elf_mmix_reloc_type (type);
1658 break;
1659
1660 case EM_MOXIE:
1661 rtype = elf_moxie_reloc_type (type);
1662 break;
1663
1664 case EM_MSP430:
1665 if (uses_msp430x_relocs (filedata))
1666 {
1667 rtype = elf_msp430x_reloc_type (type);
1668 break;
1669 }
1670 /* Fall through. */
1671 case EM_MSP430_OLD:
1672 rtype = elf_msp430_reloc_type (type);
1673 break;
1674
1675 case EM_NDS32:
1676 rtype = elf_nds32_reloc_type (type);
1677 break;
1678
1679 case EM_PPC:
1680 rtype = elf_ppc_reloc_type (type);
1681 break;
1682
1683 case EM_PPC64:
1684 rtype = elf_ppc64_reloc_type (type);
1685 break;
1686
1687 case EM_MIPS:
1688 case EM_MIPS_RS3_LE:
1689 rtype = elf_mips_reloc_type (type);
1690 break;
1691
1692 case EM_RISCV:
1693 rtype = elf_riscv_reloc_type (type);
1694 break;
1695
1696 case EM_ALPHA:
1697 rtype = elf_alpha_reloc_type (type);
1698 break;
1699
1700 case EM_ARM:
1701 rtype = elf_arm_reloc_type (type);
1702 break;
1703
1704 case EM_ARC:
1705 case EM_ARC_COMPACT:
1706 case EM_ARC_COMPACT2:
1707 rtype = elf_arc_reloc_type (type);
1708 break;
1709
1710 case EM_PARISC:
1711 rtype = elf_hppa_reloc_type (type);
1712 break;
1713
1714 case EM_H8_300:
1715 case EM_H8_300H:
1716 case EM_H8S:
1717 rtype = elf_h8_reloc_type (type);
1718 break;
1719
1720 case EM_OR1K:
1721 rtype = elf_or1k_reloc_type (type);
1722 break;
1723
1724 case EM_PJ:
1725 case EM_PJ_OLD:
1726 rtype = elf_pj_reloc_type (type);
1727 break;
1728 case EM_IA_64:
1729 rtype = elf_ia64_reloc_type (type);
1730 break;
1731
1732 case EM_CRIS:
1733 rtype = elf_cris_reloc_type (type);
1734 break;
1735
1736 case EM_860:
1737 rtype = elf_i860_reloc_type (type);
1738 break;
1739
1740 case EM_X86_64:
1741 case EM_L1OM:
1742 case EM_K1OM:
1743 rtype = elf_x86_64_reloc_type (type);
1744 break;
1745
1746 case EM_S370:
1747 rtype = i370_reloc_type (type);
1748 break;
1749
1750 case EM_S390_OLD:
1751 case EM_S390:
1752 rtype = elf_s390_reloc_type (type);
1753 break;
1754
1755 case EM_SCORE:
1756 rtype = elf_score_reloc_type (type);
1757 break;
1758
1759 case EM_XSTORMY16:
1760 rtype = elf_xstormy16_reloc_type (type);
1761 break;
1762
1763 case EM_CRX:
1764 rtype = elf_crx_reloc_type (type);
1765 break;
1766
1767 case EM_VAX:
1768 rtype = elf_vax_reloc_type (type);
1769 break;
1770
1771 case EM_VISIUM:
1772 rtype = elf_visium_reloc_type (type);
1773 break;
1774
1775 case EM_BPF:
1776 rtype = elf_bpf_reloc_type (type);
1777 break;
1778
1779 case EM_ADAPTEVA_EPIPHANY:
1780 rtype = elf_epiphany_reloc_type (type);
1781 break;
1782
1783 case EM_IP2K:
1784 case EM_IP2K_OLD:
1785 rtype = elf_ip2k_reloc_type (type);
1786 break;
1787
1788 case EM_IQ2000:
1789 rtype = elf_iq2000_reloc_type (type);
1790 break;
1791
1792 case EM_XTENSA_OLD:
1793 case EM_XTENSA:
1794 rtype = elf_xtensa_reloc_type (type);
1795 break;
1796
1797 case EM_LATTICEMICO32:
1798 rtype = elf_lm32_reloc_type (type);
1799 break;
1800
1801 case EM_M32C_OLD:
1802 case EM_M32C:
1803 rtype = elf_m32c_reloc_type (type);
1804 break;
1805
1806 case EM_MT:
1807 rtype = elf_mt_reloc_type (type);
1808 break;
1809
1810 case EM_BLACKFIN:
1811 rtype = elf_bfin_reloc_type (type);
1812 break;
1813
1814 case EM_CYGNUS_MEP:
1815 rtype = elf_mep_reloc_type (type);
1816 break;
1817
1818 case EM_CR16:
1819 rtype = elf_cr16_reloc_type (type);
1820 break;
1821
1822 case EM_MICROBLAZE:
1823 case EM_MICROBLAZE_OLD:
1824 rtype = elf_microblaze_reloc_type (type);
1825 break;
1826
1827 case EM_RL78:
1828 rtype = elf_rl78_reloc_type (type);
1829 break;
1830
1831 case EM_RX:
1832 rtype = elf_rx_reloc_type (type);
1833 break;
1834
1835 case EM_METAG:
1836 rtype = elf_metag_reloc_type (type);
1837 break;
1838
1839 case EM_TI_C6000:
1840 rtype = elf_tic6x_reloc_type (type);
1841 break;
1842
1843 case EM_TILEGX:
1844 rtype = elf_tilegx_reloc_type (type);
1845 break;
1846
1847 case EM_TILEPRO:
1848 rtype = elf_tilepro_reloc_type (type);
1849 break;
1850
1851 case EM_WEBASSEMBLY:
1852 rtype = elf_wasm32_reloc_type (type);
1853 break;
1854
1855 case EM_XGATE:
1856 rtype = elf_xgate_reloc_type (type);
1857 break;
1858
1859 case EM_ALTERA_NIOS2:
1860 rtype = elf_nios2_reloc_type (type);
1861 break;
1862
1863 case EM_TI_PRU:
1864 rtype = elf_pru_reloc_type (type);
1865 break;
1866
1867 case EM_NFP:
1868 if (EF_NFP_MACH (filedata->file_header.e_flags) == E_NFP_MACH_3200)
1869 rtype = elf_nfp3200_reloc_type (type);
1870 else
1871 rtype = elf_nfp_reloc_type (type);
1872 break;
1873
1874 case EM_Z80:
1875 rtype = elf_z80_reloc_type (type);
1876 break;
1877
1878 case EM_LOONGARCH:
1879 rtype = elf_loongarch_reloc_type (type);
1880 break;
1881
1882 case EM_AMDGPU:
1883 rtype = elf_amdgpu_reloc_type (type);
1884 break;
1885 }
1886
1887 if (rtype == NULL)
1888 printf (_("unrecognized: %-7lx"), (unsigned long) type & 0xffffffff);
1889 else
1890 printf (do_wide ? "%-22s" : "%-17.17s", rtype);
1891
1892 if (filedata->file_header.e_machine == EM_ALPHA
1893 && rtype != NULL
1894 && streq (rtype, "R_ALPHA_LITUSE")
1895 && rel_type == reltype_rela)
1896 {
1897 switch (rels[i].r_addend)
1898 {
1899 case LITUSE_ALPHA_ADDR: rtype = "ADDR"; break;
1900 case LITUSE_ALPHA_BASE: rtype = "BASE"; break;
1901 case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
1902 case LITUSE_ALPHA_JSR: rtype = "JSR"; break;
1903 case LITUSE_ALPHA_TLSGD: rtype = "TLSGD"; break;
1904 case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
1905 case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
1906 default: rtype = NULL;
1907 }
1908
1909 if (rtype)
1910 printf (" (%s)", rtype);
1911 else
1912 {
1913 putchar (' ');
1914 printf (_("<unknown addend: %lx>"),
1915 (unsigned long) rels[i].r_addend);
1916 res = false;
1917 }
1918 }
1919 else if (symtab_index)
1920 {
1921 if (symtab == NULL || symtab_index >= nsyms)
1922 {
1923 error (_(" bad symbol index: %08lx in reloc\n"),
1924 (unsigned long) symtab_index);
1925 res = false;
1926 }
1927 else
1928 {
1929 Elf_Internal_Sym * psym;
1930 const char * version_string;
1931 enum versioned_symbol_info sym_info;
1932 unsigned short vna_other;
1933
1934 psym = symtab + symtab_index;
1935
1936 version_string
1937 = get_symbol_version_string (filedata, is_dynsym,
1938 strtab, strtablen,
1939 symtab_index,
1940 psym,
1941 &sym_info,
1942 &vna_other);
1943
1944 printf (" ");
1945
1946 if (ELF_ST_TYPE (psym->st_info) == STT_GNU_IFUNC)
1947 {
1948 const char * name;
1949 unsigned int len;
1950 unsigned int width = is_32bit_elf ? 8 : 14;
1951
1952 /* Relocations against GNU_IFUNC symbols do not use the value
1953 of the symbol as the address to relocate against. Instead
1954 they invoke the function named by the symbol and use its
1955 result as the address for relocation.
1956
1957 To indicate this to the user, do not display the value of
1958 the symbol in the "Symbols's Value" field. Instead show
1959 its name followed by () as a hint that the symbol is
1960 invoked. */
1961
1962 if (strtab == NULL
1963 || psym->st_name == 0
1964 || psym->st_name >= strtablen)
1965 name = "??";
1966 else
1967 name = strtab + psym->st_name;
1968
1969 len = print_symbol (width, name);
1970 if (version_string)
1971 printf (sym_info == symbol_public ? "@@%s" : "@%s",
1972 version_string);
1973 printf ("()%-*s", len <= width ? (width + 1) - len : 1, " ");
1974 }
1975 else
1976 {
1977 print_vma (psym->st_value, LONG_HEX);
1978
1979 printf (is_32bit_elf ? " " : " ");
1980 }
1981
1982 if (psym->st_name == 0)
1983 {
1984 const char * sec_name = "<null>";
1985 char name_buf[40];
1986
1987 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
1988 {
1989 if (psym->st_shndx < filedata->file_header.e_shnum
1990 && filedata->section_headers != NULL)
1991 sec_name = section_name_print (filedata,
1992 filedata->section_headers
1993 + psym->st_shndx);
1994 else if (psym->st_shndx == SHN_ABS)
1995 sec_name = "ABS";
1996 else if (psym->st_shndx == SHN_COMMON)
1997 sec_name = "COMMON";
1998 else if ((filedata->file_header.e_machine == EM_MIPS
1999 && psym->st_shndx == SHN_MIPS_SCOMMON)
2000 || (filedata->file_header.e_machine == EM_TI_C6000
2001 && psym->st_shndx == SHN_TIC6X_SCOMMON))
2002 sec_name = "SCOMMON";
2003 else if (filedata->file_header.e_machine == EM_MIPS
2004 && psym->st_shndx == SHN_MIPS_SUNDEFINED)
2005 sec_name = "SUNDEF";
2006 else if ((filedata->file_header.e_machine == EM_X86_64
2007 || filedata->file_header.e_machine == EM_L1OM
2008 || filedata->file_header.e_machine == EM_K1OM)
2009 && psym->st_shndx == SHN_X86_64_LCOMMON)
2010 sec_name = "LARGE_COMMON";
2011 else if (filedata->file_header.e_machine == EM_IA_64
2012 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
2013 && psym->st_shndx == SHN_IA_64_ANSI_COMMON)
2014 sec_name = "ANSI_COM";
2015 else if (is_ia64_vms (filedata)
2016 && psym->st_shndx == SHN_IA_64_VMS_SYMVEC)
2017 sec_name = "VMS_SYMVEC";
2018 else
2019 {
2020 sprintf (name_buf, "<section 0x%x>",
2021 (unsigned int) psym->st_shndx);
2022 sec_name = name_buf;
2023 }
2024 }
2025 print_symbol (22, sec_name);
2026 }
2027 else if (strtab == NULL)
2028 printf (_("<string table index: %3ld>"), psym->st_name);
2029 else if (psym->st_name >= strtablen)
2030 {
2031 error (_("<corrupt string table index: %3ld>\n"),
2032 psym->st_name);
2033 res = false;
2034 }
2035 else
2036 {
2037 print_symbol (22, strtab + psym->st_name);
2038 if (version_string)
2039 printf (sym_info == symbol_public ? "@@%s" : "@%s",
2040 version_string);
2041 }
2042
2043 if (rel_type == reltype_rela)
2044 {
2045 uint64_t off = rels[i].r_addend;
2046
2047 if ((int64_t) off < 0)
2048 printf (" - %" PRIx64, -off);
2049 else
2050 printf (" + %" PRIx64, off);
2051 }
2052 }
2053 }
2054 else if (rel_type == reltype_rela)
2055 {
2056 uint64_t off = rels[i].r_addend;
2057
2058 printf ("%*c", is_32bit_elf ? 12 : 20, ' ');
2059 if ((int64_t) off < 0)
2060 printf ("-%" PRIx64, -off);
2061 else
2062 printf ("%" PRIx64, off);
2063 }
2064
2065 if (filedata->file_header.e_machine == EM_SPARCV9
2066 && rtype != NULL
2067 && streq (rtype, "R_SPARC_OLO10"))
2068 printf (" + %lx", (unsigned long) ELF64_R_TYPE_DATA (inf));
2069
2070 putchar ('\n');
2071
2072 if (! is_32bit_elf && filedata->file_header.e_machine == EM_MIPS)
2073 {
2074 uint64_t type2 = ELF64_MIPS_R_TYPE2 (inf);
2075 uint64_t type3 = ELF64_MIPS_R_TYPE3 (inf);
2076 const char * rtype2 = elf_mips_reloc_type (type2);
2077 const char * rtype3 = elf_mips_reloc_type (type3);
2078
2079 printf (" Type2: ");
2080
2081 if (rtype2 == NULL)
2082 printf (_("unrecognized: %-7lx"),
2083 (unsigned long) type2 & 0xffffffff);
2084 else
2085 printf ("%-17.17s", rtype2);
2086
2087 printf ("\n Type3: ");
2088
2089 if (rtype3 == NULL)
2090 printf (_("unrecognized: %-7lx"),
2091 (unsigned long) type3 & 0xffffffff);
2092 else
2093 printf ("%-17.17s", rtype3);
2094
2095 putchar ('\n');
2096 }
2097 }
2098
2099 free (rels);
2100
2101 return res;
2102 }
2103
2104 static const char *
2105 get_aarch64_dynamic_type (unsigned long type)
2106 {
2107 switch (type)
2108 {
2109 case DT_AARCH64_BTI_PLT: return "AARCH64_BTI_PLT";
2110 case DT_AARCH64_PAC_PLT: return "AARCH64_PAC_PLT";
2111 case DT_AARCH64_VARIANT_PCS: return "AARCH64_VARIANT_PCS";
2112 default:
2113 return NULL;
2114 }
2115 }
2116
2117 static const char *
2118 get_mips_dynamic_type (unsigned long type)
2119 {
2120 switch (type)
2121 {
2122 case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
2123 case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
2124 case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
2125 case DT_MIPS_IVERSION: return "MIPS_IVERSION";
2126 case DT_MIPS_FLAGS: return "MIPS_FLAGS";
2127 case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
2128 case DT_MIPS_MSYM: return "MIPS_MSYM";
2129 case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
2130 case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
2131 case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
2132 case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
2133 case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
2134 case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
2135 case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
2136 case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
2137 case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
2138 case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
2139 case DT_MIPS_RLD_MAP_REL: return "MIPS_RLD_MAP_REL";
2140 case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
2141 case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
2142 case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
2143 case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
2144 case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
2145 case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
2146 case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
2147 case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
2148 case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
2149 case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
2150 case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
2151 case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
2152 case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
2153 case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
2154 case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
2155 case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
2156 case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
2157 case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
2158 case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
2159 case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
2160 case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
2161 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
2162 case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
2163 case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
2164 case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
2165 case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
2166 case DT_MIPS_PLTGOT: return "MIPS_PLTGOT";
2167 case DT_MIPS_RWPLT: return "MIPS_RWPLT";
2168 case DT_MIPS_XHASH: return "MIPS_XHASH";
2169 default:
2170 return NULL;
2171 }
2172 }
2173
2174 static const char *
2175 get_sparc64_dynamic_type (unsigned long type)
2176 {
2177 switch (type)
2178 {
2179 case DT_SPARC_REGISTER: return "SPARC_REGISTER";
2180 default:
2181 return NULL;
2182 }
2183 }
2184
2185 static const char *
2186 get_ppc_dynamic_type (unsigned long type)
2187 {
2188 switch (type)
2189 {
2190 case DT_PPC_GOT: return "PPC_GOT";
2191 case DT_PPC_OPT: return "PPC_OPT";
2192 default:
2193 return NULL;
2194 }
2195 }
2196
2197 static const char *
2198 get_ppc64_dynamic_type (unsigned long type)
2199 {
2200 switch (type)
2201 {
2202 case DT_PPC64_GLINK: return "PPC64_GLINK";
2203 case DT_PPC64_OPD: return "PPC64_OPD";
2204 case DT_PPC64_OPDSZ: return "PPC64_OPDSZ";
2205 case DT_PPC64_OPT: return "PPC64_OPT";
2206 default:
2207 return NULL;
2208 }
2209 }
2210
2211 static const char *
2212 get_parisc_dynamic_type (unsigned long type)
2213 {
2214 switch (type)
2215 {
2216 case DT_HP_LOAD_MAP: return "HP_LOAD_MAP";
2217 case DT_HP_DLD_FLAGS: return "HP_DLD_FLAGS";
2218 case DT_HP_DLD_HOOK: return "HP_DLD_HOOK";
2219 case DT_HP_UX10_INIT: return "HP_UX10_INIT";
2220 case DT_HP_UX10_INITSZ: return "HP_UX10_INITSZ";
2221 case DT_HP_PREINIT: return "HP_PREINIT";
2222 case DT_HP_PREINITSZ: return "HP_PREINITSZ";
2223 case DT_HP_NEEDED: return "HP_NEEDED";
2224 case DT_HP_TIME_STAMP: return "HP_TIME_STAMP";
2225 case DT_HP_CHECKSUM: return "HP_CHECKSUM";
2226 case DT_HP_GST_SIZE: return "HP_GST_SIZE";
2227 case DT_HP_GST_VERSION: return "HP_GST_VERSION";
2228 case DT_HP_GST_HASHVAL: return "HP_GST_HASHVAL";
2229 case DT_HP_EPLTREL: return "HP_GST_EPLTREL";
2230 case DT_HP_EPLTRELSZ: return "HP_GST_EPLTRELSZ";
2231 case DT_HP_FILTERED: return "HP_FILTERED";
2232 case DT_HP_FILTER_TLS: return "HP_FILTER_TLS";
2233 case DT_HP_COMPAT_FILTERED: return "HP_COMPAT_FILTERED";
2234 case DT_HP_LAZYLOAD: return "HP_LAZYLOAD";
2235 case DT_HP_BIND_NOW_COUNT: return "HP_BIND_NOW_COUNT";
2236 case DT_PLT: return "PLT";
2237 case DT_PLT_SIZE: return "PLT_SIZE";
2238 case DT_DLT: return "DLT";
2239 case DT_DLT_SIZE: return "DLT_SIZE";
2240 default:
2241 return NULL;
2242 }
2243 }
2244
2245 static const char *
2246 get_ia64_dynamic_type (unsigned long type)
2247 {
2248 switch (type)
2249 {
2250 case DT_IA_64_PLT_RESERVE: return "IA_64_PLT_RESERVE";
2251 case DT_IA_64_VMS_SUBTYPE: return "VMS_SUBTYPE";
2252 case DT_IA_64_VMS_IMGIOCNT: return "VMS_IMGIOCNT";
2253 case DT_IA_64_VMS_LNKFLAGS: return "VMS_LNKFLAGS";
2254 case DT_IA_64_VMS_VIR_MEM_BLK_SIZ: return "VMS_VIR_MEM_BLK_SIZ";
2255 case DT_IA_64_VMS_IDENT: return "VMS_IDENT";
2256 case DT_IA_64_VMS_NEEDED_IDENT: return "VMS_NEEDED_IDENT";
2257 case DT_IA_64_VMS_IMG_RELA_CNT: return "VMS_IMG_RELA_CNT";
2258 case DT_IA_64_VMS_SEG_RELA_CNT: return "VMS_SEG_RELA_CNT";
2259 case DT_IA_64_VMS_FIXUP_RELA_CNT: return "VMS_FIXUP_RELA_CNT";
2260 case DT_IA_64_VMS_FIXUP_NEEDED: return "VMS_FIXUP_NEEDED";
2261 case DT_IA_64_VMS_SYMVEC_CNT: return "VMS_SYMVEC_CNT";
2262 case DT_IA_64_VMS_XLATED: return "VMS_XLATED";
2263 case DT_IA_64_VMS_STACKSIZE: return "VMS_STACKSIZE";
2264 case DT_IA_64_VMS_UNWINDSZ: return "VMS_UNWINDSZ";
2265 case DT_IA_64_VMS_UNWIND_CODSEG: return "VMS_UNWIND_CODSEG";
2266 case DT_IA_64_VMS_UNWIND_INFOSEG: return "VMS_UNWIND_INFOSEG";
2267 case DT_IA_64_VMS_LINKTIME: return "VMS_LINKTIME";
2268 case DT_IA_64_VMS_SEG_NO: return "VMS_SEG_NO";
2269 case DT_IA_64_VMS_SYMVEC_OFFSET: return "VMS_SYMVEC_OFFSET";
2270 case DT_IA_64_VMS_SYMVEC_SEG: return "VMS_SYMVEC_SEG";
2271 case DT_IA_64_VMS_UNWIND_OFFSET: return "VMS_UNWIND_OFFSET";
2272 case DT_IA_64_VMS_UNWIND_SEG: return "VMS_UNWIND_SEG";
2273 case DT_IA_64_VMS_STRTAB_OFFSET: return "VMS_STRTAB_OFFSET";
2274 case DT_IA_64_VMS_SYSVER_OFFSET: return "VMS_SYSVER_OFFSET";
2275 case DT_IA_64_VMS_IMG_RELA_OFF: return "VMS_IMG_RELA_OFF";
2276 case DT_IA_64_VMS_SEG_RELA_OFF: return "VMS_SEG_RELA_OFF";
2277 case DT_IA_64_VMS_FIXUP_RELA_OFF: return "VMS_FIXUP_RELA_OFF";
2278 case DT_IA_64_VMS_PLTGOT_OFFSET: return "VMS_PLTGOT_OFFSET";
2279 case DT_IA_64_VMS_PLTGOT_SEG: return "VMS_PLTGOT_SEG";
2280 case DT_IA_64_VMS_FPMODE: return "VMS_FPMODE";
2281 default:
2282 return NULL;
2283 }
2284 }
2285
2286 static const char *
2287 get_solaris_section_type (unsigned long type)
2288 {
2289 switch (type)
2290 {
2291 case 0x6fffffee: return "SUNW_ancillary";
2292 case 0x6fffffef: return "SUNW_capchain";
2293 case 0x6ffffff0: return "SUNW_capinfo";
2294 case 0x6ffffff1: return "SUNW_symsort";
2295 case 0x6ffffff2: return "SUNW_tlssort";
2296 case 0x6ffffff3: return "SUNW_LDYNSYM";
2297 case 0x6ffffff4: return "SUNW_dof";
2298 case 0x6ffffff5: return "SUNW_cap";
2299 case 0x6ffffff6: return "SUNW_SIGNATURE";
2300 case 0x6ffffff7: return "SUNW_ANNOTATE";
2301 case 0x6ffffff8: return "SUNW_DEBUGSTR";
2302 case 0x6ffffff9: return "SUNW_DEBUG";
2303 case 0x6ffffffa: return "SUNW_move";
2304 case 0x6ffffffb: return "SUNW_COMDAT";
2305 case 0x6ffffffc: return "SUNW_syminfo";
2306 case 0x6ffffffd: return "SUNW_verdef";
2307 case 0x6ffffffe: return "SUNW_verneed";
2308 case 0x6fffffff: return "SUNW_versym";
2309 case 0x70000000: return "SPARC_GOTDATA";
2310 default: return NULL;
2311 }
2312 }
2313
2314 static const char *
2315 get_alpha_dynamic_type (unsigned long type)
2316 {
2317 switch (type)
2318 {
2319 case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
2320 default: return NULL;
2321 }
2322 }
2323
2324 static const char *
2325 get_score_dynamic_type (unsigned long type)
2326 {
2327 switch (type)
2328 {
2329 case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
2330 case DT_SCORE_LOCAL_GOTNO: return "SCORE_LOCAL_GOTNO";
2331 case DT_SCORE_SYMTABNO: return "SCORE_SYMTABNO";
2332 case DT_SCORE_GOTSYM: return "SCORE_GOTSYM";
2333 case DT_SCORE_UNREFEXTNO: return "SCORE_UNREFEXTNO";
2334 case DT_SCORE_HIPAGENO: return "SCORE_HIPAGENO";
2335 default: return NULL;
2336 }
2337 }
2338
2339 static const char *
2340 get_tic6x_dynamic_type (unsigned long type)
2341 {
2342 switch (type)
2343 {
2344 case DT_C6000_GSYM_OFFSET: return "C6000_GSYM_OFFSET";
2345 case DT_C6000_GSTR_OFFSET: return "C6000_GSTR_OFFSET";
2346 case DT_C6000_DSBT_BASE: return "C6000_DSBT_BASE";
2347 case DT_C6000_DSBT_SIZE: return "C6000_DSBT_SIZE";
2348 case DT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
2349 case DT_C6000_DSBT_INDEX: return "C6000_DSBT_INDEX";
2350 default: return NULL;
2351 }
2352 }
2353
2354 static const char *
2355 get_nios2_dynamic_type (unsigned long type)
2356 {
2357 switch (type)
2358 {
2359 case DT_NIOS2_GP: return "NIOS2_GP";
2360 default: return NULL;
2361 }
2362 }
2363
2364 static const char *
2365 get_solaris_dynamic_type (unsigned long type)
2366 {
2367 switch (type)
2368 {
2369 case 0x6000000d: return "SUNW_AUXILIARY";
2370 case 0x6000000e: return "SUNW_RTLDINF";
2371 case 0x6000000f: return "SUNW_FILTER";
2372 case 0x60000010: return "SUNW_CAP";
2373 case 0x60000011: return "SUNW_SYMTAB";
2374 case 0x60000012: return "SUNW_SYMSZ";
2375 case 0x60000013: return "SUNW_SORTENT";
2376 case 0x60000014: return "SUNW_SYMSORT";
2377 case 0x60000015: return "SUNW_SYMSORTSZ";
2378 case 0x60000016: return "SUNW_TLSSORT";
2379 case 0x60000017: return "SUNW_TLSSORTSZ";
2380 case 0x60000018: return "SUNW_CAPINFO";
2381 case 0x60000019: return "SUNW_STRPAD";
2382 case 0x6000001a: return "SUNW_CAPCHAIN";
2383 case 0x6000001b: return "SUNW_LDMACH";
2384 case 0x6000001d: return "SUNW_CAPCHAINENT";
2385 case 0x6000001f: return "SUNW_CAPCHAINSZ";
2386 case 0x60000021: return "SUNW_PARENT";
2387 case 0x60000023: return "SUNW_ASLR";
2388 case 0x60000025: return "SUNW_RELAX";
2389 case 0x60000029: return "SUNW_NXHEAP";
2390 case 0x6000002b: return "SUNW_NXSTACK";
2391
2392 case 0x70000001: return "SPARC_REGISTER";
2393 case 0x7ffffffd: return "AUXILIARY";
2394 case 0x7ffffffe: return "USED";
2395 case 0x7fffffff: return "FILTER";
2396
2397 default: return NULL;
2398 }
2399 }
2400
2401 static const char *
2402 get_riscv_dynamic_type (unsigned long type)
2403 {
2404 switch (type)
2405 {
2406 case DT_RISCV_VARIANT_CC: return "RISCV_VARIANT_CC";
2407 default:
2408 return NULL;
2409 }
2410 }
2411
2412 static const char *
2413 get_dynamic_type (Filedata * filedata, unsigned long type)
2414 {
2415 static char buff[64];
2416
2417 switch (type)
2418 {
2419 case DT_NULL: return "NULL";
2420 case DT_NEEDED: return "NEEDED";
2421 case DT_PLTRELSZ: return "PLTRELSZ";
2422 case DT_PLTGOT: return "PLTGOT";
2423 case DT_HASH: return "HASH";
2424 case DT_STRTAB: return "STRTAB";
2425 case DT_SYMTAB: return "SYMTAB";
2426 case DT_RELA: return "RELA";
2427 case DT_RELASZ: return "RELASZ";
2428 case DT_RELAENT: return "RELAENT";
2429 case DT_STRSZ: return "STRSZ";
2430 case DT_SYMENT: return "SYMENT";
2431 case DT_INIT: return "INIT";
2432 case DT_FINI: return "FINI";
2433 case DT_SONAME: return "SONAME";
2434 case DT_RPATH: return "RPATH";
2435 case DT_SYMBOLIC: return "SYMBOLIC";
2436 case DT_REL: return "REL";
2437 case DT_RELSZ: return "RELSZ";
2438 case DT_RELENT: return "RELENT";
2439 case DT_RELR: return "RELR";
2440 case DT_RELRSZ: return "RELRSZ";
2441 case DT_RELRENT: return "RELRENT";
2442 case DT_PLTREL: return "PLTREL";
2443 case DT_DEBUG: return "DEBUG";
2444 case DT_TEXTREL: return "TEXTREL";
2445 case DT_JMPREL: return "JMPREL";
2446 case DT_BIND_NOW: return "BIND_NOW";
2447 case DT_INIT_ARRAY: return "INIT_ARRAY";
2448 case DT_FINI_ARRAY: return "FINI_ARRAY";
2449 case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
2450 case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
2451 case DT_RUNPATH: return "RUNPATH";
2452 case DT_FLAGS: return "FLAGS";
2453
2454 case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
2455 case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
2456 case DT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
2457
2458 case DT_CHECKSUM: return "CHECKSUM";
2459 case DT_PLTPADSZ: return "PLTPADSZ";
2460 case DT_MOVEENT: return "MOVEENT";
2461 case DT_MOVESZ: return "MOVESZ";
2462 case DT_FEATURE: return "FEATURE";
2463 case DT_POSFLAG_1: return "POSFLAG_1";
2464 case DT_SYMINSZ: return "SYMINSZ";
2465 case DT_SYMINENT: return "SYMINENT"; /* aka VALRNGHI */
2466
2467 case DT_ADDRRNGLO: return "ADDRRNGLO";
2468 case DT_CONFIG: return "CONFIG";
2469 case DT_DEPAUDIT: return "DEPAUDIT";
2470 case DT_AUDIT: return "AUDIT";
2471 case DT_PLTPAD: return "PLTPAD";
2472 case DT_MOVETAB: return "MOVETAB";
2473 case DT_SYMINFO: return "SYMINFO"; /* aka ADDRRNGHI */
2474
2475 case DT_VERSYM: return "VERSYM";
2476
2477 case DT_TLSDESC_GOT: return "TLSDESC_GOT";
2478 case DT_TLSDESC_PLT: return "TLSDESC_PLT";
2479 case DT_RELACOUNT: return "RELACOUNT";
2480 case DT_RELCOUNT: return "RELCOUNT";
2481 case DT_FLAGS_1: return "FLAGS_1";
2482 case DT_VERDEF: return "VERDEF";
2483 case DT_VERDEFNUM: return "VERDEFNUM";
2484 case DT_VERNEED: return "VERNEED";
2485 case DT_VERNEEDNUM: return "VERNEEDNUM";
2486
2487 case DT_AUXILIARY: return "AUXILIARY";
2488 case DT_USED: return "USED";
2489 case DT_FILTER: return "FILTER";
2490
2491 case DT_GNU_PRELINKED: return "GNU_PRELINKED";
2492 case DT_GNU_CONFLICT: return "GNU_CONFLICT";
2493 case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
2494 case DT_GNU_LIBLIST: return "GNU_LIBLIST";
2495 case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
2496 case DT_GNU_HASH: return "GNU_HASH";
2497 case DT_GNU_FLAGS_1: return "GNU_FLAGS_1";
2498
2499 default:
2500 if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
2501 {
2502 const char * result;
2503
2504 switch (filedata->file_header.e_machine)
2505 {
2506 case EM_AARCH64:
2507 result = get_aarch64_dynamic_type (type);
2508 break;
2509 case EM_MIPS:
2510 case EM_MIPS_RS3_LE:
2511 result = get_mips_dynamic_type (type);
2512 break;
2513 case EM_SPARCV9:
2514 result = get_sparc64_dynamic_type (type);
2515 break;
2516 case EM_PPC:
2517 result = get_ppc_dynamic_type (type);
2518 break;
2519 case EM_PPC64:
2520 result = get_ppc64_dynamic_type (type);
2521 break;
2522 case EM_IA_64:
2523 result = get_ia64_dynamic_type (type);
2524 break;
2525 case EM_ALPHA:
2526 result = get_alpha_dynamic_type (type);
2527 break;
2528 case EM_SCORE:
2529 result = get_score_dynamic_type (type);
2530 break;
2531 case EM_TI_C6000:
2532 result = get_tic6x_dynamic_type (type);
2533 break;
2534 case EM_ALTERA_NIOS2:
2535 result = get_nios2_dynamic_type (type);
2536 break;
2537 case EM_RISCV:
2538 result = get_riscv_dynamic_type (type);
2539 break;
2540 default:
2541 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2542 result = get_solaris_dynamic_type (type);
2543 else
2544 result = NULL;
2545 break;
2546 }
2547
2548 if (result != NULL)
2549 return result;
2550
2551 snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
2552 }
2553 else if (((type >= DT_LOOS) && (type <= DT_HIOS))
2554 || (filedata->file_header.e_machine == EM_PARISC
2555 && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
2556 {
2557 const char * result;
2558
2559 switch (filedata->file_header.e_machine)
2560 {
2561 case EM_PARISC:
2562 result = get_parisc_dynamic_type (type);
2563 break;
2564 case EM_IA_64:
2565 result = get_ia64_dynamic_type (type);
2566 break;
2567 default:
2568 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2569 result = get_solaris_dynamic_type (type);
2570 else
2571 result = NULL;
2572 break;
2573 }
2574
2575 if (result != NULL)
2576 return result;
2577
2578 snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
2579 type);
2580 }
2581 else
2582 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
2583
2584 return buff;
2585 }
2586 }
2587
2588 static bool get_program_headers (Filedata *);
2589 static bool get_dynamic_section (Filedata *);
2590
2591 static void
2592 locate_dynamic_section (Filedata *filedata)
2593 {
2594 unsigned long dynamic_addr = 0;
2595 uint64_t dynamic_size = 0;
2596
2597 if (filedata->file_header.e_phnum != 0
2598 && get_program_headers (filedata))
2599 {
2600 Elf_Internal_Phdr *segment;
2601 unsigned int i;
2602
2603 for (i = 0, segment = filedata->program_headers;
2604 i < filedata->file_header.e_phnum;
2605 i++, segment++)
2606 {
2607 if (segment->p_type == PT_DYNAMIC)
2608 {
2609 dynamic_addr = segment->p_offset;
2610 dynamic_size = segment->p_filesz;
2611
2612 if (filedata->section_headers != NULL)
2613 {
2614 Elf_Internal_Shdr *sec;
2615
2616 sec = find_section (filedata, ".dynamic");
2617 if (sec != NULL)
2618 {
2619 if (sec->sh_size == 0
2620 || sec->sh_type == SHT_NOBITS)
2621 {
2622 dynamic_addr = 0;
2623 dynamic_size = 0;
2624 }
2625 else
2626 {
2627 dynamic_addr = sec->sh_offset;
2628 dynamic_size = sec->sh_size;
2629 }
2630 }
2631 }
2632
2633 if (dynamic_addr > filedata->file_size
2634 || (dynamic_size > filedata->file_size - dynamic_addr))
2635 {
2636 dynamic_addr = 0;
2637 dynamic_size = 0;
2638 }
2639 break;
2640 }
2641 }
2642 }
2643 filedata->dynamic_addr = dynamic_addr;
2644 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
2645 }
2646
2647 static bool
2648 is_pie (Filedata *filedata)
2649 {
2650 Elf_Internal_Dyn *entry;
2651
2652 if (filedata->dynamic_size == 0)
2653 locate_dynamic_section (filedata);
2654 if (filedata->dynamic_size <= 1)
2655 return false;
2656
2657 if (!get_dynamic_section (filedata))
2658 return false;
2659
2660 for (entry = filedata->dynamic_section;
2661 entry < filedata->dynamic_section + filedata->dynamic_nent;
2662 entry++)
2663 {
2664 if (entry->d_tag == DT_FLAGS_1)
2665 {
2666 if ((entry->d_un.d_val & DF_1_PIE) != 0)
2667 return true;
2668 break;
2669 }
2670 }
2671 return false;
2672 }
2673
2674 static char *
2675 get_file_type (Filedata *filedata)
2676 {
2677 unsigned e_type = filedata->file_header.e_type;
2678 static char buff[64];
2679
2680 switch (e_type)
2681 {
2682 case ET_NONE: return _("NONE (None)");
2683 case ET_REL: return _("REL (Relocatable file)");
2684 case ET_EXEC: return _("EXEC (Executable file)");
2685 case ET_DYN:
2686 if (is_pie (filedata))
2687 return _("DYN (Position-Independent Executable file)");
2688 else
2689 return _("DYN (Shared object file)");
2690 case ET_CORE: return _("CORE (Core file)");
2691
2692 default:
2693 if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
2694 snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
2695 else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
2696 snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
2697 else
2698 snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
2699 return buff;
2700 }
2701 }
2702
2703 static char *
2704 get_machine_name (unsigned e_machine)
2705 {
2706 static char buff[64]; /* XXX */
2707
2708 switch (e_machine)
2709 {
2710 /* Please keep this switch table sorted by increasing EM_ value. */
2711 /* 0 */
2712 case EM_NONE: return _("None");
2713 case EM_M32: return "WE32100";
2714 case EM_SPARC: return "Sparc";
2715 case EM_386: return "Intel 80386";
2716 case EM_68K: return "MC68000";
2717 case EM_88K: return "MC88000";
2718 case EM_IAMCU: return "Intel MCU";
2719 case EM_860: return "Intel 80860";
2720 case EM_MIPS: return "MIPS R3000";
2721 case EM_S370: return "IBM System/370";
2722 /* 10 */
2723 case EM_MIPS_RS3_LE: return "MIPS R4000 big-endian";
2724 case EM_OLD_SPARCV9: return "Sparc v9 (old)";
2725 case EM_PARISC: return "HPPA";
2726 case EM_VPP550: return "Fujitsu VPP500";
2727 case EM_SPARC32PLUS: return "Sparc v8+" ;
2728 case EM_960: return "Intel 80960";
2729 case EM_PPC: return "PowerPC";
2730 /* 20 */
2731 case EM_PPC64: return "PowerPC64";
2732 case EM_S390_OLD:
2733 case EM_S390: return "IBM S/390";
2734 case EM_SPU: return "SPU";
2735 /* 30 */
2736 case EM_V800: return "Renesas V850 (using RH850 ABI)";
2737 case EM_FR20: return "Fujitsu FR20";
2738 case EM_RH32: return "TRW RH32";
2739 case EM_MCORE: return "MCORE";
2740 /* 40 */
2741 case EM_ARM: return "ARM";
2742 case EM_OLD_ALPHA: return "Digital Alpha (old)";
2743 case EM_SH: return "Renesas / SuperH SH";
2744 case EM_SPARCV9: return "Sparc v9";
2745 case EM_TRICORE: return "Siemens Tricore";
2746 case EM_ARC: return "ARC";
2747 case EM_H8_300: return "Renesas H8/300";
2748 case EM_H8_300H: return "Renesas H8/300H";
2749 case EM_H8S: return "Renesas H8S";
2750 case EM_H8_500: return "Renesas H8/500";
2751 /* 50 */
2752 case EM_IA_64: return "Intel IA-64";
2753 case EM_MIPS_X: return "Stanford MIPS-X";
2754 case EM_COLDFIRE: return "Motorola Coldfire";
2755 case EM_68HC12: return "Motorola MC68HC12 Microcontroller";
2756 case EM_MMA: return "Fujitsu Multimedia Accelerator";
2757 case EM_PCP: return "Siemens PCP";
2758 case EM_NCPU: return "Sony nCPU embedded RISC processor";
2759 case EM_NDR1: return "Denso NDR1 microprocesspr";
2760 case EM_STARCORE: return "Motorola Star*Core processor";
2761 case EM_ME16: return "Toyota ME16 processor";
2762 /* 60 */
2763 case EM_ST100: return "STMicroelectronics ST100 processor";
2764 case EM_TINYJ: return "Advanced Logic Corp. TinyJ embedded processor";
2765 case EM_X86_64: return "Advanced Micro Devices X86-64";
2766 case EM_PDSP: return "Sony DSP processor";
2767 case EM_PDP10: return "Digital Equipment Corp. PDP-10";
2768 case EM_PDP11: return "Digital Equipment Corp. PDP-11";
2769 case EM_FX66: return "Siemens FX66 microcontroller";
2770 case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 bit microcontroller";
2771 case EM_ST7: return "STMicroelectronics ST7 8-bit microcontroller";
2772 case EM_68HC16: return "Motorola MC68HC16 Microcontroller";
2773 /* 70 */
2774 case EM_68HC11: return "Motorola MC68HC11 Microcontroller";
2775 case EM_68HC08: return "Motorola MC68HC08 Microcontroller";
2776 case EM_68HC05: return "Motorola MC68HC05 Microcontroller";
2777 case EM_SVX: return "Silicon Graphics SVx";
2778 case EM_ST19: return "STMicroelectronics ST19 8-bit microcontroller";
2779 case EM_VAX: return "Digital VAX";
2780 case EM_CRIS: return "Axis Communications 32-bit embedded processor";
2781 case EM_JAVELIN: return "Infineon Technologies 32-bit embedded cpu";
2782 case EM_FIREPATH: return "Element 14 64-bit DSP processor";
2783 case EM_ZSP: return "LSI Logic's 16-bit DSP processor";
2784 /* 80 */
2785 case EM_MMIX: return "Donald Knuth's educational 64-bit processor";
2786 case EM_HUANY: return "Harvard Universitys's machine-independent object format";
2787 case EM_PRISM: return "Vitesse Prism";
2788 case EM_AVR_OLD:
2789 case EM_AVR: return "Atmel AVR 8-bit microcontroller";
2790 case EM_CYGNUS_FR30:
2791 case EM_FR30: return "Fujitsu FR30";
2792 case EM_CYGNUS_D10V:
2793 case EM_D10V: return "d10v";
2794 case EM_CYGNUS_D30V:
2795 case EM_D30V: return "d30v";
2796 case EM_CYGNUS_V850:
2797 case EM_V850: return "Renesas V850";
2798 case EM_CYGNUS_M32R:
2799 case EM_M32R: return "Renesas M32R (formerly Mitsubishi M32r)";
2800 case EM_CYGNUS_MN10300:
2801 case EM_MN10300: return "mn10300";
2802 /* 90 */
2803 case EM_CYGNUS_MN10200:
2804 case EM_MN10200: return "mn10200";
2805 case EM_PJ: return "picoJava";
2806 case EM_OR1K: return "OpenRISC 1000";
2807 case EM_ARC_COMPACT: return "ARCompact";
2808 case EM_XTENSA_OLD:
2809 case EM_XTENSA: return "Tensilica Xtensa Processor";
2810 case EM_VIDEOCORE: return "Alphamosaic VideoCore processor";
2811 case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor";
2812 case EM_NS32K: return "National Semiconductor 32000 series";
2813 case EM_TPC: return "Tenor Network TPC processor";
2814 case EM_SNP1K: return "Trebia SNP 1000 processor";
2815 /* 100 */
2816 case EM_ST200: return "STMicroelectronics ST200 microcontroller";
2817 case EM_IP2K_OLD:
2818 case EM_IP2K: return "Ubicom IP2xxx 8-bit microcontrollers";
2819 case EM_MAX: return "MAX Processor";
2820 case EM_CR: return "National Semiconductor CompactRISC";
2821 case EM_F2MC16: return "Fujitsu F2MC16";
2822 case EM_MSP430: return "Texas Instruments msp430 microcontroller";
2823 case EM_BLACKFIN: return "Analog Devices Blackfin";
2824 case EM_SE_C33: return "S1C33 Family of Seiko Epson processors";
2825 case EM_SEP: return "Sharp embedded microprocessor";
2826 case EM_ARCA: return "Arca RISC microprocessor";
2827 /* 110 */
2828 case EM_UNICORE: return "Unicore";
2829 case EM_EXCESS: return "eXcess 16/32/64-bit configurable embedded CPU";
2830 case EM_DXP: return "Icera Semiconductor Inc. Deep Execution Processor";
2831 case EM_ALTERA_NIOS2: return "Altera Nios II";
2832 case EM_CRX: return "National Semiconductor CRX microprocessor";
2833 case EM_XGATE: return "Motorola XGATE embedded processor";
2834 case EM_C166:
2835 case EM_XC16X: return "Infineon Technologies xc16x";
2836 case EM_M16C: return "Renesas M16C series microprocessors";
2837 case EM_DSPIC30F: return "Microchip Technology dsPIC30F Digital Signal Controller";
2838 case EM_CE: return "Freescale Communication Engine RISC core";
2839 /* 120 */
2840 case EM_M32C: return "Renesas M32c";
2841 /* 130 */
2842 case EM_TSK3000: return "Altium TSK3000 core";
2843 case EM_RS08: return "Freescale RS08 embedded processor";
2844 case EM_ECOG2: return "Cyan Technology eCOG2 microprocessor";
2845 case EM_SCORE: return "SUNPLUS S+Core";
2846 case EM_DSP24: return "New Japan Radio (NJR) 24-bit DSP Processor";
2847 case EM_VIDEOCORE3: return "Broadcom VideoCore III processor";
2848 case EM_LATTICEMICO32: return "Lattice Mico32";
2849 case EM_SE_C17: return "Seiko Epson C17 family";
2850 /* 140 */
2851 case EM_TI_C6000: return "Texas Instruments TMS320C6000 DSP family";
2852 case EM_TI_C2000: return "Texas Instruments TMS320C2000 DSP family";
2853 case EM_TI_C5500: return "Texas Instruments TMS320C55x DSP family";
2854 case EM_TI_PRU: return "TI PRU I/O processor";
2855 /* 160 */
2856 case EM_MMDSP_PLUS: return "STMicroelectronics 64bit VLIW Data Signal Processor";
2857 case EM_CYPRESS_M8C: return "Cypress M8C microprocessor";
2858 case EM_R32C: return "Renesas R32C series microprocessors";
2859 case EM_TRIMEDIA: return "NXP Semiconductors TriMedia architecture family";
2860 case EM_QDSP6: return "QUALCOMM DSP6 Processor";
2861 case EM_8051: return "Intel 8051 and variants";
2862 case EM_STXP7X: return "STMicroelectronics STxP7x family";
2863 case EM_NDS32: return "Andes Technology compact code size embedded RISC processor family";
2864 case EM_ECOG1X: return "Cyan Technology eCOG1X family";
2865 case EM_MAXQ30: return "Dallas Semiconductor MAXQ30 Core microcontrollers";
2866 /* 170 */
2867 case EM_XIMO16: return "New Japan Radio (NJR) 16-bit DSP Processor";
2868 case EM_MANIK: return "M2000 Reconfigurable RISC Microprocessor";
2869 case EM_CRAYNV2: return "Cray Inc. NV2 vector architecture";
2870 case EM_RX: return "Renesas RX";
2871 case EM_METAG: return "Imagination Technologies Meta processor architecture";
2872 case EM_MCST_ELBRUS: return "MCST Elbrus general purpose hardware architecture";
2873 case EM_ECOG16: return "Cyan Technology eCOG16 family";
2874 case EM_CR16:
2875 case EM_MICROBLAZE:
2876 case EM_MICROBLAZE_OLD: return "Xilinx MicroBlaze";
2877 case EM_ETPU: return "Freescale Extended Time Processing Unit";
2878 case EM_SLE9X: return "Infineon Technologies SLE9X core";
2879 /* 180 */
2880 case EM_L1OM: return "Intel L1OM";
2881 case EM_K1OM: return "Intel K1OM";
2882 case EM_INTEL182: return "Intel (reserved)";
2883 case EM_AARCH64: return "AArch64";
2884 case EM_ARM184: return "ARM (reserved)";
2885 case EM_AVR32: return "Atmel Corporation 32-bit microprocessor";
2886 case EM_STM8: return "STMicroeletronics STM8 8-bit microcontroller";
2887 case EM_TILE64: return "Tilera TILE64 multicore architecture family";
2888 case EM_TILEPRO: return "Tilera TILEPro multicore architecture family";
2889 /* 190 */
2890 case EM_CUDA: return "NVIDIA CUDA architecture";
2891 case EM_TILEGX: return "Tilera TILE-Gx multicore architecture family";
2892 case EM_CLOUDSHIELD: return "CloudShield architecture family";
2893 case EM_COREA_1ST: return "KIPO-KAIST Core-A 1st generation processor family";
2894 case EM_COREA_2ND: return "KIPO-KAIST Core-A 2nd generation processor family";
2895 case EM_ARC_COMPACT2: return "ARCv2";
2896 case EM_OPEN8: return "Open8 8-bit RISC soft processor core";
2897 case EM_RL78: return "Renesas RL78";
2898 case EM_VIDEOCORE5: return "Broadcom VideoCore V processor";
2899 case EM_78K0R: return "Renesas 78K0R";
2900 /* 200 */
2901 case EM_56800EX: return "Freescale 56800EX Digital Signal Controller (DSC)";
2902 case EM_BA1: return "Beyond BA1 CPU architecture";
2903 case EM_BA2: return "Beyond BA2 CPU architecture";
2904 case EM_XCORE: return "XMOS xCORE processor family";
2905 case EM_MCHP_PIC: return "Microchip 8-bit PIC(r) family";
2906 case EM_INTELGT: return "Intel Graphics Technology";
2907 /* 210 */
2908 case EM_KM32: return "KM211 KM32 32-bit processor";
2909 case EM_KMX32: return "KM211 KMX32 32-bit processor";
2910 case EM_KMX16: return "KM211 KMX16 16-bit processor";
2911 case EM_KMX8: return "KM211 KMX8 8-bit processor";
2912 case EM_KVARC: return "KM211 KVARC processor";
2913 case EM_CDP: return "Paneve CDP architecture family";
2914 case EM_COGE: return "Cognitive Smart Memory Processor";
2915 case EM_COOL: return "Bluechip Systems CoolEngine";
2916 case EM_NORC: return "Nanoradio Optimized RISC";
2917 case EM_CSR_KALIMBA: return "CSR Kalimba architecture family";
2918 /* 220 */
2919 case EM_Z80: return "Zilog Z80";
2920 case EM_VISIUM: return "CDS VISIUMcore processor";
2921 case EM_FT32: return "FTDI Chip FT32";
2922 case EM_MOXIE: return "Moxie";
2923 case EM_AMDGPU: return "AMD GPU";
2924 /* 230 (all reserved) */
2925 /* 240 */
2926 case EM_RISCV: return "RISC-V";
2927 case EM_LANAI: return "Lanai 32-bit processor";
2928 case EM_CEVA: return "CEVA Processor Architecture Family";
2929 case EM_CEVA_X2: return "CEVA X2 Processor Family";
2930 case EM_BPF: return "Linux BPF";
2931 case EM_GRAPHCORE_IPU: return "Graphcore Intelligent Processing Unit";
2932 case EM_IMG1: return "Imagination Technologies";
2933 /* 250 */
2934 case EM_NFP: return "Netronome Flow Processor";
2935 case EM_VE: return "NEC Vector Engine";
2936 case EM_CSKY: return "C-SKY";
2937 case EM_ARC_COMPACT3_64: return "Synopsys ARCv2.3 64-bit";
2938 case EM_MCS6502: return "MOS Technology MCS 6502 processor";
2939 case EM_ARC_COMPACT3: return "Synopsys ARCv2.3 32-bit";
2940 case EM_KVX: return "Kalray VLIW core of the MPPA processor family";
2941 case EM_65816: return "WDC 65816/65C816";
2942 case EM_LOONGARCH: return "LoongArch";
2943 case EM_KF32: return "ChipON KungFu32";
2944
2945 /* Large numbers... */
2946 case EM_MT: return "Morpho Techologies MT processor";
2947 case EM_ALPHA: return "Alpha";
2948 case EM_WEBASSEMBLY: return "Web Assembly";
2949 case EM_DLX: return "OpenDLX";
2950 case EM_XSTORMY16: return "Sanyo XStormy16 CPU core";
2951 case EM_IQ2000: return "Vitesse IQ2000";
2952 case EM_M32C_OLD:
2953 case EM_NIOS32: return "Altera Nios";
2954 case EM_CYGNUS_MEP: return "Toshiba MeP Media Engine";
2955 case EM_ADAPTEVA_EPIPHANY: return "Adapteva EPIPHANY";
2956 case EM_CYGNUS_FRV: return "Fujitsu FR-V";
2957 case EM_S12Z: return "Freescale S12Z";
2958
2959 default:
2960 snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
2961 return buff;
2962 }
2963 }
2964
2965 static void
2966 decode_ARC_machine_flags (unsigned e_flags, unsigned e_machine, char buf[])
2967 {
2968 /* ARC has two machine types EM_ARC_COMPACT and EM_ARC_COMPACT2. Some
2969 other compilers don't specify an architecture type in the e_flags, and
2970 instead use EM_ARC_COMPACT for old ARC600, ARC601, and ARC700
2971 architectures, and switch to EM_ARC_COMPACT2 for newer ARCEM and ARCHS
2972 architectures.
2973
2974 Th GNU tools follows this use of EM_ARC_COMPACT and EM_ARC_COMPACT2,
2975 but also sets a specific architecture type in the e_flags field.
2976
2977 However, when decoding the flags we don't worry if we see an
2978 unexpected pairing, for example EM_ARC_COMPACT machine type, with
2979 ARCEM architecture type. */
2980
2981 switch (e_flags & EF_ARC_MACH_MSK)
2982 {
2983 /* We only expect these to occur for EM_ARC_COMPACT2. */
2984 case EF_ARC_CPU_ARCV2EM:
2985 strcat (buf, ", ARC EM");
2986 break;
2987 case EF_ARC_CPU_ARCV2HS:
2988 strcat (buf, ", ARC HS");
2989 break;
2990
2991 /* We only expect these to occur for EM_ARC_COMPACT. */
2992 case E_ARC_MACH_ARC600:
2993 strcat (buf, ", ARC600");
2994 break;
2995 case E_ARC_MACH_ARC601:
2996 strcat (buf, ", ARC601");
2997 break;
2998 case E_ARC_MACH_ARC700:
2999 strcat (buf, ", ARC700");
3000 break;
3001
3002 /* The only times we should end up here are (a) A corrupt ELF, (b) A
3003 new ELF with new architecture being read by an old version of
3004 readelf, or (c) An ELF built with non-GNU compiler that does not
3005 set the architecture in the e_flags. */
3006 default:
3007 if (e_machine == EM_ARC_COMPACT)
3008 strcat (buf, ", Unknown ARCompact");
3009 else
3010 strcat (buf, ", Unknown ARC");
3011 break;
3012 }
3013
3014 switch (e_flags & EF_ARC_OSABI_MSK)
3015 {
3016 case E_ARC_OSABI_ORIG:
3017 strcat (buf, ", (ABI:legacy)");
3018 break;
3019 case E_ARC_OSABI_V2:
3020 strcat (buf, ", (ABI:v2)");
3021 break;
3022 /* Only upstream 3.9+ kernels will support ARCv2 ISA. */
3023 case E_ARC_OSABI_V3:
3024 strcat (buf, ", v3 no-legacy-syscalls ABI");
3025 break;
3026 case E_ARC_OSABI_V4:
3027 strcat (buf, ", v4 ABI");
3028 break;
3029 default:
3030 strcat (buf, ", unrecognised ARC OSABI flag");
3031 break;
3032 }
3033 }
3034
3035 static void
3036 decode_ARM_machine_flags (unsigned e_flags, char buf[])
3037 {
3038 unsigned eabi;
3039 bool unknown = false;
3040
3041 eabi = EF_ARM_EABI_VERSION (e_flags);
3042 e_flags &= ~ EF_ARM_EABIMASK;
3043
3044 /* Handle "generic" ARM flags. */
3045 if (e_flags & EF_ARM_RELEXEC)
3046 {
3047 strcat (buf, ", relocatable executable");
3048 e_flags &= ~ EF_ARM_RELEXEC;
3049 }
3050
3051 if (e_flags & EF_ARM_PIC)
3052 {
3053 strcat (buf, ", position independent");
3054 e_flags &= ~ EF_ARM_PIC;
3055 }
3056
3057 /* Now handle EABI specific flags. */
3058 switch (eabi)
3059 {
3060 default:
3061 strcat (buf, ", <unrecognized EABI>");
3062 if (e_flags)
3063 unknown = true;
3064 break;
3065
3066 case EF_ARM_EABI_VER1:
3067 strcat (buf, ", Version1 EABI");
3068 while (e_flags)
3069 {
3070 unsigned flag;
3071
3072 /* Process flags one bit at a time. */
3073 flag = e_flags & - e_flags;
3074 e_flags &= ~ flag;
3075
3076 switch (flag)
3077 {
3078 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
3079 strcat (buf, ", sorted symbol tables");
3080 break;
3081
3082 default:
3083 unknown = true;
3084 break;
3085 }
3086 }
3087 break;
3088
3089 case EF_ARM_EABI_VER2:
3090 strcat (buf, ", Version2 EABI");
3091 while (e_flags)
3092 {
3093 unsigned flag;
3094
3095 /* Process flags one bit at a time. */
3096 flag = e_flags & - e_flags;
3097 e_flags &= ~ flag;
3098
3099 switch (flag)
3100 {
3101 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
3102 strcat (buf, ", sorted symbol tables");
3103 break;
3104
3105 case EF_ARM_DYNSYMSUSESEGIDX:
3106 strcat (buf, ", dynamic symbols use segment index");
3107 break;
3108
3109 case EF_ARM_MAPSYMSFIRST:
3110 strcat (buf, ", mapping symbols precede others");
3111 break;
3112
3113 default:
3114 unknown = true;
3115 break;
3116 }
3117 }
3118 break;
3119
3120 case EF_ARM_EABI_VER3:
3121 strcat (buf, ", Version3 EABI");
3122 break;
3123
3124 case EF_ARM_EABI_VER4:
3125 strcat (buf, ", Version4 EABI");
3126 while (e_flags)
3127 {
3128 unsigned flag;
3129
3130 /* Process flags one bit at a time. */
3131 flag = e_flags & - e_flags;
3132 e_flags &= ~ flag;
3133
3134 switch (flag)
3135 {
3136 case EF_ARM_BE8:
3137 strcat (buf, ", BE8");
3138 break;
3139
3140 case EF_ARM_LE8:
3141 strcat (buf, ", LE8");
3142 break;
3143
3144 default:
3145 unknown = true;
3146 break;
3147 }
3148 }
3149 break;
3150
3151 case EF_ARM_EABI_VER5:
3152 strcat (buf, ", Version5 EABI");
3153 while (e_flags)
3154 {
3155 unsigned flag;
3156
3157 /* Process flags one bit at a time. */
3158 flag = e_flags & - e_flags;
3159 e_flags &= ~ flag;
3160
3161 switch (flag)
3162 {
3163 case EF_ARM_BE8:
3164 strcat (buf, ", BE8");
3165 break;
3166
3167 case EF_ARM_LE8:
3168 strcat (buf, ", LE8");
3169 break;
3170
3171 case EF_ARM_ABI_FLOAT_SOFT: /* Conflicts with EF_ARM_SOFT_FLOAT. */
3172 strcat (buf, ", soft-float ABI");
3173 break;
3174
3175 case EF_ARM_ABI_FLOAT_HARD: /* Conflicts with EF_ARM_VFP_FLOAT. */
3176 strcat (buf, ", hard-float ABI");
3177 break;
3178
3179 default:
3180 unknown = true;
3181 break;
3182 }
3183 }
3184 break;
3185
3186 case EF_ARM_EABI_UNKNOWN:
3187 strcat (buf, ", GNU EABI");
3188 while (e_flags)
3189 {
3190 unsigned flag;
3191
3192 /* Process flags one bit at a time. */
3193 flag = e_flags & - e_flags;
3194 e_flags &= ~ flag;
3195
3196 switch (flag)
3197 {
3198 case EF_ARM_INTERWORK:
3199 strcat (buf, ", interworking enabled");
3200 break;
3201
3202 case EF_ARM_APCS_26:
3203 strcat (buf, ", uses APCS/26");
3204 break;
3205
3206 case EF_ARM_APCS_FLOAT:
3207 strcat (buf, ", uses APCS/float");
3208 break;
3209
3210 case EF_ARM_PIC:
3211 strcat (buf, ", position independent");
3212 break;
3213
3214 case EF_ARM_ALIGN8:
3215 strcat (buf, ", 8 bit structure alignment");
3216 break;
3217
3218 case EF_ARM_NEW_ABI:
3219 strcat (buf, ", uses new ABI");
3220 break;
3221
3222 case EF_ARM_OLD_ABI:
3223 strcat (buf, ", uses old ABI");
3224 break;
3225
3226 case EF_ARM_SOFT_FLOAT:
3227 strcat (buf, ", software FP");
3228 break;
3229
3230 case EF_ARM_VFP_FLOAT:
3231 strcat (buf, ", VFP");
3232 break;
3233
3234 case EF_ARM_MAVERICK_FLOAT:
3235 strcat (buf, ", Maverick FP");
3236 break;
3237
3238 default:
3239 unknown = true;
3240 break;
3241 }
3242 }
3243 }
3244
3245 if (unknown)
3246 strcat (buf,_(", <unknown>"));
3247 }
3248
3249 static void
3250 decode_AVR_machine_flags (unsigned e_flags, char buf[], size_t size)
3251 {
3252 --size; /* Leave space for null terminator. */
3253
3254 switch (e_flags & EF_AVR_MACH)
3255 {
3256 case E_AVR_MACH_AVR1:
3257 strncat (buf, ", avr:1", size);
3258 break;
3259 case E_AVR_MACH_AVR2:
3260 strncat (buf, ", avr:2", size);
3261 break;
3262 case E_AVR_MACH_AVR25:
3263 strncat (buf, ", avr:25", size);
3264 break;
3265 case E_AVR_MACH_AVR3:
3266 strncat (buf, ", avr:3", size);
3267 break;
3268 case E_AVR_MACH_AVR31:
3269 strncat (buf, ", avr:31", size);
3270 break;
3271 case E_AVR_MACH_AVR35:
3272 strncat (buf, ", avr:35", size);
3273 break;
3274 case E_AVR_MACH_AVR4:
3275 strncat (buf, ", avr:4", size);
3276 break;
3277 case E_AVR_MACH_AVR5:
3278 strncat (buf, ", avr:5", size);
3279 break;
3280 case E_AVR_MACH_AVR51:
3281 strncat (buf, ", avr:51", size);
3282 break;
3283 case E_AVR_MACH_AVR6:
3284 strncat (buf, ", avr:6", size);
3285 break;
3286 case E_AVR_MACH_AVRTINY:
3287 strncat (buf, ", avr:100", size);
3288 break;
3289 case E_AVR_MACH_XMEGA1:
3290 strncat (buf, ", avr:101", size);
3291 break;
3292 case E_AVR_MACH_XMEGA2:
3293 strncat (buf, ", avr:102", size);
3294 break;
3295 case E_AVR_MACH_XMEGA3:
3296 strncat (buf, ", avr:103", size);
3297 break;
3298 case E_AVR_MACH_XMEGA4:
3299 strncat (buf, ", avr:104", size);
3300 break;
3301 case E_AVR_MACH_XMEGA5:
3302 strncat (buf, ", avr:105", size);
3303 break;
3304 case E_AVR_MACH_XMEGA6:
3305 strncat (buf, ", avr:106", size);
3306 break;
3307 case E_AVR_MACH_XMEGA7:
3308 strncat (buf, ", avr:107", size);
3309 break;
3310 default:
3311 strncat (buf, ", avr:<unknown>", size);
3312 break;
3313 }
3314
3315 size -= strlen (buf);
3316 if (e_flags & EF_AVR_LINKRELAX_PREPARED)
3317 strncat (buf, ", link-relax", size);
3318 }
3319
3320 static void
3321 decode_NDS32_machine_flags (unsigned e_flags, char buf[], size_t size)
3322 {
3323 unsigned abi;
3324 unsigned arch;
3325 unsigned config;
3326 unsigned version;
3327 bool has_fpu = false;
3328 unsigned int r = 0;
3329
3330 static const char *ABI_STRINGS[] =
3331 {
3332 "ABI v0", /* use r5 as return register; only used in N1213HC */
3333 "ABI v1", /* use r0 as return register */
3334 "ABI v2", /* use r0 as return register and don't reserve 24 bytes for arguments */
3335 "ABI v2fp", /* for FPU */
3336 "AABI",
3337 "ABI2 FP+"
3338 };
3339 static const char *VER_STRINGS[] =
3340 {
3341 "Andes ELF V1.3 or older",
3342 "Andes ELF V1.3.1",
3343 "Andes ELF V1.4"
3344 };
3345 static const char *ARCH_STRINGS[] =
3346 {
3347 "",
3348 "Andes Star v1.0",
3349 "Andes Star v2.0",
3350 "Andes Star v3.0",
3351 "Andes Star v3.0m"
3352 };
3353
3354 abi = EF_NDS_ABI & e_flags;
3355 arch = EF_NDS_ARCH & e_flags;
3356 config = EF_NDS_INST & e_flags;
3357 version = EF_NDS32_ELF_VERSION & e_flags;
3358
3359 memset (buf, 0, size);
3360
3361 switch (abi)
3362 {
3363 case E_NDS_ABI_V0:
3364 case E_NDS_ABI_V1:
3365 case E_NDS_ABI_V2:
3366 case E_NDS_ABI_V2FP:
3367 case E_NDS_ABI_AABI:
3368 case E_NDS_ABI_V2FP_PLUS:
3369 /* In case there are holes in the array. */
3370 r += snprintf (buf + r, size - r, ", %s", ABI_STRINGS[abi >> EF_NDS_ABI_SHIFT]);
3371 break;
3372
3373 default:
3374 r += snprintf (buf + r, size - r, ", <unrecognized ABI>");
3375 break;
3376 }
3377
3378 switch (version)
3379 {
3380 case E_NDS32_ELF_VER_1_2:
3381 case E_NDS32_ELF_VER_1_3:
3382 case E_NDS32_ELF_VER_1_4:
3383 r += snprintf (buf + r, size - r, ", %s", VER_STRINGS[version >> EF_NDS32_ELF_VERSION_SHIFT]);
3384 break;
3385
3386 default:
3387 r += snprintf (buf + r, size - r, ", <unrecognized ELF version number>");
3388 break;
3389 }
3390
3391 if (E_NDS_ABI_V0 == abi)
3392 {
3393 /* OLD ABI; only used in N1213HC, has performance extension 1. */
3394 r += snprintf (buf + r, size - r, ", Andes Star v1.0, N1213HC, MAC, PERF1");
3395 if (arch == E_NDS_ARCH_STAR_V1_0)
3396 r += snprintf (buf + r, size -r, ", 16b"); /* has 16-bit instructions */
3397 return;
3398 }
3399
3400 switch (arch)
3401 {
3402 case E_NDS_ARCH_STAR_V1_0:
3403 case E_NDS_ARCH_STAR_V2_0:
3404 case E_NDS_ARCH_STAR_V3_0:
3405 case E_NDS_ARCH_STAR_V3_M:
3406 r += snprintf (buf + r, size - r, ", %s", ARCH_STRINGS[arch >> EF_NDS_ARCH_SHIFT]);
3407 break;
3408
3409 default:
3410 r += snprintf (buf + r, size - r, ", <unrecognized architecture>");
3411 /* ARCH version determines how the e_flags are interpreted.
3412 If it is unknown, we cannot proceed. */
3413 return;
3414 }
3415
3416 /* Newer ABI; Now handle architecture specific flags. */
3417 if (arch == E_NDS_ARCH_STAR_V1_0)
3418 {
3419 if (config & E_NDS32_HAS_MFUSR_PC_INST)
3420 r += snprintf (buf + r, size -r, ", MFUSR_PC");
3421
3422 if (!(config & E_NDS32_HAS_NO_MAC_INST))
3423 r += snprintf (buf + r, size -r, ", MAC");
3424
3425 if (config & E_NDS32_HAS_DIV_INST)
3426 r += snprintf (buf + r, size -r, ", DIV");
3427
3428 if (config & E_NDS32_HAS_16BIT_INST)
3429 r += snprintf (buf + r, size -r, ", 16b");
3430 }
3431 else
3432 {
3433 if (config & E_NDS32_HAS_MFUSR_PC_INST)
3434 {
3435 if (version <= E_NDS32_ELF_VER_1_3)
3436 r += snprintf (buf + r, size -r, ", [B8]");
3437 else
3438 r += snprintf (buf + r, size -r, ", EX9");
3439 }
3440
3441 if (config & E_NDS32_HAS_MAC_DX_INST)
3442 r += snprintf (buf + r, size -r, ", MAC_DX");
3443
3444 if (config & E_NDS32_HAS_DIV_DX_INST)
3445 r += snprintf (buf + r, size -r, ", DIV_DX");
3446
3447 if (config & E_NDS32_HAS_16BIT_INST)
3448 {
3449 if (version <= E_NDS32_ELF_VER_1_3)
3450 r += snprintf (buf + r, size -r, ", 16b");
3451 else
3452 r += snprintf (buf + r, size -r, ", IFC");
3453 }
3454 }
3455
3456 if (config & E_NDS32_HAS_EXT_INST)
3457 r += snprintf (buf + r, size -r, ", PERF1");
3458
3459 if (config & E_NDS32_HAS_EXT2_INST)
3460 r += snprintf (buf + r, size -r, ", PERF2");
3461
3462 if (config & E_NDS32_HAS_FPU_INST)
3463 {
3464 has_fpu = true;
3465 r += snprintf (buf + r, size -r, ", FPU_SP");
3466 }
3467
3468 if (config & E_NDS32_HAS_FPU_DP_INST)
3469 {
3470 has_fpu = true;
3471 r += snprintf (buf + r, size -r, ", FPU_DP");
3472 }
3473
3474 if (config & E_NDS32_HAS_FPU_MAC_INST)
3475 {
3476 has_fpu = true;
3477 r += snprintf (buf + r, size -r, ", FPU_MAC");
3478 }
3479
3480 if (has_fpu)
3481 {
3482 switch ((config & E_NDS32_FPU_REG_CONF) >> E_NDS32_FPU_REG_CONF_SHIFT)
3483 {
3484 case E_NDS32_FPU_REG_8SP_4DP:
3485 r += snprintf (buf + r, size -r, ", FPU_REG:8/4");
3486 break;
3487 case E_NDS32_FPU_REG_16SP_8DP:
3488 r += snprintf (buf + r, size -r, ", FPU_REG:16/8");
3489 break;
3490 case E_NDS32_FPU_REG_32SP_16DP:
3491 r += snprintf (buf + r, size -r, ", FPU_REG:32/16");
3492 break;
3493 case E_NDS32_FPU_REG_32SP_32DP:
3494 r += snprintf (buf + r, size -r, ", FPU_REG:32/32");
3495 break;
3496 }
3497 }
3498
3499 if (config & E_NDS32_HAS_AUDIO_INST)
3500 r += snprintf (buf + r, size -r, ", AUDIO");
3501
3502 if (config & E_NDS32_HAS_STRING_INST)
3503 r += snprintf (buf + r, size -r, ", STR");
3504
3505 if (config & E_NDS32_HAS_REDUCED_REGS)
3506 r += snprintf (buf + r, size -r, ", 16REG");
3507
3508 if (config & E_NDS32_HAS_VIDEO_INST)
3509 {
3510 if (version <= E_NDS32_ELF_VER_1_3)
3511 r += snprintf (buf + r, size -r, ", VIDEO");
3512 else
3513 r += snprintf (buf + r, size -r, ", SATURATION");
3514 }
3515
3516 if (config & E_NDS32_HAS_ENCRIPT_INST)
3517 r += snprintf (buf + r, size -r, ", ENCRP");
3518
3519 if (config & E_NDS32_HAS_L2C_INST)
3520 r += snprintf (buf + r, size -r, ", L2C");
3521 }
3522
3523 static void
3524 decode_AMDGPU_machine_flags (Filedata *filedata, unsigned int e_flags,
3525 char *buf)
3526 {
3527 unsigned char *e_ident = filedata->file_header.e_ident;
3528 unsigned char osabi = e_ident[EI_OSABI];
3529 unsigned char abiversion = e_ident[EI_ABIVERSION];
3530 unsigned int mach;
3531
3532 /* HSA OS ABI v2 used a different encoding, but we don't need to support it,
3533 it has been deprecated for a while.
3534
3535 The PAL, MESA3D and NONE OS ABIs are not properly versioned, at the time
3536 of writing, they use the same flags as HSA v3, so the code below uses that
3537 assumption. */
3538 if (osabi == ELFOSABI_AMDGPU_HSA && abiversion < ELFABIVERSION_AMDGPU_HSA_V3)
3539 return;
3540
3541 mach = e_flags & EF_AMDGPU_MACH;
3542 switch (mach)
3543 {
3544 #define AMDGPU_CASE(code, string) \
3545 case code: strcat (buf, ", " string); break;
3546 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX600, "gfx600")
3547 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX601, "gfx601")
3548 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX700, "gfx700")
3549 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX701, "gfx701")
3550 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX702, "gfx702")
3551 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX703, "gfx703")
3552 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX704, "gfx704")
3553 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX801, "gfx801")
3554 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX802, "gfx802")
3555 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX803, "gfx803")
3556 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX810, "gfx810")
3557 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX900, "gfx900")
3558 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX902, "gfx902")
3559 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX904, "gfx904")
3560 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX906, "gfx906")
3561 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX908, "gfx908")
3562 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX909, "gfx909")
3563 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX90C, "gfx90c")
3564 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1010, "gfx1010")
3565 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1011, "gfx1011")
3566 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1012, "gfx1012")
3567 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1030, "gfx1030")
3568 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1031, "gfx1031")
3569 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1032, "gfx1032")
3570 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1033, "gfx1033")
3571 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX602, "gfx602")
3572 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX705, "gfx705")
3573 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX805, "gfx805")
3574 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1035, "gfx1035")
3575 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1034, "gfx1034")
3576 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX90A, "gfx90a")
3577 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX940, "gfx940")
3578 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1013, "gfx1013")
3579 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1036, "gfx1036")
3580 default:
3581 sprintf (buf, _(", <unknown AMDGPU GPU type: %#x>"), mach);
3582 break;
3583 #undef AMDGPU_CASE
3584 }
3585
3586 buf += strlen (buf);
3587 e_flags &= ~EF_AMDGPU_MACH;
3588
3589 if ((osabi == ELFOSABI_AMDGPU_HSA
3590 && abiversion == ELFABIVERSION_AMDGPU_HSA_V3)
3591 || osabi != ELFOSABI_AMDGPU_HSA)
3592 {
3593 /* For HSA v3 and other OS ABIs. */
3594 if (e_flags & EF_AMDGPU_FEATURE_XNACK_V3)
3595 {
3596 strcat (buf, ", xnack on");
3597 buf += strlen (buf);
3598 e_flags &= ~EF_AMDGPU_FEATURE_XNACK_V3;
3599 }
3600
3601 if (e_flags & EF_AMDGPU_FEATURE_SRAMECC_V3)
3602 {
3603 strcat (buf, ", sramecc on");
3604 buf += strlen (buf);
3605 e_flags &= ~EF_AMDGPU_FEATURE_SRAMECC_V3;
3606 }
3607 }
3608 else
3609 {
3610 /* For HSA v4+. */
3611 int xnack, sramecc;
3612
3613 xnack = e_flags & EF_AMDGPU_FEATURE_XNACK_V4;
3614 switch (xnack)
3615 {
3616 case EF_AMDGPU_FEATURE_XNACK_UNSUPPORTED_V4:
3617 break;
3618
3619 case EF_AMDGPU_FEATURE_XNACK_ANY_V4:
3620 strcat (buf, ", xnack any");
3621 break;
3622
3623 case EF_AMDGPU_FEATURE_XNACK_OFF_V4:
3624 strcat (buf, ", xnack off");
3625 break;
3626
3627 case EF_AMDGPU_FEATURE_XNACK_ON_V4:
3628 strcat (buf, ", xnack on");
3629 break;
3630
3631 default:
3632 sprintf (buf, _(", <unknown xnack value: %#x>"), xnack);
3633 break;
3634 }
3635
3636 buf += strlen (buf);
3637 e_flags &= ~EF_AMDGPU_FEATURE_XNACK_V4;
3638
3639 sramecc = e_flags & EF_AMDGPU_FEATURE_SRAMECC_V4;
3640 switch (sramecc)
3641 {
3642 case EF_AMDGPU_FEATURE_SRAMECC_UNSUPPORTED_V4:
3643 break;
3644
3645 case EF_AMDGPU_FEATURE_SRAMECC_ANY_V4:
3646 strcat (buf, ", sramecc any");
3647 break;
3648
3649 case EF_AMDGPU_FEATURE_SRAMECC_OFF_V4:
3650 strcat (buf, ", sramecc off");
3651 break;
3652
3653 case EF_AMDGPU_FEATURE_SRAMECC_ON_V4:
3654 strcat (buf, ", sramecc on");
3655 break;
3656
3657 default:
3658 sprintf (buf, _(", <unknown sramecc value: %#x>"), sramecc);
3659 break;
3660 }
3661
3662 buf += strlen (buf);
3663 e_flags &= ~EF_AMDGPU_FEATURE_SRAMECC_V4;
3664 }
3665
3666 if (e_flags != 0)
3667 sprintf (buf, _(", unknown flags bits: %#x"), e_flags);
3668 }
3669
3670 static char *
3671 get_machine_flags (Filedata * filedata, unsigned e_flags, unsigned e_machine)
3672 {
3673 static char buf[1024];
3674
3675 buf[0] = '\0';
3676
3677 if (e_flags)
3678 {
3679 switch (e_machine)
3680 {
3681 default:
3682 break;
3683
3684 case EM_ARC_COMPACT2:
3685 case EM_ARC_COMPACT:
3686 decode_ARC_machine_flags (e_flags, e_machine, buf);
3687 break;
3688
3689 case EM_ARM:
3690 decode_ARM_machine_flags (e_flags, buf);
3691 break;
3692
3693 case EM_AVR:
3694 decode_AVR_machine_flags (e_flags, buf, sizeof buf);
3695 break;
3696
3697 case EM_BLACKFIN:
3698 if (e_flags & EF_BFIN_PIC)
3699 strcat (buf, ", PIC");
3700
3701 if (e_flags & EF_BFIN_FDPIC)
3702 strcat (buf, ", FDPIC");
3703
3704 if (e_flags & EF_BFIN_CODE_IN_L1)
3705 strcat (buf, ", code in L1");
3706
3707 if (e_flags & EF_BFIN_DATA_IN_L1)
3708 strcat (buf, ", data in L1");
3709
3710 break;
3711
3712 case EM_CYGNUS_FRV:
3713 switch (e_flags & EF_FRV_CPU_MASK)
3714 {
3715 case EF_FRV_CPU_GENERIC:
3716 break;
3717
3718 default:
3719 strcat (buf, ", fr???");
3720 break;
3721
3722 case EF_FRV_CPU_FR300:
3723 strcat (buf, ", fr300");
3724 break;
3725
3726 case EF_FRV_CPU_FR400:
3727 strcat (buf, ", fr400");
3728 break;
3729 case EF_FRV_CPU_FR405:
3730 strcat (buf, ", fr405");
3731 break;
3732
3733 case EF_FRV_CPU_FR450:
3734 strcat (buf, ", fr450");
3735 break;
3736
3737 case EF_FRV_CPU_FR500:
3738 strcat (buf, ", fr500");
3739 break;
3740 case EF_FRV_CPU_FR550:
3741 strcat (buf, ", fr550");
3742 break;
3743
3744 case EF_FRV_CPU_SIMPLE:
3745 strcat (buf, ", simple");
3746 break;
3747 case EF_FRV_CPU_TOMCAT:
3748 strcat (buf, ", tomcat");
3749 break;
3750 }
3751 break;
3752
3753 case EM_68K:
3754 if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
3755 strcat (buf, ", m68000");
3756 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
3757 strcat (buf, ", cpu32");
3758 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
3759 strcat (buf, ", fido_a");
3760 else
3761 {
3762 char const * isa = _("unknown");
3763 char const * mac = _("unknown mac");
3764 char const * additional = NULL;
3765
3766 switch (e_flags & EF_M68K_CF_ISA_MASK)
3767 {
3768 case EF_M68K_CF_ISA_A_NODIV:
3769 isa = "A";
3770 additional = ", nodiv";
3771 break;
3772 case EF_M68K_CF_ISA_A:
3773 isa = "A";
3774 break;
3775 case EF_M68K_CF_ISA_A_PLUS:
3776 isa = "A+";
3777 break;
3778 case EF_M68K_CF_ISA_B_NOUSP:
3779 isa = "B";
3780 additional = ", nousp";
3781 break;
3782 case EF_M68K_CF_ISA_B:
3783 isa = "B";
3784 break;
3785 case EF_M68K_CF_ISA_C:
3786 isa = "C";
3787 break;
3788 case EF_M68K_CF_ISA_C_NODIV:
3789 isa = "C";
3790 additional = ", nodiv";
3791 break;
3792 }
3793 strcat (buf, ", cf, isa ");
3794 strcat (buf, isa);
3795 if (additional)
3796 strcat (buf, additional);
3797 if (e_flags & EF_M68K_CF_FLOAT)
3798 strcat (buf, ", float");
3799 switch (e_flags & EF_M68K_CF_MAC_MASK)
3800 {
3801 case 0:
3802 mac = NULL;
3803 break;
3804 case EF_M68K_CF_MAC:
3805 mac = "mac";
3806 break;
3807 case EF_M68K_CF_EMAC:
3808 mac = "emac";
3809 break;
3810 case EF_M68K_CF_EMAC_B:
3811 mac = "emac_b";
3812 break;
3813 }
3814 if (mac)
3815 {
3816 strcat (buf, ", ");
3817 strcat (buf, mac);
3818 }
3819 }
3820 break;
3821
3822 case EM_AMDGPU:
3823 decode_AMDGPU_machine_flags (filedata, e_flags, buf);
3824 break;
3825
3826 case EM_CYGNUS_MEP:
3827 switch (e_flags & EF_MEP_CPU_MASK)
3828 {
3829 case EF_MEP_CPU_MEP: strcat (buf, ", generic MeP"); break;
3830 case EF_MEP_CPU_C2: strcat (buf, ", MeP C2"); break;
3831 case EF_MEP_CPU_C3: strcat (buf, ", MeP C3"); break;
3832 case EF_MEP_CPU_C4: strcat (buf, ", MeP C4"); break;
3833 case EF_MEP_CPU_C5: strcat (buf, ", MeP C5"); break;
3834 case EF_MEP_CPU_H1: strcat (buf, ", MeP H1"); break;
3835 default: strcat (buf, _(", <unknown MeP cpu type>")); break;
3836 }
3837
3838 switch (e_flags & EF_MEP_COP_MASK)
3839 {
3840 case EF_MEP_COP_NONE: break;
3841 case EF_MEP_COP_AVC: strcat (buf, ", AVC coprocessor"); break;
3842 case EF_MEP_COP_AVC2: strcat (buf, ", AVC2 coprocessor"); break;
3843 case EF_MEP_COP_FMAX: strcat (buf, ", FMAX coprocessor"); break;
3844 case EF_MEP_COP_IVC2: strcat (buf, ", IVC2 coprocessor"); break;
3845 default: strcat (buf, _("<unknown MeP copro type>")); break;
3846 }
3847
3848 if (e_flags & EF_MEP_LIBRARY)
3849 strcat (buf, ", Built for Library");
3850
3851 if (e_flags & EF_MEP_INDEX_MASK)
3852 sprintf (buf + strlen (buf), ", Configuration Index: %#x",
3853 e_flags & EF_MEP_INDEX_MASK);
3854
3855 if (e_flags & ~ EF_MEP_ALL_FLAGS)
3856 sprintf (buf + strlen (buf), _(", unknown flags bits: %#x"),
3857 e_flags & ~ EF_MEP_ALL_FLAGS);
3858 break;
3859
3860 case EM_PPC:
3861 if (e_flags & EF_PPC_EMB)
3862 strcat (buf, ", emb");
3863
3864 if (e_flags & EF_PPC_RELOCATABLE)
3865 strcat (buf, _(", relocatable"));
3866
3867 if (e_flags & EF_PPC_RELOCATABLE_LIB)
3868 strcat (buf, _(", relocatable-lib"));
3869 break;
3870
3871 case EM_PPC64:
3872 if (e_flags & EF_PPC64_ABI)
3873 {
3874 char abi[] = ", abiv0";
3875
3876 abi[6] += e_flags & EF_PPC64_ABI;
3877 strcat (buf, abi);
3878 }
3879 break;
3880
3881 case EM_V800:
3882 if ((e_flags & EF_RH850_ABI) == EF_RH850_ABI)
3883 strcat (buf, ", RH850 ABI");
3884
3885 if (e_flags & EF_V800_850E3)
3886 strcat (buf, ", V3 architecture");
3887
3888 if ((e_flags & (EF_RH850_FPU_DOUBLE | EF_RH850_FPU_SINGLE)) == 0)
3889 strcat (buf, ", FPU not used");
3890
3891 if ((e_flags & (EF_RH850_REGMODE22 | EF_RH850_REGMODE32)) == 0)
3892 strcat (buf, ", regmode: COMMON");
3893
3894 if ((e_flags & (EF_RH850_GP_FIX | EF_RH850_GP_NOFIX)) == 0)
3895 strcat (buf, ", r4 not used");
3896
3897 if ((e_flags & (EF_RH850_EP_FIX | EF_RH850_EP_NOFIX)) == 0)
3898 strcat (buf, ", r30 not used");
3899
3900 if ((e_flags & (EF_RH850_TP_FIX | EF_RH850_TP_NOFIX)) == 0)
3901 strcat (buf, ", r5 not used");
3902
3903 if ((e_flags & (EF_RH850_REG2_RESERVE | EF_RH850_REG2_NORESERVE)) == 0)
3904 strcat (buf, ", r2 not used");
3905
3906 for (e_flags &= 0xFFFF; e_flags; e_flags &= ~ (e_flags & - e_flags))
3907 {
3908 switch (e_flags & - e_flags)
3909 {
3910 case EF_RH850_FPU_DOUBLE: strcat (buf, ", double precision FPU"); break;
3911 case EF_RH850_FPU_SINGLE: strcat (buf, ", single precision FPU"); break;
3912 case EF_RH850_REGMODE22: strcat (buf, ", regmode:22"); break;
3913 case EF_RH850_REGMODE32: strcat (buf, ", regmode:23"); break;
3914 case EF_RH850_GP_FIX: strcat (buf, ", r4 fixed"); break;
3915 case EF_RH850_GP_NOFIX: strcat (buf, ", r4 free"); break;
3916 case EF_RH850_EP_FIX: strcat (buf, ", r30 fixed"); break;
3917 case EF_RH850_EP_NOFIX: strcat (buf, ", r30 free"); break;
3918 case EF_RH850_TP_FIX: strcat (buf, ", r5 fixed"); break;
3919 case EF_RH850_TP_NOFIX: strcat (buf, ", r5 free"); break;
3920 case EF_RH850_REG2_RESERVE: strcat (buf, ", r2 fixed"); break;
3921 case EF_RH850_REG2_NORESERVE: strcat (buf, ", r2 free"); break;
3922 default: break;
3923 }
3924 }
3925 break;
3926
3927 case EM_V850:
3928 case EM_CYGNUS_V850:
3929 switch (e_flags & EF_V850_ARCH)
3930 {
3931 case E_V850E3V5_ARCH:
3932 strcat (buf, ", v850e3v5");
3933 break;
3934 case E_V850E2V3_ARCH:
3935 strcat (buf, ", v850e2v3");
3936 break;
3937 case E_V850E2_ARCH:
3938 strcat (buf, ", v850e2");
3939 break;
3940 case E_V850E1_ARCH:
3941 strcat (buf, ", v850e1");
3942 break;
3943 case E_V850E_ARCH:
3944 strcat (buf, ", v850e");
3945 break;
3946 case E_V850_ARCH:
3947 strcat (buf, ", v850");
3948 break;
3949 default:
3950 strcat (buf, _(", unknown v850 architecture variant"));
3951 break;
3952 }
3953 break;
3954
3955 case EM_M32R:
3956 case EM_CYGNUS_M32R:
3957 if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
3958 strcat (buf, ", m32r");
3959 break;
3960
3961 case EM_MIPS:
3962 case EM_MIPS_RS3_LE:
3963 if (e_flags & EF_MIPS_NOREORDER)
3964 strcat (buf, ", noreorder");
3965
3966 if (e_flags & EF_MIPS_PIC)
3967 strcat (buf, ", pic");
3968
3969 if (e_flags & EF_MIPS_CPIC)
3970 strcat (buf, ", cpic");
3971
3972 if (e_flags & EF_MIPS_UCODE)
3973 strcat (buf, ", ugen_reserved");
3974
3975 if (e_flags & EF_MIPS_ABI2)
3976 strcat (buf, ", abi2");
3977
3978 if (e_flags & EF_MIPS_OPTIONS_FIRST)
3979 strcat (buf, ", odk first");
3980
3981 if (e_flags & EF_MIPS_32BITMODE)
3982 strcat (buf, ", 32bitmode");
3983
3984 if (e_flags & EF_MIPS_NAN2008)
3985 strcat (buf, ", nan2008");
3986
3987 if (e_flags & EF_MIPS_FP64)
3988 strcat (buf, ", fp64");
3989
3990 switch ((e_flags & EF_MIPS_MACH))
3991 {
3992 case E_MIPS_MACH_3900: strcat (buf, ", 3900"); break;
3993 case E_MIPS_MACH_4010: strcat (buf, ", 4010"); break;
3994 case E_MIPS_MACH_4100: strcat (buf, ", 4100"); break;
3995 case E_MIPS_MACH_4111: strcat (buf, ", 4111"); break;
3996 case E_MIPS_MACH_4120: strcat (buf, ", 4120"); break;
3997 case E_MIPS_MACH_4650: strcat (buf, ", 4650"); break;
3998 case E_MIPS_MACH_5400: strcat (buf, ", 5400"); break;
3999 case E_MIPS_MACH_5500: strcat (buf, ", 5500"); break;
4000 case E_MIPS_MACH_5900: strcat (buf, ", 5900"); break;
4001 case E_MIPS_MACH_SB1: strcat (buf, ", sb1"); break;
4002 case E_MIPS_MACH_9000: strcat (buf, ", 9000"); break;
4003 case E_MIPS_MACH_LS2E: strcat (buf, ", loongson-2e"); break;
4004 case E_MIPS_MACH_LS2F: strcat (buf, ", loongson-2f"); break;
4005 case E_MIPS_MACH_GS464: strcat (buf, ", gs464"); break;
4006 case E_MIPS_MACH_GS464E: strcat (buf, ", gs464e"); break;
4007 case E_MIPS_MACH_GS264E: strcat (buf, ", gs264e"); break;
4008 case E_MIPS_MACH_OCTEON: strcat (buf, ", octeon"); break;
4009 case E_MIPS_MACH_OCTEON2: strcat (buf, ", octeon2"); break;
4010 case E_MIPS_MACH_OCTEON3: strcat (buf, ", octeon3"); break;
4011 case E_MIPS_MACH_XLR: strcat (buf, ", xlr"); break;
4012 case E_MIPS_MACH_IAMR2: strcat (buf, ", interaptiv-mr2"); break;
4013 case 0:
4014 /* We simply ignore the field in this case to avoid confusion:
4015 MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
4016 extension. */
4017 break;
4018 default: strcat (buf, _(", unknown CPU")); break;
4019 }
4020
4021 switch ((e_flags & EF_MIPS_ABI))
4022 {
4023 case E_MIPS_ABI_O32: strcat (buf, ", o32"); break;
4024 case E_MIPS_ABI_O64: strcat (buf, ", o64"); break;
4025 case E_MIPS_ABI_EABI32: strcat (buf, ", eabi32"); break;
4026 case E_MIPS_ABI_EABI64: strcat (buf, ", eabi64"); break;
4027 case 0:
4028 /* We simply ignore the field in this case to avoid confusion:
4029 MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
4030 This means it is likely to be an o32 file, but not for
4031 sure. */
4032 break;
4033 default: strcat (buf, _(", unknown ABI")); break;
4034 }
4035
4036 if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
4037 strcat (buf, ", mdmx");
4038
4039 if (e_flags & EF_MIPS_ARCH_ASE_M16)
4040 strcat (buf, ", mips16");
4041
4042 if (e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
4043 strcat (buf, ", micromips");
4044
4045 switch ((e_flags & EF_MIPS_ARCH))
4046 {
4047 case E_MIPS_ARCH_1: strcat (buf, ", mips1"); break;
4048 case E_MIPS_ARCH_2: strcat (buf, ", mips2"); break;
4049 case E_MIPS_ARCH_3: strcat (buf, ", mips3"); break;
4050 case E_MIPS_ARCH_4: strcat (buf, ", mips4"); break;
4051 case E_MIPS_ARCH_5: strcat (buf, ", mips5"); break;
4052 case E_MIPS_ARCH_32: strcat (buf, ", mips32"); break;
4053 case E_MIPS_ARCH_32R2: strcat (buf, ", mips32r2"); break;
4054 case E_MIPS_ARCH_32R6: strcat (buf, ", mips32r6"); break;
4055 case E_MIPS_ARCH_64: strcat (buf, ", mips64"); break;
4056 case E_MIPS_ARCH_64R2: strcat (buf, ", mips64r2"); break;
4057 case E_MIPS_ARCH_64R6: strcat (buf, ", mips64r6"); break;
4058 default: strcat (buf, _(", unknown ISA")); break;
4059 }
4060 break;
4061
4062 case EM_NDS32:
4063 decode_NDS32_machine_flags (e_flags, buf, sizeof buf);
4064 break;
4065
4066 case EM_NFP:
4067 switch (EF_NFP_MACH (e_flags))
4068 {
4069 case E_NFP_MACH_3200:
4070 strcat (buf, ", NFP-32xx");
4071 break;
4072 case E_NFP_MACH_6000:
4073 strcat (buf, ", NFP-6xxx");
4074 break;
4075 }
4076 break;
4077
4078 case EM_RISCV:
4079 if (e_flags & EF_RISCV_RVC)
4080 strcat (buf, ", RVC");
4081
4082 if (e_flags & EF_RISCV_RVE)
4083 strcat (buf, ", RVE");
4084
4085 if (e_flags & EF_RISCV_TSO)
4086 strcat (buf, ", TSO");
4087
4088 switch (e_flags & EF_RISCV_FLOAT_ABI)
4089 {
4090 case EF_RISCV_FLOAT_ABI_SOFT:
4091 strcat (buf, ", soft-float ABI");
4092 break;
4093
4094 case EF_RISCV_FLOAT_ABI_SINGLE:
4095 strcat (buf, ", single-float ABI");
4096 break;
4097
4098 case EF_RISCV_FLOAT_ABI_DOUBLE:
4099 strcat (buf, ", double-float ABI");
4100 break;
4101
4102 case EF_RISCV_FLOAT_ABI_QUAD:
4103 strcat (buf, ", quad-float ABI");
4104 break;
4105 }
4106 break;
4107
4108 case EM_SH:
4109 switch ((e_flags & EF_SH_MACH_MASK))
4110 {
4111 case EF_SH1: strcat (buf, ", sh1"); break;
4112 case EF_SH2: strcat (buf, ", sh2"); break;
4113 case EF_SH3: strcat (buf, ", sh3"); break;
4114 case EF_SH_DSP: strcat (buf, ", sh-dsp"); break;
4115 case EF_SH3_DSP: strcat (buf, ", sh3-dsp"); break;
4116 case EF_SH4AL_DSP: strcat (buf, ", sh4al-dsp"); break;
4117 case EF_SH3E: strcat (buf, ", sh3e"); break;
4118 case EF_SH4: strcat (buf, ", sh4"); break;
4119 case EF_SH5: strcat (buf, ", sh5"); break;
4120 case EF_SH2E: strcat (buf, ", sh2e"); break;
4121 case EF_SH4A: strcat (buf, ", sh4a"); break;
4122 case EF_SH2A: strcat (buf, ", sh2a"); break;
4123 case EF_SH4_NOFPU: strcat (buf, ", sh4-nofpu"); break;
4124 case EF_SH4A_NOFPU: strcat (buf, ", sh4a-nofpu"); break;
4125 case EF_SH2A_NOFPU: strcat (buf, ", sh2a-nofpu"); break;
4126 case EF_SH3_NOMMU: strcat (buf, ", sh3-nommu"); break;
4127 case EF_SH4_NOMMU_NOFPU: strcat (buf, ", sh4-nommu-nofpu"); break;
4128 case EF_SH2A_SH4_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh4-nommu-nofpu"); break;
4129 case EF_SH2A_SH3_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh3-nommu"); break;
4130 case EF_SH2A_SH4: strcat (buf, ", sh2a-or-sh4"); break;
4131 case EF_SH2A_SH3E: strcat (buf, ", sh2a-or-sh3e"); break;
4132 default: strcat (buf, _(", unknown ISA")); break;
4133 }
4134
4135 if (e_flags & EF_SH_PIC)
4136 strcat (buf, ", pic");
4137
4138 if (e_flags & EF_SH_FDPIC)
4139 strcat (buf, ", fdpic");
4140 break;
4141
4142 case EM_OR1K:
4143 if (e_flags & EF_OR1K_NODELAY)
4144 strcat (buf, ", no delay");
4145 break;
4146
4147 case EM_SPARCV9:
4148 if (e_flags & EF_SPARC_32PLUS)
4149 strcat (buf, ", v8+");
4150
4151 if (e_flags & EF_SPARC_SUN_US1)
4152 strcat (buf, ", ultrasparcI");
4153
4154 if (e_flags & EF_SPARC_SUN_US3)
4155 strcat (buf, ", ultrasparcIII");
4156
4157 if (e_flags & EF_SPARC_HAL_R1)
4158 strcat (buf, ", halr1");
4159
4160 if (e_flags & EF_SPARC_LEDATA)
4161 strcat (buf, ", ledata");
4162
4163 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
4164 strcat (buf, ", tso");
4165
4166 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
4167 strcat (buf, ", pso");
4168
4169 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
4170 strcat (buf, ", rmo");
4171 break;
4172
4173 case EM_PARISC:
4174 switch (e_flags & EF_PARISC_ARCH)
4175 {
4176 case EFA_PARISC_1_0:
4177 strcpy (buf, ", PA-RISC 1.0");
4178 break;
4179 case EFA_PARISC_1_1:
4180 strcpy (buf, ", PA-RISC 1.1");
4181 break;
4182 case EFA_PARISC_2_0:
4183 strcpy (buf, ", PA-RISC 2.0");
4184 break;
4185 default:
4186 break;
4187 }
4188 if (e_flags & EF_PARISC_TRAPNIL)
4189 strcat (buf, ", trapnil");
4190 if (e_flags & EF_PARISC_EXT)
4191 strcat (buf, ", ext");
4192 if (e_flags & EF_PARISC_LSB)
4193 strcat (buf, ", lsb");
4194 if (e_flags & EF_PARISC_WIDE)
4195 strcat (buf, ", wide");
4196 if (e_flags & EF_PARISC_NO_KABP)
4197 strcat (buf, ", no kabp");
4198 if (e_flags & EF_PARISC_LAZYSWAP)
4199 strcat (buf, ", lazyswap");
4200 break;
4201
4202 case EM_PJ:
4203 case EM_PJ_OLD:
4204 if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
4205 strcat (buf, ", new calling convention");
4206
4207 if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
4208 strcat (buf, ", gnu calling convention");
4209 break;
4210
4211 case EM_IA_64:
4212 if ((e_flags & EF_IA_64_ABI64))
4213 strcat (buf, ", 64-bit");
4214 else
4215 strcat (buf, ", 32-bit");
4216 if ((e_flags & EF_IA_64_REDUCEDFP))
4217 strcat (buf, ", reduced fp model");
4218 if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
4219 strcat (buf, ", no function descriptors, constant gp");
4220 else if ((e_flags & EF_IA_64_CONS_GP))
4221 strcat (buf, ", constant gp");
4222 if ((e_flags & EF_IA_64_ABSOLUTE))
4223 strcat (buf, ", absolute");
4224 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
4225 {
4226 if ((e_flags & EF_IA_64_VMS_LINKAGES))
4227 strcat (buf, ", vms_linkages");
4228 switch ((e_flags & EF_IA_64_VMS_COMCOD))
4229 {
4230 case EF_IA_64_VMS_COMCOD_SUCCESS:
4231 break;
4232 case EF_IA_64_VMS_COMCOD_WARNING:
4233 strcat (buf, ", warning");
4234 break;
4235 case EF_IA_64_VMS_COMCOD_ERROR:
4236 strcat (buf, ", error");
4237 break;
4238 case EF_IA_64_VMS_COMCOD_ABORT:
4239 strcat (buf, ", abort");
4240 break;
4241 default:
4242 warn (_("Unrecognised IA64 VMS Command Code: %x\n"),
4243 e_flags & EF_IA_64_VMS_COMCOD);
4244 strcat (buf, ", <unknown>");
4245 }
4246 }
4247 break;
4248
4249 case EM_VAX:
4250 if ((e_flags & EF_VAX_NONPIC))
4251 strcat (buf, ", non-PIC");
4252 if ((e_flags & EF_VAX_DFLOAT))
4253 strcat (buf, ", D-Float");
4254 if ((e_flags & EF_VAX_GFLOAT))
4255 strcat (buf, ", G-Float");
4256 break;
4257
4258 case EM_VISIUM:
4259 if (e_flags & EF_VISIUM_ARCH_MCM)
4260 strcat (buf, ", mcm");
4261 else if (e_flags & EF_VISIUM_ARCH_MCM24)
4262 strcat (buf, ", mcm24");
4263 if (e_flags & EF_VISIUM_ARCH_GR6)
4264 strcat (buf, ", gr6");
4265 break;
4266
4267 case EM_RL78:
4268 switch (e_flags & E_FLAG_RL78_CPU_MASK)
4269 {
4270 case E_FLAG_RL78_ANY_CPU: break;
4271 case E_FLAG_RL78_G10: strcat (buf, ", G10"); break;
4272 case E_FLAG_RL78_G13: strcat (buf, ", G13"); break;
4273 case E_FLAG_RL78_G14: strcat (buf, ", G14"); break;
4274 }
4275 if (e_flags & E_FLAG_RL78_64BIT_DOUBLES)
4276 strcat (buf, ", 64-bit doubles");
4277 break;
4278
4279 case EM_RX:
4280 if (e_flags & E_FLAG_RX_64BIT_DOUBLES)
4281 strcat (buf, ", 64-bit doubles");
4282 if (e_flags & E_FLAG_RX_DSP)
4283 strcat (buf, ", dsp");
4284 if (e_flags & E_FLAG_RX_PID)
4285 strcat (buf, ", pid");
4286 if (e_flags & E_FLAG_RX_ABI)
4287 strcat (buf, ", RX ABI");
4288 if (e_flags & E_FLAG_RX_SINSNS_SET)
4289 strcat (buf, e_flags & E_FLAG_RX_SINSNS_YES
4290 ? ", uses String instructions" : ", bans String instructions");
4291 if (e_flags & E_FLAG_RX_V2)
4292 strcat (buf, ", V2");
4293 if (e_flags & E_FLAG_RX_V3)
4294 strcat (buf, ", V3");
4295 break;
4296
4297 case EM_S390:
4298 if (e_flags & EF_S390_HIGH_GPRS)
4299 strcat (buf, ", highgprs");
4300 break;
4301
4302 case EM_TI_C6000:
4303 if ((e_flags & EF_C6000_REL))
4304 strcat (buf, ", relocatable module");
4305 break;
4306
4307 case EM_MSP430:
4308 strcat (buf, _(": architecture variant: "));
4309 switch (e_flags & EF_MSP430_MACH)
4310 {
4311 case E_MSP430_MACH_MSP430x11: strcat (buf, "MSP430x11"); break;
4312 case E_MSP430_MACH_MSP430x11x1 : strcat (buf, "MSP430x11x1 "); break;
4313 case E_MSP430_MACH_MSP430x12: strcat (buf, "MSP430x12"); break;
4314 case E_MSP430_MACH_MSP430x13: strcat (buf, "MSP430x13"); break;
4315 case E_MSP430_MACH_MSP430x14: strcat (buf, "MSP430x14"); break;
4316 case E_MSP430_MACH_MSP430x15: strcat (buf, "MSP430x15"); break;
4317 case E_MSP430_MACH_MSP430x16: strcat (buf, "MSP430x16"); break;
4318 case E_MSP430_MACH_MSP430x31: strcat (buf, "MSP430x31"); break;
4319 case E_MSP430_MACH_MSP430x32: strcat (buf, "MSP430x32"); break;
4320 case E_MSP430_MACH_MSP430x33: strcat (buf, "MSP430x33"); break;
4321 case E_MSP430_MACH_MSP430x41: strcat (buf, "MSP430x41"); break;
4322 case E_MSP430_MACH_MSP430x42: strcat (buf, "MSP430x42"); break;
4323 case E_MSP430_MACH_MSP430x43: strcat (buf, "MSP430x43"); break;
4324 case E_MSP430_MACH_MSP430x44: strcat (buf, "MSP430x44"); break;
4325 case E_MSP430_MACH_MSP430X : strcat (buf, "MSP430X"); break;
4326 default:
4327 strcat (buf, _(": unknown")); break;
4328 }
4329
4330 if (e_flags & ~ EF_MSP430_MACH)
4331 strcat (buf, _(": unknown extra flag bits also present"));
4332 break;
4333
4334 case EM_Z80:
4335 switch (e_flags & EF_Z80_MACH_MSK)
4336 {
4337 case EF_Z80_MACH_Z80: strcat (buf, ", Z80"); break;
4338 case EF_Z80_MACH_Z180: strcat (buf, ", Z180"); break;
4339 case EF_Z80_MACH_R800: strcat (buf, ", R800"); break;
4340 case EF_Z80_MACH_EZ80_Z80: strcat (buf, ", EZ80"); break;
4341 case EF_Z80_MACH_EZ80_ADL: strcat (buf, ", EZ80, ADL"); break;
4342 case EF_Z80_MACH_GBZ80: strcat (buf, ", GBZ80"); break;
4343 case EF_Z80_MACH_Z80N: strcat (buf, ", Z80N"); break;
4344 default:
4345 strcat (buf, _(", unknown")); break;
4346 }
4347 break;
4348 case EM_LOONGARCH:
4349 if (EF_LOONGARCH_IS_SOFT_FLOAT (e_flags))
4350 strcat (buf, ", SOFT-FLOAT");
4351 else if (EF_LOONGARCH_IS_SINGLE_FLOAT (e_flags))
4352 strcat (buf, ", SINGLE-FLOAT");
4353 else if (EF_LOONGARCH_IS_DOUBLE_FLOAT (e_flags))
4354 strcat (buf, ", DOUBLE-FLOAT");
4355
4356 if (EF_LOONGARCH_IS_OBJ_V0 (e_flags))
4357 strcat (buf, ", OBJ-v0");
4358 else if (EF_LOONGARCH_IS_OBJ_V1 (e_flags))
4359 strcat (buf, ", OBJ-v1");
4360
4361 break;
4362 }
4363 }
4364
4365 return buf;
4366 }
4367
4368 static const char *
4369 get_osabi_name (Filedata * filedata, unsigned int osabi)
4370 {
4371 static char buff[32];
4372
4373 switch (osabi)
4374 {
4375 case ELFOSABI_NONE: return "UNIX - System V";
4376 case ELFOSABI_HPUX: return "UNIX - HP-UX";
4377 case ELFOSABI_NETBSD: return "UNIX - NetBSD";
4378 case ELFOSABI_GNU: return "UNIX - GNU";
4379 case ELFOSABI_SOLARIS: return "UNIX - Solaris";
4380 case ELFOSABI_AIX: return "UNIX - AIX";
4381 case ELFOSABI_IRIX: return "UNIX - IRIX";
4382 case ELFOSABI_FREEBSD: return "UNIX - FreeBSD";
4383 case ELFOSABI_TRU64: return "UNIX - TRU64";
4384 case ELFOSABI_MODESTO: return "Novell - Modesto";
4385 case ELFOSABI_OPENBSD: return "UNIX - OpenBSD";
4386 case ELFOSABI_OPENVMS: return "VMS - OpenVMS";
4387 case ELFOSABI_NSK: return "HP - Non-Stop Kernel";
4388 case ELFOSABI_AROS: return "AROS";
4389 case ELFOSABI_FENIXOS: return "FenixOS";
4390 case ELFOSABI_CLOUDABI: return "Nuxi CloudABI";
4391 case ELFOSABI_OPENVOS: return "Stratus Technologies OpenVOS";
4392 default:
4393 if (osabi >= 64)
4394 switch (filedata->file_header.e_machine)
4395 {
4396 case EM_AMDGPU:
4397 switch (osabi)
4398 {
4399 case ELFOSABI_AMDGPU_HSA: return "AMD HSA";
4400 case ELFOSABI_AMDGPU_PAL: return "AMD PAL";
4401 case ELFOSABI_AMDGPU_MESA3D: return "AMD Mesa3D";
4402 default:
4403 break;
4404 }
4405 break;
4406
4407 case EM_ARM:
4408 switch (osabi)
4409 {
4410 case ELFOSABI_ARM: return "ARM";
4411 case ELFOSABI_ARM_FDPIC: return "ARM FDPIC";
4412 default:
4413 break;
4414 }
4415 break;
4416
4417 case EM_MSP430:
4418 case EM_MSP430_OLD:
4419 case EM_VISIUM:
4420 switch (osabi)
4421 {
4422 case ELFOSABI_STANDALONE: return _("Standalone App");
4423 default:
4424 break;
4425 }
4426 break;
4427
4428 case EM_TI_C6000:
4429 switch (osabi)
4430 {
4431 case ELFOSABI_C6000_ELFABI: return _("Bare-metal C6000");
4432 case ELFOSABI_C6000_LINUX: return "Linux C6000";
4433 default:
4434 break;
4435 }
4436 break;
4437
4438 default:
4439 break;
4440 }
4441 snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
4442 return buff;
4443 }
4444 }
4445
4446 static const char *
4447 get_aarch64_segment_type (unsigned long type)
4448 {
4449 switch (type)
4450 {
4451 case PT_AARCH64_ARCHEXT: return "AARCH64_ARCHEXT";
4452 case PT_AARCH64_MEMTAG_MTE: return "AARCH64_MEMTAG_MTE";
4453 default: return NULL;
4454 }
4455 }
4456
4457 static const char *
4458 get_arm_segment_type (unsigned long type)
4459 {
4460 switch (type)
4461 {
4462 case PT_ARM_EXIDX: return "EXIDX";
4463 default: return NULL;
4464 }
4465 }
4466
4467 static const char *
4468 get_s390_segment_type (unsigned long type)
4469 {
4470 switch (type)
4471 {
4472 case PT_S390_PGSTE: return "S390_PGSTE";
4473 default: return NULL;
4474 }
4475 }
4476
4477 static const char *
4478 get_mips_segment_type (unsigned long type)
4479 {
4480 switch (type)
4481 {
4482 case PT_MIPS_REGINFO: return "REGINFO";
4483 case PT_MIPS_RTPROC: return "RTPROC";
4484 case PT_MIPS_OPTIONS: return "OPTIONS";
4485 case PT_MIPS_ABIFLAGS: return "ABIFLAGS";
4486 default: return NULL;
4487 }
4488 }
4489
4490 static const char *
4491 get_parisc_segment_type (unsigned long type)
4492 {
4493 switch (type)
4494 {
4495 case PT_PARISC_ARCHEXT: return "PARISC_ARCHEXT";
4496 case PT_PARISC_UNWIND: return "PARISC_UNWIND";
4497 case PT_PARISC_WEAKORDER: return "PARISC_WEAKORDER";
4498 default: return NULL;
4499 }
4500 }
4501
4502 static const char *
4503 get_ia64_segment_type (unsigned long type)
4504 {
4505 switch (type)
4506 {
4507 case PT_IA_64_ARCHEXT: return "IA_64_ARCHEXT";
4508 case PT_IA_64_UNWIND: return "IA_64_UNWIND";
4509 default: return NULL;
4510 }
4511 }
4512
4513 static const char *
4514 get_tic6x_segment_type (unsigned long type)
4515 {
4516 switch (type)
4517 {
4518 case PT_C6000_PHATTR: return "C6000_PHATTR";
4519 default: return NULL;
4520 }
4521 }
4522
4523 static const char *
4524 get_riscv_segment_type (unsigned long type)
4525 {
4526 switch (type)
4527 {
4528 case PT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
4529 default: return NULL;
4530 }
4531 }
4532
4533 static const char *
4534 get_hpux_segment_type (unsigned long type, unsigned e_machine)
4535 {
4536 if (e_machine == EM_PARISC)
4537 switch (type)
4538 {
4539 case PT_HP_TLS: return "HP_TLS";
4540 case PT_HP_CORE_NONE: return "HP_CORE_NONE";
4541 case PT_HP_CORE_VERSION: return "HP_CORE_VERSION";
4542 case PT_HP_CORE_KERNEL: return "HP_CORE_KERNEL";
4543 case PT_HP_CORE_COMM: return "HP_CORE_COMM";
4544 case PT_HP_CORE_PROC: return "HP_CORE_PROC";
4545 case PT_HP_CORE_LOADABLE: return "HP_CORE_LOADABLE";
4546 case PT_HP_CORE_STACK: return "HP_CORE_STACK";
4547 case PT_HP_CORE_SHM: return "HP_CORE_SHM";
4548 case PT_HP_CORE_MMF: return "HP_CORE_MMF";
4549 case PT_HP_PARALLEL: return "HP_PARALLEL";
4550 case PT_HP_FASTBIND: return "HP_FASTBIND";
4551 case PT_HP_OPT_ANNOT: return "HP_OPT_ANNOT";
4552 case PT_HP_HSL_ANNOT: return "HP_HSL_ANNOT";
4553 case PT_HP_STACK: return "HP_STACK";
4554 case PT_HP_CORE_UTSNAME: return "HP_CORE_UTSNAME";
4555 default: return NULL;
4556 }
4557
4558 if (e_machine == EM_IA_64)
4559 switch (type)
4560 {
4561 case PT_HP_TLS: return "HP_TLS";
4562 case PT_IA_64_HP_OPT_ANOT: return "HP_OPT_ANNOT";
4563 case PT_IA_64_HP_HSL_ANOT: return "HP_HSL_ANNOT";
4564 case PT_IA_64_HP_STACK: return "HP_STACK";
4565 default: return NULL;
4566 }
4567
4568 return NULL;
4569 }
4570
4571 static const char *
4572 get_solaris_segment_type (unsigned long type)
4573 {
4574 switch (type)
4575 {
4576 case 0x6464e550: return "PT_SUNW_UNWIND";
4577 case 0x6474e550: return "PT_SUNW_EH_FRAME";
4578 case 0x6ffffff7: return "PT_LOSUNW";
4579 case 0x6ffffffa: return "PT_SUNWBSS";
4580 case 0x6ffffffb: return "PT_SUNWSTACK";
4581 case 0x6ffffffc: return "PT_SUNWDTRACE";
4582 case 0x6ffffffd: return "PT_SUNWCAP";
4583 case 0x6fffffff: return "PT_HISUNW";
4584 default: return NULL;
4585 }
4586 }
4587
4588 static const char *
4589 get_segment_type (Filedata * filedata, unsigned long p_type)
4590 {
4591 static char buff[32];
4592
4593 switch (p_type)
4594 {
4595 case PT_NULL: return "NULL";
4596 case PT_LOAD: return "LOAD";
4597 case PT_DYNAMIC: return "DYNAMIC";
4598 case PT_INTERP: return "INTERP";
4599 case PT_NOTE: return "NOTE";
4600 case PT_SHLIB: return "SHLIB";
4601 case PT_PHDR: return "PHDR";
4602 case PT_TLS: return "TLS";
4603 case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
4604 case PT_GNU_STACK: return "GNU_STACK";
4605 case PT_GNU_RELRO: return "GNU_RELRO";
4606 case PT_GNU_PROPERTY: return "GNU_PROPERTY";
4607
4608 case PT_OPENBSD_RANDOMIZE: return "OPENBSD_RANDOMIZE";
4609 case PT_OPENBSD_WXNEEDED: return "OPENBSD_WXNEEDED";
4610 case PT_OPENBSD_BOOTDATA: return "OPENBSD_BOOTDATA";
4611
4612 default:
4613 if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
4614 {
4615 const char * result;
4616
4617 switch (filedata->file_header.e_machine)
4618 {
4619 case EM_AARCH64:
4620 result = get_aarch64_segment_type (p_type);
4621 break;
4622 case EM_ARM:
4623 result = get_arm_segment_type (p_type);
4624 break;
4625 case EM_MIPS:
4626 case EM_MIPS_RS3_LE:
4627 result = get_mips_segment_type (p_type);
4628 break;
4629 case EM_PARISC:
4630 result = get_parisc_segment_type (p_type);
4631 break;
4632 case EM_IA_64:
4633 result = get_ia64_segment_type (p_type);
4634 break;
4635 case EM_TI_C6000:
4636 result = get_tic6x_segment_type (p_type);
4637 break;
4638 case EM_S390:
4639 case EM_S390_OLD:
4640 result = get_s390_segment_type (p_type);
4641 break;
4642 case EM_RISCV:
4643 result = get_riscv_segment_type (p_type);
4644 break;
4645 default:
4646 result = NULL;
4647 break;
4648 }
4649
4650 if (result != NULL)
4651 return result;
4652
4653 sprintf (buff, "LOPROC+%#lx", p_type - PT_LOPROC);
4654 }
4655 else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
4656 {
4657 const char * result = NULL;
4658
4659 switch (filedata->file_header.e_ident[EI_OSABI])
4660 {
4661 case ELFOSABI_GNU:
4662 case ELFOSABI_FREEBSD:
4663 if (p_type >= PT_GNU_MBIND_LO && p_type <= PT_GNU_MBIND_HI)
4664 {
4665 sprintf (buff, "GNU_MBIND+%#lx", p_type - PT_GNU_MBIND_LO);
4666 result = buff;
4667 }
4668 break;
4669 case ELFOSABI_HPUX:
4670 result = get_hpux_segment_type (p_type,
4671 filedata->file_header.e_machine);
4672 break;
4673 case ELFOSABI_SOLARIS:
4674 result = get_solaris_segment_type (p_type);
4675 break;
4676 default:
4677 break;
4678 }
4679 if (result != NULL)
4680 return result;
4681
4682 sprintf (buff, "LOOS+%#lx", p_type - PT_LOOS);
4683 }
4684 else
4685 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
4686
4687 return buff;
4688 }
4689 }
4690
4691 static const char *
4692 get_arc_section_type_name (unsigned int sh_type)
4693 {
4694 switch (sh_type)
4695 {
4696 case SHT_ARC_ATTRIBUTES: return "ARC_ATTRIBUTES";
4697 default:
4698 break;
4699 }
4700 return NULL;
4701 }
4702
4703 static const char *
4704 get_mips_section_type_name (unsigned int sh_type)
4705 {
4706 switch (sh_type)
4707 {
4708 case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
4709 case SHT_MIPS_MSYM: return "MIPS_MSYM";
4710 case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
4711 case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
4712 case SHT_MIPS_UCODE: return "MIPS_UCODE";
4713 case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
4714 case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
4715 case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
4716 case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
4717 case SHT_MIPS_RELD: return "MIPS_RELD";
4718 case SHT_MIPS_IFACE: return "MIPS_IFACE";
4719 case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
4720 case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
4721 case SHT_MIPS_SHDR: return "MIPS_SHDR";
4722 case SHT_MIPS_FDESC: return "MIPS_FDESC";
4723 case SHT_MIPS_EXTSYM: return "MIPS_EXTSYM";
4724 case SHT_MIPS_DENSE: return "MIPS_DENSE";
4725 case SHT_MIPS_PDESC: return "MIPS_PDESC";
4726 case SHT_MIPS_LOCSYM: return "MIPS_LOCSYM";
4727 case SHT_MIPS_AUXSYM: return "MIPS_AUXSYM";
4728 case SHT_MIPS_OPTSYM: return "MIPS_OPTSYM";
4729 case SHT_MIPS_LOCSTR: return "MIPS_LOCSTR";
4730 case SHT_MIPS_LINE: return "MIPS_LINE";
4731 case SHT_MIPS_RFDESC: return "MIPS_RFDESC";
4732 case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
4733 case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
4734 case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
4735 case SHT_MIPS_DWARF: return "MIPS_DWARF";
4736 case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
4737 case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
4738 case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
4739 case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
4740 case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
4741 case SHT_MIPS_XLATE: return "MIPS_XLATE";
4742 case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
4743 case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
4744 case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
4745 case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
4746 case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
4747 case SHT_MIPS_ABIFLAGS: return "MIPS_ABIFLAGS";
4748 case SHT_MIPS_XHASH: return "MIPS_XHASH";
4749 default:
4750 break;
4751 }
4752 return NULL;
4753 }
4754
4755 static const char *
4756 get_parisc_section_type_name (unsigned int sh_type)
4757 {
4758 switch (sh_type)
4759 {
4760 case SHT_PARISC_EXT: return "PARISC_EXT";
4761 case SHT_PARISC_UNWIND: return "PARISC_UNWIND";
4762 case SHT_PARISC_DOC: return "PARISC_DOC";
4763 case SHT_PARISC_ANNOT: return "PARISC_ANNOT";
4764 case SHT_PARISC_SYMEXTN: return "PARISC_SYMEXTN";
4765 case SHT_PARISC_STUBS: return "PARISC_STUBS";
4766 case SHT_PARISC_DLKM: return "PARISC_DLKM";
4767 default: return NULL;
4768 }
4769 }
4770
4771 static const char *
4772 get_ia64_section_type_name (Filedata * filedata, unsigned int sh_type)
4773 {
4774 /* If the top 8 bits are 0x78 the next 8 are the os/abi ID. */
4775 if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
4776 return get_osabi_name (filedata, (sh_type & 0x00FF0000) >> 16);
4777
4778 switch (sh_type)
4779 {
4780 case SHT_IA_64_EXT: return "IA_64_EXT";
4781 case SHT_IA_64_UNWIND: return "IA_64_UNWIND";
4782 case SHT_IA_64_PRIORITY_INIT: return "IA_64_PRIORITY_INIT";
4783 case SHT_IA_64_VMS_TRACE: return "VMS_TRACE";
4784 case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
4785 case SHT_IA_64_VMS_DEBUG: return "VMS_DEBUG";
4786 case SHT_IA_64_VMS_DEBUG_STR: return "VMS_DEBUG_STR";
4787 case SHT_IA_64_VMS_LINKAGES: return "VMS_LINKAGES";
4788 case SHT_IA_64_VMS_SYMBOL_VECTOR: return "VMS_SYMBOL_VECTOR";
4789 case SHT_IA_64_VMS_FIXUP: return "VMS_FIXUP";
4790 default:
4791 break;
4792 }
4793 return NULL;
4794 }
4795
4796 static const char *
4797 get_x86_64_section_type_name (unsigned int sh_type)
4798 {
4799 switch (sh_type)
4800 {
4801 case SHT_X86_64_UNWIND: return "X86_64_UNWIND";
4802 default: return NULL;
4803 }
4804 }
4805
4806 static const char *
4807 get_aarch64_section_type_name (unsigned int sh_type)
4808 {
4809 switch (sh_type)
4810 {
4811 case SHT_AARCH64_ATTRIBUTES: return "AARCH64_ATTRIBUTES";
4812 default: return NULL;
4813 }
4814 }
4815
4816 static const char *
4817 get_arm_section_type_name (unsigned int sh_type)
4818 {
4819 switch (sh_type)
4820 {
4821 case SHT_ARM_EXIDX: return "ARM_EXIDX";
4822 case SHT_ARM_PREEMPTMAP: return "ARM_PREEMPTMAP";
4823 case SHT_ARM_ATTRIBUTES: return "ARM_ATTRIBUTES";
4824 case SHT_ARM_DEBUGOVERLAY: return "ARM_DEBUGOVERLAY";
4825 case SHT_ARM_OVERLAYSECTION: return "ARM_OVERLAYSECTION";
4826 default: return NULL;
4827 }
4828 }
4829
4830 static const char *
4831 get_tic6x_section_type_name (unsigned int sh_type)
4832 {
4833 switch (sh_type)
4834 {
4835 case SHT_C6000_UNWIND: return "C6000_UNWIND";
4836 case SHT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
4837 case SHT_C6000_ATTRIBUTES: return "C6000_ATTRIBUTES";
4838 case SHT_TI_ICODE: return "TI_ICODE";
4839 case SHT_TI_XREF: return "TI_XREF";
4840 case SHT_TI_HANDLER: return "TI_HANDLER";
4841 case SHT_TI_INITINFO: return "TI_INITINFO";
4842 case SHT_TI_PHATTRS: return "TI_PHATTRS";
4843 default: return NULL;
4844 }
4845 }
4846
4847 static const char *
4848 get_msp430_section_type_name (unsigned int sh_type)
4849 {
4850 switch (sh_type)
4851 {
4852 case SHT_MSP430_SEC_FLAGS: return "MSP430_SEC_FLAGS";
4853 case SHT_MSP430_SYM_ALIASES: return "MSP430_SYM_ALIASES";
4854 case SHT_MSP430_ATTRIBUTES: return "MSP430_ATTRIBUTES";
4855 default: return NULL;
4856 }
4857 }
4858
4859 static const char *
4860 get_nfp_section_type_name (unsigned int sh_type)
4861 {
4862 switch (sh_type)
4863 {
4864 case SHT_NFP_MECONFIG: return "NFP_MECONFIG";
4865 case SHT_NFP_INITREG: return "NFP_INITREG";
4866 case SHT_NFP_UDEBUG: return "NFP_UDEBUG";
4867 default: return NULL;
4868 }
4869 }
4870
4871 static const char *
4872 get_v850_section_type_name (unsigned int sh_type)
4873 {
4874 switch (sh_type)
4875 {
4876 case SHT_V850_SCOMMON: return "V850 Small Common";
4877 case SHT_V850_TCOMMON: return "V850 Tiny Common";
4878 case SHT_V850_ZCOMMON: return "V850 Zero Common";
4879 case SHT_RENESAS_IOP: return "RENESAS IOP";
4880 case SHT_RENESAS_INFO: return "RENESAS INFO";
4881 default: return NULL;
4882 }
4883 }
4884
4885 static const char *
4886 get_riscv_section_type_name (unsigned int sh_type)
4887 {
4888 switch (sh_type)
4889 {
4890 case SHT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
4891 default: return NULL;
4892 }
4893 }
4894
4895 static const char *
4896 get_csky_section_type_name (unsigned int sh_type)
4897 {
4898 switch (sh_type)
4899 {
4900 case SHT_CSKY_ATTRIBUTES: return "CSKY_ATTRIBUTES";
4901 default: return NULL;
4902 }
4903 }
4904
4905 static const char *
4906 get_section_type_name (Filedata * filedata, unsigned int sh_type)
4907 {
4908 static char buff[32];
4909 const char * result;
4910
4911 switch (sh_type)
4912 {
4913 case SHT_NULL: return "NULL";
4914 case SHT_PROGBITS: return "PROGBITS";
4915 case SHT_SYMTAB: return "SYMTAB";
4916 case SHT_STRTAB: return "STRTAB";
4917 case SHT_RELA: return "RELA";
4918 case SHT_RELR: return "RELR";
4919 case SHT_HASH: return "HASH";
4920 case SHT_DYNAMIC: return "DYNAMIC";
4921 case SHT_NOTE: return "NOTE";
4922 case SHT_NOBITS: return "NOBITS";
4923 case SHT_REL: return "REL";
4924 case SHT_SHLIB: return "SHLIB";
4925 case SHT_DYNSYM: return "DYNSYM";
4926 case SHT_INIT_ARRAY: return "INIT_ARRAY";
4927 case SHT_FINI_ARRAY: return "FINI_ARRAY";
4928 case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
4929 case SHT_GNU_HASH: return "GNU_HASH";
4930 case SHT_GROUP: return "GROUP";
4931 case SHT_SYMTAB_SHNDX: return "SYMTAB SECTION INDICES";
4932 case SHT_GNU_verdef: return "VERDEF";
4933 case SHT_GNU_verneed: return "VERNEED";
4934 case SHT_GNU_versym: return "VERSYM";
4935 case 0x6ffffff0: return "VERSYM";
4936 case 0x6ffffffc: return "VERDEF";
4937 case 0x7ffffffd: return "AUXILIARY";
4938 case 0x7fffffff: return "FILTER";
4939 case SHT_GNU_LIBLIST: return "GNU_LIBLIST";
4940
4941 default:
4942 if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
4943 {
4944 switch (filedata->file_header.e_machine)
4945 {
4946 case EM_ARC:
4947 case EM_ARC_COMPACT:
4948 case EM_ARC_COMPACT2:
4949 result = get_arc_section_type_name (sh_type);
4950 break;
4951 case EM_MIPS:
4952 case EM_MIPS_RS3_LE:
4953 result = get_mips_section_type_name (sh_type);
4954 break;
4955 case EM_PARISC:
4956 result = get_parisc_section_type_name (sh_type);
4957 break;
4958 case EM_IA_64:
4959 result = get_ia64_section_type_name (filedata, sh_type);
4960 break;
4961 case EM_X86_64:
4962 case EM_L1OM:
4963 case EM_K1OM:
4964 result = get_x86_64_section_type_name (sh_type);
4965 break;
4966 case EM_AARCH64:
4967 result = get_aarch64_section_type_name (sh_type);
4968 break;
4969 case EM_ARM:
4970 result = get_arm_section_type_name (sh_type);
4971 break;
4972 case EM_TI_C6000:
4973 result = get_tic6x_section_type_name (sh_type);
4974 break;
4975 case EM_MSP430:
4976 result = get_msp430_section_type_name (sh_type);
4977 break;
4978 case EM_NFP:
4979 result = get_nfp_section_type_name (sh_type);
4980 break;
4981 case EM_V800:
4982 case EM_V850:
4983 case EM_CYGNUS_V850:
4984 result = get_v850_section_type_name (sh_type);
4985 break;
4986 case EM_RISCV:
4987 result = get_riscv_section_type_name (sh_type);
4988 break;
4989 case EM_CSKY:
4990 result = get_csky_section_type_name (sh_type);
4991 break;
4992 default:
4993 result = NULL;
4994 break;
4995 }
4996
4997 if (result != NULL)
4998 return result;
4999
5000 sprintf (buff, "LOPROC+%#x", sh_type - SHT_LOPROC);
5001 }
5002 else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
5003 {
5004 switch (filedata->file_header.e_machine)
5005 {
5006 case EM_IA_64:
5007 result = get_ia64_section_type_name (filedata, sh_type);
5008 break;
5009 default:
5010 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
5011 result = get_solaris_section_type (sh_type);
5012 else
5013 {
5014 switch (sh_type)
5015 {
5016 case SHT_GNU_INCREMENTAL_INPUTS: result = "GNU_INCREMENTAL_INPUTS"; break;
5017 case SHT_GNU_ATTRIBUTES: result = "GNU_ATTRIBUTES"; break;
5018 case SHT_GNU_HASH: result = "GNU_HASH"; break;
5019 case SHT_GNU_LIBLIST: result = "GNU_LIBLIST"; break;
5020 default:
5021 result = NULL;
5022 break;
5023 }
5024 }
5025 break;
5026 }
5027
5028 if (result != NULL)
5029 return result;
5030
5031 sprintf (buff, "LOOS+%#x", sh_type - SHT_LOOS);
5032 }
5033 else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
5034 {
5035 switch (filedata->file_header.e_machine)
5036 {
5037 case EM_V800:
5038 case EM_V850:
5039 case EM_CYGNUS_V850:
5040 result = get_v850_section_type_name (sh_type);
5041 break;
5042 default:
5043 result = NULL;
5044 break;
5045 }
5046
5047 if (result != NULL)
5048 return result;
5049
5050 sprintf (buff, "LOUSER+%#x", sh_type - SHT_LOUSER);
5051 }
5052 else
5053 /* This message is probably going to be displayed in a 15
5054 character wide field, so put the hex value first. */
5055 snprintf (buff, sizeof (buff), _("%08x: <unknown>"), sh_type);
5056
5057 return buff;
5058 }
5059 }
5060
5061 enum long_option_values
5062 {
5063 OPTION_DEBUG_DUMP = 512,
5064 OPTION_DYN_SYMS,
5065 OPTION_LTO_SYMS,
5066 OPTION_DWARF_DEPTH,
5067 OPTION_DWARF_START,
5068 OPTION_DWARF_CHECK,
5069 OPTION_CTF_DUMP,
5070 OPTION_CTF_PARENT,
5071 OPTION_CTF_SYMBOLS,
5072 OPTION_CTF_STRINGS,
5073 OPTION_WITH_SYMBOL_VERSIONS,
5074 OPTION_RECURSE_LIMIT,
5075 OPTION_NO_RECURSE_LIMIT,
5076 OPTION_NO_DEMANGLING,
5077 OPTION_SYM_BASE
5078 };
5079
5080 static struct option options[] =
5081 {
5082 /* Note - This table is alpha-sorted on the 'val'
5083 field in order to make adding new options easier. */
5084 {"arch-specific", no_argument, 0, 'A'},
5085 {"all", no_argument, 0, 'a'},
5086 {"demangle", optional_argument, 0, 'C'},
5087 {"archive-index", no_argument, 0, 'c'},
5088 {"use-dynamic", no_argument, 0, 'D'},
5089 {"dynamic", no_argument, 0, 'd'},
5090 {"headers", no_argument, 0, 'e'},
5091 {"section-groups", no_argument, 0, 'g'},
5092 {"help", no_argument, 0, 'H'},
5093 {"file-header", no_argument, 0, 'h'},
5094 {"histogram", no_argument, 0, 'I'},
5095 {"lint", no_argument, 0, 'L'},
5096 {"enable-checks", no_argument, 0, 'L'},
5097 {"program-headers", no_argument, 0, 'l'},
5098 {"segments", no_argument, 0, 'l'},
5099 {"full-section-name",no_argument, 0, 'N'},
5100 {"notes", no_argument, 0, 'n'},
5101 {"process-links", no_argument, 0, 'P'},
5102 {"string-dump", required_argument, 0, 'p'},
5103 {"relocated-dump", required_argument, 0, 'R'},
5104 {"relocs", no_argument, 0, 'r'},
5105 {"section-headers", no_argument, 0, 'S'},
5106 {"sections", no_argument, 0, 'S'},
5107 {"symbols", no_argument, 0, 's'},
5108 {"syms", no_argument, 0, 's'},
5109 {"silent-truncation",no_argument, 0, 'T'},
5110 {"section-details", no_argument, 0, 't'},
5111 {"unicode", required_argument, NULL, 'U'},
5112 {"unwind", no_argument, 0, 'u'},
5113 {"version-info", no_argument, 0, 'V'},
5114 {"version", no_argument, 0, 'v'},
5115 {"wide", no_argument, 0, 'W'},
5116 {"hex-dump", required_argument, 0, 'x'},
5117 {"decompress", no_argument, 0, 'z'},
5118
5119 {"no-demangle", no_argument, 0, OPTION_NO_DEMANGLING},
5120 {"recurse-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
5121 {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
5122 {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
5123 {"dyn-syms", no_argument, 0, OPTION_DYN_SYMS},
5124 {"lto-syms", no_argument, 0, OPTION_LTO_SYMS},
5125 {"debug-dump", optional_argument, 0, OPTION_DEBUG_DUMP},
5126 {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
5127 {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
5128 {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
5129 #ifdef ENABLE_LIBCTF
5130 {"ctf", required_argument, 0, OPTION_CTF_DUMP},
5131 {"ctf-symbols", required_argument, 0, OPTION_CTF_SYMBOLS},
5132 {"ctf-strings", required_argument, 0, OPTION_CTF_STRINGS},
5133 {"ctf-parent", required_argument, 0, OPTION_CTF_PARENT},
5134 #endif
5135 {"sym-base", optional_argument, 0, OPTION_SYM_BASE},
5136
5137 {0, no_argument, 0, 0}
5138 };
5139
5140 static void
5141 usage (FILE * stream)
5142 {
5143 fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
5144 fprintf (stream, _(" Display information about the contents of ELF format files\n"));
5145 fprintf (stream, _(" Options are:\n"));
5146 fprintf (stream, _("\
5147 -a --all Equivalent to: -h -l -S -s -r -d -V -A -I\n"));
5148 fprintf (stream, _("\
5149 -h --file-header Display the ELF file header\n"));
5150 fprintf (stream, _("\
5151 -l --program-headers Display the program headers\n"));
5152 fprintf (stream, _("\
5153 --segments An alias for --program-headers\n"));
5154 fprintf (stream, _("\
5155 -S --section-headers Display the sections' header\n"));
5156 fprintf (stream, _("\
5157 --sections An alias for --section-headers\n"));
5158 fprintf (stream, _("\
5159 -g --section-groups Display the section groups\n"));
5160 fprintf (stream, _("\
5161 -t --section-details Display the section details\n"));
5162 fprintf (stream, _("\
5163 -e --headers Equivalent to: -h -l -S\n"));
5164 fprintf (stream, _("\
5165 -s --syms Display the symbol table\n"));
5166 fprintf (stream, _("\
5167 --symbols An alias for --syms\n"));
5168 fprintf (stream, _("\
5169 --dyn-syms Display the dynamic symbol table\n"));
5170 fprintf (stream, _("\
5171 --lto-syms Display LTO symbol tables\n"));
5172 fprintf (stream, _("\
5173 --sym-base=[0|8|10|16] \n\
5174 Force base for symbol sizes. The options are \n\
5175 mixed (the default), octal, decimal, hexadecimal.\n"));
5176 fprintf (stream, _("\
5177 -C --demangle[=STYLE] Decode mangled/processed symbol names\n"));
5178 display_demangler_styles (stream, _("\
5179 STYLE can be "));
5180 fprintf (stream, _("\
5181 --no-demangle Do not demangle low-level symbol names. (default)\n"));
5182 fprintf (stream, _("\
5183 --recurse-limit Enable a demangling recursion limit. (default)\n"));
5184 fprintf (stream, _("\
5185 --no-recurse-limit Disable a demangling recursion limit\n"));
5186 fprintf (stream, _("\
5187 -U[dlexhi] --unicode=[default|locale|escape|hex|highlight|invalid]\n\
5188 Display unicode characters as determined by the current locale\n\
5189 (default), escape sequences, \"<hex sequences>\", highlighted\n\
5190 escape sequences, or treat them as invalid and display as\n\
5191 \"{hex sequences}\"\n"));
5192 fprintf (stream, _("\
5193 -n --notes Display the core notes (if present)\n"));
5194 fprintf (stream, _("\
5195 -r --relocs Display the relocations (if present)\n"));
5196 fprintf (stream, _("\
5197 -u --unwind Display the unwind info (if present)\n"));
5198 fprintf (stream, _("\
5199 -d --dynamic Display the dynamic section (if present)\n"));
5200 fprintf (stream, _("\
5201 -V --version-info Display the version sections (if present)\n"));
5202 fprintf (stream, _("\
5203 -A --arch-specific Display architecture specific information (if any)\n"));
5204 fprintf (stream, _("\
5205 -c --archive-index Display the symbol/file index in an archive\n"));
5206 fprintf (stream, _("\
5207 -D --use-dynamic Use the dynamic section info when displaying symbols\n"));
5208 fprintf (stream, _("\
5209 -L --lint|--enable-checks\n\
5210 Display warning messages for possible problems\n"));
5211 fprintf (stream, _("\
5212 -x --hex-dump=<number|name>\n\
5213 Dump the contents of section <number|name> as bytes\n"));
5214 fprintf (stream, _("\
5215 -p --string-dump=<number|name>\n\
5216 Dump the contents of section <number|name> as strings\n"));
5217 fprintf (stream, _("\
5218 -R --relocated-dump=<number|name>\n\
5219 Dump the relocated contents of section <number|name>\n"));
5220 fprintf (stream, _("\
5221 -z --decompress Decompress section before dumping it\n"));
5222 fprintf (stream, _("\
5223 -w --debug-dump[a/=abbrev, A/=addr, r/=aranges, c/=cu_index, L/=decodedline,\n\
5224 f/=frames, F/=frames-interp, g/=gdb_index, i/=info, o/=loc,\n\
5225 m/=macro, p/=pubnames, t/=pubtypes, R/=Ranges, l/=rawline,\n\
5226 s/=str, O/=str-offsets, u/=trace_abbrev, T/=trace_aranges,\n\
5227 U/=trace_info]\n\
5228 Display the contents of DWARF debug sections\n"));
5229 fprintf (stream, _("\
5230 -wk --debug-dump=links Display the contents of sections that link to separate\n\
5231 debuginfo files\n"));
5232 fprintf (stream, _("\
5233 -P --process-links Display the contents of non-debug sections in separate\n\
5234 debuginfo files. (Implies -wK)\n"));
5235 #if DEFAULT_FOR_FOLLOW_LINKS
5236 fprintf (stream, _("\
5237 -wK --debug-dump=follow-links\n\
5238 Follow links to separate debug info files (default)\n"));
5239 fprintf (stream, _("\
5240 -wN --debug-dump=no-follow-links\n\
5241 Do not follow links to separate debug info files\n"));
5242 #else
5243 fprintf (stream, _("\
5244 -wK --debug-dump=follow-links\n\
5245 Follow links to separate debug info files\n"));
5246 fprintf (stream, _("\
5247 -wN --debug-dump=no-follow-links\n\
5248 Do not follow links to separate debug info files\n\
5249 (default)\n"));
5250 #endif
5251 #if HAVE_LIBDEBUGINFOD
5252 fprintf (stream, _("\
5253 -wD --debug-dump=use-debuginfod\n\
5254 When following links, also query debuginfod servers (default)\n"));
5255 fprintf (stream, _("\
5256 -wE --debug-dump=do-not-use-debuginfod\n\
5257 When following links, do not query debuginfod servers\n"));
5258 #endif
5259 fprintf (stream, _("\
5260 --dwarf-depth=N Do not display DIEs at depth N or greater\n"));
5261 fprintf (stream, _("\
5262 --dwarf-start=N Display DIEs starting at offset N\n"));
5263 #ifdef ENABLE_LIBCTF
5264 fprintf (stream, _("\
5265 --ctf=<number|name> Display CTF info from section <number|name>\n"));
5266 fprintf (stream, _("\
5267 --ctf-parent=<name> Use CTF archive member <name> as the CTF parent\n"));
5268 fprintf (stream, _("\
5269 --ctf-symbols=<number|name>\n\
5270 Use section <number|name> as the CTF external symtab\n"));
5271 fprintf (stream, _("\
5272 --ctf-strings=<number|name>\n\
5273 Use section <number|name> as the CTF external strtab\n"));
5274 #endif
5275
5276 #ifdef SUPPORT_DISASSEMBLY
5277 fprintf (stream, _("\
5278 -i --instruction-dump=<number|name>\n\
5279 Disassemble the contents of section <number|name>\n"));
5280 #endif
5281 fprintf (stream, _("\
5282 -I --histogram Display histogram of bucket list lengths\n"));
5283 fprintf (stream, _("\
5284 -W --wide Allow output width to exceed 80 characters\n"));
5285 fprintf (stream, _("\
5286 -T --silent-truncation If a symbol name is truncated, do not add [...] suffix\n"));
5287 fprintf (stream, _("\
5288 @<file> Read options from <file>\n"));
5289 fprintf (stream, _("\
5290 -H --help Display this information\n"));
5291 fprintf (stream, _("\
5292 -v --version Display the version number of readelf\n"));
5293
5294 if (REPORT_BUGS_TO[0] && stream == stdout)
5295 fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
5296
5297 exit (stream == stdout ? 0 : 1);
5298 }
5299
5300 /* Record the fact that the user wants the contents of section number
5301 SECTION to be displayed using the method(s) encoded as flags bits
5302 in TYPE. Note, TYPE can be zero if we are creating the array for
5303 the first time. */
5304
5305 static void
5306 request_dump_bynumber (struct dump_data *dumpdata,
5307 unsigned int section, dump_type type)
5308 {
5309 if (section >= dumpdata->num_dump_sects)
5310 {
5311 dump_type * new_dump_sects;
5312
5313 new_dump_sects = (dump_type *) calloc (section + 1,
5314 sizeof (* new_dump_sects));
5315
5316 if (new_dump_sects == NULL)
5317 error (_("Out of memory allocating dump request table.\n"));
5318 else
5319 {
5320 if (dumpdata->dump_sects)
5321 {
5322 /* Copy current flag settings. */
5323 memcpy (new_dump_sects, dumpdata->dump_sects,
5324 dumpdata->num_dump_sects * sizeof (* new_dump_sects));
5325
5326 free (dumpdata->dump_sects);
5327 }
5328
5329 dumpdata->dump_sects = new_dump_sects;
5330 dumpdata->num_dump_sects = section + 1;
5331 }
5332 }
5333
5334 if (dumpdata->dump_sects)
5335 dumpdata->dump_sects[section] |= type;
5336 }
5337
5338 /* Request a dump by section name. */
5339
5340 static void
5341 request_dump_byname (const char * section, dump_type type)
5342 {
5343 struct dump_list_entry * new_request;
5344
5345 new_request = (struct dump_list_entry *)
5346 malloc (sizeof (struct dump_list_entry));
5347 if (!new_request)
5348 error (_("Out of memory allocating dump request table.\n"));
5349
5350 new_request->name = strdup (section);
5351 if (!new_request->name)
5352 error (_("Out of memory allocating dump request table.\n"));
5353
5354 new_request->type = type;
5355
5356 new_request->next = dump_sects_byname;
5357 dump_sects_byname = new_request;
5358 }
5359
5360 static inline void
5361 request_dump (struct dump_data *dumpdata, dump_type type)
5362 {
5363 int section;
5364 char * cp;
5365
5366 do_dump = true;
5367 section = strtoul (optarg, & cp, 0);
5368
5369 if (! *cp && section >= 0)
5370 request_dump_bynumber (dumpdata, section, type);
5371 else
5372 request_dump_byname (optarg, type);
5373 }
5374
5375 static void
5376 parse_args (struct dump_data *dumpdata, int argc, char ** argv)
5377 {
5378 int c;
5379
5380 if (argc < 2)
5381 usage (stderr);
5382
5383 while ((c = getopt_long
5384 (argc, argv, "ACDHILNPR:STU:VWacdeghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
5385 {
5386 switch (c)
5387 {
5388 case 0:
5389 /* Long options. */
5390 break;
5391 case 'H':
5392 usage (stdout);
5393 break;
5394
5395 case 'a':
5396 do_syms = true;
5397 do_reloc = true;
5398 do_unwind = true;
5399 do_dynamic = true;
5400 do_header = true;
5401 do_sections = true;
5402 do_section_groups = true;
5403 do_segments = true;
5404 do_version = true;
5405 do_histogram = true;
5406 do_arch = true;
5407 do_notes = true;
5408 break;
5409
5410 case 'g':
5411 do_section_groups = true;
5412 break;
5413 case 't':
5414 case 'N':
5415 do_sections = true;
5416 do_section_details = true;
5417 break;
5418 case 'e':
5419 do_header = true;
5420 do_sections = true;
5421 do_segments = true;
5422 break;
5423 case 'A':
5424 do_arch = true;
5425 break;
5426 case 'D':
5427 do_using_dynamic = true;
5428 break;
5429 case 'r':
5430 do_reloc = true;
5431 break;
5432 case 'u':
5433 do_unwind = true;
5434 break;
5435 case 'h':
5436 do_header = true;
5437 break;
5438 case 'l':
5439 do_segments = true;
5440 break;
5441 case 's':
5442 do_syms = true;
5443 break;
5444 case 'S':
5445 do_sections = true;
5446 break;
5447 case 'd':
5448 do_dynamic = true;
5449 break;
5450 case 'I':
5451 do_histogram = true;
5452 break;
5453 case 'n':
5454 do_notes = true;
5455 break;
5456 case 'c':
5457 do_archive_index = true;
5458 break;
5459 case 'L':
5460 do_checks = true;
5461 break;
5462 case 'P':
5463 process_links = true;
5464 do_follow_links = true;
5465 dump_any_debugging = true;
5466 break;
5467 case 'x':
5468 request_dump (dumpdata, HEX_DUMP);
5469 break;
5470 case 'p':
5471 request_dump (dumpdata, STRING_DUMP);
5472 break;
5473 case 'R':
5474 request_dump (dumpdata, RELOC_DUMP);
5475 break;
5476 case 'z':
5477 decompress_dumps = true;
5478 break;
5479 case 'w':
5480 if (optarg == NULL)
5481 {
5482 do_debugging = true;
5483 do_dump = true;
5484 dump_any_debugging = true;
5485 dwarf_select_sections_all ();
5486 }
5487 else
5488 {
5489 do_debugging = false;
5490 if (dwarf_select_sections_by_letters (optarg))
5491 {
5492 do_dump = true;
5493 dump_any_debugging = true;
5494 }
5495 }
5496 break;
5497 case OPTION_DEBUG_DUMP:
5498 if (optarg == NULL)
5499 {
5500 do_dump = true;
5501 do_debugging = true;
5502 dump_any_debugging = true;
5503 dwarf_select_sections_all ();
5504 }
5505 else
5506 {
5507 do_debugging = false;
5508 if (dwarf_select_sections_by_names (optarg))
5509 {
5510 do_dump = true;
5511 dump_any_debugging = true;
5512 }
5513 }
5514 break;
5515 case OPTION_DWARF_DEPTH:
5516 {
5517 char *cp;
5518
5519 dwarf_cutoff_level = strtoul (optarg, & cp, 0);
5520 }
5521 break;
5522 case OPTION_DWARF_START:
5523 {
5524 char *cp;
5525
5526 dwarf_start_die = strtoul (optarg, & cp, 0);
5527 }
5528 break;
5529 case OPTION_DWARF_CHECK:
5530 dwarf_check = true;
5531 break;
5532 case OPTION_CTF_DUMP:
5533 do_ctf = true;
5534 request_dump (dumpdata, CTF_DUMP);
5535 break;
5536 case OPTION_CTF_SYMBOLS:
5537 free (dump_ctf_symtab_name);
5538 dump_ctf_symtab_name = strdup (optarg);
5539 break;
5540 case OPTION_CTF_STRINGS:
5541 free (dump_ctf_strtab_name);
5542 dump_ctf_strtab_name = strdup (optarg);
5543 break;
5544 case OPTION_CTF_PARENT:
5545 free (dump_ctf_parent_name);
5546 dump_ctf_parent_name = strdup (optarg);
5547 break;
5548 case OPTION_DYN_SYMS:
5549 do_dyn_syms = true;
5550 break;
5551 case OPTION_LTO_SYMS:
5552 do_lto_syms = true;
5553 break;
5554 #ifdef SUPPORT_DISASSEMBLY
5555 case 'i':
5556 request_dump (dumpdata, DISASS_DUMP);
5557 break;
5558 #endif
5559 case 'v':
5560 print_version (program_name);
5561 break;
5562 case 'V':
5563 do_version = true;
5564 break;
5565 case 'W':
5566 do_wide = true;
5567 break;
5568 case 'T':
5569 do_not_show_symbol_truncation = true;
5570 break;
5571 case 'C':
5572 do_demangle = true;
5573 if (optarg != NULL)
5574 {
5575 enum demangling_styles style;
5576
5577 style = cplus_demangle_name_to_style (optarg);
5578 if (style == unknown_demangling)
5579 error (_("unknown demangling style `%s'"), optarg);
5580
5581 cplus_demangle_set_style (style);
5582 }
5583 break;
5584 case OPTION_NO_DEMANGLING:
5585 do_demangle = false;
5586 break;
5587 case OPTION_RECURSE_LIMIT:
5588 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
5589 break;
5590 case OPTION_NO_RECURSE_LIMIT:
5591 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
5592 break;
5593 case OPTION_WITH_SYMBOL_VERSIONS:
5594 /* Ignored for backward compatibility. */
5595 break;
5596
5597 case 'U':
5598 if (optarg == NULL)
5599 error (_("Missing arg to -U/--unicode")); /* Can this happen ? */
5600 else if (streq (optarg, "default") || streq (optarg, "d"))
5601 unicode_display = unicode_default;
5602 else if (streq (optarg, "locale") || streq (optarg, "l"))
5603 unicode_display = unicode_locale;
5604 else if (streq (optarg, "escape") || streq (optarg, "e"))
5605 unicode_display = unicode_escape;
5606 else if (streq (optarg, "invalid") || streq (optarg, "i"))
5607 unicode_display = unicode_invalid;
5608 else if (streq (optarg, "hex") || streq (optarg, "x"))
5609 unicode_display = unicode_hex;
5610 else if (streq (optarg, "highlight") || streq (optarg, "h"))
5611 unicode_display = unicode_highlight;
5612 else
5613 error (_("invalid argument to -U/--unicode: %s"), optarg);
5614 break;
5615
5616 case OPTION_SYM_BASE:
5617 sym_base = 0;
5618 if (optarg != NULL)
5619 {
5620 sym_base = strtoul (optarg, NULL, 0);
5621 switch (sym_base)
5622 {
5623 case 0:
5624 case 8:
5625 case 10:
5626 case 16:
5627 break;
5628
5629 default:
5630 sym_base = 0;
5631 break;
5632 }
5633 }
5634 break;
5635
5636 default:
5637 /* xgettext:c-format */
5638 error (_("Invalid option '-%c'\n"), c);
5639 /* Fall through. */
5640 case '?':
5641 usage (stderr);
5642 }
5643 }
5644
5645 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
5646 && !do_segments && !do_header && !do_dump && !do_version
5647 && !do_histogram && !do_debugging && !do_arch && !do_notes
5648 && !do_section_groups && !do_archive_index
5649 && !do_dyn_syms && !do_lto_syms)
5650 {
5651 if (do_checks)
5652 {
5653 check_all = true;
5654 do_dynamic = do_syms = do_reloc = do_unwind = do_sections = true;
5655 do_segments = do_header = do_dump = do_version = true;
5656 do_histogram = do_debugging = do_arch = do_notes = true;
5657 do_section_groups = do_archive_index = do_dyn_syms = true;
5658 do_lto_syms = true;
5659 }
5660 else
5661 usage (stderr);
5662 }
5663 }
5664
5665 static const char *
5666 get_elf_class (unsigned int elf_class)
5667 {
5668 static char buff[32];
5669
5670 switch (elf_class)
5671 {
5672 case ELFCLASSNONE: return _("none");
5673 case ELFCLASS32: return "ELF32";
5674 case ELFCLASS64: return "ELF64";
5675 default:
5676 snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
5677 return buff;
5678 }
5679 }
5680
5681 static const char *
5682 get_data_encoding (unsigned int encoding)
5683 {
5684 static char buff[32];
5685
5686 switch (encoding)
5687 {
5688 case ELFDATANONE: return _("none");
5689 case ELFDATA2LSB: return _("2's complement, little endian");
5690 case ELFDATA2MSB: return _("2's complement, big endian");
5691 default:
5692 snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
5693 return buff;
5694 }
5695 }
5696
5697 static bool
5698 check_magic_number (Filedata * filedata, Elf_Internal_Ehdr * header)
5699 {
5700 if (header->e_ident[EI_MAG0] == ELFMAG0
5701 && header->e_ident[EI_MAG1] == ELFMAG1
5702 && header->e_ident[EI_MAG2] == ELFMAG2
5703 && header->e_ident[EI_MAG3] == ELFMAG3)
5704 return true;
5705
5706 /* Some compilers produce object files that are not in the ELF file format.
5707 As an aid to users of readelf, try to identify these cases and suggest
5708 alternative tools.
5709
5710 FIXME: It is not clear if all four bytes are used as constant magic
5711 valus by all compilers. It may be necessary to recode this function if
5712 different tools use different length sequences. */
5713
5714 static struct
5715 {
5716 unsigned char magic[4];
5717 const char * obj_message;
5718 const char * ar_message;
5719 }
5720 known_magic[] =
5721 {
5722 { { 'B', 'C', 0xc0, 0xde },
5723 N_("This is a LLVM bitcode file - try using llvm-bcanalyzer\n"),
5724 N_("This is a LLVM bitcode file - try extracing and then using llvm-bcanalyzer\n")
5725 },
5726 { { 'g', 'o', ' ', 'o' },
5727 N_("This is a GO binary file - try using 'go tool objdump' or 'go tool nm'\n"),
5728 NULL
5729 }
5730 };
5731 int i;
5732
5733 for (i = ARRAY_SIZE (known_magic); i--;)
5734 {
5735 if (header->e_ident[EI_MAG0] == known_magic[i].magic[0]
5736 && header->e_ident[EI_MAG1] == known_magic[i].magic[1]
5737 && header->e_ident[EI_MAG2] == known_magic[i].magic[2]
5738 && header->e_ident[EI_MAG3] == known_magic[i].magic[3])
5739 {
5740 /* Some compiler's analyzer tools do not handle archives,
5741 so we provide two different kinds of error message. */
5742 if (filedata->archive_file_size > 0
5743 && known_magic[i].ar_message != NULL)
5744 error ("%s", known_magic[i].ar_message);
5745 else
5746 error ("%s", known_magic[i].obj_message);
5747 return false;
5748 }
5749 }
5750
5751 error (_("Not an ELF file - it has the wrong magic bytes at the start\n"));
5752 return false;
5753 }
5754
5755 /* Decode the data held in 'filedata->file_header'. */
5756
5757 static bool
5758 process_file_header (Filedata * filedata)
5759 {
5760 Elf_Internal_Ehdr * header = & filedata->file_header;
5761
5762 if (! check_magic_number (filedata, header))
5763 return false;
5764
5765 if (! filedata->is_separate)
5766 init_dwarf_regnames_by_elf_machine_code (header->e_machine);
5767
5768 if (do_header)
5769 {
5770 unsigned i;
5771
5772 if (filedata->is_separate)
5773 printf (_("ELF Header in linked file '%s':\n"), filedata->file_name);
5774 else
5775 printf (_("ELF Header:\n"));
5776 printf (_(" Magic: "));
5777 for (i = 0; i < EI_NIDENT; i++)
5778 printf ("%2.2x ", header->e_ident[i]);
5779 printf ("\n");
5780 printf (_(" Class: %s\n"),
5781 get_elf_class (header->e_ident[EI_CLASS]));
5782 printf (_(" Data: %s\n"),
5783 get_data_encoding (header->e_ident[EI_DATA]));
5784 printf (_(" Version: %d%s\n"),
5785 header->e_ident[EI_VERSION],
5786 (header->e_ident[EI_VERSION] == EV_CURRENT
5787 ? _(" (current)")
5788 : (header->e_ident[EI_VERSION] != EV_NONE
5789 ? _(" <unknown>")
5790 : "")));
5791 printf (_(" OS/ABI: %s\n"),
5792 get_osabi_name (filedata, header->e_ident[EI_OSABI]));
5793 printf (_(" ABI Version: %d\n"),
5794 header->e_ident[EI_ABIVERSION]);
5795 printf (_(" Type: %s\n"),
5796 get_file_type (filedata));
5797 printf (_(" Machine: %s\n"),
5798 get_machine_name (header->e_machine));
5799 printf (_(" Version: 0x%lx\n"),
5800 header->e_version);
5801
5802 printf (_(" Entry point address: "));
5803 print_vma (header->e_entry, PREFIX_HEX);
5804 printf (_("\n Start of program headers: "));
5805 print_vma (header->e_phoff, DEC);
5806 printf (_(" (bytes into file)\n Start of section headers: "));
5807 print_vma (header->e_shoff, DEC);
5808 printf (_(" (bytes into file)\n"));
5809
5810 printf (_(" Flags: 0x%lx%s\n"),
5811 header->e_flags,
5812 get_machine_flags (filedata, header->e_flags, header->e_machine));
5813 printf (_(" Size of this header: %u (bytes)\n"),
5814 header->e_ehsize);
5815 printf (_(" Size of program headers: %u (bytes)\n"),
5816 header->e_phentsize);
5817 printf (_(" Number of program headers: %u"),
5818 header->e_phnum);
5819 if (filedata->section_headers != NULL
5820 && header->e_phnum == PN_XNUM
5821 && filedata->section_headers[0].sh_info != 0)
5822 printf (" (%u)", filedata->section_headers[0].sh_info);
5823 putc ('\n', stdout);
5824 printf (_(" Size of section headers: %u (bytes)\n"),
5825 header->e_shentsize);
5826 printf (_(" Number of section headers: %u"),
5827 header->e_shnum);
5828 if (filedata->section_headers != NULL && header->e_shnum == SHN_UNDEF)
5829 {
5830 header->e_shnum = filedata->section_headers[0].sh_size;
5831 printf (" (%u)", header->e_shnum);
5832 }
5833 putc ('\n', stdout);
5834 printf (_(" Section header string table index: %u"),
5835 header->e_shstrndx);
5836 if (filedata->section_headers != NULL
5837 && header->e_shstrndx == (SHN_XINDEX & 0xffff))
5838 {
5839 header->e_shstrndx = filedata->section_headers[0].sh_link;
5840 printf (" (%u)", header->e_shstrndx);
5841 }
5842 if (header->e_shstrndx != SHN_UNDEF
5843 && header->e_shstrndx >= header->e_shnum)
5844 {
5845 header->e_shstrndx = SHN_UNDEF;
5846 printf (_(" <corrupt: out of range>"));
5847 }
5848 putc ('\n', stdout);
5849 }
5850
5851 if (filedata->section_headers != NULL)
5852 {
5853 if (header->e_phnum == PN_XNUM
5854 && filedata->section_headers[0].sh_info != 0)
5855 {
5856 /* Throw away any cached read of PN_XNUM headers. */
5857 free (filedata->program_headers);
5858 filedata->program_headers = NULL;
5859 header->e_phnum = filedata->section_headers[0].sh_info;
5860 }
5861 if (header->e_shnum == SHN_UNDEF)
5862 header->e_shnum = filedata->section_headers[0].sh_size;
5863 if (header->e_shstrndx == (SHN_XINDEX & 0xffff))
5864 header->e_shstrndx = filedata->section_headers[0].sh_link;
5865 if (header->e_shstrndx >= header->e_shnum)
5866 header->e_shstrndx = SHN_UNDEF;
5867 }
5868
5869 return true;
5870 }
5871
5872 /* Read in the program headers from FILEDATA and store them in PHEADERS.
5873 Returns TRUE upon success, FALSE otherwise. Loads 32-bit headers. */
5874
5875 static bool
5876 get_32bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
5877 {
5878 Elf32_External_Phdr * phdrs;
5879 Elf32_External_Phdr * external;
5880 Elf_Internal_Phdr * internal;
5881 unsigned int i;
5882 unsigned int size = filedata->file_header.e_phentsize;
5883 unsigned int num = filedata->file_header.e_phnum;
5884
5885 /* PR binutils/17531: Cope with unexpected section header sizes. */
5886 if (size == 0 || num == 0)
5887 return false;
5888 if (size < sizeof * phdrs)
5889 {
5890 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
5891 return false;
5892 }
5893 if (size > sizeof * phdrs)
5894 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
5895
5896 phdrs = (Elf32_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
5897 size, num, _("program headers"));
5898 if (phdrs == NULL)
5899 return false;
5900
5901 for (i = 0, internal = pheaders, external = phdrs;
5902 i < filedata->file_header.e_phnum;
5903 i++, internal++, external++)
5904 {
5905 internal->p_type = BYTE_GET (external->p_type);
5906 internal->p_offset = BYTE_GET (external->p_offset);
5907 internal->p_vaddr = BYTE_GET (external->p_vaddr);
5908 internal->p_paddr = BYTE_GET (external->p_paddr);
5909 internal->p_filesz = BYTE_GET (external->p_filesz);
5910 internal->p_memsz = BYTE_GET (external->p_memsz);
5911 internal->p_flags = BYTE_GET (external->p_flags);
5912 internal->p_align = BYTE_GET (external->p_align);
5913 }
5914
5915 free (phdrs);
5916 return true;
5917 }
5918
5919 /* Read in the program headers from FILEDATA and store them in PHEADERS.
5920 Returns TRUE upon success, FALSE otherwise. Loads 64-bit headers. */
5921
5922 static bool
5923 get_64bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
5924 {
5925 Elf64_External_Phdr * phdrs;
5926 Elf64_External_Phdr * external;
5927 Elf_Internal_Phdr * internal;
5928 unsigned int i;
5929 unsigned int size = filedata->file_header.e_phentsize;
5930 unsigned int num = filedata->file_header.e_phnum;
5931
5932 /* PR binutils/17531: Cope with unexpected section header sizes. */
5933 if (size == 0 || num == 0)
5934 return false;
5935 if (size < sizeof * phdrs)
5936 {
5937 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
5938 return false;
5939 }
5940 if (size > sizeof * phdrs)
5941 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
5942
5943 phdrs = (Elf64_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
5944 size, num, _("program headers"));
5945 if (!phdrs)
5946 return false;
5947
5948 for (i = 0, internal = pheaders, external = phdrs;
5949 i < filedata->file_header.e_phnum;
5950 i++, internal++, external++)
5951 {
5952 internal->p_type = BYTE_GET (external->p_type);
5953 internal->p_flags = BYTE_GET (external->p_flags);
5954 internal->p_offset = BYTE_GET (external->p_offset);
5955 internal->p_vaddr = BYTE_GET (external->p_vaddr);
5956 internal->p_paddr = BYTE_GET (external->p_paddr);
5957 internal->p_filesz = BYTE_GET (external->p_filesz);
5958 internal->p_memsz = BYTE_GET (external->p_memsz);
5959 internal->p_align = BYTE_GET (external->p_align);
5960 }
5961
5962 free (phdrs);
5963 return true;
5964 }
5965
5966 /* Returns TRUE if the program headers were read into `program_headers'. */
5967
5968 static bool
5969 get_program_headers (Filedata * filedata)
5970 {
5971 Elf_Internal_Phdr * phdrs;
5972
5973 /* Check cache of prior read. */
5974 if (filedata->program_headers != NULL)
5975 return true;
5976
5977 /* Be kind to memory checkers by looking for
5978 e_phnum values which we know must be invalid. */
5979 if (filedata->file_header.e_phnum
5980 * (is_32bit_elf ? sizeof (Elf32_External_Phdr) : sizeof (Elf64_External_Phdr))
5981 >= filedata->file_size)
5982 {
5983 error (_("Too many program headers - %#x - the file is not that big\n"),
5984 filedata->file_header.e_phnum);
5985 return false;
5986 }
5987
5988 phdrs = (Elf_Internal_Phdr *) cmalloc (filedata->file_header.e_phnum,
5989 sizeof (Elf_Internal_Phdr));
5990 if (phdrs == NULL)
5991 {
5992 error (_("Out of memory reading %u program headers\n"),
5993 filedata->file_header.e_phnum);
5994 return false;
5995 }
5996
5997 if (is_32bit_elf
5998 ? get_32bit_program_headers (filedata, phdrs)
5999 : get_64bit_program_headers (filedata, phdrs))
6000 {
6001 filedata->program_headers = phdrs;
6002 return true;
6003 }
6004
6005 free (phdrs);
6006 return false;
6007 }
6008
6009 /* Print program header info and locate dynamic section. */
6010
6011 static void
6012 process_program_headers (Filedata * filedata)
6013 {
6014 Elf_Internal_Phdr * segment;
6015 unsigned int i;
6016 Elf_Internal_Phdr * previous_load = NULL;
6017
6018 if (filedata->file_header.e_phnum == 0)
6019 {
6020 /* PR binutils/12467. */
6021 if (filedata->file_header.e_phoff != 0)
6022 warn (_("possibly corrupt ELF header - it has a non-zero program"
6023 " header offset, but no program headers\n"));
6024 else if (do_segments)
6025 {
6026 if (filedata->is_separate)
6027 printf (_("\nThere are no program headers in linked file '%s'.\n"),
6028 filedata->file_name);
6029 else
6030 printf (_("\nThere are no program headers in this file.\n"));
6031 }
6032 goto no_headers;
6033 }
6034
6035 if (do_segments && !do_header)
6036 {
6037 if (filedata->is_separate)
6038 printf ("\nIn linked file '%s' the ELF file type is %s\n",
6039 filedata->file_name, get_file_type (filedata));
6040 else
6041 printf (_("\nElf file type is %s\n"), get_file_type (filedata));
6042 printf (_("Entry point 0x%" PRIx64 "\n"),
6043 filedata->file_header.e_entry);
6044 printf (ngettext ("There is %d program header,"
6045 " starting at offset %" PRIu64 "\n",
6046 "There are %d program headers,"
6047 " starting at offset %" PRIu64 "\n",
6048 filedata->file_header.e_phnum),
6049 filedata->file_header.e_phnum,
6050 filedata->file_header.e_phoff);
6051 }
6052
6053 if (! get_program_headers (filedata))
6054 goto no_headers;
6055
6056 if (do_segments)
6057 {
6058 if (filedata->file_header.e_phnum > 1)
6059 printf (_("\nProgram Headers:\n"));
6060 else
6061 printf (_("\nProgram Headers:\n"));
6062
6063 if (is_32bit_elf)
6064 printf
6065 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
6066 else if (do_wide)
6067 printf
6068 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
6069 else
6070 {
6071 printf
6072 (_(" Type Offset VirtAddr PhysAddr\n"));
6073 printf
6074 (_(" FileSiz MemSiz Flags Align\n"));
6075 }
6076 }
6077
6078 unsigned long dynamic_addr = 0;
6079 uint64_t dynamic_size = 0;
6080 for (i = 0, segment = filedata->program_headers;
6081 i < filedata->file_header.e_phnum;
6082 i++, segment++)
6083 {
6084 if (do_segments)
6085 {
6086 printf (" %-14.14s ", get_segment_type (filedata, segment->p_type));
6087
6088 if (is_32bit_elf)
6089 {
6090 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
6091 printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
6092 printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
6093 printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
6094 printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
6095 printf ("%c%c%c ",
6096 (segment->p_flags & PF_R ? 'R' : ' '),
6097 (segment->p_flags & PF_W ? 'W' : ' '),
6098 (segment->p_flags & PF_X ? 'E' : ' '));
6099 printf ("%#lx", (unsigned long) segment->p_align);
6100 }
6101 else if (do_wide)
6102 {
6103 if ((unsigned long) segment->p_offset == segment->p_offset)
6104 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
6105 else
6106 {
6107 print_vma (segment->p_offset, FULL_HEX);
6108 putchar (' ');
6109 }
6110
6111 print_vma (segment->p_vaddr, FULL_HEX);
6112 putchar (' ');
6113 print_vma (segment->p_paddr, FULL_HEX);
6114 putchar (' ');
6115
6116 if ((unsigned long) segment->p_filesz == segment->p_filesz)
6117 printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
6118 else
6119 {
6120 print_vma (segment->p_filesz, FULL_HEX);
6121 putchar (' ');
6122 }
6123
6124 if ((unsigned long) segment->p_memsz == segment->p_memsz)
6125 printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
6126 else
6127 {
6128 print_vma (segment->p_memsz, FULL_HEX);
6129 }
6130
6131 printf (" %c%c%c ",
6132 (segment->p_flags & PF_R ? 'R' : ' '),
6133 (segment->p_flags & PF_W ? 'W' : ' '),
6134 (segment->p_flags & PF_X ? 'E' : ' '));
6135
6136 if ((unsigned long) segment->p_align == segment->p_align)
6137 printf ("%#lx", (unsigned long) segment->p_align);
6138 else
6139 {
6140 print_vma (segment->p_align, PREFIX_HEX);
6141 }
6142 }
6143 else
6144 {
6145 print_vma (segment->p_offset, FULL_HEX);
6146 putchar (' ');
6147 print_vma (segment->p_vaddr, FULL_HEX);
6148 putchar (' ');
6149 print_vma (segment->p_paddr, FULL_HEX);
6150 printf ("\n ");
6151 print_vma (segment->p_filesz, FULL_HEX);
6152 putchar (' ');
6153 print_vma (segment->p_memsz, FULL_HEX);
6154 printf (" %c%c%c ",
6155 (segment->p_flags & PF_R ? 'R' : ' '),
6156 (segment->p_flags & PF_W ? 'W' : ' '),
6157 (segment->p_flags & PF_X ? 'E' : ' '));
6158 print_vma (segment->p_align, PREFIX_HEX);
6159 }
6160
6161 putc ('\n', stdout);
6162 }
6163
6164 switch (segment->p_type)
6165 {
6166 case PT_LOAD:
6167 #if 0 /* Do not warn about out of order PT_LOAD segments. Although officially
6168 required by the ELF standard, several programs, including the Linux
6169 kernel, make use of non-ordered segments. */
6170 if (previous_load
6171 && previous_load->p_vaddr > segment->p_vaddr)
6172 error (_("LOAD segments must be sorted in order of increasing VirtAddr\n"));
6173 #endif
6174 if (segment->p_memsz < segment->p_filesz)
6175 error (_("the segment's file size is larger than its memory size\n"));
6176 previous_load = segment;
6177 break;
6178
6179 case PT_PHDR:
6180 /* PR 20815 - Verify that the program header is loaded into memory. */
6181 if (i > 0 && previous_load != NULL)
6182 error (_("the PHDR segment must occur before any LOAD segment\n"));
6183 if (filedata->file_header.e_machine != EM_PARISC)
6184 {
6185 unsigned int j;
6186
6187 for (j = 1; j < filedata->file_header.e_phnum; j++)
6188 {
6189 Elf_Internal_Phdr *load = filedata->program_headers + j;
6190 if (load->p_type == PT_LOAD
6191 && load->p_offset <= segment->p_offset
6192 && (load->p_offset + load->p_filesz
6193 >= segment->p_offset + segment->p_filesz)
6194 && load->p_vaddr <= segment->p_vaddr
6195 && (load->p_vaddr + load->p_filesz
6196 >= segment->p_vaddr + segment->p_filesz))
6197 break;
6198 }
6199 if (j == filedata->file_header.e_phnum)
6200 error (_("the PHDR segment is not covered by a LOAD segment\n"));
6201 }
6202 break;
6203
6204 case PT_DYNAMIC:
6205 if (dynamic_addr)
6206 error (_("more than one dynamic segment\n"));
6207
6208 /* By default, assume that the .dynamic section is the first
6209 section in the DYNAMIC segment. */
6210 dynamic_addr = segment->p_offset;
6211 dynamic_size = segment->p_filesz;
6212
6213 /* Try to locate the .dynamic section. If there is
6214 a section header table, we can easily locate it. */
6215 if (filedata->section_headers != NULL)
6216 {
6217 Elf_Internal_Shdr * sec;
6218
6219 sec = find_section (filedata, ".dynamic");
6220 if (sec == NULL || sec->sh_size == 0)
6221 {
6222 /* A corresponding .dynamic section is expected, but on
6223 IA-64/OpenVMS it is OK for it to be missing. */
6224 if (!is_ia64_vms (filedata))
6225 error (_("no .dynamic section in the dynamic segment\n"));
6226 break;
6227 }
6228
6229 if (sec->sh_type == SHT_NOBITS)
6230 {
6231 dynamic_addr = 0;
6232 dynamic_size = 0;
6233 break;
6234 }
6235
6236 dynamic_addr = sec->sh_offset;
6237 dynamic_size = sec->sh_size;
6238
6239 /* The PT_DYNAMIC segment, which is used by the run-time
6240 loader, should exactly match the .dynamic section. */
6241 if (do_checks
6242 && (dynamic_addr != segment->p_offset
6243 || dynamic_size != segment->p_filesz))
6244 warn (_("\
6245 the .dynamic section is not the same as the dynamic segment\n"));
6246 }
6247
6248 /* PR binutils/17512: Avoid corrupt dynamic section info in the
6249 segment. Check this after matching against the section headers
6250 so we don't warn on debuginfo file (which have NOBITS .dynamic
6251 sections). */
6252 if (dynamic_addr > filedata->file_size
6253 || (dynamic_size > filedata->file_size - dynamic_addr))
6254 {
6255 error (_("the dynamic segment offset + size exceeds the size of the file\n"));
6256 dynamic_addr = 0;
6257 dynamic_size = 0;
6258 }
6259 break;
6260
6261 case PT_INTERP:
6262 if (segment->p_offset >= filedata->file_size
6263 || segment->p_filesz > filedata->file_size - segment->p_offset
6264 || segment->p_filesz - 1 >= (size_t) -2
6265 || fseek (filedata->handle,
6266 filedata->archive_file_offset + (long) segment->p_offset,
6267 SEEK_SET))
6268 error (_("Unable to find program interpreter name\n"));
6269 else
6270 {
6271 size_t len = segment->p_filesz;
6272 free (filedata->program_interpreter);
6273 filedata->program_interpreter = xmalloc (len + 1);
6274 len = fread (filedata->program_interpreter, 1, len,
6275 filedata->handle);
6276 filedata->program_interpreter[len] = 0;
6277
6278 if (do_segments)
6279 printf (_(" [Requesting program interpreter: %s]\n"),
6280 filedata->program_interpreter);
6281 }
6282 break;
6283 }
6284 }
6285
6286 if (do_segments
6287 && filedata->section_headers != NULL
6288 && filedata->string_table != NULL)
6289 {
6290 printf (_("\n Section to Segment mapping:\n"));
6291 printf (_(" Segment Sections...\n"));
6292
6293 for (i = 0; i < filedata->file_header.e_phnum; i++)
6294 {
6295 unsigned int j;
6296 Elf_Internal_Shdr * section;
6297
6298 segment = filedata->program_headers + i;
6299 section = filedata->section_headers + 1;
6300
6301 printf (" %2.2d ", i);
6302
6303 for (j = 1; j < filedata->file_header.e_shnum; j++, section++)
6304 {
6305 if (!ELF_TBSS_SPECIAL (section, segment)
6306 && ELF_SECTION_IN_SEGMENT_STRICT (section, segment))
6307 printf ("%s ", printable_section_name (filedata, section));
6308 }
6309
6310 putc ('\n',stdout);
6311 }
6312 }
6313
6314 filedata->dynamic_addr = dynamic_addr;
6315 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
6316 return;
6317
6318 no_headers:
6319 filedata->dynamic_addr = 0;
6320 filedata->dynamic_size = 1;
6321 }
6322
6323
6324 /* Find the file offset corresponding to VMA by using the program headers. */
6325
6326 static long
6327 offset_from_vma (Filedata * filedata, uint64_t vma, uint64_t size)
6328 {
6329 Elf_Internal_Phdr * seg;
6330
6331 if (! get_program_headers (filedata))
6332 {
6333 warn (_("Cannot interpret virtual addresses without program headers.\n"));
6334 return (long) vma;
6335 }
6336
6337 for (seg = filedata->program_headers;
6338 seg < filedata->program_headers + filedata->file_header.e_phnum;
6339 ++seg)
6340 {
6341 if (seg->p_type != PT_LOAD)
6342 continue;
6343
6344 if (vma >= (seg->p_vaddr & -seg->p_align)
6345 && vma + size <= seg->p_vaddr + seg->p_filesz)
6346 return vma - seg->p_vaddr + seg->p_offset;
6347 }
6348
6349 warn (_("Virtual address 0x%lx not located in any PT_LOAD segment.\n"),
6350 (unsigned long) vma);
6351 return (long) vma;
6352 }
6353
6354
6355 /* Allocate memory and load the sections headers into FILEDATA->filedata->section_headers.
6356 If PROBE is true, this is just a probe and we do not generate any error
6357 messages if the load fails. */
6358
6359 static bool
6360 get_32bit_section_headers (Filedata * filedata, bool probe)
6361 {
6362 Elf32_External_Shdr * shdrs;
6363 Elf_Internal_Shdr * internal;
6364 unsigned int i;
6365 unsigned int size = filedata->file_header.e_shentsize;
6366 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
6367
6368 /* PR binutils/17531: Cope with unexpected section header sizes. */
6369 if (size == 0 || num == 0)
6370 return false;
6371
6372 /* The section header cannot be at the start of the file - that is
6373 where the ELF file header is located. A file with absolutely no
6374 sections in it will use a shoff of 0. */
6375 if (filedata->file_header.e_shoff == 0)
6376 return false;
6377
6378 if (size < sizeof * shdrs)
6379 {
6380 if (! probe)
6381 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
6382 return false;
6383 }
6384 if (!probe && size > sizeof * shdrs)
6385 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
6386
6387 shdrs = (Elf32_External_Shdr *) get_data (NULL, filedata, filedata->file_header.e_shoff,
6388 size, num,
6389 probe ? NULL : _("section headers"));
6390 if (shdrs == NULL)
6391 return false;
6392
6393 filedata->section_headers = (Elf_Internal_Shdr *)
6394 cmalloc (num, sizeof (Elf_Internal_Shdr));
6395 if (filedata->section_headers == NULL)
6396 {
6397 if (!probe)
6398 error (_("Out of memory reading %u section headers\n"), num);
6399 free (shdrs);
6400 return false;
6401 }
6402
6403 for (i = 0, internal = filedata->section_headers;
6404 i < num;
6405 i++, internal++)
6406 {
6407 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
6408 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
6409 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
6410 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
6411 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
6412 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
6413 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
6414 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
6415 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
6416 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
6417 if (!probe && internal->sh_link > num)
6418 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
6419 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
6420 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
6421 }
6422
6423 free (shdrs);
6424 return true;
6425 }
6426
6427 /* Like get_32bit_section_headers, except that it fetches 64-bit headers. */
6428
6429 static bool
6430 get_64bit_section_headers (Filedata * filedata, bool probe)
6431 {
6432 Elf64_External_Shdr * shdrs;
6433 Elf_Internal_Shdr * internal;
6434 unsigned int i;
6435 unsigned int size = filedata->file_header.e_shentsize;
6436 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
6437
6438 /* PR binutils/17531: Cope with unexpected section header sizes. */
6439 if (size == 0 || num == 0)
6440 return false;
6441
6442 /* The section header cannot be at the start of the file - that is
6443 where the ELF file header is located. A file with absolutely no
6444 sections in it will use a shoff of 0. */
6445 if (filedata->file_header.e_shoff == 0)
6446 return false;
6447
6448 if (size < sizeof * shdrs)
6449 {
6450 if (! probe)
6451 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
6452 return false;
6453 }
6454
6455 if (! probe && size > sizeof * shdrs)
6456 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
6457
6458 shdrs = (Elf64_External_Shdr *) get_data (NULL, filedata,
6459 filedata->file_header.e_shoff,
6460 size, num,
6461 probe ? NULL : _("section headers"));
6462 if (shdrs == NULL)
6463 return false;
6464
6465 filedata->section_headers = (Elf_Internal_Shdr *)
6466 cmalloc (num, sizeof (Elf_Internal_Shdr));
6467 if (filedata->section_headers == NULL)
6468 {
6469 if (! probe)
6470 error (_("Out of memory reading %u section headers\n"), num);
6471 free (shdrs);
6472 return false;
6473 }
6474
6475 for (i = 0, internal = filedata->section_headers;
6476 i < num;
6477 i++, internal++)
6478 {
6479 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
6480 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
6481 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
6482 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
6483 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
6484 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
6485 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
6486 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
6487 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
6488 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
6489 if (!probe && internal->sh_link > num)
6490 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
6491 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
6492 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
6493 }
6494
6495 free (shdrs);
6496 return true;
6497 }
6498
6499 static bool
6500 get_section_headers (Filedata *filedata, bool probe)
6501 {
6502 if (filedata->section_headers != NULL)
6503 return true;
6504
6505 if (is_32bit_elf)
6506 return get_32bit_section_headers (filedata, probe);
6507 else
6508 return get_64bit_section_headers (filedata, probe);
6509 }
6510
6511 static Elf_Internal_Sym *
6512 get_32bit_elf_symbols (Filedata * filedata,
6513 Elf_Internal_Shdr * section,
6514 unsigned long * num_syms_return)
6515 {
6516 unsigned long number = 0;
6517 Elf32_External_Sym * esyms = NULL;
6518 Elf_External_Sym_Shndx * shndx = NULL;
6519 Elf_Internal_Sym * isyms = NULL;
6520 Elf_Internal_Sym * psym;
6521 unsigned int j;
6522 elf_section_list * entry;
6523
6524 if (section->sh_size == 0)
6525 {
6526 if (num_syms_return != NULL)
6527 * num_syms_return = 0;
6528 return NULL;
6529 }
6530
6531 /* Run some sanity checks first. */
6532 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
6533 {
6534 error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
6535 printable_section_name (filedata, section),
6536 (unsigned long) section->sh_entsize);
6537 goto exit_point;
6538 }
6539
6540 if (section->sh_size > filedata->file_size)
6541 {
6542 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
6543 printable_section_name (filedata, section),
6544 (unsigned long) section->sh_size);
6545 goto exit_point;
6546 }
6547
6548 number = section->sh_size / section->sh_entsize;
6549
6550 if (number * sizeof (Elf32_External_Sym) > section->sh_size + 1)
6551 {
6552 error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
6553 (unsigned long) section->sh_size,
6554 printable_section_name (filedata, section),
6555 (unsigned long) section->sh_entsize);
6556 goto exit_point;
6557 }
6558
6559 esyms = (Elf32_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
6560 section->sh_size, _("symbols"));
6561 if (esyms == NULL)
6562 goto exit_point;
6563
6564 shndx = NULL;
6565 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
6566 {
6567 if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
6568 continue;
6569
6570 if (shndx != NULL)
6571 {
6572 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
6573 free (shndx);
6574 }
6575
6576 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
6577 entry->hdr->sh_offset,
6578 1, entry->hdr->sh_size,
6579 _("symbol table section indices"));
6580 if (shndx == NULL)
6581 goto exit_point;
6582
6583 /* PR17531: file: heap-buffer-overflow */
6584 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
6585 {
6586 error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
6587 printable_section_name (filedata, entry->hdr),
6588 (unsigned long) entry->hdr->sh_size,
6589 (unsigned long) section->sh_size);
6590 goto exit_point;
6591 }
6592 }
6593
6594 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
6595
6596 if (isyms == NULL)
6597 {
6598 error (_("Out of memory reading %lu symbols\n"),
6599 (unsigned long) number);
6600 goto exit_point;
6601 }
6602
6603 for (j = 0, psym = isyms; j < number; j++, psym++)
6604 {
6605 psym->st_name = BYTE_GET (esyms[j].st_name);
6606 psym->st_value = BYTE_GET (esyms[j].st_value);
6607 psym->st_size = BYTE_GET (esyms[j].st_size);
6608 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
6609 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
6610 psym->st_shndx
6611 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
6612 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
6613 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
6614 psym->st_info = BYTE_GET (esyms[j].st_info);
6615 psym->st_other = BYTE_GET (esyms[j].st_other);
6616 }
6617
6618 exit_point:
6619 free (shndx);
6620 free (esyms);
6621
6622 if (num_syms_return != NULL)
6623 * num_syms_return = isyms == NULL ? 0 : number;
6624
6625 return isyms;
6626 }
6627
6628 static Elf_Internal_Sym *
6629 get_64bit_elf_symbols (Filedata * filedata,
6630 Elf_Internal_Shdr * section,
6631 unsigned long * num_syms_return)
6632 {
6633 unsigned long number = 0;
6634 Elf64_External_Sym * esyms = NULL;
6635 Elf_External_Sym_Shndx * shndx = NULL;
6636 Elf_Internal_Sym * isyms = NULL;
6637 Elf_Internal_Sym * psym;
6638 unsigned int j;
6639 elf_section_list * entry;
6640
6641 if (section->sh_size == 0)
6642 {
6643 if (num_syms_return != NULL)
6644 * num_syms_return = 0;
6645 return NULL;
6646 }
6647
6648 /* Run some sanity checks first. */
6649 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
6650 {
6651 error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
6652 printable_section_name (filedata, section),
6653 (unsigned long) section->sh_entsize);
6654 goto exit_point;
6655 }
6656
6657 if (section->sh_size > filedata->file_size)
6658 {
6659 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
6660 printable_section_name (filedata, section),
6661 (unsigned long) section->sh_size);
6662 goto exit_point;
6663 }
6664
6665 number = section->sh_size / section->sh_entsize;
6666
6667 if (number * sizeof (Elf64_External_Sym) > section->sh_size + 1)
6668 {
6669 error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
6670 (unsigned long) section->sh_size,
6671 printable_section_name (filedata, section),
6672 (unsigned long) section->sh_entsize);
6673 goto exit_point;
6674 }
6675
6676 esyms = (Elf64_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
6677 section->sh_size, _("symbols"));
6678 if (!esyms)
6679 goto exit_point;
6680
6681 shndx = NULL;
6682 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
6683 {
6684 if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
6685 continue;
6686
6687 if (shndx != NULL)
6688 {
6689 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
6690 free (shndx);
6691 }
6692
6693 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
6694 entry->hdr->sh_offset,
6695 1, entry->hdr->sh_size,
6696 _("symbol table section indices"));
6697 if (shndx == NULL)
6698 goto exit_point;
6699
6700 /* PR17531: file: heap-buffer-overflow */
6701 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
6702 {
6703 error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
6704 printable_section_name (filedata, entry->hdr),
6705 (unsigned long) entry->hdr->sh_size,
6706 (unsigned long) section->sh_size);
6707 goto exit_point;
6708 }
6709 }
6710
6711 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
6712
6713 if (isyms == NULL)
6714 {
6715 error (_("Out of memory reading %lu symbols\n"),
6716 (unsigned long) number);
6717 goto exit_point;
6718 }
6719
6720 for (j = 0, psym = isyms; j < number; j++, psym++)
6721 {
6722 psym->st_name = BYTE_GET (esyms[j].st_name);
6723 psym->st_info = BYTE_GET (esyms[j].st_info);
6724 psym->st_other = BYTE_GET (esyms[j].st_other);
6725 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
6726
6727 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
6728 psym->st_shndx
6729 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
6730 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
6731 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
6732
6733 psym->st_value = BYTE_GET (esyms[j].st_value);
6734 psym->st_size = BYTE_GET (esyms[j].st_size);
6735 }
6736
6737 exit_point:
6738 free (shndx);
6739 free (esyms);
6740
6741 if (num_syms_return != NULL)
6742 * num_syms_return = isyms == NULL ? 0 : number;
6743
6744 return isyms;
6745 }
6746
6747 static Elf_Internal_Sym *
6748 get_elf_symbols (Filedata *filedata,
6749 Elf_Internal_Shdr *section,
6750 unsigned long *num_syms_return)
6751 {
6752 if (is_32bit_elf)
6753 return get_32bit_elf_symbols (filedata, section, num_syms_return);
6754 else
6755 return get_64bit_elf_symbols (filedata, section, num_syms_return);
6756 }
6757
6758 static const char *
6759 get_elf_section_flags (Filedata * filedata, uint64_t sh_flags)
6760 {
6761 static char buff[1024];
6762 char * p = buff;
6763 unsigned int field_size = is_32bit_elf ? 8 : 16;
6764 signed int sindex;
6765 unsigned int size = sizeof (buff) - (field_size + 4 + 1);
6766 uint64_t os_flags = 0;
6767 uint64_t proc_flags = 0;
6768 uint64_t unknown_flags = 0;
6769 static const struct
6770 {
6771 const char * str;
6772 unsigned int len;
6773 }
6774 flags [] =
6775 {
6776 /* 0 */ { STRING_COMMA_LEN ("WRITE") },
6777 /* 1 */ { STRING_COMMA_LEN ("ALLOC") },
6778 /* 2 */ { STRING_COMMA_LEN ("EXEC") },
6779 /* 3 */ { STRING_COMMA_LEN ("MERGE") },
6780 /* 4 */ { STRING_COMMA_LEN ("STRINGS") },
6781 /* 5 */ { STRING_COMMA_LEN ("INFO LINK") },
6782 /* 6 */ { STRING_COMMA_LEN ("LINK ORDER") },
6783 /* 7 */ { STRING_COMMA_LEN ("OS NONCONF") },
6784 /* 8 */ { STRING_COMMA_LEN ("GROUP") },
6785 /* 9 */ { STRING_COMMA_LEN ("TLS") },
6786 /* IA-64 specific. */
6787 /* 10 */ { STRING_COMMA_LEN ("SHORT") },
6788 /* 11 */ { STRING_COMMA_LEN ("NORECOV") },
6789 /* IA-64 OpenVMS specific. */
6790 /* 12 */ { STRING_COMMA_LEN ("VMS_GLOBAL") },
6791 /* 13 */ { STRING_COMMA_LEN ("VMS_OVERLAID") },
6792 /* 14 */ { STRING_COMMA_LEN ("VMS_SHARED") },
6793 /* 15 */ { STRING_COMMA_LEN ("VMS_VECTOR") },
6794 /* 16 */ { STRING_COMMA_LEN ("VMS_ALLOC_64BIT") },
6795 /* 17 */ { STRING_COMMA_LEN ("VMS_PROTECTED") },
6796 /* Generic. */
6797 /* 18 */ { STRING_COMMA_LEN ("EXCLUDE") },
6798 /* SPARC specific. */
6799 /* 19 */ { STRING_COMMA_LEN ("ORDERED") },
6800 /* 20 */ { STRING_COMMA_LEN ("COMPRESSED") },
6801 /* ARM specific. */
6802 /* 21 */ { STRING_COMMA_LEN ("ENTRYSECT") },
6803 /* 22 */ { STRING_COMMA_LEN ("ARM_PURECODE") },
6804 /* 23 */ { STRING_COMMA_LEN ("COMDEF") },
6805 /* GNU specific. */
6806 /* 24 */ { STRING_COMMA_LEN ("GNU_MBIND") },
6807 /* VLE specific. */
6808 /* 25 */ { STRING_COMMA_LEN ("VLE") },
6809 /* GNU specific. */
6810 /* 26 */ { STRING_COMMA_LEN ("GNU_RETAIN") },
6811 };
6812
6813 if (do_section_details)
6814 {
6815 sprintf (buff, "[%*.*lx]: ",
6816 field_size, field_size, (unsigned long) sh_flags);
6817 p += field_size + 4;
6818 }
6819
6820 while (sh_flags)
6821 {
6822 uint64_t flag;
6823
6824 flag = sh_flags & - sh_flags;
6825 sh_flags &= ~ flag;
6826
6827 if (do_section_details)
6828 {
6829 switch (flag)
6830 {
6831 case SHF_WRITE: sindex = 0; break;
6832 case SHF_ALLOC: sindex = 1; break;
6833 case SHF_EXECINSTR: sindex = 2; break;
6834 case SHF_MERGE: sindex = 3; break;
6835 case SHF_STRINGS: sindex = 4; break;
6836 case SHF_INFO_LINK: sindex = 5; break;
6837 case SHF_LINK_ORDER: sindex = 6; break;
6838 case SHF_OS_NONCONFORMING: sindex = 7; break;
6839 case SHF_GROUP: sindex = 8; break;
6840 case SHF_TLS: sindex = 9; break;
6841 case SHF_EXCLUDE: sindex = 18; break;
6842 case SHF_COMPRESSED: sindex = 20; break;
6843
6844 default:
6845 sindex = -1;
6846 switch (filedata->file_header.e_machine)
6847 {
6848 case EM_IA_64:
6849 if (flag == SHF_IA_64_SHORT)
6850 sindex = 10;
6851 else if (flag == SHF_IA_64_NORECOV)
6852 sindex = 11;
6853 else if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
6854 switch (flag)
6855 {
6856 case SHF_IA_64_VMS_GLOBAL: sindex = 12; break;
6857 case SHF_IA_64_VMS_OVERLAID: sindex = 13; break;
6858 case SHF_IA_64_VMS_SHARED: sindex = 14; break;
6859 case SHF_IA_64_VMS_VECTOR: sindex = 15; break;
6860 case SHF_IA_64_VMS_ALLOC_64BIT: sindex = 16; break;
6861 case SHF_IA_64_VMS_PROTECTED: sindex = 17; break;
6862 default: break;
6863 }
6864 break;
6865
6866 case EM_386:
6867 case EM_IAMCU:
6868 case EM_X86_64:
6869 case EM_L1OM:
6870 case EM_K1OM:
6871 case EM_OLD_SPARCV9:
6872 case EM_SPARC32PLUS:
6873 case EM_SPARCV9:
6874 case EM_SPARC:
6875 if (flag == SHF_ORDERED)
6876 sindex = 19;
6877 break;
6878
6879 case EM_ARM:
6880 switch (flag)
6881 {
6882 case SHF_ENTRYSECT: sindex = 21; break;
6883 case SHF_ARM_PURECODE: sindex = 22; break;
6884 case SHF_COMDEF: sindex = 23; break;
6885 default: break;
6886 }
6887 break;
6888 case EM_PPC:
6889 if (flag == SHF_PPC_VLE)
6890 sindex = 25;
6891 break;
6892 default:
6893 break;
6894 }
6895
6896 switch (filedata->file_header.e_ident[EI_OSABI])
6897 {
6898 case ELFOSABI_GNU:
6899 case ELFOSABI_FREEBSD:
6900 if (flag == SHF_GNU_RETAIN)
6901 sindex = 26;
6902 /* Fall through */
6903 case ELFOSABI_NONE:
6904 if (flag == SHF_GNU_MBIND)
6905 /* We should not recognize SHF_GNU_MBIND for
6906 ELFOSABI_NONE, but binutils as of 2019-07-23 did
6907 not set the EI_OSABI header byte. */
6908 sindex = 24;
6909 break;
6910 default:
6911 break;
6912 }
6913 break;
6914 }
6915
6916 if (sindex != -1)
6917 {
6918 if (p != buff + field_size + 4)
6919 {
6920 if (size < (10 + 2))
6921 {
6922 warn (_("Internal error: not enough buffer room for section flag info"));
6923 return _("<unknown>");
6924 }
6925 size -= 2;
6926 *p++ = ',';
6927 *p++ = ' ';
6928 }
6929
6930 size -= flags [sindex].len;
6931 p = stpcpy (p, flags [sindex].str);
6932 }
6933 else if (flag & SHF_MASKOS)
6934 os_flags |= flag;
6935 else if (flag & SHF_MASKPROC)
6936 proc_flags |= flag;
6937 else
6938 unknown_flags |= flag;
6939 }
6940 else
6941 {
6942 switch (flag)
6943 {
6944 case SHF_WRITE: *p = 'W'; break;
6945 case SHF_ALLOC: *p = 'A'; break;
6946 case SHF_EXECINSTR: *p = 'X'; break;
6947 case SHF_MERGE: *p = 'M'; break;
6948 case SHF_STRINGS: *p = 'S'; break;
6949 case SHF_INFO_LINK: *p = 'I'; break;
6950 case SHF_LINK_ORDER: *p = 'L'; break;
6951 case SHF_OS_NONCONFORMING: *p = 'O'; break;
6952 case SHF_GROUP: *p = 'G'; break;
6953 case SHF_TLS: *p = 'T'; break;
6954 case SHF_EXCLUDE: *p = 'E'; break;
6955 case SHF_COMPRESSED: *p = 'C'; break;
6956
6957 default:
6958 if ((filedata->file_header.e_machine == EM_X86_64
6959 || filedata->file_header.e_machine == EM_L1OM
6960 || filedata->file_header.e_machine == EM_K1OM)
6961 && flag == SHF_X86_64_LARGE)
6962 *p = 'l';
6963 else if (filedata->file_header.e_machine == EM_ARM
6964 && flag == SHF_ARM_PURECODE)
6965 *p = 'y';
6966 else if (filedata->file_header.e_machine == EM_PPC
6967 && flag == SHF_PPC_VLE)
6968 *p = 'v';
6969 else if (flag & SHF_MASKOS)
6970 {
6971 switch (filedata->file_header.e_ident[EI_OSABI])
6972 {
6973 case ELFOSABI_GNU:
6974 case ELFOSABI_FREEBSD:
6975 if (flag == SHF_GNU_RETAIN)
6976 {
6977 *p = 'R';
6978 break;
6979 }
6980 /* Fall through */
6981 case ELFOSABI_NONE:
6982 if (flag == SHF_GNU_MBIND)
6983 {
6984 /* We should not recognize SHF_GNU_MBIND for
6985 ELFOSABI_NONE, but binutils as of 2019-07-23 did
6986 not set the EI_OSABI header byte. */
6987 *p = 'D';
6988 break;
6989 }
6990 /* Fall through */
6991 default:
6992 *p = 'o';
6993 sh_flags &= ~SHF_MASKOS;
6994 break;
6995 }
6996 }
6997 else if (flag & SHF_MASKPROC)
6998 {
6999 *p = 'p';
7000 sh_flags &= ~ SHF_MASKPROC;
7001 }
7002 else
7003 *p = 'x';
7004 break;
7005 }
7006 p++;
7007 }
7008 }
7009
7010 if (do_section_details)
7011 {
7012 if (os_flags)
7013 {
7014 size -= 5 + field_size;
7015 if (p != buff + field_size + 4)
7016 {
7017 if (size < (2 + 1))
7018 {
7019 warn (_("Internal error: not enough buffer room for section flag info"));
7020 return _("<unknown>");
7021 }
7022 size -= 2;
7023 *p++ = ',';
7024 *p++ = ' ';
7025 }
7026 sprintf (p, "OS (%*.*lx)", field_size, field_size,
7027 (unsigned long) os_flags);
7028 p += 5 + field_size;
7029 }
7030 if (proc_flags)
7031 {
7032 size -= 7 + field_size;
7033 if (p != buff + field_size + 4)
7034 {
7035 if (size < (2 + 1))
7036 {
7037 warn (_("Internal error: not enough buffer room for section flag info"));
7038 return _("<unknown>");
7039 }
7040 size -= 2;
7041 *p++ = ',';
7042 *p++ = ' ';
7043 }
7044 sprintf (p, "PROC (%*.*lx)", field_size, field_size,
7045 (unsigned long) proc_flags);
7046 p += 7 + field_size;
7047 }
7048 if (unknown_flags)
7049 {
7050 size -= 10 + field_size;
7051 if (p != buff + field_size + 4)
7052 {
7053 if (size < (2 + 1))
7054 {
7055 warn (_("Internal error: not enough buffer room for section flag info"));
7056 return _("<unknown>");
7057 }
7058 size -= 2;
7059 *p++ = ',';
7060 *p++ = ' ';
7061 }
7062 sprintf (p, _("UNKNOWN (%*.*lx)"), field_size, field_size,
7063 (unsigned long) unknown_flags);
7064 p += 10 + field_size;
7065 }
7066 }
7067
7068 *p = '\0';
7069 return buff;
7070 }
7071
7072 static unsigned int ATTRIBUTE_WARN_UNUSED_RESULT
7073 get_compression_header (Elf_Internal_Chdr *chdr, unsigned char *buf,
7074 uint64_t size)
7075 {
7076 if (is_32bit_elf)
7077 {
7078 Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) buf;
7079
7080 if (size < sizeof (* echdr))
7081 {
7082 error (_("Compressed section is too small even for a compression header\n"));
7083 return 0;
7084 }
7085
7086 chdr->ch_type = BYTE_GET (echdr->ch_type);
7087 chdr->ch_size = BYTE_GET (echdr->ch_size);
7088 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
7089 return sizeof (*echdr);
7090 }
7091 else
7092 {
7093 Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) buf;
7094
7095 if (size < sizeof (* echdr))
7096 {
7097 error (_("Compressed section is too small even for a compression header\n"));
7098 return 0;
7099 }
7100
7101 chdr->ch_type = BYTE_GET (echdr->ch_type);
7102 chdr->ch_size = BYTE_GET (echdr->ch_size);
7103 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
7104 return sizeof (*echdr);
7105 }
7106 }
7107
7108 static bool
7109 process_section_headers (Filedata * filedata)
7110 {
7111 Elf_Internal_Shdr * section;
7112 unsigned int i;
7113
7114 if (filedata->file_header.e_shnum == 0)
7115 {
7116 /* PR binutils/12467. */
7117 if (filedata->file_header.e_shoff != 0)
7118 {
7119 warn (_("possibly corrupt ELF file header - it has a non-zero"
7120 " section header offset, but no section headers\n"));
7121 return false;
7122 }
7123 else if (do_sections)
7124 printf (_("\nThere are no sections in this file.\n"));
7125
7126 return true;
7127 }
7128
7129 if (do_sections && !do_header)
7130 {
7131 if (filedata->is_separate && process_links)
7132 printf (_("In linked file '%s': "), filedata->file_name);
7133 if (! filedata->is_separate || process_links)
7134 printf (ngettext ("There is %d section header, "
7135 "starting at offset 0x%lx:\n",
7136 "There are %d section headers, "
7137 "starting at offset 0x%lx:\n",
7138 filedata->file_header.e_shnum),
7139 filedata->file_header.e_shnum,
7140 (unsigned long) filedata->file_header.e_shoff);
7141 }
7142
7143 if (!get_section_headers (filedata, false))
7144 return false;
7145
7146 /* Read in the string table, so that we have names to display. */
7147 if (filedata->file_header.e_shstrndx != SHN_UNDEF
7148 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
7149 {
7150 section = filedata->section_headers + filedata->file_header.e_shstrndx;
7151
7152 if (section->sh_size != 0)
7153 {
7154 filedata->string_table = (char *) get_data (NULL, filedata, section->sh_offset,
7155 1, section->sh_size,
7156 _("string table"));
7157
7158 filedata->string_table_length = filedata->string_table != NULL ? section->sh_size : 0;
7159 }
7160 }
7161
7162 /* Scan the sections for the dynamic symbol table
7163 and dynamic string table and debug sections. */
7164 eh_addr_size = is_32bit_elf ? 4 : 8;
7165 switch (filedata->file_header.e_machine)
7166 {
7167 case EM_MIPS:
7168 case EM_MIPS_RS3_LE:
7169 /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
7170 FDE addresses. However, the ABI also has a semi-official ILP32
7171 variant for which the normal FDE address size rules apply.
7172
7173 GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
7174 section, where XX is the size of longs in bits. Unfortunately,
7175 earlier compilers provided no way of distinguishing ILP32 objects
7176 from LP64 objects, so if there's any doubt, we should assume that
7177 the official LP64 form is being used. */
7178 if ((filedata->file_header.e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64
7179 && find_section (filedata, ".gcc_compiled_long32") == NULL)
7180 eh_addr_size = 8;
7181 break;
7182
7183 case EM_H8_300:
7184 case EM_H8_300H:
7185 switch (filedata->file_header.e_flags & EF_H8_MACH)
7186 {
7187 case E_H8_MACH_H8300:
7188 case E_H8_MACH_H8300HN:
7189 case E_H8_MACH_H8300SN:
7190 case E_H8_MACH_H8300SXN:
7191 eh_addr_size = 2;
7192 break;
7193 case E_H8_MACH_H8300H:
7194 case E_H8_MACH_H8300S:
7195 case E_H8_MACH_H8300SX:
7196 eh_addr_size = 4;
7197 break;
7198 }
7199 break;
7200
7201 case EM_M32C_OLD:
7202 case EM_M32C:
7203 switch (filedata->file_header.e_flags & EF_M32C_CPU_MASK)
7204 {
7205 case EF_M32C_CPU_M16C:
7206 eh_addr_size = 2;
7207 break;
7208 }
7209 break;
7210 }
7211
7212 #define CHECK_ENTSIZE_VALUES(section, i, size32, size64) \
7213 do \
7214 { \
7215 uint64_t expected_entsize = is_32bit_elf ? size32 : size64; \
7216 if (section->sh_entsize != expected_entsize) \
7217 { \
7218 error (_("Section %d has invalid sh_entsize of %" PRIx64 "\n"), \
7219 i, section->sh_entsize); \
7220 error (_("(Using the expected size of %" PRIx64 " for the rest of this dump)\n"), \
7221 expected_entsize); \
7222 section->sh_entsize = expected_entsize; \
7223 } \
7224 } \
7225 while (0)
7226
7227 #define CHECK_ENTSIZE(section, i, type) \
7228 CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type), \
7229 sizeof (Elf64_External_##type))
7230
7231 for (i = 0, section = filedata->section_headers;
7232 i < filedata->file_header.e_shnum;
7233 i++, section++)
7234 {
7235 const char *name = section_name_print (filedata, section);
7236
7237 /* Run some sanity checks on the headers and
7238 possibly fill in some file data as well. */
7239 switch (section->sh_type)
7240 {
7241 case SHT_DYNSYM:
7242 if (filedata->dynamic_symbols != NULL)
7243 {
7244 error (_("File contains multiple dynamic symbol tables\n"));
7245 continue;
7246 }
7247
7248 CHECK_ENTSIZE (section, i, Sym);
7249 filedata->dynamic_symbols
7250 = get_elf_symbols (filedata, section, &filedata->num_dynamic_syms);
7251 filedata->dynamic_symtab_section = section;
7252 break;
7253
7254 case SHT_STRTAB:
7255 if (streq (name, ".dynstr"))
7256 {
7257 if (filedata->dynamic_strings != NULL)
7258 {
7259 error (_("File contains multiple dynamic string tables\n"));
7260 continue;
7261 }
7262
7263 filedata->dynamic_strings
7264 = (char *) get_data (NULL, filedata, section->sh_offset,
7265 1, section->sh_size, _("dynamic strings"));
7266 filedata->dynamic_strings_length
7267 = filedata->dynamic_strings == NULL ? 0 : section->sh_size;
7268 filedata->dynamic_strtab_section = section;
7269 }
7270 break;
7271
7272 case SHT_SYMTAB_SHNDX:
7273 {
7274 elf_section_list * entry = xmalloc (sizeof * entry);
7275
7276 entry->hdr = section;
7277 entry->next = filedata->symtab_shndx_list;
7278 filedata->symtab_shndx_list = entry;
7279 }
7280 break;
7281
7282 case SHT_SYMTAB:
7283 CHECK_ENTSIZE (section, i, Sym);
7284 break;
7285
7286 case SHT_GROUP:
7287 CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
7288 break;
7289
7290 case SHT_REL:
7291 CHECK_ENTSIZE (section, i, Rel);
7292 if (do_checks && section->sh_size == 0)
7293 warn (_("Section '%s': zero-sized relocation section\n"), name);
7294 break;
7295
7296 case SHT_RELA:
7297 CHECK_ENTSIZE (section, i, Rela);
7298 if (do_checks && section->sh_size == 0)
7299 warn (_("Section '%s': zero-sized relocation section\n"), name);
7300 break;
7301
7302 case SHT_RELR:
7303 CHECK_ENTSIZE (section, i, Relr);
7304 break;
7305
7306 case SHT_NOTE:
7307 case SHT_PROGBITS:
7308 /* Having a zero sized section is not illegal according to the
7309 ELF standard, but it might be an indication that something
7310 is wrong. So issue a warning if we are running in lint mode. */
7311 if (do_checks && section->sh_size == 0)
7312 warn (_("Section '%s': has a size of zero - is this intended ?\n"), name);
7313 break;
7314
7315 default:
7316 break;
7317 }
7318
7319 if ((do_debugging || do_debug_info || do_debug_abbrevs
7320 || do_debug_lines || do_debug_pubnames || do_debug_pubtypes
7321 || do_debug_aranges || do_debug_frames || do_debug_macinfo
7322 || do_debug_str || do_debug_str_offsets || do_debug_loc
7323 || do_debug_ranges
7324 || do_debug_addr || do_debug_cu_index || do_debug_links)
7325 && (startswith (name, ".debug_")
7326 || startswith (name, ".zdebug_")))
7327 {
7328 if (name[1] == 'z')
7329 name += sizeof (".zdebug_") - 1;
7330 else
7331 name += sizeof (".debug_") - 1;
7332
7333 if (do_debugging
7334 || (do_debug_info && startswith (name, "info"))
7335 || (do_debug_info && startswith (name, "types"))
7336 || (do_debug_abbrevs && startswith (name, "abbrev"))
7337 || (do_debug_lines && strcmp (name, "line") == 0)
7338 || (do_debug_lines && startswith (name, "line."))
7339 || (do_debug_pubnames && startswith (name, "pubnames"))
7340 || (do_debug_pubtypes && startswith (name, "pubtypes"))
7341 || (do_debug_pubnames && startswith (name, "gnu_pubnames"))
7342 || (do_debug_pubtypes && startswith (name, "gnu_pubtypes"))
7343 || (do_debug_aranges && startswith (name, "aranges"))
7344 || (do_debug_ranges && startswith (name, "ranges"))
7345 || (do_debug_ranges && startswith (name, "rnglists"))
7346 || (do_debug_frames && startswith (name, "frame"))
7347 || (do_debug_macinfo && startswith (name, "macinfo"))
7348 || (do_debug_macinfo && startswith (name, "macro"))
7349 || (do_debug_str && startswith (name, "str"))
7350 || (do_debug_links && startswith (name, "sup"))
7351 || (do_debug_str_offsets && startswith (name, "str_offsets"))
7352 || (do_debug_loc && startswith (name, "loc"))
7353 || (do_debug_loc && startswith (name, "loclists"))
7354 || (do_debug_addr && startswith (name, "addr"))
7355 || (do_debug_cu_index && startswith (name, "cu_index"))
7356 || (do_debug_cu_index && startswith (name, "tu_index"))
7357 )
7358 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7359 }
7360 /* Linkonce section to be combined with .debug_info at link time. */
7361 else if ((do_debugging || do_debug_info)
7362 && startswith (name, ".gnu.linkonce.wi."))
7363 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7364 else if (do_debug_frames && streq (name, ".eh_frame"))
7365 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7366 else if (do_gdb_index && (streq (name, ".gdb_index")
7367 || streq (name, ".debug_names")))
7368 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7369 /* Trace sections for Itanium VMS. */
7370 else if ((do_debugging || do_trace_info || do_trace_abbrevs
7371 || do_trace_aranges)
7372 && startswith (name, ".trace_"))
7373 {
7374 name += sizeof (".trace_") - 1;
7375
7376 if (do_debugging
7377 || (do_trace_info && streq (name, "info"))
7378 || (do_trace_abbrevs && streq (name, "abbrev"))
7379 || (do_trace_aranges && streq (name, "aranges"))
7380 )
7381 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7382 }
7383 else if ((do_debugging || do_debug_links)
7384 && (startswith (name, ".gnu_debuglink")
7385 || startswith (name, ".gnu_debugaltlink")))
7386 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7387 }
7388
7389 if (! do_sections)
7390 return true;
7391
7392 if (filedata->is_separate && ! process_links)
7393 return true;
7394
7395 if (filedata->is_separate)
7396 printf (_("\nSection Headers in linked file '%s':\n"), filedata->file_name);
7397 else if (filedata->file_header.e_shnum > 1)
7398 printf (_("\nSection Headers:\n"));
7399 else
7400 printf (_("\nSection Header:\n"));
7401
7402 if (is_32bit_elf)
7403 {
7404 if (do_section_details)
7405 {
7406 printf (_(" [Nr] Name\n"));
7407 printf (_(" Type Addr Off Size ES Lk Inf Al\n"));
7408 }
7409 else
7410 printf
7411 (_(" [Nr] Name Type Addr Off Size ES Flg Lk Inf Al\n"));
7412 }
7413 else if (do_wide)
7414 {
7415 if (do_section_details)
7416 {
7417 printf (_(" [Nr] Name\n"));
7418 printf (_(" Type Address Off Size ES Lk Inf Al\n"));
7419 }
7420 else
7421 printf
7422 (_(" [Nr] Name Type Address Off Size ES Flg Lk Inf Al\n"));
7423 }
7424 else
7425 {
7426 if (do_section_details)
7427 {
7428 printf (_(" [Nr] Name\n"));
7429 printf (_(" Type Address Offset Link\n"));
7430 printf (_(" Size EntSize Info Align\n"));
7431 }
7432 else
7433 {
7434 printf (_(" [Nr] Name Type Address Offset\n"));
7435 printf (_(" Size EntSize Flags Link Info Align\n"));
7436 }
7437 }
7438
7439 if (do_section_details)
7440 printf (_(" Flags\n"));
7441
7442 for (i = 0, section = filedata->section_headers;
7443 i < filedata->file_header.e_shnum;
7444 i++, section++)
7445 {
7446 /* Run some sanity checks on the section header. */
7447
7448 /* Check the sh_link field. */
7449 switch (section->sh_type)
7450 {
7451 case SHT_REL:
7452 case SHT_RELA:
7453 if (section->sh_link == 0
7454 && (filedata->file_header.e_type == ET_EXEC
7455 || filedata->file_header.e_type == ET_DYN))
7456 /* A dynamic relocation section where all entries use a
7457 zero symbol index need not specify a symtab section. */
7458 break;
7459 /* Fall through. */
7460 case SHT_SYMTAB_SHNDX:
7461 case SHT_GROUP:
7462 case SHT_HASH:
7463 case SHT_GNU_HASH:
7464 case SHT_GNU_versym:
7465 if (section->sh_link == 0
7466 || section->sh_link >= filedata->file_header.e_shnum
7467 || (filedata->section_headers[section->sh_link].sh_type != SHT_SYMTAB
7468 && filedata->section_headers[section->sh_link].sh_type != SHT_DYNSYM))
7469 warn (_("[%2u]: Link field (%u) should index a symtab section.\n"),
7470 i, section->sh_link);
7471 break;
7472
7473 case SHT_DYNAMIC:
7474 case SHT_SYMTAB:
7475 case SHT_DYNSYM:
7476 case SHT_GNU_verneed:
7477 case SHT_GNU_verdef:
7478 case SHT_GNU_LIBLIST:
7479 if (section->sh_link == 0
7480 || section->sh_link >= filedata->file_header.e_shnum
7481 || filedata->section_headers[section->sh_link].sh_type != SHT_STRTAB)
7482 warn (_("[%2u]: Link field (%u) should index a string section.\n"),
7483 i, section->sh_link);
7484 break;
7485
7486 case SHT_INIT_ARRAY:
7487 case SHT_FINI_ARRAY:
7488 case SHT_PREINIT_ARRAY:
7489 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
7490 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
7491 i, section->sh_link);
7492 break;
7493
7494 default:
7495 /* FIXME: Add support for target specific section types. */
7496 #if 0 /* Currently we do not check other section types as there are too
7497 many special cases. Stab sections for example have a type
7498 of SHT_PROGBITS but an sh_link field that links to the .stabstr
7499 section. */
7500 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
7501 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
7502 i, section->sh_link);
7503 #endif
7504 break;
7505 }
7506
7507 /* Check the sh_info field. */
7508 switch (section->sh_type)
7509 {
7510 case SHT_REL:
7511 case SHT_RELA:
7512 if (section->sh_info == 0
7513 && (filedata->file_header.e_type == ET_EXEC
7514 || filedata->file_header.e_type == ET_DYN))
7515 /* Dynamic relocations apply to segments, so they do not
7516 need to specify the section they relocate. */
7517 break;
7518 if (section->sh_info == 0
7519 || section->sh_info >= filedata->file_header.e_shnum
7520 || (filedata->section_headers[section->sh_info].sh_type != SHT_PROGBITS
7521 && filedata->section_headers[section->sh_info].sh_type != SHT_NOBITS
7522 && filedata->section_headers[section->sh_info].sh_type != SHT_NOTE
7523 && filedata->section_headers[section->sh_info].sh_type != SHT_INIT_ARRAY
7524 && filedata->section_headers[section->sh_info].sh_type != SHT_FINI_ARRAY
7525 && filedata->section_headers[section->sh_info].sh_type != SHT_PREINIT_ARRAY
7526 /* FIXME: Are other section types valid ? */
7527 && filedata->section_headers[section->sh_info].sh_type < SHT_LOOS))
7528 warn (_("[%2u]: Info field (%u) should index a relocatable section.\n"),
7529 i, section->sh_info);
7530 break;
7531
7532 case SHT_DYNAMIC:
7533 case SHT_HASH:
7534 case SHT_SYMTAB_SHNDX:
7535 case SHT_INIT_ARRAY:
7536 case SHT_FINI_ARRAY:
7537 case SHT_PREINIT_ARRAY:
7538 if (section->sh_info != 0)
7539 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
7540 i, section->sh_info);
7541 break;
7542
7543 case SHT_GROUP:
7544 case SHT_SYMTAB:
7545 case SHT_DYNSYM:
7546 /* A symbol index - we assume that it is valid. */
7547 break;
7548
7549 default:
7550 /* FIXME: Add support for target specific section types. */
7551 if (section->sh_type == SHT_NOBITS)
7552 /* NOBITS section headers with non-zero sh_info fields can be
7553 created when a binary is stripped of everything but its debug
7554 information. The stripped sections have their headers
7555 preserved but their types set to SHT_NOBITS. So do not check
7556 this type of section. */
7557 ;
7558 else if (section->sh_flags & SHF_INFO_LINK)
7559 {
7560 if (section->sh_info < 1 || section->sh_info >= filedata->file_header.e_shnum)
7561 warn (_("[%2u]: Expected link to another section in info field"), i);
7562 }
7563 else if (section->sh_type < SHT_LOOS
7564 && (section->sh_flags & SHF_GNU_MBIND) == 0
7565 && section->sh_info != 0)
7566 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
7567 i, section->sh_info);
7568 break;
7569 }
7570
7571 /* Check the sh_size field. */
7572 if (section->sh_size > filedata->file_size
7573 && section->sh_type != SHT_NOBITS
7574 && section->sh_type != SHT_NULL
7575 && section->sh_type < SHT_LOOS)
7576 warn (_("Size of section %u is larger than the entire file!\n"), i);
7577
7578 printf (" [%2u] ", i);
7579 if (do_section_details)
7580 printf ("%s\n ", printable_section_name (filedata, section));
7581 else
7582 print_symbol (-17, section_name_print (filedata, section));
7583
7584 printf (do_wide ? " %-15s " : " %-15.15s ",
7585 get_section_type_name (filedata, section->sh_type));
7586
7587 if (is_32bit_elf)
7588 {
7589 const char * link_too_big = NULL;
7590
7591 print_vma (section->sh_addr, LONG_HEX);
7592
7593 printf ( " %6.6lx %6.6lx %2.2lx",
7594 (unsigned long) section->sh_offset,
7595 (unsigned long) section->sh_size,
7596 (unsigned long) section->sh_entsize);
7597
7598 if (do_section_details)
7599 fputs (" ", stdout);
7600 else
7601 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7602
7603 if (section->sh_link >= filedata->file_header.e_shnum)
7604 {
7605 link_too_big = "";
7606 /* The sh_link value is out of range. Normally this indicates
7607 an error but it can have special values in Solaris binaries. */
7608 switch (filedata->file_header.e_machine)
7609 {
7610 case EM_386:
7611 case EM_IAMCU:
7612 case EM_X86_64:
7613 case EM_L1OM:
7614 case EM_K1OM:
7615 case EM_OLD_SPARCV9:
7616 case EM_SPARC32PLUS:
7617 case EM_SPARCV9:
7618 case EM_SPARC:
7619 if (section->sh_link == (SHN_BEFORE & 0xffff))
7620 link_too_big = "BEFORE";
7621 else if (section->sh_link == (SHN_AFTER & 0xffff))
7622 link_too_big = "AFTER";
7623 break;
7624 default:
7625 break;
7626 }
7627 }
7628
7629 if (do_section_details)
7630 {
7631 if (link_too_big != NULL && * link_too_big)
7632 printf ("<%s> ", link_too_big);
7633 else
7634 printf ("%2u ", section->sh_link);
7635 printf ("%3u %2lu\n", section->sh_info,
7636 (unsigned long) section->sh_addralign);
7637 }
7638 else
7639 printf ("%2u %3u %2lu\n",
7640 section->sh_link,
7641 section->sh_info,
7642 (unsigned long) section->sh_addralign);
7643
7644 if (link_too_big && ! * link_too_big)
7645 warn (_("section %u: sh_link value of %u is larger than the number of sections\n"),
7646 i, section->sh_link);
7647 }
7648 else if (do_wide)
7649 {
7650 print_vma (section->sh_addr, LONG_HEX);
7651
7652 if ((long) section->sh_offset == section->sh_offset)
7653 printf (" %6.6lx", (unsigned long) section->sh_offset);
7654 else
7655 {
7656 putchar (' ');
7657 print_vma (section->sh_offset, LONG_HEX);
7658 }
7659
7660 if ((unsigned long) section->sh_size == section->sh_size)
7661 printf (" %6.6lx", (unsigned long) section->sh_size);
7662 else
7663 {
7664 putchar (' ');
7665 print_vma (section->sh_size, LONG_HEX);
7666 }
7667
7668 if ((unsigned long) section->sh_entsize == section->sh_entsize)
7669 printf (" %2.2lx", (unsigned long) section->sh_entsize);
7670 else
7671 {
7672 putchar (' ');
7673 print_vma (section->sh_entsize, LONG_HEX);
7674 }
7675
7676 if (do_section_details)
7677 fputs (" ", stdout);
7678 else
7679 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7680
7681 printf ("%2u %3u ", section->sh_link, section->sh_info);
7682
7683 if ((unsigned long) section->sh_addralign == section->sh_addralign)
7684 printf ("%2lu\n", (unsigned long) section->sh_addralign);
7685 else
7686 {
7687 print_vma (section->sh_addralign, DEC);
7688 putchar ('\n');
7689 }
7690 }
7691 else if (do_section_details)
7692 {
7693 putchar (' ');
7694 print_vma (section->sh_addr, LONG_HEX);
7695 if ((long) section->sh_offset == section->sh_offset)
7696 printf (" %16.16lx", (unsigned long) section->sh_offset);
7697 else
7698 {
7699 printf (" ");
7700 print_vma (section->sh_offset, LONG_HEX);
7701 }
7702 printf (" %u\n ", section->sh_link);
7703 print_vma (section->sh_size, LONG_HEX);
7704 putchar (' ');
7705 print_vma (section->sh_entsize, LONG_HEX);
7706
7707 printf (" %-16u %lu\n",
7708 section->sh_info,
7709 (unsigned long) section->sh_addralign);
7710 }
7711 else
7712 {
7713 putchar (' ');
7714 print_vma (section->sh_addr, LONG_HEX);
7715 if ((long) section->sh_offset == section->sh_offset)
7716 printf (" %8.8lx", (unsigned long) section->sh_offset);
7717 else
7718 {
7719 printf (" ");
7720 print_vma (section->sh_offset, LONG_HEX);
7721 }
7722 printf ("\n ");
7723 print_vma (section->sh_size, LONG_HEX);
7724 printf (" ");
7725 print_vma (section->sh_entsize, LONG_HEX);
7726
7727 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7728
7729 printf (" %2u %3u %lu\n",
7730 section->sh_link,
7731 section->sh_info,
7732 (unsigned long) section->sh_addralign);
7733 }
7734
7735 if (do_section_details)
7736 {
7737 printf (" %s\n", get_elf_section_flags (filedata, section->sh_flags));
7738 if ((section->sh_flags & SHF_COMPRESSED) != 0)
7739 {
7740 /* Minimum section size is 12 bytes for 32-bit compression
7741 header + 12 bytes for compressed data header. */
7742 unsigned char buf[24];
7743
7744 assert (sizeof (buf) >= sizeof (Elf64_External_Chdr));
7745 if (get_data (&buf, filedata, section->sh_offset, 1,
7746 sizeof (buf), _("compression header")))
7747 {
7748 Elf_Internal_Chdr chdr;
7749
7750 if (get_compression_header (&chdr, buf, sizeof (buf)) == 0)
7751 printf (_(" [<corrupt>]\n"));
7752 else
7753 {
7754 if (chdr.ch_type == ELFCOMPRESS_ZLIB)
7755 printf (" ZLIB, ");
7756 else if (chdr.ch_type == ELFCOMPRESS_ZSTD)
7757 printf (" ZSTD, ");
7758 else
7759 printf (_(" [<unknown>: 0x%x], "),
7760 chdr.ch_type);
7761 print_vma (chdr.ch_size, LONG_HEX);
7762 printf (", %lu\n", (unsigned long) chdr.ch_addralign);
7763 }
7764 }
7765 }
7766 }
7767 }
7768
7769 if (!do_section_details)
7770 {
7771 /* The ordering of the letters shown here matches the ordering of the
7772 corresponding SHF_xxx values, and hence the order in which these
7773 letters will be displayed to the user. */
7774 printf (_("Key to Flags:\n\
7775 W (write), A (alloc), X (execute), M (merge), S (strings), I (info),\n\
7776 L (link order), O (extra OS processing required), G (group), T (TLS),\n\
7777 C (compressed), x (unknown), o (OS specific), E (exclude),\n "));
7778 switch (filedata->file_header.e_ident[EI_OSABI])
7779 {
7780 case ELFOSABI_GNU:
7781 case ELFOSABI_FREEBSD:
7782 printf (_("R (retain), "));
7783 /* Fall through */
7784 case ELFOSABI_NONE:
7785 printf (_("D (mbind), "));
7786 break;
7787 default:
7788 break;
7789 }
7790 if (filedata->file_header.e_machine == EM_X86_64
7791 || filedata->file_header.e_machine == EM_L1OM
7792 || filedata->file_header.e_machine == EM_K1OM)
7793 printf (_("l (large), "));
7794 else if (filedata->file_header.e_machine == EM_ARM)
7795 printf (_("y (purecode), "));
7796 else if (filedata->file_header.e_machine == EM_PPC)
7797 printf (_("v (VLE), "));
7798 printf ("p (processor specific)\n");
7799 }
7800
7801 return true;
7802 }
7803
7804 static bool
7805 get_symtab (Filedata *filedata, Elf_Internal_Shdr *symsec,
7806 Elf_Internal_Sym **symtab, unsigned long *nsyms,
7807 char **strtab, unsigned long *strtablen)
7808 {
7809 *strtab = NULL;
7810 *strtablen = 0;
7811 *symtab = get_elf_symbols (filedata, symsec, nsyms);
7812
7813 if (*symtab == NULL)
7814 return false;
7815
7816 if (symsec->sh_link != 0)
7817 {
7818 Elf_Internal_Shdr *strsec;
7819
7820 if (symsec->sh_link >= filedata->file_header.e_shnum)
7821 {
7822 error (_("Bad sh_link in symbol table section\n"));
7823 free (*symtab);
7824 *symtab = NULL;
7825 *nsyms = 0;
7826 return false;
7827 }
7828
7829 strsec = filedata->section_headers + symsec->sh_link;
7830
7831 *strtab = (char *) get_data (NULL, filedata, strsec->sh_offset,
7832 1, strsec->sh_size, _("string table"));
7833 if (*strtab == NULL)
7834 {
7835 free (*symtab);
7836 *symtab = NULL;
7837 *nsyms = 0;
7838 return false;
7839 }
7840 *strtablen = strsec->sh_size;
7841 }
7842 return true;
7843 }
7844
7845 static const char *
7846 get_group_flags (unsigned int flags)
7847 {
7848 static char buff[128];
7849
7850 if (flags == 0)
7851 return "";
7852 else if (flags == GRP_COMDAT)
7853 return "COMDAT ";
7854
7855 snprintf (buff, sizeof buff, "[0x%x: %s%s%s]",
7856 flags,
7857 flags & GRP_MASKOS ? _("<OS specific>") : "",
7858 flags & GRP_MASKPROC ? _("<PROC specific>") : "",
7859 (flags & ~(GRP_COMDAT | GRP_MASKOS | GRP_MASKPROC)
7860 ? _("<unknown>") : ""));
7861
7862 return buff;
7863 }
7864
7865 static bool
7866 process_section_groups (Filedata * filedata)
7867 {
7868 Elf_Internal_Shdr * section;
7869 unsigned int i;
7870 struct group * group;
7871 Elf_Internal_Shdr * symtab_sec;
7872 Elf_Internal_Shdr * strtab_sec;
7873 Elf_Internal_Sym * symtab;
7874 unsigned long num_syms;
7875 char * strtab;
7876 size_t strtab_size;
7877
7878 /* Don't process section groups unless needed. */
7879 if (!do_unwind && !do_section_groups)
7880 return true;
7881
7882 if (filedata->file_header.e_shnum == 0)
7883 {
7884 if (do_section_groups)
7885 {
7886 if (filedata->is_separate)
7887 printf (_("\nThere are no sections group in linked file '%s'.\n"),
7888 filedata->file_name);
7889 else
7890 printf (_("\nThere are no section groups in this file.\n"));
7891 }
7892 return true;
7893 }
7894
7895 if (filedata->section_headers == NULL)
7896 {
7897 error (_("Section headers are not available!\n"));
7898 /* PR 13622: This can happen with a corrupt ELF header. */
7899 return false;
7900 }
7901
7902 filedata->section_headers_groups
7903 = (struct group **) calloc (filedata->file_header.e_shnum,
7904 sizeof (struct group *));
7905
7906 if (filedata->section_headers_groups == NULL)
7907 {
7908 error (_("Out of memory reading %u section group headers\n"),
7909 filedata->file_header.e_shnum);
7910 return false;
7911 }
7912
7913 /* Scan the sections for the group section. */
7914 filedata->group_count = 0;
7915 for (i = 0, section = filedata->section_headers;
7916 i < filedata->file_header.e_shnum;
7917 i++, section++)
7918 if (section->sh_type == SHT_GROUP)
7919 filedata->group_count++;
7920
7921 if (filedata->group_count == 0)
7922 {
7923 if (do_section_groups)
7924 {
7925 if (filedata->is_separate)
7926 printf (_("\nThere are no section groups in linked file '%s'.\n"),
7927 filedata->file_name);
7928 else
7929 printf (_("\nThere are no section groups in this file.\n"));
7930 }
7931
7932 return true;
7933 }
7934
7935 filedata->section_groups = (struct group *) calloc (filedata->group_count,
7936 sizeof (struct group));
7937
7938 if (filedata->section_groups == NULL)
7939 {
7940 error (_("Out of memory reading %lu groups\n"),
7941 (unsigned long) filedata->group_count);
7942 return false;
7943 }
7944
7945 symtab_sec = NULL;
7946 strtab_sec = NULL;
7947 symtab = NULL;
7948 num_syms = 0;
7949 strtab = NULL;
7950 strtab_size = 0;
7951
7952 if (filedata->is_separate)
7953 printf (_("Section groups in linked file '%s'\n"), filedata->file_name);
7954
7955 for (i = 0, section = filedata->section_headers, group = filedata->section_groups;
7956 i < filedata->file_header.e_shnum;
7957 i++, section++)
7958 {
7959 if (section->sh_type == SHT_GROUP)
7960 {
7961 const char * name = printable_section_name (filedata, section);
7962 const char * group_name;
7963 unsigned char * start;
7964 unsigned char * indices;
7965 unsigned int entry, j, size;
7966 Elf_Internal_Shdr * sec;
7967 Elf_Internal_Sym * sym;
7968
7969 /* Get the symbol table. */
7970 if (section->sh_link >= filedata->file_header.e_shnum
7971 || ((sec = filedata->section_headers + section->sh_link)->sh_type
7972 != SHT_SYMTAB))
7973 {
7974 error (_("Bad sh_link in group section `%s'\n"), name);
7975 continue;
7976 }
7977
7978 if (symtab_sec != sec)
7979 {
7980 symtab_sec = sec;
7981 free (symtab);
7982 symtab = get_elf_symbols (filedata, symtab_sec, & num_syms);
7983 }
7984
7985 if (symtab == NULL)
7986 {
7987 error (_("Corrupt header in group section `%s'\n"), name);
7988 continue;
7989 }
7990
7991 if (section->sh_info >= num_syms)
7992 {
7993 error (_("Bad sh_info in group section `%s'\n"), name);
7994 continue;
7995 }
7996
7997 sym = symtab + section->sh_info;
7998
7999 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8000 {
8001 if (sym->st_shndx == 0
8002 || sym->st_shndx >= filedata->file_header.e_shnum)
8003 {
8004 error (_("Bad sh_info in group section `%s'\n"), name);
8005 continue;
8006 }
8007
8008 group_name = section_name_print (filedata,
8009 filedata->section_headers
8010 + sym->st_shndx);
8011 strtab_sec = NULL;
8012 free (strtab);
8013 strtab = NULL;
8014 strtab_size = 0;
8015 }
8016 else
8017 {
8018 /* Get the string table. */
8019 if (symtab_sec->sh_link >= filedata->file_header.e_shnum)
8020 {
8021 strtab_sec = NULL;
8022 free (strtab);
8023 strtab = NULL;
8024 strtab_size = 0;
8025 }
8026 else if (strtab_sec
8027 != (sec = filedata->section_headers + symtab_sec->sh_link))
8028 {
8029 strtab_sec = sec;
8030 free (strtab);
8031
8032 strtab = (char *) get_data (NULL, filedata, strtab_sec->sh_offset,
8033 1, strtab_sec->sh_size,
8034 _("string table"));
8035 strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
8036 }
8037 group_name = sym->st_name < strtab_size
8038 ? strtab + sym->st_name : _("<corrupt>");
8039 }
8040
8041 /* PR 17531: file: loop. */
8042 if (section->sh_entsize > section->sh_size)
8043 {
8044 error (_("Section %s has sh_entsize (0x%lx) which is larger than its size (0x%lx)\n"),
8045 printable_section_name (filedata, section),
8046 (unsigned long) section->sh_entsize,
8047 (unsigned long) section->sh_size);
8048 continue;
8049 }
8050
8051 start = (unsigned char *) get_data (NULL, filedata, section->sh_offset,
8052 1, section->sh_size,
8053 _("section data"));
8054 if (start == NULL)
8055 continue;
8056
8057 indices = start;
8058 size = (section->sh_size / section->sh_entsize) - 1;
8059 entry = byte_get (indices, 4);
8060 indices += 4;
8061
8062 if (do_section_groups)
8063 {
8064 printf (_("\n%sgroup section [%5u] `%s' [%s] contains %u sections:\n"),
8065 get_group_flags (entry), i, name, group_name, size);
8066
8067 printf (_(" [Index] Name\n"));
8068 }
8069
8070 group->group_index = i;
8071
8072 for (j = 0; j < size; j++)
8073 {
8074 struct group_list * g;
8075
8076 entry = byte_get (indices, 4);
8077 indices += 4;
8078
8079 if (entry >= filedata->file_header.e_shnum)
8080 {
8081 static unsigned num_group_errors = 0;
8082
8083 if (num_group_errors ++ < 10)
8084 {
8085 error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
8086 entry, i, filedata->file_header.e_shnum - 1);
8087 if (num_group_errors == 10)
8088 warn (_("Further error messages about overlarge group section indices suppressed\n"));
8089 }
8090 continue;
8091 }
8092
8093 if (filedata->section_headers_groups [entry] != NULL)
8094 {
8095 if (entry)
8096 {
8097 static unsigned num_errs = 0;
8098
8099 if (num_errs ++ < 10)
8100 {
8101 error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
8102 entry, i,
8103 filedata->section_headers_groups [entry]->group_index);
8104 if (num_errs == 10)
8105 warn (_("Further error messages about already contained group sections suppressed\n"));
8106 }
8107 continue;
8108 }
8109 else
8110 {
8111 /* Intel C/C++ compiler may put section 0 in a
8112 section group. We just warn it the first time
8113 and ignore it afterwards. */
8114 static bool warned = false;
8115 if (!warned)
8116 {
8117 error (_("section 0 in group section [%5u]\n"),
8118 filedata->section_headers_groups [entry]->group_index);
8119 warned = true;
8120 }
8121 }
8122 }
8123
8124 filedata->section_headers_groups [entry] = group;
8125
8126 if (do_section_groups)
8127 {
8128 sec = filedata->section_headers + entry;
8129 printf (" [%5u] %s\n", entry, printable_section_name (filedata, sec));
8130 }
8131
8132 g = (struct group_list *) xmalloc (sizeof (struct group_list));
8133 g->section_index = entry;
8134 g->next = group->root;
8135 group->root = g;
8136 }
8137
8138 free (start);
8139
8140 group++;
8141 }
8142 }
8143
8144 free (symtab);
8145 free (strtab);
8146 return true;
8147 }
8148
8149 /* Data used to display dynamic fixups. */
8150
8151 struct ia64_vms_dynfixup
8152 {
8153 uint64_t needed_ident; /* Library ident number. */
8154 uint64_t needed; /* Index in the dstrtab of the library name. */
8155 uint64_t fixup_needed; /* Index of the library. */
8156 uint64_t fixup_rela_cnt; /* Number of fixups. */
8157 uint64_t fixup_rela_off; /* Fixups offset in the dynamic segment. */
8158 };
8159
8160 /* Data used to display dynamic relocations. */
8161
8162 struct ia64_vms_dynimgrela
8163 {
8164 uint64_t img_rela_cnt; /* Number of relocations. */
8165 uint64_t img_rela_off; /* Reloc offset in the dynamic segment. */
8166 };
8167
8168 /* Display IA-64 OpenVMS dynamic fixups (used to dynamically link a shared
8169 library). */
8170
8171 static bool
8172 dump_ia64_vms_dynamic_fixups (Filedata * filedata,
8173 struct ia64_vms_dynfixup * fixup,
8174 const char * strtab,
8175 unsigned int strtab_sz)
8176 {
8177 Elf64_External_VMS_IMAGE_FIXUP * imfs;
8178 long i;
8179 const char * lib_name;
8180
8181 imfs = get_data (NULL, filedata,
8182 filedata->dynamic_addr + fixup->fixup_rela_off,
8183 sizeof (*imfs), fixup->fixup_rela_cnt,
8184 _("dynamic section image fixups"));
8185 if (!imfs)
8186 return false;
8187
8188 if (fixup->needed < strtab_sz)
8189 lib_name = strtab + fixup->needed;
8190 else
8191 {
8192 warn (_("corrupt library name index of 0x%lx found in dynamic entry"),
8193 (unsigned long) fixup->needed);
8194 lib_name = "???";
8195 }
8196
8197 printf (_("\nImage fixups for needed library #%d: %s - ident: %lx\n"),
8198 (int) fixup->fixup_needed, lib_name, (long) fixup->needed_ident);
8199 printf
8200 (_("Seg Offset Type SymVec DataType\n"));
8201
8202 for (i = 0; i < (long) fixup->fixup_rela_cnt; i++)
8203 {
8204 unsigned int type;
8205 const char *rtype;
8206
8207 printf ("%3u ", (unsigned) BYTE_GET (imfs [i].fixup_seg));
8208 printf ("%016" PRIx64 " ", BYTE_GET (imfs [i].fixup_offset));
8209 type = BYTE_GET (imfs [i].type);
8210 rtype = elf_ia64_reloc_type (type);
8211 if (rtype == NULL)
8212 printf ("0x%08x ", type);
8213 else
8214 printf ("%-32s ", rtype);
8215 printf ("%6u ", (unsigned) BYTE_GET (imfs [i].symvec_index));
8216 printf ("0x%08x\n", (unsigned) BYTE_GET (imfs [i].data_type));
8217 }
8218
8219 free (imfs);
8220 return true;
8221 }
8222
8223 /* Display IA-64 OpenVMS dynamic relocations (used to relocate an image). */
8224
8225 static bool
8226 dump_ia64_vms_dynamic_relocs (Filedata * filedata, struct ia64_vms_dynimgrela *imgrela)
8227 {
8228 Elf64_External_VMS_IMAGE_RELA *imrs;
8229 long i;
8230
8231 imrs = get_data (NULL, filedata,
8232 filedata->dynamic_addr + imgrela->img_rela_off,
8233 sizeof (*imrs), imgrela->img_rela_cnt,
8234 _("dynamic section image relocations"));
8235 if (!imrs)
8236 return false;
8237
8238 printf (_("\nImage relocs\n"));
8239 printf
8240 (_("Seg Offset Type Addend Seg Sym Off\n"));
8241
8242 for (i = 0; i < (long) imgrela->img_rela_cnt; i++)
8243 {
8244 unsigned int type;
8245 const char *rtype;
8246
8247 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].rela_seg));
8248 printf ("%08" PRIx64 " ", BYTE_GET (imrs [i].rela_offset));
8249 type = BYTE_GET (imrs [i].type);
8250 rtype = elf_ia64_reloc_type (type);
8251 if (rtype == NULL)
8252 printf ("0x%08x ", type);
8253 else
8254 printf ("%-31s ", rtype);
8255 print_vma (BYTE_GET (imrs [i].addend), FULL_HEX);
8256 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].sym_seg));
8257 printf ("%08" PRIx64 "\n", BYTE_GET (imrs [i].sym_offset));
8258 }
8259
8260 free (imrs);
8261 return true;
8262 }
8263
8264 /* Display IA-64 OpenVMS dynamic relocations and fixups. */
8265
8266 static bool
8267 process_ia64_vms_dynamic_relocs (Filedata * filedata)
8268 {
8269 struct ia64_vms_dynfixup fixup;
8270 struct ia64_vms_dynimgrela imgrela;
8271 Elf_Internal_Dyn *entry;
8272 uint64_t strtab_off = 0;
8273 uint64_t strtab_sz = 0;
8274 char *strtab = NULL;
8275 bool res = true;
8276
8277 memset (&fixup, 0, sizeof (fixup));
8278 memset (&imgrela, 0, sizeof (imgrela));
8279
8280 /* Note: the order of the entries is specified by the OpenVMS specs. */
8281 for (entry = filedata->dynamic_section;
8282 entry < filedata->dynamic_section + filedata->dynamic_nent;
8283 entry++)
8284 {
8285 switch (entry->d_tag)
8286 {
8287 case DT_IA_64_VMS_STRTAB_OFFSET:
8288 strtab_off = entry->d_un.d_val;
8289 break;
8290 case DT_STRSZ:
8291 strtab_sz = entry->d_un.d_val;
8292 if (strtab == NULL)
8293 strtab = get_data (NULL, filedata,
8294 filedata->dynamic_addr + strtab_off,
8295 1, strtab_sz, _("dynamic string section"));
8296 if (strtab == NULL)
8297 strtab_sz = 0;
8298 break;
8299
8300 case DT_IA_64_VMS_NEEDED_IDENT:
8301 fixup.needed_ident = entry->d_un.d_val;
8302 break;
8303 case DT_NEEDED:
8304 fixup.needed = entry->d_un.d_val;
8305 break;
8306 case DT_IA_64_VMS_FIXUP_NEEDED:
8307 fixup.fixup_needed = entry->d_un.d_val;
8308 break;
8309 case DT_IA_64_VMS_FIXUP_RELA_CNT:
8310 fixup.fixup_rela_cnt = entry->d_un.d_val;
8311 break;
8312 case DT_IA_64_VMS_FIXUP_RELA_OFF:
8313 fixup.fixup_rela_off = entry->d_un.d_val;
8314 if (! dump_ia64_vms_dynamic_fixups (filedata, &fixup, strtab, strtab_sz))
8315 res = false;
8316 break;
8317 case DT_IA_64_VMS_IMG_RELA_CNT:
8318 imgrela.img_rela_cnt = entry->d_un.d_val;
8319 break;
8320 case DT_IA_64_VMS_IMG_RELA_OFF:
8321 imgrela.img_rela_off = entry->d_un.d_val;
8322 if (! dump_ia64_vms_dynamic_relocs (filedata, &imgrela))
8323 res = false;
8324 break;
8325
8326 default:
8327 break;
8328 }
8329 }
8330
8331 free (strtab);
8332
8333 return res;
8334 }
8335
8336 static struct
8337 {
8338 const char * name;
8339 int reloc;
8340 int size;
8341 relocation_type rel_type;
8342 }
8343 dynamic_relocations [] =
8344 {
8345 { "REL", DT_REL, DT_RELSZ, reltype_rel },
8346 { "RELA", DT_RELA, DT_RELASZ, reltype_rela },
8347 { "RELR", DT_RELR, DT_RELRSZ, reltype_relr },
8348 { "PLT", DT_JMPREL, DT_PLTRELSZ, reltype_unknown }
8349 };
8350
8351 /* Process the reloc section. */
8352
8353 static bool
8354 process_relocs (Filedata * filedata)
8355 {
8356 unsigned long rel_size;
8357 unsigned long rel_offset;
8358
8359 if (!do_reloc)
8360 return true;
8361
8362 if (do_using_dynamic)
8363 {
8364 relocation_type rel_type;
8365 const char * name;
8366 bool has_dynamic_reloc;
8367 unsigned int i;
8368
8369 has_dynamic_reloc = false;
8370
8371 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
8372 {
8373 rel_type = dynamic_relocations [i].rel_type;
8374 name = dynamic_relocations [i].name;
8375 rel_size = filedata->dynamic_info[dynamic_relocations [i].size];
8376 rel_offset = filedata->dynamic_info[dynamic_relocations [i].reloc];
8377
8378 if (rel_size)
8379 has_dynamic_reloc = true;
8380
8381 if (rel_type == reltype_unknown)
8382 {
8383 if (dynamic_relocations [i].reloc == DT_JMPREL)
8384 switch (filedata->dynamic_info[DT_PLTREL])
8385 {
8386 case DT_REL:
8387 rel_type = reltype_rel;
8388 break;
8389 case DT_RELA:
8390 rel_type = reltype_rela;
8391 break;
8392 }
8393 }
8394
8395 if (rel_size)
8396 {
8397 if (filedata->is_separate)
8398 printf
8399 (_("\nIn linked file '%s' section '%s' at offset 0x%lx contains %ld bytes:\n"),
8400 filedata->file_name, name, rel_offset, rel_size);
8401 else
8402 printf
8403 (_("\n'%s' relocation section at offset 0x%lx contains %ld bytes:\n"),
8404 name, rel_offset, rel_size);
8405
8406 dump_relocations (filedata,
8407 offset_from_vma (filedata, rel_offset, rel_size),
8408 rel_size,
8409 filedata->dynamic_symbols,
8410 filedata->num_dynamic_syms,
8411 filedata->dynamic_strings,
8412 filedata->dynamic_strings_length,
8413 rel_type, true /* is_dynamic */);
8414 }
8415 }
8416
8417 if (is_ia64_vms (filedata))
8418 if (process_ia64_vms_dynamic_relocs (filedata))
8419 has_dynamic_reloc = true;
8420
8421 if (! has_dynamic_reloc)
8422 {
8423 if (filedata->is_separate)
8424 printf (_("\nThere are no dynamic relocations in linked file '%s'.\n"),
8425 filedata->file_name);
8426 else
8427 printf (_("\nThere are no dynamic relocations in this file.\n"));
8428 }
8429 }
8430 else
8431 {
8432 Elf_Internal_Shdr * section;
8433 unsigned long i;
8434 bool found = false;
8435
8436 for (i = 0, section = filedata->section_headers;
8437 i < filedata->file_header.e_shnum;
8438 i++, section++)
8439 {
8440 if ( section->sh_type != SHT_RELA
8441 && section->sh_type != SHT_REL
8442 && section->sh_type != SHT_RELR)
8443 continue;
8444
8445 rel_offset = section->sh_offset;
8446 rel_size = section->sh_size;
8447
8448 if (rel_size)
8449 {
8450 relocation_type rel_type;
8451 unsigned long num_rela;
8452
8453 if (filedata->is_separate)
8454 printf (_("\nIn linked file '%s' relocation section "),
8455 filedata->file_name);
8456 else
8457 printf (_("\nRelocation section "));
8458
8459 if (filedata->string_table == NULL)
8460 printf ("%d", section->sh_name);
8461 else
8462 printf ("'%s'", printable_section_name (filedata, section));
8463
8464 num_rela = rel_size / section->sh_entsize;
8465 printf (ngettext (" at offset 0x%lx contains %lu entry:\n",
8466 " at offset 0x%lx contains %lu entries:\n",
8467 num_rela),
8468 rel_offset, num_rela);
8469
8470 rel_type = section->sh_type == SHT_RELA ? reltype_rela :
8471 section->sh_type == SHT_REL ? reltype_rel : reltype_relr;
8472
8473 if (section->sh_link != 0
8474 && section->sh_link < filedata->file_header.e_shnum)
8475 {
8476 Elf_Internal_Shdr * symsec;
8477 Elf_Internal_Sym * symtab;
8478 unsigned long nsyms;
8479 unsigned long strtablen = 0;
8480 char * strtab = NULL;
8481
8482 symsec = filedata->section_headers + section->sh_link;
8483 if (symsec->sh_type != SHT_SYMTAB
8484 && symsec->sh_type != SHT_DYNSYM)
8485 continue;
8486
8487 if (!get_symtab (filedata, symsec,
8488 &symtab, &nsyms, &strtab, &strtablen))
8489 continue;
8490
8491 dump_relocations (filedata, rel_offset, rel_size,
8492 symtab, nsyms, strtab, strtablen,
8493 rel_type,
8494 symsec->sh_type == SHT_DYNSYM);
8495 free (strtab);
8496 free (symtab);
8497 }
8498 else
8499 dump_relocations (filedata, rel_offset, rel_size,
8500 NULL, 0, NULL, 0, rel_type, false /* is_dynamic */);
8501
8502 found = true;
8503 }
8504 }
8505
8506 if (! found)
8507 {
8508 /* Users sometimes forget the -D option, so try to be helpful. */
8509 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
8510 {
8511 if (filedata->dynamic_info[dynamic_relocations [i].size])
8512 {
8513 if (filedata->is_separate)
8514 printf (_("\nThere are no static relocations in linked file '%s'."),
8515 filedata->file_name);
8516 else
8517 printf (_("\nThere are no static relocations in this file."));
8518 printf (_("\nTo see the dynamic relocations add --use-dynamic to the command line.\n"));
8519
8520 break;
8521 }
8522 }
8523 if (i == ARRAY_SIZE (dynamic_relocations))
8524 {
8525 if (filedata->is_separate)
8526 printf (_("\nThere are no relocations in linked file '%s'.\n"),
8527 filedata->file_name);
8528 else
8529 printf (_("\nThere are no relocations in this file.\n"));
8530 }
8531 }
8532 }
8533
8534 return true;
8535 }
8536
8537 /* An absolute address consists of a section and an offset. If the
8538 section is NULL, the offset itself is the address, otherwise, the
8539 address equals to LOAD_ADDRESS(section) + offset. */
8540
8541 struct absaddr
8542 {
8543 unsigned short section;
8544 uint64_t offset;
8545 };
8546
8547 /* Find the nearest symbol at or below ADDR. Returns the symbol
8548 name, if found, and the offset from the symbol to ADDR. */
8549
8550 static void
8551 find_symbol_for_address (Filedata * filedata,
8552 Elf_Internal_Sym * symtab,
8553 unsigned long nsyms,
8554 const char * strtab,
8555 unsigned long strtab_size,
8556 struct absaddr addr,
8557 const char ** symname,
8558 uint64_t * offset)
8559 {
8560 uint64_t dist = 0x100000;
8561 Elf_Internal_Sym * sym;
8562 Elf_Internal_Sym * beg;
8563 Elf_Internal_Sym * end;
8564 Elf_Internal_Sym * best = NULL;
8565
8566 REMOVE_ARCH_BITS (addr.offset);
8567 beg = symtab;
8568 end = symtab + nsyms;
8569
8570 while (beg < end)
8571 {
8572 uint64_t value;
8573
8574 sym = beg + (end - beg) / 2;
8575
8576 value = sym->st_value;
8577 REMOVE_ARCH_BITS (value);
8578
8579 if (sym->st_name != 0
8580 && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
8581 && addr.offset >= value
8582 && addr.offset - value < dist)
8583 {
8584 best = sym;
8585 dist = addr.offset - value;
8586 if (!dist)
8587 break;
8588 }
8589
8590 if (addr.offset < value)
8591 end = sym;
8592 else
8593 beg = sym + 1;
8594 }
8595
8596 if (best)
8597 {
8598 *symname = (best->st_name >= strtab_size
8599 ? _("<corrupt>") : strtab + best->st_name);
8600 *offset = dist;
8601 return;
8602 }
8603
8604 *symname = NULL;
8605 *offset = addr.offset;
8606 }
8607
8608 static /* signed */ int
8609 symcmp (const void *p, const void *q)
8610 {
8611 Elf_Internal_Sym *sp = (Elf_Internal_Sym *) p;
8612 Elf_Internal_Sym *sq = (Elf_Internal_Sym *) q;
8613
8614 return sp->st_value > sq->st_value ? 1 : (sp->st_value < sq->st_value ? -1 : 0);
8615 }
8616
8617 /* Process the unwind section. */
8618
8619 #include "unwind-ia64.h"
8620
8621 struct ia64_unw_table_entry
8622 {
8623 struct absaddr start;
8624 struct absaddr end;
8625 struct absaddr info;
8626 };
8627
8628 struct ia64_unw_aux_info
8629 {
8630 struct ia64_unw_table_entry * table; /* Unwind table. */
8631 unsigned long table_len; /* Length of unwind table. */
8632 unsigned char * info; /* Unwind info. */
8633 unsigned long info_size; /* Size of unwind info. */
8634 uint64_t info_addr; /* Starting address of unwind info. */
8635 uint64_t seg_base; /* Starting address of segment. */
8636 Elf_Internal_Sym * symtab; /* The symbol table. */
8637 unsigned long nsyms; /* Number of symbols. */
8638 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
8639 unsigned long nfuns; /* Number of entries in funtab. */
8640 char * strtab; /* The string table. */
8641 unsigned long strtab_size; /* Size of string table. */
8642 };
8643
8644 static bool
8645 dump_ia64_unwind (Filedata * filedata, struct ia64_unw_aux_info * aux)
8646 {
8647 struct ia64_unw_table_entry * tp;
8648 unsigned long j, nfuns;
8649 int in_body;
8650 bool res = true;
8651
8652 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
8653 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
8654 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
8655 aux->funtab[nfuns++] = aux->symtab[j];
8656 aux->nfuns = nfuns;
8657 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
8658
8659 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
8660 {
8661 uint64_t stamp;
8662 uint64_t offset;
8663 const unsigned char * dp;
8664 const unsigned char * head;
8665 const unsigned char * end;
8666 const char * procname;
8667
8668 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
8669 aux->strtab_size, tp->start, &procname, &offset);
8670
8671 fputs ("\n<", stdout);
8672
8673 if (procname)
8674 {
8675 fputs (procname, stdout);
8676
8677 if (offset)
8678 printf ("+%lx", (unsigned long) offset);
8679 }
8680
8681 fputs (">: [", stdout);
8682 print_vma (tp->start.offset, PREFIX_HEX);
8683 fputc ('-', stdout);
8684 print_vma (tp->end.offset, PREFIX_HEX);
8685 printf ("], info at +0x%lx\n",
8686 (unsigned long) (tp->info.offset - aux->seg_base));
8687
8688 /* PR 17531: file: 86232b32. */
8689 if (aux->info == NULL)
8690 continue;
8691
8692 offset = tp->info.offset;
8693 if (tp->info.section)
8694 {
8695 if (tp->info.section >= filedata->file_header.e_shnum)
8696 {
8697 warn (_("Invalid section %u in table entry %ld\n"),
8698 tp->info.section, (long) (tp - aux->table));
8699 res = false;
8700 continue;
8701 }
8702 offset += filedata->section_headers[tp->info.section].sh_addr;
8703 }
8704 offset -= aux->info_addr;
8705 /* PR 17531: file: 0997b4d1. */
8706 if (offset >= aux->info_size
8707 || aux->info_size - offset < 8)
8708 {
8709 warn (_("Invalid offset %lx in table entry %ld\n"),
8710 (long) tp->info.offset, (long) (tp - aux->table));
8711 res = false;
8712 continue;
8713 }
8714
8715 head = aux->info + offset;
8716 stamp = byte_get ((unsigned char *) head, sizeof (stamp));
8717
8718 printf (" v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
8719 (unsigned) UNW_VER (stamp),
8720 (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
8721 UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
8722 UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
8723 (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
8724
8725 if (UNW_VER (stamp) != 1)
8726 {
8727 printf (_("\tUnknown version.\n"));
8728 continue;
8729 }
8730
8731 in_body = 0;
8732 end = head + 8 + eh_addr_size * UNW_LENGTH (stamp);
8733 /* PR 17531: file: 16ceda89. */
8734 if (end > aux->info + aux->info_size)
8735 end = aux->info + aux->info_size;
8736 for (dp = head + 8; dp < end;)
8737 dp = unw_decode (dp, in_body, & in_body, end);
8738 }
8739
8740 free (aux->funtab);
8741
8742 return res;
8743 }
8744
8745 static bool
8746 slurp_ia64_unwind_table (Filedata * filedata,
8747 struct ia64_unw_aux_info * aux,
8748 Elf_Internal_Shdr * sec)
8749 {
8750 unsigned long size, nrelas, i;
8751 Elf_Internal_Phdr * seg;
8752 struct ia64_unw_table_entry * tep;
8753 Elf_Internal_Shdr * relsec;
8754 Elf_Internal_Rela * rela;
8755 Elf_Internal_Rela * rp;
8756 unsigned char * table;
8757 unsigned char * tp;
8758 Elf_Internal_Sym * sym;
8759 const char * relname;
8760
8761 aux->table_len = 0;
8762
8763 /* First, find the starting address of the segment that includes
8764 this section: */
8765
8766 if (filedata->file_header.e_phnum)
8767 {
8768 if (! get_program_headers (filedata))
8769 return false;
8770
8771 for (seg = filedata->program_headers;
8772 seg < filedata->program_headers + filedata->file_header.e_phnum;
8773 ++seg)
8774 {
8775 if (seg->p_type != PT_LOAD)
8776 continue;
8777
8778 if (sec->sh_addr >= seg->p_vaddr
8779 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
8780 {
8781 aux->seg_base = seg->p_vaddr;
8782 break;
8783 }
8784 }
8785 }
8786
8787 /* Second, build the unwind table from the contents of the unwind section: */
8788 size = sec->sh_size;
8789 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
8790 _("unwind table"));
8791 if (!table)
8792 return false;
8793
8794 aux->table_len = size / (3 * eh_addr_size);
8795 aux->table = (struct ia64_unw_table_entry *)
8796 xcmalloc (aux->table_len, sizeof (aux->table[0]));
8797 tep = aux->table;
8798
8799 for (tp = table; tp <= table + size - (3 * eh_addr_size); ++tep)
8800 {
8801 tep->start.section = SHN_UNDEF;
8802 tep->end.section = SHN_UNDEF;
8803 tep->info.section = SHN_UNDEF;
8804 tep->start.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8805 tep->end.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8806 tep->info.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8807 tep->start.offset += aux->seg_base;
8808 tep->end.offset += aux->seg_base;
8809 tep->info.offset += aux->seg_base;
8810 }
8811 free (table);
8812
8813 /* Third, apply any relocations to the unwind table: */
8814 for (relsec = filedata->section_headers;
8815 relsec < filedata->section_headers + filedata->file_header.e_shnum;
8816 ++relsec)
8817 {
8818 if (relsec->sh_type != SHT_RELA
8819 || relsec->sh_info >= filedata->file_header.e_shnum
8820 || filedata->section_headers + relsec->sh_info != sec)
8821 continue;
8822
8823 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
8824 & rela, & nrelas))
8825 {
8826 free (aux->table);
8827 aux->table = NULL;
8828 aux->table_len = 0;
8829 return false;
8830 }
8831
8832 for (rp = rela; rp < rela + nrelas; ++rp)
8833 {
8834 unsigned int sym_ndx;
8835 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
8836 relname = elf_ia64_reloc_type (r_type);
8837
8838 /* PR 17531: file: 9fa67536. */
8839 if (relname == NULL)
8840 {
8841 warn (_("Skipping unknown relocation type: %u\n"), r_type);
8842 continue;
8843 }
8844
8845 if (! startswith (relname, "R_IA64_SEGREL"))
8846 {
8847 warn (_("Skipping unexpected relocation type: %s\n"), relname);
8848 continue;
8849 }
8850
8851 i = rp->r_offset / (3 * eh_addr_size);
8852
8853 /* PR 17531: file: 5bc8d9bf. */
8854 if (i >= aux->table_len)
8855 {
8856 warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
8857 continue;
8858 }
8859
8860 sym_ndx = get_reloc_symindex (rp->r_info);
8861 if (sym_ndx >= aux->nsyms)
8862 {
8863 warn (_("Skipping reloc with invalid symbol index: %u\n"),
8864 sym_ndx);
8865 continue;
8866 }
8867 sym = aux->symtab + sym_ndx;
8868
8869 switch (rp->r_offset / eh_addr_size % 3)
8870 {
8871 case 0:
8872 aux->table[i].start.section = sym->st_shndx;
8873 aux->table[i].start.offset = rp->r_addend + sym->st_value;
8874 break;
8875 case 1:
8876 aux->table[i].end.section = sym->st_shndx;
8877 aux->table[i].end.offset = rp->r_addend + sym->st_value;
8878 break;
8879 case 2:
8880 aux->table[i].info.section = sym->st_shndx;
8881 aux->table[i].info.offset = rp->r_addend + sym->st_value;
8882 break;
8883 default:
8884 break;
8885 }
8886 }
8887
8888 free (rela);
8889 }
8890
8891 return true;
8892 }
8893
8894 static bool
8895 ia64_process_unwind (Filedata * filedata)
8896 {
8897 Elf_Internal_Shdr * sec;
8898 Elf_Internal_Shdr * unwsec = NULL;
8899 unsigned long i, unwcount = 0, unwstart = 0;
8900 struct ia64_unw_aux_info aux;
8901 bool res = true;
8902
8903 memset (& aux, 0, sizeof (aux));
8904
8905 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
8906 {
8907 if (sec->sh_type == SHT_SYMTAB)
8908 {
8909 if (aux.symtab)
8910 {
8911 error (_("Multiple symbol tables encountered\n"));
8912 free (aux.symtab);
8913 aux.symtab = NULL;
8914 free (aux.strtab);
8915 aux.strtab = NULL;
8916 }
8917 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
8918 &aux.strtab, &aux.strtab_size))
8919 return false;
8920 }
8921 else if (sec->sh_type == SHT_IA_64_UNWIND)
8922 unwcount++;
8923 }
8924
8925 if (!unwcount)
8926 printf (_("\nThere are no unwind sections in this file.\n"));
8927
8928 while (unwcount-- > 0)
8929 {
8930 const char *suffix;
8931 size_t len, len2;
8932
8933 for (i = unwstart, sec = filedata->section_headers + unwstart, unwsec = NULL;
8934 i < filedata->file_header.e_shnum; ++i, ++sec)
8935 if (sec->sh_type == SHT_IA_64_UNWIND)
8936 {
8937 unwsec = sec;
8938 break;
8939 }
8940 /* We have already counted the number of SHT_IA64_UNWIND
8941 sections so the loop above should never fail. */
8942 assert (unwsec != NULL);
8943
8944 unwstart = i + 1;
8945 len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
8946
8947 if ((unwsec->sh_flags & SHF_GROUP) != 0)
8948 {
8949 /* We need to find which section group it is in. */
8950 struct group_list * g;
8951
8952 if (filedata->section_headers_groups == NULL
8953 || filedata->section_headers_groups[i] == NULL)
8954 i = filedata->file_header.e_shnum;
8955 else
8956 {
8957 g = filedata->section_headers_groups[i]->root;
8958
8959 for (; g != NULL; g = g->next)
8960 {
8961 sec = filedata->section_headers + g->section_index;
8962
8963 if (section_name_valid (filedata, sec)
8964 && streq (section_name (filedata, sec),
8965 ELF_STRING_ia64_unwind_info))
8966 break;
8967 }
8968
8969 if (g == NULL)
8970 i = filedata->file_header.e_shnum;
8971 }
8972 }
8973 else if (section_name_valid (filedata, unwsec)
8974 && startswith (section_name (filedata, unwsec),
8975 ELF_STRING_ia64_unwind_once))
8976 {
8977 /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO. */
8978 len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
8979 suffix = section_name (filedata, unwsec) + len;
8980 for (i = 0, sec = filedata->section_headers;
8981 i < filedata->file_header.e_shnum;
8982 ++i, ++sec)
8983 if (section_name_valid (filedata, sec)
8984 && startswith (section_name (filedata, sec),
8985 ELF_STRING_ia64_unwind_info_once)
8986 && streq (section_name (filedata, sec) + len2, suffix))
8987 break;
8988 }
8989 else
8990 {
8991 /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
8992 .IA_64.unwind or BAR -> .IA_64.unwind_info. */
8993 len = sizeof (ELF_STRING_ia64_unwind) - 1;
8994 len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
8995 suffix = "";
8996 if (section_name_valid (filedata, unwsec)
8997 && startswith (section_name (filedata, unwsec),
8998 ELF_STRING_ia64_unwind))
8999 suffix = section_name (filedata, unwsec) + len;
9000 for (i = 0, sec = filedata->section_headers;
9001 i < filedata->file_header.e_shnum;
9002 ++i, ++sec)
9003 if (section_name_valid (filedata, sec)
9004 && startswith (section_name (filedata, sec),
9005 ELF_STRING_ia64_unwind_info)
9006 && streq (section_name (filedata, sec) + len2, suffix))
9007 break;
9008 }
9009
9010 if (i == filedata->file_header.e_shnum)
9011 {
9012 printf (_("\nCould not find unwind info section for "));
9013
9014 if (filedata->string_table == NULL)
9015 printf ("%d", unwsec->sh_name);
9016 else
9017 printf ("'%s'", printable_section_name (filedata, unwsec));
9018 }
9019 else
9020 {
9021 aux.info_addr = sec->sh_addr;
9022 aux.info = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1,
9023 sec->sh_size,
9024 _("unwind info"));
9025 aux.info_size = aux.info == NULL ? 0 : sec->sh_size;
9026
9027 printf (_("\nUnwind section "));
9028
9029 if (filedata->string_table == NULL)
9030 printf ("%d", unwsec->sh_name);
9031 else
9032 printf ("'%s'", printable_section_name (filedata, unwsec));
9033
9034 printf (_(" at offset 0x%lx contains %lu entries:\n"),
9035 (unsigned long) unwsec->sh_offset,
9036 (unsigned long) (unwsec->sh_size / (3 * eh_addr_size)));
9037
9038 if (slurp_ia64_unwind_table (filedata, & aux, unwsec)
9039 && aux.table_len > 0)
9040 dump_ia64_unwind (filedata, & aux);
9041
9042 free ((char *) aux.table);
9043 free ((char *) aux.info);
9044 aux.table = NULL;
9045 aux.info = NULL;
9046 }
9047 }
9048
9049 free (aux.symtab);
9050 free ((char *) aux.strtab);
9051
9052 return res;
9053 }
9054
9055 struct hppa_unw_table_entry
9056 {
9057 struct absaddr start;
9058 struct absaddr end;
9059 unsigned int Cannot_unwind:1; /* 0 */
9060 unsigned int Millicode:1; /* 1 */
9061 unsigned int Millicode_save_sr0:1; /* 2 */
9062 unsigned int Region_description:2; /* 3..4 */
9063 unsigned int reserved1:1; /* 5 */
9064 unsigned int Entry_SR:1; /* 6 */
9065 unsigned int Entry_FR:4; /* Number saved 7..10 */
9066 unsigned int Entry_GR:5; /* Number saved 11..15 */
9067 unsigned int Args_stored:1; /* 16 */
9068 unsigned int Variable_Frame:1; /* 17 */
9069 unsigned int Separate_Package_Body:1; /* 18 */
9070 unsigned int Frame_Extension_Millicode:1; /* 19 */
9071 unsigned int Stack_Overflow_Check:1; /* 20 */
9072 unsigned int Two_Instruction_SP_Increment:1; /* 21 */
9073 unsigned int Ada_Region:1; /* 22 */
9074 unsigned int cxx_info:1; /* 23 */
9075 unsigned int cxx_try_catch:1; /* 24 */
9076 unsigned int sched_entry_seq:1; /* 25 */
9077 unsigned int reserved2:1; /* 26 */
9078 unsigned int Save_SP:1; /* 27 */
9079 unsigned int Save_RP:1; /* 28 */
9080 unsigned int Save_MRP_in_frame:1; /* 29 */
9081 unsigned int extn_ptr_defined:1; /* 30 */
9082 unsigned int Cleanup_defined:1; /* 31 */
9083
9084 unsigned int MPE_XL_interrupt_marker:1; /* 0 */
9085 unsigned int HP_UX_interrupt_marker:1; /* 1 */
9086 unsigned int Large_frame:1; /* 2 */
9087 unsigned int Pseudo_SP_Set:1; /* 3 */
9088 unsigned int reserved4:1; /* 4 */
9089 unsigned int Total_frame_size:27; /* 5..31 */
9090 };
9091
9092 struct hppa_unw_aux_info
9093 {
9094 struct hppa_unw_table_entry * table; /* Unwind table. */
9095 unsigned long table_len; /* Length of unwind table. */
9096 uint64_t seg_base; /* Starting address of segment. */
9097 Elf_Internal_Sym * symtab; /* The symbol table. */
9098 unsigned long nsyms; /* Number of symbols. */
9099 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
9100 unsigned long nfuns; /* Number of entries in funtab. */
9101 char * strtab; /* The string table. */
9102 unsigned long strtab_size; /* Size of string table. */
9103 };
9104
9105 static bool
9106 dump_hppa_unwind (Filedata * filedata, struct hppa_unw_aux_info * aux)
9107 {
9108 struct hppa_unw_table_entry * tp;
9109 unsigned long j, nfuns;
9110 bool res = true;
9111
9112 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
9113 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
9114 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
9115 aux->funtab[nfuns++] = aux->symtab[j];
9116 aux->nfuns = nfuns;
9117 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
9118
9119 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
9120 {
9121 uint64_t offset;
9122 const char * procname;
9123
9124 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
9125 aux->strtab_size, tp->start, &procname,
9126 &offset);
9127
9128 fputs ("\n<", stdout);
9129
9130 if (procname)
9131 {
9132 fputs (procname, stdout);
9133
9134 if (offset)
9135 printf ("+%lx", (unsigned long) offset);
9136 }
9137
9138 fputs (">: [", stdout);
9139 print_vma (tp->start.offset, PREFIX_HEX);
9140 fputc ('-', stdout);
9141 print_vma (tp->end.offset, PREFIX_HEX);
9142 printf ("]\n\t");
9143
9144 #define PF(_m) if (tp->_m) printf (#_m " ");
9145 #define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
9146 PF(Cannot_unwind);
9147 PF(Millicode);
9148 PF(Millicode_save_sr0);
9149 /* PV(Region_description); */
9150 PF(Entry_SR);
9151 PV(Entry_FR);
9152 PV(Entry_GR);
9153 PF(Args_stored);
9154 PF(Variable_Frame);
9155 PF(Separate_Package_Body);
9156 PF(Frame_Extension_Millicode);
9157 PF(Stack_Overflow_Check);
9158 PF(Two_Instruction_SP_Increment);
9159 PF(Ada_Region);
9160 PF(cxx_info);
9161 PF(cxx_try_catch);
9162 PF(sched_entry_seq);
9163 PF(Save_SP);
9164 PF(Save_RP);
9165 PF(Save_MRP_in_frame);
9166 PF(extn_ptr_defined);
9167 PF(Cleanup_defined);
9168 PF(MPE_XL_interrupt_marker);
9169 PF(HP_UX_interrupt_marker);
9170 PF(Large_frame);
9171 PF(Pseudo_SP_Set);
9172 PV(Total_frame_size);
9173 #undef PF
9174 #undef PV
9175 }
9176
9177 printf ("\n");
9178
9179 free (aux->funtab);
9180
9181 return res;
9182 }
9183
9184 static bool
9185 slurp_hppa_unwind_table (Filedata * filedata,
9186 struct hppa_unw_aux_info * aux,
9187 Elf_Internal_Shdr * sec)
9188 {
9189 unsigned long size, unw_ent_size, nentries, nrelas, i;
9190 Elf_Internal_Phdr * seg;
9191 struct hppa_unw_table_entry * tep;
9192 Elf_Internal_Shdr * relsec;
9193 Elf_Internal_Rela * rela;
9194 Elf_Internal_Rela * rp;
9195 unsigned char * table;
9196 unsigned char * tp;
9197 Elf_Internal_Sym * sym;
9198 const char * relname;
9199
9200 /* First, find the starting address of the segment that includes
9201 this section. */
9202 if (filedata->file_header.e_phnum)
9203 {
9204 if (! get_program_headers (filedata))
9205 return false;
9206
9207 for (seg = filedata->program_headers;
9208 seg < filedata->program_headers + filedata->file_header.e_phnum;
9209 ++seg)
9210 {
9211 if (seg->p_type != PT_LOAD)
9212 continue;
9213
9214 if (sec->sh_addr >= seg->p_vaddr
9215 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
9216 {
9217 aux->seg_base = seg->p_vaddr;
9218 break;
9219 }
9220 }
9221 }
9222
9223 /* Second, build the unwind table from the contents of the unwind
9224 section. */
9225 size = sec->sh_size;
9226 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
9227 _("unwind table"));
9228 if (!table)
9229 return false;
9230
9231 unw_ent_size = 16;
9232 nentries = size / unw_ent_size;
9233 size = unw_ent_size * nentries;
9234
9235 aux->table_len = nentries;
9236 tep = aux->table = (struct hppa_unw_table_entry *)
9237 xcmalloc (nentries, sizeof (aux->table[0]));
9238
9239 for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
9240 {
9241 unsigned int tmp1, tmp2;
9242
9243 tep->start.section = SHN_UNDEF;
9244 tep->end.section = SHN_UNDEF;
9245
9246 tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
9247 tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
9248 tmp1 = byte_get ((unsigned char *) tp + 8, 4);
9249 tmp2 = byte_get ((unsigned char *) tp + 12, 4);
9250
9251 tep->start.offset += aux->seg_base;
9252 tep->end.offset += aux->seg_base;
9253
9254 tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
9255 tep->Millicode = (tmp1 >> 30) & 0x1;
9256 tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
9257 tep->Region_description = (tmp1 >> 27) & 0x3;
9258 tep->reserved1 = (tmp1 >> 26) & 0x1;
9259 tep->Entry_SR = (tmp1 >> 25) & 0x1;
9260 tep->Entry_FR = (tmp1 >> 21) & 0xf;
9261 tep->Entry_GR = (tmp1 >> 16) & 0x1f;
9262 tep->Args_stored = (tmp1 >> 15) & 0x1;
9263 tep->Variable_Frame = (tmp1 >> 14) & 0x1;
9264 tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
9265 tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
9266 tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
9267 tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
9268 tep->Ada_Region = (tmp1 >> 9) & 0x1;
9269 tep->cxx_info = (tmp1 >> 8) & 0x1;
9270 tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
9271 tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
9272 tep->reserved2 = (tmp1 >> 5) & 0x1;
9273 tep->Save_SP = (tmp1 >> 4) & 0x1;
9274 tep->Save_RP = (tmp1 >> 3) & 0x1;
9275 tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
9276 tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
9277 tep->Cleanup_defined = tmp1 & 0x1;
9278
9279 tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
9280 tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
9281 tep->Large_frame = (tmp2 >> 29) & 0x1;
9282 tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
9283 tep->reserved4 = (tmp2 >> 27) & 0x1;
9284 tep->Total_frame_size = tmp2 & 0x7ffffff;
9285 }
9286 free (table);
9287
9288 /* Third, apply any relocations to the unwind table. */
9289 for (relsec = filedata->section_headers;
9290 relsec < filedata->section_headers + filedata->file_header.e_shnum;
9291 ++relsec)
9292 {
9293 if (relsec->sh_type != SHT_RELA
9294 || relsec->sh_info >= filedata->file_header.e_shnum
9295 || filedata->section_headers + relsec->sh_info != sec)
9296 continue;
9297
9298 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
9299 & rela, & nrelas))
9300 return false;
9301
9302 for (rp = rela; rp < rela + nrelas; ++rp)
9303 {
9304 unsigned int sym_ndx;
9305 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
9306 relname = elf_hppa_reloc_type (r_type);
9307
9308 if (relname == NULL)
9309 {
9310 warn (_("Skipping unknown relocation type: %u\n"), r_type);
9311 continue;
9312 }
9313
9314 /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64. */
9315 if (! startswith (relname, "R_PARISC_SEGREL"))
9316 {
9317 warn (_("Skipping unexpected relocation type: %s\n"), relname);
9318 continue;
9319 }
9320
9321 i = rp->r_offset / unw_ent_size;
9322 if (i >= aux->table_len)
9323 {
9324 warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
9325 continue;
9326 }
9327
9328 sym_ndx = get_reloc_symindex (rp->r_info);
9329 if (sym_ndx >= aux->nsyms)
9330 {
9331 warn (_("Skipping reloc with invalid symbol index: %u\n"),
9332 sym_ndx);
9333 continue;
9334 }
9335 sym = aux->symtab + sym_ndx;
9336
9337 switch ((rp->r_offset % unw_ent_size) / 4)
9338 {
9339 case 0:
9340 aux->table[i].start.section = sym->st_shndx;
9341 aux->table[i].start.offset = sym->st_value + rp->r_addend;
9342 break;
9343 case 1:
9344 aux->table[i].end.section = sym->st_shndx;
9345 aux->table[i].end.offset = sym->st_value + rp->r_addend;
9346 break;
9347 default:
9348 break;
9349 }
9350 }
9351
9352 free (rela);
9353 }
9354
9355 return true;
9356 }
9357
9358 static bool
9359 hppa_process_unwind (Filedata * filedata)
9360 {
9361 struct hppa_unw_aux_info aux;
9362 Elf_Internal_Shdr * unwsec = NULL;
9363 Elf_Internal_Shdr * sec;
9364 unsigned long i;
9365 bool res = true;
9366
9367 if (filedata->string_table == NULL)
9368 return false;
9369
9370 memset (& aux, 0, sizeof (aux));
9371
9372 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9373 {
9374 if (sec->sh_type == SHT_SYMTAB)
9375 {
9376 if (aux.symtab)
9377 {
9378 error (_("Multiple symbol tables encountered\n"));
9379 free (aux.symtab);
9380 aux.symtab = NULL;
9381 free (aux.strtab);
9382 aux.strtab = NULL;
9383 }
9384 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
9385 &aux.strtab, &aux.strtab_size))
9386 return false;
9387 }
9388 else if (section_name_valid (filedata, sec)
9389 && streq (section_name (filedata, sec), ".PARISC.unwind"))
9390 unwsec = sec;
9391 }
9392
9393 if (!unwsec)
9394 printf (_("\nThere are no unwind sections in this file.\n"));
9395
9396 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9397 {
9398 if (section_name_valid (filedata, sec)
9399 && streq (section_name (filedata, sec), ".PARISC.unwind"))
9400 {
9401 unsigned long num_unwind = sec->sh_size / 16;
9402
9403 printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
9404 "contains %lu entry:\n",
9405 "\nUnwind section '%s' at offset 0x%lx "
9406 "contains %lu entries:\n",
9407 num_unwind),
9408 printable_section_name (filedata, sec),
9409 (unsigned long) sec->sh_offset,
9410 num_unwind);
9411
9412 if (! slurp_hppa_unwind_table (filedata, &aux, sec))
9413 res = false;
9414
9415 if (res && aux.table_len > 0)
9416 {
9417 if (! dump_hppa_unwind (filedata, &aux))
9418 res = false;
9419 }
9420
9421 free ((char *) aux.table);
9422 aux.table = NULL;
9423 }
9424 }
9425
9426 free (aux.symtab);
9427 free ((char *) aux.strtab);
9428
9429 return res;
9430 }
9431
9432 struct arm_section
9433 {
9434 unsigned char * data; /* The unwind data. */
9435 Elf_Internal_Shdr * sec; /* The cached unwind section header. */
9436 Elf_Internal_Rela * rela; /* The cached relocations for this section. */
9437 unsigned long nrelas; /* The number of relocations. */
9438 unsigned int rel_type; /* REL or RELA ? */
9439 Elf_Internal_Rela * next_rela; /* Cyclic pointer to the next reloc to process. */
9440 };
9441
9442 struct arm_unw_aux_info
9443 {
9444 Filedata * filedata; /* The file containing the unwind sections. */
9445 Elf_Internal_Sym * symtab; /* The file's symbol table. */
9446 unsigned long nsyms; /* Number of symbols. */
9447 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
9448 unsigned long nfuns; /* Number of these symbols. */
9449 char * strtab; /* The file's string table. */
9450 unsigned long strtab_size; /* Size of string table. */
9451 };
9452
9453 static const char *
9454 arm_print_vma_and_name (Filedata * filedata,
9455 struct arm_unw_aux_info * aux,
9456 uint64_t fn,
9457 struct absaddr addr)
9458 {
9459 const char *procname;
9460 uint64_t sym_offset;
9461
9462 if (addr.section == SHN_UNDEF)
9463 addr.offset = fn;
9464
9465 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
9466 aux->strtab_size, addr, &procname,
9467 &sym_offset);
9468
9469 print_vma (fn, PREFIX_HEX);
9470
9471 if (procname)
9472 {
9473 fputs (" <", stdout);
9474 fputs (procname, stdout);
9475
9476 if (sym_offset)
9477 printf ("+0x%lx", (unsigned long) sym_offset);
9478 fputc ('>', stdout);
9479 }
9480
9481 return procname;
9482 }
9483
9484 static void
9485 arm_free_section (struct arm_section *arm_sec)
9486 {
9487 free (arm_sec->data);
9488 free (arm_sec->rela);
9489 }
9490
9491 /* 1) If SEC does not match the one cached in ARM_SEC, then free the current
9492 cached section and install SEC instead.
9493 2) Locate the 32-bit word at WORD_OFFSET in unwind section SEC
9494 and return its valued in * WORDP, relocating if necessary.
9495 3) Update the NEXT_RELA field in ARM_SEC and store the section index and
9496 relocation's offset in ADDR.
9497 4) If SYM_NAME is non-NULL and a relocation was applied, record the offset
9498 into the string table of the symbol associated with the reloc. If no
9499 reloc was applied store -1 there.
9500 5) Return TRUE upon success, FALSE otherwise. */
9501
9502 static bool
9503 get_unwind_section_word (Filedata * filedata,
9504 struct arm_unw_aux_info * aux,
9505 struct arm_section * arm_sec,
9506 Elf_Internal_Shdr * sec,
9507 uint64_t word_offset,
9508 unsigned int * wordp,
9509 struct absaddr * addr,
9510 uint64_t * sym_name)
9511 {
9512 Elf_Internal_Rela *rp;
9513 Elf_Internal_Sym *sym;
9514 const char * relname;
9515 unsigned int word;
9516 bool wrapped;
9517
9518 if (sec == NULL || arm_sec == NULL)
9519 return false;
9520
9521 addr->section = SHN_UNDEF;
9522 addr->offset = 0;
9523
9524 if (sym_name != NULL)
9525 *sym_name = (uint64_t) -1;
9526
9527 /* If necessary, update the section cache. */
9528 if (sec != arm_sec->sec)
9529 {
9530 Elf_Internal_Shdr *relsec;
9531
9532 arm_free_section (arm_sec);
9533
9534 arm_sec->sec = sec;
9535 arm_sec->data = get_data (NULL, aux->filedata, sec->sh_offset, 1,
9536 sec->sh_size, _("unwind data"));
9537 arm_sec->rela = NULL;
9538 arm_sec->nrelas = 0;
9539
9540 for (relsec = filedata->section_headers;
9541 relsec < filedata->section_headers + filedata->file_header.e_shnum;
9542 ++relsec)
9543 {
9544 if (relsec->sh_info >= filedata->file_header.e_shnum
9545 || filedata->section_headers + relsec->sh_info != sec
9546 /* PR 15745: Check the section type as well. */
9547 || (relsec->sh_type != SHT_REL
9548 && relsec->sh_type != SHT_RELA))
9549 continue;
9550
9551 arm_sec->rel_type = relsec->sh_type;
9552 if (relsec->sh_type == SHT_REL)
9553 {
9554 if (!slurp_rel_relocs (aux->filedata, relsec->sh_offset,
9555 relsec->sh_size,
9556 & arm_sec->rela, & arm_sec->nrelas))
9557 return false;
9558 }
9559 else /* relsec->sh_type == SHT_RELA */
9560 {
9561 if (!slurp_rela_relocs (aux->filedata, relsec->sh_offset,
9562 relsec->sh_size,
9563 & arm_sec->rela, & arm_sec->nrelas))
9564 return false;
9565 }
9566 break;
9567 }
9568
9569 arm_sec->next_rela = arm_sec->rela;
9570 }
9571
9572 /* If there is no unwind data we can do nothing. */
9573 if (arm_sec->data == NULL)
9574 return false;
9575
9576 /* If the offset is invalid then fail. */
9577 if (/* PR 21343 *//* PR 18879 */
9578 sec->sh_size < 4
9579 || word_offset > sec->sh_size - 4)
9580 return false;
9581
9582 /* Get the word at the required offset. */
9583 word = byte_get (arm_sec->data + word_offset, 4);
9584
9585 /* PR 17531: file: id:000001,src:001266+003044,op:splice,rep:128. */
9586 if (arm_sec->rela == NULL)
9587 {
9588 * wordp = word;
9589 return true;
9590 }
9591
9592 /* Look through the relocs to find the one that applies to the provided offset. */
9593 wrapped = false;
9594 for (rp = arm_sec->next_rela; rp != arm_sec->rela + arm_sec->nrelas; rp++)
9595 {
9596 uint64_t prelval, offset;
9597
9598 if (rp->r_offset > word_offset && !wrapped)
9599 {
9600 rp = arm_sec->rela;
9601 wrapped = true;
9602 }
9603 if (rp->r_offset > word_offset)
9604 break;
9605
9606 if (rp->r_offset & 3)
9607 {
9608 warn (_("Skipping unexpected relocation at offset 0x%lx\n"),
9609 (unsigned long) rp->r_offset);
9610 continue;
9611 }
9612
9613 if (rp->r_offset < word_offset)
9614 continue;
9615
9616 /* PR 17531: file: 027-161405-0.004 */
9617 if (aux->symtab == NULL)
9618 continue;
9619
9620 if (arm_sec->rel_type == SHT_REL)
9621 {
9622 offset = word & 0x7fffffff;
9623 if (offset & 0x40000000)
9624 offset |= ~ (uint64_t) 0x7fffffff;
9625 }
9626 else if (arm_sec->rel_type == SHT_RELA)
9627 offset = rp->r_addend;
9628 else
9629 {
9630 error (_("Unknown section relocation type %d encountered\n"),
9631 arm_sec->rel_type);
9632 break;
9633 }
9634
9635 /* PR 17531 file: 027-1241568-0.004. */
9636 if (ELF32_R_SYM (rp->r_info) >= aux->nsyms)
9637 {
9638 error (_("Bad symbol index in unwind relocation (%lu > %lu)\n"),
9639 (unsigned long) ELF32_R_SYM (rp->r_info), aux->nsyms);
9640 break;
9641 }
9642
9643 sym = aux->symtab + ELF32_R_SYM (rp->r_info);
9644 offset += sym->st_value;
9645 prelval = offset - (arm_sec->sec->sh_addr + rp->r_offset);
9646
9647 /* Check that we are processing the expected reloc type. */
9648 if (filedata->file_header.e_machine == EM_ARM)
9649 {
9650 relname = elf_arm_reloc_type (ELF32_R_TYPE (rp->r_info));
9651 if (relname == NULL)
9652 {
9653 warn (_("Skipping unknown ARM relocation type: %d\n"),
9654 (int) ELF32_R_TYPE (rp->r_info));
9655 continue;
9656 }
9657
9658 if (streq (relname, "R_ARM_NONE"))
9659 continue;
9660
9661 if (! streq (relname, "R_ARM_PREL31"))
9662 {
9663 warn (_("Skipping unexpected ARM relocation type %s\n"), relname);
9664 continue;
9665 }
9666 }
9667 else if (filedata->file_header.e_machine == EM_TI_C6000)
9668 {
9669 relname = elf_tic6x_reloc_type (ELF32_R_TYPE (rp->r_info));
9670 if (relname == NULL)
9671 {
9672 warn (_("Skipping unknown C6000 relocation type: %d\n"),
9673 (int) ELF32_R_TYPE (rp->r_info));
9674 continue;
9675 }
9676
9677 if (streq (relname, "R_C6000_NONE"))
9678 continue;
9679
9680 if (! streq (relname, "R_C6000_PREL31"))
9681 {
9682 warn (_("Skipping unexpected C6000 relocation type %s\n"), relname);
9683 continue;
9684 }
9685
9686 prelval >>= 1;
9687 }
9688 else
9689 {
9690 /* This function currently only supports ARM and TI unwinders. */
9691 warn (_("Only TI and ARM unwinders are currently supported\n"));
9692 break;
9693 }
9694
9695 word = (word & ~ (uint64_t) 0x7fffffff) | (prelval & 0x7fffffff);
9696 addr->section = sym->st_shndx;
9697 addr->offset = offset;
9698
9699 if (sym_name)
9700 * sym_name = sym->st_name;
9701 break;
9702 }
9703
9704 *wordp = word;
9705 arm_sec->next_rela = rp;
9706
9707 return true;
9708 }
9709
9710 static const char *tic6x_unwind_regnames[16] =
9711 {
9712 "A15", "B15", "B14", "B13", "B12", "B11", "B10", "B3",
9713 "A14", "A13", "A12", "A11", "A10",
9714 "[invalid reg 13]", "[invalid reg 14]", "[invalid reg 15]"
9715 };
9716
9717 static void
9718 decode_tic6x_unwind_regmask (unsigned int mask)
9719 {
9720 int i;
9721
9722 for (i = 12; mask; mask >>= 1, i--)
9723 {
9724 if (mask & 1)
9725 {
9726 fputs (tic6x_unwind_regnames[i], stdout);
9727 if (mask > 1)
9728 fputs (", ", stdout);
9729 }
9730 }
9731 }
9732
9733 #define ADVANCE \
9734 if (remaining == 0 && more_words) \
9735 { \
9736 data_offset += 4; \
9737 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, \
9738 data_offset, & word, & addr, NULL)) \
9739 return false; \
9740 remaining = 4; \
9741 more_words--; \
9742 } \
9743
9744 #define GET_OP(OP) \
9745 ADVANCE; \
9746 if (remaining) \
9747 { \
9748 remaining--; \
9749 (OP) = word >> 24; \
9750 word <<= 8; \
9751 } \
9752 else \
9753 { \
9754 printf (_("[Truncated opcode]\n")); \
9755 return false; \
9756 } \
9757 printf ("0x%02x ", OP)
9758
9759 static bool
9760 decode_arm_unwind_bytecode (Filedata * filedata,
9761 struct arm_unw_aux_info * aux,
9762 unsigned int word,
9763 unsigned int remaining,
9764 unsigned int more_words,
9765 uint64_t data_offset,
9766 Elf_Internal_Shdr * data_sec,
9767 struct arm_section * data_arm_sec)
9768 {
9769 struct absaddr addr;
9770 bool res = true;
9771
9772 /* Decode the unwinding instructions. */
9773 while (1)
9774 {
9775 unsigned int op, op2;
9776
9777 ADVANCE;
9778 if (remaining == 0)
9779 break;
9780 remaining--;
9781 op = word >> 24;
9782 word <<= 8;
9783
9784 printf (" 0x%02x ", op);
9785
9786 if ((op & 0xc0) == 0x00)
9787 {
9788 int offset = ((op & 0x3f) << 2) + 4;
9789
9790 printf (" vsp = vsp + %d", offset);
9791 }
9792 else if ((op & 0xc0) == 0x40)
9793 {
9794 int offset = ((op & 0x3f) << 2) + 4;
9795
9796 printf (" vsp = vsp - %d", offset);
9797 }
9798 else if ((op & 0xf0) == 0x80)
9799 {
9800 GET_OP (op2);
9801 if (op == 0x80 && op2 == 0)
9802 printf (_("Refuse to unwind"));
9803 else
9804 {
9805 unsigned int mask = ((op & 0x0f) << 8) | op2;
9806 bool first = true;
9807 int i;
9808
9809 printf ("pop {");
9810 for (i = 0; i < 12; i++)
9811 if (mask & (1 << i))
9812 {
9813 if (first)
9814 first = false;
9815 else
9816 printf (", ");
9817 printf ("r%d", 4 + i);
9818 }
9819 printf ("}");
9820 }
9821 }
9822 else if ((op & 0xf0) == 0x90)
9823 {
9824 if (op == 0x9d || op == 0x9f)
9825 printf (_(" [Reserved]"));
9826 else
9827 printf (" vsp = r%d", op & 0x0f);
9828 }
9829 else if ((op & 0xf0) == 0xa0)
9830 {
9831 int end = 4 + (op & 0x07);
9832 bool first = true;
9833 int i;
9834
9835 printf (" pop {");
9836 for (i = 4; i <= end; i++)
9837 {
9838 if (first)
9839 first = false;
9840 else
9841 printf (", ");
9842 printf ("r%d", i);
9843 }
9844 if (op & 0x08)
9845 {
9846 if (!first)
9847 printf (", ");
9848 printf ("r14");
9849 }
9850 printf ("}");
9851 }
9852 else if (op == 0xb0)
9853 printf (_(" finish"));
9854 else if (op == 0xb1)
9855 {
9856 GET_OP (op2);
9857 if (op2 == 0 || (op2 & 0xf0) != 0)
9858 printf (_("[Spare]"));
9859 else
9860 {
9861 unsigned int mask = op2 & 0x0f;
9862 bool first = true;
9863 int i;
9864
9865 printf ("pop {");
9866 for (i = 0; i < 12; i++)
9867 if (mask & (1 << i))
9868 {
9869 if (first)
9870 first = false;
9871 else
9872 printf (", ");
9873 printf ("r%d", i);
9874 }
9875 printf ("}");
9876 }
9877 }
9878 else if (op == 0xb2)
9879 {
9880 unsigned char buf[9];
9881 unsigned int i, len;
9882 unsigned long offset;
9883
9884 for (i = 0; i < sizeof (buf); i++)
9885 {
9886 GET_OP (buf[i]);
9887 if ((buf[i] & 0x80) == 0)
9888 break;
9889 }
9890 if (i == sizeof (buf))
9891 {
9892 error (_("corrupt change to vsp\n"));
9893 res = false;
9894 }
9895 else
9896 {
9897 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
9898 assert (len == i + 1);
9899 offset = offset * 4 + 0x204;
9900 printf ("vsp = vsp + %ld", offset);
9901 }
9902 }
9903 else if (op == 0xb3 || op == 0xc8 || op == 0xc9)
9904 {
9905 unsigned int first, last;
9906
9907 GET_OP (op2);
9908 first = op2 >> 4;
9909 last = op2 & 0x0f;
9910 if (op == 0xc8)
9911 first = first + 16;
9912 printf ("pop {D%d", first);
9913 if (last)
9914 printf ("-D%d", first + last);
9915 printf ("}");
9916 }
9917 else if (op == 0xb4)
9918 printf (_(" pop {ra_auth_code}"));
9919 else if ((op & 0xf8) == 0xb8 || (op & 0xf8) == 0xd0)
9920 {
9921 unsigned int count = op & 0x07;
9922
9923 printf ("pop {D8");
9924 if (count)
9925 printf ("-D%d", 8 + count);
9926 printf ("}");
9927 }
9928 else if (op >= 0xc0 && op <= 0xc5)
9929 {
9930 unsigned int count = op & 0x07;
9931
9932 printf (" pop {wR10");
9933 if (count)
9934 printf ("-wR%d", 10 + count);
9935 printf ("}");
9936 }
9937 else if (op == 0xc6)
9938 {
9939 unsigned int first, last;
9940
9941 GET_OP (op2);
9942 first = op2 >> 4;
9943 last = op2 & 0x0f;
9944 printf ("pop {wR%d", first);
9945 if (last)
9946 printf ("-wR%d", first + last);
9947 printf ("}");
9948 }
9949 else if (op == 0xc7)
9950 {
9951 GET_OP (op2);
9952 if (op2 == 0 || (op2 & 0xf0) != 0)
9953 printf (_("[Spare]"));
9954 else
9955 {
9956 unsigned int mask = op2 & 0x0f;
9957 bool first = true;
9958 int i;
9959
9960 printf ("pop {");
9961 for (i = 0; i < 4; i++)
9962 if (mask & (1 << i))
9963 {
9964 if (first)
9965 first = false;
9966 else
9967 printf (", ");
9968 printf ("wCGR%d", i);
9969 }
9970 printf ("}");
9971 }
9972 }
9973 else
9974 {
9975 printf (_(" [unsupported opcode]"));
9976 res = false;
9977 }
9978
9979 printf ("\n");
9980 }
9981
9982 return res;
9983 }
9984
9985 static bool
9986 decode_tic6x_unwind_bytecode (Filedata * filedata,
9987 struct arm_unw_aux_info * aux,
9988 unsigned int word,
9989 unsigned int remaining,
9990 unsigned int more_words,
9991 uint64_t data_offset,
9992 Elf_Internal_Shdr * data_sec,
9993 struct arm_section * data_arm_sec)
9994 {
9995 struct absaddr addr;
9996
9997 /* Decode the unwinding instructions. */
9998 while (1)
9999 {
10000 unsigned int op, op2;
10001
10002 ADVANCE;
10003 if (remaining == 0)
10004 break;
10005 remaining--;
10006 op = word >> 24;
10007 word <<= 8;
10008
10009 printf (" 0x%02x ", op);
10010
10011 if ((op & 0xc0) == 0x00)
10012 {
10013 int offset = ((op & 0x3f) << 3) + 8;
10014 printf (" sp = sp + %d", offset);
10015 }
10016 else if ((op & 0xc0) == 0x80)
10017 {
10018 GET_OP (op2);
10019 if (op == 0x80 && op2 == 0)
10020 printf (_("Refuse to unwind"));
10021 else
10022 {
10023 unsigned int mask = ((op & 0x1f) << 8) | op2;
10024 if (op & 0x20)
10025 printf ("pop compact {");
10026 else
10027 printf ("pop {");
10028
10029 decode_tic6x_unwind_regmask (mask);
10030 printf("}");
10031 }
10032 }
10033 else if ((op & 0xf0) == 0xc0)
10034 {
10035 unsigned int reg;
10036 unsigned int nregs;
10037 unsigned int i;
10038 const char *name;
10039 struct
10040 {
10041 unsigned int offset;
10042 unsigned int reg;
10043 } regpos[16];
10044
10045 /* Scan entire instruction first so that GET_OP output is not
10046 interleaved with disassembly. */
10047 nregs = 0;
10048 for (i = 0; nregs < (op & 0xf); i++)
10049 {
10050 GET_OP (op2);
10051 reg = op2 >> 4;
10052 if (reg != 0xf)
10053 {
10054 regpos[nregs].offset = i * 2;
10055 regpos[nregs].reg = reg;
10056 nregs++;
10057 }
10058
10059 reg = op2 & 0xf;
10060 if (reg != 0xf)
10061 {
10062 regpos[nregs].offset = i * 2 + 1;
10063 regpos[nregs].reg = reg;
10064 nregs++;
10065 }
10066 }
10067
10068 printf (_("pop frame {"));
10069 if (nregs == 0)
10070 {
10071 printf (_("*corrupt* - no registers specified"));
10072 }
10073 else
10074 {
10075 reg = nregs - 1;
10076 for (i = i * 2; i > 0; i--)
10077 {
10078 if (regpos[reg].offset == i - 1)
10079 {
10080 name = tic6x_unwind_regnames[regpos[reg].reg];
10081 if (reg > 0)
10082 reg--;
10083 }
10084 else
10085 name = _("[pad]");
10086
10087 fputs (name, stdout);
10088 if (i > 1)
10089 printf (", ");
10090 }
10091 }
10092
10093 printf ("}");
10094 }
10095 else if (op == 0xd0)
10096 printf (" MOV FP, SP");
10097 else if (op == 0xd1)
10098 printf (" __c6xabi_pop_rts");
10099 else if (op == 0xd2)
10100 {
10101 unsigned char buf[9];
10102 unsigned int i, len;
10103 unsigned long offset;
10104
10105 for (i = 0; i < sizeof (buf); i++)
10106 {
10107 GET_OP (buf[i]);
10108 if ((buf[i] & 0x80) == 0)
10109 break;
10110 }
10111 /* PR 17531: file: id:000001,src:001906+004739,op:splice,rep:2. */
10112 if (i == sizeof (buf))
10113 {
10114 warn (_("Corrupt stack pointer adjustment detected\n"));
10115 return false;
10116 }
10117
10118 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
10119 assert (len == i + 1);
10120 offset = offset * 8 + 0x408;
10121 printf (_("sp = sp + %ld"), offset);
10122 }
10123 else if ((op & 0xf0) == 0xe0)
10124 {
10125 if ((op & 0x0f) == 7)
10126 printf (" RETURN");
10127 else
10128 printf (" MV %s, B3", tic6x_unwind_regnames[op & 0x0f]);
10129 }
10130 else
10131 {
10132 printf (_(" [unsupported opcode]"));
10133 }
10134 putchar ('\n');
10135 }
10136
10137 return true;
10138 }
10139
10140 static uint64_t
10141 arm_expand_prel31 (Filedata * filedata, uint64_t word, uint64_t where)
10142 {
10143 uint64_t offset;
10144
10145 offset = word & 0x7fffffff;
10146 if (offset & 0x40000000)
10147 offset |= ~ (uint64_t) 0x7fffffff;
10148
10149 if (filedata->file_header.e_machine == EM_TI_C6000)
10150 offset <<= 1;
10151
10152 return offset + where;
10153 }
10154
10155 static bool
10156 decode_arm_unwind (Filedata * filedata,
10157 struct arm_unw_aux_info * aux,
10158 unsigned int word,
10159 unsigned int remaining,
10160 uint64_t data_offset,
10161 Elf_Internal_Shdr * data_sec,
10162 struct arm_section * data_arm_sec)
10163 {
10164 int per_index;
10165 unsigned int more_words = 0;
10166 struct absaddr addr;
10167 uint64_t sym_name = (uint64_t) -1;
10168 bool res = true;
10169
10170 if (remaining == 0)
10171 {
10172 /* Fetch the first word.
10173 Note - when decoding an object file the address extracted
10174 here will always be 0. So we also pass in the sym_name
10175 parameter so that we can find the symbol associated with
10176 the personality routine. */
10177 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, data_offset,
10178 & word, & addr, & sym_name))
10179 return false;
10180
10181 remaining = 4;
10182 }
10183 else
10184 {
10185 addr.section = SHN_UNDEF;
10186 addr.offset = 0;
10187 }
10188
10189 if ((word & 0x80000000) == 0)
10190 {
10191 /* Expand prel31 for personality routine. */
10192 uint64_t fn;
10193 const char *procname;
10194
10195 fn = arm_expand_prel31 (filedata, word, data_sec->sh_addr + data_offset);
10196 printf (_(" Personality routine: "));
10197 if (fn == 0
10198 && addr.section == SHN_UNDEF && addr.offset == 0
10199 && sym_name != (uint64_t) -1 && sym_name < aux->strtab_size)
10200 {
10201 procname = aux->strtab + sym_name;
10202 print_vma (fn, PREFIX_HEX);
10203 if (procname)
10204 {
10205 fputs (" <", stdout);
10206 fputs (procname, stdout);
10207 fputc ('>', stdout);
10208 }
10209 }
10210 else
10211 procname = arm_print_vma_and_name (filedata, aux, fn, addr);
10212 fputc ('\n', stdout);
10213
10214 /* The GCC personality routines use the standard compact
10215 encoding, starting with one byte giving the number of
10216 words. */
10217 if (procname != NULL
10218 && (startswith (procname, "__gcc_personality_v0")
10219 || startswith (procname, "__gxx_personality_v0")
10220 || startswith (procname, "__gcj_personality_v0")
10221 || startswith (procname, "__gnu_objc_personality_v0")))
10222 {
10223 remaining = 0;
10224 more_words = 1;
10225 ADVANCE;
10226 if (!remaining)
10227 {
10228 printf (_(" [Truncated data]\n"));
10229 return false;
10230 }
10231 more_words = word >> 24;
10232 word <<= 8;
10233 remaining--;
10234 per_index = -1;
10235 }
10236 else
10237 return true;
10238 }
10239 else
10240 {
10241 /* ARM EHABI Section 6.3:
10242
10243 An exception-handling table entry for the compact model looks like:
10244
10245 31 30-28 27-24 23-0
10246 -- ----- ----- ----
10247 1 0 index Data for personalityRoutine[index] */
10248
10249 if (filedata->file_header.e_machine == EM_ARM
10250 && (word & 0x70000000))
10251 {
10252 warn (_("Corrupt ARM compact model table entry: %x \n"), word);
10253 res = false;
10254 }
10255
10256 per_index = (word >> 24) & 0x7f;
10257 printf (_(" Compact model index: %d\n"), per_index);
10258 if (per_index == 0)
10259 {
10260 more_words = 0;
10261 word <<= 8;
10262 remaining--;
10263 }
10264 else if (per_index < 3)
10265 {
10266 more_words = (word >> 16) & 0xff;
10267 word <<= 16;
10268 remaining -= 2;
10269 }
10270 }
10271
10272 switch (filedata->file_header.e_machine)
10273 {
10274 case EM_ARM:
10275 if (per_index < 3)
10276 {
10277 if (! decode_arm_unwind_bytecode (filedata, aux, word, remaining, more_words,
10278 data_offset, data_sec, data_arm_sec))
10279 res = false;
10280 }
10281 else
10282 {
10283 warn (_("Unknown ARM compact model index encountered\n"));
10284 printf (_(" [reserved]\n"));
10285 res = false;
10286 }
10287 break;
10288
10289 case EM_TI_C6000:
10290 if (per_index < 3)
10291 {
10292 if (! decode_tic6x_unwind_bytecode (filedata, aux, word, remaining, more_words,
10293 data_offset, data_sec, data_arm_sec))
10294 res = false;
10295 }
10296 else if (per_index < 5)
10297 {
10298 if (((word >> 17) & 0x7f) == 0x7f)
10299 printf (_(" Restore stack from frame pointer\n"));
10300 else
10301 printf (_(" Stack increment %d\n"), (word >> 14) & 0x1fc);
10302 printf (_(" Registers restored: "));
10303 if (per_index == 4)
10304 printf (" (compact) ");
10305 decode_tic6x_unwind_regmask ((word >> 4) & 0x1fff);
10306 putchar ('\n');
10307 printf (_(" Return register: %s\n"),
10308 tic6x_unwind_regnames[word & 0xf]);
10309 }
10310 else
10311 printf (_(" [reserved (%d)]\n"), per_index);
10312 break;
10313
10314 default:
10315 error (_("Unsupported architecture type %d encountered when decoding unwind table\n"),
10316 filedata->file_header.e_machine);
10317 res = false;
10318 }
10319
10320 /* Decode the descriptors. Not implemented. */
10321
10322 return res;
10323 }
10324
10325 static bool
10326 dump_arm_unwind (Filedata * filedata,
10327 struct arm_unw_aux_info * aux,
10328 Elf_Internal_Shdr * exidx_sec)
10329 {
10330 struct arm_section exidx_arm_sec, extab_arm_sec;
10331 unsigned int i, exidx_len;
10332 unsigned long j, nfuns;
10333 bool res = true;
10334
10335 memset (&exidx_arm_sec, 0, sizeof (exidx_arm_sec));
10336 memset (&extab_arm_sec, 0, sizeof (extab_arm_sec));
10337 exidx_len = exidx_sec->sh_size / 8;
10338
10339 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
10340 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
10341 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
10342 aux->funtab[nfuns++] = aux->symtab[j];
10343 aux->nfuns = nfuns;
10344 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
10345
10346 for (i = 0; i < exidx_len; i++)
10347 {
10348 unsigned int exidx_fn, exidx_entry;
10349 struct absaddr fn_addr, entry_addr;
10350 uint64_t fn;
10351
10352 fputc ('\n', stdout);
10353
10354 if (! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
10355 8 * i, & exidx_fn, & fn_addr, NULL)
10356 || ! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
10357 8 * i + 4, & exidx_entry, & entry_addr, NULL))
10358 {
10359 free (aux->funtab);
10360 arm_free_section (& exidx_arm_sec);
10361 arm_free_section (& extab_arm_sec);
10362 return false;
10363 }
10364
10365 /* ARM EHABI, Section 5:
10366 An index table entry consists of 2 words.
10367 The first word contains a prel31 offset to the start of a function, with bit 31 clear. */
10368 if (exidx_fn & 0x80000000)
10369 {
10370 warn (_("corrupt index table entry: %x\n"), exidx_fn);
10371 res = false;
10372 }
10373
10374 fn = arm_expand_prel31 (filedata, exidx_fn, exidx_sec->sh_addr + 8 * i);
10375
10376 arm_print_vma_and_name (filedata, aux, fn, fn_addr);
10377 fputs (": ", stdout);
10378
10379 if (exidx_entry == 1)
10380 {
10381 print_vma (exidx_entry, PREFIX_HEX);
10382 fputs (" [cantunwind]\n", stdout);
10383 }
10384 else if (exidx_entry & 0x80000000)
10385 {
10386 print_vma (exidx_entry, PREFIX_HEX);
10387 fputc ('\n', stdout);
10388 decode_arm_unwind (filedata, aux, exidx_entry, 4, 0, NULL, NULL);
10389 }
10390 else
10391 {
10392 uint64_t table, table_offset = 0;
10393 Elf_Internal_Shdr *table_sec;
10394
10395 fputs ("@", stdout);
10396 table = arm_expand_prel31 (filedata, exidx_entry, exidx_sec->sh_addr + 8 * i + 4);
10397 print_vma (table, PREFIX_HEX);
10398 printf ("\n");
10399
10400 /* Locate the matching .ARM.extab. */
10401 if (entry_addr.section != SHN_UNDEF
10402 && entry_addr.section < filedata->file_header.e_shnum)
10403 {
10404 table_sec = filedata->section_headers + entry_addr.section;
10405 table_offset = entry_addr.offset;
10406 /* PR 18879 */
10407 if (table_offset > table_sec->sh_size)
10408 {
10409 warn (_("Unwind entry contains corrupt offset (0x%lx) into section %s\n"),
10410 (unsigned long) table_offset,
10411 printable_section_name (filedata, table_sec));
10412 res = false;
10413 continue;
10414 }
10415 }
10416 else
10417 {
10418 table_sec = find_section_by_address (filedata, table);
10419 if (table_sec != NULL)
10420 table_offset = table - table_sec->sh_addr;
10421 }
10422
10423 if (table_sec == NULL)
10424 {
10425 warn (_("Could not locate .ARM.extab section containing 0x%lx.\n"),
10426 (unsigned long) table);
10427 res = false;
10428 continue;
10429 }
10430
10431 if (! decode_arm_unwind (filedata, aux, 0, 0, table_offset, table_sec,
10432 &extab_arm_sec))
10433 res = false;
10434 }
10435 }
10436
10437 printf ("\n");
10438
10439 free (aux->funtab);
10440 arm_free_section (&exidx_arm_sec);
10441 arm_free_section (&extab_arm_sec);
10442
10443 return res;
10444 }
10445
10446 /* Used for both ARM and C6X unwinding tables. */
10447
10448 static bool
10449 arm_process_unwind (Filedata * filedata)
10450 {
10451 struct arm_unw_aux_info aux;
10452 Elf_Internal_Shdr *unwsec = NULL;
10453 Elf_Internal_Shdr *sec;
10454 unsigned long i;
10455 unsigned int sec_type;
10456 bool res = true;
10457
10458 switch (filedata->file_header.e_machine)
10459 {
10460 case EM_ARM:
10461 sec_type = SHT_ARM_EXIDX;
10462 break;
10463
10464 case EM_TI_C6000:
10465 sec_type = SHT_C6000_UNWIND;
10466 break;
10467
10468 default:
10469 error (_("Unsupported architecture type %d encountered when processing unwind table\n"),
10470 filedata->file_header.e_machine);
10471 return false;
10472 }
10473
10474 if (filedata->string_table == NULL)
10475 return false;
10476
10477 memset (& aux, 0, sizeof (aux));
10478 aux.filedata = filedata;
10479
10480 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
10481 {
10482 if (sec->sh_type == SHT_SYMTAB)
10483 {
10484 if (aux.symtab)
10485 {
10486 error (_("Multiple symbol tables encountered\n"));
10487 free (aux.symtab);
10488 aux.symtab = NULL;
10489 free (aux.strtab);
10490 aux.strtab = NULL;
10491 }
10492 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
10493 &aux.strtab, &aux.strtab_size))
10494 return false;
10495 }
10496 else if (sec->sh_type == sec_type)
10497 unwsec = sec;
10498 }
10499
10500 if (unwsec == NULL)
10501 printf (_("\nThere are no unwind sections in this file.\n"));
10502 else
10503 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
10504 {
10505 if (sec->sh_type == sec_type)
10506 {
10507 unsigned long num_unwind = sec->sh_size / (2 * eh_addr_size);
10508 printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
10509 "contains %lu entry:\n",
10510 "\nUnwind section '%s' at offset 0x%lx "
10511 "contains %lu entries:\n",
10512 num_unwind),
10513 printable_section_name (filedata, sec),
10514 (unsigned long) sec->sh_offset,
10515 num_unwind);
10516
10517 if (! dump_arm_unwind (filedata, &aux, sec))
10518 res = false;
10519 }
10520 }
10521
10522 free (aux.symtab);
10523 free ((char *) aux.strtab);
10524
10525 return res;
10526 }
10527
10528 static bool
10529 no_processor_specific_unwind (Filedata * filedata ATTRIBUTE_UNUSED)
10530 {
10531 printf (_("No processor specific unwind information to decode\n"));
10532 return true;
10533 }
10534
10535 static bool
10536 process_unwind (Filedata * filedata)
10537 {
10538 struct unwind_handler
10539 {
10540 unsigned int machtype;
10541 bool (* handler)(Filedata *);
10542 } handlers[] =
10543 {
10544 { EM_ARM, arm_process_unwind },
10545 { EM_IA_64, ia64_process_unwind },
10546 { EM_PARISC, hppa_process_unwind },
10547 { EM_TI_C6000, arm_process_unwind },
10548 { EM_386, no_processor_specific_unwind },
10549 { EM_X86_64, no_processor_specific_unwind },
10550 { 0, NULL }
10551 };
10552 int i;
10553
10554 if (!do_unwind)
10555 return true;
10556
10557 for (i = 0; handlers[i].handler != NULL; i++)
10558 if (filedata->file_header.e_machine == handlers[i].machtype)
10559 return handlers[i].handler (filedata);
10560
10561 printf (_("\nThe decoding of unwind sections for machine type %s is not currently supported.\n"),
10562 get_machine_name (filedata->file_header.e_machine));
10563 return true;
10564 }
10565
10566 static void
10567 dynamic_section_aarch64_val (Elf_Internal_Dyn * entry)
10568 {
10569 switch (entry->d_tag)
10570 {
10571 case DT_AARCH64_BTI_PLT:
10572 case DT_AARCH64_PAC_PLT:
10573 break;
10574 default:
10575 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10576 break;
10577 }
10578 putchar ('\n');
10579 }
10580
10581 static void
10582 dynamic_section_mips_val (Filedata * filedata, Elf_Internal_Dyn * entry)
10583 {
10584 switch (entry->d_tag)
10585 {
10586 case DT_MIPS_FLAGS:
10587 if (entry->d_un.d_val == 0)
10588 printf (_("NONE"));
10589 else
10590 {
10591 static const char * opts[] =
10592 {
10593 "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
10594 "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
10595 "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
10596 "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
10597 "RLD_ORDER_SAFE"
10598 };
10599 unsigned int cnt;
10600 bool first = true;
10601
10602 for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
10603 if (entry->d_un.d_val & (1 << cnt))
10604 {
10605 printf ("%s%s", first ? "" : " ", opts[cnt]);
10606 first = false;
10607 }
10608 }
10609 break;
10610
10611 case DT_MIPS_IVERSION:
10612 if (valid_dynamic_name (filedata, entry->d_un.d_val))
10613 printf (_("Interface Version: %s"),
10614 get_dynamic_name (filedata, entry->d_un.d_val));
10615 else
10616 printf (_("Interface Version: <corrupt: %" PRIx64 ">"),
10617 entry->d_un.d_ptr);
10618 break;
10619
10620 case DT_MIPS_TIME_STAMP:
10621 {
10622 char timebuf[128];
10623 struct tm * tmp;
10624 time_t atime = entry->d_un.d_val;
10625
10626 tmp = gmtime (&atime);
10627 /* PR 17531: file: 6accc532. */
10628 if (tmp == NULL)
10629 snprintf (timebuf, sizeof (timebuf), _("<corrupt>"));
10630 else
10631 snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
10632 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
10633 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
10634 printf (_("Time Stamp: %s"), timebuf);
10635 }
10636 break;
10637
10638 case DT_MIPS_RLD_VERSION:
10639 case DT_MIPS_LOCAL_GOTNO:
10640 case DT_MIPS_CONFLICTNO:
10641 case DT_MIPS_LIBLISTNO:
10642 case DT_MIPS_SYMTABNO:
10643 case DT_MIPS_UNREFEXTNO:
10644 case DT_MIPS_HIPAGENO:
10645 case DT_MIPS_DELTA_CLASS_NO:
10646 case DT_MIPS_DELTA_INSTANCE_NO:
10647 case DT_MIPS_DELTA_RELOC_NO:
10648 case DT_MIPS_DELTA_SYM_NO:
10649 case DT_MIPS_DELTA_CLASSSYM_NO:
10650 case DT_MIPS_COMPACT_SIZE:
10651 print_vma (entry->d_un.d_val, DEC);
10652 break;
10653
10654 case DT_MIPS_XHASH:
10655 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
10656 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10657 /* Falls through. */
10658
10659 default:
10660 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10661 }
10662 putchar ('\n');
10663 }
10664
10665 static void
10666 dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
10667 {
10668 switch (entry->d_tag)
10669 {
10670 case DT_HP_DLD_FLAGS:
10671 {
10672 static struct
10673 {
10674 long int bit;
10675 const char * str;
10676 }
10677 flags[] =
10678 {
10679 { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
10680 { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
10681 { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
10682 { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
10683 { DT_HP_BIND_NOW, "HP_BIND_NOW" },
10684 { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
10685 { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
10686 { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
10687 { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
10688 { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
10689 { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
10690 { DT_HP_GST, "HP_GST" },
10691 { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
10692 { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
10693 { DT_HP_NODELETE, "HP_NODELETE" },
10694 { DT_HP_GROUP, "HP_GROUP" },
10695 { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
10696 };
10697 bool first = true;
10698 size_t cnt;
10699 uint64_t val = entry->d_un.d_val;
10700
10701 for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
10702 if (val & flags[cnt].bit)
10703 {
10704 if (! first)
10705 putchar (' ');
10706 fputs (flags[cnt].str, stdout);
10707 first = false;
10708 val ^= flags[cnt].bit;
10709 }
10710
10711 if (val != 0 || first)
10712 {
10713 if (! first)
10714 putchar (' ');
10715 print_vma (val, HEX);
10716 }
10717 }
10718 break;
10719
10720 default:
10721 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10722 break;
10723 }
10724 putchar ('\n');
10725 }
10726
10727 /* VMS vs Unix time offset and factor. */
10728
10729 #define VMS_EPOCH_OFFSET 35067168000000000LL
10730 #define VMS_GRANULARITY_FACTOR 10000000
10731 #ifndef INT64_MIN
10732 #define INT64_MIN (-9223372036854775807LL - 1)
10733 #endif
10734
10735 /* Display a VMS time in a human readable format. */
10736
10737 static void
10738 print_vms_time (int64_t vmstime)
10739 {
10740 struct tm *tm = NULL;
10741 time_t unxtime;
10742
10743 if (vmstime >= INT64_MIN + VMS_EPOCH_OFFSET)
10744 {
10745 vmstime = (vmstime - VMS_EPOCH_OFFSET) / VMS_GRANULARITY_FACTOR;
10746 unxtime = vmstime;
10747 if (unxtime == vmstime)
10748 tm = gmtime (&unxtime);
10749 }
10750 if (tm != NULL)
10751 printf ("%04u-%02u-%02uT%02u:%02u:%02u",
10752 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
10753 tm->tm_hour, tm->tm_min, tm->tm_sec);
10754 }
10755
10756 static void
10757 dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
10758 {
10759 switch (entry->d_tag)
10760 {
10761 case DT_IA_64_PLT_RESERVE:
10762 /* First 3 slots reserved. */
10763 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10764 printf (" -- ");
10765 print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
10766 break;
10767
10768 case DT_IA_64_VMS_LINKTIME:
10769 print_vms_time (entry->d_un.d_val);
10770 break;
10771
10772 case DT_IA_64_VMS_LNKFLAGS:
10773 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10774 if (entry->d_un.d_val & VMS_LF_CALL_DEBUG)
10775 printf (" CALL_DEBUG");
10776 if (entry->d_un.d_val & VMS_LF_NOP0BUFS)
10777 printf (" NOP0BUFS");
10778 if (entry->d_un.d_val & VMS_LF_P0IMAGE)
10779 printf (" P0IMAGE");
10780 if (entry->d_un.d_val & VMS_LF_MKTHREADS)
10781 printf (" MKTHREADS");
10782 if (entry->d_un.d_val & VMS_LF_UPCALLS)
10783 printf (" UPCALLS");
10784 if (entry->d_un.d_val & VMS_LF_IMGSTA)
10785 printf (" IMGSTA");
10786 if (entry->d_un.d_val & VMS_LF_INITIALIZE)
10787 printf (" INITIALIZE");
10788 if (entry->d_un.d_val & VMS_LF_MAIN)
10789 printf (" MAIN");
10790 if (entry->d_un.d_val & VMS_LF_EXE_INIT)
10791 printf (" EXE_INIT");
10792 if (entry->d_un.d_val & VMS_LF_TBK_IN_IMG)
10793 printf (" TBK_IN_IMG");
10794 if (entry->d_un.d_val & VMS_LF_DBG_IN_IMG)
10795 printf (" DBG_IN_IMG");
10796 if (entry->d_un.d_val & VMS_LF_TBK_IN_DSF)
10797 printf (" TBK_IN_DSF");
10798 if (entry->d_un.d_val & VMS_LF_DBG_IN_DSF)
10799 printf (" DBG_IN_DSF");
10800 if (entry->d_un.d_val & VMS_LF_SIGNATURES)
10801 printf (" SIGNATURES");
10802 if (entry->d_un.d_val & VMS_LF_REL_SEG_OFF)
10803 printf (" REL_SEG_OFF");
10804 break;
10805
10806 default:
10807 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10808 break;
10809 }
10810 putchar ('\n');
10811 }
10812
10813 static bool
10814 get_32bit_dynamic_section (Filedata * filedata)
10815 {
10816 Elf32_External_Dyn * edyn;
10817 Elf32_External_Dyn * ext;
10818 Elf_Internal_Dyn * entry;
10819
10820 edyn = (Elf32_External_Dyn *) get_data (NULL, filedata,
10821 filedata->dynamic_addr, 1,
10822 filedata->dynamic_size,
10823 _("dynamic section"));
10824 if (!edyn)
10825 return false;
10826
10827 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
10828 might not have the luxury of section headers. Look for the DT_NULL
10829 terminator to determine the number of entries. */
10830 for (ext = edyn, filedata->dynamic_nent = 0;
10831 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
10832 ext++)
10833 {
10834 filedata->dynamic_nent++;
10835 if (BYTE_GET (ext->d_tag) == DT_NULL)
10836 break;
10837 }
10838
10839 filedata->dynamic_section
10840 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
10841 if (filedata->dynamic_section == NULL)
10842 {
10843 error (_("Out of memory allocating space for %lu dynamic entries\n"),
10844 (unsigned long) filedata->dynamic_nent);
10845 free (edyn);
10846 return false;
10847 }
10848
10849 for (ext = edyn, entry = filedata->dynamic_section;
10850 entry < filedata->dynamic_section + filedata->dynamic_nent;
10851 ext++, entry++)
10852 {
10853 entry->d_tag = BYTE_GET (ext->d_tag);
10854 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
10855 }
10856
10857 free (edyn);
10858
10859 return true;
10860 }
10861
10862 static bool
10863 get_64bit_dynamic_section (Filedata * filedata)
10864 {
10865 Elf64_External_Dyn * edyn;
10866 Elf64_External_Dyn * ext;
10867 Elf_Internal_Dyn * entry;
10868
10869 /* Read in the data. */
10870 edyn = (Elf64_External_Dyn *) get_data (NULL, filedata,
10871 filedata->dynamic_addr, 1,
10872 filedata->dynamic_size,
10873 _("dynamic section"));
10874 if (!edyn)
10875 return false;
10876
10877 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
10878 might not have the luxury of section headers. Look for the DT_NULL
10879 terminator to determine the number of entries. */
10880 for (ext = edyn, filedata->dynamic_nent = 0;
10881 /* PR 17533 file: 033-67080-0.004 - do not read past end of buffer. */
10882 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
10883 ext++)
10884 {
10885 filedata->dynamic_nent++;
10886 if (BYTE_GET (ext->d_tag) == DT_NULL)
10887 break;
10888 }
10889
10890 filedata->dynamic_section
10891 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
10892 if (filedata->dynamic_section == NULL)
10893 {
10894 error (_("Out of memory allocating space for %lu dynamic entries\n"),
10895 (unsigned long) filedata->dynamic_nent);
10896 free (edyn);
10897 return false;
10898 }
10899
10900 /* Convert from external to internal formats. */
10901 for (ext = edyn, entry = filedata->dynamic_section;
10902 entry < filedata->dynamic_section + filedata->dynamic_nent;
10903 ext++, entry++)
10904 {
10905 entry->d_tag = BYTE_GET (ext->d_tag);
10906 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
10907 }
10908
10909 free (edyn);
10910
10911 return true;
10912 }
10913
10914 static bool
10915 get_dynamic_section (Filedata *filedata)
10916 {
10917 if (filedata->dynamic_section)
10918 return true;
10919
10920 if (is_32bit_elf)
10921 return get_32bit_dynamic_section (filedata);
10922 else
10923 return get_64bit_dynamic_section (filedata);
10924 }
10925
10926 static void
10927 print_dynamic_flags (uint64_t flags)
10928 {
10929 bool first = true;
10930
10931 while (flags)
10932 {
10933 uint64_t flag;
10934
10935 flag = flags & - flags;
10936 flags &= ~ flag;
10937
10938 if (first)
10939 first = false;
10940 else
10941 putc (' ', stdout);
10942
10943 switch (flag)
10944 {
10945 case DF_ORIGIN: fputs ("ORIGIN", stdout); break;
10946 case DF_SYMBOLIC: fputs ("SYMBOLIC", stdout); break;
10947 case DF_TEXTREL: fputs ("TEXTREL", stdout); break;
10948 case DF_BIND_NOW: fputs ("BIND_NOW", stdout); break;
10949 case DF_STATIC_TLS: fputs ("STATIC_TLS", stdout); break;
10950 default: fputs (_("unknown"), stdout); break;
10951 }
10952 }
10953 puts ("");
10954 }
10955
10956 static uint64_t *
10957 get_dynamic_data (Filedata * filedata, uint64_t number, unsigned int ent_size)
10958 {
10959 unsigned char * e_data;
10960 uint64_t * i_data;
10961
10962 /* If size_t is smaller than uint64_t, eg because you are building
10963 on a 32-bit host, then make sure that when number is cast to
10964 size_t no information is lost. */
10965 if ((size_t) number != number
10966 || ent_size * number / ent_size != number)
10967 {
10968 error (_("Size overflow prevents reading %" PRIu64
10969 " elements of size %u\n"),
10970 number, ent_size);
10971 return NULL;
10972 }
10973
10974 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
10975 attempting to allocate memory when the read is bound to fail. */
10976 if (ent_size * number > filedata->file_size)
10977 {
10978 error (_("Invalid number of dynamic entries: %" PRIu64 "\n"),
10979 number);
10980 return NULL;
10981 }
10982
10983 e_data = (unsigned char *) cmalloc ((size_t) number, ent_size);
10984 if (e_data == NULL)
10985 {
10986 error (_("Out of memory reading %" PRIu64 " dynamic entries\n"),
10987 number);
10988 return NULL;
10989 }
10990
10991 if (fread (e_data, ent_size, (size_t) number, filedata->handle) != number)
10992 {
10993 error (_("Unable to read in %" PRIu64 " bytes of dynamic data\n"),
10994 number * ent_size);
10995 free (e_data);
10996 return NULL;
10997 }
10998
10999 i_data = (uint64_t *) cmalloc ((size_t) number, sizeof (*i_data));
11000 if (i_data == NULL)
11001 {
11002 error (_("Out of memory allocating space for %" PRIu64 " dynamic entries\n"),
11003 number);
11004 free (e_data);
11005 return NULL;
11006 }
11007
11008 while (number--)
11009 i_data[number] = byte_get (e_data + number * ent_size, ent_size);
11010
11011 free (e_data);
11012
11013 return i_data;
11014 }
11015
11016 static unsigned long
11017 get_num_dynamic_syms (Filedata * filedata)
11018 {
11019 unsigned long num_of_syms = 0;
11020
11021 if (!do_histogram && (!do_using_dynamic || do_dyn_syms))
11022 return num_of_syms;
11023
11024 if (filedata->dynamic_info[DT_HASH])
11025 {
11026 unsigned char nb[8];
11027 unsigned char nc[8];
11028 unsigned int hash_ent_size = 4;
11029
11030 if ((filedata->file_header.e_machine == EM_ALPHA
11031 || filedata->file_header.e_machine == EM_S390
11032 || filedata->file_header.e_machine == EM_S390_OLD)
11033 && filedata->file_header.e_ident[EI_CLASS] == ELFCLASS64)
11034 hash_ent_size = 8;
11035
11036 if (fseek (filedata->handle,
11037 (filedata->archive_file_offset
11038 + offset_from_vma (filedata, filedata->dynamic_info[DT_HASH],
11039 sizeof nb + sizeof nc)),
11040 SEEK_SET))
11041 {
11042 error (_("Unable to seek to start of dynamic information\n"));
11043 goto no_hash;
11044 }
11045
11046 if (fread (nb, hash_ent_size, 1, filedata->handle) != 1)
11047 {
11048 error (_("Failed to read in number of buckets\n"));
11049 goto no_hash;
11050 }
11051
11052 if (fread (nc, hash_ent_size, 1, filedata->handle) != 1)
11053 {
11054 error (_("Failed to read in number of chains\n"));
11055 goto no_hash;
11056 }
11057
11058 filedata->nbuckets = byte_get (nb, hash_ent_size);
11059 filedata->nchains = byte_get (nc, hash_ent_size);
11060
11061 if (filedata->nbuckets != 0 && filedata->nchains != 0)
11062 {
11063 filedata->buckets = get_dynamic_data (filedata, filedata->nbuckets,
11064 hash_ent_size);
11065 filedata->chains = get_dynamic_data (filedata, filedata->nchains,
11066 hash_ent_size);
11067
11068 if (filedata->buckets != NULL && filedata->chains != NULL)
11069 num_of_syms = filedata->nchains;
11070 }
11071 no_hash:
11072 if (num_of_syms == 0)
11073 {
11074 free (filedata->buckets);
11075 filedata->buckets = NULL;
11076 free (filedata->chains);
11077 filedata->chains = NULL;
11078 filedata->nbuckets = 0;
11079 }
11080 }
11081
11082 if (filedata->dynamic_info_DT_GNU_HASH)
11083 {
11084 unsigned char nb[16];
11085 uint64_t i, maxchain = 0xffffffff, bitmaskwords;
11086 uint64_t buckets_vma;
11087 unsigned long hn;
11088
11089 if (fseek (filedata->handle,
11090 (filedata->archive_file_offset
11091 + offset_from_vma (filedata,
11092 filedata->dynamic_info_DT_GNU_HASH,
11093 sizeof nb)),
11094 SEEK_SET))
11095 {
11096 error (_("Unable to seek to start of dynamic information\n"));
11097 goto no_gnu_hash;
11098 }
11099
11100 if (fread (nb, 16, 1, filedata->handle) != 1)
11101 {
11102 error (_("Failed to read in number of buckets\n"));
11103 goto no_gnu_hash;
11104 }
11105
11106 filedata->ngnubuckets = byte_get (nb, 4);
11107 filedata->gnusymidx = byte_get (nb + 4, 4);
11108 bitmaskwords = byte_get (nb + 8, 4);
11109 buckets_vma = filedata->dynamic_info_DT_GNU_HASH + 16;
11110 if (is_32bit_elf)
11111 buckets_vma += bitmaskwords * 4;
11112 else
11113 buckets_vma += bitmaskwords * 8;
11114
11115 if (fseek (filedata->handle,
11116 (filedata->archive_file_offset
11117 + offset_from_vma (filedata, buckets_vma, 4)),
11118 SEEK_SET))
11119 {
11120 error (_("Unable to seek to start of dynamic information\n"));
11121 goto no_gnu_hash;
11122 }
11123
11124 filedata->gnubuckets
11125 = get_dynamic_data (filedata, filedata->ngnubuckets, 4);
11126
11127 if (filedata->gnubuckets == NULL)
11128 goto no_gnu_hash;
11129
11130 for (i = 0; i < filedata->ngnubuckets; i++)
11131 if (filedata->gnubuckets[i] != 0)
11132 {
11133 if (filedata->gnubuckets[i] < filedata->gnusymidx)
11134 goto no_gnu_hash;
11135
11136 if (maxchain == 0xffffffff || filedata->gnubuckets[i] > maxchain)
11137 maxchain = filedata->gnubuckets[i];
11138 }
11139
11140 if (maxchain == 0xffffffff)
11141 goto no_gnu_hash;
11142
11143 maxchain -= filedata->gnusymidx;
11144
11145 if (fseek (filedata->handle,
11146 (filedata->archive_file_offset
11147 + offset_from_vma (filedata,
11148 buckets_vma + 4 * (filedata->ngnubuckets
11149 + maxchain),
11150 4)),
11151 SEEK_SET))
11152 {
11153 error (_("Unable to seek to start of dynamic information\n"));
11154 goto no_gnu_hash;
11155 }
11156
11157 do
11158 {
11159 if (fread (nb, 4, 1, filedata->handle) != 1)
11160 {
11161 error (_("Failed to determine last chain length\n"));
11162 goto no_gnu_hash;
11163 }
11164
11165 if (maxchain + 1 == 0)
11166 goto no_gnu_hash;
11167
11168 ++maxchain;
11169 }
11170 while ((byte_get (nb, 4) & 1) == 0);
11171
11172 if (fseek (filedata->handle,
11173 (filedata->archive_file_offset
11174 + offset_from_vma (filedata, (buckets_vma
11175 + 4 * filedata->ngnubuckets),
11176 4)),
11177 SEEK_SET))
11178 {
11179 error (_("Unable to seek to start of dynamic information\n"));
11180 goto no_gnu_hash;
11181 }
11182
11183 filedata->gnuchains = get_dynamic_data (filedata, maxchain, 4);
11184 filedata->ngnuchains = maxchain;
11185
11186 if (filedata->gnuchains == NULL)
11187 goto no_gnu_hash;
11188
11189 if (filedata->dynamic_info_DT_MIPS_XHASH)
11190 {
11191 if (fseek (filedata->handle,
11192 (filedata->archive_file_offset
11193 + offset_from_vma (filedata, (buckets_vma
11194 + 4 * (filedata->ngnubuckets
11195 + maxchain)), 4)),
11196 SEEK_SET))
11197 {
11198 error (_("Unable to seek to start of dynamic information\n"));
11199 goto no_gnu_hash;
11200 }
11201
11202 filedata->mipsxlat = get_dynamic_data (filedata, maxchain, 4);
11203 if (filedata->mipsxlat == NULL)
11204 goto no_gnu_hash;
11205 }
11206
11207 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
11208 if (filedata->gnubuckets[hn] != 0)
11209 {
11210 uint64_t si = filedata->gnubuckets[hn];
11211 uint64_t off = si - filedata->gnusymidx;
11212
11213 do
11214 {
11215 if (filedata->dynamic_info_DT_MIPS_XHASH)
11216 {
11217 if (off < filedata->ngnuchains
11218 && filedata->mipsxlat[off] >= num_of_syms)
11219 num_of_syms = filedata->mipsxlat[off] + 1;
11220 }
11221 else
11222 {
11223 if (si >= num_of_syms)
11224 num_of_syms = si + 1;
11225 }
11226 si++;
11227 }
11228 while (off < filedata->ngnuchains
11229 && (filedata->gnuchains[off++] & 1) == 0);
11230 }
11231
11232 if (num_of_syms == 0)
11233 {
11234 no_gnu_hash:
11235 free (filedata->mipsxlat);
11236 filedata->mipsxlat = NULL;
11237 free (filedata->gnuchains);
11238 filedata->gnuchains = NULL;
11239 free (filedata->gnubuckets);
11240 filedata->gnubuckets = NULL;
11241 filedata->ngnubuckets = 0;
11242 filedata->ngnuchains = 0;
11243 }
11244 }
11245
11246 return num_of_syms;
11247 }
11248
11249 /* Parse and display the contents of the dynamic section. */
11250
11251 static bool
11252 process_dynamic_section (Filedata * filedata)
11253 {
11254 Elf_Internal_Dyn * entry;
11255
11256 if (filedata->dynamic_size <= 1)
11257 {
11258 if (do_dynamic)
11259 {
11260 if (filedata->is_separate)
11261 printf (_("\nThere is no dynamic section in linked file '%s'.\n"),
11262 filedata->file_name);
11263 else
11264 printf (_("\nThere is no dynamic section in this file.\n"));
11265 }
11266
11267 return true;
11268 }
11269
11270 if (!get_dynamic_section (filedata))
11271 return false;
11272
11273 /* Find the appropriate symbol table. */
11274 if (filedata->dynamic_symbols == NULL || do_histogram)
11275 {
11276 unsigned long num_of_syms;
11277
11278 for (entry = filedata->dynamic_section;
11279 entry < filedata->dynamic_section + filedata->dynamic_nent;
11280 ++entry)
11281 if (entry->d_tag == DT_SYMTAB)
11282 filedata->dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
11283 else if (entry->d_tag == DT_SYMENT)
11284 filedata->dynamic_info[DT_SYMENT] = entry->d_un.d_val;
11285 else if (entry->d_tag == DT_HASH)
11286 filedata->dynamic_info[DT_HASH] = entry->d_un.d_val;
11287 else if (entry->d_tag == DT_GNU_HASH)
11288 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11289 else if ((filedata->file_header.e_machine == EM_MIPS
11290 || filedata->file_header.e_machine == EM_MIPS_RS3_LE)
11291 && entry->d_tag == DT_MIPS_XHASH)
11292 {
11293 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
11294 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11295 }
11296
11297 num_of_syms = get_num_dynamic_syms (filedata);
11298
11299 if (num_of_syms != 0
11300 && filedata->dynamic_symbols == NULL
11301 && filedata->dynamic_info[DT_SYMTAB]
11302 && filedata->dynamic_info[DT_SYMENT])
11303 {
11304 Elf_Internal_Phdr *seg;
11305 uint64_t vma = filedata->dynamic_info[DT_SYMTAB];
11306
11307 if (! get_program_headers (filedata))
11308 {
11309 error (_("Cannot interpret virtual addresses "
11310 "without program headers.\n"));
11311 return false;
11312 }
11313
11314 for (seg = filedata->program_headers;
11315 seg < filedata->program_headers + filedata->file_header.e_phnum;
11316 ++seg)
11317 {
11318 if (seg->p_type != PT_LOAD)
11319 continue;
11320
11321 if (seg->p_offset + seg->p_filesz > filedata->file_size)
11322 {
11323 /* See PR 21379 for a reproducer. */
11324 error (_("Invalid PT_LOAD entry\n"));
11325 return false;
11326 }
11327
11328 if (vma >= (seg->p_vaddr & -seg->p_align)
11329 && vma < seg->p_vaddr + seg->p_filesz)
11330 {
11331 /* Since we do not know how big the symbol table is,
11332 we default to reading in up to the end of PT_LOAD
11333 segment and processing that. This is overkill, I
11334 know, but it should work. */
11335 Elf_Internal_Shdr section;
11336 section.sh_offset = (vma - seg->p_vaddr
11337 + seg->p_offset);
11338 section.sh_size = (num_of_syms
11339 * filedata->dynamic_info[DT_SYMENT]);
11340 section.sh_entsize = filedata->dynamic_info[DT_SYMENT];
11341
11342 if (do_checks
11343 && filedata->dynamic_symtab_section != NULL
11344 && ((filedata->dynamic_symtab_section->sh_offset
11345 != section.sh_offset)
11346 || (filedata->dynamic_symtab_section->sh_size
11347 != section.sh_size)
11348 || (filedata->dynamic_symtab_section->sh_entsize
11349 != section.sh_entsize)))
11350 warn (_("\
11351 the .dynsym section doesn't match the DT_SYMTAB and DT_SYMENT tags\n"));
11352
11353 section.sh_name = filedata->string_table_length;
11354 filedata->dynamic_symbols
11355 = get_elf_symbols (filedata, &section,
11356 &filedata->num_dynamic_syms);
11357 if (filedata->dynamic_symbols == NULL
11358 || filedata->num_dynamic_syms != num_of_syms)
11359 {
11360 error (_("Corrupt DT_SYMTAB dynamic entry\n"));
11361 return false;
11362 }
11363 break;
11364 }
11365 }
11366 }
11367 }
11368
11369 /* Similarly find a string table. */
11370 if (filedata->dynamic_strings == NULL)
11371 for (entry = filedata->dynamic_section;
11372 entry < filedata->dynamic_section + filedata->dynamic_nent;
11373 ++entry)
11374 {
11375 if (entry->d_tag == DT_STRTAB)
11376 filedata->dynamic_info[DT_STRTAB] = entry->d_un.d_val;
11377
11378 if (entry->d_tag == DT_STRSZ)
11379 filedata->dynamic_info[DT_STRSZ] = entry->d_un.d_val;
11380
11381 if (filedata->dynamic_info[DT_STRTAB]
11382 && filedata->dynamic_info[DT_STRSZ])
11383 {
11384 unsigned long offset;
11385 uint64_t str_tab_len = filedata->dynamic_info[DT_STRSZ];
11386
11387 offset = offset_from_vma (filedata,
11388 filedata->dynamic_info[DT_STRTAB],
11389 str_tab_len);
11390 if (do_checks
11391 && filedata->dynamic_strtab_section
11392 && ((filedata->dynamic_strtab_section->sh_offset
11393 != (file_ptr) offset)
11394 || (filedata->dynamic_strtab_section->sh_size
11395 != str_tab_len)))
11396 warn (_("\
11397 the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
11398
11399 filedata->dynamic_strings
11400 = (char *) get_data (NULL, filedata, offset, 1, str_tab_len,
11401 _("dynamic string table"));
11402 if (filedata->dynamic_strings == NULL)
11403 {
11404 error (_("Corrupt DT_STRTAB dynamic entry\n"));
11405 break;
11406 }
11407
11408 filedata->dynamic_strings_length = str_tab_len;
11409 break;
11410 }
11411 }
11412
11413 /* And find the syminfo section if available. */
11414 if (filedata->dynamic_syminfo == NULL)
11415 {
11416 unsigned long syminsz = 0;
11417
11418 for (entry = filedata->dynamic_section;
11419 entry < filedata->dynamic_section + filedata->dynamic_nent;
11420 ++entry)
11421 {
11422 if (entry->d_tag == DT_SYMINENT)
11423 {
11424 /* Note: these braces are necessary to avoid a syntax
11425 error from the SunOS4 C compiler. */
11426 /* PR binutils/17531: A corrupt file can trigger this test.
11427 So do not use an assert, instead generate an error message. */
11428 if (sizeof (Elf_External_Syminfo) != entry->d_un.d_val)
11429 error (_("Bad value (%d) for SYMINENT entry\n"),
11430 (int) entry->d_un.d_val);
11431 }
11432 else if (entry->d_tag == DT_SYMINSZ)
11433 syminsz = entry->d_un.d_val;
11434 else if (entry->d_tag == DT_SYMINFO)
11435 filedata->dynamic_syminfo_offset
11436 = offset_from_vma (filedata, entry->d_un.d_val, syminsz);
11437 }
11438
11439 if (filedata->dynamic_syminfo_offset != 0 && syminsz != 0)
11440 {
11441 Elf_External_Syminfo * extsyminfo;
11442 Elf_External_Syminfo * extsym;
11443 Elf_Internal_Syminfo * syminfo;
11444
11445 /* There is a syminfo section. Read the data. */
11446 extsyminfo = (Elf_External_Syminfo *)
11447 get_data (NULL, filedata, filedata->dynamic_syminfo_offset,
11448 1, syminsz, _("symbol information"));
11449 if (!extsyminfo)
11450 return false;
11451
11452 if (filedata->dynamic_syminfo != NULL)
11453 {
11454 error (_("Multiple dynamic symbol information sections found\n"));
11455 free (filedata->dynamic_syminfo);
11456 }
11457 filedata->dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
11458 if (filedata->dynamic_syminfo == NULL)
11459 {
11460 error (_("Out of memory allocating %lu bytes "
11461 "for dynamic symbol info\n"),
11462 (unsigned long) syminsz);
11463 return false;
11464 }
11465
11466 filedata->dynamic_syminfo_nent
11467 = syminsz / sizeof (Elf_External_Syminfo);
11468 for (syminfo = filedata->dynamic_syminfo, extsym = extsyminfo;
11469 syminfo < (filedata->dynamic_syminfo
11470 + filedata->dynamic_syminfo_nent);
11471 ++syminfo, ++extsym)
11472 {
11473 syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
11474 syminfo->si_flags = BYTE_GET (extsym->si_flags);
11475 }
11476
11477 free (extsyminfo);
11478 }
11479 }
11480
11481 if (do_dynamic && filedata->dynamic_addr)
11482 {
11483 if (filedata->is_separate)
11484 printf (ngettext ("\nIn linked file '%s' the dynamic section at offset 0x%lx contains %lu entry:\n",
11485 "\nIn linked file '%s' the dynamic section at offset 0x%lx contains %lu entries:\n",
11486 (unsigned long) filedata->dynamic_nent),
11487 filedata->file_name,
11488 filedata->dynamic_addr,
11489 (unsigned long) filedata->dynamic_nent);
11490 else
11491 printf (ngettext ("\nDynamic section at offset 0x%lx contains %lu entry:\n",
11492 "\nDynamic section at offset 0x%lx contains %lu entries:\n",
11493 (unsigned long) filedata->dynamic_nent),
11494 filedata->dynamic_addr,
11495 (unsigned long) filedata->dynamic_nent);
11496 }
11497 if (do_dynamic)
11498 printf (_(" Tag Type Name/Value\n"));
11499
11500 for (entry = filedata->dynamic_section;
11501 entry < filedata->dynamic_section + filedata->dynamic_nent;
11502 entry++)
11503 {
11504 if (do_dynamic)
11505 {
11506 const char * dtype;
11507
11508 putchar (' ');
11509 print_vma (entry->d_tag, FULL_HEX);
11510 dtype = get_dynamic_type (filedata, entry->d_tag);
11511 printf (" (%s)%*s", dtype,
11512 ((is_32bit_elf ? 27 : 19) - (int) strlen (dtype)), " ");
11513 }
11514
11515 switch (entry->d_tag)
11516 {
11517 case DT_FLAGS:
11518 if (do_dynamic)
11519 print_dynamic_flags (entry->d_un.d_val);
11520 break;
11521
11522 case DT_AUXILIARY:
11523 case DT_FILTER:
11524 case DT_CONFIG:
11525 case DT_DEPAUDIT:
11526 case DT_AUDIT:
11527 if (do_dynamic)
11528 {
11529 switch (entry->d_tag)
11530 {
11531 case DT_AUXILIARY:
11532 printf (_("Auxiliary library"));
11533 break;
11534
11535 case DT_FILTER:
11536 printf (_("Filter library"));
11537 break;
11538
11539 case DT_CONFIG:
11540 printf (_("Configuration file"));
11541 break;
11542
11543 case DT_DEPAUDIT:
11544 printf (_("Dependency audit library"));
11545 break;
11546
11547 case DT_AUDIT:
11548 printf (_("Audit library"));
11549 break;
11550 }
11551
11552 if (valid_dynamic_name (filedata, entry->d_un.d_val))
11553 printf (": [%s]\n",
11554 get_dynamic_name (filedata, entry->d_un.d_val));
11555 else
11556 {
11557 printf (": ");
11558 print_vma (entry->d_un.d_val, PREFIX_HEX);
11559 putchar ('\n');
11560 }
11561 }
11562 break;
11563
11564 case DT_FEATURE:
11565 if (do_dynamic)
11566 {
11567 printf (_("Flags:"));
11568
11569 if (entry->d_un.d_val == 0)
11570 printf (_(" None\n"));
11571 else
11572 {
11573 unsigned long int val = entry->d_un.d_val;
11574
11575 if (val & DTF_1_PARINIT)
11576 {
11577 printf (" PARINIT");
11578 val ^= DTF_1_PARINIT;
11579 }
11580 if (val & DTF_1_CONFEXP)
11581 {
11582 printf (" CONFEXP");
11583 val ^= DTF_1_CONFEXP;
11584 }
11585 if (val != 0)
11586 printf (" %lx", val);
11587 puts ("");
11588 }
11589 }
11590 break;
11591
11592 case DT_POSFLAG_1:
11593 if (do_dynamic)
11594 {
11595 printf (_("Flags:"));
11596
11597 if (entry->d_un.d_val == 0)
11598 printf (_(" None\n"));
11599 else
11600 {
11601 unsigned long int val = entry->d_un.d_val;
11602
11603 if (val & DF_P1_LAZYLOAD)
11604 {
11605 printf (" LAZYLOAD");
11606 val ^= DF_P1_LAZYLOAD;
11607 }
11608 if (val & DF_P1_GROUPPERM)
11609 {
11610 printf (" GROUPPERM");
11611 val ^= DF_P1_GROUPPERM;
11612 }
11613 if (val != 0)
11614 printf (" %lx", val);
11615 puts ("");
11616 }
11617 }
11618 break;
11619
11620 case DT_FLAGS_1:
11621 if (do_dynamic)
11622 {
11623 printf (_("Flags:"));
11624 if (entry->d_un.d_val == 0)
11625 printf (_(" None\n"));
11626 else
11627 {
11628 unsigned long int val = entry->d_un.d_val;
11629
11630 if (val & DF_1_NOW)
11631 {
11632 printf (" NOW");
11633 val ^= DF_1_NOW;
11634 }
11635 if (val & DF_1_GLOBAL)
11636 {
11637 printf (" GLOBAL");
11638 val ^= DF_1_GLOBAL;
11639 }
11640 if (val & DF_1_GROUP)
11641 {
11642 printf (" GROUP");
11643 val ^= DF_1_GROUP;
11644 }
11645 if (val & DF_1_NODELETE)
11646 {
11647 printf (" NODELETE");
11648 val ^= DF_1_NODELETE;
11649 }
11650 if (val & DF_1_LOADFLTR)
11651 {
11652 printf (" LOADFLTR");
11653 val ^= DF_1_LOADFLTR;
11654 }
11655 if (val & DF_1_INITFIRST)
11656 {
11657 printf (" INITFIRST");
11658 val ^= DF_1_INITFIRST;
11659 }
11660 if (val & DF_1_NOOPEN)
11661 {
11662 printf (" NOOPEN");
11663 val ^= DF_1_NOOPEN;
11664 }
11665 if (val & DF_1_ORIGIN)
11666 {
11667 printf (" ORIGIN");
11668 val ^= DF_1_ORIGIN;
11669 }
11670 if (val & DF_1_DIRECT)
11671 {
11672 printf (" DIRECT");
11673 val ^= DF_1_DIRECT;
11674 }
11675 if (val & DF_1_TRANS)
11676 {
11677 printf (" TRANS");
11678 val ^= DF_1_TRANS;
11679 }
11680 if (val & DF_1_INTERPOSE)
11681 {
11682 printf (" INTERPOSE");
11683 val ^= DF_1_INTERPOSE;
11684 }
11685 if (val & DF_1_NODEFLIB)
11686 {
11687 printf (" NODEFLIB");
11688 val ^= DF_1_NODEFLIB;
11689 }
11690 if (val & DF_1_NODUMP)
11691 {
11692 printf (" NODUMP");
11693 val ^= DF_1_NODUMP;
11694 }
11695 if (val & DF_1_CONFALT)
11696 {
11697 printf (" CONFALT");
11698 val ^= DF_1_CONFALT;
11699 }
11700 if (val & DF_1_ENDFILTEE)
11701 {
11702 printf (" ENDFILTEE");
11703 val ^= DF_1_ENDFILTEE;
11704 }
11705 if (val & DF_1_DISPRELDNE)
11706 {
11707 printf (" DISPRELDNE");
11708 val ^= DF_1_DISPRELDNE;
11709 }
11710 if (val & DF_1_DISPRELPND)
11711 {
11712 printf (" DISPRELPND");
11713 val ^= DF_1_DISPRELPND;
11714 }
11715 if (val & DF_1_NODIRECT)
11716 {
11717 printf (" NODIRECT");
11718 val ^= DF_1_NODIRECT;
11719 }
11720 if (val & DF_1_IGNMULDEF)
11721 {
11722 printf (" IGNMULDEF");
11723 val ^= DF_1_IGNMULDEF;
11724 }
11725 if (val & DF_1_NOKSYMS)
11726 {
11727 printf (" NOKSYMS");
11728 val ^= DF_1_NOKSYMS;
11729 }
11730 if (val & DF_1_NOHDR)
11731 {
11732 printf (" NOHDR");
11733 val ^= DF_1_NOHDR;
11734 }
11735 if (val & DF_1_EDITED)
11736 {
11737 printf (" EDITED");
11738 val ^= DF_1_EDITED;
11739 }
11740 if (val & DF_1_NORELOC)
11741 {
11742 printf (" NORELOC");
11743 val ^= DF_1_NORELOC;
11744 }
11745 if (val & DF_1_SYMINTPOSE)
11746 {
11747 printf (" SYMINTPOSE");
11748 val ^= DF_1_SYMINTPOSE;
11749 }
11750 if (val & DF_1_GLOBAUDIT)
11751 {
11752 printf (" GLOBAUDIT");
11753 val ^= DF_1_GLOBAUDIT;
11754 }
11755 if (val & DF_1_SINGLETON)
11756 {
11757 printf (" SINGLETON");
11758 val ^= DF_1_SINGLETON;
11759 }
11760 if (val & DF_1_STUB)
11761 {
11762 printf (" STUB");
11763 val ^= DF_1_STUB;
11764 }
11765 if (val & DF_1_PIE)
11766 {
11767 printf (" PIE");
11768 val ^= DF_1_PIE;
11769 }
11770 if (val & DF_1_KMOD)
11771 {
11772 printf (" KMOD");
11773 val ^= DF_1_KMOD;
11774 }
11775 if (val & DF_1_WEAKFILTER)
11776 {
11777 printf (" WEAKFILTER");
11778 val ^= DF_1_WEAKFILTER;
11779 }
11780 if (val & DF_1_NOCOMMON)
11781 {
11782 printf (" NOCOMMON");
11783 val ^= DF_1_NOCOMMON;
11784 }
11785 if (val != 0)
11786 printf (" %lx", val);
11787 puts ("");
11788 }
11789 }
11790 break;
11791
11792 case DT_PLTREL:
11793 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11794 if (do_dynamic)
11795 puts (get_dynamic_type (filedata, entry->d_un.d_val));
11796 break;
11797
11798 case DT_NULL :
11799 case DT_NEEDED :
11800 case DT_PLTGOT :
11801 case DT_HASH :
11802 case DT_STRTAB :
11803 case DT_SYMTAB :
11804 case DT_RELA :
11805 case DT_INIT :
11806 case DT_FINI :
11807 case DT_SONAME :
11808 case DT_RPATH :
11809 case DT_SYMBOLIC:
11810 case DT_REL :
11811 case DT_RELR :
11812 case DT_DEBUG :
11813 case DT_TEXTREL :
11814 case DT_JMPREL :
11815 case DT_RUNPATH :
11816 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11817
11818 if (do_dynamic)
11819 {
11820 const char *name;
11821
11822 if (valid_dynamic_name (filedata, entry->d_un.d_val))
11823 name = get_dynamic_name (filedata, entry->d_un.d_val);
11824 else
11825 name = NULL;
11826
11827 if (name)
11828 {
11829 switch (entry->d_tag)
11830 {
11831 case DT_NEEDED:
11832 printf (_("Shared library: [%s]"), name);
11833
11834 if (filedata->program_interpreter
11835 && streq (name, filedata->program_interpreter))
11836 printf (_(" program interpreter"));
11837 break;
11838
11839 case DT_SONAME:
11840 printf (_("Library soname: [%s]"), name);
11841 break;
11842
11843 case DT_RPATH:
11844 printf (_("Library rpath: [%s]"), name);
11845 break;
11846
11847 case DT_RUNPATH:
11848 printf (_("Library runpath: [%s]"), name);
11849 break;
11850
11851 default:
11852 print_vma (entry->d_un.d_val, PREFIX_HEX);
11853 break;
11854 }
11855 }
11856 else
11857 print_vma (entry->d_un.d_val, PREFIX_HEX);
11858
11859 putchar ('\n');
11860 }
11861 break;
11862
11863 case DT_PLTRELSZ:
11864 case DT_RELASZ :
11865 case DT_STRSZ :
11866 case DT_RELSZ :
11867 case DT_RELAENT :
11868 case DT_RELRENT :
11869 case DT_RELRSZ :
11870 case DT_SYMENT :
11871 case DT_RELENT :
11872 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11873 /* Fall through. */
11874 case DT_PLTPADSZ:
11875 case DT_MOVEENT :
11876 case DT_MOVESZ :
11877 case DT_PREINIT_ARRAYSZ:
11878 case DT_INIT_ARRAYSZ:
11879 case DT_FINI_ARRAYSZ:
11880 case DT_GNU_CONFLICTSZ:
11881 case DT_GNU_LIBLISTSZ:
11882 if (do_dynamic)
11883 {
11884 print_vma (entry->d_un.d_val, UNSIGNED);
11885 printf (_(" (bytes)\n"));
11886 }
11887 break;
11888
11889 case DT_VERDEFNUM:
11890 case DT_VERNEEDNUM:
11891 case DT_RELACOUNT:
11892 case DT_RELCOUNT:
11893 if (do_dynamic)
11894 {
11895 print_vma (entry->d_un.d_val, UNSIGNED);
11896 putchar ('\n');
11897 }
11898 break;
11899
11900 case DT_SYMINSZ:
11901 case DT_SYMINENT:
11902 case DT_SYMINFO:
11903 case DT_USED:
11904 case DT_INIT_ARRAY:
11905 case DT_FINI_ARRAY:
11906 if (do_dynamic)
11907 {
11908 if (entry->d_tag == DT_USED
11909 && valid_dynamic_name (filedata, entry->d_un.d_val))
11910 {
11911 const char *name
11912 = get_dynamic_name (filedata, entry->d_un.d_val);
11913
11914 if (*name)
11915 {
11916 printf (_("Not needed object: [%s]\n"), name);
11917 break;
11918 }
11919 }
11920
11921 print_vma (entry->d_un.d_val, PREFIX_HEX);
11922 putchar ('\n');
11923 }
11924 break;
11925
11926 case DT_BIND_NOW:
11927 /* The value of this entry is ignored. */
11928 if (do_dynamic)
11929 putchar ('\n');
11930 break;
11931
11932 case DT_GNU_PRELINKED:
11933 if (do_dynamic)
11934 {
11935 struct tm * tmp;
11936 time_t atime = entry->d_un.d_val;
11937
11938 tmp = gmtime (&atime);
11939 /* PR 17533 file: 041-1244816-0.004. */
11940 if (tmp == NULL)
11941 printf (_("<corrupt time val: %lx"),
11942 (unsigned long) atime);
11943 else
11944 printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
11945 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
11946 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
11947
11948 }
11949 break;
11950
11951 case DT_GNU_HASH:
11952 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11953 if (do_dynamic)
11954 {
11955 print_vma (entry->d_un.d_val, PREFIX_HEX);
11956 putchar ('\n');
11957 }
11958 break;
11959
11960 case DT_GNU_FLAGS_1:
11961 if (do_dynamic)
11962 {
11963 printf (_("Flags:"));
11964 if (entry->d_un.d_val == 0)
11965 printf (_(" None\n"));
11966 else
11967 {
11968 unsigned long int val = entry->d_un.d_val;
11969
11970 if (val & DF_GNU_1_UNIQUE)
11971 {
11972 printf (" UNIQUE");
11973 val ^= DF_GNU_1_UNIQUE;
11974 }
11975 if (val != 0)
11976 printf (" %lx", val);
11977 puts ("");
11978 }
11979 }
11980 break;
11981
11982 default:
11983 if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
11984 filedata->version_info[DT_VERSIONTAGIDX (entry->d_tag)]
11985 = entry->d_un.d_val;
11986
11987 if (do_dynamic)
11988 {
11989 switch (filedata->file_header.e_machine)
11990 {
11991 case EM_AARCH64:
11992 dynamic_section_aarch64_val (entry);
11993 break;
11994 case EM_MIPS:
11995 case EM_MIPS_RS3_LE:
11996 dynamic_section_mips_val (filedata, entry);
11997 break;
11998 case EM_PARISC:
11999 dynamic_section_parisc_val (entry);
12000 break;
12001 case EM_IA_64:
12002 dynamic_section_ia64_val (entry);
12003 break;
12004 default:
12005 print_vma (entry->d_un.d_val, PREFIX_HEX);
12006 putchar ('\n');
12007 }
12008 }
12009 break;
12010 }
12011 }
12012
12013 return true;
12014 }
12015
12016 static char *
12017 get_ver_flags (unsigned int flags)
12018 {
12019 static char buff[128];
12020
12021 buff[0] = 0;
12022
12023 if (flags == 0)
12024 return _("none");
12025
12026 if (flags & VER_FLG_BASE)
12027 strcat (buff, "BASE");
12028
12029 if (flags & VER_FLG_WEAK)
12030 {
12031 if (flags & VER_FLG_BASE)
12032 strcat (buff, " | ");
12033
12034 strcat (buff, "WEAK");
12035 }
12036
12037 if (flags & VER_FLG_INFO)
12038 {
12039 if (flags & (VER_FLG_BASE|VER_FLG_WEAK))
12040 strcat (buff, " | ");
12041
12042 strcat (buff, "INFO");
12043 }
12044
12045 if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
12046 {
12047 if (flags & (VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
12048 strcat (buff, " | ");
12049
12050 strcat (buff, _("<unknown>"));
12051 }
12052
12053 return buff;
12054 }
12055
12056 /* Display the contents of the version sections. */
12057
12058 static bool
12059 process_version_sections (Filedata * filedata)
12060 {
12061 Elf_Internal_Shdr * section;
12062 unsigned i;
12063 bool found = false;
12064
12065 if (! do_version)
12066 return true;
12067
12068 for (i = 0, section = filedata->section_headers;
12069 i < filedata->file_header.e_shnum;
12070 i++, section++)
12071 {
12072 switch (section->sh_type)
12073 {
12074 case SHT_GNU_verdef:
12075 {
12076 Elf_External_Verdef * edefs;
12077 unsigned long idx;
12078 unsigned long cnt;
12079 char * endbuf;
12080
12081 found = true;
12082
12083 if (filedata->is_separate)
12084 printf (ngettext ("\nIn linked file '%s' the version definition section '%s' contains %u entry:\n",
12085 "\nIn linked file '%s' the version definition section '%s' contains %u entries:\n",
12086 section->sh_info),
12087 filedata->file_name,
12088 printable_section_name (filedata, section),
12089 section->sh_info);
12090 else
12091 printf (ngettext ("\nVersion definition section '%s' "
12092 "contains %u entry:\n",
12093 "\nVersion definition section '%s' "
12094 "contains %u entries:\n",
12095 section->sh_info),
12096 printable_section_name (filedata, section),
12097 section->sh_info);
12098
12099 printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
12100 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
12101 (unsigned long) section->sh_offset, section->sh_link,
12102 printable_section_name_from_index (filedata, section->sh_link));
12103
12104 edefs = (Elf_External_Verdef *)
12105 get_data (NULL, filedata, section->sh_offset, 1,section->sh_size,
12106 _("version definition section"));
12107 if (!edefs)
12108 break;
12109 endbuf = (char *) edefs + section->sh_size;
12110
12111 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
12112 {
12113 char * vstart;
12114 Elf_External_Verdef * edef;
12115 Elf_Internal_Verdef ent;
12116 Elf_External_Verdaux * eaux;
12117 Elf_Internal_Verdaux aux;
12118 unsigned long isum;
12119 int j;
12120
12121 vstart = ((char *) edefs) + idx;
12122 if (vstart + sizeof (*edef) > endbuf)
12123 break;
12124
12125 edef = (Elf_External_Verdef *) vstart;
12126
12127 ent.vd_version = BYTE_GET (edef->vd_version);
12128 ent.vd_flags = BYTE_GET (edef->vd_flags);
12129 ent.vd_ndx = BYTE_GET (edef->vd_ndx);
12130 ent.vd_cnt = BYTE_GET (edef->vd_cnt);
12131 ent.vd_hash = BYTE_GET (edef->vd_hash);
12132 ent.vd_aux = BYTE_GET (edef->vd_aux);
12133 ent.vd_next = BYTE_GET (edef->vd_next);
12134
12135 printf (_(" %#06lx: Rev: %d Flags: %s"),
12136 idx, ent.vd_version, get_ver_flags (ent.vd_flags));
12137
12138 printf (_(" Index: %d Cnt: %d "),
12139 ent.vd_ndx, ent.vd_cnt);
12140
12141 /* Check for overflow. */
12142 if (ent.vd_aux > (size_t) (endbuf - vstart))
12143 break;
12144
12145 vstart += ent.vd_aux;
12146
12147 if (vstart + sizeof (*eaux) > endbuf)
12148 break;
12149 eaux = (Elf_External_Verdaux *) vstart;
12150
12151 aux.vda_name = BYTE_GET (eaux->vda_name);
12152 aux.vda_next = BYTE_GET (eaux->vda_next);
12153
12154 if (valid_dynamic_name (filedata, aux.vda_name))
12155 printf (_("Name: %s\n"),
12156 get_dynamic_name (filedata, aux.vda_name));
12157 else
12158 printf (_("Name index: %ld\n"), aux.vda_name);
12159
12160 isum = idx + ent.vd_aux;
12161
12162 for (j = 1; j < ent.vd_cnt; j++)
12163 {
12164 if (aux.vda_next < sizeof (*eaux)
12165 && !(j == ent.vd_cnt - 1 && aux.vda_next == 0))
12166 {
12167 warn (_("Invalid vda_next field of %lx\n"),
12168 aux.vda_next);
12169 j = ent.vd_cnt;
12170 break;
12171 }
12172 /* Check for overflow. */
12173 if (aux.vda_next > (size_t) (endbuf - vstart))
12174 break;
12175
12176 isum += aux.vda_next;
12177 vstart += aux.vda_next;
12178
12179 if (vstart + sizeof (*eaux) > endbuf)
12180 break;
12181 eaux = (Elf_External_Verdaux *) vstart;
12182
12183 aux.vda_name = BYTE_GET (eaux->vda_name);
12184 aux.vda_next = BYTE_GET (eaux->vda_next);
12185
12186 if (valid_dynamic_name (filedata, aux.vda_name))
12187 printf (_(" %#06lx: Parent %d: %s\n"),
12188 isum, j,
12189 get_dynamic_name (filedata, aux.vda_name));
12190 else
12191 printf (_(" %#06lx: Parent %d, name index: %ld\n"),
12192 isum, j, aux.vda_name);
12193 }
12194
12195 if (j < ent.vd_cnt)
12196 printf (_(" Version def aux past end of section\n"));
12197
12198 /* PR 17531:
12199 file: id:000001,src:000172+005151,op:splice,rep:2. */
12200 if (ent.vd_next < sizeof (*edef)
12201 && !(cnt == section->sh_info - 1 && ent.vd_next == 0))
12202 {
12203 warn (_("Invalid vd_next field of %lx\n"), ent.vd_next);
12204 cnt = section->sh_info;
12205 break;
12206 }
12207 if (ent.vd_next > (size_t) (endbuf - ((char *) edefs + idx)))
12208 break;
12209
12210 idx += ent.vd_next;
12211 }
12212
12213 if (cnt < section->sh_info)
12214 printf (_(" Version definition past end of section\n"));
12215
12216 free (edefs);
12217 }
12218 break;
12219
12220 case SHT_GNU_verneed:
12221 {
12222 Elf_External_Verneed * eneed;
12223 unsigned long idx;
12224 unsigned long cnt;
12225 char * endbuf;
12226
12227 found = true;
12228
12229 if (filedata->is_separate)
12230 printf (ngettext ("\nIn linked file '%s' the version needs section '%s' contains %u entry:\n",
12231 "\nIn linked file '%s' the version needs section '%s' contains %u entries:\n",
12232 section->sh_info),
12233 filedata->file_name,
12234 printable_section_name (filedata, section),
12235 section->sh_info);
12236 else
12237 printf (ngettext ("\nVersion needs section '%s' "
12238 "contains %u entry:\n",
12239 "\nVersion needs section '%s' "
12240 "contains %u entries:\n",
12241 section->sh_info),
12242 printable_section_name (filedata, section),
12243 section->sh_info);
12244
12245 printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
12246 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
12247 (unsigned long) section->sh_offset, section->sh_link,
12248 printable_section_name_from_index (filedata, section->sh_link));
12249
12250 eneed = (Elf_External_Verneed *) get_data (NULL, filedata,
12251 section->sh_offset, 1,
12252 section->sh_size,
12253 _("Version Needs section"));
12254 if (!eneed)
12255 break;
12256 endbuf = (char *) eneed + section->sh_size;
12257
12258 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
12259 {
12260 Elf_External_Verneed * entry;
12261 Elf_Internal_Verneed ent;
12262 unsigned long isum;
12263 int j;
12264 char * vstart;
12265
12266 vstart = ((char *) eneed) + idx;
12267 if (vstart + sizeof (*entry) > endbuf)
12268 break;
12269
12270 entry = (Elf_External_Verneed *) vstart;
12271
12272 ent.vn_version = BYTE_GET (entry->vn_version);
12273 ent.vn_cnt = BYTE_GET (entry->vn_cnt);
12274 ent.vn_file = BYTE_GET (entry->vn_file);
12275 ent.vn_aux = BYTE_GET (entry->vn_aux);
12276 ent.vn_next = BYTE_GET (entry->vn_next);
12277
12278 printf (_(" %#06lx: Version: %d"), idx, ent.vn_version);
12279
12280 if (valid_dynamic_name (filedata, ent.vn_file))
12281 printf (_(" File: %s"),
12282 get_dynamic_name (filedata, ent.vn_file));
12283 else
12284 printf (_(" File: %lx"), ent.vn_file);
12285
12286 printf (_(" Cnt: %d\n"), ent.vn_cnt);
12287
12288 /* Check for overflow. */
12289 if (ent.vn_aux > (size_t) (endbuf - vstart))
12290 break;
12291 vstart += ent.vn_aux;
12292
12293 for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
12294 {
12295 Elf_External_Vernaux * eaux;
12296 Elf_Internal_Vernaux aux;
12297
12298 if (vstart + sizeof (*eaux) > endbuf)
12299 break;
12300 eaux = (Elf_External_Vernaux *) vstart;
12301
12302 aux.vna_hash = BYTE_GET (eaux->vna_hash);
12303 aux.vna_flags = BYTE_GET (eaux->vna_flags);
12304 aux.vna_other = BYTE_GET (eaux->vna_other);
12305 aux.vna_name = BYTE_GET (eaux->vna_name);
12306 aux.vna_next = BYTE_GET (eaux->vna_next);
12307
12308 if (valid_dynamic_name (filedata, aux.vna_name))
12309 printf (_(" %#06lx: Name: %s"),
12310 isum, get_dynamic_name (filedata, aux.vna_name));
12311 else
12312 printf (_(" %#06lx: Name index: %lx"),
12313 isum, aux.vna_name);
12314
12315 printf (_(" Flags: %s Version: %d\n"),
12316 get_ver_flags (aux.vna_flags), aux.vna_other);
12317
12318 if (aux.vna_next < sizeof (*eaux)
12319 && !(j == ent.vn_cnt - 1 && aux.vna_next == 0))
12320 {
12321 warn (_("Invalid vna_next field of %lx\n"),
12322 aux.vna_next);
12323 j = ent.vn_cnt;
12324 break;
12325 }
12326 /* Check for overflow. */
12327 if (aux.vna_next > (size_t) (endbuf - vstart))
12328 break;
12329 isum += aux.vna_next;
12330 vstart += aux.vna_next;
12331 }
12332
12333 if (j < ent.vn_cnt)
12334 warn (_("Missing Version Needs auxiliary information\n"));
12335
12336 if (ent.vn_next < sizeof (*entry)
12337 && !(cnt == section->sh_info - 1 && ent.vn_next == 0))
12338 {
12339 warn (_("Invalid vn_next field of %lx\n"), ent.vn_next);
12340 cnt = section->sh_info;
12341 break;
12342 }
12343 if (ent.vn_next > (size_t) (endbuf - ((char *) eneed + idx)))
12344 break;
12345 idx += ent.vn_next;
12346 }
12347
12348 if (cnt < section->sh_info)
12349 warn (_("Missing Version Needs information\n"));
12350
12351 free (eneed);
12352 }
12353 break;
12354
12355 case SHT_GNU_versym:
12356 {
12357 Elf_Internal_Shdr * link_section;
12358 size_t total;
12359 unsigned int cnt;
12360 unsigned char * edata;
12361 unsigned short * data;
12362 char * strtab;
12363 Elf_Internal_Sym * symbols;
12364 Elf_Internal_Shdr * string_sec;
12365 unsigned long num_syms;
12366 long off;
12367
12368 if (section->sh_link >= filedata->file_header.e_shnum)
12369 break;
12370
12371 link_section = filedata->section_headers + section->sh_link;
12372 total = section->sh_size / sizeof (Elf_External_Versym);
12373
12374 if (link_section->sh_link >= filedata->file_header.e_shnum)
12375 break;
12376
12377 found = true;
12378
12379 symbols = get_elf_symbols (filedata, link_section, & num_syms);
12380 if (symbols == NULL)
12381 break;
12382
12383 string_sec = filedata->section_headers + link_section->sh_link;
12384
12385 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
12386 string_sec->sh_size,
12387 _("version string table"));
12388 if (!strtab)
12389 {
12390 free (symbols);
12391 break;
12392 }
12393
12394 if (filedata->is_separate)
12395 printf (ngettext ("\nIn linked file '%s' the version symbols section '%s' contains %lu entry:\n",
12396 "\nIn linked file '%s' the version symbols section '%s' contains %lu entries:\n",
12397 total),
12398 filedata->file_name,
12399 printable_section_name (filedata, section),
12400 (unsigned long) total);
12401 else
12402 printf (ngettext ("\nVersion symbols section '%s' "
12403 "contains %lu entry:\n",
12404 "\nVersion symbols section '%s' "
12405 "contains %lu entries:\n",
12406 total),
12407 printable_section_name (filedata, section),
12408 (unsigned long) total);
12409
12410 printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
12411 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
12412 (unsigned long) section->sh_offset, section->sh_link,
12413 printable_section_name (filedata, link_section));
12414
12415 off = offset_from_vma (filedata,
12416 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
12417 total * sizeof (short));
12418 edata = (unsigned char *) get_data (NULL, filedata, off,
12419 sizeof (short), total,
12420 _("version symbol data"));
12421 if (!edata)
12422 {
12423 free (strtab);
12424 free (symbols);
12425 break;
12426 }
12427
12428 data = (short unsigned int *) cmalloc (total, sizeof (short));
12429
12430 for (cnt = total; cnt --;)
12431 data[cnt] = byte_get (edata + cnt * sizeof (short),
12432 sizeof (short));
12433
12434 free (edata);
12435
12436 for (cnt = 0; cnt < total; cnt += 4)
12437 {
12438 int j, nn;
12439 char *name;
12440 char *invalid = _("*invalid*");
12441
12442 printf (" %03x:", cnt);
12443
12444 for (j = 0; (j < 4) && (cnt + j) < total; ++j)
12445 switch (data[cnt + j])
12446 {
12447 case 0:
12448 fputs (_(" 0 (*local*) "), stdout);
12449 break;
12450
12451 case 1:
12452 fputs (_(" 1 (*global*) "), stdout);
12453 break;
12454
12455 default:
12456 nn = printf ("%4x%c", data[cnt + j] & VERSYM_VERSION,
12457 data[cnt + j] & VERSYM_HIDDEN ? 'h' : ' ');
12458
12459 /* If this index value is greater than the size of the symbols
12460 array, break to avoid an out-of-bounds read. */
12461 if ((unsigned long)(cnt + j) >= num_syms)
12462 {
12463 warn (_("invalid index into symbol array\n"));
12464 break;
12465 }
12466
12467 name = NULL;
12468 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
12469 {
12470 Elf_Internal_Verneed ivn;
12471 unsigned long offset;
12472
12473 offset = offset_from_vma
12474 (filedata,
12475 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
12476 sizeof (Elf_External_Verneed));
12477
12478 do
12479 {
12480 Elf_Internal_Vernaux ivna;
12481 Elf_External_Verneed evn;
12482 Elf_External_Vernaux evna;
12483 unsigned long a_off;
12484
12485 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
12486 _("version need")) == NULL)
12487 break;
12488
12489 ivn.vn_aux = BYTE_GET (evn.vn_aux);
12490 ivn.vn_next = BYTE_GET (evn.vn_next);
12491
12492 a_off = offset + ivn.vn_aux;
12493
12494 do
12495 {
12496 if (get_data (&evna, filedata, a_off, sizeof (evna),
12497 1, _("version need aux (2)")) == NULL)
12498 {
12499 ivna.vna_next = 0;
12500 ivna.vna_other = 0;
12501 }
12502 else
12503 {
12504 ivna.vna_next = BYTE_GET (evna.vna_next);
12505 ivna.vna_other = BYTE_GET (evna.vna_other);
12506 }
12507
12508 a_off += ivna.vna_next;
12509 }
12510 while (ivna.vna_other != data[cnt + j]
12511 && ivna.vna_next != 0);
12512
12513 if (ivna.vna_other == data[cnt + j])
12514 {
12515 ivna.vna_name = BYTE_GET (evna.vna_name);
12516
12517 if (ivna.vna_name >= string_sec->sh_size)
12518 name = invalid;
12519 else
12520 name = strtab + ivna.vna_name;
12521 break;
12522 }
12523
12524 offset += ivn.vn_next;
12525 }
12526 while (ivn.vn_next);
12527 }
12528
12529 if (data[cnt + j] != 0x8001
12530 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
12531 {
12532 Elf_Internal_Verdef ivd;
12533 Elf_External_Verdef evd;
12534 unsigned long offset;
12535
12536 offset = offset_from_vma
12537 (filedata,
12538 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
12539 sizeof evd);
12540
12541 do
12542 {
12543 if (get_data (&evd, filedata, offset, sizeof (evd), 1,
12544 _("version def")) == NULL)
12545 {
12546 ivd.vd_next = 0;
12547 /* PR 17531: file: 046-1082287-0.004. */
12548 ivd.vd_ndx = (data[cnt + j] & VERSYM_VERSION) + 1;
12549 break;
12550 }
12551 else
12552 {
12553 ivd.vd_next = BYTE_GET (evd.vd_next);
12554 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
12555 }
12556
12557 offset += ivd.vd_next;
12558 }
12559 while (ivd.vd_ndx != (data[cnt + j] & VERSYM_VERSION)
12560 && ivd.vd_next != 0);
12561
12562 if (ivd.vd_ndx == (data[cnt + j] & VERSYM_VERSION))
12563 {
12564 Elf_External_Verdaux evda;
12565 Elf_Internal_Verdaux ivda;
12566
12567 ivd.vd_aux = BYTE_GET (evd.vd_aux);
12568
12569 if (get_data (&evda, filedata,
12570 offset - ivd.vd_next + ivd.vd_aux,
12571 sizeof (evda), 1,
12572 _("version def aux")) == NULL)
12573 break;
12574
12575 ivda.vda_name = BYTE_GET (evda.vda_name);
12576
12577 if (ivda.vda_name >= string_sec->sh_size)
12578 name = invalid;
12579 else if (name != NULL && name != invalid)
12580 name = _("*both*");
12581 else
12582 name = strtab + ivda.vda_name;
12583 }
12584 }
12585 if (name != NULL)
12586 nn += printf ("(%s%-*s",
12587 name,
12588 12 - (int) strlen (name),
12589 ")");
12590
12591 if (nn < 18)
12592 printf ("%*c", 18 - nn, ' ');
12593 }
12594
12595 putchar ('\n');
12596 }
12597
12598 free (data);
12599 free (strtab);
12600 free (symbols);
12601 }
12602 break;
12603
12604 default:
12605 break;
12606 }
12607 }
12608
12609 if (! found)
12610 {
12611 if (filedata->is_separate)
12612 printf (_("\nNo version information found in linked file '%s'.\n"),
12613 filedata->file_name);
12614 else
12615 printf (_("\nNo version information found in this file.\n"));
12616 }
12617
12618 return true;
12619 }
12620
12621 static const char *
12622 get_symbol_binding (Filedata * filedata, unsigned int binding)
12623 {
12624 static char buff[64];
12625
12626 switch (binding)
12627 {
12628 case STB_LOCAL: return "LOCAL";
12629 case STB_GLOBAL: return "GLOBAL";
12630 case STB_WEAK: return "WEAK";
12631 default:
12632 if (binding >= STB_LOPROC && binding <= STB_HIPROC)
12633 snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
12634 binding);
12635 else if (binding >= STB_LOOS && binding <= STB_HIOS)
12636 {
12637 if (binding == STB_GNU_UNIQUE
12638 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU)
12639 return "UNIQUE";
12640 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
12641 }
12642 else
12643 snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
12644 return buff;
12645 }
12646 }
12647
12648 static const char *
12649 get_symbol_type (Filedata * filedata, unsigned int type)
12650 {
12651 static char buff[64];
12652
12653 switch (type)
12654 {
12655 case STT_NOTYPE: return "NOTYPE";
12656 case STT_OBJECT: return "OBJECT";
12657 case STT_FUNC: return "FUNC";
12658 case STT_SECTION: return "SECTION";
12659 case STT_FILE: return "FILE";
12660 case STT_COMMON: return "COMMON";
12661 case STT_TLS: return "TLS";
12662 case STT_RELC: return "RELC";
12663 case STT_SRELC: return "SRELC";
12664 default:
12665 if (type >= STT_LOPROC && type <= STT_HIPROC)
12666 {
12667 if (filedata->file_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
12668 return "THUMB_FUNC";
12669
12670 if (filedata->file_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
12671 return "REGISTER";
12672
12673 if (filedata->file_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
12674 return "PARISC_MILLI";
12675
12676 snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
12677 }
12678 else if (type >= STT_LOOS && type <= STT_HIOS)
12679 {
12680 if (filedata->file_header.e_machine == EM_PARISC)
12681 {
12682 if (type == STT_HP_OPAQUE)
12683 return "HP_OPAQUE";
12684 if (type == STT_HP_STUB)
12685 return "HP_STUB";
12686 }
12687
12688 if (type == STT_GNU_IFUNC
12689 && (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU
12690 || filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_FREEBSD))
12691 return "IFUNC";
12692
12693 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
12694 }
12695 else
12696 snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
12697 return buff;
12698 }
12699 }
12700
12701 static const char *
12702 get_symbol_visibility (unsigned int visibility)
12703 {
12704 switch (visibility)
12705 {
12706 case STV_DEFAULT: return "DEFAULT";
12707 case STV_INTERNAL: return "INTERNAL";
12708 case STV_HIDDEN: return "HIDDEN";
12709 case STV_PROTECTED: return "PROTECTED";
12710 default:
12711 error (_("Unrecognized visibility value: %u\n"), visibility);
12712 return _("<unknown>");
12713 }
12714 }
12715
12716 static const char *
12717 get_alpha_symbol_other (unsigned int other)
12718 {
12719 switch (other)
12720 {
12721 case STO_ALPHA_NOPV: return "NOPV";
12722 case STO_ALPHA_STD_GPLOAD: return "STD GPLOAD";
12723 default:
12724 error (_("Unrecognized alpha specific other value: %u\n"), other);
12725 return _("<unknown>");
12726 }
12727 }
12728
12729 static const char *
12730 get_solaris_symbol_visibility (unsigned int visibility)
12731 {
12732 switch (visibility)
12733 {
12734 case 4: return "EXPORTED";
12735 case 5: return "SINGLETON";
12736 case 6: return "ELIMINATE";
12737 default: return get_symbol_visibility (visibility);
12738 }
12739 }
12740
12741 static const char *
12742 get_aarch64_symbol_other (unsigned int other)
12743 {
12744 static char buf[32];
12745
12746 if (other & STO_AARCH64_VARIANT_PCS)
12747 {
12748 other &= ~STO_AARCH64_VARIANT_PCS;
12749 if (other == 0)
12750 return "VARIANT_PCS";
12751 snprintf (buf, sizeof buf, "VARIANT_PCS | %x", other);
12752 return buf;
12753 }
12754 return NULL;
12755 }
12756
12757 static const char *
12758 get_mips_symbol_other (unsigned int other)
12759 {
12760 switch (other)
12761 {
12762 case STO_OPTIONAL: return "OPTIONAL";
12763 case STO_MIPS_PLT: return "MIPS PLT";
12764 case STO_MIPS_PIC: return "MIPS PIC";
12765 case STO_MICROMIPS: return "MICROMIPS";
12766 case STO_MICROMIPS | STO_MIPS_PIC: return "MICROMIPS, MIPS PIC";
12767 case STO_MIPS16: return "MIPS16";
12768 default: return NULL;
12769 }
12770 }
12771
12772 static const char *
12773 get_ia64_symbol_other (Filedata * filedata, unsigned int other)
12774 {
12775 if (is_ia64_vms (filedata))
12776 {
12777 static char res[32];
12778
12779 res[0] = 0;
12780
12781 /* Function types is for images and .STB files only. */
12782 switch (filedata->file_header.e_type)
12783 {
12784 case ET_DYN:
12785 case ET_EXEC:
12786 switch (VMS_ST_FUNC_TYPE (other))
12787 {
12788 case VMS_SFT_CODE_ADDR:
12789 strcat (res, " CA");
12790 break;
12791 case VMS_SFT_SYMV_IDX:
12792 strcat (res, " VEC");
12793 break;
12794 case VMS_SFT_FD:
12795 strcat (res, " FD");
12796 break;
12797 case VMS_SFT_RESERVE:
12798 strcat (res, " RSV");
12799 break;
12800 default:
12801 warn (_("Unrecognized IA64 VMS ST Function type: %d\n"),
12802 VMS_ST_FUNC_TYPE (other));
12803 strcat (res, " <unknown>");
12804 break;
12805 }
12806 break;
12807 default:
12808 break;
12809 }
12810 switch (VMS_ST_LINKAGE (other))
12811 {
12812 case VMS_STL_IGNORE:
12813 strcat (res, " IGN");
12814 break;
12815 case VMS_STL_RESERVE:
12816 strcat (res, " RSV");
12817 break;
12818 case VMS_STL_STD:
12819 strcat (res, " STD");
12820 break;
12821 case VMS_STL_LNK:
12822 strcat (res, " LNK");
12823 break;
12824 default:
12825 warn (_("Unrecognized IA64 VMS ST Linkage: %d\n"),
12826 VMS_ST_LINKAGE (other));
12827 strcat (res, " <unknown>");
12828 break;
12829 }
12830
12831 if (res[0] != 0)
12832 return res + 1;
12833 else
12834 return res;
12835 }
12836 return NULL;
12837 }
12838
12839 static const char *
12840 get_ppc64_symbol_other (unsigned int other)
12841 {
12842 if ((other & ~STO_PPC64_LOCAL_MASK) != 0)
12843 return NULL;
12844
12845 other >>= STO_PPC64_LOCAL_BIT;
12846 if (other <= 6)
12847 {
12848 static char buf[64];
12849 if (other >= 2)
12850 other = ppc64_decode_local_entry (other);
12851 snprintf (buf, sizeof buf, _("<localentry>: %d"), other);
12852 return buf;
12853 }
12854 return NULL;
12855 }
12856
12857 static const char *
12858 get_riscv_symbol_other (unsigned int other)
12859 {
12860 static char buf[32];
12861 buf[0] = 0;
12862
12863 if (other & STO_RISCV_VARIANT_CC)
12864 {
12865 strcat (buf, _(" VARIANT_CC"));
12866 other &= ~STO_RISCV_VARIANT_CC;
12867 }
12868
12869 if (other != 0)
12870 snprintf (buf, sizeof buf, " %x", other);
12871
12872
12873 if (buf[0] != 0)
12874 return buf + 1;
12875 else
12876 return buf;
12877 }
12878
12879 static const char *
12880 get_symbol_other (Filedata * filedata, unsigned int other)
12881 {
12882 const char * result = NULL;
12883 static char buff [64];
12884
12885 if (other == 0)
12886 return "";
12887
12888 switch (filedata->file_header.e_machine)
12889 {
12890 case EM_ALPHA:
12891 result = get_alpha_symbol_other (other);
12892 break;
12893 case EM_AARCH64:
12894 result = get_aarch64_symbol_other (other);
12895 break;
12896 case EM_MIPS:
12897 result = get_mips_symbol_other (other);
12898 break;
12899 case EM_IA_64:
12900 result = get_ia64_symbol_other (filedata, other);
12901 break;
12902 case EM_PPC64:
12903 result = get_ppc64_symbol_other (other);
12904 break;
12905 case EM_RISCV:
12906 result = get_riscv_symbol_other (other);
12907 break;
12908 default:
12909 result = NULL;
12910 break;
12911 }
12912
12913 if (result)
12914 return result;
12915
12916 snprintf (buff, sizeof buff, _("<other>: %x"), other);
12917 return buff;
12918 }
12919
12920 static const char *
12921 get_symbol_index_type (Filedata * filedata, unsigned int type)
12922 {
12923 static char buff[32];
12924
12925 switch (type)
12926 {
12927 case SHN_UNDEF: return "UND";
12928 case SHN_ABS: return "ABS";
12929 case SHN_COMMON: return "COM";
12930 default:
12931 if (type == SHN_IA_64_ANSI_COMMON
12932 && filedata->file_header.e_machine == EM_IA_64
12933 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX)
12934 return "ANSI_COM";
12935 else if ((filedata->file_header.e_machine == EM_X86_64
12936 || filedata->file_header.e_machine == EM_L1OM
12937 || filedata->file_header.e_machine == EM_K1OM)
12938 && type == SHN_X86_64_LCOMMON)
12939 return "LARGE_COM";
12940 else if ((type == SHN_MIPS_SCOMMON
12941 && filedata->file_header.e_machine == EM_MIPS)
12942 || (type == SHN_TIC6X_SCOMMON
12943 && filedata->file_header.e_machine == EM_TI_C6000))
12944 return "SCOM";
12945 else if (type == SHN_MIPS_SUNDEFINED
12946 && filedata->file_header.e_machine == EM_MIPS)
12947 return "SUND";
12948 else if (type >= SHN_LOPROC && type <= SHN_HIPROC)
12949 sprintf (buff, "PRC[0x%04x]", type & 0xffff);
12950 else if (type >= SHN_LOOS && type <= SHN_HIOS)
12951 sprintf (buff, "OS [0x%04x]", type & 0xffff);
12952 else if (type >= SHN_LORESERVE)
12953 sprintf (buff, "RSV[0x%04x]", type & 0xffff);
12954 else if (filedata->file_header.e_shnum != 0
12955 && type >= filedata->file_header.e_shnum)
12956 sprintf (buff, _("bad section index[%3d]"), type);
12957 else
12958 sprintf (buff, "%3d", type);
12959 break;
12960 }
12961
12962 return buff;
12963 }
12964
12965 static const char *
12966 get_symbol_version_string (Filedata * filedata,
12967 bool is_dynsym,
12968 const char * strtab,
12969 unsigned long int strtab_size,
12970 unsigned int si,
12971 Elf_Internal_Sym * psym,
12972 enum versioned_symbol_info * sym_info,
12973 unsigned short * vna_other)
12974 {
12975 unsigned char data[2];
12976 unsigned short vers_data;
12977 unsigned long offset;
12978 unsigned short max_vd_ndx;
12979
12980 if (!is_dynsym
12981 || filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)] == 0)
12982 return NULL;
12983
12984 offset = offset_from_vma (filedata,
12985 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
12986 sizeof data + si * sizeof (vers_data));
12987
12988 if (get_data (&data, filedata, offset + si * sizeof (vers_data),
12989 sizeof (data), 1, _("version data")) == NULL)
12990 return NULL;
12991
12992 vers_data = byte_get (data, 2);
12993
12994 if ((vers_data & VERSYM_HIDDEN) == 0 && vers_data == 0)
12995 return NULL;
12996
12997 *sym_info = (vers_data & VERSYM_HIDDEN) != 0 ? symbol_hidden : symbol_public;
12998 max_vd_ndx = 0;
12999
13000 /* Usually we'd only see verdef for defined symbols, and verneed for
13001 undefined symbols. However, symbols defined by the linker in
13002 .dynbss for variables copied from a shared library in order to
13003 avoid text relocations are defined yet have verneed. We could
13004 use a heuristic to detect the special case, for example, check
13005 for verneed first on symbols defined in SHT_NOBITS sections, but
13006 it is simpler and more reliable to just look for both verdef and
13007 verneed. .dynbss might not be mapped to a SHT_NOBITS section. */
13008
13009 if (psym->st_shndx != SHN_UNDEF
13010 && vers_data != 0x8001
13011 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
13012 {
13013 Elf_Internal_Verdef ivd;
13014 Elf_Internal_Verdaux ivda;
13015 Elf_External_Verdaux evda;
13016 unsigned long off;
13017
13018 off = offset_from_vma (filedata,
13019 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
13020 sizeof (Elf_External_Verdef));
13021
13022 do
13023 {
13024 Elf_External_Verdef evd;
13025
13026 if (get_data (&evd, filedata, off, sizeof (evd), 1,
13027 _("version def")) == NULL)
13028 {
13029 ivd.vd_ndx = 0;
13030 ivd.vd_aux = 0;
13031 ivd.vd_next = 0;
13032 ivd.vd_flags = 0;
13033 }
13034 else
13035 {
13036 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
13037 ivd.vd_aux = BYTE_GET (evd.vd_aux);
13038 ivd.vd_next = BYTE_GET (evd.vd_next);
13039 ivd.vd_flags = BYTE_GET (evd.vd_flags);
13040 }
13041
13042 if ((ivd.vd_ndx & VERSYM_VERSION) > max_vd_ndx)
13043 max_vd_ndx = ivd.vd_ndx & VERSYM_VERSION;
13044
13045 off += ivd.vd_next;
13046 }
13047 while (ivd.vd_ndx != (vers_data & VERSYM_VERSION) && ivd.vd_next != 0);
13048
13049 if (ivd.vd_ndx == (vers_data & VERSYM_VERSION))
13050 {
13051 if (ivd.vd_ndx == 1 && ivd.vd_flags == VER_FLG_BASE)
13052 return NULL;
13053
13054 off -= ivd.vd_next;
13055 off += ivd.vd_aux;
13056
13057 if (get_data (&evda, filedata, off, sizeof (evda), 1,
13058 _("version def aux")) != NULL)
13059 {
13060 ivda.vda_name = BYTE_GET (evda.vda_name);
13061
13062 if (psym->st_name != ivda.vda_name)
13063 return (ivda.vda_name < strtab_size
13064 ? strtab + ivda.vda_name : _("<corrupt>"));
13065 }
13066 }
13067 }
13068
13069 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
13070 {
13071 Elf_External_Verneed evn;
13072 Elf_Internal_Verneed ivn;
13073 Elf_Internal_Vernaux ivna;
13074
13075 offset = offset_from_vma (filedata,
13076 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
13077 sizeof evn);
13078 do
13079 {
13080 unsigned long vna_off;
13081
13082 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
13083 _("version need")) == NULL)
13084 {
13085 ivna.vna_next = 0;
13086 ivna.vna_other = 0;
13087 ivna.vna_name = 0;
13088 break;
13089 }
13090
13091 ivn.vn_aux = BYTE_GET (evn.vn_aux);
13092 ivn.vn_next = BYTE_GET (evn.vn_next);
13093
13094 vna_off = offset + ivn.vn_aux;
13095
13096 do
13097 {
13098 Elf_External_Vernaux evna;
13099
13100 if (get_data (&evna, filedata, vna_off, sizeof (evna), 1,
13101 _("version need aux (3)")) == NULL)
13102 {
13103 ivna.vna_next = 0;
13104 ivna.vna_other = 0;
13105 ivna.vna_name = 0;
13106 }
13107 else
13108 {
13109 ivna.vna_other = BYTE_GET (evna.vna_other);
13110 ivna.vna_next = BYTE_GET (evna.vna_next);
13111 ivna.vna_name = BYTE_GET (evna.vna_name);
13112 }
13113
13114 vna_off += ivna.vna_next;
13115 }
13116 while (ivna.vna_other != vers_data && ivna.vna_next != 0);
13117
13118 if (ivna.vna_other == vers_data)
13119 break;
13120
13121 offset += ivn.vn_next;
13122 }
13123 while (ivn.vn_next != 0);
13124
13125 if (ivna.vna_other == vers_data)
13126 {
13127 *sym_info = symbol_undefined;
13128 *vna_other = ivna.vna_other;
13129 return (ivna.vna_name < strtab_size
13130 ? strtab + ivna.vna_name : _("<corrupt>"));
13131 }
13132 else if ((max_vd_ndx || (vers_data & VERSYM_VERSION) != 1)
13133 && (vers_data & VERSYM_VERSION) > max_vd_ndx)
13134 return _("<corrupt>");
13135 }
13136 return NULL;
13137 }
13138
13139 /* Display a symbol size on stdout. Format is based on --sym-base setting. */
13140
13141 static unsigned int
13142 print_dynamic_symbol_size (uint64_t vma, int base)
13143 {
13144 switch (base)
13145 {
13146 case 8:
13147 return print_vma (vma, OCTAL_5);
13148
13149 case 10:
13150 return print_vma (vma, UNSIGNED_5);
13151
13152 case 16:
13153 return print_vma (vma, PREFIX_HEX_5);
13154
13155 case 0:
13156 default:
13157 return print_vma (vma, DEC_5);
13158 }
13159 }
13160
13161 static void
13162 print_dynamic_symbol (Filedata *filedata, unsigned long si,
13163 Elf_Internal_Sym *symtab,
13164 Elf_Internal_Shdr *section,
13165 char *strtab, size_t strtab_size)
13166 {
13167 const char *version_string;
13168 enum versioned_symbol_info sym_info;
13169 unsigned short vna_other;
13170 bool is_valid;
13171 const char * sstr;
13172 Elf_Internal_Sym *psym = symtab + si;
13173
13174 printf ("%6ld: ", si);
13175 print_vma (psym->st_value, LONG_HEX);
13176 putchar (' ');
13177 print_dynamic_symbol_size (psym->st_size, sym_base);
13178 printf (" %-7s", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
13179 printf (" %-6s", get_symbol_binding (filedata, ELF_ST_BIND (psym->st_info)));
13180 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
13181 printf (" %-7s", get_solaris_symbol_visibility (psym->st_other));
13182 else
13183 {
13184 unsigned int vis = ELF_ST_VISIBILITY (psym->st_other);
13185
13186 printf (" %-7s", get_symbol_visibility (vis));
13187 /* Check to see if any other bits in the st_other field are set.
13188 Note - displaying this information disrupts the layout of the
13189 table being generated, but for the moment this case is very rare. */
13190 if (psym->st_other ^ vis)
13191 printf (" [%s] ", get_symbol_other (filedata, psym->st_other ^ vis));
13192 }
13193 printf (" %4s ", get_symbol_index_type (filedata, psym->st_shndx));
13194
13195 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION
13196 && psym->st_shndx < filedata->file_header.e_shnum
13197 && filedata->section_headers != NULL
13198 && psym->st_name == 0)
13199 {
13200 is_valid
13201 = section_name_valid (filedata,
13202 filedata->section_headers + psym->st_shndx);
13203 sstr = is_valid ?
13204 section_name_print (filedata,
13205 filedata->section_headers + psym->st_shndx)
13206 : _("<corrupt>");
13207 }
13208 else
13209 {
13210 is_valid = valid_symbol_name (strtab, strtab_size, psym->st_name);
13211 sstr = is_valid ? strtab + psym->st_name : _("<corrupt>");
13212 }
13213
13214 version_string
13215 = get_symbol_version_string (filedata,
13216 (section == NULL
13217 || section->sh_type == SHT_DYNSYM),
13218 strtab, strtab_size, si,
13219 psym, &sym_info, &vna_other);
13220
13221 int len_avail = 21;
13222 if (! do_wide && version_string != NULL)
13223 {
13224 char buffer[16];
13225
13226 len_avail -= 1 + strlen (version_string);
13227
13228 if (sym_info == symbol_undefined)
13229 len_avail -= sprintf (buffer," (%d)", vna_other);
13230 else if (sym_info != symbol_hidden)
13231 len_avail -= 1;
13232 }
13233
13234 print_symbol (len_avail, sstr);
13235
13236 if (version_string)
13237 {
13238 if (sym_info == symbol_undefined)
13239 printf ("@%s (%d)", version_string, vna_other);
13240 else
13241 printf (sym_info == symbol_hidden ? "@%s" : "@@%s",
13242 version_string);
13243 }
13244
13245 putchar ('\n');
13246
13247 if (ELF_ST_BIND (psym->st_info) == STB_LOCAL
13248 && section != NULL
13249 && si >= section->sh_info
13250 /* Irix 5 and 6 MIPS binaries are known to ignore this requirement. */
13251 && filedata->file_header.e_machine != EM_MIPS
13252 /* Solaris binaries have been found to violate this requirement as
13253 well. Not sure if this is a bug or an ABI requirement. */
13254 && filedata->file_header.e_ident[EI_OSABI] != ELFOSABI_SOLARIS)
13255 warn (_("local symbol %lu found at index >= %s's sh_info value of %u\n"),
13256 si, printable_section_name (filedata, section), section->sh_info);
13257 }
13258
13259 static const char *
13260 get_lto_kind (unsigned int kind)
13261 {
13262 switch (kind)
13263 {
13264 case 0: return "DEF";
13265 case 1: return "WEAKDEF";
13266 case 2: return "UNDEF";
13267 case 3: return "WEAKUNDEF";
13268 case 4: return "COMMON";
13269 default:
13270 break;
13271 }
13272
13273 static char buffer[30];
13274 error (_("Unknown LTO symbol definition encountered: %u\n"), kind);
13275 sprintf (buffer, "<unknown: %u>", kind);
13276 return buffer;
13277 }
13278
13279 static const char *
13280 get_lto_visibility (unsigned int visibility)
13281 {
13282 switch (visibility)
13283 {
13284 case 0: return "DEFAULT";
13285 case 1: return "PROTECTED";
13286 case 2: return "INTERNAL";
13287 case 3: return "HIDDEN";
13288 default:
13289 break;
13290 }
13291
13292 static char buffer[30];
13293 error (_("Unknown LTO symbol visibility encountered: %u\n"), visibility);
13294 sprintf (buffer, "<unknown: %u>", visibility);
13295 return buffer;
13296 }
13297
13298 static const char *
13299 get_lto_sym_type (unsigned int sym_type)
13300 {
13301 switch (sym_type)
13302 {
13303 case 0: return "UNKNOWN";
13304 case 1: return "FUNCTION";
13305 case 2: return "VARIABLE";
13306 default:
13307 break;
13308 }
13309
13310 static char buffer[30];
13311 error (_("Unknown LTO symbol type encountered: %u\n"), sym_type);
13312 sprintf (buffer, "<unknown: %u>", sym_type);
13313 return buffer;
13314 }
13315
13316 /* Display an LTO format symbol table.
13317 FIXME: The format of LTO symbol tables is not formalized.
13318 So this code could need changing in the future. */
13319
13320 static bool
13321 display_lto_symtab (Filedata * filedata,
13322 Elf_Internal_Shdr * section)
13323 {
13324 if (section->sh_size == 0)
13325 {
13326 if (filedata->is_separate)
13327 printf (_("\nThe LTO Symbol table section '%s' in linked file '%s' is empty!\n"),
13328 printable_section_name (filedata, section),
13329 filedata->file_name);
13330 else
13331 printf (_("\nLTO Symbol table '%s' is empty!\n"),
13332 printable_section_name (filedata, section));
13333
13334 return true;
13335 }
13336
13337 if (section->sh_size > filedata->file_size)
13338 {
13339 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
13340 printable_section_name (filedata, section),
13341 (unsigned long) section->sh_size);
13342 return false;
13343 }
13344
13345 void * alloced_data = get_data (NULL, filedata, section->sh_offset,
13346 section->sh_size, 1, _("LTO symbols"));
13347 if (alloced_data == NULL)
13348 return false;
13349
13350 /* Look for extended data for the symbol table. */
13351 Elf_Internal_Shdr * ext;
13352 void * ext_data_orig = NULL;
13353 char * ext_data = NULL;
13354 char * ext_data_end = NULL;
13355 char * ext_name = NULL;
13356
13357 if (asprintf (& ext_name, ".gnu.lto_.ext_symtab.%s",
13358 (section_name (filedata, section)
13359 + sizeof (".gnu.lto_.symtab.") - 1)) > 0
13360 && ext_name != NULL /* Paranoia. */
13361 && (ext = find_section (filedata, ext_name)) != NULL)
13362 {
13363 if (ext->sh_size < 3)
13364 error (_("LTO Symbol extension table '%s' is empty!\n"),
13365 printable_section_name (filedata, ext));
13366 else
13367 {
13368 ext_data_orig = ext_data = get_data (NULL, filedata, ext->sh_offset,
13369 ext->sh_size, 1,
13370 _("LTO ext symbol data"));
13371 if (ext_data != NULL)
13372 {
13373 ext_data_end = ext_data + ext->sh_size;
13374 if (* ext_data++ != 1)
13375 error (_("Unexpected version number in symbol extension table\n"));
13376 }
13377 }
13378 }
13379
13380 const unsigned char * data = (const unsigned char *) alloced_data;
13381 const unsigned char * end = data + section->sh_size;
13382
13383 if (filedata->is_separate)
13384 printf (_("\nIn linked file '%s': "), filedata->file_name);
13385 else
13386 printf ("\n");
13387
13388 if (ext_data_orig != NULL)
13389 {
13390 if (do_wide)
13391 printf (_("LTO Symbol table '%s' and extension table '%s' contain:\n"),
13392 printable_section_name (filedata, section),
13393 printable_section_name (filedata, ext));
13394 else
13395 {
13396 printf (_("LTO Symbol table '%s'\n"),
13397 printable_section_name (filedata, section));
13398 printf (_(" and extension table '%s' contain:\n"),
13399 printable_section_name (filedata, ext));
13400 }
13401 }
13402 else
13403 printf (_("LTO Symbol table '%s' contains:\n"),
13404 printable_section_name (filedata, section));
13405
13406 /* FIXME: Add a wide version. */
13407 if (ext_data_orig != NULL)
13408 printf (_(" Comdat_Key Kind Visibility Size Slot Type Section Name\n"));
13409 else
13410 printf (_(" Comdat_Key Kind Visibility Size Slot Name\n"));
13411
13412 /* FIXME: We do not handle style prefixes. */
13413
13414 while (data < end)
13415 {
13416 const unsigned char * sym_name = data;
13417 data += strnlen ((const char *) sym_name, end - data) + 1;
13418 if (data >= end)
13419 goto fail;
13420
13421 const unsigned char * comdat_key = data;
13422 data += strnlen ((const char *) comdat_key, end - data) + 1;
13423 if (data >= end)
13424 goto fail;
13425
13426 if (data + 2 + 8 + 4 > end)
13427 goto fail;
13428
13429 unsigned int kind = *data++;
13430 unsigned int visibility = *data++;
13431
13432 uint64_t size = byte_get (data, 8);
13433 data += 8;
13434
13435 uint64_t slot = byte_get (data, 4);
13436 data += 4;
13437
13438 if (ext_data != NULL)
13439 {
13440 if (ext_data < (ext_data_end - 1))
13441 {
13442 unsigned int sym_type = * ext_data ++;
13443 unsigned int sec_kind = * ext_data ++;
13444
13445 printf (" %10s %10s %11s %08" PRIx64 " %08" PRIx64 " %9s %08x _",
13446 * comdat_key == 0 ? "-" : (char *) comdat_key,
13447 get_lto_kind (kind),
13448 get_lto_visibility (visibility),
13449 size,
13450 slot,
13451 get_lto_sym_type (sym_type),
13452 sec_kind);
13453 print_symbol (6, (const char *) sym_name);
13454 }
13455 else
13456 {
13457 error (_("Ran out of LTO symbol extension data\n"));
13458 ext_data = NULL;
13459 /* FIXME: return FAIL result ? */
13460 }
13461 }
13462 else
13463 {
13464 printf (" %10s %10s %11s %08" PRIx64 " %08" PRIx64 " _",
13465 * comdat_key == 0 ? "-" : (char *) comdat_key,
13466 get_lto_kind (kind),
13467 get_lto_visibility (visibility),
13468 size,
13469 slot);
13470 print_symbol (21, (const char *) sym_name);
13471 }
13472 putchar ('\n');
13473 }
13474
13475 if (ext_data != NULL && ext_data < ext_data_end)
13476 {
13477 error (_("Data remains in the LTO symbol extension table\n"));
13478 goto fail;
13479 }
13480
13481 free (alloced_data);
13482 free (ext_data_orig);
13483 free (ext_name);
13484 return true;
13485
13486 fail:
13487 error (_("Buffer overrun encountered whilst decoding LTO symbol table\n"));
13488 free (alloced_data);
13489 free (ext_data_orig);
13490 free (ext_name);
13491 return false;
13492 }
13493
13494 /* Display LTO symbol tables. */
13495
13496 static bool
13497 process_lto_symbol_tables (Filedata * filedata)
13498 {
13499 Elf_Internal_Shdr * section;
13500 unsigned int i;
13501 bool res = true;
13502
13503 if (!do_lto_syms)
13504 return true;
13505
13506 if (filedata->section_headers == NULL)
13507 return true;
13508
13509 for (i = 0, section = filedata->section_headers;
13510 i < filedata->file_header.e_shnum;
13511 i++, section++)
13512 if (section_name_valid (filedata, section)
13513 && startswith (section_name (filedata, section), ".gnu.lto_.symtab."))
13514 res &= display_lto_symtab (filedata, section);
13515
13516 return res;
13517 }
13518
13519 /* Dump the symbol table. */
13520
13521 static bool
13522 process_symbol_table (Filedata * filedata)
13523 {
13524 Elf_Internal_Shdr * section;
13525
13526 if (!do_syms && !do_dyn_syms && !do_histogram)
13527 return true;
13528
13529 if ((filedata->dynamic_info[DT_HASH] || filedata->dynamic_info_DT_GNU_HASH)
13530 && do_syms
13531 && do_using_dynamic
13532 && filedata->dynamic_strings != NULL
13533 && filedata->dynamic_symbols != NULL)
13534 {
13535 unsigned long si;
13536
13537 if (filedata->is_separate)
13538 {
13539 printf (ngettext ("\nIn linked file '%s' the dynamic symbol table contains %lu entry:\n",
13540 "\nIn linked file '%s' the dynamic symbol table contains %lu entries:\n",
13541 filedata->num_dynamic_syms),
13542 filedata->file_name,
13543 filedata->num_dynamic_syms);
13544 }
13545 else
13546 {
13547 printf (ngettext ("\nSymbol table for image contains %lu entry:\n",
13548 "\nSymbol table for image contains %lu entries:\n",
13549 filedata->num_dynamic_syms),
13550 filedata->num_dynamic_syms);
13551 }
13552 if (is_32bit_elf)
13553 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13554 else
13555 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13556
13557 for (si = 0; si < filedata->num_dynamic_syms; si++)
13558 print_dynamic_symbol (filedata, si, filedata->dynamic_symbols, NULL,
13559 filedata->dynamic_strings,
13560 filedata->dynamic_strings_length);
13561 }
13562 else if ((do_dyn_syms || (do_syms && !do_using_dynamic))
13563 && filedata->section_headers != NULL)
13564 {
13565 unsigned int i;
13566
13567 for (i = 0, section = filedata->section_headers;
13568 i < filedata->file_header.e_shnum;
13569 i++, section++)
13570 {
13571 char * strtab = NULL;
13572 unsigned long int strtab_size = 0;
13573 Elf_Internal_Sym * symtab;
13574 unsigned long si, num_syms;
13575
13576 if ((section->sh_type != SHT_SYMTAB
13577 && section->sh_type != SHT_DYNSYM)
13578 || (!do_syms
13579 && section->sh_type == SHT_SYMTAB))
13580 continue;
13581
13582 if (section->sh_entsize == 0)
13583 {
13584 printf (_("\nSymbol table '%s' has a sh_entsize of zero!\n"),
13585 printable_section_name (filedata, section));
13586 continue;
13587 }
13588
13589 num_syms = section->sh_size / section->sh_entsize;
13590
13591 if (filedata->is_separate)
13592 printf (ngettext ("\nIn linked file '%s' symbol section '%s' contains %lu entry:\n",
13593 "\nIn linked file '%s' symbol section '%s' contains %lu entries:\n",
13594 num_syms),
13595 filedata->file_name,
13596 printable_section_name (filedata, section),
13597 num_syms);
13598 else
13599 printf (ngettext ("\nSymbol table '%s' contains %lu entry:\n",
13600 "\nSymbol table '%s' contains %lu entries:\n",
13601 num_syms),
13602 printable_section_name (filedata, section),
13603 num_syms);
13604
13605 if (is_32bit_elf)
13606 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13607 else
13608 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13609
13610 symtab = get_elf_symbols (filedata, section, & num_syms);
13611 if (symtab == NULL)
13612 continue;
13613
13614 if (section->sh_link == filedata->file_header.e_shstrndx)
13615 {
13616 strtab = filedata->string_table;
13617 strtab_size = filedata->string_table_length;
13618 }
13619 else if (section->sh_link < filedata->file_header.e_shnum)
13620 {
13621 Elf_Internal_Shdr * string_sec;
13622
13623 string_sec = filedata->section_headers + section->sh_link;
13624
13625 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset,
13626 1, string_sec->sh_size,
13627 _("string table"));
13628 strtab_size = strtab != NULL ? string_sec->sh_size : 0;
13629 }
13630
13631 for (si = 0; si < num_syms; si++)
13632 print_dynamic_symbol (filedata, si, symtab, section,
13633 strtab, strtab_size);
13634
13635 free (symtab);
13636 if (strtab != filedata->string_table)
13637 free (strtab);
13638 }
13639 }
13640 else if (do_syms)
13641 printf
13642 (_("\nDynamic symbol information is not available for displaying symbols.\n"));
13643
13644 if (do_histogram && filedata->buckets != NULL)
13645 {
13646 unsigned long * lengths;
13647 unsigned long * counts;
13648 unsigned long hn;
13649 uint64_t si;
13650 unsigned long maxlength = 0;
13651 unsigned long nzero_counts = 0;
13652 unsigned long nsyms = 0;
13653 char *visited;
13654
13655 printf (ngettext ("\nHistogram for bucket list length "
13656 "(total of %lu bucket):\n",
13657 "\nHistogram for bucket list length "
13658 "(total of %lu buckets):\n",
13659 (unsigned long) filedata->nbuckets),
13660 (unsigned long) filedata->nbuckets);
13661
13662 lengths = (unsigned long *) calloc (filedata->nbuckets,
13663 sizeof (*lengths));
13664 if (lengths == NULL)
13665 {
13666 error (_("Out of memory allocating space for histogram buckets\n"));
13667 goto err_out;
13668 }
13669 visited = xcmalloc (filedata->nchains, 1);
13670 memset (visited, 0, filedata->nchains);
13671
13672 printf (_(" Length Number %% of total Coverage\n"));
13673 for (hn = 0; hn < filedata->nbuckets; ++hn)
13674 {
13675 for (si = filedata->buckets[hn]; si > 0; si = filedata->chains[si])
13676 {
13677 ++nsyms;
13678 if (maxlength < ++lengths[hn])
13679 ++maxlength;
13680 if (si >= filedata->nchains || visited[si])
13681 {
13682 error (_("histogram chain is corrupt\n"));
13683 break;
13684 }
13685 visited[si] = 1;
13686 }
13687 }
13688 free (visited);
13689
13690 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
13691 if (counts == NULL)
13692 {
13693 free (lengths);
13694 error (_("Out of memory allocating space for histogram counts\n"));
13695 goto err_out;
13696 }
13697
13698 for (hn = 0; hn < filedata->nbuckets; ++hn)
13699 ++counts[lengths[hn]];
13700
13701 if (filedata->nbuckets > 0)
13702 {
13703 unsigned long i;
13704 printf (" 0 %-10lu (%5.1f%%)\n",
13705 counts[0], (counts[0] * 100.0) / filedata->nbuckets);
13706 for (i = 1; i <= maxlength; ++i)
13707 {
13708 nzero_counts += counts[i] * i;
13709 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
13710 i, counts[i], (counts[i] * 100.0) / filedata->nbuckets,
13711 (nzero_counts * 100.0) / nsyms);
13712 }
13713 }
13714
13715 free (counts);
13716 free (lengths);
13717 }
13718
13719 free (filedata->buckets);
13720 filedata->buckets = NULL;
13721 filedata->nbuckets = 0;
13722 free (filedata->chains);
13723 filedata->chains = NULL;
13724
13725 if (do_histogram && filedata->gnubuckets != NULL)
13726 {
13727 unsigned long * lengths;
13728 unsigned long * counts;
13729 unsigned long hn;
13730 unsigned long maxlength = 0;
13731 unsigned long nzero_counts = 0;
13732 unsigned long nsyms = 0;
13733
13734 printf (ngettext ("\nHistogram for `%s' bucket list length "
13735 "(total of %lu bucket):\n",
13736 "\nHistogram for `%s' bucket list length "
13737 "(total of %lu buckets):\n",
13738 (unsigned long) filedata->ngnubuckets),
13739 GNU_HASH_SECTION_NAME (filedata),
13740 (unsigned long) filedata->ngnubuckets);
13741
13742 lengths = (unsigned long *) calloc (filedata->ngnubuckets,
13743 sizeof (*lengths));
13744 if (lengths == NULL)
13745 {
13746 error (_("Out of memory allocating space for gnu histogram buckets\n"));
13747 goto err_out;
13748 }
13749
13750 printf (_(" Length Number %% of total Coverage\n"));
13751
13752 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
13753 if (filedata->gnubuckets[hn] != 0)
13754 {
13755 uint64_t off, length = 1;
13756
13757 for (off = filedata->gnubuckets[hn] - filedata->gnusymidx;
13758 /* PR 17531 file: 010-77222-0.004. */
13759 off < filedata->ngnuchains
13760 && (filedata->gnuchains[off] & 1) == 0;
13761 ++off)
13762 ++length;
13763 lengths[hn] = length;
13764 if (length > maxlength)
13765 maxlength = length;
13766 nsyms += length;
13767 }
13768
13769 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
13770 if (counts == NULL)
13771 {
13772 free (lengths);
13773 error (_("Out of memory allocating space for gnu histogram counts\n"));
13774 goto err_out;
13775 }
13776
13777 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
13778 ++counts[lengths[hn]];
13779
13780 if (filedata->ngnubuckets > 0)
13781 {
13782 unsigned long j;
13783 printf (" 0 %-10lu (%5.1f%%)\n",
13784 counts[0], (counts[0] * 100.0) / filedata->ngnubuckets);
13785 for (j = 1; j <= maxlength; ++j)
13786 {
13787 nzero_counts += counts[j] * j;
13788 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
13789 j, counts[j], (counts[j] * 100.0) / filedata->ngnubuckets,
13790 (nzero_counts * 100.0) / nsyms);
13791 }
13792 }
13793
13794 free (counts);
13795 free (lengths);
13796 }
13797 free (filedata->gnubuckets);
13798 filedata->gnubuckets = NULL;
13799 filedata->ngnubuckets = 0;
13800 free (filedata->gnuchains);
13801 filedata->gnuchains = NULL;
13802 filedata->ngnuchains = 0;
13803 free (filedata->mipsxlat);
13804 filedata->mipsxlat = NULL;
13805 return true;
13806
13807 err_out:
13808 free (filedata->gnubuckets);
13809 filedata->gnubuckets = NULL;
13810 filedata->ngnubuckets = 0;
13811 free (filedata->gnuchains);
13812 filedata->gnuchains = NULL;
13813 filedata->ngnuchains = 0;
13814 free (filedata->mipsxlat);
13815 filedata->mipsxlat = NULL;
13816 free (filedata->buckets);
13817 filedata->buckets = NULL;
13818 filedata->nbuckets = 0;
13819 free (filedata->chains);
13820 filedata->chains = NULL;
13821 return false;
13822 }
13823
13824 static bool
13825 process_syminfo (Filedata * filedata)
13826 {
13827 unsigned int i;
13828
13829 if (filedata->dynamic_syminfo == NULL
13830 || !do_dynamic)
13831 /* No syminfo, this is ok. */
13832 return true;
13833
13834 /* There better should be a dynamic symbol section. */
13835 if (filedata->dynamic_symbols == NULL || filedata->dynamic_strings == NULL)
13836 return false;
13837
13838 if (filedata->is_separate)
13839 printf (ngettext ("\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entry:\n",
13840 "\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entries:\n",
13841 filedata->dynamic_syminfo_nent),
13842 filedata->file_name,
13843 filedata->dynamic_syminfo_offset,
13844 filedata->dynamic_syminfo_nent);
13845 else
13846 printf (ngettext ("\nDynamic info segment at offset 0x%lx "
13847 "contains %d entry:\n",
13848 "\nDynamic info segment at offset 0x%lx "
13849 "contains %d entries:\n",
13850 filedata->dynamic_syminfo_nent),
13851 filedata->dynamic_syminfo_offset,
13852 filedata->dynamic_syminfo_nent);
13853
13854 printf (_(" Num: Name BoundTo Flags\n"));
13855 for (i = 0; i < filedata->dynamic_syminfo_nent; ++i)
13856 {
13857 unsigned short int flags = filedata->dynamic_syminfo[i].si_flags;
13858
13859 printf ("%4d: ", i);
13860 if (i >= filedata->num_dynamic_syms)
13861 printf (_("<corrupt index>"));
13862 else if (valid_dynamic_name (filedata, filedata->dynamic_symbols[i].st_name))
13863 print_symbol (30, get_dynamic_name (filedata,
13864 filedata->dynamic_symbols[i].st_name));
13865 else
13866 printf (_("<corrupt: %19ld>"), filedata->dynamic_symbols[i].st_name);
13867 putchar (' ');
13868
13869 switch (filedata->dynamic_syminfo[i].si_boundto)
13870 {
13871 case SYMINFO_BT_SELF:
13872 fputs ("SELF ", stdout);
13873 break;
13874 case SYMINFO_BT_PARENT:
13875 fputs ("PARENT ", stdout);
13876 break;
13877 default:
13878 if (filedata->dynamic_syminfo[i].si_boundto > 0
13879 && filedata->dynamic_syminfo[i].si_boundto < filedata->dynamic_nent
13880 && valid_dynamic_name (filedata,
13881 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val))
13882 {
13883 print_symbol (10, get_dynamic_name (filedata,
13884 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val));
13885 putchar (' ' );
13886 }
13887 else
13888 printf ("%-10d ", filedata->dynamic_syminfo[i].si_boundto);
13889 break;
13890 }
13891
13892 if (flags & SYMINFO_FLG_DIRECT)
13893 printf (" DIRECT");
13894 if (flags & SYMINFO_FLG_PASSTHRU)
13895 printf (" PASSTHRU");
13896 if (flags & SYMINFO_FLG_COPY)
13897 printf (" COPY");
13898 if (flags & SYMINFO_FLG_LAZYLOAD)
13899 printf (" LAZYLOAD");
13900
13901 puts ("");
13902 }
13903
13904 return true;
13905 }
13906
13907 /* A macro which evaluates to TRUE if the region ADDR .. ADDR + NELEM
13908 is contained by the region START .. END. The types of ADDR, START
13909 and END should all be the same. Note both ADDR + NELEM and END
13910 point to just beyond the end of the regions that are being tested. */
13911 #define IN_RANGE(START,END,ADDR,NELEM) \
13912 (((ADDR) >= (START)) && ((ADDR) < (END)) && ((ADDR) + (NELEM) <= (END)))
13913
13914 /* Check to see if the given reloc needs to be handled in a target specific
13915 manner. If so then process the reloc and return TRUE otherwise return
13916 FALSE.
13917
13918 If called with reloc == NULL, then this is a signal that reloc processing
13919 for the current section has finished, and any saved state should be
13920 discarded. */
13921
13922 static bool
13923 target_specific_reloc_handling (Filedata * filedata,
13924 Elf_Internal_Rela * reloc,
13925 unsigned char * start,
13926 unsigned char * end,
13927 Elf_Internal_Sym * symtab,
13928 unsigned long num_syms)
13929 {
13930 unsigned int reloc_type = 0;
13931 unsigned long sym_index = 0;
13932
13933 if (reloc)
13934 {
13935 reloc_type = get_reloc_type (filedata, reloc->r_info);
13936 sym_index = get_reloc_symindex (reloc->r_info);
13937 }
13938
13939 switch (filedata->file_header.e_machine)
13940 {
13941 case EM_MSP430:
13942 case EM_MSP430_OLD:
13943 {
13944 static Elf_Internal_Sym * saved_sym = NULL;
13945
13946 if (reloc == NULL)
13947 {
13948 saved_sym = NULL;
13949 return true;
13950 }
13951
13952 switch (reloc_type)
13953 {
13954 case 10: /* R_MSP430_SYM_DIFF */
13955 case 12: /* R_MSP430_GNU_SUB_ULEB128 */
13956 if (uses_msp430x_relocs (filedata))
13957 break;
13958 /* Fall through. */
13959 case 21: /* R_MSP430X_SYM_DIFF */
13960 case 23: /* R_MSP430X_GNU_SUB_ULEB128 */
13961 /* PR 21139. */
13962 if (sym_index >= num_syms)
13963 error (_("MSP430 SYM_DIFF reloc contains invalid symbol index %lu\n"),
13964 sym_index);
13965 else
13966 saved_sym = symtab + sym_index;
13967 return true;
13968
13969 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
13970 case 3: /* R_MSP430_16 or R_MSP430_ABS8 */
13971 goto handle_sym_diff;
13972
13973 case 5: /* R_MSP430_16_BYTE */
13974 case 9: /* R_MSP430_8 */
13975 case 11: /* R_MSP430_GNU_SET_ULEB128 */
13976 if (uses_msp430x_relocs (filedata))
13977 break;
13978 goto handle_sym_diff;
13979
13980 case 2: /* R_MSP430_ABS16 */
13981 case 15: /* R_MSP430X_ABS16 */
13982 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
13983 if (! uses_msp430x_relocs (filedata))
13984 break;
13985 goto handle_sym_diff;
13986
13987 handle_sym_diff:
13988 if (saved_sym != NULL)
13989 {
13990 uint64_t value;
13991 unsigned int reloc_size = 0;
13992 int leb_ret = 0;
13993 switch (reloc_type)
13994 {
13995 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
13996 reloc_size = 4;
13997 break;
13998 case 11: /* R_MSP430_GNU_SET_ULEB128 */
13999 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
14000 if (reloc->r_offset < (size_t) (end - start))
14001 read_leb128 (start + reloc->r_offset, end, false,
14002 &reloc_size, &leb_ret);
14003 break;
14004 default:
14005 reloc_size = 2;
14006 break;
14007 }
14008
14009 if (leb_ret != 0 || reloc_size == 0 || reloc_size > 8)
14010 error (_("MSP430 ULEB128 field at 0x%lx contains invalid "
14011 "ULEB128 value\n"),
14012 (long) reloc->r_offset);
14013 else if (sym_index >= num_syms)
14014 error (_("MSP430 reloc contains invalid symbol index %lu\n"),
14015 sym_index);
14016 else
14017 {
14018 value = reloc->r_addend + (symtab[sym_index].st_value
14019 - saved_sym->st_value);
14020
14021 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
14022 byte_put (start + reloc->r_offset, value, reloc_size);
14023 else
14024 /* PR 21137 */
14025 error (_("MSP430 sym diff reloc contains invalid offset: 0x%lx\n"),
14026 (long) reloc->r_offset);
14027 }
14028
14029 saved_sym = NULL;
14030 return true;
14031 }
14032 break;
14033
14034 default:
14035 if (saved_sym != NULL)
14036 error (_("Unhandled MSP430 reloc type found after SYM_DIFF reloc\n"));
14037 break;
14038 }
14039 break;
14040 }
14041
14042 case EM_MN10300:
14043 case EM_CYGNUS_MN10300:
14044 {
14045 static Elf_Internal_Sym * saved_sym = NULL;
14046
14047 if (reloc == NULL)
14048 {
14049 saved_sym = NULL;
14050 return true;
14051 }
14052
14053 switch (reloc_type)
14054 {
14055 case 34: /* R_MN10300_ALIGN */
14056 return true;
14057 case 33: /* R_MN10300_SYM_DIFF */
14058 if (sym_index >= num_syms)
14059 error (_("MN10300_SYM_DIFF reloc contains invalid symbol index %lu\n"),
14060 sym_index);
14061 else
14062 saved_sym = symtab + sym_index;
14063 return true;
14064
14065 case 1: /* R_MN10300_32 */
14066 case 2: /* R_MN10300_16 */
14067 if (saved_sym != NULL)
14068 {
14069 int reloc_size = reloc_type == 1 ? 4 : 2;
14070 uint64_t value;
14071
14072 if (sym_index >= num_syms)
14073 error (_("MN10300 reloc contains invalid symbol index %lu\n"),
14074 sym_index);
14075 else
14076 {
14077 value = reloc->r_addend + (symtab[sym_index].st_value
14078 - saved_sym->st_value);
14079
14080 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
14081 byte_put (start + reloc->r_offset, value, reloc_size);
14082 else
14083 error (_("MN10300 sym diff reloc contains invalid offset: 0x%lx\n"),
14084 (long) reloc->r_offset);
14085 }
14086
14087 saved_sym = NULL;
14088 return true;
14089 }
14090 break;
14091 default:
14092 if (saved_sym != NULL)
14093 error (_("Unhandled MN10300 reloc type found after SYM_DIFF reloc\n"));
14094 break;
14095 }
14096 break;
14097 }
14098
14099 case EM_RL78:
14100 {
14101 static uint64_t saved_sym1 = 0;
14102 static uint64_t saved_sym2 = 0;
14103 static uint64_t value;
14104
14105 if (reloc == NULL)
14106 {
14107 saved_sym1 = saved_sym2 = 0;
14108 return true;
14109 }
14110
14111 switch (reloc_type)
14112 {
14113 case 0x80: /* R_RL78_SYM. */
14114 saved_sym1 = saved_sym2;
14115 if (sym_index >= num_syms)
14116 error (_("RL78_SYM reloc contains invalid symbol index %lu\n"),
14117 sym_index);
14118 else
14119 {
14120 saved_sym2 = symtab[sym_index].st_value;
14121 saved_sym2 += reloc->r_addend;
14122 }
14123 return true;
14124
14125 case 0x83: /* R_RL78_OPsub. */
14126 value = saved_sym1 - saved_sym2;
14127 saved_sym2 = saved_sym1 = 0;
14128 return true;
14129 break;
14130
14131 case 0x41: /* R_RL78_ABS32. */
14132 if (IN_RANGE (start, end, start + reloc->r_offset, 4))
14133 byte_put (start + reloc->r_offset, value, 4);
14134 else
14135 error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
14136 (long) reloc->r_offset);
14137 value = 0;
14138 return true;
14139
14140 case 0x43: /* R_RL78_ABS16. */
14141 if (IN_RANGE (start, end, start + reloc->r_offset, 2))
14142 byte_put (start + reloc->r_offset, value, 2);
14143 else
14144 error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
14145 (long) reloc->r_offset);
14146 value = 0;
14147 return true;
14148
14149 default:
14150 break;
14151 }
14152 break;
14153 }
14154 }
14155
14156 return false;
14157 }
14158
14159 /* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
14160 DWARF debug sections. This is a target specific test. Note - we do not
14161 go through the whole including-target-headers-multiple-times route, (as
14162 we have already done with <elf/h8.h>) because this would become very
14163 messy and even then this function would have to contain target specific
14164 information (the names of the relocs instead of their numeric values).
14165 FIXME: This is not the correct way to solve this problem. The proper way
14166 is to have target specific reloc sizing and typing functions created by
14167 the reloc-macros.h header, in the same way that it already creates the
14168 reloc naming functions. */
14169
14170 static bool
14171 is_32bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14172 {
14173 /* Please keep this table alpha-sorted for ease of visual lookup. */
14174 switch (filedata->file_header.e_machine)
14175 {
14176 case EM_386:
14177 case EM_IAMCU:
14178 return reloc_type == 1; /* R_386_32. */
14179 case EM_68K:
14180 return reloc_type == 1; /* R_68K_32. */
14181 case EM_860:
14182 return reloc_type == 1; /* R_860_32. */
14183 case EM_960:
14184 return reloc_type == 2; /* R_960_32. */
14185 case EM_AARCH64:
14186 return (reloc_type == 258
14187 || reloc_type == 1); /* R_AARCH64_ABS32 || R_AARCH64_P32_ABS32 */
14188 case EM_BPF:
14189 return reloc_type == 11; /* R_BPF_DATA_32 */
14190 case EM_ADAPTEVA_EPIPHANY:
14191 return reloc_type == 3;
14192 case EM_ALPHA:
14193 return reloc_type == 1; /* R_ALPHA_REFLONG. */
14194 case EM_ARC:
14195 return reloc_type == 1; /* R_ARC_32. */
14196 case EM_ARC_COMPACT:
14197 case EM_ARC_COMPACT2:
14198 return reloc_type == 4; /* R_ARC_32. */
14199 case EM_ARM:
14200 return reloc_type == 2; /* R_ARM_ABS32 */
14201 case EM_AVR_OLD:
14202 case EM_AVR:
14203 return reloc_type == 1;
14204 case EM_BLACKFIN:
14205 return reloc_type == 0x12; /* R_byte4_data. */
14206 case EM_CRIS:
14207 return reloc_type == 3; /* R_CRIS_32. */
14208 case EM_CR16:
14209 return reloc_type == 3; /* R_CR16_NUM32. */
14210 case EM_CRX:
14211 return reloc_type == 15; /* R_CRX_NUM32. */
14212 case EM_CSKY:
14213 return reloc_type == 1; /* R_CKCORE_ADDR32. */
14214 case EM_CYGNUS_FRV:
14215 return reloc_type == 1;
14216 case EM_CYGNUS_D10V:
14217 case EM_D10V:
14218 return reloc_type == 6; /* R_D10V_32. */
14219 case EM_CYGNUS_D30V:
14220 case EM_D30V:
14221 return reloc_type == 12; /* R_D30V_32_NORMAL. */
14222 case EM_DLX:
14223 return reloc_type == 3; /* R_DLX_RELOC_32. */
14224 case EM_CYGNUS_FR30:
14225 case EM_FR30:
14226 return reloc_type == 3; /* R_FR30_32. */
14227 case EM_FT32:
14228 return reloc_type == 1; /* R_FT32_32. */
14229 case EM_H8S:
14230 case EM_H8_300:
14231 case EM_H8_300H:
14232 return reloc_type == 1; /* R_H8_DIR32. */
14233 case EM_IA_64:
14234 return (reloc_type == 0x64 /* R_IA64_SECREL32MSB. */
14235 || reloc_type == 0x65 /* R_IA64_SECREL32LSB. */
14236 || reloc_type == 0x24 /* R_IA64_DIR32MSB. */
14237 || reloc_type == 0x25 /* R_IA64_DIR32LSB. */);
14238 case EM_IP2K_OLD:
14239 case EM_IP2K:
14240 return reloc_type == 2; /* R_IP2K_32. */
14241 case EM_IQ2000:
14242 return reloc_type == 2; /* R_IQ2000_32. */
14243 case EM_LATTICEMICO32:
14244 return reloc_type == 3; /* R_LM32_32. */
14245 case EM_LOONGARCH:
14246 return reloc_type == 1; /* R_LARCH_32. */
14247 case EM_M32C_OLD:
14248 case EM_M32C:
14249 return reloc_type == 3; /* R_M32C_32. */
14250 case EM_M32R:
14251 return reloc_type == 34; /* R_M32R_32_RELA. */
14252 case EM_68HC11:
14253 case EM_68HC12:
14254 return reloc_type == 6; /* R_M68HC11_32. */
14255 case EM_S12Z:
14256 return reloc_type == 7 || /* R_S12Z_EXT32 */
14257 reloc_type == 6; /* R_S12Z_CW32. */
14258 case EM_MCORE:
14259 return reloc_type == 1; /* R_MCORE_ADDR32. */
14260 case EM_CYGNUS_MEP:
14261 return reloc_type == 4; /* R_MEP_32. */
14262 case EM_METAG:
14263 return reloc_type == 2; /* R_METAG_ADDR32. */
14264 case EM_MICROBLAZE:
14265 return reloc_type == 1; /* R_MICROBLAZE_32. */
14266 case EM_MIPS:
14267 return reloc_type == 2; /* R_MIPS_32. */
14268 case EM_MMIX:
14269 return reloc_type == 4; /* R_MMIX_32. */
14270 case EM_CYGNUS_MN10200:
14271 case EM_MN10200:
14272 return reloc_type == 1; /* R_MN10200_32. */
14273 case EM_CYGNUS_MN10300:
14274 case EM_MN10300:
14275 return reloc_type == 1; /* R_MN10300_32. */
14276 case EM_MOXIE:
14277 return reloc_type == 1; /* R_MOXIE_32. */
14278 case EM_MSP430_OLD:
14279 case EM_MSP430:
14280 return reloc_type == 1; /* R_MSP430_32 or R_MSP320_ABS32. */
14281 case EM_MT:
14282 return reloc_type == 2; /* R_MT_32. */
14283 case EM_NDS32:
14284 return reloc_type == 20; /* R_NDS32_32_RELA. */
14285 case EM_ALTERA_NIOS2:
14286 return reloc_type == 12; /* R_NIOS2_BFD_RELOC_32. */
14287 case EM_NIOS32:
14288 return reloc_type == 1; /* R_NIOS_32. */
14289 case EM_OR1K:
14290 return reloc_type == 1; /* R_OR1K_32. */
14291 case EM_PARISC:
14292 return (reloc_type == 1 /* R_PARISC_DIR32. */
14293 || reloc_type == 2 /* R_PARISC_DIR21L. */
14294 || reloc_type == 41); /* R_PARISC_SECREL32. */
14295 case EM_PJ:
14296 case EM_PJ_OLD:
14297 return reloc_type == 1; /* R_PJ_DATA_DIR32. */
14298 case EM_PPC64:
14299 return reloc_type == 1; /* R_PPC64_ADDR32. */
14300 case EM_PPC:
14301 return reloc_type == 1; /* R_PPC_ADDR32. */
14302 case EM_TI_PRU:
14303 return reloc_type == 11; /* R_PRU_BFD_RELOC_32. */
14304 case EM_RISCV:
14305 return reloc_type == 1; /* R_RISCV_32. */
14306 case EM_RL78:
14307 return reloc_type == 1; /* R_RL78_DIR32. */
14308 case EM_RX:
14309 return reloc_type == 1; /* R_RX_DIR32. */
14310 case EM_S370:
14311 return reloc_type == 1; /* R_I370_ADDR31. */
14312 case EM_S390_OLD:
14313 case EM_S390:
14314 return reloc_type == 4; /* R_S390_32. */
14315 case EM_SCORE:
14316 return reloc_type == 8; /* R_SCORE_ABS32. */
14317 case EM_SH:
14318 return reloc_type == 1; /* R_SH_DIR32. */
14319 case EM_SPARC32PLUS:
14320 case EM_SPARCV9:
14321 case EM_SPARC:
14322 return reloc_type == 3 /* R_SPARC_32. */
14323 || reloc_type == 23; /* R_SPARC_UA32. */
14324 case EM_SPU:
14325 return reloc_type == 6; /* R_SPU_ADDR32 */
14326 case EM_TI_C6000:
14327 return reloc_type == 1; /* R_C6000_ABS32. */
14328 case EM_TILEGX:
14329 return reloc_type == 2; /* R_TILEGX_32. */
14330 case EM_TILEPRO:
14331 return reloc_type == 1; /* R_TILEPRO_32. */
14332 case EM_CYGNUS_V850:
14333 case EM_V850:
14334 return reloc_type == 6; /* R_V850_ABS32. */
14335 case EM_V800:
14336 return reloc_type == 0x33; /* R_V810_WORD. */
14337 case EM_VAX:
14338 return reloc_type == 1; /* R_VAX_32. */
14339 case EM_VISIUM:
14340 return reloc_type == 3; /* R_VISIUM_32. */
14341 case EM_WEBASSEMBLY:
14342 return reloc_type == 1; /* R_WASM32_32. */
14343 case EM_X86_64:
14344 case EM_L1OM:
14345 case EM_K1OM:
14346 return reloc_type == 10; /* R_X86_64_32. */
14347 case EM_XGATE:
14348 return reloc_type == 4; /* R_XGATE_32. */
14349 case EM_XSTORMY16:
14350 return reloc_type == 1; /* R_XSTROMY16_32. */
14351 case EM_XTENSA_OLD:
14352 case EM_XTENSA:
14353 return reloc_type == 1; /* R_XTENSA_32. */
14354 case EM_Z80:
14355 return reloc_type == 6; /* R_Z80_32. */
14356 default:
14357 {
14358 static unsigned int prev_warn = 0;
14359
14360 /* Avoid repeating the same warning multiple times. */
14361 if (prev_warn != filedata->file_header.e_machine)
14362 error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
14363 filedata->file_header.e_machine);
14364 prev_warn = filedata->file_header.e_machine;
14365 return false;
14366 }
14367 }
14368 }
14369
14370 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14371 a 32-bit pc-relative RELA relocation used in DWARF debug sections. */
14372
14373 static bool
14374 is_32bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
14375 {
14376 switch (filedata->file_header.e_machine)
14377 /* Please keep this table alpha-sorted for ease of visual lookup. */
14378 {
14379 case EM_386:
14380 case EM_IAMCU:
14381 return reloc_type == 2; /* R_386_PC32. */
14382 case EM_68K:
14383 return reloc_type == 4; /* R_68K_PC32. */
14384 case EM_AARCH64:
14385 return reloc_type == 261; /* R_AARCH64_PREL32 */
14386 case EM_ADAPTEVA_EPIPHANY:
14387 return reloc_type == 6;
14388 case EM_ALPHA:
14389 return reloc_type == 10; /* R_ALPHA_SREL32. */
14390 case EM_ARC_COMPACT:
14391 case EM_ARC_COMPACT2:
14392 return reloc_type == 49; /* R_ARC_32_PCREL. */
14393 case EM_ARM:
14394 return reloc_type == 3; /* R_ARM_REL32 */
14395 case EM_AVR_OLD:
14396 case EM_AVR:
14397 return reloc_type == 36; /* R_AVR_32_PCREL. */
14398 case EM_LOONGARCH:
14399 return reloc_type == 99; /* R_LARCH_32_PCREL. */
14400 case EM_MICROBLAZE:
14401 return reloc_type == 2; /* R_MICROBLAZE_32_PCREL. */
14402 case EM_OR1K:
14403 return reloc_type == 9; /* R_OR1K_32_PCREL. */
14404 case EM_PARISC:
14405 return reloc_type == 9; /* R_PARISC_PCREL32. */
14406 case EM_PPC:
14407 return reloc_type == 26; /* R_PPC_REL32. */
14408 case EM_PPC64:
14409 return reloc_type == 26; /* R_PPC64_REL32. */
14410 case EM_RISCV:
14411 return reloc_type == 57; /* R_RISCV_32_PCREL. */
14412 case EM_S390_OLD:
14413 case EM_S390:
14414 return reloc_type == 5; /* R_390_PC32. */
14415 case EM_SH:
14416 return reloc_type == 2; /* R_SH_REL32. */
14417 case EM_SPARC32PLUS:
14418 case EM_SPARCV9:
14419 case EM_SPARC:
14420 return reloc_type == 6; /* R_SPARC_DISP32. */
14421 case EM_SPU:
14422 return reloc_type == 13; /* R_SPU_REL32. */
14423 case EM_TILEGX:
14424 return reloc_type == 6; /* R_TILEGX_32_PCREL. */
14425 case EM_TILEPRO:
14426 return reloc_type == 4; /* R_TILEPRO_32_PCREL. */
14427 case EM_VISIUM:
14428 return reloc_type == 6; /* R_VISIUM_32_PCREL */
14429 case EM_X86_64:
14430 case EM_L1OM:
14431 case EM_K1OM:
14432 return reloc_type == 2; /* R_X86_64_PC32. */
14433 case EM_VAX:
14434 return reloc_type == 4; /* R_VAX_PCREL32. */
14435 case EM_XTENSA_OLD:
14436 case EM_XTENSA:
14437 return reloc_type == 14; /* R_XTENSA_32_PCREL. */
14438 default:
14439 /* Do not abort or issue an error message here. Not all targets use
14440 pc-relative 32-bit relocs in their DWARF debug information and we
14441 have already tested for target coverage in is_32bit_abs_reloc. A
14442 more helpful warning message will be generated by apply_relocations
14443 anyway, so just return. */
14444 return false;
14445 }
14446 }
14447
14448 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14449 a 64-bit absolute RELA relocation used in DWARF debug sections. */
14450
14451 static bool
14452 is_64bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14453 {
14454 switch (filedata->file_header.e_machine)
14455 {
14456 case EM_AARCH64:
14457 return reloc_type == 257; /* R_AARCH64_ABS64. */
14458 case EM_ALPHA:
14459 return reloc_type == 2; /* R_ALPHA_REFQUAD. */
14460 case EM_IA_64:
14461 return (reloc_type == 0x26 /* R_IA64_DIR64MSB. */
14462 || reloc_type == 0x27 /* R_IA64_DIR64LSB. */);
14463 case EM_LOONGARCH:
14464 return reloc_type == 2; /* R_LARCH_64 */
14465 case EM_PARISC:
14466 return reloc_type == 80; /* R_PARISC_DIR64. */
14467 case EM_PPC64:
14468 return reloc_type == 38; /* R_PPC64_ADDR64. */
14469 case EM_RISCV:
14470 return reloc_type == 2; /* R_RISCV_64. */
14471 case EM_SPARC32PLUS:
14472 case EM_SPARCV9:
14473 case EM_SPARC:
14474 return reloc_type == 32 /* R_SPARC_64. */
14475 || reloc_type == 54; /* R_SPARC_UA64. */
14476 case EM_X86_64:
14477 case EM_L1OM:
14478 case EM_K1OM:
14479 return reloc_type == 1; /* R_X86_64_64. */
14480 case EM_S390_OLD:
14481 case EM_S390:
14482 return reloc_type == 22; /* R_S390_64. */
14483 case EM_TILEGX:
14484 return reloc_type == 1; /* R_TILEGX_64. */
14485 case EM_MIPS:
14486 return reloc_type == 18; /* R_MIPS_64. */
14487 default:
14488 return false;
14489 }
14490 }
14491
14492 /* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
14493 a 64-bit pc-relative RELA relocation used in DWARF debug sections. */
14494
14495 static bool
14496 is_64bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
14497 {
14498 switch (filedata->file_header.e_machine)
14499 {
14500 case EM_AARCH64:
14501 return reloc_type == 260; /* R_AARCH64_PREL64. */
14502 case EM_ALPHA:
14503 return reloc_type == 11; /* R_ALPHA_SREL64. */
14504 case EM_IA_64:
14505 return (reloc_type == 0x4e /* R_IA64_PCREL64MSB. */
14506 || reloc_type == 0x4f /* R_IA64_PCREL64LSB. */);
14507 case EM_PARISC:
14508 return reloc_type == 72; /* R_PARISC_PCREL64. */
14509 case EM_PPC64:
14510 return reloc_type == 44; /* R_PPC64_REL64. */
14511 case EM_SPARC32PLUS:
14512 case EM_SPARCV9:
14513 case EM_SPARC:
14514 return reloc_type == 46; /* R_SPARC_DISP64. */
14515 case EM_X86_64:
14516 case EM_L1OM:
14517 case EM_K1OM:
14518 return reloc_type == 24; /* R_X86_64_PC64. */
14519 case EM_S390_OLD:
14520 case EM_S390:
14521 return reloc_type == 23; /* R_S390_PC64. */
14522 case EM_TILEGX:
14523 return reloc_type == 5; /* R_TILEGX_64_PCREL. */
14524 default:
14525 return false;
14526 }
14527 }
14528
14529 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14530 a 24-bit absolute RELA relocation used in DWARF debug sections. */
14531
14532 static bool
14533 is_24bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14534 {
14535 switch (filedata->file_header.e_machine)
14536 {
14537 case EM_CYGNUS_MN10200:
14538 case EM_MN10200:
14539 return reloc_type == 4; /* R_MN10200_24. */
14540 case EM_FT32:
14541 return reloc_type == 5; /* R_FT32_20. */
14542 case EM_Z80:
14543 return reloc_type == 5; /* R_Z80_24. */
14544 default:
14545 return false;
14546 }
14547 }
14548
14549 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14550 a 16-bit absolute RELA relocation used in DWARF debug sections. */
14551
14552 static bool
14553 is_16bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14554 {
14555 /* Please keep this table alpha-sorted for ease of visual lookup. */
14556 switch (filedata->file_header.e_machine)
14557 {
14558 case EM_ARC:
14559 case EM_ARC_COMPACT:
14560 case EM_ARC_COMPACT2:
14561 return reloc_type == 2; /* R_ARC_16. */
14562 case EM_ADAPTEVA_EPIPHANY:
14563 return reloc_type == 5;
14564 case EM_AVR_OLD:
14565 case EM_AVR:
14566 return reloc_type == 4; /* R_AVR_16. */
14567 case EM_CYGNUS_D10V:
14568 case EM_D10V:
14569 return reloc_type == 3; /* R_D10V_16. */
14570 case EM_FT32:
14571 return reloc_type == 2; /* R_FT32_16. */
14572 case EM_H8S:
14573 case EM_H8_300:
14574 case EM_H8_300H:
14575 return reloc_type == R_H8_DIR16;
14576 case EM_IP2K_OLD:
14577 case EM_IP2K:
14578 return reloc_type == 1; /* R_IP2K_16. */
14579 case EM_M32C_OLD:
14580 case EM_M32C:
14581 return reloc_type == 1; /* R_M32C_16 */
14582 case EM_CYGNUS_MN10200:
14583 case EM_MN10200:
14584 return reloc_type == 2; /* R_MN10200_16. */
14585 case EM_CYGNUS_MN10300:
14586 case EM_MN10300:
14587 return reloc_type == 2; /* R_MN10300_16. */
14588 case EM_MSP430:
14589 if (uses_msp430x_relocs (filedata))
14590 return reloc_type == 2; /* R_MSP430_ABS16. */
14591 /* Fall through. */
14592 case EM_MSP430_OLD:
14593 return reloc_type == 5; /* R_MSP430_16_BYTE. */
14594 case EM_NDS32:
14595 return reloc_type == 19; /* R_NDS32_16_RELA. */
14596 case EM_ALTERA_NIOS2:
14597 return reloc_type == 13; /* R_NIOS2_BFD_RELOC_16. */
14598 case EM_NIOS32:
14599 return reloc_type == 9; /* R_NIOS_16. */
14600 case EM_OR1K:
14601 return reloc_type == 2; /* R_OR1K_16. */
14602 case EM_RISCV:
14603 return reloc_type == 55; /* R_RISCV_SET16. */
14604 case EM_TI_PRU:
14605 return reloc_type == 8; /* R_PRU_BFD_RELOC_16. */
14606 case EM_TI_C6000:
14607 return reloc_type == 2; /* R_C6000_ABS16. */
14608 case EM_VISIUM:
14609 return reloc_type == 2; /* R_VISIUM_16. */
14610 case EM_XGATE:
14611 return reloc_type == 3; /* R_XGATE_16. */
14612 case EM_Z80:
14613 return reloc_type == 4; /* R_Z80_16. */
14614 default:
14615 return false;
14616 }
14617 }
14618
14619 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14620 a 8-bit absolute RELA relocation used in DWARF debug sections. */
14621
14622 static bool
14623 is_8bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14624 {
14625 switch (filedata->file_header.e_machine)
14626 {
14627 case EM_RISCV:
14628 return reloc_type == 54; /* R_RISCV_SET8. */
14629 case EM_Z80:
14630 return reloc_type == 1; /* R_Z80_8. */
14631 default:
14632 return false;
14633 }
14634 }
14635
14636 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14637 a 6-bit absolute RELA relocation used in DWARF debug sections. */
14638
14639 static bool
14640 is_6bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14641 {
14642 switch (filedata->file_header.e_machine)
14643 {
14644 case EM_RISCV:
14645 return reloc_type == 53; /* R_RISCV_SET6. */
14646 default:
14647 return false;
14648 }
14649 }
14650
14651 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14652 a 32-bit inplace add RELA relocation used in DWARF debug sections. */
14653
14654 static bool
14655 is_32bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14656 {
14657 /* Please keep this table alpha-sorted for ease of visual lookup. */
14658 switch (filedata->file_header.e_machine)
14659 {
14660 case EM_RISCV:
14661 return reloc_type == 35; /* R_RISCV_ADD32. */
14662 default:
14663 return false;
14664 }
14665 }
14666
14667 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14668 a 32-bit inplace sub RELA relocation used in DWARF debug sections. */
14669
14670 static bool
14671 is_32bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14672 {
14673 /* Please keep this table alpha-sorted for ease of visual lookup. */
14674 switch (filedata->file_header.e_machine)
14675 {
14676 case EM_RISCV:
14677 return reloc_type == 39; /* R_RISCV_SUB32. */
14678 default:
14679 return false;
14680 }
14681 }
14682
14683 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14684 a 64-bit inplace add RELA relocation used in DWARF debug sections. */
14685
14686 static bool
14687 is_64bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14688 {
14689 /* Please keep this table alpha-sorted for ease of visual lookup. */
14690 switch (filedata->file_header.e_machine)
14691 {
14692 case EM_RISCV:
14693 return reloc_type == 36; /* R_RISCV_ADD64. */
14694 default:
14695 return false;
14696 }
14697 }
14698
14699 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14700 a 64-bit inplace sub RELA relocation used in DWARF debug sections. */
14701
14702 static bool
14703 is_64bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14704 {
14705 /* Please keep this table alpha-sorted for ease of visual lookup. */
14706 switch (filedata->file_header.e_machine)
14707 {
14708 case EM_RISCV:
14709 return reloc_type == 40; /* R_RISCV_SUB64. */
14710 default:
14711 return false;
14712 }
14713 }
14714
14715 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14716 a 16-bit inplace add RELA relocation used in DWARF debug sections. */
14717
14718 static bool
14719 is_16bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14720 {
14721 /* Please keep this table alpha-sorted for ease of visual lookup. */
14722 switch (filedata->file_header.e_machine)
14723 {
14724 case EM_RISCV:
14725 return reloc_type == 34; /* R_RISCV_ADD16. */
14726 default:
14727 return false;
14728 }
14729 }
14730
14731 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14732 a 16-bit inplace sub RELA relocation used in DWARF debug sections. */
14733
14734 static bool
14735 is_16bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14736 {
14737 /* Please keep this table alpha-sorted for ease of visual lookup. */
14738 switch (filedata->file_header.e_machine)
14739 {
14740 case EM_RISCV:
14741 return reloc_type == 38; /* R_RISCV_SUB16. */
14742 default:
14743 return false;
14744 }
14745 }
14746
14747 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14748 a 8-bit inplace add RELA relocation used in DWARF debug sections. */
14749
14750 static bool
14751 is_8bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14752 {
14753 /* Please keep this table alpha-sorted for ease of visual lookup. */
14754 switch (filedata->file_header.e_machine)
14755 {
14756 case EM_RISCV:
14757 return reloc_type == 33; /* R_RISCV_ADD8. */
14758 default:
14759 return false;
14760 }
14761 }
14762
14763 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14764 a 8-bit inplace sub RELA relocation used in DWARF debug sections. */
14765
14766 static bool
14767 is_8bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14768 {
14769 /* Please keep this table alpha-sorted for ease of visual lookup. */
14770 switch (filedata->file_header.e_machine)
14771 {
14772 case EM_RISCV:
14773 return reloc_type == 37; /* R_RISCV_SUB8. */
14774 default:
14775 return false;
14776 }
14777 }
14778
14779 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14780 a 6-bit inplace sub RELA relocation used in DWARF debug sections. */
14781
14782 static bool
14783 is_6bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14784 {
14785 switch (filedata->file_header.e_machine)
14786 {
14787 case EM_RISCV:
14788 return reloc_type == 52; /* R_RISCV_SUB6. */
14789 default:
14790 return false;
14791 }
14792 }
14793
14794 /* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
14795 relocation entries (possibly formerly used for SHT_GROUP sections). */
14796
14797 static bool
14798 is_none_reloc (Filedata * filedata, unsigned int reloc_type)
14799 {
14800 switch (filedata->file_header.e_machine)
14801 {
14802 case EM_386: /* R_386_NONE. */
14803 case EM_68K: /* R_68K_NONE. */
14804 case EM_ADAPTEVA_EPIPHANY:
14805 case EM_ALPHA: /* R_ALPHA_NONE. */
14806 case EM_ALTERA_NIOS2: /* R_NIOS2_NONE. */
14807 case EM_ARC: /* R_ARC_NONE. */
14808 case EM_ARC_COMPACT2: /* R_ARC_NONE. */
14809 case EM_ARC_COMPACT: /* R_ARC_NONE. */
14810 case EM_ARM: /* R_ARM_NONE. */
14811 case EM_CRIS: /* R_CRIS_NONE. */
14812 case EM_FT32: /* R_FT32_NONE. */
14813 case EM_IA_64: /* R_IA64_NONE. */
14814 case EM_K1OM: /* R_X86_64_NONE. */
14815 case EM_L1OM: /* R_X86_64_NONE. */
14816 case EM_M32R: /* R_M32R_NONE. */
14817 case EM_MIPS: /* R_MIPS_NONE. */
14818 case EM_MN10300: /* R_MN10300_NONE. */
14819 case EM_MOXIE: /* R_MOXIE_NONE. */
14820 case EM_NIOS32: /* R_NIOS_NONE. */
14821 case EM_OR1K: /* R_OR1K_NONE. */
14822 case EM_PARISC: /* R_PARISC_NONE. */
14823 case EM_PPC64: /* R_PPC64_NONE. */
14824 case EM_PPC: /* R_PPC_NONE. */
14825 case EM_RISCV: /* R_RISCV_NONE. */
14826 case EM_S390: /* R_390_NONE. */
14827 case EM_S390_OLD:
14828 case EM_SH: /* R_SH_NONE. */
14829 case EM_SPARC32PLUS:
14830 case EM_SPARC: /* R_SPARC_NONE. */
14831 case EM_SPARCV9:
14832 case EM_TILEGX: /* R_TILEGX_NONE. */
14833 case EM_TILEPRO: /* R_TILEPRO_NONE. */
14834 case EM_TI_C6000:/* R_C6000_NONE. */
14835 case EM_X86_64: /* R_X86_64_NONE. */
14836 case EM_Z80: /* R_Z80_NONE. */
14837 case EM_WEBASSEMBLY: /* R_WASM32_NONE. */
14838 return reloc_type == 0;
14839
14840 case EM_AARCH64:
14841 return reloc_type == 0 || reloc_type == 256;
14842 case EM_AVR_OLD:
14843 case EM_AVR:
14844 return (reloc_type == 0 /* R_AVR_NONE. */
14845 || reloc_type == 30 /* R_AVR_DIFF8. */
14846 || reloc_type == 31 /* R_AVR_DIFF16. */
14847 || reloc_type == 32 /* R_AVR_DIFF32. */);
14848 case EM_METAG:
14849 return reloc_type == 3; /* R_METAG_NONE. */
14850 case EM_NDS32:
14851 return (reloc_type == 0 /* R_NDS32_NONE. */
14852 || reloc_type == 205 /* R_NDS32_DIFF8. */
14853 || reloc_type == 206 /* R_NDS32_DIFF16. */
14854 || reloc_type == 207 /* R_NDS32_DIFF32. */
14855 || reloc_type == 208 /* R_NDS32_DIFF_ULEB128. */);
14856 case EM_TI_PRU:
14857 return (reloc_type == 0 /* R_PRU_NONE. */
14858 || reloc_type == 65 /* R_PRU_DIFF8. */
14859 || reloc_type == 66 /* R_PRU_DIFF16. */
14860 || reloc_type == 67 /* R_PRU_DIFF32. */);
14861 case EM_XTENSA_OLD:
14862 case EM_XTENSA:
14863 return (reloc_type == 0 /* R_XTENSA_NONE. */
14864 || reloc_type == 17 /* R_XTENSA_DIFF8. */
14865 || reloc_type == 18 /* R_XTENSA_DIFF16. */
14866 || reloc_type == 19 /* R_XTENSA_DIFF32. */
14867 || reloc_type == 57 /* R_XTENSA_PDIFF8. */
14868 || reloc_type == 58 /* R_XTENSA_PDIFF16. */
14869 || reloc_type == 59 /* R_XTENSA_PDIFF32. */
14870 || reloc_type == 60 /* R_XTENSA_NDIFF8. */
14871 || reloc_type == 61 /* R_XTENSA_NDIFF16. */
14872 || reloc_type == 62 /* R_XTENSA_NDIFF32. */);
14873 }
14874 return false;
14875 }
14876
14877 /* Returns TRUE if there is a relocation against
14878 section NAME at OFFSET bytes. */
14879
14880 bool
14881 reloc_at (struct dwarf_section * dsec, uint64_t offset)
14882 {
14883 Elf_Internal_Rela * relocs;
14884 Elf_Internal_Rela * rp;
14885
14886 if (dsec == NULL || dsec->reloc_info == NULL)
14887 return false;
14888
14889 relocs = (Elf_Internal_Rela *) dsec->reloc_info;
14890
14891 for (rp = relocs; rp < relocs + dsec->num_relocs; ++rp)
14892 if (rp->r_offset == offset)
14893 return true;
14894
14895 return false;
14896 }
14897
14898 /* Apply relocations to a section.
14899 Returns TRUE upon success, FALSE otherwise.
14900 If RELOCS_RETURN is non-NULL then it is set to point to the loaded relocs.
14901 It is then the caller's responsibility to free them. NUM_RELOCS_RETURN
14902 will be set to the number of relocs loaded.
14903
14904 Note: So far support has been added only for those relocations
14905 which can be found in debug sections. FIXME: Add support for
14906 more relocations ? */
14907
14908 static bool
14909 apply_relocations (Filedata *filedata,
14910 const Elf_Internal_Shdr *section,
14911 unsigned char *start,
14912 size_t size,
14913 void **relocs_return,
14914 unsigned long *num_relocs_return)
14915 {
14916 Elf_Internal_Shdr * relsec;
14917 unsigned char * end = start + size;
14918
14919 if (relocs_return != NULL)
14920 {
14921 * (Elf_Internal_Rela **) relocs_return = NULL;
14922 * num_relocs_return = 0;
14923 }
14924
14925 if (filedata->file_header.e_type != ET_REL)
14926 /* No relocs to apply. */
14927 return true;
14928
14929 /* Find the reloc section associated with the section. */
14930 for (relsec = filedata->section_headers;
14931 relsec < filedata->section_headers + filedata->file_header.e_shnum;
14932 ++relsec)
14933 {
14934 bool is_rela;
14935 unsigned long num_relocs;
14936 Elf_Internal_Rela * relocs;
14937 Elf_Internal_Rela * rp;
14938 Elf_Internal_Shdr * symsec;
14939 Elf_Internal_Sym * symtab;
14940 unsigned long num_syms;
14941 Elf_Internal_Sym * sym;
14942
14943 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
14944 || relsec->sh_info >= filedata->file_header.e_shnum
14945 || filedata->section_headers + relsec->sh_info != section
14946 || relsec->sh_size == 0
14947 || relsec->sh_link >= filedata->file_header.e_shnum)
14948 continue;
14949
14950 symsec = filedata->section_headers + relsec->sh_link;
14951 if (symsec->sh_type != SHT_SYMTAB
14952 && symsec->sh_type != SHT_DYNSYM)
14953 return false;
14954
14955 is_rela = relsec->sh_type == SHT_RELA;
14956
14957 if (is_rela)
14958 {
14959 if (!slurp_rela_relocs (filedata, relsec->sh_offset,
14960 relsec->sh_size, & relocs, & num_relocs))
14961 return false;
14962 }
14963 else
14964 {
14965 if (!slurp_rel_relocs (filedata, relsec->sh_offset,
14966 relsec->sh_size, & relocs, & num_relocs))
14967 return false;
14968 }
14969
14970 /* SH uses RELA but uses in place value instead of the addend field. */
14971 if (filedata->file_header.e_machine == EM_SH)
14972 is_rela = false;
14973
14974 symtab = get_elf_symbols (filedata, symsec, & num_syms);
14975
14976 for (rp = relocs; rp < relocs + num_relocs; ++rp)
14977 {
14978 uint64_t addend;
14979 unsigned int reloc_type;
14980 unsigned int reloc_size;
14981 bool reloc_inplace = false;
14982 bool reloc_subtract = false;
14983 unsigned char *rloc;
14984 unsigned long sym_index;
14985
14986 reloc_type = get_reloc_type (filedata, rp->r_info);
14987
14988 if (target_specific_reloc_handling (filedata, rp, start, end, symtab, num_syms))
14989 continue;
14990 else if (is_none_reloc (filedata, reloc_type))
14991 continue;
14992 else if (is_32bit_abs_reloc (filedata, reloc_type)
14993 || is_32bit_pcrel_reloc (filedata, reloc_type))
14994 reloc_size = 4;
14995 else if (is_64bit_abs_reloc (filedata, reloc_type)
14996 || is_64bit_pcrel_reloc (filedata, reloc_type))
14997 reloc_size = 8;
14998 else if (is_24bit_abs_reloc (filedata, reloc_type))
14999 reloc_size = 3;
15000 else if (is_16bit_abs_reloc (filedata, reloc_type))
15001 reloc_size = 2;
15002 else if (is_8bit_abs_reloc (filedata, reloc_type)
15003 || is_6bit_abs_reloc (filedata, reloc_type))
15004 reloc_size = 1;
15005 else if ((reloc_subtract = is_32bit_inplace_sub_reloc (filedata,
15006 reloc_type))
15007 || is_32bit_inplace_add_reloc (filedata, reloc_type))
15008 {
15009 reloc_size = 4;
15010 reloc_inplace = true;
15011 }
15012 else if ((reloc_subtract = is_64bit_inplace_sub_reloc (filedata,
15013 reloc_type))
15014 || is_64bit_inplace_add_reloc (filedata, reloc_type))
15015 {
15016 reloc_size = 8;
15017 reloc_inplace = true;
15018 }
15019 else if ((reloc_subtract = is_16bit_inplace_sub_reloc (filedata,
15020 reloc_type))
15021 || is_16bit_inplace_add_reloc (filedata, reloc_type))
15022 {
15023 reloc_size = 2;
15024 reloc_inplace = true;
15025 }
15026 else if ((reloc_subtract = is_8bit_inplace_sub_reloc (filedata,
15027 reloc_type))
15028 || is_8bit_inplace_add_reloc (filedata, reloc_type))
15029 {
15030 reloc_size = 1;
15031 reloc_inplace = true;
15032 }
15033 else if ((reloc_subtract = is_6bit_inplace_sub_reloc (filedata,
15034 reloc_type)))
15035 {
15036 reloc_size = 1;
15037 reloc_inplace = true;
15038 }
15039 else
15040 {
15041 static unsigned int prev_reloc = 0;
15042
15043 if (reloc_type != prev_reloc)
15044 warn (_("unable to apply unsupported reloc type %d to section %s\n"),
15045 reloc_type, printable_section_name (filedata, section));
15046 prev_reloc = reloc_type;
15047 continue;
15048 }
15049
15050 rloc = start + rp->r_offset;
15051 if (!IN_RANGE (start, end, rloc, reloc_size))
15052 {
15053 warn (_("skipping invalid relocation offset 0x%lx in section %s\n"),
15054 (unsigned long) rp->r_offset,
15055 printable_section_name (filedata, section));
15056 continue;
15057 }
15058
15059 sym_index = (unsigned long) get_reloc_symindex (rp->r_info);
15060 if (sym_index >= num_syms)
15061 {
15062 warn (_("skipping invalid relocation symbol index 0x%lx in section %s\n"),
15063 sym_index, printable_section_name (filedata, section));
15064 continue;
15065 }
15066 sym = symtab + sym_index;
15067
15068 /* If the reloc has a symbol associated with it,
15069 make sure that it is of an appropriate type.
15070
15071 Relocations against symbols without type can happen.
15072 Gcc -feliminate-dwarf2-dups may generate symbols
15073 without type for debug info.
15074
15075 Icc generates relocations against function symbols
15076 instead of local labels.
15077
15078 Relocations against object symbols can happen, eg when
15079 referencing a global array. For an example of this see
15080 the _clz.o binary in libgcc.a. */
15081 if (sym != symtab
15082 && ELF_ST_TYPE (sym->st_info) != STT_COMMON
15083 && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
15084 {
15085 warn (_("skipping unexpected symbol type %s in section %s relocation %ld\n"),
15086 get_symbol_type (filedata, ELF_ST_TYPE (sym->st_info)),
15087 printable_section_name (filedata, relsec),
15088 (long int)(rp - relocs));
15089 continue;
15090 }
15091
15092 addend = 0;
15093 if (is_rela)
15094 addend += rp->r_addend;
15095 /* R_XTENSA_32, R_PJ_DATA_DIR32 and R_D30V_32_NORMAL are
15096 partial_inplace. */
15097 if (!is_rela
15098 || (filedata->file_header.e_machine == EM_XTENSA
15099 && reloc_type == 1)
15100 || ((filedata->file_header.e_machine == EM_PJ
15101 || filedata->file_header.e_machine == EM_PJ_OLD)
15102 && reloc_type == 1)
15103 || ((filedata->file_header.e_machine == EM_D30V
15104 || filedata->file_header.e_machine == EM_CYGNUS_D30V)
15105 && reloc_type == 12)
15106 || reloc_inplace)
15107 {
15108 if (is_6bit_inplace_sub_reloc (filedata, reloc_type))
15109 addend += byte_get (rloc, reloc_size) & 0x3f;
15110 else
15111 addend += byte_get (rloc, reloc_size);
15112 }
15113
15114 if (is_32bit_pcrel_reloc (filedata, reloc_type)
15115 || is_64bit_pcrel_reloc (filedata, reloc_type))
15116 {
15117 /* On HPPA, all pc-relative relocations are biased by 8. */
15118 if (filedata->file_header.e_machine == EM_PARISC)
15119 addend -= 8;
15120 byte_put (rloc, (addend + sym->st_value) - rp->r_offset,
15121 reloc_size);
15122 }
15123 else if (is_6bit_abs_reloc (filedata, reloc_type)
15124 || is_6bit_inplace_sub_reloc (filedata, reloc_type))
15125 {
15126 if (reloc_subtract)
15127 addend -= sym->st_value;
15128 else
15129 addend += sym->st_value;
15130 addend = (addend & 0x3f) | (byte_get (rloc, reloc_size) & 0xc0);
15131 byte_put (rloc, addend, reloc_size);
15132 }
15133 else if (reloc_subtract)
15134 byte_put (rloc, addend - sym->st_value, reloc_size);
15135 else
15136 byte_put (rloc, addend + sym->st_value, reloc_size);
15137 }
15138
15139 free (symtab);
15140 /* Let the target specific reloc processing code know that
15141 we have finished with these relocs. */
15142 target_specific_reloc_handling (filedata, NULL, NULL, NULL, NULL, 0);
15143
15144 if (relocs_return)
15145 {
15146 * (Elf_Internal_Rela **) relocs_return = relocs;
15147 * num_relocs_return = num_relocs;
15148 }
15149 else
15150 free (relocs);
15151
15152 break;
15153 }
15154
15155 return true;
15156 }
15157
15158 #ifdef SUPPORT_DISASSEMBLY
15159 static bool
15160 disassemble_section (Elf_Internal_Shdr * section, Filedata * filedata)
15161 {
15162 printf (_("\nAssembly dump of section %s\n"), printable_section_name (filedata, section));
15163
15164 /* FIXME: XXX -- to be done --- XXX */
15165
15166 return true;
15167 }
15168 #endif
15169
15170 /* Reads in the contents of SECTION from FILE, returning a pointer
15171 to a malloc'ed buffer or NULL if something went wrong. */
15172
15173 static char *
15174 get_section_contents (Elf_Internal_Shdr * section, Filedata * filedata)
15175 {
15176 uint64_t num_bytes = section->sh_size;
15177
15178 if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
15179 {
15180 printf (_("Section '%s' has no data to dump.\n"),
15181 printable_section_name (filedata, section));
15182 return NULL;
15183 }
15184
15185 return (char *) get_data (NULL, filedata, section->sh_offset, 1, num_bytes,
15186 _("section contents"));
15187 }
15188
15189 /* Uncompresses a section that was compressed using zlib/zstd, in place. */
15190
15191 static bool
15192 uncompress_section_contents (bool is_zstd, unsigned char **buffer,
15193 uint64_t uncompressed_size, uint64_t *size)
15194 {
15195 uint64_t compressed_size = *size;
15196 unsigned char *compressed_buffer = *buffer;
15197 unsigned char *uncompressed_buffer = xmalloc (uncompressed_size);
15198 z_stream strm;
15199 int rc;
15200
15201 if (is_zstd)
15202 {
15203 #ifdef HAVE_ZSTD
15204 size_t ret = ZSTD_decompress (uncompressed_buffer, uncompressed_size,
15205 compressed_buffer, compressed_size);
15206 if (ZSTD_isError (ret))
15207 goto fail;
15208 #endif
15209 }
15210 else
15211 {
15212 /* It is possible the section consists of several compressed
15213 buffers concatenated together, so we uncompress in a loop. */
15214 /* PR 18313: The state field in the z_stream structure is supposed
15215 to be invisible to the user (ie us), but some compilers will
15216 still complain about it being used without initialisation. So
15217 we first zero the entire z_stream structure and then set the fields
15218 that we need. */
15219 memset (&strm, 0, sizeof strm);
15220 strm.avail_in = compressed_size;
15221 strm.next_in = (Bytef *)compressed_buffer;
15222 strm.avail_out = uncompressed_size;
15223
15224 rc = inflateInit (&strm);
15225 while (strm.avail_in > 0)
15226 {
15227 if (rc != Z_OK)
15228 break;
15229 strm.next_out = ((Bytef *)uncompressed_buffer
15230 + (uncompressed_size - strm.avail_out));
15231 rc = inflate (&strm, Z_FINISH);
15232 if (rc != Z_STREAM_END)
15233 break;
15234 rc = inflateReset (&strm);
15235 }
15236 if (inflateEnd (&strm) != Z_OK || rc != Z_OK || strm.avail_out != 0)
15237 goto fail;
15238 }
15239
15240 *buffer = uncompressed_buffer;
15241 *size = uncompressed_size;
15242 return true;
15243
15244 fail:
15245 free (uncompressed_buffer);
15246 /* Indicate decompression failure. */
15247 *buffer = NULL;
15248 return false;
15249 }
15250
15251 static bool
15252 dump_section_as_strings (Elf_Internal_Shdr * section, Filedata * filedata)
15253 {
15254 Elf_Internal_Shdr *relsec;
15255 uint64_t num_bytes;
15256 unsigned char *data;
15257 unsigned char *end;
15258 unsigned char *real_start;
15259 unsigned char *start;
15260 bool some_strings_shown;
15261
15262 real_start = start = (unsigned char *) get_section_contents (section, filedata);
15263 if (start == NULL)
15264 /* PR 21820: Do not fail if the section was empty. */
15265 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
15266
15267 num_bytes = section->sh_size;
15268
15269 if (filedata->is_separate)
15270 printf (_("\nString dump of section '%s' in linked file %s:\n"),
15271 printable_section_name (filedata, section),
15272 filedata->file_name);
15273 else
15274 printf (_("\nString dump of section '%s':\n"),
15275 printable_section_name (filedata, section));
15276
15277 if (decompress_dumps)
15278 {
15279 uint64_t new_size = num_bytes;
15280 uint64_t uncompressed_size = 0;
15281 bool is_zstd = false;
15282
15283 if ((section->sh_flags & SHF_COMPRESSED) != 0)
15284 {
15285 Elf_Internal_Chdr chdr;
15286 unsigned int compression_header_size
15287 = get_compression_header (& chdr, (unsigned char *) start,
15288 num_bytes);
15289 if (compression_header_size == 0)
15290 /* An error message will have already been generated
15291 by get_compression_header. */
15292 goto error_out;
15293
15294 if (chdr.ch_type == ELFCOMPRESS_ZLIB)
15295 ;
15296 #ifdef HAVE_ZSTD
15297 else if (chdr.ch_type == ELFCOMPRESS_ZSTD)
15298 is_zstd = true;
15299 #endif
15300 else
15301 {
15302 warn (_("section '%s' has unsupported compress type: %d\n"),
15303 printable_section_name (filedata, section), chdr.ch_type);
15304 goto error_out;
15305 }
15306 uncompressed_size = chdr.ch_size;
15307 start += compression_header_size;
15308 new_size -= compression_header_size;
15309 }
15310 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
15311 {
15312 /* Read the zlib header. In this case, it should be "ZLIB"
15313 followed by the uncompressed section size, 8 bytes in
15314 big-endian order. */
15315 uncompressed_size = start[4]; uncompressed_size <<= 8;
15316 uncompressed_size += start[5]; uncompressed_size <<= 8;
15317 uncompressed_size += start[6]; uncompressed_size <<= 8;
15318 uncompressed_size += start[7]; uncompressed_size <<= 8;
15319 uncompressed_size += start[8]; uncompressed_size <<= 8;
15320 uncompressed_size += start[9]; uncompressed_size <<= 8;
15321 uncompressed_size += start[10]; uncompressed_size <<= 8;
15322 uncompressed_size += start[11];
15323 start += 12;
15324 new_size -= 12;
15325 }
15326
15327 if (uncompressed_size)
15328 {
15329 if (uncompress_section_contents (is_zstd, &start, uncompressed_size,
15330 &new_size))
15331 num_bytes = new_size;
15332 else
15333 {
15334 error (_("Unable to decompress section %s\n"),
15335 printable_section_name (filedata, section));
15336 goto error_out;
15337 }
15338 }
15339 else
15340 start = real_start;
15341 }
15342
15343 /* If the section being dumped has relocations against it the user might
15344 be expecting these relocations to have been applied. Check for this
15345 case and issue a warning message in order to avoid confusion.
15346 FIXME: Maybe we ought to have an option that dumps a section with
15347 relocs applied ? */
15348 for (relsec = filedata->section_headers;
15349 relsec < filedata->section_headers + filedata->file_header.e_shnum;
15350 ++relsec)
15351 {
15352 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
15353 || relsec->sh_info >= filedata->file_header.e_shnum
15354 || filedata->section_headers + relsec->sh_info != section
15355 || relsec->sh_size == 0
15356 || relsec->sh_link >= filedata->file_header.e_shnum)
15357 continue;
15358
15359 printf (_(" Note: This section has relocations against it, but these have NOT been applied to this dump.\n"));
15360 break;
15361 }
15362
15363 data = start;
15364 end = start + num_bytes;
15365 some_strings_shown = false;
15366
15367 #ifdef HAVE_MBSTATE_T
15368 mbstate_t state;
15369 /* Initialise the multibyte conversion state. */
15370 memset (& state, 0, sizeof (state));
15371 #endif
15372
15373 bool continuing = false;
15374
15375 while (data < end)
15376 {
15377 while (!ISPRINT (* data))
15378 if (++ data >= end)
15379 break;
15380
15381 if (data < end)
15382 {
15383 size_t maxlen = end - data;
15384
15385 if (continuing)
15386 {
15387 printf (" ");
15388 continuing = false;
15389 }
15390 else
15391 {
15392 printf (" [%6lx] ", (unsigned long) (data - start));
15393 }
15394
15395 if (maxlen > 0)
15396 {
15397 char c = 0;
15398
15399 while (maxlen)
15400 {
15401 c = *data++;
15402
15403 if (c == 0)
15404 break;
15405
15406 /* PR 25543: Treat new-lines as string-ending characters. */
15407 if (c == '\n')
15408 {
15409 printf ("\\n\n");
15410 if (*data != 0)
15411 continuing = true;
15412 break;
15413 }
15414
15415 /* Do not print control characters directly as they can affect terminal
15416 settings. Such characters usually appear in the names generated
15417 by the assembler for local labels. */
15418 if (ISCNTRL (c))
15419 {
15420 printf ("^%c", c + 0x40);
15421 }
15422 else if (ISPRINT (c))
15423 {
15424 putchar (c);
15425 }
15426 else
15427 {
15428 size_t n;
15429 #ifdef HAVE_MBSTATE_T
15430 wchar_t w;
15431 #endif
15432 /* Let printf do the hard work of displaying multibyte characters. */
15433 printf ("%.1s", data - 1);
15434 #ifdef HAVE_MBSTATE_T
15435 /* Try to find out how many bytes made up the character that was
15436 just printed. Advance the symbol pointer past the bytes that
15437 were displayed. */
15438 n = mbrtowc (& w, (char *)(data - 1), MB_CUR_MAX, & state);
15439 #else
15440 n = 1;
15441 #endif
15442 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
15443 data += (n - 1);
15444 }
15445 }
15446
15447 if (c != '\n')
15448 putchar ('\n');
15449 }
15450 else
15451 {
15452 printf (_("<corrupt>\n"));
15453 data = end;
15454 }
15455 some_strings_shown = true;
15456 }
15457 }
15458
15459 if (! some_strings_shown)
15460 printf (_(" No strings found in this section."));
15461
15462 free (real_start);
15463
15464 putchar ('\n');
15465 return true;
15466
15467 error_out:
15468 free (real_start);
15469 return false;
15470 }
15471
15472 static bool
15473 dump_section_as_bytes (Elf_Internal_Shdr *section,
15474 Filedata *filedata,
15475 bool relocate)
15476 {
15477 Elf_Internal_Shdr *relsec;
15478 size_t bytes;
15479 uint64_t section_size;
15480 uint64_t addr;
15481 unsigned char *data;
15482 unsigned char *real_start;
15483 unsigned char *start;
15484
15485 real_start = start = (unsigned char *) get_section_contents (section, filedata);
15486 if (start == NULL)
15487 /* PR 21820: Do not fail if the section was empty. */
15488 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
15489
15490 section_size = section->sh_size;
15491
15492 if (filedata->is_separate)
15493 printf (_("\nHex dump of section '%s' in linked file %s:\n"),
15494 printable_section_name (filedata, section),
15495 filedata->file_name);
15496 else
15497 printf (_("\nHex dump of section '%s':\n"),
15498 printable_section_name (filedata, section));
15499
15500 if (decompress_dumps)
15501 {
15502 uint64_t new_size = section_size;
15503 uint64_t uncompressed_size = 0;
15504 bool is_zstd = false;
15505
15506 if ((section->sh_flags & SHF_COMPRESSED) != 0)
15507 {
15508 Elf_Internal_Chdr chdr;
15509 unsigned int compression_header_size
15510 = get_compression_header (& chdr, start, section_size);
15511
15512 if (compression_header_size == 0)
15513 /* An error message will have already been generated
15514 by get_compression_header. */
15515 goto error_out;
15516
15517 if (chdr.ch_type == ELFCOMPRESS_ZLIB)
15518 ;
15519 #ifdef HAVE_ZSTD
15520 else if (chdr.ch_type == ELFCOMPRESS_ZSTD)
15521 is_zstd = true;
15522 #endif
15523 else
15524 {
15525 warn (_("section '%s' has unsupported compress type: %d\n"),
15526 printable_section_name (filedata, section), chdr.ch_type);
15527 goto error_out;
15528 }
15529 uncompressed_size = chdr.ch_size;
15530 start += compression_header_size;
15531 new_size -= compression_header_size;
15532 }
15533 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
15534 {
15535 /* Read the zlib header. In this case, it should be "ZLIB"
15536 followed by the uncompressed section size, 8 bytes in
15537 big-endian order. */
15538 uncompressed_size = start[4]; uncompressed_size <<= 8;
15539 uncompressed_size += start[5]; uncompressed_size <<= 8;
15540 uncompressed_size += start[6]; uncompressed_size <<= 8;
15541 uncompressed_size += start[7]; uncompressed_size <<= 8;
15542 uncompressed_size += start[8]; uncompressed_size <<= 8;
15543 uncompressed_size += start[9]; uncompressed_size <<= 8;
15544 uncompressed_size += start[10]; uncompressed_size <<= 8;
15545 uncompressed_size += start[11];
15546 start += 12;
15547 new_size -= 12;
15548 }
15549
15550 if (uncompressed_size)
15551 {
15552 if (uncompress_section_contents (is_zstd, &start, uncompressed_size,
15553 &new_size))
15554 {
15555 section_size = new_size;
15556 }
15557 else
15558 {
15559 error (_("Unable to decompress section %s\n"),
15560 printable_section_name (filedata, section));
15561 /* FIXME: Print the section anyway ? */
15562 goto error_out;
15563 }
15564 }
15565 else
15566 start = real_start;
15567 }
15568
15569 if (relocate)
15570 {
15571 if (! apply_relocations (filedata, section, start, section_size, NULL, NULL))
15572 goto error_out;
15573 }
15574 else
15575 {
15576 /* If the section being dumped has relocations against it the user might
15577 be expecting these relocations to have been applied. Check for this
15578 case and issue a warning message in order to avoid confusion.
15579 FIXME: Maybe we ought to have an option that dumps a section with
15580 relocs applied ? */
15581 for (relsec = filedata->section_headers;
15582 relsec < filedata->section_headers + filedata->file_header.e_shnum;
15583 ++relsec)
15584 {
15585 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
15586 || relsec->sh_info >= filedata->file_header.e_shnum
15587 || filedata->section_headers + relsec->sh_info != section
15588 || relsec->sh_size == 0
15589 || relsec->sh_link >= filedata->file_header.e_shnum)
15590 continue;
15591
15592 printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
15593 break;
15594 }
15595 }
15596
15597 addr = section->sh_addr;
15598 bytes = section_size;
15599 data = start;
15600
15601 while (bytes)
15602 {
15603 int j;
15604 int k;
15605 int lbytes;
15606
15607 lbytes = (bytes > 16 ? 16 : bytes);
15608
15609 printf (" 0x%8.8lx ", (unsigned long) addr);
15610
15611 for (j = 0; j < 16; j++)
15612 {
15613 if (j < lbytes)
15614 printf ("%2.2x", data[j]);
15615 else
15616 printf (" ");
15617
15618 if ((j & 3) == 3)
15619 printf (" ");
15620 }
15621
15622 for (j = 0; j < lbytes; j++)
15623 {
15624 k = data[j];
15625 if (k >= ' ' && k < 0x7f)
15626 printf ("%c", k);
15627 else
15628 printf (".");
15629 }
15630
15631 putchar ('\n');
15632
15633 data += lbytes;
15634 addr += lbytes;
15635 bytes -= lbytes;
15636 }
15637
15638 free (real_start);
15639
15640 putchar ('\n');
15641 return true;
15642
15643 error_out:
15644 free (real_start);
15645 return false;
15646 }
15647
15648 #ifdef ENABLE_LIBCTF
15649 static ctf_sect_t *
15650 shdr_to_ctf_sect (ctf_sect_t *buf, Elf_Internal_Shdr *shdr, Filedata *filedata)
15651 {
15652 buf->cts_name = section_name_print (filedata, shdr);
15653 buf->cts_size = shdr->sh_size;
15654 buf->cts_entsize = shdr->sh_entsize;
15655
15656 return buf;
15657 }
15658
15659 /* Formatting callback function passed to ctf_dump. Returns either the pointer
15660 it is passed, or a pointer to newly-allocated storage, in which case
15661 dump_ctf() will free it when it no longer needs it. */
15662
15663 static char *
15664 dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
15665 char *s, void *arg)
15666 {
15667 const char *blanks = arg;
15668 char *new_s;
15669
15670 if (asprintf (&new_s, "%s%s", blanks, s) < 0)
15671 return s;
15672 return new_s;
15673 }
15674
15675 /* Dump CTF errors/warnings. */
15676 static void
15677 dump_ctf_errs (ctf_dict_t *fp)
15678 {
15679 ctf_next_t *it = NULL;
15680 char *errtext;
15681 int is_warning;
15682 int err;
15683
15684 /* Dump accumulated errors and warnings. */
15685 while ((errtext = ctf_errwarning_next (fp, &it, &is_warning, &err)) != NULL)
15686 {
15687 error (_("%s: %s"), is_warning ? _("warning"): _("error"),
15688 errtext);
15689 free (errtext);
15690 }
15691 if (err != ECTF_NEXT_END)
15692 error (_("CTF error: cannot get CTF errors: `%s'"), ctf_errmsg (err));
15693 }
15694
15695 /* Dump one CTF archive member. */
15696
15697 static void
15698 dump_ctf_archive_member (ctf_dict_t *ctf, const char *name, ctf_dict_t *parent,
15699 size_t member)
15700 {
15701 const char *things[] = {"Header", "Labels", "Data objects",
15702 "Function objects", "Variables", "Types", "Strings",
15703 ""};
15704 const char **thing;
15705 size_t i;
15706
15707 /* Don't print out the name of the default-named archive member if it appears
15708 first in the list. The name .ctf appears everywhere, even for things that
15709 aren't really archives, so printing it out is liable to be confusing; also,
15710 the common case by far is for only one archive member to exist, and hiding
15711 it in that case seems worthwhile. */
15712
15713 if (strcmp (name, ".ctf") != 0 || member != 0)
15714 printf (_("\nCTF archive member: %s:\n"), name);
15715
15716 if (ctf_parent_name (ctf) != NULL)
15717 ctf_import (ctf, parent);
15718
15719 for (i = 0, thing = things; *thing[0]; thing++, i++)
15720 {
15721 ctf_dump_state_t *s = NULL;
15722 char *item;
15723
15724 printf ("\n %s:\n", *thing);
15725 while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
15726 (void *) " ")) != NULL)
15727 {
15728 printf ("%s\n", item);
15729 free (item);
15730 }
15731
15732 if (ctf_errno (ctf))
15733 {
15734 error (_("Iteration failed: %s, %s\n"), *thing,
15735 ctf_errmsg (ctf_errno (ctf)));
15736 break;
15737 }
15738 }
15739
15740 dump_ctf_errs (ctf);
15741 }
15742
15743 static bool
15744 dump_section_as_ctf (Elf_Internal_Shdr * section, Filedata * filedata)
15745 {
15746 Elf_Internal_Shdr * symtab_sec = NULL;
15747 Elf_Internal_Shdr * strtab_sec = NULL;
15748 void * data = NULL;
15749 void * symdata = NULL;
15750 void * strdata = NULL;
15751 ctf_sect_t ctfsect, symsect, strsect;
15752 ctf_sect_t * symsectp = NULL;
15753 ctf_sect_t * strsectp = NULL;
15754 ctf_archive_t * ctfa = NULL;
15755 ctf_dict_t * parent = NULL;
15756 ctf_dict_t * fp;
15757
15758 ctf_next_t *i = NULL;
15759 const char *name;
15760 size_t member = 0;
15761 int err;
15762 bool ret = false;
15763
15764 shdr_to_ctf_sect (&ctfsect, section, filedata);
15765 data = get_section_contents (section, filedata);
15766 ctfsect.cts_data = data;
15767
15768 if (!dump_ctf_symtab_name)
15769 dump_ctf_symtab_name = strdup (".dynsym");
15770
15771 if (!dump_ctf_strtab_name)
15772 dump_ctf_strtab_name = strdup (".dynstr");
15773
15774 if (dump_ctf_symtab_name && dump_ctf_symtab_name[0] != 0)
15775 {
15776 if ((symtab_sec = find_section (filedata, dump_ctf_symtab_name)) == NULL)
15777 {
15778 error (_("No symbol section named %s\n"), dump_ctf_symtab_name);
15779 goto fail;
15780 }
15781 if ((symdata = (void *) get_data (NULL, filedata,
15782 symtab_sec->sh_offset, 1,
15783 symtab_sec->sh_size,
15784 _("symbols"))) == NULL)
15785 goto fail;
15786 symsectp = shdr_to_ctf_sect (&symsect, symtab_sec, filedata);
15787 symsect.cts_data = symdata;
15788 }
15789
15790 if (dump_ctf_strtab_name && dump_ctf_strtab_name[0] != 0)
15791 {
15792 if ((strtab_sec = find_section (filedata, dump_ctf_strtab_name)) == NULL)
15793 {
15794 error (_("No string table section named %s\n"),
15795 dump_ctf_strtab_name);
15796 goto fail;
15797 }
15798 if ((strdata = (void *) get_data (NULL, filedata,
15799 strtab_sec->sh_offset, 1,
15800 strtab_sec->sh_size,
15801 _("strings"))) == NULL)
15802 goto fail;
15803 strsectp = shdr_to_ctf_sect (&strsect, strtab_sec, filedata);
15804 strsect.cts_data = strdata;
15805 }
15806
15807 /* Load the CTF file and dump it. It may be a raw CTF section, or an archive:
15808 libctf papers over the difference, so we can pretend it is always an
15809 archive. */
15810
15811 if ((ctfa = ctf_arc_bufopen (&ctfsect, symsectp, strsectp, &err)) == NULL)
15812 {
15813 dump_ctf_errs (NULL);
15814 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15815 goto fail;
15816 }
15817
15818 ctf_arc_symsect_endianness (ctfa, filedata->file_header.e_ident[EI_DATA]
15819 != ELFDATA2MSB);
15820
15821 /* Preload the parent dict, since it will need to be imported into every
15822 child in turn. */
15823 if ((parent = ctf_dict_open (ctfa, dump_ctf_parent_name, &err)) == NULL)
15824 {
15825 dump_ctf_errs (NULL);
15826 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15827 goto fail;
15828 }
15829
15830 ret = true;
15831
15832 if (filedata->is_separate)
15833 printf (_("\nDump of CTF section '%s' in linked file %s:\n"),
15834 printable_section_name (filedata, section),
15835 filedata->file_name);
15836 else
15837 printf (_("\nDump of CTF section '%s':\n"),
15838 printable_section_name (filedata, section));
15839
15840 while ((fp = ctf_archive_next (ctfa, &i, &name, 0, &err)) != NULL)
15841 dump_ctf_archive_member (fp, name, parent, member++);
15842 if (err != ECTF_NEXT_END)
15843 {
15844 dump_ctf_errs (NULL);
15845 error (_("CTF member open failure: %s\n"), ctf_errmsg (err));
15846 ret = false;
15847 }
15848
15849 fail:
15850 ctf_dict_close (parent);
15851 ctf_close (ctfa);
15852 free (data);
15853 free (symdata);
15854 free (strdata);
15855 return ret;
15856 }
15857 #endif
15858
15859 static bool
15860 load_specific_debug_section (enum dwarf_section_display_enum debug,
15861 const Elf_Internal_Shdr * sec,
15862 void * data)
15863 {
15864 struct dwarf_section * section = &debug_displays [debug].section;
15865 char buf [64];
15866 Filedata * filedata = (Filedata *) data;
15867
15868 if (section->start != NULL)
15869 {
15870 /* If it is already loaded, do nothing. */
15871 if (streq (section->filename, filedata->file_name))
15872 return true;
15873 free (section->start);
15874 }
15875
15876 snprintf (buf, sizeof (buf), _("%s section data"), section->name);
15877 section->address = sec->sh_addr;
15878 section->filename = filedata->file_name;
15879 section->start = (unsigned char *) get_data (NULL, filedata,
15880 sec->sh_offset, 1,
15881 sec->sh_size, buf);
15882 if (section->start == NULL)
15883 section->size = 0;
15884 else
15885 {
15886 unsigned char *start = section->start;
15887 uint64_t size = sec->sh_size;
15888 uint64_t uncompressed_size = 0;
15889 bool is_zstd = false;
15890
15891 if ((sec->sh_flags & SHF_COMPRESSED) != 0)
15892 {
15893 Elf_Internal_Chdr chdr;
15894 unsigned int compression_header_size;
15895
15896 if (size < (is_32bit_elf
15897 ? sizeof (Elf32_External_Chdr)
15898 : sizeof (Elf64_External_Chdr)))
15899 {
15900 warn (_("compressed section %s is too small to contain a compression header\n"),
15901 section->name);
15902 return false;
15903 }
15904
15905 compression_header_size = get_compression_header (&chdr, start, size);
15906 if (compression_header_size == 0)
15907 /* An error message will have already been generated
15908 by get_compression_header. */
15909 return false;
15910
15911 if (chdr.ch_type == ELFCOMPRESS_ZLIB)
15912 ;
15913 #ifdef HAVE_ZSTD
15914 else if (chdr.ch_type == ELFCOMPRESS_ZSTD)
15915 is_zstd = true;
15916 #endif
15917 else
15918 {
15919 warn (_("section '%s' has unsupported compress type: %d\n"),
15920 section->name, chdr.ch_type);
15921 return false;
15922 }
15923 uncompressed_size = chdr.ch_size;
15924 start += compression_header_size;
15925 size -= compression_header_size;
15926 }
15927 else if (size > 12 && streq ((char *) start, "ZLIB"))
15928 {
15929 /* Read the zlib header. In this case, it should be "ZLIB"
15930 followed by the uncompressed section size, 8 bytes in
15931 big-endian order. */
15932 uncompressed_size = start[4]; uncompressed_size <<= 8;
15933 uncompressed_size += start[5]; uncompressed_size <<= 8;
15934 uncompressed_size += start[6]; uncompressed_size <<= 8;
15935 uncompressed_size += start[7]; uncompressed_size <<= 8;
15936 uncompressed_size += start[8]; uncompressed_size <<= 8;
15937 uncompressed_size += start[9]; uncompressed_size <<= 8;
15938 uncompressed_size += start[10]; uncompressed_size <<= 8;
15939 uncompressed_size += start[11];
15940 start += 12;
15941 size -= 12;
15942 }
15943
15944 if (uncompressed_size)
15945 {
15946 if (uncompress_section_contents (is_zstd, &start, uncompressed_size,
15947 &size))
15948 {
15949 /* Free the compressed buffer, update the section buffer
15950 and the section size if uncompress is successful. */
15951 free (section->start);
15952 section->start = start;
15953 }
15954 else
15955 {
15956 error (_("Unable to decompress section %s\n"),
15957 printable_section_name (filedata, sec));
15958 return false;
15959 }
15960 }
15961
15962 section->size = size;
15963 }
15964
15965 if (section->start == NULL)
15966 return false;
15967
15968 if (debug_displays [debug].relocate)
15969 {
15970 if (! apply_relocations (filedata, sec, section->start, section->size,
15971 & section->reloc_info, & section->num_relocs))
15972 return false;
15973 }
15974 else
15975 {
15976 section->reloc_info = NULL;
15977 section->num_relocs = 0;
15978 }
15979
15980 return true;
15981 }
15982
15983 #if HAVE_LIBDEBUGINFOD
15984 /* Return a hex string representation of the build-id. */
15985 unsigned char *
15986 get_build_id (void * data)
15987 {
15988 Filedata * filedata = (Filedata *) data;
15989 Elf_Internal_Shdr * shdr;
15990 unsigned long i;
15991
15992 /* Iterate through notes to find note.gnu.build-id.
15993 FIXME: Only the first note in any note section is examined. */
15994 for (i = 0, shdr = filedata->section_headers;
15995 i < filedata->file_header.e_shnum && shdr != NULL;
15996 i++, shdr++)
15997 {
15998 if (shdr->sh_type != SHT_NOTE)
15999 continue;
16000
16001 char * next;
16002 char * end;
16003 size_t data_remaining;
16004 size_t min_notesz;
16005 Elf_External_Note * enote;
16006 Elf_Internal_Note inote;
16007
16008 uint64_t offset = shdr->sh_offset;
16009 uint64_t align = shdr->sh_addralign;
16010 uint64_t length = shdr->sh_size;
16011
16012 enote = (Elf_External_Note *) get_section_contents (shdr, filedata);
16013 if (enote == NULL)
16014 continue;
16015
16016 if (align < 4)
16017 align = 4;
16018 else if (align != 4 && align != 8)
16019 {
16020 free (enote);
16021 continue;
16022 }
16023
16024 end = (char *) enote + length;
16025 data_remaining = end - (char *) enote;
16026
16027 if (!is_ia64_vms (filedata))
16028 {
16029 min_notesz = offsetof (Elf_External_Note, name);
16030 if (data_remaining < min_notesz)
16031 {
16032 warn (_("\
16033 malformed note encountered in section %s whilst scanning for build-id note\n"),
16034 printable_section_name (filedata, shdr));
16035 free (enote);
16036 continue;
16037 }
16038 data_remaining -= min_notesz;
16039
16040 inote.type = BYTE_GET (enote->type);
16041 inote.namesz = BYTE_GET (enote->namesz);
16042 inote.namedata = enote->name;
16043 inote.descsz = BYTE_GET (enote->descsz);
16044 inote.descdata = ((char *) enote
16045 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
16046 inote.descpos = offset + (inote.descdata - (char *) enote);
16047 next = ((char *) enote
16048 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
16049 }
16050 else
16051 {
16052 Elf64_External_VMS_Note *vms_enote;
16053
16054 /* PR binutils/15191
16055 Make sure that there is enough data to read. */
16056 min_notesz = offsetof (Elf64_External_VMS_Note, name);
16057 if (data_remaining < min_notesz)
16058 {
16059 warn (_("\
16060 malformed note encountered in section %s whilst scanning for build-id note\n"),
16061 printable_section_name (filedata, shdr));
16062 free (enote);
16063 continue;
16064 }
16065 data_remaining -= min_notesz;
16066
16067 vms_enote = (Elf64_External_VMS_Note *) enote;
16068 inote.type = BYTE_GET (vms_enote->type);
16069 inote.namesz = BYTE_GET (vms_enote->namesz);
16070 inote.namedata = vms_enote->name;
16071 inote.descsz = BYTE_GET (vms_enote->descsz);
16072 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
16073 inote.descpos = offset + (inote.descdata - (char *) enote);
16074 next = inote.descdata + align_power (inote.descsz, 3);
16075 }
16076
16077 /* Skip malformed notes. */
16078 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
16079 || (size_t) (inote.descdata - inote.namedata) > data_remaining
16080 || (size_t) (next - inote.descdata) < inote.descsz
16081 || ((size_t) (next - inote.descdata)
16082 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
16083 {
16084 warn (_("\
16085 malformed note encountered in section %s whilst scanning for build-id note\n"),
16086 printable_section_name (filedata, shdr));
16087 free (enote);
16088 continue;
16089 }
16090
16091 /* Check if this is the build-id note. If so then convert the build-id
16092 bytes to a hex string. */
16093 if (inote.namesz > 0
16094 && startswith (inote.namedata, "GNU")
16095 && inote.type == NT_GNU_BUILD_ID)
16096 {
16097 unsigned long j;
16098 char * build_id;
16099
16100 build_id = malloc (inote.descsz * 2 + 1);
16101 if (build_id == NULL)
16102 {
16103 free (enote);
16104 return NULL;
16105 }
16106
16107 for (j = 0; j < inote.descsz; ++j)
16108 sprintf (build_id + (j * 2), "%02x", inote.descdata[j] & 0xff);
16109 build_id[inote.descsz * 2] = '\0';
16110 free (enote);
16111
16112 return (unsigned char *) build_id;
16113 }
16114 free (enote);
16115 }
16116
16117 return NULL;
16118 }
16119 #endif /* HAVE_LIBDEBUGINFOD */
16120
16121 /* If this is not NULL, load_debug_section will only look for sections
16122 within the list of sections given here. */
16123 static unsigned int * section_subset = NULL;
16124
16125 bool
16126 load_debug_section (enum dwarf_section_display_enum debug, void * data)
16127 {
16128 struct dwarf_section * section = &debug_displays [debug].section;
16129 Elf_Internal_Shdr * sec;
16130 Filedata * filedata = (Filedata *) data;
16131
16132 if (!dump_any_debugging)
16133 return false;
16134
16135 /* Without section headers we cannot find any sections. */
16136 if (filedata->section_headers == NULL)
16137 return false;
16138
16139 if (filedata->string_table == NULL
16140 && filedata->file_header.e_shstrndx != SHN_UNDEF
16141 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
16142 {
16143 Elf_Internal_Shdr * strs;
16144
16145 /* Read in the string table, so that we have section names to scan. */
16146 strs = filedata->section_headers + filedata->file_header.e_shstrndx;
16147
16148 if (strs != NULL && strs->sh_size != 0)
16149 {
16150 filedata->string_table
16151 = (char *) get_data (NULL, filedata, strs->sh_offset,
16152 1, strs->sh_size, _("string table"));
16153
16154 filedata->string_table_length
16155 = filedata->string_table != NULL ? strs->sh_size : 0;
16156 }
16157 }
16158
16159 /* Locate the debug section. */
16160 sec = find_section_in_set (filedata, section->uncompressed_name, section_subset);
16161 if (sec != NULL)
16162 section->name = section->uncompressed_name;
16163 else
16164 {
16165 sec = find_section_in_set (filedata, section->compressed_name, section_subset);
16166 if (sec != NULL)
16167 section->name = section->compressed_name;
16168 }
16169 if (sec == NULL)
16170 return false;
16171
16172 /* If we're loading from a subset of sections, and we've loaded
16173 a section matching this name before, it's likely that it's a
16174 different one. */
16175 if (section_subset != NULL)
16176 free_debug_section (debug);
16177
16178 return load_specific_debug_section (debug, sec, data);
16179 }
16180
16181 void
16182 free_debug_section (enum dwarf_section_display_enum debug)
16183 {
16184 struct dwarf_section * section = &debug_displays [debug].section;
16185
16186 if (section->start == NULL)
16187 return;
16188
16189 free ((char *) section->start);
16190 section->start = NULL;
16191 section->address = 0;
16192 section->size = 0;
16193
16194 free (section->reloc_info);
16195 section->reloc_info = NULL;
16196 section->num_relocs = 0;
16197 }
16198
16199 static bool
16200 display_debug_section (int shndx, Elf_Internal_Shdr * section, Filedata * filedata)
16201 {
16202 const char *name = (section_name_valid (filedata, section)
16203 ? section_name (filedata, section) : "");
16204 const char *print_name = printable_section_name (filedata, section);
16205 uint64_t length;
16206 bool result = true;
16207 int i;
16208
16209 length = section->sh_size;
16210 if (length == 0)
16211 {
16212 printf (_("\nSection '%s' has no debugging data.\n"), print_name);
16213 return true;
16214 }
16215 if (section->sh_type == SHT_NOBITS)
16216 {
16217 /* There is no point in dumping the contents of a debugging section
16218 which has the NOBITS type - the bits in the file will be random.
16219 This can happen when a file containing a .eh_frame section is
16220 stripped with the --only-keep-debug command line option. */
16221 printf (_("section '%s' has the NOBITS type - its contents are unreliable.\n"),
16222 print_name);
16223 return false;
16224 }
16225
16226 if (startswith (name, ".gnu.linkonce.wi."))
16227 name = ".debug_info";
16228
16229 /* See if we know how to display the contents of this section. */
16230 for (i = 0; i < max; i++)
16231 {
16232 enum dwarf_section_display_enum id = (enum dwarf_section_display_enum) i;
16233 struct dwarf_section_display * display = debug_displays + i;
16234 struct dwarf_section * sec = & display->section;
16235
16236 if (streq (sec->uncompressed_name, name)
16237 || (id == line && startswith (name, ".debug_line."))
16238 || streq (sec->compressed_name, name))
16239 {
16240 bool secondary = (section != find_section (filedata, name));
16241
16242 if (secondary)
16243 free_debug_section (id);
16244
16245 if (i == line && startswith (name, ".debug_line."))
16246 sec->name = name;
16247 else if (streq (sec->uncompressed_name, name))
16248 sec->name = sec->uncompressed_name;
16249 else
16250 sec->name = sec->compressed_name;
16251
16252 if (load_specific_debug_section (id, section, filedata))
16253 {
16254 /* If this debug section is part of a CU/TU set in a .dwp file,
16255 restrict load_debug_section to the sections in that set. */
16256 section_subset = find_cu_tu_set (filedata, shndx);
16257
16258 result &= display->display (sec, filedata);
16259
16260 section_subset = NULL;
16261
16262 if (secondary || (id != info && id != abbrev && id != debug_addr))
16263 free_debug_section (id);
16264 }
16265 break;
16266 }
16267 }
16268
16269 if (i == max)
16270 {
16271 printf (_("Unrecognized debug section: %s\n"), print_name);
16272 result = false;
16273 }
16274
16275 return result;
16276 }
16277
16278 /* Set DUMP_SECTS for all sections where dumps were requested
16279 based on section name. */
16280
16281 static void
16282 initialise_dumps_byname (Filedata * filedata)
16283 {
16284 struct dump_list_entry * cur;
16285
16286 for (cur = dump_sects_byname; cur; cur = cur->next)
16287 {
16288 unsigned int i;
16289 bool any = false;
16290
16291 for (i = 0; i < filedata->file_header.e_shnum; i++)
16292 if (section_name_valid (filedata, filedata->section_headers + i)
16293 && streq (section_name (filedata, filedata->section_headers + i),
16294 cur->name))
16295 {
16296 request_dump_bynumber (&filedata->dump, i, cur->type);
16297 any = true;
16298 }
16299
16300 if (!any && !filedata->is_separate)
16301 warn (_("Section '%s' was not dumped because it does not exist\n"),
16302 cur->name);
16303 }
16304 }
16305
16306 static bool
16307 process_section_contents (Filedata * filedata)
16308 {
16309 Elf_Internal_Shdr * section;
16310 unsigned int i;
16311 bool res = true;
16312
16313 if (! do_dump)
16314 return true;
16315
16316 initialise_dumps_byname (filedata);
16317
16318 for (i = 0, section = filedata->section_headers;
16319 i < filedata->file_header.e_shnum && i < filedata->dump.num_dump_sects;
16320 i++, section++)
16321 {
16322 dump_type dump = filedata->dump.dump_sects[i];
16323
16324 if (filedata->is_separate && ! process_links)
16325 dump &= DEBUG_DUMP;
16326
16327 #ifdef SUPPORT_DISASSEMBLY
16328 if (dump & DISASS_DUMP)
16329 {
16330 if (! disassemble_section (section, filedata))
16331 res = false;
16332 }
16333 #endif
16334 if (dump & HEX_DUMP)
16335 {
16336 if (! dump_section_as_bytes (section, filedata, false))
16337 res = false;
16338 }
16339
16340 if (dump & RELOC_DUMP)
16341 {
16342 if (! dump_section_as_bytes (section, filedata, true))
16343 res = false;
16344 }
16345
16346 if (dump & STRING_DUMP)
16347 {
16348 if (! dump_section_as_strings (section, filedata))
16349 res = false;
16350 }
16351
16352 if (dump & DEBUG_DUMP)
16353 {
16354 if (! display_debug_section (i, section, filedata))
16355 res = false;
16356 }
16357
16358 #ifdef ENABLE_LIBCTF
16359 if (dump & CTF_DUMP)
16360 {
16361 if (! dump_section_as_ctf (section, filedata))
16362 res = false;
16363 }
16364 #endif
16365 }
16366
16367 if (! filedata->is_separate)
16368 {
16369 /* Check to see if the user requested a
16370 dump of a section that does not exist. */
16371 for (; i < filedata->dump.num_dump_sects; i++)
16372 if (filedata->dump.dump_sects[i])
16373 {
16374 warn (_("Section %d was not dumped because it does not exist!\n"), i);
16375 res = false;
16376 }
16377 }
16378
16379 return res;
16380 }
16381
16382 static void
16383 process_mips_fpe_exception (int mask)
16384 {
16385 if (mask)
16386 {
16387 bool first = true;
16388
16389 if (mask & OEX_FPU_INEX)
16390 fputs ("INEX", stdout), first = false;
16391 if (mask & OEX_FPU_UFLO)
16392 printf ("%sUFLO", first ? "" : "|"), first = false;
16393 if (mask & OEX_FPU_OFLO)
16394 printf ("%sOFLO", first ? "" : "|"), first = false;
16395 if (mask & OEX_FPU_DIV0)
16396 printf ("%sDIV0", first ? "" : "|"), first = false;
16397 if (mask & OEX_FPU_INVAL)
16398 printf ("%sINVAL", first ? "" : "|");
16399 }
16400 else
16401 fputs ("0", stdout);
16402 }
16403
16404 /* Display's the value of TAG at location P. If TAG is
16405 greater than 0 it is assumed to be an unknown tag, and
16406 a message is printed to this effect. Otherwise it is
16407 assumed that a message has already been printed.
16408
16409 If the bottom bit of TAG is set it assumed to have a
16410 string value, otherwise it is assumed to have an integer
16411 value.
16412
16413 Returns an updated P pointing to the first unread byte
16414 beyond the end of TAG's value.
16415
16416 Reads at or beyond END will not be made. */
16417
16418 static unsigned char *
16419 display_tag_value (signed int tag,
16420 unsigned char * p,
16421 const unsigned char * const end)
16422 {
16423 unsigned long val;
16424
16425 if (tag > 0)
16426 printf (" Tag_unknown_%d: ", tag);
16427
16428 if (p >= end)
16429 {
16430 warn (_("<corrupt tag>\n"));
16431 }
16432 else if (tag & 1)
16433 {
16434 /* PR 17531 file: 027-19978-0.004. */
16435 size_t maxlen = (end - p) - 1;
16436
16437 putchar ('"');
16438 if (maxlen > 0)
16439 {
16440 print_symbol ((int) maxlen, (const char *) p);
16441 p += strnlen ((char *) p, maxlen) + 1;
16442 }
16443 else
16444 {
16445 printf (_("<corrupt string tag>"));
16446 p = (unsigned char *) end;
16447 }
16448 printf ("\"\n");
16449 }
16450 else
16451 {
16452 READ_ULEB (val, p, end);
16453 printf ("%ld (0x%lx)\n", val, val);
16454 }
16455
16456 assert (p <= end);
16457 return p;
16458 }
16459
16460 /* ARC ABI attributes section. */
16461
16462 static unsigned char *
16463 display_arc_attribute (unsigned char * p,
16464 const unsigned char * const end)
16465 {
16466 unsigned int tag;
16467 unsigned int val;
16468
16469 READ_ULEB (tag, p, end);
16470
16471 switch (tag)
16472 {
16473 case Tag_ARC_PCS_config:
16474 READ_ULEB (val, p, end);
16475 printf (" Tag_ARC_PCS_config: ");
16476 switch (val)
16477 {
16478 case 0:
16479 printf (_("Absent/Non standard\n"));
16480 break;
16481 case 1:
16482 printf (_("Bare metal/mwdt\n"));
16483 break;
16484 case 2:
16485 printf (_("Bare metal/newlib\n"));
16486 break;
16487 case 3:
16488 printf (_("Linux/uclibc\n"));
16489 break;
16490 case 4:
16491 printf (_("Linux/glibc\n"));
16492 break;
16493 default:
16494 printf (_("Unknown\n"));
16495 break;
16496 }
16497 break;
16498
16499 case Tag_ARC_CPU_base:
16500 READ_ULEB (val, p, end);
16501 printf (" Tag_ARC_CPU_base: ");
16502 switch (val)
16503 {
16504 default:
16505 case TAG_CPU_NONE:
16506 printf (_("Absent\n"));
16507 break;
16508 case TAG_CPU_ARC6xx:
16509 printf ("ARC6xx\n");
16510 break;
16511 case TAG_CPU_ARC7xx:
16512 printf ("ARC7xx\n");
16513 break;
16514 case TAG_CPU_ARCEM:
16515 printf ("ARCEM\n");
16516 break;
16517 case TAG_CPU_ARCHS:
16518 printf ("ARCHS\n");
16519 break;
16520 }
16521 break;
16522
16523 case Tag_ARC_CPU_variation:
16524 READ_ULEB (val, p, end);
16525 printf (" Tag_ARC_CPU_variation: ");
16526 switch (val)
16527 {
16528 default:
16529 if (val > 0 && val < 16)
16530 printf ("Core%d\n", val);
16531 else
16532 printf ("Unknown\n");
16533 break;
16534
16535 case 0:
16536 printf (_("Absent\n"));
16537 break;
16538 }
16539 break;
16540
16541 case Tag_ARC_CPU_name:
16542 printf (" Tag_ARC_CPU_name: ");
16543 p = display_tag_value (-1, p, end);
16544 break;
16545
16546 case Tag_ARC_ABI_rf16:
16547 READ_ULEB (val, p, end);
16548 printf (" Tag_ARC_ABI_rf16: %s\n", val ? _("yes") : _("no"));
16549 break;
16550
16551 case Tag_ARC_ABI_osver:
16552 READ_ULEB (val, p, end);
16553 printf (" Tag_ARC_ABI_osver: v%d\n", val);
16554 break;
16555
16556 case Tag_ARC_ABI_pic:
16557 case Tag_ARC_ABI_sda:
16558 READ_ULEB (val, p, end);
16559 printf (tag == Tag_ARC_ABI_sda ? " Tag_ARC_ABI_sda: "
16560 : " Tag_ARC_ABI_pic: ");
16561 switch (val)
16562 {
16563 case 0:
16564 printf (_("Absent\n"));
16565 break;
16566 case 1:
16567 printf ("MWDT\n");
16568 break;
16569 case 2:
16570 printf ("GNU\n");
16571 break;
16572 default:
16573 printf (_("Unknown\n"));
16574 break;
16575 }
16576 break;
16577
16578 case Tag_ARC_ABI_tls:
16579 READ_ULEB (val, p, end);
16580 printf (" Tag_ARC_ABI_tls: %s\n", val ? "r25": "none");
16581 break;
16582
16583 case Tag_ARC_ABI_enumsize:
16584 READ_ULEB (val, p, end);
16585 printf (" Tag_ARC_ABI_enumsize: %s\n", val ? _("default") :
16586 _("smallest"));
16587 break;
16588
16589 case Tag_ARC_ABI_exceptions:
16590 READ_ULEB (val, p, end);
16591 printf (" Tag_ARC_ABI_exceptions: %s\n", val ? _("OPTFP")
16592 : _("default"));
16593 break;
16594
16595 case Tag_ARC_ABI_double_size:
16596 READ_ULEB (val, p, end);
16597 printf (" Tag_ARC_ABI_double_size: %d\n", val);
16598 break;
16599
16600 case Tag_ARC_ISA_config:
16601 printf (" Tag_ARC_ISA_config: ");
16602 p = display_tag_value (-1, p, end);
16603 break;
16604
16605 case Tag_ARC_ISA_apex:
16606 printf (" Tag_ARC_ISA_apex: ");
16607 p = display_tag_value (-1, p, end);
16608 break;
16609
16610 case Tag_ARC_ISA_mpy_option:
16611 READ_ULEB (val, p, end);
16612 printf (" Tag_ARC_ISA_mpy_option: %d\n", val);
16613 break;
16614
16615 case Tag_ARC_ATR_version:
16616 READ_ULEB (val, p, end);
16617 printf (" Tag_ARC_ATR_version: %d\n", val);
16618 break;
16619
16620 default:
16621 return display_tag_value (tag & 1, p, end);
16622 }
16623
16624 return p;
16625 }
16626
16627 /* ARM EABI attributes section. */
16628 typedef struct
16629 {
16630 unsigned int tag;
16631 const char * name;
16632 /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup. */
16633 unsigned int type;
16634 const char *const *table;
16635 } arm_attr_public_tag;
16636
16637 static const char *const arm_attr_tag_CPU_arch[] =
16638 {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
16639 "v6K", "v7", "v6-M", "v6S-M", "v7E-M", "v8", "v8-R", "v8-M.baseline",
16640 "v8-M.mainline", "v8.1-A", "v8.2-A", "v8.3-A",
16641 "v8.1-M.mainline", "v9"};
16642 static const char *const arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
16643 static const char *const arm_attr_tag_THUMB_ISA_use[] =
16644 {"No", "Thumb-1", "Thumb-2", "Yes"};
16645 static const char *const arm_attr_tag_FP_arch[] =
16646 {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16", "VFPv4", "VFPv4-D16",
16647 "FP for ARMv8", "FPv5/FP-D16 for ARMv8"};
16648 static const char *const arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
16649 static const char *const arm_attr_tag_Advanced_SIMD_arch[] =
16650 {"No", "NEONv1", "NEONv1 with Fused-MAC", "NEON for ARMv8",
16651 "NEON for ARMv8.1"};
16652 static const char *const arm_attr_tag_PCS_config[] =
16653 {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
16654 "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
16655 static const char *const arm_attr_tag_ABI_PCS_R9_use[] =
16656 {"V6", "SB", "TLS", "Unused"};
16657 static const char *const arm_attr_tag_ABI_PCS_RW_data[] =
16658 {"Absolute", "PC-relative", "SB-relative", "None"};
16659 static const char *const arm_attr_tag_ABI_PCS_RO_data[] =
16660 {"Absolute", "PC-relative", "None"};
16661 static const char *const arm_attr_tag_ABI_PCS_GOT_use[] =
16662 {"None", "direct", "GOT-indirect"};
16663 static const char *const arm_attr_tag_ABI_PCS_wchar_t[] =
16664 {"None", "??? 1", "2", "??? 3", "4"};
16665 static const char *const arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
16666 static const char *const arm_attr_tag_ABI_FP_denormal[] =
16667 {"Unused", "Needed", "Sign only"};
16668 static const char *const arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
16669 static const char *const arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
16670 static const char *const arm_attr_tag_ABI_FP_number_model[] =
16671 {"Unused", "Finite", "RTABI", "IEEE 754"};
16672 static const char *const arm_attr_tag_ABI_enum_size[] =
16673 {"Unused", "small", "int", "forced to int"};
16674 static const char *const arm_attr_tag_ABI_HardFP_use[] =
16675 {"As Tag_FP_arch", "SP only", "Reserved", "Deprecated"};
16676 static const char *const arm_attr_tag_ABI_VFP_args[] =
16677 {"AAPCS", "VFP registers", "custom", "compatible"};
16678 static const char *const arm_attr_tag_ABI_WMMX_args[] =
16679 {"AAPCS", "WMMX registers", "custom"};
16680 static const char *const arm_attr_tag_ABI_optimization_goals[] =
16681 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
16682 "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
16683 static const char *const arm_attr_tag_ABI_FP_optimization_goals[] =
16684 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
16685 "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
16686 static const char *const arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
16687 static const char *const arm_attr_tag_FP_HP_extension[] =
16688 {"Not Allowed", "Allowed"};
16689 static const char *const arm_attr_tag_ABI_FP_16bit_format[] =
16690 {"None", "IEEE 754", "Alternative Format"};
16691 static const char *const arm_attr_tag_DSP_extension[] =
16692 {"Follow architecture", "Allowed"};
16693 static const char *const arm_attr_tag_MPextension_use[] =
16694 {"Not Allowed", "Allowed"};
16695 static const char *const arm_attr_tag_DIV_use[] =
16696 {"Allowed in Thumb-ISA, v7-R or v7-M", "Not allowed",
16697 "Allowed in v7-A with integer division extension"};
16698 static const char *const arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
16699 static const char *const arm_attr_tag_Virtualization_use[] =
16700 {"Not Allowed", "TrustZone", "Virtualization Extensions",
16701 "TrustZone and Virtualization Extensions"};
16702 static const char *const arm_attr_tag_MPextension_use_legacy[] =
16703 {"Not Allowed", "Allowed"};
16704
16705 static const char *const arm_attr_tag_MVE_arch[] =
16706 {"No MVE", "MVE Integer only", "MVE Integer and FP"};
16707
16708 static const char * arm_attr_tag_PAC_extension[] =
16709 {"No PAC/AUT instructions",
16710 "PAC/AUT instructions permitted in the NOP space",
16711 "PAC/AUT instructions permitted in the NOP and in the non-NOP space"};
16712
16713 static const char * arm_attr_tag_BTI_extension[] =
16714 {"BTI instructions not permitted",
16715 "BTI instructions permitted in the NOP space",
16716 "BTI instructions permitted in the NOP and in the non-NOP space"};
16717
16718 static const char * arm_attr_tag_BTI_use[] =
16719 {"Compiled without branch target enforcement",
16720 "Compiled with branch target enforcement"};
16721
16722 static const char * arm_attr_tag_PACRET_use[] =
16723 {"Compiled without return address signing and authentication",
16724 "Compiled with return address signing and authentication"};
16725
16726 #define LOOKUP(id, name) \
16727 {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
16728 static arm_attr_public_tag arm_attr_public_tags[] =
16729 {
16730 {4, "CPU_raw_name", 1, NULL},
16731 {5, "CPU_name", 1, NULL},
16732 LOOKUP(6, CPU_arch),
16733 {7, "CPU_arch_profile", 0, NULL},
16734 LOOKUP(8, ARM_ISA_use),
16735 LOOKUP(9, THUMB_ISA_use),
16736 LOOKUP(10, FP_arch),
16737 LOOKUP(11, WMMX_arch),
16738 LOOKUP(12, Advanced_SIMD_arch),
16739 LOOKUP(13, PCS_config),
16740 LOOKUP(14, ABI_PCS_R9_use),
16741 LOOKUP(15, ABI_PCS_RW_data),
16742 LOOKUP(16, ABI_PCS_RO_data),
16743 LOOKUP(17, ABI_PCS_GOT_use),
16744 LOOKUP(18, ABI_PCS_wchar_t),
16745 LOOKUP(19, ABI_FP_rounding),
16746 LOOKUP(20, ABI_FP_denormal),
16747 LOOKUP(21, ABI_FP_exceptions),
16748 LOOKUP(22, ABI_FP_user_exceptions),
16749 LOOKUP(23, ABI_FP_number_model),
16750 {24, "ABI_align_needed", 0, NULL},
16751 {25, "ABI_align_preserved", 0, NULL},
16752 LOOKUP(26, ABI_enum_size),
16753 LOOKUP(27, ABI_HardFP_use),
16754 LOOKUP(28, ABI_VFP_args),
16755 LOOKUP(29, ABI_WMMX_args),
16756 LOOKUP(30, ABI_optimization_goals),
16757 LOOKUP(31, ABI_FP_optimization_goals),
16758 {32, "compatibility", 0, NULL},
16759 LOOKUP(34, CPU_unaligned_access),
16760 LOOKUP(36, FP_HP_extension),
16761 LOOKUP(38, ABI_FP_16bit_format),
16762 LOOKUP(42, MPextension_use),
16763 LOOKUP(44, DIV_use),
16764 LOOKUP(46, DSP_extension),
16765 LOOKUP(48, MVE_arch),
16766 LOOKUP(50, PAC_extension),
16767 LOOKUP(52, BTI_extension),
16768 LOOKUP(74, BTI_use),
16769 LOOKUP(76, PACRET_use),
16770 {64, "nodefaults", 0, NULL},
16771 {65, "also_compatible_with", 0, NULL},
16772 LOOKUP(66, T2EE_use),
16773 {67, "conformance", 1, NULL},
16774 LOOKUP(68, Virtualization_use),
16775 LOOKUP(70, MPextension_use_legacy)
16776 };
16777 #undef LOOKUP
16778
16779 static unsigned char *
16780 display_arm_attribute (unsigned char * p,
16781 const unsigned char * const end)
16782 {
16783 unsigned int tag;
16784 unsigned int val;
16785 arm_attr_public_tag * attr;
16786 unsigned i;
16787 unsigned int type;
16788
16789 READ_ULEB (tag, p, end);
16790 attr = NULL;
16791 for (i = 0; i < ARRAY_SIZE (arm_attr_public_tags); i++)
16792 {
16793 if (arm_attr_public_tags[i].tag == tag)
16794 {
16795 attr = &arm_attr_public_tags[i];
16796 break;
16797 }
16798 }
16799
16800 if (attr)
16801 {
16802 printf (" Tag_%s: ", attr->name);
16803 switch (attr->type)
16804 {
16805 case 0:
16806 switch (tag)
16807 {
16808 case 7: /* Tag_CPU_arch_profile. */
16809 READ_ULEB (val, p, end);
16810 switch (val)
16811 {
16812 case 0: printf (_("None\n")); break;
16813 case 'A': printf (_("Application\n")); break;
16814 case 'R': printf (_("Realtime\n")); break;
16815 case 'M': printf (_("Microcontroller\n")); break;
16816 case 'S': printf (_("Application or Realtime\n")); break;
16817 default: printf ("??? (%d)\n", val); break;
16818 }
16819 break;
16820
16821 case 24: /* Tag_align_needed. */
16822 READ_ULEB (val, p, end);
16823 switch (val)
16824 {
16825 case 0: printf (_("None\n")); break;
16826 case 1: printf (_("8-byte\n")); break;
16827 case 2: printf (_("4-byte\n")); break;
16828 case 3: printf ("??? 3\n"); break;
16829 default:
16830 if (val <= 12)
16831 printf (_("8-byte and up to %d-byte extended\n"),
16832 1 << val);
16833 else
16834 printf ("??? (%d)\n", val);
16835 break;
16836 }
16837 break;
16838
16839 case 25: /* Tag_align_preserved. */
16840 READ_ULEB (val, p, end);
16841 switch (val)
16842 {
16843 case 0: printf (_("None\n")); break;
16844 case 1: printf (_("8-byte, except leaf SP\n")); break;
16845 case 2: printf (_("8-byte\n")); break;
16846 case 3: printf ("??? 3\n"); break;
16847 default:
16848 if (val <= 12)
16849 printf (_("8-byte and up to %d-byte extended\n"),
16850 1 << val);
16851 else
16852 printf ("??? (%d)\n", val);
16853 break;
16854 }
16855 break;
16856
16857 case 32: /* Tag_compatibility. */
16858 {
16859 READ_ULEB (val, p, end);
16860 printf (_("flag = %d, vendor = "), val);
16861 if (p < end - 1)
16862 {
16863 size_t maxlen = (end - p) - 1;
16864
16865 print_symbol ((int) maxlen, (const char *) p);
16866 p += strnlen ((char *) p, maxlen) + 1;
16867 }
16868 else
16869 {
16870 printf (_("<corrupt>"));
16871 p = (unsigned char *) end;
16872 }
16873 putchar ('\n');
16874 }
16875 break;
16876
16877 case 64: /* Tag_nodefaults. */
16878 /* PR 17531: file: 001-505008-0.01. */
16879 if (p < end)
16880 p++;
16881 printf (_("True\n"));
16882 break;
16883
16884 case 65: /* Tag_also_compatible_with. */
16885 READ_ULEB (val, p, end);
16886 if (val == 6 /* Tag_CPU_arch. */)
16887 {
16888 READ_ULEB (val, p, end);
16889 if ((unsigned int) val >= ARRAY_SIZE (arm_attr_tag_CPU_arch))
16890 printf ("??? (%d)\n", val);
16891 else
16892 printf ("%s\n", arm_attr_tag_CPU_arch[val]);
16893 }
16894 else
16895 printf ("???\n");
16896 while (p < end && *(p++) != '\0' /* NUL terminator. */)
16897 ;
16898 break;
16899
16900 default:
16901 printf (_("<unknown: %d>\n"), tag);
16902 break;
16903 }
16904 return p;
16905
16906 case 1:
16907 return display_tag_value (-1, p, end);
16908 case 2:
16909 return display_tag_value (0, p, end);
16910
16911 default:
16912 assert (attr->type & 0x80);
16913 READ_ULEB (val, p, end);
16914 type = attr->type & 0x7f;
16915 if (val >= type)
16916 printf ("??? (%d)\n", val);
16917 else
16918 printf ("%s\n", attr->table[val]);
16919 return p;
16920 }
16921 }
16922
16923 return display_tag_value (tag, p, end);
16924 }
16925
16926 static unsigned char *
16927 display_gnu_attribute (unsigned char * p,
16928 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const),
16929 const unsigned char * const end)
16930 {
16931 unsigned int tag;
16932 unsigned int val;
16933
16934 READ_ULEB (tag, p, end);
16935
16936 /* Tag_compatibility is the only generic GNU attribute defined at
16937 present. */
16938 if (tag == 32)
16939 {
16940 READ_ULEB (val, p, end);
16941
16942 printf (_("flag = %d, vendor = "), val);
16943 if (p == end)
16944 {
16945 printf (_("<corrupt>\n"));
16946 warn (_("corrupt vendor attribute\n"));
16947 }
16948 else
16949 {
16950 if (p < end - 1)
16951 {
16952 size_t maxlen = (end - p) - 1;
16953
16954 print_symbol ((int) maxlen, (const char *) p);
16955 p += strnlen ((char *) p, maxlen) + 1;
16956 }
16957 else
16958 {
16959 printf (_("<corrupt>"));
16960 p = (unsigned char *) end;
16961 }
16962 putchar ('\n');
16963 }
16964 return p;
16965 }
16966
16967 if ((tag & 2) == 0 && display_proc_gnu_attribute)
16968 return display_proc_gnu_attribute (p, tag, end);
16969
16970 return display_tag_value (tag, p, end);
16971 }
16972
16973 static unsigned char *
16974 display_m68k_gnu_attribute (unsigned char * p,
16975 unsigned int tag,
16976 const unsigned char * const end)
16977 {
16978 unsigned int val;
16979
16980 if (tag == Tag_GNU_M68K_ABI_FP)
16981 {
16982 printf (" Tag_GNU_M68K_ABI_FP: ");
16983 if (p == end)
16984 {
16985 printf (_("<corrupt>\n"));
16986 return p;
16987 }
16988 READ_ULEB (val, p, end);
16989
16990 if (val > 3)
16991 printf ("(%#x), ", val);
16992
16993 switch (val & 3)
16994 {
16995 case 0:
16996 printf (_("unspecified hard/soft float\n"));
16997 break;
16998 case 1:
16999 printf (_("hard float\n"));
17000 break;
17001 case 2:
17002 printf (_("soft float\n"));
17003 break;
17004 }
17005 return p;
17006 }
17007
17008 return display_tag_value (tag & 1, p, end);
17009 }
17010
17011 static unsigned char *
17012 display_power_gnu_attribute (unsigned char * p,
17013 unsigned int tag,
17014 const unsigned char * const end)
17015 {
17016 unsigned int val;
17017
17018 if (tag == Tag_GNU_Power_ABI_FP)
17019 {
17020 printf (" Tag_GNU_Power_ABI_FP: ");
17021 if (p == end)
17022 {
17023 printf (_("<corrupt>\n"));
17024 return p;
17025 }
17026 READ_ULEB (val, p, end);
17027
17028 if (val > 15)
17029 printf ("(%#x), ", val);
17030
17031 switch (val & 3)
17032 {
17033 case 0:
17034 printf (_("unspecified hard/soft float, "));
17035 break;
17036 case 1:
17037 printf (_("hard float, "));
17038 break;
17039 case 2:
17040 printf (_("soft float, "));
17041 break;
17042 case 3:
17043 printf (_("single-precision hard float, "));
17044 break;
17045 }
17046
17047 switch (val & 0xC)
17048 {
17049 case 0:
17050 printf (_("unspecified long double\n"));
17051 break;
17052 case 4:
17053 printf (_("128-bit IBM long double\n"));
17054 break;
17055 case 8:
17056 printf (_("64-bit long double\n"));
17057 break;
17058 case 12:
17059 printf (_("128-bit IEEE long double\n"));
17060 break;
17061 }
17062 return p;
17063 }
17064
17065 if (tag == Tag_GNU_Power_ABI_Vector)
17066 {
17067 printf (" Tag_GNU_Power_ABI_Vector: ");
17068 if (p == end)
17069 {
17070 printf (_("<corrupt>\n"));
17071 return p;
17072 }
17073 READ_ULEB (val, p, end);
17074
17075 if (val > 3)
17076 printf ("(%#x), ", val);
17077
17078 switch (val & 3)
17079 {
17080 case 0:
17081 printf (_("unspecified\n"));
17082 break;
17083 case 1:
17084 printf (_("generic\n"));
17085 break;
17086 case 2:
17087 printf ("AltiVec\n");
17088 break;
17089 case 3:
17090 printf ("SPE\n");
17091 break;
17092 }
17093 return p;
17094 }
17095
17096 if (tag == Tag_GNU_Power_ABI_Struct_Return)
17097 {
17098 printf (" Tag_GNU_Power_ABI_Struct_Return: ");
17099 if (p == end)
17100 {
17101 printf (_("<corrupt>\n"));
17102 return p;
17103 }
17104 READ_ULEB (val, p, end);
17105
17106 if (val > 2)
17107 printf ("(%#x), ", val);
17108
17109 switch (val & 3)
17110 {
17111 case 0:
17112 printf (_("unspecified\n"));
17113 break;
17114 case 1:
17115 printf ("r3/r4\n");
17116 break;
17117 case 2:
17118 printf (_("memory\n"));
17119 break;
17120 case 3:
17121 printf ("???\n");
17122 break;
17123 }
17124 return p;
17125 }
17126
17127 return display_tag_value (tag & 1, p, end);
17128 }
17129
17130 static unsigned char *
17131 display_s390_gnu_attribute (unsigned char * p,
17132 unsigned int tag,
17133 const unsigned char * const end)
17134 {
17135 unsigned int val;
17136
17137 if (tag == Tag_GNU_S390_ABI_Vector)
17138 {
17139 printf (" Tag_GNU_S390_ABI_Vector: ");
17140 READ_ULEB (val, p, end);
17141
17142 switch (val)
17143 {
17144 case 0:
17145 printf (_("any\n"));
17146 break;
17147 case 1:
17148 printf (_("software\n"));
17149 break;
17150 case 2:
17151 printf (_("hardware\n"));
17152 break;
17153 default:
17154 printf ("??? (%d)\n", val);
17155 break;
17156 }
17157 return p;
17158 }
17159
17160 return display_tag_value (tag & 1, p, end);
17161 }
17162
17163 static void
17164 display_sparc_hwcaps (unsigned int mask)
17165 {
17166 if (mask)
17167 {
17168 bool first = true;
17169
17170 if (mask & ELF_SPARC_HWCAP_MUL32)
17171 fputs ("mul32", stdout), first = false;
17172 if (mask & ELF_SPARC_HWCAP_DIV32)
17173 printf ("%sdiv32", first ? "" : "|"), first = false;
17174 if (mask & ELF_SPARC_HWCAP_FSMULD)
17175 printf ("%sfsmuld", first ? "" : "|"), first = false;
17176 if (mask & ELF_SPARC_HWCAP_V8PLUS)
17177 printf ("%sv8plus", first ? "" : "|"), first = false;
17178 if (mask & ELF_SPARC_HWCAP_POPC)
17179 printf ("%spopc", first ? "" : "|"), first = false;
17180 if (mask & ELF_SPARC_HWCAP_VIS)
17181 printf ("%svis", first ? "" : "|"), first = false;
17182 if (mask & ELF_SPARC_HWCAP_VIS2)
17183 printf ("%svis2", first ? "" : "|"), first = false;
17184 if (mask & ELF_SPARC_HWCAP_ASI_BLK_INIT)
17185 printf ("%sASIBlkInit", first ? "" : "|"), first = false;
17186 if (mask & ELF_SPARC_HWCAP_FMAF)
17187 printf ("%sfmaf", first ? "" : "|"), first = false;
17188 if (mask & ELF_SPARC_HWCAP_VIS3)
17189 printf ("%svis3", first ? "" : "|"), first = false;
17190 if (mask & ELF_SPARC_HWCAP_HPC)
17191 printf ("%shpc", first ? "" : "|"), first = false;
17192 if (mask & ELF_SPARC_HWCAP_RANDOM)
17193 printf ("%srandom", first ? "" : "|"), first = false;
17194 if (mask & ELF_SPARC_HWCAP_TRANS)
17195 printf ("%strans", first ? "" : "|"), first = false;
17196 if (mask & ELF_SPARC_HWCAP_FJFMAU)
17197 printf ("%sfjfmau", first ? "" : "|"), first = false;
17198 if (mask & ELF_SPARC_HWCAP_IMA)
17199 printf ("%sima", first ? "" : "|"), first = false;
17200 if (mask & ELF_SPARC_HWCAP_ASI_CACHE_SPARING)
17201 printf ("%scspare", first ? "" : "|"), first = false;
17202 }
17203 else
17204 fputc ('0', stdout);
17205 fputc ('\n', stdout);
17206 }
17207
17208 static void
17209 display_sparc_hwcaps2 (unsigned int mask)
17210 {
17211 if (mask)
17212 {
17213 bool first = true;
17214
17215 if (mask & ELF_SPARC_HWCAP2_FJATHPLUS)
17216 fputs ("fjathplus", stdout), first = false;
17217 if (mask & ELF_SPARC_HWCAP2_VIS3B)
17218 printf ("%svis3b", first ? "" : "|"), first = false;
17219 if (mask & ELF_SPARC_HWCAP2_ADP)
17220 printf ("%sadp", first ? "" : "|"), first = false;
17221 if (mask & ELF_SPARC_HWCAP2_SPARC5)
17222 printf ("%ssparc5", first ? "" : "|"), first = false;
17223 if (mask & ELF_SPARC_HWCAP2_MWAIT)
17224 printf ("%smwait", first ? "" : "|"), first = false;
17225 if (mask & ELF_SPARC_HWCAP2_XMPMUL)
17226 printf ("%sxmpmul", first ? "" : "|"), first = false;
17227 if (mask & ELF_SPARC_HWCAP2_XMONT)
17228 printf ("%sxmont2", first ? "" : "|"), first = false;
17229 if (mask & ELF_SPARC_HWCAP2_NSEC)
17230 printf ("%snsec", first ? "" : "|"), first = false;
17231 if (mask & ELF_SPARC_HWCAP2_FJATHHPC)
17232 printf ("%sfjathhpc", first ? "" : "|"), first = false;
17233 if (mask & ELF_SPARC_HWCAP2_FJDES)
17234 printf ("%sfjdes", first ? "" : "|"), first = false;
17235 if (mask & ELF_SPARC_HWCAP2_FJAES)
17236 printf ("%sfjaes", first ? "" : "|"), first = false;
17237 }
17238 else
17239 fputc ('0', stdout);
17240 fputc ('\n', stdout);
17241 }
17242
17243 static unsigned char *
17244 display_sparc_gnu_attribute (unsigned char * p,
17245 unsigned int tag,
17246 const unsigned char * const end)
17247 {
17248 unsigned int val;
17249
17250 if (tag == Tag_GNU_Sparc_HWCAPS)
17251 {
17252 READ_ULEB (val, p, end);
17253 printf (" Tag_GNU_Sparc_HWCAPS: ");
17254 display_sparc_hwcaps (val);
17255 return p;
17256 }
17257 if (tag == Tag_GNU_Sparc_HWCAPS2)
17258 {
17259 READ_ULEB (val, p, end);
17260 printf (" Tag_GNU_Sparc_HWCAPS2: ");
17261 display_sparc_hwcaps2 (val);
17262 return p;
17263 }
17264
17265 return display_tag_value (tag, p, end);
17266 }
17267
17268 static void
17269 print_mips_fp_abi_value (unsigned int val)
17270 {
17271 switch (val)
17272 {
17273 case Val_GNU_MIPS_ABI_FP_ANY:
17274 printf (_("Hard or soft float\n"));
17275 break;
17276 case Val_GNU_MIPS_ABI_FP_DOUBLE:
17277 printf (_("Hard float (double precision)\n"));
17278 break;
17279 case Val_GNU_MIPS_ABI_FP_SINGLE:
17280 printf (_("Hard float (single precision)\n"));
17281 break;
17282 case Val_GNU_MIPS_ABI_FP_SOFT:
17283 printf (_("Soft float\n"));
17284 break;
17285 case Val_GNU_MIPS_ABI_FP_OLD_64:
17286 printf (_("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
17287 break;
17288 case Val_GNU_MIPS_ABI_FP_XX:
17289 printf (_("Hard float (32-bit CPU, Any FPU)\n"));
17290 break;
17291 case Val_GNU_MIPS_ABI_FP_64:
17292 printf (_("Hard float (32-bit CPU, 64-bit FPU)\n"));
17293 break;
17294 case Val_GNU_MIPS_ABI_FP_64A:
17295 printf (_("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
17296 break;
17297 case Val_GNU_MIPS_ABI_FP_NAN2008:
17298 printf (_("NaN 2008 compatibility\n"));
17299 break;
17300 default:
17301 printf ("??? (%d)\n", val);
17302 break;
17303 }
17304 }
17305
17306 static unsigned char *
17307 display_mips_gnu_attribute (unsigned char * p,
17308 unsigned int tag,
17309 const unsigned char * const end)
17310 {
17311 if (tag == Tag_GNU_MIPS_ABI_FP)
17312 {
17313 unsigned int val;
17314
17315 printf (" Tag_GNU_MIPS_ABI_FP: ");
17316 READ_ULEB (val, p, end);
17317 print_mips_fp_abi_value (val);
17318 return p;
17319 }
17320
17321 if (tag == Tag_GNU_MIPS_ABI_MSA)
17322 {
17323 unsigned int val;
17324
17325 printf (" Tag_GNU_MIPS_ABI_MSA: ");
17326 READ_ULEB (val, p, end);
17327
17328 switch (val)
17329 {
17330 case Val_GNU_MIPS_ABI_MSA_ANY:
17331 printf (_("Any MSA or not\n"));
17332 break;
17333 case Val_GNU_MIPS_ABI_MSA_128:
17334 printf (_("128-bit MSA\n"));
17335 break;
17336 default:
17337 printf ("??? (%d)\n", val);
17338 break;
17339 }
17340 return p;
17341 }
17342
17343 return display_tag_value (tag & 1, p, end);
17344 }
17345
17346 static unsigned char *
17347 display_tic6x_attribute (unsigned char * p,
17348 const unsigned char * const end)
17349 {
17350 unsigned int tag;
17351 unsigned int val;
17352
17353 READ_ULEB (tag, p, end);
17354
17355 switch (tag)
17356 {
17357 case Tag_ISA:
17358 printf (" Tag_ISA: ");
17359 READ_ULEB (val, p, end);
17360
17361 switch (val)
17362 {
17363 case C6XABI_Tag_ISA_none:
17364 printf (_("None\n"));
17365 break;
17366 case C6XABI_Tag_ISA_C62X:
17367 printf ("C62x\n");
17368 break;
17369 case C6XABI_Tag_ISA_C67X:
17370 printf ("C67x\n");
17371 break;
17372 case C6XABI_Tag_ISA_C67XP:
17373 printf ("C67x+\n");
17374 break;
17375 case C6XABI_Tag_ISA_C64X:
17376 printf ("C64x\n");
17377 break;
17378 case C6XABI_Tag_ISA_C64XP:
17379 printf ("C64x+\n");
17380 break;
17381 case C6XABI_Tag_ISA_C674X:
17382 printf ("C674x\n");
17383 break;
17384 default:
17385 printf ("??? (%d)\n", val);
17386 break;
17387 }
17388 return p;
17389
17390 case Tag_ABI_wchar_t:
17391 printf (" Tag_ABI_wchar_t: ");
17392 READ_ULEB (val, p, end);
17393 switch (val)
17394 {
17395 case 0:
17396 printf (_("Not used\n"));
17397 break;
17398 case 1:
17399 printf (_("2 bytes\n"));
17400 break;
17401 case 2:
17402 printf (_("4 bytes\n"));
17403 break;
17404 default:
17405 printf ("??? (%d)\n", val);
17406 break;
17407 }
17408 return p;
17409
17410 case Tag_ABI_stack_align_needed:
17411 printf (" Tag_ABI_stack_align_needed: ");
17412 READ_ULEB (val, p, end);
17413 switch (val)
17414 {
17415 case 0:
17416 printf (_("8-byte\n"));
17417 break;
17418 case 1:
17419 printf (_("16-byte\n"));
17420 break;
17421 default:
17422 printf ("??? (%d)\n", val);
17423 break;
17424 }
17425 return p;
17426
17427 case Tag_ABI_stack_align_preserved:
17428 READ_ULEB (val, p, end);
17429 printf (" Tag_ABI_stack_align_preserved: ");
17430 switch (val)
17431 {
17432 case 0:
17433 printf (_("8-byte\n"));
17434 break;
17435 case 1:
17436 printf (_("16-byte\n"));
17437 break;
17438 default:
17439 printf ("??? (%d)\n", val);
17440 break;
17441 }
17442 return p;
17443
17444 case Tag_ABI_DSBT:
17445 READ_ULEB (val, p, end);
17446 printf (" Tag_ABI_DSBT: ");
17447 switch (val)
17448 {
17449 case 0:
17450 printf (_("DSBT addressing not used\n"));
17451 break;
17452 case 1:
17453 printf (_("DSBT addressing used\n"));
17454 break;
17455 default:
17456 printf ("??? (%d)\n", val);
17457 break;
17458 }
17459 return p;
17460
17461 case Tag_ABI_PID:
17462 READ_ULEB (val, p, end);
17463 printf (" Tag_ABI_PID: ");
17464 switch (val)
17465 {
17466 case 0:
17467 printf (_("Data addressing position-dependent\n"));
17468 break;
17469 case 1:
17470 printf (_("Data addressing position-independent, GOT near DP\n"));
17471 break;
17472 case 2:
17473 printf (_("Data addressing position-independent, GOT far from DP\n"));
17474 break;
17475 default:
17476 printf ("??? (%d)\n", val);
17477 break;
17478 }
17479 return p;
17480
17481 case Tag_ABI_PIC:
17482 READ_ULEB (val, p, end);
17483 printf (" Tag_ABI_PIC: ");
17484 switch (val)
17485 {
17486 case 0:
17487 printf (_("Code addressing position-dependent\n"));
17488 break;
17489 case 1:
17490 printf (_("Code addressing position-independent\n"));
17491 break;
17492 default:
17493 printf ("??? (%d)\n", val);
17494 break;
17495 }
17496 return p;
17497
17498 case Tag_ABI_array_object_alignment:
17499 READ_ULEB (val, p, end);
17500 printf (" Tag_ABI_array_object_alignment: ");
17501 switch (val)
17502 {
17503 case 0:
17504 printf (_("8-byte\n"));
17505 break;
17506 case 1:
17507 printf (_("4-byte\n"));
17508 break;
17509 case 2:
17510 printf (_("16-byte\n"));
17511 break;
17512 default:
17513 printf ("??? (%d)\n", val);
17514 break;
17515 }
17516 return p;
17517
17518 case Tag_ABI_array_object_align_expected:
17519 READ_ULEB (val, p, end);
17520 printf (" Tag_ABI_array_object_align_expected: ");
17521 switch (val)
17522 {
17523 case 0:
17524 printf (_("8-byte\n"));
17525 break;
17526 case 1:
17527 printf (_("4-byte\n"));
17528 break;
17529 case 2:
17530 printf (_("16-byte\n"));
17531 break;
17532 default:
17533 printf ("??? (%d)\n", val);
17534 break;
17535 }
17536 return p;
17537
17538 case Tag_ABI_compatibility:
17539 {
17540 READ_ULEB (val, p, end);
17541 printf (" Tag_ABI_compatibility: ");
17542 printf (_("flag = %d, vendor = "), val);
17543 if (p < end - 1)
17544 {
17545 size_t maxlen = (end - p) - 1;
17546
17547 print_symbol ((int) maxlen, (const char *) p);
17548 p += strnlen ((char *) p, maxlen) + 1;
17549 }
17550 else
17551 {
17552 printf (_("<corrupt>"));
17553 p = (unsigned char *) end;
17554 }
17555 putchar ('\n');
17556 return p;
17557 }
17558
17559 case Tag_ABI_conformance:
17560 {
17561 printf (" Tag_ABI_conformance: \"");
17562 if (p < end - 1)
17563 {
17564 size_t maxlen = (end - p) - 1;
17565
17566 print_symbol ((int) maxlen, (const char *) p);
17567 p += strnlen ((char *) p, maxlen) + 1;
17568 }
17569 else
17570 {
17571 printf (_("<corrupt>"));
17572 p = (unsigned char *) end;
17573 }
17574 printf ("\"\n");
17575 return p;
17576 }
17577 }
17578
17579 return display_tag_value (tag, p, end);
17580 }
17581
17582 static void
17583 display_raw_attribute (unsigned char * p, unsigned char const * const end)
17584 {
17585 unsigned long addr = 0;
17586 size_t bytes = end - p;
17587
17588 assert (end >= p);
17589 while (bytes)
17590 {
17591 int j;
17592 int k;
17593 int lbytes = (bytes > 16 ? 16 : bytes);
17594
17595 printf (" 0x%8.8lx ", addr);
17596
17597 for (j = 0; j < 16; j++)
17598 {
17599 if (j < lbytes)
17600 printf ("%2.2x", p[j]);
17601 else
17602 printf (" ");
17603
17604 if ((j & 3) == 3)
17605 printf (" ");
17606 }
17607
17608 for (j = 0; j < lbytes; j++)
17609 {
17610 k = p[j];
17611 if (k >= ' ' && k < 0x7f)
17612 printf ("%c", k);
17613 else
17614 printf (".");
17615 }
17616
17617 putchar ('\n');
17618
17619 p += lbytes;
17620 bytes -= lbytes;
17621 addr += lbytes;
17622 }
17623
17624 putchar ('\n');
17625 }
17626
17627 static unsigned char *
17628 display_msp430_attribute (unsigned char * p,
17629 const unsigned char * const end)
17630 {
17631 unsigned int val;
17632 unsigned int tag;
17633
17634 READ_ULEB (tag, p, end);
17635
17636 switch (tag)
17637 {
17638 case OFBA_MSPABI_Tag_ISA:
17639 printf (" Tag_ISA: ");
17640 READ_ULEB (val, p, end);
17641 switch (val)
17642 {
17643 case 0: printf (_("None\n")); break;
17644 case 1: printf (_("MSP430\n")); break;
17645 case 2: printf (_("MSP430X\n")); break;
17646 default: printf ("??? (%d)\n", val); break;
17647 }
17648 break;
17649
17650 case OFBA_MSPABI_Tag_Code_Model:
17651 printf (" Tag_Code_Model: ");
17652 READ_ULEB (val, p, end);
17653 switch (val)
17654 {
17655 case 0: printf (_("None\n")); break;
17656 case 1: printf (_("Small\n")); break;
17657 case 2: printf (_("Large\n")); break;
17658 default: printf ("??? (%d)\n", val); break;
17659 }
17660 break;
17661
17662 case OFBA_MSPABI_Tag_Data_Model:
17663 printf (" Tag_Data_Model: ");
17664 READ_ULEB (val, p, end);
17665 switch (val)
17666 {
17667 case 0: printf (_("None\n")); break;
17668 case 1: printf (_("Small\n")); break;
17669 case 2: printf (_("Large\n")); break;
17670 case 3: printf (_("Restricted Large\n")); break;
17671 default: printf ("??? (%d)\n", val); break;
17672 }
17673 break;
17674
17675 default:
17676 printf (_(" <unknown tag %d>: "), tag);
17677
17678 if (tag & 1)
17679 {
17680 putchar ('"');
17681 if (p < end - 1)
17682 {
17683 size_t maxlen = (end - p) - 1;
17684
17685 print_symbol ((int) maxlen, (const char *) p);
17686 p += strnlen ((char *) p, maxlen) + 1;
17687 }
17688 else
17689 {
17690 printf (_("<corrupt>"));
17691 p = (unsigned char *) end;
17692 }
17693 printf ("\"\n");
17694 }
17695 else
17696 {
17697 READ_ULEB (val, p, end);
17698 printf ("%d (0x%x)\n", val, val);
17699 }
17700 break;
17701 }
17702
17703 assert (p <= end);
17704 return p;
17705 }
17706
17707 static unsigned char *
17708 display_msp430_gnu_attribute (unsigned char * p,
17709 unsigned int tag,
17710 const unsigned char * const end)
17711 {
17712 if (tag == Tag_GNU_MSP430_Data_Region)
17713 {
17714 unsigned int val;
17715
17716 printf (" Tag_GNU_MSP430_Data_Region: ");
17717 READ_ULEB (val, p, end);
17718
17719 switch (val)
17720 {
17721 case Val_GNU_MSP430_Data_Region_Any:
17722 printf (_("Any Region\n"));
17723 break;
17724 case Val_GNU_MSP430_Data_Region_Lower:
17725 printf (_("Lower Region Only\n"));
17726 break;
17727 default:
17728 printf ("??? (%u)\n", val);
17729 }
17730 return p;
17731 }
17732 return display_tag_value (tag & 1, p, end);
17733 }
17734
17735 struct riscv_attr_tag_t {
17736 const char *name;
17737 unsigned int tag;
17738 };
17739
17740 static struct riscv_attr_tag_t riscv_attr_tag[] =
17741 {
17742 #define T(tag) {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
17743 T(arch),
17744 T(priv_spec),
17745 T(priv_spec_minor),
17746 T(priv_spec_revision),
17747 T(unaligned_access),
17748 T(stack_align),
17749 #undef T
17750 };
17751
17752 static unsigned char *
17753 display_riscv_attribute (unsigned char *p,
17754 const unsigned char * const end)
17755 {
17756 unsigned int val;
17757 unsigned int tag;
17758 struct riscv_attr_tag_t *attr = NULL;
17759 unsigned i;
17760
17761 READ_ULEB (tag, p, end);
17762
17763 /* Find the name of attribute. */
17764 for (i = 0; i < ARRAY_SIZE (riscv_attr_tag); i++)
17765 {
17766 if (riscv_attr_tag[i].tag == tag)
17767 {
17768 attr = &riscv_attr_tag[i];
17769 break;
17770 }
17771 }
17772
17773 if (attr)
17774 printf (" %s: ", attr->name);
17775 else
17776 return display_tag_value (tag, p, end);
17777
17778 switch (tag)
17779 {
17780 case Tag_RISCV_priv_spec:
17781 case Tag_RISCV_priv_spec_minor:
17782 case Tag_RISCV_priv_spec_revision:
17783 READ_ULEB (val, p, end);
17784 printf (_("%u\n"), val);
17785 break;
17786 case Tag_RISCV_unaligned_access:
17787 READ_ULEB (val, p, end);
17788 switch (val)
17789 {
17790 case 0:
17791 printf (_("No unaligned access\n"));
17792 break;
17793 case 1:
17794 printf (_("Unaligned access\n"));
17795 break;
17796 }
17797 break;
17798 case Tag_RISCV_stack_align:
17799 READ_ULEB (val, p, end);
17800 printf (_("%u-bytes\n"), val);
17801 break;
17802 case Tag_RISCV_arch:
17803 p = display_tag_value (-1, p, end);
17804 break;
17805 default:
17806 return display_tag_value (tag, p, end);
17807 }
17808
17809 return p;
17810 }
17811
17812 static unsigned char *
17813 display_csky_attribute (unsigned char * p,
17814 const unsigned char * const end)
17815 {
17816 unsigned int tag;
17817 unsigned int val;
17818 READ_ULEB (tag, p, end);
17819
17820 if (tag >= Tag_CSKY_MAX)
17821 {
17822 return display_tag_value (-1, p, end);
17823 }
17824
17825 switch (tag)
17826 {
17827 case Tag_CSKY_ARCH_NAME:
17828 printf (" Tag_CSKY_ARCH_NAME:\t\t");
17829 return display_tag_value (-1, p, end);
17830 case Tag_CSKY_CPU_NAME:
17831 printf (" Tag_CSKY_CPU_NAME:\t\t");
17832 return display_tag_value (-1, p, end);
17833
17834 case Tag_CSKY_ISA_FLAGS:
17835 printf (" Tag_CSKY_ISA_FLAGS:\t\t");
17836 return display_tag_value (0, p, end);
17837 case Tag_CSKY_ISA_EXT_FLAGS:
17838 printf (" Tag_CSKY_ISA_EXT_FLAGS:\t");
17839 return display_tag_value (0, p, end);
17840
17841 case Tag_CSKY_DSP_VERSION:
17842 printf (" Tag_CSKY_DSP_VERSION:\t\t");
17843 READ_ULEB (val, p, end);
17844 if (val == VAL_CSKY_DSP_VERSION_EXTENSION)
17845 printf ("DSP Extension\n");
17846 else if (val == VAL_CSKY_DSP_VERSION_2)
17847 printf ("DSP 2.0\n");
17848 break;
17849
17850 case Tag_CSKY_VDSP_VERSION:
17851 printf (" Tag_CSKY_VDSP_VERSION:\t");
17852 READ_ULEB (val, p, end);
17853 printf ("VDSP Version %d\n", val);
17854 break;
17855
17856 case Tag_CSKY_FPU_VERSION:
17857 printf (" Tag_CSKY_FPU_VERSION:\t\t");
17858 READ_ULEB (val, p, end);
17859 if (val == VAL_CSKY_FPU_VERSION_1)
17860 printf ("ABIV1 FPU Version 1\n");
17861 else if (val == VAL_CSKY_FPU_VERSION_2)
17862 printf ("FPU Version 2\n");
17863 break;
17864
17865 case Tag_CSKY_FPU_ABI:
17866 printf (" Tag_CSKY_FPU_ABI:\t\t");
17867 READ_ULEB (val, p, end);
17868 if (val == VAL_CSKY_FPU_ABI_HARD)
17869 printf ("Hard\n");
17870 else if (val == VAL_CSKY_FPU_ABI_SOFTFP)
17871 printf ("SoftFP\n");
17872 else if (val == VAL_CSKY_FPU_ABI_SOFT)
17873 printf ("Soft\n");
17874 break;
17875 case Tag_CSKY_FPU_ROUNDING:
17876 READ_ULEB (val, p, end);
17877 if (val == 1)
17878 {
17879 printf (" Tag_CSKY_FPU_ROUNDING:\t");
17880 printf ("Needed\n");
17881 }
17882 break;
17883 case Tag_CSKY_FPU_DENORMAL:
17884 READ_ULEB (val, p, end);
17885 if (val == 1)
17886 {
17887 printf (" Tag_CSKY_FPU_DENORMAL:\t");
17888 printf ("Needed\n");
17889 }
17890 break;
17891 case Tag_CSKY_FPU_Exception:
17892 READ_ULEB (val, p, end);
17893 if (val == 1)
17894 {
17895 printf (" Tag_CSKY_FPU_Exception:\t");
17896 printf ("Needed\n");
17897 }
17898 break;
17899 case Tag_CSKY_FPU_NUMBER_MODULE:
17900 printf (" Tag_CSKY_FPU_NUMBER_MODULE:\t");
17901 return display_tag_value (-1, p, end);
17902 case Tag_CSKY_FPU_HARDFP:
17903 printf (" Tag_CSKY_FPU_HARDFP:\t\t");
17904 READ_ULEB (val, p, end);
17905 if (val & VAL_CSKY_FPU_HARDFP_HALF)
17906 printf (" Half");
17907 if (val & VAL_CSKY_FPU_HARDFP_SINGLE)
17908 printf (" Single");
17909 if (val & VAL_CSKY_FPU_HARDFP_DOUBLE)
17910 printf (" Double");
17911 printf ("\n");
17912 break;
17913 default:
17914 return display_tag_value (tag, p, end);
17915 }
17916 return p;
17917 }
17918
17919 static bool
17920 process_attributes (Filedata * filedata,
17921 const char * public_name,
17922 unsigned int proc_type,
17923 unsigned char * (* display_pub_attribute) (unsigned char *, const unsigned char * const),
17924 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const))
17925 {
17926 Elf_Internal_Shdr * sect;
17927 unsigned i;
17928 bool res = true;
17929
17930 /* Find the section header so that we get the size. */
17931 for (i = 0, sect = filedata->section_headers;
17932 i < filedata->file_header.e_shnum;
17933 i++, sect++)
17934 {
17935 unsigned char * contents;
17936 unsigned char * p;
17937
17938 if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
17939 continue;
17940
17941 contents = (unsigned char *) get_data (NULL, filedata, sect->sh_offset, 1,
17942 sect->sh_size, _("attributes"));
17943 if (contents == NULL)
17944 {
17945 res = false;
17946 continue;
17947 }
17948
17949 p = contents;
17950 /* The first character is the version of the attributes.
17951 Currently only version 1, (aka 'A') is recognised here. */
17952 if (*p != 'A')
17953 {
17954 printf (_("Unknown attributes version '%c'(%d) - expecting 'A'\n"), *p, *p);
17955 res = false;
17956 }
17957 else
17958 {
17959 uint64_t section_len;
17960
17961 section_len = sect->sh_size - 1;
17962 p++;
17963
17964 while (section_len > 0)
17965 {
17966 uint64_t attr_len;
17967 unsigned int namelen;
17968 bool public_section;
17969 bool gnu_section;
17970
17971 if (section_len <= 4)
17972 {
17973 error (_("Tag section ends prematurely\n"));
17974 res = false;
17975 break;
17976 }
17977 attr_len = byte_get (p, 4);
17978 p += 4;
17979
17980 if (attr_len > section_len)
17981 {
17982 error (_("Bad attribute length (%u > %u)\n"),
17983 (unsigned) attr_len, (unsigned) section_len);
17984 attr_len = section_len;
17985 res = false;
17986 }
17987 /* PR 17531: file: 001-101425-0.004 */
17988 else if (attr_len < 5)
17989 {
17990 error (_("Attribute length of %u is too small\n"), (unsigned) attr_len);
17991 res = false;
17992 break;
17993 }
17994
17995 section_len -= attr_len;
17996 attr_len -= 4;
17997
17998 namelen = strnlen ((char *) p, attr_len) + 1;
17999 if (namelen == 0 || namelen >= attr_len)
18000 {
18001 error (_("Corrupt attribute section name\n"));
18002 res = false;
18003 break;
18004 }
18005
18006 printf (_("Attribute Section: "));
18007 print_symbol (INT_MAX, (const char *) p);
18008 putchar ('\n');
18009
18010 if (public_name && streq ((char *) p, public_name))
18011 public_section = true;
18012 else
18013 public_section = false;
18014
18015 if (streq ((char *) p, "gnu"))
18016 gnu_section = true;
18017 else
18018 gnu_section = false;
18019
18020 p += namelen;
18021 attr_len -= namelen;
18022
18023 while (attr_len > 0 && p < contents + sect->sh_size)
18024 {
18025 int tag;
18026 unsigned int val;
18027 uint64_t size;
18028 unsigned char * end;
18029
18030 /* PR binutils/17531: Safe handling of corrupt files. */
18031 if (attr_len < 6)
18032 {
18033 error (_("Unused bytes at end of section\n"));
18034 res = false;
18035 section_len = 0;
18036 break;
18037 }
18038
18039 tag = *(p++);
18040 size = byte_get (p, 4);
18041 if (size > attr_len)
18042 {
18043 error (_("Bad subsection length (%u > %u)\n"),
18044 (unsigned) size, (unsigned) attr_len);
18045 res = false;
18046 size = attr_len;
18047 }
18048 /* PR binutils/17531: Safe handling of corrupt files. */
18049 if (size < 6)
18050 {
18051 error (_("Bad subsection length (%u < 6)\n"),
18052 (unsigned) size);
18053 res = false;
18054 section_len = 0;
18055 break;
18056 }
18057
18058 attr_len -= size;
18059 end = p + size - 1;
18060 assert (end <= contents + sect->sh_size);
18061 p += 4;
18062
18063 switch (tag)
18064 {
18065 case 1:
18066 printf (_("File Attributes\n"));
18067 break;
18068 case 2:
18069 printf (_("Section Attributes:"));
18070 goto do_numlist;
18071 case 3:
18072 printf (_("Symbol Attributes:"));
18073 /* Fall through. */
18074 do_numlist:
18075 for (;;)
18076 {
18077 READ_ULEB (val, p, end);
18078 if (val == 0)
18079 break;
18080 printf (" %d", val);
18081 }
18082 printf ("\n");
18083 break;
18084 default:
18085 printf (_("Unknown tag: %d\n"), tag);
18086 public_section = false;
18087 break;
18088 }
18089
18090 if (public_section && display_pub_attribute != NULL)
18091 {
18092 while (p < end)
18093 p = display_pub_attribute (p, end);
18094 assert (p == end);
18095 }
18096 else if (gnu_section && display_proc_gnu_attribute != NULL)
18097 {
18098 while (p < end)
18099 p = display_gnu_attribute (p,
18100 display_proc_gnu_attribute,
18101 end);
18102 assert (p == end);
18103 }
18104 else if (p < end)
18105 {
18106 printf (_(" Unknown attribute:\n"));
18107 display_raw_attribute (p, end);
18108 p = end;
18109 }
18110 else
18111 attr_len = 0;
18112 }
18113 }
18114 }
18115
18116 free (contents);
18117 }
18118
18119 return res;
18120 }
18121
18122 /* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
18123 Print the Address, Access and Initial fields of an entry at VMA ADDR
18124 and return the VMA of the next entry, or -1 if there was a problem.
18125 Does not read from DATA_END or beyond. */
18126
18127 static uint64_t
18128 print_mips_got_entry (unsigned char * data, uint64_t pltgot, uint64_t addr,
18129 unsigned char * data_end)
18130 {
18131 printf (" ");
18132 print_vma (addr, LONG_HEX);
18133 printf (" ");
18134 if (addr < pltgot + 0xfff0)
18135 printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
18136 else
18137 printf ("%10s", "");
18138 printf (" ");
18139 if (data == NULL)
18140 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
18141 else
18142 {
18143 uint64_t entry;
18144 unsigned char * from = data + addr - pltgot;
18145
18146 if (from + (is_32bit_elf ? 4 : 8) > data_end)
18147 {
18148 warn (_("MIPS GOT entry extends beyond the end of available data\n"));
18149 printf ("%*s", is_32bit_elf ? 8 : 16, _("<corrupt>"));
18150 return (uint64_t) -1;
18151 }
18152 else
18153 {
18154 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
18155 print_vma (entry, LONG_HEX);
18156 }
18157 }
18158 return addr + (is_32bit_elf ? 4 : 8);
18159 }
18160
18161 /* DATA points to the contents of a MIPS PLT GOT that starts at VMA
18162 PLTGOT. Print the Address and Initial fields of an entry at VMA
18163 ADDR and return the VMA of the next entry. */
18164
18165 static uint64_t
18166 print_mips_pltgot_entry (unsigned char * data, uint64_t pltgot, uint64_t addr)
18167 {
18168 printf (" ");
18169 print_vma (addr, LONG_HEX);
18170 printf (" ");
18171 if (data == NULL)
18172 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
18173 else
18174 {
18175 uint64_t entry;
18176
18177 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
18178 print_vma (entry, LONG_HEX);
18179 }
18180 return addr + (is_32bit_elf ? 4 : 8);
18181 }
18182
18183 static void
18184 print_mips_ases (unsigned int mask)
18185 {
18186 if (mask & AFL_ASE_DSP)
18187 fputs ("\n\tDSP ASE", stdout);
18188 if (mask & AFL_ASE_DSPR2)
18189 fputs ("\n\tDSP R2 ASE", stdout);
18190 if (mask & AFL_ASE_DSPR3)
18191 fputs ("\n\tDSP R3 ASE", stdout);
18192 if (mask & AFL_ASE_EVA)
18193 fputs ("\n\tEnhanced VA Scheme", stdout);
18194 if (mask & AFL_ASE_MCU)
18195 fputs ("\n\tMCU (MicroController) ASE", stdout);
18196 if (mask & AFL_ASE_MDMX)
18197 fputs ("\n\tMDMX ASE", stdout);
18198 if (mask & AFL_ASE_MIPS3D)
18199 fputs ("\n\tMIPS-3D ASE", stdout);
18200 if (mask & AFL_ASE_MT)
18201 fputs ("\n\tMT ASE", stdout);
18202 if (mask & AFL_ASE_SMARTMIPS)
18203 fputs ("\n\tSmartMIPS ASE", stdout);
18204 if (mask & AFL_ASE_VIRT)
18205 fputs ("\n\tVZ ASE", stdout);
18206 if (mask & AFL_ASE_MSA)
18207 fputs ("\n\tMSA ASE", stdout);
18208 if (mask & AFL_ASE_MIPS16)
18209 fputs ("\n\tMIPS16 ASE", stdout);
18210 if (mask & AFL_ASE_MICROMIPS)
18211 fputs ("\n\tMICROMIPS ASE", stdout);
18212 if (mask & AFL_ASE_XPA)
18213 fputs ("\n\tXPA ASE", stdout);
18214 if (mask & AFL_ASE_MIPS16E2)
18215 fputs ("\n\tMIPS16e2 ASE", stdout);
18216 if (mask & AFL_ASE_CRC)
18217 fputs ("\n\tCRC ASE", stdout);
18218 if (mask & AFL_ASE_GINV)
18219 fputs ("\n\tGINV ASE", stdout);
18220 if (mask & AFL_ASE_LOONGSON_MMI)
18221 fputs ("\n\tLoongson MMI ASE", stdout);
18222 if (mask & AFL_ASE_LOONGSON_CAM)
18223 fputs ("\n\tLoongson CAM ASE", stdout);
18224 if (mask & AFL_ASE_LOONGSON_EXT)
18225 fputs ("\n\tLoongson EXT ASE", stdout);
18226 if (mask & AFL_ASE_LOONGSON_EXT2)
18227 fputs ("\n\tLoongson EXT2 ASE", stdout);
18228 if (mask == 0)
18229 fprintf (stdout, "\n\t%s", _("None"));
18230 else if ((mask & ~AFL_ASE_MASK) != 0)
18231 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
18232 }
18233
18234 static void
18235 print_mips_isa_ext (unsigned int isa_ext)
18236 {
18237 switch (isa_ext)
18238 {
18239 case 0:
18240 fputs (_("None"), stdout);
18241 break;
18242 case AFL_EXT_XLR:
18243 fputs ("RMI XLR", stdout);
18244 break;
18245 case AFL_EXT_OCTEON3:
18246 fputs ("Cavium Networks Octeon3", stdout);
18247 break;
18248 case AFL_EXT_OCTEON2:
18249 fputs ("Cavium Networks Octeon2", stdout);
18250 break;
18251 case AFL_EXT_OCTEONP:
18252 fputs ("Cavium Networks OcteonP", stdout);
18253 break;
18254 case AFL_EXT_OCTEON:
18255 fputs ("Cavium Networks Octeon", stdout);
18256 break;
18257 case AFL_EXT_5900:
18258 fputs ("Toshiba R5900", stdout);
18259 break;
18260 case AFL_EXT_4650:
18261 fputs ("MIPS R4650", stdout);
18262 break;
18263 case AFL_EXT_4010:
18264 fputs ("LSI R4010", stdout);
18265 break;
18266 case AFL_EXT_4100:
18267 fputs ("NEC VR4100", stdout);
18268 break;
18269 case AFL_EXT_3900:
18270 fputs ("Toshiba R3900", stdout);
18271 break;
18272 case AFL_EXT_10000:
18273 fputs ("MIPS R10000", stdout);
18274 break;
18275 case AFL_EXT_SB1:
18276 fputs ("Broadcom SB-1", stdout);
18277 break;
18278 case AFL_EXT_4111:
18279 fputs ("NEC VR4111/VR4181", stdout);
18280 break;
18281 case AFL_EXT_4120:
18282 fputs ("NEC VR4120", stdout);
18283 break;
18284 case AFL_EXT_5400:
18285 fputs ("NEC VR5400", stdout);
18286 break;
18287 case AFL_EXT_5500:
18288 fputs ("NEC VR5500", stdout);
18289 break;
18290 case AFL_EXT_LOONGSON_2E:
18291 fputs ("ST Microelectronics Loongson 2E", stdout);
18292 break;
18293 case AFL_EXT_LOONGSON_2F:
18294 fputs ("ST Microelectronics Loongson 2F", stdout);
18295 break;
18296 case AFL_EXT_INTERAPTIV_MR2:
18297 fputs ("Imagination interAptiv MR2", stdout);
18298 break;
18299 default:
18300 fprintf (stdout, "%s (%d)", _("Unknown"), isa_ext);
18301 }
18302 }
18303
18304 static signed int
18305 get_mips_reg_size (int reg_size)
18306 {
18307 return (reg_size == AFL_REG_NONE) ? 0
18308 : (reg_size == AFL_REG_32) ? 32
18309 : (reg_size == AFL_REG_64) ? 64
18310 : (reg_size == AFL_REG_128) ? 128
18311 : -1;
18312 }
18313
18314 static bool
18315 process_mips_specific (Filedata * filedata)
18316 {
18317 Elf_Internal_Dyn * entry;
18318 Elf_Internal_Shdr *sect = NULL;
18319 size_t liblist_offset = 0;
18320 size_t liblistno = 0;
18321 size_t conflictsno = 0;
18322 size_t options_offset = 0;
18323 size_t conflicts_offset = 0;
18324 size_t pltrelsz = 0;
18325 size_t pltrel = 0;
18326 uint64_t pltgot = 0;
18327 uint64_t mips_pltgot = 0;
18328 uint64_t jmprel = 0;
18329 uint64_t local_gotno = 0;
18330 uint64_t gotsym = 0;
18331 uint64_t symtabno = 0;
18332 bool res = true;
18333
18334 if (! process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
18335 display_mips_gnu_attribute))
18336 res = false;
18337
18338 sect = find_section (filedata, ".MIPS.abiflags");
18339
18340 if (sect != NULL)
18341 {
18342 Elf_External_ABIFlags_v0 *abiflags_ext;
18343 Elf_Internal_ABIFlags_v0 abiflags_in;
18344
18345 if (sizeof (Elf_External_ABIFlags_v0) != sect->sh_size)
18346 {
18347 error (_("Corrupt MIPS ABI Flags section.\n"));
18348 res = false;
18349 }
18350 else
18351 {
18352 abiflags_ext = get_data (NULL, filedata, sect->sh_offset, 1,
18353 sect->sh_size, _("MIPS ABI Flags section"));
18354 if (abiflags_ext)
18355 {
18356 abiflags_in.version = BYTE_GET (abiflags_ext->version);
18357 abiflags_in.isa_level = BYTE_GET (abiflags_ext->isa_level);
18358 abiflags_in.isa_rev = BYTE_GET (abiflags_ext->isa_rev);
18359 abiflags_in.gpr_size = BYTE_GET (abiflags_ext->gpr_size);
18360 abiflags_in.cpr1_size = BYTE_GET (abiflags_ext->cpr1_size);
18361 abiflags_in.cpr2_size = BYTE_GET (abiflags_ext->cpr2_size);
18362 abiflags_in.fp_abi = BYTE_GET (abiflags_ext->fp_abi);
18363 abiflags_in.isa_ext = BYTE_GET (abiflags_ext->isa_ext);
18364 abiflags_in.ases = BYTE_GET (abiflags_ext->ases);
18365 abiflags_in.flags1 = BYTE_GET (abiflags_ext->flags1);
18366 abiflags_in.flags2 = BYTE_GET (abiflags_ext->flags2);
18367
18368 printf ("\nMIPS ABI Flags Version: %d\n", abiflags_in.version);
18369 printf ("\nISA: MIPS%d", abiflags_in.isa_level);
18370 if (abiflags_in.isa_rev > 1)
18371 printf ("r%d", abiflags_in.isa_rev);
18372 printf ("\nGPR size: %d",
18373 get_mips_reg_size (abiflags_in.gpr_size));
18374 printf ("\nCPR1 size: %d",
18375 get_mips_reg_size (abiflags_in.cpr1_size));
18376 printf ("\nCPR2 size: %d",
18377 get_mips_reg_size (abiflags_in.cpr2_size));
18378 fputs ("\nFP ABI: ", stdout);
18379 print_mips_fp_abi_value (abiflags_in.fp_abi);
18380 fputs ("ISA Extension: ", stdout);
18381 print_mips_isa_ext (abiflags_in.isa_ext);
18382 fputs ("\nASEs:", stdout);
18383 print_mips_ases (abiflags_in.ases);
18384 printf ("\nFLAGS 1: %8.8lx", abiflags_in.flags1);
18385 printf ("\nFLAGS 2: %8.8lx", abiflags_in.flags2);
18386 fputc ('\n', stdout);
18387 free (abiflags_ext);
18388 }
18389 }
18390 }
18391
18392 /* We have a lot of special sections. Thanks SGI! */
18393 if (filedata->dynamic_section == NULL)
18394 {
18395 /* No dynamic information available. See if there is static GOT. */
18396 sect = find_section (filedata, ".got");
18397 if (sect != NULL)
18398 {
18399 unsigned char *data_end;
18400 unsigned char *data;
18401 uint64_t ent, end;
18402 int addr_size;
18403
18404 pltgot = sect->sh_addr;
18405
18406 ent = pltgot;
18407 addr_size = (is_32bit_elf ? 4 : 8);
18408 end = pltgot + sect->sh_size;
18409
18410 data = (unsigned char *) get_data (NULL, filedata, sect->sh_offset,
18411 end - pltgot, 1,
18412 _("Global Offset Table data"));
18413 /* PR 12855: Null data is handled gracefully throughout. */
18414 data_end = data + (end - pltgot);
18415
18416 printf (_("\nStatic GOT:\n"));
18417 printf (_(" Canonical gp value: "));
18418 print_vma (ent + 0x7ff0, LONG_HEX);
18419 printf ("\n\n");
18420
18421 /* In a dynamic binary GOT[0] is reserved for the dynamic
18422 loader to store the lazy resolver pointer, however in
18423 a static binary it may well have been omitted and GOT
18424 reduced to a table of addresses.
18425 PR 21344: Check for the entry being fully available
18426 before fetching it. */
18427 if (data
18428 && data + ent - pltgot + addr_size <= data_end
18429 && byte_get (data + ent - pltgot, addr_size) == 0)
18430 {
18431 printf (_(" Reserved entries:\n"));
18432 printf (_(" %*s %10s %*s\n"),
18433 addr_size * 2, _("Address"), _("Access"),
18434 addr_size * 2, _("Value"));
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 /* Check for the MSB of GOT[1] being set, identifying a
18441 GNU object. This entry will be used by some runtime
18442 loaders, to store the module pointer. Otherwise this
18443 is an ordinary local entry.
18444 PR 21344: Check for the entry being fully available
18445 before fetching it. */
18446 if (data
18447 && data + ent - pltgot + addr_size <= data_end
18448 && (byte_get (data + ent - pltgot, addr_size)
18449 >> (addr_size * 8 - 1)) != 0)
18450 {
18451 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18452 printf ("\n");
18453 if (ent == (uint64_t) -1)
18454 goto sgot_print_fail;
18455 }
18456 printf ("\n");
18457 }
18458
18459 if (data != NULL && ent < end)
18460 {
18461 printf (_(" Local entries:\n"));
18462 printf (" %*s %10s %*s\n",
18463 addr_size * 2, _("Address"), _("Access"),
18464 addr_size * 2, _("Value"));
18465 while (ent < end)
18466 {
18467 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18468 printf ("\n");
18469 if (ent == (uint64_t) -1)
18470 goto sgot_print_fail;
18471 }
18472 printf ("\n");
18473 }
18474
18475 sgot_print_fail:
18476 free (data);
18477 }
18478 return res;
18479 }
18480
18481 for (entry = filedata->dynamic_section;
18482 /* PR 17531 file: 012-50589-0.004. */
18483 (entry < filedata->dynamic_section + filedata->dynamic_nent
18484 && entry->d_tag != DT_NULL);
18485 ++entry)
18486 switch (entry->d_tag)
18487 {
18488 case DT_MIPS_LIBLIST:
18489 liblist_offset
18490 = offset_from_vma (filedata, entry->d_un.d_val,
18491 liblistno * sizeof (Elf32_External_Lib));
18492 break;
18493 case DT_MIPS_LIBLISTNO:
18494 liblistno = entry->d_un.d_val;
18495 break;
18496 case DT_MIPS_OPTIONS:
18497 options_offset = offset_from_vma (filedata, entry->d_un.d_val, 0);
18498 break;
18499 case DT_MIPS_CONFLICT:
18500 conflicts_offset
18501 = offset_from_vma (filedata, entry->d_un.d_val,
18502 conflictsno * sizeof (Elf32_External_Conflict));
18503 break;
18504 case DT_MIPS_CONFLICTNO:
18505 conflictsno = entry->d_un.d_val;
18506 break;
18507 case DT_PLTGOT:
18508 pltgot = entry->d_un.d_ptr;
18509 break;
18510 case DT_MIPS_LOCAL_GOTNO:
18511 local_gotno = entry->d_un.d_val;
18512 break;
18513 case DT_MIPS_GOTSYM:
18514 gotsym = entry->d_un.d_val;
18515 break;
18516 case DT_MIPS_SYMTABNO:
18517 symtabno = entry->d_un.d_val;
18518 break;
18519 case DT_MIPS_PLTGOT:
18520 mips_pltgot = entry->d_un.d_ptr;
18521 break;
18522 case DT_PLTREL:
18523 pltrel = entry->d_un.d_val;
18524 break;
18525 case DT_PLTRELSZ:
18526 pltrelsz = entry->d_un.d_val;
18527 break;
18528 case DT_JMPREL:
18529 jmprel = entry->d_un.d_ptr;
18530 break;
18531 default:
18532 break;
18533 }
18534
18535 if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
18536 {
18537 Elf32_External_Lib * elib;
18538 size_t cnt;
18539
18540 elib = (Elf32_External_Lib *) get_data (NULL, filedata, liblist_offset,
18541 sizeof (Elf32_External_Lib),
18542 liblistno,
18543 _("liblist section data"));
18544 if (elib)
18545 {
18546 printf (ngettext ("\nSection '.liblist' contains %lu entry:\n",
18547 "\nSection '.liblist' contains %lu entries:\n",
18548 (unsigned long) liblistno),
18549 (unsigned long) liblistno);
18550 fputs (_(" Library Time Stamp Checksum Version Flags\n"),
18551 stdout);
18552
18553 for (cnt = 0; cnt < liblistno; ++cnt)
18554 {
18555 Elf32_Lib liblist;
18556 time_t atime;
18557 char timebuf[128];
18558 struct tm * tmp;
18559
18560 liblist.l_name = BYTE_GET (elib[cnt].l_name);
18561 atime = BYTE_GET (elib[cnt].l_time_stamp);
18562 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
18563 liblist.l_version = BYTE_GET (elib[cnt].l_version);
18564 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
18565
18566 tmp = gmtime (&atime);
18567 snprintf (timebuf, sizeof (timebuf),
18568 "%04u-%02u-%02uT%02u:%02u:%02u",
18569 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
18570 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
18571
18572 printf ("%3lu: ", (unsigned long) cnt);
18573 if (valid_dynamic_name (filedata, liblist.l_name))
18574 print_symbol (20, get_dynamic_name (filedata, liblist.l_name));
18575 else
18576 printf (_("<corrupt: %9ld>"), liblist.l_name);
18577 printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
18578 liblist.l_version);
18579
18580 if (liblist.l_flags == 0)
18581 puts (_(" NONE"));
18582 else
18583 {
18584 static const struct
18585 {
18586 const char * name;
18587 int bit;
18588 }
18589 l_flags_vals[] =
18590 {
18591 { " EXACT_MATCH", LL_EXACT_MATCH },
18592 { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
18593 { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
18594 { " EXPORTS", LL_EXPORTS },
18595 { " DELAY_LOAD", LL_DELAY_LOAD },
18596 { " DELTA", LL_DELTA }
18597 };
18598 int flags = liblist.l_flags;
18599 size_t fcnt;
18600
18601 for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
18602 if ((flags & l_flags_vals[fcnt].bit) != 0)
18603 {
18604 fputs (l_flags_vals[fcnt].name, stdout);
18605 flags ^= l_flags_vals[fcnt].bit;
18606 }
18607 if (flags != 0)
18608 printf (" %#x", (unsigned int) flags);
18609
18610 puts ("");
18611 }
18612 }
18613
18614 free (elib);
18615 }
18616 else
18617 res = false;
18618 }
18619
18620 if (options_offset != 0)
18621 {
18622 Elf_External_Options * eopt;
18623 size_t offset;
18624 int cnt;
18625
18626 /* Find the section header so that we get the size. */
18627 sect = find_section_by_type (filedata, SHT_MIPS_OPTIONS);
18628 /* PR 17533 file: 012-277276-0.004. */
18629 if (sect == NULL)
18630 {
18631 error (_("No MIPS_OPTIONS header found\n"));
18632 return false;
18633 }
18634 /* PR 24243 */
18635 if (sect->sh_size < sizeof (* eopt))
18636 {
18637 error (_("The MIPS options section is too small.\n"));
18638 return false;
18639 }
18640
18641 eopt = (Elf_External_Options *) get_data (NULL, filedata, options_offset, 1,
18642 sect->sh_size, _("options"));
18643 if (eopt)
18644 {
18645 Elf_Internal_Options option;
18646
18647 offset = cnt = 0;
18648 while (offset <= sect->sh_size - sizeof (* eopt))
18649 {
18650 Elf_External_Options * eoption;
18651 unsigned int optsize;
18652
18653 eoption = (Elf_External_Options *) ((char *) eopt + offset);
18654
18655 optsize = BYTE_GET (eoption->size);
18656
18657 /* PR 17531: file: ffa0fa3b. */
18658 if (optsize < sizeof (* eopt)
18659 || optsize > sect->sh_size - offset)
18660 {
18661 error (_("Invalid size (%u) for MIPS option\n"),
18662 optsize);
18663 free (eopt);
18664 return false;
18665 }
18666 offset += optsize;
18667 ++cnt;
18668 }
18669
18670 printf (ngettext ("\nSection '%s' contains %d entry:\n",
18671 "\nSection '%s' contains %d entries:\n",
18672 cnt),
18673 printable_section_name (filedata, sect), cnt);
18674
18675 offset = 0;
18676 while (cnt-- > 0)
18677 {
18678 size_t len;
18679 Elf_External_Options * eoption;
18680
18681 eoption = (Elf_External_Options *) ((char *) eopt + offset);
18682
18683 option.kind = BYTE_GET (eoption->kind);
18684 option.size = BYTE_GET (eoption->size);
18685 option.section = BYTE_GET (eoption->section);
18686 option.info = BYTE_GET (eoption->info);
18687
18688 switch (option.kind)
18689 {
18690 case ODK_NULL:
18691 /* This shouldn't happen. */
18692 printf (" NULL %" PRId16 " %" PRIx32,
18693 option.section, option.info);
18694 break;
18695
18696 case ODK_REGINFO:
18697 printf (" REGINFO ");
18698 if (filedata->file_header.e_machine == EM_MIPS)
18699 {
18700 Elf32_External_RegInfo * ereg;
18701 Elf32_RegInfo reginfo;
18702
18703 /* 32bit form. */
18704 if (option.size < (sizeof (Elf_External_Options)
18705 + sizeof (Elf32_External_RegInfo)))
18706 {
18707 printf (_("<corrupt>\n"));
18708 error (_("Truncated MIPS REGINFO option\n"));
18709 cnt = 0;
18710 break;
18711 }
18712
18713 ereg = (Elf32_External_RegInfo *) (eoption + 1);
18714
18715 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
18716 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
18717 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
18718 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
18719 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
18720 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
18721
18722 printf ("GPR %08" PRIx32 " GP 0x%" PRIx32 "\n",
18723 reginfo.ri_gprmask, reginfo.ri_gp_value);
18724 printf (" "
18725 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
18726 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
18727 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
18728 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
18729 }
18730 else
18731 {
18732 /* 64 bit form. */
18733 Elf64_External_RegInfo * ereg;
18734 Elf64_Internal_RegInfo reginfo;
18735
18736 if (option.size < (sizeof (Elf_External_Options)
18737 + sizeof (Elf64_External_RegInfo)))
18738 {
18739 printf (_("<corrupt>\n"));
18740 error (_("Truncated MIPS REGINFO option\n"));
18741 cnt = 0;
18742 break;
18743 }
18744
18745 ereg = (Elf64_External_RegInfo *) (eoption + 1);
18746 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
18747 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
18748 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
18749 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
18750 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
18751 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
18752
18753 printf ("GPR %08" PRIx32 " GP 0x%" PRIx64 "\n",
18754 reginfo.ri_gprmask, reginfo.ri_gp_value);
18755 printf (" "
18756 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
18757 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
18758 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
18759 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
18760 }
18761 offset += option.size;
18762 continue;
18763
18764 case ODK_EXCEPTIONS:
18765 fputs (" EXCEPTIONS fpe_min(", stdout);
18766 process_mips_fpe_exception (option.info & OEX_FPU_MIN);
18767 fputs (") fpe_max(", stdout);
18768 process_mips_fpe_exception ((option.info & OEX_FPU_MAX) >> 8);
18769 fputs (")", stdout);
18770
18771 if (option.info & OEX_PAGE0)
18772 fputs (" PAGE0", stdout);
18773 if (option.info & OEX_SMM)
18774 fputs (" SMM", stdout);
18775 if (option.info & OEX_FPDBUG)
18776 fputs (" FPDBUG", stdout);
18777 if (option.info & OEX_DISMISS)
18778 fputs (" DISMISS", stdout);
18779 break;
18780
18781 case ODK_PAD:
18782 fputs (" PAD ", stdout);
18783 if (option.info & OPAD_PREFIX)
18784 fputs (" PREFIX", stdout);
18785 if (option.info & OPAD_POSTFIX)
18786 fputs (" POSTFIX", stdout);
18787 if (option.info & OPAD_SYMBOL)
18788 fputs (" SYMBOL", stdout);
18789 break;
18790
18791 case ODK_HWPATCH:
18792 fputs (" HWPATCH ", stdout);
18793 if (option.info & OHW_R4KEOP)
18794 fputs (" R4KEOP", stdout);
18795 if (option.info & OHW_R8KPFETCH)
18796 fputs (" R8KPFETCH", stdout);
18797 if (option.info & OHW_R5KEOP)
18798 fputs (" R5KEOP", stdout);
18799 if (option.info & OHW_R5KCVTL)
18800 fputs (" R5KCVTL", stdout);
18801 break;
18802
18803 case ODK_FILL:
18804 fputs (" FILL ", stdout);
18805 /* XXX Print content of info word? */
18806 break;
18807
18808 case ODK_TAGS:
18809 fputs (" TAGS ", stdout);
18810 /* XXX Print content of info word? */
18811 break;
18812
18813 case ODK_HWAND:
18814 fputs (" HWAND ", stdout);
18815 if (option.info & OHWA0_R4KEOP_CHECKED)
18816 fputs (" R4KEOP_CHECKED", stdout);
18817 if (option.info & OHWA0_R4KEOP_CLEAN)
18818 fputs (" R4KEOP_CLEAN", stdout);
18819 break;
18820
18821 case ODK_HWOR:
18822 fputs (" HWOR ", stdout);
18823 if (option.info & OHWA0_R4KEOP_CHECKED)
18824 fputs (" R4KEOP_CHECKED", stdout);
18825 if (option.info & OHWA0_R4KEOP_CLEAN)
18826 fputs (" R4KEOP_CLEAN", stdout);
18827 break;
18828
18829 case ODK_GP_GROUP:
18830 printf (" GP_GROUP %#06x self-contained %#06x",
18831 option.info & OGP_GROUP,
18832 (option.info & OGP_SELF) >> 16);
18833 break;
18834
18835 case ODK_IDENT:
18836 printf (" IDENT %#06x self-contained %#06x",
18837 option.info & OGP_GROUP,
18838 (option.info & OGP_SELF) >> 16);
18839 break;
18840
18841 default:
18842 /* This shouldn't happen. */
18843 printf (" %3d ??? %" PRId16 " %" PRIx32,
18844 option.kind, option.section, option.info);
18845 break;
18846 }
18847
18848 len = sizeof (* eopt);
18849 while (len < option.size)
18850 {
18851 unsigned char datum = *((unsigned char *) eoption + len);
18852
18853 if (ISPRINT (datum))
18854 printf ("%c", datum);
18855 else
18856 printf ("\\%03o", datum);
18857 len ++;
18858 }
18859 fputs ("\n", stdout);
18860
18861 offset += option.size;
18862 }
18863 free (eopt);
18864 }
18865 else
18866 res = false;
18867 }
18868
18869 if (conflicts_offset != 0 && conflictsno != 0)
18870 {
18871 Elf32_Conflict * iconf;
18872 size_t cnt;
18873
18874 if (filedata->dynamic_symbols == NULL)
18875 {
18876 error (_("conflict list found without a dynamic symbol table\n"));
18877 return false;
18878 }
18879
18880 /* PR 21345 - print a slightly more helpful error message
18881 if we are sure that the cmalloc will fail. */
18882 if (conflictsno > filedata->file_size / sizeof (* iconf))
18883 {
18884 error (_("Overlarge number of conflicts detected: %lx\n"),
18885 (long) conflictsno);
18886 return false;
18887 }
18888
18889 iconf = (Elf32_Conflict *) cmalloc (conflictsno, sizeof (* iconf));
18890 if (iconf == NULL)
18891 {
18892 error (_("Out of memory allocating space for dynamic conflicts\n"));
18893 return false;
18894 }
18895
18896 if (is_32bit_elf)
18897 {
18898 Elf32_External_Conflict * econf32;
18899
18900 econf32 = (Elf32_External_Conflict *)
18901 get_data (NULL, filedata, conflicts_offset,
18902 sizeof (*econf32), conflictsno, _("conflict"));
18903 if (!econf32)
18904 {
18905 free (iconf);
18906 return false;
18907 }
18908
18909 for (cnt = 0; cnt < conflictsno; ++cnt)
18910 iconf[cnt] = BYTE_GET (econf32[cnt]);
18911
18912 free (econf32);
18913 }
18914 else
18915 {
18916 Elf64_External_Conflict * econf64;
18917
18918 econf64 = (Elf64_External_Conflict *)
18919 get_data (NULL, filedata, conflicts_offset,
18920 sizeof (*econf64), conflictsno, _("conflict"));
18921 if (!econf64)
18922 {
18923 free (iconf);
18924 return false;
18925 }
18926
18927 for (cnt = 0; cnt < conflictsno; ++cnt)
18928 iconf[cnt] = BYTE_GET (econf64[cnt]);
18929
18930 free (econf64);
18931 }
18932
18933 printf (ngettext ("\nSection '.conflict' contains %lu entry:\n",
18934 "\nSection '.conflict' contains %lu entries:\n",
18935 (unsigned long) conflictsno),
18936 (unsigned long) conflictsno);
18937 puts (_(" Num: Index Value Name"));
18938
18939 for (cnt = 0; cnt < conflictsno; ++cnt)
18940 {
18941 printf ("%5lu: %8lu ", (unsigned long) cnt, iconf[cnt]);
18942
18943 if (iconf[cnt] >= filedata->num_dynamic_syms)
18944 printf (_("<corrupt symbol index>"));
18945 else
18946 {
18947 Elf_Internal_Sym * psym;
18948
18949 psym = & filedata->dynamic_symbols[iconf[cnt]];
18950 print_vma (psym->st_value, FULL_HEX);
18951 putchar (' ');
18952 if (valid_dynamic_name (filedata, psym->st_name))
18953 print_symbol (25, get_dynamic_name (filedata, psym->st_name));
18954 else
18955 printf (_("<corrupt: %14ld>"), psym->st_name);
18956 }
18957 putchar ('\n');
18958 }
18959
18960 free (iconf);
18961 }
18962
18963 if (pltgot != 0 && local_gotno != 0)
18964 {
18965 uint64_t ent, local_end, global_end;
18966 size_t i, offset;
18967 unsigned char * data;
18968 unsigned char * data_end;
18969 int addr_size;
18970
18971 ent = pltgot;
18972 addr_size = (is_32bit_elf ? 4 : 8);
18973 local_end = pltgot + local_gotno * addr_size;
18974
18975 /* PR binutils/17533 file: 012-111227-0.004 */
18976 if (symtabno < gotsym)
18977 {
18978 error (_("The GOT symbol offset (%lu) is greater than the symbol table size (%lu)\n"),
18979 (unsigned long) gotsym, (unsigned long) symtabno);
18980 return false;
18981 }
18982
18983 global_end = local_end + (symtabno - gotsym) * addr_size;
18984 /* PR 17531: file: 54c91a34. */
18985 if (global_end < local_end)
18986 {
18987 error (_("Too many GOT symbols: %lu\n"), (unsigned long) symtabno);
18988 return false;
18989 }
18990
18991 offset = offset_from_vma (filedata, pltgot, global_end - pltgot);
18992 data = (unsigned char *) get_data (NULL, filedata, offset,
18993 global_end - pltgot, 1,
18994 _("Global Offset Table data"));
18995 /* PR 12855: Null data is handled gracefully throughout. */
18996 data_end = data + (global_end - pltgot);
18997
18998 printf (_("\nPrimary GOT:\n"));
18999 printf (_(" Canonical gp value: "));
19000 print_vma (pltgot + 0x7ff0, LONG_HEX);
19001 printf ("\n\n");
19002
19003 printf (_(" Reserved entries:\n"));
19004 printf (_(" %*s %10s %*s Purpose\n"),
19005 addr_size * 2, _("Address"), _("Access"),
19006 addr_size * 2, _("Initial"));
19007 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19008 printf (_(" Lazy resolver\n"));
19009 if (ent == (uint64_t) -1)
19010 goto got_print_fail;
19011
19012 /* Check for the MSB of GOT[1] being set, denoting a GNU object.
19013 This entry will be used by some runtime loaders, to store the
19014 module pointer. Otherwise this is an ordinary local entry.
19015 PR 21344: Check for the entry being fully available before
19016 fetching it. */
19017 if (data
19018 && data + ent - pltgot + addr_size <= data_end
19019 && (byte_get (data + ent - pltgot, addr_size)
19020 >> (addr_size * 8 - 1)) != 0)
19021 {
19022 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19023 printf (_(" Module pointer (GNU extension)\n"));
19024 if (ent == (uint64_t) -1)
19025 goto got_print_fail;
19026 }
19027 printf ("\n");
19028
19029 if (data != NULL && ent < local_end)
19030 {
19031 printf (_(" Local entries:\n"));
19032 printf (" %*s %10s %*s\n",
19033 addr_size * 2, _("Address"), _("Access"),
19034 addr_size * 2, _("Initial"));
19035 while (ent < local_end)
19036 {
19037 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19038 printf ("\n");
19039 if (ent == (uint64_t) -1)
19040 goto got_print_fail;
19041 }
19042 printf ("\n");
19043 }
19044
19045 if (data != NULL && gotsym < symtabno)
19046 {
19047 int sym_width;
19048
19049 printf (_(" Global entries:\n"));
19050 printf (" %*s %10s %*s %*s %-7s %3s %s\n",
19051 addr_size * 2, _("Address"),
19052 _("Access"),
19053 addr_size * 2, _("Initial"),
19054 addr_size * 2, _("Sym.Val."),
19055 _("Type"),
19056 /* Note for translators: "Ndx" = abbreviated form of "Index". */
19057 _("Ndx"), _("Name"));
19058
19059 sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
19060
19061 for (i = gotsym; i < symtabno; i++)
19062 {
19063 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19064 printf (" ");
19065
19066 if (filedata->dynamic_symbols == NULL)
19067 printf (_("<no dynamic symbols>"));
19068 else if (i < filedata->num_dynamic_syms)
19069 {
19070 Elf_Internal_Sym * psym = filedata->dynamic_symbols + i;
19071
19072 print_vma (psym->st_value, LONG_HEX);
19073 printf (" %-7s %3s ",
19074 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
19075 get_symbol_index_type (filedata, psym->st_shndx));
19076
19077 if (valid_dynamic_name (filedata, psym->st_name))
19078 print_symbol (sym_width,
19079 get_dynamic_name (filedata, psym->st_name));
19080 else
19081 printf (_("<corrupt: %14ld>"), psym->st_name);
19082 }
19083 else
19084 printf (_("<symbol index %lu exceeds number of dynamic symbols>"),
19085 (unsigned long) i);
19086
19087 printf ("\n");
19088 if (ent == (uint64_t) -1)
19089 break;
19090 }
19091 printf ("\n");
19092 }
19093
19094 got_print_fail:
19095 free (data);
19096 }
19097
19098 if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
19099 {
19100 uint64_t ent, end;
19101 size_t offset, rel_offset;
19102 unsigned long count, i;
19103 unsigned char * data;
19104 int addr_size, sym_width;
19105 Elf_Internal_Rela * rels;
19106
19107 rel_offset = offset_from_vma (filedata, jmprel, pltrelsz);
19108 if (pltrel == DT_RELA)
19109 {
19110 if (!slurp_rela_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
19111 return false;
19112 }
19113 else
19114 {
19115 if (!slurp_rel_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
19116 return false;
19117 }
19118
19119 ent = mips_pltgot;
19120 addr_size = (is_32bit_elf ? 4 : 8);
19121 end = mips_pltgot + (2 + count) * addr_size;
19122
19123 offset = offset_from_vma (filedata, mips_pltgot, end - mips_pltgot);
19124 data = (unsigned char *) get_data (NULL, filedata, offset, end - mips_pltgot,
19125 1, _("Procedure Linkage Table data"));
19126 if (data == NULL)
19127 {
19128 free (rels);
19129 return false;
19130 }
19131
19132 printf ("\nPLT GOT:\n\n");
19133 printf (_(" Reserved entries:\n"));
19134 printf (_(" %*s %*s Purpose\n"),
19135 addr_size * 2, _("Address"), addr_size * 2, _("Initial"));
19136 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
19137 printf (_(" PLT lazy resolver\n"));
19138 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
19139 printf (_(" Module pointer\n"));
19140 printf ("\n");
19141
19142 printf (_(" Entries:\n"));
19143 printf (" %*s %*s %*s %-7s %3s %s\n",
19144 addr_size * 2, _("Address"),
19145 addr_size * 2, _("Initial"),
19146 addr_size * 2, _("Sym.Val."), _("Type"), _("Ndx"), _("Name"));
19147 sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
19148 for (i = 0; i < count; i++)
19149 {
19150 unsigned long idx = get_reloc_symindex (rels[i].r_info);
19151
19152 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
19153 printf (" ");
19154
19155 if (idx >= filedata->num_dynamic_syms)
19156 printf (_("<corrupt symbol index: %lu>"), idx);
19157 else
19158 {
19159 Elf_Internal_Sym * psym = filedata->dynamic_symbols + idx;
19160
19161 print_vma (psym->st_value, LONG_HEX);
19162 printf (" %-7s %3s ",
19163 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
19164 get_symbol_index_type (filedata, psym->st_shndx));
19165 if (valid_dynamic_name (filedata, psym->st_name))
19166 print_symbol (sym_width,
19167 get_dynamic_name (filedata, psym->st_name));
19168 else
19169 printf (_("<corrupt: %14ld>"), psym->st_name);
19170 }
19171 printf ("\n");
19172 }
19173 printf ("\n");
19174
19175 free (data);
19176 free (rels);
19177 }
19178
19179 return res;
19180 }
19181
19182 static bool
19183 process_nds32_specific (Filedata * filedata)
19184 {
19185 Elf_Internal_Shdr *sect = NULL;
19186
19187 sect = find_section (filedata, ".nds32_e_flags");
19188 if (sect != NULL && sect->sh_size >= 4)
19189 {
19190 unsigned char *buf;
19191 unsigned int flag;
19192
19193 printf ("\nNDS32 elf flags section:\n");
19194 buf = get_data (NULL, filedata, sect->sh_offset, 1, 4,
19195 _("NDS32 elf flags section"));
19196
19197 if (buf == NULL)
19198 return false;
19199
19200 flag = byte_get (buf, 4);
19201 free (buf);
19202 switch (flag & 0x3)
19203 {
19204 case 0:
19205 printf ("(VEC_SIZE):\tNo entry.\n");
19206 break;
19207 case 1:
19208 printf ("(VEC_SIZE):\t4 bytes\n");
19209 break;
19210 case 2:
19211 printf ("(VEC_SIZE):\t16 bytes\n");
19212 break;
19213 case 3:
19214 printf ("(VEC_SIZE):\treserved\n");
19215 break;
19216 }
19217 }
19218
19219 return true;
19220 }
19221
19222 static bool
19223 process_gnu_liblist (Filedata * filedata)
19224 {
19225 Elf_Internal_Shdr * section;
19226 Elf_Internal_Shdr * string_sec;
19227 Elf32_External_Lib * elib;
19228 char * strtab;
19229 size_t strtab_size;
19230 size_t cnt;
19231 unsigned long num_liblist;
19232 unsigned i;
19233 bool res = true;
19234
19235 if (! do_arch)
19236 return true;
19237
19238 for (i = 0, section = filedata->section_headers;
19239 i < filedata->file_header.e_shnum;
19240 i++, section++)
19241 {
19242 switch (section->sh_type)
19243 {
19244 case SHT_GNU_LIBLIST:
19245 if (section->sh_link >= filedata->file_header.e_shnum)
19246 break;
19247
19248 elib = (Elf32_External_Lib *)
19249 get_data (NULL, filedata, section->sh_offset, 1, section->sh_size,
19250 _("liblist section data"));
19251
19252 if (elib == NULL)
19253 {
19254 res = false;
19255 break;
19256 }
19257
19258 string_sec = filedata->section_headers + section->sh_link;
19259 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
19260 string_sec->sh_size,
19261 _("liblist string table"));
19262 if (strtab == NULL
19263 || section->sh_entsize != sizeof (Elf32_External_Lib))
19264 {
19265 free (elib);
19266 free (strtab);
19267 res = false;
19268 break;
19269 }
19270 strtab_size = string_sec->sh_size;
19271
19272 num_liblist = section->sh_size / sizeof (Elf32_External_Lib);
19273 printf (ngettext ("\nLibrary list section '%s' contains %lu entries:\n",
19274 "\nLibrary list section '%s' contains %lu entries:\n",
19275 num_liblist),
19276 printable_section_name (filedata, section),
19277 num_liblist);
19278
19279 puts (_(" Library Time Stamp Checksum Version Flags"));
19280
19281 for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
19282 ++cnt)
19283 {
19284 Elf32_Lib liblist;
19285 time_t atime;
19286 char timebuf[128];
19287 struct tm * tmp;
19288
19289 liblist.l_name = BYTE_GET (elib[cnt].l_name);
19290 atime = BYTE_GET (elib[cnt].l_time_stamp);
19291 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
19292 liblist.l_version = BYTE_GET (elib[cnt].l_version);
19293 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
19294
19295 tmp = gmtime (&atime);
19296 snprintf (timebuf, sizeof (timebuf),
19297 "%04u-%02u-%02uT%02u:%02u:%02u",
19298 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
19299 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
19300
19301 printf ("%3lu: ", (unsigned long) cnt);
19302 if (do_wide)
19303 printf ("%-20s", liblist.l_name < strtab_size
19304 ? strtab + liblist.l_name : _("<corrupt>"));
19305 else
19306 printf ("%-20.20s", liblist.l_name < strtab_size
19307 ? strtab + liblist.l_name : _("<corrupt>"));
19308 printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
19309 liblist.l_version, liblist.l_flags);
19310 }
19311
19312 free (elib);
19313 free (strtab);
19314 }
19315 }
19316
19317 return res;
19318 }
19319
19320 static const char *
19321 get_note_type (Filedata * filedata, unsigned e_type)
19322 {
19323 static char buff[64];
19324
19325 if (filedata->file_header.e_type == ET_CORE)
19326 switch (e_type)
19327 {
19328 case NT_AUXV:
19329 return _("NT_AUXV (auxiliary vector)");
19330 case NT_PRSTATUS:
19331 return _("NT_PRSTATUS (prstatus structure)");
19332 case NT_FPREGSET:
19333 return _("NT_FPREGSET (floating point registers)");
19334 case NT_PRPSINFO:
19335 return _("NT_PRPSINFO (prpsinfo structure)");
19336 case NT_TASKSTRUCT:
19337 return _("NT_TASKSTRUCT (task structure)");
19338 case NT_GDB_TDESC:
19339 return _("NT_GDB_TDESC (GDB XML target description)");
19340 case NT_PRXFPREG:
19341 return _("NT_PRXFPREG (user_xfpregs structure)");
19342 case NT_PPC_VMX:
19343 return _("NT_PPC_VMX (ppc Altivec registers)");
19344 case NT_PPC_VSX:
19345 return _("NT_PPC_VSX (ppc VSX registers)");
19346 case NT_PPC_TAR:
19347 return _("NT_PPC_TAR (ppc TAR register)");
19348 case NT_PPC_PPR:
19349 return _("NT_PPC_PPR (ppc PPR register)");
19350 case NT_PPC_DSCR:
19351 return _("NT_PPC_DSCR (ppc DSCR register)");
19352 case NT_PPC_EBB:
19353 return _("NT_PPC_EBB (ppc EBB registers)");
19354 case NT_PPC_PMU:
19355 return _("NT_PPC_PMU (ppc PMU registers)");
19356 case NT_PPC_TM_CGPR:
19357 return _("NT_PPC_TM_CGPR (ppc checkpointed GPR registers)");
19358 case NT_PPC_TM_CFPR:
19359 return _("NT_PPC_TM_CFPR (ppc checkpointed floating point registers)");
19360 case NT_PPC_TM_CVMX:
19361 return _("NT_PPC_TM_CVMX (ppc checkpointed Altivec registers)");
19362 case NT_PPC_TM_CVSX:
19363 return _("NT_PPC_TM_CVSX (ppc checkpointed VSX registers)");
19364 case NT_PPC_TM_SPR:
19365 return _("NT_PPC_TM_SPR (ppc TM special purpose registers)");
19366 case NT_PPC_TM_CTAR:
19367 return _("NT_PPC_TM_CTAR (ppc checkpointed TAR register)");
19368 case NT_PPC_TM_CPPR:
19369 return _("NT_PPC_TM_CPPR (ppc checkpointed PPR register)");
19370 case NT_PPC_TM_CDSCR:
19371 return _("NT_PPC_TM_CDSCR (ppc checkpointed DSCR register)");
19372 case NT_386_TLS:
19373 return _("NT_386_TLS (x86 TLS information)");
19374 case NT_386_IOPERM:
19375 return _("NT_386_IOPERM (x86 I/O permissions)");
19376 case NT_X86_XSTATE:
19377 return _("NT_X86_XSTATE (x86 XSAVE extended state)");
19378 case NT_X86_CET:
19379 return _("NT_X86_CET (x86 CET state)");
19380 case NT_S390_HIGH_GPRS:
19381 return _("NT_S390_HIGH_GPRS (s390 upper register halves)");
19382 case NT_S390_TIMER:
19383 return _("NT_S390_TIMER (s390 timer register)");
19384 case NT_S390_TODCMP:
19385 return _("NT_S390_TODCMP (s390 TOD comparator register)");
19386 case NT_S390_TODPREG:
19387 return _("NT_S390_TODPREG (s390 TOD programmable register)");
19388 case NT_S390_CTRS:
19389 return _("NT_S390_CTRS (s390 control registers)");
19390 case NT_S390_PREFIX:
19391 return _("NT_S390_PREFIX (s390 prefix register)");
19392 case NT_S390_LAST_BREAK:
19393 return _("NT_S390_LAST_BREAK (s390 last breaking event address)");
19394 case NT_S390_SYSTEM_CALL:
19395 return _("NT_S390_SYSTEM_CALL (s390 system call restart data)");
19396 case NT_S390_TDB:
19397 return _("NT_S390_TDB (s390 transaction diagnostic block)");
19398 case NT_S390_VXRS_LOW:
19399 return _("NT_S390_VXRS_LOW (s390 vector registers 0-15 upper half)");
19400 case NT_S390_VXRS_HIGH:
19401 return _("NT_S390_VXRS_HIGH (s390 vector registers 16-31)");
19402 case NT_S390_GS_CB:
19403 return _("NT_S390_GS_CB (s390 guarded-storage registers)");
19404 case NT_S390_GS_BC:
19405 return _("NT_S390_GS_BC (s390 guarded-storage broadcast control)");
19406 case NT_ARM_VFP:
19407 return _("NT_ARM_VFP (arm VFP registers)");
19408 case NT_ARM_TLS:
19409 return _("NT_ARM_TLS (AArch TLS registers)");
19410 case NT_ARM_HW_BREAK:
19411 return _("NT_ARM_HW_BREAK (AArch hardware breakpoint registers)");
19412 case NT_ARM_HW_WATCH:
19413 return _("NT_ARM_HW_WATCH (AArch hardware watchpoint registers)");
19414 case NT_ARM_SYSTEM_CALL:
19415 return _("NT_ARM_SYSTEM_CALL (AArch system call number)");
19416 case NT_ARM_SVE:
19417 return _("NT_ARM_SVE (AArch SVE registers)");
19418 case NT_ARM_PAC_MASK:
19419 return _("NT_ARM_PAC_MASK (AArch pointer authentication code masks)");
19420 case NT_ARM_PACA_KEYS:
19421 return _("NT_ARM_PACA_KEYS (ARM pointer authentication address keys)");
19422 case NT_ARM_PACG_KEYS:
19423 return _("NT_ARM_PACG_KEYS (ARM pointer authentication generic keys)");
19424 case NT_ARM_TAGGED_ADDR_CTRL:
19425 return _("NT_ARM_TAGGED_ADDR_CTRL (AArch tagged address control)");
19426 case NT_ARM_PAC_ENABLED_KEYS:
19427 return _("NT_ARM_PAC_ENABLED_KEYS (AArch64 pointer authentication enabled keys)");
19428 case NT_ARC_V2:
19429 return _("NT_ARC_V2 (ARC HS accumulator/extra registers)");
19430 case NT_RISCV_CSR:
19431 return _("NT_RISCV_CSR (RISC-V control and status registers)");
19432 case NT_PSTATUS:
19433 return _("NT_PSTATUS (pstatus structure)");
19434 case NT_FPREGS:
19435 return _("NT_FPREGS (floating point registers)");
19436 case NT_PSINFO:
19437 return _("NT_PSINFO (psinfo structure)");
19438 case NT_LWPSTATUS:
19439 return _("NT_LWPSTATUS (lwpstatus_t structure)");
19440 case NT_LWPSINFO:
19441 return _("NT_LWPSINFO (lwpsinfo_t structure)");
19442 case NT_WIN32PSTATUS:
19443 return _("NT_WIN32PSTATUS (win32_pstatus structure)");
19444 case NT_SIGINFO:
19445 return _("NT_SIGINFO (siginfo_t data)");
19446 case NT_FILE:
19447 return _("NT_FILE (mapped files)");
19448 default:
19449 break;
19450 }
19451 else
19452 switch (e_type)
19453 {
19454 case NT_VERSION:
19455 return _("NT_VERSION (version)");
19456 case NT_ARCH:
19457 return _("NT_ARCH (architecture)");
19458 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
19459 return _("OPEN");
19460 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
19461 return _("func");
19462 case NT_GO_BUILDID:
19463 return _("GO BUILDID");
19464 case FDO_PACKAGING_METADATA:
19465 return _("FDO_PACKAGING_METADATA");
19466 default:
19467 break;
19468 }
19469
19470 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19471 return buff;
19472 }
19473
19474 static bool
19475 print_core_note (Elf_Internal_Note *pnote)
19476 {
19477 unsigned int addr_size = is_32bit_elf ? 4 : 8;
19478 uint64_t count, page_size;
19479 unsigned char *descdata, *filenames, *descend;
19480
19481 if (pnote->type != NT_FILE)
19482 {
19483 if (do_wide)
19484 printf ("\n");
19485 return true;
19486 }
19487
19488 if (!is_32bit_elf)
19489 {
19490 printf (_(" Cannot decode 64-bit note in 32-bit build\n"));
19491 /* Still "successful". */
19492 return true;
19493 }
19494
19495 if (pnote->descsz < 2 * addr_size)
19496 {
19497 error (_(" Malformed note - too short for header\n"));
19498 return false;
19499 }
19500
19501 descdata = (unsigned char *) pnote->descdata;
19502 descend = descdata + pnote->descsz;
19503
19504 if (descdata[pnote->descsz - 1] != '\0')
19505 {
19506 error (_(" Malformed note - does not end with \\0\n"));
19507 return false;
19508 }
19509
19510 count = byte_get (descdata, addr_size);
19511 descdata += addr_size;
19512
19513 page_size = byte_get (descdata, addr_size);
19514 descdata += addr_size;
19515
19516 if (count > ((uint64_t) -1 - 2 * addr_size) / (3 * addr_size)
19517 || pnote->descsz < 2 * addr_size + count * 3 * addr_size)
19518 {
19519 error (_(" Malformed note - too short for supplied file count\n"));
19520 return false;
19521 }
19522
19523 printf (_(" Page size: "));
19524 print_vma (page_size, DEC);
19525 printf ("\n");
19526
19527 printf (_(" %*s%*s%*s\n"),
19528 (int) (2 + 2 * addr_size), _("Start"),
19529 (int) (4 + 2 * addr_size), _("End"),
19530 (int) (4 + 2 * addr_size), _("Page Offset"));
19531 filenames = descdata + count * 3 * addr_size;
19532 while (count-- > 0)
19533 {
19534 uint64_t start, end, file_ofs;
19535
19536 if (filenames == descend)
19537 {
19538 error (_(" Malformed note - filenames end too early\n"));
19539 return false;
19540 }
19541
19542 start = byte_get (descdata, addr_size);
19543 descdata += addr_size;
19544 end = byte_get (descdata, addr_size);
19545 descdata += addr_size;
19546 file_ofs = byte_get (descdata, addr_size);
19547 descdata += addr_size;
19548
19549 printf (" ");
19550 print_vma (start, FULL_HEX);
19551 printf (" ");
19552 print_vma (end, FULL_HEX);
19553 printf (" ");
19554 print_vma (file_ofs, FULL_HEX);
19555 printf ("\n %s\n", filenames);
19556
19557 filenames += 1 + strlen ((char *) filenames);
19558 }
19559
19560 return true;
19561 }
19562
19563 static const char *
19564 get_gnu_elf_note_type (unsigned e_type)
19565 {
19566 /* NB/ Keep this switch statement in sync with print_gnu_note (). */
19567 switch (e_type)
19568 {
19569 case NT_GNU_ABI_TAG:
19570 return _("NT_GNU_ABI_TAG (ABI version tag)");
19571 case NT_GNU_HWCAP:
19572 return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
19573 case NT_GNU_BUILD_ID:
19574 return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
19575 case NT_GNU_GOLD_VERSION:
19576 return _("NT_GNU_GOLD_VERSION (gold version)");
19577 case NT_GNU_PROPERTY_TYPE_0:
19578 return _("NT_GNU_PROPERTY_TYPE_0");
19579 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
19580 return _("NT_GNU_BUILD_ATTRIBUTE_OPEN");
19581 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
19582 return _("NT_GNU_BUILD_ATTRIBUTE_FUNC");
19583 default:
19584 {
19585 static char buff[64];
19586
19587 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19588 return buff;
19589 }
19590 }
19591 }
19592
19593 static void
19594 decode_x86_compat_isa (unsigned int bitmask)
19595 {
19596 while (bitmask)
19597 {
19598 unsigned int bit = bitmask & (- bitmask);
19599
19600 bitmask &= ~ bit;
19601 switch (bit)
19602 {
19603 case GNU_PROPERTY_X86_COMPAT_ISA_1_486:
19604 printf ("i486");
19605 break;
19606 case GNU_PROPERTY_X86_COMPAT_ISA_1_586:
19607 printf ("586");
19608 break;
19609 case GNU_PROPERTY_X86_COMPAT_ISA_1_686:
19610 printf ("686");
19611 break;
19612 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE:
19613 printf ("SSE");
19614 break;
19615 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE2:
19616 printf ("SSE2");
19617 break;
19618 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE3:
19619 printf ("SSE3");
19620 break;
19621 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSSE3:
19622 printf ("SSSE3");
19623 break;
19624 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_1:
19625 printf ("SSE4_1");
19626 break;
19627 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_2:
19628 printf ("SSE4_2");
19629 break;
19630 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX:
19631 printf ("AVX");
19632 break;
19633 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX2:
19634 printf ("AVX2");
19635 break;
19636 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512F:
19637 printf ("AVX512F");
19638 break;
19639 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512CD:
19640 printf ("AVX512CD");
19641 break;
19642 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512ER:
19643 printf ("AVX512ER");
19644 break;
19645 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512PF:
19646 printf ("AVX512PF");
19647 break;
19648 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512VL:
19649 printf ("AVX512VL");
19650 break;
19651 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512DQ:
19652 printf ("AVX512DQ");
19653 break;
19654 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512BW:
19655 printf ("AVX512BW");
19656 break;
19657 default:
19658 printf (_("<unknown: %x>"), bit);
19659 break;
19660 }
19661 if (bitmask)
19662 printf (", ");
19663 }
19664 }
19665
19666 static void
19667 decode_x86_compat_2_isa (unsigned int bitmask)
19668 {
19669 if (!bitmask)
19670 {
19671 printf (_("<None>"));
19672 return;
19673 }
19674
19675 while (bitmask)
19676 {
19677 unsigned int bit = bitmask & (- bitmask);
19678
19679 bitmask &= ~ bit;
19680 switch (bit)
19681 {
19682 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_CMOV:
19683 printf ("CMOV");
19684 break;
19685 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE:
19686 printf ("SSE");
19687 break;
19688 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE2:
19689 printf ("SSE2");
19690 break;
19691 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE3:
19692 printf ("SSE3");
19693 break;
19694 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSSE3:
19695 printf ("SSSE3");
19696 break;
19697 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_1:
19698 printf ("SSE4_1");
19699 break;
19700 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_2:
19701 printf ("SSE4_2");
19702 break;
19703 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX:
19704 printf ("AVX");
19705 break;
19706 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX2:
19707 printf ("AVX2");
19708 break;
19709 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_FMA:
19710 printf ("FMA");
19711 break;
19712 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512F:
19713 printf ("AVX512F");
19714 break;
19715 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512CD:
19716 printf ("AVX512CD");
19717 break;
19718 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512ER:
19719 printf ("AVX512ER");
19720 break;
19721 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512PF:
19722 printf ("AVX512PF");
19723 break;
19724 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512VL:
19725 printf ("AVX512VL");
19726 break;
19727 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512DQ:
19728 printf ("AVX512DQ");
19729 break;
19730 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512BW:
19731 printf ("AVX512BW");
19732 break;
19733 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4FMAPS:
19734 printf ("AVX512_4FMAPS");
19735 break;
19736 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4VNNIW:
19737 printf ("AVX512_4VNNIW");
19738 break;
19739 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BITALG:
19740 printf ("AVX512_BITALG");
19741 break;
19742 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_IFMA:
19743 printf ("AVX512_IFMA");
19744 break;
19745 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI:
19746 printf ("AVX512_VBMI");
19747 break;
19748 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI2:
19749 printf ("AVX512_VBMI2");
19750 break;
19751 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VNNI:
19752 printf ("AVX512_VNNI");
19753 break;
19754 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BF16:
19755 printf ("AVX512_BF16");
19756 break;
19757 default:
19758 printf (_("<unknown: %x>"), bit);
19759 break;
19760 }
19761 if (bitmask)
19762 printf (", ");
19763 }
19764 }
19765
19766 static const char *
19767 get_amdgpu_elf_note_type (unsigned int e_type)
19768 {
19769 switch (e_type)
19770 {
19771 case NT_AMDGPU_METADATA:
19772 return _("NT_AMDGPU_METADATA (code object metadata)");
19773 default:
19774 {
19775 static char buf[64];
19776 snprintf (buf, sizeof (buf), _("Unknown note type: (0x%08x)"), e_type);
19777 return buf;
19778 }
19779 }
19780 }
19781
19782 static void
19783 decode_x86_isa (unsigned int bitmask)
19784 {
19785 while (bitmask)
19786 {
19787 unsigned int bit = bitmask & (- bitmask);
19788
19789 bitmask &= ~ bit;
19790 switch (bit)
19791 {
19792 case GNU_PROPERTY_X86_ISA_1_BASELINE:
19793 printf ("x86-64-baseline");
19794 break;
19795 case GNU_PROPERTY_X86_ISA_1_V2:
19796 printf ("x86-64-v2");
19797 break;
19798 case GNU_PROPERTY_X86_ISA_1_V3:
19799 printf ("x86-64-v3");
19800 break;
19801 case GNU_PROPERTY_X86_ISA_1_V4:
19802 printf ("x86-64-v4");
19803 break;
19804 default:
19805 printf (_("<unknown: %x>"), bit);
19806 break;
19807 }
19808 if (bitmask)
19809 printf (", ");
19810 }
19811 }
19812
19813 static void
19814 decode_x86_feature_1 (unsigned int bitmask)
19815 {
19816 if (!bitmask)
19817 {
19818 printf (_("<None>"));
19819 return;
19820 }
19821
19822 while (bitmask)
19823 {
19824 unsigned int bit = bitmask & (- bitmask);
19825
19826 bitmask &= ~ bit;
19827 switch (bit)
19828 {
19829 case GNU_PROPERTY_X86_FEATURE_1_IBT:
19830 printf ("IBT");
19831 break;
19832 case GNU_PROPERTY_X86_FEATURE_1_SHSTK:
19833 printf ("SHSTK");
19834 break;
19835 case GNU_PROPERTY_X86_FEATURE_1_LAM_U48:
19836 printf ("LAM_U48");
19837 break;
19838 case GNU_PROPERTY_X86_FEATURE_1_LAM_U57:
19839 printf ("LAM_U57");
19840 break;
19841 default:
19842 printf (_("<unknown: %x>"), bit);
19843 break;
19844 }
19845 if (bitmask)
19846 printf (", ");
19847 }
19848 }
19849
19850 static void
19851 decode_x86_feature_2 (unsigned int bitmask)
19852 {
19853 if (!bitmask)
19854 {
19855 printf (_("<None>"));
19856 return;
19857 }
19858
19859 while (bitmask)
19860 {
19861 unsigned int bit = bitmask & (- bitmask);
19862
19863 bitmask &= ~ bit;
19864 switch (bit)
19865 {
19866 case GNU_PROPERTY_X86_FEATURE_2_X86:
19867 printf ("x86");
19868 break;
19869 case GNU_PROPERTY_X86_FEATURE_2_X87:
19870 printf ("x87");
19871 break;
19872 case GNU_PROPERTY_X86_FEATURE_2_MMX:
19873 printf ("MMX");
19874 break;
19875 case GNU_PROPERTY_X86_FEATURE_2_XMM:
19876 printf ("XMM");
19877 break;
19878 case GNU_PROPERTY_X86_FEATURE_2_YMM:
19879 printf ("YMM");
19880 break;
19881 case GNU_PROPERTY_X86_FEATURE_2_ZMM:
19882 printf ("ZMM");
19883 break;
19884 case GNU_PROPERTY_X86_FEATURE_2_TMM:
19885 printf ("TMM");
19886 break;
19887 case GNU_PROPERTY_X86_FEATURE_2_MASK:
19888 printf ("MASK");
19889 break;
19890 case GNU_PROPERTY_X86_FEATURE_2_FXSR:
19891 printf ("FXSR");
19892 break;
19893 case GNU_PROPERTY_X86_FEATURE_2_XSAVE:
19894 printf ("XSAVE");
19895 break;
19896 case GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT:
19897 printf ("XSAVEOPT");
19898 break;
19899 case GNU_PROPERTY_X86_FEATURE_2_XSAVEC:
19900 printf ("XSAVEC");
19901 break;
19902 default:
19903 printf (_("<unknown: %x>"), bit);
19904 break;
19905 }
19906 if (bitmask)
19907 printf (", ");
19908 }
19909 }
19910
19911 static void
19912 decode_aarch64_feature_1_and (unsigned int bitmask)
19913 {
19914 while (bitmask)
19915 {
19916 unsigned int bit = bitmask & (- bitmask);
19917
19918 bitmask &= ~ bit;
19919 switch (bit)
19920 {
19921 case GNU_PROPERTY_AARCH64_FEATURE_1_BTI:
19922 printf ("BTI");
19923 break;
19924
19925 case GNU_PROPERTY_AARCH64_FEATURE_1_PAC:
19926 printf ("PAC");
19927 break;
19928
19929 default:
19930 printf (_("<unknown: %x>"), bit);
19931 break;
19932 }
19933 if (bitmask)
19934 printf (", ");
19935 }
19936 }
19937
19938 static void
19939 decode_1_needed (unsigned int bitmask)
19940 {
19941 while (bitmask)
19942 {
19943 unsigned int bit = bitmask & (- bitmask);
19944
19945 bitmask &= ~ bit;
19946 switch (bit)
19947 {
19948 case GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS:
19949 printf ("indirect external access");
19950 break;
19951 default:
19952 printf (_("<unknown: %x>"), bit);
19953 break;
19954 }
19955 if (bitmask)
19956 printf (", ");
19957 }
19958 }
19959
19960 static void
19961 print_gnu_property_note (Filedata * filedata, Elf_Internal_Note * pnote)
19962 {
19963 unsigned char * ptr = (unsigned char *) pnote->descdata;
19964 unsigned char * ptr_end = ptr + pnote->descsz;
19965 unsigned int size = is_32bit_elf ? 4 : 8;
19966
19967 printf (_(" Properties: "));
19968
19969 if (pnote->descsz < 8 || (pnote->descsz % size) != 0)
19970 {
19971 printf (_("<corrupt GNU_PROPERTY_TYPE, size = %#lx>\n"), pnote->descsz);
19972 return;
19973 }
19974
19975 while (ptr < ptr_end)
19976 {
19977 unsigned int j;
19978 unsigned int type;
19979 unsigned int datasz;
19980
19981 if ((size_t) (ptr_end - ptr) < 8)
19982 {
19983 printf (_("<corrupt descsz: %#lx>\n"), pnote->descsz);
19984 break;
19985 }
19986
19987 type = byte_get (ptr, 4);
19988 datasz = byte_get (ptr + 4, 4);
19989
19990 ptr += 8;
19991
19992 if (datasz > (size_t) (ptr_end - ptr))
19993 {
19994 printf (_("<corrupt type (%#x) datasz: %#x>\n"),
19995 type, datasz);
19996 break;
19997 }
19998
19999 if (type >= GNU_PROPERTY_LOPROC && type <= GNU_PROPERTY_HIPROC)
20000 {
20001 if (filedata->file_header.e_machine == EM_X86_64
20002 || filedata->file_header.e_machine == EM_IAMCU
20003 || filedata->file_header.e_machine == EM_386)
20004 {
20005 unsigned int bitmask;
20006
20007 if (datasz == 4)
20008 bitmask = byte_get (ptr, 4);
20009 else
20010 bitmask = 0;
20011
20012 switch (type)
20013 {
20014 case GNU_PROPERTY_X86_ISA_1_USED:
20015 if (datasz != 4)
20016 printf (_("x86 ISA used: <corrupt length: %#x> "),
20017 datasz);
20018 else
20019 {
20020 printf ("x86 ISA used: ");
20021 decode_x86_isa (bitmask);
20022 }
20023 goto next;
20024
20025 case GNU_PROPERTY_X86_ISA_1_NEEDED:
20026 if (datasz != 4)
20027 printf (_("x86 ISA needed: <corrupt length: %#x> "),
20028 datasz);
20029 else
20030 {
20031 printf ("x86 ISA needed: ");
20032 decode_x86_isa (bitmask);
20033 }
20034 goto next;
20035
20036 case GNU_PROPERTY_X86_FEATURE_1_AND:
20037 if (datasz != 4)
20038 printf (_("x86 feature: <corrupt length: %#x> "),
20039 datasz);
20040 else
20041 {
20042 printf ("x86 feature: ");
20043 decode_x86_feature_1 (bitmask);
20044 }
20045 goto next;
20046
20047 case GNU_PROPERTY_X86_FEATURE_2_USED:
20048 if (datasz != 4)
20049 printf (_("x86 feature used: <corrupt length: %#x> "),
20050 datasz);
20051 else
20052 {
20053 printf ("x86 feature used: ");
20054 decode_x86_feature_2 (bitmask);
20055 }
20056 goto next;
20057
20058 case GNU_PROPERTY_X86_FEATURE_2_NEEDED:
20059 if (datasz != 4)
20060 printf (_("x86 feature needed: <corrupt length: %#x> "), datasz);
20061 else
20062 {
20063 printf ("x86 feature needed: ");
20064 decode_x86_feature_2 (bitmask);
20065 }
20066 goto next;
20067
20068 case GNU_PROPERTY_X86_COMPAT_ISA_1_USED:
20069 if (datasz != 4)
20070 printf (_("x86 ISA used: <corrupt length: %#x> "),
20071 datasz);
20072 else
20073 {
20074 printf ("x86 ISA used: ");
20075 decode_x86_compat_isa (bitmask);
20076 }
20077 goto next;
20078
20079 case GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED:
20080 if (datasz != 4)
20081 printf (_("x86 ISA needed: <corrupt length: %#x> "),
20082 datasz);
20083 else
20084 {
20085 printf ("x86 ISA needed: ");
20086 decode_x86_compat_isa (bitmask);
20087 }
20088 goto next;
20089
20090 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_USED:
20091 if (datasz != 4)
20092 printf (_("x86 ISA used: <corrupt length: %#x> "),
20093 datasz);
20094 else
20095 {
20096 printf ("x86 ISA used: ");
20097 decode_x86_compat_2_isa (bitmask);
20098 }
20099 goto next;
20100
20101 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_NEEDED:
20102 if (datasz != 4)
20103 printf (_("x86 ISA needed: <corrupt length: %#x> "),
20104 datasz);
20105 else
20106 {
20107 printf ("x86 ISA needed: ");
20108 decode_x86_compat_2_isa (bitmask);
20109 }
20110 goto next;
20111
20112 default:
20113 break;
20114 }
20115 }
20116 else if (filedata->file_header.e_machine == EM_AARCH64)
20117 {
20118 if (type == GNU_PROPERTY_AARCH64_FEATURE_1_AND)
20119 {
20120 printf ("AArch64 feature: ");
20121 if (datasz != 4)
20122 printf (_("<corrupt length: %#x> "), datasz);
20123 else
20124 decode_aarch64_feature_1_and (byte_get (ptr, 4));
20125 goto next;
20126 }
20127 }
20128 }
20129 else
20130 {
20131 switch (type)
20132 {
20133 case GNU_PROPERTY_STACK_SIZE:
20134 printf (_("stack size: "));
20135 if (datasz != size)
20136 printf (_("<corrupt length: %#x> "), datasz);
20137 else
20138 printf ("%#lx", (unsigned long) byte_get (ptr, size));
20139 goto next;
20140
20141 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
20142 printf ("no copy on protected ");
20143 if (datasz)
20144 printf (_("<corrupt length: %#x> "), datasz);
20145 goto next;
20146
20147 default:
20148 if ((type >= GNU_PROPERTY_UINT32_AND_LO
20149 && type <= GNU_PROPERTY_UINT32_AND_HI)
20150 || (type >= GNU_PROPERTY_UINT32_OR_LO
20151 && type <= GNU_PROPERTY_UINT32_OR_HI))
20152 {
20153 switch (type)
20154 {
20155 case GNU_PROPERTY_1_NEEDED:
20156 if (datasz != 4)
20157 printf (_("1_needed: <corrupt length: %#x> "),
20158 datasz);
20159 else
20160 {
20161 unsigned int bitmask = byte_get (ptr, 4);
20162 printf ("1_needed: ");
20163 decode_1_needed (bitmask);
20164 }
20165 goto next;
20166
20167 default:
20168 break;
20169 }
20170 if (type <= GNU_PROPERTY_UINT32_AND_HI)
20171 printf (_("UINT32_AND (%#x): "), type);
20172 else
20173 printf (_("UINT32_OR (%#x): "), type);
20174 if (datasz != 4)
20175 printf (_("<corrupt length: %#x> "), datasz);
20176 else
20177 printf ("%#x", (unsigned int) byte_get (ptr, 4));
20178 goto next;
20179 }
20180 break;
20181 }
20182 }
20183
20184 if (type < GNU_PROPERTY_LOPROC)
20185 printf (_("<unknown type %#x data: "), type);
20186 else if (type < GNU_PROPERTY_LOUSER)
20187 printf (_("<processor-specific type %#x data: "), type);
20188 else
20189 printf (_("<application-specific type %#x data: "), type);
20190 for (j = 0; j < datasz; ++j)
20191 printf ("%02x ", ptr[j] & 0xff);
20192 printf (">");
20193
20194 next:
20195 ptr += ((datasz + (size - 1)) & ~ (size - 1));
20196 if (ptr == ptr_end)
20197 break;
20198
20199 if (do_wide)
20200 printf (", ");
20201 else
20202 printf ("\n\t");
20203 }
20204
20205 printf ("\n");
20206 }
20207
20208 static bool
20209 print_gnu_note (Filedata * filedata, Elf_Internal_Note *pnote)
20210 {
20211 /* NB/ Keep this switch statement in sync with get_gnu_elf_note_type (). */
20212 switch (pnote->type)
20213 {
20214 case NT_GNU_BUILD_ID:
20215 {
20216 unsigned long i;
20217
20218 printf (_(" Build ID: "));
20219 for (i = 0; i < pnote->descsz; ++i)
20220 printf ("%02x", pnote->descdata[i] & 0xff);
20221 printf ("\n");
20222 }
20223 break;
20224
20225 case NT_GNU_ABI_TAG:
20226 {
20227 unsigned long os, major, minor, subminor;
20228 const char *osname;
20229
20230 /* PR 17531: file: 030-599401-0.004. */
20231 if (pnote->descsz < 16)
20232 {
20233 printf (_(" <corrupt GNU_ABI_TAG>\n"));
20234 break;
20235 }
20236
20237 os = byte_get ((unsigned char *) pnote->descdata, 4);
20238 major = byte_get ((unsigned char *) pnote->descdata + 4, 4);
20239 minor = byte_get ((unsigned char *) pnote->descdata + 8, 4);
20240 subminor = byte_get ((unsigned char *) pnote->descdata + 12, 4);
20241
20242 switch (os)
20243 {
20244 case GNU_ABI_TAG_LINUX:
20245 osname = "Linux";
20246 break;
20247 case GNU_ABI_TAG_HURD:
20248 osname = "Hurd";
20249 break;
20250 case GNU_ABI_TAG_SOLARIS:
20251 osname = "Solaris";
20252 break;
20253 case GNU_ABI_TAG_FREEBSD:
20254 osname = "FreeBSD";
20255 break;
20256 case GNU_ABI_TAG_NETBSD:
20257 osname = "NetBSD";
20258 break;
20259 case GNU_ABI_TAG_SYLLABLE:
20260 osname = "Syllable";
20261 break;
20262 case GNU_ABI_TAG_NACL:
20263 osname = "NaCl";
20264 break;
20265 default:
20266 osname = "Unknown";
20267 break;
20268 }
20269
20270 printf (_(" OS: %s, ABI: %ld.%ld.%ld\n"), osname,
20271 major, minor, subminor);
20272 }
20273 break;
20274
20275 case NT_GNU_GOLD_VERSION:
20276 {
20277 unsigned long i;
20278
20279 printf (_(" Version: "));
20280 for (i = 0; i < pnote->descsz && pnote->descdata[i] != '\0'; ++i)
20281 printf ("%c", pnote->descdata[i]);
20282 printf ("\n");
20283 }
20284 break;
20285
20286 case NT_GNU_HWCAP:
20287 {
20288 unsigned long num_entries, mask;
20289
20290 /* Hardware capabilities information. Word 0 is the number of entries.
20291 Word 1 is a bitmask of enabled entries. The rest of the descriptor
20292 is a series of entries, where each entry is a single byte followed
20293 by a nul terminated string. The byte gives the bit number to test
20294 if enabled in the bitmask. */
20295 printf (_(" Hardware Capabilities: "));
20296 if (pnote->descsz < 8)
20297 {
20298 error (_("<corrupt GNU_HWCAP>\n"));
20299 return false;
20300 }
20301 num_entries = byte_get ((unsigned char *) pnote->descdata, 4);
20302 mask = byte_get ((unsigned char *) pnote->descdata + 4, 4);
20303 printf (_("num entries: %ld, enabled mask: %lx\n"), num_entries, mask);
20304 /* FIXME: Add code to display the entries... */
20305 }
20306 break;
20307
20308 case NT_GNU_PROPERTY_TYPE_0:
20309 print_gnu_property_note (filedata, pnote);
20310 break;
20311
20312 default:
20313 /* Handle unrecognised types. An error message should have already been
20314 created by get_gnu_elf_note_type(), so all that we need to do is to
20315 display the data. */
20316 {
20317 unsigned long i;
20318
20319 printf (_(" Description data: "));
20320 for (i = 0; i < pnote->descsz; ++i)
20321 printf ("%02x ", pnote->descdata[i] & 0xff);
20322 printf ("\n");
20323 }
20324 break;
20325 }
20326
20327 return true;
20328 }
20329
20330 static const char *
20331 get_v850_elf_note_type (enum v850_notes n_type)
20332 {
20333 static char buff[64];
20334
20335 switch (n_type)
20336 {
20337 case V850_NOTE_ALIGNMENT: return _("Alignment of 8-byte objects");
20338 case V850_NOTE_DATA_SIZE: return _("Sizeof double and long double");
20339 case V850_NOTE_FPU_INFO: return _("Type of FPU support needed");
20340 case V850_NOTE_SIMD_INFO: return _("Use of SIMD instructions");
20341 case V850_NOTE_CACHE_INFO: return _("Use of cache");
20342 case V850_NOTE_MMU_INFO: return _("Use of MMU");
20343 default:
20344 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), n_type);
20345 return buff;
20346 }
20347 }
20348
20349 static bool
20350 print_v850_note (Elf_Internal_Note * pnote)
20351 {
20352 unsigned int val;
20353
20354 if (pnote->descsz != 4)
20355 return false;
20356
20357 val = byte_get ((unsigned char *) pnote->descdata, pnote->descsz);
20358
20359 if (val == 0)
20360 {
20361 printf (_("not set\n"));
20362 return true;
20363 }
20364
20365 switch (pnote->type)
20366 {
20367 case V850_NOTE_ALIGNMENT:
20368 switch (val)
20369 {
20370 case EF_RH850_DATA_ALIGN4: printf (_("4-byte\n")); return true;
20371 case EF_RH850_DATA_ALIGN8: printf (_("8-byte\n")); return true;
20372 }
20373 break;
20374
20375 case V850_NOTE_DATA_SIZE:
20376 switch (val)
20377 {
20378 case EF_RH850_DOUBLE32: printf (_("4-bytes\n")); return true;
20379 case EF_RH850_DOUBLE64: printf (_("8-bytes\n")); return true;
20380 }
20381 break;
20382
20383 case V850_NOTE_FPU_INFO:
20384 switch (val)
20385 {
20386 case EF_RH850_FPU20: printf (_("FPU-2.0\n")); return true;
20387 case EF_RH850_FPU30: printf (_("FPU-3.0\n")); return true;
20388 }
20389 break;
20390
20391 case V850_NOTE_MMU_INFO:
20392 case V850_NOTE_CACHE_INFO:
20393 case V850_NOTE_SIMD_INFO:
20394 if (val == EF_RH850_SIMD)
20395 {
20396 printf (_("yes\n"));
20397 return true;
20398 }
20399 break;
20400
20401 default:
20402 /* An 'unknown note type' message will already have been displayed. */
20403 break;
20404 }
20405
20406 printf (_("unknown value: %x\n"), val);
20407 return false;
20408 }
20409
20410 static bool
20411 process_netbsd_elf_note (Elf_Internal_Note * pnote)
20412 {
20413 unsigned int version;
20414
20415 switch (pnote->type)
20416 {
20417 case NT_NETBSD_IDENT:
20418 if (pnote->descsz < 1)
20419 break;
20420 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
20421 if ((version / 10000) % 100)
20422 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u%s%c)\n", pnote->descsz,
20423 version, version / 100000000, (version / 1000000) % 100,
20424 (version / 10000) % 100 > 26 ? "Z" : "",
20425 'A' + (version / 10000) % 26);
20426 else
20427 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u.%u)\n", pnote->descsz,
20428 version, version / 100000000, (version / 1000000) % 100,
20429 (version / 100) % 100);
20430 return true;
20431
20432 case NT_NETBSD_MARCH:
20433 printf (" NetBSD\t\t0x%08lx\tMARCH <%s>\n", pnote->descsz,
20434 pnote->descdata);
20435 return true;
20436
20437 case NT_NETBSD_PAX:
20438 if (pnote->descsz < 1)
20439 break;
20440 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
20441 printf (" NetBSD\t\t0x%08lx\tPaX <%s%s%s%s%s%s>\n", pnote->descsz,
20442 ((version & NT_NETBSD_PAX_MPROTECT) ? "+mprotect" : ""),
20443 ((version & NT_NETBSD_PAX_NOMPROTECT) ? "-mprotect" : ""),
20444 ((version & NT_NETBSD_PAX_GUARD) ? "+guard" : ""),
20445 ((version & NT_NETBSD_PAX_NOGUARD) ? "-guard" : ""),
20446 ((version & NT_NETBSD_PAX_ASLR) ? "+ASLR" : ""),
20447 ((version & NT_NETBSD_PAX_NOASLR) ? "-ASLR" : ""));
20448 return true;
20449 }
20450
20451 printf (" NetBSD\t0x%08lx\tUnknown note type: (0x%08lx)\n",
20452 pnote->descsz, pnote->type);
20453 return false;
20454 }
20455
20456 static const char *
20457 get_freebsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
20458 {
20459 switch (e_type)
20460 {
20461 case NT_FREEBSD_THRMISC:
20462 return _("NT_THRMISC (thrmisc structure)");
20463 case NT_FREEBSD_PROCSTAT_PROC:
20464 return _("NT_PROCSTAT_PROC (proc data)");
20465 case NT_FREEBSD_PROCSTAT_FILES:
20466 return _("NT_PROCSTAT_FILES (files data)");
20467 case NT_FREEBSD_PROCSTAT_VMMAP:
20468 return _("NT_PROCSTAT_VMMAP (vmmap data)");
20469 case NT_FREEBSD_PROCSTAT_GROUPS:
20470 return _("NT_PROCSTAT_GROUPS (groups data)");
20471 case NT_FREEBSD_PROCSTAT_UMASK:
20472 return _("NT_PROCSTAT_UMASK (umask data)");
20473 case NT_FREEBSD_PROCSTAT_RLIMIT:
20474 return _("NT_PROCSTAT_RLIMIT (rlimit data)");
20475 case NT_FREEBSD_PROCSTAT_OSREL:
20476 return _("NT_PROCSTAT_OSREL (osreldate data)");
20477 case NT_FREEBSD_PROCSTAT_PSSTRINGS:
20478 return _("NT_PROCSTAT_PSSTRINGS (ps_strings data)");
20479 case NT_FREEBSD_PROCSTAT_AUXV:
20480 return _("NT_PROCSTAT_AUXV (auxv data)");
20481 case NT_FREEBSD_PTLWPINFO:
20482 return _("NT_PTLWPINFO (ptrace_lwpinfo structure)");
20483 case NT_FREEBSD_X86_SEGBASES:
20484 return _("NT_X86_SEGBASES (x86 segment base registers)");
20485 }
20486 return get_note_type (filedata, e_type);
20487 }
20488
20489 static const char *
20490 get_netbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
20491 {
20492 static char buff[64];
20493
20494 switch (e_type)
20495 {
20496 case NT_NETBSDCORE_PROCINFO:
20497 /* NetBSD core "procinfo" structure. */
20498 return _("NetBSD procinfo structure");
20499
20500 case NT_NETBSDCORE_AUXV:
20501 return _("NetBSD ELF auxiliary vector data");
20502
20503 case NT_NETBSDCORE_LWPSTATUS:
20504 return _("PT_LWPSTATUS (ptrace_lwpstatus structure)");
20505
20506 default:
20507 /* As of Jan 2020 there are no other machine-independent notes
20508 defined for NetBSD core files. If the note type is less
20509 than the start of the machine-dependent note types, we don't
20510 understand it. */
20511
20512 if (e_type < NT_NETBSDCORE_FIRSTMACH)
20513 {
20514 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20515 return buff;
20516 }
20517 break;
20518 }
20519
20520 switch (filedata->file_header.e_machine)
20521 {
20522 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
20523 and PT_GETFPREGS == mach+2. */
20524
20525 case EM_OLD_ALPHA:
20526 case EM_ALPHA:
20527 case EM_SPARC:
20528 case EM_SPARC32PLUS:
20529 case EM_SPARCV9:
20530 switch (e_type)
20531 {
20532 case NT_NETBSDCORE_FIRSTMACH + 0:
20533 return _("PT_GETREGS (reg structure)");
20534 case NT_NETBSDCORE_FIRSTMACH + 2:
20535 return _("PT_GETFPREGS (fpreg structure)");
20536 default:
20537 break;
20538 }
20539 break;
20540
20541 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
20542 There's also old PT___GETREGS40 == mach + 1 for old reg
20543 structure which lacks GBR. */
20544 case EM_SH:
20545 switch (e_type)
20546 {
20547 case NT_NETBSDCORE_FIRSTMACH + 1:
20548 return _("PT___GETREGS40 (old reg structure)");
20549 case NT_NETBSDCORE_FIRSTMACH + 3:
20550 return _("PT_GETREGS (reg structure)");
20551 case NT_NETBSDCORE_FIRSTMACH + 5:
20552 return _("PT_GETFPREGS (fpreg structure)");
20553 default:
20554 break;
20555 }
20556 break;
20557
20558 /* On all other arch's, PT_GETREGS == mach+1 and
20559 PT_GETFPREGS == mach+3. */
20560 default:
20561 switch (e_type)
20562 {
20563 case NT_NETBSDCORE_FIRSTMACH + 1:
20564 return _("PT_GETREGS (reg structure)");
20565 case NT_NETBSDCORE_FIRSTMACH + 3:
20566 return _("PT_GETFPREGS (fpreg structure)");
20567 default:
20568 break;
20569 }
20570 }
20571
20572 snprintf (buff, sizeof (buff), "PT_FIRSTMACH+%d",
20573 e_type - NT_NETBSDCORE_FIRSTMACH);
20574 return buff;
20575 }
20576
20577 static const char *
20578 get_openbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
20579 {
20580 switch (e_type)
20581 {
20582 case NT_OPENBSD_PROCINFO:
20583 return _("OpenBSD procinfo structure");
20584 case NT_OPENBSD_AUXV:
20585 return _("OpenBSD ELF auxiliary vector data");
20586 case NT_OPENBSD_REGS:
20587 return _("OpenBSD regular registers");
20588 case NT_OPENBSD_FPREGS:
20589 return _("OpenBSD floating point registers");
20590 case NT_OPENBSD_WCOOKIE:
20591 return _("OpenBSD window cookie");
20592 }
20593
20594 return get_note_type (filedata, e_type);
20595 }
20596
20597 static const char *
20598 get_stapsdt_note_type (unsigned e_type)
20599 {
20600 static char buff[64];
20601
20602 switch (e_type)
20603 {
20604 case NT_STAPSDT:
20605 return _("NT_STAPSDT (SystemTap probe descriptors)");
20606
20607 default:
20608 break;
20609 }
20610
20611 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20612 return buff;
20613 }
20614
20615 static bool
20616 print_stapsdt_note (Elf_Internal_Note *pnote)
20617 {
20618 size_t len, maxlen;
20619 unsigned long addr_size = is_32bit_elf ? 4 : 8;
20620 char *data = pnote->descdata;
20621 char *data_end = pnote->descdata + pnote->descsz;
20622 uint64_t pc, base_addr, semaphore;
20623 char *provider, *probe, *arg_fmt;
20624
20625 if (pnote->descsz < (addr_size * 3))
20626 goto stapdt_note_too_small;
20627
20628 pc = byte_get ((unsigned char *) data, addr_size);
20629 data += addr_size;
20630
20631 base_addr = byte_get ((unsigned char *) data, addr_size);
20632 data += addr_size;
20633
20634 semaphore = byte_get ((unsigned char *) data, addr_size);
20635 data += addr_size;
20636
20637 if (data >= data_end)
20638 goto stapdt_note_too_small;
20639 maxlen = data_end - data;
20640 len = strnlen (data, maxlen);
20641 if (len < maxlen)
20642 {
20643 provider = data;
20644 data += len + 1;
20645 }
20646 else
20647 goto stapdt_note_too_small;
20648
20649 if (data >= data_end)
20650 goto stapdt_note_too_small;
20651 maxlen = data_end - data;
20652 len = strnlen (data, maxlen);
20653 if (len < maxlen)
20654 {
20655 probe = data;
20656 data += len + 1;
20657 }
20658 else
20659 goto stapdt_note_too_small;
20660
20661 if (data >= data_end)
20662 goto stapdt_note_too_small;
20663 maxlen = data_end - data;
20664 len = strnlen (data, maxlen);
20665 if (len < maxlen)
20666 {
20667 arg_fmt = data;
20668 data += len + 1;
20669 }
20670 else
20671 goto stapdt_note_too_small;
20672
20673 printf (_(" Provider: %s\n"), provider);
20674 printf (_(" Name: %s\n"), probe);
20675 printf (_(" Location: "));
20676 print_vma (pc, FULL_HEX);
20677 printf (_(", Base: "));
20678 print_vma (base_addr, FULL_HEX);
20679 printf (_(", Semaphore: "));
20680 print_vma (semaphore, FULL_HEX);
20681 printf ("\n");
20682 printf (_(" Arguments: %s\n"), arg_fmt);
20683
20684 return data == data_end;
20685
20686 stapdt_note_too_small:
20687 printf (_(" <corrupt - note is too small>\n"));
20688 error (_("corrupt stapdt note - the data size is too small\n"));
20689 return false;
20690 }
20691
20692 static bool
20693 print_fdo_note (Elf_Internal_Note * pnote)
20694 {
20695 if (pnote->descsz > 0 && pnote->type == FDO_PACKAGING_METADATA)
20696 {
20697 printf (_(" Packaging Metadata: %.*s\n"), (int) pnote->descsz, pnote->descdata);
20698 return true;
20699 }
20700 return false;
20701 }
20702
20703 static const char *
20704 get_ia64_vms_note_type (unsigned e_type)
20705 {
20706 static char buff[64];
20707
20708 switch (e_type)
20709 {
20710 case NT_VMS_MHD:
20711 return _("NT_VMS_MHD (module header)");
20712 case NT_VMS_LNM:
20713 return _("NT_VMS_LNM (language name)");
20714 case NT_VMS_SRC:
20715 return _("NT_VMS_SRC (source files)");
20716 case NT_VMS_TITLE:
20717 return "NT_VMS_TITLE";
20718 case NT_VMS_EIDC:
20719 return _("NT_VMS_EIDC (consistency check)");
20720 case NT_VMS_FPMODE:
20721 return _("NT_VMS_FPMODE (FP mode)");
20722 case NT_VMS_LINKTIME:
20723 return "NT_VMS_LINKTIME";
20724 case NT_VMS_IMGNAM:
20725 return _("NT_VMS_IMGNAM (image name)");
20726 case NT_VMS_IMGID:
20727 return _("NT_VMS_IMGID (image id)");
20728 case NT_VMS_LINKID:
20729 return _("NT_VMS_LINKID (link id)");
20730 case NT_VMS_IMGBID:
20731 return _("NT_VMS_IMGBID (build id)");
20732 case NT_VMS_GSTNAM:
20733 return _("NT_VMS_GSTNAM (sym table name)");
20734 case NT_VMS_ORIG_DYN:
20735 return "NT_VMS_ORIG_DYN";
20736 case NT_VMS_PATCHTIME:
20737 return "NT_VMS_PATCHTIME";
20738 default:
20739 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20740 return buff;
20741 }
20742 }
20743
20744 static bool
20745 print_ia64_vms_note (Elf_Internal_Note * pnote)
20746 {
20747 int maxlen = pnote->descsz;
20748
20749 if (maxlen < 2 || (unsigned long) maxlen != pnote->descsz)
20750 goto desc_size_fail;
20751
20752 switch (pnote->type)
20753 {
20754 case NT_VMS_MHD:
20755 if (maxlen <= 36)
20756 goto desc_size_fail;
20757
20758 int l = (int) strnlen (pnote->descdata + 34, maxlen - 34);
20759
20760 printf (_(" Creation date : %.17s\n"), pnote->descdata);
20761 printf (_(" Last patch date: %.17s\n"), pnote->descdata + 17);
20762 if (l + 34 < maxlen)
20763 {
20764 printf (_(" Module name : %s\n"), pnote->descdata + 34);
20765 if (l + 35 < maxlen)
20766 printf (_(" Module version : %s\n"), pnote->descdata + 34 + l + 1);
20767 else
20768 printf (_(" Module version : <missing>\n"));
20769 }
20770 else
20771 {
20772 printf (_(" Module name : <missing>\n"));
20773 printf (_(" Module version : <missing>\n"));
20774 }
20775 break;
20776
20777 case NT_VMS_LNM:
20778 printf (_(" Language: %.*s\n"), maxlen, pnote->descdata);
20779 break;
20780
20781 case NT_VMS_FPMODE:
20782 printf (_(" Floating Point mode: "));
20783 if (maxlen < 8)
20784 goto desc_size_fail;
20785 /* FIXME: Generate an error if descsz > 8 ? */
20786
20787 printf ("0x%016" PRIx64 "\n",
20788 byte_get ((unsigned char *) pnote->descdata, 8));
20789 break;
20790
20791 case NT_VMS_LINKTIME:
20792 printf (_(" Link time: "));
20793 if (maxlen < 8)
20794 goto desc_size_fail;
20795 /* FIXME: Generate an error if descsz > 8 ? */
20796
20797 print_vms_time (byte_get ((unsigned char *) pnote->descdata, 8));
20798 printf ("\n");
20799 break;
20800
20801 case NT_VMS_PATCHTIME:
20802 printf (_(" Patch time: "));
20803 if (maxlen < 8)
20804 goto desc_size_fail;
20805 /* FIXME: Generate an error if descsz > 8 ? */
20806
20807 print_vms_time (byte_get ((unsigned char *) pnote->descdata, 8));
20808 printf ("\n");
20809 break;
20810
20811 case NT_VMS_ORIG_DYN:
20812 if (maxlen < 34)
20813 goto desc_size_fail;
20814
20815 printf (_(" Major id: %u, minor id: %u\n"),
20816 (unsigned) byte_get ((unsigned char *) pnote->descdata, 4),
20817 (unsigned) byte_get ((unsigned char *) pnote->descdata + 4, 4));
20818 printf (_(" Last modified : "));
20819 print_vms_time (byte_get ((unsigned char *) pnote->descdata + 8, 8));
20820 printf (_("\n Link flags : "));
20821 printf ("0x%016" PRIx64 "\n",
20822 byte_get ((unsigned char *) pnote->descdata + 16, 8));
20823 printf (_(" Header flags: 0x%08x\n"),
20824 (unsigned) byte_get ((unsigned char *) pnote->descdata + 24, 4));
20825 printf (_(" Image id : %.*s\n"), maxlen - 32, pnote->descdata + 32);
20826 break;
20827
20828 case NT_VMS_IMGNAM:
20829 printf (_(" Image name: %.*s\n"), maxlen, pnote->descdata);
20830 break;
20831
20832 case NT_VMS_GSTNAM:
20833 printf (_(" Global symbol table name: %.*s\n"), maxlen, pnote->descdata);
20834 break;
20835
20836 case NT_VMS_IMGID:
20837 printf (_(" Image id: %.*s\n"), maxlen, pnote->descdata);
20838 break;
20839
20840 case NT_VMS_LINKID:
20841 printf (_(" Linker id: %.*s\n"), maxlen, pnote->descdata);
20842 break;
20843
20844 default:
20845 return false;
20846 }
20847
20848 return true;
20849
20850 desc_size_fail:
20851 printf (_(" <corrupt - data size is too small>\n"));
20852 error (_("corrupt IA64 note: data size is too small\n"));
20853 return false;
20854 }
20855
20856 struct build_attr_cache {
20857 Filedata *filedata;
20858 char *strtab;
20859 unsigned long strtablen;
20860 Elf_Internal_Sym *symtab;
20861 unsigned long nsyms;
20862 } ba_cache;
20863
20864 /* Find the symbol associated with a build attribute that is attached
20865 to address OFFSET. If PNAME is non-NULL then store the name of
20866 the symbol (if found) in the provided pointer, Returns NULL if a
20867 symbol could not be found. */
20868
20869 static Elf_Internal_Sym *
20870 get_symbol_for_build_attribute (Filedata *filedata,
20871 unsigned long offset,
20872 bool is_open_attr,
20873 const char **pname)
20874 {
20875 Elf_Internal_Sym *saved_sym = NULL;
20876 Elf_Internal_Sym *sym;
20877
20878 if (filedata->section_headers != NULL
20879 && (ba_cache.filedata == NULL || filedata != ba_cache.filedata))
20880 {
20881 Elf_Internal_Shdr * symsec;
20882
20883 free (ba_cache.strtab);
20884 ba_cache.strtab = NULL;
20885 free (ba_cache.symtab);
20886 ba_cache.symtab = NULL;
20887
20888 /* Load the symbol and string sections. */
20889 for (symsec = filedata->section_headers;
20890 symsec < filedata->section_headers + filedata->file_header.e_shnum;
20891 symsec ++)
20892 {
20893 if (symsec->sh_type == SHT_SYMTAB
20894 && get_symtab (filedata, symsec,
20895 &ba_cache.symtab, &ba_cache.nsyms,
20896 &ba_cache.strtab, &ba_cache.strtablen))
20897 break;
20898 }
20899 ba_cache.filedata = filedata;
20900 }
20901
20902 if (ba_cache.symtab == NULL)
20903 return NULL;
20904
20905 /* Find a symbol whose value matches offset. */
20906 for (sym = ba_cache.symtab; sym < ba_cache.symtab + ba_cache.nsyms; sym ++)
20907 if (sym->st_value == offset)
20908 {
20909 if (sym->st_name >= ba_cache.strtablen)
20910 /* Huh ? This should not happen. */
20911 continue;
20912
20913 if (ba_cache.strtab[sym->st_name] == 0)
20914 continue;
20915
20916 /* The AArch64, ARM and RISC-V architectures define mapping symbols
20917 (eg $d, $x, $t) which we want to ignore. */
20918 if (ba_cache.strtab[sym->st_name] == '$'
20919 && ba_cache.strtab[sym->st_name + 1] != 0
20920 && ba_cache.strtab[sym->st_name + 2] == 0)
20921 continue;
20922
20923 if (is_open_attr)
20924 {
20925 /* For OPEN attributes we prefer GLOBAL over LOCAL symbols
20926 and FILE or OBJECT symbols over NOTYPE symbols. We skip
20927 FUNC symbols entirely. */
20928 switch (ELF_ST_TYPE (sym->st_info))
20929 {
20930 case STT_OBJECT:
20931 case STT_FILE:
20932 saved_sym = sym;
20933 if (sym->st_size)
20934 {
20935 /* If the symbol has a size associated
20936 with it then we can stop searching. */
20937 sym = ba_cache.symtab + ba_cache.nsyms;
20938 }
20939 continue;
20940
20941 case STT_FUNC:
20942 /* Ignore function symbols. */
20943 continue;
20944
20945 default:
20946 break;
20947 }
20948
20949 switch (ELF_ST_BIND (sym->st_info))
20950 {
20951 case STB_GLOBAL:
20952 if (saved_sym == NULL
20953 || ELF_ST_TYPE (saved_sym->st_info) != STT_OBJECT)
20954 saved_sym = sym;
20955 break;
20956
20957 case STB_LOCAL:
20958 if (saved_sym == NULL)
20959 saved_sym = sym;
20960 break;
20961
20962 default:
20963 break;
20964 }
20965 }
20966 else
20967 {
20968 if (ELF_ST_TYPE (sym->st_info) != STT_FUNC)
20969 continue;
20970
20971 saved_sym = sym;
20972 break;
20973 }
20974 }
20975
20976 if (saved_sym && pname)
20977 * pname = ba_cache.strtab + saved_sym->st_name;
20978
20979 return saved_sym;
20980 }
20981
20982 /* Returns true iff addr1 and addr2 are in the same section. */
20983
20984 static bool
20985 same_section (Filedata * filedata, unsigned long addr1, unsigned long addr2)
20986 {
20987 Elf_Internal_Shdr * a1;
20988 Elf_Internal_Shdr * a2;
20989
20990 a1 = find_section_by_address (filedata, addr1);
20991 a2 = find_section_by_address (filedata, addr2);
20992
20993 return a1 == a2 && a1 != NULL;
20994 }
20995
20996 static bool
20997 print_gnu_build_attribute_description (Elf_Internal_Note * pnote,
20998 Filedata * filedata)
20999 {
21000 static unsigned long global_offset = 0;
21001 static unsigned long global_end = 0;
21002 static unsigned long func_offset = 0;
21003 static unsigned long func_end = 0;
21004
21005 Elf_Internal_Sym *sym;
21006 const char *name;
21007 unsigned long start;
21008 unsigned long end;
21009 bool is_open_attr = pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN;
21010
21011 switch (pnote->descsz)
21012 {
21013 case 0:
21014 /* A zero-length description means that the range of
21015 the previous note of the same type should be used. */
21016 if (is_open_attr)
21017 {
21018 if (global_end > global_offset)
21019 printf (_(" Applies to region from %#lx to %#lx\n"),
21020 global_offset, global_end);
21021 else
21022 printf (_(" Applies to region from %#lx\n"), global_offset);
21023 }
21024 else
21025 {
21026 if (func_end > func_offset)
21027 printf (_(" Applies to region from %#lx to %#lx\n"), func_offset, func_end);
21028 else
21029 printf (_(" Applies to region from %#lx\n"), func_offset);
21030 }
21031 return true;
21032
21033 case 4:
21034 start = byte_get ((unsigned char *) pnote->descdata, 4);
21035 end = 0;
21036 break;
21037
21038 case 8:
21039 start = byte_get ((unsigned char *) pnote->descdata, 4);
21040 end = byte_get ((unsigned char *) pnote->descdata + 4, 4);
21041 break;
21042
21043 case 16:
21044 start = byte_get ((unsigned char *) pnote->descdata, 8);
21045 end = byte_get ((unsigned char *) pnote->descdata + 8, 8);
21046 break;
21047
21048 default:
21049 error (_(" <invalid description size: %lx>\n"), pnote->descsz);
21050 printf (_(" <invalid descsz>"));
21051 return false;
21052 }
21053
21054 name = NULL;
21055 sym = get_symbol_for_build_attribute (filedata, start, is_open_attr, & name);
21056 /* As of version 5 of the annobin plugin, filename symbols are biased by 2
21057 in order to avoid them being confused with the start address of the
21058 first function in the file... */
21059 if (sym == NULL && is_open_attr)
21060 sym = get_symbol_for_build_attribute (filedata, start + 2, is_open_attr,
21061 & name);
21062
21063 if (end == 0 && sym != NULL && sym->st_size > 0)
21064 end = start + sym->st_size;
21065
21066 if (is_open_attr)
21067 {
21068 /* FIXME: Need to properly allow for section alignment.
21069 16 is just the alignment used on x86_64. */
21070 if (global_end > 0
21071 && start > BFD_ALIGN (global_end, 16)
21072 /* Build notes are not guaranteed to be organised in order of
21073 increasing address, but we should find the all of the notes
21074 for one section in the same place. */
21075 && same_section (filedata, start, global_end))
21076 warn (_("Gap in build notes detected from %#lx to %#lx\n"),
21077 global_end + 1, start - 1);
21078
21079 printf (_(" Applies to region from %#lx"), start);
21080 global_offset = start;
21081
21082 if (end)
21083 {
21084 printf (_(" to %#lx"), end);
21085 global_end = end;
21086 }
21087 }
21088 else
21089 {
21090 printf (_(" Applies to region from %#lx"), start);
21091 func_offset = start;
21092
21093 if (end)
21094 {
21095 printf (_(" to %#lx"), end);
21096 func_end = end;
21097 }
21098 }
21099
21100 if (sym && name)
21101 printf (_(" (%s)"), name);
21102
21103 printf ("\n");
21104 return true;
21105 }
21106
21107 static bool
21108 print_gnu_build_attribute_name (Elf_Internal_Note * pnote)
21109 {
21110 static const char string_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_STRING, 0 };
21111 static const char number_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC, 0 };
21112 static const char bool_expected [3] = { GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE, GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE, 0 };
21113 char name_type;
21114 char name_attribute;
21115 const char * expected_types;
21116 const char * name = pnote->namedata;
21117 const char * text;
21118 signed int left;
21119
21120 if (name == NULL || pnote->namesz < 2)
21121 {
21122 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
21123 print_symbol (-20, _(" <corrupt name>"));
21124 return false;
21125 }
21126
21127 if (do_wide)
21128 left = 28;
21129 else
21130 left = 20;
21131
21132 /* Version 2 of the spec adds a "GA" prefix to the name field. */
21133 if (name[0] == 'G' && name[1] == 'A')
21134 {
21135 if (pnote->namesz < 4)
21136 {
21137 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
21138 print_symbol (-20, _(" <corrupt name>"));
21139 return false;
21140 }
21141
21142 printf ("GA");
21143 name += 2;
21144 left -= 2;
21145 }
21146
21147 switch ((name_type = * name))
21148 {
21149 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
21150 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
21151 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
21152 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
21153 printf ("%c", * name);
21154 left --;
21155 break;
21156 default:
21157 error (_("unrecognised attribute type in name field: %d\n"), name_type);
21158 print_symbol (-20, _("<unknown name type>"));
21159 return false;
21160 }
21161
21162 ++ name;
21163 text = NULL;
21164
21165 switch ((name_attribute = * name))
21166 {
21167 case GNU_BUILD_ATTRIBUTE_VERSION:
21168 text = _("<version>");
21169 expected_types = string_expected;
21170 ++ name;
21171 break;
21172 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
21173 text = _("<stack prot>");
21174 expected_types = "!+*";
21175 ++ name;
21176 break;
21177 case GNU_BUILD_ATTRIBUTE_RELRO:
21178 text = _("<relro>");
21179 expected_types = bool_expected;
21180 ++ name;
21181 break;
21182 case GNU_BUILD_ATTRIBUTE_STACK_SIZE:
21183 text = _("<stack size>");
21184 expected_types = number_expected;
21185 ++ name;
21186 break;
21187 case GNU_BUILD_ATTRIBUTE_TOOL:
21188 text = _("<tool>");
21189 expected_types = string_expected;
21190 ++ name;
21191 break;
21192 case GNU_BUILD_ATTRIBUTE_ABI:
21193 text = _("<ABI>");
21194 expected_types = "$*";
21195 ++ name;
21196 break;
21197 case GNU_BUILD_ATTRIBUTE_PIC:
21198 text = _("<PIC>");
21199 expected_types = number_expected;
21200 ++ name;
21201 break;
21202 case GNU_BUILD_ATTRIBUTE_SHORT_ENUM:
21203 text = _("<short enum>");
21204 expected_types = bool_expected;
21205 ++ name;
21206 break;
21207 default:
21208 if (ISPRINT (* name))
21209 {
21210 int len = strnlen (name, pnote->namesz - (name - pnote->namedata)) + 1;
21211
21212 if (len > left && ! do_wide)
21213 len = left;
21214 printf ("%.*s:", len, name);
21215 left -= len;
21216 name += len;
21217 }
21218 else
21219 {
21220 static char tmpbuf [128];
21221
21222 error (_("unrecognised byte in name field: %d\n"), * name);
21223 sprintf (tmpbuf, _("<unknown:_%d>"), * name);
21224 text = tmpbuf;
21225 name ++;
21226 }
21227 expected_types = "*$!+";
21228 break;
21229 }
21230
21231 if (text)
21232 left -= printf ("%s", text);
21233
21234 if (strchr (expected_types, name_type) == NULL)
21235 warn (_("attribute does not have an expected type (%c)\n"), name_type);
21236
21237 if ((unsigned long)(name - pnote->namedata) > pnote->namesz)
21238 {
21239 error (_("corrupt name field: namesz: %lu but parsing gets to %ld\n"),
21240 (unsigned long) pnote->namesz,
21241 (long) (name - pnote->namedata));
21242 return false;
21243 }
21244
21245 if (left < 1 && ! do_wide)
21246 return true;
21247
21248 switch (name_type)
21249 {
21250 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
21251 {
21252 unsigned int bytes;
21253 unsigned long long val = 0;
21254 unsigned int shift = 0;
21255 char * decoded = NULL;
21256
21257 bytes = pnote->namesz - (name - pnote->namedata);
21258 if (bytes > 0)
21259 /* The -1 is because the name field is always 0 terminated, and we
21260 want to be able to ensure that the shift in the while loop below
21261 will not overflow. */
21262 -- bytes;
21263
21264 if (bytes > sizeof (val))
21265 {
21266 error (_("corrupt numeric name field: too many bytes in the value: %x\n"),
21267 bytes);
21268 bytes = sizeof (val);
21269 }
21270 /* We do not bother to warn if bytes == 0 as this can
21271 happen with some early versions of the gcc plugin. */
21272
21273 while (bytes --)
21274 {
21275 unsigned long long byte = *name++ & 0xff;
21276
21277 val |= byte << shift;
21278 shift += 8;
21279 }
21280
21281 switch (name_attribute)
21282 {
21283 case GNU_BUILD_ATTRIBUTE_PIC:
21284 switch (val)
21285 {
21286 case 0: decoded = "static"; break;
21287 case 1: decoded = "pic"; break;
21288 case 2: decoded = "PIC"; break;
21289 case 3: decoded = "pie"; break;
21290 case 4: decoded = "PIE"; break;
21291 default: break;
21292 }
21293 break;
21294 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
21295 switch (val)
21296 {
21297 /* Based upon the SPCT_FLAG_xxx enum values in gcc/cfgexpand.c. */
21298 case 0: decoded = "off"; break;
21299 case 1: decoded = "on"; break;
21300 case 2: decoded = "all"; break;
21301 case 3: decoded = "strong"; break;
21302 case 4: decoded = "explicit"; break;
21303 default: break;
21304 }
21305 break;
21306 default:
21307 break;
21308 }
21309
21310 if (decoded != NULL)
21311 {
21312 print_symbol (-left, decoded);
21313 left = 0;
21314 }
21315 else if (val == 0)
21316 {
21317 printf ("0x0");
21318 left -= 3;
21319 }
21320 else
21321 {
21322 if (do_wide)
21323 left -= printf ("0x%llx", val);
21324 else
21325 left -= printf ("0x%-.*llx", left, val);
21326 }
21327 }
21328 break;
21329 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
21330 left -= print_symbol (- left, name);
21331 break;
21332 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
21333 left -= print_symbol (- left, "true");
21334 break;
21335 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
21336 left -= print_symbol (- left, "false");
21337 break;
21338 }
21339
21340 if (do_wide && left > 0)
21341 printf ("%-*s", left, " ");
21342
21343 return true;
21344 }
21345
21346 /* Print the contents of PNOTE as hex. */
21347
21348 static void
21349 print_note_contents_hex (Elf_Internal_Note *pnote)
21350 {
21351 if (pnote->descsz)
21352 {
21353 unsigned long i;
21354
21355 printf (_(" description data: "));
21356 for (i = 0; i < pnote->descsz; i++)
21357 printf ("%02x ", pnote->descdata[i] & 0xff);
21358 if (!do_wide)
21359 printf ("\n");
21360 }
21361
21362 if (do_wide)
21363 printf ("\n");
21364 }
21365
21366 #if defined HAVE_MSGPACK
21367
21368 static void
21369 print_indents (int n)
21370 {
21371 printf (" ");
21372
21373 for (int i = 0; i < n; i++)
21374 printf (" ");
21375 }
21376
21377 /* Print OBJ in human-readable form. */
21378
21379 static void
21380 dump_msgpack_obj (const msgpack_object *obj, int indent)
21381 {
21382 switch (obj->type)
21383 {
21384 case MSGPACK_OBJECT_NIL:
21385 printf ("(nil)");
21386 break;
21387
21388 case MSGPACK_OBJECT_BOOLEAN:
21389 printf ("%s", obj->via.boolean ? "true" : "false");
21390 break;
21391
21392 case MSGPACK_OBJECT_POSITIVE_INTEGER:
21393 printf ("%" PRIu64, obj->via.u64);
21394 break;
21395
21396 case MSGPACK_OBJECT_NEGATIVE_INTEGER:
21397 printf ("%" PRIi64, obj->via.i64);
21398 break;
21399
21400 case MSGPACK_OBJECT_FLOAT32:
21401 case MSGPACK_OBJECT_FLOAT64:
21402 printf ("%f", obj->via.f64);
21403 break;
21404
21405 case MSGPACK_OBJECT_STR:
21406 printf ("\"%.*s\"", obj->via.str.size, obj->via.str.ptr);
21407 break;
21408
21409 case MSGPACK_OBJECT_ARRAY:
21410 {
21411 const msgpack_object_array *array = &obj->via.array;
21412
21413 printf ("[\n");
21414 ++indent;
21415
21416 for (uint32_t i = 0; i < array->size; ++i)
21417 {
21418 const msgpack_object *item = &array->ptr[i];
21419
21420 print_indents (indent);
21421 dump_msgpack_obj (item, indent);
21422 printf (",\n");
21423 }
21424
21425 --indent;
21426 print_indents (indent);
21427 printf ("]");
21428 break;
21429 }
21430 break;
21431
21432 case MSGPACK_OBJECT_MAP:
21433 {
21434 const msgpack_object_map *map = &obj->via.map;
21435
21436 printf ("{\n");
21437 ++indent;
21438
21439 for (uint32_t i = 0; i < map->size; ++i)
21440 {
21441 const msgpack_object_kv *kv = &map->ptr[i];
21442 const msgpack_object *key = &kv->key;
21443 const msgpack_object *val = &kv->val;
21444
21445 print_indents (indent);
21446 dump_msgpack_obj (key, indent);
21447 printf (": ");
21448 dump_msgpack_obj (val, indent);
21449
21450 printf (",\n");
21451 }
21452
21453 --indent;
21454 print_indents (indent);
21455 printf ("}");
21456
21457 break;
21458 }
21459
21460 case MSGPACK_OBJECT_BIN:
21461 printf ("(bin)");
21462 break;
21463
21464 case MSGPACK_OBJECT_EXT:
21465 printf ("(ext)");
21466 break;
21467 }
21468 }
21469
21470 static void
21471 dump_msgpack (const msgpack_unpacked *msg)
21472 {
21473 print_indents (0);
21474 dump_msgpack_obj (&msg->data, 0);
21475 printf ("\n");
21476 }
21477
21478 #endif /* defined HAVE_MSGPACK */
21479
21480 static bool
21481 print_amdgpu_note (Elf_Internal_Note *pnote)
21482 {
21483 #if defined HAVE_MSGPACK
21484 /* If msgpack is available, decode and dump the note's content. */
21485 bool ret;
21486 msgpack_unpacked msg;
21487 msgpack_unpack_return msgpack_ret;
21488
21489 assert (pnote->type == NT_AMDGPU_METADATA);
21490
21491 msgpack_unpacked_init (&msg);
21492 msgpack_ret = msgpack_unpack_next (&msg, pnote->descdata, pnote->descsz,
21493 NULL);
21494
21495 switch (msgpack_ret)
21496 {
21497 case MSGPACK_UNPACK_SUCCESS:
21498 dump_msgpack (&msg);
21499 ret = true;
21500 break;
21501
21502 default:
21503 error (_("failed to unpack msgpack contents in NT_AMDGPU_METADATA note"));
21504 ret = false;
21505 break;
21506 }
21507
21508 msgpack_unpacked_destroy (&msg);
21509 return ret;
21510 #else
21511 /* msgpack is not available, dump contents as hex. */
21512 print_note_contents_hex (pnote);
21513 return true;
21514 #endif
21515 }
21516
21517 /* Note that by the ELF standard, the name field is already null byte
21518 terminated, and namesz includes the terminating null byte.
21519 I.E. the value of namesz for the name "FSF" is 4.
21520
21521 If the value of namesz is zero, there is no name present. */
21522
21523 static bool
21524 process_note (Elf_Internal_Note * pnote,
21525 Filedata * filedata)
21526 {
21527 const char * name = pnote->namesz ? pnote->namedata : "(NONE)";
21528 const char * nt;
21529
21530 if (pnote->namesz == 0)
21531 /* If there is no note name, then use the default set of
21532 note type strings. */
21533 nt = get_note_type (filedata, pnote->type);
21534
21535 else if (startswith (pnote->namedata, "GNU"))
21536 /* GNU-specific object file notes. */
21537 nt = get_gnu_elf_note_type (pnote->type);
21538
21539 else if (startswith (pnote->namedata, "AMDGPU"))
21540 /* AMDGPU-specific object file notes. */
21541 nt = get_amdgpu_elf_note_type (pnote->type);
21542
21543 else if (startswith (pnote->namedata, "FreeBSD"))
21544 /* FreeBSD-specific core file notes. */
21545 nt = get_freebsd_elfcore_note_type (filedata, pnote->type);
21546
21547 else if (startswith (pnote->namedata, "NetBSD-CORE"))
21548 /* NetBSD-specific core file notes. */
21549 nt = get_netbsd_elfcore_note_type (filedata, pnote->type);
21550
21551 else if (startswith (pnote->namedata, "NetBSD"))
21552 /* NetBSD-specific core file notes. */
21553 return process_netbsd_elf_note (pnote);
21554
21555 else if (startswith (pnote->namedata, "PaX"))
21556 /* NetBSD-specific core file notes. */
21557 return process_netbsd_elf_note (pnote);
21558
21559 else if (startswith (pnote->namedata, "OpenBSD"))
21560 /* OpenBSD-specific core file notes. */
21561 nt = get_openbsd_elfcore_note_type (filedata, pnote->type);
21562
21563 else if (startswith (pnote->namedata, "SPU/"))
21564 {
21565 /* SPU-specific core file notes. */
21566 nt = pnote->namedata + 4;
21567 name = "SPU";
21568 }
21569
21570 else if (startswith (pnote->namedata, "IPF/VMS"))
21571 /* VMS/ia64-specific file notes. */
21572 nt = get_ia64_vms_note_type (pnote->type);
21573
21574 else if (startswith (pnote->namedata, "stapsdt"))
21575 nt = get_stapsdt_note_type (pnote->type);
21576
21577 else
21578 /* Don't recognize this note name; just use the default set of
21579 note type strings. */
21580 nt = get_note_type (filedata, pnote->type);
21581
21582 printf (" ");
21583
21584 if (((startswith (pnote->namedata, "GA")
21585 && strchr ("*$!+", pnote->namedata[2]) != NULL)
21586 || strchr ("*$!+", pnote->namedata[0]) != NULL)
21587 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
21588 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
21589 print_gnu_build_attribute_name (pnote);
21590 else
21591 print_symbol (-20, name);
21592
21593 if (do_wide)
21594 printf (" 0x%08lx\t%s\t", pnote->descsz, nt);
21595 else
21596 printf (" 0x%08lx\t%s\n", pnote->descsz, nt);
21597
21598 if (startswith (pnote->namedata, "IPF/VMS"))
21599 return print_ia64_vms_note (pnote);
21600 else if (startswith (pnote->namedata, "GNU"))
21601 return print_gnu_note (filedata, pnote);
21602 else if (startswith (pnote->namedata, "stapsdt"))
21603 return print_stapsdt_note (pnote);
21604 else if (startswith (pnote->namedata, "CORE"))
21605 return print_core_note (pnote);
21606 else if (startswith (pnote->namedata, "FDO"))
21607 return print_fdo_note (pnote);
21608 else if (((startswith (pnote->namedata, "GA")
21609 && strchr ("*$!+", pnote->namedata[2]) != NULL)
21610 || strchr ("*$!+", pnote->namedata[0]) != NULL)
21611 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
21612 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
21613 return print_gnu_build_attribute_description (pnote, filedata);
21614 else if (startswith (pnote->namedata, "AMDGPU")
21615 && pnote->type == NT_AMDGPU_METADATA)
21616 return print_amdgpu_note (pnote);
21617
21618 print_note_contents_hex (pnote);
21619 return true;
21620 }
21621
21622 static bool
21623 process_notes_at (Filedata * filedata,
21624 Elf_Internal_Shdr * section,
21625 uint64_t offset,
21626 uint64_t length,
21627 uint64_t align)
21628 {
21629 Elf_External_Note *pnotes;
21630 Elf_External_Note *external;
21631 char *end;
21632 bool res = true;
21633
21634 if (length <= 0)
21635 return false;
21636
21637 if (section)
21638 {
21639 pnotes = (Elf_External_Note *) get_section_contents (section, filedata);
21640 if (pnotes)
21641 {
21642 if (! apply_relocations (filedata, section, (unsigned char *) pnotes, length, NULL, NULL))
21643 {
21644 free (pnotes);
21645 return false;
21646 }
21647 }
21648 }
21649 else
21650 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
21651 _("notes"));
21652
21653 if (pnotes == NULL)
21654 return false;
21655
21656 external = pnotes;
21657
21658 if (filedata->is_separate)
21659 printf (_("In linked file '%s': "), filedata->file_name);
21660 else
21661 printf ("\n");
21662 if (section)
21663 printf (_("Displaying notes found in: %s\n"), printable_section_name (filedata, section));
21664 else
21665 printf (_("Displaying notes found at file offset 0x%08lx with length 0x%08lx:\n"),
21666 (unsigned long) offset, (unsigned long) length);
21667
21668 /* NB: Some note sections may have alignment value of 0 or 1. gABI
21669 specifies that notes should be aligned to 4 bytes in 32-bit
21670 objects and to 8 bytes in 64-bit objects. As a Linux extension,
21671 we also support 4 byte alignment in 64-bit objects. If section
21672 alignment is less than 4, we treate alignment as 4 bytes. */
21673 if (align < 4)
21674 align = 4;
21675 else if (align != 4 && align != 8)
21676 {
21677 warn (_("Corrupt note: alignment %ld, expecting 4 or 8\n"),
21678 (long) align);
21679 free (pnotes);
21680 return false;
21681 }
21682
21683 printf (_(" %-20s %-10s\tDescription\n"), _("Owner"), _("Data size"));
21684
21685 end = (char *) pnotes + length;
21686 while ((char *) external < end)
21687 {
21688 Elf_Internal_Note inote;
21689 size_t min_notesz;
21690 char * next;
21691 char * temp = NULL;
21692 size_t data_remaining = end - (char *) external;
21693
21694 if (!is_ia64_vms (filedata))
21695 {
21696 /* PR binutils/15191
21697 Make sure that there is enough data to read. */
21698 min_notesz = offsetof (Elf_External_Note, name);
21699 if (data_remaining < min_notesz)
21700 {
21701 warn (ngettext ("Corrupt note: only %ld byte remains, "
21702 "not enough for a full note\n",
21703 "Corrupt note: only %ld bytes remain, "
21704 "not enough for a full note\n",
21705 data_remaining),
21706 (long) data_remaining);
21707 break;
21708 }
21709 data_remaining -= min_notesz;
21710
21711 inote.type = BYTE_GET (external->type);
21712 inote.namesz = BYTE_GET (external->namesz);
21713 inote.namedata = external->name;
21714 inote.descsz = BYTE_GET (external->descsz);
21715 inote.descdata = ((char *) external
21716 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
21717 inote.descpos = offset + (inote.descdata - (char *) pnotes);
21718 next = ((char *) external
21719 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
21720 }
21721 else
21722 {
21723 Elf64_External_VMS_Note *vms_external;
21724
21725 /* PR binutils/15191
21726 Make sure that there is enough data to read. */
21727 min_notesz = offsetof (Elf64_External_VMS_Note, name);
21728 if (data_remaining < min_notesz)
21729 {
21730 warn (ngettext ("Corrupt note: only %ld byte remains, "
21731 "not enough for a full note\n",
21732 "Corrupt note: only %ld bytes remain, "
21733 "not enough for a full note\n",
21734 data_remaining),
21735 (long) data_remaining);
21736 break;
21737 }
21738 data_remaining -= min_notesz;
21739
21740 vms_external = (Elf64_External_VMS_Note *) external;
21741 inote.type = BYTE_GET (vms_external->type);
21742 inote.namesz = BYTE_GET (vms_external->namesz);
21743 inote.namedata = vms_external->name;
21744 inote.descsz = BYTE_GET (vms_external->descsz);
21745 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
21746 inote.descpos = offset + (inote.descdata - (char *) pnotes);
21747 next = inote.descdata + align_power (inote.descsz, 3);
21748 }
21749
21750 /* PR 17531: file: 3443835e. */
21751 /* PR 17531: file: id:000000,sig:11,src:006986,op:havoc,rep:4. */
21752 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
21753 || (size_t) (inote.descdata - inote.namedata) > data_remaining
21754 || (size_t) (next - inote.descdata) < inote.descsz
21755 || ((size_t) (next - inote.descdata)
21756 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
21757 {
21758 warn (_("note with invalid namesz and/or descsz found at offset 0x%lx\n"),
21759 (unsigned long) ((char *) external - (char *) pnotes));
21760 warn (_(" type: 0x%lx, namesize: 0x%08lx, descsize: 0x%08lx, alignment: %u\n"),
21761 inote.type, inote.namesz, inote.descsz, (int) align);
21762 break;
21763 }
21764
21765 external = (Elf_External_Note *) next;
21766
21767 /* Verify that name is null terminated. It appears that at least
21768 one version of Linux (RedHat 6.0) generates corefiles that don't
21769 comply with the ELF spec by failing to include the null byte in
21770 namesz. */
21771 if (inote.namesz > 0 && inote.namedata[inote.namesz - 1] != '\0')
21772 {
21773 if ((size_t) (inote.descdata - inote.namedata) == inote.namesz)
21774 {
21775 temp = (char *) malloc (inote.namesz + 1);
21776 if (temp == NULL)
21777 {
21778 error (_("Out of memory allocating space for inote name\n"));
21779 res = false;
21780 break;
21781 }
21782
21783 memcpy (temp, inote.namedata, inote.namesz);
21784 inote.namedata = temp;
21785 }
21786 inote.namedata[inote.namesz] = 0;
21787 }
21788
21789 if (! process_note (& inote, filedata))
21790 res = false;
21791
21792 free (temp);
21793 temp = NULL;
21794 }
21795
21796 free (pnotes);
21797
21798 return res;
21799 }
21800
21801 static bool
21802 process_corefile_note_segments (Filedata * filedata)
21803 {
21804 Elf_Internal_Phdr *segment;
21805 unsigned int i;
21806 bool res = true;
21807
21808 if (! get_program_headers (filedata))
21809 return true;
21810
21811 for (i = 0, segment = filedata->program_headers;
21812 i < filedata->file_header.e_phnum;
21813 i++, segment++)
21814 {
21815 if (segment->p_type == PT_NOTE)
21816 if (! process_notes_at (filedata, NULL, segment->p_offset,
21817 segment->p_filesz, segment->p_align))
21818 res = false;
21819 }
21820
21821 return res;
21822 }
21823
21824 static bool
21825 process_v850_notes (Filedata * filedata, uint64_t offset, uint64_t length)
21826 {
21827 Elf_External_Note * pnotes;
21828 Elf_External_Note * external;
21829 char * end;
21830 bool res = true;
21831
21832 if (length <= 0)
21833 return false;
21834
21835 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
21836 _("v850 notes"));
21837 if (pnotes == NULL)
21838 return false;
21839
21840 external = pnotes;
21841 end = (char*) pnotes + length;
21842
21843 printf (_("\nDisplaying contents of Renesas V850 notes section at offset 0x%lx with length 0x%lx:\n"),
21844 (unsigned long) offset, (unsigned long) length);
21845
21846 while ((char *) external + sizeof (Elf_External_Note) < end)
21847 {
21848 Elf_External_Note * next;
21849 Elf_Internal_Note inote;
21850
21851 inote.type = BYTE_GET (external->type);
21852 inote.namesz = BYTE_GET (external->namesz);
21853 inote.namedata = external->name;
21854 inote.descsz = BYTE_GET (external->descsz);
21855 inote.descdata = inote.namedata + align_power (inote.namesz, 2);
21856 inote.descpos = offset + (inote.descdata - (char *) pnotes);
21857
21858 if (inote.descdata < (char *) pnotes || inote.descdata >= end)
21859 {
21860 warn (_("Corrupt note: name size is too big: %lx\n"), inote.namesz);
21861 inote.descdata = inote.namedata;
21862 inote.namesz = 0;
21863 }
21864
21865 next = (Elf_External_Note *) (inote.descdata + align_power (inote.descsz, 2));
21866
21867 if ( ((char *) next > end)
21868 || ((char *) next < (char *) pnotes))
21869 {
21870 warn (_("corrupt descsz found in note at offset 0x%lx\n"),
21871 (unsigned long) ((char *) external - (char *) pnotes));
21872 warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
21873 inote.type, inote.namesz, inote.descsz);
21874 break;
21875 }
21876
21877 external = next;
21878
21879 /* Prevent out-of-bounds indexing. */
21880 if ( inote.namedata + inote.namesz > end
21881 || inote.namedata + inote.namesz < inote.namedata)
21882 {
21883 warn (_("corrupt namesz found in note at offset 0x%lx\n"),
21884 (unsigned long) ((char *) external - (char *) pnotes));
21885 warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
21886 inote.type, inote.namesz, inote.descsz);
21887 break;
21888 }
21889
21890 printf (" %s: ", get_v850_elf_note_type (inote.type));
21891
21892 if (! print_v850_note (& inote))
21893 {
21894 res = false;
21895 printf ("<corrupt sizes: namesz: %lx, descsz: %lx>\n",
21896 inote.namesz, inote.descsz);
21897 }
21898 }
21899
21900 free (pnotes);
21901
21902 return res;
21903 }
21904
21905 static bool
21906 process_note_sections (Filedata * filedata)
21907 {
21908 Elf_Internal_Shdr *section;
21909 unsigned long i;
21910 unsigned int n = 0;
21911 bool res = true;
21912
21913 for (i = 0, section = filedata->section_headers;
21914 i < filedata->file_header.e_shnum && section != NULL;
21915 i++, section++)
21916 {
21917 if (section->sh_type == SHT_NOTE)
21918 {
21919 if (! process_notes_at (filedata, section, section->sh_offset,
21920 section->sh_size, section->sh_addralign))
21921 res = false;
21922 n++;
21923 }
21924
21925 if (( filedata->file_header.e_machine == EM_V800
21926 || filedata->file_header.e_machine == EM_V850
21927 || filedata->file_header.e_machine == EM_CYGNUS_V850)
21928 && section->sh_type == SHT_RENESAS_INFO)
21929 {
21930 if (! process_v850_notes (filedata, section->sh_offset,
21931 section->sh_size))
21932 res = false;
21933 n++;
21934 }
21935 }
21936
21937 if (n == 0)
21938 /* Try processing NOTE segments instead. */
21939 return process_corefile_note_segments (filedata);
21940
21941 return res;
21942 }
21943
21944 static bool
21945 process_notes (Filedata * filedata)
21946 {
21947 /* If we have not been asked to display the notes then do nothing. */
21948 if (! do_notes)
21949 return true;
21950
21951 if (filedata->file_header.e_type != ET_CORE)
21952 return process_note_sections (filedata);
21953
21954 /* No program headers means no NOTE segment. */
21955 if (filedata->file_header.e_phnum > 0)
21956 return process_corefile_note_segments (filedata);
21957
21958 if (filedata->is_separate)
21959 printf (_("No notes found in linked file '%s'.\n"),
21960 filedata->file_name);
21961 else
21962 printf (_("No notes found file.\n"));
21963
21964 return true;
21965 }
21966
21967 static unsigned char *
21968 display_public_gnu_attributes (unsigned char * start,
21969 const unsigned char * const end)
21970 {
21971 printf (_(" Unknown GNU attribute: %s\n"), start);
21972
21973 start += strnlen ((char *) start, end - start);
21974 display_raw_attribute (start, end);
21975
21976 return (unsigned char *) end;
21977 }
21978
21979 static unsigned char *
21980 display_generic_attribute (unsigned char * start,
21981 unsigned int tag,
21982 const unsigned char * const end)
21983 {
21984 if (tag == 0)
21985 return (unsigned char *) end;
21986
21987 return display_tag_value (tag, start, end);
21988 }
21989
21990 static bool
21991 process_arch_specific (Filedata * filedata)
21992 {
21993 if (! do_arch)
21994 return true;
21995
21996 switch (filedata->file_header.e_machine)
21997 {
21998 case EM_ARC:
21999 case EM_ARC_COMPACT:
22000 case EM_ARC_COMPACT2:
22001 return process_attributes (filedata, "ARC", SHT_ARC_ATTRIBUTES,
22002 display_arc_attribute,
22003 display_generic_attribute);
22004 case EM_ARM:
22005 return process_attributes (filedata, "aeabi", SHT_ARM_ATTRIBUTES,
22006 display_arm_attribute,
22007 display_generic_attribute);
22008
22009 case EM_MIPS:
22010 case EM_MIPS_RS3_LE:
22011 return process_mips_specific (filedata);
22012
22013 case EM_MSP430:
22014 return process_attributes (filedata, "mspabi", SHT_MSP430_ATTRIBUTES,
22015 display_msp430_attribute,
22016 display_msp430_gnu_attribute);
22017
22018 case EM_RISCV:
22019 return process_attributes (filedata, "riscv", SHT_RISCV_ATTRIBUTES,
22020 display_riscv_attribute,
22021 display_generic_attribute);
22022
22023 case EM_NDS32:
22024 return process_nds32_specific (filedata);
22025
22026 case EM_68K:
22027 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22028 display_m68k_gnu_attribute);
22029
22030 case EM_PPC:
22031 case EM_PPC64:
22032 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22033 display_power_gnu_attribute);
22034
22035 case EM_S390:
22036 case EM_S390_OLD:
22037 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22038 display_s390_gnu_attribute);
22039
22040 case EM_SPARC:
22041 case EM_SPARC32PLUS:
22042 case EM_SPARCV9:
22043 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22044 display_sparc_gnu_attribute);
22045
22046 case EM_TI_C6000:
22047 return process_attributes (filedata, "c6xabi", SHT_C6000_ATTRIBUTES,
22048 display_tic6x_attribute,
22049 display_generic_attribute);
22050
22051 case EM_CSKY:
22052 return process_attributes (filedata, "csky", SHT_CSKY_ATTRIBUTES,
22053 display_csky_attribute, NULL);
22054
22055 default:
22056 return process_attributes (filedata, "gnu", SHT_GNU_ATTRIBUTES,
22057 display_public_gnu_attributes,
22058 display_generic_attribute);
22059 }
22060 }
22061
22062 static bool
22063 get_file_header (Filedata * filedata)
22064 {
22065 /* Read in the identity array. */
22066 if (fread (filedata->file_header.e_ident, EI_NIDENT, 1, filedata->handle) != 1)
22067 return false;
22068
22069 /* Determine how to read the rest of the header. */
22070 switch (filedata->file_header.e_ident[EI_DATA])
22071 {
22072 default:
22073 case ELFDATANONE:
22074 case ELFDATA2LSB:
22075 byte_get = byte_get_little_endian;
22076 byte_put = byte_put_little_endian;
22077 break;
22078 case ELFDATA2MSB:
22079 byte_get = byte_get_big_endian;
22080 byte_put = byte_put_big_endian;
22081 break;
22082 }
22083
22084 /* For now we only support 32 bit and 64 bit ELF files. */
22085 is_32bit_elf = (filedata->file_header.e_ident[EI_CLASS] != ELFCLASS64);
22086
22087 /* Read in the rest of the header. */
22088 if (is_32bit_elf)
22089 {
22090 Elf32_External_Ehdr ehdr32;
22091
22092 if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, filedata->handle) != 1)
22093 return false;
22094
22095 filedata->file_header.e_type = BYTE_GET (ehdr32.e_type);
22096 filedata->file_header.e_machine = BYTE_GET (ehdr32.e_machine);
22097 filedata->file_header.e_version = BYTE_GET (ehdr32.e_version);
22098 filedata->file_header.e_entry = BYTE_GET (ehdr32.e_entry);
22099 filedata->file_header.e_phoff = BYTE_GET (ehdr32.e_phoff);
22100 filedata->file_header.e_shoff = BYTE_GET (ehdr32.e_shoff);
22101 filedata->file_header.e_flags = BYTE_GET (ehdr32.e_flags);
22102 filedata->file_header.e_ehsize = BYTE_GET (ehdr32.e_ehsize);
22103 filedata->file_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
22104 filedata->file_header.e_phnum = BYTE_GET (ehdr32.e_phnum);
22105 filedata->file_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
22106 filedata->file_header.e_shnum = BYTE_GET (ehdr32.e_shnum);
22107 filedata->file_header.e_shstrndx = BYTE_GET (ehdr32.e_shstrndx);
22108 }
22109 else
22110 {
22111 Elf64_External_Ehdr ehdr64;
22112
22113 if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, filedata->handle) != 1)
22114 return false;
22115
22116 filedata->file_header.e_type = BYTE_GET (ehdr64.e_type);
22117 filedata->file_header.e_machine = BYTE_GET (ehdr64.e_machine);
22118 filedata->file_header.e_version = BYTE_GET (ehdr64.e_version);
22119 filedata->file_header.e_entry = BYTE_GET (ehdr64.e_entry);
22120 filedata->file_header.e_phoff = BYTE_GET (ehdr64.e_phoff);
22121 filedata->file_header.e_shoff = BYTE_GET (ehdr64.e_shoff);
22122 filedata->file_header.e_flags = BYTE_GET (ehdr64.e_flags);
22123 filedata->file_header.e_ehsize = BYTE_GET (ehdr64.e_ehsize);
22124 filedata->file_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
22125 filedata->file_header.e_phnum = BYTE_GET (ehdr64.e_phnum);
22126 filedata->file_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
22127 filedata->file_header.e_shnum = BYTE_GET (ehdr64.e_shnum);
22128 filedata->file_header.e_shstrndx = BYTE_GET (ehdr64.e_shstrndx);
22129 }
22130
22131 return true;
22132 }
22133
22134 static void
22135 free_filedata (Filedata *filedata)
22136 {
22137 free (filedata->program_interpreter);
22138 free (filedata->program_headers);
22139 free (filedata->section_headers);
22140 free (filedata->string_table);
22141 free (filedata->dump.dump_sects);
22142 free (filedata->dynamic_strings);
22143 free (filedata->dynamic_symbols);
22144 free (filedata->dynamic_syminfo);
22145 free (filedata->dynamic_section);
22146
22147 while (filedata->symtab_shndx_list != NULL)
22148 {
22149 elf_section_list *next = filedata->symtab_shndx_list->next;
22150 free (filedata->symtab_shndx_list);
22151 filedata->symtab_shndx_list = next;
22152 }
22153
22154 free (filedata->section_headers_groups);
22155
22156 if (filedata->section_groups)
22157 {
22158 size_t i;
22159 struct group_list * g;
22160 struct group_list * next;
22161
22162 for (i = 0; i < filedata->group_count; i++)
22163 {
22164 for (g = filedata->section_groups [i].root; g != NULL; g = next)
22165 {
22166 next = g->next;
22167 free (g);
22168 }
22169 }
22170
22171 free (filedata->section_groups);
22172 }
22173 memset (&filedata->section_headers, 0,
22174 sizeof (Filedata) - offsetof (Filedata, section_headers));
22175 }
22176
22177 static void
22178 close_file (Filedata * filedata)
22179 {
22180 if (filedata)
22181 {
22182 if (filedata->handle)
22183 fclose (filedata->handle);
22184 free (filedata);
22185 }
22186 }
22187
22188 void
22189 close_debug_file (void * data)
22190 {
22191 free_filedata ((Filedata *) data);
22192 close_file ((Filedata *) data);
22193 }
22194
22195 static Filedata *
22196 open_file (const char * pathname, bool is_separate)
22197 {
22198 struct stat statbuf;
22199 Filedata * filedata = NULL;
22200
22201 if (stat (pathname, & statbuf) < 0
22202 || ! S_ISREG (statbuf.st_mode))
22203 goto fail;
22204
22205 filedata = calloc (1, sizeof * filedata);
22206 if (filedata == NULL)
22207 goto fail;
22208
22209 filedata->handle = fopen (pathname, "rb");
22210 if (filedata->handle == NULL)
22211 goto fail;
22212
22213 filedata->file_size = statbuf.st_size;
22214 filedata->file_name = pathname;
22215 filedata->is_separate = is_separate;
22216
22217 if (! get_file_header (filedata))
22218 goto fail;
22219
22220 if (!get_section_headers (filedata, false))
22221 goto fail;
22222
22223 return filedata;
22224
22225 fail:
22226 if (filedata)
22227 {
22228 if (filedata->handle)
22229 fclose (filedata->handle);
22230 free (filedata);
22231 }
22232 return NULL;
22233 }
22234
22235 void *
22236 open_debug_file (const char * pathname)
22237 {
22238 return open_file (pathname, true);
22239 }
22240
22241 static void
22242 initialise_dump_sects (Filedata * filedata)
22243 {
22244 /* Initialise the dump_sects array from the cmdline_dump_sects array.
22245 Note we do this even if cmdline_dump_sects is empty because we
22246 must make sure that the dump_sets array is zeroed out before each
22247 object file is processed. */
22248 if (filedata->dump.num_dump_sects > cmdline.num_dump_sects)
22249 memset (filedata->dump.dump_sects, 0,
22250 filedata->dump.num_dump_sects * sizeof (*filedata->dump.dump_sects));
22251
22252 if (cmdline.num_dump_sects > 0)
22253 {
22254 if (filedata->dump.num_dump_sects == 0)
22255 /* A sneaky way of allocating the dump_sects array. */
22256 request_dump_bynumber (&filedata->dump, cmdline.num_dump_sects, 0);
22257
22258 assert (filedata->dump.num_dump_sects >= cmdline.num_dump_sects);
22259 memcpy (filedata->dump.dump_sects, cmdline.dump_sects,
22260 cmdline.num_dump_sects * sizeof (*filedata->dump.dump_sects));
22261 }
22262 }
22263
22264 static bool
22265 might_need_separate_debug_info (Filedata * filedata)
22266 {
22267 /* Debuginfo files do not need further separate file loading. */
22268 if (filedata->file_header.e_shstrndx == SHN_UNDEF)
22269 return false;
22270
22271 /* Since do_follow_links might be enabled by default, only treat it as an
22272 indication that separate files should be loaded if setting it was a
22273 deliberate user action. */
22274 if (DEFAULT_FOR_FOLLOW_LINKS == 0 && do_follow_links)
22275 return true;
22276
22277 if (process_links || do_syms || do_unwind
22278 || dump_any_debugging || do_dump || do_debugging)
22279 return true;
22280
22281 return false;
22282 }
22283
22284 /* Process one ELF object file according to the command line options.
22285 This file may actually be stored in an archive. The file is
22286 positioned at the start of the ELF object. Returns TRUE if no
22287 problems were encountered, FALSE otherwise. */
22288
22289 static bool
22290 process_object (Filedata * filedata)
22291 {
22292 bool have_separate_files;
22293 unsigned int i;
22294 bool res;
22295
22296 if (! get_file_header (filedata))
22297 {
22298 error (_("%s: Failed to read file header\n"), filedata->file_name);
22299 return false;
22300 }
22301
22302 /* Initialise per file variables. */
22303 for (i = ARRAY_SIZE (filedata->version_info); i--;)
22304 filedata->version_info[i] = 0;
22305
22306 for (i = ARRAY_SIZE (filedata->dynamic_info); i--;)
22307 filedata->dynamic_info[i] = 0;
22308 filedata->dynamic_info_DT_GNU_HASH = 0;
22309 filedata->dynamic_info_DT_MIPS_XHASH = 0;
22310
22311 /* Process the file. */
22312 if (show_name)
22313 printf (_("\nFile: %s\n"), filedata->file_name);
22314
22315 initialise_dump_sects (filedata);
22316
22317 /* There may be some extensions in the first section header. Don't
22318 bomb if we can't read it. */
22319 get_section_headers (filedata, true);
22320
22321 if (! process_file_header (filedata))
22322 {
22323 res = false;
22324 goto out;
22325 }
22326
22327 /* Throw away the single section header read above, so that we
22328 re-read the entire set. */
22329 free (filedata->section_headers);
22330 filedata->section_headers = NULL;
22331
22332 if (! process_section_headers (filedata))
22333 {
22334 /* Without loaded section headers we cannot process lots of things. */
22335 do_unwind = do_version = do_dump = do_arch = false;
22336
22337 if (! do_using_dynamic)
22338 do_syms = do_dyn_syms = do_reloc = false;
22339 }
22340
22341 if (! process_section_groups (filedata))
22342 /* Without loaded section groups we cannot process unwind. */
22343 do_unwind = false;
22344
22345 process_program_headers (filedata);
22346
22347 res = process_dynamic_section (filedata);
22348
22349 if (! process_relocs (filedata))
22350 res = false;
22351
22352 if (! process_unwind (filedata))
22353 res = false;
22354
22355 if (! process_symbol_table (filedata))
22356 res = false;
22357
22358 if (! process_lto_symbol_tables (filedata))
22359 res = false;
22360
22361 if (! process_syminfo (filedata))
22362 res = false;
22363
22364 if (! process_version_sections (filedata))
22365 res = false;
22366
22367 if (might_need_separate_debug_info (filedata))
22368 have_separate_files = load_separate_debug_files (filedata, filedata->file_name);
22369 else
22370 have_separate_files = false;
22371
22372 if (! process_section_contents (filedata))
22373 res = false;
22374
22375 if (have_separate_files)
22376 {
22377 separate_info * d;
22378
22379 for (d = first_separate_info; d != NULL; d = d->next)
22380 {
22381 initialise_dump_sects (d->handle);
22382
22383 if (process_links && ! process_file_header (d->handle))
22384 res = false;
22385 else if (! process_section_headers (d->handle))
22386 res = false;
22387 else if (! process_section_contents (d->handle))
22388 res = false;
22389 else if (process_links)
22390 {
22391 if (! process_section_groups (d->handle))
22392 res = false;
22393 process_program_headers (d->handle);
22394 if (! process_dynamic_section (d->handle))
22395 res = false;
22396 if (! process_relocs (d->handle))
22397 res = false;
22398 if (! process_unwind (d->handle))
22399 res = false;
22400 if (! process_symbol_table (d->handle))
22401 res = false;
22402 if (! process_lto_symbol_tables (d->handle))
22403 res = false;
22404 if (! process_syminfo (d->handle))
22405 res = false;
22406 if (! process_version_sections (d->handle))
22407 res = false;
22408 if (! process_notes (d->handle))
22409 res = false;
22410 }
22411 }
22412
22413 /* The file handles are closed by the call to free_debug_memory() below. */
22414 }
22415
22416 if (! process_notes (filedata))
22417 res = false;
22418
22419 if (! process_gnu_liblist (filedata))
22420 res = false;
22421
22422 if (! process_arch_specific (filedata))
22423 res = false;
22424
22425 out:
22426 free_filedata (filedata);
22427
22428 free_debug_memory ();
22429
22430 return res;
22431 }
22432
22433 /* Process an ELF archive.
22434 On entry the file is positioned just after the ARMAG string.
22435 Returns TRUE upon success, FALSE otherwise. */
22436
22437 static bool
22438 process_archive (Filedata * filedata, bool is_thin_archive)
22439 {
22440 struct archive_info arch;
22441 struct archive_info nested_arch;
22442 size_t got;
22443 bool ret = true;
22444
22445 show_name = true;
22446
22447 /* The ARCH structure is used to hold information about this archive. */
22448 arch.file_name = NULL;
22449 arch.file = NULL;
22450 arch.index_array = NULL;
22451 arch.sym_table = NULL;
22452 arch.longnames = NULL;
22453
22454 /* The NESTED_ARCH structure is used as a single-item cache of information
22455 about a nested archive (when members of a thin archive reside within
22456 another regular archive file). */
22457 nested_arch.file_name = NULL;
22458 nested_arch.file = NULL;
22459 nested_arch.index_array = NULL;
22460 nested_arch.sym_table = NULL;
22461 nested_arch.longnames = NULL;
22462
22463 if (setup_archive (&arch, filedata->file_name, filedata->handle,
22464 filedata->file_size, is_thin_archive,
22465 do_archive_index) != 0)
22466 {
22467 ret = false;
22468 goto out;
22469 }
22470
22471 if (do_archive_index)
22472 {
22473 if (arch.sym_table == NULL)
22474 error (_("%s: unable to dump the index as none was found\n"),
22475 filedata->file_name);
22476 else
22477 {
22478 unsigned long i, l;
22479 unsigned long current_pos;
22480
22481 printf (_("Index of archive %s: (%lu entries, 0x%lx bytes "
22482 "in the symbol table)\n"),
22483 filedata->file_name, (unsigned long) arch.index_num,
22484 arch.sym_size);
22485
22486 current_pos = ftell (filedata->handle);
22487
22488 for (i = l = 0; i < arch.index_num; i++)
22489 {
22490 if (i == 0
22491 || (i > 0 && arch.index_array[i] != arch.index_array[i - 1]))
22492 {
22493 char * member_name
22494 = get_archive_member_name_at (&arch, arch.index_array[i],
22495 &nested_arch);
22496
22497 if (member_name != NULL)
22498 {
22499 char * qualified_name
22500 = make_qualified_name (&arch, &nested_arch,
22501 member_name);
22502
22503 if (qualified_name != NULL)
22504 {
22505 printf (_("Contents of binary %s at offset "),
22506 qualified_name);
22507 (void) print_vma (arch.index_array[i], PREFIX_HEX);
22508 putchar ('\n');
22509 free (qualified_name);
22510 }
22511 free (member_name);
22512 }
22513 }
22514
22515 if (l >= arch.sym_size)
22516 {
22517 error (_("%s: end of the symbol table reached "
22518 "before the end of the index\n"),
22519 filedata->file_name);
22520 ret = false;
22521 break;
22522 }
22523 /* PR 17531: file: 0b6630b2. */
22524 printf ("\t%.*s\n",
22525 (int) (arch.sym_size - l), arch.sym_table + l);
22526 l += strnlen (arch.sym_table + l, arch.sym_size - l) + 1;
22527 }
22528
22529 if (arch.uses_64bit_indices)
22530 l = (l + 7) & ~ 7;
22531 else
22532 l += l & 1;
22533
22534 if (l < arch.sym_size)
22535 {
22536 error (ngettext ("%s: %ld byte remains in the symbol table, "
22537 "but without corresponding entries in "
22538 "the index table\n",
22539 "%s: %ld bytes remain in the symbol table, "
22540 "but without corresponding entries in "
22541 "the index table\n",
22542 arch.sym_size - l),
22543 filedata->file_name, arch.sym_size - l);
22544 ret = false;
22545 }
22546
22547 if (fseek (filedata->handle, current_pos, SEEK_SET) != 0)
22548 {
22549 error (_("%s: failed to seek back to start of object files "
22550 "in the archive\n"),
22551 filedata->file_name);
22552 ret = false;
22553 goto out;
22554 }
22555 }
22556
22557 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
22558 && !do_segments && !do_header && !do_dump && !do_version
22559 && !do_histogram && !do_debugging && !do_arch && !do_notes
22560 && !do_section_groups && !do_dyn_syms)
22561 {
22562 ret = true; /* Archive index only. */
22563 goto out;
22564 }
22565 }
22566
22567 while (1)
22568 {
22569 char * name;
22570 size_t namelen;
22571 char * qualified_name;
22572
22573 /* Read the next archive header. */
22574 if (fseek (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0)
22575 {
22576 error (_("%s: failed to seek to next archive header\n"),
22577 arch.file_name);
22578 ret = false;
22579 break;
22580 }
22581 got = fread (&arch.arhdr, 1, sizeof arch.arhdr, filedata->handle);
22582 if (got != sizeof arch.arhdr)
22583 {
22584 if (got == 0)
22585 break;
22586 /* PR 24049 - we cannot use filedata->file_name as this will
22587 have already been freed. */
22588 error (_("%s: failed to read archive header\n"), arch.file_name);
22589
22590 ret = false;
22591 break;
22592 }
22593 if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
22594 {
22595 error (_("%s: did not find a valid archive header\n"),
22596 arch.file_name);
22597 ret = false;
22598 break;
22599 }
22600
22601 arch.next_arhdr_offset += sizeof arch.arhdr;
22602
22603 filedata->archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
22604
22605 name = get_archive_member_name (&arch, &nested_arch);
22606 if (name == NULL)
22607 {
22608 error (_("%s: bad archive file name\n"), arch.file_name);
22609 ret = false;
22610 break;
22611 }
22612 namelen = strlen (name);
22613
22614 qualified_name = make_qualified_name (&arch, &nested_arch, name);
22615 if (qualified_name == NULL)
22616 {
22617 error (_("%s: bad archive file name\n"), arch.file_name);
22618 free (name);
22619 ret = false;
22620 break;
22621 }
22622
22623 if (is_thin_archive && arch.nested_member_origin == 0)
22624 {
22625 /* This is a proxy for an external member of a thin archive. */
22626 Filedata * member_filedata;
22627 char * member_file_name = adjust_relative_path
22628 (filedata->file_name, name, namelen);
22629
22630 free (name);
22631 if (member_file_name == NULL)
22632 {
22633 free (qualified_name);
22634 ret = false;
22635 break;
22636 }
22637
22638 member_filedata = open_file (member_file_name, false);
22639 if (member_filedata == NULL)
22640 {
22641 error (_("Input file '%s' is not readable.\n"), member_file_name);
22642 free (member_file_name);
22643 free (qualified_name);
22644 ret = false;
22645 break;
22646 }
22647
22648 filedata->archive_file_offset = arch.nested_member_origin;
22649 member_filedata->file_name = qualified_name;
22650
22651 /* The call to process_object() expects the file to be at the beginning. */
22652 rewind (member_filedata->handle);
22653
22654 if (! process_object (member_filedata))
22655 ret = false;
22656
22657 close_file (member_filedata);
22658 free (member_file_name);
22659 }
22660 else if (is_thin_archive)
22661 {
22662 Filedata thin_filedata;
22663
22664 memset (&thin_filedata, 0, sizeof (thin_filedata));
22665
22666 /* PR 15140: Allow for corrupt thin archives. */
22667 if (nested_arch.file == NULL)
22668 {
22669 error (_("%s: contains corrupt thin archive: %s\n"),
22670 qualified_name, name);
22671 free (qualified_name);
22672 free (name);
22673 ret = false;
22674 break;
22675 }
22676 free (name);
22677
22678 /* This is a proxy for a member of a nested archive. */
22679 filedata->archive_file_offset
22680 = arch.nested_member_origin + sizeof arch.arhdr;
22681
22682 /* The nested archive file will have been opened and setup by
22683 get_archive_member_name. */
22684 if (fseek (nested_arch.file, filedata->archive_file_offset,
22685 SEEK_SET) != 0)
22686 {
22687 error (_("%s: failed to seek to archive member.\n"),
22688 nested_arch.file_name);
22689 free (qualified_name);
22690 ret = false;
22691 break;
22692 }
22693
22694 thin_filedata.handle = nested_arch.file;
22695 thin_filedata.file_name = qualified_name;
22696
22697 if (! process_object (& thin_filedata))
22698 ret = false;
22699 }
22700 else
22701 {
22702 free (name);
22703 filedata->archive_file_offset = arch.next_arhdr_offset;
22704 filedata->file_name = qualified_name;
22705 if (! process_object (filedata))
22706 ret = false;
22707 arch.next_arhdr_offset += (filedata->archive_file_size + 1) & -2;
22708 /* Stop looping with "negative" archive_file_size. */
22709 if (arch.next_arhdr_offset < filedata->archive_file_size)
22710 arch.next_arhdr_offset = -1ul;
22711 }
22712
22713 free (qualified_name);
22714 }
22715
22716 out:
22717 if (nested_arch.file != NULL)
22718 fclose (nested_arch.file);
22719 release_archive (&nested_arch);
22720 release_archive (&arch);
22721
22722 return ret;
22723 }
22724
22725 static bool
22726 process_file (char * file_name)
22727 {
22728 Filedata * filedata = NULL;
22729 struct stat statbuf;
22730 char armag[SARMAG];
22731 bool ret = true;
22732
22733 if (stat (file_name, &statbuf) < 0)
22734 {
22735 if (errno == ENOENT)
22736 error (_("'%s': No such file\n"), file_name);
22737 else
22738 error (_("Could not locate '%s'. System error message: %s\n"),
22739 file_name, strerror (errno));
22740 return false;
22741 }
22742
22743 if (! S_ISREG (statbuf.st_mode))
22744 {
22745 error (_("'%s' is not an ordinary file\n"), file_name);
22746 return false;
22747 }
22748
22749 filedata = calloc (1, sizeof * filedata);
22750 if (filedata == NULL)
22751 {
22752 error (_("Out of memory allocating file data structure\n"));
22753 return false;
22754 }
22755
22756 filedata->file_name = file_name;
22757 filedata->handle = fopen (file_name, "rb");
22758 if (filedata->handle == NULL)
22759 {
22760 error (_("Input file '%s' is not readable.\n"), file_name);
22761 free (filedata);
22762 return false;
22763 }
22764
22765 if (fread (armag, SARMAG, 1, filedata->handle) != 1)
22766 {
22767 error (_("%s: Failed to read file's magic number\n"), file_name);
22768 fclose (filedata->handle);
22769 free (filedata);
22770 return false;
22771 }
22772
22773 filedata->file_size = statbuf.st_size;
22774 filedata->is_separate = false;
22775
22776 if (memcmp (armag, ARMAG, SARMAG) == 0)
22777 {
22778 if (! process_archive (filedata, false))
22779 ret = false;
22780 }
22781 else if (memcmp (armag, ARMAGT, SARMAG) == 0)
22782 {
22783 if ( ! process_archive (filedata, true))
22784 ret = false;
22785 }
22786 else
22787 {
22788 if (do_archive_index && !check_all)
22789 error (_("File %s is not an archive so its index cannot be displayed.\n"),
22790 file_name);
22791
22792 rewind (filedata->handle);
22793 filedata->archive_file_size = filedata->archive_file_offset = 0;
22794
22795 if (! process_object (filedata))
22796 ret = false;
22797 }
22798
22799 fclose (filedata->handle);
22800 free (filedata->section_headers);
22801 free (filedata->program_headers);
22802 free (filedata->string_table);
22803 free (filedata->dump.dump_sects);
22804 free (filedata);
22805
22806 free (ba_cache.strtab);
22807 ba_cache.strtab = NULL;
22808 free (ba_cache.symtab);
22809 ba_cache.symtab = NULL;
22810 ba_cache.filedata = NULL;
22811
22812 return ret;
22813 }
22814
22815 #ifdef SUPPORT_DISASSEMBLY
22816 /* Needed by the i386 disassembler. For extra credit, someone could
22817 fix this so that we insert symbolic addresses here, esp for GOT/PLT
22818 symbols. */
22819
22820 void
22821 print_address (unsigned int addr, FILE * outfile)
22822 {
22823 fprintf (outfile,"0x%8.8x", addr);
22824 }
22825
22826 /* Needed by the i386 disassembler. */
22827
22828 void
22829 db_task_printsym (unsigned int addr)
22830 {
22831 print_address (addr, stderr);
22832 }
22833 #endif
22834
22835 int
22836 main (int argc, char ** argv)
22837 {
22838 int err;
22839
22840 #ifdef HAVE_LC_MESSAGES
22841 setlocale (LC_MESSAGES, "");
22842 #endif
22843 setlocale (LC_CTYPE, "");
22844 bindtextdomain (PACKAGE, LOCALEDIR);
22845 textdomain (PACKAGE);
22846
22847 expandargv (&argc, &argv);
22848
22849 parse_args (& cmdline, argc, argv);
22850
22851 if (optind < (argc - 1))
22852 /* When displaying information for more than one file,
22853 prefix the information with the file name. */
22854 show_name = true;
22855 else if (optind >= argc)
22856 {
22857 /* Ensure that the warning is always displayed. */
22858 do_checks = true;
22859
22860 warn (_("Nothing to do.\n"));
22861 usage (stderr);
22862 }
22863
22864 err = false;
22865 while (optind < argc)
22866 if (! process_file (argv[optind++]))
22867 err = true;
22868
22869 free (cmdline.dump_sects);
22870
22871 free (dump_ctf_symtab_name);
22872 free (dump_ctf_strtab_name);
22873 free (dump_ctf_parent_name);
22874
22875 return err ? EXIT_FAILURE : EXIT_SUCCESS;
22876 }