PR29799 heap buffer overflow in display_gdb_index dwarf.c:10548
[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 "sframe-api.h"
67 #include "demangle.h"
68
69 #include "elf/common.h"
70 #include "elf/external.h"
71 #include "elf/internal.h"
72
73
74 /* Included here, before RELOC_MACROS_GEN_FUNC is defined, so that
75 we can obtain the H8 reloc numbers. We need these for the
76 get_reloc_size() function. We include h8.h again after defining
77 RELOC_MACROS_GEN_FUNC so that we get the naming function as well. */
78
79 #include "elf/h8.h"
80 #undef _ELF_H8_H
81
82 /* Undo the effects of #including reloc-macros.h. */
83
84 #undef START_RELOC_NUMBERS
85 #undef RELOC_NUMBER
86 #undef FAKE_RELOC
87 #undef EMPTY_RELOC
88 #undef END_RELOC_NUMBERS
89 #undef _RELOC_MACROS_H
90
91 /* The following headers use the elf/reloc-macros.h file to
92 automatically generate relocation recognition functions
93 such as elf_mips_reloc_type() */
94
95 #define RELOC_MACROS_GEN_FUNC
96
97 #include "elf/aarch64.h"
98 #include "elf/alpha.h"
99 #include "elf/amdgpu.h"
100 #include "elf/arc.h"
101 #include "elf/arm.h"
102 #include "elf/avr.h"
103 #include "elf/bfin.h"
104 #include "elf/cr16.h"
105 #include "elf/cris.h"
106 #include "elf/crx.h"
107 #include "elf/csky.h"
108 #include "elf/d10v.h"
109 #include "elf/d30v.h"
110 #include "elf/dlx.h"
111 #include "elf/bpf.h"
112 #include "elf/epiphany.h"
113 #include "elf/fr30.h"
114 #include "elf/frv.h"
115 #include "elf/ft32.h"
116 #include "elf/h8.h"
117 #include "elf/hppa.h"
118 #include "elf/i386.h"
119 #include "elf/i370.h"
120 #include "elf/i860.h"
121 #include "elf/i960.h"
122 #include "elf/ia64.h"
123 #include "elf/ip2k.h"
124 #include "elf/lm32.h"
125 #include "elf/iq2000.h"
126 #include "elf/m32c.h"
127 #include "elf/m32r.h"
128 #include "elf/m68k.h"
129 #include "elf/m68hc11.h"
130 #include "elf/s12z.h"
131 #include "elf/mcore.h"
132 #include "elf/mep.h"
133 #include "elf/metag.h"
134 #include "elf/microblaze.h"
135 #include "elf/mips.h"
136 #include "elf/mmix.h"
137 #include "elf/mn10200.h"
138 #include "elf/mn10300.h"
139 #include "elf/moxie.h"
140 #include "elf/mt.h"
141 #include "elf/msp430.h"
142 #include "elf/nds32.h"
143 #include "elf/nfp.h"
144 #include "elf/nios2.h"
145 #include "elf/or1k.h"
146 #include "elf/pj.h"
147 #include "elf/ppc.h"
148 #include "elf/ppc64.h"
149 #include "elf/pru.h"
150 #include "elf/riscv.h"
151 #include "elf/rl78.h"
152 #include "elf/rx.h"
153 #include "elf/s390.h"
154 #include "elf/score.h"
155 #include "elf/sh.h"
156 #include "elf/sparc.h"
157 #include "elf/spu.h"
158 #include "elf/tic6x.h"
159 #include "elf/tilegx.h"
160 #include "elf/tilepro.h"
161 #include "elf/v850.h"
162 #include "elf/vax.h"
163 #include "elf/visium.h"
164 #include "elf/wasm32.h"
165 #include "elf/x86-64.h"
166 #include "elf/xgate.h"
167 #include "elf/xstormy16.h"
168 #include "elf/xtensa.h"
169 #include "elf/z80.h"
170 #include "elf/loongarch.h"
171
172 #include "getopt.h"
173 #include "libiberty.h"
174 #include "safe-ctype.h"
175 #include "filenames.h"
176
177 #ifndef offsetof
178 #define offsetof(TYPE, MEMBER) ((size_t) &(((TYPE *) 0)->MEMBER))
179 #endif
180
181 typedef struct elf_section_list
182 {
183 Elf_Internal_Shdr * hdr;
184 struct elf_section_list * next;
185 } elf_section_list;
186
187 /* Flag bits indicating particular types of dump. */
188 #define HEX_DUMP (1 << 0) /* The -x command line switch. */
189 #define DISASS_DUMP (1 << 1) /* The -i command line switch. */
190 #define DEBUG_DUMP (1 << 2) /* The -w command line switch. */
191 #define STRING_DUMP (1 << 3) /* The -p command line switch. */
192 #define RELOC_DUMP (1 << 4) /* The -R command line switch. */
193 #define CTF_DUMP (1 << 5) /* The --ctf command line switch. */
194 #define SFRAME_DUMP (1 << 6) /* The --sframe command line switch. */
195
196 typedef unsigned char dump_type;
197
198 /* A linked list of the section names for which dumps were requested. */
199 struct dump_list_entry
200 {
201 char * name;
202 dump_type type;
203 struct dump_list_entry * next;
204 };
205
206 /* A dynamic array of flags indicating for which sections a dump
207 has been requested via command line switches. */
208 struct dump_data
209 {
210 dump_type * dump_sects;
211 unsigned int num_dump_sects;
212 };
213
214 static struct dump_data cmdline;
215
216 static struct dump_list_entry * dump_sects_byname;
217
218 char * program_name = "readelf";
219
220 static bool show_name = false;
221 static bool do_dynamic = false;
222 static bool do_syms = false;
223 static bool do_dyn_syms = false;
224 static bool do_lto_syms = false;
225 static bool do_reloc = false;
226 static bool do_sections = false;
227 static bool do_section_groups = false;
228 static bool do_section_details = false;
229 static bool do_segments = false;
230 static bool do_unwind = false;
231 static bool do_using_dynamic = false;
232 static bool do_header = false;
233 static bool do_dump = false;
234 static bool do_version = false;
235 static bool do_histogram = false;
236 static bool do_debugging = false;
237 static bool do_ctf = false;
238 static bool do_sframe = false;
239 static bool do_arch = false;
240 static bool do_notes = false;
241 static bool do_archive_index = false;
242 static bool check_all = false;
243 static bool is_32bit_elf = false;
244 static bool decompress_dumps = false;
245 static bool do_not_show_symbol_truncation = false;
246 static bool do_demangle = false; /* Pretty print C++ symbol names. */
247 static bool process_links = false;
248 static bool dump_any_debugging = false;
249 static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
250 static int sym_base = 0;
251
252 static char *dump_ctf_parent_name;
253 static char *dump_ctf_symtab_name;
254 static char *dump_ctf_strtab_name;
255
256 struct group_list
257 {
258 struct group_list * next;
259 unsigned int section_index;
260 };
261
262 struct group
263 {
264 struct group_list * root;
265 unsigned int group_index;
266 };
267
268 typedef struct filedata
269 {
270 const char * file_name;
271 bool is_separate;
272 FILE * handle;
273 uint64_t file_size;
274 Elf_Internal_Ehdr file_header;
275 unsigned long archive_file_offset;
276 unsigned long archive_file_size;
277 /* Everything below this point is cleared out by free_filedata. */
278 Elf_Internal_Shdr * section_headers;
279 Elf_Internal_Phdr * program_headers;
280 char * string_table;
281 unsigned long string_table_length;
282 unsigned long dynamic_addr;
283 uint64_t dynamic_size;
284 size_t dynamic_nent;
285 Elf_Internal_Dyn * dynamic_section;
286 Elf_Internal_Shdr * dynamic_strtab_section;
287 char * dynamic_strings;
288 unsigned long dynamic_strings_length;
289 Elf_Internal_Shdr * dynamic_symtab_section;
290 unsigned long num_dynamic_syms;
291 Elf_Internal_Sym * dynamic_symbols;
292 uint64_t version_info[16];
293 unsigned int dynamic_syminfo_nent;
294 Elf_Internal_Syminfo * dynamic_syminfo;
295 unsigned long dynamic_syminfo_offset;
296 uint64_t nbuckets;
297 uint64_t nchains;
298 uint64_t * buckets;
299 uint64_t * chains;
300 uint64_t ngnubuckets;
301 uint64_t ngnuchains;
302 uint64_t * gnubuckets;
303 uint64_t * gnuchains;
304 uint64_t * mipsxlat;
305 uint64_t gnusymidx;
306 char * program_interpreter;
307 uint64_t dynamic_info[DT_ENCODING];
308 uint64_t dynamic_info_DT_GNU_HASH;
309 uint64_t dynamic_info_DT_MIPS_XHASH;
310 elf_section_list * symtab_shndx_list;
311 size_t group_count;
312 struct group * section_groups;
313 struct group ** section_headers_groups;
314 /* A dynamic array of flags indicating for which sections a dump of
315 some kind has been requested. It is reset on a per-object file
316 basis and then initialised from the cmdline_dump_sects array,
317 the results of interpreting the -w switch, and the
318 dump_sects_byname list. */
319 struct dump_data dump;
320 } Filedata;
321
322 /* How to print a vma value. */
323 typedef enum print_mode
324 {
325 HEX,
326 HEX_5,
327 DEC,
328 DEC_5,
329 UNSIGNED,
330 UNSIGNED_5,
331 PREFIX_HEX,
332 PREFIX_HEX_5,
333 FULL_HEX,
334 LONG_HEX,
335 OCTAL,
336 OCTAL_5
337 }
338 print_mode;
339
340 typedef enum unicode_display_type
341 {
342 unicode_default = 0,
343 unicode_locale,
344 unicode_escape,
345 unicode_hex,
346 unicode_highlight,
347 unicode_invalid
348 } unicode_display_type;
349
350 static unicode_display_type unicode_display = unicode_default;
351
352 typedef enum
353 {
354 reltype_unknown,
355 reltype_rel,
356 reltype_rela,
357 reltype_relr
358 } relocation_type;
359
360 /* Versioned symbol info. */
361 enum versioned_symbol_info
362 {
363 symbol_undefined,
364 symbol_hidden,
365 symbol_public
366 };
367
368 static const char * get_symbol_version_string
369 (Filedata *, bool, const char *, unsigned long, unsigned,
370 Elf_Internal_Sym *, enum versioned_symbol_info *, unsigned short *);
371
372 #define UNKNOWN -1
373
374 static inline const char *
375 section_name (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
376 {
377 return filedata->string_table + hdr->sh_name;
378 }
379
380 static inline bool
381 section_name_valid (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
382 {
383 return (hdr != NULL
384 && filedata->string_table != NULL
385 && hdr->sh_name < filedata->string_table_length);
386 }
387
388 static inline const char *
389 section_name_print (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
390 {
391 if (hdr == NULL)
392 return _("<none>");
393 if (filedata->string_table == NULL)
394 return _("<no-strings>");
395 if (hdr->sh_name >= filedata->string_table_length)
396 return _("<corrupt>");
397 return section_name (filedata, hdr);
398 }
399
400 #define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */
401
402 static inline bool
403 valid_symbol_name (const char *strtab, size_t strtab_size, uint64_t offset)
404 {
405 return strtab != NULL && offset < strtab_size;
406 }
407
408 static inline bool
409 valid_dynamic_name (const Filedata *filedata, uint64_t offset)
410 {
411 return valid_symbol_name (filedata->dynamic_strings,
412 filedata->dynamic_strings_length, offset);
413 }
414
415 /* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
416 already been called and verified that the string exists. */
417 static inline const char *
418 get_dynamic_name (const Filedata *filedata, size_t offset)
419 {
420 return filedata->dynamic_strings + offset;
421 }
422
423 #define REMOVE_ARCH_BITS(ADDR) \
424 do \
425 { \
426 if (filedata->file_header.e_machine == EM_ARM) \
427 (ADDR) &= ~1; \
428 } \
429 while (0)
430
431 /* Get the correct GNU hash section name. */
432 #define GNU_HASH_SECTION_NAME(filedata) \
433 filedata->dynamic_info_DT_MIPS_XHASH ? ".MIPS.xhash" : ".gnu.hash"
434 \f
435 /* Retrieve NMEMB structures, each SIZE bytes long from FILEDATA starting at
436 OFFSET + the offset of the current archive member, if we are examining an
437 archive. Put the retrieved data into VAR, if it is not NULL. Otherwise
438 allocate a buffer using malloc and fill that. In either case return the
439 pointer to the start of the retrieved data or NULL if something went wrong.
440 If something does go wrong and REASON is not NULL then emit an error
441 message using REASON as part of the context. */
442
443 static void *
444 get_data (void *var,
445 Filedata *filedata,
446 unsigned long offset,
447 uint64_t size,
448 uint64_t nmemb,
449 const char *reason)
450 {
451 void * mvar;
452 uint64_t amt = size * nmemb;
453
454 if (size == 0 || nmemb == 0)
455 return NULL;
456
457 /* If size_t is smaller than uint64_t, eg because you are building
458 on a 32-bit host, then make sure that when the sizes are cast to
459 size_t no information is lost. */
460 if ((size_t) size != size
461 || (size_t) nmemb != nmemb
462 || (size_t) amt != amt
463 || amt / size != nmemb
464 || (size_t) amt + 1 == 0)
465 {
466 if (reason)
467 error (_("Size overflow prevents reading %" PRIu64
468 " elements of size %" PRIu64 " for %s\n"),
469 nmemb, size, reason);
470 return NULL;
471 }
472
473 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
474 attempting to allocate memory when the read is bound to fail. */
475 if (filedata->archive_file_offset > filedata->file_size
476 || offset > filedata->file_size - filedata->archive_file_offset
477 || amt > filedata->file_size - filedata->archive_file_offset - offset)
478 {
479 if (reason)
480 error (_("Reading %" PRIu64 " bytes extends past end of file for %s\n"),
481 amt, reason);
482 return NULL;
483 }
484
485 if (fseek (filedata->handle, filedata->archive_file_offset + offset,
486 SEEK_SET))
487 {
488 if (reason)
489 error (_("Unable to seek to 0x%lx for %s\n"),
490 filedata->archive_file_offset + offset, reason);
491 return NULL;
492 }
493
494 mvar = var;
495 if (mvar == NULL)
496 {
497 /* + 1 so that we can '\0' terminate invalid string table sections. */
498 mvar = malloc ((size_t) amt + 1);
499
500 if (mvar == NULL)
501 {
502 if (reason)
503 error (_("Out of memory allocating %" PRIu64 " bytes for %s\n"),
504 amt, reason);
505 return NULL;
506 }
507
508 ((char *) mvar)[amt] = '\0';
509 }
510
511 if (fread (mvar, (size_t) size, (size_t) nmemb, filedata->handle) != nmemb)
512 {
513 if (reason)
514 error (_("Unable to read in %" PRIu64 " bytes of %s\n"),
515 amt, reason);
516 if (mvar != var)
517 free (mvar);
518 return NULL;
519 }
520
521 return mvar;
522 }
523
524 /* Print a VMA value in the MODE specified.
525 Returns the number of characters displayed. */
526
527 static unsigned int
528 print_vma (uint64_t vma, print_mode mode)
529 {
530 unsigned int nc = 0;
531
532 switch (mode)
533 {
534 case FULL_HEX:
535 nc = printf ("0x");
536 /* Fall through. */
537 case LONG_HEX:
538 if (!is_32bit_elf)
539 return nc + printf ("%16.16" PRIx64, vma);
540 return nc + printf ("%8.8" PRIx64, vma);
541
542 case DEC_5:
543 if (vma <= 99999)
544 return printf ("%5" PRId64, vma);
545 /* Fall through. */
546 case PREFIX_HEX:
547 nc = printf ("0x");
548 /* Fall through. */
549 case HEX:
550 return nc + printf ("%" PRIx64, vma);
551
552 case PREFIX_HEX_5:
553 nc = printf ("0x");
554 /* Fall through. */
555 case HEX_5:
556 return nc + printf ("%05" PRIx64, vma);
557
558 case DEC:
559 return printf ("%" PRId64, vma);
560
561 case UNSIGNED:
562 return printf ("%" PRIu64, vma);
563
564 case UNSIGNED_5:
565 return printf ("%5" PRIu64, vma);
566
567 case OCTAL:
568 return printf ("%" PRIo64, vma);
569
570 case OCTAL_5:
571 return printf ("%5" PRIo64, vma);
572
573 default:
574 /* FIXME: Report unrecognised mode ? */
575 return 0;
576 }
577 }
578
579
580 /* Display a symbol on stdout. Handles the display of control characters and
581 multibye characters (assuming the host environment supports them).
582
583 Display at most abs(WIDTH) characters, truncating as necessary, unless do_wide is true.
584
585 If truncation will happen and do_not_show_symbol_truncation is FALSE then display
586 abs(WIDTH) - 5 characters followed by "[...]".
587
588 If WIDTH is negative then ensure that the output is at least (- WIDTH) characters,
589 padding as necessary.
590
591 Returns the number of emitted characters. */
592
593 static unsigned int
594 print_symbol (signed int width, const char * symbol)
595 {
596 bool extra_padding = false;
597 bool do_dots = false;
598 signed int num_printed = 0;
599 #ifdef HAVE_MBSTATE_T
600 mbstate_t state;
601 #endif
602 unsigned int width_remaining;
603 const void * alloced_symbol = NULL;
604
605 if (width < 0)
606 {
607 /* Keep the width positive. This helps the code below. */
608 width = - width;
609 extra_padding = true;
610 }
611 else if (width == 0)
612 return 0;
613
614 if (do_wide)
615 /* Set the remaining width to a very large value.
616 This simplifies the code below. */
617 width_remaining = INT_MAX;
618 else
619 {
620 width_remaining = width;
621 if (! do_not_show_symbol_truncation
622 && (int) strlen (symbol) > width)
623 {
624 width_remaining -= 5;
625 if ((int) width_remaining < 0)
626 width_remaining = 0;
627 do_dots = true;
628 }
629 }
630
631 #ifdef HAVE_MBSTATE_T
632 /* Initialise the multibyte conversion state. */
633 memset (& state, 0, sizeof (state));
634 #endif
635
636 if (do_demangle && *symbol)
637 {
638 const char * res = cplus_demangle (symbol, demangle_flags);
639
640 if (res != NULL)
641 alloced_symbol = symbol = res;
642 }
643
644 while (width_remaining)
645 {
646 size_t n;
647 const char c = *symbol++;
648
649 if (c == 0)
650 break;
651
652 if (ISPRINT (c))
653 {
654 putchar (c);
655 width_remaining --;
656 num_printed ++;
657 }
658 else if (ISCNTRL (c))
659 {
660 /* Do not print control characters directly as they can affect terminal
661 settings. Such characters usually appear in the names generated
662 by the assembler for local labels. */
663
664 if (width_remaining < 2)
665 break;
666
667 printf ("^%c", c + 0x40);
668 width_remaining -= 2;
669 num_printed += 2;
670 }
671 else if (c == 0x7f)
672 {
673 if (width_remaining < 5)
674 break;
675 printf ("<DEL>");
676 width_remaining -= 5;
677 num_printed += 5;
678 }
679 else if (unicode_display != unicode_locale
680 && unicode_display != unicode_default)
681 {
682 /* Display unicode characters as something else. */
683 unsigned char bytes[4];
684 bool is_utf8;
685 unsigned int nbytes;
686
687 bytes[0] = c;
688
689 if (bytes[0] < 0xc0)
690 {
691 nbytes = 1;
692 is_utf8 = false;
693 }
694 else
695 {
696 bytes[1] = *symbol++;
697
698 if ((bytes[1] & 0xc0) != 0x80)
699 {
700 is_utf8 = false;
701 /* Do not consume this character. It may only
702 be the first byte in the sequence that was
703 corrupt. */
704 --symbol;
705 nbytes = 1;
706 }
707 else if ((bytes[0] & 0x20) == 0)
708 {
709 is_utf8 = true;
710 nbytes = 2;
711 }
712 else
713 {
714 bytes[2] = *symbol++;
715
716 if ((bytes[2] & 0xc0) != 0x80)
717 {
718 is_utf8 = false;
719 symbol -= 2;
720 nbytes = 1;
721 }
722 else if ((bytes[0] & 0x10) == 0)
723 {
724 is_utf8 = true;
725 nbytes = 3;
726 }
727 else
728 {
729 bytes[3] = *symbol++;
730
731 nbytes = 4;
732
733 if ((bytes[3] & 0xc0) != 0x80)
734 {
735 is_utf8 = false;
736 symbol -= 3;
737 nbytes = 1;
738 }
739 else
740 is_utf8 = true;
741 }
742 }
743 }
744
745 if (unicode_display == unicode_invalid)
746 is_utf8 = false;
747
748 if (unicode_display == unicode_hex || ! is_utf8)
749 {
750 unsigned int i;
751
752 if (width_remaining < (nbytes * 2) + 2)
753 break;
754
755 putchar (is_utf8 ? '<' : '{');
756 printf ("0x");
757 for (i = 0; i < nbytes; i++)
758 printf ("%02x", bytes[i]);
759 putchar (is_utf8 ? '>' : '}');
760 }
761 else
762 {
763 if (unicode_display == unicode_highlight && isatty (1))
764 printf ("\x1B[31;47m"); /* Red. */
765
766 switch (nbytes)
767 {
768 case 2:
769 if (width_remaining < 6)
770 break;
771 printf ("\\u%02x%02x",
772 (bytes[0] & 0x1c) >> 2,
773 ((bytes[0] & 0x03) << 6) | (bytes[1] & 0x3f));
774 break;
775 case 3:
776 if (width_remaining < 6)
777 break;
778 printf ("\\u%02x%02x",
779 ((bytes[0] & 0x0f) << 4) | ((bytes[1] & 0x3c) >> 2),
780 ((bytes[1] & 0x03) << 6) | (bytes[2] & 0x3f));
781 break;
782 case 4:
783 if (width_remaining < 8)
784 break;
785 printf ("\\u%02x%02x%02x",
786 ((bytes[0] & 0x07) << 6) | ((bytes[1] & 0x3c) >> 2),
787 ((bytes[1] & 0x03) << 6) | ((bytes[2] & 0x3c) >> 2),
788 ((bytes[2] & 0x03) << 6) | (bytes[3] & 0x3f));
789
790 break;
791 default:
792 /* URG. */
793 break;
794 }
795
796 if (unicode_display == unicode_highlight && isatty (1))
797 printf ("\033[0m"); /* Default colour. */
798 }
799
800 if (bytes[nbytes - 1] == 0)
801 break;
802 }
803 else
804 {
805 #ifdef HAVE_MBSTATE_T
806 wchar_t w;
807 #endif
808 /* Let printf do the hard work of displaying multibyte characters. */
809 printf ("%.1s", symbol - 1);
810 width_remaining --;
811 num_printed ++;
812
813 #ifdef HAVE_MBSTATE_T
814 /* Try to find out how many bytes made up the character that was
815 just printed. Advance the symbol pointer past the bytes that
816 were displayed. */
817 n = mbrtowc (& w, symbol - 1, MB_CUR_MAX, & state);
818 #else
819 n = 1;
820 #endif
821 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
822 symbol += (n - 1);
823 }
824 }
825
826 if (do_dots)
827 num_printed += printf ("[...]");
828
829 if (extra_padding && num_printed < width)
830 {
831 /* Fill in the remaining spaces. */
832 printf ("%-*s", width - num_printed, " ");
833 num_printed = width;
834 }
835
836 free ((void *) alloced_symbol);
837 return num_printed;
838 }
839
840 /* Returns a pointer to a static buffer containing a printable version of
841 the given section's name. Like print_symbol, except that it does not try
842 to print multibyte characters, it just interprets them as hex values. */
843
844 static const char *
845 printable_section_name (Filedata * filedata, const Elf_Internal_Shdr * sec)
846 {
847 #define MAX_PRINT_SEC_NAME_LEN 256
848 static char sec_name_buf [MAX_PRINT_SEC_NAME_LEN + 1];
849 const char * name = section_name_print (filedata, sec);
850 char * buf = sec_name_buf;
851 char c;
852 unsigned int remaining = MAX_PRINT_SEC_NAME_LEN;
853
854 while ((c = * name ++) != 0)
855 {
856 if (ISCNTRL (c))
857 {
858 if (remaining < 2)
859 break;
860
861 * buf ++ = '^';
862 * buf ++ = c + 0x40;
863 remaining -= 2;
864 }
865 else if (ISPRINT (c))
866 {
867 * buf ++ = c;
868 remaining -= 1;
869 }
870 else
871 {
872 static char hex[17] = "0123456789ABCDEF";
873
874 if (remaining < 4)
875 break;
876 * buf ++ = '<';
877 * buf ++ = hex[(c & 0xf0) >> 4];
878 * buf ++ = hex[c & 0x0f];
879 * buf ++ = '>';
880 remaining -= 4;
881 }
882
883 if (remaining == 0)
884 break;
885 }
886
887 * buf = 0;
888 return sec_name_buf;
889 }
890
891 static const char *
892 printable_section_name_from_index (Filedata * filedata, unsigned long ndx)
893 {
894 if (ndx >= filedata->file_header.e_shnum)
895 return _("<corrupt>");
896
897 return printable_section_name (filedata, filedata->section_headers + ndx);
898 }
899
900 /* Return a pointer to section NAME, or NULL if no such section exists. */
901
902 static Elf_Internal_Shdr *
903 find_section (Filedata * filedata, const char * name)
904 {
905 unsigned int i;
906
907 if (filedata->section_headers == NULL)
908 return NULL;
909
910 for (i = 0; i < filedata->file_header.e_shnum; i++)
911 if (section_name_valid (filedata, filedata->section_headers + i)
912 && streq (section_name (filedata, filedata->section_headers + i),
913 name))
914 return filedata->section_headers + i;
915
916 return NULL;
917 }
918
919 /* Return a pointer to a section containing ADDR, or NULL if no such
920 section exists. */
921
922 static Elf_Internal_Shdr *
923 find_section_by_address (Filedata * filedata, uint64_t addr)
924 {
925 unsigned int i;
926
927 if (filedata->section_headers == NULL)
928 return NULL;
929
930 for (i = 0; i < filedata->file_header.e_shnum; i++)
931 {
932 Elf_Internal_Shdr *sec = filedata->section_headers + i;
933
934 if (addr >= sec->sh_addr && addr < sec->sh_addr + sec->sh_size)
935 return sec;
936 }
937
938 return NULL;
939 }
940
941 static Elf_Internal_Shdr *
942 find_section_by_type (Filedata * filedata, unsigned int type)
943 {
944 unsigned int i;
945
946 if (filedata->section_headers == NULL)
947 return NULL;
948
949 for (i = 0; i < filedata->file_header.e_shnum; i++)
950 {
951 Elf_Internal_Shdr *sec = filedata->section_headers + i;
952
953 if (sec->sh_type == type)
954 return sec;
955 }
956
957 return NULL;
958 }
959
960 /* Return a pointer to section NAME, or NULL if no such section exists,
961 restricted to the list of sections given in SET. */
962
963 static Elf_Internal_Shdr *
964 find_section_in_set (Filedata * filedata, const char * name, unsigned int * set)
965 {
966 unsigned int i;
967
968 if (filedata->section_headers == NULL)
969 return NULL;
970
971 if (set != NULL)
972 {
973 while ((i = *set++) > 0)
974 {
975 /* See PR 21156 for a reproducer. */
976 if (i >= filedata->file_header.e_shnum)
977 continue; /* FIXME: Should we issue an error message ? */
978
979 if (section_name_valid (filedata, filedata->section_headers + i)
980 && streq (section_name (filedata, filedata->section_headers + i),
981 name))
982 return filedata->section_headers + i;
983 }
984 }
985
986 return find_section (filedata, name);
987 }
988
989 /* Return TRUE if the current file is for IA-64 machine and OpenVMS ABI.
990 This OS has so many departures from the ELF standard that we test it at
991 many places. */
992
993 static inline bool
994 is_ia64_vms (Filedata * filedata)
995 {
996 return filedata->file_header.e_machine == EM_IA_64
997 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS;
998 }
999
1000 /* Guess the relocation size commonly used by the specific machines. */
1001
1002 static bool
1003 guess_is_rela (unsigned int e_machine)
1004 {
1005 switch (e_machine)
1006 {
1007 /* Targets that use REL relocations. */
1008 case EM_386:
1009 case EM_IAMCU:
1010 case EM_960:
1011 case EM_ARM:
1012 case EM_D10V:
1013 case EM_CYGNUS_D10V:
1014 case EM_DLX:
1015 case EM_MIPS:
1016 case EM_MIPS_RS3_LE:
1017 case EM_CYGNUS_M32R:
1018 case EM_SCORE:
1019 case EM_XGATE:
1020 case EM_NFP:
1021 case EM_BPF:
1022 return false;
1023
1024 /* Targets that use RELA relocations. */
1025 case EM_68K:
1026 case EM_860:
1027 case EM_AARCH64:
1028 case EM_ADAPTEVA_EPIPHANY:
1029 case EM_ALPHA:
1030 case EM_ALTERA_NIOS2:
1031 case EM_ARC:
1032 case EM_ARC_COMPACT:
1033 case EM_ARC_COMPACT2:
1034 case EM_AVR:
1035 case EM_AVR_OLD:
1036 case EM_BLACKFIN:
1037 case EM_CR16:
1038 case EM_CRIS:
1039 case EM_CRX:
1040 case EM_CSKY:
1041 case EM_D30V:
1042 case EM_CYGNUS_D30V:
1043 case EM_FR30:
1044 case EM_FT32:
1045 case EM_CYGNUS_FR30:
1046 case EM_CYGNUS_FRV:
1047 case EM_H8S:
1048 case EM_H8_300:
1049 case EM_H8_300H:
1050 case EM_IA_64:
1051 case EM_IP2K:
1052 case EM_IP2K_OLD:
1053 case EM_IQ2000:
1054 case EM_LATTICEMICO32:
1055 case EM_M32C_OLD:
1056 case EM_M32C:
1057 case EM_M32R:
1058 case EM_MCORE:
1059 case EM_CYGNUS_MEP:
1060 case EM_METAG:
1061 case EM_MMIX:
1062 case EM_MN10200:
1063 case EM_CYGNUS_MN10200:
1064 case EM_MN10300:
1065 case EM_CYGNUS_MN10300:
1066 case EM_MOXIE:
1067 case EM_MSP430:
1068 case EM_MSP430_OLD:
1069 case EM_MT:
1070 case EM_NDS32:
1071 case EM_NIOS32:
1072 case EM_OR1K:
1073 case EM_PPC64:
1074 case EM_PPC:
1075 case EM_TI_PRU:
1076 case EM_RISCV:
1077 case EM_RL78:
1078 case EM_RX:
1079 case EM_S390:
1080 case EM_S390_OLD:
1081 case EM_SH:
1082 case EM_SPARC:
1083 case EM_SPARC32PLUS:
1084 case EM_SPARCV9:
1085 case EM_SPU:
1086 case EM_TI_C6000:
1087 case EM_TILEGX:
1088 case EM_TILEPRO:
1089 case EM_V800:
1090 case EM_V850:
1091 case EM_CYGNUS_V850:
1092 case EM_VAX:
1093 case EM_VISIUM:
1094 case EM_X86_64:
1095 case EM_L1OM:
1096 case EM_K1OM:
1097 case EM_XSTORMY16:
1098 case EM_XTENSA:
1099 case EM_XTENSA_OLD:
1100 case EM_MICROBLAZE:
1101 case EM_MICROBLAZE_OLD:
1102 case EM_WEBASSEMBLY:
1103 return true;
1104
1105 case EM_68HC05:
1106 case EM_68HC08:
1107 case EM_68HC11:
1108 case EM_68HC16:
1109 case EM_FX66:
1110 case EM_ME16:
1111 case EM_MMA:
1112 case EM_NCPU:
1113 case EM_NDR1:
1114 case EM_PCP:
1115 case EM_ST100:
1116 case EM_ST19:
1117 case EM_ST7:
1118 case EM_ST9PLUS:
1119 case EM_STARCORE:
1120 case EM_SVX:
1121 case EM_TINYJ:
1122 default:
1123 warn (_("Don't know about relocations on this machine architecture\n"));
1124 return false;
1125 }
1126 }
1127
1128 /* Load RELA type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1129 Returns TRUE upon success, FALSE otherwise. If successful then a
1130 pointer to a malloc'ed buffer containing the relocs is placed in *RELASP,
1131 and the number of relocs loaded is placed in *NRELASP. It is the caller's
1132 responsibility to free the allocated buffer. */
1133
1134 static bool
1135 slurp_rela_relocs (Filedata * filedata,
1136 unsigned long rel_offset,
1137 unsigned long rel_size,
1138 Elf_Internal_Rela ** relasp,
1139 unsigned long * nrelasp)
1140 {
1141 Elf_Internal_Rela * relas;
1142 size_t nrelas;
1143 unsigned int i;
1144
1145 if (is_32bit_elf)
1146 {
1147 Elf32_External_Rela * erelas;
1148
1149 erelas = (Elf32_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1150 rel_size, _("32-bit relocation data"));
1151 if (!erelas)
1152 return false;
1153
1154 nrelas = rel_size / sizeof (Elf32_External_Rela);
1155
1156 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1157 sizeof (Elf_Internal_Rela));
1158
1159 if (relas == NULL)
1160 {
1161 free (erelas);
1162 error (_("out of memory parsing relocs\n"));
1163 return false;
1164 }
1165
1166 for (i = 0; i < nrelas; i++)
1167 {
1168 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1169 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1170 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1171 }
1172
1173 free (erelas);
1174 }
1175 else
1176 {
1177 Elf64_External_Rela * erelas;
1178
1179 erelas = (Elf64_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1180 rel_size, _("64-bit relocation data"));
1181 if (!erelas)
1182 return false;
1183
1184 nrelas = rel_size / sizeof (Elf64_External_Rela);
1185
1186 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1187 sizeof (Elf_Internal_Rela));
1188
1189 if (relas == NULL)
1190 {
1191 free (erelas);
1192 error (_("out of memory parsing relocs\n"));
1193 return false;
1194 }
1195
1196 for (i = 0; i < nrelas; i++)
1197 {
1198 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1199 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1200 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1201
1202 if (filedata->file_header.e_machine == EM_MIPS
1203 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1204 {
1205 /* In little-endian objects, r_info isn't really a
1206 64-bit little-endian value: it has a 32-bit
1207 little-endian symbol index followed by four
1208 individual byte fields. Reorder INFO
1209 accordingly. */
1210 uint64_t inf = relas[i].r_info;
1211 inf = (((inf & 0xffffffff) << 32)
1212 | ((inf >> 56) & 0xff)
1213 | ((inf >> 40) & 0xff00)
1214 | ((inf >> 24) & 0xff0000)
1215 | ((inf >> 8) & 0xff000000));
1216 relas[i].r_info = inf;
1217 }
1218 }
1219
1220 free (erelas);
1221 }
1222
1223 *relasp = relas;
1224 *nrelasp = nrelas;
1225 return true;
1226 }
1227
1228 /* Load REL type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1229 Returns TRUE upon success, FALSE otherwise. If successful then a
1230 pointer to a malloc'ed buffer containing the relocs is placed in *RELSP,
1231 and the number of relocs loaded is placed in *NRELSP. It is the caller's
1232 responsibility to free the allocated buffer. */
1233
1234 static bool
1235 slurp_rel_relocs (Filedata * filedata,
1236 unsigned long rel_offset,
1237 unsigned long rel_size,
1238 Elf_Internal_Rela ** relsp,
1239 unsigned long * nrelsp)
1240 {
1241 Elf_Internal_Rela * rels;
1242 size_t nrels;
1243 unsigned int i;
1244
1245 if (is_32bit_elf)
1246 {
1247 Elf32_External_Rel * erels;
1248
1249 erels = (Elf32_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1250 rel_size, _("32-bit relocation data"));
1251 if (!erels)
1252 return false;
1253
1254 nrels = rel_size / sizeof (Elf32_External_Rel);
1255
1256 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1257
1258 if (rels == NULL)
1259 {
1260 free (erels);
1261 error (_("out of memory parsing relocs\n"));
1262 return false;
1263 }
1264
1265 for (i = 0; i < nrels; i++)
1266 {
1267 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1268 rels[i].r_info = BYTE_GET (erels[i].r_info);
1269 rels[i].r_addend = 0;
1270 }
1271
1272 free (erels);
1273 }
1274 else
1275 {
1276 Elf64_External_Rel * erels;
1277
1278 erels = (Elf64_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1279 rel_size, _("64-bit relocation data"));
1280 if (!erels)
1281 return false;
1282
1283 nrels = rel_size / sizeof (Elf64_External_Rel);
1284
1285 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1286
1287 if (rels == NULL)
1288 {
1289 free (erels);
1290 error (_("out of memory parsing relocs\n"));
1291 return false;
1292 }
1293
1294 for (i = 0; i < nrels; i++)
1295 {
1296 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1297 rels[i].r_info = BYTE_GET (erels[i].r_info);
1298 rels[i].r_addend = 0;
1299
1300 if (filedata->file_header.e_machine == EM_MIPS
1301 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1302 {
1303 /* In little-endian objects, r_info isn't really a
1304 64-bit little-endian value: it has a 32-bit
1305 little-endian symbol index followed by four
1306 individual byte fields. Reorder INFO
1307 accordingly. */
1308 uint64_t inf = rels[i].r_info;
1309 inf = (((inf & 0xffffffff) << 32)
1310 | ((inf >> 56) & 0xff)
1311 | ((inf >> 40) & 0xff00)
1312 | ((inf >> 24) & 0xff0000)
1313 | ((inf >> 8) & 0xff000000));
1314 rels[i].r_info = inf;
1315 }
1316 }
1317
1318 free (erels);
1319 }
1320
1321 *relsp = rels;
1322 *nrelsp = nrels;
1323 return true;
1324 }
1325
1326 static bool
1327 slurp_relr_relocs (Filedata * filedata,
1328 unsigned long relr_offset,
1329 unsigned long relr_size,
1330 uint64_t ** relrsp,
1331 unsigned long * nrelrsp)
1332 {
1333 void *relrs;
1334 size_t size = 0, nentries, i;
1335 uint64_t base = 0, addr, entry;
1336
1337 relrs = get_data (NULL, filedata, relr_offset, 1, relr_size,
1338 _("RELR relocation data"));
1339 if (!relrs)
1340 return false;
1341
1342 if (is_32bit_elf)
1343 nentries = relr_size / sizeof (Elf32_External_Relr);
1344 else
1345 nentries = relr_size / sizeof (Elf64_External_Relr);
1346 for (i = 0; i < nentries; i++)
1347 {
1348 if (is_32bit_elf)
1349 entry = BYTE_GET (((Elf32_External_Relr *)relrs)[i].r_data);
1350 else
1351 entry = BYTE_GET (((Elf64_External_Relr *)relrs)[i].r_data);
1352 if ((entry & 1) == 0)
1353 size++;
1354 else
1355 while ((entry >>= 1) != 0)
1356 if ((entry & 1) == 1)
1357 size++;
1358 }
1359
1360 *relrsp = malloc (size * sizeof (**relrsp));
1361 if (*relrsp == NULL)
1362 {
1363 free (relrs);
1364 error (_("out of memory parsing relocs\n"));
1365 return false;
1366 }
1367
1368 size = 0;
1369 for (i = 0; i < nentries; i++)
1370 {
1371 const uint64_t entry_bytes = is_32bit_elf ? 4 : 8;
1372
1373 if (is_32bit_elf)
1374 entry = BYTE_GET (((Elf32_External_Relr *)relrs)[i].r_data);
1375 else
1376 entry = BYTE_GET (((Elf64_External_Relr *)relrs)[i].r_data);
1377 if ((entry & 1) == 0)
1378 {
1379 (*relrsp)[size++] = entry;
1380 base = entry + entry_bytes;
1381 }
1382 else
1383 {
1384 for (addr = base; (entry >>= 1) != 0; addr += entry_bytes)
1385 if ((entry & 1) != 0)
1386 (*relrsp)[size++] = addr;
1387 base += entry_bytes * (entry_bytes * CHAR_BIT - 1);
1388 }
1389 }
1390
1391 *nrelrsp = size;
1392 free (relrs);
1393 return true;
1394 }
1395
1396 /* Returns the reloc type extracted from the reloc info field. */
1397
1398 static unsigned int
1399 get_reloc_type (Filedata * filedata, uint64_t reloc_info)
1400 {
1401 if (is_32bit_elf)
1402 return ELF32_R_TYPE (reloc_info);
1403
1404 switch (filedata->file_header.e_machine)
1405 {
1406 case EM_MIPS:
1407 /* Note: We assume that reloc_info has already been adjusted for us. */
1408 return ELF64_MIPS_R_TYPE (reloc_info);
1409
1410 case EM_SPARCV9:
1411 return ELF64_R_TYPE_ID (reloc_info);
1412
1413 default:
1414 return ELF64_R_TYPE (reloc_info);
1415 }
1416 }
1417
1418 /* Return the symbol index extracted from the reloc info field. */
1419
1420 static uint64_t
1421 get_reloc_symindex (uint64_t reloc_info)
1422 {
1423 return is_32bit_elf ? ELF32_R_SYM (reloc_info) : ELF64_R_SYM (reloc_info);
1424 }
1425
1426 static inline bool
1427 uses_msp430x_relocs (Filedata * filedata)
1428 {
1429 return
1430 filedata->file_header.e_machine == EM_MSP430 /* Paranoia. */
1431 /* GCC uses osabi == ELFOSBI_STANDALONE. */
1432 && (((filedata->file_header.e_flags & EF_MSP430_MACH) == E_MSP430_MACH_MSP430X)
1433 /* TI compiler uses ELFOSABI_NONE. */
1434 || (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_NONE));
1435 }
1436
1437 /* Display the contents of the relocation data found at the specified
1438 offset. */
1439
1440 static bool
1441 dump_relocations (Filedata * filedata,
1442 unsigned long rel_offset,
1443 unsigned long rel_size,
1444 Elf_Internal_Sym * symtab,
1445 unsigned long nsyms,
1446 char * strtab,
1447 unsigned long strtablen,
1448 relocation_type rel_type,
1449 bool is_dynsym)
1450 {
1451 unsigned long i;
1452 Elf_Internal_Rela * rels;
1453 bool res = true;
1454
1455 if (rel_type == reltype_unknown)
1456 rel_type = guess_is_rela (filedata->file_header.e_machine) ? reltype_rela : reltype_rel;
1457
1458 if (rel_type == reltype_rela)
1459 {
1460 if (!slurp_rela_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1461 return false;
1462 }
1463 else if (rel_type == reltype_rel)
1464 {
1465 if (!slurp_rel_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1466 return false;
1467 }
1468 else if (rel_type == reltype_relr)
1469 {
1470 uint64_t * relrs;
1471 const char *format
1472 = is_32bit_elf ? "%08" PRIx64 "\n" : "%016" PRIx64 "\n";
1473
1474 if (!slurp_relr_relocs (filedata, rel_offset, rel_size, &relrs,
1475 &rel_size))
1476 return false;
1477
1478 printf (ngettext (" %lu offset\n", " %lu offsets\n", rel_size),
1479 rel_size);
1480 for (i = 0; i < rel_size; i++)
1481 printf (format, relrs[i]);
1482 free (relrs);
1483 return true;
1484 }
1485
1486 if (is_32bit_elf)
1487 {
1488 if (rel_type == reltype_rela)
1489 {
1490 if (do_wide)
1491 printf (_(" Offset Info Type Sym. Value Symbol's Name + Addend\n"));
1492 else
1493 printf (_(" Offset Info Type Sym.Value Sym. Name + Addend\n"));
1494 }
1495 else
1496 {
1497 if (do_wide)
1498 printf (_(" Offset Info Type Sym. Value Symbol's Name\n"));
1499 else
1500 printf (_(" Offset Info Type Sym.Value Sym. Name\n"));
1501 }
1502 }
1503 else
1504 {
1505 if (rel_type == reltype_rela)
1506 {
1507 if (do_wide)
1508 printf (_(" Offset Info Type Symbol's Value Symbol's Name + Addend\n"));
1509 else
1510 printf (_(" Offset Info Type Sym. Value Sym. Name + Addend\n"));
1511 }
1512 else
1513 {
1514 if (do_wide)
1515 printf (_(" Offset Info Type Symbol's Value Symbol's Name\n"));
1516 else
1517 printf (_(" Offset Info Type Sym. Value Sym. Name\n"));
1518 }
1519 }
1520
1521 for (i = 0; i < rel_size; i++)
1522 {
1523 const char * rtype;
1524 uint64_t offset;
1525 uint64_t inf;
1526 uint64_t symtab_index;
1527 uint64_t type;
1528
1529 offset = rels[i].r_offset;
1530 inf = rels[i].r_info;
1531
1532 type = get_reloc_type (filedata, inf);
1533 symtab_index = get_reloc_symindex (inf);
1534
1535 if (is_32bit_elf)
1536 {
1537 printf ("%8.8lx %8.8lx ",
1538 (unsigned long) offset & 0xffffffff,
1539 (unsigned long) inf & 0xffffffff);
1540 }
1541 else
1542 {
1543 printf (do_wide
1544 ? "%16.16" PRIx64 " %16.16" PRIx64 " "
1545 : "%12.12" PRIx64 " %12.12" PRIx64 " ",
1546 offset, inf);
1547 }
1548
1549 switch (filedata->file_header.e_machine)
1550 {
1551 default:
1552 rtype = NULL;
1553 break;
1554
1555 case EM_AARCH64:
1556 rtype = elf_aarch64_reloc_type (type);
1557 break;
1558
1559 case EM_M32R:
1560 case EM_CYGNUS_M32R:
1561 rtype = elf_m32r_reloc_type (type);
1562 break;
1563
1564 case EM_386:
1565 case EM_IAMCU:
1566 rtype = elf_i386_reloc_type (type);
1567 break;
1568
1569 case EM_68HC11:
1570 case EM_68HC12:
1571 rtype = elf_m68hc11_reloc_type (type);
1572 break;
1573
1574 case EM_S12Z:
1575 rtype = elf_s12z_reloc_type (type);
1576 break;
1577
1578 case EM_68K:
1579 rtype = elf_m68k_reloc_type (type);
1580 break;
1581
1582 case EM_960:
1583 rtype = elf_i960_reloc_type (type);
1584 break;
1585
1586 case EM_AVR:
1587 case EM_AVR_OLD:
1588 rtype = elf_avr_reloc_type (type);
1589 break;
1590
1591 case EM_OLD_SPARCV9:
1592 case EM_SPARC32PLUS:
1593 case EM_SPARCV9:
1594 case EM_SPARC:
1595 rtype = elf_sparc_reloc_type (type);
1596 break;
1597
1598 case EM_SPU:
1599 rtype = elf_spu_reloc_type (type);
1600 break;
1601
1602 case EM_V800:
1603 rtype = v800_reloc_type (type);
1604 break;
1605 case EM_V850:
1606 case EM_CYGNUS_V850:
1607 rtype = v850_reloc_type (type);
1608 break;
1609
1610 case EM_D10V:
1611 case EM_CYGNUS_D10V:
1612 rtype = elf_d10v_reloc_type (type);
1613 break;
1614
1615 case EM_D30V:
1616 case EM_CYGNUS_D30V:
1617 rtype = elf_d30v_reloc_type (type);
1618 break;
1619
1620 case EM_DLX:
1621 rtype = elf_dlx_reloc_type (type);
1622 break;
1623
1624 case EM_SH:
1625 rtype = elf_sh_reloc_type (type);
1626 break;
1627
1628 case EM_MN10300:
1629 case EM_CYGNUS_MN10300:
1630 rtype = elf_mn10300_reloc_type (type);
1631 break;
1632
1633 case EM_MN10200:
1634 case EM_CYGNUS_MN10200:
1635 rtype = elf_mn10200_reloc_type (type);
1636 break;
1637
1638 case EM_FR30:
1639 case EM_CYGNUS_FR30:
1640 rtype = elf_fr30_reloc_type (type);
1641 break;
1642
1643 case EM_CYGNUS_FRV:
1644 rtype = elf_frv_reloc_type (type);
1645 break;
1646
1647 case EM_CSKY:
1648 rtype = elf_csky_reloc_type (type);
1649 break;
1650
1651 case EM_FT32:
1652 rtype = elf_ft32_reloc_type (type);
1653 break;
1654
1655 case EM_MCORE:
1656 rtype = elf_mcore_reloc_type (type);
1657 break;
1658
1659 case EM_MMIX:
1660 rtype = elf_mmix_reloc_type (type);
1661 break;
1662
1663 case EM_MOXIE:
1664 rtype = elf_moxie_reloc_type (type);
1665 break;
1666
1667 case EM_MSP430:
1668 if (uses_msp430x_relocs (filedata))
1669 {
1670 rtype = elf_msp430x_reloc_type (type);
1671 break;
1672 }
1673 /* Fall through. */
1674 case EM_MSP430_OLD:
1675 rtype = elf_msp430_reloc_type (type);
1676 break;
1677
1678 case EM_NDS32:
1679 rtype = elf_nds32_reloc_type (type);
1680 break;
1681
1682 case EM_PPC:
1683 rtype = elf_ppc_reloc_type (type);
1684 break;
1685
1686 case EM_PPC64:
1687 rtype = elf_ppc64_reloc_type (type);
1688 break;
1689
1690 case EM_MIPS:
1691 case EM_MIPS_RS3_LE:
1692 rtype = elf_mips_reloc_type (type);
1693 break;
1694
1695 case EM_RISCV:
1696 rtype = elf_riscv_reloc_type (type);
1697 break;
1698
1699 case EM_ALPHA:
1700 rtype = elf_alpha_reloc_type (type);
1701 break;
1702
1703 case EM_ARM:
1704 rtype = elf_arm_reloc_type (type);
1705 break;
1706
1707 case EM_ARC:
1708 case EM_ARC_COMPACT:
1709 case EM_ARC_COMPACT2:
1710 rtype = elf_arc_reloc_type (type);
1711 break;
1712
1713 case EM_PARISC:
1714 rtype = elf_hppa_reloc_type (type);
1715 break;
1716
1717 case EM_H8_300:
1718 case EM_H8_300H:
1719 case EM_H8S:
1720 rtype = elf_h8_reloc_type (type);
1721 break;
1722
1723 case EM_OR1K:
1724 rtype = elf_or1k_reloc_type (type);
1725 break;
1726
1727 case EM_PJ:
1728 case EM_PJ_OLD:
1729 rtype = elf_pj_reloc_type (type);
1730 break;
1731 case EM_IA_64:
1732 rtype = elf_ia64_reloc_type (type);
1733 break;
1734
1735 case EM_CRIS:
1736 rtype = elf_cris_reloc_type (type);
1737 break;
1738
1739 case EM_860:
1740 rtype = elf_i860_reloc_type (type);
1741 break;
1742
1743 case EM_X86_64:
1744 case EM_L1OM:
1745 case EM_K1OM:
1746 rtype = elf_x86_64_reloc_type (type);
1747 break;
1748
1749 case EM_S370:
1750 rtype = i370_reloc_type (type);
1751 break;
1752
1753 case EM_S390_OLD:
1754 case EM_S390:
1755 rtype = elf_s390_reloc_type (type);
1756 break;
1757
1758 case EM_SCORE:
1759 rtype = elf_score_reloc_type (type);
1760 break;
1761
1762 case EM_XSTORMY16:
1763 rtype = elf_xstormy16_reloc_type (type);
1764 break;
1765
1766 case EM_CRX:
1767 rtype = elf_crx_reloc_type (type);
1768 break;
1769
1770 case EM_VAX:
1771 rtype = elf_vax_reloc_type (type);
1772 break;
1773
1774 case EM_VISIUM:
1775 rtype = elf_visium_reloc_type (type);
1776 break;
1777
1778 case EM_BPF:
1779 rtype = elf_bpf_reloc_type (type);
1780 break;
1781
1782 case EM_ADAPTEVA_EPIPHANY:
1783 rtype = elf_epiphany_reloc_type (type);
1784 break;
1785
1786 case EM_IP2K:
1787 case EM_IP2K_OLD:
1788 rtype = elf_ip2k_reloc_type (type);
1789 break;
1790
1791 case EM_IQ2000:
1792 rtype = elf_iq2000_reloc_type (type);
1793 break;
1794
1795 case EM_XTENSA_OLD:
1796 case EM_XTENSA:
1797 rtype = elf_xtensa_reloc_type (type);
1798 break;
1799
1800 case EM_LATTICEMICO32:
1801 rtype = elf_lm32_reloc_type (type);
1802 break;
1803
1804 case EM_M32C_OLD:
1805 case EM_M32C:
1806 rtype = elf_m32c_reloc_type (type);
1807 break;
1808
1809 case EM_MT:
1810 rtype = elf_mt_reloc_type (type);
1811 break;
1812
1813 case EM_BLACKFIN:
1814 rtype = elf_bfin_reloc_type (type);
1815 break;
1816
1817 case EM_CYGNUS_MEP:
1818 rtype = elf_mep_reloc_type (type);
1819 break;
1820
1821 case EM_CR16:
1822 rtype = elf_cr16_reloc_type (type);
1823 break;
1824
1825 case EM_MICROBLAZE:
1826 case EM_MICROBLAZE_OLD:
1827 rtype = elf_microblaze_reloc_type (type);
1828 break;
1829
1830 case EM_RL78:
1831 rtype = elf_rl78_reloc_type (type);
1832 break;
1833
1834 case EM_RX:
1835 rtype = elf_rx_reloc_type (type);
1836 break;
1837
1838 case EM_METAG:
1839 rtype = elf_metag_reloc_type (type);
1840 break;
1841
1842 case EM_TI_C6000:
1843 rtype = elf_tic6x_reloc_type (type);
1844 break;
1845
1846 case EM_TILEGX:
1847 rtype = elf_tilegx_reloc_type (type);
1848 break;
1849
1850 case EM_TILEPRO:
1851 rtype = elf_tilepro_reloc_type (type);
1852 break;
1853
1854 case EM_WEBASSEMBLY:
1855 rtype = elf_wasm32_reloc_type (type);
1856 break;
1857
1858 case EM_XGATE:
1859 rtype = elf_xgate_reloc_type (type);
1860 break;
1861
1862 case EM_ALTERA_NIOS2:
1863 rtype = elf_nios2_reloc_type (type);
1864 break;
1865
1866 case EM_TI_PRU:
1867 rtype = elf_pru_reloc_type (type);
1868 break;
1869
1870 case EM_NFP:
1871 if (EF_NFP_MACH (filedata->file_header.e_flags) == E_NFP_MACH_3200)
1872 rtype = elf_nfp3200_reloc_type (type);
1873 else
1874 rtype = elf_nfp_reloc_type (type);
1875 break;
1876
1877 case EM_Z80:
1878 rtype = elf_z80_reloc_type (type);
1879 break;
1880
1881 case EM_LOONGARCH:
1882 rtype = elf_loongarch_reloc_type (type);
1883 break;
1884
1885 case EM_AMDGPU:
1886 rtype = elf_amdgpu_reloc_type (type);
1887 break;
1888 }
1889
1890 if (rtype == NULL)
1891 printf (_("unrecognized: %-7lx"), (unsigned long) type & 0xffffffff);
1892 else
1893 printf (do_wide ? "%-22s" : "%-17.17s", rtype);
1894
1895 if (filedata->file_header.e_machine == EM_ALPHA
1896 && rtype != NULL
1897 && streq (rtype, "R_ALPHA_LITUSE")
1898 && rel_type == reltype_rela)
1899 {
1900 switch (rels[i].r_addend)
1901 {
1902 case LITUSE_ALPHA_ADDR: rtype = "ADDR"; break;
1903 case LITUSE_ALPHA_BASE: rtype = "BASE"; break;
1904 case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
1905 case LITUSE_ALPHA_JSR: rtype = "JSR"; break;
1906 case LITUSE_ALPHA_TLSGD: rtype = "TLSGD"; break;
1907 case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
1908 case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
1909 default: rtype = NULL;
1910 }
1911
1912 if (rtype)
1913 printf (" (%s)", rtype);
1914 else
1915 {
1916 putchar (' ');
1917 printf (_("<unknown addend: %lx>"),
1918 (unsigned long) rels[i].r_addend);
1919 res = false;
1920 }
1921 }
1922 else if (symtab_index)
1923 {
1924 if (symtab == NULL || symtab_index >= nsyms)
1925 {
1926 error (_(" bad symbol index: %08lx in reloc\n"),
1927 (unsigned long) symtab_index);
1928 res = false;
1929 }
1930 else
1931 {
1932 Elf_Internal_Sym * psym;
1933 const char * version_string;
1934 enum versioned_symbol_info sym_info;
1935 unsigned short vna_other;
1936
1937 psym = symtab + symtab_index;
1938
1939 version_string
1940 = get_symbol_version_string (filedata, is_dynsym,
1941 strtab, strtablen,
1942 symtab_index,
1943 psym,
1944 &sym_info,
1945 &vna_other);
1946
1947 printf (" ");
1948
1949 if (ELF_ST_TYPE (psym->st_info) == STT_GNU_IFUNC)
1950 {
1951 const char * name;
1952 unsigned int len;
1953 unsigned int width = is_32bit_elf ? 8 : 14;
1954
1955 /* Relocations against GNU_IFUNC symbols do not use the value
1956 of the symbol as the address to relocate against. Instead
1957 they invoke the function named by the symbol and use its
1958 result as the address for relocation.
1959
1960 To indicate this to the user, do not display the value of
1961 the symbol in the "Symbols's Value" field. Instead show
1962 its name followed by () as a hint that the symbol is
1963 invoked. */
1964
1965 if (strtab == NULL
1966 || psym->st_name == 0
1967 || psym->st_name >= strtablen)
1968 name = "??";
1969 else
1970 name = strtab + psym->st_name;
1971
1972 len = print_symbol (width, name);
1973 if (version_string)
1974 printf (sym_info == symbol_public ? "@@%s" : "@%s",
1975 version_string);
1976 printf ("()%-*s", len <= width ? (width + 1) - len : 1, " ");
1977 }
1978 else
1979 {
1980 print_vma (psym->st_value, LONG_HEX);
1981
1982 printf (is_32bit_elf ? " " : " ");
1983 }
1984
1985 if (psym->st_name == 0)
1986 {
1987 const char * sec_name = "<null>";
1988 char name_buf[40];
1989
1990 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
1991 {
1992 if (psym->st_shndx < filedata->file_header.e_shnum
1993 && filedata->section_headers != NULL)
1994 sec_name = section_name_print (filedata,
1995 filedata->section_headers
1996 + psym->st_shndx);
1997 else if (psym->st_shndx == SHN_ABS)
1998 sec_name = "ABS";
1999 else if (psym->st_shndx == SHN_COMMON)
2000 sec_name = "COMMON";
2001 else if ((filedata->file_header.e_machine == EM_MIPS
2002 && psym->st_shndx == SHN_MIPS_SCOMMON)
2003 || (filedata->file_header.e_machine == EM_TI_C6000
2004 && psym->st_shndx == SHN_TIC6X_SCOMMON))
2005 sec_name = "SCOMMON";
2006 else if (filedata->file_header.e_machine == EM_MIPS
2007 && psym->st_shndx == SHN_MIPS_SUNDEFINED)
2008 sec_name = "SUNDEF";
2009 else if ((filedata->file_header.e_machine == EM_X86_64
2010 || filedata->file_header.e_machine == EM_L1OM
2011 || filedata->file_header.e_machine == EM_K1OM)
2012 && psym->st_shndx == SHN_X86_64_LCOMMON)
2013 sec_name = "LARGE_COMMON";
2014 else if (filedata->file_header.e_machine == EM_IA_64
2015 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
2016 && psym->st_shndx == SHN_IA_64_ANSI_COMMON)
2017 sec_name = "ANSI_COM";
2018 else if (is_ia64_vms (filedata)
2019 && psym->st_shndx == SHN_IA_64_VMS_SYMVEC)
2020 sec_name = "VMS_SYMVEC";
2021 else
2022 {
2023 sprintf (name_buf, "<section 0x%x>",
2024 (unsigned int) psym->st_shndx);
2025 sec_name = name_buf;
2026 }
2027 }
2028 print_symbol (22, sec_name);
2029 }
2030 else if (strtab == NULL)
2031 printf (_("<string table index: %3ld>"), psym->st_name);
2032 else if (psym->st_name >= strtablen)
2033 {
2034 error (_("<corrupt string table index: %3ld>\n"),
2035 psym->st_name);
2036 res = false;
2037 }
2038 else
2039 {
2040 print_symbol (22, strtab + psym->st_name);
2041 if (version_string)
2042 printf (sym_info == symbol_public ? "@@%s" : "@%s",
2043 version_string);
2044 }
2045
2046 if (rel_type == reltype_rela)
2047 {
2048 uint64_t off = rels[i].r_addend;
2049
2050 if ((int64_t) off < 0)
2051 printf (" - %" PRIx64, -off);
2052 else
2053 printf (" + %" PRIx64, off);
2054 }
2055 }
2056 }
2057 else if (rel_type == reltype_rela)
2058 {
2059 uint64_t off = rels[i].r_addend;
2060
2061 printf ("%*c", is_32bit_elf ? 12 : 20, ' ');
2062 if ((int64_t) off < 0)
2063 printf ("-%" PRIx64, -off);
2064 else
2065 printf ("%" PRIx64, off);
2066 }
2067
2068 if (filedata->file_header.e_machine == EM_SPARCV9
2069 && rtype != NULL
2070 && streq (rtype, "R_SPARC_OLO10"))
2071 printf (" + %lx", (unsigned long) ELF64_R_TYPE_DATA (inf));
2072
2073 putchar ('\n');
2074
2075 if (! is_32bit_elf && filedata->file_header.e_machine == EM_MIPS)
2076 {
2077 uint64_t type2 = ELF64_MIPS_R_TYPE2 (inf);
2078 uint64_t type3 = ELF64_MIPS_R_TYPE3 (inf);
2079 const char * rtype2 = elf_mips_reloc_type (type2);
2080 const char * rtype3 = elf_mips_reloc_type (type3);
2081
2082 printf (" Type2: ");
2083
2084 if (rtype2 == NULL)
2085 printf (_("unrecognized: %-7lx"),
2086 (unsigned long) type2 & 0xffffffff);
2087 else
2088 printf ("%-17.17s", rtype2);
2089
2090 printf ("\n Type3: ");
2091
2092 if (rtype3 == NULL)
2093 printf (_("unrecognized: %-7lx"),
2094 (unsigned long) type3 & 0xffffffff);
2095 else
2096 printf ("%-17.17s", rtype3);
2097
2098 putchar ('\n');
2099 }
2100 }
2101
2102 free (rels);
2103
2104 return res;
2105 }
2106
2107 static const char *
2108 get_aarch64_dynamic_type (unsigned long type)
2109 {
2110 switch (type)
2111 {
2112 case DT_AARCH64_BTI_PLT: return "AARCH64_BTI_PLT";
2113 case DT_AARCH64_PAC_PLT: return "AARCH64_PAC_PLT";
2114 case DT_AARCH64_VARIANT_PCS: return "AARCH64_VARIANT_PCS";
2115 default:
2116 return NULL;
2117 }
2118 }
2119
2120 static const char *
2121 get_mips_dynamic_type (unsigned long type)
2122 {
2123 switch (type)
2124 {
2125 case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
2126 case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
2127 case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
2128 case DT_MIPS_IVERSION: return "MIPS_IVERSION";
2129 case DT_MIPS_FLAGS: return "MIPS_FLAGS";
2130 case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
2131 case DT_MIPS_MSYM: return "MIPS_MSYM";
2132 case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
2133 case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
2134 case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
2135 case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
2136 case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
2137 case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
2138 case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
2139 case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
2140 case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
2141 case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
2142 case DT_MIPS_RLD_MAP_REL: return "MIPS_RLD_MAP_REL";
2143 case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
2144 case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
2145 case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
2146 case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
2147 case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
2148 case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
2149 case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
2150 case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
2151 case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
2152 case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
2153 case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
2154 case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
2155 case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
2156 case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
2157 case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
2158 case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
2159 case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
2160 case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
2161 case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
2162 case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
2163 case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
2164 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
2165 case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
2166 case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
2167 case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
2168 case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
2169 case DT_MIPS_PLTGOT: return "MIPS_PLTGOT";
2170 case DT_MIPS_RWPLT: return "MIPS_RWPLT";
2171 case DT_MIPS_XHASH: return "MIPS_XHASH";
2172 default:
2173 return NULL;
2174 }
2175 }
2176
2177 static const char *
2178 get_sparc64_dynamic_type (unsigned long type)
2179 {
2180 switch (type)
2181 {
2182 case DT_SPARC_REGISTER: return "SPARC_REGISTER";
2183 default:
2184 return NULL;
2185 }
2186 }
2187
2188 static const char *
2189 get_ppc_dynamic_type (unsigned long type)
2190 {
2191 switch (type)
2192 {
2193 case DT_PPC_GOT: return "PPC_GOT";
2194 case DT_PPC_OPT: return "PPC_OPT";
2195 default:
2196 return NULL;
2197 }
2198 }
2199
2200 static const char *
2201 get_ppc64_dynamic_type (unsigned long type)
2202 {
2203 switch (type)
2204 {
2205 case DT_PPC64_GLINK: return "PPC64_GLINK";
2206 case DT_PPC64_OPD: return "PPC64_OPD";
2207 case DT_PPC64_OPDSZ: return "PPC64_OPDSZ";
2208 case DT_PPC64_OPT: return "PPC64_OPT";
2209 default:
2210 return NULL;
2211 }
2212 }
2213
2214 static const char *
2215 get_parisc_dynamic_type (unsigned long type)
2216 {
2217 switch (type)
2218 {
2219 case DT_HP_LOAD_MAP: return "HP_LOAD_MAP";
2220 case DT_HP_DLD_FLAGS: return "HP_DLD_FLAGS";
2221 case DT_HP_DLD_HOOK: return "HP_DLD_HOOK";
2222 case DT_HP_UX10_INIT: return "HP_UX10_INIT";
2223 case DT_HP_UX10_INITSZ: return "HP_UX10_INITSZ";
2224 case DT_HP_PREINIT: return "HP_PREINIT";
2225 case DT_HP_PREINITSZ: return "HP_PREINITSZ";
2226 case DT_HP_NEEDED: return "HP_NEEDED";
2227 case DT_HP_TIME_STAMP: return "HP_TIME_STAMP";
2228 case DT_HP_CHECKSUM: return "HP_CHECKSUM";
2229 case DT_HP_GST_SIZE: return "HP_GST_SIZE";
2230 case DT_HP_GST_VERSION: return "HP_GST_VERSION";
2231 case DT_HP_GST_HASHVAL: return "HP_GST_HASHVAL";
2232 case DT_HP_EPLTREL: return "HP_GST_EPLTREL";
2233 case DT_HP_EPLTRELSZ: return "HP_GST_EPLTRELSZ";
2234 case DT_HP_FILTERED: return "HP_FILTERED";
2235 case DT_HP_FILTER_TLS: return "HP_FILTER_TLS";
2236 case DT_HP_COMPAT_FILTERED: return "HP_COMPAT_FILTERED";
2237 case DT_HP_LAZYLOAD: return "HP_LAZYLOAD";
2238 case DT_HP_BIND_NOW_COUNT: return "HP_BIND_NOW_COUNT";
2239 case DT_PLT: return "PLT";
2240 case DT_PLT_SIZE: return "PLT_SIZE";
2241 case DT_DLT: return "DLT";
2242 case DT_DLT_SIZE: return "DLT_SIZE";
2243 default:
2244 return NULL;
2245 }
2246 }
2247
2248 static const char *
2249 get_ia64_dynamic_type (unsigned long type)
2250 {
2251 switch (type)
2252 {
2253 case DT_IA_64_PLT_RESERVE: return "IA_64_PLT_RESERVE";
2254 case DT_IA_64_VMS_SUBTYPE: return "VMS_SUBTYPE";
2255 case DT_IA_64_VMS_IMGIOCNT: return "VMS_IMGIOCNT";
2256 case DT_IA_64_VMS_LNKFLAGS: return "VMS_LNKFLAGS";
2257 case DT_IA_64_VMS_VIR_MEM_BLK_SIZ: return "VMS_VIR_MEM_BLK_SIZ";
2258 case DT_IA_64_VMS_IDENT: return "VMS_IDENT";
2259 case DT_IA_64_VMS_NEEDED_IDENT: return "VMS_NEEDED_IDENT";
2260 case DT_IA_64_VMS_IMG_RELA_CNT: return "VMS_IMG_RELA_CNT";
2261 case DT_IA_64_VMS_SEG_RELA_CNT: return "VMS_SEG_RELA_CNT";
2262 case DT_IA_64_VMS_FIXUP_RELA_CNT: return "VMS_FIXUP_RELA_CNT";
2263 case DT_IA_64_VMS_FIXUP_NEEDED: return "VMS_FIXUP_NEEDED";
2264 case DT_IA_64_VMS_SYMVEC_CNT: return "VMS_SYMVEC_CNT";
2265 case DT_IA_64_VMS_XLATED: return "VMS_XLATED";
2266 case DT_IA_64_VMS_STACKSIZE: return "VMS_STACKSIZE";
2267 case DT_IA_64_VMS_UNWINDSZ: return "VMS_UNWINDSZ";
2268 case DT_IA_64_VMS_UNWIND_CODSEG: return "VMS_UNWIND_CODSEG";
2269 case DT_IA_64_VMS_UNWIND_INFOSEG: return "VMS_UNWIND_INFOSEG";
2270 case DT_IA_64_VMS_LINKTIME: return "VMS_LINKTIME";
2271 case DT_IA_64_VMS_SEG_NO: return "VMS_SEG_NO";
2272 case DT_IA_64_VMS_SYMVEC_OFFSET: return "VMS_SYMVEC_OFFSET";
2273 case DT_IA_64_VMS_SYMVEC_SEG: return "VMS_SYMVEC_SEG";
2274 case DT_IA_64_VMS_UNWIND_OFFSET: return "VMS_UNWIND_OFFSET";
2275 case DT_IA_64_VMS_UNWIND_SEG: return "VMS_UNWIND_SEG";
2276 case DT_IA_64_VMS_STRTAB_OFFSET: return "VMS_STRTAB_OFFSET";
2277 case DT_IA_64_VMS_SYSVER_OFFSET: return "VMS_SYSVER_OFFSET";
2278 case DT_IA_64_VMS_IMG_RELA_OFF: return "VMS_IMG_RELA_OFF";
2279 case DT_IA_64_VMS_SEG_RELA_OFF: return "VMS_SEG_RELA_OFF";
2280 case DT_IA_64_VMS_FIXUP_RELA_OFF: return "VMS_FIXUP_RELA_OFF";
2281 case DT_IA_64_VMS_PLTGOT_OFFSET: return "VMS_PLTGOT_OFFSET";
2282 case DT_IA_64_VMS_PLTGOT_SEG: return "VMS_PLTGOT_SEG";
2283 case DT_IA_64_VMS_FPMODE: return "VMS_FPMODE";
2284 default:
2285 return NULL;
2286 }
2287 }
2288
2289 static const char *
2290 get_solaris_section_type (unsigned long type)
2291 {
2292 switch (type)
2293 {
2294 case 0x6fffffee: return "SUNW_ancillary";
2295 case 0x6fffffef: return "SUNW_capchain";
2296 case 0x6ffffff0: return "SUNW_capinfo";
2297 case 0x6ffffff1: return "SUNW_symsort";
2298 case 0x6ffffff2: return "SUNW_tlssort";
2299 case 0x6ffffff3: return "SUNW_LDYNSYM";
2300 case 0x6ffffff4: return "SUNW_dof";
2301 case 0x6ffffff5: return "SUNW_cap";
2302 case 0x6ffffff6: return "SUNW_SIGNATURE";
2303 case 0x6ffffff7: return "SUNW_ANNOTATE";
2304 case 0x6ffffff8: return "SUNW_DEBUGSTR";
2305 case 0x6ffffff9: return "SUNW_DEBUG";
2306 case 0x6ffffffa: return "SUNW_move";
2307 case 0x6ffffffb: return "SUNW_COMDAT";
2308 case 0x6ffffffc: return "SUNW_syminfo";
2309 case 0x6ffffffd: return "SUNW_verdef";
2310 case 0x6ffffffe: return "SUNW_verneed";
2311 case 0x6fffffff: return "SUNW_versym";
2312 case 0x70000000: return "SPARC_GOTDATA";
2313 default: return NULL;
2314 }
2315 }
2316
2317 static const char *
2318 get_alpha_dynamic_type (unsigned long type)
2319 {
2320 switch (type)
2321 {
2322 case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
2323 default: return NULL;
2324 }
2325 }
2326
2327 static const char *
2328 get_score_dynamic_type (unsigned long type)
2329 {
2330 switch (type)
2331 {
2332 case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
2333 case DT_SCORE_LOCAL_GOTNO: return "SCORE_LOCAL_GOTNO";
2334 case DT_SCORE_SYMTABNO: return "SCORE_SYMTABNO";
2335 case DT_SCORE_GOTSYM: return "SCORE_GOTSYM";
2336 case DT_SCORE_UNREFEXTNO: return "SCORE_UNREFEXTNO";
2337 case DT_SCORE_HIPAGENO: return "SCORE_HIPAGENO";
2338 default: return NULL;
2339 }
2340 }
2341
2342 static const char *
2343 get_tic6x_dynamic_type (unsigned long type)
2344 {
2345 switch (type)
2346 {
2347 case DT_C6000_GSYM_OFFSET: return "C6000_GSYM_OFFSET";
2348 case DT_C6000_GSTR_OFFSET: return "C6000_GSTR_OFFSET";
2349 case DT_C6000_DSBT_BASE: return "C6000_DSBT_BASE";
2350 case DT_C6000_DSBT_SIZE: return "C6000_DSBT_SIZE";
2351 case DT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
2352 case DT_C6000_DSBT_INDEX: return "C6000_DSBT_INDEX";
2353 default: return NULL;
2354 }
2355 }
2356
2357 static const char *
2358 get_nios2_dynamic_type (unsigned long type)
2359 {
2360 switch (type)
2361 {
2362 case DT_NIOS2_GP: return "NIOS2_GP";
2363 default: return NULL;
2364 }
2365 }
2366
2367 static const char *
2368 get_solaris_dynamic_type (unsigned long type)
2369 {
2370 switch (type)
2371 {
2372 case 0x6000000d: return "SUNW_AUXILIARY";
2373 case 0x6000000e: return "SUNW_RTLDINF";
2374 case 0x6000000f: return "SUNW_FILTER";
2375 case 0x60000010: return "SUNW_CAP";
2376 case 0x60000011: return "SUNW_SYMTAB";
2377 case 0x60000012: return "SUNW_SYMSZ";
2378 case 0x60000013: return "SUNW_SORTENT";
2379 case 0x60000014: return "SUNW_SYMSORT";
2380 case 0x60000015: return "SUNW_SYMSORTSZ";
2381 case 0x60000016: return "SUNW_TLSSORT";
2382 case 0x60000017: return "SUNW_TLSSORTSZ";
2383 case 0x60000018: return "SUNW_CAPINFO";
2384 case 0x60000019: return "SUNW_STRPAD";
2385 case 0x6000001a: return "SUNW_CAPCHAIN";
2386 case 0x6000001b: return "SUNW_LDMACH";
2387 case 0x6000001d: return "SUNW_CAPCHAINENT";
2388 case 0x6000001f: return "SUNW_CAPCHAINSZ";
2389 case 0x60000021: return "SUNW_PARENT";
2390 case 0x60000023: return "SUNW_ASLR";
2391 case 0x60000025: return "SUNW_RELAX";
2392 case 0x60000029: return "SUNW_NXHEAP";
2393 case 0x6000002b: return "SUNW_NXSTACK";
2394
2395 case 0x70000001: return "SPARC_REGISTER";
2396 case 0x7ffffffd: return "AUXILIARY";
2397 case 0x7ffffffe: return "USED";
2398 case 0x7fffffff: return "FILTER";
2399
2400 default: return NULL;
2401 }
2402 }
2403
2404 static const char *
2405 get_riscv_dynamic_type (unsigned long type)
2406 {
2407 switch (type)
2408 {
2409 case DT_RISCV_VARIANT_CC: return "RISCV_VARIANT_CC";
2410 default:
2411 return NULL;
2412 }
2413 }
2414
2415 static const char *
2416 get_dynamic_type (Filedata * filedata, unsigned long type)
2417 {
2418 static char buff[64];
2419
2420 switch (type)
2421 {
2422 case DT_NULL: return "NULL";
2423 case DT_NEEDED: return "NEEDED";
2424 case DT_PLTRELSZ: return "PLTRELSZ";
2425 case DT_PLTGOT: return "PLTGOT";
2426 case DT_HASH: return "HASH";
2427 case DT_STRTAB: return "STRTAB";
2428 case DT_SYMTAB: return "SYMTAB";
2429 case DT_RELA: return "RELA";
2430 case DT_RELASZ: return "RELASZ";
2431 case DT_RELAENT: return "RELAENT";
2432 case DT_STRSZ: return "STRSZ";
2433 case DT_SYMENT: return "SYMENT";
2434 case DT_INIT: return "INIT";
2435 case DT_FINI: return "FINI";
2436 case DT_SONAME: return "SONAME";
2437 case DT_RPATH: return "RPATH";
2438 case DT_SYMBOLIC: return "SYMBOLIC";
2439 case DT_REL: return "REL";
2440 case DT_RELSZ: return "RELSZ";
2441 case DT_RELENT: return "RELENT";
2442 case DT_RELR: return "RELR";
2443 case DT_RELRSZ: return "RELRSZ";
2444 case DT_RELRENT: return "RELRENT";
2445 case DT_PLTREL: return "PLTREL";
2446 case DT_DEBUG: return "DEBUG";
2447 case DT_TEXTREL: return "TEXTREL";
2448 case DT_JMPREL: return "JMPREL";
2449 case DT_BIND_NOW: return "BIND_NOW";
2450 case DT_INIT_ARRAY: return "INIT_ARRAY";
2451 case DT_FINI_ARRAY: return "FINI_ARRAY";
2452 case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
2453 case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
2454 case DT_RUNPATH: return "RUNPATH";
2455 case DT_FLAGS: return "FLAGS";
2456
2457 case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
2458 case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
2459 case DT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
2460
2461 case DT_CHECKSUM: return "CHECKSUM";
2462 case DT_PLTPADSZ: return "PLTPADSZ";
2463 case DT_MOVEENT: return "MOVEENT";
2464 case DT_MOVESZ: return "MOVESZ";
2465 case DT_FEATURE: return "FEATURE";
2466 case DT_POSFLAG_1: return "POSFLAG_1";
2467 case DT_SYMINSZ: return "SYMINSZ";
2468 case DT_SYMINENT: return "SYMINENT"; /* aka VALRNGHI */
2469
2470 case DT_ADDRRNGLO: return "ADDRRNGLO";
2471 case DT_CONFIG: return "CONFIG";
2472 case DT_DEPAUDIT: return "DEPAUDIT";
2473 case DT_AUDIT: return "AUDIT";
2474 case DT_PLTPAD: return "PLTPAD";
2475 case DT_MOVETAB: return "MOVETAB";
2476 case DT_SYMINFO: return "SYMINFO"; /* aka ADDRRNGHI */
2477
2478 case DT_VERSYM: return "VERSYM";
2479
2480 case DT_TLSDESC_GOT: return "TLSDESC_GOT";
2481 case DT_TLSDESC_PLT: return "TLSDESC_PLT";
2482 case DT_RELACOUNT: return "RELACOUNT";
2483 case DT_RELCOUNT: return "RELCOUNT";
2484 case DT_FLAGS_1: return "FLAGS_1";
2485 case DT_VERDEF: return "VERDEF";
2486 case DT_VERDEFNUM: return "VERDEFNUM";
2487 case DT_VERNEED: return "VERNEED";
2488 case DT_VERNEEDNUM: return "VERNEEDNUM";
2489
2490 case DT_AUXILIARY: return "AUXILIARY";
2491 case DT_USED: return "USED";
2492 case DT_FILTER: return "FILTER";
2493
2494 case DT_GNU_PRELINKED: return "GNU_PRELINKED";
2495 case DT_GNU_CONFLICT: return "GNU_CONFLICT";
2496 case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
2497 case DT_GNU_LIBLIST: return "GNU_LIBLIST";
2498 case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
2499 case DT_GNU_HASH: return "GNU_HASH";
2500 case DT_GNU_FLAGS_1: return "GNU_FLAGS_1";
2501
2502 default:
2503 if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
2504 {
2505 const char * result;
2506
2507 switch (filedata->file_header.e_machine)
2508 {
2509 case EM_AARCH64:
2510 result = get_aarch64_dynamic_type (type);
2511 break;
2512 case EM_MIPS:
2513 case EM_MIPS_RS3_LE:
2514 result = get_mips_dynamic_type (type);
2515 break;
2516 case EM_SPARCV9:
2517 result = get_sparc64_dynamic_type (type);
2518 break;
2519 case EM_PPC:
2520 result = get_ppc_dynamic_type (type);
2521 break;
2522 case EM_PPC64:
2523 result = get_ppc64_dynamic_type (type);
2524 break;
2525 case EM_IA_64:
2526 result = get_ia64_dynamic_type (type);
2527 break;
2528 case EM_ALPHA:
2529 result = get_alpha_dynamic_type (type);
2530 break;
2531 case EM_SCORE:
2532 result = get_score_dynamic_type (type);
2533 break;
2534 case EM_TI_C6000:
2535 result = get_tic6x_dynamic_type (type);
2536 break;
2537 case EM_ALTERA_NIOS2:
2538 result = get_nios2_dynamic_type (type);
2539 break;
2540 case EM_RISCV:
2541 result = get_riscv_dynamic_type (type);
2542 break;
2543 default:
2544 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2545 result = get_solaris_dynamic_type (type);
2546 else
2547 result = NULL;
2548 break;
2549 }
2550
2551 if (result != NULL)
2552 return result;
2553
2554 snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
2555 }
2556 else if (((type >= DT_LOOS) && (type <= DT_HIOS))
2557 || (filedata->file_header.e_machine == EM_PARISC
2558 && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
2559 {
2560 const char * result;
2561
2562 switch (filedata->file_header.e_machine)
2563 {
2564 case EM_PARISC:
2565 result = get_parisc_dynamic_type (type);
2566 break;
2567 case EM_IA_64:
2568 result = get_ia64_dynamic_type (type);
2569 break;
2570 default:
2571 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2572 result = get_solaris_dynamic_type (type);
2573 else
2574 result = NULL;
2575 break;
2576 }
2577
2578 if (result != NULL)
2579 return result;
2580
2581 snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
2582 type);
2583 }
2584 else
2585 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
2586
2587 return buff;
2588 }
2589 }
2590
2591 static bool get_program_headers (Filedata *);
2592 static bool get_dynamic_section (Filedata *);
2593
2594 static void
2595 locate_dynamic_section (Filedata *filedata)
2596 {
2597 unsigned long dynamic_addr = 0;
2598 uint64_t dynamic_size = 0;
2599
2600 if (filedata->file_header.e_phnum != 0
2601 && get_program_headers (filedata))
2602 {
2603 Elf_Internal_Phdr *segment;
2604 unsigned int i;
2605
2606 for (i = 0, segment = filedata->program_headers;
2607 i < filedata->file_header.e_phnum;
2608 i++, segment++)
2609 {
2610 if (segment->p_type == PT_DYNAMIC)
2611 {
2612 dynamic_addr = segment->p_offset;
2613 dynamic_size = segment->p_filesz;
2614
2615 if (filedata->section_headers != NULL)
2616 {
2617 Elf_Internal_Shdr *sec;
2618
2619 sec = find_section (filedata, ".dynamic");
2620 if (sec != NULL)
2621 {
2622 if (sec->sh_size == 0
2623 || sec->sh_type == SHT_NOBITS)
2624 {
2625 dynamic_addr = 0;
2626 dynamic_size = 0;
2627 }
2628 else
2629 {
2630 dynamic_addr = sec->sh_offset;
2631 dynamic_size = sec->sh_size;
2632 }
2633 }
2634 }
2635
2636 if (dynamic_addr > filedata->file_size
2637 || (dynamic_size > filedata->file_size - dynamic_addr))
2638 {
2639 dynamic_addr = 0;
2640 dynamic_size = 0;
2641 }
2642 break;
2643 }
2644 }
2645 }
2646 filedata->dynamic_addr = dynamic_addr;
2647 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
2648 }
2649
2650 static bool
2651 is_pie (Filedata *filedata)
2652 {
2653 Elf_Internal_Dyn *entry;
2654
2655 if (filedata->dynamic_size == 0)
2656 locate_dynamic_section (filedata);
2657 if (filedata->dynamic_size <= 1)
2658 return false;
2659
2660 if (!get_dynamic_section (filedata))
2661 return false;
2662
2663 for (entry = filedata->dynamic_section;
2664 entry < filedata->dynamic_section + filedata->dynamic_nent;
2665 entry++)
2666 {
2667 if (entry->d_tag == DT_FLAGS_1)
2668 {
2669 if ((entry->d_un.d_val & DF_1_PIE) != 0)
2670 return true;
2671 break;
2672 }
2673 }
2674 return false;
2675 }
2676
2677 static char *
2678 get_file_type (Filedata *filedata)
2679 {
2680 unsigned e_type = filedata->file_header.e_type;
2681 static char buff[64];
2682
2683 switch (e_type)
2684 {
2685 case ET_NONE: return _("NONE (None)");
2686 case ET_REL: return _("REL (Relocatable file)");
2687 case ET_EXEC: return _("EXEC (Executable file)");
2688 case ET_DYN:
2689 if (is_pie (filedata))
2690 return _("DYN (Position-Independent Executable file)");
2691 else
2692 return _("DYN (Shared object file)");
2693 case ET_CORE: return _("CORE (Core file)");
2694
2695 default:
2696 if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
2697 snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
2698 else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
2699 snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
2700 else
2701 snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
2702 return buff;
2703 }
2704 }
2705
2706 static char *
2707 get_machine_name (unsigned e_machine)
2708 {
2709 static char buff[64]; /* XXX */
2710
2711 switch (e_machine)
2712 {
2713 /* Please keep this switch table sorted by increasing EM_ value. */
2714 /* 0 */
2715 case EM_NONE: return _("None");
2716 case EM_M32: return "WE32100";
2717 case EM_SPARC: return "Sparc";
2718 case EM_386: return "Intel 80386";
2719 case EM_68K: return "MC68000";
2720 case EM_88K: return "MC88000";
2721 case EM_IAMCU: return "Intel MCU";
2722 case EM_860: return "Intel 80860";
2723 case EM_MIPS: return "MIPS R3000";
2724 case EM_S370: return "IBM System/370";
2725 /* 10 */
2726 case EM_MIPS_RS3_LE: return "MIPS R4000 big-endian";
2727 case EM_OLD_SPARCV9: return "Sparc v9 (old)";
2728 case EM_PARISC: return "HPPA";
2729 case EM_VPP550: return "Fujitsu VPP500";
2730 case EM_SPARC32PLUS: return "Sparc v8+" ;
2731 case EM_960: return "Intel 80960";
2732 case EM_PPC: return "PowerPC";
2733 /* 20 */
2734 case EM_PPC64: return "PowerPC64";
2735 case EM_S390_OLD:
2736 case EM_S390: return "IBM S/390";
2737 case EM_SPU: return "SPU";
2738 /* 30 */
2739 case EM_V800: return "Renesas V850 (using RH850 ABI)";
2740 case EM_FR20: return "Fujitsu FR20";
2741 case EM_RH32: return "TRW RH32";
2742 case EM_MCORE: return "MCORE";
2743 /* 40 */
2744 case EM_ARM: return "ARM";
2745 case EM_OLD_ALPHA: return "Digital Alpha (old)";
2746 case EM_SH: return "Renesas / SuperH SH";
2747 case EM_SPARCV9: return "Sparc v9";
2748 case EM_TRICORE: return "Siemens Tricore";
2749 case EM_ARC: return "ARC";
2750 case EM_H8_300: return "Renesas H8/300";
2751 case EM_H8_300H: return "Renesas H8/300H";
2752 case EM_H8S: return "Renesas H8S";
2753 case EM_H8_500: return "Renesas H8/500";
2754 /* 50 */
2755 case EM_IA_64: return "Intel IA-64";
2756 case EM_MIPS_X: return "Stanford MIPS-X";
2757 case EM_COLDFIRE: return "Motorola Coldfire";
2758 case EM_68HC12: return "Motorola MC68HC12 Microcontroller";
2759 case EM_MMA: return "Fujitsu Multimedia Accelerator";
2760 case EM_PCP: return "Siemens PCP";
2761 case EM_NCPU: return "Sony nCPU embedded RISC processor";
2762 case EM_NDR1: return "Denso NDR1 microprocesspr";
2763 case EM_STARCORE: return "Motorola Star*Core processor";
2764 case EM_ME16: return "Toyota ME16 processor";
2765 /* 60 */
2766 case EM_ST100: return "STMicroelectronics ST100 processor";
2767 case EM_TINYJ: return "Advanced Logic Corp. TinyJ embedded processor";
2768 case EM_X86_64: return "Advanced Micro Devices X86-64";
2769 case EM_PDSP: return "Sony DSP processor";
2770 case EM_PDP10: return "Digital Equipment Corp. PDP-10";
2771 case EM_PDP11: return "Digital Equipment Corp. PDP-11";
2772 case EM_FX66: return "Siemens FX66 microcontroller";
2773 case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 bit microcontroller";
2774 case EM_ST7: return "STMicroelectronics ST7 8-bit microcontroller";
2775 case EM_68HC16: return "Motorola MC68HC16 Microcontroller";
2776 /* 70 */
2777 case EM_68HC11: return "Motorola MC68HC11 Microcontroller";
2778 case EM_68HC08: return "Motorola MC68HC08 Microcontroller";
2779 case EM_68HC05: return "Motorola MC68HC05 Microcontroller";
2780 case EM_SVX: return "Silicon Graphics SVx";
2781 case EM_ST19: return "STMicroelectronics ST19 8-bit microcontroller";
2782 case EM_VAX: return "Digital VAX";
2783 case EM_CRIS: return "Axis Communications 32-bit embedded processor";
2784 case EM_JAVELIN: return "Infineon Technologies 32-bit embedded cpu";
2785 case EM_FIREPATH: return "Element 14 64-bit DSP processor";
2786 case EM_ZSP: return "LSI Logic's 16-bit DSP processor";
2787 /* 80 */
2788 case EM_MMIX: return "Donald Knuth's educational 64-bit processor";
2789 case EM_HUANY: return "Harvard Universitys's machine-independent object format";
2790 case EM_PRISM: return "Vitesse Prism";
2791 case EM_AVR_OLD:
2792 case EM_AVR: return "Atmel AVR 8-bit microcontroller";
2793 case EM_CYGNUS_FR30:
2794 case EM_FR30: return "Fujitsu FR30";
2795 case EM_CYGNUS_D10V:
2796 case EM_D10V: return "d10v";
2797 case EM_CYGNUS_D30V:
2798 case EM_D30V: return "d30v";
2799 case EM_CYGNUS_V850:
2800 case EM_V850: return "Renesas V850";
2801 case EM_CYGNUS_M32R:
2802 case EM_M32R: return "Renesas M32R (formerly Mitsubishi M32r)";
2803 case EM_CYGNUS_MN10300:
2804 case EM_MN10300: return "mn10300";
2805 /* 90 */
2806 case EM_CYGNUS_MN10200:
2807 case EM_MN10200: return "mn10200";
2808 case EM_PJ: return "picoJava";
2809 case EM_OR1K: return "OpenRISC 1000";
2810 case EM_ARC_COMPACT: return "ARCompact";
2811 case EM_XTENSA_OLD:
2812 case EM_XTENSA: return "Tensilica Xtensa Processor";
2813 case EM_VIDEOCORE: return "Alphamosaic VideoCore processor";
2814 case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor";
2815 case EM_NS32K: return "National Semiconductor 32000 series";
2816 case EM_TPC: return "Tenor Network TPC processor";
2817 case EM_SNP1K: return "Trebia SNP 1000 processor";
2818 /* 100 */
2819 case EM_ST200: return "STMicroelectronics ST200 microcontroller";
2820 case EM_IP2K_OLD:
2821 case EM_IP2K: return "Ubicom IP2xxx 8-bit microcontrollers";
2822 case EM_MAX: return "MAX Processor";
2823 case EM_CR: return "National Semiconductor CompactRISC";
2824 case EM_F2MC16: return "Fujitsu F2MC16";
2825 case EM_MSP430: return "Texas Instruments msp430 microcontroller";
2826 case EM_BLACKFIN: return "Analog Devices Blackfin";
2827 case EM_SE_C33: return "S1C33 Family of Seiko Epson processors";
2828 case EM_SEP: return "Sharp embedded microprocessor";
2829 case EM_ARCA: return "Arca RISC microprocessor";
2830 /* 110 */
2831 case EM_UNICORE: return "Unicore";
2832 case EM_EXCESS: return "eXcess 16/32/64-bit configurable embedded CPU";
2833 case EM_DXP: return "Icera Semiconductor Inc. Deep Execution Processor";
2834 case EM_ALTERA_NIOS2: return "Altera Nios II";
2835 case EM_CRX: return "National Semiconductor CRX microprocessor";
2836 case EM_XGATE: return "Motorola XGATE embedded processor";
2837 case EM_C166:
2838 case EM_XC16X: return "Infineon Technologies xc16x";
2839 case EM_M16C: return "Renesas M16C series microprocessors";
2840 case EM_DSPIC30F: return "Microchip Technology dsPIC30F Digital Signal Controller";
2841 case EM_CE: return "Freescale Communication Engine RISC core";
2842 /* 120 */
2843 case EM_M32C: return "Renesas M32c";
2844 /* 130 */
2845 case EM_TSK3000: return "Altium TSK3000 core";
2846 case EM_RS08: return "Freescale RS08 embedded processor";
2847 case EM_ECOG2: return "Cyan Technology eCOG2 microprocessor";
2848 case EM_SCORE: return "SUNPLUS S+Core";
2849 case EM_DSP24: return "New Japan Radio (NJR) 24-bit DSP Processor";
2850 case EM_VIDEOCORE3: return "Broadcom VideoCore III processor";
2851 case EM_LATTICEMICO32: return "Lattice Mico32";
2852 case EM_SE_C17: return "Seiko Epson C17 family";
2853 /* 140 */
2854 case EM_TI_C6000: return "Texas Instruments TMS320C6000 DSP family";
2855 case EM_TI_C2000: return "Texas Instruments TMS320C2000 DSP family";
2856 case EM_TI_C5500: return "Texas Instruments TMS320C55x DSP family";
2857 case EM_TI_PRU: return "TI PRU I/O processor";
2858 /* 160 */
2859 case EM_MMDSP_PLUS: return "STMicroelectronics 64bit VLIW Data Signal Processor";
2860 case EM_CYPRESS_M8C: return "Cypress M8C microprocessor";
2861 case EM_R32C: return "Renesas R32C series microprocessors";
2862 case EM_TRIMEDIA: return "NXP Semiconductors TriMedia architecture family";
2863 case EM_QDSP6: return "QUALCOMM DSP6 Processor";
2864 case EM_8051: return "Intel 8051 and variants";
2865 case EM_STXP7X: return "STMicroelectronics STxP7x family";
2866 case EM_NDS32: return "Andes Technology compact code size embedded RISC processor family";
2867 case EM_ECOG1X: return "Cyan Technology eCOG1X family";
2868 case EM_MAXQ30: return "Dallas Semiconductor MAXQ30 Core microcontrollers";
2869 /* 170 */
2870 case EM_XIMO16: return "New Japan Radio (NJR) 16-bit DSP Processor";
2871 case EM_MANIK: return "M2000 Reconfigurable RISC Microprocessor";
2872 case EM_CRAYNV2: return "Cray Inc. NV2 vector architecture";
2873 case EM_RX: return "Renesas RX";
2874 case EM_METAG: return "Imagination Technologies Meta processor architecture";
2875 case EM_MCST_ELBRUS: return "MCST Elbrus general purpose hardware architecture";
2876 case EM_ECOG16: return "Cyan Technology eCOG16 family";
2877 case EM_CR16:
2878 case EM_MICROBLAZE:
2879 case EM_MICROBLAZE_OLD: return "Xilinx MicroBlaze";
2880 case EM_ETPU: return "Freescale Extended Time Processing Unit";
2881 case EM_SLE9X: return "Infineon Technologies SLE9X core";
2882 /* 180 */
2883 case EM_L1OM: return "Intel L1OM";
2884 case EM_K1OM: return "Intel K1OM";
2885 case EM_INTEL182: return "Intel (reserved)";
2886 case EM_AARCH64: return "AArch64";
2887 case EM_ARM184: return "ARM (reserved)";
2888 case EM_AVR32: return "Atmel Corporation 32-bit microprocessor";
2889 case EM_STM8: return "STMicroeletronics STM8 8-bit microcontroller";
2890 case EM_TILE64: return "Tilera TILE64 multicore architecture family";
2891 case EM_TILEPRO: return "Tilera TILEPro multicore architecture family";
2892 /* 190 */
2893 case EM_CUDA: return "NVIDIA CUDA architecture";
2894 case EM_TILEGX: return "Tilera TILE-Gx multicore architecture family";
2895 case EM_CLOUDSHIELD: return "CloudShield architecture family";
2896 case EM_COREA_1ST: return "KIPO-KAIST Core-A 1st generation processor family";
2897 case EM_COREA_2ND: return "KIPO-KAIST Core-A 2nd generation processor family";
2898 case EM_ARC_COMPACT2: return "ARCv2";
2899 case EM_OPEN8: return "Open8 8-bit RISC soft processor core";
2900 case EM_RL78: return "Renesas RL78";
2901 case EM_VIDEOCORE5: return "Broadcom VideoCore V processor";
2902 case EM_78K0R: return "Renesas 78K0R";
2903 /* 200 */
2904 case EM_56800EX: return "Freescale 56800EX Digital Signal Controller (DSC)";
2905 case EM_BA1: return "Beyond BA1 CPU architecture";
2906 case EM_BA2: return "Beyond BA2 CPU architecture";
2907 case EM_XCORE: return "XMOS xCORE processor family";
2908 case EM_MCHP_PIC: return "Microchip 8-bit PIC(r) family";
2909 case EM_INTELGT: return "Intel Graphics Technology";
2910 /* 210 */
2911 case EM_KM32: return "KM211 KM32 32-bit processor";
2912 case EM_KMX32: return "KM211 KMX32 32-bit processor";
2913 case EM_KMX16: return "KM211 KMX16 16-bit processor";
2914 case EM_KMX8: return "KM211 KMX8 8-bit processor";
2915 case EM_KVARC: return "KM211 KVARC processor";
2916 case EM_CDP: return "Paneve CDP architecture family";
2917 case EM_COGE: return "Cognitive Smart Memory Processor";
2918 case EM_COOL: return "Bluechip Systems CoolEngine";
2919 case EM_NORC: return "Nanoradio Optimized RISC";
2920 case EM_CSR_KALIMBA: return "CSR Kalimba architecture family";
2921 /* 220 */
2922 case EM_Z80: return "Zilog Z80";
2923 case EM_VISIUM: return "CDS VISIUMcore processor";
2924 case EM_FT32: return "FTDI Chip FT32";
2925 case EM_MOXIE: return "Moxie";
2926 case EM_AMDGPU: return "AMD GPU";
2927 /* 230 (all reserved) */
2928 /* 240 */
2929 case EM_RISCV: return "RISC-V";
2930 case EM_LANAI: return "Lanai 32-bit processor";
2931 case EM_CEVA: return "CEVA Processor Architecture Family";
2932 case EM_CEVA_X2: return "CEVA X2 Processor Family";
2933 case EM_BPF: return "Linux BPF";
2934 case EM_GRAPHCORE_IPU: return "Graphcore Intelligent Processing Unit";
2935 case EM_IMG1: return "Imagination Technologies";
2936 /* 250 */
2937 case EM_NFP: return "Netronome Flow Processor";
2938 case EM_VE: return "NEC Vector Engine";
2939 case EM_CSKY: return "C-SKY";
2940 case EM_ARC_COMPACT3_64: return "Synopsys ARCv2.3 64-bit";
2941 case EM_MCS6502: return "MOS Technology MCS 6502 processor";
2942 case EM_ARC_COMPACT3: return "Synopsys ARCv2.3 32-bit";
2943 case EM_KVX: return "Kalray VLIW core of the MPPA processor family";
2944 case EM_65816: return "WDC 65816/65C816";
2945 case EM_LOONGARCH: return "LoongArch";
2946 case EM_KF32: return "ChipON KungFu32";
2947
2948 /* Large numbers... */
2949 case EM_MT: return "Morpho Techologies MT processor";
2950 case EM_ALPHA: return "Alpha";
2951 case EM_WEBASSEMBLY: return "Web Assembly";
2952 case EM_DLX: return "OpenDLX";
2953 case EM_XSTORMY16: return "Sanyo XStormy16 CPU core";
2954 case EM_IQ2000: return "Vitesse IQ2000";
2955 case EM_M32C_OLD:
2956 case EM_NIOS32: return "Altera Nios";
2957 case EM_CYGNUS_MEP: return "Toshiba MeP Media Engine";
2958 case EM_ADAPTEVA_EPIPHANY: return "Adapteva EPIPHANY";
2959 case EM_CYGNUS_FRV: return "Fujitsu FR-V";
2960 case EM_S12Z: return "Freescale S12Z";
2961
2962 default:
2963 snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
2964 return buff;
2965 }
2966 }
2967
2968 static void
2969 decode_ARC_machine_flags (unsigned e_flags, unsigned e_machine, char buf[])
2970 {
2971 /* ARC has two machine types EM_ARC_COMPACT and EM_ARC_COMPACT2. Some
2972 other compilers don't specify an architecture type in the e_flags, and
2973 instead use EM_ARC_COMPACT for old ARC600, ARC601, and ARC700
2974 architectures, and switch to EM_ARC_COMPACT2 for newer ARCEM and ARCHS
2975 architectures.
2976
2977 Th GNU tools follows this use of EM_ARC_COMPACT and EM_ARC_COMPACT2,
2978 but also sets a specific architecture type in the e_flags field.
2979
2980 However, when decoding the flags we don't worry if we see an
2981 unexpected pairing, for example EM_ARC_COMPACT machine type, with
2982 ARCEM architecture type. */
2983
2984 switch (e_flags & EF_ARC_MACH_MSK)
2985 {
2986 /* We only expect these to occur for EM_ARC_COMPACT2. */
2987 case EF_ARC_CPU_ARCV2EM:
2988 strcat (buf, ", ARC EM");
2989 break;
2990 case EF_ARC_CPU_ARCV2HS:
2991 strcat (buf, ", ARC HS");
2992 break;
2993
2994 /* We only expect these to occur for EM_ARC_COMPACT. */
2995 case E_ARC_MACH_ARC600:
2996 strcat (buf, ", ARC600");
2997 break;
2998 case E_ARC_MACH_ARC601:
2999 strcat (buf, ", ARC601");
3000 break;
3001 case E_ARC_MACH_ARC700:
3002 strcat (buf, ", ARC700");
3003 break;
3004
3005 /* The only times we should end up here are (a) A corrupt ELF, (b) A
3006 new ELF with new architecture being read by an old version of
3007 readelf, or (c) An ELF built with non-GNU compiler that does not
3008 set the architecture in the e_flags. */
3009 default:
3010 if (e_machine == EM_ARC_COMPACT)
3011 strcat (buf, ", Unknown ARCompact");
3012 else
3013 strcat (buf, ", Unknown ARC");
3014 break;
3015 }
3016
3017 switch (e_flags & EF_ARC_OSABI_MSK)
3018 {
3019 case E_ARC_OSABI_ORIG:
3020 strcat (buf, ", (ABI:legacy)");
3021 break;
3022 case E_ARC_OSABI_V2:
3023 strcat (buf, ", (ABI:v2)");
3024 break;
3025 /* Only upstream 3.9+ kernels will support ARCv2 ISA. */
3026 case E_ARC_OSABI_V3:
3027 strcat (buf, ", v3 no-legacy-syscalls ABI");
3028 break;
3029 case E_ARC_OSABI_V4:
3030 strcat (buf, ", v4 ABI");
3031 break;
3032 default:
3033 strcat (buf, ", unrecognised ARC OSABI flag");
3034 break;
3035 }
3036 }
3037
3038 static void
3039 decode_ARM_machine_flags (unsigned e_flags, char buf[])
3040 {
3041 unsigned eabi;
3042 bool unknown = false;
3043
3044 eabi = EF_ARM_EABI_VERSION (e_flags);
3045 e_flags &= ~ EF_ARM_EABIMASK;
3046
3047 /* Handle "generic" ARM flags. */
3048 if (e_flags & EF_ARM_RELEXEC)
3049 {
3050 strcat (buf, ", relocatable executable");
3051 e_flags &= ~ EF_ARM_RELEXEC;
3052 }
3053
3054 if (e_flags & EF_ARM_PIC)
3055 {
3056 strcat (buf, ", position independent");
3057 e_flags &= ~ EF_ARM_PIC;
3058 }
3059
3060 /* Now handle EABI specific flags. */
3061 switch (eabi)
3062 {
3063 default:
3064 strcat (buf, ", <unrecognized EABI>");
3065 if (e_flags)
3066 unknown = true;
3067 break;
3068
3069 case EF_ARM_EABI_VER1:
3070 strcat (buf, ", Version1 EABI");
3071 while (e_flags)
3072 {
3073 unsigned flag;
3074
3075 /* Process flags one bit at a time. */
3076 flag = e_flags & - e_flags;
3077 e_flags &= ~ flag;
3078
3079 switch (flag)
3080 {
3081 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
3082 strcat (buf, ", sorted symbol tables");
3083 break;
3084
3085 default:
3086 unknown = true;
3087 break;
3088 }
3089 }
3090 break;
3091
3092 case EF_ARM_EABI_VER2:
3093 strcat (buf, ", Version2 EABI");
3094 while (e_flags)
3095 {
3096 unsigned flag;
3097
3098 /* Process flags one bit at a time. */
3099 flag = e_flags & - e_flags;
3100 e_flags &= ~ flag;
3101
3102 switch (flag)
3103 {
3104 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
3105 strcat (buf, ", sorted symbol tables");
3106 break;
3107
3108 case EF_ARM_DYNSYMSUSESEGIDX:
3109 strcat (buf, ", dynamic symbols use segment index");
3110 break;
3111
3112 case EF_ARM_MAPSYMSFIRST:
3113 strcat (buf, ", mapping symbols precede others");
3114 break;
3115
3116 default:
3117 unknown = true;
3118 break;
3119 }
3120 }
3121 break;
3122
3123 case EF_ARM_EABI_VER3:
3124 strcat (buf, ", Version3 EABI");
3125 break;
3126
3127 case EF_ARM_EABI_VER4:
3128 strcat (buf, ", Version4 EABI");
3129 while (e_flags)
3130 {
3131 unsigned flag;
3132
3133 /* Process flags one bit at a time. */
3134 flag = e_flags & - e_flags;
3135 e_flags &= ~ flag;
3136
3137 switch (flag)
3138 {
3139 case EF_ARM_BE8:
3140 strcat (buf, ", BE8");
3141 break;
3142
3143 case EF_ARM_LE8:
3144 strcat (buf, ", LE8");
3145 break;
3146
3147 default:
3148 unknown = true;
3149 break;
3150 }
3151 }
3152 break;
3153
3154 case EF_ARM_EABI_VER5:
3155 strcat (buf, ", Version5 EABI");
3156 while (e_flags)
3157 {
3158 unsigned flag;
3159
3160 /* Process flags one bit at a time. */
3161 flag = e_flags & - e_flags;
3162 e_flags &= ~ flag;
3163
3164 switch (flag)
3165 {
3166 case EF_ARM_BE8:
3167 strcat (buf, ", BE8");
3168 break;
3169
3170 case EF_ARM_LE8:
3171 strcat (buf, ", LE8");
3172 break;
3173
3174 case EF_ARM_ABI_FLOAT_SOFT: /* Conflicts with EF_ARM_SOFT_FLOAT. */
3175 strcat (buf, ", soft-float ABI");
3176 break;
3177
3178 case EF_ARM_ABI_FLOAT_HARD: /* Conflicts with EF_ARM_VFP_FLOAT. */
3179 strcat (buf, ", hard-float ABI");
3180 break;
3181
3182 default:
3183 unknown = true;
3184 break;
3185 }
3186 }
3187 break;
3188
3189 case EF_ARM_EABI_UNKNOWN:
3190 strcat (buf, ", GNU EABI");
3191 while (e_flags)
3192 {
3193 unsigned flag;
3194
3195 /* Process flags one bit at a time. */
3196 flag = e_flags & - e_flags;
3197 e_flags &= ~ flag;
3198
3199 switch (flag)
3200 {
3201 case EF_ARM_INTERWORK:
3202 strcat (buf, ", interworking enabled");
3203 break;
3204
3205 case EF_ARM_APCS_26:
3206 strcat (buf, ", uses APCS/26");
3207 break;
3208
3209 case EF_ARM_APCS_FLOAT:
3210 strcat (buf, ", uses APCS/float");
3211 break;
3212
3213 case EF_ARM_PIC:
3214 strcat (buf, ", position independent");
3215 break;
3216
3217 case EF_ARM_ALIGN8:
3218 strcat (buf, ", 8 bit structure alignment");
3219 break;
3220
3221 case EF_ARM_NEW_ABI:
3222 strcat (buf, ", uses new ABI");
3223 break;
3224
3225 case EF_ARM_OLD_ABI:
3226 strcat (buf, ", uses old ABI");
3227 break;
3228
3229 case EF_ARM_SOFT_FLOAT:
3230 strcat (buf, ", software FP");
3231 break;
3232
3233 case EF_ARM_VFP_FLOAT:
3234 strcat (buf, ", VFP");
3235 break;
3236
3237 case EF_ARM_MAVERICK_FLOAT:
3238 strcat (buf, ", Maverick FP");
3239 break;
3240
3241 default:
3242 unknown = true;
3243 break;
3244 }
3245 }
3246 }
3247
3248 if (unknown)
3249 strcat (buf,_(", <unknown>"));
3250 }
3251
3252 static void
3253 decode_AVR_machine_flags (unsigned e_flags, char buf[], size_t size)
3254 {
3255 --size; /* Leave space for null terminator. */
3256
3257 switch (e_flags & EF_AVR_MACH)
3258 {
3259 case E_AVR_MACH_AVR1:
3260 strncat (buf, ", avr:1", size);
3261 break;
3262 case E_AVR_MACH_AVR2:
3263 strncat (buf, ", avr:2", size);
3264 break;
3265 case E_AVR_MACH_AVR25:
3266 strncat (buf, ", avr:25", size);
3267 break;
3268 case E_AVR_MACH_AVR3:
3269 strncat (buf, ", avr:3", size);
3270 break;
3271 case E_AVR_MACH_AVR31:
3272 strncat (buf, ", avr:31", size);
3273 break;
3274 case E_AVR_MACH_AVR35:
3275 strncat (buf, ", avr:35", size);
3276 break;
3277 case E_AVR_MACH_AVR4:
3278 strncat (buf, ", avr:4", size);
3279 break;
3280 case E_AVR_MACH_AVR5:
3281 strncat (buf, ", avr:5", size);
3282 break;
3283 case E_AVR_MACH_AVR51:
3284 strncat (buf, ", avr:51", size);
3285 break;
3286 case E_AVR_MACH_AVR6:
3287 strncat (buf, ", avr:6", size);
3288 break;
3289 case E_AVR_MACH_AVRTINY:
3290 strncat (buf, ", avr:100", size);
3291 break;
3292 case E_AVR_MACH_XMEGA1:
3293 strncat (buf, ", avr:101", size);
3294 break;
3295 case E_AVR_MACH_XMEGA2:
3296 strncat (buf, ", avr:102", size);
3297 break;
3298 case E_AVR_MACH_XMEGA3:
3299 strncat (buf, ", avr:103", size);
3300 break;
3301 case E_AVR_MACH_XMEGA4:
3302 strncat (buf, ", avr:104", size);
3303 break;
3304 case E_AVR_MACH_XMEGA5:
3305 strncat (buf, ", avr:105", size);
3306 break;
3307 case E_AVR_MACH_XMEGA6:
3308 strncat (buf, ", avr:106", size);
3309 break;
3310 case E_AVR_MACH_XMEGA7:
3311 strncat (buf, ", avr:107", size);
3312 break;
3313 default:
3314 strncat (buf, ", avr:<unknown>", size);
3315 break;
3316 }
3317
3318 size -= strlen (buf);
3319 if (e_flags & EF_AVR_LINKRELAX_PREPARED)
3320 strncat (buf, ", link-relax", size);
3321 }
3322
3323 static void
3324 decode_NDS32_machine_flags (unsigned e_flags, char buf[], size_t size)
3325 {
3326 unsigned abi;
3327 unsigned arch;
3328 unsigned config;
3329 unsigned version;
3330 bool has_fpu = false;
3331 unsigned int r = 0;
3332
3333 static const char *ABI_STRINGS[] =
3334 {
3335 "ABI v0", /* use r5 as return register; only used in N1213HC */
3336 "ABI v1", /* use r0 as return register */
3337 "ABI v2", /* use r0 as return register and don't reserve 24 bytes for arguments */
3338 "ABI v2fp", /* for FPU */
3339 "AABI",
3340 "ABI2 FP+"
3341 };
3342 static const char *VER_STRINGS[] =
3343 {
3344 "Andes ELF V1.3 or older",
3345 "Andes ELF V1.3.1",
3346 "Andes ELF V1.4"
3347 };
3348 static const char *ARCH_STRINGS[] =
3349 {
3350 "",
3351 "Andes Star v1.0",
3352 "Andes Star v2.0",
3353 "Andes Star v3.0",
3354 "Andes Star v3.0m"
3355 };
3356
3357 abi = EF_NDS_ABI & e_flags;
3358 arch = EF_NDS_ARCH & e_flags;
3359 config = EF_NDS_INST & e_flags;
3360 version = EF_NDS32_ELF_VERSION & e_flags;
3361
3362 memset (buf, 0, size);
3363
3364 switch (abi)
3365 {
3366 case E_NDS_ABI_V0:
3367 case E_NDS_ABI_V1:
3368 case E_NDS_ABI_V2:
3369 case E_NDS_ABI_V2FP:
3370 case E_NDS_ABI_AABI:
3371 case E_NDS_ABI_V2FP_PLUS:
3372 /* In case there are holes in the array. */
3373 r += snprintf (buf + r, size - r, ", %s", ABI_STRINGS[abi >> EF_NDS_ABI_SHIFT]);
3374 break;
3375
3376 default:
3377 r += snprintf (buf + r, size - r, ", <unrecognized ABI>");
3378 break;
3379 }
3380
3381 switch (version)
3382 {
3383 case E_NDS32_ELF_VER_1_2:
3384 case E_NDS32_ELF_VER_1_3:
3385 case E_NDS32_ELF_VER_1_4:
3386 r += snprintf (buf + r, size - r, ", %s", VER_STRINGS[version >> EF_NDS32_ELF_VERSION_SHIFT]);
3387 break;
3388
3389 default:
3390 r += snprintf (buf + r, size - r, ", <unrecognized ELF version number>");
3391 break;
3392 }
3393
3394 if (E_NDS_ABI_V0 == abi)
3395 {
3396 /* OLD ABI; only used in N1213HC, has performance extension 1. */
3397 r += snprintf (buf + r, size - r, ", Andes Star v1.0, N1213HC, MAC, PERF1");
3398 if (arch == E_NDS_ARCH_STAR_V1_0)
3399 r += snprintf (buf + r, size -r, ", 16b"); /* has 16-bit instructions */
3400 return;
3401 }
3402
3403 switch (arch)
3404 {
3405 case E_NDS_ARCH_STAR_V1_0:
3406 case E_NDS_ARCH_STAR_V2_0:
3407 case E_NDS_ARCH_STAR_V3_0:
3408 case E_NDS_ARCH_STAR_V3_M:
3409 r += snprintf (buf + r, size - r, ", %s", ARCH_STRINGS[arch >> EF_NDS_ARCH_SHIFT]);
3410 break;
3411
3412 default:
3413 r += snprintf (buf + r, size - r, ", <unrecognized architecture>");
3414 /* ARCH version determines how the e_flags are interpreted.
3415 If it is unknown, we cannot proceed. */
3416 return;
3417 }
3418
3419 /* Newer ABI; Now handle architecture specific flags. */
3420 if (arch == E_NDS_ARCH_STAR_V1_0)
3421 {
3422 if (config & E_NDS32_HAS_MFUSR_PC_INST)
3423 r += snprintf (buf + r, size -r, ", MFUSR_PC");
3424
3425 if (!(config & E_NDS32_HAS_NO_MAC_INST))
3426 r += snprintf (buf + r, size -r, ", MAC");
3427
3428 if (config & E_NDS32_HAS_DIV_INST)
3429 r += snprintf (buf + r, size -r, ", DIV");
3430
3431 if (config & E_NDS32_HAS_16BIT_INST)
3432 r += snprintf (buf + r, size -r, ", 16b");
3433 }
3434 else
3435 {
3436 if (config & E_NDS32_HAS_MFUSR_PC_INST)
3437 {
3438 if (version <= E_NDS32_ELF_VER_1_3)
3439 r += snprintf (buf + r, size -r, ", [B8]");
3440 else
3441 r += snprintf (buf + r, size -r, ", EX9");
3442 }
3443
3444 if (config & E_NDS32_HAS_MAC_DX_INST)
3445 r += snprintf (buf + r, size -r, ", MAC_DX");
3446
3447 if (config & E_NDS32_HAS_DIV_DX_INST)
3448 r += snprintf (buf + r, size -r, ", DIV_DX");
3449
3450 if (config & E_NDS32_HAS_16BIT_INST)
3451 {
3452 if (version <= E_NDS32_ELF_VER_1_3)
3453 r += snprintf (buf + r, size -r, ", 16b");
3454 else
3455 r += snprintf (buf + r, size -r, ", IFC");
3456 }
3457 }
3458
3459 if (config & E_NDS32_HAS_EXT_INST)
3460 r += snprintf (buf + r, size -r, ", PERF1");
3461
3462 if (config & E_NDS32_HAS_EXT2_INST)
3463 r += snprintf (buf + r, size -r, ", PERF2");
3464
3465 if (config & E_NDS32_HAS_FPU_INST)
3466 {
3467 has_fpu = true;
3468 r += snprintf (buf + r, size -r, ", FPU_SP");
3469 }
3470
3471 if (config & E_NDS32_HAS_FPU_DP_INST)
3472 {
3473 has_fpu = true;
3474 r += snprintf (buf + r, size -r, ", FPU_DP");
3475 }
3476
3477 if (config & E_NDS32_HAS_FPU_MAC_INST)
3478 {
3479 has_fpu = true;
3480 r += snprintf (buf + r, size -r, ", FPU_MAC");
3481 }
3482
3483 if (has_fpu)
3484 {
3485 switch ((config & E_NDS32_FPU_REG_CONF) >> E_NDS32_FPU_REG_CONF_SHIFT)
3486 {
3487 case E_NDS32_FPU_REG_8SP_4DP:
3488 r += snprintf (buf + r, size -r, ", FPU_REG:8/4");
3489 break;
3490 case E_NDS32_FPU_REG_16SP_8DP:
3491 r += snprintf (buf + r, size -r, ", FPU_REG:16/8");
3492 break;
3493 case E_NDS32_FPU_REG_32SP_16DP:
3494 r += snprintf (buf + r, size -r, ", FPU_REG:32/16");
3495 break;
3496 case E_NDS32_FPU_REG_32SP_32DP:
3497 r += snprintf (buf + r, size -r, ", FPU_REG:32/32");
3498 break;
3499 }
3500 }
3501
3502 if (config & E_NDS32_HAS_AUDIO_INST)
3503 r += snprintf (buf + r, size -r, ", AUDIO");
3504
3505 if (config & E_NDS32_HAS_STRING_INST)
3506 r += snprintf (buf + r, size -r, ", STR");
3507
3508 if (config & E_NDS32_HAS_REDUCED_REGS)
3509 r += snprintf (buf + r, size -r, ", 16REG");
3510
3511 if (config & E_NDS32_HAS_VIDEO_INST)
3512 {
3513 if (version <= E_NDS32_ELF_VER_1_3)
3514 r += snprintf (buf + r, size -r, ", VIDEO");
3515 else
3516 r += snprintf (buf + r, size -r, ", SATURATION");
3517 }
3518
3519 if (config & E_NDS32_HAS_ENCRIPT_INST)
3520 r += snprintf (buf + r, size -r, ", ENCRP");
3521
3522 if (config & E_NDS32_HAS_L2C_INST)
3523 r += snprintf (buf + r, size -r, ", L2C");
3524 }
3525
3526 static void
3527 decode_AMDGPU_machine_flags (Filedata *filedata, unsigned int e_flags,
3528 char *buf)
3529 {
3530 unsigned char *e_ident = filedata->file_header.e_ident;
3531 unsigned char osabi = e_ident[EI_OSABI];
3532 unsigned char abiversion = e_ident[EI_ABIVERSION];
3533 unsigned int mach;
3534
3535 /* HSA OS ABI v2 used a different encoding, but we don't need to support it,
3536 it has been deprecated for a while.
3537
3538 The PAL, MESA3D and NONE OS ABIs are not properly versioned, at the time
3539 of writing, they use the same flags as HSA v3, so the code below uses that
3540 assumption. */
3541 if (osabi == ELFOSABI_AMDGPU_HSA && abiversion < ELFABIVERSION_AMDGPU_HSA_V3)
3542 return;
3543
3544 mach = e_flags & EF_AMDGPU_MACH;
3545 switch (mach)
3546 {
3547 #define AMDGPU_CASE(code, string) \
3548 case code: strcat (buf, ", " string); break;
3549 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX600, "gfx600")
3550 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX601, "gfx601")
3551 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX700, "gfx700")
3552 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX701, "gfx701")
3553 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX702, "gfx702")
3554 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX703, "gfx703")
3555 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX704, "gfx704")
3556 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX801, "gfx801")
3557 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX802, "gfx802")
3558 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX803, "gfx803")
3559 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX810, "gfx810")
3560 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX900, "gfx900")
3561 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX902, "gfx902")
3562 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX904, "gfx904")
3563 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX906, "gfx906")
3564 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX908, "gfx908")
3565 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX909, "gfx909")
3566 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX90C, "gfx90c")
3567 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1010, "gfx1010")
3568 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1011, "gfx1011")
3569 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1012, "gfx1012")
3570 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1030, "gfx1030")
3571 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1031, "gfx1031")
3572 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1032, "gfx1032")
3573 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1033, "gfx1033")
3574 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX602, "gfx602")
3575 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX705, "gfx705")
3576 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX805, "gfx805")
3577 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1035, "gfx1035")
3578 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1034, "gfx1034")
3579 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX90A, "gfx90a")
3580 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX940, "gfx940")
3581 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1013, "gfx1013")
3582 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1036, "gfx1036")
3583 default:
3584 sprintf (buf, _(", <unknown AMDGPU GPU type: %#x>"), mach);
3585 break;
3586 #undef AMDGPU_CASE
3587 }
3588
3589 buf += strlen (buf);
3590 e_flags &= ~EF_AMDGPU_MACH;
3591
3592 if ((osabi == ELFOSABI_AMDGPU_HSA
3593 && abiversion == ELFABIVERSION_AMDGPU_HSA_V3)
3594 || osabi != ELFOSABI_AMDGPU_HSA)
3595 {
3596 /* For HSA v3 and other OS ABIs. */
3597 if (e_flags & EF_AMDGPU_FEATURE_XNACK_V3)
3598 {
3599 strcat (buf, ", xnack on");
3600 buf += strlen (buf);
3601 e_flags &= ~EF_AMDGPU_FEATURE_XNACK_V3;
3602 }
3603
3604 if (e_flags & EF_AMDGPU_FEATURE_SRAMECC_V3)
3605 {
3606 strcat (buf, ", sramecc on");
3607 buf += strlen (buf);
3608 e_flags &= ~EF_AMDGPU_FEATURE_SRAMECC_V3;
3609 }
3610 }
3611 else
3612 {
3613 /* For HSA v4+. */
3614 int xnack, sramecc;
3615
3616 xnack = e_flags & EF_AMDGPU_FEATURE_XNACK_V4;
3617 switch (xnack)
3618 {
3619 case EF_AMDGPU_FEATURE_XNACK_UNSUPPORTED_V4:
3620 break;
3621
3622 case EF_AMDGPU_FEATURE_XNACK_ANY_V4:
3623 strcat (buf, ", xnack any");
3624 break;
3625
3626 case EF_AMDGPU_FEATURE_XNACK_OFF_V4:
3627 strcat (buf, ", xnack off");
3628 break;
3629
3630 case EF_AMDGPU_FEATURE_XNACK_ON_V4:
3631 strcat (buf, ", xnack on");
3632 break;
3633
3634 default:
3635 sprintf (buf, _(", <unknown xnack value: %#x>"), xnack);
3636 break;
3637 }
3638
3639 buf += strlen (buf);
3640 e_flags &= ~EF_AMDGPU_FEATURE_XNACK_V4;
3641
3642 sramecc = e_flags & EF_AMDGPU_FEATURE_SRAMECC_V4;
3643 switch (sramecc)
3644 {
3645 case EF_AMDGPU_FEATURE_SRAMECC_UNSUPPORTED_V4:
3646 break;
3647
3648 case EF_AMDGPU_FEATURE_SRAMECC_ANY_V4:
3649 strcat (buf, ", sramecc any");
3650 break;
3651
3652 case EF_AMDGPU_FEATURE_SRAMECC_OFF_V4:
3653 strcat (buf, ", sramecc off");
3654 break;
3655
3656 case EF_AMDGPU_FEATURE_SRAMECC_ON_V4:
3657 strcat (buf, ", sramecc on");
3658 break;
3659
3660 default:
3661 sprintf (buf, _(", <unknown sramecc value: %#x>"), sramecc);
3662 break;
3663 }
3664
3665 buf += strlen (buf);
3666 e_flags &= ~EF_AMDGPU_FEATURE_SRAMECC_V4;
3667 }
3668
3669 if (e_flags != 0)
3670 sprintf (buf, _(", unknown flags bits: %#x"), e_flags);
3671 }
3672
3673 static char *
3674 get_machine_flags (Filedata * filedata, unsigned e_flags, unsigned e_machine)
3675 {
3676 static char buf[1024];
3677
3678 buf[0] = '\0';
3679
3680 if (e_flags)
3681 {
3682 switch (e_machine)
3683 {
3684 default:
3685 break;
3686
3687 case EM_ARC_COMPACT2:
3688 case EM_ARC_COMPACT:
3689 decode_ARC_machine_flags (e_flags, e_machine, buf);
3690 break;
3691
3692 case EM_ARM:
3693 decode_ARM_machine_flags (e_flags, buf);
3694 break;
3695
3696 case EM_AVR:
3697 decode_AVR_machine_flags (e_flags, buf, sizeof buf);
3698 break;
3699
3700 case EM_BLACKFIN:
3701 if (e_flags & EF_BFIN_PIC)
3702 strcat (buf, ", PIC");
3703
3704 if (e_flags & EF_BFIN_FDPIC)
3705 strcat (buf, ", FDPIC");
3706
3707 if (e_flags & EF_BFIN_CODE_IN_L1)
3708 strcat (buf, ", code in L1");
3709
3710 if (e_flags & EF_BFIN_DATA_IN_L1)
3711 strcat (buf, ", data in L1");
3712
3713 break;
3714
3715 case EM_CYGNUS_FRV:
3716 switch (e_flags & EF_FRV_CPU_MASK)
3717 {
3718 case EF_FRV_CPU_GENERIC:
3719 break;
3720
3721 default:
3722 strcat (buf, ", fr???");
3723 break;
3724
3725 case EF_FRV_CPU_FR300:
3726 strcat (buf, ", fr300");
3727 break;
3728
3729 case EF_FRV_CPU_FR400:
3730 strcat (buf, ", fr400");
3731 break;
3732 case EF_FRV_CPU_FR405:
3733 strcat (buf, ", fr405");
3734 break;
3735
3736 case EF_FRV_CPU_FR450:
3737 strcat (buf, ", fr450");
3738 break;
3739
3740 case EF_FRV_CPU_FR500:
3741 strcat (buf, ", fr500");
3742 break;
3743 case EF_FRV_CPU_FR550:
3744 strcat (buf, ", fr550");
3745 break;
3746
3747 case EF_FRV_CPU_SIMPLE:
3748 strcat (buf, ", simple");
3749 break;
3750 case EF_FRV_CPU_TOMCAT:
3751 strcat (buf, ", tomcat");
3752 break;
3753 }
3754 break;
3755
3756 case EM_68K:
3757 if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
3758 strcat (buf, ", m68000");
3759 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
3760 strcat (buf, ", cpu32");
3761 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
3762 strcat (buf, ", fido_a");
3763 else
3764 {
3765 char const * isa = _("unknown");
3766 char const * mac = _("unknown mac");
3767 char const * additional = NULL;
3768
3769 switch (e_flags & EF_M68K_CF_ISA_MASK)
3770 {
3771 case EF_M68K_CF_ISA_A_NODIV:
3772 isa = "A";
3773 additional = ", nodiv";
3774 break;
3775 case EF_M68K_CF_ISA_A:
3776 isa = "A";
3777 break;
3778 case EF_M68K_CF_ISA_A_PLUS:
3779 isa = "A+";
3780 break;
3781 case EF_M68K_CF_ISA_B_NOUSP:
3782 isa = "B";
3783 additional = ", nousp";
3784 break;
3785 case EF_M68K_CF_ISA_B:
3786 isa = "B";
3787 break;
3788 case EF_M68K_CF_ISA_C:
3789 isa = "C";
3790 break;
3791 case EF_M68K_CF_ISA_C_NODIV:
3792 isa = "C";
3793 additional = ", nodiv";
3794 break;
3795 }
3796 strcat (buf, ", cf, isa ");
3797 strcat (buf, isa);
3798 if (additional)
3799 strcat (buf, additional);
3800 if (e_flags & EF_M68K_CF_FLOAT)
3801 strcat (buf, ", float");
3802 switch (e_flags & EF_M68K_CF_MAC_MASK)
3803 {
3804 case 0:
3805 mac = NULL;
3806 break;
3807 case EF_M68K_CF_MAC:
3808 mac = "mac";
3809 break;
3810 case EF_M68K_CF_EMAC:
3811 mac = "emac";
3812 break;
3813 case EF_M68K_CF_EMAC_B:
3814 mac = "emac_b";
3815 break;
3816 }
3817 if (mac)
3818 {
3819 strcat (buf, ", ");
3820 strcat (buf, mac);
3821 }
3822 }
3823 break;
3824
3825 case EM_AMDGPU:
3826 decode_AMDGPU_machine_flags (filedata, e_flags, buf);
3827 break;
3828
3829 case EM_CYGNUS_MEP:
3830 switch (e_flags & EF_MEP_CPU_MASK)
3831 {
3832 case EF_MEP_CPU_MEP: strcat (buf, ", generic MeP"); break;
3833 case EF_MEP_CPU_C2: strcat (buf, ", MeP C2"); break;
3834 case EF_MEP_CPU_C3: strcat (buf, ", MeP C3"); break;
3835 case EF_MEP_CPU_C4: strcat (buf, ", MeP C4"); break;
3836 case EF_MEP_CPU_C5: strcat (buf, ", MeP C5"); break;
3837 case EF_MEP_CPU_H1: strcat (buf, ", MeP H1"); break;
3838 default: strcat (buf, _(", <unknown MeP cpu type>")); break;
3839 }
3840
3841 switch (e_flags & EF_MEP_COP_MASK)
3842 {
3843 case EF_MEP_COP_NONE: break;
3844 case EF_MEP_COP_AVC: strcat (buf, ", AVC coprocessor"); break;
3845 case EF_MEP_COP_AVC2: strcat (buf, ", AVC2 coprocessor"); break;
3846 case EF_MEP_COP_FMAX: strcat (buf, ", FMAX coprocessor"); break;
3847 case EF_MEP_COP_IVC2: strcat (buf, ", IVC2 coprocessor"); break;
3848 default: strcat (buf, _("<unknown MeP copro type>")); break;
3849 }
3850
3851 if (e_flags & EF_MEP_LIBRARY)
3852 strcat (buf, ", Built for Library");
3853
3854 if (e_flags & EF_MEP_INDEX_MASK)
3855 sprintf (buf + strlen (buf), ", Configuration Index: %#x",
3856 e_flags & EF_MEP_INDEX_MASK);
3857
3858 if (e_flags & ~ EF_MEP_ALL_FLAGS)
3859 sprintf (buf + strlen (buf), _(", unknown flags bits: %#x"),
3860 e_flags & ~ EF_MEP_ALL_FLAGS);
3861 break;
3862
3863 case EM_PPC:
3864 if (e_flags & EF_PPC_EMB)
3865 strcat (buf, ", emb");
3866
3867 if (e_flags & EF_PPC_RELOCATABLE)
3868 strcat (buf, _(", relocatable"));
3869
3870 if (e_flags & EF_PPC_RELOCATABLE_LIB)
3871 strcat (buf, _(", relocatable-lib"));
3872 break;
3873
3874 case EM_PPC64:
3875 if (e_flags & EF_PPC64_ABI)
3876 {
3877 char abi[] = ", abiv0";
3878
3879 abi[6] += e_flags & EF_PPC64_ABI;
3880 strcat (buf, abi);
3881 }
3882 break;
3883
3884 case EM_V800:
3885 if ((e_flags & EF_RH850_ABI) == EF_RH850_ABI)
3886 strcat (buf, ", RH850 ABI");
3887
3888 if (e_flags & EF_V800_850E3)
3889 strcat (buf, ", V3 architecture");
3890
3891 if ((e_flags & (EF_RH850_FPU_DOUBLE | EF_RH850_FPU_SINGLE)) == 0)
3892 strcat (buf, ", FPU not used");
3893
3894 if ((e_flags & (EF_RH850_REGMODE22 | EF_RH850_REGMODE32)) == 0)
3895 strcat (buf, ", regmode: COMMON");
3896
3897 if ((e_flags & (EF_RH850_GP_FIX | EF_RH850_GP_NOFIX)) == 0)
3898 strcat (buf, ", r4 not used");
3899
3900 if ((e_flags & (EF_RH850_EP_FIX | EF_RH850_EP_NOFIX)) == 0)
3901 strcat (buf, ", r30 not used");
3902
3903 if ((e_flags & (EF_RH850_TP_FIX | EF_RH850_TP_NOFIX)) == 0)
3904 strcat (buf, ", r5 not used");
3905
3906 if ((e_flags & (EF_RH850_REG2_RESERVE | EF_RH850_REG2_NORESERVE)) == 0)
3907 strcat (buf, ", r2 not used");
3908
3909 for (e_flags &= 0xFFFF; e_flags; e_flags &= ~ (e_flags & - e_flags))
3910 {
3911 switch (e_flags & - e_flags)
3912 {
3913 case EF_RH850_FPU_DOUBLE: strcat (buf, ", double precision FPU"); break;
3914 case EF_RH850_FPU_SINGLE: strcat (buf, ", single precision FPU"); break;
3915 case EF_RH850_REGMODE22: strcat (buf, ", regmode:22"); break;
3916 case EF_RH850_REGMODE32: strcat (buf, ", regmode:23"); break;
3917 case EF_RH850_GP_FIX: strcat (buf, ", r4 fixed"); break;
3918 case EF_RH850_GP_NOFIX: strcat (buf, ", r4 free"); break;
3919 case EF_RH850_EP_FIX: strcat (buf, ", r30 fixed"); break;
3920 case EF_RH850_EP_NOFIX: strcat (buf, ", r30 free"); break;
3921 case EF_RH850_TP_FIX: strcat (buf, ", r5 fixed"); break;
3922 case EF_RH850_TP_NOFIX: strcat (buf, ", r5 free"); break;
3923 case EF_RH850_REG2_RESERVE: strcat (buf, ", r2 fixed"); break;
3924 case EF_RH850_REG2_NORESERVE: strcat (buf, ", r2 free"); break;
3925 default: break;
3926 }
3927 }
3928 break;
3929
3930 case EM_V850:
3931 case EM_CYGNUS_V850:
3932 switch (e_flags & EF_V850_ARCH)
3933 {
3934 case E_V850E3V5_ARCH:
3935 strcat (buf, ", v850e3v5");
3936 break;
3937 case E_V850E2V3_ARCH:
3938 strcat (buf, ", v850e2v3");
3939 break;
3940 case E_V850E2_ARCH:
3941 strcat (buf, ", v850e2");
3942 break;
3943 case E_V850E1_ARCH:
3944 strcat (buf, ", v850e1");
3945 break;
3946 case E_V850E_ARCH:
3947 strcat (buf, ", v850e");
3948 break;
3949 case E_V850_ARCH:
3950 strcat (buf, ", v850");
3951 break;
3952 default:
3953 strcat (buf, _(", unknown v850 architecture variant"));
3954 break;
3955 }
3956 break;
3957
3958 case EM_M32R:
3959 case EM_CYGNUS_M32R:
3960 if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
3961 strcat (buf, ", m32r");
3962 break;
3963
3964 case EM_MIPS:
3965 case EM_MIPS_RS3_LE:
3966 if (e_flags & EF_MIPS_NOREORDER)
3967 strcat (buf, ", noreorder");
3968
3969 if (e_flags & EF_MIPS_PIC)
3970 strcat (buf, ", pic");
3971
3972 if (e_flags & EF_MIPS_CPIC)
3973 strcat (buf, ", cpic");
3974
3975 if (e_flags & EF_MIPS_UCODE)
3976 strcat (buf, ", ugen_reserved");
3977
3978 if (e_flags & EF_MIPS_ABI2)
3979 strcat (buf, ", abi2");
3980
3981 if (e_flags & EF_MIPS_OPTIONS_FIRST)
3982 strcat (buf, ", odk first");
3983
3984 if (e_flags & EF_MIPS_32BITMODE)
3985 strcat (buf, ", 32bitmode");
3986
3987 if (e_flags & EF_MIPS_NAN2008)
3988 strcat (buf, ", nan2008");
3989
3990 if (e_flags & EF_MIPS_FP64)
3991 strcat (buf, ", fp64");
3992
3993 switch ((e_flags & EF_MIPS_MACH))
3994 {
3995 case E_MIPS_MACH_3900: strcat (buf, ", 3900"); break;
3996 case E_MIPS_MACH_4010: strcat (buf, ", 4010"); break;
3997 case E_MIPS_MACH_4100: strcat (buf, ", 4100"); break;
3998 case E_MIPS_MACH_4111: strcat (buf, ", 4111"); break;
3999 case E_MIPS_MACH_4120: strcat (buf, ", 4120"); break;
4000 case E_MIPS_MACH_4650: strcat (buf, ", 4650"); break;
4001 case E_MIPS_MACH_5400: strcat (buf, ", 5400"); break;
4002 case E_MIPS_MACH_5500: strcat (buf, ", 5500"); break;
4003 case E_MIPS_MACH_5900: strcat (buf, ", 5900"); break;
4004 case E_MIPS_MACH_SB1: strcat (buf, ", sb1"); break;
4005 case E_MIPS_MACH_9000: strcat (buf, ", 9000"); break;
4006 case E_MIPS_MACH_LS2E: strcat (buf, ", loongson-2e"); break;
4007 case E_MIPS_MACH_LS2F: strcat (buf, ", loongson-2f"); break;
4008 case E_MIPS_MACH_GS464: strcat (buf, ", gs464"); break;
4009 case E_MIPS_MACH_GS464E: strcat (buf, ", gs464e"); break;
4010 case E_MIPS_MACH_GS264E: strcat (buf, ", gs264e"); break;
4011 case E_MIPS_MACH_OCTEON: strcat (buf, ", octeon"); break;
4012 case E_MIPS_MACH_OCTEON2: strcat (buf, ", octeon2"); break;
4013 case E_MIPS_MACH_OCTEON3: strcat (buf, ", octeon3"); break;
4014 case E_MIPS_MACH_XLR: strcat (buf, ", xlr"); break;
4015 case E_MIPS_MACH_IAMR2: strcat (buf, ", interaptiv-mr2"); break;
4016 case 0:
4017 /* We simply ignore the field in this case to avoid confusion:
4018 MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
4019 extension. */
4020 break;
4021 default: strcat (buf, _(", unknown CPU")); break;
4022 }
4023
4024 switch ((e_flags & EF_MIPS_ABI))
4025 {
4026 case E_MIPS_ABI_O32: strcat (buf, ", o32"); break;
4027 case E_MIPS_ABI_O64: strcat (buf, ", o64"); break;
4028 case E_MIPS_ABI_EABI32: strcat (buf, ", eabi32"); break;
4029 case E_MIPS_ABI_EABI64: strcat (buf, ", eabi64"); break;
4030 case 0:
4031 /* We simply ignore the field in this case to avoid confusion:
4032 MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
4033 This means it is likely to be an o32 file, but not for
4034 sure. */
4035 break;
4036 default: strcat (buf, _(", unknown ABI")); break;
4037 }
4038
4039 if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
4040 strcat (buf, ", mdmx");
4041
4042 if (e_flags & EF_MIPS_ARCH_ASE_M16)
4043 strcat (buf, ", mips16");
4044
4045 if (e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
4046 strcat (buf, ", micromips");
4047
4048 switch ((e_flags & EF_MIPS_ARCH))
4049 {
4050 case E_MIPS_ARCH_1: strcat (buf, ", mips1"); break;
4051 case E_MIPS_ARCH_2: strcat (buf, ", mips2"); break;
4052 case E_MIPS_ARCH_3: strcat (buf, ", mips3"); break;
4053 case E_MIPS_ARCH_4: strcat (buf, ", mips4"); break;
4054 case E_MIPS_ARCH_5: strcat (buf, ", mips5"); break;
4055 case E_MIPS_ARCH_32: strcat (buf, ", mips32"); break;
4056 case E_MIPS_ARCH_32R2: strcat (buf, ", mips32r2"); break;
4057 case E_MIPS_ARCH_32R6: strcat (buf, ", mips32r6"); break;
4058 case E_MIPS_ARCH_64: strcat (buf, ", mips64"); break;
4059 case E_MIPS_ARCH_64R2: strcat (buf, ", mips64r2"); break;
4060 case E_MIPS_ARCH_64R6: strcat (buf, ", mips64r6"); break;
4061 default: strcat (buf, _(", unknown ISA")); break;
4062 }
4063 break;
4064
4065 case EM_NDS32:
4066 decode_NDS32_machine_flags (e_flags, buf, sizeof buf);
4067 break;
4068
4069 case EM_NFP:
4070 switch (EF_NFP_MACH (e_flags))
4071 {
4072 case E_NFP_MACH_3200:
4073 strcat (buf, ", NFP-32xx");
4074 break;
4075 case E_NFP_MACH_6000:
4076 strcat (buf, ", NFP-6xxx");
4077 break;
4078 }
4079 break;
4080
4081 case EM_RISCV:
4082 if (e_flags & EF_RISCV_RVC)
4083 strcat (buf, ", RVC");
4084
4085 if (e_flags & EF_RISCV_RVE)
4086 strcat (buf, ", RVE");
4087
4088 if (e_flags & EF_RISCV_TSO)
4089 strcat (buf, ", TSO");
4090
4091 switch (e_flags & EF_RISCV_FLOAT_ABI)
4092 {
4093 case EF_RISCV_FLOAT_ABI_SOFT:
4094 strcat (buf, ", soft-float ABI");
4095 break;
4096
4097 case EF_RISCV_FLOAT_ABI_SINGLE:
4098 strcat (buf, ", single-float ABI");
4099 break;
4100
4101 case EF_RISCV_FLOAT_ABI_DOUBLE:
4102 strcat (buf, ", double-float ABI");
4103 break;
4104
4105 case EF_RISCV_FLOAT_ABI_QUAD:
4106 strcat (buf, ", quad-float ABI");
4107 break;
4108 }
4109 break;
4110
4111 case EM_SH:
4112 switch ((e_flags & EF_SH_MACH_MASK))
4113 {
4114 case EF_SH1: strcat (buf, ", sh1"); break;
4115 case EF_SH2: strcat (buf, ", sh2"); break;
4116 case EF_SH3: strcat (buf, ", sh3"); break;
4117 case EF_SH_DSP: strcat (buf, ", sh-dsp"); break;
4118 case EF_SH3_DSP: strcat (buf, ", sh3-dsp"); break;
4119 case EF_SH4AL_DSP: strcat (buf, ", sh4al-dsp"); break;
4120 case EF_SH3E: strcat (buf, ", sh3e"); break;
4121 case EF_SH4: strcat (buf, ", sh4"); break;
4122 case EF_SH5: strcat (buf, ", sh5"); break;
4123 case EF_SH2E: strcat (buf, ", sh2e"); break;
4124 case EF_SH4A: strcat (buf, ", sh4a"); break;
4125 case EF_SH2A: strcat (buf, ", sh2a"); break;
4126 case EF_SH4_NOFPU: strcat (buf, ", sh4-nofpu"); break;
4127 case EF_SH4A_NOFPU: strcat (buf, ", sh4a-nofpu"); break;
4128 case EF_SH2A_NOFPU: strcat (buf, ", sh2a-nofpu"); break;
4129 case EF_SH3_NOMMU: strcat (buf, ", sh3-nommu"); break;
4130 case EF_SH4_NOMMU_NOFPU: strcat (buf, ", sh4-nommu-nofpu"); break;
4131 case EF_SH2A_SH4_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh4-nommu-nofpu"); break;
4132 case EF_SH2A_SH3_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh3-nommu"); break;
4133 case EF_SH2A_SH4: strcat (buf, ", sh2a-or-sh4"); break;
4134 case EF_SH2A_SH3E: strcat (buf, ", sh2a-or-sh3e"); break;
4135 default: strcat (buf, _(", unknown ISA")); break;
4136 }
4137
4138 if (e_flags & EF_SH_PIC)
4139 strcat (buf, ", pic");
4140
4141 if (e_flags & EF_SH_FDPIC)
4142 strcat (buf, ", fdpic");
4143 break;
4144
4145 case EM_OR1K:
4146 if (e_flags & EF_OR1K_NODELAY)
4147 strcat (buf, ", no delay");
4148 break;
4149
4150 case EM_SPARCV9:
4151 if (e_flags & EF_SPARC_32PLUS)
4152 strcat (buf, ", v8+");
4153
4154 if (e_flags & EF_SPARC_SUN_US1)
4155 strcat (buf, ", ultrasparcI");
4156
4157 if (e_flags & EF_SPARC_SUN_US3)
4158 strcat (buf, ", ultrasparcIII");
4159
4160 if (e_flags & EF_SPARC_HAL_R1)
4161 strcat (buf, ", halr1");
4162
4163 if (e_flags & EF_SPARC_LEDATA)
4164 strcat (buf, ", ledata");
4165
4166 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
4167 strcat (buf, ", tso");
4168
4169 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
4170 strcat (buf, ", pso");
4171
4172 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
4173 strcat (buf, ", rmo");
4174 break;
4175
4176 case EM_PARISC:
4177 switch (e_flags & EF_PARISC_ARCH)
4178 {
4179 case EFA_PARISC_1_0:
4180 strcpy (buf, ", PA-RISC 1.0");
4181 break;
4182 case EFA_PARISC_1_1:
4183 strcpy (buf, ", PA-RISC 1.1");
4184 break;
4185 case EFA_PARISC_2_0:
4186 strcpy (buf, ", PA-RISC 2.0");
4187 break;
4188 default:
4189 break;
4190 }
4191 if (e_flags & EF_PARISC_TRAPNIL)
4192 strcat (buf, ", trapnil");
4193 if (e_flags & EF_PARISC_EXT)
4194 strcat (buf, ", ext");
4195 if (e_flags & EF_PARISC_LSB)
4196 strcat (buf, ", lsb");
4197 if (e_flags & EF_PARISC_WIDE)
4198 strcat (buf, ", wide");
4199 if (e_flags & EF_PARISC_NO_KABP)
4200 strcat (buf, ", no kabp");
4201 if (e_flags & EF_PARISC_LAZYSWAP)
4202 strcat (buf, ", lazyswap");
4203 break;
4204
4205 case EM_PJ:
4206 case EM_PJ_OLD:
4207 if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
4208 strcat (buf, ", new calling convention");
4209
4210 if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
4211 strcat (buf, ", gnu calling convention");
4212 break;
4213
4214 case EM_IA_64:
4215 if ((e_flags & EF_IA_64_ABI64))
4216 strcat (buf, ", 64-bit");
4217 else
4218 strcat (buf, ", 32-bit");
4219 if ((e_flags & EF_IA_64_REDUCEDFP))
4220 strcat (buf, ", reduced fp model");
4221 if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
4222 strcat (buf, ", no function descriptors, constant gp");
4223 else if ((e_flags & EF_IA_64_CONS_GP))
4224 strcat (buf, ", constant gp");
4225 if ((e_flags & EF_IA_64_ABSOLUTE))
4226 strcat (buf, ", absolute");
4227 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
4228 {
4229 if ((e_flags & EF_IA_64_VMS_LINKAGES))
4230 strcat (buf, ", vms_linkages");
4231 switch ((e_flags & EF_IA_64_VMS_COMCOD))
4232 {
4233 case EF_IA_64_VMS_COMCOD_SUCCESS:
4234 break;
4235 case EF_IA_64_VMS_COMCOD_WARNING:
4236 strcat (buf, ", warning");
4237 break;
4238 case EF_IA_64_VMS_COMCOD_ERROR:
4239 strcat (buf, ", error");
4240 break;
4241 case EF_IA_64_VMS_COMCOD_ABORT:
4242 strcat (buf, ", abort");
4243 break;
4244 default:
4245 warn (_("Unrecognised IA64 VMS Command Code: %x\n"),
4246 e_flags & EF_IA_64_VMS_COMCOD);
4247 strcat (buf, ", <unknown>");
4248 }
4249 }
4250 break;
4251
4252 case EM_VAX:
4253 if ((e_flags & EF_VAX_NONPIC))
4254 strcat (buf, ", non-PIC");
4255 if ((e_flags & EF_VAX_DFLOAT))
4256 strcat (buf, ", D-Float");
4257 if ((e_flags & EF_VAX_GFLOAT))
4258 strcat (buf, ", G-Float");
4259 break;
4260
4261 case EM_VISIUM:
4262 if (e_flags & EF_VISIUM_ARCH_MCM)
4263 strcat (buf, ", mcm");
4264 else if (e_flags & EF_VISIUM_ARCH_MCM24)
4265 strcat (buf, ", mcm24");
4266 if (e_flags & EF_VISIUM_ARCH_GR6)
4267 strcat (buf, ", gr6");
4268 break;
4269
4270 case EM_RL78:
4271 switch (e_flags & E_FLAG_RL78_CPU_MASK)
4272 {
4273 case E_FLAG_RL78_ANY_CPU: break;
4274 case E_FLAG_RL78_G10: strcat (buf, ", G10"); break;
4275 case E_FLAG_RL78_G13: strcat (buf, ", G13"); break;
4276 case E_FLAG_RL78_G14: strcat (buf, ", G14"); break;
4277 }
4278 if (e_flags & E_FLAG_RL78_64BIT_DOUBLES)
4279 strcat (buf, ", 64-bit doubles");
4280 break;
4281
4282 case EM_RX:
4283 if (e_flags & E_FLAG_RX_64BIT_DOUBLES)
4284 strcat (buf, ", 64-bit doubles");
4285 if (e_flags & E_FLAG_RX_DSP)
4286 strcat (buf, ", dsp");
4287 if (e_flags & E_FLAG_RX_PID)
4288 strcat (buf, ", pid");
4289 if (e_flags & E_FLAG_RX_ABI)
4290 strcat (buf, ", RX ABI");
4291 if (e_flags & E_FLAG_RX_SINSNS_SET)
4292 strcat (buf, e_flags & E_FLAG_RX_SINSNS_YES
4293 ? ", uses String instructions" : ", bans String instructions");
4294 if (e_flags & E_FLAG_RX_V2)
4295 strcat (buf, ", V2");
4296 if (e_flags & E_FLAG_RX_V3)
4297 strcat (buf, ", V3");
4298 break;
4299
4300 case EM_S390:
4301 if (e_flags & EF_S390_HIGH_GPRS)
4302 strcat (buf, ", highgprs");
4303 break;
4304
4305 case EM_TI_C6000:
4306 if ((e_flags & EF_C6000_REL))
4307 strcat (buf, ", relocatable module");
4308 break;
4309
4310 case EM_MSP430:
4311 strcat (buf, _(": architecture variant: "));
4312 switch (e_flags & EF_MSP430_MACH)
4313 {
4314 case E_MSP430_MACH_MSP430x11: strcat (buf, "MSP430x11"); break;
4315 case E_MSP430_MACH_MSP430x11x1 : strcat (buf, "MSP430x11x1 "); break;
4316 case E_MSP430_MACH_MSP430x12: strcat (buf, "MSP430x12"); break;
4317 case E_MSP430_MACH_MSP430x13: strcat (buf, "MSP430x13"); break;
4318 case E_MSP430_MACH_MSP430x14: strcat (buf, "MSP430x14"); break;
4319 case E_MSP430_MACH_MSP430x15: strcat (buf, "MSP430x15"); break;
4320 case E_MSP430_MACH_MSP430x16: strcat (buf, "MSP430x16"); break;
4321 case E_MSP430_MACH_MSP430x31: strcat (buf, "MSP430x31"); break;
4322 case E_MSP430_MACH_MSP430x32: strcat (buf, "MSP430x32"); break;
4323 case E_MSP430_MACH_MSP430x33: strcat (buf, "MSP430x33"); break;
4324 case E_MSP430_MACH_MSP430x41: strcat (buf, "MSP430x41"); break;
4325 case E_MSP430_MACH_MSP430x42: strcat (buf, "MSP430x42"); break;
4326 case E_MSP430_MACH_MSP430x43: strcat (buf, "MSP430x43"); break;
4327 case E_MSP430_MACH_MSP430x44: strcat (buf, "MSP430x44"); break;
4328 case E_MSP430_MACH_MSP430X : strcat (buf, "MSP430X"); break;
4329 default:
4330 strcat (buf, _(": unknown")); break;
4331 }
4332
4333 if (e_flags & ~ EF_MSP430_MACH)
4334 strcat (buf, _(": unknown extra flag bits also present"));
4335 break;
4336
4337 case EM_Z80:
4338 switch (e_flags & EF_Z80_MACH_MSK)
4339 {
4340 case EF_Z80_MACH_Z80: strcat (buf, ", Z80"); break;
4341 case EF_Z80_MACH_Z180: strcat (buf, ", Z180"); break;
4342 case EF_Z80_MACH_R800: strcat (buf, ", R800"); break;
4343 case EF_Z80_MACH_EZ80_Z80: strcat (buf, ", EZ80"); break;
4344 case EF_Z80_MACH_EZ80_ADL: strcat (buf, ", EZ80, ADL"); break;
4345 case EF_Z80_MACH_GBZ80: strcat (buf, ", GBZ80"); break;
4346 case EF_Z80_MACH_Z80N: strcat (buf, ", Z80N"); break;
4347 default:
4348 strcat (buf, _(", unknown")); break;
4349 }
4350 break;
4351 case EM_LOONGARCH:
4352 if (EF_LOONGARCH_IS_SOFT_FLOAT (e_flags))
4353 strcat (buf, ", SOFT-FLOAT");
4354 else if (EF_LOONGARCH_IS_SINGLE_FLOAT (e_flags))
4355 strcat (buf, ", SINGLE-FLOAT");
4356 else if (EF_LOONGARCH_IS_DOUBLE_FLOAT (e_flags))
4357 strcat (buf, ", DOUBLE-FLOAT");
4358
4359 if (EF_LOONGARCH_IS_OBJ_V0 (e_flags))
4360 strcat (buf, ", OBJ-v0");
4361 else if (EF_LOONGARCH_IS_OBJ_V1 (e_flags))
4362 strcat (buf, ", OBJ-v1");
4363
4364 break;
4365 }
4366 }
4367
4368 return buf;
4369 }
4370
4371 static const char *
4372 get_osabi_name (Filedata * filedata, unsigned int osabi)
4373 {
4374 static char buff[32];
4375
4376 switch (osabi)
4377 {
4378 case ELFOSABI_NONE: return "UNIX - System V";
4379 case ELFOSABI_HPUX: return "UNIX - HP-UX";
4380 case ELFOSABI_NETBSD: return "UNIX - NetBSD";
4381 case ELFOSABI_GNU: return "UNIX - GNU";
4382 case ELFOSABI_SOLARIS: return "UNIX - Solaris";
4383 case ELFOSABI_AIX: return "UNIX - AIX";
4384 case ELFOSABI_IRIX: return "UNIX - IRIX";
4385 case ELFOSABI_FREEBSD: return "UNIX - FreeBSD";
4386 case ELFOSABI_TRU64: return "UNIX - TRU64";
4387 case ELFOSABI_MODESTO: return "Novell - Modesto";
4388 case ELFOSABI_OPENBSD: return "UNIX - OpenBSD";
4389 case ELFOSABI_OPENVMS: return "VMS - OpenVMS";
4390 case ELFOSABI_NSK: return "HP - Non-Stop Kernel";
4391 case ELFOSABI_AROS: return "AROS";
4392 case ELFOSABI_FENIXOS: return "FenixOS";
4393 case ELFOSABI_CLOUDABI: return "Nuxi CloudABI";
4394 case ELFOSABI_OPENVOS: return "Stratus Technologies OpenVOS";
4395 default:
4396 if (osabi >= 64)
4397 switch (filedata->file_header.e_machine)
4398 {
4399 case EM_AMDGPU:
4400 switch (osabi)
4401 {
4402 case ELFOSABI_AMDGPU_HSA: return "AMD HSA";
4403 case ELFOSABI_AMDGPU_PAL: return "AMD PAL";
4404 case ELFOSABI_AMDGPU_MESA3D: return "AMD Mesa3D";
4405 default:
4406 break;
4407 }
4408 break;
4409
4410 case EM_ARM:
4411 switch (osabi)
4412 {
4413 case ELFOSABI_ARM: return "ARM";
4414 case ELFOSABI_ARM_FDPIC: return "ARM FDPIC";
4415 default:
4416 break;
4417 }
4418 break;
4419
4420 case EM_MSP430:
4421 case EM_MSP430_OLD:
4422 case EM_VISIUM:
4423 switch (osabi)
4424 {
4425 case ELFOSABI_STANDALONE: return _("Standalone App");
4426 default:
4427 break;
4428 }
4429 break;
4430
4431 case EM_TI_C6000:
4432 switch (osabi)
4433 {
4434 case ELFOSABI_C6000_ELFABI: return _("Bare-metal C6000");
4435 case ELFOSABI_C6000_LINUX: return "Linux C6000";
4436 default:
4437 break;
4438 }
4439 break;
4440
4441 default:
4442 break;
4443 }
4444 snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
4445 return buff;
4446 }
4447 }
4448
4449 static const char *
4450 get_aarch64_segment_type (unsigned long type)
4451 {
4452 switch (type)
4453 {
4454 case PT_AARCH64_ARCHEXT: return "AARCH64_ARCHEXT";
4455 case PT_AARCH64_MEMTAG_MTE: return "AARCH64_MEMTAG_MTE";
4456 default: return NULL;
4457 }
4458 }
4459
4460 static const char *
4461 get_arm_segment_type (unsigned long type)
4462 {
4463 switch (type)
4464 {
4465 case PT_ARM_EXIDX: return "EXIDX";
4466 default: return NULL;
4467 }
4468 }
4469
4470 static const char *
4471 get_s390_segment_type (unsigned long type)
4472 {
4473 switch (type)
4474 {
4475 case PT_S390_PGSTE: return "S390_PGSTE";
4476 default: return NULL;
4477 }
4478 }
4479
4480 static const char *
4481 get_mips_segment_type (unsigned long type)
4482 {
4483 switch (type)
4484 {
4485 case PT_MIPS_REGINFO: return "REGINFO";
4486 case PT_MIPS_RTPROC: return "RTPROC";
4487 case PT_MIPS_OPTIONS: return "OPTIONS";
4488 case PT_MIPS_ABIFLAGS: return "ABIFLAGS";
4489 default: return NULL;
4490 }
4491 }
4492
4493 static const char *
4494 get_parisc_segment_type (unsigned long type)
4495 {
4496 switch (type)
4497 {
4498 case PT_PARISC_ARCHEXT: return "PARISC_ARCHEXT";
4499 case PT_PARISC_UNWIND: return "PARISC_UNWIND";
4500 case PT_PARISC_WEAKORDER: return "PARISC_WEAKORDER";
4501 default: return NULL;
4502 }
4503 }
4504
4505 static const char *
4506 get_ia64_segment_type (unsigned long type)
4507 {
4508 switch (type)
4509 {
4510 case PT_IA_64_ARCHEXT: return "IA_64_ARCHEXT";
4511 case PT_IA_64_UNWIND: return "IA_64_UNWIND";
4512 default: return NULL;
4513 }
4514 }
4515
4516 static const char *
4517 get_tic6x_segment_type (unsigned long type)
4518 {
4519 switch (type)
4520 {
4521 case PT_C6000_PHATTR: return "C6000_PHATTR";
4522 default: return NULL;
4523 }
4524 }
4525
4526 static const char *
4527 get_riscv_segment_type (unsigned long type)
4528 {
4529 switch (type)
4530 {
4531 case PT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
4532 default: return NULL;
4533 }
4534 }
4535
4536 static const char *
4537 get_hpux_segment_type (unsigned long type, unsigned e_machine)
4538 {
4539 if (e_machine == EM_PARISC)
4540 switch (type)
4541 {
4542 case PT_HP_TLS: return "HP_TLS";
4543 case PT_HP_CORE_NONE: return "HP_CORE_NONE";
4544 case PT_HP_CORE_VERSION: return "HP_CORE_VERSION";
4545 case PT_HP_CORE_KERNEL: return "HP_CORE_KERNEL";
4546 case PT_HP_CORE_COMM: return "HP_CORE_COMM";
4547 case PT_HP_CORE_PROC: return "HP_CORE_PROC";
4548 case PT_HP_CORE_LOADABLE: return "HP_CORE_LOADABLE";
4549 case PT_HP_CORE_STACK: return "HP_CORE_STACK";
4550 case PT_HP_CORE_SHM: return "HP_CORE_SHM";
4551 case PT_HP_CORE_MMF: return "HP_CORE_MMF";
4552 case PT_HP_PARALLEL: return "HP_PARALLEL";
4553 case PT_HP_FASTBIND: return "HP_FASTBIND";
4554 case PT_HP_OPT_ANNOT: return "HP_OPT_ANNOT";
4555 case PT_HP_HSL_ANNOT: return "HP_HSL_ANNOT";
4556 case PT_HP_STACK: return "HP_STACK";
4557 case PT_HP_CORE_UTSNAME: return "HP_CORE_UTSNAME";
4558 default: return NULL;
4559 }
4560
4561 if (e_machine == EM_IA_64)
4562 switch (type)
4563 {
4564 case PT_HP_TLS: return "HP_TLS";
4565 case PT_IA_64_HP_OPT_ANOT: return "HP_OPT_ANNOT";
4566 case PT_IA_64_HP_HSL_ANOT: return "HP_HSL_ANNOT";
4567 case PT_IA_64_HP_STACK: return "HP_STACK";
4568 default: return NULL;
4569 }
4570
4571 return NULL;
4572 }
4573
4574 static const char *
4575 get_solaris_segment_type (unsigned long type)
4576 {
4577 switch (type)
4578 {
4579 case 0x6464e550: return "PT_SUNW_UNWIND";
4580 case 0x6474e550: return "PT_SUNW_EH_FRAME";
4581 case 0x6ffffff7: return "PT_LOSUNW";
4582 case 0x6ffffffa: return "PT_SUNWBSS";
4583 case 0x6ffffffb: return "PT_SUNWSTACK";
4584 case 0x6ffffffc: return "PT_SUNWDTRACE";
4585 case 0x6ffffffd: return "PT_SUNWCAP";
4586 case 0x6fffffff: return "PT_HISUNW";
4587 default: return NULL;
4588 }
4589 }
4590
4591 static const char *
4592 get_segment_type (Filedata * filedata, unsigned long p_type)
4593 {
4594 static char buff[32];
4595
4596 switch (p_type)
4597 {
4598 case PT_NULL: return "NULL";
4599 case PT_LOAD: return "LOAD";
4600 case PT_DYNAMIC: return "DYNAMIC";
4601 case PT_INTERP: return "INTERP";
4602 case PT_NOTE: return "NOTE";
4603 case PT_SHLIB: return "SHLIB";
4604 case PT_PHDR: return "PHDR";
4605 case PT_TLS: return "TLS";
4606 case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
4607 case PT_GNU_STACK: return "GNU_STACK";
4608 case PT_GNU_RELRO: return "GNU_RELRO";
4609 case PT_GNU_PROPERTY: return "GNU_PROPERTY";
4610 case PT_GNU_SFRAME: return "GNU_SFRAME";
4611
4612 case PT_OPENBSD_RANDOMIZE: return "OPENBSD_RANDOMIZE";
4613 case PT_OPENBSD_WXNEEDED: return "OPENBSD_WXNEEDED";
4614 case PT_OPENBSD_BOOTDATA: return "OPENBSD_BOOTDATA";
4615
4616 default:
4617 if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
4618 {
4619 const char * result;
4620
4621 switch (filedata->file_header.e_machine)
4622 {
4623 case EM_AARCH64:
4624 result = get_aarch64_segment_type (p_type);
4625 break;
4626 case EM_ARM:
4627 result = get_arm_segment_type (p_type);
4628 break;
4629 case EM_MIPS:
4630 case EM_MIPS_RS3_LE:
4631 result = get_mips_segment_type (p_type);
4632 break;
4633 case EM_PARISC:
4634 result = get_parisc_segment_type (p_type);
4635 break;
4636 case EM_IA_64:
4637 result = get_ia64_segment_type (p_type);
4638 break;
4639 case EM_TI_C6000:
4640 result = get_tic6x_segment_type (p_type);
4641 break;
4642 case EM_S390:
4643 case EM_S390_OLD:
4644 result = get_s390_segment_type (p_type);
4645 break;
4646 case EM_RISCV:
4647 result = get_riscv_segment_type (p_type);
4648 break;
4649 default:
4650 result = NULL;
4651 break;
4652 }
4653
4654 if (result != NULL)
4655 return result;
4656
4657 sprintf (buff, "LOPROC+%#lx", p_type - PT_LOPROC);
4658 }
4659 else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
4660 {
4661 const char * result = NULL;
4662
4663 switch (filedata->file_header.e_ident[EI_OSABI])
4664 {
4665 case ELFOSABI_GNU:
4666 case ELFOSABI_FREEBSD:
4667 if (p_type >= PT_GNU_MBIND_LO && p_type <= PT_GNU_MBIND_HI)
4668 {
4669 sprintf (buff, "GNU_MBIND+%#lx", p_type - PT_GNU_MBIND_LO);
4670 result = buff;
4671 }
4672 break;
4673 case ELFOSABI_HPUX:
4674 result = get_hpux_segment_type (p_type,
4675 filedata->file_header.e_machine);
4676 break;
4677 case ELFOSABI_SOLARIS:
4678 result = get_solaris_segment_type (p_type);
4679 break;
4680 default:
4681 break;
4682 }
4683 if (result != NULL)
4684 return result;
4685
4686 sprintf (buff, "LOOS+%#lx", p_type - PT_LOOS);
4687 }
4688 else
4689 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
4690
4691 return buff;
4692 }
4693 }
4694
4695 static const char *
4696 get_arc_section_type_name (unsigned int sh_type)
4697 {
4698 switch (sh_type)
4699 {
4700 case SHT_ARC_ATTRIBUTES: return "ARC_ATTRIBUTES";
4701 default:
4702 break;
4703 }
4704 return NULL;
4705 }
4706
4707 static const char *
4708 get_mips_section_type_name (unsigned int sh_type)
4709 {
4710 switch (sh_type)
4711 {
4712 case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
4713 case SHT_MIPS_MSYM: return "MIPS_MSYM";
4714 case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
4715 case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
4716 case SHT_MIPS_UCODE: return "MIPS_UCODE";
4717 case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
4718 case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
4719 case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
4720 case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
4721 case SHT_MIPS_RELD: return "MIPS_RELD";
4722 case SHT_MIPS_IFACE: return "MIPS_IFACE";
4723 case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
4724 case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
4725 case SHT_MIPS_SHDR: return "MIPS_SHDR";
4726 case SHT_MIPS_FDESC: return "MIPS_FDESC";
4727 case SHT_MIPS_EXTSYM: return "MIPS_EXTSYM";
4728 case SHT_MIPS_DENSE: return "MIPS_DENSE";
4729 case SHT_MIPS_PDESC: return "MIPS_PDESC";
4730 case SHT_MIPS_LOCSYM: return "MIPS_LOCSYM";
4731 case SHT_MIPS_AUXSYM: return "MIPS_AUXSYM";
4732 case SHT_MIPS_OPTSYM: return "MIPS_OPTSYM";
4733 case SHT_MIPS_LOCSTR: return "MIPS_LOCSTR";
4734 case SHT_MIPS_LINE: return "MIPS_LINE";
4735 case SHT_MIPS_RFDESC: return "MIPS_RFDESC";
4736 case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
4737 case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
4738 case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
4739 case SHT_MIPS_DWARF: return "MIPS_DWARF";
4740 case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
4741 case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
4742 case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
4743 case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
4744 case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
4745 case SHT_MIPS_XLATE: return "MIPS_XLATE";
4746 case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
4747 case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
4748 case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
4749 case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
4750 case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
4751 case SHT_MIPS_ABIFLAGS: return "MIPS_ABIFLAGS";
4752 case SHT_MIPS_XHASH: return "MIPS_XHASH";
4753 default:
4754 break;
4755 }
4756 return NULL;
4757 }
4758
4759 static const char *
4760 get_parisc_section_type_name (unsigned int sh_type)
4761 {
4762 switch (sh_type)
4763 {
4764 case SHT_PARISC_EXT: return "PARISC_EXT";
4765 case SHT_PARISC_UNWIND: return "PARISC_UNWIND";
4766 case SHT_PARISC_DOC: return "PARISC_DOC";
4767 case SHT_PARISC_ANNOT: return "PARISC_ANNOT";
4768 case SHT_PARISC_SYMEXTN: return "PARISC_SYMEXTN";
4769 case SHT_PARISC_STUBS: return "PARISC_STUBS";
4770 case SHT_PARISC_DLKM: return "PARISC_DLKM";
4771 default: return NULL;
4772 }
4773 }
4774
4775 static const char *
4776 get_ia64_section_type_name (Filedata * filedata, unsigned int sh_type)
4777 {
4778 /* If the top 8 bits are 0x78 the next 8 are the os/abi ID. */
4779 if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
4780 return get_osabi_name (filedata, (sh_type & 0x00FF0000) >> 16);
4781
4782 switch (sh_type)
4783 {
4784 case SHT_IA_64_EXT: return "IA_64_EXT";
4785 case SHT_IA_64_UNWIND: return "IA_64_UNWIND";
4786 case SHT_IA_64_PRIORITY_INIT: return "IA_64_PRIORITY_INIT";
4787 case SHT_IA_64_VMS_TRACE: return "VMS_TRACE";
4788 case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
4789 case SHT_IA_64_VMS_DEBUG: return "VMS_DEBUG";
4790 case SHT_IA_64_VMS_DEBUG_STR: return "VMS_DEBUG_STR";
4791 case SHT_IA_64_VMS_LINKAGES: return "VMS_LINKAGES";
4792 case SHT_IA_64_VMS_SYMBOL_VECTOR: return "VMS_SYMBOL_VECTOR";
4793 case SHT_IA_64_VMS_FIXUP: return "VMS_FIXUP";
4794 default:
4795 break;
4796 }
4797 return NULL;
4798 }
4799
4800 static const char *
4801 get_x86_64_section_type_name (unsigned int sh_type)
4802 {
4803 switch (sh_type)
4804 {
4805 case SHT_X86_64_UNWIND: return "X86_64_UNWIND";
4806 default: return NULL;
4807 }
4808 }
4809
4810 static const char *
4811 get_aarch64_section_type_name (unsigned int sh_type)
4812 {
4813 switch (sh_type)
4814 {
4815 case SHT_AARCH64_ATTRIBUTES: return "AARCH64_ATTRIBUTES";
4816 default: return NULL;
4817 }
4818 }
4819
4820 static const char *
4821 get_arm_section_type_name (unsigned int sh_type)
4822 {
4823 switch (sh_type)
4824 {
4825 case SHT_ARM_EXIDX: return "ARM_EXIDX";
4826 case SHT_ARM_PREEMPTMAP: return "ARM_PREEMPTMAP";
4827 case SHT_ARM_ATTRIBUTES: return "ARM_ATTRIBUTES";
4828 case SHT_ARM_DEBUGOVERLAY: return "ARM_DEBUGOVERLAY";
4829 case SHT_ARM_OVERLAYSECTION: return "ARM_OVERLAYSECTION";
4830 default: return NULL;
4831 }
4832 }
4833
4834 static const char *
4835 get_tic6x_section_type_name (unsigned int sh_type)
4836 {
4837 switch (sh_type)
4838 {
4839 case SHT_C6000_UNWIND: return "C6000_UNWIND";
4840 case SHT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
4841 case SHT_C6000_ATTRIBUTES: return "C6000_ATTRIBUTES";
4842 case SHT_TI_ICODE: return "TI_ICODE";
4843 case SHT_TI_XREF: return "TI_XREF";
4844 case SHT_TI_HANDLER: return "TI_HANDLER";
4845 case SHT_TI_INITINFO: return "TI_INITINFO";
4846 case SHT_TI_PHATTRS: return "TI_PHATTRS";
4847 default: return NULL;
4848 }
4849 }
4850
4851 static const char *
4852 get_msp430_section_type_name (unsigned int sh_type)
4853 {
4854 switch (sh_type)
4855 {
4856 case SHT_MSP430_SEC_FLAGS: return "MSP430_SEC_FLAGS";
4857 case SHT_MSP430_SYM_ALIASES: return "MSP430_SYM_ALIASES";
4858 case SHT_MSP430_ATTRIBUTES: return "MSP430_ATTRIBUTES";
4859 default: return NULL;
4860 }
4861 }
4862
4863 static const char *
4864 get_nfp_section_type_name (unsigned int sh_type)
4865 {
4866 switch (sh_type)
4867 {
4868 case SHT_NFP_MECONFIG: return "NFP_MECONFIG";
4869 case SHT_NFP_INITREG: return "NFP_INITREG";
4870 case SHT_NFP_UDEBUG: return "NFP_UDEBUG";
4871 default: return NULL;
4872 }
4873 }
4874
4875 static const char *
4876 get_v850_section_type_name (unsigned int sh_type)
4877 {
4878 switch (sh_type)
4879 {
4880 case SHT_V850_SCOMMON: return "V850 Small Common";
4881 case SHT_V850_TCOMMON: return "V850 Tiny Common";
4882 case SHT_V850_ZCOMMON: return "V850 Zero Common";
4883 case SHT_RENESAS_IOP: return "RENESAS IOP";
4884 case SHT_RENESAS_INFO: return "RENESAS INFO";
4885 default: return NULL;
4886 }
4887 }
4888
4889 static const char *
4890 get_riscv_section_type_name (unsigned int sh_type)
4891 {
4892 switch (sh_type)
4893 {
4894 case SHT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
4895 default: return NULL;
4896 }
4897 }
4898
4899 static const char *
4900 get_csky_section_type_name (unsigned int sh_type)
4901 {
4902 switch (sh_type)
4903 {
4904 case SHT_CSKY_ATTRIBUTES: return "CSKY_ATTRIBUTES";
4905 default: return NULL;
4906 }
4907 }
4908
4909 static const char *
4910 get_section_type_name (Filedata * filedata, unsigned int sh_type)
4911 {
4912 static char buff[32];
4913 const char * result;
4914
4915 switch (sh_type)
4916 {
4917 case SHT_NULL: return "NULL";
4918 case SHT_PROGBITS: return "PROGBITS";
4919 case SHT_SYMTAB: return "SYMTAB";
4920 case SHT_STRTAB: return "STRTAB";
4921 case SHT_RELA: return "RELA";
4922 case SHT_RELR: return "RELR";
4923 case SHT_HASH: return "HASH";
4924 case SHT_DYNAMIC: return "DYNAMIC";
4925 case SHT_NOTE: return "NOTE";
4926 case SHT_NOBITS: return "NOBITS";
4927 case SHT_REL: return "REL";
4928 case SHT_SHLIB: return "SHLIB";
4929 case SHT_DYNSYM: return "DYNSYM";
4930 case SHT_INIT_ARRAY: return "INIT_ARRAY";
4931 case SHT_FINI_ARRAY: return "FINI_ARRAY";
4932 case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
4933 case SHT_GNU_HASH: return "GNU_HASH";
4934 case SHT_GROUP: return "GROUP";
4935 case SHT_SYMTAB_SHNDX: return "SYMTAB SECTION INDICES";
4936 case SHT_GNU_verdef: return "VERDEF";
4937 case SHT_GNU_verneed: return "VERNEED";
4938 case SHT_GNU_versym: return "VERSYM";
4939 case 0x6ffffff0: return "VERSYM";
4940 case 0x6ffffffc: return "VERDEF";
4941 case 0x7ffffffd: return "AUXILIARY";
4942 case 0x7fffffff: return "FILTER";
4943 case SHT_GNU_LIBLIST: return "GNU_LIBLIST";
4944
4945 default:
4946 if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
4947 {
4948 switch (filedata->file_header.e_machine)
4949 {
4950 case EM_ARC:
4951 case EM_ARC_COMPACT:
4952 case EM_ARC_COMPACT2:
4953 result = get_arc_section_type_name (sh_type);
4954 break;
4955 case EM_MIPS:
4956 case EM_MIPS_RS3_LE:
4957 result = get_mips_section_type_name (sh_type);
4958 break;
4959 case EM_PARISC:
4960 result = get_parisc_section_type_name (sh_type);
4961 break;
4962 case EM_IA_64:
4963 result = get_ia64_section_type_name (filedata, sh_type);
4964 break;
4965 case EM_X86_64:
4966 case EM_L1OM:
4967 case EM_K1OM:
4968 result = get_x86_64_section_type_name (sh_type);
4969 break;
4970 case EM_AARCH64:
4971 result = get_aarch64_section_type_name (sh_type);
4972 break;
4973 case EM_ARM:
4974 result = get_arm_section_type_name (sh_type);
4975 break;
4976 case EM_TI_C6000:
4977 result = get_tic6x_section_type_name (sh_type);
4978 break;
4979 case EM_MSP430:
4980 result = get_msp430_section_type_name (sh_type);
4981 break;
4982 case EM_NFP:
4983 result = get_nfp_section_type_name (sh_type);
4984 break;
4985 case EM_V800:
4986 case EM_V850:
4987 case EM_CYGNUS_V850:
4988 result = get_v850_section_type_name (sh_type);
4989 break;
4990 case EM_RISCV:
4991 result = get_riscv_section_type_name (sh_type);
4992 break;
4993 case EM_CSKY:
4994 result = get_csky_section_type_name (sh_type);
4995 break;
4996 default:
4997 result = NULL;
4998 break;
4999 }
5000
5001 if (result != NULL)
5002 return result;
5003
5004 sprintf (buff, "LOPROC+%#x", sh_type - SHT_LOPROC);
5005 }
5006 else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
5007 {
5008 switch (filedata->file_header.e_machine)
5009 {
5010 case EM_IA_64:
5011 result = get_ia64_section_type_name (filedata, sh_type);
5012 break;
5013 default:
5014 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
5015 result = get_solaris_section_type (sh_type);
5016 else
5017 {
5018 switch (sh_type)
5019 {
5020 case SHT_GNU_INCREMENTAL_INPUTS: result = "GNU_INCREMENTAL_INPUTS"; break;
5021 case SHT_GNU_ATTRIBUTES: result = "GNU_ATTRIBUTES"; break;
5022 case SHT_GNU_HASH: result = "GNU_HASH"; break;
5023 case SHT_GNU_LIBLIST: result = "GNU_LIBLIST"; break;
5024 default:
5025 result = NULL;
5026 break;
5027 }
5028 }
5029 break;
5030 }
5031
5032 if (result != NULL)
5033 return result;
5034
5035 sprintf (buff, "LOOS+%#x", sh_type - SHT_LOOS);
5036 }
5037 else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
5038 {
5039 switch (filedata->file_header.e_machine)
5040 {
5041 case EM_V800:
5042 case EM_V850:
5043 case EM_CYGNUS_V850:
5044 result = get_v850_section_type_name (sh_type);
5045 break;
5046 default:
5047 result = NULL;
5048 break;
5049 }
5050
5051 if (result != NULL)
5052 return result;
5053
5054 sprintf (buff, "LOUSER+%#x", sh_type - SHT_LOUSER);
5055 }
5056 else
5057 /* This message is probably going to be displayed in a 15
5058 character wide field, so put the hex value first. */
5059 snprintf (buff, sizeof (buff), _("%08x: <unknown>"), sh_type);
5060
5061 return buff;
5062 }
5063 }
5064
5065 enum long_option_values
5066 {
5067 OPTION_DEBUG_DUMP = 512,
5068 OPTION_DYN_SYMS,
5069 OPTION_LTO_SYMS,
5070 OPTION_DWARF_DEPTH,
5071 OPTION_DWARF_START,
5072 OPTION_DWARF_CHECK,
5073 OPTION_CTF_DUMP,
5074 OPTION_CTF_PARENT,
5075 OPTION_CTF_SYMBOLS,
5076 OPTION_CTF_STRINGS,
5077 OPTION_SFRAME_DUMP,
5078 OPTION_WITH_SYMBOL_VERSIONS,
5079 OPTION_RECURSE_LIMIT,
5080 OPTION_NO_RECURSE_LIMIT,
5081 OPTION_NO_DEMANGLING,
5082 OPTION_SYM_BASE
5083 };
5084
5085 static struct option options[] =
5086 {
5087 /* Note - This table is alpha-sorted on the 'val'
5088 field in order to make adding new options easier. */
5089 {"arch-specific", no_argument, 0, 'A'},
5090 {"all", no_argument, 0, 'a'},
5091 {"demangle", optional_argument, 0, 'C'},
5092 {"archive-index", no_argument, 0, 'c'},
5093 {"use-dynamic", no_argument, 0, 'D'},
5094 {"dynamic", no_argument, 0, 'd'},
5095 {"headers", no_argument, 0, 'e'},
5096 {"section-groups", no_argument, 0, 'g'},
5097 {"help", no_argument, 0, 'H'},
5098 {"file-header", no_argument, 0, 'h'},
5099 {"histogram", no_argument, 0, 'I'},
5100 {"lint", no_argument, 0, 'L'},
5101 {"enable-checks", no_argument, 0, 'L'},
5102 {"program-headers", no_argument, 0, 'l'},
5103 {"segments", no_argument, 0, 'l'},
5104 {"full-section-name",no_argument, 0, 'N'},
5105 {"notes", no_argument, 0, 'n'},
5106 {"process-links", no_argument, 0, 'P'},
5107 {"string-dump", required_argument, 0, 'p'},
5108 {"relocated-dump", required_argument, 0, 'R'},
5109 {"relocs", no_argument, 0, 'r'},
5110 {"section-headers", no_argument, 0, 'S'},
5111 {"sections", no_argument, 0, 'S'},
5112 {"symbols", no_argument, 0, 's'},
5113 {"syms", no_argument, 0, 's'},
5114 {"silent-truncation",no_argument, 0, 'T'},
5115 {"section-details", no_argument, 0, 't'},
5116 {"unicode", required_argument, NULL, 'U'},
5117 {"unwind", no_argument, 0, 'u'},
5118 {"version-info", no_argument, 0, 'V'},
5119 {"version", no_argument, 0, 'v'},
5120 {"wide", no_argument, 0, 'W'},
5121 {"hex-dump", required_argument, 0, 'x'},
5122 {"decompress", no_argument, 0, 'z'},
5123
5124 {"no-demangle", no_argument, 0, OPTION_NO_DEMANGLING},
5125 {"recurse-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
5126 {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
5127 {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
5128 {"dyn-syms", no_argument, 0, OPTION_DYN_SYMS},
5129 {"lto-syms", no_argument, 0, OPTION_LTO_SYMS},
5130 {"debug-dump", optional_argument, 0, OPTION_DEBUG_DUMP},
5131 {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
5132 {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
5133 {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
5134 #ifdef ENABLE_LIBCTF
5135 {"ctf", required_argument, 0, OPTION_CTF_DUMP},
5136 {"ctf-symbols", required_argument, 0, OPTION_CTF_SYMBOLS},
5137 {"ctf-strings", required_argument, 0, OPTION_CTF_STRINGS},
5138 {"ctf-parent", required_argument, 0, OPTION_CTF_PARENT},
5139 #endif
5140 {"sframe", optional_argument, 0, OPTION_SFRAME_DUMP},
5141 {"sym-base", optional_argument, 0, OPTION_SYM_BASE},
5142
5143 {0, no_argument, 0, 0}
5144 };
5145
5146 static void
5147 usage (FILE * stream)
5148 {
5149 fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
5150 fprintf (stream, _(" Display information about the contents of ELF format files\n"));
5151 fprintf (stream, _(" Options are:\n"));
5152 fprintf (stream, _("\
5153 -a --all Equivalent to: -h -l -S -s -r -d -V -A -I\n"));
5154 fprintf (stream, _("\
5155 -h --file-header Display the ELF file header\n"));
5156 fprintf (stream, _("\
5157 -l --program-headers Display the program headers\n"));
5158 fprintf (stream, _("\
5159 --segments An alias for --program-headers\n"));
5160 fprintf (stream, _("\
5161 -S --section-headers Display the sections' header\n"));
5162 fprintf (stream, _("\
5163 --sections An alias for --section-headers\n"));
5164 fprintf (stream, _("\
5165 -g --section-groups Display the section groups\n"));
5166 fprintf (stream, _("\
5167 -t --section-details Display the section details\n"));
5168 fprintf (stream, _("\
5169 -e --headers Equivalent to: -h -l -S\n"));
5170 fprintf (stream, _("\
5171 -s --syms Display the symbol table\n"));
5172 fprintf (stream, _("\
5173 --symbols An alias for --syms\n"));
5174 fprintf (stream, _("\
5175 --dyn-syms Display the dynamic symbol table\n"));
5176 fprintf (stream, _("\
5177 --lto-syms Display LTO symbol tables\n"));
5178 fprintf (stream, _("\
5179 --sym-base=[0|8|10|16] \n\
5180 Force base for symbol sizes. The options are \n\
5181 mixed (the default), octal, decimal, hexadecimal.\n"));
5182 fprintf (stream, _("\
5183 -C --demangle[=STYLE] Decode mangled/processed symbol names\n"));
5184 display_demangler_styles (stream, _("\
5185 STYLE can be "));
5186 fprintf (stream, _("\
5187 --no-demangle Do not demangle low-level symbol names. (default)\n"));
5188 fprintf (stream, _("\
5189 --recurse-limit Enable a demangling recursion limit. (default)\n"));
5190 fprintf (stream, _("\
5191 --no-recurse-limit Disable a demangling recursion limit\n"));
5192 fprintf (stream, _("\
5193 -U[dlexhi] --unicode=[default|locale|escape|hex|highlight|invalid]\n\
5194 Display unicode characters as determined by the current locale\n\
5195 (default), escape sequences, \"<hex sequences>\", highlighted\n\
5196 escape sequences, or treat them as invalid and display as\n\
5197 \"{hex sequences}\"\n"));
5198 fprintf (stream, _("\
5199 -n --notes Display the core notes (if present)\n"));
5200 fprintf (stream, _("\
5201 -r --relocs Display the relocations (if present)\n"));
5202 fprintf (stream, _("\
5203 -u --unwind Display the unwind info (if present)\n"));
5204 fprintf (stream, _("\
5205 -d --dynamic Display the dynamic section (if present)\n"));
5206 fprintf (stream, _("\
5207 -V --version-info Display the version sections (if present)\n"));
5208 fprintf (stream, _("\
5209 -A --arch-specific Display architecture specific information (if any)\n"));
5210 fprintf (stream, _("\
5211 -c --archive-index Display the symbol/file index in an archive\n"));
5212 fprintf (stream, _("\
5213 -D --use-dynamic Use the dynamic section info when displaying symbols\n"));
5214 fprintf (stream, _("\
5215 -L --lint|--enable-checks\n\
5216 Display warning messages for possible problems\n"));
5217 fprintf (stream, _("\
5218 -x --hex-dump=<number|name>\n\
5219 Dump the contents of section <number|name> as bytes\n"));
5220 fprintf (stream, _("\
5221 -p --string-dump=<number|name>\n\
5222 Dump the contents of section <number|name> as strings\n"));
5223 fprintf (stream, _("\
5224 -R --relocated-dump=<number|name>\n\
5225 Dump the relocated contents of section <number|name>\n"));
5226 fprintf (stream, _("\
5227 -z --decompress Decompress section before dumping it\n"));
5228 fprintf (stream, _("\
5229 -w --debug-dump[a/=abbrev, A/=addr, r/=aranges, c/=cu_index, L/=decodedline,\n\
5230 f/=frames, F/=frames-interp, g/=gdb_index, i/=info, o/=loc,\n\
5231 m/=macro, p/=pubnames, t/=pubtypes, R/=Ranges, l/=rawline,\n\
5232 s/=str, O/=str-offsets, u/=trace_abbrev, T/=trace_aranges,\n\
5233 U/=trace_info]\n\
5234 Display the contents of DWARF debug sections\n"));
5235 fprintf (stream, _("\
5236 -wk --debug-dump=links Display the contents of sections that link to separate\n\
5237 debuginfo files\n"));
5238 fprintf (stream, _("\
5239 -P --process-links Display the contents of non-debug sections in separate\n\
5240 debuginfo files. (Implies -wK)\n"));
5241 #if DEFAULT_FOR_FOLLOW_LINKS
5242 fprintf (stream, _("\
5243 -wK --debug-dump=follow-links\n\
5244 Follow links to separate debug info files (default)\n"));
5245 fprintf (stream, _("\
5246 -wN --debug-dump=no-follow-links\n\
5247 Do not follow links to separate debug info files\n"));
5248 #else
5249 fprintf (stream, _("\
5250 -wK --debug-dump=follow-links\n\
5251 Follow links to separate debug info files\n"));
5252 fprintf (stream, _("\
5253 -wN --debug-dump=no-follow-links\n\
5254 Do not follow links to separate debug info files\n\
5255 (default)\n"));
5256 #endif
5257 #if HAVE_LIBDEBUGINFOD
5258 fprintf (stream, _("\
5259 -wD --debug-dump=use-debuginfod\n\
5260 When following links, also query debuginfod servers (default)\n"));
5261 fprintf (stream, _("\
5262 -wE --debug-dump=do-not-use-debuginfod\n\
5263 When following links, do not query debuginfod servers\n"));
5264 #endif
5265 fprintf (stream, _("\
5266 --dwarf-depth=N Do not display DIEs at depth N or greater\n"));
5267 fprintf (stream, _("\
5268 --dwarf-start=N Display DIEs starting at offset N\n"));
5269 #ifdef ENABLE_LIBCTF
5270 fprintf (stream, _("\
5271 --ctf=<number|name> Display CTF info from section <number|name>\n"));
5272 fprintf (stream, _("\
5273 --ctf-parent=<name> Use CTF archive member <name> as the CTF parent\n"));
5274 fprintf (stream, _("\
5275 --ctf-symbols=<number|name>\n\
5276 Use section <number|name> as the CTF external symtab\n"));
5277 fprintf (stream, _("\
5278 --ctf-strings=<number|name>\n\
5279 Use section <number|name> as the CTF external strtab\n"));
5280 #endif
5281 fprintf (stream, _("\
5282 --sframe[=NAME] Display SFrame info from section NAME, (default '.sframe')\n"));
5283
5284 #ifdef SUPPORT_DISASSEMBLY
5285 fprintf (stream, _("\
5286 -i --instruction-dump=<number|name>\n\
5287 Disassemble the contents of section <number|name>\n"));
5288 #endif
5289 fprintf (stream, _("\
5290 -I --histogram Display histogram of bucket list lengths\n"));
5291 fprintf (stream, _("\
5292 -W --wide Allow output width to exceed 80 characters\n"));
5293 fprintf (stream, _("\
5294 -T --silent-truncation If a symbol name is truncated, do not add [...] suffix\n"));
5295 fprintf (stream, _("\
5296 @<file> Read options from <file>\n"));
5297 fprintf (stream, _("\
5298 -H --help Display this information\n"));
5299 fprintf (stream, _("\
5300 -v --version Display the version number of readelf\n"));
5301
5302 if (REPORT_BUGS_TO[0] && stream == stdout)
5303 fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
5304
5305 exit (stream == stdout ? 0 : 1);
5306 }
5307
5308 /* Record the fact that the user wants the contents of section number
5309 SECTION to be displayed using the method(s) encoded as flags bits
5310 in TYPE. Note, TYPE can be zero if we are creating the array for
5311 the first time. */
5312
5313 static void
5314 request_dump_bynumber (struct dump_data *dumpdata,
5315 unsigned int section, dump_type type)
5316 {
5317 if (section >= dumpdata->num_dump_sects)
5318 {
5319 dump_type * new_dump_sects;
5320
5321 new_dump_sects = (dump_type *) calloc (section + 1,
5322 sizeof (* new_dump_sects));
5323
5324 if (new_dump_sects == NULL)
5325 error (_("Out of memory allocating dump request table.\n"));
5326 else
5327 {
5328 if (dumpdata->dump_sects)
5329 {
5330 /* Copy current flag settings. */
5331 memcpy (new_dump_sects, dumpdata->dump_sects,
5332 dumpdata->num_dump_sects * sizeof (* new_dump_sects));
5333
5334 free (dumpdata->dump_sects);
5335 }
5336
5337 dumpdata->dump_sects = new_dump_sects;
5338 dumpdata->num_dump_sects = section + 1;
5339 }
5340 }
5341
5342 if (dumpdata->dump_sects)
5343 dumpdata->dump_sects[section] |= type;
5344 }
5345
5346 /* Request a dump by section name. */
5347
5348 static void
5349 request_dump_byname (const char * section, dump_type type)
5350 {
5351 struct dump_list_entry * new_request;
5352
5353 new_request = (struct dump_list_entry *)
5354 malloc (sizeof (struct dump_list_entry));
5355 if (!new_request)
5356 error (_("Out of memory allocating dump request table.\n"));
5357
5358 new_request->name = strdup (section);
5359 if (!new_request->name)
5360 error (_("Out of memory allocating dump request table.\n"));
5361
5362 new_request->type = type;
5363
5364 new_request->next = dump_sects_byname;
5365 dump_sects_byname = new_request;
5366 }
5367
5368 static inline void
5369 request_dump (struct dump_data *dumpdata, dump_type type)
5370 {
5371 int section;
5372 char * cp;
5373
5374 do_dump = true;
5375 section = strtoul (optarg, & cp, 0);
5376
5377 if (! *cp && section >= 0)
5378 request_dump_bynumber (dumpdata, section, type);
5379 else
5380 request_dump_byname (optarg, type);
5381 }
5382
5383 static void
5384 parse_args (struct dump_data *dumpdata, int argc, char ** argv)
5385 {
5386 int c;
5387
5388 if (argc < 2)
5389 usage (stderr);
5390
5391 while ((c = getopt_long
5392 (argc, argv, "ACDHILNPR:STU:VWacdeghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
5393 {
5394 switch (c)
5395 {
5396 case 0:
5397 /* Long options. */
5398 break;
5399 case 'H':
5400 usage (stdout);
5401 break;
5402
5403 case 'a':
5404 do_syms = true;
5405 do_reloc = true;
5406 do_unwind = true;
5407 do_dynamic = true;
5408 do_header = true;
5409 do_sections = true;
5410 do_section_groups = true;
5411 do_segments = true;
5412 do_version = true;
5413 do_histogram = true;
5414 do_arch = true;
5415 do_notes = true;
5416 break;
5417
5418 case 'g':
5419 do_section_groups = true;
5420 break;
5421 case 't':
5422 case 'N':
5423 do_sections = true;
5424 do_section_details = true;
5425 break;
5426 case 'e':
5427 do_header = true;
5428 do_sections = true;
5429 do_segments = true;
5430 break;
5431 case 'A':
5432 do_arch = true;
5433 break;
5434 case 'D':
5435 do_using_dynamic = true;
5436 break;
5437 case 'r':
5438 do_reloc = true;
5439 break;
5440 case 'u':
5441 do_unwind = true;
5442 break;
5443 case 'h':
5444 do_header = true;
5445 break;
5446 case 'l':
5447 do_segments = true;
5448 break;
5449 case 's':
5450 do_syms = true;
5451 break;
5452 case 'S':
5453 do_sections = true;
5454 break;
5455 case 'd':
5456 do_dynamic = true;
5457 break;
5458 case 'I':
5459 do_histogram = true;
5460 break;
5461 case 'n':
5462 do_notes = true;
5463 break;
5464 case 'c':
5465 do_archive_index = true;
5466 break;
5467 case 'L':
5468 do_checks = true;
5469 break;
5470 case 'P':
5471 process_links = true;
5472 do_follow_links = true;
5473 dump_any_debugging = true;
5474 break;
5475 case 'x':
5476 request_dump (dumpdata, HEX_DUMP);
5477 break;
5478 case 'p':
5479 request_dump (dumpdata, STRING_DUMP);
5480 break;
5481 case 'R':
5482 request_dump (dumpdata, RELOC_DUMP);
5483 break;
5484 case 'z':
5485 decompress_dumps = true;
5486 break;
5487 case 'w':
5488 if (optarg == NULL)
5489 {
5490 do_debugging = true;
5491 do_dump = true;
5492 dump_any_debugging = true;
5493 dwarf_select_sections_all ();
5494 }
5495 else
5496 {
5497 do_debugging = false;
5498 if (dwarf_select_sections_by_letters (optarg))
5499 {
5500 do_dump = true;
5501 dump_any_debugging = true;
5502 }
5503 }
5504 break;
5505 case OPTION_DEBUG_DUMP:
5506 if (optarg == NULL)
5507 {
5508 do_dump = true;
5509 do_debugging = true;
5510 dump_any_debugging = true;
5511 dwarf_select_sections_all ();
5512 }
5513 else
5514 {
5515 do_debugging = false;
5516 if (dwarf_select_sections_by_names (optarg))
5517 {
5518 do_dump = true;
5519 dump_any_debugging = true;
5520 }
5521 }
5522 break;
5523 case OPTION_DWARF_DEPTH:
5524 {
5525 char *cp;
5526
5527 dwarf_cutoff_level = strtoul (optarg, & cp, 0);
5528 }
5529 break;
5530 case OPTION_DWARF_START:
5531 {
5532 char *cp;
5533
5534 dwarf_start_die = strtoul (optarg, & cp, 0);
5535 }
5536 break;
5537 case OPTION_DWARF_CHECK:
5538 dwarf_check = true;
5539 break;
5540 case OPTION_CTF_DUMP:
5541 do_ctf = true;
5542 request_dump (dumpdata, CTF_DUMP);
5543 break;
5544 case OPTION_CTF_SYMBOLS:
5545 free (dump_ctf_symtab_name);
5546 dump_ctf_symtab_name = strdup (optarg);
5547 break;
5548 case OPTION_CTF_STRINGS:
5549 free (dump_ctf_strtab_name);
5550 dump_ctf_strtab_name = strdup (optarg);
5551 break;
5552 case OPTION_CTF_PARENT:
5553 free (dump_ctf_parent_name);
5554 dump_ctf_parent_name = strdup (optarg);
5555 break;
5556 case OPTION_SFRAME_DUMP:
5557 do_sframe = true;
5558 /* Providing section name is optional. request_dump (), however,
5559 thrives on non NULL optarg. Handle it explicitly here. */
5560 if (optarg != NULL)
5561 request_dump (dumpdata, SFRAME_DUMP);
5562 else
5563 {
5564 do_dump = true;
5565 const char *sframe_sec_name = strdup (".sframe");
5566 request_dump_byname (sframe_sec_name, SFRAME_DUMP);
5567 }
5568 break;
5569 case OPTION_DYN_SYMS:
5570 do_dyn_syms = true;
5571 break;
5572 case OPTION_LTO_SYMS:
5573 do_lto_syms = true;
5574 break;
5575 #ifdef SUPPORT_DISASSEMBLY
5576 case 'i':
5577 request_dump (dumpdata, DISASS_DUMP);
5578 break;
5579 #endif
5580 case 'v':
5581 print_version (program_name);
5582 break;
5583 case 'V':
5584 do_version = true;
5585 break;
5586 case 'W':
5587 do_wide = true;
5588 break;
5589 case 'T':
5590 do_not_show_symbol_truncation = true;
5591 break;
5592 case 'C':
5593 do_demangle = true;
5594 if (optarg != NULL)
5595 {
5596 enum demangling_styles style;
5597
5598 style = cplus_demangle_name_to_style (optarg);
5599 if (style == unknown_demangling)
5600 error (_("unknown demangling style `%s'"), optarg);
5601
5602 cplus_demangle_set_style (style);
5603 }
5604 break;
5605 case OPTION_NO_DEMANGLING:
5606 do_demangle = false;
5607 break;
5608 case OPTION_RECURSE_LIMIT:
5609 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
5610 break;
5611 case OPTION_NO_RECURSE_LIMIT:
5612 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
5613 break;
5614 case OPTION_WITH_SYMBOL_VERSIONS:
5615 /* Ignored for backward compatibility. */
5616 break;
5617
5618 case 'U':
5619 if (optarg == NULL)
5620 error (_("Missing arg to -U/--unicode")); /* Can this happen ? */
5621 else if (streq (optarg, "default") || streq (optarg, "d"))
5622 unicode_display = unicode_default;
5623 else if (streq (optarg, "locale") || streq (optarg, "l"))
5624 unicode_display = unicode_locale;
5625 else if (streq (optarg, "escape") || streq (optarg, "e"))
5626 unicode_display = unicode_escape;
5627 else if (streq (optarg, "invalid") || streq (optarg, "i"))
5628 unicode_display = unicode_invalid;
5629 else if (streq (optarg, "hex") || streq (optarg, "x"))
5630 unicode_display = unicode_hex;
5631 else if (streq (optarg, "highlight") || streq (optarg, "h"))
5632 unicode_display = unicode_highlight;
5633 else
5634 error (_("invalid argument to -U/--unicode: %s"), optarg);
5635 break;
5636
5637 case OPTION_SYM_BASE:
5638 sym_base = 0;
5639 if (optarg != NULL)
5640 {
5641 sym_base = strtoul (optarg, NULL, 0);
5642 switch (sym_base)
5643 {
5644 case 0:
5645 case 8:
5646 case 10:
5647 case 16:
5648 break;
5649
5650 default:
5651 sym_base = 0;
5652 break;
5653 }
5654 }
5655 break;
5656
5657 default:
5658 /* xgettext:c-format */
5659 error (_("Invalid option '-%c'\n"), c);
5660 /* Fall through. */
5661 case '?':
5662 usage (stderr);
5663 }
5664 }
5665
5666 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
5667 && !do_segments && !do_header && !do_dump && !do_version
5668 && !do_histogram && !do_debugging && !do_arch && !do_notes
5669 && !do_section_groups && !do_archive_index
5670 && !do_dyn_syms && !do_lto_syms)
5671 {
5672 if (do_checks)
5673 {
5674 check_all = true;
5675 do_dynamic = do_syms = do_reloc = do_unwind = do_sections = true;
5676 do_segments = do_header = do_dump = do_version = true;
5677 do_histogram = do_debugging = do_arch = do_notes = true;
5678 do_section_groups = do_archive_index = do_dyn_syms = true;
5679 do_lto_syms = true;
5680 }
5681 else
5682 usage (stderr);
5683 }
5684 }
5685
5686 static const char *
5687 get_elf_class (unsigned int elf_class)
5688 {
5689 static char buff[32];
5690
5691 switch (elf_class)
5692 {
5693 case ELFCLASSNONE: return _("none");
5694 case ELFCLASS32: return "ELF32";
5695 case ELFCLASS64: return "ELF64";
5696 default:
5697 snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
5698 return buff;
5699 }
5700 }
5701
5702 static const char *
5703 get_data_encoding (unsigned int encoding)
5704 {
5705 static char buff[32];
5706
5707 switch (encoding)
5708 {
5709 case ELFDATANONE: return _("none");
5710 case ELFDATA2LSB: return _("2's complement, little endian");
5711 case ELFDATA2MSB: return _("2's complement, big endian");
5712 default:
5713 snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
5714 return buff;
5715 }
5716 }
5717
5718 static bool
5719 check_magic_number (Filedata * filedata, Elf_Internal_Ehdr * header)
5720 {
5721 if (header->e_ident[EI_MAG0] == ELFMAG0
5722 && header->e_ident[EI_MAG1] == ELFMAG1
5723 && header->e_ident[EI_MAG2] == ELFMAG2
5724 && header->e_ident[EI_MAG3] == ELFMAG3)
5725 return true;
5726
5727 /* Some compilers produce object files that are not in the ELF file format.
5728 As an aid to users of readelf, try to identify these cases and suggest
5729 alternative tools.
5730
5731 FIXME: It is not clear if all four bytes are used as constant magic
5732 valus by all compilers. It may be necessary to recode this function if
5733 different tools use different length sequences. */
5734
5735 static struct
5736 {
5737 unsigned char magic[4];
5738 const char * obj_message;
5739 const char * ar_message;
5740 }
5741 known_magic[] =
5742 {
5743 { { 'B', 'C', 0xc0, 0xde },
5744 N_("This is a LLVM bitcode file - try using llvm-bcanalyzer\n"),
5745 N_("This is a LLVM bitcode file - try extracing and then using llvm-bcanalyzer\n")
5746 },
5747 { { 'g', 'o', ' ', 'o' },
5748 N_("This is a GO binary file - try using 'go tool objdump' or 'go tool nm'\n"),
5749 NULL
5750 }
5751 };
5752 int i;
5753
5754 for (i = ARRAY_SIZE (known_magic); i--;)
5755 {
5756 if (header->e_ident[EI_MAG0] == known_magic[i].magic[0]
5757 && header->e_ident[EI_MAG1] == known_magic[i].magic[1]
5758 && header->e_ident[EI_MAG2] == known_magic[i].magic[2]
5759 && header->e_ident[EI_MAG3] == known_magic[i].magic[3])
5760 {
5761 /* Some compiler's analyzer tools do not handle archives,
5762 so we provide two different kinds of error message. */
5763 if (filedata->archive_file_size > 0
5764 && known_magic[i].ar_message != NULL)
5765 error ("%s", known_magic[i].ar_message);
5766 else
5767 error ("%s", known_magic[i].obj_message);
5768 return false;
5769 }
5770 }
5771
5772 error (_("Not an ELF file - it has the wrong magic bytes at the start\n"));
5773 return false;
5774 }
5775
5776 /* Decode the data held in 'filedata->file_header'. */
5777
5778 static bool
5779 process_file_header (Filedata * filedata)
5780 {
5781 Elf_Internal_Ehdr * header = & filedata->file_header;
5782
5783 if (! check_magic_number (filedata, header))
5784 return false;
5785
5786 if (! filedata->is_separate)
5787 init_dwarf_regnames_by_elf_machine_code (header->e_machine);
5788
5789 if (do_header)
5790 {
5791 unsigned i;
5792
5793 if (filedata->is_separate)
5794 printf (_("ELF Header in linked file '%s':\n"), filedata->file_name);
5795 else
5796 printf (_("ELF Header:\n"));
5797 printf (_(" Magic: "));
5798 for (i = 0; i < EI_NIDENT; i++)
5799 printf ("%2.2x ", header->e_ident[i]);
5800 printf ("\n");
5801 printf (_(" Class: %s\n"),
5802 get_elf_class (header->e_ident[EI_CLASS]));
5803 printf (_(" Data: %s\n"),
5804 get_data_encoding (header->e_ident[EI_DATA]));
5805 printf (_(" Version: %d%s\n"),
5806 header->e_ident[EI_VERSION],
5807 (header->e_ident[EI_VERSION] == EV_CURRENT
5808 ? _(" (current)")
5809 : (header->e_ident[EI_VERSION] != EV_NONE
5810 ? _(" <unknown>")
5811 : "")));
5812 printf (_(" OS/ABI: %s\n"),
5813 get_osabi_name (filedata, header->e_ident[EI_OSABI]));
5814 printf (_(" ABI Version: %d\n"),
5815 header->e_ident[EI_ABIVERSION]);
5816 printf (_(" Type: %s\n"),
5817 get_file_type (filedata));
5818 printf (_(" Machine: %s\n"),
5819 get_machine_name (header->e_machine));
5820 printf (_(" Version: 0x%lx\n"),
5821 header->e_version);
5822
5823 printf (_(" Entry point address: "));
5824 print_vma (header->e_entry, PREFIX_HEX);
5825 printf (_("\n Start of program headers: "));
5826 print_vma (header->e_phoff, DEC);
5827 printf (_(" (bytes into file)\n Start of section headers: "));
5828 print_vma (header->e_shoff, DEC);
5829 printf (_(" (bytes into file)\n"));
5830
5831 printf (_(" Flags: 0x%lx%s\n"),
5832 header->e_flags,
5833 get_machine_flags (filedata, header->e_flags, header->e_machine));
5834 printf (_(" Size of this header: %u (bytes)\n"),
5835 header->e_ehsize);
5836 printf (_(" Size of program headers: %u (bytes)\n"),
5837 header->e_phentsize);
5838 printf (_(" Number of program headers: %u"),
5839 header->e_phnum);
5840 if (filedata->section_headers != NULL
5841 && header->e_phnum == PN_XNUM
5842 && filedata->section_headers[0].sh_info != 0)
5843 printf (" (%u)", filedata->section_headers[0].sh_info);
5844 putc ('\n', stdout);
5845 printf (_(" Size of section headers: %u (bytes)\n"),
5846 header->e_shentsize);
5847 printf (_(" Number of section headers: %u"),
5848 header->e_shnum);
5849 if (filedata->section_headers != NULL && header->e_shnum == SHN_UNDEF)
5850 {
5851 header->e_shnum = filedata->section_headers[0].sh_size;
5852 printf (" (%u)", header->e_shnum);
5853 }
5854 putc ('\n', stdout);
5855 printf (_(" Section header string table index: %u"),
5856 header->e_shstrndx);
5857 if (filedata->section_headers != NULL
5858 && header->e_shstrndx == (SHN_XINDEX & 0xffff))
5859 {
5860 header->e_shstrndx = filedata->section_headers[0].sh_link;
5861 printf (" (%u)", header->e_shstrndx);
5862 }
5863 if (header->e_shstrndx != SHN_UNDEF
5864 && header->e_shstrndx >= header->e_shnum)
5865 {
5866 header->e_shstrndx = SHN_UNDEF;
5867 printf (_(" <corrupt: out of range>"));
5868 }
5869 putc ('\n', stdout);
5870 }
5871
5872 if (filedata->section_headers != NULL)
5873 {
5874 if (header->e_phnum == PN_XNUM
5875 && filedata->section_headers[0].sh_info != 0)
5876 {
5877 /* Throw away any cached read of PN_XNUM headers. */
5878 free (filedata->program_headers);
5879 filedata->program_headers = NULL;
5880 header->e_phnum = filedata->section_headers[0].sh_info;
5881 }
5882 if (header->e_shnum == SHN_UNDEF)
5883 header->e_shnum = filedata->section_headers[0].sh_size;
5884 if (header->e_shstrndx == (SHN_XINDEX & 0xffff))
5885 header->e_shstrndx = filedata->section_headers[0].sh_link;
5886 if (header->e_shstrndx >= header->e_shnum)
5887 header->e_shstrndx = SHN_UNDEF;
5888 }
5889
5890 return true;
5891 }
5892
5893 /* Read in the program headers from FILEDATA and store them in PHEADERS.
5894 Returns TRUE upon success, FALSE otherwise. Loads 32-bit headers. */
5895
5896 static bool
5897 get_32bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
5898 {
5899 Elf32_External_Phdr * phdrs;
5900 Elf32_External_Phdr * external;
5901 Elf_Internal_Phdr * internal;
5902 unsigned int i;
5903 unsigned int size = filedata->file_header.e_phentsize;
5904 unsigned int num = filedata->file_header.e_phnum;
5905
5906 /* PR binutils/17531: Cope with unexpected section header sizes. */
5907 if (size == 0 || num == 0)
5908 return false;
5909 if (size < sizeof * phdrs)
5910 {
5911 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
5912 return false;
5913 }
5914 if (size > sizeof * phdrs)
5915 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
5916
5917 phdrs = (Elf32_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
5918 size, num, _("program headers"));
5919 if (phdrs == NULL)
5920 return false;
5921
5922 for (i = 0, internal = pheaders, external = phdrs;
5923 i < filedata->file_header.e_phnum;
5924 i++, internal++, external++)
5925 {
5926 internal->p_type = BYTE_GET (external->p_type);
5927 internal->p_offset = BYTE_GET (external->p_offset);
5928 internal->p_vaddr = BYTE_GET (external->p_vaddr);
5929 internal->p_paddr = BYTE_GET (external->p_paddr);
5930 internal->p_filesz = BYTE_GET (external->p_filesz);
5931 internal->p_memsz = BYTE_GET (external->p_memsz);
5932 internal->p_flags = BYTE_GET (external->p_flags);
5933 internal->p_align = BYTE_GET (external->p_align);
5934 }
5935
5936 free (phdrs);
5937 return true;
5938 }
5939
5940 /* Read in the program headers from FILEDATA and store them in PHEADERS.
5941 Returns TRUE upon success, FALSE otherwise. Loads 64-bit headers. */
5942
5943 static bool
5944 get_64bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
5945 {
5946 Elf64_External_Phdr * phdrs;
5947 Elf64_External_Phdr * external;
5948 Elf_Internal_Phdr * internal;
5949 unsigned int i;
5950 unsigned int size = filedata->file_header.e_phentsize;
5951 unsigned int num = filedata->file_header.e_phnum;
5952
5953 /* PR binutils/17531: Cope with unexpected section header sizes. */
5954 if (size == 0 || num == 0)
5955 return false;
5956 if (size < sizeof * phdrs)
5957 {
5958 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
5959 return false;
5960 }
5961 if (size > sizeof * phdrs)
5962 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
5963
5964 phdrs = (Elf64_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
5965 size, num, _("program headers"));
5966 if (!phdrs)
5967 return false;
5968
5969 for (i = 0, internal = pheaders, external = phdrs;
5970 i < filedata->file_header.e_phnum;
5971 i++, internal++, external++)
5972 {
5973 internal->p_type = BYTE_GET (external->p_type);
5974 internal->p_flags = BYTE_GET (external->p_flags);
5975 internal->p_offset = BYTE_GET (external->p_offset);
5976 internal->p_vaddr = BYTE_GET (external->p_vaddr);
5977 internal->p_paddr = BYTE_GET (external->p_paddr);
5978 internal->p_filesz = BYTE_GET (external->p_filesz);
5979 internal->p_memsz = BYTE_GET (external->p_memsz);
5980 internal->p_align = BYTE_GET (external->p_align);
5981 }
5982
5983 free (phdrs);
5984 return true;
5985 }
5986
5987 /* Returns TRUE if the program headers were read into `program_headers'. */
5988
5989 static bool
5990 get_program_headers (Filedata * filedata)
5991 {
5992 Elf_Internal_Phdr * phdrs;
5993
5994 /* Check cache of prior read. */
5995 if (filedata->program_headers != NULL)
5996 return true;
5997
5998 /* Be kind to memory checkers by looking for
5999 e_phnum values which we know must be invalid. */
6000 if (filedata->file_header.e_phnum
6001 * (is_32bit_elf ? sizeof (Elf32_External_Phdr) : sizeof (Elf64_External_Phdr))
6002 >= filedata->file_size)
6003 {
6004 error (_("Too many program headers - %#x - the file is not that big\n"),
6005 filedata->file_header.e_phnum);
6006 return false;
6007 }
6008
6009 phdrs = (Elf_Internal_Phdr *) cmalloc (filedata->file_header.e_phnum,
6010 sizeof (Elf_Internal_Phdr));
6011 if (phdrs == NULL)
6012 {
6013 error (_("Out of memory reading %u program headers\n"),
6014 filedata->file_header.e_phnum);
6015 return false;
6016 }
6017
6018 if (is_32bit_elf
6019 ? get_32bit_program_headers (filedata, phdrs)
6020 : get_64bit_program_headers (filedata, phdrs))
6021 {
6022 filedata->program_headers = phdrs;
6023 return true;
6024 }
6025
6026 free (phdrs);
6027 return false;
6028 }
6029
6030 /* Print program header info and locate dynamic section. */
6031
6032 static void
6033 process_program_headers (Filedata * filedata)
6034 {
6035 Elf_Internal_Phdr * segment;
6036 unsigned int i;
6037 Elf_Internal_Phdr * previous_load = NULL;
6038
6039 if (filedata->file_header.e_phnum == 0)
6040 {
6041 /* PR binutils/12467. */
6042 if (filedata->file_header.e_phoff != 0)
6043 warn (_("possibly corrupt ELF header - it has a non-zero program"
6044 " header offset, but no program headers\n"));
6045 else if (do_segments)
6046 {
6047 if (filedata->is_separate)
6048 printf (_("\nThere are no program headers in linked file '%s'.\n"),
6049 filedata->file_name);
6050 else
6051 printf (_("\nThere are no program headers in this file.\n"));
6052 }
6053 goto no_headers;
6054 }
6055
6056 if (do_segments && !do_header)
6057 {
6058 if (filedata->is_separate)
6059 printf ("\nIn linked file '%s' the ELF file type is %s\n",
6060 filedata->file_name, get_file_type (filedata));
6061 else
6062 printf (_("\nElf file type is %s\n"), get_file_type (filedata));
6063 printf (_("Entry point 0x%" PRIx64 "\n"),
6064 filedata->file_header.e_entry);
6065 printf (ngettext ("There is %d program header,"
6066 " starting at offset %" PRIu64 "\n",
6067 "There are %d program headers,"
6068 " starting at offset %" PRIu64 "\n",
6069 filedata->file_header.e_phnum),
6070 filedata->file_header.e_phnum,
6071 filedata->file_header.e_phoff);
6072 }
6073
6074 if (! get_program_headers (filedata))
6075 goto no_headers;
6076
6077 if (do_segments)
6078 {
6079 if (filedata->file_header.e_phnum > 1)
6080 printf (_("\nProgram Headers:\n"));
6081 else
6082 printf (_("\nProgram Headers:\n"));
6083
6084 if (is_32bit_elf)
6085 printf
6086 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
6087 else if (do_wide)
6088 printf
6089 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
6090 else
6091 {
6092 printf
6093 (_(" Type Offset VirtAddr PhysAddr\n"));
6094 printf
6095 (_(" FileSiz MemSiz Flags Align\n"));
6096 }
6097 }
6098
6099 unsigned long dynamic_addr = 0;
6100 uint64_t dynamic_size = 0;
6101 for (i = 0, segment = filedata->program_headers;
6102 i < filedata->file_header.e_phnum;
6103 i++, segment++)
6104 {
6105 if (do_segments)
6106 {
6107 printf (" %-14.14s ", get_segment_type (filedata, segment->p_type));
6108
6109 if (is_32bit_elf)
6110 {
6111 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
6112 printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
6113 printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
6114 printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
6115 printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
6116 printf ("%c%c%c ",
6117 (segment->p_flags & PF_R ? 'R' : ' '),
6118 (segment->p_flags & PF_W ? 'W' : ' '),
6119 (segment->p_flags & PF_X ? 'E' : ' '));
6120 printf ("%#lx", (unsigned long) segment->p_align);
6121 }
6122 else if (do_wide)
6123 {
6124 if ((unsigned long) segment->p_offset == segment->p_offset)
6125 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
6126 else
6127 {
6128 print_vma (segment->p_offset, FULL_HEX);
6129 putchar (' ');
6130 }
6131
6132 print_vma (segment->p_vaddr, FULL_HEX);
6133 putchar (' ');
6134 print_vma (segment->p_paddr, FULL_HEX);
6135 putchar (' ');
6136
6137 if ((unsigned long) segment->p_filesz == segment->p_filesz)
6138 printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
6139 else
6140 {
6141 print_vma (segment->p_filesz, FULL_HEX);
6142 putchar (' ');
6143 }
6144
6145 if ((unsigned long) segment->p_memsz == segment->p_memsz)
6146 printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
6147 else
6148 {
6149 print_vma (segment->p_memsz, FULL_HEX);
6150 }
6151
6152 printf (" %c%c%c ",
6153 (segment->p_flags & PF_R ? 'R' : ' '),
6154 (segment->p_flags & PF_W ? 'W' : ' '),
6155 (segment->p_flags & PF_X ? 'E' : ' '));
6156
6157 if ((unsigned long) segment->p_align == segment->p_align)
6158 printf ("%#lx", (unsigned long) segment->p_align);
6159 else
6160 {
6161 print_vma (segment->p_align, PREFIX_HEX);
6162 }
6163 }
6164 else
6165 {
6166 print_vma (segment->p_offset, FULL_HEX);
6167 putchar (' ');
6168 print_vma (segment->p_vaddr, FULL_HEX);
6169 putchar (' ');
6170 print_vma (segment->p_paddr, FULL_HEX);
6171 printf ("\n ");
6172 print_vma (segment->p_filesz, FULL_HEX);
6173 putchar (' ');
6174 print_vma (segment->p_memsz, FULL_HEX);
6175 printf (" %c%c%c ",
6176 (segment->p_flags & PF_R ? 'R' : ' '),
6177 (segment->p_flags & PF_W ? 'W' : ' '),
6178 (segment->p_flags & PF_X ? 'E' : ' '));
6179 print_vma (segment->p_align, PREFIX_HEX);
6180 }
6181
6182 putc ('\n', stdout);
6183 }
6184
6185 switch (segment->p_type)
6186 {
6187 case PT_LOAD:
6188 #if 0 /* Do not warn about out of order PT_LOAD segments. Although officially
6189 required by the ELF standard, several programs, including the Linux
6190 kernel, make use of non-ordered segments. */
6191 if (previous_load
6192 && previous_load->p_vaddr > segment->p_vaddr)
6193 error (_("LOAD segments must be sorted in order of increasing VirtAddr\n"));
6194 #endif
6195 if (segment->p_memsz < segment->p_filesz)
6196 error (_("the segment's file size is larger than its memory size\n"));
6197 previous_load = segment;
6198 break;
6199
6200 case PT_PHDR:
6201 /* PR 20815 - Verify that the program header is loaded into memory. */
6202 if (i > 0 && previous_load != NULL)
6203 error (_("the PHDR segment must occur before any LOAD segment\n"));
6204 if (filedata->file_header.e_machine != EM_PARISC)
6205 {
6206 unsigned int j;
6207
6208 for (j = 1; j < filedata->file_header.e_phnum; j++)
6209 {
6210 Elf_Internal_Phdr *load = filedata->program_headers + j;
6211 if (load->p_type == PT_LOAD
6212 && load->p_offset <= segment->p_offset
6213 && (load->p_offset + load->p_filesz
6214 >= segment->p_offset + segment->p_filesz)
6215 && load->p_vaddr <= segment->p_vaddr
6216 && (load->p_vaddr + load->p_filesz
6217 >= segment->p_vaddr + segment->p_filesz))
6218 break;
6219 }
6220 if (j == filedata->file_header.e_phnum)
6221 error (_("the PHDR segment is not covered by a LOAD segment\n"));
6222 }
6223 break;
6224
6225 case PT_DYNAMIC:
6226 if (dynamic_addr)
6227 error (_("more than one dynamic segment\n"));
6228
6229 /* By default, assume that the .dynamic section is the first
6230 section in the DYNAMIC segment. */
6231 dynamic_addr = segment->p_offset;
6232 dynamic_size = segment->p_filesz;
6233
6234 /* Try to locate the .dynamic section. If there is
6235 a section header table, we can easily locate it. */
6236 if (filedata->section_headers != NULL)
6237 {
6238 Elf_Internal_Shdr * sec;
6239
6240 sec = find_section (filedata, ".dynamic");
6241 if (sec == NULL || sec->sh_size == 0)
6242 {
6243 /* A corresponding .dynamic section is expected, but on
6244 IA-64/OpenVMS it is OK for it to be missing. */
6245 if (!is_ia64_vms (filedata))
6246 error (_("no .dynamic section in the dynamic segment\n"));
6247 break;
6248 }
6249
6250 if (sec->sh_type == SHT_NOBITS)
6251 {
6252 dynamic_addr = 0;
6253 dynamic_size = 0;
6254 break;
6255 }
6256
6257 dynamic_addr = sec->sh_offset;
6258 dynamic_size = sec->sh_size;
6259
6260 /* The PT_DYNAMIC segment, which is used by the run-time
6261 loader, should exactly match the .dynamic section. */
6262 if (do_checks
6263 && (dynamic_addr != segment->p_offset
6264 || dynamic_size != segment->p_filesz))
6265 warn (_("\
6266 the .dynamic section is not the same as the dynamic segment\n"));
6267 }
6268
6269 /* PR binutils/17512: Avoid corrupt dynamic section info in the
6270 segment. Check this after matching against the section headers
6271 so we don't warn on debuginfo file (which have NOBITS .dynamic
6272 sections). */
6273 if (dynamic_addr > filedata->file_size
6274 || (dynamic_size > filedata->file_size - dynamic_addr))
6275 {
6276 error (_("the dynamic segment offset + size exceeds the size of the file\n"));
6277 dynamic_addr = 0;
6278 dynamic_size = 0;
6279 }
6280 break;
6281
6282 case PT_INTERP:
6283 if (segment->p_offset >= filedata->file_size
6284 || segment->p_filesz > filedata->file_size - segment->p_offset
6285 || segment->p_filesz - 1 >= (size_t) -2
6286 || fseek (filedata->handle,
6287 filedata->archive_file_offset + (long) segment->p_offset,
6288 SEEK_SET))
6289 error (_("Unable to find program interpreter name\n"));
6290 else
6291 {
6292 size_t len = segment->p_filesz;
6293 free (filedata->program_interpreter);
6294 filedata->program_interpreter = xmalloc (len + 1);
6295 len = fread (filedata->program_interpreter, 1, len,
6296 filedata->handle);
6297 filedata->program_interpreter[len] = 0;
6298
6299 if (do_segments)
6300 printf (_(" [Requesting program interpreter: %s]\n"),
6301 filedata->program_interpreter);
6302 }
6303 break;
6304 }
6305 }
6306
6307 if (do_segments
6308 && filedata->section_headers != NULL
6309 && filedata->string_table != NULL)
6310 {
6311 printf (_("\n Section to Segment mapping:\n"));
6312 printf (_(" Segment Sections...\n"));
6313
6314 for (i = 0; i < filedata->file_header.e_phnum; i++)
6315 {
6316 unsigned int j;
6317 Elf_Internal_Shdr * section;
6318
6319 segment = filedata->program_headers + i;
6320 section = filedata->section_headers + 1;
6321
6322 printf (" %2.2d ", i);
6323
6324 for (j = 1; j < filedata->file_header.e_shnum; j++, section++)
6325 {
6326 if (!ELF_TBSS_SPECIAL (section, segment)
6327 && ELF_SECTION_IN_SEGMENT_STRICT (section, segment))
6328 printf ("%s ", printable_section_name (filedata, section));
6329 }
6330
6331 putc ('\n',stdout);
6332 }
6333 }
6334
6335 filedata->dynamic_addr = dynamic_addr;
6336 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
6337 return;
6338
6339 no_headers:
6340 filedata->dynamic_addr = 0;
6341 filedata->dynamic_size = 1;
6342 }
6343
6344
6345 /* Find the file offset corresponding to VMA by using the program headers. */
6346
6347 static long
6348 offset_from_vma (Filedata * filedata, uint64_t vma, uint64_t size)
6349 {
6350 Elf_Internal_Phdr * seg;
6351
6352 if (! get_program_headers (filedata))
6353 {
6354 warn (_("Cannot interpret virtual addresses without program headers.\n"));
6355 return (long) vma;
6356 }
6357
6358 for (seg = filedata->program_headers;
6359 seg < filedata->program_headers + filedata->file_header.e_phnum;
6360 ++seg)
6361 {
6362 if (seg->p_type != PT_LOAD)
6363 continue;
6364
6365 if (vma >= (seg->p_vaddr & -seg->p_align)
6366 && vma + size <= seg->p_vaddr + seg->p_filesz)
6367 return vma - seg->p_vaddr + seg->p_offset;
6368 }
6369
6370 warn (_("Virtual address 0x%lx not located in any PT_LOAD segment.\n"),
6371 (unsigned long) vma);
6372 return (long) vma;
6373 }
6374
6375
6376 /* Allocate memory and load the sections headers into FILEDATA->filedata->section_headers.
6377 If PROBE is true, this is just a probe and we do not generate any error
6378 messages if the load fails. */
6379
6380 static bool
6381 get_32bit_section_headers (Filedata * filedata, bool probe)
6382 {
6383 Elf32_External_Shdr * shdrs;
6384 Elf_Internal_Shdr * internal;
6385 unsigned int i;
6386 unsigned int size = filedata->file_header.e_shentsize;
6387 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
6388
6389 /* PR binutils/17531: Cope with unexpected section header sizes. */
6390 if (size == 0 || num == 0)
6391 return false;
6392
6393 /* The section header cannot be at the start of the file - that is
6394 where the ELF file header is located. A file with absolutely no
6395 sections in it will use a shoff of 0. */
6396 if (filedata->file_header.e_shoff == 0)
6397 return false;
6398
6399 if (size < sizeof * shdrs)
6400 {
6401 if (! probe)
6402 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
6403 return false;
6404 }
6405 if (!probe && size > sizeof * shdrs)
6406 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
6407
6408 shdrs = (Elf32_External_Shdr *) get_data (NULL, filedata, filedata->file_header.e_shoff,
6409 size, num,
6410 probe ? NULL : _("section headers"));
6411 if (shdrs == NULL)
6412 return false;
6413
6414 filedata->section_headers = (Elf_Internal_Shdr *)
6415 cmalloc (num, sizeof (Elf_Internal_Shdr));
6416 if (filedata->section_headers == NULL)
6417 {
6418 if (!probe)
6419 error (_("Out of memory reading %u section headers\n"), num);
6420 free (shdrs);
6421 return false;
6422 }
6423
6424 for (i = 0, internal = filedata->section_headers;
6425 i < num;
6426 i++, internal++)
6427 {
6428 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
6429 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
6430 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
6431 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
6432 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
6433 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
6434 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
6435 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
6436 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
6437 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
6438 if (!probe && internal->sh_link > num)
6439 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
6440 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
6441 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
6442 }
6443
6444 free (shdrs);
6445 return true;
6446 }
6447
6448 /* Like get_32bit_section_headers, except that it fetches 64-bit headers. */
6449
6450 static bool
6451 get_64bit_section_headers (Filedata * filedata, bool probe)
6452 {
6453 Elf64_External_Shdr * shdrs;
6454 Elf_Internal_Shdr * internal;
6455 unsigned int i;
6456 unsigned int size = filedata->file_header.e_shentsize;
6457 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
6458
6459 /* PR binutils/17531: Cope with unexpected section header sizes. */
6460 if (size == 0 || num == 0)
6461 return false;
6462
6463 /* The section header cannot be at the start of the file - that is
6464 where the ELF file header is located. A file with absolutely no
6465 sections in it will use a shoff of 0. */
6466 if (filedata->file_header.e_shoff == 0)
6467 return false;
6468
6469 if (size < sizeof * shdrs)
6470 {
6471 if (! probe)
6472 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
6473 return false;
6474 }
6475
6476 if (! probe && size > sizeof * shdrs)
6477 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
6478
6479 shdrs = (Elf64_External_Shdr *) get_data (NULL, filedata,
6480 filedata->file_header.e_shoff,
6481 size, num,
6482 probe ? NULL : _("section headers"));
6483 if (shdrs == NULL)
6484 return false;
6485
6486 filedata->section_headers = (Elf_Internal_Shdr *)
6487 cmalloc (num, sizeof (Elf_Internal_Shdr));
6488 if (filedata->section_headers == NULL)
6489 {
6490 if (! probe)
6491 error (_("Out of memory reading %u section headers\n"), num);
6492 free (shdrs);
6493 return false;
6494 }
6495
6496 for (i = 0, internal = filedata->section_headers;
6497 i < num;
6498 i++, internal++)
6499 {
6500 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
6501 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
6502 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
6503 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
6504 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
6505 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
6506 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
6507 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
6508 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
6509 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
6510 if (!probe && internal->sh_link > num)
6511 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
6512 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
6513 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
6514 }
6515
6516 free (shdrs);
6517 return true;
6518 }
6519
6520 static bool
6521 get_section_headers (Filedata *filedata, bool probe)
6522 {
6523 if (filedata->section_headers != NULL)
6524 return true;
6525
6526 if (is_32bit_elf)
6527 return get_32bit_section_headers (filedata, probe);
6528 else
6529 return get_64bit_section_headers (filedata, probe);
6530 }
6531
6532 static Elf_Internal_Sym *
6533 get_32bit_elf_symbols (Filedata * filedata,
6534 Elf_Internal_Shdr * section,
6535 unsigned long * num_syms_return)
6536 {
6537 unsigned long number = 0;
6538 Elf32_External_Sym * esyms = NULL;
6539 Elf_External_Sym_Shndx * shndx = NULL;
6540 Elf_Internal_Sym * isyms = NULL;
6541 Elf_Internal_Sym * psym;
6542 unsigned int j;
6543 elf_section_list * entry;
6544
6545 if (section->sh_size == 0)
6546 {
6547 if (num_syms_return != NULL)
6548 * num_syms_return = 0;
6549 return NULL;
6550 }
6551
6552 /* Run some sanity checks first. */
6553 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
6554 {
6555 error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
6556 printable_section_name (filedata, section),
6557 (unsigned long) section->sh_entsize);
6558 goto exit_point;
6559 }
6560
6561 if (section->sh_size > filedata->file_size)
6562 {
6563 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
6564 printable_section_name (filedata, section),
6565 (unsigned long) section->sh_size);
6566 goto exit_point;
6567 }
6568
6569 number = section->sh_size / section->sh_entsize;
6570
6571 if (number * sizeof (Elf32_External_Sym) > section->sh_size + 1)
6572 {
6573 error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
6574 (unsigned long) section->sh_size,
6575 printable_section_name (filedata, section),
6576 (unsigned long) section->sh_entsize);
6577 goto exit_point;
6578 }
6579
6580 esyms = (Elf32_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
6581 section->sh_size, _("symbols"));
6582 if (esyms == NULL)
6583 goto exit_point;
6584
6585 shndx = NULL;
6586 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
6587 {
6588 if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
6589 continue;
6590
6591 if (shndx != NULL)
6592 {
6593 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
6594 free (shndx);
6595 }
6596
6597 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
6598 entry->hdr->sh_offset,
6599 1, entry->hdr->sh_size,
6600 _("symbol table section indices"));
6601 if (shndx == NULL)
6602 goto exit_point;
6603
6604 /* PR17531: file: heap-buffer-overflow */
6605 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
6606 {
6607 error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
6608 printable_section_name (filedata, entry->hdr),
6609 (unsigned long) entry->hdr->sh_size,
6610 (unsigned long) section->sh_size);
6611 goto exit_point;
6612 }
6613 }
6614
6615 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
6616
6617 if (isyms == NULL)
6618 {
6619 error (_("Out of memory reading %lu symbols\n"),
6620 (unsigned long) number);
6621 goto exit_point;
6622 }
6623
6624 for (j = 0, psym = isyms; j < number; j++, psym++)
6625 {
6626 psym->st_name = BYTE_GET (esyms[j].st_name);
6627 psym->st_value = BYTE_GET (esyms[j].st_value);
6628 psym->st_size = BYTE_GET (esyms[j].st_size);
6629 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
6630 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
6631 psym->st_shndx
6632 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
6633 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
6634 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
6635 psym->st_info = BYTE_GET (esyms[j].st_info);
6636 psym->st_other = BYTE_GET (esyms[j].st_other);
6637 }
6638
6639 exit_point:
6640 free (shndx);
6641 free (esyms);
6642
6643 if (num_syms_return != NULL)
6644 * num_syms_return = isyms == NULL ? 0 : number;
6645
6646 return isyms;
6647 }
6648
6649 static Elf_Internal_Sym *
6650 get_64bit_elf_symbols (Filedata * filedata,
6651 Elf_Internal_Shdr * section,
6652 unsigned long * num_syms_return)
6653 {
6654 unsigned long number = 0;
6655 Elf64_External_Sym * esyms = NULL;
6656 Elf_External_Sym_Shndx * shndx = NULL;
6657 Elf_Internal_Sym * isyms = NULL;
6658 Elf_Internal_Sym * psym;
6659 unsigned int j;
6660 elf_section_list * entry;
6661
6662 if (section->sh_size == 0)
6663 {
6664 if (num_syms_return != NULL)
6665 * num_syms_return = 0;
6666 return NULL;
6667 }
6668
6669 /* Run some sanity checks first. */
6670 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
6671 {
6672 error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
6673 printable_section_name (filedata, section),
6674 (unsigned long) section->sh_entsize);
6675 goto exit_point;
6676 }
6677
6678 if (section->sh_size > filedata->file_size)
6679 {
6680 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
6681 printable_section_name (filedata, section),
6682 (unsigned long) section->sh_size);
6683 goto exit_point;
6684 }
6685
6686 number = section->sh_size / section->sh_entsize;
6687
6688 if (number * sizeof (Elf64_External_Sym) > section->sh_size + 1)
6689 {
6690 error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
6691 (unsigned long) section->sh_size,
6692 printable_section_name (filedata, section),
6693 (unsigned long) section->sh_entsize);
6694 goto exit_point;
6695 }
6696
6697 esyms = (Elf64_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
6698 section->sh_size, _("symbols"));
6699 if (!esyms)
6700 goto exit_point;
6701
6702 shndx = NULL;
6703 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
6704 {
6705 if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
6706 continue;
6707
6708 if (shndx != NULL)
6709 {
6710 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
6711 free (shndx);
6712 }
6713
6714 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
6715 entry->hdr->sh_offset,
6716 1, entry->hdr->sh_size,
6717 _("symbol table section indices"));
6718 if (shndx == NULL)
6719 goto exit_point;
6720
6721 /* PR17531: file: heap-buffer-overflow */
6722 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
6723 {
6724 error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
6725 printable_section_name (filedata, entry->hdr),
6726 (unsigned long) entry->hdr->sh_size,
6727 (unsigned long) section->sh_size);
6728 goto exit_point;
6729 }
6730 }
6731
6732 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
6733
6734 if (isyms == NULL)
6735 {
6736 error (_("Out of memory reading %lu symbols\n"),
6737 (unsigned long) number);
6738 goto exit_point;
6739 }
6740
6741 for (j = 0, psym = isyms; j < number; j++, psym++)
6742 {
6743 psym->st_name = BYTE_GET (esyms[j].st_name);
6744 psym->st_info = BYTE_GET (esyms[j].st_info);
6745 psym->st_other = BYTE_GET (esyms[j].st_other);
6746 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
6747
6748 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
6749 psym->st_shndx
6750 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
6751 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
6752 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
6753
6754 psym->st_value = BYTE_GET (esyms[j].st_value);
6755 psym->st_size = BYTE_GET (esyms[j].st_size);
6756 }
6757
6758 exit_point:
6759 free (shndx);
6760 free (esyms);
6761
6762 if (num_syms_return != NULL)
6763 * num_syms_return = isyms == NULL ? 0 : number;
6764
6765 return isyms;
6766 }
6767
6768 static Elf_Internal_Sym *
6769 get_elf_symbols (Filedata *filedata,
6770 Elf_Internal_Shdr *section,
6771 unsigned long *num_syms_return)
6772 {
6773 if (is_32bit_elf)
6774 return get_32bit_elf_symbols (filedata, section, num_syms_return);
6775 else
6776 return get_64bit_elf_symbols (filedata, section, num_syms_return);
6777 }
6778
6779 static const char *
6780 get_elf_section_flags (Filedata * filedata, uint64_t sh_flags)
6781 {
6782 static char buff[1024];
6783 char * p = buff;
6784 unsigned int field_size = is_32bit_elf ? 8 : 16;
6785 signed int sindex;
6786 unsigned int size = sizeof (buff) - (field_size + 4 + 1);
6787 uint64_t os_flags = 0;
6788 uint64_t proc_flags = 0;
6789 uint64_t unknown_flags = 0;
6790 static const struct
6791 {
6792 const char * str;
6793 unsigned int len;
6794 }
6795 flags [] =
6796 {
6797 /* 0 */ { STRING_COMMA_LEN ("WRITE") },
6798 /* 1 */ { STRING_COMMA_LEN ("ALLOC") },
6799 /* 2 */ { STRING_COMMA_LEN ("EXEC") },
6800 /* 3 */ { STRING_COMMA_LEN ("MERGE") },
6801 /* 4 */ { STRING_COMMA_LEN ("STRINGS") },
6802 /* 5 */ { STRING_COMMA_LEN ("INFO LINK") },
6803 /* 6 */ { STRING_COMMA_LEN ("LINK ORDER") },
6804 /* 7 */ { STRING_COMMA_LEN ("OS NONCONF") },
6805 /* 8 */ { STRING_COMMA_LEN ("GROUP") },
6806 /* 9 */ { STRING_COMMA_LEN ("TLS") },
6807 /* IA-64 specific. */
6808 /* 10 */ { STRING_COMMA_LEN ("SHORT") },
6809 /* 11 */ { STRING_COMMA_LEN ("NORECOV") },
6810 /* IA-64 OpenVMS specific. */
6811 /* 12 */ { STRING_COMMA_LEN ("VMS_GLOBAL") },
6812 /* 13 */ { STRING_COMMA_LEN ("VMS_OVERLAID") },
6813 /* 14 */ { STRING_COMMA_LEN ("VMS_SHARED") },
6814 /* 15 */ { STRING_COMMA_LEN ("VMS_VECTOR") },
6815 /* 16 */ { STRING_COMMA_LEN ("VMS_ALLOC_64BIT") },
6816 /* 17 */ { STRING_COMMA_LEN ("VMS_PROTECTED") },
6817 /* Generic. */
6818 /* 18 */ { STRING_COMMA_LEN ("EXCLUDE") },
6819 /* SPARC specific. */
6820 /* 19 */ { STRING_COMMA_LEN ("ORDERED") },
6821 /* 20 */ { STRING_COMMA_LEN ("COMPRESSED") },
6822 /* ARM specific. */
6823 /* 21 */ { STRING_COMMA_LEN ("ENTRYSECT") },
6824 /* 22 */ { STRING_COMMA_LEN ("ARM_PURECODE") },
6825 /* 23 */ { STRING_COMMA_LEN ("COMDEF") },
6826 /* GNU specific. */
6827 /* 24 */ { STRING_COMMA_LEN ("GNU_MBIND") },
6828 /* VLE specific. */
6829 /* 25 */ { STRING_COMMA_LEN ("VLE") },
6830 /* GNU specific. */
6831 /* 26 */ { STRING_COMMA_LEN ("GNU_RETAIN") },
6832 };
6833
6834 if (do_section_details)
6835 {
6836 sprintf (buff, "[%*.*lx]: ",
6837 field_size, field_size, (unsigned long) sh_flags);
6838 p += field_size + 4;
6839 }
6840
6841 while (sh_flags)
6842 {
6843 uint64_t flag;
6844
6845 flag = sh_flags & - sh_flags;
6846 sh_flags &= ~ flag;
6847
6848 if (do_section_details)
6849 {
6850 switch (flag)
6851 {
6852 case SHF_WRITE: sindex = 0; break;
6853 case SHF_ALLOC: sindex = 1; break;
6854 case SHF_EXECINSTR: sindex = 2; break;
6855 case SHF_MERGE: sindex = 3; break;
6856 case SHF_STRINGS: sindex = 4; break;
6857 case SHF_INFO_LINK: sindex = 5; break;
6858 case SHF_LINK_ORDER: sindex = 6; break;
6859 case SHF_OS_NONCONFORMING: sindex = 7; break;
6860 case SHF_GROUP: sindex = 8; break;
6861 case SHF_TLS: sindex = 9; break;
6862 case SHF_EXCLUDE: sindex = 18; break;
6863 case SHF_COMPRESSED: sindex = 20; break;
6864
6865 default:
6866 sindex = -1;
6867 switch (filedata->file_header.e_machine)
6868 {
6869 case EM_IA_64:
6870 if (flag == SHF_IA_64_SHORT)
6871 sindex = 10;
6872 else if (flag == SHF_IA_64_NORECOV)
6873 sindex = 11;
6874 else if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
6875 switch (flag)
6876 {
6877 case SHF_IA_64_VMS_GLOBAL: sindex = 12; break;
6878 case SHF_IA_64_VMS_OVERLAID: sindex = 13; break;
6879 case SHF_IA_64_VMS_SHARED: sindex = 14; break;
6880 case SHF_IA_64_VMS_VECTOR: sindex = 15; break;
6881 case SHF_IA_64_VMS_ALLOC_64BIT: sindex = 16; break;
6882 case SHF_IA_64_VMS_PROTECTED: sindex = 17; break;
6883 default: break;
6884 }
6885 break;
6886
6887 case EM_386:
6888 case EM_IAMCU:
6889 case EM_X86_64:
6890 case EM_L1OM:
6891 case EM_K1OM:
6892 case EM_OLD_SPARCV9:
6893 case EM_SPARC32PLUS:
6894 case EM_SPARCV9:
6895 case EM_SPARC:
6896 if (flag == SHF_ORDERED)
6897 sindex = 19;
6898 break;
6899
6900 case EM_ARM:
6901 switch (flag)
6902 {
6903 case SHF_ENTRYSECT: sindex = 21; break;
6904 case SHF_ARM_PURECODE: sindex = 22; break;
6905 case SHF_COMDEF: sindex = 23; break;
6906 default: break;
6907 }
6908 break;
6909 case EM_PPC:
6910 if (flag == SHF_PPC_VLE)
6911 sindex = 25;
6912 break;
6913 default:
6914 break;
6915 }
6916
6917 switch (filedata->file_header.e_ident[EI_OSABI])
6918 {
6919 case ELFOSABI_GNU:
6920 case ELFOSABI_FREEBSD:
6921 if (flag == SHF_GNU_RETAIN)
6922 sindex = 26;
6923 /* Fall through */
6924 case ELFOSABI_NONE:
6925 if (flag == SHF_GNU_MBIND)
6926 /* We should not recognize SHF_GNU_MBIND for
6927 ELFOSABI_NONE, but binutils as of 2019-07-23 did
6928 not set the EI_OSABI header byte. */
6929 sindex = 24;
6930 break;
6931 default:
6932 break;
6933 }
6934 break;
6935 }
6936
6937 if (sindex != -1)
6938 {
6939 if (p != buff + field_size + 4)
6940 {
6941 if (size < (10 + 2))
6942 {
6943 warn (_("Internal error: not enough buffer room for section flag info"));
6944 return _("<unknown>");
6945 }
6946 size -= 2;
6947 *p++ = ',';
6948 *p++ = ' ';
6949 }
6950
6951 size -= flags [sindex].len;
6952 p = stpcpy (p, flags [sindex].str);
6953 }
6954 else if (flag & SHF_MASKOS)
6955 os_flags |= flag;
6956 else if (flag & SHF_MASKPROC)
6957 proc_flags |= flag;
6958 else
6959 unknown_flags |= flag;
6960 }
6961 else
6962 {
6963 switch (flag)
6964 {
6965 case SHF_WRITE: *p = 'W'; break;
6966 case SHF_ALLOC: *p = 'A'; break;
6967 case SHF_EXECINSTR: *p = 'X'; break;
6968 case SHF_MERGE: *p = 'M'; break;
6969 case SHF_STRINGS: *p = 'S'; break;
6970 case SHF_INFO_LINK: *p = 'I'; break;
6971 case SHF_LINK_ORDER: *p = 'L'; break;
6972 case SHF_OS_NONCONFORMING: *p = 'O'; break;
6973 case SHF_GROUP: *p = 'G'; break;
6974 case SHF_TLS: *p = 'T'; break;
6975 case SHF_EXCLUDE: *p = 'E'; break;
6976 case SHF_COMPRESSED: *p = 'C'; break;
6977
6978 default:
6979 if ((filedata->file_header.e_machine == EM_X86_64
6980 || filedata->file_header.e_machine == EM_L1OM
6981 || filedata->file_header.e_machine == EM_K1OM)
6982 && flag == SHF_X86_64_LARGE)
6983 *p = 'l';
6984 else if (filedata->file_header.e_machine == EM_ARM
6985 && flag == SHF_ARM_PURECODE)
6986 *p = 'y';
6987 else if (filedata->file_header.e_machine == EM_PPC
6988 && flag == SHF_PPC_VLE)
6989 *p = 'v';
6990 else if (flag & SHF_MASKOS)
6991 {
6992 switch (filedata->file_header.e_ident[EI_OSABI])
6993 {
6994 case ELFOSABI_GNU:
6995 case ELFOSABI_FREEBSD:
6996 if (flag == SHF_GNU_RETAIN)
6997 {
6998 *p = 'R';
6999 break;
7000 }
7001 /* Fall through */
7002 case ELFOSABI_NONE:
7003 if (flag == SHF_GNU_MBIND)
7004 {
7005 /* We should not recognize SHF_GNU_MBIND for
7006 ELFOSABI_NONE, but binutils as of 2019-07-23 did
7007 not set the EI_OSABI header byte. */
7008 *p = 'D';
7009 break;
7010 }
7011 /* Fall through */
7012 default:
7013 *p = 'o';
7014 sh_flags &= ~SHF_MASKOS;
7015 break;
7016 }
7017 }
7018 else if (flag & SHF_MASKPROC)
7019 {
7020 *p = 'p';
7021 sh_flags &= ~ SHF_MASKPROC;
7022 }
7023 else
7024 *p = 'x';
7025 break;
7026 }
7027 p++;
7028 }
7029 }
7030
7031 if (do_section_details)
7032 {
7033 if (os_flags)
7034 {
7035 size -= 5 + field_size;
7036 if (p != buff + field_size + 4)
7037 {
7038 if (size < (2 + 1))
7039 {
7040 warn (_("Internal error: not enough buffer room for section flag info"));
7041 return _("<unknown>");
7042 }
7043 size -= 2;
7044 *p++ = ',';
7045 *p++ = ' ';
7046 }
7047 sprintf (p, "OS (%*.*lx)", field_size, field_size,
7048 (unsigned long) os_flags);
7049 p += 5 + field_size;
7050 }
7051 if (proc_flags)
7052 {
7053 size -= 7 + field_size;
7054 if (p != buff + field_size + 4)
7055 {
7056 if (size < (2 + 1))
7057 {
7058 warn (_("Internal error: not enough buffer room for section flag info"));
7059 return _("<unknown>");
7060 }
7061 size -= 2;
7062 *p++ = ',';
7063 *p++ = ' ';
7064 }
7065 sprintf (p, "PROC (%*.*lx)", field_size, field_size,
7066 (unsigned long) proc_flags);
7067 p += 7 + field_size;
7068 }
7069 if (unknown_flags)
7070 {
7071 size -= 10 + field_size;
7072 if (p != buff + field_size + 4)
7073 {
7074 if (size < (2 + 1))
7075 {
7076 warn (_("Internal error: not enough buffer room for section flag info"));
7077 return _("<unknown>");
7078 }
7079 size -= 2;
7080 *p++ = ',';
7081 *p++ = ' ';
7082 }
7083 sprintf (p, _("UNKNOWN (%*.*lx)"), field_size, field_size,
7084 (unsigned long) unknown_flags);
7085 p += 10 + field_size;
7086 }
7087 }
7088
7089 *p = '\0';
7090 return buff;
7091 }
7092
7093 static unsigned int ATTRIBUTE_WARN_UNUSED_RESULT
7094 get_compression_header (Elf_Internal_Chdr *chdr, unsigned char *buf,
7095 uint64_t size)
7096 {
7097 if (is_32bit_elf)
7098 {
7099 Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) buf;
7100
7101 if (size < sizeof (* echdr))
7102 {
7103 error (_("Compressed section is too small even for a compression header\n"));
7104 return 0;
7105 }
7106
7107 chdr->ch_type = BYTE_GET (echdr->ch_type);
7108 chdr->ch_size = BYTE_GET (echdr->ch_size);
7109 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
7110 return sizeof (*echdr);
7111 }
7112 else
7113 {
7114 Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) buf;
7115
7116 if (size < sizeof (* echdr))
7117 {
7118 error (_("Compressed section is too small even for a compression header\n"));
7119 return 0;
7120 }
7121
7122 chdr->ch_type = BYTE_GET (echdr->ch_type);
7123 chdr->ch_size = BYTE_GET (echdr->ch_size);
7124 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
7125 return sizeof (*echdr);
7126 }
7127 }
7128
7129 static bool
7130 process_section_headers (Filedata * filedata)
7131 {
7132 Elf_Internal_Shdr * section;
7133 unsigned int i;
7134
7135 if (filedata->file_header.e_shnum == 0)
7136 {
7137 /* PR binutils/12467. */
7138 if (filedata->file_header.e_shoff != 0)
7139 {
7140 warn (_("possibly corrupt ELF file header - it has a non-zero"
7141 " section header offset, but no section headers\n"));
7142 return false;
7143 }
7144 else if (do_sections)
7145 printf (_("\nThere are no sections in this file.\n"));
7146
7147 return true;
7148 }
7149
7150 if (do_sections && !do_header)
7151 {
7152 if (filedata->is_separate && process_links)
7153 printf (_("In linked file '%s': "), filedata->file_name);
7154 if (! filedata->is_separate || process_links)
7155 printf (ngettext ("There is %d section header, "
7156 "starting at offset 0x%lx:\n",
7157 "There are %d section headers, "
7158 "starting at offset 0x%lx:\n",
7159 filedata->file_header.e_shnum),
7160 filedata->file_header.e_shnum,
7161 (unsigned long) filedata->file_header.e_shoff);
7162 }
7163
7164 if (!get_section_headers (filedata, false))
7165 return false;
7166
7167 /* Read in the string table, so that we have names to display. */
7168 if (filedata->file_header.e_shstrndx != SHN_UNDEF
7169 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
7170 {
7171 section = filedata->section_headers + filedata->file_header.e_shstrndx;
7172
7173 if (section->sh_size != 0)
7174 {
7175 filedata->string_table = (char *) get_data (NULL, filedata, section->sh_offset,
7176 1, section->sh_size,
7177 _("string table"));
7178
7179 filedata->string_table_length = filedata->string_table != NULL ? section->sh_size : 0;
7180 }
7181 }
7182
7183 /* Scan the sections for the dynamic symbol table
7184 and dynamic string table and debug sections. */
7185 eh_addr_size = is_32bit_elf ? 4 : 8;
7186 switch (filedata->file_header.e_machine)
7187 {
7188 case EM_MIPS:
7189 case EM_MIPS_RS3_LE:
7190 /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
7191 FDE addresses. However, the ABI also has a semi-official ILP32
7192 variant for which the normal FDE address size rules apply.
7193
7194 GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
7195 section, where XX is the size of longs in bits. Unfortunately,
7196 earlier compilers provided no way of distinguishing ILP32 objects
7197 from LP64 objects, so if there's any doubt, we should assume that
7198 the official LP64 form is being used. */
7199 if ((filedata->file_header.e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64
7200 && find_section (filedata, ".gcc_compiled_long32") == NULL)
7201 eh_addr_size = 8;
7202 break;
7203
7204 case EM_H8_300:
7205 case EM_H8_300H:
7206 switch (filedata->file_header.e_flags & EF_H8_MACH)
7207 {
7208 case E_H8_MACH_H8300:
7209 case E_H8_MACH_H8300HN:
7210 case E_H8_MACH_H8300SN:
7211 case E_H8_MACH_H8300SXN:
7212 eh_addr_size = 2;
7213 break;
7214 case E_H8_MACH_H8300H:
7215 case E_H8_MACH_H8300S:
7216 case E_H8_MACH_H8300SX:
7217 eh_addr_size = 4;
7218 break;
7219 }
7220 break;
7221
7222 case EM_M32C_OLD:
7223 case EM_M32C:
7224 switch (filedata->file_header.e_flags & EF_M32C_CPU_MASK)
7225 {
7226 case EF_M32C_CPU_M16C:
7227 eh_addr_size = 2;
7228 break;
7229 }
7230 break;
7231 }
7232
7233 #define CHECK_ENTSIZE_VALUES(section, i, size32, size64) \
7234 do \
7235 { \
7236 uint64_t expected_entsize = is_32bit_elf ? size32 : size64; \
7237 if (section->sh_entsize != expected_entsize) \
7238 { \
7239 error (_("Section %d has invalid sh_entsize of %" PRIx64 "\n"), \
7240 i, section->sh_entsize); \
7241 error (_("(Using the expected size of %" PRIx64 " for the rest of this dump)\n"), \
7242 expected_entsize); \
7243 section->sh_entsize = expected_entsize; \
7244 } \
7245 } \
7246 while (0)
7247
7248 #define CHECK_ENTSIZE(section, i, type) \
7249 CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type), \
7250 sizeof (Elf64_External_##type))
7251
7252 for (i = 0, section = filedata->section_headers;
7253 i < filedata->file_header.e_shnum;
7254 i++, section++)
7255 {
7256 const char *name = section_name_print (filedata, section);
7257
7258 /* Run some sanity checks on the headers and
7259 possibly fill in some file data as well. */
7260 switch (section->sh_type)
7261 {
7262 case SHT_DYNSYM:
7263 if (filedata->dynamic_symbols != NULL)
7264 {
7265 error (_("File contains multiple dynamic symbol tables\n"));
7266 continue;
7267 }
7268
7269 CHECK_ENTSIZE (section, i, Sym);
7270 filedata->dynamic_symbols
7271 = get_elf_symbols (filedata, section, &filedata->num_dynamic_syms);
7272 filedata->dynamic_symtab_section = section;
7273 break;
7274
7275 case SHT_STRTAB:
7276 if (streq (name, ".dynstr"))
7277 {
7278 if (filedata->dynamic_strings != NULL)
7279 {
7280 error (_("File contains multiple dynamic string tables\n"));
7281 continue;
7282 }
7283
7284 filedata->dynamic_strings
7285 = (char *) get_data (NULL, filedata, section->sh_offset,
7286 1, section->sh_size, _("dynamic strings"));
7287 filedata->dynamic_strings_length
7288 = filedata->dynamic_strings == NULL ? 0 : section->sh_size;
7289 filedata->dynamic_strtab_section = section;
7290 }
7291 break;
7292
7293 case SHT_SYMTAB_SHNDX:
7294 {
7295 elf_section_list * entry = xmalloc (sizeof * entry);
7296
7297 entry->hdr = section;
7298 entry->next = filedata->symtab_shndx_list;
7299 filedata->symtab_shndx_list = entry;
7300 }
7301 break;
7302
7303 case SHT_SYMTAB:
7304 CHECK_ENTSIZE (section, i, Sym);
7305 break;
7306
7307 case SHT_GROUP:
7308 CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
7309 break;
7310
7311 case SHT_REL:
7312 CHECK_ENTSIZE (section, i, Rel);
7313 if (do_checks && section->sh_size == 0)
7314 warn (_("Section '%s': zero-sized relocation section\n"), name);
7315 break;
7316
7317 case SHT_RELA:
7318 CHECK_ENTSIZE (section, i, Rela);
7319 if (do_checks && section->sh_size == 0)
7320 warn (_("Section '%s': zero-sized relocation section\n"), name);
7321 break;
7322
7323 case SHT_RELR:
7324 CHECK_ENTSIZE (section, i, Relr);
7325 break;
7326
7327 case SHT_NOTE:
7328 case SHT_PROGBITS:
7329 /* Having a zero sized section is not illegal according to the
7330 ELF standard, but it might be an indication that something
7331 is wrong. So issue a warning if we are running in lint mode. */
7332 if (do_checks && section->sh_size == 0)
7333 warn (_("Section '%s': has a size of zero - is this intended ?\n"), name);
7334 break;
7335
7336 default:
7337 break;
7338 }
7339
7340 if ((do_debugging || do_debug_info || do_debug_abbrevs
7341 || do_debug_lines || do_debug_pubnames || do_debug_pubtypes
7342 || do_debug_aranges || do_debug_frames || do_debug_macinfo
7343 || do_debug_str || do_debug_str_offsets || do_debug_loc
7344 || do_debug_ranges
7345 || do_debug_addr || do_debug_cu_index || do_debug_links)
7346 && (startswith (name, ".debug_")
7347 || startswith (name, ".zdebug_")))
7348 {
7349 if (name[1] == 'z')
7350 name += sizeof (".zdebug_") - 1;
7351 else
7352 name += sizeof (".debug_") - 1;
7353
7354 if (do_debugging
7355 || (do_debug_info && startswith (name, "info"))
7356 || (do_debug_info && startswith (name, "types"))
7357 || (do_debug_abbrevs && startswith (name, "abbrev"))
7358 || (do_debug_lines && strcmp (name, "line") == 0)
7359 || (do_debug_lines && startswith (name, "line."))
7360 || (do_debug_pubnames && startswith (name, "pubnames"))
7361 || (do_debug_pubtypes && startswith (name, "pubtypes"))
7362 || (do_debug_pubnames && startswith (name, "gnu_pubnames"))
7363 || (do_debug_pubtypes && startswith (name, "gnu_pubtypes"))
7364 || (do_debug_aranges && startswith (name, "aranges"))
7365 || (do_debug_ranges && startswith (name, "ranges"))
7366 || (do_debug_ranges && startswith (name, "rnglists"))
7367 || (do_debug_frames && startswith (name, "frame"))
7368 || (do_debug_macinfo && startswith (name, "macinfo"))
7369 || (do_debug_macinfo && startswith (name, "macro"))
7370 || (do_debug_str && startswith (name, "str"))
7371 || (do_debug_links && startswith (name, "sup"))
7372 || (do_debug_str_offsets && startswith (name, "str_offsets"))
7373 || (do_debug_loc && startswith (name, "loc"))
7374 || (do_debug_loc && startswith (name, "loclists"))
7375 || (do_debug_addr && startswith (name, "addr"))
7376 || (do_debug_cu_index && startswith (name, "cu_index"))
7377 || (do_debug_cu_index && startswith (name, "tu_index"))
7378 )
7379 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7380 }
7381 /* Linkonce section to be combined with .debug_info at link time. */
7382 else if ((do_debugging || do_debug_info)
7383 && startswith (name, ".gnu.linkonce.wi."))
7384 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7385 else if (do_debug_frames && streq (name, ".eh_frame"))
7386 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7387 else if (do_gdb_index && (streq (name, ".gdb_index")
7388 || streq (name, ".debug_names")))
7389 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7390 /* Trace sections for Itanium VMS. */
7391 else if ((do_debugging || do_trace_info || do_trace_abbrevs
7392 || do_trace_aranges)
7393 && startswith (name, ".trace_"))
7394 {
7395 name += sizeof (".trace_") - 1;
7396
7397 if (do_debugging
7398 || (do_trace_info && streq (name, "info"))
7399 || (do_trace_abbrevs && streq (name, "abbrev"))
7400 || (do_trace_aranges && streq (name, "aranges"))
7401 )
7402 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7403 }
7404 else if ((do_debugging || do_debug_links)
7405 && (startswith (name, ".gnu_debuglink")
7406 || startswith (name, ".gnu_debugaltlink")))
7407 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7408 }
7409
7410 if (! do_sections)
7411 return true;
7412
7413 if (filedata->is_separate && ! process_links)
7414 return true;
7415
7416 if (filedata->is_separate)
7417 printf (_("\nSection Headers in linked file '%s':\n"), filedata->file_name);
7418 else if (filedata->file_header.e_shnum > 1)
7419 printf (_("\nSection Headers:\n"));
7420 else
7421 printf (_("\nSection Header:\n"));
7422
7423 if (is_32bit_elf)
7424 {
7425 if (do_section_details)
7426 {
7427 printf (_(" [Nr] Name\n"));
7428 printf (_(" Type Addr Off Size ES Lk Inf Al\n"));
7429 }
7430 else
7431 printf
7432 (_(" [Nr] Name Type Addr Off Size ES Flg Lk Inf Al\n"));
7433 }
7434 else if (do_wide)
7435 {
7436 if (do_section_details)
7437 {
7438 printf (_(" [Nr] Name\n"));
7439 printf (_(" Type Address Off Size ES Lk Inf Al\n"));
7440 }
7441 else
7442 printf
7443 (_(" [Nr] Name Type Address Off Size ES Flg Lk Inf Al\n"));
7444 }
7445 else
7446 {
7447 if (do_section_details)
7448 {
7449 printf (_(" [Nr] Name\n"));
7450 printf (_(" Type Address Offset Link\n"));
7451 printf (_(" Size EntSize Info Align\n"));
7452 }
7453 else
7454 {
7455 printf (_(" [Nr] Name Type Address Offset\n"));
7456 printf (_(" Size EntSize Flags Link Info Align\n"));
7457 }
7458 }
7459
7460 if (do_section_details)
7461 printf (_(" Flags\n"));
7462
7463 for (i = 0, section = filedata->section_headers;
7464 i < filedata->file_header.e_shnum;
7465 i++, section++)
7466 {
7467 /* Run some sanity checks on the section header. */
7468
7469 /* Check the sh_link field. */
7470 switch (section->sh_type)
7471 {
7472 case SHT_REL:
7473 case SHT_RELA:
7474 if (section->sh_link == 0
7475 && (filedata->file_header.e_type == ET_EXEC
7476 || filedata->file_header.e_type == ET_DYN))
7477 /* A dynamic relocation section where all entries use a
7478 zero symbol index need not specify a symtab section. */
7479 break;
7480 /* Fall through. */
7481 case SHT_SYMTAB_SHNDX:
7482 case SHT_GROUP:
7483 case SHT_HASH:
7484 case SHT_GNU_HASH:
7485 case SHT_GNU_versym:
7486 if (section->sh_link == 0
7487 || section->sh_link >= filedata->file_header.e_shnum
7488 || (filedata->section_headers[section->sh_link].sh_type != SHT_SYMTAB
7489 && filedata->section_headers[section->sh_link].sh_type != SHT_DYNSYM))
7490 warn (_("[%2u]: Link field (%u) should index a symtab section.\n"),
7491 i, section->sh_link);
7492 break;
7493
7494 case SHT_DYNAMIC:
7495 case SHT_SYMTAB:
7496 case SHT_DYNSYM:
7497 case SHT_GNU_verneed:
7498 case SHT_GNU_verdef:
7499 case SHT_GNU_LIBLIST:
7500 if (section->sh_link == 0
7501 || section->sh_link >= filedata->file_header.e_shnum
7502 || filedata->section_headers[section->sh_link].sh_type != SHT_STRTAB)
7503 warn (_("[%2u]: Link field (%u) should index a string section.\n"),
7504 i, section->sh_link);
7505 break;
7506
7507 case SHT_INIT_ARRAY:
7508 case SHT_FINI_ARRAY:
7509 case SHT_PREINIT_ARRAY:
7510 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
7511 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
7512 i, section->sh_link);
7513 break;
7514
7515 default:
7516 /* FIXME: Add support for target specific section types. */
7517 #if 0 /* Currently we do not check other section types as there are too
7518 many special cases. Stab sections for example have a type
7519 of SHT_PROGBITS but an sh_link field that links to the .stabstr
7520 section. */
7521 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
7522 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
7523 i, section->sh_link);
7524 #endif
7525 break;
7526 }
7527
7528 /* Check the sh_info field. */
7529 switch (section->sh_type)
7530 {
7531 case SHT_REL:
7532 case SHT_RELA:
7533 if (section->sh_info == 0
7534 && (filedata->file_header.e_type == ET_EXEC
7535 || filedata->file_header.e_type == ET_DYN))
7536 /* Dynamic relocations apply to segments, so they do not
7537 need to specify the section they relocate. */
7538 break;
7539 if (section->sh_info == 0
7540 || section->sh_info >= filedata->file_header.e_shnum
7541 || (filedata->section_headers[section->sh_info].sh_type != SHT_PROGBITS
7542 && filedata->section_headers[section->sh_info].sh_type != SHT_NOBITS
7543 && filedata->section_headers[section->sh_info].sh_type != SHT_NOTE
7544 && filedata->section_headers[section->sh_info].sh_type != SHT_INIT_ARRAY
7545 && filedata->section_headers[section->sh_info].sh_type != SHT_FINI_ARRAY
7546 && filedata->section_headers[section->sh_info].sh_type != SHT_PREINIT_ARRAY
7547 /* FIXME: Are other section types valid ? */
7548 && filedata->section_headers[section->sh_info].sh_type < SHT_LOOS))
7549 warn (_("[%2u]: Info field (%u) should index a relocatable section.\n"),
7550 i, section->sh_info);
7551 break;
7552
7553 case SHT_DYNAMIC:
7554 case SHT_HASH:
7555 case SHT_SYMTAB_SHNDX:
7556 case SHT_INIT_ARRAY:
7557 case SHT_FINI_ARRAY:
7558 case SHT_PREINIT_ARRAY:
7559 if (section->sh_info != 0)
7560 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
7561 i, section->sh_info);
7562 break;
7563
7564 case SHT_GROUP:
7565 case SHT_SYMTAB:
7566 case SHT_DYNSYM:
7567 /* A symbol index - we assume that it is valid. */
7568 break;
7569
7570 default:
7571 /* FIXME: Add support for target specific section types. */
7572 if (section->sh_type == SHT_NOBITS)
7573 /* NOBITS section headers with non-zero sh_info fields can be
7574 created when a binary is stripped of everything but its debug
7575 information. The stripped sections have their headers
7576 preserved but their types set to SHT_NOBITS. So do not check
7577 this type of section. */
7578 ;
7579 else if (section->sh_flags & SHF_INFO_LINK)
7580 {
7581 if (section->sh_info < 1 || section->sh_info >= filedata->file_header.e_shnum)
7582 warn (_("[%2u]: Expected link to another section in info field"), i);
7583 }
7584 else if (section->sh_type < SHT_LOOS
7585 && (section->sh_flags & SHF_GNU_MBIND) == 0
7586 && section->sh_info != 0)
7587 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
7588 i, section->sh_info);
7589 break;
7590 }
7591
7592 /* Check the sh_size field. */
7593 if (section->sh_size > filedata->file_size
7594 && section->sh_type != SHT_NOBITS
7595 && section->sh_type != SHT_NULL
7596 && section->sh_type < SHT_LOOS)
7597 warn (_("Size of section %u is larger than the entire file!\n"), i);
7598
7599 printf (" [%2u] ", i);
7600 if (do_section_details)
7601 printf ("%s\n ", printable_section_name (filedata, section));
7602 else
7603 print_symbol (-17, section_name_print (filedata, section));
7604
7605 printf (do_wide ? " %-15s " : " %-15.15s ",
7606 get_section_type_name (filedata, section->sh_type));
7607
7608 if (is_32bit_elf)
7609 {
7610 const char * link_too_big = NULL;
7611
7612 print_vma (section->sh_addr, LONG_HEX);
7613
7614 printf ( " %6.6lx %6.6lx %2.2lx",
7615 (unsigned long) section->sh_offset,
7616 (unsigned long) section->sh_size,
7617 (unsigned long) section->sh_entsize);
7618
7619 if (do_section_details)
7620 fputs (" ", stdout);
7621 else
7622 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7623
7624 if (section->sh_link >= filedata->file_header.e_shnum)
7625 {
7626 link_too_big = "";
7627 /* The sh_link value is out of range. Normally this indicates
7628 an error but it can have special values in Solaris binaries. */
7629 switch (filedata->file_header.e_machine)
7630 {
7631 case EM_386:
7632 case EM_IAMCU:
7633 case EM_X86_64:
7634 case EM_L1OM:
7635 case EM_K1OM:
7636 case EM_OLD_SPARCV9:
7637 case EM_SPARC32PLUS:
7638 case EM_SPARCV9:
7639 case EM_SPARC:
7640 if (section->sh_link == (SHN_BEFORE & 0xffff))
7641 link_too_big = "BEFORE";
7642 else if (section->sh_link == (SHN_AFTER & 0xffff))
7643 link_too_big = "AFTER";
7644 break;
7645 default:
7646 break;
7647 }
7648 }
7649
7650 if (do_section_details)
7651 {
7652 if (link_too_big != NULL && * link_too_big)
7653 printf ("<%s> ", link_too_big);
7654 else
7655 printf ("%2u ", section->sh_link);
7656 printf ("%3u %2lu\n", section->sh_info,
7657 (unsigned long) section->sh_addralign);
7658 }
7659 else
7660 printf ("%2u %3u %2lu\n",
7661 section->sh_link,
7662 section->sh_info,
7663 (unsigned long) section->sh_addralign);
7664
7665 if (link_too_big && ! * link_too_big)
7666 warn (_("section %u: sh_link value of %u is larger than the number of sections\n"),
7667 i, section->sh_link);
7668 }
7669 else if (do_wide)
7670 {
7671 print_vma (section->sh_addr, LONG_HEX);
7672
7673 if ((long) section->sh_offset == section->sh_offset)
7674 printf (" %6.6lx", (unsigned long) section->sh_offset);
7675 else
7676 {
7677 putchar (' ');
7678 print_vma (section->sh_offset, LONG_HEX);
7679 }
7680
7681 if ((unsigned long) section->sh_size == section->sh_size)
7682 printf (" %6.6lx", (unsigned long) section->sh_size);
7683 else
7684 {
7685 putchar (' ');
7686 print_vma (section->sh_size, LONG_HEX);
7687 }
7688
7689 if ((unsigned long) section->sh_entsize == section->sh_entsize)
7690 printf (" %2.2lx", (unsigned long) section->sh_entsize);
7691 else
7692 {
7693 putchar (' ');
7694 print_vma (section->sh_entsize, LONG_HEX);
7695 }
7696
7697 if (do_section_details)
7698 fputs (" ", stdout);
7699 else
7700 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7701
7702 printf ("%2u %3u ", section->sh_link, section->sh_info);
7703
7704 if ((unsigned long) section->sh_addralign == section->sh_addralign)
7705 printf ("%2lu\n", (unsigned long) section->sh_addralign);
7706 else
7707 {
7708 print_vma (section->sh_addralign, DEC);
7709 putchar ('\n');
7710 }
7711 }
7712 else if (do_section_details)
7713 {
7714 putchar (' ');
7715 print_vma (section->sh_addr, LONG_HEX);
7716 if ((long) section->sh_offset == section->sh_offset)
7717 printf (" %16.16lx", (unsigned long) section->sh_offset);
7718 else
7719 {
7720 printf (" ");
7721 print_vma (section->sh_offset, LONG_HEX);
7722 }
7723 printf (" %u\n ", section->sh_link);
7724 print_vma (section->sh_size, LONG_HEX);
7725 putchar (' ');
7726 print_vma (section->sh_entsize, LONG_HEX);
7727
7728 printf (" %-16u %lu\n",
7729 section->sh_info,
7730 (unsigned long) section->sh_addralign);
7731 }
7732 else
7733 {
7734 putchar (' ');
7735 print_vma (section->sh_addr, LONG_HEX);
7736 if ((long) section->sh_offset == section->sh_offset)
7737 printf (" %8.8lx", (unsigned long) section->sh_offset);
7738 else
7739 {
7740 printf (" ");
7741 print_vma (section->sh_offset, LONG_HEX);
7742 }
7743 printf ("\n ");
7744 print_vma (section->sh_size, LONG_HEX);
7745 printf (" ");
7746 print_vma (section->sh_entsize, LONG_HEX);
7747
7748 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7749
7750 printf (" %2u %3u %lu\n",
7751 section->sh_link,
7752 section->sh_info,
7753 (unsigned long) section->sh_addralign);
7754 }
7755
7756 if (do_section_details)
7757 {
7758 printf (" %s\n", get_elf_section_flags (filedata, section->sh_flags));
7759 if ((section->sh_flags & SHF_COMPRESSED) != 0)
7760 {
7761 /* Minimum section size is 12 bytes for 32-bit compression
7762 header + 12 bytes for compressed data header. */
7763 unsigned char buf[24];
7764
7765 assert (sizeof (buf) >= sizeof (Elf64_External_Chdr));
7766 if (get_data (&buf, filedata, section->sh_offset, 1,
7767 sizeof (buf), _("compression header")))
7768 {
7769 Elf_Internal_Chdr chdr;
7770
7771 if (get_compression_header (&chdr, buf, sizeof (buf)) == 0)
7772 printf (_(" [<corrupt>]\n"));
7773 else
7774 {
7775 if (chdr.ch_type == ELFCOMPRESS_ZLIB)
7776 printf (" ZLIB, ");
7777 else if (chdr.ch_type == ELFCOMPRESS_ZSTD)
7778 printf (" ZSTD, ");
7779 else
7780 printf (_(" [<unknown>: 0x%x], "),
7781 chdr.ch_type);
7782 print_vma (chdr.ch_size, LONG_HEX);
7783 printf (", %lu\n", (unsigned long) chdr.ch_addralign);
7784 }
7785 }
7786 }
7787 }
7788 }
7789
7790 if (!do_section_details)
7791 {
7792 /* The ordering of the letters shown here matches the ordering of the
7793 corresponding SHF_xxx values, and hence the order in which these
7794 letters will be displayed to the user. */
7795 printf (_("Key to Flags:\n\
7796 W (write), A (alloc), X (execute), M (merge), S (strings), I (info),\n\
7797 L (link order), O (extra OS processing required), G (group), T (TLS),\n\
7798 C (compressed), x (unknown), o (OS specific), E (exclude),\n "));
7799 switch (filedata->file_header.e_ident[EI_OSABI])
7800 {
7801 case ELFOSABI_GNU:
7802 case ELFOSABI_FREEBSD:
7803 printf (_("R (retain), "));
7804 /* Fall through */
7805 case ELFOSABI_NONE:
7806 printf (_("D (mbind), "));
7807 break;
7808 default:
7809 break;
7810 }
7811 if (filedata->file_header.e_machine == EM_X86_64
7812 || filedata->file_header.e_machine == EM_L1OM
7813 || filedata->file_header.e_machine == EM_K1OM)
7814 printf (_("l (large), "));
7815 else if (filedata->file_header.e_machine == EM_ARM)
7816 printf (_("y (purecode), "));
7817 else if (filedata->file_header.e_machine == EM_PPC)
7818 printf (_("v (VLE), "));
7819 printf ("p (processor specific)\n");
7820 }
7821
7822 return true;
7823 }
7824
7825 static bool
7826 get_symtab (Filedata *filedata, Elf_Internal_Shdr *symsec,
7827 Elf_Internal_Sym **symtab, unsigned long *nsyms,
7828 char **strtab, unsigned long *strtablen)
7829 {
7830 *strtab = NULL;
7831 *strtablen = 0;
7832 *symtab = get_elf_symbols (filedata, symsec, nsyms);
7833
7834 if (*symtab == NULL)
7835 return false;
7836
7837 if (symsec->sh_link != 0)
7838 {
7839 Elf_Internal_Shdr *strsec;
7840
7841 if (symsec->sh_link >= filedata->file_header.e_shnum)
7842 {
7843 error (_("Bad sh_link in symbol table section\n"));
7844 free (*symtab);
7845 *symtab = NULL;
7846 *nsyms = 0;
7847 return false;
7848 }
7849
7850 strsec = filedata->section_headers + symsec->sh_link;
7851
7852 *strtab = (char *) get_data (NULL, filedata, strsec->sh_offset,
7853 1, strsec->sh_size, _("string table"));
7854 if (*strtab == NULL)
7855 {
7856 free (*symtab);
7857 *symtab = NULL;
7858 *nsyms = 0;
7859 return false;
7860 }
7861 *strtablen = strsec->sh_size;
7862 }
7863 return true;
7864 }
7865
7866 static const char *
7867 get_group_flags (unsigned int flags)
7868 {
7869 static char buff[128];
7870
7871 if (flags == 0)
7872 return "";
7873 else if (flags == GRP_COMDAT)
7874 return "COMDAT ";
7875
7876 snprintf (buff, sizeof buff, "[0x%x: %s%s%s]",
7877 flags,
7878 flags & GRP_MASKOS ? _("<OS specific>") : "",
7879 flags & GRP_MASKPROC ? _("<PROC specific>") : "",
7880 (flags & ~(GRP_COMDAT | GRP_MASKOS | GRP_MASKPROC)
7881 ? _("<unknown>") : ""));
7882
7883 return buff;
7884 }
7885
7886 static bool
7887 process_section_groups (Filedata * filedata)
7888 {
7889 Elf_Internal_Shdr * section;
7890 unsigned int i;
7891 struct group * group;
7892 Elf_Internal_Shdr * symtab_sec;
7893 Elf_Internal_Shdr * strtab_sec;
7894 Elf_Internal_Sym * symtab;
7895 unsigned long num_syms;
7896 char * strtab;
7897 size_t strtab_size;
7898
7899 /* Don't process section groups unless needed. */
7900 if (!do_unwind && !do_section_groups)
7901 return true;
7902
7903 if (filedata->file_header.e_shnum == 0)
7904 {
7905 if (do_section_groups)
7906 {
7907 if (filedata->is_separate)
7908 printf (_("\nThere are no sections group in linked file '%s'.\n"),
7909 filedata->file_name);
7910 else
7911 printf (_("\nThere are no section groups in this file.\n"));
7912 }
7913 return true;
7914 }
7915
7916 if (filedata->section_headers == NULL)
7917 {
7918 error (_("Section headers are not available!\n"));
7919 /* PR 13622: This can happen with a corrupt ELF header. */
7920 return false;
7921 }
7922
7923 filedata->section_headers_groups
7924 = (struct group **) calloc (filedata->file_header.e_shnum,
7925 sizeof (struct group *));
7926
7927 if (filedata->section_headers_groups == NULL)
7928 {
7929 error (_("Out of memory reading %u section group headers\n"),
7930 filedata->file_header.e_shnum);
7931 return false;
7932 }
7933
7934 /* Scan the sections for the group section. */
7935 filedata->group_count = 0;
7936 for (i = 0, section = filedata->section_headers;
7937 i < filedata->file_header.e_shnum;
7938 i++, section++)
7939 if (section->sh_type == SHT_GROUP)
7940 filedata->group_count++;
7941
7942 if (filedata->group_count == 0)
7943 {
7944 if (do_section_groups)
7945 {
7946 if (filedata->is_separate)
7947 printf (_("\nThere are no section groups in linked file '%s'.\n"),
7948 filedata->file_name);
7949 else
7950 printf (_("\nThere are no section groups in this file.\n"));
7951 }
7952
7953 return true;
7954 }
7955
7956 filedata->section_groups = (struct group *) calloc (filedata->group_count,
7957 sizeof (struct group));
7958
7959 if (filedata->section_groups == NULL)
7960 {
7961 error (_("Out of memory reading %lu groups\n"),
7962 (unsigned long) filedata->group_count);
7963 return false;
7964 }
7965
7966 symtab_sec = NULL;
7967 strtab_sec = NULL;
7968 symtab = NULL;
7969 num_syms = 0;
7970 strtab = NULL;
7971 strtab_size = 0;
7972
7973 if (filedata->is_separate)
7974 printf (_("Section groups in linked file '%s'\n"), filedata->file_name);
7975
7976 for (i = 0, section = filedata->section_headers, group = filedata->section_groups;
7977 i < filedata->file_header.e_shnum;
7978 i++, section++)
7979 {
7980 if (section->sh_type == SHT_GROUP)
7981 {
7982 const char * name = printable_section_name (filedata, section);
7983 const char * group_name;
7984 unsigned char * start;
7985 unsigned char * indices;
7986 unsigned int entry, j, size;
7987 Elf_Internal_Shdr * sec;
7988 Elf_Internal_Sym * sym;
7989
7990 /* Get the symbol table. */
7991 if (section->sh_link >= filedata->file_header.e_shnum
7992 || ((sec = filedata->section_headers + section->sh_link)->sh_type
7993 != SHT_SYMTAB))
7994 {
7995 error (_("Bad sh_link in group section `%s'\n"), name);
7996 continue;
7997 }
7998
7999 if (symtab_sec != sec)
8000 {
8001 symtab_sec = sec;
8002 free (symtab);
8003 symtab = get_elf_symbols (filedata, symtab_sec, & num_syms);
8004 }
8005
8006 if (symtab == NULL)
8007 {
8008 error (_("Corrupt header in group section `%s'\n"), name);
8009 continue;
8010 }
8011
8012 if (section->sh_info >= num_syms)
8013 {
8014 error (_("Bad sh_info in group section `%s'\n"), name);
8015 continue;
8016 }
8017
8018 sym = symtab + section->sh_info;
8019
8020 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8021 {
8022 if (sym->st_shndx == 0
8023 || sym->st_shndx >= filedata->file_header.e_shnum)
8024 {
8025 error (_("Bad sh_info in group section `%s'\n"), name);
8026 continue;
8027 }
8028
8029 group_name = section_name_print (filedata,
8030 filedata->section_headers
8031 + sym->st_shndx);
8032 strtab_sec = NULL;
8033 free (strtab);
8034 strtab = NULL;
8035 strtab_size = 0;
8036 }
8037 else
8038 {
8039 /* Get the string table. */
8040 if (symtab_sec->sh_link >= filedata->file_header.e_shnum)
8041 {
8042 strtab_sec = NULL;
8043 free (strtab);
8044 strtab = NULL;
8045 strtab_size = 0;
8046 }
8047 else if (strtab_sec
8048 != (sec = filedata->section_headers + symtab_sec->sh_link))
8049 {
8050 strtab_sec = sec;
8051 free (strtab);
8052
8053 strtab = (char *) get_data (NULL, filedata, strtab_sec->sh_offset,
8054 1, strtab_sec->sh_size,
8055 _("string table"));
8056 strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
8057 }
8058 group_name = sym->st_name < strtab_size
8059 ? strtab + sym->st_name : _("<corrupt>");
8060 }
8061
8062 /* PR 17531: file: loop. */
8063 if (section->sh_entsize > section->sh_size)
8064 {
8065 error (_("Section %s has sh_entsize (0x%lx) which is larger than its size (0x%lx)\n"),
8066 printable_section_name (filedata, section),
8067 (unsigned long) section->sh_entsize,
8068 (unsigned long) section->sh_size);
8069 continue;
8070 }
8071
8072 start = (unsigned char *) get_data (NULL, filedata, section->sh_offset,
8073 1, section->sh_size,
8074 _("section data"));
8075 if (start == NULL)
8076 continue;
8077
8078 indices = start;
8079 size = (section->sh_size / section->sh_entsize) - 1;
8080 entry = byte_get (indices, 4);
8081 indices += 4;
8082
8083 if (do_section_groups)
8084 {
8085 printf (_("\n%sgroup section [%5u] `%s' [%s] contains %u sections:\n"),
8086 get_group_flags (entry), i, name, group_name, size);
8087
8088 printf (_(" [Index] Name\n"));
8089 }
8090
8091 group->group_index = i;
8092
8093 for (j = 0; j < size; j++)
8094 {
8095 struct group_list * g;
8096
8097 entry = byte_get (indices, 4);
8098 indices += 4;
8099
8100 if (entry >= filedata->file_header.e_shnum)
8101 {
8102 static unsigned num_group_errors = 0;
8103
8104 if (num_group_errors ++ < 10)
8105 {
8106 error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
8107 entry, i, filedata->file_header.e_shnum - 1);
8108 if (num_group_errors == 10)
8109 warn (_("Further error messages about overlarge group section indices suppressed\n"));
8110 }
8111 continue;
8112 }
8113
8114 if (filedata->section_headers_groups [entry] != NULL)
8115 {
8116 if (entry)
8117 {
8118 static unsigned num_errs = 0;
8119
8120 if (num_errs ++ < 10)
8121 {
8122 error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
8123 entry, i,
8124 filedata->section_headers_groups [entry]->group_index);
8125 if (num_errs == 10)
8126 warn (_("Further error messages about already contained group sections suppressed\n"));
8127 }
8128 continue;
8129 }
8130 else
8131 {
8132 /* Intel C/C++ compiler may put section 0 in a
8133 section group. We just warn it the first time
8134 and ignore it afterwards. */
8135 static bool warned = false;
8136 if (!warned)
8137 {
8138 error (_("section 0 in group section [%5u]\n"),
8139 filedata->section_headers_groups [entry]->group_index);
8140 warned = true;
8141 }
8142 }
8143 }
8144
8145 filedata->section_headers_groups [entry] = group;
8146
8147 if (do_section_groups)
8148 {
8149 sec = filedata->section_headers + entry;
8150 printf (" [%5u] %s\n", entry, printable_section_name (filedata, sec));
8151 }
8152
8153 g = (struct group_list *) xmalloc (sizeof (struct group_list));
8154 g->section_index = entry;
8155 g->next = group->root;
8156 group->root = g;
8157 }
8158
8159 free (start);
8160
8161 group++;
8162 }
8163 }
8164
8165 free (symtab);
8166 free (strtab);
8167 return true;
8168 }
8169
8170 /* Data used to display dynamic fixups. */
8171
8172 struct ia64_vms_dynfixup
8173 {
8174 uint64_t needed_ident; /* Library ident number. */
8175 uint64_t needed; /* Index in the dstrtab of the library name. */
8176 uint64_t fixup_needed; /* Index of the library. */
8177 uint64_t fixup_rela_cnt; /* Number of fixups. */
8178 uint64_t fixup_rela_off; /* Fixups offset in the dynamic segment. */
8179 };
8180
8181 /* Data used to display dynamic relocations. */
8182
8183 struct ia64_vms_dynimgrela
8184 {
8185 uint64_t img_rela_cnt; /* Number of relocations. */
8186 uint64_t img_rela_off; /* Reloc offset in the dynamic segment. */
8187 };
8188
8189 /* Display IA-64 OpenVMS dynamic fixups (used to dynamically link a shared
8190 library). */
8191
8192 static bool
8193 dump_ia64_vms_dynamic_fixups (Filedata * filedata,
8194 struct ia64_vms_dynfixup * fixup,
8195 const char * strtab,
8196 unsigned int strtab_sz)
8197 {
8198 Elf64_External_VMS_IMAGE_FIXUP * imfs;
8199 long i;
8200 const char * lib_name;
8201
8202 imfs = get_data (NULL, filedata,
8203 filedata->dynamic_addr + fixup->fixup_rela_off,
8204 sizeof (*imfs), fixup->fixup_rela_cnt,
8205 _("dynamic section image fixups"));
8206 if (!imfs)
8207 return false;
8208
8209 if (fixup->needed < strtab_sz)
8210 lib_name = strtab + fixup->needed;
8211 else
8212 {
8213 warn (_("corrupt library name index of 0x%lx found in dynamic entry"),
8214 (unsigned long) fixup->needed);
8215 lib_name = "???";
8216 }
8217
8218 printf (_("\nImage fixups for needed library #%d: %s - ident: %lx\n"),
8219 (int) fixup->fixup_needed, lib_name, (long) fixup->needed_ident);
8220 printf
8221 (_("Seg Offset Type SymVec DataType\n"));
8222
8223 for (i = 0; i < (long) fixup->fixup_rela_cnt; i++)
8224 {
8225 unsigned int type;
8226 const char *rtype;
8227
8228 printf ("%3u ", (unsigned) BYTE_GET (imfs [i].fixup_seg));
8229 printf ("%016" PRIx64 " ", BYTE_GET (imfs [i].fixup_offset));
8230 type = BYTE_GET (imfs [i].type);
8231 rtype = elf_ia64_reloc_type (type);
8232 if (rtype == NULL)
8233 printf ("0x%08x ", type);
8234 else
8235 printf ("%-32s ", rtype);
8236 printf ("%6u ", (unsigned) BYTE_GET (imfs [i].symvec_index));
8237 printf ("0x%08x\n", (unsigned) BYTE_GET (imfs [i].data_type));
8238 }
8239
8240 free (imfs);
8241 return true;
8242 }
8243
8244 /* Display IA-64 OpenVMS dynamic relocations (used to relocate an image). */
8245
8246 static bool
8247 dump_ia64_vms_dynamic_relocs (Filedata * filedata, struct ia64_vms_dynimgrela *imgrela)
8248 {
8249 Elf64_External_VMS_IMAGE_RELA *imrs;
8250 long i;
8251
8252 imrs = get_data (NULL, filedata,
8253 filedata->dynamic_addr + imgrela->img_rela_off,
8254 sizeof (*imrs), imgrela->img_rela_cnt,
8255 _("dynamic section image relocations"));
8256 if (!imrs)
8257 return false;
8258
8259 printf (_("\nImage relocs\n"));
8260 printf
8261 (_("Seg Offset Type Addend Seg Sym Off\n"));
8262
8263 for (i = 0; i < (long) imgrela->img_rela_cnt; i++)
8264 {
8265 unsigned int type;
8266 const char *rtype;
8267
8268 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].rela_seg));
8269 printf ("%08" PRIx64 " ", BYTE_GET (imrs [i].rela_offset));
8270 type = BYTE_GET (imrs [i].type);
8271 rtype = elf_ia64_reloc_type (type);
8272 if (rtype == NULL)
8273 printf ("0x%08x ", type);
8274 else
8275 printf ("%-31s ", rtype);
8276 print_vma (BYTE_GET (imrs [i].addend), FULL_HEX);
8277 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].sym_seg));
8278 printf ("%08" PRIx64 "\n", BYTE_GET (imrs [i].sym_offset));
8279 }
8280
8281 free (imrs);
8282 return true;
8283 }
8284
8285 /* Display IA-64 OpenVMS dynamic relocations and fixups. */
8286
8287 static bool
8288 process_ia64_vms_dynamic_relocs (Filedata * filedata)
8289 {
8290 struct ia64_vms_dynfixup fixup;
8291 struct ia64_vms_dynimgrela imgrela;
8292 Elf_Internal_Dyn *entry;
8293 uint64_t strtab_off = 0;
8294 uint64_t strtab_sz = 0;
8295 char *strtab = NULL;
8296 bool res = true;
8297
8298 memset (&fixup, 0, sizeof (fixup));
8299 memset (&imgrela, 0, sizeof (imgrela));
8300
8301 /* Note: the order of the entries is specified by the OpenVMS specs. */
8302 for (entry = filedata->dynamic_section;
8303 entry < filedata->dynamic_section + filedata->dynamic_nent;
8304 entry++)
8305 {
8306 switch (entry->d_tag)
8307 {
8308 case DT_IA_64_VMS_STRTAB_OFFSET:
8309 strtab_off = entry->d_un.d_val;
8310 break;
8311 case DT_STRSZ:
8312 strtab_sz = entry->d_un.d_val;
8313 if (strtab == NULL)
8314 strtab = get_data (NULL, filedata,
8315 filedata->dynamic_addr + strtab_off,
8316 1, strtab_sz, _("dynamic string section"));
8317 if (strtab == NULL)
8318 strtab_sz = 0;
8319 break;
8320
8321 case DT_IA_64_VMS_NEEDED_IDENT:
8322 fixup.needed_ident = entry->d_un.d_val;
8323 break;
8324 case DT_NEEDED:
8325 fixup.needed = entry->d_un.d_val;
8326 break;
8327 case DT_IA_64_VMS_FIXUP_NEEDED:
8328 fixup.fixup_needed = entry->d_un.d_val;
8329 break;
8330 case DT_IA_64_VMS_FIXUP_RELA_CNT:
8331 fixup.fixup_rela_cnt = entry->d_un.d_val;
8332 break;
8333 case DT_IA_64_VMS_FIXUP_RELA_OFF:
8334 fixup.fixup_rela_off = entry->d_un.d_val;
8335 if (! dump_ia64_vms_dynamic_fixups (filedata, &fixup, strtab, strtab_sz))
8336 res = false;
8337 break;
8338 case DT_IA_64_VMS_IMG_RELA_CNT:
8339 imgrela.img_rela_cnt = entry->d_un.d_val;
8340 break;
8341 case DT_IA_64_VMS_IMG_RELA_OFF:
8342 imgrela.img_rela_off = entry->d_un.d_val;
8343 if (! dump_ia64_vms_dynamic_relocs (filedata, &imgrela))
8344 res = false;
8345 break;
8346
8347 default:
8348 break;
8349 }
8350 }
8351
8352 free (strtab);
8353
8354 return res;
8355 }
8356
8357 static struct
8358 {
8359 const char * name;
8360 int reloc;
8361 int size;
8362 relocation_type rel_type;
8363 }
8364 dynamic_relocations [] =
8365 {
8366 { "REL", DT_REL, DT_RELSZ, reltype_rel },
8367 { "RELA", DT_RELA, DT_RELASZ, reltype_rela },
8368 { "RELR", DT_RELR, DT_RELRSZ, reltype_relr },
8369 { "PLT", DT_JMPREL, DT_PLTRELSZ, reltype_unknown }
8370 };
8371
8372 /* Process the reloc section. */
8373
8374 static bool
8375 process_relocs (Filedata * filedata)
8376 {
8377 unsigned long rel_size;
8378 unsigned long rel_offset;
8379
8380 if (!do_reloc)
8381 return true;
8382
8383 if (do_using_dynamic)
8384 {
8385 relocation_type rel_type;
8386 const char * name;
8387 bool has_dynamic_reloc;
8388 unsigned int i;
8389
8390 has_dynamic_reloc = false;
8391
8392 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
8393 {
8394 rel_type = dynamic_relocations [i].rel_type;
8395 name = dynamic_relocations [i].name;
8396 rel_size = filedata->dynamic_info[dynamic_relocations [i].size];
8397 rel_offset = filedata->dynamic_info[dynamic_relocations [i].reloc];
8398
8399 if (rel_size)
8400 has_dynamic_reloc = true;
8401
8402 if (rel_type == reltype_unknown)
8403 {
8404 if (dynamic_relocations [i].reloc == DT_JMPREL)
8405 switch (filedata->dynamic_info[DT_PLTREL])
8406 {
8407 case DT_REL:
8408 rel_type = reltype_rel;
8409 break;
8410 case DT_RELA:
8411 rel_type = reltype_rela;
8412 break;
8413 }
8414 }
8415
8416 if (rel_size)
8417 {
8418 if (filedata->is_separate)
8419 printf
8420 (_("\nIn linked file '%s' section '%s' at offset 0x%lx contains %ld bytes:\n"),
8421 filedata->file_name, name, rel_offset, rel_size);
8422 else
8423 printf
8424 (_("\n'%s' relocation section at offset 0x%lx contains %ld bytes:\n"),
8425 name, rel_offset, rel_size);
8426
8427 dump_relocations (filedata,
8428 offset_from_vma (filedata, rel_offset, rel_size),
8429 rel_size,
8430 filedata->dynamic_symbols,
8431 filedata->num_dynamic_syms,
8432 filedata->dynamic_strings,
8433 filedata->dynamic_strings_length,
8434 rel_type, true /* is_dynamic */);
8435 }
8436 }
8437
8438 if (is_ia64_vms (filedata))
8439 if (process_ia64_vms_dynamic_relocs (filedata))
8440 has_dynamic_reloc = true;
8441
8442 if (! has_dynamic_reloc)
8443 {
8444 if (filedata->is_separate)
8445 printf (_("\nThere are no dynamic relocations in linked file '%s'.\n"),
8446 filedata->file_name);
8447 else
8448 printf (_("\nThere are no dynamic relocations in this file.\n"));
8449 }
8450 }
8451 else
8452 {
8453 Elf_Internal_Shdr * section;
8454 unsigned long i;
8455 bool found = false;
8456
8457 for (i = 0, section = filedata->section_headers;
8458 i < filedata->file_header.e_shnum;
8459 i++, section++)
8460 {
8461 if ( section->sh_type != SHT_RELA
8462 && section->sh_type != SHT_REL
8463 && section->sh_type != SHT_RELR)
8464 continue;
8465
8466 rel_offset = section->sh_offset;
8467 rel_size = section->sh_size;
8468
8469 if (rel_size)
8470 {
8471 relocation_type rel_type;
8472 unsigned long num_rela;
8473
8474 if (filedata->is_separate)
8475 printf (_("\nIn linked file '%s' relocation section "),
8476 filedata->file_name);
8477 else
8478 printf (_("\nRelocation section "));
8479
8480 if (filedata->string_table == NULL)
8481 printf ("%d", section->sh_name);
8482 else
8483 printf ("'%s'", printable_section_name (filedata, section));
8484
8485 num_rela = rel_size / section->sh_entsize;
8486 printf (ngettext (" at offset 0x%lx contains %lu entry:\n",
8487 " at offset 0x%lx contains %lu entries:\n",
8488 num_rela),
8489 rel_offset, num_rela);
8490
8491 rel_type = section->sh_type == SHT_RELA ? reltype_rela :
8492 section->sh_type == SHT_REL ? reltype_rel : reltype_relr;
8493
8494 if (section->sh_link != 0
8495 && section->sh_link < filedata->file_header.e_shnum)
8496 {
8497 Elf_Internal_Shdr * symsec;
8498 Elf_Internal_Sym * symtab;
8499 unsigned long nsyms;
8500 unsigned long strtablen = 0;
8501 char * strtab = NULL;
8502
8503 symsec = filedata->section_headers + section->sh_link;
8504 if (symsec->sh_type != SHT_SYMTAB
8505 && symsec->sh_type != SHT_DYNSYM)
8506 continue;
8507
8508 if (!get_symtab (filedata, symsec,
8509 &symtab, &nsyms, &strtab, &strtablen))
8510 continue;
8511
8512 dump_relocations (filedata, rel_offset, rel_size,
8513 symtab, nsyms, strtab, strtablen,
8514 rel_type,
8515 symsec->sh_type == SHT_DYNSYM);
8516 free (strtab);
8517 free (symtab);
8518 }
8519 else
8520 dump_relocations (filedata, rel_offset, rel_size,
8521 NULL, 0, NULL, 0, rel_type, false /* is_dynamic */);
8522
8523 found = true;
8524 }
8525 }
8526
8527 if (! found)
8528 {
8529 /* Users sometimes forget the -D option, so try to be helpful. */
8530 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
8531 {
8532 if (filedata->dynamic_info[dynamic_relocations [i].size])
8533 {
8534 if (filedata->is_separate)
8535 printf (_("\nThere are no static relocations in linked file '%s'."),
8536 filedata->file_name);
8537 else
8538 printf (_("\nThere are no static relocations in this file."));
8539 printf (_("\nTo see the dynamic relocations add --use-dynamic to the command line.\n"));
8540
8541 break;
8542 }
8543 }
8544 if (i == ARRAY_SIZE (dynamic_relocations))
8545 {
8546 if (filedata->is_separate)
8547 printf (_("\nThere are no relocations in linked file '%s'.\n"),
8548 filedata->file_name);
8549 else
8550 printf (_("\nThere are no relocations in this file.\n"));
8551 }
8552 }
8553 }
8554
8555 return true;
8556 }
8557
8558 /* An absolute address consists of a section and an offset. If the
8559 section is NULL, the offset itself is the address, otherwise, the
8560 address equals to LOAD_ADDRESS(section) + offset. */
8561
8562 struct absaddr
8563 {
8564 unsigned short section;
8565 uint64_t offset;
8566 };
8567
8568 /* Find the nearest symbol at or below ADDR. Returns the symbol
8569 name, if found, and the offset from the symbol to ADDR. */
8570
8571 static void
8572 find_symbol_for_address (Filedata * filedata,
8573 Elf_Internal_Sym * symtab,
8574 unsigned long nsyms,
8575 const char * strtab,
8576 unsigned long strtab_size,
8577 struct absaddr addr,
8578 const char ** symname,
8579 uint64_t * offset)
8580 {
8581 uint64_t dist = 0x100000;
8582 Elf_Internal_Sym * sym;
8583 Elf_Internal_Sym * beg;
8584 Elf_Internal_Sym * end;
8585 Elf_Internal_Sym * best = NULL;
8586
8587 REMOVE_ARCH_BITS (addr.offset);
8588 beg = symtab;
8589 end = symtab + nsyms;
8590
8591 while (beg < end)
8592 {
8593 uint64_t value;
8594
8595 sym = beg + (end - beg) / 2;
8596
8597 value = sym->st_value;
8598 REMOVE_ARCH_BITS (value);
8599
8600 if (sym->st_name != 0
8601 && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
8602 && addr.offset >= value
8603 && addr.offset - value < dist)
8604 {
8605 best = sym;
8606 dist = addr.offset - value;
8607 if (!dist)
8608 break;
8609 }
8610
8611 if (addr.offset < value)
8612 end = sym;
8613 else
8614 beg = sym + 1;
8615 }
8616
8617 if (best)
8618 {
8619 *symname = (best->st_name >= strtab_size
8620 ? _("<corrupt>") : strtab + best->st_name);
8621 *offset = dist;
8622 return;
8623 }
8624
8625 *symname = NULL;
8626 *offset = addr.offset;
8627 }
8628
8629 static /* signed */ int
8630 symcmp (const void *p, const void *q)
8631 {
8632 Elf_Internal_Sym *sp = (Elf_Internal_Sym *) p;
8633 Elf_Internal_Sym *sq = (Elf_Internal_Sym *) q;
8634
8635 return sp->st_value > sq->st_value ? 1 : (sp->st_value < sq->st_value ? -1 : 0);
8636 }
8637
8638 /* Process the unwind section. */
8639
8640 #include "unwind-ia64.h"
8641
8642 struct ia64_unw_table_entry
8643 {
8644 struct absaddr start;
8645 struct absaddr end;
8646 struct absaddr info;
8647 };
8648
8649 struct ia64_unw_aux_info
8650 {
8651 struct ia64_unw_table_entry * table; /* Unwind table. */
8652 unsigned long table_len; /* Length of unwind table. */
8653 unsigned char * info; /* Unwind info. */
8654 unsigned long info_size; /* Size of unwind info. */
8655 uint64_t info_addr; /* Starting address of unwind info. */
8656 uint64_t seg_base; /* Starting address of segment. */
8657 Elf_Internal_Sym * symtab; /* The symbol table. */
8658 unsigned long nsyms; /* Number of symbols. */
8659 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
8660 unsigned long nfuns; /* Number of entries in funtab. */
8661 char * strtab; /* The string table. */
8662 unsigned long strtab_size; /* Size of string table. */
8663 };
8664
8665 static bool
8666 dump_ia64_unwind (Filedata * filedata, struct ia64_unw_aux_info * aux)
8667 {
8668 struct ia64_unw_table_entry * tp;
8669 unsigned long j, nfuns;
8670 int in_body;
8671 bool res = true;
8672
8673 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
8674 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
8675 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
8676 aux->funtab[nfuns++] = aux->symtab[j];
8677 aux->nfuns = nfuns;
8678 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
8679
8680 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
8681 {
8682 uint64_t stamp;
8683 uint64_t offset;
8684 const unsigned char * dp;
8685 const unsigned char * head;
8686 const unsigned char * end;
8687 const char * procname;
8688
8689 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
8690 aux->strtab_size, tp->start, &procname, &offset);
8691
8692 fputs ("\n<", stdout);
8693
8694 if (procname)
8695 {
8696 fputs (procname, stdout);
8697
8698 if (offset)
8699 printf ("+%lx", (unsigned long) offset);
8700 }
8701
8702 fputs (">: [", stdout);
8703 print_vma (tp->start.offset, PREFIX_HEX);
8704 fputc ('-', stdout);
8705 print_vma (tp->end.offset, PREFIX_HEX);
8706 printf ("], info at +0x%lx\n",
8707 (unsigned long) (tp->info.offset - aux->seg_base));
8708
8709 /* PR 17531: file: 86232b32. */
8710 if (aux->info == NULL)
8711 continue;
8712
8713 offset = tp->info.offset;
8714 if (tp->info.section)
8715 {
8716 if (tp->info.section >= filedata->file_header.e_shnum)
8717 {
8718 warn (_("Invalid section %u in table entry %ld\n"),
8719 tp->info.section, (long) (tp - aux->table));
8720 res = false;
8721 continue;
8722 }
8723 offset += filedata->section_headers[tp->info.section].sh_addr;
8724 }
8725 offset -= aux->info_addr;
8726 /* PR 17531: file: 0997b4d1. */
8727 if (offset >= aux->info_size
8728 || aux->info_size - offset < 8)
8729 {
8730 warn (_("Invalid offset %lx in table entry %ld\n"),
8731 (long) tp->info.offset, (long) (tp - aux->table));
8732 res = false;
8733 continue;
8734 }
8735
8736 head = aux->info + offset;
8737 stamp = byte_get ((unsigned char *) head, sizeof (stamp));
8738
8739 printf (" v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
8740 (unsigned) UNW_VER (stamp),
8741 (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
8742 UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
8743 UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
8744 (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
8745
8746 if (UNW_VER (stamp) != 1)
8747 {
8748 printf (_("\tUnknown version.\n"));
8749 continue;
8750 }
8751
8752 in_body = 0;
8753 end = head + 8 + eh_addr_size * UNW_LENGTH (stamp);
8754 /* PR 17531: file: 16ceda89. */
8755 if (end > aux->info + aux->info_size)
8756 end = aux->info + aux->info_size;
8757 for (dp = head + 8; dp < end;)
8758 dp = unw_decode (dp, in_body, & in_body, end);
8759 }
8760
8761 free (aux->funtab);
8762
8763 return res;
8764 }
8765
8766 static bool
8767 slurp_ia64_unwind_table (Filedata * filedata,
8768 struct ia64_unw_aux_info * aux,
8769 Elf_Internal_Shdr * sec)
8770 {
8771 unsigned long size, nrelas, i;
8772 Elf_Internal_Phdr * seg;
8773 struct ia64_unw_table_entry * tep;
8774 Elf_Internal_Shdr * relsec;
8775 Elf_Internal_Rela * rela;
8776 Elf_Internal_Rela * rp;
8777 unsigned char * table;
8778 unsigned char * tp;
8779 Elf_Internal_Sym * sym;
8780 const char * relname;
8781
8782 aux->table_len = 0;
8783
8784 /* First, find the starting address of the segment that includes
8785 this section: */
8786
8787 if (filedata->file_header.e_phnum)
8788 {
8789 if (! get_program_headers (filedata))
8790 return false;
8791
8792 for (seg = filedata->program_headers;
8793 seg < filedata->program_headers + filedata->file_header.e_phnum;
8794 ++seg)
8795 {
8796 if (seg->p_type != PT_LOAD)
8797 continue;
8798
8799 if (sec->sh_addr >= seg->p_vaddr
8800 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
8801 {
8802 aux->seg_base = seg->p_vaddr;
8803 break;
8804 }
8805 }
8806 }
8807
8808 /* Second, build the unwind table from the contents of the unwind section: */
8809 size = sec->sh_size;
8810 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
8811 _("unwind table"));
8812 if (!table)
8813 return false;
8814
8815 aux->table_len = size / (3 * eh_addr_size);
8816 aux->table = (struct ia64_unw_table_entry *)
8817 xcmalloc (aux->table_len, sizeof (aux->table[0]));
8818 tep = aux->table;
8819
8820 for (tp = table; tp <= table + size - (3 * eh_addr_size); ++tep)
8821 {
8822 tep->start.section = SHN_UNDEF;
8823 tep->end.section = SHN_UNDEF;
8824 tep->info.section = SHN_UNDEF;
8825 tep->start.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8826 tep->end.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8827 tep->info.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8828 tep->start.offset += aux->seg_base;
8829 tep->end.offset += aux->seg_base;
8830 tep->info.offset += aux->seg_base;
8831 }
8832 free (table);
8833
8834 /* Third, apply any relocations to the unwind table: */
8835 for (relsec = filedata->section_headers;
8836 relsec < filedata->section_headers + filedata->file_header.e_shnum;
8837 ++relsec)
8838 {
8839 if (relsec->sh_type != SHT_RELA
8840 || relsec->sh_info >= filedata->file_header.e_shnum
8841 || filedata->section_headers + relsec->sh_info != sec)
8842 continue;
8843
8844 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
8845 & rela, & nrelas))
8846 {
8847 free (aux->table);
8848 aux->table = NULL;
8849 aux->table_len = 0;
8850 return false;
8851 }
8852
8853 for (rp = rela; rp < rela + nrelas; ++rp)
8854 {
8855 unsigned int sym_ndx;
8856 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
8857 relname = elf_ia64_reloc_type (r_type);
8858
8859 /* PR 17531: file: 9fa67536. */
8860 if (relname == NULL)
8861 {
8862 warn (_("Skipping unknown relocation type: %u\n"), r_type);
8863 continue;
8864 }
8865
8866 if (! startswith (relname, "R_IA64_SEGREL"))
8867 {
8868 warn (_("Skipping unexpected relocation type: %s\n"), relname);
8869 continue;
8870 }
8871
8872 i = rp->r_offset / (3 * eh_addr_size);
8873
8874 /* PR 17531: file: 5bc8d9bf. */
8875 if (i >= aux->table_len)
8876 {
8877 warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
8878 continue;
8879 }
8880
8881 sym_ndx = get_reloc_symindex (rp->r_info);
8882 if (sym_ndx >= aux->nsyms)
8883 {
8884 warn (_("Skipping reloc with invalid symbol index: %u\n"),
8885 sym_ndx);
8886 continue;
8887 }
8888 sym = aux->symtab + sym_ndx;
8889
8890 switch (rp->r_offset / eh_addr_size % 3)
8891 {
8892 case 0:
8893 aux->table[i].start.section = sym->st_shndx;
8894 aux->table[i].start.offset = rp->r_addend + sym->st_value;
8895 break;
8896 case 1:
8897 aux->table[i].end.section = sym->st_shndx;
8898 aux->table[i].end.offset = rp->r_addend + sym->st_value;
8899 break;
8900 case 2:
8901 aux->table[i].info.section = sym->st_shndx;
8902 aux->table[i].info.offset = rp->r_addend + sym->st_value;
8903 break;
8904 default:
8905 break;
8906 }
8907 }
8908
8909 free (rela);
8910 }
8911
8912 return true;
8913 }
8914
8915 static bool
8916 ia64_process_unwind (Filedata * filedata)
8917 {
8918 Elf_Internal_Shdr * sec;
8919 Elf_Internal_Shdr * unwsec = NULL;
8920 unsigned long i, unwcount = 0, unwstart = 0;
8921 struct ia64_unw_aux_info aux;
8922 bool res = true;
8923
8924 memset (& aux, 0, sizeof (aux));
8925
8926 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
8927 {
8928 if (sec->sh_type == SHT_SYMTAB)
8929 {
8930 if (aux.symtab)
8931 {
8932 error (_("Multiple symbol tables encountered\n"));
8933 free (aux.symtab);
8934 aux.symtab = NULL;
8935 free (aux.strtab);
8936 aux.strtab = NULL;
8937 }
8938 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
8939 &aux.strtab, &aux.strtab_size))
8940 return false;
8941 }
8942 else if (sec->sh_type == SHT_IA_64_UNWIND)
8943 unwcount++;
8944 }
8945
8946 if (!unwcount)
8947 printf (_("\nThere are no unwind sections in this file.\n"));
8948
8949 while (unwcount-- > 0)
8950 {
8951 const char *suffix;
8952 size_t len, len2;
8953
8954 for (i = unwstart, sec = filedata->section_headers + unwstart, unwsec = NULL;
8955 i < filedata->file_header.e_shnum; ++i, ++sec)
8956 if (sec->sh_type == SHT_IA_64_UNWIND)
8957 {
8958 unwsec = sec;
8959 break;
8960 }
8961 /* We have already counted the number of SHT_IA64_UNWIND
8962 sections so the loop above should never fail. */
8963 assert (unwsec != NULL);
8964
8965 unwstart = i + 1;
8966 len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
8967
8968 if ((unwsec->sh_flags & SHF_GROUP) != 0)
8969 {
8970 /* We need to find which section group it is in. */
8971 struct group_list * g;
8972
8973 if (filedata->section_headers_groups == NULL
8974 || filedata->section_headers_groups[i] == NULL)
8975 i = filedata->file_header.e_shnum;
8976 else
8977 {
8978 g = filedata->section_headers_groups[i]->root;
8979
8980 for (; g != NULL; g = g->next)
8981 {
8982 sec = filedata->section_headers + g->section_index;
8983
8984 if (section_name_valid (filedata, sec)
8985 && streq (section_name (filedata, sec),
8986 ELF_STRING_ia64_unwind_info))
8987 break;
8988 }
8989
8990 if (g == NULL)
8991 i = filedata->file_header.e_shnum;
8992 }
8993 }
8994 else if (section_name_valid (filedata, unwsec)
8995 && startswith (section_name (filedata, unwsec),
8996 ELF_STRING_ia64_unwind_once))
8997 {
8998 /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO. */
8999 len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
9000 suffix = section_name (filedata, unwsec) + len;
9001 for (i = 0, sec = filedata->section_headers;
9002 i < filedata->file_header.e_shnum;
9003 ++i, ++sec)
9004 if (section_name_valid (filedata, sec)
9005 && startswith (section_name (filedata, sec),
9006 ELF_STRING_ia64_unwind_info_once)
9007 && streq (section_name (filedata, sec) + len2, suffix))
9008 break;
9009 }
9010 else
9011 {
9012 /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
9013 .IA_64.unwind or BAR -> .IA_64.unwind_info. */
9014 len = sizeof (ELF_STRING_ia64_unwind) - 1;
9015 len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
9016 suffix = "";
9017 if (section_name_valid (filedata, unwsec)
9018 && startswith (section_name (filedata, unwsec),
9019 ELF_STRING_ia64_unwind))
9020 suffix = section_name (filedata, unwsec) + len;
9021 for (i = 0, sec = filedata->section_headers;
9022 i < filedata->file_header.e_shnum;
9023 ++i, ++sec)
9024 if (section_name_valid (filedata, sec)
9025 && startswith (section_name (filedata, sec),
9026 ELF_STRING_ia64_unwind_info)
9027 && streq (section_name (filedata, sec) + len2, suffix))
9028 break;
9029 }
9030
9031 if (i == filedata->file_header.e_shnum)
9032 {
9033 printf (_("\nCould not find unwind info section for "));
9034
9035 if (filedata->string_table == NULL)
9036 printf ("%d", unwsec->sh_name);
9037 else
9038 printf ("'%s'", printable_section_name (filedata, unwsec));
9039 }
9040 else
9041 {
9042 aux.info_addr = sec->sh_addr;
9043 aux.info = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1,
9044 sec->sh_size,
9045 _("unwind info"));
9046 aux.info_size = aux.info == NULL ? 0 : sec->sh_size;
9047
9048 printf (_("\nUnwind section "));
9049
9050 if (filedata->string_table == NULL)
9051 printf ("%d", unwsec->sh_name);
9052 else
9053 printf ("'%s'", printable_section_name (filedata, unwsec));
9054
9055 printf (_(" at offset 0x%lx contains %lu entries:\n"),
9056 (unsigned long) unwsec->sh_offset,
9057 (unsigned long) (unwsec->sh_size / (3 * eh_addr_size)));
9058
9059 if (slurp_ia64_unwind_table (filedata, & aux, unwsec)
9060 && aux.table_len > 0)
9061 dump_ia64_unwind (filedata, & aux);
9062
9063 free ((char *) aux.table);
9064 free ((char *) aux.info);
9065 aux.table = NULL;
9066 aux.info = NULL;
9067 }
9068 }
9069
9070 free (aux.symtab);
9071 free ((char *) aux.strtab);
9072
9073 return res;
9074 }
9075
9076 struct hppa_unw_table_entry
9077 {
9078 struct absaddr start;
9079 struct absaddr end;
9080 unsigned int Cannot_unwind:1; /* 0 */
9081 unsigned int Millicode:1; /* 1 */
9082 unsigned int Millicode_save_sr0:1; /* 2 */
9083 unsigned int Region_description:2; /* 3..4 */
9084 unsigned int reserved1:1; /* 5 */
9085 unsigned int Entry_SR:1; /* 6 */
9086 unsigned int Entry_FR:4; /* Number saved 7..10 */
9087 unsigned int Entry_GR:5; /* Number saved 11..15 */
9088 unsigned int Args_stored:1; /* 16 */
9089 unsigned int Variable_Frame:1; /* 17 */
9090 unsigned int Separate_Package_Body:1; /* 18 */
9091 unsigned int Frame_Extension_Millicode:1; /* 19 */
9092 unsigned int Stack_Overflow_Check:1; /* 20 */
9093 unsigned int Two_Instruction_SP_Increment:1; /* 21 */
9094 unsigned int Ada_Region:1; /* 22 */
9095 unsigned int cxx_info:1; /* 23 */
9096 unsigned int cxx_try_catch:1; /* 24 */
9097 unsigned int sched_entry_seq:1; /* 25 */
9098 unsigned int reserved2:1; /* 26 */
9099 unsigned int Save_SP:1; /* 27 */
9100 unsigned int Save_RP:1; /* 28 */
9101 unsigned int Save_MRP_in_frame:1; /* 29 */
9102 unsigned int extn_ptr_defined:1; /* 30 */
9103 unsigned int Cleanup_defined:1; /* 31 */
9104
9105 unsigned int MPE_XL_interrupt_marker:1; /* 0 */
9106 unsigned int HP_UX_interrupt_marker:1; /* 1 */
9107 unsigned int Large_frame:1; /* 2 */
9108 unsigned int Pseudo_SP_Set:1; /* 3 */
9109 unsigned int reserved4:1; /* 4 */
9110 unsigned int Total_frame_size:27; /* 5..31 */
9111 };
9112
9113 struct hppa_unw_aux_info
9114 {
9115 struct hppa_unw_table_entry * table; /* Unwind table. */
9116 unsigned long table_len; /* Length of unwind table. */
9117 uint64_t seg_base; /* Starting address of segment. */
9118 Elf_Internal_Sym * symtab; /* The symbol table. */
9119 unsigned long nsyms; /* Number of symbols. */
9120 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
9121 unsigned long nfuns; /* Number of entries in funtab. */
9122 char * strtab; /* The string table. */
9123 unsigned long strtab_size; /* Size of string table. */
9124 };
9125
9126 static bool
9127 dump_hppa_unwind (Filedata * filedata, struct hppa_unw_aux_info * aux)
9128 {
9129 struct hppa_unw_table_entry * tp;
9130 unsigned long j, nfuns;
9131 bool res = true;
9132
9133 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
9134 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
9135 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
9136 aux->funtab[nfuns++] = aux->symtab[j];
9137 aux->nfuns = nfuns;
9138 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
9139
9140 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
9141 {
9142 uint64_t offset;
9143 const char * procname;
9144
9145 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
9146 aux->strtab_size, tp->start, &procname,
9147 &offset);
9148
9149 fputs ("\n<", stdout);
9150
9151 if (procname)
9152 {
9153 fputs (procname, stdout);
9154
9155 if (offset)
9156 printf ("+%lx", (unsigned long) offset);
9157 }
9158
9159 fputs (">: [", stdout);
9160 print_vma (tp->start.offset, PREFIX_HEX);
9161 fputc ('-', stdout);
9162 print_vma (tp->end.offset, PREFIX_HEX);
9163 printf ("]\n\t");
9164
9165 #define PF(_m) if (tp->_m) printf (#_m " ");
9166 #define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
9167 PF(Cannot_unwind);
9168 PF(Millicode);
9169 PF(Millicode_save_sr0);
9170 /* PV(Region_description); */
9171 PF(Entry_SR);
9172 PV(Entry_FR);
9173 PV(Entry_GR);
9174 PF(Args_stored);
9175 PF(Variable_Frame);
9176 PF(Separate_Package_Body);
9177 PF(Frame_Extension_Millicode);
9178 PF(Stack_Overflow_Check);
9179 PF(Two_Instruction_SP_Increment);
9180 PF(Ada_Region);
9181 PF(cxx_info);
9182 PF(cxx_try_catch);
9183 PF(sched_entry_seq);
9184 PF(Save_SP);
9185 PF(Save_RP);
9186 PF(Save_MRP_in_frame);
9187 PF(extn_ptr_defined);
9188 PF(Cleanup_defined);
9189 PF(MPE_XL_interrupt_marker);
9190 PF(HP_UX_interrupt_marker);
9191 PF(Large_frame);
9192 PF(Pseudo_SP_Set);
9193 PV(Total_frame_size);
9194 #undef PF
9195 #undef PV
9196 }
9197
9198 printf ("\n");
9199
9200 free (aux->funtab);
9201
9202 return res;
9203 }
9204
9205 static bool
9206 slurp_hppa_unwind_table (Filedata * filedata,
9207 struct hppa_unw_aux_info * aux,
9208 Elf_Internal_Shdr * sec)
9209 {
9210 unsigned long size, unw_ent_size, nentries, nrelas, i;
9211 Elf_Internal_Phdr * seg;
9212 struct hppa_unw_table_entry * tep;
9213 Elf_Internal_Shdr * relsec;
9214 Elf_Internal_Rela * rela;
9215 Elf_Internal_Rela * rp;
9216 unsigned char * table;
9217 unsigned char * tp;
9218 Elf_Internal_Sym * sym;
9219 const char * relname;
9220
9221 /* First, find the starting address of the segment that includes
9222 this section. */
9223 if (filedata->file_header.e_phnum)
9224 {
9225 if (! get_program_headers (filedata))
9226 return false;
9227
9228 for (seg = filedata->program_headers;
9229 seg < filedata->program_headers + filedata->file_header.e_phnum;
9230 ++seg)
9231 {
9232 if (seg->p_type != PT_LOAD)
9233 continue;
9234
9235 if (sec->sh_addr >= seg->p_vaddr
9236 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
9237 {
9238 aux->seg_base = seg->p_vaddr;
9239 break;
9240 }
9241 }
9242 }
9243
9244 /* Second, build the unwind table from the contents of the unwind
9245 section. */
9246 size = sec->sh_size;
9247 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
9248 _("unwind table"));
9249 if (!table)
9250 return false;
9251
9252 unw_ent_size = 16;
9253 nentries = size / unw_ent_size;
9254 size = unw_ent_size * nentries;
9255
9256 aux->table_len = nentries;
9257 tep = aux->table = (struct hppa_unw_table_entry *)
9258 xcmalloc (nentries, sizeof (aux->table[0]));
9259
9260 for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
9261 {
9262 unsigned int tmp1, tmp2;
9263
9264 tep->start.section = SHN_UNDEF;
9265 tep->end.section = SHN_UNDEF;
9266
9267 tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
9268 tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
9269 tmp1 = byte_get ((unsigned char *) tp + 8, 4);
9270 tmp2 = byte_get ((unsigned char *) tp + 12, 4);
9271
9272 tep->start.offset += aux->seg_base;
9273 tep->end.offset += aux->seg_base;
9274
9275 tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
9276 tep->Millicode = (tmp1 >> 30) & 0x1;
9277 tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
9278 tep->Region_description = (tmp1 >> 27) & 0x3;
9279 tep->reserved1 = (tmp1 >> 26) & 0x1;
9280 tep->Entry_SR = (tmp1 >> 25) & 0x1;
9281 tep->Entry_FR = (tmp1 >> 21) & 0xf;
9282 tep->Entry_GR = (tmp1 >> 16) & 0x1f;
9283 tep->Args_stored = (tmp1 >> 15) & 0x1;
9284 tep->Variable_Frame = (tmp1 >> 14) & 0x1;
9285 tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
9286 tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
9287 tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
9288 tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
9289 tep->Ada_Region = (tmp1 >> 9) & 0x1;
9290 tep->cxx_info = (tmp1 >> 8) & 0x1;
9291 tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
9292 tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
9293 tep->reserved2 = (tmp1 >> 5) & 0x1;
9294 tep->Save_SP = (tmp1 >> 4) & 0x1;
9295 tep->Save_RP = (tmp1 >> 3) & 0x1;
9296 tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
9297 tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
9298 tep->Cleanup_defined = tmp1 & 0x1;
9299
9300 tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
9301 tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
9302 tep->Large_frame = (tmp2 >> 29) & 0x1;
9303 tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
9304 tep->reserved4 = (tmp2 >> 27) & 0x1;
9305 tep->Total_frame_size = tmp2 & 0x7ffffff;
9306 }
9307 free (table);
9308
9309 /* Third, apply any relocations to the unwind table. */
9310 for (relsec = filedata->section_headers;
9311 relsec < filedata->section_headers + filedata->file_header.e_shnum;
9312 ++relsec)
9313 {
9314 if (relsec->sh_type != SHT_RELA
9315 || relsec->sh_info >= filedata->file_header.e_shnum
9316 || filedata->section_headers + relsec->sh_info != sec)
9317 continue;
9318
9319 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
9320 & rela, & nrelas))
9321 return false;
9322
9323 for (rp = rela; rp < rela + nrelas; ++rp)
9324 {
9325 unsigned int sym_ndx;
9326 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
9327 relname = elf_hppa_reloc_type (r_type);
9328
9329 if (relname == NULL)
9330 {
9331 warn (_("Skipping unknown relocation type: %u\n"), r_type);
9332 continue;
9333 }
9334
9335 /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64. */
9336 if (! startswith (relname, "R_PARISC_SEGREL"))
9337 {
9338 warn (_("Skipping unexpected relocation type: %s\n"), relname);
9339 continue;
9340 }
9341
9342 i = rp->r_offset / unw_ent_size;
9343 if (i >= aux->table_len)
9344 {
9345 warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
9346 continue;
9347 }
9348
9349 sym_ndx = get_reloc_symindex (rp->r_info);
9350 if (sym_ndx >= aux->nsyms)
9351 {
9352 warn (_("Skipping reloc with invalid symbol index: %u\n"),
9353 sym_ndx);
9354 continue;
9355 }
9356 sym = aux->symtab + sym_ndx;
9357
9358 switch ((rp->r_offset % unw_ent_size) / 4)
9359 {
9360 case 0:
9361 aux->table[i].start.section = sym->st_shndx;
9362 aux->table[i].start.offset = sym->st_value + rp->r_addend;
9363 break;
9364 case 1:
9365 aux->table[i].end.section = sym->st_shndx;
9366 aux->table[i].end.offset = sym->st_value + rp->r_addend;
9367 break;
9368 default:
9369 break;
9370 }
9371 }
9372
9373 free (rela);
9374 }
9375
9376 return true;
9377 }
9378
9379 static bool
9380 hppa_process_unwind (Filedata * filedata)
9381 {
9382 struct hppa_unw_aux_info aux;
9383 Elf_Internal_Shdr * unwsec = NULL;
9384 Elf_Internal_Shdr * sec;
9385 unsigned long i;
9386 bool res = true;
9387
9388 if (filedata->string_table == NULL)
9389 return false;
9390
9391 memset (& aux, 0, sizeof (aux));
9392
9393 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9394 {
9395 if (sec->sh_type == SHT_SYMTAB)
9396 {
9397 if (aux.symtab)
9398 {
9399 error (_("Multiple symbol tables encountered\n"));
9400 free (aux.symtab);
9401 aux.symtab = NULL;
9402 free (aux.strtab);
9403 aux.strtab = NULL;
9404 }
9405 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
9406 &aux.strtab, &aux.strtab_size))
9407 return false;
9408 }
9409 else if (section_name_valid (filedata, sec)
9410 && streq (section_name (filedata, sec), ".PARISC.unwind"))
9411 unwsec = sec;
9412 }
9413
9414 if (!unwsec)
9415 printf (_("\nThere are no unwind sections in this file.\n"));
9416
9417 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9418 {
9419 if (section_name_valid (filedata, sec)
9420 && streq (section_name (filedata, sec), ".PARISC.unwind"))
9421 {
9422 unsigned long num_unwind = sec->sh_size / 16;
9423
9424 printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
9425 "contains %lu entry:\n",
9426 "\nUnwind section '%s' at offset 0x%lx "
9427 "contains %lu entries:\n",
9428 num_unwind),
9429 printable_section_name (filedata, sec),
9430 (unsigned long) sec->sh_offset,
9431 num_unwind);
9432
9433 if (! slurp_hppa_unwind_table (filedata, &aux, sec))
9434 res = false;
9435
9436 if (res && aux.table_len > 0)
9437 {
9438 if (! dump_hppa_unwind (filedata, &aux))
9439 res = false;
9440 }
9441
9442 free ((char *) aux.table);
9443 aux.table = NULL;
9444 }
9445 }
9446
9447 free (aux.symtab);
9448 free ((char *) aux.strtab);
9449
9450 return res;
9451 }
9452
9453 struct arm_section
9454 {
9455 unsigned char * data; /* The unwind data. */
9456 Elf_Internal_Shdr * sec; /* The cached unwind section header. */
9457 Elf_Internal_Rela * rela; /* The cached relocations for this section. */
9458 unsigned long nrelas; /* The number of relocations. */
9459 unsigned int rel_type; /* REL or RELA ? */
9460 Elf_Internal_Rela * next_rela; /* Cyclic pointer to the next reloc to process. */
9461 };
9462
9463 struct arm_unw_aux_info
9464 {
9465 Filedata * filedata; /* The file containing the unwind sections. */
9466 Elf_Internal_Sym * symtab; /* The file's symbol table. */
9467 unsigned long nsyms; /* Number of symbols. */
9468 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
9469 unsigned long nfuns; /* Number of these symbols. */
9470 char * strtab; /* The file's string table. */
9471 unsigned long strtab_size; /* Size of string table. */
9472 };
9473
9474 static const char *
9475 arm_print_vma_and_name (Filedata * filedata,
9476 struct arm_unw_aux_info * aux,
9477 uint64_t fn,
9478 struct absaddr addr)
9479 {
9480 const char *procname;
9481 uint64_t sym_offset;
9482
9483 if (addr.section == SHN_UNDEF)
9484 addr.offset = fn;
9485
9486 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
9487 aux->strtab_size, addr, &procname,
9488 &sym_offset);
9489
9490 print_vma (fn, PREFIX_HEX);
9491
9492 if (procname)
9493 {
9494 fputs (" <", stdout);
9495 fputs (procname, stdout);
9496
9497 if (sym_offset)
9498 printf ("+0x%lx", (unsigned long) sym_offset);
9499 fputc ('>', stdout);
9500 }
9501
9502 return procname;
9503 }
9504
9505 static void
9506 arm_free_section (struct arm_section *arm_sec)
9507 {
9508 free (arm_sec->data);
9509 free (arm_sec->rela);
9510 }
9511
9512 /* 1) If SEC does not match the one cached in ARM_SEC, then free the current
9513 cached section and install SEC instead.
9514 2) Locate the 32-bit word at WORD_OFFSET in unwind section SEC
9515 and return its valued in * WORDP, relocating if necessary.
9516 3) Update the NEXT_RELA field in ARM_SEC and store the section index and
9517 relocation's offset in ADDR.
9518 4) If SYM_NAME is non-NULL and a relocation was applied, record the offset
9519 into the string table of the symbol associated with the reloc. If no
9520 reloc was applied store -1 there.
9521 5) Return TRUE upon success, FALSE otherwise. */
9522
9523 static bool
9524 get_unwind_section_word (Filedata * filedata,
9525 struct arm_unw_aux_info * aux,
9526 struct arm_section * arm_sec,
9527 Elf_Internal_Shdr * sec,
9528 uint64_t word_offset,
9529 unsigned int * wordp,
9530 struct absaddr * addr,
9531 uint64_t * sym_name)
9532 {
9533 Elf_Internal_Rela *rp;
9534 Elf_Internal_Sym *sym;
9535 const char * relname;
9536 unsigned int word;
9537 bool wrapped;
9538
9539 if (sec == NULL || arm_sec == NULL)
9540 return false;
9541
9542 addr->section = SHN_UNDEF;
9543 addr->offset = 0;
9544
9545 if (sym_name != NULL)
9546 *sym_name = (uint64_t) -1;
9547
9548 /* If necessary, update the section cache. */
9549 if (sec != arm_sec->sec)
9550 {
9551 Elf_Internal_Shdr *relsec;
9552
9553 arm_free_section (arm_sec);
9554
9555 arm_sec->sec = sec;
9556 arm_sec->data = get_data (NULL, aux->filedata, sec->sh_offset, 1,
9557 sec->sh_size, _("unwind data"));
9558 arm_sec->rela = NULL;
9559 arm_sec->nrelas = 0;
9560
9561 for (relsec = filedata->section_headers;
9562 relsec < filedata->section_headers + filedata->file_header.e_shnum;
9563 ++relsec)
9564 {
9565 if (relsec->sh_info >= filedata->file_header.e_shnum
9566 || filedata->section_headers + relsec->sh_info != sec
9567 /* PR 15745: Check the section type as well. */
9568 || (relsec->sh_type != SHT_REL
9569 && relsec->sh_type != SHT_RELA))
9570 continue;
9571
9572 arm_sec->rel_type = relsec->sh_type;
9573 if (relsec->sh_type == SHT_REL)
9574 {
9575 if (!slurp_rel_relocs (aux->filedata, relsec->sh_offset,
9576 relsec->sh_size,
9577 & arm_sec->rela, & arm_sec->nrelas))
9578 return false;
9579 }
9580 else /* relsec->sh_type == SHT_RELA */
9581 {
9582 if (!slurp_rela_relocs (aux->filedata, relsec->sh_offset,
9583 relsec->sh_size,
9584 & arm_sec->rela, & arm_sec->nrelas))
9585 return false;
9586 }
9587 break;
9588 }
9589
9590 arm_sec->next_rela = arm_sec->rela;
9591 }
9592
9593 /* If there is no unwind data we can do nothing. */
9594 if (arm_sec->data == NULL)
9595 return false;
9596
9597 /* If the offset is invalid then fail. */
9598 if (/* PR 21343 *//* PR 18879 */
9599 sec->sh_size < 4
9600 || word_offset > sec->sh_size - 4)
9601 return false;
9602
9603 /* Get the word at the required offset. */
9604 word = byte_get (arm_sec->data + word_offset, 4);
9605
9606 /* PR 17531: file: id:000001,src:001266+003044,op:splice,rep:128. */
9607 if (arm_sec->rela == NULL)
9608 {
9609 * wordp = word;
9610 return true;
9611 }
9612
9613 /* Look through the relocs to find the one that applies to the provided offset. */
9614 wrapped = false;
9615 for (rp = arm_sec->next_rela; rp != arm_sec->rela + arm_sec->nrelas; rp++)
9616 {
9617 uint64_t prelval, offset;
9618
9619 if (rp->r_offset > word_offset && !wrapped)
9620 {
9621 rp = arm_sec->rela;
9622 wrapped = true;
9623 }
9624 if (rp->r_offset > word_offset)
9625 break;
9626
9627 if (rp->r_offset & 3)
9628 {
9629 warn (_("Skipping unexpected relocation at offset 0x%lx\n"),
9630 (unsigned long) rp->r_offset);
9631 continue;
9632 }
9633
9634 if (rp->r_offset < word_offset)
9635 continue;
9636
9637 /* PR 17531: file: 027-161405-0.004 */
9638 if (aux->symtab == NULL)
9639 continue;
9640
9641 if (arm_sec->rel_type == SHT_REL)
9642 {
9643 offset = word & 0x7fffffff;
9644 if (offset & 0x40000000)
9645 offset |= ~ (uint64_t) 0x7fffffff;
9646 }
9647 else if (arm_sec->rel_type == SHT_RELA)
9648 offset = rp->r_addend;
9649 else
9650 {
9651 error (_("Unknown section relocation type %d encountered\n"),
9652 arm_sec->rel_type);
9653 break;
9654 }
9655
9656 /* PR 17531 file: 027-1241568-0.004. */
9657 if (ELF32_R_SYM (rp->r_info) >= aux->nsyms)
9658 {
9659 error (_("Bad symbol index in unwind relocation (%lu > %lu)\n"),
9660 (unsigned long) ELF32_R_SYM (rp->r_info), aux->nsyms);
9661 break;
9662 }
9663
9664 sym = aux->symtab + ELF32_R_SYM (rp->r_info);
9665 offset += sym->st_value;
9666 prelval = offset - (arm_sec->sec->sh_addr + rp->r_offset);
9667
9668 /* Check that we are processing the expected reloc type. */
9669 if (filedata->file_header.e_machine == EM_ARM)
9670 {
9671 relname = elf_arm_reloc_type (ELF32_R_TYPE (rp->r_info));
9672 if (relname == NULL)
9673 {
9674 warn (_("Skipping unknown ARM relocation type: %d\n"),
9675 (int) ELF32_R_TYPE (rp->r_info));
9676 continue;
9677 }
9678
9679 if (streq (relname, "R_ARM_NONE"))
9680 continue;
9681
9682 if (! streq (relname, "R_ARM_PREL31"))
9683 {
9684 warn (_("Skipping unexpected ARM relocation type %s\n"), relname);
9685 continue;
9686 }
9687 }
9688 else if (filedata->file_header.e_machine == EM_TI_C6000)
9689 {
9690 relname = elf_tic6x_reloc_type (ELF32_R_TYPE (rp->r_info));
9691 if (relname == NULL)
9692 {
9693 warn (_("Skipping unknown C6000 relocation type: %d\n"),
9694 (int) ELF32_R_TYPE (rp->r_info));
9695 continue;
9696 }
9697
9698 if (streq (relname, "R_C6000_NONE"))
9699 continue;
9700
9701 if (! streq (relname, "R_C6000_PREL31"))
9702 {
9703 warn (_("Skipping unexpected C6000 relocation type %s\n"), relname);
9704 continue;
9705 }
9706
9707 prelval >>= 1;
9708 }
9709 else
9710 {
9711 /* This function currently only supports ARM and TI unwinders. */
9712 warn (_("Only TI and ARM unwinders are currently supported\n"));
9713 break;
9714 }
9715
9716 word = (word & ~ (uint64_t) 0x7fffffff) | (prelval & 0x7fffffff);
9717 addr->section = sym->st_shndx;
9718 addr->offset = offset;
9719
9720 if (sym_name)
9721 * sym_name = sym->st_name;
9722 break;
9723 }
9724
9725 *wordp = word;
9726 arm_sec->next_rela = rp;
9727
9728 return true;
9729 }
9730
9731 static const char *tic6x_unwind_regnames[16] =
9732 {
9733 "A15", "B15", "B14", "B13", "B12", "B11", "B10", "B3",
9734 "A14", "A13", "A12", "A11", "A10",
9735 "[invalid reg 13]", "[invalid reg 14]", "[invalid reg 15]"
9736 };
9737
9738 static void
9739 decode_tic6x_unwind_regmask (unsigned int mask)
9740 {
9741 int i;
9742
9743 for (i = 12; mask; mask >>= 1, i--)
9744 {
9745 if (mask & 1)
9746 {
9747 fputs (tic6x_unwind_regnames[i], stdout);
9748 if (mask > 1)
9749 fputs (", ", stdout);
9750 }
9751 }
9752 }
9753
9754 #define ADVANCE \
9755 if (remaining == 0 && more_words) \
9756 { \
9757 data_offset += 4; \
9758 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, \
9759 data_offset, & word, & addr, NULL)) \
9760 return false; \
9761 remaining = 4; \
9762 more_words--; \
9763 } \
9764
9765 #define GET_OP(OP) \
9766 ADVANCE; \
9767 if (remaining) \
9768 { \
9769 remaining--; \
9770 (OP) = word >> 24; \
9771 word <<= 8; \
9772 } \
9773 else \
9774 { \
9775 printf (_("[Truncated opcode]\n")); \
9776 return false; \
9777 } \
9778 printf ("0x%02x ", OP)
9779
9780 static bool
9781 decode_arm_unwind_bytecode (Filedata * filedata,
9782 struct arm_unw_aux_info * aux,
9783 unsigned int word,
9784 unsigned int remaining,
9785 unsigned int more_words,
9786 uint64_t data_offset,
9787 Elf_Internal_Shdr * data_sec,
9788 struct arm_section * data_arm_sec)
9789 {
9790 struct absaddr addr;
9791 bool res = true;
9792
9793 /* Decode the unwinding instructions. */
9794 while (1)
9795 {
9796 unsigned int op, op2;
9797
9798 ADVANCE;
9799 if (remaining == 0)
9800 break;
9801 remaining--;
9802 op = word >> 24;
9803 word <<= 8;
9804
9805 printf (" 0x%02x ", op);
9806
9807 if ((op & 0xc0) == 0x00)
9808 {
9809 int offset = ((op & 0x3f) << 2) + 4;
9810
9811 printf (" vsp = vsp + %d", offset);
9812 }
9813 else if ((op & 0xc0) == 0x40)
9814 {
9815 int offset = ((op & 0x3f) << 2) + 4;
9816
9817 printf (" vsp = vsp - %d", offset);
9818 }
9819 else if ((op & 0xf0) == 0x80)
9820 {
9821 GET_OP (op2);
9822 if (op == 0x80 && op2 == 0)
9823 printf (_("Refuse to unwind"));
9824 else
9825 {
9826 unsigned int mask = ((op & 0x0f) << 8) | op2;
9827 bool first = true;
9828 int i;
9829
9830 printf ("pop {");
9831 for (i = 0; i < 12; i++)
9832 if (mask & (1 << i))
9833 {
9834 if (first)
9835 first = false;
9836 else
9837 printf (", ");
9838 printf ("r%d", 4 + i);
9839 }
9840 printf ("}");
9841 }
9842 }
9843 else if ((op & 0xf0) == 0x90)
9844 {
9845 if (op == 0x9d || op == 0x9f)
9846 printf (_(" [Reserved]"));
9847 else
9848 printf (" vsp = r%d", op & 0x0f);
9849 }
9850 else if ((op & 0xf0) == 0xa0)
9851 {
9852 int end = 4 + (op & 0x07);
9853 bool first = true;
9854 int i;
9855
9856 printf (" pop {");
9857 for (i = 4; i <= end; i++)
9858 {
9859 if (first)
9860 first = false;
9861 else
9862 printf (", ");
9863 printf ("r%d", i);
9864 }
9865 if (op & 0x08)
9866 {
9867 if (!first)
9868 printf (", ");
9869 printf ("r14");
9870 }
9871 printf ("}");
9872 }
9873 else if (op == 0xb0)
9874 printf (_(" finish"));
9875 else if (op == 0xb1)
9876 {
9877 GET_OP (op2);
9878 if (op2 == 0 || (op2 & 0xf0) != 0)
9879 printf (_("[Spare]"));
9880 else
9881 {
9882 unsigned int mask = op2 & 0x0f;
9883 bool first = true;
9884 int i;
9885
9886 printf ("pop {");
9887 for (i = 0; i < 12; i++)
9888 if (mask & (1 << i))
9889 {
9890 if (first)
9891 first = false;
9892 else
9893 printf (", ");
9894 printf ("r%d", i);
9895 }
9896 printf ("}");
9897 }
9898 }
9899 else if (op == 0xb2)
9900 {
9901 unsigned char buf[9];
9902 unsigned int i, len;
9903 unsigned long offset;
9904
9905 for (i = 0; i < sizeof (buf); i++)
9906 {
9907 GET_OP (buf[i]);
9908 if ((buf[i] & 0x80) == 0)
9909 break;
9910 }
9911 if (i == sizeof (buf))
9912 {
9913 error (_("corrupt change to vsp\n"));
9914 res = false;
9915 }
9916 else
9917 {
9918 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
9919 assert (len == i + 1);
9920 offset = offset * 4 + 0x204;
9921 printf ("vsp = vsp + %ld", offset);
9922 }
9923 }
9924 else if (op == 0xb3 || op == 0xc8 || op == 0xc9)
9925 {
9926 unsigned int first, last;
9927
9928 GET_OP (op2);
9929 first = op2 >> 4;
9930 last = op2 & 0x0f;
9931 if (op == 0xc8)
9932 first = first + 16;
9933 printf ("pop {D%d", first);
9934 if (last)
9935 printf ("-D%d", first + last);
9936 printf ("}");
9937 }
9938 else if (op == 0xb4)
9939 printf (_(" pop {ra_auth_code}"));
9940 else if (op == 0xb5)
9941 printf (_(" vsp as modifier for PAC validation"));
9942 else if ((op & 0xf8) == 0xb8 || (op & 0xf8) == 0xd0)
9943 {
9944 unsigned int count = op & 0x07;
9945
9946 printf ("pop {D8");
9947 if (count)
9948 printf ("-D%d", 8 + count);
9949 printf ("}");
9950 }
9951 else if (op >= 0xc0 && op <= 0xc5)
9952 {
9953 unsigned int count = op & 0x07;
9954
9955 printf (" pop {wR10");
9956 if (count)
9957 printf ("-wR%d", 10 + count);
9958 printf ("}");
9959 }
9960 else if (op == 0xc6)
9961 {
9962 unsigned int first, last;
9963
9964 GET_OP (op2);
9965 first = op2 >> 4;
9966 last = op2 & 0x0f;
9967 printf ("pop {wR%d", first);
9968 if (last)
9969 printf ("-wR%d", first + last);
9970 printf ("}");
9971 }
9972 else if (op == 0xc7)
9973 {
9974 GET_OP (op2);
9975 if (op2 == 0 || (op2 & 0xf0) != 0)
9976 printf (_("[Spare]"));
9977 else
9978 {
9979 unsigned int mask = op2 & 0x0f;
9980 bool first = true;
9981 int i;
9982
9983 printf ("pop {");
9984 for (i = 0; i < 4; i++)
9985 if (mask & (1 << i))
9986 {
9987 if (first)
9988 first = false;
9989 else
9990 printf (", ");
9991 printf ("wCGR%d", i);
9992 }
9993 printf ("}");
9994 }
9995 }
9996 else
9997 {
9998 printf (_(" [unsupported opcode]"));
9999 res = false;
10000 }
10001
10002 printf ("\n");
10003 }
10004
10005 return res;
10006 }
10007
10008 static bool
10009 decode_tic6x_unwind_bytecode (Filedata * filedata,
10010 struct arm_unw_aux_info * aux,
10011 unsigned int word,
10012 unsigned int remaining,
10013 unsigned int more_words,
10014 uint64_t data_offset,
10015 Elf_Internal_Shdr * data_sec,
10016 struct arm_section * data_arm_sec)
10017 {
10018 struct absaddr addr;
10019
10020 /* Decode the unwinding instructions. */
10021 while (1)
10022 {
10023 unsigned int op, op2;
10024
10025 ADVANCE;
10026 if (remaining == 0)
10027 break;
10028 remaining--;
10029 op = word >> 24;
10030 word <<= 8;
10031
10032 printf (" 0x%02x ", op);
10033
10034 if ((op & 0xc0) == 0x00)
10035 {
10036 int offset = ((op & 0x3f) << 3) + 8;
10037 printf (" sp = sp + %d", offset);
10038 }
10039 else if ((op & 0xc0) == 0x80)
10040 {
10041 GET_OP (op2);
10042 if (op == 0x80 && op2 == 0)
10043 printf (_("Refuse to unwind"));
10044 else
10045 {
10046 unsigned int mask = ((op & 0x1f) << 8) | op2;
10047 if (op & 0x20)
10048 printf ("pop compact {");
10049 else
10050 printf ("pop {");
10051
10052 decode_tic6x_unwind_regmask (mask);
10053 printf("}");
10054 }
10055 }
10056 else if ((op & 0xf0) == 0xc0)
10057 {
10058 unsigned int reg;
10059 unsigned int nregs;
10060 unsigned int i;
10061 const char *name;
10062 struct
10063 {
10064 unsigned int offset;
10065 unsigned int reg;
10066 } regpos[16];
10067
10068 /* Scan entire instruction first so that GET_OP output is not
10069 interleaved with disassembly. */
10070 nregs = 0;
10071 for (i = 0; nregs < (op & 0xf); i++)
10072 {
10073 GET_OP (op2);
10074 reg = op2 >> 4;
10075 if (reg != 0xf)
10076 {
10077 regpos[nregs].offset = i * 2;
10078 regpos[nregs].reg = reg;
10079 nregs++;
10080 }
10081
10082 reg = op2 & 0xf;
10083 if (reg != 0xf)
10084 {
10085 regpos[nregs].offset = i * 2 + 1;
10086 regpos[nregs].reg = reg;
10087 nregs++;
10088 }
10089 }
10090
10091 printf (_("pop frame {"));
10092 if (nregs == 0)
10093 {
10094 printf (_("*corrupt* - no registers specified"));
10095 }
10096 else
10097 {
10098 reg = nregs - 1;
10099 for (i = i * 2; i > 0; i--)
10100 {
10101 if (regpos[reg].offset == i - 1)
10102 {
10103 name = tic6x_unwind_regnames[regpos[reg].reg];
10104 if (reg > 0)
10105 reg--;
10106 }
10107 else
10108 name = _("[pad]");
10109
10110 fputs (name, stdout);
10111 if (i > 1)
10112 printf (", ");
10113 }
10114 }
10115
10116 printf ("}");
10117 }
10118 else if (op == 0xd0)
10119 printf (" MOV FP, SP");
10120 else if (op == 0xd1)
10121 printf (" __c6xabi_pop_rts");
10122 else if (op == 0xd2)
10123 {
10124 unsigned char buf[9];
10125 unsigned int i, len;
10126 unsigned long offset;
10127
10128 for (i = 0; i < sizeof (buf); i++)
10129 {
10130 GET_OP (buf[i]);
10131 if ((buf[i] & 0x80) == 0)
10132 break;
10133 }
10134 /* PR 17531: file: id:000001,src:001906+004739,op:splice,rep:2. */
10135 if (i == sizeof (buf))
10136 {
10137 warn (_("Corrupt stack pointer adjustment detected\n"));
10138 return false;
10139 }
10140
10141 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
10142 assert (len == i + 1);
10143 offset = offset * 8 + 0x408;
10144 printf (_("sp = sp + %ld"), offset);
10145 }
10146 else if ((op & 0xf0) == 0xe0)
10147 {
10148 if ((op & 0x0f) == 7)
10149 printf (" RETURN");
10150 else
10151 printf (" MV %s, B3", tic6x_unwind_regnames[op & 0x0f]);
10152 }
10153 else
10154 {
10155 printf (_(" [unsupported opcode]"));
10156 }
10157 putchar ('\n');
10158 }
10159
10160 return true;
10161 }
10162
10163 static uint64_t
10164 arm_expand_prel31 (Filedata * filedata, uint64_t word, uint64_t where)
10165 {
10166 uint64_t offset;
10167
10168 offset = word & 0x7fffffff;
10169 if (offset & 0x40000000)
10170 offset |= ~ (uint64_t) 0x7fffffff;
10171
10172 if (filedata->file_header.e_machine == EM_TI_C6000)
10173 offset <<= 1;
10174
10175 return offset + where;
10176 }
10177
10178 static bool
10179 decode_arm_unwind (Filedata * filedata,
10180 struct arm_unw_aux_info * aux,
10181 unsigned int word,
10182 unsigned int remaining,
10183 uint64_t data_offset,
10184 Elf_Internal_Shdr * data_sec,
10185 struct arm_section * data_arm_sec)
10186 {
10187 int per_index;
10188 unsigned int more_words = 0;
10189 struct absaddr addr;
10190 uint64_t sym_name = (uint64_t) -1;
10191 bool res = true;
10192
10193 if (remaining == 0)
10194 {
10195 /* Fetch the first word.
10196 Note - when decoding an object file the address extracted
10197 here will always be 0. So we also pass in the sym_name
10198 parameter so that we can find the symbol associated with
10199 the personality routine. */
10200 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, data_offset,
10201 & word, & addr, & sym_name))
10202 return false;
10203
10204 remaining = 4;
10205 }
10206 else
10207 {
10208 addr.section = SHN_UNDEF;
10209 addr.offset = 0;
10210 }
10211
10212 if ((word & 0x80000000) == 0)
10213 {
10214 /* Expand prel31 for personality routine. */
10215 uint64_t fn;
10216 const char *procname;
10217
10218 fn = arm_expand_prel31 (filedata, word, data_sec->sh_addr + data_offset);
10219 printf (_(" Personality routine: "));
10220 if (fn == 0
10221 && addr.section == SHN_UNDEF && addr.offset == 0
10222 && sym_name != (uint64_t) -1 && sym_name < aux->strtab_size)
10223 {
10224 procname = aux->strtab + sym_name;
10225 print_vma (fn, PREFIX_HEX);
10226 if (procname)
10227 {
10228 fputs (" <", stdout);
10229 fputs (procname, stdout);
10230 fputc ('>', stdout);
10231 }
10232 }
10233 else
10234 procname = arm_print_vma_and_name (filedata, aux, fn, addr);
10235 fputc ('\n', stdout);
10236
10237 /* The GCC personality routines use the standard compact
10238 encoding, starting with one byte giving the number of
10239 words. */
10240 if (procname != NULL
10241 && (startswith (procname, "__gcc_personality_v0")
10242 || startswith (procname, "__gxx_personality_v0")
10243 || startswith (procname, "__gcj_personality_v0")
10244 || startswith (procname, "__gnu_objc_personality_v0")))
10245 {
10246 remaining = 0;
10247 more_words = 1;
10248 ADVANCE;
10249 if (!remaining)
10250 {
10251 printf (_(" [Truncated data]\n"));
10252 return false;
10253 }
10254 more_words = word >> 24;
10255 word <<= 8;
10256 remaining--;
10257 per_index = -1;
10258 }
10259 else
10260 return true;
10261 }
10262 else
10263 {
10264 /* ARM EHABI Section 6.3:
10265
10266 An exception-handling table entry for the compact model looks like:
10267
10268 31 30-28 27-24 23-0
10269 -- ----- ----- ----
10270 1 0 index Data for personalityRoutine[index] */
10271
10272 if (filedata->file_header.e_machine == EM_ARM
10273 && (word & 0x70000000))
10274 {
10275 warn (_("Corrupt ARM compact model table entry: %x \n"), word);
10276 res = false;
10277 }
10278
10279 per_index = (word >> 24) & 0x7f;
10280 printf (_(" Compact model index: %d\n"), per_index);
10281 if (per_index == 0)
10282 {
10283 more_words = 0;
10284 word <<= 8;
10285 remaining--;
10286 }
10287 else if (per_index < 3)
10288 {
10289 more_words = (word >> 16) & 0xff;
10290 word <<= 16;
10291 remaining -= 2;
10292 }
10293 }
10294
10295 switch (filedata->file_header.e_machine)
10296 {
10297 case EM_ARM:
10298 if (per_index < 3)
10299 {
10300 if (! decode_arm_unwind_bytecode (filedata, aux, word, remaining, more_words,
10301 data_offset, data_sec, data_arm_sec))
10302 res = false;
10303 }
10304 else
10305 {
10306 warn (_("Unknown ARM compact model index encountered\n"));
10307 printf (_(" [reserved]\n"));
10308 res = false;
10309 }
10310 break;
10311
10312 case EM_TI_C6000:
10313 if (per_index < 3)
10314 {
10315 if (! decode_tic6x_unwind_bytecode (filedata, aux, word, remaining, more_words,
10316 data_offset, data_sec, data_arm_sec))
10317 res = false;
10318 }
10319 else if (per_index < 5)
10320 {
10321 if (((word >> 17) & 0x7f) == 0x7f)
10322 printf (_(" Restore stack from frame pointer\n"));
10323 else
10324 printf (_(" Stack increment %d\n"), (word >> 14) & 0x1fc);
10325 printf (_(" Registers restored: "));
10326 if (per_index == 4)
10327 printf (" (compact) ");
10328 decode_tic6x_unwind_regmask ((word >> 4) & 0x1fff);
10329 putchar ('\n');
10330 printf (_(" Return register: %s\n"),
10331 tic6x_unwind_regnames[word & 0xf]);
10332 }
10333 else
10334 printf (_(" [reserved (%d)]\n"), per_index);
10335 break;
10336
10337 default:
10338 error (_("Unsupported architecture type %d encountered when decoding unwind table\n"),
10339 filedata->file_header.e_machine);
10340 res = false;
10341 }
10342
10343 /* Decode the descriptors. Not implemented. */
10344
10345 return res;
10346 }
10347
10348 static bool
10349 dump_arm_unwind (Filedata * filedata,
10350 struct arm_unw_aux_info * aux,
10351 Elf_Internal_Shdr * exidx_sec)
10352 {
10353 struct arm_section exidx_arm_sec, extab_arm_sec;
10354 unsigned int i, exidx_len;
10355 unsigned long j, nfuns;
10356 bool res = true;
10357
10358 memset (&exidx_arm_sec, 0, sizeof (exidx_arm_sec));
10359 memset (&extab_arm_sec, 0, sizeof (extab_arm_sec));
10360 exidx_len = exidx_sec->sh_size / 8;
10361
10362 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
10363 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
10364 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
10365 aux->funtab[nfuns++] = aux->symtab[j];
10366 aux->nfuns = nfuns;
10367 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
10368
10369 for (i = 0; i < exidx_len; i++)
10370 {
10371 unsigned int exidx_fn, exidx_entry;
10372 struct absaddr fn_addr, entry_addr;
10373 uint64_t fn;
10374
10375 fputc ('\n', stdout);
10376
10377 if (! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
10378 8 * i, & exidx_fn, & fn_addr, NULL)
10379 || ! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
10380 8 * i + 4, & exidx_entry, & entry_addr, NULL))
10381 {
10382 free (aux->funtab);
10383 arm_free_section (& exidx_arm_sec);
10384 arm_free_section (& extab_arm_sec);
10385 return false;
10386 }
10387
10388 /* ARM EHABI, Section 5:
10389 An index table entry consists of 2 words.
10390 The first word contains a prel31 offset to the start of a function, with bit 31 clear. */
10391 if (exidx_fn & 0x80000000)
10392 {
10393 warn (_("corrupt index table entry: %x\n"), exidx_fn);
10394 res = false;
10395 }
10396
10397 fn = arm_expand_prel31 (filedata, exidx_fn, exidx_sec->sh_addr + 8 * i);
10398
10399 arm_print_vma_and_name (filedata, aux, fn, fn_addr);
10400 fputs (": ", stdout);
10401
10402 if (exidx_entry == 1)
10403 {
10404 print_vma (exidx_entry, PREFIX_HEX);
10405 fputs (" [cantunwind]\n", stdout);
10406 }
10407 else if (exidx_entry & 0x80000000)
10408 {
10409 print_vma (exidx_entry, PREFIX_HEX);
10410 fputc ('\n', stdout);
10411 decode_arm_unwind (filedata, aux, exidx_entry, 4, 0, NULL, NULL);
10412 }
10413 else
10414 {
10415 uint64_t table, table_offset = 0;
10416 Elf_Internal_Shdr *table_sec;
10417
10418 fputs ("@", stdout);
10419 table = arm_expand_prel31 (filedata, exidx_entry, exidx_sec->sh_addr + 8 * i + 4);
10420 print_vma (table, PREFIX_HEX);
10421 printf ("\n");
10422
10423 /* Locate the matching .ARM.extab. */
10424 if (entry_addr.section != SHN_UNDEF
10425 && entry_addr.section < filedata->file_header.e_shnum)
10426 {
10427 table_sec = filedata->section_headers + entry_addr.section;
10428 table_offset = entry_addr.offset;
10429 /* PR 18879 */
10430 if (table_offset > table_sec->sh_size)
10431 {
10432 warn (_("Unwind entry contains corrupt offset (0x%lx) into section %s\n"),
10433 (unsigned long) table_offset,
10434 printable_section_name (filedata, table_sec));
10435 res = false;
10436 continue;
10437 }
10438 }
10439 else
10440 {
10441 table_sec = find_section_by_address (filedata, table);
10442 if (table_sec != NULL)
10443 table_offset = table - table_sec->sh_addr;
10444 }
10445
10446 if (table_sec == NULL)
10447 {
10448 warn (_("Could not locate .ARM.extab section containing 0x%lx.\n"),
10449 (unsigned long) table);
10450 res = false;
10451 continue;
10452 }
10453
10454 if (! decode_arm_unwind (filedata, aux, 0, 0, table_offset, table_sec,
10455 &extab_arm_sec))
10456 res = false;
10457 }
10458 }
10459
10460 printf ("\n");
10461
10462 free (aux->funtab);
10463 arm_free_section (&exidx_arm_sec);
10464 arm_free_section (&extab_arm_sec);
10465
10466 return res;
10467 }
10468
10469 /* Used for both ARM and C6X unwinding tables. */
10470
10471 static bool
10472 arm_process_unwind (Filedata * filedata)
10473 {
10474 struct arm_unw_aux_info aux;
10475 Elf_Internal_Shdr *unwsec = NULL;
10476 Elf_Internal_Shdr *sec;
10477 unsigned long i;
10478 unsigned int sec_type;
10479 bool res = true;
10480
10481 switch (filedata->file_header.e_machine)
10482 {
10483 case EM_ARM:
10484 sec_type = SHT_ARM_EXIDX;
10485 break;
10486
10487 case EM_TI_C6000:
10488 sec_type = SHT_C6000_UNWIND;
10489 break;
10490
10491 default:
10492 error (_("Unsupported architecture type %d encountered when processing unwind table\n"),
10493 filedata->file_header.e_machine);
10494 return false;
10495 }
10496
10497 if (filedata->string_table == NULL)
10498 return false;
10499
10500 memset (& aux, 0, sizeof (aux));
10501 aux.filedata = filedata;
10502
10503 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
10504 {
10505 if (sec->sh_type == SHT_SYMTAB)
10506 {
10507 if (aux.symtab)
10508 {
10509 error (_("Multiple symbol tables encountered\n"));
10510 free (aux.symtab);
10511 aux.symtab = NULL;
10512 free (aux.strtab);
10513 aux.strtab = NULL;
10514 }
10515 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
10516 &aux.strtab, &aux.strtab_size))
10517 return false;
10518 }
10519 else if (sec->sh_type == sec_type)
10520 unwsec = sec;
10521 }
10522
10523 if (unwsec == NULL)
10524 printf (_("\nThere are no unwind sections in this file.\n"));
10525 else
10526 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
10527 {
10528 if (sec->sh_type == sec_type)
10529 {
10530 unsigned long num_unwind = sec->sh_size / (2 * eh_addr_size);
10531 printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
10532 "contains %lu entry:\n",
10533 "\nUnwind section '%s' at offset 0x%lx "
10534 "contains %lu entries:\n",
10535 num_unwind),
10536 printable_section_name (filedata, sec),
10537 (unsigned long) sec->sh_offset,
10538 num_unwind);
10539
10540 if (! dump_arm_unwind (filedata, &aux, sec))
10541 res = false;
10542 }
10543 }
10544
10545 free (aux.symtab);
10546 free ((char *) aux.strtab);
10547
10548 return res;
10549 }
10550
10551 static bool
10552 no_processor_specific_unwind (Filedata * filedata ATTRIBUTE_UNUSED)
10553 {
10554 printf (_("No processor specific unwind information to decode\n"));
10555 return true;
10556 }
10557
10558 static bool
10559 process_unwind (Filedata * filedata)
10560 {
10561 struct unwind_handler
10562 {
10563 unsigned int machtype;
10564 bool (* handler)(Filedata *);
10565 } handlers[] =
10566 {
10567 { EM_ARM, arm_process_unwind },
10568 { EM_IA_64, ia64_process_unwind },
10569 { EM_PARISC, hppa_process_unwind },
10570 { EM_TI_C6000, arm_process_unwind },
10571 { EM_386, no_processor_specific_unwind },
10572 { EM_X86_64, no_processor_specific_unwind },
10573 { 0, NULL }
10574 };
10575 int i;
10576
10577 if (!do_unwind)
10578 return true;
10579
10580 for (i = 0; handlers[i].handler != NULL; i++)
10581 if (filedata->file_header.e_machine == handlers[i].machtype)
10582 return handlers[i].handler (filedata);
10583
10584 printf (_("\nThe decoding of unwind sections for machine type %s is not currently supported.\n"),
10585 get_machine_name (filedata->file_header.e_machine));
10586 return true;
10587 }
10588
10589 static void
10590 dynamic_section_aarch64_val (Elf_Internal_Dyn * entry)
10591 {
10592 switch (entry->d_tag)
10593 {
10594 case DT_AARCH64_BTI_PLT:
10595 case DT_AARCH64_PAC_PLT:
10596 break;
10597 default:
10598 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10599 break;
10600 }
10601 putchar ('\n');
10602 }
10603
10604 static void
10605 dynamic_section_mips_val (Filedata * filedata, Elf_Internal_Dyn * entry)
10606 {
10607 switch (entry->d_tag)
10608 {
10609 case DT_MIPS_FLAGS:
10610 if (entry->d_un.d_val == 0)
10611 printf (_("NONE"));
10612 else
10613 {
10614 static const char * opts[] =
10615 {
10616 "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
10617 "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
10618 "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
10619 "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
10620 "RLD_ORDER_SAFE"
10621 };
10622 unsigned int cnt;
10623 bool first = true;
10624
10625 for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
10626 if (entry->d_un.d_val & (1 << cnt))
10627 {
10628 printf ("%s%s", first ? "" : " ", opts[cnt]);
10629 first = false;
10630 }
10631 }
10632 break;
10633
10634 case DT_MIPS_IVERSION:
10635 if (valid_dynamic_name (filedata, entry->d_un.d_val))
10636 printf (_("Interface Version: %s"),
10637 get_dynamic_name (filedata, entry->d_un.d_val));
10638 else
10639 printf (_("Interface Version: <corrupt: %" PRIx64 ">"),
10640 entry->d_un.d_ptr);
10641 break;
10642
10643 case DT_MIPS_TIME_STAMP:
10644 {
10645 char timebuf[128];
10646 struct tm * tmp;
10647 time_t atime = entry->d_un.d_val;
10648
10649 tmp = gmtime (&atime);
10650 /* PR 17531: file: 6accc532. */
10651 if (tmp == NULL)
10652 snprintf (timebuf, sizeof (timebuf), _("<corrupt>"));
10653 else
10654 snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
10655 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
10656 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
10657 printf (_("Time Stamp: %s"), timebuf);
10658 }
10659 break;
10660
10661 case DT_MIPS_RLD_VERSION:
10662 case DT_MIPS_LOCAL_GOTNO:
10663 case DT_MIPS_CONFLICTNO:
10664 case DT_MIPS_LIBLISTNO:
10665 case DT_MIPS_SYMTABNO:
10666 case DT_MIPS_UNREFEXTNO:
10667 case DT_MIPS_HIPAGENO:
10668 case DT_MIPS_DELTA_CLASS_NO:
10669 case DT_MIPS_DELTA_INSTANCE_NO:
10670 case DT_MIPS_DELTA_RELOC_NO:
10671 case DT_MIPS_DELTA_SYM_NO:
10672 case DT_MIPS_DELTA_CLASSSYM_NO:
10673 case DT_MIPS_COMPACT_SIZE:
10674 print_vma (entry->d_un.d_val, DEC);
10675 break;
10676
10677 case DT_MIPS_XHASH:
10678 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
10679 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10680 /* Falls through. */
10681
10682 default:
10683 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10684 }
10685 putchar ('\n');
10686 }
10687
10688 static void
10689 dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
10690 {
10691 switch (entry->d_tag)
10692 {
10693 case DT_HP_DLD_FLAGS:
10694 {
10695 static struct
10696 {
10697 long int bit;
10698 const char * str;
10699 }
10700 flags[] =
10701 {
10702 { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
10703 { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
10704 { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
10705 { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
10706 { DT_HP_BIND_NOW, "HP_BIND_NOW" },
10707 { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
10708 { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
10709 { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
10710 { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
10711 { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
10712 { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
10713 { DT_HP_GST, "HP_GST" },
10714 { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
10715 { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
10716 { DT_HP_NODELETE, "HP_NODELETE" },
10717 { DT_HP_GROUP, "HP_GROUP" },
10718 { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
10719 };
10720 bool first = true;
10721 size_t cnt;
10722 uint64_t val = entry->d_un.d_val;
10723
10724 for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
10725 if (val & flags[cnt].bit)
10726 {
10727 if (! first)
10728 putchar (' ');
10729 fputs (flags[cnt].str, stdout);
10730 first = false;
10731 val ^= flags[cnt].bit;
10732 }
10733
10734 if (val != 0 || first)
10735 {
10736 if (! first)
10737 putchar (' ');
10738 print_vma (val, HEX);
10739 }
10740 }
10741 break;
10742
10743 default:
10744 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10745 break;
10746 }
10747 putchar ('\n');
10748 }
10749
10750 /* VMS vs Unix time offset and factor. */
10751
10752 #define VMS_EPOCH_OFFSET 35067168000000000LL
10753 #define VMS_GRANULARITY_FACTOR 10000000
10754 #ifndef INT64_MIN
10755 #define INT64_MIN (-9223372036854775807LL - 1)
10756 #endif
10757
10758 /* Display a VMS time in a human readable format. */
10759
10760 static void
10761 print_vms_time (int64_t vmstime)
10762 {
10763 struct tm *tm = NULL;
10764 time_t unxtime;
10765
10766 if (vmstime >= INT64_MIN + VMS_EPOCH_OFFSET)
10767 {
10768 vmstime = (vmstime - VMS_EPOCH_OFFSET) / VMS_GRANULARITY_FACTOR;
10769 unxtime = vmstime;
10770 if (unxtime == vmstime)
10771 tm = gmtime (&unxtime);
10772 }
10773 if (tm != NULL)
10774 printf ("%04u-%02u-%02uT%02u:%02u:%02u",
10775 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
10776 tm->tm_hour, tm->tm_min, tm->tm_sec);
10777 }
10778
10779 static void
10780 dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
10781 {
10782 switch (entry->d_tag)
10783 {
10784 case DT_IA_64_PLT_RESERVE:
10785 /* First 3 slots reserved. */
10786 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10787 printf (" -- ");
10788 print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
10789 break;
10790
10791 case DT_IA_64_VMS_LINKTIME:
10792 print_vms_time (entry->d_un.d_val);
10793 break;
10794
10795 case DT_IA_64_VMS_LNKFLAGS:
10796 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10797 if (entry->d_un.d_val & VMS_LF_CALL_DEBUG)
10798 printf (" CALL_DEBUG");
10799 if (entry->d_un.d_val & VMS_LF_NOP0BUFS)
10800 printf (" NOP0BUFS");
10801 if (entry->d_un.d_val & VMS_LF_P0IMAGE)
10802 printf (" P0IMAGE");
10803 if (entry->d_un.d_val & VMS_LF_MKTHREADS)
10804 printf (" MKTHREADS");
10805 if (entry->d_un.d_val & VMS_LF_UPCALLS)
10806 printf (" UPCALLS");
10807 if (entry->d_un.d_val & VMS_LF_IMGSTA)
10808 printf (" IMGSTA");
10809 if (entry->d_un.d_val & VMS_LF_INITIALIZE)
10810 printf (" INITIALIZE");
10811 if (entry->d_un.d_val & VMS_LF_MAIN)
10812 printf (" MAIN");
10813 if (entry->d_un.d_val & VMS_LF_EXE_INIT)
10814 printf (" EXE_INIT");
10815 if (entry->d_un.d_val & VMS_LF_TBK_IN_IMG)
10816 printf (" TBK_IN_IMG");
10817 if (entry->d_un.d_val & VMS_LF_DBG_IN_IMG)
10818 printf (" DBG_IN_IMG");
10819 if (entry->d_un.d_val & VMS_LF_TBK_IN_DSF)
10820 printf (" TBK_IN_DSF");
10821 if (entry->d_un.d_val & VMS_LF_DBG_IN_DSF)
10822 printf (" DBG_IN_DSF");
10823 if (entry->d_un.d_val & VMS_LF_SIGNATURES)
10824 printf (" SIGNATURES");
10825 if (entry->d_un.d_val & VMS_LF_REL_SEG_OFF)
10826 printf (" REL_SEG_OFF");
10827 break;
10828
10829 default:
10830 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10831 break;
10832 }
10833 putchar ('\n');
10834 }
10835
10836 static bool
10837 get_32bit_dynamic_section (Filedata * filedata)
10838 {
10839 Elf32_External_Dyn * edyn;
10840 Elf32_External_Dyn * ext;
10841 Elf_Internal_Dyn * entry;
10842
10843 edyn = (Elf32_External_Dyn *) get_data (NULL, filedata,
10844 filedata->dynamic_addr, 1,
10845 filedata->dynamic_size,
10846 _("dynamic section"));
10847 if (!edyn)
10848 return false;
10849
10850 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
10851 might not have the luxury of section headers. Look for the DT_NULL
10852 terminator to determine the number of entries. */
10853 for (ext = edyn, filedata->dynamic_nent = 0;
10854 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
10855 ext++)
10856 {
10857 filedata->dynamic_nent++;
10858 if (BYTE_GET (ext->d_tag) == DT_NULL)
10859 break;
10860 }
10861
10862 filedata->dynamic_section
10863 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
10864 if (filedata->dynamic_section == NULL)
10865 {
10866 error (_("Out of memory allocating space for %lu dynamic entries\n"),
10867 (unsigned long) filedata->dynamic_nent);
10868 free (edyn);
10869 return false;
10870 }
10871
10872 for (ext = edyn, entry = filedata->dynamic_section;
10873 entry < filedata->dynamic_section + filedata->dynamic_nent;
10874 ext++, entry++)
10875 {
10876 entry->d_tag = BYTE_GET (ext->d_tag);
10877 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
10878 }
10879
10880 free (edyn);
10881
10882 return true;
10883 }
10884
10885 static bool
10886 get_64bit_dynamic_section (Filedata * filedata)
10887 {
10888 Elf64_External_Dyn * edyn;
10889 Elf64_External_Dyn * ext;
10890 Elf_Internal_Dyn * entry;
10891
10892 /* Read in the data. */
10893 edyn = (Elf64_External_Dyn *) get_data (NULL, filedata,
10894 filedata->dynamic_addr, 1,
10895 filedata->dynamic_size,
10896 _("dynamic section"));
10897 if (!edyn)
10898 return false;
10899
10900 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
10901 might not have the luxury of section headers. Look for the DT_NULL
10902 terminator to determine the number of entries. */
10903 for (ext = edyn, filedata->dynamic_nent = 0;
10904 /* PR 17533 file: 033-67080-0.004 - do not read past end of buffer. */
10905 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
10906 ext++)
10907 {
10908 filedata->dynamic_nent++;
10909 if (BYTE_GET (ext->d_tag) == DT_NULL)
10910 break;
10911 }
10912
10913 filedata->dynamic_section
10914 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
10915 if (filedata->dynamic_section == NULL)
10916 {
10917 error (_("Out of memory allocating space for %lu dynamic entries\n"),
10918 (unsigned long) filedata->dynamic_nent);
10919 free (edyn);
10920 return false;
10921 }
10922
10923 /* Convert from external to internal formats. */
10924 for (ext = edyn, entry = filedata->dynamic_section;
10925 entry < filedata->dynamic_section + filedata->dynamic_nent;
10926 ext++, entry++)
10927 {
10928 entry->d_tag = BYTE_GET (ext->d_tag);
10929 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
10930 }
10931
10932 free (edyn);
10933
10934 return true;
10935 }
10936
10937 static bool
10938 get_dynamic_section (Filedata *filedata)
10939 {
10940 if (filedata->dynamic_section)
10941 return true;
10942
10943 if (is_32bit_elf)
10944 return get_32bit_dynamic_section (filedata);
10945 else
10946 return get_64bit_dynamic_section (filedata);
10947 }
10948
10949 static void
10950 print_dynamic_flags (uint64_t flags)
10951 {
10952 bool first = true;
10953
10954 while (flags)
10955 {
10956 uint64_t flag;
10957
10958 flag = flags & - flags;
10959 flags &= ~ flag;
10960
10961 if (first)
10962 first = false;
10963 else
10964 putc (' ', stdout);
10965
10966 switch (flag)
10967 {
10968 case DF_ORIGIN: fputs ("ORIGIN", stdout); break;
10969 case DF_SYMBOLIC: fputs ("SYMBOLIC", stdout); break;
10970 case DF_TEXTREL: fputs ("TEXTREL", stdout); break;
10971 case DF_BIND_NOW: fputs ("BIND_NOW", stdout); break;
10972 case DF_STATIC_TLS: fputs ("STATIC_TLS", stdout); break;
10973 default: fputs (_("unknown"), stdout); break;
10974 }
10975 }
10976 puts ("");
10977 }
10978
10979 static uint64_t *
10980 get_dynamic_data (Filedata * filedata, uint64_t number, unsigned int ent_size)
10981 {
10982 unsigned char * e_data;
10983 uint64_t * i_data;
10984
10985 /* If size_t is smaller than uint64_t, eg because you are building
10986 on a 32-bit host, then make sure that when number is cast to
10987 size_t no information is lost. */
10988 if ((size_t) number != number
10989 || ent_size * number / ent_size != number)
10990 {
10991 error (_("Size overflow prevents reading %" PRIu64
10992 " elements of size %u\n"),
10993 number, ent_size);
10994 return NULL;
10995 }
10996
10997 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
10998 attempting to allocate memory when the read is bound to fail. */
10999 if (ent_size * number > filedata->file_size)
11000 {
11001 error (_("Invalid number of dynamic entries: %" PRIu64 "\n"),
11002 number);
11003 return NULL;
11004 }
11005
11006 e_data = (unsigned char *) cmalloc ((size_t) number, ent_size);
11007 if (e_data == NULL)
11008 {
11009 error (_("Out of memory reading %" PRIu64 " dynamic entries\n"),
11010 number);
11011 return NULL;
11012 }
11013
11014 if (fread (e_data, ent_size, (size_t) number, filedata->handle) != number)
11015 {
11016 error (_("Unable to read in %" PRIu64 " bytes of dynamic data\n"),
11017 number * ent_size);
11018 free (e_data);
11019 return NULL;
11020 }
11021
11022 i_data = (uint64_t *) cmalloc ((size_t) number, sizeof (*i_data));
11023 if (i_data == NULL)
11024 {
11025 error (_("Out of memory allocating space for %" PRIu64 " dynamic entries\n"),
11026 number);
11027 free (e_data);
11028 return NULL;
11029 }
11030
11031 while (number--)
11032 i_data[number] = byte_get (e_data + number * ent_size, ent_size);
11033
11034 free (e_data);
11035
11036 return i_data;
11037 }
11038
11039 static unsigned long
11040 get_num_dynamic_syms (Filedata * filedata)
11041 {
11042 unsigned long num_of_syms = 0;
11043
11044 if (!do_histogram && (!do_using_dynamic || do_dyn_syms))
11045 return num_of_syms;
11046
11047 if (filedata->dynamic_info[DT_HASH])
11048 {
11049 unsigned char nb[8];
11050 unsigned char nc[8];
11051 unsigned int hash_ent_size = 4;
11052
11053 if ((filedata->file_header.e_machine == EM_ALPHA
11054 || filedata->file_header.e_machine == EM_S390
11055 || filedata->file_header.e_machine == EM_S390_OLD)
11056 && filedata->file_header.e_ident[EI_CLASS] == ELFCLASS64)
11057 hash_ent_size = 8;
11058
11059 if (fseek (filedata->handle,
11060 (filedata->archive_file_offset
11061 + offset_from_vma (filedata, filedata->dynamic_info[DT_HASH],
11062 sizeof nb + sizeof nc)),
11063 SEEK_SET))
11064 {
11065 error (_("Unable to seek to start of dynamic information\n"));
11066 goto no_hash;
11067 }
11068
11069 if (fread (nb, hash_ent_size, 1, filedata->handle) != 1)
11070 {
11071 error (_("Failed to read in number of buckets\n"));
11072 goto no_hash;
11073 }
11074
11075 if (fread (nc, hash_ent_size, 1, filedata->handle) != 1)
11076 {
11077 error (_("Failed to read in number of chains\n"));
11078 goto no_hash;
11079 }
11080
11081 filedata->nbuckets = byte_get (nb, hash_ent_size);
11082 filedata->nchains = byte_get (nc, hash_ent_size);
11083
11084 if (filedata->nbuckets != 0 && filedata->nchains != 0)
11085 {
11086 filedata->buckets = get_dynamic_data (filedata, filedata->nbuckets,
11087 hash_ent_size);
11088 filedata->chains = get_dynamic_data (filedata, filedata->nchains,
11089 hash_ent_size);
11090
11091 if (filedata->buckets != NULL && filedata->chains != NULL)
11092 num_of_syms = filedata->nchains;
11093 }
11094 no_hash:
11095 if (num_of_syms == 0)
11096 {
11097 free (filedata->buckets);
11098 filedata->buckets = NULL;
11099 free (filedata->chains);
11100 filedata->chains = NULL;
11101 filedata->nbuckets = 0;
11102 }
11103 }
11104
11105 if (filedata->dynamic_info_DT_GNU_HASH)
11106 {
11107 unsigned char nb[16];
11108 uint64_t i, maxchain = 0xffffffff, bitmaskwords;
11109 uint64_t buckets_vma;
11110 unsigned long hn;
11111
11112 if (fseek (filedata->handle,
11113 (filedata->archive_file_offset
11114 + offset_from_vma (filedata,
11115 filedata->dynamic_info_DT_GNU_HASH,
11116 sizeof nb)),
11117 SEEK_SET))
11118 {
11119 error (_("Unable to seek to start of dynamic information\n"));
11120 goto no_gnu_hash;
11121 }
11122
11123 if (fread (nb, 16, 1, filedata->handle) != 1)
11124 {
11125 error (_("Failed to read in number of buckets\n"));
11126 goto no_gnu_hash;
11127 }
11128
11129 filedata->ngnubuckets = byte_get (nb, 4);
11130 filedata->gnusymidx = byte_get (nb + 4, 4);
11131 bitmaskwords = byte_get (nb + 8, 4);
11132 buckets_vma = filedata->dynamic_info_DT_GNU_HASH + 16;
11133 if (is_32bit_elf)
11134 buckets_vma += bitmaskwords * 4;
11135 else
11136 buckets_vma += bitmaskwords * 8;
11137
11138 if (fseek (filedata->handle,
11139 (filedata->archive_file_offset
11140 + offset_from_vma (filedata, buckets_vma, 4)),
11141 SEEK_SET))
11142 {
11143 error (_("Unable to seek to start of dynamic information\n"));
11144 goto no_gnu_hash;
11145 }
11146
11147 filedata->gnubuckets
11148 = get_dynamic_data (filedata, filedata->ngnubuckets, 4);
11149
11150 if (filedata->gnubuckets == NULL)
11151 goto no_gnu_hash;
11152
11153 for (i = 0; i < filedata->ngnubuckets; i++)
11154 if (filedata->gnubuckets[i] != 0)
11155 {
11156 if (filedata->gnubuckets[i] < filedata->gnusymidx)
11157 goto no_gnu_hash;
11158
11159 if (maxchain == 0xffffffff || filedata->gnubuckets[i] > maxchain)
11160 maxchain = filedata->gnubuckets[i];
11161 }
11162
11163 if (maxchain == 0xffffffff)
11164 goto no_gnu_hash;
11165
11166 maxchain -= filedata->gnusymidx;
11167
11168 if (fseek (filedata->handle,
11169 (filedata->archive_file_offset
11170 + offset_from_vma (filedata,
11171 buckets_vma + 4 * (filedata->ngnubuckets
11172 + maxchain),
11173 4)),
11174 SEEK_SET))
11175 {
11176 error (_("Unable to seek to start of dynamic information\n"));
11177 goto no_gnu_hash;
11178 }
11179
11180 do
11181 {
11182 if (fread (nb, 4, 1, filedata->handle) != 1)
11183 {
11184 error (_("Failed to determine last chain length\n"));
11185 goto no_gnu_hash;
11186 }
11187
11188 if (maxchain + 1 == 0)
11189 goto no_gnu_hash;
11190
11191 ++maxchain;
11192 }
11193 while ((byte_get (nb, 4) & 1) == 0);
11194
11195 if (fseek (filedata->handle,
11196 (filedata->archive_file_offset
11197 + offset_from_vma (filedata, (buckets_vma
11198 + 4 * filedata->ngnubuckets),
11199 4)),
11200 SEEK_SET))
11201 {
11202 error (_("Unable to seek to start of dynamic information\n"));
11203 goto no_gnu_hash;
11204 }
11205
11206 filedata->gnuchains = get_dynamic_data (filedata, maxchain, 4);
11207 filedata->ngnuchains = maxchain;
11208
11209 if (filedata->gnuchains == NULL)
11210 goto no_gnu_hash;
11211
11212 if (filedata->dynamic_info_DT_MIPS_XHASH)
11213 {
11214 if (fseek (filedata->handle,
11215 (filedata->archive_file_offset
11216 + offset_from_vma (filedata, (buckets_vma
11217 + 4 * (filedata->ngnubuckets
11218 + maxchain)), 4)),
11219 SEEK_SET))
11220 {
11221 error (_("Unable to seek to start of dynamic information\n"));
11222 goto no_gnu_hash;
11223 }
11224
11225 filedata->mipsxlat = get_dynamic_data (filedata, maxchain, 4);
11226 if (filedata->mipsxlat == NULL)
11227 goto no_gnu_hash;
11228 }
11229
11230 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
11231 if (filedata->gnubuckets[hn] != 0)
11232 {
11233 uint64_t si = filedata->gnubuckets[hn];
11234 uint64_t off = si - filedata->gnusymidx;
11235
11236 do
11237 {
11238 if (filedata->dynamic_info_DT_MIPS_XHASH)
11239 {
11240 if (off < filedata->ngnuchains
11241 && filedata->mipsxlat[off] >= num_of_syms)
11242 num_of_syms = filedata->mipsxlat[off] + 1;
11243 }
11244 else
11245 {
11246 if (si >= num_of_syms)
11247 num_of_syms = si + 1;
11248 }
11249 si++;
11250 }
11251 while (off < filedata->ngnuchains
11252 && (filedata->gnuchains[off++] & 1) == 0);
11253 }
11254
11255 if (num_of_syms == 0)
11256 {
11257 no_gnu_hash:
11258 free (filedata->mipsxlat);
11259 filedata->mipsxlat = NULL;
11260 free (filedata->gnuchains);
11261 filedata->gnuchains = NULL;
11262 free (filedata->gnubuckets);
11263 filedata->gnubuckets = NULL;
11264 filedata->ngnubuckets = 0;
11265 filedata->ngnuchains = 0;
11266 }
11267 }
11268
11269 return num_of_syms;
11270 }
11271
11272 /* Parse and display the contents of the dynamic section. */
11273
11274 static bool
11275 process_dynamic_section (Filedata * filedata)
11276 {
11277 Elf_Internal_Dyn * entry;
11278
11279 if (filedata->dynamic_size <= 1)
11280 {
11281 if (do_dynamic)
11282 {
11283 if (filedata->is_separate)
11284 printf (_("\nThere is no dynamic section in linked file '%s'.\n"),
11285 filedata->file_name);
11286 else
11287 printf (_("\nThere is no dynamic section in this file.\n"));
11288 }
11289
11290 return true;
11291 }
11292
11293 if (!get_dynamic_section (filedata))
11294 return false;
11295
11296 /* Find the appropriate symbol table. */
11297 if (filedata->dynamic_symbols == NULL || do_histogram)
11298 {
11299 unsigned long num_of_syms;
11300
11301 for (entry = filedata->dynamic_section;
11302 entry < filedata->dynamic_section + filedata->dynamic_nent;
11303 ++entry)
11304 if (entry->d_tag == DT_SYMTAB)
11305 filedata->dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
11306 else if (entry->d_tag == DT_SYMENT)
11307 filedata->dynamic_info[DT_SYMENT] = entry->d_un.d_val;
11308 else if (entry->d_tag == DT_HASH)
11309 filedata->dynamic_info[DT_HASH] = entry->d_un.d_val;
11310 else if (entry->d_tag == DT_GNU_HASH)
11311 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11312 else if ((filedata->file_header.e_machine == EM_MIPS
11313 || filedata->file_header.e_machine == EM_MIPS_RS3_LE)
11314 && entry->d_tag == DT_MIPS_XHASH)
11315 {
11316 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
11317 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11318 }
11319
11320 num_of_syms = get_num_dynamic_syms (filedata);
11321
11322 if (num_of_syms != 0
11323 && filedata->dynamic_symbols == NULL
11324 && filedata->dynamic_info[DT_SYMTAB]
11325 && filedata->dynamic_info[DT_SYMENT])
11326 {
11327 Elf_Internal_Phdr *seg;
11328 uint64_t vma = filedata->dynamic_info[DT_SYMTAB];
11329
11330 if (! get_program_headers (filedata))
11331 {
11332 error (_("Cannot interpret virtual addresses "
11333 "without program headers.\n"));
11334 return false;
11335 }
11336
11337 for (seg = filedata->program_headers;
11338 seg < filedata->program_headers + filedata->file_header.e_phnum;
11339 ++seg)
11340 {
11341 if (seg->p_type != PT_LOAD)
11342 continue;
11343
11344 if (seg->p_offset + seg->p_filesz > filedata->file_size)
11345 {
11346 /* See PR 21379 for a reproducer. */
11347 error (_("Invalid PT_LOAD entry\n"));
11348 return false;
11349 }
11350
11351 if (vma >= (seg->p_vaddr & -seg->p_align)
11352 && vma < seg->p_vaddr + seg->p_filesz)
11353 {
11354 /* Since we do not know how big the symbol table is,
11355 we default to reading in up to the end of PT_LOAD
11356 segment and processing that. This is overkill, I
11357 know, but it should work. */
11358 Elf_Internal_Shdr section;
11359 section.sh_offset = (vma - seg->p_vaddr
11360 + seg->p_offset);
11361 section.sh_size = (num_of_syms
11362 * filedata->dynamic_info[DT_SYMENT]);
11363 section.sh_entsize = filedata->dynamic_info[DT_SYMENT];
11364
11365 if (do_checks
11366 && filedata->dynamic_symtab_section != NULL
11367 && ((filedata->dynamic_symtab_section->sh_offset
11368 != section.sh_offset)
11369 || (filedata->dynamic_symtab_section->sh_size
11370 != section.sh_size)
11371 || (filedata->dynamic_symtab_section->sh_entsize
11372 != section.sh_entsize)))
11373 warn (_("\
11374 the .dynsym section doesn't match the DT_SYMTAB and DT_SYMENT tags\n"));
11375
11376 section.sh_name = filedata->string_table_length;
11377 filedata->dynamic_symbols
11378 = get_elf_symbols (filedata, &section,
11379 &filedata->num_dynamic_syms);
11380 if (filedata->dynamic_symbols == NULL
11381 || filedata->num_dynamic_syms != num_of_syms)
11382 {
11383 error (_("Corrupt DT_SYMTAB dynamic entry\n"));
11384 return false;
11385 }
11386 break;
11387 }
11388 }
11389 }
11390 }
11391
11392 /* Similarly find a string table. */
11393 if (filedata->dynamic_strings == NULL)
11394 for (entry = filedata->dynamic_section;
11395 entry < filedata->dynamic_section + filedata->dynamic_nent;
11396 ++entry)
11397 {
11398 if (entry->d_tag == DT_STRTAB)
11399 filedata->dynamic_info[DT_STRTAB] = entry->d_un.d_val;
11400
11401 if (entry->d_tag == DT_STRSZ)
11402 filedata->dynamic_info[DT_STRSZ] = entry->d_un.d_val;
11403
11404 if (filedata->dynamic_info[DT_STRTAB]
11405 && filedata->dynamic_info[DT_STRSZ])
11406 {
11407 unsigned long offset;
11408 uint64_t str_tab_len = filedata->dynamic_info[DT_STRSZ];
11409
11410 offset = offset_from_vma (filedata,
11411 filedata->dynamic_info[DT_STRTAB],
11412 str_tab_len);
11413 if (do_checks
11414 && filedata->dynamic_strtab_section
11415 && ((filedata->dynamic_strtab_section->sh_offset
11416 != (file_ptr) offset)
11417 || (filedata->dynamic_strtab_section->sh_size
11418 != str_tab_len)))
11419 warn (_("\
11420 the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
11421
11422 filedata->dynamic_strings
11423 = (char *) get_data (NULL, filedata, offset, 1, str_tab_len,
11424 _("dynamic string table"));
11425 if (filedata->dynamic_strings == NULL)
11426 {
11427 error (_("Corrupt DT_STRTAB dynamic entry\n"));
11428 break;
11429 }
11430
11431 filedata->dynamic_strings_length = str_tab_len;
11432 break;
11433 }
11434 }
11435
11436 /* And find the syminfo section if available. */
11437 if (filedata->dynamic_syminfo == NULL)
11438 {
11439 unsigned long syminsz = 0;
11440
11441 for (entry = filedata->dynamic_section;
11442 entry < filedata->dynamic_section + filedata->dynamic_nent;
11443 ++entry)
11444 {
11445 if (entry->d_tag == DT_SYMINENT)
11446 {
11447 /* Note: these braces are necessary to avoid a syntax
11448 error from the SunOS4 C compiler. */
11449 /* PR binutils/17531: A corrupt file can trigger this test.
11450 So do not use an assert, instead generate an error message. */
11451 if (sizeof (Elf_External_Syminfo) != entry->d_un.d_val)
11452 error (_("Bad value (%d) for SYMINENT entry\n"),
11453 (int) entry->d_un.d_val);
11454 }
11455 else if (entry->d_tag == DT_SYMINSZ)
11456 syminsz = entry->d_un.d_val;
11457 else if (entry->d_tag == DT_SYMINFO)
11458 filedata->dynamic_syminfo_offset
11459 = offset_from_vma (filedata, entry->d_un.d_val, syminsz);
11460 }
11461
11462 if (filedata->dynamic_syminfo_offset != 0 && syminsz != 0)
11463 {
11464 Elf_External_Syminfo * extsyminfo;
11465 Elf_External_Syminfo * extsym;
11466 Elf_Internal_Syminfo * syminfo;
11467
11468 /* There is a syminfo section. Read the data. */
11469 extsyminfo = (Elf_External_Syminfo *)
11470 get_data (NULL, filedata, filedata->dynamic_syminfo_offset,
11471 1, syminsz, _("symbol information"));
11472 if (!extsyminfo)
11473 return false;
11474
11475 if (filedata->dynamic_syminfo != NULL)
11476 {
11477 error (_("Multiple dynamic symbol information sections found\n"));
11478 free (filedata->dynamic_syminfo);
11479 }
11480 filedata->dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
11481 if (filedata->dynamic_syminfo == NULL)
11482 {
11483 error (_("Out of memory allocating %lu bytes "
11484 "for dynamic symbol info\n"),
11485 (unsigned long) syminsz);
11486 return false;
11487 }
11488
11489 filedata->dynamic_syminfo_nent
11490 = syminsz / sizeof (Elf_External_Syminfo);
11491 for (syminfo = filedata->dynamic_syminfo, extsym = extsyminfo;
11492 syminfo < (filedata->dynamic_syminfo
11493 + filedata->dynamic_syminfo_nent);
11494 ++syminfo, ++extsym)
11495 {
11496 syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
11497 syminfo->si_flags = BYTE_GET (extsym->si_flags);
11498 }
11499
11500 free (extsyminfo);
11501 }
11502 }
11503
11504 if (do_dynamic && filedata->dynamic_addr)
11505 {
11506 if (filedata->is_separate)
11507 printf (ngettext ("\nIn linked file '%s' the dynamic section at offset 0x%lx contains %lu entry:\n",
11508 "\nIn linked file '%s' the dynamic section at offset 0x%lx contains %lu entries:\n",
11509 (unsigned long) filedata->dynamic_nent),
11510 filedata->file_name,
11511 filedata->dynamic_addr,
11512 (unsigned long) filedata->dynamic_nent);
11513 else
11514 printf (ngettext ("\nDynamic section at offset 0x%lx contains %lu entry:\n",
11515 "\nDynamic section at offset 0x%lx contains %lu entries:\n",
11516 (unsigned long) filedata->dynamic_nent),
11517 filedata->dynamic_addr,
11518 (unsigned long) filedata->dynamic_nent);
11519 }
11520 if (do_dynamic)
11521 printf (_(" Tag Type Name/Value\n"));
11522
11523 for (entry = filedata->dynamic_section;
11524 entry < filedata->dynamic_section + filedata->dynamic_nent;
11525 entry++)
11526 {
11527 if (do_dynamic)
11528 {
11529 const char * dtype;
11530
11531 putchar (' ');
11532 print_vma (entry->d_tag, FULL_HEX);
11533 dtype = get_dynamic_type (filedata, entry->d_tag);
11534 printf (" (%s)%*s", dtype,
11535 ((is_32bit_elf ? 27 : 19) - (int) strlen (dtype)), " ");
11536 }
11537
11538 switch (entry->d_tag)
11539 {
11540 case DT_FLAGS:
11541 if (do_dynamic)
11542 print_dynamic_flags (entry->d_un.d_val);
11543 break;
11544
11545 case DT_AUXILIARY:
11546 case DT_FILTER:
11547 case DT_CONFIG:
11548 case DT_DEPAUDIT:
11549 case DT_AUDIT:
11550 if (do_dynamic)
11551 {
11552 switch (entry->d_tag)
11553 {
11554 case DT_AUXILIARY:
11555 printf (_("Auxiliary library"));
11556 break;
11557
11558 case DT_FILTER:
11559 printf (_("Filter library"));
11560 break;
11561
11562 case DT_CONFIG:
11563 printf (_("Configuration file"));
11564 break;
11565
11566 case DT_DEPAUDIT:
11567 printf (_("Dependency audit library"));
11568 break;
11569
11570 case DT_AUDIT:
11571 printf (_("Audit library"));
11572 break;
11573 }
11574
11575 if (valid_dynamic_name (filedata, entry->d_un.d_val))
11576 printf (": [%s]\n",
11577 get_dynamic_name (filedata, entry->d_un.d_val));
11578 else
11579 {
11580 printf (": ");
11581 print_vma (entry->d_un.d_val, PREFIX_HEX);
11582 putchar ('\n');
11583 }
11584 }
11585 break;
11586
11587 case DT_FEATURE:
11588 if (do_dynamic)
11589 {
11590 printf (_("Flags:"));
11591
11592 if (entry->d_un.d_val == 0)
11593 printf (_(" None\n"));
11594 else
11595 {
11596 unsigned long int val = entry->d_un.d_val;
11597
11598 if (val & DTF_1_PARINIT)
11599 {
11600 printf (" PARINIT");
11601 val ^= DTF_1_PARINIT;
11602 }
11603 if (val & DTF_1_CONFEXP)
11604 {
11605 printf (" CONFEXP");
11606 val ^= DTF_1_CONFEXP;
11607 }
11608 if (val != 0)
11609 printf (" %lx", val);
11610 puts ("");
11611 }
11612 }
11613 break;
11614
11615 case DT_POSFLAG_1:
11616 if (do_dynamic)
11617 {
11618 printf (_("Flags:"));
11619
11620 if (entry->d_un.d_val == 0)
11621 printf (_(" None\n"));
11622 else
11623 {
11624 unsigned long int val = entry->d_un.d_val;
11625
11626 if (val & DF_P1_LAZYLOAD)
11627 {
11628 printf (" LAZYLOAD");
11629 val ^= DF_P1_LAZYLOAD;
11630 }
11631 if (val & DF_P1_GROUPPERM)
11632 {
11633 printf (" GROUPPERM");
11634 val ^= DF_P1_GROUPPERM;
11635 }
11636 if (val != 0)
11637 printf (" %lx", val);
11638 puts ("");
11639 }
11640 }
11641 break;
11642
11643 case DT_FLAGS_1:
11644 if (do_dynamic)
11645 {
11646 printf (_("Flags:"));
11647 if (entry->d_un.d_val == 0)
11648 printf (_(" None\n"));
11649 else
11650 {
11651 unsigned long int val = entry->d_un.d_val;
11652
11653 if (val & DF_1_NOW)
11654 {
11655 printf (" NOW");
11656 val ^= DF_1_NOW;
11657 }
11658 if (val & DF_1_GLOBAL)
11659 {
11660 printf (" GLOBAL");
11661 val ^= DF_1_GLOBAL;
11662 }
11663 if (val & DF_1_GROUP)
11664 {
11665 printf (" GROUP");
11666 val ^= DF_1_GROUP;
11667 }
11668 if (val & DF_1_NODELETE)
11669 {
11670 printf (" NODELETE");
11671 val ^= DF_1_NODELETE;
11672 }
11673 if (val & DF_1_LOADFLTR)
11674 {
11675 printf (" LOADFLTR");
11676 val ^= DF_1_LOADFLTR;
11677 }
11678 if (val & DF_1_INITFIRST)
11679 {
11680 printf (" INITFIRST");
11681 val ^= DF_1_INITFIRST;
11682 }
11683 if (val & DF_1_NOOPEN)
11684 {
11685 printf (" NOOPEN");
11686 val ^= DF_1_NOOPEN;
11687 }
11688 if (val & DF_1_ORIGIN)
11689 {
11690 printf (" ORIGIN");
11691 val ^= DF_1_ORIGIN;
11692 }
11693 if (val & DF_1_DIRECT)
11694 {
11695 printf (" DIRECT");
11696 val ^= DF_1_DIRECT;
11697 }
11698 if (val & DF_1_TRANS)
11699 {
11700 printf (" TRANS");
11701 val ^= DF_1_TRANS;
11702 }
11703 if (val & DF_1_INTERPOSE)
11704 {
11705 printf (" INTERPOSE");
11706 val ^= DF_1_INTERPOSE;
11707 }
11708 if (val & DF_1_NODEFLIB)
11709 {
11710 printf (" NODEFLIB");
11711 val ^= DF_1_NODEFLIB;
11712 }
11713 if (val & DF_1_NODUMP)
11714 {
11715 printf (" NODUMP");
11716 val ^= DF_1_NODUMP;
11717 }
11718 if (val & DF_1_CONFALT)
11719 {
11720 printf (" CONFALT");
11721 val ^= DF_1_CONFALT;
11722 }
11723 if (val & DF_1_ENDFILTEE)
11724 {
11725 printf (" ENDFILTEE");
11726 val ^= DF_1_ENDFILTEE;
11727 }
11728 if (val & DF_1_DISPRELDNE)
11729 {
11730 printf (" DISPRELDNE");
11731 val ^= DF_1_DISPRELDNE;
11732 }
11733 if (val & DF_1_DISPRELPND)
11734 {
11735 printf (" DISPRELPND");
11736 val ^= DF_1_DISPRELPND;
11737 }
11738 if (val & DF_1_NODIRECT)
11739 {
11740 printf (" NODIRECT");
11741 val ^= DF_1_NODIRECT;
11742 }
11743 if (val & DF_1_IGNMULDEF)
11744 {
11745 printf (" IGNMULDEF");
11746 val ^= DF_1_IGNMULDEF;
11747 }
11748 if (val & DF_1_NOKSYMS)
11749 {
11750 printf (" NOKSYMS");
11751 val ^= DF_1_NOKSYMS;
11752 }
11753 if (val & DF_1_NOHDR)
11754 {
11755 printf (" NOHDR");
11756 val ^= DF_1_NOHDR;
11757 }
11758 if (val & DF_1_EDITED)
11759 {
11760 printf (" EDITED");
11761 val ^= DF_1_EDITED;
11762 }
11763 if (val & DF_1_NORELOC)
11764 {
11765 printf (" NORELOC");
11766 val ^= DF_1_NORELOC;
11767 }
11768 if (val & DF_1_SYMINTPOSE)
11769 {
11770 printf (" SYMINTPOSE");
11771 val ^= DF_1_SYMINTPOSE;
11772 }
11773 if (val & DF_1_GLOBAUDIT)
11774 {
11775 printf (" GLOBAUDIT");
11776 val ^= DF_1_GLOBAUDIT;
11777 }
11778 if (val & DF_1_SINGLETON)
11779 {
11780 printf (" SINGLETON");
11781 val ^= DF_1_SINGLETON;
11782 }
11783 if (val & DF_1_STUB)
11784 {
11785 printf (" STUB");
11786 val ^= DF_1_STUB;
11787 }
11788 if (val & DF_1_PIE)
11789 {
11790 printf (" PIE");
11791 val ^= DF_1_PIE;
11792 }
11793 if (val & DF_1_KMOD)
11794 {
11795 printf (" KMOD");
11796 val ^= DF_1_KMOD;
11797 }
11798 if (val & DF_1_WEAKFILTER)
11799 {
11800 printf (" WEAKFILTER");
11801 val ^= DF_1_WEAKFILTER;
11802 }
11803 if (val & DF_1_NOCOMMON)
11804 {
11805 printf (" NOCOMMON");
11806 val ^= DF_1_NOCOMMON;
11807 }
11808 if (val != 0)
11809 printf (" %lx", val);
11810 puts ("");
11811 }
11812 }
11813 break;
11814
11815 case DT_PLTREL:
11816 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11817 if (do_dynamic)
11818 puts (get_dynamic_type (filedata, entry->d_un.d_val));
11819 break;
11820
11821 case DT_NULL :
11822 case DT_NEEDED :
11823 case DT_PLTGOT :
11824 case DT_HASH :
11825 case DT_STRTAB :
11826 case DT_SYMTAB :
11827 case DT_RELA :
11828 case DT_INIT :
11829 case DT_FINI :
11830 case DT_SONAME :
11831 case DT_RPATH :
11832 case DT_SYMBOLIC:
11833 case DT_REL :
11834 case DT_RELR :
11835 case DT_DEBUG :
11836 case DT_TEXTREL :
11837 case DT_JMPREL :
11838 case DT_RUNPATH :
11839 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11840
11841 if (do_dynamic)
11842 {
11843 const char *name;
11844
11845 if (valid_dynamic_name (filedata, entry->d_un.d_val))
11846 name = get_dynamic_name (filedata, entry->d_un.d_val);
11847 else
11848 name = NULL;
11849
11850 if (name)
11851 {
11852 switch (entry->d_tag)
11853 {
11854 case DT_NEEDED:
11855 printf (_("Shared library: [%s]"), name);
11856
11857 if (filedata->program_interpreter
11858 && streq (name, filedata->program_interpreter))
11859 printf (_(" program interpreter"));
11860 break;
11861
11862 case DT_SONAME:
11863 printf (_("Library soname: [%s]"), name);
11864 break;
11865
11866 case DT_RPATH:
11867 printf (_("Library rpath: [%s]"), name);
11868 break;
11869
11870 case DT_RUNPATH:
11871 printf (_("Library runpath: [%s]"), name);
11872 break;
11873
11874 default:
11875 print_vma (entry->d_un.d_val, PREFIX_HEX);
11876 break;
11877 }
11878 }
11879 else
11880 print_vma (entry->d_un.d_val, PREFIX_HEX);
11881
11882 putchar ('\n');
11883 }
11884 break;
11885
11886 case DT_PLTRELSZ:
11887 case DT_RELASZ :
11888 case DT_STRSZ :
11889 case DT_RELSZ :
11890 case DT_RELAENT :
11891 case DT_RELRENT :
11892 case DT_RELRSZ :
11893 case DT_SYMENT :
11894 case DT_RELENT :
11895 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11896 /* Fall through. */
11897 case DT_PLTPADSZ:
11898 case DT_MOVEENT :
11899 case DT_MOVESZ :
11900 case DT_PREINIT_ARRAYSZ:
11901 case DT_INIT_ARRAYSZ:
11902 case DT_FINI_ARRAYSZ:
11903 case DT_GNU_CONFLICTSZ:
11904 case DT_GNU_LIBLISTSZ:
11905 if (do_dynamic)
11906 {
11907 print_vma (entry->d_un.d_val, UNSIGNED);
11908 printf (_(" (bytes)\n"));
11909 }
11910 break;
11911
11912 case DT_VERDEFNUM:
11913 case DT_VERNEEDNUM:
11914 case DT_RELACOUNT:
11915 case DT_RELCOUNT:
11916 if (do_dynamic)
11917 {
11918 print_vma (entry->d_un.d_val, UNSIGNED);
11919 putchar ('\n');
11920 }
11921 break;
11922
11923 case DT_SYMINSZ:
11924 case DT_SYMINENT:
11925 case DT_SYMINFO:
11926 case DT_USED:
11927 case DT_INIT_ARRAY:
11928 case DT_FINI_ARRAY:
11929 if (do_dynamic)
11930 {
11931 if (entry->d_tag == DT_USED
11932 && valid_dynamic_name (filedata, entry->d_un.d_val))
11933 {
11934 const char *name
11935 = get_dynamic_name (filedata, entry->d_un.d_val);
11936
11937 if (*name)
11938 {
11939 printf (_("Not needed object: [%s]\n"), name);
11940 break;
11941 }
11942 }
11943
11944 print_vma (entry->d_un.d_val, PREFIX_HEX);
11945 putchar ('\n');
11946 }
11947 break;
11948
11949 case DT_BIND_NOW:
11950 /* The value of this entry is ignored. */
11951 if (do_dynamic)
11952 putchar ('\n');
11953 break;
11954
11955 case DT_GNU_PRELINKED:
11956 if (do_dynamic)
11957 {
11958 struct tm * tmp;
11959 time_t atime = entry->d_un.d_val;
11960
11961 tmp = gmtime (&atime);
11962 /* PR 17533 file: 041-1244816-0.004. */
11963 if (tmp == NULL)
11964 printf (_("<corrupt time val: %lx"),
11965 (unsigned long) atime);
11966 else
11967 printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
11968 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
11969 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
11970
11971 }
11972 break;
11973
11974 case DT_GNU_HASH:
11975 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11976 if (do_dynamic)
11977 {
11978 print_vma (entry->d_un.d_val, PREFIX_HEX);
11979 putchar ('\n');
11980 }
11981 break;
11982
11983 case DT_GNU_FLAGS_1:
11984 if (do_dynamic)
11985 {
11986 printf (_("Flags:"));
11987 if (entry->d_un.d_val == 0)
11988 printf (_(" None\n"));
11989 else
11990 {
11991 unsigned long int val = entry->d_un.d_val;
11992
11993 if (val & DF_GNU_1_UNIQUE)
11994 {
11995 printf (" UNIQUE");
11996 val ^= DF_GNU_1_UNIQUE;
11997 }
11998 if (val != 0)
11999 printf (" %lx", val);
12000 puts ("");
12001 }
12002 }
12003 break;
12004
12005 default:
12006 if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
12007 filedata->version_info[DT_VERSIONTAGIDX (entry->d_tag)]
12008 = entry->d_un.d_val;
12009
12010 if (do_dynamic)
12011 {
12012 switch (filedata->file_header.e_machine)
12013 {
12014 case EM_AARCH64:
12015 dynamic_section_aarch64_val (entry);
12016 break;
12017 case EM_MIPS:
12018 case EM_MIPS_RS3_LE:
12019 dynamic_section_mips_val (filedata, entry);
12020 break;
12021 case EM_PARISC:
12022 dynamic_section_parisc_val (entry);
12023 break;
12024 case EM_IA_64:
12025 dynamic_section_ia64_val (entry);
12026 break;
12027 default:
12028 print_vma (entry->d_un.d_val, PREFIX_HEX);
12029 putchar ('\n');
12030 }
12031 }
12032 break;
12033 }
12034 }
12035
12036 return true;
12037 }
12038
12039 static char *
12040 get_ver_flags (unsigned int flags)
12041 {
12042 static char buff[128];
12043
12044 buff[0] = 0;
12045
12046 if (flags == 0)
12047 return _("none");
12048
12049 if (flags & VER_FLG_BASE)
12050 strcat (buff, "BASE");
12051
12052 if (flags & VER_FLG_WEAK)
12053 {
12054 if (flags & VER_FLG_BASE)
12055 strcat (buff, " | ");
12056
12057 strcat (buff, "WEAK");
12058 }
12059
12060 if (flags & VER_FLG_INFO)
12061 {
12062 if (flags & (VER_FLG_BASE|VER_FLG_WEAK))
12063 strcat (buff, " | ");
12064
12065 strcat (buff, "INFO");
12066 }
12067
12068 if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
12069 {
12070 if (flags & (VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
12071 strcat (buff, " | ");
12072
12073 strcat (buff, _("<unknown>"));
12074 }
12075
12076 return buff;
12077 }
12078
12079 /* Display the contents of the version sections. */
12080
12081 static bool
12082 process_version_sections (Filedata * filedata)
12083 {
12084 Elf_Internal_Shdr * section;
12085 unsigned i;
12086 bool found = false;
12087
12088 if (! do_version)
12089 return true;
12090
12091 for (i = 0, section = filedata->section_headers;
12092 i < filedata->file_header.e_shnum;
12093 i++, section++)
12094 {
12095 switch (section->sh_type)
12096 {
12097 case SHT_GNU_verdef:
12098 {
12099 Elf_External_Verdef * edefs;
12100 unsigned long idx;
12101 unsigned long cnt;
12102 char * endbuf;
12103
12104 found = true;
12105
12106 if (filedata->is_separate)
12107 printf (ngettext ("\nIn linked file '%s' the version definition section '%s' contains %u entry:\n",
12108 "\nIn linked file '%s' the version definition section '%s' contains %u entries:\n",
12109 section->sh_info),
12110 filedata->file_name,
12111 printable_section_name (filedata, section),
12112 section->sh_info);
12113 else
12114 printf (ngettext ("\nVersion definition section '%s' "
12115 "contains %u entry:\n",
12116 "\nVersion definition section '%s' "
12117 "contains %u entries:\n",
12118 section->sh_info),
12119 printable_section_name (filedata, section),
12120 section->sh_info);
12121
12122 printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
12123 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
12124 (unsigned long) section->sh_offset, section->sh_link,
12125 printable_section_name_from_index (filedata, section->sh_link));
12126
12127 edefs = (Elf_External_Verdef *)
12128 get_data (NULL, filedata, section->sh_offset, 1,section->sh_size,
12129 _("version definition section"));
12130 if (!edefs)
12131 break;
12132 endbuf = (char *) edefs + section->sh_size;
12133
12134 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
12135 {
12136 char * vstart;
12137 Elf_External_Verdef * edef;
12138 Elf_Internal_Verdef ent;
12139 Elf_External_Verdaux * eaux;
12140 Elf_Internal_Verdaux aux;
12141 unsigned long isum;
12142 int j;
12143
12144 vstart = ((char *) edefs) + idx;
12145 if (vstart + sizeof (*edef) > endbuf)
12146 break;
12147
12148 edef = (Elf_External_Verdef *) vstart;
12149
12150 ent.vd_version = BYTE_GET (edef->vd_version);
12151 ent.vd_flags = BYTE_GET (edef->vd_flags);
12152 ent.vd_ndx = BYTE_GET (edef->vd_ndx);
12153 ent.vd_cnt = BYTE_GET (edef->vd_cnt);
12154 ent.vd_hash = BYTE_GET (edef->vd_hash);
12155 ent.vd_aux = BYTE_GET (edef->vd_aux);
12156 ent.vd_next = BYTE_GET (edef->vd_next);
12157
12158 printf (_(" %#06lx: Rev: %d Flags: %s"),
12159 idx, ent.vd_version, get_ver_flags (ent.vd_flags));
12160
12161 printf (_(" Index: %d Cnt: %d "),
12162 ent.vd_ndx, ent.vd_cnt);
12163
12164 /* Check for overflow. */
12165 if (ent.vd_aux > (size_t) (endbuf - vstart))
12166 break;
12167
12168 vstart += ent.vd_aux;
12169
12170 if (vstart + sizeof (*eaux) > endbuf)
12171 break;
12172 eaux = (Elf_External_Verdaux *) vstart;
12173
12174 aux.vda_name = BYTE_GET (eaux->vda_name);
12175 aux.vda_next = BYTE_GET (eaux->vda_next);
12176
12177 if (valid_dynamic_name (filedata, aux.vda_name))
12178 printf (_("Name: %s\n"),
12179 get_dynamic_name (filedata, aux.vda_name));
12180 else
12181 printf (_("Name index: %ld\n"), aux.vda_name);
12182
12183 isum = idx + ent.vd_aux;
12184
12185 for (j = 1; j < ent.vd_cnt; j++)
12186 {
12187 if (aux.vda_next < sizeof (*eaux)
12188 && !(j == ent.vd_cnt - 1 && aux.vda_next == 0))
12189 {
12190 warn (_("Invalid vda_next field of %lx\n"),
12191 aux.vda_next);
12192 j = ent.vd_cnt;
12193 break;
12194 }
12195 /* Check for overflow. */
12196 if (aux.vda_next > (size_t) (endbuf - vstart))
12197 break;
12198
12199 isum += aux.vda_next;
12200 vstart += aux.vda_next;
12201
12202 if (vstart + sizeof (*eaux) > endbuf)
12203 break;
12204 eaux = (Elf_External_Verdaux *) vstart;
12205
12206 aux.vda_name = BYTE_GET (eaux->vda_name);
12207 aux.vda_next = BYTE_GET (eaux->vda_next);
12208
12209 if (valid_dynamic_name (filedata, aux.vda_name))
12210 printf (_(" %#06lx: Parent %d: %s\n"),
12211 isum, j,
12212 get_dynamic_name (filedata, aux.vda_name));
12213 else
12214 printf (_(" %#06lx: Parent %d, name index: %ld\n"),
12215 isum, j, aux.vda_name);
12216 }
12217
12218 if (j < ent.vd_cnt)
12219 printf (_(" Version def aux past end of section\n"));
12220
12221 /* PR 17531:
12222 file: id:000001,src:000172+005151,op:splice,rep:2. */
12223 if (ent.vd_next < sizeof (*edef)
12224 && !(cnt == section->sh_info - 1 && ent.vd_next == 0))
12225 {
12226 warn (_("Invalid vd_next field of %lx\n"), ent.vd_next);
12227 cnt = section->sh_info;
12228 break;
12229 }
12230 if (ent.vd_next > (size_t) (endbuf - ((char *) edefs + idx)))
12231 break;
12232
12233 idx += ent.vd_next;
12234 }
12235
12236 if (cnt < section->sh_info)
12237 printf (_(" Version definition past end of section\n"));
12238
12239 free (edefs);
12240 }
12241 break;
12242
12243 case SHT_GNU_verneed:
12244 {
12245 Elf_External_Verneed * eneed;
12246 unsigned long idx;
12247 unsigned long cnt;
12248 char * endbuf;
12249
12250 found = true;
12251
12252 if (filedata->is_separate)
12253 printf (ngettext ("\nIn linked file '%s' the version needs section '%s' contains %u entry:\n",
12254 "\nIn linked file '%s' the version needs section '%s' contains %u entries:\n",
12255 section->sh_info),
12256 filedata->file_name,
12257 printable_section_name (filedata, section),
12258 section->sh_info);
12259 else
12260 printf (ngettext ("\nVersion needs section '%s' "
12261 "contains %u entry:\n",
12262 "\nVersion needs section '%s' "
12263 "contains %u entries:\n",
12264 section->sh_info),
12265 printable_section_name (filedata, section),
12266 section->sh_info);
12267
12268 printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
12269 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
12270 (unsigned long) section->sh_offset, section->sh_link,
12271 printable_section_name_from_index (filedata, section->sh_link));
12272
12273 eneed = (Elf_External_Verneed *) get_data (NULL, filedata,
12274 section->sh_offset, 1,
12275 section->sh_size,
12276 _("Version Needs section"));
12277 if (!eneed)
12278 break;
12279 endbuf = (char *) eneed + section->sh_size;
12280
12281 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
12282 {
12283 Elf_External_Verneed * entry;
12284 Elf_Internal_Verneed ent;
12285 unsigned long isum;
12286 int j;
12287 char * vstart;
12288
12289 vstart = ((char *) eneed) + idx;
12290 if (vstart + sizeof (*entry) > endbuf)
12291 break;
12292
12293 entry = (Elf_External_Verneed *) vstart;
12294
12295 ent.vn_version = BYTE_GET (entry->vn_version);
12296 ent.vn_cnt = BYTE_GET (entry->vn_cnt);
12297 ent.vn_file = BYTE_GET (entry->vn_file);
12298 ent.vn_aux = BYTE_GET (entry->vn_aux);
12299 ent.vn_next = BYTE_GET (entry->vn_next);
12300
12301 printf (_(" %#06lx: Version: %d"), idx, ent.vn_version);
12302
12303 if (valid_dynamic_name (filedata, ent.vn_file))
12304 printf (_(" File: %s"),
12305 get_dynamic_name (filedata, ent.vn_file));
12306 else
12307 printf (_(" File: %lx"), ent.vn_file);
12308
12309 printf (_(" Cnt: %d\n"), ent.vn_cnt);
12310
12311 /* Check for overflow. */
12312 if (ent.vn_aux > (size_t) (endbuf - vstart))
12313 break;
12314 vstart += ent.vn_aux;
12315
12316 for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
12317 {
12318 Elf_External_Vernaux * eaux;
12319 Elf_Internal_Vernaux aux;
12320
12321 if (vstart + sizeof (*eaux) > endbuf)
12322 break;
12323 eaux = (Elf_External_Vernaux *) vstart;
12324
12325 aux.vna_hash = BYTE_GET (eaux->vna_hash);
12326 aux.vna_flags = BYTE_GET (eaux->vna_flags);
12327 aux.vna_other = BYTE_GET (eaux->vna_other);
12328 aux.vna_name = BYTE_GET (eaux->vna_name);
12329 aux.vna_next = BYTE_GET (eaux->vna_next);
12330
12331 if (valid_dynamic_name (filedata, aux.vna_name))
12332 printf (_(" %#06lx: Name: %s"),
12333 isum, get_dynamic_name (filedata, aux.vna_name));
12334 else
12335 printf (_(" %#06lx: Name index: %lx"),
12336 isum, aux.vna_name);
12337
12338 printf (_(" Flags: %s Version: %d\n"),
12339 get_ver_flags (aux.vna_flags), aux.vna_other);
12340
12341 if (aux.vna_next < sizeof (*eaux)
12342 && !(j == ent.vn_cnt - 1 && aux.vna_next == 0))
12343 {
12344 warn (_("Invalid vna_next field of %lx\n"),
12345 aux.vna_next);
12346 j = ent.vn_cnt;
12347 break;
12348 }
12349 /* Check for overflow. */
12350 if (aux.vna_next > (size_t) (endbuf - vstart))
12351 break;
12352 isum += aux.vna_next;
12353 vstart += aux.vna_next;
12354 }
12355
12356 if (j < ent.vn_cnt)
12357 warn (_("Missing Version Needs auxiliary information\n"));
12358
12359 if (ent.vn_next < sizeof (*entry)
12360 && !(cnt == section->sh_info - 1 && ent.vn_next == 0))
12361 {
12362 warn (_("Invalid vn_next field of %lx\n"), ent.vn_next);
12363 cnt = section->sh_info;
12364 break;
12365 }
12366 if (ent.vn_next > (size_t) (endbuf - ((char *) eneed + idx)))
12367 break;
12368 idx += ent.vn_next;
12369 }
12370
12371 if (cnt < section->sh_info)
12372 warn (_("Missing Version Needs information\n"));
12373
12374 free (eneed);
12375 }
12376 break;
12377
12378 case SHT_GNU_versym:
12379 {
12380 Elf_Internal_Shdr * link_section;
12381 size_t total;
12382 unsigned int cnt;
12383 unsigned char * edata;
12384 unsigned short * data;
12385 char * strtab;
12386 Elf_Internal_Sym * symbols;
12387 Elf_Internal_Shdr * string_sec;
12388 unsigned long num_syms;
12389 long off;
12390
12391 if (section->sh_link >= filedata->file_header.e_shnum)
12392 break;
12393
12394 link_section = filedata->section_headers + section->sh_link;
12395 total = section->sh_size / sizeof (Elf_External_Versym);
12396
12397 if (link_section->sh_link >= filedata->file_header.e_shnum)
12398 break;
12399
12400 found = true;
12401
12402 symbols = get_elf_symbols (filedata, link_section, & num_syms);
12403 if (symbols == NULL)
12404 break;
12405
12406 string_sec = filedata->section_headers + link_section->sh_link;
12407
12408 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
12409 string_sec->sh_size,
12410 _("version string table"));
12411 if (!strtab)
12412 {
12413 free (symbols);
12414 break;
12415 }
12416
12417 if (filedata->is_separate)
12418 printf (ngettext ("\nIn linked file '%s' the version symbols section '%s' contains %lu entry:\n",
12419 "\nIn linked file '%s' the version symbols section '%s' contains %lu entries:\n",
12420 total),
12421 filedata->file_name,
12422 printable_section_name (filedata, section),
12423 (unsigned long) total);
12424 else
12425 printf (ngettext ("\nVersion symbols section '%s' "
12426 "contains %lu entry:\n",
12427 "\nVersion symbols section '%s' "
12428 "contains %lu entries:\n",
12429 total),
12430 printable_section_name (filedata, section),
12431 (unsigned long) total);
12432
12433 printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
12434 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
12435 (unsigned long) section->sh_offset, section->sh_link,
12436 printable_section_name (filedata, link_section));
12437
12438 off = offset_from_vma (filedata,
12439 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
12440 total * sizeof (short));
12441 edata = (unsigned char *) get_data (NULL, filedata, off,
12442 sizeof (short), total,
12443 _("version symbol data"));
12444 if (!edata)
12445 {
12446 free (strtab);
12447 free (symbols);
12448 break;
12449 }
12450
12451 data = (short unsigned int *) cmalloc (total, sizeof (short));
12452
12453 for (cnt = total; cnt --;)
12454 data[cnt] = byte_get (edata + cnt * sizeof (short),
12455 sizeof (short));
12456
12457 free (edata);
12458
12459 for (cnt = 0; cnt < total; cnt += 4)
12460 {
12461 int j, nn;
12462 char *name;
12463 char *invalid = _("*invalid*");
12464
12465 printf (" %03x:", cnt);
12466
12467 for (j = 0; (j < 4) && (cnt + j) < total; ++j)
12468 switch (data[cnt + j])
12469 {
12470 case 0:
12471 fputs (_(" 0 (*local*) "), stdout);
12472 break;
12473
12474 case 1:
12475 fputs (_(" 1 (*global*) "), stdout);
12476 break;
12477
12478 default:
12479 nn = printf ("%4x%c", data[cnt + j] & VERSYM_VERSION,
12480 data[cnt + j] & VERSYM_HIDDEN ? 'h' : ' ');
12481
12482 /* If this index value is greater than the size of the symbols
12483 array, break to avoid an out-of-bounds read. */
12484 if ((unsigned long)(cnt + j) >= num_syms)
12485 {
12486 warn (_("invalid index into symbol array\n"));
12487 break;
12488 }
12489
12490 name = NULL;
12491 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
12492 {
12493 Elf_Internal_Verneed ivn;
12494 unsigned long offset;
12495
12496 offset = offset_from_vma
12497 (filedata,
12498 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
12499 sizeof (Elf_External_Verneed));
12500
12501 do
12502 {
12503 Elf_Internal_Vernaux ivna;
12504 Elf_External_Verneed evn;
12505 Elf_External_Vernaux evna;
12506 unsigned long a_off;
12507
12508 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
12509 _("version need")) == NULL)
12510 break;
12511
12512 ivn.vn_aux = BYTE_GET (evn.vn_aux);
12513 ivn.vn_next = BYTE_GET (evn.vn_next);
12514
12515 a_off = offset + ivn.vn_aux;
12516
12517 do
12518 {
12519 if (get_data (&evna, filedata, a_off, sizeof (evna),
12520 1, _("version need aux (2)")) == NULL)
12521 {
12522 ivna.vna_next = 0;
12523 ivna.vna_other = 0;
12524 }
12525 else
12526 {
12527 ivna.vna_next = BYTE_GET (evna.vna_next);
12528 ivna.vna_other = BYTE_GET (evna.vna_other);
12529 }
12530
12531 a_off += ivna.vna_next;
12532 }
12533 while (ivna.vna_other != data[cnt + j]
12534 && ivna.vna_next != 0);
12535
12536 if (ivna.vna_other == data[cnt + j])
12537 {
12538 ivna.vna_name = BYTE_GET (evna.vna_name);
12539
12540 if (ivna.vna_name >= string_sec->sh_size)
12541 name = invalid;
12542 else
12543 name = strtab + ivna.vna_name;
12544 break;
12545 }
12546
12547 offset += ivn.vn_next;
12548 }
12549 while (ivn.vn_next);
12550 }
12551
12552 if (data[cnt + j] != 0x8001
12553 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
12554 {
12555 Elf_Internal_Verdef ivd;
12556 Elf_External_Verdef evd;
12557 unsigned long offset;
12558
12559 offset = offset_from_vma
12560 (filedata,
12561 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
12562 sizeof evd);
12563
12564 do
12565 {
12566 if (get_data (&evd, filedata, offset, sizeof (evd), 1,
12567 _("version def")) == NULL)
12568 {
12569 ivd.vd_next = 0;
12570 /* PR 17531: file: 046-1082287-0.004. */
12571 ivd.vd_ndx = (data[cnt + j] & VERSYM_VERSION) + 1;
12572 break;
12573 }
12574 else
12575 {
12576 ivd.vd_next = BYTE_GET (evd.vd_next);
12577 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
12578 }
12579
12580 offset += ivd.vd_next;
12581 }
12582 while (ivd.vd_ndx != (data[cnt + j] & VERSYM_VERSION)
12583 && ivd.vd_next != 0);
12584
12585 if (ivd.vd_ndx == (data[cnt + j] & VERSYM_VERSION))
12586 {
12587 Elf_External_Verdaux evda;
12588 Elf_Internal_Verdaux ivda;
12589
12590 ivd.vd_aux = BYTE_GET (evd.vd_aux);
12591
12592 if (get_data (&evda, filedata,
12593 offset - ivd.vd_next + ivd.vd_aux,
12594 sizeof (evda), 1,
12595 _("version def aux")) == NULL)
12596 break;
12597
12598 ivda.vda_name = BYTE_GET (evda.vda_name);
12599
12600 if (ivda.vda_name >= string_sec->sh_size)
12601 name = invalid;
12602 else if (name != NULL && name != invalid)
12603 name = _("*both*");
12604 else
12605 name = strtab + ivda.vda_name;
12606 }
12607 }
12608 if (name != NULL)
12609 nn += printf ("(%s%-*s",
12610 name,
12611 12 - (int) strlen (name),
12612 ")");
12613
12614 if (nn < 18)
12615 printf ("%*c", 18 - nn, ' ');
12616 }
12617
12618 putchar ('\n');
12619 }
12620
12621 free (data);
12622 free (strtab);
12623 free (symbols);
12624 }
12625 break;
12626
12627 default:
12628 break;
12629 }
12630 }
12631
12632 if (! found)
12633 {
12634 if (filedata->is_separate)
12635 printf (_("\nNo version information found in linked file '%s'.\n"),
12636 filedata->file_name);
12637 else
12638 printf (_("\nNo version information found in this file.\n"));
12639 }
12640
12641 return true;
12642 }
12643
12644 static const char *
12645 get_symbol_binding (Filedata * filedata, unsigned int binding)
12646 {
12647 static char buff[64];
12648
12649 switch (binding)
12650 {
12651 case STB_LOCAL: return "LOCAL";
12652 case STB_GLOBAL: return "GLOBAL";
12653 case STB_WEAK: return "WEAK";
12654 default:
12655 if (binding >= STB_LOPROC && binding <= STB_HIPROC)
12656 snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
12657 binding);
12658 else if (binding >= STB_LOOS && binding <= STB_HIOS)
12659 {
12660 if (binding == STB_GNU_UNIQUE
12661 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU)
12662 return "UNIQUE";
12663 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
12664 }
12665 else
12666 snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
12667 return buff;
12668 }
12669 }
12670
12671 static const char *
12672 get_symbol_type (Filedata * filedata, unsigned int type)
12673 {
12674 static char buff[64];
12675
12676 switch (type)
12677 {
12678 case STT_NOTYPE: return "NOTYPE";
12679 case STT_OBJECT: return "OBJECT";
12680 case STT_FUNC: return "FUNC";
12681 case STT_SECTION: return "SECTION";
12682 case STT_FILE: return "FILE";
12683 case STT_COMMON: return "COMMON";
12684 case STT_TLS: return "TLS";
12685 case STT_RELC: return "RELC";
12686 case STT_SRELC: return "SRELC";
12687 default:
12688 if (type >= STT_LOPROC && type <= STT_HIPROC)
12689 {
12690 if (filedata->file_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
12691 return "THUMB_FUNC";
12692
12693 if (filedata->file_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
12694 return "REGISTER";
12695
12696 if (filedata->file_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
12697 return "PARISC_MILLI";
12698
12699 snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
12700 }
12701 else if (type >= STT_LOOS && type <= STT_HIOS)
12702 {
12703 if (filedata->file_header.e_machine == EM_PARISC)
12704 {
12705 if (type == STT_HP_OPAQUE)
12706 return "HP_OPAQUE";
12707 if (type == STT_HP_STUB)
12708 return "HP_STUB";
12709 }
12710
12711 if (type == STT_GNU_IFUNC
12712 && (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU
12713 || filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_FREEBSD))
12714 return "IFUNC";
12715
12716 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
12717 }
12718 else
12719 snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
12720 return buff;
12721 }
12722 }
12723
12724 static const char *
12725 get_symbol_visibility (unsigned int visibility)
12726 {
12727 switch (visibility)
12728 {
12729 case STV_DEFAULT: return "DEFAULT";
12730 case STV_INTERNAL: return "INTERNAL";
12731 case STV_HIDDEN: return "HIDDEN";
12732 case STV_PROTECTED: return "PROTECTED";
12733 default:
12734 error (_("Unrecognized visibility value: %u\n"), visibility);
12735 return _("<unknown>");
12736 }
12737 }
12738
12739 static const char *
12740 get_alpha_symbol_other (unsigned int other)
12741 {
12742 switch (other)
12743 {
12744 case STO_ALPHA_NOPV: return "NOPV";
12745 case STO_ALPHA_STD_GPLOAD: return "STD GPLOAD";
12746 default:
12747 error (_("Unrecognized alpha specific other value: %u\n"), other);
12748 return _("<unknown>");
12749 }
12750 }
12751
12752 static const char *
12753 get_solaris_symbol_visibility (unsigned int visibility)
12754 {
12755 switch (visibility)
12756 {
12757 case 4: return "EXPORTED";
12758 case 5: return "SINGLETON";
12759 case 6: return "ELIMINATE";
12760 default: return get_symbol_visibility (visibility);
12761 }
12762 }
12763
12764 static const char *
12765 get_aarch64_symbol_other (unsigned int other)
12766 {
12767 static char buf[32];
12768
12769 if (other & STO_AARCH64_VARIANT_PCS)
12770 {
12771 other &= ~STO_AARCH64_VARIANT_PCS;
12772 if (other == 0)
12773 return "VARIANT_PCS";
12774 snprintf (buf, sizeof buf, "VARIANT_PCS | %x", other);
12775 return buf;
12776 }
12777 return NULL;
12778 }
12779
12780 static const char *
12781 get_mips_symbol_other (unsigned int other)
12782 {
12783 switch (other)
12784 {
12785 case STO_OPTIONAL: return "OPTIONAL";
12786 case STO_MIPS_PLT: return "MIPS PLT";
12787 case STO_MIPS_PIC: return "MIPS PIC";
12788 case STO_MICROMIPS: return "MICROMIPS";
12789 case STO_MICROMIPS | STO_MIPS_PIC: return "MICROMIPS, MIPS PIC";
12790 case STO_MIPS16: return "MIPS16";
12791 default: return NULL;
12792 }
12793 }
12794
12795 static const char *
12796 get_ia64_symbol_other (Filedata * filedata, unsigned int other)
12797 {
12798 if (is_ia64_vms (filedata))
12799 {
12800 static char res[32];
12801
12802 res[0] = 0;
12803
12804 /* Function types is for images and .STB files only. */
12805 switch (filedata->file_header.e_type)
12806 {
12807 case ET_DYN:
12808 case ET_EXEC:
12809 switch (VMS_ST_FUNC_TYPE (other))
12810 {
12811 case VMS_SFT_CODE_ADDR:
12812 strcat (res, " CA");
12813 break;
12814 case VMS_SFT_SYMV_IDX:
12815 strcat (res, " VEC");
12816 break;
12817 case VMS_SFT_FD:
12818 strcat (res, " FD");
12819 break;
12820 case VMS_SFT_RESERVE:
12821 strcat (res, " RSV");
12822 break;
12823 default:
12824 warn (_("Unrecognized IA64 VMS ST Function type: %d\n"),
12825 VMS_ST_FUNC_TYPE (other));
12826 strcat (res, " <unknown>");
12827 break;
12828 }
12829 break;
12830 default:
12831 break;
12832 }
12833 switch (VMS_ST_LINKAGE (other))
12834 {
12835 case VMS_STL_IGNORE:
12836 strcat (res, " IGN");
12837 break;
12838 case VMS_STL_RESERVE:
12839 strcat (res, " RSV");
12840 break;
12841 case VMS_STL_STD:
12842 strcat (res, " STD");
12843 break;
12844 case VMS_STL_LNK:
12845 strcat (res, " LNK");
12846 break;
12847 default:
12848 warn (_("Unrecognized IA64 VMS ST Linkage: %d\n"),
12849 VMS_ST_LINKAGE (other));
12850 strcat (res, " <unknown>");
12851 break;
12852 }
12853
12854 if (res[0] != 0)
12855 return res + 1;
12856 else
12857 return res;
12858 }
12859 return NULL;
12860 }
12861
12862 static const char *
12863 get_ppc64_symbol_other (unsigned int other)
12864 {
12865 if ((other & ~STO_PPC64_LOCAL_MASK) != 0)
12866 return NULL;
12867
12868 other >>= STO_PPC64_LOCAL_BIT;
12869 if (other <= 6)
12870 {
12871 static char buf[64];
12872 if (other >= 2)
12873 other = ppc64_decode_local_entry (other);
12874 snprintf (buf, sizeof buf, _("<localentry>: %d"), other);
12875 return buf;
12876 }
12877 return NULL;
12878 }
12879
12880 static const char *
12881 get_riscv_symbol_other (unsigned int other)
12882 {
12883 static char buf[32];
12884 buf[0] = 0;
12885
12886 if (other & STO_RISCV_VARIANT_CC)
12887 {
12888 strcat (buf, _(" VARIANT_CC"));
12889 other &= ~STO_RISCV_VARIANT_CC;
12890 }
12891
12892 if (other != 0)
12893 snprintf (buf, sizeof buf, " %x", other);
12894
12895
12896 if (buf[0] != 0)
12897 return buf + 1;
12898 else
12899 return buf;
12900 }
12901
12902 static const char *
12903 get_symbol_other (Filedata * filedata, unsigned int other)
12904 {
12905 const char * result = NULL;
12906 static char buff [64];
12907
12908 if (other == 0)
12909 return "";
12910
12911 switch (filedata->file_header.e_machine)
12912 {
12913 case EM_ALPHA:
12914 result = get_alpha_symbol_other (other);
12915 break;
12916 case EM_AARCH64:
12917 result = get_aarch64_symbol_other (other);
12918 break;
12919 case EM_MIPS:
12920 result = get_mips_symbol_other (other);
12921 break;
12922 case EM_IA_64:
12923 result = get_ia64_symbol_other (filedata, other);
12924 break;
12925 case EM_PPC64:
12926 result = get_ppc64_symbol_other (other);
12927 break;
12928 case EM_RISCV:
12929 result = get_riscv_symbol_other (other);
12930 break;
12931 default:
12932 result = NULL;
12933 break;
12934 }
12935
12936 if (result)
12937 return result;
12938
12939 snprintf (buff, sizeof buff, _("<other>: %x"), other);
12940 return buff;
12941 }
12942
12943 static const char *
12944 get_symbol_index_type (Filedata * filedata, unsigned int type)
12945 {
12946 static char buff[32];
12947
12948 switch (type)
12949 {
12950 case SHN_UNDEF: return "UND";
12951 case SHN_ABS: return "ABS";
12952 case SHN_COMMON: return "COM";
12953 default:
12954 if (type == SHN_IA_64_ANSI_COMMON
12955 && filedata->file_header.e_machine == EM_IA_64
12956 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX)
12957 return "ANSI_COM";
12958 else if ((filedata->file_header.e_machine == EM_X86_64
12959 || filedata->file_header.e_machine == EM_L1OM
12960 || filedata->file_header.e_machine == EM_K1OM)
12961 && type == SHN_X86_64_LCOMMON)
12962 return "LARGE_COM";
12963 else if ((type == SHN_MIPS_SCOMMON
12964 && filedata->file_header.e_machine == EM_MIPS)
12965 || (type == SHN_TIC6X_SCOMMON
12966 && filedata->file_header.e_machine == EM_TI_C6000))
12967 return "SCOM";
12968 else if (type == SHN_MIPS_SUNDEFINED
12969 && filedata->file_header.e_machine == EM_MIPS)
12970 return "SUND";
12971 else if (type >= SHN_LOPROC && type <= SHN_HIPROC)
12972 sprintf (buff, "PRC[0x%04x]", type & 0xffff);
12973 else if (type >= SHN_LOOS && type <= SHN_HIOS)
12974 sprintf (buff, "OS [0x%04x]", type & 0xffff);
12975 else if (type >= SHN_LORESERVE)
12976 sprintf (buff, "RSV[0x%04x]", type & 0xffff);
12977 else if (filedata->file_header.e_shnum != 0
12978 && type >= filedata->file_header.e_shnum)
12979 sprintf (buff, _("bad section index[%3d]"), type);
12980 else
12981 sprintf (buff, "%3d", type);
12982 break;
12983 }
12984
12985 return buff;
12986 }
12987
12988 static const char *
12989 get_symbol_version_string (Filedata * filedata,
12990 bool is_dynsym,
12991 const char * strtab,
12992 unsigned long int strtab_size,
12993 unsigned int si,
12994 Elf_Internal_Sym * psym,
12995 enum versioned_symbol_info * sym_info,
12996 unsigned short * vna_other)
12997 {
12998 unsigned char data[2];
12999 unsigned short vers_data;
13000 unsigned long offset;
13001 unsigned short max_vd_ndx;
13002
13003 if (!is_dynsym
13004 || filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)] == 0)
13005 return NULL;
13006
13007 offset = offset_from_vma (filedata,
13008 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
13009 sizeof data + si * sizeof (vers_data));
13010
13011 if (get_data (&data, filedata, offset + si * sizeof (vers_data),
13012 sizeof (data), 1, _("version data")) == NULL)
13013 return NULL;
13014
13015 vers_data = byte_get (data, 2);
13016
13017 if ((vers_data & VERSYM_HIDDEN) == 0 && vers_data == 0)
13018 return NULL;
13019
13020 *sym_info = (vers_data & VERSYM_HIDDEN) != 0 ? symbol_hidden : symbol_public;
13021 max_vd_ndx = 0;
13022
13023 /* Usually we'd only see verdef for defined symbols, and verneed for
13024 undefined symbols. However, symbols defined by the linker in
13025 .dynbss for variables copied from a shared library in order to
13026 avoid text relocations are defined yet have verneed. We could
13027 use a heuristic to detect the special case, for example, check
13028 for verneed first on symbols defined in SHT_NOBITS sections, but
13029 it is simpler and more reliable to just look for both verdef and
13030 verneed. .dynbss might not be mapped to a SHT_NOBITS section. */
13031
13032 if (psym->st_shndx != SHN_UNDEF
13033 && vers_data != 0x8001
13034 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
13035 {
13036 Elf_Internal_Verdef ivd;
13037 Elf_Internal_Verdaux ivda;
13038 Elf_External_Verdaux evda;
13039 unsigned long off;
13040
13041 off = offset_from_vma (filedata,
13042 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
13043 sizeof (Elf_External_Verdef));
13044
13045 do
13046 {
13047 Elf_External_Verdef evd;
13048
13049 if (get_data (&evd, filedata, off, sizeof (evd), 1,
13050 _("version def")) == NULL)
13051 {
13052 ivd.vd_ndx = 0;
13053 ivd.vd_aux = 0;
13054 ivd.vd_next = 0;
13055 ivd.vd_flags = 0;
13056 }
13057 else
13058 {
13059 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
13060 ivd.vd_aux = BYTE_GET (evd.vd_aux);
13061 ivd.vd_next = BYTE_GET (evd.vd_next);
13062 ivd.vd_flags = BYTE_GET (evd.vd_flags);
13063 }
13064
13065 if ((ivd.vd_ndx & VERSYM_VERSION) > max_vd_ndx)
13066 max_vd_ndx = ivd.vd_ndx & VERSYM_VERSION;
13067
13068 off += ivd.vd_next;
13069 }
13070 while (ivd.vd_ndx != (vers_data & VERSYM_VERSION) && ivd.vd_next != 0);
13071
13072 if (ivd.vd_ndx == (vers_data & VERSYM_VERSION))
13073 {
13074 if (ivd.vd_ndx == 1 && ivd.vd_flags == VER_FLG_BASE)
13075 return NULL;
13076
13077 off -= ivd.vd_next;
13078 off += ivd.vd_aux;
13079
13080 if (get_data (&evda, filedata, off, sizeof (evda), 1,
13081 _("version def aux")) != NULL)
13082 {
13083 ivda.vda_name = BYTE_GET (evda.vda_name);
13084
13085 if (psym->st_name != ivda.vda_name)
13086 return (ivda.vda_name < strtab_size
13087 ? strtab + ivda.vda_name : _("<corrupt>"));
13088 }
13089 }
13090 }
13091
13092 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
13093 {
13094 Elf_External_Verneed evn;
13095 Elf_Internal_Verneed ivn;
13096 Elf_Internal_Vernaux ivna;
13097
13098 offset = offset_from_vma (filedata,
13099 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
13100 sizeof evn);
13101 do
13102 {
13103 unsigned long vna_off;
13104
13105 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
13106 _("version need")) == NULL)
13107 {
13108 ivna.vna_next = 0;
13109 ivna.vna_other = 0;
13110 ivna.vna_name = 0;
13111 break;
13112 }
13113
13114 ivn.vn_aux = BYTE_GET (evn.vn_aux);
13115 ivn.vn_next = BYTE_GET (evn.vn_next);
13116
13117 vna_off = offset + ivn.vn_aux;
13118
13119 do
13120 {
13121 Elf_External_Vernaux evna;
13122
13123 if (get_data (&evna, filedata, vna_off, sizeof (evna), 1,
13124 _("version need aux (3)")) == NULL)
13125 {
13126 ivna.vna_next = 0;
13127 ivna.vna_other = 0;
13128 ivna.vna_name = 0;
13129 }
13130 else
13131 {
13132 ivna.vna_other = BYTE_GET (evna.vna_other);
13133 ivna.vna_next = BYTE_GET (evna.vna_next);
13134 ivna.vna_name = BYTE_GET (evna.vna_name);
13135 }
13136
13137 vna_off += ivna.vna_next;
13138 }
13139 while (ivna.vna_other != vers_data && ivna.vna_next != 0);
13140
13141 if (ivna.vna_other == vers_data)
13142 break;
13143
13144 offset += ivn.vn_next;
13145 }
13146 while (ivn.vn_next != 0);
13147
13148 if (ivna.vna_other == vers_data)
13149 {
13150 *sym_info = symbol_undefined;
13151 *vna_other = ivna.vna_other;
13152 return (ivna.vna_name < strtab_size
13153 ? strtab + ivna.vna_name : _("<corrupt>"));
13154 }
13155 else if ((max_vd_ndx || (vers_data & VERSYM_VERSION) != 1)
13156 && (vers_data & VERSYM_VERSION) > max_vd_ndx)
13157 return _("<corrupt>");
13158 }
13159 return NULL;
13160 }
13161
13162 /* Display a symbol size on stdout. Format is based on --sym-base setting. */
13163
13164 static unsigned int
13165 print_dynamic_symbol_size (uint64_t vma, int base)
13166 {
13167 switch (base)
13168 {
13169 case 8:
13170 return print_vma (vma, OCTAL_5);
13171
13172 case 10:
13173 return print_vma (vma, UNSIGNED_5);
13174
13175 case 16:
13176 return print_vma (vma, PREFIX_HEX_5);
13177
13178 case 0:
13179 default:
13180 return print_vma (vma, DEC_5);
13181 }
13182 }
13183
13184 static void
13185 print_dynamic_symbol (Filedata *filedata, unsigned long si,
13186 Elf_Internal_Sym *symtab,
13187 Elf_Internal_Shdr *section,
13188 char *strtab, size_t strtab_size)
13189 {
13190 const char *version_string;
13191 enum versioned_symbol_info sym_info;
13192 unsigned short vna_other;
13193 bool is_valid;
13194 const char * sstr;
13195 Elf_Internal_Sym *psym = symtab + si;
13196
13197 printf ("%6ld: ", si);
13198 print_vma (psym->st_value, LONG_HEX);
13199 putchar (' ');
13200 print_dynamic_symbol_size (psym->st_size, sym_base);
13201 printf (" %-7s", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
13202 printf (" %-6s", get_symbol_binding (filedata, ELF_ST_BIND (psym->st_info)));
13203 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
13204 printf (" %-7s", get_solaris_symbol_visibility (psym->st_other));
13205 else
13206 {
13207 unsigned int vis = ELF_ST_VISIBILITY (psym->st_other);
13208
13209 printf (" %-7s", get_symbol_visibility (vis));
13210 /* Check to see if any other bits in the st_other field are set.
13211 Note - displaying this information disrupts the layout of the
13212 table being generated, but for the moment this case is very rare. */
13213 if (psym->st_other ^ vis)
13214 printf (" [%s] ", get_symbol_other (filedata, psym->st_other ^ vis));
13215 }
13216 printf (" %4s ", get_symbol_index_type (filedata, psym->st_shndx));
13217
13218 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION
13219 && psym->st_shndx < filedata->file_header.e_shnum
13220 && filedata->section_headers != NULL
13221 && psym->st_name == 0)
13222 {
13223 is_valid
13224 = section_name_valid (filedata,
13225 filedata->section_headers + psym->st_shndx);
13226 sstr = is_valid ?
13227 section_name_print (filedata,
13228 filedata->section_headers + psym->st_shndx)
13229 : _("<corrupt>");
13230 }
13231 else
13232 {
13233 is_valid = valid_symbol_name (strtab, strtab_size, psym->st_name);
13234 sstr = is_valid ? strtab + psym->st_name : _("<corrupt>");
13235 }
13236
13237 version_string
13238 = get_symbol_version_string (filedata,
13239 (section == NULL
13240 || section->sh_type == SHT_DYNSYM),
13241 strtab, strtab_size, si,
13242 psym, &sym_info, &vna_other);
13243
13244 int len_avail = 21;
13245 if (! do_wide && version_string != NULL)
13246 {
13247 char buffer[16];
13248
13249 len_avail -= 1 + strlen (version_string);
13250
13251 if (sym_info == symbol_undefined)
13252 len_avail -= sprintf (buffer," (%d)", vna_other);
13253 else if (sym_info != symbol_hidden)
13254 len_avail -= 1;
13255 }
13256
13257 print_symbol (len_avail, sstr);
13258
13259 if (version_string)
13260 {
13261 if (sym_info == symbol_undefined)
13262 printf ("@%s (%d)", version_string, vna_other);
13263 else
13264 printf (sym_info == symbol_hidden ? "@%s" : "@@%s",
13265 version_string);
13266 }
13267
13268 putchar ('\n');
13269
13270 if (ELF_ST_BIND (psym->st_info) == STB_LOCAL
13271 && section != NULL
13272 && si >= section->sh_info
13273 /* Irix 5 and 6 MIPS binaries are known to ignore this requirement. */
13274 && filedata->file_header.e_machine != EM_MIPS
13275 /* Solaris binaries have been found to violate this requirement as
13276 well. Not sure if this is a bug or an ABI requirement. */
13277 && filedata->file_header.e_ident[EI_OSABI] != ELFOSABI_SOLARIS)
13278 warn (_("local symbol %lu found at index >= %s's sh_info value of %u\n"),
13279 si, printable_section_name (filedata, section), section->sh_info);
13280 }
13281
13282 static const char *
13283 get_lto_kind (unsigned int kind)
13284 {
13285 switch (kind)
13286 {
13287 case 0: return "DEF";
13288 case 1: return "WEAKDEF";
13289 case 2: return "UNDEF";
13290 case 3: return "WEAKUNDEF";
13291 case 4: return "COMMON";
13292 default:
13293 break;
13294 }
13295
13296 static char buffer[30];
13297 error (_("Unknown LTO symbol definition encountered: %u\n"), kind);
13298 sprintf (buffer, "<unknown: %u>", kind);
13299 return buffer;
13300 }
13301
13302 static const char *
13303 get_lto_visibility (unsigned int visibility)
13304 {
13305 switch (visibility)
13306 {
13307 case 0: return "DEFAULT";
13308 case 1: return "PROTECTED";
13309 case 2: return "INTERNAL";
13310 case 3: return "HIDDEN";
13311 default:
13312 break;
13313 }
13314
13315 static char buffer[30];
13316 error (_("Unknown LTO symbol visibility encountered: %u\n"), visibility);
13317 sprintf (buffer, "<unknown: %u>", visibility);
13318 return buffer;
13319 }
13320
13321 static const char *
13322 get_lto_sym_type (unsigned int sym_type)
13323 {
13324 switch (sym_type)
13325 {
13326 case 0: return "UNKNOWN";
13327 case 1: return "FUNCTION";
13328 case 2: return "VARIABLE";
13329 default:
13330 break;
13331 }
13332
13333 static char buffer[30];
13334 error (_("Unknown LTO symbol type encountered: %u\n"), sym_type);
13335 sprintf (buffer, "<unknown: %u>", sym_type);
13336 return buffer;
13337 }
13338
13339 /* Display an LTO format symbol table.
13340 FIXME: The format of LTO symbol tables is not formalized.
13341 So this code could need changing in the future. */
13342
13343 static bool
13344 display_lto_symtab (Filedata * filedata,
13345 Elf_Internal_Shdr * section)
13346 {
13347 if (section->sh_size == 0)
13348 {
13349 if (filedata->is_separate)
13350 printf (_("\nThe LTO Symbol table section '%s' in linked file '%s' is empty!\n"),
13351 printable_section_name (filedata, section),
13352 filedata->file_name);
13353 else
13354 printf (_("\nLTO Symbol table '%s' is empty!\n"),
13355 printable_section_name (filedata, section));
13356
13357 return true;
13358 }
13359
13360 if (section->sh_size > filedata->file_size)
13361 {
13362 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
13363 printable_section_name (filedata, section),
13364 (unsigned long) section->sh_size);
13365 return false;
13366 }
13367
13368 void * alloced_data = get_data (NULL, filedata, section->sh_offset,
13369 section->sh_size, 1, _("LTO symbols"));
13370 if (alloced_data == NULL)
13371 return false;
13372
13373 /* Look for extended data for the symbol table. */
13374 Elf_Internal_Shdr * ext;
13375 void * ext_data_orig = NULL;
13376 char * ext_data = NULL;
13377 char * ext_data_end = NULL;
13378 char * ext_name = NULL;
13379
13380 if (asprintf (& ext_name, ".gnu.lto_.ext_symtab.%s",
13381 (section_name (filedata, section)
13382 + sizeof (".gnu.lto_.symtab.") - 1)) > 0
13383 && ext_name != NULL /* Paranoia. */
13384 && (ext = find_section (filedata, ext_name)) != NULL)
13385 {
13386 if (ext->sh_size < 3)
13387 error (_("LTO Symbol extension table '%s' is empty!\n"),
13388 printable_section_name (filedata, ext));
13389 else
13390 {
13391 ext_data_orig = ext_data = get_data (NULL, filedata, ext->sh_offset,
13392 ext->sh_size, 1,
13393 _("LTO ext symbol data"));
13394 if (ext_data != NULL)
13395 {
13396 ext_data_end = ext_data + ext->sh_size;
13397 if (* ext_data++ != 1)
13398 error (_("Unexpected version number in symbol extension table\n"));
13399 }
13400 }
13401 }
13402
13403 const unsigned char * data = (const unsigned char *) alloced_data;
13404 const unsigned char * end = data + section->sh_size;
13405
13406 if (filedata->is_separate)
13407 printf (_("\nIn linked file '%s': "), filedata->file_name);
13408 else
13409 printf ("\n");
13410
13411 if (ext_data_orig != NULL)
13412 {
13413 if (do_wide)
13414 printf (_("LTO Symbol table '%s' and extension table '%s' contain:\n"),
13415 printable_section_name (filedata, section),
13416 printable_section_name (filedata, ext));
13417 else
13418 {
13419 printf (_("LTO Symbol table '%s'\n"),
13420 printable_section_name (filedata, section));
13421 printf (_(" and extension table '%s' contain:\n"),
13422 printable_section_name (filedata, ext));
13423 }
13424 }
13425 else
13426 printf (_("LTO Symbol table '%s' contains:\n"),
13427 printable_section_name (filedata, section));
13428
13429 /* FIXME: Add a wide version. */
13430 if (ext_data_orig != NULL)
13431 printf (_(" Comdat_Key Kind Visibility Size Slot Type Section Name\n"));
13432 else
13433 printf (_(" Comdat_Key Kind Visibility Size Slot Name\n"));
13434
13435 /* FIXME: We do not handle style prefixes. */
13436
13437 while (data < end)
13438 {
13439 const unsigned char * sym_name = data;
13440 data += strnlen ((const char *) sym_name, end - data) + 1;
13441 if (data >= end)
13442 goto fail;
13443
13444 const unsigned char * comdat_key = data;
13445 data += strnlen ((const char *) comdat_key, end - data) + 1;
13446 if (data >= end)
13447 goto fail;
13448
13449 if (data + 2 + 8 + 4 > end)
13450 goto fail;
13451
13452 unsigned int kind = *data++;
13453 unsigned int visibility = *data++;
13454
13455 uint64_t size = byte_get (data, 8);
13456 data += 8;
13457
13458 uint64_t slot = byte_get (data, 4);
13459 data += 4;
13460
13461 if (ext_data != NULL)
13462 {
13463 if (ext_data < (ext_data_end - 1))
13464 {
13465 unsigned int sym_type = * ext_data ++;
13466 unsigned int sec_kind = * ext_data ++;
13467
13468 printf (" %10s %10s %11s %08" PRIx64 " %08" PRIx64 " %9s %08x _",
13469 * comdat_key == 0 ? "-" : (char *) comdat_key,
13470 get_lto_kind (kind),
13471 get_lto_visibility (visibility),
13472 size,
13473 slot,
13474 get_lto_sym_type (sym_type),
13475 sec_kind);
13476 print_symbol (6, (const char *) sym_name);
13477 }
13478 else
13479 {
13480 error (_("Ran out of LTO symbol extension data\n"));
13481 ext_data = NULL;
13482 /* FIXME: return FAIL result ? */
13483 }
13484 }
13485 else
13486 {
13487 printf (" %10s %10s %11s %08" PRIx64 " %08" PRIx64 " _",
13488 * comdat_key == 0 ? "-" : (char *) comdat_key,
13489 get_lto_kind (kind),
13490 get_lto_visibility (visibility),
13491 size,
13492 slot);
13493 print_symbol (21, (const char *) sym_name);
13494 }
13495 putchar ('\n');
13496 }
13497
13498 if (ext_data != NULL && ext_data < ext_data_end)
13499 {
13500 error (_("Data remains in the LTO symbol extension table\n"));
13501 goto fail;
13502 }
13503
13504 free (alloced_data);
13505 free (ext_data_orig);
13506 free (ext_name);
13507 return true;
13508
13509 fail:
13510 error (_("Buffer overrun encountered whilst decoding LTO symbol table\n"));
13511 free (alloced_data);
13512 free (ext_data_orig);
13513 free (ext_name);
13514 return false;
13515 }
13516
13517 /* Display LTO symbol tables. */
13518
13519 static bool
13520 process_lto_symbol_tables (Filedata * filedata)
13521 {
13522 Elf_Internal_Shdr * section;
13523 unsigned int i;
13524 bool res = true;
13525
13526 if (!do_lto_syms)
13527 return true;
13528
13529 if (filedata->section_headers == NULL)
13530 return true;
13531
13532 for (i = 0, section = filedata->section_headers;
13533 i < filedata->file_header.e_shnum;
13534 i++, section++)
13535 if (section_name_valid (filedata, section)
13536 && startswith (section_name (filedata, section), ".gnu.lto_.symtab."))
13537 res &= display_lto_symtab (filedata, section);
13538
13539 return res;
13540 }
13541
13542 /* Dump the symbol table. */
13543
13544 static bool
13545 process_symbol_table (Filedata * filedata)
13546 {
13547 Elf_Internal_Shdr * section;
13548
13549 if (!do_syms && !do_dyn_syms && !do_histogram)
13550 return true;
13551
13552 if ((filedata->dynamic_info[DT_HASH] || filedata->dynamic_info_DT_GNU_HASH)
13553 && do_syms
13554 && do_using_dynamic
13555 && filedata->dynamic_strings != NULL
13556 && filedata->dynamic_symbols != NULL)
13557 {
13558 unsigned long si;
13559
13560 if (filedata->is_separate)
13561 {
13562 printf (ngettext ("\nIn linked file '%s' the dynamic symbol table contains %lu entry:\n",
13563 "\nIn linked file '%s' the dynamic symbol table contains %lu entries:\n",
13564 filedata->num_dynamic_syms),
13565 filedata->file_name,
13566 filedata->num_dynamic_syms);
13567 }
13568 else
13569 {
13570 printf (ngettext ("\nSymbol table for image contains %lu entry:\n",
13571 "\nSymbol table for image contains %lu entries:\n",
13572 filedata->num_dynamic_syms),
13573 filedata->num_dynamic_syms);
13574 }
13575 if (is_32bit_elf)
13576 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13577 else
13578 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13579
13580 for (si = 0; si < filedata->num_dynamic_syms; si++)
13581 print_dynamic_symbol (filedata, si, filedata->dynamic_symbols, NULL,
13582 filedata->dynamic_strings,
13583 filedata->dynamic_strings_length);
13584 }
13585 else if ((do_dyn_syms || (do_syms && !do_using_dynamic))
13586 && filedata->section_headers != NULL)
13587 {
13588 unsigned int i;
13589
13590 for (i = 0, section = filedata->section_headers;
13591 i < filedata->file_header.e_shnum;
13592 i++, section++)
13593 {
13594 char * strtab = NULL;
13595 unsigned long int strtab_size = 0;
13596 Elf_Internal_Sym * symtab;
13597 unsigned long si, num_syms;
13598
13599 if ((section->sh_type != SHT_SYMTAB
13600 && section->sh_type != SHT_DYNSYM)
13601 || (!do_syms
13602 && section->sh_type == SHT_SYMTAB))
13603 continue;
13604
13605 if (section->sh_entsize == 0)
13606 {
13607 printf (_("\nSymbol table '%s' has a sh_entsize of zero!\n"),
13608 printable_section_name (filedata, section));
13609 continue;
13610 }
13611
13612 num_syms = section->sh_size / section->sh_entsize;
13613
13614 if (filedata->is_separate)
13615 printf (ngettext ("\nIn linked file '%s' symbol section '%s' contains %lu entry:\n",
13616 "\nIn linked file '%s' symbol section '%s' contains %lu entries:\n",
13617 num_syms),
13618 filedata->file_name,
13619 printable_section_name (filedata, section),
13620 num_syms);
13621 else
13622 printf (ngettext ("\nSymbol table '%s' contains %lu entry:\n",
13623 "\nSymbol table '%s' contains %lu entries:\n",
13624 num_syms),
13625 printable_section_name (filedata, section),
13626 num_syms);
13627
13628 if (is_32bit_elf)
13629 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13630 else
13631 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13632
13633 symtab = get_elf_symbols (filedata, section, & num_syms);
13634 if (symtab == NULL)
13635 continue;
13636
13637 if (section->sh_link == filedata->file_header.e_shstrndx)
13638 {
13639 strtab = filedata->string_table;
13640 strtab_size = filedata->string_table_length;
13641 }
13642 else if (section->sh_link < filedata->file_header.e_shnum)
13643 {
13644 Elf_Internal_Shdr * string_sec;
13645
13646 string_sec = filedata->section_headers + section->sh_link;
13647
13648 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset,
13649 1, string_sec->sh_size,
13650 _("string table"));
13651 strtab_size = strtab != NULL ? string_sec->sh_size : 0;
13652 }
13653
13654 for (si = 0; si < num_syms; si++)
13655 print_dynamic_symbol (filedata, si, symtab, section,
13656 strtab, strtab_size);
13657
13658 free (symtab);
13659 if (strtab != filedata->string_table)
13660 free (strtab);
13661 }
13662 }
13663 else if (do_syms)
13664 printf
13665 (_("\nDynamic symbol information is not available for displaying symbols.\n"));
13666
13667 if (do_histogram && filedata->buckets != NULL)
13668 {
13669 unsigned long * lengths;
13670 unsigned long * counts;
13671 unsigned long hn;
13672 uint64_t si;
13673 unsigned long maxlength = 0;
13674 unsigned long nzero_counts = 0;
13675 unsigned long nsyms = 0;
13676 char *visited;
13677
13678 printf (ngettext ("\nHistogram for bucket list length "
13679 "(total of %lu bucket):\n",
13680 "\nHistogram for bucket list length "
13681 "(total of %lu buckets):\n",
13682 (unsigned long) filedata->nbuckets),
13683 (unsigned long) filedata->nbuckets);
13684
13685 lengths = (unsigned long *) calloc (filedata->nbuckets,
13686 sizeof (*lengths));
13687 if (lengths == NULL)
13688 {
13689 error (_("Out of memory allocating space for histogram buckets\n"));
13690 goto err_out;
13691 }
13692 visited = xcmalloc (filedata->nchains, 1);
13693 memset (visited, 0, filedata->nchains);
13694
13695 printf (_(" Length Number %% of total Coverage\n"));
13696 for (hn = 0; hn < filedata->nbuckets; ++hn)
13697 {
13698 for (si = filedata->buckets[hn]; si > 0; si = filedata->chains[si])
13699 {
13700 ++nsyms;
13701 if (maxlength < ++lengths[hn])
13702 ++maxlength;
13703 if (si >= filedata->nchains || visited[si])
13704 {
13705 error (_("histogram chain is corrupt\n"));
13706 break;
13707 }
13708 visited[si] = 1;
13709 }
13710 }
13711 free (visited);
13712
13713 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
13714 if (counts == NULL)
13715 {
13716 free (lengths);
13717 error (_("Out of memory allocating space for histogram counts\n"));
13718 goto err_out;
13719 }
13720
13721 for (hn = 0; hn < filedata->nbuckets; ++hn)
13722 ++counts[lengths[hn]];
13723
13724 if (filedata->nbuckets > 0)
13725 {
13726 unsigned long i;
13727 printf (" 0 %-10lu (%5.1f%%)\n",
13728 counts[0], (counts[0] * 100.0) / filedata->nbuckets);
13729 for (i = 1; i <= maxlength; ++i)
13730 {
13731 nzero_counts += counts[i] * i;
13732 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
13733 i, counts[i], (counts[i] * 100.0) / filedata->nbuckets,
13734 (nzero_counts * 100.0) / nsyms);
13735 }
13736 }
13737
13738 free (counts);
13739 free (lengths);
13740 }
13741
13742 free (filedata->buckets);
13743 filedata->buckets = NULL;
13744 filedata->nbuckets = 0;
13745 free (filedata->chains);
13746 filedata->chains = NULL;
13747
13748 if (do_histogram && filedata->gnubuckets != NULL)
13749 {
13750 unsigned long * lengths;
13751 unsigned long * counts;
13752 unsigned long hn;
13753 unsigned long maxlength = 0;
13754 unsigned long nzero_counts = 0;
13755 unsigned long nsyms = 0;
13756
13757 printf (ngettext ("\nHistogram for `%s' bucket list length "
13758 "(total of %lu bucket):\n",
13759 "\nHistogram for `%s' bucket list length "
13760 "(total of %lu buckets):\n",
13761 (unsigned long) filedata->ngnubuckets),
13762 GNU_HASH_SECTION_NAME (filedata),
13763 (unsigned long) filedata->ngnubuckets);
13764
13765 lengths = (unsigned long *) calloc (filedata->ngnubuckets,
13766 sizeof (*lengths));
13767 if (lengths == NULL)
13768 {
13769 error (_("Out of memory allocating space for gnu histogram buckets\n"));
13770 goto err_out;
13771 }
13772
13773 printf (_(" Length Number %% of total Coverage\n"));
13774
13775 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
13776 if (filedata->gnubuckets[hn] != 0)
13777 {
13778 uint64_t off, length = 1;
13779
13780 for (off = filedata->gnubuckets[hn] - filedata->gnusymidx;
13781 /* PR 17531 file: 010-77222-0.004. */
13782 off < filedata->ngnuchains
13783 && (filedata->gnuchains[off] & 1) == 0;
13784 ++off)
13785 ++length;
13786 lengths[hn] = length;
13787 if (length > maxlength)
13788 maxlength = length;
13789 nsyms += length;
13790 }
13791
13792 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
13793 if (counts == NULL)
13794 {
13795 free (lengths);
13796 error (_("Out of memory allocating space for gnu histogram counts\n"));
13797 goto err_out;
13798 }
13799
13800 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
13801 ++counts[lengths[hn]];
13802
13803 if (filedata->ngnubuckets > 0)
13804 {
13805 unsigned long j;
13806 printf (" 0 %-10lu (%5.1f%%)\n",
13807 counts[0], (counts[0] * 100.0) / filedata->ngnubuckets);
13808 for (j = 1; j <= maxlength; ++j)
13809 {
13810 nzero_counts += counts[j] * j;
13811 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
13812 j, counts[j], (counts[j] * 100.0) / filedata->ngnubuckets,
13813 (nzero_counts * 100.0) / nsyms);
13814 }
13815 }
13816
13817 free (counts);
13818 free (lengths);
13819 }
13820 free (filedata->gnubuckets);
13821 filedata->gnubuckets = NULL;
13822 filedata->ngnubuckets = 0;
13823 free (filedata->gnuchains);
13824 filedata->gnuchains = NULL;
13825 filedata->ngnuchains = 0;
13826 free (filedata->mipsxlat);
13827 filedata->mipsxlat = NULL;
13828 return true;
13829
13830 err_out:
13831 free (filedata->gnubuckets);
13832 filedata->gnubuckets = NULL;
13833 filedata->ngnubuckets = 0;
13834 free (filedata->gnuchains);
13835 filedata->gnuchains = NULL;
13836 filedata->ngnuchains = 0;
13837 free (filedata->mipsxlat);
13838 filedata->mipsxlat = NULL;
13839 free (filedata->buckets);
13840 filedata->buckets = NULL;
13841 filedata->nbuckets = 0;
13842 free (filedata->chains);
13843 filedata->chains = NULL;
13844 return false;
13845 }
13846
13847 static bool
13848 process_syminfo (Filedata * filedata)
13849 {
13850 unsigned int i;
13851
13852 if (filedata->dynamic_syminfo == NULL
13853 || !do_dynamic)
13854 /* No syminfo, this is ok. */
13855 return true;
13856
13857 /* There better should be a dynamic symbol section. */
13858 if (filedata->dynamic_symbols == NULL || filedata->dynamic_strings == NULL)
13859 return false;
13860
13861 if (filedata->is_separate)
13862 printf (ngettext ("\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entry:\n",
13863 "\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entries:\n",
13864 filedata->dynamic_syminfo_nent),
13865 filedata->file_name,
13866 filedata->dynamic_syminfo_offset,
13867 filedata->dynamic_syminfo_nent);
13868 else
13869 printf (ngettext ("\nDynamic info segment at offset 0x%lx "
13870 "contains %d entry:\n",
13871 "\nDynamic info segment at offset 0x%lx "
13872 "contains %d entries:\n",
13873 filedata->dynamic_syminfo_nent),
13874 filedata->dynamic_syminfo_offset,
13875 filedata->dynamic_syminfo_nent);
13876
13877 printf (_(" Num: Name BoundTo Flags\n"));
13878 for (i = 0; i < filedata->dynamic_syminfo_nent; ++i)
13879 {
13880 unsigned short int flags = filedata->dynamic_syminfo[i].si_flags;
13881
13882 printf ("%4d: ", i);
13883 if (i >= filedata->num_dynamic_syms)
13884 printf (_("<corrupt index>"));
13885 else if (valid_dynamic_name (filedata, filedata->dynamic_symbols[i].st_name))
13886 print_symbol (30, get_dynamic_name (filedata,
13887 filedata->dynamic_symbols[i].st_name));
13888 else
13889 printf (_("<corrupt: %19ld>"), filedata->dynamic_symbols[i].st_name);
13890 putchar (' ');
13891
13892 switch (filedata->dynamic_syminfo[i].si_boundto)
13893 {
13894 case SYMINFO_BT_SELF:
13895 fputs ("SELF ", stdout);
13896 break;
13897 case SYMINFO_BT_PARENT:
13898 fputs ("PARENT ", stdout);
13899 break;
13900 default:
13901 if (filedata->dynamic_syminfo[i].si_boundto > 0
13902 && filedata->dynamic_syminfo[i].si_boundto < filedata->dynamic_nent
13903 && valid_dynamic_name (filedata,
13904 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val))
13905 {
13906 print_symbol (10, get_dynamic_name (filedata,
13907 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val));
13908 putchar (' ' );
13909 }
13910 else
13911 printf ("%-10d ", filedata->dynamic_syminfo[i].si_boundto);
13912 break;
13913 }
13914
13915 if (flags & SYMINFO_FLG_DIRECT)
13916 printf (" DIRECT");
13917 if (flags & SYMINFO_FLG_PASSTHRU)
13918 printf (" PASSTHRU");
13919 if (flags & SYMINFO_FLG_COPY)
13920 printf (" COPY");
13921 if (flags & SYMINFO_FLG_LAZYLOAD)
13922 printf (" LAZYLOAD");
13923
13924 puts ("");
13925 }
13926
13927 return true;
13928 }
13929
13930 /* A macro which evaluates to TRUE if the region ADDR .. ADDR + NELEM
13931 is contained by the region START .. END. The types of ADDR, START
13932 and END should all be the same. Note both ADDR + NELEM and END
13933 point to just beyond the end of the regions that are being tested. */
13934 #define IN_RANGE(START,END,ADDR,NELEM) \
13935 (((ADDR) >= (START)) && ((ADDR) < (END)) && ((ADDR) + (NELEM) <= (END)))
13936
13937 /* Check to see if the given reloc needs to be handled in a target specific
13938 manner. If so then process the reloc and return TRUE otherwise return
13939 FALSE.
13940
13941 If called with reloc == NULL, then this is a signal that reloc processing
13942 for the current section has finished, and any saved state should be
13943 discarded. */
13944
13945 static bool
13946 target_specific_reloc_handling (Filedata * filedata,
13947 Elf_Internal_Rela * reloc,
13948 unsigned char * start,
13949 unsigned char * end,
13950 Elf_Internal_Sym * symtab,
13951 unsigned long num_syms)
13952 {
13953 unsigned int reloc_type = 0;
13954 unsigned long sym_index = 0;
13955
13956 if (reloc)
13957 {
13958 reloc_type = get_reloc_type (filedata, reloc->r_info);
13959 sym_index = get_reloc_symindex (reloc->r_info);
13960 }
13961
13962 switch (filedata->file_header.e_machine)
13963 {
13964 case EM_MSP430:
13965 case EM_MSP430_OLD:
13966 {
13967 static Elf_Internal_Sym * saved_sym = NULL;
13968
13969 if (reloc == NULL)
13970 {
13971 saved_sym = NULL;
13972 return true;
13973 }
13974
13975 switch (reloc_type)
13976 {
13977 case 10: /* R_MSP430_SYM_DIFF */
13978 case 12: /* R_MSP430_GNU_SUB_ULEB128 */
13979 if (uses_msp430x_relocs (filedata))
13980 break;
13981 /* Fall through. */
13982 case 21: /* R_MSP430X_SYM_DIFF */
13983 case 23: /* R_MSP430X_GNU_SUB_ULEB128 */
13984 /* PR 21139. */
13985 if (sym_index >= num_syms)
13986 error (_("MSP430 SYM_DIFF reloc contains invalid symbol index %lu\n"),
13987 sym_index);
13988 else
13989 saved_sym = symtab + sym_index;
13990 return true;
13991
13992 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
13993 case 3: /* R_MSP430_16 or R_MSP430_ABS8 */
13994 goto handle_sym_diff;
13995
13996 case 5: /* R_MSP430_16_BYTE */
13997 case 9: /* R_MSP430_8 */
13998 case 11: /* R_MSP430_GNU_SET_ULEB128 */
13999 if (uses_msp430x_relocs (filedata))
14000 break;
14001 goto handle_sym_diff;
14002
14003 case 2: /* R_MSP430_ABS16 */
14004 case 15: /* R_MSP430X_ABS16 */
14005 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
14006 if (! uses_msp430x_relocs (filedata))
14007 break;
14008 goto handle_sym_diff;
14009
14010 handle_sym_diff:
14011 if (saved_sym != NULL)
14012 {
14013 uint64_t value;
14014 unsigned int reloc_size = 0;
14015 int leb_ret = 0;
14016 switch (reloc_type)
14017 {
14018 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
14019 reloc_size = 4;
14020 break;
14021 case 11: /* R_MSP430_GNU_SET_ULEB128 */
14022 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
14023 if (reloc->r_offset < (size_t) (end - start))
14024 read_leb128 (start + reloc->r_offset, end, false,
14025 &reloc_size, &leb_ret);
14026 break;
14027 default:
14028 reloc_size = 2;
14029 break;
14030 }
14031
14032 if (leb_ret != 0 || reloc_size == 0 || reloc_size > 8)
14033 error (_("MSP430 ULEB128 field at 0x%lx contains invalid "
14034 "ULEB128 value\n"),
14035 (long) reloc->r_offset);
14036 else if (sym_index >= num_syms)
14037 error (_("MSP430 reloc contains invalid symbol index %lu\n"),
14038 sym_index);
14039 else
14040 {
14041 value = reloc->r_addend + (symtab[sym_index].st_value
14042 - saved_sym->st_value);
14043
14044 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
14045 byte_put (start + reloc->r_offset, value, reloc_size);
14046 else
14047 /* PR 21137 */
14048 error (_("MSP430 sym diff reloc contains invalid offset: 0x%lx\n"),
14049 (long) reloc->r_offset);
14050 }
14051
14052 saved_sym = NULL;
14053 return true;
14054 }
14055 break;
14056
14057 default:
14058 if (saved_sym != NULL)
14059 error (_("Unhandled MSP430 reloc type found after SYM_DIFF reloc\n"));
14060 break;
14061 }
14062 break;
14063 }
14064
14065 case EM_MN10300:
14066 case EM_CYGNUS_MN10300:
14067 {
14068 static Elf_Internal_Sym * saved_sym = NULL;
14069
14070 if (reloc == NULL)
14071 {
14072 saved_sym = NULL;
14073 return true;
14074 }
14075
14076 switch (reloc_type)
14077 {
14078 case 34: /* R_MN10300_ALIGN */
14079 return true;
14080 case 33: /* R_MN10300_SYM_DIFF */
14081 if (sym_index >= num_syms)
14082 error (_("MN10300_SYM_DIFF reloc contains invalid symbol index %lu\n"),
14083 sym_index);
14084 else
14085 saved_sym = symtab + sym_index;
14086 return true;
14087
14088 case 1: /* R_MN10300_32 */
14089 case 2: /* R_MN10300_16 */
14090 if (saved_sym != NULL)
14091 {
14092 int reloc_size = reloc_type == 1 ? 4 : 2;
14093 uint64_t value;
14094
14095 if (sym_index >= num_syms)
14096 error (_("MN10300 reloc contains invalid symbol index %lu\n"),
14097 sym_index);
14098 else
14099 {
14100 value = reloc->r_addend + (symtab[sym_index].st_value
14101 - saved_sym->st_value);
14102
14103 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
14104 byte_put (start + reloc->r_offset, value, reloc_size);
14105 else
14106 error (_("MN10300 sym diff reloc contains invalid offset: 0x%lx\n"),
14107 (long) reloc->r_offset);
14108 }
14109
14110 saved_sym = NULL;
14111 return true;
14112 }
14113 break;
14114 default:
14115 if (saved_sym != NULL)
14116 error (_("Unhandled MN10300 reloc type found after SYM_DIFF reloc\n"));
14117 break;
14118 }
14119 break;
14120 }
14121
14122 case EM_RL78:
14123 {
14124 static uint64_t saved_sym1 = 0;
14125 static uint64_t saved_sym2 = 0;
14126 static uint64_t value;
14127
14128 if (reloc == NULL)
14129 {
14130 saved_sym1 = saved_sym2 = 0;
14131 return true;
14132 }
14133
14134 switch (reloc_type)
14135 {
14136 case 0x80: /* R_RL78_SYM. */
14137 saved_sym1 = saved_sym2;
14138 if (sym_index >= num_syms)
14139 error (_("RL78_SYM reloc contains invalid symbol index %lu\n"),
14140 sym_index);
14141 else
14142 {
14143 saved_sym2 = symtab[sym_index].st_value;
14144 saved_sym2 += reloc->r_addend;
14145 }
14146 return true;
14147
14148 case 0x83: /* R_RL78_OPsub. */
14149 value = saved_sym1 - saved_sym2;
14150 saved_sym2 = saved_sym1 = 0;
14151 return true;
14152 break;
14153
14154 case 0x41: /* R_RL78_ABS32. */
14155 if (IN_RANGE (start, end, start + reloc->r_offset, 4))
14156 byte_put (start + reloc->r_offset, value, 4);
14157 else
14158 error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
14159 (long) reloc->r_offset);
14160 value = 0;
14161 return true;
14162
14163 case 0x43: /* R_RL78_ABS16. */
14164 if (IN_RANGE (start, end, start + reloc->r_offset, 2))
14165 byte_put (start + reloc->r_offset, value, 2);
14166 else
14167 error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
14168 (long) reloc->r_offset);
14169 value = 0;
14170 return true;
14171
14172 default:
14173 break;
14174 }
14175 break;
14176 }
14177 }
14178
14179 return false;
14180 }
14181
14182 /* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
14183 DWARF debug sections. This is a target specific test. Note - we do not
14184 go through the whole including-target-headers-multiple-times route, (as
14185 we have already done with <elf/h8.h>) because this would become very
14186 messy and even then this function would have to contain target specific
14187 information (the names of the relocs instead of their numeric values).
14188 FIXME: This is not the correct way to solve this problem. The proper way
14189 is to have target specific reloc sizing and typing functions created by
14190 the reloc-macros.h header, in the same way that it already creates the
14191 reloc naming functions. */
14192
14193 static bool
14194 is_32bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14195 {
14196 /* Please keep this table alpha-sorted for ease of visual lookup. */
14197 switch (filedata->file_header.e_machine)
14198 {
14199 case EM_386:
14200 case EM_IAMCU:
14201 return reloc_type == 1; /* R_386_32. */
14202 case EM_68K:
14203 return reloc_type == 1; /* R_68K_32. */
14204 case EM_860:
14205 return reloc_type == 1; /* R_860_32. */
14206 case EM_960:
14207 return reloc_type == 2; /* R_960_32. */
14208 case EM_AARCH64:
14209 return (reloc_type == 258
14210 || reloc_type == 1); /* R_AARCH64_ABS32 || R_AARCH64_P32_ABS32 */
14211 case EM_BPF:
14212 return reloc_type == 11; /* R_BPF_DATA_32 */
14213 case EM_ADAPTEVA_EPIPHANY:
14214 return reloc_type == 3;
14215 case EM_ALPHA:
14216 return reloc_type == 1; /* R_ALPHA_REFLONG. */
14217 case EM_ARC:
14218 return reloc_type == 1; /* R_ARC_32. */
14219 case EM_ARC_COMPACT:
14220 case EM_ARC_COMPACT2:
14221 return reloc_type == 4; /* R_ARC_32. */
14222 case EM_ARM:
14223 return reloc_type == 2; /* R_ARM_ABS32 */
14224 case EM_AVR_OLD:
14225 case EM_AVR:
14226 return reloc_type == 1;
14227 case EM_BLACKFIN:
14228 return reloc_type == 0x12; /* R_byte4_data. */
14229 case EM_CRIS:
14230 return reloc_type == 3; /* R_CRIS_32. */
14231 case EM_CR16:
14232 return reloc_type == 3; /* R_CR16_NUM32. */
14233 case EM_CRX:
14234 return reloc_type == 15; /* R_CRX_NUM32. */
14235 case EM_CSKY:
14236 return reloc_type == 1; /* R_CKCORE_ADDR32. */
14237 case EM_CYGNUS_FRV:
14238 return reloc_type == 1;
14239 case EM_CYGNUS_D10V:
14240 case EM_D10V:
14241 return reloc_type == 6; /* R_D10V_32. */
14242 case EM_CYGNUS_D30V:
14243 case EM_D30V:
14244 return reloc_type == 12; /* R_D30V_32_NORMAL. */
14245 case EM_DLX:
14246 return reloc_type == 3; /* R_DLX_RELOC_32. */
14247 case EM_CYGNUS_FR30:
14248 case EM_FR30:
14249 return reloc_type == 3; /* R_FR30_32. */
14250 case EM_FT32:
14251 return reloc_type == 1; /* R_FT32_32. */
14252 case EM_H8S:
14253 case EM_H8_300:
14254 case EM_H8_300H:
14255 return reloc_type == 1; /* R_H8_DIR32. */
14256 case EM_IA_64:
14257 return (reloc_type == 0x64 /* R_IA64_SECREL32MSB. */
14258 || reloc_type == 0x65 /* R_IA64_SECREL32LSB. */
14259 || reloc_type == 0x24 /* R_IA64_DIR32MSB. */
14260 || reloc_type == 0x25 /* R_IA64_DIR32LSB. */);
14261 case EM_IP2K_OLD:
14262 case EM_IP2K:
14263 return reloc_type == 2; /* R_IP2K_32. */
14264 case EM_IQ2000:
14265 return reloc_type == 2; /* R_IQ2000_32. */
14266 case EM_LATTICEMICO32:
14267 return reloc_type == 3; /* R_LM32_32. */
14268 case EM_LOONGARCH:
14269 return reloc_type == 1; /* R_LARCH_32. */
14270 case EM_M32C_OLD:
14271 case EM_M32C:
14272 return reloc_type == 3; /* R_M32C_32. */
14273 case EM_M32R:
14274 return reloc_type == 34; /* R_M32R_32_RELA. */
14275 case EM_68HC11:
14276 case EM_68HC12:
14277 return reloc_type == 6; /* R_M68HC11_32. */
14278 case EM_S12Z:
14279 return reloc_type == 7 || /* R_S12Z_EXT32 */
14280 reloc_type == 6; /* R_S12Z_CW32. */
14281 case EM_MCORE:
14282 return reloc_type == 1; /* R_MCORE_ADDR32. */
14283 case EM_CYGNUS_MEP:
14284 return reloc_type == 4; /* R_MEP_32. */
14285 case EM_METAG:
14286 return reloc_type == 2; /* R_METAG_ADDR32. */
14287 case EM_MICROBLAZE:
14288 return reloc_type == 1; /* R_MICROBLAZE_32. */
14289 case EM_MIPS:
14290 return reloc_type == 2; /* R_MIPS_32. */
14291 case EM_MMIX:
14292 return reloc_type == 4; /* R_MMIX_32. */
14293 case EM_CYGNUS_MN10200:
14294 case EM_MN10200:
14295 return reloc_type == 1; /* R_MN10200_32. */
14296 case EM_CYGNUS_MN10300:
14297 case EM_MN10300:
14298 return reloc_type == 1; /* R_MN10300_32. */
14299 case EM_MOXIE:
14300 return reloc_type == 1; /* R_MOXIE_32. */
14301 case EM_MSP430_OLD:
14302 case EM_MSP430:
14303 return reloc_type == 1; /* R_MSP430_32 or R_MSP320_ABS32. */
14304 case EM_MT:
14305 return reloc_type == 2; /* R_MT_32. */
14306 case EM_NDS32:
14307 return reloc_type == 20; /* R_NDS32_32_RELA. */
14308 case EM_ALTERA_NIOS2:
14309 return reloc_type == 12; /* R_NIOS2_BFD_RELOC_32. */
14310 case EM_NIOS32:
14311 return reloc_type == 1; /* R_NIOS_32. */
14312 case EM_OR1K:
14313 return reloc_type == 1; /* R_OR1K_32. */
14314 case EM_PARISC:
14315 return (reloc_type == 1 /* R_PARISC_DIR32. */
14316 || reloc_type == 2 /* R_PARISC_DIR21L. */
14317 || reloc_type == 41); /* R_PARISC_SECREL32. */
14318 case EM_PJ:
14319 case EM_PJ_OLD:
14320 return reloc_type == 1; /* R_PJ_DATA_DIR32. */
14321 case EM_PPC64:
14322 return reloc_type == 1; /* R_PPC64_ADDR32. */
14323 case EM_PPC:
14324 return reloc_type == 1; /* R_PPC_ADDR32. */
14325 case EM_TI_PRU:
14326 return reloc_type == 11; /* R_PRU_BFD_RELOC_32. */
14327 case EM_RISCV:
14328 return reloc_type == 1; /* R_RISCV_32. */
14329 case EM_RL78:
14330 return reloc_type == 1; /* R_RL78_DIR32. */
14331 case EM_RX:
14332 return reloc_type == 1; /* R_RX_DIR32. */
14333 case EM_S370:
14334 return reloc_type == 1; /* R_I370_ADDR31. */
14335 case EM_S390_OLD:
14336 case EM_S390:
14337 return reloc_type == 4; /* R_S390_32. */
14338 case EM_SCORE:
14339 return reloc_type == 8; /* R_SCORE_ABS32. */
14340 case EM_SH:
14341 return reloc_type == 1; /* R_SH_DIR32. */
14342 case EM_SPARC32PLUS:
14343 case EM_SPARCV9:
14344 case EM_SPARC:
14345 return reloc_type == 3 /* R_SPARC_32. */
14346 || reloc_type == 23; /* R_SPARC_UA32. */
14347 case EM_SPU:
14348 return reloc_type == 6; /* R_SPU_ADDR32 */
14349 case EM_TI_C6000:
14350 return reloc_type == 1; /* R_C6000_ABS32. */
14351 case EM_TILEGX:
14352 return reloc_type == 2; /* R_TILEGX_32. */
14353 case EM_TILEPRO:
14354 return reloc_type == 1; /* R_TILEPRO_32. */
14355 case EM_CYGNUS_V850:
14356 case EM_V850:
14357 return reloc_type == 6; /* R_V850_ABS32. */
14358 case EM_V800:
14359 return reloc_type == 0x33; /* R_V810_WORD. */
14360 case EM_VAX:
14361 return reloc_type == 1; /* R_VAX_32. */
14362 case EM_VISIUM:
14363 return reloc_type == 3; /* R_VISIUM_32. */
14364 case EM_WEBASSEMBLY:
14365 return reloc_type == 1; /* R_WASM32_32. */
14366 case EM_X86_64:
14367 case EM_L1OM:
14368 case EM_K1OM:
14369 return reloc_type == 10; /* R_X86_64_32. */
14370 case EM_XGATE:
14371 return reloc_type == 4; /* R_XGATE_32. */
14372 case EM_XSTORMY16:
14373 return reloc_type == 1; /* R_XSTROMY16_32. */
14374 case EM_XTENSA_OLD:
14375 case EM_XTENSA:
14376 return reloc_type == 1; /* R_XTENSA_32. */
14377 case EM_Z80:
14378 return reloc_type == 6; /* R_Z80_32. */
14379 default:
14380 {
14381 static unsigned int prev_warn = 0;
14382
14383 /* Avoid repeating the same warning multiple times. */
14384 if (prev_warn != filedata->file_header.e_machine)
14385 error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
14386 filedata->file_header.e_machine);
14387 prev_warn = filedata->file_header.e_machine;
14388 return false;
14389 }
14390 }
14391 }
14392
14393 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14394 a 32-bit pc-relative RELA relocation used in DWARF debug sections. */
14395
14396 static bool
14397 is_32bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
14398 {
14399 switch (filedata->file_header.e_machine)
14400 /* Please keep this table alpha-sorted for ease of visual lookup. */
14401 {
14402 case EM_386:
14403 case EM_IAMCU:
14404 return reloc_type == 2; /* R_386_PC32. */
14405 case EM_68K:
14406 return reloc_type == 4; /* R_68K_PC32. */
14407 case EM_AARCH64:
14408 return reloc_type == 261; /* R_AARCH64_PREL32 */
14409 case EM_ADAPTEVA_EPIPHANY:
14410 return reloc_type == 6;
14411 case EM_ALPHA:
14412 return reloc_type == 10; /* R_ALPHA_SREL32. */
14413 case EM_ARC_COMPACT:
14414 case EM_ARC_COMPACT2:
14415 return reloc_type == 49; /* R_ARC_32_PCREL. */
14416 case EM_ARM:
14417 return reloc_type == 3; /* R_ARM_REL32 */
14418 case EM_AVR_OLD:
14419 case EM_AVR:
14420 return reloc_type == 36; /* R_AVR_32_PCREL. */
14421 case EM_LOONGARCH:
14422 return reloc_type == 99; /* R_LARCH_32_PCREL. */
14423 case EM_MICROBLAZE:
14424 return reloc_type == 2; /* R_MICROBLAZE_32_PCREL. */
14425 case EM_OR1K:
14426 return reloc_type == 9; /* R_OR1K_32_PCREL. */
14427 case EM_PARISC:
14428 return reloc_type == 9; /* R_PARISC_PCREL32. */
14429 case EM_PPC:
14430 return reloc_type == 26; /* R_PPC_REL32. */
14431 case EM_PPC64:
14432 return reloc_type == 26; /* R_PPC64_REL32. */
14433 case EM_RISCV:
14434 return reloc_type == 57; /* R_RISCV_32_PCREL. */
14435 case EM_S390_OLD:
14436 case EM_S390:
14437 return reloc_type == 5; /* R_390_PC32. */
14438 case EM_SH:
14439 return reloc_type == 2; /* R_SH_REL32. */
14440 case EM_SPARC32PLUS:
14441 case EM_SPARCV9:
14442 case EM_SPARC:
14443 return reloc_type == 6; /* R_SPARC_DISP32. */
14444 case EM_SPU:
14445 return reloc_type == 13; /* R_SPU_REL32. */
14446 case EM_TILEGX:
14447 return reloc_type == 6; /* R_TILEGX_32_PCREL. */
14448 case EM_TILEPRO:
14449 return reloc_type == 4; /* R_TILEPRO_32_PCREL. */
14450 case EM_VISIUM:
14451 return reloc_type == 6; /* R_VISIUM_32_PCREL */
14452 case EM_X86_64:
14453 case EM_L1OM:
14454 case EM_K1OM:
14455 return reloc_type == 2; /* R_X86_64_PC32. */
14456 case EM_VAX:
14457 return reloc_type == 4; /* R_VAX_PCREL32. */
14458 case EM_XTENSA_OLD:
14459 case EM_XTENSA:
14460 return reloc_type == 14; /* R_XTENSA_32_PCREL. */
14461 default:
14462 /* Do not abort or issue an error message here. Not all targets use
14463 pc-relative 32-bit relocs in their DWARF debug information and we
14464 have already tested for target coverage in is_32bit_abs_reloc. A
14465 more helpful warning message will be generated by apply_relocations
14466 anyway, so just return. */
14467 return false;
14468 }
14469 }
14470
14471 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14472 a 64-bit absolute RELA relocation used in DWARF debug sections. */
14473
14474 static bool
14475 is_64bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14476 {
14477 switch (filedata->file_header.e_machine)
14478 {
14479 case EM_AARCH64:
14480 return reloc_type == 257; /* R_AARCH64_ABS64. */
14481 case EM_ALPHA:
14482 return reloc_type == 2; /* R_ALPHA_REFQUAD. */
14483 case EM_IA_64:
14484 return (reloc_type == 0x26 /* R_IA64_DIR64MSB. */
14485 || reloc_type == 0x27 /* R_IA64_DIR64LSB. */);
14486 case EM_LOONGARCH:
14487 return reloc_type == 2; /* R_LARCH_64 */
14488 case EM_PARISC:
14489 return reloc_type == 80; /* R_PARISC_DIR64. */
14490 case EM_PPC64:
14491 return reloc_type == 38; /* R_PPC64_ADDR64. */
14492 case EM_RISCV:
14493 return reloc_type == 2; /* R_RISCV_64. */
14494 case EM_SPARC32PLUS:
14495 case EM_SPARCV9:
14496 case EM_SPARC:
14497 return reloc_type == 32 /* R_SPARC_64. */
14498 || reloc_type == 54; /* R_SPARC_UA64. */
14499 case EM_X86_64:
14500 case EM_L1OM:
14501 case EM_K1OM:
14502 return reloc_type == 1; /* R_X86_64_64. */
14503 case EM_S390_OLD:
14504 case EM_S390:
14505 return reloc_type == 22; /* R_S390_64. */
14506 case EM_TILEGX:
14507 return reloc_type == 1; /* R_TILEGX_64. */
14508 case EM_MIPS:
14509 return reloc_type == 18; /* R_MIPS_64. */
14510 default:
14511 return false;
14512 }
14513 }
14514
14515 /* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
14516 a 64-bit pc-relative RELA relocation used in DWARF debug sections. */
14517
14518 static bool
14519 is_64bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
14520 {
14521 switch (filedata->file_header.e_machine)
14522 {
14523 case EM_AARCH64:
14524 return reloc_type == 260; /* R_AARCH64_PREL64. */
14525 case EM_ALPHA:
14526 return reloc_type == 11; /* R_ALPHA_SREL64. */
14527 case EM_IA_64:
14528 return (reloc_type == 0x4e /* R_IA64_PCREL64MSB. */
14529 || reloc_type == 0x4f /* R_IA64_PCREL64LSB. */);
14530 case EM_PARISC:
14531 return reloc_type == 72; /* R_PARISC_PCREL64. */
14532 case EM_PPC64:
14533 return reloc_type == 44; /* R_PPC64_REL64. */
14534 case EM_SPARC32PLUS:
14535 case EM_SPARCV9:
14536 case EM_SPARC:
14537 return reloc_type == 46; /* R_SPARC_DISP64. */
14538 case EM_X86_64:
14539 case EM_L1OM:
14540 case EM_K1OM:
14541 return reloc_type == 24; /* R_X86_64_PC64. */
14542 case EM_S390_OLD:
14543 case EM_S390:
14544 return reloc_type == 23; /* R_S390_PC64. */
14545 case EM_TILEGX:
14546 return reloc_type == 5; /* R_TILEGX_64_PCREL. */
14547 default:
14548 return false;
14549 }
14550 }
14551
14552 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14553 a 24-bit absolute RELA relocation used in DWARF debug sections. */
14554
14555 static bool
14556 is_24bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14557 {
14558 switch (filedata->file_header.e_machine)
14559 {
14560 case EM_CYGNUS_MN10200:
14561 case EM_MN10200:
14562 return reloc_type == 4; /* R_MN10200_24. */
14563 case EM_FT32:
14564 return reloc_type == 5; /* R_FT32_20. */
14565 case EM_Z80:
14566 return reloc_type == 5; /* R_Z80_24. */
14567 default:
14568 return false;
14569 }
14570 }
14571
14572 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14573 a 16-bit absolute RELA relocation used in DWARF debug sections. */
14574
14575 static bool
14576 is_16bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14577 {
14578 /* Please keep this table alpha-sorted for ease of visual lookup. */
14579 switch (filedata->file_header.e_machine)
14580 {
14581 case EM_ARC:
14582 case EM_ARC_COMPACT:
14583 case EM_ARC_COMPACT2:
14584 return reloc_type == 2; /* R_ARC_16. */
14585 case EM_ADAPTEVA_EPIPHANY:
14586 return reloc_type == 5;
14587 case EM_AVR_OLD:
14588 case EM_AVR:
14589 return reloc_type == 4; /* R_AVR_16. */
14590 case EM_CYGNUS_D10V:
14591 case EM_D10V:
14592 return reloc_type == 3; /* R_D10V_16. */
14593 case EM_FT32:
14594 return reloc_type == 2; /* R_FT32_16. */
14595 case EM_H8S:
14596 case EM_H8_300:
14597 case EM_H8_300H:
14598 return reloc_type == R_H8_DIR16;
14599 case EM_IP2K_OLD:
14600 case EM_IP2K:
14601 return reloc_type == 1; /* R_IP2K_16. */
14602 case EM_M32C_OLD:
14603 case EM_M32C:
14604 return reloc_type == 1; /* R_M32C_16 */
14605 case EM_CYGNUS_MN10200:
14606 case EM_MN10200:
14607 return reloc_type == 2; /* R_MN10200_16. */
14608 case EM_CYGNUS_MN10300:
14609 case EM_MN10300:
14610 return reloc_type == 2; /* R_MN10300_16. */
14611 case EM_MSP430:
14612 if (uses_msp430x_relocs (filedata))
14613 return reloc_type == 2; /* R_MSP430_ABS16. */
14614 /* Fall through. */
14615 case EM_MSP430_OLD:
14616 return reloc_type == 5; /* R_MSP430_16_BYTE. */
14617 case EM_NDS32:
14618 return reloc_type == 19; /* R_NDS32_16_RELA. */
14619 case EM_ALTERA_NIOS2:
14620 return reloc_type == 13; /* R_NIOS2_BFD_RELOC_16. */
14621 case EM_NIOS32:
14622 return reloc_type == 9; /* R_NIOS_16. */
14623 case EM_OR1K:
14624 return reloc_type == 2; /* R_OR1K_16. */
14625 case EM_RISCV:
14626 return reloc_type == 55; /* R_RISCV_SET16. */
14627 case EM_TI_PRU:
14628 return reloc_type == 8; /* R_PRU_BFD_RELOC_16. */
14629 case EM_TI_C6000:
14630 return reloc_type == 2; /* R_C6000_ABS16. */
14631 case EM_VISIUM:
14632 return reloc_type == 2; /* R_VISIUM_16. */
14633 case EM_XGATE:
14634 return reloc_type == 3; /* R_XGATE_16. */
14635 case EM_Z80:
14636 return reloc_type == 4; /* R_Z80_16. */
14637 default:
14638 return false;
14639 }
14640 }
14641
14642 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14643 a 8-bit absolute RELA relocation used in DWARF debug sections. */
14644
14645 static bool
14646 is_8bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14647 {
14648 switch (filedata->file_header.e_machine)
14649 {
14650 case EM_RISCV:
14651 return reloc_type == 54; /* R_RISCV_SET8. */
14652 case EM_Z80:
14653 return reloc_type == 1; /* R_Z80_8. */
14654 default:
14655 return false;
14656 }
14657 }
14658
14659 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14660 a 6-bit absolute RELA relocation used in DWARF debug sections. */
14661
14662 static bool
14663 is_6bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14664 {
14665 switch (filedata->file_header.e_machine)
14666 {
14667 case EM_RISCV:
14668 return reloc_type == 53; /* R_RISCV_SET6. */
14669 default:
14670 return false;
14671 }
14672 }
14673
14674 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14675 a 32-bit inplace add RELA relocation used in DWARF debug sections. */
14676
14677 static bool
14678 is_32bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14679 {
14680 /* Please keep this table alpha-sorted for ease of visual lookup. */
14681 switch (filedata->file_header.e_machine)
14682 {
14683 case EM_RISCV:
14684 return reloc_type == 35; /* R_RISCV_ADD32. */
14685 default:
14686 return false;
14687 }
14688 }
14689
14690 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14691 a 32-bit inplace sub RELA relocation used in DWARF debug sections. */
14692
14693 static bool
14694 is_32bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14695 {
14696 /* Please keep this table alpha-sorted for ease of visual lookup. */
14697 switch (filedata->file_header.e_machine)
14698 {
14699 case EM_RISCV:
14700 return reloc_type == 39; /* R_RISCV_SUB32. */
14701 default:
14702 return false;
14703 }
14704 }
14705
14706 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14707 a 64-bit inplace add RELA relocation used in DWARF debug sections. */
14708
14709 static bool
14710 is_64bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14711 {
14712 /* Please keep this table alpha-sorted for ease of visual lookup. */
14713 switch (filedata->file_header.e_machine)
14714 {
14715 case EM_RISCV:
14716 return reloc_type == 36; /* R_RISCV_ADD64. */
14717 default:
14718 return false;
14719 }
14720 }
14721
14722 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14723 a 64-bit inplace sub RELA relocation used in DWARF debug sections. */
14724
14725 static bool
14726 is_64bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14727 {
14728 /* Please keep this table alpha-sorted for ease of visual lookup. */
14729 switch (filedata->file_header.e_machine)
14730 {
14731 case EM_RISCV:
14732 return reloc_type == 40; /* R_RISCV_SUB64. */
14733 default:
14734 return false;
14735 }
14736 }
14737
14738 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14739 a 16-bit inplace add RELA relocation used in DWARF debug sections. */
14740
14741 static bool
14742 is_16bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14743 {
14744 /* Please keep this table alpha-sorted for ease of visual lookup. */
14745 switch (filedata->file_header.e_machine)
14746 {
14747 case EM_RISCV:
14748 return reloc_type == 34; /* R_RISCV_ADD16. */
14749 default:
14750 return false;
14751 }
14752 }
14753
14754 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14755 a 16-bit inplace sub RELA relocation used in DWARF debug sections. */
14756
14757 static bool
14758 is_16bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14759 {
14760 /* Please keep this table alpha-sorted for ease of visual lookup. */
14761 switch (filedata->file_header.e_machine)
14762 {
14763 case EM_RISCV:
14764 return reloc_type == 38; /* R_RISCV_SUB16. */
14765 default:
14766 return false;
14767 }
14768 }
14769
14770 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14771 a 8-bit inplace add RELA relocation used in DWARF debug sections. */
14772
14773 static bool
14774 is_8bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14775 {
14776 /* Please keep this table alpha-sorted for ease of visual lookup. */
14777 switch (filedata->file_header.e_machine)
14778 {
14779 case EM_RISCV:
14780 return reloc_type == 33; /* R_RISCV_ADD8. */
14781 default:
14782 return false;
14783 }
14784 }
14785
14786 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14787 a 8-bit inplace sub RELA relocation used in DWARF debug sections. */
14788
14789 static bool
14790 is_8bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14791 {
14792 /* Please keep this table alpha-sorted for ease of visual lookup. */
14793 switch (filedata->file_header.e_machine)
14794 {
14795 case EM_RISCV:
14796 return reloc_type == 37; /* R_RISCV_SUB8. */
14797 default:
14798 return false;
14799 }
14800 }
14801
14802 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14803 a 6-bit inplace sub RELA relocation used in DWARF debug sections. */
14804
14805 static bool
14806 is_6bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14807 {
14808 switch (filedata->file_header.e_machine)
14809 {
14810 case EM_RISCV:
14811 return reloc_type == 52; /* R_RISCV_SUB6. */
14812 default:
14813 return false;
14814 }
14815 }
14816
14817 /* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
14818 relocation entries (possibly formerly used for SHT_GROUP sections). */
14819
14820 static bool
14821 is_none_reloc (Filedata * filedata, unsigned int reloc_type)
14822 {
14823 switch (filedata->file_header.e_machine)
14824 {
14825 case EM_386: /* R_386_NONE. */
14826 case EM_68K: /* R_68K_NONE. */
14827 case EM_ADAPTEVA_EPIPHANY:
14828 case EM_ALPHA: /* R_ALPHA_NONE. */
14829 case EM_ALTERA_NIOS2: /* R_NIOS2_NONE. */
14830 case EM_ARC: /* R_ARC_NONE. */
14831 case EM_ARC_COMPACT2: /* R_ARC_NONE. */
14832 case EM_ARC_COMPACT: /* R_ARC_NONE. */
14833 case EM_ARM: /* R_ARM_NONE. */
14834 case EM_CRIS: /* R_CRIS_NONE. */
14835 case EM_FT32: /* R_FT32_NONE. */
14836 case EM_IA_64: /* R_IA64_NONE. */
14837 case EM_K1OM: /* R_X86_64_NONE. */
14838 case EM_L1OM: /* R_X86_64_NONE. */
14839 case EM_M32R: /* R_M32R_NONE. */
14840 case EM_MIPS: /* R_MIPS_NONE. */
14841 case EM_MN10300: /* R_MN10300_NONE. */
14842 case EM_MOXIE: /* R_MOXIE_NONE. */
14843 case EM_NIOS32: /* R_NIOS_NONE. */
14844 case EM_OR1K: /* R_OR1K_NONE. */
14845 case EM_PARISC: /* R_PARISC_NONE. */
14846 case EM_PPC64: /* R_PPC64_NONE. */
14847 case EM_PPC: /* R_PPC_NONE. */
14848 case EM_RISCV: /* R_RISCV_NONE. */
14849 case EM_S390: /* R_390_NONE. */
14850 case EM_S390_OLD:
14851 case EM_SH: /* R_SH_NONE. */
14852 case EM_SPARC32PLUS:
14853 case EM_SPARC: /* R_SPARC_NONE. */
14854 case EM_SPARCV9:
14855 case EM_TILEGX: /* R_TILEGX_NONE. */
14856 case EM_TILEPRO: /* R_TILEPRO_NONE. */
14857 case EM_TI_C6000:/* R_C6000_NONE. */
14858 case EM_X86_64: /* R_X86_64_NONE. */
14859 case EM_Z80: /* R_Z80_NONE. */
14860 case EM_WEBASSEMBLY: /* R_WASM32_NONE. */
14861 return reloc_type == 0;
14862
14863 case EM_AARCH64:
14864 return reloc_type == 0 || reloc_type == 256;
14865 case EM_AVR_OLD:
14866 case EM_AVR:
14867 return (reloc_type == 0 /* R_AVR_NONE. */
14868 || reloc_type == 30 /* R_AVR_DIFF8. */
14869 || reloc_type == 31 /* R_AVR_DIFF16. */
14870 || reloc_type == 32 /* R_AVR_DIFF32. */);
14871 case EM_METAG:
14872 return reloc_type == 3; /* R_METAG_NONE. */
14873 case EM_NDS32:
14874 return (reloc_type == 0 /* R_NDS32_NONE. */
14875 || reloc_type == 205 /* R_NDS32_DIFF8. */
14876 || reloc_type == 206 /* R_NDS32_DIFF16. */
14877 || reloc_type == 207 /* R_NDS32_DIFF32. */
14878 || reloc_type == 208 /* R_NDS32_DIFF_ULEB128. */);
14879 case EM_TI_PRU:
14880 return (reloc_type == 0 /* R_PRU_NONE. */
14881 || reloc_type == 65 /* R_PRU_DIFF8. */
14882 || reloc_type == 66 /* R_PRU_DIFF16. */
14883 || reloc_type == 67 /* R_PRU_DIFF32. */);
14884 case EM_XTENSA_OLD:
14885 case EM_XTENSA:
14886 return (reloc_type == 0 /* R_XTENSA_NONE. */
14887 || reloc_type == 17 /* R_XTENSA_DIFF8. */
14888 || reloc_type == 18 /* R_XTENSA_DIFF16. */
14889 || reloc_type == 19 /* R_XTENSA_DIFF32. */
14890 || reloc_type == 57 /* R_XTENSA_PDIFF8. */
14891 || reloc_type == 58 /* R_XTENSA_PDIFF16. */
14892 || reloc_type == 59 /* R_XTENSA_PDIFF32. */
14893 || reloc_type == 60 /* R_XTENSA_NDIFF8. */
14894 || reloc_type == 61 /* R_XTENSA_NDIFF16. */
14895 || reloc_type == 62 /* R_XTENSA_NDIFF32. */);
14896 }
14897 return false;
14898 }
14899
14900 /* Returns TRUE if there is a relocation against
14901 section NAME at OFFSET bytes. */
14902
14903 bool
14904 reloc_at (struct dwarf_section * dsec, uint64_t offset)
14905 {
14906 Elf_Internal_Rela * relocs;
14907 Elf_Internal_Rela * rp;
14908
14909 if (dsec == NULL || dsec->reloc_info == NULL)
14910 return false;
14911
14912 relocs = (Elf_Internal_Rela *) dsec->reloc_info;
14913
14914 for (rp = relocs; rp < relocs + dsec->num_relocs; ++rp)
14915 if (rp->r_offset == offset)
14916 return true;
14917
14918 return false;
14919 }
14920
14921 /* Apply relocations to a section.
14922 Returns TRUE upon success, FALSE otherwise.
14923 If RELOCS_RETURN is non-NULL then it is set to point to the loaded relocs.
14924 It is then the caller's responsibility to free them. NUM_RELOCS_RETURN
14925 will be set to the number of relocs loaded.
14926
14927 Note: So far support has been added only for those relocations
14928 which can be found in debug sections. FIXME: Add support for
14929 more relocations ? */
14930
14931 static bool
14932 apply_relocations (Filedata *filedata,
14933 const Elf_Internal_Shdr *section,
14934 unsigned char *start,
14935 size_t size,
14936 void **relocs_return,
14937 unsigned long *num_relocs_return)
14938 {
14939 Elf_Internal_Shdr * relsec;
14940 unsigned char * end = start + size;
14941
14942 if (relocs_return != NULL)
14943 {
14944 * (Elf_Internal_Rela **) relocs_return = NULL;
14945 * num_relocs_return = 0;
14946 }
14947
14948 if (filedata->file_header.e_type != ET_REL)
14949 /* No relocs to apply. */
14950 return true;
14951
14952 /* Find the reloc section associated with the section. */
14953 for (relsec = filedata->section_headers;
14954 relsec < filedata->section_headers + filedata->file_header.e_shnum;
14955 ++relsec)
14956 {
14957 bool is_rela;
14958 unsigned long num_relocs;
14959 Elf_Internal_Rela * relocs;
14960 Elf_Internal_Rela * rp;
14961 Elf_Internal_Shdr * symsec;
14962 Elf_Internal_Sym * symtab;
14963 unsigned long num_syms;
14964 Elf_Internal_Sym * sym;
14965
14966 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
14967 || relsec->sh_info >= filedata->file_header.e_shnum
14968 || filedata->section_headers + relsec->sh_info != section
14969 || relsec->sh_size == 0
14970 || relsec->sh_link >= filedata->file_header.e_shnum)
14971 continue;
14972
14973 symsec = filedata->section_headers + relsec->sh_link;
14974 if (symsec->sh_type != SHT_SYMTAB
14975 && symsec->sh_type != SHT_DYNSYM)
14976 return false;
14977
14978 is_rela = relsec->sh_type == SHT_RELA;
14979
14980 if (is_rela)
14981 {
14982 if (!slurp_rela_relocs (filedata, relsec->sh_offset,
14983 relsec->sh_size, & relocs, & num_relocs))
14984 return false;
14985 }
14986 else
14987 {
14988 if (!slurp_rel_relocs (filedata, relsec->sh_offset,
14989 relsec->sh_size, & relocs, & num_relocs))
14990 return false;
14991 }
14992
14993 /* SH uses RELA but uses in place value instead of the addend field. */
14994 if (filedata->file_header.e_machine == EM_SH)
14995 is_rela = false;
14996
14997 symtab = get_elf_symbols (filedata, symsec, & num_syms);
14998
14999 for (rp = relocs; rp < relocs + num_relocs; ++rp)
15000 {
15001 uint64_t addend;
15002 unsigned int reloc_type;
15003 unsigned int reloc_size;
15004 bool reloc_inplace = false;
15005 bool reloc_subtract = false;
15006 unsigned char *rloc;
15007 unsigned long sym_index;
15008
15009 reloc_type = get_reloc_type (filedata, rp->r_info);
15010
15011 if (target_specific_reloc_handling (filedata, rp, start, end, symtab, num_syms))
15012 continue;
15013 else if (is_none_reloc (filedata, reloc_type))
15014 continue;
15015 else if (is_32bit_abs_reloc (filedata, reloc_type)
15016 || is_32bit_pcrel_reloc (filedata, reloc_type))
15017 reloc_size = 4;
15018 else if (is_64bit_abs_reloc (filedata, reloc_type)
15019 || is_64bit_pcrel_reloc (filedata, reloc_type))
15020 reloc_size = 8;
15021 else if (is_24bit_abs_reloc (filedata, reloc_type))
15022 reloc_size = 3;
15023 else if (is_16bit_abs_reloc (filedata, reloc_type))
15024 reloc_size = 2;
15025 else if (is_8bit_abs_reloc (filedata, reloc_type)
15026 || is_6bit_abs_reloc (filedata, reloc_type))
15027 reloc_size = 1;
15028 else if ((reloc_subtract = is_32bit_inplace_sub_reloc (filedata,
15029 reloc_type))
15030 || is_32bit_inplace_add_reloc (filedata, reloc_type))
15031 {
15032 reloc_size = 4;
15033 reloc_inplace = true;
15034 }
15035 else if ((reloc_subtract = is_64bit_inplace_sub_reloc (filedata,
15036 reloc_type))
15037 || is_64bit_inplace_add_reloc (filedata, reloc_type))
15038 {
15039 reloc_size = 8;
15040 reloc_inplace = true;
15041 }
15042 else if ((reloc_subtract = is_16bit_inplace_sub_reloc (filedata,
15043 reloc_type))
15044 || is_16bit_inplace_add_reloc (filedata, reloc_type))
15045 {
15046 reloc_size = 2;
15047 reloc_inplace = true;
15048 }
15049 else if ((reloc_subtract = is_8bit_inplace_sub_reloc (filedata,
15050 reloc_type))
15051 || is_8bit_inplace_add_reloc (filedata, reloc_type))
15052 {
15053 reloc_size = 1;
15054 reloc_inplace = true;
15055 }
15056 else if ((reloc_subtract = is_6bit_inplace_sub_reloc (filedata,
15057 reloc_type)))
15058 {
15059 reloc_size = 1;
15060 reloc_inplace = true;
15061 }
15062 else
15063 {
15064 static unsigned int prev_reloc = 0;
15065
15066 if (reloc_type != prev_reloc)
15067 warn (_("unable to apply unsupported reloc type %d to section %s\n"),
15068 reloc_type, printable_section_name (filedata, section));
15069 prev_reloc = reloc_type;
15070 continue;
15071 }
15072
15073 rloc = start + rp->r_offset;
15074 if (!IN_RANGE (start, end, rloc, reloc_size))
15075 {
15076 warn (_("skipping invalid relocation offset 0x%lx in section %s\n"),
15077 (unsigned long) rp->r_offset,
15078 printable_section_name (filedata, section));
15079 continue;
15080 }
15081
15082 sym_index = (unsigned long) get_reloc_symindex (rp->r_info);
15083 if (sym_index >= num_syms)
15084 {
15085 warn (_("skipping invalid relocation symbol index 0x%lx in section %s\n"),
15086 sym_index, printable_section_name (filedata, section));
15087 continue;
15088 }
15089 sym = symtab + sym_index;
15090
15091 /* If the reloc has a symbol associated with it,
15092 make sure that it is of an appropriate type.
15093
15094 Relocations against symbols without type can happen.
15095 Gcc -feliminate-dwarf2-dups may generate symbols
15096 without type for debug info.
15097
15098 Icc generates relocations against function symbols
15099 instead of local labels.
15100
15101 Relocations against object symbols can happen, eg when
15102 referencing a global array. For an example of this see
15103 the _clz.o binary in libgcc.a. */
15104 if (sym != symtab
15105 && ELF_ST_TYPE (sym->st_info) != STT_COMMON
15106 && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
15107 {
15108 warn (_("skipping unexpected symbol type %s in section %s relocation %ld\n"),
15109 get_symbol_type (filedata, ELF_ST_TYPE (sym->st_info)),
15110 printable_section_name (filedata, relsec),
15111 (long int)(rp - relocs));
15112 continue;
15113 }
15114
15115 addend = 0;
15116 if (is_rela)
15117 addend += rp->r_addend;
15118 /* R_XTENSA_32, R_PJ_DATA_DIR32 and R_D30V_32_NORMAL are
15119 partial_inplace. */
15120 if (!is_rela
15121 || (filedata->file_header.e_machine == EM_XTENSA
15122 && reloc_type == 1)
15123 || ((filedata->file_header.e_machine == EM_PJ
15124 || filedata->file_header.e_machine == EM_PJ_OLD)
15125 && reloc_type == 1)
15126 || ((filedata->file_header.e_machine == EM_D30V
15127 || filedata->file_header.e_machine == EM_CYGNUS_D30V)
15128 && reloc_type == 12)
15129 || reloc_inplace)
15130 {
15131 if (is_6bit_inplace_sub_reloc (filedata, reloc_type))
15132 addend += byte_get (rloc, reloc_size) & 0x3f;
15133 else
15134 addend += byte_get (rloc, reloc_size);
15135 }
15136
15137 if (is_32bit_pcrel_reloc (filedata, reloc_type)
15138 || is_64bit_pcrel_reloc (filedata, reloc_type))
15139 {
15140 /* On HPPA, all pc-relative relocations are biased by 8. */
15141 if (filedata->file_header.e_machine == EM_PARISC)
15142 addend -= 8;
15143 byte_put (rloc, (addend + sym->st_value) - rp->r_offset,
15144 reloc_size);
15145 }
15146 else if (is_6bit_abs_reloc (filedata, reloc_type)
15147 || is_6bit_inplace_sub_reloc (filedata, reloc_type))
15148 {
15149 if (reloc_subtract)
15150 addend -= sym->st_value;
15151 else
15152 addend += sym->st_value;
15153 addend = (addend & 0x3f) | (byte_get (rloc, reloc_size) & 0xc0);
15154 byte_put (rloc, addend, reloc_size);
15155 }
15156 else if (reloc_subtract)
15157 byte_put (rloc, addend - sym->st_value, reloc_size);
15158 else
15159 byte_put (rloc, addend + sym->st_value, reloc_size);
15160 }
15161
15162 free (symtab);
15163 /* Let the target specific reloc processing code know that
15164 we have finished with these relocs. */
15165 target_specific_reloc_handling (filedata, NULL, NULL, NULL, NULL, 0);
15166
15167 if (relocs_return)
15168 {
15169 * (Elf_Internal_Rela **) relocs_return = relocs;
15170 * num_relocs_return = num_relocs;
15171 }
15172 else
15173 free (relocs);
15174
15175 break;
15176 }
15177
15178 return true;
15179 }
15180
15181 #ifdef SUPPORT_DISASSEMBLY
15182 static bool
15183 disassemble_section (Elf_Internal_Shdr * section, Filedata * filedata)
15184 {
15185 printf (_("\nAssembly dump of section %s\n"), printable_section_name (filedata, section));
15186
15187 /* FIXME: XXX -- to be done --- XXX */
15188
15189 return true;
15190 }
15191 #endif
15192
15193 /* Reads in the contents of SECTION from FILE, returning a pointer
15194 to a malloc'ed buffer or NULL if something went wrong. */
15195
15196 static char *
15197 get_section_contents (Elf_Internal_Shdr * section, Filedata * filedata)
15198 {
15199 uint64_t num_bytes = section->sh_size;
15200
15201 if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
15202 {
15203 printf (_("Section '%s' has no data to dump.\n"),
15204 printable_section_name (filedata, section));
15205 return NULL;
15206 }
15207
15208 return (char *) get_data (NULL, filedata, section->sh_offset, 1, num_bytes,
15209 _("section contents"));
15210 }
15211
15212 /* Uncompresses a section that was compressed using zlib/zstd, in place. */
15213
15214 static bool
15215 uncompress_section_contents (bool is_zstd, unsigned char **buffer,
15216 uint64_t uncompressed_size, uint64_t *size)
15217 {
15218 uint64_t compressed_size = *size;
15219 unsigned char *compressed_buffer = *buffer;
15220 unsigned char *uncompressed_buffer = xmalloc (uncompressed_size);
15221 z_stream strm;
15222 int rc;
15223
15224 if (is_zstd)
15225 {
15226 #ifdef HAVE_ZSTD
15227 size_t ret = ZSTD_decompress (uncompressed_buffer, uncompressed_size,
15228 compressed_buffer, compressed_size);
15229 if (ZSTD_isError (ret))
15230 goto fail;
15231 #endif
15232 }
15233 else
15234 {
15235 /* It is possible the section consists of several compressed
15236 buffers concatenated together, so we uncompress in a loop. */
15237 /* PR 18313: The state field in the z_stream structure is supposed
15238 to be invisible to the user (ie us), but some compilers will
15239 still complain about it being used without initialisation. So
15240 we first zero the entire z_stream structure and then set the fields
15241 that we need. */
15242 memset (&strm, 0, sizeof strm);
15243 strm.avail_in = compressed_size;
15244 strm.next_in = (Bytef *)compressed_buffer;
15245 strm.avail_out = uncompressed_size;
15246
15247 rc = inflateInit (&strm);
15248 while (strm.avail_in > 0)
15249 {
15250 if (rc != Z_OK)
15251 break;
15252 strm.next_out = ((Bytef *)uncompressed_buffer
15253 + (uncompressed_size - strm.avail_out));
15254 rc = inflate (&strm, Z_FINISH);
15255 if (rc != Z_STREAM_END)
15256 break;
15257 rc = inflateReset (&strm);
15258 }
15259 if (inflateEnd (&strm) != Z_OK || rc != Z_OK || strm.avail_out != 0)
15260 goto fail;
15261 }
15262
15263 *buffer = uncompressed_buffer;
15264 *size = uncompressed_size;
15265 return true;
15266
15267 fail:
15268 free (uncompressed_buffer);
15269 /* Indicate decompression failure. */
15270 *buffer = NULL;
15271 return false;
15272 }
15273
15274 static bool
15275 dump_section_as_strings (Elf_Internal_Shdr * section, Filedata * filedata)
15276 {
15277 Elf_Internal_Shdr *relsec;
15278 uint64_t num_bytes;
15279 unsigned char *data;
15280 unsigned char *end;
15281 unsigned char *real_start;
15282 unsigned char *start;
15283 bool some_strings_shown;
15284
15285 real_start = start = (unsigned char *) get_section_contents (section, filedata);
15286 if (start == NULL)
15287 /* PR 21820: Do not fail if the section was empty. */
15288 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
15289
15290 num_bytes = section->sh_size;
15291
15292 if (filedata->is_separate)
15293 printf (_("\nString dump of section '%s' in linked file %s:\n"),
15294 printable_section_name (filedata, section),
15295 filedata->file_name);
15296 else
15297 printf (_("\nString dump of section '%s':\n"),
15298 printable_section_name (filedata, section));
15299
15300 if (decompress_dumps)
15301 {
15302 uint64_t new_size = num_bytes;
15303 uint64_t uncompressed_size = 0;
15304 bool is_zstd = false;
15305
15306 if ((section->sh_flags & SHF_COMPRESSED) != 0)
15307 {
15308 Elf_Internal_Chdr chdr;
15309 unsigned int compression_header_size
15310 = get_compression_header (& chdr, (unsigned char *) start,
15311 num_bytes);
15312 if (compression_header_size == 0)
15313 /* An error message will have already been generated
15314 by get_compression_header. */
15315 goto error_out;
15316
15317 if (chdr.ch_type == ELFCOMPRESS_ZLIB)
15318 ;
15319 #ifdef HAVE_ZSTD
15320 else if (chdr.ch_type == ELFCOMPRESS_ZSTD)
15321 is_zstd = true;
15322 #endif
15323 else
15324 {
15325 warn (_("section '%s' has unsupported compress type: %d\n"),
15326 printable_section_name (filedata, section), chdr.ch_type);
15327 goto error_out;
15328 }
15329 uncompressed_size = chdr.ch_size;
15330 start += compression_header_size;
15331 new_size -= compression_header_size;
15332 }
15333 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
15334 {
15335 /* Read the zlib header. In this case, it should be "ZLIB"
15336 followed by the uncompressed section size, 8 bytes in
15337 big-endian order. */
15338 uncompressed_size = start[4]; uncompressed_size <<= 8;
15339 uncompressed_size += start[5]; uncompressed_size <<= 8;
15340 uncompressed_size += start[6]; uncompressed_size <<= 8;
15341 uncompressed_size += start[7]; uncompressed_size <<= 8;
15342 uncompressed_size += start[8]; uncompressed_size <<= 8;
15343 uncompressed_size += start[9]; uncompressed_size <<= 8;
15344 uncompressed_size += start[10]; uncompressed_size <<= 8;
15345 uncompressed_size += start[11];
15346 start += 12;
15347 new_size -= 12;
15348 }
15349
15350 if (uncompressed_size)
15351 {
15352 if (uncompress_section_contents (is_zstd, &start, uncompressed_size,
15353 &new_size))
15354 num_bytes = new_size;
15355 else
15356 {
15357 error (_("Unable to decompress section %s\n"),
15358 printable_section_name (filedata, section));
15359 goto error_out;
15360 }
15361 }
15362 else
15363 start = real_start;
15364 }
15365
15366 /* If the section being dumped has relocations against it the user might
15367 be expecting these relocations to have been applied. Check for this
15368 case and issue a warning message in order to avoid confusion.
15369 FIXME: Maybe we ought to have an option that dumps a section with
15370 relocs applied ? */
15371 for (relsec = filedata->section_headers;
15372 relsec < filedata->section_headers + filedata->file_header.e_shnum;
15373 ++relsec)
15374 {
15375 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
15376 || relsec->sh_info >= filedata->file_header.e_shnum
15377 || filedata->section_headers + relsec->sh_info != section
15378 || relsec->sh_size == 0
15379 || relsec->sh_link >= filedata->file_header.e_shnum)
15380 continue;
15381
15382 printf (_(" Note: This section has relocations against it, but these have NOT been applied to this dump.\n"));
15383 break;
15384 }
15385
15386 data = start;
15387 end = start + num_bytes;
15388 some_strings_shown = false;
15389
15390 #ifdef HAVE_MBSTATE_T
15391 mbstate_t state;
15392 /* Initialise the multibyte conversion state. */
15393 memset (& state, 0, sizeof (state));
15394 #endif
15395
15396 bool continuing = false;
15397
15398 while (data < end)
15399 {
15400 while (!ISPRINT (* data))
15401 if (++ data >= end)
15402 break;
15403
15404 if (data < end)
15405 {
15406 size_t maxlen = end - data;
15407
15408 if (continuing)
15409 {
15410 printf (" ");
15411 continuing = false;
15412 }
15413 else
15414 {
15415 printf (" [%6lx] ", (unsigned long) (data - start));
15416 }
15417
15418 if (maxlen > 0)
15419 {
15420 char c = 0;
15421
15422 while (maxlen)
15423 {
15424 c = *data++;
15425
15426 if (c == 0)
15427 break;
15428
15429 /* PR 25543: Treat new-lines as string-ending characters. */
15430 if (c == '\n')
15431 {
15432 printf ("\\n\n");
15433 if (*data != 0)
15434 continuing = true;
15435 break;
15436 }
15437
15438 /* Do not print control characters directly as they can affect terminal
15439 settings. Such characters usually appear in the names generated
15440 by the assembler for local labels. */
15441 if (ISCNTRL (c))
15442 {
15443 printf ("^%c", c + 0x40);
15444 }
15445 else if (ISPRINT (c))
15446 {
15447 putchar (c);
15448 }
15449 else
15450 {
15451 size_t n;
15452 #ifdef HAVE_MBSTATE_T
15453 wchar_t w;
15454 #endif
15455 /* Let printf do the hard work of displaying multibyte characters. */
15456 printf ("%.1s", data - 1);
15457 #ifdef HAVE_MBSTATE_T
15458 /* Try to find out how many bytes made up the character that was
15459 just printed. Advance the symbol pointer past the bytes that
15460 were displayed. */
15461 n = mbrtowc (& w, (char *)(data - 1), MB_CUR_MAX, & state);
15462 #else
15463 n = 1;
15464 #endif
15465 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
15466 data += (n - 1);
15467 }
15468 }
15469
15470 if (c != '\n')
15471 putchar ('\n');
15472 }
15473 else
15474 {
15475 printf (_("<corrupt>\n"));
15476 data = end;
15477 }
15478 some_strings_shown = true;
15479 }
15480 }
15481
15482 if (! some_strings_shown)
15483 printf (_(" No strings found in this section."));
15484
15485 free (real_start);
15486
15487 putchar ('\n');
15488 return true;
15489
15490 error_out:
15491 free (real_start);
15492 return false;
15493 }
15494
15495 static bool
15496 dump_section_as_bytes (Elf_Internal_Shdr *section,
15497 Filedata *filedata,
15498 bool relocate)
15499 {
15500 Elf_Internal_Shdr *relsec;
15501 size_t bytes;
15502 uint64_t section_size;
15503 uint64_t addr;
15504 unsigned char *data;
15505 unsigned char *real_start;
15506 unsigned char *start;
15507
15508 real_start = start = (unsigned char *) get_section_contents (section, filedata);
15509 if (start == NULL)
15510 /* PR 21820: Do not fail if the section was empty. */
15511 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
15512
15513 section_size = section->sh_size;
15514
15515 if (filedata->is_separate)
15516 printf (_("\nHex dump of section '%s' in linked file %s:\n"),
15517 printable_section_name (filedata, section),
15518 filedata->file_name);
15519 else
15520 printf (_("\nHex dump of section '%s':\n"),
15521 printable_section_name (filedata, section));
15522
15523 if (decompress_dumps)
15524 {
15525 uint64_t new_size = section_size;
15526 uint64_t uncompressed_size = 0;
15527 bool is_zstd = false;
15528
15529 if ((section->sh_flags & SHF_COMPRESSED) != 0)
15530 {
15531 Elf_Internal_Chdr chdr;
15532 unsigned int compression_header_size
15533 = get_compression_header (& chdr, start, section_size);
15534
15535 if (compression_header_size == 0)
15536 /* An error message will have already been generated
15537 by get_compression_header. */
15538 goto error_out;
15539
15540 if (chdr.ch_type == ELFCOMPRESS_ZLIB)
15541 ;
15542 #ifdef HAVE_ZSTD
15543 else if (chdr.ch_type == ELFCOMPRESS_ZSTD)
15544 is_zstd = true;
15545 #endif
15546 else
15547 {
15548 warn (_("section '%s' has unsupported compress type: %d\n"),
15549 printable_section_name (filedata, section), chdr.ch_type);
15550 goto error_out;
15551 }
15552 uncompressed_size = chdr.ch_size;
15553 start += compression_header_size;
15554 new_size -= compression_header_size;
15555 }
15556 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
15557 {
15558 /* Read the zlib header. In this case, it should be "ZLIB"
15559 followed by the uncompressed section size, 8 bytes in
15560 big-endian order. */
15561 uncompressed_size = start[4]; uncompressed_size <<= 8;
15562 uncompressed_size += start[5]; uncompressed_size <<= 8;
15563 uncompressed_size += start[6]; uncompressed_size <<= 8;
15564 uncompressed_size += start[7]; uncompressed_size <<= 8;
15565 uncompressed_size += start[8]; uncompressed_size <<= 8;
15566 uncompressed_size += start[9]; uncompressed_size <<= 8;
15567 uncompressed_size += start[10]; uncompressed_size <<= 8;
15568 uncompressed_size += start[11];
15569 start += 12;
15570 new_size -= 12;
15571 }
15572
15573 if (uncompressed_size)
15574 {
15575 if (uncompress_section_contents (is_zstd, &start, uncompressed_size,
15576 &new_size))
15577 {
15578 section_size = new_size;
15579 }
15580 else
15581 {
15582 error (_("Unable to decompress section %s\n"),
15583 printable_section_name (filedata, section));
15584 /* FIXME: Print the section anyway ? */
15585 goto error_out;
15586 }
15587 }
15588 else
15589 start = real_start;
15590 }
15591
15592 if (relocate)
15593 {
15594 if (! apply_relocations (filedata, section, start, section_size, NULL, NULL))
15595 goto error_out;
15596 }
15597 else
15598 {
15599 /* If the section being dumped has relocations against it the user might
15600 be expecting these relocations to have been applied. Check for this
15601 case and issue a warning message in order to avoid confusion.
15602 FIXME: Maybe we ought to have an option that dumps a section with
15603 relocs applied ? */
15604 for (relsec = filedata->section_headers;
15605 relsec < filedata->section_headers + filedata->file_header.e_shnum;
15606 ++relsec)
15607 {
15608 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
15609 || relsec->sh_info >= filedata->file_header.e_shnum
15610 || filedata->section_headers + relsec->sh_info != section
15611 || relsec->sh_size == 0
15612 || relsec->sh_link >= filedata->file_header.e_shnum)
15613 continue;
15614
15615 printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
15616 break;
15617 }
15618 }
15619
15620 addr = section->sh_addr;
15621 bytes = section_size;
15622 data = start;
15623
15624 while (bytes)
15625 {
15626 int j;
15627 int k;
15628 int lbytes;
15629
15630 lbytes = (bytes > 16 ? 16 : bytes);
15631
15632 printf (" 0x%8.8lx ", (unsigned long) addr);
15633
15634 for (j = 0; j < 16; j++)
15635 {
15636 if (j < lbytes)
15637 printf ("%2.2x", data[j]);
15638 else
15639 printf (" ");
15640
15641 if ((j & 3) == 3)
15642 printf (" ");
15643 }
15644
15645 for (j = 0; j < lbytes; j++)
15646 {
15647 k = data[j];
15648 if (k >= ' ' && k < 0x7f)
15649 printf ("%c", k);
15650 else
15651 printf (".");
15652 }
15653
15654 putchar ('\n');
15655
15656 data += lbytes;
15657 addr += lbytes;
15658 bytes -= lbytes;
15659 }
15660
15661 free (real_start);
15662
15663 putchar ('\n');
15664 return true;
15665
15666 error_out:
15667 free (real_start);
15668 return false;
15669 }
15670
15671 #ifdef ENABLE_LIBCTF
15672 static ctf_sect_t *
15673 shdr_to_ctf_sect (ctf_sect_t *buf, Elf_Internal_Shdr *shdr, Filedata *filedata)
15674 {
15675 buf->cts_name = section_name_print (filedata, shdr);
15676 buf->cts_size = shdr->sh_size;
15677 buf->cts_entsize = shdr->sh_entsize;
15678
15679 return buf;
15680 }
15681
15682 /* Formatting callback function passed to ctf_dump. Returns either the pointer
15683 it is passed, or a pointer to newly-allocated storage, in which case
15684 dump_ctf() will free it when it no longer needs it. */
15685
15686 static char *
15687 dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
15688 char *s, void *arg)
15689 {
15690 const char *blanks = arg;
15691 char *new_s;
15692
15693 if (asprintf (&new_s, "%s%s", blanks, s) < 0)
15694 return s;
15695 return new_s;
15696 }
15697
15698 /* Dump CTF errors/warnings. */
15699 static void
15700 dump_ctf_errs (ctf_dict_t *fp)
15701 {
15702 ctf_next_t *it = NULL;
15703 char *errtext;
15704 int is_warning;
15705 int err;
15706
15707 /* Dump accumulated errors and warnings. */
15708 while ((errtext = ctf_errwarning_next (fp, &it, &is_warning, &err)) != NULL)
15709 {
15710 error (_("%s: %s"), is_warning ? _("warning"): _("error"),
15711 errtext);
15712 free (errtext);
15713 }
15714 if (err != ECTF_NEXT_END)
15715 error (_("CTF error: cannot get CTF errors: `%s'"), ctf_errmsg (err));
15716 }
15717
15718 /* Dump one CTF archive member. */
15719
15720 static void
15721 dump_ctf_archive_member (ctf_dict_t *ctf, const char *name, ctf_dict_t *parent,
15722 size_t member)
15723 {
15724 const char *things[] = {"Header", "Labels", "Data objects",
15725 "Function objects", "Variables", "Types", "Strings",
15726 ""};
15727 const char **thing;
15728 size_t i;
15729
15730 /* Don't print out the name of the default-named archive member if it appears
15731 first in the list. The name .ctf appears everywhere, even for things that
15732 aren't really archives, so printing it out is liable to be confusing; also,
15733 the common case by far is for only one archive member to exist, and hiding
15734 it in that case seems worthwhile. */
15735
15736 if (strcmp (name, ".ctf") != 0 || member != 0)
15737 printf (_("\nCTF archive member: %s:\n"), name);
15738
15739 if (ctf_parent_name (ctf) != NULL)
15740 ctf_import (ctf, parent);
15741
15742 for (i = 0, thing = things; *thing[0]; thing++, i++)
15743 {
15744 ctf_dump_state_t *s = NULL;
15745 char *item;
15746
15747 printf ("\n %s:\n", *thing);
15748 while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
15749 (void *) " ")) != NULL)
15750 {
15751 printf ("%s\n", item);
15752 free (item);
15753 }
15754
15755 if (ctf_errno (ctf))
15756 {
15757 error (_("Iteration failed: %s, %s\n"), *thing,
15758 ctf_errmsg (ctf_errno (ctf)));
15759 break;
15760 }
15761 }
15762
15763 dump_ctf_errs (ctf);
15764 }
15765
15766 static bool
15767 dump_section_as_ctf (Elf_Internal_Shdr * section, Filedata * filedata)
15768 {
15769 Elf_Internal_Shdr * symtab_sec = NULL;
15770 Elf_Internal_Shdr * strtab_sec = NULL;
15771 void * data = NULL;
15772 void * symdata = NULL;
15773 void * strdata = NULL;
15774 ctf_sect_t ctfsect, symsect, strsect;
15775 ctf_sect_t * symsectp = NULL;
15776 ctf_sect_t * strsectp = NULL;
15777 ctf_archive_t * ctfa = NULL;
15778 ctf_dict_t * parent = NULL;
15779 ctf_dict_t * fp;
15780
15781 ctf_next_t *i = NULL;
15782 const char *name;
15783 size_t member = 0;
15784 int err;
15785 bool ret = false;
15786
15787 shdr_to_ctf_sect (&ctfsect, section, filedata);
15788 data = get_section_contents (section, filedata);
15789 ctfsect.cts_data = data;
15790
15791 if (!dump_ctf_symtab_name)
15792 dump_ctf_symtab_name = strdup (".dynsym");
15793
15794 if (!dump_ctf_strtab_name)
15795 dump_ctf_strtab_name = strdup (".dynstr");
15796
15797 if (dump_ctf_symtab_name && dump_ctf_symtab_name[0] != 0)
15798 {
15799 if ((symtab_sec = find_section (filedata, dump_ctf_symtab_name)) == NULL)
15800 {
15801 error (_("No symbol section named %s\n"), dump_ctf_symtab_name);
15802 goto fail;
15803 }
15804 if ((symdata = (void *) get_data (NULL, filedata,
15805 symtab_sec->sh_offset, 1,
15806 symtab_sec->sh_size,
15807 _("symbols"))) == NULL)
15808 goto fail;
15809 symsectp = shdr_to_ctf_sect (&symsect, symtab_sec, filedata);
15810 symsect.cts_data = symdata;
15811 }
15812
15813 if (dump_ctf_strtab_name && dump_ctf_strtab_name[0] != 0)
15814 {
15815 if ((strtab_sec = find_section (filedata, dump_ctf_strtab_name)) == NULL)
15816 {
15817 error (_("No string table section named %s\n"),
15818 dump_ctf_strtab_name);
15819 goto fail;
15820 }
15821 if ((strdata = (void *) get_data (NULL, filedata,
15822 strtab_sec->sh_offset, 1,
15823 strtab_sec->sh_size,
15824 _("strings"))) == NULL)
15825 goto fail;
15826 strsectp = shdr_to_ctf_sect (&strsect, strtab_sec, filedata);
15827 strsect.cts_data = strdata;
15828 }
15829
15830 /* Load the CTF file and dump it. It may be a raw CTF section, or an archive:
15831 libctf papers over the difference, so we can pretend it is always an
15832 archive. */
15833
15834 if ((ctfa = ctf_arc_bufopen (&ctfsect, symsectp, strsectp, &err)) == NULL)
15835 {
15836 dump_ctf_errs (NULL);
15837 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15838 goto fail;
15839 }
15840
15841 ctf_arc_symsect_endianness (ctfa, filedata->file_header.e_ident[EI_DATA]
15842 != ELFDATA2MSB);
15843
15844 /* Preload the parent dict, since it will need to be imported into every
15845 child in turn. */
15846 if ((parent = ctf_dict_open (ctfa, dump_ctf_parent_name, &err)) == NULL)
15847 {
15848 dump_ctf_errs (NULL);
15849 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15850 goto fail;
15851 }
15852
15853 ret = true;
15854
15855 if (filedata->is_separate)
15856 printf (_("\nDump of CTF section '%s' in linked file %s:\n"),
15857 printable_section_name (filedata, section),
15858 filedata->file_name);
15859 else
15860 printf (_("\nDump of CTF section '%s':\n"),
15861 printable_section_name (filedata, section));
15862
15863 while ((fp = ctf_archive_next (ctfa, &i, &name, 0, &err)) != NULL)
15864 dump_ctf_archive_member (fp, name, parent, member++);
15865 if (err != ECTF_NEXT_END)
15866 {
15867 dump_ctf_errs (NULL);
15868 error (_("CTF member open failure: %s\n"), ctf_errmsg (err));
15869 ret = false;
15870 }
15871
15872 fail:
15873 ctf_dict_close (parent);
15874 ctf_close (ctfa);
15875 free (data);
15876 free (symdata);
15877 free (strdata);
15878 return ret;
15879 }
15880 #endif
15881
15882 static bool
15883 dump_section_as_sframe (Elf_Internal_Shdr * section, Filedata * filedata)
15884 {
15885 void * data = NULL;
15886 sframe_decoder_ctx *sfd_ctx = NULL;
15887 const char *print_name = printable_section_name (filedata, section);
15888
15889 bool ret = true;
15890 size_t sf_size;
15891 int err = 0;
15892
15893 if (strcmp (print_name, "") == 0)
15894 {
15895 error (_("Section name must be provided \n"));
15896 ret = false;
15897 return ret;
15898 }
15899
15900 data = get_section_contents (section, filedata);
15901 sf_size = section->sh_size;
15902 /* Decode the contents of the section. */
15903 sfd_ctx = sframe_decode ((const char*)data, sf_size, &err);
15904 if (!sfd_ctx)
15905 {
15906 ret = false;
15907 error (_("SFrame decode failure: %s\n"), sframe_errmsg (err));
15908 goto fail;
15909 }
15910
15911 printf (_("Contents of the SFrame section %s:"), print_name);
15912 /* Dump the contents as text. */
15913 dump_sframe (sfd_ctx, section->sh_addr);
15914
15915 fail:
15916 free (data);
15917 return ret;
15918 }
15919
15920 static bool
15921 load_specific_debug_section (enum dwarf_section_display_enum debug,
15922 const Elf_Internal_Shdr * sec,
15923 void * data)
15924 {
15925 struct dwarf_section * section = &debug_displays [debug].section;
15926 char buf [64];
15927 Filedata * filedata = (Filedata *) data;
15928
15929 if (section->start != NULL)
15930 {
15931 /* If it is already loaded, do nothing. */
15932 if (streq (section->filename, filedata->file_name))
15933 return true;
15934 free (section->start);
15935 }
15936
15937 snprintf (buf, sizeof (buf), _("%s section data"), section->name);
15938 section->address = sec->sh_addr;
15939 section->filename = filedata->file_name;
15940 section->start = (unsigned char *) get_data (NULL, filedata,
15941 sec->sh_offset, 1,
15942 sec->sh_size, buf);
15943 if (section->start == NULL)
15944 section->size = 0;
15945 else
15946 {
15947 unsigned char *start = section->start;
15948 uint64_t size = sec->sh_size;
15949 uint64_t uncompressed_size = 0;
15950 bool is_zstd = false;
15951
15952 if ((sec->sh_flags & SHF_COMPRESSED) != 0)
15953 {
15954 Elf_Internal_Chdr chdr;
15955 unsigned int compression_header_size;
15956
15957 if (size < (is_32bit_elf
15958 ? sizeof (Elf32_External_Chdr)
15959 : sizeof (Elf64_External_Chdr)))
15960 {
15961 warn (_("compressed section %s is too small to contain a compression header\n"),
15962 section->name);
15963 return false;
15964 }
15965
15966 compression_header_size = get_compression_header (&chdr, start, size);
15967 if (compression_header_size == 0)
15968 /* An error message will have already been generated
15969 by get_compression_header. */
15970 return false;
15971
15972 if (chdr.ch_type == ELFCOMPRESS_ZLIB)
15973 ;
15974 #ifdef HAVE_ZSTD
15975 else if (chdr.ch_type == ELFCOMPRESS_ZSTD)
15976 is_zstd = true;
15977 #endif
15978 else
15979 {
15980 warn (_("section '%s' has unsupported compress type: %d\n"),
15981 section->name, chdr.ch_type);
15982 return false;
15983 }
15984 uncompressed_size = chdr.ch_size;
15985 start += compression_header_size;
15986 size -= compression_header_size;
15987 }
15988 else if (size > 12 && streq ((char *) start, "ZLIB"))
15989 {
15990 /* Read the zlib header. In this case, it should be "ZLIB"
15991 followed by the uncompressed section size, 8 bytes in
15992 big-endian order. */
15993 uncompressed_size = start[4]; uncompressed_size <<= 8;
15994 uncompressed_size += start[5]; uncompressed_size <<= 8;
15995 uncompressed_size += start[6]; uncompressed_size <<= 8;
15996 uncompressed_size += start[7]; uncompressed_size <<= 8;
15997 uncompressed_size += start[8]; uncompressed_size <<= 8;
15998 uncompressed_size += start[9]; uncompressed_size <<= 8;
15999 uncompressed_size += start[10]; uncompressed_size <<= 8;
16000 uncompressed_size += start[11];
16001 start += 12;
16002 size -= 12;
16003 }
16004
16005 if (uncompressed_size)
16006 {
16007 if (uncompress_section_contents (is_zstd, &start, uncompressed_size,
16008 &size))
16009 {
16010 /* Free the compressed buffer, update the section buffer
16011 and the section size if uncompress is successful. */
16012 free (section->start);
16013 section->start = start;
16014 }
16015 else
16016 {
16017 error (_("Unable to decompress section %s\n"),
16018 printable_section_name (filedata, sec));
16019 return false;
16020 }
16021 }
16022
16023 section->size = size;
16024 }
16025
16026 if (section->start == NULL)
16027 return false;
16028
16029 if (debug_displays [debug].relocate)
16030 {
16031 if (! apply_relocations (filedata, sec, section->start, section->size,
16032 & section->reloc_info, & section->num_relocs))
16033 return false;
16034 }
16035 else
16036 {
16037 section->reloc_info = NULL;
16038 section->num_relocs = 0;
16039 }
16040
16041 return true;
16042 }
16043
16044 #if HAVE_LIBDEBUGINFOD
16045 /* Return a hex string representation of the build-id. */
16046 unsigned char *
16047 get_build_id (void * data)
16048 {
16049 Filedata * filedata = (Filedata *) data;
16050 Elf_Internal_Shdr * shdr;
16051 unsigned long i;
16052
16053 /* Iterate through notes to find note.gnu.build-id.
16054 FIXME: Only the first note in any note section is examined. */
16055 for (i = 0, shdr = filedata->section_headers;
16056 i < filedata->file_header.e_shnum && shdr != NULL;
16057 i++, shdr++)
16058 {
16059 if (shdr->sh_type != SHT_NOTE)
16060 continue;
16061
16062 char * next;
16063 char * end;
16064 size_t data_remaining;
16065 size_t min_notesz;
16066 Elf_External_Note * enote;
16067 Elf_Internal_Note inote;
16068
16069 uint64_t offset = shdr->sh_offset;
16070 uint64_t align = shdr->sh_addralign;
16071 uint64_t length = shdr->sh_size;
16072
16073 enote = (Elf_External_Note *) get_section_contents (shdr, filedata);
16074 if (enote == NULL)
16075 continue;
16076
16077 if (align < 4)
16078 align = 4;
16079 else if (align != 4 && align != 8)
16080 {
16081 free (enote);
16082 continue;
16083 }
16084
16085 end = (char *) enote + length;
16086 data_remaining = end - (char *) enote;
16087
16088 if (!is_ia64_vms (filedata))
16089 {
16090 min_notesz = offsetof (Elf_External_Note, name);
16091 if (data_remaining < min_notesz)
16092 {
16093 warn (_("\
16094 malformed note encountered in section %s whilst scanning for build-id note\n"),
16095 printable_section_name (filedata, shdr));
16096 free (enote);
16097 continue;
16098 }
16099 data_remaining -= min_notesz;
16100
16101 inote.type = BYTE_GET (enote->type);
16102 inote.namesz = BYTE_GET (enote->namesz);
16103 inote.namedata = enote->name;
16104 inote.descsz = BYTE_GET (enote->descsz);
16105 inote.descdata = ((char *) enote
16106 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
16107 inote.descpos = offset + (inote.descdata - (char *) enote);
16108 next = ((char *) enote
16109 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
16110 }
16111 else
16112 {
16113 Elf64_External_VMS_Note *vms_enote;
16114
16115 /* PR binutils/15191
16116 Make sure that there is enough data to read. */
16117 min_notesz = offsetof (Elf64_External_VMS_Note, name);
16118 if (data_remaining < min_notesz)
16119 {
16120 warn (_("\
16121 malformed note encountered in section %s whilst scanning for build-id note\n"),
16122 printable_section_name (filedata, shdr));
16123 free (enote);
16124 continue;
16125 }
16126 data_remaining -= min_notesz;
16127
16128 vms_enote = (Elf64_External_VMS_Note *) enote;
16129 inote.type = BYTE_GET (vms_enote->type);
16130 inote.namesz = BYTE_GET (vms_enote->namesz);
16131 inote.namedata = vms_enote->name;
16132 inote.descsz = BYTE_GET (vms_enote->descsz);
16133 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
16134 inote.descpos = offset + (inote.descdata - (char *) enote);
16135 next = inote.descdata + align_power (inote.descsz, 3);
16136 }
16137
16138 /* Skip malformed notes. */
16139 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
16140 || (size_t) (inote.descdata - inote.namedata) > data_remaining
16141 || (size_t) (next - inote.descdata) < inote.descsz
16142 || ((size_t) (next - inote.descdata)
16143 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
16144 {
16145 warn (_("\
16146 malformed note encountered in section %s whilst scanning for build-id note\n"),
16147 printable_section_name (filedata, shdr));
16148 free (enote);
16149 continue;
16150 }
16151
16152 /* Check if this is the build-id note. If so then convert the build-id
16153 bytes to a hex string. */
16154 if (inote.namesz > 0
16155 && startswith (inote.namedata, "GNU")
16156 && inote.type == NT_GNU_BUILD_ID)
16157 {
16158 unsigned long j;
16159 char * build_id;
16160
16161 build_id = malloc (inote.descsz * 2 + 1);
16162 if (build_id == NULL)
16163 {
16164 free (enote);
16165 return NULL;
16166 }
16167
16168 for (j = 0; j < inote.descsz; ++j)
16169 sprintf (build_id + (j * 2), "%02x", inote.descdata[j] & 0xff);
16170 build_id[inote.descsz * 2] = '\0';
16171 free (enote);
16172
16173 return (unsigned char *) build_id;
16174 }
16175 free (enote);
16176 }
16177
16178 return NULL;
16179 }
16180 #endif /* HAVE_LIBDEBUGINFOD */
16181
16182 /* If this is not NULL, load_debug_section will only look for sections
16183 within the list of sections given here. */
16184 static unsigned int * section_subset = NULL;
16185
16186 bool
16187 load_debug_section (enum dwarf_section_display_enum debug, void * data)
16188 {
16189 struct dwarf_section * section = &debug_displays [debug].section;
16190 Elf_Internal_Shdr * sec;
16191 Filedata * filedata = (Filedata *) data;
16192
16193 if (!dump_any_debugging)
16194 return false;
16195
16196 /* Without section headers we cannot find any sections. */
16197 if (filedata->section_headers == NULL)
16198 return false;
16199
16200 if (filedata->string_table == NULL
16201 && filedata->file_header.e_shstrndx != SHN_UNDEF
16202 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
16203 {
16204 Elf_Internal_Shdr * strs;
16205
16206 /* Read in the string table, so that we have section names to scan. */
16207 strs = filedata->section_headers + filedata->file_header.e_shstrndx;
16208
16209 if (strs != NULL && strs->sh_size != 0)
16210 {
16211 filedata->string_table
16212 = (char *) get_data (NULL, filedata, strs->sh_offset,
16213 1, strs->sh_size, _("string table"));
16214
16215 filedata->string_table_length
16216 = filedata->string_table != NULL ? strs->sh_size : 0;
16217 }
16218 }
16219
16220 /* Locate the debug section. */
16221 sec = find_section_in_set (filedata, section->uncompressed_name, section_subset);
16222 if (sec != NULL)
16223 section->name = section->uncompressed_name;
16224 else
16225 {
16226 sec = find_section_in_set (filedata, section->compressed_name, section_subset);
16227 if (sec != NULL)
16228 section->name = section->compressed_name;
16229 }
16230 if (sec == NULL)
16231 return false;
16232
16233 /* If we're loading from a subset of sections, and we've loaded
16234 a section matching this name before, it's likely that it's a
16235 different one. */
16236 if (section_subset != NULL)
16237 free_debug_section (debug);
16238
16239 return load_specific_debug_section (debug, sec, data);
16240 }
16241
16242 void
16243 free_debug_section (enum dwarf_section_display_enum debug)
16244 {
16245 struct dwarf_section * section = &debug_displays [debug].section;
16246
16247 if (section->start == NULL)
16248 return;
16249
16250 free ((char *) section->start);
16251 section->start = NULL;
16252 section->address = 0;
16253 section->size = 0;
16254
16255 free (section->reloc_info);
16256 section->reloc_info = NULL;
16257 section->num_relocs = 0;
16258 }
16259
16260 static bool
16261 display_debug_section (int shndx, Elf_Internal_Shdr * section, Filedata * filedata)
16262 {
16263 const char *name = (section_name_valid (filedata, section)
16264 ? section_name (filedata, section) : "");
16265 const char *print_name = printable_section_name (filedata, section);
16266 uint64_t length;
16267 bool result = true;
16268 int i;
16269
16270 length = section->sh_size;
16271 if (length == 0)
16272 {
16273 printf (_("\nSection '%s' has no debugging data.\n"), print_name);
16274 return true;
16275 }
16276 if (section->sh_type == SHT_NOBITS)
16277 {
16278 /* There is no point in dumping the contents of a debugging section
16279 which has the NOBITS type - the bits in the file will be random.
16280 This can happen when a file containing a .eh_frame section is
16281 stripped with the --only-keep-debug command line option. */
16282 printf (_("section '%s' has the NOBITS type - its contents are unreliable.\n"),
16283 print_name);
16284 return false;
16285 }
16286
16287 if (startswith (name, ".gnu.linkonce.wi."))
16288 name = ".debug_info";
16289
16290 /* See if we know how to display the contents of this section. */
16291 for (i = 0; i < max; i++)
16292 {
16293 enum dwarf_section_display_enum id = (enum dwarf_section_display_enum) i;
16294 struct dwarf_section_display * display = debug_displays + i;
16295 struct dwarf_section * sec = & display->section;
16296
16297 if (streq (sec->uncompressed_name, name)
16298 || (id == line && startswith (name, ".debug_line."))
16299 || streq (sec->compressed_name, name))
16300 {
16301 bool secondary = (section != find_section (filedata, name));
16302
16303 if (secondary)
16304 free_debug_section (id);
16305
16306 if (i == line && startswith (name, ".debug_line."))
16307 sec->name = name;
16308 else if (streq (sec->uncompressed_name, name))
16309 sec->name = sec->uncompressed_name;
16310 else
16311 sec->name = sec->compressed_name;
16312
16313 if (load_specific_debug_section (id, section, filedata))
16314 {
16315 /* If this debug section is part of a CU/TU set in a .dwp file,
16316 restrict load_debug_section to the sections in that set. */
16317 section_subset = find_cu_tu_set (filedata, shndx);
16318
16319 result &= display->display (sec, filedata);
16320
16321 section_subset = NULL;
16322
16323 if (secondary || (id != info && id != abbrev && id != debug_addr))
16324 free_debug_section (id);
16325 }
16326 break;
16327 }
16328 }
16329
16330 if (i == max)
16331 {
16332 printf (_("Unrecognized debug section: %s\n"), print_name);
16333 result = false;
16334 }
16335
16336 return result;
16337 }
16338
16339 /* Set DUMP_SECTS for all sections where dumps were requested
16340 based on section name. */
16341
16342 static void
16343 initialise_dumps_byname (Filedata * filedata)
16344 {
16345 struct dump_list_entry * cur;
16346
16347 for (cur = dump_sects_byname; cur; cur = cur->next)
16348 {
16349 unsigned int i;
16350 bool any = false;
16351
16352 for (i = 0; i < filedata->file_header.e_shnum; i++)
16353 if (section_name_valid (filedata, filedata->section_headers + i)
16354 && streq (section_name (filedata, filedata->section_headers + i),
16355 cur->name))
16356 {
16357 request_dump_bynumber (&filedata->dump, i, cur->type);
16358 any = true;
16359 }
16360
16361 if (!any && !filedata->is_separate)
16362 warn (_("Section '%s' was not dumped because it does not exist\n"),
16363 cur->name);
16364 }
16365 }
16366
16367 static bool
16368 process_section_contents (Filedata * filedata)
16369 {
16370 Elf_Internal_Shdr * section;
16371 unsigned int i;
16372 bool res = true;
16373
16374 if (! do_dump)
16375 return true;
16376
16377 initialise_dumps_byname (filedata);
16378
16379 for (i = 0, section = filedata->section_headers;
16380 i < filedata->file_header.e_shnum && i < filedata->dump.num_dump_sects;
16381 i++, section++)
16382 {
16383 dump_type dump = filedata->dump.dump_sects[i];
16384
16385 if (filedata->is_separate && ! process_links)
16386 dump &= DEBUG_DUMP;
16387
16388 #ifdef SUPPORT_DISASSEMBLY
16389 if (dump & DISASS_DUMP)
16390 {
16391 if (! disassemble_section (section, filedata))
16392 res = false;
16393 }
16394 #endif
16395 if (dump & HEX_DUMP)
16396 {
16397 if (! dump_section_as_bytes (section, filedata, false))
16398 res = false;
16399 }
16400
16401 if (dump & RELOC_DUMP)
16402 {
16403 if (! dump_section_as_bytes (section, filedata, true))
16404 res = false;
16405 }
16406
16407 if (dump & STRING_DUMP)
16408 {
16409 if (! dump_section_as_strings (section, filedata))
16410 res = false;
16411 }
16412
16413 if (dump & DEBUG_DUMP)
16414 {
16415 if (! display_debug_section (i, section, filedata))
16416 res = false;
16417 }
16418
16419 #ifdef ENABLE_LIBCTF
16420 if (dump & CTF_DUMP)
16421 {
16422 if (! dump_section_as_ctf (section, filedata))
16423 res = false;
16424 }
16425 #endif
16426 if (dump & SFRAME_DUMP)
16427 {
16428 if (! dump_section_as_sframe (section, filedata))
16429 res = false;
16430 }
16431 }
16432
16433 if (! filedata->is_separate)
16434 {
16435 /* Check to see if the user requested a
16436 dump of a section that does not exist. */
16437 for (; i < filedata->dump.num_dump_sects; i++)
16438 if (filedata->dump.dump_sects[i])
16439 {
16440 warn (_("Section %d was not dumped because it does not exist!\n"), i);
16441 res = false;
16442 }
16443 }
16444
16445 return res;
16446 }
16447
16448 static void
16449 process_mips_fpe_exception (int mask)
16450 {
16451 if (mask)
16452 {
16453 bool first = true;
16454
16455 if (mask & OEX_FPU_INEX)
16456 fputs ("INEX", stdout), first = false;
16457 if (mask & OEX_FPU_UFLO)
16458 printf ("%sUFLO", first ? "" : "|"), first = false;
16459 if (mask & OEX_FPU_OFLO)
16460 printf ("%sOFLO", first ? "" : "|"), first = false;
16461 if (mask & OEX_FPU_DIV0)
16462 printf ("%sDIV0", first ? "" : "|"), first = false;
16463 if (mask & OEX_FPU_INVAL)
16464 printf ("%sINVAL", first ? "" : "|");
16465 }
16466 else
16467 fputs ("0", stdout);
16468 }
16469
16470 /* Display's the value of TAG at location P. If TAG is
16471 greater than 0 it is assumed to be an unknown tag, and
16472 a message is printed to this effect. Otherwise it is
16473 assumed that a message has already been printed.
16474
16475 If the bottom bit of TAG is set it assumed to have a
16476 string value, otherwise it is assumed to have an integer
16477 value.
16478
16479 Returns an updated P pointing to the first unread byte
16480 beyond the end of TAG's value.
16481
16482 Reads at or beyond END will not be made. */
16483
16484 static unsigned char *
16485 display_tag_value (signed int tag,
16486 unsigned char * p,
16487 const unsigned char * const end)
16488 {
16489 unsigned long val;
16490
16491 if (tag > 0)
16492 printf (" Tag_unknown_%d: ", tag);
16493
16494 if (p >= end)
16495 {
16496 warn (_("<corrupt tag>\n"));
16497 }
16498 else if (tag & 1)
16499 {
16500 /* PR 17531 file: 027-19978-0.004. */
16501 size_t maxlen = (end - p) - 1;
16502
16503 putchar ('"');
16504 if (maxlen > 0)
16505 {
16506 print_symbol ((int) maxlen, (const char *) p);
16507 p += strnlen ((char *) p, maxlen) + 1;
16508 }
16509 else
16510 {
16511 printf (_("<corrupt string tag>"));
16512 p = (unsigned char *) end;
16513 }
16514 printf ("\"\n");
16515 }
16516 else
16517 {
16518 READ_ULEB (val, p, end);
16519 printf ("%ld (0x%lx)\n", val, val);
16520 }
16521
16522 assert (p <= end);
16523 return p;
16524 }
16525
16526 /* ARC ABI attributes section. */
16527
16528 static unsigned char *
16529 display_arc_attribute (unsigned char * p,
16530 const unsigned char * const end)
16531 {
16532 unsigned int tag;
16533 unsigned int val;
16534
16535 READ_ULEB (tag, p, end);
16536
16537 switch (tag)
16538 {
16539 case Tag_ARC_PCS_config:
16540 READ_ULEB (val, p, end);
16541 printf (" Tag_ARC_PCS_config: ");
16542 switch (val)
16543 {
16544 case 0:
16545 printf (_("Absent/Non standard\n"));
16546 break;
16547 case 1:
16548 printf (_("Bare metal/mwdt\n"));
16549 break;
16550 case 2:
16551 printf (_("Bare metal/newlib\n"));
16552 break;
16553 case 3:
16554 printf (_("Linux/uclibc\n"));
16555 break;
16556 case 4:
16557 printf (_("Linux/glibc\n"));
16558 break;
16559 default:
16560 printf (_("Unknown\n"));
16561 break;
16562 }
16563 break;
16564
16565 case Tag_ARC_CPU_base:
16566 READ_ULEB (val, p, end);
16567 printf (" Tag_ARC_CPU_base: ");
16568 switch (val)
16569 {
16570 default:
16571 case TAG_CPU_NONE:
16572 printf (_("Absent\n"));
16573 break;
16574 case TAG_CPU_ARC6xx:
16575 printf ("ARC6xx\n");
16576 break;
16577 case TAG_CPU_ARC7xx:
16578 printf ("ARC7xx\n");
16579 break;
16580 case TAG_CPU_ARCEM:
16581 printf ("ARCEM\n");
16582 break;
16583 case TAG_CPU_ARCHS:
16584 printf ("ARCHS\n");
16585 break;
16586 }
16587 break;
16588
16589 case Tag_ARC_CPU_variation:
16590 READ_ULEB (val, p, end);
16591 printf (" Tag_ARC_CPU_variation: ");
16592 switch (val)
16593 {
16594 default:
16595 if (val > 0 && val < 16)
16596 printf ("Core%d\n", val);
16597 else
16598 printf ("Unknown\n");
16599 break;
16600
16601 case 0:
16602 printf (_("Absent\n"));
16603 break;
16604 }
16605 break;
16606
16607 case Tag_ARC_CPU_name:
16608 printf (" Tag_ARC_CPU_name: ");
16609 p = display_tag_value (-1, p, end);
16610 break;
16611
16612 case Tag_ARC_ABI_rf16:
16613 READ_ULEB (val, p, end);
16614 printf (" Tag_ARC_ABI_rf16: %s\n", val ? _("yes") : _("no"));
16615 break;
16616
16617 case Tag_ARC_ABI_osver:
16618 READ_ULEB (val, p, end);
16619 printf (" Tag_ARC_ABI_osver: v%d\n", val);
16620 break;
16621
16622 case Tag_ARC_ABI_pic:
16623 case Tag_ARC_ABI_sda:
16624 READ_ULEB (val, p, end);
16625 printf (tag == Tag_ARC_ABI_sda ? " Tag_ARC_ABI_sda: "
16626 : " Tag_ARC_ABI_pic: ");
16627 switch (val)
16628 {
16629 case 0:
16630 printf (_("Absent\n"));
16631 break;
16632 case 1:
16633 printf ("MWDT\n");
16634 break;
16635 case 2:
16636 printf ("GNU\n");
16637 break;
16638 default:
16639 printf (_("Unknown\n"));
16640 break;
16641 }
16642 break;
16643
16644 case Tag_ARC_ABI_tls:
16645 READ_ULEB (val, p, end);
16646 printf (" Tag_ARC_ABI_tls: %s\n", val ? "r25": "none");
16647 break;
16648
16649 case Tag_ARC_ABI_enumsize:
16650 READ_ULEB (val, p, end);
16651 printf (" Tag_ARC_ABI_enumsize: %s\n", val ? _("default") :
16652 _("smallest"));
16653 break;
16654
16655 case Tag_ARC_ABI_exceptions:
16656 READ_ULEB (val, p, end);
16657 printf (" Tag_ARC_ABI_exceptions: %s\n", val ? _("OPTFP")
16658 : _("default"));
16659 break;
16660
16661 case Tag_ARC_ABI_double_size:
16662 READ_ULEB (val, p, end);
16663 printf (" Tag_ARC_ABI_double_size: %d\n", val);
16664 break;
16665
16666 case Tag_ARC_ISA_config:
16667 printf (" Tag_ARC_ISA_config: ");
16668 p = display_tag_value (-1, p, end);
16669 break;
16670
16671 case Tag_ARC_ISA_apex:
16672 printf (" Tag_ARC_ISA_apex: ");
16673 p = display_tag_value (-1, p, end);
16674 break;
16675
16676 case Tag_ARC_ISA_mpy_option:
16677 READ_ULEB (val, p, end);
16678 printf (" Tag_ARC_ISA_mpy_option: %d\n", val);
16679 break;
16680
16681 case Tag_ARC_ATR_version:
16682 READ_ULEB (val, p, end);
16683 printf (" Tag_ARC_ATR_version: %d\n", val);
16684 break;
16685
16686 default:
16687 return display_tag_value (tag & 1, p, end);
16688 }
16689
16690 return p;
16691 }
16692
16693 /* ARM EABI attributes section. */
16694 typedef struct
16695 {
16696 unsigned int tag;
16697 const char * name;
16698 /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup. */
16699 unsigned int type;
16700 const char *const *table;
16701 } arm_attr_public_tag;
16702
16703 static const char *const arm_attr_tag_CPU_arch[] =
16704 {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
16705 "v6K", "v7", "v6-M", "v6S-M", "v7E-M", "v8", "v8-R", "v8-M.baseline",
16706 "v8-M.mainline", "v8.1-A", "v8.2-A", "v8.3-A",
16707 "v8.1-M.mainline", "v9"};
16708 static const char *const arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
16709 static const char *const arm_attr_tag_THUMB_ISA_use[] =
16710 {"No", "Thumb-1", "Thumb-2", "Yes"};
16711 static const char *const arm_attr_tag_FP_arch[] =
16712 {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16", "VFPv4", "VFPv4-D16",
16713 "FP for ARMv8", "FPv5/FP-D16 for ARMv8"};
16714 static const char *const arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
16715 static const char *const arm_attr_tag_Advanced_SIMD_arch[] =
16716 {"No", "NEONv1", "NEONv1 with Fused-MAC", "NEON for ARMv8",
16717 "NEON for ARMv8.1"};
16718 static const char *const arm_attr_tag_PCS_config[] =
16719 {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
16720 "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
16721 static const char *const arm_attr_tag_ABI_PCS_R9_use[] =
16722 {"V6", "SB", "TLS", "Unused"};
16723 static const char *const arm_attr_tag_ABI_PCS_RW_data[] =
16724 {"Absolute", "PC-relative", "SB-relative", "None"};
16725 static const char *const arm_attr_tag_ABI_PCS_RO_data[] =
16726 {"Absolute", "PC-relative", "None"};
16727 static const char *const arm_attr_tag_ABI_PCS_GOT_use[] =
16728 {"None", "direct", "GOT-indirect"};
16729 static const char *const arm_attr_tag_ABI_PCS_wchar_t[] =
16730 {"None", "??? 1", "2", "??? 3", "4"};
16731 static const char *const arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
16732 static const char *const arm_attr_tag_ABI_FP_denormal[] =
16733 {"Unused", "Needed", "Sign only"};
16734 static const char *const arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
16735 static const char *const arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
16736 static const char *const arm_attr_tag_ABI_FP_number_model[] =
16737 {"Unused", "Finite", "RTABI", "IEEE 754"};
16738 static const char *const arm_attr_tag_ABI_enum_size[] =
16739 {"Unused", "small", "int", "forced to int"};
16740 static const char *const arm_attr_tag_ABI_HardFP_use[] =
16741 {"As Tag_FP_arch", "SP only", "Reserved", "Deprecated"};
16742 static const char *const arm_attr_tag_ABI_VFP_args[] =
16743 {"AAPCS", "VFP registers", "custom", "compatible"};
16744 static const char *const arm_attr_tag_ABI_WMMX_args[] =
16745 {"AAPCS", "WMMX registers", "custom"};
16746 static const char *const arm_attr_tag_ABI_optimization_goals[] =
16747 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
16748 "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
16749 static const char *const arm_attr_tag_ABI_FP_optimization_goals[] =
16750 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
16751 "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
16752 static const char *const arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
16753 static const char *const arm_attr_tag_FP_HP_extension[] =
16754 {"Not Allowed", "Allowed"};
16755 static const char *const arm_attr_tag_ABI_FP_16bit_format[] =
16756 {"None", "IEEE 754", "Alternative Format"};
16757 static const char *const arm_attr_tag_DSP_extension[] =
16758 {"Follow architecture", "Allowed"};
16759 static const char *const arm_attr_tag_MPextension_use[] =
16760 {"Not Allowed", "Allowed"};
16761 static const char *const arm_attr_tag_DIV_use[] =
16762 {"Allowed in Thumb-ISA, v7-R or v7-M", "Not allowed",
16763 "Allowed in v7-A with integer division extension"};
16764 static const char *const arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
16765 static const char *const arm_attr_tag_Virtualization_use[] =
16766 {"Not Allowed", "TrustZone", "Virtualization Extensions",
16767 "TrustZone and Virtualization Extensions"};
16768 static const char *const arm_attr_tag_MPextension_use_legacy[] =
16769 {"Not Allowed", "Allowed"};
16770
16771 static const char *const arm_attr_tag_MVE_arch[] =
16772 {"No MVE", "MVE Integer only", "MVE Integer and FP"};
16773
16774 static const char * arm_attr_tag_PAC_extension[] =
16775 {"No PAC/AUT instructions",
16776 "PAC/AUT instructions permitted in the NOP space",
16777 "PAC/AUT instructions permitted in the NOP and in the non-NOP space"};
16778
16779 static const char * arm_attr_tag_BTI_extension[] =
16780 {"BTI instructions not permitted",
16781 "BTI instructions permitted in the NOP space",
16782 "BTI instructions permitted in the NOP and in the non-NOP space"};
16783
16784 static const char * arm_attr_tag_BTI_use[] =
16785 {"Compiled without branch target enforcement",
16786 "Compiled with branch target enforcement"};
16787
16788 static const char * arm_attr_tag_PACRET_use[] =
16789 {"Compiled without return address signing and authentication",
16790 "Compiled with return address signing and authentication"};
16791
16792 #define LOOKUP(id, name) \
16793 {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
16794 static arm_attr_public_tag arm_attr_public_tags[] =
16795 {
16796 {4, "CPU_raw_name", 1, NULL},
16797 {5, "CPU_name", 1, NULL},
16798 LOOKUP(6, CPU_arch),
16799 {7, "CPU_arch_profile", 0, NULL},
16800 LOOKUP(8, ARM_ISA_use),
16801 LOOKUP(9, THUMB_ISA_use),
16802 LOOKUP(10, FP_arch),
16803 LOOKUP(11, WMMX_arch),
16804 LOOKUP(12, Advanced_SIMD_arch),
16805 LOOKUP(13, PCS_config),
16806 LOOKUP(14, ABI_PCS_R9_use),
16807 LOOKUP(15, ABI_PCS_RW_data),
16808 LOOKUP(16, ABI_PCS_RO_data),
16809 LOOKUP(17, ABI_PCS_GOT_use),
16810 LOOKUP(18, ABI_PCS_wchar_t),
16811 LOOKUP(19, ABI_FP_rounding),
16812 LOOKUP(20, ABI_FP_denormal),
16813 LOOKUP(21, ABI_FP_exceptions),
16814 LOOKUP(22, ABI_FP_user_exceptions),
16815 LOOKUP(23, ABI_FP_number_model),
16816 {24, "ABI_align_needed", 0, NULL},
16817 {25, "ABI_align_preserved", 0, NULL},
16818 LOOKUP(26, ABI_enum_size),
16819 LOOKUP(27, ABI_HardFP_use),
16820 LOOKUP(28, ABI_VFP_args),
16821 LOOKUP(29, ABI_WMMX_args),
16822 LOOKUP(30, ABI_optimization_goals),
16823 LOOKUP(31, ABI_FP_optimization_goals),
16824 {32, "compatibility", 0, NULL},
16825 LOOKUP(34, CPU_unaligned_access),
16826 LOOKUP(36, FP_HP_extension),
16827 LOOKUP(38, ABI_FP_16bit_format),
16828 LOOKUP(42, MPextension_use),
16829 LOOKUP(44, DIV_use),
16830 LOOKUP(46, DSP_extension),
16831 LOOKUP(48, MVE_arch),
16832 LOOKUP(50, PAC_extension),
16833 LOOKUP(52, BTI_extension),
16834 LOOKUP(74, BTI_use),
16835 LOOKUP(76, PACRET_use),
16836 {64, "nodefaults", 0, NULL},
16837 {65, "also_compatible_with", 0, NULL},
16838 LOOKUP(66, T2EE_use),
16839 {67, "conformance", 1, NULL},
16840 LOOKUP(68, Virtualization_use),
16841 LOOKUP(70, MPextension_use_legacy)
16842 };
16843 #undef LOOKUP
16844
16845 static unsigned char *
16846 display_arm_attribute (unsigned char * p,
16847 const unsigned char * const end)
16848 {
16849 unsigned int tag;
16850 unsigned int val;
16851 arm_attr_public_tag * attr;
16852 unsigned i;
16853 unsigned int type;
16854
16855 READ_ULEB (tag, p, end);
16856 attr = NULL;
16857 for (i = 0; i < ARRAY_SIZE (arm_attr_public_tags); i++)
16858 {
16859 if (arm_attr_public_tags[i].tag == tag)
16860 {
16861 attr = &arm_attr_public_tags[i];
16862 break;
16863 }
16864 }
16865
16866 if (attr)
16867 {
16868 printf (" Tag_%s: ", attr->name);
16869 switch (attr->type)
16870 {
16871 case 0:
16872 switch (tag)
16873 {
16874 case 7: /* Tag_CPU_arch_profile. */
16875 READ_ULEB (val, p, end);
16876 switch (val)
16877 {
16878 case 0: printf (_("None\n")); break;
16879 case 'A': printf (_("Application\n")); break;
16880 case 'R': printf (_("Realtime\n")); break;
16881 case 'M': printf (_("Microcontroller\n")); break;
16882 case 'S': printf (_("Application or Realtime\n")); break;
16883 default: printf ("??? (%d)\n", val); break;
16884 }
16885 break;
16886
16887 case 24: /* Tag_align_needed. */
16888 READ_ULEB (val, p, end);
16889 switch (val)
16890 {
16891 case 0: printf (_("None\n")); break;
16892 case 1: printf (_("8-byte\n")); break;
16893 case 2: printf (_("4-byte\n")); break;
16894 case 3: printf ("??? 3\n"); break;
16895 default:
16896 if (val <= 12)
16897 printf (_("8-byte and up to %d-byte extended\n"),
16898 1 << val);
16899 else
16900 printf ("??? (%d)\n", val);
16901 break;
16902 }
16903 break;
16904
16905 case 25: /* Tag_align_preserved. */
16906 READ_ULEB (val, p, end);
16907 switch (val)
16908 {
16909 case 0: printf (_("None\n")); break;
16910 case 1: printf (_("8-byte, except leaf SP\n")); break;
16911 case 2: printf (_("8-byte\n")); break;
16912 case 3: printf ("??? 3\n"); break;
16913 default:
16914 if (val <= 12)
16915 printf (_("8-byte and up to %d-byte extended\n"),
16916 1 << val);
16917 else
16918 printf ("??? (%d)\n", val);
16919 break;
16920 }
16921 break;
16922
16923 case 32: /* Tag_compatibility. */
16924 {
16925 READ_ULEB (val, p, end);
16926 printf (_("flag = %d, vendor = "), val);
16927 if (p < end - 1)
16928 {
16929 size_t maxlen = (end - p) - 1;
16930
16931 print_symbol ((int) maxlen, (const char *) p);
16932 p += strnlen ((char *) p, maxlen) + 1;
16933 }
16934 else
16935 {
16936 printf (_("<corrupt>"));
16937 p = (unsigned char *) end;
16938 }
16939 putchar ('\n');
16940 }
16941 break;
16942
16943 case 64: /* Tag_nodefaults. */
16944 /* PR 17531: file: 001-505008-0.01. */
16945 if (p < end)
16946 p++;
16947 printf (_("True\n"));
16948 break;
16949
16950 case 65: /* Tag_also_compatible_with. */
16951 READ_ULEB (val, p, end);
16952 if (val == 6 /* Tag_CPU_arch. */)
16953 {
16954 READ_ULEB (val, p, end);
16955 if ((unsigned int) val >= ARRAY_SIZE (arm_attr_tag_CPU_arch))
16956 printf ("??? (%d)\n", val);
16957 else
16958 printf ("%s\n", arm_attr_tag_CPU_arch[val]);
16959 }
16960 else
16961 printf ("???\n");
16962 while (p < end && *(p++) != '\0' /* NUL terminator. */)
16963 ;
16964 break;
16965
16966 default:
16967 printf (_("<unknown: %d>\n"), tag);
16968 break;
16969 }
16970 return p;
16971
16972 case 1:
16973 return display_tag_value (-1, p, end);
16974 case 2:
16975 return display_tag_value (0, p, end);
16976
16977 default:
16978 assert (attr->type & 0x80);
16979 READ_ULEB (val, p, end);
16980 type = attr->type & 0x7f;
16981 if (val >= type)
16982 printf ("??? (%d)\n", val);
16983 else
16984 printf ("%s\n", attr->table[val]);
16985 return p;
16986 }
16987 }
16988
16989 return display_tag_value (tag, p, end);
16990 }
16991
16992 static unsigned char *
16993 display_gnu_attribute (unsigned char * p,
16994 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const),
16995 const unsigned char * const end)
16996 {
16997 unsigned int tag;
16998 unsigned int val;
16999
17000 READ_ULEB (tag, p, end);
17001
17002 /* Tag_compatibility is the only generic GNU attribute defined at
17003 present. */
17004 if (tag == 32)
17005 {
17006 READ_ULEB (val, p, end);
17007
17008 printf (_("flag = %d, vendor = "), val);
17009 if (p == end)
17010 {
17011 printf (_("<corrupt>\n"));
17012 warn (_("corrupt vendor attribute\n"));
17013 }
17014 else
17015 {
17016 if (p < end - 1)
17017 {
17018 size_t maxlen = (end - p) - 1;
17019
17020 print_symbol ((int) maxlen, (const char *) p);
17021 p += strnlen ((char *) p, maxlen) + 1;
17022 }
17023 else
17024 {
17025 printf (_("<corrupt>"));
17026 p = (unsigned char *) end;
17027 }
17028 putchar ('\n');
17029 }
17030 return p;
17031 }
17032
17033 if ((tag & 2) == 0 && display_proc_gnu_attribute)
17034 return display_proc_gnu_attribute (p, tag, end);
17035
17036 return display_tag_value (tag, p, end);
17037 }
17038
17039 static unsigned char *
17040 display_m68k_gnu_attribute (unsigned char * p,
17041 unsigned int tag,
17042 const unsigned char * const end)
17043 {
17044 unsigned int val;
17045
17046 if (tag == Tag_GNU_M68K_ABI_FP)
17047 {
17048 printf (" Tag_GNU_M68K_ABI_FP: ");
17049 if (p == end)
17050 {
17051 printf (_("<corrupt>\n"));
17052 return p;
17053 }
17054 READ_ULEB (val, p, end);
17055
17056 if (val > 3)
17057 printf ("(%#x), ", val);
17058
17059 switch (val & 3)
17060 {
17061 case 0:
17062 printf (_("unspecified hard/soft float\n"));
17063 break;
17064 case 1:
17065 printf (_("hard float\n"));
17066 break;
17067 case 2:
17068 printf (_("soft float\n"));
17069 break;
17070 }
17071 return p;
17072 }
17073
17074 return display_tag_value (tag & 1, p, end);
17075 }
17076
17077 static unsigned char *
17078 display_power_gnu_attribute (unsigned char * p,
17079 unsigned int tag,
17080 const unsigned char * const end)
17081 {
17082 unsigned int val;
17083
17084 if (tag == Tag_GNU_Power_ABI_FP)
17085 {
17086 printf (" Tag_GNU_Power_ABI_FP: ");
17087 if (p == end)
17088 {
17089 printf (_("<corrupt>\n"));
17090 return p;
17091 }
17092 READ_ULEB (val, p, end);
17093
17094 if (val > 15)
17095 printf ("(%#x), ", val);
17096
17097 switch (val & 3)
17098 {
17099 case 0:
17100 printf (_("unspecified hard/soft float, "));
17101 break;
17102 case 1:
17103 printf (_("hard float, "));
17104 break;
17105 case 2:
17106 printf (_("soft float, "));
17107 break;
17108 case 3:
17109 printf (_("single-precision hard float, "));
17110 break;
17111 }
17112
17113 switch (val & 0xC)
17114 {
17115 case 0:
17116 printf (_("unspecified long double\n"));
17117 break;
17118 case 4:
17119 printf (_("128-bit IBM long double\n"));
17120 break;
17121 case 8:
17122 printf (_("64-bit long double\n"));
17123 break;
17124 case 12:
17125 printf (_("128-bit IEEE long double\n"));
17126 break;
17127 }
17128 return p;
17129 }
17130
17131 if (tag == Tag_GNU_Power_ABI_Vector)
17132 {
17133 printf (" Tag_GNU_Power_ABI_Vector: ");
17134 if (p == end)
17135 {
17136 printf (_("<corrupt>\n"));
17137 return p;
17138 }
17139 READ_ULEB (val, p, end);
17140
17141 if (val > 3)
17142 printf ("(%#x), ", val);
17143
17144 switch (val & 3)
17145 {
17146 case 0:
17147 printf (_("unspecified\n"));
17148 break;
17149 case 1:
17150 printf (_("generic\n"));
17151 break;
17152 case 2:
17153 printf ("AltiVec\n");
17154 break;
17155 case 3:
17156 printf ("SPE\n");
17157 break;
17158 }
17159 return p;
17160 }
17161
17162 if (tag == Tag_GNU_Power_ABI_Struct_Return)
17163 {
17164 printf (" Tag_GNU_Power_ABI_Struct_Return: ");
17165 if (p == end)
17166 {
17167 printf (_("<corrupt>\n"));
17168 return p;
17169 }
17170 READ_ULEB (val, p, end);
17171
17172 if (val > 2)
17173 printf ("(%#x), ", val);
17174
17175 switch (val & 3)
17176 {
17177 case 0:
17178 printf (_("unspecified\n"));
17179 break;
17180 case 1:
17181 printf ("r3/r4\n");
17182 break;
17183 case 2:
17184 printf (_("memory\n"));
17185 break;
17186 case 3:
17187 printf ("???\n");
17188 break;
17189 }
17190 return p;
17191 }
17192
17193 return display_tag_value (tag & 1, p, end);
17194 }
17195
17196 static unsigned char *
17197 display_s390_gnu_attribute (unsigned char * p,
17198 unsigned int tag,
17199 const unsigned char * const end)
17200 {
17201 unsigned int val;
17202
17203 if (tag == Tag_GNU_S390_ABI_Vector)
17204 {
17205 printf (" Tag_GNU_S390_ABI_Vector: ");
17206 READ_ULEB (val, p, end);
17207
17208 switch (val)
17209 {
17210 case 0:
17211 printf (_("any\n"));
17212 break;
17213 case 1:
17214 printf (_("software\n"));
17215 break;
17216 case 2:
17217 printf (_("hardware\n"));
17218 break;
17219 default:
17220 printf ("??? (%d)\n", val);
17221 break;
17222 }
17223 return p;
17224 }
17225
17226 return display_tag_value (tag & 1, p, end);
17227 }
17228
17229 static void
17230 display_sparc_hwcaps (unsigned int mask)
17231 {
17232 if (mask)
17233 {
17234 bool first = true;
17235
17236 if (mask & ELF_SPARC_HWCAP_MUL32)
17237 fputs ("mul32", stdout), first = false;
17238 if (mask & ELF_SPARC_HWCAP_DIV32)
17239 printf ("%sdiv32", first ? "" : "|"), first = false;
17240 if (mask & ELF_SPARC_HWCAP_FSMULD)
17241 printf ("%sfsmuld", first ? "" : "|"), first = false;
17242 if (mask & ELF_SPARC_HWCAP_V8PLUS)
17243 printf ("%sv8plus", first ? "" : "|"), first = false;
17244 if (mask & ELF_SPARC_HWCAP_POPC)
17245 printf ("%spopc", first ? "" : "|"), first = false;
17246 if (mask & ELF_SPARC_HWCAP_VIS)
17247 printf ("%svis", first ? "" : "|"), first = false;
17248 if (mask & ELF_SPARC_HWCAP_VIS2)
17249 printf ("%svis2", first ? "" : "|"), first = false;
17250 if (mask & ELF_SPARC_HWCAP_ASI_BLK_INIT)
17251 printf ("%sASIBlkInit", first ? "" : "|"), first = false;
17252 if (mask & ELF_SPARC_HWCAP_FMAF)
17253 printf ("%sfmaf", first ? "" : "|"), first = false;
17254 if (mask & ELF_SPARC_HWCAP_VIS3)
17255 printf ("%svis3", first ? "" : "|"), first = false;
17256 if (mask & ELF_SPARC_HWCAP_HPC)
17257 printf ("%shpc", first ? "" : "|"), first = false;
17258 if (mask & ELF_SPARC_HWCAP_RANDOM)
17259 printf ("%srandom", first ? "" : "|"), first = false;
17260 if (mask & ELF_SPARC_HWCAP_TRANS)
17261 printf ("%strans", first ? "" : "|"), first = false;
17262 if (mask & ELF_SPARC_HWCAP_FJFMAU)
17263 printf ("%sfjfmau", first ? "" : "|"), first = false;
17264 if (mask & ELF_SPARC_HWCAP_IMA)
17265 printf ("%sima", first ? "" : "|"), first = false;
17266 if (mask & ELF_SPARC_HWCAP_ASI_CACHE_SPARING)
17267 printf ("%scspare", first ? "" : "|"), first = false;
17268 }
17269 else
17270 fputc ('0', stdout);
17271 fputc ('\n', stdout);
17272 }
17273
17274 static void
17275 display_sparc_hwcaps2 (unsigned int mask)
17276 {
17277 if (mask)
17278 {
17279 bool first = true;
17280
17281 if (mask & ELF_SPARC_HWCAP2_FJATHPLUS)
17282 fputs ("fjathplus", stdout), first = false;
17283 if (mask & ELF_SPARC_HWCAP2_VIS3B)
17284 printf ("%svis3b", first ? "" : "|"), first = false;
17285 if (mask & ELF_SPARC_HWCAP2_ADP)
17286 printf ("%sadp", first ? "" : "|"), first = false;
17287 if (mask & ELF_SPARC_HWCAP2_SPARC5)
17288 printf ("%ssparc5", first ? "" : "|"), first = false;
17289 if (mask & ELF_SPARC_HWCAP2_MWAIT)
17290 printf ("%smwait", first ? "" : "|"), first = false;
17291 if (mask & ELF_SPARC_HWCAP2_XMPMUL)
17292 printf ("%sxmpmul", first ? "" : "|"), first = false;
17293 if (mask & ELF_SPARC_HWCAP2_XMONT)
17294 printf ("%sxmont2", first ? "" : "|"), first = false;
17295 if (mask & ELF_SPARC_HWCAP2_NSEC)
17296 printf ("%snsec", first ? "" : "|"), first = false;
17297 if (mask & ELF_SPARC_HWCAP2_FJATHHPC)
17298 printf ("%sfjathhpc", first ? "" : "|"), first = false;
17299 if (mask & ELF_SPARC_HWCAP2_FJDES)
17300 printf ("%sfjdes", first ? "" : "|"), first = false;
17301 if (mask & ELF_SPARC_HWCAP2_FJAES)
17302 printf ("%sfjaes", first ? "" : "|"), first = false;
17303 }
17304 else
17305 fputc ('0', stdout);
17306 fputc ('\n', stdout);
17307 }
17308
17309 static unsigned char *
17310 display_sparc_gnu_attribute (unsigned char * p,
17311 unsigned int tag,
17312 const unsigned char * const end)
17313 {
17314 unsigned int val;
17315
17316 if (tag == Tag_GNU_Sparc_HWCAPS)
17317 {
17318 READ_ULEB (val, p, end);
17319 printf (" Tag_GNU_Sparc_HWCAPS: ");
17320 display_sparc_hwcaps (val);
17321 return p;
17322 }
17323 if (tag == Tag_GNU_Sparc_HWCAPS2)
17324 {
17325 READ_ULEB (val, p, end);
17326 printf (" Tag_GNU_Sparc_HWCAPS2: ");
17327 display_sparc_hwcaps2 (val);
17328 return p;
17329 }
17330
17331 return display_tag_value (tag, p, end);
17332 }
17333
17334 static void
17335 print_mips_fp_abi_value (unsigned int val)
17336 {
17337 switch (val)
17338 {
17339 case Val_GNU_MIPS_ABI_FP_ANY:
17340 printf (_("Hard or soft float\n"));
17341 break;
17342 case Val_GNU_MIPS_ABI_FP_DOUBLE:
17343 printf (_("Hard float (double precision)\n"));
17344 break;
17345 case Val_GNU_MIPS_ABI_FP_SINGLE:
17346 printf (_("Hard float (single precision)\n"));
17347 break;
17348 case Val_GNU_MIPS_ABI_FP_SOFT:
17349 printf (_("Soft float\n"));
17350 break;
17351 case Val_GNU_MIPS_ABI_FP_OLD_64:
17352 printf (_("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
17353 break;
17354 case Val_GNU_MIPS_ABI_FP_XX:
17355 printf (_("Hard float (32-bit CPU, Any FPU)\n"));
17356 break;
17357 case Val_GNU_MIPS_ABI_FP_64:
17358 printf (_("Hard float (32-bit CPU, 64-bit FPU)\n"));
17359 break;
17360 case Val_GNU_MIPS_ABI_FP_64A:
17361 printf (_("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
17362 break;
17363 case Val_GNU_MIPS_ABI_FP_NAN2008:
17364 printf (_("NaN 2008 compatibility\n"));
17365 break;
17366 default:
17367 printf ("??? (%d)\n", val);
17368 break;
17369 }
17370 }
17371
17372 static unsigned char *
17373 display_mips_gnu_attribute (unsigned char * p,
17374 unsigned int tag,
17375 const unsigned char * const end)
17376 {
17377 if (tag == Tag_GNU_MIPS_ABI_FP)
17378 {
17379 unsigned int val;
17380
17381 printf (" Tag_GNU_MIPS_ABI_FP: ");
17382 READ_ULEB (val, p, end);
17383 print_mips_fp_abi_value (val);
17384 return p;
17385 }
17386
17387 if (tag == Tag_GNU_MIPS_ABI_MSA)
17388 {
17389 unsigned int val;
17390
17391 printf (" Tag_GNU_MIPS_ABI_MSA: ");
17392 READ_ULEB (val, p, end);
17393
17394 switch (val)
17395 {
17396 case Val_GNU_MIPS_ABI_MSA_ANY:
17397 printf (_("Any MSA or not\n"));
17398 break;
17399 case Val_GNU_MIPS_ABI_MSA_128:
17400 printf (_("128-bit MSA\n"));
17401 break;
17402 default:
17403 printf ("??? (%d)\n", val);
17404 break;
17405 }
17406 return p;
17407 }
17408
17409 return display_tag_value (tag & 1, p, end);
17410 }
17411
17412 static unsigned char *
17413 display_tic6x_attribute (unsigned char * p,
17414 const unsigned char * const end)
17415 {
17416 unsigned int tag;
17417 unsigned int val;
17418
17419 READ_ULEB (tag, p, end);
17420
17421 switch (tag)
17422 {
17423 case Tag_ISA:
17424 printf (" Tag_ISA: ");
17425 READ_ULEB (val, p, end);
17426
17427 switch (val)
17428 {
17429 case C6XABI_Tag_ISA_none:
17430 printf (_("None\n"));
17431 break;
17432 case C6XABI_Tag_ISA_C62X:
17433 printf ("C62x\n");
17434 break;
17435 case C6XABI_Tag_ISA_C67X:
17436 printf ("C67x\n");
17437 break;
17438 case C6XABI_Tag_ISA_C67XP:
17439 printf ("C67x+\n");
17440 break;
17441 case C6XABI_Tag_ISA_C64X:
17442 printf ("C64x\n");
17443 break;
17444 case C6XABI_Tag_ISA_C64XP:
17445 printf ("C64x+\n");
17446 break;
17447 case C6XABI_Tag_ISA_C674X:
17448 printf ("C674x\n");
17449 break;
17450 default:
17451 printf ("??? (%d)\n", val);
17452 break;
17453 }
17454 return p;
17455
17456 case Tag_ABI_wchar_t:
17457 printf (" Tag_ABI_wchar_t: ");
17458 READ_ULEB (val, p, end);
17459 switch (val)
17460 {
17461 case 0:
17462 printf (_("Not used\n"));
17463 break;
17464 case 1:
17465 printf (_("2 bytes\n"));
17466 break;
17467 case 2:
17468 printf (_("4 bytes\n"));
17469 break;
17470 default:
17471 printf ("??? (%d)\n", val);
17472 break;
17473 }
17474 return p;
17475
17476 case Tag_ABI_stack_align_needed:
17477 printf (" Tag_ABI_stack_align_needed: ");
17478 READ_ULEB (val, p, end);
17479 switch (val)
17480 {
17481 case 0:
17482 printf (_("8-byte\n"));
17483 break;
17484 case 1:
17485 printf (_("16-byte\n"));
17486 break;
17487 default:
17488 printf ("??? (%d)\n", val);
17489 break;
17490 }
17491 return p;
17492
17493 case Tag_ABI_stack_align_preserved:
17494 READ_ULEB (val, p, end);
17495 printf (" Tag_ABI_stack_align_preserved: ");
17496 switch (val)
17497 {
17498 case 0:
17499 printf (_("8-byte\n"));
17500 break;
17501 case 1:
17502 printf (_("16-byte\n"));
17503 break;
17504 default:
17505 printf ("??? (%d)\n", val);
17506 break;
17507 }
17508 return p;
17509
17510 case Tag_ABI_DSBT:
17511 READ_ULEB (val, p, end);
17512 printf (" Tag_ABI_DSBT: ");
17513 switch (val)
17514 {
17515 case 0:
17516 printf (_("DSBT addressing not used\n"));
17517 break;
17518 case 1:
17519 printf (_("DSBT addressing used\n"));
17520 break;
17521 default:
17522 printf ("??? (%d)\n", val);
17523 break;
17524 }
17525 return p;
17526
17527 case Tag_ABI_PID:
17528 READ_ULEB (val, p, end);
17529 printf (" Tag_ABI_PID: ");
17530 switch (val)
17531 {
17532 case 0:
17533 printf (_("Data addressing position-dependent\n"));
17534 break;
17535 case 1:
17536 printf (_("Data addressing position-independent, GOT near DP\n"));
17537 break;
17538 case 2:
17539 printf (_("Data addressing position-independent, GOT far from DP\n"));
17540 break;
17541 default:
17542 printf ("??? (%d)\n", val);
17543 break;
17544 }
17545 return p;
17546
17547 case Tag_ABI_PIC:
17548 READ_ULEB (val, p, end);
17549 printf (" Tag_ABI_PIC: ");
17550 switch (val)
17551 {
17552 case 0:
17553 printf (_("Code addressing position-dependent\n"));
17554 break;
17555 case 1:
17556 printf (_("Code addressing position-independent\n"));
17557 break;
17558 default:
17559 printf ("??? (%d)\n", val);
17560 break;
17561 }
17562 return p;
17563
17564 case Tag_ABI_array_object_alignment:
17565 READ_ULEB (val, p, end);
17566 printf (" Tag_ABI_array_object_alignment: ");
17567 switch (val)
17568 {
17569 case 0:
17570 printf (_("8-byte\n"));
17571 break;
17572 case 1:
17573 printf (_("4-byte\n"));
17574 break;
17575 case 2:
17576 printf (_("16-byte\n"));
17577 break;
17578 default:
17579 printf ("??? (%d)\n", val);
17580 break;
17581 }
17582 return p;
17583
17584 case Tag_ABI_array_object_align_expected:
17585 READ_ULEB (val, p, end);
17586 printf (" Tag_ABI_array_object_align_expected: ");
17587 switch (val)
17588 {
17589 case 0:
17590 printf (_("8-byte\n"));
17591 break;
17592 case 1:
17593 printf (_("4-byte\n"));
17594 break;
17595 case 2:
17596 printf (_("16-byte\n"));
17597 break;
17598 default:
17599 printf ("??? (%d)\n", val);
17600 break;
17601 }
17602 return p;
17603
17604 case Tag_ABI_compatibility:
17605 {
17606 READ_ULEB (val, p, end);
17607 printf (" Tag_ABI_compatibility: ");
17608 printf (_("flag = %d, vendor = "), val);
17609 if (p < end - 1)
17610 {
17611 size_t maxlen = (end - p) - 1;
17612
17613 print_symbol ((int) maxlen, (const char *) p);
17614 p += strnlen ((char *) p, maxlen) + 1;
17615 }
17616 else
17617 {
17618 printf (_("<corrupt>"));
17619 p = (unsigned char *) end;
17620 }
17621 putchar ('\n');
17622 return p;
17623 }
17624
17625 case Tag_ABI_conformance:
17626 {
17627 printf (" Tag_ABI_conformance: \"");
17628 if (p < end - 1)
17629 {
17630 size_t maxlen = (end - p) - 1;
17631
17632 print_symbol ((int) maxlen, (const char *) p);
17633 p += strnlen ((char *) p, maxlen) + 1;
17634 }
17635 else
17636 {
17637 printf (_("<corrupt>"));
17638 p = (unsigned char *) end;
17639 }
17640 printf ("\"\n");
17641 return p;
17642 }
17643 }
17644
17645 return display_tag_value (tag, p, end);
17646 }
17647
17648 static void
17649 display_raw_attribute (unsigned char * p, unsigned char const * const end)
17650 {
17651 unsigned long addr = 0;
17652 size_t bytes = end - p;
17653
17654 assert (end >= p);
17655 while (bytes)
17656 {
17657 int j;
17658 int k;
17659 int lbytes = (bytes > 16 ? 16 : bytes);
17660
17661 printf (" 0x%8.8lx ", addr);
17662
17663 for (j = 0; j < 16; j++)
17664 {
17665 if (j < lbytes)
17666 printf ("%2.2x", p[j]);
17667 else
17668 printf (" ");
17669
17670 if ((j & 3) == 3)
17671 printf (" ");
17672 }
17673
17674 for (j = 0; j < lbytes; j++)
17675 {
17676 k = p[j];
17677 if (k >= ' ' && k < 0x7f)
17678 printf ("%c", k);
17679 else
17680 printf (".");
17681 }
17682
17683 putchar ('\n');
17684
17685 p += lbytes;
17686 bytes -= lbytes;
17687 addr += lbytes;
17688 }
17689
17690 putchar ('\n');
17691 }
17692
17693 static unsigned char *
17694 display_msp430_attribute (unsigned char * p,
17695 const unsigned char * const end)
17696 {
17697 unsigned int val;
17698 unsigned int tag;
17699
17700 READ_ULEB (tag, p, end);
17701
17702 switch (tag)
17703 {
17704 case OFBA_MSPABI_Tag_ISA:
17705 printf (" Tag_ISA: ");
17706 READ_ULEB (val, p, end);
17707 switch (val)
17708 {
17709 case 0: printf (_("None\n")); break;
17710 case 1: printf (_("MSP430\n")); break;
17711 case 2: printf (_("MSP430X\n")); break;
17712 default: printf ("??? (%d)\n", val); break;
17713 }
17714 break;
17715
17716 case OFBA_MSPABI_Tag_Code_Model:
17717 printf (" Tag_Code_Model: ");
17718 READ_ULEB (val, p, end);
17719 switch (val)
17720 {
17721 case 0: printf (_("None\n")); break;
17722 case 1: printf (_("Small\n")); break;
17723 case 2: printf (_("Large\n")); break;
17724 default: printf ("??? (%d)\n", val); break;
17725 }
17726 break;
17727
17728 case OFBA_MSPABI_Tag_Data_Model:
17729 printf (" Tag_Data_Model: ");
17730 READ_ULEB (val, p, end);
17731 switch (val)
17732 {
17733 case 0: printf (_("None\n")); break;
17734 case 1: printf (_("Small\n")); break;
17735 case 2: printf (_("Large\n")); break;
17736 case 3: printf (_("Restricted Large\n")); break;
17737 default: printf ("??? (%d)\n", val); break;
17738 }
17739 break;
17740
17741 default:
17742 printf (_(" <unknown tag %d>: "), tag);
17743
17744 if (tag & 1)
17745 {
17746 putchar ('"');
17747 if (p < end - 1)
17748 {
17749 size_t maxlen = (end - p) - 1;
17750
17751 print_symbol ((int) maxlen, (const char *) p);
17752 p += strnlen ((char *) p, maxlen) + 1;
17753 }
17754 else
17755 {
17756 printf (_("<corrupt>"));
17757 p = (unsigned char *) end;
17758 }
17759 printf ("\"\n");
17760 }
17761 else
17762 {
17763 READ_ULEB (val, p, end);
17764 printf ("%d (0x%x)\n", val, val);
17765 }
17766 break;
17767 }
17768
17769 assert (p <= end);
17770 return p;
17771 }
17772
17773 static unsigned char *
17774 display_msp430_gnu_attribute (unsigned char * p,
17775 unsigned int tag,
17776 const unsigned char * const end)
17777 {
17778 if (tag == Tag_GNU_MSP430_Data_Region)
17779 {
17780 unsigned int val;
17781
17782 printf (" Tag_GNU_MSP430_Data_Region: ");
17783 READ_ULEB (val, p, end);
17784
17785 switch (val)
17786 {
17787 case Val_GNU_MSP430_Data_Region_Any:
17788 printf (_("Any Region\n"));
17789 break;
17790 case Val_GNU_MSP430_Data_Region_Lower:
17791 printf (_("Lower Region Only\n"));
17792 break;
17793 default:
17794 printf ("??? (%u)\n", val);
17795 }
17796 return p;
17797 }
17798 return display_tag_value (tag & 1, p, end);
17799 }
17800
17801 struct riscv_attr_tag_t {
17802 const char *name;
17803 unsigned int tag;
17804 };
17805
17806 static struct riscv_attr_tag_t riscv_attr_tag[] =
17807 {
17808 #define T(tag) {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
17809 T(arch),
17810 T(priv_spec),
17811 T(priv_spec_minor),
17812 T(priv_spec_revision),
17813 T(unaligned_access),
17814 T(stack_align),
17815 #undef T
17816 };
17817
17818 static unsigned char *
17819 display_riscv_attribute (unsigned char *p,
17820 const unsigned char * const end)
17821 {
17822 unsigned int val;
17823 unsigned int tag;
17824 struct riscv_attr_tag_t *attr = NULL;
17825 unsigned i;
17826
17827 READ_ULEB (tag, p, end);
17828
17829 /* Find the name of attribute. */
17830 for (i = 0; i < ARRAY_SIZE (riscv_attr_tag); i++)
17831 {
17832 if (riscv_attr_tag[i].tag == tag)
17833 {
17834 attr = &riscv_attr_tag[i];
17835 break;
17836 }
17837 }
17838
17839 if (attr)
17840 printf (" %s: ", attr->name);
17841 else
17842 return display_tag_value (tag, p, end);
17843
17844 switch (tag)
17845 {
17846 case Tag_RISCV_priv_spec:
17847 case Tag_RISCV_priv_spec_minor:
17848 case Tag_RISCV_priv_spec_revision:
17849 READ_ULEB (val, p, end);
17850 printf (_("%u\n"), val);
17851 break;
17852 case Tag_RISCV_unaligned_access:
17853 READ_ULEB (val, p, end);
17854 switch (val)
17855 {
17856 case 0:
17857 printf (_("No unaligned access\n"));
17858 break;
17859 case 1:
17860 printf (_("Unaligned access\n"));
17861 break;
17862 }
17863 break;
17864 case Tag_RISCV_stack_align:
17865 READ_ULEB (val, p, end);
17866 printf (_("%u-bytes\n"), val);
17867 break;
17868 case Tag_RISCV_arch:
17869 p = display_tag_value (-1, p, end);
17870 break;
17871 default:
17872 return display_tag_value (tag, p, end);
17873 }
17874
17875 return p;
17876 }
17877
17878 static unsigned char *
17879 display_csky_attribute (unsigned char * p,
17880 const unsigned char * const end)
17881 {
17882 unsigned int tag;
17883 unsigned int val;
17884 READ_ULEB (tag, p, end);
17885
17886 if (tag >= Tag_CSKY_MAX)
17887 {
17888 return display_tag_value (-1, p, end);
17889 }
17890
17891 switch (tag)
17892 {
17893 case Tag_CSKY_ARCH_NAME:
17894 printf (" Tag_CSKY_ARCH_NAME:\t\t");
17895 return display_tag_value (-1, p, end);
17896 case Tag_CSKY_CPU_NAME:
17897 printf (" Tag_CSKY_CPU_NAME:\t\t");
17898 return display_tag_value (-1, p, end);
17899
17900 case Tag_CSKY_ISA_FLAGS:
17901 printf (" Tag_CSKY_ISA_FLAGS:\t\t");
17902 return display_tag_value (0, p, end);
17903 case Tag_CSKY_ISA_EXT_FLAGS:
17904 printf (" Tag_CSKY_ISA_EXT_FLAGS:\t");
17905 return display_tag_value (0, p, end);
17906
17907 case Tag_CSKY_DSP_VERSION:
17908 printf (" Tag_CSKY_DSP_VERSION:\t\t");
17909 READ_ULEB (val, p, end);
17910 if (val == VAL_CSKY_DSP_VERSION_EXTENSION)
17911 printf ("DSP Extension\n");
17912 else if (val == VAL_CSKY_DSP_VERSION_2)
17913 printf ("DSP 2.0\n");
17914 break;
17915
17916 case Tag_CSKY_VDSP_VERSION:
17917 printf (" Tag_CSKY_VDSP_VERSION:\t");
17918 READ_ULEB (val, p, end);
17919 printf ("VDSP Version %d\n", val);
17920 break;
17921
17922 case Tag_CSKY_FPU_VERSION:
17923 printf (" Tag_CSKY_FPU_VERSION:\t\t");
17924 READ_ULEB (val, p, end);
17925 if (val == VAL_CSKY_FPU_VERSION_1)
17926 printf ("ABIV1 FPU Version 1\n");
17927 else if (val == VAL_CSKY_FPU_VERSION_2)
17928 printf ("FPU Version 2\n");
17929 break;
17930
17931 case Tag_CSKY_FPU_ABI:
17932 printf (" Tag_CSKY_FPU_ABI:\t\t");
17933 READ_ULEB (val, p, end);
17934 if (val == VAL_CSKY_FPU_ABI_HARD)
17935 printf ("Hard\n");
17936 else if (val == VAL_CSKY_FPU_ABI_SOFTFP)
17937 printf ("SoftFP\n");
17938 else if (val == VAL_CSKY_FPU_ABI_SOFT)
17939 printf ("Soft\n");
17940 break;
17941 case Tag_CSKY_FPU_ROUNDING:
17942 READ_ULEB (val, p, end);
17943 if (val == 1)
17944 {
17945 printf (" Tag_CSKY_FPU_ROUNDING:\t");
17946 printf ("Needed\n");
17947 }
17948 break;
17949 case Tag_CSKY_FPU_DENORMAL:
17950 READ_ULEB (val, p, end);
17951 if (val == 1)
17952 {
17953 printf (" Tag_CSKY_FPU_DENORMAL:\t");
17954 printf ("Needed\n");
17955 }
17956 break;
17957 case Tag_CSKY_FPU_Exception:
17958 READ_ULEB (val, p, end);
17959 if (val == 1)
17960 {
17961 printf (" Tag_CSKY_FPU_Exception:\t");
17962 printf ("Needed\n");
17963 }
17964 break;
17965 case Tag_CSKY_FPU_NUMBER_MODULE:
17966 printf (" Tag_CSKY_FPU_NUMBER_MODULE:\t");
17967 return display_tag_value (-1, p, end);
17968 case Tag_CSKY_FPU_HARDFP:
17969 printf (" Tag_CSKY_FPU_HARDFP:\t\t");
17970 READ_ULEB (val, p, end);
17971 if (val & VAL_CSKY_FPU_HARDFP_HALF)
17972 printf (" Half");
17973 if (val & VAL_CSKY_FPU_HARDFP_SINGLE)
17974 printf (" Single");
17975 if (val & VAL_CSKY_FPU_HARDFP_DOUBLE)
17976 printf (" Double");
17977 printf ("\n");
17978 break;
17979 default:
17980 return display_tag_value (tag, p, end);
17981 }
17982 return p;
17983 }
17984
17985 static bool
17986 process_attributes (Filedata * filedata,
17987 const char * public_name,
17988 unsigned int proc_type,
17989 unsigned char * (* display_pub_attribute) (unsigned char *, const unsigned char * const),
17990 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const))
17991 {
17992 Elf_Internal_Shdr * sect;
17993 unsigned i;
17994 bool res = true;
17995
17996 /* Find the section header so that we get the size. */
17997 for (i = 0, sect = filedata->section_headers;
17998 i < filedata->file_header.e_shnum;
17999 i++, sect++)
18000 {
18001 unsigned char * contents;
18002 unsigned char * p;
18003
18004 if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
18005 continue;
18006
18007 contents = (unsigned char *) get_data (NULL, filedata, sect->sh_offset, 1,
18008 sect->sh_size, _("attributes"));
18009 if (contents == NULL)
18010 {
18011 res = false;
18012 continue;
18013 }
18014
18015 p = contents;
18016 /* The first character is the version of the attributes.
18017 Currently only version 1, (aka 'A') is recognised here. */
18018 if (*p != 'A')
18019 {
18020 printf (_("Unknown attributes version '%c'(%d) - expecting 'A'\n"), *p, *p);
18021 res = false;
18022 }
18023 else
18024 {
18025 uint64_t section_len;
18026
18027 section_len = sect->sh_size - 1;
18028 p++;
18029
18030 while (section_len > 0)
18031 {
18032 uint64_t attr_len;
18033 unsigned int namelen;
18034 bool public_section;
18035 bool gnu_section;
18036
18037 if (section_len <= 4)
18038 {
18039 error (_("Tag section ends prematurely\n"));
18040 res = false;
18041 break;
18042 }
18043 attr_len = byte_get (p, 4);
18044 p += 4;
18045
18046 if (attr_len > section_len)
18047 {
18048 error (_("Bad attribute length (%u > %u)\n"),
18049 (unsigned) attr_len, (unsigned) section_len);
18050 attr_len = section_len;
18051 res = false;
18052 }
18053 /* PR 17531: file: 001-101425-0.004 */
18054 else if (attr_len < 5)
18055 {
18056 error (_("Attribute length of %u is too small\n"), (unsigned) attr_len);
18057 res = false;
18058 break;
18059 }
18060
18061 section_len -= attr_len;
18062 attr_len -= 4;
18063
18064 namelen = strnlen ((char *) p, attr_len) + 1;
18065 if (namelen == 0 || namelen >= attr_len)
18066 {
18067 error (_("Corrupt attribute section name\n"));
18068 res = false;
18069 break;
18070 }
18071
18072 printf (_("Attribute Section: "));
18073 print_symbol (INT_MAX, (const char *) p);
18074 putchar ('\n');
18075
18076 if (public_name && streq ((char *) p, public_name))
18077 public_section = true;
18078 else
18079 public_section = false;
18080
18081 if (streq ((char *) p, "gnu"))
18082 gnu_section = true;
18083 else
18084 gnu_section = false;
18085
18086 p += namelen;
18087 attr_len -= namelen;
18088
18089 while (attr_len > 0 && p < contents + sect->sh_size)
18090 {
18091 int tag;
18092 unsigned int val;
18093 uint64_t size;
18094 unsigned char * end;
18095
18096 /* PR binutils/17531: Safe handling of corrupt files. */
18097 if (attr_len < 6)
18098 {
18099 error (_("Unused bytes at end of section\n"));
18100 res = false;
18101 section_len = 0;
18102 break;
18103 }
18104
18105 tag = *(p++);
18106 size = byte_get (p, 4);
18107 if (size > attr_len)
18108 {
18109 error (_("Bad subsection length (%u > %u)\n"),
18110 (unsigned) size, (unsigned) attr_len);
18111 res = false;
18112 size = attr_len;
18113 }
18114 /* PR binutils/17531: Safe handling of corrupt files. */
18115 if (size < 6)
18116 {
18117 error (_("Bad subsection length (%u < 6)\n"),
18118 (unsigned) size);
18119 res = false;
18120 section_len = 0;
18121 break;
18122 }
18123
18124 attr_len -= size;
18125 end = p + size - 1;
18126 assert (end <= contents + sect->sh_size);
18127 p += 4;
18128
18129 switch (tag)
18130 {
18131 case 1:
18132 printf (_("File Attributes\n"));
18133 break;
18134 case 2:
18135 printf (_("Section Attributes:"));
18136 goto do_numlist;
18137 case 3:
18138 printf (_("Symbol Attributes:"));
18139 /* Fall through. */
18140 do_numlist:
18141 for (;;)
18142 {
18143 READ_ULEB (val, p, end);
18144 if (val == 0)
18145 break;
18146 printf (" %d", val);
18147 }
18148 printf ("\n");
18149 break;
18150 default:
18151 printf (_("Unknown tag: %d\n"), tag);
18152 public_section = false;
18153 break;
18154 }
18155
18156 if (public_section && display_pub_attribute != NULL)
18157 {
18158 while (p < end)
18159 p = display_pub_attribute (p, end);
18160 assert (p == end);
18161 }
18162 else if (gnu_section && display_proc_gnu_attribute != NULL)
18163 {
18164 while (p < end)
18165 p = display_gnu_attribute (p,
18166 display_proc_gnu_attribute,
18167 end);
18168 assert (p == end);
18169 }
18170 else if (p < end)
18171 {
18172 printf (_(" Unknown attribute:\n"));
18173 display_raw_attribute (p, end);
18174 p = end;
18175 }
18176 else
18177 attr_len = 0;
18178 }
18179 }
18180 }
18181
18182 free (contents);
18183 }
18184
18185 return res;
18186 }
18187
18188 /* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
18189 Print the Address, Access and Initial fields of an entry at VMA ADDR
18190 and return the VMA of the next entry, or -1 if there was a problem.
18191 Does not read from DATA_END or beyond. */
18192
18193 static uint64_t
18194 print_mips_got_entry (unsigned char * data, uint64_t pltgot, uint64_t addr,
18195 unsigned char * data_end)
18196 {
18197 printf (" ");
18198 print_vma (addr, LONG_HEX);
18199 printf (" ");
18200 if (addr < pltgot + 0xfff0)
18201 printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
18202 else
18203 printf ("%10s", "");
18204 printf (" ");
18205 if (data == NULL)
18206 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
18207 else
18208 {
18209 uint64_t entry;
18210 unsigned char * from = data + addr - pltgot;
18211
18212 if (from + (is_32bit_elf ? 4 : 8) > data_end)
18213 {
18214 warn (_("MIPS GOT entry extends beyond the end of available data\n"));
18215 printf ("%*s", is_32bit_elf ? 8 : 16, _("<corrupt>"));
18216 return (uint64_t) -1;
18217 }
18218 else
18219 {
18220 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
18221 print_vma (entry, LONG_HEX);
18222 }
18223 }
18224 return addr + (is_32bit_elf ? 4 : 8);
18225 }
18226
18227 /* DATA points to the contents of a MIPS PLT GOT that starts at VMA
18228 PLTGOT. Print the Address and Initial fields of an entry at VMA
18229 ADDR and return the VMA of the next entry. */
18230
18231 static uint64_t
18232 print_mips_pltgot_entry (unsigned char * data, uint64_t pltgot, uint64_t addr)
18233 {
18234 printf (" ");
18235 print_vma (addr, LONG_HEX);
18236 printf (" ");
18237 if (data == NULL)
18238 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
18239 else
18240 {
18241 uint64_t entry;
18242
18243 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
18244 print_vma (entry, LONG_HEX);
18245 }
18246 return addr + (is_32bit_elf ? 4 : 8);
18247 }
18248
18249 static void
18250 print_mips_ases (unsigned int mask)
18251 {
18252 if (mask & AFL_ASE_DSP)
18253 fputs ("\n\tDSP ASE", stdout);
18254 if (mask & AFL_ASE_DSPR2)
18255 fputs ("\n\tDSP R2 ASE", stdout);
18256 if (mask & AFL_ASE_DSPR3)
18257 fputs ("\n\tDSP R3 ASE", stdout);
18258 if (mask & AFL_ASE_EVA)
18259 fputs ("\n\tEnhanced VA Scheme", stdout);
18260 if (mask & AFL_ASE_MCU)
18261 fputs ("\n\tMCU (MicroController) ASE", stdout);
18262 if (mask & AFL_ASE_MDMX)
18263 fputs ("\n\tMDMX ASE", stdout);
18264 if (mask & AFL_ASE_MIPS3D)
18265 fputs ("\n\tMIPS-3D ASE", stdout);
18266 if (mask & AFL_ASE_MT)
18267 fputs ("\n\tMT ASE", stdout);
18268 if (mask & AFL_ASE_SMARTMIPS)
18269 fputs ("\n\tSmartMIPS ASE", stdout);
18270 if (mask & AFL_ASE_VIRT)
18271 fputs ("\n\tVZ ASE", stdout);
18272 if (mask & AFL_ASE_MSA)
18273 fputs ("\n\tMSA ASE", stdout);
18274 if (mask & AFL_ASE_MIPS16)
18275 fputs ("\n\tMIPS16 ASE", stdout);
18276 if (mask & AFL_ASE_MICROMIPS)
18277 fputs ("\n\tMICROMIPS ASE", stdout);
18278 if (mask & AFL_ASE_XPA)
18279 fputs ("\n\tXPA ASE", stdout);
18280 if (mask & AFL_ASE_MIPS16E2)
18281 fputs ("\n\tMIPS16e2 ASE", stdout);
18282 if (mask & AFL_ASE_CRC)
18283 fputs ("\n\tCRC ASE", stdout);
18284 if (mask & AFL_ASE_GINV)
18285 fputs ("\n\tGINV ASE", stdout);
18286 if (mask & AFL_ASE_LOONGSON_MMI)
18287 fputs ("\n\tLoongson MMI ASE", stdout);
18288 if (mask & AFL_ASE_LOONGSON_CAM)
18289 fputs ("\n\tLoongson CAM ASE", stdout);
18290 if (mask & AFL_ASE_LOONGSON_EXT)
18291 fputs ("\n\tLoongson EXT ASE", stdout);
18292 if (mask & AFL_ASE_LOONGSON_EXT2)
18293 fputs ("\n\tLoongson EXT2 ASE", stdout);
18294 if (mask == 0)
18295 fprintf (stdout, "\n\t%s", _("None"));
18296 else if ((mask & ~AFL_ASE_MASK) != 0)
18297 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
18298 }
18299
18300 static void
18301 print_mips_isa_ext (unsigned int isa_ext)
18302 {
18303 switch (isa_ext)
18304 {
18305 case 0:
18306 fputs (_("None"), stdout);
18307 break;
18308 case AFL_EXT_XLR:
18309 fputs ("RMI XLR", stdout);
18310 break;
18311 case AFL_EXT_OCTEON3:
18312 fputs ("Cavium Networks Octeon3", stdout);
18313 break;
18314 case AFL_EXT_OCTEON2:
18315 fputs ("Cavium Networks Octeon2", stdout);
18316 break;
18317 case AFL_EXT_OCTEONP:
18318 fputs ("Cavium Networks OcteonP", stdout);
18319 break;
18320 case AFL_EXT_OCTEON:
18321 fputs ("Cavium Networks Octeon", stdout);
18322 break;
18323 case AFL_EXT_5900:
18324 fputs ("Toshiba R5900", stdout);
18325 break;
18326 case AFL_EXT_4650:
18327 fputs ("MIPS R4650", stdout);
18328 break;
18329 case AFL_EXT_4010:
18330 fputs ("LSI R4010", stdout);
18331 break;
18332 case AFL_EXT_4100:
18333 fputs ("NEC VR4100", stdout);
18334 break;
18335 case AFL_EXT_3900:
18336 fputs ("Toshiba R3900", stdout);
18337 break;
18338 case AFL_EXT_10000:
18339 fputs ("MIPS R10000", stdout);
18340 break;
18341 case AFL_EXT_SB1:
18342 fputs ("Broadcom SB-1", stdout);
18343 break;
18344 case AFL_EXT_4111:
18345 fputs ("NEC VR4111/VR4181", stdout);
18346 break;
18347 case AFL_EXT_4120:
18348 fputs ("NEC VR4120", stdout);
18349 break;
18350 case AFL_EXT_5400:
18351 fputs ("NEC VR5400", stdout);
18352 break;
18353 case AFL_EXT_5500:
18354 fputs ("NEC VR5500", stdout);
18355 break;
18356 case AFL_EXT_LOONGSON_2E:
18357 fputs ("ST Microelectronics Loongson 2E", stdout);
18358 break;
18359 case AFL_EXT_LOONGSON_2F:
18360 fputs ("ST Microelectronics Loongson 2F", stdout);
18361 break;
18362 case AFL_EXT_INTERAPTIV_MR2:
18363 fputs ("Imagination interAptiv MR2", stdout);
18364 break;
18365 default:
18366 fprintf (stdout, "%s (%d)", _("Unknown"), isa_ext);
18367 }
18368 }
18369
18370 static signed int
18371 get_mips_reg_size (int reg_size)
18372 {
18373 return (reg_size == AFL_REG_NONE) ? 0
18374 : (reg_size == AFL_REG_32) ? 32
18375 : (reg_size == AFL_REG_64) ? 64
18376 : (reg_size == AFL_REG_128) ? 128
18377 : -1;
18378 }
18379
18380 static bool
18381 process_mips_specific (Filedata * filedata)
18382 {
18383 Elf_Internal_Dyn * entry;
18384 Elf_Internal_Shdr *sect = NULL;
18385 size_t liblist_offset = 0;
18386 size_t liblistno = 0;
18387 size_t conflictsno = 0;
18388 size_t options_offset = 0;
18389 size_t conflicts_offset = 0;
18390 size_t pltrelsz = 0;
18391 size_t pltrel = 0;
18392 uint64_t pltgot = 0;
18393 uint64_t mips_pltgot = 0;
18394 uint64_t jmprel = 0;
18395 uint64_t local_gotno = 0;
18396 uint64_t gotsym = 0;
18397 uint64_t symtabno = 0;
18398 bool res = true;
18399
18400 if (! process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
18401 display_mips_gnu_attribute))
18402 res = false;
18403
18404 sect = find_section (filedata, ".MIPS.abiflags");
18405
18406 if (sect != NULL)
18407 {
18408 Elf_External_ABIFlags_v0 *abiflags_ext;
18409 Elf_Internal_ABIFlags_v0 abiflags_in;
18410
18411 if (sizeof (Elf_External_ABIFlags_v0) != sect->sh_size)
18412 {
18413 error (_("Corrupt MIPS ABI Flags section.\n"));
18414 res = false;
18415 }
18416 else
18417 {
18418 abiflags_ext = get_data (NULL, filedata, sect->sh_offset, 1,
18419 sect->sh_size, _("MIPS ABI Flags section"));
18420 if (abiflags_ext)
18421 {
18422 abiflags_in.version = BYTE_GET (abiflags_ext->version);
18423 abiflags_in.isa_level = BYTE_GET (abiflags_ext->isa_level);
18424 abiflags_in.isa_rev = BYTE_GET (abiflags_ext->isa_rev);
18425 abiflags_in.gpr_size = BYTE_GET (abiflags_ext->gpr_size);
18426 abiflags_in.cpr1_size = BYTE_GET (abiflags_ext->cpr1_size);
18427 abiflags_in.cpr2_size = BYTE_GET (abiflags_ext->cpr2_size);
18428 abiflags_in.fp_abi = BYTE_GET (abiflags_ext->fp_abi);
18429 abiflags_in.isa_ext = BYTE_GET (abiflags_ext->isa_ext);
18430 abiflags_in.ases = BYTE_GET (abiflags_ext->ases);
18431 abiflags_in.flags1 = BYTE_GET (abiflags_ext->flags1);
18432 abiflags_in.flags2 = BYTE_GET (abiflags_ext->flags2);
18433
18434 printf ("\nMIPS ABI Flags Version: %d\n", abiflags_in.version);
18435 printf ("\nISA: MIPS%d", abiflags_in.isa_level);
18436 if (abiflags_in.isa_rev > 1)
18437 printf ("r%d", abiflags_in.isa_rev);
18438 printf ("\nGPR size: %d",
18439 get_mips_reg_size (abiflags_in.gpr_size));
18440 printf ("\nCPR1 size: %d",
18441 get_mips_reg_size (abiflags_in.cpr1_size));
18442 printf ("\nCPR2 size: %d",
18443 get_mips_reg_size (abiflags_in.cpr2_size));
18444 fputs ("\nFP ABI: ", stdout);
18445 print_mips_fp_abi_value (abiflags_in.fp_abi);
18446 fputs ("ISA Extension: ", stdout);
18447 print_mips_isa_ext (abiflags_in.isa_ext);
18448 fputs ("\nASEs:", stdout);
18449 print_mips_ases (abiflags_in.ases);
18450 printf ("\nFLAGS 1: %8.8lx", abiflags_in.flags1);
18451 printf ("\nFLAGS 2: %8.8lx", abiflags_in.flags2);
18452 fputc ('\n', stdout);
18453 free (abiflags_ext);
18454 }
18455 }
18456 }
18457
18458 /* We have a lot of special sections. Thanks SGI! */
18459 if (filedata->dynamic_section == NULL)
18460 {
18461 /* No dynamic information available. See if there is static GOT. */
18462 sect = find_section (filedata, ".got");
18463 if (sect != NULL)
18464 {
18465 unsigned char *data_end;
18466 unsigned char *data;
18467 uint64_t ent, end;
18468 int addr_size;
18469
18470 pltgot = sect->sh_addr;
18471
18472 ent = pltgot;
18473 addr_size = (is_32bit_elf ? 4 : 8);
18474 end = pltgot + sect->sh_size;
18475
18476 data = (unsigned char *) get_data (NULL, filedata, sect->sh_offset,
18477 end - pltgot, 1,
18478 _("Global Offset Table data"));
18479 /* PR 12855: Null data is handled gracefully throughout. */
18480 data_end = data + (end - pltgot);
18481
18482 printf (_("\nStatic GOT:\n"));
18483 printf (_(" Canonical gp value: "));
18484 print_vma (ent + 0x7ff0, LONG_HEX);
18485 printf ("\n\n");
18486
18487 /* In a dynamic binary GOT[0] is reserved for the dynamic
18488 loader to store the lazy resolver pointer, however in
18489 a static binary it may well have been omitted and GOT
18490 reduced to a table of addresses.
18491 PR 21344: Check for the entry being fully available
18492 before fetching it. */
18493 if (data
18494 && data + ent - pltgot + addr_size <= data_end
18495 && byte_get (data + ent - pltgot, addr_size) == 0)
18496 {
18497 printf (_(" Reserved entries:\n"));
18498 printf (_(" %*s %10s %*s\n"),
18499 addr_size * 2, _("Address"), _("Access"),
18500 addr_size * 2, _("Value"));
18501 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18502 printf ("\n");
18503 if (ent == (uint64_t) -1)
18504 goto sgot_print_fail;
18505
18506 /* Check for the MSB of GOT[1] being set, identifying a
18507 GNU object. This entry will be used by some runtime
18508 loaders, to store the module pointer. Otherwise this
18509 is an ordinary local entry.
18510 PR 21344: Check for the entry being fully available
18511 before fetching it. */
18512 if (data
18513 && data + ent - pltgot + addr_size <= data_end
18514 && (byte_get (data + ent - pltgot, addr_size)
18515 >> (addr_size * 8 - 1)) != 0)
18516 {
18517 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18518 printf ("\n");
18519 if (ent == (uint64_t) -1)
18520 goto sgot_print_fail;
18521 }
18522 printf ("\n");
18523 }
18524
18525 if (data != NULL && ent < end)
18526 {
18527 printf (_(" Local entries:\n"));
18528 printf (" %*s %10s %*s\n",
18529 addr_size * 2, _("Address"), _("Access"),
18530 addr_size * 2, _("Value"));
18531 while (ent < end)
18532 {
18533 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18534 printf ("\n");
18535 if (ent == (uint64_t) -1)
18536 goto sgot_print_fail;
18537 }
18538 printf ("\n");
18539 }
18540
18541 sgot_print_fail:
18542 free (data);
18543 }
18544 return res;
18545 }
18546
18547 for (entry = filedata->dynamic_section;
18548 /* PR 17531 file: 012-50589-0.004. */
18549 (entry < filedata->dynamic_section + filedata->dynamic_nent
18550 && entry->d_tag != DT_NULL);
18551 ++entry)
18552 switch (entry->d_tag)
18553 {
18554 case DT_MIPS_LIBLIST:
18555 liblist_offset
18556 = offset_from_vma (filedata, entry->d_un.d_val,
18557 liblistno * sizeof (Elf32_External_Lib));
18558 break;
18559 case DT_MIPS_LIBLISTNO:
18560 liblistno = entry->d_un.d_val;
18561 break;
18562 case DT_MIPS_OPTIONS:
18563 options_offset = offset_from_vma (filedata, entry->d_un.d_val, 0);
18564 break;
18565 case DT_MIPS_CONFLICT:
18566 conflicts_offset
18567 = offset_from_vma (filedata, entry->d_un.d_val,
18568 conflictsno * sizeof (Elf32_External_Conflict));
18569 break;
18570 case DT_MIPS_CONFLICTNO:
18571 conflictsno = entry->d_un.d_val;
18572 break;
18573 case DT_PLTGOT:
18574 pltgot = entry->d_un.d_ptr;
18575 break;
18576 case DT_MIPS_LOCAL_GOTNO:
18577 local_gotno = entry->d_un.d_val;
18578 break;
18579 case DT_MIPS_GOTSYM:
18580 gotsym = entry->d_un.d_val;
18581 break;
18582 case DT_MIPS_SYMTABNO:
18583 symtabno = entry->d_un.d_val;
18584 break;
18585 case DT_MIPS_PLTGOT:
18586 mips_pltgot = entry->d_un.d_ptr;
18587 break;
18588 case DT_PLTREL:
18589 pltrel = entry->d_un.d_val;
18590 break;
18591 case DT_PLTRELSZ:
18592 pltrelsz = entry->d_un.d_val;
18593 break;
18594 case DT_JMPREL:
18595 jmprel = entry->d_un.d_ptr;
18596 break;
18597 default:
18598 break;
18599 }
18600
18601 if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
18602 {
18603 Elf32_External_Lib * elib;
18604 size_t cnt;
18605
18606 elib = (Elf32_External_Lib *) get_data (NULL, filedata, liblist_offset,
18607 sizeof (Elf32_External_Lib),
18608 liblistno,
18609 _("liblist section data"));
18610 if (elib)
18611 {
18612 printf (ngettext ("\nSection '.liblist' contains %lu entry:\n",
18613 "\nSection '.liblist' contains %lu entries:\n",
18614 (unsigned long) liblistno),
18615 (unsigned long) liblistno);
18616 fputs (_(" Library Time Stamp Checksum Version Flags\n"),
18617 stdout);
18618
18619 for (cnt = 0; cnt < liblistno; ++cnt)
18620 {
18621 Elf32_Lib liblist;
18622 time_t atime;
18623 char timebuf[128];
18624 struct tm * tmp;
18625
18626 liblist.l_name = BYTE_GET (elib[cnt].l_name);
18627 atime = BYTE_GET (elib[cnt].l_time_stamp);
18628 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
18629 liblist.l_version = BYTE_GET (elib[cnt].l_version);
18630 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
18631
18632 tmp = gmtime (&atime);
18633 snprintf (timebuf, sizeof (timebuf),
18634 "%04u-%02u-%02uT%02u:%02u:%02u",
18635 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
18636 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
18637
18638 printf ("%3lu: ", (unsigned long) cnt);
18639 if (valid_dynamic_name (filedata, liblist.l_name))
18640 print_symbol (20, get_dynamic_name (filedata, liblist.l_name));
18641 else
18642 printf (_("<corrupt: %9ld>"), liblist.l_name);
18643 printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
18644 liblist.l_version);
18645
18646 if (liblist.l_flags == 0)
18647 puts (_(" NONE"));
18648 else
18649 {
18650 static const struct
18651 {
18652 const char * name;
18653 int bit;
18654 }
18655 l_flags_vals[] =
18656 {
18657 { " EXACT_MATCH", LL_EXACT_MATCH },
18658 { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
18659 { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
18660 { " EXPORTS", LL_EXPORTS },
18661 { " DELAY_LOAD", LL_DELAY_LOAD },
18662 { " DELTA", LL_DELTA }
18663 };
18664 int flags = liblist.l_flags;
18665 size_t fcnt;
18666
18667 for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
18668 if ((flags & l_flags_vals[fcnt].bit) != 0)
18669 {
18670 fputs (l_flags_vals[fcnt].name, stdout);
18671 flags ^= l_flags_vals[fcnt].bit;
18672 }
18673 if (flags != 0)
18674 printf (" %#x", (unsigned int) flags);
18675
18676 puts ("");
18677 }
18678 }
18679
18680 free (elib);
18681 }
18682 else
18683 res = false;
18684 }
18685
18686 if (options_offset != 0)
18687 {
18688 Elf_External_Options * eopt;
18689 size_t offset;
18690 int cnt;
18691
18692 /* Find the section header so that we get the size. */
18693 sect = find_section_by_type (filedata, SHT_MIPS_OPTIONS);
18694 /* PR 17533 file: 012-277276-0.004. */
18695 if (sect == NULL)
18696 {
18697 error (_("No MIPS_OPTIONS header found\n"));
18698 return false;
18699 }
18700 /* PR 24243 */
18701 if (sect->sh_size < sizeof (* eopt))
18702 {
18703 error (_("The MIPS options section is too small.\n"));
18704 return false;
18705 }
18706
18707 eopt = (Elf_External_Options *) get_data (NULL, filedata, options_offset, 1,
18708 sect->sh_size, _("options"));
18709 if (eopt)
18710 {
18711 Elf_Internal_Options option;
18712
18713 offset = cnt = 0;
18714 while (offset <= sect->sh_size - sizeof (* eopt))
18715 {
18716 Elf_External_Options * eoption;
18717 unsigned int optsize;
18718
18719 eoption = (Elf_External_Options *) ((char *) eopt + offset);
18720
18721 optsize = BYTE_GET (eoption->size);
18722
18723 /* PR 17531: file: ffa0fa3b. */
18724 if (optsize < sizeof (* eopt)
18725 || optsize > sect->sh_size - offset)
18726 {
18727 error (_("Invalid size (%u) for MIPS option\n"),
18728 optsize);
18729 free (eopt);
18730 return false;
18731 }
18732 offset += optsize;
18733 ++cnt;
18734 }
18735
18736 printf (ngettext ("\nSection '%s' contains %d entry:\n",
18737 "\nSection '%s' contains %d entries:\n",
18738 cnt),
18739 printable_section_name (filedata, sect), cnt);
18740
18741 offset = 0;
18742 while (cnt-- > 0)
18743 {
18744 size_t len;
18745 Elf_External_Options * eoption;
18746
18747 eoption = (Elf_External_Options *) ((char *) eopt + offset);
18748
18749 option.kind = BYTE_GET (eoption->kind);
18750 option.size = BYTE_GET (eoption->size);
18751 option.section = BYTE_GET (eoption->section);
18752 option.info = BYTE_GET (eoption->info);
18753
18754 switch (option.kind)
18755 {
18756 case ODK_NULL:
18757 /* This shouldn't happen. */
18758 printf (" NULL %" PRId16 " %" PRIx32,
18759 option.section, option.info);
18760 break;
18761
18762 case ODK_REGINFO:
18763 printf (" REGINFO ");
18764 if (filedata->file_header.e_machine == EM_MIPS)
18765 {
18766 Elf32_External_RegInfo * ereg;
18767 Elf32_RegInfo reginfo;
18768
18769 /* 32bit form. */
18770 if (option.size < (sizeof (Elf_External_Options)
18771 + sizeof (Elf32_External_RegInfo)))
18772 {
18773 printf (_("<corrupt>\n"));
18774 error (_("Truncated MIPS REGINFO option\n"));
18775 cnt = 0;
18776 break;
18777 }
18778
18779 ereg = (Elf32_External_RegInfo *) (eoption + 1);
18780
18781 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
18782 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
18783 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
18784 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
18785 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
18786 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
18787
18788 printf ("GPR %08" PRIx32 " GP 0x%" PRIx32 "\n",
18789 reginfo.ri_gprmask, reginfo.ri_gp_value);
18790 printf (" "
18791 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
18792 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
18793 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
18794 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
18795 }
18796 else
18797 {
18798 /* 64 bit form. */
18799 Elf64_External_RegInfo * ereg;
18800 Elf64_Internal_RegInfo reginfo;
18801
18802 if (option.size < (sizeof (Elf_External_Options)
18803 + sizeof (Elf64_External_RegInfo)))
18804 {
18805 printf (_("<corrupt>\n"));
18806 error (_("Truncated MIPS REGINFO option\n"));
18807 cnt = 0;
18808 break;
18809 }
18810
18811 ereg = (Elf64_External_RegInfo *) (eoption + 1);
18812 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
18813 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
18814 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
18815 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
18816 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
18817 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
18818
18819 printf ("GPR %08" PRIx32 " GP 0x%" PRIx64 "\n",
18820 reginfo.ri_gprmask, reginfo.ri_gp_value);
18821 printf (" "
18822 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
18823 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
18824 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
18825 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
18826 }
18827 offset += option.size;
18828 continue;
18829
18830 case ODK_EXCEPTIONS:
18831 fputs (" EXCEPTIONS fpe_min(", stdout);
18832 process_mips_fpe_exception (option.info & OEX_FPU_MIN);
18833 fputs (") fpe_max(", stdout);
18834 process_mips_fpe_exception ((option.info & OEX_FPU_MAX) >> 8);
18835 fputs (")", stdout);
18836
18837 if (option.info & OEX_PAGE0)
18838 fputs (" PAGE0", stdout);
18839 if (option.info & OEX_SMM)
18840 fputs (" SMM", stdout);
18841 if (option.info & OEX_FPDBUG)
18842 fputs (" FPDBUG", stdout);
18843 if (option.info & OEX_DISMISS)
18844 fputs (" DISMISS", stdout);
18845 break;
18846
18847 case ODK_PAD:
18848 fputs (" PAD ", stdout);
18849 if (option.info & OPAD_PREFIX)
18850 fputs (" PREFIX", stdout);
18851 if (option.info & OPAD_POSTFIX)
18852 fputs (" POSTFIX", stdout);
18853 if (option.info & OPAD_SYMBOL)
18854 fputs (" SYMBOL", stdout);
18855 break;
18856
18857 case ODK_HWPATCH:
18858 fputs (" HWPATCH ", stdout);
18859 if (option.info & OHW_R4KEOP)
18860 fputs (" R4KEOP", stdout);
18861 if (option.info & OHW_R8KPFETCH)
18862 fputs (" R8KPFETCH", stdout);
18863 if (option.info & OHW_R5KEOP)
18864 fputs (" R5KEOP", stdout);
18865 if (option.info & OHW_R5KCVTL)
18866 fputs (" R5KCVTL", stdout);
18867 break;
18868
18869 case ODK_FILL:
18870 fputs (" FILL ", stdout);
18871 /* XXX Print content of info word? */
18872 break;
18873
18874 case ODK_TAGS:
18875 fputs (" TAGS ", stdout);
18876 /* XXX Print content of info word? */
18877 break;
18878
18879 case ODK_HWAND:
18880 fputs (" HWAND ", stdout);
18881 if (option.info & OHWA0_R4KEOP_CHECKED)
18882 fputs (" R4KEOP_CHECKED", stdout);
18883 if (option.info & OHWA0_R4KEOP_CLEAN)
18884 fputs (" R4KEOP_CLEAN", stdout);
18885 break;
18886
18887 case ODK_HWOR:
18888 fputs (" HWOR ", stdout);
18889 if (option.info & OHWA0_R4KEOP_CHECKED)
18890 fputs (" R4KEOP_CHECKED", stdout);
18891 if (option.info & OHWA0_R4KEOP_CLEAN)
18892 fputs (" R4KEOP_CLEAN", stdout);
18893 break;
18894
18895 case ODK_GP_GROUP:
18896 printf (" GP_GROUP %#06x self-contained %#06x",
18897 option.info & OGP_GROUP,
18898 (option.info & OGP_SELF) >> 16);
18899 break;
18900
18901 case ODK_IDENT:
18902 printf (" IDENT %#06x self-contained %#06x",
18903 option.info & OGP_GROUP,
18904 (option.info & OGP_SELF) >> 16);
18905 break;
18906
18907 default:
18908 /* This shouldn't happen. */
18909 printf (" %3d ??? %" PRId16 " %" PRIx32,
18910 option.kind, option.section, option.info);
18911 break;
18912 }
18913
18914 len = sizeof (* eopt);
18915 while (len < option.size)
18916 {
18917 unsigned char datum = *((unsigned char *) eoption + len);
18918
18919 if (ISPRINT (datum))
18920 printf ("%c", datum);
18921 else
18922 printf ("\\%03o", datum);
18923 len ++;
18924 }
18925 fputs ("\n", stdout);
18926
18927 offset += option.size;
18928 }
18929 free (eopt);
18930 }
18931 else
18932 res = false;
18933 }
18934
18935 if (conflicts_offset != 0 && conflictsno != 0)
18936 {
18937 Elf32_Conflict * iconf;
18938 size_t cnt;
18939
18940 if (filedata->dynamic_symbols == NULL)
18941 {
18942 error (_("conflict list found without a dynamic symbol table\n"));
18943 return false;
18944 }
18945
18946 /* PR 21345 - print a slightly more helpful error message
18947 if we are sure that the cmalloc will fail. */
18948 if (conflictsno > filedata->file_size / sizeof (* iconf))
18949 {
18950 error (_("Overlarge number of conflicts detected: %lx\n"),
18951 (long) conflictsno);
18952 return false;
18953 }
18954
18955 iconf = (Elf32_Conflict *) cmalloc (conflictsno, sizeof (* iconf));
18956 if (iconf == NULL)
18957 {
18958 error (_("Out of memory allocating space for dynamic conflicts\n"));
18959 return false;
18960 }
18961
18962 if (is_32bit_elf)
18963 {
18964 Elf32_External_Conflict * econf32;
18965
18966 econf32 = (Elf32_External_Conflict *)
18967 get_data (NULL, filedata, conflicts_offset,
18968 sizeof (*econf32), conflictsno, _("conflict"));
18969 if (!econf32)
18970 {
18971 free (iconf);
18972 return false;
18973 }
18974
18975 for (cnt = 0; cnt < conflictsno; ++cnt)
18976 iconf[cnt] = BYTE_GET (econf32[cnt]);
18977
18978 free (econf32);
18979 }
18980 else
18981 {
18982 Elf64_External_Conflict * econf64;
18983
18984 econf64 = (Elf64_External_Conflict *)
18985 get_data (NULL, filedata, conflicts_offset,
18986 sizeof (*econf64), conflictsno, _("conflict"));
18987 if (!econf64)
18988 {
18989 free (iconf);
18990 return false;
18991 }
18992
18993 for (cnt = 0; cnt < conflictsno; ++cnt)
18994 iconf[cnt] = BYTE_GET (econf64[cnt]);
18995
18996 free (econf64);
18997 }
18998
18999 printf (ngettext ("\nSection '.conflict' contains %lu entry:\n",
19000 "\nSection '.conflict' contains %lu entries:\n",
19001 (unsigned long) conflictsno),
19002 (unsigned long) conflictsno);
19003 puts (_(" Num: Index Value Name"));
19004
19005 for (cnt = 0; cnt < conflictsno; ++cnt)
19006 {
19007 printf ("%5lu: %8lu ", (unsigned long) cnt, iconf[cnt]);
19008
19009 if (iconf[cnt] >= filedata->num_dynamic_syms)
19010 printf (_("<corrupt symbol index>"));
19011 else
19012 {
19013 Elf_Internal_Sym * psym;
19014
19015 psym = & filedata->dynamic_symbols[iconf[cnt]];
19016 print_vma (psym->st_value, FULL_HEX);
19017 putchar (' ');
19018 if (valid_dynamic_name (filedata, psym->st_name))
19019 print_symbol (25, get_dynamic_name (filedata, psym->st_name));
19020 else
19021 printf (_("<corrupt: %14ld>"), psym->st_name);
19022 }
19023 putchar ('\n');
19024 }
19025
19026 free (iconf);
19027 }
19028
19029 if (pltgot != 0 && local_gotno != 0)
19030 {
19031 uint64_t ent, local_end, global_end;
19032 size_t i, offset;
19033 unsigned char * data;
19034 unsigned char * data_end;
19035 int addr_size;
19036
19037 ent = pltgot;
19038 addr_size = (is_32bit_elf ? 4 : 8);
19039 local_end = pltgot + local_gotno * addr_size;
19040
19041 /* PR binutils/17533 file: 012-111227-0.004 */
19042 if (symtabno < gotsym)
19043 {
19044 error (_("The GOT symbol offset (%lu) is greater than the symbol table size (%lu)\n"),
19045 (unsigned long) gotsym, (unsigned long) symtabno);
19046 return false;
19047 }
19048
19049 global_end = local_end + (symtabno - gotsym) * addr_size;
19050 /* PR 17531: file: 54c91a34. */
19051 if (global_end < local_end)
19052 {
19053 error (_("Too many GOT symbols: %lu\n"), (unsigned long) symtabno);
19054 return false;
19055 }
19056
19057 offset = offset_from_vma (filedata, pltgot, global_end - pltgot);
19058 data = (unsigned char *) get_data (NULL, filedata, offset,
19059 global_end - pltgot, 1,
19060 _("Global Offset Table data"));
19061 /* PR 12855: Null data is handled gracefully throughout. */
19062 data_end = data + (global_end - pltgot);
19063
19064 printf (_("\nPrimary GOT:\n"));
19065 printf (_(" Canonical gp value: "));
19066 print_vma (pltgot + 0x7ff0, LONG_HEX);
19067 printf ("\n\n");
19068
19069 printf (_(" Reserved entries:\n"));
19070 printf (_(" %*s %10s %*s Purpose\n"),
19071 addr_size * 2, _("Address"), _("Access"),
19072 addr_size * 2, _("Initial"));
19073 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19074 printf (_(" Lazy resolver\n"));
19075 if (ent == (uint64_t) -1)
19076 goto got_print_fail;
19077
19078 /* Check for the MSB of GOT[1] being set, denoting a GNU object.
19079 This entry will be used by some runtime loaders, to store the
19080 module pointer. Otherwise this is an ordinary local entry.
19081 PR 21344: Check for the entry being fully available before
19082 fetching it. */
19083 if (data
19084 && data + ent - pltgot + addr_size <= data_end
19085 && (byte_get (data + ent - pltgot, addr_size)
19086 >> (addr_size * 8 - 1)) != 0)
19087 {
19088 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19089 printf (_(" Module pointer (GNU extension)\n"));
19090 if (ent == (uint64_t) -1)
19091 goto got_print_fail;
19092 }
19093 printf ("\n");
19094
19095 if (data != NULL && ent < local_end)
19096 {
19097 printf (_(" Local entries:\n"));
19098 printf (" %*s %10s %*s\n",
19099 addr_size * 2, _("Address"), _("Access"),
19100 addr_size * 2, _("Initial"));
19101 while (ent < local_end)
19102 {
19103 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19104 printf ("\n");
19105 if (ent == (uint64_t) -1)
19106 goto got_print_fail;
19107 }
19108 printf ("\n");
19109 }
19110
19111 if (data != NULL && gotsym < symtabno)
19112 {
19113 int sym_width;
19114
19115 printf (_(" Global entries:\n"));
19116 printf (" %*s %10s %*s %*s %-7s %3s %s\n",
19117 addr_size * 2, _("Address"),
19118 _("Access"),
19119 addr_size * 2, _("Initial"),
19120 addr_size * 2, _("Sym.Val."),
19121 _("Type"),
19122 /* Note for translators: "Ndx" = abbreviated form of "Index". */
19123 _("Ndx"), _("Name"));
19124
19125 sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
19126
19127 for (i = gotsym; i < symtabno; i++)
19128 {
19129 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19130 printf (" ");
19131
19132 if (filedata->dynamic_symbols == NULL)
19133 printf (_("<no dynamic symbols>"));
19134 else if (i < filedata->num_dynamic_syms)
19135 {
19136 Elf_Internal_Sym * psym = filedata->dynamic_symbols + i;
19137
19138 print_vma (psym->st_value, LONG_HEX);
19139 printf (" %-7s %3s ",
19140 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
19141 get_symbol_index_type (filedata, psym->st_shndx));
19142
19143 if (valid_dynamic_name (filedata, psym->st_name))
19144 print_symbol (sym_width,
19145 get_dynamic_name (filedata, psym->st_name));
19146 else
19147 printf (_("<corrupt: %14ld>"), psym->st_name);
19148 }
19149 else
19150 printf (_("<symbol index %lu exceeds number of dynamic symbols>"),
19151 (unsigned long) i);
19152
19153 printf ("\n");
19154 if (ent == (uint64_t) -1)
19155 break;
19156 }
19157 printf ("\n");
19158 }
19159
19160 got_print_fail:
19161 free (data);
19162 }
19163
19164 if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
19165 {
19166 uint64_t ent, end;
19167 size_t offset, rel_offset;
19168 unsigned long count, i;
19169 unsigned char * data;
19170 int addr_size, sym_width;
19171 Elf_Internal_Rela * rels;
19172
19173 rel_offset = offset_from_vma (filedata, jmprel, pltrelsz);
19174 if (pltrel == DT_RELA)
19175 {
19176 if (!slurp_rela_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
19177 return false;
19178 }
19179 else
19180 {
19181 if (!slurp_rel_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
19182 return false;
19183 }
19184
19185 ent = mips_pltgot;
19186 addr_size = (is_32bit_elf ? 4 : 8);
19187 end = mips_pltgot + (2 + count) * addr_size;
19188
19189 offset = offset_from_vma (filedata, mips_pltgot, end - mips_pltgot);
19190 data = (unsigned char *) get_data (NULL, filedata, offset, end - mips_pltgot,
19191 1, _("Procedure Linkage Table data"));
19192 if (data == NULL)
19193 {
19194 free (rels);
19195 return false;
19196 }
19197
19198 printf ("\nPLT GOT:\n\n");
19199 printf (_(" Reserved entries:\n"));
19200 printf (_(" %*s %*s Purpose\n"),
19201 addr_size * 2, _("Address"), addr_size * 2, _("Initial"));
19202 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
19203 printf (_(" PLT lazy resolver\n"));
19204 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
19205 printf (_(" Module pointer\n"));
19206 printf ("\n");
19207
19208 printf (_(" Entries:\n"));
19209 printf (" %*s %*s %*s %-7s %3s %s\n",
19210 addr_size * 2, _("Address"),
19211 addr_size * 2, _("Initial"),
19212 addr_size * 2, _("Sym.Val."), _("Type"), _("Ndx"), _("Name"));
19213 sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
19214 for (i = 0; i < count; i++)
19215 {
19216 unsigned long idx = get_reloc_symindex (rels[i].r_info);
19217
19218 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
19219 printf (" ");
19220
19221 if (idx >= filedata->num_dynamic_syms)
19222 printf (_("<corrupt symbol index: %lu>"), idx);
19223 else
19224 {
19225 Elf_Internal_Sym * psym = filedata->dynamic_symbols + idx;
19226
19227 print_vma (psym->st_value, LONG_HEX);
19228 printf (" %-7s %3s ",
19229 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
19230 get_symbol_index_type (filedata, psym->st_shndx));
19231 if (valid_dynamic_name (filedata, psym->st_name))
19232 print_symbol (sym_width,
19233 get_dynamic_name (filedata, psym->st_name));
19234 else
19235 printf (_("<corrupt: %14ld>"), psym->st_name);
19236 }
19237 printf ("\n");
19238 }
19239 printf ("\n");
19240
19241 free (data);
19242 free (rels);
19243 }
19244
19245 return res;
19246 }
19247
19248 static bool
19249 process_nds32_specific (Filedata * filedata)
19250 {
19251 Elf_Internal_Shdr *sect = NULL;
19252
19253 sect = find_section (filedata, ".nds32_e_flags");
19254 if (sect != NULL && sect->sh_size >= 4)
19255 {
19256 unsigned char *buf;
19257 unsigned int flag;
19258
19259 printf ("\nNDS32 elf flags section:\n");
19260 buf = get_data (NULL, filedata, sect->sh_offset, 1, 4,
19261 _("NDS32 elf flags section"));
19262
19263 if (buf == NULL)
19264 return false;
19265
19266 flag = byte_get (buf, 4);
19267 free (buf);
19268 switch (flag & 0x3)
19269 {
19270 case 0:
19271 printf ("(VEC_SIZE):\tNo entry.\n");
19272 break;
19273 case 1:
19274 printf ("(VEC_SIZE):\t4 bytes\n");
19275 break;
19276 case 2:
19277 printf ("(VEC_SIZE):\t16 bytes\n");
19278 break;
19279 case 3:
19280 printf ("(VEC_SIZE):\treserved\n");
19281 break;
19282 }
19283 }
19284
19285 return true;
19286 }
19287
19288 static bool
19289 process_gnu_liblist (Filedata * filedata)
19290 {
19291 Elf_Internal_Shdr * section;
19292 Elf_Internal_Shdr * string_sec;
19293 Elf32_External_Lib * elib;
19294 char * strtab;
19295 size_t strtab_size;
19296 size_t cnt;
19297 unsigned long num_liblist;
19298 unsigned i;
19299 bool res = true;
19300
19301 if (! do_arch)
19302 return true;
19303
19304 for (i = 0, section = filedata->section_headers;
19305 i < filedata->file_header.e_shnum;
19306 i++, section++)
19307 {
19308 switch (section->sh_type)
19309 {
19310 case SHT_GNU_LIBLIST:
19311 if (section->sh_link >= filedata->file_header.e_shnum)
19312 break;
19313
19314 elib = (Elf32_External_Lib *)
19315 get_data (NULL, filedata, section->sh_offset, 1, section->sh_size,
19316 _("liblist section data"));
19317
19318 if (elib == NULL)
19319 {
19320 res = false;
19321 break;
19322 }
19323
19324 string_sec = filedata->section_headers + section->sh_link;
19325 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
19326 string_sec->sh_size,
19327 _("liblist string table"));
19328 if (strtab == NULL
19329 || section->sh_entsize != sizeof (Elf32_External_Lib))
19330 {
19331 free (elib);
19332 free (strtab);
19333 res = false;
19334 break;
19335 }
19336 strtab_size = string_sec->sh_size;
19337
19338 num_liblist = section->sh_size / sizeof (Elf32_External_Lib);
19339 printf (ngettext ("\nLibrary list section '%s' contains %lu entries:\n",
19340 "\nLibrary list section '%s' contains %lu entries:\n",
19341 num_liblist),
19342 printable_section_name (filedata, section),
19343 num_liblist);
19344
19345 puts (_(" Library Time Stamp Checksum Version Flags"));
19346
19347 for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
19348 ++cnt)
19349 {
19350 Elf32_Lib liblist;
19351 time_t atime;
19352 char timebuf[128];
19353 struct tm * tmp;
19354
19355 liblist.l_name = BYTE_GET (elib[cnt].l_name);
19356 atime = BYTE_GET (elib[cnt].l_time_stamp);
19357 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
19358 liblist.l_version = BYTE_GET (elib[cnt].l_version);
19359 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
19360
19361 tmp = gmtime (&atime);
19362 snprintf (timebuf, sizeof (timebuf),
19363 "%04u-%02u-%02uT%02u:%02u:%02u",
19364 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
19365 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
19366
19367 printf ("%3lu: ", (unsigned long) cnt);
19368 if (do_wide)
19369 printf ("%-20s", liblist.l_name < strtab_size
19370 ? strtab + liblist.l_name : _("<corrupt>"));
19371 else
19372 printf ("%-20.20s", liblist.l_name < strtab_size
19373 ? strtab + liblist.l_name : _("<corrupt>"));
19374 printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
19375 liblist.l_version, liblist.l_flags);
19376 }
19377
19378 free (elib);
19379 free (strtab);
19380 }
19381 }
19382
19383 return res;
19384 }
19385
19386 static const char *
19387 get_note_type (Filedata * filedata, unsigned e_type)
19388 {
19389 static char buff[64];
19390
19391 if (filedata->file_header.e_type == ET_CORE)
19392 switch (e_type)
19393 {
19394 case NT_AUXV:
19395 return _("NT_AUXV (auxiliary vector)");
19396 case NT_PRSTATUS:
19397 return _("NT_PRSTATUS (prstatus structure)");
19398 case NT_FPREGSET:
19399 return _("NT_FPREGSET (floating point registers)");
19400 case NT_PRPSINFO:
19401 return _("NT_PRPSINFO (prpsinfo structure)");
19402 case NT_TASKSTRUCT:
19403 return _("NT_TASKSTRUCT (task structure)");
19404 case NT_GDB_TDESC:
19405 return _("NT_GDB_TDESC (GDB XML target description)");
19406 case NT_PRXFPREG:
19407 return _("NT_PRXFPREG (user_xfpregs structure)");
19408 case NT_PPC_VMX:
19409 return _("NT_PPC_VMX (ppc Altivec registers)");
19410 case NT_PPC_VSX:
19411 return _("NT_PPC_VSX (ppc VSX registers)");
19412 case NT_PPC_TAR:
19413 return _("NT_PPC_TAR (ppc TAR register)");
19414 case NT_PPC_PPR:
19415 return _("NT_PPC_PPR (ppc PPR register)");
19416 case NT_PPC_DSCR:
19417 return _("NT_PPC_DSCR (ppc DSCR register)");
19418 case NT_PPC_EBB:
19419 return _("NT_PPC_EBB (ppc EBB registers)");
19420 case NT_PPC_PMU:
19421 return _("NT_PPC_PMU (ppc PMU registers)");
19422 case NT_PPC_TM_CGPR:
19423 return _("NT_PPC_TM_CGPR (ppc checkpointed GPR registers)");
19424 case NT_PPC_TM_CFPR:
19425 return _("NT_PPC_TM_CFPR (ppc checkpointed floating point registers)");
19426 case NT_PPC_TM_CVMX:
19427 return _("NT_PPC_TM_CVMX (ppc checkpointed Altivec registers)");
19428 case NT_PPC_TM_CVSX:
19429 return _("NT_PPC_TM_CVSX (ppc checkpointed VSX registers)");
19430 case NT_PPC_TM_SPR:
19431 return _("NT_PPC_TM_SPR (ppc TM special purpose registers)");
19432 case NT_PPC_TM_CTAR:
19433 return _("NT_PPC_TM_CTAR (ppc checkpointed TAR register)");
19434 case NT_PPC_TM_CPPR:
19435 return _("NT_PPC_TM_CPPR (ppc checkpointed PPR register)");
19436 case NT_PPC_TM_CDSCR:
19437 return _("NT_PPC_TM_CDSCR (ppc checkpointed DSCR register)");
19438 case NT_386_TLS:
19439 return _("NT_386_TLS (x86 TLS information)");
19440 case NT_386_IOPERM:
19441 return _("NT_386_IOPERM (x86 I/O permissions)");
19442 case NT_X86_XSTATE:
19443 return _("NT_X86_XSTATE (x86 XSAVE extended state)");
19444 case NT_X86_CET:
19445 return _("NT_X86_CET (x86 CET state)");
19446 case NT_S390_HIGH_GPRS:
19447 return _("NT_S390_HIGH_GPRS (s390 upper register halves)");
19448 case NT_S390_TIMER:
19449 return _("NT_S390_TIMER (s390 timer register)");
19450 case NT_S390_TODCMP:
19451 return _("NT_S390_TODCMP (s390 TOD comparator register)");
19452 case NT_S390_TODPREG:
19453 return _("NT_S390_TODPREG (s390 TOD programmable register)");
19454 case NT_S390_CTRS:
19455 return _("NT_S390_CTRS (s390 control registers)");
19456 case NT_S390_PREFIX:
19457 return _("NT_S390_PREFIX (s390 prefix register)");
19458 case NT_S390_LAST_BREAK:
19459 return _("NT_S390_LAST_BREAK (s390 last breaking event address)");
19460 case NT_S390_SYSTEM_CALL:
19461 return _("NT_S390_SYSTEM_CALL (s390 system call restart data)");
19462 case NT_S390_TDB:
19463 return _("NT_S390_TDB (s390 transaction diagnostic block)");
19464 case NT_S390_VXRS_LOW:
19465 return _("NT_S390_VXRS_LOW (s390 vector registers 0-15 upper half)");
19466 case NT_S390_VXRS_HIGH:
19467 return _("NT_S390_VXRS_HIGH (s390 vector registers 16-31)");
19468 case NT_S390_GS_CB:
19469 return _("NT_S390_GS_CB (s390 guarded-storage registers)");
19470 case NT_S390_GS_BC:
19471 return _("NT_S390_GS_BC (s390 guarded-storage broadcast control)");
19472 case NT_ARM_VFP:
19473 return _("NT_ARM_VFP (arm VFP registers)");
19474 case NT_ARM_TLS:
19475 return _("NT_ARM_TLS (AArch TLS registers)");
19476 case NT_ARM_HW_BREAK:
19477 return _("NT_ARM_HW_BREAK (AArch hardware breakpoint registers)");
19478 case NT_ARM_HW_WATCH:
19479 return _("NT_ARM_HW_WATCH (AArch hardware watchpoint registers)");
19480 case NT_ARM_SYSTEM_CALL:
19481 return _("NT_ARM_SYSTEM_CALL (AArch system call number)");
19482 case NT_ARM_SVE:
19483 return _("NT_ARM_SVE (AArch SVE registers)");
19484 case NT_ARM_PAC_MASK:
19485 return _("NT_ARM_PAC_MASK (AArch pointer authentication code masks)");
19486 case NT_ARM_PACA_KEYS:
19487 return _("NT_ARM_PACA_KEYS (ARM pointer authentication address keys)");
19488 case NT_ARM_PACG_KEYS:
19489 return _("NT_ARM_PACG_KEYS (ARM pointer authentication generic keys)");
19490 case NT_ARM_TAGGED_ADDR_CTRL:
19491 return _("NT_ARM_TAGGED_ADDR_CTRL (AArch tagged address control)");
19492 case NT_ARM_PAC_ENABLED_KEYS:
19493 return _("NT_ARM_PAC_ENABLED_KEYS (AArch64 pointer authentication enabled keys)");
19494 case NT_ARC_V2:
19495 return _("NT_ARC_V2 (ARC HS accumulator/extra registers)");
19496 case NT_RISCV_CSR:
19497 return _("NT_RISCV_CSR (RISC-V control and status registers)");
19498 case NT_PSTATUS:
19499 return _("NT_PSTATUS (pstatus structure)");
19500 case NT_FPREGS:
19501 return _("NT_FPREGS (floating point registers)");
19502 case NT_PSINFO:
19503 return _("NT_PSINFO (psinfo structure)");
19504 case NT_LWPSTATUS:
19505 return _("NT_LWPSTATUS (lwpstatus_t structure)");
19506 case NT_LWPSINFO:
19507 return _("NT_LWPSINFO (lwpsinfo_t structure)");
19508 case NT_WIN32PSTATUS:
19509 return _("NT_WIN32PSTATUS (win32_pstatus structure)");
19510 case NT_SIGINFO:
19511 return _("NT_SIGINFO (siginfo_t data)");
19512 case NT_FILE:
19513 return _("NT_FILE (mapped files)");
19514 default:
19515 break;
19516 }
19517 else
19518 switch (e_type)
19519 {
19520 case NT_VERSION:
19521 return _("NT_VERSION (version)");
19522 case NT_ARCH:
19523 return _("NT_ARCH (architecture)");
19524 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
19525 return _("OPEN");
19526 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
19527 return _("func");
19528 case NT_GO_BUILDID:
19529 return _("GO BUILDID");
19530 case FDO_PACKAGING_METADATA:
19531 return _("FDO_PACKAGING_METADATA");
19532 default:
19533 break;
19534 }
19535
19536 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19537 return buff;
19538 }
19539
19540 static bool
19541 print_core_note (Elf_Internal_Note *pnote)
19542 {
19543 unsigned int addr_size = is_32bit_elf ? 4 : 8;
19544 uint64_t count, page_size;
19545 unsigned char *descdata, *filenames, *descend;
19546
19547 if (pnote->type != NT_FILE)
19548 {
19549 if (do_wide)
19550 printf ("\n");
19551 return true;
19552 }
19553
19554 if (!is_32bit_elf)
19555 {
19556 printf (_(" Cannot decode 64-bit note in 32-bit build\n"));
19557 /* Still "successful". */
19558 return true;
19559 }
19560
19561 if (pnote->descsz < 2 * addr_size)
19562 {
19563 error (_(" Malformed note - too short for header\n"));
19564 return false;
19565 }
19566
19567 descdata = (unsigned char *) pnote->descdata;
19568 descend = descdata + pnote->descsz;
19569
19570 if (descdata[pnote->descsz - 1] != '\0')
19571 {
19572 error (_(" Malformed note - does not end with \\0\n"));
19573 return false;
19574 }
19575
19576 count = byte_get (descdata, addr_size);
19577 descdata += addr_size;
19578
19579 page_size = byte_get (descdata, addr_size);
19580 descdata += addr_size;
19581
19582 if (count > ((uint64_t) -1 - 2 * addr_size) / (3 * addr_size)
19583 || pnote->descsz < 2 * addr_size + count * 3 * addr_size)
19584 {
19585 error (_(" Malformed note - too short for supplied file count\n"));
19586 return false;
19587 }
19588
19589 printf (_(" Page size: "));
19590 print_vma (page_size, DEC);
19591 printf ("\n");
19592
19593 printf (_(" %*s%*s%*s\n"),
19594 (int) (2 + 2 * addr_size), _("Start"),
19595 (int) (4 + 2 * addr_size), _("End"),
19596 (int) (4 + 2 * addr_size), _("Page Offset"));
19597 filenames = descdata + count * 3 * addr_size;
19598 while (count-- > 0)
19599 {
19600 uint64_t start, end, file_ofs;
19601
19602 if (filenames == descend)
19603 {
19604 error (_(" Malformed note - filenames end too early\n"));
19605 return false;
19606 }
19607
19608 start = byte_get (descdata, addr_size);
19609 descdata += addr_size;
19610 end = byte_get (descdata, addr_size);
19611 descdata += addr_size;
19612 file_ofs = byte_get (descdata, addr_size);
19613 descdata += addr_size;
19614
19615 printf (" ");
19616 print_vma (start, FULL_HEX);
19617 printf (" ");
19618 print_vma (end, FULL_HEX);
19619 printf (" ");
19620 print_vma (file_ofs, FULL_HEX);
19621 printf ("\n %s\n", filenames);
19622
19623 filenames += 1 + strlen ((char *) filenames);
19624 }
19625
19626 return true;
19627 }
19628
19629 static const char *
19630 get_gnu_elf_note_type (unsigned e_type)
19631 {
19632 /* NB/ Keep this switch statement in sync with print_gnu_note (). */
19633 switch (e_type)
19634 {
19635 case NT_GNU_ABI_TAG:
19636 return _("NT_GNU_ABI_TAG (ABI version tag)");
19637 case NT_GNU_HWCAP:
19638 return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
19639 case NT_GNU_BUILD_ID:
19640 return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
19641 case NT_GNU_GOLD_VERSION:
19642 return _("NT_GNU_GOLD_VERSION (gold version)");
19643 case NT_GNU_PROPERTY_TYPE_0:
19644 return _("NT_GNU_PROPERTY_TYPE_0");
19645 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
19646 return _("NT_GNU_BUILD_ATTRIBUTE_OPEN");
19647 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
19648 return _("NT_GNU_BUILD_ATTRIBUTE_FUNC");
19649 default:
19650 {
19651 static char buff[64];
19652
19653 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19654 return buff;
19655 }
19656 }
19657 }
19658
19659 static void
19660 decode_x86_compat_isa (unsigned int bitmask)
19661 {
19662 while (bitmask)
19663 {
19664 unsigned int bit = bitmask & (- bitmask);
19665
19666 bitmask &= ~ bit;
19667 switch (bit)
19668 {
19669 case GNU_PROPERTY_X86_COMPAT_ISA_1_486:
19670 printf ("i486");
19671 break;
19672 case GNU_PROPERTY_X86_COMPAT_ISA_1_586:
19673 printf ("586");
19674 break;
19675 case GNU_PROPERTY_X86_COMPAT_ISA_1_686:
19676 printf ("686");
19677 break;
19678 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE:
19679 printf ("SSE");
19680 break;
19681 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE2:
19682 printf ("SSE2");
19683 break;
19684 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE3:
19685 printf ("SSE3");
19686 break;
19687 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSSE3:
19688 printf ("SSSE3");
19689 break;
19690 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_1:
19691 printf ("SSE4_1");
19692 break;
19693 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_2:
19694 printf ("SSE4_2");
19695 break;
19696 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX:
19697 printf ("AVX");
19698 break;
19699 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX2:
19700 printf ("AVX2");
19701 break;
19702 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512F:
19703 printf ("AVX512F");
19704 break;
19705 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512CD:
19706 printf ("AVX512CD");
19707 break;
19708 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512ER:
19709 printf ("AVX512ER");
19710 break;
19711 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512PF:
19712 printf ("AVX512PF");
19713 break;
19714 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512VL:
19715 printf ("AVX512VL");
19716 break;
19717 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512DQ:
19718 printf ("AVX512DQ");
19719 break;
19720 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512BW:
19721 printf ("AVX512BW");
19722 break;
19723 default:
19724 printf (_("<unknown: %x>"), bit);
19725 break;
19726 }
19727 if (bitmask)
19728 printf (", ");
19729 }
19730 }
19731
19732 static void
19733 decode_x86_compat_2_isa (unsigned int bitmask)
19734 {
19735 if (!bitmask)
19736 {
19737 printf (_("<None>"));
19738 return;
19739 }
19740
19741 while (bitmask)
19742 {
19743 unsigned int bit = bitmask & (- bitmask);
19744
19745 bitmask &= ~ bit;
19746 switch (bit)
19747 {
19748 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_CMOV:
19749 printf ("CMOV");
19750 break;
19751 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE:
19752 printf ("SSE");
19753 break;
19754 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE2:
19755 printf ("SSE2");
19756 break;
19757 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE3:
19758 printf ("SSE3");
19759 break;
19760 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSSE3:
19761 printf ("SSSE3");
19762 break;
19763 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_1:
19764 printf ("SSE4_1");
19765 break;
19766 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_2:
19767 printf ("SSE4_2");
19768 break;
19769 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX:
19770 printf ("AVX");
19771 break;
19772 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX2:
19773 printf ("AVX2");
19774 break;
19775 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_FMA:
19776 printf ("FMA");
19777 break;
19778 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512F:
19779 printf ("AVX512F");
19780 break;
19781 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512CD:
19782 printf ("AVX512CD");
19783 break;
19784 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512ER:
19785 printf ("AVX512ER");
19786 break;
19787 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512PF:
19788 printf ("AVX512PF");
19789 break;
19790 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512VL:
19791 printf ("AVX512VL");
19792 break;
19793 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512DQ:
19794 printf ("AVX512DQ");
19795 break;
19796 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512BW:
19797 printf ("AVX512BW");
19798 break;
19799 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4FMAPS:
19800 printf ("AVX512_4FMAPS");
19801 break;
19802 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4VNNIW:
19803 printf ("AVX512_4VNNIW");
19804 break;
19805 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BITALG:
19806 printf ("AVX512_BITALG");
19807 break;
19808 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_IFMA:
19809 printf ("AVX512_IFMA");
19810 break;
19811 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI:
19812 printf ("AVX512_VBMI");
19813 break;
19814 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI2:
19815 printf ("AVX512_VBMI2");
19816 break;
19817 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VNNI:
19818 printf ("AVX512_VNNI");
19819 break;
19820 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BF16:
19821 printf ("AVX512_BF16");
19822 break;
19823 default:
19824 printf (_("<unknown: %x>"), bit);
19825 break;
19826 }
19827 if (bitmask)
19828 printf (", ");
19829 }
19830 }
19831
19832 static const char *
19833 get_amdgpu_elf_note_type (unsigned int e_type)
19834 {
19835 switch (e_type)
19836 {
19837 case NT_AMDGPU_METADATA:
19838 return _("NT_AMDGPU_METADATA (code object metadata)");
19839 default:
19840 {
19841 static char buf[64];
19842 snprintf (buf, sizeof (buf), _("Unknown note type: (0x%08x)"), e_type);
19843 return buf;
19844 }
19845 }
19846 }
19847
19848 static void
19849 decode_x86_isa (unsigned int bitmask)
19850 {
19851 while (bitmask)
19852 {
19853 unsigned int bit = bitmask & (- bitmask);
19854
19855 bitmask &= ~ bit;
19856 switch (bit)
19857 {
19858 case GNU_PROPERTY_X86_ISA_1_BASELINE:
19859 printf ("x86-64-baseline");
19860 break;
19861 case GNU_PROPERTY_X86_ISA_1_V2:
19862 printf ("x86-64-v2");
19863 break;
19864 case GNU_PROPERTY_X86_ISA_1_V3:
19865 printf ("x86-64-v3");
19866 break;
19867 case GNU_PROPERTY_X86_ISA_1_V4:
19868 printf ("x86-64-v4");
19869 break;
19870 default:
19871 printf (_("<unknown: %x>"), bit);
19872 break;
19873 }
19874 if (bitmask)
19875 printf (", ");
19876 }
19877 }
19878
19879 static void
19880 decode_x86_feature_1 (unsigned int bitmask)
19881 {
19882 if (!bitmask)
19883 {
19884 printf (_("<None>"));
19885 return;
19886 }
19887
19888 while (bitmask)
19889 {
19890 unsigned int bit = bitmask & (- bitmask);
19891
19892 bitmask &= ~ bit;
19893 switch (bit)
19894 {
19895 case GNU_PROPERTY_X86_FEATURE_1_IBT:
19896 printf ("IBT");
19897 break;
19898 case GNU_PROPERTY_X86_FEATURE_1_SHSTK:
19899 printf ("SHSTK");
19900 break;
19901 case GNU_PROPERTY_X86_FEATURE_1_LAM_U48:
19902 printf ("LAM_U48");
19903 break;
19904 case GNU_PROPERTY_X86_FEATURE_1_LAM_U57:
19905 printf ("LAM_U57");
19906 break;
19907 default:
19908 printf (_("<unknown: %x>"), bit);
19909 break;
19910 }
19911 if (bitmask)
19912 printf (", ");
19913 }
19914 }
19915
19916 static void
19917 decode_x86_feature_2 (unsigned int bitmask)
19918 {
19919 if (!bitmask)
19920 {
19921 printf (_("<None>"));
19922 return;
19923 }
19924
19925 while (bitmask)
19926 {
19927 unsigned int bit = bitmask & (- bitmask);
19928
19929 bitmask &= ~ bit;
19930 switch (bit)
19931 {
19932 case GNU_PROPERTY_X86_FEATURE_2_X86:
19933 printf ("x86");
19934 break;
19935 case GNU_PROPERTY_X86_FEATURE_2_X87:
19936 printf ("x87");
19937 break;
19938 case GNU_PROPERTY_X86_FEATURE_2_MMX:
19939 printf ("MMX");
19940 break;
19941 case GNU_PROPERTY_X86_FEATURE_2_XMM:
19942 printf ("XMM");
19943 break;
19944 case GNU_PROPERTY_X86_FEATURE_2_YMM:
19945 printf ("YMM");
19946 break;
19947 case GNU_PROPERTY_X86_FEATURE_2_ZMM:
19948 printf ("ZMM");
19949 break;
19950 case GNU_PROPERTY_X86_FEATURE_2_TMM:
19951 printf ("TMM");
19952 break;
19953 case GNU_PROPERTY_X86_FEATURE_2_MASK:
19954 printf ("MASK");
19955 break;
19956 case GNU_PROPERTY_X86_FEATURE_2_FXSR:
19957 printf ("FXSR");
19958 break;
19959 case GNU_PROPERTY_X86_FEATURE_2_XSAVE:
19960 printf ("XSAVE");
19961 break;
19962 case GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT:
19963 printf ("XSAVEOPT");
19964 break;
19965 case GNU_PROPERTY_X86_FEATURE_2_XSAVEC:
19966 printf ("XSAVEC");
19967 break;
19968 default:
19969 printf (_("<unknown: %x>"), bit);
19970 break;
19971 }
19972 if (bitmask)
19973 printf (", ");
19974 }
19975 }
19976
19977 static void
19978 decode_aarch64_feature_1_and (unsigned int bitmask)
19979 {
19980 while (bitmask)
19981 {
19982 unsigned int bit = bitmask & (- bitmask);
19983
19984 bitmask &= ~ bit;
19985 switch (bit)
19986 {
19987 case GNU_PROPERTY_AARCH64_FEATURE_1_BTI:
19988 printf ("BTI");
19989 break;
19990
19991 case GNU_PROPERTY_AARCH64_FEATURE_1_PAC:
19992 printf ("PAC");
19993 break;
19994
19995 default:
19996 printf (_("<unknown: %x>"), bit);
19997 break;
19998 }
19999 if (bitmask)
20000 printf (", ");
20001 }
20002 }
20003
20004 static void
20005 decode_1_needed (unsigned int bitmask)
20006 {
20007 while (bitmask)
20008 {
20009 unsigned int bit = bitmask & (- bitmask);
20010
20011 bitmask &= ~ bit;
20012 switch (bit)
20013 {
20014 case GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS:
20015 printf ("indirect external access");
20016 break;
20017 default:
20018 printf (_("<unknown: %x>"), bit);
20019 break;
20020 }
20021 if (bitmask)
20022 printf (", ");
20023 }
20024 }
20025
20026 static void
20027 print_gnu_property_note (Filedata * filedata, Elf_Internal_Note * pnote)
20028 {
20029 unsigned char * ptr = (unsigned char *) pnote->descdata;
20030 unsigned char * ptr_end = ptr + pnote->descsz;
20031 unsigned int size = is_32bit_elf ? 4 : 8;
20032
20033 printf (_(" Properties: "));
20034
20035 if (pnote->descsz < 8 || (pnote->descsz % size) != 0)
20036 {
20037 printf (_("<corrupt GNU_PROPERTY_TYPE, size = %#lx>\n"), pnote->descsz);
20038 return;
20039 }
20040
20041 while (ptr < ptr_end)
20042 {
20043 unsigned int j;
20044 unsigned int type;
20045 unsigned int datasz;
20046
20047 if ((size_t) (ptr_end - ptr) < 8)
20048 {
20049 printf (_("<corrupt descsz: %#lx>\n"), pnote->descsz);
20050 break;
20051 }
20052
20053 type = byte_get (ptr, 4);
20054 datasz = byte_get (ptr + 4, 4);
20055
20056 ptr += 8;
20057
20058 if (datasz > (size_t) (ptr_end - ptr))
20059 {
20060 printf (_("<corrupt type (%#x) datasz: %#x>\n"),
20061 type, datasz);
20062 break;
20063 }
20064
20065 if (type >= GNU_PROPERTY_LOPROC && type <= GNU_PROPERTY_HIPROC)
20066 {
20067 if (filedata->file_header.e_machine == EM_X86_64
20068 || filedata->file_header.e_machine == EM_IAMCU
20069 || filedata->file_header.e_machine == EM_386)
20070 {
20071 unsigned int bitmask;
20072
20073 if (datasz == 4)
20074 bitmask = byte_get (ptr, 4);
20075 else
20076 bitmask = 0;
20077
20078 switch (type)
20079 {
20080 case GNU_PROPERTY_X86_ISA_1_USED:
20081 if (datasz != 4)
20082 printf (_("x86 ISA used: <corrupt length: %#x> "),
20083 datasz);
20084 else
20085 {
20086 printf ("x86 ISA used: ");
20087 decode_x86_isa (bitmask);
20088 }
20089 goto next;
20090
20091 case GNU_PROPERTY_X86_ISA_1_NEEDED:
20092 if (datasz != 4)
20093 printf (_("x86 ISA needed: <corrupt length: %#x> "),
20094 datasz);
20095 else
20096 {
20097 printf ("x86 ISA needed: ");
20098 decode_x86_isa (bitmask);
20099 }
20100 goto next;
20101
20102 case GNU_PROPERTY_X86_FEATURE_1_AND:
20103 if (datasz != 4)
20104 printf (_("x86 feature: <corrupt length: %#x> "),
20105 datasz);
20106 else
20107 {
20108 printf ("x86 feature: ");
20109 decode_x86_feature_1 (bitmask);
20110 }
20111 goto next;
20112
20113 case GNU_PROPERTY_X86_FEATURE_2_USED:
20114 if (datasz != 4)
20115 printf (_("x86 feature used: <corrupt length: %#x> "),
20116 datasz);
20117 else
20118 {
20119 printf ("x86 feature used: ");
20120 decode_x86_feature_2 (bitmask);
20121 }
20122 goto next;
20123
20124 case GNU_PROPERTY_X86_FEATURE_2_NEEDED:
20125 if (datasz != 4)
20126 printf (_("x86 feature needed: <corrupt length: %#x> "), datasz);
20127 else
20128 {
20129 printf ("x86 feature needed: ");
20130 decode_x86_feature_2 (bitmask);
20131 }
20132 goto next;
20133
20134 case GNU_PROPERTY_X86_COMPAT_ISA_1_USED:
20135 if (datasz != 4)
20136 printf (_("x86 ISA used: <corrupt length: %#x> "),
20137 datasz);
20138 else
20139 {
20140 printf ("x86 ISA used: ");
20141 decode_x86_compat_isa (bitmask);
20142 }
20143 goto next;
20144
20145 case GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED:
20146 if (datasz != 4)
20147 printf (_("x86 ISA needed: <corrupt length: %#x> "),
20148 datasz);
20149 else
20150 {
20151 printf ("x86 ISA needed: ");
20152 decode_x86_compat_isa (bitmask);
20153 }
20154 goto next;
20155
20156 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_USED:
20157 if (datasz != 4)
20158 printf (_("x86 ISA used: <corrupt length: %#x> "),
20159 datasz);
20160 else
20161 {
20162 printf ("x86 ISA used: ");
20163 decode_x86_compat_2_isa (bitmask);
20164 }
20165 goto next;
20166
20167 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_NEEDED:
20168 if (datasz != 4)
20169 printf (_("x86 ISA needed: <corrupt length: %#x> "),
20170 datasz);
20171 else
20172 {
20173 printf ("x86 ISA needed: ");
20174 decode_x86_compat_2_isa (bitmask);
20175 }
20176 goto next;
20177
20178 default:
20179 break;
20180 }
20181 }
20182 else if (filedata->file_header.e_machine == EM_AARCH64)
20183 {
20184 if (type == GNU_PROPERTY_AARCH64_FEATURE_1_AND)
20185 {
20186 printf ("AArch64 feature: ");
20187 if (datasz != 4)
20188 printf (_("<corrupt length: %#x> "), datasz);
20189 else
20190 decode_aarch64_feature_1_and (byte_get (ptr, 4));
20191 goto next;
20192 }
20193 }
20194 }
20195 else
20196 {
20197 switch (type)
20198 {
20199 case GNU_PROPERTY_STACK_SIZE:
20200 printf (_("stack size: "));
20201 if (datasz != size)
20202 printf (_("<corrupt length: %#x> "), datasz);
20203 else
20204 printf ("%#lx", (unsigned long) byte_get (ptr, size));
20205 goto next;
20206
20207 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
20208 printf ("no copy on protected ");
20209 if (datasz)
20210 printf (_("<corrupt length: %#x> "), datasz);
20211 goto next;
20212
20213 default:
20214 if ((type >= GNU_PROPERTY_UINT32_AND_LO
20215 && type <= GNU_PROPERTY_UINT32_AND_HI)
20216 || (type >= GNU_PROPERTY_UINT32_OR_LO
20217 && type <= GNU_PROPERTY_UINT32_OR_HI))
20218 {
20219 switch (type)
20220 {
20221 case GNU_PROPERTY_1_NEEDED:
20222 if (datasz != 4)
20223 printf (_("1_needed: <corrupt length: %#x> "),
20224 datasz);
20225 else
20226 {
20227 unsigned int bitmask = byte_get (ptr, 4);
20228 printf ("1_needed: ");
20229 decode_1_needed (bitmask);
20230 }
20231 goto next;
20232
20233 default:
20234 break;
20235 }
20236 if (type <= GNU_PROPERTY_UINT32_AND_HI)
20237 printf (_("UINT32_AND (%#x): "), type);
20238 else
20239 printf (_("UINT32_OR (%#x): "), type);
20240 if (datasz != 4)
20241 printf (_("<corrupt length: %#x> "), datasz);
20242 else
20243 printf ("%#x", (unsigned int) byte_get (ptr, 4));
20244 goto next;
20245 }
20246 break;
20247 }
20248 }
20249
20250 if (type < GNU_PROPERTY_LOPROC)
20251 printf (_("<unknown type %#x data: "), type);
20252 else if (type < GNU_PROPERTY_LOUSER)
20253 printf (_("<processor-specific type %#x data: "), type);
20254 else
20255 printf (_("<application-specific type %#x data: "), type);
20256 for (j = 0; j < datasz; ++j)
20257 printf ("%02x ", ptr[j] & 0xff);
20258 printf (">");
20259
20260 next:
20261 ptr += ((datasz + (size - 1)) & ~ (size - 1));
20262 if (ptr == ptr_end)
20263 break;
20264
20265 if (do_wide)
20266 printf (", ");
20267 else
20268 printf ("\n\t");
20269 }
20270
20271 printf ("\n");
20272 }
20273
20274 static bool
20275 print_gnu_note (Filedata * filedata, Elf_Internal_Note *pnote)
20276 {
20277 /* NB/ Keep this switch statement in sync with get_gnu_elf_note_type (). */
20278 switch (pnote->type)
20279 {
20280 case NT_GNU_BUILD_ID:
20281 {
20282 unsigned long i;
20283
20284 printf (_(" Build ID: "));
20285 for (i = 0; i < pnote->descsz; ++i)
20286 printf ("%02x", pnote->descdata[i] & 0xff);
20287 printf ("\n");
20288 }
20289 break;
20290
20291 case NT_GNU_ABI_TAG:
20292 {
20293 unsigned long os, major, minor, subminor;
20294 const char *osname;
20295
20296 /* PR 17531: file: 030-599401-0.004. */
20297 if (pnote->descsz < 16)
20298 {
20299 printf (_(" <corrupt GNU_ABI_TAG>\n"));
20300 break;
20301 }
20302
20303 os = byte_get ((unsigned char *) pnote->descdata, 4);
20304 major = byte_get ((unsigned char *) pnote->descdata + 4, 4);
20305 minor = byte_get ((unsigned char *) pnote->descdata + 8, 4);
20306 subminor = byte_get ((unsigned char *) pnote->descdata + 12, 4);
20307
20308 switch (os)
20309 {
20310 case GNU_ABI_TAG_LINUX:
20311 osname = "Linux";
20312 break;
20313 case GNU_ABI_TAG_HURD:
20314 osname = "Hurd";
20315 break;
20316 case GNU_ABI_TAG_SOLARIS:
20317 osname = "Solaris";
20318 break;
20319 case GNU_ABI_TAG_FREEBSD:
20320 osname = "FreeBSD";
20321 break;
20322 case GNU_ABI_TAG_NETBSD:
20323 osname = "NetBSD";
20324 break;
20325 case GNU_ABI_TAG_SYLLABLE:
20326 osname = "Syllable";
20327 break;
20328 case GNU_ABI_TAG_NACL:
20329 osname = "NaCl";
20330 break;
20331 default:
20332 osname = "Unknown";
20333 break;
20334 }
20335
20336 printf (_(" OS: %s, ABI: %ld.%ld.%ld\n"), osname,
20337 major, minor, subminor);
20338 }
20339 break;
20340
20341 case NT_GNU_GOLD_VERSION:
20342 {
20343 unsigned long i;
20344
20345 printf (_(" Version: "));
20346 for (i = 0; i < pnote->descsz && pnote->descdata[i] != '\0'; ++i)
20347 printf ("%c", pnote->descdata[i]);
20348 printf ("\n");
20349 }
20350 break;
20351
20352 case NT_GNU_HWCAP:
20353 {
20354 unsigned long num_entries, mask;
20355
20356 /* Hardware capabilities information. Word 0 is the number of entries.
20357 Word 1 is a bitmask of enabled entries. The rest of the descriptor
20358 is a series of entries, where each entry is a single byte followed
20359 by a nul terminated string. The byte gives the bit number to test
20360 if enabled in the bitmask. */
20361 printf (_(" Hardware Capabilities: "));
20362 if (pnote->descsz < 8)
20363 {
20364 error (_("<corrupt GNU_HWCAP>\n"));
20365 return false;
20366 }
20367 num_entries = byte_get ((unsigned char *) pnote->descdata, 4);
20368 mask = byte_get ((unsigned char *) pnote->descdata + 4, 4);
20369 printf (_("num entries: %ld, enabled mask: %lx\n"), num_entries, mask);
20370 /* FIXME: Add code to display the entries... */
20371 }
20372 break;
20373
20374 case NT_GNU_PROPERTY_TYPE_0:
20375 print_gnu_property_note (filedata, pnote);
20376 break;
20377
20378 default:
20379 /* Handle unrecognised types. An error message should have already been
20380 created by get_gnu_elf_note_type(), so all that we need to do is to
20381 display the data. */
20382 {
20383 unsigned long i;
20384
20385 printf (_(" Description data: "));
20386 for (i = 0; i < pnote->descsz; ++i)
20387 printf ("%02x ", pnote->descdata[i] & 0xff);
20388 printf ("\n");
20389 }
20390 break;
20391 }
20392
20393 return true;
20394 }
20395
20396 static const char *
20397 get_v850_elf_note_type (enum v850_notes n_type)
20398 {
20399 static char buff[64];
20400
20401 switch (n_type)
20402 {
20403 case V850_NOTE_ALIGNMENT: return _("Alignment of 8-byte objects");
20404 case V850_NOTE_DATA_SIZE: return _("Sizeof double and long double");
20405 case V850_NOTE_FPU_INFO: return _("Type of FPU support needed");
20406 case V850_NOTE_SIMD_INFO: return _("Use of SIMD instructions");
20407 case V850_NOTE_CACHE_INFO: return _("Use of cache");
20408 case V850_NOTE_MMU_INFO: return _("Use of MMU");
20409 default:
20410 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), n_type);
20411 return buff;
20412 }
20413 }
20414
20415 static bool
20416 print_v850_note (Elf_Internal_Note * pnote)
20417 {
20418 unsigned int val;
20419
20420 if (pnote->descsz != 4)
20421 return false;
20422
20423 val = byte_get ((unsigned char *) pnote->descdata, pnote->descsz);
20424
20425 if (val == 0)
20426 {
20427 printf (_("not set\n"));
20428 return true;
20429 }
20430
20431 switch (pnote->type)
20432 {
20433 case V850_NOTE_ALIGNMENT:
20434 switch (val)
20435 {
20436 case EF_RH850_DATA_ALIGN4: printf (_("4-byte\n")); return true;
20437 case EF_RH850_DATA_ALIGN8: printf (_("8-byte\n")); return true;
20438 }
20439 break;
20440
20441 case V850_NOTE_DATA_SIZE:
20442 switch (val)
20443 {
20444 case EF_RH850_DOUBLE32: printf (_("4-bytes\n")); return true;
20445 case EF_RH850_DOUBLE64: printf (_("8-bytes\n")); return true;
20446 }
20447 break;
20448
20449 case V850_NOTE_FPU_INFO:
20450 switch (val)
20451 {
20452 case EF_RH850_FPU20: printf (_("FPU-2.0\n")); return true;
20453 case EF_RH850_FPU30: printf (_("FPU-3.0\n")); return true;
20454 }
20455 break;
20456
20457 case V850_NOTE_MMU_INFO:
20458 case V850_NOTE_CACHE_INFO:
20459 case V850_NOTE_SIMD_INFO:
20460 if (val == EF_RH850_SIMD)
20461 {
20462 printf (_("yes\n"));
20463 return true;
20464 }
20465 break;
20466
20467 default:
20468 /* An 'unknown note type' message will already have been displayed. */
20469 break;
20470 }
20471
20472 printf (_("unknown value: %x\n"), val);
20473 return false;
20474 }
20475
20476 static bool
20477 process_netbsd_elf_note (Elf_Internal_Note * pnote)
20478 {
20479 unsigned int version;
20480
20481 switch (pnote->type)
20482 {
20483 case NT_NETBSD_IDENT:
20484 if (pnote->descsz < 1)
20485 break;
20486 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
20487 if ((version / 10000) % 100)
20488 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u%s%c)\n", pnote->descsz,
20489 version, version / 100000000, (version / 1000000) % 100,
20490 (version / 10000) % 100 > 26 ? "Z" : "",
20491 'A' + (version / 10000) % 26);
20492 else
20493 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u.%u)\n", pnote->descsz,
20494 version, version / 100000000, (version / 1000000) % 100,
20495 (version / 100) % 100);
20496 return true;
20497
20498 case NT_NETBSD_MARCH:
20499 printf (" NetBSD\t\t0x%08lx\tMARCH <%s>\n", pnote->descsz,
20500 pnote->descdata);
20501 return true;
20502
20503 case NT_NETBSD_PAX:
20504 if (pnote->descsz < 1)
20505 break;
20506 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
20507 printf (" NetBSD\t\t0x%08lx\tPaX <%s%s%s%s%s%s>\n", pnote->descsz,
20508 ((version & NT_NETBSD_PAX_MPROTECT) ? "+mprotect" : ""),
20509 ((version & NT_NETBSD_PAX_NOMPROTECT) ? "-mprotect" : ""),
20510 ((version & NT_NETBSD_PAX_GUARD) ? "+guard" : ""),
20511 ((version & NT_NETBSD_PAX_NOGUARD) ? "-guard" : ""),
20512 ((version & NT_NETBSD_PAX_ASLR) ? "+ASLR" : ""),
20513 ((version & NT_NETBSD_PAX_NOASLR) ? "-ASLR" : ""));
20514 return true;
20515 }
20516
20517 printf (" NetBSD\t0x%08lx\tUnknown note type: (0x%08lx)\n",
20518 pnote->descsz, pnote->type);
20519 return false;
20520 }
20521
20522 static const char *
20523 get_freebsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
20524 {
20525 switch (e_type)
20526 {
20527 case NT_FREEBSD_THRMISC:
20528 return _("NT_THRMISC (thrmisc structure)");
20529 case NT_FREEBSD_PROCSTAT_PROC:
20530 return _("NT_PROCSTAT_PROC (proc data)");
20531 case NT_FREEBSD_PROCSTAT_FILES:
20532 return _("NT_PROCSTAT_FILES (files data)");
20533 case NT_FREEBSD_PROCSTAT_VMMAP:
20534 return _("NT_PROCSTAT_VMMAP (vmmap data)");
20535 case NT_FREEBSD_PROCSTAT_GROUPS:
20536 return _("NT_PROCSTAT_GROUPS (groups data)");
20537 case NT_FREEBSD_PROCSTAT_UMASK:
20538 return _("NT_PROCSTAT_UMASK (umask data)");
20539 case NT_FREEBSD_PROCSTAT_RLIMIT:
20540 return _("NT_PROCSTAT_RLIMIT (rlimit data)");
20541 case NT_FREEBSD_PROCSTAT_OSREL:
20542 return _("NT_PROCSTAT_OSREL (osreldate data)");
20543 case NT_FREEBSD_PROCSTAT_PSSTRINGS:
20544 return _("NT_PROCSTAT_PSSTRINGS (ps_strings data)");
20545 case NT_FREEBSD_PROCSTAT_AUXV:
20546 return _("NT_PROCSTAT_AUXV (auxv data)");
20547 case NT_FREEBSD_PTLWPINFO:
20548 return _("NT_PTLWPINFO (ptrace_lwpinfo structure)");
20549 case NT_FREEBSD_X86_SEGBASES:
20550 return _("NT_X86_SEGBASES (x86 segment base registers)");
20551 }
20552 return get_note_type (filedata, e_type);
20553 }
20554
20555 static const char *
20556 get_netbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
20557 {
20558 static char buff[64];
20559
20560 switch (e_type)
20561 {
20562 case NT_NETBSDCORE_PROCINFO:
20563 /* NetBSD core "procinfo" structure. */
20564 return _("NetBSD procinfo structure");
20565
20566 case NT_NETBSDCORE_AUXV:
20567 return _("NetBSD ELF auxiliary vector data");
20568
20569 case NT_NETBSDCORE_LWPSTATUS:
20570 return _("PT_LWPSTATUS (ptrace_lwpstatus structure)");
20571
20572 default:
20573 /* As of Jan 2020 there are no other machine-independent notes
20574 defined for NetBSD core files. If the note type is less
20575 than the start of the machine-dependent note types, we don't
20576 understand it. */
20577
20578 if (e_type < NT_NETBSDCORE_FIRSTMACH)
20579 {
20580 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20581 return buff;
20582 }
20583 break;
20584 }
20585
20586 switch (filedata->file_header.e_machine)
20587 {
20588 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
20589 and PT_GETFPREGS == mach+2. */
20590
20591 case EM_OLD_ALPHA:
20592 case EM_ALPHA:
20593 case EM_SPARC:
20594 case EM_SPARC32PLUS:
20595 case EM_SPARCV9:
20596 switch (e_type)
20597 {
20598 case NT_NETBSDCORE_FIRSTMACH + 0:
20599 return _("PT_GETREGS (reg structure)");
20600 case NT_NETBSDCORE_FIRSTMACH + 2:
20601 return _("PT_GETFPREGS (fpreg structure)");
20602 default:
20603 break;
20604 }
20605 break;
20606
20607 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
20608 There's also old PT___GETREGS40 == mach + 1 for old reg
20609 structure which lacks GBR. */
20610 case EM_SH:
20611 switch (e_type)
20612 {
20613 case NT_NETBSDCORE_FIRSTMACH + 1:
20614 return _("PT___GETREGS40 (old reg structure)");
20615 case NT_NETBSDCORE_FIRSTMACH + 3:
20616 return _("PT_GETREGS (reg structure)");
20617 case NT_NETBSDCORE_FIRSTMACH + 5:
20618 return _("PT_GETFPREGS (fpreg structure)");
20619 default:
20620 break;
20621 }
20622 break;
20623
20624 /* On all other arch's, PT_GETREGS == mach+1 and
20625 PT_GETFPREGS == mach+3. */
20626 default:
20627 switch (e_type)
20628 {
20629 case NT_NETBSDCORE_FIRSTMACH + 1:
20630 return _("PT_GETREGS (reg structure)");
20631 case NT_NETBSDCORE_FIRSTMACH + 3:
20632 return _("PT_GETFPREGS (fpreg structure)");
20633 default:
20634 break;
20635 }
20636 }
20637
20638 snprintf (buff, sizeof (buff), "PT_FIRSTMACH+%d",
20639 e_type - NT_NETBSDCORE_FIRSTMACH);
20640 return buff;
20641 }
20642
20643 static const char *
20644 get_openbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
20645 {
20646 switch (e_type)
20647 {
20648 case NT_OPENBSD_PROCINFO:
20649 return _("OpenBSD procinfo structure");
20650 case NT_OPENBSD_AUXV:
20651 return _("OpenBSD ELF auxiliary vector data");
20652 case NT_OPENBSD_REGS:
20653 return _("OpenBSD regular registers");
20654 case NT_OPENBSD_FPREGS:
20655 return _("OpenBSD floating point registers");
20656 case NT_OPENBSD_WCOOKIE:
20657 return _("OpenBSD window cookie");
20658 }
20659
20660 return get_note_type (filedata, e_type);
20661 }
20662
20663 static const char *
20664 get_stapsdt_note_type (unsigned e_type)
20665 {
20666 static char buff[64];
20667
20668 switch (e_type)
20669 {
20670 case NT_STAPSDT:
20671 return _("NT_STAPSDT (SystemTap probe descriptors)");
20672
20673 default:
20674 break;
20675 }
20676
20677 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20678 return buff;
20679 }
20680
20681 static bool
20682 print_stapsdt_note (Elf_Internal_Note *pnote)
20683 {
20684 size_t len, maxlen;
20685 unsigned long addr_size = is_32bit_elf ? 4 : 8;
20686 char *data = pnote->descdata;
20687 char *data_end = pnote->descdata + pnote->descsz;
20688 uint64_t pc, base_addr, semaphore;
20689 char *provider, *probe, *arg_fmt;
20690
20691 if (pnote->descsz < (addr_size * 3))
20692 goto stapdt_note_too_small;
20693
20694 pc = byte_get ((unsigned char *) data, addr_size);
20695 data += addr_size;
20696
20697 base_addr = byte_get ((unsigned char *) data, addr_size);
20698 data += addr_size;
20699
20700 semaphore = byte_get ((unsigned char *) data, addr_size);
20701 data += addr_size;
20702
20703 if (data >= data_end)
20704 goto stapdt_note_too_small;
20705 maxlen = data_end - data;
20706 len = strnlen (data, maxlen);
20707 if (len < maxlen)
20708 {
20709 provider = data;
20710 data += len + 1;
20711 }
20712 else
20713 goto stapdt_note_too_small;
20714
20715 if (data >= data_end)
20716 goto stapdt_note_too_small;
20717 maxlen = data_end - data;
20718 len = strnlen (data, maxlen);
20719 if (len < maxlen)
20720 {
20721 probe = data;
20722 data += len + 1;
20723 }
20724 else
20725 goto stapdt_note_too_small;
20726
20727 if (data >= data_end)
20728 goto stapdt_note_too_small;
20729 maxlen = data_end - data;
20730 len = strnlen (data, maxlen);
20731 if (len < maxlen)
20732 {
20733 arg_fmt = data;
20734 data += len + 1;
20735 }
20736 else
20737 goto stapdt_note_too_small;
20738
20739 printf (_(" Provider: %s\n"), provider);
20740 printf (_(" Name: %s\n"), probe);
20741 printf (_(" Location: "));
20742 print_vma (pc, FULL_HEX);
20743 printf (_(", Base: "));
20744 print_vma (base_addr, FULL_HEX);
20745 printf (_(", Semaphore: "));
20746 print_vma (semaphore, FULL_HEX);
20747 printf ("\n");
20748 printf (_(" Arguments: %s\n"), arg_fmt);
20749
20750 return data == data_end;
20751
20752 stapdt_note_too_small:
20753 printf (_(" <corrupt - note is too small>\n"));
20754 error (_("corrupt stapdt note - the data size is too small\n"));
20755 return false;
20756 }
20757
20758 static bool
20759 print_fdo_note (Elf_Internal_Note * pnote)
20760 {
20761 if (pnote->descsz > 0 && pnote->type == FDO_PACKAGING_METADATA)
20762 {
20763 printf (_(" Packaging Metadata: %.*s\n"), (int) pnote->descsz, pnote->descdata);
20764 return true;
20765 }
20766 return false;
20767 }
20768
20769 static const char *
20770 get_ia64_vms_note_type (unsigned e_type)
20771 {
20772 static char buff[64];
20773
20774 switch (e_type)
20775 {
20776 case NT_VMS_MHD:
20777 return _("NT_VMS_MHD (module header)");
20778 case NT_VMS_LNM:
20779 return _("NT_VMS_LNM (language name)");
20780 case NT_VMS_SRC:
20781 return _("NT_VMS_SRC (source files)");
20782 case NT_VMS_TITLE:
20783 return "NT_VMS_TITLE";
20784 case NT_VMS_EIDC:
20785 return _("NT_VMS_EIDC (consistency check)");
20786 case NT_VMS_FPMODE:
20787 return _("NT_VMS_FPMODE (FP mode)");
20788 case NT_VMS_LINKTIME:
20789 return "NT_VMS_LINKTIME";
20790 case NT_VMS_IMGNAM:
20791 return _("NT_VMS_IMGNAM (image name)");
20792 case NT_VMS_IMGID:
20793 return _("NT_VMS_IMGID (image id)");
20794 case NT_VMS_LINKID:
20795 return _("NT_VMS_LINKID (link id)");
20796 case NT_VMS_IMGBID:
20797 return _("NT_VMS_IMGBID (build id)");
20798 case NT_VMS_GSTNAM:
20799 return _("NT_VMS_GSTNAM (sym table name)");
20800 case NT_VMS_ORIG_DYN:
20801 return "NT_VMS_ORIG_DYN";
20802 case NT_VMS_PATCHTIME:
20803 return "NT_VMS_PATCHTIME";
20804 default:
20805 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20806 return buff;
20807 }
20808 }
20809
20810 static bool
20811 print_ia64_vms_note (Elf_Internal_Note * pnote)
20812 {
20813 int maxlen = pnote->descsz;
20814
20815 if (maxlen < 2 || (unsigned long) maxlen != pnote->descsz)
20816 goto desc_size_fail;
20817
20818 switch (pnote->type)
20819 {
20820 case NT_VMS_MHD:
20821 if (maxlen <= 36)
20822 goto desc_size_fail;
20823
20824 int l = (int) strnlen (pnote->descdata + 34, maxlen - 34);
20825
20826 printf (_(" Creation date : %.17s\n"), pnote->descdata);
20827 printf (_(" Last patch date: %.17s\n"), pnote->descdata + 17);
20828 if (l + 34 < maxlen)
20829 {
20830 printf (_(" Module name : %s\n"), pnote->descdata + 34);
20831 if (l + 35 < maxlen)
20832 printf (_(" Module version : %s\n"), pnote->descdata + 34 + l + 1);
20833 else
20834 printf (_(" Module version : <missing>\n"));
20835 }
20836 else
20837 {
20838 printf (_(" Module name : <missing>\n"));
20839 printf (_(" Module version : <missing>\n"));
20840 }
20841 break;
20842
20843 case NT_VMS_LNM:
20844 printf (_(" Language: %.*s\n"), maxlen, pnote->descdata);
20845 break;
20846
20847 case NT_VMS_FPMODE:
20848 printf (_(" Floating Point mode: "));
20849 if (maxlen < 8)
20850 goto desc_size_fail;
20851 /* FIXME: Generate an error if descsz > 8 ? */
20852
20853 printf ("0x%016" PRIx64 "\n",
20854 byte_get ((unsigned char *) pnote->descdata, 8));
20855 break;
20856
20857 case NT_VMS_LINKTIME:
20858 printf (_(" Link time: "));
20859 if (maxlen < 8)
20860 goto desc_size_fail;
20861 /* FIXME: Generate an error if descsz > 8 ? */
20862
20863 print_vms_time (byte_get ((unsigned char *) pnote->descdata, 8));
20864 printf ("\n");
20865 break;
20866
20867 case NT_VMS_PATCHTIME:
20868 printf (_(" Patch time: "));
20869 if (maxlen < 8)
20870 goto desc_size_fail;
20871 /* FIXME: Generate an error if descsz > 8 ? */
20872
20873 print_vms_time (byte_get ((unsigned char *) pnote->descdata, 8));
20874 printf ("\n");
20875 break;
20876
20877 case NT_VMS_ORIG_DYN:
20878 if (maxlen < 34)
20879 goto desc_size_fail;
20880
20881 printf (_(" Major id: %u, minor id: %u\n"),
20882 (unsigned) byte_get ((unsigned char *) pnote->descdata, 4),
20883 (unsigned) byte_get ((unsigned char *) pnote->descdata + 4, 4));
20884 printf (_(" Last modified : "));
20885 print_vms_time (byte_get ((unsigned char *) pnote->descdata + 8, 8));
20886 printf (_("\n Link flags : "));
20887 printf ("0x%016" PRIx64 "\n",
20888 byte_get ((unsigned char *) pnote->descdata + 16, 8));
20889 printf (_(" Header flags: 0x%08x\n"),
20890 (unsigned) byte_get ((unsigned char *) pnote->descdata + 24, 4));
20891 printf (_(" Image id : %.*s\n"), maxlen - 32, pnote->descdata + 32);
20892 break;
20893
20894 case NT_VMS_IMGNAM:
20895 printf (_(" Image name: %.*s\n"), maxlen, pnote->descdata);
20896 break;
20897
20898 case NT_VMS_GSTNAM:
20899 printf (_(" Global symbol table name: %.*s\n"), maxlen, pnote->descdata);
20900 break;
20901
20902 case NT_VMS_IMGID:
20903 printf (_(" Image id: %.*s\n"), maxlen, pnote->descdata);
20904 break;
20905
20906 case NT_VMS_LINKID:
20907 printf (_(" Linker id: %.*s\n"), maxlen, pnote->descdata);
20908 break;
20909
20910 default:
20911 return false;
20912 }
20913
20914 return true;
20915
20916 desc_size_fail:
20917 printf (_(" <corrupt - data size is too small>\n"));
20918 error (_("corrupt IA64 note: data size is too small\n"));
20919 return false;
20920 }
20921
20922 struct build_attr_cache {
20923 Filedata *filedata;
20924 char *strtab;
20925 unsigned long strtablen;
20926 Elf_Internal_Sym *symtab;
20927 unsigned long nsyms;
20928 } ba_cache;
20929
20930 /* Find the symbol associated with a build attribute that is attached
20931 to address OFFSET. If PNAME is non-NULL then store the name of
20932 the symbol (if found) in the provided pointer, Returns NULL if a
20933 symbol could not be found. */
20934
20935 static Elf_Internal_Sym *
20936 get_symbol_for_build_attribute (Filedata *filedata,
20937 unsigned long offset,
20938 bool is_open_attr,
20939 const char **pname)
20940 {
20941 Elf_Internal_Sym *saved_sym = NULL;
20942 Elf_Internal_Sym *sym;
20943
20944 if (filedata->section_headers != NULL
20945 && (ba_cache.filedata == NULL || filedata != ba_cache.filedata))
20946 {
20947 Elf_Internal_Shdr * symsec;
20948
20949 free (ba_cache.strtab);
20950 ba_cache.strtab = NULL;
20951 free (ba_cache.symtab);
20952 ba_cache.symtab = NULL;
20953
20954 /* Load the symbol and string sections. */
20955 for (symsec = filedata->section_headers;
20956 symsec < filedata->section_headers + filedata->file_header.e_shnum;
20957 symsec ++)
20958 {
20959 if (symsec->sh_type == SHT_SYMTAB
20960 && get_symtab (filedata, symsec,
20961 &ba_cache.symtab, &ba_cache.nsyms,
20962 &ba_cache.strtab, &ba_cache.strtablen))
20963 break;
20964 }
20965 ba_cache.filedata = filedata;
20966 }
20967
20968 if (ba_cache.symtab == NULL)
20969 return NULL;
20970
20971 /* Find a symbol whose value matches offset. */
20972 for (sym = ba_cache.symtab; sym < ba_cache.symtab + ba_cache.nsyms; sym ++)
20973 if (sym->st_value == offset)
20974 {
20975 if (sym->st_name >= ba_cache.strtablen)
20976 /* Huh ? This should not happen. */
20977 continue;
20978
20979 if (ba_cache.strtab[sym->st_name] == 0)
20980 continue;
20981
20982 /* The AArch64, ARM and RISC-V architectures define mapping symbols
20983 (eg $d, $x, $t) which we want to ignore. */
20984 if (ba_cache.strtab[sym->st_name] == '$'
20985 && ba_cache.strtab[sym->st_name + 1] != 0
20986 && ba_cache.strtab[sym->st_name + 2] == 0)
20987 continue;
20988
20989 if (is_open_attr)
20990 {
20991 /* For OPEN attributes we prefer GLOBAL over LOCAL symbols
20992 and FILE or OBJECT symbols over NOTYPE symbols. We skip
20993 FUNC symbols entirely. */
20994 switch (ELF_ST_TYPE (sym->st_info))
20995 {
20996 case STT_OBJECT:
20997 case STT_FILE:
20998 saved_sym = sym;
20999 if (sym->st_size)
21000 {
21001 /* If the symbol has a size associated
21002 with it then we can stop searching. */
21003 sym = ba_cache.symtab + ba_cache.nsyms;
21004 }
21005 continue;
21006
21007 case STT_FUNC:
21008 /* Ignore function symbols. */
21009 continue;
21010
21011 default:
21012 break;
21013 }
21014
21015 switch (ELF_ST_BIND (sym->st_info))
21016 {
21017 case STB_GLOBAL:
21018 if (saved_sym == NULL
21019 || ELF_ST_TYPE (saved_sym->st_info) != STT_OBJECT)
21020 saved_sym = sym;
21021 break;
21022
21023 case STB_LOCAL:
21024 if (saved_sym == NULL)
21025 saved_sym = sym;
21026 break;
21027
21028 default:
21029 break;
21030 }
21031 }
21032 else
21033 {
21034 if (ELF_ST_TYPE (sym->st_info) != STT_FUNC)
21035 continue;
21036
21037 saved_sym = sym;
21038 break;
21039 }
21040 }
21041
21042 if (saved_sym && pname)
21043 * pname = ba_cache.strtab + saved_sym->st_name;
21044
21045 return saved_sym;
21046 }
21047
21048 /* Returns true iff addr1 and addr2 are in the same section. */
21049
21050 static bool
21051 same_section (Filedata * filedata, unsigned long addr1, unsigned long addr2)
21052 {
21053 Elf_Internal_Shdr * a1;
21054 Elf_Internal_Shdr * a2;
21055
21056 a1 = find_section_by_address (filedata, addr1);
21057 a2 = find_section_by_address (filedata, addr2);
21058
21059 return a1 == a2 && a1 != NULL;
21060 }
21061
21062 static bool
21063 print_gnu_build_attribute_description (Elf_Internal_Note * pnote,
21064 Filedata * filedata)
21065 {
21066 static unsigned long global_offset = 0;
21067 static unsigned long global_end = 0;
21068 static unsigned long func_offset = 0;
21069 static unsigned long func_end = 0;
21070
21071 Elf_Internal_Sym *sym;
21072 const char *name;
21073 unsigned long start;
21074 unsigned long end;
21075 bool is_open_attr = pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN;
21076
21077 switch (pnote->descsz)
21078 {
21079 case 0:
21080 /* A zero-length description means that the range of
21081 the previous note of the same type should be used. */
21082 if (is_open_attr)
21083 {
21084 if (global_end > global_offset)
21085 printf (_(" Applies to region from %#lx to %#lx\n"),
21086 global_offset, global_end);
21087 else
21088 printf (_(" Applies to region from %#lx\n"), global_offset);
21089 }
21090 else
21091 {
21092 if (func_end > func_offset)
21093 printf (_(" Applies to region from %#lx to %#lx\n"), func_offset, func_end);
21094 else
21095 printf (_(" Applies to region from %#lx\n"), func_offset);
21096 }
21097 return true;
21098
21099 case 4:
21100 start = byte_get ((unsigned char *) pnote->descdata, 4);
21101 end = 0;
21102 break;
21103
21104 case 8:
21105 start = byte_get ((unsigned char *) pnote->descdata, 4);
21106 end = byte_get ((unsigned char *) pnote->descdata + 4, 4);
21107 break;
21108
21109 case 16:
21110 start = byte_get ((unsigned char *) pnote->descdata, 8);
21111 end = byte_get ((unsigned char *) pnote->descdata + 8, 8);
21112 break;
21113
21114 default:
21115 error (_(" <invalid description size: %lx>\n"), pnote->descsz);
21116 printf (_(" <invalid descsz>"));
21117 return false;
21118 }
21119
21120 name = NULL;
21121 sym = get_symbol_for_build_attribute (filedata, start, is_open_attr, & name);
21122 /* As of version 5 of the annobin plugin, filename symbols are biased by 2
21123 in order to avoid them being confused with the start address of the
21124 first function in the file... */
21125 if (sym == NULL && is_open_attr)
21126 sym = get_symbol_for_build_attribute (filedata, start + 2, is_open_attr,
21127 & name);
21128
21129 if (end == 0 && sym != NULL && sym->st_size > 0)
21130 end = start + sym->st_size;
21131
21132 if (is_open_attr)
21133 {
21134 /* FIXME: Need to properly allow for section alignment.
21135 16 is just the alignment used on x86_64. */
21136 if (global_end > 0
21137 && start > BFD_ALIGN (global_end, 16)
21138 /* Build notes are not guaranteed to be organised in order of
21139 increasing address, but we should find the all of the notes
21140 for one section in the same place. */
21141 && same_section (filedata, start, global_end))
21142 warn (_("Gap in build notes detected from %#lx to %#lx\n"),
21143 global_end + 1, start - 1);
21144
21145 printf (_(" Applies to region from %#lx"), start);
21146 global_offset = start;
21147
21148 if (end)
21149 {
21150 printf (_(" to %#lx"), end);
21151 global_end = end;
21152 }
21153 }
21154 else
21155 {
21156 printf (_(" Applies to region from %#lx"), start);
21157 func_offset = start;
21158
21159 if (end)
21160 {
21161 printf (_(" to %#lx"), end);
21162 func_end = end;
21163 }
21164 }
21165
21166 if (sym && name)
21167 printf (_(" (%s)"), name);
21168
21169 printf ("\n");
21170 return true;
21171 }
21172
21173 static bool
21174 print_gnu_build_attribute_name (Elf_Internal_Note * pnote)
21175 {
21176 static const char string_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_STRING, 0 };
21177 static const char number_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC, 0 };
21178 static const char bool_expected [3] = { GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE, GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE, 0 };
21179 char name_type;
21180 char name_attribute;
21181 const char * expected_types;
21182 const char * name = pnote->namedata;
21183 const char * text;
21184 signed int left;
21185
21186 if (name == NULL || pnote->namesz < 2)
21187 {
21188 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
21189 print_symbol (-20, _(" <corrupt name>"));
21190 return false;
21191 }
21192
21193 if (do_wide)
21194 left = 28;
21195 else
21196 left = 20;
21197
21198 /* Version 2 of the spec adds a "GA" prefix to the name field. */
21199 if (name[0] == 'G' && name[1] == 'A')
21200 {
21201 if (pnote->namesz < 4)
21202 {
21203 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
21204 print_symbol (-20, _(" <corrupt name>"));
21205 return false;
21206 }
21207
21208 printf ("GA");
21209 name += 2;
21210 left -= 2;
21211 }
21212
21213 switch ((name_type = * name))
21214 {
21215 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
21216 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
21217 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
21218 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
21219 printf ("%c", * name);
21220 left --;
21221 break;
21222 default:
21223 error (_("unrecognised attribute type in name field: %d\n"), name_type);
21224 print_symbol (-20, _("<unknown name type>"));
21225 return false;
21226 }
21227
21228 ++ name;
21229 text = NULL;
21230
21231 switch ((name_attribute = * name))
21232 {
21233 case GNU_BUILD_ATTRIBUTE_VERSION:
21234 text = _("<version>");
21235 expected_types = string_expected;
21236 ++ name;
21237 break;
21238 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
21239 text = _("<stack prot>");
21240 expected_types = "!+*";
21241 ++ name;
21242 break;
21243 case GNU_BUILD_ATTRIBUTE_RELRO:
21244 text = _("<relro>");
21245 expected_types = bool_expected;
21246 ++ name;
21247 break;
21248 case GNU_BUILD_ATTRIBUTE_STACK_SIZE:
21249 text = _("<stack size>");
21250 expected_types = number_expected;
21251 ++ name;
21252 break;
21253 case GNU_BUILD_ATTRIBUTE_TOOL:
21254 text = _("<tool>");
21255 expected_types = string_expected;
21256 ++ name;
21257 break;
21258 case GNU_BUILD_ATTRIBUTE_ABI:
21259 text = _("<ABI>");
21260 expected_types = "$*";
21261 ++ name;
21262 break;
21263 case GNU_BUILD_ATTRIBUTE_PIC:
21264 text = _("<PIC>");
21265 expected_types = number_expected;
21266 ++ name;
21267 break;
21268 case GNU_BUILD_ATTRIBUTE_SHORT_ENUM:
21269 text = _("<short enum>");
21270 expected_types = bool_expected;
21271 ++ name;
21272 break;
21273 default:
21274 if (ISPRINT (* name))
21275 {
21276 int len = strnlen (name, pnote->namesz - (name - pnote->namedata)) + 1;
21277
21278 if (len > left && ! do_wide)
21279 len = left;
21280 printf ("%.*s:", len, name);
21281 left -= len;
21282 name += len;
21283 }
21284 else
21285 {
21286 static char tmpbuf [128];
21287
21288 error (_("unrecognised byte in name field: %d\n"), * name);
21289 sprintf (tmpbuf, _("<unknown:_%d>"), * name);
21290 text = tmpbuf;
21291 name ++;
21292 }
21293 expected_types = "*$!+";
21294 break;
21295 }
21296
21297 if (text)
21298 left -= printf ("%s", text);
21299
21300 if (strchr (expected_types, name_type) == NULL)
21301 warn (_("attribute does not have an expected type (%c)\n"), name_type);
21302
21303 if ((unsigned long)(name - pnote->namedata) > pnote->namesz)
21304 {
21305 error (_("corrupt name field: namesz: %lu but parsing gets to %ld\n"),
21306 (unsigned long) pnote->namesz,
21307 (long) (name - pnote->namedata));
21308 return false;
21309 }
21310
21311 if (left < 1 && ! do_wide)
21312 return true;
21313
21314 switch (name_type)
21315 {
21316 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
21317 {
21318 unsigned int bytes;
21319 unsigned long long val = 0;
21320 unsigned int shift = 0;
21321 char * decoded = NULL;
21322
21323 bytes = pnote->namesz - (name - pnote->namedata);
21324 if (bytes > 0)
21325 /* The -1 is because the name field is always 0 terminated, and we
21326 want to be able to ensure that the shift in the while loop below
21327 will not overflow. */
21328 -- bytes;
21329
21330 if (bytes > sizeof (val))
21331 {
21332 error (_("corrupt numeric name field: too many bytes in the value: %x\n"),
21333 bytes);
21334 bytes = sizeof (val);
21335 }
21336 /* We do not bother to warn if bytes == 0 as this can
21337 happen with some early versions of the gcc plugin. */
21338
21339 while (bytes --)
21340 {
21341 unsigned long long byte = *name++ & 0xff;
21342
21343 val |= byte << shift;
21344 shift += 8;
21345 }
21346
21347 switch (name_attribute)
21348 {
21349 case GNU_BUILD_ATTRIBUTE_PIC:
21350 switch (val)
21351 {
21352 case 0: decoded = "static"; break;
21353 case 1: decoded = "pic"; break;
21354 case 2: decoded = "PIC"; break;
21355 case 3: decoded = "pie"; break;
21356 case 4: decoded = "PIE"; break;
21357 default: break;
21358 }
21359 break;
21360 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
21361 switch (val)
21362 {
21363 /* Based upon the SPCT_FLAG_xxx enum values in gcc/cfgexpand.c. */
21364 case 0: decoded = "off"; break;
21365 case 1: decoded = "on"; break;
21366 case 2: decoded = "all"; break;
21367 case 3: decoded = "strong"; break;
21368 case 4: decoded = "explicit"; break;
21369 default: break;
21370 }
21371 break;
21372 default:
21373 break;
21374 }
21375
21376 if (decoded != NULL)
21377 {
21378 print_symbol (-left, decoded);
21379 left = 0;
21380 }
21381 else if (val == 0)
21382 {
21383 printf ("0x0");
21384 left -= 3;
21385 }
21386 else
21387 {
21388 if (do_wide)
21389 left -= printf ("0x%llx", val);
21390 else
21391 left -= printf ("0x%-.*llx", left, val);
21392 }
21393 }
21394 break;
21395 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
21396 left -= print_symbol (- left, name);
21397 break;
21398 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
21399 left -= print_symbol (- left, "true");
21400 break;
21401 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
21402 left -= print_symbol (- left, "false");
21403 break;
21404 }
21405
21406 if (do_wide && left > 0)
21407 printf ("%-*s", left, " ");
21408
21409 return true;
21410 }
21411
21412 /* Print the contents of PNOTE as hex. */
21413
21414 static void
21415 print_note_contents_hex (Elf_Internal_Note *pnote)
21416 {
21417 if (pnote->descsz)
21418 {
21419 unsigned long i;
21420
21421 printf (_(" description data: "));
21422 for (i = 0; i < pnote->descsz; i++)
21423 printf ("%02x ", pnote->descdata[i] & 0xff);
21424 if (!do_wide)
21425 printf ("\n");
21426 }
21427
21428 if (do_wide)
21429 printf ("\n");
21430 }
21431
21432 #if defined HAVE_MSGPACK
21433
21434 static void
21435 print_indents (int n)
21436 {
21437 printf (" ");
21438
21439 for (int i = 0; i < n; i++)
21440 printf (" ");
21441 }
21442
21443 /* Print OBJ in human-readable form. */
21444
21445 static void
21446 dump_msgpack_obj (const msgpack_object *obj, int indent)
21447 {
21448 switch (obj->type)
21449 {
21450 case MSGPACK_OBJECT_NIL:
21451 printf ("(nil)");
21452 break;
21453
21454 case MSGPACK_OBJECT_BOOLEAN:
21455 printf ("%s", obj->via.boolean ? "true" : "false");
21456 break;
21457
21458 case MSGPACK_OBJECT_POSITIVE_INTEGER:
21459 printf ("%" PRIu64, obj->via.u64);
21460 break;
21461
21462 case MSGPACK_OBJECT_NEGATIVE_INTEGER:
21463 printf ("%" PRIi64, obj->via.i64);
21464 break;
21465
21466 case MSGPACK_OBJECT_FLOAT32:
21467 case MSGPACK_OBJECT_FLOAT64:
21468 printf ("%f", obj->via.f64);
21469 break;
21470
21471 case MSGPACK_OBJECT_STR:
21472 printf ("\"%.*s\"", obj->via.str.size, obj->via.str.ptr);
21473 break;
21474
21475 case MSGPACK_OBJECT_ARRAY:
21476 {
21477 const msgpack_object_array *array = &obj->via.array;
21478
21479 printf ("[\n");
21480 ++indent;
21481
21482 for (uint32_t i = 0; i < array->size; ++i)
21483 {
21484 const msgpack_object *item = &array->ptr[i];
21485
21486 print_indents (indent);
21487 dump_msgpack_obj (item, indent);
21488 printf (",\n");
21489 }
21490
21491 --indent;
21492 print_indents (indent);
21493 printf ("]");
21494 break;
21495 }
21496 break;
21497
21498 case MSGPACK_OBJECT_MAP:
21499 {
21500 const msgpack_object_map *map = &obj->via.map;
21501
21502 printf ("{\n");
21503 ++indent;
21504
21505 for (uint32_t i = 0; i < map->size; ++i)
21506 {
21507 const msgpack_object_kv *kv = &map->ptr[i];
21508 const msgpack_object *key = &kv->key;
21509 const msgpack_object *val = &kv->val;
21510
21511 print_indents (indent);
21512 dump_msgpack_obj (key, indent);
21513 printf (": ");
21514 dump_msgpack_obj (val, indent);
21515
21516 printf (",\n");
21517 }
21518
21519 --indent;
21520 print_indents (indent);
21521 printf ("}");
21522
21523 break;
21524 }
21525
21526 case MSGPACK_OBJECT_BIN:
21527 printf ("(bin)");
21528 break;
21529
21530 case MSGPACK_OBJECT_EXT:
21531 printf ("(ext)");
21532 break;
21533 }
21534 }
21535
21536 static void
21537 dump_msgpack (const msgpack_unpacked *msg)
21538 {
21539 print_indents (0);
21540 dump_msgpack_obj (&msg->data, 0);
21541 printf ("\n");
21542 }
21543
21544 #endif /* defined HAVE_MSGPACK */
21545
21546 static bool
21547 print_amdgpu_note (Elf_Internal_Note *pnote)
21548 {
21549 #if defined HAVE_MSGPACK
21550 /* If msgpack is available, decode and dump the note's content. */
21551 bool ret;
21552 msgpack_unpacked msg;
21553 msgpack_unpack_return msgpack_ret;
21554
21555 assert (pnote->type == NT_AMDGPU_METADATA);
21556
21557 msgpack_unpacked_init (&msg);
21558 msgpack_ret = msgpack_unpack_next (&msg, pnote->descdata, pnote->descsz,
21559 NULL);
21560
21561 switch (msgpack_ret)
21562 {
21563 case MSGPACK_UNPACK_SUCCESS:
21564 dump_msgpack (&msg);
21565 ret = true;
21566 break;
21567
21568 default:
21569 error (_("failed to unpack msgpack contents in NT_AMDGPU_METADATA note"));
21570 ret = false;
21571 break;
21572 }
21573
21574 msgpack_unpacked_destroy (&msg);
21575 return ret;
21576 #else
21577 /* msgpack is not available, dump contents as hex. */
21578 print_note_contents_hex (pnote);
21579 return true;
21580 #endif
21581 }
21582
21583 /* Note that by the ELF standard, the name field is already null byte
21584 terminated, and namesz includes the terminating null byte.
21585 I.E. the value of namesz for the name "FSF" is 4.
21586
21587 If the value of namesz is zero, there is no name present. */
21588
21589 static bool
21590 process_note (Elf_Internal_Note * pnote,
21591 Filedata * filedata)
21592 {
21593 const char * name = pnote->namesz ? pnote->namedata : "(NONE)";
21594 const char * nt;
21595
21596 if (pnote->namesz == 0)
21597 /* If there is no note name, then use the default set of
21598 note type strings. */
21599 nt = get_note_type (filedata, pnote->type);
21600
21601 else if (startswith (pnote->namedata, "GNU"))
21602 /* GNU-specific object file notes. */
21603 nt = get_gnu_elf_note_type (pnote->type);
21604
21605 else if (startswith (pnote->namedata, "AMDGPU"))
21606 /* AMDGPU-specific object file notes. */
21607 nt = get_amdgpu_elf_note_type (pnote->type);
21608
21609 else if (startswith (pnote->namedata, "FreeBSD"))
21610 /* FreeBSD-specific core file notes. */
21611 nt = get_freebsd_elfcore_note_type (filedata, pnote->type);
21612
21613 else if (startswith (pnote->namedata, "NetBSD-CORE"))
21614 /* NetBSD-specific core file notes. */
21615 nt = get_netbsd_elfcore_note_type (filedata, pnote->type);
21616
21617 else if (startswith (pnote->namedata, "NetBSD"))
21618 /* NetBSD-specific core file notes. */
21619 return process_netbsd_elf_note (pnote);
21620
21621 else if (startswith (pnote->namedata, "PaX"))
21622 /* NetBSD-specific core file notes. */
21623 return process_netbsd_elf_note (pnote);
21624
21625 else if (startswith (pnote->namedata, "OpenBSD"))
21626 /* OpenBSD-specific core file notes. */
21627 nt = get_openbsd_elfcore_note_type (filedata, pnote->type);
21628
21629 else if (startswith (pnote->namedata, "SPU/"))
21630 {
21631 /* SPU-specific core file notes. */
21632 nt = pnote->namedata + 4;
21633 name = "SPU";
21634 }
21635
21636 else if (startswith (pnote->namedata, "IPF/VMS"))
21637 /* VMS/ia64-specific file notes. */
21638 nt = get_ia64_vms_note_type (pnote->type);
21639
21640 else if (startswith (pnote->namedata, "stapsdt"))
21641 nt = get_stapsdt_note_type (pnote->type);
21642
21643 else
21644 /* Don't recognize this note name; just use the default set of
21645 note type strings. */
21646 nt = get_note_type (filedata, pnote->type);
21647
21648 printf (" ");
21649
21650 if (((startswith (pnote->namedata, "GA")
21651 && strchr ("*$!+", pnote->namedata[2]) != NULL)
21652 || strchr ("*$!+", pnote->namedata[0]) != NULL)
21653 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
21654 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
21655 print_gnu_build_attribute_name (pnote);
21656 else
21657 print_symbol (-20, name);
21658
21659 if (do_wide)
21660 printf (" 0x%08lx\t%s\t", pnote->descsz, nt);
21661 else
21662 printf (" 0x%08lx\t%s\n", pnote->descsz, nt);
21663
21664 if (startswith (pnote->namedata, "IPF/VMS"))
21665 return print_ia64_vms_note (pnote);
21666 else if (startswith (pnote->namedata, "GNU"))
21667 return print_gnu_note (filedata, pnote);
21668 else if (startswith (pnote->namedata, "stapsdt"))
21669 return print_stapsdt_note (pnote);
21670 else if (startswith (pnote->namedata, "CORE"))
21671 return print_core_note (pnote);
21672 else if (startswith (pnote->namedata, "FDO"))
21673 return print_fdo_note (pnote);
21674 else if (((startswith (pnote->namedata, "GA")
21675 && strchr ("*$!+", pnote->namedata[2]) != NULL)
21676 || strchr ("*$!+", pnote->namedata[0]) != NULL)
21677 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
21678 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
21679 return print_gnu_build_attribute_description (pnote, filedata);
21680 else if (startswith (pnote->namedata, "AMDGPU")
21681 && pnote->type == NT_AMDGPU_METADATA)
21682 return print_amdgpu_note (pnote);
21683
21684 print_note_contents_hex (pnote);
21685 return true;
21686 }
21687
21688 static bool
21689 process_notes_at (Filedata * filedata,
21690 Elf_Internal_Shdr * section,
21691 uint64_t offset,
21692 uint64_t length,
21693 uint64_t align)
21694 {
21695 Elf_External_Note *pnotes;
21696 Elf_External_Note *external;
21697 char *end;
21698 bool res = true;
21699
21700 if (length <= 0)
21701 return false;
21702
21703 if (section)
21704 {
21705 pnotes = (Elf_External_Note *) get_section_contents (section, filedata);
21706 if (pnotes)
21707 {
21708 if (! apply_relocations (filedata, section, (unsigned char *) pnotes, length, NULL, NULL))
21709 {
21710 free (pnotes);
21711 return false;
21712 }
21713 }
21714 }
21715 else
21716 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
21717 _("notes"));
21718
21719 if (pnotes == NULL)
21720 return false;
21721
21722 external = pnotes;
21723
21724 if (filedata->is_separate)
21725 printf (_("In linked file '%s': "), filedata->file_name);
21726 else
21727 printf ("\n");
21728 if (section)
21729 printf (_("Displaying notes found in: %s\n"), printable_section_name (filedata, section));
21730 else
21731 printf (_("Displaying notes found at file offset 0x%08lx with length 0x%08lx:\n"),
21732 (unsigned long) offset, (unsigned long) length);
21733
21734 /* NB: Some note sections may have alignment value of 0 or 1. gABI
21735 specifies that notes should be aligned to 4 bytes in 32-bit
21736 objects and to 8 bytes in 64-bit objects. As a Linux extension,
21737 we also support 4 byte alignment in 64-bit objects. If section
21738 alignment is less than 4, we treate alignment as 4 bytes. */
21739 if (align < 4)
21740 align = 4;
21741 else if (align != 4 && align != 8)
21742 {
21743 warn (_("Corrupt note: alignment %ld, expecting 4 or 8\n"),
21744 (long) align);
21745 free (pnotes);
21746 return false;
21747 }
21748
21749 printf (_(" %-20s %-10s\tDescription\n"), _("Owner"), _("Data size"));
21750
21751 end = (char *) pnotes + length;
21752 while ((char *) external < end)
21753 {
21754 Elf_Internal_Note inote;
21755 size_t min_notesz;
21756 char * next;
21757 char * temp = NULL;
21758 size_t data_remaining = end - (char *) external;
21759
21760 if (!is_ia64_vms (filedata))
21761 {
21762 /* PR binutils/15191
21763 Make sure that there is enough data to read. */
21764 min_notesz = offsetof (Elf_External_Note, name);
21765 if (data_remaining < min_notesz)
21766 {
21767 warn (ngettext ("Corrupt note: only %ld byte remains, "
21768 "not enough for a full note\n",
21769 "Corrupt note: only %ld bytes remain, "
21770 "not enough for a full note\n",
21771 data_remaining),
21772 (long) data_remaining);
21773 break;
21774 }
21775 data_remaining -= min_notesz;
21776
21777 inote.type = BYTE_GET (external->type);
21778 inote.namesz = BYTE_GET (external->namesz);
21779 inote.namedata = external->name;
21780 inote.descsz = BYTE_GET (external->descsz);
21781 inote.descdata = ((char *) external
21782 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
21783 inote.descpos = offset + (inote.descdata - (char *) pnotes);
21784 next = ((char *) external
21785 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
21786 }
21787 else
21788 {
21789 Elf64_External_VMS_Note *vms_external;
21790
21791 /* PR binutils/15191
21792 Make sure that there is enough data to read. */
21793 min_notesz = offsetof (Elf64_External_VMS_Note, name);
21794 if (data_remaining < min_notesz)
21795 {
21796 warn (ngettext ("Corrupt note: only %ld byte remains, "
21797 "not enough for a full note\n",
21798 "Corrupt note: only %ld bytes remain, "
21799 "not enough for a full note\n",
21800 data_remaining),
21801 (long) data_remaining);
21802 break;
21803 }
21804 data_remaining -= min_notesz;
21805
21806 vms_external = (Elf64_External_VMS_Note *) external;
21807 inote.type = BYTE_GET (vms_external->type);
21808 inote.namesz = BYTE_GET (vms_external->namesz);
21809 inote.namedata = vms_external->name;
21810 inote.descsz = BYTE_GET (vms_external->descsz);
21811 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
21812 inote.descpos = offset + (inote.descdata - (char *) pnotes);
21813 next = inote.descdata + align_power (inote.descsz, 3);
21814 }
21815
21816 /* PR 17531: file: 3443835e. */
21817 /* PR 17531: file: id:000000,sig:11,src:006986,op:havoc,rep:4. */
21818 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
21819 || (size_t) (inote.descdata - inote.namedata) > data_remaining
21820 || (size_t) (next - inote.descdata) < inote.descsz
21821 || ((size_t) (next - inote.descdata)
21822 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
21823 {
21824 warn (_("note with invalid namesz and/or descsz found at offset 0x%lx\n"),
21825 (unsigned long) ((char *) external - (char *) pnotes));
21826 warn (_(" type: 0x%lx, namesize: 0x%08lx, descsize: 0x%08lx, alignment: %u\n"),
21827 inote.type, inote.namesz, inote.descsz, (int) align);
21828 break;
21829 }
21830
21831 external = (Elf_External_Note *) next;
21832
21833 /* Verify that name is null terminated. It appears that at least
21834 one version of Linux (RedHat 6.0) generates corefiles that don't
21835 comply with the ELF spec by failing to include the null byte in
21836 namesz. */
21837 if (inote.namesz > 0 && inote.namedata[inote.namesz - 1] != '\0')
21838 {
21839 if ((size_t) (inote.descdata - inote.namedata) == inote.namesz)
21840 {
21841 temp = (char *) malloc (inote.namesz + 1);
21842 if (temp == NULL)
21843 {
21844 error (_("Out of memory allocating space for inote name\n"));
21845 res = false;
21846 break;
21847 }
21848
21849 memcpy (temp, inote.namedata, inote.namesz);
21850 inote.namedata = temp;
21851 }
21852 inote.namedata[inote.namesz] = 0;
21853 }
21854
21855 if (! process_note (& inote, filedata))
21856 res = false;
21857
21858 free (temp);
21859 temp = NULL;
21860 }
21861
21862 free (pnotes);
21863
21864 return res;
21865 }
21866
21867 static bool
21868 process_corefile_note_segments (Filedata * filedata)
21869 {
21870 Elf_Internal_Phdr *segment;
21871 unsigned int i;
21872 bool res = true;
21873
21874 if (! get_program_headers (filedata))
21875 return true;
21876
21877 for (i = 0, segment = filedata->program_headers;
21878 i < filedata->file_header.e_phnum;
21879 i++, segment++)
21880 {
21881 if (segment->p_type == PT_NOTE)
21882 if (! process_notes_at (filedata, NULL, segment->p_offset,
21883 segment->p_filesz, segment->p_align))
21884 res = false;
21885 }
21886
21887 return res;
21888 }
21889
21890 static bool
21891 process_v850_notes (Filedata * filedata, uint64_t offset, uint64_t length)
21892 {
21893 Elf_External_Note * pnotes;
21894 Elf_External_Note * external;
21895 char * end;
21896 bool res = true;
21897
21898 if (length <= 0)
21899 return false;
21900
21901 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
21902 _("v850 notes"));
21903 if (pnotes == NULL)
21904 return false;
21905
21906 external = pnotes;
21907 end = (char*) pnotes + length;
21908
21909 printf (_("\nDisplaying contents of Renesas V850 notes section at offset 0x%lx with length 0x%lx:\n"),
21910 (unsigned long) offset, (unsigned long) length);
21911
21912 while ((char *) external + sizeof (Elf_External_Note) < end)
21913 {
21914 Elf_External_Note * next;
21915 Elf_Internal_Note inote;
21916
21917 inote.type = BYTE_GET (external->type);
21918 inote.namesz = BYTE_GET (external->namesz);
21919 inote.namedata = external->name;
21920 inote.descsz = BYTE_GET (external->descsz);
21921 inote.descdata = inote.namedata + align_power (inote.namesz, 2);
21922 inote.descpos = offset + (inote.descdata - (char *) pnotes);
21923
21924 if (inote.descdata < (char *) pnotes || inote.descdata >= end)
21925 {
21926 warn (_("Corrupt note: name size is too big: %lx\n"), inote.namesz);
21927 inote.descdata = inote.namedata;
21928 inote.namesz = 0;
21929 }
21930
21931 next = (Elf_External_Note *) (inote.descdata + align_power (inote.descsz, 2));
21932
21933 if ( ((char *) next > end)
21934 || ((char *) next < (char *) pnotes))
21935 {
21936 warn (_("corrupt descsz found in note at offset 0x%lx\n"),
21937 (unsigned long) ((char *) external - (char *) pnotes));
21938 warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
21939 inote.type, inote.namesz, inote.descsz);
21940 break;
21941 }
21942
21943 external = next;
21944
21945 /* Prevent out-of-bounds indexing. */
21946 if ( inote.namedata + inote.namesz > end
21947 || inote.namedata + inote.namesz < inote.namedata)
21948 {
21949 warn (_("corrupt namesz found in note at offset 0x%lx\n"),
21950 (unsigned long) ((char *) external - (char *) pnotes));
21951 warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
21952 inote.type, inote.namesz, inote.descsz);
21953 break;
21954 }
21955
21956 printf (" %s: ", get_v850_elf_note_type (inote.type));
21957
21958 if (! print_v850_note (& inote))
21959 {
21960 res = false;
21961 printf ("<corrupt sizes: namesz: %lx, descsz: %lx>\n",
21962 inote.namesz, inote.descsz);
21963 }
21964 }
21965
21966 free (pnotes);
21967
21968 return res;
21969 }
21970
21971 static bool
21972 process_note_sections (Filedata * filedata)
21973 {
21974 Elf_Internal_Shdr *section;
21975 unsigned long i;
21976 unsigned int n = 0;
21977 bool res = true;
21978
21979 for (i = 0, section = filedata->section_headers;
21980 i < filedata->file_header.e_shnum && section != NULL;
21981 i++, section++)
21982 {
21983 if (section->sh_type == SHT_NOTE)
21984 {
21985 if (! process_notes_at (filedata, section, section->sh_offset,
21986 section->sh_size, section->sh_addralign))
21987 res = false;
21988 n++;
21989 }
21990
21991 if (( filedata->file_header.e_machine == EM_V800
21992 || filedata->file_header.e_machine == EM_V850
21993 || filedata->file_header.e_machine == EM_CYGNUS_V850)
21994 && section->sh_type == SHT_RENESAS_INFO)
21995 {
21996 if (! process_v850_notes (filedata, section->sh_offset,
21997 section->sh_size))
21998 res = false;
21999 n++;
22000 }
22001 }
22002
22003 if (n == 0)
22004 /* Try processing NOTE segments instead. */
22005 return process_corefile_note_segments (filedata);
22006
22007 return res;
22008 }
22009
22010 static bool
22011 process_notes (Filedata * filedata)
22012 {
22013 /* If we have not been asked to display the notes then do nothing. */
22014 if (! do_notes)
22015 return true;
22016
22017 if (filedata->file_header.e_type != ET_CORE)
22018 return process_note_sections (filedata);
22019
22020 /* No program headers means no NOTE segment. */
22021 if (filedata->file_header.e_phnum > 0)
22022 return process_corefile_note_segments (filedata);
22023
22024 if (filedata->is_separate)
22025 printf (_("No notes found in linked file '%s'.\n"),
22026 filedata->file_name);
22027 else
22028 printf (_("No notes found file.\n"));
22029
22030 return true;
22031 }
22032
22033 static unsigned char *
22034 display_public_gnu_attributes (unsigned char * start,
22035 const unsigned char * const end)
22036 {
22037 printf (_(" Unknown GNU attribute: %s\n"), start);
22038
22039 start += strnlen ((char *) start, end - start);
22040 display_raw_attribute (start, end);
22041
22042 return (unsigned char *) end;
22043 }
22044
22045 static unsigned char *
22046 display_generic_attribute (unsigned char * start,
22047 unsigned int tag,
22048 const unsigned char * const end)
22049 {
22050 if (tag == 0)
22051 return (unsigned char *) end;
22052
22053 return display_tag_value (tag, start, end);
22054 }
22055
22056 static bool
22057 process_arch_specific (Filedata * filedata)
22058 {
22059 if (! do_arch)
22060 return true;
22061
22062 switch (filedata->file_header.e_machine)
22063 {
22064 case EM_ARC:
22065 case EM_ARC_COMPACT:
22066 case EM_ARC_COMPACT2:
22067 return process_attributes (filedata, "ARC", SHT_ARC_ATTRIBUTES,
22068 display_arc_attribute,
22069 display_generic_attribute);
22070 case EM_ARM:
22071 return process_attributes (filedata, "aeabi", SHT_ARM_ATTRIBUTES,
22072 display_arm_attribute,
22073 display_generic_attribute);
22074
22075 case EM_MIPS:
22076 case EM_MIPS_RS3_LE:
22077 return process_mips_specific (filedata);
22078
22079 case EM_MSP430:
22080 return process_attributes (filedata, "mspabi", SHT_MSP430_ATTRIBUTES,
22081 display_msp430_attribute,
22082 display_msp430_gnu_attribute);
22083
22084 case EM_RISCV:
22085 return process_attributes (filedata, "riscv", SHT_RISCV_ATTRIBUTES,
22086 display_riscv_attribute,
22087 display_generic_attribute);
22088
22089 case EM_NDS32:
22090 return process_nds32_specific (filedata);
22091
22092 case EM_68K:
22093 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22094 display_m68k_gnu_attribute);
22095
22096 case EM_PPC:
22097 case EM_PPC64:
22098 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22099 display_power_gnu_attribute);
22100
22101 case EM_S390:
22102 case EM_S390_OLD:
22103 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22104 display_s390_gnu_attribute);
22105
22106 case EM_SPARC:
22107 case EM_SPARC32PLUS:
22108 case EM_SPARCV9:
22109 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22110 display_sparc_gnu_attribute);
22111
22112 case EM_TI_C6000:
22113 return process_attributes (filedata, "c6xabi", SHT_C6000_ATTRIBUTES,
22114 display_tic6x_attribute,
22115 display_generic_attribute);
22116
22117 case EM_CSKY:
22118 return process_attributes (filedata, "csky", SHT_CSKY_ATTRIBUTES,
22119 display_csky_attribute, NULL);
22120
22121 default:
22122 return process_attributes (filedata, "gnu", SHT_GNU_ATTRIBUTES,
22123 display_public_gnu_attributes,
22124 display_generic_attribute);
22125 }
22126 }
22127
22128 static bool
22129 get_file_header (Filedata * filedata)
22130 {
22131 /* Read in the identity array. */
22132 if (fread (filedata->file_header.e_ident, EI_NIDENT, 1, filedata->handle) != 1)
22133 return false;
22134
22135 /* Determine how to read the rest of the header. */
22136 switch (filedata->file_header.e_ident[EI_DATA])
22137 {
22138 default:
22139 case ELFDATANONE:
22140 case ELFDATA2LSB:
22141 byte_get = byte_get_little_endian;
22142 byte_put = byte_put_little_endian;
22143 break;
22144 case ELFDATA2MSB:
22145 byte_get = byte_get_big_endian;
22146 byte_put = byte_put_big_endian;
22147 break;
22148 }
22149
22150 /* For now we only support 32 bit and 64 bit ELF files. */
22151 is_32bit_elf = (filedata->file_header.e_ident[EI_CLASS] != ELFCLASS64);
22152
22153 /* Read in the rest of the header. */
22154 if (is_32bit_elf)
22155 {
22156 Elf32_External_Ehdr ehdr32;
22157
22158 if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, filedata->handle) != 1)
22159 return false;
22160
22161 filedata->file_header.e_type = BYTE_GET (ehdr32.e_type);
22162 filedata->file_header.e_machine = BYTE_GET (ehdr32.e_machine);
22163 filedata->file_header.e_version = BYTE_GET (ehdr32.e_version);
22164 filedata->file_header.e_entry = BYTE_GET (ehdr32.e_entry);
22165 filedata->file_header.e_phoff = BYTE_GET (ehdr32.e_phoff);
22166 filedata->file_header.e_shoff = BYTE_GET (ehdr32.e_shoff);
22167 filedata->file_header.e_flags = BYTE_GET (ehdr32.e_flags);
22168 filedata->file_header.e_ehsize = BYTE_GET (ehdr32.e_ehsize);
22169 filedata->file_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
22170 filedata->file_header.e_phnum = BYTE_GET (ehdr32.e_phnum);
22171 filedata->file_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
22172 filedata->file_header.e_shnum = BYTE_GET (ehdr32.e_shnum);
22173 filedata->file_header.e_shstrndx = BYTE_GET (ehdr32.e_shstrndx);
22174 }
22175 else
22176 {
22177 Elf64_External_Ehdr ehdr64;
22178
22179 if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, filedata->handle) != 1)
22180 return false;
22181
22182 filedata->file_header.e_type = BYTE_GET (ehdr64.e_type);
22183 filedata->file_header.e_machine = BYTE_GET (ehdr64.e_machine);
22184 filedata->file_header.e_version = BYTE_GET (ehdr64.e_version);
22185 filedata->file_header.e_entry = BYTE_GET (ehdr64.e_entry);
22186 filedata->file_header.e_phoff = BYTE_GET (ehdr64.e_phoff);
22187 filedata->file_header.e_shoff = BYTE_GET (ehdr64.e_shoff);
22188 filedata->file_header.e_flags = BYTE_GET (ehdr64.e_flags);
22189 filedata->file_header.e_ehsize = BYTE_GET (ehdr64.e_ehsize);
22190 filedata->file_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
22191 filedata->file_header.e_phnum = BYTE_GET (ehdr64.e_phnum);
22192 filedata->file_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
22193 filedata->file_header.e_shnum = BYTE_GET (ehdr64.e_shnum);
22194 filedata->file_header.e_shstrndx = BYTE_GET (ehdr64.e_shstrndx);
22195 }
22196
22197 return true;
22198 }
22199
22200 static void
22201 free_filedata (Filedata *filedata)
22202 {
22203 free (filedata->program_interpreter);
22204 free (filedata->program_headers);
22205 free (filedata->section_headers);
22206 free (filedata->string_table);
22207 free (filedata->dump.dump_sects);
22208 free (filedata->dynamic_strings);
22209 free (filedata->dynamic_symbols);
22210 free (filedata->dynamic_syminfo);
22211 free (filedata->dynamic_section);
22212
22213 while (filedata->symtab_shndx_list != NULL)
22214 {
22215 elf_section_list *next = filedata->symtab_shndx_list->next;
22216 free (filedata->symtab_shndx_list);
22217 filedata->symtab_shndx_list = next;
22218 }
22219
22220 free (filedata->section_headers_groups);
22221
22222 if (filedata->section_groups)
22223 {
22224 size_t i;
22225 struct group_list * g;
22226 struct group_list * next;
22227
22228 for (i = 0; i < filedata->group_count; i++)
22229 {
22230 for (g = filedata->section_groups [i].root; g != NULL; g = next)
22231 {
22232 next = g->next;
22233 free (g);
22234 }
22235 }
22236
22237 free (filedata->section_groups);
22238 }
22239 memset (&filedata->section_headers, 0,
22240 sizeof (Filedata) - offsetof (Filedata, section_headers));
22241 }
22242
22243 static void
22244 close_file (Filedata * filedata)
22245 {
22246 if (filedata)
22247 {
22248 if (filedata->handle)
22249 fclose (filedata->handle);
22250 free (filedata);
22251 }
22252 }
22253
22254 void
22255 close_debug_file (void * data)
22256 {
22257 free_filedata ((Filedata *) data);
22258 close_file ((Filedata *) data);
22259 }
22260
22261 static Filedata *
22262 open_file (const char * pathname, bool is_separate)
22263 {
22264 struct stat statbuf;
22265 Filedata * filedata = NULL;
22266
22267 if (stat (pathname, & statbuf) < 0
22268 || ! S_ISREG (statbuf.st_mode))
22269 goto fail;
22270
22271 filedata = calloc (1, sizeof * filedata);
22272 if (filedata == NULL)
22273 goto fail;
22274
22275 filedata->handle = fopen (pathname, "rb");
22276 if (filedata->handle == NULL)
22277 goto fail;
22278
22279 filedata->file_size = statbuf.st_size;
22280 filedata->file_name = pathname;
22281 filedata->is_separate = is_separate;
22282
22283 if (! get_file_header (filedata))
22284 goto fail;
22285
22286 if (!get_section_headers (filedata, false))
22287 goto fail;
22288
22289 return filedata;
22290
22291 fail:
22292 if (filedata)
22293 {
22294 if (filedata->handle)
22295 fclose (filedata->handle);
22296 free (filedata);
22297 }
22298 return NULL;
22299 }
22300
22301 void *
22302 open_debug_file (const char * pathname)
22303 {
22304 return open_file (pathname, true);
22305 }
22306
22307 static void
22308 initialise_dump_sects (Filedata * filedata)
22309 {
22310 /* Initialise the dump_sects array from the cmdline_dump_sects array.
22311 Note we do this even if cmdline_dump_sects is empty because we
22312 must make sure that the dump_sets array is zeroed out before each
22313 object file is processed. */
22314 if (filedata->dump.num_dump_sects > cmdline.num_dump_sects)
22315 memset (filedata->dump.dump_sects, 0,
22316 filedata->dump.num_dump_sects * sizeof (*filedata->dump.dump_sects));
22317
22318 if (cmdline.num_dump_sects > 0)
22319 {
22320 if (filedata->dump.num_dump_sects == 0)
22321 /* A sneaky way of allocating the dump_sects array. */
22322 request_dump_bynumber (&filedata->dump, cmdline.num_dump_sects, 0);
22323
22324 assert (filedata->dump.num_dump_sects >= cmdline.num_dump_sects);
22325 memcpy (filedata->dump.dump_sects, cmdline.dump_sects,
22326 cmdline.num_dump_sects * sizeof (*filedata->dump.dump_sects));
22327 }
22328 }
22329
22330 static bool
22331 might_need_separate_debug_info (Filedata * filedata)
22332 {
22333 /* Debuginfo files do not need further separate file loading. */
22334 if (filedata->file_header.e_shstrndx == SHN_UNDEF)
22335 return false;
22336
22337 /* Since do_follow_links might be enabled by default, only treat it as an
22338 indication that separate files should be loaded if setting it was a
22339 deliberate user action. */
22340 if (DEFAULT_FOR_FOLLOW_LINKS == 0 && do_follow_links)
22341 return true;
22342
22343 if (process_links || do_syms || do_unwind
22344 || dump_any_debugging || do_dump || do_debugging)
22345 return true;
22346
22347 return false;
22348 }
22349
22350 /* Process one ELF object file according to the command line options.
22351 This file may actually be stored in an archive. The file is
22352 positioned at the start of the ELF object. Returns TRUE if no
22353 problems were encountered, FALSE otherwise. */
22354
22355 static bool
22356 process_object (Filedata * filedata)
22357 {
22358 bool have_separate_files;
22359 unsigned int i;
22360 bool res;
22361
22362 if (! get_file_header (filedata))
22363 {
22364 error (_("%s: Failed to read file header\n"), filedata->file_name);
22365 return false;
22366 }
22367
22368 /* Initialise per file variables. */
22369 for (i = ARRAY_SIZE (filedata->version_info); i--;)
22370 filedata->version_info[i] = 0;
22371
22372 for (i = ARRAY_SIZE (filedata->dynamic_info); i--;)
22373 filedata->dynamic_info[i] = 0;
22374 filedata->dynamic_info_DT_GNU_HASH = 0;
22375 filedata->dynamic_info_DT_MIPS_XHASH = 0;
22376
22377 /* Process the file. */
22378 if (show_name)
22379 printf (_("\nFile: %s\n"), filedata->file_name);
22380
22381 initialise_dump_sects (filedata);
22382
22383 /* There may be some extensions in the first section header. Don't
22384 bomb if we can't read it. */
22385 get_section_headers (filedata, true);
22386
22387 if (! process_file_header (filedata))
22388 {
22389 res = false;
22390 goto out;
22391 }
22392
22393 /* Throw away the single section header read above, so that we
22394 re-read the entire set. */
22395 free (filedata->section_headers);
22396 filedata->section_headers = NULL;
22397
22398 if (! process_section_headers (filedata))
22399 {
22400 /* Without loaded section headers we cannot process lots of things. */
22401 do_unwind = do_version = do_dump = do_arch = false;
22402
22403 if (! do_using_dynamic)
22404 do_syms = do_dyn_syms = do_reloc = false;
22405 }
22406
22407 if (! process_section_groups (filedata))
22408 /* Without loaded section groups we cannot process unwind. */
22409 do_unwind = false;
22410
22411 process_program_headers (filedata);
22412
22413 res = process_dynamic_section (filedata);
22414
22415 if (! process_relocs (filedata))
22416 res = false;
22417
22418 if (! process_unwind (filedata))
22419 res = false;
22420
22421 if (! process_symbol_table (filedata))
22422 res = false;
22423
22424 if (! process_lto_symbol_tables (filedata))
22425 res = false;
22426
22427 if (! process_syminfo (filedata))
22428 res = false;
22429
22430 if (! process_version_sections (filedata))
22431 res = false;
22432
22433 if (might_need_separate_debug_info (filedata))
22434 have_separate_files = load_separate_debug_files (filedata, filedata->file_name);
22435 else
22436 have_separate_files = false;
22437
22438 if (! process_section_contents (filedata))
22439 res = false;
22440
22441 if (have_separate_files)
22442 {
22443 separate_info * d;
22444
22445 for (d = first_separate_info; d != NULL; d = d->next)
22446 {
22447 initialise_dump_sects (d->handle);
22448
22449 if (process_links && ! process_file_header (d->handle))
22450 res = false;
22451 else if (! process_section_headers (d->handle))
22452 res = false;
22453 else if (! process_section_contents (d->handle))
22454 res = false;
22455 else if (process_links)
22456 {
22457 if (! process_section_groups (d->handle))
22458 res = false;
22459 process_program_headers (d->handle);
22460 if (! process_dynamic_section (d->handle))
22461 res = false;
22462 if (! process_relocs (d->handle))
22463 res = false;
22464 if (! process_unwind (d->handle))
22465 res = false;
22466 if (! process_symbol_table (d->handle))
22467 res = false;
22468 if (! process_lto_symbol_tables (d->handle))
22469 res = false;
22470 if (! process_syminfo (d->handle))
22471 res = false;
22472 if (! process_version_sections (d->handle))
22473 res = false;
22474 if (! process_notes (d->handle))
22475 res = false;
22476 }
22477 }
22478
22479 /* The file handles are closed by the call to free_debug_memory() below. */
22480 }
22481
22482 if (! process_notes (filedata))
22483 res = false;
22484
22485 if (! process_gnu_liblist (filedata))
22486 res = false;
22487
22488 if (! process_arch_specific (filedata))
22489 res = false;
22490
22491 out:
22492 free_filedata (filedata);
22493
22494 free_debug_memory ();
22495
22496 return res;
22497 }
22498
22499 /* Process an ELF archive.
22500 On entry the file is positioned just after the ARMAG string.
22501 Returns TRUE upon success, FALSE otherwise. */
22502
22503 static bool
22504 process_archive (Filedata * filedata, bool is_thin_archive)
22505 {
22506 struct archive_info arch;
22507 struct archive_info nested_arch;
22508 size_t got;
22509 bool ret = true;
22510
22511 show_name = true;
22512
22513 /* The ARCH structure is used to hold information about this archive. */
22514 arch.file_name = NULL;
22515 arch.file = NULL;
22516 arch.index_array = NULL;
22517 arch.sym_table = NULL;
22518 arch.longnames = NULL;
22519
22520 /* The NESTED_ARCH structure is used as a single-item cache of information
22521 about a nested archive (when members of a thin archive reside within
22522 another regular archive file). */
22523 nested_arch.file_name = NULL;
22524 nested_arch.file = NULL;
22525 nested_arch.index_array = NULL;
22526 nested_arch.sym_table = NULL;
22527 nested_arch.longnames = NULL;
22528
22529 if (setup_archive (&arch, filedata->file_name, filedata->handle,
22530 filedata->file_size, is_thin_archive,
22531 do_archive_index) != 0)
22532 {
22533 ret = false;
22534 goto out;
22535 }
22536
22537 if (do_archive_index)
22538 {
22539 if (arch.sym_table == NULL)
22540 error (_("%s: unable to dump the index as none was found\n"),
22541 filedata->file_name);
22542 else
22543 {
22544 unsigned long i, l;
22545 unsigned long current_pos;
22546
22547 printf (_("Index of archive %s: (%lu entries, 0x%lx bytes "
22548 "in the symbol table)\n"),
22549 filedata->file_name, (unsigned long) arch.index_num,
22550 arch.sym_size);
22551
22552 current_pos = ftell (filedata->handle);
22553
22554 for (i = l = 0; i < arch.index_num; i++)
22555 {
22556 if (i == 0
22557 || (i > 0 && arch.index_array[i] != arch.index_array[i - 1]))
22558 {
22559 char * member_name
22560 = get_archive_member_name_at (&arch, arch.index_array[i],
22561 &nested_arch);
22562
22563 if (member_name != NULL)
22564 {
22565 char * qualified_name
22566 = make_qualified_name (&arch, &nested_arch,
22567 member_name);
22568
22569 if (qualified_name != NULL)
22570 {
22571 printf (_("Contents of binary %s at offset "),
22572 qualified_name);
22573 (void) print_vma (arch.index_array[i], PREFIX_HEX);
22574 putchar ('\n');
22575 free (qualified_name);
22576 }
22577 free (member_name);
22578 }
22579 }
22580
22581 if (l >= arch.sym_size)
22582 {
22583 error (_("%s: end of the symbol table reached "
22584 "before the end of the index\n"),
22585 filedata->file_name);
22586 ret = false;
22587 break;
22588 }
22589 /* PR 17531: file: 0b6630b2. */
22590 printf ("\t%.*s\n",
22591 (int) (arch.sym_size - l), arch.sym_table + l);
22592 l += strnlen (arch.sym_table + l, arch.sym_size - l) + 1;
22593 }
22594
22595 if (arch.uses_64bit_indices)
22596 l = (l + 7) & ~ 7;
22597 else
22598 l += l & 1;
22599
22600 if (l < arch.sym_size)
22601 {
22602 error (ngettext ("%s: %ld byte remains in the symbol table, "
22603 "but without corresponding entries in "
22604 "the index table\n",
22605 "%s: %ld bytes remain in the symbol table, "
22606 "but without corresponding entries in "
22607 "the index table\n",
22608 arch.sym_size - l),
22609 filedata->file_name, arch.sym_size - l);
22610 ret = false;
22611 }
22612
22613 if (fseek (filedata->handle, current_pos, SEEK_SET) != 0)
22614 {
22615 error (_("%s: failed to seek back to start of object files "
22616 "in the archive\n"),
22617 filedata->file_name);
22618 ret = false;
22619 goto out;
22620 }
22621 }
22622
22623 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
22624 && !do_segments && !do_header && !do_dump && !do_version
22625 && !do_histogram && !do_debugging && !do_arch && !do_notes
22626 && !do_section_groups && !do_dyn_syms)
22627 {
22628 ret = true; /* Archive index only. */
22629 goto out;
22630 }
22631 }
22632
22633 while (1)
22634 {
22635 char * name;
22636 size_t namelen;
22637 char * qualified_name;
22638
22639 /* Read the next archive header. */
22640 if (fseek (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0)
22641 {
22642 error (_("%s: failed to seek to next archive header\n"),
22643 arch.file_name);
22644 ret = false;
22645 break;
22646 }
22647 got = fread (&arch.arhdr, 1, sizeof arch.arhdr, filedata->handle);
22648 if (got != sizeof arch.arhdr)
22649 {
22650 if (got == 0)
22651 break;
22652 /* PR 24049 - we cannot use filedata->file_name as this will
22653 have already been freed. */
22654 error (_("%s: failed to read archive header\n"), arch.file_name);
22655
22656 ret = false;
22657 break;
22658 }
22659 if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
22660 {
22661 error (_("%s: did not find a valid archive header\n"),
22662 arch.file_name);
22663 ret = false;
22664 break;
22665 }
22666
22667 arch.next_arhdr_offset += sizeof arch.arhdr;
22668
22669 filedata->archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
22670
22671 name = get_archive_member_name (&arch, &nested_arch);
22672 if (name == NULL)
22673 {
22674 error (_("%s: bad archive file name\n"), arch.file_name);
22675 ret = false;
22676 break;
22677 }
22678 namelen = strlen (name);
22679
22680 qualified_name = make_qualified_name (&arch, &nested_arch, name);
22681 if (qualified_name == NULL)
22682 {
22683 error (_("%s: bad archive file name\n"), arch.file_name);
22684 free (name);
22685 ret = false;
22686 break;
22687 }
22688
22689 if (is_thin_archive && arch.nested_member_origin == 0)
22690 {
22691 /* This is a proxy for an external member of a thin archive. */
22692 Filedata * member_filedata;
22693 char * member_file_name = adjust_relative_path
22694 (filedata->file_name, name, namelen);
22695
22696 free (name);
22697 if (member_file_name == NULL)
22698 {
22699 free (qualified_name);
22700 ret = false;
22701 break;
22702 }
22703
22704 member_filedata = open_file (member_file_name, false);
22705 if (member_filedata == NULL)
22706 {
22707 error (_("Input file '%s' is not readable.\n"), member_file_name);
22708 free (member_file_name);
22709 free (qualified_name);
22710 ret = false;
22711 break;
22712 }
22713
22714 filedata->archive_file_offset = arch.nested_member_origin;
22715 member_filedata->file_name = qualified_name;
22716
22717 /* The call to process_object() expects the file to be at the beginning. */
22718 rewind (member_filedata->handle);
22719
22720 if (! process_object (member_filedata))
22721 ret = false;
22722
22723 close_file (member_filedata);
22724 free (member_file_name);
22725 }
22726 else if (is_thin_archive)
22727 {
22728 Filedata thin_filedata;
22729
22730 memset (&thin_filedata, 0, sizeof (thin_filedata));
22731
22732 /* PR 15140: Allow for corrupt thin archives. */
22733 if (nested_arch.file == NULL)
22734 {
22735 error (_("%s: contains corrupt thin archive: %s\n"),
22736 qualified_name, name);
22737 free (qualified_name);
22738 free (name);
22739 ret = false;
22740 break;
22741 }
22742 free (name);
22743
22744 /* This is a proxy for a member of a nested archive. */
22745 filedata->archive_file_offset
22746 = arch.nested_member_origin + sizeof arch.arhdr;
22747
22748 /* The nested archive file will have been opened and setup by
22749 get_archive_member_name. */
22750 if (fseek (nested_arch.file, filedata->archive_file_offset,
22751 SEEK_SET) != 0)
22752 {
22753 error (_("%s: failed to seek to archive member.\n"),
22754 nested_arch.file_name);
22755 free (qualified_name);
22756 ret = false;
22757 break;
22758 }
22759
22760 thin_filedata.handle = nested_arch.file;
22761 thin_filedata.file_name = qualified_name;
22762
22763 if (! process_object (& thin_filedata))
22764 ret = false;
22765 }
22766 else
22767 {
22768 free (name);
22769 filedata->archive_file_offset = arch.next_arhdr_offset;
22770 filedata->file_name = qualified_name;
22771 if (! process_object (filedata))
22772 ret = false;
22773 arch.next_arhdr_offset += (filedata->archive_file_size + 1) & -2;
22774 /* Stop looping with "negative" archive_file_size. */
22775 if (arch.next_arhdr_offset < filedata->archive_file_size)
22776 arch.next_arhdr_offset = -1ul;
22777 }
22778
22779 free (qualified_name);
22780 }
22781
22782 out:
22783 if (nested_arch.file != NULL)
22784 fclose (nested_arch.file);
22785 release_archive (&nested_arch);
22786 release_archive (&arch);
22787
22788 return ret;
22789 }
22790
22791 static bool
22792 process_file (char * file_name)
22793 {
22794 Filedata * filedata = NULL;
22795 struct stat statbuf;
22796 char armag[SARMAG];
22797 bool ret = true;
22798
22799 if (stat (file_name, &statbuf) < 0)
22800 {
22801 if (errno == ENOENT)
22802 error (_("'%s': No such file\n"), file_name);
22803 else
22804 error (_("Could not locate '%s'. System error message: %s\n"),
22805 file_name, strerror (errno));
22806 return false;
22807 }
22808
22809 if (! S_ISREG (statbuf.st_mode))
22810 {
22811 error (_("'%s' is not an ordinary file\n"), file_name);
22812 return false;
22813 }
22814
22815 filedata = calloc (1, sizeof * filedata);
22816 if (filedata == NULL)
22817 {
22818 error (_("Out of memory allocating file data structure\n"));
22819 return false;
22820 }
22821
22822 filedata->file_name = file_name;
22823 filedata->handle = fopen (file_name, "rb");
22824 if (filedata->handle == NULL)
22825 {
22826 error (_("Input file '%s' is not readable.\n"), file_name);
22827 free (filedata);
22828 return false;
22829 }
22830
22831 if (fread (armag, SARMAG, 1, filedata->handle) != 1)
22832 {
22833 error (_("%s: Failed to read file's magic number\n"), file_name);
22834 fclose (filedata->handle);
22835 free (filedata);
22836 return false;
22837 }
22838
22839 filedata->file_size = statbuf.st_size;
22840 filedata->is_separate = false;
22841
22842 if (memcmp (armag, ARMAG, SARMAG) == 0)
22843 {
22844 if (! process_archive (filedata, false))
22845 ret = false;
22846 }
22847 else if (memcmp (armag, ARMAGT, SARMAG) == 0)
22848 {
22849 if ( ! process_archive (filedata, true))
22850 ret = false;
22851 }
22852 else
22853 {
22854 if (do_archive_index && !check_all)
22855 error (_("File %s is not an archive so its index cannot be displayed.\n"),
22856 file_name);
22857
22858 rewind (filedata->handle);
22859 filedata->archive_file_size = filedata->archive_file_offset = 0;
22860
22861 if (! process_object (filedata))
22862 ret = false;
22863 }
22864
22865 fclose (filedata->handle);
22866 free (filedata->section_headers);
22867 free (filedata->program_headers);
22868 free (filedata->string_table);
22869 free (filedata->dump.dump_sects);
22870 free (filedata);
22871
22872 free (ba_cache.strtab);
22873 ba_cache.strtab = NULL;
22874 free (ba_cache.symtab);
22875 ba_cache.symtab = NULL;
22876 ba_cache.filedata = NULL;
22877
22878 return ret;
22879 }
22880
22881 #ifdef SUPPORT_DISASSEMBLY
22882 /* Needed by the i386 disassembler. For extra credit, someone could
22883 fix this so that we insert symbolic addresses here, esp for GOT/PLT
22884 symbols. */
22885
22886 void
22887 print_address (unsigned int addr, FILE * outfile)
22888 {
22889 fprintf (outfile,"0x%8.8x", addr);
22890 }
22891
22892 /* Needed by the i386 disassembler. */
22893
22894 void
22895 db_task_printsym (unsigned int addr)
22896 {
22897 print_address (addr, stderr);
22898 }
22899 #endif
22900
22901 int
22902 main (int argc, char ** argv)
22903 {
22904 int err;
22905
22906 #ifdef HAVE_LC_MESSAGES
22907 setlocale (LC_MESSAGES, "");
22908 #endif
22909 setlocale (LC_CTYPE, "");
22910 bindtextdomain (PACKAGE, LOCALEDIR);
22911 textdomain (PACKAGE);
22912
22913 expandargv (&argc, &argv);
22914
22915 parse_args (& cmdline, argc, argv);
22916
22917 if (optind < (argc - 1))
22918 /* When displaying information for more than one file,
22919 prefix the information with the file name. */
22920 show_name = true;
22921 else if (optind >= argc)
22922 {
22923 /* Ensure that the warning is always displayed. */
22924 do_checks = true;
22925
22926 warn (_("Nothing to do.\n"));
22927 usage (stderr);
22928 }
22929
22930 err = false;
22931 while (optind < argc)
22932 if (! process_file (argv[optind++]))
22933 err = true;
22934
22935 free (cmdline.dump_sects);
22936
22937 free (dump_ctf_symtab_name);
22938 free (dump_ctf_strtab_name);
22939 free (dump_ctf_parent_name);
22940
22941 return err ? EXIT_FAILURE : EXIT_SUCCESS;
22942 }