]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - binutils/readelf.c
Re: readelf: use fseeko64 or fseeko if possible
[thirdparty/binutils-gdb.git] / binutils / readelf.c
1 /* readelf.c -- display contents of an ELF format file
2 Copyright (C) 1998-2022 Free Software Foundation, Inc.
3
4 Originally developed by Eric Youngdale <eric@andante.jic.com>
5 Modifications by Nick Clifton <nickc@redhat.com>
6
7 This file is part of GNU Binutils.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
22 02110-1301, USA. */
23 \f
24 /* The difference between readelf and objdump:
25
26 Both programs are capable of displaying the contents of ELF format files,
27 so why does the binutils project have two file dumpers ?
28
29 The reason is that objdump sees an ELF file through a BFD filter of the
30 world; if BFD has a bug where, say, it disagrees about a machine constant
31 in e_flags, then the odds are good that it will remain internally
32 consistent. The linker sees it the BFD way, objdump sees it the BFD way,
33 GAS sees it the BFD way. There was need for a tool to go find out what
34 the file actually says.
35
36 This is why the readelf program does not link against the BFD library - it
37 exists as an independent program to help verify the correct working of BFD.
38
39 There is also the case that readelf can provide more information about an
40 ELF file than is provided by objdump. In particular it can display DWARF
41 debugging information which (at the moment) objdump cannot. */
42 \f
43 #include "sysdep.h"
44 #include <assert.h>
45 #include <time.h>
46 #include <zlib.h>
47 #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 int
369 fseek64 (FILE *stream, int64_t offset, int whence)
370 {
371 #if defined (HAVE_FSEEKO64)
372 off64_t o = offset;
373 if (o != offset)
374 {
375 errno = EINVAL;
376 return -1;
377 }
378 return fseeko64 (stream, o, whence);
379 #elif defined (HAVE_FSEEKO)
380 off_t o = offset;
381 if (o != offset)
382 {
383 errno = EINVAL;
384 return -1;
385 }
386 return fseeko (stream, o, whence);
387 #else
388 long o = offset;
389 if (o != offset)
390 {
391 errno = EINVAL;
392 return -1;
393 }
394 return fseek (stream, o, whence);
395 #endif
396 }
397
398 static const char * get_symbol_version_string
399 (Filedata *, bool, const char *, unsigned long, unsigned,
400 Elf_Internal_Sym *, enum versioned_symbol_info *, unsigned short *);
401
402 #define UNKNOWN -1
403
404 static inline const char *
405 section_name (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
406 {
407 return filedata->string_table + hdr->sh_name;
408 }
409
410 static inline bool
411 section_name_valid (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
412 {
413 return (hdr != NULL
414 && filedata->string_table != NULL
415 && hdr->sh_name < filedata->string_table_length);
416 }
417
418 static inline const char *
419 section_name_print (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
420 {
421 if (hdr == NULL)
422 return _("<none>");
423 if (filedata->string_table == NULL)
424 return _("<no-strings>");
425 if (hdr->sh_name >= filedata->string_table_length)
426 return _("<corrupt>");
427 return section_name (filedata, hdr);
428 }
429
430 #define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */
431
432 static inline bool
433 valid_symbol_name (const char *strtab, size_t strtab_size, uint64_t offset)
434 {
435 return strtab != NULL && offset < strtab_size;
436 }
437
438 static inline bool
439 valid_dynamic_name (const Filedata *filedata, uint64_t offset)
440 {
441 return valid_symbol_name (filedata->dynamic_strings,
442 filedata->dynamic_strings_length, offset);
443 }
444
445 /* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
446 already been called and verified that the string exists. */
447 static inline const char *
448 get_dynamic_name (const Filedata *filedata, size_t offset)
449 {
450 return filedata->dynamic_strings + offset;
451 }
452
453 #define REMOVE_ARCH_BITS(ADDR) \
454 do \
455 { \
456 if (filedata->file_header.e_machine == EM_ARM) \
457 (ADDR) &= ~1; \
458 } \
459 while (0)
460
461 /* Get the correct GNU hash section name. */
462 #define GNU_HASH_SECTION_NAME(filedata) \
463 filedata->dynamic_info_DT_MIPS_XHASH ? ".MIPS.xhash" : ".gnu.hash"
464 \f
465 /* Retrieve NMEMB structures, each SIZE bytes long from FILEDATA starting at
466 OFFSET + the offset of the current archive member, if we are examining an
467 archive. Put the retrieved data into VAR, if it is not NULL. Otherwise
468 allocate a buffer using malloc and fill that. In either case return the
469 pointer to the start of the retrieved data or NULL if something went wrong.
470 If something does go wrong and REASON is not NULL then emit an error
471 message using REASON as part of the context. */
472
473 static void *
474 get_data (void *var,
475 Filedata *filedata,
476 unsigned long offset,
477 uint64_t size,
478 uint64_t nmemb,
479 const char *reason)
480 {
481 void * mvar;
482 uint64_t amt = size * nmemb;
483
484 if (size == 0 || nmemb == 0)
485 return NULL;
486
487 /* If size_t is smaller than uint64_t, eg because you are building
488 on a 32-bit host, then make sure that when the sizes are cast to
489 size_t no information is lost. */
490 if ((size_t) size != size
491 || (size_t) nmemb != nmemb
492 || (size_t) amt != amt
493 || amt / size != nmemb
494 || (size_t) amt + 1 == 0)
495 {
496 if (reason)
497 error (_("Size overflow prevents reading %" PRIu64
498 " elements of size %" PRIu64 " for %s\n"),
499 nmemb, size, reason);
500 return NULL;
501 }
502
503 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
504 attempting to allocate memory when the read is bound to fail. */
505 if (filedata->archive_file_offset > filedata->file_size
506 || offset > filedata->file_size - filedata->archive_file_offset
507 || amt > filedata->file_size - filedata->archive_file_offset - offset)
508 {
509 if (reason)
510 error (_("Reading %" PRIu64 " bytes extends past end of file for %s\n"),
511 amt, reason);
512 return NULL;
513 }
514
515 if (fseek64 (filedata->handle, filedata->archive_file_offset + offset,
516 SEEK_SET))
517 {
518 if (reason)
519 error (_("Unable to seek to 0x%lx for %s\n"),
520 filedata->archive_file_offset + offset, reason);
521 return NULL;
522 }
523
524 mvar = var;
525 if (mvar == NULL)
526 {
527 /* + 1 so that we can '\0' terminate invalid string table sections. */
528 mvar = malloc ((size_t) amt + 1);
529
530 if (mvar == NULL)
531 {
532 if (reason)
533 error (_("Out of memory allocating %" PRIu64 " bytes for %s\n"),
534 amt, reason);
535 return NULL;
536 }
537
538 ((char *) mvar)[amt] = '\0';
539 }
540
541 if (fread (mvar, (size_t) size, (size_t) nmemb, filedata->handle) != nmemb)
542 {
543 if (reason)
544 error (_("Unable to read in %" PRIu64 " bytes of %s\n"),
545 amt, reason);
546 if (mvar != var)
547 free (mvar);
548 return NULL;
549 }
550
551 return mvar;
552 }
553
554 /* Print a VMA value in the MODE specified.
555 Returns the number of characters displayed. */
556
557 static unsigned int
558 print_vma (uint64_t vma, print_mode mode)
559 {
560 unsigned int nc = 0;
561
562 switch (mode)
563 {
564 case FULL_HEX:
565 nc = printf ("0x");
566 /* Fall through. */
567 case LONG_HEX:
568 if (!is_32bit_elf)
569 return nc + printf ("%16.16" PRIx64, vma);
570 return nc + printf ("%8.8" PRIx64, vma);
571
572 case DEC_5:
573 if (vma <= 99999)
574 return printf ("%5" PRId64, vma);
575 /* Fall through. */
576 case PREFIX_HEX:
577 nc = printf ("0x");
578 /* Fall through. */
579 case HEX:
580 return nc + printf ("%" PRIx64, vma);
581
582 case PREFIX_HEX_5:
583 nc = printf ("0x");
584 /* Fall through. */
585 case HEX_5:
586 return nc + printf ("%05" PRIx64, vma);
587
588 case DEC:
589 return printf ("%" PRId64, vma);
590
591 case UNSIGNED:
592 return printf ("%" PRIu64, vma);
593
594 case UNSIGNED_5:
595 return printf ("%5" PRIu64, vma);
596
597 case OCTAL:
598 return printf ("%" PRIo64, vma);
599
600 case OCTAL_5:
601 return printf ("%5" PRIo64, vma);
602
603 default:
604 /* FIXME: Report unrecognised mode ? */
605 return 0;
606 }
607 }
608
609
610 /* Display a symbol on stdout. Handles the display of control characters and
611 multibye characters (assuming the host environment supports them).
612
613 Display at most abs(WIDTH) characters, truncating as necessary, unless do_wide is true.
614
615 If truncation will happen and do_not_show_symbol_truncation is FALSE then display
616 abs(WIDTH) - 5 characters followed by "[...]".
617
618 If WIDTH is negative then ensure that the output is at least (- WIDTH) characters,
619 padding as necessary.
620
621 Returns the number of emitted characters. */
622
623 static unsigned int
624 print_symbol (signed int width, const char * symbol)
625 {
626 bool extra_padding = false;
627 bool do_dots = false;
628 signed int num_printed = 0;
629 #ifdef HAVE_MBSTATE_T
630 mbstate_t state;
631 #endif
632 unsigned int width_remaining;
633 const void * alloced_symbol = NULL;
634
635 if (width < 0)
636 {
637 /* Keep the width positive. This helps the code below. */
638 width = - width;
639 extra_padding = true;
640 }
641 else if (width == 0)
642 return 0;
643
644 if (do_wide)
645 /* Set the remaining width to a very large value.
646 This simplifies the code below. */
647 width_remaining = INT_MAX;
648 else
649 {
650 width_remaining = width;
651 if (! do_not_show_symbol_truncation
652 && (int) strlen (symbol) > width)
653 {
654 width_remaining -= 5;
655 if ((int) width_remaining < 0)
656 width_remaining = 0;
657 do_dots = true;
658 }
659 }
660
661 #ifdef HAVE_MBSTATE_T
662 /* Initialise the multibyte conversion state. */
663 memset (& state, 0, sizeof (state));
664 #endif
665
666 if (do_demangle && *symbol)
667 {
668 const char * res = cplus_demangle (symbol, demangle_flags);
669
670 if (res != NULL)
671 alloced_symbol = symbol = res;
672 }
673
674 while (width_remaining)
675 {
676 size_t n;
677 const char c = *symbol++;
678
679 if (c == 0)
680 break;
681
682 if (ISPRINT (c))
683 {
684 putchar (c);
685 width_remaining --;
686 num_printed ++;
687 }
688 else if (ISCNTRL (c))
689 {
690 /* Do not print control characters directly as they can affect terminal
691 settings. Such characters usually appear in the names generated
692 by the assembler for local labels. */
693
694 if (width_remaining < 2)
695 break;
696
697 printf ("^%c", c + 0x40);
698 width_remaining -= 2;
699 num_printed += 2;
700 }
701 else if (c == 0x7f)
702 {
703 if (width_remaining < 5)
704 break;
705 printf ("<DEL>");
706 width_remaining -= 5;
707 num_printed += 5;
708 }
709 else if (unicode_display != unicode_locale
710 && unicode_display != unicode_default)
711 {
712 /* Display unicode characters as something else. */
713 unsigned char bytes[4];
714 bool is_utf8;
715 unsigned int nbytes;
716
717 bytes[0] = c;
718
719 if (bytes[0] < 0xc0)
720 {
721 nbytes = 1;
722 is_utf8 = false;
723 }
724 else
725 {
726 bytes[1] = *symbol++;
727
728 if ((bytes[1] & 0xc0) != 0x80)
729 {
730 is_utf8 = false;
731 /* Do not consume this character. It may only
732 be the first byte in the sequence that was
733 corrupt. */
734 --symbol;
735 nbytes = 1;
736 }
737 else if ((bytes[0] & 0x20) == 0)
738 {
739 is_utf8 = true;
740 nbytes = 2;
741 }
742 else
743 {
744 bytes[2] = *symbol++;
745
746 if ((bytes[2] & 0xc0) != 0x80)
747 {
748 is_utf8 = false;
749 symbol -= 2;
750 nbytes = 1;
751 }
752 else if ((bytes[0] & 0x10) == 0)
753 {
754 is_utf8 = true;
755 nbytes = 3;
756 }
757 else
758 {
759 bytes[3] = *symbol++;
760
761 nbytes = 4;
762
763 if ((bytes[3] & 0xc0) != 0x80)
764 {
765 is_utf8 = false;
766 symbol -= 3;
767 nbytes = 1;
768 }
769 else
770 is_utf8 = true;
771 }
772 }
773 }
774
775 if (unicode_display == unicode_invalid)
776 is_utf8 = false;
777
778 if (unicode_display == unicode_hex || ! is_utf8)
779 {
780 unsigned int i;
781
782 if (width_remaining < (nbytes * 2) + 2)
783 break;
784
785 putchar (is_utf8 ? '<' : '{');
786 printf ("0x");
787 for (i = 0; i < nbytes; i++)
788 printf ("%02x", bytes[i]);
789 putchar (is_utf8 ? '>' : '}');
790 }
791 else
792 {
793 if (unicode_display == unicode_highlight && isatty (1))
794 printf ("\x1B[31;47m"); /* Red. */
795
796 switch (nbytes)
797 {
798 case 2:
799 if (width_remaining < 6)
800 break;
801 printf ("\\u%02x%02x",
802 (bytes[0] & 0x1c) >> 2,
803 ((bytes[0] & 0x03) << 6) | (bytes[1] & 0x3f));
804 break;
805 case 3:
806 if (width_remaining < 6)
807 break;
808 printf ("\\u%02x%02x",
809 ((bytes[0] & 0x0f) << 4) | ((bytes[1] & 0x3c) >> 2),
810 ((bytes[1] & 0x03) << 6) | (bytes[2] & 0x3f));
811 break;
812 case 4:
813 if (width_remaining < 8)
814 break;
815 printf ("\\u%02x%02x%02x",
816 ((bytes[0] & 0x07) << 6) | ((bytes[1] & 0x3c) >> 2),
817 ((bytes[1] & 0x03) << 6) | ((bytes[2] & 0x3c) >> 2),
818 ((bytes[2] & 0x03) << 6) | (bytes[3] & 0x3f));
819
820 break;
821 default:
822 /* URG. */
823 break;
824 }
825
826 if (unicode_display == unicode_highlight && isatty (1))
827 printf ("\033[0m"); /* Default colour. */
828 }
829
830 if (bytes[nbytes - 1] == 0)
831 break;
832 }
833 else
834 {
835 #ifdef HAVE_MBSTATE_T
836 wchar_t w;
837 #endif
838 /* Let printf do the hard work of displaying multibyte characters. */
839 printf ("%.1s", symbol - 1);
840 width_remaining --;
841 num_printed ++;
842
843 #ifdef HAVE_MBSTATE_T
844 /* Try to find out how many bytes made up the character that was
845 just printed. Advance the symbol pointer past the bytes that
846 were displayed. */
847 n = mbrtowc (& w, symbol - 1, MB_CUR_MAX, & state);
848 #else
849 n = 1;
850 #endif
851 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
852 symbol += (n - 1);
853 }
854 }
855
856 if (do_dots)
857 num_printed += printf ("[...]");
858
859 if (extra_padding && num_printed < width)
860 {
861 /* Fill in the remaining spaces. */
862 printf ("%-*s", width - num_printed, " ");
863 num_printed = width;
864 }
865
866 free ((void *) alloced_symbol);
867 return num_printed;
868 }
869
870 /* Returns a pointer to a static buffer containing a printable version of
871 the given section's name. Like print_symbol, except that it does not try
872 to print multibyte characters, it just interprets them as hex values. */
873
874 static const char *
875 printable_section_name (Filedata * filedata, const Elf_Internal_Shdr * sec)
876 {
877 #define MAX_PRINT_SEC_NAME_LEN 256
878 static char sec_name_buf [MAX_PRINT_SEC_NAME_LEN + 1];
879 const char * name = section_name_print (filedata, sec);
880 char * buf = sec_name_buf;
881 char c;
882 unsigned int remaining = MAX_PRINT_SEC_NAME_LEN;
883
884 while ((c = * name ++) != 0)
885 {
886 if (ISCNTRL (c))
887 {
888 if (remaining < 2)
889 break;
890
891 * buf ++ = '^';
892 * buf ++ = c + 0x40;
893 remaining -= 2;
894 }
895 else if (ISPRINT (c))
896 {
897 * buf ++ = c;
898 remaining -= 1;
899 }
900 else
901 {
902 static char hex[17] = "0123456789ABCDEF";
903
904 if (remaining < 4)
905 break;
906 * buf ++ = '<';
907 * buf ++ = hex[(c & 0xf0) >> 4];
908 * buf ++ = hex[c & 0x0f];
909 * buf ++ = '>';
910 remaining -= 4;
911 }
912
913 if (remaining == 0)
914 break;
915 }
916
917 * buf = 0;
918 return sec_name_buf;
919 }
920
921 static const char *
922 printable_section_name_from_index (Filedata * filedata, unsigned long ndx)
923 {
924 if (ndx >= filedata->file_header.e_shnum)
925 return _("<corrupt>");
926
927 return printable_section_name (filedata, filedata->section_headers + ndx);
928 }
929
930 /* Return a pointer to section NAME, or NULL if no such section exists. */
931
932 static Elf_Internal_Shdr *
933 find_section (Filedata * filedata, const char * name)
934 {
935 unsigned int i;
936
937 if (filedata->section_headers == NULL)
938 return NULL;
939
940 for (i = 0; i < filedata->file_header.e_shnum; i++)
941 if (section_name_valid (filedata, filedata->section_headers + i)
942 && streq (section_name (filedata, filedata->section_headers + i),
943 name))
944 return filedata->section_headers + i;
945
946 return NULL;
947 }
948
949 /* Return a pointer to a section containing ADDR, or NULL if no such
950 section exists. */
951
952 static Elf_Internal_Shdr *
953 find_section_by_address (Filedata * filedata, uint64_t addr)
954 {
955 unsigned int i;
956
957 if (filedata->section_headers == NULL)
958 return NULL;
959
960 for (i = 0; i < filedata->file_header.e_shnum; i++)
961 {
962 Elf_Internal_Shdr *sec = filedata->section_headers + i;
963
964 if (addr >= sec->sh_addr && addr < sec->sh_addr + sec->sh_size)
965 return sec;
966 }
967
968 return NULL;
969 }
970
971 static Elf_Internal_Shdr *
972 find_section_by_type (Filedata * filedata, unsigned int type)
973 {
974 unsigned int i;
975
976 if (filedata->section_headers == NULL)
977 return NULL;
978
979 for (i = 0; i < filedata->file_header.e_shnum; i++)
980 {
981 Elf_Internal_Shdr *sec = filedata->section_headers + i;
982
983 if (sec->sh_type == type)
984 return sec;
985 }
986
987 return NULL;
988 }
989
990 /* Return a pointer to section NAME, or NULL if no such section exists,
991 restricted to the list of sections given in SET. */
992
993 static Elf_Internal_Shdr *
994 find_section_in_set (Filedata * filedata, const char * name, unsigned int * set)
995 {
996 unsigned int i;
997
998 if (filedata->section_headers == NULL)
999 return NULL;
1000
1001 if (set != NULL)
1002 {
1003 while ((i = *set++) > 0)
1004 {
1005 /* See PR 21156 for a reproducer. */
1006 if (i >= filedata->file_header.e_shnum)
1007 continue; /* FIXME: Should we issue an error message ? */
1008
1009 if (section_name_valid (filedata, filedata->section_headers + i)
1010 && streq (section_name (filedata, filedata->section_headers + i),
1011 name))
1012 return filedata->section_headers + i;
1013 }
1014 }
1015
1016 return find_section (filedata, name);
1017 }
1018
1019 /* Return TRUE if the current file is for IA-64 machine and OpenVMS ABI.
1020 This OS has so many departures from the ELF standard that we test it at
1021 many places. */
1022
1023 static inline bool
1024 is_ia64_vms (Filedata * filedata)
1025 {
1026 return filedata->file_header.e_machine == EM_IA_64
1027 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS;
1028 }
1029
1030 /* Guess the relocation size commonly used by the specific machines. */
1031
1032 static bool
1033 guess_is_rela (unsigned int e_machine)
1034 {
1035 switch (e_machine)
1036 {
1037 /* Targets that use REL relocations. */
1038 case EM_386:
1039 case EM_IAMCU:
1040 case EM_960:
1041 case EM_ARM:
1042 case EM_D10V:
1043 case EM_CYGNUS_D10V:
1044 case EM_DLX:
1045 case EM_MIPS:
1046 case EM_MIPS_RS3_LE:
1047 case EM_CYGNUS_M32R:
1048 case EM_SCORE:
1049 case EM_XGATE:
1050 case EM_NFP:
1051 case EM_BPF:
1052 return false;
1053
1054 /* Targets that use RELA relocations. */
1055 case EM_68K:
1056 case EM_860:
1057 case EM_AARCH64:
1058 case EM_ADAPTEVA_EPIPHANY:
1059 case EM_ALPHA:
1060 case EM_ALTERA_NIOS2:
1061 case EM_ARC:
1062 case EM_ARC_COMPACT:
1063 case EM_ARC_COMPACT2:
1064 case EM_AVR:
1065 case EM_AVR_OLD:
1066 case EM_BLACKFIN:
1067 case EM_CR16:
1068 case EM_CRIS:
1069 case EM_CRX:
1070 case EM_CSKY:
1071 case EM_D30V:
1072 case EM_CYGNUS_D30V:
1073 case EM_FR30:
1074 case EM_FT32:
1075 case EM_CYGNUS_FR30:
1076 case EM_CYGNUS_FRV:
1077 case EM_H8S:
1078 case EM_H8_300:
1079 case EM_H8_300H:
1080 case EM_IA_64:
1081 case EM_IP2K:
1082 case EM_IP2K_OLD:
1083 case EM_IQ2000:
1084 case EM_LATTICEMICO32:
1085 case EM_M32C_OLD:
1086 case EM_M32C:
1087 case EM_M32R:
1088 case EM_MCORE:
1089 case EM_CYGNUS_MEP:
1090 case EM_METAG:
1091 case EM_MMIX:
1092 case EM_MN10200:
1093 case EM_CYGNUS_MN10200:
1094 case EM_MN10300:
1095 case EM_CYGNUS_MN10300:
1096 case EM_MOXIE:
1097 case EM_MSP430:
1098 case EM_MSP430_OLD:
1099 case EM_MT:
1100 case EM_NDS32:
1101 case EM_NIOS32:
1102 case EM_OR1K:
1103 case EM_PPC64:
1104 case EM_PPC:
1105 case EM_TI_PRU:
1106 case EM_RISCV:
1107 case EM_RL78:
1108 case EM_RX:
1109 case EM_S390:
1110 case EM_S390_OLD:
1111 case EM_SH:
1112 case EM_SPARC:
1113 case EM_SPARC32PLUS:
1114 case EM_SPARCV9:
1115 case EM_SPU:
1116 case EM_TI_C6000:
1117 case EM_TILEGX:
1118 case EM_TILEPRO:
1119 case EM_V800:
1120 case EM_V850:
1121 case EM_CYGNUS_V850:
1122 case EM_VAX:
1123 case EM_VISIUM:
1124 case EM_X86_64:
1125 case EM_L1OM:
1126 case EM_K1OM:
1127 case EM_XSTORMY16:
1128 case EM_XTENSA:
1129 case EM_XTENSA_OLD:
1130 case EM_MICROBLAZE:
1131 case EM_MICROBLAZE_OLD:
1132 case EM_WEBASSEMBLY:
1133 return true;
1134
1135 case EM_68HC05:
1136 case EM_68HC08:
1137 case EM_68HC11:
1138 case EM_68HC16:
1139 case EM_FX66:
1140 case EM_ME16:
1141 case EM_MMA:
1142 case EM_NCPU:
1143 case EM_NDR1:
1144 case EM_PCP:
1145 case EM_ST100:
1146 case EM_ST19:
1147 case EM_ST7:
1148 case EM_ST9PLUS:
1149 case EM_STARCORE:
1150 case EM_SVX:
1151 case EM_TINYJ:
1152 default:
1153 warn (_("Don't know about relocations on this machine architecture\n"));
1154 return false;
1155 }
1156 }
1157
1158 /* Load RELA type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1159 Returns TRUE upon success, FALSE otherwise. If successful then a
1160 pointer to a malloc'ed buffer containing the relocs is placed in *RELASP,
1161 and the number of relocs loaded is placed in *NRELASP. It is the caller's
1162 responsibility to free the allocated buffer. */
1163
1164 static bool
1165 slurp_rela_relocs (Filedata * filedata,
1166 unsigned long rel_offset,
1167 unsigned long rel_size,
1168 Elf_Internal_Rela ** relasp,
1169 unsigned long * nrelasp)
1170 {
1171 Elf_Internal_Rela * relas;
1172 size_t nrelas;
1173 unsigned int i;
1174
1175 if (is_32bit_elf)
1176 {
1177 Elf32_External_Rela * erelas;
1178
1179 erelas = (Elf32_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1180 rel_size, _("32-bit relocation data"));
1181 if (!erelas)
1182 return false;
1183
1184 nrelas = rel_size / sizeof (Elf32_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
1203 free (erelas);
1204 }
1205 else
1206 {
1207 Elf64_External_Rela * erelas;
1208
1209 erelas = (Elf64_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1210 rel_size, _("64-bit relocation data"));
1211 if (!erelas)
1212 return false;
1213
1214 nrelas = rel_size / sizeof (Elf64_External_Rela);
1215
1216 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1217 sizeof (Elf_Internal_Rela));
1218
1219 if (relas == NULL)
1220 {
1221 free (erelas);
1222 error (_("out of memory parsing relocs\n"));
1223 return false;
1224 }
1225
1226 for (i = 0; i < nrelas; i++)
1227 {
1228 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1229 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1230 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1231
1232 if (filedata->file_header.e_machine == EM_MIPS
1233 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1234 {
1235 /* In little-endian objects, r_info isn't really a
1236 64-bit little-endian value: it has a 32-bit
1237 little-endian symbol index followed by four
1238 individual byte fields. Reorder INFO
1239 accordingly. */
1240 uint64_t inf = relas[i].r_info;
1241 inf = (((inf & 0xffffffff) << 32)
1242 | ((inf >> 56) & 0xff)
1243 | ((inf >> 40) & 0xff00)
1244 | ((inf >> 24) & 0xff0000)
1245 | ((inf >> 8) & 0xff000000));
1246 relas[i].r_info = inf;
1247 }
1248 }
1249
1250 free (erelas);
1251 }
1252
1253 *relasp = relas;
1254 *nrelasp = nrelas;
1255 return true;
1256 }
1257
1258 /* Load REL type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1259 Returns TRUE upon success, FALSE otherwise. If successful then a
1260 pointer to a malloc'ed buffer containing the relocs is placed in *RELSP,
1261 and the number of relocs loaded is placed in *NRELSP. It is the caller's
1262 responsibility to free the allocated buffer. */
1263
1264 static bool
1265 slurp_rel_relocs (Filedata * filedata,
1266 unsigned long rel_offset,
1267 unsigned long rel_size,
1268 Elf_Internal_Rela ** relsp,
1269 unsigned long * nrelsp)
1270 {
1271 Elf_Internal_Rela * rels;
1272 size_t nrels;
1273 unsigned int i;
1274
1275 if (is_32bit_elf)
1276 {
1277 Elf32_External_Rel * erels;
1278
1279 erels = (Elf32_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1280 rel_size, _("32-bit relocation data"));
1281 if (!erels)
1282 return false;
1283
1284 nrels = rel_size / sizeof (Elf32_External_Rel);
1285
1286 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1287
1288 if (rels == NULL)
1289 {
1290 free (erels);
1291 error (_("out of memory parsing relocs\n"));
1292 return false;
1293 }
1294
1295 for (i = 0; i < nrels; i++)
1296 {
1297 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1298 rels[i].r_info = BYTE_GET (erels[i].r_info);
1299 rels[i].r_addend = 0;
1300 }
1301
1302 free (erels);
1303 }
1304 else
1305 {
1306 Elf64_External_Rel * erels;
1307
1308 erels = (Elf64_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1309 rel_size, _("64-bit relocation data"));
1310 if (!erels)
1311 return false;
1312
1313 nrels = rel_size / sizeof (Elf64_External_Rel);
1314
1315 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1316
1317 if (rels == NULL)
1318 {
1319 free (erels);
1320 error (_("out of memory parsing relocs\n"));
1321 return false;
1322 }
1323
1324 for (i = 0; i < nrels; i++)
1325 {
1326 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1327 rels[i].r_info = BYTE_GET (erels[i].r_info);
1328 rels[i].r_addend = 0;
1329
1330 if (filedata->file_header.e_machine == EM_MIPS
1331 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1332 {
1333 /* In little-endian objects, r_info isn't really a
1334 64-bit little-endian value: it has a 32-bit
1335 little-endian symbol index followed by four
1336 individual byte fields. Reorder INFO
1337 accordingly. */
1338 uint64_t inf = rels[i].r_info;
1339 inf = (((inf & 0xffffffff) << 32)
1340 | ((inf >> 56) & 0xff)
1341 | ((inf >> 40) & 0xff00)
1342 | ((inf >> 24) & 0xff0000)
1343 | ((inf >> 8) & 0xff000000));
1344 rels[i].r_info = inf;
1345 }
1346 }
1347
1348 free (erels);
1349 }
1350
1351 *relsp = rels;
1352 *nrelsp = nrels;
1353 return true;
1354 }
1355
1356 static bool
1357 slurp_relr_relocs (Filedata * filedata,
1358 unsigned long relr_offset,
1359 unsigned long relr_size,
1360 uint64_t ** relrsp,
1361 unsigned long * nrelrsp)
1362 {
1363 void *relrs;
1364 size_t size = 0, nentries, i;
1365 uint64_t base = 0, addr, entry;
1366
1367 relrs = get_data (NULL, filedata, relr_offset, 1, relr_size,
1368 _("RELR relocation data"));
1369 if (!relrs)
1370 return false;
1371
1372 if (is_32bit_elf)
1373 nentries = relr_size / sizeof (Elf32_External_Relr);
1374 else
1375 nentries = relr_size / sizeof (Elf64_External_Relr);
1376 for (i = 0; i < nentries; i++)
1377 {
1378 if (is_32bit_elf)
1379 entry = BYTE_GET (((Elf32_External_Relr *)relrs)[i].r_data);
1380 else
1381 entry = BYTE_GET (((Elf64_External_Relr *)relrs)[i].r_data);
1382 if ((entry & 1) == 0)
1383 size++;
1384 else
1385 while ((entry >>= 1) != 0)
1386 if ((entry & 1) == 1)
1387 size++;
1388 }
1389
1390 *relrsp = malloc (size * sizeof (**relrsp));
1391 if (*relrsp == NULL)
1392 {
1393 free (relrs);
1394 error (_("out of memory parsing relocs\n"));
1395 return false;
1396 }
1397
1398 size = 0;
1399 for (i = 0; i < nentries; i++)
1400 {
1401 const uint64_t entry_bytes = is_32bit_elf ? 4 : 8;
1402
1403 if (is_32bit_elf)
1404 entry = BYTE_GET (((Elf32_External_Relr *)relrs)[i].r_data);
1405 else
1406 entry = BYTE_GET (((Elf64_External_Relr *)relrs)[i].r_data);
1407 if ((entry & 1) == 0)
1408 {
1409 (*relrsp)[size++] = entry;
1410 base = entry + entry_bytes;
1411 }
1412 else
1413 {
1414 for (addr = base; (entry >>= 1) != 0; addr += entry_bytes)
1415 if ((entry & 1) != 0)
1416 (*relrsp)[size++] = addr;
1417 base += entry_bytes * (entry_bytes * CHAR_BIT - 1);
1418 }
1419 }
1420
1421 *nrelrsp = size;
1422 free (relrs);
1423 return true;
1424 }
1425
1426 /* Returns the reloc type extracted from the reloc info field. */
1427
1428 static unsigned int
1429 get_reloc_type (Filedata * filedata, uint64_t reloc_info)
1430 {
1431 if (is_32bit_elf)
1432 return ELF32_R_TYPE (reloc_info);
1433
1434 switch (filedata->file_header.e_machine)
1435 {
1436 case EM_MIPS:
1437 /* Note: We assume that reloc_info has already been adjusted for us. */
1438 return ELF64_MIPS_R_TYPE (reloc_info);
1439
1440 case EM_SPARCV9:
1441 return ELF64_R_TYPE_ID (reloc_info);
1442
1443 default:
1444 return ELF64_R_TYPE (reloc_info);
1445 }
1446 }
1447
1448 /* Return the symbol index extracted from the reloc info field. */
1449
1450 static uint64_t
1451 get_reloc_symindex (uint64_t reloc_info)
1452 {
1453 return is_32bit_elf ? ELF32_R_SYM (reloc_info) : ELF64_R_SYM (reloc_info);
1454 }
1455
1456 static inline bool
1457 uses_msp430x_relocs (Filedata * filedata)
1458 {
1459 return
1460 filedata->file_header.e_machine == EM_MSP430 /* Paranoia. */
1461 /* GCC uses osabi == ELFOSBI_STANDALONE. */
1462 && (((filedata->file_header.e_flags & EF_MSP430_MACH) == E_MSP430_MACH_MSP430X)
1463 /* TI compiler uses ELFOSABI_NONE. */
1464 || (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_NONE));
1465 }
1466
1467 /* Display the contents of the relocation data found at the specified
1468 offset. */
1469
1470 static bool
1471 dump_relocations (Filedata * filedata,
1472 unsigned long rel_offset,
1473 unsigned long rel_size,
1474 Elf_Internal_Sym * symtab,
1475 unsigned long nsyms,
1476 char * strtab,
1477 unsigned long strtablen,
1478 relocation_type rel_type,
1479 bool is_dynsym)
1480 {
1481 unsigned long i;
1482 Elf_Internal_Rela * rels;
1483 bool res = true;
1484
1485 if (rel_type == reltype_unknown)
1486 rel_type = guess_is_rela (filedata->file_header.e_machine) ? reltype_rela : reltype_rel;
1487
1488 if (rel_type == reltype_rela)
1489 {
1490 if (!slurp_rela_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1491 return false;
1492 }
1493 else if (rel_type == reltype_rel)
1494 {
1495 if (!slurp_rel_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1496 return false;
1497 }
1498 else if (rel_type == reltype_relr)
1499 {
1500 uint64_t * relrs;
1501 const char *format
1502 = is_32bit_elf ? "%08" PRIx64 "\n" : "%016" PRIx64 "\n";
1503
1504 if (!slurp_relr_relocs (filedata, rel_offset, rel_size, &relrs,
1505 &rel_size))
1506 return false;
1507
1508 printf (ngettext (" %lu offset\n", " %lu offsets\n", rel_size),
1509 rel_size);
1510 for (i = 0; i < rel_size; i++)
1511 printf (format, relrs[i]);
1512 free (relrs);
1513 return true;
1514 }
1515
1516 if (is_32bit_elf)
1517 {
1518 if (rel_type == reltype_rela)
1519 {
1520 if (do_wide)
1521 printf (_(" Offset Info Type Sym. Value Symbol's Name + Addend\n"));
1522 else
1523 printf (_(" Offset Info Type Sym.Value Sym. Name + Addend\n"));
1524 }
1525 else
1526 {
1527 if (do_wide)
1528 printf (_(" Offset Info Type Sym. Value Symbol's Name\n"));
1529 else
1530 printf (_(" Offset Info Type Sym.Value Sym. Name\n"));
1531 }
1532 }
1533 else
1534 {
1535 if (rel_type == reltype_rela)
1536 {
1537 if (do_wide)
1538 printf (_(" Offset Info Type Symbol's Value Symbol's Name + Addend\n"));
1539 else
1540 printf (_(" Offset Info Type Sym. Value Sym. Name + Addend\n"));
1541 }
1542 else
1543 {
1544 if (do_wide)
1545 printf (_(" Offset Info Type Symbol's Value Symbol's Name\n"));
1546 else
1547 printf (_(" Offset Info Type Sym. Value Sym. Name\n"));
1548 }
1549 }
1550
1551 for (i = 0; i < rel_size; i++)
1552 {
1553 const char * rtype;
1554 uint64_t offset;
1555 uint64_t inf;
1556 uint64_t symtab_index;
1557 uint64_t type;
1558
1559 offset = rels[i].r_offset;
1560 inf = rels[i].r_info;
1561
1562 type = get_reloc_type (filedata, inf);
1563 symtab_index = get_reloc_symindex (inf);
1564
1565 if (is_32bit_elf)
1566 {
1567 printf ("%8.8lx %8.8lx ",
1568 (unsigned long) offset & 0xffffffff,
1569 (unsigned long) inf & 0xffffffff);
1570 }
1571 else
1572 {
1573 printf (do_wide
1574 ? "%16.16" PRIx64 " %16.16" PRIx64 " "
1575 : "%12.12" PRIx64 " %12.12" PRIx64 " ",
1576 offset, inf);
1577 }
1578
1579 switch (filedata->file_header.e_machine)
1580 {
1581 default:
1582 rtype = NULL;
1583 break;
1584
1585 case EM_AARCH64:
1586 rtype = elf_aarch64_reloc_type (type);
1587 break;
1588
1589 case EM_M32R:
1590 case EM_CYGNUS_M32R:
1591 rtype = elf_m32r_reloc_type (type);
1592 break;
1593
1594 case EM_386:
1595 case EM_IAMCU:
1596 rtype = elf_i386_reloc_type (type);
1597 break;
1598
1599 case EM_68HC11:
1600 case EM_68HC12:
1601 rtype = elf_m68hc11_reloc_type (type);
1602 break;
1603
1604 case EM_S12Z:
1605 rtype = elf_s12z_reloc_type (type);
1606 break;
1607
1608 case EM_68K:
1609 rtype = elf_m68k_reloc_type (type);
1610 break;
1611
1612 case EM_960:
1613 rtype = elf_i960_reloc_type (type);
1614 break;
1615
1616 case EM_AVR:
1617 case EM_AVR_OLD:
1618 rtype = elf_avr_reloc_type (type);
1619 break;
1620
1621 case EM_OLD_SPARCV9:
1622 case EM_SPARC32PLUS:
1623 case EM_SPARCV9:
1624 case EM_SPARC:
1625 rtype = elf_sparc_reloc_type (type);
1626 break;
1627
1628 case EM_SPU:
1629 rtype = elf_spu_reloc_type (type);
1630 break;
1631
1632 case EM_V800:
1633 rtype = v800_reloc_type (type);
1634 break;
1635 case EM_V850:
1636 case EM_CYGNUS_V850:
1637 rtype = v850_reloc_type (type);
1638 break;
1639
1640 case EM_D10V:
1641 case EM_CYGNUS_D10V:
1642 rtype = elf_d10v_reloc_type (type);
1643 break;
1644
1645 case EM_D30V:
1646 case EM_CYGNUS_D30V:
1647 rtype = elf_d30v_reloc_type (type);
1648 break;
1649
1650 case EM_DLX:
1651 rtype = elf_dlx_reloc_type (type);
1652 break;
1653
1654 case EM_SH:
1655 rtype = elf_sh_reloc_type (type);
1656 break;
1657
1658 case EM_MN10300:
1659 case EM_CYGNUS_MN10300:
1660 rtype = elf_mn10300_reloc_type (type);
1661 break;
1662
1663 case EM_MN10200:
1664 case EM_CYGNUS_MN10200:
1665 rtype = elf_mn10200_reloc_type (type);
1666 break;
1667
1668 case EM_FR30:
1669 case EM_CYGNUS_FR30:
1670 rtype = elf_fr30_reloc_type (type);
1671 break;
1672
1673 case EM_CYGNUS_FRV:
1674 rtype = elf_frv_reloc_type (type);
1675 break;
1676
1677 case EM_CSKY:
1678 rtype = elf_csky_reloc_type (type);
1679 break;
1680
1681 case EM_FT32:
1682 rtype = elf_ft32_reloc_type (type);
1683 break;
1684
1685 case EM_MCORE:
1686 rtype = elf_mcore_reloc_type (type);
1687 break;
1688
1689 case EM_MMIX:
1690 rtype = elf_mmix_reloc_type (type);
1691 break;
1692
1693 case EM_MOXIE:
1694 rtype = elf_moxie_reloc_type (type);
1695 break;
1696
1697 case EM_MSP430:
1698 if (uses_msp430x_relocs (filedata))
1699 {
1700 rtype = elf_msp430x_reloc_type (type);
1701 break;
1702 }
1703 /* Fall through. */
1704 case EM_MSP430_OLD:
1705 rtype = elf_msp430_reloc_type (type);
1706 break;
1707
1708 case EM_NDS32:
1709 rtype = elf_nds32_reloc_type (type);
1710 break;
1711
1712 case EM_PPC:
1713 rtype = elf_ppc_reloc_type (type);
1714 break;
1715
1716 case EM_PPC64:
1717 rtype = elf_ppc64_reloc_type (type);
1718 break;
1719
1720 case EM_MIPS:
1721 case EM_MIPS_RS3_LE:
1722 rtype = elf_mips_reloc_type (type);
1723 break;
1724
1725 case EM_RISCV:
1726 rtype = elf_riscv_reloc_type (type);
1727 break;
1728
1729 case EM_ALPHA:
1730 rtype = elf_alpha_reloc_type (type);
1731 break;
1732
1733 case EM_ARM:
1734 rtype = elf_arm_reloc_type (type);
1735 break;
1736
1737 case EM_ARC:
1738 case EM_ARC_COMPACT:
1739 case EM_ARC_COMPACT2:
1740 rtype = elf_arc_reloc_type (type);
1741 break;
1742
1743 case EM_PARISC:
1744 rtype = elf_hppa_reloc_type (type);
1745 break;
1746
1747 case EM_H8_300:
1748 case EM_H8_300H:
1749 case EM_H8S:
1750 rtype = elf_h8_reloc_type (type);
1751 break;
1752
1753 case EM_OR1K:
1754 rtype = elf_or1k_reloc_type (type);
1755 break;
1756
1757 case EM_PJ:
1758 case EM_PJ_OLD:
1759 rtype = elf_pj_reloc_type (type);
1760 break;
1761 case EM_IA_64:
1762 rtype = elf_ia64_reloc_type (type);
1763 break;
1764
1765 case EM_CRIS:
1766 rtype = elf_cris_reloc_type (type);
1767 break;
1768
1769 case EM_860:
1770 rtype = elf_i860_reloc_type (type);
1771 break;
1772
1773 case EM_X86_64:
1774 case EM_L1OM:
1775 case EM_K1OM:
1776 rtype = elf_x86_64_reloc_type (type);
1777 break;
1778
1779 case EM_S370:
1780 rtype = i370_reloc_type (type);
1781 break;
1782
1783 case EM_S390_OLD:
1784 case EM_S390:
1785 rtype = elf_s390_reloc_type (type);
1786 break;
1787
1788 case EM_SCORE:
1789 rtype = elf_score_reloc_type (type);
1790 break;
1791
1792 case EM_XSTORMY16:
1793 rtype = elf_xstormy16_reloc_type (type);
1794 break;
1795
1796 case EM_CRX:
1797 rtype = elf_crx_reloc_type (type);
1798 break;
1799
1800 case EM_VAX:
1801 rtype = elf_vax_reloc_type (type);
1802 break;
1803
1804 case EM_VISIUM:
1805 rtype = elf_visium_reloc_type (type);
1806 break;
1807
1808 case EM_BPF:
1809 rtype = elf_bpf_reloc_type (type);
1810 break;
1811
1812 case EM_ADAPTEVA_EPIPHANY:
1813 rtype = elf_epiphany_reloc_type (type);
1814 break;
1815
1816 case EM_IP2K:
1817 case EM_IP2K_OLD:
1818 rtype = elf_ip2k_reloc_type (type);
1819 break;
1820
1821 case EM_IQ2000:
1822 rtype = elf_iq2000_reloc_type (type);
1823 break;
1824
1825 case EM_XTENSA_OLD:
1826 case EM_XTENSA:
1827 rtype = elf_xtensa_reloc_type (type);
1828 break;
1829
1830 case EM_LATTICEMICO32:
1831 rtype = elf_lm32_reloc_type (type);
1832 break;
1833
1834 case EM_M32C_OLD:
1835 case EM_M32C:
1836 rtype = elf_m32c_reloc_type (type);
1837 break;
1838
1839 case EM_MT:
1840 rtype = elf_mt_reloc_type (type);
1841 break;
1842
1843 case EM_BLACKFIN:
1844 rtype = elf_bfin_reloc_type (type);
1845 break;
1846
1847 case EM_CYGNUS_MEP:
1848 rtype = elf_mep_reloc_type (type);
1849 break;
1850
1851 case EM_CR16:
1852 rtype = elf_cr16_reloc_type (type);
1853 break;
1854
1855 case EM_MICROBLAZE:
1856 case EM_MICROBLAZE_OLD:
1857 rtype = elf_microblaze_reloc_type (type);
1858 break;
1859
1860 case EM_RL78:
1861 rtype = elf_rl78_reloc_type (type);
1862 break;
1863
1864 case EM_RX:
1865 rtype = elf_rx_reloc_type (type);
1866 break;
1867
1868 case EM_METAG:
1869 rtype = elf_metag_reloc_type (type);
1870 break;
1871
1872 case EM_TI_C6000:
1873 rtype = elf_tic6x_reloc_type (type);
1874 break;
1875
1876 case EM_TILEGX:
1877 rtype = elf_tilegx_reloc_type (type);
1878 break;
1879
1880 case EM_TILEPRO:
1881 rtype = elf_tilepro_reloc_type (type);
1882 break;
1883
1884 case EM_WEBASSEMBLY:
1885 rtype = elf_wasm32_reloc_type (type);
1886 break;
1887
1888 case EM_XGATE:
1889 rtype = elf_xgate_reloc_type (type);
1890 break;
1891
1892 case EM_ALTERA_NIOS2:
1893 rtype = elf_nios2_reloc_type (type);
1894 break;
1895
1896 case EM_TI_PRU:
1897 rtype = elf_pru_reloc_type (type);
1898 break;
1899
1900 case EM_NFP:
1901 if (EF_NFP_MACH (filedata->file_header.e_flags) == E_NFP_MACH_3200)
1902 rtype = elf_nfp3200_reloc_type (type);
1903 else
1904 rtype = elf_nfp_reloc_type (type);
1905 break;
1906
1907 case EM_Z80:
1908 rtype = elf_z80_reloc_type (type);
1909 break;
1910
1911 case EM_LOONGARCH:
1912 rtype = elf_loongarch_reloc_type (type);
1913 break;
1914
1915 case EM_AMDGPU:
1916 rtype = elf_amdgpu_reloc_type (type);
1917 break;
1918 }
1919
1920 if (rtype == NULL)
1921 printf (_("unrecognized: %-7lx"), (unsigned long) type & 0xffffffff);
1922 else
1923 printf (do_wide ? "%-22s" : "%-17.17s", rtype);
1924
1925 if (filedata->file_header.e_machine == EM_ALPHA
1926 && rtype != NULL
1927 && streq (rtype, "R_ALPHA_LITUSE")
1928 && rel_type == reltype_rela)
1929 {
1930 switch (rels[i].r_addend)
1931 {
1932 case LITUSE_ALPHA_ADDR: rtype = "ADDR"; break;
1933 case LITUSE_ALPHA_BASE: rtype = "BASE"; break;
1934 case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
1935 case LITUSE_ALPHA_JSR: rtype = "JSR"; break;
1936 case LITUSE_ALPHA_TLSGD: rtype = "TLSGD"; break;
1937 case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
1938 case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
1939 default: rtype = NULL;
1940 }
1941
1942 if (rtype)
1943 printf (" (%s)", rtype);
1944 else
1945 {
1946 putchar (' ');
1947 printf (_("<unknown addend: %lx>"),
1948 (unsigned long) rels[i].r_addend);
1949 res = false;
1950 }
1951 }
1952 else if (symtab_index)
1953 {
1954 if (symtab == NULL || symtab_index >= nsyms)
1955 {
1956 error (_(" bad symbol index: %08lx in reloc\n"),
1957 (unsigned long) symtab_index);
1958 res = false;
1959 }
1960 else
1961 {
1962 Elf_Internal_Sym * psym;
1963 const char * version_string;
1964 enum versioned_symbol_info sym_info;
1965 unsigned short vna_other;
1966
1967 psym = symtab + symtab_index;
1968
1969 version_string
1970 = get_symbol_version_string (filedata, is_dynsym,
1971 strtab, strtablen,
1972 symtab_index,
1973 psym,
1974 &sym_info,
1975 &vna_other);
1976
1977 printf (" ");
1978
1979 if (ELF_ST_TYPE (psym->st_info) == STT_GNU_IFUNC)
1980 {
1981 const char * name;
1982 unsigned int len;
1983 unsigned int width = is_32bit_elf ? 8 : 14;
1984
1985 /* Relocations against GNU_IFUNC symbols do not use the value
1986 of the symbol as the address to relocate against. Instead
1987 they invoke the function named by the symbol and use its
1988 result as the address for relocation.
1989
1990 To indicate this to the user, do not display the value of
1991 the symbol in the "Symbols's Value" field. Instead show
1992 its name followed by () as a hint that the symbol is
1993 invoked. */
1994
1995 if (strtab == NULL
1996 || psym->st_name == 0
1997 || psym->st_name >= strtablen)
1998 name = "??";
1999 else
2000 name = strtab + psym->st_name;
2001
2002 len = print_symbol (width, name);
2003 if (version_string)
2004 printf (sym_info == symbol_public ? "@@%s" : "@%s",
2005 version_string);
2006 printf ("()%-*s", len <= width ? (width + 1) - len : 1, " ");
2007 }
2008 else
2009 {
2010 print_vma (psym->st_value, LONG_HEX);
2011
2012 printf (is_32bit_elf ? " " : " ");
2013 }
2014
2015 if (psym->st_name == 0)
2016 {
2017 const char * sec_name = "<null>";
2018 char name_buf[40];
2019
2020 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
2021 {
2022 if (psym->st_shndx < filedata->file_header.e_shnum
2023 && filedata->section_headers != NULL)
2024 sec_name = section_name_print (filedata,
2025 filedata->section_headers
2026 + psym->st_shndx);
2027 else if (psym->st_shndx == SHN_ABS)
2028 sec_name = "ABS";
2029 else if (psym->st_shndx == SHN_COMMON)
2030 sec_name = "COMMON";
2031 else if ((filedata->file_header.e_machine == EM_MIPS
2032 && psym->st_shndx == SHN_MIPS_SCOMMON)
2033 || (filedata->file_header.e_machine == EM_TI_C6000
2034 && psym->st_shndx == SHN_TIC6X_SCOMMON))
2035 sec_name = "SCOMMON";
2036 else if (filedata->file_header.e_machine == EM_MIPS
2037 && psym->st_shndx == SHN_MIPS_SUNDEFINED)
2038 sec_name = "SUNDEF";
2039 else if ((filedata->file_header.e_machine == EM_X86_64
2040 || filedata->file_header.e_machine == EM_L1OM
2041 || filedata->file_header.e_machine == EM_K1OM)
2042 && psym->st_shndx == SHN_X86_64_LCOMMON)
2043 sec_name = "LARGE_COMMON";
2044 else if (filedata->file_header.e_machine == EM_IA_64
2045 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
2046 && psym->st_shndx == SHN_IA_64_ANSI_COMMON)
2047 sec_name = "ANSI_COM";
2048 else if (is_ia64_vms (filedata)
2049 && psym->st_shndx == SHN_IA_64_VMS_SYMVEC)
2050 sec_name = "VMS_SYMVEC";
2051 else
2052 {
2053 sprintf (name_buf, "<section 0x%x>",
2054 (unsigned int) psym->st_shndx);
2055 sec_name = name_buf;
2056 }
2057 }
2058 print_symbol (22, sec_name);
2059 }
2060 else if (strtab == NULL)
2061 printf (_("<string table index: %3ld>"), psym->st_name);
2062 else if (psym->st_name >= strtablen)
2063 {
2064 error (_("<corrupt string table index: %3ld>\n"),
2065 psym->st_name);
2066 res = false;
2067 }
2068 else
2069 {
2070 print_symbol (22, strtab + psym->st_name);
2071 if (version_string)
2072 printf (sym_info == symbol_public ? "@@%s" : "@%s",
2073 version_string);
2074 }
2075
2076 if (rel_type == reltype_rela)
2077 {
2078 uint64_t off = rels[i].r_addend;
2079
2080 if ((int64_t) off < 0)
2081 printf (" - %" PRIx64, -off);
2082 else
2083 printf (" + %" PRIx64, off);
2084 }
2085 }
2086 }
2087 else if (rel_type == reltype_rela)
2088 {
2089 uint64_t off = rels[i].r_addend;
2090
2091 printf ("%*c", is_32bit_elf ? 12 : 20, ' ');
2092 if ((int64_t) off < 0)
2093 printf ("-%" PRIx64, -off);
2094 else
2095 printf ("%" PRIx64, off);
2096 }
2097
2098 if (filedata->file_header.e_machine == EM_SPARCV9
2099 && rtype != NULL
2100 && streq (rtype, "R_SPARC_OLO10"))
2101 printf (" + %lx", (unsigned long) ELF64_R_TYPE_DATA (inf));
2102
2103 putchar ('\n');
2104
2105 if (! is_32bit_elf && filedata->file_header.e_machine == EM_MIPS)
2106 {
2107 uint64_t type2 = ELF64_MIPS_R_TYPE2 (inf);
2108 uint64_t type3 = ELF64_MIPS_R_TYPE3 (inf);
2109 const char * rtype2 = elf_mips_reloc_type (type2);
2110 const char * rtype3 = elf_mips_reloc_type (type3);
2111
2112 printf (" Type2: ");
2113
2114 if (rtype2 == NULL)
2115 printf (_("unrecognized: %-7lx"),
2116 (unsigned long) type2 & 0xffffffff);
2117 else
2118 printf ("%-17.17s", rtype2);
2119
2120 printf ("\n Type3: ");
2121
2122 if (rtype3 == NULL)
2123 printf (_("unrecognized: %-7lx"),
2124 (unsigned long) type3 & 0xffffffff);
2125 else
2126 printf ("%-17.17s", rtype3);
2127
2128 putchar ('\n');
2129 }
2130 }
2131
2132 free (rels);
2133
2134 return res;
2135 }
2136
2137 static const char *
2138 get_aarch64_dynamic_type (unsigned long type)
2139 {
2140 switch (type)
2141 {
2142 case DT_AARCH64_BTI_PLT: return "AARCH64_BTI_PLT";
2143 case DT_AARCH64_PAC_PLT: return "AARCH64_PAC_PLT";
2144 case DT_AARCH64_VARIANT_PCS: return "AARCH64_VARIANT_PCS";
2145 default:
2146 return NULL;
2147 }
2148 }
2149
2150 static const char *
2151 get_mips_dynamic_type (unsigned long type)
2152 {
2153 switch (type)
2154 {
2155 case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
2156 case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
2157 case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
2158 case DT_MIPS_IVERSION: return "MIPS_IVERSION";
2159 case DT_MIPS_FLAGS: return "MIPS_FLAGS";
2160 case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
2161 case DT_MIPS_MSYM: return "MIPS_MSYM";
2162 case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
2163 case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
2164 case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
2165 case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
2166 case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
2167 case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
2168 case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
2169 case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
2170 case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
2171 case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
2172 case DT_MIPS_RLD_MAP_REL: return "MIPS_RLD_MAP_REL";
2173 case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
2174 case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
2175 case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
2176 case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
2177 case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
2178 case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
2179 case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
2180 case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
2181 case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
2182 case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
2183 case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
2184 case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
2185 case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
2186 case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
2187 case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
2188 case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
2189 case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
2190 case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
2191 case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
2192 case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
2193 case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
2194 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
2195 case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
2196 case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
2197 case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
2198 case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
2199 case DT_MIPS_PLTGOT: return "MIPS_PLTGOT";
2200 case DT_MIPS_RWPLT: return "MIPS_RWPLT";
2201 case DT_MIPS_XHASH: return "MIPS_XHASH";
2202 default:
2203 return NULL;
2204 }
2205 }
2206
2207 static const char *
2208 get_sparc64_dynamic_type (unsigned long type)
2209 {
2210 switch (type)
2211 {
2212 case DT_SPARC_REGISTER: return "SPARC_REGISTER";
2213 default:
2214 return NULL;
2215 }
2216 }
2217
2218 static const char *
2219 get_ppc_dynamic_type (unsigned long type)
2220 {
2221 switch (type)
2222 {
2223 case DT_PPC_GOT: return "PPC_GOT";
2224 case DT_PPC_OPT: return "PPC_OPT";
2225 default:
2226 return NULL;
2227 }
2228 }
2229
2230 static const char *
2231 get_ppc64_dynamic_type (unsigned long type)
2232 {
2233 switch (type)
2234 {
2235 case DT_PPC64_GLINK: return "PPC64_GLINK";
2236 case DT_PPC64_OPD: return "PPC64_OPD";
2237 case DT_PPC64_OPDSZ: return "PPC64_OPDSZ";
2238 case DT_PPC64_OPT: return "PPC64_OPT";
2239 default:
2240 return NULL;
2241 }
2242 }
2243
2244 static const char *
2245 get_parisc_dynamic_type (unsigned long type)
2246 {
2247 switch (type)
2248 {
2249 case DT_HP_LOAD_MAP: return "HP_LOAD_MAP";
2250 case DT_HP_DLD_FLAGS: return "HP_DLD_FLAGS";
2251 case DT_HP_DLD_HOOK: return "HP_DLD_HOOK";
2252 case DT_HP_UX10_INIT: return "HP_UX10_INIT";
2253 case DT_HP_UX10_INITSZ: return "HP_UX10_INITSZ";
2254 case DT_HP_PREINIT: return "HP_PREINIT";
2255 case DT_HP_PREINITSZ: return "HP_PREINITSZ";
2256 case DT_HP_NEEDED: return "HP_NEEDED";
2257 case DT_HP_TIME_STAMP: return "HP_TIME_STAMP";
2258 case DT_HP_CHECKSUM: return "HP_CHECKSUM";
2259 case DT_HP_GST_SIZE: return "HP_GST_SIZE";
2260 case DT_HP_GST_VERSION: return "HP_GST_VERSION";
2261 case DT_HP_GST_HASHVAL: return "HP_GST_HASHVAL";
2262 case DT_HP_EPLTREL: return "HP_GST_EPLTREL";
2263 case DT_HP_EPLTRELSZ: return "HP_GST_EPLTRELSZ";
2264 case DT_HP_FILTERED: return "HP_FILTERED";
2265 case DT_HP_FILTER_TLS: return "HP_FILTER_TLS";
2266 case DT_HP_COMPAT_FILTERED: return "HP_COMPAT_FILTERED";
2267 case DT_HP_LAZYLOAD: return "HP_LAZYLOAD";
2268 case DT_HP_BIND_NOW_COUNT: return "HP_BIND_NOW_COUNT";
2269 case DT_PLT: return "PLT";
2270 case DT_PLT_SIZE: return "PLT_SIZE";
2271 case DT_DLT: return "DLT";
2272 case DT_DLT_SIZE: return "DLT_SIZE";
2273 default:
2274 return NULL;
2275 }
2276 }
2277
2278 static const char *
2279 get_ia64_dynamic_type (unsigned long type)
2280 {
2281 switch (type)
2282 {
2283 case DT_IA_64_PLT_RESERVE: return "IA_64_PLT_RESERVE";
2284 case DT_IA_64_VMS_SUBTYPE: return "VMS_SUBTYPE";
2285 case DT_IA_64_VMS_IMGIOCNT: return "VMS_IMGIOCNT";
2286 case DT_IA_64_VMS_LNKFLAGS: return "VMS_LNKFLAGS";
2287 case DT_IA_64_VMS_VIR_MEM_BLK_SIZ: return "VMS_VIR_MEM_BLK_SIZ";
2288 case DT_IA_64_VMS_IDENT: return "VMS_IDENT";
2289 case DT_IA_64_VMS_NEEDED_IDENT: return "VMS_NEEDED_IDENT";
2290 case DT_IA_64_VMS_IMG_RELA_CNT: return "VMS_IMG_RELA_CNT";
2291 case DT_IA_64_VMS_SEG_RELA_CNT: return "VMS_SEG_RELA_CNT";
2292 case DT_IA_64_VMS_FIXUP_RELA_CNT: return "VMS_FIXUP_RELA_CNT";
2293 case DT_IA_64_VMS_FIXUP_NEEDED: return "VMS_FIXUP_NEEDED";
2294 case DT_IA_64_VMS_SYMVEC_CNT: return "VMS_SYMVEC_CNT";
2295 case DT_IA_64_VMS_XLATED: return "VMS_XLATED";
2296 case DT_IA_64_VMS_STACKSIZE: return "VMS_STACKSIZE";
2297 case DT_IA_64_VMS_UNWINDSZ: return "VMS_UNWINDSZ";
2298 case DT_IA_64_VMS_UNWIND_CODSEG: return "VMS_UNWIND_CODSEG";
2299 case DT_IA_64_VMS_UNWIND_INFOSEG: return "VMS_UNWIND_INFOSEG";
2300 case DT_IA_64_VMS_LINKTIME: return "VMS_LINKTIME";
2301 case DT_IA_64_VMS_SEG_NO: return "VMS_SEG_NO";
2302 case DT_IA_64_VMS_SYMVEC_OFFSET: return "VMS_SYMVEC_OFFSET";
2303 case DT_IA_64_VMS_SYMVEC_SEG: return "VMS_SYMVEC_SEG";
2304 case DT_IA_64_VMS_UNWIND_OFFSET: return "VMS_UNWIND_OFFSET";
2305 case DT_IA_64_VMS_UNWIND_SEG: return "VMS_UNWIND_SEG";
2306 case DT_IA_64_VMS_STRTAB_OFFSET: return "VMS_STRTAB_OFFSET";
2307 case DT_IA_64_VMS_SYSVER_OFFSET: return "VMS_SYSVER_OFFSET";
2308 case DT_IA_64_VMS_IMG_RELA_OFF: return "VMS_IMG_RELA_OFF";
2309 case DT_IA_64_VMS_SEG_RELA_OFF: return "VMS_SEG_RELA_OFF";
2310 case DT_IA_64_VMS_FIXUP_RELA_OFF: return "VMS_FIXUP_RELA_OFF";
2311 case DT_IA_64_VMS_PLTGOT_OFFSET: return "VMS_PLTGOT_OFFSET";
2312 case DT_IA_64_VMS_PLTGOT_SEG: return "VMS_PLTGOT_SEG";
2313 case DT_IA_64_VMS_FPMODE: return "VMS_FPMODE";
2314 default:
2315 return NULL;
2316 }
2317 }
2318
2319 static const char *
2320 get_solaris_section_type (unsigned long type)
2321 {
2322 switch (type)
2323 {
2324 case 0x6fffffee: return "SUNW_ancillary";
2325 case 0x6fffffef: return "SUNW_capchain";
2326 case 0x6ffffff0: return "SUNW_capinfo";
2327 case 0x6ffffff1: return "SUNW_symsort";
2328 case 0x6ffffff2: return "SUNW_tlssort";
2329 case 0x6ffffff3: return "SUNW_LDYNSYM";
2330 case 0x6ffffff4: return "SUNW_dof";
2331 case 0x6ffffff5: return "SUNW_cap";
2332 case 0x6ffffff6: return "SUNW_SIGNATURE";
2333 case 0x6ffffff7: return "SUNW_ANNOTATE";
2334 case 0x6ffffff8: return "SUNW_DEBUGSTR";
2335 case 0x6ffffff9: return "SUNW_DEBUG";
2336 case 0x6ffffffa: return "SUNW_move";
2337 case 0x6ffffffb: return "SUNW_COMDAT";
2338 case 0x6ffffffc: return "SUNW_syminfo";
2339 case 0x6ffffffd: return "SUNW_verdef";
2340 case 0x6ffffffe: return "SUNW_verneed";
2341 case 0x6fffffff: return "SUNW_versym";
2342 case 0x70000000: return "SPARC_GOTDATA";
2343 default: return NULL;
2344 }
2345 }
2346
2347 static const char *
2348 get_alpha_dynamic_type (unsigned long type)
2349 {
2350 switch (type)
2351 {
2352 case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
2353 default: return NULL;
2354 }
2355 }
2356
2357 static const char *
2358 get_score_dynamic_type (unsigned long type)
2359 {
2360 switch (type)
2361 {
2362 case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
2363 case DT_SCORE_LOCAL_GOTNO: return "SCORE_LOCAL_GOTNO";
2364 case DT_SCORE_SYMTABNO: return "SCORE_SYMTABNO";
2365 case DT_SCORE_GOTSYM: return "SCORE_GOTSYM";
2366 case DT_SCORE_UNREFEXTNO: return "SCORE_UNREFEXTNO";
2367 case DT_SCORE_HIPAGENO: return "SCORE_HIPAGENO";
2368 default: return NULL;
2369 }
2370 }
2371
2372 static const char *
2373 get_tic6x_dynamic_type (unsigned long type)
2374 {
2375 switch (type)
2376 {
2377 case DT_C6000_GSYM_OFFSET: return "C6000_GSYM_OFFSET";
2378 case DT_C6000_GSTR_OFFSET: return "C6000_GSTR_OFFSET";
2379 case DT_C6000_DSBT_BASE: return "C6000_DSBT_BASE";
2380 case DT_C6000_DSBT_SIZE: return "C6000_DSBT_SIZE";
2381 case DT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
2382 case DT_C6000_DSBT_INDEX: return "C6000_DSBT_INDEX";
2383 default: return NULL;
2384 }
2385 }
2386
2387 static const char *
2388 get_nios2_dynamic_type (unsigned long type)
2389 {
2390 switch (type)
2391 {
2392 case DT_NIOS2_GP: return "NIOS2_GP";
2393 default: return NULL;
2394 }
2395 }
2396
2397 static const char *
2398 get_solaris_dynamic_type (unsigned long type)
2399 {
2400 switch (type)
2401 {
2402 case 0x6000000d: return "SUNW_AUXILIARY";
2403 case 0x6000000e: return "SUNW_RTLDINF";
2404 case 0x6000000f: return "SUNW_FILTER";
2405 case 0x60000010: return "SUNW_CAP";
2406 case 0x60000011: return "SUNW_SYMTAB";
2407 case 0x60000012: return "SUNW_SYMSZ";
2408 case 0x60000013: return "SUNW_SORTENT";
2409 case 0x60000014: return "SUNW_SYMSORT";
2410 case 0x60000015: return "SUNW_SYMSORTSZ";
2411 case 0x60000016: return "SUNW_TLSSORT";
2412 case 0x60000017: return "SUNW_TLSSORTSZ";
2413 case 0x60000018: return "SUNW_CAPINFO";
2414 case 0x60000019: return "SUNW_STRPAD";
2415 case 0x6000001a: return "SUNW_CAPCHAIN";
2416 case 0x6000001b: return "SUNW_LDMACH";
2417 case 0x6000001d: return "SUNW_CAPCHAINENT";
2418 case 0x6000001f: return "SUNW_CAPCHAINSZ";
2419 case 0x60000021: return "SUNW_PARENT";
2420 case 0x60000023: return "SUNW_ASLR";
2421 case 0x60000025: return "SUNW_RELAX";
2422 case 0x60000029: return "SUNW_NXHEAP";
2423 case 0x6000002b: return "SUNW_NXSTACK";
2424
2425 case 0x70000001: return "SPARC_REGISTER";
2426 case 0x7ffffffd: return "AUXILIARY";
2427 case 0x7ffffffe: return "USED";
2428 case 0x7fffffff: return "FILTER";
2429
2430 default: return NULL;
2431 }
2432 }
2433
2434 static const char *
2435 get_riscv_dynamic_type (unsigned long type)
2436 {
2437 switch (type)
2438 {
2439 case DT_RISCV_VARIANT_CC: return "RISCV_VARIANT_CC";
2440 default:
2441 return NULL;
2442 }
2443 }
2444
2445 static const char *
2446 get_dynamic_type (Filedata * filedata, unsigned long type)
2447 {
2448 static char buff[64];
2449
2450 switch (type)
2451 {
2452 case DT_NULL: return "NULL";
2453 case DT_NEEDED: return "NEEDED";
2454 case DT_PLTRELSZ: return "PLTRELSZ";
2455 case DT_PLTGOT: return "PLTGOT";
2456 case DT_HASH: return "HASH";
2457 case DT_STRTAB: return "STRTAB";
2458 case DT_SYMTAB: return "SYMTAB";
2459 case DT_RELA: return "RELA";
2460 case DT_RELASZ: return "RELASZ";
2461 case DT_RELAENT: return "RELAENT";
2462 case DT_STRSZ: return "STRSZ";
2463 case DT_SYMENT: return "SYMENT";
2464 case DT_INIT: return "INIT";
2465 case DT_FINI: return "FINI";
2466 case DT_SONAME: return "SONAME";
2467 case DT_RPATH: return "RPATH";
2468 case DT_SYMBOLIC: return "SYMBOLIC";
2469 case DT_REL: return "REL";
2470 case DT_RELSZ: return "RELSZ";
2471 case DT_RELENT: return "RELENT";
2472 case DT_RELR: return "RELR";
2473 case DT_RELRSZ: return "RELRSZ";
2474 case DT_RELRENT: return "RELRENT";
2475 case DT_PLTREL: return "PLTREL";
2476 case DT_DEBUG: return "DEBUG";
2477 case DT_TEXTREL: return "TEXTREL";
2478 case DT_JMPREL: return "JMPREL";
2479 case DT_BIND_NOW: return "BIND_NOW";
2480 case DT_INIT_ARRAY: return "INIT_ARRAY";
2481 case DT_FINI_ARRAY: return "FINI_ARRAY";
2482 case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
2483 case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
2484 case DT_RUNPATH: return "RUNPATH";
2485 case DT_FLAGS: return "FLAGS";
2486
2487 case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
2488 case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
2489 case DT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
2490
2491 case DT_CHECKSUM: return "CHECKSUM";
2492 case DT_PLTPADSZ: return "PLTPADSZ";
2493 case DT_MOVEENT: return "MOVEENT";
2494 case DT_MOVESZ: return "MOVESZ";
2495 case DT_FEATURE: return "FEATURE";
2496 case DT_POSFLAG_1: return "POSFLAG_1";
2497 case DT_SYMINSZ: return "SYMINSZ";
2498 case DT_SYMINENT: return "SYMINENT"; /* aka VALRNGHI */
2499
2500 case DT_ADDRRNGLO: return "ADDRRNGLO";
2501 case DT_CONFIG: return "CONFIG";
2502 case DT_DEPAUDIT: return "DEPAUDIT";
2503 case DT_AUDIT: return "AUDIT";
2504 case DT_PLTPAD: return "PLTPAD";
2505 case DT_MOVETAB: return "MOVETAB";
2506 case DT_SYMINFO: return "SYMINFO"; /* aka ADDRRNGHI */
2507
2508 case DT_VERSYM: return "VERSYM";
2509
2510 case DT_TLSDESC_GOT: return "TLSDESC_GOT";
2511 case DT_TLSDESC_PLT: return "TLSDESC_PLT";
2512 case DT_RELACOUNT: return "RELACOUNT";
2513 case DT_RELCOUNT: return "RELCOUNT";
2514 case DT_FLAGS_1: return "FLAGS_1";
2515 case DT_VERDEF: return "VERDEF";
2516 case DT_VERDEFNUM: return "VERDEFNUM";
2517 case DT_VERNEED: return "VERNEED";
2518 case DT_VERNEEDNUM: return "VERNEEDNUM";
2519
2520 case DT_AUXILIARY: return "AUXILIARY";
2521 case DT_USED: return "USED";
2522 case DT_FILTER: return "FILTER";
2523
2524 case DT_GNU_PRELINKED: return "GNU_PRELINKED";
2525 case DT_GNU_CONFLICT: return "GNU_CONFLICT";
2526 case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
2527 case DT_GNU_LIBLIST: return "GNU_LIBLIST";
2528 case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
2529 case DT_GNU_HASH: return "GNU_HASH";
2530 case DT_GNU_FLAGS_1: return "GNU_FLAGS_1";
2531
2532 default:
2533 if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
2534 {
2535 const char * result;
2536
2537 switch (filedata->file_header.e_machine)
2538 {
2539 case EM_AARCH64:
2540 result = get_aarch64_dynamic_type (type);
2541 break;
2542 case EM_MIPS:
2543 case EM_MIPS_RS3_LE:
2544 result = get_mips_dynamic_type (type);
2545 break;
2546 case EM_SPARCV9:
2547 result = get_sparc64_dynamic_type (type);
2548 break;
2549 case EM_PPC:
2550 result = get_ppc_dynamic_type (type);
2551 break;
2552 case EM_PPC64:
2553 result = get_ppc64_dynamic_type (type);
2554 break;
2555 case EM_IA_64:
2556 result = get_ia64_dynamic_type (type);
2557 break;
2558 case EM_ALPHA:
2559 result = get_alpha_dynamic_type (type);
2560 break;
2561 case EM_SCORE:
2562 result = get_score_dynamic_type (type);
2563 break;
2564 case EM_TI_C6000:
2565 result = get_tic6x_dynamic_type (type);
2566 break;
2567 case EM_ALTERA_NIOS2:
2568 result = get_nios2_dynamic_type (type);
2569 break;
2570 case EM_RISCV:
2571 result = get_riscv_dynamic_type (type);
2572 break;
2573 default:
2574 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2575 result = get_solaris_dynamic_type (type);
2576 else
2577 result = NULL;
2578 break;
2579 }
2580
2581 if (result != NULL)
2582 return result;
2583
2584 snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
2585 }
2586 else if (((type >= DT_LOOS) && (type <= DT_HIOS))
2587 || (filedata->file_header.e_machine == EM_PARISC
2588 && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
2589 {
2590 const char * result;
2591
2592 switch (filedata->file_header.e_machine)
2593 {
2594 case EM_PARISC:
2595 result = get_parisc_dynamic_type (type);
2596 break;
2597 case EM_IA_64:
2598 result = get_ia64_dynamic_type (type);
2599 break;
2600 default:
2601 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2602 result = get_solaris_dynamic_type (type);
2603 else
2604 result = NULL;
2605 break;
2606 }
2607
2608 if (result != NULL)
2609 return result;
2610
2611 snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
2612 type);
2613 }
2614 else
2615 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
2616
2617 return buff;
2618 }
2619 }
2620
2621 static bool get_program_headers (Filedata *);
2622 static bool get_dynamic_section (Filedata *);
2623
2624 static void
2625 locate_dynamic_section (Filedata *filedata)
2626 {
2627 unsigned long dynamic_addr = 0;
2628 uint64_t dynamic_size = 0;
2629
2630 if (filedata->file_header.e_phnum != 0
2631 && get_program_headers (filedata))
2632 {
2633 Elf_Internal_Phdr *segment;
2634 unsigned int i;
2635
2636 for (i = 0, segment = filedata->program_headers;
2637 i < filedata->file_header.e_phnum;
2638 i++, segment++)
2639 {
2640 if (segment->p_type == PT_DYNAMIC)
2641 {
2642 dynamic_addr = segment->p_offset;
2643 dynamic_size = segment->p_filesz;
2644
2645 if (filedata->section_headers != NULL)
2646 {
2647 Elf_Internal_Shdr *sec;
2648
2649 sec = find_section (filedata, ".dynamic");
2650 if (sec != NULL)
2651 {
2652 if (sec->sh_size == 0
2653 || sec->sh_type == SHT_NOBITS)
2654 {
2655 dynamic_addr = 0;
2656 dynamic_size = 0;
2657 }
2658 else
2659 {
2660 dynamic_addr = sec->sh_offset;
2661 dynamic_size = sec->sh_size;
2662 }
2663 }
2664 }
2665
2666 if (dynamic_addr > filedata->file_size
2667 || (dynamic_size > filedata->file_size - dynamic_addr))
2668 {
2669 dynamic_addr = 0;
2670 dynamic_size = 0;
2671 }
2672 break;
2673 }
2674 }
2675 }
2676 filedata->dynamic_addr = dynamic_addr;
2677 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
2678 }
2679
2680 static bool
2681 is_pie (Filedata *filedata)
2682 {
2683 Elf_Internal_Dyn *entry;
2684
2685 if (filedata->dynamic_size == 0)
2686 locate_dynamic_section (filedata);
2687 if (filedata->dynamic_size <= 1)
2688 return false;
2689
2690 if (!get_dynamic_section (filedata))
2691 return false;
2692
2693 for (entry = filedata->dynamic_section;
2694 entry < filedata->dynamic_section + filedata->dynamic_nent;
2695 entry++)
2696 {
2697 if (entry->d_tag == DT_FLAGS_1)
2698 {
2699 if ((entry->d_un.d_val & DF_1_PIE) != 0)
2700 return true;
2701 break;
2702 }
2703 }
2704 return false;
2705 }
2706
2707 static char *
2708 get_file_type (Filedata *filedata)
2709 {
2710 unsigned e_type = filedata->file_header.e_type;
2711 static char buff[64];
2712
2713 switch (e_type)
2714 {
2715 case ET_NONE: return _("NONE (None)");
2716 case ET_REL: return _("REL (Relocatable file)");
2717 case ET_EXEC: return _("EXEC (Executable file)");
2718 case ET_DYN:
2719 if (is_pie (filedata))
2720 return _("DYN (Position-Independent Executable file)");
2721 else
2722 return _("DYN (Shared object file)");
2723 case ET_CORE: return _("CORE (Core file)");
2724
2725 default:
2726 if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
2727 snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
2728 else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
2729 snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
2730 else
2731 snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
2732 return buff;
2733 }
2734 }
2735
2736 static char *
2737 get_machine_name (unsigned e_machine)
2738 {
2739 static char buff[64]; /* XXX */
2740
2741 switch (e_machine)
2742 {
2743 /* Please keep this switch table sorted by increasing EM_ value. */
2744 /* 0 */
2745 case EM_NONE: return _("None");
2746 case EM_M32: return "WE32100";
2747 case EM_SPARC: return "Sparc";
2748 case EM_386: return "Intel 80386";
2749 case EM_68K: return "MC68000";
2750 case EM_88K: return "MC88000";
2751 case EM_IAMCU: return "Intel MCU";
2752 case EM_860: return "Intel 80860";
2753 case EM_MIPS: return "MIPS R3000";
2754 case EM_S370: return "IBM System/370";
2755 /* 10 */
2756 case EM_MIPS_RS3_LE: return "MIPS R4000 big-endian";
2757 case EM_OLD_SPARCV9: return "Sparc v9 (old)";
2758 case EM_PARISC: return "HPPA";
2759 case EM_VPP550: return "Fujitsu VPP500";
2760 case EM_SPARC32PLUS: return "Sparc v8+" ;
2761 case EM_960: return "Intel 80960";
2762 case EM_PPC: return "PowerPC";
2763 /* 20 */
2764 case EM_PPC64: return "PowerPC64";
2765 case EM_S390_OLD:
2766 case EM_S390: return "IBM S/390";
2767 case EM_SPU: return "SPU";
2768 /* 30 */
2769 case EM_V800: return "Renesas V850 (using RH850 ABI)";
2770 case EM_FR20: return "Fujitsu FR20";
2771 case EM_RH32: return "TRW RH32";
2772 case EM_MCORE: return "MCORE";
2773 /* 40 */
2774 case EM_ARM: return "ARM";
2775 case EM_OLD_ALPHA: return "Digital Alpha (old)";
2776 case EM_SH: return "Renesas / SuperH SH";
2777 case EM_SPARCV9: return "Sparc v9";
2778 case EM_TRICORE: return "Siemens Tricore";
2779 case EM_ARC: return "ARC";
2780 case EM_H8_300: return "Renesas H8/300";
2781 case EM_H8_300H: return "Renesas H8/300H";
2782 case EM_H8S: return "Renesas H8S";
2783 case EM_H8_500: return "Renesas H8/500";
2784 /* 50 */
2785 case EM_IA_64: return "Intel IA-64";
2786 case EM_MIPS_X: return "Stanford MIPS-X";
2787 case EM_COLDFIRE: return "Motorola Coldfire";
2788 case EM_68HC12: return "Motorola MC68HC12 Microcontroller";
2789 case EM_MMA: return "Fujitsu Multimedia Accelerator";
2790 case EM_PCP: return "Siemens PCP";
2791 case EM_NCPU: return "Sony nCPU embedded RISC processor";
2792 case EM_NDR1: return "Denso NDR1 microprocesspr";
2793 case EM_STARCORE: return "Motorola Star*Core processor";
2794 case EM_ME16: return "Toyota ME16 processor";
2795 /* 60 */
2796 case EM_ST100: return "STMicroelectronics ST100 processor";
2797 case EM_TINYJ: return "Advanced Logic Corp. TinyJ embedded processor";
2798 case EM_X86_64: return "Advanced Micro Devices X86-64";
2799 case EM_PDSP: return "Sony DSP processor";
2800 case EM_PDP10: return "Digital Equipment Corp. PDP-10";
2801 case EM_PDP11: return "Digital Equipment Corp. PDP-11";
2802 case EM_FX66: return "Siemens FX66 microcontroller";
2803 case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 bit microcontroller";
2804 case EM_ST7: return "STMicroelectronics ST7 8-bit microcontroller";
2805 case EM_68HC16: return "Motorola MC68HC16 Microcontroller";
2806 /* 70 */
2807 case EM_68HC11: return "Motorola MC68HC11 Microcontroller";
2808 case EM_68HC08: return "Motorola MC68HC08 Microcontroller";
2809 case EM_68HC05: return "Motorola MC68HC05 Microcontroller";
2810 case EM_SVX: return "Silicon Graphics SVx";
2811 case EM_ST19: return "STMicroelectronics ST19 8-bit microcontroller";
2812 case EM_VAX: return "Digital VAX";
2813 case EM_CRIS: return "Axis Communications 32-bit embedded processor";
2814 case EM_JAVELIN: return "Infineon Technologies 32-bit embedded cpu";
2815 case EM_FIREPATH: return "Element 14 64-bit DSP processor";
2816 case EM_ZSP: return "LSI Logic's 16-bit DSP processor";
2817 /* 80 */
2818 case EM_MMIX: return "Donald Knuth's educational 64-bit processor";
2819 case EM_HUANY: return "Harvard Universitys's machine-independent object format";
2820 case EM_PRISM: return "Vitesse Prism";
2821 case EM_AVR_OLD:
2822 case EM_AVR: return "Atmel AVR 8-bit microcontroller";
2823 case EM_CYGNUS_FR30:
2824 case EM_FR30: return "Fujitsu FR30";
2825 case EM_CYGNUS_D10V:
2826 case EM_D10V: return "d10v";
2827 case EM_CYGNUS_D30V:
2828 case EM_D30V: return "d30v";
2829 case EM_CYGNUS_V850:
2830 case EM_V850: return "Renesas V850";
2831 case EM_CYGNUS_M32R:
2832 case EM_M32R: return "Renesas M32R (formerly Mitsubishi M32r)";
2833 case EM_CYGNUS_MN10300:
2834 case EM_MN10300: return "mn10300";
2835 /* 90 */
2836 case EM_CYGNUS_MN10200:
2837 case EM_MN10200: return "mn10200";
2838 case EM_PJ: return "picoJava";
2839 case EM_OR1K: return "OpenRISC 1000";
2840 case EM_ARC_COMPACT: return "ARCompact";
2841 case EM_XTENSA_OLD:
2842 case EM_XTENSA: return "Tensilica Xtensa Processor";
2843 case EM_VIDEOCORE: return "Alphamosaic VideoCore processor";
2844 case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor";
2845 case EM_NS32K: return "National Semiconductor 32000 series";
2846 case EM_TPC: return "Tenor Network TPC processor";
2847 case EM_SNP1K: return "Trebia SNP 1000 processor";
2848 /* 100 */
2849 case EM_ST200: return "STMicroelectronics ST200 microcontroller";
2850 case EM_IP2K_OLD:
2851 case EM_IP2K: return "Ubicom IP2xxx 8-bit microcontrollers";
2852 case EM_MAX: return "MAX Processor";
2853 case EM_CR: return "National Semiconductor CompactRISC";
2854 case EM_F2MC16: return "Fujitsu F2MC16";
2855 case EM_MSP430: return "Texas Instruments msp430 microcontroller";
2856 case EM_BLACKFIN: return "Analog Devices Blackfin";
2857 case EM_SE_C33: return "S1C33 Family of Seiko Epson processors";
2858 case EM_SEP: return "Sharp embedded microprocessor";
2859 case EM_ARCA: return "Arca RISC microprocessor";
2860 /* 110 */
2861 case EM_UNICORE: return "Unicore";
2862 case EM_EXCESS: return "eXcess 16/32/64-bit configurable embedded CPU";
2863 case EM_DXP: return "Icera Semiconductor Inc. Deep Execution Processor";
2864 case EM_ALTERA_NIOS2: return "Altera Nios II";
2865 case EM_CRX: return "National Semiconductor CRX microprocessor";
2866 case EM_XGATE: return "Motorola XGATE embedded processor";
2867 case EM_C166:
2868 case EM_XC16X: return "Infineon Technologies xc16x";
2869 case EM_M16C: return "Renesas M16C series microprocessors";
2870 case EM_DSPIC30F: return "Microchip Technology dsPIC30F Digital Signal Controller";
2871 case EM_CE: return "Freescale Communication Engine RISC core";
2872 /* 120 */
2873 case EM_M32C: return "Renesas M32c";
2874 /* 130 */
2875 case EM_TSK3000: return "Altium TSK3000 core";
2876 case EM_RS08: return "Freescale RS08 embedded processor";
2877 case EM_ECOG2: return "Cyan Technology eCOG2 microprocessor";
2878 case EM_SCORE: return "SUNPLUS S+Core";
2879 case EM_DSP24: return "New Japan Radio (NJR) 24-bit DSP Processor";
2880 case EM_VIDEOCORE3: return "Broadcom VideoCore III processor";
2881 case EM_LATTICEMICO32: return "Lattice Mico32";
2882 case EM_SE_C17: return "Seiko Epson C17 family";
2883 /* 140 */
2884 case EM_TI_C6000: return "Texas Instruments TMS320C6000 DSP family";
2885 case EM_TI_C2000: return "Texas Instruments TMS320C2000 DSP family";
2886 case EM_TI_C5500: return "Texas Instruments TMS320C55x DSP family";
2887 case EM_TI_PRU: return "TI PRU I/O processor";
2888 /* 160 */
2889 case EM_MMDSP_PLUS: return "STMicroelectronics 64bit VLIW Data Signal Processor";
2890 case EM_CYPRESS_M8C: return "Cypress M8C microprocessor";
2891 case EM_R32C: return "Renesas R32C series microprocessors";
2892 case EM_TRIMEDIA: return "NXP Semiconductors TriMedia architecture family";
2893 case EM_QDSP6: return "QUALCOMM DSP6 Processor";
2894 case EM_8051: return "Intel 8051 and variants";
2895 case EM_STXP7X: return "STMicroelectronics STxP7x family";
2896 case EM_NDS32: return "Andes Technology compact code size embedded RISC processor family";
2897 case EM_ECOG1X: return "Cyan Technology eCOG1X family";
2898 case EM_MAXQ30: return "Dallas Semiconductor MAXQ30 Core microcontrollers";
2899 /* 170 */
2900 case EM_XIMO16: return "New Japan Radio (NJR) 16-bit DSP Processor";
2901 case EM_MANIK: return "M2000 Reconfigurable RISC Microprocessor";
2902 case EM_CRAYNV2: return "Cray Inc. NV2 vector architecture";
2903 case EM_RX: return "Renesas RX";
2904 case EM_METAG: return "Imagination Technologies Meta processor architecture";
2905 case EM_MCST_ELBRUS: return "MCST Elbrus general purpose hardware architecture";
2906 case EM_ECOG16: return "Cyan Technology eCOG16 family";
2907 case EM_CR16:
2908 case EM_MICROBLAZE:
2909 case EM_MICROBLAZE_OLD: return "Xilinx MicroBlaze";
2910 case EM_ETPU: return "Freescale Extended Time Processing Unit";
2911 case EM_SLE9X: return "Infineon Technologies SLE9X core";
2912 /* 180 */
2913 case EM_L1OM: return "Intel L1OM";
2914 case EM_K1OM: return "Intel K1OM";
2915 case EM_INTEL182: return "Intel (reserved)";
2916 case EM_AARCH64: return "AArch64";
2917 case EM_ARM184: return "ARM (reserved)";
2918 case EM_AVR32: return "Atmel Corporation 32-bit microprocessor";
2919 case EM_STM8: return "STMicroeletronics STM8 8-bit microcontroller";
2920 case EM_TILE64: return "Tilera TILE64 multicore architecture family";
2921 case EM_TILEPRO: return "Tilera TILEPro multicore architecture family";
2922 /* 190 */
2923 case EM_CUDA: return "NVIDIA CUDA architecture";
2924 case EM_TILEGX: return "Tilera TILE-Gx multicore architecture family";
2925 case EM_CLOUDSHIELD: return "CloudShield architecture family";
2926 case EM_COREA_1ST: return "KIPO-KAIST Core-A 1st generation processor family";
2927 case EM_COREA_2ND: return "KIPO-KAIST Core-A 2nd generation processor family";
2928 case EM_ARC_COMPACT2: return "ARCv2";
2929 case EM_OPEN8: return "Open8 8-bit RISC soft processor core";
2930 case EM_RL78: return "Renesas RL78";
2931 case EM_VIDEOCORE5: return "Broadcom VideoCore V processor";
2932 case EM_78K0R: return "Renesas 78K0R";
2933 /* 200 */
2934 case EM_56800EX: return "Freescale 56800EX Digital Signal Controller (DSC)";
2935 case EM_BA1: return "Beyond BA1 CPU architecture";
2936 case EM_BA2: return "Beyond BA2 CPU architecture";
2937 case EM_XCORE: return "XMOS xCORE processor family";
2938 case EM_MCHP_PIC: return "Microchip 8-bit PIC(r) family";
2939 case EM_INTELGT: return "Intel Graphics Technology";
2940 /* 210 */
2941 case EM_KM32: return "KM211 KM32 32-bit processor";
2942 case EM_KMX32: return "KM211 KMX32 32-bit processor";
2943 case EM_KMX16: return "KM211 KMX16 16-bit processor";
2944 case EM_KMX8: return "KM211 KMX8 8-bit processor";
2945 case EM_KVARC: return "KM211 KVARC processor";
2946 case EM_CDP: return "Paneve CDP architecture family";
2947 case EM_COGE: return "Cognitive Smart Memory Processor";
2948 case EM_COOL: return "Bluechip Systems CoolEngine";
2949 case EM_NORC: return "Nanoradio Optimized RISC";
2950 case EM_CSR_KALIMBA: return "CSR Kalimba architecture family";
2951 /* 220 */
2952 case EM_Z80: return "Zilog Z80";
2953 case EM_VISIUM: return "CDS VISIUMcore processor";
2954 case EM_FT32: return "FTDI Chip FT32";
2955 case EM_MOXIE: return "Moxie";
2956 case EM_AMDGPU: return "AMD GPU";
2957 /* 230 (all reserved) */
2958 /* 240 */
2959 case EM_RISCV: return "RISC-V";
2960 case EM_LANAI: return "Lanai 32-bit processor";
2961 case EM_CEVA: return "CEVA Processor Architecture Family";
2962 case EM_CEVA_X2: return "CEVA X2 Processor Family";
2963 case EM_BPF: return "Linux BPF";
2964 case EM_GRAPHCORE_IPU: return "Graphcore Intelligent Processing Unit";
2965 case EM_IMG1: return "Imagination Technologies";
2966 /* 250 */
2967 case EM_NFP: return "Netronome Flow Processor";
2968 case EM_VE: return "NEC Vector Engine";
2969 case EM_CSKY: return "C-SKY";
2970 case EM_ARC_COMPACT3_64: return "Synopsys ARCv2.3 64-bit";
2971 case EM_MCS6502: return "MOS Technology MCS 6502 processor";
2972 case EM_ARC_COMPACT3: return "Synopsys ARCv2.3 32-bit";
2973 case EM_KVX: return "Kalray VLIW core of the MPPA processor family";
2974 case EM_65816: return "WDC 65816/65C816";
2975 case EM_LOONGARCH: return "LoongArch";
2976 case EM_KF32: return "ChipON KungFu32";
2977
2978 /* Large numbers... */
2979 case EM_MT: return "Morpho Techologies MT processor";
2980 case EM_ALPHA: return "Alpha";
2981 case EM_WEBASSEMBLY: return "Web Assembly";
2982 case EM_DLX: return "OpenDLX";
2983 case EM_XSTORMY16: return "Sanyo XStormy16 CPU core";
2984 case EM_IQ2000: return "Vitesse IQ2000";
2985 case EM_M32C_OLD:
2986 case EM_NIOS32: return "Altera Nios";
2987 case EM_CYGNUS_MEP: return "Toshiba MeP Media Engine";
2988 case EM_ADAPTEVA_EPIPHANY: return "Adapteva EPIPHANY";
2989 case EM_CYGNUS_FRV: return "Fujitsu FR-V";
2990 case EM_S12Z: return "Freescale S12Z";
2991
2992 default:
2993 snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
2994 return buff;
2995 }
2996 }
2997
2998 static void
2999 decode_ARC_machine_flags (unsigned e_flags, unsigned e_machine, char buf[])
3000 {
3001 /* ARC has two machine types EM_ARC_COMPACT and EM_ARC_COMPACT2. Some
3002 other compilers don't specify an architecture type in the e_flags, and
3003 instead use EM_ARC_COMPACT for old ARC600, ARC601, and ARC700
3004 architectures, and switch to EM_ARC_COMPACT2 for newer ARCEM and ARCHS
3005 architectures.
3006
3007 Th GNU tools follows this use of EM_ARC_COMPACT and EM_ARC_COMPACT2,
3008 but also sets a specific architecture type in the e_flags field.
3009
3010 However, when decoding the flags we don't worry if we see an
3011 unexpected pairing, for example EM_ARC_COMPACT machine type, with
3012 ARCEM architecture type. */
3013
3014 switch (e_flags & EF_ARC_MACH_MSK)
3015 {
3016 /* We only expect these to occur for EM_ARC_COMPACT2. */
3017 case EF_ARC_CPU_ARCV2EM:
3018 strcat (buf, ", ARC EM");
3019 break;
3020 case EF_ARC_CPU_ARCV2HS:
3021 strcat (buf, ", ARC HS");
3022 break;
3023
3024 /* We only expect these to occur for EM_ARC_COMPACT. */
3025 case E_ARC_MACH_ARC600:
3026 strcat (buf, ", ARC600");
3027 break;
3028 case E_ARC_MACH_ARC601:
3029 strcat (buf, ", ARC601");
3030 break;
3031 case E_ARC_MACH_ARC700:
3032 strcat (buf, ", ARC700");
3033 break;
3034
3035 /* The only times we should end up here are (a) A corrupt ELF, (b) A
3036 new ELF with new architecture being read by an old version of
3037 readelf, or (c) An ELF built with non-GNU compiler that does not
3038 set the architecture in the e_flags. */
3039 default:
3040 if (e_machine == EM_ARC_COMPACT)
3041 strcat (buf, ", Unknown ARCompact");
3042 else
3043 strcat (buf, ", Unknown ARC");
3044 break;
3045 }
3046
3047 switch (e_flags & EF_ARC_OSABI_MSK)
3048 {
3049 case E_ARC_OSABI_ORIG:
3050 strcat (buf, ", (ABI:legacy)");
3051 break;
3052 case E_ARC_OSABI_V2:
3053 strcat (buf, ", (ABI:v2)");
3054 break;
3055 /* Only upstream 3.9+ kernels will support ARCv2 ISA. */
3056 case E_ARC_OSABI_V3:
3057 strcat (buf, ", v3 no-legacy-syscalls ABI");
3058 break;
3059 case E_ARC_OSABI_V4:
3060 strcat (buf, ", v4 ABI");
3061 break;
3062 default:
3063 strcat (buf, ", unrecognised ARC OSABI flag");
3064 break;
3065 }
3066 }
3067
3068 static void
3069 decode_ARM_machine_flags (unsigned e_flags, char buf[])
3070 {
3071 unsigned eabi;
3072 bool unknown = false;
3073
3074 eabi = EF_ARM_EABI_VERSION (e_flags);
3075 e_flags &= ~ EF_ARM_EABIMASK;
3076
3077 /* Handle "generic" ARM flags. */
3078 if (e_flags & EF_ARM_RELEXEC)
3079 {
3080 strcat (buf, ", relocatable executable");
3081 e_flags &= ~ EF_ARM_RELEXEC;
3082 }
3083
3084 if (e_flags & EF_ARM_PIC)
3085 {
3086 strcat (buf, ", position independent");
3087 e_flags &= ~ EF_ARM_PIC;
3088 }
3089
3090 /* Now handle EABI specific flags. */
3091 switch (eabi)
3092 {
3093 default:
3094 strcat (buf, ", <unrecognized EABI>");
3095 if (e_flags)
3096 unknown = true;
3097 break;
3098
3099 case EF_ARM_EABI_VER1:
3100 strcat (buf, ", Version1 EABI");
3101 while (e_flags)
3102 {
3103 unsigned flag;
3104
3105 /* Process flags one bit at a time. */
3106 flag = e_flags & - e_flags;
3107 e_flags &= ~ flag;
3108
3109 switch (flag)
3110 {
3111 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
3112 strcat (buf, ", sorted symbol tables");
3113 break;
3114
3115 default:
3116 unknown = true;
3117 break;
3118 }
3119 }
3120 break;
3121
3122 case EF_ARM_EABI_VER2:
3123 strcat (buf, ", Version2 EABI");
3124 while (e_flags)
3125 {
3126 unsigned flag;
3127
3128 /* Process flags one bit at a time. */
3129 flag = e_flags & - e_flags;
3130 e_flags &= ~ flag;
3131
3132 switch (flag)
3133 {
3134 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
3135 strcat (buf, ", sorted symbol tables");
3136 break;
3137
3138 case EF_ARM_DYNSYMSUSESEGIDX:
3139 strcat (buf, ", dynamic symbols use segment index");
3140 break;
3141
3142 case EF_ARM_MAPSYMSFIRST:
3143 strcat (buf, ", mapping symbols precede others");
3144 break;
3145
3146 default:
3147 unknown = true;
3148 break;
3149 }
3150 }
3151 break;
3152
3153 case EF_ARM_EABI_VER3:
3154 strcat (buf, ", Version3 EABI");
3155 break;
3156
3157 case EF_ARM_EABI_VER4:
3158 strcat (buf, ", Version4 EABI");
3159 while (e_flags)
3160 {
3161 unsigned flag;
3162
3163 /* Process flags one bit at a time. */
3164 flag = e_flags & - e_flags;
3165 e_flags &= ~ flag;
3166
3167 switch (flag)
3168 {
3169 case EF_ARM_BE8:
3170 strcat (buf, ", BE8");
3171 break;
3172
3173 case EF_ARM_LE8:
3174 strcat (buf, ", LE8");
3175 break;
3176
3177 default:
3178 unknown = true;
3179 break;
3180 }
3181 }
3182 break;
3183
3184 case EF_ARM_EABI_VER5:
3185 strcat (buf, ", Version5 EABI");
3186 while (e_flags)
3187 {
3188 unsigned flag;
3189
3190 /* Process flags one bit at a time. */
3191 flag = e_flags & - e_flags;
3192 e_flags &= ~ flag;
3193
3194 switch (flag)
3195 {
3196 case EF_ARM_BE8:
3197 strcat (buf, ", BE8");
3198 break;
3199
3200 case EF_ARM_LE8:
3201 strcat (buf, ", LE8");
3202 break;
3203
3204 case EF_ARM_ABI_FLOAT_SOFT: /* Conflicts with EF_ARM_SOFT_FLOAT. */
3205 strcat (buf, ", soft-float ABI");
3206 break;
3207
3208 case EF_ARM_ABI_FLOAT_HARD: /* Conflicts with EF_ARM_VFP_FLOAT. */
3209 strcat (buf, ", hard-float ABI");
3210 break;
3211
3212 default:
3213 unknown = true;
3214 break;
3215 }
3216 }
3217 break;
3218
3219 case EF_ARM_EABI_UNKNOWN:
3220 strcat (buf, ", GNU EABI");
3221 while (e_flags)
3222 {
3223 unsigned flag;
3224
3225 /* Process flags one bit at a time. */
3226 flag = e_flags & - e_flags;
3227 e_flags &= ~ flag;
3228
3229 switch (flag)
3230 {
3231 case EF_ARM_INTERWORK:
3232 strcat (buf, ", interworking enabled");
3233 break;
3234
3235 case EF_ARM_APCS_26:
3236 strcat (buf, ", uses APCS/26");
3237 break;
3238
3239 case EF_ARM_APCS_FLOAT:
3240 strcat (buf, ", uses APCS/float");
3241 break;
3242
3243 case EF_ARM_PIC:
3244 strcat (buf, ", position independent");
3245 break;
3246
3247 case EF_ARM_ALIGN8:
3248 strcat (buf, ", 8 bit structure alignment");
3249 break;
3250
3251 case EF_ARM_NEW_ABI:
3252 strcat (buf, ", uses new ABI");
3253 break;
3254
3255 case EF_ARM_OLD_ABI:
3256 strcat (buf, ", uses old ABI");
3257 break;
3258
3259 case EF_ARM_SOFT_FLOAT:
3260 strcat (buf, ", software FP");
3261 break;
3262
3263 case EF_ARM_VFP_FLOAT:
3264 strcat (buf, ", VFP");
3265 break;
3266
3267 case EF_ARM_MAVERICK_FLOAT:
3268 strcat (buf, ", Maverick FP");
3269 break;
3270
3271 default:
3272 unknown = true;
3273 break;
3274 }
3275 }
3276 }
3277
3278 if (unknown)
3279 strcat (buf,_(", <unknown>"));
3280 }
3281
3282 static void
3283 decode_AVR_machine_flags (unsigned e_flags, char buf[], size_t size)
3284 {
3285 --size; /* Leave space for null terminator. */
3286
3287 switch (e_flags & EF_AVR_MACH)
3288 {
3289 case E_AVR_MACH_AVR1:
3290 strncat (buf, ", avr:1", size);
3291 break;
3292 case E_AVR_MACH_AVR2:
3293 strncat (buf, ", avr:2", size);
3294 break;
3295 case E_AVR_MACH_AVR25:
3296 strncat (buf, ", avr:25", size);
3297 break;
3298 case E_AVR_MACH_AVR3:
3299 strncat (buf, ", avr:3", size);
3300 break;
3301 case E_AVR_MACH_AVR31:
3302 strncat (buf, ", avr:31", size);
3303 break;
3304 case E_AVR_MACH_AVR35:
3305 strncat (buf, ", avr:35", size);
3306 break;
3307 case E_AVR_MACH_AVR4:
3308 strncat (buf, ", avr:4", size);
3309 break;
3310 case E_AVR_MACH_AVR5:
3311 strncat (buf, ", avr:5", size);
3312 break;
3313 case E_AVR_MACH_AVR51:
3314 strncat (buf, ", avr:51", size);
3315 break;
3316 case E_AVR_MACH_AVR6:
3317 strncat (buf, ", avr:6", size);
3318 break;
3319 case E_AVR_MACH_AVRTINY:
3320 strncat (buf, ", avr:100", size);
3321 break;
3322 case E_AVR_MACH_XMEGA1:
3323 strncat (buf, ", avr:101", size);
3324 break;
3325 case E_AVR_MACH_XMEGA2:
3326 strncat (buf, ", avr:102", size);
3327 break;
3328 case E_AVR_MACH_XMEGA3:
3329 strncat (buf, ", avr:103", size);
3330 break;
3331 case E_AVR_MACH_XMEGA4:
3332 strncat (buf, ", avr:104", size);
3333 break;
3334 case E_AVR_MACH_XMEGA5:
3335 strncat (buf, ", avr:105", size);
3336 break;
3337 case E_AVR_MACH_XMEGA6:
3338 strncat (buf, ", avr:106", size);
3339 break;
3340 case E_AVR_MACH_XMEGA7:
3341 strncat (buf, ", avr:107", size);
3342 break;
3343 default:
3344 strncat (buf, ", avr:<unknown>", size);
3345 break;
3346 }
3347
3348 size -= strlen (buf);
3349 if (e_flags & EF_AVR_LINKRELAX_PREPARED)
3350 strncat (buf, ", link-relax", size);
3351 }
3352
3353 static void
3354 decode_NDS32_machine_flags (unsigned e_flags, char buf[], size_t size)
3355 {
3356 unsigned abi;
3357 unsigned arch;
3358 unsigned config;
3359 unsigned version;
3360 bool has_fpu = false;
3361 unsigned int r = 0;
3362
3363 static const char *ABI_STRINGS[] =
3364 {
3365 "ABI v0", /* use r5 as return register; only used in N1213HC */
3366 "ABI v1", /* use r0 as return register */
3367 "ABI v2", /* use r0 as return register and don't reserve 24 bytes for arguments */
3368 "ABI v2fp", /* for FPU */
3369 "AABI",
3370 "ABI2 FP+"
3371 };
3372 static const char *VER_STRINGS[] =
3373 {
3374 "Andes ELF V1.3 or older",
3375 "Andes ELF V1.3.1",
3376 "Andes ELF V1.4"
3377 };
3378 static const char *ARCH_STRINGS[] =
3379 {
3380 "",
3381 "Andes Star v1.0",
3382 "Andes Star v2.0",
3383 "Andes Star v3.0",
3384 "Andes Star v3.0m"
3385 };
3386
3387 abi = EF_NDS_ABI & e_flags;
3388 arch = EF_NDS_ARCH & e_flags;
3389 config = EF_NDS_INST & e_flags;
3390 version = EF_NDS32_ELF_VERSION & e_flags;
3391
3392 memset (buf, 0, size);
3393
3394 switch (abi)
3395 {
3396 case E_NDS_ABI_V0:
3397 case E_NDS_ABI_V1:
3398 case E_NDS_ABI_V2:
3399 case E_NDS_ABI_V2FP:
3400 case E_NDS_ABI_AABI:
3401 case E_NDS_ABI_V2FP_PLUS:
3402 /* In case there are holes in the array. */
3403 r += snprintf (buf + r, size - r, ", %s", ABI_STRINGS[abi >> EF_NDS_ABI_SHIFT]);
3404 break;
3405
3406 default:
3407 r += snprintf (buf + r, size - r, ", <unrecognized ABI>");
3408 break;
3409 }
3410
3411 switch (version)
3412 {
3413 case E_NDS32_ELF_VER_1_2:
3414 case E_NDS32_ELF_VER_1_3:
3415 case E_NDS32_ELF_VER_1_4:
3416 r += snprintf (buf + r, size - r, ", %s", VER_STRINGS[version >> EF_NDS32_ELF_VERSION_SHIFT]);
3417 break;
3418
3419 default:
3420 r += snprintf (buf + r, size - r, ", <unrecognized ELF version number>");
3421 break;
3422 }
3423
3424 if (E_NDS_ABI_V0 == abi)
3425 {
3426 /* OLD ABI; only used in N1213HC, has performance extension 1. */
3427 r += snprintf (buf + r, size - r, ", Andes Star v1.0, N1213HC, MAC, PERF1");
3428 if (arch == E_NDS_ARCH_STAR_V1_0)
3429 r += snprintf (buf + r, size -r, ", 16b"); /* has 16-bit instructions */
3430 return;
3431 }
3432
3433 switch (arch)
3434 {
3435 case E_NDS_ARCH_STAR_V1_0:
3436 case E_NDS_ARCH_STAR_V2_0:
3437 case E_NDS_ARCH_STAR_V3_0:
3438 case E_NDS_ARCH_STAR_V3_M:
3439 r += snprintf (buf + r, size - r, ", %s", ARCH_STRINGS[arch >> EF_NDS_ARCH_SHIFT]);
3440 break;
3441
3442 default:
3443 r += snprintf (buf + r, size - r, ", <unrecognized architecture>");
3444 /* ARCH version determines how the e_flags are interpreted.
3445 If it is unknown, we cannot proceed. */
3446 return;
3447 }
3448
3449 /* Newer ABI; Now handle architecture specific flags. */
3450 if (arch == E_NDS_ARCH_STAR_V1_0)
3451 {
3452 if (config & E_NDS32_HAS_MFUSR_PC_INST)
3453 r += snprintf (buf + r, size -r, ", MFUSR_PC");
3454
3455 if (!(config & E_NDS32_HAS_NO_MAC_INST))
3456 r += snprintf (buf + r, size -r, ", MAC");
3457
3458 if (config & E_NDS32_HAS_DIV_INST)
3459 r += snprintf (buf + r, size -r, ", DIV");
3460
3461 if (config & E_NDS32_HAS_16BIT_INST)
3462 r += snprintf (buf + r, size -r, ", 16b");
3463 }
3464 else
3465 {
3466 if (config & E_NDS32_HAS_MFUSR_PC_INST)
3467 {
3468 if (version <= E_NDS32_ELF_VER_1_3)
3469 r += snprintf (buf + r, size -r, ", [B8]");
3470 else
3471 r += snprintf (buf + r, size -r, ", EX9");
3472 }
3473
3474 if (config & E_NDS32_HAS_MAC_DX_INST)
3475 r += snprintf (buf + r, size -r, ", MAC_DX");
3476
3477 if (config & E_NDS32_HAS_DIV_DX_INST)
3478 r += snprintf (buf + r, size -r, ", DIV_DX");
3479
3480 if (config & E_NDS32_HAS_16BIT_INST)
3481 {
3482 if (version <= E_NDS32_ELF_VER_1_3)
3483 r += snprintf (buf + r, size -r, ", 16b");
3484 else
3485 r += snprintf (buf + r, size -r, ", IFC");
3486 }
3487 }
3488
3489 if (config & E_NDS32_HAS_EXT_INST)
3490 r += snprintf (buf + r, size -r, ", PERF1");
3491
3492 if (config & E_NDS32_HAS_EXT2_INST)
3493 r += snprintf (buf + r, size -r, ", PERF2");
3494
3495 if (config & E_NDS32_HAS_FPU_INST)
3496 {
3497 has_fpu = true;
3498 r += snprintf (buf + r, size -r, ", FPU_SP");
3499 }
3500
3501 if (config & E_NDS32_HAS_FPU_DP_INST)
3502 {
3503 has_fpu = true;
3504 r += snprintf (buf + r, size -r, ", FPU_DP");
3505 }
3506
3507 if (config & E_NDS32_HAS_FPU_MAC_INST)
3508 {
3509 has_fpu = true;
3510 r += snprintf (buf + r, size -r, ", FPU_MAC");
3511 }
3512
3513 if (has_fpu)
3514 {
3515 switch ((config & E_NDS32_FPU_REG_CONF) >> E_NDS32_FPU_REG_CONF_SHIFT)
3516 {
3517 case E_NDS32_FPU_REG_8SP_4DP:
3518 r += snprintf (buf + r, size -r, ", FPU_REG:8/4");
3519 break;
3520 case E_NDS32_FPU_REG_16SP_8DP:
3521 r += snprintf (buf + r, size -r, ", FPU_REG:16/8");
3522 break;
3523 case E_NDS32_FPU_REG_32SP_16DP:
3524 r += snprintf (buf + r, size -r, ", FPU_REG:32/16");
3525 break;
3526 case E_NDS32_FPU_REG_32SP_32DP:
3527 r += snprintf (buf + r, size -r, ", FPU_REG:32/32");
3528 break;
3529 }
3530 }
3531
3532 if (config & E_NDS32_HAS_AUDIO_INST)
3533 r += snprintf (buf + r, size -r, ", AUDIO");
3534
3535 if (config & E_NDS32_HAS_STRING_INST)
3536 r += snprintf (buf + r, size -r, ", STR");
3537
3538 if (config & E_NDS32_HAS_REDUCED_REGS)
3539 r += snprintf (buf + r, size -r, ", 16REG");
3540
3541 if (config & E_NDS32_HAS_VIDEO_INST)
3542 {
3543 if (version <= E_NDS32_ELF_VER_1_3)
3544 r += snprintf (buf + r, size -r, ", VIDEO");
3545 else
3546 r += snprintf (buf + r, size -r, ", SATURATION");
3547 }
3548
3549 if (config & E_NDS32_HAS_ENCRIPT_INST)
3550 r += snprintf (buf + r, size -r, ", ENCRP");
3551
3552 if (config & E_NDS32_HAS_L2C_INST)
3553 r += snprintf (buf + r, size -r, ", L2C");
3554 }
3555
3556 static void
3557 decode_AMDGPU_machine_flags (Filedata *filedata, unsigned int e_flags,
3558 char *buf)
3559 {
3560 unsigned char *e_ident = filedata->file_header.e_ident;
3561 unsigned char osabi = e_ident[EI_OSABI];
3562 unsigned char abiversion = e_ident[EI_ABIVERSION];
3563 unsigned int mach;
3564
3565 /* HSA OS ABI v2 used a different encoding, but we don't need to support it,
3566 it has been deprecated for a while.
3567
3568 The PAL, MESA3D and NONE OS ABIs are not properly versioned, at the time
3569 of writing, they use the same flags as HSA v3, so the code below uses that
3570 assumption. */
3571 if (osabi == ELFOSABI_AMDGPU_HSA && abiversion < ELFABIVERSION_AMDGPU_HSA_V3)
3572 return;
3573
3574 mach = e_flags & EF_AMDGPU_MACH;
3575 switch (mach)
3576 {
3577 #define AMDGPU_CASE(code, string) \
3578 case code: strcat (buf, ", " string); break;
3579 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX600, "gfx600")
3580 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX601, "gfx601")
3581 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX700, "gfx700")
3582 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX701, "gfx701")
3583 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX702, "gfx702")
3584 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX703, "gfx703")
3585 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX704, "gfx704")
3586 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX801, "gfx801")
3587 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX802, "gfx802")
3588 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX803, "gfx803")
3589 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX810, "gfx810")
3590 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX900, "gfx900")
3591 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX902, "gfx902")
3592 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX904, "gfx904")
3593 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX906, "gfx906")
3594 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX908, "gfx908")
3595 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX909, "gfx909")
3596 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX90C, "gfx90c")
3597 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1010, "gfx1010")
3598 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1011, "gfx1011")
3599 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1012, "gfx1012")
3600 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1030, "gfx1030")
3601 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1031, "gfx1031")
3602 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1032, "gfx1032")
3603 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1033, "gfx1033")
3604 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX602, "gfx602")
3605 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX705, "gfx705")
3606 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX805, "gfx805")
3607 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1035, "gfx1035")
3608 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1034, "gfx1034")
3609 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX90A, "gfx90a")
3610 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX940, "gfx940")
3611 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1013, "gfx1013")
3612 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1036, "gfx1036")
3613 default:
3614 sprintf (buf, _(", <unknown AMDGPU GPU type: %#x>"), mach);
3615 break;
3616 #undef AMDGPU_CASE
3617 }
3618
3619 buf += strlen (buf);
3620 e_flags &= ~EF_AMDGPU_MACH;
3621
3622 if ((osabi == ELFOSABI_AMDGPU_HSA
3623 && abiversion == ELFABIVERSION_AMDGPU_HSA_V3)
3624 || osabi != ELFOSABI_AMDGPU_HSA)
3625 {
3626 /* For HSA v3 and other OS ABIs. */
3627 if (e_flags & EF_AMDGPU_FEATURE_XNACK_V3)
3628 {
3629 strcat (buf, ", xnack on");
3630 buf += strlen (buf);
3631 e_flags &= ~EF_AMDGPU_FEATURE_XNACK_V3;
3632 }
3633
3634 if (e_flags & EF_AMDGPU_FEATURE_SRAMECC_V3)
3635 {
3636 strcat (buf, ", sramecc on");
3637 buf += strlen (buf);
3638 e_flags &= ~EF_AMDGPU_FEATURE_SRAMECC_V3;
3639 }
3640 }
3641 else
3642 {
3643 /* For HSA v4+. */
3644 int xnack, sramecc;
3645
3646 xnack = e_flags & EF_AMDGPU_FEATURE_XNACK_V4;
3647 switch (xnack)
3648 {
3649 case EF_AMDGPU_FEATURE_XNACK_UNSUPPORTED_V4:
3650 break;
3651
3652 case EF_AMDGPU_FEATURE_XNACK_ANY_V4:
3653 strcat (buf, ", xnack any");
3654 break;
3655
3656 case EF_AMDGPU_FEATURE_XNACK_OFF_V4:
3657 strcat (buf, ", xnack off");
3658 break;
3659
3660 case EF_AMDGPU_FEATURE_XNACK_ON_V4:
3661 strcat (buf, ", xnack on");
3662 break;
3663
3664 default:
3665 sprintf (buf, _(", <unknown xnack value: %#x>"), xnack);
3666 break;
3667 }
3668
3669 buf += strlen (buf);
3670 e_flags &= ~EF_AMDGPU_FEATURE_XNACK_V4;
3671
3672 sramecc = e_flags & EF_AMDGPU_FEATURE_SRAMECC_V4;
3673 switch (sramecc)
3674 {
3675 case EF_AMDGPU_FEATURE_SRAMECC_UNSUPPORTED_V4:
3676 break;
3677
3678 case EF_AMDGPU_FEATURE_SRAMECC_ANY_V4:
3679 strcat (buf, ", sramecc any");
3680 break;
3681
3682 case EF_AMDGPU_FEATURE_SRAMECC_OFF_V4:
3683 strcat (buf, ", sramecc off");
3684 break;
3685
3686 case EF_AMDGPU_FEATURE_SRAMECC_ON_V4:
3687 strcat (buf, ", sramecc on");
3688 break;
3689
3690 default:
3691 sprintf (buf, _(", <unknown sramecc value: %#x>"), sramecc);
3692 break;
3693 }
3694
3695 buf += strlen (buf);
3696 e_flags &= ~EF_AMDGPU_FEATURE_SRAMECC_V4;
3697 }
3698
3699 if (e_flags != 0)
3700 sprintf (buf, _(", unknown flags bits: %#x"), e_flags);
3701 }
3702
3703 static char *
3704 get_machine_flags (Filedata * filedata, unsigned e_flags, unsigned e_machine)
3705 {
3706 static char buf[1024];
3707
3708 buf[0] = '\0';
3709
3710 if (e_flags)
3711 {
3712 switch (e_machine)
3713 {
3714 default:
3715 break;
3716
3717 case EM_ARC_COMPACT2:
3718 case EM_ARC_COMPACT:
3719 decode_ARC_machine_flags (e_flags, e_machine, buf);
3720 break;
3721
3722 case EM_ARM:
3723 decode_ARM_machine_flags (e_flags, buf);
3724 break;
3725
3726 case EM_AVR:
3727 decode_AVR_machine_flags (e_flags, buf, sizeof buf);
3728 break;
3729
3730 case EM_BLACKFIN:
3731 if (e_flags & EF_BFIN_PIC)
3732 strcat (buf, ", PIC");
3733
3734 if (e_flags & EF_BFIN_FDPIC)
3735 strcat (buf, ", FDPIC");
3736
3737 if (e_flags & EF_BFIN_CODE_IN_L1)
3738 strcat (buf, ", code in L1");
3739
3740 if (e_flags & EF_BFIN_DATA_IN_L1)
3741 strcat (buf, ", data in L1");
3742
3743 break;
3744
3745 case EM_CYGNUS_FRV:
3746 switch (e_flags & EF_FRV_CPU_MASK)
3747 {
3748 case EF_FRV_CPU_GENERIC:
3749 break;
3750
3751 default:
3752 strcat (buf, ", fr???");
3753 break;
3754
3755 case EF_FRV_CPU_FR300:
3756 strcat (buf, ", fr300");
3757 break;
3758
3759 case EF_FRV_CPU_FR400:
3760 strcat (buf, ", fr400");
3761 break;
3762 case EF_FRV_CPU_FR405:
3763 strcat (buf, ", fr405");
3764 break;
3765
3766 case EF_FRV_CPU_FR450:
3767 strcat (buf, ", fr450");
3768 break;
3769
3770 case EF_FRV_CPU_FR500:
3771 strcat (buf, ", fr500");
3772 break;
3773 case EF_FRV_CPU_FR550:
3774 strcat (buf, ", fr550");
3775 break;
3776
3777 case EF_FRV_CPU_SIMPLE:
3778 strcat (buf, ", simple");
3779 break;
3780 case EF_FRV_CPU_TOMCAT:
3781 strcat (buf, ", tomcat");
3782 break;
3783 }
3784 break;
3785
3786 case EM_68K:
3787 if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
3788 strcat (buf, ", m68000");
3789 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
3790 strcat (buf, ", cpu32");
3791 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
3792 strcat (buf, ", fido_a");
3793 else
3794 {
3795 char const * isa = _("unknown");
3796 char const * mac = _("unknown mac");
3797 char const * additional = NULL;
3798
3799 switch (e_flags & EF_M68K_CF_ISA_MASK)
3800 {
3801 case EF_M68K_CF_ISA_A_NODIV:
3802 isa = "A";
3803 additional = ", nodiv";
3804 break;
3805 case EF_M68K_CF_ISA_A:
3806 isa = "A";
3807 break;
3808 case EF_M68K_CF_ISA_A_PLUS:
3809 isa = "A+";
3810 break;
3811 case EF_M68K_CF_ISA_B_NOUSP:
3812 isa = "B";
3813 additional = ", nousp";
3814 break;
3815 case EF_M68K_CF_ISA_B:
3816 isa = "B";
3817 break;
3818 case EF_M68K_CF_ISA_C:
3819 isa = "C";
3820 break;
3821 case EF_M68K_CF_ISA_C_NODIV:
3822 isa = "C";
3823 additional = ", nodiv";
3824 break;
3825 }
3826 strcat (buf, ", cf, isa ");
3827 strcat (buf, isa);
3828 if (additional)
3829 strcat (buf, additional);
3830 if (e_flags & EF_M68K_CF_FLOAT)
3831 strcat (buf, ", float");
3832 switch (e_flags & EF_M68K_CF_MAC_MASK)
3833 {
3834 case 0:
3835 mac = NULL;
3836 break;
3837 case EF_M68K_CF_MAC:
3838 mac = "mac";
3839 break;
3840 case EF_M68K_CF_EMAC:
3841 mac = "emac";
3842 break;
3843 case EF_M68K_CF_EMAC_B:
3844 mac = "emac_b";
3845 break;
3846 }
3847 if (mac)
3848 {
3849 strcat (buf, ", ");
3850 strcat (buf, mac);
3851 }
3852 }
3853 break;
3854
3855 case EM_AMDGPU:
3856 decode_AMDGPU_machine_flags (filedata, e_flags, buf);
3857 break;
3858
3859 case EM_CYGNUS_MEP:
3860 switch (e_flags & EF_MEP_CPU_MASK)
3861 {
3862 case EF_MEP_CPU_MEP: strcat (buf, ", generic MeP"); break;
3863 case EF_MEP_CPU_C2: strcat (buf, ", MeP C2"); break;
3864 case EF_MEP_CPU_C3: strcat (buf, ", MeP C3"); break;
3865 case EF_MEP_CPU_C4: strcat (buf, ", MeP C4"); break;
3866 case EF_MEP_CPU_C5: strcat (buf, ", MeP C5"); break;
3867 case EF_MEP_CPU_H1: strcat (buf, ", MeP H1"); break;
3868 default: strcat (buf, _(", <unknown MeP cpu type>")); break;
3869 }
3870
3871 switch (e_flags & EF_MEP_COP_MASK)
3872 {
3873 case EF_MEP_COP_NONE: break;
3874 case EF_MEP_COP_AVC: strcat (buf, ", AVC coprocessor"); break;
3875 case EF_MEP_COP_AVC2: strcat (buf, ", AVC2 coprocessor"); break;
3876 case EF_MEP_COP_FMAX: strcat (buf, ", FMAX coprocessor"); break;
3877 case EF_MEP_COP_IVC2: strcat (buf, ", IVC2 coprocessor"); break;
3878 default: strcat (buf, _("<unknown MeP copro type>")); break;
3879 }
3880
3881 if (e_flags & EF_MEP_LIBRARY)
3882 strcat (buf, ", Built for Library");
3883
3884 if (e_flags & EF_MEP_INDEX_MASK)
3885 sprintf (buf + strlen (buf), ", Configuration Index: %#x",
3886 e_flags & EF_MEP_INDEX_MASK);
3887
3888 if (e_flags & ~ EF_MEP_ALL_FLAGS)
3889 sprintf (buf + strlen (buf), _(", unknown flags bits: %#x"),
3890 e_flags & ~ EF_MEP_ALL_FLAGS);
3891 break;
3892
3893 case EM_PPC:
3894 if (e_flags & EF_PPC_EMB)
3895 strcat (buf, ", emb");
3896
3897 if (e_flags & EF_PPC_RELOCATABLE)
3898 strcat (buf, _(", relocatable"));
3899
3900 if (e_flags & EF_PPC_RELOCATABLE_LIB)
3901 strcat (buf, _(", relocatable-lib"));
3902 break;
3903
3904 case EM_PPC64:
3905 if (e_flags & EF_PPC64_ABI)
3906 {
3907 char abi[] = ", abiv0";
3908
3909 abi[6] += e_flags & EF_PPC64_ABI;
3910 strcat (buf, abi);
3911 }
3912 break;
3913
3914 case EM_V800:
3915 if ((e_flags & EF_RH850_ABI) == EF_RH850_ABI)
3916 strcat (buf, ", RH850 ABI");
3917
3918 if (e_flags & EF_V800_850E3)
3919 strcat (buf, ", V3 architecture");
3920
3921 if ((e_flags & (EF_RH850_FPU_DOUBLE | EF_RH850_FPU_SINGLE)) == 0)
3922 strcat (buf, ", FPU not used");
3923
3924 if ((e_flags & (EF_RH850_REGMODE22 | EF_RH850_REGMODE32)) == 0)
3925 strcat (buf, ", regmode: COMMON");
3926
3927 if ((e_flags & (EF_RH850_GP_FIX | EF_RH850_GP_NOFIX)) == 0)
3928 strcat (buf, ", r4 not used");
3929
3930 if ((e_flags & (EF_RH850_EP_FIX | EF_RH850_EP_NOFIX)) == 0)
3931 strcat (buf, ", r30 not used");
3932
3933 if ((e_flags & (EF_RH850_TP_FIX | EF_RH850_TP_NOFIX)) == 0)
3934 strcat (buf, ", r5 not used");
3935
3936 if ((e_flags & (EF_RH850_REG2_RESERVE | EF_RH850_REG2_NORESERVE)) == 0)
3937 strcat (buf, ", r2 not used");
3938
3939 for (e_flags &= 0xFFFF; e_flags; e_flags &= ~ (e_flags & - e_flags))
3940 {
3941 switch (e_flags & - e_flags)
3942 {
3943 case EF_RH850_FPU_DOUBLE: strcat (buf, ", double precision FPU"); break;
3944 case EF_RH850_FPU_SINGLE: strcat (buf, ", single precision FPU"); break;
3945 case EF_RH850_REGMODE22: strcat (buf, ", regmode:22"); break;
3946 case EF_RH850_REGMODE32: strcat (buf, ", regmode:23"); break;
3947 case EF_RH850_GP_FIX: strcat (buf, ", r4 fixed"); break;
3948 case EF_RH850_GP_NOFIX: strcat (buf, ", r4 free"); break;
3949 case EF_RH850_EP_FIX: strcat (buf, ", r30 fixed"); break;
3950 case EF_RH850_EP_NOFIX: strcat (buf, ", r30 free"); break;
3951 case EF_RH850_TP_FIX: strcat (buf, ", r5 fixed"); break;
3952 case EF_RH850_TP_NOFIX: strcat (buf, ", r5 free"); break;
3953 case EF_RH850_REG2_RESERVE: strcat (buf, ", r2 fixed"); break;
3954 case EF_RH850_REG2_NORESERVE: strcat (buf, ", r2 free"); break;
3955 default: break;
3956 }
3957 }
3958 break;
3959
3960 case EM_V850:
3961 case EM_CYGNUS_V850:
3962 switch (e_flags & EF_V850_ARCH)
3963 {
3964 case E_V850E3V5_ARCH:
3965 strcat (buf, ", v850e3v5");
3966 break;
3967 case E_V850E2V3_ARCH:
3968 strcat (buf, ", v850e2v3");
3969 break;
3970 case E_V850E2_ARCH:
3971 strcat (buf, ", v850e2");
3972 break;
3973 case E_V850E1_ARCH:
3974 strcat (buf, ", v850e1");
3975 break;
3976 case E_V850E_ARCH:
3977 strcat (buf, ", v850e");
3978 break;
3979 case E_V850_ARCH:
3980 strcat (buf, ", v850");
3981 break;
3982 default:
3983 strcat (buf, _(", unknown v850 architecture variant"));
3984 break;
3985 }
3986 break;
3987
3988 case EM_M32R:
3989 case EM_CYGNUS_M32R:
3990 if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
3991 strcat (buf, ", m32r");
3992 break;
3993
3994 case EM_MIPS:
3995 case EM_MIPS_RS3_LE:
3996 if (e_flags & EF_MIPS_NOREORDER)
3997 strcat (buf, ", noreorder");
3998
3999 if (e_flags & EF_MIPS_PIC)
4000 strcat (buf, ", pic");
4001
4002 if (e_flags & EF_MIPS_CPIC)
4003 strcat (buf, ", cpic");
4004
4005 if (e_flags & EF_MIPS_UCODE)
4006 strcat (buf, ", ugen_reserved");
4007
4008 if (e_flags & EF_MIPS_ABI2)
4009 strcat (buf, ", abi2");
4010
4011 if (e_flags & EF_MIPS_OPTIONS_FIRST)
4012 strcat (buf, ", odk first");
4013
4014 if (e_flags & EF_MIPS_32BITMODE)
4015 strcat (buf, ", 32bitmode");
4016
4017 if (e_flags & EF_MIPS_NAN2008)
4018 strcat (buf, ", nan2008");
4019
4020 if (e_flags & EF_MIPS_FP64)
4021 strcat (buf, ", fp64");
4022
4023 switch ((e_flags & EF_MIPS_MACH))
4024 {
4025 case E_MIPS_MACH_3900: strcat (buf, ", 3900"); break;
4026 case E_MIPS_MACH_4010: strcat (buf, ", 4010"); break;
4027 case E_MIPS_MACH_4100: strcat (buf, ", 4100"); break;
4028 case E_MIPS_MACH_4111: strcat (buf, ", 4111"); break;
4029 case E_MIPS_MACH_4120: strcat (buf, ", 4120"); break;
4030 case E_MIPS_MACH_4650: strcat (buf, ", 4650"); break;
4031 case E_MIPS_MACH_5400: strcat (buf, ", 5400"); break;
4032 case E_MIPS_MACH_5500: strcat (buf, ", 5500"); break;
4033 case E_MIPS_MACH_5900: strcat (buf, ", 5900"); break;
4034 case E_MIPS_MACH_SB1: strcat (buf, ", sb1"); break;
4035 case E_MIPS_MACH_9000: strcat (buf, ", 9000"); break;
4036 case E_MIPS_MACH_LS2E: strcat (buf, ", loongson-2e"); break;
4037 case E_MIPS_MACH_LS2F: strcat (buf, ", loongson-2f"); break;
4038 case E_MIPS_MACH_GS464: strcat (buf, ", gs464"); break;
4039 case E_MIPS_MACH_GS464E: strcat (buf, ", gs464e"); break;
4040 case E_MIPS_MACH_GS264E: strcat (buf, ", gs264e"); break;
4041 case E_MIPS_MACH_OCTEON: strcat (buf, ", octeon"); break;
4042 case E_MIPS_MACH_OCTEON2: strcat (buf, ", octeon2"); break;
4043 case E_MIPS_MACH_OCTEON3: strcat (buf, ", octeon3"); break;
4044 case E_MIPS_MACH_XLR: strcat (buf, ", xlr"); break;
4045 case E_MIPS_MACH_IAMR2: strcat (buf, ", interaptiv-mr2"); break;
4046 case 0:
4047 /* We simply ignore the field in this case to avoid confusion:
4048 MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
4049 extension. */
4050 break;
4051 default: strcat (buf, _(", unknown CPU")); break;
4052 }
4053
4054 switch ((e_flags & EF_MIPS_ABI))
4055 {
4056 case E_MIPS_ABI_O32: strcat (buf, ", o32"); break;
4057 case E_MIPS_ABI_O64: strcat (buf, ", o64"); break;
4058 case E_MIPS_ABI_EABI32: strcat (buf, ", eabi32"); break;
4059 case E_MIPS_ABI_EABI64: strcat (buf, ", eabi64"); break;
4060 case 0:
4061 /* We simply ignore the field in this case to avoid confusion:
4062 MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
4063 This means it is likely to be an o32 file, but not for
4064 sure. */
4065 break;
4066 default: strcat (buf, _(", unknown ABI")); break;
4067 }
4068
4069 if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
4070 strcat (buf, ", mdmx");
4071
4072 if (e_flags & EF_MIPS_ARCH_ASE_M16)
4073 strcat (buf, ", mips16");
4074
4075 if (e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
4076 strcat (buf, ", micromips");
4077
4078 switch ((e_flags & EF_MIPS_ARCH))
4079 {
4080 case E_MIPS_ARCH_1: strcat (buf, ", mips1"); break;
4081 case E_MIPS_ARCH_2: strcat (buf, ", mips2"); break;
4082 case E_MIPS_ARCH_3: strcat (buf, ", mips3"); break;
4083 case E_MIPS_ARCH_4: strcat (buf, ", mips4"); break;
4084 case E_MIPS_ARCH_5: strcat (buf, ", mips5"); break;
4085 case E_MIPS_ARCH_32: strcat (buf, ", mips32"); break;
4086 case E_MIPS_ARCH_32R2: strcat (buf, ", mips32r2"); break;
4087 case E_MIPS_ARCH_32R6: strcat (buf, ", mips32r6"); break;
4088 case E_MIPS_ARCH_64: strcat (buf, ", mips64"); break;
4089 case E_MIPS_ARCH_64R2: strcat (buf, ", mips64r2"); break;
4090 case E_MIPS_ARCH_64R6: strcat (buf, ", mips64r6"); break;
4091 default: strcat (buf, _(", unknown ISA")); break;
4092 }
4093 break;
4094
4095 case EM_NDS32:
4096 decode_NDS32_machine_flags (e_flags, buf, sizeof buf);
4097 break;
4098
4099 case EM_NFP:
4100 switch (EF_NFP_MACH (e_flags))
4101 {
4102 case E_NFP_MACH_3200:
4103 strcat (buf, ", NFP-32xx");
4104 break;
4105 case E_NFP_MACH_6000:
4106 strcat (buf, ", NFP-6xxx");
4107 break;
4108 }
4109 break;
4110
4111 case EM_RISCV:
4112 if (e_flags & EF_RISCV_RVC)
4113 strcat (buf, ", RVC");
4114
4115 if (e_flags & EF_RISCV_RVE)
4116 strcat (buf, ", RVE");
4117
4118 if (e_flags & EF_RISCV_TSO)
4119 strcat (buf, ", TSO");
4120
4121 switch (e_flags & EF_RISCV_FLOAT_ABI)
4122 {
4123 case EF_RISCV_FLOAT_ABI_SOFT:
4124 strcat (buf, ", soft-float ABI");
4125 break;
4126
4127 case EF_RISCV_FLOAT_ABI_SINGLE:
4128 strcat (buf, ", single-float ABI");
4129 break;
4130
4131 case EF_RISCV_FLOAT_ABI_DOUBLE:
4132 strcat (buf, ", double-float ABI");
4133 break;
4134
4135 case EF_RISCV_FLOAT_ABI_QUAD:
4136 strcat (buf, ", quad-float ABI");
4137 break;
4138 }
4139 break;
4140
4141 case EM_SH:
4142 switch ((e_flags & EF_SH_MACH_MASK))
4143 {
4144 case EF_SH1: strcat (buf, ", sh1"); break;
4145 case EF_SH2: strcat (buf, ", sh2"); break;
4146 case EF_SH3: strcat (buf, ", sh3"); break;
4147 case EF_SH_DSP: strcat (buf, ", sh-dsp"); break;
4148 case EF_SH3_DSP: strcat (buf, ", sh3-dsp"); break;
4149 case EF_SH4AL_DSP: strcat (buf, ", sh4al-dsp"); break;
4150 case EF_SH3E: strcat (buf, ", sh3e"); break;
4151 case EF_SH4: strcat (buf, ", sh4"); break;
4152 case EF_SH5: strcat (buf, ", sh5"); break;
4153 case EF_SH2E: strcat (buf, ", sh2e"); break;
4154 case EF_SH4A: strcat (buf, ", sh4a"); break;
4155 case EF_SH2A: strcat (buf, ", sh2a"); break;
4156 case EF_SH4_NOFPU: strcat (buf, ", sh4-nofpu"); break;
4157 case EF_SH4A_NOFPU: strcat (buf, ", sh4a-nofpu"); break;
4158 case EF_SH2A_NOFPU: strcat (buf, ", sh2a-nofpu"); break;
4159 case EF_SH3_NOMMU: strcat (buf, ", sh3-nommu"); break;
4160 case EF_SH4_NOMMU_NOFPU: strcat (buf, ", sh4-nommu-nofpu"); break;
4161 case EF_SH2A_SH4_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh4-nommu-nofpu"); break;
4162 case EF_SH2A_SH3_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh3-nommu"); break;
4163 case EF_SH2A_SH4: strcat (buf, ", sh2a-or-sh4"); break;
4164 case EF_SH2A_SH3E: strcat (buf, ", sh2a-or-sh3e"); break;
4165 default: strcat (buf, _(", unknown ISA")); break;
4166 }
4167
4168 if (e_flags & EF_SH_PIC)
4169 strcat (buf, ", pic");
4170
4171 if (e_flags & EF_SH_FDPIC)
4172 strcat (buf, ", fdpic");
4173 break;
4174
4175 case EM_OR1K:
4176 if (e_flags & EF_OR1K_NODELAY)
4177 strcat (buf, ", no delay");
4178 break;
4179
4180 case EM_SPARCV9:
4181 if (e_flags & EF_SPARC_32PLUS)
4182 strcat (buf, ", v8+");
4183
4184 if (e_flags & EF_SPARC_SUN_US1)
4185 strcat (buf, ", ultrasparcI");
4186
4187 if (e_flags & EF_SPARC_SUN_US3)
4188 strcat (buf, ", ultrasparcIII");
4189
4190 if (e_flags & EF_SPARC_HAL_R1)
4191 strcat (buf, ", halr1");
4192
4193 if (e_flags & EF_SPARC_LEDATA)
4194 strcat (buf, ", ledata");
4195
4196 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
4197 strcat (buf, ", tso");
4198
4199 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
4200 strcat (buf, ", pso");
4201
4202 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
4203 strcat (buf, ", rmo");
4204 break;
4205
4206 case EM_PARISC:
4207 switch (e_flags & EF_PARISC_ARCH)
4208 {
4209 case EFA_PARISC_1_0:
4210 strcpy (buf, ", PA-RISC 1.0");
4211 break;
4212 case EFA_PARISC_1_1:
4213 strcpy (buf, ", PA-RISC 1.1");
4214 break;
4215 case EFA_PARISC_2_0:
4216 strcpy (buf, ", PA-RISC 2.0");
4217 break;
4218 default:
4219 break;
4220 }
4221 if (e_flags & EF_PARISC_TRAPNIL)
4222 strcat (buf, ", trapnil");
4223 if (e_flags & EF_PARISC_EXT)
4224 strcat (buf, ", ext");
4225 if (e_flags & EF_PARISC_LSB)
4226 strcat (buf, ", lsb");
4227 if (e_flags & EF_PARISC_WIDE)
4228 strcat (buf, ", wide");
4229 if (e_flags & EF_PARISC_NO_KABP)
4230 strcat (buf, ", no kabp");
4231 if (e_flags & EF_PARISC_LAZYSWAP)
4232 strcat (buf, ", lazyswap");
4233 break;
4234
4235 case EM_PJ:
4236 case EM_PJ_OLD:
4237 if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
4238 strcat (buf, ", new calling convention");
4239
4240 if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
4241 strcat (buf, ", gnu calling convention");
4242 break;
4243
4244 case EM_IA_64:
4245 if ((e_flags & EF_IA_64_ABI64))
4246 strcat (buf, ", 64-bit");
4247 else
4248 strcat (buf, ", 32-bit");
4249 if ((e_flags & EF_IA_64_REDUCEDFP))
4250 strcat (buf, ", reduced fp model");
4251 if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
4252 strcat (buf, ", no function descriptors, constant gp");
4253 else if ((e_flags & EF_IA_64_CONS_GP))
4254 strcat (buf, ", constant gp");
4255 if ((e_flags & EF_IA_64_ABSOLUTE))
4256 strcat (buf, ", absolute");
4257 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
4258 {
4259 if ((e_flags & EF_IA_64_VMS_LINKAGES))
4260 strcat (buf, ", vms_linkages");
4261 switch ((e_flags & EF_IA_64_VMS_COMCOD))
4262 {
4263 case EF_IA_64_VMS_COMCOD_SUCCESS:
4264 break;
4265 case EF_IA_64_VMS_COMCOD_WARNING:
4266 strcat (buf, ", warning");
4267 break;
4268 case EF_IA_64_VMS_COMCOD_ERROR:
4269 strcat (buf, ", error");
4270 break;
4271 case EF_IA_64_VMS_COMCOD_ABORT:
4272 strcat (buf, ", abort");
4273 break;
4274 default:
4275 warn (_("Unrecognised IA64 VMS Command Code: %x\n"),
4276 e_flags & EF_IA_64_VMS_COMCOD);
4277 strcat (buf, ", <unknown>");
4278 }
4279 }
4280 break;
4281
4282 case EM_VAX:
4283 if ((e_flags & EF_VAX_NONPIC))
4284 strcat (buf, ", non-PIC");
4285 if ((e_flags & EF_VAX_DFLOAT))
4286 strcat (buf, ", D-Float");
4287 if ((e_flags & EF_VAX_GFLOAT))
4288 strcat (buf, ", G-Float");
4289 break;
4290
4291 case EM_VISIUM:
4292 if (e_flags & EF_VISIUM_ARCH_MCM)
4293 strcat (buf, ", mcm");
4294 else if (e_flags & EF_VISIUM_ARCH_MCM24)
4295 strcat (buf, ", mcm24");
4296 if (e_flags & EF_VISIUM_ARCH_GR6)
4297 strcat (buf, ", gr6");
4298 break;
4299
4300 case EM_RL78:
4301 switch (e_flags & E_FLAG_RL78_CPU_MASK)
4302 {
4303 case E_FLAG_RL78_ANY_CPU: break;
4304 case E_FLAG_RL78_G10: strcat (buf, ", G10"); break;
4305 case E_FLAG_RL78_G13: strcat (buf, ", G13"); break;
4306 case E_FLAG_RL78_G14: strcat (buf, ", G14"); break;
4307 }
4308 if (e_flags & E_FLAG_RL78_64BIT_DOUBLES)
4309 strcat (buf, ", 64-bit doubles");
4310 break;
4311
4312 case EM_RX:
4313 if (e_flags & E_FLAG_RX_64BIT_DOUBLES)
4314 strcat (buf, ", 64-bit doubles");
4315 if (e_flags & E_FLAG_RX_DSP)
4316 strcat (buf, ", dsp");
4317 if (e_flags & E_FLAG_RX_PID)
4318 strcat (buf, ", pid");
4319 if (e_flags & E_FLAG_RX_ABI)
4320 strcat (buf, ", RX ABI");
4321 if (e_flags & E_FLAG_RX_SINSNS_SET)
4322 strcat (buf, e_flags & E_FLAG_RX_SINSNS_YES
4323 ? ", uses String instructions" : ", bans String instructions");
4324 if (e_flags & E_FLAG_RX_V2)
4325 strcat (buf, ", V2");
4326 if (e_flags & E_FLAG_RX_V3)
4327 strcat (buf, ", V3");
4328 break;
4329
4330 case EM_S390:
4331 if (e_flags & EF_S390_HIGH_GPRS)
4332 strcat (buf, ", highgprs");
4333 break;
4334
4335 case EM_TI_C6000:
4336 if ((e_flags & EF_C6000_REL))
4337 strcat (buf, ", relocatable module");
4338 break;
4339
4340 case EM_MSP430:
4341 strcat (buf, _(": architecture variant: "));
4342 switch (e_flags & EF_MSP430_MACH)
4343 {
4344 case E_MSP430_MACH_MSP430x11: strcat (buf, "MSP430x11"); break;
4345 case E_MSP430_MACH_MSP430x11x1 : strcat (buf, "MSP430x11x1 "); break;
4346 case E_MSP430_MACH_MSP430x12: strcat (buf, "MSP430x12"); break;
4347 case E_MSP430_MACH_MSP430x13: strcat (buf, "MSP430x13"); break;
4348 case E_MSP430_MACH_MSP430x14: strcat (buf, "MSP430x14"); break;
4349 case E_MSP430_MACH_MSP430x15: strcat (buf, "MSP430x15"); break;
4350 case E_MSP430_MACH_MSP430x16: strcat (buf, "MSP430x16"); break;
4351 case E_MSP430_MACH_MSP430x31: strcat (buf, "MSP430x31"); break;
4352 case E_MSP430_MACH_MSP430x32: strcat (buf, "MSP430x32"); break;
4353 case E_MSP430_MACH_MSP430x33: strcat (buf, "MSP430x33"); break;
4354 case E_MSP430_MACH_MSP430x41: strcat (buf, "MSP430x41"); break;
4355 case E_MSP430_MACH_MSP430x42: strcat (buf, "MSP430x42"); break;
4356 case E_MSP430_MACH_MSP430x43: strcat (buf, "MSP430x43"); break;
4357 case E_MSP430_MACH_MSP430x44: strcat (buf, "MSP430x44"); break;
4358 case E_MSP430_MACH_MSP430X : strcat (buf, "MSP430X"); break;
4359 default:
4360 strcat (buf, _(": unknown")); break;
4361 }
4362
4363 if (e_flags & ~ EF_MSP430_MACH)
4364 strcat (buf, _(": unknown extra flag bits also present"));
4365 break;
4366
4367 case EM_Z80:
4368 switch (e_flags & EF_Z80_MACH_MSK)
4369 {
4370 case EF_Z80_MACH_Z80: strcat (buf, ", Z80"); break;
4371 case EF_Z80_MACH_Z180: strcat (buf, ", Z180"); break;
4372 case EF_Z80_MACH_R800: strcat (buf, ", R800"); break;
4373 case EF_Z80_MACH_EZ80_Z80: strcat (buf, ", EZ80"); break;
4374 case EF_Z80_MACH_EZ80_ADL: strcat (buf, ", EZ80, ADL"); break;
4375 case EF_Z80_MACH_GBZ80: strcat (buf, ", GBZ80"); break;
4376 case EF_Z80_MACH_Z80N: strcat (buf, ", Z80N"); break;
4377 default:
4378 strcat (buf, _(", unknown")); break;
4379 }
4380 break;
4381 case EM_LOONGARCH:
4382 if (EF_LOONGARCH_IS_SOFT_FLOAT (e_flags))
4383 strcat (buf, ", SOFT-FLOAT");
4384 else if (EF_LOONGARCH_IS_SINGLE_FLOAT (e_flags))
4385 strcat (buf, ", SINGLE-FLOAT");
4386 else if (EF_LOONGARCH_IS_DOUBLE_FLOAT (e_flags))
4387 strcat (buf, ", DOUBLE-FLOAT");
4388
4389 if (EF_LOONGARCH_IS_OBJ_V0 (e_flags))
4390 strcat (buf, ", OBJ-v0");
4391 else if (EF_LOONGARCH_IS_OBJ_V1 (e_flags))
4392 strcat (buf, ", OBJ-v1");
4393
4394 break;
4395 }
4396 }
4397
4398 return buf;
4399 }
4400
4401 static const char *
4402 get_osabi_name (Filedata * filedata, unsigned int osabi)
4403 {
4404 static char buff[32];
4405
4406 switch (osabi)
4407 {
4408 case ELFOSABI_NONE: return "UNIX - System V";
4409 case ELFOSABI_HPUX: return "UNIX - HP-UX";
4410 case ELFOSABI_NETBSD: return "UNIX - NetBSD";
4411 case ELFOSABI_GNU: return "UNIX - GNU";
4412 case ELFOSABI_SOLARIS: return "UNIX - Solaris";
4413 case ELFOSABI_AIX: return "UNIX - AIX";
4414 case ELFOSABI_IRIX: return "UNIX - IRIX";
4415 case ELFOSABI_FREEBSD: return "UNIX - FreeBSD";
4416 case ELFOSABI_TRU64: return "UNIX - TRU64";
4417 case ELFOSABI_MODESTO: return "Novell - Modesto";
4418 case ELFOSABI_OPENBSD: return "UNIX - OpenBSD";
4419 case ELFOSABI_OPENVMS: return "VMS - OpenVMS";
4420 case ELFOSABI_NSK: return "HP - Non-Stop Kernel";
4421 case ELFOSABI_AROS: return "AROS";
4422 case ELFOSABI_FENIXOS: return "FenixOS";
4423 case ELFOSABI_CLOUDABI: return "Nuxi CloudABI";
4424 case ELFOSABI_OPENVOS: return "Stratus Technologies OpenVOS";
4425 default:
4426 if (osabi >= 64)
4427 switch (filedata->file_header.e_machine)
4428 {
4429 case EM_AMDGPU:
4430 switch (osabi)
4431 {
4432 case ELFOSABI_AMDGPU_HSA: return "AMD HSA";
4433 case ELFOSABI_AMDGPU_PAL: return "AMD PAL";
4434 case ELFOSABI_AMDGPU_MESA3D: return "AMD Mesa3D";
4435 default:
4436 break;
4437 }
4438 break;
4439
4440 case EM_ARM:
4441 switch (osabi)
4442 {
4443 case ELFOSABI_ARM: return "ARM";
4444 case ELFOSABI_ARM_FDPIC: return "ARM FDPIC";
4445 default:
4446 break;
4447 }
4448 break;
4449
4450 case EM_MSP430:
4451 case EM_MSP430_OLD:
4452 case EM_VISIUM:
4453 switch (osabi)
4454 {
4455 case ELFOSABI_STANDALONE: return _("Standalone App");
4456 default:
4457 break;
4458 }
4459 break;
4460
4461 case EM_TI_C6000:
4462 switch (osabi)
4463 {
4464 case ELFOSABI_C6000_ELFABI: return _("Bare-metal C6000");
4465 case ELFOSABI_C6000_LINUX: return "Linux C6000";
4466 default:
4467 break;
4468 }
4469 break;
4470
4471 default:
4472 break;
4473 }
4474 snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
4475 return buff;
4476 }
4477 }
4478
4479 static const char *
4480 get_aarch64_segment_type (unsigned long type)
4481 {
4482 switch (type)
4483 {
4484 case PT_AARCH64_ARCHEXT: return "AARCH64_ARCHEXT";
4485 case PT_AARCH64_MEMTAG_MTE: return "AARCH64_MEMTAG_MTE";
4486 default: return NULL;
4487 }
4488 }
4489
4490 static const char *
4491 get_arm_segment_type (unsigned long type)
4492 {
4493 switch (type)
4494 {
4495 case PT_ARM_EXIDX: return "EXIDX";
4496 default: return NULL;
4497 }
4498 }
4499
4500 static const char *
4501 get_s390_segment_type (unsigned long type)
4502 {
4503 switch (type)
4504 {
4505 case PT_S390_PGSTE: return "S390_PGSTE";
4506 default: return NULL;
4507 }
4508 }
4509
4510 static const char *
4511 get_mips_segment_type (unsigned long type)
4512 {
4513 switch (type)
4514 {
4515 case PT_MIPS_REGINFO: return "REGINFO";
4516 case PT_MIPS_RTPROC: return "RTPROC";
4517 case PT_MIPS_OPTIONS: return "OPTIONS";
4518 case PT_MIPS_ABIFLAGS: return "ABIFLAGS";
4519 default: return NULL;
4520 }
4521 }
4522
4523 static const char *
4524 get_parisc_segment_type (unsigned long type)
4525 {
4526 switch (type)
4527 {
4528 case PT_PARISC_ARCHEXT: return "PARISC_ARCHEXT";
4529 case PT_PARISC_UNWIND: return "PARISC_UNWIND";
4530 case PT_PARISC_WEAKORDER: return "PARISC_WEAKORDER";
4531 default: return NULL;
4532 }
4533 }
4534
4535 static const char *
4536 get_ia64_segment_type (unsigned long type)
4537 {
4538 switch (type)
4539 {
4540 case PT_IA_64_ARCHEXT: return "IA_64_ARCHEXT";
4541 case PT_IA_64_UNWIND: return "IA_64_UNWIND";
4542 default: return NULL;
4543 }
4544 }
4545
4546 static const char *
4547 get_tic6x_segment_type (unsigned long type)
4548 {
4549 switch (type)
4550 {
4551 case PT_C6000_PHATTR: return "C6000_PHATTR";
4552 default: return NULL;
4553 }
4554 }
4555
4556 static const char *
4557 get_riscv_segment_type (unsigned long type)
4558 {
4559 switch (type)
4560 {
4561 case PT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
4562 default: return NULL;
4563 }
4564 }
4565
4566 static const char *
4567 get_hpux_segment_type (unsigned long type, unsigned e_machine)
4568 {
4569 if (e_machine == EM_PARISC)
4570 switch (type)
4571 {
4572 case PT_HP_TLS: return "HP_TLS";
4573 case PT_HP_CORE_NONE: return "HP_CORE_NONE";
4574 case PT_HP_CORE_VERSION: return "HP_CORE_VERSION";
4575 case PT_HP_CORE_KERNEL: return "HP_CORE_KERNEL";
4576 case PT_HP_CORE_COMM: return "HP_CORE_COMM";
4577 case PT_HP_CORE_PROC: return "HP_CORE_PROC";
4578 case PT_HP_CORE_LOADABLE: return "HP_CORE_LOADABLE";
4579 case PT_HP_CORE_STACK: return "HP_CORE_STACK";
4580 case PT_HP_CORE_SHM: return "HP_CORE_SHM";
4581 case PT_HP_CORE_MMF: return "HP_CORE_MMF";
4582 case PT_HP_PARALLEL: return "HP_PARALLEL";
4583 case PT_HP_FASTBIND: return "HP_FASTBIND";
4584 case PT_HP_OPT_ANNOT: return "HP_OPT_ANNOT";
4585 case PT_HP_HSL_ANNOT: return "HP_HSL_ANNOT";
4586 case PT_HP_STACK: return "HP_STACK";
4587 case PT_HP_CORE_UTSNAME: return "HP_CORE_UTSNAME";
4588 default: return NULL;
4589 }
4590
4591 if (e_machine == EM_IA_64)
4592 switch (type)
4593 {
4594 case PT_HP_TLS: return "HP_TLS";
4595 case PT_IA_64_HP_OPT_ANOT: return "HP_OPT_ANNOT";
4596 case PT_IA_64_HP_HSL_ANOT: return "HP_HSL_ANNOT";
4597 case PT_IA_64_HP_STACK: return "HP_STACK";
4598 default: return NULL;
4599 }
4600
4601 return NULL;
4602 }
4603
4604 static const char *
4605 get_solaris_segment_type (unsigned long type)
4606 {
4607 switch (type)
4608 {
4609 case 0x6464e550: return "PT_SUNW_UNWIND";
4610 case 0x6474e550: return "PT_SUNW_EH_FRAME";
4611 case 0x6ffffff7: return "PT_LOSUNW";
4612 case 0x6ffffffa: return "PT_SUNWBSS";
4613 case 0x6ffffffb: return "PT_SUNWSTACK";
4614 case 0x6ffffffc: return "PT_SUNWDTRACE";
4615 case 0x6ffffffd: return "PT_SUNWCAP";
4616 case 0x6fffffff: return "PT_HISUNW";
4617 default: return NULL;
4618 }
4619 }
4620
4621 static const char *
4622 get_segment_type (Filedata * filedata, unsigned long p_type)
4623 {
4624 static char buff[32];
4625
4626 switch (p_type)
4627 {
4628 case PT_NULL: return "NULL";
4629 case PT_LOAD: return "LOAD";
4630 case PT_DYNAMIC: return "DYNAMIC";
4631 case PT_INTERP: return "INTERP";
4632 case PT_NOTE: return "NOTE";
4633 case PT_SHLIB: return "SHLIB";
4634 case PT_PHDR: return "PHDR";
4635 case PT_TLS: return "TLS";
4636 case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
4637 case PT_GNU_STACK: return "GNU_STACK";
4638 case PT_GNU_RELRO: return "GNU_RELRO";
4639 case PT_GNU_PROPERTY: return "GNU_PROPERTY";
4640 case PT_GNU_SFRAME: return "GNU_SFRAME";
4641
4642 case PT_OPENBSD_RANDOMIZE: return "OPENBSD_RANDOMIZE";
4643 case PT_OPENBSD_WXNEEDED: return "OPENBSD_WXNEEDED";
4644 case PT_OPENBSD_BOOTDATA: return "OPENBSD_BOOTDATA";
4645
4646 default:
4647 if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
4648 {
4649 const char * result;
4650
4651 switch (filedata->file_header.e_machine)
4652 {
4653 case EM_AARCH64:
4654 result = get_aarch64_segment_type (p_type);
4655 break;
4656 case EM_ARM:
4657 result = get_arm_segment_type (p_type);
4658 break;
4659 case EM_MIPS:
4660 case EM_MIPS_RS3_LE:
4661 result = get_mips_segment_type (p_type);
4662 break;
4663 case EM_PARISC:
4664 result = get_parisc_segment_type (p_type);
4665 break;
4666 case EM_IA_64:
4667 result = get_ia64_segment_type (p_type);
4668 break;
4669 case EM_TI_C6000:
4670 result = get_tic6x_segment_type (p_type);
4671 break;
4672 case EM_S390:
4673 case EM_S390_OLD:
4674 result = get_s390_segment_type (p_type);
4675 break;
4676 case EM_RISCV:
4677 result = get_riscv_segment_type (p_type);
4678 break;
4679 default:
4680 result = NULL;
4681 break;
4682 }
4683
4684 if (result != NULL)
4685 return result;
4686
4687 sprintf (buff, "LOPROC+%#lx", p_type - PT_LOPROC);
4688 }
4689 else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
4690 {
4691 const char * result = NULL;
4692
4693 switch (filedata->file_header.e_ident[EI_OSABI])
4694 {
4695 case ELFOSABI_GNU:
4696 case ELFOSABI_FREEBSD:
4697 if (p_type >= PT_GNU_MBIND_LO && p_type <= PT_GNU_MBIND_HI)
4698 {
4699 sprintf (buff, "GNU_MBIND+%#lx", p_type - PT_GNU_MBIND_LO);
4700 result = buff;
4701 }
4702 break;
4703 case ELFOSABI_HPUX:
4704 result = get_hpux_segment_type (p_type,
4705 filedata->file_header.e_machine);
4706 break;
4707 case ELFOSABI_SOLARIS:
4708 result = get_solaris_segment_type (p_type);
4709 break;
4710 default:
4711 break;
4712 }
4713 if (result != NULL)
4714 return result;
4715
4716 sprintf (buff, "LOOS+%#lx", p_type - PT_LOOS);
4717 }
4718 else
4719 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
4720
4721 return buff;
4722 }
4723 }
4724
4725 static const char *
4726 get_arc_section_type_name (unsigned int sh_type)
4727 {
4728 switch (sh_type)
4729 {
4730 case SHT_ARC_ATTRIBUTES: return "ARC_ATTRIBUTES";
4731 default:
4732 break;
4733 }
4734 return NULL;
4735 }
4736
4737 static const char *
4738 get_mips_section_type_name (unsigned int sh_type)
4739 {
4740 switch (sh_type)
4741 {
4742 case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
4743 case SHT_MIPS_MSYM: return "MIPS_MSYM";
4744 case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
4745 case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
4746 case SHT_MIPS_UCODE: return "MIPS_UCODE";
4747 case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
4748 case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
4749 case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
4750 case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
4751 case SHT_MIPS_RELD: return "MIPS_RELD";
4752 case SHT_MIPS_IFACE: return "MIPS_IFACE";
4753 case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
4754 case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
4755 case SHT_MIPS_SHDR: return "MIPS_SHDR";
4756 case SHT_MIPS_FDESC: return "MIPS_FDESC";
4757 case SHT_MIPS_EXTSYM: return "MIPS_EXTSYM";
4758 case SHT_MIPS_DENSE: return "MIPS_DENSE";
4759 case SHT_MIPS_PDESC: return "MIPS_PDESC";
4760 case SHT_MIPS_LOCSYM: return "MIPS_LOCSYM";
4761 case SHT_MIPS_AUXSYM: return "MIPS_AUXSYM";
4762 case SHT_MIPS_OPTSYM: return "MIPS_OPTSYM";
4763 case SHT_MIPS_LOCSTR: return "MIPS_LOCSTR";
4764 case SHT_MIPS_LINE: return "MIPS_LINE";
4765 case SHT_MIPS_RFDESC: return "MIPS_RFDESC";
4766 case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
4767 case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
4768 case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
4769 case SHT_MIPS_DWARF: return "MIPS_DWARF";
4770 case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
4771 case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
4772 case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
4773 case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
4774 case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
4775 case SHT_MIPS_XLATE: return "MIPS_XLATE";
4776 case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
4777 case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
4778 case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
4779 case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
4780 case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
4781 case SHT_MIPS_ABIFLAGS: return "MIPS_ABIFLAGS";
4782 case SHT_MIPS_XHASH: return "MIPS_XHASH";
4783 default:
4784 break;
4785 }
4786 return NULL;
4787 }
4788
4789 static const char *
4790 get_parisc_section_type_name (unsigned int sh_type)
4791 {
4792 switch (sh_type)
4793 {
4794 case SHT_PARISC_EXT: return "PARISC_EXT";
4795 case SHT_PARISC_UNWIND: return "PARISC_UNWIND";
4796 case SHT_PARISC_DOC: return "PARISC_DOC";
4797 case SHT_PARISC_ANNOT: return "PARISC_ANNOT";
4798 case SHT_PARISC_SYMEXTN: return "PARISC_SYMEXTN";
4799 case SHT_PARISC_STUBS: return "PARISC_STUBS";
4800 case SHT_PARISC_DLKM: return "PARISC_DLKM";
4801 default: return NULL;
4802 }
4803 }
4804
4805 static const char *
4806 get_ia64_section_type_name (Filedata * filedata, unsigned int sh_type)
4807 {
4808 /* If the top 8 bits are 0x78 the next 8 are the os/abi ID. */
4809 if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
4810 return get_osabi_name (filedata, (sh_type & 0x00FF0000) >> 16);
4811
4812 switch (sh_type)
4813 {
4814 case SHT_IA_64_EXT: return "IA_64_EXT";
4815 case SHT_IA_64_UNWIND: return "IA_64_UNWIND";
4816 case SHT_IA_64_PRIORITY_INIT: return "IA_64_PRIORITY_INIT";
4817 case SHT_IA_64_VMS_TRACE: return "VMS_TRACE";
4818 case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
4819 case SHT_IA_64_VMS_DEBUG: return "VMS_DEBUG";
4820 case SHT_IA_64_VMS_DEBUG_STR: return "VMS_DEBUG_STR";
4821 case SHT_IA_64_VMS_LINKAGES: return "VMS_LINKAGES";
4822 case SHT_IA_64_VMS_SYMBOL_VECTOR: return "VMS_SYMBOL_VECTOR";
4823 case SHT_IA_64_VMS_FIXUP: return "VMS_FIXUP";
4824 default:
4825 break;
4826 }
4827 return NULL;
4828 }
4829
4830 static const char *
4831 get_x86_64_section_type_name (unsigned int sh_type)
4832 {
4833 switch (sh_type)
4834 {
4835 case SHT_X86_64_UNWIND: return "X86_64_UNWIND";
4836 default: return NULL;
4837 }
4838 }
4839
4840 static const char *
4841 get_aarch64_section_type_name (unsigned int sh_type)
4842 {
4843 switch (sh_type)
4844 {
4845 case SHT_AARCH64_ATTRIBUTES: return "AARCH64_ATTRIBUTES";
4846 default: return NULL;
4847 }
4848 }
4849
4850 static const char *
4851 get_arm_section_type_name (unsigned int sh_type)
4852 {
4853 switch (sh_type)
4854 {
4855 case SHT_ARM_EXIDX: return "ARM_EXIDX";
4856 case SHT_ARM_PREEMPTMAP: return "ARM_PREEMPTMAP";
4857 case SHT_ARM_ATTRIBUTES: return "ARM_ATTRIBUTES";
4858 case SHT_ARM_DEBUGOVERLAY: return "ARM_DEBUGOVERLAY";
4859 case SHT_ARM_OVERLAYSECTION: return "ARM_OVERLAYSECTION";
4860 default: return NULL;
4861 }
4862 }
4863
4864 static const char *
4865 get_tic6x_section_type_name (unsigned int sh_type)
4866 {
4867 switch (sh_type)
4868 {
4869 case SHT_C6000_UNWIND: return "C6000_UNWIND";
4870 case SHT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
4871 case SHT_C6000_ATTRIBUTES: return "C6000_ATTRIBUTES";
4872 case SHT_TI_ICODE: return "TI_ICODE";
4873 case SHT_TI_XREF: return "TI_XREF";
4874 case SHT_TI_HANDLER: return "TI_HANDLER";
4875 case SHT_TI_INITINFO: return "TI_INITINFO";
4876 case SHT_TI_PHATTRS: return "TI_PHATTRS";
4877 default: return NULL;
4878 }
4879 }
4880
4881 static const char *
4882 get_msp430_section_type_name (unsigned int sh_type)
4883 {
4884 switch (sh_type)
4885 {
4886 case SHT_MSP430_SEC_FLAGS: return "MSP430_SEC_FLAGS";
4887 case SHT_MSP430_SYM_ALIASES: return "MSP430_SYM_ALIASES";
4888 case SHT_MSP430_ATTRIBUTES: return "MSP430_ATTRIBUTES";
4889 default: return NULL;
4890 }
4891 }
4892
4893 static const char *
4894 get_nfp_section_type_name (unsigned int sh_type)
4895 {
4896 switch (sh_type)
4897 {
4898 case SHT_NFP_MECONFIG: return "NFP_MECONFIG";
4899 case SHT_NFP_INITREG: return "NFP_INITREG";
4900 case SHT_NFP_UDEBUG: return "NFP_UDEBUG";
4901 default: return NULL;
4902 }
4903 }
4904
4905 static const char *
4906 get_v850_section_type_name (unsigned int sh_type)
4907 {
4908 switch (sh_type)
4909 {
4910 case SHT_V850_SCOMMON: return "V850 Small Common";
4911 case SHT_V850_TCOMMON: return "V850 Tiny Common";
4912 case SHT_V850_ZCOMMON: return "V850 Zero Common";
4913 case SHT_RENESAS_IOP: return "RENESAS IOP";
4914 case SHT_RENESAS_INFO: return "RENESAS INFO";
4915 default: return NULL;
4916 }
4917 }
4918
4919 static const char *
4920 get_riscv_section_type_name (unsigned int sh_type)
4921 {
4922 switch (sh_type)
4923 {
4924 case SHT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
4925 default: return NULL;
4926 }
4927 }
4928
4929 static const char *
4930 get_csky_section_type_name (unsigned int sh_type)
4931 {
4932 switch (sh_type)
4933 {
4934 case SHT_CSKY_ATTRIBUTES: return "CSKY_ATTRIBUTES";
4935 default: return NULL;
4936 }
4937 }
4938
4939 static const char *
4940 get_section_type_name (Filedata * filedata, unsigned int sh_type)
4941 {
4942 static char buff[32];
4943 const char * result;
4944
4945 switch (sh_type)
4946 {
4947 case SHT_NULL: return "NULL";
4948 case SHT_PROGBITS: return "PROGBITS";
4949 case SHT_SYMTAB: return "SYMTAB";
4950 case SHT_STRTAB: return "STRTAB";
4951 case SHT_RELA: return "RELA";
4952 case SHT_RELR: return "RELR";
4953 case SHT_HASH: return "HASH";
4954 case SHT_DYNAMIC: return "DYNAMIC";
4955 case SHT_NOTE: return "NOTE";
4956 case SHT_NOBITS: return "NOBITS";
4957 case SHT_REL: return "REL";
4958 case SHT_SHLIB: return "SHLIB";
4959 case SHT_DYNSYM: return "DYNSYM";
4960 case SHT_INIT_ARRAY: return "INIT_ARRAY";
4961 case SHT_FINI_ARRAY: return "FINI_ARRAY";
4962 case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
4963 case SHT_GNU_HASH: return "GNU_HASH";
4964 case SHT_GROUP: return "GROUP";
4965 case SHT_SYMTAB_SHNDX: return "SYMTAB SECTION INDICES";
4966 case SHT_GNU_verdef: return "VERDEF";
4967 case SHT_GNU_verneed: return "VERNEED";
4968 case SHT_GNU_versym: return "VERSYM";
4969 case 0x6ffffff0: return "VERSYM";
4970 case 0x6ffffffc: return "VERDEF";
4971 case 0x7ffffffd: return "AUXILIARY";
4972 case 0x7fffffff: return "FILTER";
4973 case SHT_GNU_LIBLIST: return "GNU_LIBLIST";
4974
4975 default:
4976 if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
4977 {
4978 switch (filedata->file_header.e_machine)
4979 {
4980 case EM_ARC:
4981 case EM_ARC_COMPACT:
4982 case EM_ARC_COMPACT2:
4983 result = get_arc_section_type_name (sh_type);
4984 break;
4985 case EM_MIPS:
4986 case EM_MIPS_RS3_LE:
4987 result = get_mips_section_type_name (sh_type);
4988 break;
4989 case EM_PARISC:
4990 result = get_parisc_section_type_name (sh_type);
4991 break;
4992 case EM_IA_64:
4993 result = get_ia64_section_type_name (filedata, sh_type);
4994 break;
4995 case EM_X86_64:
4996 case EM_L1OM:
4997 case EM_K1OM:
4998 result = get_x86_64_section_type_name (sh_type);
4999 break;
5000 case EM_AARCH64:
5001 result = get_aarch64_section_type_name (sh_type);
5002 break;
5003 case EM_ARM:
5004 result = get_arm_section_type_name (sh_type);
5005 break;
5006 case EM_TI_C6000:
5007 result = get_tic6x_section_type_name (sh_type);
5008 break;
5009 case EM_MSP430:
5010 result = get_msp430_section_type_name (sh_type);
5011 break;
5012 case EM_NFP:
5013 result = get_nfp_section_type_name (sh_type);
5014 break;
5015 case EM_V800:
5016 case EM_V850:
5017 case EM_CYGNUS_V850:
5018 result = get_v850_section_type_name (sh_type);
5019 break;
5020 case EM_RISCV:
5021 result = get_riscv_section_type_name (sh_type);
5022 break;
5023 case EM_CSKY:
5024 result = get_csky_section_type_name (sh_type);
5025 break;
5026 default:
5027 result = NULL;
5028 break;
5029 }
5030
5031 if (result != NULL)
5032 return result;
5033
5034 sprintf (buff, "LOPROC+%#x", sh_type - SHT_LOPROC);
5035 }
5036 else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
5037 {
5038 switch (filedata->file_header.e_machine)
5039 {
5040 case EM_IA_64:
5041 result = get_ia64_section_type_name (filedata, sh_type);
5042 break;
5043 default:
5044 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
5045 result = get_solaris_section_type (sh_type);
5046 else
5047 {
5048 switch (sh_type)
5049 {
5050 case SHT_GNU_INCREMENTAL_INPUTS: result = "GNU_INCREMENTAL_INPUTS"; break;
5051 case SHT_GNU_ATTRIBUTES: result = "GNU_ATTRIBUTES"; break;
5052 case SHT_GNU_HASH: result = "GNU_HASH"; break;
5053 case SHT_GNU_LIBLIST: result = "GNU_LIBLIST"; break;
5054 default:
5055 result = NULL;
5056 break;
5057 }
5058 }
5059 break;
5060 }
5061
5062 if (result != NULL)
5063 return result;
5064
5065 sprintf (buff, "LOOS+%#x", sh_type - SHT_LOOS);
5066 }
5067 else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
5068 {
5069 switch (filedata->file_header.e_machine)
5070 {
5071 case EM_V800:
5072 case EM_V850:
5073 case EM_CYGNUS_V850:
5074 result = get_v850_section_type_name (sh_type);
5075 break;
5076 default:
5077 result = NULL;
5078 break;
5079 }
5080
5081 if (result != NULL)
5082 return result;
5083
5084 sprintf (buff, "LOUSER+%#x", sh_type - SHT_LOUSER);
5085 }
5086 else
5087 /* This message is probably going to be displayed in a 15
5088 character wide field, so put the hex value first. */
5089 snprintf (buff, sizeof (buff), _("%08x: <unknown>"), sh_type);
5090
5091 return buff;
5092 }
5093 }
5094
5095 enum long_option_values
5096 {
5097 OPTION_DEBUG_DUMP = 512,
5098 OPTION_DYN_SYMS,
5099 OPTION_LTO_SYMS,
5100 OPTION_DWARF_DEPTH,
5101 OPTION_DWARF_START,
5102 OPTION_DWARF_CHECK,
5103 OPTION_CTF_DUMP,
5104 OPTION_CTF_PARENT,
5105 OPTION_CTF_SYMBOLS,
5106 OPTION_CTF_STRINGS,
5107 OPTION_SFRAME_DUMP,
5108 OPTION_WITH_SYMBOL_VERSIONS,
5109 OPTION_RECURSE_LIMIT,
5110 OPTION_NO_RECURSE_LIMIT,
5111 OPTION_NO_DEMANGLING,
5112 OPTION_SYM_BASE
5113 };
5114
5115 static struct option options[] =
5116 {
5117 /* Note - This table is alpha-sorted on the 'val'
5118 field in order to make adding new options easier. */
5119 {"arch-specific", no_argument, 0, 'A'},
5120 {"all", no_argument, 0, 'a'},
5121 {"demangle", optional_argument, 0, 'C'},
5122 {"archive-index", no_argument, 0, 'c'},
5123 {"use-dynamic", no_argument, 0, 'D'},
5124 {"dynamic", no_argument, 0, 'd'},
5125 {"headers", no_argument, 0, 'e'},
5126 {"section-groups", no_argument, 0, 'g'},
5127 {"help", no_argument, 0, 'H'},
5128 {"file-header", no_argument, 0, 'h'},
5129 {"histogram", no_argument, 0, 'I'},
5130 {"lint", no_argument, 0, 'L'},
5131 {"enable-checks", no_argument, 0, 'L'},
5132 {"program-headers", no_argument, 0, 'l'},
5133 {"segments", no_argument, 0, 'l'},
5134 {"full-section-name",no_argument, 0, 'N'},
5135 {"notes", no_argument, 0, 'n'},
5136 {"process-links", no_argument, 0, 'P'},
5137 {"string-dump", required_argument, 0, 'p'},
5138 {"relocated-dump", required_argument, 0, 'R'},
5139 {"relocs", no_argument, 0, 'r'},
5140 {"section-headers", no_argument, 0, 'S'},
5141 {"sections", no_argument, 0, 'S'},
5142 {"symbols", no_argument, 0, 's'},
5143 {"syms", no_argument, 0, 's'},
5144 {"silent-truncation",no_argument, 0, 'T'},
5145 {"section-details", no_argument, 0, 't'},
5146 {"unicode", required_argument, NULL, 'U'},
5147 {"unwind", no_argument, 0, 'u'},
5148 {"version-info", no_argument, 0, 'V'},
5149 {"version", no_argument, 0, 'v'},
5150 {"wide", no_argument, 0, 'W'},
5151 {"hex-dump", required_argument, 0, 'x'},
5152 {"decompress", no_argument, 0, 'z'},
5153
5154 {"no-demangle", no_argument, 0, OPTION_NO_DEMANGLING},
5155 {"recurse-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
5156 {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
5157 {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
5158 {"dyn-syms", no_argument, 0, OPTION_DYN_SYMS},
5159 {"lto-syms", no_argument, 0, OPTION_LTO_SYMS},
5160 {"debug-dump", optional_argument, 0, OPTION_DEBUG_DUMP},
5161 {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
5162 {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
5163 {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
5164 #ifdef ENABLE_LIBCTF
5165 {"ctf", required_argument, 0, OPTION_CTF_DUMP},
5166 {"ctf-symbols", required_argument, 0, OPTION_CTF_SYMBOLS},
5167 {"ctf-strings", required_argument, 0, OPTION_CTF_STRINGS},
5168 {"ctf-parent", required_argument, 0, OPTION_CTF_PARENT},
5169 #endif
5170 {"sframe", optional_argument, 0, OPTION_SFRAME_DUMP},
5171 {"sym-base", optional_argument, 0, OPTION_SYM_BASE},
5172
5173 {0, no_argument, 0, 0}
5174 };
5175
5176 static void
5177 usage (FILE * stream)
5178 {
5179 fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
5180 fprintf (stream, _(" Display information about the contents of ELF format files\n"));
5181 fprintf (stream, _(" Options are:\n"));
5182 fprintf (stream, _("\
5183 -a --all Equivalent to: -h -l -S -s -r -d -V -A -I\n"));
5184 fprintf (stream, _("\
5185 -h --file-header Display the ELF file header\n"));
5186 fprintf (stream, _("\
5187 -l --program-headers Display the program headers\n"));
5188 fprintf (stream, _("\
5189 --segments An alias for --program-headers\n"));
5190 fprintf (stream, _("\
5191 -S --section-headers Display the sections' header\n"));
5192 fprintf (stream, _("\
5193 --sections An alias for --section-headers\n"));
5194 fprintf (stream, _("\
5195 -g --section-groups Display the section groups\n"));
5196 fprintf (stream, _("\
5197 -t --section-details Display the section details\n"));
5198 fprintf (stream, _("\
5199 -e --headers Equivalent to: -h -l -S\n"));
5200 fprintf (stream, _("\
5201 -s --syms Display the symbol table\n"));
5202 fprintf (stream, _("\
5203 --symbols An alias for --syms\n"));
5204 fprintf (stream, _("\
5205 --dyn-syms Display the dynamic symbol table\n"));
5206 fprintf (stream, _("\
5207 --lto-syms Display LTO symbol tables\n"));
5208 fprintf (stream, _("\
5209 --sym-base=[0|8|10|16] \n\
5210 Force base for symbol sizes. The options are \n\
5211 mixed (the default), octal, decimal, hexadecimal.\n"));
5212 fprintf (stream, _("\
5213 -C --demangle[=STYLE] Decode mangled/processed symbol names\n"));
5214 display_demangler_styles (stream, _("\
5215 STYLE can be "));
5216 fprintf (stream, _("\
5217 --no-demangle Do not demangle low-level symbol names. (default)\n"));
5218 fprintf (stream, _("\
5219 --recurse-limit Enable a demangling recursion limit. (default)\n"));
5220 fprintf (stream, _("\
5221 --no-recurse-limit Disable a demangling recursion limit\n"));
5222 fprintf (stream, _("\
5223 -U[dlexhi] --unicode=[default|locale|escape|hex|highlight|invalid]\n\
5224 Display unicode characters as determined by the current locale\n\
5225 (default), escape sequences, \"<hex sequences>\", highlighted\n\
5226 escape sequences, or treat them as invalid and display as\n\
5227 \"{hex sequences}\"\n"));
5228 fprintf (stream, _("\
5229 -n --notes Display the core notes (if present)\n"));
5230 fprintf (stream, _("\
5231 -r --relocs Display the relocations (if present)\n"));
5232 fprintf (stream, _("\
5233 -u --unwind Display the unwind info (if present)\n"));
5234 fprintf (stream, _("\
5235 -d --dynamic Display the dynamic section (if present)\n"));
5236 fprintf (stream, _("\
5237 -V --version-info Display the version sections (if present)\n"));
5238 fprintf (stream, _("\
5239 -A --arch-specific Display architecture specific information (if any)\n"));
5240 fprintf (stream, _("\
5241 -c --archive-index Display the symbol/file index in an archive\n"));
5242 fprintf (stream, _("\
5243 -D --use-dynamic Use the dynamic section info when displaying symbols\n"));
5244 fprintf (stream, _("\
5245 -L --lint|--enable-checks\n\
5246 Display warning messages for possible problems\n"));
5247 fprintf (stream, _("\
5248 -x --hex-dump=<number|name>\n\
5249 Dump the contents of section <number|name> as bytes\n"));
5250 fprintf (stream, _("\
5251 -p --string-dump=<number|name>\n\
5252 Dump the contents of section <number|name> as strings\n"));
5253 fprintf (stream, _("\
5254 -R --relocated-dump=<number|name>\n\
5255 Dump the relocated contents of section <number|name>\n"));
5256 fprintf (stream, _("\
5257 -z --decompress Decompress section before dumping it\n"));
5258 fprintf (stream, _("\
5259 -w --debug-dump[a/=abbrev, A/=addr, r/=aranges, c/=cu_index, L/=decodedline,\n\
5260 f/=frames, F/=frames-interp, g/=gdb_index, i/=info, o/=loc,\n\
5261 m/=macro, p/=pubnames, t/=pubtypes, R/=Ranges, l/=rawline,\n\
5262 s/=str, O/=str-offsets, u/=trace_abbrev, T/=trace_aranges,\n\
5263 U/=trace_info]\n\
5264 Display the contents of DWARF debug sections\n"));
5265 fprintf (stream, _("\
5266 -wk --debug-dump=links Display the contents of sections that link to separate\n\
5267 debuginfo files\n"));
5268 fprintf (stream, _("\
5269 -P --process-links Display the contents of non-debug sections in separate\n\
5270 debuginfo files. (Implies -wK)\n"));
5271 #if DEFAULT_FOR_FOLLOW_LINKS
5272 fprintf (stream, _("\
5273 -wK --debug-dump=follow-links\n\
5274 Follow links to separate debug info files (default)\n"));
5275 fprintf (stream, _("\
5276 -wN --debug-dump=no-follow-links\n\
5277 Do not follow links to separate debug info files\n"));
5278 #else
5279 fprintf (stream, _("\
5280 -wK --debug-dump=follow-links\n\
5281 Follow links to separate debug info files\n"));
5282 fprintf (stream, _("\
5283 -wN --debug-dump=no-follow-links\n\
5284 Do not follow links to separate debug info files\n\
5285 (default)\n"));
5286 #endif
5287 #if HAVE_LIBDEBUGINFOD
5288 fprintf (stream, _("\
5289 -wD --debug-dump=use-debuginfod\n\
5290 When following links, also query debuginfod servers (default)\n"));
5291 fprintf (stream, _("\
5292 -wE --debug-dump=do-not-use-debuginfod\n\
5293 When following links, do not query debuginfod servers\n"));
5294 #endif
5295 fprintf (stream, _("\
5296 --dwarf-depth=N Do not display DIEs at depth N or greater\n"));
5297 fprintf (stream, _("\
5298 --dwarf-start=N Display DIEs starting at offset N\n"));
5299 #ifdef ENABLE_LIBCTF
5300 fprintf (stream, _("\
5301 --ctf=<number|name> Display CTF info from section <number|name>\n"));
5302 fprintf (stream, _("\
5303 --ctf-parent=<name> Use CTF archive member <name> as the CTF parent\n"));
5304 fprintf (stream, _("\
5305 --ctf-symbols=<number|name>\n\
5306 Use section <number|name> as the CTF external symtab\n"));
5307 fprintf (stream, _("\
5308 --ctf-strings=<number|name>\n\
5309 Use section <number|name> as the CTF external strtab\n"));
5310 #endif
5311 fprintf (stream, _("\
5312 --sframe[=NAME] Display SFrame info from section NAME, (default '.sframe')\n"));
5313
5314 #ifdef SUPPORT_DISASSEMBLY
5315 fprintf (stream, _("\
5316 -i --instruction-dump=<number|name>\n\
5317 Disassemble the contents of section <number|name>\n"));
5318 #endif
5319 fprintf (stream, _("\
5320 -I --histogram Display histogram of bucket list lengths\n"));
5321 fprintf (stream, _("\
5322 -W --wide Allow output width to exceed 80 characters\n"));
5323 fprintf (stream, _("\
5324 -T --silent-truncation If a symbol name is truncated, do not add [...] suffix\n"));
5325 fprintf (stream, _("\
5326 @<file> Read options from <file>\n"));
5327 fprintf (stream, _("\
5328 -H --help Display this information\n"));
5329 fprintf (stream, _("\
5330 -v --version Display the version number of readelf\n"));
5331
5332 if (REPORT_BUGS_TO[0] && stream == stdout)
5333 fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
5334
5335 exit (stream == stdout ? 0 : 1);
5336 }
5337
5338 /* Record the fact that the user wants the contents of section number
5339 SECTION to be displayed using the method(s) encoded as flags bits
5340 in TYPE. Note, TYPE can be zero if we are creating the array for
5341 the first time. */
5342
5343 static void
5344 request_dump_bynumber (struct dump_data *dumpdata,
5345 unsigned int section, dump_type type)
5346 {
5347 if (section >= dumpdata->num_dump_sects)
5348 {
5349 dump_type * new_dump_sects;
5350
5351 new_dump_sects = (dump_type *) calloc (section + 1,
5352 sizeof (* new_dump_sects));
5353
5354 if (new_dump_sects == NULL)
5355 error (_("Out of memory allocating dump request table.\n"));
5356 else
5357 {
5358 if (dumpdata->dump_sects)
5359 {
5360 /* Copy current flag settings. */
5361 memcpy (new_dump_sects, dumpdata->dump_sects,
5362 dumpdata->num_dump_sects * sizeof (* new_dump_sects));
5363
5364 free (dumpdata->dump_sects);
5365 }
5366
5367 dumpdata->dump_sects = new_dump_sects;
5368 dumpdata->num_dump_sects = section + 1;
5369 }
5370 }
5371
5372 if (dumpdata->dump_sects)
5373 dumpdata->dump_sects[section] |= type;
5374 }
5375
5376 /* Request a dump by section name. */
5377
5378 static void
5379 request_dump_byname (const char * section, dump_type type)
5380 {
5381 struct dump_list_entry * new_request;
5382
5383 new_request = (struct dump_list_entry *)
5384 malloc (sizeof (struct dump_list_entry));
5385 if (!new_request)
5386 error (_("Out of memory allocating dump request table.\n"));
5387
5388 new_request->name = strdup (section);
5389 if (!new_request->name)
5390 error (_("Out of memory allocating dump request table.\n"));
5391
5392 new_request->type = type;
5393
5394 new_request->next = dump_sects_byname;
5395 dump_sects_byname = new_request;
5396 }
5397
5398 static inline void
5399 request_dump (struct dump_data *dumpdata, dump_type type)
5400 {
5401 int section;
5402 char * cp;
5403
5404 do_dump = true;
5405 section = strtoul (optarg, & cp, 0);
5406
5407 if (! *cp && section >= 0)
5408 request_dump_bynumber (dumpdata, section, type);
5409 else
5410 request_dump_byname (optarg, type);
5411 }
5412
5413 static void
5414 parse_args (struct dump_data *dumpdata, int argc, char ** argv)
5415 {
5416 int c;
5417
5418 if (argc < 2)
5419 usage (stderr);
5420
5421 while ((c = getopt_long
5422 (argc, argv, "ACDHILNPR:STU:VWacdeghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
5423 {
5424 switch (c)
5425 {
5426 case 0:
5427 /* Long options. */
5428 break;
5429 case 'H':
5430 usage (stdout);
5431 break;
5432
5433 case 'a':
5434 do_syms = true;
5435 do_reloc = true;
5436 do_unwind = true;
5437 do_dynamic = true;
5438 do_header = true;
5439 do_sections = true;
5440 do_section_groups = true;
5441 do_segments = true;
5442 do_version = true;
5443 do_histogram = true;
5444 do_arch = true;
5445 do_notes = true;
5446 break;
5447
5448 case 'g':
5449 do_section_groups = true;
5450 break;
5451 case 't':
5452 case 'N':
5453 do_sections = true;
5454 do_section_details = true;
5455 break;
5456 case 'e':
5457 do_header = true;
5458 do_sections = true;
5459 do_segments = true;
5460 break;
5461 case 'A':
5462 do_arch = true;
5463 break;
5464 case 'D':
5465 do_using_dynamic = true;
5466 break;
5467 case 'r':
5468 do_reloc = true;
5469 break;
5470 case 'u':
5471 do_unwind = true;
5472 break;
5473 case 'h':
5474 do_header = true;
5475 break;
5476 case 'l':
5477 do_segments = true;
5478 break;
5479 case 's':
5480 do_syms = true;
5481 break;
5482 case 'S':
5483 do_sections = true;
5484 break;
5485 case 'd':
5486 do_dynamic = true;
5487 break;
5488 case 'I':
5489 do_histogram = true;
5490 break;
5491 case 'n':
5492 do_notes = true;
5493 break;
5494 case 'c':
5495 do_archive_index = true;
5496 break;
5497 case 'L':
5498 do_checks = true;
5499 break;
5500 case 'P':
5501 process_links = true;
5502 do_follow_links = true;
5503 dump_any_debugging = true;
5504 break;
5505 case 'x':
5506 request_dump (dumpdata, HEX_DUMP);
5507 break;
5508 case 'p':
5509 request_dump (dumpdata, STRING_DUMP);
5510 break;
5511 case 'R':
5512 request_dump (dumpdata, RELOC_DUMP);
5513 break;
5514 case 'z':
5515 decompress_dumps = true;
5516 break;
5517 case 'w':
5518 if (optarg == NULL)
5519 {
5520 do_debugging = true;
5521 do_dump = true;
5522 dump_any_debugging = true;
5523 dwarf_select_sections_all ();
5524 }
5525 else
5526 {
5527 do_debugging = false;
5528 if (dwarf_select_sections_by_letters (optarg))
5529 {
5530 do_dump = true;
5531 dump_any_debugging = true;
5532 }
5533 }
5534 break;
5535 case OPTION_DEBUG_DUMP:
5536 if (optarg == NULL)
5537 {
5538 do_dump = true;
5539 do_debugging = true;
5540 dump_any_debugging = true;
5541 dwarf_select_sections_all ();
5542 }
5543 else
5544 {
5545 do_debugging = false;
5546 if (dwarf_select_sections_by_names (optarg))
5547 {
5548 do_dump = true;
5549 dump_any_debugging = true;
5550 }
5551 }
5552 break;
5553 case OPTION_DWARF_DEPTH:
5554 {
5555 char *cp;
5556
5557 dwarf_cutoff_level = strtoul (optarg, & cp, 0);
5558 }
5559 break;
5560 case OPTION_DWARF_START:
5561 {
5562 char *cp;
5563
5564 dwarf_start_die = strtoul (optarg, & cp, 0);
5565 }
5566 break;
5567 case OPTION_DWARF_CHECK:
5568 dwarf_check = true;
5569 break;
5570 case OPTION_CTF_DUMP:
5571 do_ctf = true;
5572 request_dump (dumpdata, CTF_DUMP);
5573 break;
5574 case OPTION_CTF_SYMBOLS:
5575 free (dump_ctf_symtab_name);
5576 dump_ctf_symtab_name = strdup (optarg);
5577 break;
5578 case OPTION_CTF_STRINGS:
5579 free (dump_ctf_strtab_name);
5580 dump_ctf_strtab_name = strdup (optarg);
5581 break;
5582 case OPTION_CTF_PARENT:
5583 free (dump_ctf_parent_name);
5584 dump_ctf_parent_name = strdup (optarg);
5585 break;
5586 case OPTION_SFRAME_DUMP:
5587 do_sframe = true;
5588 /* Providing section name is optional. request_dump (), however,
5589 thrives on non NULL optarg. Handle it explicitly here. */
5590 if (optarg != NULL)
5591 request_dump (dumpdata, SFRAME_DUMP);
5592 else
5593 {
5594 do_dump = true;
5595 const char *sframe_sec_name = strdup (".sframe");
5596 request_dump_byname (sframe_sec_name, SFRAME_DUMP);
5597 }
5598 break;
5599 case OPTION_DYN_SYMS:
5600 do_dyn_syms = true;
5601 break;
5602 case OPTION_LTO_SYMS:
5603 do_lto_syms = true;
5604 break;
5605 #ifdef SUPPORT_DISASSEMBLY
5606 case 'i':
5607 request_dump (dumpdata, DISASS_DUMP);
5608 break;
5609 #endif
5610 case 'v':
5611 print_version (program_name);
5612 break;
5613 case 'V':
5614 do_version = true;
5615 break;
5616 case 'W':
5617 do_wide = true;
5618 break;
5619 case 'T':
5620 do_not_show_symbol_truncation = true;
5621 break;
5622 case 'C':
5623 do_demangle = true;
5624 if (optarg != NULL)
5625 {
5626 enum demangling_styles style;
5627
5628 style = cplus_demangle_name_to_style (optarg);
5629 if (style == unknown_demangling)
5630 error (_("unknown demangling style `%s'"), optarg);
5631
5632 cplus_demangle_set_style (style);
5633 }
5634 break;
5635 case OPTION_NO_DEMANGLING:
5636 do_demangle = false;
5637 break;
5638 case OPTION_RECURSE_LIMIT:
5639 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
5640 break;
5641 case OPTION_NO_RECURSE_LIMIT:
5642 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
5643 break;
5644 case OPTION_WITH_SYMBOL_VERSIONS:
5645 /* Ignored for backward compatibility. */
5646 break;
5647
5648 case 'U':
5649 if (optarg == NULL)
5650 error (_("Missing arg to -U/--unicode")); /* Can this happen ? */
5651 else if (streq (optarg, "default") || streq (optarg, "d"))
5652 unicode_display = unicode_default;
5653 else if (streq (optarg, "locale") || streq (optarg, "l"))
5654 unicode_display = unicode_locale;
5655 else if (streq (optarg, "escape") || streq (optarg, "e"))
5656 unicode_display = unicode_escape;
5657 else if (streq (optarg, "invalid") || streq (optarg, "i"))
5658 unicode_display = unicode_invalid;
5659 else if (streq (optarg, "hex") || streq (optarg, "x"))
5660 unicode_display = unicode_hex;
5661 else if (streq (optarg, "highlight") || streq (optarg, "h"))
5662 unicode_display = unicode_highlight;
5663 else
5664 error (_("invalid argument to -U/--unicode: %s"), optarg);
5665 break;
5666
5667 case OPTION_SYM_BASE:
5668 sym_base = 0;
5669 if (optarg != NULL)
5670 {
5671 sym_base = strtoul (optarg, NULL, 0);
5672 switch (sym_base)
5673 {
5674 case 0:
5675 case 8:
5676 case 10:
5677 case 16:
5678 break;
5679
5680 default:
5681 sym_base = 0;
5682 break;
5683 }
5684 }
5685 break;
5686
5687 default:
5688 /* xgettext:c-format */
5689 error (_("Invalid option '-%c'\n"), c);
5690 /* Fall through. */
5691 case '?':
5692 usage (stderr);
5693 }
5694 }
5695
5696 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
5697 && !do_segments && !do_header && !do_dump && !do_version
5698 && !do_histogram && !do_debugging && !do_arch && !do_notes
5699 && !do_section_groups && !do_archive_index
5700 && !do_dyn_syms && !do_lto_syms)
5701 {
5702 if (do_checks)
5703 {
5704 check_all = true;
5705 do_dynamic = do_syms = do_reloc = do_unwind = do_sections = true;
5706 do_segments = do_header = do_dump = do_version = true;
5707 do_histogram = do_debugging = do_arch = do_notes = true;
5708 do_section_groups = do_archive_index = do_dyn_syms = true;
5709 do_lto_syms = true;
5710 }
5711 else
5712 usage (stderr);
5713 }
5714 }
5715
5716 static const char *
5717 get_elf_class (unsigned int elf_class)
5718 {
5719 static char buff[32];
5720
5721 switch (elf_class)
5722 {
5723 case ELFCLASSNONE: return _("none");
5724 case ELFCLASS32: return "ELF32";
5725 case ELFCLASS64: return "ELF64";
5726 default:
5727 snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
5728 return buff;
5729 }
5730 }
5731
5732 static const char *
5733 get_data_encoding (unsigned int encoding)
5734 {
5735 static char buff[32];
5736
5737 switch (encoding)
5738 {
5739 case ELFDATANONE: return _("none");
5740 case ELFDATA2LSB: return _("2's complement, little endian");
5741 case ELFDATA2MSB: return _("2's complement, big endian");
5742 default:
5743 snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
5744 return buff;
5745 }
5746 }
5747
5748 static bool
5749 check_magic_number (Filedata * filedata, Elf_Internal_Ehdr * header)
5750 {
5751 if (header->e_ident[EI_MAG0] == ELFMAG0
5752 && header->e_ident[EI_MAG1] == ELFMAG1
5753 && header->e_ident[EI_MAG2] == ELFMAG2
5754 && header->e_ident[EI_MAG3] == ELFMAG3)
5755 return true;
5756
5757 /* Some compilers produce object files that are not in the ELF file format.
5758 As an aid to users of readelf, try to identify these cases and suggest
5759 alternative tools.
5760
5761 FIXME: It is not clear if all four bytes are used as constant magic
5762 valus by all compilers. It may be necessary to recode this function if
5763 different tools use different length sequences. */
5764
5765 static struct
5766 {
5767 unsigned char magic[4];
5768 const char * obj_message;
5769 const char * ar_message;
5770 }
5771 known_magic[] =
5772 {
5773 { { 'B', 'C', 0xc0, 0xde },
5774 N_("This is a LLVM bitcode file - try using llvm-bcanalyzer\n"),
5775 N_("This is a LLVM bitcode file - try extracing and then using llvm-bcanalyzer\n")
5776 },
5777 { { 'g', 'o', ' ', 'o' },
5778 N_("This is a GO binary file - try using 'go tool objdump' or 'go tool nm'\n"),
5779 NULL
5780 }
5781 };
5782 int i;
5783
5784 for (i = ARRAY_SIZE (known_magic); i--;)
5785 {
5786 if (header->e_ident[EI_MAG0] == known_magic[i].magic[0]
5787 && header->e_ident[EI_MAG1] == known_magic[i].magic[1]
5788 && header->e_ident[EI_MAG2] == known_magic[i].magic[2]
5789 && header->e_ident[EI_MAG3] == known_magic[i].magic[3])
5790 {
5791 /* Some compiler's analyzer tools do not handle archives,
5792 so we provide two different kinds of error message. */
5793 if (filedata->archive_file_size > 0
5794 && known_magic[i].ar_message != NULL)
5795 error ("%s", known_magic[i].ar_message);
5796 else
5797 error ("%s", known_magic[i].obj_message);
5798 return false;
5799 }
5800 }
5801
5802 error (_("Not an ELF file - it has the wrong magic bytes at the start\n"));
5803 return false;
5804 }
5805
5806 /* Decode the data held in 'filedata->file_header'. */
5807
5808 static bool
5809 process_file_header (Filedata * filedata)
5810 {
5811 Elf_Internal_Ehdr * header = & filedata->file_header;
5812
5813 if (! check_magic_number (filedata, header))
5814 return false;
5815
5816 if (! filedata->is_separate)
5817 init_dwarf_regnames_by_elf_machine_code (header->e_machine);
5818
5819 if (do_header)
5820 {
5821 unsigned i;
5822
5823 if (filedata->is_separate)
5824 printf (_("ELF Header in linked file '%s':\n"), filedata->file_name);
5825 else
5826 printf (_("ELF Header:\n"));
5827 printf (_(" Magic: "));
5828 for (i = 0; i < EI_NIDENT; i++)
5829 printf ("%2.2x ", header->e_ident[i]);
5830 printf ("\n");
5831 printf (_(" Class: %s\n"),
5832 get_elf_class (header->e_ident[EI_CLASS]));
5833 printf (_(" Data: %s\n"),
5834 get_data_encoding (header->e_ident[EI_DATA]));
5835 printf (_(" Version: %d%s\n"),
5836 header->e_ident[EI_VERSION],
5837 (header->e_ident[EI_VERSION] == EV_CURRENT
5838 ? _(" (current)")
5839 : (header->e_ident[EI_VERSION] != EV_NONE
5840 ? _(" <unknown>")
5841 : "")));
5842 printf (_(" OS/ABI: %s\n"),
5843 get_osabi_name (filedata, header->e_ident[EI_OSABI]));
5844 printf (_(" ABI Version: %d\n"),
5845 header->e_ident[EI_ABIVERSION]);
5846 printf (_(" Type: %s\n"),
5847 get_file_type (filedata));
5848 printf (_(" Machine: %s\n"),
5849 get_machine_name (header->e_machine));
5850 printf (_(" Version: 0x%lx\n"),
5851 header->e_version);
5852
5853 printf (_(" Entry point address: "));
5854 print_vma (header->e_entry, PREFIX_HEX);
5855 printf (_("\n Start of program headers: "));
5856 print_vma (header->e_phoff, DEC);
5857 printf (_(" (bytes into file)\n Start of section headers: "));
5858 print_vma (header->e_shoff, DEC);
5859 printf (_(" (bytes into file)\n"));
5860
5861 printf (_(" Flags: 0x%lx%s\n"),
5862 header->e_flags,
5863 get_machine_flags (filedata, header->e_flags, header->e_machine));
5864 printf (_(" Size of this header: %u (bytes)\n"),
5865 header->e_ehsize);
5866 printf (_(" Size of program headers: %u (bytes)\n"),
5867 header->e_phentsize);
5868 printf (_(" Number of program headers: %u"),
5869 header->e_phnum);
5870 if (filedata->section_headers != NULL
5871 && header->e_phnum == PN_XNUM
5872 && filedata->section_headers[0].sh_info != 0)
5873 printf (" (%u)", filedata->section_headers[0].sh_info);
5874 putc ('\n', stdout);
5875 printf (_(" Size of section headers: %u (bytes)\n"),
5876 header->e_shentsize);
5877 printf (_(" Number of section headers: %u"),
5878 header->e_shnum);
5879 if (filedata->section_headers != NULL && header->e_shnum == SHN_UNDEF)
5880 {
5881 header->e_shnum = filedata->section_headers[0].sh_size;
5882 printf (" (%u)", header->e_shnum);
5883 }
5884 putc ('\n', stdout);
5885 printf (_(" Section header string table index: %u"),
5886 header->e_shstrndx);
5887 if (filedata->section_headers != NULL
5888 && header->e_shstrndx == (SHN_XINDEX & 0xffff))
5889 {
5890 header->e_shstrndx = filedata->section_headers[0].sh_link;
5891 printf (" (%u)", header->e_shstrndx);
5892 }
5893 if (header->e_shstrndx != SHN_UNDEF
5894 && header->e_shstrndx >= header->e_shnum)
5895 {
5896 header->e_shstrndx = SHN_UNDEF;
5897 printf (_(" <corrupt: out of range>"));
5898 }
5899 putc ('\n', stdout);
5900 }
5901
5902 if (filedata->section_headers != NULL)
5903 {
5904 if (header->e_phnum == PN_XNUM
5905 && filedata->section_headers[0].sh_info != 0)
5906 {
5907 /* Throw away any cached read of PN_XNUM headers. */
5908 free (filedata->program_headers);
5909 filedata->program_headers = NULL;
5910 header->e_phnum = filedata->section_headers[0].sh_info;
5911 }
5912 if (header->e_shnum == SHN_UNDEF)
5913 header->e_shnum = filedata->section_headers[0].sh_size;
5914 if (header->e_shstrndx == (SHN_XINDEX & 0xffff))
5915 header->e_shstrndx = filedata->section_headers[0].sh_link;
5916 if (header->e_shstrndx >= header->e_shnum)
5917 header->e_shstrndx = SHN_UNDEF;
5918 }
5919
5920 return true;
5921 }
5922
5923 /* Read in the program headers from FILEDATA and store them in PHEADERS.
5924 Returns TRUE upon success, FALSE otherwise. Loads 32-bit headers. */
5925
5926 static bool
5927 get_32bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
5928 {
5929 Elf32_External_Phdr * phdrs;
5930 Elf32_External_Phdr * external;
5931 Elf_Internal_Phdr * internal;
5932 unsigned int i;
5933 unsigned int size = filedata->file_header.e_phentsize;
5934 unsigned int num = filedata->file_header.e_phnum;
5935
5936 /* PR binutils/17531: Cope with unexpected section header sizes. */
5937 if (size == 0 || num == 0)
5938 return false;
5939 if (size < sizeof * phdrs)
5940 {
5941 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
5942 return false;
5943 }
5944 if (size > sizeof * phdrs)
5945 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
5946
5947 phdrs = (Elf32_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
5948 size, num, _("program headers"));
5949 if (phdrs == NULL)
5950 return false;
5951
5952 for (i = 0, internal = pheaders, external = phdrs;
5953 i < filedata->file_header.e_phnum;
5954 i++, internal++, external++)
5955 {
5956 internal->p_type = BYTE_GET (external->p_type);
5957 internal->p_offset = BYTE_GET (external->p_offset);
5958 internal->p_vaddr = BYTE_GET (external->p_vaddr);
5959 internal->p_paddr = BYTE_GET (external->p_paddr);
5960 internal->p_filesz = BYTE_GET (external->p_filesz);
5961 internal->p_memsz = BYTE_GET (external->p_memsz);
5962 internal->p_flags = BYTE_GET (external->p_flags);
5963 internal->p_align = BYTE_GET (external->p_align);
5964 }
5965
5966 free (phdrs);
5967 return true;
5968 }
5969
5970 /* Read in the program headers from FILEDATA and store them in PHEADERS.
5971 Returns TRUE upon success, FALSE otherwise. Loads 64-bit headers. */
5972
5973 static bool
5974 get_64bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
5975 {
5976 Elf64_External_Phdr * phdrs;
5977 Elf64_External_Phdr * external;
5978 Elf_Internal_Phdr * internal;
5979 unsigned int i;
5980 unsigned int size = filedata->file_header.e_phentsize;
5981 unsigned int num = filedata->file_header.e_phnum;
5982
5983 /* PR binutils/17531: Cope with unexpected section header sizes. */
5984 if (size == 0 || num == 0)
5985 return false;
5986 if (size < sizeof * phdrs)
5987 {
5988 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
5989 return false;
5990 }
5991 if (size > sizeof * phdrs)
5992 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
5993
5994 phdrs = (Elf64_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
5995 size, num, _("program headers"));
5996 if (!phdrs)
5997 return false;
5998
5999 for (i = 0, internal = pheaders, external = phdrs;
6000 i < filedata->file_header.e_phnum;
6001 i++, internal++, external++)
6002 {
6003 internal->p_type = BYTE_GET (external->p_type);
6004 internal->p_flags = BYTE_GET (external->p_flags);
6005 internal->p_offset = BYTE_GET (external->p_offset);
6006 internal->p_vaddr = BYTE_GET (external->p_vaddr);
6007 internal->p_paddr = BYTE_GET (external->p_paddr);
6008 internal->p_filesz = BYTE_GET (external->p_filesz);
6009 internal->p_memsz = BYTE_GET (external->p_memsz);
6010 internal->p_align = BYTE_GET (external->p_align);
6011 }
6012
6013 free (phdrs);
6014 return true;
6015 }
6016
6017 /* Returns TRUE if the program headers were read into `program_headers'. */
6018
6019 static bool
6020 get_program_headers (Filedata * filedata)
6021 {
6022 Elf_Internal_Phdr * phdrs;
6023
6024 /* Check cache of prior read. */
6025 if (filedata->program_headers != NULL)
6026 return true;
6027
6028 /* Be kind to memory checkers by looking for
6029 e_phnum values which we know must be invalid. */
6030 if (filedata->file_header.e_phnum
6031 * (is_32bit_elf ? sizeof (Elf32_External_Phdr) : sizeof (Elf64_External_Phdr))
6032 >= filedata->file_size)
6033 {
6034 error (_("Too many program headers - %#x - the file is not that big\n"),
6035 filedata->file_header.e_phnum);
6036 return false;
6037 }
6038
6039 phdrs = (Elf_Internal_Phdr *) cmalloc (filedata->file_header.e_phnum,
6040 sizeof (Elf_Internal_Phdr));
6041 if (phdrs == NULL)
6042 {
6043 error (_("Out of memory reading %u program headers\n"),
6044 filedata->file_header.e_phnum);
6045 return false;
6046 }
6047
6048 if (is_32bit_elf
6049 ? get_32bit_program_headers (filedata, phdrs)
6050 : get_64bit_program_headers (filedata, phdrs))
6051 {
6052 filedata->program_headers = phdrs;
6053 return true;
6054 }
6055
6056 free (phdrs);
6057 return false;
6058 }
6059
6060 /* Print program header info and locate dynamic section. */
6061
6062 static void
6063 process_program_headers (Filedata * filedata)
6064 {
6065 Elf_Internal_Phdr * segment;
6066 unsigned int i;
6067 Elf_Internal_Phdr * previous_load = NULL;
6068
6069 if (filedata->file_header.e_phnum == 0)
6070 {
6071 /* PR binutils/12467. */
6072 if (filedata->file_header.e_phoff != 0)
6073 warn (_("possibly corrupt ELF header - it has a non-zero program"
6074 " header offset, but no program headers\n"));
6075 else if (do_segments)
6076 {
6077 if (filedata->is_separate)
6078 printf (_("\nThere are no program headers in linked file '%s'.\n"),
6079 filedata->file_name);
6080 else
6081 printf (_("\nThere are no program headers in this file.\n"));
6082 }
6083 goto no_headers;
6084 }
6085
6086 if (do_segments && !do_header)
6087 {
6088 if (filedata->is_separate)
6089 printf ("\nIn linked file '%s' the ELF file type is %s\n",
6090 filedata->file_name, get_file_type (filedata));
6091 else
6092 printf (_("\nElf file type is %s\n"), get_file_type (filedata));
6093 printf (_("Entry point 0x%" PRIx64 "\n"),
6094 filedata->file_header.e_entry);
6095 printf (ngettext ("There is %d program header,"
6096 " starting at offset %" PRIu64 "\n",
6097 "There are %d program headers,"
6098 " starting at offset %" PRIu64 "\n",
6099 filedata->file_header.e_phnum),
6100 filedata->file_header.e_phnum,
6101 filedata->file_header.e_phoff);
6102 }
6103
6104 if (! get_program_headers (filedata))
6105 goto no_headers;
6106
6107 if (do_segments)
6108 {
6109 if (filedata->file_header.e_phnum > 1)
6110 printf (_("\nProgram Headers:\n"));
6111 else
6112 printf (_("\nProgram Headers:\n"));
6113
6114 if (is_32bit_elf)
6115 printf
6116 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
6117 else if (do_wide)
6118 printf
6119 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
6120 else
6121 {
6122 printf
6123 (_(" Type Offset VirtAddr PhysAddr\n"));
6124 printf
6125 (_(" FileSiz MemSiz Flags Align\n"));
6126 }
6127 }
6128
6129 unsigned long dynamic_addr = 0;
6130 uint64_t dynamic_size = 0;
6131 for (i = 0, segment = filedata->program_headers;
6132 i < filedata->file_header.e_phnum;
6133 i++, segment++)
6134 {
6135 if (do_segments)
6136 {
6137 printf (" %-14.14s ", get_segment_type (filedata, segment->p_type));
6138
6139 if (is_32bit_elf)
6140 {
6141 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
6142 printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
6143 printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
6144 printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
6145 printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
6146 printf ("%c%c%c ",
6147 (segment->p_flags & PF_R ? 'R' : ' '),
6148 (segment->p_flags & PF_W ? 'W' : ' '),
6149 (segment->p_flags & PF_X ? 'E' : ' '));
6150 printf ("%#lx", (unsigned long) segment->p_align);
6151 }
6152 else if (do_wide)
6153 {
6154 if ((unsigned long) segment->p_offset == segment->p_offset)
6155 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
6156 else
6157 {
6158 print_vma (segment->p_offset, FULL_HEX);
6159 putchar (' ');
6160 }
6161
6162 print_vma (segment->p_vaddr, FULL_HEX);
6163 putchar (' ');
6164 print_vma (segment->p_paddr, FULL_HEX);
6165 putchar (' ');
6166
6167 if ((unsigned long) segment->p_filesz == segment->p_filesz)
6168 printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
6169 else
6170 {
6171 print_vma (segment->p_filesz, FULL_HEX);
6172 putchar (' ');
6173 }
6174
6175 if ((unsigned long) segment->p_memsz == segment->p_memsz)
6176 printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
6177 else
6178 {
6179 print_vma (segment->p_memsz, FULL_HEX);
6180 }
6181
6182 printf (" %c%c%c ",
6183 (segment->p_flags & PF_R ? 'R' : ' '),
6184 (segment->p_flags & PF_W ? 'W' : ' '),
6185 (segment->p_flags & PF_X ? 'E' : ' '));
6186
6187 if ((unsigned long) segment->p_align == segment->p_align)
6188 printf ("%#lx", (unsigned long) segment->p_align);
6189 else
6190 {
6191 print_vma (segment->p_align, PREFIX_HEX);
6192 }
6193 }
6194 else
6195 {
6196 print_vma (segment->p_offset, FULL_HEX);
6197 putchar (' ');
6198 print_vma (segment->p_vaddr, FULL_HEX);
6199 putchar (' ');
6200 print_vma (segment->p_paddr, FULL_HEX);
6201 printf ("\n ");
6202 print_vma (segment->p_filesz, FULL_HEX);
6203 putchar (' ');
6204 print_vma (segment->p_memsz, FULL_HEX);
6205 printf (" %c%c%c ",
6206 (segment->p_flags & PF_R ? 'R' : ' '),
6207 (segment->p_flags & PF_W ? 'W' : ' '),
6208 (segment->p_flags & PF_X ? 'E' : ' '));
6209 print_vma (segment->p_align, PREFIX_HEX);
6210 }
6211
6212 putc ('\n', stdout);
6213 }
6214
6215 switch (segment->p_type)
6216 {
6217 case PT_LOAD:
6218 #if 0 /* Do not warn about out of order PT_LOAD segments. Although officially
6219 required by the ELF standard, several programs, including the Linux
6220 kernel, make use of non-ordered segments. */
6221 if (previous_load
6222 && previous_load->p_vaddr > segment->p_vaddr)
6223 error (_("LOAD segments must be sorted in order of increasing VirtAddr\n"));
6224 #endif
6225 if (segment->p_memsz < segment->p_filesz)
6226 error (_("the segment's file size is larger than its memory size\n"));
6227 previous_load = segment;
6228 break;
6229
6230 case PT_PHDR:
6231 /* PR 20815 - Verify that the program header is loaded into memory. */
6232 if (i > 0 && previous_load != NULL)
6233 error (_("the PHDR segment must occur before any LOAD segment\n"));
6234 if (filedata->file_header.e_machine != EM_PARISC)
6235 {
6236 unsigned int j;
6237
6238 for (j = 1; j < filedata->file_header.e_phnum; j++)
6239 {
6240 Elf_Internal_Phdr *load = filedata->program_headers + j;
6241 if (load->p_type == PT_LOAD
6242 && load->p_offset <= segment->p_offset
6243 && (load->p_offset + load->p_filesz
6244 >= segment->p_offset + segment->p_filesz)
6245 && load->p_vaddr <= segment->p_vaddr
6246 && (load->p_vaddr + load->p_filesz
6247 >= segment->p_vaddr + segment->p_filesz))
6248 break;
6249 }
6250 if (j == filedata->file_header.e_phnum)
6251 error (_("the PHDR segment is not covered by a LOAD segment\n"));
6252 }
6253 break;
6254
6255 case PT_DYNAMIC:
6256 if (dynamic_addr)
6257 error (_("more than one dynamic segment\n"));
6258
6259 /* By default, assume that the .dynamic section is the first
6260 section in the DYNAMIC segment. */
6261 dynamic_addr = segment->p_offset;
6262 dynamic_size = segment->p_filesz;
6263
6264 /* Try to locate the .dynamic section. If there is
6265 a section header table, we can easily locate it. */
6266 if (filedata->section_headers != NULL)
6267 {
6268 Elf_Internal_Shdr * sec;
6269
6270 sec = find_section (filedata, ".dynamic");
6271 if (sec == NULL || sec->sh_size == 0)
6272 {
6273 /* A corresponding .dynamic section is expected, but on
6274 IA-64/OpenVMS it is OK for it to be missing. */
6275 if (!is_ia64_vms (filedata))
6276 error (_("no .dynamic section in the dynamic segment\n"));
6277 break;
6278 }
6279
6280 if (sec->sh_type == SHT_NOBITS)
6281 {
6282 dynamic_addr = 0;
6283 dynamic_size = 0;
6284 break;
6285 }
6286
6287 dynamic_addr = sec->sh_offset;
6288 dynamic_size = sec->sh_size;
6289
6290 /* The PT_DYNAMIC segment, which is used by the run-time
6291 loader, should exactly match the .dynamic section. */
6292 if (do_checks
6293 && (dynamic_addr != segment->p_offset
6294 || dynamic_size != segment->p_filesz))
6295 warn (_("\
6296 the .dynamic section is not the same as the dynamic segment\n"));
6297 }
6298
6299 /* PR binutils/17512: Avoid corrupt dynamic section info in the
6300 segment. Check this after matching against the section headers
6301 so we don't warn on debuginfo file (which have NOBITS .dynamic
6302 sections). */
6303 if (dynamic_addr > filedata->file_size
6304 || (dynamic_size > filedata->file_size - dynamic_addr))
6305 {
6306 error (_("the dynamic segment offset + size exceeds the size of the file\n"));
6307 dynamic_addr = 0;
6308 dynamic_size = 0;
6309 }
6310 break;
6311
6312 case PT_INTERP:
6313 if (segment->p_offset >= filedata->file_size
6314 || segment->p_filesz > filedata->file_size - segment->p_offset
6315 || segment->p_filesz - 1 >= (size_t) -2
6316 || fseek64 (filedata->handle,
6317 filedata->archive_file_offset + segment->p_offset,
6318 SEEK_SET))
6319 error (_("Unable to find program interpreter name\n"));
6320 else
6321 {
6322 size_t len = segment->p_filesz;
6323 free (filedata->program_interpreter);
6324 filedata->program_interpreter = xmalloc (len + 1);
6325 len = fread (filedata->program_interpreter, 1, len,
6326 filedata->handle);
6327 filedata->program_interpreter[len] = 0;
6328
6329 if (do_segments)
6330 printf (_(" [Requesting program interpreter: %s]\n"),
6331 filedata->program_interpreter);
6332 }
6333 break;
6334 }
6335 }
6336
6337 if (do_segments
6338 && filedata->section_headers != NULL
6339 && filedata->string_table != NULL)
6340 {
6341 printf (_("\n Section to Segment mapping:\n"));
6342 printf (_(" Segment Sections...\n"));
6343
6344 for (i = 0; i < filedata->file_header.e_phnum; i++)
6345 {
6346 unsigned int j;
6347 Elf_Internal_Shdr * section;
6348
6349 segment = filedata->program_headers + i;
6350 section = filedata->section_headers + 1;
6351
6352 printf (" %2.2d ", i);
6353
6354 for (j = 1; j < filedata->file_header.e_shnum; j++, section++)
6355 {
6356 if (!ELF_TBSS_SPECIAL (section, segment)
6357 && ELF_SECTION_IN_SEGMENT_STRICT (section, segment))
6358 printf ("%s ", printable_section_name (filedata, section));
6359 }
6360
6361 putc ('\n',stdout);
6362 }
6363 }
6364
6365 filedata->dynamic_addr = dynamic_addr;
6366 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
6367 return;
6368
6369 no_headers:
6370 filedata->dynamic_addr = 0;
6371 filedata->dynamic_size = 1;
6372 }
6373
6374
6375 /* Find the file offset corresponding to VMA by using the program headers. */
6376
6377 static long
6378 offset_from_vma (Filedata * filedata, uint64_t vma, uint64_t size)
6379 {
6380 Elf_Internal_Phdr * seg;
6381
6382 if (! get_program_headers (filedata))
6383 {
6384 warn (_("Cannot interpret virtual addresses without program headers.\n"));
6385 return (long) vma;
6386 }
6387
6388 for (seg = filedata->program_headers;
6389 seg < filedata->program_headers + filedata->file_header.e_phnum;
6390 ++seg)
6391 {
6392 if (seg->p_type != PT_LOAD)
6393 continue;
6394
6395 if (vma >= (seg->p_vaddr & -seg->p_align)
6396 && vma + size <= seg->p_vaddr + seg->p_filesz)
6397 return vma - seg->p_vaddr + seg->p_offset;
6398 }
6399
6400 warn (_("Virtual address 0x%lx not located in any PT_LOAD segment.\n"),
6401 (unsigned long) vma);
6402 return (long) vma;
6403 }
6404
6405
6406 /* Allocate memory and load the sections headers into FILEDATA->filedata->section_headers.
6407 If PROBE is true, this is just a probe and we do not generate any error
6408 messages if the load fails. */
6409
6410 static bool
6411 get_32bit_section_headers (Filedata * filedata, bool probe)
6412 {
6413 Elf32_External_Shdr * shdrs;
6414 Elf_Internal_Shdr * internal;
6415 unsigned int i;
6416 unsigned int size = filedata->file_header.e_shentsize;
6417 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
6418
6419 /* PR binutils/17531: Cope with unexpected section header sizes. */
6420 if (size == 0 || num == 0)
6421 return false;
6422
6423 /* The section header cannot be at the start of the file - that is
6424 where the ELF file header is located. A file with absolutely no
6425 sections in it will use a shoff of 0. */
6426 if (filedata->file_header.e_shoff == 0)
6427 return false;
6428
6429 if (size < sizeof * shdrs)
6430 {
6431 if (! probe)
6432 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
6433 return false;
6434 }
6435 if (!probe && size > sizeof * shdrs)
6436 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
6437
6438 shdrs = (Elf32_External_Shdr *) get_data (NULL, filedata, filedata->file_header.e_shoff,
6439 size, num,
6440 probe ? NULL : _("section headers"));
6441 if (shdrs == NULL)
6442 return false;
6443
6444 filedata->section_headers = (Elf_Internal_Shdr *)
6445 cmalloc (num, sizeof (Elf_Internal_Shdr));
6446 if (filedata->section_headers == NULL)
6447 {
6448 if (!probe)
6449 error (_("Out of memory reading %u section headers\n"), num);
6450 free (shdrs);
6451 return false;
6452 }
6453
6454 for (i = 0, internal = filedata->section_headers;
6455 i < num;
6456 i++, internal++)
6457 {
6458 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
6459 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
6460 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
6461 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
6462 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
6463 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
6464 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
6465 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
6466 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
6467 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
6468 if (!probe && internal->sh_link > num)
6469 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
6470 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
6471 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
6472 }
6473
6474 free (shdrs);
6475 return true;
6476 }
6477
6478 /* Like get_32bit_section_headers, except that it fetches 64-bit headers. */
6479
6480 static bool
6481 get_64bit_section_headers (Filedata * filedata, bool probe)
6482 {
6483 Elf64_External_Shdr * shdrs;
6484 Elf_Internal_Shdr * internal;
6485 unsigned int i;
6486 unsigned int size = filedata->file_header.e_shentsize;
6487 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
6488
6489 /* PR binutils/17531: Cope with unexpected section header sizes. */
6490 if (size == 0 || num == 0)
6491 return false;
6492
6493 /* The section header cannot be at the start of the file - that is
6494 where the ELF file header is located. A file with absolutely no
6495 sections in it will use a shoff of 0. */
6496 if (filedata->file_header.e_shoff == 0)
6497 return false;
6498
6499 if (size < sizeof * shdrs)
6500 {
6501 if (! probe)
6502 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
6503 return false;
6504 }
6505
6506 if (! probe && size > sizeof * shdrs)
6507 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
6508
6509 shdrs = (Elf64_External_Shdr *) get_data (NULL, filedata,
6510 filedata->file_header.e_shoff,
6511 size, num,
6512 probe ? NULL : _("section headers"));
6513 if (shdrs == NULL)
6514 return false;
6515
6516 filedata->section_headers = (Elf_Internal_Shdr *)
6517 cmalloc (num, sizeof (Elf_Internal_Shdr));
6518 if (filedata->section_headers == NULL)
6519 {
6520 if (! probe)
6521 error (_("Out of memory reading %u section headers\n"), num);
6522 free (shdrs);
6523 return false;
6524 }
6525
6526 for (i = 0, internal = filedata->section_headers;
6527 i < num;
6528 i++, internal++)
6529 {
6530 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
6531 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
6532 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
6533 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
6534 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
6535 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
6536 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
6537 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
6538 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
6539 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
6540 if (!probe && internal->sh_link > num)
6541 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
6542 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
6543 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
6544 }
6545
6546 free (shdrs);
6547 return true;
6548 }
6549
6550 static bool
6551 get_section_headers (Filedata *filedata, bool probe)
6552 {
6553 if (filedata->section_headers != NULL)
6554 return true;
6555
6556 if (is_32bit_elf)
6557 return get_32bit_section_headers (filedata, probe);
6558 else
6559 return get_64bit_section_headers (filedata, probe);
6560 }
6561
6562 static Elf_Internal_Sym *
6563 get_32bit_elf_symbols (Filedata * filedata,
6564 Elf_Internal_Shdr * section,
6565 unsigned long * num_syms_return)
6566 {
6567 unsigned long number = 0;
6568 Elf32_External_Sym * esyms = NULL;
6569 Elf_External_Sym_Shndx * shndx = NULL;
6570 Elf_Internal_Sym * isyms = NULL;
6571 Elf_Internal_Sym * psym;
6572 unsigned int j;
6573 elf_section_list * entry;
6574
6575 if (section->sh_size == 0)
6576 {
6577 if (num_syms_return != NULL)
6578 * num_syms_return = 0;
6579 return NULL;
6580 }
6581
6582 /* Run some sanity checks first. */
6583 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
6584 {
6585 error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
6586 printable_section_name (filedata, section),
6587 (unsigned long) section->sh_entsize);
6588 goto exit_point;
6589 }
6590
6591 if (section->sh_size > filedata->file_size)
6592 {
6593 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
6594 printable_section_name (filedata, section),
6595 (unsigned long) section->sh_size);
6596 goto exit_point;
6597 }
6598
6599 number = section->sh_size / section->sh_entsize;
6600
6601 if (number * sizeof (Elf32_External_Sym) > section->sh_size + 1)
6602 {
6603 error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
6604 (unsigned long) section->sh_size,
6605 printable_section_name (filedata, section),
6606 (unsigned long) section->sh_entsize);
6607 goto exit_point;
6608 }
6609
6610 esyms = (Elf32_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
6611 section->sh_size, _("symbols"));
6612 if (esyms == NULL)
6613 goto exit_point;
6614
6615 shndx = NULL;
6616 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
6617 {
6618 if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
6619 continue;
6620
6621 if (shndx != NULL)
6622 {
6623 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
6624 free (shndx);
6625 }
6626
6627 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
6628 entry->hdr->sh_offset,
6629 1, entry->hdr->sh_size,
6630 _("symbol table section indices"));
6631 if (shndx == NULL)
6632 goto exit_point;
6633
6634 /* PR17531: file: heap-buffer-overflow */
6635 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
6636 {
6637 error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
6638 printable_section_name (filedata, entry->hdr),
6639 (unsigned long) entry->hdr->sh_size,
6640 (unsigned long) section->sh_size);
6641 goto exit_point;
6642 }
6643 }
6644
6645 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
6646
6647 if (isyms == NULL)
6648 {
6649 error (_("Out of memory reading %lu symbols\n"),
6650 (unsigned long) number);
6651 goto exit_point;
6652 }
6653
6654 for (j = 0, psym = isyms; j < number; j++, psym++)
6655 {
6656 psym->st_name = BYTE_GET (esyms[j].st_name);
6657 psym->st_value = BYTE_GET (esyms[j].st_value);
6658 psym->st_size = BYTE_GET (esyms[j].st_size);
6659 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
6660 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
6661 psym->st_shndx
6662 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
6663 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
6664 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
6665 psym->st_info = BYTE_GET (esyms[j].st_info);
6666 psym->st_other = BYTE_GET (esyms[j].st_other);
6667 }
6668
6669 exit_point:
6670 free (shndx);
6671 free (esyms);
6672
6673 if (num_syms_return != NULL)
6674 * num_syms_return = isyms == NULL ? 0 : number;
6675
6676 return isyms;
6677 }
6678
6679 static Elf_Internal_Sym *
6680 get_64bit_elf_symbols (Filedata * filedata,
6681 Elf_Internal_Shdr * section,
6682 unsigned long * num_syms_return)
6683 {
6684 unsigned long number = 0;
6685 Elf64_External_Sym * esyms = NULL;
6686 Elf_External_Sym_Shndx * shndx = NULL;
6687 Elf_Internal_Sym * isyms = NULL;
6688 Elf_Internal_Sym * psym;
6689 unsigned int j;
6690 elf_section_list * entry;
6691
6692 if (section->sh_size == 0)
6693 {
6694 if (num_syms_return != NULL)
6695 * num_syms_return = 0;
6696 return NULL;
6697 }
6698
6699 /* Run some sanity checks first. */
6700 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
6701 {
6702 error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
6703 printable_section_name (filedata, section),
6704 (unsigned long) section->sh_entsize);
6705 goto exit_point;
6706 }
6707
6708 if (section->sh_size > filedata->file_size)
6709 {
6710 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
6711 printable_section_name (filedata, section),
6712 (unsigned long) section->sh_size);
6713 goto exit_point;
6714 }
6715
6716 number = section->sh_size / section->sh_entsize;
6717
6718 if (number * sizeof (Elf64_External_Sym) > section->sh_size + 1)
6719 {
6720 error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
6721 (unsigned long) section->sh_size,
6722 printable_section_name (filedata, section),
6723 (unsigned long) section->sh_entsize);
6724 goto exit_point;
6725 }
6726
6727 esyms = (Elf64_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
6728 section->sh_size, _("symbols"));
6729 if (!esyms)
6730 goto exit_point;
6731
6732 shndx = NULL;
6733 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
6734 {
6735 if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
6736 continue;
6737
6738 if (shndx != NULL)
6739 {
6740 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
6741 free (shndx);
6742 }
6743
6744 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
6745 entry->hdr->sh_offset,
6746 1, entry->hdr->sh_size,
6747 _("symbol table section indices"));
6748 if (shndx == NULL)
6749 goto exit_point;
6750
6751 /* PR17531: file: heap-buffer-overflow */
6752 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
6753 {
6754 error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
6755 printable_section_name (filedata, entry->hdr),
6756 (unsigned long) entry->hdr->sh_size,
6757 (unsigned long) section->sh_size);
6758 goto exit_point;
6759 }
6760 }
6761
6762 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
6763
6764 if (isyms == NULL)
6765 {
6766 error (_("Out of memory reading %lu symbols\n"),
6767 (unsigned long) number);
6768 goto exit_point;
6769 }
6770
6771 for (j = 0, psym = isyms; j < number; j++, psym++)
6772 {
6773 psym->st_name = BYTE_GET (esyms[j].st_name);
6774 psym->st_info = BYTE_GET (esyms[j].st_info);
6775 psym->st_other = BYTE_GET (esyms[j].st_other);
6776 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
6777
6778 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
6779 psym->st_shndx
6780 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
6781 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
6782 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
6783
6784 psym->st_value = BYTE_GET (esyms[j].st_value);
6785 psym->st_size = BYTE_GET (esyms[j].st_size);
6786 }
6787
6788 exit_point:
6789 free (shndx);
6790 free (esyms);
6791
6792 if (num_syms_return != NULL)
6793 * num_syms_return = isyms == NULL ? 0 : number;
6794
6795 return isyms;
6796 }
6797
6798 static Elf_Internal_Sym *
6799 get_elf_symbols (Filedata *filedata,
6800 Elf_Internal_Shdr *section,
6801 unsigned long *num_syms_return)
6802 {
6803 if (is_32bit_elf)
6804 return get_32bit_elf_symbols (filedata, section, num_syms_return);
6805 else
6806 return get_64bit_elf_symbols (filedata, section, num_syms_return);
6807 }
6808
6809 static const char *
6810 get_elf_section_flags (Filedata * filedata, uint64_t sh_flags)
6811 {
6812 static char buff[1024];
6813 char * p = buff;
6814 unsigned int field_size = is_32bit_elf ? 8 : 16;
6815 signed int sindex;
6816 unsigned int size = sizeof (buff) - (field_size + 4 + 1);
6817 uint64_t os_flags = 0;
6818 uint64_t proc_flags = 0;
6819 uint64_t unknown_flags = 0;
6820 static const struct
6821 {
6822 const char * str;
6823 unsigned int len;
6824 }
6825 flags [] =
6826 {
6827 /* 0 */ { STRING_COMMA_LEN ("WRITE") },
6828 /* 1 */ { STRING_COMMA_LEN ("ALLOC") },
6829 /* 2 */ { STRING_COMMA_LEN ("EXEC") },
6830 /* 3 */ { STRING_COMMA_LEN ("MERGE") },
6831 /* 4 */ { STRING_COMMA_LEN ("STRINGS") },
6832 /* 5 */ { STRING_COMMA_LEN ("INFO LINK") },
6833 /* 6 */ { STRING_COMMA_LEN ("LINK ORDER") },
6834 /* 7 */ { STRING_COMMA_LEN ("OS NONCONF") },
6835 /* 8 */ { STRING_COMMA_LEN ("GROUP") },
6836 /* 9 */ { STRING_COMMA_LEN ("TLS") },
6837 /* IA-64 specific. */
6838 /* 10 */ { STRING_COMMA_LEN ("SHORT") },
6839 /* 11 */ { STRING_COMMA_LEN ("NORECOV") },
6840 /* IA-64 OpenVMS specific. */
6841 /* 12 */ { STRING_COMMA_LEN ("VMS_GLOBAL") },
6842 /* 13 */ { STRING_COMMA_LEN ("VMS_OVERLAID") },
6843 /* 14 */ { STRING_COMMA_LEN ("VMS_SHARED") },
6844 /* 15 */ { STRING_COMMA_LEN ("VMS_VECTOR") },
6845 /* 16 */ { STRING_COMMA_LEN ("VMS_ALLOC_64BIT") },
6846 /* 17 */ { STRING_COMMA_LEN ("VMS_PROTECTED") },
6847 /* Generic. */
6848 /* 18 */ { STRING_COMMA_LEN ("EXCLUDE") },
6849 /* SPARC specific. */
6850 /* 19 */ { STRING_COMMA_LEN ("ORDERED") },
6851 /* 20 */ { STRING_COMMA_LEN ("COMPRESSED") },
6852 /* ARM specific. */
6853 /* 21 */ { STRING_COMMA_LEN ("ENTRYSECT") },
6854 /* 22 */ { STRING_COMMA_LEN ("ARM_PURECODE") },
6855 /* 23 */ { STRING_COMMA_LEN ("COMDEF") },
6856 /* GNU specific. */
6857 /* 24 */ { STRING_COMMA_LEN ("GNU_MBIND") },
6858 /* VLE specific. */
6859 /* 25 */ { STRING_COMMA_LEN ("VLE") },
6860 /* GNU specific. */
6861 /* 26 */ { STRING_COMMA_LEN ("GNU_RETAIN") },
6862 };
6863
6864 if (do_section_details)
6865 {
6866 sprintf (buff, "[%*.*lx]: ",
6867 field_size, field_size, (unsigned long) sh_flags);
6868 p += field_size + 4;
6869 }
6870
6871 while (sh_flags)
6872 {
6873 uint64_t flag;
6874
6875 flag = sh_flags & - sh_flags;
6876 sh_flags &= ~ flag;
6877
6878 if (do_section_details)
6879 {
6880 switch (flag)
6881 {
6882 case SHF_WRITE: sindex = 0; break;
6883 case SHF_ALLOC: sindex = 1; break;
6884 case SHF_EXECINSTR: sindex = 2; break;
6885 case SHF_MERGE: sindex = 3; break;
6886 case SHF_STRINGS: sindex = 4; break;
6887 case SHF_INFO_LINK: sindex = 5; break;
6888 case SHF_LINK_ORDER: sindex = 6; break;
6889 case SHF_OS_NONCONFORMING: sindex = 7; break;
6890 case SHF_GROUP: sindex = 8; break;
6891 case SHF_TLS: sindex = 9; break;
6892 case SHF_EXCLUDE: sindex = 18; break;
6893 case SHF_COMPRESSED: sindex = 20; break;
6894
6895 default:
6896 sindex = -1;
6897 switch (filedata->file_header.e_machine)
6898 {
6899 case EM_IA_64:
6900 if (flag == SHF_IA_64_SHORT)
6901 sindex = 10;
6902 else if (flag == SHF_IA_64_NORECOV)
6903 sindex = 11;
6904 else if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
6905 switch (flag)
6906 {
6907 case SHF_IA_64_VMS_GLOBAL: sindex = 12; break;
6908 case SHF_IA_64_VMS_OVERLAID: sindex = 13; break;
6909 case SHF_IA_64_VMS_SHARED: sindex = 14; break;
6910 case SHF_IA_64_VMS_VECTOR: sindex = 15; break;
6911 case SHF_IA_64_VMS_ALLOC_64BIT: sindex = 16; break;
6912 case SHF_IA_64_VMS_PROTECTED: sindex = 17; break;
6913 default: break;
6914 }
6915 break;
6916
6917 case EM_386:
6918 case EM_IAMCU:
6919 case EM_X86_64:
6920 case EM_L1OM:
6921 case EM_K1OM:
6922 case EM_OLD_SPARCV9:
6923 case EM_SPARC32PLUS:
6924 case EM_SPARCV9:
6925 case EM_SPARC:
6926 if (flag == SHF_ORDERED)
6927 sindex = 19;
6928 break;
6929
6930 case EM_ARM:
6931 switch (flag)
6932 {
6933 case SHF_ENTRYSECT: sindex = 21; break;
6934 case SHF_ARM_PURECODE: sindex = 22; break;
6935 case SHF_COMDEF: sindex = 23; break;
6936 default: break;
6937 }
6938 break;
6939 case EM_PPC:
6940 if (flag == SHF_PPC_VLE)
6941 sindex = 25;
6942 break;
6943 default:
6944 break;
6945 }
6946
6947 switch (filedata->file_header.e_ident[EI_OSABI])
6948 {
6949 case ELFOSABI_GNU:
6950 case ELFOSABI_FREEBSD:
6951 if (flag == SHF_GNU_RETAIN)
6952 sindex = 26;
6953 /* Fall through */
6954 case ELFOSABI_NONE:
6955 if (flag == SHF_GNU_MBIND)
6956 /* We should not recognize SHF_GNU_MBIND for
6957 ELFOSABI_NONE, but binutils as of 2019-07-23 did
6958 not set the EI_OSABI header byte. */
6959 sindex = 24;
6960 break;
6961 default:
6962 break;
6963 }
6964 break;
6965 }
6966
6967 if (sindex != -1)
6968 {
6969 if (p != buff + field_size + 4)
6970 {
6971 if (size < (10 + 2))
6972 {
6973 warn (_("Internal error: not enough buffer room for section flag info"));
6974 return _("<unknown>");
6975 }
6976 size -= 2;
6977 *p++ = ',';
6978 *p++ = ' ';
6979 }
6980
6981 size -= flags [sindex].len;
6982 p = stpcpy (p, flags [sindex].str);
6983 }
6984 else if (flag & SHF_MASKOS)
6985 os_flags |= flag;
6986 else if (flag & SHF_MASKPROC)
6987 proc_flags |= flag;
6988 else
6989 unknown_flags |= flag;
6990 }
6991 else
6992 {
6993 switch (flag)
6994 {
6995 case SHF_WRITE: *p = 'W'; break;
6996 case SHF_ALLOC: *p = 'A'; break;
6997 case SHF_EXECINSTR: *p = 'X'; break;
6998 case SHF_MERGE: *p = 'M'; break;
6999 case SHF_STRINGS: *p = 'S'; break;
7000 case SHF_INFO_LINK: *p = 'I'; break;
7001 case SHF_LINK_ORDER: *p = 'L'; break;
7002 case SHF_OS_NONCONFORMING: *p = 'O'; break;
7003 case SHF_GROUP: *p = 'G'; break;
7004 case SHF_TLS: *p = 'T'; break;
7005 case SHF_EXCLUDE: *p = 'E'; break;
7006 case SHF_COMPRESSED: *p = 'C'; break;
7007
7008 default:
7009 if ((filedata->file_header.e_machine == EM_X86_64
7010 || filedata->file_header.e_machine == EM_L1OM
7011 || filedata->file_header.e_machine == EM_K1OM)
7012 && flag == SHF_X86_64_LARGE)
7013 *p = 'l';
7014 else if (filedata->file_header.e_machine == EM_ARM
7015 && flag == SHF_ARM_PURECODE)
7016 *p = 'y';
7017 else if (filedata->file_header.e_machine == EM_PPC
7018 && flag == SHF_PPC_VLE)
7019 *p = 'v';
7020 else if (flag & SHF_MASKOS)
7021 {
7022 switch (filedata->file_header.e_ident[EI_OSABI])
7023 {
7024 case ELFOSABI_GNU:
7025 case ELFOSABI_FREEBSD:
7026 if (flag == SHF_GNU_RETAIN)
7027 {
7028 *p = 'R';
7029 break;
7030 }
7031 /* Fall through */
7032 case ELFOSABI_NONE:
7033 if (flag == SHF_GNU_MBIND)
7034 {
7035 /* We should not recognize SHF_GNU_MBIND for
7036 ELFOSABI_NONE, but binutils as of 2019-07-23 did
7037 not set the EI_OSABI header byte. */
7038 *p = 'D';
7039 break;
7040 }
7041 /* Fall through */
7042 default:
7043 *p = 'o';
7044 sh_flags &= ~SHF_MASKOS;
7045 break;
7046 }
7047 }
7048 else if (flag & SHF_MASKPROC)
7049 {
7050 *p = 'p';
7051 sh_flags &= ~ SHF_MASKPROC;
7052 }
7053 else
7054 *p = 'x';
7055 break;
7056 }
7057 p++;
7058 }
7059 }
7060
7061 if (do_section_details)
7062 {
7063 if (os_flags)
7064 {
7065 size -= 5 + field_size;
7066 if (p != buff + field_size + 4)
7067 {
7068 if (size < (2 + 1))
7069 {
7070 warn (_("Internal error: not enough buffer room for section flag info"));
7071 return _("<unknown>");
7072 }
7073 size -= 2;
7074 *p++ = ',';
7075 *p++ = ' ';
7076 }
7077 sprintf (p, "OS (%*.*lx)", field_size, field_size,
7078 (unsigned long) os_flags);
7079 p += 5 + field_size;
7080 }
7081 if (proc_flags)
7082 {
7083 size -= 7 + field_size;
7084 if (p != buff + field_size + 4)
7085 {
7086 if (size < (2 + 1))
7087 {
7088 warn (_("Internal error: not enough buffer room for section flag info"));
7089 return _("<unknown>");
7090 }
7091 size -= 2;
7092 *p++ = ',';
7093 *p++ = ' ';
7094 }
7095 sprintf (p, "PROC (%*.*lx)", field_size, field_size,
7096 (unsigned long) proc_flags);
7097 p += 7 + field_size;
7098 }
7099 if (unknown_flags)
7100 {
7101 size -= 10 + field_size;
7102 if (p != buff + field_size + 4)
7103 {
7104 if (size < (2 + 1))
7105 {
7106 warn (_("Internal error: not enough buffer room for section flag info"));
7107 return _("<unknown>");
7108 }
7109 size -= 2;
7110 *p++ = ',';
7111 *p++ = ' ';
7112 }
7113 sprintf (p, _("UNKNOWN (%*.*lx)"), field_size, field_size,
7114 (unsigned long) unknown_flags);
7115 p += 10 + field_size;
7116 }
7117 }
7118
7119 *p = '\0';
7120 return buff;
7121 }
7122
7123 static unsigned int ATTRIBUTE_WARN_UNUSED_RESULT
7124 get_compression_header (Elf_Internal_Chdr *chdr, unsigned char *buf,
7125 uint64_t size)
7126 {
7127 if (is_32bit_elf)
7128 {
7129 Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) buf;
7130
7131 if (size < sizeof (* echdr))
7132 {
7133 error (_("Compressed section is too small even for a compression header\n"));
7134 return 0;
7135 }
7136
7137 chdr->ch_type = BYTE_GET (echdr->ch_type);
7138 chdr->ch_size = BYTE_GET (echdr->ch_size);
7139 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
7140 return sizeof (*echdr);
7141 }
7142 else
7143 {
7144 Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) buf;
7145
7146 if (size < sizeof (* echdr))
7147 {
7148 error (_("Compressed section is too small even for a compression header\n"));
7149 return 0;
7150 }
7151
7152 chdr->ch_type = BYTE_GET (echdr->ch_type);
7153 chdr->ch_size = BYTE_GET (echdr->ch_size);
7154 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
7155 return sizeof (*echdr);
7156 }
7157 }
7158
7159 static bool
7160 process_section_headers (Filedata * filedata)
7161 {
7162 Elf_Internal_Shdr * section;
7163 unsigned int i;
7164
7165 if (filedata->file_header.e_shnum == 0)
7166 {
7167 /* PR binutils/12467. */
7168 if (filedata->file_header.e_shoff != 0)
7169 {
7170 warn (_("possibly corrupt ELF file header - it has a non-zero"
7171 " section header offset, but no section headers\n"));
7172 return false;
7173 }
7174 else if (do_sections)
7175 printf (_("\nThere are no sections in this file.\n"));
7176
7177 return true;
7178 }
7179
7180 if (do_sections && !do_header)
7181 {
7182 if (filedata->is_separate && process_links)
7183 printf (_("In linked file '%s': "), filedata->file_name);
7184 if (! filedata->is_separate || process_links)
7185 printf (ngettext ("There is %d section header, "
7186 "starting at offset 0x%lx:\n",
7187 "There are %d section headers, "
7188 "starting at offset 0x%lx:\n",
7189 filedata->file_header.e_shnum),
7190 filedata->file_header.e_shnum,
7191 (unsigned long) filedata->file_header.e_shoff);
7192 }
7193
7194 if (!get_section_headers (filedata, false))
7195 return false;
7196
7197 /* Read in the string table, so that we have names to display. */
7198 if (filedata->file_header.e_shstrndx != SHN_UNDEF
7199 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
7200 {
7201 section = filedata->section_headers + filedata->file_header.e_shstrndx;
7202
7203 if (section->sh_size != 0)
7204 {
7205 filedata->string_table = (char *) get_data (NULL, filedata, section->sh_offset,
7206 1, section->sh_size,
7207 _("string table"));
7208
7209 filedata->string_table_length = filedata->string_table != NULL ? section->sh_size : 0;
7210 }
7211 }
7212
7213 /* Scan the sections for the dynamic symbol table
7214 and dynamic string table and debug sections. */
7215 eh_addr_size = is_32bit_elf ? 4 : 8;
7216 switch (filedata->file_header.e_machine)
7217 {
7218 case EM_MIPS:
7219 case EM_MIPS_RS3_LE:
7220 /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
7221 FDE addresses. However, the ABI also has a semi-official ILP32
7222 variant for which the normal FDE address size rules apply.
7223
7224 GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
7225 section, where XX is the size of longs in bits. Unfortunately,
7226 earlier compilers provided no way of distinguishing ILP32 objects
7227 from LP64 objects, so if there's any doubt, we should assume that
7228 the official LP64 form is being used. */
7229 if ((filedata->file_header.e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64
7230 && find_section (filedata, ".gcc_compiled_long32") == NULL)
7231 eh_addr_size = 8;
7232 break;
7233
7234 case EM_H8_300:
7235 case EM_H8_300H:
7236 switch (filedata->file_header.e_flags & EF_H8_MACH)
7237 {
7238 case E_H8_MACH_H8300:
7239 case E_H8_MACH_H8300HN:
7240 case E_H8_MACH_H8300SN:
7241 case E_H8_MACH_H8300SXN:
7242 eh_addr_size = 2;
7243 break;
7244 case E_H8_MACH_H8300H:
7245 case E_H8_MACH_H8300S:
7246 case E_H8_MACH_H8300SX:
7247 eh_addr_size = 4;
7248 break;
7249 }
7250 break;
7251
7252 case EM_M32C_OLD:
7253 case EM_M32C:
7254 switch (filedata->file_header.e_flags & EF_M32C_CPU_MASK)
7255 {
7256 case EF_M32C_CPU_M16C:
7257 eh_addr_size = 2;
7258 break;
7259 }
7260 break;
7261 }
7262
7263 #define CHECK_ENTSIZE_VALUES(section, i, size32, size64) \
7264 do \
7265 { \
7266 uint64_t expected_entsize = is_32bit_elf ? size32 : size64; \
7267 if (section->sh_entsize != expected_entsize) \
7268 { \
7269 error (_("Section %d has invalid sh_entsize of %" PRIx64 "\n"), \
7270 i, section->sh_entsize); \
7271 error (_("(Using the expected size of %" PRIx64 " for the rest of this dump)\n"), \
7272 expected_entsize); \
7273 section->sh_entsize = expected_entsize; \
7274 } \
7275 } \
7276 while (0)
7277
7278 #define CHECK_ENTSIZE(section, i, type) \
7279 CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type), \
7280 sizeof (Elf64_External_##type))
7281
7282 for (i = 0, section = filedata->section_headers;
7283 i < filedata->file_header.e_shnum;
7284 i++, section++)
7285 {
7286 const char *name = section_name_print (filedata, section);
7287
7288 /* Run some sanity checks on the headers and
7289 possibly fill in some file data as well. */
7290 switch (section->sh_type)
7291 {
7292 case SHT_DYNSYM:
7293 if (filedata->dynamic_symbols != NULL)
7294 {
7295 error (_("File contains multiple dynamic symbol tables\n"));
7296 continue;
7297 }
7298
7299 CHECK_ENTSIZE (section, i, Sym);
7300 filedata->dynamic_symbols
7301 = get_elf_symbols (filedata, section, &filedata->num_dynamic_syms);
7302 filedata->dynamic_symtab_section = section;
7303 break;
7304
7305 case SHT_STRTAB:
7306 if (streq (name, ".dynstr"))
7307 {
7308 if (filedata->dynamic_strings != NULL)
7309 {
7310 error (_("File contains multiple dynamic string tables\n"));
7311 continue;
7312 }
7313
7314 filedata->dynamic_strings
7315 = (char *) get_data (NULL, filedata, section->sh_offset,
7316 1, section->sh_size, _("dynamic strings"));
7317 filedata->dynamic_strings_length
7318 = filedata->dynamic_strings == NULL ? 0 : section->sh_size;
7319 filedata->dynamic_strtab_section = section;
7320 }
7321 break;
7322
7323 case SHT_SYMTAB_SHNDX:
7324 {
7325 elf_section_list * entry = xmalloc (sizeof * entry);
7326
7327 entry->hdr = section;
7328 entry->next = filedata->symtab_shndx_list;
7329 filedata->symtab_shndx_list = entry;
7330 }
7331 break;
7332
7333 case SHT_SYMTAB:
7334 CHECK_ENTSIZE (section, i, Sym);
7335 break;
7336
7337 case SHT_GROUP:
7338 CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
7339 break;
7340
7341 case SHT_REL:
7342 CHECK_ENTSIZE (section, i, Rel);
7343 if (do_checks && section->sh_size == 0)
7344 warn (_("Section '%s': zero-sized relocation section\n"), name);
7345 break;
7346
7347 case SHT_RELA:
7348 CHECK_ENTSIZE (section, i, Rela);
7349 if (do_checks && section->sh_size == 0)
7350 warn (_("Section '%s': zero-sized relocation section\n"), name);
7351 break;
7352
7353 case SHT_RELR:
7354 CHECK_ENTSIZE (section, i, Relr);
7355 break;
7356
7357 case SHT_NOTE:
7358 case SHT_PROGBITS:
7359 /* Having a zero sized section is not illegal according to the
7360 ELF standard, but it might be an indication that something
7361 is wrong. So issue a warning if we are running in lint mode. */
7362 if (do_checks && section->sh_size == 0)
7363 warn (_("Section '%s': has a size of zero - is this intended ?\n"), name);
7364 break;
7365
7366 default:
7367 break;
7368 }
7369
7370 if ((do_debugging || do_debug_info || do_debug_abbrevs
7371 || do_debug_lines || do_debug_pubnames || do_debug_pubtypes
7372 || do_debug_aranges || do_debug_frames || do_debug_macinfo
7373 || do_debug_str || do_debug_str_offsets || do_debug_loc
7374 || do_debug_ranges
7375 || do_debug_addr || do_debug_cu_index || do_debug_links)
7376 && (startswith (name, ".debug_")
7377 || startswith (name, ".zdebug_")))
7378 {
7379 if (name[1] == 'z')
7380 name += sizeof (".zdebug_") - 1;
7381 else
7382 name += sizeof (".debug_") - 1;
7383
7384 if (do_debugging
7385 || (do_debug_info && startswith (name, "info"))
7386 || (do_debug_info && startswith (name, "types"))
7387 || (do_debug_abbrevs && startswith (name, "abbrev"))
7388 || (do_debug_lines && strcmp (name, "line") == 0)
7389 || (do_debug_lines && startswith (name, "line."))
7390 || (do_debug_pubnames && startswith (name, "pubnames"))
7391 || (do_debug_pubtypes && startswith (name, "pubtypes"))
7392 || (do_debug_pubnames && startswith (name, "gnu_pubnames"))
7393 || (do_debug_pubtypes && startswith (name, "gnu_pubtypes"))
7394 || (do_debug_aranges && startswith (name, "aranges"))
7395 || (do_debug_ranges && startswith (name, "ranges"))
7396 || (do_debug_ranges && startswith (name, "rnglists"))
7397 || (do_debug_frames && startswith (name, "frame"))
7398 || (do_debug_macinfo && startswith (name, "macinfo"))
7399 || (do_debug_macinfo && startswith (name, "macro"))
7400 || (do_debug_str && startswith (name, "str"))
7401 || (do_debug_links && startswith (name, "sup"))
7402 || (do_debug_str_offsets && startswith (name, "str_offsets"))
7403 || (do_debug_loc && startswith (name, "loc"))
7404 || (do_debug_loc && startswith (name, "loclists"))
7405 || (do_debug_addr && startswith (name, "addr"))
7406 || (do_debug_cu_index && startswith (name, "cu_index"))
7407 || (do_debug_cu_index && startswith (name, "tu_index"))
7408 )
7409 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7410 }
7411 /* Linkonce section to be combined with .debug_info at link time. */
7412 else if ((do_debugging || do_debug_info)
7413 && startswith (name, ".gnu.linkonce.wi."))
7414 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7415 else if (do_debug_frames && streq (name, ".eh_frame"))
7416 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7417 else if (do_gdb_index && (streq (name, ".gdb_index")
7418 || streq (name, ".debug_names")))
7419 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7420 /* Trace sections for Itanium VMS. */
7421 else if ((do_debugging || do_trace_info || do_trace_abbrevs
7422 || do_trace_aranges)
7423 && startswith (name, ".trace_"))
7424 {
7425 name += sizeof (".trace_") - 1;
7426
7427 if (do_debugging
7428 || (do_trace_info && streq (name, "info"))
7429 || (do_trace_abbrevs && streq (name, "abbrev"))
7430 || (do_trace_aranges && streq (name, "aranges"))
7431 )
7432 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7433 }
7434 else if ((do_debugging || do_debug_links)
7435 && (startswith (name, ".gnu_debuglink")
7436 || startswith (name, ".gnu_debugaltlink")))
7437 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7438 }
7439
7440 if (! do_sections)
7441 return true;
7442
7443 if (filedata->is_separate && ! process_links)
7444 return true;
7445
7446 if (filedata->is_separate)
7447 printf (_("\nSection Headers in linked file '%s':\n"), filedata->file_name);
7448 else if (filedata->file_header.e_shnum > 1)
7449 printf (_("\nSection Headers:\n"));
7450 else
7451 printf (_("\nSection Header:\n"));
7452
7453 if (is_32bit_elf)
7454 {
7455 if (do_section_details)
7456 {
7457 printf (_(" [Nr] Name\n"));
7458 printf (_(" Type Addr Off Size ES Lk Inf Al\n"));
7459 }
7460 else
7461 printf
7462 (_(" [Nr] Name Type Addr Off Size ES Flg Lk Inf Al\n"));
7463 }
7464 else if (do_wide)
7465 {
7466 if (do_section_details)
7467 {
7468 printf (_(" [Nr] Name\n"));
7469 printf (_(" Type Address Off Size ES Lk Inf Al\n"));
7470 }
7471 else
7472 printf
7473 (_(" [Nr] Name Type Address Off Size ES Flg Lk Inf Al\n"));
7474 }
7475 else
7476 {
7477 if (do_section_details)
7478 {
7479 printf (_(" [Nr] Name\n"));
7480 printf (_(" Type Address Offset Link\n"));
7481 printf (_(" Size EntSize Info Align\n"));
7482 }
7483 else
7484 {
7485 printf (_(" [Nr] Name Type Address Offset\n"));
7486 printf (_(" Size EntSize Flags Link Info Align\n"));
7487 }
7488 }
7489
7490 if (do_section_details)
7491 printf (_(" Flags\n"));
7492
7493 for (i = 0, section = filedata->section_headers;
7494 i < filedata->file_header.e_shnum;
7495 i++, section++)
7496 {
7497 /* Run some sanity checks on the section header. */
7498
7499 /* Check the sh_link field. */
7500 switch (section->sh_type)
7501 {
7502 case SHT_REL:
7503 case SHT_RELA:
7504 if (section->sh_link == 0
7505 && (filedata->file_header.e_type == ET_EXEC
7506 || filedata->file_header.e_type == ET_DYN))
7507 /* A dynamic relocation section where all entries use a
7508 zero symbol index need not specify a symtab section. */
7509 break;
7510 /* Fall through. */
7511 case SHT_SYMTAB_SHNDX:
7512 case SHT_GROUP:
7513 case SHT_HASH:
7514 case SHT_GNU_HASH:
7515 case SHT_GNU_versym:
7516 if (section->sh_link == 0
7517 || section->sh_link >= filedata->file_header.e_shnum
7518 || (filedata->section_headers[section->sh_link].sh_type != SHT_SYMTAB
7519 && filedata->section_headers[section->sh_link].sh_type != SHT_DYNSYM))
7520 warn (_("[%2u]: Link field (%u) should index a symtab section.\n"),
7521 i, section->sh_link);
7522 break;
7523
7524 case SHT_DYNAMIC:
7525 case SHT_SYMTAB:
7526 case SHT_DYNSYM:
7527 case SHT_GNU_verneed:
7528 case SHT_GNU_verdef:
7529 case SHT_GNU_LIBLIST:
7530 if (section->sh_link == 0
7531 || section->sh_link >= filedata->file_header.e_shnum
7532 || filedata->section_headers[section->sh_link].sh_type != SHT_STRTAB)
7533 warn (_("[%2u]: Link field (%u) should index a string section.\n"),
7534 i, section->sh_link);
7535 break;
7536
7537 case SHT_INIT_ARRAY:
7538 case SHT_FINI_ARRAY:
7539 case SHT_PREINIT_ARRAY:
7540 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
7541 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
7542 i, section->sh_link);
7543 break;
7544
7545 default:
7546 /* FIXME: Add support for target specific section types. */
7547 #if 0 /* Currently we do not check other section types as there are too
7548 many special cases. Stab sections for example have a type
7549 of SHT_PROGBITS but an sh_link field that links to the .stabstr
7550 section. */
7551 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
7552 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
7553 i, section->sh_link);
7554 #endif
7555 break;
7556 }
7557
7558 /* Check the sh_info field. */
7559 switch (section->sh_type)
7560 {
7561 case SHT_REL:
7562 case SHT_RELA:
7563 if (section->sh_info == 0
7564 && (filedata->file_header.e_type == ET_EXEC
7565 || filedata->file_header.e_type == ET_DYN))
7566 /* Dynamic relocations apply to segments, so they do not
7567 need to specify the section they relocate. */
7568 break;
7569 if (section->sh_info == 0
7570 || section->sh_info >= filedata->file_header.e_shnum
7571 || (filedata->section_headers[section->sh_info].sh_type != SHT_PROGBITS
7572 && filedata->section_headers[section->sh_info].sh_type != SHT_NOBITS
7573 && filedata->section_headers[section->sh_info].sh_type != SHT_NOTE
7574 && filedata->section_headers[section->sh_info].sh_type != SHT_INIT_ARRAY
7575 && filedata->section_headers[section->sh_info].sh_type != SHT_FINI_ARRAY
7576 && filedata->section_headers[section->sh_info].sh_type != SHT_PREINIT_ARRAY
7577 /* FIXME: Are other section types valid ? */
7578 && filedata->section_headers[section->sh_info].sh_type < SHT_LOOS))
7579 warn (_("[%2u]: Info field (%u) should index a relocatable section.\n"),
7580 i, section->sh_info);
7581 break;
7582
7583 case SHT_DYNAMIC:
7584 case SHT_HASH:
7585 case SHT_SYMTAB_SHNDX:
7586 case SHT_INIT_ARRAY:
7587 case SHT_FINI_ARRAY:
7588 case SHT_PREINIT_ARRAY:
7589 if (section->sh_info != 0)
7590 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
7591 i, section->sh_info);
7592 break;
7593
7594 case SHT_GROUP:
7595 case SHT_SYMTAB:
7596 case SHT_DYNSYM:
7597 /* A symbol index - we assume that it is valid. */
7598 break;
7599
7600 default:
7601 /* FIXME: Add support for target specific section types. */
7602 if (section->sh_type == SHT_NOBITS)
7603 /* NOBITS section headers with non-zero sh_info fields can be
7604 created when a binary is stripped of everything but its debug
7605 information. The stripped sections have their headers
7606 preserved but their types set to SHT_NOBITS. So do not check
7607 this type of section. */
7608 ;
7609 else if (section->sh_flags & SHF_INFO_LINK)
7610 {
7611 if (section->sh_info < 1 || section->sh_info >= filedata->file_header.e_shnum)
7612 warn (_("[%2u]: Expected link to another section in info field"), i);
7613 }
7614 else if (section->sh_type < SHT_LOOS
7615 && (section->sh_flags & SHF_GNU_MBIND) == 0
7616 && section->sh_info != 0)
7617 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
7618 i, section->sh_info);
7619 break;
7620 }
7621
7622 /* Check the sh_size field. */
7623 if (section->sh_size > filedata->file_size
7624 && section->sh_type != SHT_NOBITS
7625 && section->sh_type != SHT_NULL
7626 && section->sh_type < SHT_LOOS)
7627 warn (_("Size of section %u is larger than the entire file!\n"), i);
7628
7629 printf (" [%2u] ", i);
7630 if (do_section_details)
7631 printf ("%s\n ", printable_section_name (filedata, section));
7632 else
7633 print_symbol (-17, section_name_print (filedata, section));
7634
7635 printf (do_wide ? " %-15s " : " %-15.15s ",
7636 get_section_type_name (filedata, section->sh_type));
7637
7638 if (is_32bit_elf)
7639 {
7640 const char * link_too_big = NULL;
7641
7642 print_vma (section->sh_addr, LONG_HEX);
7643
7644 printf ( " %6.6lx %6.6lx %2.2lx",
7645 (unsigned long) section->sh_offset,
7646 (unsigned long) section->sh_size,
7647 (unsigned long) section->sh_entsize);
7648
7649 if (do_section_details)
7650 fputs (" ", stdout);
7651 else
7652 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7653
7654 if (section->sh_link >= filedata->file_header.e_shnum)
7655 {
7656 link_too_big = "";
7657 /* The sh_link value is out of range. Normally this indicates
7658 an error but it can have special values in Solaris binaries. */
7659 switch (filedata->file_header.e_machine)
7660 {
7661 case EM_386:
7662 case EM_IAMCU:
7663 case EM_X86_64:
7664 case EM_L1OM:
7665 case EM_K1OM:
7666 case EM_OLD_SPARCV9:
7667 case EM_SPARC32PLUS:
7668 case EM_SPARCV9:
7669 case EM_SPARC:
7670 if (section->sh_link == (SHN_BEFORE & 0xffff))
7671 link_too_big = "BEFORE";
7672 else if (section->sh_link == (SHN_AFTER & 0xffff))
7673 link_too_big = "AFTER";
7674 break;
7675 default:
7676 break;
7677 }
7678 }
7679
7680 if (do_section_details)
7681 {
7682 if (link_too_big != NULL && * link_too_big)
7683 printf ("<%s> ", link_too_big);
7684 else
7685 printf ("%2u ", section->sh_link);
7686 printf ("%3u %2lu\n", section->sh_info,
7687 (unsigned long) section->sh_addralign);
7688 }
7689 else
7690 printf ("%2u %3u %2lu\n",
7691 section->sh_link,
7692 section->sh_info,
7693 (unsigned long) section->sh_addralign);
7694
7695 if (link_too_big && ! * link_too_big)
7696 warn (_("section %u: sh_link value of %u is larger than the number of sections\n"),
7697 i, section->sh_link);
7698 }
7699 else if (do_wide)
7700 {
7701 print_vma (section->sh_addr, LONG_HEX);
7702
7703 if ((long) section->sh_offset == section->sh_offset)
7704 printf (" %6.6lx", (unsigned long) section->sh_offset);
7705 else
7706 {
7707 putchar (' ');
7708 print_vma (section->sh_offset, LONG_HEX);
7709 }
7710
7711 if ((unsigned long) section->sh_size == section->sh_size)
7712 printf (" %6.6lx", (unsigned long) section->sh_size);
7713 else
7714 {
7715 putchar (' ');
7716 print_vma (section->sh_size, LONG_HEX);
7717 }
7718
7719 if ((unsigned long) section->sh_entsize == section->sh_entsize)
7720 printf (" %2.2lx", (unsigned long) section->sh_entsize);
7721 else
7722 {
7723 putchar (' ');
7724 print_vma (section->sh_entsize, LONG_HEX);
7725 }
7726
7727 if (do_section_details)
7728 fputs (" ", stdout);
7729 else
7730 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7731
7732 printf ("%2u %3u ", section->sh_link, section->sh_info);
7733
7734 if ((unsigned long) section->sh_addralign == section->sh_addralign)
7735 printf ("%2lu\n", (unsigned long) section->sh_addralign);
7736 else
7737 {
7738 print_vma (section->sh_addralign, DEC);
7739 putchar ('\n');
7740 }
7741 }
7742 else if (do_section_details)
7743 {
7744 putchar (' ');
7745 print_vma (section->sh_addr, LONG_HEX);
7746 if ((long) section->sh_offset == section->sh_offset)
7747 printf (" %16.16lx", (unsigned long) section->sh_offset);
7748 else
7749 {
7750 printf (" ");
7751 print_vma (section->sh_offset, LONG_HEX);
7752 }
7753 printf (" %u\n ", section->sh_link);
7754 print_vma (section->sh_size, LONG_HEX);
7755 putchar (' ');
7756 print_vma (section->sh_entsize, LONG_HEX);
7757
7758 printf (" %-16u %lu\n",
7759 section->sh_info,
7760 (unsigned long) section->sh_addralign);
7761 }
7762 else
7763 {
7764 putchar (' ');
7765 print_vma (section->sh_addr, LONG_HEX);
7766 if ((long) section->sh_offset == section->sh_offset)
7767 printf (" %8.8lx", (unsigned long) section->sh_offset);
7768 else
7769 {
7770 printf (" ");
7771 print_vma (section->sh_offset, LONG_HEX);
7772 }
7773 printf ("\n ");
7774 print_vma (section->sh_size, LONG_HEX);
7775 printf (" ");
7776 print_vma (section->sh_entsize, LONG_HEX);
7777
7778 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7779
7780 printf (" %2u %3u %lu\n",
7781 section->sh_link,
7782 section->sh_info,
7783 (unsigned long) section->sh_addralign);
7784 }
7785
7786 if (do_section_details)
7787 {
7788 printf (" %s\n", get_elf_section_flags (filedata, section->sh_flags));
7789 if ((section->sh_flags & SHF_COMPRESSED) != 0)
7790 {
7791 /* Minimum section size is 12 bytes for 32-bit compression
7792 header + 12 bytes for compressed data header. */
7793 unsigned char buf[24];
7794
7795 assert (sizeof (buf) >= sizeof (Elf64_External_Chdr));
7796 if (get_data (&buf, filedata, section->sh_offset, 1,
7797 sizeof (buf), _("compression header")))
7798 {
7799 Elf_Internal_Chdr chdr;
7800
7801 if (get_compression_header (&chdr, buf, sizeof (buf)) == 0)
7802 printf (_(" [<corrupt>]\n"));
7803 else
7804 {
7805 if (chdr.ch_type == ELFCOMPRESS_ZLIB)
7806 printf (" ZLIB, ");
7807 else if (chdr.ch_type == ELFCOMPRESS_ZSTD)
7808 printf (" ZSTD, ");
7809 else
7810 printf (_(" [<unknown>: 0x%x], "),
7811 chdr.ch_type);
7812 print_vma (chdr.ch_size, LONG_HEX);
7813 printf (", %lu\n", (unsigned long) chdr.ch_addralign);
7814 }
7815 }
7816 }
7817 }
7818 }
7819
7820 if (!do_section_details)
7821 {
7822 /* The ordering of the letters shown here matches the ordering of the
7823 corresponding SHF_xxx values, and hence the order in which these
7824 letters will be displayed to the user. */
7825 printf (_("Key to Flags:\n\
7826 W (write), A (alloc), X (execute), M (merge), S (strings), I (info),\n\
7827 L (link order), O (extra OS processing required), G (group), T (TLS),\n\
7828 C (compressed), x (unknown), o (OS specific), E (exclude),\n "));
7829 switch (filedata->file_header.e_ident[EI_OSABI])
7830 {
7831 case ELFOSABI_GNU:
7832 case ELFOSABI_FREEBSD:
7833 printf (_("R (retain), "));
7834 /* Fall through */
7835 case ELFOSABI_NONE:
7836 printf (_("D (mbind), "));
7837 break;
7838 default:
7839 break;
7840 }
7841 if (filedata->file_header.e_machine == EM_X86_64
7842 || filedata->file_header.e_machine == EM_L1OM
7843 || filedata->file_header.e_machine == EM_K1OM)
7844 printf (_("l (large), "));
7845 else if (filedata->file_header.e_machine == EM_ARM)
7846 printf (_("y (purecode), "));
7847 else if (filedata->file_header.e_machine == EM_PPC)
7848 printf (_("v (VLE), "));
7849 printf ("p (processor specific)\n");
7850 }
7851
7852 return true;
7853 }
7854
7855 static bool
7856 get_symtab (Filedata *filedata, Elf_Internal_Shdr *symsec,
7857 Elf_Internal_Sym **symtab, unsigned long *nsyms,
7858 char **strtab, unsigned long *strtablen)
7859 {
7860 *strtab = NULL;
7861 *strtablen = 0;
7862 *symtab = get_elf_symbols (filedata, symsec, nsyms);
7863
7864 if (*symtab == NULL)
7865 return false;
7866
7867 if (symsec->sh_link != 0)
7868 {
7869 Elf_Internal_Shdr *strsec;
7870
7871 if (symsec->sh_link >= filedata->file_header.e_shnum)
7872 {
7873 error (_("Bad sh_link in symbol table section\n"));
7874 free (*symtab);
7875 *symtab = NULL;
7876 *nsyms = 0;
7877 return false;
7878 }
7879
7880 strsec = filedata->section_headers + symsec->sh_link;
7881
7882 *strtab = (char *) get_data (NULL, filedata, strsec->sh_offset,
7883 1, strsec->sh_size, _("string table"));
7884 if (*strtab == NULL)
7885 {
7886 free (*symtab);
7887 *symtab = NULL;
7888 *nsyms = 0;
7889 return false;
7890 }
7891 *strtablen = strsec->sh_size;
7892 }
7893 return true;
7894 }
7895
7896 static const char *
7897 get_group_flags (unsigned int flags)
7898 {
7899 static char buff[128];
7900
7901 if (flags == 0)
7902 return "";
7903 else if (flags == GRP_COMDAT)
7904 return "COMDAT ";
7905
7906 snprintf (buff, sizeof buff, "[0x%x: %s%s%s]",
7907 flags,
7908 flags & GRP_MASKOS ? _("<OS specific>") : "",
7909 flags & GRP_MASKPROC ? _("<PROC specific>") : "",
7910 (flags & ~(GRP_COMDAT | GRP_MASKOS | GRP_MASKPROC)
7911 ? _("<unknown>") : ""));
7912
7913 return buff;
7914 }
7915
7916 static bool
7917 process_section_groups (Filedata * filedata)
7918 {
7919 Elf_Internal_Shdr * section;
7920 unsigned int i;
7921 struct group * group;
7922 Elf_Internal_Shdr * symtab_sec;
7923 Elf_Internal_Shdr * strtab_sec;
7924 Elf_Internal_Sym * symtab;
7925 unsigned long num_syms;
7926 char * strtab;
7927 size_t strtab_size;
7928
7929 /* Don't process section groups unless needed. */
7930 if (!do_unwind && !do_section_groups)
7931 return true;
7932
7933 if (filedata->file_header.e_shnum == 0)
7934 {
7935 if (do_section_groups)
7936 {
7937 if (filedata->is_separate)
7938 printf (_("\nThere are no sections group in linked file '%s'.\n"),
7939 filedata->file_name);
7940 else
7941 printf (_("\nThere are no section groups in this file.\n"));
7942 }
7943 return true;
7944 }
7945
7946 if (filedata->section_headers == NULL)
7947 {
7948 error (_("Section headers are not available!\n"));
7949 /* PR 13622: This can happen with a corrupt ELF header. */
7950 return false;
7951 }
7952
7953 filedata->section_headers_groups
7954 = (struct group **) calloc (filedata->file_header.e_shnum,
7955 sizeof (struct group *));
7956
7957 if (filedata->section_headers_groups == NULL)
7958 {
7959 error (_("Out of memory reading %u section group headers\n"),
7960 filedata->file_header.e_shnum);
7961 return false;
7962 }
7963
7964 /* Scan the sections for the group section. */
7965 filedata->group_count = 0;
7966 for (i = 0, section = filedata->section_headers;
7967 i < filedata->file_header.e_shnum;
7968 i++, section++)
7969 if (section->sh_type == SHT_GROUP)
7970 filedata->group_count++;
7971
7972 if (filedata->group_count == 0)
7973 {
7974 if (do_section_groups)
7975 {
7976 if (filedata->is_separate)
7977 printf (_("\nThere are no section groups in linked file '%s'.\n"),
7978 filedata->file_name);
7979 else
7980 printf (_("\nThere are no section groups in this file.\n"));
7981 }
7982
7983 return true;
7984 }
7985
7986 filedata->section_groups = (struct group *) calloc (filedata->group_count,
7987 sizeof (struct group));
7988
7989 if (filedata->section_groups == NULL)
7990 {
7991 error (_("Out of memory reading %lu groups\n"),
7992 (unsigned long) filedata->group_count);
7993 return false;
7994 }
7995
7996 symtab_sec = NULL;
7997 strtab_sec = NULL;
7998 symtab = NULL;
7999 num_syms = 0;
8000 strtab = NULL;
8001 strtab_size = 0;
8002
8003 if (filedata->is_separate)
8004 printf (_("Section groups in linked file '%s'\n"), filedata->file_name);
8005
8006 for (i = 0, section = filedata->section_headers, group = filedata->section_groups;
8007 i < filedata->file_header.e_shnum;
8008 i++, section++)
8009 {
8010 if (section->sh_type == SHT_GROUP)
8011 {
8012 const char * name = printable_section_name (filedata, section);
8013 const char * group_name;
8014 unsigned char * start;
8015 unsigned char * indices;
8016 unsigned int entry, j, size;
8017 Elf_Internal_Shdr * sec;
8018 Elf_Internal_Sym * sym;
8019
8020 /* Get the symbol table. */
8021 if (section->sh_link >= filedata->file_header.e_shnum
8022 || ((sec = filedata->section_headers + section->sh_link)->sh_type
8023 != SHT_SYMTAB))
8024 {
8025 error (_("Bad sh_link in group section `%s'\n"), name);
8026 continue;
8027 }
8028
8029 if (symtab_sec != sec)
8030 {
8031 symtab_sec = sec;
8032 free (symtab);
8033 symtab = get_elf_symbols (filedata, symtab_sec, & num_syms);
8034 }
8035
8036 if (symtab == NULL)
8037 {
8038 error (_("Corrupt header in group section `%s'\n"), name);
8039 continue;
8040 }
8041
8042 if (section->sh_info >= num_syms)
8043 {
8044 error (_("Bad sh_info in group section `%s'\n"), name);
8045 continue;
8046 }
8047
8048 sym = symtab + section->sh_info;
8049
8050 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8051 {
8052 if (sym->st_shndx == 0
8053 || sym->st_shndx >= filedata->file_header.e_shnum)
8054 {
8055 error (_("Bad sh_info in group section `%s'\n"), name);
8056 continue;
8057 }
8058
8059 group_name = section_name_print (filedata,
8060 filedata->section_headers
8061 + sym->st_shndx);
8062 strtab_sec = NULL;
8063 free (strtab);
8064 strtab = NULL;
8065 strtab_size = 0;
8066 }
8067 else
8068 {
8069 /* Get the string table. */
8070 if (symtab_sec->sh_link >= filedata->file_header.e_shnum)
8071 {
8072 strtab_sec = NULL;
8073 free (strtab);
8074 strtab = NULL;
8075 strtab_size = 0;
8076 }
8077 else if (strtab_sec
8078 != (sec = filedata->section_headers + symtab_sec->sh_link))
8079 {
8080 strtab_sec = sec;
8081 free (strtab);
8082
8083 strtab = (char *) get_data (NULL, filedata, strtab_sec->sh_offset,
8084 1, strtab_sec->sh_size,
8085 _("string table"));
8086 strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
8087 }
8088 group_name = sym->st_name < strtab_size
8089 ? strtab + sym->st_name : _("<corrupt>");
8090 }
8091
8092 /* PR 17531: file: loop. */
8093 if (section->sh_entsize > section->sh_size)
8094 {
8095 error (_("Section %s has sh_entsize (0x%lx) which is larger than its size (0x%lx)\n"),
8096 printable_section_name (filedata, section),
8097 (unsigned long) section->sh_entsize,
8098 (unsigned long) section->sh_size);
8099 continue;
8100 }
8101
8102 start = (unsigned char *) get_data (NULL, filedata, section->sh_offset,
8103 1, section->sh_size,
8104 _("section data"));
8105 if (start == NULL)
8106 continue;
8107
8108 indices = start;
8109 size = (section->sh_size / section->sh_entsize) - 1;
8110 entry = byte_get (indices, 4);
8111 indices += 4;
8112
8113 if (do_section_groups)
8114 {
8115 printf (_("\n%sgroup section [%5u] `%s' [%s] contains %u sections:\n"),
8116 get_group_flags (entry), i, name, group_name, size);
8117
8118 printf (_(" [Index] Name\n"));
8119 }
8120
8121 group->group_index = i;
8122
8123 for (j = 0; j < size; j++)
8124 {
8125 struct group_list * g;
8126
8127 entry = byte_get (indices, 4);
8128 indices += 4;
8129
8130 if (entry >= filedata->file_header.e_shnum)
8131 {
8132 static unsigned num_group_errors = 0;
8133
8134 if (num_group_errors ++ < 10)
8135 {
8136 error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
8137 entry, i, filedata->file_header.e_shnum - 1);
8138 if (num_group_errors == 10)
8139 warn (_("Further error messages about overlarge group section indices suppressed\n"));
8140 }
8141 continue;
8142 }
8143
8144 if (filedata->section_headers_groups [entry] != NULL)
8145 {
8146 if (entry)
8147 {
8148 static unsigned num_errs = 0;
8149
8150 if (num_errs ++ < 10)
8151 {
8152 error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
8153 entry, i,
8154 filedata->section_headers_groups [entry]->group_index);
8155 if (num_errs == 10)
8156 warn (_("Further error messages about already contained group sections suppressed\n"));
8157 }
8158 continue;
8159 }
8160 else
8161 {
8162 /* Intel C/C++ compiler may put section 0 in a
8163 section group. We just warn it the first time
8164 and ignore it afterwards. */
8165 static bool warned = false;
8166 if (!warned)
8167 {
8168 error (_("section 0 in group section [%5u]\n"),
8169 filedata->section_headers_groups [entry]->group_index);
8170 warned = true;
8171 }
8172 }
8173 }
8174
8175 filedata->section_headers_groups [entry] = group;
8176
8177 if (do_section_groups)
8178 {
8179 sec = filedata->section_headers + entry;
8180 printf (" [%5u] %s\n", entry, printable_section_name (filedata, sec));
8181 }
8182
8183 g = (struct group_list *) xmalloc (sizeof (struct group_list));
8184 g->section_index = entry;
8185 g->next = group->root;
8186 group->root = g;
8187 }
8188
8189 free (start);
8190
8191 group++;
8192 }
8193 }
8194
8195 free (symtab);
8196 free (strtab);
8197 return true;
8198 }
8199
8200 /* Data used to display dynamic fixups. */
8201
8202 struct ia64_vms_dynfixup
8203 {
8204 uint64_t needed_ident; /* Library ident number. */
8205 uint64_t needed; /* Index in the dstrtab of the library name. */
8206 uint64_t fixup_needed; /* Index of the library. */
8207 uint64_t fixup_rela_cnt; /* Number of fixups. */
8208 uint64_t fixup_rela_off; /* Fixups offset in the dynamic segment. */
8209 };
8210
8211 /* Data used to display dynamic relocations. */
8212
8213 struct ia64_vms_dynimgrela
8214 {
8215 uint64_t img_rela_cnt; /* Number of relocations. */
8216 uint64_t img_rela_off; /* Reloc offset in the dynamic segment. */
8217 };
8218
8219 /* Display IA-64 OpenVMS dynamic fixups (used to dynamically link a shared
8220 library). */
8221
8222 static bool
8223 dump_ia64_vms_dynamic_fixups (Filedata * filedata,
8224 struct ia64_vms_dynfixup * fixup,
8225 const char * strtab,
8226 unsigned int strtab_sz)
8227 {
8228 Elf64_External_VMS_IMAGE_FIXUP * imfs;
8229 long i;
8230 const char * lib_name;
8231
8232 imfs = get_data (NULL, filedata,
8233 filedata->dynamic_addr + fixup->fixup_rela_off,
8234 sizeof (*imfs), fixup->fixup_rela_cnt,
8235 _("dynamic section image fixups"));
8236 if (!imfs)
8237 return false;
8238
8239 if (fixup->needed < strtab_sz)
8240 lib_name = strtab + fixup->needed;
8241 else
8242 {
8243 warn (_("corrupt library name index of 0x%lx found in dynamic entry"),
8244 (unsigned long) fixup->needed);
8245 lib_name = "???";
8246 }
8247
8248 printf (_("\nImage fixups for needed library #%d: %s - ident: %lx\n"),
8249 (int) fixup->fixup_needed, lib_name, (long) fixup->needed_ident);
8250 printf
8251 (_("Seg Offset Type SymVec DataType\n"));
8252
8253 for (i = 0; i < (long) fixup->fixup_rela_cnt; i++)
8254 {
8255 unsigned int type;
8256 const char *rtype;
8257
8258 printf ("%3u ", (unsigned) BYTE_GET (imfs [i].fixup_seg));
8259 printf ("%016" PRIx64 " ", BYTE_GET (imfs [i].fixup_offset));
8260 type = BYTE_GET (imfs [i].type);
8261 rtype = elf_ia64_reloc_type (type);
8262 if (rtype == NULL)
8263 printf ("0x%08x ", type);
8264 else
8265 printf ("%-32s ", rtype);
8266 printf ("%6u ", (unsigned) BYTE_GET (imfs [i].symvec_index));
8267 printf ("0x%08x\n", (unsigned) BYTE_GET (imfs [i].data_type));
8268 }
8269
8270 free (imfs);
8271 return true;
8272 }
8273
8274 /* Display IA-64 OpenVMS dynamic relocations (used to relocate an image). */
8275
8276 static bool
8277 dump_ia64_vms_dynamic_relocs (Filedata * filedata, struct ia64_vms_dynimgrela *imgrela)
8278 {
8279 Elf64_External_VMS_IMAGE_RELA *imrs;
8280 long i;
8281
8282 imrs = get_data (NULL, filedata,
8283 filedata->dynamic_addr + imgrela->img_rela_off,
8284 sizeof (*imrs), imgrela->img_rela_cnt,
8285 _("dynamic section image relocations"));
8286 if (!imrs)
8287 return false;
8288
8289 printf (_("\nImage relocs\n"));
8290 printf
8291 (_("Seg Offset Type Addend Seg Sym Off\n"));
8292
8293 for (i = 0; i < (long) imgrela->img_rela_cnt; i++)
8294 {
8295 unsigned int type;
8296 const char *rtype;
8297
8298 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].rela_seg));
8299 printf ("%08" PRIx64 " ", BYTE_GET (imrs [i].rela_offset));
8300 type = BYTE_GET (imrs [i].type);
8301 rtype = elf_ia64_reloc_type (type);
8302 if (rtype == NULL)
8303 printf ("0x%08x ", type);
8304 else
8305 printf ("%-31s ", rtype);
8306 print_vma (BYTE_GET (imrs [i].addend), FULL_HEX);
8307 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].sym_seg));
8308 printf ("%08" PRIx64 "\n", BYTE_GET (imrs [i].sym_offset));
8309 }
8310
8311 free (imrs);
8312 return true;
8313 }
8314
8315 /* Display IA-64 OpenVMS dynamic relocations and fixups. */
8316
8317 static bool
8318 process_ia64_vms_dynamic_relocs (Filedata * filedata)
8319 {
8320 struct ia64_vms_dynfixup fixup;
8321 struct ia64_vms_dynimgrela imgrela;
8322 Elf_Internal_Dyn *entry;
8323 uint64_t strtab_off = 0;
8324 uint64_t strtab_sz = 0;
8325 char *strtab = NULL;
8326 bool res = true;
8327
8328 memset (&fixup, 0, sizeof (fixup));
8329 memset (&imgrela, 0, sizeof (imgrela));
8330
8331 /* Note: the order of the entries is specified by the OpenVMS specs. */
8332 for (entry = filedata->dynamic_section;
8333 entry < filedata->dynamic_section + filedata->dynamic_nent;
8334 entry++)
8335 {
8336 switch (entry->d_tag)
8337 {
8338 case DT_IA_64_VMS_STRTAB_OFFSET:
8339 strtab_off = entry->d_un.d_val;
8340 break;
8341 case DT_STRSZ:
8342 strtab_sz = entry->d_un.d_val;
8343 if (strtab == NULL)
8344 strtab = get_data (NULL, filedata,
8345 filedata->dynamic_addr + strtab_off,
8346 1, strtab_sz, _("dynamic string section"));
8347 if (strtab == NULL)
8348 strtab_sz = 0;
8349 break;
8350
8351 case DT_IA_64_VMS_NEEDED_IDENT:
8352 fixup.needed_ident = entry->d_un.d_val;
8353 break;
8354 case DT_NEEDED:
8355 fixup.needed = entry->d_un.d_val;
8356 break;
8357 case DT_IA_64_VMS_FIXUP_NEEDED:
8358 fixup.fixup_needed = entry->d_un.d_val;
8359 break;
8360 case DT_IA_64_VMS_FIXUP_RELA_CNT:
8361 fixup.fixup_rela_cnt = entry->d_un.d_val;
8362 break;
8363 case DT_IA_64_VMS_FIXUP_RELA_OFF:
8364 fixup.fixup_rela_off = entry->d_un.d_val;
8365 if (! dump_ia64_vms_dynamic_fixups (filedata, &fixup, strtab, strtab_sz))
8366 res = false;
8367 break;
8368 case DT_IA_64_VMS_IMG_RELA_CNT:
8369 imgrela.img_rela_cnt = entry->d_un.d_val;
8370 break;
8371 case DT_IA_64_VMS_IMG_RELA_OFF:
8372 imgrela.img_rela_off = entry->d_un.d_val;
8373 if (! dump_ia64_vms_dynamic_relocs (filedata, &imgrela))
8374 res = false;
8375 break;
8376
8377 default:
8378 break;
8379 }
8380 }
8381
8382 free (strtab);
8383
8384 return res;
8385 }
8386
8387 static struct
8388 {
8389 const char * name;
8390 int reloc;
8391 int size;
8392 relocation_type rel_type;
8393 }
8394 dynamic_relocations [] =
8395 {
8396 { "REL", DT_REL, DT_RELSZ, reltype_rel },
8397 { "RELA", DT_RELA, DT_RELASZ, reltype_rela },
8398 { "RELR", DT_RELR, DT_RELRSZ, reltype_relr },
8399 { "PLT", DT_JMPREL, DT_PLTRELSZ, reltype_unknown }
8400 };
8401
8402 /* Process the reloc section. */
8403
8404 static bool
8405 process_relocs (Filedata * filedata)
8406 {
8407 unsigned long rel_size;
8408 unsigned long rel_offset;
8409
8410 if (!do_reloc)
8411 return true;
8412
8413 if (do_using_dynamic)
8414 {
8415 relocation_type rel_type;
8416 const char * name;
8417 bool has_dynamic_reloc;
8418 unsigned int i;
8419
8420 has_dynamic_reloc = false;
8421
8422 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
8423 {
8424 rel_type = dynamic_relocations [i].rel_type;
8425 name = dynamic_relocations [i].name;
8426 rel_size = filedata->dynamic_info[dynamic_relocations [i].size];
8427 rel_offset = filedata->dynamic_info[dynamic_relocations [i].reloc];
8428
8429 if (rel_size)
8430 has_dynamic_reloc = true;
8431
8432 if (rel_type == reltype_unknown)
8433 {
8434 if (dynamic_relocations [i].reloc == DT_JMPREL)
8435 switch (filedata->dynamic_info[DT_PLTREL])
8436 {
8437 case DT_REL:
8438 rel_type = reltype_rel;
8439 break;
8440 case DT_RELA:
8441 rel_type = reltype_rela;
8442 break;
8443 }
8444 }
8445
8446 if (rel_size)
8447 {
8448 if (filedata->is_separate)
8449 printf
8450 (_("\nIn linked file '%s' section '%s' at offset 0x%lx contains %ld bytes:\n"),
8451 filedata->file_name, name, rel_offset, rel_size);
8452 else
8453 printf
8454 (_("\n'%s' relocation section at offset 0x%lx contains %ld bytes:\n"),
8455 name, rel_offset, rel_size);
8456
8457 dump_relocations (filedata,
8458 offset_from_vma (filedata, rel_offset, rel_size),
8459 rel_size,
8460 filedata->dynamic_symbols,
8461 filedata->num_dynamic_syms,
8462 filedata->dynamic_strings,
8463 filedata->dynamic_strings_length,
8464 rel_type, true /* is_dynamic */);
8465 }
8466 }
8467
8468 if (is_ia64_vms (filedata))
8469 if (process_ia64_vms_dynamic_relocs (filedata))
8470 has_dynamic_reloc = true;
8471
8472 if (! has_dynamic_reloc)
8473 {
8474 if (filedata->is_separate)
8475 printf (_("\nThere are no dynamic relocations in linked file '%s'.\n"),
8476 filedata->file_name);
8477 else
8478 printf (_("\nThere are no dynamic relocations in this file.\n"));
8479 }
8480 }
8481 else
8482 {
8483 Elf_Internal_Shdr * section;
8484 unsigned long i;
8485 bool found = false;
8486
8487 for (i = 0, section = filedata->section_headers;
8488 i < filedata->file_header.e_shnum;
8489 i++, section++)
8490 {
8491 if ( section->sh_type != SHT_RELA
8492 && section->sh_type != SHT_REL
8493 && section->sh_type != SHT_RELR)
8494 continue;
8495
8496 rel_offset = section->sh_offset;
8497 rel_size = section->sh_size;
8498
8499 if (rel_size)
8500 {
8501 relocation_type rel_type;
8502 unsigned long num_rela;
8503
8504 if (filedata->is_separate)
8505 printf (_("\nIn linked file '%s' relocation section "),
8506 filedata->file_name);
8507 else
8508 printf (_("\nRelocation section "));
8509
8510 if (filedata->string_table == NULL)
8511 printf ("%d", section->sh_name);
8512 else
8513 printf ("'%s'", printable_section_name (filedata, section));
8514
8515 num_rela = rel_size / section->sh_entsize;
8516 printf (ngettext (" at offset 0x%lx contains %lu entry:\n",
8517 " at offset 0x%lx contains %lu entries:\n",
8518 num_rela),
8519 rel_offset, num_rela);
8520
8521 rel_type = section->sh_type == SHT_RELA ? reltype_rela :
8522 section->sh_type == SHT_REL ? reltype_rel : reltype_relr;
8523
8524 if (section->sh_link != 0
8525 && section->sh_link < filedata->file_header.e_shnum)
8526 {
8527 Elf_Internal_Shdr * symsec;
8528 Elf_Internal_Sym * symtab;
8529 unsigned long nsyms;
8530 unsigned long strtablen = 0;
8531 char * strtab = NULL;
8532
8533 symsec = filedata->section_headers + section->sh_link;
8534 if (symsec->sh_type != SHT_SYMTAB
8535 && symsec->sh_type != SHT_DYNSYM)
8536 continue;
8537
8538 if (!get_symtab (filedata, symsec,
8539 &symtab, &nsyms, &strtab, &strtablen))
8540 continue;
8541
8542 dump_relocations (filedata, rel_offset, rel_size,
8543 symtab, nsyms, strtab, strtablen,
8544 rel_type,
8545 symsec->sh_type == SHT_DYNSYM);
8546 free (strtab);
8547 free (symtab);
8548 }
8549 else
8550 dump_relocations (filedata, rel_offset, rel_size,
8551 NULL, 0, NULL, 0, rel_type, false /* is_dynamic */);
8552
8553 found = true;
8554 }
8555 }
8556
8557 if (! found)
8558 {
8559 /* Users sometimes forget the -D option, so try to be helpful. */
8560 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
8561 {
8562 if (filedata->dynamic_info[dynamic_relocations [i].size])
8563 {
8564 if (filedata->is_separate)
8565 printf (_("\nThere are no static relocations in linked file '%s'."),
8566 filedata->file_name);
8567 else
8568 printf (_("\nThere are no static relocations in this file."));
8569 printf (_("\nTo see the dynamic relocations add --use-dynamic to the command line.\n"));
8570
8571 break;
8572 }
8573 }
8574 if (i == ARRAY_SIZE (dynamic_relocations))
8575 {
8576 if (filedata->is_separate)
8577 printf (_("\nThere are no relocations in linked file '%s'.\n"),
8578 filedata->file_name);
8579 else
8580 printf (_("\nThere are no relocations in this file.\n"));
8581 }
8582 }
8583 }
8584
8585 return true;
8586 }
8587
8588 /* An absolute address consists of a section and an offset. If the
8589 section is NULL, the offset itself is the address, otherwise, the
8590 address equals to LOAD_ADDRESS(section) + offset. */
8591
8592 struct absaddr
8593 {
8594 unsigned short section;
8595 uint64_t offset;
8596 };
8597
8598 /* Find the nearest symbol at or below ADDR. Returns the symbol
8599 name, if found, and the offset from the symbol to ADDR. */
8600
8601 static void
8602 find_symbol_for_address (Filedata * filedata,
8603 Elf_Internal_Sym * symtab,
8604 unsigned long nsyms,
8605 const char * strtab,
8606 unsigned long strtab_size,
8607 struct absaddr addr,
8608 const char ** symname,
8609 uint64_t * offset)
8610 {
8611 uint64_t dist = 0x100000;
8612 Elf_Internal_Sym * sym;
8613 Elf_Internal_Sym * beg;
8614 Elf_Internal_Sym * end;
8615 Elf_Internal_Sym * best = NULL;
8616
8617 REMOVE_ARCH_BITS (addr.offset);
8618 beg = symtab;
8619 end = symtab + nsyms;
8620
8621 while (beg < end)
8622 {
8623 uint64_t value;
8624
8625 sym = beg + (end - beg) / 2;
8626
8627 value = sym->st_value;
8628 REMOVE_ARCH_BITS (value);
8629
8630 if (sym->st_name != 0
8631 && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
8632 && addr.offset >= value
8633 && addr.offset - value < dist)
8634 {
8635 best = sym;
8636 dist = addr.offset - value;
8637 if (!dist)
8638 break;
8639 }
8640
8641 if (addr.offset < value)
8642 end = sym;
8643 else
8644 beg = sym + 1;
8645 }
8646
8647 if (best)
8648 {
8649 *symname = (best->st_name >= strtab_size
8650 ? _("<corrupt>") : strtab + best->st_name);
8651 *offset = dist;
8652 return;
8653 }
8654
8655 *symname = NULL;
8656 *offset = addr.offset;
8657 }
8658
8659 static /* signed */ int
8660 symcmp (const void *p, const void *q)
8661 {
8662 Elf_Internal_Sym *sp = (Elf_Internal_Sym *) p;
8663 Elf_Internal_Sym *sq = (Elf_Internal_Sym *) q;
8664
8665 return sp->st_value > sq->st_value ? 1 : (sp->st_value < sq->st_value ? -1 : 0);
8666 }
8667
8668 /* Process the unwind section. */
8669
8670 #include "unwind-ia64.h"
8671
8672 struct ia64_unw_table_entry
8673 {
8674 struct absaddr start;
8675 struct absaddr end;
8676 struct absaddr info;
8677 };
8678
8679 struct ia64_unw_aux_info
8680 {
8681 struct ia64_unw_table_entry * table; /* Unwind table. */
8682 unsigned long table_len; /* Length of unwind table. */
8683 unsigned char * info; /* Unwind info. */
8684 unsigned long info_size; /* Size of unwind info. */
8685 uint64_t info_addr; /* Starting address of unwind info. */
8686 uint64_t seg_base; /* Starting address of segment. */
8687 Elf_Internal_Sym * symtab; /* The symbol table. */
8688 unsigned long nsyms; /* Number of symbols. */
8689 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
8690 unsigned long nfuns; /* Number of entries in funtab. */
8691 char * strtab; /* The string table. */
8692 unsigned long strtab_size; /* Size of string table. */
8693 };
8694
8695 static bool
8696 dump_ia64_unwind (Filedata * filedata, struct ia64_unw_aux_info * aux)
8697 {
8698 struct ia64_unw_table_entry * tp;
8699 unsigned long j, nfuns;
8700 int in_body;
8701 bool res = true;
8702
8703 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
8704 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
8705 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
8706 aux->funtab[nfuns++] = aux->symtab[j];
8707 aux->nfuns = nfuns;
8708 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
8709
8710 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
8711 {
8712 uint64_t stamp;
8713 uint64_t offset;
8714 const unsigned char * dp;
8715 const unsigned char * head;
8716 const unsigned char * end;
8717 const char * procname;
8718
8719 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
8720 aux->strtab_size, tp->start, &procname, &offset);
8721
8722 fputs ("\n<", stdout);
8723
8724 if (procname)
8725 {
8726 fputs (procname, stdout);
8727
8728 if (offset)
8729 printf ("+%lx", (unsigned long) offset);
8730 }
8731
8732 fputs (">: [", stdout);
8733 print_vma (tp->start.offset, PREFIX_HEX);
8734 fputc ('-', stdout);
8735 print_vma (tp->end.offset, PREFIX_HEX);
8736 printf ("], info at +0x%lx\n",
8737 (unsigned long) (tp->info.offset - aux->seg_base));
8738
8739 /* PR 17531: file: 86232b32. */
8740 if (aux->info == NULL)
8741 continue;
8742
8743 offset = tp->info.offset;
8744 if (tp->info.section)
8745 {
8746 if (tp->info.section >= filedata->file_header.e_shnum)
8747 {
8748 warn (_("Invalid section %u in table entry %ld\n"),
8749 tp->info.section, (long) (tp - aux->table));
8750 res = false;
8751 continue;
8752 }
8753 offset += filedata->section_headers[tp->info.section].sh_addr;
8754 }
8755 offset -= aux->info_addr;
8756 /* PR 17531: file: 0997b4d1. */
8757 if (offset >= aux->info_size
8758 || aux->info_size - offset < 8)
8759 {
8760 warn (_("Invalid offset %lx in table entry %ld\n"),
8761 (long) tp->info.offset, (long) (tp - aux->table));
8762 res = false;
8763 continue;
8764 }
8765
8766 head = aux->info + offset;
8767 stamp = byte_get ((unsigned char *) head, sizeof (stamp));
8768
8769 printf (" v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
8770 (unsigned) UNW_VER (stamp),
8771 (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
8772 UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
8773 UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
8774 (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
8775
8776 if (UNW_VER (stamp) != 1)
8777 {
8778 printf (_("\tUnknown version.\n"));
8779 continue;
8780 }
8781
8782 in_body = 0;
8783 end = head + 8 + eh_addr_size * UNW_LENGTH (stamp);
8784 /* PR 17531: file: 16ceda89. */
8785 if (end > aux->info + aux->info_size)
8786 end = aux->info + aux->info_size;
8787 for (dp = head + 8; dp < end;)
8788 dp = unw_decode (dp, in_body, & in_body, end);
8789 }
8790
8791 free (aux->funtab);
8792
8793 return res;
8794 }
8795
8796 static bool
8797 slurp_ia64_unwind_table (Filedata * filedata,
8798 struct ia64_unw_aux_info * aux,
8799 Elf_Internal_Shdr * sec)
8800 {
8801 unsigned long size, nrelas, i;
8802 Elf_Internal_Phdr * seg;
8803 struct ia64_unw_table_entry * tep;
8804 Elf_Internal_Shdr * relsec;
8805 Elf_Internal_Rela * rela;
8806 Elf_Internal_Rela * rp;
8807 unsigned char * table;
8808 unsigned char * tp;
8809 Elf_Internal_Sym * sym;
8810 const char * relname;
8811
8812 aux->table_len = 0;
8813
8814 /* First, find the starting address of the segment that includes
8815 this section: */
8816
8817 if (filedata->file_header.e_phnum)
8818 {
8819 if (! get_program_headers (filedata))
8820 return false;
8821
8822 for (seg = filedata->program_headers;
8823 seg < filedata->program_headers + filedata->file_header.e_phnum;
8824 ++seg)
8825 {
8826 if (seg->p_type != PT_LOAD)
8827 continue;
8828
8829 if (sec->sh_addr >= seg->p_vaddr
8830 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
8831 {
8832 aux->seg_base = seg->p_vaddr;
8833 break;
8834 }
8835 }
8836 }
8837
8838 /* Second, build the unwind table from the contents of the unwind section: */
8839 size = sec->sh_size;
8840 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
8841 _("unwind table"));
8842 if (!table)
8843 return false;
8844
8845 aux->table_len = size / (3 * eh_addr_size);
8846 aux->table = (struct ia64_unw_table_entry *)
8847 xcmalloc (aux->table_len, sizeof (aux->table[0]));
8848 tep = aux->table;
8849
8850 for (tp = table; tp <= table + size - (3 * eh_addr_size); ++tep)
8851 {
8852 tep->start.section = SHN_UNDEF;
8853 tep->end.section = SHN_UNDEF;
8854 tep->info.section = SHN_UNDEF;
8855 tep->start.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8856 tep->end.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8857 tep->info.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8858 tep->start.offset += aux->seg_base;
8859 tep->end.offset += aux->seg_base;
8860 tep->info.offset += aux->seg_base;
8861 }
8862 free (table);
8863
8864 /* Third, apply any relocations to the unwind table: */
8865 for (relsec = filedata->section_headers;
8866 relsec < filedata->section_headers + filedata->file_header.e_shnum;
8867 ++relsec)
8868 {
8869 if (relsec->sh_type != SHT_RELA
8870 || relsec->sh_info >= filedata->file_header.e_shnum
8871 || filedata->section_headers + relsec->sh_info != sec)
8872 continue;
8873
8874 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
8875 & rela, & nrelas))
8876 {
8877 free (aux->table);
8878 aux->table = NULL;
8879 aux->table_len = 0;
8880 return false;
8881 }
8882
8883 for (rp = rela; rp < rela + nrelas; ++rp)
8884 {
8885 unsigned int sym_ndx;
8886 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
8887 relname = elf_ia64_reloc_type (r_type);
8888
8889 /* PR 17531: file: 9fa67536. */
8890 if (relname == NULL)
8891 {
8892 warn (_("Skipping unknown relocation type: %u\n"), r_type);
8893 continue;
8894 }
8895
8896 if (! startswith (relname, "R_IA64_SEGREL"))
8897 {
8898 warn (_("Skipping unexpected relocation type: %s\n"), relname);
8899 continue;
8900 }
8901
8902 i = rp->r_offset / (3 * eh_addr_size);
8903
8904 /* PR 17531: file: 5bc8d9bf. */
8905 if (i >= aux->table_len)
8906 {
8907 warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
8908 continue;
8909 }
8910
8911 sym_ndx = get_reloc_symindex (rp->r_info);
8912 if (sym_ndx >= aux->nsyms)
8913 {
8914 warn (_("Skipping reloc with invalid symbol index: %u\n"),
8915 sym_ndx);
8916 continue;
8917 }
8918 sym = aux->symtab + sym_ndx;
8919
8920 switch (rp->r_offset / eh_addr_size % 3)
8921 {
8922 case 0:
8923 aux->table[i].start.section = sym->st_shndx;
8924 aux->table[i].start.offset = rp->r_addend + sym->st_value;
8925 break;
8926 case 1:
8927 aux->table[i].end.section = sym->st_shndx;
8928 aux->table[i].end.offset = rp->r_addend + sym->st_value;
8929 break;
8930 case 2:
8931 aux->table[i].info.section = sym->st_shndx;
8932 aux->table[i].info.offset = rp->r_addend + sym->st_value;
8933 break;
8934 default:
8935 break;
8936 }
8937 }
8938
8939 free (rela);
8940 }
8941
8942 return true;
8943 }
8944
8945 static bool
8946 ia64_process_unwind (Filedata * filedata)
8947 {
8948 Elf_Internal_Shdr * sec;
8949 Elf_Internal_Shdr * unwsec = NULL;
8950 unsigned long i, unwcount = 0, unwstart = 0;
8951 struct ia64_unw_aux_info aux;
8952 bool res = true;
8953
8954 memset (& aux, 0, sizeof (aux));
8955
8956 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
8957 {
8958 if (sec->sh_type == SHT_SYMTAB)
8959 {
8960 if (aux.symtab)
8961 {
8962 error (_("Multiple symbol tables encountered\n"));
8963 free (aux.symtab);
8964 aux.symtab = NULL;
8965 free (aux.strtab);
8966 aux.strtab = NULL;
8967 }
8968 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
8969 &aux.strtab, &aux.strtab_size))
8970 return false;
8971 }
8972 else if (sec->sh_type == SHT_IA_64_UNWIND)
8973 unwcount++;
8974 }
8975
8976 if (!unwcount)
8977 printf (_("\nThere are no unwind sections in this file.\n"));
8978
8979 while (unwcount-- > 0)
8980 {
8981 const char *suffix;
8982 size_t len, len2;
8983
8984 for (i = unwstart, sec = filedata->section_headers + unwstart, unwsec = NULL;
8985 i < filedata->file_header.e_shnum; ++i, ++sec)
8986 if (sec->sh_type == SHT_IA_64_UNWIND)
8987 {
8988 unwsec = sec;
8989 break;
8990 }
8991 /* We have already counted the number of SHT_IA64_UNWIND
8992 sections so the loop above should never fail. */
8993 assert (unwsec != NULL);
8994
8995 unwstart = i + 1;
8996 len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
8997
8998 if ((unwsec->sh_flags & SHF_GROUP) != 0)
8999 {
9000 /* We need to find which section group it is in. */
9001 struct group_list * g;
9002
9003 if (filedata->section_headers_groups == NULL
9004 || filedata->section_headers_groups[i] == NULL)
9005 i = filedata->file_header.e_shnum;
9006 else
9007 {
9008 g = filedata->section_headers_groups[i]->root;
9009
9010 for (; g != NULL; g = g->next)
9011 {
9012 sec = filedata->section_headers + g->section_index;
9013
9014 if (section_name_valid (filedata, sec)
9015 && streq (section_name (filedata, sec),
9016 ELF_STRING_ia64_unwind_info))
9017 break;
9018 }
9019
9020 if (g == NULL)
9021 i = filedata->file_header.e_shnum;
9022 }
9023 }
9024 else if (section_name_valid (filedata, unwsec)
9025 && startswith (section_name (filedata, unwsec),
9026 ELF_STRING_ia64_unwind_once))
9027 {
9028 /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO. */
9029 len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
9030 suffix = section_name (filedata, unwsec) + len;
9031 for (i = 0, sec = filedata->section_headers;
9032 i < filedata->file_header.e_shnum;
9033 ++i, ++sec)
9034 if (section_name_valid (filedata, sec)
9035 && startswith (section_name (filedata, sec),
9036 ELF_STRING_ia64_unwind_info_once)
9037 && streq (section_name (filedata, sec) + len2, suffix))
9038 break;
9039 }
9040 else
9041 {
9042 /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
9043 .IA_64.unwind or BAR -> .IA_64.unwind_info. */
9044 len = sizeof (ELF_STRING_ia64_unwind) - 1;
9045 len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
9046 suffix = "";
9047 if (section_name_valid (filedata, unwsec)
9048 && startswith (section_name (filedata, unwsec),
9049 ELF_STRING_ia64_unwind))
9050 suffix = section_name (filedata, unwsec) + len;
9051 for (i = 0, sec = filedata->section_headers;
9052 i < filedata->file_header.e_shnum;
9053 ++i, ++sec)
9054 if (section_name_valid (filedata, sec)
9055 && startswith (section_name (filedata, sec),
9056 ELF_STRING_ia64_unwind_info)
9057 && streq (section_name (filedata, sec) + len2, suffix))
9058 break;
9059 }
9060
9061 if (i == filedata->file_header.e_shnum)
9062 {
9063 printf (_("\nCould not find unwind info section for "));
9064
9065 if (filedata->string_table == NULL)
9066 printf ("%d", unwsec->sh_name);
9067 else
9068 printf ("'%s'", printable_section_name (filedata, unwsec));
9069 }
9070 else
9071 {
9072 aux.info_addr = sec->sh_addr;
9073 aux.info = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1,
9074 sec->sh_size,
9075 _("unwind info"));
9076 aux.info_size = aux.info == NULL ? 0 : sec->sh_size;
9077
9078 printf (_("\nUnwind section "));
9079
9080 if (filedata->string_table == NULL)
9081 printf ("%d", unwsec->sh_name);
9082 else
9083 printf ("'%s'", printable_section_name (filedata, unwsec));
9084
9085 printf (_(" at offset 0x%lx contains %lu entries:\n"),
9086 (unsigned long) unwsec->sh_offset,
9087 (unsigned long) (unwsec->sh_size / (3 * eh_addr_size)));
9088
9089 if (slurp_ia64_unwind_table (filedata, & aux, unwsec)
9090 && aux.table_len > 0)
9091 dump_ia64_unwind (filedata, & aux);
9092
9093 free ((char *) aux.table);
9094 free ((char *) aux.info);
9095 aux.table = NULL;
9096 aux.info = NULL;
9097 }
9098 }
9099
9100 free (aux.symtab);
9101 free ((char *) aux.strtab);
9102
9103 return res;
9104 }
9105
9106 struct hppa_unw_table_entry
9107 {
9108 struct absaddr start;
9109 struct absaddr end;
9110 unsigned int Cannot_unwind:1; /* 0 */
9111 unsigned int Millicode:1; /* 1 */
9112 unsigned int Millicode_save_sr0:1; /* 2 */
9113 unsigned int Region_description:2; /* 3..4 */
9114 unsigned int reserved1:1; /* 5 */
9115 unsigned int Entry_SR:1; /* 6 */
9116 unsigned int Entry_FR:4; /* Number saved 7..10 */
9117 unsigned int Entry_GR:5; /* Number saved 11..15 */
9118 unsigned int Args_stored:1; /* 16 */
9119 unsigned int Variable_Frame:1; /* 17 */
9120 unsigned int Separate_Package_Body:1; /* 18 */
9121 unsigned int Frame_Extension_Millicode:1; /* 19 */
9122 unsigned int Stack_Overflow_Check:1; /* 20 */
9123 unsigned int Two_Instruction_SP_Increment:1; /* 21 */
9124 unsigned int Ada_Region:1; /* 22 */
9125 unsigned int cxx_info:1; /* 23 */
9126 unsigned int cxx_try_catch:1; /* 24 */
9127 unsigned int sched_entry_seq:1; /* 25 */
9128 unsigned int reserved2:1; /* 26 */
9129 unsigned int Save_SP:1; /* 27 */
9130 unsigned int Save_RP:1; /* 28 */
9131 unsigned int Save_MRP_in_frame:1; /* 29 */
9132 unsigned int extn_ptr_defined:1; /* 30 */
9133 unsigned int Cleanup_defined:1; /* 31 */
9134
9135 unsigned int MPE_XL_interrupt_marker:1; /* 0 */
9136 unsigned int HP_UX_interrupt_marker:1; /* 1 */
9137 unsigned int Large_frame:1; /* 2 */
9138 unsigned int Pseudo_SP_Set:1; /* 3 */
9139 unsigned int reserved4:1; /* 4 */
9140 unsigned int Total_frame_size:27; /* 5..31 */
9141 };
9142
9143 struct hppa_unw_aux_info
9144 {
9145 struct hppa_unw_table_entry * table; /* Unwind table. */
9146 unsigned long table_len; /* Length of unwind table. */
9147 uint64_t seg_base; /* Starting address of segment. */
9148 Elf_Internal_Sym * symtab; /* The symbol table. */
9149 unsigned long nsyms; /* Number of symbols. */
9150 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
9151 unsigned long nfuns; /* Number of entries in funtab. */
9152 char * strtab; /* The string table. */
9153 unsigned long strtab_size; /* Size of string table. */
9154 };
9155
9156 static bool
9157 dump_hppa_unwind (Filedata * filedata, struct hppa_unw_aux_info * aux)
9158 {
9159 struct hppa_unw_table_entry * tp;
9160 unsigned long j, nfuns;
9161 bool res = true;
9162
9163 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
9164 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
9165 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
9166 aux->funtab[nfuns++] = aux->symtab[j];
9167 aux->nfuns = nfuns;
9168 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
9169
9170 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
9171 {
9172 uint64_t offset;
9173 const char * procname;
9174
9175 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
9176 aux->strtab_size, tp->start, &procname,
9177 &offset);
9178
9179 fputs ("\n<", stdout);
9180
9181 if (procname)
9182 {
9183 fputs (procname, stdout);
9184
9185 if (offset)
9186 printf ("+%lx", (unsigned long) offset);
9187 }
9188
9189 fputs (">: [", stdout);
9190 print_vma (tp->start.offset, PREFIX_HEX);
9191 fputc ('-', stdout);
9192 print_vma (tp->end.offset, PREFIX_HEX);
9193 printf ("]\n\t");
9194
9195 #define PF(_m) if (tp->_m) printf (#_m " ");
9196 #define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
9197 PF(Cannot_unwind);
9198 PF(Millicode);
9199 PF(Millicode_save_sr0);
9200 /* PV(Region_description); */
9201 PF(Entry_SR);
9202 PV(Entry_FR);
9203 PV(Entry_GR);
9204 PF(Args_stored);
9205 PF(Variable_Frame);
9206 PF(Separate_Package_Body);
9207 PF(Frame_Extension_Millicode);
9208 PF(Stack_Overflow_Check);
9209 PF(Two_Instruction_SP_Increment);
9210 PF(Ada_Region);
9211 PF(cxx_info);
9212 PF(cxx_try_catch);
9213 PF(sched_entry_seq);
9214 PF(Save_SP);
9215 PF(Save_RP);
9216 PF(Save_MRP_in_frame);
9217 PF(extn_ptr_defined);
9218 PF(Cleanup_defined);
9219 PF(MPE_XL_interrupt_marker);
9220 PF(HP_UX_interrupt_marker);
9221 PF(Large_frame);
9222 PF(Pseudo_SP_Set);
9223 PV(Total_frame_size);
9224 #undef PF
9225 #undef PV
9226 }
9227
9228 printf ("\n");
9229
9230 free (aux->funtab);
9231
9232 return res;
9233 }
9234
9235 static bool
9236 slurp_hppa_unwind_table (Filedata * filedata,
9237 struct hppa_unw_aux_info * aux,
9238 Elf_Internal_Shdr * sec)
9239 {
9240 unsigned long size, unw_ent_size, nentries, nrelas, i;
9241 Elf_Internal_Phdr * seg;
9242 struct hppa_unw_table_entry * tep;
9243 Elf_Internal_Shdr * relsec;
9244 Elf_Internal_Rela * rela;
9245 Elf_Internal_Rela * rp;
9246 unsigned char * table;
9247 unsigned char * tp;
9248 Elf_Internal_Sym * sym;
9249 const char * relname;
9250
9251 /* First, find the starting address of the segment that includes
9252 this section. */
9253 if (filedata->file_header.e_phnum)
9254 {
9255 if (! get_program_headers (filedata))
9256 return false;
9257
9258 for (seg = filedata->program_headers;
9259 seg < filedata->program_headers + filedata->file_header.e_phnum;
9260 ++seg)
9261 {
9262 if (seg->p_type != PT_LOAD)
9263 continue;
9264
9265 if (sec->sh_addr >= seg->p_vaddr
9266 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
9267 {
9268 aux->seg_base = seg->p_vaddr;
9269 break;
9270 }
9271 }
9272 }
9273
9274 /* Second, build the unwind table from the contents of the unwind
9275 section. */
9276 size = sec->sh_size;
9277 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
9278 _("unwind table"));
9279 if (!table)
9280 return false;
9281
9282 unw_ent_size = 16;
9283 nentries = size / unw_ent_size;
9284 size = unw_ent_size * nentries;
9285
9286 aux->table_len = nentries;
9287 tep = aux->table = (struct hppa_unw_table_entry *)
9288 xcmalloc (nentries, sizeof (aux->table[0]));
9289
9290 for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
9291 {
9292 unsigned int tmp1, tmp2;
9293
9294 tep->start.section = SHN_UNDEF;
9295 tep->end.section = SHN_UNDEF;
9296
9297 tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
9298 tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
9299 tmp1 = byte_get ((unsigned char *) tp + 8, 4);
9300 tmp2 = byte_get ((unsigned char *) tp + 12, 4);
9301
9302 tep->start.offset += aux->seg_base;
9303 tep->end.offset += aux->seg_base;
9304
9305 tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
9306 tep->Millicode = (tmp1 >> 30) & 0x1;
9307 tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
9308 tep->Region_description = (tmp1 >> 27) & 0x3;
9309 tep->reserved1 = (tmp1 >> 26) & 0x1;
9310 tep->Entry_SR = (tmp1 >> 25) & 0x1;
9311 tep->Entry_FR = (tmp1 >> 21) & 0xf;
9312 tep->Entry_GR = (tmp1 >> 16) & 0x1f;
9313 tep->Args_stored = (tmp1 >> 15) & 0x1;
9314 tep->Variable_Frame = (tmp1 >> 14) & 0x1;
9315 tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
9316 tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
9317 tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
9318 tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
9319 tep->Ada_Region = (tmp1 >> 9) & 0x1;
9320 tep->cxx_info = (tmp1 >> 8) & 0x1;
9321 tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
9322 tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
9323 tep->reserved2 = (tmp1 >> 5) & 0x1;
9324 tep->Save_SP = (tmp1 >> 4) & 0x1;
9325 tep->Save_RP = (tmp1 >> 3) & 0x1;
9326 tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
9327 tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
9328 tep->Cleanup_defined = tmp1 & 0x1;
9329
9330 tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
9331 tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
9332 tep->Large_frame = (tmp2 >> 29) & 0x1;
9333 tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
9334 tep->reserved4 = (tmp2 >> 27) & 0x1;
9335 tep->Total_frame_size = tmp2 & 0x7ffffff;
9336 }
9337 free (table);
9338
9339 /* Third, apply any relocations to the unwind table. */
9340 for (relsec = filedata->section_headers;
9341 relsec < filedata->section_headers + filedata->file_header.e_shnum;
9342 ++relsec)
9343 {
9344 if (relsec->sh_type != SHT_RELA
9345 || relsec->sh_info >= filedata->file_header.e_shnum
9346 || filedata->section_headers + relsec->sh_info != sec)
9347 continue;
9348
9349 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
9350 & rela, & nrelas))
9351 return false;
9352
9353 for (rp = rela; rp < rela + nrelas; ++rp)
9354 {
9355 unsigned int sym_ndx;
9356 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
9357 relname = elf_hppa_reloc_type (r_type);
9358
9359 if (relname == NULL)
9360 {
9361 warn (_("Skipping unknown relocation type: %u\n"), r_type);
9362 continue;
9363 }
9364
9365 /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64. */
9366 if (! startswith (relname, "R_PARISC_SEGREL"))
9367 {
9368 warn (_("Skipping unexpected relocation type: %s\n"), relname);
9369 continue;
9370 }
9371
9372 i = rp->r_offset / unw_ent_size;
9373 if (i >= aux->table_len)
9374 {
9375 warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
9376 continue;
9377 }
9378
9379 sym_ndx = get_reloc_symindex (rp->r_info);
9380 if (sym_ndx >= aux->nsyms)
9381 {
9382 warn (_("Skipping reloc with invalid symbol index: %u\n"),
9383 sym_ndx);
9384 continue;
9385 }
9386 sym = aux->symtab + sym_ndx;
9387
9388 switch ((rp->r_offset % unw_ent_size) / 4)
9389 {
9390 case 0:
9391 aux->table[i].start.section = sym->st_shndx;
9392 aux->table[i].start.offset = sym->st_value + rp->r_addend;
9393 break;
9394 case 1:
9395 aux->table[i].end.section = sym->st_shndx;
9396 aux->table[i].end.offset = sym->st_value + rp->r_addend;
9397 break;
9398 default:
9399 break;
9400 }
9401 }
9402
9403 free (rela);
9404 }
9405
9406 return true;
9407 }
9408
9409 static bool
9410 hppa_process_unwind (Filedata * filedata)
9411 {
9412 struct hppa_unw_aux_info aux;
9413 Elf_Internal_Shdr * unwsec = NULL;
9414 Elf_Internal_Shdr * sec;
9415 unsigned long i;
9416 bool res = true;
9417
9418 if (filedata->string_table == NULL)
9419 return false;
9420
9421 memset (& aux, 0, sizeof (aux));
9422
9423 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9424 {
9425 if (sec->sh_type == SHT_SYMTAB)
9426 {
9427 if (aux.symtab)
9428 {
9429 error (_("Multiple symbol tables encountered\n"));
9430 free (aux.symtab);
9431 aux.symtab = NULL;
9432 free (aux.strtab);
9433 aux.strtab = NULL;
9434 }
9435 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
9436 &aux.strtab, &aux.strtab_size))
9437 return false;
9438 }
9439 else if (section_name_valid (filedata, sec)
9440 && streq (section_name (filedata, sec), ".PARISC.unwind"))
9441 unwsec = sec;
9442 }
9443
9444 if (!unwsec)
9445 printf (_("\nThere are no unwind sections in this file.\n"));
9446
9447 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9448 {
9449 if (section_name_valid (filedata, sec)
9450 && streq (section_name (filedata, sec), ".PARISC.unwind"))
9451 {
9452 unsigned long num_unwind = sec->sh_size / 16;
9453
9454 printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
9455 "contains %lu entry:\n",
9456 "\nUnwind section '%s' at offset 0x%lx "
9457 "contains %lu entries:\n",
9458 num_unwind),
9459 printable_section_name (filedata, sec),
9460 (unsigned long) sec->sh_offset,
9461 num_unwind);
9462
9463 if (! slurp_hppa_unwind_table (filedata, &aux, sec))
9464 res = false;
9465
9466 if (res && aux.table_len > 0)
9467 {
9468 if (! dump_hppa_unwind (filedata, &aux))
9469 res = false;
9470 }
9471
9472 free ((char *) aux.table);
9473 aux.table = NULL;
9474 }
9475 }
9476
9477 free (aux.symtab);
9478 free ((char *) aux.strtab);
9479
9480 return res;
9481 }
9482
9483 struct arm_section
9484 {
9485 unsigned char * data; /* The unwind data. */
9486 Elf_Internal_Shdr * sec; /* The cached unwind section header. */
9487 Elf_Internal_Rela * rela; /* The cached relocations for this section. */
9488 unsigned long nrelas; /* The number of relocations. */
9489 unsigned int rel_type; /* REL or RELA ? */
9490 Elf_Internal_Rela * next_rela; /* Cyclic pointer to the next reloc to process. */
9491 };
9492
9493 struct arm_unw_aux_info
9494 {
9495 Filedata * filedata; /* The file containing the unwind sections. */
9496 Elf_Internal_Sym * symtab; /* The file's symbol table. */
9497 unsigned long nsyms; /* Number of symbols. */
9498 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
9499 unsigned long nfuns; /* Number of these symbols. */
9500 char * strtab; /* The file's string table. */
9501 unsigned long strtab_size; /* Size of string table. */
9502 };
9503
9504 static const char *
9505 arm_print_vma_and_name (Filedata * filedata,
9506 struct arm_unw_aux_info * aux,
9507 uint64_t fn,
9508 struct absaddr addr)
9509 {
9510 const char *procname;
9511 uint64_t sym_offset;
9512
9513 if (addr.section == SHN_UNDEF)
9514 addr.offset = fn;
9515
9516 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
9517 aux->strtab_size, addr, &procname,
9518 &sym_offset);
9519
9520 print_vma (fn, PREFIX_HEX);
9521
9522 if (procname)
9523 {
9524 fputs (" <", stdout);
9525 fputs (procname, stdout);
9526
9527 if (sym_offset)
9528 printf ("+0x%lx", (unsigned long) sym_offset);
9529 fputc ('>', stdout);
9530 }
9531
9532 return procname;
9533 }
9534
9535 static void
9536 arm_free_section (struct arm_section *arm_sec)
9537 {
9538 free (arm_sec->data);
9539 free (arm_sec->rela);
9540 }
9541
9542 /* 1) If SEC does not match the one cached in ARM_SEC, then free the current
9543 cached section and install SEC instead.
9544 2) Locate the 32-bit word at WORD_OFFSET in unwind section SEC
9545 and return its valued in * WORDP, relocating if necessary.
9546 3) Update the NEXT_RELA field in ARM_SEC and store the section index and
9547 relocation's offset in ADDR.
9548 4) If SYM_NAME is non-NULL and a relocation was applied, record the offset
9549 into the string table of the symbol associated with the reloc. If no
9550 reloc was applied store -1 there.
9551 5) Return TRUE upon success, FALSE otherwise. */
9552
9553 static bool
9554 get_unwind_section_word (Filedata * filedata,
9555 struct arm_unw_aux_info * aux,
9556 struct arm_section * arm_sec,
9557 Elf_Internal_Shdr * sec,
9558 uint64_t word_offset,
9559 unsigned int * wordp,
9560 struct absaddr * addr,
9561 uint64_t * sym_name)
9562 {
9563 Elf_Internal_Rela *rp;
9564 Elf_Internal_Sym *sym;
9565 const char * relname;
9566 unsigned int word;
9567 bool wrapped;
9568
9569 if (sec == NULL || arm_sec == NULL)
9570 return false;
9571
9572 addr->section = SHN_UNDEF;
9573 addr->offset = 0;
9574
9575 if (sym_name != NULL)
9576 *sym_name = (uint64_t) -1;
9577
9578 /* If necessary, update the section cache. */
9579 if (sec != arm_sec->sec)
9580 {
9581 Elf_Internal_Shdr *relsec;
9582
9583 arm_free_section (arm_sec);
9584
9585 arm_sec->sec = sec;
9586 arm_sec->data = get_data (NULL, aux->filedata, sec->sh_offset, 1,
9587 sec->sh_size, _("unwind data"));
9588 arm_sec->rela = NULL;
9589 arm_sec->nrelas = 0;
9590
9591 for (relsec = filedata->section_headers;
9592 relsec < filedata->section_headers + filedata->file_header.e_shnum;
9593 ++relsec)
9594 {
9595 if (relsec->sh_info >= filedata->file_header.e_shnum
9596 || filedata->section_headers + relsec->sh_info != sec
9597 /* PR 15745: Check the section type as well. */
9598 || (relsec->sh_type != SHT_REL
9599 && relsec->sh_type != SHT_RELA))
9600 continue;
9601
9602 arm_sec->rel_type = relsec->sh_type;
9603 if (relsec->sh_type == SHT_REL)
9604 {
9605 if (!slurp_rel_relocs (aux->filedata, relsec->sh_offset,
9606 relsec->sh_size,
9607 & arm_sec->rela, & arm_sec->nrelas))
9608 return false;
9609 }
9610 else /* relsec->sh_type == SHT_RELA */
9611 {
9612 if (!slurp_rela_relocs (aux->filedata, relsec->sh_offset,
9613 relsec->sh_size,
9614 & arm_sec->rela, & arm_sec->nrelas))
9615 return false;
9616 }
9617 break;
9618 }
9619
9620 arm_sec->next_rela = arm_sec->rela;
9621 }
9622
9623 /* If there is no unwind data we can do nothing. */
9624 if (arm_sec->data == NULL)
9625 return false;
9626
9627 /* If the offset is invalid then fail. */
9628 if (/* PR 21343 *//* PR 18879 */
9629 sec->sh_size < 4
9630 || word_offset > sec->sh_size - 4)
9631 return false;
9632
9633 /* Get the word at the required offset. */
9634 word = byte_get (arm_sec->data + word_offset, 4);
9635
9636 /* PR 17531: file: id:000001,src:001266+003044,op:splice,rep:128. */
9637 if (arm_sec->rela == NULL)
9638 {
9639 * wordp = word;
9640 return true;
9641 }
9642
9643 /* Look through the relocs to find the one that applies to the provided offset. */
9644 wrapped = false;
9645 for (rp = arm_sec->next_rela; rp != arm_sec->rela + arm_sec->nrelas; rp++)
9646 {
9647 uint64_t prelval, offset;
9648
9649 if (rp->r_offset > word_offset && !wrapped)
9650 {
9651 rp = arm_sec->rela;
9652 wrapped = true;
9653 }
9654 if (rp->r_offset > word_offset)
9655 break;
9656
9657 if (rp->r_offset & 3)
9658 {
9659 warn (_("Skipping unexpected relocation at offset 0x%lx\n"),
9660 (unsigned long) rp->r_offset);
9661 continue;
9662 }
9663
9664 if (rp->r_offset < word_offset)
9665 continue;
9666
9667 /* PR 17531: file: 027-161405-0.004 */
9668 if (aux->symtab == NULL)
9669 continue;
9670
9671 if (arm_sec->rel_type == SHT_REL)
9672 {
9673 offset = word & 0x7fffffff;
9674 if (offset & 0x40000000)
9675 offset |= ~ (uint64_t) 0x7fffffff;
9676 }
9677 else if (arm_sec->rel_type == SHT_RELA)
9678 offset = rp->r_addend;
9679 else
9680 {
9681 error (_("Unknown section relocation type %d encountered\n"),
9682 arm_sec->rel_type);
9683 break;
9684 }
9685
9686 /* PR 17531 file: 027-1241568-0.004. */
9687 if (ELF32_R_SYM (rp->r_info) >= aux->nsyms)
9688 {
9689 error (_("Bad symbol index in unwind relocation (%lu > %lu)\n"),
9690 (unsigned long) ELF32_R_SYM (rp->r_info), aux->nsyms);
9691 break;
9692 }
9693
9694 sym = aux->symtab + ELF32_R_SYM (rp->r_info);
9695 offset += sym->st_value;
9696 prelval = offset - (arm_sec->sec->sh_addr + rp->r_offset);
9697
9698 /* Check that we are processing the expected reloc type. */
9699 if (filedata->file_header.e_machine == EM_ARM)
9700 {
9701 relname = elf_arm_reloc_type (ELF32_R_TYPE (rp->r_info));
9702 if (relname == NULL)
9703 {
9704 warn (_("Skipping unknown ARM relocation type: %d\n"),
9705 (int) ELF32_R_TYPE (rp->r_info));
9706 continue;
9707 }
9708
9709 if (streq (relname, "R_ARM_NONE"))
9710 continue;
9711
9712 if (! streq (relname, "R_ARM_PREL31"))
9713 {
9714 warn (_("Skipping unexpected ARM relocation type %s\n"), relname);
9715 continue;
9716 }
9717 }
9718 else if (filedata->file_header.e_machine == EM_TI_C6000)
9719 {
9720 relname = elf_tic6x_reloc_type (ELF32_R_TYPE (rp->r_info));
9721 if (relname == NULL)
9722 {
9723 warn (_("Skipping unknown C6000 relocation type: %d\n"),
9724 (int) ELF32_R_TYPE (rp->r_info));
9725 continue;
9726 }
9727
9728 if (streq (relname, "R_C6000_NONE"))
9729 continue;
9730
9731 if (! streq (relname, "R_C6000_PREL31"))
9732 {
9733 warn (_("Skipping unexpected C6000 relocation type %s\n"), relname);
9734 continue;
9735 }
9736
9737 prelval >>= 1;
9738 }
9739 else
9740 {
9741 /* This function currently only supports ARM and TI unwinders. */
9742 warn (_("Only TI and ARM unwinders are currently supported\n"));
9743 break;
9744 }
9745
9746 word = (word & ~ (uint64_t) 0x7fffffff) | (prelval & 0x7fffffff);
9747 addr->section = sym->st_shndx;
9748 addr->offset = offset;
9749
9750 if (sym_name)
9751 * sym_name = sym->st_name;
9752 break;
9753 }
9754
9755 *wordp = word;
9756 arm_sec->next_rela = rp;
9757
9758 return true;
9759 }
9760
9761 static const char *tic6x_unwind_regnames[16] =
9762 {
9763 "A15", "B15", "B14", "B13", "B12", "B11", "B10", "B3",
9764 "A14", "A13", "A12", "A11", "A10",
9765 "[invalid reg 13]", "[invalid reg 14]", "[invalid reg 15]"
9766 };
9767
9768 static void
9769 decode_tic6x_unwind_regmask (unsigned int mask)
9770 {
9771 int i;
9772
9773 for (i = 12; mask; mask >>= 1, i--)
9774 {
9775 if (mask & 1)
9776 {
9777 fputs (tic6x_unwind_regnames[i], stdout);
9778 if (mask > 1)
9779 fputs (", ", stdout);
9780 }
9781 }
9782 }
9783
9784 #define ADVANCE \
9785 if (remaining == 0 && more_words) \
9786 { \
9787 data_offset += 4; \
9788 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, \
9789 data_offset, & word, & addr, NULL)) \
9790 return false; \
9791 remaining = 4; \
9792 more_words--; \
9793 } \
9794
9795 #define GET_OP(OP) \
9796 ADVANCE; \
9797 if (remaining) \
9798 { \
9799 remaining--; \
9800 (OP) = word >> 24; \
9801 word <<= 8; \
9802 } \
9803 else \
9804 { \
9805 printf (_("[Truncated opcode]\n")); \
9806 return false; \
9807 } \
9808 printf ("0x%02x ", OP)
9809
9810 static bool
9811 decode_arm_unwind_bytecode (Filedata * filedata,
9812 struct arm_unw_aux_info * aux,
9813 unsigned int word,
9814 unsigned int remaining,
9815 unsigned int more_words,
9816 uint64_t data_offset,
9817 Elf_Internal_Shdr * data_sec,
9818 struct arm_section * data_arm_sec)
9819 {
9820 struct absaddr addr;
9821 bool res = true;
9822
9823 /* Decode the unwinding instructions. */
9824 while (1)
9825 {
9826 unsigned int op, op2;
9827
9828 ADVANCE;
9829 if (remaining == 0)
9830 break;
9831 remaining--;
9832 op = word >> 24;
9833 word <<= 8;
9834
9835 printf (" 0x%02x ", op);
9836
9837 if ((op & 0xc0) == 0x00)
9838 {
9839 int offset = ((op & 0x3f) << 2) + 4;
9840
9841 printf (" vsp = vsp + %d", offset);
9842 }
9843 else if ((op & 0xc0) == 0x40)
9844 {
9845 int offset = ((op & 0x3f) << 2) + 4;
9846
9847 printf (" vsp = vsp - %d", offset);
9848 }
9849 else if ((op & 0xf0) == 0x80)
9850 {
9851 GET_OP (op2);
9852 if (op == 0x80 && op2 == 0)
9853 printf (_("Refuse to unwind"));
9854 else
9855 {
9856 unsigned int mask = ((op & 0x0f) << 8) | op2;
9857 bool first = true;
9858 int i;
9859
9860 printf ("pop {");
9861 for (i = 0; i < 12; i++)
9862 if (mask & (1 << i))
9863 {
9864 if (first)
9865 first = false;
9866 else
9867 printf (", ");
9868 printf ("r%d", 4 + i);
9869 }
9870 printf ("}");
9871 }
9872 }
9873 else if ((op & 0xf0) == 0x90)
9874 {
9875 if (op == 0x9d || op == 0x9f)
9876 printf (_(" [Reserved]"));
9877 else
9878 printf (" vsp = r%d", op & 0x0f);
9879 }
9880 else if ((op & 0xf0) == 0xa0)
9881 {
9882 int end = 4 + (op & 0x07);
9883 bool first = true;
9884 int i;
9885
9886 printf (" pop {");
9887 for (i = 4; i <= end; i++)
9888 {
9889 if (first)
9890 first = false;
9891 else
9892 printf (", ");
9893 printf ("r%d", i);
9894 }
9895 if (op & 0x08)
9896 {
9897 if (!first)
9898 printf (", ");
9899 printf ("r14");
9900 }
9901 printf ("}");
9902 }
9903 else if (op == 0xb0)
9904 printf (_(" finish"));
9905 else if (op == 0xb1)
9906 {
9907 GET_OP (op2);
9908 if (op2 == 0 || (op2 & 0xf0) != 0)
9909 printf (_("[Spare]"));
9910 else
9911 {
9912 unsigned int mask = op2 & 0x0f;
9913 bool first = true;
9914 int i;
9915
9916 printf ("pop {");
9917 for (i = 0; i < 12; i++)
9918 if (mask & (1 << i))
9919 {
9920 if (first)
9921 first = false;
9922 else
9923 printf (", ");
9924 printf ("r%d", i);
9925 }
9926 printf ("}");
9927 }
9928 }
9929 else if (op == 0xb2)
9930 {
9931 unsigned char buf[9];
9932 unsigned int i, len;
9933 unsigned long offset;
9934
9935 for (i = 0; i < sizeof (buf); i++)
9936 {
9937 GET_OP (buf[i]);
9938 if ((buf[i] & 0x80) == 0)
9939 break;
9940 }
9941 if (i == sizeof (buf))
9942 {
9943 error (_("corrupt change to vsp\n"));
9944 res = false;
9945 }
9946 else
9947 {
9948 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
9949 assert (len == i + 1);
9950 offset = offset * 4 + 0x204;
9951 printf ("vsp = vsp + %ld", offset);
9952 }
9953 }
9954 else if (op == 0xb3 || op == 0xc8 || op == 0xc9)
9955 {
9956 unsigned int first, last;
9957
9958 GET_OP (op2);
9959 first = op2 >> 4;
9960 last = op2 & 0x0f;
9961 if (op == 0xc8)
9962 first = first + 16;
9963 printf ("pop {D%d", first);
9964 if (last)
9965 printf ("-D%d", first + last);
9966 printf ("}");
9967 }
9968 else if (op == 0xb4)
9969 printf (_(" pop {ra_auth_code}"));
9970 else if (op == 0xb5)
9971 printf (_(" vsp as modifier for PAC validation"));
9972 else if ((op & 0xf8) == 0xb8 || (op & 0xf8) == 0xd0)
9973 {
9974 unsigned int count = op & 0x07;
9975
9976 printf ("pop {D8");
9977 if (count)
9978 printf ("-D%d", 8 + count);
9979 printf ("}");
9980 }
9981 else if (op >= 0xc0 && op <= 0xc5)
9982 {
9983 unsigned int count = op & 0x07;
9984
9985 printf (" pop {wR10");
9986 if (count)
9987 printf ("-wR%d", 10 + count);
9988 printf ("}");
9989 }
9990 else if (op == 0xc6)
9991 {
9992 unsigned int first, last;
9993
9994 GET_OP (op2);
9995 first = op2 >> 4;
9996 last = op2 & 0x0f;
9997 printf ("pop {wR%d", first);
9998 if (last)
9999 printf ("-wR%d", first + last);
10000 printf ("}");
10001 }
10002 else if (op == 0xc7)
10003 {
10004 GET_OP (op2);
10005 if (op2 == 0 || (op2 & 0xf0) != 0)
10006 printf (_("[Spare]"));
10007 else
10008 {
10009 unsigned int mask = op2 & 0x0f;
10010 bool first = true;
10011 int i;
10012
10013 printf ("pop {");
10014 for (i = 0; i < 4; i++)
10015 if (mask & (1 << i))
10016 {
10017 if (first)
10018 first = false;
10019 else
10020 printf (", ");
10021 printf ("wCGR%d", i);
10022 }
10023 printf ("}");
10024 }
10025 }
10026 else
10027 {
10028 printf (_(" [unsupported opcode]"));
10029 res = false;
10030 }
10031
10032 printf ("\n");
10033 }
10034
10035 return res;
10036 }
10037
10038 static bool
10039 decode_tic6x_unwind_bytecode (Filedata * filedata,
10040 struct arm_unw_aux_info * aux,
10041 unsigned int word,
10042 unsigned int remaining,
10043 unsigned int more_words,
10044 uint64_t data_offset,
10045 Elf_Internal_Shdr * data_sec,
10046 struct arm_section * data_arm_sec)
10047 {
10048 struct absaddr addr;
10049
10050 /* Decode the unwinding instructions. */
10051 while (1)
10052 {
10053 unsigned int op, op2;
10054
10055 ADVANCE;
10056 if (remaining == 0)
10057 break;
10058 remaining--;
10059 op = word >> 24;
10060 word <<= 8;
10061
10062 printf (" 0x%02x ", op);
10063
10064 if ((op & 0xc0) == 0x00)
10065 {
10066 int offset = ((op & 0x3f) << 3) + 8;
10067 printf (" sp = sp + %d", offset);
10068 }
10069 else if ((op & 0xc0) == 0x80)
10070 {
10071 GET_OP (op2);
10072 if (op == 0x80 && op2 == 0)
10073 printf (_("Refuse to unwind"));
10074 else
10075 {
10076 unsigned int mask = ((op & 0x1f) << 8) | op2;
10077 if (op & 0x20)
10078 printf ("pop compact {");
10079 else
10080 printf ("pop {");
10081
10082 decode_tic6x_unwind_regmask (mask);
10083 printf("}");
10084 }
10085 }
10086 else if ((op & 0xf0) == 0xc0)
10087 {
10088 unsigned int reg;
10089 unsigned int nregs;
10090 unsigned int i;
10091 const char *name;
10092 struct
10093 {
10094 unsigned int offset;
10095 unsigned int reg;
10096 } regpos[16];
10097
10098 /* Scan entire instruction first so that GET_OP output is not
10099 interleaved with disassembly. */
10100 nregs = 0;
10101 for (i = 0; nregs < (op & 0xf); i++)
10102 {
10103 GET_OP (op2);
10104 reg = op2 >> 4;
10105 if (reg != 0xf)
10106 {
10107 regpos[nregs].offset = i * 2;
10108 regpos[nregs].reg = reg;
10109 nregs++;
10110 }
10111
10112 reg = op2 & 0xf;
10113 if (reg != 0xf)
10114 {
10115 regpos[nregs].offset = i * 2 + 1;
10116 regpos[nregs].reg = reg;
10117 nregs++;
10118 }
10119 }
10120
10121 printf (_("pop frame {"));
10122 if (nregs == 0)
10123 {
10124 printf (_("*corrupt* - no registers specified"));
10125 }
10126 else
10127 {
10128 reg = nregs - 1;
10129 for (i = i * 2; i > 0; i--)
10130 {
10131 if (regpos[reg].offset == i - 1)
10132 {
10133 name = tic6x_unwind_regnames[regpos[reg].reg];
10134 if (reg > 0)
10135 reg--;
10136 }
10137 else
10138 name = _("[pad]");
10139
10140 fputs (name, stdout);
10141 if (i > 1)
10142 printf (", ");
10143 }
10144 }
10145
10146 printf ("}");
10147 }
10148 else if (op == 0xd0)
10149 printf (" MOV FP, SP");
10150 else if (op == 0xd1)
10151 printf (" __c6xabi_pop_rts");
10152 else if (op == 0xd2)
10153 {
10154 unsigned char buf[9];
10155 unsigned int i, len;
10156 unsigned long offset;
10157
10158 for (i = 0; i < sizeof (buf); i++)
10159 {
10160 GET_OP (buf[i]);
10161 if ((buf[i] & 0x80) == 0)
10162 break;
10163 }
10164 /* PR 17531: file: id:000001,src:001906+004739,op:splice,rep:2. */
10165 if (i == sizeof (buf))
10166 {
10167 warn (_("Corrupt stack pointer adjustment detected\n"));
10168 return false;
10169 }
10170
10171 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
10172 assert (len == i + 1);
10173 offset = offset * 8 + 0x408;
10174 printf (_("sp = sp + %ld"), offset);
10175 }
10176 else if ((op & 0xf0) == 0xe0)
10177 {
10178 if ((op & 0x0f) == 7)
10179 printf (" RETURN");
10180 else
10181 printf (" MV %s, B3", tic6x_unwind_regnames[op & 0x0f]);
10182 }
10183 else
10184 {
10185 printf (_(" [unsupported opcode]"));
10186 }
10187 putchar ('\n');
10188 }
10189
10190 return true;
10191 }
10192
10193 static uint64_t
10194 arm_expand_prel31 (Filedata * filedata, uint64_t word, uint64_t where)
10195 {
10196 uint64_t offset;
10197
10198 offset = word & 0x7fffffff;
10199 if (offset & 0x40000000)
10200 offset |= ~ (uint64_t) 0x7fffffff;
10201
10202 if (filedata->file_header.e_machine == EM_TI_C6000)
10203 offset <<= 1;
10204
10205 return offset + where;
10206 }
10207
10208 static bool
10209 decode_arm_unwind (Filedata * filedata,
10210 struct arm_unw_aux_info * aux,
10211 unsigned int word,
10212 unsigned int remaining,
10213 uint64_t data_offset,
10214 Elf_Internal_Shdr * data_sec,
10215 struct arm_section * data_arm_sec)
10216 {
10217 int per_index;
10218 unsigned int more_words = 0;
10219 struct absaddr addr;
10220 uint64_t sym_name = (uint64_t) -1;
10221 bool res = true;
10222
10223 if (remaining == 0)
10224 {
10225 /* Fetch the first word.
10226 Note - when decoding an object file the address extracted
10227 here will always be 0. So we also pass in the sym_name
10228 parameter so that we can find the symbol associated with
10229 the personality routine. */
10230 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, data_offset,
10231 & word, & addr, & sym_name))
10232 return false;
10233
10234 remaining = 4;
10235 }
10236 else
10237 {
10238 addr.section = SHN_UNDEF;
10239 addr.offset = 0;
10240 }
10241
10242 if ((word & 0x80000000) == 0)
10243 {
10244 /* Expand prel31 for personality routine. */
10245 uint64_t fn;
10246 const char *procname;
10247
10248 fn = arm_expand_prel31 (filedata, word, data_sec->sh_addr + data_offset);
10249 printf (_(" Personality routine: "));
10250 if (fn == 0
10251 && addr.section == SHN_UNDEF && addr.offset == 0
10252 && sym_name != (uint64_t) -1 && sym_name < aux->strtab_size)
10253 {
10254 procname = aux->strtab + sym_name;
10255 print_vma (fn, PREFIX_HEX);
10256 if (procname)
10257 {
10258 fputs (" <", stdout);
10259 fputs (procname, stdout);
10260 fputc ('>', stdout);
10261 }
10262 }
10263 else
10264 procname = arm_print_vma_and_name (filedata, aux, fn, addr);
10265 fputc ('\n', stdout);
10266
10267 /* The GCC personality routines use the standard compact
10268 encoding, starting with one byte giving the number of
10269 words. */
10270 if (procname != NULL
10271 && (startswith (procname, "__gcc_personality_v0")
10272 || startswith (procname, "__gxx_personality_v0")
10273 || startswith (procname, "__gcj_personality_v0")
10274 || startswith (procname, "__gnu_objc_personality_v0")))
10275 {
10276 remaining = 0;
10277 more_words = 1;
10278 ADVANCE;
10279 if (!remaining)
10280 {
10281 printf (_(" [Truncated data]\n"));
10282 return false;
10283 }
10284 more_words = word >> 24;
10285 word <<= 8;
10286 remaining--;
10287 per_index = -1;
10288 }
10289 else
10290 return true;
10291 }
10292 else
10293 {
10294 /* ARM EHABI Section 6.3:
10295
10296 An exception-handling table entry for the compact model looks like:
10297
10298 31 30-28 27-24 23-0
10299 -- ----- ----- ----
10300 1 0 index Data for personalityRoutine[index] */
10301
10302 if (filedata->file_header.e_machine == EM_ARM
10303 && (word & 0x70000000))
10304 {
10305 warn (_("Corrupt ARM compact model table entry: %x \n"), word);
10306 res = false;
10307 }
10308
10309 per_index = (word >> 24) & 0x7f;
10310 printf (_(" Compact model index: %d\n"), per_index);
10311 if (per_index == 0)
10312 {
10313 more_words = 0;
10314 word <<= 8;
10315 remaining--;
10316 }
10317 else if (per_index < 3)
10318 {
10319 more_words = (word >> 16) & 0xff;
10320 word <<= 16;
10321 remaining -= 2;
10322 }
10323 }
10324
10325 switch (filedata->file_header.e_machine)
10326 {
10327 case EM_ARM:
10328 if (per_index < 3)
10329 {
10330 if (! decode_arm_unwind_bytecode (filedata, aux, word, remaining, more_words,
10331 data_offset, data_sec, data_arm_sec))
10332 res = false;
10333 }
10334 else
10335 {
10336 warn (_("Unknown ARM compact model index encountered\n"));
10337 printf (_(" [reserved]\n"));
10338 res = false;
10339 }
10340 break;
10341
10342 case EM_TI_C6000:
10343 if (per_index < 3)
10344 {
10345 if (! decode_tic6x_unwind_bytecode (filedata, aux, word, remaining, more_words,
10346 data_offset, data_sec, data_arm_sec))
10347 res = false;
10348 }
10349 else if (per_index < 5)
10350 {
10351 if (((word >> 17) & 0x7f) == 0x7f)
10352 printf (_(" Restore stack from frame pointer\n"));
10353 else
10354 printf (_(" Stack increment %d\n"), (word >> 14) & 0x1fc);
10355 printf (_(" Registers restored: "));
10356 if (per_index == 4)
10357 printf (" (compact) ");
10358 decode_tic6x_unwind_regmask ((word >> 4) & 0x1fff);
10359 putchar ('\n');
10360 printf (_(" Return register: %s\n"),
10361 tic6x_unwind_regnames[word & 0xf]);
10362 }
10363 else
10364 printf (_(" [reserved (%d)]\n"), per_index);
10365 break;
10366
10367 default:
10368 error (_("Unsupported architecture type %d encountered when decoding unwind table\n"),
10369 filedata->file_header.e_machine);
10370 res = false;
10371 }
10372
10373 /* Decode the descriptors. Not implemented. */
10374
10375 return res;
10376 }
10377
10378 static bool
10379 dump_arm_unwind (Filedata * filedata,
10380 struct arm_unw_aux_info * aux,
10381 Elf_Internal_Shdr * exidx_sec)
10382 {
10383 struct arm_section exidx_arm_sec, extab_arm_sec;
10384 unsigned int i, exidx_len;
10385 unsigned long j, nfuns;
10386 bool res = true;
10387
10388 memset (&exidx_arm_sec, 0, sizeof (exidx_arm_sec));
10389 memset (&extab_arm_sec, 0, sizeof (extab_arm_sec));
10390 exidx_len = exidx_sec->sh_size / 8;
10391
10392 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
10393 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
10394 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
10395 aux->funtab[nfuns++] = aux->symtab[j];
10396 aux->nfuns = nfuns;
10397 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
10398
10399 for (i = 0; i < exidx_len; i++)
10400 {
10401 unsigned int exidx_fn, exidx_entry;
10402 struct absaddr fn_addr, entry_addr;
10403 uint64_t fn;
10404
10405 fputc ('\n', stdout);
10406
10407 if (! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
10408 8 * i, & exidx_fn, & fn_addr, NULL)
10409 || ! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
10410 8 * i + 4, & exidx_entry, & entry_addr, NULL))
10411 {
10412 free (aux->funtab);
10413 arm_free_section (& exidx_arm_sec);
10414 arm_free_section (& extab_arm_sec);
10415 return false;
10416 }
10417
10418 /* ARM EHABI, Section 5:
10419 An index table entry consists of 2 words.
10420 The first word contains a prel31 offset to the start of a function, with bit 31 clear. */
10421 if (exidx_fn & 0x80000000)
10422 {
10423 warn (_("corrupt index table entry: %x\n"), exidx_fn);
10424 res = false;
10425 }
10426
10427 fn = arm_expand_prel31 (filedata, exidx_fn, exidx_sec->sh_addr + 8 * i);
10428
10429 arm_print_vma_and_name (filedata, aux, fn, fn_addr);
10430 fputs (": ", stdout);
10431
10432 if (exidx_entry == 1)
10433 {
10434 print_vma (exidx_entry, PREFIX_HEX);
10435 fputs (" [cantunwind]\n", stdout);
10436 }
10437 else if (exidx_entry & 0x80000000)
10438 {
10439 print_vma (exidx_entry, PREFIX_HEX);
10440 fputc ('\n', stdout);
10441 decode_arm_unwind (filedata, aux, exidx_entry, 4, 0, NULL, NULL);
10442 }
10443 else
10444 {
10445 uint64_t table, table_offset = 0;
10446 Elf_Internal_Shdr *table_sec;
10447
10448 fputs ("@", stdout);
10449 table = arm_expand_prel31 (filedata, exidx_entry, exidx_sec->sh_addr + 8 * i + 4);
10450 print_vma (table, PREFIX_HEX);
10451 printf ("\n");
10452
10453 /* Locate the matching .ARM.extab. */
10454 if (entry_addr.section != SHN_UNDEF
10455 && entry_addr.section < filedata->file_header.e_shnum)
10456 {
10457 table_sec = filedata->section_headers + entry_addr.section;
10458 table_offset = entry_addr.offset;
10459 /* PR 18879 */
10460 if (table_offset > table_sec->sh_size)
10461 {
10462 warn (_("Unwind entry contains corrupt offset (0x%lx) into section %s\n"),
10463 (unsigned long) table_offset,
10464 printable_section_name (filedata, table_sec));
10465 res = false;
10466 continue;
10467 }
10468 }
10469 else
10470 {
10471 table_sec = find_section_by_address (filedata, table);
10472 if (table_sec != NULL)
10473 table_offset = table - table_sec->sh_addr;
10474 }
10475
10476 if (table_sec == NULL)
10477 {
10478 warn (_("Could not locate .ARM.extab section containing 0x%lx.\n"),
10479 (unsigned long) table);
10480 res = false;
10481 continue;
10482 }
10483
10484 if (! decode_arm_unwind (filedata, aux, 0, 0, table_offset, table_sec,
10485 &extab_arm_sec))
10486 res = false;
10487 }
10488 }
10489
10490 printf ("\n");
10491
10492 free (aux->funtab);
10493 arm_free_section (&exidx_arm_sec);
10494 arm_free_section (&extab_arm_sec);
10495
10496 return res;
10497 }
10498
10499 /* Used for both ARM and C6X unwinding tables. */
10500
10501 static bool
10502 arm_process_unwind (Filedata * filedata)
10503 {
10504 struct arm_unw_aux_info aux;
10505 Elf_Internal_Shdr *unwsec = NULL;
10506 Elf_Internal_Shdr *sec;
10507 unsigned long i;
10508 unsigned int sec_type;
10509 bool res = true;
10510
10511 switch (filedata->file_header.e_machine)
10512 {
10513 case EM_ARM:
10514 sec_type = SHT_ARM_EXIDX;
10515 break;
10516
10517 case EM_TI_C6000:
10518 sec_type = SHT_C6000_UNWIND;
10519 break;
10520
10521 default:
10522 error (_("Unsupported architecture type %d encountered when processing unwind table\n"),
10523 filedata->file_header.e_machine);
10524 return false;
10525 }
10526
10527 if (filedata->string_table == NULL)
10528 return false;
10529
10530 memset (& aux, 0, sizeof (aux));
10531 aux.filedata = filedata;
10532
10533 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
10534 {
10535 if (sec->sh_type == SHT_SYMTAB)
10536 {
10537 if (aux.symtab)
10538 {
10539 error (_("Multiple symbol tables encountered\n"));
10540 free (aux.symtab);
10541 aux.symtab = NULL;
10542 free (aux.strtab);
10543 aux.strtab = NULL;
10544 }
10545 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
10546 &aux.strtab, &aux.strtab_size))
10547 return false;
10548 }
10549 else if (sec->sh_type == sec_type)
10550 unwsec = sec;
10551 }
10552
10553 if (unwsec == NULL)
10554 printf (_("\nThere are no unwind sections in this file.\n"));
10555 else
10556 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
10557 {
10558 if (sec->sh_type == sec_type)
10559 {
10560 unsigned long num_unwind = sec->sh_size / (2 * eh_addr_size);
10561 printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
10562 "contains %lu entry:\n",
10563 "\nUnwind section '%s' at offset 0x%lx "
10564 "contains %lu entries:\n",
10565 num_unwind),
10566 printable_section_name (filedata, sec),
10567 (unsigned long) sec->sh_offset,
10568 num_unwind);
10569
10570 if (! dump_arm_unwind (filedata, &aux, sec))
10571 res = false;
10572 }
10573 }
10574
10575 free (aux.symtab);
10576 free ((char *) aux.strtab);
10577
10578 return res;
10579 }
10580
10581 static bool
10582 no_processor_specific_unwind (Filedata * filedata ATTRIBUTE_UNUSED)
10583 {
10584 printf (_("No processor specific unwind information to decode\n"));
10585 return true;
10586 }
10587
10588 static bool
10589 process_unwind (Filedata * filedata)
10590 {
10591 struct unwind_handler
10592 {
10593 unsigned int machtype;
10594 bool (* handler)(Filedata *);
10595 } handlers[] =
10596 {
10597 { EM_ARM, arm_process_unwind },
10598 { EM_IA_64, ia64_process_unwind },
10599 { EM_PARISC, hppa_process_unwind },
10600 { EM_TI_C6000, arm_process_unwind },
10601 { EM_386, no_processor_specific_unwind },
10602 { EM_X86_64, no_processor_specific_unwind },
10603 { 0, NULL }
10604 };
10605 int i;
10606
10607 if (!do_unwind)
10608 return true;
10609
10610 for (i = 0; handlers[i].handler != NULL; i++)
10611 if (filedata->file_header.e_machine == handlers[i].machtype)
10612 return handlers[i].handler (filedata);
10613
10614 printf (_("\nThe decoding of unwind sections for machine type %s is not currently supported.\n"),
10615 get_machine_name (filedata->file_header.e_machine));
10616 return true;
10617 }
10618
10619 static void
10620 dynamic_section_aarch64_val (Elf_Internal_Dyn * entry)
10621 {
10622 switch (entry->d_tag)
10623 {
10624 case DT_AARCH64_BTI_PLT:
10625 case DT_AARCH64_PAC_PLT:
10626 break;
10627 default:
10628 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10629 break;
10630 }
10631 putchar ('\n');
10632 }
10633
10634 static void
10635 dynamic_section_mips_val (Filedata * filedata, Elf_Internal_Dyn * entry)
10636 {
10637 switch (entry->d_tag)
10638 {
10639 case DT_MIPS_FLAGS:
10640 if (entry->d_un.d_val == 0)
10641 printf (_("NONE"));
10642 else
10643 {
10644 static const char * opts[] =
10645 {
10646 "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
10647 "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
10648 "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
10649 "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
10650 "RLD_ORDER_SAFE"
10651 };
10652 unsigned int cnt;
10653 bool first = true;
10654
10655 for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
10656 if (entry->d_un.d_val & (1 << cnt))
10657 {
10658 printf ("%s%s", first ? "" : " ", opts[cnt]);
10659 first = false;
10660 }
10661 }
10662 break;
10663
10664 case DT_MIPS_IVERSION:
10665 if (valid_dynamic_name (filedata, entry->d_un.d_val))
10666 printf (_("Interface Version: %s"),
10667 get_dynamic_name (filedata, entry->d_un.d_val));
10668 else
10669 printf (_("Interface Version: <corrupt: %" PRIx64 ">"),
10670 entry->d_un.d_ptr);
10671 break;
10672
10673 case DT_MIPS_TIME_STAMP:
10674 {
10675 char timebuf[128];
10676 struct tm * tmp;
10677 time_t atime = entry->d_un.d_val;
10678
10679 tmp = gmtime (&atime);
10680 /* PR 17531: file: 6accc532. */
10681 if (tmp == NULL)
10682 snprintf (timebuf, sizeof (timebuf), _("<corrupt>"));
10683 else
10684 snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
10685 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
10686 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
10687 printf (_("Time Stamp: %s"), timebuf);
10688 }
10689 break;
10690
10691 case DT_MIPS_RLD_VERSION:
10692 case DT_MIPS_LOCAL_GOTNO:
10693 case DT_MIPS_CONFLICTNO:
10694 case DT_MIPS_LIBLISTNO:
10695 case DT_MIPS_SYMTABNO:
10696 case DT_MIPS_UNREFEXTNO:
10697 case DT_MIPS_HIPAGENO:
10698 case DT_MIPS_DELTA_CLASS_NO:
10699 case DT_MIPS_DELTA_INSTANCE_NO:
10700 case DT_MIPS_DELTA_RELOC_NO:
10701 case DT_MIPS_DELTA_SYM_NO:
10702 case DT_MIPS_DELTA_CLASSSYM_NO:
10703 case DT_MIPS_COMPACT_SIZE:
10704 print_vma (entry->d_un.d_val, DEC);
10705 break;
10706
10707 case DT_MIPS_XHASH:
10708 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
10709 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10710 /* Falls through. */
10711
10712 default:
10713 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10714 }
10715 putchar ('\n');
10716 }
10717
10718 static void
10719 dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
10720 {
10721 switch (entry->d_tag)
10722 {
10723 case DT_HP_DLD_FLAGS:
10724 {
10725 static struct
10726 {
10727 long int bit;
10728 const char * str;
10729 }
10730 flags[] =
10731 {
10732 { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
10733 { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
10734 { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
10735 { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
10736 { DT_HP_BIND_NOW, "HP_BIND_NOW" },
10737 { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
10738 { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
10739 { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
10740 { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
10741 { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
10742 { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
10743 { DT_HP_GST, "HP_GST" },
10744 { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
10745 { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
10746 { DT_HP_NODELETE, "HP_NODELETE" },
10747 { DT_HP_GROUP, "HP_GROUP" },
10748 { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
10749 };
10750 bool first = true;
10751 size_t cnt;
10752 uint64_t val = entry->d_un.d_val;
10753
10754 for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
10755 if (val & flags[cnt].bit)
10756 {
10757 if (! first)
10758 putchar (' ');
10759 fputs (flags[cnt].str, stdout);
10760 first = false;
10761 val ^= flags[cnt].bit;
10762 }
10763
10764 if (val != 0 || first)
10765 {
10766 if (! first)
10767 putchar (' ');
10768 print_vma (val, HEX);
10769 }
10770 }
10771 break;
10772
10773 default:
10774 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10775 break;
10776 }
10777 putchar ('\n');
10778 }
10779
10780 /* VMS vs Unix time offset and factor. */
10781
10782 #define VMS_EPOCH_OFFSET 35067168000000000LL
10783 #define VMS_GRANULARITY_FACTOR 10000000
10784 #ifndef INT64_MIN
10785 #define INT64_MIN (-9223372036854775807LL - 1)
10786 #endif
10787
10788 /* Display a VMS time in a human readable format. */
10789
10790 static void
10791 print_vms_time (int64_t vmstime)
10792 {
10793 struct tm *tm = NULL;
10794 time_t unxtime;
10795
10796 if (vmstime >= INT64_MIN + VMS_EPOCH_OFFSET)
10797 {
10798 vmstime = (vmstime - VMS_EPOCH_OFFSET) / VMS_GRANULARITY_FACTOR;
10799 unxtime = vmstime;
10800 if (unxtime == vmstime)
10801 tm = gmtime (&unxtime);
10802 }
10803 if (tm != NULL)
10804 printf ("%04u-%02u-%02uT%02u:%02u:%02u",
10805 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
10806 tm->tm_hour, tm->tm_min, tm->tm_sec);
10807 }
10808
10809 static void
10810 dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
10811 {
10812 switch (entry->d_tag)
10813 {
10814 case DT_IA_64_PLT_RESERVE:
10815 /* First 3 slots reserved. */
10816 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10817 printf (" -- ");
10818 print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
10819 break;
10820
10821 case DT_IA_64_VMS_LINKTIME:
10822 print_vms_time (entry->d_un.d_val);
10823 break;
10824
10825 case DT_IA_64_VMS_LNKFLAGS:
10826 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10827 if (entry->d_un.d_val & VMS_LF_CALL_DEBUG)
10828 printf (" CALL_DEBUG");
10829 if (entry->d_un.d_val & VMS_LF_NOP0BUFS)
10830 printf (" NOP0BUFS");
10831 if (entry->d_un.d_val & VMS_LF_P0IMAGE)
10832 printf (" P0IMAGE");
10833 if (entry->d_un.d_val & VMS_LF_MKTHREADS)
10834 printf (" MKTHREADS");
10835 if (entry->d_un.d_val & VMS_LF_UPCALLS)
10836 printf (" UPCALLS");
10837 if (entry->d_un.d_val & VMS_LF_IMGSTA)
10838 printf (" IMGSTA");
10839 if (entry->d_un.d_val & VMS_LF_INITIALIZE)
10840 printf (" INITIALIZE");
10841 if (entry->d_un.d_val & VMS_LF_MAIN)
10842 printf (" MAIN");
10843 if (entry->d_un.d_val & VMS_LF_EXE_INIT)
10844 printf (" EXE_INIT");
10845 if (entry->d_un.d_val & VMS_LF_TBK_IN_IMG)
10846 printf (" TBK_IN_IMG");
10847 if (entry->d_un.d_val & VMS_LF_DBG_IN_IMG)
10848 printf (" DBG_IN_IMG");
10849 if (entry->d_un.d_val & VMS_LF_TBK_IN_DSF)
10850 printf (" TBK_IN_DSF");
10851 if (entry->d_un.d_val & VMS_LF_DBG_IN_DSF)
10852 printf (" DBG_IN_DSF");
10853 if (entry->d_un.d_val & VMS_LF_SIGNATURES)
10854 printf (" SIGNATURES");
10855 if (entry->d_un.d_val & VMS_LF_REL_SEG_OFF)
10856 printf (" REL_SEG_OFF");
10857 break;
10858
10859 default:
10860 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10861 break;
10862 }
10863 putchar ('\n');
10864 }
10865
10866 static bool
10867 get_32bit_dynamic_section (Filedata * filedata)
10868 {
10869 Elf32_External_Dyn * edyn;
10870 Elf32_External_Dyn * ext;
10871 Elf_Internal_Dyn * entry;
10872
10873 edyn = (Elf32_External_Dyn *) get_data (NULL, filedata,
10874 filedata->dynamic_addr, 1,
10875 filedata->dynamic_size,
10876 _("dynamic section"));
10877 if (!edyn)
10878 return false;
10879
10880 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
10881 might not have the luxury of section headers. Look for the DT_NULL
10882 terminator to determine the number of entries. */
10883 for (ext = edyn, filedata->dynamic_nent = 0;
10884 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
10885 ext++)
10886 {
10887 filedata->dynamic_nent++;
10888 if (BYTE_GET (ext->d_tag) == DT_NULL)
10889 break;
10890 }
10891
10892 filedata->dynamic_section
10893 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
10894 if (filedata->dynamic_section == NULL)
10895 {
10896 error (_("Out of memory allocating space for %lu dynamic entries\n"),
10897 (unsigned long) filedata->dynamic_nent);
10898 free (edyn);
10899 return false;
10900 }
10901
10902 for (ext = edyn, entry = filedata->dynamic_section;
10903 entry < filedata->dynamic_section + filedata->dynamic_nent;
10904 ext++, entry++)
10905 {
10906 entry->d_tag = BYTE_GET (ext->d_tag);
10907 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
10908 }
10909
10910 free (edyn);
10911
10912 return true;
10913 }
10914
10915 static bool
10916 get_64bit_dynamic_section (Filedata * filedata)
10917 {
10918 Elf64_External_Dyn * edyn;
10919 Elf64_External_Dyn * ext;
10920 Elf_Internal_Dyn * entry;
10921
10922 /* Read in the data. */
10923 edyn = (Elf64_External_Dyn *) get_data (NULL, filedata,
10924 filedata->dynamic_addr, 1,
10925 filedata->dynamic_size,
10926 _("dynamic section"));
10927 if (!edyn)
10928 return false;
10929
10930 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
10931 might not have the luxury of section headers. Look for the DT_NULL
10932 terminator to determine the number of entries. */
10933 for (ext = edyn, filedata->dynamic_nent = 0;
10934 /* PR 17533 file: 033-67080-0.004 - do not read past end of buffer. */
10935 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
10936 ext++)
10937 {
10938 filedata->dynamic_nent++;
10939 if (BYTE_GET (ext->d_tag) == DT_NULL)
10940 break;
10941 }
10942
10943 filedata->dynamic_section
10944 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
10945 if (filedata->dynamic_section == NULL)
10946 {
10947 error (_("Out of memory allocating space for %lu dynamic entries\n"),
10948 (unsigned long) filedata->dynamic_nent);
10949 free (edyn);
10950 return false;
10951 }
10952
10953 /* Convert from external to internal formats. */
10954 for (ext = edyn, entry = filedata->dynamic_section;
10955 entry < filedata->dynamic_section + filedata->dynamic_nent;
10956 ext++, entry++)
10957 {
10958 entry->d_tag = BYTE_GET (ext->d_tag);
10959 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
10960 }
10961
10962 free (edyn);
10963
10964 return true;
10965 }
10966
10967 static bool
10968 get_dynamic_section (Filedata *filedata)
10969 {
10970 if (filedata->dynamic_section)
10971 return true;
10972
10973 if (is_32bit_elf)
10974 return get_32bit_dynamic_section (filedata);
10975 else
10976 return get_64bit_dynamic_section (filedata);
10977 }
10978
10979 static void
10980 print_dynamic_flags (uint64_t flags)
10981 {
10982 bool first = true;
10983
10984 while (flags)
10985 {
10986 uint64_t flag;
10987
10988 flag = flags & - flags;
10989 flags &= ~ flag;
10990
10991 if (first)
10992 first = false;
10993 else
10994 putc (' ', stdout);
10995
10996 switch (flag)
10997 {
10998 case DF_ORIGIN: fputs ("ORIGIN", stdout); break;
10999 case DF_SYMBOLIC: fputs ("SYMBOLIC", stdout); break;
11000 case DF_TEXTREL: fputs ("TEXTREL", stdout); break;
11001 case DF_BIND_NOW: fputs ("BIND_NOW", stdout); break;
11002 case DF_STATIC_TLS: fputs ("STATIC_TLS", stdout); break;
11003 default: fputs (_("unknown"), stdout); break;
11004 }
11005 }
11006 puts ("");
11007 }
11008
11009 static uint64_t *
11010 get_dynamic_data (Filedata * filedata, uint64_t number, unsigned int ent_size)
11011 {
11012 unsigned char * e_data;
11013 uint64_t * i_data;
11014
11015 /* If size_t is smaller than uint64_t, eg because you are building
11016 on a 32-bit host, then make sure that when number is cast to
11017 size_t no information is lost. */
11018 if ((size_t) number != number
11019 || ent_size * number / ent_size != number)
11020 {
11021 error (_("Size overflow prevents reading %" PRIu64
11022 " elements of size %u\n"),
11023 number, ent_size);
11024 return NULL;
11025 }
11026
11027 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
11028 attempting to allocate memory when the read is bound to fail. */
11029 if (ent_size * number > filedata->file_size)
11030 {
11031 error (_("Invalid number of dynamic entries: %" PRIu64 "\n"),
11032 number);
11033 return NULL;
11034 }
11035
11036 e_data = (unsigned char *) cmalloc ((size_t) number, ent_size);
11037 if (e_data == NULL)
11038 {
11039 error (_("Out of memory reading %" PRIu64 " dynamic entries\n"),
11040 number);
11041 return NULL;
11042 }
11043
11044 if (fread (e_data, ent_size, (size_t) number, filedata->handle) != number)
11045 {
11046 error (_("Unable to read in %" PRIu64 " bytes of dynamic data\n"),
11047 number * ent_size);
11048 free (e_data);
11049 return NULL;
11050 }
11051
11052 i_data = (uint64_t *) cmalloc ((size_t) number, sizeof (*i_data));
11053 if (i_data == NULL)
11054 {
11055 error (_("Out of memory allocating space for %" PRIu64 " dynamic entries\n"),
11056 number);
11057 free (e_data);
11058 return NULL;
11059 }
11060
11061 while (number--)
11062 i_data[number] = byte_get (e_data + number * ent_size, ent_size);
11063
11064 free (e_data);
11065
11066 return i_data;
11067 }
11068
11069 static unsigned long
11070 get_num_dynamic_syms (Filedata * filedata)
11071 {
11072 unsigned long num_of_syms = 0;
11073
11074 if (!do_histogram && (!do_using_dynamic || do_dyn_syms))
11075 return num_of_syms;
11076
11077 if (filedata->dynamic_info[DT_HASH])
11078 {
11079 unsigned char nb[8];
11080 unsigned char nc[8];
11081 unsigned int hash_ent_size = 4;
11082
11083 if ((filedata->file_header.e_machine == EM_ALPHA
11084 || filedata->file_header.e_machine == EM_S390
11085 || filedata->file_header.e_machine == EM_S390_OLD)
11086 && filedata->file_header.e_ident[EI_CLASS] == ELFCLASS64)
11087 hash_ent_size = 8;
11088
11089 if (fseek64 (filedata->handle,
11090 (filedata->archive_file_offset
11091 + offset_from_vma (filedata,
11092 filedata->dynamic_info[DT_HASH],
11093 sizeof nb + sizeof nc)),
11094 SEEK_SET))
11095 {
11096 error (_("Unable to seek to start of dynamic information\n"));
11097 goto no_hash;
11098 }
11099
11100 if (fread (nb, hash_ent_size, 1, filedata->handle) != 1)
11101 {
11102 error (_("Failed to read in number of buckets\n"));
11103 goto no_hash;
11104 }
11105
11106 if (fread (nc, hash_ent_size, 1, filedata->handle) != 1)
11107 {
11108 error (_("Failed to read in number of chains\n"));
11109 goto no_hash;
11110 }
11111
11112 filedata->nbuckets = byte_get (nb, hash_ent_size);
11113 filedata->nchains = byte_get (nc, hash_ent_size);
11114
11115 if (filedata->nbuckets != 0 && filedata->nchains != 0)
11116 {
11117 filedata->buckets = get_dynamic_data (filedata, filedata->nbuckets,
11118 hash_ent_size);
11119 filedata->chains = get_dynamic_data (filedata, filedata->nchains,
11120 hash_ent_size);
11121
11122 if (filedata->buckets != NULL && filedata->chains != NULL)
11123 num_of_syms = filedata->nchains;
11124 }
11125 no_hash:
11126 if (num_of_syms == 0)
11127 {
11128 free (filedata->buckets);
11129 filedata->buckets = NULL;
11130 free (filedata->chains);
11131 filedata->chains = NULL;
11132 filedata->nbuckets = 0;
11133 }
11134 }
11135
11136 if (filedata->dynamic_info_DT_GNU_HASH)
11137 {
11138 unsigned char nb[16];
11139 uint64_t i, maxchain = 0xffffffff, bitmaskwords;
11140 uint64_t buckets_vma;
11141 unsigned long hn;
11142
11143 if (fseek64 (filedata->handle,
11144 (filedata->archive_file_offset
11145 + offset_from_vma (filedata,
11146 filedata->dynamic_info_DT_GNU_HASH,
11147 sizeof nb)),
11148 SEEK_SET))
11149 {
11150 error (_("Unable to seek to start of dynamic information\n"));
11151 goto no_gnu_hash;
11152 }
11153
11154 if (fread (nb, 16, 1, filedata->handle) != 1)
11155 {
11156 error (_("Failed to read in number of buckets\n"));
11157 goto no_gnu_hash;
11158 }
11159
11160 filedata->ngnubuckets = byte_get (nb, 4);
11161 filedata->gnusymidx = byte_get (nb + 4, 4);
11162 bitmaskwords = byte_get (nb + 8, 4);
11163 buckets_vma = filedata->dynamic_info_DT_GNU_HASH + 16;
11164 if (is_32bit_elf)
11165 buckets_vma += bitmaskwords * 4;
11166 else
11167 buckets_vma += bitmaskwords * 8;
11168
11169 if (fseek64 (filedata->handle,
11170 (filedata->archive_file_offset
11171 + offset_from_vma (filedata, buckets_vma, 4)),
11172 SEEK_SET))
11173 {
11174 error (_("Unable to seek to start of dynamic information\n"));
11175 goto no_gnu_hash;
11176 }
11177
11178 filedata->gnubuckets
11179 = get_dynamic_data (filedata, filedata->ngnubuckets, 4);
11180
11181 if (filedata->gnubuckets == NULL)
11182 goto no_gnu_hash;
11183
11184 for (i = 0; i < filedata->ngnubuckets; i++)
11185 if (filedata->gnubuckets[i] != 0)
11186 {
11187 if (filedata->gnubuckets[i] < filedata->gnusymidx)
11188 goto no_gnu_hash;
11189
11190 if (maxchain == 0xffffffff || filedata->gnubuckets[i] > maxchain)
11191 maxchain = filedata->gnubuckets[i];
11192 }
11193
11194 if (maxchain == 0xffffffff)
11195 goto no_gnu_hash;
11196
11197 maxchain -= filedata->gnusymidx;
11198
11199 if (fseek64 (filedata->handle,
11200 (filedata->archive_file_offset
11201 + offset_from_vma (filedata,
11202 buckets_vma + 4 * (filedata->ngnubuckets
11203 + maxchain),
11204 4)),
11205 SEEK_SET))
11206 {
11207 error (_("Unable to seek to start of dynamic information\n"));
11208 goto no_gnu_hash;
11209 }
11210
11211 do
11212 {
11213 if (fread (nb, 4, 1, filedata->handle) != 1)
11214 {
11215 error (_("Failed to determine last chain length\n"));
11216 goto no_gnu_hash;
11217 }
11218
11219 if (maxchain + 1 == 0)
11220 goto no_gnu_hash;
11221
11222 ++maxchain;
11223 }
11224 while ((byte_get (nb, 4) & 1) == 0);
11225
11226 if (fseek64 (filedata->handle,
11227 (filedata->archive_file_offset
11228 + offset_from_vma (filedata, (buckets_vma
11229 + 4 * filedata->ngnubuckets),
11230 4)),
11231 SEEK_SET))
11232 {
11233 error (_("Unable to seek to start of dynamic information\n"));
11234 goto no_gnu_hash;
11235 }
11236
11237 filedata->gnuchains = get_dynamic_data (filedata, maxchain, 4);
11238 filedata->ngnuchains = maxchain;
11239
11240 if (filedata->gnuchains == NULL)
11241 goto no_gnu_hash;
11242
11243 if (filedata->dynamic_info_DT_MIPS_XHASH)
11244 {
11245 if (fseek64 (filedata->handle,
11246 (filedata->archive_file_offset
11247 + offset_from_vma (filedata, (buckets_vma
11248 + 4 * (filedata->ngnubuckets
11249 + maxchain)), 4)),
11250 SEEK_SET))
11251 {
11252 error (_("Unable to seek to start of dynamic information\n"));
11253 goto no_gnu_hash;
11254 }
11255
11256 filedata->mipsxlat = get_dynamic_data (filedata, maxchain, 4);
11257 if (filedata->mipsxlat == NULL)
11258 goto no_gnu_hash;
11259 }
11260
11261 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
11262 if (filedata->gnubuckets[hn] != 0)
11263 {
11264 uint64_t si = filedata->gnubuckets[hn];
11265 uint64_t off = si - filedata->gnusymidx;
11266
11267 do
11268 {
11269 if (filedata->dynamic_info_DT_MIPS_XHASH)
11270 {
11271 if (off < filedata->ngnuchains
11272 && filedata->mipsxlat[off] >= num_of_syms)
11273 num_of_syms = filedata->mipsxlat[off] + 1;
11274 }
11275 else
11276 {
11277 if (si >= num_of_syms)
11278 num_of_syms = si + 1;
11279 }
11280 si++;
11281 }
11282 while (off < filedata->ngnuchains
11283 && (filedata->gnuchains[off++] & 1) == 0);
11284 }
11285
11286 if (num_of_syms == 0)
11287 {
11288 no_gnu_hash:
11289 free (filedata->mipsxlat);
11290 filedata->mipsxlat = NULL;
11291 free (filedata->gnuchains);
11292 filedata->gnuchains = NULL;
11293 free (filedata->gnubuckets);
11294 filedata->gnubuckets = NULL;
11295 filedata->ngnubuckets = 0;
11296 filedata->ngnuchains = 0;
11297 }
11298 }
11299
11300 return num_of_syms;
11301 }
11302
11303 /* Parse and display the contents of the dynamic section. */
11304
11305 static bool
11306 process_dynamic_section (Filedata * filedata)
11307 {
11308 Elf_Internal_Dyn * entry;
11309
11310 if (filedata->dynamic_size <= 1)
11311 {
11312 if (do_dynamic)
11313 {
11314 if (filedata->is_separate)
11315 printf (_("\nThere is no dynamic section in linked file '%s'.\n"),
11316 filedata->file_name);
11317 else
11318 printf (_("\nThere is no dynamic section in this file.\n"));
11319 }
11320
11321 return true;
11322 }
11323
11324 if (!get_dynamic_section (filedata))
11325 return false;
11326
11327 /* Find the appropriate symbol table. */
11328 if (filedata->dynamic_symbols == NULL || do_histogram)
11329 {
11330 unsigned long num_of_syms;
11331
11332 for (entry = filedata->dynamic_section;
11333 entry < filedata->dynamic_section + filedata->dynamic_nent;
11334 ++entry)
11335 if (entry->d_tag == DT_SYMTAB)
11336 filedata->dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
11337 else if (entry->d_tag == DT_SYMENT)
11338 filedata->dynamic_info[DT_SYMENT] = entry->d_un.d_val;
11339 else if (entry->d_tag == DT_HASH)
11340 filedata->dynamic_info[DT_HASH] = entry->d_un.d_val;
11341 else if (entry->d_tag == DT_GNU_HASH)
11342 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11343 else if ((filedata->file_header.e_machine == EM_MIPS
11344 || filedata->file_header.e_machine == EM_MIPS_RS3_LE)
11345 && entry->d_tag == DT_MIPS_XHASH)
11346 {
11347 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
11348 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11349 }
11350
11351 num_of_syms = get_num_dynamic_syms (filedata);
11352
11353 if (num_of_syms != 0
11354 && filedata->dynamic_symbols == NULL
11355 && filedata->dynamic_info[DT_SYMTAB]
11356 && filedata->dynamic_info[DT_SYMENT])
11357 {
11358 Elf_Internal_Phdr *seg;
11359 uint64_t vma = filedata->dynamic_info[DT_SYMTAB];
11360
11361 if (! get_program_headers (filedata))
11362 {
11363 error (_("Cannot interpret virtual addresses "
11364 "without program headers.\n"));
11365 return false;
11366 }
11367
11368 for (seg = filedata->program_headers;
11369 seg < filedata->program_headers + filedata->file_header.e_phnum;
11370 ++seg)
11371 {
11372 if (seg->p_type != PT_LOAD)
11373 continue;
11374
11375 if (seg->p_offset + seg->p_filesz > filedata->file_size)
11376 {
11377 /* See PR 21379 for a reproducer. */
11378 error (_("Invalid PT_LOAD entry\n"));
11379 return false;
11380 }
11381
11382 if (vma >= (seg->p_vaddr & -seg->p_align)
11383 && vma < seg->p_vaddr + seg->p_filesz)
11384 {
11385 /* Since we do not know how big the symbol table is,
11386 we default to reading in up to the end of PT_LOAD
11387 segment and processing that. This is overkill, I
11388 know, but it should work. */
11389 Elf_Internal_Shdr section;
11390 section.sh_offset = (vma - seg->p_vaddr
11391 + seg->p_offset);
11392 section.sh_size = (num_of_syms
11393 * filedata->dynamic_info[DT_SYMENT]);
11394 section.sh_entsize = filedata->dynamic_info[DT_SYMENT];
11395
11396 if (do_checks
11397 && filedata->dynamic_symtab_section != NULL
11398 && ((filedata->dynamic_symtab_section->sh_offset
11399 != section.sh_offset)
11400 || (filedata->dynamic_symtab_section->sh_size
11401 != section.sh_size)
11402 || (filedata->dynamic_symtab_section->sh_entsize
11403 != section.sh_entsize)))
11404 warn (_("\
11405 the .dynsym section doesn't match the DT_SYMTAB and DT_SYMENT tags\n"));
11406
11407 section.sh_name = filedata->string_table_length;
11408 filedata->dynamic_symbols
11409 = get_elf_symbols (filedata, &section,
11410 &filedata->num_dynamic_syms);
11411 if (filedata->dynamic_symbols == NULL
11412 || filedata->num_dynamic_syms != num_of_syms)
11413 {
11414 error (_("Corrupt DT_SYMTAB dynamic entry\n"));
11415 return false;
11416 }
11417 break;
11418 }
11419 }
11420 }
11421 }
11422
11423 /* Similarly find a string table. */
11424 if (filedata->dynamic_strings == NULL)
11425 for (entry = filedata->dynamic_section;
11426 entry < filedata->dynamic_section + filedata->dynamic_nent;
11427 ++entry)
11428 {
11429 if (entry->d_tag == DT_STRTAB)
11430 filedata->dynamic_info[DT_STRTAB] = entry->d_un.d_val;
11431
11432 if (entry->d_tag == DT_STRSZ)
11433 filedata->dynamic_info[DT_STRSZ] = entry->d_un.d_val;
11434
11435 if (filedata->dynamic_info[DT_STRTAB]
11436 && filedata->dynamic_info[DT_STRSZ])
11437 {
11438 unsigned long offset;
11439 uint64_t str_tab_len = filedata->dynamic_info[DT_STRSZ];
11440
11441 offset = offset_from_vma (filedata,
11442 filedata->dynamic_info[DT_STRTAB],
11443 str_tab_len);
11444 if (do_checks
11445 && filedata->dynamic_strtab_section
11446 && ((filedata->dynamic_strtab_section->sh_offset
11447 != (file_ptr) offset)
11448 || (filedata->dynamic_strtab_section->sh_size
11449 != str_tab_len)))
11450 warn (_("\
11451 the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
11452
11453 filedata->dynamic_strings
11454 = (char *) get_data (NULL, filedata, offset, 1, str_tab_len,
11455 _("dynamic string table"));
11456 if (filedata->dynamic_strings == NULL)
11457 {
11458 error (_("Corrupt DT_STRTAB dynamic entry\n"));
11459 break;
11460 }
11461
11462 filedata->dynamic_strings_length = str_tab_len;
11463 break;
11464 }
11465 }
11466
11467 /* And find the syminfo section if available. */
11468 if (filedata->dynamic_syminfo == NULL)
11469 {
11470 unsigned long syminsz = 0;
11471
11472 for (entry = filedata->dynamic_section;
11473 entry < filedata->dynamic_section + filedata->dynamic_nent;
11474 ++entry)
11475 {
11476 if (entry->d_tag == DT_SYMINENT)
11477 {
11478 /* Note: these braces are necessary to avoid a syntax
11479 error from the SunOS4 C compiler. */
11480 /* PR binutils/17531: A corrupt file can trigger this test.
11481 So do not use an assert, instead generate an error message. */
11482 if (sizeof (Elf_External_Syminfo) != entry->d_un.d_val)
11483 error (_("Bad value (%d) for SYMINENT entry\n"),
11484 (int) entry->d_un.d_val);
11485 }
11486 else if (entry->d_tag == DT_SYMINSZ)
11487 syminsz = entry->d_un.d_val;
11488 else if (entry->d_tag == DT_SYMINFO)
11489 filedata->dynamic_syminfo_offset
11490 = offset_from_vma (filedata, entry->d_un.d_val, syminsz);
11491 }
11492
11493 if (filedata->dynamic_syminfo_offset != 0 && syminsz != 0)
11494 {
11495 Elf_External_Syminfo * extsyminfo;
11496 Elf_External_Syminfo * extsym;
11497 Elf_Internal_Syminfo * syminfo;
11498
11499 /* There is a syminfo section. Read the data. */
11500 extsyminfo = (Elf_External_Syminfo *)
11501 get_data (NULL, filedata, filedata->dynamic_syminfo_offset,
11502 1, syminsz, _("symbol information"));
11503 if (!extsyminfo)
11504 return false;
11505
11506 if (filedata->dynamic_syminfo != NULL)
11507 {
11508 error (_("Multiple dynamic symbol information sections found\n"));
11509 free (filedata->dynamic_syminfo);
11510 }
11511 filedata->dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
11512 if (filedata->dynamic_syminfo == NULL)
11513 {
11514 error (_("Out of memory allocating %lu bytes "
11515 "for dynamic symbol info\n"),
11516 (unsigned long) syminsz);
11517 return false;
11518 }
11519
11520 filedata->dynamic_syminfo_nent
11521 = syminsz / sizeof (Elf_External_Syminfo);
11522 for (syminfo = filedata->dynamic_syminfo, extsym = extsyminfo;
11523 syminfo < (filedata->dynamic_syminfo
11524 + filedata->dynamic_syminfo_nent);
11525 ++syminfo, ++extsym)
11526 {
11527 syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
11528 syminfo->si_flags = BYTE_GET (extsym->si_flags);
11529 }
11530
11531 free (extsyminfo);
11532 }
11533 }
11534
11535 if (do_dynamic && filedata->dynamic_addr)
11536 {
11537 if (filedata->is_separate)
11538 printf (ngettext ("\nIn linked file '%s' the dynamic section at offset 0x%lx contains %lu entry:\n",
11539 "\nIn linked file '%s' the dynamic section at offset 0x%lx contains %lu entries:\n",
11540 (unsigned long) filedata->dynamic_nent),
11541 filedata->file_name,
11542 filedata->dynamic_addr,
11543 (unsigned long) filedata->dynamic_nent);
11544 else
11545 printf (ngettext ("\nDynamic section at offset 0x%lx contains %lu entry:\n",
11546 "\nDynamic section at offset 0x%lx contains %lu entries:\n",
11547 (unsigned long) filedata->dynamic_nent),
11548 filedata->dynamic_addr,
11549 (unsigned long) filedata->dynamic_nent);
11550 }
11551 if (do_dynamic)
11552 printf (_(" Tag Type Name/Value\n"));
11553
11554 for (entry = filedata->dynamic_section;
11555 entry < filedata->dynamic_section + filedata->dynamic_nent;
11556 entry++)
11557 {
11558 if (do_dynamic)
11559 {
11560 const char * dtype;
11561
11562 putchar (' ');
11563 print_vma (entry->d_tag, FULL_HEX);
11564 dtype = get_dynamic_type (filedata, entry->d_tag);
11565 printf (" (%s)%*s", dtype,
11566 ((is_32bit_elf ? 27 : 19) - (int) strlen (dtype)), " ");
11567 }
11568
11569 switch (entry->d_tag)
11570 {
11571 case DT_FLAGS:
11572 if (do_dynamic)
11573 print_dynamic_flags (entry->d_un.d_val);
11574 break;
11575
11576 case DT_AUXILIARY:
11577 case DT_FILTER:
11578 case DT_CONFIG:
11579 case DT_DEPAUDIT:
11580 case DT_AUDIT:
11581 if (do_dynamic)
11582 {
11583 switch (entry->d_tag)
11584 {
11585 case DT_AUXILIARY:
11586 printf (_("Auxiliary library"));
11587 break;
11588
11589 case DT_FILTER:
11590 printf (_("Filter library"));
11591 break;
11592
11593 case DT_CONFIG:
11594 printf (_("Configuration file"));
11595 break;
11596
11597 case DT_DEPAUDIT:
11598 printf (_("Dependency audit library"));
11599 break;
11600
11601 case DT_AUDIT:
11602 printf (_("Audit library"));
11603 break;
11604 }
11605
11606 if (valid_dynamic_name (filedata, entry->d_un.d_val))
11607 printf (": [%s]\n",
11608 get_dynamic_name (filedata, entry->d_un.d_val));
11609 else
11610 {
11611 printf (": ");
11612 print_vma (entry->d_un.d_val, PREFIX_HEX);
11613 putchar ('\n');
11614 }
11615 }
11616 break;
11617
11618 case DT_FEATURE:
11619 if (do_dynamic)
11620 {
11621 printf (_("Flags:"));
11622
11623 if (entry->d_un.d_val == 0)
11624 printf (_(" None\n"));
11625 else
11626 {
11627 unsigned long int val = entry->d_un.d_val;
11628
11629 if (val & DTF_1_PARINIT)
11630 {
11631 printf (" PARINIT");
11632 val ^= DTF_1_PARINIT;
11633 }
11634 if (val & DTF_1_CONFEXP)
11635 {
11636 printf (" CONFEXP");
11637 val ^= DTF_1_CONFEXP;
11638 }
11639 if (val != 0)
11640 printf (" %lx", val);
11641 puts ("");
11642 }
11643 }
11644 break;
11645
11646 case DT_POSFLAG_1:
11647 if (do_dynamic)
11648 {
11649 printf (_("Flags:"));
11650
11651 if (entry->d_un.d_val == 0)
11652 printf (_(" None\n"));
11653 else
11654 {
11655 unsigned long int val = entry->d_un.d_val;
11656
11657 if (val & DF_P1_LAZYLOAD)
11658 {
11659 printf (" LAZYLOAD");
11660 val ^= DF_P1_LAZYLOAD;
11661 }
11662 if (val & DF_P1_GROUPPERM)
11663 {
11664 printf (" GROUPPERM");
11665 val ^= DF_P1_GROUPPERM;
11666 }
11667 if (val != 0)
11668 printf (" %lx", val);
11669 puts ("");
11670 }
11671 }
11672 break;
11673
11674 case DT_FLAGS_1:
11675 if (do_dynamic)
11676 {
11677 printf (_("Flags:"));
11678 if (entry->d_un.d_val == 0)
11679 printf (_(" None\n"));
11680 else
11681 {
11682 unsigned long int val = entry->d_un.d_val;
11683
11684 if (val & DF_1_NOW)
11685 {
11686 printf (" NOW");
11687 val ^= DF_1_NOW;
11688 }
11689 if (val & DF_1_GLOBAL)
11690 {
11691 printf (" GLOBAL");
11692 val ^= DF_1_GLOBAL;
11693 }
11694 if (val & DF_1_GROUP)
11695 {
11696 printf (" GROUP");
11697 val ^= DF_1_GROUP;
11698 }
11699 if (val & DF_1_NODELETE)
11700 {
11701 printf (" NODELETE");
11702 val ^= DF_1_NODELETE;
11703 }
11704 if (val & DF_1_LOADFLTR)
11705 {
11706 printf (" LOADFLTR");
11707 val ^= DF_1_LOADFLTR;
11708 }
11709 if (val & DF_1_INITFIRST)
11710 {
11711 printf (" INITFIRST");
11712 val ^= DF_1_INITFIRST;
11713 }
11714 if (val & DF_1_NOOPEN)
11715 {
11716 printf (" NOOPEN");
11717 val ^= DF_1_NOOPEN;
11718 }
11719 if (val & DF_1_ORIGIN)
11720 {
11721 printf (" ORIGIN");
11722 val ^= DF_1_ORIGIN;
11723 }
11724 if (val & DF_1_DIRECT)
11725 {
11726 printf (" DIRECT");
11727 val ^= DF_1_DIRECT;
11728 }
11729 if (val & DF_1_TRANS)
11730 {
11731 printf (" TRANS");
11732 val ^= DF_1_TRANS;
11733 }
11734 if (val & DF_1_INTERPOSE)
11735 {
11736 printf (" INTERPOSE");
11737 val ^= DF_1_INTERPOSE;
11738 }
11739 if (val & DF_1_NODEFLIB)
11740 {
11741 printf (" NODEFLIB");
11742 val ^= DF_1_NODEFLIB;
11743 }
11744 if (val & DF_1_NODUMP)
11745 {
11746 printf (" NODUMP");
11747 val ^= DF_1_NODUMP;
11748 }
11749 if (val & DF_1_CONFALT)
11750 {
11751 printf (" CONFALT");
11752 val ^= DF_1_CONFALT;
11753 }
11754 if (val & DF_1_ENDFILTEE)
11755 {
11756 printf (" ENDFILTEE");
11757 val ^= DF_1_ENDFILTEE;
11758 }
11759 if (val & DF_1_DISPRELDNE)
11760 {
11761 printf (" DISPRELDNE");
11762 val ^= DF_1_DISPRELDNE;
11763 }
11764 if (val & DF_1_DISPRELPND)
11765 {
11766 printf (" DISPRELPND");
11767 val ^= DF_1_DISPRELPND;
11768 }
11769 if (val & DF_1_NODIRECT)
11770 {
11771 printf (" NODIRECT");
11772 val ^= DF_1_NODIRECT;
11773 }
11774 if (val & DF_1_IGNMULDEF)
11775 {
11776 printf (" IGNMULDEF");
11777 val ^= DF_1_IGNMULDEF;
11778 }
11779 if (val & DF_1_NOKSYMS)
11780 {
11781 printf (" NOKSYMS");
11782 val ^= DF_1_NOKSYMS;
11783 }
11784 if (val & DF_1_NOHDR)
11785 {
11786 printf (" NOHDR");
11787 val ^= DF_1_NOHDR;
11788 }
11789 if (val & DF_1_EDITED)
11790 {
11791 printf (" EDITED");
11792 val ^= DF_1_EDITED;
11793 }
11794 if (val & DF_1_NORELOC)
11795 {
11796 printf (" NORELOC");
11797 val ^= DF_1_NORELOC;
11798 }
11799 if (val & DF_1_SYMINTPOSE)
11800 {
11801 printf (" SYMINTPOSE");
11802 val ^= DF_1_SYMINTPOSE;
11803 }
11804 if (val & DF_1_GLOBAUDIT)
11805 {
11806 printf (" GLOBAUDIT");
11807 val ^= DF_1_GLOBAUDIT;
11808 }
11809 if (val & DF_1_SINGLETON)
11810 {
11811 printf (" SINGLETON");
11812 val ^= DF_1_SINGLETON;
11813 }
11814 if (val & DF_1_STUB)
11815 {
11816 printf (" STUB");
11817 val ^= DF_1_STUB;
11818 }
11819 if (val & DF_1_PIE)
11820 {
11821 printf (" PIE");
11822 val ^= DF_1_PIE;
11823 }
11824 if (val & DF_1_KMOD)
11825 {
11826 printf (" KMOD");
11827 val ^= DF_1_KMOD;
11828 }
11829 if (val & DF_1_WEAKFILTER)
11830 {
11831 printf (" WEAKFILTER");
11832 val ^= DF_1_WEAKFILTER;
11833 }
11834 if (val & DF_1_NOCOMMON)
11835 {
11836 printf (" NOCOMMON");
11837 val ^= DF_1_NOCOMMON;
11838 }
11839 if (val != 0)
11840 printf (" %lx", val);
11841 puts ("");
11842 }
11843 }
11844 break;
11845
11846 case DT_PLTREL:
11847 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11848 if (do_dynamic)
11849 puts (get_dynamic_type (filedata, entry->d_un.d_val));
11850 break;
11851
11852 case DT_NULL :
11853 case DT_NEEDED :
11854 case DT_PLTGOT :
11855 case DT_HASH :
11856 case DT_STRTAB :
11857 case DT_SYMTAB :
11858 case DT_RELA :
11859 case DT_INIT :
11860 case DT_FINI :
11861 case DT_SONAME :
11862 case DT_RPATH :
11863 case DT_SYMBOLIC:
11864 case DT_REL :
11865 case DT_RELR :
11866 case DT_DEBUG :
11867 case DT_TEXTREL :
11868 case DT_JMPREL :
11869 case DT_RUNPATH :
11870 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11871
11872 if (do_dynamic)
11873 {
11874 const char *name;
11875
11876 if (valid_dynamic_name (filedata, entry->d_un.d_val))
11877 name = get_dynamic_name (filedata, entry->d_un.d_val);
11878 else
11879 name = NULL;
11880
11881 if (name)
11882 {
11883 switch (entry->d_tag)
11884 {
11885 case DT_NEEDED:
11886 printf (_("Shared library: [%s]"), name);
11887
11888 if (filedata->program_interpreter
11889 && streq (name, filedata->program_interpreter))
11890 printf (_(" program interpreter"));
11891 break;
11892
11893 case DT_SONAME:
11894 printf (_("Library soname: [%s]"), name);
11895 break;
11896
11897 case DT_RPATH:
11898 printf (_("Library rpath: [%s]"), name);
11899 break;
11900
11901 case DT_RUNPATH:
11902 printf (_("Library runpath: [%s]"), name);
11903 break;
11904
11905 default:
11906 print_vma (entry->d_un.d_val, PREFIX_HEX);
11907 break;
11908 }
11909 }
11910 else
11911 print_vma (entry->d_un.d_val, PREFIX_HEX);
11912
11913 putchar ('\n');
11914 }
11915 break;
11916
11917 case DT_PLTRELSZ:
11918 case DT_RELASZ :
11919 case DT_STRSZ :
11920 case DT_RELSZ :
11921 case DT_RELAENT :
11922 case DT_RELRENT :
11923 case DT_RELRSZ :
11924 case DT_SYMENT :
11925 case DT_RELENT :
11926 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11927 /* Fall through. */
11928 case DT_PLTPADSZ:
11929 case DT_MOVEENT :
11930 case DT_MOVESZ :
11931 case DT_PREINIT_ARRAYSZ:
11932 case DT_INIT_ARRAYSZ:
11933 case DT_FINI_ARRAYSZ:
11934 case DT_GNU_CONFLICTSZ:
11935 case DT_GNU_LIBLISTSZ:
11936 if (do_dynamic)
11937 {
11938 print_vma (entry->d_un.d_val, UNSIGNED);
11939 printf (_(" (bytes)\n"));
11940 }
11941 break;
11942
11943 case DT_VERDEFNUM:
11944 case DT_VERNEEDNUM:
11945 case DT_RELACOUNT:
11946 case DT_RELCOUNT:
11947 if (do_dynamic)
11948 {
11949 print_vma (entry->d_un.d_val, UNSIGNED);
11950 putchar ('\n');
11951 }
11952 break;
11953
11954 case DT_SYMINSZ:
11955 case DT_SYMINENT:
11956 case DT_SYMINFO:
11957 case DT_USED:
11958 case DT_INIT_ARRAY:
11959 case DT_FINI_ARRAY:
11960 if (do_dynamic)
11961 {
11962 if (entry->d_tag == DT_USED
11963 && valid_dynamic_name (filedata, entry->d_un.d_val))
11964 {
11965 const char *name
11966 = get_dynamic_name (filedata, entry->d_un.d_val);
11967
11968 if (*name)
11969 {
11970 printf (_("Not needed object: [%s]\n"), name);
11971 break;
11972 }
11973 }
11974
11975 print_vma (entry->d_un.d_val, PREFIX_HEX);
11976 putchar ('\n');
11977 }
11978 break;
11979
11980 case DT_BIND_NOW:
11981 /* The value of this entry is ignored. */
11982 if (do_dynamic)
11983 putchar ('\n');
11984 break;
11985
11986 case DT_GNU_PRELINKED:
11987 if (do_dynamic)
11988 {
11989 struct tm * tmp;
11990 time_t atime = entry->d_un.d_val;
11991
11992 tmp = gmtime (&atime);
11993 /* PR 17533 file: 041-1244816-0.004. */
11994 if (tmp == NULL)
11995 printf (_("<corrupt time val: %lx"),
11996 (unsigned long) atime);
11997 else
11998 printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
11999 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
12000 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
12001
12002 }
12003 break;
12004
12005 case DT_GNU_HASH:
12006 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
12007 if (do_dynamic)
12008 {
12009 print_vma (entry->d_un.d_val, PREFIX_HEX);
12010 putchar ('\n');
12011 }
12012 break;
12013
12014 case DT_GNU_FLAGS_1:
12015 if (do_dynamic)
12016 {
12017 printf (_("Flags:"));
12018 if (entry->d_un.d_val == 0)
12019 printf (_(" None\n"));
12020 else
12021 {
12022 unsigned long int val = entry->d_un.d_val;
12023
12024 if (val & DF_GNU_1_UNIQUE)
12025 {
12026 printf (" UNIQUE");
12027 val ^= DF_GNU_1_UNIQUE;
12028 }
12029 if (val != 0)
12030 printf (" %lx", val);
12031 puts ("");
12032 }
12033 }
12034 break;
12035
12036 default:
12037 if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
12038 filedata->version_info[DT_VERSIONTAGIDX (entry->d_tag)]
12039 = entry->d_un.d_val;
12040
12041 if (do_dynamic)
12042 {
12043 switch (filedata->file_header.e_machine)
12044 {
12045 case EM_AARCH64:
12046 dynamic_section_aarch64_val (entry);
12047 break;
12048 case EM_MIPS:
12049 case EM_MIPS_RS3_LE:
12050 dynamic_section_mips_val (filedata, entry);
12051 break;
12052 case EM_PARISC:
12053 dynamic_section_parisc_val (entry);
12054 break;
12055 case EM_IA_64:
12056 dynamic_section_ia64_val (entry);
12057 break;
12058 default:
12059 print_vma (entry->d_un.d_val, PREFIX_HEX);
12060 putchar ('\n');
12061 }
12062 }
12063 break;
12064 }
12065 }
12066
12067 return true;
12068 }
12069
12070 static char *
12071 get_ver_flags (unsigned int flags)
12072 {
12073 static char buff[128];
12074
12075 buff[0] = 0;
12076
12077 if (flags == 0)
12078 return _("none");
12079
12080 if (flags & VER_FLG_BASE)
12081 strcat (buff, "BASE");
12082
12083 if (flags & VER_FLG_WEAK)
12084 {
12085 if (flags & VER_FLG_BASE)
12086 strcat (buff, " | ");
12087
12088 strcat (buff, "WEAK");
12089 }
12090
12091 if (flags & VER_FLG_INFO)
12092 {
12093 if (flags & (VER_FLG_BASE|VER_FLG_WEAK))
12094 strcat (buff, " | ");
12095
12096 strcat (buff, "INFO");
12097 }
12098
12099 if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
12100 {
12101 if (flags & (VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
12102 strcat (buff, " | ");
12103
12104 strcat (buff, _("<unknown>"));
12105 }
12106
12107 return buff;
12108 }
12109
12110 /* Display the contents of the version sections. */
12111
12112 static bool
12113 process_version_sections (Filedata * filedata)
12114 {
12115 Elf_Internal_Shdr * section;
12116 unsigned i;
12117 bool found = false;
12118
12119 if (! do_version)
12120 return true;
12121
12122 for (i = 0, section = filedata->section_headers;
12123 i < filedata->file_header.e_shnum;
12124 i++, section++)
12125 {
12126 switch (section->sh_type)
12127 {
12128 case SHT_GNU_verdef:
12129 {
12130 Elf_External_Verdef * edefs;
12131 unsigned long idx;
12132 unsigned long cnt;
12133 char * endbuf;
12134
12135 found = true;
12136
12137 if (filedata->is_separate)
12138 printf (ngettext ("\nIn linked file '%s' the version definition section '%s' contains %u entry:\n",
12139 "\nIn linked file '%s' the version definition section '%s' contains %u entries:\n",
12140 section->sh_info),
12141 filedata->file_name,
12142 printable_section_name (filedata, section),
12143 section->sh_info);
12144 else
12145 printf (ngettext ("\nVersion definition section '%s' "
12146 "contains %u entry:\n",
12147 "\nVersion definition section '%s' "
12148 "contains %u entries:\n",
12149 section->sh_info),
12150 printable_section_name (filedata, section),
12151 section->sh_info);
12152
12153 printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
12154 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
12155 (unsigned long) section->sh_offset, section->sh_link,
12156 printable_section_name_from_index (filedata, section->sh_link));
12157
12158 edefs = (Elf_External_Verdef *)
12159 get_data (NULL, filedata, section->sh_offset, 1,section->sh_size,
12160 _("version definition section"));
12161 if (!edefs)
12162 break;
12163 endbuf = (char *) edefs + section->sh_size;
12164
12165 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
12166 {
12167 char * vstart;
12168 Elf_External_Verdef * edef;
12169 Elf_Internal_Verdef ent;
12170 Elf_External_Verdaux * eaux;
12171 Elf_Internal_Verdaux aux;
12172 unsigned long isum;
12173 int j;
12174
12175 vstart = ((char *) edefs) + idx;
12176 if (vstart + sizeof (*edef) > endbuf)
12177 break;
12178
12179 edef = (Elf_External_Verdef *) vstart;
12180
12181 ent.vd_version = BYTE_GET (edef->vd_version);
12182 ent.vd_flags = BYTE_GET (edef->vd_flags);
12183 ent.vd_ndx = BYTE_GET (edef->vd_ndx);
12184 ent.vd_cnt = BYTE_GET (edef->vd_cnt);
12185 ent.vd_hash = BYTE_GET (edef->vd_hash);
12186 ent.vd_aux = BYTE_GET (edef->vd_aux);
12187 ent.vd_next = BYTE_GET (edef->vd_next);
12188
12189 printf (_(" %#06lx: Rev: %d Flags: %s"),
12190 idx, ent.vd_version, get_ver_flags (ent.vd_flags));
12191
12192 printf (_(" Index: %d Cnt: %d "),
12193 ent.vd_ndx, ent.vd_cnt);
12194
12195 /* Check for overflow. */
12196 if (ent.vd_aux > (size_t) (endbuf - vstart))
12197 break;
12198
12199 vstart += ent.vd_aux;
12200
12201 if (vstart + sizeof (*eaux) > endbuf)
12202 break;
12203 eaux = (Elf_External_Verdaux *) vstart;
12204
12205 aux.vda_name = BYTE_GET (eaux->vda_name);
12206 aux.vda_next = BYTE_GET (eaux->vda_next);
12207
12208 if (valid_dynamic_name (filedata, aux.vda_name))
12209 printf (_("Name: %s\n"),
12210 get_dynamic_name (filedata, aux.vda_name));
12211 else
12212 printf (_("Name index: %ld\n"), aux.vda_name);
12213
12214 isum = idx + ent.vd_aux;
12215
12216 for (j = 1; j < ent.vd_cnt; j++)
12217 {
12218 if (aux.vda_next < sizeof (*eaux)
12219 && !(j == ent.vd_cnt - 1 && aux.vda_next == 0))
12220 {
12221 warn (_("Invalid vda_next field of %lx\n"),
12222 aux.vda_next);
12223 j = ent.vd_cnt;
12224 break;
12225 }
12226 /* Check for overflow. */
12227 if (aux.vda_next > (size_t) (endbuf - vstart))
12228 break;
12229
12230 isum += aux.vda_next;
12231 vstart += aux.vda_next;
12232
12233 if (vstart + sizeof (*eaux) > endbuf)
12234 break;
12235 eaux = (Elf_External_Verdaux *) vstart;
12236
12237 aux.vda_name = BYTE_GET (eaux->vda_name);
12238 aux.vda_next = BYTE_GET (eaux->vda_next);
12239
12240 if (valid_dynamic_name (filedata, aux.vda_name))
12241 printf (_(" %#06lx: Parent %d: %s\n"),
12242 isum, j,
12243 get_dynamic_name (filedata, aux.vda_name));
12244 else
12245 printf (_(" %#06lx: Parent %d, name index: %ld\n"),
12246 isum, j, aux.vda_name);
12247 }
12248
12249 if (j < ent.vd_cnt)
12250 printf (_(" Version def aux past end of section\n"));
12251
12252 /* PR 17531:
12253 file: id:000001,src:000172+005151,op:splice,rep:2. */
12254 if (ent.vd_next < sizeof (*edef)
12255 && !(cnt == section->sh_info - 1 && ent.vd_next == 0))
12256 {
12257 warn (_("Invalid vd_next field of %lx\n"), ent.vd_next);
12258 cnt = section->sh_info;
12259 break;
12260 }
12261 if (ent.vd_next > (size_t) (endbuf - ((char *) edefs + idx)))
12262 break;
12263
12264 idx += ent.vd_next;
12265 }
12266
12267 if (cnt < section->sh_info)
12268 printf (_(" Version definition past end of section\n"));
12269
12270 free (edefs);
12271 }
12272 break;
12273
12274 case SHT_GNU_verneed:
12275 {
12276 Elf_External_Verneed * eneed;
12277 unsigned long idx;
12278 unsigned long cnt;
12279 char * endbuf;
12280
12281 found = true;
12282
12283 if (filedata->is_separate)
12284 printf (ngettext ("\nIn linked file '%s' the version needs section '%s' contains %u entry:\n",
12285 "\nIn linked file '%s' the version needs section '%s' contains %u entries:\n",
12286 section->sh_info),
12287 filedata->file_name,
12288 printable_section_name (filedata, section),
12289 section->sh_info);
12290 else
12291 printf (ngettext ("\nVersion needs section '%s' "
12292 "contains %u entry:\n",
12293 "\nVersion needs section '%s' "
12294 "contains %u entries:\n",
12295 section->sh_info),
12296 printable_section_name (filedata, section),
12297 section->sh_info);
12298
12299 printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
12300 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
12301 (unsigned long) section->sh_offset, section->sh_link,
12302 printable_section_name_from_index (filedata, section->sh_link));
12303
12304 eneed = (Elf_External_Verneed *) get_data (NULL, filedata,
12305 section->sh_offset, 1,
12306 section->sh_size,
12307 _("Version Needs section"));
12308 if (!eneed)
12309 break;
12310 endbuf = (char *) eneed + section->sh_size;
12311
12312 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
12313 {
12314 Elf_External_Verneed * entry;
12315 Elf_Internal_Verneed ent;
12316 unsigned long isum;
12317 int j;
12318 char * vstart;
12319
12320 vstart = ((char *) eneed) + idx;
12321 if (vstart + sizeof (*entry) > endbuf)
12322 break;
12323
12324 entry = (Elf_External_Verneed *) vstart;
12325
12326 ent.vn_version = BYTE_GET (entry->vn_version);
12327 ent.vn_cnt = BYTE_GET (entry->vn_cnt);
12328 ent.vn_file = BYTE_GET (entry->vn_file);
12329 ent.vn_aux = BYTE_GET (entry->vn_aux);
12330 ent.vn_next = BYTE_GET (entry->vn_next);
12331
12332 printf (_(" %#06lx: Version: %d"), idx, ent.vn_version);
12333
12334 if (valid_dynamic_name (filedata, ent.vn_file))
12335 printf (_(" File: %s"),
12336 get_dynamic_name (filedata, ent.vn_file));
12337 else
12338 printf (_(" File: %lx"), ent.vn_file);
12339
12340 printf (_(" Cnt: %d\n"), ent.vn_cnt);
12341
12342 /* Check for overflow. */
12343 if (ent.vn_aux > (size_t) (endbuf - vstart))
12344 break;
12345 vstart += ent.vn_aux;
12346
12347 for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
12348 {
12349 Elf_External_Vernaux * eaux;
12350 Elf_Internal_Vernaux aux;
12351
12352 if (vstart + sizeof (*eaux) > endbuf)
12353 break;
12354 eaux = (Elf_External_Vernaux *) vstart;
12355
12356 aux.vna_hash = BYTE_GET (eaux->vna_hash);
12357 aux.vna_flags = BYTE_GET (eaux->vna_flags);
12358 aux.vna_other = BYTE_GET (eaux->vna_other);
12359 aux.vna_name = BYTE_GET (eaux->vna_name);
12360 aux.vna_next = BYTE_GET (eaux->vna_next);
12361
12362 if (valid_dynamic_name (filedata, aux.vna_name))
12363 printf (_(" %#06lx: Name: %s"),
12364 isum, get_dynamic_name (filedata, aux.vna_name));
12365 else
12366 printf (_(" %#06lx: Name index: %lx"),
12367 isum, aux.vna_name);
12368
12369 printf (_(" Flags: %s Version: %d\n"),
12370 get_ver_flags (aux.vna_flags), aux.vna_other);
12371
12372 if (aux.vna_next < sizeof (*eaux)
12373 && !(j == ent.vn_cnt - 1 && aux.vna_next == 0))
12374 {
12375 warn (_("Invalid vna_next field of %lx\n"),
12376 aux.vna_next);
12377 j = ent.vn_cnt;
12378 break;
12379 }
12380 /* Check for overflow. */
12381 if (aux.vna_next > (size_t) (endbuf - vstart))
12382 break;
12383 isum += aux.vna_next;
12384 vstart += aux.vna_next;
12385 }
12386
12387 if (j < ent.vn_cnt)
12388 warn (_("Missing Version Needs auxiliary information\n"));
12389
12390 if (ent.vn_next < sizeof (*entry)
12391 && !(cnt == section->sh_info - 1 && ent.vn_next == 0))
12392 {
12393 warn (_("Invalid vn_next field of %lx\n"), ent.vn_next);
12394 cnt = section->sh_info;
12395 break;
12396 }
12397 if (ent.vn_next > (size_t) (endbuf - ((char *) eneed + idx)))
12398 break;
12399 idx += ent.vn_next;
12400 }
12401
12402 if (cnt < section->sh_info)
12403 warn (_("Missing Version Needs information\n"));
12404
12405 free (eneed);
12406 }
12407 break;
12408
12409 case SHT_GNU_versym:
12410 {
12411 Elf_Internal_Shdr * link_section;
12412 size_t total;
12413 unsigned int cnt;
12414 unsigned char * edata;
12415 unsigned short * data;
12416 char * strtab;
12417 Elf_Internal_Sym * symbols;
12418 Elf_Internal_Shdr * string_sec;
12419 unsigned long num_syms;
12420 long off;
12421
12422 if (section->sh_link >= filedata->file_header.e_shnum)
12423 break;
12424
12425 link_section = filedata->section_headers + section->sh_link;
12426 total = section->sh_size / sizeof (Elf_External_Versym);
12427
12428 if (link_section->sh_link >= filedata->file_header.e_shnum)
12429 break;
12430
12431 found = true;
12432
12433 symbols = get_elf_symbols (filedata, link_section, & num_syms);
12434 if (symbols == NULL)
12435 break;
12436
12437 string_sec = filedata->section_headers + link_section->sh_link;
12438
12439 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
12440 string_sec->sh_size,
12441 _("version string table"));
12442 if (!strtab)
12443 {
12444 free (symbols);
12445 break;
12446 }
12447
12448 if (filedata->is_separate)
12449 printf (ngettext ("\nIn linked file '%s' the version symbols section '%s' contains %lu entry:\n",
12450 "\nIn linked file '%s' the version symbols section '%s' contains %lu entries:\n",
12451 total),
12452 filedata->file_name,
12453 printable_section_name (filedata, section),
12454 (unsigned long) total);
12455 else
12456 printf (ngettext ("\nVersion symbols section '%s' "
12457 "contains %lu entry:\n",
12458 "\nVersion symbols section '%s' "
12459 "contains %lu entries:\n",
12460 total),
12461 printable_section_name (filedata, section),
12462 (unsigned long) total);
12463
12464 printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
12465 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
12466 (unsigned long) section->sh_offset, section->sh_link,
12467 printable_section_name (filedata, link_section));
12468
12469 off = offset_from_vma (filedata,
12470 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
12471 total * sizeof (short));
12472 edata = (unsigned char *) get_data (NULL, filedata, off,
12473 sizeof (short), total,
12474 _("version symbol data"));
12475 if (!edata)
12476 {
12477 free (strtab);
12478 free (symbols);
12479 break;
12480 }
12481
12482 data = (short unsigned int *) cmalloc (total, sizeof (short));
12483
12484 for (cnt = total; cnt --;)
12485 data[cnt] = byte_get (edata + cnt * sizeof (short),
12486 sizeof (short));
12487
12488 free (edata);
12489
12490 for (cnt = 0; cnt < total; cnt += 4)
12491 {
12492 int j, nn;
12493 char *name;
12494 char *invalid = _("*invalid*");
12495
12496 printf (" %03x:", cnt);
12497
12498 for (j = 0; (j < 4) && (cnt + j) < total; ++j)
12499 switch (data[cnt + j])
12500 {
12501 case 0:
12502 fputs (_(" 0 (*local*) "), stdout);
12503 break;
12504
12505 case 1:
12506 fputs (_(" 1 (*global*) "), stdout);
12507 break;
12508
12509 default:
12510 nn = printf ("%4x%c", data[cnt + j] & VERSYM_VERSION,
12511 data[cnt + j] & VERSYM_HIDDEN ? 'h' : ' ');
12512
12513 /* If this index value is greater than the size of the symbols
12514 array, break to avoid an out-of-bounds read. */
12515 if ((unsigned long)(cnt + j) >= num_syms)
12516 {
12517 warn (_("invalid index into symbol array\n"));
12518 break;
12519 }
12520
12521 name = NULL;
12522 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
12523 {
12524 Elf_Internal_Verneed ivn;
12525 unsigned long offset;
12526
12527 offset = offset_from_vma
12528 (filedata,
12529 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
12530 sizeof (Elf_External_Verneed));
12531
12532 do
12533 {
12534 Elf_Internal_Vernaux ivna;
12535 Elf_External_Verneed evn;
12536 Elf_External_Vernaux evna;
12537 unsigned long a_off;
12538
12539 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
12540 _("version need")) == NULL)
12541 break;
12542
12543 ivn.vn_aux = BYTE_GET (evn.vn_aux);
12544 ivn.vn_next = BYTE_GET (evn.vn_next);
12545
12546 a_off = offset + ivn.vn_aux;
12547
12548 do
12549 {
12550 if (get_data (&evna, filedata, a_off, sizeof (evna),
12551 1, _("version need aux (2)")) == NULL)
12552 {
12553 ivna.vna_next = 0;
12554 ivna.vna_other = 0;
12555 }
12556 else
12557 {
12558 ivna.vna_next = BYTE_GET (evna.vna_next);
12559 ivna.vna_other = BYTE_GET (evna.vna_other);
12560 }
12561
12562 a_off += ivna.vna_next;
12563 }
12564 while (ivna.vna_other != data[cnt + j]
12565 && ivna.vna_next != 0);
12566
12567 if (ivna.vna_other == data[cnt + j])
12568 {
12569 ivna.vna_name = BYTE_GET (evna.vna_name);
12570
12571 if (ivna.vna_name >= string_sec->sh_size)
12572 name = invalid;
12573 else
12574 name = strtab + ivna.vna_name;
12575 break;
12576 }
12577
12578 offset += ivn.vn_next;
12579 }
12580 while (ivn.vn_next);
12581 }
12582
12583 if (data[cnt + j] != 0x8001
12584 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
12585 {
12586 Elf_Internal_Verdef ivd;
12587 Elf_External_Verdef evd;
12588 unsigned long offset;
12589
12590 offset = offset_from_vma
12591 (filedata,
12592 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
12593 sizeof evd);
12594
12595 do
12596 {
12597 if (get_data (&evd, filedata, offset, sizeof (evd), 1,
12598 _("version def")) == NULL)
12599 {
12600 ivd.vd_next = 0;
12601 /* PR 17531: file: 046-1082287-0.004. */
12602 ivd.vd_ndx = (data[cnt + j] & VERSYM_VERSION) + 1;
12603 break;
12604 }
12605 else
12606 {
12607 ivd.vd_next = BYTE_GET (evd.vd_next);
12608 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
12609 }
12610
12611 offset += ivd.vd_next;
12612 }
12613 while (ivd.vd_ndx != (data[cnt + j] & VERSYM_VERSION)
12614 && ivd.vd_next != 0);
12615
12616 if (ivd.vd_ndx == (data[cnt + j] & VERSYM_VERSION))
12617 {
12618 Elf_External_Verdaux evda;
12619 Elf_Internal_Verdaux ivda;
12620
12621 ivd.vd_aux = BYTE_GET (evd.vd_aux);
12622
12623 if (get_data (&evda, filedata,
12624 offset - ivd.vd_next + ivd.vd_aux,
12625 sizeof (evda), 1,
12626 _("version def aux")) == NULL)
12627 break;
12628
12629 ivda.vda_name = BYTE_GET (evda.vda_name);
12630
12631 if (ivda.vda_name >= string_sec->sh_size)
12632 name = invalid;
12633 else if (name != NULL && name != invalid)
12634 name = _("*both*");
12635 else
12636 name = strtab + ivda.vda_name;
12637 }
12638 }
12639 if (name != NULL)
12640 nn += printf ("(%s%-*s",
12641 name,
12642 12 - (int) strlen (name),
12643 ")");
12644
12645 if (nn < 18)
12646 printf ("%*c", 18 - nn, ' ');
12647 }
12648
12649 putchar ('\n');
12650 }
12651
12652 free (data);
12653 free (strtab);
12654 free (symbols);
12655 }
12656 break;
12657
12658 default:
12659 break;
12660 }
12661 }
12662
12663 if (! found)
12664 {
12665 if (filedata->is_separate)
12666 printf (_("\nNo version information found in linked file '%s'.\n"),
12667 filedata->file_name);
12668 else
12669 printf (_("\nNo version information found in this file.\n"));
12670 }
12671
12672 return true;
12673 }
12674
12675 static const char *
12676 get_symbol_binding (Filedata * filedata, unsigned int binding)
12677 {
12678 static char buff[64];
12679
12680 switch (binding)
12681 {
12682 case STB_LOCAL: return "LOCAL";
12683 case STB_GLOBAL: return "GLOBAL";
12684 case STB_WEAK: return "WEAK";
12685 default:
12686 if (binding >= STB_LOPROC && binding <= STB_HIPROC)
12687 snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
12688 binding);
12689 else if (binding >= STB_LOOS && binding <= STB_HIOS)
12690 {
12691 if (binding == STB_GNU_UNIQUE
12692 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU)
12693 return "UNIQUE";
12694 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
12695 }
12696 else
12697 snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
12698 return buff;
12699 }
12700 }
12701
12702 static const char *
12703 get_symbol_type (Filedata * filedata, unsigned int type)
12704 {
12705 static char buff[64];
12706
12707 switch (type)
12708 {
12709 case STT_NOTYPE: return "NOTYPE";
12710 case STT_OBJECT: return "OBJECT";
12711 case STT_FUNC: return "FUNC";
12712 case STT_SECTION: return "SECTION";
12713 case STT_FILE: return "FILE";
12714 case STT_COMMON: return "COMMON";
12715 case STT_TLS: return "TLS";
12716 case STT_RELC: return "RELC";
12717 case STT_SRELC: return "SRELC";
12718 default:
12719 if (type >= STT_LOPROC && type <= STT_HIPROC)
12720 {
12721 if (filedata->file_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
12722 return "THUMB_FUNC";
12723
12724 if (filedata->file_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
12725 return "REGISTER";
12726
12727 if (filedata->file_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
12728 return "PARISC_MILLI";
12729
12730 snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
12731 }
12732 else if (type >= STT_LOOS && type <= STT_HIOS)
12733 {
12734 if (filedata->file_header.e_machine == EM_PARISC)
12735 {
12736 if (type == STT_HP_OPAQUE)
12737 return "HP_OPAQUE";
12738 if (type == STT_HP_STUB)
12739 return "HP_STUB";
12740 }
12741
12742 if (type == STT_GNU_IFUNC
12743 && (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU
12744 || filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_FREEBSD))
12745 return "IFUNC";
12746
12747 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
12748 }
12749 else
12750 snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
12751 return buff;
12752 }
12753 }
12754
12755 static const char *
12756 get_symbol_visibility (unsigned int visibility)
12757 {
12758 switch (visibility)
12759 {
12760 case STV_DEFAULT: return "DEFAULT";
12761 case STV_INTERNAL: return "INTERNAL";
12762 case STV_HIDDEN: return "HIDDEN";
12763 case STV_PROTECTED: return "PROTECTED";
12764 default:
12765 error (_("Unrecognized visibility value: %u\n"), visibility);
12766 return _("<unknown>");
12767 }
12768 }
12769
12770 static const char *
12771 get_alpha_symbol_other (unsigned int other)
12772 {
12773 switch (other)
12774 {
12775 case STO_ALPHA_NOPV: return "NOPV";
12776 case STO_ALPHA_STD_GPLOAD: return "STD GPLOAD";
12777 default:
12778 error (_("Unrecognized alpha specific other value: %u\n"), other);
12779 return _("<unknown>");
12780 }
12781 }
12782
12783 static const char *
12784 get_solaris_symbol_visibility (unsigned int visibility)
12785 {
12786 switch (visibility)
12787 {
12788 case 4: return "EXPORTED";
12789 case 5: return "SINGLETON";
12790 case 6: return "ELIMINATE";
12791 default: return get_symbol_visibility (visibility);
12792 }
12793 }
12794
12795 static const char *
12796 get_aarch64_symbol_other (unsigned int other)
12797 {
12798 static char buf[32];
12799
12800 if (other & STO_AARCH64_VARIANT_PCS)
12801 {
12802 other &= ~STO_AARCH64_VARIANT_PCS;
12803 if (other == 0)
12804 return "VARIANT_PCS";
12805 snprintf (buf, sizeof buf, "VARIANT_PCS | %x", other);
12806 return buf;
12807 }
12808 return NULL;
12809 }
12810
12811 static const char *
12812 get_mips_symbol_other (unsigned int other)
12813 {
12814 switch (other)
12815 {
12816 case STO_OPTIONAL: return "OPTIONAL";
12817 case STO_MIPS_PLT: return "MIPS PLT";
12818 case STO_MIPS_PIC: return "MIPS PIC";
12819 case STO_MICROMIPS: return "MICROMIPS";
12820 case STO_MICROMIPS | STO_MIPS_PIC: return "MICROMIPS, MIPS PIC";
12821 case STO_MIPS16: return "MIPS16";
12822 default: return NULL;
12823 }
12824 }
12825
12826 static const char *
12827 get_ia64_symbol_other (Filedata * filedata, unsigned int other)
12828 {
12829 if (is_ia64_vms (filedata))
12830 {
12831 static char res[32];
12832
12833 res[0] = 0;
12834
12835 /* Function types is for images and .STB files only. */
12836 switch (filedata->file_header.e_type)
12837 {
12838 case ET_DYN:
12839 case ET_EXEC:
12840 switch (VMS_ST_FUNC_TYPE (other))
12841 {
12842 case VMS_SFT_CODE_ADDR:
12843 strcat (res, " CA");
12844 break;
12845 case VMS_SFT_SYMV_IDX:
12846 strcat (res, " VEC");
12847 break;
12848 case VMS_SFT_FD:
12849 strcat (res, " FD");
12850 break;
12851 case VMS_SFT_RESERVE:
12852 strcat (res, " RSV");
12853 break;
12854 default:
12855 warn (_("Unrecognized IA64 VMS ST Function type: %d\n"),
12856 VMS_ST_FUNC_TYPE (other));
12857 strcat (res, " <unknown>");
12858 break;
12859 }
12860 break;
12861 default:
12862 break;
12863 }
12864 switch (VMS_ST_LINKAGE (other))
12865 {
12866 case VMS_STL_IGNORE:
12867 strcat (res, " IGN");
12868 break;
12869 case VMS_STL_RESERVE:
12870 strcat (res, " RSV");
12871 break;
12872 case VMS_STL_STD:
12873 strcat (res, " STD");
12874 break;
12875 case VMS_STL_LNK:
12876 strcat (res, " LNK");
12877 break;
12878 default:
12879 warn (_("Unrecognized IA64 VMS ST Linkage: %d\n"),
12880 VMS_ST_LINKAGE (other));
12881 strcat (res, " <unknown>");
12882 break;
12883 }
12884
12885 if (res[0] != 0)
12886 return res + 1;
12887 else
12888 return res;
12889 }
12890 return NULL;
12891 }
12892
12893 static const char *
12894 get_ppc64_symbol_other (unsigned int other)
12895 {
12896 if ((other & ~STO_PPC64_LOCAL_MASK) != 0)
12897 return NULL;
12898
12899 other >>= STO_PPC64_LOCAL_BIT;
12900 if (other <= 6)
12901 {
12902 static char buf[64];
12903 if (other >= 2)
12904 other = ppc64_decode_local_entry (other);
12905 snprintf (buf, sizeof buf, _("<localentry>: %d"), other);
12906 return buf;
12907 }
12908 return NULL;
12909 }
12910
12911 static const char *
12912 get_riscv_symbol_other (unsigned int other)
12913 {
12914 static char buf[32];
12915 buf[0] = 0;
12916
12917 if (other & STO_RISCV_VARIANT_CC)
12918 {
12919 strcat (buf, _(" VARIANT_CC"));
12920 other &= ~STO_RISCV_VARIANT_CC;
12921 }
12922
12923 if (other != 0)
12924 snprintf (buf, sizeof buf, " %x", other);
12925
12926
12927 if (buf[0] != 0)
12928 return buf + 1;
12929 else
12930 return buf;
12931 }
12932
12933 static const char *
12934 get_symbol_other (Filedata * filedata, unsigned int other)
12935 {
12936 const char * result = NULL;
12937 static char buff [64];
12938
12939 if (other == 0)
12940 return "";
12941
12942 switch (filedata->file_header.e_machine)
12943 {
12944 case EM_ALPHA:
12945 result = get_alpha_symbol_other (other);
12946 break;
12947 case EM_AARCH64:
12948 result = get_aarch64_symbol_other (other);
12949 break;
12950 case EM_MIPS:
12951 result = get_mips_symbol_other (other);
12952 break;
12953 case EM_IA_64:
12954 result = get_ia64_symbol_other (filedata, other);
12955 break;
12956 case EM_PPC64:
12957 result = get_ppc64_symbol_other (other);
12958 break;
12959 case EM_RISCV:
12960 result = get_riscv_symbol_other (other);
12961 break;
12962 default:
12963 result = NULL;
12964 break;
12965 }
12966
12967 if (result)
12968 return result;
12969
12970 snprintf (buff, sizeof buff, _("<other>: %x"), other);
12971 return buff;
12972 }
12973
12974 static const char *
12975 get_symbol_index_type (Filedata * filedata, unsigned int type)
12976 {
12977 static char buff[32];
12978
12979 switch (type)
12980 {
12981 case SHN_UNDEF: return "UND";
12982 case SHN_ABS: return "ABS";
12983 case SHN_COMMON: return "COM";
12984 default:
12985 if (type == SHN_IA_64_ANSI_COMMON
12986 && filedata->file_header.e_machine == EM_IA_64
12987 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX)
12988 return "ANSI_COM";
12989 else if ((filedata->file_header.e_machine == EM_X86_64
12990 || filedata->file_header.e_machine == EM_L1OM
12991 || filedata->file_header.e_machine == EM_K1OM)
12992 && type == SHN_X86_64_LCOMMON)
12993 return "LARGE_COM";
12994 else if ((type == SHN_MIPS_SCOMMON
12995 && filedata->file_header.e_machine == EM_MIPS)
12996 || (type == SHN_TIC6X_SCOMMON
12997 && filedata->file_header.e_machine == EM_TI_C6000))
12998 return "SCOM";
12999 else if (type == SHN_MIPS_SUNDEFINED
13000 && filedata->file_header.e_machine == EM_MIPS)
13001 return "SUND";
13002 else if (type >= SHN_LOPROC && type <= SHN_HIPROC)
13003 sprintf (buff, "PRC[0x%04x]", type & 0xffff);
13004 else if (type >= SHN_LOOS && type <= SHN_HIOS)
13005 sprintf (buff, "OS [0x%04x]", type & 0xffff);
13006 else if (type >= SHN_LORESERVE)
13007 sprintf (buff, "RSV[0x%04x]", type & 0xffff);
13008 else if (filedata->file_header.e_shnum != 0
13009 && type >= filedata->file_header.e_shnum)
13010 sprintf (buff, _("bad section index[%3d]"), type);
13011 else
13012 sprintf (buff, "%3d", type);
13013 break;
13014 }
13015
13016 return buff;
13017 }
13018
13019 static const char *
13020 get_symbol_version_string (Filedata * filedata,
13021 bool is_dynsym,
13022 const char * strtab,
13023 unsigned long int strtab_size,
13024 unsigned int si,
13025 Elf_Internal_Sym * psym,
13026 enum versioned_symbol_info * sym_info,
13027 unsigned short * vna_other)
13028 {
13029 unsigned char data[2];
13030 unsigned short vers_data;
13031 unsigned long offset;
13032 unsigned short max_vd_ndx;
13033
13034 if (!is_dynsym
13035 || filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)] == 0)
13036 return NULL;
13037
13038 offset = offset_from_vma (filedata,
13039 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
13040 sizeof data + si * sizeof (vers_data));
13041
13042 if (get_data (&data, filedata, offset + si * sizeof (vers_data),
13043 sizeof (data), 1, _("version data")) == NULL)
13044 return NULL;
13045
13046 vers_data = byte_get (data, 2);
13047
13048 if ((vers_data & VERSYM_HIDDEN) == 0 && vers_data == 0)
13049 return NULL;
13050
13051 *sym_info = (vers_data & VERSYM_HIDDEN) != 0 ? symbol_hidden : symbol_public;
13052 max_vd_ndx = 0;
13053
13054 /* Usually we'd only see verdef for defined symbols, and verneed for
13055 undefined symbols. However, symbols defined by the linker in
13056 .dynbss for variables copied from a shared library in order to
13057 avoid text relocations are defined yet have verneed. We could
13058 use a heuristic to detect the special case, for example, check
13059 for verneed first on symbols defined in SHT_NOBITS sections, but
13060 it is simpler and more reliable to just look for both verdef and
13061 verneed. .dynbss might not be mapped to a SHT_NOBITS section. */
13062
13063 if (psym->st_shndx != SHN_UNDEF
13064 && vers_data != 0x8001
13065 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
13066 {
13067 Elf_Internal_Verdef ivd;
13068 Elf_Internal_Verdaux ivda;
13069 Elf_External_Verdaux evda;
13070 unsigned long off;
13071
13072 off = offset_from_vma (filedata,
13073 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
13074 sizeof (Elf_External_Verdef));
13075
13076 do
13077 {
13078 Elf_External_Verdef evd;
13079
13080 if (get_data (&evd, filedata, off, sizeof (evd), 1,
13081 _("version def")) == NULL)
13082 {
13083 ivd.vd_ndx = 0;
13084 ivd.vd_aux = 0;
13085 ivd.vd_next = 0;
13086 ivd.vd_flags = 0;
13087 }
13088 else
13089 {
13090 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
13091 ivd.vd_aux = BYTE_GET (evd.vd_aux);
13092 ivd.vd_next = BYTE_GET (evd.vd_next);
13093 ivd.vd_flags = BYTE_GET (evd.vd_flags);
13094 }
13095
13096 if ((ivd.vd_ndx & VERSYM_VERSION) > max_vd_ndx)
13097 max_vd_ndx = ivd.vd_ndx & VERSYM_VERSION;
13098
13099 off += ivd.vd_next;
13100 }
13101 while (ivd.vd_ndx != (vers_data & VERSYM_VERSION) && ivd.vd_next != 0);
13102
13103 if (ivd.vd_ndx == (vers_data & VERSYM_VERSION))
13104 {
13105 if (ivd.vd_ndx == 1 && ivd.vd_flags == VER_FLG_BASE)
13106 return NULL;
13107
13108 off -= ivd.vd_next;
13109 off += ivd.vd_aux;
13110
13111 if (get_data (&evda, filedata, off, sizeof (evda), 1,
13112 _("version def aux")) != NULL)
13113 {
13114 ivda.vda_name = BYTE_GET (evda.vda_name);
13115
13116 if (psym->st_name != ivda.vda_name)
13117 return (ivda.vda_name < strtab_size
13118 ? strtab + ivda.vda_name : _("<corrupt>"));
13119 }
13120 }
13121 }
13122
13123 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
13124 {
13125 Elf_External_Verneed evn;
13126 Elf_Internal_Verneed ivn;
13127 Elf_Internal_Vernaux ivna;
13128
13129 offset = offset_from_vma (filedata,
13130 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
13131 sizeof evn);
13132 do
13133 {
13134 unsigned long vna_off;
13135
13136 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
13137 _("version need")) == NULL)
13138 {
13139 ivna.vna_next = 0;
13140 ivna.vna_other = 0;
13141 ivna.vna_name = 0;
13142 break;
13143 }
13144
13145 ivn.vn_aux = BYTE_GET (evn.vn_aux);
13146 ivn.vn_next = BYTE_GET (evn.vn_next);
13147
13148 vna_off = offset + ivn.vn_aux;
13149
13150 do
13151 {
13152 Elf_External_Vernaux evna;
13153
13154 if (get_data (&evna, filedata, vna_off, sizeof (evna), 1,
13155 _("version need aux (3)")) == NULL)
13156 {
13157 ivna.vna_next = 0;
13158 ivna.vna_other = 0;
13159 ivna.vna_name = 0;
13160 }
13161 else
13162 {
13163 ivna.vna_other = BYTE_GET (evna.vna_other);
13164 ivna.vna_next = BYTE_GET (evna.vna_next);
13165 ivna.vna_name = BYTE_GET (evna.vna_name);
13166 }
13167
13168 vna_off += ivna.vna_next;
13169 }
13170 while (ivna.vna_other != vers_data && ivna.vna_next != 0);
13171
13172 if (ivna.vna_other == vers_data)
13173 break;
13174
13175 offset += ivn.vn_next;
13176 }
13177 while (ivn.vn_next != 0);
13178
13179 if (ivna.vna_other == vers_data)
13180 {
13181 *sym_info = symbol_undefined;
13182 *vna_other = ivna.vna_other;
13183 return (ivna.vna_name < strtab_size
13184 ? strtab + ivna.vna_name : _("<corrupt>"));
13185 }
13186 else if ((max_vd_ndx || (vers_data & VERSYM_VERSION) != 1)
13187 && (vers_data & VERSYM_VERSION) > max_vd_ndx)
13188 return _("<corrupt>");
13189 }
13190 return NULL;
13191 }
13192
13193 /* Display a symbol size on stdout. Format is based on --sym-base setting. */
13194
13195 static unsigned int
13196 print_dynamic_symbol_size (uint64_t vma, int base)
13197 {
13198 switch (base)
13199 {
13200 case 8:
13201 return print_vma (vma, OCTAL_5);
13202
13203 case 10:
13204 return print_vma (vma, UNSIGNED_5);
13205
13206 case 16:
13207 return print_vma (vma, PREFIX_HEX_5);
13208
13209 case 0:
13210 default:
13211 return print_vma (vma, DEC_5);
13212 }
13213 }
13214
13215 static void
13216 print_dynamic_symbol (Filedata *filedata, unsigned long si,
13217 Elf_Internal_Sym *symtab,
13218 Elf_Internal_Shdr *section,
13219 char *strtab, size_t strtab_size)
13220 {
13221 const char *version_string;
13222 enum versioned_symbol_info sym_info;
13223 unsigned short vna_other;
13224 bool is_valid;
13225 const char * sstr;
13226 Elf_Internal_Sym *psym = symtab + si;
13227
13228 printf ("%6ld: ", si);
13229 print_vma (psym->st_value, LONG_HEX);
13230 putchar (' ');
13231 print_dynamic_symbol_size (psym->st_size, sym_base);
13232 printf (" %-7s", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
13233 printf (" %-6s", get_symbol_binding (filedata, ELF_ST_BIND (psym->st_info)));
13234 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
13235 printf (" %-7s", get_solaris_symbol_visibility (psym->st_other));
13236 else
13237 {
13238 unsigned int vis = ELF_ST_VISIBILITY (psym->st_other);
13239
13240 printf (" %-7s", get_symbol_visibility (vis));
13241 /* Check to see if any other bits in the st_other field are set.
13242 Note - displaying this information disrupts the layout of the
13243 table being generated, but for the moment this case is very rare. */
13244 if (psym->st_other ^ vis)
13245 printf (" [%s] ", get_symbol_other (filedata, psym->st_other ^ vis));
13246 }
13247 printf (" %4s ", get_symbol_index_type (filedata, psym->st_shndx));
13248
13249 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION
13250 && psym->st_shndx < filedata->file_header.e_shnum
13251 && filedata->section_headers != NULL
13252 && psym->st_name == 0)
13253 {
13254 is_valid
13255 = section_name_valid (filedata,
13256 filedata->section_headers + psym->st_shndx);
13257 sstr = is_valid ?
13258 section_name_print (filedata,
13259 filedata->section_headers + psym->st_shndx)
13260 : _("<corrupt>");
13261 }
13262 else
13263 {
13264 is_valid = valid_symbol_name (strtab, strtab_size, psym->st_name);
13265 sstr = is_valid ? strtab + psym->st_name : _("<corrupt>");
13266 }
13267
13268 version_string
13269 = get_symbol_version_string (filedata,
13270 (section == NULL
13271 || section->sh_type == SHT_DYNSYM),
13272 strtab, strtab_size, si,
13273 psym, &sym_info, &vna_other);
13274
13275 int len_avail = 21;
13276 if (! do_wide && version_string != NULL)
13277 {
13278 char buffer[16];
13279
13280 len_avail -= 1 + strlen (version_string);
13281
13282 if (sym_info == symbol_undefined)
13283 len_avail -= sprintf (buffer," (%d)", vna_other);
13284 else if (sym_info != symbol_hidden)
13285 len_avail -= 1;
13286 }
13287
13288 print_symbol (len_avail, sstr);
13289
13290 if (version_string)
13291 {
13292 if (sym_info == symbol_undefined)
13293 printf ("@%s (%d)", version_string, vna_other);
13294 else
13295 printf (sym_info == symbol_hidden ? "@%s" : "@@%s",
13296 version_string);
13297 }
13298
13299 putchar ('\n');
13300
13301 if (ELF_ST_BIND (psym->st_info) == STB_LOCAL
13302 && section != NULL
13303 && si >= section->sh_info
13304 /* Irix 5 and 6 MIPS binaries are known to ignore this requirement. */
13305 && filedata->file_header.e_machine != EM_MIPS
13306 /* Solaris binaries have been found to violate this requirement as
13307 well. Not sure if this is a bug or an ABI requirement. */
13308 && filedata->file_header.e_ident[EI_OSABI] != ELFOSABI_SOLARIS)
13309 warn (_("local symbol %lu found at index >= %s's sh_info value of %u\n"),
13310 si, printable_section_name (filedata, section), section->sh_info);
13311 }
13312
13313 static const char *
13314 get_lto_kind (unsigned int kind)
13315 {
13316 switch (kind)
13317 {
13318 case 0: return "DEF";
13319 case 1: return "WEAKDEF";
13320 case 2: return "UNDEF";
13321 case 3: return "WEAKUNDEF";
13322 case 4: return "COMMON";
13323 default:
13324 break;
13325 }
13326
13327 static char buffer[30];
13328 error (_("Unknown LTO symbol definition encountered: %u\n"), kind);
13329 sprintf (buffer, "<unknown: %u>", kind);
13330 return buffer;
13331 }
13332
13333 static const char *
13334 get_lto_visibility (unsigned int visibility)
13335 {
13336 switch (visibility)
13337 {
13338 case 0: return "DEFAULT";
13339 case 1: return "PROTECTED";
13340 case 2: return "INTERNAL";
13341 case 3: return "HIDDEN";
13342 default:
13343 break;
13344 }
13345
13346 static char buffer[30];
13347 error (_("Unknown LTO symbol visibility encountered: %u\n"), visibility);
13348 sprintf (buffer, "<unknown: %u>", visibility);
13349 return buffer;
13350 }
13351
13352 static const char *
13353 get_lto_sym_type (unsigned int sym_type)
13354 {
13355 switch (sym_type)
13356 {
13357 case 0: return "UNKNOWN";
13358 case 1: return "FUNCTION";
13359 case 2: return "VARIABLE";
13360 default:
13361 break;
13362 }
13363
13364 static char buffer[30];
13365 error (_("Unknown LTO symbol type encountered: %u\n"), sym_type);
13366 sprintf (buffer, "<unknown: %u>", sym_type);
13367 return buffer;
13368 }
13369
13370 /* Display an LTO format symbol table.
13371 FIXME: The format of LTO symbol tables is not formalized.
13372 So this code could need changing in the future. */
13373
13374 static bool
13375 display_lto_symtab (Filedata * filedata,
13376 Elf_Internal_Shdr * section)
13377 {
13378 if (section->sh_size == 0)
13379 {
13380 if (filedata->is_separate)
13381 printf (_("\nThe LTO Symbol table section '%s' in linked file '%s' is empty!\n"),
13382 printable_section_name (filedata, section),
13383 filedata->file_name);
13384 else
13385 printf (_("\nLTO Symbol table '%s' is empty!\n"),
13386 printable_section_name (filedata, section));
13387
13388 return true;
13389 }
13390
13391 if (section->sh_size > filedata->file_size)
13392 {
13393 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
13394 printable_section_name (filedata, section),
13395 (unsigned long) section->sh_size);
13396 return false;
13397 }
13398
13399 void * alloced_data = get_data (NULL, filedata, section->sh_offset,
13400 section->sh_size, 1, _("LTO symbols"));
13401 if (alloced_data == NULL)
13402 return false;
13403
13404 /* Look for extended data for the symbol table. */
13405 Elf_Internal_Shdr * ext;
13406 void * ext_data_orig = NULL;
13407 char * ext_data = NULL;
13408 char * ext_data_end = NULL;
13409 char * ext_name = NULL;
13410
13411 if (asprintf (& ext_name, ".gnu.lto_.ext_symtab.%s",
13412 (section_name (filedata, section)
13413 + sizeof (".gnu.lto_.symtab.") - 1)) > 0
13414 && ext_name != NULL /* Paranoia. */
13415 && (ext = find_section (filedata, ext_name)) != NULL)
13416 {
13417 if (ext->sh_size < 3)
13418 error (_("LTO Symbol extension table '%s' is empty!\n"),
13419 printable_section_name (filedata, ext));
13420 else
13421 {
13422 ext_data_orig = ext_data = get_data (NULL, filedata, ext->sh_offset,
13423 ext->sh_size, 1,
13424 _("LTO ext symbol data"));
13425 if (ext_data != NULL)
13426 {
13427 ext_data_end = ext_data + ext->sh_size;
13428 if (* ext_data++ != 1)
13429 error (_("Unexpected version number in symbol extension table\n"));
13430 }
13431 }
13432 }
13433
13434 const unsigned char * data = (const unsigned char *) alloced_data;
13435 const unsigned char * end = data + section->sh_size;
13436
13437 if (filedata->is_separate)
13438 printf (_("\nIn linked file '%s': "), filedata->file_name);
13439 else
13440 printf ("\n");
13441
13442 if (ext_data_orig != NULL)
13443 {
13444 if (do_wide)
13445 printf (_("LTO Symbol table '%s' and extension table '%s' contain:\n"),
13446 printable_section_name (filedata, section),
13447 printable_section_name (filedata, ext));
13448 else
13449 {
13450 printf (_("LTO Symbol table '%s'\n"),
13451 printable_section_name (filedata, section));
13452 printf (_(" and extension table '%s' contain:\n"),
13453 printable_section_name (filedata, ext));
13454 }
13455 }
13456 else
13457 printf (_("LTO Symbol table '%s' contains:\n"),
13458 printable_section_name (filedata, section));
13459
13460 /* FIXME: Add a wide version. */
13461 if (ext_data_orig != NULL)
13462 printf (_(" Comdat_Key Kind Visibility Size Slot Type Section Name\n"));
13463 else
13464 printf (_(" Comdat_Key Kind Visibility Size Slot Name\n"));
13465
13466 /* FIXME: We do not handle style prefixes. */
13467
13468 while (data < end)
13469 {
13470 const unsigned char * sym_name = data;
13471 data += strnlen ((const char *) sym_name, end - data) + 1;
13472 if (data >= end)
13473 goto fail;
13474
13475 const unsigned char * comdat_key = data;
13476 data += strnlen ((const char *) comdat_key, end - data) + 1;
13477 if (data >= end)
13478 goto fail;
13479
13480 if (data + 2 + 8 + 4 > end)
13481 goto fail;
13482
13483 unsigned int kind = *data++;
13484 unsigned int visibility = *data++;
13485
13486 uint64_t size = byte_get (data, 8);
13487 data += 8;
13488
13489 uint64_t slot = byte_get (data, 4);
13490 data += 4;
13491
13492 if (ext_data != NULL)
13493 {
13494 if (ext_data < (ext_data_end - 1))
13495 {
13496 unsigned int sym_type = * ext_data ++;
13497 unsigned int sec_kind = * ext_data ++;
13498
13499 printf (" %10s %10s %11s %08" PRIx64 " %08" PRIx64 " %9s %08x _",
13500 * comdat_key == 0 ? "-" : (char *) comdat_key,
13501 get_lto_kind (kind),
13502 get_lto_visibility (visibility),
13503 size,
13504 slot,
13505 get_lto_sym_type (sym_type),
13506 sec_kind);
13507 print_symbol (6, (const char *) sym_name);
13508 }
13509 else
13510 {
13511 error (_("Ran out of LTO symbol extension data\n"));
13512 ext_data = NULL;
13513 /* FIXME: return FAIL result ? */
13514 }
13515 }
13516 else
13517 {
13518 printf (" %10s %10s %11s %08" PRIx64 " %08" PRIx64 " _",
13519 * comdat_key == 0 ? "-" : (char *) comdat_key,
13520 get_lto_kind (kind),
13521 get_lto_visibility (visibility),
13522 size,
13523 slot);
13524 print_symbol (21, (const char *) sym_name);
13525 }
13526 putchar ('\n');
13527 }
13528
13529 if (ext_data != NULL && ext_data < ext_data_end)
13530 {
13531 error (_("Data remains in the LTO symbol extension table\n"));
13532 goto fail;
13533 }
13534
13535 free (alloced_data);
13536 free (ext_data_orig);
13537 free (ext_name);
13538 return true;
13539
13540 fail:
13541 error (_("Buffer overrun encountered whilst decoding LTO symbol table\n"));
13542 free (alloced_data);
13543 free (ext_data_orig);
13544 free (ext_name);
13545 return false;
13546 }
13547
13548 /* Display LTO symbol tables. */
13549
13550 static bool
13551 process_lto_symbol_tables (Filedata * filedata)
13552 {
13553 Elf_Internal_Shdr * section;
13554 unsigned int i;
13555 bool res = true;
13556
13557 if (!do_lto_syms)
13558 return true;
13559
13560 if (filedata->section_headers == NULL)
13561 return true;
13562
13563 for (i = 0, section = filedata->section_headers;
13564 i < filedata->file_header.e_shnum;
13565 i++, section++)
13566 if (section_name_valid (filedata, section)
13567 && startswith (section_name (filedata, section), ".gnu.lto_.symtab."))
13568 res &= display_lto_symtab (filedata, section);
13569
13570 return res;
13571 }
13572
13573 /* Dump the symbol table. */
13574
13575 static bool
13576 process_symbol_table (Filedata * filedata)
13577 {
13578 Elf_Internal_Shdr * section;
13579
13580 if (!do_syms && !do_dyn_syms && !do_histogram)
13581 return true;
13582
13583 if ((filedata->dynamic_info[DT_HASH] || filedata->dynamic_info_DT_GNU_HASH)
13584 && do_syms
13585 && do_using_dynamic
13586 && filedata->dynamic_strings != NULL
13587 && filedata->dynamic_symbols != NULL)
13588 {
13589 unsigned long si;
13590
13591 if (filedata->is_separate)
13592 {
13593 printf (ngettext ("\nIn linked file '%s' the dynamic symbol table contains %lu entry:\n",
13594 "\nIn linked file '%s' the dynamic symbol table contains %lu entries:\n",
13595 filedata->num_dynamic_syms),
13596 filedata->file_name,
13597 filedata->num_dynamic_syms);
13598 }
13599 else
13600 {
13601 printf (ngettext ("\nSymbol table for image contains %lu entry:\n",
13602 "\nSymbol table for image contains %lu entries:\n",
13603 filedata->num_dynamic_syms),
13604 filedata->num_dynamic_syms);
13605 }
13606 if (is_32bit_elf)
13607 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13608 else
13609 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13610
13611 for (si = 0; si < filedata->num_dynamic_syms; si++)
13612 print_dynamic_symbol (filedata, si, filedata->dynamic_symbols, NULL,
13613 filedata->dynamic_strings,
13614 filedata->dynamic_strings_length);
13615 }
13616 else if ((do_dyn_syms || (do_syms && !do_using_dynamic))
13617 && filedata->section_headers != NULL)
13618 {
13619 unsigned int i;
13620
13621 for (i = 0, section = filedata->section_headers;
13622 i < filedata->file_header.e_shnum;
13623 i++, section++)
13624 {
13625 char * strtab = NULL;
13626 unsigned long int strtab_size = 0;
13627 Elf_Internal_Sym * symtab;
13628 unsigned long si, num_syms;
13629
13630 if ((section->sh_type != SHT_SYMTAB
13631 && section->sh_type != SHT_DYNSYM)
13632 || (!do_syms
13633 && section->sh_type == SHT_SYMTAB))
13634 continue;
13635
13636 if (section->sh_entsize == 0)
13637 {
13638 printf (_("\nSymbol table '%s' has a sh_entsize of zero!\n"),
13639 printable_section_name (filedata, section));
13640 continue;
13641 }
13642
13643 num_syms = section->sh_size / section->sh_entsize;
13644
13645 if (filedata->is_separate)
13646 printf (ngettext ("\nIn linked file '%s' symbol section '%s' contains %lu entry:\n",
13647 "\nIn linked file '%s' symbol section '%s' contains %lu entries:\n",
13648 num_syms),
13649 filedata->file_name,
13650 printable_section_name (filedata, section),
13651 num_syms);
13652 else
13653 printf (ngettext ("\nSymbol table '%s' contains %lu entry:\n",
13654 "\nSymbol table '%s' contains %lu entries:\n",
13655 num_syms),
13656 printable_section_name (filedata, section),
13657 num_syms);
13658
13659 if (is_32bit_elf)
13660 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13661 else
13662 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13663
13664 symtab = get_elf_symbols (filedata, section, & num_syms);
13665 if (symtab == NULL)
13666 continue;
13667
13668 if (section->sh_link == filedata->file_header.e_shstrndx)
13669 {
13670 strtab = filedata->string_table;
13671 strtab_size = filedata->string_table_length;
13672 }
13673 else if (section->sh_link < filedata->file_header.e_shnum)
13674 {
13675 Elf_Internal_Shdr * string_sec;
13676
13677 string_sec = filedata->section_headers + section->sh_link;
13678
13679 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset,
13680 1, string_sec->sh_size,
13681 _("string table"));
13682 strtab_size = strtab != NULL ? string_sec->sh_size : 0;
13683 }
13684
13685 for (si = 0; si < num_syms; si++)
13686 print_dynamic_symbol (filedata, si, symtab, section,
13687 strtab, strtab_size);
13688
13689 free (symtab);
13690 if (strtab != filedata->string_table)
13691 free (strtab);
13692 }
13693 }
13694 else if (do_syms)
13695 printf
13696 (_("\nDynamic symbol information is not available for displaying symbols.\n"));
13697
13698 if (do_histogram && filedata->buckets != NULL)
13699 {
13700 unsigned long * lengths;
13701 unsigned long * counts;
13702 unsigned long hn;
13703 uint64_t si;
13704 unsigned long maxlength = 0;
13705 unsigned long nzero_counts = 0;
13706 unsigned long nsyms = 0;
13707 char *visited;
13708
13709 printf (ngettext ("\nHistogram for bucket list length "
13710 "(total of %lu bucket):\n",
13711 "\nHistogram for bucket list length "
13712 "(total of %lu buckets):\n",
13713 (unsigned long) filedata->nbuckets),
13714 (unsigned long) filedata->nbuckets);
13715
13716 lengths = (unsigned long *) calloc (filedata->nbuckets,
13717 sizeof (*lengths));
13718 if (lengths == NULL)
13719 {
13720 error (_("Out of memory allocating space for histogram buckets\n"));
13721 goto err_out;
13722 }
13723 visited = xcmalloc (filedata->nchains, 1);
13724 memset (visited, 0, filedata->nchains);
13725
13726 printf (_(" Length Number %% of total Coverage\n"));
13727 for (hn = 0; hn < filedata->nbuckets; ++hn)
13728 {
13729 for (si = filedata->buckets[hn]; si > 0; si = filedata->chains[si])
13730 {
13731 ++nsyms;
13732 if (maxlength < ++lengths[hn])
13733 ++maxlength;
13734 if (si >= filedata->nchains || visited[si])
13735 {
13736 error (_("histogram chain is corrupt\n"));
13737 break;
13738 }
13739 visited[si] = 1;
13740 }
13741 }
13742 free (visited);
13743
13744 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
13745 if (counts == NULL)
13746 {
13747 free (lengths);
13748 error (_("Out of memory allocating space for histogram counts\n"));
13749 goto err_out;
13750 }
13751
13752 for (hn = 0; hn < filedata->nbuckets; ++hn)
13753 ++counts[lengths[hn]];
13754
13755 if (filedata->nbuckets > 0)
13756 {
13757 unsigned long i;
13758 printf (" 0 %-10lu (%5.1f%%)\n",
13759 counts[0], (counts[0] * 100.0) / filedata->nbuckets);
13760 for (i = 1; i <= maxlength; ++i)
13761 {
13762 nzero_counts += counts[i] * i;
13763 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
13764 i, counts[i], (counts[i] * 100.0) / filedata->nbuckets,
13765 (nzero_counts * 100.0) / nsyms);
13766 }
13767 }
13768
13769 free (counts);
13770 free (lengths);
13771 }
13772
13773 free (filedata->buckets);
13774 filedata->buckets = NULL;
13775 filedata->nbuckets = 0;
13776 free (filedata->chains);
13777 filedata->chains = NULL;
13778
13779 if (do_histogram && filedata->gnubuckets != NULL)
13780 {
13781 unsigned long * lengths;
13782 unsigned long * counts;
13783 unsigned long hn;
13784 unsigned long maxlength = 0;
13785 unsigned long nzero_counts = 0;
13786 unsigned long nsyms = 0;
13787
13788 printf (ngettext ("\nHistogram for `%s' bucket list length "
13789 "(total of %lu bucket):\n",
13790 "\nHistogram for `%s' bucket list length "
13791 "(total of %lu buckets):\n",
13792 (unsigned long) filedata->ngnubuckets),
13793 GNU_HASH_SECTION_NAME (filedata),
13794 (unsigned long) filedata->ngnubuckets);
13795
13796 lengths = (unsigned long *) calloc (filedata->ngnubuckets,
13797 sizeof (*lengths));
13798 if (lengths == NULL)
13799 {
13800 error (_("Out of memory allocating space for gnu histogram buckets\n"));
13801 goto err_out;
13802 }
13803
13804 printf (_(" Length Number %% of total Coverage\n"));
13805
13806 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
13807 if (filedata->gnubuckets[hn] != 0)
13808 {
13809 uint64_t off, length = 1;
13810
13811 for (off = filedata->gnubuckets[hn] - filedata->gnusymidx;
13812 /* PR 17531 file: 010-77222-0.004. */
13813 off < filedata->ngnuchains
13814 && (filedata->gnuchains[off] & 1) == 0;
13815 ++off)
13816 ++length;
13817 lengths[hn] = length;
13818 if (length > maxlength)
13819 maxlength = length;
13820 nsyms += length;
13821 }
13822
13823 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
13824 if (counts == NULL)
13825 {
13826 free (lengths);
13827 error (_("Out of memory allocating space for gnu histogram counts\n"));
13828 goto err_out;
13829 }
13830
13831 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
13832 ++counts[lengths[hn]];
13833
13834 if (filedata->ngnubuckets > 0)
13835 {
13836 unsigned long j;
13837 printf (" 0 %-10lu (%5.1f%%)\n",
13838 counts[0], (counts[0] * 100.0) / filedata->ngnubuckets);
13839 for (j = 1; j <= maxlength; ++j)
13840 {
13841 nzero_counts += counts[j] * j;
13842 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
13843 j, counts[j], (counts[j] * 100.0) / filedata->ngnubuckets,
13844 (nzero_counts * 100.0) / nsyms);
13845 }
13846 }
13847
13848 free (counts);
13849 free (lengths);
13850 }
13851 free (filedata->gnubuckets);
13852 filedata->gnubuckets = NULL;
13853 filedata->ngnubuckets = 0;
13854 free (filedata->gnuchains);
13855 filedata->gnuchains = NULL;
13856 filedata->ngnuchains = 0;
13857 free (filedata->mipsxlat);
13858 filedata->mipsxlat = NULL;
13859 return true;
13860
13861 err_out:
13862 free (filedata->gnubuckets);
13863 filedata->gnubuckets = NULL;
13864 filedata->ngnubuckets = 0;
13865 free (filedata->gnuchains);
13866 filedata->gnuchains = NULL;
13867 filedata->ngnuchains = 0;
13868 free (filedata->mipsxlat);
13869 filedata->mipsxlat = NULL;
13870 free (filedata->buckets);
13871 filedata->buckets = NULL;
13872 filedata->nbuckets = 0;
13873 free (filedata->chains);
13874 filedata->chains = NULL;
13875 return false;
13876 }
13877
13878 static bool
13879 process_syminfo (Filedata * filedata)
13880 {
13881 unsigned int i;
13882
13883 if (filedata->dynamic_syminfo == NULL
13884 || !do_dynamic)
13885 /* No syminfo, this is ok. */
13886 return true;
13887
13888 /* There better should be a dynamic symbol section. */
13889 if (filedata->dynamic_symbols == NULL || filedata->dynamic_strings == NULL)
13890 return false;
13891
13892 if (filedata->is_separate)
13893 printf (ngettext ("\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entry:\n",
13894 "\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entries:\n",
13895 filedata->dynamic_syminfo_nent),
13896 filedata->file_name,
13897 filedata->dynamic_syminfo_offset,
13898 filedata->dynamic_syminfo_nent);
13899 else
13900 printf (ngettext ("\nDynamic info segment at offset 0x%lx "
13901 "contains %d entry:\n",
13902 "\nDynamic info segment at offset 0x%lx "
13903 "contains %d entries:\n",
13904 filedata->dynamic_syminfo_nent),
13905 filedata->dynamic_syminfo_offset,
13906 filedata->dynamic_syminfo_nent);
13907
13908 printf (_(" Num: Name BoundTo Flags\n"));
13909 for (i = 0; i < filedata->dynamic_syminfo_nent; ++i)
13910 {
13911 unsigned short int flags = filedata->dynamic_syminfo[i].si_flags;
13912
13913 printf ("%4d: ", i);
13914 if (i >= filedata->num_dynamic_syms)
13915 printf (_("<corrupt index>"));
13916 else if (valid_dynamic_name (filedata, filedata->dynamic_symbols[i].st_name))
13917 print_symbol (30, get_dynamic_name (filedata,
13918 filedata->dynamic_symbols[i].st_name));
13919 else
13920 printf (_("<corrupt: %19ld>"), filedata->dynamic_symbols[i].st_name);
13921 putchar (' ');
13922
13923 switch (filedata->dynamic_syminfo[i].si_boundto)
13924 {
13925 case SYMINFO_BT_SELF:
13926 fputs ("SELF ", stdout);
13927 break;
13928 case SYMINFO_BT_PARENT:
13929 fputs ("PARENT ", stdout);
13930 break;
13931 default:
13932 if (filedata->dynamic_syminfo[i].si_boundto > 0
13933 && filedata->dynamic_syminfo[i].si_boundto < filedata->dynamic_nent
13934 && valid_dynamic_name (filedata,
13935 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val))
13936 {
13937 print_symbol (10, get_dynamic_name (filedata,
13938 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val));
13939 putchar (' ' );
13940 }
13941 else
13942 printf ("%-10d ", filedata->dynamic_syminfo[i].si_boundto);
13943 break;
13944 }
13945
13946 if (flags & SYMINFO_FLG_DIRECT)
13947 printf (" DIRECT");
13948 if (flags & SYMINFO_FLG_PASSTHRU)
13949 printf (" PASSTHRU");
13950 if (flags & SYMINFO_FLG_COPY)
13951 printf (" COPY");
13952 if (flags & SYMINFO_FLG_LAZYLOAD)
13953 printf (" LAZYLOAD");
13954
13955 puts ("");
13956 }
13957
13958 return true;
13959 }
13960
13961 /* A macro which evaluates to TRUE if the region ADDR .. ADDR + NELEM
13962 is contained by the region START .. END. The types of ADDR, START
13963 and END should all be the same. Note both ADDR + NELEM and END
13964 point to just beyond the end of the regions that are being tested. */
13965 #define IN_RANGE(START,END,ADDR,NELEM) \
13966 (((ADDR) >= (START)) && ((ADDR) < (END)) && ((ADDR) + (NELEM) <= (END)))
13967
13968 /* Check to see if the given reloc needs to be handled in a target specific
13969 manner. If so then process the reloc and return TRUE otherwise return
13970 FALSE.
13971
13972 If called with reloc == NULL, then this is a signal that reloc processing
13973 for the current section has finished, and any saved state should be
13974 discarded. */
13975
13976 static bool
13977 target_specific_reloc_handling (Filedata * filedata,
13978 Elf_Internal_Rela * reloc,
13979 unsigned char * start,
13980 unsigned char * end,
13981 Elf_Internal_Sym * symtab,
13982 unsigned long num_syms)
13983 {
13984 unsigned int reloc_type = 0;
13985 unsigned long sym_index = 0;
13986
13987 if (reloc)
13988 {
13989 reloc_type = get_reloc_type (filedata, reloc->r_info);
13990 sym_index = get_reloc_symindex (reloc->r_info);
13991 }
13992
13993 switch (filedata->file_header.e_machine)
13994 {
13995 case EM_MSP430:
13996 case EM_MSP430_OLD:
13997 {
13998 static Elf_Internal_Sym * saved_sym = NULL;
13999
14000 if (reloc == NULL)
14001 {
14002 saved_sym = NULL;
14003 return true;
14004 }
14005
14006 switch (reloc_type)
14007 {
14008 case 10: /* R_MSP430_SYM_DIFF */
14009 case 12: /* R_MSP430_GNU_SUB_ULEB128 */
14010 if (uses_msp430x_relocs (filedata))
14011 break;
14012 /* Fall through. */
14013 case 21: /* R_MSP430X_SYM_DIFF */
14014 case 23: /* R_MSP430X_GNU_SUB_ULEB128 */
14015 /* PR 21139. */
14016 if (sym_index >= num_syms)
14017 error (_("MSP430 SYM_DIFF reloc contains invalid symbol index %lu\n"),
14018 sym_index);
14019 else
14020 saved_sym = symtab + sym_index;
14021 return true;
14022
14023 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
14024 case 3: /* R_MSP430_16 or R_MSP430_ABS8 */
14025 goto handle_sym_diff;
14026
14027 case 5: /* R_MSP430_16_BYTE */
14028 case 9: /* R_MSP430_8 */
14029 case 11: /* R_MSP430_GNU_SET_ULEB128 */
14030 if (uses_msp430x_relocs (filedata))
14031 break;
14032 goto handle_sym_diff;
14033
14034 case 2: /* R_MSP430_ABS16 */
14035 case 15: /* R_MSP430X_ABS16 */
14036 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
14037 if (! uses_msp430x_relocs (filedata))
14038 break;
14039 goto handle_sym_diff;
14040
14041 handle_sym_diff:
14042 if (saved_sym != NULL)
14043 {
14044 uint64_t value;
14045 unsigned int reloc_size = 0;
14046 int leb_ret = 0;
14047 switch (reloc_type)
14048 {
14049 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
14050 reloc_size = 4;
14051 break;
14052 case 11: /* R_MSP430_GNU_SET_ULEB128 */
14053 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
14054 if (reloc->r_offset < (size_t) (end - start))
14055 read_leb128 (start + reloc->r_offset, end, false,
14056 &reloc_size, &leb_ret);
14057 break;
14058 default:
14059 reloc_size = 2;
14060 break;
14061 }
14062
14063 if (leb_ret != 0 || reloc_size == 0 || reloc_size > 8)
14064 error (_("MSP430 ULEB128 field at 0x%lx contains invalid "
14065 "ULEB128 value\n"),
14066 (long) reloc->r_offset);
14067 else if (sym_index >= num_syms)
14068 error (_("MSP430 reloc contains invalid symbol index %lu\n"),
14069 sym_index);
14070 else
14071 {
14072 value = reloc->r_addend + (symtab[sym_index].st_value
14073 - saved_sym->st_value);
14074
14075 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
14076 byte_put (start + reloc->r_offset, value, reloc_size);
14077 else
14078 /* PR 21137 */
14079 error (_("MSP430 sym diff reloc contains invalid offset: 0x%lx\n"),
14080 (long) reloc->r_offset);
14081 }
14082
14083 saved_sym = NULL;
14084 return true;
14085 }
14086 break;
14087
14088 default:
14089 if (saved_sym != NULL)
14090 error (_("Unhandled MSP430 reloc type found after SYM_DIFF reloc\n"));
14091 break;
14092 }
14093 break;
14094 }
14095
14096 case EM_MN10300:
14097 case EM_CYGNUS_MN10300:
14098 {
14099 static Elf_Internal_Sym * saved_sym = NULL;
14100
14101 if (reloc == NULL)
14102 {
14103 saved_sym = NULL;
14104 return true;
14105 }
14106
14107 switch (reloc_type)
14108 {
14109 case 34: /* R_MN10300_ALIGN */
14110 return true;
14111 case 33: /* R_MN10300_SYM_DIFF */
14112 if (sym_index >= num_syms)
14113 error (_("MN10300_SYM_DIFF reloc contains invalid symbol index %lu\n"),
14114 sym_index);
14115 else
14116 saved_sym = symtab + sym_index;
14117 return true;
14118
14119 case 1: /* R_MN10300_32 */
14120 case 2: /* R_MN10300_16 */
14121 if (saved_sym != NULL)
14122 {
14123 int reloc_size = reloc_type == 1 ? 4 : 2;
14124 uint64_t value;
14125
14126 if (sym_index >= num_syms)
14127 error (_("MN10300 reloc contains invalid symbol index %lu\n"),
14128 sym_index);
14129 else
14130 {
14131 value = reloc->r_addend + (symtab[sym_index].st_value
14132 - saved_sym->st_value);
14133
14134 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
14135 byte_put (start + reloc->r_offset, value, reloc_size);
14136 else
14137 error (_("MN10300 sym diff reloc contains invalid offset: 0x%lx\n"),
14138 (long) reloc->r_offset);
14139 }
14140
14141 saved_sym = NULL;
14142 return true;
14143 }
14144 break;
14145 default:
14146 if (saved_sym != NULL)
14147 error (_("Unhandled MN10300 reloc type found after SYM_DIFF reloc\n"));
14148 break;
14149 }
14150 break;
14151 }
14152
14153 case EM_RL78:
14154 {
14155 static uint64_t saved_sym1 = 0;
14156 static uint64_t saved_sym2 = 0;
14157 static uint64_t value;
14158
14159 if (reloc == NULL)
14160 {
14161 saved_sym1 = saved_sym2 = 0;
14162 return true;
14163 }
14164
14165 switch (reloc_type)
14166 {
14167 case 0x80: /* R_RL78_SYM. */
14168 saved_sym1 = saved_sym2;
14169 if (sym_index >= num_syms)
14170 error (_("RL78_SYM reloc contains invalid symbol index %lu\n"),
14171 sym_index);
14172 else
14173 {
14174 saved_sym2 = symtab[sym_index].st_value;
14175 saved_sym2 += reloc->r_addend;
14176 }
14177 return true;
14178
14179 case 0x83: /* R_RL78_OPsub. */
14180 value = saved_sym1 - saved_sym2;
14181 saved_sym2 = saved_sym1 = 0;
14182 return true;
14183 break;
14184
14185 case 0x41: /* R_RL78_ABS32. */
14186 if (IN_RANGE (start, end, start + reloc->r_offset, 4))
14187 byte_put (start + reloc->r_offset, value, 4);
14188 else
14189 error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
14190 (long) reloc->r_offset);
14191 value = 0;
14192 return true;
14193
14194 case 0x43: /* R_RL78_ABS16. */
14195 if (IN_RANGE (start, end, start + reloc->r_offset, 2))
14196 byte_put (start + reloc->r_offset, value, 2);
14197 else
14198 error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
14199 (long) reloc->r_offset);
14200 value = 0;
14201 return true;
14202
14203 default:
14204 break;
14205 }
14206 break;
14207 }
14208 }
14209
14210 return false;
14211 }
14212
14213 /* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
14214 DWARF debug sections. This is a target specific test. Note - we do not
14215 go through the whole including-target-headers-multiple-times route, (as
14216 we have already done with <elf/h8.h>) because this would become very
14217 messy and even then this function would have to contain target specific
14218 information (the names of the relocs instead of their numeric values).
14219 FIXME: This is not the correct way to solve this problem. The proper way
14220 is to have target specific reloc sizing and typing functions created by
14221 the reloc-macros.h header, in the same way that it already creates the
14222 reloc naming functions. */
14223
14224 static bool
14225 is_32bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14226 {
14227 /* Please keep this table alpha-sorted for ease of visual lookup. */
14228 switch (filedata->file_header.e_machine)
14229 {
14230 case EM_386:
14231 case EM_IAMCU:
14232 return reloc_type == 1; /* R_386_32. */
14233 case EM_68K:
14234 return reloc_type == 1; /* R_68K_32. */
14235 case EM_860:
14236 return reloc_type == 1; /* R_860_32. */
14237 case EM_960:
14238 return reloc_type == 2; /* R_960_32. */
14239 case EM_AARCH64:
14240 return (reloc_type == 258
14241 || reloc_type == 1); /* R_AARCH64_ABS32 || R_AARCH64_P32_ABS32 */
14242 case EM_BPF:
14243 return reloc_type == 11; /* R_BPF_DATA_32 */
14244 case EM_ADAPTEVA_EPIPHANY:
14245 return reloc_type == 3;
14246 case EM_ALPHA:
14247 return reloc_type == 1; /* R_ALPHA_REFLONG. */
14248 case EM_ARC:
14249 return reloc_type == 1; /* R_ARC_32. */
14250 case EM_ARC_COMPACT:
14251 case EM_ARC_COMPACT2:
14252 return reloc_type == 4; /* R_ARC_32. */
14253 case EM_ARM:
14254 return reloc_type == 2; /* R_ARM_ABS32 */
14255 case EM_AVR_OLD:
14256 case EM_AVR:
14257 return reloc_type == 1;
14258 case EM_BLACKFIN:
14259 return reloc_type == 0x12; /* R_byte4_data. */
14260 case EM_CRIS:
14261 return reloc_type == 3; /* R_CRIS_32. */
14262 case EM_CR16:
14263 return reloc_type == 3; /* R_CR16_NUM32. */
14264 case EM_CRX:
14265 return reloc_type == 15; /* R_CRX_NUM32. */
14266 case EM_CSKY:
14267 return reloc_type == 1; /* R_CKCORE_ADDR32. */
14268 case EM_CYGNUS_FRV:
14269 return reloc_type == 1;
14270 case EM_CYGNUS_D10V:
14271 case EM_D10V:
14272 return reloc_type == 6; /* R_D10V_32. */
14273 case EM_CYGNUS_D30V:
14274 case EM_D30V:
14275 return reloc_type == 12; /* R_D30V_32_NORMAL. */
14276 case EM_DLX:
14277 return reloc_type == 3; /* R_DLX_RELOC_32. */
14278 case EM_CYGNUS_FR30:
14279 case EM_FR30:
14280 return reloc_type == 3; /* R_FR30_32. */
14281 case EM_FT32:
14282 return reloc_type == 1; /* R_FT32_32. */
14283 case EM_H8S:
14284 case EM_H8_300:
14285 case EM_H8_300H:
14286 return reloc_type == 1; /* R_H8_DIR32. */
14287 case EM_IA_64:
14288 return (reloc_type == 0x64 /* R_IA64_SECREL32MSB. */
14289 || reloc_type == 0x65 /* R_IA64_SECREL32LSB. */
14290 || reloc_type == 0x24 /* R_IA64_DIR32MSB. */
14291 || reloc_type == 0x25 /* R_IA64_DIR32LSB. */);
14292 case EM_IP2K_OLD:
14293 case EM_IP2K:
14294 return reloc_type == 2; /* R_IP2K_32. */
14295 case EM_IQ2000:
14296 return reloc_type == 2; /* R_IQ2000_32. */
14297 case EM_LATTICEMICO32:
14298 return reloc_type == 3; /* R_LM32_32. */
14299 case EM_LOONGARCH:
14300 return reloc_type == 1; /* R_LARCH_32. */
14301 case EM_M32C_OLD:
14302 case EM_M32C:
14303 return reloc_type == 3; /* R_M32C_32. */
14304 case EM_M32R:
14305 return reloc_type == 34; /* R_M32R_32_RELA. */
14306 case EM_68HC11:
14307 case EM_68HC12:
14308 return reloc_type == 6; /* R_M68HC11_32. */
14309 case EM_S12Z:
14310 return reloc_type == 7 || /* R_S12Z_EXT32 */
14311 reloc_type == 6; /* R_S12Z_CW32. */
14312 case EM_MCORE:
14313 return reloc_type == 1; /* R_MCORE_ADDR32. */
14314 case EM_CYGNUS_MEP:
14315 return reloc_type == 4; /* R_MEP_32. */
14316 case EM_METAG:
14317 return reloc_type == 2; /* R_METAG_ADDR32. */
14318 case EM_MICROBLAZE:
14319 return reloc_type == 1; /* R_MICROBLAZE_32. */
14320 case EM_MIPS:
14321 return reloc_type == 2; /* R_MIPS_32. */
14322 case EM_MMIX:
14323 return reloc_type == 4; /* R_MMIX_32. */
14324 case EM_CYGNUS_MN10200:
14325 case EM_MN10200:
14326 return reloc_type == 1; /* R_MN10200_32. */
14327 case EM_CYGNUS_MN10300:
14328 case EM_MN10300:
14329 return reloc_type == 1; /* R_MN10300_32. */
14330 case EM_MOXIE:
14331 return reloc_type == 1; /* R_MOXIE_32. */
14332 case EM_MSP430_OLD:
14333 case EM_MSP430:
14334 return reloc_type == 1; /* R_MSP430_32 or R_MSP320_ABS32. */
14335 case EM_MT:
14336 return reloc_type == 2; /* R_MT_32. */
14337 case EM_NDS32:
14338 return reloc_type == 20; /* R_NDS32_32_RELA. */
14339 case EM_ALTERA_NIOS2:
14340 return reloc_type == 12; /* R_NIOS2_BFD_RELOC_32. */
14341 case EM_NIOS32:
14342 return reloc_type == 1; /* R_NIOS_32. */
14343 case EM_OR1K:
14344 return reloc_type == 1; /* R_OR1K_32. */
14345 case EM_PARISC:
14346 return (reloc_type == 1 /* R_PARISC_DIR32. */
14347 || reloc_type == 2 /* R_PARISC_DIR21L. */
14348 || reloc_type == 41); /* R_PARISC_SECREL32. */
14349 case EM_PJ:
14350 case EM_PJ_OLD:
14351 return reloc_type == 1; /* R_PJ_DATA_DIR32. */
14352 case EM_PPC64:
14353 return reloc_type == 1; /* R_PPC64_ADDR32. */
14354 case EM_PPC:
14355 return reloc_type == 1; /* R_PPC_ADDR32. */
14356 case EM_TI_PRU:
14357 return reloc_type == 11; /* R_PRU_BFD_RELOC_32. */
14358 case EM_RISCV:
14359 return reloc_type == 1; /* R_RISCV_32. */
14360 case EM_RL78:
14361 return reloc_type == 1; /* R_RL78_DIR32. */
14362 case EM_RX:
14363 return reloc_type == 1; /* R_RX_DIR32. */
14364 case EM_S370:
14365 return reloc_type == 1; /* R_I370_ADDR31. */
14366 case EM_S390_OLD:
14367 case EM_S390:
14368 return reloc_type == 4; /* R_S390_32. */
14369 case EM_SCORE:
14370 return reloc_type == 8; /* R_SCORE_ABS32. */
14371 case EM_SH:
14372 return reloc_type == 1; /* R_SH_DIR32. */
14373 case EM_SPARC32PLUS:
14374 case EM_SPARCV9:
14375 case EM_SPARC:
14376 return reloc_type == 3 /* R_SPARC_32. */
14377 || reloc_type == 23; /* R_SPARC_UA32. */
14378 case EM_SPU:
14379 return reloc_type == 6; /* R_SPU_ADDR32 */
14380 case EM_TI_C6000:
14381 return reloc_type == 1; /* R_C6000_ABS32. */
14382 case EM_TILEGX:
14383 return reloc_type == 2; /* R_TILEGX_32. */
14384 case EM_TILEPRO:
14385 return reloc_type == 1; /* R_TILEPRO_32. */
14386 case EM_CYGNUS_V850:
14387 case EM_V850:
14388 return reloc_type == 6; /* R_V850_ABS32. */
14389 case EM_V800:
14390 return reloc_type == 0x33; /* R_V810_WORD. */
14391 case EM_VAX:
14392 return reloc_type == 1; /* R_VAX_32. */
14393 case EM_VISIUM:
14394 return reloc_type == 3; /* R_VISIUM_32. */
14395 case EM_WEBASSEMBLY:
14396 return reloc_type == 1; /* R_WASM32_32. */
14397 case EM_X86_64:
14398 case EM_L1OM:
14399 case EM_K1OM:
14400 return reloc_type == 10; /* R_X86_64_32. */
14401 case EM_XGATE:
14402 return reloc_type == 4; /* R_XGATE_32. */
14403 case EM_XSTORMY16:
14404 return reloc_type == 1; /* R_XSTROMY16_32. */
14405 case EM_XTENSA_OLD:
14406 case EM_XTENSA:
14407 return reloc_type == 1; /* R_XTENSA_32. */
14408 case EM_Z80:
14409 return reloc_type == 6; /* R_Z80_32. */
14410 default:
14411 {
14412 static unsigned int prev_warn = 0;
14413
14414 /* Avoid repeating the same warning multiple times. */
14415 if (prev_warn != filedata->file_header.e_machine)
14416 error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
14417 filedata->file_header.e_machine);
14418 prev_warn = filedata->file_header.e_machine;
14419 return false;
14420 }
14421 }
14422 }
14423
14424 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14425 a 32-bit pc-relative RELA relocation used in DWARF debug sections. */
14426
14427 static bool
14428 is_32bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
14429 {
14430 switch (filedata->file_header.e_machine)
14431 /* Please keep this table alpha-sorted for ease of visual lookup. */
14432 {
14433 case EM_386:
14434 case EM_IAMCU:
14435 return reloc_type == 2; /* R_386_PC32. */
14436 case EM_68K:
14437 return reloc_type == 4; /* R_68K_PC32. */
14438 case EM_AARCH64:
14439 return reloc_type == 261; /* R_AARCH64_PREL32 */
14440 case EM_ADAPTEVA_EPIPHANY:
14441 return reloc_type == 6;
14442 case EM_ALPHA:
14443 return reloc_type == 10; /* R_ALPHA_SREL32. */
14444 case EM_ARC_COMPACT:
14445 case EM_ARC_COMPACT2:
14446 return reloc_type == 49; /* R_ARC_32_PCREL. */
14447 case EM_ARM:
14448 return reloc_type == 3; /* R_ARM_REL32 */
14449 case EM_AVR_OLD:
14450 case EM_AVR:
14451 return reloc_type == 36; /* R_AVR_32_PCREL. */
14452 case EM_LOONGARCH:
14453 return reloc_type == 99; /* R_LARCH_32_PCREL. */
14454 case EM_MICROBLAZE:
14455 return reloc_type == 2; /* R_MICROBLAZE_32_PCREL. */
14456 case EM_OR1K:
14457 return reloc_type == 9; /* R_OR1K_32_PCREL. */
14458 case EM_PARISC:
14459 return reloc_type == 9; /* R_PARISC_PCREL32. */
14460 case EM_PPC:
14461 return reloc_type == 26; /* R_PPC_REL32. */
14462 case EM_PPC64:
14463 return reloc_type == 26; /* R_PPC64_REL32. */
14464 case EM_RISCV:
14465 return reloc_type == 57; /* R_RISCV_32_PCREL. */
14466 case EM_S390_OLD:
14467 case EM_S390:
14468 return reloc_type == 5; /* R_390_PC32. */
14469 case EM_SH:
14470 return reloc_type == 2; /* R_SH_REL32. */
14471 case EM_SPARC32PLUS:
14472 case EM_SPARCV9:
14473 case EM_SPARC:
14474 return reloc_type == 6; /* R_SPARC_DISP32. */
14475 case EM_SPU:
14476 return reloc_type == 13; /* R_SPU_REL32. */
14477 case EM_TILEGX:
14478 return reloc_type == 6; /* R_TILEGX_32_PCREL. */
14479 case EM_TILEPRO:
14480 return reloc_type == 4; /* R_TILEPRO_32_PCREL. */
14481 case EM_VISIUM:
14482 return reloc_type == 6; /* R_VISIUM_32_PCREL */
14483 case EM_X86_64:
14484 case EM_L1OM:
14485 case EM_K1OM:
14486 return reloc_type == 2; /* R_X86_64_PC32. */
14487 case EM_VAX:
14488 return reloc_type == 4; /* R_VAX_PCREL32. */
14489 case EM_XTENSA_OLD:
14490 case EM_XTENSA:
14491 return reloc_type == 14; /* R_XTENSA_32_PCREL. */
14492 default:
14493 /* Do not abort or issue an error message here. Not all targets use
14494 pc-relative 32-bit relocs in their DWARF debug information and we
14495 have already tested for target coverage in is_32bit_abs_reloc. A
14496 more helpful warning message will be generated by apply_relocations
14497 anyway, so just return. */
14498 return false;
14499 }
14500 }
14501
14502 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14503 a 64-bit absolute RELA relocation used in DWARF debug sections. */
14504
14505 static bool
14506 is_64bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14507 {
14508 switch (filedata->file_header.e_machine)
14509 {
14510 case EM_AARCH64:
14511 return reloc_type == 257; /* R_AARCH64_ABS64. */
14512 case EM_ALPHA:
14513 return reloc_type == 2; /* R_ALPHA_REFQUAD. */
14514 case EM_IA_64:
14515 return (reloc_type == 0x26 /* R_IA64_DIR64MSB. */
14516 || reloc_type == 0x27 /* R_IA64_DIR64LSB. */);
14517 case EM_LOONGARCH:
14518 return reloc_type == 2; /* R_LARCH_64 */
14519 case EM_PARISC:
14520 return reloc_type == 80; /* R_PARISC_DIR64. */
14521 case EM_PPC64:
14522 return reloc_type == 38; /* R_PPC64_ADDR64. */
14523 case EM_RISCV:
14524 return reloc_type == 2; /* R_RISCV_64. */
14525 case EM_SPARC32PLUS:
14526 case EM_SPARCV9:
14527 case EM_SPARC:
14528 return reloc_type == 32 /* R_SPARC_64. */
14529 || reloc_type == 54; /* R_SPARC_UA64. */
14530 case EM_X86_64:
14531 case EM_L1OM:
14532 case EM_K1OM:
14533 return reloc_type == 1; /* R_X86_64_64. */
14534 case EM_S390_OLD:
14535 case EM_S390:
14536 return reloc_type == 22; /* R_S390_64. */
14537 case EM_TILEGX:
14538 return reloc_type == 1; /* R_TILEGX_64. */
14539 case EM_MIPS:
14540 return reloc_type == 18; /* R_MIPS_64. */
14541 default:
14542 return false;
14543 }
14544 }
14545
14546 /* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
14547 a 64-bit pc-relative RELA relocation used in DWARF debug sections. */
14548
14549 static bool
14550 is_64bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
14551 {
14552 switch (filedata->file_header.e_machine)
14553 {
14554 case EM_AARCH64:
14555 return reloc_type == 260; /* R_AARCH64_PREL64. */
14556 case EM_ALPHA:
14557 return reloc_type == 11; /* R_ALPHA_SREL64. */
14558 case EM_IA_64:
14559 return (reloc_type == 0x4e /* R_IA64_PCREL64MSB. */
14560 || reloc_type == 0x4f /* R_IA64_PCREL64LSB. */);
14561 case EM_PARISC:
14562 return reloc_type == 72; /* R_PARISC_PCREL64. */
14563 case EM_PPC64:
14564 return reloc_type == 44; /* R_PPC64_REL64. */
14565 case EM_SPARC32PLUS:
14566 case EM_SPARCV9:
14567 case EM_SPARC:
14568 return reloc_type == 46; /* R_SPARC_DISP64. */
14569 case EM_X86_64:
14570 case EM_L1OM:
14571 case EM_K1OM:
14572 return reloc_type == 24; /* R_X86_64_PC64. */
14573 case EM_S390_OLD:
14574 case EM_S390:
14575 return reloc_type == 23; /* R_S390_PC64. */
14576 case EM_TILEGX:
14577 return reloc_type == 5; /* R_TILEGX_64_PCREL. */
14578 default:
14579 return false;
14580 }
14581 }
14582
14583 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14584 a 24-bit absolute RELA relocation used in DWARF debug sections. */
14585
14586 static bool
14587 is_24bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14588 {
14589 switch (filedata->file_header.e_machine)
14590 {
14591 case EM_CYGNUS_MN10200:
14592 case EM_MN10200:
14593 return reloc_type == 4; /* R_MN10200_24. */
14594 case EM_FT32:
14595 return reloc_type == 5; /* R_FT32_20. */
14596 case EM_Z80:
14597 return reloc_type == 5; /* R_Z80_24. */
14598 default:
14599 return false;
14600 }
14601 }
14602
14603 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14604 a 16-bit absolute RELA relocation used in DWARF debug sections. */
14605
14606 static bool
14607 is_16bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14608 {
14609 /* Please keep this table alpha-sorted for ease of visual lookup. */
14610 switch (filedata->file_header.e_machine)
14611 {
14612 case EM_ARC:
14613 case EM_ARC_COMPACT:
14614 case EM_ARC_COMPACT2:
14615 return reloc_type == 2; /* R_ARC_16. */
14616 case EM_ADAPTEVA_EPIPHANY:
14617 return reloc_type == 5;
14618 case EM_AVR_OLD:
14619 case EM_AVR:
14620 return reloc_type == 4; /* R_AVR_16. */
14621 case EM_CYGNUS_D10V:
14622 case EM_D10V:
14623 return reloc_type == 3; /* R_D10V_16. */
14624 case EM_FT32:
14625 return reloc_type == 2; /* R_FT32_16. */
14626 case EM_H8S:
14627 case EM_H8_300:
14628 case EM_H8_300H:
14629 return reloc_type == R_H8_DIR16;
14630 case EM_IP2K_OLD:
14631 case EM_IP2K:
14632 return reloc_type == 1; /* R_IP2K_16. */
14633 case EM_M32C_OLD:
14634 case EM_M32C:
14635 return reloc_type == 1; /* R_M32C_16 */
14636 case EM_CYGNUS_MN10200:
14637 case EM_MN10200:
14638 return reloc_type == 2; /* R_MN10200_16. */
14639 case EM_CYGNUS_MN10300:
14640 case EM_MN10300:
14641 return reloc_type == 2; /* R_MN10300_16. */
14642 case EM_MSP430:
14643 if (uses_msp430x_relocs (filedata))
14644 return reloc_type == 2; /* R_MSP430_ABS16. */
14645 /* Fall through. */
14646 case EM_MSP430_OLD:
14647 return reloc_type == 5; /* R_MSP430_16_BYTE. */
14648 case EM_NDS32:
14649 return reloc_type == 19; /* R_NDS32_16_RELA. */
14650 case EM_ALTERA_NIOS2:
14651 return reloc_type == 13; /* R_NIOS2_BFD_RELOC_16. */
14652 case EM_NIOS32:
14653 return reloc_type == 9; /* R_NIOS_16. */
14654 case EM_OR1K:
14655 return reloc_type == 2; /* R_OR1K_16. */
14656 case EM_RISCV:
14657 return reloc_type == 55; /* R_RISCV_SET16. */
14658 case EM_TI_PRU:
14659 return reloc_type == 8; /* R_PRU_BFD_RELOC_16. */
14660 case EM_TI_C6000:
14661 return reloc_type == 2; /* R_C6000_ABS16. */
14662 case EM_VISIUM:
14663 return reloc_type == 2; /* R_VISIUM_16. */
14664 case EM_XGATE:
14665 return reloc_type == 3; /* R_XGATE_16. */
14666 case EM_Z80:
14667 return reloc_type == 4; /* R_Z80_16. */
14668 default:
14669 return false;
14670 }
14671 }
14672
14673 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14674 a 8-bit absolute RELA relocation used in DWARF debug sections. */
14675
14676 static bool
14677 is_8bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14678 {
14679 switch (filedata->file_header.e_machine)
14680 {
14681 case EM_RISCV:
14682 return reloc_type == 54; /* R_RISCV_SET8. */
14683 case EM_Z80:
14684 return reloc_type == 1; /* R_Z80_8. */
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 6-bit absolute RELA relocation used in DWARF debug sections. */
14692
14693 static bool
14694 is_6bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14695 {
14696 switch (filedata->file_header.e_machine)
14697 {
14698 case EM_RISCV:
14699 return reloc_type == 53; /* R_RISCV_SET6. */
14700 default:
14701 return false;
14702 }
14703 }
14704
14705 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14706 a 32-bit inplace add RELA relocation used in DWARF debug sections. */
14707
14708 static bool
14709 is_32bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14710 {
14711 /* Please keep this table alpha-sorted for ease of visual lookup. */
14712 switch (filedata->file_header.e_machine)
14713 {
14714 case EM_RISCV:
14715 return reloc_type == 35; /* R_RISCV_ADD32. */
14716 default:
14717 return false;
14718 }
14719 }
14720
14721 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14722 a 32-bit inplace sub RELA relocation used in DWARF debug sections. */
14723
14724 static bool
14725 is_32bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14726 {
14727 /* Please keep this table alpha-sorted for ease of visual lookup. */
14728 switch (filedata->file_header.e_machine)
14729 {
14730 case EM_RISCV:
14731 return reloc_type == 39; /* R_RISCV_SUB32. */
14732 default:
14733 return false;
14734 }
14735 }
14736
14737 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14738 a 64-bit inplace add RELA relocation used in DWARF debug sections. */
14739
14740 static bool
14741 is_64bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14742 {
14743 /* Please keep this table alpha-sorted for ease of visual lookup. */
14744 switch (filedata->file_header.e_machine)
14745 {
14746 case EM_RISCV:
14747 return reloc_type == 36; /* R_RISCV_ADD64. */
14748 default:
14749 return false;
14750 }
14751 }
14752
14753 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14754 a 64-bit inplace sub RELA relocation used in DWARF debug sections. */
14755
14756 static bool
14757 is_64bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14758 {
14759 /* Please keep this table alpha-sorted for ease of visual lookup. */
14760 switch (filedata->file_header.e_machine)
14761 {
14762 case EM_RISCV:
14763 return reloc_type == 40; /* R_RISCV_SUB64. */
14764 default:
14765 return false;
14766 }
14767 }
14768
14769 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14770 a 16-bit inplace add RELA relocation used in DWARF debug sections. */
14771
14772 static bool
14773 is_16bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14774 {
14775 /* Please keep this table alpha-sorted for ease of visual lookup. */
14776 switch (filedata->file_header.e_machine)
14777 {
14778 case EM_RISCV:
14779 return reloc_type == 34; /* R_RISCV_ADD16. */
14780 default:
14781 return false;
14782 }
14783 }
14784
14785 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14786 a 16-bit inplace sub RELA relocation used in DWARF debug sections. */
14787
14788 static bool
14789 is_16bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14790 {
14791 /* Please keep this table alpha-sorted for ease of visual lookup. */
14792 switch (filedata->file_header.e_machine)
14793 {
14794 case EM_RISCV:
14795 return reloc_type == 38; /* R_RISCV_SUB16. */
14796 default:
14797 return false;
14798 }
14799 }
14800
14801 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14802 a 8-bit inplace add RELA relocation used in DWARF debug sections. */
14803
14804 static bool
14805 is_8bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14806 {
14807 /* Please keep this table alpha-sorted for ease of visual lookup. */
14808 switch (filedata->file_header.e_machine)
14809 {
14810 case EM_RISCV:
14811 return reloc_type == 33; /* R_RISCV_ADD8. */
14812 default:
14813 return false;
14814 }
14815 }
14816
14817 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14818 a 8-bit inplace sub RELA relocation used in DWARF debug sections. */
14819
14820 static bool
14821 is_8bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14822 {
14823 /* Please keep this table alpha-sorted for ease of visual lookup. */
14824 switch (filedata->file_header.e_machine)
14825 {
14826 case EM_RISCV:
14827 return reloc_type == 37; /* R_RISCV_SUB8. */
14828 default:
14829 return false;
14830 }
14831 }
14832
14833 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14834 a 6-bit inplace sub RELA relocation used in DWARF debug sections. */
14835
14836 static bool
14837 is_6bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14838 {
14839 switch (filedata->file_header.e_machine)
14840 {
14841 case EM_RISCV:
14842 return reloc_type == 52; /* R_RISCV_SUB6. */
14843 default:
14844 return false;
14845 }
14846 }
14847
14848 /* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
14849 relocation entries (possibly formerly used for SHT_GROUP sections). */
14850
14851 static bool
14852 is_none_reloc (Filedata * filedata, unsigned int reloc_type)
14853 {
14854 switch (filedata->file_header.e_machine)
14855 {
14856 case EM_386: /* R_386_NONE. */
14857 case EM_68K: /* R_68K_NONE. */
14858 case EM_ADAPTEVA_EPIPHANY:
14859 case EM_ALPHA: /* R_ALPHA_NONE. */
14860 case EM_ALTERA_NIOS2: /* R_NIOS2_NONE. */
14861 case EM_ARC: /* R_ARC_NONE. */
14862 case EM_ARC_COMPACT2: /* R_ARC_NONE. */
14863 case EM_ARC_COMPACT: /* R_ARC_NONE. */
14864 case EM_ARM: /* R_ARM_NONE. */
14865 case EM_CRIS: /* R_CRIS_NONE. */
14866 case EM_FT32: /* R_FT32_NONE. */
14867 case EM_IA_64: /* R_IA64_NONE. */
14868 case EM_K1OM: /* R_X86_64_NONE. */
14869 case EM_L1OM: /* R_X86_64_NONE. */
14870 case EM_M32R: /* R_M32R_NONE. */
14871 case EM_MIPS: /* R_MIPS_NONE. */
14872 case EM_MN10300: /* R_MN10300_NONE. */
14873 case EM_MOXIE: /* R_MOXIE_NONE. */
14874 case EM_NIOS32: /* R_NIOS_NONE. */
14875 case EM_OR1K: /* R_OR1K_NONE. */
14876 case EM_PARISC: /* R_PARISC_NONE. */
14877 case EM_PPC64: /* R_PPC64_NONE. */
14878 case EM_PPC: /* R_PPC_NONE. */
14879 case EM_RISCV: /* R_RISCV_NONE. */
14880 case EM_S390: /* R_390_NONE. */
14881 case EM_S390_OLD:
14882 case EM_SH: /* R_SH_NONE. */
14883 case EM_SPARC32PLUS:
14884 case EM_SPARC: /* R_SPARC_NONE. */
14885 case EM_SPARCV9:
14886 case EM_TILEGX: /* R_TILEGX_NONE. */
14887 case EM_TILEPRO: /* R_TILEPRO_NONE. */
14888 case EM_TI_C6000:/* R_C6000_NONE. */
14889 case EM_X86_64: /* R_X86_64_NONE. */
14890 case EM_Z80: /* R_Z80_NONE. */
14891 case EM_WEBASSEMBLY: /* R_WASM32_NONE. */
14892 return reloc_type == 0;
14893
14894 case EM_AARCH64:
14895 return reloc_type == 0 || reloc_type == 256;
14896 case EM_AVR_OLD:
14897 case EM_AVR:
14898 return (reloc_type == 0 /* R_AVR_NONE. */
14899 || reloc_type == 30 /* R_AVR_DIFF8. */
14900 || reloc_type == 31 /* R_AVR_DIFF16. */
14901 || reloc_type == 32 /* R_AVR_DIFF32. */);
14902 case EM_METAG:
14903 return reloc_type == 3; /* R_METAG_NONE. */
14904 case EM_NDS32:
14905 return (reloc_type == 0 /* R_NDS32_NONE. */
14906 || reloc_type == 205 /* R_NDS32_DIFF8. */
14907 || reloc_type == 206 /* R_NDS32_DIFF16. */
14908 || reloc_type == 207 /* R_NDS32_DIFF32. */
14909 || reloc_type == 208 /* R_NDS32_DIFF_ULEB128. */);
14910 case EM_TI_PRU:
14911 return (reloc_type == 0 /* R_PRU_NONE. */
14912 || reloc_type == 65 /* R_PRU_DIFF8. */
14913 || reloc_type == 66 /* R_PRU_DIFF16. */
14914 || reloc_type == 67 /* R_PRU_DIFF32. */);
14915 case EM_XTENSA_OLD:
14916 case EM_XTENSA:
14917 return (reloc_type == 0 /* R_XTENSA_NONE. */
14918 || reloc_type == 17 /* R_XTENSA_DIFF8. */
14919 || reloc_type == 18 /* R_XTENSA_DIFF16. */
14920 || reloc_type == 19 /* R_XTENSA_DIFF32. */
14921 || reloc_type == 57 /* R_XTENSA_PDIFF8. */
14922 || reloc_type == 58 /* R_XTENSA_PDIFF16. */
14923 || reloc_type == 59 /* R_XTENSA_PDIFF32. */
14924 || reloc_type == 60 /* R_XTENSA_NDIFF8. */
14925 || reloc_type == 61 /* R_XTENSA_NDIFF16. */
14926 || reloc_type == 62 /* R_XTENSA_NDIFF32. */);
14927 }
14928 return false;
14929 }
14930
14931 /* Returns TRUE if there is a relocation against
14932 section NAME at OFFSET bytes. */
14933
14934 bool
14935 reloc_at (struct dwarf_section * dsec, uint64_t offset)
14936 {
14937 Elf_Internal_Rela * relocs;
14938 Elf_Internal_Rela * rp;
14939
14940 if (dsec == NULL || dsec->reloc_info == NULL)
14941 return false;
14942
14943 relocs = (Elf_Internal_Rela *) dsec->reloc_info;
14944
14945 for (rp = relocs; rp < relocs + dsec->num_relocs; ++rp)
14946 if (rp->r_offset == offset)
14947 return true;
14948
14949 return false;
14950 }
14951
14952 /* Apply relocations to a section.
14953 Returns TRUE upon success, FALSE otherwise.
14954 If RELOCS_RETURN is non-NULL then it is set to point to the loaded relocs.
14955 It is then the caller's responsibility to free them. NUM_RELOCS_RETURN
14956 will be set to the number of relocs loaded.
14957
14958 Note: So far support has been added only for those relocations
14959 which can be found in debug sections. FIXME: Add support for
14960 more relocations ? */
14961
14962 static bool
14963 apply_relocations (Filedata *filedata,
14964 const Elf_Internal_Shdr *section,
14965 unsigned char *start,
14966 size_t size,
14967 void **relocs_return,
14968 unsigned long *num_relocs_return)
14969 {
14970 Elf_Internal_Shdr * relsec;
14971 unsigned char * end = start + size;
14972
14973 if (relocs_return != NULL)
14974 {
14975 * (Elf_Internal_Rela **) relocs_return = NULL;
14976 * num_relocs_return = 0;
14977 }
14978
14979 if (filedata->file_header.e_type != ET_REL)
14980 /* No relocs to apply. */
14981 return true;
14982
14983 /* Find the reloc section associated with the section. */
14984 for (relsec = filedata->section_headers;
14985 relsec < filedata->section_headers + filedata->file_header.e_shnum;
14986 ++relsec)
14987 {
14988 bool is_rela;
14989 unsigned long num_relocs;
14990 Elf_Internal_Rela * relocs;
14991 Elf_Internal_Rela * rp;
14992 Elf_Internal_Shdr * symsec;
14993 Elf_Internal_Sym * symtab;
14994 unsigned long num_syms;
14995 Elf_Internal_Sym * sym;
14996
14997 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
14998 || relsec->sh_info >= filedata->file_header.e_shnum
14999 || filedata->section_headers + relsec->sh_info != section
15000 || relsec->sh_size == 0
15001 || relsec->sh_link >= filedata->file_header.e_shnum)
15002 continue;
15003
15004 symsec = filedata->section_headers + relsec->sh_link;
15005 if (symsec->sh_type != SHT_SYMTAB
15006 && symsec->sh_type != SHT_DYNSYM)
15007 return false;
15008
15009 is_rela = relsec->sh_type == SHT_RELA;
15010
15011 if (is_rela)
15012 {
15013 if (!slurp_rela_relocs (filedata, relsec->sh_offset,
15014 relsec->sh_size, & relocs, & num_relocs))
15015 return false;
15016 }
15017 else
15018 {
15019 if (!slurp_rel_relocs (filedata, relsec->sh_offset,
15020 relsec->sh_size, & relocs, & num_relocs))
15021 return false;
15022 }
15023
15024 /* SH uses RELA but uses in place value instead of the addend field. */
15025 if (filedata->file_header.e_machine == EM_SH)
15026 is_rela = false;
15027
15028 symtab = get_elf_symbols (filedata, symsec, & num_syms);
15029
15030 for (rp = relocs; rp < relocs + num_relocs; ++rp)
15031 {
15032 uint64_t addend;
15033 unsigned int reloc_type;
15034 unsigned int reloc_size;
15035 bool reloc_inplace = false;
15036 bool reloc_subtract = false;
15037 unsigned char *rloc;
15038 unsigned long sym_index;
15039
15040 reloc_type = get_reloc_type (filedata, rp->r_info);
15041
15042 if (target_specific_reloc_handling (filedata, rp, start, end, symtab, num_syms))
15043 continue;
15044 else if (is_none_reloc (filedata, reloc_type))
15045 continue;
15046 else if (is_32bit_abs_reloc (filedata, reloc_type)
15047 || is_32bit_pcrel_reloc (filedata, reloc_type))
15048 reloc_size = 4;
15049 else if (is_64bit_abs_reloc (filedata, reloc_type)
15050 || is_64bit_pcrel_reloc (filedata, reloc_type))
15051 reloc_size = 8;
15052 else if (is_24bit_abs_reloc (filedata, reloc_type))
15053 reloc_size = 3;
15054 else if (is_16bit_abs_reloc (filedata, reloc_type))
15055 reloc_size = 2;
15056 else if (is_8bit_abs_reloc (filedata, reloc_type)
15057 || is_6bit_abs_reloc (filedata, reloc_type))
15058 reloc_size = 1;
15059 else if ((reloc_subtract = is_32bit_inplace_sub_reloc (filedata,
15060 reloc_type))
15061 || is_32bit_inplace_add_reloc (filedata, reloc_type))
15062 {
15063 reloc_size = 4;
15064 reloc_inplace = true;
15065 }
15066 else if ((reloc_subtract = is_64bit_inplace_sub_reloc (filedata,
15067 reloc_type))
15068 || is_64bit_inplace_add_reloc (filedata, reloc_type))
15069 {
15070 reloc_size = 8;
15071 reloc_inplace = true;
15072 }
15073 else if ((reloc_subtract = is_16bit_inplace_sub_reloc (filedata,
15074 reloc_type))
15075 || is_16bit_inplace_add_reloc (filedata, reloc_type))
15076 {
15077 reloc_size = 2;
15078 reloc_inplace = true;
15079 }
15080 else if ((reloc_subtract = is_8bit_inplace_sub_reloc (filedata,
15081 reloc_type))
15082 || is_8bit_inplace_add_reloc (filedata, reloc_type))
15083 {
15084 reloc_size = 1;
15085 reloc_inplace = true;
15086 }
15087 else if ((reloc_subtract = is_6bit_inplace_sub_reloc (filedata,
15088 reloc_type)))
15089 {
15090 reloc_size = 1;
15091 reloc_inplace = true;
15092 }
15093 else
15094 {
15095 static unsigned int prev_reloc = 0;
15096
15097 if (reloc_type != prev_reloc)
15098 warn (_("unable to apply unsupported reloc type %d to section %s\n"),
15099 reloc_type, printable_section_name (filedata, section));
15100 prev_reloc = reloc_type;
15101 continue;
15102 }
15103
15104 rloc = start + rp->r_offset;
15105 if (!IN_RANGE (start, end, rloc, reloc_size))
15106 {
15107 warn (_("skipping invalid relocation offset 0x%lx in section %s\n"),
15108 (unsigned long) rp->r_offset,
15109 printable_section_name (filedata, section));
15110 continue;
15111 }
15112
15113 sym_index = (unsigned long) get_reloc_symindex (rp->r_info);
15114 if (sym_index >= num_syms)
15115 {
15116 warn (_("skipping invalid relocation symbol index 0x%lx in section %s\n"),
15117 sym_index, printable_section_name (filedata, section));
15118 continue;
15119 }
15120 sym = symtab + sym_index;
15121
15122 /* If the reloc has a symbol associated with it,
15123 make sure that it is of an appropriate type.
15124
15125 Relocations against symbols without type can happen.
15126 Gcc -feliminate-dwarf2-dups may generate symbols
15127 without type for debug info.
15128
15129 Icc generates relocations against function symbols
15130 instead of local labels.
15131
15132 Relocations against object symbols can happen, eg when
15133 referencing a global array. For an example of this see
15134 the _clz.o binary in libgcc.a. */
15135 if (sym != symtab
15136 && ELF_ST_TYPE (sym->st_info) != STT_COMMON
15137 && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
15138 {
15139 warn (_("skipping unexpected symbol type %s in section %s relocation %ld\n"),
15140 get_symbol_type (filedata, ELF_ST_TYPE (sym->st_info)),
15141 printable_section_name (filedata, relsec),
15142 (long int)(rp - relocs));
15143 continue;
15144 }
15145
15146 addend = 0;
15147 if (is_rela)
15148 addend += rp->r_addend;
15149 /* R_XTENSA_32, R_PJ_DATA_DIR32 and R_D30V_32_NORMAL are
15150 partial_inplace. */
15151 if (!is_rela
15152 || (filedata->file_header.e_machine == EM_XTENSA
15153 && reloc_type == 1)
15154 || ((filedata->file_header.e_machine == EM_PJ
15155 || filedata->file_header.e_machine == EM_PJ_OLD)
15156 && reloc_type == 1)
15157 || ((filedata->file_header.e_machine == EM_D30V
15158 || filedata->file_header.e_machine == EM_CYGNUS_D30V)
15159 && reloc_type == 12)
15160 || reloc_inplace)
15161 {
15162 if (is_6bit_inplace_sub_reloc (filedata, reloc_type))
15163 addend += byte_get (rloc, reloc_size) & 0x3f;
15164 else
15165 addend += byte_get (rloc, reloc_size);
15166 }
15167
15168 if (is_32bit_pcrel_reloc (filedata, reloc_type)
15169 || is_64bit_pcrel_reloc (filedata, reloc_type))
15170 {
15171 /* On HPPA, all pc-relative relocations are biased by 8. */
15172 if (filedata->file_header.e_machine == EM_PARISC)
15173 addend -= 8;
15174 byte_put (rloc, (addend + sym->st_value) - rp->r_offset,
15175 reloc_size);
15176 }
15177 else if (is_6bit_abs_reloc (filedata, reloc_type)
15178 || is_6bit_inplace_sub_reloc (filedata, reloc_type))
15179 {
15180 if (reloc_subtract)
15181 addend -= sym->st_value;
15182 else
15183 addend += sym->st_value;
15184 addend = (addend & 0x3f) | (byte_get (rloc, reloc_size) & 0xc0);
15185 byte_put (rloc, addend, reloc_size);
15186 }
15187 else if (reloc_subtract)
15188 byte_put (rloc, addend - sym->st_value, reloc_size);
15189 else
15190 byte_put (rloc, addend + sym->st_value, reloc_size);
15191 }
15192
15193 free (symtab);
15194 /* Let the target specific reloc processing code know that
15195 we have finished with these relocs. */
15196 target_specific_reloc_handling (filedata, NULL, NULL, NULL, NULL, 0);
15197
15198 if (relocs_return)
15199 {
15200 * (Elf_Internal_Rela **) relocs_return = relocs;
15201 * num_relocs_return = num_relocs;
15202 }
15203 else
15204 free (relocs);
15205
15206 break;
15207 }
15208
15209 return true;
15210 }
15211
15212 #ifdef SUPPORT_DISASSEMBLY
15213 static bool
15214 disassemble_section (Elf_Internal_Shdr * section, Filedata * filedata)
15215 {
15216 printf (_("\nAssembly dump of section %s\n"), printable_section_name (filedata, section));
15217
15218 /* FIXME: XXX -- to be done --- XXX */
15219
15220 return true;
15221 }
15222 #endif
15223
15224 /* Reads in the contents of SECTION from FILE, returning a pointer
15225 to a malloc'ed buffer or NULL if something went wrong. */
15226
15227 static char *
15228 get_section_contents (Elf_Internal_Shdr * section, Filedata * filedata)
15229 {
15230 uint64_t num_bytes = section->sh_size;
15231
15232 if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
15233 {
15234 printf (_("Section '%s' has no data to dump.\n"),
15235 printable_section_name (filedata, section));
15236 return NULL;
15237 }
15238
15239 return (char *) get_data (NULL, filedata, section->sh_offset, 1, num_bytes,
15240 _("section contents"));
15241 }
15242
15243 /* Uncompresses a section that was compressed using zlib/zstd, in place. */
15244
15245 static bool
15246 uncompress_section_contents (bool is_zstd, unsigned char **buffer,
15247 uint64_t uncompressed_size, uint64_t *size)
15248 {
15249 uint64_t compressed_size = *size;
15250 unsigned char *compressed_buffer = *buffer;
15251 unsigned char *uncompressed_buffer = xmalloc (uncompressed_size);
15252 z_stream strm;
15253 int rc;
15254
15255 if (is_zstd)
15256 {
15257 #ifdef HAVE_ZSTD
15258 size_t ret = ZSTD_decompress (uncompressed_buffer, uncompressed_size,
15259 compressed_buffer, compressed_size);
15260 if (ZSTD_isError (ret))
15261 goto fail;
15262 #endif
15263 }
15264 else
15265 {
15266 /* It is possible the section consists of several compressed
15267 buffers concatenated together, so we uncompress in a loop. */
15268 /* PR 18313: The state field in the z_stream structure is supposed
15269 to be invisible to the user (ie us), but some compilers will
15270 still complain about it being used without initialisation. So
15271 we first zero the entire z_stream structure and then set the fields
15272 that we need. */
15273 memset (&strm, 0, sizeof strm);
15274 strm.avail_in = compressed_size;
15275 strm.next_in = (Bytef *)compressed_buffer;
15276 strm.avail_out = uncompressed_size;
15277
15278 rc = inflateInit (&strm);
15279 while (strm.avail_in > 0)
15280 {
15281 if (rc != Z_OK)
15282 break;
15283 strm.next_out = ((Bytef *)uncompressed_buffer
15284 + (uncompressed_size - strm.avail_out));
15285 rc = inflate (&strm, Z_FINISH);
15286 if (rc != Z_STREAM_END)
15287 break;
15288 rc = inflateReset (&strm);
15289 }
15290 if (inflateEnd (&strm) != Z_OK || rc != Z_OK || strm.avail_out != 0)
15291 goto fail;
15292 }
15293
15294 *buffer = uncompressed_buffer;
15295 *size = uncompressed_size;
15296 return true;
15297
15298 fail:
15299 free (uncompressed_buffer);
15300 /* Indicate decompression failure. */
15301 *buffer = NULL;
15302 return false;
15303 }
15304
15305 static bool
15306 dump_section_as_strings (Elf_Internal_Shdr * section, Filedata * filedata)
15307 {
15308 Elf_Internal_Shdr *relsec;
15309 uint64_t num_bytes;
15310 unsigned char *data;
15311 unsigned char *end;
15312 unsigned char *real_start;
15313 unsigned char *start;
15314 bool some_strings_shown;
15315
15316 real_start = start = (unsigned char *) get_section_contents (section, filedata);
15317 if (start == NULL)
15318 /* PR 21820: Do not fail if the section was empty. */
15319 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
15320
15321 num_bytes = section->sh_size;
15322
15323 if (filedata->is_separate)
15324 printf (_("\nString dump of section '%s' in linked file %s:\n"),
15325 printable_section_name (filedata, section),
15326 filedata->file_name);
15327 else
15328 printf (_("\nString dump of section '%s':\n"),
15329 printable_section_name (filedata, section));
15330
15331 if (decompress_dumps)
15332 {
15333 uint64_t new_size = num_bytes;
15334 uint64_t uncompressed_size = 0;
15335 bool is_zstd = false;
15336
15337 if ((section->sh_flags & SHF_COMPRESSED) != 0)
15338 {
15339 Elf_Internal_Chdr chdr;
15340 unsigned int compression_header_size
15341 = get_compression_header (& chdr, (unsigned char *) start,
15342 num_bytes);
15343 if (compression_header_size == 0)
15344 /* An error message will have already been generated
15345 by get_compression_header. */
15346 goto error_out;
15347
15348 if (chdr.ch_type == ELFCOMPRESS_ZLIB)
15349 ;
15350 #ifdef HAVE_ZSTD
15351 else if (chdr.ch_type == ELFCOMPRESS_ZSTD)
15352 is_zstd = true;
15353 #endif
15354 else
15355 {
15356 warn (_("section '%s' has unsupported compress type: %d\n"),
15357 printable_section_name (filedata, section), chdr.ch_type);
15358 goto error_out;
15359 }
15360 uncompressed_size = chdr.ch_size;
15361 start += compression_header_size;
15362 new_size -= compression_header_size;
15363 }
15364 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
15365 {
15366 /* Read the zlib header. In this case, it should be "ZLIB"
15367 followed by the uncompressed section size, 8 bytes in
15368 big-endian order. */
15369 uncompressed_size = start[4]; uncompressed_size <<= 8;
15370 uncompressed_size += start[5]; uncompressed_size <<= 8;
15371 uncompressed_size += start[6]; uncompressed_size <<= 8;
15372 uncompressed_size += start[7]; uncompressed_size <<= 8;
15373 uncompressed_size += start[8]; uncompressed_size <<= 8;
15374 uncompressed_size += start[9]; uncompressed_size <<= 8;
15375 uncompressed_size += start[10]; uncompressed_size <<= 8;
15376 uncompressed_size += start[11];
15377 start += 12;
15378 new_size -= 12;
15379 }
15380
15381 if (uncompressed_size)
15382 {
15383 if (uncompress_section_contents (is_zstd, &start, uncompressed_size,
15384 &new_size))
15385 num_bytes = new_size;
15386 else
15387 {
15388 error (_("Unable to decompress section %s\n"),
15389 printable_section_name (filedata, section));
15390 goto error_out;
15391 }
15392 }
15393 else
15394 start = real_start;
15395 }
15396
15397 /* If the section being dumped has relocations against it the user might
15398 be expecting these relocations to have been applied. Check for this
15399 case and issue a warning message in order to avoid confusion.
15400 FIXME: Maybe we ought to have an option that dumps a section with
15401 relocs applied ? */
15402 for (relsec = filedata->section_headers;
15403 relsec < filedata->section_headers + filedata->file_header.e_shnum;
15404 ++relsec)
15405 {
15406 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
15407 || relsec->sh_info >= filedata->file_header.e_shnum
15408 || filedata->section_headers + relsec->sh_info != section
15409 || relsec->sh_size == 0
15410 || relsec->sh_link >= filedata->file_header.e_shnum)
15411 continue;
15412
15413 printf (_(" Note: This section has relocations against it, but these have NOT been applied to this dump.\n"));
15414 break;
15415 }
15416
15417 data = start;
15418 end = start + num_bytes;
15419 some_strings_shown = false;
15420
15421 #ifdef HAVE_MBSTATE_T
15422 mbstate_t state;
15423 /* Initialise the multibyte conversion state. */
15424 memset (& state, 0, sizeof (state));
15425 #endif
15426
15427 bool continuing = false;
15428
15429 while (data < end)
15430 {
15431 while (!ISPRINT (* data))
15432 if (++ data >= end)
15433 break;
15434
15435 if (data < end)
15436 {
15437 size_t maxlen = end - data;
15438
15439 if (continuing)
15440 {
15441 printf (" ");
15442 continuing = false;
15443 }
15444 else
15445 {
15446 printf (" [%6lx] ", (unsigned long) (data - start));
15447 }
15448
15449 if (maxlen > 0)
15450 {
15451 char c = 0;
15452
15453 while (maxlen)
15454 {
15455 c = *data++;
15456
15457 if (c == 0)
15458 break;
15459
15460 /* PR 25543: Treat new-lines as string-ending characters. */
15461 if (c == '\n')
15462 {
15463 printf ("\\n\n");
15464 if (*data != 0)
15465 continuing = true;
15466 break;
15467 }
15468
15469 /* Do not print control characters directly as they can affect terminal
15470 settings. Such characters usually appear in the names generated
15471 by the assembler for local labels. */
15472 if (ISCNTRL (c))
15473 {
15474 printf ("^%c", c + 0x40);
15475 }
15476 else if (ISPRINT (c))
15477 {
15478 putchar (c);
15479 }
15480 else
15481 {
15482 size_t n;
15483 #ifdef HAVE_MBSTATE_T
15484 wchar_t w;
15485 #endif
15486 /* Let printf do the hard work of displaying multibyte characters. */
15487 printf ("%.1s", data - 1);
15488 #ifdef HAVE_MBSTATE_T
15489 /* Try to find out how many bytes made up the character that was
15490 just printed. Advance the symbol pointer past the bytes that
15491 were displayed. */
15492 n = mbrtowc (& w, (char *)(data - 1), MB_CUR_MAX, & state);
15493 #else
15494 n = 1;
15495 #endif
15496 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
15497 data += (n - 1);
15498 }
15499 }
15500
15501 if (c != '\n')
15502 putchar ('\n');
15503 }
15504 else
15505 {
15506 printf (_("<corrupt>\n"));
15507 data = end;
15508 }
15509 some_strings_shown = true;
15510 }
15511 }
15512
15513 if (! some_strings_shown)
15514 printf (_(" No strings found in this section."));
15515
15516 free (real_start);
15517
15518 putchar ('\n');
15519 return true;
15520
15521 error_out:
15522 free (real_start);
15523 return false;
15524 }
15525
15526 static bool
15527 dump_section_as_bytes (Elf_Internal_Shdr *section,
15528 Filedata *filedata,
15529 bool relocate)
15530 {
15531 Elf_Internal_Shdr *relsec;
15532 size_t bytes;
15533 uint64_t section_size;
15534 uint64_t addr;
15535 unsigned char *data;
15536 unsigned char *real_start;
15537 unsigned char *start;
15538
15539 real_start = start = (unsigned char *) get_section_contents (section, filedata);
15540 if (start == NULL)
15541 /* PR 21820: Do not fail if the section was empty. */
15542 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
15543
15544 section_size = section->sh_size;
15545
15546 if (filedata->is_separate)
15547 printf (_("\nHex dump of section '%s' in linked file %s:\n"),
15548 printable_section_name (filedata, section),
15549 filedata->file_name);
15550 else
15551 printf (_("\nHex dump of section '%s':\n"),
15552 printable_section_name (filedata, section));
15553
15554 if (decompress_dumps)
15555 {
15556 uint64_t new_size = section_size;
15557 uint64_t uncompressed_size = 0;
15558 bool is_zstd = false;
15559
15560 if ((section->sh_flags & SHF_COMPRESSED) != 0)
15561 {
15562 Elf_Internal_Chdr chdr;
15563 unsigned int compression_header_size
15564 = get_compression_header (& chdr, start, section_size);
15565
15566 if (compression_header_size == 0)
15567 /* An error message will have already been generated
15568 by get_compression_header. */
15569 goto error_out;
15570
15571 if (chdr.ch_type == ELFCOMPRESS_ZLIB)
15572 ;
15573 #ifdef HAVE_ZSTD
15574 else if (chdr.ch_type == ELFCOMPRESS_ZSTD)
15575 is_zstd = true;
15576 #endif
15577 else
15578 {
15579 warn (_("section '%s' has unsupported compress type: %d\n"),
15580 printable_section_name (filedata, section), chdr.ch_type);
15581 goto error_out;
15582 }
15583 uncompressed_size = chdr.ch_size;
15584 start += compression_header_size;
15585 new_size -= compression_header_size;
15586 }
15587 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
15588 {
15589 /* Read the zlib header. In this case, it should be "ZLIB"
15590 followed by the uncompressed section size, 8 bytes in
15591 big-endian order. */
15592 uncompressed_size = start[4]; uncompressed_size <<= 8;
15593 uncompressed_size += start[5]; uncompressed_size <<= 8;
15594 uncompressed_size += start[6]; uncompressed_size <<= 8;
15595 uncompressed_size += start[7]; uncompressed_size <<= 8;
15596 uncompressed_size += start[8]; uncompressed_size <<= 8;
15597 uncompressed_size += start[9]; uncompressed_size <<= 8;
15598 uncompressed_size += start[10]; uncompressed_size <<= 8;
15599 uncompressed_size += start[11];
15600 start += 12;
15601 new_size -= 12;
15602 }
15603
15604 if (uncompressed_size)
15605 {
15606 if (uncompress_section_contents (is_zstd, &start, uncompressed_size,
15607 &new_size))
15608 {
15609 section_size = new_size;
15610 }
15611 else
15612 {
15613 error (_("Unable to decompress section %s\n"),
15614 printable_section_name (filedata, section));
15615 /* FIXME: Print the section anyway ? */
15616 goto error_out;
15617 }
15618 }
15619 else
15620 start = real_start;
15621 }
15622
15623 if (relocate)
15624 {
15625 if (! apply_relocations (filedata, section, start, section_size, NULL, NULL))
15626 goto error_out;
15627 }
15628 else
15629 {
15630 /* If the section being dumped has relocations against it the user might
15631 be expecting these relocations to have been applied. Check for this
15632 case and issue a warning message in order to avoid confusion.
15633 FIXME: Maybe we ought to have an option that dumps a section with
15634 relocs applied ? */
15635 for (relsec = filedata->section_headers;
15636 relsec < filedata->section_headers + filedata->file_header.e_shnum;
15637 ++relsec)
15638 {
15639 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
15640 || relsec->sh_info >= filedata->file_header.e_shnum
15641 || filedata->section_headers + relsec->sh_info != section
15642 || relsec->sh_size == 0
15643 || relsec->sh_link >= filedata->file_header.e_shnum)
15644 continue;
15645
15646 printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
15647 break;
15648 }
15649 }
15650
15651 addr = section->sh_addr;
15652 bytes = section_size;
15653 data = start;
15654
15655 while (bytes)
15656 {
15657 int j;
15658 int k;
15659 int lbytes;
15660
15661 lbytes = (bytes > 16 ? 16 : bytes);
15662
15663 printf (" 0x%8.8lx ", (unsigned long) addr);
15664
15665 for (j = 0; j < 16; j++)
15666 {
15667 if (j < lbytes)
15668 printf ("%2.2x", data[j]);
15669 else
15670 printf (" ");
15671
15672 if ((j & 3) == 3)
15673 printf (" ");
15674 }
15675
15676 for (j = 0; j < lbytes; j++)
15677 {
15678 k = data[j];
15679 if (k >= ' ' && k < 0x7f)
15680 printf ("%c", k);
15681 else
15682 printf (".");
15683 }
15684
15685 putchar ('\n');
15686
15687 data += lbytes;
15688 addr += lbytes;
15689 bytes -= lbytes;
15690 }
15691
15692 free (real_start);
15693
15694 putchar ('\n');
15695 return true;
15696
15697 error_out:
15698 free (real_start);
15699 return false;
15700 }
15701
15702 #ifdef ENABLE_LIBCTF
15703 static ctf_sect_t *
15704 shdr_to_ctf_sect (ctf_sect_t *buf, Elf_Internal_Shdr *shdr, Filedata *filedata)
15705 {
15706 buf->cts_name = section_name_print (filedata, shdr);
15707 buf->cts_size = shdr->sh_size;
15708 buf->cts_entsize = shdr->sh_entsize;
15709
15710 return buf;
15711 }
15712
15713 /* Formatting callback function passed to ctf_dump. Returns either the pointer
15714 it is passed, or a pointer to newly-allocated storage, in which case
15715 dump_ctf() will free it when it no longer needs it. */
15716
15717 static char *
15718 dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
15719 char *s, void *arg)
15720 {
15721 const char *blanks = arg;
15722 char *new_s;
15723
15724 if (asprintf (&new_s, "%s%s", blanks, s) < 0)
15725 return s;
15726 return new_s;
15727 }
15728
15729 /* Dump CTF errors/warnings. */
15730 static void
15731 dump_ctf_errs (ctf_dict_t *fp)
15732 {
15733 ctf_next_t *it = NULL;
15734 char *errtext;
15735 int is_warning;
15736 int err;
15737
15738 /* Dump accumulated errors and warnings. */
15739 while ((errtext = ctf_errwarning_next (fp, &it, &is_warning, &err)) != NULL)
15740 {
15741 error (_("%s: %s"), is_warning ? _("warning"): _("error"),
15742 errtext);
15743 free (errtext);
15744 }
15745 if (err != ECTF_NEXT_END)
15746 error (_("CTF error: cannot get CTF errors: `%s'"), ctf_errmsg (err));
15747 }
15748
15749 /* Dump one CTF archive member. */
15750
15751 static void
15752 dump_ctf_archive_member (ctf_dict_t *ctf, const char *name, ctf_dict_t *parent,
15753 size_t member)
15754 {
15755 const char *things[] = {"Header", "Labels", "Data objects",
15756 "Function objects", "Variables", "Types", "Strings",
15757 ""};
15758 const char **thing;
15759 size_t i;
15760
15761 /* Don't print out the name of the default-named archive member if it appears
15762 first in the list. The name .ctf appears everywhere, even for things that
15763 aren't really archives, so printing it out is liable to be confusing; also,
15764 the common case by far is for only one archive member to exist, and hiding
15765 it in that case seems worthwhile. */
15766
15767 if (strcmp (name, ".ctf") != 0 || member != 0)
15768 printf (_("\nCTF archive member: %s:\n"), name);
15769
15770 if (ctf_parent_name (ctf) != NULL)
15771 ctf_import (ctf, parent);
15772
15773 for (i = 0, thing = things; *thing[0]; thing++, i++)
15774 {
15775 ctf_dump_state_t *s = NULL;
15776 char *item;
15777
15778 printf ("\n %s:\n", *thing);
15779 while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
15780 (void *) " ")) != NULL)
15781 {
15782 printf ("%s\n", item);
15783 free (item);
15784 }
15785
15786 if (ctf_errno (ctf))
15787 {
15788 error (_("Iteration failed: %s, %s\n"), *thing,
15789 ctf_errmsg (ctf_errno (ctf)));
15790 break;
15791 }
15792 }
15793
15794 dump_ctf_errs (ctf);
15795 }
15796
15797 static bool
15798 dump_section_as_ctf (Elf_Internal_Shdr * section, Filedata * filedata)
15799 {
15800 Elf_Internal_Shdr * symtab_sec = NULL;
15801 Elf_Internal_Shdr * strtab_sec = NULL;
15802 void * data = NULL;
15803 void * symdata = NULL;
15804 void * strdata = NULL;
15805 ctf_sect_t ctfsect, symsect, strsect;
15806 ctf_sect_t * symsectp = NULL;
15807 ctf_sect_t * strsectp = NULL;
15808 ctf_archive_t * ctfa = NULL;
15809 ctf_dict_t * parent = NULL;
15810 ctf_dict_t * fp;
15811
15812 ctf_next_t *i = NULL;
15813 const char *name;
15814 size_t member = 0;
15815 int err;
15816 bool ret = false;
15817
15818 shdr_to_ctf_sect (&ctfsect, section, filedata);
15819 data = get_section_contents (section, filedata);
15820 ctfsect.cts_data = data;
15821
15822 if (!dump_ctf_symtab_name)
15823 dump_ctf_symtab_name = strdup (".dynsym");
15824
15825 if (!dump_ctf_strtab_name)
15826 dump_ctf_strtab_name = strdup (".dynstr");
15827
15828 if (dump_ctf_symtab_name && dump_ctf_symtab_name[0] != 0)
15829 {
15830 if ((symtab_sec = find_section (filedata, dump_ctf_symtab_name)) == NULL)
15831 {
15832 error (_("No symbol section named %s\n"), dump_ctf_symtab_name);
15833 goto fail;
15834 }
15835 if ((symdata = (void *) get_data (NULL, filedata,
15836 symtab_sec->sh_offset, 1,
15837 symtab_sec->sh_size,
15838 _("symbols"))) == NULL)
15839 goto fail;
15840 symsectp = shdr_to_ctf_sect (&symsect, symtab_sec, filedata);
15841 symsect.cts_data = symdata;
15842 }
15843
15844 if (dump_ctf_strtab_name && dump_ctf_strtab_name[0] != 0)
15845 {
15846 if ((strtab_sec = find_section (filedata, dump_ctf_strtab_name)) == NULL)
15847 {
15848 error (_("No string table section named %s\n"),
15849 dump_ctf_strtab_name);
15850 goto fail;
15851 }
15852 if ((strdata = (void *) get_data (NULL, filedata,
15853 strtab_sec->sh_offset, 1,
15854 strtab_sec->sh_size,
15855 _("strings"))) == NULL)
15856 goto fail;
15857 strsectp = shdr_to_ctf_sect (&strsect, strtab_sec, filedata);
15858 strsect.cts_data = strdata;
15859 }
15860
15861 /* Load the CTF file and dump it. It may be a raw CTF section, or an archive:
15862 libctf papers over the difference, so we can pretend it is always an
15863 archive. */
15864
15865 if ((ctfa = ctf_arc_bufopen (&ctfsect, symsectp, strsectp, &err)) == NULL)
15866 {
15867 dump_ctf_errs (NULL);
15868 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15869 goto fail;
15870 }
15871
15872 ctf_arc_symsect_endianness (ctfa, filedata->file_header.e_ident[EI_DATA]
15873 != ELFDATA2MSB);
15874
15875 /* Preload the parent dict, since it will need to be imported into every
15876 child in turn. */
15877 if ((parent = ctf_dict_open (ctfa, dump_ctf_parent_name, &err)) == NULL)
15878 {
15879 dump_ctf_errs (NULL);
15880 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15881 goto fail;
15882 }
15883
15884 ret = true;
15885
15886 if (filedata->is_separate)
15887 printf (_("\nDump of CTF section '%s' in linked file %s:\n"),
15888 printable_section_name (filedata, section),
15889 filedata->file_name);
15890 else
15891 printf (_("\nDump of CTF section '%s':\n"),
15892 printable_section_name (filedata, section));
15893
15894 while ((fp = ctf_archive_next (ctfa, &i, &name, 0, &err)) != NULL)
15895 dump_ctf_archive_member (fp, name, parent, member++);
15896 if (err != ECTF_NEXT_END)
15897 {
15898 dump_ctf_errs (NULL);
15899 error (_("CTF member open failure: %s\n"), ctf_errmsg (err));
15900 ret = false;
15901 }
15902
15903 fail:
15904 ctf_dict_close (parent);
15905 ctf_close (ctfa);
15906 free (data);
15907 free (symdata);
15908 free (strdata);
15909 return ret;
15910 }
15911 #endif
15912
15913 static bool
15914 dump_section_as_sframe (Elf_Internal_Shdr * section, Filedata * filedata)
15915 {
15916 void * data = NULL;
15917 sframe_decoder_ctx *sfd_ctx = NULL;
15918 const char *print_name = printable_section_name (filedata, section);
15919
15920 bool ret = true;
15921 size_t sf_size;
15922 int err = 0;
15923
15924 if (strcmp (print_name, "") == 0)
15925 {
15926 error (_("Section name must be provided \n"));
15927 ret = false;
15928 return ret;
15929 }
15930
15931 data = get_section_contents (section, filedata);
15932 sf_size = section->sh_size;
15933 /* Decode the contents of the section. */
15934 sfd_ctx = sframe_decode ((const char*)data, sf_size, &err);
15935 if (!sfd_ctx)
15936 {
15937 ret = false;
15938 error (_("SFrame decode failure: %s\n"), sframe_errmsg (err));
15939 goto fail;
15940 }
15941
15942 printf (_("Contents of the SFrame section %s:"), print_name);
15943 /* Dump the contents as text. */
15944 dump_sframe (sfd_ctx, section->sh_addr);
15945
15946 fail:
15947 free (data);
15948 return ret;
15949 }
15950
15951 static bool
15952 load_specific_debug_section (enum dwarf_section_display_enum debug,
15953 const Elf_Internal_Shdr * sec,
15954 void * data)
15955 {
15956 struct dwarf_section * section = &debug_displays [debug].section;
15957 char buf [64];
15958 Filedata * filedata = (Filedata *) data;
15959
15960 if (section->start != NULL)
15961 {
15962 /* If it is already loaded, do nothing. */
15963 if (streq (section->filename, filedata->file_name))
15964 return true;
15965 free (section->start);
15966 }
15967
15968 snprintf (buf, sizeof (buf), _("%s section data"), section->name);
15969 section->address = sec->sh_addr;
15970 section->filename = filedata->file_name;
15971 section->start = (unsigned char *) get_data (NULL, filedata,
15972 sec->sh_offset, 1,
15973 sec->sh_size, buf);
15974 if (section->start == NULL)
15975 section->size = 0;
15976 else
15977 {
15978 unsigned char *start = section->start;
15979 uint64_t size = sec->sh_size;
15980 uint64_t uncompressed_size = 0;
15981 bool is_zstd = false;
15982
15983 if ((sec->sh_flags & SHF_COMPRESSED) != 0)
15984 {
15985 Elf_Internal_Chdr chdr;
15986 unsigned int compression_header_size;
15987
15988 if (size < (is_32bit_elf
15989 ? sizeof (Elf32_External_Chdr)
15990 : sizeof (Elf64_External_Chdr)))
15991 {
15992 warn (_("compressed section %s is too small to contain a compression header\n"),
15993 section->name);
15994 return false;
15995 }
15996
15997 compression_header_size = get_compression_header (&chdr, start, size);
15998 if (compression_header_size == 0)
15999 /* An error message will have already been generated
16000 by get_compression_header. */
16001 return false;
16002
16003 if (chdr.ch_type == ELFCOMPRESS_ZLIB)
16004 ;
16005 #ifdef HAVE_ZSTD
16006 else if (chdr.ch_type == ELFCOMPRESS_ZSTD)
16007 is_zstd = true;
16008 #endif
16009 else
16010 {
16011 warn (_("section '%s' has unsupported compress type: %d\n"),
16012 section->name, chdr.ch_type);
16013 return false;
16014 }
16015 uncompressed_size = chdr.ch_size;
16016 start += compression_header_size;
16017 size -= compression_header_size;
16018 }
16019 else if (size > 12 && streq ((char *) start, "ZLIB"))
16020 {
16021 /* Read the zlib header. In this case, it should be "ZLIB"
16022 followed by the uncompressed section size, 8 bytes in
16023 big-endian order. */
16024 uncompressed_size = start[4]; uncompressed_size <<= 8;
16025 uncompressed_size += start[5]; uncompressed_size <<= 8;
16026 uncompressed_size += start[6]; uncompressed_size <<= 8;
16027 uncompressed_size += start[7]; uncompressed_size <<= 8;
16028 uncompressed_size += start[8]; uncompressed_size <<= 8;
16029 uncompressed_size += start[9]; uncompressed_size <<= 8;
16030 uncompressed_size += start[10]; uncompressed_size <<= 8;
16031 uncompressed_size += start[11];
16032 start += 12;
16033 size -= 12;
16034 }
16035
16036 if (uncompressed_size)
16037 {
16038 if (uncompress_section_contents (is_zstd, &start, uncompressed_size,
16039 &size))
16040 {
16041 /* Free the compressed buffer, update the section buffer
16042 and the section size if uncompress is successful. */
16043 free (section->start);
16044 section->start = start;
16045 }
16046 else
16047 {
16048 error (_("Unable to decompress section %s\n"),
16049 printable_section_name (filedata, sec));
16050 return false;
16051 }
16052 }
16053
16054 section->size = size;
16055 }
16056
16057 if (section->start == NULL)
16058 return false;
16059
16060 if (debug_displays [debug].relocate)
16061 {
16062 if (! apply_relocations (filedata, sec, section->start, section->size,
16063 & section->reloc_info, & section->num_relocs))
16064 return false;
16065 }
16066 else
16067 {
16068 section->reloc_info = NULL;
16069 section->num_relocs = 0;
16070 }
16071
16072 return true;
16073 }
16074
16075 #if HAVE_LIBDEBUGINFOD
16076 /* Return a hex string representation of the build-id. */
16077 unsigned char *
16078 get_build_id (void * data)
16079 {
16080 Filedata * filedata = (Filedata *) data;
16081 Elf_Internal_Shdr * shdr;
16082 unsigned long i;
16083
16084 /* Iterate through notes to find note.gnu.build-id.
16085 FIXME: Only the first note in any note section is examined. */
16086 for (i = 0, shdr = filedata->section_headers;
16087 i < filedata->file_header.e_shnum && shdr != NULL;
16088 i++, shdr++)
16089 {
16090 if (shdr->sh_type != SHT_NOTE)
16091 continue;
16092
16093 char * next;
16094 char * end;
16095 size_t data_remaining;
16096 size_t min_notesz;
16097 Elf_External_Note * enote;
16098 Elf_Internal_Note inote;
16099
16100 uint64_t offset = shdr->sh_offset;
16101 uint64_t align = shdr->sh_addralign;
16102 uint64_t length = shdr->sh_size;
16103
16104 enote = (Elf_External_Note *) get_section_contents (shdr, filedata);
16105 if (enote == NULL)
16106 continue;
16107
16108 if (align < 4)
16109 align = 4;
16110 else if (align != 4 && align != 8)
16111 {
16112 free (enote);
16113 continue;
16114 }
16115
16116 end = (char *) enote + length;
16117 data_remaining = end - (char *) enote;
16118
16119 if (!is_ia64_vms (filedata))
16120 {
16121 min_notesz = offsetof (Elf_External_Note, name);
16122 if (data_remaining < min_notesz)
16123 {
16124 warn (_("\
16125 malformed note encountered in section %s whilst scanning for build-id note\n"),
16126 printable_section_name (filedata, shdr));
16127 free (enote);
16128 continue;
16129 }
16130 data_remaining -= min_notesz;
16131
16132 inote.type = BYTE_GET (enote->type);
16133 inote.namesz = BYTE_GET (enote->namesz);
16134 inote.namedata = enote->name;
16135 inote.descsz = BYTE_GET (enote->descsz);
16136 inote.descdata = ((char *) enote
16137 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
16138 inote.descpos = offset + (inote.descdata - (char *) enote);
16139 next = ((char *) enote
16140 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
16141 }
16142 else
16143 {
16144 Elf64_External_VMS_Note *vms_enote;
16145
16146 /* PR binutils/15191
16147 Make sure that there is enough data to read. */
16148 min_notesz = offsetof (Elf64_External_VMS_Note, name);
16149 if (data_remaining < min_notesz)
16150 {
16151 warn (_("\
16152 malformed note encountered in section %s whilst scanning for build-id note\n"),
16153 printable_section_name (filedata, shdr));
16154 free (enote);
16155 continue;
16156 }
16157 data_remaining -= min_notesz;
16158
16159 vms_enote = (Elf64_External_VMS_Note *) enote;
16160 inote.type = BYTE_GET (vms_enote->type);
16161 inote.namesz = BYTE_GET (vms_enote->namesz);
16162 inote.namedata = vms_enote->name;
16163 inote.descsz = BYTE_GET (vms_enote->descsz);
16164 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
16165 inote.descpos = offset + (inote.descdata - (char *) enote);
16166 next = inote.descdata + align_power (inote.descsz, 3);
16167 }
16168
16169 /* Skip malformed notes. */
16170 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
16171 || (size_t) (inote.descdata - inote.namedata) > data_remaining
16172 || (size_t) (next - inote.descdata) < inote.descsz
16173 || ((size_t) (next - inote.descdata)
16174 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
16175 {
16176 warn (_("\
16177 malformed note encountered in section %s whilst scanning for build-id note\n"),
16178 printable_section_name (filedata, shdr));
16179 free (enote);
16180 continue;
16181 }
16182
16183 /* Check if this is the build-id note. If so then convert the build-id
16184 bytes to a hex string. */
16185 if (inote.namesz > 0
16186 && startswith (inote.namedata, "GNU")
16187 && inote.type == NT_GNU_BUILD_ID)
16188 {
16189 unsigned long j;
16190 char * build_id;
16191
16192 build_id = malloc (inote.descsz * 2 + 1);
16193 if (build_id == NULL)
16194 {
16195 free (enote);
16196 return NULL;
16197 }
16198
16199 for (j = 0; j < inote.descsz; ++j)
16200 sprintf (build_id + (j * 2), "%02x", inote.descdata[j] & 0xff);
16201 build_id[inote.descsz * 2] = '\0';
16202 free (enote);
16203
16204 return (unsigned char *) build_id;
16205 }
16206 free (enote);
16207 }
16208
16209 return NULL;
16210 }
16211 #endif /* HAVE_LIBDEBUGINFOD */
16212
16213 /* If this is not NULL, load_debug_section will only look for sections
16214 within the list of sections given here. */
16215 static unsigned int * section_subset = NULL;
16216
16217 bool
16218 load_debug_section (enum dwarf_section_display_enum debug, void * data)
16219 {
16220 struct dwarf_section * section = &debug_displays [debug].section;
16221 Elf_Internal_Shdr * sec;
16222 Filedata * filedata = (Filedata *) data;
16223
16224 if (!dump_any_debugging)
16225 return false;
16226
16227 /* Without section headers we cannot find any sections. */
16228 if (filedata->section_headers == NULL)
16229 return false;
16230
16231 if (filedata->string_table == NULL
16232 && filedata->file_header.e_shstrndx != SHN_UNDEF
16233 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
16234 {
16235 Elf_Internal_Shdr * strs;
16236
16237 /* Read in the string table, so that we have section names to scan. */
16238 strs = filedata->section_headers + filedata->file_header.e_shstrndx;
16239
16240 if (strs != NULL && strs->sh_size != 0)
16241 {
16242 filedata->string_table
16243 = (char *) get_data (NULL, filedata, strs->sh_offset,
16244 1, strs->sh_size, _("string table"));
16245
16246 filedata->string_table_length
16247 = filedata->string_table != NULL ? strs->sh_size : 0;
16248 }
16249 }
16250
16251 /* Locate the debug section. */
16252 sec = find_section_in_set (filedata, section->uncompressed_name, section_subset);
16253 if (sec != NULL)
16254 section->name = section->uncompressed_name;
16255 else
16256 {
16257 sec = find_section_in_set (filedata, section->compressed_name, section_subset);
16258 if (sec != NULL)
16259 section->name = section->compressed_name;
16260 }
16261 if (sec == NULL)
16262 return false;
16263
16264 /* If we're loading from a subset of sections, and we've loaded
16265 a section matching this name before, it's likely that it's a
16266 different one. */
16267 if (section_subset != NULL)
16268 free_debug_section (debug);
16269
16270 return load_specific_debug_section (debug, sec, data);
16271 }
16272
16273 void
16274 free_debug_section (enum dwarf_section_display_enum debug)
16275 {
16276 struct dwarf_section * section = &debug_displays [debug].section;
16277
16278 if (section->start == NULL)
16279 return;
16280
16281 free ((char *) section->start);
16282 section->start = NULL;
16283 section->address = 0;
16284 section->size = 0;
16285
16286 free (section->reloc_info);
16287 section->reloc_info = NULL;
16288 section->num_relocs = 0;
16289 }
16290
16291 static bool
16292 display_debug_section (int shndx, Elf_Internal_Shdr * section, Filedata * filedata)
16293 {
16294 const char *name = (section_name_valid (filedata, section)
16295 ? section_name (filedata, section) : "");
16296 const char *print_name = printable_section_name (filedata, section);
16297 uint64_t length;
16298 bool result = true;
16299 int i;
16300
16301 length = section->sh_size;
16302 if (length == 0)
16303 {
16304 printf (_("\nSection '%s' has no debugging data.\n"), print_name);
16305 return true;
16306 }
16307 if (section->sh_type == SHT_NOBITS)
16308 {
16309 /* There is no point in dumping the contents of a debugging section
16310 which has the NOBITS type - the bits in the file will be random.
16311 This can happen when a file containing a .eh_frame section is
16312 stripped with the --only-keep-debug command line option. */
16313 printf (_("section '%s' has the NOBITS type - its contents are unreliable.\n"),
16314 print_name);
16315 return false;
16316 }
16317
16318 if (startswith (name, ".gnu.linkonce.wi."))
16319 name = ".debug_info";
16320
16321 /* See if we know how to display the contents of this section. */
16322 for (i = 0; i < max; i++)
16323 {
16324 enum dwarf_section_display_enum id = (enum dwarf_section_display_enum) i;
16325 struct dwarf_section_display * display = debug_displays + i;
16326 struct dwarf_section * sec = & display->section;
16327
16328 if (streq (sec->uncompressed_name, name)
16329 || (id == line && startswith (name, ".debug_line."))
16330 || streq (sec->compressed_name, name))
16331 {
16332 bool secondary = (section != find_section (filedata, name));
16333
16334 if (secondary)
16335 free_debug_section (id);
16336
16337 if (i == line && startswith (name, ".debug_line."))
16338 sec->name = name;
16339 else if (streq (sec->uncompressed_name, name))
16340 sec->name = sec->uncompressed_name;
16341 else
16342 sec->name = sec->compressed_name;
16343
16344 if (load_specific_debug_section (id, section, filedata))
16345 {
16346 /* If this debug section is part of a CU/TU set in a .dwp file,
16347 restrict load_debug_section to the sections in that set. */
16348 section_subset = find_cu_tu_set (filedata, shndx);
16349
16350 result &= display->display (sec, filedata);
16351
16352 section_subset = NULL;
16353
16354 if (secondary || (id != info && id != abbrev && id != debug_addr))
16355 free_debug_section (id);
16356 }
16357 break;
16358 }
16359 }
16360
16361 if (i == max)
16362 {
16363 printf (_("Unrecognized debug section: %s\n"), print_name);
16364 result = false;
16365 }
16366
16367 return result;
16368 }
16369
16370 /* Set DUMP_SECTS for all sections where dumps were requested
16371 based on section name. */
16372
16373 static void
16374 initialise_dumps_byname (Filedata * filedata)
16375 {
16376 struct dump_list_entry * cur;
16377
16378 for (cur = dump_sects_byname; cur; cur = cur->next)
16379 {
16380 unsigned int i;
16381 bool any = false;
16382
16383 for (i = 0; i < filedata->file_header.e_shnum; i++)
16384 if (section_name_valid (filedata, filedata->section_headers + i)
16385 && streq (section_name (filedata, filedata->section_headers + i),
16386 cur->name))
16387 {
16388 request_dump_bynumber (&filedata->dump, i, cur->type);
16389 any = true;
16390 }
16391
16392 if (!any && !filedata->is_separate)
16393 warn (_("Section '%s' was not dumped because it does not exist\n"),
16394 cur->name);
16395 }
16396 }
16397
16398 static bool
16399 process_section_contents (Filedata * filedata)
16400 {
16401 Elf_Internal_Shdr * section;
16402 unsigned int i;
16403 bool res = true;
16404
16405 if (! do_dump)
16406 return true;
16407
16408 initialise_dumps_byname (filedata);
16409
16410 for (i = 0, section = filedata->section_headers;
16411 i < filedata->file_header.e_shnum && i < filedata->dump.num_dump_sects;
16412 i++, section++)
16413 {
16414 dump_type dump = filedata->dump.dump_sects[i];
16415
16416 if (filedata->is_separate && ! process_links)
16417 dump &= DEBUG_DUMP;
16418
16419 #ifdef SUPPORT_DISASSEMBLY
16420 if (dump & DISASS_DUMP)
16421 {
16422 if (! disassemble_section (section, filedata))
16423 res = false;
16424 }
16425 #endif
16426 if (dump & HEX_DUMP)
16427 {
16428 if (! dump_section_as_bytes (section, filedata, false))
16429 res = false;
16430 }
16431
16432 if (dump & RELOC_DUMP)
16433 {
16434 if (! dump_section_as_bytes (section, filedata, true))
16435 res = false;
16436 }
16437
16438 if (dump & STRING_DUMP)
16439 {
16440 if (! dump_section_as_strings (section, filedata))
16441 res = false;
16442 }
16443
16444 if (dump & DEBUG_DUMP)
16445 {
16446 if (! display_debug_section (i, section, filedata))
16447 res = false;
16448 }
16449
16450 #ifdef ENABLE_LIBCTF
16451 if (dump & CTF_DUMP)
16452 {
16453 if (! dump_section_as_ctf (section, filedata))
16454 res = false;
16455 }
16456 #endif
16457 if (dump & SFRAME_DUMP)
16458 {
16459 if (! dump_section_as_sframe (section, filedata))
16460 res = false;
16461 }
16462 }
16463
16464 if (! filedata->is_separate)
16465 {
16466 /* Check to see if the user requested a
16467 dump of a section that does not exist. */
16468 for (; i < filedata->dump.num_dump_sects; i++)
16469 if (filedata->dump.dump_sects[i])
16470 {
16471 warn (_("Section %d was not dumped because it does not exist!\n"), i);
16472 res = false;
16473 }
16474 }
16475
16476 return res;
16477 }
16478
16479 static void
16480 process_mips_fpe_exception (int mask)
16481 {
16482 if (mask)
16483 {
16484 bool first = true;
16485
16486 if (mask & OEX_FPU_INEX)
16487 fputs ("INEX", stdout), first = false;
16488 if (mask & OEX_FPU_UFLO)
16489 printf ("%sUFLO", first ? "" : "|"), first = false;
16490 if (mask & OEX_FPU_OFLO)
16491 printf ("%sOFLO", first ? "" : "|"), first = false;
16492 if (mask & OEX_FPU_DIV0)
16493 printf ("%sDIV0", first ? "" : "|"), first = false;
16494 if (mask & OEX_FPU_INVAL)
16495 printf ("%sINVAL", first ? "" : "|");
16496 }
16497 else
16498 fputs ("0", stdout);
16499 }
16500
16501 /* Display's the value of TAG at location P. If TAG is
16502 greater than 0 it is assumed to be an unknown tag, and
16503 a message is printed to this effect. Otherwise it is
16504 assumed that a message has already been printed.
16505
16506 If the bottom bit of TAG is set it assumed to have a
16507 string value, otherwise it is assumed to have an integer
16508 value.
16509
16510 Returns an updated P pointing to the first unread byte
16511 beyond the end of TAG's value.
16512
16513 Reads at or beyond END will not be made. */
16514
16515 static unsigned char *
16516 display_tag_value (signed int tag,
16517 unsigned char * p,
16518 const unsigned char * const end)
16519 {
16520 unsigned long val;
16521
16522 if (tag > 0)
16523 printf (" Tag_unknown_%d: ", tag);
16524
16525 if (p >= end)
16526 {
16527 warn (_("<corrupt tag>\n"));
16528 }
16529 else if (tag & 1)
16530 {
16531 /* PR 17531 file: 027-19978-0.004. */
16532 size_t maxlen = (end - p) - 1;
16533
16534 putchar ('"');
16535 if (maxlen > 0)
16536 {
16537 print_symbol ((int) maxlen, (const char *) p);
16538 p += strnlen ((char *) p, maxlen) + 1;
16539 }
16540 else
16541 {
16542 printf (_("<corrupt string tag>"));
16543 p = (unsigned char *) end;
16544 }
16545 printf ("\"\n");
16546 }
16547 else
16548 {
16549 READ_ULEB (val, p, end);
16550 printf ("%ld (0x%lx)\n", val, val);
16551 }
16552
16553 assert (p <= end);
16554 return p;
16555 }
16556
16557 /* ARC ABI attributes section. */
16558
16559 static unsigned char *
16560 display_arc_attribute (unsigned char * p,
16561 const unsigned char * const end)
16562 {
16563 unsigned int tag;
16564 unsigned int val;
16565
16566 READ_ULEB (tag, p, end);
16567
16568 switch (tag)
16569 {
16570 case Tag_ARC_PCS_config:
16571 READ_ULEB (val, p, end);
16572 printf (" Tag_ARC_PCS_config: ");
16573 switch (val)
16574 {
16575 case 0:
16576 printf (_("Absent/Non standard\n"));
16577 break;
16578 case 1:
16579 printf (_("Bare metal/mwdt\n"));
16580 break;
16581 case 2:
16582 printf (_("Bare metal/newlib\n"));
16583 break;
16584 case 3:
16585 printf (_("Linux/uclibc\n"));
16586 break;
16587 case 4:
16588 printf (_("Linux/glibc\n"));
16589 break;
16590 default:
16591 printf (_("Unknown\n"));
16592 break;
16593 }
16594 break;
16595
16596 case Tag_ARC_CPU_base:
16597 READ_ULEB (val, p, end);
16598 printf (" Tag_ARC_CPU_base: ");
16599 switch (val)
16600 {
16601 default:
16602 case TAG_CPU_NONE:
16603 printf (_("Absent\n"));
16604 break;
16605 case TAG_CPU_ARC6xx:
16606 printf ("ARC6xx\n");
16607 break;
16608 case TAG_CPU_ARC7xx:
16609 printf ("ARC7xx\n");
16610 break;
16611 case TAG_CPU_ARCEM:
16612 printf ("ARCEM\n");
16613 break;
16614 case TAG_CPU_ARCHS:
16615 printf ("ARCHS\n");
16616 break;
16617 }
16618 break;
16619
16620 case Tag_ARC_CPU_variation:
16621 READ_ULEB (val, p, end);
16622 printf (" Tag_ARC_CPU_variation: ");
16623 switch (val)
16624 {
16625 default:
16626 if (val > 0 && val < 16)
16627 printf ("Core%d\n", val);
16628 else
16629 printf ("Unknown\n");
16630 break;
16631
16632 case 0:
16633 printf (_("Absent\n"));
16634 break;
16635 }
16636 break;
16637
16638 case Tag_ARC_CPU_name:
16639 printf (" Tag_ARC_CPU_name: ");
16640 p = display_tag_value (-1, p, end);
16641 break;
16642
16643 case Tag_ARC_ABI_rf16:
16644 READ_ULEB (val, p, end);
16645 printf (" Tag_ARC_ABI_rf16: %s\n", val ? _("yes") : _("no"));
16646 break;
16647
16648 case Tag_ARC_ABI_osver:
16649 READ_ULEB (val, p, end);
16650 printf (" Tag_ARC_ABI_osver: v%d\n", val);
16651 break;
16652
16653 case Tag_ARC_ABI_pic:
16654 case Tag_ARC_ABI_sda:
16655 READ_ULEB (val, p, end);
16656 printf (tag == Tag_ARC_ABI_sda ? " Tag_ARC_ABI_sda: "
16657 : " Tag_ARC_ABI_pic: ");
16658 switch (val)
16659 {
16660 case 0:
16661 printf (_("Absent\n"));
16662 break;
16663 case 1:
16664 printf ("MWDT\n");
16665 break;
16666 case 2:
16667 printf ("GNU\n");
16668 break;
16669 default:
16670 printf (_("Unknown\n"));
16671 break;
16672 }
16673 break;
16674
16675 case Tag_ARC_ABI_tls:
16676 READ_ULEB (val, p, end);
16677 printf (" Tag_ARC_ABI_tls: %s\n", val ? "r25": "none");
16678 break;
16679
16680 case Tag_ARC_ABI_enumsize:
16681 READ_ULEB (val, p, end);
16682 printf (" Tag_ARC_ABI_enumsize: %s\n", val ? _("default") :
16683 _("smallest"));
16684 break;
16685
16686 case Tag_ARC_ABI_exceptions:
16687 READ_ULEB (val, p, end);
16688 printf (" Tag_ARC_ABI_exceptions: %s\n", val ? _("OPTFP")
16689 : _("default"));
16690 break;
16691
16692 case Tag_ARC_ABI_double_size:
16693 READ_ULEB (val, p, end);
16694 printf (" Tag_ARC_ABI_double_size: %d\n", val);
16695 break;
16696
16697 case Tag_ARC_ISA_config:
16698 printf (" Tag_ARC_ISA_config: ");
16699 p = display_tag_value (-1, p, end);
16700 break;
16701
16702 case Tag_ARC_ISA_apex:
16703 printf (" Tag_ARC_ISA_apex: ");
16704 p = display_tag_value (-1, p, end);
16705 break;
16706
16707 case Tag_ARC_ISA_mpy_option:
16708 READ_ULEB (val, p, end);
16709 printf (" Tag_ARC_ISA_mpy_option: %d\n", val);
16710 break;
16711
16712 case Tag_ARC_ATR_version:
16713 READ_ULEB (val, p, end);
16714 printf (" Tag_ARC_ATR_version: %d\n", val);
16715 break;
16716
16717 default:
16718 return display_tag_value (tag & 1, p, end);
16719 }
16720
16721 return p;
16722 }
16723
16724 /* ARM EABI attributes section. */
16725 typedef struct
16726 {
16727 unsigned int tag;
16728 const char * name;
16729 /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup. */
16730 unsigned int type;
16731 const char *const *table;
16732 } arm_attr_public_tag;
16733
16734 static const char *const arm_attr_tag_CPU_arch[] =
16735 {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
16736 "v6K", "v7", "v6-M", "v6S-M", "v7E-M", "v8", "v8-R", "v8-M.baseline",
16737 "v8-M.mainline", "v8.1-A", "v8.2-A", "v8.3-A",
16738 "v8.1-M.mainline", "v9"};
16739 static const char *const arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
16740 static const char *const arm_attr_tag_THUMB_ISA_use[] =
16741 {"No", "Thumb-1", "Thumb-2", "Yes"};
16742 static const char *const arm_attr_tag_FP_arch[] =
16743 {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16", "VFPv4", "VFPv4-D16",
16744 "FP for ARMv8", "FPv5/FP-D16 for ARMv8"};
16745 static const char *const arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
16746 static const char *const arm_attr_tag_Advanced_SIMD_arch[] =
16747 {"No", "NEONv1", "NEONv1 with Fused-MAC", "NEON for ARMv8",
16748 "NEON for ARMv8.1"};
16749 static const char *const arm_attr_tag_PCS_config[] =
16750 {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
16751 "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
16752 static const char *const arm_attr_tag_ABI_PCS_R9_use[] =
16753 {"V6", "SB", "TLS", "Unused"};
16754 static const char *const arm_attr_tag_ABI_PCS_RW_data[] =
16755 {"Absolute", "PC-relative", "SB-relative", "None"};
16756 static const char *const arm_attr_tag_ABI_PCS_RO_data[] =
16757 {"Absolute", "PC-relative", "None"};
16758 static const char *const arm_attr_tag_ABI_PCS_GOT_use[] =
16759 {"None", "direct", "GOT-indirect"};
16760 static const char *const arm_attr_tag_ABI_PCS_wchar_t[] =
16761 {"None", "??? 1", "2", "??? 3", "4"};
16762 static const char *const arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
16763 static const char *const arm_attr_tag_ABI_FP_denormal[] =
16764 {"Unused", "Needed", "Sign only"};
16765 static const char *const arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
16766 static const char *const arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
16767 static const char *const arm_attr_tag_ABI_FP_number_model[] =
16768 {"Unused", "Finite", "RTABI", "IEEE 754"};
16769 static const char *const arm_attr_tag_ABI_enum_size[] =
16770 {"Unused", "small", "int", "forced to int"};
16771 static const char *const arm_attr_tag_ABI_HardFP_use[] =
16772 {"As Tag_FP_arch", "SP only", "Reserved", "Deprecated"};
16773 static const char *const arm_attr_tag_ABI_VFP_args[] =
16774 {"AAPCS", "VFP registers", "custom", "compatible"};
16775 static const char *const arm_attr_tag_ABI_WMMX_args[] =
16776 {"AAPCS", "WMMX registers", "custom"};
16777 static const char *const arm_attr_tag_ABI_optimization_goals[] =
16778 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
16779 "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
16780 static const char *const arm_attr_tag_ABI_FP_optimization_goals[] =
16781 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
16782 "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
16783 static const char *const arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
16784 static const char *const arm_attr_tag_FP_HP_extension[] =
16785 {"Not Allowed", "Allowed"};
16786 static const char *const arm_attr_tag_ABI_FP_16bit_format[] =
16787 {"None", "IEEE 754", "Alternative Format"};
16788 static const char *const arm_attr_tag_DSP_extension[] =
16789 {"Follow architecture", "Allowed"};
16790 static const char *const arm_attr_tag_MPextension_use[] =
16791 {"Not Allowed", "Allowed"};
16792 static const char *const arm_attr_tag_DIV_use[] =
16793 {"Allowed in Thumb-ISA, v7-R or v7-M", "Not allowed",
16794 "Allowed in v7-A with integer division extension"};
16795 static const char *const arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
16796 static const char *const arm_attr_tag_Virtualization_use[] =
16797 {"Not Allowed", "TrustZone", "Virtualization Extensions",
16798 "TrustZone and Virtualization Extensions"};
16799 static const char *const arm_attr_tag_MPextension_use_legacy[] =
16800 {"Not Allowed", "Allowed"};
16801
16802 static const char *const arm_attr_tag_MVE_arch[] =
16803 {"No MVE", "MVE Integer only", "MVE Integer and FP"};
16804
16805 static const char * arm_attr_tag_PAC_extension[] =
16806 {"No PAC/AUT instructions",
16807 "PAC/AUT instructions permitted in the NOP space",
16808 "PAC/AUT instructions permitted in the NOP and in the non-NOP space"};
16809
16810 static const char * arm_attr_tag_BTI_extension[] =
16811 {"BTI instructions not permitted",
16812 "BTI instructions permitted in the NOP space",
16813 "BTI instructions permitted in the NOP and in the non-NOP space"};
16814
16815 static const char * arm_attr_tag_BTI_use[] =
16816 {"Compiled without branch target enforcement",
16817 "Compiled with branch target enforcement"};
16818
16819 static const char * arm_attr_tag_PACRET_use[] =
16820 {"Compiled without return address signing and authentication",
16821 "Compiled with return address signing and authentication"};
16822
16823 #define LOOKUP(id, name) \
16824 {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
16825 static arm_attr_public_tag arm_attr_public_tags[] =
16826 {
16827 {4, "CPU_raw_name", 1, NULL},
16828 {5, "CPU_name", 1, NULL},
16829 LOOKUP(6, CPU_arch),
16830 {7, "CPU_arch_profile", 0, NULL},
16831 LOOKUP(8, ARM_ISA_use),
16832 LOOKUP(9, THUMB_ISA_use),
16833 LOOKUP(10, FP_arch),
16834 LOOKUP(11, WMMX_arch),
16835 LOOKUP(12, Advanced_SIMD_arch),
16836 LOOKUP(13, PCS_config),
16837 LOOKUP(14, ABI_PCS_R9_use),
16838 LOOKUP(15, ABI_PCS_RW_data),
16839 LOOKUP(16, ABI_PCS_RO_data),
16840 LOOKUP(17, ABI_PCS_GOT_use),
16841 LOOKUP(18, ABI_PCS_wchar_t),
16842 LOOKUP(19, ABI_FP_rounding),
16843 LOOKUP(20, ABI_FP_denormal),
16844 LOOKUP(21, ABI_FP_exceptions),
16845 LOOKUP(22, ABI_FP_user_exceptions),
16846 LOOKUP(23, ABI_FP_number_model),
16847 {24, "ABI_align_needed", 0, NULL},
16848 {25, "ABI_align_preserved", 0, NULL},
16849 LOOKUP(26, ABI_enum_size),
16850 LOOKUP(27, ABI_HardFP_use),
16851 LOOKUP(28, ABI_VFP_args),
16852 LOOKUP(29, ABI_WMMX_args),
16853 LOOKUP(30, ABI_optimization_goals),
16854 LOOKUP(31, ABI_FP_optimization_goals),
16855 {32, "compatibility", 0, NULL},
16856 LOOKUP(34, CPU_unaligned_access),
16857 LOOKUP(36, FP_HP_extension),
16858 LOOKUP(38, ABI_FP_16bit_format),
16859 LOOKUP(42, MPextension_use),
16860 LOOKUP(44, DIV_use),
16861 LOOKUP(46, DSP_extension),
16862 LOOKUP(48, MVE_arch),
16863 LOOKUP(50, PAC_extension),
16864 LOOKUP(52, BTI_extension),
16865 LOOKUP(74, BTI_use),
16866 LOOKUP(76, PACRET_use),
16867 {64, "nodefaults", 0, NULL},
16868 {65, "also_compatible_with", 0, NULL},
16869 LOOKUP(66, T2EE_use),
16870 {67, "conformance", 1, NULL},
16871 LOOKUP(68, Virtualization_use),
16872 LOOKUP(70, MPextension_use_legacy)
16873 };
16874 #undef LOOKUP
16875
16876 static unsigned char *
16877 display_arm_attribute (unsigned char * p,
16878 const unsigned char * const end)
16879 {
16880 unsigned int tag;
16881 unsigned int val;
16882 arm_attr_public_tag * attr;
16883 unsigned i;
16884 unsigned int type;
16885
16886 READ_ULEB (tag, p, end);
16887 attr = NULL;
16888 for (i = 0; i < ARRAY_SIZE (arm_attr_public_tags); i++)
16889 {
16890 if (arm_attr_public_tags[i].tag == tag)
16891 {
16892 attr = &arm_attr_public_tags[i];
16893 break;
16894 }
16895 }
16896
16897 if (attr)
16898 {
16899 printf (" Tag_%s: ", attr->name);
16900 switch (attr->type)
16901 {
16902 case 0:
16903 switch (tag)
16904 {
16905 case 7: /* Tag_CPU_arch_profile. */
16906 READ_ULEB (val, p, end);
16907 switch (val)
16908 {
16909 case 0: printf (_("None\n")); break;
16910 case 'A': printf (_("Application\n")); break;
16911 case 'R': printf (_("Realtime\n")); break;
16912 case 'M': printf (_("Microcontroller\n")); break;
16913 case 'S': printf (_("Application or Realtime\n")); break;
16914 default: printf ("??? (%d)\n", val); break;
16915 }
16916 break;
16917
16918 case 24: /* Tag_align_needed. */
16919 READ_ULEB (val, p, end);
16920 switch (val)
16921 {
16922 case 0: printf (_("None\n")); break;
16923 case 1: printf (_("8-byte\n")); break;
16924 case 2: printf (_("4-byte\n")); break;
16925 case 3: printf ("??? 3\n"); break;
16926 default:
16927 if (val <= 12)
16928 printf (_("8-byte and up to %d-byte extended\n"),
16929 1 << val);
16930 else
16931 printf ("??? (%d)\n", val);
16932 break;
16933 }
16934 break;
16935
16936 case 25: /* Tag_align_preserved. */
16937 READ_ULEB (val, p, end);
16938 switch (val)
16939 {
16940 case 0: printf (_("None\n")); break;
16941 case 1: printf (_("8-byte, except leaf SP\n")); break;
16942 case 2: printf (_("8-byte\n")); break;
16943 case 3: printf ("??? 3\n"); break;
16944 default:
16945 if (val <= 12)
16946 printf (_("8-byte and up to %d-byte extended\n"),
16947 1 << val);
16948 else
16949 printf ("??? (%d)\n", val);
16950 break;
16951 }
16952 break;
16953
16954 case 32: /* Tag_compatibility. */
16955 {
16956 READ_ULEB (val, p, end);
16957 printf (_("flag = %d, vendor = "), val);
16958 if (p < end - 1)
16959 {
16960 size_t maxlen = (end - p) - 1;
16961
16962 print_symbol ((int) maxlen, (const char *) p);
16963 p += strnlen ((char *) p, maxlen) + 1;
16964 }
16965 else
16966 {
16967 printf (_("<corrupt>"));
16968 p = (unsigned char *) end;
16969 }
16970 putchar ('\n');
16971 }
16972 break;
16973
16974 case 64: /* Tag_nodefaults. */
16975 /* PR 17531: file: 001-505008-0.01. */
16976 if (p < end)
16977 p++;
16978 printf (_("True\n"));
16979 break;
16980
16981 case 65: /* Tag_also_compatible_with. */
16982 READ_ULEB (val, p, end);
16983 if (val == 6 /* Tag_CPU_arch. */)
16984 {
16985 READ_ULEB (val, p, end);
16986 if ((unsigned int) val >= ARRAY_SIZE (arm_attr_tag_CPU_arch))
16987 printf ("??? (%d)\n", val);
16988 else
16989 printf ("%s\n", arm_attr_tag_CPU_arch[val]);
16990 }
16991 else
16992 printf ("???\n");
16993 while (p < end && *(p++) != '\0' /* NUL terminator. */)
16994 ;
16995 break;
16996
16997 default:
16998 printf (_("<unknown: %d>\n"), tag);
16999 break;
17000 }
17001 return p;
17002
17003 case 1:
17004 return display_tag_value (-1, p, end);
17005 case 2:
17006 return display_tag_value (0, p, end);
17007
17008 default:
17009 assert (attr->type & 0x80);
17010 READ_ULEB (val, p, end);
17011 type = attr->type & 0x7f;
17012 if (val >= type)
17013 printf ("??? (%d)\n", val);
17014 else
17015 printf ("%s\n", attr->table[val]);
17016 return p;
17017 }
17018 }
17019
17020 return display_tag_value (tag, p, end);
17021 }
17022
17023 static unsigned char *
17024 display_gnu_attribute (unsigned char * p,
17025 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const),
17026 const unsigned char * const end)
17027 {
17028 unsigned int tag;
17029 unsigned int val;
17030
17031 READ_ULEB (tag, p, end);
17032
17033 /* Tag_compatibility is the only generic GNU attribute defined at
17034 present. */
17035 if (tag == 32)
17036 {
17037 READ_ULEB (val, p, end);
17038
17039 printf (_("flag = %d, vendor = "), val);
17040 if (p == end)
17041 {
17042 printf (_("<corrupt>\n"));
17043 warn (_("corrupt vendor attribute\n"));
17044 }
17045 else
17046 {
17047 if (p < end - 1)
17048 {
17049 size_t maxlen = (end - p) - 1;
17050
17051 print_symbol ((int) maxlen, (const char *) p);
17052 p += strnlen ((char *) p, maxlen) + 1;
17053 }
17054 else
17055 {
17056 printf (_("<corrupt>"));
17057 p = (unsigned char *) end;
17058 }
17059 putchar ('\n');
17060 }
17061 return p;
17062 }
17063
17064 if ((tag & 2) == 0 && display_proc_gnu_attribute)
17065 return display_proc_gnu_attribute (p, tag, end);
17066
17067 return display_tag_value (tag, p, end);
17068 }
17069
17070 static unsigned char *
17071 display_m68k_gnu_attribute (unsigned char * p,
17072 unsigned int tag,
17073 const unsigned char * const end)
17074 {
17075 unsigned int val;
17076
17077 if (tag == Tag_GNU_M68K_ABI_FP)
17078 {
17079 printf (" Tag_GNU_M68K_ABI_FP: ");
17080 if (p == end)
17081 {
17082 printf (_("<corrupt>\n"));
17083 return p;
17084 }
17085 READ_ULEB (val, p, end);
17086
17087 if (val > 3)
17088 printf ("(%#x), ", val);
17089
17090 switch (val & 3)
17091 {
17092 case 0:
17093 printf (_("unspecified hard/soft float\n"));
17094 break;
17095 case 1:
17096 printf (_("hard float\n"));
17097 break;
17098 case 2:
17099 printf (_("soft float\n"));
17100 break;
17101 }
17102 return p;
17103 }
17104
17105 return display_tag_value (tag & 1, p, end);
17106 }
17107
17108 static unsigned char *
17109 display_power_gnu_attribute (unsigned char * p,
17110 unsigned int tag,
17111 const unsigned char * const end)
17112 {
17113 unsigned int val;
17114
17115 if (tag == Tag_GNU_Power_ABI_FP)
17116 {
17117 printf (" Tag_GNU_Power_ABI_FP: ");
17118 if (p == end)
17119 {
17120 printf (_("<corrupt>\n"));
17121 return p;
17122 }
17123 READ_ULEB (val, p, end);
17124
17125 if (val > 15)
17126 printf ("(%#x), ", val);
17127
17128 switch (val & 3)
17129 {
17130 case 0:
17131 printf (_("unspecified hard/soft float, "));
17132 break;
17133 case 1:
17134 printf (_("hard float, "));
17135 break;
17136 case 2:
17137 printf (_("soft float, "));
17138 break;
17139 case 3:
17140 printf (_("single-precision hard float, "));
17141 break;
17142 }
17143
17144 switch (val & 0xC)
17145 {
17146 case 0:
17147 printf (_("unspecified long double\n"));
17148 break;
17149 case 4:
17150 printf (_("128-bit IBM long double\n"));
17151 break;
17152 case 8:
17153 printf (_("64-bit long double\n"));
17154 break;
17155 case 12:
17156 printf (_("128-bit IEEE long double\n"));
17157 break;
17158 }
17159 return p;
17160 }
17161
17162 if (tag == Tag_GNU_Power_ABI_Vector)
17163 {
17164 printf (" Tag_GNU_Power_ABI_Vector: ");
17165 if (p == end)
17166 {
17167 printf (_("<corrupt>\n"));
17168 return p;
17169 }
17170 READ_ULEB (val, p, end);
17171
17172 if (val > 3)
17173 printf ("(%#x), ", val);
17174
17175 switch (val & 3)
17176 {
17177 case 0:
17178 printf (_("unspecified\n"));
17179 break;
17180 case 1:
17181 printf (_("generic\n"));
17182 break;
17183 case 2:
17184 printf ("AltiVec\n");
17185 break;
17186 case 3:
17187 printf ("SPE\n");
17188 break;
17189 }
17190 return p;
17191 }
17192
17193 if (tag == Tag_GNU_Power_ABI_Struct_Return)
17194 {
17195 printf (" Tag_GNU_Power_ABI_Struct_Return: ");
17196 if (p == end)
17197 {
17198 printf (_("<corrupt>\n"));
17199 return p;
17200 }
17201 READ_ULEB (val, p, end);
17202
17203 if (val > 2)
17204 printf ("(%#x), ", val);
17205
17206 switch (val & 3)
17207 {
17208 case 0:
17209 printf (_("unspecified\n"));
17210 break;
17211 case 1:
17212 printf ("r3/r4\n");
17213 break;
17214 case 2:
17215 printf (_("memory\n"));
17216 break;
17217 case 3:
17218 printf ("???\n");
17219 break;
17220 }
17221 return p;
17222 }
17223
17224 return display_tag_value (tag & 1, p, end);
17225 }
17226
17227 static unsigned char *
17228 display_s390_gnu_attribute (unsigned char * p,
17229 unsigned int tag,
17230 const unsigned char * const end)
17231 {
17232 unsigned int val;
17233
17234 if (tag == Tag_GNU_S390_ABI_Vector)
17235 {
17236 printf (" Tag_GNU_S390_ABI_Vector: ");
17237 READ_ULEB (val, p, end);
17238
17239 switch (val)
17240 {
17241 case 0:
17242 printf (_("any\n"));
17243 break;
17244 case 1:
17245 printf (_("software\n"));
17246 break;
17247 case 2:
17248 printf (_("hardware\n"));
17249 break;
17250 default:
17251 printf ("??? (%d)\n", val);
17252 break;
17253 }
17254 return p;
17255 }
17256
17257 return display_tag_value (tag & 1, p, end);
17258 }
17259
17260 static void
17261 display_sparc_hwcaps (unsigned int mask)
17262 {
17263 if (mask)
17264 {
17265 bool first = true;
17266
17267 if (mask & ELF_SPARC_HWCAP_MUL32)
17268 fputs ("mul32", stdout), first = false;
17269 if (mask & ELF_SPARC_HWCAP_DIV32)
17270 printf ("%sdiv32", first ? "" : "|"), first = false;
17271 if (mask & ELF_SPARC_HWCAP_FSMULD)
17272 printf ("%sfsmuld", first ? "" : "|"), first = false;
17273 if (mask & ELF_SPARC_HWCAP_V8PLUS)
17274 printf ("%sv8plus", first ? "" : "|"), first = false;
17275 if (mask & ELF_SPARC_HWCAP_POPC)
17276 printf ("%spopc", first ? "" : "|"), first = false;
17277 if (mask & ELF_SPARC_HWCAP_VIS)
17278 printf ("%svis", first ? "" : "|"), first = false;
17279 if (mask & ELF_SPARC_HWCAP_VIS2)
17280 printf ("%svis2", first ? "" : "|"), first = false;
17281 if (mask & ELF_SPARC_HWCAP_ASI_BLK_INIT)
17282 printf ("%sASIBlkInit", first ? "" : "|"), first = false;
17283 if (mask & ELF_SPARC_HWCAP_FMAF)
17284 printf ("%sfmaf", first ? "" : "|"), first = false;
17285 if (mask & ELF_SPARC_HWCAP_VIS3)
17286 printf ("%svis3", first ? "" : "|"), first = false;
17287 if (mask & ELF_SPARC_HWCAP_HPC)
17288 printf ("%shpc", first ? "" : "|"), first = false;
17289 if (mask & ELF_SPARC_HWCAP_RANDOM)
17290 printf ("%srandom", first ? "" : "|"), first = false;
17291 if (mask & ELF_SPARC_HWCAP_TRANS)
17292 printf ("%strans", first ? "" : "|"), first = false;
17293 if (mask & ELF_SPARC_HWCAP_FJFMAU)
17294 printf ("%sfjfmau", first ? "" : "|"), first = false;
17295 if (mask & ELF_SPARC_HWCAP_IMA)
17296 printf ("%sima", first ? "" : "|"), first = false;
17297 if (mask & ELF_SPARC_HWCAP_ASI_CACHE_SPARING)
17298 printf ("%scspare", first ? "" : "|"), first = false;
17299 }
17300 else
17301 fputc ('0', stdout);
17302 fputc ('\n', stdout);
17303 }
17304
17305 static void
17306 display_sparc_hwcaps2 (unsigned int mask)
17307 {
17308 if (mask)
17309 {
17310 bool first = true;
17311
17312 if (mask & ELF_SPARC_HWCAP2_FJATHPLUS)
17313 fputs ("fjathplus", stdout), first = false;
17314 if (mask & ELF_SPARC_HWCAP2_VIS3B)
17315 printf ("%svis3b", first ? "" : "|"), first = false;
17316 if (mask & ELF_SPARC_HWCAP2_ADP)
17317 printf ("%sadp", first ? "" : "|"), first = false;
17318 if (mask & ELF_SPARC_HWCAP2_SPARC5)
17319 printf ("%ssparc5", first ? "" : "|"), first = false;
17320 if (mask & ELF_SPARC_HWCAP2_MWAIT)
17321 printf ("%smwait", first ? "" : "|"), first = false;
17322 if (mask & ELF_SPARC_HWCAP2_XMPMUL)
17323 printf ("%sxmpmul", first ? "" : "|"), first = false;
17324 if (mask & ELF_SPARC_HWCAP2_XMONT)
17325 printf ("%sxmont2", first ? "" : "|"), first = false;
17326 if (mask & ELF_SPARC_HWCAP2_NSEC)
17327 printf ("%snsec", first ? "" : "|"), first = false;
17328 if (mask & ELF_SPARC_HWCAP2_FJATHHPC)
17329 printf ("%sfjathhpc", first ? "" : "|"), first = false;
17330 if (mask & ELF_SPARC_HWCAP2_FJDES)
17331 printf ("%sfjdes", first ? "" : "|"), first = false;
17332 if (mask & ELF_SPARC_HWCAP2_FJAES)
17333 printf ("%sfjaes", first ? "" : "|"), first = false;
17334 }
17335 else
17336 fputc ('0', stdout);
17337 fputc ('\n', stdout);
17338 }
17339
17340 static unsigned char *
17341 display_sparc_gnu_attribute (unsigned char * p,
17342 unsigned int tag,
17343 const unsigned char * const end)
17344 {
17345 unsigned int val;
17346
17347 if (tag == Tag_GNU_Sparc_HWCAPS)
17348 {
17349 READ_ULEB (val, p, end);
17350 printf (" Tag_GNU_Sparc_HWCAPS: ");
17351 display_sparc_hwcaps (val);
17352 return p;
17353 }
17354 if (tag == Tag_GNU_Sparc_HWCAPS2)
17355 {
17356 READ_ULEB (val, p, end);
17357 printf (" Tag_GNU_Sparc_HWCAPS2: ");
17358 display_sparc_hwcaps2 (val);
17359 return p;
17360 }
17361
17362 return display_tag_value (tag, p, end);
17363 }
17364
17365 static void
17366 print_mips_fp_abi_value (unsigned int val)
17367 {
17368 switch (val)
17369 {
17370 case Val_GNU_MIPS_ABI_FP_ANY:
17371 printf (_("Hard or soft float\n"));
17372 break;
17373 case Val_GNU_MIPS_ABI_FP_DOUBLE:
17374 printf (_("Hard float (double precision)\n"));
17375 break;
17376 case Val_GNU_MIPS_ABI_FP_SINGLE:
17377 printf (_("Hard float (single precision)\n"));
17378 break;
17379 case Val_GNU_MIPS_ABI_FP_SOFT:
17380 printf (_("Soft float\n"));
17381 break;
17382 case Val_GNU_MIPS_ABI_FP_OLD_64:
17383 printf (_("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
17384 break;
17385 case Val_GNU_MIPS_ABI_FP_XX:
17386 printf (_("Hard float (32-bit CPU, Any FPU)\n"));
17387 break;
17388 case Val_GNU_MIPS_ABI_FP_64:
17389 printf (_("Hard float (32-bit CPU, 64-bit FPU)\n"));
17390 break;
17391 case Val_GNU_MIPS_ABI_FP_64A:
17392 printf (_("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
17393 break;
17394 case Val_GNU_MIPS_ABI_FP_NAN2008:
17395 printf (_("NaN 2008 compatibility\n"));
17396 break;
17397 default:
17398 printf ("??? (%d)\n", val);
17399 break;
17400 }
17401 }
17402
17403 static unsigned char *
17404 display_mips_gnu_attribute (unsigned char * p,
17405 unsigned int tag,
17406 const unsigned char * const end)
17407 {
17408 if (tag == Tag_GNU_MIPS_ABI_FP)
17409 {
17410 unsigned int val;
17411
17412 printf (" Tag_GNU_MIPS_ABI_FP: ");
17413 READ_ULEB (val, p, end);
17414 print_mips_fp_abi_value (val);
17415 return p;
17416 }
17417
17418 if (tag == Tag_GNU_MIPS_ABI_MSA)
17419 {
17420 unsigned int val;
17421
17422 printf (" Tag_GNU_MIPS_ABI_MSA: ");
17423 READ_ULEB (val, p, end);
17424
17425 switch (val)
17426 {
17427 case Val_GNU_MIPS_ABI_MSA_ANY:
17428 printf (_("Any MSA or not\n"));
17429 break;
17430 case Val_GNU_MIPS_ABI_MSA_128:
17431 printf (_("128-bit MSA\n"));
17432 break;
17433 default:
17434 printf ("??? (%d)\n", val);
17435 break;
17436 }
17437 return p;
17438 }
17439
17440 return display_tag_value (tag & 1, p, end);
17441 }
17442
17443 static unsigned char *
17444 display_tic6x_attribute (unsigned char * p,
17445 const unsigned char * const end)
17446 {
17447 unsigned int tag;
17448 unsigned int val;
17449
17450 READ_ULEB (tag, p, end);
17451
17452 switch (tag)
17453 {
17454 case Tag_ISA:
17455 printf (" Tag_ISA: ");
17456 READ_ULEB (val, p, end);
17457
17458 switch (val)
17459 {
17460 case C6XABI_Tag_ISA_none:
17461 printf (_("None\n"));
17462 break;
17463 case C6XABI_Tag_ISA_C62X:
17464 printf ("C62x\n");
17465 break;
17466 case C6XABI_Tag_ISA_C67X:
17467 printf ("C67x\n");
17468 break;
17469 case C6XABI_Tag_ISA_C67XP:
17470 printf ("C67x+\n");
17471 break;
17472 case C6XABI_Tag_ISA_C64X:
17473 printf ("C64x\n");
17474 break;
17475 case C6XABI_Tag_ISA_C64XP:
17476 printf ("C64x+\n");
17477 break;
17478 case C6XABI_Tag_ISA_C674X:
17479 printf ("C674x\n");
17480 break;
17481 default:
17482 printf ("??? (%d)\n", val);
17483 break;
17484 }
17485 return p;
17486
17487 case Tag_ABI_wchar_t:
17488 printf (" Tag_ABI_wchar_t: ");
17489 READ_ULEB (val, p, end);
17490 switch (val)
17491 {
17492 case 0:
17493 printf (_("Not used\n"));
17494 break;
17495 case 1:
17496 printf (_("2 bytes\n"));
17497 break;
17498 case 2:
17499 printf (_("4 bytes\n"));
17500 break;
17501 default:
17502 printf ("??? (%d)\n", val);
17503 break;
17504 }
17505 return p;
17506
17507 case Tag_ABI_stack_align_needed:
17508 printf (" Tag_ABI_stack_align_needed: ");
17509 READ_ULEB (val, p, end);
17510 switch (val)
17511 {
17512 case 0:
17513 printf (_("8-byte\n"));
17514 break;
17515 case 1:
17516 printf (_("16-byte\n"));
17517 break;
17518 default:
17519 printf ("??? (%d)\n", val);
17520 break;
17521 }
17522 return p;
17523
17524 case Tag_ABI_stack_align_preserved:
17525 READ_ULEB (val, p, end);
17526 printf (" Tag_ABI_stack_align_preserved: ");
17527 switch (val)
17528 {
17529 case 0:
17530 printf (_("8-byte\n"));
17531 break;
17532 case 1:
17533 printf (_("16-byte\n"));
17534 break;
17535 default:
17536 printf ("??? (%d)\n", val);
17537 break;
17538 }
17539 return p;
17540
17541 case Tag_ABI_DSBT:
17542 READ_ULEB (val, p, end);
17543 printf (" Tag_ABI_DSBT: ");
17544 switch (val)
17545 {
17546 case 0:
17547 printf (_("DSBT addressing not used\n"));
17548 break;
17549 case 1:
17550 printf (_("DSBT addressing used\n"));
17551 break;
17552 default:
17553 printf ("??? (%d)\n", val);
17554 break;
17555 }
17556 return p;
17557
17558 case Tag_ABI_PID:
17559 READ_ULEB (val, p, end);
17560 printf (" Tag_ABI_PID: ");
17561 switch (val)
17562 {
17563 case 0:
17564 printf (_("Data addressing position-dependent\n"));
17565 break;
17566 case 1:
17567 printf (_("Data addressing position-independent, GOT near DP\n"));
17568 break;
17569 case 2:
17570 printf (_("Data addressing position-independent, GOT far from DP\n"));
17571 break;
17572 default:
17573 printf ("??? (%d)\n", val);
17574 break;
17575 }
17576 return p;
17577
17578 case Tag_ABI_PIC:
17579 READ_ULEB (val, p, end);
17580 printf (" Tag_ABI_PIC: ");
17581 switch (val)
17582 {
17583 case 0:
17584 printf (_("Code addressing position-dependent\n"));
17585 break;
17586 case 1:
17587 printf (_("Code addressing position-independent\n"));
17588 break;
17589 default:
17590 printf ("??? (%d)\n", val);
17591 break;
17592 }
17593 return p;
17594
17595 case Tag_ABI_array_object_alignment:
17596 READ_ULEB (val, p, end);
17597 printf (" Tag_ABI_array_object_alignment: ");
17598 switch (val)
17599 {
17600 case 0:
17601 printf (_("8-byte\n"));
17602 break;
17603 case 1:
17604 printf (_("4-byte\n"));
17605 break;
17606 case 2:
17607 printf (_("16-byte\n"));
17608 break;
17609 default:
17610 printf ("??? (%d)\n", val);
17611 break;
17612 }
17613 return p;
17614
17615 case Tag_ABI_array_object_align_expected:
17616 READ_ULEB (val, p, end);
17617 printf (" Tag_ABI_array_object_align_expected: ");
17618 switch (val)
17619 {
17620 case 0:
17621 printf (_("8-byte\n"));
17622 break;
17623 case 1:
17624 printf (_("4-byte\n"));
17625 break;
17626 case 2:
17627 printf (_("16-byte\n"));
17628 break;
17629 default:
17630 printf ("??? (%d)\n", val);
17631 break;
17632 }
17633 return p;
17634
17635 case Tag_ABI_compatibility:
17636 {
17637 READ_ULEB (val, p, end);
17638 printf (" Tag_ABI_compatibility: ");
17639 printf (_("flag = %d, vendor = "), val);
17640 if (p < end - 1)
17641 {
17642 size_t maxlen = (end - p) - 1;
17643
17644 print_symbol ((int) maxlen, (const char *) p);
17645 p += strnlen ((char *) p, maxlen) + 1;
17646 }
17647 else
17648 {
17649 printf (_("<corrupt>"));
17650 p = (unsigned char *) end;
17651 }
17652 putchar ('\n');
17653 return p;
17654 }
17655
17656 case Tag_ABI_conformance:
17657 {
17658 printf (" Tag_ABI_conformance: \"");
17659 if (p < end - 1)
17660 {
17661 size_t maxlen = (end - p) - 1;
17662
17663 print_symbol ((int) maxlen, (const char *) p);
17664 p += strnlen ((char *) p, maxlen) + 1;
17665 }
17666 else
17667 {
17668 printf (_("<corrupt>"));
17669 p = (unsigned char *) end;
17670 }
17671 printf ("\"\n");
17672 return p;
17673 }
17674 }
17675
17676 return display_tag_value (tag, p, end);
17677 }
17678
17679 static void
17680 display_raw_attribute (unsigned char * p, unsigned char const * const end)
17681 {
17682 unsigned long addr = 0;
17683 size_t bytes = end - p;
17684
17685 assert (end >= p);
17686 while (bytes)
17687 {
17688 int j;
17689 int k;
17690 int lbytes = (bytes > 16 ? 16 : bytes);
17691
17692 printf (" 0x%8.8lx ", addr);
17693
17694 for (j = 0; j < 16; j++)
17695 {
17696 if (j < lbytes)
17697 printf ("%2.2x", p[j]);
17698 else
17699 printf (" ");
17700
17701 if ((j & 3) == 3)
17702 printf (" ");
17703 }
17704
17705 for (j = 0; j < lbytes; j++)
17706 {
17707 k = p[j];
17708 if (k >= ' ' && k < 0x7f)
17709 printf ("%c", k);
17710 else
17711 printf (".");
17712 }
17713
17714 putchar ('\n');
17715
17716 p += lbytes;
17717 bytes -= lbytes;
17718 addr += lbytes;
17719 }
17720
17721 putchar ('\n');
17722 }
17723
17724 static unsigned char *
17725 display_msp430_attribute (unsigned char * p,
17726 const unsigned char * const end)
17727 {
17728 unsigned int val;
17729 unsigned int tag;
17730
17731 READ_ULEB (tag, p, end);
17732
17733 switch (tag)
17734 {
17735 case OFBA_MSPABI_Tag_ISA:
17736 printf (" Tag_ISA: ");
17737 READ_ULEB (val, p, end);
17738 switch (val)
17739 {
17740 case 0: printf (_("None\n")); break;
17741 case 1: printf (_("MSP430\n")); break;
17742 case 2: printf (_("MSP430X\n")); break;
17743 default: printf ("??? (%d)\n", val); break;
17744 }
17745 break;
17746
17747 case OFBA_MSPABI_Tag_Code_Model:
17748 printf (" Tag_Code_Model: ");
17749 READ_ULEB (val, p, end);
17750 switch (val)
17751 {
17752 case 0: printf (_("None\n")); break;
17753 case 1: printf (_("Small\n")); break;
17754 case 2: printf (_("Large\n")); break;
17755 default: printf ("??? (%d)\n", val); break;
17756 }
17757 break;
17758
17759 case OFBA_MSPABI_Tag_Data_Model:
17760 printf (" Tag_Data_Model: ");
17761 READ_ULEB (val, p, end);
17762 switch (val)
17763 {
17764 case 0: printf (_("None\n")); break;
17765 case 1: printf (_("Small\n")); break;
17766 case 2: printf (_("Large\n")); break;
17767 case 3: printf (_("Restricted Large\n")); break;
17768 default: printf ("??? (%d)\n", val); break;
17769 }
17770 break;
17771
17772 default:
17773 printf (_(" <unknown tag %d>: "), tag);
17774
17775 if (tag & 1)
17776 {
17777 putchar ('"');
17778 if (p < end - 1)
17779 {
17780 size_t maxlen = (end - p) - 1;
17781
17782 print_symbol ((int) maxlen, (const char *) p);
17783 p += strnlen ((char *) p, maxlen) + 1;
17784 }
17785 else
17786 {
17787 printf (_("<corrupt>"));
17788 p = (unsigned char *) end;
17789 }
17790 printf ("\"\n");
17791 }
17792 else
17793 {
17794 READ_ULEB (val, p, end);
17795 printf ("%d (0x%x)\n", val, val);
17796 }
17797 break;
17798 }
17799
17800 assert (p <= end);
17801 return p;
17802 }
17803
17804 static unsigned char *
17805 display_msp430_gnu_attribute (unsigned char * p,
17806 unsigned int tag,
17807 const unsigned char * const end)
17808 {
17809 if (tag == Tag_GNU_MSP430_Data_Region)
17810 {
17811 unsigned int val;
17812
17813 printf (" Tag_GNU_MSP430_Data_Region: ");
17814 READ_ULEB (val, p, end);
17815
17816 switch (val)
17817 {
17818 case Val_GNU_MSP430_Data_Region_Any:
17819 printf (_("Any Region\n"));
17820 break;
17821 case Val_GNU_MSP430_Data_Region_Lower:
17822 printf (_("Lower Region Only\n"));
17823 break;
17824 default:
17825 printf ("??? (%u)\n", val);
17826 }
17827 return p;
17828 }
17829 return display_tag_value (tag & 1, p, end);
17830 }
17831
17832 struct riscv_attr_tag_t {
17833 const char *name;
17834 unsigned int tag;
17835 };
17836
17837 static struct riscv_attr_tag_t riscv_attr_tag[] =
17838 {
17839 #define T(tag) {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
17840 T(arch),
17841 T(priv_spec),
17842 T(priv_spec_minor),
17843 T(priv_spec_revision),
17844 T(unaligned_access),
17845 T(stack_align),
17846 #undef T
17847 };
17848
17849 static unsigned char *
17850 display_riscv_attribute (unsigned char *p,
17851 const unsigned char * const end)
17852 {
17853 unsigned int val;
17854 unsigned int tag;
17855 struct riscv_attr_tag_t *attr = NULL;
17856 unsigned i;
17857
17858 READ_ULEB (tag, p, end);
17859
17860 /* Find the name of attribute. */
17861 for (i = 0; i < ARRAY_SIZE (riscv_attr_tag); i++)
17862 {
17863 if (riscv_attr_tag[i].tag == tag)
17864 {
17865 attr = &riscv_attr_tag[i];
17866 break;
17867 }
17868 }
17869
17870 if (attr)
17871 printf (" %s: ", attr->name);
17872 else
17873 return display_tag_value (tag, p, end);
17874
17875 switch (tag)
17876 {
17877 case Tag_RISCV_priv_spec:
17878 case Tag_RISCV_priv_spec_minor:
17879 case Tag_RISCV_priv_spec_revision:
17880 READ_ULEB (val, p, end);
17881 printf (_("%u\n"), val);
17882 break;
17883 case Tag_RISCV_unaligned_access:
17884 READ_ULEB (val, p, end);
17885 switch (val)
17886 {
17887 case 0:
17888 printf (_("No unaligned access\n"));
17889 break;
17890 case 1:
17891 printf (_("Unaligned access\n"));
17892 break;
17893 }
17894 break;
17895 case Tag_RISCV_stack_align:
17896 READ_ULEB (val, p, end);
17897 printf (_("%u-bytes\n"), val);
17898 break;
17899 case Tag_RISCV_arch:
17900 p = display_tag_value (-1, p, end);
17901 break;
17902 default:
17903 return display_tag_value (tag, p, end);
17904 }
17905
17906 return p;
17907 }
17908
17909 static unsigned char *
17910 display_csky_attribute (unsigned char * p,
17911 const unsigned char * const end)
17912 {
17913 unsigned int tag;
17914 unsigned int val;
17915 READ_ULEB (tag, p, end);
17916
17917 if (tag >= Tag_CSKY_MAX)
17918 {
17919 return display_tag_value (-1, p, end);
17920 }
17921
17922 switch (tag)
17923 {
17924 case Tag_CSKY_ARCH_NAME:
17925 printf (" Tag_CSKY_ARCH_NAME:\t\t");
17926 return display_tag_value (-1, p, end);
17927 case Tag_CSKY_CPU_NAME:
17928 printf (" Tag_CSKY_CPU_NAME:\t\t");
17929 return display_tag_value (-1, p, end);
17930
17931 case Tag_CSKY_ISA_FLAGS:
17932 printf (" Tag_CSKY_ISA_FLAGS:\t\t");
17933 return display_tag_value (0, p, end);
17934 case Tag_CSKY_ISA_EXT_FLAGS:
17935 printf (" Tag_CSKY_ISA_EXT_FLAGS:\t");
17936 return display_tag_value (0, p, end);
17937
17938 case Tag_CSKY_DSP_VERSION:
17939 printf (" Tag_CSKY_DSP_VERSION:\t\t");
17940 READ_ULEB (val, p, end);
17941 if (val == VAL_CSKY_DSP_VERSION_EXTENSION)
17942 printf ("DSP Extension\n");
17943 else if (val == VAL_CSKY_DSP_VERSION_2)
17944 printf ("DSP 2.0\n");
17945 break;
17946
17947 case Tag_CSKY_VDSP_VERSION:
17948 printf (" Tag_CSKY_VDSP_VERSION:\t");
17949 READ_ULEB (val, p, end);
17950 printf ("VDSP Version %d\n", val);
17951 break;
17952
17953 case Tag_CSKY_FPU_VERSION:
17954 printf (" Tag_CSKY_FPU_VERSION:\t\t");
17955 READ_ULEB (val, p, end);
17956 if (val == VAL_CSKY_FPU_VERSION_1)
17957 printf ("ABIV1 FPU Version 1\n");
17958 else if (val == VAL_CSKY_FPU_VERSION_2)
17959 printf ("FPU Version 2\n");
17960 break;
17961
17962 case Tag_CSKY_FPU_ABI:
17963 printf (" Tag_CSKY_FPU_ABI:\t\t");
17964 READ_ULEB (val, p, end);
17965 if (val == VAL_CSKY_FPU_ABI_HARD)
17966 printf ("Hard\n");
17967 else if (val == VAL_CSKY_FPU_ABI_SOFTFP)
17968 printf ("SoftFP\n");
17969 else if (val == VAL_CSKY_FPU_ABI_SOFT)
17970 printf ("Soft\n");
17971 break;
17972 case Tag_CSKY_FPU_ROUNDING:
17973 READ_ULEB (val, p, end);
17974 if (val == 1)
17975 {
17976 printf (" Tag_CSKY_FPU_ROUNDING:\t");
17977 printf ("Needed\n");
17978 }
17979 break;
17980 case Tag_CSKY_FPU_DENORMAL:
17981 READ_ULEB (val, p, end);
17982 if (val == 1)
17983 {
17984 printf (" Tag_CSKY_FPU_DENORMAL:\t");
17985 printf ("Needed\n");
17986 }
17987 break;
17988 case Tag_CSKY_FPU_Exception:
17989 READ_ULEB (val, p, end);
17990 if (val == 1)
17991 {
17992 printf (" Tag_CSKY_FPU_Exception:\t");
17993 printf ("Needed\n");
17994 }
17995 break;
17996 case Tag_CSKY_FPU_NUMBER_MODULE:
17997 printf (" Tag_CSKY_FPU_NUMBER_MODULE:\t");
17998 return display_tag_value (-1, p, end);
17999 case Tag_CSKY_FPU_HARDFP:
18000 printf (" Tag_CSKY_FPU_HARDFP:\t\t");
18001 READ_ULEB (val, p, end);
18002 if (val & VAL_CSKY_FPU_HARDFP_HALF)
18003 printf (" Half");
18004 if (val & VAL_CSKY_FPU_HARDFP_SINGLE)
18005 printf (" Single");
18006 if (val & VAL_CSKY_FPU_HARDFP_DOUBLE)
18007 printf (" Double");
18008 printf ("\n");
18009 break;
18010 default:
18011 return display_tag_value (tag, p, end);
18012 }
18013 return p;
18014 }
18015
18016 static bool
18017 process_attributes (Filedata * filedata,
18018 const char * public_name,
18019 unsigned int proc_type,
18020 unsigned char * (* display_pub_attribute) (unsigned char *, const unsigned char * const),
18021 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const))
18022 {
18023 Elf_Internal_Shdr * sect;
18024 unsigned i;
18025 bool res = true;
18026
18027 /* Find the section header so that we get the size. */
18028 for (i = 0, sect = filedata->section_headers;
18029 i < filedata->file_header.e_shnum;
18030 i++, sect++)
18031 {
18032 unsigned char * contents;
18033 unsigned char * p;
18034
18035 if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
18036 continue;
18037
18038 contents = (unsigned char *) get_data (NULL, filedata, sect->sh_offset, 1,
18039 sect->sh_size, _("attributes"));
18040 if (contents == NULL)
18041 {
18042 res = false;
18043 continue;
18044 }
18045
18046 p = contents;
18047 /* The first character is the version of the attributes.
18048 Currently only version 1, (aka 'A') is recognised here. */
18049 if (*p != 'A')
18050 {
18051 printf (_("Unknown attributes version '%c'(%d) - expecting 'A'\n"), *p, *p);
18052 res = false;
18053 }
18054 else
18055 {
18056 uint64_t section_len;
18057
18058 section_len = sect->sh_size - 1;
18059 p++;
18060
18061 while (section_len > 0)
18062 {
18063 uint64_t attr_len;
18064 unsigned int namelen;
18065 bool public_section;
18066 bool gnu_section;
18067
18068 if (section_len <= 4)
18069 {
18070 error (_("Tag section ends prematurely\n"));
18071 res = false;
18072 break;
18073 }
18074 attr_len = byte_get (p, 4);
18075 p += 4;
18076
18077 if (attr_len > section_len)
18078 {
18079 error (_("Bad attribute length (%u > %u)\n"),
18080 (unsigned) attr_len, (unsigned) section_len);
18081 attr_len = section_len;
18082 res = false;
18083 }
18084 /* PR 17531: file: 001-101425-0.004 */
18085 else if (attr_len < 5)
18086 {
18087 error (_("Attribute length of %u is too small\n"), (unsigned) attr_len);
18088 res = false;
18089 break;
18090 }
18091
18092 section_len -= attr_len;
18093 attr_len -= 4;
18094
18095 namelen = strnlen ((char *) p, attr_len) + 1;
18096 if (namelen == 0 || namelen >= attr_len)
18097 {
18098 error (_("Corrupt attribute section name\n"));
18099 res = false;
18100 break;
18101 }
18102
18103 printf (_("Attribute Section: "));
18104 print_symbol (INT_MAX, (const char *) p);
18105 putchar ('\n');
18106
18107 if (public_name && streq ((char *) p, public_name))
18108 public_section = true;
18109 else
18110 public_section = false;
18111
18112 if (streq ((char *) p, "gnu"))
18113 gnu_section = true;
18114 else
18115 gnu_section = false;
18116
18117 p += namelen;
18118 attr_len -= namelen;
18119
18120 while (attr_len > 0 && p < contents + sect->sh_size)
18121 {
18122 int tag;
18123 unsigned int val;
18124 uint64_t size;
18125 unsigned char * end;
18126
18127 /* PR binutils/17531: Safe handling of corrupt files. */
18128 if (attr_len < 6)
18129 {
18130 error (_("Unused bytes at end of section\n"));
18131 res = false;
18132 section_len = 0;
18133 break;
18134 }
18135
18136 tag = *(p++);
18137 size = byte_get (p, 4);
18138 if (size > attr_len)
18139 {
18140 error (_("Bad subsection length (%u > %u)\n"),
18141 (unsigned) size, (unsigned) attr_len);
18142 res = false;
18143 size = attr_len;
18144 }
18145 /* PR binutils/17531: Safe handling of corrupt files. */
18146 if (size < 6)
18147 {
18148 error (_("Bad subsection length (%u < 6)\n"),
18149 (unsigned) size);
18150 res = false;
18151 section_len = 0;
18152 break;
18153 }
18154
18155 attr_len -= size;
18156 end = p + size - 1;
18157 assert (end <= contents + sect->sh_size);
18158 p += 4;
18159
18160 switch (tag)
18161 {
18162 case 1:
18163 printf (_("File Attributes\n"));
18164 break;
18165 case 2:
18166 printf (_("Section Attributes:"));
18167 goto do_numlist;
18168 case 3:
18169 printf (_("Symbol Attributes:"));
18170 /* Fall through. */
18171 do_numlist:
18172 for (;;)
18173 {
18174 READ_ULEB (val, p, end);
18175 if (val == 0)
18176 break;
18177 printf (" %d", val);
18178 }
18179 printf ("\n");
18180 break;
18181 default:
18182 printf (_("Unknown tag: %d\n"), tag);
18183 public_section = false;
18184 break;
18185 }
18186
18187 if (public_section && display_pub_attribute != NULL)
18188 {
18189 while (p < end)
18190 p = display_pub_attribute (p, end);
18191 assert (p == end);
18192 }
18193 else if (gnu_section && display_proc_gnu_attribute != NULL)
18194 {
18195 while (p < end)
18196 p = display_gnu_attribute (p,
18197 display_proc_gnu_attribute,
18198 end);
18199 assert (p == end);
18200 }
18201 else if (p < end)
18202 {
18203 printf (_(" Unknown attribute:\n"));
18204 display_raw_attribute (p, end);
18205 p = end;
18206 }
18207 else
18208 attr_len = 0;
18209 }
18210 }
18211 }
18212
18213 free (contents);
18214 }
18215
18216 return res;
18217 }
18218
18219 /* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
18220 Print the Address, Access and Initial fields of an entry at VMA ADDR
18221 and return the VMA of the next entry, or -1 if there was a problem.
18222 Does not read from DATA_END or beyond. */
18223
18224 static uint64_t
18225 print_mips_got_entry (unsigned char * data, uint64_t pltgot, uint64_t addr,
18226 unsigned char * data_end)
18227 {
18228 printf (" ");
18229 print_vma (addr, LONG_HEX);
18230 printf (" ");
18231 if (addr < pltgot + 0xfff0)
18232 printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
18233 else
18234 printf ("%10s", "");
18235 printf (" ");
18236 if (data == NULL)
18237 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
18238 else
18239 {
18240 uint64_t entry;
18241 unsigned char * from = data + addr - pltgot;
18242
18243 if (from + (is_32bit_elf ? 4 : 8) > data_end)
18244 {
18245 warn (_("MIPS GOT entry extends beyond the end of available data\n"));
18246 printf ("%*s", is_32bit_elf ? 8 : 16, _("<corrupt>"));
18247 return (uint64_t) -1;
18248 }
18249 else
18250 {
18251 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
18252 print_vma (entry, LONG_HEX);
18253 }
18254 }
18255 return addr + (is_32bit_elf ? 4 : 8);
18256 }
18257
18258 /* DATA points to the contents of a MIPS PLT GOT that starts at VMA
18259 PLTGOT. Print the Address and Initial fields of an entry at VMA
18260 ADDR and return the VMA of the next entry. */
18261
18262 static uint64_t
18263 print_mips_pltgot_entry (unsigned char * data, uint64_t pltgot, uint64_t addr)
18264 {
18265 printf (" ");
18266 print_vma (addr, LONG_HEX);
18267 printf (" ");
18268 if (data == NULL)
18269 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
18270 else
18271 {
18272 uint64_t entry;
18273
18274 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
18275 print_vma (entry, LONG_HEX);
18276 }
18277 return addr + (is_32bit_elf ? 4 : 8);
18278 }
18279
18280 static void
18281 print_mips_ases (unsigned int mask)
18282 {
18283 if (mask & AFL_ASE_DSP)
18284 fputs ("\n\tDSP ASE", stdout);
18285 if (mask & AFL_ASE_DSPR2)
18286 fputs ("\n\tDSP R2 ASE", stdout);
18287 if (mask & AFL_ASE_DSPR3)
18288 fputs ("\n\tDSP R3 ASE", stdout);
18289 if (mask & AFL_ASE_EVA)
18290 fputs ("\n\tEnhanced VA Scheme", stdout);
18291 if (mask & AFL_ASE_MCU)
18292 fputs ("\n\tMCU (MicroController) ASE", stdout);
18293 if (mask & AFL_ASE_MDMX)
18294 fputs ("\n\tMDMX ASE", stdout);
18295 if (mask & AFL_ASE_MIPS3D)
18296 fputs ("\n\tMIPS-3D ASE", stdout);
18297 if (mask & AFL_ASE_MT)
18298 fputs ("\n\tMT ASE", stdout);
18299 if (mask & AFL_ASE_SMARTMIPS)
18300 fputs ("\n\tSmartMIPS ASE", stdout);
18301 if (mask & AFL_ASE_VIRT)
18302 fputs ("\n\tVZ ASE", stdout);
18303 if (mask & AFL_ASE_MSA)
18304 fputs ("\n\tMSA ASE", stdout);
18305 if (mask & AFL_ASE_MIPS16)
18306 fputs ("\n\tMIPS16 ASE", stdout);
18307 if (mask & AFL_ASE_MICROMIPS)
18308 fputs ("\n\tMICROMIPS ASE", stdout);
18309 if (mask & AFL_ASE_XPA)
18310 fputs ("\n\tXPA ASE", stdout);
18311 if (mask & AFL_ASE_MIPS16E2)
18312 fputs ("\n\tMIPS16e2 ASE", stdout);
18313 if (mask & AFL_ASE_CRC)
18314 fputs ("\n\tCRC ASE", stdout);
18315 if (mask & AFL_ASE_GINV)
18316 fputs ("\n\tGINV ASE", stdout);
18317 if (mask & AFL_ASE_LOONGSON_MMI)
18318 fputs ("\n\tLoongson MMI ASE", stdout);
18319 if (mask & AFL_ASE_LOONGSON_CAM)
18320 fputs ("\n\tLoongson CAM ASE", stdout);
18321 if (mask & AFL_ASE_LOONGSON_EXT)
18322 fputs ("\n\tLoongson EXT ASE", stdout);
18323 if (mask & AFL_ASE_LOONGSON_EXT2)
18324 fputs ("\n\tLoongson EXT2 ASE", stdout);
18325 if (mask == 0)
18326 fprintf (stdout, "\n\t%s", _("None"));
18327 else if ((mask & ~AFL_ASE_MASK) != 0)
18328 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
18329 }
18330
18331 static void
18332 print_mips_isa_ext (unsigned int isa_ext)
18333 {
18334 switch (isa_ext)
18335 {
18336 case 0:
18337 fputs (_("None"), stdout);
18338 break;
18339 case AFL_EXT_XLR:
18340 fputs ("RMI XLR", stdout);
18341 break;
18342 case AFL_EXT_OCTEON3:
18343 fputs ("Cavium Networks Octeon3", stdout);
18344 break;
18345 case AFL_EXT_OCTEON2:
18346 fputs ("Cavium Networks Octeon2", stdout);
18347 break;
18348 case AFL_EXT_OCTEONP:
18349 fputs ("Cavium Networks OcteonP", stdout);
18350 break;
18351 case AFL_EXT_OCTEON:
18352 fputs ("Cavium Networks Octeon", stdout);
18353 break;
18354 case AFL_EXT_5900:
18355 fputs ("Toshiba R5900", stdout);
18356 break;
18357 case AFL_EXT_4650:
18358 fputs ("MIPS R4650", stdout);
18359 break;
18360 case AFL_EXT_4010:
18361 fputs ("LSI R4010", stdout);
18362 break;
18363 case AFL_EXT_4100:
18364 fputs ("NEC VR4100", stdout);
18365 break;
18366 case AFL_EXT_3900:
18367 fputs ("Toshiba R3900", stdout);
18368 break;
18369 case AFL_EXT_10000:
18370 fputs ("MIPS R10000", stdout);
18371 break;
18372 case AFL_EXT_SB1:
18373 fputs ("Broadcom SB-1", stdout);
18374 break;
18375 case AFL_EXT_4111:
18376 fputs ("NEC VR4111/VR4181", stdout);
18377 break;
18378 case AFL_EXT_4120:
18379 fputs ("NEC VR4120", stdout);
18380 break;
18381 case AFL_EXT_5400:
18382 fputs ("NEC VR5400", stdout);
18383 break;
18384 case AFL_EXT_5500:
18385 fputs ("NEC VR5500", stdout);
18386 break;
18387 case AFL_EXT_LOONGSON_2E:
18388 fputs ("ST Microelectronics Loongson 2E", stdout);
18389 break;
18390 case AFL_EXT_LOONGSON_2F:
18391 fputs ("ST Microelectronics Loongson 2F", stdout);
18392 break;
18393 case AFL_EXT_INTERAPTIV_MR2:
18394 fputs ("Imagination interAptiv MR2", stdout);
18395 break;
18396 default:
18397 fprintf (stdout, "%s (%d)", _("Unknown"), isa_ext);
18398 }
18399 }
18400
18401 static signed int
18402 get_mips_reg_size (int reg_size)
18403 {
18404 return (reg_size == AFL_REG_NONE) ? 0
18405 : (reg_size == AFL_REG_32) ? 32
18406 : (reg_size == AFL_REG_64) ? 64
18407 : (reg_size == AFL_REG_128) ? 128
18408 : -1;
18409 }
18410
18411 static bool
18412 process_mips_specific (Filedata * filedata)
18413 {
18414 Elf_Internal_Dyn * entry;
18415 Elf_Internal_Shdr *sect = NULL;
18416 size_t liblist_offset = 0;
18417 size_t liblistno = 0;
18418 size_t conflictsno = 0;
18419 size_t options_offset = 0;
18420 size_t conflicts_offset = 0;
18421 size_t pltrelsz = 0;
18422 size_t pltrel = 0;
18423 uint64_t pltgot = 0;
18424 uint64_t mips_pltgot = 0;
18425 uint64_t jmprel = 0;
18426 uint64_t local_gotno = 0;
18427 uint64_t gotsym = 0;
18428 uint64_t symtabno = 0;
18429 bool res = true;
18430
18431 if (! process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
18432 display_mips_gnu_attribute))
18433 res = false;
18434
18435 sect = find_section (filedata, ".MIPS.abiflags");
18436
18437 if (sect != NULL)
18438 {
18439 Elf_External_ABIFlags_v0 *abiflags_ext;
18440 Elf_Internal_ABIFlags_v0 abiflags_in;
18441
18442 if (sizeof (Elf_External_ABIFlags_v0) != sect->sh_size)
18443 {
18444 error (_("Corrupt MIPS ABI Flags section.\n"));
18445 res = false;
18446 }
18447 else
18448 {
18449 abiflags_ext = get_data (NULL, filedata, sect->sh_offset, 1,
18450 sect->sh_size, _("MIPS ABI Flags section"));
18451 if (abiflags_ext)
18452 {
18453 abiflags_in.version = BYTE_GET (abiflags_ext->version);
18454 abiflags_in.isa_level = BYTE_GET (abiflags_ext->isa_level);
18455 abiflags_in.isa_rev = BYTE_GET (abiflags_ext->isa_rev);
18456 abiflags_in.gpr_size = BYTE_GET (abiflags_ext->gpr_size);
18457 abiflags_in.cpr1_size = BYTE_GET (abiflags_ext->cpr1_size);
18458 abiflags_in.cpr2_size = BYTE_GET (abiflags_ext->cpr2_size);
18459 abiflags_in.fp_abi = BYTE_GET (abiflags_ext->fp_abi);
18460 abiflags_in.isa_ext = BYTE_GET (abiflags_ext->isa_ext);
18461 abiflags_in.ases = BYTE_GET (abiflags_ext->ases);
18462 abiflags_in.flags1 = BYTE_GET (abiflags_ext->flags1);
18463 abiflags_in.flags2 = BYTE_GET (abiflags_ext->flags2);
18464
18465 printf ("\nMIPS ABI Flags Version: %d\n", abiflags_in.version);
18466 printf ("\nISA: MIPS%d", abiflags_in.isa_level);
18467 if (abiflags_in.isa_rev > 1)
18468 printf ("r%d", abiflags_in.isa_rev);
18469 printf ("\nGPR size: %d",
18470 get_mips_reg_size (abiflags_in.gpr_size));
18471 printf ("\nCPR1 size: %d",
18472 get_mips_reg_size (abiflags_in.cpr1_size));
18473 printf ("\nCPR2 size: %d",
18474 get_mips_reg_size (abiflags_in.cpr2_size));
18475 fputs ("\nFP ABI: ", stdout);
18476 print_mips_fp_abi_value (abiflags_in.fp_abi);
18477 fputs ("ISA Extension: ", stdout);
18478 print_mips_isa_ext (abiflags_in.isa_ext);
18479 fputs ("\nASEs:", stdout);
18480 print_mips_ases (abiflags_in.ases);
18481 printf ("\nFLAGS 1: %8.8lx", abiflags_in.flags1);
18482 printf ("\nFLAGS 2: %8.8lx", abiflags_in.flags2);
18483 fputc ('\n', stdout);
18484 free (abiflags_ext);
18485 }
18486 }
18487 }
18488
18489 /* We have a lot of special sections. Thanks SGI! */
18490 if (filedata->dynamic_section == NULL)
18491 {
18492 /* No dynamic information available. See if there is static GOT. */
18493 sect = find_section (filedata, ".got");
18494 if (sect != NULL)
18495 {
18496 unsigned char *data_end;
18497 unsigned char *data;
18498 uint64_t ent, end;
18499 int addr_size;
18500
18501 pltgot = sect->sh_addr;
18502
18503 ent = pltgot;
18504 addr_size = (is_32bit_elf ? 4 : 8);
18505 end = pltgot + sect->sh_size;
18506
18507 data = (unsigned char *) get_data (NULL, filedata, sect->sh_offset,
18508 end - pltgot, 1,
18509 _("Global Offset Table data"));
18510 /* PR 12855: Null data is handled gracefully throughout. */
18511 data_end = data + (end - pltgot);
18512
18513 printf (_("\nStatic GOT:\n"));
18514 printf (_(" Canonical gp value: "));
18515 print_vma (ent + 0x7ff0, LONG_HEX);
18516 printf ("\n\n");
18517
18518 /* In a dynamic binary GOT[0] is reserved for the dynamic
18519 loader to store the lazy resolver pointer, however in
18520 a static binary it may well have been omitted and GOT
18521 reduced to a table of addresses.
18522 PR 21344: Check for the entry being fully available
18523 before fetching it. */
18524 if (data
18525 && data + ent - pltgot + addr_size <= data_end
18526 && byte_get (data + ent - pltgot, addr_size) == 0)
18527 {
18528 printf (_(" Reserved entries:\n"));
18529 printf (_(" %*s %10s %*s\n"),
18530 addr_size * 2, _("Address"), _("Access"),
18531 addr_size * 2, _("Value"));
18532 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18533 printf ("\n");
18534 if (ent == (uint64_t) -1)
18535 goto sgot_print_fail;
18536
18537 /* Check for the MSB of GOT[1] being set, identifying a
18538 GNU object. This entry will be used by some runtime
18539 loaders, to store the module pointer. Otherwise this
18540 is an ordinary local entry.
18541 PR 21344: Check for the entry being fully available
18542 before fetching it. */
18543 if (data
18544 && data + ent - pltgot + addr_size <= data_end
18545 && (byte_get (data + ent - pltgot, addr_size)
18546 >> (addr_size * 8 - 1)) != 0)
18547 {
18548 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18549 printf ("\n");
18550 if (ent == (uint64_t) -1)
18551 goto sgot_print_fail;
18552 }
18553 printf ("\n");
18554 }
18555
18556 if (data != NULL && ent < end)
18557 {
18558 printf (_(" Local entries:\n"));
18559 printf (" %*s %10s %*s\n",
18560 addr_size * 2, _("Address"), _("Access"),
18561 addr_size * 2, _("Value"));
18562 while (ent < end)
18563 {
18564 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18565 printf ("\n");
18566 if (ent == (uint64_t) -1)
18567 goto sgot_print_fail;
18568 }
18569 printf ("\n");
18570 }
18571
18572 sgot_print_fail:
18573 free (data);
18574 }
18575 return res;
18576 }
18577
18578 for (entry = filedata->dynamic_section;
18579 /* PR 17531 file: 012-50589-0.004. */
18580 (entry < filedata->dynamic_section + filedata->dynamic_nent
18581 && entry->d_tag != DT_NULL);
18582 ++entry)
18583 switch (entry->d_tag)
18584 {
18585 case DT_MIPS_LIBLIST:
18586 liblist_offset
18587 = offset_from_vma (filedata, entry->d_un.d_val,
18588 liblistno * sizeof (Elf32_External_Lib));
18589 break;
18590 case DT_MIPS_LIBLISTNO:
18591 liblistno = entry->d_un.d_val;
18592 break;
18593 case DT_MIPS_OPTIONS:
18594 options_offset = offset_from_vma (filedata, entry->d_un.d_val, 0);
18595 break;
18596 case DT_MIPS_CONFLICT:
18597 conflicts_offset
18598 = offset_from_vma (filedata, entry->d_un.d_val,
18599 conflictsno * sizeof (Elf32_External_Conflict));
18600 break;
18601 case DT_MIPS_CONFLICTNO:
18602 conflictsno = entry->d_un.d_val;
18603 break;
18604 case DT_PLTGOT:
18605 pltgot = entry->d_un.d_ptr;
18606 break;
18607 case DT_MIPS_LOCAL_GOTNO:
18608 local_gotno = entry->d_un.d_val;
18609 break;
18610 case DT_MIPS_GOTSYM:
18611 gotsym = entry->d_un.d_val;
18612 break;
18613 case DT_MIPS_SYMTABNO:
18614 symtabno = entry->d_un.d_val;
18615 break;
18616 case DT_MIPS_PLTGOT:
18617 mips_pltgot = entry->d_un.d_ptr;
18618 break;
18619 case DT_PLTREL:
18620 pltrel = entry->d_un.d_val;
18621 break;
18622 case DT_PLTRELSZ:
18623 pltrelsz = entry->d_un.d_val;
18624 break;
18625 case DT_JMPREL:
18626 jmprel = entry->d_un.d_ptr;
18627 break;
18628 default:
18629 break;
18630 }
18631
18632 if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
18633 {
18634 Elf32_External_Lib * elib;
18635 size_t cnt;
18636
18637 elib = (Elf32_External_Lib *) get_data (NULL, filedata, liblist_offset,
18638 sizeof (Elf32_External_Lib),
18639 liblistno,
18640 _("liblist section data"));
18641 if (elib)
18642 {
18643 printf (ngettext ("\nSection '.liblist' contains %lu entry:\n",
18644 "\nSection '.liblist' contains %lu entries:\n",
18645 (unsigned long) liblistno),
18646 (unsigned long) liblistno);
18647 fputs (_(" Library Time Stamp Checksum Version Flags\n"),
18648 stdout);
18649
18650 for (cnt = 0; cnt < liblistno; ++cnt)
18651 {
18652 Elf32_Lib liblist;
18653 time_t atime;
18654 char timebuf[128];
18655 struct tm * tmp;
18656
18657 liblist.l_name = BYTE_GET (elib[cnt].l_name);
18658 atime = BYTE_GET (elib[cnt].l_time_stamp);
18659 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
18660 liblist.l_version = BYTE_GET (elib[cnt].l_version);
18661 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
18662
18663 tmp = gmtime (&atime);
18664 snprintf (timebuf, sizeof (timebuf),
18665 "%04u-%02u-%02uT%02u:%02u:%02u",
18666 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
18667 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
18668
18669 printf ("%3lu: ", (unsigned long) cnt);
18670 if (valid_dynamic_name (filedata, liblist.l_name))
18671 print_symbol (20, get_dynamic_name (filedata, liblist.l_name));
18672 else
18673 printf (_("<corrupt: %9ld>"), liblist.l_name);
18674 printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
18675 liblist.l_version);
18676
18677 if (liblist.l_flags == 0)
18678 puts (_(" NONE"));
18679 else
18680 {
18681 static const struct
18682 {
18683 const char * name;
18684 int bit;
18685 }
18686 l_flags_vals[] =
18687 {
18688 { " EXACT_MATCH", LL_EXACT_MATCH },
18689 { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
18690 { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
18691 { " EXPORTS", LL_EXPORTS },
18692 { " DELAY_LOAD", LL_DELAY_LOAD },
18693 { " DELTA", LL_DELTA }
18694 };
18695 int flags = liblist.l_flags;
18696 size_t fcnt;
18697
18698 for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
18699 if ((flags & l_flags_vals[fcnt].bit) != 0)
18700 {
18701 fputs (l_flags_vals[fcnt].name, stdout);
18702 flags ^= l_flags_vals[fcnt].bit;
18703 }
18704 if (flags != 0)
18705 printf (" %#x", (unsigned int) flags);
18706
18707 puts ("");
18708 }
18709 }
18710
18711 free (elib);
18712 }
18713 else
18714 res = false;
18715 }
18716
18717 if (options_offset != 0)
18718 {
18719 Elf_External_Options * eopt;
18720 size_t offset;
18721 int cnt;
18722
18723 /* Find the section header so that we get the size. */
18724 sect = find_section_by_type (filedata, SHT_MIPS_OPTIONS);
18725 /* PR 17533 file: 012-277276-0.004. */
18726 if (sect == NULL)
18727 {
18728 error (_("No MIPS_OPTIONS header found\n"));
18729 return false;
18730 }
18731 /* PR 24243 */
18732 if (sect->sh_size < sizeof (* eopt))
18733 {
18734 error (_("The MIPS options section is too small.\n"));
18735 return false;
18736 }
18737
18738 eopt = (Elf_External_Options *) get_data (NULL, filedata, options_offset, 1,
18739 sect->sh_size, _("options"));
18740 if (eopt)
18741 {
18742 Elf_Internal_Options option;
18743
18744 offset = cnt = 0;
18745 while (offset <= sect->sh_size - sizeof (* eopt))
18746 {
18747 Elf_External_Options * eoption;
18748 unsigned int optsize;
18749
18750 eoption = (Elf_External_Options *) ((char *) eopt + offset);
18751
18752 optsize = BYTE_GET (eoption->size);
18753
18754 /* PR 17531: file: ffa0fa3b. */
18755 if (optsize < sizeof (* eopt)
18756 || optsize > sect->sh_size - offset)
18757 {
18758 error (_("Invalid size (%u) for MIPS option\n"),
18759 optsize);
18760 free (eopt);
18761 return false;
18762 }
18763 offset += optsize;
18764 ++cnt;
18765 }
18766
18767 printf (ngettext ("\nSection '%s' contains %d entry:\n",
18768 "\nSection '%s' contains %d entries:\n",
18769 cnt),
18770 printable_section_name (filedata, sect), cnt);
18771
18772 offset = 0;
18773 while (cnt-- > 0)
18774 {
18775 size_t len;
18776 Elf_External_Options * eoption;
18777
18778 eoption = (Elf_External_Options *) ((char *) eopt + offset);
18779
18780 option.kind = BYTE_GET (eoption->kind);
18781 option.size = BYTE_GET (eoption->size);
18782 option.section = BYTE_GET (eoption->section);
18783 option.info = BYTE_GET (eoption->info);
18784
18785 switch (option.kind)
18786 {
18787 case ODK_NULL:
18788 /* This shouldn't happen. */
18789 printf (" NULL %" PRId16 " %" PRIx32,
18790 option.section, option.info);
18791 break;
18792
18793 case ODK_REGINFO:
18794 printf (" REGINFO ");
18795 if (filedata->file_header.e_machine == EM_MIPS)
18796 {
18797 Elf32_External_RegInfo * ereg;
18798 Elf32_RegInfo reginfo;
18799
18800 /* 32bit form. */
18801 if (option.size < (sizeof (Elf_External_Options)
18802 + sizeof (Elf32_External_RegInfo)))
18803 {
18804 printf (_("<corrupt>\n"));
18805 error (_("Truncated MIPS REGINFO option\n"));
18806 cnt = 0;
18807 break;
18808 }
18809
18810 ereg = (Elf32_External_RegInfo *) (eoption + 1);
18811
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%" PRIx32 "\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 else
18828 {
18829 /* 64 bit form. */
18830 Elf64_External_RegInfo * ereg;
18831 Elf64_Internal_RegInfo reginfo;
18832
18833 if (option.size < (sizeof (Elf_External_Options)
18834 + sizeof (Elf64_External_RegInfo)))
18835 {
18836 printf (_("<corrupt>\n"));
18837 error (_("Truncated MIPS REGINFO option\n"));
18838 cnt = 0;
18839 break;
18840 }
18841
18842 ereg = (Elf64_External_RegInfo *) (eoption + 1);
18843 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
18844 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
18845 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
18846 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
18847 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
18848 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
18849
18850 printf ("GPR %08" PRIx32 " GP 0x%" PRIx64 "\n",
18851 reginfo.ri_gprmask, reginfo.ri_gp_value);
18852 printf (" "
18853 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
18854 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
18855 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
18856 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
18857 }
18858 offset += option.size;
18859 continue;
18860
18861 case ODK_EXCEPTIONS:
18862 fputs (" EXCEPTIONS fpe_min(", stdout);
18863 process_mips_fpe_exception (option.info & OEX_FPU_MIN);
18864 fputs (") fpe_max(", stdout);
18865 process_mips_fpe_exception ((option.info & OEX_FPU_MAX) >> 8);
18866 fputs (")", stdout);
18867
18868 if (option.info & OEX_PAGE0)
18869 fputs (" PAGE0", stdout);
18870 if (option.info & OEX_SMM)
18871 fputs (" SMM", stdout);
18872 if (option.info & OEX_FPDBUG)
18873 fputs (" FPDBUG", stdout);
18874 if (option.info & OEX_DISMISS)
18875 fputs (" DISMISS", stdout);
18876 break;
18877
18878 case ODK_PAD:
18879 fputs (" PAD ", stdout);
18880 if (option.info & OPAD_PREFIX)
18881 fputs (" PREFIX", stdout);
18882 if (option.info & OPAD_POSTFIX)
18883 fputs (" POSTFIX", stdout);
18884 if (option.info & OPAD_SYMBOL)
18885 fputs (" SYMBOL", stdout);
18886 break;
18887
18888 case ODK_HWPATCH:
18889 fputs (" HWPATCH ", stdout);
18890 if (option.info & OHW_R4KEOP)
18891 fputs (" R4KEOP", stdout);
18892 if (option.info & OHW_R8KPFETCH)
18893 fputs (" R8KPFETCH", stdout);
18894 if (option.info & OHW_R5KEOP)
18895 fputs (" R5KEOP", stdout);
18896 if (option.info & OHW_R5KCVTL)
18897 fputs (" R5KCVTL", stdout);
18898 break;
18899
18900 case ODK_FILL:
18901 fputs (" FILL ", stdout);
18902 /* XXX Print content of info word? */
18903 break;
18904
18905 case ODK_TAGS:
18906 fputs (" TAGS ", stdout);
18907 /* XXX Print content of info word? */
18908 break;
18909
18910 case ODK_HWAND:
18911 fputs (" HWAND ", stdout);
18912 if (option.info & OHWA0_R4KEOP_CHECKED)
18913 fputs (" R4KEOP_CHECKED", stdout);
18914 if (option.info & OHWA0_R4KEOP_CLEAN)
18915 fputs (" R4KEOP_CLEAN", stdout);
18916 break;
18917
18918 case ODK_HWOR:
18919 fputs (" HWOR ", stdout);
18920 if (option.info & OHWA0_R4KEOP_CHECKED)
18921 fputs (" R4KEOP_CHECKED", stdout);
18922 if (option.info & OHWA0_R4KEOP_CLEAN)
18923 fputs (" R4KEOP_CLEAN", stdout);
18924 break;
18925
18926 case ODK_GP_GROUP:
18927 printf (" GP_GROUP %#06x self-contained %#06x",
18928 option.info & OGP_GROUP,
18929 (option.info & OGP_SELF) >> 16);
18930 break;
18931
18932 case ODK_IDENT:
18933 printf (" IDENT %#06x self-contained %#06x",
18934 option.info & OGP_GROUP,
18935 (option.info & OGP_SELF) >> 16);
18936 break;
18937
18938 default:
18939 /* This shouldn't happen. */
18940 printf (" %3d ??? %" PRId16 " %" PRIx32,
18941 option.kind, option.section, option.info);
18942 break;
18943 }
18944
18945 len = sizeof (* eopt);
18946 while (len < option.size)
18947 {
18948 unsigned char datum = *((unsigned char *) eoption + len);
18949
18950 if (ISPRINT (datum))
18951 printf ("%c", datum);
18952 else
18953 printf ("\\%03o", datum);
18954 len ++;
18955 }
18956 fputs ("\n", stdout);
18957
18958 offset += option.size;
18959 }
18960 free (eopt);
18961 }
18962 else
18963 res = false;
18964 }
18965
18966 if (conflicts_offset != 0 && conflictsno != 0)
18967 {
18968 Elf32_Conflict * iconf;
18969 size_t cnt;
18970
18971 if (filedata->dynamic_symbols == NULL)
18972 {
18973 error (_("conflict list found without a dynamic symbol table\n"));
18974 return false;
18975 }
18976
18977 /* PR 21345 - print a slightly more helpful error message
18978 if we are sure that the cmalloc will fail. */
18979 if (conflictsno > filedata->file_size / sizeof (* iconf))
18980 {
18981 error (_("Overlarge number of conflicts detected: %lx\n"),
18982 (long) conflictsno);
18983 return false;
18984 }
18985
18986 iconf = (Elf32_Conflict *) cmalloc (conflictsno, sizeof (* iconf));
18987 if (iconf == NULL)
18988 {
18989 error (_("Out of memory allocating space for dynamic conflicts\n"));
18990 return false;
18991 }
18992
18993 if (is_32bit_elf)
18994 {
18995 Elf32_External_Conflict * econf32;
18996
18997 econf32 = (Elf32_External_Conflict *)
18998 get_data (NULL, filedata, conflicts_offset,
18999 sizeof (*econf32), conflictsno, _("conflict"));
19000 if (!econf32)
19001 {
19002 free (iconf);
19003 return false;
19004 }
19005
19006 for (cnt = 0; cnt < conflictsno; ++cnt)
19007 iconf[cnt] = BYTE_GET (econf32[cnt]);
19008
19009 free (econf32);
19010 }
19011 else
19012 {
19013 Elf64_External_Conflict * econf64;
19014
19015 econf64 = (Elf64_External_Conflict *)
19016 get_data (NULL, filedata, conflicts_offset,
19017 sizeof (*econf64), conflictsno, _("conflict"));
19018 if (!econf64)
19019 {
19020 free (iconf);
19021 return false;
19022 }
19023
19024 for (cnt = 0; cnt < conflictsno; ++cnt)
19025 iconf[cnt] = BYTE_GET (econf64[cnt]);
19026
19027 free (econf64);
19028 }
19029
19030 printf (ngettext ("\nSection '.conflict' contains %lu entry:\n",
19031 "\nSection '.conflict' contains %lu entries:\n",
19032 (unsigned long) conflictsno),
19033 (unsigned long) conflictsno);
19034 puts (_(" Num: Index Value Name"));
19035
19036 for (cnt = 0; cnt < conflictsno; ++cnt)
19037 {
19038 printf ("%5lu: %8lu ", (unsigned long) cnt, iconf[cnt]);
19039
19040 if (iconf[cnt] >= filedata->num_dynamic_syms)
19041 printf (_("<corrupt symbol index>"));
19042 else
19043 {
19044 Elf_Internal_Sym * psym;
19045
19046 psym = & filedata->dynamic_symbols[iconf[cnt]];
19047 print_vma (psym->st_value, FULL_HEX);
19048 putchar (' ');
19049 if (valid_dynamic_name (filedata, psym->st_name))
19050 print_symbol (25, get_dynamic_name (filedata, psym->st_name));
19051 else
19052 printf (_("<corrupt: %14ld>"), psym->st_name);
19053 }
19054 putchar ('\n');
19055 }
19056
19057 free (iconf);
19058 }
19059
19060 if (pltgot != 0 && local_gotno != 0)
19061 {
19062 uint64_t ent, local_end, global_end;
19063 size_t i, offset;
19064 unsigned char * data;
19065 unsigned char * data_end;
19066 int addr_size;
19067
19068 ent = pltgot;
19069 addr_size = (is_32bit_elf ? 4 : 8);
19070 local_end = pltgot + local_gotno * addr_size;
19071
19072 /* PR binutils/17533 file: 012-111227-0.004 */
19073 if (symtabno < gotsym)
19074 {
19075 error (_("The GOT symbol offset (%lu) is greater than the symbol table size (%lu)\n"),
19076 (unsigned long) gotsym, (unsigned long) symtabno);
19077 return false;
19078 }
19079
19080 global_end = local_end + (symtabno - gotsym) * addr_size;
19081 /* PR 17531: file: 54c91a34. */
19082 if (global_end < local_end)
19083 {
19084 error (_("Too many GOT symbols: %lu\n"), (unsigned long) symtabno);
19085 return false;
19086 }
19087
19088 offset = offset_from_vma (filedata, pltgot, global_end - pltgot);
19089 data = (unsigned char *) get_data (NULL, filedata, offset,
19090 global_end - pltgot, 1,
19091 _("Global Offset Table data"));
19092 /* PR 12855: Null data is handled gracefully throughout. */
19093 data_end = data + (global_end - pltgot);
19094
19095 printf (_("\nPrimary GOT:\n"));
19096 printf (_(" Canonical gp value: "));
19097 print_vma (pltgot + 0x7ff0, LONG_HEX);
19098 printf ("\n\n");
19099
19100 printf (_(" Reserved entries:\n"));
19101 printf (_(" %*s %10s %*s Purpose\n"),
19102 addr_size * 2, _("Address"), _("Access"),
19103 addr_size * 2, _("Initial"));
19104 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19105 printf (_(" Lazy resolver\n"));
19106 if (ent == (uint64_t) -1)
19107 goto got_print_fail;
19108
19109 /* Check for the MSB of GOT[1] being set, denoting a GNU object.
19110 This entry will be used by some runtime loaders, to store the
19111 module pointer. Otherwise this is an ordinary local entry.
19112 PR 21344: Check for the entry being fully available before
19113 fetching it. */
19114 if (data
19115 && data + ent - pltgot + addr_size <= data_end
19116 && (byte_get (data + ent - pltgot, addr_size)
19117 >> (addr_size * 8 - 1)) != 0)
19118 {
19119 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19120 printf (_(" Module pointer (GNU extension)\n"));
19121 if (ent == (uint64_t) -1)
19122 goto got_print_fail;
19123 }
19124 printf ("\n");
19125
19126 if (data != NULL && ent < local_end)
19127 {
19128 printf (_(" Local entries:\n"));
19129 printf (" %*s %10s %*s\n",
19130 addr_size * 2, _("Address"), _("Access"),
19131 addr_size * 2, _("Initial"));
19132 while (ent < local_end)
19133 {
19134 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19135 printf ("\n");
19136 if (ent == (uint64_t) -1)
19137 goto got_print_fail;
19138 }
19139 printf ("\n");
19140 }
19141
19142 if (data != NULL && gotsym < symtabno)
19143 {
19144 int sym_width;
19145
19146 printf (_(" Global entries:\n"));
19147 printf (" %*s %10s %*s %*s %-7s %3s %s\n",
19148 addr_size * 2, _("Address"),
19149 _("Access"),
19150 addr_size * 2, _("Initial"),
19151 addr_size * 2, _("Sym.Val."),
19152 _("Type"),
19153 /* Note for translators: "Ndx" = abbreviated form of "Index". */
19154 _("Ndx"), _("Name"));
19155
19156 sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
19157
19158 for (i = gotsym; i < symtabno; i++)
19159 {
19160 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19161 printf (" ");
19162
19163 if (filedata->dynamic_symbols == NULL)
19164 printf (_("<no dynamic symbols>"));
19165 else if (i < filedata->num_dynamic_syms)
19166 {
19167 Elf_Internal_Sym * psym = filedata->dynamic_symbols + i;
19168
19169 print_vma (psym->st_value, LONG_HEX);
19170 printf (" %-7s %3s ",
19171 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
19172 get_symbol_index_type (filedata, psym->st_shndx));
19173
19174 if (valid_dynamic_name (filedata, psym->st_name))
19175 print_symbol (sym_width,
19176 get_dynamic_name (filedata, psym->st_name));
19177 else
19178 printf (_("<corrupt: %14ld>"), psym->st_name);
19179 }
19180 else
19181 printf (_("<symbol index %lu exceeds number of dynamic symbols>"),
19182 (unsigned long) i);
19183
19184 printf ("\n");
19185 if (ent == (uint64_t) -1)
19186 break;
19187 }
19188 printf ("\n");
19189 }
19190
19191 got_print_fail:
19192 free (data);
19193 }
19194
19195 if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
19196 {
19197 uint64_t ent, end;
19198 size_t offset, rel_offset;
19199 unsigned long count, i;
19200 unsigned char * data;
19201 int addr_size, sym_width;
19202 Elf_Internal_Rela * rels;
19203
19204 rel_offset = offset_from_vma (filedata, jmprel, pltrelsz);
19205 if (pltrel == DT_RELA)
19206 {
19207 if (!slurp_rela_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
19208 return false;
19209 }
19210 else
19211 {
19212 if (!slurp_rel_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
19213 return false;
19214 }
19215
19216 ent = mips_pltgot;
19217 addr_size = (is_32bit_elf ? 4 : 8);
19218 end = mips_pltgot + (2 + count) * addr_size;
19219
19220 offset = offset_from_vma (filedata, mips_pltgot, end - mips_pltgot);
19221 data = (unsigned char *) get_data (NULL, filedata, offset, end - mips_pltgot,
19222 1, _("Procedure Linkage Table data"));
19223 if (data == NULL)
19224 {
19225 free (rels);
19226 return false;
19227 }
19228
19229 printf ("\nPLT GOT:\n\n");
19230 printf (_(" Reserved entries:\n"));
19231 printf (_(" %*s %*s Purpose\n"),
19232 addr_size * 2, _("Address"), addr_size * 2, _("Initial"));
19233 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
19234 printf (_(" PLT lazy resolver\n"));
19235 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
19236 printf (_(" Module pointer\n"));
19237 printf ("\n");
19238
19239 printf (_(" Entries:\n"));
19240 printf (" %*s %*s %*s %-7s %3s %s\n",
19241 addr_size * 2, _("Address"),
19242 addr_size * 2, _("Initial"),
19243 addr_size * 2, _("Sym.Val."), _("Type"), _("Ndx"), _("Name"));
19244 sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
19245 for (i = 0; i < count; i++)
19246 {
19247 unsigned long idx = get_reloc_symindex (rels[i].r_info);
19248
19249 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
19250 printf (" ");
19251
19252 if (idx >= filedata->num_dynamic_syms)
19253 printf (_("<corrupt symbol index: %lu>"), idx);
19254 else
19255 {
19256 Elf_Internal_Sym * psym = filedata->dynamic_symbols + idx;
19257
19258 print_vma (psym->st_value, LONG_HEX);
19259 printf (" %-7s %3s ",
19260 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
19261 get_symbol_index_type (filedata, psym->st_shndx));
19262 if (valid_dynamic_name (filedata, psym->st_name))
19263 print_symbol (sym_width,
19264 get_dynamic_name (filedata, psym->st_name));
19265 else
19266 printf (_("<corrupt: %14ld>"), psym->st_name);
19267 }
19268 printf ("\n");
19269 }
19270 printf ("\n");
19271
19272 free (data);
19273 free (rels);
19274 }
19275
19276 return res;
19277 }
19278
19279 static bool
19280 process_nds32_specific (Filedata * filedata)
19281 {
19282 Elf_Internal_Shdr *sect = NULL;
19283
19284 sect = find_section (filedata, ".nds32_e_flags");
19285 if (sect != NULL && sect->sh_size >= 4)
19286 {
19287 unsigned char *buf;
19288 unsigned int flag;
19289
19290 printf ("\nNDS32 elf flags section:\n");
19291 buf = get_data (NULL, filedata, sect->sh_offset, 1, 4,
19292 _("NDS32 elf flags section"));
19293
19294 if (buf == NULL)
19295 return false;
19296
19297 flag = byte_get (buf, 4);
19298 free (buf);
19299 switch (flag & 0x3)
19300 {
19301 case 0:
19302 printf ("(VEC_SIZE):\tNo entry.\n");
19303 break;
19304 case 1:
19305 printf ("(VEC_SIZE):\t4 bytes\n");
19306 break;
19307 case 2:
19308 printf ("(VEC_SIZE):\t16 bytes\n");
19309 break;
19310 case 3:
19311 printf ("(VEC_SIZE):\treserved\n");
19312 break;
19313 }
19314 }
19315
19316 return true;
19317 }
19318
19319 static bool
19320 process_gnu_liblist (Filedata * filedata)
19321 {
19322 Elf_Internal_Shdr * section;
19323 Elf_Internal_Shdr * string_sec;
19324 Elf32_External_Lib * elib;
19325 char * strtab;
19326 size_t strtab_size;
19327 size_t cnt;
19328 unsigned long num_liblist;
19329 unsigned i;
19330 bool res = true;
19331
19332 if (! do_arch)
19333 return true;
19334
19335 for (i = 0, section = filedata->section_headers;
19336 i < filedata->file_header.e_shnum;
19337 i++, section++)
19338 {
19339 switch (section->sh_type)
19340 {
19341 case SHT_GNU_LIBLIST:
19342 if (section->sh_link >= filedata->file_header.e_shnum)
19343 break;
19344
19345 elib = (Elf32_External_Lib *)
19346 get_data (NULL, filedata, section->sh_offset, 1, section->sh_size,
19347 _("liblist section data"));
19348
19349 if (elib == NULL)
19350 {
19351 res = false;
19352 break;
19353 }
19354
19355 string_sec = filedata->section_headers + section->sh_link;
19356 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
19357 string_sec->sh_size,
19358 _("liblist string table"));
19359 if (strtab == NULL
19360 || section->sh_entsize != sizeof (Elf32_External_Lib))
19361 {
19362 free (elib);
19363 free (strtab);
19364 res = false;
19365 break;
19366 }
19367 strtab_size = string_sec->sh_size;
19368
19369 num_liblist = section->sh_size / sizeof (Elf32_External_Lib);
19370 printf (ngettext ("\nLibrary list section '%s' contains %lu entries:\n",
19371 "\nLibrary list section '%s' contains %lu entries:\n",
19372 num_liblist),
19373 printable_section_name (filedata, section),
19374 num_liblist);
19375
19376 puts (_(" Library Time Stamp Checksum Version Flags"));
19377
19378 for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
19379 ++cnt)
19380 {
19381 Elf32_Lib liblist;
19382 time_t atime;
19383 char timebuf[128];
19384 struct tm * tmp;
19385
19386 liblist.l_name = BYTE_GET (elib[cnt].l_name);
19387 atime = BYTE_GET (elib[cnt].l_time_stamp);
19388 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
19389 liblist.l_version = BYTE_GET (elib[cnt].l_version);
19390 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
19391
19392 tmp = gmtime (&atime);
19393 snprintf (timebuf, sizeof (timebuf),
19394 "%04u-%02u-%02uT%02u:%02u:%02u",
19395 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
19396 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
19397
19398 printf ("%3lu: ", (unsigned long) cnt);
19399 if (do_wide)
19400 printf ("%-20s", liblist.l_name < strtab_size
19401 ? strtab + liblist.l_name : _("<corrupt>"));
19402 else
19403 printf ("%-20.20s", liblist.l_name < strtab_size
19404 ? strtab + liblist.l_name : _("<corrupt>"));
19405 printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
19406 liblist.l_version, liblist.l_flags);
19407 }
19408
19409 free (elib);
19410 free (strtab);
19411 }
19412 }
19413
19414 return res;
19415 }
19416
19417 static const char *
19418 get_note_type (Filedata * filedata, unsigned e_type)
19419 {
19420 static char buff[64];
19421
19422 if (filedata->file_header.e_type == ET_CORE)
19423 switch (e_type)
19424 {
19425 case NT_AUXV:
19426 return _("NT_AUXV (auxiliary vector)");
19427 case NT_PRSTATUS:
19428 return _("NT_PRSTATUS (prstatus structure)");
19429 case NT_FPREGSET:
19430 return _("NT_FPREGSET (floating point registers)");
19431 case NT_PRPSINFO:
19432 return _("NT_PRPSINFO (prpsinfo structure)");
19433 case NT_TASKSTRUCT:
19434 return _("NT_TASKSTRUCT (task structure)");
19435 case NT_GDB_TDESC:
19436 return _("NT_GDB_TDESC (GDB XML target description)");
19437 case NT_PRXFPREG:
19438 return _("NT_PRXFPREG (user_xfpregs structure)");
19439 case NT_PPC_VMX:
19440 return _("NT_PPC_VMX (ppc Altivec registers)");
19441 case NT_PPC_VSX:
19442 return _("NT_PPC_VSX (ppc VSX registers)");
19443 case NT_PPC_TAR:
19444 return _("NT_PPC_TAR (ppc TAR register)");
19445 case NT_PPC_PPR:
19446 return _("NT_PPC_PPR (ppc PPR register)");
19447 case NT_PPC_DSCR:
19448 return _("NT_PPC_DSCR (ppc DSCR register)");
19449 case NT_PPC_EBB:
19450 return _("NT_PPC_EBB (ppc EBB registers)");
19451 case NT_PPC_PMU:
19452 return _("NT_PPC_PMU (ppc PMU registers)");
19453 case NT_PPC_TM_CGPR:
19454 return _("NT_PPC_TM_CGPR (ppc checkpointed GPR registers)");
19455 case NT_PPC_TM_CFPR:
19456 return _("NT_PPC_TM_CFPR (ppc checkpointed floating point registers)");
19457 case NT_PPC_TM_CVMX:
19458 return _("NT_PPC_TM_CVMX (ppc checkpointed Altivec registers)");
19459 case NT_PPC_TM_CVSX:
19460 return _("NT_PPC_TM_CVSX (ppc checkpointed VSX registers)");
19461 case NT_PPC_TM_SPR:
19462 return _("NT_PPC_TM_SPR (ppc TM special purpose registers)");
19463 case NT_PPC_TM_CTAR:
19464 return _("NT_PPC_TM_CTAR (ppc checkpointed TAR register)");
19465 case NT_PPC_TM_CPPR:
19466 return _("NT_PPC_TM_CPPR (ppc checkpointed PPR register)");
19467 case NT_PPC_TM_CDSCR:
19468 return _("NT_PPC_TM_CDSCR (ppc checkpointed DSCR register)");
19469 case NT_386_TLS:
19470 return _("NT_386_TLS (x86 TLS information)");
19471 case NT_386_IOPERM:
19472 return _("NT_386_IOPERM (x86 I/O permissions)");
19473 case NT_X86_XSTATE:
19474 return _("NT_X86_XSTATE (x86 XSAVE extended state)");
19475 case NT_X86_CET:
19476 return _("NT_X86_CET (x86 CET state)");
19477 case NT_S390_HIGH_GPRS:
19478 return _("NT_S390_HIGH_GPRS (s390 upper register halves)");
19479 case NT_S390_TIMER:
19480 return _("NT_S390_TIMER (s390 timer register)");
19481 case NT_S390_TODCMP:
19482 return _("NT_S390_TODCMP (s390 TOD comparator register)");
19483 case NT_S390_TODPREG:
19484 return _("NT_S390_TODPREG (s390 TOD programmable register)");
19485 case NT_S390_CTRS:
19486 return _("NT_S390_CTRS (s390 control registers)");
19487 case NT_S390_PREFIX:
19488 return _("NT_S390_PREFIX (s390 prefix register)");
19489 case NT_S390_LAST_BREAK:
19490 return _("NT_S390_LAST_BREAK (s390 last breaking event address)");
19491 case NT_S390_SYSTEM_CALL:
19492 return _("NT_S390_SYSTEM_CALL (s390 system call restart data)");
19493 case NT_S390_TDB:
19494 return _("NT_S390_TDB (s390 transaction diagnostic block)");
19495 case NT_S390_VXRS_LOW:
19496 return _("NT_S390_VXRS_LOW (s390 vector registers 0-15 upper half)");
19497 case NT_S390_VXRS_HIGH:
19498 return _("NT_S390_VXRS_HIGH (s390 vector registers 16-31)");
19499 case NT_S390_GS_CB:
19500 return _("NT_S390_GS_CB (s390 guarded-storage registers)");
19501 case NT_S390_GS_BC:
19502 return _("NT_S390_GS_BC (s390 guarded-storage broadcast control)");
19503 case NT_ARM_VFP:
19504 return _("NT_ARM_VFP (arm VFP registers)");
19505 case NT_ARM_TLS:
19506 return _("NT_ARM_TLS (AArch TLS registers)");
19507 case NT_ARM_HW_BREAK:
19508 return _("NT_ARM_HW_BREAK (AArch hardware breakpoint registers)");
19509 case NT_ARM_HW_WATCH:
19510 return _("NT_ARM_HW_WATCH (AArch hardware watchpoint registers)");
19511 case NT_ARM_SYSTEM_CALL:
19512 return _("NT_ARM_SYSTEM_CALL (AArch system call number)");
19513 case NT_ARM_SVE:
19514 return _("NT_ARM_SVE (AArch SVE registers)");
19515 case NT_ARM_PAC_MASK:
19516 return _("NT_ARM_PAC_MASK (AArch pointer authentication code masks)");
19517 case NT_ARM_PACA_KEYS:
19518 return _("NT_ARM_PACA_KEYS (ARM pointer authentication address keys)");
19519 case NT_ARM_PACG_KEYS:
19520 return _("NT_ARM_PACG_KEYS (ARM pointer authentication generic keys)");
19521 case NT_ARM_TAGGED_ADDR_CTRL:
19522 return _("NT_ARM_TAGGED_ADDR_CTRL (AArch tagged address control)");
19523 case NT_ARM_PAC_ENABLED_KEYS:
19524 return _("NT_ARM_PAC_ENABLED_KEYS (AArch64 pointer authentication enabled keys)");
19525 case NT_ARC_V2:
19526 return _("NT_ARC_V2 (ARC HS accumulator/extra registers)");
19527 case NT_RISCV_CSR:
19528 return _("NT_RISCV_CSR (RISC-V control and status registers)");
19529 case NT_PSTATUS:
19530 return _("NT_PSTATUS (pstatus structure)");
19531 case NT_FPREGS:
19532 return _("NT_FPREGS (floating point registers)");
19533 case NT_PSINFO:
19534 return _("NT_PSINFO (psinfo structure)");
19535 case NT_LWPSTATUS:
19536 return _("NT_LWPSTATUS (lwpstatus_t structure)");
19537 case NT_LWPSINFO:
19538 return _("NT_LWPSINFO (lwpsinfo_t structure)");
19539 case NT_WIN32PSTATUS:
19540 return _("NT_WIN32PSTATUS (win32_pstatus structure)");
19541 case NT_SIGINFO:
19542 return _("NT_SIGINFO (siginfo_t data)");
19543 case NT_FILE:
19544 return _("NT_FILE (mapped files)");
19545 default:
19546 break;
19547 }
19548 else
19549 switch (e_type)
19550 {
19551 case NT_VERSION:
19552 return _("NT_VERSION (version)");
19553 case NT_ARCH:
19554 return _("NT_ARCH (architecture)");
19555 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
19556 return _("OPEN");
19557 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
19558 return _("func");
19559 case NT_GO_BUILDID:
19560 return _("GO BUILDID");
19561 case FDO_PACKAGING_METADATA:
19562 return _("FDO_PACKAGING_METADATA");
19563 default:
19564 break;
19565 }
19566
19567 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19568 return buff;
19569 }
19570
19571 static bool
19572 print_core_note (Elf_Internal_Note *pnote)
19573 {
19574 unsigned int addr_size = is_32bit_elf ? 4 : 8;
19575 uint64_t count, page_size;
19576 unsigned char *descdata, *filenames, *descend;
19577
19578 if (pnote->type != NT_FILE)
19579 {
19580 if (do_wide)
19581 printf ("\n");
19582 return true;
19583 }
19584
19585 if (!is_32bit_elf)
19586 {
19587 printf (_(" Cannot decode 64-bit note in 32-bit build\n"));
19588 /* Still "successful". */
19589 return true;
19590 }
19591
19592 if (pnote->descsz < 2 * addr_size)
19593 {
19594 error (_(" Malformed note - too short for header\n"));
19595 return false;
19596 }
19597
19598 descdata = (unsigned char *) pnote->descdata;
19599 descend = descdata + pnote->descsz;
19600
19601 if (descdata[pnote->descsz - 1] != '\0')
19602 {
19603 error (_(" Malformed note - does not end with \\0\n"));
19604 return false;
19605 }
19606
19607 count = byte_get (descdata, addr_size);
19608 descdata += addr_size;
19609
19610 page_size = byte_get (descdata, addr_size);
19611 descdata += addr_size;
19612
19613 if (count > ((uint64_t) -1 - 2 * addr_size) / (3 * addr_size)
19614 || pnote->descsz < 2 * addr_size + count * 3 * addr_size)
19615 {
19616 error (_(" Malformed note - too short for supplied file count\n"));
19617 return false;
19618 }
19619
19620 printf (_(" Page size: "));
19621 print_vma (page_size, DEC);
19622 printf ("\n");
19623
19624 printf (_(" %*s%*s%*s\n"),
19625 (int) (2 + 2 * addr_size), _("Start"),
19626 (int) (4 + 2 * addr_size), _("End"),
19627 (int) (4 + 2 * addr_size), _("Page Offset"));
19628 filenames = descdata + count * 3 * addr_size;
19629 while (count-- > 0)
19630 {
19631 uint64_t start, end, file_ofs;
19632
19633 if (filenames == descend)
19634 {
19635 error (_(" Malformed note - filenames end too early\n"));
19636 return false;
19637 }
19638
19639 start = byte_get (descdata, addr_size);
19640 descdata += addr_size;
19641 end = byte_get (descdata, addr_size);
19642 descdata += addr_size;
19643 file_ofs = byte_get (descdata, addr_size);
19644 descdata += addr_size;
19645
19646 printf (" ");
19647 print_vma (start, FULL_HEX);
19648 printf (" ");
19649 print_vma (end, FULL_HEX);
19650 printf (" ");
19651 print_vma (file_ofs, FULL_HEX);
19652 printf ("\n %s\n", filenames);
19653
19654 filenames += 1 + strlen ((char *) filenames);
19655 }
19656
19657 return true;
19658 }
19659
19660 static const char *
19661 get_gnu_elf_note_type (unsigned e_type)
19662 {
19663 /* NB/ Keep this switch statement in sync with print_gnu_note (). */
19664 switch (e_type)
19665 {
19666 case NT_GNU_ABI_TAG:
19667 return _("NT_GNU_ABI_TAG (ABI version tag)");
19668 case NT_GNU_HWCAP:
19669 return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
19670 case NT_GNU_BUILD_ID:
19671 return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
19672 case NT_GNU_GOLD_VERSION:
19673 return _("NT_GNU_GOLD_VERSION (gold version)");
19674 case NT_GNU_PROPERTY_TYPE_0:
19675 return _("NT_GNU_PROPERTY_TYPE_0");
19676 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
19677 return _("NT_GNU_BUILD_ATTRIBUTE_OPEN");
19678 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
19679 return _("NT_GNU_BUILD_ATTRIBUTE_FUNC");
19680 default:
19681 {
19682 static char buff[64];
19683
19684 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19685 return buff;
19686 }
19687 }
19688 }
19689
19690 static void
19691 decode_x86_compat_isa (unsigned int bitmask)
19692 {
19693 while (bitmask)
19694 {
19695 unsigned int bit = bitmask & (- bitmask);
19696
19697 bitmask &= ~ bit;
19698 switch (bit)
19699 {
19700 case GNU_PROPERTY_X86_COMPAT_ISA_1_486:
19701 printf ("i486");
19702 break;
19703 case GNU_PROPERTY_X86_COMPAT_ISA_1_586:
19704 printf ("586");
19705 break;
19706 case GNU_PROPERTY_X86_COMPAT_ISA_1_686:
19707 printf ("686");
19708 break;
19709 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE:
19710 printf ("SSE");
19711 break;
19712 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE2:
19713 printf ("SSE2");
19714 break;
19715 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE3:
19716 printf ("SSE3");
19717 break;
19718 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSSE3:
19719 printf ("SSSE3");
19720 break;
19721 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_1:
19722 printf ("SSE4_1");
19723 break;
19724 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_2:
19725 printf ("SSE4_2");
19726 break;
19727 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX:
19728 printf ("AVX");
19729 break;
19730 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX2:
19731 printf ("AVX2");
19732 break;
19733 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512F:
19734 printf ("AVX512F");
19735 break;
19736 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512CD:
19737 printf ("AVX512CD");
19738 break;
19739 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512ER:
19740 printf ("AVX512ER");
19741 break;
19742 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512PF:
19743 printf ("AVX512PF");
19744 break;
19745 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512VL:
19746 printf ("AVX512VL");
19747 break;
19748 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512DQ:
19749 printf ("AVX512DQ");
19750 break;
19751 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512BW:
19752 printf ("AVX512BW");
19753 break;
19754 default:
19755 printf (_("<unknown: %x>"), bit);
19756 break;
19757 }
19758 if (bitmask)
19759 printf (", ");
19760 }
19761 }
19762
19763 static void
19764 decode_x86_compat_2_isa (unsigned int bitmask)
19765 {
19766 if (!bitmask)
19767 {
19768 printf (_("<None>"));
19769 return;
19770 }
19771
19772 while (bitmask)
19773 {
19774 unsigned int bit = bitmask & (- bitmask);
19775
19776 bitmask &= ~ bit;
19777 switch (bit)
19778 {
19779 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_CMOV:
19780 printf ("CMOV");
19781 break;
19782 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE:
19783 printf ("SSE");
19784 break;
19785 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE2:
19786 printf ("SSE2");
19787 break;
19788 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE3:
19789 printf ("SSE3");
19790 break;
19791 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSSE3:
19792 printf ("SSSE3");
19793 break;
19794 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_1:
19795 printf ("SSE4_1");
19796 break;
19797 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_2:
19798 printf ("SSE4_2");
19799 break;
19800 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX:
19801 printf ("AVX");
19802 break;
19803 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX2:
19804 printf ("AVX2");
19805 break;
19806 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_FMA:
19807 printf ("FMA");
19808 break;
19809 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512F:
19810 printf ("AVX512F");
19811 break;
19812 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512CD:
19813 printf ("AVX512CD");
19814 break;
19815 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512ER:
19816 printf ("AVX512ER");
19817 break;
19818 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512PF:
19819 printf ("AVX512PF");
19820 break;
19821 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512VL:
19822 printf ("AVX512VL");
19823 break;
19824 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512DQ:
19825 printf ("AVX512DQ");
19826 break;
19827 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512BW:
19828 printf ("AVX512BW");
19829 break;
19830 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4FMAPS:
19831 printf ("AVX512_4FMAPS");
19832 break;
19833 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4VNNIW:
19834 printf ("AVX512_4VNNIW");
19835 break;
19836 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BITALG:
19837 printf ("AVX512_BITALG");
19838 break;
19839 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_IFMA:
19840 printf ("AVX512_IFMA");
19841 break;
19842 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI:
19843 printf ("AVX512_VBMI");
19844 break;
19845 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI2:
19846 printf ("AVX512_VBMI2");
19847 break;
19848 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VNNI:
19849 printf ("AVX512_VNNI");
19850 break;
19851 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BF16:
19852 printf ("AVX512_BF16");
19853 break;
19854 default:
19855 printf (_("<unknown: %x>"), bit);
19856 break;
19857 }
19858 if (bitmask)
19859 printf (", ");
19860 }
19861 }
19862
19863 static const char *
19864 get_amdgpu_elf_note_type (unsigned int e_type)
19865 {
19866 switch (e_type)
19867 {
19868 case NT_AMDGPU_METADATA:
19869 return _("NT_AMDGPU_METADATA (code object metadata)");
19870 default:
19871 {
19872 static char buf[64];
19873 snprintf (buf, sizeof (buf), _("Unknown note type: (0x%08x)"), e_type);
19874 return buf;
19875 }
19876 }
19877 }
19878
19879 static void
19880 decode_x86_isa (unsigned int bitmask)
19881 {
19882 while (bitmask)
19883 {
19884 unsigned int bit = bitmask & (- bitmask);
19885
19886 bitmask &= ~ bit;
19887 switch (bit)
19888 {
19889 case GNU_PROPERTY_X86_ISA_1_BASELINE:
19890 printf ("x86-64-baseline");
19891 break;
19892 case GNU_PROPERTY_X86_ISA_1_V2:
19893 printf ("x86-64-v2");
19894 break;
19895 case GNU_PROPERTY_X86_ISA_1_V3:
19896 printf ("x86-64-v3");
19897 break;
19898 case GNU_PROPERTY_X86_ISA_1_V4:
19899 printf ("x86-64-v4");
19900 break;
19901 default:
19902 printf (_("<unknown: %x>"), bit);
19903 break;
19904 }
19905 if (bitmask)
19906 printf (", ");
19907 }
19908 }
19909
19910 static void
19911 decode_x86_feature_1 (unsigned int bitmask)
19912 {
19913 if (!bitmask)
19914 {
19915 printf (_("<None>"));
19916 return;
19917 }
19918
19919 while (bitmask)
19920 {
19921 unsigned int bit = bitmask & (- bitmask);
19922
19923 bitmask &= ~ bit;
19924 switch (bit)
19925 {
19926 case GNU_PROPERTY_X86_FEATURE_1_IBT:
19927 printf ("IBT");
19928 break;
19929 case GNU_PROPERTY_X86_FEATURE_1_SHSTK:
19930 printf ("SHSTK");
19931 break;
19932 case GNU_PROPERTY_X86_FEATURE_1_LAM_U48:
19933 printf ("LAM_U48");
19934 break;
19935 case GNU_PROPERTY_X86_FEATURE_1_LAM_U57:
19936 printf ("LAM_U57");
19937 break;
19938 default:
19939 printf (_("<unknown: %x>"), bit);
19940 break;
19941 }
19942 if (bitmask)
19943 printf (", ");
19944 }
19945 }
19946
19947 static void
19948 decode_x86_feature_2 (unsigned int bitmask)
19949 {
19950 if (!bitmask)
19951 {
19952 printf (_("<None>"));
19953 return;
19954 }
19955
19956 while (bitmask)
19957 {
19958 unsigned int bit = bitmask & (- bitmask);
19959
19960 bitmask &= ~ bit;
19961 switch (bit)
19962 {
19963 case GNU_PROPERTY_X86_FEATURE_2_X86:
19964 printf ("x86");
19965 break;
19966 case GNU_PROPERTY_X86_FEATURE_2_X87:
19967 printf ("x87");
19968 break;
19969 case GNU_PROPERTY_X86_FEATURE_2_MMX:
19970 printf ("MMX");
19971 break;
19972 case GNU_PROPERTY_X86_FEATURE_2_XMM:
19973 printf ("XMM");
19974 break;
19975 case GNU_PROPERTY_X86_FEATURE_2_YMM:
19976 printf ("YMM");
19977 break;
19978 case GNU_PROPERTY_X86_FEATURE_2_ZMM:
19979 printf ("ZMM");
19980 break;
19981 case GNU_PROPERTY_X86_FEATURE_2_TMM:
19982 printf ("TMM");
19983 break;
19984 case GNU_PROPERTY_X86_FEATURE_2_MASK:
19985 printf ("MASK");
19986 break;
19987 case GNU_PROPERTY_X86_FEATURE_2_FXSR:
19988 printf ("FXSR");
19989 break;
19990 case GNU_PROPERTY_X86_FEATURE_2_XSAVE:
19991 printf ("XSAVE");
19992 break;
19993 case GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT:
19994 printf ("XSAVEOPT");
19995 break;
19996 case GNU_PROPERTY_X86_FEATURE_2_XSAVEC:
19997 printf ("XSAVEC");
19998 break;
19999 default:
20000 printf (_("<unknown: %x>"), bit);
20001 break;
20002 }
20003 if (bitmask)
20004 printf (", ");
20005 }
20006 }
20007
20008 static void
20009 decode_aarch64_feature_1_and (unsigned int bitmask)
20010 {
20011 while (bitmask)
20012 {
20013 unsigned int bit = bitmask & (- bitmask);
20014
20015 bitmask &= ~ bit;
20016 switch (bit)
20017 {
20018 case GNU_PROPERTY_AARCH64_FEATURE_1_BTI:
20019 printf ("BTI");
20020 break;
20021
20022 case GNU_PROPERTY_AARCH64_FEATURE_1_PAC:
20023 printf ("PAC");
20024 break;
20025
20026 default:
20027 printf (_("<unknown: %x>"), bit);
20028 break;
20029 }
20030 if (bitmask)
20031 printf (", ");
20032 }
20033 }
20034
20035 static void
20036 decode_1_needed (unsigned int bitmask)
20037 {
20038 while (bitmask)
20039 {
20040 unsigned int bit = bitmask & (- bitmask);
20041
20042 bitmask &= ~ bit;
20043 switch (bit)
20044 {
20045 case GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS:
20046 printf ("indirect external access");
20047 break;
20048 default:
20049 printf (_("<unknown: %x>"), bit);
20050 break;
20051 }
20052 if (bitmask)
20053 printf (", ");
20054 }
20055 }
20056
20057 static void
20058 print_gnu_property_note (Filedata * filedata, Elf_Internal_Note * pnote)
20059 {
20060 unsigned char * ptr = (unsigned char *) pnote->descdata;
20061 unsigned char * ptr_end = ptr + pnote->descsz;
20062 unsigned int size = is_32bit_elf ? 4 : 8;
20063
20064 printf (_(" Properties: "));
20065
20066 if (pnote->descsz < 8 || (pnote->descsz % size) != 0)
20067 {
20068 printf (_("<corrupt GNU_PROPERTY_TYPE, size = %#lx>\n"), pnote->descsz);
20069 return;
20070 }
20071
20072 while (ptr < ptr_end)
20073 {
20074 unsigned int j;
20075 unsigned int type;
20076 unsigned int datasz;
20077
20078 if ((size_t) (ptr_end - ptr) < 8)
20079 {
20080 printf (_("<corrupt descsz: %#lx>\n"), pnote->descsz);
20081 break;
20082 }
20083
20084 type = byte_get (ptr, 4);
20085 datasz = byte_get (ptr + 4, 4);
20086
20087 ptr += 8;
20088
20089 if (datasz > (size_t) (ptr_end - ptr))
20090 {
20091 printf (_("<corrupt type (%#x) datasz: %#x>\n"),
20092 type, datasz);
20093 break;
20094 }
20095
20096 if (type >= GNU_PROPERTY_LOPROC && type <= GNU_PROPERTY_HIPROC)
20097 {
20098 if (filedata->file_header.e_machine == EM_X86_64
20099 || filedata->file_header.e_machine == EM_IAMCU
20100 || filedata->file_header.e_machine == EM_386)
20101 {
20102 unsigned int bitmask;
20103
20104 if (datasz == 4)
20105 bitmask = byte_get (ptr, 4);
20106 else
20107 bitmask = 0;
20108
20109 switch (type)
20110 {
20111 case GNU_PROPERTY_X86_ISA_1_USED:
20112 if (datasz != 4)
20113 printf (_("x86 ISA used: <corrupt length: %#x> "),
20114 datasz);
20115 else
20116 {
20117 printf ("x86 ISA used: ");
20118 decode_x86_isa (bitmask);
20119 }
20120 goto next;
20121
20122 case GNU_PROPERTY_X86_ISA_1_NEEDED:
20123 if (datasz != 4)
20124 printf (_("x86 ISA needed: <corrupt length: %#x> "),
20125 datasz);
20126 else
20127 {
20128 printf ("x86 ISA needed: ");
20129 decode_x86_isa (bitmask);
20130 }
20131 goto next;
20132
20133 case GNU_PROPERTY_X86_FEATURE_1_AND:
20134 if (datasz != 4)
20135 printf (_("x86 feature: <corrupt length: %#x> "),
20136 datasz);
20137 else
20138 {
20139 printf ("x86 feature: ");
20140 decode_x86_feature_1 (bitmask);
20141 }
20142 goto next;
20143
20144 case GNU_PROPERTY_X86_FEATURE_2_USED:
20145 if (datasz != 4)
20146 printf (_("x86 feature used: <corrupt length: %#x> "),
20147 datasz);
20148 else
20149 {
20150 printf ("x86 feature used: ");
20151 decode_x86_feature_2 (bitmask);
20152 }
20153 goto next;
20154
20155 case GNU_PROPERTY_X86_FEATURE_2_NEEDED:
20156 if (datasz != 4)
20157 printf (_("x86 feature needed: <corrupt length: %#x> "), datasz);
20158 else
20159 {
20160 printf ("x86 feature needed: ");
20161 decode_x86_feature_2 (bitmask);
20162 }
20163 goto next;
20164
20165 case GNU_PROPERTY_X86_COMPAT_ISA_1_USED:
20166 if (datasz != 4)
20167 printf (_("x86 ISA used: <corrupt length: %#x> "),
20168 datasz);
20169 else
20170 {
20171 printf ("x86 ISA used: ");
20172 decode_x86_compat_isa (bitmask);
20173 }
20174 goto next;
20175
20176 case GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED:
20177 if (datasz != 4)
20178 printf (_("x86 ISA needed: <corrupt length: %#x> "),
20179 datasz);
20180 else
20181 {
20182 printf ("x86 ISA needed: ");
20183 decode_x86_compat_isa (bitmask);
20184 }
20185 goto next;
20186
20187 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_USED:
20188 if (datasz != 4)
20189 printf (_("x86 ISA used: <corrupt length: %#x> "),
20190 datasz);
20191 else
20192 {
20193 printf ("x86 ISA used: ");
20194 decode_x86_compat_2_isa (bitmask);
20195 }
20196 goto next;
20197
20198 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_NEEDED:
20199 if (datasz != 4)
20200 printf (_("x86 ISA needed: <corrupt length: %#x> "),
20201 datasz);
20202 else
20203 {
20204 printf ("x86 ISA needed: ");
20205 decode_x86_compat_2_isa (bitmask);
20206 }
20207 goto next;
20208
20209 default:
20210 break;
20211 }
20212 }
20213 else if (filedata->file_header.e_machine == EM_AARCH64)
20214 {
20215 if (type == GNU_PROPERTY_AARCH64_FEATURE_1_AND)
20216 {
20217 printf ("AArch64 feature: ");
20218 if (datasz != 4)
20219 printf (_("<corrupt length: %#x> "), datasz);
20220 else
20221 decode_aarch64_feature_1_and (byte_get (ptr, 4));
20222 goto next;
20223 }
20224 }
20225 }
20226 else
20227 {
20228 switch (type)
20229 {
20230 case GNU_PROPERTY_STACK_SIZE:
20231 printf (_("stack size: "));
20232 if (datasz != size)
20233 printf (_("<corrupt length: %#x> "), datasz);
20234 else
20235 printf ("%#lx", (unsigned long) byte_get (ptr, size));
20236 goto next;
20237
20238 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
20239 printf ("no copy on protected ");
20240 if (datasz)
20241 printf (_("<corrupt length: %#x> "), datasz);
20242 goto next;
20243
20244 default:
20245 if ((type >= GNU_PROPERTY_UINT32_AND_LO
20246 && type <= GNU_PROPERTY_UINT32_AND_HI)
20247 || (type >= GNU_PROPERTY_UINT32_OR_LO
20248 && type <= GNU_PROPERTY_UINT32_OR_HI))
20249 {
20250 switch (type)
20251 {
20252 case GNU_PROPERTY_1_NEEDED:
20253 if (datasz != 4)
20254 printf (_("1_needed: <corrupt length: %#x> "),
20255 datasz);
20256 else
20257 {
20258 unsigned int bitmask = byte_get (ptr, 4);
20259 printf ("1_needed: ");
20260 decode_1_needed (bitmask);
20261 }
20262 goto next;
20263
20264 default:
20265 break;
20266 }
20267 if (type <= GNU_PROPERTY_UINT32_AND_HI)
20268 printf (_("UINT32_AND (%#x): "), type);
20269 else
20270 printf (_("UINT32_OR (%#x): "), type);
20271 if (datasz != 4)
20272 printf (_("<corrupt length: %#x> "), datasz);
20273 else
20274 printf ("%#x", (unsigned int) byte_get (ptr, 4));
20275 goto next;
20276 }
20277 break;
20278 }
20279 }
20280
20281 if (type < GNU_PROPERTY_LOPROC)
20282 printf (_("<unknown type %#x data: "), type);
20283 else if (type < GNU_PROPERTY_LOUSER)
20284 printf (_("<processor-specific type %#x data: "), type);
20285 else
20286 printf (_("<application-specific type %#x data: "), type);
20287 for (j = 0; j < datasz; ++j)
20288 printf ("%02x ", ptr[j] & 0xff);
20289 printf (">");
20290
20291 next:
20292 ptr += ((datasz + (size - 1)) & ~ (size - 1));
20293 if (ptr == ptr_end)
20294 break;
20295
20296 if (do_wide)
20297 printf (", ");
20298 else
20299 printf ("\n\t");
20300 }
20301
20302 printf ("\n");
20303 }
20304
20305 static bool
20306 print_gnu_note (Filedata * filedata, Elf_Internal_Note *pnote)
20307 {
20308 /* NB/ Keep this switch statement in sync with get_gnu_elf_note_type (). */
20309 switch (pnote->type)
20310 {
20311 case NT_GNU_BUILD_ID:
20312 {
20313 unsigned long i;
20314
20315 printf (_(" Build ID: "));
20316 for (i = 0; i < pnote->descsz; ++i)
20317 printf ("%02x", pnote->descdata[i] & 0xff);
20318 printf ("\n");
20319 }
20320 break;
20321
20322 case NT_GNU_ABI_TAG:
20323 {
20324 unsigned long os, major, minor, subminor;
20325 const char *osname;
20326
20327 /* PR 17531: file: 030-599401-0.004. */
20328 if (pnote->descsz < 16)
20329 {
20330 printf (_(" <corrupt GNU_ABI_TAG>\n"));
20331 break;
20332 }
20333
20334 os = byte_get ((unsigned char *) pnote->descdata, 4);
20335 major = byte_get ((unsigned char *) pnote->descdata + 4, 4);
20336 minor = byte_get ((unsigned char *) pnote->descdata + 8, 4);
20337 subminor = byte_get ((unsigned char *) pnote->descdata + 12, 4);
20338
20339 switch (os)
20340 {
20341 case GNU_ABI_TAG_LINUX:
20342 osname = "Linux";
20343 break;
20344 case GNU_ABI_TAG_HURD:
20345 osname = "Hurd";
20346 break;
20347 case GNU_ABI_TAG_SOLARIS:
20348 osname = "Solaris";
20349 break;
20350 case GNU_ABI_TAG_FREEBSD:
20351 osname = "FreeBSD";
20352 break;
20353 case GNU_ABI_TAG_NETBSD:
20354 osname = "NetBSD";
20355 break;
20356 case GNU_ABI_TAG_SYLLABLE:
20357 osname = "Syllable";
20358 break;
20359 case GNU_ABI_TAG_NACL:
20360 osname = "NaCl";
20361 break;
20362 default:
20363 osname = "Unknown";
20364 break;
20365 }
20366
20367 printf (_(" OS: %s, ABI: %ld.%ld.%ld\n"), osname,
20368 major, minor, subminor);
20369 }
20370 break;
20371
20372 case NT_GNU_GOLD_VERSION:
20373 {
20374 unsigned long i;
20375
20376 printf (_(" Version: "));
20377 for (i = 0; i < pnote->descsz && pnote->descdata[i] != '\0'; ++i)
20378 printf ("%c", pnote->descdata[i]);
20379 printf ("\n");
20380 }
20381 break;
20382
20383 case NT_GNU_HWCAP:
20384 {
20385 unsigned long num_entries, mask;
20386
20387 /* Hardware capabilities information. Word 0 is the number of entries.
20388 Word 1 is a bitmask of enabled entries. The rest of the descriptor
20389 is a series of entries, where each entry is a single byte followed
20390 by a nul terminated string. The byte gives the bit number to test
20391 if enabled in the bitmask. */
20392 printf (_(" Hardware Capabilities: "));
20393 if (pnote->descsz < 8)
20394 {
20395 error (_("<corrupt GNU_HWCAP>\n"));
20396 return false;
20397 }
20398 num_entries = byte_get ((unsigned char *) pnote->descdata, 4);
20399 mask = byte_get ((unsigned char *) pnote->descdata + 4, 4);
20400 printf (_("num entries: %ld, enabled mask: %lx\n"), num_entries, mask);
20401 /* FIXME: Add code to display the entries... */
20402 }
20403 break;
20404
20405 case NT_GNU_PROPERTY_TYPE_0:
20406 print_gnu_property_note (filedata, pnote);
20407 break;
20408
20409 default:
20410 /* Handle unrecognised types. An error message should have already been
20411 created by get_gnu_elf_note_type(), so all that we need to do is to
20412 display the data. */
20413 {
20414 unsigned long i;
20415
20416 printf (_(" Description data: "));
20417 for (i = 0; i < pnote->descsz; ++i)
20418 printf ("%02x ", pnote->descdata[i] & 0xff);
20419 printf ("\n");
20420 }
20421 break;
20422 }
20423
20424 return true;
20425 }
20426
20427 static const char *
20428 get_v850_elf_note_type (enum v850_notes n_type)
20429 {
20430 static char buff[64];
20431
20432 switch (n_type)
20433 {
20434 case V850_NOTE_ALIGNMENT: return _("Alignment of 8-byte objects");
20435 case V850_NOTE_DATA_SIZE: return _("Sizeof double and long double");
20436 case V850_NOTE_FPU_INFO: return _("Type of FPU support needed");
20437 case V850_NOTE_SIMD_INFO: return _("Use of SIMD instructions");
20438 case V850_NOTE_CACHE_INFO: return _("Use of cache");
20439 case V850_NOTE_MMU_INFO: return _("Use of MMU");
20440 default:
20441 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), n_type);
20442 return buff;
20443 }
20444 }
20445
20446 static bool
20447 print_v850_note (Elf_Internal_Note * pnote)
20448 {
20449 unsigned int val;
20450
20451 if (pnote->descsz != 4)
20452 return false;
20453
20454 val = byte_get ((unsigned char *) pnote->descdata, pnote->descsz);
20455
20456 if (val == 0)
20457 {
20458 printf (_("not set\n"));
20459 return true;
20460 }
20461
20462 switch (pnote->type)
20463 {
20464 case V850_NOTE_ALIGNMENT:
20465 switch (val)
20466 {
20467 case EF_RH850_DATA_ALIGN4: printf (_("4-byte\n")); return true;
20468 case EF_RH850_DATA_ALIGN8: printf (_("8-byte\n")); return true;
20469 }
20470 break;
20471
20472 case V850_NOTE_DATA_SIZE:
20473 switch (val)
20474 {
20475 case EF_RH850_DOUBLE32: printf (_("4-bytes\n")); return true;
20476 case EF_RH850_DOUBLE64: printf (_("8-bytes\n")); return true;
20477 }
20478 break;
20479
20480 case V850_NOTE_FPU_INFO:
20481 switch (val)
20482 {
20483 case EF_RH850_FPU20: printf (_("FPU-2.0\n")); return true;
20484 case EF_RH850_FPU30: printf (_("FPU-3.0\n")); return true;
20485 }
20486 break;
20487
20488 case V850_NOTE_MMU_INFO:
20489 case V850_NOTE_CACHE_INFO:
20490 case V850_NOTE_SIMD_INFO:
20491 if (val == EF_RH850_SIMD)
20492 {
20493 printf (_("yes\n"));
20494 return true;
20495 }
20496 break;
20497
20498 default:
20499 /* An 'unknown note type' message will already have been displayed. */
20500 break;
20501 }
20502
20503 printf (_("unknown value: %x\n"), val);
20504 return false;
20505 }
20506
20507 static bool
20508 process_netbsd_elf_note (Elf_Internal_Note * pnote)
20509 {
20510 unsigned int version;
20511
20512 switch (pnote->type)
20513 {
20514 case NT_NETBSD_IDENT:
20515 if (pnote->descsz < 1)
20516 break;
20517 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
20518 if ((version / 10000) % 100)
20519 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u%s%c)\n", pnote->descsz,
20520 version, version / 100000000, (version / 1000000) % 100,
20521 (version / 10000) % 100 > 26 ? "Z" : "",
20522 'A' + (version / 10000) % 26);
20523 else
20524 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u.%u)\n", pnote->descsz,
20525 version, version / 100000000, (version / 1000000) % 100,
20526 (version / 100) % 100);
20527 return true;
20528
20529 case NT_NETBSD_MARCH:
20530 printf (" NetBSD\t\t0x%08lx\tMARCH <%s>\n", pnote->descsz,
20531 pnote->descdata);
20532 return true;
20533
20534 case NT_NETBSD_PAX:
20535 if (pnote->descsz < 1)
20536 break;
20537 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
20538 printf (" NetBSD\t\t0x%08lx\tPaX <%s%s%s%s%s%s>\n", pnote->descsz,
20539 ((version & NT_NETBSD_PAX_MPROTECT) ? "+mprotect" : ""),
20540 ((version & NT_NETBSD_PAX_NOMPROTECT) ? "-mprotect" : ""),
20541 ((version & NT_NETBSD_PAX_GUARD) ? "+guard" : ""),
20542 ((version & NT_NETBSD_PAX_NOGUARD) ? "-guard" : ""),
20543 ((version & NT_NETBSD_PAX_ASLR) ? "+ASLR" : ""),
20544 ((version & NT_NETBSD_PAX_NOASLR) ? "-ASLR" : ""));
20545 return true;
20546 }
20547
20548 printf (" NetBSD\t0x%08lx\tUnknown note type: (0x%08lx)\n",
20549 pnote->descsz, pnote->type);
20550 return false;
20551 }
20552
20553 static const char *
20554 get_freebsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
20555 {
20556 switch (e_type)
20557 {
20558 case NT_FREEBSD_THRMISC:
20559 return _("NT_THRMISC (thrmisc structure)");
20560 case NT_FREEBSD_PROCSTAT_PROC:
20561 return _("NT_PROCSTAT_PROC (proc data)");
20562 case NT_FREEBSD_PROCSTAT_FILES:
20563 return _("NT_PROCSTAT_FILES (files data)");
20564 case NT_FREEBSD_PROCSTAT_VMMAP:
20565 return _("NT_PROCSTAT_VMMAP (vmmap data)");
20566 case NT_FREEBSD_PROCSTAT_GROUPS:
20567 return _("NT_PROCSTAT_GROUPS (groups data)");
20568 case NT_FREEBSD_PROCSTAT_UMASK:
20569 return _("NT_PROCSTAT_UMASK (umask data)");
20570 case NT_FREEBSD_PROCSTAT_RLIMIT:
20571 return _("NT_PROCSTAT_RLIMIT (rlimit data)");
20572 case NT_FREEBSD_PROCSTAT_OSREL:
20573 return _("NT_PROCSTAT_OSREL (osreldate data)");
20574 case NT_FREEBSD_PROCSTAT_PSSTRINGS:
20575 return _("NT_PROCSTAT_PSSTRINGS (ps_strings data)");
20576 case NT_FREEBSD_PROCSTAT_AUXV:
20577 return _("NT_PROCSTAT_AUXV (auxv data)");
20578 case NT_FREEBSD_PTLWPINFO:
20579 return _("NT_PTLWPINFO (ptrace_lwpinfo structure)");
20580 case NT_FREEBSD_X86_SEGBASES:
20581 return _("NT_X86_SEGBASES (x86 segment base registers)");
20582 }
20583 return get_note_type (filedata, e_type);
20584 }
20585
20586 static const char *
20587 get_netbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
20588 {
20589 static char buff[64];
20590
20591 switch (e_type)
20592 {
20593 case NT_NETBSDCORE_PROCINFO:
20594 /* NetBSD core "procinfo" structure. */
20595 return _("NetBSD procinfo structure");
20596
20597 case NT_NETBSDCORE_AUXV:
20598 return _("NetBSD ELF auxiliary vector data");
20599
20600 case NT_NETBSDCORE_LWPSTATUS:
20601 return _("PT_LWPSTATUS (ptrace_lwpstatus structure)");
20602
20603 default:
20604 /* As of Jan 2020 there are no other machine-independent notes
20605 defined for NetBSD core files. If the note type is less
20606 than the start of the machine-dependent note types, we don't
20607 understand it. */
20608
20609 if (e_type < NT_NETBSDCORE_FIRSTMACH)
20610 {
20611 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20612 return buff;
20613 }
20614 break;
20615 }
20616
20617 switch (filedata->file_header.e_machine)
20618 {
20619 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
20620 and PT_GETFPREGS == mach+2. */
20621
20622 case EM_OLD_ALPHA:
20623 case EM_ALPHA:
20624 case EM_SPARC:
20625 case EM_SPARC32PLUS:
20626 case EM_SPARCV9:
20627 switch (e_type)
20628 {
20629 case NT_NETBSDCORE_FIRSTMACH + 0:
20630 return _("PT_GETREGS (reg structure)");
20631 case NT_NETBSDCORE_FIRSTMACH + 2:
20632 return _("PT_GETFPREGS (fpreg structure)");
20633 default:
20634 break;
20635 }
20636 break;
20637
20638 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
20639 There's also old PT___GETREGS40 == mach + 1 for old reg
20640 structure which lacks GBR. */
20641 case EM_SH:
20642 switch (e_type)
20643 {
20644 case NT_NETBSDCORE_FIRSTMACH + 1:
20645 return _("PT___GETREGS40 (old reg structure)");
20646 case NT_NETBSDCORE_FIRSTMACH + 3:
20647 return _("PT_GETREGS (reg structure)");
20648 case NT_NETBSDCORE_FIRSTMACH + 5:
20649 return _("PT_GETFPREGS (fpreg structure)");
20650 default:
20651 break;
20652 }
20653 break;
20654
20655 /* On all other arch's, PT_GETREGS == mach+1 and
20656 PT_GETFPREGS == mach+3. */
20657 default:
20658 switch (e_type)
20659 {
20660 case NT_NETBSDCORE_FIRSTMACH + 1:
20661 return _("PT_GETREGS (reg structure)");
20662 case NT_NETBSDCORE_FIRSTMACH + 3:
20663 return _("PT_GETFPREGS (fpreg structure)");
20664 default:
20665 break;
20666 }
20667 }
20668
20669 snprintf (buff, sizeof (buff), "PT_FIRSTMACH+%d",
20670 e_type - NT_NETBSDCORE_FIRSTMACH);
20671 return buff;
20672 }
20673
20674 static const char *
20675 get_openbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
20676 {
20677 switch (e_type)
20678 {
20679 case NT_OPENBSD_PROCINFO:
20680 return _("OpenBSD procinfo structure");
20681 case NT_OPENBSD_AUXV:
20682 return _("OpenBSD ELF auxiliary vector data");
20683 case NT_OPENBSD_REGS:
20684 return _("OpenBSD regular registers");
20685 case NT_OPENBSD_FPREGS:
20686 return _("OpenBSD floating point registers");
20687 case NT_OPENBSD_WCOOKIE:
20688 return _("OpenBSD window cookie");
20689 }
20690
20691 return get_note_type (filedata, e_type);
20692 }
20693
20694 static const char *
20695 get_stapsdt_note_type (unsigned e_type)
20696 {
20697 static char buff[64];
20698
20699 switch (e_type)
20700 {
20701 case NT_STAPSDT:
20702 return _("NT_STAPSDT (SystemTap probe descriptors)");
20703
20704 default:
20705 break;
20706 }
20707
20708 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20709 return buff;
20710 }
20711
20712 static bool
20713 print_stapsdt_note (Elf_Internal_Note *pnote)
20714 {
20715 size_t len, maxlen;
20716 unsigned long addr_size = is_32bit_elf ? 4 : 8;
20717 char *data = pnote->descdata;
20718 char *data_end = pnote->descdata + pnote->descsz;
20719 uint64_t pc, base_addr, semaphore;
20720 char *provider, *probe, *arg_fmt;
20721
20722 if (pnote->descsz < (addr_size * 3))
20723 goto stapdt_note_too_small;
20724
20725 pc = byte_get ((unsigned char *) data, addr_size);
20726 data += addr_size;
20727
20728 base_addr = byte_get ((unsigned char *) data, addr_size);
20729 data += addr_size;
20730
20731 semaphore = byte_get ((unsigned char *) data, addr_size);
20732 data += addr_size;
20733
20734 if (data >= data_end)
20735 goto stapdt_note_too_small;
20736 maxlen = data_end - data;
20737 len = strnlen (data, maxlen);
20738 if (len < maxlen)
20739 {
20740 provider = data;
20741 data += len + 1;
20742 }
20743 else
20744 goto stapdt_note_too_small;
20745
20746 if (data >= data_end)
20747 goto stapdt_note_too_small;
20748 maxlen = data_end - data;
20749 len = strnlen (data, maxlen);
20750 if (len < maxlen)
20751 {
20752 probe = data;
20753 data += len + 1;
20754 }
20755 else
20756 goto stapdt_note_too_small;
20757
20758 if (data >= data_end)
20759 goto stapdt_note_too_small;
20760 maxlen = data_end - data;
20761 len = strnlen (data, maxlen);
20762 if (len < maxlen)
20763 {
20764 arg_fmt = data;
20765 data += len + 1;
20766 }
20767 else
20768 goto stapdt_note_too_small;
20769
20770 printf (_(" Provider: %s\n"), provider);
20771 printf (_(" Name: %s\n"), probe);
20772 printf (_(" Location: "));
20773 print_vma (pc, FULL_HEX);
20774 printf (_(", Base: "));
20775 print_vma (base_addr, FULL_HEX);
20776 printf (_(", Semaphore: "));
20777 print_vma (semaphore, FULL_HEX);
20778 printf ("\n");
20779 printf (_(" Arguments: %s\n"), arg_fmt);
20780
20781 return data == data_end;
20782
20783 stapdt_note_too_small:
20784 printf (_(" <corrupt - note is too small>\n"));
20785 error (_("corrupt stapdt note - the data size is too small\n"));
20786 return false;
20787 }
20788
20789 static bool
20790 print_fdo_note (Elf_Internal_Note * pnote)
20791 {
20792 if (pnote->descsz > 0 && pnote->type == FDO_PACKAGING_METADATA)
20793 {
20794 printf (_(" Packaging Metadata: %.*s\n"), (int) pnote->descsz, pnote->descdata);
20795 return true;
20796 }
20797 return false;
20798 }
20799
20800 static const char *
20801 get_ia64_vms_note_type (unsigned e_type)
20802 {
20803 static char buff[64];
20804
20805 switch (e_type)
20806 {
20807 case NT_VMS_MHD:
20808 return _("NT_VMS_MHD (module header)");
20809 case NT_VMS_LNM:
20810 return _("NT_VMS_LNM (language name)");
20811 case NT_VMS_SRC:
20812 return _("NT_VMS_SRC (source files)");
20813 case NT_VMS_TITLE:
20814 return "NT_VMS_TITLE";
20815 case NT_VMS_EIDC:
20816 return _("NT_VMS_EIDC (consistency check)");
20817 case NT_VMS_FPMODE:
20818 return _("NT_VMS_FPMODE (FP mode)");
20819 case NT_VMS_LINKTIME:
20820 return "NT_VMS_LINKTIME";
20821 case NT_VMS_IMGNAM:
20822 return _("NT_VMS_IMGNAM (image name)");
20823 case NT_VMS_IMGID:
20824 return _("NT_VMS_IMGID (image id)");
20825 case NT_VMS_LINKID:
20826 return _("NT_VMS_LINKID (link id)");
20827 case NT_VMS_IMGBID:
20828 return _("NT_VMS_IMGBID (build id)");
20829 case NT_VMS_GSTNAM:
20830 return _("NT_VMS_GSTNAM (sym table name)");
20831 case NT_VMS_ORIG_DYN:
20832 return "NT_VMS_ORIG_DYN";
20833 case NT_VMS_PATCHTIME:
20834 return "NT_VMS_PATCHTIME";
20835 default:
20836 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20837 return buff;
20838 }
20839 }
20840
20841 static bool
20842 print_ia64_vms_note (Elf_Internal_Note * pnote)
20843 {
20844 int maxlen = pnote->descsz;
20845
20846 if (maxlen < 2 || (unsigned long) maxlen != pnote->descsz)
20847 goto desc_size_fail;
20848
20849 switch (pnote->type)
20850 {
20851 case NT_VMS_MHD:
20852 if (maxlen <= 36)
20853 goto desc_size_fail;
20854
20855 int l = (int) strnlen (pnote->descdata + 34, maxlen - 34);
20856
20857 printf (_(" Creation date : %.17s\n"), pnote->descdata);
20858 printf (_(" Last patch date: %.17s\n"), pnote->descdata + 17);
20859 if (l + 34 < maxlen)
20860 {
20861 printf (_(" Module name : %s\n"), pnote->descdata + 34);
20862 if (l + 35 < maxlen)
20863 printf (_(" Module version : %s\n"), pnote->descdata + 34 + l + 1);
20864 else
20865 printf (_(" Module version : <missing>\n"));
20866 }
20867 else
20868 {
20869 printf (_(" Module name : <missing>\n"));
20870 printf (_(" Module version : <missing>\n"));
20871 }
20872 break;
20873
20874 case NT_VMS_LNM:
20875 printf (_(" Language: %.*s\n"), maxlen, pnote->descdata);
20876 break;
20877
20878 case NT_VMS_FPMODE:
20879 printf (_(" Floating Point mode: "));
20880 if (maxlen < 8)
20881 goto desc_size_fail;
20882 /* FIXME: Generate an error if descsz > 8 ? */
20883
20884 printf ("0x%016" PRIx64 "\n",
20885 byte_get ((unsigned char *) pnote->descdata, 8));
20886 break;
20887
20888 case NT_VMS_LINKTIME:
20889 printf (_(" Link time: "));
20890 if (maxlen < 8)
20891 goto desc_size_fail;
20892 /* FIXME: Generate an error if descsz > 8 ? */
20893
20894 print_vms_time (byte_get ((unsigned char *) pnote->descdata, 8));
20895 printf ("\n");
20896 break;
20897
20898 case NT_VMS_PATCHTIME:
20899 printf (_(" Patch time: "));
20900 if (maxlen < 8)
20901 goto desc_size_fail;
20902 /* FIXME: Generate an error if descsz > 8 ? */
20903
20904 print_vms_time (byte_get ((unsigned char *) pnote->descdata, 8));
20905 printf ("\n");
20906 break;
20907
20908 case NT_VMS_ORIG_DYN:
20909 if (maxlen < 34)
20910 goto desc_size_fail;
20911
20912 printf (_(" Major id: %u, minor id: %u\n"),
20913 (unsigned) byte_get ((unsigned char *) pnote->descdata, 4),
20914 (unsigned) byte_get ((unsigned char *) pnote->descdata + 4, 4));
20915 printf (_(" Last modified : "));
20916 print_vms_time (byte_get ((unsigned char *) pnote->descdata + 8, 8));
20917 printf (_("\n Link flags : "));
20918 printf ("0x%016" PRIx64 "\n",
20919 byte_get ((unsigned char *) pnote->descdata + 16, 8));
20920 printf (_(" Header flags: 0x%08x\n"),
20921 (unsigned) byte_get ((unsigned char *) pnote->descdata + 24, 4));
20922 printf (_(" Image id : %.*s\n"), maxlen - 32, pnote->descdata + 32);
20923 break;
20924
20925 case NT_VMS_IMGNAM:
20926 printf (_(" Image name: %.*s\n"), maxlen, pnote->descdata);
20927 break;
20928
20929 case NT_VMS_GSTNAM:
20930 printf (_(" Global symbol table name: %.*s\n"), maxlen, pnote->descdata);
20931 break;
20932
20933 case NT_VMS_IMGID:
20934 printf (_(" Image id: %.*s\n"), maxlen, pnote->descdata);
20935 break;
20936
20937 case NT_VMS_LINKID:
20938 printf (_(" Linker id: %.*s\n"), maxlen, pnote->descdata);
20939 break;
20940
20941 default:
20942 return false;
20943 }
20944
20945 return true;
20946
20947 desc_size_fail:
20948 printf (_(" <corrupt - data size is too small>\n"));
20949 error (_("corrupt IA64 note: data size is too small\n"));
20950 return false;
20951 }
20952
20953 struct build_attr_cache {
20954 Filedata *filedata;
20955 char *strtab;
20956 unsigned long strtablen;
20957 Elf_Internal_Sym *symtab;
20958 unsigned long nsyms;
20959 } ba_cache;
20960
20961 /* Find the symbol associated with a build attribute that is attached
20962 to address OFFSET. If PNAME is non-NULL then store the name of
20963 the symbol (if found) in the provided pointer, Returns NULL if a
20964 symbol could not be found. */
20965
20966 static Elf_Internal_Sym *
20967 get_symbol_for_build_attribute (Filedata *filedata,
20968 unsigned long offset,
20969 bool is_open_attr,
20970 const char **pname)
20971 {
20972 Elf_Internal_Sym *saved_sym = NULL;
20973 Elf_Internal_Sym *sym;
20974
20975 if (filedata->section_headers != NULL
20976 && (ba_cache.filedata == NULL || filedata != ba_cache.filedata))
20977 {
20978 Elf_Internal_Shdr * symsec;
20979
20980 free (ba_cache.strtab);
20981 ba_cache.strtab = NULL;
20982 free (ba_cache.symtab);
20983 ba_cache.symtab = NULL;
20984
20985 /* Load the symbol and string sections. */
20986 for (symsec = filedata->section_headers;
20987 symsec < filedata->section_headers + filedata->file_header.e_shnum;
20988 symsec ++)
20989 {
20990 if (symsec->sh_type == SHT_SYMTAB
20991 && get_symtab (filedata, symsec,
20992 &ba_cache.symtab, &ba_cache.nsyms,
20993 &ba_cache.strtab, &ba_cache.strtablen))
20994 break;
20995 }
20996 ba_cache.filedata = filedata;
20997 }
20998
20999 if (ba_cache.symtab == NULL)
21000 return NULL;
21001
21002 /* Find a symbol whose value matches offset. */
21003 for (sym = ba_cache.symtab; sym < ba_cache.symtab + ba_cache.nsyms; sym ++)
21004 if (sym->st_value == offset)
21005 {
21006 if (sym->st_name >= ba_cache.strtablen)
21007 /* Huh ? This should not happen. */
21008 continue;
21009
21010 if (ba_cache.strtab[sym->st_name] == 0)
21011 continue;
21012
21013 /* The AArch64, ARM and RISC-V architectures define mapping symbols
21014 (eg $d, $x, $t) which we want to ignore. */
21015 if (ba_cache.strtab[sym->st_name] == '$'
21016 && ba_cache.strtab[sym->st_name + 1] != 0
21017 && ba_cache.strtab[sym->st_name + 2] == 0)
21018 continue;
21019
21020 if (is_open_attr)
21021 {
21022 /* For OPEN attributes we prefer GLOBAL over LOCAL symbols
21023 and FILE or OBJECT symbols over NOTYPE symbols. We skip
21024 FUNC symbols entirely. */
21025 switch (ELF_ST_TYPE (sym->st_info))
21026 {
21027 case STT_OBJECT:
21028 case STT_FILE:
21029 saved_sym = sym;
21030 if (sym->st_size)
21031 {
21032 /* If the symbol has a size associated
21033 with it then we can stop searching. */
21034 sym = ba_cache.symtab + ba_cache.nsyms;
21035 }
21036 continue;
21037
21038 case STT_FUNC:
21039 /* Ignore function symbols. */
21040 continue;
21041
21042 default:
21043 break;
21044 }
21045
21046 switch (ELF_ST_BIND (sym->st_info))
21047 {
21048 case STB_GLOBAL:
21049 if (saved_sym == NULL
21050 || ELF_ST_TYPE (saved_sym->st_info) != STT_OBJECT)
21051 saved_sym = sym;
21052 break;
21053
21054 case STB_LOCAL:
21055 if (saved_sym == NULL)
21056 saved_sym = sym;
21057 break;
21058
21059 default:
21060 break;
21061 }
21062 }
21063 else
21064 {
21065 if (ELF_ST_TYPE (sym->st_info) != STT_FUNC)
21066 continue;
21067
21068 saved_sym = sym;
21069 break;
21070 }
21071 }
21072
21073 if (saved_sym && pname)
21074 * pname = ba_cache.strtab + saved_sym->st_name;
21075
21076 return saved_sym;
21077 }
21078
21079 /* Returns true iff addr1 and addr2 are in the same section. */
21080
21081 static bool
21082 same_section (Filedata * filedata, unsigned long addr1, unsigned long addr2)
21083 {
21084 Elf_Internal_Shdr * a1;
21085 Elf_Internal_Shdr * a2;
21086
21087 a1 = find_section_by_address (filedata, addr1);
21088 a2 = find_section_by_address (filedata, addr2);
21089
21090 return a1 == a2 && a1 != NULL;
21091 }
21092
21093 static bool
21094 print_gnu_build_attribute_description (Elf_Internal_Note * pnote,
21095 Filedata * filedata)
21096 {
21097 static unsigned long global_offset = 0;
21098 static unsigned long global_end = 0;
21099 static unsigned long func_offset = 0;
21100 static unsigned long func_end = 0;
21101
21102 Elf_Internal_Sym *sym;
21103 const char *name;
21104 unsigned long start;
21105 unsigned long end;
21106 bool is_open_attr = pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN;
21107
21108 switch (pnote->descsz)
21109 {
21110 case 0:
21111 /* A zero-length description means that the range of
21112 the previous note of the same type should be used. */
21113 if (is_open_attr)
21114 {
21115 if (global_end > global_offset)
21116 printf (_(" Applies to region from %#lx to %#lx\n"),
21117 global_offset, global_end);
21118 else
21119 printf (_(" Applies to region from %#lx\n"), global_offset);
21120 }
21121 else
21122 {
21123 if (func_end > func_offset)
21124 printf (_(" Applies to region from %#lx to %#lx\n"), func_offset, func_end);
21125 else
21126 printf (_(" Applies to region from %#lx\n"), func_offset);
21127 }
21128 return true;
21129
21130 case 4:
21131 start = byte_get ((unsigned char *) pnote->descdata, 4);
21132 end = 0;
21133 break;
21134
21135 case 8:
21136 start = byte_get ((unsigned char *) pnote->descdata, 4);
21137 end = byte_get ((unsigned char *) pnote->descdata + 4, 4);
21138 break;
21139
21140 case 16:
21141 start = byte_get ((unsigned char *) pnote->descdata, 8);
21142 end = byte_get ((unsigned char *) pnote->descdata + 8, 8);
21143 break;
21144
21145 default:
21146 error (_(" <invalid description size: %lx>\n"), pnote->descsz);
21147 printf (_(" <invalid descsz>"));
21148 return false;
21149 }
21150
21151 name = NULL;
21152 sym = get_symbol_for_build_attribute (filedata, start, is_open_attr, & name);
21153 /* As of version 5 of the annobin plugin, filename symbols are biased by 2
21154 in order to avoid them being confused with the start address of the
21155 first function in the file... */
21156 if (sym == NULL && is_open_attr)
21157 sym = get_symbol_for_build_attribute (filedata, start + 2, is_open_attr,
21158 & name);
21159
21160 if (end == 0 && sym != NULL && sym->st_size > 0)
21161 end = start + sym->st_size;
21162
21163 if (is_open_attr)
21164 {
21165 /* FIXME: Need to properly allow for section alignment.
21166 16 is just the alignment used on x86_64. */
21167 if (global_end > 0
21168 && start > BFD_ALIGN (global_end, 16)
21169 /* Build notes are not guaranteed to be organised in order of
21170 increasing address, but we should find the all of the notes
21171 for one section in the same place. */
21172 && same_section (filedata, start, global_end))
21173 warn (_("Gap in build notes detected from %#lx to %#lx\n"),
21174 global_end + 1, start - 1);
21175
21176 printf (_(" Applies to region from %#lx"), start);
21177 global_offset = start;
21178
21179 if (end)
21180 {
21181 printf (_(" to %#lx"), end);
21182 global_end = end;
21183 }
21184 }
21185 else
21186 {
21187 printf (_(" Applies to region from %#lx"), start);
21188 func_offset = start;
21189
21190 if (end)
21191 {
21192 printf (_(" to %#lx"), end);
21193 func_end = end;
21194 }
21195 }
21196
21197 if (sym && name)
21198 printf (_(" (%s)"), name);
21199
21200 printf ("\n");
21201 return true;
21202 }
21203
21204 static bool
21205 print_gnu_build_attribute_name (Elf_Internal_Note * pnote)
21206 {
21207 static const char string_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_STRING, 0 };
21208 static const char number_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC, 0 };
21209 static const char bool_expected [3] = { GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE, GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE, 0 };
21210 char name_type;
21211 char name_attribute;
21212 const char * expected_types;
21213 const char * name = pnote->namedata;
21214 const char * text;
21215 signed int left;
21216
21217 if (name == NULL || pnote->namesz < 2)
21218 {
21219 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
21220 print_symbol (-20, _(" <corrupt name>"));
21221 return false;
21222 }
21223
21224 if (do_wide)
21225 left = 28;
21226 else
21227 left = 20;
21228
21229 /* Version 2 of the spec adds a "GA" prefix to the name field. */
21230 if (name[0] == 'G' && name[1] == 'A')
21231 {
21232 if (pnote->namesz < 4)
21233 {
21234 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
21235 print_symbol (-20, _(" <corrupt name>"));
21236 return false;
21237 }
21238
21239 printf ("GA");
21240 name += 2;
21241 left -= 2;
21242 }
21243
21244 switch ((name_type = * name))
21245 {
21246 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
21247 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
21248 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
21249 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
21250 printf ("%c", * name);
21251 left --;
21252 break;
21253 default:
21254 error (_("unrecognised attribute type in name field: %d\n"), name_type);
21255 print_symbol (-20, _("<unknown name type>"));
21256 return false;
21257 }
21258
21259 ++ name;
21260 text = NULL;
21261
21262 switch ((name_attribute = * name))
21263 {
21264 case GNU_BUILD_ATTRIBUTE_VERSION:
21265 text = _("<version>");
21266 expected_types = string_expected;
21267 ++ name;
21268 break;
21269 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
21270 text = _("<stack prot>");
21271 expected_types = "!+*";
21272 ++ name;
21273 break;
21274 case GNU_BUILD_ATTRIBUTE_RELRO:
21275 text = _("<relro>");
21276 expected_types = bool_expected;
21277 ++ name;
21278 break;
21279 case GNU_BUILD_ATTRIBUTE_STACK_SIZE:
21280 text = _("<stack size>");
21281 expected_types = number_expected;
21282 ++ name;
21283 break;
21284 case GNU_BUILD_ATTRIBUTE_TOOL:
21285 text = _("<tool>");
21286 expected_types = string_expected;
21287 ++ name;
21288 break;
21289 case GNU_BUILD_ATTRIBUTE_ABI:
21290 text = _("<ABI>");
21291 expected_types = "$*";
21292 ++ name;
21293 break;
21294 case GNU_BUILD_ATTRIBUTE_PIC:
21295 text = _("<PIC>");
21296 expected_types = number_expected;
21297 ++ name;
21298 break;
21299 case GNU_BUILD_ATTRIBUTE_SHORT_ENUM:
21300 text = _("<short enum>");
21301 expected_types = bool_expected;
21302 ++ name;
21303 break;
21304 default:
21305 if (ISPRINT (* name))
21306 {
21307 int len = strnlen (name, pnote->namesz - (name - pnote->namedata)) + 1;
21308
21309 if (len > left && ! do_wide)
21310 len = left;
21311 printf ("%.*s:", len, name);
21312 left -= len;
21313 name += len;
21314 }
21315 else
21316 {
21317 static char tmpbuf [128];
21318
21319 error (_("unrecognised byte in name field: %d\n"), * name);
21320 sprintf (tmpbuf, _("<unknown:_%d>"), * name);
21321 text = tmpbuf;
21322 name ++;
21323 }
21324 expected_types = "*$!+";
21325 break;
21326 }
21327
21328 if (text)
21329 left -= printf ("%s", text);
21330
21331 if (strchr (expected_types, name_type) == NULL)
21332 warn (_("attribute does not have an expected type (%c)\n"), name_type);
21333
21334 if ((unsigned long)(name - pnote->namedata) > pnote->namesz)
21335 {
21336 error (_("corrupt name field: namesz: %lu but parsing gets to %ld\n"),
21337 (unsigned long) pnote->namesz,
21338 (long) (name - pnote->namedata));
21339 return false;
21340 }
21341
21342 if (left < 1 && ! do_wide)
21343 return true;
21344
21345 switch (name_type)
21346 {
21347 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
21348 {
21349 unsigned int bytes;
21350 unsigned long long val = 0;
21351 unsigned int shift = 0;
21352 char * decoded = NULL;
21353
21354 bytes = pnote->namesz - (name - pnote->namedata);
21355 if (bytes > 0)
21356 /* The -1 is because the name field is always 0 terminated, and we
21357 want to be able to ensure that the shift in the while loop below
21358 will not overflow. */
21359 -- bytes;
21360
21361 if (bytes > sizeof (val))
21362 {
21363 error (_("corrupt numeric name field: too many bytes in the value: %x\n"),
21364 bytes);
21365 bytes = sizeof (val);
21366 }
21367 /* We do not bother to warn if bytes == 0 as this can
21368 happen with some early versions of the gcc plugin. */
21369
21370 while (bytes --)
21371 {
21372 unsigned long long byte = *name++ & 0xff;
21373
21374 val |= byte << shift;
21375 shift += 8;
21376 }
21377
21378 switch (name_attribute)
21379 {
21380 case GNU_BUILD_ATTRIBUTE_PIC:
21381 switch (val)
21382 {
21383 case 0: decoded = "static"; break;
21384 case 1: decoded = "pic"; break;
21385 case 2: decoded = "PIC"; break;
21386 case 3: decoded = "pie"; break;
21387 case 4: decoded = "PIE"; break;
21388 default: break;
21389 }
21390 break;
21391 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
21392 switch (val)
21393 {
21394 /* Based upon the SPCT_FLAG_xxx enum values in gcc/cfgexpand.c. */
21395 case 0: decoded = "off"; break;
21396 case 1: decoded = "on"; break;
21397 case 2: decoded = "all"; break;
21398 case 3: decoded = "strong"; break;
21399 case 4: decoded = "explicit"; break;
21400 default: break;
21401 }
21402 break;
21403 default:
21404 break;
21405 }
21406
21407 if (decoded != NULL)
21408 {
21409 print_symbol (-left, decoded);
21410 left = 0;
21411 }
21412 else if (val == 0)
21413 {
21414 printf ("0x0");
21415 left -= 3;
21416 }
21417 else
21418 {
21419 if (do_wide)
21420 left -= printf ("0x%llx", val);
21421 else
21422 left -= printf ("0x%-.*llx", left, val);
21423 }
21424 }
21425 break;
21426 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
21427 left -= print_symbol (- left, name);
21428 break;
21429 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
21430 left -= print_symbol (- left, "true");
21431 break;
21432 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
21433 left -= print_symbol (- left, "false");
21434 break;
21435 }
21436
21437 if (do_wide && left > 0)
21438 printf ("%-*s", left, " ");
21439
21440 return true;
21441 }
21442
21443 /* Print the contents of PNOTE as hex. */
21444
21445 static void
21446 print_note_contents_hex (Elf_Internal_Note *pnote)
21447 {
21448 if (pnote->descsz)
21449 {
21450 unsigned long i;
21451
21452 printf (_(" description data: "));
21453 for (i = 0; i < pnote->descsz; i++)
21454 printf ("%02x ", pnote->descdata[i] & 0xff);
21455 if (!do_wide)
21456 printf ("\n");
21457 }
21458
21459 if (do_wide)
21460 printf ("\n");
21461 }
21462
21463 #if defined HAVE_MSGPACK
21464
21465 static void
21466 print_indents (int n)
21467 {
21468 printf (" ");
21469
21470 for (int i = 0; i < n; i++)
21471 printf (" ");
21472 }
21473
21474 /* Print OBJ in human-readable form. */
21475
21476 static void
21477 dump_msgpack_obj (const msgpack_object *obj, int indent)
21478 {
21479 switch (obj->type)
21480 {
21481 case MSGPACK_OBJECT_NIL:
21482 printf ("(nil)");
21483 break;
21484
21485 case MSGPACK_OBJECT_BOOLEAN:
21486 printf ("%s", obj->via.boolean ? "true" : "false");
21487 break;
21488
21489 case MSGPACK_OBJECT_POSITIVE_INTEGER:
21490 printf ("%" PRIu64, obj->via.u64);
21491 break;
21492
21493 case MSGPACK_OBJECT_NEGATIVE_INTEGER:
21494 printf ("%" PRIi64, obj->via.i64);
21495 break;
21496
21497 case MSGPACK_OBJECT_FLOAT32:
21498 case MSGPACK_OBJECT_FLOAT64:
21499 printf ("%f", obj->via.f64);
21500 break;
21501
21502 case MSGPACK_OBJECT_STR:
21503 printf ("\"%.*s\"", obj->via.str.size, obj->via.str.ptr);
21504 break;
21505
21506 case MSGPACK_OBJECT_ARRAY:
21507 {
21508 const msgpack_object_array *array = &obj->via.array;
21509
21510 printf ("[\n");
21511 ++indent;
21512
21513 for (uint32_t i = 0; i < array->size; ++i)
21514 {
21515 const msgpack_object *item = &array->ptr[i];
21516
21517 print_indents (indent);
21518 dump_msgpack_obj (item, indent);
21519 printf (",\n");
21520 }
21521
21522 --indent;
21523 print_indents (indent);
21524 printf ("]");
21525 break;
21526 }
21527 break;
21528
21529 case MSGPACK_OBJECT_MAP:
21530 {
21531 const msgpack_object_map *map = &obj->via.map;
21532
21533 printf ("{\n");
21534 ++indent;
21535
21536 for (uint32_t i = 0; i < map->size; ++i)
21537 {
21538 const msgpack_object_kv *kv = &map->ptr[i];
21539 const msgpack_object *key = &kv->key;
21540 const msgpack_object *val = &kv->val;
21541
21542 print_indents (indent);
21543 dump_msgpack_obj (key, indent);
21544 printf (": ");
21545 dump_msgpack_obj (val, indent);
21546
21547 printf (",\n");
21548 }
21549
21550 --indent;
21551 print_indents (indent);
21552 printf ("}");
21553
21554 break;
21555 }
21556
21557 case MSGPACK_OBJECT_BIN:
21558 printf ("(bin)");
21559 break;
21560
21561 case MSGPACK_OBJECT_EXT:
21562 printf ("(ext)");
21563 break;
21564 }
21565 }
21566
21567 static void
21568 dump_msgpack (const msgpack_unpacked *msg)
21569 {
21570 print_indents (0);
21571 dump_msgpack_obj (&msg->data, 0);
21572 printf ("\n");
21573 }
21574
21575 #endif /* defined HAVE_MSGPACK */
21576
21577 static bool
21578 print_amdgpu_note (Elf_Internal_Note *pnote)
21579 {
21580 #if defined HAVE_MSGPACK
21581 /* If msgpack is available, decode and dump the note's content. */
21582 bool ret;
21583 msgpack_unpacked msg;
21584 msgpack_unpack_return msgpack_ret;
21585
21586 assert (pnote->type == NT_AMDGPU_METADATA);
21587
21588 msgpack_unpacked_init (&msg);
21589 msgpack_ret = msgpack_unpack_next (&msg, pnote->descdata, pnote->descsz,
21590 NULL);
21591
21592 switch (msgpack_ret)
21593 {
21594 case MSGPACK_UNPACK_SUCCESS:
21595 dump_msgpack (&msg);
21596 ret = true;
21597 break;
21598
21599 default:
21600 error (_("failed to unpack msgpack contents in NT_AMDGPU_METADATA note"));
21601 ret = false;
21602 break;
21603 }
21604
21605 msgpack_unpacked_destroy (&msg);
21606 return ret;
21607 #else
21608 /* msgpack is not available, dump contents as hex. */
21609 print_note_contents_hex (pnote);
21610 return true;
21611 #endif
21612 }
21613
21614 /* Note that by the ELF standard, the name field is already null byte
21615 terminated, and namesz includes the terminating null byte.
21616 I.E. the value of namesz for the name "FSF" is 4.
21617
21618 If the value of namesz is zero, there is no name present. */
21619
21620 static bool
21621 process_note (Elf_Internal_Note * pnote,
21622 Filedata * filedata)
21623 {
21624 const char * name = pnote->namesz ? pnote->namedata : "(NONE)";
21625 const char * nt;
21626
21627 if (pnote->namesz == 0)
21628 /* If there is no note name, then use the default set of
21629 note type strings. */
21630 nt = get_note_type (filedata, pnote->type);
21631
21632 else if (startswith (pnote->namedata, "GNU"))
21633 /* GNU-specific object file notes. */
21634 nt = get_gnu_elf_note_type (pnote->type);
21635
21636 else if (startswith (pnote->namedata, "AMDGPU"))
21637 /* AMDGPU-specific object file notes. */
21638 nt = get_amdgpu_elf_note_type (pnote->type);
21639
21640 else if (startswith (pnote->namedata, "FreeBSD"))
21641 /* FreeBSD-specific core file notes. */
21642 nt = get_freebsd_elfcore_note_type (filedata, pnote->type);
21643
21644 else if (startswith (pnote->namedata, "NetBSD-CORE"))
21645 /* NetBSD-specific core file notes. */
21646 nt = get_netbsd_elfcore_note_type (filedata, pnote->type);
21647
21648 else if (startswith (pnote->namedata, "NetBSD"))
21649 /* NetBSD-specific core file notes. */
21650 return process_netbsd_elf_note (pnote);
21651
21652 else if (startswith (pnote->namedata, "PaX"))
21653 /* NetBSD-specific core file notes. */
21654 return process_netbsd_elf_note (pnote);
21655
21656 else if (startswith (pnote->namedata, "OpenBSD"))
21657 /* OpenBSD-specific core file notes. */
21658 nt = get_openbsd_elfcore_note_type (filedata, pnote->type);
21659
21660 else if (startswith (pnote->namedata, "SPU/"))
21661 {
21662 /* SPU-specific core file notes. */
21663 nt = pnote->namedata + 4;
21664 name = "SPU";
21665 }
21666
21667 else if (startswith (pnote->namedata, "IPF/VMS"))
21668 /* VMS/ia64-specific file notes. */
21669 nt = get_ia64_vms_note_type (pnote->type);
21670
21671 else if (startswith (pnote->namedata, "stapsdt"))
21672 nt = get_stapsdt_note_type (pnote->type);
21673
21674 else
21675 /* Don't recognize this note name; just use the default set of
21676 note type strings. */
21677 nt = get_note_type (filedata, pnote->type);
21678
21679 printf (" ");
21680
21681 if (((startswith (pnote->namedata, "GA")
21682 && strchr ("*$!+", pnote->namedata[2]) != NULL)
21683 || strchr ("*$!+", pnote->namedata[0]) != NULL)
21684 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
21685 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
21686 print_gnu_build_attribute_name (pnote);
21687 else
21688 print_symbol (-20, name);
21689
21690 if (do_wide)
21691 printf (" 0x%08lx\t%s\t", pnote->descsz, nt);
21692 else
21693 printf (" 0x%08lx\t%s\n", pnote->descsz, nt);
21694
21695 if (startswith (pnote->namedata, "IPF/VMS"))
21696 return print_ia64_vms_note (pnote);
21697 else if (startswith (pnote->namedata, "GNU"))
21698 return print_gnu_note (filedata, pnote);
21699 else if (startswith (pnote->namedata, "stapsdt"))
21700 return print_stapsdt_note (pnote);
21701 else if (startswith (pnote->namedata, "CORE"))
21702 return print_core_note (pnote);
21703 else if (startswith (pnote->namedata, "FDO"))
21704 return print_fdo_note (pnote);
21705 else if (((startswith (pnote->namedata, "GA")
21706 && strchr ("*$!+", pnote->namedata[2]) != NULL)
21707 || strchr ("*$!+", pnote->namedata[0]) != NULL)
21708 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
21709 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
21710 return print_gnu_build_attribute_description (pnote, filedata);
21711 else if (startswith (pnote->namedata, "AMDGPU")
21712 && pnote->type == NT_AMDGPU_METADATA)
21713 return print_amdgpu_note (pnote);
21714
21715 print_note_contents_hex (pnote);
21716 return true;
21717 }
21718
21719 static bool
21720 process_notes_at (Filedata * filedata,
21721 Elf_Internal_Shdr * section,
21722 uint64_t offset,
21723 uint64_t length,
21724 uint64_t align)
21725 {
21726 Elf_External_Note *pnotes;
21727 Elf_External_Note *external;
21728 char *end;
21729 bool res = true;
21730
21731 if (length <= 0)
21732 return false;
21733
21734 if (section)
21735 {
21736 pnotes = (Elf_External_Note *) get_section_contents (section, filedata);
21737 if (pnotes)
21738 {
21739 if (! apply_relocations (filedata, section, (unsigned char *) pnotes, length, NULL, NULL))
21740 {
21741 free (pnotes);
21742 return false;
21743 }
21744 }
21745 }
21746 else
21747 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
21748 _("notes"));
21749
21750 if (pnotes == NULL)
21751 return false;
21752
21753 external = pnotes;
21754
21755 if (filedata->is_separate)
21756 printf (_("In linked file '%s': "), filedata->file_name);
21757 else
21758 printf ("\n");
21759 if (section)
21760 printf (_("Displaying notes found in: %s\n"), printable_section_name (filedata, section));
21761 else
21762 printf (_("Displaying notes found at file offset 0x%08lx with length 0x%08lx:\n"),
21763 (unsigned long) offset, (unsigned long) length);
21764
21765 /* NB: Some note sections may have alignment value of 0 or 1. gABI
21766 specifies that notes should be aligned to 4 bytes in 32-bit
21767 objects and to 8 bytes in 64-bit objects. As a Linux extension,
21768 we also support 4 byte alignment in 64-bit objects. If section
21769 alignment is less than 4, we treate alignment as 4 bytes. */
21770 if (align < 4)
21771 align = 4;
21772 else if (align != 4 && align != 8)
21773 {
21774 warn (_("Corrupt note: alignment %ld, expecting 4 or 8\n"),
21775 (long) align);
21776 free (pnotes);
21777 return false;
21778 }
21779
21780 printf (_(" %-20s %-10s\tDescription\n"), _("Owner"), _("Data size"));
21781
21782 end = (char *) pnotes + length;
21783 while ((char *) external < end)
21784 {
21785 Elf_Internal_Note inote;
21786 size_t min_notesz;
21787 char * next;
21788 char * temp = NULL;
21789 size_t data_remaining = end - (char *) external;
21790
21791 if (!is_ia64_vms (filedata))
21792 {
21793 /* PR binutils/15191
21794 Make sure that there is enough data to read. */
21795 min_notesz = offsetof (Elf_External_Note, name);
21796 if (data_remaining < min_notesz)
21797 {
21798 warn (ngettext ("Corrupt note: only %ld byte remains, "
21799 "not enough for a full note\n",
21800 "Corrupt note: only %ld bytes remain, "
21801 "not enough for a full note\n",
21802 data_remaining),
21803 (long) data_remaining);
21804 break;
21805 }
21806 data_remaining -= min_notesz;
21807
21808 inote.type = BYTE_GET (external->type);
21809 inote.namesz = BYTE_GET (external->namesz);
21810 inote.namedata = external->name;
21811 inote.descsz = BYTE_GET (external->descsz);
21812 inote.descdata = ((char *) external
21813 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
21814 inote.descpos = offset + (inote.descdata - (char *) pnotes);
21815 next = ((char *) external
21816 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
21817 }
21818 else
21819 {
21820 Elf64_External_VMS_Note *vms_external;
21821
21822 /* PR binutils/15191
21823 Make sure that there is enough data to read. */
21824 min_notesz = offsetof (Elf64_External_VMS_Note, name);
21825 if (data_remaining < min_notesz)
21826 {
21827 warn (ngettext ("Corrupt note: only %ld byte remains, "
21828 "not enough for a full note\n",
21829 "Corrupt note: only %ld bytes remain, "
21830 "not enough for a full note\n",
21831 data_remaining),
21832 (long) data_remaining);
21833 break;
21834 }
21835 data_remaining -= min_notesz;
21836
21837 vms_external = (Elf64_External_VMS_Note *) external;
21838 inote.type = BYTE_GET (vms_external->type);
21839 inote.namesz = BYTE_GET (vms_external->namesz);
21840 inote.namedata = vms_external->name;
21841 inote.descsz = BYTE_GET (vms_external->descsz);
21842 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
21843 inote.descpos = offset + (inote.descdata - (char *) pnotes);
21844 next = inote.descdata + align_power (inote.descsz, 3);
21845 }
21846
21847 /* PR 17531: file: 3443835e. */
21848 /* PR 17531: file: id:000000,sig:11,src:006986,op:havoc,rep:4. */
21849 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
21850 || (size_t) (inote.descdata - inote.namedata) > data_remaining
21851 || (size_t) (next - inote.descdata) < inote.descsz
21852 || ((size_t) (next - inote.descdata)
21853 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
21854 {
21855 warn (_("note with invalid namesz and/or descsz found at offset 0x%lx\n"),
21856 (unsigned long) ((char *) external - (char *) pnotes));
21857 warn (_(" type: 0x%lx, namesize: 0x%08lx, descsize: 0x%08lx, alignment: %u\n"),
21858 inote.type, inote.namesz, inote.descsz, (int) align);
21859 break;
21860 }
21861
21862 external = (Elf_External_Note *) next;
21863
21864 /* Verify that name is null terminated. It appears that at least
21865 one version of Linux (RedHat 6.0) generates corefiles that don't
21866 comply with the ELF spec by failing to include the null byte in
21867 namesz. */
21868 if (inote.namesz > 0 && inote.namedata[inote.namesz - 1] != '\0')
21869 {
21870 if ((size_t) (inote.descdata - inote.namedata) == inote.namesz)
21871 {
21872 temp = (char *) malloc (inote.namesz + 1);
21873 if (temp == NULL)
21874 {
21875 error (_("Out of memory allocating space for inote name\n"));
21876 res = false;
21877 break;
21878 }
21879
21880 memcpy (temp, inote.namedata, inote.namesz);
21881 inote.namedata = temp;
21882 }
21883 inote.namedata[inote.namesz] = 0;
21884 }
21885
21886 if (! process_note (& inote, filedata))
21887 res = false;
21888
21889 free (temp);
21890 temp = NULL;
21891 }
21892
21893 free (pnotes);
21894
21895 return res;
21896 }
21897
21898 static bool
21899 process_corefile_note_segments (Filedata * filedata)
21900 {
21901 Elf_Internal_Phdr *segment;
21902 unsigned int i;
21903 bool res = true;
21904
21905 if (! get_program_headers (filedata))
21906 return true;
21907
21908 for (i = 0, segment = filedata->program_headers;
21909 i < filedata->file_header.e_phnum;
21910 i++, segment++)
21911 {
21912 if (segment->p_type == PT_NOTE)
21913 if (! process_notes_at (filedata, NULL, segment->p_offset,
21914 segment->p_filesz, segment->p_align))
21915 res = false;
21916 }
21917
21918 return res;
21919 }
21920
21921 static bool
21922 process_v850_notes (Filedata * filedata, uint64_t offset, uint64_t length)
21923 {
21924 Elf_External_Note * pnotes;
21925 Elf_External_Note * external;
21926 char * end;
21927 bool res = true;
21928
21929 if (length <= 0)
21930 return false;
21931
21932 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
21933 _("v850 notes"));
21934 if (pnotes == NULL)
21935 return false;
21936
21937 external = pnotes;
21938 end = (char*) pnotes + length;
21939
21940 printf (_("\nDisplaying contents of Renesas V850 notes section at offset 0x%lx with length 0x%lx:\n"),
21941 (unsigned long) offset, (unsigned long) length);
21942
21943 while ((char *) external + sizeof (Elf_External_Note) < end)
21944 {
21945 Elf_External_Note * next;
21946 Elf_Internal_Note inote;
21947
21948 inote.type = BYTE_GET (external->type);
21949 inote.namesz = BYTE_GET (external->namesz);
21950 inote.namedata = external->name;
21951 inote.descsz = BYTE_GET (external->descsz);
21952 inote.descdata = inote.namedata + align_power (inote.namesz, 2);
21953 inote.descpos = offset + (inote.descdata - (char *) pnotes);
21954
21955 if (inote.descdata < (char *) pnotes || inote.descdata >= end)
21956 {
21957 warn (_("Corrupt note: name size is too big: %lx\n"), inote.namesz);
21958 inote.descdata = inote.namedata;
21959 inote.namesz = 0;
21960 }
21961
21962 next = (Elf_External_Note *) (inote.descdata + align_power (inote.descsz, 2));
21963
21964 if ( ((char *) next > end)
21965 || ((char *) next < (char *) pnotes))
21966 {
21967 warn (_("corrupt descsz found in note at offset 0x%lx\n"),
21968 (unsigned long) ((char *) external - (char *) pnotes));
21969 warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
21970 inote.type, inote.namesz, inote.descsz);
21971 break;
21972 }
21973
21974 external = next;
21975
21976 /* Prevent out-of-bounds indexing. */
21977 if ( inote.namedata + inote.namesz > end
21978 || inote.namedata + inote.namesz < inote.namedata)
21979 {
21980 warn (_("corrupt namesz found in note at offset 0x%lx\n"),
21981 (unsigned long) ((char *) external - (char *) pnotes));
21982 warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
21983 inote.type, inote.namesz, inote.descsz);
21984 break;
21985 }
21986
21987 printf (" %s: ", get_v850_elf_note_type (inote.type));
21988
21989 if (! print_v850_note (& inote))
21990 {
21991 res = false;
21992 printf ("<corrupt sizes: namesz: %lx, descsz: %lx>\n",
21993 inote.namesz, inote.descsz);
21994 }
21995 }
21996
21997 free (pnotes);
21998
21999 return res;
22000 }
22001
22002 static bool
22003 process_note_sections (Filedata * filedata)
22004 {
22005 Elf_Internal_Shdr *section;
22006 unsigned long i;
22007 unsigned int n = 0;
22008 bool res = true;
22009
22010 for (i = 0, section = filedata->section_headers;
22011 i < filedata->file_header.e_shnum && section != NULL;
22012 i++, section++)
22013 {
22014 if (section->sh_type == SHT_NOTE)
22015 {
22016 if (! process_notes_at (filedata, section, section->sh_offset,
22017 section->sh_size, section->sh_addralign))
22018 res = false;
22019 n++;
22020 }
22021
22022 if (( filedata->file_header.e_machine == EM_V800
22023 || filedata->file_header.e_machine == EM_V850
22024 || filedata->file_header.e_machine == EM_CYGNUS_V850)
22025 && section->sh_type == SHT_RENESAS_INFO)
22026 {
22027 if (! process_v850_notes (filedata, section->sh_offset,
22028 section->sh_size))
22029 res = false;
22030 n++;
22031 }
22032 }
22033
22034 if (n == 0)
22035 /* Try processing NOTE segments instead. */
22036 return process_corefile_note_segments (filedata);
22037
22038 return res;
22039 }
22040
22041 static bool
22042 process_notes (Filedata * filedata)
22043 {
22044 /* If we have not been asked to display the notes then do nothing. */
22045 if (! do_notes)
22046 return true;
22047
22048 if (filedata->file_header.e_type != ET_CORE)
22049 return process_note_sections (filedata);
22050
22051 /* No program headers means no NOTE segment. */
22052 if (filedata->file_header.e_phnum > 0)
22053 return process_corefile_note_segments (filedata);
22054
22055 if (filedata->is_separate)
22056 printf (_("No notes found in linked file '%s'.\n"),
22057 filedata->file_name);
22058 else
22059 printf (_("No notes found file.\n"));
22060
22061 return true;
22062 }
22063
22064 static unsigned char *
22065 display_public_gnu_attributes (unsigned char * start,
22066 const unsigned char * const end)
22067 {
22068 printf (_(" Unknown GNU attribute: %s\n"), start);
22069
22070 start += strnlen ((char *) start, end - start);
22071 display_raw_attribute (start, end);
22072
22073 return (unsigned char *) end;
22074 }
22075
22076 static unsigned char *
22077 display_generic_attribute (unsigned char * start,
22078 unsigned int tag,
22079 const unsigned char * const end)
22080 {
22081 if (tag == 0)
22082 return (unsigned char *) end;
22083
22084 return display_tag_value (tag, start, end);
22085 }
22086
22087 static bool
22088 process_arch_specific (Filedata * filedata)
22089 {
22090 if (! do_arch)
22091 return true;
22092
22093 switch (filedata->file_header.e_machine)
22094 {
22095 case EM_ARC:
22096 case EM_ARC_COMPACT:
22097 case EM_ARC_COMPACT2:
22098 return process_attributes (filedata, "ARC", SHT_ARC_ATTRIBUTES,
22099 display_arc_attribute,
22100 display_generic_attribute);
22101 case EM_ARM:
22102 return process_attributes (filedata, "aeabi", SHT_ARM_ATTRIBUTES,
22103 display_arm_attribute,
22104 display_generic_attribute);
22105
22106 case EM_MIPS:
22107 case EM_MIPS_RS3_LE:
22108 return process_mips_specific (filedata);
22109
22110 case EM_MSP430:
22111 return process_attributes (filedata, "mspabi", SHT_MSP430_ATTRIBUTES,
22112 display_msp430_attribute,
22113 display_msp430_gnu_attribute);
22114
22115 case EM_RISCV:
22116 return process_attributes (filedata, "riscv", SHT_RISCV_ATTRIBUTES,
22117 display_riscv_attribute,
22118 display_generic_attribute);
22119
22120 case EM_NDS32:
22121 return process_nds32_specific (filedata);
22122
22123 case EM_68K:
22124 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22125 display_m68k_gnu_attribute);
22126
22127 case EM_PPC:
22128 case EM_PPC64:
22129 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22130 display_power_gnu_attribute);
22131
22132 case EM_S390:
22133 case EM_S390_OLD:
22134 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22135 display_s390_gnu_attribute);
22136
22137 case EM_SPARC:
22138 case EM_SPARC32PLUS:
22139 case EM_SPARCV9:
22140 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22141 display_sparc_gnu_attribute);
22142
22143 case EM_TI_C6000:
22144 return process_attributes (filedata, "c6xabi", SHT_C6000_ATTRIBUTES,
22145 display_tic6x_attribute,
22146 display_generic_attribute);
22147
22148 case EM_CSKY:
22149 return process_attributes (filedata, "csky", SHT_CSKY_ATTRIBUTES,
22150 display_csky_attribute, NULL);
22151
22152 default:
22153 return process_attributes (filedata, "gnu", SHT_GNU_ATTRIBUTES,
22154 display_public_gnu_attributes,
22155 display_generic_attribute);
22156 }
22157 }
22158
22159 static bool
22160 get_file_header (Filedata * filedata)
22161 {
22162 /* Read in the identity array. */
22163 if (fread (filedata->file_header.e_ident, EI_NIDENT, 1, filedata->handle) != 1)
22164 return false;
22165
22166 /* Determine how to read the rest of the header. */
22167 switch (filedata->file_header.e_ident[EI_DATA])
22168 {
22169 default:
22170 case ELFDATANONE:
22171 case ELFDATA2LSB:
22172 byte_get = byte_get_little_endian;
22173 byte_put = byte_put_little_endian;
22174 break;
22175 case ELFDATA2MSB:
22176 byte_get = byte_get_big_endian;
22177 byte_put = byte_put_big_endian;
22178 break;
22179 }
22180
22181 /* For now we only support 32 bit and 64 bit ELF files. */
22182 is_32bit_elf = (filedata->file_header.e_ident[EI_CLASS] != ELFCLASS64);
22183
22184 /* Read in the rest of the header. */
22185 if (is_32bit_elf)
22186 {
22187 Elf32_External_Ehdr ehdr32;
22188
22189 if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, filedata->handle) != 1)
22190 return false;
22191
22192 filedata->file_header.e_type = BYTE_GET (ehdr32.e_type);
22193 filedata->file_header.e_machine = BYTE_GET (ehdr32.e_machine);
22194 filedata->file_header.e_version = BYTE_GET (ehdr32.e_version);
22195 filedata->file_header.e_entry = BYTE_GET (ehdr32.e_entry);
22196 filedata->file_header.e_phoff = BYTE_GET (ehdr32.e_phoff);
22197 filedata->file_header.e_shoff = BYTE_GET (ehdr32.e_shoff);
22198 filedata->file_header.e_flags = BYTE_GET (ehdr32.e_flags);
22199 filedata->file_header.e_ehsize = BYTE_GET (ehdr32.e_ehsize);
22200 filedata->file_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
22201 filedata->file_header.e_phnum = BYTE_GET (ehdr32.e_phnum);
22202 filedata->file_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
22203 filedata->file_header.e_shnum = BYTE_GET (ehdr32.e_shnum);
22204 filedata->file_header.e_shstrndx = BYTE_GET (ehdr32.e_shstrndx);
22205 }
22206 else
22207 {
22208 Elf64_External_Ehdr ehdr64;
22209
22210 if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, filedata->handle) != 1)
22211 return false;
22212
22213 filedata->file_header.e_type = BYTE_GET (ehdr64.e_type);
22214 filedata->file_header.e_machine = BYTE_GET (ehdr64.e_machine);
22215 filedata->file_header.e_version = BYTE_GET (ehdr64.e_version);
22216 filedata->file_header.e_entry = BYTE_GET (ehdr64.e_entry);
22217 filedata->file_header.e_phoff = BYTE_GET (ehdr64.e_phoff);
22218 filedata->file_header.e_shoff = BYTE_GET (ehdr64.e_shoff);
22219 filedata->file_header.e_flags = BYTE_GET (ehdr64.e_flags);
22220 filedata->file_header.e_ehsize = BYTE_GET (ehdr64.e_ehsize);
22221 filedata->file_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
22222 filedata->file_header.e_phnum = BYTE_GET (ehdr64.e_phnum);
22223 filedata->file_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
22224 filedata->file_header.e_shnum = BYTE_GET (ehdr64.e_shnum);
22225 filedata->file_header.e_shstrndx = BYTE_GET (ehdr64.e_shstrndx);
22226 }
22227
22228 return true;
22229 }
22230
22231 static void
22232 free_filedata (Filedata *filedata)
22233 {
22234 free (filedata->program_interpreter);
22235 free (filedata->program_headers);
22236 free (filedata->section_headers);
22237 free (filedata->string_table);
22238 free (filedata->dump.dump_sects);
22239 free (filedata->dynamic_strings);
22240 free (filedata->dynamic_symbols);
22241 free (filedata->dynamic_syminfo);
22242 free (filedata->dynamic_section);
22243
22244 while (filedata->symtab_shndx_list != NULL)
22245 {
22246 elf_section_list *next = filedata->symtab_shndx_list->next;
22247 free (filedata->symtab_shndx_list);
22248 filedata->symtab_shndx_list = next;
22249 }
22250
22251 free (filedata->section_headers_groups);
22252
22253 if (filedata->section_groups)
22254 {
22255 size_t i;
22256 struct group_list * g;
22257 struct group_list * next;
22258
22259 for (i = 0; i < filedata->group_count; i++)
22260 {
22261 for (g = filedata->section_groups [i].root; g != NULL; g = next)
22262 {
22263 next = g->next;
22264 free (g);
22265 }
22266 }
22267
22268 free (filedata->section_groups);
22269 }
22270 memset (&filedata->section_headers, 0,
22271 sizeof (Filedata) - offsetof (Filedata, section_headers));
22272 }
22273
22274 static void
22275 close_file (Filedata * filedata)
22276 {
22277 if (filedata)
22278 {
22279 if (filedata->handle)
22280 fclose (filedata->handle);
22281 free (filedata);
22282 }
22283 }
22284
22285 void
22286 close_debug_file (void * data)
22287 {
22288 free_filedata ((Filedata *) data);
22289 close_file ((Filedata *) data);
22290 }
22291
22292 static Filedata *
22293 open_file (const char * pathname, bool is_separate)
22294 {
22295 struct stat statbuf;
22296 Filedata * filedata = NULL;
22297
22298 if (stat (pathname, & statbuf) < 0
22299 || ! S_ISREG (statbuf.st_mode))
22300 goto fail;
22301
22302 filedata = calloc (1, sizeof * filedata);
22303 if (filedata == NULL)
22304 goto fail;
22305
22306 filedata->handle = fopen (pathname, "rb");
22307 if (filedata->handle == NULL)
22308 goto fail;
22309
22310 filedata->file_size = statbuf.st_size;
22311 filedata->file_name = pathname;
22312 filedata->is_separate = is_separate;
22313
22314 if (! get_file_header (filedata))
22315 goto fail;
22316
22317 if (!get_section_headers (filedata, false))
22318 goto fail;
22319
22320 return filedata;
22321
22322 fail:
22323 if (filedata)
22324 {
22325 if (filedata->handle)
22326 fclose (filedata->handle);
22327 free (filedata);
22328 }
22329 return NULL;
22330 }
22331
22332 void *
22333 open_debug_file (const char * pathname)
22334 {
22335 return open_file (pathname, true);
22336 }
22337
22338 static void
22339 initialise_dump_sects (Filedata * filedata)
22340 {
22341 /* Initialise the dump_sects array from the cmdline_dump_sects array.
22342 Note we do this even if cmdline_dump_sects is empty because we
22343 must make sure that the dump_sets array is zeroed out before each
22344 object file is processed. */
22345 if (filedata->dump.num_dump_sects > cmdline.num_dump_sects)
22346 memset (filedata->dump.dump_sects, 0,
22347 filedata->dump.num_dump_sects * sizeof (*filedata->dump.dump_sects));
22348
22349 if (cmdline.num_dump_sects > 0)
22350 {
22351 if (filedata->dump.num_dump_sects == 0)
22352 /* A sneaky way of allocating the dump_sects array. */
22353 request_dump_bynumber (&filedata->dump, cmdline.num_dump_sects, 0);
22354
22355 assert (filedata->dump.num_dump_sects >= cmdline.num_dump_sects);
22356 memcpy (filedata->dump.dump_sects, cmdline.dump_sects,
22357 cmdline.num_dump_sects * sizeof (*filedata->dump.dump_sects));
22358 }
22359 }
22360
22361 static bool
22362 might_need_separate_debug_info (Filedata * filedata)
22363 {
22364 /* Debuginfo files do not need further separate file loading. */
22365 if (filedata->file_header.e_shstrndx == SHN_UNDEF)
22366 return false;
22367
22368 /* Since do_follow_links might be enabled by default, only treat it as an
22369 indication that separate files should be loaded if setting it was a
22370 deliberate user action. */
22371 if (DEFAULT_FOR_FOLLOW_LINKS == 0 && do_follow_links)
22372 return true;
22373
22374 if (process_links || do_syms || do_unwind
22375 || dump_any_debugging || do_dump || do_debugging)
22376 return true;
22377
22378 return false;
22379 }
22380
22381 /* Process one ELF object file according to the command line options.
22382 This file may actually be stored in an archive. The file is
22383 positioned at the start of the ELF object. Returns TRUE if no
22384 problems were encountered, FALSE otherwise. */
22385
22386 static bool
22387 process_object (Filedata * filedata)
22388 {
22389 bool have_separate_files;
22390 unsigned int i;
22391 bool res;
22392
22393 if (! get_file_header (filedata))
22394 {
22395 error (_("%s: Failed to read file header\n"), filedata->file_name);
22396 return false;
22397 }
22398
22399 /* Initialise per file variables. */
22400 for (i = ARRAY_SIZE (filedata->version_info); i--;)
22401 filedata->version_info[i] = 0;
22402
22403 for (i = ARRAY_SIZE (filedata->dynamic_info); i--;)
22404 filedata->dynamic_info[i] = 0;
22405 filedata->dynamic_info_DT_GNU_HASH = 0;
22406 filedata->dynamic_info_DT_MIPS_XHASH = 0;
22407
22408 /* Process the file. */
22409 if (show_name)
22410 printf (_("\nFile: %s\n"), filedata->file_name);
22411
22412 initialise_dump_sects (filedata);
22413
22414 /* There may be some extensions in the first section header. Don't
22415 bomb if we can't read it. */
22416 get_section_headers (filedata, true);
22417
22418 if (! process_file_header (filedata))
22419 {
22420 res = false;
22421 goto out;
22422 }
22423
22424 /* Throw away the single section header read above, so that we
22425 re-read the entire set. */
22426 free (filedata->section_headers);
22427 filedata->section_headers = NULL;
22428
22429 if (! process_section_headers (filedata))
22430 {
22431 /* Without loaded section headers we cannot process lots of things. */
22432 do_unwind = do_version = do_dump = do_arch = false;
22433
22434 if (! do_using_dynamic)
22435 do_syms = do_dyn_syms = do_reloc = false;
22436 }
22437
22438 if (! process_section_groups (filedata))
22439 /* Without loaded section groups we cannot process unwind. */
22440 do_unwind = false;
22441
22442 process_program_headers (filedata);
22443
22444 res = process_dynamic_section (filedata);
22445
22446 if (! process_relocs (filedata))
22447 res = false;
22448
22449 if (! process_unwind (filedata))
22450 res = false;
22451
22452 if (! process_symbol_table (filedata))
22453 res = false;
22454
22455 if (! process_lto_symbol_tables (filedata))
22456 res = false;
22457
22458 if (! process_syminfo (filedata))
22459 res = false;
22460
22461 if (! process_version_sections (filedata))
22462 res = false;
22463
22464 if (might_need_separate_debug_info (filedata))
22465 have_separate_files = load_separate_debug_files (filedata, filedata->file_name);
22466 else
22467 have_separate_files = false;
22468
22469 if (! process_section_contents (filedata))
22470 res = false;
22471
22472 if (have_separate_files)
22473 {
22474 separate_info * d;
22475
22476 for (d = first_separate_info; d != NULL; d = d->next)
22477 {
22478 initialise_dump_sects (d->handle);
22479
22480 if (process_links && ! process_file_header (d->handle))
22481 res = false;
22482 else if (! process_section_headers (d->handle))
22483 res = false;
22484 else if (! process_section_contents (d->handle))
22485 res = false;
22486 else if (process_links)
22487 {
22488 if (! process_section_groups (d->handle))
22489 res = false;
22490 process_program_headers (d->handle);
22491 if (! process_dynamic_section (d->handle))
22492 res = false;
22493 if (! process_relocs (d->handle))
22494 res = false;
22495 if (! process_unwind (d->handle))
22496 res = false;
22497 if (! process_symbol_table (d->handle))
22498 res = false;
22499 if (! process_lto_symbol_tables (d->handle))
22500 res = false;
22501 if (! process_syminfo (d->handle))
22502 res = false;
22503 if (! process_version_sections (d->handle))
22504 res = false;
22505 if (! process_notes (d->handle))
22506 res = false;
22507 }
22508 }
22509
22510 /* The file handles are closed by the call to free_debug_memory() below. */
22511 }
22512
22513 if (! process_notes (filedata))
22514 res = false;
22515
22516 if (! process_gnu_liblist (filedata))
22517 res = false;
22518
22519 if (! process_arch_specific (filedata))
22520 res = false;
22521
22522 out:
22523 free_filedata (filedata);
22524
22525 free_debug_memory ();
22526
22527 return res;
22528 }
22529
22530 /* Process an ELF archive.
22531 On entry the file is positioned just after the ARMAG string.
22532 Returns TRUE upon success, FALSE otherwise. */
22533
22534 static bool
22535 process_archive (Filedata * filedata, bool is_thin_archive)
22536 {
22537 struct archive_info arch;
22538 struct archive_info nested_arch;
22539 size_t got;
22540 bool ret = true;
22541
22542 show_name = true;
22543
22544 /* The ARCH structure is used to hold information about this archive. */
22545 arch.file_name = NULL;
22546 arch.file = NULL;
22547 arch.index_array = NULL;
22548 arch.sym_table = NULL;
22549 arch.longnames = NULL;
22550
22551 /* The NESTED_ARCH structure is used as a single-item cache of information
22552 about a nested archive (when members of a thin archive reside within
22553 another regular archive file). */
22554 nested_arch.file_name = NULL;
22555 nested_arch.file = NULL;
22556 nested_arch.index_array = NULL;
22557 nested_arch.sym_table = NULL;
22558 nested_arch.longnames = NULL;
22559
22560 if (setup_archive (&arch, filedata->file_name, filedata->handle,
22561 filedata->file_size, is_thin_archive,
22562 do_archive_index) != 0)
22563 {
22564 ret = false;
22565 goto out;
22566 }
22567
22568 if (do_archive_index)
22569 {
22570 if (arch.sym_table == NULL)
22571 error (_("%s: unable to dump the index as none was found\n"),
22572 filedata->file_name);
22573 else
22574 {
22575 unsigned long i, l;
22576 unsigned long current_pos;
22577
22578 printf (_("Index of archive %s: (%lu entries, 0x%lx bytes "
22579 "in the symbol table)\n"),
22580 filedata->file_name, (unsigned long) arch.index_num,
22581 arch.sym_size);
22582
22583 current_pos = ftell (filedata->handle);
22584
22585 for (i = l = 0; i < arch.index_num; i++)
22586 {
22587 if (i == 0
22588 || (i > 0 && arch.index_array[i] != arch.index_array[i - 1]))
22589 {
22590 char * member_name
22591 = get_archive_member_name_at (&arch, arch.index_array[i],
22592 &nested_arch);
22593
22594 if (member_name != NULL)
22595 {
22596 char * qualified_name
22597 = make_qualified_name (&arch, &nested_arch,
22598 member_name);
22599
22600 if (qualified_name != NULL)
22601 {
22602 printf (_("Contents of binary %s at offset "),
22603 qualified_name);
22604 (void) print_vma (arch.index_array[i], PREFIX_HEX);
22605 putchar ('\n');
22606 free (qualified_name);
22607 }
22608 free (member_name);
22609 }
22610 }
22611
22612 if (l >= arch.sym_size)
22613 {
22614 error (_("%s: end of the symbol table reached "
22615 "before the end of the index\n"),
22616 filedata->file_name);
22617 ret = false;
22618 break;
22619 }
22620 /* PR 17531: file: 0b6630b2. */
22621 printf ("\t%.*s\n",
22622 (int) (arch.sym_size - l), arch.sym_table + l);
22623 l += strnlen (arch.sym_table + l, arch.sym_size - l) + 1;
22624 }
22625
22626 if (arch.uses_64bit_indices)
22627 l = (l + 7) & ~ 7;
22628 else
22629 l += l & 1;
22630
22631 if (l < arch.sym_size)
22632 {
22633 error (ngettext ("%s: %ld byte remains in the symbol table, "
22634 "but without corresponding entries in "
22635 "the index table\n",
22636 "%s: %ld bytes remain in the symbol table, "
22637 "but without corresponding entries in "
22638 "the index table\n",
22639 arch.sym_size - l),
22640 filedata->file_name, arch.sym_size - l);
22641 ret = false;
22642 }
22643
22644 if (fseek64 (filedata->handle, current_pos, SEEK_SET) != 0)
22645 {
22646 error (_("%s: failed to seek back to start of object files "
22647 "in the archive\n"),
22648 filedata->file_name);
22649 ret = false;
22650 goto out;
22651 }
22652 }
22653
22654 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
22655 && !do_segments && !do_header && !do_dump && !do_version
22656 && !do_histogram && !do_debugging && !do_arch && !do_notes
22657 && !do_section_groups && !do_dyn_syms)
22658 {
22659 ret = true; /* Archive index only. */
22660 goto out;
22661 }
22662 }
22663
22664 while (1)
22665 {
22666 char * name;
22667 size_t namelen;
22668 char * qualified_name;
22669
22670 /* Read the next archive header. */
22671 if (fseek64 (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0)
22672 {
22673 error (_("%s: failed to seek to next archive header\n"),
22674 arch.file_name);
22675 ret = false;
22676 break;
22677 }
22678 got = fread (&arch.arhdr, 1, sizeof arch.arhdr, filedata->handle);
22679 if (got != sizeof arch.arhdr)
22680 {
22681 if (got == 0)
22682 break;
22683 /* PR 24049 - we cannot use filedata->file_name as this will
22684 have already been freed. */
22685 error (_("%s: failed to read archive header\n"), arch.file_name);
22686
22687 ret = false;
22688 break;
22689 }
22690 if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
22691 {
22692 error (_("%s: did not find a valid archive header\n"),
22693 arch.file_name);
22694 ret = false;
22695 break;
22696 }
22697
22698 arch.next_arhdr_offset += sizeof arch.arhdr;
22699
22700 filedata->archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
22701
22702 name = get_archive_member_name (&arch, &nested_arch);
22703 if (name == NULL)
22704 {
22705 error (_("%s: bad archive file name\n"), arch.file_name);
22706 ret = false;
22707 break;
22708 }
22709 namelen = strlen (name);
22710
22711 qualified_name = make_qualified_name (&arch, &nested_arch, name);
22712 if (qualified_name == NULL)
22713 {
22714 error (_("%s: bad archive file name\n"), arch.file_name);
22715 free (name);
22716 ret = false;
22717 break;
22718 }
22719
22720 if (is_thin_archive && arch.nested_member_origin == 0)
22721 {
22722 /* This is a proxy for an external member of a thin archive. */
22723 Filedata * member_filedata;
22724 char * member_file_name = adjust_relative_path
22725 (filedata->file_name, name, namelen);
22726
22727 free (name);
22728 if (member_file_name == NULL)
22729 {
22730 free (qualified_name);
22731 ret = false;
22732 break;
22733 }
22734
22735 member_filedata = open_file (member_file_name, false);
22736 if (member_filedata == NULL)
22737 {
22738 error (_("Input file '%s' is not readable.\n"), member_file_name);
22739 free (member_file_name);
22740 free (qualified_name);
22741 ret = false;
22742 break;
22743 }
22744
22745 filedata->archive_file_offset = arch.nested_member_origin;
22746 member_filedata->file_name = qualified_name;
22747
22748 /* The call to process_object() expects the file to be at the beginning. */
22749 rewind (member_filedata->handle);
22750
22751 if (! process_object (member_filedata))
22752 ret = false;
22753
22754 close_file (member_filedata);
22755 free (member_file_name);
22756 }
22757 else if (is_thin_archive)
22758 {
22759 Filedata thin_filedata;
22760
22761 memset (&thin_filedata, 0, sizeof (thin_filedata));
22762
22763 /* PR 15140: Allow for corrupt thin archives. */
22764 if (nested_arch.file == NULL)
22765 {
22766 error (_("%s: contains corrupt thin archive: %s\n"),
22767 qualified_name, name);
22768 free (qualified_name);
22769 free (name);
22770 ret = false;
22771 break;
22772 }
22773 free (name);
22774
22775 /* This is a proxy for a member of a nested archive. */
22776 filedata->archive_file_offset
22777 = arch.nested_member_origin + sizeof arch.arhdr;
22778
22779 /* The nested archive file will have been opened and setup by
22780 get_archive_member_name. */
22781 if (fseek64 (nested_arch.file, filedata->archive_file_offset,
22782 SEEK_SET) != 0)
22783 {
22784 error (_("%s: failed to seek to archive member.\n"),
22785 nested_arch.file_name);
22786 free (qualified_name);
22787 ret = false;
22788 break;
22789 }
22790
22791 thin_filedata.handle = nested_arch.file;
22792 thin_filedata.file_name = qualified_name;
22793
22794 if (! process_object (& thin_filedata))
22795 ret = false;
22796 }
22797 else
22798 {
22799 free (name);
22800 filedata->archive_file_offset = arch.next_arhdr_offset;
22801 filedata->file_name = qualified_name;
22802 if (! process_object (filedata))
22803 ret = false;
22804 arch.next_arhdr_offset += (filedata->archive_file_size + 1) & -2;
22805 /* Stop looping with "negative" archive_file_size. */
22806 if (arch.next_arhdr_offset < filedata->archive_file_size)
22807 arch.next_arhdr_offset = -1ul;
22808 }
22809
22810 free (qualified_name);
22811 }
22812
22813 out:
22814 if (nested_arch.file != NULL)
22815 fclose (nested_arch.file);
22816 release_archive (&nested_arch);
22817 release_archive (&arch);
22818
22819 return ret;
22820 }
22821
22822 static bool
22823 process_file (char * file_name)
22824 {
22825 Filedata * filedata = NULL;
22826 struct stat statbuf;
22827 char armag[SARMAG];
22828 bool ret = true;
22829
22830 if (stat (file_name, &statbuf) < 0)
22831 {
22832 if (errno == ENOENT)
22833 error (_("'%s': No such file\n"), file_name);
22834 else
22835 error (_("Could not locate '%s'. System error message: %s\n"),
22836 file_name, strerror (errno));
22837 return false;
22838 }
22839
22840 if (! S_ISREG (statbuf.st_mode))
22841 {
22842 error (_("'%s' is not an ordinary file\n"), file_name);
22843 return false;
22844 }
22845
22846 filedata = calloc (1, sizeof * filedata);
22847 if (filedata == NULL)
22848 {
22849 error (_("Out of memory allocating file data structure\n"));
22850 return false;
22851 }
22852
22853 filedata->file_name = file_name;
22854 filedata->handle = fopen (file_name, "rb");
22855 if (filedata->handle == NULL)
22856 {
22857 error (_("Input file '%s' is not readable.\n"), file_name);
22858 free (filedata);
22859 return false;
22860 }
22861
22862 if (fread (armag, SARMAG, 1, filedata->handle) != 1)
22863 {
22864 error (_("%s: Failed to read file's magic number\n"), file_name);
22865 fclose (filedata->handle);
22866 free (filedata);
22867 return false;
22868 }
22869
22870 filedata->file_size = statbuf.st_size;
22871 filedata->is_separate = false;
22872
22873 if (memcmp (armag, ARMAG, SARMAG) == 0)
22874 {
22875 if (! process_archive (filedata, false))
22876 ret = false;
22877 }
22878 else if (memcmp (armag, ARMAGT, SARMAG) == 0)
22879 {
22880 if ( ! process_archive (filedata, true))
22881 ret = false;
22882 }
22883 else
22884 {
22885 if (do_archive_index && !check_all)
22886 error (_("File %s is not an archive so its index cannot be displayed.\n"),
22887 file_name);
22888
22889 rewind (filedata->handle);
22890 filedata->archive_file_size = filedata->archive_file_offset = 0;
22891
22892 if (! process_object (filedata))
22893 ret = false;
22894 }
22895
22896 fclose (filedata->handle);
22897 free (filedata->section_headers);
22898 free (filedata->program_headers);
22899 free (filedata->string_table);
22900 free (filedata->dump.dump_sects);
22901 free (filedata);
22902
22903 free (ba_cache.strtab);
22904 ba_cache.strtab = NULL;
22905 free (ba_cache.symtab);
22906 ba_cache.symtab = NULL;
22907 ba_cache.filedata = NULL;
22908
22909 return ret;
22910 }
22911
22912 #ifdef SUPPORT_DISASSEMBLY
22913 /* Needed by the i386 disassembler. For extra credit, someone could
22914 fix this so that we insert symbolic addresses here, esp for GOT/PLT
22915 symbols. */
22916
22917 void
22918 print_address (unsigned int addr, FILE * outfile)
22919 {
22920 fprintf (outfile,"0x%8.8x", addr);
22921 }
22922
22923 /* Needed by the i386 disassembler. */
22924
22925 void
22926 db_task_printsym (unsigned int addr)
22927 {
22928 print_address (addr, stderr);
22929 }
22930 #endif
22931
22932 int
22933 main (int argc, char ** argv)
22934 {
22935 int err;
22936
22937 #ifdef HAVE_LC_MESSAGES
22938 setlocale (LC_MESSAGES, "");
22939 #endif
22940 setlocale (LC_CTYPE, "");
22941 bindtextdomain (PACKAGE, LOCALEDIR);
22942 textdomain (PACKAGE);
22943
22944 expandargv (&argc, &argv);
22945
22946 parse_args (& cmdline, argc, argv);
22947
22948 if (optind < (argc - 1))
22949 /* When displaying information for more than one file,
22950 prefix the information with the file name. */
22951 show_name = true;
22952 else if (optind >= argc)
22953 {
22954 /* Ensure that the warning is always displayed. */
22955 do_checks = true;
22956
22957 warn (_("Nothing to do.\n"));
22958 usage (stderr);
22959 }
22960
22961 err = false;
22962 while (optind < argc)
22963 if (! process_file (argv[optind++]))
22964 err = true;
22965
22966 free (cmdline.dump_sects);
22967
22968 free (dump_ctf_symtab_name);
22969 free (dump_ctf_strtab_name);
22970 free (dump_ctf_parent_name);
22971
22972 return err ? EXIT_FAILURE : EXIT_SUCCESS;
22973 }