d45e09207880d0fa7c314108e0f64997feb9dd20
[binutils-gdb.git] / binutils / readelf.c
1 /* readelf.c -- display contents of an ELF format file
2 Copyright (C) 1998-2022 Free Software Foundation, Inc.
3
4 Originally developed by Eric Youngdale <eric@andante.jic.com>
5 Modifications by Nick Clifton <nickc@redhat.com>
6
7 This file is part of GNU Binutils.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
22 02110-1301, USA. */
23 \f
24 /* The difference between readelf and objdump:
25
26 Both programs are capable of displaying the contents of ELF format files,
27 so why does the binutils project have two file dumpers ?
28
29 The reason is that objdump sees an ELF file through a BFD filter of the
30 world; if BFD has a bug where, say, it disagrees about a machine constant
31 in e_flags, then the odds are good that it will remain internally
32 consistent. The linker sees it the BFD way, objdump sees it the BFD way,
33 GAS sees it the BFD way. There was need for a tool to go find out what
34 the file actually says.
35
36 This is why the readelf program does not link against the BFD library - it
37 exists as an independent program to help verify the correct working of BFD.
38
39 There is also the case that readelf can provide more information about an
40 ELF file than is provided by objdump. In particular it can display DWARF
41 debugging information which (at the moment) objdump cannot. */
42 \f
43 #include "sysdep.h"
44 #include <assert.h>
45 #include <time.h>
46 #include <zlib.h>
47 #include <wchar.h>
48
49 #if defined HAVE_MSGPACK
50 #include <msgpack.h>
51 #endif
52
53 #if __GNUC__ >= 2
54 /* Define BFD64 here, even if our default architecture is 32 bit ELF
55 as this will allow us to read in and parse 64bit and 32bit ELF files.
56 Only do this if we believe that the compiler can support a 64 bit
57 data type. For now we only rely on GCC being able to do this. */
58 #define BFD64
59 #endif
60
61 #include "bfd.h"
62 #include "bucomm.h"
63 #include "elfcomm.h"
64 #include "demanguse.h"
65 #include "dwarf.h"
66 #include "ctf-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/xc16x.h"
167 #include "elf/xgate.h"
168 #include "elf/xstormy16.h"
169 #include "elf/xtensa.h"
170 #include "elf/z80.h"
171 #include "elf/loongarch.h"
172
173 #include "getopt.h"
174 #include "libiberty.h"
175 #include "safe-ctype.h"
176 #include "filenames.h"
177
178 #ifndef offsetof
179 #define offsetof(TYPE, MEMBER) ((size_t) &(((TYPE *) 0)->MEMBER))
180 #endif
181
182 typedef struct elf_section_list
183 {
184 Elf_Internal_Shdr * hdr;
185 struct elf_section_list * next;
186 } elf_section_list;
187
188 /* Flag bits indicating particular types of dump. */
189 #define HEX_DUMP (1 << 0) /* The -x command line switch. */
190 #define DISASS_DUMP (1 << 1) /* The -i command line switch. */
191 #define DEBUG_DUMP (1 << 2) /* The -w command line switch. */
192 #define STRING_DUMP (1 << 3) /* The -p command line switch. */
193 #define RELOC_DUMP (1 << 4) /* The -R command line switch. */
194 #define CTF_DUMP (1 << 5) /* The --ctf 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_arch = false;
239 static bool do_notes = false;
240 static bool do_archive_index = false;
241 static bool check_all = false;
242 static bool is_32bit_elf = false;
243 static bool decompress_dumps = false;
244 static bool do_not_show_symbol_truncation = false;
245 static bool do_demangle = false; /* Pretty print C++ symbol names. */
246 static bool process_links = false;
247 static bool dump_any_debugging = false;
248 static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
249 static int sym_base = 0;
250
251 static char *dump_ctf_parent_name;
252 static char *dump_ctf_symtab_name;
253 static char *dump_ctf_strtab_name;
254
255 struct group_list
256 {
257 struct group_list * next;
258 unsigned int section_index;
259 };
260
261 struct group
262 {
263 struct group_list * root;
264 unsigned int group_index;
265 };
266
267 typedef struct filedata
268 {
269 const char * file_name;
270 bool is_separate;
271 FILE * handle;
272 bfd_size_type file_size;
273 Elf_Internal_Ehdr file_header;
274 unsigned long archive_file_offset;
275 unsigned long archive_file_size;
276 /* Everything below this point is cleared out by free_filedata. */
277 Elf_Internal_Shdr * section_headers;
278 Elf_Internal_Phdr * program_headers;
279 char * string_table;
280 unsigned long string_table_length;
281 unsigned long dynamic_addr;
282 bfd_size_type dynamic_size;
283 size_t dynamic_nent;
284 Elf_Internal_Dyn * dynamic_section;
285 Elf_Internal_Shdr * dynamic_strtab_section;
286 char * dynamic_strings;
287 unsigned long dynamic_strings_length;
288 Elf_Internal_Shdr * dynamic_symtab_section;
289 unsigned long num_dynamic_syms;
290 Elf_Internal_Sym * dynamic_symbols;
291 bfd_vma version_info[16];
292 unsigned int dynamic_syminfo_nent;
293 Elf_Internal_Syminfo * dynamic_syminfo;
294 unsigned long dynamic_syminfo_offset;
295 bfd_size_type nbuckets;
296 bfd_size_type nchains;
297 bfd_vma * buckets;
298 bfd_vma * chains;
299 bfd_size_type ngnubuckets;
300 bfd_size_type ngnuchains;
301 bfd_vma * gnubuckets;
302 bfd_vma * gnuchains;
303 bfd_vma * mipsxlat;
304 bfd_vma gnusymidx;
305 char * program_interpreter;
306 bfd_vma dynamic_info[DT_ENCODING];
307 bfd_vma dynamic_info_DT_GNU_HASH;
308 bfd_vma dynamic_info_DT_MIPS_XHASH;
309 elf_section_list * symtab_shndx_list;
310 size_t group_count;
311 struct group * section_groups;
312 struct group ** section_headers_groups;
313 /* A dynamic array of flags indicating for which sections a dump of
314 some kind has been requested. It is reset on a per-object file
315 basis and then initialised from the cmdline_dump_sects array,
316 the results of interpreting the -w switch, and the
317 dump_sects_byname list. */
318 struct dump_data dump;
319 } Filedata;
320
321 /* How to print a vma value. */
322 typedef enum print_mode
323 {
324 HEX,
325 HEX_5,
326 DEC,
327 DEC_5,
328 UNSIGNED,
329 UNSIGNED_5,
330 PREFIX_HEX,
331 PREFIX_HEX_5,
332 FULL_HEX,
333 LONG_HEX,
334 OCTAL,
335 OCTAL_5
336 }
337 print_mode;
338
339 typedef enum unicode_display_type
340 {
341 unicode_default = 0,
342 unicode_locale,
343 unicode_escape,
344 unicode_hex,
345 unicode_highlight,
346 unicode_invalid
347 } unicode_display_type;
348
349 static unicode_display_type unicode_display = unicode_default;
350
351 typedef enum
352 {
353 reltype_unknown,
354 reltype_rel,
355 reltype_rela,
356 reltype_relr
357 } relocation_type;
358
359 /* Versioned symbol info. */
360 enum versioned_symbol_info
361 {
362 symbol_undefined,
363 symbol_hidden,
364 symbol_public
365 };
366
367 static const char * get_symbol_version_string
368 (Filedata *, bool, const char *, unsigned long, unsigned,
369 Elf_Internal_Sym *, enum versioned_symbol_info *, unsigned short *);
370
371 #define UNKNOWN -1
372
373 static inline const char *
374 section_name (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
375 {
376 return filedata->string_table + hdr->sh_name;
377 }
378
379 static inline bool
380 section_name_valid (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
381 {
382 return (hdr != NULL
383 && filedata->string_table != NULL
384 && hdr->sh_name < filedata->string_table_length);
385 }
386
387 static inline const char *
388 section_name_print (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
389 {
390 if (hdr == NULL)
391 return _("<none>");
392 if (filedata->string_table == NULL)
393 return _("<no-strings>");
394 if (hdr->sh_name >= filedata->string_table_length)
395 return _("<corrupt>");
396 return section_name (filedata, hdr);
397 }
398
399 #define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */
400
401 static inline bool
402 valid_symbol_name (const char *strtab, size_t strtab_size, uint64_t offset)
403 {
404 return strtab != NULL && offset < strtab_size;
405 }
406
407 static inline bool
408 valid_dynamic_name (const Filedata *filedata, uint64_t offset)
409 {
410 return valid_symbol_name (filedata->dynamic_strings,
411 filedata->dynamic_strings_length, offset);
412 }
413
414 /* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
415 already been called and verified that the string exists. */
416 static inline const char *
417 get_dynamic_name (const Filedata *filedata, size_t offset)
418 {
419 return filedata->dynamic_strings + offset;
420 }
421
422 #define REMOVE_ARCH_BITS(ADDR) \
423 do \
424 { \
425 if (filedata->file_header.e_machine == EM_ARM) \
426 (ADDR) &= ~1; \
427 } \
428 while (0)
429
430 /* Get the correct GNU hash section name. */
431 #define GNU_HASH_SECTION_NAME(filedata) \
432 filedata->dynamic_info_DT_MIPS_XHASH ? ".MIPS.xhash" : ".gnu.hash"
433 \f
434 /* Print a BFD_VMA to an internal buffer, for use in error messages.
435 BFD_FMA_FMT can't be used in translated strings. */
436
437 static const char *
438 bfd_vmatoa (char *fmtch, bfd_vma value)
439 {
440 /* bfd_vmatoa is used more then once in a printf call for output.
441 Cycle through an array of buffers. */
442 static int buf_pos = 0;
443 static struct bfd_vmatoa_buf
444 {
445 char place[64];
446 } buf[4];
447 char *ret;
448 char fmt[32];
449
450 ret = buf[buf_pos++].place;
451 buf_pos %= ARRAY_SIZE (buf);
452
453 sprintf (fmt, "%%%s%s", BFD_VMA_FMT, fmtch);
454 snprintf (ret, sizeof (buf[0].place), fmt, value);
455 return ret;
456 }
457
458 /* Retrieve NMEMB structures, each SIZE bytes long from FILEDATA starting at
459 OFFSET + the offset of the current archive member, if we are examining an
460 archive. Put the retrieved data into VAR, if it is not NULL. Otherwise
461 allocate a buffer using malloc and fill that. In either case return the
462 pointer to the start of the retrieved data or NULL if something went wrong.
463 If something does go wrong and REASON is not NULL then emit an error
464 message using REASON as part of the context. */
465
466 static void *
467 get_data (void * var,
468 Filedata * filedata,
469 unsigned long offset,
470 bfd_size_type size,
471 bfd_size_type nmemb,
472 const char * reason)
473 {
474 void * mvar;
475 bfd_size_type amt = size * nmemb;
476
477 if (size == 0 || nmemb == 0)
478 return NULL;
479
480 /* If the size_t type is smaller than the bfd_size_type, eg because
481 you are building a 32-bit tool on a 64-bit host, then make sure
482 that when the sizes are cast to (size_t) no information is lost. */
483 if ((size_t) size != size
484 || (size_t) nmemb != nmemb
485 || (size_t) amt != amt)
486 {
487 if (reason)
488 error (_("Size truncation prevents reading %s"
489 " elements of size %s for %s\n"),
490 bfd_vmatoa ("u", nmemb), bfd_vmatoa ("u", size), reason);
491 return NULL;
492 }
493
494 /* Check for size overflow. */
495 if (amt / size != nmemb || (size_t) amt + 1 == 0)
496 {
497 if (reason)
498 error (_("Size overflow prevents reading %s"
499 " elements of size %s for %s\n"),
500 bfd_vmatoa ("u", nmemb), bfd_vmatoa ("u", size), reason);
501 return NULL;
502 }
503
504 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
505 attempting to allocate memory when the read is bound to fail. */
506 if (filedata->archive_file_offset > filedata->file_size
507 || offset > filedata->file_size - filedata->archive_file_offset
508 || amt > filedata->file_size - filedata->archive_file_offset - offset)
509 {
510 if (reason)
511 error (_("Reading %s bytes extends past end of file for %s\n"),
512 bfd_vmatoa ("u", amt), reason);
513 return NULL;
514 }
515
516 if (fseek (filedata->handle, filedata->archive_file_offset + offset,
517 SEEK_SET))
518 {
519 if (reason)
520 error (_("Unable to seek to 0x%lx for %s\n"),
521 filedata->archive_file_offset + offset, reason);
522 return NULL;
523 }
524
525 mvar = var;
526 if (mvar == NULL)
527 {
528 /* + 1 so that we can '\0' terminate invalid string table sections. */
529 mvar = malloc ((size_t) amt + 1);
530
531 if (mvar == NULL)
532 {
533 if (reason)
534 error (_("Out of memory allocating %s bytes for %s\n"),
535 bfd_vmatoa ("u", amt), reason);
536 return NULL;
537 }
538
539 ((char *) mvar)[amt] = '\0';
540 }
541
542 if (fread (mvar, (size_t) size, (size_t) nmemb, filedata->handle) != nmemb)
543 {
544 if (reason)
545 error (_("Unable to read in %s bytes of %s\n"),
546 bfd_vmatoa ("u", amt), reason);
547 if (mvar != var)
548 free (mvar);
549 return NULL;
550 }
551
552 return mvar;
553 }
554
555 /* Print a VMA value in the MODE specified.
556 Returns the number of characters displayed. */
557
558 static unsigned int
559 print_vma (bfd_vma vma, print_mode mode)
560 {
561 unsigned int nc = 0;
562
563 switch (mode)
564 {
565 case FULL_HEX:
566 nc = printf ("0x");
567 /* Fall through. */
568 case LONG_HEX:
569 #ifdef BFD64
570 if (is_32bit_elf)
571 return nc + printf ("%8.8" BFD_VMA_FMT "x", vma);
572 #endif
573 printf_vma (vma);
574 return nc + 16;
575
576 case DEC_5:
577 if (vma <= 99999)
578 return printf ("%5" BFD_VMA_FMT "d", vma);
579 /* Fall through. */
580 case PREFIX_HEX:
581 nc = printf ("0x");
582 /* Fall through. */
583 case HEX:
584 return nc + printf ("%" BFD_VMA_FMT "x", vma);
585
586 case PREFIX_HEX_5:
587 nc = printf ("0x");
588 /* Fall through. */
589 case HEX_5:
590 return nc + printf ("%05" BFD_VMA_FMT "x", vma);
591
592 case DEC:
593 return printf ("%" BFD_VMA_FMT "d", vma);
594
595 case UNSIGNED:
596 return printf ("%" BFD_VMA_FMT "u", vma);
597
598 case UNSIGNED_5:
599 return printf ("%5" BFD_VMA_FMT "u", vma);
600
601 case OCTAL:
602 return printf ("%" BFD_VMA_FMT "o", vma);
603
604 case OCTAL_5:
605 return printf ("%5" BFD_VMA_FMT "o", vma);
606
607 default:
608 /* FIXME: Report unrecognised mode ? */
609 return 0;
610 }
611 }
612
613
614 /* Display a symbol on stdout. Handles the display of control characters and
615 multibye characters (assuming the host environment supports them).
616
617 Display at most abs(WIDTH) characters, truncating as necessary, unless do_wide is true.
618
619 If truncation will happen and do_not_show_symbol_truncation is FALSE then display
620 abs(WIDTH) - 5 characters followed by "[...]".
621
622 If WIDTH is negative then ensure that the output is at least (- WIDTH) characters,
623 padding as necessary.
624
625 Returns the number of emitted characters. */
626
627 static unsigned int
628 print_symbol (signed int width, const char * symbol)
629 {
630 bool extra_padding = false;
631 bool do_dots = false;
632 signed int num_printed = 0;
633 #ifdef HAVE_MBSTATE_T
634 mbstate_t state;
635 #endif
636 unsigned int width_remaining;
637 const void * alloced_symbol = NULL;
638
639 if (width < 0)
640 {
641 /* Keep the width positive. This helps the code below. */
642 width = - width;
643 extra_padding = true;
644 }
645 else if (width == 0)
646 return 0;
647
648 if (do_wide)
649 /* Set the remaining width to a very large value.
650 This simplifies the code below. */
651 width_remaining = INT_MAX;
652 else
653 {
654 width_remaining = width;
655 if (! do_not_show_symbol_truncation
656 && (int) strlen (symbol) > width)
657 {
658 width_remaining -= 5;
659 if ((int) width_remaining < 0)
660 width_remaining = 0;
661 do_dots = true;
662 }
663 }
664
665 #ifdef HAVE_MBSTATE_T
666 /* Initialise the multibyte conversion state. */
667 memset (& state, 0, sizeof (state));
668 #endif
669
670 if (do_demangle && *symbol)
671 {
672 const char * res = cplus_demangle (symbol, demangle_flags);
673
674 if (res != NULL)
675 alloced_symbol = symbol = res;
676 }
677
678 while (width_remaining)
679 {
680 size_t n;
681 const char c = *symbol++;
682
683 if (c == 0)
684 break;
685
686 if (ISPRINT (c))
687 {
688 putchar (c);
689 width_remaining --;
690 num_printed ++;
691 }
692 else if (ISCNTRL (c))
693 {
694 /* Do not print control characters directly as they can affect terminal
695 settings. Such characters usually appear in the names generated
696 by the assembler for local labels. */
697
698 if (width_remaining < 2)
699 break;
700
701 printf ("^%c", c + 0x40);
702 width_remaining -= 2;
703 num_printed += 2;
704 }
705 else if (c == 0x7f)
706 {
707 if (width_remaining < 5)
708 break;
709 printf ("<DEL>");
710 width_remaining -= 5;
711 num_printed += 5;
712 }
713 else if (unicode_display != unicode_locale
714 && unicode_display != unicode_default)
715 {
716 /* Display unicode characters as something else. */
717 unsigned char bytes[4];
718 bool is_utf8;
719 unsigned int nbytes;
720
721 bytes[0] = c;
722
723 if (bytes[0] < 0xc0)
724 {
725 nbytes = 1;
726 is_utf8 = false;
727 }
728 else
729 {
730 bytes[1] = *symbol++;
731
732 if ((bytes[1] & 0xc0) != 0x80)
733 {
734 is_utf8 = false;
735 /* Do not consume this character. It may only
736 be the first byte in the sequence that was
737 corrupt. */
738 --symbol;
739 nbytes = 1;
740 }
741 else if ((bytes[0] & 0x20) == 0)
742 {
743 is_utf8 = true;
744 nbytes = 2;
745 }
746 else
747 {
748 bytes[2] = *symbol++;
749
750 if ((bytes[2] & 0xc0) != 0x80)
751 {
752 is_utf8 = false;
753 symbol -= 2;
754 nbytes = 1;
755 }
756 else if ((bytes[0] & 0x10) == 0)
757 {
758 is_utf8 = true;
759 nbytes = 3;
760 }
761 else
762 {
763 bytes[3] = *symbol++;
764
765 nbytes = 4;
766
767 if ((bytes[3] & 0xc0) != 0x80)
768 {
769 is_utf8 = false;
770 symbol -= 3;
771 nbytes = 1;
772 }
773 else
774 is_utf8 = true;
775 }
776 }
777 }
778
779 if (unicode_display == unicode_invalid)
780 is_utf8 = false;
781
782 if (unicode_display == unicode_hex || ! is_utf8)
783 {
784 unsigned int i;
785
786 if (width_remaining < (nbytes * 2) + 2)
787 break;
788
789 putchar (is_utf8 ? '<' : '{');
790 printf ("0x");
791 for (i = 0; i < nbytes; i++)
792 printf ("%02x", bytes[i]);
793 putchar (is_utf8 ? '>' : '}');
794 }
795 else
796 {
797 if (unicode_display == unicode_highlight && isatty (1))
798 printf ("\x1B[31;47m"); /* Red. */
799
800 switch (nbytes)
801 {
802 case 2:
803 if (width_remaining < 6)
804 break;
805 printf ("\\u%02x%02x",
806 (bytes[0] & 0x1c) >> 2,
807 ((bytes[0] & 0x03) << 6) | (bytes[1] & 0x3f));
808 break;
809 case 3:
810 if (width_remaining < 6)
811 break;
812 printf ("\\u%02x%02x",
813 ((bytes[0] & 0x0f) << 4) | ((bytes[1] & 0x3c) >> 2),
814 ((bytes[1] & 0x03) << 6) | (bytes[2] & 0x3f));
815 break;
816 case 4:
817 if (width_remaining < 8)
818 break;
819 printf ("\\u%02x%02x%02x",
820 ((bytes[0] & 0x07) << 6) | ((bytes[1] & 0x3c) >> 2),
821 ((bytes[1] & 0x03) << 6) | ((bytes[2] & 0x3c) >> 2),
822 ((bytes[2] & 0x03) << 6) | (bytes[3] & 0x3f));
823
824 break;
825 default:
826 /* URG. */
827 break;
828 }
829
830 if (unicode_display == unicode_highlight && isatty (1))
831 printf ("\033[0m"); /* Default colour. */
832 }
833
834 if (bytes[nbytes - 1] == 0)
835 break;
836 }
837 else
838 {
839 #ifdef HAVE_MBSTATE_T
840 wchar_t w;
841 #endif
842 /* Let printf do the hard work of displaying multibyte characters. */
843 printf ("%.1s", symbol - 1);
844 width_remaining --;
845 num_printed ++;
846
847 #ifdef HAVE_MBSTATE_T
848 /* Try to find out how many bytes made up the character that was
849 just printed. Advance the symbol pointer past the bytes that
850 were displayed. */
851 n = mbrtowc (& w, symbol - 1, MB_CUR_MAX, & state);
852 #else
853 n = 1;
854 #endif
855 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
856 symbol += (n - 1);
857 }
858 }
859
860 if (do_dots)
861 num_printed += printf ("[...]");
862
863 if (extra_padding && num_printed < width)
864 {
865 /* Fill in the remaining spaces. */
866 printf ("%-*s", width - num_printed, " ");
867 num_printed = width;
868 }
869
870 free ((void *) alloced_symbol);
871 return num_printed;
872 }
873
874 /* Returns a pointer to a static buffer containing a printable version of
875 the given section's name. Like print_symbol, except that it does not try
876 to print multibyte characters, it just interprets them as hex values. */
877
878 static const char *
879 printable_section_name (Filedata * filedata, const Elf_Internal_Shdr * sec)
880 {
881 #define MAX_PRINT_SEC_NAME_LEN 256
882 static char sec_name_buf [MAX_PRINT_SEC_NAME_LEN + 1];
883 const char * name = section_name_print (filedata, sec);
884 char * buf = sec_name_buf;
885 char c;
886 unsigned int remaining = MAX_PRINT_SEC_NAME_LEN;
887
888 while ((c = * name ++) != 0)
889 {
890 if (ISCNTRL (c))
891 {
892 if (remaining < 2)
893 break;
894
895 * buf ++ = '^';
896 * buf ++ = c + 0x40;
897 remaining -= 2;
898 }
899 else if (ISPRINT (c))
900 {
901 * buf ++ = c;
902 remaining -= 1;
903 }
904 else
905 {
906 static char hex[17] = "0123456789ABCDEF";
907
908 if (remaining < 4)
909 break;
910 * buf ++ = '<';
911 * buf ++ = hex[(c & 0xf0) >> 4];
912 * buf ++ = hex[c & 0x0f];
913 * buf ++ = '>';
914 remaining -= 4;
915 }
916
917 if (remaining == 0)
918 break;
919 }
920
921 * buf = 0;
922 return sec_name_buf;
923 }
924
925 static const char *
926 printable_section_name_from_index (Filedata * filedata, unsigned long ndx)
927 {
928 if (ndx >= filedata->file_header.e_shnum)
929 return _("<corrupt>");
930
931 return printable_section_name (filedata, filedata->section_headers + ndx);
932 }
933
934 /* Return a pointer to section NAME, or NULL if no such section exists. */
935
936 static Elf_Internal_Shdr *
937 find_section (Filedata * filedata, const char * name)
938 {
939 unsigned int i;
940
941 if (filedata->section_headers == NULL)
942 return NULL;
943
944 for (i = 0; i < filedata->file_header.e_shnum; i++)
945 if (section_name_valid (filedata, filedata->section_headers + i)
946 && streq (section_name (filedata, filedata->section_headers + i),
947 name))
948 return filedata->section_headers + i;
949
950 return NULL;
951 }
952
953 /* Return a pointer to a section containing ADDR, or NULL if no such
954 section exists. */
955
956 static Elf_Internal_Shdr *
957 find_section_by_address (Filedata * filedata, bfd_vma addr)
958 {
959 unsigned int i;
960
961 if (filedata->section_headers == NULL)
962 return NULL;
963
964 for (i = 0; i < filedata->file_header.e_shnum; i++)
965 {
966 Elf_Internal_Shdr *sec = filedata->section_headers + i;
967
968 if (addr >= sec->sh_addr && addr < sec->sh_addr + sec->sh_size)
969 return sec;
970 }
971
972 return NULL;
973 }
974
975 static Elf_Internal_Shdr *
976 find_section_by_type (Filedata * filedata, unsigned int type)
977 {
978 unsigned int i;
979
980 if (filedata->section_headers == NULL)
981 return NULL;
982
983 for (i = 0; i < filedata->file_header.e_shnum; i++)
984 {
985 Elf_Internal_Shdr *sec = filedata->section_headers + i;
986
987 if (sec->sh_type == type)
988 return sec;
989 }
990
991 return NULL;
992 }
993
994 /* Return a pointer to section NAME, or NULL if no such section exists,
995 restricted to the list of sections given in SET. */
996
997 static Elf_Internal_Shdr *
998 find_section_in_set (Filedata * filedata, const char * name, unsigned int * set)
999 {
1000 unsigned int i;
1001
1002 if (filedata->section_headers == NULL)
1003 return NULL;
1004
1005 if (set != NULL)
1006 {
1007 while ((i = *set++) > 0)
1008 {
1009 /* See PR 21156 for a reproducer. */
1010 if (i >= filedata->file_header.e_shnum)
1011 continue; /* FIXME: Should we issue an error message ? */
1012
1013 if (section_name_valid (filedata, filedata->section_headers + i)
1014 && streq (section_name (filedata, filedata->section_headers + i),
1015 name))
1016 return filedata->section_headers + i;
1017 }
1018 }
1019
1020 return find_section (filedata, name);
1021 }
1022
1023 /* Return TRUE if the current file is for IA-64 machine and OpenVMS ABI.
1024 This OS has so many departures from the ELF standard that we test it at
1025 many places. */
1026
1027 static inline bool
1028 is_ia64_vms (Filedata * filedata)
1029 {
1030 return filedata->file_header.e_machine == EM_IA_64
1031 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS;
1032 }
1033
1034 /* Guess the relocation size commonly used by the specific machines. */
1035
1036 static bool
1037 guess_is_rela (unsigned int e_machine)
1038 {
1039 switch (e_machine)
1040 {
1041 /* Targets that use REL relocations. */
1042 case EM_386:
1043 case EM_IAMCU:
1044 case EM_960:
1045 case EM_ARM:
1046 case EM_D10V:
1047 case EM_CYGNUS_D10V:
1048 case EM_DLX:
1049 case EM_MIPS:
1050 case EM_MIPS_RS3_LE:
1051 case EM_CYGNUS_M32R:
1052 case EM_SCORE:
1053 case EM_XGATE:
1054 case EM_NFP:
1055 case EM_BPF:
1056 return false;
1057
1058 /* Targets that use RELA relocations. */
1059 case EM_68K:
1060 case EM_860:
1061 case EM_AARCH64:
1062 case EM_ADAPTEVA_EPIPHANY:
1063 case EM_ALPHA:
1064 case EM_ALTERA_NIOS2:
1065 case EM_ARC:
1066 case EM_ARC_COMPACT:
1067 case EM_ARC_COMPACT2:
1068 case EM_AVR:
1069 case EM_AVR_OLD:
1070 case EM_BLACKFIN:
1071 case EM_CR16:
1072 case EM_CRIS:
1073 case EM_CRX:
1074 case EM_CSKY:
1075 case EM_D30V:
1076 case EM_CYGNUS_D30V:
1077 case EM_FR30:
1078 case EM_FT32:
1079 case EM_CYGNUS_FR30:
1080 case EM_CYGNUS_FRV:
1081 case EM_H8S:
1082 case EM_H8_300:
1083 case EM_H8_300H:
1084 case EM_IA_64:
1085 case EM_IP2K:
1086 case EM_IP2K_OLD:
1087 case EM_IQ2000:
1088 case EM_LATTICEMICO32:
1089 case EM_M32C_OLD:
1090 case EM_M32C:
1091 case EM_M32R:
1092 case EM_MCORE:
1093 case EM_CYGNUS_MEP:
1094 case EM_METAG:
1095 case EM_MMIX:
1096 case EM_MN10200:
1097 case EM_CYGNUS_MN10200:
1098 case EM_MN10300:
1099 case EM_CYGNUS_MN10300:
1100 case EM_MOXIE:
1101 case EM_MSP430:
1102 case EM_MSP430_OLD:
1103 case EM_MT:
1104 case EM_NDS32:
1105 case EM_NIOS32:
1106 case EM_OR1K:
1107 case EM_PPC64:
1108 case EM_PPC:
1109 case EM_TI_PRU:
1110 case EM_RISCV:
1111 case EM_RL78:
1112 case EM_RX:
1113 case EM_S390:
1114 case EM_S390_OLD:
1115 case EM_SH:
1116 case EM_SPARC:
1117 case EM_SPARC32PLUS:
1118 case EM_SPARCV9:
1119 case EM_SPU:
1120 case EM_TI_C6000:
1121 case EM_TILEGX:
1122 case EM_TILEPRO:
1123 case EM_V800:
1124 case EM_V850:
1125 case EM_CYGNUS_V850:
1126 case EM_VAX:
1127 case EM_VISIUM:
1128 case EM_X86_64:
1129 case EM_L1OM:
1130 case EM_K1OM:
1131 case EM_XSTORMY16:
1132 case EM_XTENSA:
1133 case EM_XTENSA_OLD:
1134 case EM_MICROBLAZE:
1135 case EM_MICROBLAZE_OLD:
1136 case EM_WEBASSEMBLY:
1137 return true;
1138
1139 case EM_68HC05:
1140 case EM_68HC08:
1141 case EM_68HC11:
1142 case EM_68HC16:
1143 case EM_FX66:
1144 case EM_ME16:
1145 case EM_MMA:
1146 case EM_NCPU:
1147 case EM_NDR1:
1148 case EM_PCP:
1149 case EM_ST100:
1150 case EM_ST19:
1151 case EM_ST7:
1152 case EM_ST9PLUS:
1153 case EM_STARCORE:
1154 case EM_SVX:
1155 case EM_TINYJ:
1156 default:
1157 warn (_("Don't know about relocations on this machine architecture\n"));
1158 return false;
1159 }
1160 }
1161
1162 /* Load RELA type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1163 Returns TRUE upon success, FALSE otherwise. If successful then a
1164 pointer to a malloc'ed buffer containing the relocs is placed in *RELASP,
1165 and the number of relocs loaded is placed in *NRELASP. It is the caller's
1166 responsibility to free the allocated buffer. */
1167
1168 static bool
1169 slurp_rela_relocs (Filedata * filedata,
1170 unsigned long rel_offset,
1171 unsigned long rel_size,
1172 Elf_Internal_Rela ** relasp,
1173 unsigned long * nrelasp)
1174 {
1175 Elf_Internal_Rela * relas;
1176 size_t nrelas;
1177 unsigned int i;
1178
1179 if (is_32bit_elf)
1180 {
1181 Elf32_External_Rela * erelas;
1182
1183 erelas = (Elf32_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1184 rel_size, _("32-bit relocation data"));
1185 if (!erelas)
1186 return false;
1187
1188 nrelas = rel_size / sizeof (Elf32_External_Rela);
1189
1190 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1191 sizeof (Elf_Internal_Rela));
1192
1193 if (relas == NULL)
1194 {
1195 free (erelas);
1196 error (_("out of memory parsing relocs\n"));
1197 return false;
1198 }
1199
1200 for (i = 0; i < nrelas; i++)
1201 {
1202 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1203 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1204 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1205 }
1206
1207 free (erelas);
1208 }
1209 else
1210 {
1211 Elf64_External_Rela * erelas;
1212
1213 erelas = (Elf64_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1214 rel_size, _("64-bit relocation data"));
1215 if (!erelas)
1216 return false;
1217
1218 nrelas = rel_size / sizeof (Elf64_External_Rela);
1219
1220 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1221 sizeof (Elf_Internal_Rela));
1222
1223 if (relas == NULL)
1224 {
1225 free (erelas);
1226 error (_("out of memory parsing relocs\n"));
1227 return false;
1228 }
1229
1230 for (i = 0; i < nrelas; i++)
1231 {
1232 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1233 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1234 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1235
1236 /* The #ifdef BFD64 below is to prevent a compile time
1237 warning. We know that if we do not have a 64 bit data
1238 type that we will never execute this code anyway. */
1239 #ifdef BFD64
1240 if (filedata->file_header.e_machine == EM_MIPS
1241 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1242 {
1243 /* In little-endian objects, r_info isn't really a
1244 64-bit little-endian value: it has a 32-bit
1245 little-endian symbol index followed by four
1246 individual byte fields. Reorder INFO
1247 accordingly. */
1248 bfd_vma inf = relas[i].r_info;
1249 inf = (((inf & 0xffffffff) << 32)
1250 | ((inf >> 56) & 0xff)
1251 | ((inf >> 40) & 0xff00)
1252 | ((inf >> 24) & 0xff0000)
1253 | ((inf >> 8) & 0xff000000));
1254 relas[i].r_info = inf;
1255 }
1256 #endif /* BFD64 */
1257 }
1258
1259 free (erelas);
1260 }
1261
1262 *relasp = relas;
1263 *nrelasp = nrelas;
1264 return true;
1265 }
1266
1267 /* Load REL type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1268 Returns TRUE upon success, FALSE otherwise. If successful then a
1269 pointer to a malloc'ed buffer containing the relocs is placed in *RELSP,
1270 and the number of relocs loaded is placed in *NRELSP. It is the caller's
1271 responsibility to free the allocated buffer. */
1272
1273 static bool
1274 slurp_rel_relocs (Filedata * filedata,
1275 unsigned long rel_offset,
1276 unsigned long rel_size,
1277 Elf_Internal_Rela ** relsp,
1278 unsigned long * nrelsp)
1279 {
1280 Elf_Internal_Rela * rels;
1281 size_t nrels;
1282 unsigned int i;
1283
1284 if (is_32bit_elf)
1285 {
1286 Elf32_External_Rel * erels;
1287
1288 erels = (Elf32_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1289 rel_size, _("32-bit relocation data"));
1290 if (!erels)
1291 return false;
1292
1293 nrels = rel_size / sizeof (Elf32_External_Rel);
1294
1295 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1296
1297 if (rels == NULL)
1298 {
1299 free (erels);
1300 error (_("out of memory parsing relocs\n"));
1301 return false;
1302 }
1303
1304 for (i = 0; i < nrels; i++)
1305 {
1306 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1307 rels[i].r_info = BYTE_GET (erels[i].r_info);
1308 rels[i].r_addend = 0;
1309 }
1310
1311 free (erels);
1312 }
1313 else
1314 {
1315 Elf64_External_Rel * erels;
1316
1317 erels = (Elf64_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1318 rel_size, _("64-bit relocation data"));
1319 if (!erels)
1320 return false;
1321
1322 nrels = rel_size / sizeof (Elf64_External_Rel);
1323
1324 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1325
1326 if (rels == NULL)
1327 {
1328 free (erels);
1329 error (_("out of memory parsing relocs\n"));
1330 return false;
1331 }
1332
1333 for (i = 0; i < nrels; i++)
1334 {
1335 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1336 rels[i].r_info = BYTE_GET (erels[i].r_info);
1337 rels[i].r_addend = 0;
1338
1339 /* The #ifdef BFD64 below is to prevent a compile time
1340 warning. We know that if we do not have a 64 bit data
1341 type that we will never execute this code anyway. */
1342 #ifdef BFD64
1343 if (filedata->file_header.e_machine == EM_MIPS
1344 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1345 {
1346 /* In little-endian objects, r_info isn't really a
1347 64-bit little-endian value: it has a 32-bit
1348 little-endian symbol index followed by four
1349 individual byte fields. Reorder INFO
1350 accordingly. */
1351 bfd_vma inf = rels[i].r_info;
1352 inf = (((inf & 0xffffffff) << 32)
1353 | ((inf >> 56) & 0xff)
1354 | ((inf >> 40) & 0xff00)
1355 | ((inf >> 24) & 0xff0000)
1356 | ((inf >> 8) & 0xff000000));
1357 rels[i].r_info = inf;
1358 }
1359 #endif /* BFD64 */
1360 }
1361
1362 free (erels);
1363 }
1364
1365 *relsp = rels;
1366 *nrelsp = nrels;
1367 return true;
1368 }
1369
1370 static bool
1371 slurp_relr_relocs (Filedata * filedata,
1372 unsigned long relr_offset,
1373 unsigned long relr_size,
1374 bfd_vma ** relrsp,
1375 unsigned long * nrelrsp)
1376 {
1377 void *relrs;
1378 size_t size = 0, nentries, i;
1379 bfd_vma base = 0, addr, entry;
1380
1381 relrs = get_data (NULL, filedata, relr_offset, 1, relr_size,
1382 _("RELR relocation data"));
1383 if (!relrs)
1384 return false;
1385
1386 if (is_32bit_elf)
1387 nentries = relr_size / sizeof (Elf32_External_Relr);
1388 else
1389 nentries = relr_size / sizeof (Elf64_External_Relr);
1390 for (i = 0; i < nentries; i++)
1391 {
1392 if (is_32bit_elf)
1393 entry = BYTE_GET (((Elf32_External_Relr *)relrs)[i].r_data);
1394 else
1395 entry = BYTE_GET (((Elf64_External_Relr *)relrs)[i].r_data);
1396 if ((entry & 1) == 0)
1397 size++;
1398 else
1399 while ((entry >>= 1) != 0)
1400 if ((entry & 1) == 1)
1401 size++;
1402 }
1403
1404 *relrsp = (bfd_vma *) xmalloc (size * sizeof (bfd_vma));
1405 if (*relrsp == NULL)
1406 {
1407 free (relrs);
1408 error (_("out of memory parsing relocs\n"));
1409 return false;
1410 }
1411
1412 size = 0;
1413 for (i = 0; i < nentries; i++)
1414 {
1415 const bfd_vma entry_bytes = is_32bit_elf ? 4 : 8;
1416
1417 if (is_32bit_elf)
1418 entry = BYTE_GET (((Elf32_External_Relr *)relrs)[i].r_data);
1419 else
1420 entry = BYTE_GET (((Elf64_External_Relr *)relrs)[i].r_data);
1421 if ((entry & 1) == 0)
1422 {
1423 (*relrsp)[size++] = entry;
1424 base = entry + entry_bytes;
1425 }
1426 else
1427 {
1428 for (addr = base; (entry >>= 1) != 0; addr += entry_bytes)
1429 if ((entry & 1) != 0)
1430 (*relrsp)[size++] = addr;
1431 base += entry_bytes * (entry_bytes * CHAR_BIT - 1);
1432 }
1433 }
1434
1435 *nrelrsp = size;
1436 free (relrs);
1437 return true;
1438 }
1439
1440 /* Returns the reloc type extracted from the reloc info field. */
1441
1442 static unsigned int
1443 get_reloc_type (Filedata * filedata, bfd_vma reloc_info)
1444 {
1445 if (is_32bit_elf)
1446 return ELF32_R_TYPE (reloc_info);
1447
1448 switch (filedata->file_header.e_machine)
1449 {
1450 case EM_MIPS:
1451 /* Note: We assume that reloc_info has already been adjusted for us. */
1452 return ELF64_MIPS_R_TYPE (reloc_info);
1453
1454 case EM_SPARCV9:
1455 return ELF64_R_TYPE_ID (reloc_info);
1456
1457 default:
1458 return ELF64_R_TYPE (reloc_info);
1459 }
1460 }
1461
1462 /* Return the symbol index extracted from the reloc info field. */
1463
1464 static bfd_vma
1465 get_reloc_symindex (bfd_vma reloc_info)
1466 {
1467 return is_32bit_elf ? ELF32_R_SYM (reloc_info) : ELF64_R_SYM (reloc_info);
1468 }
1469
1470 static inline bool
1471 uses_msp430x_relocs (Filedata * filedata)
1472 {
1473 return
1474 filedata->file_header.e_machine == EM_MSP430 /* Paranoia. */
1475 /* GCC uses osabi == ELFOSBI_STANDALONE. */
1476 && (((filedata->file_header.e_flags & EF_MSP430_MACH) == E_MSP430_MACH_MSP430X)
1477 /* TI compiler uses ELFOSABI_NONE. */
1478 || (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_NONE));
1479 }
1480
1481 /* Display the contents of the relocation data found at the specified
1482 offset. */
1483
1484 static bool
1485 dump_relocations (Filedata * filedata,
1486 unsigned long rel_offset,
1487 unsigned long rel_size,
1488 Elf_Internal_Sym * symtab,
1489 unsigned long nsyms,
1490 char * strtab,
1491 unsigned long strtablen,
1492 relocation_type rel_type,
1493 bool is_dynsym)
1494 {
1495 unsigned long i;
1496 Elf_Internal_Rela * rels;
1497 bool res = true;
1498
1499 if (rel_type == reltype_unknown)
1500 rel_type = guess_is_rela (filedata->file_header.e_machine) ? reltype_rela : reltype_rel;
1501
1502 if (rel_type == reltype_rela)
1503 {
1504 if (!slurp_rela_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1505 return false;
1506 }
1507 else if (rel_type == reltype_rel)
1508 {
1509 if (!slurp_rel_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1510 return false;
1511 }
1512 else if (rel_type == reltype_relr)
1513 {
1514 bfd_vma * relrs;
1515 const char *format
1516 = is_32bit_elf ? "%08" BFD_VMA_FMT "x\n" : "%016" BFD_VMA_FMT "x\n";
1517
1518 if (!slurp_relr_relocs (filedata, rel_offset, rel_size, &relrs,
1519 &rel_size))
1520 return false;
1521
1522 printf (ngettext (" %lu offset\n", " %lu offsets\n", rel_size), rel_size);
1523 for (i = 0; i < rel_size; i++)
1524 printf (format, relrs[i]);
1525 free (relrs);
1526 return true;
1527 }
1528
1529 if (is_32bit_elf)
1530 {
1531 if (rel_type == reltype_rela)
1532 {
1533 if (do_wide)
1534 printf (_(" Offset Info Type Sym. Value Symbol's Name + Addend\n"));
1535 else
1536 printf (_(" Offset Info Type Sym.Value Sym. Name + Addend\n"));
1537 }
1538 else
1539 {
1540 if (do_wide)
1541 printf (_(" Offset Info Type Sym. Value Symbol's Name\n"));
1542 else
1543 printf (_(" Offset Info Type Sym.Value Sym. Name\n"));
1544 }
1545 }
1546 else
1547 {
1548 if (rel_type == reltype_rela)
1549 {
1550 if (do_wide)
1551 printf (_(" Offset Info Type Symbol's Value Symbol's Name + Addend\n"));
1552 else
1553 printf (_(" Offset Info Type Sym. Value Sym. Name + Addend\n"));
1554 }
1555 else
1556 {
1557 if (do_wide)
1558 printf (_(" Offset Info Type Symbol's Value Symbol's Name\n"));
1559 else
1560 printf (_(" Offset Info Type Sym. Value Sym. Name\n"));
1561 }
1562 }
1563
1564 for (i = 0; i < rel_size; i++)
1565 {
1566 const char * rtype;
1567 bfd_vma offset;
1568 bfd_vma inf;
1569 bfd_vma symtab_index;
1570 bfd_vma type;
1571
1572 offset = rels[i].r_offset;
1573 inf = rels[i].r_info;
1574
1575 type = get_reloc_type (filedata, inf);
1576 symtab_index = get_reloc_symindex (inf);
1577
1578 if (is_32bit_elf)
1579 {
1580 printf ("%8.8lx %8.8lx ",
1581 (unsigned long) offset & 0xffffffff,
1582 (unsigned long) inf & 0xffffffff);
1583 }
1584 else
1585 {
1586 printf (do_wide
1587 ? "%16.16" BFD_VMA_FMT "x %16.16" BFD_VMA_FMT "x "
1588 : "%12.12" BFD_VMA_FMT "x %12.12" BFD_VMA_FMT "x ",
1589 offset, inf);
1590 }
1591
1592 switch (filedata->file_header.e_machine)
1593 {
1594 default:
1595 rtype = NULL;
1596 break;
1597
1598 case EM_AARCH64:
1599 rtype = elf_aarch64_reloc_type (type);
1600 break;
1601
1602 case EM_M32R:
1603 case EM_CYGNUS_M32R:
1604 rtype = elf_m32r_reloc_type (type);
1605 break;
1606
1607 case EM_386:
1608 case EM_IAMCU:
1609 rtype = elf_i386_reloc_type (type);
1610 break;
1611
1612 case EM_68HC11:
1613 case EM_68HC12:
1614 rtype = elf_m68hc11_reloc_type (type);
1615 break;
1616
1617 case EM_S12Z:
1618 rtype = elf_s12z_reloc_type (type);
1619 break;
1620
1621 case EM_68K:
1622 rtype = elf_m68k_reloc_type (type);
1623 break;
1624
1625 case EM_960:
1626 rtype = elf_i960_reloc_type (type);
1627 break;
1628
1629 case EM_AVR:
1630 case EM_AVR_OLD:
1631 rtype = elf_avr_reloc_type (type);
1632 break;
1633
1634 case EM_OLD_SPARCV9:
1635 case EM_SPARC32PLUS:
1636 case EM_SPARCV9:
1637 case EM_SPARC:
1638 rtype = elf_sparc_reloc_type (type);
1639 break;
1640
1641 case EM_SPU:
1642 rtype = elf_spu_reloc_type (type);
1643 break;
1644
1645 case EM_V800:
1646 rtype = v800_reloc_type (type);
1647 break;
1648 case EM_V850:
1649 case EM_CYGNUS_V850:
1650 rtype = v850_reloc_type (type);
1651 break;
1652
1653 case EM_D10V:
1654 case EM_CYGNUS_D10V:
1655 rtype = elf_d10v_reloc_type (type);
1656 break;
1657
1658 case EM_D30V:
1659 case EM_CYGNUS_D30V:
1660 rtype = elf_d30v_reloc_type (type);
1661 break;
1662
1663 case EM_DLX:
1664 rtype = elf_dlx_reloc_type (type);
1665 break;
1666
1667 case EM_SH:
1668 rtype = elf_sh_reloc_type (type);
1669 break;
1670
1671 case EM_MN10300:
1672 case EM_CYGNUS_MN10300:
1673 rtype = elf_mn10300_reloc_type (type);
1674 break;
1675
1676 case EM_MN10200:
1677 case EM_CYGNUS_MN10200:
1678 rtype = elf_mn10200_reloc_type (type);
1679 break;
1680
1681 case EM_FR30:
1682 case EM_CYGNUS_FR30:
1683 rtype = elf_fr30_reloc_type (type);
1684 break;
1685
1686 case EM_CYGNUS_FRV:
1687 rtype = elf_frv_reloc_type (type);
1688 break;
1689
1690 case EM_CSKY:
1691 rtype = elf_csky_reloc_type (type);
1692 break;
1693
1694 case EM_FT32:
1695 rtype = elf_ft32_reloc_type (type);
1696 break;
1697
1698 case EM_MCORE:
1699 rtype = elf_mcore_reloc_type (type);
1700 break;
1701
1702 case EM_MMIX:
1703 rtype = elf_mmix_reloc_type (type);
1704 break;
1705
1706 case EM_MOXIE:
1707 rtype = elf_moxie_reloc_type (type);
1708 break;
1709
1710 case EM_MSP430:
1711 if (uses_msp430x_relocs (filedata))
1712 {
1713 rtype = elf_msp430x_reloc_type (type);
1714 break;
1715 }
1716 /* Fall through. */
1717 case EM_MSP430_OLD:
1718 rtype = elf_msp430_reloc_type (type);
1719 break;
1720
1721 case EM_NDS32:
1722 rtype = elf_nds32_reloc_type (type);
1723 break;
1724
1725 case EM_PPC:
1726 rtype = elf_ppc_reloc_type (type);
1727 break;
1728
1729 case EM_PPC64:
1730 rtype = elf_ppc64_reloc_type (type);
1731 break;
1732
1733 case EM_MIPS:
1734 case EM_MIPS_RS3_LE:
1735 rtype = elf_mips_reloc_type (type);
1736 break;
1737
1738 case EM_RISCV:
1739 rtype = elf_riscv_reloc_type (type);
1740 break;
1741
1742 case EM_ALPHA:
1743 rtype = elf_alpha_reloc_type (type);
1744 break;
1745
1746 case EM_ARM:
1747 rtype = elf_arm_reloc_type (type);
1748 break;
1749
1750 case EM_ARC:
1751 case EM_ARC_COMPACT:
1752 case EM_ARC_COMPACT2:
1753 rtype = elf_arc_reloc_type (type);
1754 break;
1755
1756 case EM_PARISC:
1757 rtype = elf_hppa_reloc_type (type);
1758 break;
1759
1760 case EM_H8_300:
1761 case EM_H8_300H:
1762 case EM_H8S:
1763 rtype = elf_h8_reloc_type (type);
1764 break;
1765
1766 case EM_OR1K:
1767 rtype = elf_or1k_reloc_type (type);
1768 break;
1769
1770 case EM_PJ:
1771 case EM_PJ_OLD:
1772 rtype = elf_pj_reloc_type (type);
1773 break;
1774 case EM_IA_64:
1775 rtype = elf_ia64_reloc_type (type);
1776 break;
1777
1778 case EM_CRIS:
1779 rtype = elf_cris_reloc_type (type);
1780 break;
1781
1782 case EM_860:
1783 rtype = elf_i860_reloc_type (type);
1784 break;
1785
1786 case EM_X86_64:
1787 case EM_L1OM:
1788 case EM_K1OM:
1789 rtype = elf_x86_64_reloc_type (type);
1790 break;
1791
1792 case EM_S370:
1793 rtype = i370_reloc_type (type);
1794 break;
1795
1796 case EM_S390_OLD:
1797 case EM_S390:
1798 rtype = elf_s390_reloc_type (type);
1799 break;
1800
1801 case EM_SCORE:
1802 rtype = elf_score_reloc_type (type);
1803 break;
1804
1805 case EM_XSTORMY16:
1806 rtype = elf_xstormy16_reloc_type (type);
1807 break;
1808
1809 case EM_CRX:
1810 rtype = elf_crx_reloc_type (type);
1811 break;
1812
1813 case EM_VAX:
1814 rtype = elf_vax_reloc_type (type);
1815 break;
1816
1817 case EM_VISIUM:
1818 rtype = elf_visium_reloc_type (type);
1819 break;
1820
1821 case EM_BPF:
1822 rtype = elf_bpf_reloc_type (type);
1823 break;
1824
1825 case EM_ADAPTEVA_EPIPHANY:
1826 rtype = elf_epiphany_reloc_type (type);
1827 break;
1828
1829 case EM_IP2K:
1830 case EM_IP2K_OLD:
1831 rtype = elf_ip2k_reloc_type (type);
1832 break;
1833
1834 case EM_IQ2000:
1835 rtype = elf_iq2000_reloc_type (type);
1836 break;
1837
1838 case EM_XTENSA_OLD:
1839 case EM_XTENSA:
1840 rtype = elf_xtensa_reloc_type (type);
1841 break;
1842
1843 case EM_LATTICEMICO32:
1844 rtype = elf_lm32_reloc_type (type);
1845 break;
1846
1847 case EM_M32C_OLD:
1848 case EM_M32C:
1849 rtype = elf_m32c_reloc_type (type);
1850 break;
1851
1852 case EM_MT:
1853 rtype = elf_mt_reloc_type (type);
1854 break;
1855
1856 case EM_BLACKFIN:
1857 rtype = elf_bfin_reloc_type (type);
1858 break;
1859
1860 case EM_CYGNUS_MEP:
1861 rtype = elf_mep_reloc_type (type);
1862 break;
1863
1864 case EM_CR16:
1865 rtype = elf_cr16_reloc_type (type);
1866 break;
1867
1868 case EM_MICROBLAZE:
1869 case EM_MICROBLAZE_OLD:
1870 rtype = elf_microblaze_reloc_type (type);
1871 break;
1872
1873 case EM_RL78:
1874 rtype = elf_rl78_reloc_type (type);
1875 break;
1876
1877 case EM_RX:
1878 rtype = elf_rx_reloc_type (type);
1879 break;
1880
1881 case EM_METAG:
1882 rtype = elf_metag_reloc_type (type);
1883 break;
1884
1885 case EM_XC16X:
1886 case EM_C166:
1887 rtype = elf_xc16x_reloc_type (type);
1888 break;
1889
1890 case EM_TI_C6000:
1891 rtype = elf_tic6x_reloc_type (type);
1892 break;
1893
1894 case EM_TILEGX:
1895 rtype = elf_tilegx_reloc_type (type);
1896 break;
1897
1898 case EM_TILEPRO:
1899 rtype = elf_tilepro_reloc_type (type);
1900 break;
1901
1902 case EM_WEBASSEMBLY:
1903 rtype = elf_wasm32_reloc_type (type);
1904 break;
1905
1906 case EM_XGATE:
1907 rtype = elf_xgate_reloc_type (type);
1908 break;
1909
1910 case EM_ALTERA_NIOS2:
1911 rtype = elf_nios2_reloc_type (type);
1912 break;
1913
1914 case EM_TI_PRU:
1915 rtype = elf_pru_reloc_type (type);
1916 break;
1917
1918 case EM_NFP:
1919 if (EF_NFP_MACH (filedata->file_header.e_flags) == E_NFP_MACH_3200)
1920 rtype = elf_nfp3200_reloc_type (type);
1921 else
1922 rtype = elf_nfp_reloc_type (type);
1923 break;
1924
1925 case EM_Z80:
1926 rtype = elf_z80_reloc_type (type);
1927 break;
1928
1929 case EM_LOONGARCH:
1930 rtype = elf_loongarch_reloc_type (type);
1931 break;
1932
1933 case EM_AMDGPU:
1934 rtype = elf_amdgpu_reloc_type (type);
1935 break;
1936 }
1937
1938 if (rtype == NULL)
1939 printf (_("unrecognized: %-7lx"), (unsigned long) type & 0xffffffff);
1940 else
1941 printf (do_wide ? "%-22s" : "%-17.17s", rtype);
1942
1943 if (filedata->file_header.e_machine == EM_ALPHA
1944 && rtype != NULL
1945 && streq (rtype, "R_ALPHA_LITUSE")
1946 && rel_type == reltype_rela)
1947 {
1948 switch (rels[i].r_addend)
1949 {
1950 case LITUSE_ALPHA_ADDR: rtype = "ADDR"; break;
1951 case LITUSE_ALPHA_BASE: rtype = "BASE"; break;
1952 case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
1953 case LITUSE_ALPHA_JSR: rtype = "JSR"; break;
1954 case LITUSE_ALPHA_TLSGD: rtype = "TLSGD"; break;
1955 case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
1956 case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
1957 default: rtype = NULL;
1958 }
1959
1960 if (rtype)
1961 printf (" (%s)", rtype);
1962 else
1963 {
1964 putchar (' ');
1965 printf (_("<unknown addend: %lx>"),
1966 (unsigned long) rels[i].r_addend);
1967 res = false;
1968 }
1969 }
1970 else if (symtab_index)
1971 {
1972 if (symtab == NULL || symtab_index >= nsyms)
1973 {
1974 error (_(" bad symbol index: %08lx in reloc\n"),
1975 (unsigned long) symtab_index);
1976 res = false;
1977 }
1978 else
1979 {
1980 Elf_Internal_Sym * psym;
1981 const char * version_string;
1982 enum versioned_symbol_info sym_info;
1983 unsigned short vna_other;
1984
1985 psym = symtab + symtab_index;
1986
1987 version_string
1988 = get_symbol_version_string (filedata, is_dynsym,
1989 strtab, strtablen,
1990 symtab_index,
1991 psym,
1992 &sym_info,
1993 &vna_other);
1994
1995 printf (" ");
1996
1997 if (ELF_ST_TYPE (psym->st_info) == STT_GNU_IFUNC)
1998 {
1999 const char * name;
2000 unsigned int len;
2001 unsigned int width = is_32bit_elf ? 8 : 14;
2002
2003 /* Relocations against GNU_IFUNC symbols do not use the value
2004 of the symbol as the address to relocate against. Instead
2005 they invoke the function named by the symbol and use its
2006 result as the address for relocation.
2007
2008 To indicate this to the user, do not display the value of
2009 the symbol in the "Symbols's Value" field. Instead show
2010 its name followed by () as a hint that the symbol is
2011 invoked. */
2012
2013 if (strtab == NULL
2014 || psym->st_name == 0
2015 || psym->st_name >= strtablen)
2016 name = "??";
2017 else
2018 name = strtab + psym->st_name;
2019
2020 len = print_symbol (width, name);
2021 if (version_string)
2022 printf (sym_info == symbol_public ? "@@%s" : "@%s",
2023 version_string);
2024 printf ("()%-*s", len <= width ? (width + 1) - len : 1, " ");
2025 }
2026 else
2027 {
2028 print_vma (psym->st_value, LONG_HEX);
2029
2030 printf (is_32bit_elf ? " " : " ");
2031 }
2032
2033 if (psym->st_name == 0)
2034 {
2035 const char * sec_name = "<null>";
2036 char name_buf[40];
2037
2038 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
2039 {
2040 if (psym->st_shndx < filedata->file_header.e_shnum
2041 && filedata->section_headers != NULL)
2042 sec_name = section_name_print (filedata,
2043 filedata->section_headers
2044 + psym->st_shndx);
2045 else if (psym->st_shndx == SHN_ABS)
2046 sec_name = "ABS";
2047 else if (psym->st_shndx == SHN_COMMON)
2048 sec_name = "COMMON";
2049 else if ((filedata->file_header.e_machine == EM_MIPS
2050 && psym->st_shndx == SHN_MIPS_SCOMMON)
2051 || (filedata->file_header.e_machine == EM_TI_C6000
2052 && psym->st_shndx == SHN_TIC6X_SCOMMON))
2053 sec_name = "SCOMMON";
2054 else if (filedata->file_header.e_machine == EM_MIPS
2055 && psym->st_shndx == SHN_MIPS_SUNDEFINED)
2056 sec_name = "SUNDEF";
2057 else if ((filedata->file_header.e_machine == EM_X86_64
2058 || filedata->file_header.e_machine == EM_L1OM
2059 || filedata->file_header.e_machine == EM_K1OM)
2060 && psym->st_shndx == SHN_X86_64_LCOMMON)
2061 sec_name = "LARGE_COMMON";
2062 else if (filedata->file_header.e_machine == EM_IA_64
2063 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
2064 && psym->st_shndx == SHN_IA_64_ANSI_COMMON)
2065 sec_name = "ANSI_COM";
2066 else if (is_ia64_vms (filedata)
2067 && psym->st_shndx == SHN_IA_64_VMS_SYMVEC)
2068 sec_name = "VMS_SYMVEC";
2069 else
2070 {
2071 sprintf (name_buf, "<section 0x%x>",
2072 (unsigned int) psym->st_shndx);
2073 sec_name = name_buf;
2074 }
2075 }
2076 print_symbol (22, sec_name);
2077 }
2078 else if (strtab == NULL)
2079 printf (_("<string table index: %3ld>"), psym->st_name);
2080 else if (psym->st_name >= strtablen)
2081 {
2082 error (_("<corrupt string table index: %3ld>\n"),
2083 psym->st_name);
2084 res = false;
2085 }
2086 else
2087 {
2088 print_symbol (22, strtab + psym->st_name);
2089 if (version_string)
2090 printf (sym_info == symbol_public ? "@@%s" : "@%s",
2091 version_string);
2092 }
2093
2094 if (rel_type == reltype_rela)
2095 {
2096 bfd_vma off = rels[i].r_addend;
2097
2098 if ((bfd_signed_vma) off < 0)
2099 printf (" - %" BFD_VMA_FMT "x", - off);
2100 else
2101 printf (" + %" BFD_VMA_FMT "x", off);
2102 }
2103 }
2104 }
2105 else if (rel_type == reltype_rela)
2106 {
2107 bfd_vma off = rels[i].r_addend;
2108
2109 printf ("%*c", is_32bit_elf ? 12 : 20, ' ');
2110 if ((bfd_signed_vma) off < 0)
2111 printf ("-%" BFD_VMA_FMT "x", - off);
2112 else
2113 printf ("%" BFD_VMA_FMT "x", off);
2114 }
2115
2116 if (filedata->file_header.e_machine == EM_SPARCV9
2117 && rtype != NULL
2118 && streq (rtype, "R_SPARC_OLO10"))
2119 printf (" + %lx", (unsigned long) ELF64_R_TYPE_DATA (inf));
2120
2121 putchar ('\n');
2122
2123 #ifdef BFD64
2124 if (! is_32bit_elf && filedata->file_header.e_machine == EM_MIPS)
2125 {
2126 bfd_vma type2 = ELF64_MIPS_R_TYPE2 (inf);
2127 bfd_vma type3 = ELF64_MIPS_R_TYPE3 (inf);
2128 const char * rtype2 = elf_mips_reloc_type (type2);
2129 const char * rtype3 = elf_mips_reloc_type (type3);
2130
2131 printf (" Type2: ");
2132
2133 if (rtype2 == NULL)
2134 printf (_("unrecognized: %-7lx"),
2135 (unsigned long) type2 & 0xffffffff);
2136 else
2137 printf ("%-17.17s", rtype2);
2138
2139 printf ("\n Type3: ");
2140
2141 if (rtype3 == NULL)
2142 printf (_("unrecognized: %-7lx"),
2143 (unsigned long) type3 & 0xffffffff);
2144 else
2145 printf ("%-17.17s", rtype3);
2146
2147 putchar ('\n');
2148 }
2149 #endif /* BFD64 */
2150 }
2151
2152 free (rels);
2153
2154 return res;
2155 }
2156
2157 static const char *
2158 get_aarch64_dynamic_type (unsigned long type)
2159 {
2160 switch (type)
2161 {
2162 case DT_AARCH64_BTI_PLT: return "AARCH64_BTI_PLT";
2163 case DT_AARCH64_PAC_PLT: return "AARCH64_PAC_PLT";
2164 case DT_AARCH64_VARIANT_PCS: return "AARCH64_VARIANT_PCS";
2165 default:
2166 return NULL;
2167 }
2168 }
2169
2170 static const char *
2171 get_mips_dynamic_type (unsigned long type)
2172 {
2173 switch (type)
2174 {
2175 case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
2176 case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
2177 case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
2178 case DT_MIPS_IVERSION: return "MIPS_IVERSION";
2179 case DT_MIPS_FLAGS: return "MIPS_FLAGS";
2180 case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
2181 case DT_MIPS_MSYM: return "MIPS_MSYM";
2182 case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
2183 case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
2184 case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
2185 case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
2186 case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
2187 case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
2188 case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
2189 case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
2190 case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
2191 case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
2192 case DT_MIPS_RLD_MAP_REL: return "MIPS_RLD_MAP_REL";
2193 case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
2194 case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
2195 case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
2196 case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
2197 case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
2198 case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
2199 case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
2200 case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
2201 case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
2202 case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
2203 case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
2204 case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
2205 case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
2206 case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
2207 case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
2208 case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
2209 case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
2210 case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
2211 case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
2212 case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
2213 case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
2214 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
2215 case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
2216 case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
2217 case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
2218 case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
2219 case DT_MIPS_PLTGOT: return "MIPS_PLTGOT";
2220 case DT_MIPS_RWPLT: return "MIPS_RWPLT";
2221 case DT_MIPS_XHASH: return "MIPS_XHASH";
2222 default:
2223 return NULL;
2224 }
2225 }
2226
2227 static const char *
2228 get_sparc64_dynamic_type (unsigned long type)
2229 {
2230 switch (type)
2231 {
2232 case DT_SPARC_REGISTER: return "SPARC_REGISTER";
2233 default:
2234 return NULL;
2235 }
2236 }
2237
2238 static const char *
2239 get_ppc_dynamic_type (unsigned long type)
2240 {
2241 switch (type)
2242 {
2243 case DT_PPC_GOT: return "PPC_GOT";
2244 case DT_PPC_OPT: return "PPC_OPT";
2245 default:
2246 return NULL;
2247 }
2248 }
2249
2250 static const char *
2251 get_ppc64_dynamic_type (unsigned long type)
2252 {
2253 switch (type)
2254 {
2255 case DT_PPC64_GLINK: return "PPC64_GLINK";
2256 case DT_PPC64_OPD: return "PPC64_OPD";
2257 case DT_PPC64_OPDSZ: return "PPC64_OPDSZ";
2258 case DT_PPC64_OPT: return "PPC64_OPT";
2259 default:
2260 return NULL;
2261 }
2262 }
2263
2264 static const char *
2265 get_parisc_dynamic_type (unsigned long type)
2266 {
2267 switch (type)
2268 {
2269 case DT_HP_LOAD_MAP: return "HP_LOAD_MAP";
2270 case DT_HP_DLD_FLAGS: return "HP_DLD_FLAGS";
2271 case DT_HP_DLD_HOOK: return "HP_DLD_HOOK";
2272 case DT_HP_UX10_INIT: return "HP_UX10_INIT";
2273 case DT_HP_UX10_INITSZ: return "HP_UX10_INITSZ";
2274 case DT_HP_PREINIT: return "HP_PREINIT";
2275 case DT_HP_PREINITSZ: return "HP_PREINITSZ";
2276 case DT_HP_NEEDED: return "HP_NEEDED";
2277 case DT_HP_TIME_STAMP: return "HP_TIME_STAMP";
2278 case DT_HP_CHECKSUM: return "HP_CHECKSUM";
2279 case DT_HP_GST_SIZE: return "HP_GST_SIZE";
2280 case DT_HP_GST_VERSION: return "HP_GST_VERSION";
2281 case DT_HP_GST_HASHVAL: return "HP_GST_HASHVAL";
2282 case DT_HP_EPLTREL: return "HP_GST_EPLTREL";
2283 case DT_HP_EPLTRELSZ: return "HP_GST_EPLTRELSZ";
2284 case DT_HP_FILTERED: return "HP_FILTERED";
2285 case DT_HP_FILTER_TLS: return "HP_FILTER_TLS";
2286 case DT_HP_COMPAT_FILTERED: return "HP_COMPAT_FILTERED";
2287 case DT_HP_LAZYLOAD: return "HP_LAZYLOAD";
2288 case DT_HP_BIND_NOW_COUNT: return "HP_BIND_NOW_COUNT";
2289 case DT_PLT: return "PLT";
2290 case DT_PLT_SIZE: return "PLT_SIZE";
2291 case DT_DLT: return "DLT";
2292 case DT_DLT_SIZE: return "DLT_SIZE";
2293 default:
2294 return NULL;
2295 }
2296 }
2297
2298 static const char *
2299 get_ia64_dynamic_type (unsigned long type)
2300 {
2301 switch (type)
2302 {
2303 case DT_IA_64_PLT_RESERVE: return "IA_64_PLT_RESERVE";
2304 case DT_IA_64_VMS_SUBTYPE: return "VMS_SUBTYPE";
2305 case DT_IA_64_VMS_IMGIOCNT: return "VMS_IMGIOCNT";
2306 case DT_IA_64_VMS_LNKFLAGS: return "VMS_LNKFLAGS";
2307 case DT_IA_64_VMS_VIR_MEM_BLK_SIZ: return "VMS_VIR_MEM_BLK_SIZ";
2308 case DT_IA_64_VMS_IDENT: return "VMS_IDENT";
2309 case DT_IA_64_VMS_NEEDED_IDENT: return "VMS_NEEDED_IDENT";
2310 case DT_IA_64_VMS_IMG_RELA_CNT: return "VMS_IMG_RELA_CNT";
2311 case DT_IA_64_VMS_SEG_RELA_CNT: return "VMS_SEG_RELA_CNT";
2312 case DT_IA_64_VMS_FIXUP_RELA_CNT: return "VMS_FIXUP_RELA_CNT";
2313 case DT_IA_64_VMS_FIXUP_NEEDED: return "VMS_FIXUP_NEEDED";
2314 case DT_IA_64_VMS_SYMVEC_CNT: return "VMS_SYMVEC_CNT";
2315 case DT_IA_64_VMS_XLATED: return "VMS_XLATED";
2316 case DT_IA_64_VMS_STACKSIZE: return "VMS_STACKSIZE";
2317 case DT_IA_64_VMS_UNWINDSZ: return "VMS_UNWINDSZ";
2318 case DT_IA_64_VMS_UNWIND_CODSEG: return "VMS_UNWIND_CODSEG";
2319 case DT_IA_64_VMS_UNWIND_INFOSEG: return "VMS_UNWIND_INFOSEG";
2320 case DT_IA_64_VMS_LINKTIME: return "VMS_LINKTIME";
2321 case DT_IA_64_VMS_SEG_NO: return "VMS_SEG_NO";
2322 case DT_IA_64_VMS_SYMVEC_OFFSET: return "VMS_SYMVEC_OFFSET";
2323 case DT_IA_64_VMS_SYMVEC_SEG: return "VMS_SYMVEC_SEG";
2324 case DT_IA_64_VMS_UNWIND_OFFSET: return "VMS_UNWIND_OFFSET";
2325 case DT_IA_64_VMS_UNWIND_SEG: return "VMS_UNWIND_SEG";
2326 case DT_IA_64_VMS_STRTAB_OFFSET: return "VMS_STRTAB_OFFSET";
2327 case DT_IA_64_VMS_SYSVER_OFFSET: return "VMS_SYSVER_OFFSET";
2328 case DT_IA_64_VMS_IMG_RELA_OFF: return "VMS_IMG_RELA_OFF";
2329 case DT_IA_64_VMS_SEG_RELA_OFF: return "VMS_SEG_RELA_OFF";
2330 case DT_IA_64_VMS_FIXUP_RELA_OFF: return "VMS_FIXUP_RELA_OFF";
2331 case DT_IA_64_VMS_PLTGOT_OFFSET: return "VMS_PLTGOT_OFFSET";
2332 case DT_IA_64_VMS_PLTGOT_SEG: return "VMS_PLTGOT_SEG";
2333 case DT_IA_64_VMS_FPMODE: return "VMS_FPMODE";
2334 default:
2335 return NULL;
2336 }
2337 }
2338
2339 static const char *
2340 get_solaris_section_type (unsigned long type)
2341 {
2342 switch (type)
2343 {
2344 case 0x6fffffee: return "SUNW_ancillary";
2345 case 0x6fffffef: return "SUNW_capchain";
2346 case 0x6ffffff0: return "SUNW_capinfo";
2347 case 0x6ffffff1: return "SUNW_symsort";
2348 case 0x6ffffff2: return "SUNW_tlssort";
2349 case 0x6ffffff3: return "SUNW_LDYNSYM";
2350 case 0x6ffffff4: return "SUNW_dof";
2351 case 0x6ffffff5: return "SUNW_cap";
2352 case 0x6ffffff6: return "SUNW_SIGNATURE";
2353 case 0x6ffffff7: return "SUNW_ANNOTATE";
2354 case 0x6ffffff8: return "SUNW_DEBUGSTR";
2355 case 0x6ffffff9: return "SUNW_DEBUG";
2356 case 0x6ffffffa: return "SUNW_move";
2357 case 0x6ffffffb: return "SUNW_COMDAT";
2358 case 0x6ffffffc: return "SUNW_syminfo";
2359 case 0x6ffffffd: return "SUNW_verdef";
2360 case 0x6ffffffe: return "SUNW_verneed";
2361 case 0x6fffffff: return "SUNW_versym";
2362 case 0x70000000: return "SPARC_GOTDATA";
2363 default: return NULL;
2364 }
2365 }
2366
2367 static const char *
2368 get_alpha_dynamic_type (unsigned long type)
2369 {
2370 switch (type)
2371 {
2372 case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
2373 default: return NULL;
2374 }
2375 }
2376
2377 static const char *
2378 get_score_dynamic_type (unsigned long type)
2379 {
2380 switch (type)
2381 {
2382 case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
2383 case DT_SCORE_LOCAL_GOTNO: return "SCORE_LOCAL_GOTNO";
2384 case DT_SCORE_SYMTABNO: return "SCORE_SYMTABNO";
2385 case DT_SCORE_GOTSYM: return "SCORE_GOTSYM";
2386 case DT_SCORE_UNREFEXTNO: return "SCORE_UNREFEXTNO";
2387 case DT_SCORE_HIPAGENO: return "SCORE_HIPAGENO";
2388 default: return NULL;
2389 }
2390 }
2391
2392 static const char *
2393 get_tic6x_dynamic_type (unsigned long type)
2394 {
2395 switch (type)
2396 {
2397 case DT_C6000_GSYM_OFFSET: return "C6000_GSYM_OFFSET";
2398 case DT_C6000_GSTR_OFFSET: return "C6000_GSTR_OFFSET";
2399 case DT_C6000_DSBT_BASE: return "C6000_DSBT_BASE";
2400 case DT_C6000_DSBT_SIZE: return "C6000_DSBT_SIZE";
2401 case DT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
2402 case DT_C6000_DSBT_INDEX: return "C6000_DSBT_INDEX";
2403 default: return NULL;
2404 }
2405 }
2406
2407 static const char *
2408 get_nios2_dynamic_type (unsigned long type)
2409 {
2410 switch (type)
2411 {
2412 case DT_NIOS2_GP: return "NIOS2_GP";
2413 default: return NULL;
2414 }
2415 }
2416
2417 static const char *
2418 get_solaris_dynamic_type (unsigned long type)
2419 {
2420 switch (type)
2421 {
2422 case 0x6000000d: return "SUNW_AUXILIARY";
2423 case 0x6000000e: return "SUNW_RTLDINF";
2424 case 0x6000000f: return "SUNW_FILTER";
2425 case 0x60000010: return "SUNW_CAP";
2426 case 0x60000011: return "SUNW_SYMTAB";
2427 case 0x60000012: return "SUNW_SYMSZ";
2428 case 0x60000013: return "SUNW_SORTENT";
2429 case 0x60000014: return "SUNW_SYMSORT";
2430 case 0x60000015: return "SUNW_SYMSORTSZ";
2431 case 0x60000016: return "SUNW_TLSSORT";
2432 case 0x60000017: return "SUNW_TLSSORTSZ";
2433 case 0x60000018: return "SUNW_CAPINFO";
2434 case 0x60000019: return "SUNW_STRPAD";
2435 case 0x6000001a: return "SUNW_CAPCHAIN";
2436 case 0x6000001b: return "SUNW_LDMACH";
2437 case 0x6000001d: return "SUNW_CAPCHAINENT";
2438 case 0x6000001f: return "SUNW_CAPCHAINSZ";
2439 case 0x60000021: return "SUNW_PARENT";
2440 case 0x60000023: return "SUNW_ASLR";
2441 case 0x60000025: return "SUNW_RELAX";
2442 case 0x60000029: return "SUNW_NXHEAP";
2443 case 0x6000002b: return "SUNW_NXSTACK";
2444
2445 case 0x70000001: return "SPARC_REGISTER";
2446 case 0x7ffffffd: return "AUXILIARY";
2447 case 0x7ffffffe: return "USED";
2448 case 0x7fffffff: return "FILTER";
2449
2450 default: return NULL;
2451 }
2452 }
2453
2454 static const char *
2455 get_riscv_dynamic_type (unsigned long type)
2456 {
2457 switch (type)
2458 {
2459 case DT_RISCV_VARIANT_CC: return "RISCV_VARIANT_CC";
2460 default:
2461 return NULL;
2462 }
2463 }
2464
2465 static const char *
2466 get_dynamic_type (Filedata * filedata, unsigned long type)
2467 {
2468 static char buff[64];
2469
2470 switch (type)
2471 {
2472 case DT_NULL: return "NULL";
2473 case DT_NEEDED: return "NEEDED";
2474 case DT_PLTRELSZ: return "PLTRELSZ";
2475 case DT_PLTGOT: return "PLTGOT";
2476 case DT_HASH: return "HASH";
2477 case DT_STRTAB: return "STRTAB";
2478 case DT_SYMTAB: return "SYMTAB";
2479 case DT_RELA: return "RELA";
2480 case DT_RELASZ: return "RELASZ";
2481 case DT_RELAENT: return "RELAENT";
2482 case DT_STRSZ: return "STRSZ";
2483 case DT_SYMENT: return "SYMENT";
2484 case DT_INIT: return "INIT";
2485 case DT_FINI: return "FINI";
2486 case DT_SONAME: return "SONAME";
2487 case DT_RPATH: return "RPATH";
2488 case DT_SYMBOLIC: return "SYMBOLIC";
2489 case DT_REL: return "REL";
2490 case DT_RELSZ: return "RELSZ";
2491 case DT_RELENT: return "RELENT";
2492 case DT_RELR: return "RELR";
2493 case DT_RELRSZ: return "RELRSZ";
2494 case DT_RELRENT: return "RELRENT";
2495 case DT_PLTREL: return "PLTREL";
2496 case DT_DEBUG: return "DEBUG";
2497 case DT_TEXTREL: return "TEXTREL";
2498 case DT_JMPREL: return "JMPREL";
2499 case DT_BIND_NOW: return "BIND_NOW";
2500 case DT_INIT_ARRAY: return "INIT_ARRAY";
2501 case DT_FINI_ARRAY: return "FINI_ARRAY";
2502 case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
2503 case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
2504 case DT_RUNPATH: return "RUNPATH";
2505 case DT_FLAGS: return "FLAGS";
2506
2507 case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
2508 case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
2509 case DT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
2510
2511 case DT_CHECKSUM: return "CHECKSUM";
2512 case DT_PLTPADSZ: return "PLTPADSZ";
2513 case DT_MOVEENT: return "MOVEENT";
2514 case DT_MOVESZ: return "MOVESZ";
2515 case DT_FEATURE: return "FEATURE";
2516 case DT_POSFLAG_1: return "POSFLAG_1";
2517 case DT_SYMINSZ: return "SYMINSZ";
2518 case DT_SYMINENT: return "SYMINENT"; /* aka VALRNGHI */
2519
2520 case DT_ADDRRNGLO: return "ADDRRNGLO";
2521 case DT_CONFIG: return "CONFIG";
2522 case DT_DEPAUDIT: return "DEPAUDIT";
2523 case DT_AUDIT: return "AUDIT";
2524 case DT_PLTPAD: return "PLTPAD";
2525 case DT_MOVETAB: return "MOVETAB";
2526 case DT_SYMINFO: return "SYMINFO"; /* aka ADDRRNGHI */
2527
2528 case DT_VERSYM: return "VERSYM";
2529
2530 case DT_TLSDESC_GOT: return "TLSDESC_GOT";
2531 case DT_TLSDESC_PLT: return "TLSDESC_PLT";
2532 case DT_RELACOUNT: return "RELACOUNT";
2533 case DT_RELCOUNT: return "RELCOUNT";
2534 case DT_FLAGS_1: return "FLAGS_1";
2535 case DT_VERDEF: return "VERDEF";
2536 case DT_VERDEFNUM: return "VERDEFNUM";
2537 case DT_VERNEED: return "VERNEED";
2538 case DT_VERNEEDNUM: return "VERNEEDNUM";
2539
2540 case DT_AUXILIARY: return "AUXILIARY";
2541 case DT_USED: return "USED";
2542 case DT_FILTER: return "FILTER";
2543
2544 case DT_GNU_PRELINKED: return "GNU_PRELINKED";
2545 case DT_GNU_CONFLICT: return "GNU_CONFLICT";
2546 case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
2547 case DT_GNU_LIBLIST: return "GNU_LIBLIST";
2548 case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
2549 case DT_GNU_HASH: return "GNU_HASH";
2550 case DT_GNU_FLAGS_1: return "GNU_FLAGS_1";
2551
2552 default:
2553 if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
2554 {
2555 const char * result;
2556
2557 switch (filedata->file_header.e_machine)
2558 {
2559 case EM_AARCH64:
2560 result = get_aarch64_dynamic_type (type);
2561 break;
2562 case EM_MIPS:
2563 case EM_MIPS_RS3_LE:
2564 result = get_mips_dynamic_type (type);
2565 break;
2566 case EM_SPARCV9:
2567 result = get_sparc64_dynamic_type (type);
2568 break;
2569 case EM_PPC:
2570 result = get_ppc_dynamic_type (type);
2571 break;
2572 case EM_PPC64:
2573 result = get_ppc64_dynamic_type (type);
2574 break;
2575 case EM_IA_64:
2576 result = get_ia64_dynamic_type (type);
2577 break;
2578 case EM_ALPHA:
2579 result = get_alpha_dynamic_type (type);
2580 break;
2581 case EM_SCORE:
2582 result = get_score_dynamic_type (type);
2583 break;
2584 case EM_TI_C6000:
2585 result = get_tic6x_dynamic_type (type);
2586 break;
2587 case EM_ALTERA_NIOS2:
2588 result = get_nios2_dynamic_type (type);
2589 break;
2590 case EM_RISCV:
2591 result = get_riscv_dynamic_type (type);
2592 break;
2593 default:
2594 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2595 result = get_solaris_dynamic_type (type);
2596 else
2597 result = NULL;
2598 break;
2599 }
2600
2601 if (result != NULL)
2602 return result;
2603
2604 snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
2605 }
2606 else if (((type >= DT_LOOS) && (type <= DT_HIOS))
2607 || (filedata->file_header.e_machine == EM_PARISC
2608 && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
2609 {
2610 const char * result;
2611
2612 switch (filedata->file_header.e_machine)
2613 {
2614 case EM_PARISC:
2615 result = get_parisc_dynamic_type (type);
2616 break;
2617 case EM_IA_64:
2618 result = get_ia64_dynamic_type (type);
2619 break;
2620 default:
2621 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2622 result = get_solaris_dynamic_type (type);
2623 else
2624 result = NULL;
2625 break;
2626 }
2627
2628 if (result != NULL)
2629 return result;
2630
2631 snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
2632 type);
2633 }
2634 else
2635 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
2636
2637 return buff;
2638 }
2639 }
2640
2641 static bool get_program_headers (Filedata *);
2642 static bool get_dynamic_section (Filedata *);
2643
2644 static void
2645 locate_dynamic_section (Filedata *filedata)
2646 {
2647 unsigned long dynamic_addr = 0;
2648 bfd_size_type dynamic_size = 0;
2649
2650 if (filedata->file_header.e_phnum != 0
2651 && get_program_headers (filedata))
2652 {
2653 Elf_Internal_Phdr *segment;
2654 unsigned int i;
2655
2656 for (i = 0, segment = filedata->program_headers;
2657 i < filedata->file_header.e_phnum;
2658 i++, segment++)
2659 {
2660 if (segment->p_type == PT_DYNAMIC)
2661 {
2662 dynamic_addr = segment->p_offset;
2663 dynamic_size = segment->p_filesz;
2664
2665 if (filedata->section_headers != NULL)
2666 {
2667 Elf_Internal_Shdr *sec;
2668
2669 sec = find_section (filedata, ".dynamic");
2670 if (sec != NULL)
2671 {
2672 if (sec->sh_size == 0
2673 || sec->sh_type == SHT_NOBITS)
2674 {
2675 dynamic_addr = 0;
2676 dynamic_size = 0;
2677 }
2678 else
2679 {
2680 dynamic_addr = sec->sh_offset;
2681 dynamic_size = sec->sh_size;
2682 }
2683 }
2684 }
2685
2686 if (dynamic_addr > filedata->file_size
2687 || (dynamic_size > filedata->file_size - dynamic_addr))
2688 {
2689 dynamic_addr = 0;
2690 dynamic_size = 0;
2691 }
2692 break;
2693 }
2694 }
2695 }
2696 filedata->dynamic_addr = dynamic_addr;
2697 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
2698 }
2699
2700 static bool
2701 is_pie (Filedata *filedata)
2702 {
2703 Elf_Internal_Dyn *entry;
2704
2705 if (filedata->dynamic_size == 0)
2706 locate_dynamic_section (filedata);
2707 if (filedata->dynamic_size <= 1)
2708 return false;
2709
2710 if (!get_dynamic_section (filedata))
2711 return false;
2712
2713 for (entry = filedata->dynamic_section;
2714 entry < filedata->dynamic_section + filedata->dynamic_nent;
2715 entry++)
2716 {
2717 if (entry->d_tag == DT_FLAGS_1)
2718 {
2719 if ((entry->d_un.d_val & DF_1_PIE) != 0)
2720 return true;
2721 break;
2722 }
2723 }
2724 return false;
2725 }
2726
2727 static char *
2728 get_file_type (Filedata *filedata)
2729 {
2730 unsigned e_type = filedata->file_header.e_type;
2731 static char buff[64];
2732
2733 switch (e_type)
2734 {
2735 case ET_NONE: return _("NONE (None)");
2736 case ET_REL: return _("REL (Relocatable file)");
2737 case ET_EXEC: return _("EXEC (Executable file)");
2738 case ET_DYN:
2739 if (is_pie (filedata))
2740 return _("DYN (Position-Independent Executable file)");
2741 else
2742 return _("DYN (Shared object file)");
2743 case ET_CORE: return _("CORE (Core file)");
2744
2745 default:
2746 if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
2747 snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
2748 else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
2749 snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
2750 else
2751 snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
2752 return buff;
2753 }
2754 }
2755
2756 static char *
2757 get_machine_name (unsigned e_machine)
2758 {
2759 static char buff[64]; /* XXX */
2760
2761 switch (e_machine)
2762 {
2763 /* Please keep this switch table sorted by increasing EM_ value. */
2764 /* 0 */
2765 case EM_NONE: return _("None");
2766 case EM_M32: return "WE32100";
2767 case EM_SPARC: return "Sparc";
2768 case EM_386: return "Intel 80386";
2769 case EM_68K: return "MC68000";
2770 case EM_88K: return "MC88000";
2771 case EM_IAMCU: return "Intel MCU";
2772 case EM_860: return "Intel 80860";
2773 case EM_MIPS: return "MIPS R3000";
2774 case EM_S370: return "IBM System/370";
2775 /* 10 */
2776 case EM_MIPS_RS3_LE: return "MIPS R4000 big-endian";
2777 case EM_OLD_SPARCV9: return "Sparc v9 (old)";
2778 case EM_PARISC: return "HPPA";
2779 case EM_VPP550: return "Fujitsu VPP500";
2780 case EM_SPARC32PLUS: return "Sparc v8+" ;
2781 case EM_960: return "Intel 80960";
2782 case EM_PPC: return "PowerPC";
2783 /* 20 */
2784 case EM_PPC64: return "PowerPC64";
2785 case EM_S390_OLD:
2786 case EM_S390: return "IBM S/390";
2787 case EM_SPU: return "SPU";
2788 /* 30 */
2789 case EM_V800: return "Renesas V850 (using RH850 ABI)";
2790 case EM_FR20: return "Fujitsu FR20";
2791 case EM_RH32: return "TRW RH32";
2792 case EM_MCORE: return "MCORE";
2793 /* 40 */
2794 case EM_ARM: return "ARM";
2795 case EM_OLD_ALPHA: return "Digital Alpha (old)";
2796 case EM_SH: return "Renesas / SuperH SH";
2797 case EM_SPARCV9: return "Sparc v9";
2798 case EM_TRICORE: return "Siemens Tricore";
2799 case EM_ARC: return "ARC";
2800 case EM_H8_300: return "Renesas H8/300";
2801 case EM_H8_300H: return "Renesas H8/300H";
2802 case EM_H8S: return "Renesas H8S";
2803 case EM_H8_500: return "Renesas H8/500";
2804 /* 50 */
2805 case EM_IA_64: return "Intel IA-64";
2806 case EM_MIPS_X: return "Stanford MIPS-X";
2807 case EM_COLDFIRE: return "Motorola Coldfire";
2808 case EM_68HC12: return "Motorola MC68HC12 Microcontroller";
2809 case EM_MMA: return "Fujitsu Multimedia Accelerator";
2810 case EM_PCP: return "Siemens PCP";
2811 case EM_NCPU: return "Sony nCPU embedded RISC processor";
2812 case EM_NDR1: return "Denso NDR1 microprocesspr";
2813 case EM_STARCORE: return "Motorola Star*Core processor";
2814 case EM_ME16: return "Toyota ME16 processor";
2815 /* 60 */
2816 case EM_ST100: return "STMicroelectronics ST100 processor";
2817 case EM_TINYJ: return "Advanced Logic Corp. TinyJ embedded processor";
2818 case EM_X86_64: return "Advanced Micro Devices X86-64";
2819 case EM_PDSP: return "Sony DSP processor";
2820 case EM_PDP10: return "Digital Equipment Corp. PDP-10";
2821 case EM_PDP11: return "Digital Equipment Corp. PDP-11";
2822 case EM_FX66: return "Siemens FX66 microcontroller";
2823 case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 bit microcontroller";
2824 case EM_ST7: return "STMicroelectronics ST7 8-bit microcontroller";
2825 case EM_68HC16: return "Motorola MC68HC16 Microcontroller";
2826 /* 70 */
2827 case EM_68HC11: return "Motorola MC68HC11 Microcontroller";
2828 case EM_68HC08: return "Motorola MC68HC08 Microcontroller";
2829 case EM_68HC05: return "Motorola MC68HC05 Microcontroller";
2830 case EM_SVX: return "Silicon Graphics SVx";
2831 case EM_ST19: return "STMicroelectronics ST19 8-bit microcontroller";
2832 case EM_VAX: return "Digital VAX";
2833 case EM_CRIS: return "Axis Communications 32-bit embedded processor";
2834 case EM_JAVELIN: return "Infineon Technologies 32-bit embedded cpu";
2835 case EM_FIREPATH: return "Element 14 64-bit DSP processor";
2836 case EM_ZSP: return "LSI Logic's 16-bit DSP processor";
2837 /* 80 */
2838 case EM_MMIX: return "Donald Knuth's educational 64-bit processor";
2839 case EM_HUANY: return "Harvard Universitys's machine-independent object format";
2840 case EM_PRISM: return "Vitesse Prism";
2841 case EM_AVR_OLD:
2842 case EM_AVR: return "Atmel AVR 8-bit microcontroller";
2843 case EM_CYGNUS_FR30:
2844 case EM_FR30: return "Fujitsu FR30";
2845 case EM_CYGNUS_D10V:
2846 case EM_D10V: return "d10v";
2847 case EM_CYGNUS_D30V:
2848 case EM_D30V: return "d30v";
2849 case EM_CYGNUS_V850:
2850 case EM_V850: return "Renesas V850";
2851 case EM_CYGNUS_M32R:
2852 case EM_M32R: return "Renesas M32R (formerly Mitsubishi M32r)";
2853 case EM_CYGNUS_MN10300:
2854 case EM_MN10300: return "mn10300";
2855 /* 90 */
2856 case EM_CYGNUS_MN10200:
2857 case EM_MN10200: return "mn10200";
2858 case EM_PJ: return "picoJava";
2859 case EM_OR1K: return "OpenRISC 1000";
2860 case EM_ARC_COMPACT: return "ARCompact";
2861 case EM_XTENSA_OLD:
2862 case EM_XTENSA: return "Tensilica Xtensa Processor";
2863 case EM_VIDEOCORE: return "Alphamosaic VideoCore processor";
2864 case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor";
2865 case EM_NS32K: return "National Semiconductor 32000 series";
2866 case EM_TPC: return "Tenor Network TPC processor";
2867 case EM_SNP1K: return "Trebia SNP 1000 processor";
2868 /* 100 */
2869 case EM_ST200: return "STMicroelectronics ST200 microcontroller";
2870 case EM_IP2K_OLD:
2871 case EM_IP2K: return "Ubicom IP2xxx 8-bit microcontrollers";
2872 case EM_MAX: return "MAX Processor";
2873 case EM_CR: return "National Semiconductor CompactRISC";
2874 case EM_F2MC16: return "Fujitsu F2MC16";
2875 case EM_MSP430: return "Texas Instruments msp430 microcontroller";
2876 case EM_BLACKFIN: return "Analog Devices Blackfin";
2877 case EM_SE_C33: return "S1C33 Family of Seiko Epson processors";
2878 case EM_SEP: return "Sharp embedded microprocessor";
2879 case EM_ARCA: return "Arca RISC microprocessor";
2880 /* 110 */
2881 case EM_UNICORE: return "Unicore";
2882 case EM_EXCESS: return "eXcess 16/32/64-bit configurable embedded CPU";
2883 case EM_DXP: return "Icera Semiconductor Inc. Deep Execution Processor";
2884 case EM_ALTERA_NIOS2: return "Altera Nios II";
2885 case EM_CRX: return "National Semiconductor CRX microprocessor";
2886 case EM_XGATE: return "Motorola XGATE embedded processor";
2887 case EM_C166:
2888 case EM_XC16X: return "Infineon Technologies xc16x";
2889 case EM_M16C: return "Renesas M16C series microprocessors";
2890 case EM_DSPIC30F: return "Microchip Technology dsPIC30F Digital Signal Controller";
2891 case EM_CE: return "Freescale Communication Engine RISC core";
2892 /* 120 */
2893 case EM_M32C: return "Renesas M32c";
2894 /* 130 */
2895 case EM_TSK3000: return "Altium TSK3000 core";
2896 case EM_RS08: return "Freescale RS08 embedded processor";
2897 case EM_ECOG2: return "Cyan Technology eCOG2 microprocessor";
2898 case EM_SCORE: return "SUNPLUS S+Core";
2899 case EM_DSP24: return "New Japan Radio (NJR) 24-bit DSP Processor";
2900 case EM_VIDEOCORE3: return "Broadcom VideoCore III processor";
2901 case EM_LATTICEMICO32: return "Lattice Mico32";
2902 case EM_SE_C17: return "Seiko Epson C17 family";
2903 /* 140 */
2904 case EM_TI_C6000: return "Texas Instruments TMS320C6000 DSP family";
2905 case EM_TI_C2000: return "Texas Instruments TMS320C2000 DSP family";
2906 case EM_TI_C5500: return "Texas Instruments TMS320C55x DSP family";
2907 case EM_TI_PRU: return "TI PRU I/O processor";
2908 /* 160 */
2909 case EM_MMDSP_PLUS: return "STMicroelectronics 64bit VLIW Data Signal Processor";
2910 case EM_CYPRESS_M8C: return "Cypress M8C microprocessor";
2911 case EM_R32C: return "Renesas R32C series microprocessors";
2912 case EM_TRIMEDIA: return "NXP Semiconductors TriMedia architecture family";
2913 case EM_QDSP6: return "QUALCOMM DSP6 Processor";
2914 case EM_8051: return "Intel 8051 and variants";
2915 case EM_STXP7X: return "STMicroelectronics STxP7x family";
2916 case EM_NDS32: return "Andes Technology compact code size embedded RISC processor family";
2917 case EM_ECOG1X: return "Cyan Technology eCOG1X family";
2918 case EM_MAXQ30: return "Dallas Semiconductor MAXQ30 Core microcontrollers";
2919 /* 170 */
2920 case EM_XIMO16: return "New Japan Radio (NJR) 16-bit DSP Processor";
2921 case EM_MANIK: return "M2000 Reconfigurable RISC Microprocessor";
2922 case EM_CRAYNV2: return "Cray Inc. NV2 vector architecture";
2923 case EM_RX: return "Renesas RX";
2924 case EM_METAG: return "Imagination Technologies Meta processor architecture";
2925 case EM_MCST_ELBRUS: return "MCST Elbrus general purpose hardware architecture";
2926 case EM_ECOG16: return "Cyan Technology eCOG16 family";
2927 case EM_CR16:
2928 case EM_MICROBLAZE:
2929 case EM_MICROBLAZE_OLD: return "Xilinx MicroBlaze";
2930 case EM_ETPU: return "Freescale Extended Time Processing Unit";
2931 case EM_SLE9X: return "Infineon Technologies SLE9X core";
2932 /* 180 */
2933 case EM_L1OM: return "Intel L1OM";
2934 case EM_K1OM: return "Intel K1OM";
2935 case EM_INTEL182: return "Intel (reserved)";
2936 case EM_AARCH64: return "AArch64";
2937 case EM_ARM184: return "ARM (reserved)";
2938 case EM_AVR32: return "Atmel Corporation 32-bit microprocessor";
2939 case EM_STM8: return "STMicroeletronics STM8 8-bit microcontroller";
2940 case EM_TILE64: return "Tilera TILE64 multicore architecture family";
2941 case EM_TILEPRO: return "Tilera TILEPro multicore architecture family";
2942 /* 190 */
2943 case EM_CUDA: return "NVIDIA CUDA architecture";
2944 case EM_TILEGX: return "Tilera TILE-Gx multicore architecture family";
2945 case EM_CLOUDSHIELD: return "CloudShield architecture family";
2946 case EM_COREA_1ST: return "KIPO-KAIST Core-A 1st generation processor family";
2947 case EM_COREA_2ND: return "KIPO-KAIST Core-A 2nd generation processor family";
2948 case EM_ARC_COMPACT2: return "ARCv2";
2949 case EM_OPEN8: return "Open8 8-bit RISC soft processor core";
2950 case EM_RL78: return "Renesas RL78";
2951 case EM_VIDEOCORE5: return "Broadcom VideoCore V processor";
2952 case EM_78K0R: return "Renesas 78K0R";
2953 /* 200 */
2954 case EM_56800EX: return "Freescale 56800EX Digital Signal Controller (DSC)";
2955 case EM_BA1: return "Beyond BA1 CPU architecture";
2956 case EM_BA2: return "Beyond BA2 CPU architecture";
2957 case EM_XCORE: return "XMOS xCORE processor family";
2958 case EM_MCHP_PIC: return "Microchip 8-bit PIC(r) family";
2959 case EM_INTELGT: return "Intel Graphics Technology";
2960 /* 210 */
2961 case EM_KM32: return "KM211 KM32 32-bit processor";
2962 case EM_KMX32: return "KM211 KMX32 32-bit processor";
2963 case EM_KMX16: return "KM211 KMX16 16-bit processor";
2964 case EM_KMX8: return "KM211 KMX8 8-bit processor";
2965 case EM_KVARC: return "KM211 KVARC processor";
2966 case EM_CDP: return "Paneve CDP architecture family";
2967 case EM_COGE: return "Cognitive Smart Memory Processor";
2968 case EM_COOL: return "Bluechip Systems CoolEngine";
2969 case EM_NORC: return "Nanoradio Optimized RISC";
2970 case EM_CSR_KALIMBA: return "CSR Kalimba architecture family";
2971 /* 220 */
2972 case EM_Z80: return "Zilog Z80";
2973 case EM_VISIUM: return "CDS VISIUMcore processor";
2974 case EM_FT32: return "FTDI Chip FT32";
2975 case EM_MOXIE: return "Moxie";
2976 case EM_AMDGPU: return "AMD GPU";
2977 /* 230 (all reserved) */
2978 /* 240 */
2979 case EM_RISCV: return "RISC-V";
2980 case EM_LANAI: return "Lanai 32-bit processor";
2981 case EM_CEVA: return "CEVA Processor Architecture Family";
2982 case EM_CEVA_X2: return "CEVA X2 Processor Family";
2983 case EM_BPF: return "Linux BPF";
2984 case EM_GRAPHCORE_IPU: return "Graphcore Intelligent Processing Unit";
2985 case EM_IMG1: return "Imagination Technologies";
2986 /* 250 */
2987 case EM_NFP: return "Netronome Flow Processor";
2988 case EM_VE: return "NEC Vector Engine";
2989 case EM_CSKY: return "C-SKY";
2990 case EM_ARC_COMPACT3_64: return "Synopsys ARCv2.3 64-bit";
2991 case EM_MCS6502: return "MOS Technology MCS 6502 processor";
2992 case EM_ARC_COMPACT3: return "Synopsys ARCv2.3 32-bit";
2993 case EM_KVX: return "Kalray VLIW core of the MPPA processor family";
2994 case EM_65816: return "WDC 65816/65C816";
2995 case EM_LOONGARCH: return "LoongArch";
2996 case EM_KF32: return "ChipON KungFu32";
2997
2998 /* Large numbers... */
2999 case EM_MT: return "Morpho Techologies MT processor";
3000 case EM_ALPHA: return "Alpha";
3001 case EM_WEBASSEMBLY: return "Web Assembly";
3002 case EM_DLX: return "OpenDLX";
3003 case EM_XSTORMY16: return "Sanyo XStormy16 CPU core";
3004 case EM_IQ2000: return "Vitesse IQ2000";
3005 case EM_M32C_OLD:
3006 case EM_NIOS32: return "Altera Nios";
3007 case EM_CYGNUS_MEP: return "Toshiba MeP Media Engine";
3008 case EM_ADAPTEVA_EPIPHANY: return "Adapteva EPIPHANY";
3009 case EM_CYGNUS_FRV: return "Fujitsu FR-V";
3010 case EM_S12Z: return "Freescale S12Z";
3011
3012 default:
3013 snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
3014 return buff;
3015 }
3016 }
3017
3018 static void
3019 decode_ARC_machine_flags (unsigned e_flags, unsigned e_machine, char buf[])
3020 {
3021 /* ARC has two machine types EM_ARC_COMPACT and EM_ARC_COMPACT2. Some
3022 other compilers don't specify an architecture type in the e_flags, and
3023 instead use EM_ARC_COMPACT for old ARC600, ARC601, and ARC700
3024 architectures, and switch to EM_ARC_COMPACT2 for newer ARCEM and ARCHS
3025 architectures.
3026
3027 Th GNU tools follows this use of EM_ARC_COMPACT and EM_ARC_COMPACT2,
3028 but also sets a specific architecture type in the e_flags field.
3029
3030 However, when decoding the flags we don't worry if we see an
3031 unexpected pairing, for example EM_ARC_COMPACT machine type, with
3032 ARCEM architecture type. */
3033
3034 switch (e_flags & EF_ARC_MACH_MSK)
3035 {
3036 /* We only expect these to occur for EM_ARC_COMPACT2. */
3037 case EF_ARC_CPU_ARCV2EM:
3038 strcat (buf, ", ARC EM");
3039 break;
3040 case EF_ARC_CPU_ARCV2HS:
3041 strcat (buf, ", ARC HS");
3042 break;
3043
3044 /* We only expect these to occur for EM_ARC_COMPACT. */
3045 case E_ARC_MACH_ARC600:
3046 strcat (buf, ", ARC600");
3047 break;
3048 case E_ARC_MACH_ARC601:
3049 strcat (buf, ", ARC601");
3050 break;
3051 case E_ARC_MACH_ARC700:
3052 strcat (buf, ", ARC700");
3053 break;
3054
3055 /* The only times we should end up here are (a) A corrupt ELF, (b) A
3056 new ELF with new architecture being read by an old version of
3057 readelf, or (c) An ELF built with non-GNU compiler that does not
3058 set the architecture in the e_flags. */
3059 default:
3060 if (e_machine == EM_ARC_COMPACT)
3061 strcat (buf, ", Unknown ARCompact");
3062 else
3063 strcat (buf, ", Unknown ARC");
3064 break;
3065 }
3066
3067 switch (e_flags & EF_ARC_OSABI_MSK)
3068 {
3069 case E_ARC_OSABI_ORIG:
3070 strcat (buf, ", (ABI:legacy)");
3071 break;
3072 case E_ARC_OSABI_V2:
3073 strcat (buf, ", (ABI:v2)");
3074 break;
3075 /* Only upstream 3.9+ kernels will support ARCv2 ISA. */
3076 case E_ARC_OSABI_V3:
3077 strcat (buf, ", v3 no-legacy-syscalls ABI");
3078 break;
3079 case E_ARC_OSABI_V4:
3080 strcat (buf, ", v4 ABI");
3081 break;
3082 default:
3083 strcat (buf, ", unrecognised ARC OSABI flag");
3084 break;
3085 }
3086 }
3087
3088 static void
3089 decode_ARM_machine_flags (unsigned e_flags, char buf[])
3090 {
3091 unsigned eabi;
3092 bool unknown = false;
3093
3094 eabi = EF_ARM_EABI_VERSION (e_flags);
3095 e_flags &= ~ EF_ARM_EABIMASK;
3096
3097 /* Handle "generic" ARM flags. */
3098 if (e_flags & EF_ARM_RELEXEC)
3099 {
3100 strcat (buf, ", relocatable executable");
3101 e_flags &= ~ EF_ARM_RELEXEC;
3102 }
3103
3104 if (e_flags & EF_ARM_PIC)
3105 {
3106 strcat (buf, ", position independent");
3107 e_flags &= ~ EF_ARM_PIC;
3108 }
3109
3110 /* Now handle EABI specific flags. */
3111 switch (eabi)
3112 {
3113 default:
3114 strcat (buf, ", <unrecognized EABI>");
3115 if (e_flags)
3116 unknown = true;
3117 break;
3118
3119 case EF_ARM_EABI_VER1:
3120 strcat (buf, ", Version1 EABI");
3121 while (e_flags)
3122 {
3123 unsigned flag;
3124
3125 /* Process flags one bit at a time. */
3126 flag = e_flags & - e_flags;
3127 e_flags &= ~ flag;
3128
3129 switch (flag)
3130 {
3131 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
3132 strcat (buf, ", sorted symbol tables");
3133 break;
3134
3135 default:
3136 unknown = true;
3137 break;
3138 }
3139 }
3140 break;
3141
3142 case EF_ARM_EABI_VER2:
3143 strcat (buf, ", Version2 EABI");
3144 while (e_flags)
3145 {
3146 unsigned flag;
3147
3148 /* Process flags one bit at a time. */
3149 flag = e_flags & - e_flags;
3150 e_flags &= ~ flag;
3151
3152 switch (flag)
3153 {
3154 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
3155 strcat (buf, ", sorted symbol tables");
3156 break;
3157
3158 case EF_ARM_DYNSYMSUSESEGIDX:
3159 strcat (buf, ", dynamic symbols use segment index");
3160 break;
3161
3162 case EF_ARM_MAPSYMSFIRST:
3163 strcat (buf, ", mapping symbols precede others");
3164 break;
3165
3166 default:
3167 unknown = true;
3168 break;
3169 }
3170 }
3171 break;
3172
3173 case EF_ARM_EABI_VER3:
3174 strcat (buf, ", Version3 EABI");
3175 break;
3176
3177 case EF_ARM_EABI_VER4:
3178 strcat (buf, ", Version4 EABI");
3179 while (e_flags)
3180 {
3181 unsigned flag;
3182
3183 /* Process flags one bit at a time. */
3184 flag = e_flags & - e_flags;
3185 e_flags &= ~ flag;
3186
3187 switch (flag)
3188 {
3189 case EF_ARM_BE8:
3190 strcat (buf, ", BE8");
3191 break;
3192
3193 case EF_ARM_LE8:
3194 strcat (buf, ", LE8");
3195 break;
3196
3197 default:
3198 unknown = true;
3199 break;
3200 }
3201 }
3202 break;
3203
3204 case EF_ARM_EABI_VER5:
3205 strcat (buf, ", Version5 EABI");
3206 while (e_flags)
3207 {
3208 unsigned flag;
3209
3210 /* Process flags one bit at a time. */
3211 flag = e_flags & - e_flags;
3212 e_flags &= ~ flag;
3213
3214 switch (flag)
3215 {
3216 case EF_ARM_BE8:
3217 strcat (buf, ", BE8");
3218 break;
3219
3220 case EF_ARM_LE8:
3221 strcat (buf, ", LE8");
3222 break;
3223
3224 case EF_ARM_ABI_FLOAT_SOFT: /* Conflicts with EF_ARM_SOFT_FLOAT. */
3225 strcat (buf, ", soft-float ABI");
3226 break;
3227
3228 case EF_ARM_ABI_FLOAT_HARD: /* Conflicts with EF_ARM_VFP_FLOAT. */
3229 strcat (buf, ", hard-float ABI");
3230 break;
3231
3232 default:
3233 unknown = true;
3234 break;
3235 }
3236 }
3237 break;
3238
3239 case EF_ARM_EABI_UNKNOWN:
3240 strcat (buf, ", GNU EABI");
3241 while (e_flags)
3242 {
3243 unsigned flag;
3244
3245 /* Process flags one bit at a time. */
3246 flag = e_flags & - e_flags;
3247 e_flags &= ~ flag;
3248
3249 switch (flag)
3250 {
3251 case EF_ARM_INTERWORK:
3252 strcat (buf, ", interworking enabled");
3253 break;
3254
3255 case EF_ARM_APCS_26:
3256 strcat (buf, ", uses APCS/26");
3257 break;
3258
3259 case EF_ARM_APCS_FLOAT:
3260 strcat (buf, ", uses APCS/float");
3261 break;
3262
3263 case EF_ARM_PIC:
3264 strcat (buf, ", position independent");
3265 break;
3266
3267 case EF_ARM_ALIGN8:
3268 strcat (buf, ", 8 bit structure alignment");
3269 break;
3270
3271 case EF_ARM_NEW_ABI:
3272 strcat (buf, ", uses new ABI");
3273 break;
3274
3275 case EF_ARM_OLD_ABI:
3276 strcat (buf, ", uses old ABI");
3277 break;
3278
3279 case EF_ARM_SOFT_FLOAT:
3280 strcat (buf, ", software FP");
3281 break;
3282
3283 case EF_ARM_VFP_FLOAT:
3284 strcat (buf, ", VFP");
3285 break;
3286
3287 case EF_ARM_MAVERICK_FLOAT:
3288 strcat (buf, ", Maverick FP");
3289 break;
3290
3291 default:
3292 unknown = true;
3293 break;
3294 }
3295 }
3296 }
3297
3298 if (unknown)
3299 strcat (buf,_(", <unknown>"));
3300 }
3301
3302 static void
3303 decode_AVR_machine_flags (unsigned e_flags, char buf[], size_t size)
3304 {
3305 --size; /* Leave space for null terminator. */
3306
3307 switch (e_flags & EF_AVR_MACH)
3308 {
3309 case E_AVR_MACH_AVR1:
3310 strncat (buf, ", avr:1", size);
3311 break;
3312 case E_AVR_MACH_AVR2:
3313 strncat (buf, ", avr:2", size);
3314 break;
3315 case E_AVR_MACH_AVR25:
3316 strncat (buf, ", avr:25", size);
3317 break;
3318 case E_AVR_MACH_AVR3:
3319 strncat (buf, ", avr:3", size);
3320 break;
3321 case E_AVR_MACH_AVR31:
3322 strncat (buf, ", avr:31", size);
3323 break;
3324 case E_AVR_MACH_AVR35:
3325 strncat (buf, ", avr:35", size);
3326 break;
3327 case E_AVR_MACH_AVR4:
3328 strncat (buf, ", avr:4", size);
3329 break;
3330 case E_AVR_MACH_AVR5:
3331 strncat (buf, ", avr:5", size);
3332 break;
3333 case E_AVR_MACH_AVR51:
3334 strncat (buf, ", avr:51", size);
3335 break;
3336 case E_AVR_MACH_AVR6:
3337 strncat (buf, ", avr:6", size);
3338 break;
3339 case E_AVR_MACH_AVRTINY:
3340 strncat (buf, ", avr:100", size);
3341 break;
3342 case E_AVR_MACH_XMEGA1:
3343 strncat (buf, ", avr:101", size);
3344 break;
3345 case E_AVR_MACH_XMEGA2:
3346 strncat (buf, ", avr:102", size);
3347 break;
3348 case E_AVR_MACH_XMEGA3:
3349 strncat (buf, ", avr:103", size);
3350 break;
3351 case E_AVR_MACH_XMEGA4:
3352 strncat (buf, ", avr:104", size);
3353 break;
3354 case E_AVR_MACH_XMEGA5:
3355 strncat (buf, ", avr:105", size);
3356 break;
3357 case E_AVR_MACH_XMEGA6:
3358 strncat (buf, ", avr:106", size);
3359 break;
3360 case E_AVR_MACH_XMEGA7:
3361 strncat (buf, ", avr:107", size);
3362 break;
3363 default:
3364 strncat (buf, ", avr:<unknown>", size);
3365 break;
3366 }
3367
3368 size -= strlen (buf);
3369 if (e_flags & EF_AVR_LINKRELAX_PREPARED)
3370 strncat (buf, ", link-relax", size);
3371 }
3372
3373 static void
3374 decode_NDS32_machine_flags (unsigned e_flags, char buf[], size_t size)
3375 {
3376 unsigned abi;
3377 unsigned arch;
3378 unsigned config;
3379 unsigned version;
3380 bool has_fpu = false;
3381 unsigned int r = 0;
3382
3383 static const char *ABI_STRINGS[] =
3384 {
3385 "ABI v0", /* use r5 as return register; only used in N1213HC */
3386 "ABI v1", /* use r0 as return register */
3387 "ABI v2", /* use r0 as return register and don't reserve 24 bytes for arguments */
3388 "ABI v2fp", /* for FPU */
3389 "AABI",
3390 "ABI2 FP+"
3391 };
3392 static const char *VER_STRINGS[] =
3393 {
3394 "Andes ELF V1.3 or older",
3395 "Andes ELF V1.3.1",
3396 "Andes ELF V1.4"
3397 };
3398 static const char *ARCH_STRINGS[] =
3399 {
3400 "",
3401 "Andes Star v1.0",
3402 "Andes Star v2.0",
3403 "Andes Star v3.0",
3404 "Andes Star v3.0m"
3405 };
3406
3407 abi = EF_NDS_ABI & e_flags;
3408 arch = EF_NDS_ARCH & e_flags;
3409 config = EF_NDS_INST & e_flags;
3410 version = EF_NDS32_ELF_VERSION & e_flags;
3411
3412 memset (buf, 0, size);
3413
3414 switch (abi)
3415 {
3416 case E_NDS_ABI_V0:
3417 case E_NDS_ABI_V1:
3418 case E_NDS_ABI_V2:
3419 case E_NDS_ABI_V2FP:
3420 case E_NDS_ABI_AABI:
3421 case E_NDS_ABI_V2FP_PLUS:
3422 /* In case there are holes in the array. */
3423 r += snprintf (buf + r, size - r, ", %s", ABI_STRINGS[abi >> EF_NDS_ABI_SHIFT]);
3424 break;
3425
3426 default:
3427 r += snprintf (buf + r, size - r, ", <unrecognized ABI>");
3428 break;
3429 }
3430
3431 switch (version)
3432 {
3433 case E_NDS32_ELF_VER_1_2:
3434 case E_NDS32_ELF_VER_1_3:
3435 case E_NDS32_ELF_VER_1_4:
3436 r += snprintf (buf + r, size - r, ", %s", VER_STRINGS[version >> EF_NDS32_ELF_VERSION_SHIFT]);
3437 break;
3438
3439 default:
3440 r += snprintf (buf + r, size - r, ", <unrecognized ELF version number>");
3441 break;
3442 }
3443
3444 if (E_NDS_ABI_V0 == abi)
3445 {
3446 /* OLD ABI; only used in N1213HC, has performance extension 1. */
3447 r += snprintf (buf + r, size - r, ", Andes Star v1.0, N1213HC, MAC, PERF1");
3448 if (arch == E_NDS_ARCH_STAR_V1_0)
3449 r += snprintf (buf + r, size -r, ", 16b"); /* has 16-bit instructions */
3450 return;
3451 }
3452
3453 switch (arch)
3454 {
3455 case E_NDS_ARCH_STAR_V1_0:
3456 case E_NDS_ARCH_STAR_V2_0:
3457 case E_NDS_ARCH_STAR_V3_0:
3458 case E_NDS_ARCH_STAR_V3_M:
3459 r += snprintf (buf + r, size - r, ", %s", ARCH_STRINGS[arch >> EF_NDS_ARCH_SHIFT]);
3460 break;
3461
3462 default:
3463 r += snprintf (buf + r, size - r, ", <unrecognized architecture>");
3464 /* ARCH version determines how the e_flags are interpreted.
3465 If it is unknown, we cannot proceed. */
3466 return;
3467 }
3468
3469 /* Newer ABI; Now handle architecture specific flags. */
3470 if (arch == E_NDS_ARCH_STAR_V1_0)
3471 {
3472 if (config & E_NDS32_HAS_MFUSR_PC_INST)
3473 r += snprintf (buf + r, size -r, ", MFUSR_PC");
3474
3475 if (!(config & E_NDS32_HAS_NO_MAC_INST))
3476 r += snprintf (buf + r, size -r, ", MAC");
3477
3478 if (config & E_NDS32_HAS_DIV_INST)
3479 r += snprintf (buf + r, size -r, ", DIV");
3480
3481 if (config & E_NDS32_HAS_16BIT_INST)
3482 r += snprintf (buf + r, size -r, ", 16b");
3483 }
3484 else
3485 {
3486 if (config & E_NDS32_HAS_MFUSR_PC_INST)
3487 {
3488 if (version <= E_NDS32_ELF_VER_1_3)
3489 r += snprintf (buf + r, size -r, ", [B8]");
3490 else
3491 r += snprintf (buf + r, size -r, ", EX9");
3492 }
3493
3494 if (config & E_NDS32_HAS_MAC_DX_INST)
3495 r += snprintf (buf + r, size -r, ", MAC_DX");
3496
3497 if (config & E_NDS32_HAS_DIV_DX_INST)
3498 r += snprintf (buf + r, size -r, ", DIV_DX");
3499
3500 if (config & E_NDS32_HAS_16BIT_INST)
3501 {
3502 if (version <= E_NDS32_ELF_VER_1_3)
3503 r += snprintf (buf + r, size -r, ", 16b");
3504 else
3505 r += snprintf (buf + r, size -r, ", IFC");
3506 }
3507 }
3508
3509 if (config & E_NDS32_HAS_EXT_INST)
3510 r += snprintf (buf + r, size -r, ", PERF1");
3511
3512 if (config & E_NDS32_HAS_EXT2_INST)
3513 r += snprintf (buf + r, size -r, ", PERF2");
3514
3515 if (config & E_NDS32_HAS_FPU_INST)
3516 {
3517 has_fpu = true;
3518 r += snprintf (buf + r, size -r, ", FPU_SP");
3519 }
3520
3521 if (config & E_NDS32_HAS_FPU_DP_INST)
3522 {
3523 has_fpu = true;
3524 r += snprintf (buf + r, size -r, ", FPU_DP");
3525 }
3526
3527 if (config & E_NDS32_HAS_FPU_MAC_INST)
3528 {
3529 has_fpu = true;
3530 r += snprintf (buf + r, size -r, ", FPU_MAC");
3531 }
3532
3533 if (has_fpu)
3534 {
3535 switch ((config & E_NDS32_FPU_REG_CONF) >> E_NDS32_FPU_REG_CONF_SHIFT)
3536 {
3537 case E_NDS32_FPU_REG_8SP_4DP:
3538 r += snprintf (buf + r, size -r, ", FPU_REG:8/4");
3539 break;
3540 case E_NDS32_FPU_REG_16SP_8DP:
3541 r += snprintf (buf + r, size -r, ", FPU_REG:16/8");
3542 break;
3543 case E_NDS32_FPU_REG_32SP_16DP:
3544 r += snprintf (buf + r, size -r, ", FPU_REG:32/16");
3545 break;
3546 case E_NDS32_FPU_REG_32SP_32DP:
3547 r += snprintf (buf + r, size -r, ", FPU_REG:32/32");
3548 break;
3549 }
3550 }
3551
3552 if (config & E_NDS32_HAS_AUDIO_INST)
3553 r += snprintf (buf + r, size -r, ", AUDIO");
3554
3555 if (config & E_NDS32_HAS_STRING_INST)
3556 r += snprintf (buf + r, size -r, ", STR");
3557
3558 if (config & E_NDS32_HAS_REDUCED_REGS)
3559 r += snprintf (buf + r, size -r, ", 16REG");
3560
3561 if (config & E_NDS32_HAS_VIDEO_INST)
3562 {
3563 if (version <= E_NDS32_ELF_VER_1_3)
3564 r += snprintf (buf + r, size -r, ", VIDEO");
3565 else
3566 r += snprintf (buf + r, size -r, ", SATURATION");
3567 }
3568
3569 if (config & E_NDS32_HAS_ENCRIPT_INST)
3570 r += snprintf (buf + r, size -r, ", ENCRP");
3571
3572 if (config & E_NDS32_HAS_L2C_INST)
3573 r += snprintf (buf + r, size -r, ", L2C");
3574 }
3575
3576 static void
3577 decode_AMDGPU_machine_flags (Filedata *filedata, unsigned int e_flags,
3578 char *buf)
3579 {
3580 unsigned char *e_ident = filedata->file_header.e_ident;
3581 unsigned char osabi = e_ident[EI_OSABI];
3582 unsigned char abiversion = e_ident[EI_ABIVERSION];
3583 unsigned int mach;
3584
3585 /* HSA OS ABI v2 used a different encoding, but we don't need to support it,
3586 it has been deprecated for a while.
3587
3588 The PAL, MESA3D and NONE OS ABIs are not properly versioned, at the time
3589 of writing, they use the same flags as HSA v3, so the code below uses that
3590 assumption. */
3591 if (osabi == ELFOSABI_AMDGPU_HSA && abiversion < ELFABIVERSION_AMDGPU_HSA_V3)
3592 return;
3593
3594 mach = e_flags & EF_AMDGPU_MACH;
3595 switch (mach)
3596 {
3597 #define AMDGPU_CASE(code, string) \
3598 case code: strcat (buf, ", " string); break;
3599 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX600, "gfx600")
3600 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX601, "gfx601")
3601 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX700, "gfx700")
3602 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX701, "gfx701")
3603 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX702, "gfx702")
3604 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX703, "gfx703")
3605 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX704, "gfx704")
3606 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX801, "gfx801")
3607 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX802, "gfx802")
3608 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX803, "gfx803")
3609 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX810, "gfx810")
3610 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX900, "gfx900")
3611 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX902, "gfx902")
3612 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX904, "gfx904")
3613 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX906, "gfx906")
3614 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX908, "gfx908")
3615 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX909, "gfx909")
3616 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX90C, "gfx90c")
3617 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1010, "gfx1010")
3618 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1011, "gfx1011")
3619 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1012, "gfx1012")
3620 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1030, "gfx1030")
3621 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1031, "gfx1031")
3622 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1032, "gfx1032")
3623 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1033, "gfx1033")
3624 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX602, "gfx602")
3625 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX705, "gfx705")
3626 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX805, "gfx805")
3627 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1035, "gfx1035")
3628 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1034, "gfx1034")
3629 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX90A, "gfx90a")
3630 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX940, "gfx940")
3631 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1013, "gfx1013")
3632 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1036, "gfx1036")
3633 default:
3634 sprintf (buf, _(", <unknown AMDGPU GPU type: %#x>"), mach);
3635 break;
3636 #undef AMDGPU_CASE
3637 }
3638
3639 buf += strlen (buf);
3640 e_flags &= ~EF_AMDGPU_MACH;
3641
3642 if ((osabi == ELFOSABI_AMDGPU_HSA
3643 && abiversion == ELFABIVERSION_AMDGPU_HSA_V3)
3644 || osabi != ELFOSABI_AMDGPU_HSA)
3645 {
3646 /* For HSA v3 and other OS ABIs. */
3647 if (e_flags & EF_AMDGPU_FEATURE_XNACK_V3)
3648 {
3649 strcat (buf, ", xnack on");
3650 buf += strlen (buf);
3651 e_flags &= ~EF_AMDGPU_FEATURE_XNACK_V3;
3652 }
3653
3654 if (e_flags & EF_AMDGPU_FEATURE_SRAMECC_V3)
3655 {
3656 strcat (buf, ", sramecc on");
3657 buf += strlen (buf);
3658 e_flags &= ~EF_AMDGPU_FEATURE_SRAMECC_V3;
3659 }
3660 }
3661 else
3662 {
3663 /* For HSA v4+. */
3664 int xnack, sramecc;
3665
3666 xnack = e_flags & EF_AMDGPU_FEATURE_XNACK_V4;
3667 switch (xnack)
3668 {
3669 case EF_AMDGPU_FEATURE_XNACK_UNSUPPORTED_V4:
3670 break;
3671
3672 case EF_AMDGPU_FEATURE_XNACK_ANY_V4:
3673 strcat (buf, ", xnack any");
3674 break;
3675
3676 case EF_AMDGPU_FEATURE_XNACK_OFF_V4:
3677 strcat (buf, ", xnack off");
3678 break;
3679
3680 case EF_AMDGPU_FEATURE_XNACK_ON_V4:
3681 strcat (buf, ", xnack on");
3682 break;
3683
3684 default:
3685 sprintf (buf, _(", <unknown xnack value: %#x>"), xnack);
3686 break;
3687 }
3688
3689 buf += strlen (buf);
3690 e_flags &= ~EF_AMDGPU_FEATURE_XNACK_V4;
3691
3692 sramecc = e_flags & EF_AMDGPU_FEATURE_SRAMECC_V4;
3693 switch (sramecc)
3694 {
3695 case EF_AMDGPU_FEATURE_SRAMECC_UNSUPPORTED_V4:
3696 break;
3697
3698 case EF_AMDGPU_FEATURE_SRAMECC_ANY_V4:
3699 strcat (buf, ", sramecc any");
3700 break;
3701
3702 case EF_AMDGPU_FEATURE_SRAMECC_OFF_V4:
3703 strcat (buf, ", sramecc off");
3704 break;
3705
3706 case EF_AMDGPU_FEATURE_SRAMECC_ON_V4:
3707 strcat (buf, ", sramecc on");
3708 break;
3709
3710 default:
3711 sprintf (buf, _(", <unknown sramecc value: %#x>"), sramecc);
3712 break;
3713 }
3714
3715 buf += strlen (buf);
3716 e_flags &= ~EF_AMDGPU_FEATURE_SRAMECC_V4;
3717 }
3718
3719 if (e_flags != 0)
3720 sprintf (buf, _(", unknown flags bits: %#x"), e_flags);
3721 }
3722
3723 static char *
3724 get_machine_flags (Filedata * filedata, unsigned e_flags, unsigned e_machine)
3725 {
3726 static char buf[1024];
3727
3728 buf[0] = '\0';
3729
3730 if (e_flags)
3731 {
3732 switch (e_machine)
3733 {
3734 default:
3735 break;
3736
3737 case EM_ARC_COMPACT2:
3738 case EM_ARC_COMPACT:
3739 decode_ARC_machine_flags (e_flags, e_machine, buf);
3740 break;
3741
3742 case EM_ARM:
3743 decode_ARM_machine_flags (e_flags, buf);
3744 break;
3745
3746 case EM_AVR:
3747 decode_AVR_machine_flags (e_flags, buf, sizeof buf);
3748 break;
3749
3750 case EM_BLACKFIN:
3751 if (e_flags & EF_BFIN_PIC)
3752 strcat (buf, ", PIC");
3753
3754 if (e_flags & EF_BFIN_FDPIC)
3755 strcat (buf, ", FDPIC");
3756
3757 if (e_flags & EF_BFIN_CODE_IN_L1)
3758 strcat (buf, ", code in L1");
3759
3760 if (e_flags & EF_BFIN_DATA_IN_L1)
3761 strcat (buf, ", data in L1");
3762
3763 break;
3764
3765 case EM_CYGNUS_FRV:
3766 switch (e_flags & EF_FRV_CPU_MASK)
3767 {
3768 case EF_FRV_CPU_GENERIC:
3769 break;
3770
3771 default:
3772 strcat (buf, ", fr???");
3773 break;
3774
3775 case EF_FRV_CPU_FR300:
3776 strcat (buf, ", fr300");
3777 break;
3778
3779 case EF_FRV_CPU_FR400:
3780 strcat (buf, ", fr400");
3781 break;
3782 case EF_FRV_CPU_FR405:
3783 strcat (buf, ", fr405");
3784 break;
3785
3786 case EF_FRV_CPU_FR450:
3787 strcat (buf, ", fr450");
3788 break;
3789
3790 case EF_FRV_CPU_FR500:
3791 strcat (buf, ", fr500");
3792 break;
3793 case EF_FRV_CPU_FR550:
3794 strcat (buf, ", fr550");
3795 break;
3796
3797 case EF_FRV_CPU_SIMPLE:
3798 strcat (buf, ", simple");
3799 break;
3800 case EF_FRV_CPU_TOMCAT:
3801 strcat (buf, ", tomcat");
3802 break;
3803 }
3804 break;
3805
3806 case EM_68K:
3807 if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
3808 strcat (buf, ", m68000");
3809 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
3810 strcat (buf, ", cpu32");
3811 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
3812 strcat (buf, ", fido_a");
3813 else
3814 {
3815 char const * isa = _("unknown");
3816 char const * mac = _("unknown mac");
3817 char const * additional = NULL;
3818
3819 switch (e_flags & EF_M68K_CF_ISA_MASK)
3820 {
3821 case EF_M68K_CF_ISA_A_NODIV:
3822 isa = "A";
3823 additional = ", nodiv";
3824 break;
3825 case EF_M68K_CF_ISA_A:
3826 isa = "A";
3827 break;
3828 case EF_M68K_CF_ISA_A_PLUS:
3829 isa = "A+";
3830 break;
3831 case EF_M68K_CF_ISA_B_NOUSP:
3832 isa = "B";
3833 additional = ", nousp";
3834 break;
3835 case EF_M68K_CF_ISA_B:
3836 isa = "B";
3837 break;
3838 case EF_M68K_CF_ISA_C:
3839 isa = "C";
3840 break;
3841 case EF_M68K_CF_ISA_C_NODIV:
3842 isa = "C";
3843 additional = ", nodiv";
3844 break;
3845 }
3846 strcat (buf, ", cf, isa ");
3847 strcat (buf, isa);
3848 if (additional)
3849 strcat (buf, additional);
3850 if (e_flags & EF_M68K_CF_FLOAT)
3851 strcat (buf, ", float");
3852 switch (e_flags & EF_M68K_CF_MAC_MASK)
3853 {
3854 case 0:
3855 mac = NULL;
3856 break;
3857 case EF_M68K_CF_MAC:
3858 mac = "mac";
3859 break;
3860 case EF_M68K_CF_EMAC:
3861 mac = "emac";
3862 break;
3863 case EF_M68K_CF_EMAC_B:
3864 mac = "emac_b";
3865 break;
3866 }
3867 if (mac)
3868 {
3869 strcat (buf, ", ");
3870 strcat (buf, mac);
3871 }
3872 }
3873 break;
3874
3875 case EM_AMDGPU:
3876 decode_AMDGPU_machine_flags (filedata, e_flags, buf);
3877 break;
3878
3879 case EM_CYGNUS_MEP:
3880 switch (e_flags & EF_MEP_CPU_MASK)
3881 {
3882 case EF_MEP_CPU_MEP: strcat (buf, ", generic MeP"); break;
3883 case EF_MEP_CPU_C2: strcat (buf, ", MeP C2"); break;
3884 case EF_MEP_CPU_C3: strcat (buf, ", MeP C3"); break;
3885 case EF_MEP_CPU_C4: strcat (buf, ", MeP C4"); break;
3886 case EF_MEP_CPU_C5: strcat (buf, ", MeP C5"); break;
3887 case EF_MEP_CPU_H1: strcat (buf, ", MeP H1"); break;
3888 default: strcat (buf, _(", <unknown MeP cpu type>")); break;
3889 }
3890
3891 switch (e_flags & EF_MEP_COP_MASK)
3892 {
3893 case EF_MEP_COP_NONE: break;
3894 case EF_MEP_COP_AVC: strcat (buf, ", AVC coprocessor"); break;
3895 case EF_MEP_COP_AVC2: strcat (buf, ", AVC2 coprocessor"); break;
3896 case EF_MEP_COP_FMAX: strcat (buf, ", FMAX coprocessor"); break;
3897 case EF_MEP_COP_IVC2: strcat (buf, ", IVC2 coprocessor"); break;
3898 default: strcat (buf, _("<unknown MeP copro type>")); break;
3899 }
3900
3901 if (e_flags & EF_MEP_LIBRARY)
3902 strcat (buf, ", Built for Library");
3903
3904 if (e_flags & EF_MEP_INDEX_MASK)
3905 sprintf (buf + strlen (buf), ", Configuration Index: %#x",
3906 e_flags & EF_MEP_INDEX_MASK);
3907
3908 if (e_flags & ~ EF_MEP_ALL_FLAGS)
3909 sprintf (buf + strlen (buf), _(", unknown flags bits: %#x"),
3910 e_flags & ~ EF_MEP_ALL_FLAGS);
3911 break;
3912
3913 case EM_PPC:
3914 if (e_flags & EF_PPC_EMB)
3915 strcat (buf, ", emb");
3916
3917 if (e_flags & EF_PPC_RELOCATABLE)
3918 strcat (buf, _(", relocatable"));
3919
3920 if (e_flags & EF_PPC_RELOCATABLE_LIB)
3921 strcat (buf, _(", relocatable-lib"));
3922 break;
3923
3924 case EM_PPC64:
3925 if (e_flags & EF_PPC64_ABI)
3926 {
3927 char abi[] = ", abiv0";
3928
3929 abi[6] += e_flags & EF_PPC64_ABI;
3930 strcat (buf, abi);
3931 }
3932 break;
3933
3934 case EM_V800:
3935 if ((e_flags & EF_RH850_ABI) == EF_RH850_ABI)
3936 strcat (buf, ", RH850 ABI");
3937
3938 if (e_flags & EF_V800_850E3)
3939 strcat (buf, ", V3 architecture");
3940
3941 if ((e_flags & (EF_RH850_FPU_DOUBLE | EF_RH850_FPU_SINGLE)) == 0)
3942 strcat (buf, ", FPU not used");
3943
3944 if ((e_flags & (EF_RH850_REGMODE22 | EF_RH850_REGMODE32)) == 0)
3945 strcat (buf, ", regmode: COMMON");
3946
3947 if ((e_flags & (EF_RH850_GP_FIX | EF_RH850_GP_NOFIX)) == 0)
3948 strcat (buf, ", r4 not used");
3949
3950 if ((e_flags & (EF_RH850_EP_FIX | EF_RH850_EP_NOFIX)) == 0)
3951 strcat (buf, ", r30 not used");
3952
3953 if ((e_flags & (EF_RH850_TP_FIX | EF_RH850_TP_NOFIX)) == 0)
3954 strcat (buf, ", r5 not used");
3955
3956 if ((e_flags & (EF_RH850_REG2_RESERVE | EF_RH850_REG2_NORESERVE)) == 0)
3957 strcat (buf, ", r2 not used");
3958
3959 for (e_flags &= 0xFFFF; e_flags; e_flags &= ~ (e_flags & - e_flags))
3960 {
3961 switch (e_flags & - e_flags)
3962 {
3963 case EF_RH850_FPU_DOUBLE: strcat (buf, ", double precision FPU"); break;
3964 case EF_RH850_FPU_SINGLE: strcat (buf, ", single precision FPU"); break;
3965 case EF_RH850_REGMODE22: strcat (buf, ", regmode:22"); break;
3966 case EF_RH850_REGMODE32: strcat (buf, ", regmode:23"); break;
3967 case EF_RH850_GP_FIX: strcat (buf, ", r4 fixed"); break;
3968 case EF_RH850_GP_NOFIX: strcat (buf, ", r4 free"); break;
3969 case EF_RH850_EP_FIX: strcat (buf, ", r30 fixed"); break;
3970 case EF_RH850_EP_NOFIX: strcat (buf, ", r30 free"); break;
3971 case EF_RH850_TP_FIX: strcat (buf, ", r5 fixed"); break;
3972 case EF_RH850_TP_NOFIX: strcat (buf, ", r5 free"); break;
3973 case EF_RH850_REG2_RESERVE: strcat (buf, ", r2 fixed"); break;
3974 case EF_RH850_REG2_NORESERVE: strcat (buf, ", r2 free"); break;
3975 default: break;
3976 }
3977 }
3978 break;
3979
3980 case EM_V850:
3981 case EM_CYGNUS_V850:
3982 switch (e_flags & EF_V850_ARCH)
3983 {
3984 case E_V850E3V5_ARCH:
3985 strcat (buf, ", v850e3v5");
3986 break;
3987 case E_V850E2V3_ARCH:
3988 strcat (buf, ", v850e2v3");
3989 break;
3990 case E_V850E2_ARCH:
3991 strcat (buf, ", v850e2");
3992 break;
3993 case E_V850E1_ARCH:
3994 strcat (buf, ", v850e1");
3995 break;
3996 case E_V850E_ARCH:
3997 strcat (buf, ", v850e");
3998 break;
3999 case E_V850_ARCH:
4000 strcat (buf, ", v850");
4001 break;
4002 default:
4003 strcat (buf, _(", unknown v850 architecture variant"));
4004 break;
4005 }
4006 break;
4007
4008 case EM_M32R:
4009 case EM_CYGNUS_M32R:
4010 if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
4011 strcat (buf, ", m32r");
4012 break;
4013
4014 case EM_MIPS:
4015 case EM_MIPS_RS3_LE:
4016 if (e_flags & EF_MIPS_NOREORDER)
4017 strcat (buf, ", noreorder");
4018
4019 if (e_flags & EF_MIPS_PIC)
4020 strcat (buf, ", pic");
4021
4022 if (e_flags & EF_MIPS_CPIC)
4023 strcat (buf, ", cpic");
4024
4025 if (e_flags & EF_MIPS_UCODE)
4026 strcat (buf, ", ugen_reserved");
4027
4028 if (e_flags & EF_MIPS_ABI2)
4029 strcat (buf, ", abi2");
4030
4031 if (e_flags & EF_MIPS_OPTIONS_FIRST)
4032 strcat (buf, ", odk first");
4033
4034 if (e_flags & EF_MIPS_32BITMODE)
4035 strcat (buf, ", 32bitmode");
4036
4037 if (e_flags & EF_MIPS_NAN2008)
4038 strcat (buf, ", nan2008");
4039
4040 if (e_flags & EF_MIPS_FP64)
4041 strcat (buf, ", fp64");
4042
4043 switch ((e_flags & EF_MIPS_MACH))
4044 {
4045 case E_MIPS_MACH_3900: strcat (buf, ", 3900"); break;
4046 case E_MIPS_MACH_4010: strcat (buf, ", 4010"); break;
4047 case E_MIPS_MACH_4100: strcat (buf, ", 4100"); break;
4048 case E_MIPS_MACH_4111: strcat (buf, ", 4111"); break;
4049 case E_MIPS_MACH_4120: strcat (buf, ", 4120"); break;
4050 case E_MIPS_MACH_4650: strcat (buf, ", 4650"); break;
4051 case E_MIPS_MACH_5400: strcat (buf, ", 5400"); break;
4052 case E_MIPS_MACH_5500: strcat (buf, ", 5500"); break;
4053 case E_MIPS_MACH_5900: strcat (buf, ", 5900"); break;
4054 case E_MIPS_MACH_SB1: strcat (buf, ", sb1"); break;
4055 case E_MIPS_MACH_9000: strcat (buf, ", 9000"); break;
4056 case E_MIPS_MACH_LS2E: strcat (buf, ", loongson-2e"); break;
4057 case E_MIPS_MACH_LS2F: strcat (buf, ", loongson-2f"); break;
4058 case E_MIPS_MACH_GS464: strcat (buf, ", gs464"); break;
4059 case E_MIPS_MACH_GS464E: strcat (buf, ", gs464e"); break;
4060 case E_MIPS_MACH_GS264E: strcat (buf, ", gs264e"); break;
4061 case E_MIPS_MACH_OCTEON: strcat (buf, ", octeon"); break;
4062 case E_MIPS_MACH_OCTEON2: strcat (buf, ", octeon2"); break;
4063 case E_MIPS_MACH_OCTEON3: strcat (buf, ", octeon3"); break;
4064 case E_MIPS_MACH_XLR: strcat (buf, ", xlr"); break;
4065 case E_MIPS_MACH_IAMR2: strcat (buf, ", interaptiv-mr2"); break;
4066 case 0:
4067 /* We simply ignore the field in this case to avoid confusion:
4068 MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
4069 extension. */
4070 break;
4071 default: strcat (buf, _(", unknown CPU")); break;
4072 }
4073
4074 switch ((e_flags & EF_MIPS_ABI))
4075 {
4076 case E_MIPS_ABI_O32: strcat (buf, ", o32"); break;
4077 case E_MIPS_ABI_O64: strcat (buf, ", o64"); break;
4078 case E_MIPS_ABI_EABI32: strcat (buf, ", eabi32"); break;
4079 case E_MIPS_ABI_EABI64: strcat (buf, ", eabi64"); break;
4080 case 0:
4081 /* We simply ignore the field in this case to avoid confusion:
4082 MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
4083 This means it is likely to be an o32 file, but not for
4084 sure. */
4085 break;
4086 default: strcat (buf, _(", unknown ABI")); break;
4087 }
4088
4089 if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
4090 strcat (buf, ", mdmx");
4091
4092 if (e_flags & EF_MIPS_ARCH_ASE_M16)
4093 strcat (buf, ", mips16");
4094
4095 if (e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
4096 strcat (buf, ", micromips");
4097
4098 switch ((e_flags & EF_MIPS_ARCH))
4099 {
4100 case E_MIPS_ARCH_1: strcat (buf, ", mips1"); break;
4101 case E_MIPS_ARCH_2: strcat (buf, ", mips2"); break;
4102 case E_MIPS_ARCH_3: strcat (buf, ", mips3"); break;
4103 case E_MIPS_ARCH_4: strcat (buf, ", mips4"); break;
4104 case E_MIPS_ARCH_5: strcat (buf, ", mips5"); break;
4105 case E_MIPS_ARCH_32: strcat (buf, ", mips32"); break;
4106 case E_MIPS_ARCH_32R2: strcat (buf, ", mips32r2"); break;
4107 case E_MIPS_ARCH_32R6: strcat (buf, ", mips32r6"); break;
4108 case E_MIPS_ARCH_64: strcat (buf, ", mips64"); break;
4109 case E_MIPS_ARCH_64R2: strcat (buf, ", mips64r2"); break;
4110 case E_MIPS_ARCH_64R6: strcat (buf, ", mips64r6"); break;
4111 default: strcat (buf, _(", unknown ISA")); break;
4112 }
4113 break;
4114
4115 case EM_NDS32:
4116 decode_NDS32_machine_flags (e_flags, buf, sizeof buf);
4117 break;
4118
4119 case EM_NFP:
4120 switch (EF_NFP_MACH (e_flags))
4121 {
4122 case E_NFP_MACH_3200:
4123 strcat (buf, ", NFP-32xx");
4124 break;
4125 case E_NFP_MACH_6000:
4126 strcat (buf, ", NFP-6xxx");
4127 break;
4128 }
4129 break;
4130
4131 case EM_RISCV:
4132 if (e_flags & EF_RISCV_RVC)
4133 strcat (buf, ", RVC");
4134
4135 if (e_flags & EF_RISCV_RVE)
4136 strcat (buf, ", RVE");
4137
4138 switch (e_flags & EF_RISCV_FLOAT_ABI)
4139 {
4140 case EF_RISCV_FLOAT_ABI_SOFT:
4141 strcat (buf, ", soft-float ABI");
4142 break;
4143
4144 case EF_RISCV_FLOAT_ABI_SINGLE:
4145 strcat (buf, ", single-float ABI");
4146 break;
4147
4148 case EF_RISCV_FLOAT_ABI_DOUBLE:
4149 strcat (buf, ", double-float ABI");
4150 break;
4151
4152 case EF_RISCV_FLOAT_ABI_QUAD:
4153 strcat (buf, ", quad-float ABI");
4154 break;
4155 }
4156 break;
4157
4158 case EM_SH:
4159 switch ((e_flags & EF_SH_MACH_MASK))
4160 {
4161 case EF_SH1: strcat (buf, ", sh1"); break;
4162 case EF_SH2: strcat (buf, ", sh2"); break;
4163 case EF_SH3: strcat (buf, ", sh3"); break;
4164 case EF_SH_DSP: strcat (buf, ", sh-dsp"); break;
4165 case EF_SH3_DSP: strcat (buf, ", sh3-dsp"); break;
4166 case EF_SH4AL_DSP: strcat (buf, ", sh4al-dsp"); break;
4167 case EF_SH3E: strcat (buf, ", sh3e"); break;
4168 case EF_SH4: strcat (buf, ", sh4"); break;
4169 case EF_SH5: strcat (buf, ", sh5"); break;
4170 case EF_SH2E: strcat (buf, ", sh2e"); break;
4171 case EF_SH4A: strcat (buf, ", sh4a"); break;
4172 case EF_SH2A: strcat (buf, ", sh2a"); break;
4173 case EF_SH4_NOFPU: strcat (buf, ", sh4-nofpu"); break;
4174 case EF_SH4A_NOFPU: strcat (buf, ", sh4a-nofpu"); break;
4175 case EF_SH2A_NOFPU: strcat (buf, ", sh2a-nofpu"); break;
4176 case EF_SH3_NOMMU: strcat (buf, ", sh3-nommu"); break;
4177 case EF_SH4_NOMMU_NOFPU: strcat (buf, ", sh4-nommu-nofpu"); break;
4178 case EF_SH2A_SH4_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh4-nommu-nofpu"); break;
4179 case EF_SH2A_SH3_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh3-nommu"); break;
4180 case EF_SH2A_SH4: strcat (buf, ", sh2a-or-sh4"); break;
4181 case EF_SH2A_SH3E: strcat (buf, ", sh2a-or-sh3e"); break;
4182 default: strcat (buf, _(", unknown ISA")); break;
4183 }
4184
4185 if (e_flags & EF_SH_PIC)
4186 strcat (buf, ", pic");
4187
4188 if (e_flags & EF_SH_FDPIC)
4189 strcat (buf, ", fdpic");
4190 break;
4191
4192 case EM_OR1K:
4193 if (e_flags & EF_OR1K_NODELAY)
4194 strcat (buf, ", no delay");
4195 break;
4196
4197 case EM_SPARCV9:
4198 if (e_flags & EF_SPARC_32PLUS)
4199 strcat (buf, ", v8+");
4200
4201 if (e_flags & EF_SPARC_SUN_US1)
4202 strcat (buf, ", ultrasparcI");
4203
4204 if (e_flags & EF_SPARC_SUN_US3)
4205 strcat (buf, ", ultrasparcIII");
4206
4207 if (e_flags & EF_SPARC_HAL_R1)
4208 strcat (buf, ", halr1");
4209
4210 if (e_flags & EF_SPARC_LEDATA)
4211 strcat (buf, ", ledata");
4212
4213 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
4214 strcat (buf, ", tso");
4215
4216 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
4217 strcat (buf, ", pso");
4218
4219 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
4220 strcat (buf, ", rmo");
4221 break;
4222
4223 case EM_PARISC:
4224 switch (e_flags & EF_PARISC_ARCH)
4225 {
4226 case EFA_PARISC_1_0:
4227 strcpy (buf, ", PA-RISC 1.0");
4228 break;
4229 case EFA_PARISC_1_1:
4230 strcpy (buf, ", PA-RISC 1.1");
4231 break;
4232 case EFA_PARISC_2_0:
4233 strcpy (buf, ", PA-RISC 2.0");
4234 break;
4235 default:
4236 break;
4237 }
4238 if (e_flags & EF_PARISC_TRAPNIL)
4239 strcat (buf, ", trapnil");
4240 if (e_flags & EF_PARISC_EXT)
4241 strcat (buf, ", ext");
4242 if (e_flags & EF_PARISC_LSB)
4243 strcat (buf, ", lsb");
4244 if (e_flags & EF_PARISC_WIDE)
4245 strcat (buf, ", wide");
4246 if (e_flags & EF_PARISC_NO_KABP)
4247 strcat (buf, ", no kabp");
4248 if (e_flags & EF_PARISC_LAZYSWAP)
4249 strcat (buf, ", lazyswap");
4250 break;
4251
4252 case EM_PJ:
4253 case EM_PJ_OLD:
4254 if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
4255 strcat (buf, ", new calling convention");
4256
4257 if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
4258 strcat (buf, ", gnu calling convention");
4259 break;
4260
4261 case EM_IA_64:
4262 if ((e_flags & EF_IA_64_ABI64))
4263 strcat (buf, ", 64-bit");
4264 else
4265 strcat (buf, ", 32-bit");
4266 if ((e_flags & EF_IA_64_REDUCEDFP))
4267 strcat (buf, ", reduced fp model");
4268 if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
4269 strcat (buf, ", no function descriptors, constant gp");
4270 else if ((e_flags & EF_IA_64_CONS_GP))
4271 strcat (buf, ", constant gp");
4272 if ((e_flags & EF_IA_64_ABSOLUTE))
4273 strcat (buf, ", absolute");
4274 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
4275 {
4276 if ((e_flags & EF_IA_64_VMS_LINKAGES))
4277 strcat (buf, ", vms_linkages");
4278 switch ((e_flags & EF_IA_64_VMS_COMCOD))
4279 {
4280 case EF_IA_64_VMS_COMCOD_SUCCESS:
4281 break;
4282 case EF_IA_64_VMS_COMCOD_WARNING:
4283 strcat (buf, ", warning");
4284 break;
4285 case EF_IA_64_VMS_COMCOD_ERROR:
4286 strcat (buf, ", error");
4287 break;
4288 case EF_IA_64_VMS_COMCOD_ABORT:
4289 strcat (buf, ", abort");
4290 break;
4291 default:
4292 warn (_("Unrecognised IA64 VMS Command Code: %x\n"),
4293 e_flags & EF_IA_64_VMS_COMCOD);
4294 strcat (buf, ", <unknown>");
4295 }
4296 }
4297 break;
4298
4299 case EM_VAX:
4300 if ((e_flags & EF_VAX_NONPIC))
4301 strcat (buf, ", non-PIC");
4302 if ((e_flags & EF_VAX_DFLOAT))
4303 strcat (buf, ", D-Float");
4304 if ((e_flags & EF_VAX_GFLOAT))
4305 strcat (buf, ", G-Float");
4306 break;
4307
4308 case EM_VISIUM:
4309 if (e_flags & EF_VISIUM_ARCH_MCM)
4310 strcat (buf, ", mcm");
4311 else if (e_flags & EF_VISIUM_ARCH_MCM24)
4312 strcat (buf, ", mcm24");
4313 if (e_flags & EF_VISIUM_ARCH_GR6)
4314 strcat (buf, ", gr6");
4315 break;
4316
4317 case EM_RL78:
4318 switch (e_flags & E_FLAG_RL78_CPU_MASK)
4319 {
4320 case E_FLAG_RL78_ANY_CPU: break;
4321 case E_FLAG_RL78_G10: strcat (buf, ", G10"); break;
4322 case E_FLAG_RL78_G13: strcat (buf, ", G13"); break;
4323 case E_FLAG_RL78_G14: strcat (buf, ", G14"); break;
4324 }
4325 if (e_flags & E_FLAG_RL78_64BIT_DOUBLES)
4326 strcat (buf, ", 64-bit doubles");
4327 break;
4328
4329 case EM_RX:
4330 if (e_flags & E_FLAG_RX_64BIT_DOUBLES)
4331 strcat (buf, ", 64-bit doubles");
4332 if (e_flags & E_FLAG_RX_DSP)
4333 strcat (buf, ", dsp");
4334 if (e_flags & E_FLAG_RX_PID)
4335 strcat (buf, ", pid");
4336 if (e_flags & E_FLAG_RX_ABI)
4337 strcat (buf, ", RX ABI");
4338 if (e_flags & E_FLAG_RX_SINSNS_SET)
4339 strcat (buf, e_flags & E_FLAG_RX_SINSNS_YES
4340 ? ", uses String instructions" : ", bans String instructions");
4341 if (e_flags & E_FLAG_RX_V2)
4342 strcat (buf, ", V2");
4343 if (e_flags & E_FLAG_RX_V3)
4344 strcat (buf, ", V3");
4345 break;
4346
4347 case EM_S390:
4348 if (e_flags & EF_S390_HIGH_GPRS)
4349 strcat (buf, ", highgprs");
4350 break;
4351
4352 case EM_TI_C6000:
4353 if ((e_flags & EF_C6000_REL))
4354 strcat (buf, ", relocatable module");
4355 break;
4356
4357 case EM_MSP430:
4358 strcat (buf, _(": architecture variant: "));
4359 switch (e_flags & EF_MSP430_MACH)
4360 {
4361 case E_MSP430_MACH_MSP430x11: strcat (buf, "MSP430x11"); break;
4362 case E_MSP430_MACH_MSP430x11x1 : strcat (buf, "MSP430x11x1 "); break;
4363 case E_MSP430_MACH_MSP430x12: strcat (buf, "MSP430x12"); break;
4364 case E_MSP430_MACH_MSP430x13: strcat (buf, "MSP430x13"); break;
4365 case E_MSP430_MACH_MSP430x14: strcat (buf, "MSP430x14"); break;
4366 case E_MSP430_MACH_MSP430x15: strcat (buf, "MSP430x15"); break;
4367 case E_MSP430_MACH_MSP430x16: strcat (buf, "MSP430x16"); break;
4368 case E_MSP430_MACH_MSP430x31: strcat (buf, "MSP430x31"); break;
4369 case E_MSP430_MACH_MSP430x32: strcat (buf, "MSP430x32"); break;
4370 case E_MSP430_MACH_MSP430x33: strcat (buf, "MSP430x33"); break;
4371 case E_MSP430_MACH_MSP430x41: strcat (buf, "MSP430x41"); break;
4372 case E_MSP430_MACH_MSP430x42: strcat (buf, "MSP430x42"); break;
4373 case E_MSP430_MACH_MSP430x43: strcat (buf, "MSP430x43"); break;
4374 case E_MSP430_MACH_MSP430x44: strcat (buf, "MSP430x44"); break;
4375 case E_MSP430_MACH_MSP430X : strcat (buf, "MSP430X"); break;
4376 default:
4377 strcat (buf, _(": unknown")); break;
4378 }
4379
4380 if (e_flags & ~ EF_MSP430_MACH)
4381 strcat (buf, _(": unknown extra flag bits also present"));
4382 break;
4383
4384 case EM_Z80:
4385 switch (e_flags & EF_Z80_MACH_MSK)
4386 {
4387 case EF_Z80_MACH_Z80: strcat (buf, ", Z80"); break;
4388 case EF_Z80_MACH_Z180: strcat (buf, ", Z180"); break;
4389 case EF_Z80_MACH_R800: strcat (buf, ", R800"); break;
4390 case EF_Z80_MACH_EZ80_Z80: strcat (buf, ", EZ80"); break;
4391 case EF_Z80_MACH_EZ80_ADL: strcat (buf, ", EZ80, ADL"); break;
4392 case EF_Z80_MACH_GBZ80: strcat (buf, ", GBZ80"); break;
4393 case EF_Z80_MACH_Z80N: strcat (buf, ", Z80N"); break;
4394 default:
4395 strcat (buf, _(", unknown")); break;
4396 }
4397 break;
4398 case EM_LOONGARCH:
4399 if (EF_LOONGARCH_IS_LP64 (e_flags))
4400 strcat (buf, ", LP64");
4401 else if (EF_LOONGARCH_IS_ILP32 (e_flags))
4402 strcat (buf, ", ILP32");
4403
4404 if (EF_LOONGARCH_IS_SOFT_FLOAT (e_flags))
4405 strcat (buf, ", SOFT-FLOAT");
4406 else if (EF_LOONGARCH_IS_SINGLE_FLOAT (e_flags))
4407 strcat (buf, ", SINGLE-FLOAT");
4408 else if (EF_LOONGARCH_IS_DOUBLE_FLOAT (e_flags))
4409 strcat (buf, ", DOUBLE-FLOAT");
4410
4411 break;
4412 }
4413 }
4414
4415 return buf;
4416 }
4417
4418 static const char *
4419 get_osabi_name (Filedata * filedata, unsigned int osabi)
4420 {
4421 static char buff[32];
4422
4423 switch (osabi)
4424 {
4425 case ELFOSABI_NONE: return "UNIX - System V";
4426 case ELFOSABI_HPUX: return "UNIX - HP-UX";
4427 case ELFOSABI_NETBSD: return "UNIX - NetBSD";
4428 case ELFOSABI_GNU: return "UNIX - GNU";
4429 case ELFOSABI_SOLARIS: return "UNIX - Solaris";
4430 case ELFOSABI_AIX: return "UNIX - AIX";
4431 case ELFOSABI_IRIX: return "UNIX - IRIX";
4432 case ELFOSABI_FREEBSD: return "UNIX - FreeBSD";
4433 case ELFOSABI_TRU64: return "UNIX - TRU64";
4434 case ELFOSABI_MODESTO: return "Novell - Modesto";
4435 case ELFOSABI_OPENBSD: return "UNIX - OpenBSD";
4436 case ELFOSABI_OPENVMS: return "VMS - OpenVMS";
4437 case ELFOSABI_NSK: return "HP - Non-Stop Kernel";
4438 case ELFOSABI_AROS: return "AROS";
4439 case ELFOSABI_FENIXOS: return "FenixOS";
4440 case ELFOSABI_CLOUDABI: return "Nuxi CloudABI";
4441 case ELFOSABI_OPENVOS: return "Stratus Technologies OpenVOS";
4442 default:
4443 if (osabi >= 64)
4444 switch (filedata->file_header.e_machine)
4445 {
4446 case EM_AMDGPU:
4447 switch (osabi)
4448 {
4449 case ELFOSABI_AMDGPU_HSA: return "AMD HSA";
4450 case ELFOSABI_AMDGPU_PAL: return "AMD PAL";
4451 case ELFOSABI_AMDGPU_MESA3D: return "AMD Mesa3D";
4452 default:
4453 break;
4454 }
4455 break;
4456
4457 case EM_ARM:
4458 switch (osabi)
4459 {
4460 case ELFOSABI_ARM: return "ARM";
4461 case ELFOSABI_ARM_FDPIC: return "ARM FDPIC";
4462 default:
4463 break;
4464 }
4465 break;
4466
4467 case EM_MSP430:
4468 case EM_MSP430_OLD:
4469 case EM_VISIUM:
4470 switch (osabi)
4471 {
4472 case ELFOSABI_STANDALONE: return _("Standalone App");
4473 default:
4474 break;
4475 }
4476 break;
4477
4478 case EM_TI_C6000:
4479 switch (osabi)
4480 {
4481 case ELFOSABI_C6000_ELFABI: return _("Bare-metal C6000");
4482 case ELFOSABI_C6000_LINUX: return "Linux C6000";
4483 default:
4484 break;
4485 }
4486 break;
4487
4488 default:
4489 break;
4490 }
4491 snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
4492 return buff;
4493 }
4494 }
4495
4496 static const char *
4497 get_aarch64_segment_type (unsigned long type)
4498 {
4499 switch (type)
4500 {
4501 case PT_AARCH64_ARCHEXT: return "AARCH64_ARCHEXT";
4502 default: return NULL;
4503 }
4504 }
4505
4506 static const char *
4507 get_arm_segment_type (unsigned long type)
4508 {
4509 switch (type)
4510 {
4511 case PT_ARM_EXIDX: return "EXIDX";
4512 default: return NULL;
4513 }
4514 }
4515
4516 static const char *
4517 get_s390_segment_type (unsigned long type)
4518 {
4519 switch (type)
4520 {
4521 case PT_S390_PGSTE: return "S390_PGSTE";
4522 default: return NULL;
4523 }
4524 }
4525
4526 static const char *
4527 get_mips_segment_type (unsigned long type)
4528 {
4529 switch (type)
4530 {
4531 case PT_MIPS_REGINFO: return "REGINFO";
4532 case PT_MIPS_RTPROC: return "RTPROC";
4533 case PT_MIPS_OPTIONS: return "OPTIONS";
4534 case PT_MIPS_ABIFLAGS: return "ABIFLAGS";
4535 default: return NULL;
4536 }
4537 }
4538
4539 static const char *
4540 get_parisc_segment_type (unsigned long type)
4541 {
4542 switch (type)
4543 {
4544 case PT_PARISC_ARCHEXT: return "PARISC_ARCHEXT";
4545 case PT_PARISC_UNWIND: return "PARISC_UNWIND";
4546 case PT_PARISC_WEAKORDER: return "PARISC_WEAKORDER";
4547 default: return NULL;
4548 }
4549 }
4550
4551 static const char *
4552 get_ia64_segment_type (unsigned long type)
4553 {
4554 switch (type)
4555 {
4556 case PT_IA_64_ARCHEXT: return "IA_64_ARCHEXT";
4557 case PT_IA_64_UNWIND: return "IA_64_UNWIND";
4558 default: return NULL;
4559 }
4560 }
4561
4562 static const char *
4563 get_tic6x_segment_type (unsigned long type)
4564 {
4565 switch (type)
4566 {
4567 case PT_C6000_PHATTR: return "C6000_PHATTR";
4568 default: return NULL;
4569 }
4570 }
4571
4572 static const char *
4573 get_riscv_segment_type (unsigned long type)
4574 {
4575 switch (type)
4576 {
4577 case PT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
4578 default: return NULL;
4579 }
4580 }
4581
4582 static const char *
4583 get_hpux_segment_type (unsigned long type, unsigned e_machine)
4584 {
4585 if (e_machine == EM_PARISC)
4586 switch (type)
4587 {
4588 case PT_HP_TLS: return "HP_TLS";
4589 case PT_HP_CORE_NONE: return "HP_CORE_NONE";
4590 case PT_HP_CORE_VERSION: return "HP_CORE_VERSION";
4591 case PT_HP_CORE_KERNEL: return "HP_CORE_KERNEL";
4592 case PT_HP_CORE_COMM: return "HP_CORE_COMM";
4593 case PT_HP_CORE_PROC: return "HP_CORE_PROC";
4594 case PT_HP_CORE_LOADABLE: return "HP_CORE_LOADABLE";
4595 case PT_HP_CORE_STACK: return "HP_CORE_STACK";
4596 case PT_HP_CORE_SHM: return "HP_CORE_SHM";
4597 case PT_HP_CORE_MMF: return "HP_CORE_MMF";
4598 case PT_HP_PARALLEL: return "HP_PARALLEL";
4599 case PT_HP_FASTBIND: return "HP_FASTBIND";
4600 case PT_HP_OPT_ANNOT: return "HP_OPT_ANNOT";
4601 case PT_HP_HSL_ANNOT: return "HP_HSL_ANNOT";
4602 case PT_HP_STACK: return "HP_STACK";
4603 case PT_HP_CORE_UTSNAME: return "HP_CORE_UTSNAME";
4604 default: return NULL;
4605 }
4606
4607 if (e_machine == EM_IA_64)
4608 switch (type)
4609 {
4610 case PT_HP_TLS: return "HP_TLS";
4611 case PT_IA_64_HP_OPT_ANOT: return "HP_OPT_ANNOT";
4612 case PT_IA_64_HP_HSL_ANOT: return "HP_HSL_ANNOT";
4613 case PT_IA_64_HP_STACK: return "HP_STACK";
4614 default: return NULL;
4615 }
4616
4617 return NULL;
4618 }
4619
4620 static const char *
4621 get_solaris_segment_type (unsigned long type)
4622 {
4623 switch (type)
4624 {
4625 case 0x6464e550: return "PT_SUNW_UNWIND";
4626 case 0x6474e550: return "PT_SUNW_EH_FRAME";
4627 case 0x6ffffff7: return "PT_LOSUNW";
4628 case 0x6ffffffa: return "PT_SUNWBSS";
4629 case 0x6ffffffb: return "PT_SUNWSTACK";
4630 case 0x6ffffffc: return "PT_SUNWDTRACE";
4631 case 0x6ffffffd: return "PT_SUNWCAP";
4632 case 0x6fffffff: return "PT_HISUNW";
4633 default: return NULL;
4634 }
4635 }
4636
4637 static const char *
4638 get_segment_type (Filedata * filedata, unsigned long p_type)
4639 {
4640 static char buff[32];
4641
4642 switch (p_type)
4643 {
4644 case PT_NULL: return "NULL";
4645 case PT_LOAD: return "LOAD";
4646 case PT_DYNAMIC: return "DYNAMIC";
4647 case PT_INTERP: return "INTERP";
4648 case PT_NOTE: return "NOTE";
4649 case PT_SHLIB: return "SHLIB";
4650 case PT_PHDR: return "PHDR";
4651 case PT_TLS: return "TLS";
4652 case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
4653 case PT_GNU_STACK: return "GNU_STACK";
4654 case PT_GNU_RELRO: return "GNU_RELRO";
4655 case PT_GNU_PROPERTY: return "GNU_PROPERTY";
4656
4657 case PT_OPENBSD_RANDOMIZE: return "OPENBSD_RANDOMIZE";
4658 case PT_OPENBSD_WXNEEDED: return "OPENBSD_WXNEEDED";
4659 case PT_OPENBSD_BOOTDATA: return "OPENBSD_BOOTDATA";
4660
4661 default:
4662 if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
4663 {
4664 const char * result;
4665
4666 switch (filedata->file_header.e_machine)
4667 {
4668 case EM_AARCH64:
4669 result = get_aarch64_segment_type (p_type);
4670 break;
4671 case EM_ARM:
4672 result = get_arm_segment_type (p_type);
4673 break;
4674 case EM_MIPS:
4675 case EM_MIPS_RS3_LE:
4676 result = get_mips_segment_type (p_type);
4677 break;
4678 case EM_PARISC:
4679 result = get_parisc_segment_type (p_type);
4680 break;
4681 case EM_IA_64:
4682 result = get_ia64_segment_type (p_type);
4683 break;
4684 case EM_TI_C6000:
4685 result = get_tic6x_segment_type (p_type);
4686 break;
4687 case EM_S390:
4688 case EM_S390_OLD:
4689 result = get_s390_segment_type (p_type);
4690 break;
4691 case EM_RISCV:
4692 result = get_riscv_segment_type (p_type);
4693 break;
4694 default:
4695 result = NULL;
4696 break;
4697 }
4698
4699 if (result != NULL)
4700 return result;
4701
4702 sprintf (buff, "LOPROC+%#lx", p_type - PT_LOPROC);
4703 }
4704 else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
4705 {
4706 const char * result = NULL;
4707
4708 switch (filedata->file_header.e_ident[EI_OSABI])
4709 {
4710 case ELFOSABI_GNU:
4711 case ELFOSABI_FREEBSD:
4712 if (p_type >= PT_GNU_MBIND_LO && p_type <= PT_GNU_MBIND_HI)
4713 {
4714 sprintf (buff, "GNU_MBIND+%#lx", p_type - PT_GNU_MBIND_LO);
4715 result = buff;
4716 }
4717 break;
4718 case ELFOSABI_HPUX:
4719 result = get_hpux_segment_type (p_type,
4720 filedata->file_header.e_machine);
4721 break;
4722 case ELFOSABI_SOLARIS:
4723 result = get_solaris_segment_type (p_type);
4724 break;
4725 default:
4726 break;
4727 }
4728 if (result != NULL)
4729 return result;
4730
4731 sprintf (buff, "LOOS+%#lx", p_type - PT_LOOS);
4732 }
4733 else
4734 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
4735
4736 return buff;
4737 }
4738 }
4739
4740 static const char *
4741 get_arc_section_type_name (unsigned int sh_type)
4742 {
4743 switch (sh_type)
4744 {
4745 case SHT_ARC_ATTRIBUTES: return "ARC_ATTRIBUTES";
4746 default:
4747 break;
4748 }
4749 return NULL;
4750 }
4751
4752 static const char *
4753 get_mips_section_type_name (unsigned int sh_type)
4754 {
4755 switch (sh_type)
4756 {
4757 case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
4758 case SHT_MIPS_MSYM: return "MIPS_MSYM";
4759 case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
4760 case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
4761 case SHT_MIPS_UCODE: return "MIPS_UCODE";
4762 case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
4763 case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
4764 case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
4765 case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
4766 case SHT_MIPS_RELD: return "MIPS_RELD";
4767 case SHT_MIPS_IFACE: return "MIPS_IFACE";
4768 case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
4769 case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
4770 case SHT_MIPS_SHDR: return "MIPS_SHDR";
4771 case SHT_MIPS_FDESC: return "MIPS_FDESC";
4772 case SHT_MIPS_EXTSYM: return "MIPS_EXTSYM";
4773 case SHT_MIPS_DENSE: return "MIPS_DENSE";
4774 case SHT_MIPS_PDESC: return "MIPS_PDESC";
4775 case SHT_MIPS_LOCSYM: return "MIPS_LOCSYM";
4776 case SHT_MIPS_AUXSYM: return "MIPS_AUXSYM";
4777 case SHT_MIPS_OPTSYM: return "MIPS_OPTSYM";
4778 case SHT_MIPS_LOCSTR: return "MIPS_LOCSTR";
4779 case SHT_MIPS_LINE: return "MIPS_LINE";
4780 case SHT_MIPS_RFDESC: return "MIPS_RFDESC";
4781 case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
4782 case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
4783 case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
4784 case SHT_MIPS_DWARF: return "MIPS_DWARF";
4785 case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
4786 case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
4787 case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
4788 case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
4789 case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
4790 case SHT_MIPS_XLATE: return "MIPS_XLATE";
4791 case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
4792 case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
4793 case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
4794 case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
4795 case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
4796 case SHT_MIPS_ABIFLAGS: return "MIPS_ABIFLAGS";
4797 case SHT_MIPS_XHASH: return "MIPS_XHASH";
4798 default:
4799 break;
4800 }
4801 return NULL;
4802 }
4803
4804 static const char *
4805 get_parisc_section_type_name (unsigned int sh_type)
4806 {
4807 switch (sh_type)
4808 {
4809 case SHT_PARISC_EXT: return "PARISC_EXT";
4810 case SHT_PARISC_UNWIND: return "PARISC_UNWIND";
4811 case SHT_PARISC_DOC: return "PARISC_DOC";
4812 case SHT_PARISC_ANNOT: return "PARISC_ANNOT";
4813 case SHT_PARISC_SYMEXTN: return "PARISC_SYMEXTN";
4814 case SHT_PARISC_STUBS: return "PARISC_STUBS";
4815 case SHT_PARISC_DLKM: return "PARISC_DLKM";
4816 default: return NULL;
4817 }
4818 }
4819
4820 static const char *
4821 get_ia64_section_type_name (Filedata * filedata, unsigned int sh_type)
4822 {
4823 /* If the top 8 bits are 0x78 the next 8 are the os/abi ID. */
4824 if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
4825 return get_osabi_name (filedata, (sh_type & 0x00FF0000) >> 16);
4826
4827 switch (sh_type)
4828 {
4829 case SHT_IA_64_EXT: return "IA_64_EXT";
4830 case SHT_IA_64_UNWIND: return "IA_64_UNWIND";
4831 case SHT_IA_64_PRIORITY_INIT: return "IA_64_PRIORITY_INIT";
4832 case SHT_IA_64_VMS_TRACE: return "VMS_TRACE";
4833 case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
4834 case SHT_IA_64_VMS_DEBUG: return "VMS_DEBUG";
4835 case SHT_IA_64_VMS_DEBUG_STR: return "VMS_DEBUG_STR";
4836 case SHT_IA_64_VMS_LINKAGES: return "VMS_LINKAGES";
4837 case SHT_IA_64_VMS_SYMBOL_VECTOR: return "VMS_SYMBOL_VECTOR";
4838 case SHT_IA_64_VMS_FIXUP: return "VMS_FIXUP";
4839 default:
4840 break;
4841 }
4842 return NULL;
4843 }
4844
4845 static const char *
4846 get_x86_64_section_type_name (unsigned int sh_type)
4847 {
4848 switch (sh_type)
4849 {
4850 case SHT_X86_64_UNWIND: return "X86_64_UNWIND";
4851 default: return NULL;
4852 }
4853 }
4854
4855 static const char *
4856 get_aarch64_section_type_name (unsigned int sh_type)
4857 {
4858 switch (sh_type)
4859 {
4860 case SHT_AARCH64_ATTRIBUTES: return "AARCH64_ATTRIBUTES";
4861 default: return NULL;
4862 }
4863 }
4864
4865 static const char *
4866 get_arm_section_type_name (unsigned int sh_type)
4867 {
4868 switch (sh_type)
4869 {
4870 case SHT_ARM_EXIDX: return "ARM_EXIDX";
4871 case SHT_ARM_PREEMPTMAP: return "ARM_PREEMPTMAP";
4872 case SHT_ARM_ATTRIBUTES: return "ARM_ATTRIBUTES";
4873 case SHT_ARM_DEBUGOVERLAY: return "ARM_DEBUGOVERLAY";
4874 case SHT_ARM_OVERLAYSECTION: return "ARM_OVERLAYSECTION";
4875 default: return NULL;
4876 }
4877 }
4878
4879 static const char *
4880 get_tic6x_section_type_name (unsigned int sh_type)
4881 {
4882 switch (sh_type)
4883 {
4884 case SHT_C6000_UNWIND: return "C6000_UNWIND";
4885 case SHT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
4886 case SHT_C6000_ATTRIBUTES: return "C6000_ATTRIBUTES";
4887 case SHT_TI_ICODE: return "TI_ICODE";
4888 case SHT_TI_XREF: return "TI_XREF";
4889 case SHT_TI_HANDLER: return "TI_HANDLER";
4890 case SHT_TI_INITINFO: return "TI_INITINFO";
4891 case SHT_TI_PHATTRS: return "TI_PHATTRS";
4892 default: return NULL;
4893 }
4894 }
4895
4896 static const char *
4897 get_msp430_section_type_name (unsigned int sh_type)
4898 {
4899 switch (sh_type)
4900 {
4901 case SHT_MSP430_SEC_FLAGS: return "MSP430_SEC_FLAGS";
4902 case SHT_MSP430_SYM_ALIASES: return "MSP430_SYM_ALIASES";
4903 case SHT_MSP430_ATTRIBUTES: return "MSP430_ATTRIBUTES";
4904 default: return NULL;
4905 }
4906 }
4907
4908 static const char *
4909 get_nfp_section_type_name (unsigned int sh_type)
4910 {
4911 switch (sh_type)
4912 {
4913 case SHT_NFP_MECONFIG: return "NFP_MECONFIG";
4914 case SHT_NFP_INITREG: return "NFP_INITREG";
4915 case SHT_NFP_UDEBUG: return "NFP_UDEBUG";
4916 default: return NULL;
4917 }
4918 }
4919
4920 static const char *
4921 get_v850_section_type_name (unsigned int sh_type)
4922 {
4923 switch (sh_type)
4924 {
4925 case SHT_V850_SCOMMON: return "V850 Small Common";
4926 case SHT_V850_TCOMMON: return "V850 Tiny Common";
4927 case SHT_V850_ZCOMMON: return "V850 Zero Common";
4928 case SHT_RENESAS_IOP: return "RENESAS IOP";
4929 case SHT_RENESAS_INFO: return "RENESAS INFO";
4930 default: return NULL;
4931 }
4932 }
4933
4934 static const char *
4935 get_riscv_section_type_name (unsigned int sh_type)
4936 {
4937 switch (sh_type)
4938 {
4939 case SHT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
4940 default: return NULL;
4941 }
4942 }
4943
4944 static const char *
4945 get_csky_section_type_name (unsigned int sh_type)
4946 {
4947 switch (sh_type)
4948 {
4949 case SHT_CSKY_ATTRIBUTES: return "CSKY_ATTRIBUTES";
4950 default: return NULL;
4951 }
4952 }
4953
4954 static const char *
4955 get_section_type_name (Filedata * filedata, unsigned int sh_type)
4956 {
4957 static char buff[32];
4958 const char * result;
4959
4960 switch (sh_type)
4961 {
4962 case SHT_NULL: return "NULL";
4963 case SHT_PROGBITS: return "PROGBITS";
4964 case SHT_SYMTAB: return "SYMTAB";
4965 case SHT_STRTAB: return "STRTAB";
4966 case SHT_RELA: return "RELA";
4967 case SHT_RELR: return "RELR";
4968 case SHT_HASH: return "HASH";
4969 case SHT_DYNAMIC: return "DYNAMIC";
4970 case SHT_NOTE: return "NOTE";
4971 case SHT_NOBITS: return "NOBITS";
4972 case SHT_REL: return "REL";
4973 case SHT_SHLIB: return "SHLIB";
4974 case SHT_DYNSYM: return "DYNSYM";
4975 case SHT_INIT_ARRAY: return "INIT_ARRAY";
4976 case SHT_FINI_ARRAY: return "FINI_ARRAY";
4977 case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
4978 case SHT_GNU_HASH: return "GNU_HASH";
4979 case SHT_GROUP: return "GROUP";
4980 case SHT_SYMTAB_SHNDX: return "SYMTAB SECTION INDICES";
4981 case SHT_GNU_verdef: return "VERDEF";
4982 case SHT_GNU_verneed: return "VERNEED";
4983 case SHT_GNU_versym: return "VERSYM";
4984 case 0x6ffffff0: return "VERSYM";
4985 case 0x6ffffffc: return "VERDEF";
4986 case 0x7ffffffd: return "AUXILIARY";
4987 case 0x7fffffff: return "FILTER";
4988 case SHT_GNU_LIBLIST: return "GNU_LIBLIST";
4989
4990 default:
4991 if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
4992 {
4993 switch (filedata->file_header.e_machine)
4994 {
4995 case EM_ARC:
4996 case EM_ARC_COMPACT:
4997 case EM_ARC_COMPACT2:
4998 result = get_arc_section_type_name (sh_type);
4999 break;
5000 case EM_MIPS:
5001 case EM_MIPS_RS3_LE:
5002 result = get_mips_section_type_name (sh_type);
5003 break;
5004 case EM_PARISC:
5005 result = get_parisc_section_type_name (sh_type);
5006 break;
5007 case EM_IA_64:
5008 result = get_ia64_section_type_name (filedata, sh_type);
5009 break;
5010 case EM_X86_64:
5011 case EM_L1OM:
5012 case EM_K1OM:
5013 result = get_x86_64_section_type_name (sh_type);
5014 break;
5015 case EM_AARCH64:
5016 result = get_aarch64_section_type_name (sh_type);
5017 break;
5018 case EM_ARM:
5019 result = get_arm_section_type_name (sh_type);
5020 break;
5021 case EM_TI_C6000:
5022 result = get_tic6x_section_type_name (sh_type);
5023 break;
5024 case EM_MSP430:
5025 result = get_msp430_section_type_name (sh_type);
5026 break;
5027 case EM_NFP:
5028 result = get_nfp_section_type_name (sh_type);
5029 break;
5030 case EM_V800:
5031 case EM_V850:
5032 case EM_CYGNUS_V850:
5033 result = get_v850_section_type_name (sh_type);
5034 break;
5035 case EM_RISCV:
5036 result = get_riscv_section_type_name (sh_type);
5037 break;
5038 case EM_CSKY:
5039 result = get_csky_section_type_name (sh_type);
5040 break;
5041 default:
5042 result = NULL;
5043 break;
5044 }
5045
5046 if (result != NULL)
5047 return result;
5048
5049 sprintf (buff, "LOPROC+%#x", sh_type - SHT_LOPROC);
5050 }
5051 else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
5052 {
5053 switch (filedata->file_header.e_machine)
5054 {
5055 case EM_IA_64:
5056 result = get_ia64_section_type_name (filedata, sh_type);
5057 break;
5058 default:
5059 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
5060 result = get_solaris_section_type (sh_type);
5061 else
5062 {
5063 switch (sh_type)
5064 {
5065 case SHT_GNU_INCREMENTAL_INPUTS: result = "GNU_INCREMENTAL_INPUTS"; break;
5066 case SHT_GNU_ATTRIBUTES: result = "GNU_ATTRIBUTES"; break;
5067 case SHT_GNU_HASH: result = "GNU_HASH"; break;
5068 case SHT_GNU_LIBLIST: result = "GNU_LIBLIST"; break;
5069 default:
5070 result = NULL;
5071 break;
5072 }
5073 }
5074 break;
5075 }
5076
5077 if (result != NULL)
5078 return result;
5079
5080 sprintf (buff, "LOOS+%#x", sh_type - SHT_LOOS);
5081 }
5082 else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
5083 {
5084 switch (filedata->file_header.e_machine)
5085 {
5086 case EM_V800:
5087 case EM_V850:
5088 case EM_CYGNUS_V850:
5089 result = get_v850_section_type_name (sh_type);
5090 break;
5091 default:
5092 result = NULL;
5093 break;
5094 }
5095
5096 if (result != NULL)
5097 return result;
5098
5099 sprintf (buff, "LOUSER+%#x", sh_type - SHT_LOUSER);
5100 }
5101 else
5102 /* This message is probably going to be displayed in a 15
5103 character wide field, so put the hex value first. */
5104 snprintf (buff, sizeof (buff), _("%08x: <unknown>"), sh_type);
5105
5106 return buff;
5107 }
5108 }
5109
5110 enum long_option_values
5111 {
5112 OPTION_DEBUG_DUMP = 512,
5113 OPTION_DYN_SYMS,
5114 OPTION_LTO_SYMS,
5115 OPTION_DWARF_DEPTH,
5116 OPTION_DWARF_START,
5117 OPTION_DWARF_CHECK,
5118 OPTION_CTF_DUMP,
5119 OPTION_CTF_PARENT,
5120 OPTION_CTF_SYMBOLS,
5121 OPTION_CTF_STRINGS,
5122 OPTION_WITH_SYMBOL_VERSIONS,
5123 OPTION_RECURSE_LIMIT,
5124 OPTION_NO_RECURSE_LIMIT,
5125 OPTION_NO_DEMANGLING,
5126 OPTION_SYM_BASE
5127 };
5128
5129 static struct option options[] =
5130 {
5131 /* Note - This table is alpha-sorted on the 'val'
5132 field in order to make adding new options easier. */
5133 {"arch-specific", no_argument, 0, 'A'},
5134 {"all", no_argument, 0, 'a'},
5135 {"demangle", optional_argument, 0, 'C'},
5136 {"archive-index", no_argument, 0, 'c'},
5137 {"use-dynamic", no_argument, 0, 'D'},
5138 {"dynamic", no_argument, 0, 'd'},
5139 {"headers", no_argument, 0, 'e'},
5140 {"section-groups", no_argument, 0, 'g'},
5141 {"help", no_argument, 0, 'H'},
5142 {"file-header", no_argument, 0, 'h'},
5143 {"histogram", no_argument, 0, 'I'},
5144 {"lint", no_argument, 0, 'L'},
5145 {"enable-checks", no_argument, 0, 'L'},
5146 {"program-headers", no_argument, 0, 'l'},
5147 {"segments", no_argument, 0, 'l'},
5148 {"full-section-name",no_argument, 0, 'N'},
5149 {"notes", no_argument, 0, 'n'},
5150 {"process-links", no_argument, 0, 'P'},
5151 {"string-dump", required_argument, 0, 'p'},
5152 {"relocated-dump", required_argument, 0, 'R'},
5153 {"relocs", no_argument, 0, 'r'},
5154 {"section-headers", no_argument, 0, 'S'},
5155 {"sections", no_argument, 0, 'S'},
5156 {"symbols", no_argument, 0, 's'},
5157 {"syms", no_argument, 0, 's'},
5158 {"silent-truncation",no_argument, 0, 'T'},
5159 {"section-details", no_argument, 0, 't'},
5160 {"unicode", required_argument, NULL, 'U'},
5161 {"unwind", no_argument, 0, 'u'},
5162 {"version-info", no_argument, 0, 'V'},
5163 {"version", no_argument, 0, 'v'},
5164 {"wide", no_argument, 0, 'W'},
5165 {"hex-dump", required_argument, 0, 'x'},
5166 {"decompress", no_argument, 0, 'z'},
5167
5168 {"no-demangle", no_argument, 0, OPTION_NO_DEMANGLING},
5169 {"recurse-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
5170 {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
5171 {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
5172 {"dyn-syms", no_argument, 0, OPTION_DYN_SYMS},
5173 {"lto-syms", no_argument, 0, OPTION_LTO_SYMS},
5174 {"debug-dump", optional_argument, 0, OPTION_DEBUG_DUMP},
5175 {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
5176 {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
5177 {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
5178 #ifdef ENABLE_LIBCTF
5179 {"ctf", required_argument, 0, OPTION_CTF_DUMP},
5180 {"ctf-symbols", required_argument, 0, OPTION_CTF_SYMBOLS},
5181 {"ctf-strings", required_argument, 0, OPTION_CTF_STRINGS},
5182 {"ctf-parent", required_argument, 0, OPTION_CTF_PARENT},
5183 #endif
5184 {"sym-base", optional_argument, 0, OPTION_SYM_BASE},
5185
5186 {0, no_argument, 0, 0}
5187 };
5188
5189 static void
5190 usage (FILE * stream)
5191 {
5192 fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
5193 fprintf (stream, _(" Display information about the contents of ELF format files\n"));
5194 fprintf (stream, _(" Options are:\n"));
5195 fprintf (stream, _("\
5196 -a --all Equivalent to: -h -l -S -s -r -d -V -A -I\n"));
5197 fprintf (stream, _("\
5198 -h --file-header Display the ELF file header\n"));
5199 fprintf (stream, _("\
5200 -l --program-headers Display the program headers\n"));
5201 fprintf (stream, _("\
5202 --segments An alias for --program-headers\n"));
5203 fprintf (stream, _("\
5204 -S --section-headers Display the sections' header\n"));
5205 fprintf (stream, _("\
5206 --sections An alias for --section-headers\n"));
5207 fprintf (stream, _("\
5208 -g --section-groups Display the section groups\n"));
5209 fprintf (stream, _("\
5210 -t --section-details Display the section details\n"));
5211 fprintf (stream, _("\
5212 -e --headers Equivalent to: -h -l -S\n"));
5213 fprintf (stream, _("\
5214 -s --syms Display the symbol table\n"));
5215 fprintf (stream, _("\
5216 --symbols An alias for --syms\n"));
5217 fprintf (stream, _("\
5218 --dyn-syms Display the dynamic symbol table\n"));
5219 fprintf (stream, _("\
5220 --lto-syms Display LTO symbol tables\n"));
5221 fprintf (stream, _("\
5222 --sym-base=[0|8|10|16] \n\
5223 Force base for symbol sizes. The options are \n\
5224 mixed (the default), octal, decimal, hexadecimal.\n"));
5225 fprintf (stream, _("\
5226 -C --demangle[=STYLE] Decode mangled/processed symbol names\n"));
5227 display_demangler_styles (stream, _("\
5228 STYLE can be "));
5229 fprintf (stream, _("\
5230 --no-demangle Do not demangle low-level symbol names. (default)\n"));
5231 fprintf (stream, _("\
5232 --recurse-limit Enable a demangling recursion limit. (default)\n"));
5233 fprintf (stream, _("\
5234 --no-recurse-limit Disable a demangling recursion limit\n"));
5235 fprintf (stream, _("\
5236 -U[dlexhi] --unicode=[default|locale|escape|hex|highlight|invalid]\n\
5237 Display unicode characters as determined by the current locale\n\
5238 (default), escape sequences, \"<hex sequences>\", highlighted\n\
5239 escape sequences, or treat them as invalid and display as\n\
5240 \"{hex sequences}\"\n"));
5241 fprintf (stream, _("\
5242 -n --notes Display the core notes (if present)\n"));
5243 fprintf (stream, _("\
5244 -r --relocs Display the relocations (if present)\n"));
5245 fprintf (stream, _("\
5246 -u --unwind Display the unwind info (if present)\n"));
5247 fprintf (stream, _("\
5248 -d --dynamic Display the dynamic section (if present)\n"));
5249 fprintf (stream, _("\
5250 -V --version-info Display the version sections (if present)\n"));
5251 fprintf (stream, _("\
5252 -A --arch-specific Display architecture specific information (if any)\n"));
5253 fprintf (stream, _("\
5254 -c --archive-index Display the symbol/file index in an archive\n"));
5255 fprintf (stream, _("\
5256 -D --use-dynamic Use the dynamic section info when displaying symbols\n"));
5257 fprintf (stream, _("\
5258 -L --lint|--enable-checks\n\
5259 Display warning messages for possible problems\n"));
5260 fprintf (stream, _("\
5261 -x --hex-dump=<number|name>\n\
5262 Dump the contents of section <number|name> as bytes\n"));
5263 fprintf (stream, _("\
5264 -p --string-dump=<number|name>\n\
5265 Dump the contents of section <number|name> as strings\n"));
5266 fprintf (stream, _("\
5267 -R --relocated-dump=<number|name>\n\
5268 Dump the relocated contents of section <number|name>\n"));
5269 fprintf (stream, _("\
5270 -z --decompress Decompress section before dumping it\n"));
5271 fprintf (stream, _("\
5272 -w --debug-dump[a/=abbrev, A/=addr, r/=aranges, c/=cu_index, L/=decodedline,\n\
5273 f/=frames, F/=frames-interp, g/=gdb_index, i/=info, o/=loc,\n\
5274 m/=macro, p/=pubnames, t/=pubtypes, R/=Ranges, l/=rawline,\n\
5275 s/=str, O/=str-offsets, u/=trace_abbrev, T/=trace_aranges,\n\
5276 U/=trace_info]\n\
5277 Display the contents of DWARF debug sections\n"));
5278 fprintf (stream, _("\
5279 -wk --debug-dump=links Display the contents of sections that link to separate\n\
5280 debuginfo files\n"));
5281 fprintf (stream, _("\
5282 -P --process-links Display the contents of non-debug sections in separate\n\
5283 debuginfo files. (Implies -wK)\n"));
5284 #if DEFAULT_FOR_FOLLOW_LINKS
5285 fprintf (stream, _("\
5286 -wK --debug-dump=follow-links\n\
5287 Follow links to separate debug info files (default)\n"));
5288 fprintf (stream, _("\
5289 -wN --debug-dump=no-follow-links\n\
5290 Do not follow links to separate debug info files\n"));
5291 #else
5292 fprintf (stream, _("\
5293 -wK --debug-dump=follow-links\n\
5294 Follow links to separate debug info files\n"));
5295 fprintf (stream, _("\
5296 -wN --debug-dump=no-follow-links\n\
5297 Do not follow links to separate debug info files\n\
5298 (default)\n"));
5299 #endif
5300 #if HAVE_LIBDEBUGINFOD
5301 fprintf (stream, _("\
5302 -wD --debug-dump=use-debuginfod\n\
5303 When following links, also query debuginfod servers (default)\n"));
5304 fprintf (stream, _("\
5305 -wE --debug-dump=do-not-use-debuginfod\n\
5306 When following links, do not query debuginfod servers\n"));
5307 #endif
5308 fprintf (stream, _("\
5309 --dwarf-depth=N Do not display DIEs at depth N or greater\n"));
5310 fprintf (stream, _("\
5311 --dwarf-start=N Display DIEs starting at offset N\n"));
5312 #ifdef ENABLE_LIBCTF
5313 fprintf (stream, _("\
5314 --ctf=<number|name> Display CTF info from section <number|name>\n"));
5315 fprintf (stream, _("\
5316 --ctf-parent=<name> Use CTF archive member <name> as the CTF parent\n"));
5317 fprintf (stream, _("\
5318 --ctf-symbols=<number|name>\n\
5319 Use section <number|name> as the CTF external symtab\n"));
5320 fprintf (stream, _("\
5321 --ctf-strings=<number|name>\n\
5322 Use section <number|name> as the CTF external strtab\n"));
5323 #endif
5324
5325 #ifdef SUPPORT_DISASSEMBLY
5326 fprintf (stream, _("\
5327 -i --instruction-dump=<number|name>\n\
5328 Disassemble the contents of section <number|name>\n"));
5329 #endif
5330 fprintf (stream, _("\
5331 -I --histogram Display histogram of bucket list lengths\n"));
5332 fprintf (stream, _("\
5333 -W --wide Allow output width to exceed 80 characters\n"));
5334 fprintf (stream, _("\
5335 -T --silent-truncation If a symbol name is truncated, do not add [...] suffix\n"));
5336 fprintf (stream, _("\
5337 @<file> Read options from <file>\n"));
5338 fprintf (stream, _("\
5339 -H --help Display this information\n"));
5340 fprintf (stream, _("\
5341 -v --version Display the version number of readelf\n"));
5342
5343 if (REPORT_BUGS_TO[0] && stream == stdout)
5344 fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
5345
5346 exit (stream == stdout ? 0 : 1);
5347 }
5348
5349 /* Record the fact that the user wants the contents of section number
5350 SECTION to be displayed using the method(s) encoded as flags bits
5351 in TYPE. Note, TYPE can be zero if we are creating the array for
5352 the first time. */
5353
5354 static void
5355 request_dump_bynumber (struct dump_data *dumpdata,
5356 unsigned int section, dump_type type)
5357 {
5358 if (section >= dumpdata->num_dump_sects)
5359 {
5360 dump_type * new_dump_sects;
5361
5362 new_dump_sects = (dump_type *) calloc (section + 1,
5363 sizeof (* new_dump_sects));
5364
5365 if (new_dump_sects == NULL)
5366 error (_("Out of memory allocating dump request table.\n"));
5367 else
5368 {
5369 if (dumpdata->dump_sects)
5370 {
5371 /* Copy current flag settings. */
5372 memcpy (new_dump_sects, dumpdata->dump_sects,
5373 dumpdata->num_dump_sects * sizeof (* new_dump_sects));
5374
5375 free (dumpdata->dump_sects);
5376 }
5377
5378 dumpdata->dump_sects = new_dump_sects;
5379 dumpdata->num_dump_sects = section + 1;
5380 }
5381 }
5382
5383 if (dumpdata->dump_sects)
5384 dumpdata->dump_sects[section] |= type;
5385 }
5386
5387 /* Request a dump by section name. */
5388
5389 static void
5390 request_dump_byname (const char * section, dump_type type)
5391 {
5392 struct dump_list_entry * new_request;
5393
5394 new_request = (struct dump_list_entry *)
5395 malloc (sizeof (struct dump_list_entry));
5396 if (!new_request)
5397 error (_("Out of memory allocating dump request table.\n"));
5398
5399 new_request->name = strdup (section);
5400 if (!new_request->name)
5401 error (_("Out of memory allocating dump request table.\n"));
5402
5403 new_request->type = type;
5404
5405 new_request->next = dump_sects_byname;
5406 dump_sects_byname = new_request;
5407 }
5408
5409 static inline void
5410 request_dump (struct dump_data *dumpdata, dump_type type)
5411 {
5412 int section;
5413 char * cp;
5414
5415 do_dump = true;
5416 section = strtoul (optarg, & cp, 0);
5417
5418 if (! *cp && section >= 0)
5419 request_dump_bynumber (dumpdata, section, type);
5420 else
5421 request_dump_byname (optarg, type);
5422 }
5423
5424 static void
5425 parse_args (struct dump_data *dumpdata, int argc, char ** argv)
5426 {
5427 int c;
5428
5429 if (argc < 2)
5430 usage (stderr);
5431
5432 while ((c = getopt_long
5433 (argc, argv, "ACDHILNPR:STU:VWacdeghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
5434 {
5435 switch (c)
5436 {
5437 case 0:
5438 /* Long options. */
5439 break;
5440 case 'H':
5441 usage (stdout);
5442 break;
5443
5444 case 'a':
5445 do_syms = true;
5446 do_reloc = true;
5447 do_unwind = true;
5448 do_dynamic = true;
5449 do_header = true;
5450 do_sections = true;
5451 do_section_groups = true;
5452 do_segments = true;
5453 do_version = true;
5454 do_histogram = true;
5455 do_arch = true;
5456 do_notes = true;
5457 break;
5458
5459 case 'g':
5460 do_section_groups = true;
5461 break;
5462 case 't':
5463 case 'N':
5464 do_sections = true;
5465 do_section_details = true;
5466 break;
5467 case 'e':
5468 do_header = true;
5469 do_sections = true;
5470 do_segments = true;
5471 break;
5472 case 'A':
5473 do_arch = true;
5474 break;
5475 case 'D':
5476 do_using_dynamic = true;
5477 break;
5478 case 'r':
5479 do_reloc = true;
5480 break;
5481 case 'u':
5482 do_unwind = true;
5483 break;
5484 case 'h':
5485 do_header = true;
5486 break;
5487 case 'l':
5488 do_segments = true;
5489 break;
5490 case 's':
5491 do_syms = true;
5492 break;
5493 case 'S':
5494 do_sections = true;
5495 break;
5496 case 'd':
5497 do_dynamic = true;
5498 break;
5499 case 'I':
5500 do_histogram = true;
5501 break;
5502 case 'n':
5503 do_notes = true;
5504 break;
5505 case 'c':
5506 do_archive_index = true;
5507 break;
5508 case 'L':
5509 do_checks = true;
5510 break;
5511 case 'P':
5512 process_links = true;
5513 do_follow_links = true;
5514 dump_any_debugging = true;
5515 break;
5516 case 'x':
5517 request_dump (dumpdata, HEX_DUMP);
5518 break;
5519 case 'p':
5520 request_dump (dumpdata, STRING_DUMP);
5521 break;
5522 case 'R':
5523 request_dump (dumpdata, RELOC_DUMP);
5524 break;
5525 case 'z':
5526 decompress_dumps = true;
5527 break;
5528 case 'w':
5529 do_dump = true;
5530 dump_any_debugging = true;
5531 if (optarg == NULL)
5532 {
5533 do_debugging = true;
5534 dwarf_select_sections_all ();
5535 }
5536 else
5537 {
5538 do_debugging = false;
5539 dwarf_select_sections_by_letters (optarg);
5540 }
5541 break;
5542 case OPTION_DEBUG_DUMP:
5543 do_dump = true;
5544 dump_any_debugging = true;
5545 if (optarg == NULL)
5546 {
5547 do_debugging = true;
5548 dwarf_select_sections_all ();
5549 }
5550 else
5551 {
5552 do_debugging = false;
5553 dwarf_select_sections_by_names (optarg);
5554 }
5555 break;
5556 case OPTION_DWARF_DEPTH:
5557 {
5558 char *cp;
5559
5560 dwarf_cutoff_level = strtoul (optarg, & cp, 0);
5561 }
5562 break;
5563 case OPTION_DWARF_START:
5564 {
5565 char *cp;
5566
5567 dwarf_start_die = strtoul (optarg, & cp, 0);
5568 }
5569 break;
5570 case OPTION_DWARF_CHECK:
5571 dwarf_check = true;
5572 break;
5573 case OPTION_CTF_DUMP:
5574 do_ctf = true;
5575 request_dump (dumpdata, CTF_DUMP);
5576 break;
5577 case OPTION_CTF_SYMBOLS:
5578 free (dump_ctf_symtab_name);
5579 dump_ctf_symtab_name = strdup (optarg);
5580 break;
5581 case OPTION_CTF_STRINGS:
5582 free (dump_ctf_strtab_name);
5583 dump_ctf_strtab_name = strdup (optarg);
5584 break;
5585 case OPTION_CTF_PARENT:
5586 free (dump_ctf_parent_name);
5587 dump_ctf_parent_name = strdup (optarg);
5588 break;
5589 case OPTION_DYN_SYMS:
5590 do_dyn_syms = true;
5591 break;
5592 case OPTION_LTO_SYMS:
5593 do_lto_syms = true;
5594 break;
5595 #ifdef SUPPORT_DISASSEMBLY
5596 case 'i':
5597 request_dump (dumpdata, DISASS_DUMP);
5598 break;
5599 #endif
5600 case 'v':
5601 print_version (program_name);
5602 break;
5603 case 'V':
5604 do_version = true;
5605 break;
5606 case 'W':
5607 do_wide = true;
5608 break;
5609 case 'T':
5610 do_not_show_symbol_truncation = true;
5611 break;
5612 case 'C':
5613 do_demangle = true;
5614 if (optarg != NULL)
5615 {
5616 enum demangling_styles style;
5617
5618 style = cplus_demangle_name_to_style (optarg);
5619 if (style == unknown_demangling)
5620 error (_("unknown demangling style `%s'"), optarg);
5621
5622 cplus_demangle_set_style (style);
5623 }
5624 break;
5625 case OPTION_NO_DEMANGLING:
5626 do_demangle = false;
5627 break;
5628 case OPTION_RECURSE_LIMIT:
5629 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
5630 break;
5631 case OPTION_NO_RECURSE_LIMIT:
5632 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
5633 break;
5634 case OPTION_WITH_SYMBOL_VERSIONS:
5635 /* Ignored for backward compatibility. */
5636 break;
5637
5638 case 'U':
5639 if (optarg == NULL)
5640 error (_("Missing arg to -U/--unicode")); /* Can this happen ? */
5641 else if (streq (optarg, "default") || streq (optarg, "d"))
5642 unicode_display = unicode_default;
5643 else if (streq (optarg, "locale") || streq (optarg, "l"))
5644 unicode_display = unicode_locale;
5645 else if (streq (optarg, "escape") || streq (optarg, "e"))
5646 unicode_display = unicode_escape;
5647 else if (streq (optarg, "invalid") || streq (optarg, "i"))
5648 unicode_display = unicode_invalid;
5649 else if (streq (optarg, "hex") || streq (optarg, "x"))
5650 unicode_display = unicode_hex;
5651 else if (streq (optarg, "highlight") || streq (optarg, "h"))
5652 unicode_display = unicode_highlight;
5653 else
5654 error (_("invalid argument to -U/--unicode: %s"), optarg);
5655 break;
5656
5657 case OPTION_SYM_BASE:
5658 sym_base = 0;
5659 if (optarg != NULL)
5660 {
5661 sym_base = strtoul (optarg, NULL, 0);
5662 switch (sym_base)
5663 {
5664 case 0:
5665 case 8:
5666 case 10:
5667 case 16:
5668 break;
5669
5670 default:
5671 sym_base = 0;
5672 break;
5673 }
5674 }
5675 break;
5676
5677 default:
5678 /* xgettext:c-format */
5679 error (_("Invalid option '-%c'\n"), c);
5680 /* Fall through. */
5681 case '?':
5682 usage (stderr);
5683 }
5684 }
5685
5686 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
5687 && !do_segments && !do_header && !do_dump && !do_version
5688 && !do_histogram && !do_debugging && !do_arch && !do_notes
5689 && !do_section_groups && !do_archive_index
5690 && !do_dyn_syms && !do_lto_syms)
5691 {
5692 if (do_checks)
5693 {
5694 check_all = true;
5695 do_dynamic = do_syms = do_reloc = do_unwind = do_sections = true;
5696 do_segments = do_header = do_dump = do_version = true;
5697 do_histogram = do_debugging = do_arch = do_notes = true;
5698 do_section_groups = do_archive_index = do_dyn_syms = true;
5699 do_lto_syms = true;
5700 }
5701 else
5702 usage (stderr);
5703 }
5704 }
5705
5706 static const char *
5707 get_elf_class (unsigned int elf_class)
5708 {
5709 static char buff[32];
5710
5711 switch (elf_class)
5712 {
5713 case ELFCLASSNONE: return _("none");
5714 case ELFCLASS32: return "ELF32";
5715 case ELFCLASS64: return "ELF64";
5716 default:
5717 snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
5718 return buff;
5719 }
5720 }
5721
5722 static const char *
5723 get_data_encoding (unsigned int encoding)
5724 {
5725 static char buff[32];
5726
5727 switch (encoding)
5728 {
5729 case ELFDATANONE: return _("none");
5730 case ELFDATA2LSB: return _("2's complement, little endian");
5731 case ELFDATA2MSB: return _("2's complement, big endian");
5732 default:
5733 snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
5734 return buff;
5735 }
5736 }
5737
5738 /* Decode the data held in 'filedata->file_header'. */
5739
5740 static bool
5741 process_file_header (Filedata * filedata)
5742 {
5743 Elf_Internal_Ehdr * header = & filedata->file_header;
5744
5745 if ( header->e_ident[EI_MAG0] != ELFMAG0
5746 || header->e_ident[EI_MAG1] != ELFMAG1
5747 || header->e_ident[EI_MAG2] != ELFMAG2
5748 || header->e_ident[EI_MAG3] != ELFMAG3)
5749 {
5750 error
5751 (_("Not an ELF file - it has the wrong magic bytes at the start\n"));
5752 return false;
5753 }
5754
5755 if (! filedata->is_separate)
5756 init_dwarf_regnames_by_elf_machine_code (header->e_machine);
5757
5758 if (do_header)
5759 {
5760 unsigned i;
5761
5762 if (filedata->is_separate)
5763 printf (_("ELF Header in linked file '%s':\n"), filedata->file_name);
5764 else
5765 printf (_("ELF Header:\n"));
5766 printf (_(" Magic: "));
5767 for (i = 0; i < EI_NIDENT; i++)
5768 printf ("%2.2x ", header->e_ident[i]);
5769 printf ("\n");
5770 printf (_(" Class: %s\n"),
5771 get_elf_class (header->e_ident[EI_CLASS]));
5772 printf (_(" Data: %s\n"),
5773 get_data_encoding (header->e_ident[EI_DATA]));
5774 printf (_(" Version: %d%s\n"),
5775 header->e_ident[EI_VERSION],
5776 (header->e_ident[EI_VERSION] == EV_CURRENT
5777 ? _(" (current)")
5778 : (header->e_ident[EI_VERSION] != EV_NONE
5779 ? _(" <unknown>")
5780 : "")));
5781 printf (_(" OS/ABI: %s\n"),
5782 get_osabi_name (filedata, header->e_ident[EI_OSABI]));
5783 printf (_(" ABI Version: %d\n"),
5784 header->e_ident[EI_ABIVERSION]);
5785 printf (_(" Type: %s\n"),
5786 get_file_type (filedata));
5787 printf (_(" Machine: %s\n"),
5788 get_machine_name (header->e_machine));
5789 printf (_(" Version: 0x%lx\n"),
5790 header->e_version);
5791
5792 printf (_(" Entry point address: "));
5793 print_vma (header->e_entry, PREFIX_HEX);
5794 printf (_("\n Start of program headers: "));
5795 print_vma (header->e_phoff, DEC);
5796 printf (_(" (bytes into file)\n Start of section headers: "));
5797 print_vma (header->e_shoff, DEC);
5798 printf (_(" (bytes into file)\n"));
5799
5800 printf (_(" Flags: 0x%lx%s\n"),
5801 header->e_flags,
5802 get_machine_flags (filedata, header->e_flags, header->e_machine));
5803 printf (_(" Size of this header: %u (bytes)\n"),
5804 header->e_ehsize);
5805 printf (_(" Size of program headers: %u (bytes)\n"),
5806 header->e_phentsize);
5807 printf (_(" Number of program headers: %u"),
5808 header->e_phnum);
5809 if (filedata->section_headers != NULL
5810 && header->e_phnum == PN_XNUM
5811 && filedata->section_headers[0].sh_info != 0)
5812 printf (" (%u)", filedata->section_headers[0].sh_info);
5813 putc ('\n', stdout);
5814 printf (_(" Size of section headers: %u (bytes)\n"),
5815 header->e_shentsize);
5816 printf (_(" Number of section headers: %u"),
5817 header->e_shnum);
5818 if (filedata->section_headers != NULL && header->e_shnum == SHN_UNDEF)
5819 {
5820 header->e_shnum = filedata->section_headers[0].sh_size;
5821 printf (" (%u)", header->e_shnum);
5822 }
5823 putc ('\n', stdout);
5824 printf (_(" Section header string table index: %u"),
5825 header->e_shstrndx);
5826 if (filedata->section_headers != NULL
5827 && header->e_shstrndx == (SHN_XINDEX & 0xffff))
5828 {
5829 header->e_shstrndx = filedata->section_headers[0].sh_link;
5830 printf (" (%u)", header->e_shstrndx);
5831 }
5832 if (header->e_shstrndx != SHN_UNDEF
5833 && header->e_shstrndx >= header->e_shnum)
5834 {
5835 header->e_shstrndx = SHN_UNDEF;
5836 printf (_(" <corrupt: out of range>"));
5837 }
5838 putc ('\n', stdout);
5839 }
5840
5841 if (filedata->section_headers != NULL)
5842 {
5843 if (header->e_phnum == PN_XNUM
5844 && filedata->section_headers[0].sh_info != 0)
5845 {
5846 /* Throw away any cached read of PN_XNUM headers. */
5847 free (filedata->program_headers);
5848 filedata->program_headers = NULL;
5849 header->e_phnum = filedata->section_headers[0].sh_info;
5850 }
5851 if (header->e_shnum == SHN_UNDEF)
5852 header->e_shnum = filedata->section_headers[0].sh_size;
5853 if (header->e_shstrndx == (SHN_XINDEX & 0xffff))
5854 header->e_shstrndx = filedata->section_headers[0].sh_link;
5855 if (header->e_shstrndx >= header->e_shnum)
5856 header->e_shstrndx = SHN_UNDEF;
5857 }
5858
5859 return true;
5860 }
5861
5862 /* Read in the program headers from FILEDATA and store them in PHEADERS.
5863 Returns TRUE upon success, FALSE otherwise. Loads 32-bit headers. */
5864
5865 static bool
5866 get_32bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
5867 {
5868 Elf32_External_Phdr * phdrs;
5869 Elf32_External_Phdr * external;
5870 Elf_Internal_Phdr * internal;
5871 unsigned int i;
5872 unsigned int size = filedata->file_header.e_phentsize;
5873 unsigned int num = filedata->file_header.e_phnum;
5874
5875 /* PR binutils/17531: Cope with unexpected section header sizes. */
5876 if (size == 0 || num == 0)
5877 return false;
5878 if (size < sizeof * phdrs)
5879 {
5880 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
5881 return false;
5882 }
5883 if (size > sizeof * phdrs)
5884 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
5885
5886 phdrs = (Elf32_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
5887 size, num, _("program headers"));
5888 if (phdrs == NULL)
5889 return false;
5890
5891 for (i = 0, internal = pheaders, external = phdrs;
5892 i < filedata->file_header.e_phnum;
5893 i++, internal++, external++)
5894 {
5895 internal->p_type = BYTE_GET (external->p_type);
5896 internal->p_offset = BYTE_GET (external->p_offset);
5897 internal->p_vaddr = BYTE_GET (external->p_vaddr);
5898 internal->p_paddr = BYTE_GET (external->p_paddr);
5899 internal->p_filesz = BYTE_GET (external->p_filesz);
5900 internal->p_memsz = BYTE_GET (external->p_memsz);
5901 internal->p_flags = BYTE_GET (external->p_flags);
5902 internal->p_align = BYTE_GET (external->p_align);
5903 }
5904
5905 free (phdrs);
5906 return true;
5907 }
5908
5909 /* Read in the program headers from FILEDATA and store them in PHEADERS.
5910 Returns TRUE upon success, FALSE otherwise. Loads 64-bit headers. */
5911
5912 static bool
5913 get_64bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
5914 {
5915 Elf64_External_Phdr * phdrs;
5916 Elf64_External_Phdr * external;
5917 Elf_Internal_Phdr * internal;
5918 unsigned int i;
5919 unsigned int size = filedata->file_header.e_phentsize;
5920 unsigned int num = filedata->file_header.e_phnum;
5921
5922 /* PR binutils/17531: Cope with unexpected section header sizes. */
5923 if (size == 0 || num == 0)
5924 return false;
5925 if (size < sizeof * phdrs)
5926 {
5927 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
5928 return false;
5929 }
5930 if (size > sizeof * phdrs)
5931 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
5932
5933 phdrs = (Elf64_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
5934 size, num, _("program headers"));
5935 if (!phdrs)
5936 return false;
5937
5938 for (i = 0, internal = pheaders, external = phdrs;
5939 i < filedata->file_header.e_phnum;
5940 i++, internal++, external++)
5941 {
5942 internal->p_type = BYTE_GET (external->p_type);
5943 internal->p_flags = BYTE_GET (external->p_flags);
5944 internal->p_offset = BYTE_GET (external->p_offset);
5945 internal->p_vaddr = BYTE_GET (external->p_vaddr);
5946 internal->p_paddr = BYTE_GET (external->p_paddr);
5947 internal->p_filesz = BYTE_GET (external->p_filesz);
5948 internal->p_memsz = BYTE_GET (external->p_memsz);
5949 internal->p_align = BYTE_GET (external->p_align);
5950 }
5951
5952 free (phdrs);
5953 return true;
5954 }
5955
5956 /* Returns TRUE if the program headers were read into `program_headers'. */
5957
5958 static bool
5959 get_program_headers (Filedata * filedata)
5960 {
5961 Elf_Internal_Phdr * phdrs;
5962
5963 /* Check cache of prior read. */
5964 if (filedata->program_headers != NULL)
5965 return true;
5966
5967 /* Be kind to memory checkers by looking for
5968 e_phnum values which we know must be invalid. */
5969 if (filedata->file_header.e_phnum
5970 * (is_32bit_elf ? sizeof (Elf32_External_Phdr) : sizeof (Elf64_External_Phdr))
5971 >= filedata->file_size)
5972 {
5973 error (_("Too many program headers - %#x - the file is not that big\n"),
5974 filedata->file_header.e_phnum);
5975 return false;
5976 }
5977
5978 phdrs = (Elf_Internal_Phdr *) cmalloc (filedata->file_header.e_phnum,
5979 sizeof (Elf_Internal_Phdr));
5980 if (phdrs == NULL)
5981 {
5982 error (_("Out of memory reading %u program headers\n"),
5983 filedata->file_header.e_phnum);
5984 return false;
5985 }
5986
5987 if (is_32bit_elf
5988 ? get_32bit_program_headers (filedata, phdrs)
5989 : get_64bit_program_headers (filedata, phdrs))
5990 {
5991 filedata->program_headers = phdrs;
5992 return true;
5993 }
5994
5995 free (phdrs);
5996 return false;
5997 }
5998
5999 /* Print program header info and locate dynamic section. */
6000
6001 static void
6002 process_program_headers (Filedata * filedata)
6003 {
6004 Elf_Internal_Phdr * segment;
6005 unsigned int i;
6006 Elf_Internal_Phdr * previous_load = NULL;
6007
6008 if (filedata->file_header.e_phnum == 0)
6009 {
6010 /* PR binutils/12467. */
6011 if (filedata->file_header.e_phoff != 0)
6012 warn (_("possibly corrupt ELF header - it has a non-zero program"
6013 " header offset, but no program headers\n"));
6014 else if (do_segments)
6015 {
6016 if (filedata->is_separate)
6017 printf (_("\nThere are no program headers in linked file '%s'.\n"),
6018 filedata->file_name);
6019 else
6020 printf (_("\nThere are no program headers in this file.\n"));
6021 }
6022 goto no_headers;
6023 }
6024
6025 if (do_segments && !do_header)
6026 {
6027 if (filedata->is_separate)
6028 printf ("\nIn linked file '%s' the ELF file type is %s\n",
6029 filedata->file_name, get_file_type (filedata));
6030 else
6031 printf (_("\nElf file type is %s\n"), get_file_type (filedata));
6032 printf (_("Entry point 0x%s\n"), bfd_vmatoa ("x", filedata->file_header.e_entry));
6033 printf (ngettext ("There is %d program header, starting at offset %s\n",
6034 "There are %d program headers, starting at offset %s\n",
6035 filedata->file_header.e_phnum),
6036 filedata->file_header.e_phnum,
6037 bfd_vmatoa ("u", filedata->file_header.e_phoff));
6038 }
6039
6040 if (! get_program_headers (filedata))
6041 goto no_headers;
6042
6043 if (do_segments)
6044 {
6045 if (filedata->file_header.e_phnum > 1)
6046 printf (_("\nProgram Headers:\n"));
6047 else
6048 printf (_("\nProgram Headers:\n"));
6049
6050 if (is_32bit_elf)
6051 printf
6052 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
6053 else if (do_wide)
6054 printf
6055 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
6056 else
6057 {
6058 printf
6059 (_(" Type Offset VirtAddr PhysAddr\n"));
6060 printf
6061 (_(" FileSiz MemSiz Flags Align\n"));
6062 }
6063 }
6064
6065 unsigned long dynamic_addr = 0;
6066 bfd_size_type dynamic_size = 0;
6067 for (i = 0, segment = filedata->program_headers;
6068 i < filedata->file_header.e_phnum;
6069 i++, segment++)
6070 {
6071 if (do_segments)
6072 {
6073 printf (" %-14.14s ", get_segment_type (filedata, segment->p_type));
6074
6075 if (is_32bit_elf)
6076 {
6077 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
6078 printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
6079 printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
6080 printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
6081 printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
6082 printf ("%c%c%c ",
6083 (segment->p_flags & PF_R ? 'R' : ' '),
6084 (segment->p_flags & PF_W ? 'W' : ' '),
6085 (segment->p_flags & PF_X ? 'E' : ' '));
6086 printf ("%#lx", (unsigned long) segment->p_align);
6087 }
6088 else if (do_wide)
6089 {
6090 if ((unsigned long) segment->p_offset == segment->p_offset)
6091 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
6092 else
6093 {
6094 print_vma (segment->p_offset, FULL_HEX);
6095 putchar (' ');
6096 }
6097
6098 print_vma (segment->p_vaddr, FULL_HEX);
6099 putchar (' ');
6100 print_vma (segment->p_paddr, FULL_HEX);
6101 putchar (' ');
6102
6103 if ((unsigned long) segment->p_filesz == segment->p_filesz)
6104 printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
6105 else
6106 {
6107 print_vma (segment->p_filesz, FULL_HEX);
6108 putchar (' ');
6109 }
6110
6111 if ((unsigned long) segment->p_memsz == segment->p_memsz)
6112 printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
6113 else
6114 {
6115 print_vma (segment->p_memsz, FULL_HEX);
6116 }
6117
6118 printf (" %c%c%c ",
6119 (segment->p_flags & PF_R ? 'R' : ' '),
6120 (segment->p_flags & PF_W ? 'W' : ' '),
6121 (segment->p_flags & PF_X ? 'E' : ' '));
6122
6123 if ((unsigned long) segment->p_align == segment->p_align)
6124 printf ("%#lx", (unsigned long) segment->p_align);
6125 else
6126 {
6127 print_vma (segment->p_align, PREFIX_HEX);
6128 }
6129 }
6130 else
6131 {
6132 print_vma (segment->p_offset, FULL_HEX);
6133 putchar (' ');
6134 print_vma (segment->p_vaddr, FULL_HEX);
6135 putchar (' ');
6136 print_vma (segment->p_paddr, FULL_HEX);
6137 printf ("\n ");
6138 print_vma (segment->p_filesz, FULL_HEX);
6139 putchar (' ');
6140 print_vma (segment->p_memsz, FULL_HEX);
6141 printf (" %c%c%c ",
6142 (segment->p_flags & PF_R ? 'R' : ' '),
6143 (segment->p_flags & PF_W ? 'W' : ' '),
6144 (segment->p_flags & PF_X ? 'E' : ' '));
6145 print_vma (segment->p_align, PREFIX_HEX);
6146 }
6147
6148 putc ('\n', stdout);
6149 }
6150
6151 switch (segment->p_type)
6152 {
6153 case PT_LOAD:
6154 #if 0 /* Do not warn about out of order PT_LOAD segments. Although officially
6155 required by the ELF standard, several programs, including the Linux
6156 kernel, make use of non-ordered segments. */
6157 if (previous_load
6158 && previous_load->p_vaddr > segment->p_vaddr)
6159 error (_("LOAD segments must be sorted in order of increasing VirtAddr\n"));
6160 #endif
6161 if (segment->p_memsz < segment->p_filesz)
6162 error (_("the segment's file size is larger than its memory size\n"));
6163 previous_load = segment;
6164 break;
6165
6166 case PT_PHDR:
6167 /* PR 20815 - Verify that the program header is loaded into memory. */
6168 if (i > 0 && previous_load != NULL)
6169 error (_("the PHDR segment must occur before any LOAD segment\n"));
6170 if (filedata->file_header.e_machine != EM_PARISC)
6171 {
6172 unsigned int j;
6173
6174 for (j = 1; j < filedata->file_header.e_phnum; j++)
6175 {
6176 Elf_Internal_Phdr *load = filedata->program_headers + j;
6177 if (load->p_type == PT_LOAD
6178 && load->p_offset <= segment->p_offset
6179 && (load->p_offset + load->p_filesz
6180 >= segment->p_offset + segment->p_filesz)
6181 && load->p_vaddr <= segment->p_vaddr
6182 && (load->p_vaddr + load->p_filesz
6183 >= segment->p_vaddr + segment->p_filesz))
6184 break;
6185 }
6186 if (j == filedata->file_header.e_phnum)
6187 error (_("the PHDR segment is not covered by a LOAD segment\n"));
6188 }
6189 break;
6190
6191 case PT_DYNAMIC:
6192 if (dynamic_addr)
6193 error (_("more than one dynamic segment\n"));
6194
6195 /* By default, assume that the .dynamic section is the first
6196 section in the DYNAMIC segment. */
6197 dynamic_addr = segment->p_offset;
6198 dynamic_size = segment->p_filesz;
6199
6200 /* Try to locate the .dynamic section. If there is
6201 a section header table, we can easily locate it. */
6202 if (filedata->section_headers != NULL)
6203 {
6204 Elf_Internal_Shdr * sec;
6205
6206 sec = find_section (filedata, ".dynamic");
6207 if (sec == NULL || sec->sh_size == 0)
6208 {
6209 /* A corresponding .dynamic section is expected, but on
6210 IA-64/OpenVMS it is OK for it to be missing. */
6211 if (!is_ia64_vms (filedata))
6212 error (_("no .dynamic section in the dynamic segment\n"));
6213 break;
6214 }
6215
6216 if (sec->sh_type == SHT_NOBITS)
6217 {
6218 dynamic_addr = 0;
6219 dynamic_size = 0;
6220 break;
6221 }
6222
6223 dynamic_addr = sec->sh_offset;
6224 dynamic_size = sec->sh_size;
6225
6226 /* The PT_DYNAMIC segment, which is used by the run-time
6227 loader, should exactly match the .dynamic section. */
6228 if (do_checks
6229 && (dynamic_addr != segment->p_offset
6230 || dynamic_size != segment->p_filesz))
6231 warn (_("\
6232 the .dynamic section is not the same as the dynamic segment\n"));
6233 }
6234
6235 /* PR binutils/17512: Avoid corrupt dynamic section info in the
6236 segment. Check this after matching against the section headers
6237 so we don't warn on debuginfo file (which have NOBITS .dynamic
6238 sections). */
6239 if (dynamic_addr > filedata->file_size
6240 || (dynamic_size > filedata->file_size - dynamic_addr))
6241 {
6242 error (_("the dynamic segment offset + size exceeds the size of the file\n"));
6243 dynamic_addr = 0;
6244 dynamic_size = 0;
6245 }
6246 break;
6247
6248 case PT_INTERP:
6249 if (segment->p_offset >= filedata->file_size
6250 || segment->p_filesz > filedata->file_size - segment->p_offset
6251 || segment->p_filesz - 1 >= (size_t) -2
6252 || fseek (filedata->handle,
6253 filedata->archive_file_offset + (long) segment->p_offset,
6254 SEEK_SET))
6255 error (_("Unable to find program interpreter name\n"));
6256 else
6257 {
6258 size_t len = segment->p_filesz;
6259 free (filedata->program_interpreter);
6260 filedata->program_interpreter = xmalloc (len + 1);
6261 len = fread (filedata->program_interpreter, 1, len,
6262 filedata->handle);
6263 filedata->program_interpreter[len] = 0;
6264
6265 if (do_segments)
6266 printf (_(" [Requesting program interpreter: %s]\n"),
6267 filedata->program_interpreter);
6268 }
6269 break;
6270 }
6271 }
6272
6273 if (do_segments
6274 && filedata->section_headers != NULL
6275 && filedata->string_table != NULL)
6276 {
6277 printf (_("\n Section to Segment mapping:\n"));
6278 printf (_(" Segment Sections...\n"));
6279
6280 for (i = 0; i < filedata->file_header.e_phnum; i++)
6281 {
6282 unsigned int j;
6283 Elf_Internal_Shdr * section;
6284
6285 segment = filedata->program_headers + i;
6286 section = filedata->section_headers + 1;
6287
6288 printf (" %2.2d ", i);
6289
6290 for (j = 1; j < filedata->file_header.e_shnum; j++, section++)
6291 {
6292 if (!ELF_TBSS_SPECIAL (section, segment)
6293 && ELF_SECTION_IN_SEGMENT_STRICT (section, segment))
6294 printf ("%s ", printable_section_name (filedata, section));
6295 }
6296
6297 putc ('\n',stdout);
6298 }
6299 }
6300
6301 filedata->dynamic_addr = dynamic_addr;
6302 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
6303 return;
6304
6305 no_headers:
6306 filedata->dynamic_addr = 0;
6307 filedata->dynamic_size = 1;
6308 }
6309
6310
6311 /* Find the file offset corresponding to VMA by using the program headers. */
6312
6313 static long
6314 offset_from_vma (Filedata * filedata, bfd_vma vma, bfd_size_type size)
6315 {
6316 Elf_Internal_Phdr * seg;
6317
6318 if (! get_program_headers (filedata))
6319 {
6320 warn (_("Cannot interpret virtual addresses without program headers.\n"));
6321 return (long) vma;
6322 }
6323
6324 for (seg = filedata->program_headers;
6325 seg < filedata->program_headers + filedata->file_header.e_phnum;
6326 ++seg)
6327 {
6328 if (seg->p_type != PT_LOAD)
6329 continue;
6330
6331 if (vma >= (seg->p_vaddr & -seg->p_align)
6332 && vma + size <= seg->p_vaddr + seg->p_filesz)
6333 return vma - seg->p_vaddr + seg->p_offset;
6334 }
6335
6336 warn (_("Virtual address 0x%lx not located in any PT_LOAD segment.\n"),
6337 (unsigned long) vma);
6338 return (long) vma;
6339 }
6340
6341
6342 /* Allocate memory and load the sections headers into FILEDATA->filedata->section_headers.
6343 If PROBE is true, this is just a probe and we do not generate any error
6344 messages if the load fails. */
6345
6346 static bool
6347 get_32bit_section_headers (Filedata * filedata, bool probe)
6348 {
6349 Elf32_External_Shdr * shdrs;
6350 Elf_Internal_Shdr * internal;
6351 unsigned int i;
6352 unsigned int size = filedata->file_header.e_shentsize;
6353 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
6354
6355 /* PR binutils/17531: Cope with unexpected section header sizes. */
6356 if (size == 0 || num == 0)
6357 return false;
6358 if (size < sizeof * shdrs)
6359 {
6360 if (! probe)
6361 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
6362 return false;
6363 }
6364 if (!probe && size > sizeof * shdrs)
6365 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
6366
6367 shdrs = (Elf32_External_Shdr *) get_data (NULL, filedata, filedata->file_header.e_shoff,
6368 size, num,
6369 probe ? NULL : _("section headers"));
6370 if (shdrs == NULL)
6371 return false;
6372
6373 filedata->section_headers = (Elf_Internal_Shdr *)
6374 cmalloc (num, sizeof (Elf_Internal_Shdr));
6375 if (filedata->section_headers == NULL)
6376 {
6377 if (!probe)
6378 error (_("Out of memory reading %u section headers\n"), num);
6379 free (shdrs);
6380 return false;
6381 }
6382
6383 for (i = 0, internal = filedata->section_headers;
6384 i < num;
6385 i++, internal++)
6386 {
6387 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
6388 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
6389 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
6390 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
6391 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
6392 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
6393 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
6394 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
6395 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
6396 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
6397 if (!probe && internal->sh_link > num)
6398 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
6399 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
6400 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
6401 }
6402
6403 free (shdrs);
6404 return true;
6405 }
6406
6407 /* Like get_32bit_section_headers, except that it fetches 64-bit headers. */
6408
6409 static bool
6410 get_64bit_section_headers (Filedata * filedata, bool probe)
6411 {
6412 Elf64_External_Shdr * shdrs;
6413 Elf_Internal_Shdr * internal;
6414 unsigned int i;
6415 unsigned int size = filedata->file_header.e_shentsize;
6416 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
6417
6418 /* PR binutils/17531: Cope with unexpected section header sizes. */
6419 if (size == 0 || num == 0)
6420 return false;
6421
6422 if (size < sizeof * shdrs)
6423 {
6424 if (! probe)
6425 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
6426 return false;
6427 }
6428
6429 if (! probe && size > sizeof * shdrs)
6430 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
6431
6432 shdrs = (Elf64_External_Shdr *) get_data (NULL, filedata,
6433 filedata->file_header.e_shoff,
6434 size, num,
6435 probe ? NULL : _("section headers"));
6436 if (shdrs == NULL)
6437 return false;
6438
6439 filedata->section_headers = (Elf_Internal_Shdr *)
6440 cmalloc (num, sizeof (Elf_Internal_Shdr));
6441 if (filedata->section_headers == NULL)
6442 {
6443 if (! probe)
6444 error (_("Out of memory reading %u section headers\n"), num);
6445 free (shdrs);
6446 return false;
6447 }
6448
6449 for (i = 0, internal = filedata->section_headers;
6450 i < num;
6451 i++, internal++)
6452 {
6453 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
6454 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
6455 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
6456 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
6457 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
6458 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
6459 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
6460 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
6461 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
6462 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
6463 if (!probe && internal->sh_link > num)
6464 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
6465 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
6466 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
6467 }
6468
6469 free (shdrs);
6470 return true;
6471 }
6472
6473 static bool
6474 get_section_headers (Filedata *filedata, bool probe)
6475 {
6476 if (filedata->section_headers != NULL)
6477 return true;
6478
6479 if (is_32bit_elf)
6480 return get_32bit_section_headers (filedata, probe);
6481 else
6482 return get_64bit_section_headers (filedata, probe);
6483 }
6484
6485 static Elf_Internal_Sym *
6486 get_32bit_elf_symbols (Filedata * filedata,
6487 Elf_Internal_Shdr * section,
6488 unsigned long * num_syms_return)
6489 {
6490 unsigned long number = 0;
6491 Elf32_External_Sym * esyms = NULL;
6492 Elf_External_Sym_Shndx * shndx = NULL;
6493 Elf_Internal_Sym * isyms = NULL;
6494 Elf_Internal_Sym * psym;
6495 unsigned int j;
6496 elf_section_list * entry;
6497
6498 if (section->sh_size == 0)
6499 {
6500 if (num_syms_return != NULL)
6501 * num_syms_return = 0;
6502 return NULL;
6503 }
6504
6505 /* Run some sanity checks first. */
6506 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
6507 {
6508 error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
6509 printable_section_name (filedata, section),
6510 (unsigned long) section->sh_entsize);
6511 goto exit_point;
6512 }
6513
6514 if (section->sh_size > filedata->file_size)
6515 {
6516 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
6517 printable_section_name (filedata, section),
6518 (unsigned long) section->sh_size);
6519 goto exit_point;
6520 }
6521
6522 number = section->sh_size / section->sh_entsize;
6523
6524 if (number * sizeof (Elf32_External_Sym) > section->sh_size + 1)
6525 {
6526 error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
6527 (unsigned long) section->sh_size,
6528 printable_section_name (filedata, section),
6529 (unsigned long) section->sh_entsize);
6530 goto exit_point;
6531 }
6532
6533 esyms = (Elf32_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
6534 section->sh_size, _("symbols"));
6535 if (esyms == NULL)
6536 goto exit_point;
6537
6538 shndx = NULL;
6539 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
6540 {
6541 if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
6542 continue;
6543
6544 if (shndx != NULL)
6545 {
6546 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
6547 free (shndx);
6548 }
6549
6550 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
6551 entry->hdr->sh_offset,
6552 1, entry->hdr->sh_size,
6553 _("symbol table section indices"));
6554 if (shndx == NULL)
6555 goto exit_point;
6556
6557 /* PR17531: file: heap-buffer-overflow */
6558 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
6559 {
6560 error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
6561 printable_section_name (filedata, entry->hdr),
6562 (unsigned long) entry->hdr->sh_size,
6563 (unsigned long) section->sh_size);
6564 goto exit_point;
6565 }
6566 }
6567
6568 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
6569
6570 if (isyms == NULL)
6571 {
6572 error (_("Out of memory reading %lu symbols\n"),
6573 (unsigned long) number);
6574 goto exit_point;
6575 }
6576
6577 for (j = 0, psym = isyms; j < number; j++, psym++)
6578 {
6579 psym->st_name = BYTE_GET (esyms[j].st_name);
6580 psym->st_value = BYTE_GET (esyms[j].st_value);
6581 psym->st_size = BYTE_GET (esyms[j].st_size);
6582 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
6583 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
6584 psym->st_shndx
6585 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
6586 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
6587 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
6588 psym->st_info = BYTE_GET (esyms[j].st_info);
6589 psym->st_other = BYTE_GET (esyms[j].st_other);
6590 }
6591
6592 exit_point:
6593 free (shndx);
6594 free (esyms);
6595
6596 if (num_syms_return != NULL)
6597 * num_syms_return = isyms == NULL ? 0 : number;
6598
6599 return isyms;
6600 }
6601
6602 static Elf_Internal_Sym *
6603 get_64bit_elf_symbols (Filedata * filedata,
6604 Elf_Internal_Shdr * section,
6605 unsigned long * num_syms_return)
6606 {
6607 unsigned long number = 0;
6608 Elf64_External_Sym * esyms = NULL;
6609 Elf_External_Sym_Shndx * shndx = NULL;
6610 Elf_Internal_Sym * isyms = NULL;
6611 Elf_Internal_Sym * psym;
6612 unsigned int j;
6613 elf_section_list * entry;
6614
6615 if (section->sh_size == 0)
6616 {
6617 if (num_syms_return != NULL)
6618 * num_syms_return = 0;
6619 return NULL;
6620 }
6621
6622 /* Run some sanity checks first. */
6623 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
6624 {
6625 error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
6626 printable_section_name (filedata, section),
6627 (unsigned long) section->sh_entsize);
6628 goto exit_point;
6629 }
6630
6631 if (section->sh_size > filedata->file_size)
6632 {
6633 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
6634 printable_section_name (filedata, section),
6635 (unsigned long) section->sh_size);
6636 goto exit_point;
6637 }
6638
6639 number = section->sh_size / section->sh_entsize;
6640
6641 if (number * sizeof (Elf64_External_Sym) > section->sh_size + 1)
6642 {
6643 error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
6644 (unsigned long) section->sh_size,
6645 printable_section_name (filedata, section),
6646 (unsigned long) section->sh_entsize);
6647 goto exit_point;
6648 }
6649
6650 esyms = (Elf64_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
6651 section->sh_size, _("symbols"));
6652 if (!esyms)
6653 goto exit_point;
6654
6655 shndx = NULL;
6656 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
6657 {
6658 if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
6659 continue;
6660
6661 if (shndx != NULL)
6662 {
6663 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
6664 free (shndx);
6665 }
6666
6667 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
6668 entry->hdr->sh_offset,
6669 1, entry->hdr->sh_size,
6670 _("symbol table section indices"));
6671 if (shndx == NULL)
6672 goto exit_point;
6673
6674 /* PR17531: file: heap-buffer-overflow */
6675 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
6676 {
6677 error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
6678 printable_section_name (filedata, entry->hdr),
6679 (unsigned long) entry->hdr->sh_size,
6680 (unsigned long) section->sh_size);
6681 goto exit_point;
6682 }
6683 }
6684
6685 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
6686
6687 if (isyms == NULL)
6688 {
6689 error (_("Out of memory reading %lu symbols\n"),
6690 (unsigned long) number);
6691 goto exit_point;
6692 }
6693
6694 for (j = 0, psym = isyms; j < number; j++, psym++)
6695 {
6696 psym->st_name = BYTE_GET (esyms[j].st_name);
6697 psym->st_info = BYTE_GET (esyms[j].st_info);
6698 psym->st_other = BYTE_GET (esyms[j].st_other);
6699 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
6700
6701 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
6702 psym->st_shndx
6703 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
6704 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
6705 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
6706
6707 psym->st_value = BYTE_GET (esyms[j].st_value);
6708 psym->st_size = BYTE_GET (esyms[j].st_size);
6709 }
6710
6711 exit_point:
6712 free (shndx);
6713 free (esyms);
6714
6715 if (num_syms_return != NULL)
6716 * num_syms_return = isyms == NULL ? 0 : number;
6717
6718 return isyms;
6719 }
6720
6721 static Elf_Internal_Sym *
6722 get_elf_symbols (Filedata *filedata,
6723 Elf_Internal_Shdr *section,
6724 unsigned long *num_syms_return)
6725 {
6726 if (is_32bit_elf)
6727 return get_32bit_elf_symbols (filedata, section, num_syms_return);
6728 else
6729 return get_64bit_elf_symbols (filedata, section, num_syms_return);
6730 }
6731
6732 static const char *
6733 get_elf_section_flags (Filedata * filedata, bfd_vma sh_flags)
6734 {
6735 static char buff[1024];
6736 char * p = buff;
6737 unsigned int field_size = is_32bit_elf ? 8 : 16;
6738 signed int sindex;
6739 unsigned int size = sizeof (buff) - (field_size + 4 + 1);
6740 bfd_vma os_flags = 0;
6741 bfd_vma proc_flags = 0;
6742 bfd_vma unknown_flags = 0;
6743 static const struct
6744 {
6745 const char * str;
6746 unsigned int len;
6747 }
6748 flags [] =
6749 {
6750 /* 0 */ { STRING_COMMA_LEN ("WRITE") },
6751 /* 1 */ { STRING_COMMA_LEN ("ALLOC") },
6752 /* 2 */ { STRING_COMMA_LEN ("EXEC") },
6753 /* 3 */ { STRING_COMMA_LEN ("MERGE") },
6754 /* 4 */ { STRING_COMMA_LEN ("STRINGS") },
6755 /* 5 */ { STRING_COMMA_LEN ("INFO LINK") },
6756 /* 6 */ { STRING_COMMA_LEN ("LINK ORDER") },
6757 /* 7 */ { STRING_COMMA_LEN ("OS NONCONF") },
6758 /* 8 */ { STRING_COMMA_LEN ("GROUP") },
6759 /* 9 */ { STRING_COMMA_LEN ("TLS") },
6760 /* IA-64 specific. */
6761 /* 10 */ { STRING_COMMA_LEN ("SHORT") },
6762 /* 11 */ { STRING_COMMA_LEN ("NORECOV") },
6763 /* IA-64 OpenVMS specific. */
6764 /* 12 */ { STRING_COMMA_LEN ("VMS_GLOBAL") },
6765 /* 13 */ { STRING_COMMA_LEN ("VMS_OVERLAID") },
6766 /* 14 */ { STRING_COMMA_LEN ("VMS_SHARED") },
6767 /* 15 */ { STRING_COMMA_LEN ("VMS_VECTOR") },
6768 /* 16 */ { STRING_COMMA_LEN ("VMS_ALLOC_64BIT") },
6769 /* 17 */ { STRING_COMMA_LEN ("VMS_PROTECTED") },
6770 /* Generic. */
6771 /* 18 */ { STRING_COMMA_LEN ("EXCLUDE") },
6772 /* SPARC specific. */
6773 /* 19 */ { STRING_COMMA_LEN ("ORDERED") },
6774 /* 20 */ { STRING_COMMA_LEN ("COMPRESSED") },
6775 /* ARM specific. */
6776 /* 21 */ { STRING_COMMA_LEN ("ENTRYSECT") },
6777 /* 22 */ { STRING_COMMA_LEN ("ARM_PURECODE") },
6778 /* 23 */ { STRING_COMMA_LEN ("COMDEF") },
6779 /* GNU specific. */
6780 /* 24 */ { STRING_COMMA_LEN ("GNU_MBIND") },
6781 /* VLE specific. */
6782 /* 25 */ { STRING_COMMA_LEN ("VLE") },
6783 /* GNU specific. */
6784 /* 26 */ { STRING_COMMA_LEN ("GNU_RETAIN") },
6785 };
6786
6787 if (do_section_details)
6788 {
6789 sprintf (buff, "[%*.*lx]: ",
6790 field_size, field_size, (unsigned long) sh_flags);
6791 p += field_size + 4;
6792 }
6793
6794 while (sh_flags)
6795 {
6796 bfd_vma flag;
6797
6798 flag = sh_flags & - sh_flags;
6799 sh_flags &= ~ flag;
6800
6801 if (do_section_details)
6802 {
6803 switch (flag)
6804 {
6805 case SHF_WRITE: sindex = 0; break;
6806 case SHF_ALLOC: sindex = 1; break;
6807 case SHF_EXECINSTR: sindex = 2; break;
6808 case SHF_MERGE: sindex = 3; break;
6809 case SHF_STRINGS: sindex = 4; break;
6810 case SHF_INFO_LINK: sindex = 5; break;
6811 case SHF_LINK_ORDER: sindex = 6; break;
6812 case SHF_OS_NONCONFORMING: sindex = 7; break;
6813 case SHF_GROUP: sindex = 8; break;
6814 case SHF_TLS: sindex = 9; break;
6815 case SHF_EXCLUDE: sindex = 18; break;
6816 case SHF_COMPRESSED: sindex = 20; break;
6817
6818 default:
6819 sindex = -1;
6820 switch (filedata->file_header.e_machine)
6821 {
6822 case EM_IA_64:
6823 if (flag == SHF_IA_64_SHORT)
6824 sindex = 10;
6825 else if (flag == SHF_IA_64_NORECOV)
6826 sindex = 11;
6827 #ifdef BFD64
6828 else if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
6829 switch (flag)
6830 {
6831 case SHF_IA_64_VMS_GLOBAL: sindex = 12; break;
6832 case SHF_IA_64_VMS_OVERLAID: sindex = 13; break;
6833 case SHF_IA_64_VMS_SHARED: sindex = 14; break;
6834 case SHF_IA_64_VMS_VECTOR: sindex = 15; break;
6835 case SHF_IA_64_VMS_ALLOC_64BIT: sindex = 16; break;
6836 case SHF_IA_64_VMS_PROTECTED: sindex = 17; break;
6837 default: break;
6838 }
6839 #endif
6840 break;
6841
6842 case EM_386:
6843 case EM_IAMCU:
6844 case EM_X86_64:
6845 case EM_L1OM:
6846 case EM_K1OM:
6847 case EM_OLD_SPARCV9:
6848 case EM_SPARC32PLUS:
6849 case EM_SPARCV9:
6850 case EM_SPARC:
6851 if (flag == SHF_ORDERED)
6852 sindex = 19;
6853 break;
6854
6855 case EM_ARM:
6856 switch (flag)
6857 {
6858 case SHF_ENTRYSECT: sindex = 21; break;
6859 case SHF_ARM_PURECODE: sindex = 22; break;
6860 case SHF_COMDEF: sindex = 23; break;
6861 default: break;
6862 }
6863 break;
6864 case EM_PPC:
6865 if (flag == SHF_PPC_VLE)
6866 sindex = 25;
6867 break;
6868 default:
6869 break;
6870 }
6871
6872 switch (filedata->file_header.e_ident[EI_OSABI])
6873 {
6874 case ELFOSABI_GNU:
6875 case ELFOSABI_FREEBSD:
6876 if (flag == SHF_GNU_RETAIN)
6877 sindex = 26;
6878 /* Fall through */
6879 case ELFOSABI_NONE:
6880 if (flag == SHF_GNU_MBIND)
6881 /* We should not recognize SHF_GNU_MBIND for
6882 ELFOSABI_NONE, but binutils as of 2019-07-23 did
6883 not set the EI_OSABI header byte. */
6884 sindex = 24;
6885 break;
6886 default:
6887 break;
6888 }
6889 break;
6890 }
6891
6892 if (sindex != -1)
6893 {
6894 if (p != buff + field_size + 4)
6895 {
6896 if (size < (10 + 2))
6897 {
6898 warn (_("Internal error: not enough buffer room for section flag info"));
6899 return _("<unknown>");
6900 }
6901 size -= 2;
6902 *p++ = ',';
6903 *p++ = ' ';
6904 }
6905
6906 size -= flags [sindex].len;
6907 p = stpcpy (p, flags [sindex].str);
6908 }
6909 else if (flag & SHF_MASKOS)
6910 os_flags |= flag;
6911 else if (flag & SHF_MASKPROC)
6912 proc_flags |= flag;
6913 else
6914 unknown_flags |= flag;
6915 }
6916 else
6917 {
6918 switch (flag)
6919 {
6920 case SHF_WRITE: *p = 'W'; break;
6921 case SHF_ALLOC: *p = 'A'; break;
6922 case SHF_EXECINSTR: *p = 'X'; break;
6923 case SHF_MERGE: *p = 'M'; break;
6924 case SHF_STRINGS: *p = 'S'; break;
6925 case SHF_INFO_LINK: *p = 'I'; break;
6926 case SHF_LINK_ORDER: *p = 'L'; break;
6927 case SHF_OS_NONCONFORMING: *p = 'O'; break;
6928 case SHF_GROUP: *p = 'G'; break;
6929 case SHF_TLS: *p = 'T'; break;
6930 case SHF_EXCLUDE: *p = 'E'; break;
6931 case SHF_COMPRESSED: *p = 'C'; break;
6932
6933 default:
6934 if ((filedata->file_header.e_machine == EM_X86_64
6935 || filedata->file_header.e_machine == EM_L1OM
6936 || filedata->file_header.e_machine == EM_K1OM)
6937 && flag == SHF_X86_64_LARGE)
6938 *p = 'l';
6939 else if (filedata->file_header.e_machine == EM_ARM
6940 && flag == SHF_ARM_PURECODE)
6941 *p = 'y';
6942 else if (filedata->file_header.e_machine == EM_PPC
6943 && flag == SHF_PPC_VLE)
6944 *p = 'v';
6945 else if (flag & SHF_MASKOS)
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 {
6953 *p = 'R';
6954 break;
6955 }
6956 /* Fall through */
6957 case ELFOSABI_NONE:
6958 if (flag == SHF_GNU_MBIND)
6959 {
6960 /* We should not recognize SHF_GNU_MBIND for
6961 ELFOSABI_NONE, but binutils as of 2019-07-23 did
6962 not set the EI_OSABI header byte. */
6963 *p = 'D';
6964 break;
6965 }
6966 /* Fall through */
6967 default:
6968 *p = 'o';
6969 sh_flags &= ~SHF_MASKOS;
6970 break;
6971 }
6972 }
6973 else if (flag & SHF_MASKPROC)
6974 {
6975 *p = 'p';
6976 sh_flags &= ~ SHF_MASKPROC;
6977 }
6978 else
6979 *p = 'x';
6980 break;
6981 }
6982 p++;
6983 }
6984 }
6985
6986 if (do_section_details)
6987 {
6988 if (os_flags)
6989 {
6990 size -= 5 + field_size;
6991 if (p != buff + field_size + 4)
6992 {
6993 if (size < (2 + 1))
6994 {
6995 warn (_("Internal error: not enough buffer room for section flag info"));
6996 return _("<unknown>");
6997 }
6998 size -= 2;
6999 *p++ = ',';
7000 *p++ = ' ';
7001 }
7002 sprintf (p, "OS (%*.*lx)", field_size, field_size,
7003 (unsigned long) os_flags);
7004 p += 5 + field_size;
7005 }
7006 if (proc_flags)
7007 {
7008 size -= 7 + field_size;
7009 if (p != buff + field_size + 4)
7010 {
7011 if (size < (2 + 1))
7012 {
7013 warn (_("Internal error: not enough buffer room for section flag info"));
7014 return _("<unknown>");
7015 }
7016 size -= 2;
7017 *p++ = ',';
7018 *p++ = ' ';
7019 }
7020 sprintf (p, "PROC (%*.*lx)", field_size, field_size,
7021 (unsigned long) proc_flags);
7022 p += 7 + field_size;
7023 }
7024 if (unknown_flags)
7025 {
7026 size -= 10 + field_size;
7027 if (p != buff + field_size + 4)
7028 {
7029 if (size < (2 + 1))
7030 {
7031 warn (_("Internal error: not enough buffer room for section flag info"));
7032 return _("<unknown>");
7033 }
7034 size -= 2;
7035 *p++ = ',';
7036 *p++ = ' ';
7037 }
7038 sprintf (p, _("UNKNOWN (%*.*lx)"), field_size, field_size,
7039 (unsigned long) unknown_flags);
7040 p += 10 + field_size;
7041 }
7042 }
7043
7044 *p = '\0';
7045 return buff;
7046 }
7047
7048 static unsigned int ATTRIBUTE_WARN_UNUSED_RESULT
7049 get_compression_header (Elf_Internal_Chdr *chdr, unsigned char *buf, bfd_size_type size)
7050 {
7051 if (is_32bit_elf)
7052 {
7053 Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) buf;
7054
7055 if (size < sizeof (* echdr))
7056 {
7057 error (_("Compressed section is too small even for a compression header\n"));
7058 return 0;
7059 }
7060
7061 chdr->ch_type = BYTE_GET (echdr->ch_type);
7062 chdr->ch_size = BYTE_GET (echdr->ch_size);
7063 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
7064 return sizeof (*echdr);
7065 }
7066 else
7067 {
7068 Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) buf;
7069
7070 if (size < sizeof (* echdr))
7071 {
7072 error (_("Compressed section is too small even for a compression header\n"));
7073 return 0;
7074 }
7075
7076 chdr->ch_type = BYTE_GET (echdr->ch_type);
7077 chdr->ch_size = BYTE_GET (echdr->ch_size);
7078 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
7079 return sizeof (*echdr);
7080 }
7081 }
7082
7083 static bool
7084 process_section_headers (Filedata * filedata)
7085 {
7086 Elf_Internal_Shdr * section;
7087 unsigned int i;
7088
7089 if (filedata->file_header.e_shnum == 0)
7090 {
7091 /* PR binutils/12467. */
7092 if (filedata->file_header.e_shoff != 0)
7093 {
7094 warn (_("possibly corrupt ELF file header - it has a non-zero"
7095 " section header offset, but no section headers\n"));
7096 return false;
7097 }
7098 else if (do_sections)
7099 printf (_("\nThere are no sections in this file.\n"));
7100
7101 return true;
7102 }
7103
7104 if (do_sections && !do_header)
7105 {
7106 if (filedata->is_separate && process_links)
7107 printf (_("In linked file '%s': "), filedata->file_name);
7108 if (! filedata->is_separate || process_links)
7109 printf (ngettext ("There is %d section header, "
7110 "starting at offset 0x%lx:\n",
7111 "There are %d section headers, "
7112 "starting at offset 0x%lx:\n",
7113 filedata->file_header.e_shnum),
7114 filedata->file_header.e_shnum,
7115 (unsigned long) filedata->file_header.e_shoff);
7116 }
7117
7118 if (!get_section_headers (filedata, false))
7119 return false;
7120
7121 /* Read in the string table, so that we have names to display. */
7122 if (filedata->file_header.e_shstrndx != SHN_UNDEF
7123 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
7124 {
7125 section = filedata->section_headers + filedata->file_header.e_shstrndx;
7126
7127 if (section->sh_size != 0)
7128 {
7129 filedata->string_table = (char *) get_data (NULL, filedata, section->sh_offset,
7130 1, section->sh_size,
7131 _("string table"));
7132
7133 filedata->string_table_length = filedata->string_table != NULL ? section->sh_size : 0;
7134 }
7135 }
7136
7137 /* Scan the sections for the dynamic symbol table
7138 and dynamic string table and debug sections. */
7139 eh_addr_size = is_32bit_elf ? 4 : 8;
7140 switch (filedata->file_header.e_machine)
7141 {
7142 case EM_MIPS:
7143 case EM_MIPS_RS3_LE:
7144 /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
7145 FDE addresses. However, the ABI also has a semi-official ILP32
7146 variant for which the normal FDE address size rules apply.
7147
7148 GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
7149 section, where XX is the size of longs in bits. Unfortunately,
7150 earlier compilers provided no way of distinguishing ILP32 objects
7151 from LP64 objects, so if there's any doubt, we should assume that
7152 the official LP64 form is being used. */
7153 if ((filedata->file_header.e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64
7154 && find_section (filedata, ".gcc_compiled_long32") == NULL)
7155 eh_addr_size = 8;
7156 break;
7157
7158 case EM_H8_300:
7159 case EM_H8_300H:
7160 switch (filedata->file_header.e_flags & EF_H8_MACH)
7161 {
7162 case E_H8_MACH_H8300:
7163 case E_H8_MACH_H8300HN:
7164 case E_H8_MACH_H8300SN:
7165 case E_H8_MACH_H8300SXN:
7166 eh_addr_size = 2;
7167 break;
7168 case E_H8_MACH_H8300H:
7169 case E_H8_MACH_H8300S:
7170 case E_H8_MACH_H8300SX:
7171 eh_addr_size = 4;
7172 break;
7173 }
7174 break;
7175
7176 case EM_M32C_OLD:
7177 case EM_M32C:
7178 switch (filedata->file_header.e_flags & EF_M32C_CPU_MASK)
7179 {
7180 case EF_M32C_CPU_M16C:
7181 eh_addr_size = 2;
7182 break;
7183 }
7184 break;
7185 }
7186
7187 #define CHECK_ENTSIZE_VALUES(section, i, size32, size64) \
7188 do \
7189 { \
7190 bfd_size_type expected_entsize = is_32bit_elf ? size32 : size64; \
7191 if (section->sh_entsize != expected_entsize) \
7192 { \
7193 char buf[40]; \
7194 sprintf_vma (buf, section->sh_entsize); \
7195 /* Note: coded this way so that there is a single string for \
7196 translation. */ \
7197 error (_("Section %d has invalid sh_entsize of %s\n"), i, buf); \
7198 error (_("(Using the expected size of %u for the rest of this dump)\n"), \
7199 (unsigned) expected_entsize); \
7200 section->sh_entsize = expected_entsize; \
7201 } \
7202 } \
7203 while (0)
7204
7205 #define CHECK_ENTSIZE(section, i, type) \
7206 CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type), \
7207 sizeof (Elf64_External_##type))
7208
7209 for (i = 0, section = filedata->section_headers;
7210 i < filedata->file_header.e_shnum;
7211 i++, section++)
7212 {
7213 const char *name = section_name_print (filedata, section);
7214
7215 /* Run some sanity checks on the headers and
7216 possibly fill in some file data as well. */
7217 switch (section->sh_type)
7218 {
7219 case SHT_DYNSYM:
7220 if (filedata->dynamic_symbols != NULL)
7221 {
7222 error (_("File contains multiple dynamic symbol tables\n"));
7223 continue;
7224 }
7225
7226 CHECK_ENTSIZE (section, i, Sym);
7227 filedata->dynamic_symbols
7228 = get_elf_symbols (filedata, section, &filedata->num_dynamic_syms);
7229 filedata->dynamic_symtab_section = section;
7230 break;
7231
7232 case SHT_STRTAB:
7233 if (streq (name, ".dynstr"))
7234 {
7235 if (filedata->dynamic_strings != NULL)
7236 {
7237 error (_("File contains multiple dynamic string tables\n"));
7238 continue;
7239 }
7240
7241 filedata->dynamic_strings
7242 = (char *) get_data (NULL, filedata, section->sh_offset,
7243 1, section->sh_size, _("dynamic strings"));
7244 filedata->dynamic_strings_length
7245 = filedata->dynamic_strings == NULL ? 0 : section->sh_size;
7246 filedata->dynamic_strtab_section = section;
7247 }
7248 break;
7249
7250 case SHT_SYMTAB_SHNDX:
7251 {
7252 elf_section_list * entry = xmalloc (sizeof * entry);
7253
7254 entry->hdr = section;
7255 entry->next = filedata->symtab_shndx_list;
7256 filedata->symtab_shndx_list = entry;
7257 }
7258 break;
7259
7260 case SHT_SYMTAB:
7261 CHECK_ENTSIZE (section, i, Sym);
7262 break;
7263
7264 case SHT_GROUP:
7265 CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
7266 break;
7267
7268 case SHT_REL:
7269 CHECK_ENTSIZE (section, i, Rel);
7270 if (do_checks && section->sh_size == 0)
7271 warn (_("Section '%s': zero-sized relocation section\n"), name);
7272 break;
7273
7274 case SHT_RELA:
7275 CHECK_ENTSIZE (section, i, Rela);
7276 if (do_checks && section->sh_size == 0)
7277 warn (_("Section '%s': zero-sized relocation section\n"), name);
7278 break;
7279
7280 case SHT_RELR:
7281 CHECK_ENTSIZE (section, i, Relr);
7282 break;
7283
7284 case SHT_NOTE:
7285 case SHT_PROGBITS:
7286 /* Having a zero sized section is not illegal according to the
7287 ELF standard, but it might be an indication that something
7288 is wrong. So issue a warning if we are running in lint mode. */
7289 if (do_checks && section->sh_size == 0)
7290 warn (_("Section '%s': has a size of zero - is this intended ?\n"), name);
7291 break;
7292
7293 default:
7294 break;
7295 }
7296
7297 if ((do_debugging || do_debug_info || do_debug_abbrevs
7298 || do_debug_lines || do_debug_pubnames || do_debug_pubtypes
7299 || do_debug_aranges || do_debug_frames || do_debug_macinfo
7300 || do_debug_str || do_debug_str_offsets || do_debug_loc
7301 || do_debug_ranges
7302 || do_debug_addr || do_debug_cu_index || do_debug_links)
7303 && (startswith (name, ".debug_")
7304 || startswith (name, ".zdebug_")))
7305 {
7306 if (name[1] == 'z')
7307 name += sizeof (".zdebug_") - 1;
7308 else
7309 name += sizeof (".debug_") - 1;
7310
7311 if (do_debugging
7312 || (do_debug_info && startswith (name, "info"))
7313 || (do_debug_info && startswith (name, "types"))
7314 || (do_debug_abbrevs && startswith (name, "abbrev"))
7315 || (do_debug_lines && strcmp (name, "line") == 0)
7316 || (do_debug_lines && startswith (name, "line."))
7317 || (do_debug_pubnames && startswith (name, "pubnames"))
7318 || (do_debug_pubtypes && startswith (name, "pubtypes"))
7319 || (do_debug_pubnames && startswith (name, "gnu_pubnames"))
7320 || (do_debug_pubtypes && startswith (name, "gnu_pubtypes"))
7321 || (do_debug_aranges && startswith (name, "aranges"))
7322 || (do_debug_ranges && startswith (name, "ranges"))
7323 || (do_debug_ranges && startswith (name, "rnglists"))
7324 || (do_debug_frames && startswith (name, "frame"))
7325 || (do_debug_macinfo && startswith (name, "macinfo"))
7326 || (do_debug_macinfo && startswith (name, "macro"))
7327 || (do_debug_str && startswith (name, "str"))
7328 || (do_debug_links && startswith (name, "sup"))
7329 || (do_debug_str_offsets && startswith (name, "str_offsets"))
7330 || (do_debug_loc && startswith (name, "loc"))
7331 || (do_debug_loc && startswith (name, "loclists"))
7332 || (do_debug_addr && startswith (name, "addr"))
7333 || (do_debug_cu_index && startswith (name, "cu_index"))
7334 || (do_debug_cu_index && startswith (name, "tu_index"))
7335 )
7336 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7337 }
7338 /* Linkonce section to be combined with .debug_info at link time. */
7339 else if ((do_debugging || do_debug_info)
7340 && startswith (name, ".gnu.linkonce.wi."))
7341 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7342 else if (do_debug_frames && streq (name, ".eh_frame"))
7343 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7344 else if (do_gdb_index && (streq (name, ".gdb_index")
7345 || streq (name, ".debug_names")))
7346 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7347 /* Trace sections for Itanium VMS. */
7348 else if ((do_debugging || do_trace_info || do_trace_abbrevs
7349 || do_trace_aranges)
7350 && startswith (name, ".trace_"))
7351 {
7352 name += sizeof (".trace_") - 1;
7353
7354 if (do_debugging
7355 || (do_trace_info && streq (name, "info"))
7356 || (do_trace_abbrevs && streq (name, "abbrev"))
7357 || (do_trace_aranges && streq (name, "aranges"))
7358 )
7359 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7360 }
7361 else if ((do_debugging || do_debug_links)
7362 && (startswith (name, ".gnu_debuglink")
7363 || startswith (name, ".gnu_debugaltlink")))
7364 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7365 }
7366
7367 if (! do_sections)
7368 return true;
7369
7370 if (filedata->is_separate && ! process_links)
7371 return true;
7372
7373 if (filedata->is_separate)
7374 printf (_("\nSection Headers in linked file '%s':\n"), filedata->file_name);
7375 else if (filedata->file_header.e_shnum > 1)
7376 printf (_("\nSection Headers:\n"));
7377 else
7378 printf (_("\nSection Header:\n"));
7379
7380 if (is_32bit_elf)
7381 {
7382 if (do_section_details)
7383 {
7384 printf (_(" [Nr] Name\n"));
7385 printf (_(" Type Addr Off Size ES Lk Inf Al\n"));
7386 }
7387 else
7388 printf
7389 (_(" [Nr] Name Type Addr Off Size ES Flg Lk Inf Al\n"));
7390 }
7391 else if (do_wide)
7392 {
7393 if (do_section_details)
7394 {
7395 printf (_(" [Nr] Name\n"));
7396 printf (_(" Type Address Off Size ES Lk Inf Al\n"));
7397 }
7398 else
7399 printf
7400 (_(" [Nr] Name Type Address Off Size ES Flg Lk Inf Al\n"));
7401 }
7402 else
7403 {
7404 if (do_section_details)
7405 {
7406 printf (_(" [Nr] Name\n"));
7407 printf (_(" Type Address Offset Link\n"));
7408 printf (_(" Size EntSize Info Align\n"));
7409 }
7410 else
7411 {
7412 printf (_(" [Nr] Name Type Address Offset\n"));
7413 printf (_(" Size EntSize Flags Link Info Align\n"));
7414 }
7415 }
7416
7417 if (do_section_details)
7418 printf (_(" Flags\n"));
7419
7420 for (i = 0, section = filedata->section_headers;
7421 i < filedata->file_header.e_shnum;
7422 i++, section++)
7423 {
7424 /* Run some sanity checks on the section header. */
7425
7426 /* Check the sh_link field. */
7427 switch (section->sh_type)
7428 {
7429 case SHT_REL:
7430 case SHT_RELA:
7431 if (section->sh_link == 0
7432 && (filedata->file_header.e_type == ET_EXEC
7433 || filedata->file_header.e_type == ET_DYN))
7434 /* A dynamic relocation section where all entries use a
7435 zero symbol index need not specify a symtab section. */
7436 break;
7437 /* Fall through. */
7438 case SHT_SYMTAB_SHNDX:
7439 case SHT_GROUP:
7440 case SHT_HASH:
7441 case SHT_GNU_HASH:
7442 case SHT_GNU_versym:
7443 if (section->sh_link == 0
7444 || section->sh_link >= filedata->file_header.e_shnum
7445 || (filedata->section_headers[section->sh_link].sh_type != SHT_SYMTAB
7446 && filedata->section_headers[section->sh_link].sh_type != SHT_DYNSYM))
7447 warn (_("[%2u]: Link field (%u) should index a symtab section.\n"),
7448 i, section->sh_link);
7449 break;
7450
7451 case SHT_DYNAMIC:
7452 case SHT_SYMTAB:
7453 case SHT_DYNSYM:
7454 case SHT_GNU_verneed:
7455 case SHT_GNU_verdef:
7456 case SHT_GNU_LIBLIST:
7457 if (section->sh_link == 0
7458 || section->sh_link >= filedata->file_header.e_shnum
7459 || filedata->section_headers[section->sh_link].sh_type != SHT_STRTAB)
7460 warn (_("[%2u]: Link field (%u) should index a string section.\n"),
7461 i, section->sh_link);
7462 break;
7463
7464 case SHT_INIT_ARRAY:
7465 case SHT_FINI_ARRAY:
7466 case SHT_PREINIT_ARRAY:
7467 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
7468 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
7469 i, section->sh_link);
7470 break;
7471
7472 default:
7473 /* FIXME: Add support for target specific section types. */
7474 #if 0 /* Currently we do not check other section types as there are too
7475 many special cases. Stab sections for example have a type
7476 of SHT_PROGBITS but an sh_link field that links to the .stabstr
7477 section. */
7478 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
7479 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
7480 i, section->sh_link);
7481 #endif
7482 break;
7483 }
7484
7485 /* Check the sh_info field. */
7486 switch (section->sh_type)
7487 {
7488 case SHT_REL:
7489 case SHT_RELA:
7490 if (section->sh_info == 0
7491 && (filedata->file_header.e_type == ET_EXEC
7492 || filedata->file_header.e_type == ET_DYN))
7493 /* Dynamic relocations apply to segments, so they do not
7494 need to specify the section they relocate. */
7495 break;
7496 if (section->sh_info == 0
7497 || section->sh_info >= filedata->file_header.e_shnum
7498 || (filedata->section_headers[section->sh_info].sh_type != SHT_PROGBITS
7499 && filedata->section_headers[section->sh_info].sh_type != SHT_NOBITS
7500 && filedata->section_headers[section->sh_info].sh_type != SHT_NOTE
7501 && filedata->section_headers[section->sh_info].sh_type != SHT_INIT_ARRAY
7502 && filedata->section_headers[section->sh_info].sh_type != SHT_FINI_ARRAY
7503 && filedata->section_headers[section->sh_info].sh_type != SHT_PREINIT_ARRAY
7504 /* FIXME: Are other section types valid ? */
7505 && filedata->section_headers[section->sh_info].sh_type < SHT_LOOS))
7506 warn (_("[%2u]: Info field (%u) should index a relocatable section.\n"),
7507 i, section->sh_info);
7508 break;
7509
7510 case SHT_DYNAMIC:
7511 case SHT_HASH:
7512 case SHT_SYMTAB_SHNDX:
7513 case SHT_INIT_ARRAY:
7514 case SHT_FINI_ARRAY:
7515 case SHT_PREINIT_ARRAY:
7516 if (section->sh_info != 0)
7517 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
7518 i, section->sh_info);
7519 break;
7520
7521 case SHT_GROUP:
7522 case SHT_SYMTAB:
7523 case SHT_DYNSYM:
7524 /* A symbol index - we assume that it is valid. */
7525 break;
7526
7527 default:
7528 /* FIXME: Add support for target specific section types. */
7529 if (section->sh_type == SHT_NOBITS)
7530 /* NOBITS section headers with non-zero sh_info fields can be
7531 created when a binary is stripped of everything but its debug
7532 information. The stripped sections have their headers
7533 preserved but their types set to SHT_NOBITS. So do not check
7534 this type of section. */
7535 ;
7536 else if (section->sh_flags & SHF_INFO_LINK)
7537 {
7538 if (section->sh_info < 1 || section->sh_info >= filedata->file_header.e_shnum)
7539 warn (_("[%2u]: Expected link to another section in info field"), i);
7540 }
7541 else if (section->sh_type < SHT_LOOS
7542 && (section->sh_flags & SHF_GNU_MBIND) == 0
7543 && section->sh_info != 0)
7544 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
7545 i, section->sh_info);
7546 break;
7547 }
7548
7549 /* Check the sh_size field. */
7550 if (section->sh_size > filedata->file_size
7551 && section->sh_type != SHT_NOBITS
7552 && section->sh_type != SHT_NULL
7553 && section->sh_type < SHT_LOOS)
7554 warn (_("Size of section %u is larger than the entire file!\n"), i);
7555
7556 printf (" [%2u] ", i);
7557 if (do_section_details)
7558 printf ("%s\n ", printable_section_name (filedata, section));
7559 else
7560 print_symbol (-17, section_name_print (filedata, section));
7561
7562 printf (do_wide ? " %-15s " : " %-15.15s ",
7563 get_section_type_name (filedata, section->sh_type));
7564
7565 if (is_32bit_elf)
7566 {
7567 const char * link_too_big = NULL;
7568
7569 print_vma (section->sh_addr, LONG_HEX);
7570
7571 printf ( " %6.6lx %6.6lx %2.2lx",
7572 (unsigned long) section->sh_offset,
7573 (unsigned long) section->sh_size,
7574 (unsigned long) section->sh_entsize);
7575
7576 if (do_section_details)
7577 fputs (" ", stdout);
7578 else
7579 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7580
7581 if (section->sh_link >= filedata->file_header.e_shnum)
7582 {
7583 link_too_big = "";
7584 /* The sh_link value is out of range. Normally this indicates
7585 an error but it can have special values in Solaris binaries. */
7586 switch (filedata->file_header.e_machine)
7587 {
7588 case EM_386:
7589 case EM_IAMCU:
7590 case EM_X86_64:
7591 case EM_L1OM:
7592 case EM_K1OM:
7593 case EM_OLD_SPARCV9:
7594 case EM_SPARC32PLUS:
7595 case EM_SPARCV9:
7596 case EM_SPARC:
7597 if (section->sh_link == (SHN_BEFORE & 0xffff))
7598 link_too_big = "BEFORE";
7599 else if (section->sh_link == (SHN_AFTER & 0xffff))
7600 link_too_big = "AFTER";
7601 break;
7602 default:
7603 break;
7604 }
7605 }
7606
7607 if (do_section_details)
7608 {
7609 if (link_too_big != NULL && * link_too_big)
7610 printf ("<%s> ", link_too_big);
7611 else
7612 printf ("%2u ", section->sh_link);
7613 printf ("%3u %2lu\n", section->sh_info,
7614 (unsigned long) section->sh_addralign);
7615 }
7616 else
7617 printf ("%2u %3u %2lu\n",
7618 section->sh_link,
7619 section->sh_info,
7620 (unsigned long) section->sh_addralign);
7621
7622 if (link_too_big && ! * link_too_big)
7623 warn (_("section %u: sh_link value of %u is larger than the number of sections\n"),
7624 i, section->sh_link);
7625 }
7626 else if (do_wide)
7627 {
7628 print_vma (section->sh_addr, LONG_HEX);
7629
7630 if ((long) section->sh_offset == section->sh_offset)
7631 printf (" %6.6lx", (unsigned long) section->sh_offset);
7632 else
7633 {
7634 putchar (' ');
7635 print_vma (section->sh_offset, LONG_HEX);
7636 }
7637
7638 if ((unsigned long) section->sh_size == section->sh_size)
7639 printf (" %6.6lx", (unsigned long) section->sh_size);
7640 else
7641 {
7642 putchar (' ');
7643 print_vma (section->sh_size, LONG_HEX);
7644 }
7645
7646 if ((unsigned long) section->sh_entsize == section->sh_entsize)
7647 printf (" %2.2lx", (unsigned long) section->sh_entsize);
7648 else
7649 {
7650 putchar (' ');
7651 print_vma (section->sh_entsize, LONG_HEX);
7652 }
7653
7654 if (do_section_details)
7655 fputs (" ", stdout);
7656 else
7657 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7658
7659 printf ("%2u %3u ", section->sh_link, section->sh_info);
7660
7661 if ((unsigned long) section->sh_addralign == section->sh_addralign)
7662 printf ("%2lu\n", (unsigned long) section->sh_addralign);
7663 else
7664 {
7665 print_vma (section->sh_addralign, DEC);
7666 putchar ('\n');
7667 }
7668 }
7669 else if (do_section_details)
7670 {
7671 putchar (' ');
7672 print_vma (section->sh_addr, LONG_HEX);
7673 if ((long) section->sh_offset == section->sh_offset)
7674 printf (" %16.16lx", (unsigned long) section->sh_offset);
7675 else
7676 {
7677 printf (" ");
7678 print_vma (section->sh_offset, LONG_HEX);
7679 }
7680 printf (" %u\n ", section->sh_link);
7681 print_vma (section->sh_size, LONG_HEX);
7682 putchar (' ');
7683 print_vma (section->sh_entsize, LONG_HEX);
7684
7685 printf (" %-16u %lu\n",
7686 section->sh_info,
7687 (unsigned long) section->sh_addralign);
7688 }
7689 else
7690 {
7691 putchar (' ');
7692 print_vma (section->sh_addr, LONG_HEX);
7693 if ((long) section->sh_offset == section->sh_offset)
7694 printf (" %8.8lx", (unsigned long) section->sh_offset);
7695 else
7696 {
7697 printf (" ");
7698 print_vma (section->sh_offset, LONG_HEX);
7699 }
7700 printf ("\n ");
7701 print_vma (section->sh_size, LONG_HEX);
7702 printf (" ");
7703 print_vma (section->sh_entsize, LONG_HEX);
7704
7705 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7706
7707 printf (" %2u %3u %lu\n",
7708 section->sh_link,
7709 section->sh_info,
7710 (unsigned long) section->sh_addralign);
7711 }
7712
7713 if (do_section_details)
7714 {
7715 printf (" %s\n", get_elf_section_flags (filedata, section->sh_flags));
7716 if ((section->sh_flags & SHF_COMPRESSED) != 0)
7717 {
7718 /* Minimum section size is 12 bytes for 32-bit compression
7719 header + 12 bytes for compressed data header. */
7720 unsigned char buf[24];
7721
7722 assert (sizeof (buf) >= sizeof (Elf64_External_Chdr));
7723 if (get_data (&buf, filedata, section->sh_offset, 1,
7724 sizeof (buf), _("compression header")))
7725 {
7726 Elf_Internal_Chdr chdr;
7727
7728 if (get_compression_header (&chdr, buf, sizeof (buf)) == 0)
7729 printf (_(" [<corrupt>]\n"));
7730 else
7731 {
7732 if (chdr.ch_type == ELFCOMPRESS_ZLIB)
7733 printf (" ZLIB, ");
7734 else
7735 printf (_(" [<unknown>: 0x%x], "),
7736 chdr.ch_type);
7737 print_vma (chdr.ch_size, LONG_HEX);
7738 printf (", %lu\n", (unsigned long) chdr.ch_addralign);
7739 }
7740 }
7741 }
7742 }
7743 }
7744
7745 if (!do_section_details)
7746 {
7747 /* The ordering of the letters shown here matches the ordering of the
7748 corresponding SHF_xxx values, and hence the order in which these
7749 letters will be displayed to the user. */
7750 printf (_("Key to Flags:\n\
7751 W (write), A (alloc), X (execute), M (merge), S (strings), I (info),\n\
7752 L (link order), O (extra OS processing required), G (group), T (TLS),\n\
7753 C (compressed), x (unknown), o (OS specific), E (exclude),\n "));
7754 switch (filedata->file_header.e_ident[EI_OSABI])
7755 {
7756 case ELFOSABI_GNU:
7757 case ELFOSABI_FREEBSD:
7758 printf (_("R (retain), "));
7759 /* Fall through */
7760 case ELFOSABI_NONE:
7761 printf (_("D (mbind), "));
7762 break;
7763 default:
7764 break;
7765 }
7766 if (filedata->file_header.e_machine == EM_X86_64
7767 || filedata->file_header.e_machine == EM_L1OM
7768 || filedata->file_header.e_machine == EM_K1OM)
7769 printf (_("l (large), "));
7770 else if (filedata->file_header.e_machine == EM_ARM)
7771 printf (_("y (purecode), "));
7772 else if (filedata->file_header.e_machine == EM_PPC)
7773 printf (_("v (VLE), "));
7774 printf ("p (processor specific)\n");
7775 }
7776
7777 return true;
7778 }
7779
7780 static bool
7781 get_symtab (Filedata *filedata, Elf_Internal_Shdr *symsec,
7782 Elf_Internal_Sym **symtab, unsigned long *nsyms,
7783 char **strtab, unsigned long *strtablen)
7784 {
7785 *strtab = NULL;
7786 *strtablen = 0;
7787 *symtab = get_elf_symbols (filedata, symsec, nsyms);
7788
7789 if (*symtab == NULL)
7790 return false;
7791
7792 if (symsec->sh_link != 0)
7793 {
7794 Elf_Internal_Shdr *strsec;
7795
7796 if (symsec->sh_link >= filedata->file_header.e_shnum)
7797 {
7798 error (_("Bad sh_link in symbol table section\n"));
7799 free (*symtab);
7800 *symtab = NULL;
7801 *nsyms = 0;
7802 return false;
7803 }
7804
7805 strsec = filedata->section_headers + symsec->sh_link;
7806
7807 *strtab = (char *) get_data (NULL, filedata, strsec->sh_offset,
7808 1, strsec->sh_size, _("string table"));
7809 if (*strtab == NULL)
7810 {
7811 free (*symtab);
7812 *symtab = NULL;
7813 *nsyms = 0;
7814 return false;
7815 }
7816 *strtablen = strsec->sh_size;
7817 }
7818 return true;
7819 }
7820
7821 static const char *
7822 get_group_flags (unsigned int flags)
7823 {
7824 static char buff[128];
7825
7826 if (flags == 0)
7827 return "";
7828 else if (flags == GRP_COMDAT)
7829 return "COMDAT ";
7830
7831 snprintf (buff, sizeof buff, "[0x%x: %s%s%s]",
7832 flags,
7833 flags & GRP_MASKOS ? _("<OS specific>") : "",
7834 flags & GRP_MASKPROC ? _("<PROC specific>") : "",
7835 (flags & ~(GRP_COMDAT | GRP_MASKOS | GRP_MASKPROC)
7836 ? _("<unknown>") : ""));
7837
7838 return buff;
7839 }
7840
7841 static bool
7842 process_section_groups (Filedata * filedata)
7843 {
7844 Elf_Internal_Shdr * section;
7845 unsigned int i;
7846 struct group * group;
7847 Elf_Internal_Shdr * symtab_sec;
7848 Elf_Internal_Shdr * strtab_sec;
7849 Elf_Internal_Sym * symtab;
7850 unsigned long num_syms;
7851 char * strtab;
7852 size_t strtab_size;
7853
7854 /* Don't process section groups unless needed. */
7855 if (!do_unwind && !do_section_groups)
7856 return true;
7857
7858 if (filedata->file_header.e_shnum == 0)
7859 {
7860 if (do_section_groups)
7861 {
7862 if (filedata->is_separate)
7863 printf (_("\nThere are no sections group in linked file '%s'.\n"),
7864 filedata->file_name);
7865 else
7866 printf (_("\nThere are no section groups in this file.\n"));
7867 }
7868 return true;
7869 }
7870
7871 if (filedata->section_headers == NULL)
7872 {
7873 error (_("Section headers are not available!\n"));
7874 /* PR 13622: This can happen with a corrupt ELF header. */
7875 return false;
7876 }
7877
7878 filedata->section_headers_groups
7879 = (struct group **) calloc (filedata->file_header.e_shnum,
7880 sizeof (struct group *));
7881
7882 if (filedata->section_headers_groups == NULL)
7883 {
7884 error (_("Out of memory reading %u section group headers\n"),
7885 filedata->file_header.e_shnum);
7886 return false;
7887 }
7888
7889 /* Scan the sections for the group section. */
7890 filedata->group_count = 0;
7891 for (i = 0, section = filedata->section_headers;
7892 i < filedata->file_header.e_shnum;
7893 i++, section++)
7894 if (section->sh_type == SHT_GROUP)
7895 filedata->group_count++;
7896
7897 if (filedata->group_count == 0)
7898 {
7899 if (do_section_groups)
7900 {
7901 if (filedata->is_separate)
7902 printf (_("\nThere are no section groups in linked file '%s'.\n"),
7903 filedata->file_name);
7904 else
7905 printf (_("\nThere are no section groups in this file.\n"));
7906 }
7907
7908 return true;
7909 }
7910
7911 filedata->section_groups = (struct group *) calloc (filedata->group_count,
7912 sizeof (struct group));
7913
7914 if (filedata->section_groups == NULL)
7915 {
7916 error (_("Out of memory reading %lu groups\n"),
7917 (unsigned long) filedata->group_count);
7918 return false;
7919 }
7920
7921 symtab_sec = NULL;
7922 strtab_sec = NULL;
7923 symtab = NULL;
7924 num_syms = 0;
7925 strtab = NULL;
7926 strtab_size = 0;
7927
7928 if (filedata->is_separate)
7929 printf (_("Section groups in linked file '%s'\n"), filedata->file_name);
7930
7931 for (i = 0, section = filedata->section_headers, group = filedata->section_groups;
7932 i < filedata->file_header.e_shnum;
7933 i++, section++)
7934 {
7935 if (section->sh_type == SHT_GROUP)
7936 {
7937 const char * name = printable_section_name (filedata, section);
7938 const char * group_name;
7939 unsigned char * start;
7940 unsigned char * indices;
7941 unsigned int entry, j, size;
7942 Elf_Internal_Shdr * sec;
7943 Elf_Internal_Sym * sym;
7944
7945 /* Get the symbol table. */
7946 if (section->sh_link >= filedata->file_header.e_shnum
7947 || ((sec = filedata->section_headers + section->sh_link)->sh_type
7948 != SHT_SYMTAB))
7949 {
7950 error (_("Bad sh_link in group section `%s'\n"), name);
7951 continue;
7952 }
7953
7954 if (symtab_sec != sec)
7955 {
7956 symtab_sec = sec;
7957 free (symtab);
7958 symtab = get_elf_symbols (filedata, symtab_sec, & num_syms);
7959 }
7960
7961 if (symtab == NULL)
7962 {
7963 error (_("Corrupt header in group section `%s'\n"), name);
7964 continue;
7965 }
7966
7967 if (section->sh_info >= num_syms)
7968 {
7969 error (_("Bad sh_info in group section `%s'\n"), name);
7970 continue;
7971 }
7972
7973 sym = symtab + section->sh_info;
7974
7975 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
7976 {
7977 if (sym->st_shndx == 0
7978 || sym->st_shndx >= filedata->file_header.e_shnum)
7979 {
7980 error (_("Bad sh_info in group section `%s'\n"), name);
7981 continue;
7982 }
7983
7984 group_name = section_name_print (filedata,
7985 filedata->section_headers
7986 + sym->st_shndx);
7987 strtab_sec = NULL;
7988 free (strtab);
7989 strtab = NULL;
7990 strtab_size = 0;
7991 }
7992 else
7993 {
7994 /* Get the string table. */
7995 if (symtab_sec->sh_link >= filedata->file_header.e_shnum)
7996 {
7997 strtab_sec = NULL;
7998 free (strtab);
7999 strtab = NULL;
8000 strtab_size = 0;
8001 }
8002 else if (strtab_sec
8003 != (sec = filedata->section_headers + symtab_sec->sh_link))
8004 {
8005 strtab_sec = sec;
8006 free (strtab);
8007
8008 strtab = (char *) get_data (NULL, filedata, strtab_sec->sh_offset,
8009 1, strtab_sec->sh_size,
8010 _("string table"));
8011 strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
8012 }
8013 group_name = sym->st_name < strtab_size
8014 ? strtab + sym->st_name : _("<corrupt>");
8015 }
8016
8017 /* PR 17531: file: loop. */
8018 if (section->sh_entsize > section->sh_size)
8019 {
8020 error (_("Section %s has sh_entsize (0x%lx) which is larger than its size (0x%lx)\n"),
8021 printable_section_name (filedata, section),
8022 (unsigned long) section->sh_entsize,
8023 (unsigned long) section->sh_size);
8024 continue;
8025 }
8026
8027 start = (unsigned char *) get_data (NULL, filedata, section->sh_offset,
8028 1, section->sh_size,
8029 _("section data"));
8030 if (start == NULL)
8031 continue;
8032
8033 indices = start;
8034 size = (section->sh_size / section->sh_entsize) - 1;
8035 entry = byte_get (indices, 4);
8036 indices += 4;
8037
8038 if (do_section_groups)
8039 {
8040 printf (_("\n%sgroup section [%5u] `%s' [%s] contains %u sections:\n"),
8041 get_group_flags (entry), i, name, group_name, size);
8042
8043 printf (_(" [Index] Name\n"));
8044 }
8045
8046 group->group_index = i;
8047
8048 for (j = 0; j < size; j++)
8049 {
8050 struct group_list * g;
8051
8052 entry = byte_get (indices, 4);
8053 indices += 4;
8054
8055 if (entry >= filedata->file_header.e_shnum)
8056 {
8057 static unsigned num_group_errors = 0;
8058
8059 if (num_group_errors ++ < 10)
8060 {
8061 error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
8062 entry, i, filedata->file_header.e_shnum - 1);
8063 if (num_group_errors == 10)
8064 warn (_("Further error messages about overlarge group section indices suppressed\n"));
8065 }
8066 continue;
8067 }
8068
8069 if (filedata->section_headers_groups [entry] != NULL)
8070 {
8071 if (entry)
8072 {
8073 static unsigned num_errs = 0;
8074
8075 if (num_errs ++ < 10)
8076 {
8077 error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
8078 entry, i,
8079 filedata->section_headers_groups [entry]->group_index);
8080 if (num_errs == 10)
8081 warn (_("Further error messages about already contained group sections suppressed\n"));
8082 }
8083 continue;
8084 }
8085 else
8086 {
8087 /* Intel C/C++ compiler may put section 0 in a
8088 section group. We just warn it the first time
8089 and ignore it afterwards. */
8090 static bool warned = false;
8091 if (!warned)
8092 {
8093 error (_("section 0 in group section [%5u]\n"),
8094 filedata->section_headers_groups [entry]->group_index);
8095 warned = true;
8096 }
8097 }
8098 }
8099
8100 filedata->section_headers_groups [entry] = group;
8101
8102 if (do_section_groups)
8103 {
8104 sec = filedata->section_headers + entry;
8105 printf (" [%5u] %s\n", entry, printable_section_name (filedata, sec));
8106 }
8107
8108 g = (struct group_list *) xmalloc (sizeof (struct group_list));
8109 g->section_index = entry;
8110 g->next = group->root;
8111 group->root = g;
8112 }
8113
8114 free (start);
8115
8116 group++;
8117 }
8118 }
8119
8120 free (symtab);
8121 free (strtab);
8122 return true;
8123 }
8124
8125 /* Data used to display dynamic fixups. */
8126
8127 struct ia64_vms_dynfixup
8128 {
8129 bfd_vma needed_ident; /* Library ident number. */
8130 bfd_vma needed; /* Index in the dstrtab of the library name. */
8131 bfd_vma fixup_needed; /* Index of the library. */
8132 bfd_vma fixup_rela_cnt; /* Number of fixups. */
8133 bfd_vma fixup_rela_off; /* Fixups offset in the dynamic segment. */
8134 };
8135
8136 /* Data used to display dynamic relocations. */
8137
8138 struct ia64_vms_dynimgrela
8139 {
8140 bfd_vma img_rela_cnt; /* Number of relocations. */
8141 bfd_vma img_rela_off; /* Reloc offset in the dynamic segment. */
8142 };
8143
8144 /* Display IA-64 OpenVMS dynamic fixups (used to dynamically link a shared
8145 library). */
8146
8147 static bool
8148 dump_ia64_vms_dynamic_fixups (Filedata * filedata,
8149 struct ia64_vms_dynfixup * fixup,
8150 const char * strtab,
8151 unsigned int strtab_sz)
8152 {
8153 Elf64_External_VMS_IMAGE_FIXUP * imfs;
8154 long i;
8155 const char * lib_name;
8156
8157 imfs = get_data (NULL, filedata,
8158 filedata->dynamic_addr + fixup->fixup_rela_off,
8159 sizeof (*imfs), fixup->fixup_rela_cnt,
8160 _("dynamic section image fixups"));
8161 if (!imfs)
8162 return false;
8163
8164 if (fixup->needed < strtab_sz)
8165 lib_name = strtab + fixup->needed;
8166 else
8167 {
8168 warn (_("corrupt library name index of 0x%lx found in dynamic entry"),
8169 (unsigned long) fixup->needed);
8170 lib_name = "???";
8171 }
8172
8173 printf (_("\nImage fixups for needed library #%d: %s - ident: %lx\n"),
8174 (int) fixup->fixup_needed, lib_name, (long) fixup->needed_ident);
8175 printf
8176 (_("Seg Offset Type SymVec DataType\n"));
8177
8178 for (i = 0; i < (long) fixup->fixup_rela_cnt; i++)
8179 {
8180 unsigned int type;
8181 const char *rtype;
8182
8183 printf ("%3u ", (unsigned) BYTE_GET (imfs [i].fixup_seg));
8184 printf_vma ((bfd_vma) BYTE_GET (imfs [i].fixup_offset));
8185 type = BYTE_GET (imfs [i].type);
8186 rtype = elf_ia64_reloc_type (type);
8187 if (rtype == NULL)
8188 printf (" 0x%08x ", type);
8189 else
8190 printf (" %-32s ", rtype);
8191 printf ("%6u ", (unsigned) BYTE_GET (imfs [i].symvec_index));
8192 printf ("0x%08x\n", (unsigned) BYTE_GET (imfs [i].data_type));
8193 }
8194
8195 free (imfs);
8196 return true;
8197 }
8198
8199 /* Display IA-64 OpenVMS dynamic relocations (used to relocate an image). */
8200
8201 static bool
8202 dump_ia64_vms_dynamic_relocs (Filedata * filedata, struct ia64_vms_dynimgrela *imgrela)
8203 {
8204 Elf64_External_VMS_IMAGE_RELA *imrs;
8205 long i;
8206
8207 imrs = get_data (NULL, filedata,
8208 filedata->dynamic_addr + imgrela->img_rela_off,
8209 sizeof (*imrs), imgrela->img_rela_cnt,
8210 _("dynamic section image relocations"));
8211 if (!imrs)
8212 return false;
8213
8214 printf (_("\nImage relocs\n"));
8215 printf
8216 (_("Seg Offset Type Addend Seg Sym Off\n"));
8217
8218 for (i = 0; i < (long) imgrela->img_rela_cnt; i++)
8219 {
8220 unsigned int type;
8221 const char *rtype;
8222
8223 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].rela_seg));
8224 printf ("%08" BFD_VMA_FMT "x ",
8225 (bfd_vma) BYTE_GET (imrs [i].rela_offset));
8226 type = BYTE_GET (imrs [i].type);
8227 rtype = elf_ia64_reloc_type (type);
8228 if (rtype == NULL)
8229 printf ("0x%08x ", type);
8230 else
8231 printf ("%-31s ", rtype);
8232 print_vma (BYTE_GET (imrs [i].addend), FULL_HEX);
8233 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].sym_seg));
8234 printf ("%08" BFD_VMA_FMT "x\n",
8235 (bfd_vma) BYTE_GET (imrs [i].sym_offset));
8236 }
8237
8238 free (imrs);
8239 return true;
8240 }
8241
8242 /* Display IA-64 OpenVMS dynamic relocations and fixups. */
8243
8244 static bool
8245 process_ia64_vms_dynamic_relocs (Filedata * filedata)
8246 {
8247 struct ia64_vms_dynfixup fixup;
8248 struct ia64_vms_dynimgrela imgrela;
8249 Elf_Internal_Dyn *entry;
8250 bfd_vma strtab_off = 0;
8251 bfd_vma strtab_sz = 0;
8252 char *strtab = NULL;
8253 bool res = true;
8254
8255 memset (&fixup, 0, sizeof (fixup));
8256 memset (&imgrela, 0, sizeof (imgrela));
8257
8258 /* Note: the order of the entries is specified by the OpenVMS specs. */
8259 for (entry = filedata->dynamic_section;
8260 entry < filedata->dynamic_section + filedata->dynamic_nent;
8261 entry++)
8262 {
8263 switch (entry->d_tag)
8264 {
8265 case DT_IA_64_VMS_STRTAB_OFFSET:
8266 strtab_off = entry->d_un.d_val;
8267 break;
8268 case DT_STRSZ:
8269 strtab_sz = entry->d_un.d_val;
8270 if (strtab == NULL)
8271 strtab = get_data (NULL, filedata,
8272 filedata->dynamic_addr + strtab_off,
8273 1, strtab_sz, _("dynamic string section"));
8274 if (strtab == NULL)
8275 strtab_sz = 0;
8276 break;
8277
8278 case DT_IA_64_VMS_NEEDED_IDENT:
8279 fixup.needed_ident = entry->d_un.d_val;
8280 break;
8281 case DT_NEEDED:
8282 fixup.needed = entry->d_un.d_val;
8283 break;
8284 case DT_IA_64_VMS_FIXUP_NEEDED:
8285 fixup.fixup_needed = entry->d_un.d_val;
8286 break;
8287 case DT_IA_64_VMS_FIXUP_RELA_CNT:
8288 fixup.fixup_rela_cnt = entry->d_un.d_val;
8289 break;
8290 case DT_IA_64_VMS_FIXUP_RELA_OFF:
8291 fixup.fixup_rela_off = entry->d_un.d_val;
8292 if (! dump_ia64_vms_dynamic_fixups (filedata, &fixup, strtab, strtab_sz))
8293 res = false;
8294 break;
8295 case DT_IA_64_VMS_IMG_RELA_CNT:
8296 imgrela.img_rela_cnt = entry->d_un.d_val;
8297 break;
8298 case DT_IA_64_VMS_IMG_RELA_OFF:
8299 imgrela.img_rela_off = entry->d_un.d_val;
8300 if (! dump_ia64_vms_dynamic_relocs (filedata, &imgrela))
8301 res = false;
8302 break;
8303
8304 default:
8305 break;
8306 }
8307 }
8308
8309 free (strtab);
8310
8311 return res;
8312 }
8313
8314 static struct
8315 {
8316 const char * name;
8317 int reloc;
8318 int size;
8319 relocation_type rel_type;
8320 }
8321 dynamic_relocations [] =
8322 {
8323 { "REL", DT_REL, DT_RELSZ, reltype_rel },
8324 { "RELA", DT_RELA, DT_RELASZ, reltype_rela },
8325 { "RELR", DT_RELR, DT_RELRSZ, reltype_relr },
8326 { "PLT", DT_JMPREL, DT_PLTRELSZ, reltype_unknown }
8327 };
8328
8329 /* Process the reloc section. */
8330
8331 static bool
8332 process_relocs (Filedata * filedata)
8333 {
8334 unsigned long rel_size;
8335 unsigned long rel_offset;
8336
8337 if (!do_reloc)
8338 return true;
8339
8340 if (do_using_dynamic)
8341 {
8342 relocation_type rel_type;
8343 const char * name;
8344 bool has_dynamic_reloc;
8345 unsigned int i;
8346
8347 has_dynamic_reloc = false;
8348
8349 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
8350 {
8351 rel_type = dynamic_relocations [i].rel_type;
8352 name = dynamic_relocations [i].name;
8353 rel_size = filedata->dynamic_info[dynamic_relocations [i].size];
8354 rel_offset = filedata->dynamic_info[dynamic_relocations [i].reloc];
8355
8356 if (rel_size)
8357 has_dynamic_reloc = true;
8358
8359 if (rel_type == reltype_unknown)
8360 {
8361 if (dynamic_relocations [i].reloc == DT_JMPREL)
8362 switch (filedata->dynamic_info[DT_PLTREL])
8363 {
8364 case DT_REL:
8365 rel_type = reltype_rel;
8366 break;
8367 case DT_RELA:
8368 rel_type = reltype_rela;
8369 break;
8370 }
8371 }
8372
8373 if (rel_size)
8374 {
8375 if (filedata->is_separate)
8376 printf
8377 (_("\nIn linked file '%s' section '%s' at offset 0x%lx contains %ld bytes:\n"),
8378 filedata->file_name, name, rel_offset, rel_size);
8379 else
8380 printf
8381 (_("\n'%s' relocation section at offset 0x%lx contains %ld bytes:\n"),
8382 name, rel_offset, rel_size);
8383
8384 dump_relocations (filedata,
8385 offset_from_vma (filedata, rel_offset, rel_size),
8386 rel_size,
8387 filedata->dynamic_symbols,
8388 filedata->num_dynamic_syms,
8389 filedata->dynamic_strings,
8390 filedata->dynamic_strings_length,
8391 rel_type, true /* is_dynamic */);
8392 }
8393 }
8394
8395 if (is_ia64_vms (filedata))
8396 if (process_ia64_vms_dynamic_relocs (filedata))
8397 has_dynamic_reloc = true;
8398
8399 if (! has_dynamic_reloc)
8400 {
8401 if (filedata->is_separate)
8402 printf (_("\nThere are no dynamic relocations in linked file '%s'.\n"),
8403 filedata->file_name);
8404 else
8405 printf (_("\nThere are no dynamic relocations in this file.\n"));
8406 }
8407 }
8408 else
8409 {
8410 Elf_Internal_Shdr * section;
8411 unsigned long i;
8412 bool found = false;
8413
8414 for (i = 0, section = filedata->section_headers;
8415 i < filedata->file_header.e_shnum;
8416 i++, section++)
8417 {
8418 if ( section->sh_type != SHT_RELA
8419 && section->sh_type != SHT_REL
8420 && section->sh_type != SHT_RELR)
8421 continue;
8422
8423 rel_offset = section->sh_offset;
8424 rel_size = section->sh_size;
8425
8426 if (rel_size)
8427 {
8428 relocation_type rel_type;
8429 unsigned long num_rela;
8430
8431 if (filedata->is_separate)
8432 printf (_("\nIn linked file '%s' relocation section "),
8433 filedata->file_name);
8434 else
8435 printf (_("\nRelocation section "));
8436
8437 if (filedata->string_table == NULL)
8438 printf ("%d", section->sh_name);
8439 else
8440 printf ("'%s'", printable_section_name (filedata, section));
8441
8442 num_rela = rel_size / section->sh_entsize;
8443 printf (ngettext (" at offset 0x%lx contains %lu entry:\n",
8444 " at offset 0x%lx contains %lu entries:\n",
8445 num_rela),
8446 rel_offset, num_rela);
8447
8448 rel_type = section->sh_type == SHT_RELA ? reltype_rela :
8449 section->sh_type == SHT_REL ? reltype_rel : reltype_relr;
8450
8451 if (section->sh_link != 0
8452 && section->sh_link < filedata->file_header.e_shnum)
8453 {
8454 Elf_Internal_Shdr * symsec;
8455 Elf_Internal_Sym * symtab;
8456 unsigned long nsyms;
8457 unsigned long strtablen = 0;
8458 char * strtab = NULL;
8459
8460 symsec = filedata->section_headers + section->sh_link;
8461 if (symsec->sh_type != SHT_SYMTAB
8462 && symsec->sh_type != SHT_DYNSYM)
8463 continue;
8464
8465 if (!get_symtab (filedata, symsec,
8466 &symtab, &nsyms, &strtab, &strtablen))
8467 continue;
8468
8469 dump_relocations (filedata, rel_offset, rel_size,
8470 symtab, nsyms, strtab, strtablen,
8471 rel_type,
8472 symsec->sh_type == SHT_DYNSYM);
8473 free (strtab);
8474 free (symtab);
8475 }
8476 else
8477 dump_relocations (filedata, rel_offset, rel_size,
8478 NULL, 0, NULL, 0, rel_type, false /* is_dynamic */);
8479
8480 found = true;
8481 }
8482 }
8483
8484 if (! found)
8485 {
8486 /* Users sometimes forget the -D option, so try to be helpful. */
8487 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
8488 {
8489 if (filedata->dynamic_info[dynamic_relocations [i].size])
8490 {
8491 if (filedata->is_separate)
8492 printf (_("\nThere are no static relocations in linked file '%s'."),
8493 filedata->file_name);
8494 else
8495 printf (_("\nThere are no static relocations in this file."));
8496 printf (_("\nTo see the dynamic relocations add --use-dynamic to the command line.\n"));
8497
8498 break;
8499 }
8500 }
8501 if (i == ARRAY_SIZE (dynamic_relocations))
8502 {
8503 if (filedata->is_separate)
8504 printf (_("\nThere are no relocations in linked file '%s'.\n"),
8505 filedata->file_name);
8506 else
8507 printf (_("\nThere are no relocations in this file.\n"));
8508 }
8509 }
8510 }
8511
8512 return true;
8513 }
8514
8515 /* An absolute address consists of a section and an offset. If the
8516 section is NULL, the offset itself is the address, otherwise, the
8517 address equals to LOAD_ADDRESS(section) + offset. */
8518
8519 struct absaddr
8520 {
8521 unsigned short section;
8522 bfd_vma offset;
8523 };
8524
8525 /* Find the nearest symbol at or below ADDR. Returns the symbol
8526 name, if found, and the offset from the symbol to ADDR. */
8527
8528 static void
8529 find_symbol_for_address (Filedata * filedata,
8530 Elf_Internal_Sym * symtab,
8531 unsigned long nsyms,
8532 const char * strtab,
8533 unsigned long strtab_size,
8534 struct absaddr addr,
8535 const char ** symname,
8536 bfd_vma * offset)
8537 {
8538 bfd_vma dist = 0x100000;
8539 Elf_Internal_Sym * sym;
8540 Elf_Internal_Sym * beg;
8541 Elf_Internal_Sym * end;
8542 Elf_Internal_Sym * best = NULL;
8543
8544 REMOVE_ARCH_BITS (addr.offset);
8545 beg = symtab;
8546 end = symtab + nsyms;
8547
8548 while (beg < end)
8549 {
8550 bfd_vma value;
8551
8552 sym = beg + (end - beg) / 2;
8553
8554 value = sym->st_value;
8555 REMOVE_ARCH_BITS (value);
8556
8557 if (sym->st_name != 0
8558 && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
8559 && addr.offset >= value
8560 && addr.offset - value < dist)
8561 {
8562 best = sym;
8563 dist = addr.offset - value;
8564 if (!dist)
8565 break;
8566 }
8567
8568 if (addr.offset < value)
8569 end = sym;
8570 else
8571 beg = sym + 1;
8572 }
8573
8574 if (best)
8575 {
8576 *symname = (best->st_name >= strtab_size
8577 ? _("<corrupt>") : strtab + best->st_name);
8578 *offset = dist;
8579 return;
8580 }
8581
8582 *symname = NULL;
8583 *offset = addr.offset;
8584 }
8585
8586 static /* signed */ int
8587 symcmp (const void *p, const void *q)
8588 {
8589 Elf_Internal_Sym *sp = (Elf_Internal_Sym *) p;
8590 Elf_Internal_Sym *sq = (Elf_Internal_Sym *) q;
8591
8592 return sp->st_value > sq->st_value ? 1 : (sp->st_value < sq->st_value ? -1 : 0);
8593 }
8594
8595 /* Process the unwind section. */
8596
8597 #include "unwind-ia64.h"
8598
8599 struct ia64_unw_table_entry
8600 {
8601 struct absaddr start;
8602 struct absaddr end;
8603 struct absaddr info;
8604 };
8605
8606 struct ia64_unw_aux_info
8607 {
8608 struct ia64_unw_table_entry * table; /* Unwind table. */
8609 unsigned long table_len; /* Length of unwind table. */
8610 unsigned char * info; /* Unwind info. */
8611 unsigned long info_size; /* Size of unwind info. */
8612 bfd_vma info_addr; /* Starting address of unwind info. */
8613 bfd_vma seg_base; /* Starting address of segment. */
8614 Elf_Internal_Sym * symtab; /* The symbol table. */
8615 unsigned long nsyms; /* Number of symbols. */
8616 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
8617 unsigned long nfuns; /* Number of entries in funtab. */
8618 char * strtab; /* The string table. */
8619 unsigned long strtab_size; /* Size of string table. */
8620 };
8621
8622 static bool
8623 dump_ia64_unwind (Filedata * filedata, struct ia64_unw_aux_info * aux)
8624 {
8625 struct ia64_unw_table_entry * tp;
8626 unsigned long j, nfuns;
8627 int in_body;
8628 bool res = true;
8629
8630 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
8631 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
8632 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
8633 aux->funtab[nfuns++] = aux->symtab[j];
8634 aux->nfuns = nfuns;
8635 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
8636
8637 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
8638 {
8639 bfd_vma stamp;
8640 bfd_vma offset;
8641 const unsigned char * dp;
8642 const unsigned char * head;
8643 const unsigned char * end;
8644 const char * procname;
8645
8646 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
8647 aux->strtab_size, tp->start, &procname, &offset);
8648
8649 fputs ("\n<", stdout);
8650
8651 if (procname)
8652 {
8653 fputs (procname, stdout);
8654
8655 if (offset)
8656 printf ("+%lx", (unsigned long) offset);
8657 }
8658
8659 fputs (">: [", stdout);
8660 print_vma (tp->start.offset, PREFIX_HEX);
8661 fputc ('-', stdout);
8662 print_vma (tp->end.offset, PREFIX_HEX);
8663 printf ("], info at +0x%lx\n",
8664 (unsigned long) (tp->info.offset - aux->seg_base));
8665
8666 /* PR 17531: file: 86232b32. */
8667 if (aux->info == NULL)
8668 continue;
8669
8670 offset = tp->info.offset;
8671 if (tp->info.section)
8672 {
8673 if (tp->info.section >= filedata->file_header.e_shnum)
8674 {
8675 warn (_("Invalid section %u in table entry %ld\n"),
8676 tp->info.section, (long) (tp - aux->table));
8677 res = false;
8678 continue;
8679 }
8680 offset += filedata->section_headers[tp->info.section].sh_addr;
8681 }
8682 offset -= aux->info_addr;
8683 /* PR 17531: file: 0997b4d1. */
8684 if (offset >= aux->info_size
8685 || aux->info_size - offset < 8)
8686 {
8687 warn (_("Invalid offset %lx in table entry %ld\n"),
8688 (long) tp->info.offset, (long) (tp - aux->table));
8689 res = false;
8690 continue;
8691 }
8692
8693 head = aux->info + offset;
8694 stamp = byte_get ((unsigned char *) head, sizeof (stamp));
8695
8696 printf (" v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
8697 (unsigned) UNW_VER (stamp),
8698 (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
8699 UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
8700 UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
8701 (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
8702
8703 if (UNW_VER (stamp) != 1)
8704 {
8705 printf (_("\tUnknown version.\n"));
8706 continue;
8707 }
8708
8709 in_body = 0;
8710 end = head + 8 + eh_addr_size * UNW_LENGTH (stamp);
8711 /* PR 17531: file: 16ceda89. */
8712 if (end > aux->info + aux->info_size)
8713 end = aux->info + aux->info_size;
8714 for (dp = head + 8; dp < end;)
8715 dp = unw_decode (dp, in_body, & in_body, end);
8716 }
8717
8718 free (aux->funtab);
8719
8720 return res;
8721 }
8722
8723 static bool
8724 slurp_ia64_unwind_table (Filedata * filedata,
8725 struct ia64_unw_aux_info * aux,
8726 Elf_Internal_Shdr * sec)
8727 {
8728 unsigned long size, nrelas, i;
8729 Elf_Internal_Phdr * seg;
8730 struct ia64_unw_table_entry * tep;
8731 Elf_Internal_Shdr * relsec;
8732 Elf_Internal_Rela * rela;
8733 Elf_Internal_Rela * rp;
8734 unsigned char * table;
8735 unsigned char * tp;
8736 Elf_Internal_Sym * sym;
8737 const char * relname;
8738
8739 aux->table_len = 0;
8740
8741 /* First, find the starting address of the segment that includes
8742 this section: */
8743
8744 if (filedata->file_header.e_phnum)
8745 {
8746 if (! get_program_headers (filedata))
8747 return false;
8748
8749 for (seg = filedata->program_headers;
8750 seg < filedata->program_headers + filedata->file_header.e_phnum;
8751 ++seg)
8752 {
8753 if (seg->p_type != PT_LOAD)
8754 continue;
8755
8756 if (sec->sh_addr >= seg->p_vaddr
8757 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
8758 {
8759 aux->seg_base = seg->p_vaddr;
8760 break;
8761 }
8762 }
8763 }
8764
8765 /* Second, build the unwind table from the contents of the unwind section: */
8766 size = sec->sh_size;
8767 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
8768 _("unwind table"));
8769 if (!table)
8770 return false;
8771
8772 aux->table_len = size / (3 * eh_addr_size);
8773 aux->table = (struct ia64_unw_table_entry *)
8774 xcmalloc (aux->table_len, sizeof (aux->table[0]));
8775 tep = aux->table;
8776
8777 for (tp = table; tp <= table + size - (3 * eh_addr_size); ++tep)
8778 {
8779 tep->start.section = SHN_UNDEF;
8780 tep->end.section = SHN_UNDEF;
8781 tep->info.section = SHN_UNDEF;
8782 tep->start.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8783 tep->end.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8784 tep->info.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8785 tep->start.offset += aux->seg_base;
8786 tep->end.offset += aux->seg_base;
8787 tep->info.offset += aux->seg_base;
8788 }
8789 free (table);
8790
8791 /* Third, apply any relocations to the unwind table: */
8792 for (relsec = filedata->section_headers;
8793 relsec < filedata->section_headers + filedata->file_header.e_shnum;
8794 ++relsec)
8795 {
8796 if (relsec->sh_type != SHT_RELA
8797 || relsec->sh_info >= filedata->file_header.e_shnum
8798 || filedata->section_headers + relsec->sh_info != sec)
8799 continue;
8800
8801 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
8802 & rela, & nrelas))
8803 {
8804 free (aux->table);
8805 aux->table = NULL;
8806 aux->table_len = 0;
8807 return false;
8808 }
8809
8810 for (rp = rela; rp < rela + nrelas; ++rp)
8811 {
8812 unsigned int sym_ndx;
8813 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
8814 relname = elf_ia64_reloc_type (r_type);
8815
8816 /* PR 17531: file: 9fa67536. */
8817 if (relname == NULL)
8818 {
8819 warn (_("Skipping unknown relocation type: %u\n"), r_type);
8820 continue;
8821 }
8822
8823 if (! startswith (relname, "R_IA64_SEGREL"))
8824 {
8825 warn (_("Skipping unexpected relocation type: %s\n"), relname);
8826 continue;
8827 }
8828
8829 i = rp->r_offset / (3 * eh_addr_size);
8830
8831 /* PR 17531: file: 5bc8d9bf. */
8832 if (i >= aux->table_len)
8833 {
8834 warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
8835 continue;
8836 }
8837
8838 sym_ndx = get_reloc_symindex (rp->r_info);
8839 if (sym_ndx >= aux->nsyms)
8840 {
8841 warn (_("Skipping reloc with invalid symbol index: %u\n"),
8842 sym_ndx);
8843 continue;
8844 }
8845 sym = aux->symtab + sym_ndx;
8846
8847 switch (rp->r_offset / eh_addr_size % 3)
8848 {
8849 case 0:
8850 aux->table[i].start.section = sym->st_shndx;
8851 aux->table[i].start.offset = rp->r_addend + sym->st_value;
8852 break;
8853 case 1:
8854 aux->table[i].end.section = sym->st_shndx;
8855 aux->table[i].end.offset = rp->r_addend + sym->st_value;
8856 break;
8857 case 2:
8858 aux->table[i].info.section = sym->st_shndx;
8859 aux->table[i].info.offset = rp->r_addend + sym->st_value;
8860 break;
8861 default:
8862 break;
8863 }
8864 }
8865
8866 free (rela);
8867 }
8868
8869 return true;
8870 }
8871
8872 static bool
8873 ia64_process_unwind (Filedata * filedata)
8874 {
8875 Elf_Internal_Shdr * sec;
8876 Elf_Internal_Shdr * unwsec = NULL;
8877 unsigned long i, unwcount = 0, unwstart = 0;
8878 struct ia64_unw_aux_info aux;
8879 bool res = true;
8880
8881 memset (& aux, 0, sizeof (aux));
8882
8883 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
8884 {
8885 if (sec->sh_type == SHT_SYMTAB)
8886 {
8887 if (aux.symtab)
8888 {
8889 error (_("Multiple symbol tables encountered\n"));
8890 free (aux.symtab);
8891 aux.symtab = NULL;
8892 free (aux.strtab);
8893 aux.strtab = NULL;
8894 }
8895 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
8896 &aux.strtab, &aux.strtab_size))
8897 return false;
8898 }
8899 else if (sec->sh_type == SHT_IA_64_UNWIND)
8900 unwcount++;
8901 }
8902
8903 if (!unwcount)
8904 printf (_("\nThere are no unwind sections in this file.\n"));
8905
8906 while (unwcount-- > 0)
8907 {
8908 const char *suffix;
8909 size_t len, len2;
8910
8911 for (i = unwstart, sec = filedata->section_headers + unwstart, unwsec = NULL;
8912 i < filedata->file_header.e_shnum; ++i, ++sec)
8913 if (sec->sh_type == SHT_IA_64_UNWIND)
8914 {
8915 unwsec = sec;
8916 break;
8917 }
8918 /* We have already counted the number of SHT_IA64_UNWIND
8919 sections so the loop above should never fail. */
8920 assert (unwsec != NULL);
8921
8922 unwstart = i + 1;
8923 len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
8924
8925 if ((unwsec->sh_flags & SHF_GROUP) != 0)
8926 {
8927 /* We need to find which section group it is in. */
8928 struct group_list * g;
8929
8930 if (filedata->section_headers_groups == NULL
8931 || filedata->section_headers_groups[i] == NULL)
8932 i = filedata->file_header.e_shnum;
8933 else
8934 {
8935 g = filedata->section_headers_groups[i]->root;
8936
8937 for (; g != NULL; g = g->next)
8938 {
8939 sec = filedata->section_headers + g->section_index;
8940
8941 if (section_name_valid (filedata, sec)
8942 && streq (section_name (filedata, sec),
8943 ELF_STRING_ia64_unwind_info))
8944 break;
8945 }
8946
8947 if (g == NULL)
8948 i = filedata->file_header.e_shnum;
8949 }
8950 }
8951 else if (section_name_valid (filedata, unwsec)
8952 && startswith (section_name (filedata, unwsec),
8953 ELF_STRING_ia64_unwind_once))
8954 {
8955 /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO. */
8956 len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
8957 suffix = section_name (filedata, unwsec) + len;
8958 for (i = 0, sec = filedata->section_headers;
8959 i < filedata->file_header.e_shnum;
8960 ++i, ++sec)
8961 if (section_name_valid (filedata, sec)
8962 && startswith (section_name (filedata, sec),
8963 ELF_STRING_ia64_unwind_info_once)
8964 && streq (section_name (filedata, sec) + len2, suffix))
8965 break;
8966 }
8967 else
8968 {
8969 /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
8970 .IA_64.unwind or BAR -> .IA_64.unwind_info. */
8971 len = sizeof (ELF_STRING_ia64_unwind) - 1;
8972 len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
8973 suffix = "";
8974 if (section_name_valid (filedata, unwsec)
8975 && startswith (section_name (filedata, unwsec),
8976 ELF_STRING_ia64_unwind))
8977 suffix = section_name (filedata, unwsec) + len;
8978 for (i = 0, sec = filedata->section_headers;
8979 i < filedata->file_header.e_shnum;
8980 ++i, ++sec)
8981 if (section_name_valid (filedata, sec)
8982 && startswith (section_name (filedata, sec),
8983 ELF_STRING_ia64_unwind_info)
8984 && streq (section_name (filedata, sec) + len2, suffix))
8985 break;
8986 }
8987
8988 if (i == filedata->file_header.e_shnum)
8989 {
8990 printf (_("\nCould not find unwind info section for "));
8991
8992 if (filedata->string_table == NULL)
8993 printf ("%d", unwsec->sh_name);
8994 else
8995 printf ("'%s'", printable_section_name (filedata, unwsec));
8996 }
8997 else
8998 {
8999 aux.info_addr = sec->sh_addr;
9000 aux.info = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1,
9001 sec->sh_size,
9002 _("unwind info"));
9003 aux.info_size = aux.info == NULL ? 0 : sec->sh_size;
9004
9005 printf (_("\nUnwind section "));
9006
9007 if (filedata->string_table == NULL)
9008 printf ("%d", unwsec->sh_name);
9009 else
9010 printf ("'%s'", printable_section_name (filedata, unwsec));
9011
9012 printf (_(" at offset 0x%lx contains %lu entries:\n"),
9013 (unsigned long) unwsec->sh_offset,
9014 (unsigned long) (unwsec->sh_size / (3 * eh_addr_size)));
9015
9016 if (slurp_ia64_unwind_table (filedata, & aux, unwsec)
9017 && aux.table_len > 0)
9018 dump_ia64_unwind (filedata, & aux);
9019
9020 free ((char *) aux.table);
9021 free ((char *) aux.info);
9022 aux.table = NULL;
9023 aux.info = NULL;
9024 }
9025 }
9026
9027 free (aux.symtab);
9028 free ((char *) aux.strtab);
9029
9030 return res;
9031 }
9032
9033 struct hppa_unw_table_entry
9034 {
9035 struct absaddr start;
9036 struct absaddr end;
9037 unsigned int Cannot_unwind:1; /* 0 */
9038 unsigned int Millicode:1; /* 1 */
9039 unsigned int Millicode_save_sr0:1; /* 2 */
9040 unsigned int Region_description:2; /* 3..4 */
9041 unsigned int reserved1:1; /* 5 */
9042 unsigned int Entry_SR:1; /* 6 */
9043 unsigned int Entry_FR:4; /* Number saved 7..10 */
9044 unsigned int Entry_GR:5; /* Number saved 11..15 */
9045 unsigned int Args_stored:1; /* 16 */
9046 unsigned int Variable_Frame:1; /* 17 */
9047 unsigned int Separate_Package_Body:1; /* 18 */
9048 unsigned int Frame_Extension_Millicode:1; /* 19 */
9049 unsigned int Stack_Overflow_Check:1; /* 20 */
9050 unsigned int Two_Instruction_SP_Increment:1; /* 21 */
9051 unsigned int Ada_Region:1; /* 22 */
9052 unsigned int cxx_info:1; /* 23 */
9053 unsigned int cxx_try_catch:1; /* 24 */
9054 unsigned int sched_entry_seq:1; /* 25 */
9055 unsigned int reserved2:1; /* 26 */
9056 unsigned int Save_SP:1; /* 27 */
9057 unsigned int Save_RP:1; /* 28 */
9058 unsigned int Save_MRP_in_frame:1; /* 29 */
9059 unsigned int extn_ptr_defined:1; /* 30 */
9060 unsigned int Cleanup_defined:1; /* 31 */
9061
9062 unsigned int MPE_XL_interrupt_marker:1; /* 0 */
9063 unsigned int HP_UX_interrupt_marker:1; /* 1 */
9064 unsigned int Large_frame:1; /* 2 */
9065 unsigned int Pseudo_SP_Set:1; /* 3 */
9066 unsigned int reserved4:1; /* 4 */
9067 unsigned int Total_frame_size:27; /* 5..31 */
9068 };
9069
9070 struct hppa_unw_aux_info
9071 {
9072 struct hppa_unw_table_entry * table; /* Unwind table. */
9073 unsigned long table_len; /* Length of unwind table. */
9074 bfd_vma seg_base; /* Starting address of segment. */
9075 Elf_Internal_Sym * symtab; /* The symbol table. */
9076 unsigned long nsyms; /* Number of symbols. */
9077 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
9078 unsigned long nfuns; /* Number of entries in funtab. */
9079 char * strtab; /* The string table. */
9080 unsigned long strtab_size; /* Size of string table. */
9081 };
9082
9083 static bool
9084 dump_hppa_unwind (Filedata * filedata, struct hppa_unw_aux_info * aux)
9085 {
9086 struct hppa_unw_table_entry * tp;
9087 unsigned long j, nfuns;
9088 bool res = true;
9089
9090 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
9091 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
9092 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
9093 aux->funtab[nfuns++] = aux->symtab[j];
9094 aux->nfuns = nfuns;
9095 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
9096
9097 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
9098 {
9099 bfd_vma offset;
9100 const char * procname;
9101
9102 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
9103 aux->strtab_size, tp->start, &procname,
9104 &offset);
9105
9106 fputs ("\n<", stdout);
9107
9108 if (procname)
9109 {
9110 fputs (procname, stdout);
9111
9112 if (offset)
9113 printf ("+%lx", (unsigned long) offset);
9114 }
9115
9116 fputs (">: [", stdout);
9117 print_vma (tp->start.offset, PREFIX_HEX);
9118 fputc ('-', stdout);
9119 print_vma (tp->end.offset, PREFIX_HEX);
9120 printf ("]\n\t");
9121
9122 #define PF(_m) if (tp->_m) printf (#_m " ");
9123 #define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
9124 PF(Cannot_unwind);
9125 PF(Millicode);
9126 PF(Millicode_save_sr0);
9127 /* PV(Region_description); */
9128 PF(Entry_SR);
9129 PV(Entry_FR);
9130 PV(Entry_GR);
9131 PF(Args_stored);
9132 PF(Variable_Frame);
9133 PF(Separate_Package_Body);
9134 PF(Frame_Extension_Millicode);
9135 PF(Stack_Overflow_Check);
9136 PF(Two_Instruction_SP_Increment);
9137 PF(Ada_Region);
9138 PF(cxx_info);
9139 PF(cxx_try_catch);
9140 PF(sched_entry_seq);
9141 PF(Save_SP);
9142 PF(Save_RP);
9143 PF(Save_MRP_in_frame);
9144 PF(extn_ptr_defined);
9145 PF(Cleanup_defined);
9146 PF(MPE_XL_interrupt_marker);
9147 PF(HP_UX_interrupt_marker);
9148 PF(Large_frame);
9149 PF(Pseudo_SP_Set);
9150 PV(Total_frame_size);
9151 #undef PF
9152 #undef PV
9153 }
9154
9155 printf ("\n");
9156
9157 free (aux->funtab);
9158
9159 return res;
9160 }
9161
9162 static bool
9163 slurp_hppa_unwind_table (Filedata * filedata,
9164 struct hppa_unw_aux_info * aux,
9165 Elf_Internal_Shdr * sec)
9166 {
9167 unsigned long size, unw_ent_size, nentries, nrelas, i;
9168 Elf_Internal_Phdr * seg;
9169 struct hppa_unw_table_entry * tep;
9170 Elf_Internal_Shdr * relsec;
9171 Elf_Internal_Rela * rela;
9172 Elf_Internal_Rela * rp;
9173 unsigned char * table;
9174 unsigned char * tp;
9175 Elf_Internal_Sym * sym;
9176 const char * relname;
9177
9178 /* First, find the starting address of the segment that includes
9179 this section. */
9180 if (filedata->file_header.e_phnum)
9181 {
9182 if (! get_program_headers (filedata))
9183 return false;
9184
9185 for (seg = filedata->program_headers;
9186 seg < filedata->program_headers + filedata->file_header.e_phnum;
9187 ++seg)
9188 {
9189 if (seg->p_type != PT_LOAD)
9190 continue;
9191
9192 if (sec->sh_addr >= seg->p_vaddr
9193 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
9194 {
9195 aux->seg_base = seg->p_vaddr;
9196 break;
9197 }
9198 }
9199 }
9200
9201 /* Second, build the unwind table from the contents of the unwind
9202 section. */
9203 size = sec->sh_size;
9204 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
9205 _("unwind table"));
9206 if (!table)
9207 return false;
9208
9209 unw_ent_size = 16;
9210 nentries = size / unw_ent_size;
9211 size = unw_ent_size * nentries;
9212
9213 aux->table_len = nentries;
9214 tep = aux->table = (struct hppa_unw_table_entry *)
9215 xcmalloc (nentries, sizeof (aux->table[0]));
9216
9217 for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
9218 {
9219 unsigned int tmp1, tmp2;
9220
9221 tep->start.section = SHN_UNDEF;
9222 tep->end.section = SHN_UNDEF;
9223
9224 tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
9225 tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
9226 tmp1 = byte_get ((unsigned char *) tp + 8, 4);
9227 tmp2 = byte_get ((unsigned char *) tp + 12, 4);
9228
9229 tep->start.offset += aux->seg_base;
9230 tep->end.offset += aux->seg_base;
9231
9232 tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
9233 tep->Millicode = (tmp1 >> 30) & 0x1;
9234 tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
9235 tep->Region_description = (tmp1 >> 27) & 0x3;
9236 tep->reserved1 = (tmp1 >> 26) & 0x1;
9237 tep->Entry_SR = (tmp1 >> 25) & 0x1;
9238 tep->Entry_FR = (tmp1 >> 21) & 0xf;
9239 tep->Entry_GR = (tmp1 >> 16) & 0x1f;
9240 tep->Args_stored = (tmp1 >> 15) & 0x1;
9241 tep->Variable_Frame = (tmp1 >> 14) & 0x1;
9242 tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
9243 tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
9244 tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
9245 tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
9246 tep->Ada_Region = (tmp1 >> 9) & 0x1;
9247 tep->cxx_info = (tmp1 >> 8) & 0x1;
9248 tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
9249 tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
9250 tep->reserved2 = (tmp1 >> 5) & 0x1;
9251 tep->Save_SP = (tmp1 >> 4) & 0x1;
9252 tep->Save_RP = (tmp1 >> 3) & 0x1;
9253 tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
9254 tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
9255 tep->Cleanup_defined = tmp1 & 0x1;
9256
9257 tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
9258 tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
9259 tep->Large_frame = (tmp2 >> 29) & 0x1;
9260 tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
9261 tep->reserved4 = (tmp2 >> 27) & 0x1;
9262 tep->Total_frame_size = tmp2 & 0x7ffffff;
9263 }
9264 free (table);
9265
9266 /* Third, apply any relocations to the unwind table. */
9267 for (relsec = filedata->section_headers;
9268 relsec < filedata->section_headers + filedata->file_header.e_shnum;
9269 ++relsec)
9270 {
9271 if (relsec->sh_type != SHT_RELA
9272 || relsec->sh_info >= filedata->file_header.e_shnum
9273 || filedata->section_headers + relsec->sh_info != sec)
9274 continue;
9275
9276 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
9277 & rela, & nrelas))
9278 return false;
9279
9280 for (rp = rela; rp < rela + nrelas; ++rp)
9281 {
9282 unsigned int sym_ndx;
9283 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
9284 relname = elf_hppa_reloc_type (r_type);
9285
9286 if (relname == NULL)
9287 {
9288 warn (_("Skipping unknown relocation type: %u\n"), r_type);
9289 continue;
9290 }
9291
9292 /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64. */
9293 if (! startswith (relname, "R_PARISC_SEGREL"))
9294 {
9295 warn (_("Skipping unexpected relocation type: %s\n"), relname);
9296 continue;
9297 }
9298
9299 i = rp->r_offset / unw_ent_size;
9300 if (i >= aux->table_len)
9301 {
9302 warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
9303 continue;
9304 }
9305
9306 sym_ndx = get_reloc_symindex (rp->r_info);
9307 if (sym_ndx >= aux->nsyms)
9308 {
9309 warn (_("Skipping reloc with invalid symbol index: %u\n"),
9310 sym_ndx);
9311 continue;
9312 }
9313 sym = aux->symtab + sym_ndx;
9314
9315 switch ((rp->r_offset % unw_ent_size) / 4)
9316 {
9317 case 0:
9318 aux->table[i].start.section = sym->st_shndx;
9319 aux->table[i].start.offset = sym->st_value + rp->r_addend;
9320 break;
9321 case 1:
9322 aux->table[i].end.section = sym->st_shndx;
9323 aux->table[i].end.offset = sym->st_value + rp->r_addend;
9324 break;
9325 default:
9326 break;
9327 }
9328 }
9329
9330 free (rela);
9331 }
9332
9333 return true;
9334 }
9335
9336 static bool
9337 hppa_process_unwind (Filedata * filedata)
9338 {
9339 struct hppa_unw_aux_info aux;
9340 Elf_Internal_Shdr * unwsec = NULL;
9341 Elf_Internal_Shdr * sec;
9342 unsigned long i;
9343 bool res = true;
9344
9345 if (filedata->string_table == NULL)
9346 return false;
9347
9348 memset (& aux, 0, sizeof (aux));
9349
9350 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9351 {
9352 if (sec->sh_type == SHT_SYMTAB)
9353 {
9354 if (aux.symtab)
9355 {
9356 error (_("Multiple symbol tables encountered\n"));
9357 free (aux.symtab);
9358 aux.symtab = NULL;
9359 free (aux.strtab);
9360 aux.strtab = NULL;
9361 }
9362 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
9363 &aux.strtab, &aux.strtab_size))
9364 return false;
9365 }
9366 else if (section_name_valid (filedata, sec)
9367 && streq (section_name (filedata, sec), ".PARISC.unwind"))
9368 unwsec = sec;
9369 }
9370
9371 if (!unwsec)
9372 printf (_("\nThere are no unwind sections in this file.\n"));
9373
9374 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9375 {
9376 if (section_name_valid (filedata, sec)
9377 && streq (section_name (filedata, sec), ".PARISC.unwind"))
9378 {
9379 unsigned long num_unwind = sec->sh_size / 16;
9380
9381 printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
9382 "contains %lu entry:\n",
9383 "\nUnwind section '%s' at offset 0x%lx "
9384 "contains %lu entries:\n",
9385 num_unwind),
9386 printable_section_name (filedata, sec),
9387 (unsigned long) sec->sh_offset,
9388 num_unwind);
9389
9390 if (! slurp_hppa_unwind_table (filedata, &aux, sec))
9391 res = false;
9392
9393 if (res && aux.table_len > 0)
9394 {
9395 if (! dump_hppa_unwind (filedata, &aux))
9396 res = false;
9397 }
9398
9399 free ((char *) aux.table);
9400 aux.table = NULL;
9401 }
9402 }
9403
9404 free (aux.symtab);
9405 free ((char *) aux.strtab);
9406
9407 return res;
9408 }
9409
9410 struct arm_section
9411 {
9412 unsigned char * data; /* The unwind data. */
9413 Elf_Internal_Shdr * sec; /* The cached unwind section header. */
9414 Elf_Internal_Rela * rela; /* The cached relocations for this section. */
9415 unsigned long nrelas; /* The number of relocations. */
9416 unsigned int rel_type; /* REL or RELA ? */
9417 Elf_Internal_Rela * next_rela; /* Cyclic pointer to the next reloc to process. */
9418 };
9419
9420 struct arm_unw_aux_info
9421 {
9422 Filedata * filedata; /* The file containing the unwind sections. */
9423 Elf_Internal_Sym * symtab; /* The file's symbol table. */
9424 unsigned long nsyms; /* Number of symbols. */
9425 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
9426 unsigned long nfuns; /* Number of these symbols. */
9427 char * strtab; /* The file's string table. */
9428 unsigned long strtab_size; /* Size of string table. */
9429 };
9430
9431 static const char *
9432 arm_print_vma_and_name (Filedata * filedata,
9433 struct arm_unw_aux_info * aux,
9434 bfd_vma fn,
9435 struct absaddr addr)
9436 {
9437 const char *procname;
9438 bfd_vma sym_offset;
9439
9440 if (addr.section == SHN_UNDEF)
9441 addr.offset = fn;
9442
9443 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
9444 aux->strtab_size, addr, &procname,
9445 &sym_offset);
9446
9447 print_vma (fn, PREFIX_HEX);
9448
9449 if (procname)
9450 {
9451 fputs (" <", stdout);
9452 fputs (procname, stdout);
9453
9454 if (sym_offset)
9455 printf ("+0x%lx", (unsigned long) sym_offset);
9456 fputc ('>', stdout);
9457 }
9458
9459 return procname;
9460 }
9461
9462 static void
9463 arm_free_section (struct arm_section *arm_sec)
9464 {
9465 free (arm_sec->data);
9466 free (arm_sec->rela);
9467 }
9468
9469 /* 1) If SEC does not match the one cached in ARM_SEC, then free the current
9470 cached section and install SEC instead.
9471 2) Locate the 32-bit word at WORD_OFFSET in unwind section SEC
9472 and return its valued in * WORDP, relocating if necessary.
9473 3) Update the NEXT_RELA field in ARM_SEC and store the section index and
9474 relocation's offset in ADDR.
9475 4) If SYM_NAME is non-NULL and a relocation was applied, record the offset
9476 into the string table of the symbol associated with the reloc. If no
9477 reloc was applied store -1 there.
9478 5) Return TRUE upon success, FALSE otherwise. */
9479
9480 static bool
9481 get_unwind_section_word (Filedata * filedata,
9482 struct arm_unw_aux_info * aux,
9483 struct arm_section * arm_sec,
9484 Elf_Internal_Shdr * sec,
9485 bfd_vma word_offset,
9486 unsigned int * wordp,
9487 struct absaddr * addr,
9488 bfd_vma * sym_name)
9489 {
9490 Elf_Internal_Rela *rp;
9491 Elf_Internal_Sym *sym;
9492 const char * relname;
9493 unsigned int word;
9494 bool wrapped;
9495
9496 if (sec == NULL || arm_sec == NULL)
9497 return false;
9498
9499 addr->section = SHN_UNDEF;
9500 addr->offset = 0;
9501
9502 if (sym_name != NULL)
9503 *sym_name = (bfd_vma) -1;
9504
9505 /* If necessary, update the section cache. */
9506 if (sec != arm_sec->sec)
9507 {
9508 Elf_Internal_Shdr *relsec;
9509
9510 arm_free_section (arm_sec);
9511
9512 arm_sec->sec = sec;
9513 arm_sec->data = get_data (NULL, aux->filedata, sec->sh_offset, 1,
9514 sec->sh_size, _("unwind data"));
9515 arm_sec->rela = NULL;
9516 arm_sec->nrelas = 0;
9517
9518 for (relsec = filedata->section_headers;
9519 relsec < filedata->section_headers + filedata->file_header.e_shnum;
9520 ++relsec)
9521 {
9522 if (relsec->sh_info >= filedata->file_header.e_shnum
9523 || filedata->section_headers + relsec->sh_info != sec
9524 /* PR 15745: Check the section type as well. */
9525 || (relsec->sh_type != SHT_REL
9526 && relsec->sh_type != SHT_RELA))
9527 continue;
9528
9529 arm_sec->rel_type = relsec->sh_type;
9530 if (relsec->sh_type == SHT_REL)
9531 {
9532 if (!slurp_rel_relocs (aux->filedata, relsec->sh_offset,
9533 relsec->sh_size,
9534 & arm_sec->rela, & arm_sec->nrelas))
9535 return false;
9536 }
9537 else /* relsec->sh_type == SHT_RELA */
9538 {
9539 if (!slurp_rela_relocs (aux->filedata, relsec->sh_offset,
9540 relsec->sh_size,
9541 & arm_sec->rela, & arm_sec->nrelas))
9542 return false;
9543 }
9544 break;
9545 }
9546
9547 arm_sec->next_rela = arm_sec->rela;
9548 }
9549
9550 /* If there is no unwind data we can do nothing. */
9551 if (arm_sec->data == NULL)
9552 return false;
9553
9554 /* If the offset is invalid then fail. */
9555 if (/* PR 21343 *//* PR 18879 */
9556 sec->sh_size < 4
9557 || word_offset > (sec->sh_size - 4)
9558 || ((bfd_signed_vma) word_offset) < 0)
9559 return false;
9560
9561 /* Get the word at the required offset. */
9562 word = byte_get (arm_sec->data + word_offset, 4);
9563
9564 /* PR 17531: file: id:000001,src:001266+003044,op:splice,rep:128. */
9565 if (arm_sec->rela == NULL)
9566 {
9567 * wordp = word;
9568 return true;
9569 }
9570
9571 /* Look through the relocs to find the one that applies to the provided offset. */
9572 wrapped = false;
9573 for (rp = arm_sec->next_rela; rp != arm_sec->rela + arm_sec->nrelas; rp++)
9574 {
9575 bfd_vma prelval, offset;
9576
9577 if (rp->r_offset > word_offset && !wrapped)
9578 {
9579 rp = arm_sec->rela;
9580 wrapped = true;
9581 }
9582 if (rp->r_offset > word_offset)
9583 break;
9584
9585 if (rp->r_offset & 3)
9586 {
9587 warn (_("Skipping unexpected relocation at offset 0x%lx\n"),
9588 (unsigned long) rp->r_offset);
9589 continue;
9590 }
9591
9592 if (rp->r_offset < word_offset)
9593 continue;
9594
9595 /* PR 17531: file: 027-161405-0.004 */
9596 if (aux->symtab == NULL)
9597 continue;
9598
9599 if (arm_sec->rel_type == SHT_REL)
9600 {
9601 offset = word & 0x7fffffff;
9602 if (offset & 0x40000000)
9603 offset |= ~ (bfd_vma) 0x7fffffff;
9604 }
9605 else if (arm_sec->rel_type == SHT_RELA)
9606 offset = rp->r_addend;
9607 else
9608 {
9609 error (_("Unknown section relocation type %d encountered\n"),
9610 arm_sec->rel_type);
9611 break;
9612 }
9613
9614 /* PR 17531 file: 027-1241568-0.004. */
9615 if (ELF32_R_SYM (rp->r_info) >= aux->nsyms)
9616 {
9617 error (_("Bad symbol index in unwind relocation (%lu > %lu)\n"),
9618 (unsigned long) ELF32_R_SYM (rp->r_info), aux->nsyms);
9619 break;
9620 }
9621
9622 sym = aux->symtab + ELF32_R_SYM (rp->r_info);
9623 offset += sym->st_value;
9624 prelval = offset - (arm_sec->sec->sh_addr + rp->r_offset);
9625
9626 /* Check that we are processing the expected reloc type. */
9627 if (filedata->file_header.e_machine == EM_ARM)
9628 {
9629 relname = elf_arm_reloc_type (ELF32_R_TYPE (rp->r_info));
9630 if (relname == NULL)
9631 {
9632 warn (_("Skipping unknown ARM relocation type: %d\n"),
9633 (int) ELF32_R_TYPE (rp->r_info));
9634 continue;
9635 }
9636
9637 if (streq (relname, "R_ARM_NONE"))
9638 continue;
9639
9640 if (! streq (relname, "R_ARM_PREL31"))
9641 {
9642 warn (_("Skipping unexpected ARM relocation type %s\n"), relname);
9643 continue;
9644 }
9645 }
9646 else if (filedata->file_header.e_machine == EM_TI_C6000)
9647 {
9648 relname = elf_tic6x_reloc_type (ELF32_R_TYPE (rp->r_info));
9649 if (relname == NULL)
9650 {
9651 warn (_("Skipping unknown C6000 relocation type: %d\n"),
9652 (int) ELF32_R_TYPE (rp->r_info));
9653 continue;
9654 }
9655
9656 if (streq (relname, "R_C6000_NONE"))
9657 continue;
9658
9659 if (! streq (relname, "R_C6000_PREL31"))
9660 {
9661 warn (_("Skipping unexpected C6000 relocation type %s\n"), relname);
9662 continue;
9663 }
9664
9665 prelval >>= 1;
9666 }
9667 else
9668 {
9669 /* This function currently only supports ARM and TI unwinders. */
9670 warn (_("Only TI and ARM unwinders are currently supported\n"));
9671 break;
9672 }
9673
9674 word = (word & ~ (bfd_vma) 0x7fffffff) | (prelval & 0x7fffffff);
9675 addr->section = sym->st_shndx;
9676 addr->offset = offset;
9677
9678 if (sym_name)
9679 * sym_name = sym->st_name;
9680 break;
9681 }
9682
9683 *wordp = word;
9684 arm_sec->next_rela = rp;
9685
9686 return true;
9687 }
9688
9689 static const char *tic6x_unwind_regnames[16] =
9690 {
9691 "A15", "B15", "B14", "B13", "B12", "B11", "B10", "B3",
9692 "A14", "A13", "A12", "A11", "A10",
9693 "[invalid reg 13]", "[invalid reg 14]", "[invalid reg 15]"
9694 };
9695
9696 static void
9697 decode_tic6x_unwind_regmask (unsigned int mask)
9698 {
9699 int i;
9700
9701 for (i = 12; mask; mask >>= 1, i--)
9702 {
9703 if (mask & 1)
9704 {
9705 fputs (tic6x_unwind_regnames[i], stdout);
9706 if (mask > 1)
9707 fputs (", ", stdout);
9708 }
9709 }
9710 }
9711
9712 #define ADVANCE \
9713 if (remaining == 0 && more_words) \
9714 { \
9715 data_offset += 4; \
9716 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, \
9717 data_offset, & word, & addr, NULL)) \
9718 return false; \
9719 remaining = 4; \
9720 more_words--; \
9721 } \
9722
9723 #define GET_OP(OP) \
9724 ADVANCE; \
9725 if (remaining) \
9726 { \
9727 remaining--; \
9728 (OP) = word >> 24; \
9729 word <<= 8; \
9730 } \
9731 else \
9732 { \
9733 printf (_("[Truncated opcode]\n")); \
9734 return false; \
9735 } \
9736 printf ("0x%02x ", OP)
9737
9738 static bool
9739 decode_arm_unwind_bytecode (Filedata * filedata,
9740 struct arm_unw_aux_info * aux,
9741 unsigned int word,
9742 unsigned int remaining,
9743 unsigned int more_words,
9744 bfd_vma data_offset,
9745 Elf_Internal_Shdr * data_sec,
9746 struct arm_section * data_arm_sec)
9747 {
9748 struct absaddr addr;
9749 bool res = true;
9750
9751 /* Decode the unwinding instructions. */
9752 while (1)
9753 {
9754 unsigned int op, op2;
9755
9756 ADVANCE;
9757 if (remaining == 0)
9758 break;
9759 remaining--;
9760 op = word >> 24;
9761 word <<= 8;
9762
9763 printf (" 0x%02x ", op);
9764
9765 if ((op & 0xc0) == 0x00)
9766 {
9767 int offset = ((op & 0x3f) << 2) + 4;
9768
9769 printf (" vsp = vsp + %d", offset);
9770 }
9771 else if ((op & 0xc0) == 0x40)
9772 {
9773 int offset = ((op & 0x3f) << 2) + 4;
9774
9775 printf (" vsp = vsp - %d", offset);
9776 }
9777 else if ((op & 0xf0) == 0x80)
9778 {
9779 GET_OP (op2);
9780 if (op == 0x80 && op2 == 0)
9781 printf (_("Refuse to unwind"));
9782 else
9783 {
9784 unsigned int mask = ((op & 0x0f) << 8) | op2;
9785 bool first = true;
9786 int i;
9787
9788 printf ("pop {");
9789 for (i = 0; i < 12; i++)
9790 if (mask & (1 << i))
9791 {
9792 if (first)
9793 first = false;
9794 else
9795 printf (", ");
9796 printf ("r%d", 4 + i);
9797 }
9798 printf ("}");
9799 }
9800 }
9801 else if ((op & 0xf0) == 0x90)
9802 {
9803 if (op == 0x9d || op == 0x9f)
9804 printf (_(" [Reserved]"));
9805 else
9806 printf (" vsp = r%d", op & 0x0f);
9807 }
9808 else if ((op & 0xf0) == 0xa0)
9809 {
9810 int end = 4 + (op & 0x07);
9811 bool first = true;
9812 int i;
9813
9814 printf (" pop {");
9815 for (i = 4; i <= end; i++)
9816 {
9817 if (first)
9818 first = false;
9819 else
9820 printf (", ");
9821 printf ("r%d", i);
9822 }
9823 if (op & 0x08)
9824 {
9825 if (!first)
9826 printf (", ");
9827 printf ("r14");
9828 }
9829 printf ("}");
9830 }
9831 else if (op == 0xb0)
9832 printf (_(" finish"));
9833 else if (op == 0xb1)
9834 {
9835 GET_OP (op2);
9836 if (op2 == 0 || (op2 & 0xf0) != 0)
9837 printf (_("[Spare]"));
9838 else
9839 {
9840 unsigned int mask = op2 & 0x0f;
9841 bool first = true;
9842 int i;
9843
9844 printf ("pop {");
9845 for (i = 0; i < 12; i++)
9846 if (mask & (1 << i))
9847 {
9848 if (first)
9849 first = false;
9850 else
9851 printf (", ");
9852 printf ("r%d", i);
9853 }
9854 printf ("}");
9855 }
9856 }
9857 else if (op == 0xb2)
9858 {
9859 unsigned char buf[9];
9860 unsigned int i, len;
9861 unsigned long offset;
9862
9863 for (i = 0; i < sizeof (buf); i++)
9864 {
9865 GET_OP (buf[i]);
9866 if ((buf[i] & 0x80) == 0)
9867 break;
9868 }
9869 if (i == sizeof (buf))
9870 {
9871 error (_("corrupt change to vsp\n"));
9872 res = false;
9873 }
9874 else
9875 {
9876 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
9877 assert (len == i + 1);
9878 offset = offset * 4 + 0x204;
9879 printf ("vsp = vsp + %ld", offset);
9880 }
9881 }
9882 else if (op == 0xb3 || op == 0xc8 || op == 0xc9)
9883 {
9884 unsigned int first, last;
9885
9886 GET_OP (op2);
9887 first = op2 >> 4;
9888 last = op2 & 0x0f;
9889 if (op == 0xc8)
9890 first = first + 16;
9891 printf ("pop {D%d", first);
9892 if (last)
9893 printf ("-D%d", first + last);
9894 printf ("}");
9895 }
9896 else if (op == 0xb4)
9897 printf (_(" pop {ra_auth_code}"));
9898 else if ((op & 0xf8) == 0xb8 || (op & 0xf8) == 0xd0)
9899 {
9900 unsigned int count = op & 0x07;
9901
9902 printf ("pop {D8");
9903 if (count)
9904 printf ("-D%d", 8 + count);
9905 printf ("}");
9906 }
9907 else if (op >= 0xc0 && op <= 0xc5)
9908 {
9909 unsigned int count = op & 0x07;
9910
9911 printf (" pop {wR10");
9912 if (count)
9913 printf ("-wR%d", 10 + count);
9914 printf ("}");
9915 }
9916 else if (op == 0xc6)
9917 {
9918 unsigned int first, last;
9919
9920 GET_OP (op2);
9921 first = op2 >> 4;
9922 last = op2 & 0x0f;
9923 printf ("pop {wR%d", first);
9924 if (last)
9925 printf ("-wR%d", first + last);
9926 printf ("}");
9927 }
9928 else if (op == 0xc7)
9929 {
9930 GET_OP (op2);
9931 if (op2 == 0 || (op2 & 0xf0) != 0)
9932 printf (_("[Spare]"));
9933 else
9934 {
9935 unsigned int mask = op2 & 0x0f;
9936 bool first = true;
9937 int i;
9938
9939 printf ("pop {");
9940 for (i = 0; i < 4; i++)
9941 if (mask & (1 << i))
9942 {
9943 if (first)
9944 first = false;
9945 else
9946 printf (", ");
9947 printf ("wCGR%d", i);
9948 }
9949 printf ("}");
9950 }
9951 }
9952 else
9953 {
9954 printf (_(" [unsupported opcode]"));
9955 res = false;
9956 }
9957
9958 printf ("\n");
9959 }
9960
9961 return res;
9962 }
9963
9964 static bool
9965 decode_tic6x_unwind_bytecode (Filedata * filedata,
9966 struct arm_unw_aux_info * aux,
9967 unsigned int word,
9968 unsigned int remaining,
9969 unsigned int more_words,
9970 bfd_vma data_offset,
9971 Elf_Internal_Shdr * data_sec,
9972 struct arm_section * data_arm_sec)
9973 {
9974 struct absaddr addr;
9975
9976 /* Decode the unwinding instructions. */
9977 while (1)
9978 {
9979 unsigned int op, op2;
9980
9981 ADVANCE;
9982 if (remaining == 0)
9983 break;
9984 remaining--;
9985 op = word >> 24;
9986 word <<= 8;
9987
9988 printf (" 0x%02x ", op);
9989
9990 if ((op & 0xc0) == 0x00)
9991 {
9992 int offset = ((op & 0x3f) << 3) + 8;
9993 printf (" sp = sp + %d", offset);
9994 }
9995 else if ((op & 0xc0) == 0x80)
9996 {
9997 GET_OP (op2);
9998 if (op == 0x80 && op2 == 0)
9999 printf (_("Refuse to unwind"));
10000 else
10001 {
10002 unsigned int mask = ((op & 0x1f) << 8) | op2;
10003 if (op & 0x20)
10004 printf ("pop compact {");
10005 else
10006 printf ("pop {");
10007
10008 decode_tic6x_unwind_regmask (mask);
10009 printf("}");
10010 }
10011 }
10012 else if ((op & 0xf0) == 0xc0)
10013 {
10014 unsigned int reg;
10015 unsigned int nregs;
10016 unsigned int i;
10017 const char *name;
10018 struct
10019 {
10020 unsigned int offset;
10021 unsigned int reg;
10022 } regpos[16];
10023
10024 /* Scan entire instruction first so that GET_OP output is not
10025 interleaved with disassembly. */
10026 nregs = 0;
10027 for (i = 0; nregs < (op & 0xf); i++)
10028 {
10029 GET_OP (op2);
10030 reg = op2 >> 4;
10031 if (reg != 0xf)
10032 {
10033 regpos[nregs].offset = i * 2;
10034 regpos[nregs].reg = reg;
10035 nregs++;
10036 }
10037
10038 reg = op2 & 0xf;
10039 if (reg != 0xf)
10040 {
10041 regpos[nregs].offset = i * 2 + 1;
10042 regpos[nregs].reg = reg;
10043 nregs++;
10044 }
10045 }
10046
10047 printf (_("pop frame {"));
10048 if (nregs == 0)
10049 {
10050 printf (_("*corrupt* - no registers specified"));
10051 }
10052 else
10053 {
10054 reg = nregs - 1;
10055 for (i = i * 2; i > 0; i--)
10056 {
10057 if (regpos[reg].offset == i - 1)
10058 {
10059 name = tic6x_unwind_regnames[regpos[reg].reg];
10060 if (reg > 0)
10061 reg--;
10062 }
10063 else
10064 name = _("[pad]");
10065
10066 fputs (name, stdout);
10067 if (i > 1)
10068 printf (", ");
10069 }
10070 }
10071
10072 printf ("}");
10073 }
10074 else if (op == 0xd0)
10075 printf (" MOV FP, SP");
10076 else if (op == 0xd1)
10077 printf (" __c6xabi_pop_rts");
10078 else if (op == 0xd2)
10079 {
10080 unsigned char buf[9];
10081 unsigned int i, len;
10082 unsigned long offset;
10083
10084 for (i = 0; i < sizeof (buf); i++)
10085 {
10086 GET_OP (buf[i]);
10087 if ((buf[i] & 0x80) == 0)
10088 break;
10089 }
10090 /* PR 17531: file: id:000001,src:001906+004739,op:splice,rep:2. */
10091 if (i == sizeof (buf))
10092 {
10093 warn (_("Corrupt stack pointer adjustment detected\n"));
10094 return false;
10095 }
10096
10097 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
10098 assert (len == i + 1);
10099 offset = offset * 8 + 0x408;
10100 printf (_("sp = sp + %ld"), offset);
10101 }
10102 else if ((op & 0xf0) == 0xe0)
10103 {
10104 if ((op & 0x0f) == 7)
10105 printf (" RETURN");
10106 else
10107 printf (" MV %s, B3", tic6x_unwind_regnames[op & 0x0f]);
10108 }
10109 else
10110 {
10111 printf (_(" [unsupported opcode]"));
10112 }
10113 putchar ('\n');
10114 }
10115
10116 return true;
10117 }
10118
10119 static bfd_vma
10120 arm_expand_prel31 (Filedata * filedata, bfd_vma word, bfd_vma where)
10121 {
10122 bfd_vma offset;
10123
10124 offset = word & 0x7fffffff;
10125 if (offset & 0x40000000)
10126 offset |= ~ (bfd_vma) 0x7fffffff;
10127
10128 if (filedata->file_header.e_machine == EM_TI_C6000)
10129 offset <<= 1;
10130
10131 return offset + where;
10132 }
10133
10134 static bool
10135 decode_arm_unwind (Filedata * filedata,
10136 struct arm_unw_aux_info * aux,
10137 unsigned int word,
10138 unsigned int remaining,
10139 bfd_vma data_offset,
10140 Elf_Internal_Shdr * data_sec,
10141 struct arm_section * data_arm_sec)
10142 {
10143 int per_index;
10144 unsigned int more_words = 0;
10145 struct absaddr addr;
10146 bfd_vma sym_name = (bfd_vma) -1;
10147 bool res = true;
10148
10149 if (remaining == 0)
10150 {
10151 /* Fetch the first word.
10152 Note - when decoding an object file the address extracted
10153 here will always be 0. So we also pass in the sym_name
10154 parameter so that we can find the symbol associated with
10155 the personality routine. */
10156 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, data_offset,
10157 & word, & addr, & sym_name))
10158 return false;
10159
10160 remaining = 4;
10161 }
10162 else
10163 {
10164 addr.section = SHN_UNDEF;
10165 addr.offset = 0;
10166 }
10167
10168 if ((word & 0x80000000) == 0)
10169 {
10170 /* Expand prel31 for personality routine. */
10171 bfd_vma fn;
10172 const char *procname;
10173
10174 fn = arm_expand_prel31 (filedata, word, data_sec->sh_addr + data_offset);
10175 printf (_(" Personality routine: "));
10176 if (fn == 0
10177 && addr.section == SHN_UNDEF && addr.offset == 0
10178 && sym_name != (bfd_vma) -1 && sym_name < aux->strtab_size)
10179 {
10180 procname = aux->strtab + sym_name;
10181 print_vma (fn, PREFIX_HEX);
10182 if (procname)
10183 {
10184 fputs (" <", stdout);
10185 fputs (procname, stdout);
10186 fputc ('>', stdout);
10187 }
10188 }
10189 else
10190 procname = arm_print_vma_and_name (filedata, aux, fn, addr);
10191 fputc ('\n', stdout);
10192
10193 /* The GCC personality routines use the standard compact
10194 encoding, starting with one byte giving the number of
10195 words. */
10196 if (procname != NULL
10197 && (startswith (procname, "__gcc_personality_v0")
10198 || startswith (procname, "__gxx_personality_v0")
10199 || startswith (procname, "__gcj_personality_v0")
10200 || startswith (procname, "__gnu_objc_personality_v0")))
10201 {
10202 remaining = 0;
10203 more_words = 1;
10204 ADVANCE;
10205 if (!remaining)
10206 {
10207 printf (_(" [Truncated data]\n"));
10208 return false;
10209 }
10210 more_words = word >> 24;
10211 word <<= 8;
10212 remaining--;
10213 per_index = -1;
10214 }
10215 else
10216 return true;
10217 }
10218 else
10219 {
10220 /* ARM EHABI Section 6.3:
10221
10222 An exception-handling table entry for the compact model looks like:
10223
10224 31 30-28 27-24 23-0
10225 -- ----- ----- ----
10226 1 0 index Data for personalityRoutine[index] */
10227
10228 if (filedata->file_header.e_machine == EM_ARM
10229 && (word & 0x70000000))
10230 {
10231 warn (_("Corrupt ARM compact model table entry: %x \n"), word);
10232 res = false;
10233 }
10234
10235 per_index = (word >> 24) & 0x7f;
10236 printf (_(" Compact model index: %d\n"), per_index);
10237 if (per_index == 0)
10238 {
10239 more_words = 0;
10240 word <<= 8;
10241 remaining--;
10242 }
10243 else if (per_index < 3)
10244 {
10245 more_words = (word >> 16) & 0xff;
10246 word <<= 16;
10247 remaining -= 2;
10248 }
10249 }
10250
10251 switch (filedata->file_header.e_machine)
10252 {
10253 case EM_ARM:
10254 if (per_index < 3)
10255 {
10256 if (! decode_arm_unwind_bytecode (filedata, aux, word, remaining, more_words,
10257 data_offset, data_sec, data_arm_sec))
10258 res = false;
10259 }
10260 else
10261 {
10262 warn (_("Unknown ARM compact model index encountered\n"));
10263 printf (_(" [reserved]\n"));
10264 res = false;
10265 }
10266 break;
10267
10268 case EM_TI_C6000:
10269 if (per_index < 3)
10270 {
10271 if (! decode_tic6x_unwind_bytecode (filedata, aux, word, remaining, more_words,
10272 data_offset, data_sec, data_arm_sec))
10273 res = false;
10274 }
10275 else if (per_index < 5)
10276 {
10277 if (((word >> 17) & 0x7f) == 0x7f)
10278 printf (_(" Restore stack from frame pointer\n"));
10279 else
10280 printf (_(" Stack increment %d\n"), (word >> 14) & 0x1fc);
10281 printf (_(" Registers restored: "));
10282 if (per_index == 4)
10283 printf (" (compact) ");
10284 decode_tic6x_unwind_regmask ((word >> 4) & 0x1fff);
10285 putchar ('\n');
10286 printf (_(" Return register: %s\n"),
10287 tic6x_unwind_regnames[word & 0xf]);
10288 }
10289 else
10290 printf (_(" [reserved (%d)]\n"), per_index);
10291 break;
10292
10293 default:
10294 error (_("Unsupported architecture type %d encountered when decoding unwind table\n"),
10295 filedata->file_header.e_machine);
10296 res = false;
10297 }
10298
10299 /* Decode the descriptors. Not implemented. */
10300
10301 return res;
10302 }
10303
10304 static bool
10305 dump_arm_unwind (Filedata * filedata,
10306 struct arm_unw_aux_info * aux,
10307 Elf_Internal_Shdr * exidx_sec)
10308 {
10309 struct arm_section exidx_arm_sec, extab_arm_sec;
10310 unsigned int i, exidx_len;
10311 unsigned long j, nfuns;
10312 bool res = true;
10313
10314 memset (&exidx_arm_sec, 0, sizeof (exidx_arm_sec));
10315 memset (&extab_arm_sec, 0, sizeof (extab_arm_sec));
10316 exidx_len = exidx_sec->sh_size / 8;
10317
10318 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
10319 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
10320 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
10321 aux->funtab[nfuns++] = aux->symtab[j];
10322 aux->nfuns = nfuns;
10323 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
10324
10325 for (i = 0; i < exidx_len; i++)
10326 {
10327 unsigned int exidx_fn, exidx_entry;
10328 struct absaddr fn_addr, entry_addr;
10329 bfd_vma fn;
10330
10331 fputc ('\n', stdout);
10332
10333 if (! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
10334 8 * i, & exidx_fn, & fn_addr, NULL)
10335 || ! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
10336 8 * i + 4, & exidx_entry, & entry_addr, NULL))
10337 {
10338 free (aux->funtab);
10339 arm_free_section (& exidx_arm_sec);
10340 arm_free_section (& extab_arm_sec);
10341 return false;
10342 }
10343
10344 /* ARM EHABI, Section 5:
10345 An index table entry consists of 2 words.
10346 The first word contains a prel31 offset to the start of a function, with bit 31 clear. */
10347 if (exidx_fn & 0x80000000)
10348 {
10349 warn (_("corrupt index table entry: %x\n"), exidx_fn);
10350 res = false;
10351 }
10352
10353 fn = arm_expand_prel31 (filedata, exidx_fn, exidx_sec->sh_addr + 8 * i);
10354
10355 arm_print_vma_and_name (filedata, aux, fn, fn_addr);
10356 fputs (": ", stdout);
10357
10358 if (exidx_entry == 1)
10359 {
10360 print_vma (exidx_entry, PREFIX_HEX);
10361 fputs (" [cantunwind]\n", stdout);
10362 }
10363 else if (exidx_entry & 0x80000000)
10364 {
10365 print_vma (exidx_entry, PREFIX_HEX);
10366 fputc ('\n', stdout);
10367 decode_arm_unwind (filedata, aux, exidx_entry, 4, 0, NULL, NULL);
10368 }
10369 else
10370 {
10371 bfd_vma table, table_offset = 0;
10372 Elf_Internal_Shdr *table_sec;
10373
10374 fputs ("@", stdout);
10375 table = arm_expand_prel31 (filedata, exidx_entry, exidx_sec->sh_addr + 8 * i + 4);
10376 print_vma (table, PREFIX_HEX);
10377 printf ("\n");
10378
10379 /* Locate the matching .ARM.extab. */
10380 if (entry_addr.section != SHN_UNDEF
10381 && entry_addr.section < filedata->file_header.e_shnum)
10382 {
10383 table_sec = filedata->section_headers + entry_addr.section;
10384 table_offset = entry_addr.offset;
10385 /* PR 18879 */
10386 if (table_offset > table_sec->sh_size
10387 || ((bfd_signed_vma) table_offset) < 0)
10388 {
10389 warn (_("Unwind entry contains corrupt offset (0x%lx) into section %s\n"),
10390 (unsigned long) table_offset,
10391 printable_section_name (filedata, table_sec));
10392 res = false;
10393 continue;
10394 }
10395 }
10396 else
10397 {
10398 table_sec = find_section_by_address (filedata, table);
10399 if (table_sec != NULL)
10400 table_offset = table - table_sec->sh_addr;
10401 }
10402
10403 if (table_sec == NULL)
10404 {
10405 warn (_("Could not locate .ARM.extab section containing 0x%lx.\n"),
10406 (unsigned long) table);
10407 res = false;
10408 continue;
10409 }
10410
10411 if (! decode_arm_unwind (filedata, aux, 0, 0, table_offset, table_sec,
10412 &extab_arm_sec))
10413 res = false;
10414 }
10415 }
10416
10417 printf ("\n");
10418
10419 free (aux->funtab);
10420 arm_free_section (&exidx_arm_sec);
10421 arm_free_section (&extab_arm_sec);
10422
10423 return res;
10424 }
10425
10426 /* Used for both ARM and C6X unwinding tables. */
10427
10428 static bool
10429 arm_process_unwind (Filedata * filedata)
10430 {
10431 struct arm_unw_aux_info aux;
10432 Elf_Internal_Shdr *unwsec = NULL;
10433 Elf_Internal_Shdr *sec;
10434 unsigned long i;
10435 unsigned int sec_type;
10436 bool res = true;
10437
10438 switch (filedata->file_header.e_machine)
10439 {
10440 case EM_ARM:
10441 sec_type = SHT_ARM_EXIDX;
10442 break;
10443
10444 case EM_TI_C6000:
10445 sec_type = SHT_C6000_UNWIND;
10446 break;
10447
10448 default:
10449 error (_("Unsupported architecture type %d encountered when processing unwind table\n"),
10450 filedata->file_header.e_machine);
10451 return false;
10452 }
10453
10454 if (filedata->string_table == NULL)
10455 return false;
10456
10457 memset (& aux, 0, sizeof (aux));
10458 aux.filedata = filedata;
10459
10460 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
10461 {
10462 if (sec->sh_type == SHT_SYMTAB)
10463 {
10464 if (aux.symtab)
10465 {
10466 error (_("Multiple symbol tables encountered\n"));
10467 free (aux.symtab);
10468 aux.symtab = NULL;
10469 free (aux.strtab);
10470 aux.strtab = NULL;
10471 }
10472 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
10473 &aux.strtab, &aux.strtab_size))
10474 return false;
10475 }
10476 else if (sec->sh_type == sec_type)
10477 unwsec = sec;
10478 }
10479
10480 if (unwsec == NULL)
10481 printf (_("\nThere are no unwind sections in this file.\n"));
10482 else
10483 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
10484 {
10485 if (sec->sh_type == sec_type)
10486 {
10487 unsigned long num_unwind = sec->sh_size / (2 * eh_addr_size);
10488 printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
10489 "contains %lu entry:\n",
10490 "\nUnwind section '%s' at offset 0x%lx "
10491 "contains %lu entries:\n",
10492 num_unwind),
10493 printable_section_name (filedata, sec),
10494 (unsigned long) sec->sh_offset,
10495 num_unwind);
10496
10497 if (! dump_arm_unwind (filedata, &aux, sec))
10498 res = false;
10499 }
10500 }
10501
10502 free (aux.symtab);
10503 free ((char *) aux.strtab);
10504
10505 return res;
10506 }
10507
10508 static bool
10509 no_processor_specific_unwind (Filedata * filedata ATTRIBUTE_UNUSED)
10510 {
10511 printf (_("No processor specific unwind information to decode\n"));
10512 return true;
10513 }
10514
10515 static bool
10516 process_unwind (Filedata * filedata)
10517 {
10518 struct unwind_handler
10519 {
10520 unsigned int machtype;
10521 bool (* handler)(Filedata *);
10522 } handlers[] =
10523 {
10524 { EM_ARM, arm_process_unwind },
10525 { EM_IA_64, ia64_process_unwind },
10526 { EM_PARISC, hppa_process_unwind },
10527 { EM_TI_C6000, arm_process_unwind },
10528 { EM_386, no_processor_specific_unwind },
10529 { EM_X86_64, no_processor_specific_unwind },
10530 { 0, NULL }
10531 };
10532 int i;
10533
10534 if (!do_unwind)
10535 return true;
10536
10537 for (i = 0; handlers[i].handler != NULL; i++)
10538 if (filedata->file_header.e_machine == handlers[i].machtype)
10539 return handlers[i].handler (filedata);
10540
10541 printf (_("\nThe decoding of unwind sections for machine type %s is not currently supported.\n"),
10542 get_machine_name (filedata->file_header.e_machine));
10543 return true;
10544 }
10545
10546 static void
10547 dynamic_section_aarch64_val (Elf_Internal_Dyn * entry)
10548 {
10549 switch (entry->d_tag)
10550 {
10551 case DT_AARCH64_BTI_PLT:
10552 case DT_AARCH64_PAC_PLT:
10553 break;
10554 default:
10555 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10556 break;
10557 }
10558 putchar ('\n');
10559 }
10560
10561 static void
10562 dynamic_section_mips_val (Filedata * filedata, Elf_Internal_Dyn * entry)
10563 {
10564 switch (entry->d_tag)
10565 {
10566 case DT_MIPS_FLAGS:
10567 if (entry->d_un.d_val == 0)
10568 printf (_("NONE"));
10569 else
10570 {
10571 static const char * opts[] =
10572 {
10573 "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
10574 "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
10575 "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
10576 "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
10577 "RLD_ORDER_SAFE"
10578 };
10579 unsigned int cnt;
10580 bool first = true;
10581
10582 for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
10583 if (entry->d_un.d_val & (1 << cnt))
10584 {
10585 printf ("%s%s", first ? "" : " ", opts[cnt]);
10586 first = false;
10587 }
10588 }
10589 break;
10590
10591 case DT_MIPS_IVERSION:
10592 if (valid_dynamic_name (filedata, entry->d_un.d_val))
10593 printf (_("Interface Version: %s"),
10594 get_dynamic_name (filedata, entry->d_un.d_val));
10595 else
10596 {
10597 char buf[40];
10598 sprintf_vma (buf, entry->d_un.d_ptr);
10599 /* Note: coded this way so that there is a single string for translation. */
10600 printf (_("<corrupt: %s>"), buf);
10601 }
10602 break;
10603
10604 case DT_MIPS_TIME_STAMP:
10605 {
10606 char timebuf[128];
10607 struct tm * tmp;
10608 time_t atime = entry->d_un.d_val;
10609
10610 tmp = gmtime (&atime);
10611 /* PR 17531: file: 6accc532. */
10612 if (tmp == NULL)
10613 snprintf (timebuf, sizeof (timebuf), _("<corrupt>"));
10614 else
10615 snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
10616 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
10617 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
10618 printf (_("Time Stamp: %s"), timebuf);
10619 }
10620 break;
10621
10622 case DT_MIPS_RLD_VERSION:
10623 case DT_MIPS_LOCAL_GOTNO:
10624 case DT_MIPS_CONFLICTNO:
10625 case DT_MIPS_LIBLISTNO:
10626 case DT_MIPS_SYMTABNO:
10627 case DT_MIPS_UNREFEXTNO:
10628 case DT_MIPS_HIPAGENO:
10629 case DT_MIPS_DELTA_CLASS_NO:
10630 case DT_MIPS_DELTA_INSTANCE_NO:
10631 case DT_MIPS_DELTA_RELOC_NO:
10632 case DT_MIPS_DELTA_SYM_NO:
10633 case DT_MIPS_DELTA_CLASSSYM_NO:
10634 case DT_MIPS_COMPACT_SIZE:
10635 print_vma (entry->d_un.d_val, DEC);
10636 break;
10637
10638 case DT_MIPS_XHASH:
10639 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
10640 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10641 /* Falls through. */
10642
10643 default:
10644 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10645 }
10646 putchar ('\n');
10647 }
10648
10649 static void
10650 dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
10651 {
10652 switch (entry->d_tag)
10653 {
10654 case DT_HP_DLD_FLAGS:
10655 {
10656 static struct
10657 {
10658 long int bit;
10659 const char * str;
10660 }
10661 flags[] =
10662 {
10663 { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
10664 { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
10665 { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
10666 { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
10667 { DT_HP_BIND_NOW, "HP_BIND_NOW" },
10668 { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
10669 { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
10670 { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
10671 { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
10672 { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
10673 { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
10674 { DT_HP_GST, "HP_GST" },
10675 { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
10676 { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
10677 { DT_HP_NODELETE, "HP_NODELETE" },
10678 { DT_HP_GROUP, "HP_GROUP" },
10679 { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
10680 };
10681 bool first = true;
10682 size_t cnt;
10683 bfd_vma val = entry->d_un.d_val;
10684
10685 for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
10686 if (val & flags[cnt].bit)
10687 {
10688 if (! first)
10689 putchar (' ');
10690 fputs (flags[cnt].str, stdout);
10691 first = false;
10692 val ^= flags[cnt].bit;
10693 }
10694
10695 if (val != 0 || first)
10696 {
10697 if (! first)
10698 putchar (' ');
10699 print_vma (val, HEX);
10700 }
10701 }
10702 break;
10703
10704 default:
10705 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10706 break;
10707 }
10708 putchar ('\n');
10709 }
10710
10711 #ifdef BFD64
10712
10713 /* VMS vs Unix time offset and factor. */
10714
10715 #define VMS_EPOCH_OFFSET 35067168000000000LL
10716 #define VMS_GRANULARITY_FACTOR 10000000
10717 #ifndef INT64_MIN
10718 #define INT64_MIN (-9223372036854775807LL - 1)
10719 #endif
10720
10721 /* Display a VMS time in a human readable format. */
10722
10723 static void
10724 print_vms_time (bfd_int64_t vmstime)
10725 {
10726 struct tm *tm = NULL;
10727 time_t unxtime;
10728
10729 if (vmstime >= INT64_MIN + VMS_EPOCH_OFFSET)
10730 {
10731 vmstime = (vmstime - VMS_EPOCH_OFFSET) / VMS_GRANULARITY_FACTOR;
10732 unxtime = vmstime;
10733 if (unxtime == vmstime)
10734 tm = gmtime (&unxtime);
10735 }
10736 if (tm != NULL)
10737 printf ("%04u-%02u-%02uT%02u:%02u:%02u",
10738 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
10739 tm->tm_hour, tm->tm_min, tm->tm_sec);
10740 }
10741 #endif /* BFD64 */
10742
10743 static void
10744 dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
10745 {
10746 switch (entry->d_tag)
10747 {
10748 case DT_IA_64_PLT_RESERVE:
10749 /* First 3 slots reserved. */
10750 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10751 printf (" -- ");
10752 print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
10753 break;
10754
10755 case DT_IA_64_VMS_LINKTIME:
10756 #ifdef BFD64
10757 print_vms_time (entry->d_un.d_val);
10758 #endif
10759 break;
10760
10761 case DT_IA_64_VMS_LNKFLAGS:
10762 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10763 if (entry->d_un.d_val & VMS_LF_CALL_DEBUG)
10764 printf (" CALL_DEBUG");
10765 if (entry->d_un.d_val & VMS_LF_NOP0BUFS)
10766 printf (" NOP0BUFS");
10767 if (entry->d_un.d_val & VMS_LF_P0IMAGE)
10768 printf (" P0IMAGE");
10769 if (entry->d_un.d_val & VMS_LF_MKTHREADS)
10770 printf (" MKTHREADS");
10771 if (entry->d_un.d_val & VMS_LF_UPCALLS)
10772 printf (" UPCALLS");
10773 if (entry->d_un.d_val & VMS_LF_IMGSTA)
10774 printf (" IMGSTA");
10775 if (entry->d_un.d_val & VMS_LF_INITIALIZE)
10776 printf (" INITIALIZE");
10777 if (entry->d_un.d_val & VMS_LF_MAIN)
10778 printf (" MAIN");
10779 if (entry->d_un.d_val & VMS_LF_EXE_INIT)
10780 printf (" EXE_INIT");
10781 if (entry->d_un.d_val & VMS_LF_TBK_IN_IMG)
10782 printf (" TBK_IN_IMG");
10783 if (entry->d_un.d_val & VMS_LF_DBG_IN_IMG)
10784 printf (" DBG_IN_IMG");
10785 if (entry->d_un.d_val & VMS_LF_TBK_IN_DSF)
10786 printf (" TBK_IN_DSF");
10787 if (entry->d_un.d_val & VMS_LF_DBG_IN_DSF)
10788 printf (" DBG_IN_DSF");
10789 if (entry->d_un.d_val & VMS_LF_SIGNATURES)
10790 printf (" SIGNATURES");
10791 if (entry->d_un.d_val & VMS_LF_REL_SEG_OFF)
10792 printf (" REL_SEG_OFF");
10793 break;
10794
10795 default:
10796 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10797 break;
10798 }
10799 putchar ('\n');
10800 }
10801
10802 static bool
10803 get_32bit_dynamic_section (Filedata * filedata)
10804 {
10805 Elf32_External_Dyn * edyn;
10806 Elf32_External_Dyn * ext;
10807 Elf_Internal_Dyn * entry;
10808
10809 edyn = (Elf32_External_Dyn *) get_data (NULL, filedata,
10810 filedata->dynamic_addr, 1,
10811 filedata->dynamic_size,
10812 _("dynamic section"));
10813 if (!edyn)
10814 return false;
10815
10816 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
10817 might not have the luxury of section headers. Look for the DT_NULL
10818 terminator to determine the number of entries. */
10819 for (ext = edyn, filedata->dynamic_nent = 0;
10820 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
10821 ext++)
10822 {
10823 filedata->dynamic_nent++;
10824 if (BYTE_GET (ext->d_tag) == DT_NULL)
10825 break;
10826 }
10827
10828 filedata->dynamic_section
10829 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
10830 if (filedata->dynamic_section == NULL)
10831 {
10832 error (_("Out of memory allocating space for %lu dynamic entries\n"),
10833 (unsigned long) filedata->dynamic_nent);
10834 free (edyn);
10835 return false;
10836 }
10837
10838 for (ext = edyn, entry = filedata->dynamic_section;
10839 entry < filedata->dynamic_section + filedata->dynamic_nent;
10840 ext++, entry++)
10841 {
10842 entry->d_tag = BYTE_GET (ext->d_tag);
10843 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
10844 }
10845
10846 free (edyn);
10847
10848 return true;
10849 }
10850
10851 static bool
10852 get_64bit_dynamic_section (Filedata * filedata)
10853 {
10854 Elf64_External_Dyn * edyn;
10855 Elf64_External_Dyn * ext;
10856 Elf_Internal_Dyn * entry;
10857
10858 /* Read in the data. */
10859 edyn = (Elf64_External_Dyn *) get_data (NULL, filedata,
10860 filedata->dynamic_addr, 1,
10861 filedata->dynamic_size,
10862 _("dynamic section"));
10863 if (!edyn)
10864 return false;
10865
10866 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
10867 might not have the luxury of section headers. Look for the DT_NULL
10868 terminator to determine the number of entries. */
10869 for (ext = edyn, filedata->dynamic_nent = 0;
10870 /* PR 17533 file: 033-67080-0.004 - do not read past end of buffer. */
10871 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
10872 ext++)
10873 {
10874 filedata->dynamic_nent++;
10875 if (BYTE_GET (ext->d_tag) == DT_NULL)
10876 break;
10877 }
10878
10879 filedata->dynamic_section
10880 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
10881 if (filedata->dynamic_section == NULL)
10882 {
10883 error (_("Out of memory allocating space for %lu dynamic entries\n"),
10884 (unsigned long) filedata->dynamic_nent);
10885 free (edyn);
10886 return false;
10887 }
10888
10889 /* Convert from external to internal formats. */
10890 for (ext = edyn, entry = filedata->dynamic_section;
10891 entry < filedata->dynamic_section + filedata->dynamic_nent;
10892 ext++, entry++)
10893 {
10894 entry->d_tag = BYTE_GET (ext->d_tag);
10895 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
10896 }
10897
10898 free (edyn);
10899
10900 return true;
10901 }
10902
10903 static bool
10904 get_dynamic_section (Filedata *filedata)
10905 {
10906 if (filedata->dynamic_section)
10907 return true;
10908
10909 if (is_32bit_elf)
10910 return get_32bit_dynamic_section (filedata);
10911 else
10912 return get_64bit_dynamic_section (filedata);
10913 }
10914
10915 static void
10916 print_dynamic_flags (bfd_vma flags)
10917 {
10918 bool first = true;
10919
10920 while (flags)
10921 {
10922 bfd_vma flag;
10923
10924 flag = flags & - flags;
10925 flags &= ~ flag;
10926
10927 if (first)
10928 first = false;
10929 else
10930 putc (' ', stdout);
10931
10932 switch (flag)
10933 {
10934 case DF_ORIGIN: fputs ("ORIGIN", stdout); break;
10935 case DF_SYMBOLIC: fputs ("SYMBOLIC", stdout); break;
10936 case DF_TEXTREL: fputs ("TEXTREL", stdout); break;
10937 case DF_BIND_NOW: fputs ("BIND_NOW", stdout); break;
10938 case DF_STATIC_TLS: fputs ("STATIC_TLS", stdout); break;
10939 default: fputs (_("unknown"), stdout); break;
10940 }
10941 }
10942 puts ("");
10943 }
10944
10945 static bfd_vma *
10946 get_dynamic_data (Filedata * filedata, bfd_size_type number, unsigned int ent_size)
10947 {
10948 unsigned char * e_data;
10949 bfd_vma * i_data;
10950
10951 /* If the size_t type is smaller than the bfd_size_type, eg because
10952 you are building a 32-bit tool on a 64-bit host, then make sure
10953 that when (number) is cast to (size_t) no information is lost. */
10954 if (sizeof (size_t) < sizeof (bfd_size_type)
10955 && (bfd_size_type) ((size_t) number) != number)
10956 {
10957 error (_("Size truncation prevents reading %s elements of size %u\n"),
10958 bfd_vmatoa ("u", number), ent_size);
10959 return NULL;
10960 }
10961
10962 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
10963 attempting to allocate memory when the read is bound to fail. */
10964 if (ent_size * number > filedata->file_size)
10965 {
10966 error (_("Invalid number of dynamic entries: %s\n"),
10967 bfd_vmatoa ("u", number));
10968 return NULL;
10969 }
10970
10971 e_data = (unsigned char *) cmalloc ((size_t) number, ent_size);
10972 if (e_data == NULL)
10973 {
10974 error (_("Out of memory reading %s dynamic entries\n"),
10975 bfd_vmatoa ("u", number));
10976 return NULL;
10977 }
10978
10979 if (fread (e_data, ent_size, (size_t) number, filedata->handle) != number)
10980 {
10981 error (_("Unable to read in %s bytes of dynamic data\n"),
10982 bfd_vmatoa ("u", number * ent_size));
10983 free (e_data);
10984 return NULL;
10985 }
10986
10987 i_data = (bfd_vma *) cmalloc ((size_t) number, sizeof (*i_data));
10988 if (i_data == NULL)
10989 {
10990 error (_("Out of memory allocating space for %s dynamic entries\n"),
10991 bfd_vmatoa ("u", number));
10992 free (e_data);
10993 return NULL;
10994 }
10995
10996 while (number--)
10997 i_data[number] = byte_get (e_data + number * ent_size, ent_size);
10998
10999 free (e_data);
11000
11001 return i_data;
11002 }
11003
11004 static unsigned long
11005 get_num_dynamic_syms (Filedata * filedata)
11006 {
11007 unsigned long num_of_syms = 0;
11008
11009 if (!do_histogram && (!do_using_dynamic || do_dyn_syms))
11010 return num_of_syms;
11011
11012 if (filedata->dynamic_info[DT_HASH])
11013 {
11014 unsigned char nb[8];
11015 unsigned char nc[8];
11016 unsigned int hash_ent_size = 4;
11017
11018 if ((filedata->file_header.e_machine == EM_ALPHA
11019 || filedata->file_header.e_machine == EM_S390
11020 || filedata->file_header.e_machine == EM_S390_OLD)
11021 && filedata->file_header.e_ident[EI_CLASS] == ELFCLASS64)
11022 hash_ent_size = 8;
11023
11024 if (fseek (filedata->handle,
11025 (filedata->archive_file_offset
11026 + offset_from_vma (filedata, filedata->dynamic_info[DT_HASH],
11027 sizeof nb + sizeof nc)),
11028 SEEK_SET))
11029 {
11030 error (_("Unable to seek to start of dynamic information\n"));
11031 goto no_hash;
11032 }
11033
11034 if (fread (nb, hash_ent_size, 1, filedata->handle) != 1)
11035 {
11036 error (_("Failed to read in number of buckets\n"));
11037 goto no_hash;
11038 }
11039
11040 if (fread (nc, hash_ent_size, 1, filedata->handle) != 1)
11041 {
11042 error (_("Failed to read in number of chains\n"));
11043 goto no_hash;
11044 }
11045
11046 filedata->nbuckets = byte_get (nb, hash_ent_size);
11047 filedata->nchains = byte_get (nc, hash_ent_size);
11048
11049 if (filedata->nbuckets != 0 && filedata->nchains != 0)
11050 {
11051 filedata->buckets = get_dynamic_data (filedata, filedata->nbuckets,
11052 hash_ent_size);
11053 filedata->chains = get_dynamic_data (filedata, filedata->nchains,
11054 hash_ent_size);
11055
11056 if (filedata->buckets != NULL && filedata->chains != NULL)
11057 num_of_syms = filedata->nchains;
11058 }
11059 no_hash:
11060 if (num_of_syms == 0)
11061 {
11062 free (filedata->buckets);
11063 filedata->buckets = NULL;
11064 free (filedata->chains);
11065 filedata->chains = NULL;
11066 filedata->nbuckets = 0;
11067 }
11068 }
11069
11070 if (filedata->dynamic_info_DT_GNU_HASH)
11071 {
11072 unsigned char nb[16];
11073 bfd_vma i, maxchain = 0xffffffff, bitmaskwords;
11074 bfd_vma buckets_vma;
11075 unsigned long hn;
11076
11077 if (fseek (filedata->handle,
11078 (filedata->archive_file_offset
11079 + offset_from_vma (filedata,
11080 filedata->dynamic_info_DT_GNU_HASH,
11081 sizeof nb)),
11082 SEEK_SET))
11083 {
11084 error (_("Unable to seek to start of dynamic information\n"));
11085 goto no_gnu_hash;
11086 }
11087
11088 if (fread (nb, 16, 1, filedata->handle) != 1)
11089 {
11090 error (_("Failed to read in number of buckets\n"));
11091 goto no_gnu_hash;
11092 }
11093
11094 filedata->ngnubuckets = byte_get (nb, 4);
11095 filedata->gnusymidx = byte_get (nb + 4, 4);
11096 bitmaskwords = byte_get (nb + 8, 4);
11097 buckets_vma = filedata->dynamic_info_DT_GNU_HASH + 16;
11098 if (is_32bit_elf)
11099 buckets_vma += bitmaskwords * 4;
11100 else
11101 buckets_vma += bitmaskwords * 8;
11102
11103 if (fseek (filedata->handle,
11104 (filedata->archive_file_offset
11105 + offset_from_vma (filedata, buckets_vma, 4)),
11106 SEEK_SET))
11107 {
11108 error (_("Unable to seek to start of dynamic information\n"));
11109 goto no_gnu_hash;
11110 }
11111
11112 filedata->gnubuckets
11113 = get_dynamic_data (filedata, filedata->ngnubuckets, 4);
11114
11115 if (filedata->gnubuckets == NULL)
11116 goto no_gnu_hash;
11117
11118 for (i = 0; i < filedata->ngnubuckets; i++)
11119 if (filedata->gnubuckets[i] != 0)
11120 {
11121 if (filedata->gnubuckets[i] < filedata->gnusymidx)
11122 goto no_gnu_hash;
11123
11124 if (maxchain == 0xffffffff || filedata->gnubuckets[i] > maxchain)
11125 maxchain = filedata->gnubuckets[i];
11126 }
11127
11128 if (maxchain == 0xffffffff)
11129 goto no_gnu_hash;
11130
11131 maxchain -= filedata->gnusymidx;
11132
11133 if (fseek (filedata->handle,
11134 (filedata->archive_file_offset
11135 + offset_from_vma (filedata,
11136 buckets_vma + 4 * (filedata->ngnubuckets
11137 + maxchain),
11138 4)),
11139 SEEK_SET))
11140 {
11141 error (_("Unable to seek to start of dynamic information\n"));
11142 goto no_gnu_hash;
11143 }
11144
11145 do
11146 {
11147 if (fread (nb, 4, 1, filedata->handle) != 1)
11148 {
11149 error (_("Failed to determine last chain length\n"));
11150 goto no_gnu_hash;
11151 }
11152
11153 if (maxchain + 1 == 0)
11154 goto no_gnu_hash;
11155
11156 ++maxchain;
11157 }
11158 while ((byte_get (nb, 4) & 1) == 0);
11159
11160 if (fseek (filedata->handle,
11161 (filedata->archive_file_offset
11162 + offset_from_vma (filedata, (buckets_vma
11163 + 4 * filedata->ngnubuckets),
11164 4)),
11165 SEEK_SET))
11166 {
11167 error (_("Unable to seek to start of dynamic information\n"));
11168 goto no_gnu_hash;
11169 }
11170
11171 filedata->gnuchains = get_dynamic_data (filedata, maxchain, 4);
11172 filedata->ngnuchains = maxchain;
11173
11174 if (filedata->gnuchains == NULL)
11175 goto no_gnu_hash;
11176
11177 if (filedata->dynamic_info_DT_MIPS_XHASH)
11178 {
11179 if (fseek (filedata->handle,
11180 (filedata->archive_file_offset
11181 + offset_from_vma (filedata, (buckets_vma
11182 + 4 * (filedata->ngnubuckets
11183 + maxchain)), 4)),
11184 SEEK_SET))
11185 {
11186 error (_("Unable to seek to start of dynamic information\n"));
11187 goto no_gnu_hash;
11188 }
11189
11190 filedata->mipsxlat = get_dynamic_data (filedata, maxchain, 4);
11191 if (filedata->mipsxlat == NULL)
11192 goto no_gnu_hash;
11193 }
11194
11195 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
11196 if (filedata->gnubuckets[hn] != 0)
11197 {
11198 bfd_vma si = filedata->gnubuckets[hn];
11199 bfd_vma off = si - filedata->gnusymidx;
11200
11201 do
11202 {
11203 if (filedata->dynamic_info_DT_MIPS_XHASH)
11204 {
11205 if (off < filedata->ngnuchains
11206 && filedata->mipsxlat[off] >= num_of_syms)
11207 num_of_syms = filedata->mipsxlat[off] + 1;
11208 }
11209 else
11210 {
11211 if (si >= num_of_syms)
11212 num_of_syms = si + 1;
11213 }
11214 si++;
11215 }
11216 while (off < filedata->ngnuchains
11217 && (filedata->gnuchains[off++] & 1) == 0);
11218 }
11219
11220 if (num_of_syms == 0)
11221 {
11222 no_gnu_hash:
11223 free (filedata->mipsxlat);
11224 filedata->mipsxlat = NULL;
11225 free (filedata->gnuchains);
11226 filedata->gnuchains = NULL;
11227 free (filedata->gnubuckets);
11228 filedata->gnubuckets = NULL;
11229 filedata->ngnubuckets = 0;
11230 filedata->ngnuchains = 0;
11231 }
11232 }
11233
11234 return num_of_syms;
11235 }
11236
11237 /* Parse and display the contents of the dynamic section. */
11238
11239 static bool
11240 process_dynamic_section (Filedata * filedata)
11241 {
11242 Elf_Internal_Dyn * entry;
11243
11244 if (filedata->dynamic_size <= 1)
11245 {
11246 if (do_dynamic)
11247 {
11248 if (filedata->is_separate)
11249 printf (_("\nThere is no dynamic section in linked file '%s'.\n"),
11250 filedata->file_name);
11251 else
11252 printf (_("\nThere is no dynamic section in this file.\n"));
11253 }
11254
11255 return true;
11256 }
11257
11258 if (!get_dynamic_section (filedata))
11259 return false;
11260
11261 /* Find the appropriate symbol table. */
11262 if (filedata->dynamic_symbols == NULL || do_histogram)
11263 {
11264 unsigned long num_of_syms;
11265
11266 for (entry = filedata->dynamic_section;
11267 entry < filedata->dynamic_section + filedata->dynamic_nent;
11268 ++entry)
11269 if (entry->d_tag == DT_SYMTAB)
11270 filedata->dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
11271 else if (entry->d_tag == DT_SYMENT)
11272 filedata->dynamic_info[DT_SYMENT] = entry->d_un.d_val;
11273 else if (entry->d_tag == DT_HASH)
11274 filedata->dynamic_info[DT_HASH] = entry->d_un.d_val;
11275 else if (entry->d_tag == DT_GNU_HASH)
11276 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11277 else if ((filedata->file_header.e_machine == EM_MIPS
11278 || filedata->file_header.e_machine == EM_MIPS_RS3_LE)
11279 && entry->d_tag == DT_MIPS_XHASH)
11280 {
11281 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
11282 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11283 }
11284
11285 num_of_syms = get_num_dynamic_syms (filedata);
11286
11287 if (num_of_syms != 0
11288 && filedata->dynamic_symbols == NULL
11289 && filedata->dynamic_info[DT_SYMTAB]
11290 && filedata->dynamic_info[DT_SYMENT])
11291 {
11292 Elf_Internal_Phdr *seg;
11293 bfd_vma vma = filedata->dynamic_info[DT_SYMTAB];
11294
11295 if (! get_program_headers (filedata))
11296 {
11297 error (_("Cannot interpret virtual addresses "
11298 "without program headers.\n"));
11299 return false;
11300 }
11301
11302 for (seg = filedata->program_headers;
11303 seg < filedata->program_headers + filedata->file_header.e_phnum;
11304 ++seg)
11305 {
11306 if (seg->p_type != PT_LOAD)
11307 continue;
11308
11309 if (seg->p_offset + seg->p_filesz > filedata->file_size)
11310 {
11311 /* See PR 21379 for a reproducer. */
11312 error (_("Invalid PT_LOAD entry\n"));
11313 return false;
11314 }
11315
11316 if (vma >= (seg->p_vaddr & -seg->p_align)
11317 && vma < seg->p_vaddr + seg->p_filesz)
11318 {
11319 /* Since we do not know how big the symbol table is,
11320 we default to reading in up to the end of PT_LOAD
11321 segment and processing that. This is overkill, I
11322 know, but it should work. */
11323 Elf_Internal_Shdr section;
11324 section.sh_offset = (vma - seg->p_vaddr
11325 + seg->p_offset);
11326 section.sh_size = (num_of_syms
11327 * filedata->dynamic_info[DT_SYMENT]);
11328 section.sh_entsize = filedata->dynamic_info[DT_SYMENT];
11329
11330 if (do_checks
11331 && filedata->dynamic_symtab_section != NULL
11332 && ((filedata->dynamic_symtab_section->sh_offset
11333 != section.sh_offset)
11334 || (filedata->dynamic_symtab_section->sh_size
11335 != section.sh_size)
11336 || (filedata->dynamic_symtab_section->sh_entsize
11337 != section.sh_entsize)))
11338 warn (_("\
11339 the .dynsym section doesn't match the DT_SYMTAB and DT_SYMENT tags\n"));
11340
11341 section.sh_name = filedata->string_table_length;
11342 filedata->dynamic_symbols
11343 = get_elf_symbols (filedata, &section,
11344 &filedata->num_dynamic_syms);
11345 if (filedata->dynamic_symbols == NULL
11346 || filedata->num_dynamic_syms != num_of_syms)
11347 {
11348 error (_("Corrupt DT_SYMTAB dynamic entry\n"));
11349 return false;
11350 }
11351 break;
11352 }
11353 }
11354 }
11355 }
11356
11357 /* Similarly find a string table. */
11358 if (filedata->dynamic_strings == NULL)
11359 for (entry = filedata->dynamic_section;
11360 entry < filedata->dynamic_section + filedata->dynamic_nent;
11361 ++entry)
11362 {
11363 if (entry->d_tag == DT_STRTAB)
11364 filedata->dynamic_info[DT_STRTAB] = entry->d_un.d_val;
11365
11366 if (entry->d_tag == DT_STRSZ)
11367 filedata->dynamic_info[DT_STRSZ] = entry->d_un.d_val;
11368
11369 if (filedata->dynamic_info[DT_STRTAB]
11370 && filedata->dynamic_info[DT_STRSZ])
11371 {
11372 unsigned long offset;
11373 bfd_size_type str_tab_len = filedata->dynamic_info[DT_STRSZ];
11374
11375 offset = offset_from_vma (filedata,
11376 filedata->dynamic_info[DT_STRTAB],
11377 str_tab_len);
11378 if (do_checks
11379 && filedata->dynamic_strtab_section
11380 && ((filedata->dynamic_strtab_section->sh_offset
11381 != (file_ptr) offset)
11382 || (filedata->dynamic_strtab_section->sh_size
11383 != str_tab_len)))
11384 warn (_("\
11385 the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
11386
11387 filedata->dynamic_strings
11388 = (char *) get_data (NULL, filedata, offset, 1, str_tab_len,
11389 _("dynamic string table"));
11390 if (filedata->dynamic_strings == NULL)
11391 {
11392 error (_("Corrupt DT_STRTAB dynamic entry\n"));
11393 break;
11394 }
11395
11396 filedata->dynamic_strings_length = str_tab_len;
11397 break;
11398 }
11399 }
11400
11401 /* And find the syminfo section if available. */
11402 if (filedata->dynamic_syminfo == NULL)
11403 {
11404 unsigned long syminsz = 0;
11405
11406 for (entry = filedata->dynamic_section;
11407 entry < filedata->dynamic_section + filedata->dynamic_nent;
11408 ++entry)
11409 {
11410 if (entry->d_tag == DT_SYMINENT)
11411 {
11412 /* Note: these braces are necessary to avoid a syntax
11413 error from the SunOS4 C compiler. */
11414 /* PR binutils/17531: A corrupt file can trigger this test.
11415 So do not use an assert, instead generate an error message. */
11416 if (sizeof (Elf_External_Syminfo) != entry->d_un.d_val)
11417 error (_("Bad value (%d) for SYMINENT entry\n"),
11418 (int) entry->d_un.d_val);
11419 }
11420 else if (entry->d_tag == DT_SYMINSZ)
11421 syminsz = entry->d_un.d_val;
11422 else if (entry->d_tag == DT_SYMINFO)
11423 filedata->dynamic_syminfo_offset
11424 = offset_from_vma (filedata, entry->d_un.d_val, syminsz);
11425 }
11426
11427 if (filedata->dynamic_syminfo_offset != 0 && syminsz != 0)
11428 {
11429 Elf_External_Syminfo * extsyminfo;
11430 Elf_External_Syminfo * extsym;
11431 Elf_Internal_Syminfo * syminfo;
11432
11433 /* There is a syminfo section. Read the data. */
11434 extsyminfo = (Elf_External_Syminfo *)
11435 get_data (NULL, filedata, filedata->dynamic_syminfo_offset,
11436 1, syminsz, _("symbol information"));
11437 if (!extsyminfo)
11438 return false;
11439
11440 if (filedata->dynamic_syminfo != NULL)
11441 {
11442 error (_("Multiple dynamic symbol information sections found\n"));
11443 free (filedata->dynamic_syminfo);
11444 }
11445 filedata->dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
11446 if (filedata->dynamic_syminfo == NULL)
11447 {
11448 error (_("Out of memory allocating %lu bytes "
11449 "for dynamic symbol info\n"),
11450 (unsigned long) syminsz);
11451 return false;
11452 }
11453
11454 filedata->dynamic_syminfo_nent
11455 = syminsz / sizeof (Elf_External_Syminfo);
11456 for (syminfo = filedata->dynamic_syminfo, extsym = extsyminfo;
11457 syminfo < (filedata->dynamic_syminfo
11458 + filedata->dynamic_syminfo_nent);
11459 ++syminfo, ++extsym)
11460 {
11461 syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
11462 syminfo->si_flags = BYTE_GET (extsym->si_flags);
11463 }
11464
11465 free (extsyminfo);
11466 }
11467 }
11468
11469 if (do_dynamic && filedata->dynamic_addr)
11470 {
11471 if (filedata->is_separate)
11472 printf (ngettext ("\nIn linked file '%s' the dynamic section at offset 0x%lx contains %lu entry:\n",
11473 "\nIn linked file '%s' the dynamic section at offset 0x%lx contains %lu entries:\n",
11474 (unsigned long) filedata->dynamic_nent),
11475 filedata->file_name,
11476 filedata->dynamic_addr,
11477 (unsigned long) filedata->dynamic_nent);
11478 else
11479 printf (ngettext ("\nDynamic section at offset 0x%lx contains %lu entry:\n",
11480 "\nDynamic section at offset 0x%lx contains %lu entries:\n",
11481 (unsigned long) filedata->dynamic_nent),
11482 filedata->dynamic_addr,
11483 (unsigned long) filedata->dynamic_nent);
11484 }
11485 if (do_dynamic)
11486 printf (_(" Tag Type Name/Value\n"));
11487
11488 for (entry = filedata->dynamic_section;
11489 entry < filedata->dynamic_section + filedata->dynamic_nent;
11490 entry++)
11491 {
11492 if (do_dynamic)
11493 {
11494 const char * dtype;
11495
11496 putchar (' ');
11497 print_vma (entry->d_tag, FULL_HEX);
11498 dtype = get_dynamic_type (filedata, entry->d_tag);
11499 printf (" (%s)%*s", dtype,
11500 ((is_32bit_elf ? 27 : 19) - (int) strlen (dtype)), " ");
11501 }
11502
11503 switch (entry->d_tag)
11504 {
11505 case DT_FLAGS:
11506 if (do_dynamic)
11507 print_dynamic_flags (entry->d_un.d_val);
11508 break;
11509
11510 case DT_AUXILIARY:
11511 case DT_FILTER:
11512 case DT_CONFIG:
11513 case DT_DEPAUDIT:
11514 case DT_AUDIT:
11515 if (do_dynamic)
11516 {
11517 switch (entry->d_tag)
11518 {
11519 case DT_AUXILIARY:
11520 printf (_("Auxiliary library"));
11521 break;
11522
11523 case DT_FILTER:
11524 printf (_("Filter library"));
11525 break;
11526
11527 case DT_CONFIG:
11528 printf (_("Configuration file"));
11529 break;
11530
11531 case DT_DEPAUDIT:
11532 printf (_("Dependency audit library"));
11533 break;
11534
11535 case DT_AUDIT:
11536 printf (_("Audit library"));
11537 break;
11538 }
11539
11540 if (valid_dynamic_name (filedata, entry->d_un.d_val))
11541 printf (": [%s]\n",
11542 get_dynamic_name (filedata, entry->d_un.d_val));
11543 else
11544 {
11545 printf (": ");
11546 print_vma (entry->d_un.d_val, PREFIX_HEX);
11547 putchar ('\n');
11548 }
11549 }
11550 break;
11551
11552 case DT_FEATURE:
11553 if (do_dynamic)
11554 {
11555 printf (_("Flags:"));
11556
11557 if (entry->d_un.d_val == 0)
11558 printf (_(" None\n"));
11559 else
11560 {
11561 unsigned long int val = entry->d_un.d_val;
11562
11563 if (val & DTF_1_PARINIT)
11564 {
11565 printf (" PARINIT");
11566 val ^= DTF_1_PARINIT;
11567 }
11568 if (val & DTF_1_CONFEXP)
11569 {
11570 printf (" CONFEXP");
11571 val ^= DTF_1_CONFEXP;
11572 }
11573 if (val != 0)
11574 printf (" %lx", val);
11575 puts ("");
11576 }
11577 }
11578 break;
11579
11580 case DT_POSFLAG_1:
11581 if (do_dynamic)
11582 {
11583 printf (_("Flags:"));
11584
11585 if (entry->d_un.d_val == 0)
11586 printf (_(" None\n"));
11587 else
11588 {
11589 unsigned long int val = entry->d_un.d_val;
11590
11591 if (val & DF_P1_LAZYLOAD)
11592 {
11593 printf (" LAZYLOAD");
11594 val ^= DF_P1_LAZYLOAD;
11595 }
11596 if (val & DF_P1_GROUPPERM)
11597 {
11598 printf (" GROUPPERM");
11599 val ^= DF_P1_GROUPPERM;
11600 }
11601 if (val != 0)
11602 printf (" %lx", val);
11603 puts ("");
11604 }
11605 }
11606 break;
11607
11608 case DT_FLAGS_1:
11609 if (do_dynamic)
11610 {
11611 printf (_("Flags:"));
11612 if (entry->d_un.d_val == 0)
11613 printf (_(" None\n"));
11614 else
11615 {
11616 unsigned long int val = entry->d_un.d_val;
11617
11618 if (val & DF_1_NOW)
11619 {
11620 printf (" NOW");
11621 val ^= DF_1_NOW;
11622 }
11623 if (val & DF_1_GLOBAL)
11624 {
11625 printf (" GLOBAL");
11626 val ^= DF_1_GLOBAL;
11627 }
11628 if (val & DF_1_GROUP)
11629 {
11630 printf (" GROUP");
11631 val ^= DF_1_GROUP;
11632 }
11633 if (val & DF_1_NODELETE)
11634 {
11635 printf (" NODELETE");
11636 val ^= DF_1_NODELETE;
11637 }
11638 if (val & DF_1_LOADFLTR)
11639 {
11640 printf (" LOADFLTR");
11641 val ^= DF_1_LOADFLTR;
11642 }
11643 if (val & DF_1_INITFIRST)
11644 {
11645 printf (" INITFIRST");
11646 val ^= DF_1_INITFIRST;
11647 }
11648 if (val & DF_1_NOOPEN)
11649 {
11650 printf (" NOOPEN");
11651 val ^= DF_1_NOOPEN;
11652 }
11653 if (val & DF_1_ORIGIN)
11654 {
11655 printf (" ORIGIN");
11656 val ^= DF_1_ORIGIN;
11657 }
11658 if (val & DF_1_DIRECT)
11659 {
11660 printf (" DIRECT");
11661 val ^= DF_1_DIRECT;
11662 }
11663 if (val & DF_1_TRANS)
11664 {
11665 printf (" TRANS");
11666 val ^= DF_1_TRANS;
11667 }
11668 if (val & DF_1_INTERPOSE)
11669 {
11670 printf (" INTERPOSE");
11671 val ^= DF_1_INTERPOSE;
11672 }
11673 if (val & DF_1_NODEFLIB)
11674 {
11675 printf (" NODEFLIB");
11676 val ^= DF_1_NODEFLIB;
11677 }
11678 if (val & DF_1_NODUMP)
11679 {
11680 printf (" NODUMP");
11681 val ^= DF_1_NODUMP;
11682 }
11683 if (val & DF_1_CONFALT)
11684 {
11685 printf (" CONFALT");
11686 val ^= DF_1_CONFALT;
11687 }
11688 if (val & DF_1_ENDFILTEE)
11689 {
11690 printf (" ENDFILTEE");
11691 val ^= DF_1_ENDFILTEE;
11692 }
11693 if (val & DF_1_DISPRELDNE)
11694 {
11695 printf (" DISPRELDNE");
11696 val ^= DF_1_DISPRELDNE;
11697 }
11698 if (val & DF_1_DISPRELPND)
11699 {
11700 printf (" DISPRELPND");
11701 val ^= DF_1_DISPRELPND;
11702 }
11703 if (val & DF_1_NODIRECT)
11704 {
11705 printf (" NODIRECT");
11706 val ^= DF_1_NODIRECT;
11707 }
11708 if (val & DF_1_IGNMULDEF)
11709 {
11710 printf (" IGNMULDEF");
11711 val ^= DF_1_IGNMULDEF;
11712 }
11713 if (val & DF_1_NOKSYMS)
11714 {
11715 printf (" NOKSYMS");
11716 val ^= DF_1_NOKSYMS;
11717 }
11718 if (val & DF_1_NOHDR)
11719 {
11720 printf (" NOHDR");
11721 val ^= DF_1_NOHDR;
11722 }
11723 if (val & DF_1_EDITED)
11724 {
11725 printf (" EDITED");
11726 val ^= DF_1_EDITED;
11727 }
11728 if (val & DF_1_NORELOC)
11729 {
11730 printf (" NORELOC");
11731 val ^= DF_1_NORELOC;
11732 }
11733 if (val & DF_1_SYMINTPOSE)
11734 {
11735 printf (" SYMINTPOSE");
11736 val ^= DF_1_SYMINTPOSE;
11737 }
11738 if (val & DF_1_GLOBAUDIT)
11739 {
11740 printf (" GLOBAUDIT");
11741 val ^= DF_1_GLOBAUDIT;
11742 }
11743 if (val & DF_1_SINGLETON)
11744 {
11745 printf (" SINGLETON");
11746 val ^= DF_1_SINGLETON;
11747 }
11748 if (val & DF_1_STUB)
11749 {
11750 printf (" STUB");
11751 val ^= DF_1_STUB;
11752 }
11753 if (val & DF_1_PIE)
11754 {
11755 printf (" PIE");
11756 val ^= DF_1_PIE;
11757 }
11758 if (val & DF_1_KMOD)
11759 {
11760 printf (" KMOD");
11761 val ^= DF_1_KMOD;
11762 }
11763 if (val & DF_1_WEAKFILTER)
11764 {
11765 printf (" WEAKFILTER");
11766 val ^= DF_1_WEAKFILTER;
11767 }
11768 if (val & DF_1_NOCOMMON)
11769 {
11770 printf (" NOCOMMON");
11771 val ^= DF_1_NOCOMMON;
11772 }
11773 if (val != 0)
11774 printf (" %lx", val);
11775 puts ("");
11776 }
11777 }
11778 break;
11779
11780 case DT_PLTREL:
11781 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11782 if (do_dynamic)
11783 puts (get_dynamic_type (filedata, entry->d_un.d_val));
11784 break;
11785
11786 case DT_NULL :
11787 case DT_NEEDED :
11788 case DT_PLTGOT :
11789 case DT_HASH :
11790 case DT_STRTAB :
11791 case DT_SYMTAB :
11792 case DT_RELA :
11793 case DT_INIT :
11794 case DT_FINI :
11795 case DT_SONAME :
11796 case DT_RPATH :
11797 case DT_SYMBOLIC:
11798 case DT_REL :
11799 case DT_RELR :
11800 case DT_DEBUG :
11801 case DT_TEXTREL :
11802 case DT_JMPREL :
11803 case DT_RUNPATH :
11804 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11805
11806 if (do_dynamic)
11807 {
11808 const char *name;
11809
11810 if (valid_dynamic_name (filedata, entry->d_un.d_val))
11811 name = get_dynamic_name (filedata, entry->d_un.d_val);
11812 else
11813 name = NULL;
11814
11815 if (name)
11816 {
11817 switch (entry->d_tag)
11818 {
11819 case DT_NEEDED:
11820 printf (_("Shared library: [%s]"), name);
11821
11822 if (filedata->program_interpreter
11823 && streq (name, filedata->program_interpreter))
11824 printf (_(" program interpreter"));
11825 break;
11826
11827 case DT_SONAME:
11828 printf (_("Library soname: [%s]"), name);
11829 break;
11830
11831 case DT_RPATH:
11832 printf (_("Library rpath: [%s]"), name);
11833 break;
11834
11835 case DT_RUNPATH:
11836 printf (_("Library runpath: [%s]"), name);
11837 break;
11838
11839 default:
11840 print_vma (entry->d_un.d_val, PREFIX_HEX);
11841 break;
11842 }
11843 }
11844 else
11845 print_vma (entry->d_un.d_val, PREFIX_HEX);
11846
11847 putchar ('\n');
11848 }
11849 break;
11850
11851 case DT_PLTRELSZ:
11852 case DT_RELASZ :
11853 case DT_STRSZ :
11854 case DT_RELSZ :
11855 case DT_RELAENT :
11856 case DT_RELRENT :
11857 case DT_RELRSZ :
11858 case DT_SYMENT :
11859 case DT_RELENT :
11860 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11861 /* Fall through. */
11862 case DT_PLTPADSZ:
11863 case DT_MOVEENT :
11864 case DT_MOVESZ :
11865 case DT_PREINIT_ARRAYSZ:
11866 case DT_INIT_ARRAYSZ:
11867 case DT_FINI_ARRAYSZ:
11868 case DT_GNU_CONFLICTSZ:
11869 case DT_GNU_LIBLISTSZ:
11870 if (do_dynamic)
11871 {
11872 print_vma (entry->d_un.d_val, UNSIGNED);
11873 printf (_(" (bytes)\n"));
11874 }
11875 break;
11876
11877 case DT_VERDEFNUM:
11878 case DT_VERNEEDNUM:
11879 case DT_RELACOUNT:
11880 case DT_RELCOUNT:
11881 if (do_dynamic)
11882 {
11883 print_vma (entry->d_un.d_val, UNSIGNED);
11884 putchar ('\n');
11885 }
11886 break;
11887
11888 case DT_SYMINSZ:
11889 case DT_SYMINENT:
11890 case DT_SYMINFO:
11891 case DT_USED:
11892 case DT_INIT_ARRAY:
11893 case DT_FINI_ARRAY:
11894 if (do_dynamic)
11895 {
11896 if (entry->d_tag == DT_USED
11897 && valid_dynamic_name (filedata, entry->d_un.d_val))
11898 {
11899 const char *name
11900 = get_dynamic_name (filedata, entry->d_un.d_val);
11901
11902 if (*name)
11903 {
11904 printf (_("Not needed object: [%s]\n"), name);
11905 break;
11906 }
11907 }
11908
11909 print_vma (entry->d_un.d_val, PREFIX_HEX);
11910 putchar ('\n');
11911 }
11912 break;
11913
11914 case DT_BIND_NOW:
11915 /* The value of this entry is ignored. */
11916 if (do_dynamic)
11917 putchar ('\n');
11918 break;
11919
11920 case DT_GNU_PRELINKED:
11921 if (do_dynamic)
11922 {
11923 struct tm * tmp;
11924 time_t atime = entry->d_un.d_val;
11925
11926 tmp = gmtime (&atime);
11927 /* PR 17533 file: 041-1244816-0.004. */
11928 if (tmp == NULL)
11929 printf (_("<corrupt time val: %lx"),
11930 (unsigned long) atime);
11931 else
11932 printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
11933 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
11934 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
11935
11936 }
11937 break;
11938
11939 case DT_GNU_HASH:
11940 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11941 if (do_dynamic)
11942 {
11943 print_vma (entry->d_un.d_val, PREFIX_HEX);
11944 putchar ('\n');
11945 }
11946 break;
11947
11948 case DT_GNU_FLAGS_1:
11949 if (do_dynamic)
11950 {
11951 printf (_("Flags:"));
11952 if (entry->d_un.d_val == 0)
11953 printf (_(" None\n"));
11954 else
11955 {
11956 unsigned long int val = entry->d_un.d_val;
11957
11958 if (val & DF_GNU_1_UNIQUE)
11959 {
11960 printf (" UNIQUE");
11961 val ^= DF_GNU_1_UNIQUE;
11962 }
11963 if (val != 0)
11964 printf (" %lx", val);
11965 puts ("");
11966 }
11967 }
11968 break;
11969
11970 default:
11971 if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
11972 filedata->version_info[DT_VERSIONTAGIDX (entry->d_tag)]
11973 = entry->d_un.d_val;
11974
11975 if (do_dynamic)
11976 {
11977 switch (filedata->file_header.e_machine)
11978 {
11979 case EM_AARCH64:
11980 dynamic_section_aarch64_val (entry);
11981 break;
11982 case EM_MIPS:
11983 case EM_MIPS_RS3_LE:
11984 dynamic_section_mips_val (filedata, entry);
11985 break;
11986 case EM_PARISC:
11987 dynamic_section_parisc_val (entry);
11988 break;
11989 case EM_IA_64:
11990 dynamic_section_ia64_val (entry);
11991 break;
11992 default:
11993 print_vma (entry->d_un.d_val, PREFIX_HEX);
11994 putchar ('\n');
11995 }
11996 }
11997 break;
11998 }
11999 }
12000
12001 return true;
12002 }
12003
12004 static char *
12005 get_ver_flags (unsigned int flags)
12006 {
12007 static char buff[128];
12008
12009 buff[0] = 0;
12010
12011 if (flags == 0)
12012 return _("none");
12013
12014 if (flags & VER_FLG_BASE)
12015 strcat (buff, "BASE");
12016
12017 if (flags & VER_FLG_WEAK)
12018 {
12019 if (flags & VER_FLG_BASE)
12020 strcat (buff, " | ");
12021
12022 strcat (buff, "WEAK");
12023 }
12024
12025 if (flags & VER_FLG_INFO)
12026 {
12027 if (flags & (VER_FLG_BASE|VER_FLG_WEAK))
12028 strcat (buff, " | ");
12029
12030 strcat (buff, "INFO");
12031 }
12032
12033 if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
12034 {
12035 if (flags & (VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
12036 strcat (buff, " | ");
12037
12038 strcat (buff, _("<unknown>"));
12039 }
12040
12041 return buff;
12042 }
12043
12044 /* Display the contents of the version sections. */
12045
12046 static bool
12047 process_version_sections (Filedata * filedata)
12048 {
12049 Elf_Internal_Shdr * section;
12050 unsigned i;
12051 bool found = false;
12052
12053 if (! do_version)
12054 return true;
12055
12056 for (i = 0, section = filedata->section_headers;
12057 i < filedata->file_header.e_shnum;
12058 i++, section++)
12059 {
12060 switch (section->sh_type)
12061 {
12062 case SHT_GNU_verdef:
12063 {
12064 Elf_External_Verdef * edefs;
12065 unsigned long idx;
12066 unsigned long cnt;
12067 char * endbuf;
12068
12069 found = true;
12070
12071 if (filedata->is_separate)
12072 printf (ngettext ("\nIn linked file '%s' the version definition section '%s' contains %u entry:\n",
12073 "\nIn linked file '%s' the version definition section '%s' contains %u entries:\n",
12074 section->sh_info),
12075 filedata->file_name,
12076 printable_section_name (filedata, section),
12077 section->sh_info);
12078 else
12079 printf (ngettext ("\nVersion definition section '%s' "
12080 "contains %u entry:\n",
12081 "\nVersion definition section '%s' "
12082 "contains %u entries:\n",
12083 section->sh_info),
12084 printable_section_name (filedata, section),
12085 section->sh_info);
12086
12087 printf (_(" Addr: 0x"));
12088 printf_vma (section->sh_addr);
12089 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
12090 (unsigned long) section->sh_offset, section->sh_link,
12091 printable_section_name_from_index (filedata, section->sh_link));
12092
12093 edefs = (Elf_External_Verdef *)
12094 get_data (NULL, filedata, section->sh_offset, 1,section->sh_size,
12095 _("version definition section"));
12096 if (!edefs)
12097 break;
12098 endbuf = (char *) edefs + section->sh_size;
12099
12100 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
12101 {
12102 char * vstart;
12103 Elf_External_Verdef * edef;
12104 Elf_Internal_Verdef ent;
12105 Elf_External_Verdaux * eaux;
12106 Elf_Internal_Verdaux aux;
12107 unsigned long isum;
12108 int j;
12109
12110 vstart = ((char *) edefs) + idx;
12111 if (vstart + sizeof (*edef) > endbuf)
12112 break;
12113
12114 edef = (Elf_External_Verdef *) vstart;
12115
12116 ent.vd_version = BYTE_GET (edef->vd_version);
12117 ent.vd_flags = BYTE_GET (edef->vd_flags);
12118 ent.vd_ndx = BYTE_GET (edef->vd_ndx);
12119 ent.vd_cnt = BYTE_GET (edef->vd_cnt);
12120 ent.vd_hash = BYTE_GET (edef->vd_hash);
12121 ent.vd_aux = BYTE_GET (edef->vd_aux);
12122 ent.vd_next = BYTE_GET (edef->vd_next);
12123
12124 printf (_(" %#06lx: Rev: %d Flags: %s"),
12125 idx, ent.vd_version, get_ver_flags (ent.vd_flags));
12126
12127 printf (_(" Index: %d Cnt: %d "),
12128 ent.vd_ndx, ent.vd_cnt);
12129
12130 /* Check for overflow. */
12131 if (ent.vd_aux > (size_t) (endbuf - vstart))
12132 break;
12133
12134 vstart += ent.vd_aux;
12135
12136 if (vstart + sizeof (*eaux) > endbuf)
12137 break;
12138 eaux = (Elf_External_Verdaux *) vstart;
12139
12140 aux.vda_name = BYTE_GET (eaux->vda_name);
12141 aux.vda_next = BYTE_GET (eaux->vda_next);
12142
12143 if (valid_dynamic_name (filedata, aux.vda_name))
12144 printf (_("Name: %s\n"),
12145 get_dynamic_name (filedata, aux.vda_name));
12146 else
12147 printf (_("Name index: %ld\n"), aux.vda_name);
12148
12149 isum = idx + ent.vd_aux;
12150
12151 for (j = 1; j < ent.vd_cnt; j++)
12152 {
12153 if (aux.vda_next < sizeof (*eaux)
12154 && !(j == ent.vd_cnt - 1 && aux.vda_next == 0))
12155 {
12156 warn (_("Invalid vda_next field of %lx\n"),
12157 aux.vda_next);
12158 j = ent.vd_cnt;
12159 break;
12160 }
12161 /* Check for overflow. */
12162 if (aux.vda_next > (size_t) (endbuf - vstart))
12163 break;
12164
12165 isum += aux.vda_next;
12166 vstart += aux.vda_next;
12167
12168 if (vstart + sizeof (*eaux) > endbuf)
12169 break;
12170 eaux = (Elf_External_Verdaux *) vstart;
12171
12172 aux.vda_name = BYTE_GET (eaux->vda_name);
12173 aux.vda_next = BYTE_GET (eaux->vda_next);
12174
12175 if (valid_dynamic_name (filedata, aux.vda_name))
12176 printf (_(" %#06lx: Parent %d: %s\n"),
12177 isum, j,
12178 get_dynamic_name (filedata, aux.vda_name));
12179 else
12180 printf (_(" %#06lx: Parent %d, name index: %ld\n"),
12181 isum, j, aux.vda_name);
12182 }
12183
12184 if (j < ent.vd_cnt)
12185 printf (_(" Version def aux past end of section\n"));
12186
12187 /* PR 17531:
12188 file: id:000001,src:000172+005151,op:splice,rep:2. */
12189 if (ent.vd_next < sizeof (*edef)
12190 && !(cnt == section->sh_info - 1 && ent.vd_next == 0))
12191 {
12192 warn (_("Invalid vd_next field of %lx\n"), ent.vd_next);
12193 cnt = section->sh_info;
12194 break;
12195 }
12196 if (ent.vd_next > (size_t) (endbuf - ((char *) edefs + idx)))
12197 break;
12198
12199 idx += ent.vd_next;
12200 }
12201
12202 if (cnt < section->sh_info)
12203 printf (_(" Version definition past end of section\n"));
12204
12205 free (edefs);
12206 }
12207 break;
12208
12209 case SHT_GNU_verneed:
12210 {
12211 Elf_External_Verneed * eneed;
12212 unsigned long idx;
12213 unsigned long cnt;
12214 char * endbuf;
12215
12216 found = true;
12217
12218 if (filedata->is_separate)
12219 printf (ngettext ("\nIn linked file '%s' the version needs section '%s' contains %u entry:\n",
12220 "\nIn linked file '%s' the version needs section '%s' contains %u entries:\n",
12221 section->sh_info),
12222 filedata->file_name,
12223 printable_section_name (filedata, section),
12224 section->sh_info);
12225 else
12226 printf (ngettext ("\nVersion needs section '%s' "
12227 "contains %u entry:\n",
12228 "\nVersion needs section '%s' "
12229 "contains %u entries:\n",
12230 section->sh_info),
12231 printable_section_name (filedata, section),
12232 section->sh_info);
12233
12234 printf (_(" Addr: 0x"));
12235 printf_vma (section->sh_addr);
12236 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
12237 (unsigned long) section->sh_offset, section->sh_link,
12238 printable_section_name_from_index (filedata, section->sh_link));
12239
12240 eneed = (Elf_External_Verneed *) get_data (NULL, filedata,
12241 section->sh_offset, 1,
12242 section->sh_size,
12243 _("Version Needs section"));
12244 if (!eneed)
12245 break;
12246 endbuf = (char *) eneed + section->sh_size;
12247
12248 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
12249 {
12250 Elf_External_Verneed * entry;
12251 Elf_Internal_Verneed ent;
12252 unsigned long isum;
12253 int j;
12254 char * vstart;
12255
12256 vstart = ((char *) eneed) + idx;
12257 if (vstart + sizeof (*entry) > endbuf)
12258 break;
12259
12260 entry = (Elf_External_Verneed *) vstart;
12261
12262 ent.vn_version = BYTE_GET (entry->vn_version);
12263 ent.vn_cnt = BYTE_GET (entry->vn_cnt);
12264 ent.vn_file = BYTE_GET (entry->vn_file);
12265 ent.vn_aux = BYTE_GET (entry->vn_aux);
12266 ent.vn_next = BYTE_GET (entry->vn_next);
12267
12268 printf (_(" %#06lx: Version: %d"), idx, ent.vn_version);
12269
12270 if (valid_dynamic_name (filedata, ent.vn_file))
12271 printf (_(" File: %s"),
12272 get_dynamic_name (filedata, ent.vn_file));
12273 else
12274 printf (_(" File: %lx"), ent.vn_file);
12275
12276 printf (_(" Cnt: %d\n"), ent.vn_cnt);
12277
12278 /* Check for overflow. */
12279 if (ent.vn_aux > (size_t) (endbuf - vstart))
12280 break;
12281 vstart += ent.vn_aux;
12282
12283 for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
12284 {
12285 Elf_External_Vernaux * eaux;
12286 Elf_Internal_Vernaux aux;
12287
12288 if (vstart + sizeof (*eaux) > endbuf)
12289 break;
12290 eaux = (Elf_External_Vernaux *) vstart;
12291
12292 aux.vna_hash = BYTE_GET (eaux->vna_hash);
12293 aux.vna_flags = BYTE_GET (eaux->vna_flags);
12294 aux.vna_other = BYTE_GET (eaux->vna_other);
12295 aux.vna_name = BYTE_GET (eaux->vna_name);
12296 aux.vna_next = BYTE_GET (eaux->vna_next);
12297
12298 if (valid_dynamic_name (filedata, aux.vna_name))
12299 printf (_(" %#06lx: Name: %s"),
12300 isum, get_dynamic_name (filedata, aux.vna_name));
12301 else
12302 printf (_(" %#06lx: Name index: %lx"),
12303 isum, aux.vna_name);
12304
12305 printf (_(" Flags: %s Version: %d\n"),
12306 get_ver_flags (aux.vna_flags), aux.vna_other);
12307
12308 if (aux.vna_next < sizeof (*eaux)
12309 && !(j == ent.vn_cnt - 1 && aux.vna_next == 0))
12310 {
12311 warn (_("Invalid vna_next field of %lx\n"),
12312 aux.vna_next);
12313 j = ent.vn_cnt;
12314 break;
12315 }
12316 /* Check for overflow. */
12317 if (aux.vna_next > (size_t) (endbuf - vstart))
12318 break;
12319 isum += aux.vna_next;
12320 vstart += aux.vna_next;
12321 }
12322
12323 if (j < ent.vn_cnt)
12324 warn (_("Missing Version Needs auxiliary information\n"));
12325
12326 if (ent.vn_next < sizeof (*entry)
12327 && !(cnt == section->sh_info - 1 && ent.vn_next == 0))
12328 {
12329 warn (_("Invalid vn_next field of %lx\n"), ent.vn_next);
12330 cnt = section->sh_info;
12331 break;
12332 }
12333 if (ent.vn_next > (size_t) (endbuf - ((char *) eneed + idx)))
12334 break;
12335 idx += ent.vn_next;
12336 }
12337
12338 if (cnt < section->sh_info)
12339 warn (_("Missing Version Needs information\n"));
12340
12341 free (eneed);
12342 }
12343 break;
12344
12345 case SHT_GNU_versym:
12346 {
12347 Elf_Internal_Shdr * link_section;
12348 size_t total;
12349 unsigned int cnt;
12350 unsigned char * edata;
12351 unsigned short * data;
12352 char * strtab;
12353 Elf_Internal_Sym * symbols;
12354 Elf_Internal_Shdr * string_sec;
12355 unsigned long num_syms;
12356 long off;
12357
12358 if (section->sh_link >= filedata->file_header.e_shnum)
12359 break;
12360
12361 link_section = filedata->section_headers + section->sh_link;
12362 total = section->sh_size / sizeof (Elf_External_Versym);
12363
12364 if (link_section->sh_link >= filedata->file_header.e_shnum)
12365 break;
12366
12367 found = true;
12368
12369 symbols = get_elf_symbols (filedata, link_section, & num_syms);
12370 if (symbols == NULL)
12371 break;
12372
12373 string_sec = filedata->section_headers + link_section->sh_link;
12374
12375 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
12376 string_sec->sh_size,
12377 _("version string table"));
12378 if (!strtab)
12379 {
12380 free (symbols);
12381 break;
12382 }
12383
12384 if (filedata->is_separate)
12385 printf (ngettext ("\nIn linked file '%s' the version symbols section '%s' contains %lu entry:\n",
12386 "\nIn linked file '%s' the version symbols section '%s' contains %lu entries:\n",
12387 total),
12388 filedata->file_name,
12389 printable_section_name (filedata, section),
12390 (unsigned long) total);
12391 else
12392 printf (ngettext ("\nVersion symbols section '%s' "
12393 "contains %lu entry:\n",
12394 "\nVersion symbols section '%s' "
12395 "contains %lu entries:\n",
12396 total),
12397 printable_section_name (filedata, section),
12398 (unsigned long) total);
12399
12400 printf (_(" Addr: 0x"));
12401 printf_vma (section->sh_addr);
12402 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
12403 (unsigned long) section->sh_offset, section->sh_link,
12404 printable_section_name (filedata, link_section));
12405
12406 off = offset_from_vma (filedata,
12407 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
12408 total * sizeof (short));
12409 edata = (unsigned char *) get_data (NULL, filedata, off,
12410 sizeof (short), total,
12411 _("version symbol data"));
12412 if (!edata)
12413 {
12414 free (strtab);
12415 free (symbols);
12416 break;
12417 }
12418
12419 data = (short unsigned int *) cmalloc (total, sizeof (short));
12420
12421 for (cnt = total; cnt --;)
12422 data[cnt] = byte_get (edata + cnt * sizeof (short),
12423 sizeof (short));
12424
12425 free (edata);
12426
12427 for (cnt = 0; cnt < total; cnt += 4)
12428 {
12429 int j, nn;
12430 char *name;
12431 char *invalid = _("*invalid*");
12432
12433 printf (" %03x:", cnt);
12434
12435 for (j = 0; (j < 4) && (cnt + j) < total; ++j)
12436 switch (data[cnt + j])
12437 {
12438 case 0:
12439 fputs (_(" 0 (*local*) "), stdout);
12440 break;
12441
12442 case 1:
12443 fputs (_(" 1 (*global*) "), stdout);
12444 break;
12445
12446 default:
12447 nn = printf ("%4x%c", data[cnt + j] & VERSYM_VERSION,
12448 data[cnt + j] & VERSYM_HIDDEN ? 'h' : ' ');
12449
12450 /* If this index value is greater than the size of the symbols
12451 array, break to avoid an out-of-bounds read. */
12452 if ((unsigned long)(cnt + j) >= num_syms)
12453 {
12454 warn (_("invalid index into symbol array\n"));
12455 break;
12456 }
12457
12458 name = NULL;
12459 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
12460 {
12461 Elf_Internal_Verneed ivn;
12462 unsigned long offset;
12463
12464 offset = offset_from_vma
12465 (filedata,
12466 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
12467 sizeof (Elf_External_Verneed));
12468
12469 do
12470 {
12471 Elf_Internal_Vernaux ivna;
12472 Elf_External_Verneed evn;
12473 Elf_External_Vernaux evna;
12474 unsigned long a_off;
12475
12476 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
12477 _("version need")) == NULL)
12478 break;
12479
12480 ivn.vn_aux = BYTE_GET (evn.vn_aux);
12481 ivn.vn_next = BYTE_GET (evn.vn_next);
12482
12483 a_off = offset + ivn.vn_aux;
12484
12485 do
12486 {
12487 if (get_data (&evna, filedata, a_off, sizeof (evna),
12488 1, _("version need aux (2)")) == NULL)
12489 {
12490 ivna.vna_next = 0;
12491 ivna.vna_other = 0;
12492 }
12493 else
12494 {
12495 ivna.vna_next = BYTE_GET (evna.vna_next);
12496 ivna.vna_other = BYTE_GET (evna.vna_other);
12497 }
12498
12499 a_off += ivna.vna_next;
12500 }
12501 while (ivna.vna_other != data[cnt + j]
12502 && ivna.vna_next != 0);
12503
12504 if (ivna.vna_other == data[cnt + j])
12505 {
12506 ivna.vna_name = BYTE_GET (evna.vna_name);
12507
12508 if (ivna.vna_name >= string_sec->sh_size)
12509 name = invalid;
12510 else
12511 name = strtab + ivna.vna_name;
12512 break;
12513 }
12514
12515 offset += ivn.vn_next;
12516 }
12517 while (ivn.vn_next);
12518 }
12519
12520 if (data[cnt + j] != 0x8001
12521 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
12522 {
12523 Elf_Internal_Verdef ivd;
12524 Elf_External_Verdef evd;
12525 unsigned long offset;
12526
12527 offset = offset_from_vma
12528 (filedata,
12529 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
12530 sizeof evd);
12531
12532 do
12533 {
12534 if (get_data (&evd, filedata, offset, sizeof (evd), 1,
12535 _("version def")) == NULL)
12536 {
12537 ivd.vd_next = 0;
12538 /* PR 17531: file: 046-1082287-0.004. */
12539 ivd.vd_ndx = (data[cnt + j] & VERSYM_VERSION) + 1;
12540 break;
12541 }
12542 else
12543 {
12544 ivd.vd_next = BYTE_GET (evd.vd_next);
12545 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
12546 }
12547
12548 offset += ivd.vd_next;
12549 }
12550 while (ivd.vd_ndx != (data[cnt + j] & VERSYM_VERSION)
12551 && ivd.vd_next != 0);
12552
12553 if (ivd.vd_ndx == (data[cnt + j] & VERSYM_VERSION))
12554 {
12555 Elf_External_Verdaux evda;
12556 Elf_Internal_Verdaux ivda;
12557
12558 ivd.vd_aux = BYTE_GET (evd.vd_aux);
12559
12560 if (get_data (&evda, filedata,
12561 offset - ivd.vd_next + ivd.vd_aux,
12562 sizeof (evda), 1,
12563 _("version def aux")) == NULL)
12564 break;
12565
12566 ivda.vda_name = BYTE_GET (evda.vda_name);
12567
12568 if (ivda.vda_name >= string_sec->sh_size)
12569 name = invalid;
12570 else if (name != NULL && name != invalid)
12571 name = _("*both*");
12572 else
12573 name = strtab + ivda.vda_name;
12574 }
12575 }
12576 if (name != NULL)
12577 nn += printf ("(%s%-*s",
12578 name,
12579 12 - (int) strlen (name),
12580 ")");
12581
12582 if (nn < 18)
12583 printf ("%*c", 18 - nn, ' ');
12584 }
12585
12586 putchar ('\n');
12587 }
12588
12589 free (data);
12590 free (strtab);
12591 free (symbols);
12592 }
12593 break;
12594
12595 default:
12596 break;
12597 }
12598 }
12599
12600 if (! found)
12601 {
12602 if (filedata->is_separate)
12603 printf (_("\nNo version information found in linked file '%s'.\n"),
12604 filedata->file_name);
12605 else
12606 printf (_("\nNo version information found in this file.\n"));
12607 }
12608
12609 return true;
12610 }
12611
12612 static const char *
12613 get_symbol_binding (Filedata * filedata, unsigned int binding)
12614 {
12615 static char buff[64];
12616
12617 switch (binding)
12618 {
12619 case STB_LOCAL: return "LOCAL";
12620 case STB_GLOBAL: return "GLOBAL";
12621 case STB_WEAK: return "WEAK";
12622 default:
12623 if (binding >= STB_LOPROC && binding <= STB_HIPROC)
12624 snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
12625 binding);
12626 else if (binding >= STB_LOOS && binding <= STB_HIOS)
12627 {
12628 if (binding == STB_GNU_UNIQUE
12629 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU)
12630 return "UNIQUE";
12631 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
12632 }
12633 else
12634 snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
12635 return buff;
12636 }
12637 }
12638
12639 static const char *
12640 get_symbol_type (Filedata * filedata, unsigned int type)
12641 {
12642 static char buff[64];
12643
12644 switch (type)
12645 {
12646 case STT_NOTYPE: return "NOTYPE";
12647 case STT_OBJECT: return "OBJECT";
12648 case STT_FUNC: return "FUNC";
12649 case STT_SECTION: return "SECTION";
12650 case STT_FILE: return "FILE";
12651 case STT_COMMON: return "COMMON";
12652 case STT_TLS: return "TLS";
12653 case STT_RELC: return "RELC";
12654 case STT_SRELC: return "SRELC";
12655 default:
12656 if (type >= STT_LOPROC && type <= STT_HIPROC)
12657 {
12658 if (filedata->file_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
12659 return "THUMB_FUNC";
12660
12661 if (filedata->file_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
12662 return "REGISTER";
12663
12664 if (filedata->file_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
12665 return "PARISC_MILLI";
12666
12667 snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
12668 }
12669 else if (type >= STT_LOOS && type <= STT_HIOS)
12670 {
12671 if (filedata->file_header.e_machine == EM_PARISC)
12672 {
12673 if (type == STT_HP_OPAQUE)
12674 return "HP_OPAQUE";
12675 if (type == STT_HP_STUB)
12676 return "HP_STUB";
12677 }
12678
12679 if (type == STT_GNU_IFUNC
12680 && (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU
12681 || filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_FREEBSD))
12682 return "IFUNC";
12683
12684 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
12685 }
12686 else
12687 snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
12688 return buff;
12689 }
12690 }
12691
12692 static const char *
12693 get_symbol_visibility (unsigned int visibility)
12694 {
12695 switch (visibility)
12696 {
12697 case STV_DEFAULT: return "DEFAULT";
12698 case STV_INTERNAL: return "INTERNAL";
12699 case STV_HIDDEN: return "HIDDEN";
12700 case STV_PROTECTED: return "PROTECTED";
12701 default:
12702 error (_("Unrecognized visibility value: %u\n"), visibility);
12703 return _("<unknown>");
12704 }
12705 }
12706
12707 static const char *
12708 get_alpha_symbol_other (unsigned int other)
12709 {
12710 switch (other)
12711 {
12712 case STO_ALPHA_NOPV: return "NOPV";
12713 case STO_ALPHA_STD_GPLOAD: return "STD GPLOAD";
12714 default:
12715 error (_("Unrecognized alpha specific other value: %u\n"), other);
12716 return _("<unknown>");
12717 }
12718 }
12719
12720 static const char *
12721 get_solaris_symbol_visibility (unsigned int visibility)
12722 {
12723 switch (visibility)
12724 {
12725 case 4: return "EXPORTED";
12726 case 5: return "SINGLETON";
12727 case 6: return "ELIMINATE";
12728 default: return get_symbol_visibility (visibility);
12729 }
12730 }
12731
12732 static const char *
12733 get_aarch64_symbol_other (unsigned int other)
12734 {
12735 static char buf[32];
12736
12737 if (other & STO_AARCH64_VARIANT_PCS)
12738 {
12739 other &= ~STO_AARCH64_VARIANT_PCS;
12740 if (other == 0)
12741 return "VARIANT_PCS";
12742 snprintf (buf, sizeof buf, "VARIANT_PCS | %x", other);
12743 return buf;
12744 }
12745 return NULL;
12746 }
12747
12748 static const char *
12749 get_mips_symbol_other (unsigned int other)
12750 {
12751 switch (other)
12752 {
12753 case STO_OPTIONAL: return "OPTIONAL";
12754 case STO_MIPS_PLT: return "MIPS PLT";
12755 case STO_MIPS_PIC: return "MIPS PIC";
12756 case STO_MICROMIPS: return "MICROMIPS";
12757 case STO_MICROMIPS | STO_MIPS_PIC: return "MICROMIPS, MIPS PIC";
12758 case STO_MIPS16: return "MIPS16";
12759 default: return NULL;
12760 }
12761 }
12762
12763 static const char *
12764 get_ia64_symbol_other (Filedata * filedata, unsigned int other)
12765 {
12766 if (is_ia64_vms (filedata))
12767 {
12768 static char res[32];
12769
12770 res[0] = 0;
12771
12772 /* Function types is for images and .STB files only. */
12773 switch (filedata->file_header.e_type)
12774 {
12775 case ET_DYN:
12776 case ET_EXEC:
12777 switch (VMS_ST_FUNC_TYPE (other))
12778 {
12779 case VMS_SFT_CODE_ADDR:
12780 strcat (res, " CA");
12781 break;
12782 case VMS_SFT_SYMV_IDX:
12783 strcat (res, " VEC");
12784 break;
12785 case VMS_SFT_FD:
12786 strcat (res, " FD");
12787 break;
12788 case VMS_SFT_RESERVE:
12789 strcat (res, " RSV");
12790 break;
12791 default:
12792 warn (_("Unrecognized IA64 VMS ST Function type: %d\n"),
12793 VMS_ST_FUNC_TYPE (other));
12794 strcat (res, " <unknown>");
12795 break;
12796 }
12797 break;
12798 default:
12799 break;
12800 }
12801 switch (VMS_ST_LINKAGE (other))
12802 {
12803 case VMS_STL_IGNORE:
12804 strcat (res, " IGN");
12805 break;
12806 case VMS_STL_RESERVE:
12807 strcat (res, " RSV");
12808 break;
12809 case VMS_STL_STD:
12810 strcat (res, " STD");
12811 break;
12812 case VMS_STL_LNK:
12813 strcat (res, " LNK");
12814 break;
12815 default:
12816 warn (_("Unrecognized IA64 VMS ST Linkage: %d\n"),
12817 VMS_ST_LINKAGE (other));
12818 strcat (res, " <unknown>");
12819 break;
12820 }
12821
12822 if (res[0] != 0)
12823 return res + 1;
12824 else
12825 return res;
12826 }
12827 return NULL;
12828 }
12829
12830 static const char *
12831 get_ppc64_symbol_other (unsigned int other)
12832 {
12833 if ((other & ~STO_PPC64_LOCAL_MASK) != 0)
12834 return NULL;
12835
12836 other >>= STO_PPC64_LOCAL_BIT;
12837 if (other <= 6)
12838 {
12839 static char buf[64];
12840 if (other >= 2)
12841 other = ppc64_decode_local_entry (other);
12842 snprintf (buf, sizeof buf, _("<localentry>: %d"), other);
12843 return buf;
12844 }
12845 return NULL;
12846 }
12847
12848 static const char *
12849 get_riscv_symbol_other (unsigned int other)
12850 {
12851 static char buf[32];
12852 buf[0] = 0;
12853
12854 if (other & STO_RISCV_VARIANT_CC)
12855 {
12856 strcat (buf, _(" VARIANT_CC"));
12857 other &= ~STO_RISCV_VARIANT_CC;
12858 }
12859
12860 if (other != 0)
12861 snprintf (buf, sizeof buf, " %x", other);
12862
12863
12864 if (buf[0] != 0)
12865 return buf + 1;
12866 else
12867 return buf;
12868 }
12869
12870 static const char *
12871 get_symbol_other (Filedata * filedata, unsigned int other)
12872 {
12873 const char * result = NULL;
12874 static char buff [64];
12875
12876 if (other == 0)
12877 return "";
12878
12879 switch (filedata->file_header.e_machine)
12880 {
12881 case EM_ALPHA:
12882 result = get_alpha_symbol_other (other);
12883 break;
12884 case EM_AARCH64:
12885 result = get_aarch64_symbol_other (other);
12886 break;
12887 case EM_MIPS:
12888 result = get_mips_symbol_other (other);
12889 break;
12890 case EM_IA_64:
12891 result = get_ia64_symbol_other (filedata, other);
12892 break;
12893 case EM_PPC64:
12894 result = get_ppc64_symbol_other (other);
12895 break;
12896 case EM_RISCV:
12897 result = get_riscv_symbol_other (other);
12898 break;
12899 default:
12900 result = NULL;
12901 break;
12902 }
12903
12904 if (result)
12905 return result;
12906
12907 snprintf (buff, sizeof buff, _("<other>: %x"), other);
12908 return buff;
12909 }
12910
12911 static const char *
12912 get_symbol_index_type (Filedata * filedata, unsigned int type)
12913 {
12914 static char buff[32];
12915
12916 switch (type)
12917 {
12918 case SHN_UNDEF: return "UND";
12919 case SHN_ABS: return "ABS";
12920 case SHN_COMMON: return "COM";
12921 default:
12922 if (type == SHN_IA_64_ANSI_COMMON
12923 && filedata->file_header.e_machine == EM_IA_64
12924 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX)
12925 return "ANSI_COM";
12926 else if ((filedata->file_header.e_machine == EM_X86_64
12927 || filedata->file_header.e_machine == EM_L1OM
12928 || filedata->file_header.e_machine == EM_K1OM)
12929 && type == SHN_X86_64_LCOMMON)
12930 return "LARGE_COM";
12931 else if ((type == SHN_MIPS_SCOMMON
12932 && filedata->file_header.e_machine == EM_MIPS)
12933 || (type == SHN_TIC6X_SCOMMON
12934 && filedata->file_header.e_machine == EM_TI_C6000))
12935 return "SCOM";
12936 else if (type == SHN_MIPS_SUNDEFINED
12937 && filedata->file_header.e_machine == EM_MIPS)
12938 return "SUND";
12939 else if (type >= SHN_LOPROC && type <= SHN_HIPROC)
12940 sprintf (buff, "PRC[0x%04x]", type & 0xffff);
12941 else if (type >= SHN_LOOS && type <= SHN_HIOS)
12942 sprintf (buff, "OS [0x%04x]", type & 0xffff);
12943 else if (type >= SHN_LORESERVE)
12944 sprintf (buff, "RSV[0x%04x]", type & 0xffff);
12945 else if (filedata->file_header.e_shnum != 0
12946 && type >= filedata->file_header.e_shnum)
12947 sprintf (buff, _("bad section index[%3d]"), type);
12948 else
12949 sprintf (buff, "%3d", type);
12950 break;
12951 }
12952
12953 return buff;
12954 }
12955
12956 static const char *
12957 get_symbol_version_string (Filedata * filedata,
12958 bool is_dynsym,
12959 const char * strtab,
12960 unsigned long int strtab_size,
12961 unsigned int si,
12962 Elf_Internal_Sym * psym,
12963 enum versioned_symbol_info * sym_info,
12964 unsigned short * vna_other)
12965 {
12966 unsigned char data[2];
12967 unsigned short vers_data;
12968 unsigned long offset;
12969 unsigned short max_vd_ndx;
12970
12971 if (!is_dynsym
12972 || filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)] == 0)
12973 return NULL;
12974
12975 offset = offset_from_vma (filedata,
12976 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
12977 sizeof data + si * sizeof (vers_data));
12978
12979 if (get_data (&data, filedata, offset + si * sizeof (vers_data),
12980 sizeof (data), 1, _("version data")) == NULL)
12981 return NULL;
12982
12983 vers_data = byte_get (data, 2);
12984
12985 if ((vers_data & VERSYM_HIDDEN) == 0 && vers_data == 0)
12986 return NULL;
12987
12988 *sym_info = (vers_data & VERSYM_HIDDEN) != 0 ? symbol_hidden : symbol_public;
12989 max_vd_ndx = 0;
12990
12991 /* Usually we'd only see verdef for defined symbols, and verneed for
12992 undefined symbols. However, symbols defined by the linker in
12993 .dynbss for variables copied from a shared library in order to
12994 avoid text relocations are defined yet have verneed. We could
12995 use a heuristic to detect the special case, for example, check
12996 for verneed first on symbols defined in SHT_NOBITS sections, but
12997 it is simpler and more reliable to just look for both verdef and
12998 verneed. .dynbss might not be mapped to a SHT_NOBITS section. */
12999
13000 if (psym->st_shndx != SHN_UNDEF
13001 && vers_data != 0x8001
13002 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
13003 {
13004 Elf_Internal_Verdef ivd;
13005 Elf_Internal_Verdaux ivda;
13006 Elf_External_Verdaux evda;
13007 unsigned long off;
13008
13009 off = offset_from_vma (filedata,
13010 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
13011 sizeof (Elf_External_Verdef));
13012
13013 do
13014 {
13015 Elf_External_Verdef evd;
13016
13017 if (get_data (&evd, filedata, off, sizeof (evd), 1,
13018 _("version def")) == NULL)
13019 {
13020 ivd.vd_ndx = 0;
13021 ivd.vd_aux = 0;
13022 ivd.vd_next = 0;
13023 ivd.vd_flags = 0;
13024 }
13025 else
13026 {
13027 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
13028 ivd.vd_aux = BYTE_GET (evd.vd_aux);
13029 ivd.vd_next = BYTE_GET (evd.vd_next);
13030 ivd.vd_flags = BYTE_GET (evd.vd_flags);
13031 }
13032
13033 if ((ivd.vd_ndx & VERSYM_VERSION) > max_vd_ndx)
13034 max_vd_ndx = ivd.vd_ndx & VERSYM_VERSION;
13035
13036 off += ivd.vd_next;
13037 }
13038 while (ivd.vd_ndx != (vers_data & VERSYM_VERSION) && ivd.vd_next != 0);
13039
13040 if (ivd.vd_ndx == (vers_data & VERSYM_VERSION))
13041 {
13042 if (ivd.vd_ndx == 1 && ivd.vd_flags == VER_FLG_BASE)
13043 return NULL;
13044
13045 off -= ivd.vd_next;
13046 off += ivd.vd_aux;
13047
13048 if (get_data (&evda, filedata, off, sizeof (evda), 1,
13049 _("version def aux")) != NULL)
13050 {
13051 ivda.vda_name = BYTE_GET (evda.vda_name);
13052
13053 if (psym->st_name != ivda.vda_name)
13054 return (ivda.vda_name < strtab_size
13055 ? strtab + ivda.vda_name : _("<corrupt>"));
13056 }
13057 }
13058 }
13059
13060 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
13061 {
13062 Elf_External_Verneed evn;
13063 Elf_Internal_Verneed ivn;
13064 Elf_Internal_Vernaux ivna;
13065
13066 offset = offset_from_vma (filedata,
13067 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
13068 sizeof evn);
13069 do
13070 {
13071 unsigned long vna_off;
13072
13073 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
13074 _("version need")) == NULL)
13075 {
13076 ivna.vna_next = 0;
13077 ivna.vna_other = 0;
13078 ivna.vna_name = 0;
13079 break;
13080 }
13081
13082 ivn.vn_aux = BYTE_GET (evn.vn_aux);
13083 ivn.vn_next = BYTE_GET (evn.vn_next);
13084
13085 vna_off = offset + ivn.vn_aux;
13086
13087 do
13088 {
13089 Elf_External_Vernaux evna;
13090
13091 if (get_data (&evna, filedata, vna_off, sizeof (evna), 1,
13092 _("version need aux (3)")) == NULL)
13093 {
13094 ivna.vna_next = 0;
13095 ivna.vna_other = 0;
13096 ivna.vna_name = 0;
13097 }
13098 else
13099 {
13100 ivna.vna_other = BYTE_GET (evna.vna_other);
13101 ivna.vna_next = BYTE_GET (evna.vna_next);
13102 ivna.vna_name = BYTE_GET (evna.vna_name);
13103 }
13104
13105 vna_off += ivna.vna_next;
13106 }
13107 while (ivna.vna_other != vers_data && ivna.vna_next != 0);
13108
13109 if (ivna.vna_other == vers_data)
13110 break;
13111
13112 offset += ivn.vn_next;
13113 }
13114 while (ivn.vn_next != 0);
13115
13116 if (ivna.vna_other == vers_data)
13117 {
13118 *sym_info = symbol_undefined;
13119 *vna_other = ivna.vna_other;
13120 return (ivna.vna_name < strtab_size
13121 ? strtab + ivna.vna_name : _("<corrupt>"));
13122 }
13123 else if ((max_vd_ndx || (vers_data & VERSYM_VERSION) != 1)
13124 && (vers_data & VERSYM_VERSION) > max_vd_ndx)
13125 return _("<corrupt>");
13126 }
13127 return NULL;
13128 }
13129
13130 /* Display a symbol size on stdout. Format is based on --sym-base setting. */
13131
13132 static unsigned int
13133 print_dynamic_symbol_size (bfd_vma vma, int base)
13134 {
13135 switch (base)
13136 {
13137 case 8:
13138 return print_vma (vma, OCTAL_5);
13139
13140 case 10:
13141 return print_vma (vma, UNSIGNED_5);
13142
13143 case 16:
13144 return print_vma (vma, PREFIX_HEX_5);
13145
13146 case 0:
13147 default:
13148 return print_vma (vma, DEC_5);
13149 }
13150 }
13151
13152 static void
13153 print_dynamic_symbol (Filedata *filedata, unsigned long si,
13154 Elf_Internal_Sym *symtab,
13155 Elf_Internal_Shdr *section,
13156 char *strtab, size_t strtab_size)
13157 {
13158 const char *version_string;
13159 enum versioned_symbol_info sym_info;
13160 unsigned short vna_other;
13161 bool is_valid;
13162 const char * sstr;
13163 Elf_Internal_Sym *psym = symtab + si;
13164
13165 printf ("%6ld: ", si);
13166 print_vma (psym->st_value, LONG_HEX);
13167 putchar (' ');
13168 print_dynamic_symbol_size (psym->st_size, sym_base);
13169 printf (" %-7s", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
13170 printf (" %-6s", get_symbol_binding (filedata, ELF_ST_BIND (psym->st_info)));
13171 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
13172 printf (" %-7s", get_solaris_symbol_visibility (psym->st_other));
13173 else
13174 {
13175 unsigned int vis = ELF_ST_VISIBILITY (psym->st_other);
13176
13177 printf (" %-7s", get_symbol_visibility (vis));
13178 /* Check to see if any other bits in the st_other field are set.
13179 Note - displaying this information disrupts the layout of the
13180 table being generated, but for the moment this case is very rare. */
13181 if (psym->st_other ^ vis)
13182 printf (" [%s] ", get_symbol_other (filedata, psym->st_other ^ vis));
13183 }
13184 printf (" %4s ", get_symbol_index_type (filedata, psym->st_shndx));
13185
13186 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION
13187 && psym->st_shndx < filedata->file_header.e_shnum
13188 && filedata->section_headers != NULL
13189 && psym->st_name == 0)
13190 {
13191 is_valid
13192 = section_name_valid (filedata,
13193 filedata->section_headers + psym->st_shndx);
13194 sstr = is_valid ?
13195 section_name_print (filedata,
13196 filedata->section_headers + psym->st_shndx)
13197 : _("<corrupt>");
13198 }
13199 else
13200 {
13201 is_valid = valid_symbol_name (strtab, strtab_size, psym->st_name);
13202 sstr = is_valid ? strtab + psym->st_name : _("<corrupt>");
13203 }
13204
13205 version_string
13206 = get_symbol_version_string (filedata,
13207 (section == NULL
13208 || section->sh_type == SHT_DYNSYM),
13209 strtab, strtab_size, si,
13210 psym, &sym_info, &vna_other);
13211
13212 int len_avail = 21;
13213 if (! do_wide && version_string != NULL)
13214 {
13215 char buffer[16];
13216
13217 len_avail -= 1 + strlen (version_string);
13218
13219 if (sym_info == symbol_undefined)
13220 len_avail -= sprintf (buffer," (%d)", vna_other);
13221 else if (sym_info != symbol_hidden)
13222 len_avail -= 1;
13223 }
13224
13225 print_symbol (len_avail, sstr);
13226
13227 if (version_string)
13228 {
13229 if (sym_info == symbol_undefined)
13230 printf ("@%s (%d)", version_string, vna_other);
13231 else
13232 printf (sym_info == symbol_hidden ? "@%s" : "@@%s",
13233 version_string);
13234 }
13235
13236 putchar ('\n');
13237
13238 if (ELF_ST_BIND (psym->st_info) == STB_LOCAL
13239 && section != NULL
13240 && si >= section->sh_info
13241 /* Irix 5 and 6 MIPS binaries are known to ignore this requirement. */
13242 && filedata->file_header.e_machine != EM_MIPS
13243 /* Solaris binaries have been found to violate this requirement as
13244 well. Not sure if this is a bug or an ABI requirement. */
13245 && filedata->file_header.e_ident[EI_OSABI] != ELFOSABI_SOLARIS)
13246 warn (_("local symbol %lu found at index >= %s's sh_info value of %u\n"),
13247 si, printable_section_name (filedata, section), section->sh_info);
13248 }
13249
13250 static const char *
13251 get_lto_kind (unsigned int kind)
13252 {
13253 switch (kind)
13254 {
13255 case 0: return "DEF";
13256 case 1: return "WEAKDEF";
13257 case 2: return "UNDEF";
13258 case 3: return "WEAKUNDEF";
13259 case 4: return "COMMON";
13260 default:
13261 break;
13262 }
13263
13264 static char buffer[30];
13265 error (_("Unknown LTO symbol definition encountered: %u\n"), kind);
13266 sprintf (buffer, "<unknown: %u>", kind);
13267 return buffer;
13268 }
13269
13270 static const char *
13271 get_lto_visibility (unsigned int visibility)
13272 {
13273 switch (visibility)
13274 {
13275 case 0: return "DEFAULT";
13276 case 1: return "PROTECTED";
13277 case 2: return "INTERNAL";
13278 case 3: return "HIDDEN";
13279 default:
13280 break;
13281 }
13282
13283 static char buffer[30];
13284 error (_("Unknown LTO symbol visibility encountered: %u\n"), visibility);
13285 sprintf (buffer, "<unknown: %u>", visibility);
13286 return buffer;
13287 }
13288
13289 static const char *
13290 get_lto_sym_type (unsigned int sym_type)
13291 {
13292 switch (sym_type)
13293 {
13294 case 0: return "UNKNOWN";
13295 case 1: return "FUNCTION";
13296 case 2: return "VARIABLE";
13297 default:
13298 break;
13299 }
13300
13301 static char buffer[30];
13302 error (_("Unknown LTO symbol type encountered: %u\n"), sym_type);
13303 sprintf (buffer, "<unknown: %u>", sym_type);
13304 return buffer;
13305 }
13306
13307 /* Display an LTO format symbol table.
13308 FIXME: The format of LTO symbol tables is not formalized.
13309 So this code could need changing in the future. */
13310
13311 static bool
13312 display_lto_symtab (Filedata * filedata,
13313 Elf_Internal_Shdr * section)
13314 {
13315 if (section->sh_size == 0)
13316 {
13317 if (filedata->is_separate)
13318 printf (_("\nThe LTO Symbol table section '%s' in linked file '%s' is empty!\n"),
13319 printable_section_name (filedata, section),
13320 filedata->file_name);
13321 else
13322 printf (_("\nLTO Symbol table '%s' is empty!\n"),
13323 printable_section_name (filedata, section));
13324
13325 return true;
13326 }
13327
13328 if (section->sh_size > filedata->file_size)
13329 {
13330 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
13331 printable_section_name (filedata, section),
13332 (unsigned long) section->sh_size);
13333 return false;
13334 }
13335
13336 void * alloced_data = get_data (NULL, filedata, section->sh_offset,
13337 section->sh_size, 1, _("LTO symbols"));
13338 if (alloced_data == NULL)
13339 return false;
13340
13341 /* Look for extended data for the symbol table. */
13342 Elf_Internal_Shdr * ext;
13343 void * ext_data_orig = NULL;
13344 char * ext_data = NULL;
13345 char * ext_data_end = NULL;
13346 char * ext_name = NULL;
13347
13348 if (asprintf (& ext_name, ".gnu.lto_.ext_symtab.%s",
13349 (section_name (filedata, section)
13350 + sizeof (".gnu.lto_.symtab.") - 1)) > 0
13351 && ext_name != NULL /* Paranoia. */
13352 && (ext = find_section (filedata, ext_name)) != NULL)
13353 {
13354 if (ext->sh_size < 3)
13355 error (_("LTO Symbol extension table '%s' is empty!\n"),
13356 printable_section_name (filedata, ext));
13357 else
13358 {
13359 ext_data_orig = ext_data = get_data (NULL, filedata, ext->sh_offset,
13360 ext->sh_size, 1,
13361 _("LTO ext symbol data"));
13362 if (ext_data != NULL)
13363 {
13364 ext_data_end = ext_data + ext->sh_size;
13365 if (* ext_data++ != 1)
13366 error (_("Unexpected version number in symbol extension table\n"));
13367 }
13368 }
13369 }
13370
13371 const unsigned char * data = (const unsigned char *) alloced_data;
13372 const unsigned char * end = data + section->sh_size;
13373
13374 if (filedata->is_separate)
13375 printf (_("\nIn linked file '%s': "), filedata->file_name);
13376 else
13377 printf ("\n");
13378
13379 if (ext_data_orig != NULL)
13380 {
13381 if (do_wide)
13382 printf (_("LTO Symbol table '%s' and extension table '%s' contain:\n"),
13383 printable_section_name (filedata, section),
13384 printable_section_name (filedata, ext));
13385 else
13386 {
13387 printf (_("LTO Symbol table '%s'\n"),
13388 printable_section_name (filedata, section));
13389 printf (_(" and extension table '%s' contain:\n"),
13390 printable_section_name (filedata, ext));
13391 }
13392 }
13393 else
13394 printf (_("LTO Symbol table '%s' contains:\n"),
13395 printable_section_name (filedata, section));
13396
13397 /* FIXME: Add a wide version. */
13398 if (ext_data_orig != NULL)
13399 printf (_(" Comdat_Key Kind Visibility Size Slot Type Section Name\n"));
13400 else
13401 printf (_(" Comdat_Key Kind Visibility Size Slot Name\n"));
13402
13403 /* FIXME: We do not handle style prefixes. */
13404
13405 while (data < end)
13406 {
13407 const unsigned char * sym_name = data;
13408 data += strnlen ((const char *) sym_name, end - data) + 1;
13409 if (data >= end)
13410 goto fail;
13411
13412 const unsigned char * comdat_key = data;
13413 data += strnlen ((const char *) comdat_key, end - data) + 1;
13414 if (data >= end)
13415 goto fail;
13416
13417 if (data + 2 + 8 + 4 > end)
13418 goto fail;
13419
13420 unsigned int kind = *data++;
13421 unsigned int visibility = *data++;
13422
13423 elf_vma size = byte_get (data, 8);
13424 data += 8;
13425
13426 elf_vma slot = byte_get (data, 4);
13427 data += 4;
13428
13429 if (ext_data != NULL)
13430 {
13431 if (ext_data < (ext_data_end - 1))
13432 {
13433 unsigned int sym_type = * ext_data ++;
13434 unsigned int sec_kind = * ext_data ++;
13435
13436 printf (" %10s %10s %11s %08lx %08lx %9s %08lx _",
13437 * comdat_key == 0 ? "-" : (char *) comdat_key,
13438 get_lto_kind (kind),
13439 get_lto_visibility (visibility),
13440 (long) size,
13441 (long) slot,
13442 get_lto_sym_type (sym_type),
13443 (long) sec_kind);
13444 print_symbol (6, (const char *) sym_name);
13445 }
13446 else
13447 {
13448 error (_("Ran out of LTO symbol extension data\n"));
13449 ext_data = NULL;
13450 /* FIXME: return FAIL result ? */
13451 }
13452 }
13453 else
13454 {
13455 printf (" %10s %10s %11s %08lx %08lx _",
13456 * comdat_key == 0 ? "-" : (char *) comdat_key,
13457 get_lto_kind (kind),
13458 get_lto_visibility (visibility),
13459 (long) size,
13460 (long) slot);
13461 print_symbol (21, (const char *) sym_name);
13462 }
13463 putchar ('\n');
13464 }
13465
13466 if (ext_data != NULL && ext_data < ext_data_end)
13467 {
13468 error (_("Data remains in the LTO symbol extension table\n"));
13469 goto fail;
13470 }
13471
13472 free (alloced_data);
13473 free (ext_data_orig);
13474 free (ext_name);
13475 return true;
13476
13477 fail:
13478 error (_("Buffer overrun encountered whilst decoding LTO symbol table\n"));
13479 free (alloced_data);
13480 free (ext_data_orig);
13481 free (ext_name);
13482 return false;
13483 }
13484
13485 /* Display LTO symbol tables. */
13486
13487 static bool
13488 process_lto_symbol_tables (Filedata * filedata)
13489 {
13490 Elf_Internal_Shdr * section;
13491 unsigned int i;
13492 bool res = true;
13493
13494 if (!do_lto_syms)
13495 return true;
13496
13497 if (filedata->section_headers == NULL)
13498 return true;
13499
13500 for (i = 0, section = filedata->section_headers;
13501 i < filedata->file_header.e_shnum;
13502 i++, section++)
13503 if (section_name_valid (filedata, section)
13504 && startswith (section_name (filedata, section), ".gnu.lto_.symtab."))
13505 res &= display_lto_symtab (filedata, section);
13506
13507 return res;
13508 }
13509
13510 /* Dump the symbol table. */
13511
13512 static bool
13513 process_symbol_table (Filedata * filedata)
13514 {
13515 Elf_Internal_Shdr * section;
13516
13517 if (!do_syms && !do_dyn_syms && !do_histogram)
13518 return true;
13519
13520 if ((filedata->dynamic_info[DT_HASH] || filedata->dynamic_info_DT_GNU_HASH)
13521 && do_syms
13522 && do_using_dynamic
13523 && filedata->dynamic_strings != NULL
13524 && filedata->dynamic_symbols != NULL)
13525 {
13526 unsigned long si;
13527
13528 if (filedata->is_separate)
13529 {
13530 printf (ngettext ("\nIn linked file '%s' the dynamic symbol table contains %lu entry:\n",
13531 "\nIn linked file '%s' the dynamic symbol table contains %lu entries:\n",
13532 filedata->num_dynamic_syms),
13533 filedata->file_name,
13534 filedata->num_dynamic_syms);
13535 }
13536 else
13537 {
13538 printf (ngettext ("\nSymbol table for image contains %lu entry:\n",
13539 "\nSymbol table for image contains %lu entries:\n",
13540 filedata->num_dynamic_syms),
13541 filedata->num_dynamic_syms);
13542 }
13543 if (is_32bit_elf)
13544 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13545 else
13546 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13547
13548 for (si = 0; si < filedata->num_dynamic_syms; si++)
13549 print_dynamic_symbol (filedata, si, filedata->dynamic_symbols, NULL,
13550 filedata->dynamic_strings,
13551 filedata->dynamic_strings_length);
13552 }
13553 else if ((do_dyn_syms || (do_syms && !do_using_dynamic))
13554 && filedata->section_headers != NULL)
13555 {
13556 unsigned int i;
13557
13558 for (i = 0, section = filedata->section_headers;
13559 i < filedata->file_header.e_shnum;
13560 i++, section++)
13561 {
13562 char * strtab = NULL;
13563 unsigned long int strtab_size = 0;
13564 Elf_Internal_Sym * symtab;
13565 unsigned long si, num_syms;
13566
13567 if ((section->sh_type != SHT_SYMTAB
13568 && section->sh_type != SHT_DYNSYM)
13569 || (!do_syms
13570 && section->sh_type == SHT_SYMTAB))
13571 continue;
13572
13573 if (section->sh_entsize == 0)
13574 {
13575 printf (_("\nSymbol table '%s' has a sh_entsize of zero!\n"),
13576 printable_section_name (filedata, section));
13577 continue;
13578 }
13579
13580 num_syms = section->sh_size / section->sh_entsize;
13581
13582 if (filedata->is_separate)
13583 printf (ngettext ("\nIn linked file '%s' symbol section '%s' contains %lu entry:\n",
13584 "\nIn linked file '%s' symbol section '%s' contains %lu entries:\n",
13585 num_syms),
13586 filedata->file_name,
13587 printable_section_name (filedata, section),
13588 num_syms);
13589 else
13590 printf (ngettext ("\nSymbol table '%s' contains %lu entry:\n",
13591 "\nSymbol table '%s' contains %lu entries:\n",
13592 num_syms),
13593 printable_section_name (filedata, section),
13594 num_syms);
13595
13596 if (is_32bit_elf)
13597 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13598 else
13599 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13600
13601 symtab = get_elf_symbols (filedata, section, & num_syms);
13602 if (symtab == NULL)
13603 continue;
13604
13605 if (section->sh_link == filedata->file_header.e_shstrndx)
13606 {
13607 strtab = filedata->string_table;
13608 strtab_size = filedata->string_table_length;
13609 }
13610 else if (section->sh_link < filedata->file_header.e_shnum)
13611 {
13612 Elf_Internal_Shdr * string_sec;
13613
13614 string_sec = filedata->section_headers + section->sh_link;
13615
13616 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset,
13617 1, string_sec->sh_size,
13618 _("string table"));
13619 strtab_size = strtab != NULL ? string_sec->sh_size : 0;
13620 }
13621
13622 for (si = 0; si < num_syms; si++)
13623 print_dynamic_symbol (filedata, si, symtab, section,
13624 strtab, strtab_size);
13625
13626 free (symtab);
13627 if (strtab != filedata->string_table)
13628 free (strtab);
13629 }
13630 }
13631 else if (do_syms)
13632 printf
13633 (_("\nDynamic symbol information is not available for displaying symbols.\n"));
13634
13635 if (do_histogram && filedata->buckets != NULL)
13636 {
13637 unsigned long * lengths;
13638 unsigned long * counts;
13639 unsigned long hn;
13640 bfd_vma si;
13641 unsigned long maxlength = 0;
13642 unsigned long nzero_counts = 0;
13643 unsigned long nsyms = 0;
13644 char *visited;
13645
13646 printf (ngettext ("\nHistogram for bucket list length "
13647 "(total of %lu bucket):\n",
13648 "\nHistogram for bucket list length "
13649 "(total of %lu buckets):\n",
13650 (unsigned long) filedata->nbuckets),
13651 (unsigned long) filedata->nbuckets);
13652
13653 lengths = (unsigned long *) calloc (filedata->nbuckets,
13654 sizeof (*lengths));
13655 if (lengths == NULL)
13656 {
13657 error (_("Out of memory allocating space for histogram buckets\n"));
13658 goto err_out;
13659 }
13660 visited = xcmalloc (filedata->nchains, 1);
13661 memset (visited, 0, filedata->nchains);
13662
13663 printf (_(" Length Number %% of total Coverage\n"));
13664 for (hn = 0; hn < filedata->nbuckets; ++hn)
13665 {
13666 for (si = filedata->buckets[hn]; si > 0; si = filedata->chains[si])
13667 {
13668 ++nsyms;
13669 if (maxlength < ++lengths[hn])
13670 ++maxlength;
13671 if (si >= filedata->nchains || visited[si])
13672 {
13673 error (_("histogram chain is corrupt\n"));
13674 break;
13675 }
13676 visited[si] = 1;
13677 }
13678 }
13679 free (visited);
13680
13681 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
13682 if (counts == NULL)
13683 {
13684 free (lengths);
13685 error (_("Out of memory allocating space for histogram counts\n"));
13686 goto err_out;
13687 }
13688
13689 for (hn = 0; hn < filedata->nbuckets; ++hn)
13690 ++counts[lengths[hn]];
13691
13692 if (filedata->nbuckets > 0)
13693 {
13694 unsigned long i;
13695 printf (" 0 %-10lu (%5.1f%%)\n",
13696 counts[0], (counts[0] * 100.0) / filedata->nbuckets);
13697 for (i = 1; i <= maxlength; ++i)
13698 {
13699 nzero_counts += counts[i] * i;
13700 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
13701 i, counts[i], (counts[i] * 100.0) / filedata->nbuckets,
13702 (nzero_counts * 100.0) / nsyms);
13703 }
13704 }
13705
13706 free (counts);
13707 free (lengths);
13708 }
13709
13710 free (filedata->buckets);
13711 filedata->buckets = NULL;
13712 filedata->nbuckets = 0;
13713 free (filedata->chains);
13714 filedata->chains = NULL;
13715
13716 if (do_histogram && filedata->gnubuckets != NULL)
13717 {
13718 unsigned long * lengths;
13719 unsigned long * counts;
13720 unsigned long hn;
13721 unsigned long maxlength = 0;
13722 unsigned long nzero_counts = 0;
13723 unsigned long nsyms = 0;
13724
13725 printf (ngettext ("\nHistogram for `%s' bucket list length "
13726 "(total of %lu bucket):\n",
13727 "\nHistogram for `%s' bucket list length "
13728 "(total of %lu buckets):\n",
13729 (unsigned long) filedata->ngnubuckets),
13730 GNU_HASH_SECTION_NAME (filedata),
13731 (unsigned long) filedata->ngnubuckets);
13732
13733 lengths = (unsigned long *) calloc (filedata->ngnubuckets,
13734 sizeof (*lengths));
13735 if (lengths == NULL)
13736 {
13737 error (_("Out of memory allocating space for gnu histogram buckets\n"));
13738 goto err_out;
13739 }
13740
13741 printf (_(" Length Number %% of total Coverage\n"));
13742
13743 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
13744 if (filedata->gnubuckets[hn] != 0)
13745 {
13746 bfd_vma off, length = 1;
13747
13748 for (off = filedata->gnubuckets[hn] - filedata->gnusymidx;
13749 /* PR 17531 file: 010-77222-0.004. */
13750 off < filedata->ngnuchains
13751 && (filedata->gnuchains[off] & 1) == 0;
13752 ++off)
13753 ++length;
13754 lengths[hn] = length;
13755 if (length > maxlength)
13756 maxlength = length;
13757 nsyms += length;
13758 }
13759
13760 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
13761 if (counts == NULL)
13762 {
13763 free (lengths);
13764 error (_("Out of memory allocating space for gnu histogram counts\n"));
13765 goto err_out;
13766 }
13767
13768 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
13769 ++counts[lengths[hn]];
13770
13771 if (filedata->ngnubuckets > 0)
13772 {
13773 unsigned long j;
13774 printf (" 0 %-10lu (%5.1f%%)\n",
13775 counts[0], (counts[0] * 100.0) / filedata->ngnubuckets);
13776 for (j = 1; j <= maxlength; ++j)
13777 {
13778 nzero_counts += counts[j] * j;
13779 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
13780 j, counts[j], (counts[j] * 100.0) / filedata->ngnubuckets,
13781 (nzero_counts * 100.0) / nsyms);
13782 }
13783 }
13784
13785 free (counts);
13786 free (lengths);
13787 }
13788 free (filedata->gnubuckets);
13789 filedata->gnubuckets = NULL;
13790 filedata->ngnubuckets = 0;
13791 free (filedata->gnuchains);
13792 filedata->gnuchains = NULL;
13793 filedata->ngnuchains = 0;
13794 free (filedata->mipsxlat);
13795 filedata->mipsxlat = NULL;
13796 return true;
13797
13798 err_out:
13799 free (filedata->gnubuckets);
13800 filedata->gnubuckets = NULL;
13801 filedata->ngnubuckets = 0;
13802 free (filedata->gnuchains);
13803 filedata->gnuchains = NULL;
13804 filedata->ngnuchains = 0;
13805 free (filedata->mipsxlat);
13806 filedata->mipsxlat = NULL;
13807 free (filedata->buckets);
13808 filedata->buckets = NULL;
13809 filedata->nbuckets = 0;
13810 free (filedata->chains);
13811 filedata->chains = NULL;
13812 return false;
13813 }
13814
13815 static bool
13816 process_syminfo (Filedata * filedata)
13817 {
13818 unsigned int i;
13819
13820 if (filedata->dynamic_syminfo == NULL
13821 || !do_dynamic)
13822 /* No syminfo, this is ok. */
13823 return true;
13824
13825 /* There better should be a dynamic symbol section. */
13826 if (filedata->dynamic_symbols == NULL || filedata->dynamic_strings == NULL)
13827 return false;
13828
13829 if (filedata->is_separate)
13830 printf (ngettext ("\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entry:\n",
13831 "\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entries:\n",
13832 filedata->dynamic_syminfo_nent),
13833 filedata->file_name,
13834 filedata->dynamic_syminfo_offset,
13835 filedata->dynamic_syminfo_nent);
13836 else
13837 printf (ngettext ("\nDynamic info segment at offset 0x%lx "
13838 "contains %d entry:\n",
13839 "\nDynamic info segment at offset 0x%lx "
13840 "contains %d entries:\n",
13841 filedata->dynamic_syminfo_nent),
13842 filedata->dynamic_syminfo_offset,
13843 filedata->dynamic_syminfo_nent);
13844
13845 printf (_(" Num: Name BoundTo Flags\n"));
13846 for (i = 0; i < filedata->dynamic_syminfo_nent; ++i)
13847 {
13848 unsigned short int flags = filedata->dynamic_syminfo[i].si_flags;
13849
13850 printf ("%4d: ", i);
13851 if (i >= filedata->num_dynamic_syms)
13852 printf (_("<corrupt index>"));
13853 else if (valid_dynamic_name (filedata, filedata->dynamic_symbols[i].st_name))
13854 print_symbol (30, get_dynamic_name (filedata,
13855 filedata->dynamic_symbols[i].st_name));
13856 else
13857 printf (_("<corrupt: %19ld>"), filedata->dynamic_symbols[i].st_name);
13858 putchar (' ');
13859
13860 switch (filedata->dynamic_syminfo[i].si_boundto)
13861 {
13862 case SYMINFO_BT_SELF:
13863 fputs ("SELF ", stdout);
13864 break;
13865 case SYMINFO_BT_PARENT:
13866 fputs ("PARENT ", stdout);
13867 break;
13868 default:
13869 if (filedata->dynamic_syminfo[i].si_boundto > 0
13870 && filedata->dynamic_syminfo[i].si_boundto < filedata->dynamic_nent
13871 && valid_dynamic_name (filedata,
13872 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val))
13873 {
13874 print_symbol (10, get_dynamic_name (filedata,
13875 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val));
13876 putchar (' ' );
13877 }
13878 else
13879 printf ("%-10d ", filedata->dynamic_syminfo[i].si_boundto);
13880 break;
13881 }
13882
13883 if (flags & SYMINFO_FLG_DIRECT)
13884 printf (" DIRECT");
13885 if (flags & SYMINFO_FLG_PASSTHRU)
13886 printf (" PASSTHRU");
13887 if (flags & SYMINFO_FLG_COPY)
13888 printf (" COPY");
13889 if (flags & SYMINFO_FLG_LAZYLOAD)
13890 printf (" LAZYLOAD");
13891
13892 puts ("");
13893 }
13894
13895 return true;
13896 }
13897
13898 /* A macro which evaluates to TRUE if the region ADDR .. ADDR + NELEM
13899 is contained by the region START .. END. The types of ADDR, START
13900 and END should all be the same. Note both ADDR + NELEM and END
13901 point to just beyond the end of the regions that are being tested. */
13902 #define IN_RANGE(START,END,ADDR,NELEM) \
13903 (((ADDR) >= (START)) && ((ADDR) < (END)) && ((ADDR) + (NELEM) <= (END)))
13904
13905 /* Check to see if the given reloc needs to be handled in a target specific
13906 manner. If so then process the reloc and return TRUE otherwise return
13907 FALSE.
13908
13909 If called with reloc == NULL, then this is a signal that reloc processing
13910 for the current section has finished, and any saved state should be
13911 discarded. */
13912
13913 static bool
13914 target_specific_reloc_handling (Filedata * filedata,
13915 Elf_Internal_Rela * reloc,
13916 unsigned char * start,
13917 unsigned char * end,
13918 Elf_Internal_Sym * symtab,
13919 unsigned long num_syms)
13920 {
13921 unsigned int reloc_type = 0;
13922 unsigned long sym_index = 0;
13923
13924 if (reloc)
13925 {
13926 reloc_type = get_reloc_type (filedata, reloc->r_info);
13927 sym_index = get_reloc_symindex (reloc->r_info);
13928 }
13929
13930 switch (filedata->file_header.e_machine)
13931 {
13932 case EM_MSP430:
13933 case EM_MSP430_OLD:
13934 {
13935 static Elf_Internal_Sym * saved_sym = NULL;
13936
13937 if (reloc == NULL)
13938 {
13939 saved_sym = NULL;
13940 return true;
13941 }
13942
13943 switch (reloc_type)
13944 {
13945 case 10: /* R_MSP430_SYM_DIFF */
13946 case 12: /* R_MSP430_GNU_SUB_ULEB128 */
13947 if (uses_msp430x_relocs (filedata))
13948 break;
13949 /* Fall through. */
13950 case 21: /* R_MSP430X_SYM_DIFF */
13951 case 23: /* R_MSP430X_GNU_SUB_ULEB128 */
13952 /* PR 21139. */
13953 if (sym_index >= num_syms)
13954 error (_("MSP430 SYM_DIFF reloc contains invalid symbol index %lu\n"),
13955 sym_index);
13956 else
13957 saved_sym = symtab + sym_index;
13958 return true;
13959
13960 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
13961 case 3: /* R_MSP430_16 or R_MSP430_ABS8 */
13962 goto handle_sym_diff;
13963
13964 case 5: /* R_MSP430_16_BYTE */
13965 case 9: /* R_MSP430_8 */
13966 case 11: /* R_MSP430_GNU_SET_ULEB128 */
13967 if (uses_msp430x_relocs (filedata))
13968 break;
13969 goto handle_sym_diff;
13970
13971 case 2: /* R_MSP430_ABS16 */
13972 case 15: /* R_MSP430X_ABS16 */
13973 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
13974 if (! uses_msp430x_relocs (filedata))
13975 break;
13976 goto handle_sym_diff;
13977
13978 handle_sym_diff:
13979 if (saved_sym != NULL)
13980 {
13981 bfd_vma value;
13982 unsigned int reloc_size = 0;
13983 int leb_ret = 0;
13984 switch (reloc_type)
13985 {
13986 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
13987 reloc_size = 4;
13988 break;
13989 case 11: /* R_MSP430_GNU_SET_ULEB128 */
13990 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
13991 if (reloc->r_offset < (size_t) (end - start))
13992 read_leb128 (start + reloc->r_offset, end, false,
13993 &reloc_size, &leb_ret);
13994 break;
13995 default:
13996 reloc_size = 2;
13997 break;
13998 }
13999
14000 if (leb_ret != 0 || reloc_size == 0 || reloc_size > 8)
14001 error (_("MSP430 ULEB128 field at 0x%lx contains invalid "
14002 "ULEB128 value\n"),
14003 (long) reloc->r_offset);
14004 else if (sym_index >= num_syms)
14005 error (_("MSP430 reloc contains invalid symbol index %lu\n"),
14006 sym_index);
14007 else
14008 {
14009 value = reloc->r_addend + (symtab[sym_index].st_value
14010 - saved_sym->st_value);
14011
14012 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
14013 byte_put (start + reloc->r_offset, value, reloc_size);
14014 else
14015 /* PR 21137 */
14016 error (_("MSP430 sym diff reloc contains invalid offset: 0x%lx\n"),
14017 (long) reloc->r_offset);
14018 }
14019
14020 saved_sym = NULL;
14021 return true;
14022 }
14023 break;
14024
14025 default:
14026 if (saved_sym != NULL)
14027 error (_("Unhandled MSP430 reloc type found after SYM_DIFF reloc\n"));
14028 break;
14029 }
14030 break;
14031 }
14032
14033 case EM_MN10300:
14034 case EM_CYGNUS_MN10300:
14035 {
14036 static Elf_Internal_Sym * saved_sym = NULL;
14037
14038 if (reloc == NULL)
14039 {
14040 saved_sym = NULL;
14041 return true;
14042 }
14043
14044 switch (reloc_type)
14045 {
14046 case 34: /* R_MN10300_ALIGN */
14047 return true;
14048 case 33: /* R_MN10300_SYM_DIFF */
14049 if (sym_index >= num_syms)
14050 error (_("MN10300_SYM_DIFF reloc contains invalid symbol index %lu\n"),
14051 sym_index);
14052 else
14053 saved_sym = symtab + sym_index;
14054 return true;
14055
14056 case 1: /* R_MN10300_32 */
14057 case 2: /* R_MN10300_16 */
14058 if (saved_sym != NULL)
14059 {
14060 int reloc_size = reloc_type == 1 ? 4 : 2;
14061 bfd_vma value;
14062
14063 if (sym_index >= num_syms)
14064 error (_("MN10300 reloc contains invalid symbol index %lu\n"),
14065 sym_index);
14066 else
14067 {
14068 value = reloc->r_addend + (symtab[sym_index].st_value
14069 - saved_sym->st_value);
14070
14071 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
14072 byte_put (start + reloc->r_offset, value, reloc_size);
14073 else
14074 error (_("MN10300 sym diff reloc contains invalid offset: 0x%lx\n"),
14075 (long) reloc->r_offset);
14076 }
14077
14078 saved_sym = NULL;
14079 return true;
14080 }
14081 break;
14082 default:
14083 if (saved_sym != NULL)
14084 error (_("Unhandled MN10300 reloc type found after SYM_DIFF reloc\n"));
14085 break;
14086 }
14087 break;
14088 }
14089
14090 case EM_RL78:
14091 {
14092 static bfd_vma saved_sym1 = 0;
14093 static bfd_vma saved_sym2 = 0;
14094 static bfd_vma value;
14095
14096 if (reloc == NULL)
14097 {
14098 saved_sym1 = saved_sym2 = 0;
14099 return true;
14100 }
14101
14102 switch (reloc_type)
14103 {
14104 case 0x80: /* R_RL78_SYM. */
14105 saved_sym1 = saved_sym2;
14106 if (sym_index >= num_syms)
14107 error (_("RL78_SYM reloc contains invalid symbol index %lu\n"),
14108 sym_index);
14109 else
14110 {
14111 saved_sym2 = symtab[sym_index].st_value;
14112 saved_sym2 += reloc->r_addend;
14113 }
14114 return true;
14115
14116 case 0x83: /* R_RL78_OPsub. */
14117 value = saved_sym1 - saved_sym2;
14118 saved_sym2 = saved_sym1 = 0;
14119 return true;
14120 break;
14121
14122 case 0x41: /* R_RL78_ABS32. */
14123 if (IN_RANGE (start, end, start + reloc->r_offset, 4))
14124 byte_put (start + reloc->r_offset, value, 4);
14125 else
14126 error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
14127 (long) reloc->r_offset);
14128 value = 0;
14129 return true;
14130
14131 case 0x43: /* R_RL78_ABS16. */
14132 if (IN_RANGE (start, end, start + reloc->r_offset, 2))
14133 byte_put (start + reloc->r_offset, value, 2);
14134 else
14135 error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
14136 (long) reloc->r_offset);
14137 value = 0;
14138 return true;
14139
14140 default:
14141 break;
14142 }
14143 break;
14144 }
14145 }
14146
14147 return false;
14148 }
14149
14150 /* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
14151 DWARF debug sections. This is a target specific test. Note - we do not
14152 go through the whole including-target-headers-multiple-times route, (as
14153 we have already done with <elf/h8.h>) because this would become very
14154 messy and even then this function would have to contain target specific
14155 information (the names of the relocs instead of their numeric values).
14156 FIXME: This is not the correct way to solve this problem. The proper way
14157 is to have target specific reloc sizing and typing functions created by
14158 the reloc-macros.h header, in the same way that it already creates the
14159 reloc naming functions. */
14160
14161 static bool
14162 is_32bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14163 {
14164 /* Please keep this table alpha-sorted for ease of visual lookup. */
14165 switch (filedata->file_header.e_machine)
14166 {
14167 case EM_386:
14168 case EM_IAMCU:
14169 return reloc_type == 1; /* R_386_32. */
14170 case EM_68K:
14171 return reloc_type == 1; /* R_68K_32. */
14172 case EM_860:
14173 return reloc_type == 1; /* R_860_32. */
14174 case EM_960:
14175 return reloc_type == 2; /* R_960_32. */
14176 case EM_AARCH64:
14177 return (reloc_type == 258
14178 || reloc_type == 1); /* R_AARCH64_ABS32 || R_AARCH64_P32_ABS32 */
14179 case EM_BPF:
14180 return reloc_type == 11; /* R_BPF_DATA_32 */
14181 case EM_ADAPTEVA_EPIPHANY:
14182 return reloc_type == 3;
14183 case EM_ALPHA:
14184 return reloc_type == 1; /* R_ALPHA_REFLONG. */
14185 case EM_ARC:
14186 return reloc_type == 1; /* R_ARC_32. */
14187 case EM_ARC_COMPACT:
14188 case EM_ARC_COMPACT2:
14189 return reloc_type == 4; /* R_ARC_32. */
14190 case EM_ARM:
14191 return reloc_type == 2; /* R_ARM_ABS32 */
14192 case EM_AVR_OLD:
14193 case EM_AVR:
14194 return reloc_type == 1;
14195 case EM_BLACKFIN:
14196 return reloc_type == 0x12; /* R_byte4_data. */
14197 case EM_CRIS:
14198 return reloc_type == 3; /* R_CRIS_32. */
14199 case EM_CR16:
14200 return reloc_type == 3; /* R_CR16_NUM32. */
14201 case EM_CRX:
14202 return reloc_type == 15; /* R_CRX_NUM32. */
14203 case EM_CSKY:
14204 return reloc_type == 1; /* R_CKCORE_ADDR32. */
14205 case EM_CYGNUS_FRV:
14206 return reloc_type == 1;
14207 case EM_CYGNUS_D10V:
14208 case EM_D10V:
14209 return reloc_type == 6; /* R_D10V_32. */
14210 case EM_CYGNUS_D30V:
14211 case EM_D30V:
14212 return reloc_type == 12; /* R_D30V_32_NORMAL. */
14213 case EM_DLX:
14214 return reloc_type == 3; /* R_DLX_RELOC_32. */
14215 case EM_CYGNUS_FR30:
14216 case EM_FR30:
14217 return reloc_type == 3; /* R_FR30_32. */
14218 case EM_FT32:
14219 return reloc_type == 1; /* R_FT32_32. */
14220 case EM_H8S:
14221 case EM_H8_300:
14222 case EM_H8_300H:
14223 return reloc_type == 1; /* R_H8_DIR32. */
14224 case EM_IA_64:
14225 return (reloc_type == 0x64 /* R_IA64_SECREL32MSB. */
14226 || reloc_type == 0x65 /* R_IA64_SECREL32LSB. */
14227 || reloc_type == 0x24 /* R_IA64_DIR32MSB. */
14228 || reloc_type == 0x25 /* R_IA64_DIR32LSB. */);
14229 case EM_IP2K_OLD:
14230 case EM_IP2K:
14231 return reloc_type == 2; /* R_IP2K_32. */
14232 case EM_IQ2000:
14233 return reloc_type == 2; /* R_IQ2000_32. */
14234 case EM_LATTICEMICO32:
14235 return reloc_type == 3; /* R_LM32_32. */
14236 case EM_LOONGARCH:
14237 return reloc_type == 1; /* R_LARCH_32. */
14238 case EM_M32C_OLD:
14239 case EM_M32C:
14240 return reloc_type == 3; /* R_M32C_32. */
14241 case EM_M32R:
14242 return reloc_type == 34; /* R_M32R_32_RELA. */
14243 case EM_68HC11:
14244 case EM_68HC12:
14245 return reloc_type == 6; /* R_M68HC11_32. */
14246 case EM_S12Z:
14247 return reloc_type == 7 || /* R_S12Z_EXT32 */
14248 reloc_type == 6; /* R_S12Z_CW32. */
14249 case EM_MCORE:
14250 return reloc_type == 1; /* R_MCORE_ADDR32. */
14251 case EM_CYGNUS_MEP:
14252 return reloc_type == 4; /* R_MEP_32. */
14253 case EM_METAG:
14254 return reloc_type == 2; /* R_METAG_ADDR32. */
14255 case EM_MICROBLAZE:
14256 return reloc_type == 1; /* R_MICROBLAZE_32. */
14257 case EM_MIPS:
14258 return reloc_type == 2; /* R_MIPS_32. */
14259 case EM_MMIX:
14260 return reloc_type == 4; /* R_MMIX_32. */
14261 case EM_CYGNUS_MN10200:
14262 case EM_MN10200:
14263 return reloc_type == 1; /* R_MN10200_32. */
14264 case EM_CYGNUS_MN10300:
14265 case EM_MN10300:
14266 return reloc_type == 1; /* R_MN10300_32. */
14267 case EM_MOXIE:
14268 return reloc_type == 1; /* R_MOXIE_32. */
14269 case EM_MSP430_OLD:
14270 case EM_MSP430:
14271 return reloc_type == 1; /* R_MSP430_32 or R_MSP320_ABS32. */
14272 case EM_MT:
14273 return reloc_type == 2; /* R_MT_32. */
14274 case EM_NDS32:
14275 return reloc_type == 20; /* R_NDS32_32_RELA. */
14276 case EM_ALTERA_NIOS2:
14277 return reloc_type == 12; /* R_NIOS2_BFD_RELOC_32. */
14278 case EM_NIOS32:
14279 return reloc_type == 1; /* R_NIOS_32. */
14280 case EM_OR1K:
14281 return reloc_type == 1; /* R_OR1K_32. */
14282 case EM_PARISC:
14283 return (reloc_type == 1 /* R_PARISC_DIR32. */
14284 || reloc_type == 2 /* R_PARISC_DIR21L. */
14285 || reloc_type == 41); /* R_PARISC_SECREL32. */
14286 case EM_PJ:
14287 case EM_PJ_OLD:
14288 return reloc_type == 1; /* R_PJ_DATA_DIR32. */
14289 case EM_PPC64:
14290 return reloc_type == 1; /* R_PPC64_ADDR32. */
14291 case EM_PPC:
14292 return reloc_type == 1; /* R_PPC_ADDR32. */
14293 case EM_TI_PRU:
14294 return reloc_type == 11; /* R_PRU_BFD_RELOC_32. */
14295 case EM_RISCV:
14296 return reloc_type == 1; /* R_RISCV_32. */
14297 case EM_RL78:
14298 return reloc_type == 1; /* R_RL78_DIR32. */
14299 case EM_RX:
14300 return reloc_type == 1; /* R_RX_DIR32. */
14301 case EM_S370:
14302 return reloc_type == 1; /* R_I370_ADDR31. */
14303 case EM_S390_OLD:
14304 case EM_S390:
14305 return reloc_type == 4; /* R_S390_32. */
14306 case EM_SCORE:
14307 return reloc_type == 8; /* R_SCORE_ABS32. */
14308 case EM_SH:
14309 return reloc_type == 1; /* R_SH_DIR32. */
14310 case EM_SPARC32PLUS:
14311 case EM_SPARCV9:
14312 case EM_SPARC:
14313 return reloc_type == 3 /* R_SPARC_32. */
14314 || reloc_type == 23; /* R_SPARC_UA32. */
14315 case EM_SPU:
14316 return reloc_type == 6; /* R_SPU_ADDR32 */
14317 case EM_TI_C6000:
14318 return reloc_type == 1; /* R_C6000_ABS32. */
14319 case EM_TILEGX:
14320 return reloc_type == 2; /* R_TILEGX_32. */
14321 case EM_TILEPRO:
14322 return reloc_type == 1; /* R_TILEPRO_32. */
14323 case EM_CYGNUS_V850:
14324 case EM_V850:
14325 return reloc_type == 6; /* R_V850_ABS32. */
14326 case EM_V800:
14327 return reloc_type == 0x33; /* R_V810_WORD. */
14328 case EM_VAX:
14329 return reloc_type == 1; /* R_VAX_32. */
14330 case EM_VISIUM:
14331 return reloc_type == 3; /* R_VISIUM_32. */
14332 case EM_WEBASSEMBLY:
14333 return reloc_type == 1; /* R_WASM32_32. */
14334 case EM_X86_64:
14335 case EM_L1OM:
14336 case EM_K1OM:
14337 return reloc_type == 10; /* R_X86_64_32. */
14338 case EM_XC16X:
14339 case EM_C166:
14340 return reloc_type == 3; /* R_XC16C_ABS_32. */
14341 case EM_XGATE:
14342 return reloc_type == 4; /* R_XGATE_32. */
14343 case EM_XSTORMY16:
14344 return reloc_type == 1; /* R_XSTROMY16_32. */
14345 case EM_XTENSA_OLD:
14346 case EM_XTENSA:
14347 return reloc_type == 1; /* R_XTENSA_32. */
14348 case EM_Z80:
14349 return reloc_type == 6; /* R_Z80_32. */
14350 default:
14351 {
14352 static unsigned int prev_warn = 0;
14353
14354 /* Avoid repeating the same warning multiple times. */
14355 if (prev_warn != filedata->file_header.e_machine)
14356 error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
14357 filedata->file_header.e_machine);
14358 prev_warn = filedata->file_header.e_machine;
14359 return false;
14360 }
14361 }
14362 }
14363
14364 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14365 a 32-bit pc-relative RELA relocation used in DWARF debug sections. */
14366
14367 static bool
14368 is_32bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
14369 {
14370 switch (filedata->file_header.e_machine)
14371 /* Please keep this table alpha-sorted for ease of visual lookup. */
14372 {
14373 case EM_386:
14374 case EM_IAMCU:
14375 return reloc_type == 2; /* R_386_PC32. */
14376 case EM_68K:
14377 return reloc_type == 4; /* R_68K_PC32. */
14378 case EM_AARCH64:
14379 return reloc_type == 261; /* R_AARCH64_PREL32 */
14380 case EM_ADAPTEVA_EPIPHANY:
14381 return reloc_type == 6;
14382 case EM_ALPHA:
14383 return reloc_type == 10; /* R_ALPHA_SREL32. */
14384 case EM_ARC_COMPACT:
14385 case EM_ARC_COMPACT2:
14386 return reloc_type == 49; /* R_ARC_32_PCREL. */
14387 case EM_ARM:
14388 return reloc_type == 3; /* R_ARM_REL32 */
14389 case EM_AVR_OLD:
14390 case EM_AVR:
14391 return reloc_type == 36; /* R_AVR_32_PCREL. */
14392 case EM_MICROBLAZE:
14393 return reloc_type == 2; /* R_MICROBLAZE_32_PCREL. */
14394 case EM_OR1K:
14395 return reloc_type == 9; /* R_OR1K_32_PCREL. */
14396 case EM_PARISC:
14397 return reloc_type == 9; /* R_PARISC_PCREL32. */
14398 case EM_PPC:
14399 return reloc_type == 26; /* R_PPC_REL32. */
14400 case EM_PPC64:
14401 return reloc_type == 26; /* R_PPC64_REL32. */
14402 case EM_RISCV:
14403 return reloc_type == 57; /* R_RISCV_32_PCREL. */
14404 case EM_S390_OLD:
14405 case EM_S390:
14406 return reloc_type == 5; /* R_390_PC32. */
14407 case EM_SH:
14408 return reloc_type == 2; /* R_SH_REL32. */
14409 case EM_SPARC32PLUS:
14410 case EM_SPARCV9:
14411 case EM_SPARC:
14412 return reloc_type == 6; /* R_SPARC_DISP32. */
14413 case EM_SPU:
14414 return reloc_type == 13; /* R_SPU_REL32. */
14415 case EM_TILEGX:
14416 return reloc_type == 6; /* R_TILEGX_32_PCREL. */
14417 case EM_TILEPRO:
14418 return reloc_type == 4; /* R_TILEPRO_32_PCREL. */
14419 case EM_VISIUM:
14420 return reloc_type == 6; /* R_VISIUM_32_PCREL */
14421 case EM_X86_64:
14422 case EM_L1OM:
14423 case EM_K1OM:
14424 return reloc_type == 2; /* R_X86_64_PC32. */
14425 case EM_VAX:
14426 return reloc_type == 4; /* R_VAX_PCREL32. */
14427 case EM_XTENSA_OLD:
14428 case EM_XTENSA:
14429 return reloc_type == 14; /* R_XTENSA_32_PCREL. */
14430 default:
14431 /* Do not abort or issue an error message here. Not all targets use
14432 pc-relative 32-bit relocs in their DWARF debug information and we
14433 have already tested for target coverage in is_32bit_abs_reloc. A
14434 more helpful warning message will be generated by apply_relocations
14435 anyway, so just return. */
14436 return false;
14437 }
14438 }
14439
14440 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14441 a 64-bit absolute RELA relocation used in DWARF debug sections. */
14442
14443 static bool
14444 is_64bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14445 {
14446 switch (filedata->file_header.e_machine)
14447 {
14448 case EM_AARCH64:
14449 return reloc_type == 257; /* R_AARCH64_ABS64. */
14450 case EM_ALPHA:
14451 return reloc_type == 2; /* R_ALPHA_REFQUAD. */
14452 case EM_IA_64:
14453 return (reloc_type == 0x26 /* R_IA64_DIR64MSB. */
14454 || reloc_type == 0x27 /* R_IA64_DIR64LSB. */);
14455 case EM_LOONGARCH:
14456 return reloc_type == 2; /* R_LARCH_64 */
14457 case EM_PARISC:
14458 return reloc_type == 80; /* R_PARISC_DIR64. */
14459 case EM_PPC64:
14460 return reloc_type == 38; /* R_PPC64_ADDR64. */
14461 case EM_RISCV:
14462 return reloc_type == 2; /* R_RISCV_64. */
14463 case EM_SPARC32PLUS:
14464 case EM_SPARCV9:
14465 case EM_SPARC:
14466 return reloc_type == 32 /* R_SPARC_64. */
14467 || reloc_type == 54; /* R_SPARC_UA64. */
14468 case EM_X86_64:
14469 case EM_L1OM:
14470 case EM_K1OM:
14471 return reloc_type == 1; /* R_X86_64_64. */
14472 case EM_S390_OLD:
14473 case EM_S390:
14474 return reloc_type == 22; /* R_S390_64. */
14475 case EM_TILEGX:
14476 return reloc_type == 1; /* R_TILEGX_64. */
14477 case EM_MIPS:
14478 return reloc_type == 18; /* R_MIPS_64. */
14479 default:
14480 return false;
14481 }
14482 }
14483
14484 /* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
14485 a 64-bit pc-relative RELA relocation used in DWARF debug sections. */
14486
14487 static bool
14488 is_64bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
14489 {
14490 switch (filedata->file_header.e_machine)
14491 {
14492 case EM_AARCH64:
14493 return reloc_type == 260; /* R_AARCH64_PREL64. */
14494 case EM_ALPHA:
14495 return reloc_type == 11; /* R_ALPHA_SREL64. */
14496 case EM_IA_64:
14497 return (reloc_type == 0x4e /* R_IA64_PCREL64MSB. */
14498 || reloc_type == 0x4f /* R_IA64_PCREL64LSB. */);
14499 case EM_PARISC:
14500 return reloc_type == 72; /* R_PARISC_PCREL64. */
14501 case EM_PPC64:
14502 return reloc_type == 44; /* R_PPC64_REL64. */
14503 case EM_SPARC32PLUS:
14504 case EM_SPARCV9:
14505 case EM_SPARC:
14506 return reloc_type == 46; /* R_SPARC_DISP64. */
14507 case EM_X86_64:
14508 case EM_L1OM:
14509 case EM_K1OM:
14510 return reloc_type == 24; /* R_X86_64_PC64. */
14511 case EM_S390_OLD:
14512 case EM_S390:
14513 return reloc_type == 23; /* R_S390_PC64. */
14514 case EM_TILEGX:
14515 return reloc_type == 5; /* R_TILEGX_64_PCREL. */
14516 default:
14517 return false;
14518 }
14519 }
14520
14521 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14522 a 24-bit absolute RELA relocation used in DWARF debug sections. */
14523
14524 static bool
14525 is_24bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14526 {
14527 switch (filedata->file_header.e_machine)
14528 {
14529 case EM_CYGNUS_MN10200:
14530 case EM_MN10200:
14531 return reloc_type == 4; /* R_MN10200_24. */
14532 case EM_FT32:
14533 return reloc_type == 5; /* R_FT32_20. */
14534 case EM_Z80:
14535 return reloc_type == 5; /* R_Z80_24. */
14536 default:
14537 return false;
14538 }
14539 }
14540
14541 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14542 a 16-bit absolute RELA relocation used in DWARF debug sections. */
14543
14544 static bool
14545 is_16bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14546 {
14547 /* Please keep this table alpha-sorted for ease of visual lookup. */
14548 switch (filedata->file_header.e_machine)
14549 {
14550 case EM_ARC:
14551 case EM_ARC_COMPACT:
14552 case EM_ARC_COMPACT2:
14553 return reloc_type == 2; /* R_ARC_16. */
14554 case EM_ADAPTEVA_EPIPHANY:
14555 return reloc_type == 5;
14556 case EM_AVR_OLD:
14557 case EM_AVR:
14558 return reloc_type == 4; /* R_AVR_16. */
14559 case EM_CYGNUS_D10V:
14560 case EM_D10V:
14561 return reloc_type == 3; /* R_D10V_16. */
14562 case EM_FT32:
14563 return reloc_type == 2; /* R_FT32_16. */
14564 case EM_H8S:
14565 case EM_H8_300:
14566 case EM_H8_300H:
14567 return reloc_type == R_H8_DIR16;
14568 case EM_IP2K_OLD:
14569 case EM_IP2K:
14570 return reloc_type == 1; /* R_IP2K_16. */
14571 case EM_M32C_OLD:
14572 case EM_M32C:
14573 return reloc_type == 1; /* R_M32C_16 */
14574 case EM_CYGNUS_MN10200:
14575 case EM_MN10200:
14576 return reloc_type == 2; /* R_MN10200_16. */
14577 case EM_CYGNUS_MN10300:
14578 case EM_MN10300:
14579 return reloc_type == 2; /* R_MN10300_16. */
14580 case EM_MSP430:
14581 if (uses_msp430x_relocs (filedata))
14582 return reloc_type == 2; /* R_MSP430_ABS16. */
14583 /* Fall through. */
14584 case EM_MSP430_OLD:
14585 return reloc_type == 5; /* R_MSP430_16_BYTE. */
14586 case EM_NDS32:
14587 return reloc_type == 19; /* R_NDS32_16_RELA. */
14588 case EM_ALTERA_NIOS2:
14589 return reloc_type == 13; /* R_NIOS2_BFD_RELOC_16. */
14590 case EM_NIOS32:
14591 return reloc_type == 9; /* R_NIOS_16. */
14592 case EM_OR1K:
14593 return reloc_type == 2; /* R_OR1K_16. */
14594 case EM_RISCV:
14595 return reloc_type == 55; /* R_RISCV_SET16. */
14596 case EM_TI_PRU:
14597 return reloc_type == 8; /* R_PRU_BFD_RELOC_16. */
14598 case EM_TI_C6000:
14599 return reloc_type == 2; /* R_C6000_ABS16. */
14600 case EM_VISIUM:
14601 return reloc_type == 2; /* R_VISIUM_16. */
14602 case EM_XC16X:
14603 case EM_C166:
14604 return reloc_type == 2; /* R_XC16C_ABS_16. */
14605 case EM_XGATE:
14606 return reloc_type == 3; /* R_XGATE_16. */
14607 case EM_Z80:
14608 return reloc_type == 4; /* R_Z80_16. */
14609 default:
14610 return false;
14611 }
14612 }
14613
14614 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14615 a 8-bit absolute RELA relocation used in DWARF debug sections. */
14616
14617 static bool
14618 is_8bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14619 {
14620 switch (filedata->file_header.e_machine)
14621 {
14622 case EM_RISCV:
14623 return reloc_type == 54; /* R_RISCV_SET8. */
14624 case EM_Z80:
14625 return reloc_type == 1; /* R_Z80_8. */
14626 default:
14627 return false;
14628 }
14629 }
14630
14631 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14632 a 6-bit absolute RELA relocation used in DWARF debug sections. */
14633
14634 static bool
14635 is_6bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14636 {
14637 switch (filedata->file_header.e_machine)
14638 {
14639 case EM_RISCV:
14640 return reloc_type == 53; /* R_RISCV_SET6. */
14641 default:
14642 return false;
14643 }
14644 }
14645
14646 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14647 a 32-bit inplace add RELA relocation used in DWARF debug sections. */
14648
14649 static bool
14650 is_32bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14651 {
14652 /* Please keep this table alpha-sorted for ease of visual lookup. */
14653 switch (filedata->file_header.e_machine)
14654 {
14655 case EM_RISCV:
14656 return reloc_type == 35; /* R_RISCV_ADD32. */
14657 default:
14658 return false;
14659 }
14660 }
14661
14662 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14663 a 32-bit inplace sub RELA relocation used in DWARF debug sections. */
14664
14665 static bool
14666 is_32bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14667 {
14668 /* Please keep this table alpha-sorted for ease of visual lookup. */
14669 switch (filedata->file_header.e_machine)
14670 {
14671 case EM_RISCV:
14672 return reloc_type == 39; /* R_RISCV_SUB32. */
14673 default:
14674 return false;
14675 }
14676 }
14677
14678 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14679 a 64-bit inplace add RELA relocation used in DWARF debug sections. */
14680
14681 static bool
14682 is_64bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14683 {
14684 /* Please keep this table alpha-sorted for ease of visual lookup. */
14685 switch (filedata->file_header.e_machine)
14686 {
14687 case EM_RISCV:
14688 return reloc_type == 36; /* R_RISCV_ADD64. */
14689 default:
14690 return false;
14691 }
14692 }
14693
14694 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14695 a 64-bit inplace sub RELA relocation used in DWARF debug sections. */
14696
14697 static bool
14698 is_64bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14699 {
14700 /* Please keep this table alpha-sorted for ease of visual lookup. */
14701 switch (filedata->file_header.e_machine)
14702 {
14703 case EM_RISCV:
14704 return reloc_type == 40; /* R_RISCV_SUB64. */
14705 default:
14706 return false;
14707 }
14708 }
14709
14710 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14711 a 16-bit inplace add RELA relocation used in DWARF debug sections. */
14712
14713 static bool
14714 is_16bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14715 {
14716 /* Please keep this table alpha-sorted for ease of visual lookup. */
14717 switch (filedata->file_header.e_machine)
14718 {
14719 case EM_RISCV:
14720 return reloc_type == 34; /* R_RISCV_ADD16. */
14721 default:
14722 return false;
14723 }
14724 }
14725
14726 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14727 a 16-bit inplace sub RELA relocation used in DWARF debug sections. */
14728
14729 static bool
14730 is_16bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14731 {
14732 /* Please keep this table alpha-sorted for ease of visual lookup. */
14733 switch (filedata->file_header.e_machine)
14734 {
14735 case EM_RISCV:
14736 return reloc_type == 38; /* R_RISCV_SUB16. */
14737 default:
14738 return false;
14739 }
14740 }
14741
14742 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14743 a 8-bit inplace add RELA relocation used in DWARF debug sections. */
14744
14745 static bool
14746 is_8bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14747 {
14748 /* Please keep this table alpha-sorted for ease of visual lookup. */
14749 switch (filedata->file_header.e_machine)
14750 {
14751 case EM_RISCV:
14752 return reloc_type == 33; /* R_RISCV_ADD8. */
14753 default:
14754 return false;
14755 }
14756 }
14757
14758 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14759 a 8-bit inplace sub RELA relocation used in DWARF debug sections. */
14760
14761 static bool
14762 is_8bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14763 {
14764 /* Please keep this table alpha-sorted for ease of visual lookup. */
14765 switch (filedata->file_header.e_machine)
14766 {
14767 case EM_RISCV:
14768 return reloc_type == 37; /* R_RISCV_SUB8. */
14769 default:
14770 return false;
14771 }
14772 }
14773
14774 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14775 a 6-bit inplace sub RELA relocation used in DWARF debug sections. */
14776
14777 static bool
14778 is_6bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14779 {
14780 switch (filedata->file_header.e_machine)
14781 {
14782 case EM_RISCV:
14783 return reloc_type == 52; /* R_RISCV_SUB6. */
14784 default:
14785 return false;
14786 }
14787 }
14788
14789 /* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
14790 relocation entries (possibly formerly used for SHT_GROUP sections). */
14791
14792 static bool
14793 is_none_reloc (Filedata * filedata, unsigned int reloc_type)
14794 {
14795 switch (filedata->file_header.e_machine)
14796 {
14797 case EM_386: /* R_386_NONE. */
14798 case EM_68K: /* R_68K_NONE. */
14799 case EM_ADAPTEVA_EPIPHANY:
14800 case EM_ALPHA: /* R_ALPHA_NONE. */
14801 case EM_ALTERA_NIOS2: /* R_NIOS2_NONE. */
14802 case EM_ARC: /* R_ARC_NONE. */
14803 case EM_ARC_COMPACT2: /* R_ARC_NONE. */
14804 case EM_ARC_COMPACT: /* R_ARC_NONE. */
14805 case EM_ARM: /* R_ARM_NONE. */
14806 case EM_C166: /* R_XC16X_NONE. */
14807 case EM_CRIS: /* R_CRIS_NONE. */
14808 case EM_FT32: /* R_FT32_NONE. */
14809 case EM_IA_64: /* R_IA64_NONE. */
14810 case EM_K1OM: /* R_X86_64_NONE. */
14811 case EM_L1OM: /* R_X86_64_NONE. */
14812 case EM_M32R: /* R_M32R_NONE. */
14813 case EM_MIPS: /* R_MIPS_NONE. */
14814 case EM_MN10300: /* R_MN10300_NONE. */
14815 case EM_MOXIE: /* R_MOXIE_NONE. */
14816 case EM_NIOS32: /* R_NIOS_NONE. */
14817 case EM_OR1K: /* R_OR1K_NONE. */
14818 case EM_PARISC: /* R_PARISC_NONE. */
14819 case EM_PPC64: /* R_PPC64_NONE. */
14820 case EM_PPC: /* R_PPC_NONE. */
14821 case EM_RISCV: /* R_RISCV_NONE. */
14822 case EM_S390: /* R_390_NONE. */
14823 case EM_S390_OLD:
14824 case EM_SH: /* R_SH_NONE. */
14825 case EM_SPARC32PLUS:
14826 case EM_SPARC: /* R_SPARC_NONE. */
14827 case EM_SPARCV9:
14828 case EM_TILEGX: /* R_TILEGX_NONE. */
14829 case EM_TILEPRO: /* R_TILEPRO_NONE. */
14830 case EM_TI_C6000:/* R_C6000_NONE. */
14831 case EM_X86_64: /* R_X86_64_NONE. */
14832 case EM_XC16X:
14833 case EM_Z80: /* R_Z80_NONE. */
14834 case EM_WEBASSEMBLY: /* R_WASM32_NONE. */
14835 return reloc_type == 0;
14836
14837 case EM_AARCH64:
14838 return reloc_type == 0 || reloc_type == 256;
14839 case EM_AVR_OLD:
14840 case EM_AVR:
14841 return (reloc_type == 0 /* R_AVR_NONE. */
14842 || reloc_type == 30 /* R_AVR_DIFF8. */
14843 || reloc_type == 31 /* R_AVR_DIFF16. */
14844 || reloc_type == 32 /* R_AVR_DIFF32. */);
14845 case EM_METAG:
14846 return reloc_type == 3; /* R_METAG_NONE. */
14847 case EM_NDS32:
14848 return (reloc_type == 0 /* R_NDS32_NONE. */
14849 || reloc_type == 205 /* R_NDS32_DIFF8. */
14850 || reloc_type == 206 /* R_NDS32_DIFF16. */
14851 || reloc_type == 207 /* R_NDS32_DIFF32. */
14852 || reloc_type == 208 /* R_NDS32_DIFF_ULEB128. */);
14853 case EM_TI_PRU:
14854 return (reloc_type == 0 /* R_PRU_NONE. */
14855 || reloc_type == 65 /* R_PRU_DIFF8. */
14856 || reloc_type == 66 /* R_PRU_DIFF16. */
14857 || reloc_type == 67 /* R_PRU_DIFF32. */);
14858 case EM_XTENSA_OLD:
14859 case EM_XTENSA:
14860 return (reloc_type == 0 /* R_XTENSA_NONE. */
14861 || reloc_type == 17 /* R_XTENSA_DIFF8. */
14862 || reloc_type == 18 /* R_XTENSA_DIFF16. */
14863 || reloc_type == 19 /* R_XTENSA_DIFF32. */
14864 || reloc_type == 57 /* R_XTENSA_PDIFF8. */
14865 || reloc_type == 58 /* R_XTENSA_PDIFF16. */
14866 || reloc_type == 59 /* R_XTENSA_PDIFF32. */
14867 || reloc_type == 60 /* R_XTENSA_NDIFF8. */
14868 || reloc_type == 61 /* R_XTENSA_NDIFF16. */
14869 || reloc_type == 62 /* R_XTENSA_NDIFF32. */);
14870 }
14871 return false;
14872 }
14873
14874 /* Returns TRUE if there is a relocation against
14875 section NAME at OFFSET bytes. */
14876
14877 bool
14878 reloc_at (struct dwarf_section * dsec, dwarf_vma offset)
14879 {
14880 Elf_Internal_Rela * relocs;
14881 Elf_Internal_Rela * rp;
14882
14883 if (dsec == NULL || dsec->reloc_info == NULL)
14884 return false;
14885
14886 relocs = (Elf_Internal_Rela *) dsec->reloc_info;
14887
14888 for (rp = relocs; rp < relocs + dsec->num_relocs; ++rp)
14889 if (rp->r_offset == offset)
14890 return true;
14891
14892 return false;
14893 }
14894
14895 /* Apply relocations to a section.
14896 Returns TRUE upon success, FALSE otherwise.
14897 If RELOCS_RETURN is non-NULL then it is set to point to the loaded relocs.
14898 It is then the caller's responsibility to free them. NUM_RELOCS_RETURN
14899 will be set to the number of relocs loaded.
14900
14901 Note: So far support has been added only for those relocations
14902 which can be found in debug sections. FIXME: Add support for
14903 more relocations ? */
14904
14905 static bool
14906 apply_relocations (Filedata * filedata,
14907 const Elf_Internal_Shdr * section,
14908 unsigned char * start,
14909 bfd_size_type size,
14910 void ** relocs_return,
14911 unsigned long * num_relocs_return)
14912 {
14913 Elf_Internal_Shdr * relsec;
14914 unsigned char * end = start + size;
14915
14916 if (relocs_return != NULL)
14917 {
14918 * (Elf_Internal_Rela **) relocs_return = NULL;
14919 * num_relocs_return = 0;
14920 }
14921
14922 if (filedata->file_header.e_type != ET_REL)
14923 /* No relocs to apply. */
14924 return true;
14925
14926 /* Find the reloc section associated with the section. */
14927 for (relsec = filedata->section_headers;
14928 relsec < filedata->section_headers + filedata->file_header.e_shnum;
14929 ++relsec)
14930 {
14931 bool is_rela;
14932 unsigned long num_relocs;
14933 Elf_Internal_Rela * relocs;
14934 Elf_Internal_Rela * rp;
14935 Elf_Internal_Shdr * symsec;
14936 Elf_Internal_Sym * symtab;
14937 unsigned long num_syms;
14938 Elf_Internal_Sym * sym;
14939
14940 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
14941 || relsec->sh_info >= filedata->file_header.e_shnum
14942 || filedata->section_headers + relsec->sh_info != section
14943 || relsec->sh_size == 0
14944 || relsec->sh_link >= filedata->file_header.e_shnum)
14945 continue;
14946
14947 symsec = filedata->section_headers + relsec->sh_link;
14948 if (symsec->sh_type != SHT_SYMTAB
14949 && symsec->sh_type != SHT_DYNSYM)
14950 return false;
14951
14952 is_rela = relsec->sh_type == SHT_RELA;
14953
14954 if (is_rela)
14955 {
14956 if (!slurp_rela_relocs (filedata, relsec->sh_offset,
14957 relsec->sh_size, & relocs, & num_relocs))
14958 return false;
14959 }
14960 else
14961 {
14962 if (!slurp_rel_relocs (filedata, relsec->sh_offset,
14963 relsec->sh_size, & relocs, & num_relocs))
14964 return false;
14965 }
14966
14967 /* SH uses RELA but uses in place value instead of the addend field. */
14968 if (filedata->file_header.e_machine == EM_SH)
14969 is_rela = false;
14970
14971 symtab = get_elf_symbols (filedata, symsec, & num_syms);
14972
14973 for (rp = relocs; rp < relocs + num_relocs; ++rp)
14974 {
14975 bfd_vma addend;
14976 unsigned int reloc_type;
14977 unsigned int reloc_size;
14978 bool reloc_inplace = false;
14979 bool reloc_subtract = false;
14980 unsigned char *rloc;
14981 unsigned long sym_index;
14982
14983 reloc_type = get_reloc_type (filedata, rp->r_info);
14984
14985 if (target_specific_reloc_handling (filedata, rp, start, end, symtab, num_syms))
14986 continue;
14987 else if (is_none_reloc (filedata, reloc_type))
14988 continue;
14989 else if (is_32bit_abs_reloc (filedata, reloc_type)
14990 || is_32bit_pcrel_reloc (filedata, reloc_type))
14991 reloc_size = 4;
14992 else if (is_64bit_abs_reloc (filedata, reloc_type)
14993 || is_64bit_pcrel_reloc (filedata, reloc_type))
14994 reloc_size = 8;
14995 else if (is_24bit_abs_reloc (filedata, reloc_type))
14996 reloc_size = 3;
14997 else if (is_16bit_abs_reloc (filedata, reloc_type))
14998 reloc_size = 2;
14999 else if (is_8bit_abs_reloc (filedata, reloc_type)
15000 || is_6bit_abs_reloc (filedata, reloc_type))
15001 reloc_size = 1;
15002 else if ((reloc_subtract = is_32bit_inplace_sub_reloc (filedata,
15003 reloc_type))
15004 || is_32bit_inplace_add_reloc (filedata, reloc_type))
15005 {
15006 reloc_size = 4;
15007 reloc_inplace = true;
15008 }
15009 else if ((reloc_subtract = is_64bit_inplace_sub_reloc (filedata,
15010 reloc_type))
15011 || is_64bit_inplace_add_reloc (filedata, reloc_type))
15012 {
15013 reloc_size = 8;
15014 reloc_inplace = true;
15015 }
15016 else if ((reloc_subtract = is_16bit_inplace_sub_reloc (filedata,
15017 reloc_type))
15018 || is_16bit_inplace_add_reloc (filedata, reloc_type))
15019 {
15020 reloc_size = 2;
15021 reloc_inplace = true;
15022 }
15023 else if ((reloc_subtract = is_8bit_inplace_sub_reloc (filedata,
15024 reloc_type))
15025 || is_8bit_inplace_add_reloc (filedata, reloc_type))
15026 {
15027 reloc_size = 1;
15028 reloc_inplace = true;
15029 }
15030 else if ((reloc_subtract = is_6bit_inplace_sub_reloc (filedata,
15031 reloc_type)))
15032 {
15033 reloc_size = 1;
15034 reloc_inplace = true;
15035 }
15036 else
15037 {
15038 static unsigned int prev_reloc = 0;
15039
15040 if (reloc_type != prev_reloc)
15041 warn (_("unable to apply unsupported reloc type %d to section %s\n"),
15042 reloc_type, printable_section_name (filedata, section));
15043 prev_reloc = reloc_type;
15044 continue;
15045 }
15046
15047 rloc = start + rp->r_offset;
15048 if (!IN_RANGE (start, end, rloc, reloc_size))
15049 {
15050 warn (_("skipping invalid relocation offset 0x%lx in section %s\n"),
15051 (unsigned long) rp->r_offset,
15052 printable_section_name (filedata, section));
15053 continue;
15054 }
15055
15056 sym_index = (unsigned long) get_reloc_symindex (rp->r_info);
15057 if (sym_index >= num_syms)
15058 {
15059 warn (_("skipping invalid relocation symbol index 0x%lx in section %s\n"),
15060 sym_index, printable_section_name (filedata, section));
15061 continue;
15062 }
15063 sym = symtab + sym_index;
15064
15065 /* If the reloc has a symbol associated with it,
15066 make sure that it is of an appropriate type.
15067
15068 Relocations against symbols without type can happen.
15069 Gcc -feliminate-dwarf2-dups may generate symbols
15070 without type for debug info.
15071
15072 Icc generates relocations against function symbols
15073 instead of local labels.
15074
15075 Relocations against object symbols can happen, eg when
15076 referencing a global array. For an example of this see
15077 the _clz.o binary in libgcc.a. */
15078 if (sym != symtab
15079 && ELF_ST_TYPE (sym->st_info) != STT_COMMON
15080 && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
15081 {
15082 warn (_("skipping unexpected symbol type %s in section %s relocation %ld\n"),
15083 get_symbol_type (filedata, ELF_ST_TYPE (sym->st_info)),
15084 printable_section_name (filedata, relsec),
15085 (long int)(rp - relocs));
15086 continue;
15087 }
15088
15089 addend = 0;
15090 if (is_rela)
15091 addend += rp->r_addend;
15092 /* R_XTENSA_32, R_PJ_DATA_DIR32 and R_D30V_32_NORMAL are
15093 partial_inplace. */
15094 if (!is_rela
15095 || (filedata->file_header.e_machine == EM_XTENSA
15096 && reloc_type == 1)
15097 || ((filedata->file_header.e_machine == EM_PJ
15098 || filedata->file_header.e_machine == EM_PJ_OLD)
15099 && reloc_type == 1)
15100 || ((filedata->file_header.e_machine == EM_D30V
15101 || filedata->file_header.e_machine == EM_CYGNUS_D30V)
15102 && reloc_type == 12)
15103 || reloc_inplace)
15104 {
15105 if (is_6bit_inplace_sub_reloc (filedata, reloc_type))
15106 addend += byte_get (rloc, reloc_size) & 0x3f;
15107 else
15108 addend += byte_get (rloc, reloc_size);
15109 }
15110
15111 if (is_32bit_pcrel_reloc (filedata, reloc_type)
15112 || is_64bit_pcrel_reloc (filedata, reloc_type))
15113 {
15114 /* On HPPA, all pc-relative relocations are biased by 8. */
15115 if (filedata->file_header.e_machine == EM_PARISC)
15116 addend -= 8;
15117 byte_put (rloc, (addend + sym->st_value) - rp->r_offset,
15118 reloc_size);
15119 }
15120 else if (is_6bit_abs_reloc (filedata, reloc_type)
15121 || is_6bit_inplace_sub_reloc (filedata, reloc_type))
15122 {
15123 if (reloc_subtract)
15124 addend -= sym->st_value;
15125 else
15126 addend += sym->st_value;
15127 addend = (addend & 0x3f) | (byte_get (rloc, reloc_size) & 0xc0);
15128 byte_put (rloc, addend, reloc_size);
15129 }
15130 else if (reloc_subtract)
15131 byte_put (rloc, addend - sym->st_value, reloc_size);
15132 else
15133 byte_put (rloc, addend + sym->st_value, reloc_size);
15134 }
15135
15136 free (symtab);
15137 /* Let the target specific reloc processing code know that
15138 we have finished with these relocs. */
15139 target_specific_reloc_handling (filedata, NULL, NULL, NULL, NULL, 0);
15140
15141 if (relocs_return)
15142 {
15143 * (Elf_Internal_Rela **) relocs_return = relocs;
15144 * num_relocs_return = num_relocs;
15145 }
15146 else
15147 free (relocs);
15148
15149 break;
15150 }
15151
15152 return true;
15153 }
15154
15155 #ifdef SUPPORT_DISASSEMBLY
15156 static bool
15157 disassemble_section (Elf_Internal_Shdr * section, Filedata * filedata)
15158 {
15159 printf (_("\nAssembly dump of section %s\n"), printable_section_name (filedata, section));
15160
15161 /* FIXME: XXX -- to be done --- XXX */
15162
15163 return true;
15164 }
15165 #endif
15166
15167 /* Reads in the contents of SECTION from FILE, returning a pointer
15168 to a malloc'ed buffer or NULL if something went wrong. */
15169
15170 static char *
15171 get_section_contents (Elf_Internal_Shdr * section, Filedata * filedata)
15172 {
15173 bfd_size_type num_bytes = section->sh_size;
15174
15175 if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
15176 {
15177 printf (_("Section '%s' has no data to dump.\n"),
15178 printable_section_name (filedata, section));
15179 return NULL;
15180 }
15181
15182 return (char *) get_data (NULL, filedata, section->sh_offset, 1, num_bytes,
15183 _("section contents"));
15184 }
15185
15186 /* Uncompresses a section that was compressed using zlib, in place. */
15187
15188 static bool
15189 uncompress_section_contents (unsigned char ** buffer,
15190 dwarf_size_type uncompressed_size,
15191 dwarf_size_type * size)
15192 {
15193 dwarf_size_type compressed_size = *size;
15194 unsigned char * compressed_buffer = *buffer;
15195 unsigned char * uncompressed_buffer;
15196 z_stream strm;
15197 int rc;
15198
15199 /* It is possible the section consists of several compressed
15200 buffers concatenated together, so we uncompress in a loop. */
15201 /* PR 18313: The state field in the z_stream structure is supposed
15202 to be invisible to the user (ie us), but some compilers will
15203 still complain about it being used without initialisation. So
15204 we first zero the entire z_stream structure and then set the fields
15205 that we need. */
15206 memset (& strm, 0, sizeof strm);
15207 strm.avail_in = compressed_size;
15208 strm.next_in = (Bytef *) compressed_buffer;
15209 strm.avail_out = uncompressed_size;
15210 uncompressed_buffer = (unsigned char *) xmalloc (uncompressed_size);
15211
15212 rc = inflateInit (& strm);
15213 while (strm.avail_in > 0)
15214 {
15215 if (rc != Z_OK)
15216 break;
15217 strm.next_out = ((Bytef *) uncompressed_buffer
15218 + (uncompressed_size - strm.avail_out));
15219 rc = inflate (&strm, Z_FINISH);
15220 if (rc != Z_STREAM_END)
15221 break;
15222 rc = inflateReset (& strm);
15223 }
15224 if (inflateEnd (& strm) != Z_OK
15225 || rc != Z_OK
15226 || strm.avail_out != 0)
15227 goto fail;
15228
15229 *buffer = uncompressed_buffer;
15230 *size = uncompressed_size;
15231 return true;
15232
15233 fail:
15234 free (uncompressed_buffer);
15235 /* Indicate decompression failure. */
15236 *buffer = NULL;
15237 return false;
15238 }
15239
15240 static bool
15241 dump_section_as_strings (Elf_Internal_Shdr * section, Filedata * filedata)
15242 {
15243 Elf_Internal_Shdr *relsec;
15244 bfd_size_type num_bytes;
15245 unsigned char *data;
15246 unsigned char *end;
15247 unsigned char *real_start;
15248 unsigned char *start;
15249 bool some_strings_shown;
15250
15251 real_start = start = (unsigned char *) get_section_contents (section, filedata);
15252 if (start == NULL)
15253 /* PR 21820: Do not fail if the section was empty. */
15254 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
15255
15256 num_bytes = section->sh_size;
15257
15258 if (filedata->is_separate)
15259 printf (_("\nString dump of section '%s' in linked file %s:\n"),
15260 printable_section_name (filedata, section),
15261 filedata->file_name);
15262 else
15263 printf (_("\nString dump of section '%s':\n"),
15264 printable_section_name (filedata, section));
15265
15266 if (decompress_dumps)
15267 {
15268 dwarf_size_type new_size = num_bytes;
15269 dwarf_size_type uncompressed_size = 0;
15270
15271 if ((section->sh_flags & SHF_COMPRESSED) != 0)
15272 {
15273 Elf_Internal_Chdr chdr;
15274 unsigned int compression_header_size
15275 = get_compression_header (& chdr, (unsigned char *) start,
15276 num_bytes);
15277 if (compression_header_size == 0)
15278 /* An error message will have already been generated
15279 by get_compression_header. */
15280 goto error_out;
15281
15282 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
15283 {
15284 warn (_("section '%s' has unsupported compress type: %d\n"),
15285 printable_section_name (filedata, section), chdr.ch_type);
15286 goto error_out;
15287 }
15288 uncompressed_size = chdr.ch_size;
15289 start += compression_header_size;
15290 new_size -= compression_header_size;
15291 }
15292 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
15293 {
15294 /* Read the zlib header. In this case, it should be "ZLIB"
15295 followed by the uncompressed section size, 8 bytes in
15296 big-endian order. */
15297 uncompressed_size = start[4]; uncompressed_size <<= 8;
15298 uncompressed_size += start[5]; uncompressed_size <<= 8;
15299 uncompressed_size += start[6]; uncompressed_size <<= 8;
15300 uncompressed_size += start[7]; uncompressed_size <<= 8;
15301 uncompressed_size += start[8]; uncompressed_size <<= 8;
15302 uncompressed_size += start[9]; uncompressed_size <<= 8;
15303 uncompressed_size += start[10]; uncompressed_size <<= 8;
15304 uncompressed_size += start[11];
15305 start += 12;
15306 new_size -= 12;
15307 }
15308
15309 if (uncompressed_size)
15310 {
15311 if (uncompress_section_contents (& start,
15312 uncompressed_size, & new_size))
15313 num_bytes = new_size;
15314 else
15315 {
15316 error (_("Unable to decompress section %s\n"),
15317 printable_section_name (filedata, section));
15318 goto error_out;
15319 }
15320 }
15321 else
15322 start = real_start;
15323 }
15324
15325 /* If the section being dumped has relocations against it the user might
15326 be expecting these relocations to have been applied. Check for this
15327 case and issue a warning message in order to avoid confusion.
15328 FIXME: Maybe we ought to have an option that dumps a section with
15329 relocs applied ? */
15330 for (relsec = filedata->section_headers;
15331 relsec < filedata->section_headers + filedata->file_header.e_shnum;
15332 ++relsec)
15333 {
15334 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
15335 || relsec->sh_info >= filedata->file_header.e_shnum
15336 || filedata->section_headers + relsec->sh_info != section
15337 || relsec->sh_size == 0
15338 || relsec->sh_link >= filedata->file_header.e_shnum)
15339 continue;
15340
15341 printf (_(" Note: This section has relocations against it, but these have NOT been applied to this dump.\n"));
15342 break;
15343 }
15344
15345 data = start;
15346 end = start + num_bytes;
15347 some_strings_shown = false;
15348
15349 #ifdef HAVE_MBSTATE_T
15350 mbstate_t state;
15351 /* Initialise the multibyte conversion state. */
15352 memset (& state, 0, sizeof (state));
15353 #endif
15354
15355 bool continuing = false;
15356
15357 while (data < end)
15358 {
15359 while (!ISPRINT (* data))
15360 if (++ data >= end)
15361 break;
15362
15363 if (data < end)
15364 {
15365 size_t maxlen = end - data;
15366
15367 if (continuing)
15368 {
15369 printf (" ");
15370 continuing = false;
15371 }
15372 else
15373 {
15374 printf (" [%6lx] ", (unsigned long) (data - start));
15375 }
15376
15377 if (maxlen > 0)
15378 {
15379 char c = 0;
15380
15381 while (maxlen)
15382 {
15383 c = *data++;
15384
15385 if (c == 0)
15386 break;
15387
15388 /* PR 25543: Treat new-lines as string-ending characters. */
15389 if (c == '\n')
15390 {
15391 printf ("\\n\n");
15392 if (*data != 0)
15393 continuing = true;
15394 break;
15395 }
15396
15397 /* Do not print control characters directly as they can affect terminal
15398 settings. Such characters usually appear in the names generated
15399 by the assembler for local labels. */
15400 if (ISCNTRL (c))
15401 {
15402 printf ("^%c", c + 0x40);
15403 }
15404 else if (ISPRINT (c))
15405 {
15406 putchar (c);
15407 }
15408 else
15409 {
15410 size_t n;
15411 #ifdef HAVE_MBSTATE_T
15412 wchar_t w;
15413 #endif
15414 /* Let printf do the hard work of displaying multibyte characters. */
15415 printf ("%.1s", data - 1);
15416 #ifdef HAVE_MBSTATE_T
15417 /* Try to find out how many bytes made up the character that was
15418 just printed. Advance the symbol pointer past the bytes that
15419 were displayed. */
15420 n = mbrtowc (& w, (char *)(data - 1), MB_CUR_MAX, & state);
15421 #else
15422 n = 1;
15423 #endif
15424 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
15425 data += (n - 1);
15426 }
15427 }
15428
15429 if (c != '\n')
15430 putchar ('\n');
15431 }
15432 else
15433 {
15434 printf (_("<corrupt>\n"));
15435 data = end;
15436 }
15437 some_strings_shown = true;
15438 }
15439 }
15440
15441 if (! some_strings_shown)
15442 printf (_(" No strings found in this section."));
15443
15444 free (real_start);
15445
15446 putchar ('\n');
15447 return true;
15448
15449 error_out:
15450 free (real_start);
15451 return false;
15452 }
15453
15454 static bool
15455 dump_section_as_bytes (Elf_Internal_Shdr *section,
15456 Filedata *filedata,
15457 bool relocate)
15458 {
15459 Elf_Internal_Shdr * relsec;
15460 bfd_size_type bytes;
15461 bfd_size_type section_size;
15462 bfd_vma addr;
15463 unsigned char * data;
15464 unsigned char * real_start;
15465 unsigned char * start;
15466
15467 real_start = start = (unsigned char *) get_section_contents (section, filedata);
15468 if (start == NULL)
15469 /* PR 21820: Do not fail if the section was empty. */
15470 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
15471
15472 section_size = section->sh_size;
15473
15474 if (filedata->is_separate)
15475 printf (_("\nHex dump of section '%s' in linked file %s:\n"),
15476 printable_section_name (filedata, section),
15477 filedata->file_name);
15478 else
15479 printf (_("\nHex dump of section '%s':\n"),
15480 printable_section_name (filedata, section));
15481
15482 if (decompress_dumps)
15483 {
15484 dwarf_size_type new_size = section_size;
15485 dwarf_size_type uncompressed_size = 0;
15486
15487 if ((section->sh_flags & SHF_COMPRESSED) != 0)
15488 {
15489 Elf_Internal_Chdr chdr;
15490 unsigned int compression_header_size
15491 = get_compression_header (& chdr, start, section_size);
15492
15493 if (compression_header_size == 0)
15494 /* An error message will have already been generated
15495 by get_compression_header. */
15496 goto error_out;
15497
15498 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
15499 {
15500 warn (_("section '%s' has unsupported compress type: %d\n"),
15501 printable_section_name (filedata, section), chdr.ch_type);
15502 goto error_out;
15503 }
15504 uncompressed_size = chdr.ch_size;
15505 start += compression_header_size;
15506 new_size -= compression_header_size;
15507 }
15508 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
15509 {
15510 /* Read the zlib header. In this case, it should be "ZLIB"
15511 followed by the uncompressed section size, 8 bytes in
15512 big-endian order. */
15513 uncompressed_size = start[4]; uncompressed_size <<= 8;
15514 uncompressed_size += start[5]; uncompressed_size <<= 8;
15515 uncompressed_size += start[6]; uncompressed_size <<= 8;
15516 uncompressed_size += start[7]; uncompressed_size <<= 8;
15517 uncompressed_size += start[8]; uncompressed_size <<= 8;
15518 uncompressed_size += start[9]; uncompressed_size <<= 8;
15519 uncompressed_size += start[10]; uncompressed_size <<= 8;
15520 uncompressed_size += start[11];
15521 start += 12;
15522 new_size -= 12;
15523 }
15524
15525 if (uncompressed_size)
15526 {
15527 if (uncompress_section_contents (& start, uncompressed_size,
15528 & new_size))
15529 {
15530 section_size = new_size;
15531 }
15532 else
15533 {
15534 error (_("Unable to decompress section %s\n"),
15535 printable_section_name (filedata, section));
15536 /* FIXME: Print the section anyway ? */
15537 goto error_out;
15538 }
15539 }
15540 else
15541 start = real_start;
15542 }
15543
15544 if (relocate)
15545 {
15546 if (! apply_relocations (filedata, section, start, section_size, NULL, NULL))
15547 goto error_out;
15548 }
15549 else
15550 {
15551 /* If the section being dumped has relocations against it the user might
15552 be expecting these relocations to have been applied. Check for this
15553 case and issue a warning message in order to avoid confusion.
15554 FIXME: Maybe we ought to have an option that dumps a section with
15555 relocs applied ? */
15556 for (relsec = filedata->section_headers;
15557 relsec < filedata->section_headers + filedata->file_header.e_shnum;
15558 ++relsec)
15559 {
15560 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
15561 || relsec->sh_info >= filedata->file_header.e_shnum
15562 || filedata->section_headers + relsec->sh_info != section
15563 || relsec->sh_size == 0
15564 || relsec->sh_link >= filedata->file_header.e_shnum)
15565 continue;
15566
15567 printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
15568 break;
15569 }
15570 }
15571
15572 addr = section->sh_addr;
15573 bytes = section_size;
15574 data = start;
15575
15576 while (bytes)
15577 {
15578 int j;
15579 int k;
15580 int lbytes;
15581
15582 lbytes = (bytes > 16 ? 16 : bytes);
15583
15584 printf (" 0x%8.8lx ", (unsigned long) addr);
15585
15586 for (j = 0; j < 16; j++)
15587 {
15588 if (j < lbytes)
15589 printf ("%2.2x", data[j]);
15590 else
15591 printf (" ");
15592
15593 if ((j & 3) == 3)
15594 printf (" ");
15595 }
15596
15597 for (j = 0; j < lbytes; j++)
15598 {
15599 k = data[j];
15600 if (k >= ' ' && k < 0x7f)
15601 printf ("%c", k);
15602 else
15603 printf (".");
15604 }
15605
15606 putchar ('\n');
15607
15608 data += lbytes;
15609 addr += lbytes;
15610 bytes -= lbytes;
15611 }
15612
15613 free (real_start);
15614
15615 putchar ('\n');
15616 return true;
15617
15618 error_out:
15619 free (real_start);
15620 return false;
15621 }
15622
15623 #ifdef ENABLE_LIBCTF
15624 static ctf_sect_t *
15625 shdr_to_ctf_sect (ctf_sect_t *buf, Elf_Internal_Shdr *shdr, Filedata *filedata)
15626 {
15627 buf->cts_name = section_name_print (filedata, shdr);
15628 buf->cts_size = shdr->sh_size;
15629 buf->cts_entsize = shdr->sh_entsize;
15630
15631 return buf;
15632 }
15633
15634 /* Formatting callback function passed to ctf_dump. Returns either the pointer
15635 it is passed, or a pointer to newly-allocated storage, in which case
15636 dump_ctf() will free it when it no longer needs it. */
15637
15638 static char *
15639 dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
15640 char *s, void *arg)
15641 {
15642 const char *blanks = arg;
15643 char *new_s;
15644
15645 if (asprintf (&new_s, "%s%s", blanks, s) < 0)
15646 return s;
15647 return new_s;
15648 }
15649
15650 /* Dump CTF errors/warnings. */
15651 static void
15652 dump_ctf_errs (ctf_dict_t *fp)
15653 {
15654 ctf_next_t *it = NULL;
15655 char *errtext;
15656 int is_warning;
15657 int err;
15658
15659 /* Dump accumulated errors and warnings. */
15660 while ((errtext = ctf_errwarning_next (fp, &it, &is_warning, &err)) != NULL)
15661 {
15662 error (_("%s: %s"), is_warning ? _("warning"): _("error"),
15663 errtext);
15664 free (errtext);
15665 }
15666 if (err != ECTF_NEXT_END)
15667 error (_("CTF error: cannot get CTF errors: `%s'"), ctf_errmsg (err));
15668 }
15669
15670 /* Dump one CTF archive member. */
15671
15672 static void
15673 dump_ctf_archive_member (ctf_dict_t *ctf, const char *name, ctf_dict_t *parent,
15674 size_t member)
15675 {
15676 const char *things[] = {"Header", "Labels", "Data objects",
15677 "Function objects", "Variables", "Types", "Strings",
15678 ""};
15679 const char **thing;
15680 size_t i;
15681
15682 /* Don't print out the name of the default-named archive member if it appears
15683 first in the list. The name .ctf appears everywhere, even for things that
15684 aren't really archives, so printing it out is liable to be confusing; also,
15685 the common case by far is for only one archive member to exist, and hiding
15686 it in that case seems worthwhile. */
15687
15688 if (strcmp (name, ".ctf") != 0 || member != 0)
15689 printf (_("\nCTF archive member: %s:\n"), name);
15690
15691 if (ctf_parent_name (ctf) != NULL)
15692 ctf_import (ctf, parent);
15693
15694 for (i = 0, thing = things; *thing[0]; thing++, i++)
15695 {
15696 ctf_dump_state_t *s = NULL;
15697 char *item;
15698
15699 printf ("\n %s:\n", *thing);
15700 while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
15701 (void *) " ")) != NULL)
15702 {
15703 printf ("%s\n", item);
15704 free (item);
15705 }
15706
15707 if (ctf_errno (ctf))
15708 {
15709 error (_("Iteration failed: %s, %s\n"), *thing,
15710 ctf_errmsg (ctf_errno (ctf)));
15711 break;
15712 }
15713 }
15714
15715 dump_ctf_errs (ctf);
15716 }
15717
15718 static bool
15719 dump_section_as_ctf (Elf_Internal_Shdr * section, Filedata * filedata)
15720 {
15721 Elf_Internal_Shdr * symtab_sec = NULL;
15722 Elf_Internal_Shdr * strtab_sec = NULL;
15723 void * data = NULL;
15724 void * symdata = NULL;
15725 void * strdata = NULL;
15726 ctf_sect_t ctfsect, symsect, strsect;
15727 ctf_sect_t * symsectp = NULL;
15728 ctf_sect_t * strsectp = NULL;
15729 ctf_archive_t * ctfa = NULL;
15730 ctf_dict_t * parent = NULL;
15731 ctf_dict_t * fp;
15732
15733 ctf_next_t *i = NULL;
15734 const char *name;
15735 size_t member = 0;
15736 int err;
15737 bool ret = false;
15738
15739 shdr_to_ctf_sect (&ctfsect, section, filedata);
15740 data = get_section_contents (section, filedata);
15741 ctfsect.cts_data = data;
15742
15743 if (!dump_ctf_symtab_name)
15744 dump_ctf_symtab_name = strdup (".dynsym");
15745
15746 if (!dump_ctf_strtab_name)
15747 dump_ctf_strtab_name = strdup (".dynstr");
15748
15749 if (dump_ctf_symtab_name && dump_ctf_symtab_name[0] != 0)
15750 {
15751 if ((symtab_sec = find_section (filedata, dump_ctf_symtab_name)) == NULL)
15752 {
15753 error (_("No symbol section named %s\n"), dump_ctf_symtab_name);
15754 goto fail;
15755 }
15756 if ((symdata = (void *) get_data (NULL, filedata,
15757 symtab_sec->sh_offset, 1,
15758 symtab_sec->sh_size,
15759 _("symbols"))) == NULL)
15760 goto fail;
15761 symsectp = shdr_to_ctf_sect (&symsect, symtab_sec, filedata);
15762 symsect.cts_data = symdata;
15763 }
15764
15765 if (dump_ctf_strtab_name && dump_ctf_strtab_name[0] != 0)
15766 {
15767 if ((strtab_sec = find_section (filedata, dump_ctf_strtab_name)) == NULL)
15768 {
15769 error (_("No string table section named %s\n"),
15770 dump_ctf_strtab_name);
15771 goto fail;
15772 }
15773 if ((strdata = (void *) get_data (NULL, filedata,
15774 strtab_sec->sh_offset, 1,
15775 strtab_sec->sh_size,
15776 _("strings"))) == NULL)
15777 goto fail;
15778 strsectp = shdr_to_ctf_sect (&strsect, strtab_sec, filedata);
15779 strsect.cts_data = strdata;
15780 }
15781
15782 /* Load the CTF file and dump it. It may be a raw CTF section, or an archive:
15783 libctf papers over the difference, so we can pretend it is always an
15784 archive. */
15785
15786 if ((ctfa = ctf_arc_bufopen (&ctfsect, symsectp, strsectp, &err)) == NULL)
15787 {
15788 dump_ctf_errs (NULL);
15789 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15790 goto fail;
15791 }
15792
15793 ctf_arc_symsect_endianness (ctfa, filedata->file_header.e_ident[EI_DATA]
15794 != ELFDATA2MSB);
15795
15796 /* Preload the parent dict, since it will need to be imported into every
15797 child in turn. */
15798 if ((parent = ctf_dict_open (ctfa, dump_ctf_parent_name, &err)) == NULL)
15799 {
15800 dump_ctf_errs (NULL);
15801 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15802 goto fail;
15803 }
15804
15805 ret = true;
15806
15807 if (filedata->is_separate)
15808 printf (_("\nDump of CTF section '%s' in linked file %s:\n"),
15809 printable_section_name (filedata, section),
15810 filedata->file_name);
15811 else
15812 printf (_("\nDump of CTF section '%s':\n"),
15813 printable_section_name (filedata, section));
15814
15815 while ((fp = ctf_archive_next (ctfa, &i, &name, 0, &err)) != NULL)
15816 dump_ctf_archive_member (fp, name, parent, member++);
15817 if (err != ECTF_NEXT_END)
15818 {
15819 dump_ctf_errs (NULL);
15820 error (_("CTF member open failure: %s\n"), ctf_errmsg (err));
15821 ret = false;
15822 }
15823
15824 fail:
15825 ctf_dict_close (parent);
15826 ctf_close (ctfa);
15827 free (data);
15828 free (symdata);
15829 free (strdata);
15830 return ret;
15831 }
15832 #endif
15833
15834 static bool
15835 load_specific_debug_section (enum dwarf_section_display_enum debug,
15836 const Elf_Internal_Shdr * sec,
15837 void * data)
15838 {
15839 struct dwarf_section * section = &debug_displays [debug].section;
15840 char buf [64];
15841 Filedata * filedata = (Filedata *) data;
15842
15843 if (section->start != NULL)
15844 {
15845 /* If it is already loaded, do nothing. */
15846 if (streq (section->filename, filedata->file_name))
15847 return true;
15848 free (section->start);
15849 }
15850
15851 snprintf (buf, sizeof (buf), _("%s section data"), section->name);
15852 section->address = sec->sh_addr;
15853 section->filename = filedata->file_name;
15854 section->start = (unsigned char *) get_data (NULL, filedata,
15855 sec->sh_offset, 1,
15856 sec->sh_size, buf);
15857 if (section->start == NULL)
15858 section->size = 0;
15859 else
15860 {
15861 unsigned char *start = section->start;
15862 dwarf_size_type size = sec->sh_size;
15863 dwarf_size_type uncompressed_size = 0;
15864
15865 if ((sec->sh_flags & SHF_COMPRESSED) != 0)
15866 {
15867 Elf_Internal_Chdr chdr;
15868 unsigned int compression_header_size;
15869
15870 if (size < (is_32bit_elf
15871 ? sizeof (Elf32_External_Chdr)
15872 : sizeof (Elf64_External_Chdr)))
15873 {
15874 warn (_("compressed section %s is too small to contain a compression header\n"),
15875 section->name);
15876 return false;
15877 }
15878
15879 compression_header_size = get_compression_header (&chdr, start, size);
15880 if (compression_header_size == 0)
15881 /* An error message will have already been generated
15882 by get_compression_header. */
15883 return false;
15884
15885 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
15886 {
15887 warn (_("section '%s' has unsupported compress type: %d\n"),
15888 section->name, chdr.ch_type);
15889 return false;
15890 }
15891 uncompressed_size = chdr.ch_size;
15892 start += compression_header_size;
15893 size -= compression_header_size;
15894 }
15895 else if (size > 12 && streq ((char *) start, "ZLIB"))
15896 {
15897 /* Read the zlib header. In this case, it should be "ZLIB"
15898 followed by the uncompressed section size, 8 bytes in
15899 big-endian order. */
15900 uncompressed_size = start[4]; uncompressed_size <<= 8;
15901 uncompressed_size += start[5]; uncompressed_size <<= 8;
15902 uncompressed_size += start[6]; uncompressed_size <<= 8;
15903 uncompressed_size += start[7]; uncompressed_size <<= 8;
15904 uncompressed_size += start[8]; uncompressed_size <<= 8;
15905 uncompressed_size += start[9]; uncompressed_size <<= 8;
15906 uncompressed_size += start[10]; uncompressed_size <<= 8;
15907 uncompressed_size += start[11];
15908 start += 12;
15909 size -= 12;
15910 }
15911
15912 if (uncompressed_size)
15913 {
15914 if (uncompress_section_contents (&start, uncompressed_size,
15915 &size))
15916 {
15917 /* Free the compressed buffer, update the section buffer
15918 and the section size if uncompress is successful. */
15919 free (section->start);
15920 section->start = start;
15921 }
15922 else
15923 {
15924 error (_("Unable to decompress section %s\n"),
15925 printable_section_name (filedata, sec));
15926 return false;
15927 }
15928 }
15929
15930 section->size = size;
15931 }
15932
15933 if (section->start == NULL)
15934 return false;
15935
15936 if (debug_displays [debug].relocate)
15937 {
15938 if (! apply_relocations (filedata, sec, section->start, section->size,
15939 & section->reloc_info, & section->num_relocs))
15940 return false;
15941 }
15942 else
15943 {
15944 section->reloc_info = NULL;
15945 section->num_relocs = 0;
15946 }
15947
15948 return true;
15949 }
15950
15951 #if HAVE_LIBDEBUGINFOD
15952 /* Return a hex string representation of the build-id. */
15953 unsigned char *
15954 get_build_id (void * data)
15955 {
15956 Filedata * filedata = (Filedata *) data;
15957 Elf_Internal_Shdr * shdr;
15958 unsigned long i;
15959
15960 /* Iterate through notes to find note.gnu.build-id.
15961 FIXME: Only the first note in any note section is examined. */
15962 for (i = 0, shdr = filedata->section_headers;
15963 i < filedata->file_header.e_shnum && shdr != NULL;
15964 i++, shdr++)
15965 {
15966 if (shdr->sh_type != SHT_NOTE)
15967 continue;
15968
15969 char * next;
15970 char * end;
15971 size_t data_remaining;
15972 size_t min_notesz;
15973 Elf_External_Note * enote;
15974 Elf_Internal_Note inote;
15975
15976 bfd_vma offset = shdr->sh_offset;
15977 bfd_vma align = shdr->sh_addralign;
15978 bfd_vma length = shdr->sh_size;
15979
15980 enote = (Elf_External_Note *) get_section_contents (shdr, filedata);
15981 if (enote == NULL)
15982 continue;
15983
15984 if (align < 4)
15985 align = 4;
15986 else if (align != 4 && align != 8)
15987 {
15988 free (enote);
15989 continue;
15990 }
15991
15992 end = (char *) enote + length;
15993 data_remaining = end - (char *) enote;
15994
15995 if (!is_ia64_vms (filedata))
15996 {
15997 min_notesz = offsetof (Elf_External_Note, name);
15998 if (data_remaining < min_notesz)
15999 {
16000 warn (_("\
16001 malformed note encountered in section %s whilst scanning for build-id note\n"),
16002 printable_section_name (filedata, shdr));
16003 free (enote);
16004 continue;
16005 }
16006 data_remaining -= min_notesz;
16007
16008 inote.type = BYTE_GET (enote->type);
16009 inote.namesz = BYTE_GET (enote->namesz);
16010 inote.namedata = enote->name;
16011 inote.descsz = BYTE_GET (enote->descsz);
16012 inote.descdata = ((char *) enote
16013 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
16014 inote.descpos = offset + (inote.descdata - (char *) enote);
16015 next = ((char *) enote
16016 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
16017 }
16018 else
16019 {
16020 Elf64_External_VMS_Note *vms_enote;
16021
16022 /* PR binutils/15191
16023 Make sure that there is enough data to read. */
16024 min_notesz = offsetof (Elf64_External_VMS_Note, name);
16025 if (data_remaining < min_notesz)
16026 {
16027 warn (_("\
16028 malformed note encountered in section %s whilst scanning for build-id note\n"),
16029 printable_section_name (filedata, shdr));
16030 free (enote);
16031 continue;
16032 }
16033 data_remaining -= min_notesz;
16034
16035 vms_enote = (Elf64_External_VMS_Note *) enote;
16036 inote.type = BYTE_GET (vms_enote->type);
16037 inote.namesz = BYTE_GET (vms_enote->namesz);
16038 inote.namedata = vms_enote->name;
16039 inote.descsz = BYTE_GET (vms_enote->descsz);
16040 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
16041 inote.descpos = offset + (inote.descdata - (char *) enote);
16042 next = inote.descdata + align_power (inote.descsz, 3);
16043 }
16044
16045 /* Skip malformed notes. */
16046 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
16047 || (size_t) (inote.descdata - inote.namedata) > data_remaining
16048 || (size_t) (next - inote.descdata) < inote.descsz
16049 || ((size_t) (next - inote.descdata)
16050 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
16051 {
16052 warn (_("\
16053 malformed note encountered in section %s whilst scanning for build-id note\n"),
16054 printable_section_name (filedata, shdr));
16055 free (enote);
16056 continue;
16057 }
16058
16059 /* Check if this is the build-id note. If so then convert the build-id
16060 bytes to a hex string. */
16061 if (inote.namesz > 0
16062 && startswith (inote.namedata, "GNU")
16063 && inote.type == NT_GNU_BUILD_ID)
16064 {
16065 unsigned long j;
16066 char * build_id;
16067
16068 build_id = malloc (inote.descsz * 2 + 1);
16069 if (build_id == NULL)
16070 {
16071 free (enote);
16072 return NULL;
16073 }
16074
16075 for (j = 0; j < inote.descsz; ++j)
16076 sprintf (build_id + (j * 2), "%02x", inote.descdata[j] & 0xff);
16077 build_id[inote.descsz * 2] = '\0';
16078 free (enote);
16079
16080 return (unsigned char *) build_id;
16081 }
16082 free (enote);
16083 }
16084
16085 return NULL;
16086 }
16087 #endif /* HAVE_LIBDEBUGINFOD */
16088
16089 /* If this is not NULL, load_debug_section will only look for sections
16090 within the list of sections given here. */
16091 static unsigned int * section_subset = NULL;
16092
16093 bool
16094 load_debug_section (enum dwarf_section_display_enum debug, void * data)
16095 {
16096 struct dwarf_section * section = &debug_displays [debug].section;
16097 Elf_Internal_Shdr * sec;
16098 Filedata * filedata = (Filedata *) data;
16099
16100 if (!dump_any_debugging)
16101 return false;
16102
16103 /* Without section headers we cannot find any sections. */
16104 if (filedata->section_headers == NULL)
16105 return false;
16106
16107 if (filedata->string_table == NULL
16108 && filedata->file_header.e_shstrndx != SHN_UNDEF
16109 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
16110 {
16111 Elf_Internal_Shdr * strs;
16112
16113 /* Read in the string table, so that we have section names to scan. */
16114 strs = filedata->section_headers + filedata->file_header.e_shstrndx;
16115
16116 if (strs != NULL && strs->sh_size != 0)
16117 {
16118 filedata->string_table
16119 = (char *) get_data (NULL, filedata, strs->sh_offset,
16120 1, strs->sh_size, _("string table"));
16121
16122 filedata->string_table_length
16123 = filedata->string_table != NULL ? strs->sh_size : 0;
16124 }
16125 }
16126
16127 /* Locate the debug section. */
16128 sec = find_section_in_set (filedata, section->uncompressed_name, section_subset);
16129 if (sec != NULL)
16130 section->name = section->uncompressed_name;
16131 else
16132 {
16133 sec = find_section_in_set (filedata, section->compressed_name, section_subset);
16134 if (sec != NULL)
16135 section->name = section->compressed_name;
16136 }
16137 if (sec == NULL)
16138 return false;
16139
16140 /* If we're loading from a subset of sections, and we've loaded
16141 a section matching this name before, it's likely that it's a
16142 different one. */
16143 if (section_subset != NULL)
16144 free_debug_section (debug);
16145
16146 return load_specific_debug_section (debug, sec, data);
16147 }
16148
16149 void
16150 free_debug_section (enum dwarf_section_display_enum debug)
16151 {
16152 struct dwarf_section * section = &debug_displays [debug].section;
16153
16154 if (section->start == NULL)
16155 return;
16156
16157 free ((char *) section->start);
16158 section->start = NULL;
16159 section->address = 0;
16160 section->size = 0;
16161
16162 free (section->reloc_info);
16163 section->reloc_info = NULL;
16164 section->num_relocs = 0;
16165 }
16166
16167 static bool
16168 display_debug_section (int shndx, Elf_Internal_Shdr * section, Filedata * filedata)
16169 {
16170 const char *name = (section_name_valid (filedata, section)
16171 ? section_name (filedata, section) : "");
16172 const char *print_name = printable_section_name (filedata, section);
16173 bfd_size_type length;
16174 bool result = true;
16175 int i;
16176
16177 length = section->sh_size;
16178 if (length == 0)
16179 {
16180 printf (_("\nSection '%s' has no debugging data.\n"), print_name);
16181 return true;
16182 }
16183 if (section->sh_type == SHT_NOBITS)
16184 {
16185 /* There is no point in dumping the contents of a debugging section
16186 which has the NOBITS type - the bits in the file will be random.
16187 This can happen when a file containing a .eh_frame section is
16188 stripped with the --only-keep-debug command line option. */
16189 printf (_("section '%s' has the NOBITS type - its contents are unreliable.\n"),
16190 print_name);
16191 return false;
16192 }
16193
16194 if (startswith (name, ".gnu.linkonce.wi."))
16195 name = ".debug_info";
16196
16197 /* See if we know how to display the contents of this section. */
16198 for (i = 0; i < max; i++)
16199 {
16200 enum dwarf_section_display_enum id = (enum dwarf_section_display_enum) i;
16201 struct dwarf_section_display * display = debug_displays + i;
16202 struct dwarf_section * sec = & display->section;
16203
16204 if (streq (sec->uncompressed_name, name)
16205 || (id == line && startswith (name, ".debug_line."))
16206 || streq (sec->compressed_name, name))
16207 {
16208 bool secondary = (section != find_section (filedata, name));
16209
16210 if (secondary)
16211 free_debug_section (id);
16212
16213 if (i == line && startswith (name, ".debug_line."))
16214 sec->name = name;
16215 else if (streq (sec->uncompressed_name, name))
16216 sec->name = sec->uncompressed_name;
16217 else
16218 sec->name = sec->compressed_name;
16219
16220 if (load_specific_debug_section (id, section, filedata))
16221 {
16222 /* If this debug section is part of a CU/TU set in a .dwp file,
16223 restrict load_debug_section to the sections in that set. */
16224 section_subset = find_cu_tu_set (filedata, shndx);
16225
16226 result &= display->display (sec, filedata);
16227
16228 section_subset = NULL;
16229
16230 if (secondary || (id != info && id != abbrev && id != debug_addr))
16231 free_debug_section (id);
16232 }
16233 break;
16234 }
16235 }
16236
16237 if (i == max)
16238 {
16239 printf (_("Unrecognized debug section: %s\n"), print_name);
16240 result = false;
16241 }
16242
16243 return result;
16244 }
16245
16246 /* Set DUMP_SECTS for all sections where dumps were requested
16247 based on section name. */
16248
16249 static void
16250 initialise_dumps_byname (Filedata * filedata)
16251 {
16252 struct dump_list_entry * cur;
16253
16254 for (cur = dump_sects_byname; cur; cur = cur->next)
16255 {
16256 unsigned int i;
16257 bool any = false;
16258
16259 for (i = 0; i < filedata->file_header.e_shnum; i++)
16260 if (section_name_valid (filedata, filedata->section_headers + i)
16261 && streq (section_name (filedata, filedata->section_headers + i),
16262 cur->name))
16263 {
16264 request_dump_bynumber (&filedata->dump, i, cur->type);
16265 any = true;
16266 }
16267
16268 if (!any && !filedata->is_separate)
16269 warn (_("Section '%s' was not dumped because it does not exist\n"),
16270 cur->name);
16271 }
16272 }
16273
16274 static bool
16275 process_section_contents (Filedata * filedata)
16276 {
16277 Elf_Internal_Shdr * section;
16278 unsigned int i;
16279 bool res = true;
16280
16281 if (! do_dump)
16282 return true;
16283
16284 initialise_dumps_byname (filedata);
16285
16286 for (i = 0, section = filedata->section_headers;
16287 i < filedata->file_header.e_shnum && i < filedata->dump.num_dump_sects;
16288 i++, section++)
16289 {
16290 dump_type dump = filedata->dump.dump_sects[i];
16291
16292 if (filedata->is_separate && ! process_links)
16293 dump &= DEBUG_DUMP;
16294
16295 #ifdef SUPPORT_DISASSEMBLY
16296 if (dump & DISASS_DUMP)
16297 {
16298 if (! disassemble_section (section, filedata))
16299 res = false;
16300 }
16301 #endif
16302 if (dump & HEX_DUMP)
16303 {
16304 if (! dump_section_as_bytes (section, filedata, false))
16305 res = false;
16306 }
16307
16308 if (dump & RELOC_DUMP)
16309 {
16310 if (! dump_section_as_bytes (section, filedata, true))
16311 res = false;
16312 }
16313
16314 if (dump & STRING_DUMP)
16315 {
16316 if (! dump_section_as_strings (section, filedata))
16317 res = false;
16318 }
16319
16320 if (dump & DEBUG_DUMP)
16321 {
16322 if (! display_debug_section (i, section, filedata))
16323 res = false;
16324 }
16325
16326 #ifdef ENABLE_LIBCTF
16327 if (dump & CTF_DUMP)
16328 {
16329 if (! dump_section_as_ctf (section, filedata))
16330 res = false;
16331 }
16332 #endif
16333 }
16334
16335 if (! filedata->is_separate)
16336 {
16337 /* Check to see if the user requested a
16338 dump of a section that does not exist. */
16339 for (; i < filedata->dump.num_dump_sects; i++)
16340 if (filedata->dump.dump_sects[i])
16341 {
16342 warn (_("Section %d was not dumped because it does not exist!\n"), i);
16343 res = false;
16344 }
16345 }
16346
16347 return res;
16348 }
16349
16350 static void
16351 process_mips_fpe_exception (int mask)
16352 {
16353 if (mask)
16354 {
16355 bool first = true;
16356
16357 if (mask & OEX_FPU_INEX)
16358 fputs ("INEX", stdout), first = false;
16359 if (mask & OEX_FPU_UFLO)
16360 printf ("%sUFLO", first ? "" : "|"), first = false;
16361 if (mask & OEX_FPU_OFLO)
16362 printf ("%sOFLO", first ? "" : "|"), first = false;
16363 if (mask & OEX_FPU_DIV0)
16364 printf ("%sDIV0", first ? "" : "|"), first = false;
16365 if (mask & OEX_FPU_INVAL)
16366 printf ("%sINVAL", first ? "" : "|");
16367 }
16368 else
16369 fputs ("0", stdout);
16370 }
16371
16372 /* Display's the value of TAG at location P. If TAG is
16373 greater than 0 it is assumed to be an unknown tag, and
16374 a message is printed to this effect. Otherwise it is
16375 assumed that a message has already been printed.
16376
16377 If the bottom bit of TAG is set it assumed to have a
16378 string value, otherwise it is assumed to have an integer
16379 value.
16380
16381 Returns an updated P pointing to the first unread byte
16382 beyond the end of TAG's value.
16383
16384 Reads at or beyond END will not be made. */
16385
16386 static unsigned char *
16387 display_tag_value (signed int tag,
16388 unsigned char * p,
16389 const unsigned char * const end)
16390 {
16391 unsigned long val;
16392
16393 if (tag > 0)
16394 printf (" Tag_unknown_%d: ", tag);
16395
16396 if (p >= end)
16397 {
16398 warn (_("<corrupt tag>\n"));
16399 }
16400 else if (tag & 1)
16401 {
16402 /* PR 17531 file: 027-19978-0.004. */
16403 size_t maxlen = (end - p) - 1;
16404
16405 putchar ('"');
16406 if (maxlen > 0)
16407 {
16408 print_symbol ((int) maxlen, (const char *) p);
16409 p += strnlen ((char *) p, maxlen) + 1;
16410 }
16411 else
16412 {
16413 printf (_("<corrupt string tag>"));
16414 p = (unsigned char *) end;
16415 }
16416 printf ("\"\n");
16417 }
16418 else
16419 {
16420 READ_ULEB (val, p, end);
16421 printf ("%ld (0x%lx)\n", val, val);
16422 }
16423
16424 assert (p <= end);
16425 return p;
16426 }
16427
16428 /* ARC ABI attributes section. */
16429
16430 static unsigned char *
16431 display_arc_attribute (unsigned char * p,
16432 const unsigned char * const end)
16433 {
16434 unsigned int tag;
16435 unsigned int val;
16436
16437 READ_ULEB (tag, p, end);
16438
16439 switch (tag)
16440 {
16441 case Tag_ARC_PCS_config:
16442 READ_ULEB (val, p, end);
16443 printf (" Tag_ARC_PCS_config: ");
16444 switch (val)
16445 {
16446 case 0:
16447 printf (_("Absent/Non standard\n"));
16448 break;
16449 case 1:
16450 printf (_("Bare metal/mwdt\n"));
16451 break;
16452 case 2:
16453 printf (_("Bare metal/newlib\n"));
16454 break;
16455 case 3:
16456 printf (_("Linux/uclibc\n"));
16457 break;
16458 case 4:
16459 printf (_("Linux/glibc\n"));
16460 break;
16461 default:
16462 printf (_("Unknown\n"));
16463 break;
16464 }
16465 break;
16466
16467 case Tag_ARC_CPU_base:
16468 READ_ULEB (val, p, end);
16469 printf (" Tag_ARC_CPU_base: ");
16470 switch (val)
16471 {
16472 default:
16473 case TAG_CPU_NONE:
16474 printf (_("Absent\n"));
16475 break;
16476 case TAG_CPU_ARC6xx:
16477 printf ("ARC6xx\n");
16478 break;
16479 case TAG_CPU_ARC7xx:
16480 printf ("ARC7xx\n");
16481 break;
16482 case TAG_CPU_ARCEM:
16483 printf ("ARCEM\n");
16484 break;
16485 case TAG_CPU_ARCHS:
16486 printf ("ARCHS\n");
16487 break;
16488 }
16489 break;
16490
16491 case Tag_ARC_CPU_variation:
16492 READ_ULEB (val, p, end);
16493 printf (" Tag_ARC_CPU_variation: ");
16494 switch (val)
16495 {
16496 default:
16497 if (val > 0 && val < 16)
16498 printf ("Core%d\n", val);
16499 else
16500 printf ("Unknown\n");
16501 break;
16502
16503 case 0:
16504 printf (_("Absent\n"));
16505 break;
16506 }
16507 break;
16508
16509 case Tag_ARC_CPU_name:
16510 printf (" Tag_ARC_CPU_name: ");
16511 p = display_tag_value (-1, p, end);
16512 break;
16513
16514 case Tag_ARC_ABI_rf16:
16515 READ_ULEB (val, p, end);
16516 printf (" Tag_ARC_ABI_rf16: %s\n", val ? _("yes") : _("no"));
16517 break;
16518
16519 case Tag_ARC_ABI_osver:
16520 READ_ULEB (val, p, end);
16521 printf (" Tag_ARC_ABI_osver: v%d\n", val);
16522 break;
16523
16524 case Tag_ARC_ABI_pic:
16525 case Tag_ARC_ABI_sda:
16526 READ_ULEB (val, p, end);
16527 printf (tag == Tag_ARC_ABI_sda ? " Tag_ARC_ABI_sda: "
16528 : " Tag_ARC_ABI_pic: ");
16529 switch (val)
16530 {
16531 case 0:
16532 printf (_("Absent\n"));
16533 break;
16534 case 1:
16535 printf ("MWDT\n");
16536 break;
16537 case 2:
16538 printf ("GNU\n");
16539 break;
16540 default:
16541 printf (_("Unknown\n"));
16542 break;
16543 }
16544 break;
16545
16546 case Tag_ARC_ABI_tls:
16547 READ_ULEB (val, p, end);
16548 printf (" Tag_ARC_ABI_tls: %s\n", val ? "r25": "none");
16549 break;
16550
16551 case Tag_ARC_ABI_enumsize:
16552 READ_ULEB (val, p, end);
16553 printf (" Tag_ARC_ABI_enumsize: %s\n", val ? _("default") :
16554 _("smallest"));
16555 break;
16556
16557 case Tag_ARC_ABI_exceptions:
16558 READ_ULEB (val, p, end);
16559 printf (" Tag_ARC_ABI_exceptions: %s\n", val ? _("OPTFP")
16560 : _("default"));
16561 break;
16562
16563 case Tag_ARC_ABI_double_size:
16564 READ_ULEB (val, p, end);
16565 printf (" Tag_ARC_ABI_double_size: %d\n", val);
16566 break;
16567
16568 case Tag_ARC_ISA_config:
16569 printf (" Tag_ARC_ISA_config: ");
16570 p = display_tag_value (-1, p, end);
16571 break;
16572
16573 case Tag_ARC_ISA_apex:
16574 printf (" Tag_ARC_ISA_apex: ");
16575 p = display_tag_value (-1, p, end);
16576 break;
16577
16578 case Tag_ARC_ISA_mpy_option:
16579 READ_ULEB (val, p, end);
16580 printf (" Tag_ARC_ISA_mpy_option: %d\n", val);
16581 break;
16582
16583 case Tag_ARC_ATR_version:
16584 READ_ULEB (val, p, end);
16585 printf (" Tag_ARC_ATR_version: %d\n", val);
16586 break;
16587
16588 default:
16589 return display_tag_value (tag & 1, p, end);
16590 }
16591
16592 return p;
16593 }
16594
16595 /* ARM EABI attributes section. */
16596 typedef struct
16597 {
16598 unsigned int tag;
16599 const char * name;
16600 /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup. */
16601 unsigned int type;
16602 const char *const *table;
16603 } arm_attr_public_tag;
16604
16605 static const char *const arm_attr_tag_CPU_arch[] =
16606 {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
16607 "v6K", "v7", "v6-M", "v6S-M", "v7E-M", "v8", "v8-R", "v8-M.baseline",
16608 "v8-M.mainline", "v8.1-A", "v8.2-A", "v8.3-A",
16609 "v8.1-M.mainline", "v9"};
16610 static const char *const arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
16611 static const char *const arm_attr_tag_THUMB_ISA_use[] =
16612 {"No", "Thumb-1", "Thumb-2", "Yes"};
16613 static const char *const arm_attr_tag_FP_arch[] =
16614 {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16", "VFPv4", "VFPv4-D16",
16615 "FP for ARMv8", "FPv5/FP-D16 for ARMv8"};
16616 static const char *const arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
16617 static const char *const arm_attr_tag_Advanced_SIMD_arch[] =
16618 {"No", "NEONv1", "NEONv1 with Fused-MAC", "NEON for ARMv8",
16619 "NEON for ARMv8.1"};
16620 static const char *const arm_attr_tag_PCS_config[] =
16621 {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
16622 "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
16623 static const char *const arm_attr_tag_ABI_PCS_R9_use[] =
16624 {"V6", "SB", "TLS", "Unused"};
16625 static const char *const arm_attr_tag_ABI_PCS_RW_data[] =
16626 {"Absolute", "PC-relative", "SB-relative", "None"};
16627 static const char *const arm_attr_tag_ABI_PCS_RO_data[] =
16628 {"Absolute", "PC-relative", "None"};
16629 static const char *const arm_attr_tag_ABI_PCS_GOT_use[] =
16630 {"None", "direct", "GOT-indirect"};
16631 static const char *const arm_attr_tag_ABI_PCS_wchar_t[] =
16632 {"None", "??? 1", "2", "??? 3", "4"};
16633 static const char *const arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
16634 static const char *const arm_attr_tag_ABI_FP_denormal[] =
16635 {"Unused", "Needed", "Sign only"};
16636 static const char *const arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
16637 static const char *const arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
16638 static const char *const arm_attr_tag_ABI_FP_number_model[] =
16639 {"Unused", "Finite", "RTABI", "IEEE 754"};
16640 static const char *const arm_attr_tag_ABI_enum_size[] =
16641 {"Unused", "small", "int", "forced to int"};
16642 static const char *const arm_attr_tag_ABI_HardFP_use[] =
16643 {"As Tag_FP_arch", "SP only", "Reserved", "Deprecated"};
16644 static const char *const arm_attr_tag_ABI_VFP_args[] =
16645 {"AAPCS", "VFP registers", "custom", "compatible"};
16646 static const char *const arm_attr_tag_ABI_WMMX_args[] =
16647 {"AAPCS", "WMMX registers", "custom"};
16648 static const char *const arm_attr_tag_ABI_optimization_goals[] =
16649 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
16650 "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
16651 static const char *const arm_attr_tag_ABI_FP_optimization_goals[] =
16652 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
16653 "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
16654 static const char *const arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
16655 static const char *const arm_attr_tag_FP_HP_extension[] =
16656 {"Not Allowed", "Allowed"};
16657 static const char *const arm_attr_tag_ABI_FP_16bit_format[] =
16658 {"None", "IEEE 754", "Alternative Format"};
16659 static const char *const arm_attr_tag_DSP_extension[] =
16660 {"Follow architecture", "Allowed"};
16661 static const char *const arm_attr_tag_MPextension_use[] =
16662 {"Not Allowed", "Allowed"};
16663 static const char *const arm_attr_tag_DIV_use[] =
16664 {"Allowed in Thumb-ISA, v7-R or v7-M", "Not allowed",
16665 "Allowed in v7-A with integer division extension"};
16666 static const char *const arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
16667 static const char *const arm_attr_tag_Virtualization_use[] =
16668 {"Not Allowed", "TrustZone", "Virtualization Extensions",
16669 "TrustZone and Virtualization Extensions"};
16670 static const char *const arm_attr_tag_MPextension_use_legacy[] =
16671 {"Not Allowed", "Allowed"};
16672
16673 static const char *const arm_attr_tag_MVE_arch[] =
16674 {"No MVE", "MVE Integer only", "MVE Integer and FP"};
16675
16676 static const char * arm_attr_tag_PAC_extension[] =
16677 {"No PAC/AUT instructions",
16678 "PAC/AUT instructions permitted in the NOP space",
16679 "PAC/AUT instructions permitted in the NOP and in the non-NOP space"};
16680
16681 static const char * arm_attr_tag_BTI_extension[] =
16682 {"BTI instructions not permitted",
16683 "BTI instructions permitted in the NOP space",
16684 "BTI instructions permitted in the NOP and in the non-NOP space"};
16685
16686 static const char * arm_attr_tag_BTI_use[] =
16687 {"Compiled without branch target enforcement",
16688 "Compiled with branch target enforcement"};
16689
16690 static const char * arm_attr_tag_PACRET_use[] =
16691 {"Compiled without return address signing and authentication",
16692 "Compiled with return address signing and authentication"};
16693
16694 #define LOOKUP(id, name) \
16695 {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
16696 static arm_attr_public_tag arm_attr_public_tags[] =
16697 {
16698 {4, "CPU_raw_name", 1, NULL},
16699 {5, "CPU_name", 1, NULL},
16700 LOOKUP(6, CPU_arch),
16701 {7, "CPU_arch_profile", 0, NULL},
16702 LOOKUP(8, ARM_ISA_use),
16703 LOOKUP(9, THUMB_ISA_use),
16704 LOOKUP(10, FP_arch),
16705 LOOKUP(11, WMMX_arch),
16706 LOOKUP(12, Advanced_SIMD_arch),
16707 LOOKUP(13, PCS_config),
16708 LOOKUP(14, ABI_PCS_R9_use),
16709 LOOKUP(15, ABI_PCS_RW_data),
16710 LOOKUP(16, ABI_PCS_RO_data),
16711 LOOKUP(17, ABI_PCS_GOT_use),
16712 LOOKUP(18, ABI_PCS_wchar_t),
16713 LOOKUP(19, ABI_FP_rounding),
16714 LOOKUP(20, ABI_FP_denormal),
16715 LOOKUP(21, ABI_FP_exceptions),
16716 LOOKUP(22, ABI_FP_user_exceptions),
16717 LOOKUP(23, ABI_FP_number_model),
16718 {24, "ABI_align_needed", 0, NULL},
16719 {25, "ABI_align_preserved", 0, NULL},
16720 LOOKUP(26, ABI_enum_size),
16721 LOOKUP(27, ABI_HardFP_use),
16722 LOOKUP(28, ABI_VFP_args),
16723 LOOKUP(29, ABI_WMMX_args),
16724 LOOKUP(30, ABI_optimization_goals),
16725 LOOKUP(31, ABI_FP_optimization_goals),
16726 {32, "compatibility", 0, NULL},
16727 LOOKUP(34, CPU_unaligned_access),
16728 LOOKUP(36, FP_HP_extension),
16729 LOOKUP(38, ABI_FP_16bit_format),
16730 LOOKUP(42, MPextension_use),
16731 LOOKUP(44, DIV_use),
16732 LOOKUP(46, DSP_extension),
16733 LOOKUP(48, MVE_arch),
16734 LOOKUP(50, PAC_extension),
16735 LOOKUP(52, BTI_extension),
16736 LOOKUP(74, BTI_use),
16737 LOOKUP(76, PACRET_use),
16738 {64, "nodefaults", 0, NULL},
16739 {65, "also_compatible_with", 0, NULL},
16740 LOOKUP(66, T2EE_use),
16741 {67, "conformance", 1, NULL},
16742 LOOKUP(68, Virtualization_use),
16743 LOOKUP(70, MPextension_use_legacy)
16744 };
16745 #undef LOOKUP
16746
16747 static unsigned char *
16748 display_arm_attribute (unsigned char * p,
16749 const unsigned char * const end)
16750 {
16751 unsigned int tag;
16752 unsigned int val;
16753 arm_attr_public_tag * attr;
16754 unsigned i;
16755 unsigned int type;
16756
16757 READ_ULEB (tag, p, end);
16758 attr = NULL;
16759 for (i = 0; i < ARRAY_SIZE (arm_attr_public_tags); i++)
16760 {
16761 if (arm_attr_public_tags[i].tag == tag)
16762 {
16763 attr = &arm_attr_public_tags[i];
16764 break;
16765 }
16766 }
16767
16768 if (attr)
16769 {
16770 printf (" Tag_%s: ", attr->name);
16771 switch (attr->type)
16772 {
16773 case 0:
16774 switch (tag)
16775 {
16776 case 7: /* Tag_CPU_arch_profile. */
16777 READ_ULEB (val, p, end);
16778 switch (val)
16779 {
16780 case 0: printf (_("None\n")); break;
16781 case 'A': printf (_("Application\n")); break;
16782 case 'R': printf (_("Realtime\n")); break;
16783 case 'M': printf (_("Microcontroller\n")); break;
16784 case 'S': printf (_("Application or Realtime\n")); break;
16785 default: printf ("??? (%d)\n", val); break;
16786 }
16787 break;
16788
16789 case 24: /* Tag_align_needed. */
16790 READ_ULEB (val, p, end);
16791 switch (val)
16792 {
16793 case 0: printf (_("None\n")); break;
16794 case 1: printf (_("8-byte\n")); break;
16795 case 2: printf (_("4-byte\n")); break;
16796 case 3: printf ("??? 3\n"); break;
16797 default:
16798 if (val <= 12)
16799 printf (_("8-byte and up to %d-byte extended\n"),
16800 1 << val);
16801 else
16802 printf ("??? (%d)\n", val);
16803 break;
16804 }
16805 break;
16806
16807 case 25: /* Tag_align_preserved. */
16808 READ_ULEB (val, p, end);
16809 switch (val)
16810 {
16811 case 0: printf (_("None\n")); break;
16812 case 1: printf (_("8-byte, except leaf SP\n")); break;
16813 case 2: printf (_("8-byte\n")); break;
16814 case 3: printf ("??? 3\n"); break;
16815 default:
16816 if (val <= 12)
16817 printf (_("8-byte and up to %d-byte extended\n"),
16818 1 << val);
16819 else
16820 printf ("??? (%d)\n", val);
16821 break;
16822 }
16823 break;
16824
16825 case 32: /* Tag_compatibility. */
16826 {
16827 READ_ULEB (val, p, end);
16828 printf (_("flag = %d, vendor = "), val);
16829 if (p < end - 1)
16830 {
16831 size_t maxlen = (end - p) - 1;
16832
16833 print_symbol ((int) maxlen, (const char *) p);
16834 p += strnlen ((char *) p, maxlen) + 1;
16835 }
16836 else
16837 {
16838 printf (_("<corrupt>"));
16839 p = (unsigned char *) end;
16840 }
16841 putchar ('\n');
16842 }
16843 break;
16844
16845 case 64: /* Tag_nodefaults. */
16846 /* PR 17531: file: 001-505008-0.01. */
16847 if (p < end)
16848 p++;
16849 printf (_("True\n"));
16850 break;
16851
16852 case 65: /* Tag_also_compatible_with. */
16853 READ_ULEB (val, p, end);
16854 if (val == 6 /* Tag_CPU_arch. */)
16855 {
16856 READ_ULEB (val, p, end);
16857 if ((unsigned int) val >= ARRAY_SIZE (arm_attr_tag_CPU_arch))
16858 printf ("??? (%d)\n", val);
16859 else
16860 printf ("%s\n", arm_attr_tag_CPU_arch[val]);
16861 }
16862 else
16863 printf ("???\n");
16864 while (p < end && *(p++) != '\0' /* NUL terminator. */)
16865 ;
16866 break;
16867
16868 default:
16869 printf (_("<unknown: %d>\n"), tag);
16870 break;
16871 }
16872 return p;
16873
16874 case 1:
16875 return display_tag_value (-1, p, end);
16876 case 2:
16877 return display_tag_value (0, p, end);
16878
16879 default:
16880 assert (attr->type & 0x80);
16881 READ_ULEB (val, p, end);
16882 type = attr->type & 0x7f;
16883 if (val >= type)
16884 printf ("??? (%d)\n", val);
16885 else
16886 printf ("%s\n", attr->table[val]);
16887 return p;
16888 }
16889 }
16890
16891 return display_tag_value (tag, p, end);
16892 }
16893
16894 static unsigned char *
16895 display_gnu_attribute (unsigned char * p,
16896 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const),
16897 const unsigned char * const end)
16898 {
16899 unsigned int tag;
16900 unsigned int val;
16901
16902 READ_ULEB (tag, p, end);
16903
16904 /* Tag_compatibility is the only generic GNU attribute defined at
16905 present. */
16906 if (tag == 32)
16907 {
16908 READ_ULEB (val, p, end);
16909
16910 printf (_("flag = %d, vendor = "), val);
16911 if (p == end)
16912 {
16913 printf (_("<corrupt>\n"));
16914 warn (_("corrupt vendor attribute\n"));
16915 }
16916 else
16917 {
16918 if (p < end - 1)
16919 {
16920 size_t maxlen = (end - p) - 1;
16921
16922 print_symbol ((int) maxlen, (const char *) p);
16923 p += strnlen ((char *) p, maxlen) + 1;
16924 }
16925 else
16926 {
16927 printf (_("<corrupt>"));
16928 p = (unsigned char *) end;
16929 }
16930 putchar ('\n');
16931 }
16932 return p;
16933 }
16934
16935 if ((tag & 2) == 0 && display_proc_gnu_attribute)
16936 return display_proc_gnu_attribute (p, tag, end);
16937
16938 return display_tag_value (tag, p, end);
16939 }
16940
16941 static unsigned char *
16942 display_m68k_gnu_attribute (unsigned char * p,
16943 unsigned int tag,
16944 const unsigned char * const end)
16945 {
16946 unsigned int val;
16947
16948 if (tag == Tag_GNU_M68K_ABI_FP)
16949 {
16950 printf (" Tag_GNU_M68K_ABI_FP: ");
16951 if (p == end)
16952 {
16953 printf (_("<corrupt>\n"));
16954 return p;
16955 }
16956 READ_ULEB (val, p, end);
16957
16958 if (val > 3)
16959 printf ("(%#x), ", val);
16960
16961 switch (val & 3)
16962 {
16963 case 0:
16964 printf (_("unspecified hard/soft float\n"));
16965 break;
16966 case 1:
16967 printf (_("hard float\n"));
16968 break;
16969 case 2:
16970 printf (_("soft float\n"));
16971 break;
16972 }
16973 return p;
16974 }
16975
16976 return display_tag_value (tag & 1, p, end);
16977 }
16978
16979 static unsigned char *
16980 display_power_gnu_attribute (unsigned char * p,
16981 unsigned int tag,
16982 const unsigned char * const end)
16983 {
16984 unsigned int val;
16985
16986 if (tag == Tag_GNU_Power_ABI_FP)
16987 {
16988 printf (" Tag_GNU_Power_ABI_FP: ");
16989 if (p == end)
16990 {
16991 printf (_("<corrupt>\n"));
16992 return p;
16993 }
16994 READ_ULEB (val, p, end);
16995
16996 if (val > 15)
16997 printf ("(%#x), ", val);
16998
16999 switch (val & 3)
17000 {
17001 case 0:
17002 printf (_("unspecified hard/soft float, "));
17003 break;
17004 case 1:
17005 printf (_("hard float, "));
17006 break;
17007 case 2:
17008 printf (_("soft float, "));
17009 break;
17010 case 3:
17011 printf (_("single-precision hard float, "));
17012 break;
17013 }
17014
17015 switch (val & 0xC)
17016 {
17017 case 0:
17018 printf (_("unspecified long double\n"));
17019 break;
17020 case 4:
17021 printf (_("128-bit IBM long double\n"));
17022 break;
17023 case 8:
17024 printf (_("64-bit long double\n"));
17025 break;
17026 case 12:
17027 printf (_("128-bit IEEE long double\n"));
17028 break;
17029 }
17030 return p;
17031 }
17032
17033 if (tag == Tag_GNU_Power_ABI_Vector)
17034 {
17035 printf (" Tag_GNU_Power_ABI_Vector: ");
17036 if (p == end)
17037 {
17038 printf (_("<corrupt>\n"));
17039 return p;
17040 }
17041 READ_ULEB (val, p, end);
17042
17043 if (val > 3)
17044 printf ("(%#x), ", val);
17045
17046 switch (val & 3)
17047 {
17048 case 0:
17049 printf (_("unspecified\n"));
17050 break;
17051 case 1:
17052 printf (_("generic\n"));
17053 break;
17054 case 2:
17055 printf ("AltiVec\n");
17056 break;
17057 case 3:
17058 printf ("SPE\n");
17059 break;
17060 }
17061 return p;
17062 }
17063
17064 if (tag == Tag_GNU_Power_ABI_Struct_Return)
17065 {
17066 printf (" Tag_GNU_Power_ABI_Struct_Return: ");
17067 if (p == end)
17068 {
17069 printf (_("<corrupt>\n"));
17070 return p;
17071 }
17072 READ_ULEB (val, p, end);
17073
17074 if (val > 2)
17075 printf ("(%#x), ", val);
17076
17077 switch (val & 3)
17078 {
17079 case 0:
17080 printf (_("unspecified\n"));
17081 break;
17082 case 1:
17083 printf ("r3/r4\n");
17084 break;
17085 case 2:
17086 printf (_("memory\n"));
17087 break;
17088 case 3:
17089 printf ("???\n");
17090 break;
17091 }
17092 return p;
17093 }
17094
17095 return display_tag_value (tag & 1, p, end);
17096 }
17097
17098 static unsigned char *
17099 display_s390_gnu_attribute (unsigned char * p,
17100 unsigned int tag,
17101 const unsigned char * const end)
17102 {
17103 unsigned int val;
17104
17105 if (tag == Tag_GNU_S390_ABI_Vector)
17106 {
17107 printf (" Tag_GNU_S390_ABI_Vector: ");
17108 READ_ULEB (val, p, end);
17109
17110 switch (val)
17111 {
17112 case 0:
17113 printf (_("any\n"));
17114 break;
17115 case 1:
17116 printf (_("software\n"));
17117 break;
17118 case 2:
17119 printf (_("hardware\n"));
17120 break;
17121 default:
17122 printf ("??? (%d)\n", val);
17123 break;
17124 }
17125 return p;
17126 }
17127
17128 return display_tag_value (tag & 1, p, end);
17129 }
17130
17131 static void
17132 display_sparc_hwcaps (unsigned int mask)
17133 {
17134 if (mask)
17135 {
17136 bool first = true;
17137
17138 if (mask & ELF_SPARC_HWCAP_MUL32)
17139 fputs ("mul32", stdout), first = false;
17140 if (mask & ELF_SPARC_HWCAP_DIV32)
17141 printf ("%sdiv32", first ? "" : "|"), first = false;
17142 if (mask & ELF_SPARC_HWCAP_FSMULD)
17143 printf ("%sfsmuld", first ? "" : "|"), first = false;
17144 if (mask & ELF_SPARC_HWCAP_V8PLUS)
17145 printf ("%sv8plus", first ? "" : "|"), first = false;
17146 if (mask & ELF_SPARC_HWCAP_POPC)
17147 printf ("%spopc", first ? "" : "|"), first = false;
17148 if (mask & ELF_SPARC_HWCAP_VIS)
17149 printf ("%svis", first ? "" : "|"), first = false;
17150 if (mask & ELF_SPARC_HWCAP_VIS2)
17151 printf ("%svis2", first ? "" : "|"), first = false;
17152 if (mask & ELF_SPARC_HWCAP_ASI_BLK_INIT)
17153 printf ("%sASIBlkInit", first ? "" : "|"), first = false;
17154 if (mask & ELF_SPARC_HWCAP_FMAF)
17155 printf ("%sfmaf", first ? "" : "|"), first = false;
17156 if (mask & ELF_SPARC_HWCAP_VIS3)
17157 printf ("%svis3", first ? "" : "|"), first = false;
17158 if (mask & ELF_SPARC_HWCAP_HPC)
17159 printf ("%shpc", first ? "" : "|"), first = false;
17160 if (mask & ELF_SPARC_HWCAP_RANDOM)
17161 printf ("%srandom", first ? "" : "|"), first = false;
17162 if (mask & ELF_SPARC_HWCAP_TRANS)
17163 printf ("%strans", first ? "" : "|"), first = false;
17164 if (mask & ELF_SPARC_HWCAP_FJFMAU)
17165 printf ("%sfjfmau", first ? "" : "|"), first = false;
17166 if (mask & ELF_SPARC_HWCAP_IMA)
17167 printf ("%sima", first ? "" : "|"), first = false;
17168 if (mask & ELF_SPARC_HWCAP_ASI_CACHE_SPARING)
17169 printf ("%scspare", first ? "" : "|"), first = false;
17170 }
17171 else
17172 fputc ('0', stdout);
17173 fputc ('\n', stdout);
17174 }
17175
17176 static void
17177 display_sparc_hwcaps2 (unsigned int mask)
17178 {
17179 if (mask)
17180 {
17181 bool first = true;
17182
17183 if (mask & ELF_SPARC_HWCAP2_FJATHPLUS)
17184 fputs ("fjathplus", stdout), first = false;
17185 if (mask & ELF_SPARC_HWCAP2_VIS3B)
17186 printf ("%svis3b", first ? "" : "|"), first = false;
17187 if (mask & ELF_SPARC_HWCAP2_ADP)
17188 printf ("%sadp", first ? "" : "|"), first = false;
17189 if (mask & ELF_SPARC_HWCAP2_SPARC5)
17190 printf ("%ssparc5", first ? "" : "|"), first = false;
17191 if (mask & ELF_SPARC_HWCAP2_MWAIT)
17192 printf ("%smwait", first ? "" : "|"), first = false;
17193 if (mask & ELF_SPARC_HWCAP2_XMPMUL)
17194 printf ("%sxmpmul", first ? "" : "|"), first = false;
17195 if (mask & ELF_SPARC_HWCAP2_XMONT)
17196 printf ("%sxmont2", first ? "" : "|"), first = false;
17197 if (mask & ELF_SPARC_HWCAP2_NSEC)
17198 printf ("%snsec", first ? "" : "|"), first = false;
17199 if (mask & ELF_SPARC_HWCAP2_FJATHHPC)
17200 printf ("%sfjathhpc", first ? "" : "|"), first = false;
17201 if (mask & ELF_SPARC_HWCAP2_FJDES)
17202 printf ("%sfjdes", first ? "" : "|"), first = false;
17203 if (mask & ELF_SPARC_HWCAP2_FJAES)
17204 printf ("%sfjaes", first ? "" : "|"), first = false;
17205 }
17206 else
17207 fputc ('0', stdout);
17208 fputc ('\n', stdout);
17209 }
17210
17211 static unsigned char *
17212 display_sparc_gnu_attribute (unsigned char * p,
17213 unsigned int tag,
17214 const unsigned char * const end)
17215 {
17216 unsigned int val;
17217
17218 if (tag == Tag_GNU_Sparc_HWCAPS)
17219 {
17220 READ_ULEB (val, p, end);
17221 printf (" Tag_GNU_Sparc_HWCAPS: ");
17222 display_sparc_hwcaps (val);
17223 return p;
17224 }
17225 if (tag == Tag_GNU_Sparc_HWCAPS2)
17226 {
17227 READ_ULEB (val, p, end);
17228 printf (" Tag_GNU_Sparc_HWCAPS2: ");
17229 display_sparc_hwcaps2 (val);
17230 return p;
17231 }
17232
17233 return display_tag_value (tag, p, end);
17234 }
17235
17236 static void
17237 print_mips_fp_abi_value (unsigned int val)
17238 {
17239 switch (val)
17240 {
17241 case Val_GNU_MIPS_ABI_FP_ANY:
17242 printf (_("Hard or soft float\n"));
17243 break;
17244 case Val_GNU_MIPS_ABI_FP_DOUBLE:
17245 printf (_("Hard float (double precision)\n"));
17246 break;
17247 case Val_GNU_MIPS_ABI_FP_SINGLE:
17248 printf (_("Hard float (single precision)\n"));
17249 break;
17250 case Val_GNU_MIPS_ABI_FP_SOFT:
17251 printf (_("Soft float\n"));
17252 break;
17253 case Val_GNU_MIPS_ABI_FP_OLD_64:
17254 printf (_("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
17255 break;
17256 case Val_GNU_MIPS_ABI_FP_XX:
17257 printf (_("Hard float (32-bit CPU, Any FPU)\n"));
17258 break;
17259 case Val_GNU_MIPS_ABI_FP_64:
17260 printf (_("Hard float (32-bit CPU, 64-bit FPU)\n"));
17261 break;
17262 case Val_GNU_MIPS_ABI_FP_64A:
17263 printf (_("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
17264 break;
17265 case Val_GNU_MIPS_ABI_FP_NAN2008:
17266 printf (_("NaN 2008 compatibility\n"));
17267 break;
17268 default:
17269 printf ("??? (%d)\n", val);
17270 break;
17271 }
17272 }
17273
17274 static unsigned char *
17275 display_mips_gnu_attribute (unsigned char * p,
17276 unsigned int tag,
17277 const unsigned char * const end)
17278 {
17279 if (tag == Tag_GNU_MIPS_ABI_FP)
17280 {
17281 unsigned int val;
17282
17283 printf (" Tag_GNU_MIPS_ABI_FP: ");
17284 READ_ULEB (val, p, end);
17285 print_mips_fp_abi_value (val);
17286 return p;
17287 }
17288
17289 if (tag == Tag_GNU_MIPS_ABI_MSA)
17290 {
17291 unsigned int val;
17292
17293 printf (" Tag_GNU_MIPS_ABI_MSA: ");
17294 READ_ULEB (val, p, end);
17295
17296 switch (val)
17297 {
17298 case Val_GNU_MIPS_ABI_MSA_ANY:
17299 printf (_("Any MSA or not\n"));
17300 break;
17301 case Val_GNU_MIPS_ABI_MSA_128:
17302 printf (_("128-bit MSA\n"));
17303 break;
17304 default:
17305 printf ("??? (%d)\n", val);
17306 break;
17307 }
17308 return p;
17309 }
17310
17311 return display_tag_value (tag & 1, p, end);
17312 }
17313
17314 static unsigned char *
17315 display_tic6x_attribute (unsigned char * p,
17316 const unsigned char * const end)
17317 {
17318 unsigned int tag;
17319 unsigned int val;
17320
17321 READ_ULEB (tag, p, end);
17322
17323 switch (tag)
17324 {
17325 case Tag_ISA:
17326 printf (" Tag_ISA: ");
17327 READ_ULEB (val, p, end);
17328
17329 switch (val)
17330 {
17331 case C6XABI_Tag_ISA_none:
17332 printf (_("None\n"));
17333 break;
17334 case C6XABI_Tag_ISA_C62X:
17335 printf ("C62x\n");
17336 break;
17337 case C6XABI_Tag_ISA_C67X:
17338 printf ("C67x\n");
17339 break;
17340 case C6XABI_Tag_ISA_C67XP:
17341 printf ("C67x+\n");
17342 break;
17343 case C6XABI_Tag_ISA_C64X:
17344 printf ("C64x\n");
17345 break;
17346 case C6XABI_Tag_ISA_C64XP:
17347 printf ("C64x+\n");
17348 break;
17349 case C6XABI_Tag_ISA_C674X:
17350 printf ("C674x\n");
17351 break;
17352 default:
17353 printf ("??? (%d)\n", val);
17354 break;
17355 }
17356 return p;
17357
17358 case Tag_ABI_wchar_t:
17359 printf (" Tag_ABI_wchar_t: ");
17360 READ_ULEB (val, p, end);
17361 switch (val)
17362 {
17363 case 0:
17364 printf (_("Not used\n"));
17365 break;
17366 case 1:
17367 printf (_("2 bytes\n"));
17368 break;
17369 case 2:
17370 printf (_("4 bytes\n"));
17371 break;
17372 default:
17373 printf ("??? (%d)\n", val);
17374 break;
17375 }
17376 return p;
17377
17378 case Tag_ABI_stack_align_needed:
17379 printf (" Tag_ABI_stack_align_needed: ");
17380 READ_ULEB (val, p, end);
17381 switch (val)
17382 {
17383 case 0:
17384 printf (_("8-byte\n"));
17385 break;
17386 case 1:
17387 printf (_("16-byte\n"));
17388 break;
17389 default:
17390 printf ("??? (%d)\n", val);
17391 break;
17392 }
17393 return p;
17394
17395 case Tag_ABI_stack_align_preserved:
17396 READ_ULEB (val, p, end);
17397 printf (" Tag_ABI_stack_align_preserved: ");
17398 switch (val)
17399 {
17400 case 0:
17401 printf (_("8-byte\n"));
17402 break;
17403 case 1:
17404 printf (_("16-byte\n"));
17405 break;
17406 default:
17407 printf ("??? (%d)\n", val);
17408 break;
17409 }
17410 return p;
17411
17412 case Tag_ABI_DSBT:
17413 READ_ULEB (val, p, end);
17414 printf (" Tag_ABI_DSBT: ");
17415 switch (val)
17416 {
17417 case 0:
17418 printf (_("DSBT addressing not used\n"));
17419 break;
17420 case 1:
17421 printf (_("DSBT addressing used\n"));
17422 break;
17423 default:
17424 printf ("??? (%d)\n", val);
17425 break;
17426 }
17427 return p;
17428
17429 case Tag_ABI_PID:
17430 READ_ULEB (val, p, end);
17431 printf (" Tag_ABI_PID: ");
17432 switch (val)
17433 {
17434 case 0:
17435 printf (_("Data addressing position-dependent\n"));
17436 break;
17437 case 1:
17438 printf (_("Data addressing position-independent, GOT near DP\n"));
17439 break;
17440 case 2:
17441 printf (_("Data addressing position-independent, GOT far from DP\n"));
17442 break;
17443 default:
17444 printf ("??? (%d)\n", val);
17445 break;
17446 }
17447 return p;
17448
17449 case Tag_ABI_PIC:
17450 READ_ULEB (val, p, end);
17451 printf (" Tag_ABI_PIC: ");
17452 switch (val)
17453 {
17454 case 0:
17455 printf (_("Code addressing position-dependent\n"));
17456 break;
17457 case 1:
17458 printf (_("Code addressing position-independent\n"));
17459 break;
17460 default:
17461 printf ("??? (%d)\n", val);
17462 break;
17463 }
17464 return p;
17465
17466 case Tag_ABI_array_object_alignment:
17467 READ_ULEB (val, p, end);
17468 printf (" Tag_ABI_array_object_alignment: ");
17469 switch (val)
17470 {
17471 case 0:
17472 printf (_("8-byte\n"));
17473 break;
17474 case 1:
17475 printf (_("4-byte\n"));
17476 break;
17477 case 2:
17478 printf (_("16-byte\n"));
17479 break;
17480 default:
17481 printf ("??? (%d)\n", val);
17482 break;
17483 }
17484 return p;
17485
17486 case Tag_ABI_array_object_align_expected:
17487 READ_ULEB (val, p, end);
17488 printf (" Tag_ABI_array_object_align_expected: ");
17489 switch (val)
17490 {
17491 case 0:
17492 printf (_("8-byte\n"));
17493 break;
17494 case 1:
17495 printf (_("4-byte\n"));
17496 break;
17497 case 2:
17498 printf (_("16-byte\n"));
17499 break;
17500 default:
17501 printf ("??? (%d)\n", val);
17502 break;
17503 }
17504 return p;
17505
17506 case Tag_ABI_compatibility:
17507 {
17508 READ_ULEB (val, p, end);
17509 printf (" Tag_ABI_compatibility: ");
17510 printf (_("flag = %d, vendor = "), val);
17511 if (p < end - 1)
17512 {
17513 size_t maxlen = (end - p) - 1;
17514
17515 print_symbol ((int) maxlen, (const char *) p);
17516 p += strnlen ((char *) p, maxlen) + 1;
17517 }
17518 else
17519 {
17520 printf (_("<corrupt>"));
17521 p = (unsigned char *) end;
17522 }
17523 putchar ('\n');
17524 return p;
17525 }
17526
17527 case Tag_ABI_conformance:
17528 {
17529 printf (" Tag_ABI_conformance: \"");
17530 if (p < end - 1)
17531 {
17532 size_t maxlen = (end - p) - 1;
17533
17534 print_symbol ((int) maxlen, (const char *) p);
17535 p += strnlen ((char *) p, maxlen) + 1;
17536 }
17537 else
17538 {
17539 printf (_("<corrupt>"));
17540 p = (unsigned char *) end;
17541 }
17542 printf ("\"\n");
17543 return p;
17544 }
17545 }
17546
17547 return display_tag_value (tag, p, end);
17548 }
17549
17550 static void
17551 display_raw_attribute (unsigned char * p, unsigned char const * const end)
17552 {
17553 unsigned long addr = 0;
17554 size_t bytes = end - p;
17555
17556 assert (end >= p);
17557 while (bytes)
17558 {
17559 int j;
17560 int k;
17561 int lbytes = (bytes > 16 ? 16 : bytes);
17562
17563 printf (" 0x%8.8lx ", addr);
17564
17565 for (j = 0; j < 16; j++)
17566 {
17567 if (j < lbytes)
17568 printf ("%2.2x", p[j]);
17569 else
17570 printf (" ");
17571
17572 if ((j & 3) == 3)
17573 printf (" ");
17574 }
17575
17576 for (j = 0; j < lbytes; j++)
17577 {
17578 k = p[j];
17579 if (k >= ' ' && k < 0x7f)
17580 printf ("%c", k);
17581 else
17582 printf (".");
17583 }
17584
17585 putchar ('\n');
17586
17587 p += lbytes;
17588 bytes -= lbytes;
17589 addr += lbytes;
17590 }
17591
17592 putchar ('\n');
17593 }
17594
17595 static unsigned char *
17596 display_msp430_attribute (unsigned char * p,
17597 const unsigned char * const end)
17598 {
17599 unsigned int val;
17600 unsigned int tag;
17601
17602 READ_ULEB (tag, p, end);
17603
17604 switch (tag)
17605 {
17606 case OFBA_MSPABI_Tag_ISA:
17607 printf (" Tag_ISA: ");
17608 READ_ULEB (val, p, end);
17609 switch (val)
17610 {
17611 case 0: printf (_("None\n")); break;
17612 case 1: printf (_("MSP430\n")); break;
17613 case 2: printf (_("MSP430X\n")); break;
17614 default: printf ("??? (%d)\n", val); break;
17615 }
17616 break;
17617
17618 case OFBA_MSPABI_Tag_Code_Model:
17619 printf (" Tag_Code_Model: ");
17620 READ_ULEB (val, p, end);
17621 switch (val)
17622 {
17623 case 0: printf (_("None\n")); break;
17624 case 1: printf (_("Small\n")); break;
17625 case 2: printf (_("Large\n")); break;
17626 default: printf ("??? (%d)\n", val); break;
17627 }
17628 break;
17629
17630 case OFBA_MSPABI_Tag_Data_Model:
17631 printf (" Tag_Data_Model: ");
17632 READ_ULEB (val, p, end);
17633 switch (val)
17634 {
17635 case 0: printf (_("None\n")); break;
17636 case 1: printf (_("Small\n")); break;
17637 case 2: printf (_("Large\n")); break;
17638 case 3: printf (_("Restricted Large\n")); break;
17639 default: printf ("??? (%d)\n", val); break;
17640 }
17641 break;
17642
17643 default:
17644 printf (_(" <unknown tag %d>: "), tag);
17645
17646 if (tag & 1)
17647 {
17648 putchar ('"');
17649 if (p < end - 1)
17650 {
17651 size_t maxlen = (end - p) - 1;
17652
17653 print_symbol ((int) maxlen, (const char *) p);
17654 p += strnlen ((char *) p, maxlen) + 1;
17655 }
17656 else
17657 {
17658 printf (_("<corrupt>"));
17659 p = (unsigned char *) end;
17660 }
17661 printf ("\"\n");
17662 }
17663 else
17664 {
17665 READ_ULEB (val, p, end);
17666 printf ("%d (0x%x)\n", val, val);
17667 }
17668 break;
17669 }
17670
17671 assert (p <= end);
17672 return p;
17673 }
17674
17675 static unsigned char *
17676 display_msp430_gnu_attribute (unsigned char * p,
17677 unsigned int tag,
17678 const unsigned char * const end)
17679 {
17680 if (tag == Tag_GNU_MSP430_Data_Region)
17681 {
17682 unsigned int val;
17683
17684 printf (" Tag_GNU_MSP430_Data_Region: ");
17685 READ_ULEB (val, p, end);
17686
17687 switch (val)
17688 {
17689 case Val_GNU_MSP430_Data_Region_Any:
17690 printf (_("Any Region\n"));
17691 break;
17692 case Val_GNU_MSP430_Data_Region_Lower:
17693 printf (_("Lower Region Only\n"));
17694 break;
17695 default:
17696 printf ("??? (%u)\n", val);
17697 }
17698 return p;
17699 }
17700 return display_tag_value (tag & 1, p, end);
17701 }
17702
17703 struct riscv_attr_tag_t {
17704 const char *name;
17705 unsigned int tag;
17706 };
17707
17708 static struct riscv_attr_tag_t riscv_attr_tag[] =
17709 {
17710 #define T(tag) {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
17711 T(arch),
17712 T(priv_spec),
17713 T(priv_spec_minor),
17714 T(priv_spec_revision),
17715 T(unaligned_access),
17716 T(stack_align),
17717 #undef T
17718 };
17719
17720 static unsigned char *
17721 display_riscv_attribute (unsigned char *p,
17722 const unsigned char * const end)
17723 {
17724 unsigned int val;
17725 unsigned int tag;
17726 struct riscv_attr_tag_t *attr = NULL;
17727 unsigned i;
17728
17729 READ_ULEB (tag, p, end);
17730
17731 /* Find the name of attribute. */
17732 for (i = 0; i < ARRAY_SIZE (riscv_attr_tag); i++)
17733 {
17734 if (riscv_attr_tag[i].tag == tag)
17735 {
17736 attr = &riscv_attr_tag[i];
17737 break;
17738 }
17739 }
17740
17741 if (attr)
17742 printf (" %s: ", attr->name);
17743 else
17744 return display_tag_value (tag, p, end);
17745
17746 switch (tag)
17747 {
17748 case Tag_RISCV_priv_spec:
17749 case Tag_RISCV_priv_spec_minor:
17750 case Tag_RISCV_priv_spec_revision:
17751 READ_ULEB (val, p, end);
17752 printf (_("%u\n"), val);
17753 break;
17754 case Tag_RISCV_unaligned_access:
17755 READ_ULEB (val, p, end);
17756 switch (val)
17757 {
17758 case 0:
17759 printf (_("No unaligned access\n"));
17760 break;
17761 case 1:
17762 printf (_("Unaligned access\n"));
17763 break;
17764 }
17765 break;
17766 case Tag_RISCV_stack_align:
17767 READ_ULEB (val, p, end);
17768 printf (_("%u-bytes\n"), val);
17769 break;
17770 case Tag_RISCV_arch:
17771 p = display_tag_value (-1, p, end);
17772 break;
17773 default:
17774 return display_tag_value (tag, p, end);
17775 }
17776
17777 return p;
17778 }
17779
17780 static unsigned char *
17781 display_csky_attribute (unsigned char * p,
17782 const unsigned char * const end)
17783 {
17784 unsigned int tag;
17785 unsigned int val;
17786 READ_ULEB (tag, p, end);
17787
17788 if (tag >= Tag_CSKY_MAX)
17789 {
17790 return display_tag_value (-1, p, end);
17791 }
17792
17793 switch (tag)
17794 {
17795 case Tag_CSKY_ARCH_NAME:
17796 printf (" Tag_CSKY_ARCH_NAME:\t\t");
17797 return display_tag_value (-1, p, end);
17798 case Tag_CSKY_CPU_NAME:
17799 printf (" Tag_CSKY_CPU_NAME:\t\t");
17800 return display_tag_value (-1, p, end);
17801
17802 case Tag_CSKY_ISA_FLAGS:
17803 printf (" Tag_CSKY_ISA_FLAGS:\t\t");
17804 return display_tag_value (0, p, end);
17805 case Tag_CSKY_ISA_EXT_FLAGS:
17806 printf (" Tag_CSKY_ISA_EXT_FLAGS:\t");
17807 return display_tag_value (0, p, end);
17808
17809 case Tag_CSKY_DSP_VERSION:
17810 printf (" Tag_CSKY_DSP_VERSION:\t\t");
17811 READ_ULEB (val, p, end);
17812 if (val == VAL_CSKY_DSP_VERSION_EXTENSION)
17813 printf ("DSP Extension\n");
17814 else if (val == VAL_CSKY_DSP_VERSION_2)
17815 printf ("DSP 2.0\n");
17816 break;
17817
17818 case Tag_CSKY_VDSP_VERSION:
17819 printf (" Tag_CSKY_VDSP_VERSION:\t");
17820 READ_ULEB (val, p, end);
17821 printf ("VDSP Version %d\n", val);
17822 break;
17823
17824 case Tag_CSKY_FPU_VERSION:
17825 printf (" Tag_CSKY_FPU_VERSION:\t\t");
17826 READ_ULEB (val, p, end);
17827 if (val == VAL_CSKY_FPU_VERSION_1)
17828 printf ("ABIV1 FPU Version 1\n");
17829 else if (val == VAL_CSKY_FPU_VERSION_2)
17830 printf ("FPU Version 2\n");
17831 break;
17832
17833 case Tag_CSKY_FPU_ABI:
17834 printf (" Tag_CSKY_FPU_ABI:\t\t");
17835 READ_ULEB (val, p, end);
17836 if (val == VAL_CSKY_FPU_ABI_HARD)
17837 printf ("Hard\n");
17838 else if (val == VAL_CSKY_FPU_ABI_SOFTFP)
17839 printf ("SoftFP\n");
17840 else if (val == VAL_CSKY_FPU_ABI_SOFT)
17841 printf ("Soft\n");
17842 break;
17843 case Tag_CSKY_FPU_ROUNDING:
17844 READ_ULEB (val, p, end);
17845 if (val == 1)
17846 {
17847 printf (" Tag_CSKY_FPU_ROUNDING:\t");
17848 printf ("Needed\n");
17849 }
17850 break;
17851 case Tag_CSKY_FPU_DENORMAL:
17852 READ_ULEB (val, p, end);
17853 if (val == 1)
17854 {
17855 printf (" Tag_CSKY_FPU_DENORMAL:\t");
17856 printf ("Needed\n");
17857 }
17858 break;
17859 case Tag_CSKY_FPU_Exception:
17860 READ_ULEB (val, p, end);
17861 if (val == 1)
17862 {
17863 printf (" Tag_CSKY_FPU_Exception:\t");
17864 printf ("Needed\n");
17865 }
17866 break;
17867 case Tag_CSKY_FPU_NUMBER_MODULE:
17868 printf (" Tag_CSKY_FPU_NUMBER_MODULE:\t");
17869 return display_tag_value (-1, p, end);
17870 case Tag_CSKY_FPU_HARDFP:
17871 printf (" Tag_CSKY_FPU_HARDFP:\t\t");
17872 READ_ULEB (val, p, end);
17873 if (val & VAL_CSKY_FPU_HARDFP_HALF)
17874 printf (" Half");
17875 if (val & VAL_CSKY_FPU_HARDFP_SINGLE)
17876 printf (" Single");
17877 if (val & VAL_CSKY_FPU_HARDFP_DOUBLE)
17878 printf (" Double");
17879 printf ("\n");
17880 break;
17881 default:
17882 return display_tag_value (tag, p, end);
17883 }
17884 return p;
17885 }
17886
17887 static bool
17888 process_attributes (Filedata * filedata,
17889 const char * public_name,
17890 unsigned int proc_type,
17891 unsigned char * (* display_pub_attribute) (unsigned char *, const unsigned char * const),
17892 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const))
17893 {
17894 Elf_Internal_Shdr * sect;
17895 unsigned i;
17896 bool res = true;
17897
17898 /* Find the section header so that we get the size. */
17899 for (i = 0, sect = filedata->section_headers;
17900 i < filedata->file_header.e_shnum;
17901 i++, sect++)
17902 {
17903 unsigned char * contents;
17904 unsigned char * p;
17905
17906 if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
17907 continue;
17908
17909 contents = (unsigned char *) get_data (NULL, filedata, sect->sh_offset, 1,
17910 sect->sh_size, _("attributes"));
17911 if (contents == NULL)
17912 {
17913 res = false;
17914 continue;
17915 }
17916
17917 p = contents;
17918 /* The first character is the version of the attributes.
17919 Currently only version 1, (aka 'A') is recognised here. */
17920 if (*p != 'A')
17921 {
17922 printf (_("Unknown attributes version '%c'(%d) - expecting 'A'\n"), *p, *p);
17923 res = false;
17924 }
17925 else
17926 {
17927 bfd_vma section_len;
17928
17929 section_len = sect->sh_size - 1;
17930 p++;
17931
17932 while (section_len > 0)
17933 {
17934 bfd_vma attr_len;
17935 unsigned int namelen;
17936 bool public_section;
17937 bool gnu_section;
17938
17939 if (section_len <= 4)
17940 {
17941 error (_("Tag section ends prematurely\n"));
17942 res = false;
17943 break;
17944 }
17945 attr_len = byte_get (p, 4);
17946 p += 4;
17947
17948 if (attr_len > section_len)
17949 {
17950 error (_("Bad attribute length (%u > %u)\n"),
17951 (unsigned) attr_len, (unsigned) section_len);
17952 attr_len = section_len;
17953 res = false;
17954 }
17955 /* PR 17531: file: 001-101425-0.004 */
17956 else if (attr_len < 5)
17957 {
17958 error (_("Attribute length of %u is too small\n"), (unsigned) attr_len);
17959 res = false;
17960 break;
17961 }
17962
17963 section_len -= attr_len;
17964 attr_len -= 4;
17965
17966 namelen = strnlen ((char *) p, attr_len) + 1;
17967 if (namelen == 0 || namelen >= attr_len)
17968 {
17969 error (_("Corrupt attribute section name\n"));
17970 res = false;
17971 break;
17972 }
17973
17974 printf (_("Attribute Section: "));
17975 print_symbol (INT_MAX, (const char *) p);
17976 putchar ('\n');
17977
17978 if (public_name && streq ((char *) p, public_name))
17979 public_section = true;
17980 else
17981 public_section = false;
17982
17983 if (streq ((char *) p, "gnu"))
17984 gnu_section = true;
17985 else
17986 gnu_section = false;
17987
17988 p += namelen;
17989 attr_len -= namelen;
17990
17991 while (attr_len > 0 && p < contents + sect->sh_size)
17992 {
17993 int tag;
17994 unsigned int val;
17995 bfd_vma size;
17996 unsigned char * end;
17997
17998 /* PR binutils/17531: Safe handling of corrupt files. */
17999 if (attr_len < 6)
18000 {
18001 error (_("Unused bytes at end of section\n"));
18002 res = false;
18003 section_len = 0;
18004 break;
18005 }
18006
18007 tag = *(p++);
18008 size = byte_get (p, 4);
18009 if (size > attr_len)
18010 {
18011 error (_("Bad subsection length (%u > %u)\n"),
18012 (unsigned) size, (unsigned) attr_len);
18013 res = false;
18014 size = attr_len;
18015 }
18016 /* PR binutils/17531: Safe handling of corrupt files. */
18017 if (size < 6)
18018 {
18019 error (_("Bad subsection length (%u < 6)\n"),
18020 (unsigned) size);
18021 res = false;
18022 section_len = 0;
18023 break;
18024 }
18025
18026 attr_len -= size;
18027 end = p + size - 1;
18028 assert (end <= contents + sect->sh_size);
18029 p += 4;
18030
18031 switch (tag)
18032 {
18033 case 1:
18034 printf (_("File Attributes\n"));
18035 break;
18036 case 2:
18037 printf (_("Section Attributes:"));
18038 goto do_numlist;
18039 case 3:
18040 printf (_("Symbol Attributes:"));
18041 /* Fall through. */
18042 do_numlist:
18043 for (;;)
18044 {
18045 READ_ULEB (val, p, end);
18046 if (val == 0)
18047 break;
18048 printf (" %d", val);
18049 }
18050 printf ("\n");
18051 break;
18052 default:
18053 printf (_("Unknown tag: %d\n"), tag);
18054 public_section = false;
18055 break;
18056 }
18057
18058 if (public_section && display_pub_attribute != NULL)
18059 {
18060 while (p < end)
18061 p = display_pub_attribute (p, end);
18062 assert (p == end);
18063 }
18064 else if (gnu_section && display_proc_gnu_attribute != NULL)
18065 {
18066 while (p < end)
18067 p = display_gnu_attribute (p,
18068 display_proc_gnu_attribute,
18069 end);
18070 assert (p == end);
18071 }
18072 else if (p < end)
18073 {
18074 printf (_(" Unknown attribute:\n"));
18075 display_raw_attribute (p, end);
18076 p = end;
18077 }
18078 else
18079 attr_len = 0;
18080 }
18081 }
18082 }
18083
18084 free (contents);
18085 }
18086
18087 return res;
18088 }
18089
18090 /* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
18091 Print the Address, Access and Initial fields of an entry at VMA ADDR
18092 and return the VMA of the next entry, or -1 if there was a problem.
18093 Does not read from DATA_END or beyond. */
18094
18095 static bfd_vma
18096 print_mips_got_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr,
18097 unsigned char * data_end)
18098 {
18099 printf (" ");
18100 print_vma (addr, LONG_HEX);
18101 printf (" ");
18102 if (addr < pltgot + 0xfff0)
18103 printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
18104 else
18105 printf ("%10s", "");
18106 printf (" ");
18107 if (data == NULL)
18108 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
18109 else
18110 {
18111 bfd_vma entry;
18112 unsigned char * from = data + addr - pltgot;
18113
18114 if (from + (is_32bit_elf ? 4 : 8) > data_end)
18115 {
18116 warn (_("MIPS GOT entry extends beyond the end of available data\n"));
18117 printf ("%*s", is_32bit_elf ? 8 : 16, _("<corrupt>"));
18118 return (bfd_vma) -1;
18119 }
18120 else
18121 {
18122 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
18123 print_vma (entry, LONG_HEX);
18124 }
18125 }
18126 return addr + (is_32bit_elf ? 4 : 8);
18127 }
18128
18129 /* DATA points to the contents of a MIPS PLT GOT that starts at VMA
18130 PLTGOT. Print the Address and Initial fields of an entry at VMA
18131 ADDR and return the VMA of the next entry. */
18132
18133 static bfd_vma
18134 print_mips_pltgot_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr)
18135 {
18136 printf (" ");
18137 print_vma (addr, LONG_HEX);
18138 printf (" ");
18139 if (data == NULL)
18140 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
18141 else
18142 {
18143 bfd_vma entry;
18144
18145 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
18146 print_vma (entry, LONG_HEX);
18147 }
18148 return addr + (is_32bit_elf ? 4 : 8);
18149 }
18150
18151 static void
18152 print_mips_ases (unsigned int mask)
18153 {
18154 if (mask & AFL_ASE_DSP)
18155 fputs ("\n\tDSP ASE", stdout);
18156 if (mask & AFL_ASE_DSPR2)
18157 fputs ("\n\tDSP R2 ASE", stdout);
18158 if (mask & AFL_ASE_DSPR3)
18159 fputs ("\n\tDSP R3 ASE", stdout);
18160 if (mask & AFL_ASE_EVA)
18161 fputs ("\n\tEnhanced VA Scheme", stdout);
18162 if (mask & AFL_ASE_MCU)
18163 fputs ("\n\tMCU (MicroController) ASE", stdout);
18164 if (mask & AFL_ASE_MDMX)
18165 fputs ("\n\tMDMX ASE", stdout);
18166 if (mask & AFL_ASE_MIPS3D)
18167 fputs ("\n\tMIPS-3D ASE", stdout);
18168 if (mask & AFL_ASE_MT)
18169 fputs ("\n\tMT ASE", stdout);
18170 if (mask & AFL_ASE_SMARTMIPS)
18171 fputs ("\n\tSmartMIPS ASE", stdout);
18172 if (mask & AFL_ASE_VIRT)
18173 fputs ("\n\tVZ ASE", stdout);
18174 if (mask & AFL_ASE_MSA)
18175 fputs ("\n\tMSA ASE", stdout);
18176 if (mask & AFL_ASE_MIPS16)
18177 fputs ("\n\tMIPS16 ASE", stdout);
18178 if (mask & AFL_ASE_MICROMIPS)
18179 fputs ("\n\tMICROMIPS ASE", stdout);
18180 if (mask & AFL_ASE_XPA)
18181 fputs ("\n\tXPA ASE", stdout);
18182 if (mask & AFL_ASE_MIPS16E2)
18183 fputs ("\n\tMIPS16e2 ASE", stdout);
18184 if (mask & AFL_ASE_CRC)
18185 fputs ("\n\tCRC ASE", stdout);
18186 if (mask & AFL_ASE_GINV)
18187 fputs ("\n\tGINV ASE", stdout);
18188 if (mask & AFL_ASE_LOONGSON_MMI)
18189 fputs ("\n\tLoongson MMI ASE", stdout);
18190 if (mask & AFL_ASE_LOONGSON_CAM)
18191 fputs ("\n\tLoongson CAM ASE", stdout);
18192 if (mask & AFL_ASE_LOONGSON_EXT)
18193 fputs ("\n\tLoongson EXT ASE", stdout);
18194 if (mask & AFL_ASE_LOONGSON_EXT2)
18195 fputs ("\n\tLoongson EXT2 ASE", stdout);
18196 if (mask == 0)
18197 fprintf (stdout, "\n\t%s", _("None"));
18198 else if ((mask & ~AFL_ASE_MASK) != 0)
18199 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
18200 }
18201
18202 static void
18203 print_mips_isa_ext (unsigned int isa_ext)
18204 {
18205 switch (isa_ext)
18206 {
18207 case 0:
18208 fputs (_("None"), stdout);
18209 break;
18210 case AFL_EXT_XLR:
18211 fputs ("RMI XLR", stdout);
18212 break;
18213 case AFL_EXT_OCTEON3:
18214 fputs ("Cavium Networks Octeon3", stdout);
18215 break;
18216 case AFL_EXT_OCTEON2:
18217 fputs ("Cavium Networks Octeon2", stdout);
18218 break;
18219 case AFL_EXT_OCTEONP:
18220 fputs ("Cavium Networks OcteonP", stdout);
18221 break;
18222 case AFL_EXT_OCTEON:
18223 fputs ("Cavium Networks Octeon", stdout);
18224 break;
18225 case AFL_EXT_5900:
18226 fputs ("Toshiba R5900", stdout);
18227 break;
18228 case AFL_EXT_4650:
18229 fputs ("MIPS R4650", stdout);
18230 break;
18231 case AFL_EXT_4010:
18232 fputs ("LSI R4010", stdout);
18233 break;
18234 case AFL_EXT_4100:
18235 fputs ("NEC VR4100", stdout);
18236 break;
18237 case AFL_EXT_3900:
18238 fputs ("Toshiba R3900", stdout);
18239 break;
18240 case AFL_EXT_10000:
18241 fputs ("MIPS R10000", stdout);
18242 break;
18243 case AFL_EXT_SB1:
18244 fputs ("Broadcom SB-1", stdout);
18245 break;
18246 case AFL_EXT_4111:
18247 fputs ("NEC VR4111/VR4181", stdout);
18248 break;
18249 case AFL_EXT_4120:
18250 fputs ("NEC VR4120", stdout);
18251 break;
18252 case AFL_EXT_5400:
18253 fputs ("NEC VR5400", stdout);
18254 break;
18255 case AFL_EXT_5500:
18256 fputs ("NEC VR5500", stdout);
18257 break;
18258 case AFL_EXT_LOONGSON_2E:
18259 fputs ("ST Microelectronics Loongson 2E", stdout);
18260 break;
18261 case AFL_EXT_LOONGSON_2F:
18262 fputs ("ST Microelectronics Loongson 2F", stdout);
18263 break;
18264 case AFL_EXT_INTERAPTIV_MR2:
18265 fputs ("Imagination interAptiv MR2", stdout);
18266 break;
18267 default:
18268 fprintf (stdout, "%s (%d)", _("Unknown"), isa_ext);
18269 }
18270 }
18271
18272 static signed int
18273 get_mips_reg_size (int reg_size)
18274 {
18275 return (reg_size == AFL_REG_NONE) ? 0
18276 : (reg_size == AFL_REG_32) ? 32
18277 : (reg_size == AFL_REG_64) ? 64
18278 : (reg_size == AFL_REG_128) ? 128
18279 : -1;
18280 }
18281
18282 static bool
18283 process_mips_specific (Filedata * filedata)
18284 {
18285 Elf_Internal_Dyn * entry;
18286 Elf_Internal_Shdr *sect = NULL;
18287 size_t liblist_offset = 0;
18288 size_t liblistno = 0;
18289 size_t conflictsno = 0;
18290 size_t options_offset = 0;
18291 size_t conflicts_offset = 0;
18292 size_t pltrelsz = 0;
18293 size_t pltrel = 0;
18294 bfd_vma pltgot = 0;
18295 bfd_vma mips_pltgot = 0;
18296 bfd_vma jmprel = 0;
18297 bfd_vma local_gotno = 0;
18298 bfd_vma gotsym = 0;
18299 bfd_vma symtabno = 0;
18300 bool res = true;
18301
18302 if (! process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
18303 display_mips_gnu_attribute))
18304 res = false;
18305
18306 sect = find_section (filedata, ".MIPS.abiflags");
18307
18308 if (sect != NULL)
18309 {
18310 Elf_External_ABIFlags_v0 *abiflags_ext;
18311 Elf_Internal_ABIFlags_v0 abiflags_in;
18312
18313 if (sizeof (Elf_External_ABIFlags_v0) != sect->sh_size)
18314 {
18315 error (_("Corrupt MIPS ABI Flags section.\n"));
18316 res = false;
18317 }
18318 else
18319 {
18320 abiflags_ext = get_data (NULL, filedata, sect->sh_offset, 1,
18321 sect->sh_size, _("MIPS ABI Flags section"));
18322 if (abiflags_ext)
18323 {
18324 abiflags_in.version = BYTE_GET (abiflags_ext->version);
18325 abiflags_in.isa_level = BYTE_GET (abiflags_ext->isa_level);
18326 abiflags_in.isa_rev = BYTE_GET (abiflags_ext->isa_rev);
18327 abiflags_in.gpr_size = BYTE_GET (abiflags_ext->gpr_size);
18328 abiflags_in.cpr1_size = BYTE_GET (abiflags_ext->cpr1_size);
18329 abiflags_in.cpr2_size = BYTE_GET (abiflags_ext->cpr2_size);
18330 abiflags_in.fp_abi = BYTE_GET (abiflags_ext->fp_abi);
18331 abiflags_in.isa_ext = BYTE_GET (abiflags_ext->isa_ext);
18332 abiflags_in.ases = BYTE_GET (abiflags_ext->ases);
18333 abiflags_in.flags1 = BYTE_GET (abiflags_ext->flags1);
18334 abiflags_in.flags2 = BYTE_GET (abiflags_ext->flags2);
18335
18336 printf ("\nMIPS ABI Flags Version: %d\n", abiflags_in.version);
18337 printf ("\nISA: MIPS%d", abiflags_in.isa_level);
18338 if (abiflags_in.isa_rev > 1)
18339 printf ("r%d", abiflags_in.isa_rev);
18340 printf ("\nGPR size: %d",
18341 get_mips_reg_size (abiflags_in.gpr_size));
18342 printf ("\nCPR1 size: %d",
18343 get_mips_reg_size (abiflags_in.cpr1_size));
18344 printf ("\nCPR2 size: %d",
18345 get_mips_reg_size (abiflags_in.cpr2_size));
18346 fputs ("\nFP ABI: ", stdout);
18347 print_mips_fp_abi_value (abiflags_in.fp_abi);
18348 fputs ("ISA Extension: ", stdout);
18349 print_mips_isa_ext (abiflags_in.isa_ext);
18350 fputs ("\nASEs:", stdout);
18351 print_mips_ases (abiflags_in.ases);
18352 printf ("\nFLAGS 1: %8.8lx", abiflags_in.flags1);
18353 printf ("\nFLAGS 2: %8.8lx", abiflags_in.flags2);
18354 fputc ('\n', stdout);
18355 free (abiflags_ext);
18356 }
18357 }
18358 }
18359
18360 /* We have a lot of special sections. Thanks SGI! */
18361 if (filedata->dynamic_section == NULL)
18362 {
18363 /* No dynamic information available. See if there is static GOT. */
18364 sect = find_section (filedata, ".got");
18365 if (sect != NULL)
18366 {
18367 unsigned char *data_end;
18368 unsigned char *data;
18369 bfd_vma ent, end;
18370 int addr_size;
18371
18372 pltgot = sect->sh_addr;
18373
18374 ent = pltgot;
18375 addr_size = (is_32bit_elf ? 4 : 8);
18376 end = pltgot + sect->sh_size;
18377
18378 data = (unsigned char *) get_data (NULL, filedata, sect->sh_offset,
18379 end - pltgot, 1,
18380 _("Global Offset Table data"));
18381 /* PR 12855: Null data is handled gracefully throughout. */
18382 data_end = data + (end - pltgot);
18383
18384 printf (_("\nStatic GOT:\n"));
18385 printf (_(" Canonical gp value: "));
18386 print_vma (ent + 0x7ff0, LONG_HEX);
18387 printf ("\n\n");
18388
18389 /* In a dynamic binary GOT[0] is reserved for the dynamic
18390 loader to store the lazy resolver pointer, however in
18391 a static binary it may well have been omitted and GOT
18392 reduced to a table of addresses.
18393 PR 21344: Check for the entry being fully available
18394 before fetching it. */
18395 if (data
18396 && data + ent - pltgot + addr_size <= data_end
18397 && byte_get (data + ent - pltgot, addr_size) == 0)
18398 {
18399 printf (_(" Reserved entries:\n"));
18400 printf (_(" %*s %10s %*s\n"),
18401 addr_size * 2, _("Address"), _("Access"),
18402 addr_size * 2, _("Value"));
18403 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18404 printf ("\n");
18405 if (ent == (bfd_vma) -1)
18406 goto sgot_print_fail;
18407
18408 /* Check for the MSB of GOT[1] being set, identifying a
18409 GNU object. This entry will be used by some runtime
18410 loaders, to store the module pointer. Otherwise this
18411 is an ordinary local entry.
18412 PR 21344: Check for the entry being fully available
18413 before fetching it. */
18414 if (data
18415 && data + ent - pltgot + addr_size <= data_end
18416 && (byte_get (data + ent - pltgot, addr_size)
18417 >> (addr_size * 8 - 1)) != 0)
18418 {
18419 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18420 printf ("\n");
18421 if (ent == (bfd_vma) -1)
18422 goto sgot_print_fail;
18423 }
18424 printf ("\n");
18425 }
18426
18427 if (data != NULL && ent < end)
18428 {
18429 printf (_(" Local entries:\n"));
18430 printf (" %*s %10s %*s\n",
18431 addr_size * 2, _("Address"), _("Access"),
18432 addr_size * 2, _("Value"));
18433 while (ent < end)
18434 {
18435 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18436 printf ("\n");
18437 if (ent == (bfd_vma) -1)
18438 goto sgot_print_fail;
18439 }
18440 printf ("\n");
18441 }
18442
18443 sgot_print_fail:
18444 free (data);
18445 }
18446 return res;
18447 }
18448
18449 for (entry = filedata->dynamic_section;
18450 /* PR 17531 file: 012-50589-0.004. */
18451 (entry < filedata->dynamic_section + filedata->dynamic_nent
18452 && entry->d_tag != DT_NULL);
18453 ++entry)
18454 switch (entry->d_tag)
18455 {
18456 case DT_MIPS_LIBLIST:
18457 liblist_offset
18458 = offset_from_vma (filedata, entry->d_un.d_val,
18459 liblistno * sizeof (Elf32_External_Lib));
18460 break;
18461 case DT_MIPS_LIBLISTNO:
18462 liblistno = entry->d_un.d_val;
18463 break;
18464 case DT_MIPS_OPTIONS:
18465 options_offset = offset_from_vma (filedata, entry->d_un.d_val, 0);
18466 break;
18467 case DT_MIPS_CONFLICT:
18468 conflicts_offset
18469 = offset_from_vma (filedata, entry->d_un.d_val,
18470 conflictsno * sizeof (Elf32_External_Conflict));
18471 break;
18472 case DT_MIPS_CONFLICTNO:
18473 conflictsno = entry->d_un.d_val;
18474 break;
18475 case DT_PLTGOT:
18476 pltgot = entry->d_un.d_ptr;
18477 break;
18478 case DT_MIPS_LOCAL_GOTNO:
18479 local_gotno = entry->d_un.d_val;
18480 break;
18481 case DT_MIPS_GOTSYM:
18482 gotsym = entry->d_un.d_val;
18483 break;
18484 case DT_MIPS_SYMTABNO:
18485 symtabno = entry->d_un.d_val;
18486 break;
18487 case DT_MIPS_PLTGOT:
18488 mips_pltgot = entry->d_un.d_ptr;
18489 break;
18490 case DT_PLTREL:
18491 pltrel = entry->d_un.d_val;
18492 break;
18493 case DT_PLTRELSZ:
18494 pltrelsz = entry->d_un.d_val;
18495 break;
18496 case DT_JMPREL:
18497 jmprel = entry->d_un.d_ptr;
18498 break;
18499 default:
18500 break;
18501 }
18502
18503 if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
18504 {
18505 Elf32_External_Lib * elib;
18506 size_t cnt;
18507
18508 elib = (Elf32_External_Lib *) get_data (NULL, filedata, liblist_offset,
18509 sizeof (Elf32_External_Lib),
18510 liblistno,
18511 _("liblist section data"));
18512 if (elib)
18513 {
18514 printf (ngettext ("\nSection '.liblist' contains %lu entry:\n",
18515 "\nSection '.liblist' contains %lu entries:\n",
18516 (unsigned long) liblistno),
18517 (unsigned long) liblistno);
18518 fputs (_(" Library Time Stamp Checksum Version Flags\n"),
18519 stdout);
18520
18521 for (cnt = 0; cnt < liblistno; ++cnt)
18522 {
18523 Elf32_Lib liblist;
18524 time_t atime;
18525 char timebuf[128];
18526 struct tm * tmp;
18527
18528 liblist.l_name = BYTE_GET (elib[cnt].l_name);
18529 atime = BYTE_GET (elib[cnt].l_time_stamp);
18530 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
18531 liblist.l_version = BYTE_GET (elib[cnt].l_version);
18532 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
18533
18534 tmp = gmtime (&atime);
18535 snprintf (timebuf, sizeof (timebuf),
18536 "%04u-%02u-%02uT%02u:%02u:%02u",
18537 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
18538 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
18539
18540 printf ("%3lu: ", (unsigned long) cnt);
18541 if (valid_dynamic_name (filedata, liblist.l_name))
18542 print_symbol (20, get_dynamic_name (filedata, liblist.l_name));
18543 else
18544 printf (_("<corrupt: %9ld>"), liblist.l_name);
18545 printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
18546 liblist.l_version);
18547
18548 if (liblist.l_flags == 0)
18549 puts (_(" NONE"));
18550 else
18551 {
18552 static const struct
18553 {
18554 const char * name;
18555 int bit;
18556 }
18557 l_flags_vals[] =
18558 {
18559 { " EXACT_MATCH", LL_EXACT_MATCH },
18560 { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
18561 { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
18562 { " EXPORTS", LL_EXPORTS },
18563 { " DELAY_LOAD", LL_DELAY_LOAD },
18564 { " DELTA", LL_DELTA }
18565 };
18566 int flags = liblist.l_flags;
18567 size_t fcnt;
18568
18569 for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
18570 if ((flags & l_flags_vals[fcnt].bit) != 0)
18571 {
18572 fputs (l_flags_vals[fcnt].name, stdout);
18573 flags ^= l_flags_vals[fcnt].bit;
18574 }
18575 if (flags != 0)
18576 printf (" %#x", (unsigned int) flags);
18577
18578 puts ("");
18579 }
18580 }
18581
18582 free (elib);
18583 }
18584 else
18585 res = false;
18586 }
18587
18588 if (options_offset != 0)
18589 {
18590 Elf_External_Options * eopt;
18591 size_t offset;
18592 int cnt;
18593
18594 /* Find the section header so that we get the size. */
18595 sect = find_section_by_type (filedata, SHT_MIPS_OPTIONS);
18596 /* PR 17533 file: 012-277276-0.004. */
18597 if (sect == NULL)
18598 {
18599 error (_("No MIPS_OPTIONS header found\n"));
18600 return false;
18601 }
18602 /* PR 24243 */
18603 if (sect->sh_size < sizeof (* eopt))
18604 {
18605 error (_("The MIPS options section is too small.\n"));
18606 return false;
18607 }
18608
18609 eopt = (Elf_External_Options *) get_data (NULL, filedata, options_offset, 1,
18610 sect->sh_size, _("options"));
18611 if (eopt)
18612 {
18613 Elf_Internal_Options option;
18614
18615 offset = cnt = 0;
18616 while (offset <= sect->sh_size - sizeof (* eopt))
18617 {
18618 Elf_External_Options * eoption;
18619 unsigned int optsize;
18620
18621 eoption = (Elf_External_Options *) ((char *) eopt + offset);
18622
18623 optsize = BYTE_GET (eoption->size);
18624
18625 /* PR 17531: file: ffa0fa3b. */
18626 if (optsize < sizeof (* eopt)
18627 || optsize > sect->sh_size - offset)
18628 {
18629 error (_("Invalid size (%u) for MIPS option\n"),
18630 optsize);
18631 free (eopt);
18632 return false;
18633 }
18634 offset += optsize;
18635 ++cnt;
18636 }
18637
18638 printf (ngettext ("\nSection '%s' contains %d entry:\n",
18639 "\nSection '%s' contains %d entries:\n",
18640 cnt),
18641 printable_section_name (filedata, sect), cnt);
18642
18643 offset = 0;
18644 while (cnt-- > 0)
18645 {
18646 size_t len;
18647 Elf_External_Options * eoption;
18648
18649 eoption = (Elf_External_Options *) ((char *) eopt + offset);
18650
18651 option.kind = BYTE_GET (eoption->kind);
18652 option.size = BYTE_GET (eoption->size);
18653 option.section = BYTE_GET (eoption->section);
18654 option.info = BYTE_GET (eoption->info);
18655
18656 switch (option.kind)
18657 {
18658 case ODK_NULL:
18659 /* This shouldn't happen. */
18660 printf (" NULL %" PRId16 " %" PRIx32,
18661 option.section, option.info);
18662 break;
18663
18664 case ODK_REGINFO:
18665 printf (" REGINFO ");
18666 if (filedata->file_header.e_machine == EM_MIPS)
18667 {
18668 Elf32_External_RegInfo * ereg;
18669 Elf32_RegInfo reginfo;
18670
18671 /* 32bit form. */
18672 if (option.size < (sizeof (Elf_External_Options)
18673 + sizeof (Elf32_External_RegInfo)))
18674 {
18675 printf (_("<corrupt>\n"));
18676 error (_("Truncated MIPS REGINFO option\n"));
18677 cnt = 0;
18678 break;
18679 }
18680
18681 ereg = (Elf32_External_RegInfo *) (eoption + 1);
18682
18683 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
18684 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
18685 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
18686 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
18687 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
18688 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
18689
18690 printf ("GPR %08" PRIx32 " GP 0x%" PRIx32 "\n",
18691 reginfo.ri_gprmask, reginfo.ri_gp_value);
18692 printf (" "
18693 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
18694 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
18695 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
18696 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
18697 }
18698 else
18699 {
18700 /* 64 bit form. */
18701 Elf64_External_RegInfo * ereg;
18702 Elf64_Internal_RegInfo reginfo;
18703
18704 if (option.size < (sizeof (Elf_External_Options)
18705 + sizeof (Elf64_External_RegInfo)))
18706 {
18707 printf (_("<corrupt>\n"));
18708 error (_("Truncated MIPS REGINFO option\n"));
18709 cnt = 0;
18710 break;
18711 }
18712
18713 ereg = (Elf64_External_RegInfo *) (eoption + 1);
18714 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
18715 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
18716 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
18717 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
18718 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
18719 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
18720
18721 printf ("GPR %08" PRIx32 " GP 0x%" PRIx64 "\n",
18722 reginfo.ri_gprmask, reginfo.ri_gp_value);
18723 printf (" "
18724 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
18725 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
18726 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
18727 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
18728 }
18729 offset += option.size;
18730 continue;
18731
18732 case ODK_EXCEPTIONS:
18733 fputs (" EXCEPTIONS fpe_min(", stdout);
18734 process_mips_fpe_exception (option.info & OEX_FPU_MIN);
18735 fputs (") fpe_max(", stdout);
18736 process_mips_fpe_exception ((option.info & OEX_FPU_MAX) >> 8);
18737 fputs (")", stdout);
18738
18739 if (option.info & OEX_PAGE0)
18740 fputs (" PAGE0", stdout);
18741 if (option.info & OEX_SMM)
18742 fputs (" SMM", stdout);
18743 if (option.info & OEX_FPDBUG)
18744 fputs (" FPDBUG", stdout);
18745 if (option.info & OEX_DISMISS)
18746 fputs (" DISMISS", stdout);
18747 break;
18748
18749 case ODK_PAD:
18750 fputs (" PAD ", stdout);
18751 if (option.info & OPAD_PREFIX)
18752 fputs (" PREFIX", stdout);
18753 if (option.info & OPAD_POSTFIX)
18754 fputs (" POSTFIX", stdout);
18755 if (option.info & OPAD_SYMBOL)
18756 fputs (" SYMBOL", stdout);
18757 break;
18758
18759 case ODK_HWPATCH:
18760 fputs (" HWPATCH ", stdout);
18761 if (option.info & OHW_R4KEOP)
18762 fputs (" R4KEOP", stdout);
18763 if (option.info & OHW_R8KPFETCH)
18764 fputs (" R8KPFETCH", stdout);
18765 if (option.info & OHW_R5KEOP)
18766 fputs (" R5KEOP", stdout);
18767 if (option.info & OHW_R5KCVTL)
18768 fputs (" R5KCVTL", stdout);
18769 break;
18770
18771 case ODK_FILL:
18772 fputs (" FILL ", stdout);
18773 /* XXX Print content of info word? */
18774 break;
18775
18776 case ODK_TAGS:
18777 fputs (" TAGS ", stdout);
18778 /* XXX Print content of info word? */
18779 break;
18780
18781 case ODK_HWAND:
18782 fputs (" HWAND ", stdout);
18783 if (option.info & OHWA0_R4KEOP_CHECKED)
18784 fputs (" R4KEOP_CHECKED", stdout);
18785 if (option.info & OHWA0_R4KEOP_CLEAN)
18786 fputs (" R4KEOP_CLEAN", stdout);
18787 break;
18788
18789 case ODK_HWOR:
18790 fputs (" HWOR ", stdout);
18791 if (option.info & OHWA0_R4KEOP_CHECKED)
18792 fputs (" R4KEOP_CHECKED", stdout);
18793 if (option.info & OHWA0_R4KEOP_CLEAN)
18794 fputs (" R4KEOP_CLEAN", stdout);
18795 break;
18796
18797 case ODK_GP_GROUP:
18798 printf (" GP_GROUP %#06x self-contained %#06x",
18799 option.info & OGP_GROUP,
18800 (option.info & OGP_SELF) >> 16);
18801 break;
18802
18803 case ODK_IDENT:
18804 printf (" IDENT %#06x self-contained %#06x",
18805 option.info & OGP_GROUP,
18806 (option.info & OGP_SELF) >> 16);
18807 break;
18808
18809 default:
18810 /* This shouldn't happen. */
18811 printf (" %3d ??? %" PRId16 " %" PRIx32,
18812 option.kind, option.section, option.info);
18813 break;
18814 }
18815
18816 len = sizeof (* eopt);
18817 while (len < option.size)
18818 {
18819 unsigned char datum = *((unsigned char *) eoption + len);
18820
18821 if (ISPRINT (datum))
18822 printf ("%c", datum);
18823 else
18824 printf ("\\%03o", datum);
18825 len ++;
18826 }
18827 fputs ("\n", stdout);
18828
18829 offset += option.size;
18830 }
18831 free (eopt);
18832 }
18833 else
18834 res = false;
18835 }
18836
18837 if (conflicts_offset != 0 && conflictsno != 0)
18838 {
18839 Elf32_Conflict * iconf;
18840 size_t cnt;
18841
18842 if (filedata->dynamic_symbols == NULL)
18843 {
18844 error (_("conflict list found without a dynamic symbol table\n"));
18845 return false;
18846 }
18847
18848 /* PR 21345 - print a slightly more helpful error message
18849 if we are sure that the cmalloc will fail. */
18850 if (conflictsno > filedata->file_size / sizeof (* iconf))
18851 {
18852 error (_("Overlarge number of conflicts detected: %lx\n"),
18853 (long) conflictsno);
18854 return false;
18855 }
18856
18857 iconf = (Elf32_Conflict *) cmalloc (conflictsno, sizeof (* iconf));
18858 if (iconf == NULL)
18859 {
18860 error (_("Out of memory allocating space for dynamic conflicts\n"));
18861 return false;
18862 }
18863
18864 if (is_32bit_elf)
18865 {
18866 Elf32_External_Conflict * econf32;
18867
18868 econf32 = (Elf32_External_Conflict *)
18869 get_data (NULL, filedata, conflicts_offset,
18870 sizeof (*econf32), conflictsno, _("conflict"));
18871 if (!econf32)
18872 {
18873 free (iconf);
18874 return false;
18875 }
18876
18877 for (cnt = 0; cnt < conflictsno; ++cnt)
18878 iconf[cnt] = BYTE_GET (econf32[cnt]);
18879
18880 free (econf32);
18881 }
18882 else
18883 {
18884 Elf64_External_Conflict * econf64;
18885
18886 econf64 = (Elf64_External_Conflict *)
18887 get_data (NULL, filedata, conflicts_offset,
18888 sizeof (*econf64), conflictsno, _("conflict"));
18889 if (!econf64)
18890 {
18891 free (iconf);
18892 return false;
18893 }
18894
18895 for (cnt = 0; cnt < conflictsno; ++cnt)
18896 iconf[cnt] = BYTE_GET (econf64[cnt]);
18897
18898 free (econf64);
18899 }
18900
18901 printf (ngettext ("\nSection '.conflict' contains %lu entry:\n",
18902 "\nSection '.conflict' contains %lu entries:\n",
18903 (unsigned long) conflictsno),
18904 (unsigned long) conflictsno);
18905 puts (_(" Num: Index Value Name"));
18906
18907 for (cnt = 0; cnt < conflictsno; ++cnt)
18908 {
18909 printf ("%5lu: %8lu ", (unsigned long) cnt, iconf[cnt]);
18910
18911 if (iconf[cnt] >= filedata->num_dynamic_syms)
18912 printf (_("<corrupt symbol index>"));
18913 else
18914 {
18915 Elf_Internal_Sym * psym;
18916
18917 psym = & filedata->dynamic_symbols[iconf[cnt]];
18918 print_vma (psym->st_value, FULL_HEX);
18919 putchar (' ');
18920 if (valid_dynamic_name (filedata, psym->st_name))
18921 print_symbol (25, get_dynamic_name (filedata, psym->st_name));
18922 else
18923 printf (_("<corrupt: %14ld>"), psym->st_name);
18924 }
18925 putchar ('\n');
18926 }
18927
18928 free (iconf);
18929 }
18930
18931 if (pltgot != 0 && local_gotno != 0)
18932 {
18933 bfd_vma ent, local_end, global_end;
18934 size_t i, offset;
18935 unsigned char * data;
18936 unsigned char * data_end;
18937 int addr_size;
18938
18939 ent = pltgot;
18940 addr_size = (is_32bit_elf ? 4 : 8);
18941 local_end = pltgot + local_gotno * addr_size;
18942
18943 /* PR binutils/17533 file: 012-111227-0.004 */
18944 if (symtabno < gotsym)
18945 {
18946 error (_("The GOT symbol offset (%lu) is greater than the symbol table size (%lu)\n"),
18947 (unsigned long) gotsym, (unsigned long) symtabno);
18948 return false;
18949 }
18950
18951 global_end = local_end + (symtabno - gotsym) * addr_size;
18952 /* PR 17531: file: 54c91a34. */
18953 if (global_end < local_end)
18954 {
18955 error (_("Too many GOT symbols: %lu\n"), (unsigned long) symtabno);
18956 return false;
18957 }
18958
18959 offset = offset_from_vma (filedata, pltgot, global_end - pltgot);
18960 data = (unsigned char *) get_data (NULL, filedata, offset,
18961 global_end - pltgot, 1,
18962 _("Global Offset Table data"));
18963 /* PR 12855: Null data is handled gracefully throughout. */
18964 data_end = data + (global_end - pltgot);
18965
18966 printf (_("\nPrimary GOT:\n"));
18967 printf (_(" Canonical gp value: "));
18968 print_vma (pltgot + 0x7ff0, LONG_HEX);
18969 printf ("\n\n");
18970
18971 printf (_(" Reserved entries:\n"));
18972 printf (_(" %*s %10s %*s Purpose\n"),
18973 addr_size * 2, _("Address"), _("Access"),
18974 addr_size * 2, _("Initial"));
18975 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18976 printf (_(" Lazy resolver\n"));
18977 if (ent == (bfd_vma) -1)
18978 goto got_print_fail;
18979
18980 /* Check for the MSB of GOT[1] being set, denoting a GNU object.
18981 This entry will be used by some runtime loaders, to store the
18982 module pointer. Otherwise this is an ordinary local entry.
18983 PR 21344: Check for the entry being fully available before
18984 fetching it. */
18985 if (data
18986 && data + ent - pltgot + addr_size <= data_end
18987 && (byte_get (data + ent - pltgot, addr_size)
18988 >> (addr_size * 8 - 1)) != 0)
18989 {
18990 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18991 printf (_(" Module pointer (GNU extension)\n"));
18992 if (ent == (bfd_vma) -1)
18993 goto got_print_fail;
18994 }
18995 printf ("\n");
18996
18997 if (data != NULL && ent < local_end)
18998 {
18999 printf (_(" Local entries:\n"));
19000 printf (" %*s %10s %*s\n",
19001 addr_size * 2, _("Address"), _("Access"),
19002 addr_size * 2, _("Initial"));
19003 while (ent < local_end)
19004 {
19005 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19006 printf ("\n");
19007 if (ent == (bfd_vma) -1)
19008 goto got_print_fail;
19009 }
19010 printf ("\n");
19011 }
19012
19013 if (data != NULL && gotsym < symtabno)
19014 {
19015 int sym_width;
19016
19017 printf (_(" Global entries:\n"));
19018 printf (" %*s %10s %*s %*s %-7s %3s %s\n",
19019 addr_size * 2, _("Address"),
19020 _("Access"),
19021 addr_size * 2, _("Initial"),
19022 addr_size * 2, _("Sym.Val."),
19023 _("Type"),
19024 /* Note for translators: "Ndx" = abbreviated form of "Index". */
19025 _("Ndx"), _("Name"));
19026
19027 sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
19028
19029 for (i = gotsym; i < symtabno; i++)
19030 {
19031 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19032 printf (" ");
19033
19034 if (filedata->dynamic_symbols == NULL)
19035 printf (_("<no dynamic symbols>"));
19036 else if (i < filedata->num_dynamic_syms)
19037 {
19038 Elf_Internal_Sym * psym = filedata->dynamic_symbols + i;
19039
19040 print_vma (psym->st_value, LONG_HEX);
19041 printf (" %-7s %3s ",
19042 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
19043 get_symbol_index_type (filedata, psym->st_shndx));
19044
19045 if (valid_dynamic_name (filedata, psym->st_name))
19046 print_symbol (sym_width,
19047 get_dynamic_name (filedata, psym->st_name));
19048 else
19049 printf (_("<corrupt: %14ld>"), psym->st_name);
19050 }
19051 else
19052 printf (_("<symbol index %lu exceeds number of dynamic symbols>"),
19053 (unsigned long) i);
19054
19055 printf ("\n");
19056 if (ent == (bfd_vma) -1)
19057 break;
19058 }
19059 printf ("\n");
19060 }
19061
19062 got_print_fail:
19063 free (data);
19064 }
19065
19066 if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
19067 {
19068 bfd_vma ent, end;
19069 size_t offset, rel_offset;
19070 unsigned long count, i;
19071 unsigned char * data;
19072 int addr_size, sym_width;
19073 Elf_Internal_Rela * rels;
19074
19075 rel_offset = offset_from_vma (filedata, jmprel, pltrelsz);
19076 if (pltrel == DT_RELA)
19077 {
19078 if (!slurp_rela_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
19079 return false;
19080 }
19081 else
19082 {
19083 if (!slurp_rel_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
19084 return false;
19085 }
19086
19087 ent = mips_pltgot;
19088 addr_size = (is_32bit_elf ? 4 : 8);
19089 end = mips_pltgot + (2 + count) * addr_size;
19090
19091 offset = offset_from_vma (filedata, mips_pltgot, end - mips_pltgot);
19092 data = (unsigned char *) get_data (NULL, filedata, offset, end - mips_pltgot,
19093 1, _("Procedure Linkage Table data"));
19094 if (data == NULL)
19095 {
19096 free (rels);
19097 return false;
19098 }
19099
19100 printf ("\nPLT GOT:\n\n");
19101 printf (_(" Reserved entries:\n"));
19102 printf (_(" %*s %*s Purpose\n"),
19103 addr_size * 2, _("Address"), addr_size * 2, _("Initial"));
19104 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
19105 printf (_(" PLT lazy resolver\n"));
19106 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
19107 printf (_(" Module pointer\n"));
19108 printf ("\n");
19109
19110 printf (_(" Entries:\n"));
19111 printf (" %*s %*s %*s %-7s %3s %s\n",
19112 addr_size * 2, _("Address"),
19113 addr_size * 2, _("Initial"),
19114 addr_size * 2, _("Sym.Val."), _("Type"), _("Ndx"), _("Name"));
19115 sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
19116 for (i = 0; i < count; i++)
19117 {
19118 unsigned long idx = get_reloc_symindex (rels[i].r_info);
19119
19120 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
19121 printf (" ");
19122
19123 if (idx >= filedata->num_dynamic_syms)
19124 printf (_("<corrupt symbol index: %lu>"), idx);
19125 else
19126 {
19127 Elf_Internal_Sym * psym = filedata->dynamic_symbols + idx;
19128
19129 print_vma (psym->st_value, LONG_HEX);
19130 printf (" %-7s %3s ",
19131 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
19132 get_symbol_index_type (filedata, psym->st_shndx));
19133 if (valid_dynamic_name (filedata, psym->st_name))
19134 print_symbol (sym_width,
19135 get_dynamic_name (filedata, psym->st_name));
19136 else
19137 printf (_("<corrupt: %14ld>"), psym->st_name);
19138 }
19139 printf ("\n");
19140 }
19141 printf ("\n");
19142
19143 free (data);
19144 free (rels);
19145 }
19146
19147 return res;
19148 }
19149
19150 static bool
19151 process_nds32_specific (Filedata * filedata)
19152 {
19153 Elf_Internal_Shdr *sect = NULL;
19154
19155 sect = find_section (filedata, ".nds32_e_flags");
19156 if (sect != NULL && sect->sh_size >= 4)
19157 {
19158 unsigned char *buf;
19159 unsigned int flag;
19160
19161 printf ("\nNDS32 elf flags section:\n");
19162 buf = get_data (NULL, filedata, sect->sh_offset, 1, 4,
19163 _("NDS32 elf flags section"));
19164
19165 if (buf == NULL)
19166 return false;
19167
19168 flag = byte_get (buf, 4);
19169 free (buf);
19170 switch (flag & 0x3)
19171 {
19172 case 0:
19173 printf ("(VEC_SIZE):\tNo entry.\n");
19174 break;
19175 case 1:
19176 printf ("(VEC_SIZE):\t4 bytes\n");
19177 break;
19178 case 2:
19179 printf ("(VEC_SIZE):\t16 bytes\n");
19180 break;
19181 case 3:
19182 printf ("(VEC_SIZE):\treserved\n");
19183 break;
19184 }
19185 }
19186
19187 return true;
19188 }
19189
19190 static bool
19191 process_gnu_liblist (Filedata * filedata)
19192 {
19193 Elf_Internal_Shdr * section;
19194 Elf_Internal_Shdr * string_sec;
19195 Elf32_External_Lib * elib;
19196 char * strtab;
19197 size_t strtab_size;
19198 size_t cnt;
19199 unsigned long num_liblist;
19200 unsigned i;
19201 bool res = true;
19202
19203 if (! do_arch)
19204 return true;
19205
19206 for (i = 0, section = filedata->section_headers;
19207 i < filedata->file_header.e_shnum;
19208 i++, section++)
19209 {
19210 switch (section->sh_type)
19211 {
19212 case SHT_GNU_LIBLIST:
19213 if (section->sh_link >= filedata->file_header.e_shnum)
19214 break;
19215
19216 elib = (Elf32_External_Lib *)
19217 get_data (NULL, filedata, section->sh_offset, 1, section->sh_size,
19218 _("liblist section data"));
19219
19220 if (elib == NULL)
19221 {
19222 res = false;
19223 break;
19224 }
19225
19226 string_sec = filedata->section_headers + section->sh_link;
19227 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
19228 string_sec->sh_size,
19229 _("liblist string table"));
19230 if (strtab == NULL
19231 || section->sh_entsize != sizeof (Elf32_External_Lib))
19232 {
19233 free (elib);
19234 free (strtab);
19235 res = false;
19236 break;
19237 }
19238 strtab_size = string_sec->sh_size;
19239
19240 num_liblist = section->sh_size / sizeof (Elf32_External_Lib);
19241 printf (ngettext ("\nLibrary list section '%s' contains %lu entries:\n",
19242 "\nLibrary list section '%s' contains %lu entries:\n",
19243 num_liblist),
19244 printable_section_name (filedata, section),
19245 num_liblist);
19246
19247 puts (_(" Library Time Stamp Checksum Version Flags"));
19248
19249 for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
19250 ++cnt)
19251 {
19252 Elf32_Lib liblist;
19253 time_t atime;
19254 char timebuf[128];
19255 struct tm * tmp;
19256
19257 liblist.l_name = BYTE_GET (elib[cnt].l_name);
19258 atime = BYTE_GET (elib[cnt].l_time_stamp);
19259 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
19260 liblist.l_version = BYTE_GET (elib[cnt].l_version);
19261 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
19262
19263 tmp = gmtime (&atime);
19264 snprintf (timebuf, sizeof (timebuf),
19265 "%04u-%02u-%02uT%02u:%02u:%02u",
19266 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
19267 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
19268
19269 printf ("%3lu: ", (unsigned long) cnt);
19270 if (do_wide)
19271 printf ("%-20s", liblist.l_name < strtab_size
19272 ? strtab + liblist.l_name : _("<corrupt>"));
19273 else
19274 printf ("%-20.20s", liblist.l_name < strtab_size
19275 ? strtab + liblist.l_name : _("<corrupt>"));
19276 printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
19277 liblist.l_version, liblist.l_flags);
19278 }
19279
19280 free (elib);
19281 free (strtab);
19282 }
19283 }
19284
19285 return res;
19286 }
19287
19288 static const char *
19289 get_note_type (Filedata * filedata, unsigned e_type)
19290 {
19291 static char buff[64];
19292
19293 if (filedata->file_header.e_type == ET_CORE)
19294 switch (e_type)
19295 {
19296 case NT_AUXV:
19297 return _("NT_AUXV (auxiliary vector)");
19298 case NT_PRSTATUS:
19299 return _("NT_PRSTATUS (prstatus structure)");
19300 case NT_FPREGSET:
19301 return _("NT_FPREGSET (floating point registers)");
19302 case NT_PRPSINFO:
19303 return _("NT_PRPSINFO (prpsinfo structure)");
19304 case NT_TASKSTRUCT:
19305 return _("NT_TASKSTRUCT (task structure)");
19306 case NT_GDB_TDESC:
19307 return _("NT_GDB_TDESC (GDB XML target description)");
19308 case NT_PRXFPREG:
19309 return _("NT_PRXFPREG (user_xfpregs structure)");
19310 case NT_PPC_VMX:
19311 return _("NT_PPC_VMX (ppc Altivec registers)");
19312 case NT_PPC_VSX:
19313 return _("NT_PPC_VSX (ppc VSX registers)");
19314 case NT_PPC_TAR:
19315 return _("NT_PPC_TAR (ppc TAR register)");
19316 case NT_PPC_PPR:
19317 return _("NT_PPC_PPR (ppc PPR register)");
19318 case NT_PPC_DSCR:
19319 return _("NT_PPC_DSCR (ppc DSCR register)");
19320 case NT_PPC_EBB:
19321 return _("NT_PPC_EBB (ppc EBB registers)");
19322 case NT_PPC_PMU:
19323 return _("NT_PPC_PMU (ppc PMU registers)");
19324 case NT_PPC_TM_CGPR:
19325 return _("NT_PPC_TM_CGPR (ppc checkpointed GPR registers)");
19326 case NT_PPC_TM_CFPR:
19327 return _("NT_PPC_TM_CFPR (ppc checkpointed floating point registers)");
19328 case NT_PPC_TM_CVMX:
19329 return _("NT_PPC_TM_CVMX (ppc checkpointed Altivec registers)");
19330 case NT_PPC_TM_CVSX:
19331 return _("NT_PPC_TM_CVSX (ppc checkpointed VSX registers)");
19332 case NT_PPC_TM_SPR:
19333 return _("NT_PPC_TM_SPR (ppc TM special purpose registers)");
19334 case NT_PPC_TM_CTAR:
19335 return _("NT_PPC_TM_CTAR (ppc checkpointed TAR register)");
19336 case NT_PPC_TM_CPPR:
19337 return _("NT_PPC_TM_CPPR (ppc checkpointed PPR register)");
19338 case NT_PPC_TM_CDSCR:
19339 return _("NT_PPC_TM_CDSCR (ppc checkpointed DSCR register)");
19340 case NT_386_TLS:
19341 return _("NT_386_TLS (x86 TLS information)");
19342 case NT_386_IOPERM:
19343 return _("NT_386_IOPERM (x86 I/O permissions)");
19344 case NT_X86_XSTATE:
19345 return _("NT_X86_XSTATE (x86 XSAVE extended state)");
19346 case NT_X86_CET:
19347 return _("NT_X86_CET (x86 CET state)");
19348 case NT_S390_HIGH_GPRS:
19349 return _("NT_S390_HIGH_GPRS (s390 upper register halves)");
19350 case NT_S390_TIMER:
19351 return _("NT_S390_TIMER (s390 timer register)");
19352 case NT_S390_TODCMP:
19353 return _("NT_S390_TODCMP (s390 TOD comparator register)");
19354 case NT_S390_TODPREG:
19355 return _("NT_S390_TODPREG (s390 TOD programmable register)");
19356 case NT_S390_CTRS:
19357 return _("NT_S390_CTRS (s390 control registers)");
19358 case NT_S390_PREFIX:
19359 return _("NT_S390_PREFIX (s390 prefix register)");
19360 case NT_S390_LAST_BREAK:
19361 return _("NT_S390_LAST_BREAK (s390 last breaking event address)");
19362 case NT_S390_SYSTEM_CALL:
19363 return _("NT_S390_SYSTEM_CALL (s390 system call restart data)");
19364 case NT_S390_TDB:
19365 return _("NT_S390_TDB (s390 transaction diagnostic block)");
19366 case NT_S390_VXRS_LOW:
19367 return _("NT_S390_VXRS_LOW (s390 vector registers 0-15 upper half)");
19368 case NT_S390_VXRS_HIGH:
19369 return _("NT_S390_VXRS_HIGH (s390 vector registers 16-31)");
19370 case NT_S390_GS_CB:
19371 return _("NT_S390_GS_CB (s390 guarded-storage registers)");
19372 case NT_S390_GS_BC:
19373 return _("NT_S390_GS_BC (s390 guarded-storage broadcast control)");
19374 case NT_ARM_VFP:
19375 return _("NT_ARM_VFP (arm VFP registers)");
19376 case NT_ARM_TLS:
19377 return _("NT_ARM_TLS (AArch TLS registers)");
19378 case NT_ARM_HW_BREAK:
19379 return _("NT_ARM_HW_BREAK (AArch hardware breakpoint registers)");
19380 case NT_ARM_HW_WATCH:
19381 return _("NT_ARM_HW_WATCH (AArch hardware watchpoint registers)");
19382 case NT_ARM_SYSTEM_CALL:
19383 return _("NT_ARM_SYSTEM_CALL (AArch system call number)");
19384 case NT_ARM_SVE:
19385 return _("NT_ARM_SVE (AArch SVE registers)");
19386 case NT_ARM_PAC_MASK:
19387 return _("NT_ARM_PAC_MASK (AArch pointer authentication code masks)");
19388 case NT_ARM_PACA_KEYS:
19389 return _("NT_ARM_PACA_KEYS (ARM pointer authentication address keys)");
19390 case NT_ARM_PACG_KEYS:
19391 return _("NT_ARM_PACG_KEYS (ARM pointer authentication generic keys)");
19392 case NT_ARM_TAGGED_ADDR_CTRL:
19393 return _("NT_ARM_TAGGED_ADDR_CTRL (AArch tagged address control)");
19394 case NT_ARM_PAC_ENABLED_KEYS:
19395 return _("NT_ARM_PAC_ENABLED_KEYS (AArch64 pointer authentication enabled keys)");
19396 case NT_ARC_V2:
19397 return _("NT_ARC_V2 (ARC HS accumulator/extra registers)");
19398 case NT_RISCV_CSR:
19399 return _("NT_RISCV_CSR (RISC-V control and status registers)");
19400 case NT_PSTATUS:
19401 return _("NT_PSTATUS (pstatus structure)");
19402 case NT_FPREGS:
19403 return _("NT_FPREGS (floating point registers)");
19404 case NT_PSINFO:
19405 return _("NT_PSINFO (psinfo structure)");
19406 case NT_LWPSTATUS:
19407 return _("NT_LWPSTATUS (lwpstatus_t structure)");
19408 case NT_LWPSINFO:
19409 return _("NT_LWPSINFO (lwpsinfo_t structure)");
19410 case NT_WIN32PSTATUS:
19411 return _("NT_WIN32PSTATUS (win32_pstatus structure)");
19412 case NT_SIGINFO:
19413 return _("NT_SIGINFO (siginfo_t data)");
19414 case NT_FILE:
19415 return _("NT_FILE (mapped files)");
19416 default:
19417 break;
19418 }
19419 else
19420 switch (e_type)
19421 {
19422 case NT_VERSION:
19423 return _("NT_VERSION (version)");
19424 case NT_ARCH:
19425 return _("NT_ARCH (architecture)");
19426 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
19427 return _("OPEN");
19428 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
19429 return _("func");
19430 case NT_GO_BUILDID:
19431 return _("GO BUILDID");
19432 case FDO_PACKAGING_METADATA:
19433 return _("FDO_PACKAGING_METADATA");
19434 default:
19435 break;
19436 }
19437
19438 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19439 return buff;
19440 }
19441
19442 static bool
19443 print_core_note (Elf_Internal_Note *pnote)
19444 {
19445 unsigned int addr_size = is_32bit_elf ? 4 : 8;
19446 bfd_vma count, page_size;
19447 unsigned char *descdata, *filenames, *descend;
19448
19449 if (pnote->type != NT_FILE)
19450 {
19451 if (do_wide)
19452 printf ("\n");
19453 return true;
19454 }
19455
19456 #ifndef BFD64
19457 if (!is_32bit_elf)
19458 {
19459 printf (_(" Cannot decode 64-bit note in 32-bit build\n"));
19460 /* Still "successful". */
19461 return true;
19462 }
19463 #endif
19464
19465 if (pnote->descsz < 2 * addr_size)
19466 {
19467 error (_(" Malformed note - too short for header\n"));
19468 return false;
19469 }
19470
19471 descdata = (unsigned char *) pnote->descdata;
19472 descend = descdata + pnote->descsz;
19473
19474 if (descdata[pnote->descsz - 1] != '\0')
19475 {
19476 error (_(" Malformed note - does not end with \\0\n"));
19477 return false;
19478 }
19479
19480 count = byte_get (descdata, addr_size);
19481 descdata += addr_size;
19482
19483 page_size = byte_get (descdata, addr_size);
19484 descdata += addr_size;
19485
19486 if (count > ((bfd_vma) -1 - 2 * addr_size) / (3 * addr_size)
19487 || pnote->descsz < 2 * addr_size + count * 3 * addr_size)
19488 {
19489 error (_(" Malformed note - too short for supplied file count\n"));
19490 return false;
19491 }
19492
19493 printf (_(" Page size: "));
19494 print_vma (page_size, DEC);
19495 printf ("\n");
19496
19497 printf (_(" %*s%*s%*s\n"),
19498 (int) (2 + 2 * addr_size), _("Start"),
19499 (int) (4 + 2 * addr_size), _("End"),
19500 (int) (4 + 2 * addr_size), _("Page Offset"));
19501 filenames = descdata + count * 3 * addr_size;
19502 while (count-- > 0)
19503 {
19504 bfd_vma start, end, file_ofs;
19505
19506 if (filenames == descend)
19507 {
19508 error (_(" Malformed note - filenames end too early\n"));
19509 return false;
19510 }
19511
19512 start = byte_get (descdata, addr_size);
19513 descdata += addr_size;
19514 end = byte_get (descdata, addr_size);
19515 descdata += addr_size;
19516 file_ofs = byte_get (descdata, addr_size);
19517 descdata += addr_size;
19518
19519 printf (" ");
19520 print_vma (start, FULL_HEX);
19521 printf (" ");
19522 print_vma (end, FULL_HEX);
19523 printf (" ");
19524 print_vma (file_ofs, FULL_HEX);
19525 printf ("\n %s\n", filenames);
19526
19527 filenames += 1 + strlen ((char *) filenames);
19528 }
19529
19530 return true;
19531 }
19532
19533 static const char *
19534 get_gnu_elf_note_type (unsigned e_type)
19535 {
19536 /* NB/ Keep this switch statement in sync with print_gnu_note (). */
19537 switch (e_type)
19538 {
19539 case NT_GNU_ABI_TAG:
19540 return _("NT_GNU_ABI_TAG (ABI version tag)");
19541 case NT_GNU_HWCAP:
19542 return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
19543 case NT_GNU_BUILD_ID:
19544 return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
19545 case NT_GNU_GOLD_VERSION:
19546 return _("NT_GNU_GOLD_VERSION (gold version)");
19547 case NT_GNU_PROPERTY_TYPE_0:
19548 return _("NT_GNU_PROPERTY_TYPE_0");
19549 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
19550 return _("NT_GNU_BUILD_ATTRIBUTE_OPEN");
19551 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
19552 return _("NT_GNU_BUILD_ATTRIBUTE_FUNC");
19553 default:
19554 {
19555 static char buff[64];
19556
19557 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19558 return buff;
19559 }
19560 }
19561 }
19562
19563 static void
19564 decode_x86_compat_isa (unsigned int bitmask)
19565 {
19566 while (bitmask)
19567 {
19568 unsigned int bit = bitmask & (- bitmask);
19569
19570 bitmask &= ~ bit;
19571 switch (bit)
19572 {
19573 case GNU_PROPERTY_X86_COMPAT_ISA_1_486:
19574 printf ("i486");
19575 break;
19576 case GNU_PROPERTY_X86_COMPAT_ISA_1_586:
19577 printf ("586");
19578 break;
19579 case GNU_PROPERTY_X86_COMPAT_ISA_1_686:
19580 printf ("686");
19581 break;
19582 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE:
19583 printf ("SSE");
19584 break;
19585 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE2:
19586 printf ("SSE2");
19587 break;
19588 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE3:
19589 printf ("SSE3");
19590 break;
19591 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSSE3:
19592 printf ("SSSE3");
19593 break;
19594 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_1:
19595 printf ("SSE4_1");
19596 break;
19597 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_2:
19598 printf ("SSE4_2");
19599 break;
19600 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX:
19601 printf ("AVX");
19602 break;
19603 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX2:
19604 printf ("AVX2");
19605 break;
19606 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512F:
19607 printf ("AVX512F");
19608 break;
19609 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512CD:
19610 printf ("AVX512CD");
19611 break;
19612 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512ER:
19613 printf ("AVX512ER");
19614 break;
19615 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512PF:
19616 printf ("AVX512PF");
19617 break;
19618 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512VL:
19619 printf ("AVX512VL");
19620 break;
19621 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512DQ:
19622 printf ("AVX512DQ");
19623 break;
19624 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512BW:
19625 printf ("AVX512BW");
19626 break;
19627 default:
19628 printf (_("<unknown: %x>"), bit);
19629 break;
19630 }
19631 if (bitmask)
19632 printf (", ");
19633 }
19634 }
19635
19636 static void
19637 decode_x86_compat_2_isa (unsigned int bitmask)
19638 {
19639 if (!bitmask)
19640 {
19641 printf (_("<None>"));
19642 return;
19643 }
19644
19645 while (bitmask)
19646 {
19647 unsigned int bit = bitmask & (- bitmask);
19648
19649 bitmask &= ~ bit;
19650 switch (bit)
19651 {
19652 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_CMOV:
19653 printf ("CMOV");
19654 break;
19655 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE:
19656 printf ("SSE");
19657 break;
19658 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE2:
19659 printf ("SSE2");
19660 break;
19661 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE3:
19662 printf ("SSE3");
19663 break;
19664 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSSE3:
19665 printf ("SSSE3");
19666 break;
19667 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_1:
19668 printf ("SSE4_1");
19669 break;
19670 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_2:
19671 printf ("SSE4_2");
19672 break;
19673 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX:
19674 printf ("AVX");
19675 break;
19676 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX2:
19677 printf ("AVX2");
19678 break;
19679 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_FMA:
19680 printf ("FMA");
19681 break;
19682 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512F:
19683 printf ("AVX512F");
19684 break;
19685 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512CD:
19686 printf ("AVX512CD");
19687 break;
19688 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512ER:
19689 printf ("AVX512ER");
19690 break;
19691 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512PF:
19692 printf ("AVX512PF");
19693 break;
19694 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512VL:
19695 printf ("AVX512VL");
19696 break;
19697 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512DQ:
19698 printf ("AVX512DQ");
19699 break;
19700 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512BW:
19701 printf ("AVX512BW");
19702 break;
19703 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4FMAPS:
19704 printf ("AVX512_4FMAPS");
19705 break;
19706 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4VNNIW:
19707 printf ("AVX512_4VNNIW");
19708 break;
19709 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BITALG:
19710 printf ("AVX512_BITALG");
19711 break;
19712 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_IFMA:
19713 printf ("AVX512_IFMA");
19714 break;
19715 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI:
19716 printf ("AVX512_VBMI");
19717 break;
19718 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI2:
19719 printf ("AVX512_VBMI2");
19720 break;
19721 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VNNI:
19722 printf ("AVX512_VNNI");
19723 break;
19724 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BF16:
19725 printf ("AVX512_BF16");
19726 break;
19727 default:
19728 printf (_("<unknown: %x>"), bit);
19729 break;
19730 }
19731 if (bitmask)
19732 printf (", ");
19733 }
19734 }
19735
19736 static const char *
19737 get_amdgpu_elf_note_type (unsigned int e_type)
19738 {
19739 switch (e_type)
19740 {
19741 case NT_AMDGPU_METADATA:
19742 return _("NT_AMDGPU_METADATA (code object metadata)");
19743 default:
19744 {
19745 static char buf[64];
19746 snprintf (buf, sizeof (buf), _("Unknown note type: (0x%08x)"), e_type);
19747 return buf;
19748 }
19749 }
19750 }
19751
19752 static void
19753 decode_x86_isa (unsigned int bitmask)
19754 {
19755 while (bitmask)
19756 {
19757 unsigned int bit = bitmask & (- bitmask);
19758
19759 bitmask &= ~ bit;
19760 switch (bit)
19761 {
19762 case GNU_PROPERTY_X86_ISA_1_BASELINE:
19763 printf ("x86-64-baseline");
19764 break;
19765 case GNU_PROPERTY_X86_ISA_1_V2:
19766 printf ("x86-64-v2");
19767 break;
19768 case GNU_PROPERTY_X86_ISA_1_V3:
19769 printf ("x86-64-v3");
19770 break;
19771 case GNU_PROPERTY_X86_ISA_1_V4:
19772 printf ("x86-64-v4");
19773 break;
19774 default:
19775 printf (_("<unknown: %x>"), bit);
19776 break;
19777 }
19778 if (bitmask)
19779 printf (", ");
19780 }
19781 }
19782
19783 static void
19784 decode_x86_feature_1 (unsigned int bitmask)
19785 {
19786 if (!bitmask)
19787 {
19788 printf (_("<None>"));
19789 return;
19790 }
19791
19792 while (bitmask)
19793 {
19794 unsigned int bit = bitmask & (- bitmask);
19795
19796 bitmask &= ~ bit;
19797 switch (bit)
19798 {
19799 case GNU_PROPERTY_X86_FEATURE_1_IBT:
19800 printf ("IBT");
19801 break;
19802 case GNU_PROPERTY_X86_FEATURE_1_SHSTK:
19803 printf ("SHSTK");
19804 break;
19805 case GNU_PROPERTY_X86_FEATURE_1_LAM_U48:
19806 printf ("LAM_U48");
19807 break;
19808 case GNU_PROPERTY_X86_FEATURE_1_LAM_U57:
19809 printf ("LAM_U57");
19810 break;
19811 default:
19812 printf (_("<unknown: %x>"), bit);
19813 break;
19814 }
19815 if (bitmask)
19816 printf (", ");
19817 }
19818 }
19819
19820 static void
19821 decode_x86_feature_2 (unsigned int bitmask)
19822 {
19823 if (!bitmask)
19824 {
19825 printf (_("<None>"));
19826 return;
19827 }
19828
19829 while (bitmask)
19830 {
19831 unsigned int bit = bitmask & (- bitmask);
19832
19833 bitmask &= ~ bit;
19834 switch (bit)
19835 {
19836 case GNU_PROPERTY_X86_FEATURE_2_X86:
19837 printf ("x86");
19838 break;
19839 case GNU_PROPERTY_X86_FEATURE_2_X87:
19840 printf ("x87");
19841 break;
19842 case GNU_PROPERTY_X86_FEATURE_2_MMX:
19843 printf ("MMX");
19844 break;
19845 case GNU_PROPERTY_X86_FEATURE_2_XMM:
19846 printf ("XMM");
19847 break;
19848 case GNU_PROPERTY_X86_FEATURE_2_YMM:
19849 printf ("YMM");
19850 break;
19851 case GNU_PROPERTY_X86_FEATURE_2_ZMM:
19852 printf ("ZMM");
19853 break;
19854 case GNU_PROPERTY_X86_FEATURE_2_TMM:
19855 printf ("TMM");
19856 break;
19857 case GNU_PROPERTY_X86_FEATURE_2_MASK:
19858 printf ("MASK");
19859 break;
19860 case GNU_PROPERTY_X86_FEATURE_2_FXSR:
19861 printf ("FXSR");
19862 break;
19863 case GNU_PROPERTY_X86_FEATURE_2_XSAVE:
19864 printf ("XSAVE");
19865 break;
19866 case GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT:
19867 printf ("XSAVEOPT");
19868 break;
19869 case GNU_PROPERTY_X86_FEATURE_2_XSAVEC:
19870 printf ("XSAVEC");
19871 break;
19872 default:
19873 printf (_("<unknown: %x>"), bit);
19874 break;
19875 }
19876 if (bitmask)
19877 printf (", ");
19878 }
19879 }
19880
19881 static void
19882 decode_aarch64_feature_1_and (unsigned int bitmask)
19883 {
19884 while (bitmask)
19885 {
19886 unsigned int bit = bitmask & (- bitmask);
19887
19888 bitmask &= ~ bit;
19889 switch (bit)
19890 {
19891 case GNU_PROPERTY_AARCH64_FEATURE_1_BTI:
19892 printf ("BTI");
19893 break;
19894
19895 case GNU_PROPERTY_AARCH64_FEATURE_1_PAC:
19896 printf ("PAC");
19897 break;
19898
19899 default:
19900 printf (_("<unknown: %x>"), bit);
19901 break;
19902 }
19903 if (bitmask)
19904 printf (", ");
19905 }
19906 }
19907
19908 static void
19909 decode_1_needed (unsigned int bitmask)
19910 {
19911 while (bitmask)
19912 {
19913 unsigned int bit = bitmask & (- bitmask);
19914
19915 bitmask &= ~ bit;
19916 switch (bit)
19917 {
19918 case GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS:
19919 printf ("indirect external access");
19920 break;
19921 default:
19922 printf (_("<unknown: %x>"), bit);
19923 break;
19924 }
19925 if (bitmask)
19926 printf (", ");
19927 }
19928 }
19929
19930 static void
19931 print_gnu_property_note (Filedata * filedata, Elf_Internal_Note * pnote)
19932 {
19933 unsigned char * ptr = (unsigned char *) pnote->descdata;
19934 unsigned char * ptr_end = ptr + pnote->descsz;
19935 unsigned int size = is_32bit_elf ? 4 : 8;
19936
19937 printf (_(" Properties: "));
19938
19939 if (pnote->descsz < 8 || (pnote->descsz % size) != 0)
19940 {
19941 printf (_("<corrupt GNU_PROPERTY_TYPE, size = %#lx>\n"), pnote->descsz);
19942 return;
19943 }
19944
19945 while (ptr < ptr_end)
19946 {
19947 unsigned int j;
19948 unsigned int type;
19949 unsigned int datasz;
19950
19951 if ((size_t) (ptr_end - ptr) < 8)
19952 {
19953 printf (_("<corrupt descsz: %#lx>\n"), pnote->descsz);
19954 break;
19955 }
19956
19957 type = byte_get (ptr, 4);
19958 datasz = byte_get (ptr + 4, 4);
19959
19960 ptr += 8;
19961
19962 if (datasz > (size_t) (ptr_end - ptr))
19963 {
19964 printf (_("<corrupt type (%#x) datasz: %#x>\n"),
19965 type, datasz);
19966 break;
19967 }
19968
19969 if (type >= GNU_PROPERTY_LOPROC && type <= GNU_PROPERTY_HIPROC)
19970 {
19971 if (filedata->file_header.e_machine == EM_X86_64
19972 || filedata->file_header.e_machine == EM_IAMCU
19973 || filedata->file_header.e_machine == EM_386)
19974 {
19975 unsigned int bitmask;
19976
19977 if (datasz == 4)
19978 bitmask = byte_get (ptr, 4);
19979 else
19980 bitmask = 0;
19981
19982 switch (type)
19983 {
19984 case GNU_PROPERTY_X86_ISA_1_USED:
19985 if (datasz != 4)
19986 printf (_("x86 ISA used: <corrupt length: %#x> "),
19987 datasz);
19988 else
19989 {
19990 printf ("x86 ISA used: ");
19991 decode_x86_isa (bitmask);
19992 }
19993 goto next;
19994
19995 case GNU_PROPERTY_X86_ISA_1_NEEDED:
19996 if (datasz != 4)
19997 printf (_("x86 ISA needed: <corrupt length: %#x> "),
19998 datasz);
19999 else
20000 {
20001 printf ("x86 ISA needed: ");
20002 decode_x86_isa (bitmask);
20003 }
20004 goto next;
20005
20006 case GNU_PROPERTY_X86_FEATURE_1_AND:
20007 if (datasz != 4)
20008 printf (_("x86 feature: <corrupt length: %#x> "),
20009 datasz);
20010 else
20011 {
20012 printf ("x86 feature: ");
20013 decode_x86_feature_1 (bitmask);
20014 }
20015 goto next;
20016
20017 case GNU_PROPERTY_X86_FEATURE_2_USED:
20018 if (datasz != 4)
20019 printf (_("x86 feature used: <corrupt length: %#x> "),
20020 datasz);
20021 else
20022 {
20023 printf ("x86 feature used: ");
20024 decode_x86_feature_2 (bitmask);
20025 }
20026 goto next;
20027
20028 case GNU_PROPERTY_X86_FEATURE_2_NEEDED:
20029 if (datasz != 4)
20030 printf (_("x86 feature needed: <corrupt length: %#x> "), datasz);
20031 else
20032 {
20033 printf ("x86 feature needed: ");
20034 decode_x86_feature_2 (bitmask);
20035 }
20036 goto next;
20037
20038 case GNU_PROPERTY_X86_COMPAT_ISA_1_USED:
20039 if (datasz != 4)
20040 printf (_("x86 ISA used: <corrupt length: %#x> "),
20041 datasz);
20042 else
20043 {
20044 printf ("x86 ISA used: ");
20045 decode_x86_compat_isa (bitmask);
20046 }
20047 goto next;
20048
20049 case GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED:
20050 if (datasz != 4)
20051 printf (_("x86 ISA needed: <corrupt length: %#x> "),
20052 datasz);
20053 else
20054 {
20055 printf ("x86 ISA needed: ");
20056 decode_x86_compat_isa (bitmask);
20057 }
20058 goto next;
20059
20060 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_USED:
20061 if (datasz != 4)
20062 printf (_("x86 ISA used: <corrupt length: %#x> "),
20063 datasz);
20064 else
20065 {
20066 printf ("x86 ISA used: ");
20067 decode_x86_compat_2_isa (bitmask);
20068 }
20069 goto next;
20070
20071 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_NEEDED:
20072 if (datasz != 4)
20073 printf (_("x86 ISA needed: <corrupt length: %#x> "),
20074 datasz);
20075 else
20076 {
20077 printf ("x86 ISA needed: ");
20078 decode_x86_compat_2_isa (bitmask);
20079 }
20080 goto next;
20081
20082 default:
20083 break;
20084 }
20085 }
20086 else if (filedata->file_header.e_machine == EM_AARCH64)
20087 {
20088 if (type == GNU_PROPERTY_AARCH64_FEATURE_1_AND)
20089 {
20090 printf ("AArch64 feature: ");
20091 if (datasz != 4)
20092 printf (_("<corrupt length: %#x> "), datasz);
20093 else
20094 decode_aarch64_feature_1_and (byte_get (ptr, 4));
20095 goto next;
20096 }
20097 }
20098 }
20099 else
20100 {
20101 switch (type)
20102 {
20103 case GNU_PROPERTY_STACK_SIZE:
20104 printf (_("stack size: "));
20105 if (datasz != size)
20106 printf (_("<corrupt length: %#x> "), datasz);
20107 else
20108 printf ("%#lx", (unsigned long) byte_get (ptr, size));
20109 goto next;
20110
20111 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
20112 printf ("no copy on protected ");
20113 if (datasz)
20114 printf (_("<corrupt length: %#x> "), datasz);
20115 goto next;
20116
20117 default:
20118 if ((type >= GNU_PROPERTY_UINT32_AND_LO
20119 && type <= GNU_PROPERTY_UINT32_AND_HI)
20120 || (type >= GNU_PROPERTY_UINT32_OR_LO
20121 && type <= GNU_PROPERTY_UINT32_OR_HI))
20122 {
20123 switch (type)
20124 {
20125 case GNU_PROPERTY_1_NEEDED:
20126 if (datasz != 4)
20127 printf (_("1_needed: <corrupt length: %#x> "),
20128 datasz);
20129 else
20130 {
20131 unsigned int bitmask = byte_get (ptr, 4);
20132 printf ("1_needed: ");
20133 decode_1_needed (bitmask);
20134 }
20135 goto next;
20136
20137 default:
20138 break;
20139 }
20140 if (type <= GNU_PROPERTY_UINT32_AND_HI)
20141 printf (_("UINT32_AND (%#x): "), type);
20142 else
20143 printf (_("UINT32_OR (%#x): "), type);
20144 if (datasz != 4)
20145 printf (_("<corrupt length: %#x> "), datasz);
20146 else
20147 printf ("%#x", (unsigned int) byte_get (ptr, 4));
20148 goto next;
20149 }
20150 break;
20151 }
20152 }
20153
20154 if (type < GNU_PROPERTY_LOPROC)
20155 printf (_("<unknown type %#x data: "), type);
20156 else if (type < GNU_PROPERTY_LOUSER)
20157 printf (_("<processor-specific type %#x data: "), type);
20158 else
20159 printf (_("<application-specific type %#x data: "), type);
20160 for (j = 0; j < datasz; ++j)
20161 printf ("%02x ", ptr[j] & 0xff);
20162 printf (">");
20163
20164 next:
20165 ptr += ((datasz + (size - 1)) & ~ (size - 1));
20166 if (ptr == ptr_end)
20167 break;
20168
20169 if (do_wide)
20170 printf (", ");
20171 else
20172 printf ("\n\t");
20173 }
20174
20175 printf ("\n");
20176 }
20177
20178 static bool
20179 print_gnu_note (Filedata * filedata, Elf_Internal_Note *pnote)
20180 {
20181 /* NB/ Keep this switch statement in sync with get_gnu_elf_note_type (). */
20182 switch (pnote->type)
20183 {
20184 case NT_GNU_BUILD_ID:
20185 {
20186 unsigned long i;
20187
20188 printf (_(" Build ID: "));
20189 for (i = 0; i < pnote->descsz; ++i)
20190 printf ("%02x", pnote->descdata[i] & 0xff);
20191 printf ("\n");
20192 }
20193 break;
20194
20195 case NT_GNU_ABI_TAG:
20196 {
20197 unsigned long os, major, minor, subminor;
20198 const char *osname;
20199
20200 /* PR 17531: file: 030-599401-0.004. */
20201 if (pnote->descsz < 16)
20202 {
20203 printf (_(" <corrupt GNU_ABI_TAG>\n"));
20204 break;
20205 }
20206
20207 os = byte_get ((unsigned char *) pnote->descdata, 4);
20208 major = byte_get ((unsigned char *) pnote->descdata + 4, 4);
20209 minor = byte_get ((unsigned char *) pnote->descdata + 8, 4);
20210 subminor = byte_get ((unsigned char *) pnote->descdata + 12, 4);
20211
20212 switch (os)
20213 {
20214 case GNU_ABI_TAG_LINUX:
20215 osname = "Linux";
20216 break;
20217 case GNU_ABI_TAG_HURD:
20218 osname = "Hurd";
20219 break;
20220 case GNU_ABI_TAG_SOLARIS:
20221 osname = "Solaris";
20222 break;
20223 case GNU_ABI_TAG_FREEBSD:
20224 osname = "FreeBSD";
20225 break;
20226 case GNU_ABI_TAG_NETBSD:
20227 osname = "NetBSD";
20228 break;
20229 case GNU_ABI_TAG_SYLLABLE:
20230 osname = "Syllable";
20231 break;
20232 case GNU_ABI_TAG_NACL:
20233 osname = "NaCl";
20234 break;
20235 default:
20236 osname = "Unknown";
20237 break;
20238 }
20239
20240 printf (_(" OS: %s, ABI: %ld.%ld.%ld\n"), osname,
20241 major, minor, subminor);
20242 }
20243 break;
20244
20245 case NT_GNU_GOLD_VERSION:
20246 {
20247 unsigned long i;
20248
20249 printf (_(" Version: "));
20250 for (i = 0; i < pnote->descsz && pnote->descdata[i] != '\0'; ++i)
20251 printf ("%c", pnote->descdata[i]);
20252 printf ("\n");
20253 }
20254 break;
20255
20256 case NT_GNU_HWCAP:
20257 {
20258 unsigned long num_entries, mask;
20259
20260 /* Hardware capabilities information. Word 0 is the number of entries.
20261 Word 1 is a bitmask of enabled entries. The rest of the descriptor
20262 is a series of entries, where each entry is a single byte followed
20263 by a nul terminated string. The byte gives the bit number to test
20264 if enabled in the bitmask. */
20265 printf (_(" Hardware Capabilities: "));
20266 if (pnote->descsz < 8)
20267 {
20268 error (_("<corrupt GNU_HWCAP>\n"));
20269 return false;
20270 }
20271 num_entries = byte_get ((unsigned char *) pnote->descdata, 4);
20272 mask = byte_get ((unsigned char *) pnote->descdata + 4, 4);
20273 printf (_("num entries: %ld, enabled mask: %lx\n"), num_entries, mask);
20274 /* FIXME: Add code to display the entries... */
20275 }
20276 break;
20277
20278 case NT_GNU_PROPERTY_TYPE_0:
20279 print_gnu_property_note (filedata, pnote);
20280 break;
20281
20282 default:
20283 /* Handle unrecognised types. An error message should have already been
20284 created by get_gnu_elf_note_type(), so all that we need to do is to
20285 display the data. */
20286 {
20287 unsigned long i;
20288
20289 printf (_(" Description data: "));
20290 for (i = 0; i < pnote->descsz; ++i)
20291 printf ("%02x ", pnote->descdata[i] & 0xff);
20292 printf ("\n");
20293 }
20294 break;
20295 }
20296
20297 return true;
20298 }
20299
20300 static const char *
20301 get_v850_elf_note_type (enum v850_notes n_type)
20302 {
20303 static char buff[64];
20304
20305 switch (n_type)
20306 {
20307 case V850_NOTE_ALIGNMENT: return _("Alignment of 8-byte objects");
20308 case V850_NOTE_DATA_SIZE: return _("Sizeof double and long double");
20309 case V850_NOTE_FPU_INFO: return _("Type of FPU support needed");
20310 case V850_NOTE_SIMD_INFO: return _("Use of SIMD instructions");
20311 case V850_NOTE_CACHE_INFO: return _("Use of cache");
20312 case V850_NOTE_MMU_INFO: return _("Use of MMU");
20313 default:
20314 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), n_type);
20315 return buff;
20316 }
20317 }
20318
20319 static bool
20320 print_v850_note (Elf_Internal_Note * pnote)
20321 {
20322 unsigned int val;
20323
20324 if (pnote->descsz != 4)
20325 return false;
20326
20327 val = byte_get ((unsigned char *) pnote->descdata, pnote->descsz);
20328
20329 if (val == 0)
20330 {
20331 printf (_("not set\n"));
20332 return true;
20333 }
20334
20335 switch (pnote->type)
20336 {
20337 case V850_NOTE_ALIGNMENT:
20338 switch (val)
20339 {
20340 case EF_RH850_DATA_ALIGN4: printf (_("4-byte\n")); return true;
20341 case EF_RH850_DATA_ALIGN8: printf (_("8-byte\n")); return true;
20342 }
20343 break;
20344
20345 case V850_NOTE_DATA_SIZE:
20346 switch (val)
20347 {
20348 case EF_RH850_DOUBLE32: printf (_("4-bytes\n")); return true;
20349 case EF_RH850_DOUBLE64: printf (_("8-bytes\n")); return true;
20350 }
20351 break;
20352
20353 case V850_NOTE_FPU_INFO:
20354 switch (val)
20355 {
20356 case EF_RH850_FPU20: printf (_("FPU-2.0\n")); return true;
20357 case EF_RH850_FPU30: printf (_("FPU-3.0\n")); return true;
20358 }
20359 break;
20360
20361 case V850_NOTE_MMU_INFO:
20362 case V850_NOTE_CACHE_INFO:
20363 case V850_NOTE_SIMD_INFO:
20364 if (val == EF_RH850_SIMD)
20365 {
20366 printf (_("yes\n"));
20367 return true;
20368 }
20369 break;
20370
20371 default:
20372 /* An 'unknown note type' message will already have been displayed. */
20373 break;
20374 }
20375
20376 printf (_("unknown value: %x\n"), val);
20377 return false;
20378 }
20379
20380 static bool
20381 process_netbsd_elf_note (Elf_Internal_Note * pnote)
20382 {
20383 unsigned int version;
20384
20385 switch (pnote->type)
20386 {
20387 case NT_NETBSD_IDENT:
20388 if (pnote->descsz < 1)
20389 break;
20390 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
20391 if ((version / 10000) % 100)
20392 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u%s%c)\n", pnote->descsz,
20393 version, version / 100000000, (version / 1000000) % 100,
20394 (version / 10000) % 100 > 26 ? "Z" : "",
20395 'A' + (version / 10000) % 26);
20396 else
20397 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u.%u)\n", pnote->descsz,
20398 version, version / 100000000, (version / 1000000) % 100,
20399 (version / 100) % 100);
20400 return true;
20401
20402 case NT_NETBSD_MARCH:
20403 printf (" NetBSD\t\t0x%08lx\tMARCH <%s>\n", pnote->descsz,
20404 pnote->descdata);
20405 return true;
20406
20407 case NT_NETBSD_PAX:
20408 if (pnote->descsz < 1)
20409 break;
20410 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
20411 printf (" NetBSD\t\t0x%08lx\tPaX <%s%s%s%s%s%s>\n", pnote->descsz,
20412 ((version & NT_NETBSD_PAX_MPROTECT) ? "+mprotect" : ""),
20413 ((version & NT_NETBSD_PAX_NOMPROTECT) ? "-mprotect" : ""),
20414 ((version & NT_NETBSD_PAX_GUARD) ? "+guard" : ""),
20415 ((version & NT_NETBSD_PAX_NOGUARD) ? "-guard" : ""),
20416 ((version & NT_NETBSD_PAX_ASLR) ? "+ASLR" : ""),
20417 ((version & NT_NETBSD_PAX_NOASLR) ? "-ASLR" : ""));
20418 return true;
20419 }
20420
20421 printf (" NetBSD\t0x%08lx\tUnknown note type: (0x%08lx)\n",
20422 pnote->descsz, pnote->type);
20423 return false;
20424 }
20425
20426 static const char *
20427 get_freebsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
20428 {
20429 switch (e_type)
20430 {
20431 case NT_FREEBSD_THRMISC:
20432 return _("NT_THRMISC (thrmisc structure)");
20433 case NT_FREEBSD_PROCSTAT_PROC:
20434 return _("NT_PROCSTAT_PROC (proc data)");
20435 case NT_FREEBSD_PROCSTAT_FILES:
20436 return _("NT_PROCSTAT_FILES (files data)");
20437 case NT_FREEBSD_PROCSTAT_VMMAP:
20438 return _("NT_PROCSTAT_VMMAP (vmmap data)");
20439 case NT_FREEBSD_PROCSTAT_GROUPS:
20440 return _("NT_PROCSTAT_GROUPS (groups data)");
20441 case NT_FREEBSD_PROCSTAT_UMASK:
20442 return _("NT_PROCSTAT_UMASK (umask data)");
20443 case NT_FREEBSD_PROCSTAT_RLIMIT:
20444 return _("NT_PROCSTAT_RLIMIT (rlimit data)");
20445 case NT_FREEBSD_PROCSTAT_OSREL:
20446 return _("NT_PROCSTAT_OSREL (osreldate data)");
20447 case NT_FREEBSD_PROCSTAT_PSSTRINGS:
20448 return _("NT_PROCSTAT_PSSTRINGS (ps_strings data)");
20449 case NT_FREEBSD_PROCSTAT_AUXV:
20450 return _("NT_PROCSTAT_AUXV (auxv data)");
20451 case NT_FREEBSD_PTLWPINFO:
20452 return _("NT_PTLWPINFO (ptrace_lwpinfo structure)");
20453 case NT_FREEBSD_X86_SEGBASES:
20454 return _("NT_X86_SEGBASES (x86 segment base registers)");
20455 }
20456 return get_note_type (filedata, e_type);
20457 }
20458
20459 static const char *
20460 get_netbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
20461 {
20462 static char buff[64];
20463
20464 switch (e_type)
20465 {
20466 case NT_NETBSDCORE_PROCINFO:
20467 /* NetBSD core "procinfo" structure. */
20468 return _("NetBSD procinfo structure");
20469
20470 case NT_NETBSDCORE_AUXV:
20471 return _("NetBSD ELF auxiliary vector data");
20472
20473 case NT_NETBSDCORE_LWPSTATUS:
20474 return _("PT_LWPSTATUS (ptrace_lwpstatus structure)");
20475
20476 default:
20477 /* As of Jan 2020 there are no other machine-independent notes
20478 defined for NetBSD core files. If the note type is less
20479 than the start of the machine-dependent note types, we don't
20480 understand it. */
20481
20482 if (e_type < NT_NETBSDCORE_FIRSTMACH)
20483 {
20484 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20485 return buff;
20486 }
20487 break;
20488 }
20489
20490 switch (filedata->file_header.e_machine)
20491 {
20492 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
20493 and PT_GETFPREGS == mach+2. */
20494
20495 case EM_OLD_ALPHA:
20496 case EM_ALPHA:
20497 case EM_SPARC:
20498 case EM_SPARC32PLUS:
20499 case EM_SPARCV9:
20500 switch (e_type)
20501 {
20502 case NT_NETBSDCORE_FIRSTMACH + 0:
20503 return _("PT_GETREGS (reg structure)");
20504 case NT_NETBSDCORE_FIRSTMACH + 2:
20505 return _("PT_GETFPREGS (fpreg structure)");
20506 default:
20507 break;
20508 }
20509 break;
20510
20511 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
20512 There's also old PT___GETREGS40 == mach + 1 for old reg
20513 structure which lacks GBR. */
20514 case EM_SH:
20515 switch (e_type)
20516 {
20517 case NT_NETBSDCORE_FIRSTMACH + 1:
20518 return _("PT___GETREGS40 (old reg structure)");
20519 case NT_NETBSDCORE_FIRSTMACH + 3:
20520 return _("PT_GETREGS (reg structure)");
20521 case NT_NETBSDCORE_FIRSTMACH + 5:
20522 return _("PT_GETFPREGS (fpreg structure)");
20523 default:
20524 break;
20525 }
20526 break;
20527
20528 /* On all other arch's, PT_GETREGS == mach+1 and
20529 PT_GETFPREGS == mach+3. */
20530 default:
20531 switch (e_type)
20532 {
20533 case NT_NETBSDCORE_FIRSTMACH + 1:
20534 return _("PT_GETREGS (reg structure)");
20535 case NT_NETBSDCORE_FIRSTMACH + 3:
20536 return _("PT_GETFPREGS (fpreg structure)");
20537 default:
20538 break;
20539 }
20540 }
20541
20542 snprintf (buff, sizeof (buff), "PT_FIRSTMACH+%d",
20543 e_type - NT_NETBSDCORE_FIRSTMACH);
20544 return buff;
20545 }
20546
20547 static const char *
20548 get_openbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
20549 {
20550 switch (e_type)
20551 {
20552 case NT_OPENBSD_PROCINFO:
20553 return _("OpenBSD procinfo structure");
20554 case NT_OPENBSD_AUXV:
20555 return _("OpenBSD ELF auxiliary vector data");
20556 case NT_OPENBSD_REGS:
20557 return _("OpenBSD regular registers");
20558 case NT_OPENBSD_FPREGS:
20559 return _("OpenBSD floating point registers");
20560 case NT_OPENBSD_WCOOKIE:
20561 return _("OpenBSD window cookie");
20562 }
20563
20564 return get_note_type (filedata, e_type);
20565 }
20566
20567 static const char *
20568 get_stapsdt_note_type (unsigned e_type)
20569 {
20570 static char buff[64];
20571
20572 switch (e_type)
20573 {
20574 case NT_STAPSDT:
20575 return _("NT_STAPSDT (SystemTap probe descriptors)");
20576
20577 default:
20578 break;
20579 }
20580
20581 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20582 return buff;
20583 }
20584
20585 static bool
20586 print_stapsdt_note (Elf_Internal_Note *pnote)
20587 {
20588 size_t len, maxlen;
20589 unsigned long addr_size = is_32bit_elf ? 4 : 8;
20590 char *data = pnote->descdata;
20591 char *data_end = pnote->descdata + pnote->descsz;
20592 bfd_vma pc, base_addr, semaphore;
20593 char *provider, *probe, *arg_fmt;
20594
20595 if (pnote->descsz < (addr_size * 3))
20596 goto stapdt_note_too_small;
20597
20598 pc = byte_get ((unsigned char *) data, addr_size);
20599 data += addr_size;
20600
20601 base_addr = byte_get ((unsigned char *) data, addr_size);
20602 data += addr_size;
20603
20604 semaphore = byte_get ((unsigned char *) data, addr_size);
20605 data += addr_size;
20606
20607 if (data >= data_end)
20608 goto stapdt_note_too_small;
20609 maxlen = data_end - data;
20610 len = strnlen (data, maxlen);
20611 if (len < maxlen)
20612 {
20613 provider = data;
20614 data += len + 1;
20615 }
20616 else
20617 goto stapdt_note_too_small;
20618
20619 if (data >= data_end)
20620 goto stapdt_note_too_small;
20621 maxlen = data_end - data;
20622 len = strnlen (data, maxlen);
20623 if (len < maxlen)
20624 {
20625 probe = data;
20626 data += len + 1;
20627 }
20628 else
20629 goto stapdt_note_too_small;
20630
20631 if (data >= data_end)
20632 goto stapdt_note_too_small;
20633 maxlen = data_end - data;
20634 len = strnlen (data, maxlen);
20635 if (len < maxlen)
20636 {
20637 arg_fmt = data;
20638 data += len + 1;
20639 }
20640 else
20641 goto stapdt_note_too_small;
20642
20643 printf (_(" Provider: %s\n"), provider);
20644 printf (_(" Name: %s\n"), probe);
20645 printf (_(" Location: "));
20646 print_vma (pc, FULL_HEX);
20647 printf (_(", Base: "));
20648 print_vma (base_addr, FULL_HEX);
20649 printf (_(", Semaphore: "));
20650 print_vma (semaphore, FULL_HEX);
20651 printf ("\n");
20652 printf (_(" Arguments: %s\n"), arg_fmt);
20653
20654 return data == data_end;
20655
20656 stapdt_note_too_small:
20657 printf (_(" <corrupt - note is too small>\n"));
20658 error (_("corrupt stapdt note - the data size is too small\n"));
20659 return false;
20660 }
20661
20662 static bool
20663 print_fdo_note (Elf_Internal_Note * pnote)
20664 {
20665 if (pnote->descsz > 0 && pnote->type == FDO_PACKAGING_METADATA)
20666 {
20667 printf (_(" Packaging Metadata: %.*s\n"), (int) pnote->descsz, pnote->descdata);
20668 return true;
20669 }
20670 return false;
20671 }
20672
20673 static const char *
20674 get_ia64_vms_note_type (unsigned e_type)
20675 {
20676 static char buff[64];
20677
20678 switch (e_type)
20679 {
20680 case NT_VMS_MHD:
20681 return _("NT_VMS_MHD (module header)");
20682 case NT_VMS_LNM:
20683 return _("NT_VMS_LNM (language name)");
20684 case NT_VMS_SRC:
20685 return _("NT_VMS_SRC (source files)");
20686 case NT_VMS_TITLE:
20687 return "NT_VMS_TITLE";
20688 case NT_VMS_EIDC:
20689 return _("NT_VMS_EIDC (consistency check)");
20690 case NT_VMS_FPMODE:
20691 return _("NT_VMS_FPMODE (FP mode)");
20692 case NT_VMS_LINKTIME:
20693 return "NT_VMS_LINKTIME";
20694 case NT_VMS_IMGNAM:
20695 return _("NT_VMS_IMGNAM (image name)");
20696 case NT_VMS_IMGID:
20697 return _("NT_VMS_IMGID (image id)");
20698 case NT_VMS_LINKID:
20699 return _("NT_VMS_LINKID (link id)");
20700 case NT_VMS_IMGBID:
20701 return _("NT_VMS_IMGBID (build id)");
20702 case NT_VMS_GSTNAM:
20703 return _("NT_VMS_GSTNAM (sym table name)");
20704 case NT_VMS_ORIG_DYN:
20705 return "NT_VMS_ORIG_DYN";
20706 case NT_VMS_PATCHTIME:
20707 return "NT_VMS_PATCHTIME";
20708 default:
20709 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20710 return buff;
20711 }
20712 }
20713
20714 static bool
20715 print_ia64_vms_note (Elf_Internal_Note * pnote)
20716 {
20717 int maxlen = pnote->descsz;
20718
20719 if (maxlen < 2 || (unsigned long) maxlen != pnote->descsz)
20720 goto desc_size_fail;
20721
20722 switch (pnote->type)
20723 {
20724 case NT_VMS_MHD:
20725 if (maxlen <= 36)
20726 goto desc_size_fail;
20727
20728 int l = (int) strnlen (pnote->descdata + 34, maxlen - 34);
20729
20730 printf (_(" Creation date : %.17s\n"), pnote->descdata);
20731 printf (_(" Last patch date: %.17s\n"), pnote->descdata + 17);
20732 if (l + 34 < maxlen)
20733 {
20734 printf (_(" Module name : %s\n"), pnote->descdata + 34);
20735 if (l + 35 < maxlen)
20736 printf (_(" Module version : %s\n"), pnote->descdata + 34 + l + 1);
20737 else
20738 printf (_(" Module version : <missing>\n"));
20739 }
20740 else
20741 {
20742 printf (_(" Module name : <missing>\n"));
20743 printf (_(" Module version : <missing>\n"));
20744 }
20745 break;
20746
20747 case NT_VMS_LNM:
20748 printf (_(" Language: %.*s\n"), maxlen, pnote->descdata);
20749 break;
20750
20751 #ifdef BFD64
20752 case NT_VMS_FPMODE:
20753 printf (_(" Floating Point mode: "));
20754 if (maxlen < 8)
20755 goto desc_size_fail;
20756 /* FIXME: Generate an error if descsz > 8 ? */
20757
20758 printf ("0x%016" BFD_VMA_FMT "x\n",
20759 (bfd_vma) byte_get ((unsigned char *)pnote->descdata, 8));
20760 break;
20761
20762 case NT_VMS_LINKTIME:
20763 printf (_(" Link time: "));
20764 if (maxlen < 8)
20765 goto desc_size_fail;
20766 /* FIXME: Generate an error if descsz > 8 ? */
20767
20768 print_vms_time
20769 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
20770 printf ("\n");
20771 break;
20772
20773 case NT_VMS_PATCHTIME:
20774 printf (_(" Patch time: "));
20775 if (maxlen < 8)
20776 goto desc_size_fail;
20777 /* FIXME: Generate an error if descsz > 8 ? */
20778
20779 print_vms_time
20780 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
20781 printf ("\n");
20782 break;
20783
20784 case NT_VMS_ORIG_DYN:
20785 if (maxlen < 34)
20786 goto desc_size_fail;
20787
20788 printf (_(" Major id: %u, minor id: %u\n"),
20789 (unsigned) byte_get ((unsigned char *)pnote->descdata, 4),
20790 (unsigned) byte_get ((unsigned char *)pnote->descdata + 4, 4));
20791 printf (_(" Last modified : "));
20792 print_vms_time
20793 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata + 8, 8));
20794 printf (_("\n Link flags : "));
20795 printf ("0x%016" BFD_VMA_FMT "x\n",
20796 (bfd_vma) byte_get ((unsigned char *)pnote->descdata + 16, 8));
20797 printf (_(" Header flags: 0x%08x\n"),
20798 (unsigned) byte_get ((unsigned char *)pnote->descdata + 24, 4));
20799 printf (_(" Image id : %.*s\n"), maxlen - 32, pnote->descdata + 32);
20800 break;
20801 #endif
20802
20803 case NT_VMS_IMGNAM:
20804 printf (_(" Image name: %.*s\n"), maxlen, pnote->descdata);
20805 break;
20806
20807 case NT_VMS_GSTNAM:
20808 printf (_(" Global symbol table name: %.*s\n"), maxlen, pnote->descdata);
20809 break;
20810
20811 case NT_VMS_IMGID:
20812 printf (_(" Image id: %.*s\n"), maxlen, pnote->descdata);
20813 break;
20814
20815 case NT_VMS_LINKID:
20816 printf (_(" Linker id: %.*s\n"), maxlen, pnote->descdata);
20817 break;
20818
20819 default:
20820 return false;
20821 }
20822
20823 return true;
20824
20825 desc_size_fail:
20826 printf (_(" <corrupt - data size is too small>\n"));
20827 error (_("corrupt IA64 note: data size is too small\n"));
20828 return false;
20829 }
20830
20831 struct build_attr_cache {
20832 Filedata *filedata;
20833 char *strtab;
20834 unsigned long strtablen;
20835 Elf_Internal_Sym *symtab;
20836 unsigned long nsyms;
20837 } ba_cache;
20838
20839 /* Find the symbol associated with a build attribute that is attached
20840 to address OFFSET. If PNAME is non-NULL then store the name of
20841 the symbol (if found) in the provided pointer, Returns NULL if a
20842 symbol could not be found. */
20843
20844 static Elf_Internal_Sym *
20845 get_symbol_for_build_attribute (Filedata *filedata,
20846 unsigned long offset,
20847 bool is_open_attr,
20848 const char **pname)
20849 {
20850 Elf_Internal_Sym *saved_sym = NULL;
20851 Elf_Internal_Sym *sym;
20852
20853 if (filedata->section_headers != NULL
20854 && (ba_cache.filedata == NULL || filedata != ba_cache.filedata))
20855 {
20856 Elf_Internal_Shdr * symsec;
20857
20858 free (ba_cache.strtab);
20859 ba_cache.strtab = NULL;
20860 free (ba_cache.symtab);
20861 ba_cache.symtab = NULL;
20862
20863 /* Load the symbol and string sections. */
20864 for (symsec = filedata->section_headers;
20865 symsec < filedata->section_headers + filedata->file_header.e_shnum;
20866 symsec ++)
20867 {
20868 if (symsec->sh_type == SHT_SYMTAB
20869 && get_symtab (filedata, symsec,
20870 &ba_cache.symtab, &ba_cache.nsyms,
20871 &ba_cache.strtab, &ba_cache.strtablen))
20872 break;
20873 }
20874 ba_cache.filedata = filedata;
20875 }
20876
20877 if (ba_cache.symtab == NULL)
20878 return NULL;
20879
20880 /* Find a symbol whose value matches offset. */
20881 for (sym = ba_cache.symtab; sym < ba_cache.symtab + ba_cache.nsyms; sym ++)
20882 if (sym->st_value == offset)
20883 {
20884 if (sym->st_name >= ba_cache.strtablen)
20885 /* Huh ? This should not happen. */
20886 continue;
20887
20888 if (ba_cache.strtab[sym->st_name] == 0)
20889 continue;
20890
20891 /* The AArch64, ARM and RISC-V architectures define mapping symbols
20892 (eg $d, $x, $t) which we want to ignore. */
20893 if (ba_cache.strtab[sym->st_name] == '$'
20894 && ba_cache.strtab[sym->st_name + 1] != 0
20895 && ba_cache.strtab[sym->st_name + 2] == 0)
20896 continue;
20897
20898 if (is_open_attr)
20899 {
20900 /* For OPEN attributes we prefer GLOBAL over LOCAL symbols
20901 and FILE or OBJECT symbols over NOTYPE symbols. We skip
20902 FUNC symbols entirely. */
20903 switch (ELF_ST_TYPE (sym->st_info))
20904 {
20905 case STT_OBJECT:
20906 case STT_FILE:
20907 saved_sym = sym;
20908 if (sym->st_size)
20909 {
20910 /* If the symbol has a size associated
20911 with it then we can stop searching. */
20912 sym = ba_cache.symtab + ba_cache.nsyms;
20913 }
20914 continue;
20915
20916 case STT_FUNC:
20917 /* Ignore function symbols. */
20918 continue;
20919
20920 default:
20921 break;
20922 }
20923
20924 switch (ELF_ST_BIND (sym->st_info))
20925 {
20926 case STB_GLOBAL:
20927 if (saved_sym == NULL
20928 || ELF_ST_TYPE (saved_sym->st_info) != STT_OBJECT)
20929 saved_sym = sym;
20930 break;
20931
20932 case STB_LOCAL:
20933 if (saved_sym == NULL)
20934 saved_sym = sym;
20935 break;
20936
20937 default:
20938 break;
20939 }
20940 }
20941 else
20942 {
20943 if (ELF_ST_TYPE (sym->st_info) != STT_FUNC)
20944 continue;
20945
20946 saved_sym = sym;
20947 break;
20948 }
20949 }
20950
20951 if (saved_sym && pname)
20952 * pname = ba_cache.strtab + saved_sym->st_name;
20953
20954 return saved_sym;
20955 }
20956
20957 /* Returns true iff addr1 and addr2 are in the same section. */
20958
20959 static bool
20960 same_section (Filedata * filedata, unsigned long addr1, unsigned long addr2)
20961 {
20962 Elf_Internal_Shdr * a1;
20963 Elf_Internal_Shdr * a2;
20964
20965 a1 = find_section_by_address (filedata, addr1);
20966 a2 = find_section_by_address (filedata, addr2);
20967
20968 return a1 == a2 && a1 != NULL;
20969 }
20970
20971 static bool
20972 print_gnu_build_attribute_description (Elf_Internal_Note * pnote,
20973 Filedata * filedata)
20974 {
20975 static unsigned long global_offset = 0;
20976 static unsigned long global_end = 0;
20977 static unsigned long func_offset = 0;
20978 static unsigned long func_end = 0;
20979
20980 Elf_Internal_Sym *sym;
20981 const char *name;
20982 unsigned long start;
20983 unsigned long end;
20984 bool is_open_attr = pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN;
20985
20986 switch (pnote->descsz)
20987 {
20988 case 0:
20989 /* A zero-length description means that the range of
20990 the previous note of the same type should be used. */
20991 if (is_open_attr)
20992 {
20993 if (global_end > global_offset)
20994 printf (_(" Applies to region from %#lx to %#lx\n"),
20995 global_offset, global_end);
20996 else
20997 printf (_(" Applies to region from %#lx\n"), global_offset);
20998 }
20999 else
21000 {
21001 if (func_end > func_offset)
21002 printf (_(" Applies to region from %#lx to %#lx\n"), func_offset, func_end);
21003 else
21004 printf (_(" Applies to region from %#lx\n"), func_offset);
21005 }
21006 return true;
21007
21008 case 4:
21009 start = byte_get ((unsigned char *) pnote->descdata, 4);
21010 end = 0;
21011 break;
21012
21013 case 8:
21014 start = byte_get ((unsigned char *) pnote->descdata, 4);
21015 end = byte_get ((unsigned char *) pnote->descdata + 4, 4);
21016 break;
21017
21018 case 16:
21019 start = byte_get ((unsigned char *) pnote->descdata, 8);
21020 end = byte_get ((unsigned char *) pnote->descdata + 8, 8);
21021 break;
21022
21023 default:
21024 error (_(" <invalid description size: %lx>\n"), pnote->descsz);
21025 printf (_(" <invalid descsz>"));
21026 return false;
21027 }
21028
21029 name = NULL;
21030 sym = get_symbol_for_build_attribute (filedata, start, is_open_attr, & name);
21031 /* As of version 5 of the annobin plugin, filename symbols are biased by 2
21032 in order to avoid them being confused with the start address of the
21033 first function in the file... */
21034 if (sym == NULL && is_open_attr)
21035 sym = get_symbol_for_build_attribute (filedata, start + 2, is_open_attr,
21036 & name);
21037
21038 if (end == 0 && sym != NULL && sym->st_size > 0)
21039 end = start + sym->st_size;
21040
21041 if (is_open_attr)
21042 {
21043 /* FIXME: Need to properly allow for section alignment.
21044 16 is just the alignment used on x86_64. */
21045 if (global_end > 0
21046 && start > BFD_ALIGN (global_end, 16)
21047 /* Build notes are not guaranteed to be organised in order of
21048 increasing address, but we should find the all of the notes
21049 for one section in the same place. */
21050 && same_section (filedata, start, global_end))
21051 warn (_("Gap in build notes detected from %#lx to %#lx\n"),
21052 global_end + 1, start - 1);
21053
21054 printf (_(" Applies to region from %#lx"), start);
21055 global_offset = start;
21056
21057 if (end)
21058 {
21059 printf (_(" to %#lx"), end);
21060 global_end = end;
21061 }
21062 }
21063 else
21064 {
21065 printf (_(" Applies to region from %#lx"), start);
21066 func_offset = start;
21067
21068 if (end)
21069 {
21070 printf (_(" to %#lx"), end);
21071 func_end = end;
21072 }
21073 }
21074
21075 if (sym && name)
21076 printf (_(" (%s)"), name);
21077
21078 printf ("\n");
21079 return true;
21080 }
21081
21082 static bool
21083 print_gnu_build_attribute_name (Elf_Internal_Note * pnote)
21084 {
21085 static const char string_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_STRING, 0 };
21086 static const char number_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC, 0 };
21087 static const char bool_expected [3] = { GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE, GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE, 0 };
21088 char name_type;
21089 char name_attribute;
21090 const char * expected_types;
21091 const char * name = pnote->namedata;
21092 const char * text;
21093 signed int left;
21094
21095 if (name == NULL || pnote->namesz < 2)
21096 {
21097 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
21098 print_symbol (-20, _(" <corrupt name>"));
21099 return false;
21100 }
21101
21102 if (do_wide)
21103 left = 28;
21104 else
21105 left = 20;
21106
21107 /* Version 2 of the spec adds a "GA" prefix to the name field. */
21108 if (name[0] == 'G' && name[1] == 'A')
21109 {
21110 if (pnote->namesz < 4)
21111 {
21112 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
21113 print_symbol (-20, _(" <corrupt name>"));
21114 return false;
21115 }
21116
21117 printf ("GA");
21118 name += 2;
21119 left -= 2;
21120 }
21121
21122 switch ((name_type = * name))
21123 {
21124 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
21125 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
21126 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
21127 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
21128 printf ("%c", * name);
21129 left --;
21130 break;
21131 default:
21132 error (_("unrecognised attribute type in name field: %d\n"), name_type);
21133 print_symbol (-20, _("<unknown name type>"));
21134 return false;
21135 }
21136
21137 ++ name;
21138 text = NULL;
21139
21140 switch ((name_attribute = * name))
21141 {
21142 case GNU_BUILD_ATTRIBUTE_VERSION:
21143 text = _("<version>");
21144 expected_types = string_expected;
21145 ++ name;
21146 break;
21147 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
21148 text = _("<stack prot>");
21149 expected_types = "!+*";
21150 ++ name;
21151 break;
21152 case GNU_BUILD_ATTRIBUTE_RELRO:
21153 text = _("<relro>");
21154 expected_types = bool_expected;
21155 ++ name;
21156 break;
21157 case GNU_BUILD_ATTRIBUTE_STACK_SIZE:
21158 text = _("<stack size>");
21159 expected_types = number_expected;
21160 ++ name;
21161 break;
21162 case GNU_BUILD_ATTRIBUTE_TOOL:
21163 text = _("<tool>");
21164 expected_types = string_expected;
21165 ++ name;
21166 break;
21167 case GNU_BUILD_ATTRIBUTE_ABI:
21168 text = _("<ABI>");
21169 expected_types = "$*";
21170 ++ name;
21171 break;
21172 case GNU_BUILD_ATTRIBUTE_PIC:
21173 text = _("<PIC>");
21174 expected_types = number_expected;
21175 ++ name;
21176 break;
21177 case GNU_BUILD_ATTRIBUTE_SHORT_ENUM:
21178 text = _("<short enum>");
21179 expected_types = bool_expected;
21180 ++ name;
21181 break;
21182 default:
21183 if (ISPRINT (* name))
21184 {
21185 int len = strnlen (name, pnote->namesz - (name - pnote->namedata)) + 1;
21186
21187 if (len > left && ! do_wide)
21188 len = left;
21189 printf ("%.*s:", len, name);
21190 left -= len;
21191 name += len;
21192 }
21193 else
21194 {
21195 static char tmpbuf [128];
21196
21197 error (_("unrecognised byte in name field: %d\n"), * name);
21198 sprintf (tmpbuf, _("<unknown:_%d>"), * name);
21199 text = tmpbuf;
21200 name ++;
21201 }
21202 expected_types = "*$!+";
21203 break;
21204 }
21205
21206 if (text)
21207 left -= printf ("%s", text);
21208
21209 if (strchr (expected_types, name_type) == NULL)
21210 warn (_("attribute does not have an expected type (%c)\n"), name_type);
21211
21212 if ((unsigned long)(name - pnote->namedata) > pnote->namesz)
21213 {
21214 error (_("corrupt name field: namesz: %lu but parsing gets to %ld\n"),
21215 (unsigned long) pnote->namesz,
21216 (long) (name - pnote->namedata));
21217 return false;
21218 }
21219
21220 if (left < 1 && ! do_wide)
21221 return true;
21222
21223 switch (name_type)
21224 {
21225 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
21226 {
21227 unsigned int bytes;
21228 unsigned long long val = 0;
21229 unsigned int shift = 0;
21230 char * decoded = NULL;
21231
21232 bytes = pnote->namesz - (name - pnote->namedata);
21233 if (bytes > 0)
21234 /* The -1 is because the name field is always 0 terminated, and we
21235 want to be able to ensure that the shift in the while loop below
21236 will not overflow. */
21237 -- bytes;
21238
21239 if (bytes > sizeof (val))
21240 {
21241 error (_("corrupt numeric name field: too many bytes in the value: %x\n"),
21242 bytes);
21243 bytes = sizeof (val);
21244 }
21245 /* We do not bother to warn if bytes == 0 as this can
21246 happen with some early versions of the gcc plugin. */
21247
21248 while (bytes --)
21249 {
21250 unsigned long long byte = *name++ & 0xff;
21251
21252 val |= byte << shift;
21253 shift += 8;
21254 }
21255
21256 switch (name_attribute)
21257 {
21258 case GNU_BUILD_ATTRIBUTE_PIC:
21259 switch (val)
21260 {
21261 case 0: decoded = "static"; break;
21262 case 1: decoded = "pic"; break;
21263 case 2: decoded = "PIC"; break;
21264 case 3: decoded = "pie"; break;
21265 case 4: decoded = "PIE"; break;
21266 default: break;
21267 }
21268 break;
21269 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
21270 switch (val)
21271 {
21272 /* Based upon the SPCT_FLAG_xxx enum values in gcc/cfgexpand.c. */
21273 case 0: decoded = "off"; break;
21274 case 1: decoded = "on"; break;
21275 case 2: decoded = "all"; break;
21276 case 3: decoded = "strong"; break;
21277 case 4: decoded = "explicit"; break;
21278 default: break;
21279 }
21280 break;
21281 default:
21282 break;
21283 }
21284
21285 if (decoded != NULL)
21286 {
21287 print_symbol (-left, decoded);
21288 left = 0;
21289 }
21290 else if (val == 0)
21291 {
21292 printf ("0x0");
21293 left -= 3;
21294 }
21295 else
21296 {
21297 if (do_wide)
21298 left -= printf ("0x%llx", val);
21299 else
21300 left -= printf ("0x%-.*llx", left, val);
21301 }
21302 }
21303 break;
21304 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
21305 left -= print_symbol (- left, name);
21306 break;
21307 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
21308 left -= print_symbol (- left, "true");
21309 break;
21310 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
21311 left -= print_symbol (- left, "false");
21312 break;
21313 }
21314
21315 if (do_wide && left > 0)
21316 printf ("%-*s", left, " ");
21317
21318 return true;
21319 }
21320
21321 /* Print the contents of PNOTE as hex. */
21322
21323 static void
21324 print_note_contents_hex (Elf_Internal_Note *pnote)
21325 {
21326 if (pnote->descsz)
21327 {
21328 unsigned long i;
21329
21330 printf (_(" description data: "));
21331 for (i = 0; i < pnote->descsz; i++)
21332 printf ("%02x ", pnote->descdata[i] & 0xff);
21333 if (!do_wide)
21334 printf ("\n");
21335 }
21336
21337 if (do_wide)
21338 printf ("\n");
21339 }
21340
21341 #if defined HAVE_MSGPACK
21342
21343 static void
21344 print_indents (int n)
21345 {
21346 printf (" ");
21347
21348 for (int i = 0; i < n; i++)
21349 printf (" ");
21350 }
21351
21352 /* Print OBJ in human-readable form. */
21353
21354 static void
21355 dump_msgpack_obj (const msgpack_object *obj, int indent)
21356 {
21357 switch (obj->type)
21358 {
21359 case MSGPACK_OBJECT_NIL:
21360 printf ("(nil)");
21361 break;
21362
21363 case MSGPACK_OBJECT_BOOLEAN:
21364 printf ("%s", obj->via.boolean ? "true" : "false");
21365 break;
21366
21367 case MSGPACK_OBJECT_POSITIVE_INTEGER:
21368 printf ("%" PRIu64, obj->via.u64);
21369 break;
21370
21371 case MSGPACK_OBJECT_NEGATIVE_INTEGER:
21372 printf ("%" PRIi64, obj->via.i64);
21373 break;
21374
21375 case MSGPACK_OBJECT_FLOAT32:
21376 case MSGPACK_OBJECT_FLOAT64:
21377 printf ("%f", obj->via.f64);
21378 break;
21379
21380 case MSGPACK_OBJECT_STR:
21381 printf ("\"%.*s\"", obj->via.str.size, obj->via.str.ptr);
21382 break;
21383
21384 case MSGPACK_OBJECT_ARRAY:
21385 {
21386 const msgpack_object_array *array = &obj->via.array;
21387
21388 printf ("[\n");
21389 ++indent;
21390
21391 for (uint32_t i = 0; i < array->size; ++i)
21392 {
21393 const msgpack_object *item = &array->ptr[i];
21394
21395 print_indents (indent);
21396 dump_msgpack_obj (item, indent);
21397 printf (",\n");
21398 }
21399
21400 --indent;
21401 print_indents (indent);
21402 printf ("]");
21403 break;
21404 }
21405 break;
21406
21407 case MSGPACK_OBJECT_MAP:
21408 {
21409 const msgpack_object_map *map = &obj->via.map;
21410
21411 printf ("{\n");
21412 ++indent;
21413
21414 for (uint32_t i = 0; i < map->size; ++i)
21415 {
21416 const msgpack_object_kv *kv = &map->ptr[i];
21417 const msgpack_object *key = &kv->key;
21418 const msgpack_object *val = &kv->val;
21419
21420 print_indents (indent);
21421 dump_msgpack_obj (key, indent);
21422 printf (": ");
21423 dump_msgpack_obj (val, indent);
21424
21425 printf (",\n");
21426 }
21427
21428 --indent;
21429 print_indents (indent);
21430 printf ("}");
21431
21432 break;
21433 }
21434
21435 case MSGPACK_OBJECT_BIN:
21436 printf ("(bin)");
21437 break;
21438
21439 case MSGPACK_OBJECT_EXT:
21440 printf ("(ext)");
21441 break;
21442 }
21443 }
21444
21445 static void
21446 dump_msgpack (const msgpack_unpacked *msg)
21447 {
21448 print_indents (0);
21449 dump_msgpack_obj (&msg->data, 0);
21450 printf ("\n");
21451 }
21452
21453 #endif /* defined HAVE_MSGPACK */
21454
21455 static bool
21456 print_amdgpu_note (Elf_Internal_Note *pnote)
21457 {
21458 #if defined HAVE_MSGPACK
21459 /* If msgpack is available, decode and dump the note's content. */
21460 bool ret;
21461 msgpack_unpacked msg;
21462 msgpack_unpack_return msgpack_ret;
21463
21464 assert (pnote->type == NT_AMDGPU_METADATA);
21465
21466 msgpack_unpacked_init (&msg);
21467 msgpack_ret = msgpack_unpack_next (&msg, pnote->descdata, pnote->descsz,
21468 NULL);
21469
21470 switch (msgpack_ret)
21471 {
21472 case MSGPACK_UNPACK_SUCCESS:
21473 dump_msgpack (&msg);
21474 ret = true;
21475 break;
21476
21477 default:
21478 error (_("failed to unpack msgpack contents in NT_AMDGPU_METADATA note"));
21479 ret = false;
21480 break;
21481 }
21482
21483 msgpack_unpacked_destroy (&msg);
21484 return ret;
21485 #else
21486 /* msgpack is not available, dump contents as hex. */
21487 print_note_contents_hex (pnote);
21488 return true;
21489 #endif
21490 }
21491
21492 /* Note that by the ELF standard, the name field is already null byte
21493 terminated, and namesz includes the terminating null byte.
21494 I.E. the value of namesz for the name "FSF" is 4.
21495
21496 If the value of namesz is zero, there is no name present. */
21497
21498 static bool
21499 process_note (Elf_Internal_Note * pnote,
21500 Filedata * filedata)
21501 {
21502 const char * name = pnote->namesz ? pnote->namedata : "(NONE)";
21503 const char * nt;
21504
21505 if (pnote->namesz == 0)
21506 /* If there is no note name, then use the default set of
21507 note type strings. */
21508 nt = get_note_type (filedata, pnote->type);
21509
21510 else if (startswith (pnote->namedata, "GNU"))
21511 /* GNU-specific object file notes. */
21512 nt = get_gnu_elf_note_type (pnote->type);
21513
21514 else if (startswith (pnote->namedata, "AMDGPU"))
21515 /* AMDGPU-specific object file notes. */
21516 nt = get_amdgpu_elf_note_type (pnote->type);
21517
21518 else if (startswith (pnote->namedata, "FreeBSD"))
21519 /* FreeBSD-specific core file notes. */
21520 nt = get_freebsd_elfcore_note_type (filedata, pnote->type);
21521
21522 else if (startswith (pnote->namedata, "NetBSD-CORE"))
21523 /* NetBSD-specific core file notes. */
21524 nt = get_netbsd_elfcore_note_type (filedata, pnote->type);
21525
21526 else if (startswith (pnote->namedata, "NetBSD"))
21527 /* NetBSD-specific core file notes. */
21528 return process_netbsd_elf_note (pnote);
21529
21530 else if (startswith (pnote->namedata, "PaX"))
21531 /* NetBSD-specific core file notes. */
21532 return process_netbsd_elf_note (pnote);
21533
21534 else if (startswith (pnote->namedata, "OpenBSD"))
21535 /* OpenBSD-specific core file notes. */
21536 nt = get_openbsd_elfcore_note_type (filedata, pnote->type);
21537
21538 else if (startswith (pnote->namedata, "SPU/"))
21539 {
21540 /* SPU-specific core file notes. */
21541 nt = pnote->namedata + 4;
21542 name = "SPU";
21543 }
21544
21545 else if (startswith (pnote->namedata, "IPF/VMS"))
21546 /* VMS/ia64-specific file notes. */
21547 nt = get_ia64_vms_note_type (pnote->type);
21548
21549 else if (startswith (pnote->namedata, "stapsdt"))
21550 nt = get_stapsdt_note_type (pnote->type);
21551
21552 else
21553 /* Don't recognize this note name; just use the default set of
21554 note type strings. */
21555 nt = get_note_type (filedata, pnote->type);
21556
21557 printf (" ");
21558
21559 if (((startswith (pnote->namedata, "GA")
21560 && strchr ("*$!+", pnote->namedata[2]) != NULL)
21561 || strchr ("*$!+", pnote->namedata[0]) != NULL)
21562 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
21563 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
21564 print_gnu_build_attribute_name (pnote);
21565 else
21566 print_symbol (-20, name);
21567
21568 if (do_wide)
21569 printf (" 0x%08lx\t%s\t", pnote->descsz, nt);
21570 else
21571 printf (" 0x%08lx\t%s\n", pnote->descsz, nt);
21572
21573 if (startswith (pnote->namedata, "IPF/VMS"))
21574 return print_ia64_vms_note (pnote);
21575 else if (startswith (pnote->namedata, "GNU"))
21576 return print_gnu_note (filedata, pnote);
21577 else if (startswith (pnote->namedata, "stapsdt"))
21578 return print_stapsdt_note (pnote);
21579 else if (startswith (pnote->namedata, "CORE"))
21580 return print_core_note (pnote);
21581 else if (startswith (pnote->namedata, "FDO"))
21582 return print_fdo_note (pnote);
21583 else if (((startswith (pnote->namedata, "GA")
21584 && strchr ("*$!+", pnote->namedata[2]) != NULL)
21585 || strchr ("*$!+", pnote->namedata[0]) != NULL)
21586 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
21587 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
21588 return print_gnu_build_attribute_description (pnote, filedata);
21589 else if (startswith (pnote->namedata, "AMDGPU")
21590 && pnote->type == NT_AMDGPU_METADATA)
21591 return print_amdgpu_note (pnote);
21592
21593 print_note_contents_hex (pnote);
21594 return true;
21595 }
21596
21597 static bool
21598 process_notes_at (Filedata * filedata,
21599 Elf_Internal_Shdr * section,
21600 bfd_vma offset,
21601 bfd_vma length,
21602 bfd_vma align)
21603 {
21604 Elf_External_Note *pnotes;
21605 Elf_External_Note *external;
21606 char *end;
21607 bool res = true;
21608
21609 if (length <= 0)
21610 return false;
21611
21612 if (section)
21613 {
21614 pnotes = (Elf_External_Note *) get_section_contents (section, filedata);
21615 if (pnotes)
21616 {
21617 if (! apply_relocations (filedata, section, (unsigned char *) pnotes, length, NULL, NULL))
21618 {
21619 free (pnotes);
21620 return false;
21621 }
21622 }
21623 }
21624 else
21625 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
21626 _("notes"));
21627
21628 if (pnotes == NULL)
21629 return false;
21630
21631 external = pnotes;
21632
21633 if (filedata->is_separate)
21634 printf (_("In linked file '%s': "), filedata->file_name);
21635 else
21636 printf ("\n");
21637 if (section)
21638 printf (_("Displaying notes found in: %s\n"), printable_section_name (filedata, section));
21639 else
21640 printf (_("Displaying notes found at file offset 0x%08lx with length 0x%08lx:\n"),
21641 (unsigned long) offset, (unsigned long) length);
21642
21643 /* NB: Some note sections may have alignment value of 0 or 1. gABI
21644 specifies that notes should be aligned to 4 bytes in 32-bit
21645 objects and to 8 bytes in 64-bit objects. As a Linux extension,
21646 we also support 4 byte alignment in 64-bit objects. If section
21647 alignment is less than 4, we treate alignment as 4 bytes. */
21648 if (align < 4)
21649 align = 4;
21650 else if (align != 4 && align != 8)
21651 {
21652 warn (_("Corrupt note: alignment %ld, expecting 4 or 8\n"),
21653 (long) align);
21654 free (pnotes);
21655 return false;
21656 }
21657
21658 printf (_(" %-20s %-10s\tDescription\n"), _("Owner"), _("Data size"));
21659
21660 end = (char *) pnotes + length;
21661 while ((char *) external < end)
21662 {
21663 Elf_Internal_Note inote;
21664 size_t min_notesz;
21665 char * next;
21666 char * temp = NULL;
21667 size_t data_remaining = end - (char *) external;
21668
21669 if (!is_ia64_vms (filedata))
21670 {
21671 /* PR binutils/15191
21672 Make sure that there is enough data to read. */
21673 min_notesz = offsetof (Elf_External_Note, name);
21674 if (data_remaining < min_notesz)
21675 {
21676 warn (ngettext ("Corrupt note: only %ld byte remains, "
21677 "not enough for a full note\n",
21678 "Corrupt note: only %ld bytes remain, "
21679 "not enough for a full note\n",
21680 data_remaining),
21681 (long) data_remaining);
21682 break;
21683 }
21684 data_remaining -= min_notesz;
21685
21686 inote.type = BYTE_GET (external->type);
21687 inote.namesz = BYTE_GET (external->namesz);
21688 inote.namedata = external->name;
21689 inote.descsz = BYTE_GET (external->descsz);
21690 inote.descdata = ((char *) external
21691 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
21692 inote.descpos = offset + (inote.descdata - (char *) pnotes);
21693 next = ((char *) external
21694 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
21695 }
21696 else
21697 {
21698 Elf64_External_VMS_Note *vms_external;
21699
21700 /* PR binutils/15191
21701 Make sure that there is enough data to read. */
21702 min_notesz = offsetof (Elf64_External_VMS_Note, name);
21703 if (data_remaining < min_notesz)
21704 {
21705 warn (ngettext ("Corrupt note: only %ld byte remains, "
21706 "not enough for a full note\n",
21707 "Corrupt note: only %ld bytes remain, "
21708 "not enough for a full note\n",
21709 data_remaining),
21710 (long) data_remaining);
21711 break;
21712 }
21713 data_remaining -= min_notesz;
21714
21715 vms_external = (Elf64_External_VMS_Note *) external;
21716 inote.type = BYTE_GET (vms_external->type);
21717 inote.namesz = BYTE_GET (vms_external->namesz);
21718 inote.namedata = vms_external->name;
21719 inote.descsz = BYTE_GET (vms_external->descsz);
21720 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
21721 inote.descpos = offset + (inote.descdata - (char *) pnotes);
21722 next = inote.descdata + align_power (inote.descsz, 3);
21723 }
21724
21725 /* PR 17531: file: 3443835e. */
21726 /* PR 17531: file: id:000000,sig:11,src:006986,op:havoc,rep:4. */
21727 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
21728 || (size_t) (inote.descdata - inote.namedata) > data_remaining
21729 || (size_t) (next - inote.descdata) < inote.descsz
21730 || ((size_t) (next - inote.descdata)
21731 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
21732 {
21733 warn (_("note with invalid namesz and/or descsz found at offset 0x%lx\n"),
21734 (unsigned long) ((char *) external - (char *) pnotes));
21735 warn (_(" type: 0x%lx, namesize: 0x%08lx, descsize: 0x%08lx, alignment: %u\n"),
21736 inote.type, inote.namesz, inote.descsz, (int) align);
21737 break;
21738 }
21739
21740 external = (Elf_External_Note *) next;
21741
21742 /* Verify that name is null terminated. It appears that at least
21743 one version of Linux (RedHat 6.0) generates corefiles that don't
21744 comply with the ELF spec by failing to include the null byte in
21745 namesz. */
21746 if (inote.namesz > 0 && inote.namedata[inote.namesz - 1] != '\0')
21747 {
21748 if ((size_t) (inote.descdata - inote.namedata) == inote.namesz)
21749 {
21750 temp = (char *) malloc (inote.namesz + 1);
21751 if (temp == NULL)
21752 {
21753 error (_("Out of memory allocating space for inote name\n"));
21754 res = false;
21755 break;
21756 }
21757
21758 memcpy (temp, inote.namedata, inote.namesz);
21759 inote.namedata = temp;
21760 }
21761 inote.namedata[inote.namesz] = 0;
21762 }
21763
21764 if (! process_note (& inote, filedata))
21765 res = false;
21766
21767 free (temp);
21768 temp = NULL;
21769 }
21770
21771 free (pnotes);
21772
21773 return res;
21774 }
21775
21776 static bool
21777 process_corefile_note_segments (Filedata * filedata)
21778 {
21779 Elf_Internal_Phdr *segment;
21780 unsigned int i;
21781 bool res = true;
21782
21783 if (! get_program_headers (filedata))
21784 return true;
21785
21786 for (i = 0, segment = filedata->program_headers;
21787 i < filedata->file_header.e_phnum;
21788 i++, segment++)
21789 {
21790 if (segment->p_type == PT_NOTE)
21791 if (! process_notes_at (filedata, NULL,
21792 (bfd_vma) segment->p_offset,
21793 (bfd_vma) segment->p_filesz,
21794 (bfd_vma) segment->p_align))
21795 res = false;
21796 }
21797
21798 return res;
21799 }
21800
21801 static bool
21802 process_v850_notes (Filedata * filedata, bfd_vma offset, bfd_vma length)
21803 {
21804 Elf_External_Note * pnotes;
21805 Elf_External_Note * external;
21806 char * end;
21807 bool res = true;
21808
21809 if (length <= 0)
21810 return false;
21811
21812 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
21813 _("v850 notes"));
21814 if (pnotes == NULL)
21815 return false;
21816
21817 external = pnotes;
21818 end = (char*) pnotes + length;
21819
21820 printf (_("\nDisplaying contents of Renesas V850 notes section at offset 0x%lx with length 0x%lx:\n"),
21821 (unsigned long) offset, (unsigned long) length);
21822
21823 while ((char *) external + sizeof (Elf_External_Note) < end)
21824 {
21825 Elf_External_Note * next;
21826 Elf_Internal_Note inote;
21827
21828 inote.type = BYTE_GET (external->type);
21829 inote.namesz = BYTE_GET (external->namesz);
21830 inote.namedata = external->name;
21831 inote.descsz = BYTE_GET (external->descsz);
21832 inote.descdata = inote.namedata + align_power (inote.namesz, 2);
21833 inote.descpos = offset + (inote.descdata - (char *) pnotes);
21834
21835 if (inote.descdata < (char *) pnotes || inote.descdata >= end)
21836 {
21837 warn (_("Corrupt note: name size is too big: %lx\n"), inote.namesz);
21838 inote.descdata = inote.namedata;
21839 inote.namesz = 0;
21840 }
21841
21842 next = (Elf_External_Note *) (inote.descdata + align_power (inote.descsz, 2));
21843
21844 if ( ((char *) next > end)
21845 || ((char *) next < (char *) pnotes))
21846 {
21847 warn (_("corrupt descsz found in note at offset 0x%lx\n"),
21848 (unsigned long) ((char *) external - (char *) pnotes));
21849 warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
21850 inote.type, inote.namesz, inote.descsz);
21851 break;
21852 }
21853
21854 external = next;
21855
21856 /* Prevent out-of-bounds indexing. */
21857 if ( inote.namedata + inote.namesz > end
21858 || inote.namedata + inote.namesz < inote.namedata)
21859 {
21860 warn (_("corrupt namesz found in note at offset 0x%lx\n"),
21861 (unsigned long) ((char *) external - (char *) pnotes));
21862 warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
21863 inote.type, inote.namesz, inote.descsz);
21864 break;
21865 }
21866
21867 printf (" %s: ", get_v850_elf_note_type (inote.type));
21868
21869 if (! print_v850_note (& inote))
21870 {
21871 res = false;
21872 printf ("<corrupt sizes: namesz: %lx, descsz: %lx>\n",
21873 inote.namesz, inote.descsz);
21874 }
21875 }
21876
21877 free (pnotes);
21878
21879 return res;
21880 }
21881
21882 static bool
21883 process_note_sections (Filedata * filedata)
21884 {
21885 Elf_Internal_Shdr *section;
21886 unsigned long i;
21887 unsigned int n = 0;
21888 bool res = true;
21889
21890 for (i = 0, section = filedata->section_headers;
21891 i < filedata->file_header.e_shnum && section != NULL;
21892 i++, section++)
21893 {
21894 if (section->sh_type == SHT_NOTE)
21895 {
21896 if (! process_notes_at (filedata, section,
21897 (bfd_vma) section->sh_offset,
21898 (bfd_vma) section->sh_size,
21899 (bfd_vma) section->sh_addralign))
21900 res = false;
21901 n++;
21902 }
21903
21904 if (( filedata->file_header.e_machine == EM_V800
21905 || filedata->file_header.e_machine == EM_V850
21906 || filedata->file_header.e_machine == EM_CYGNUS_V850)
21907 && section->sh_type == SHT_RENESAS_INFO)
21908 {
21909 if (! process_v850_notes (filedata,
21910 (bfd_vma) section->sh_offset,
21911 (bfd_vma) section->sh_size))
21912 res = false;
21913 n++;
21914 }
21915 }
21916
21917 if (n == 0)
21918 /* Try processing NOTE segments instead. */
21919 return process_corefile_note_segments (filedata);
21920
21921 return res;
21922 }
21923
21924 static bool
21925 process_notes (Filedata * filedata)
21926 {
21927 /* If we have not been asked to display the notes then do nothing. */
21928 if (! do_notes)
21929 return true;
21930
21931 if (filedata->file_header.e_type != ET_CORE)
21932 return process_note_sections (filedata);
21933
21934 /* No program headers means no NOTE segment. */
21935 if (filedata->file_header.e_phnum > 0)
21936 return process_corefile_note_segments (filedata);
21937
21938 if (filedata->is_separate)
21939 printf (_("No notes found in linked file '%s'.\n"),
21940 filedata->file_name);
21941 else
21942 printf (_("No notes found file.\n"));
21943
21944 return true;
21945 }
21946
21947 static unsigned char *
21948 display_public_gnu_attributes (unsigned char * start,
21949 const unsigned char * const end)
21950 {
21951 printf (_(" Unknown GNU attribute: %s\n"), start);
21952
21953 start += strnlen ((char *) start, end - start);
21954 display_raw_attribute (start, end);
21955
21956 return (unsigned char *) end;
21957 }
21958
21959 static unsigned char *
21960 display_generic_attribute (unsigned char * start,
21961 unsigned int tag,
21962 const unsigned char * const end)
21963 {
21964 if (tag == 0)
21965 return (unsigned char *) end;
21966
21967 return display_tag_value (tag, start, end);
21968 }
21969
21970 static bool
21971 process_arch_specific (Filedata * filedata)
21972 {
21973 if (! do_arch)
21974 return true;
21975
21976 switch (filedata->file_header.e_machine)
21977 {
21978 case EM_ARC:
21979 case EM_ARC_COMPACT:
21980 case EM_ARC_COMPACT2:
21981 return process_attributes (filedata, "ARC", SHT_ARC_ATTRIBUTES,
21982 display_arc_attribute,
21983 display_generic_attribute);
21984 case EM_ARM:
21985 return process_attributes (filedata, "aeabi", SHT_ARM_ATTRIBUTES,
21986 display_arm_attribute,
21987 display_generic_attribute);
21988
21989 case EM_MIPS:
21990 case EM_MIPS_RS3_LE:
21991 return process_mips_specific (filedata);
21992
21993 case EM_MSP430:
21994 return process_attributes (filedata, "mspabi", SHT_MSP430_ATTRIBUTES,
21995 display_msp430_attribute,
21996 display_msp430_gnu_attribute);
21997
21998 case EM_RISCV:
21999 return process_attributes (filedata, "riscv", SHT_RISCV_ATTRIBUTES,
22000 display_riscv_attribute,
22001 display_generic_attribute);
22002
22003 case EM_NDS32:
22004 return process_nds32_specific (filedata);
22005
22006 case EM_68K:
22007 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22008 display_m68k_gnu_attribute);
22009
22010 case EM_PPC:
22011 case EM_PPC64:
22012 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22013 display_power_gnu_attribute);
22014
22015 case EM_S390:
22016 case EM_S390_OLD:
22017 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22018 display_s390_gnu_attribute);
22019
22020 case EM_SPARC:
22021 case EM_SPARC32PLUS:
22022 case EM_SPARCV9:
22023 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22024 display_sparc_gnu_attribute);
22025
22026 case EM_TI_C6000:
22027 return process_attributes (filedata, "c6xabi", SHT_C6000_ATTRIBUTES,
22028 display_tic6x_attribute,
22029 display_generic_attribute);
22030
22031 case EM_CSKY:
22032 return process_attributes (filedata, "csky", SHT_CSKY_ATTRIBUTES,
22033 display_csky_attribute, NULL);
22034
22035 default:
22036 return process_attributes (filedata, "gnu", SHT_GNU_ATTRIBUTES,
22037 display_public_gnu_attributes,
22038 display_generic_attribute);
22039 }
22040 }
22041
22042 static bool
22043 get_file_header (Filedata * filedata)
22044 {
22045 /* Read in the identity array. */
22046 if (fread (filedata->file_header.e_ident, EI_NIDENT, 1, filedata->handle) != 1)
22047 return false;
22048
22049 /* Determine how to read the rest of the header. */
22050 switch (filedata->file_header.e_ident[EI_DATA])
22051 {
22052 default:
22053 case ELFDATANONE:
22054 case ELFDATA2LSB:
22055 byte_get = byte_get_little_endian;
22056 byte_put = byte_put_little_endian;
22057 break;
22058 case ELFDATA2MSB:
22059 byte_get = byte_get_big_endian;
22060 byte_put = byte_put_big_endian;
22061 break;
22062 }
22063
22064 /* For now we only support 32 bit and 64 bit ELF files. */
22065 is_32bit_elf = (filedata->file_header.e_ident[EI_CLASS] != ELFCLASS64);
22066
22067 /* Read in the rest of the header. */
22068 if (is_32bit_elf)
22069 {
22070 Elf32_External_Ehdr ehdr32;
22071
22072 if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, filedata->handle) != 1)
22073 return false;
22074
22075 filedata->file_header.e_type = BYTE_GET (ehdr32.e_type);
22076 filedata->file_header.e_machine = BYTE_GET (ehdr32.e_machine);
22077 filedata->file_header.e_version = BYTE_GET (ehdr32.e_version);
22078 filedata->file_header.e_entry = BYTE_GET (ehdr32.e_entry);
22079 filedata->file_header.e_phoff = BYTE_GET (ehdr32.e_phoff);
22080 filedata->file_header.e_shoff = BYTE_GET (ehdr32.e_shoff);
22081 filedata->file_header.e_flags = BYTE_GET (ehdr32.e_flags);
22082 filedata->file_header.e_ehsize = BYTE_GET (ehdr32.e_ehsize);
22083 filedata->file_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
22084 filedata->file_header.e_phnum = BYTE_GET (ehdr32.e_phnum);
22085 filedata->file_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
22086 filedata->file_header.e_shnum = BYTE_GET (ehdr32.e_shnum);
22087 filedata->file_header.e_shstrndx = BYTE_GET (ehdr32.e_shstrndx);
22088 }
22089 else
22090 {
22091 Elf64_External_Ehdr ehdr64;
22092
22093 /* If we have been compiled with sizeof (bfd_vma) == 4, then
22094 we will not be able to cope with the 64bit data found in
22095 64 ELF files. Detect this now and abort before we start
22096 overwriting things. */
22097 if (sizeof (bfd_vma) < 8)
22098 {
22099 error (_("This instance of readelf has been built without support for a\n\
22100 64 bit data type and so it cannot read 64 bit ELF files.\n"));
22101 return false;
22102 }
22103
22104 if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, filedata->handle) != 1)
22105 return false;
22106
22107 filedata->file_header.e_type = BYTE_GET (ehdr64.e_type);
22108 filedata->file_header.e_machine = BYTE_GET (ehdr64.e_machine);
22109 filedata->file_header.e_version = BYTE_GET (ehdr64.e_version);
22110 filedata->file_header.e_entry = BYTE_GET (ehdr64.e_entry);
22111 filedata->file_header.e_phoff = BYTE_GET (ehdr64.e_phoff);
22112 filedata->file_header.e_shoff = BYTE_GET (ehdr64.e_shoff);
22113 filedata->file_header.e_flags = BYTE_GET (ehdr64.e_flags);
22114 filedata->file_header.e_ehsize = BYTE_GET (ehdr64.e_ehsize);
22115 filedata->file_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
22116 filedata->file_header.e_phnum = BYTE_GET (ehdr64.e_phnum);
22117 filedata->file_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
22118 filedata->file_header.e_shnum = BYTE_GET (ehdr64.e_shnum);
22119 filedata->file_header.e_shstrndx = BYTE_GET (ehdr64.e_shstrndx);
22120 }
22121
22122 return true;
22123 }
22124
22125 static void
22126 free_filedata (Filedata *filedata)
22127 {
22128 free (filedata->program_interpreter);
22129 free (filedata->program_headers);
22130 free (filedata->section_headers);
22131 free (filedata->string_table);
22132 free (filedata->dump.dump_sects);
22133 free (filedata->dynamic_strings);
22134 free (filedata->dynamic_symbols);
22135 free (filedata->dynamic_syminfo);
22136 free (filedata->dynamic_section);
22137
22138 while (filedata->symtab_shndx_list != NULL)
22139 {
22140 elf_section_list *next = filedata->symtab_shndx_list->next;
22141 free (filedata->symtab_shndx_list);
22142 filedata->symtab_shndx_list = next;
22143 }
22144
22145 free (filedata->section_headers_groups);
22146
22147 if (filedata->section_groups)
22148 {
22149 size_t i;
22150 struct group_list * g;
22151 struct group_list * next;
22152
22153 for (i = 0; i < filedata->group_count; i++)
22154 {
22155 for (g = filedata->section_groups [i].root; g != NULL; g = next)
22156 {
22157 next = g->next;
22158 free (g);
22159 }
22160 }
22161
22162 free (filedata->section_groups);
22163 }
22164 memset (&filedata->section_headers, 0,
22165 sizeof (Filedata) - offsetof (Filedata, section_headers));
22166 }
22167
22168 static void
22169 close_file (Filedata * filedata)
22170 {
22171 if (filedata)
22172 {
22173 if (filedata->handle)
22174 fclose (filedata->handle);
22175 free (filedata);
22176 }
22177 }
22178
22179 void
22180 close_debug_file (void * data)
22181 {
22182 free_filedata ((Filedata *) data);
22183 close_file ((Filedata *) data);
22184 }
22185
22186 static Filedata *
22187 open_file (const char * pathname, bool is_separate)
22188 {
22189 struct stat statbuf;
22190 Filedata * filedata = NULL;
22191
22192 if (stat (pathname, & statbuf) < 0
22193 || ! S_ISREG (statbuf.st_mode))
22194 goto fail;
22195
22196 filedata = calloc (1, sizeof * filedata);
22197 if (filedata == NULL)
22198 goto fail;
22199
22200 filedata->handle = fopen (pathname, "rb");
22201 if (filedata->handle == NULL)
22202 goto fail;
22203
22204 filedata->file_size = (bfd_size_type) statbuf.st_size;
22205 filedata->file_name = pathname;
22206 filedata->is_separate = is_separate;
22207
22208 if (! get_file_header (filedata))
22209 goto fail;
22210
22211 if (!get_section_headers (filedata, false))
22212 goto fail;
22213
22214 return filedata;
22215
22216 fail:
22217 if (filedata)
22218 {
22219 if (filedata->handle)
22220 fclose (filedata->handle);
22221 free (filedata);
22222 }
22223 return NULL;
22224 }
22225
22226 void *
22227 open_debug_file (const char * pathname)
22228 {
22229 return open_file (pathname, true);
22230 }
22231
22232 static void
22233 initialise_dump_sects (Filedata * filedata)
22234 {
22235 /* Initialise the dump_sects array from the cmdline_dump_sects array.
22236 Note we do this even if cmdline_dump_sects is empty because we
22237 must make sure that the dump_sets array is zeroed out before each
22238 object file is processed. */
22239 if (filedata->dump.num_dump_sects > cmdline.num_dump_sects)
22240 memset (filedata->dump.dump_sects, 0,
22241 filedata->dump.num_dump_sects * sizeof (*filedata->dump.dump_sects));
22242
22243 if (cmdline.num_dump_sects > 0)
22244 {
22245 if (filedata->dump.num_dump_sects == 0)
22246 /* A sneaky way of allocating the dump_sects array. */
22247 request_dump_bynumber (&filedata->dump, cmdline.num_dump_sects, 0);
22248
22249 assert (filedata->dump.num_dump_sects >= cmdline.num_dump_sects);
22250 memcpy (filedata->dump.dump_sects, cmdline.dump_sects,
22251 cmdline.num_dump_sects * sizeof (*filedata->dump.dump_sects));
22252 }
22253 }
22254
22255 /* Process one ELF object file according to the command line options.
22256 This file may actually be stored in an archive. The file is
22257 positioned at the start of the ELF object. Returns TRUE if no
22258 problems were encountered, FALSE otherwise. */
22259
22260 static bool
22261 process_object (Filedata * filedata)
22262 {
22263 bool have_separate_files;
22264 unsigned int i;
22265 bool res;
22266
22267 if (! get_file_header (filedata))
22268 {
22269 error (_("%s: Failed to read file header\n"), filedata->file_name);
22270 return false;
22271 }
22272
22273 /* Initialise per file variables. */
22274 for (i = ARRAY_SIZE (filedata->version_info); i--;)
22275 filedata->version_info[i] = 0;
22276
22277 for (i = ARRAY_SIZE (filedata->dynamic_info); i--;)
22278 filedata->dynamic_info[i] = 0;
22279 filedata->dynamic_info_DT_GNU_HASH = 0;
22280 filedata->dynamic_info_DT_MIPS_XHASH = 0;
22281
22282 /* Process the file. */
22283 if (show_name)
22284 printf (_("\nFile: %s\n"), filedata->file_name);
22285
22286 initialise_dump_sects (filedata);
22287
22288 /* There may be some extensions in the first section header. Don't
22289 bomb if we can't read it. */
22290 get_section_headers (filedata, true);
22291
22292 if (! process_file_header (filedata))
22293 {
22294 res = false;
22295 goto out;
22296 }
22297
22298 /* Throw away the single section header read above, so that we
22299 re-read the entire set. */
22300 free (filedata->section_headers);
22301 filedata->section_headers = NULL;
22302
22303 if (! process_section_headers (filedata))
22304 {
22305 /* Without loaded section headers we cannot process lots of things. */
22306 do_unwind = do_version = do_dump = do_arch = false;
22307
22308 if (! do_using_dynamic)
22309 do_syms = do_dyn_syms = do_reloc = false;
22310 }
22311
22312 if (! process_section_groups (filedata))
22313 /* Without loaded section groups we cannot process unwind. */
22314 do_unwind = false;
22315
22316 process_program_headers (filedata);
22317
22318 res = process_dynamic_section (filedata);
22319
22320 if (! process_relocs (filedata))
22321 res = false;
22322
22323 if (! process_unwind (filedata))
22324 res = false;
22325
22326 if (! process_symbol_table (filedata))
22327 res = false;
22328
22329 if (! process_lto_symbol_tables (filedata))
22330 res = false;
22331
22332 if (! process_syminfo (filedata))
22333 res = false;
22334
22335 if (! process_version_sections (filedata))
22336 res = false;
22337
22338 if (filedata->file_header.e_shstrndx != SHN_UNDEF)
22339 have_separate_files = load_separate_debug_files (filedata, filedata->file_name);
22340 else
22341 have_separate_files = false;
22342
22343 if (! process_section_contents (filedata))
22344 res = false;
22345
22346 if (have_separate_files)
22347 {
22348 separate_info * d;
22349
22350 for (d = first_separate_info; d != NULL; d = d->next)
22351 {
22352 initialise_dump_sects (d->handle);
22353
22354 if (process_links && ! process_file_header (d->handle))
22355 res = false;
22356 else if (! process_section_headers (d->handle))
22357 res = false;
22358 else if (! process_section_contents (d->handle))
22359 res = false;
22360 else if (process_links)
22361 {
22362 if (! process_section_groups (d->handle))
22363 res = false;
22364 process_program_headers (d->handle);
22365 if (! process_dynamic_section (d->handle))
22366 res = false;
22367 if (! process_relocs (d->handle))
22368 res = false;
22369 if (! process_unwind (d->handle))
22370 res = false;
22371 if (! process_symbol_table (d->handle))
22372 res = false;
22373 if (! process_lto_symbol_tables (d->handle))
22374 res = false;
22375 if (! process_syminfo (d->handle))
22376 res = false;
22377 if (! process_version_sections (d->handle))
22378 res = false;
22379 if (! process_notes (d->handle))
22380 res = false;
22381 }
22382 }
22383
22384 /* The file handles are closed by the call to free_debug_memory() below. */
22385 }
22386
22387 if (! process_notes (filedata))
22388 res = false;
22389
22390 if (! process_gnu_liblist (filedata))
22391 res = false;
22392
22393 if (! process_arch_specific (filedata))
22394 res = false;
22395
22396 out:
22397 free_filedata (filedata);
22398
22399 free_debug_memory ();
22400
22401 return res;
22402 }
22403
22404 /* Process an ELF archive.
22405 On entry the file is positioned just after the ARMAG string.
22406 Returns TRUE upon success, FALSE otherwise. */
22407
22408 static bool
22409 process_archive (Filedata * filedata, bool is_thin_archive)
22410 {
22411 struct archive_info arch;
22412 struct archive_info nested_arch;
22413 size_t got;
22414 bool ret = true;
22415
22416 show_name = true;
22417
22418 /* The ARCH structure is used to hold information about this archive. */
22419 arch.file_name = NULL;
22420 arch.file = NULL;
22421 arch.index_array = NULL;
22422 arch.sym_table = NULL;
22423 arch.longnames = NULL;
22424
22425 /* The NESTED_ARCH structure is used as a single-item cache of information
22426 about a nested archive (when members of a thin archive reside within
22427 another regular archive file). */
22428 nested_arch.file_name = NULL;
22429 nested_arch.file = NULL;
22430 nested_arch.index_array = NULL;
22431 nested_arch.sym_table = NULL;
22432 nested_arch.longnames = NULL;
22433
22434 if (setup_archive (&arch, filedata->file_name, filedata->handle,
22435 filedata->file_size, is_thin_archive,
22436 do_archive_index) != 0)
22437 {
22438 ret = false;
22439 goto out;
22440 }
22441
22442 if (do_archive_index)
22443 {
22444 if (arch.sym_table == NULL)
22445 error (_("%s: unable to dump the index as none was found\n"),
22446 filedata->file_name);
22447 else
22448 {
22449 unsigned long i, l;
22450 unsigned long current_pos;
22451
22452 printf (_("Index of archive %s: (%lu entries, 0x%lx bytes "
22453 "in the symbol table)\n"),
22454 filedata->file_name, (unsigned long) arch.index_num,
22455 arch.sym_size);
22456
22457 current_pos = ftell (filedata->handle);
22458
22459 for (i = l = 0; i < arch.index_num; i++)
22460 {
22461 if (i == 0
22462 || (i > 0 && arch.index_array[i] != arch.index_array[i - 1]))
22463 {
22464 char * member_name
22465 = get_archive_member_name_at (&arch, arch.index_array[i],
22466 &nested_arch);
22467
22468 if (member_name != NULL)
22469 {
22470 char * qualified_name
22471 = make_qualified_name (&arch, &nested_arch,
22472 member_name);
22473
22474 if (qualified_name != NULL)
22475 {
22476 printf (_("Contents of binary %s at offset "),
22477 qualified_name);
22478 (void) print_vma (arch.index_array[i], PREFIX_HEX);
22479 putchar ('\n');
22480 free (qualified_name);
22481 }
22482 free (member_name);
22483 }
22484 }
22485
22486 if (l >= arch.sym_size)
22487 {
22488 error (_("%s: end of the symbol table reached "
22489 "before the end of the index\n"),
22490 filedata->file_name);
22491 ret = false;
22492 break;
22493 }
22494 /* PR 17531: file: 0b6630b2. */
22495 printf ("\t%.*s\n",
22496 (int) (arch.sym_size - l), arch.sym_table + l);
22497 l += strnlen (arch.sym_table + l, arch.sym_size - l) + 1;
22498 }
22499
22500 if (arch.uses_64bit_indices)
22501 l = (l + 7) & ~ 7;
22502 else
22503 l += l & 1;
22504
22505 if (l < arch.sym_size)
22506 {
22507 error (ngettext ("%s: %ld byte remains in the symbol table, "
22508 "but without corresponding entries in "
22509 "the index table\n",
22510 "%s: %ld bytes remain in the symbol table, "
22511 "but without corresponding entries in "
22512 "the index table\n",
22513 arch.sym_size - l),
22514 filedata->file_name, arch.sym_size - l);
22515 ret = false;
22516 }
22517
22518 if (fseek (filedata->handle, current_pos, SEEK_SET) != 0)
22519 {
22520 error (_("%s: failed to seek back to start of object files "
22521 "in the archive\n"),
22522 filedata->file_name);
22523 ret = false;
22524 goto out;
22525 }
22526 }
22527
22528 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
22529 && !do_segments && !do_header && !do_dump && !do_version
22530 && !do_histogram && !do_debugging && !do_arch && !do_notes
22531 && !do_section_groups && !do_dyn_syms)
22532 {
22533 ret = true; /* Archive index only. */
22534 goto out;
22535 }
22536 }
22537
22538 while (1)
22539 {
22540 char * name;
22541 size_t namelen;
22542 char * qualified_name;
22543
22544 /* Read the next archive header. */
22545 if (fseek (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0)
22546 {
22547 error (_("%s: failed to seek to next archive header\n"),
22548 arch.file_name);
22549 ret = false;
22550 break;
22551 }
22552 got = fread (&arch.arhdr, 1, sizeof arch.arhdr, filedata->handle);
22553 if (got != sizeof arch.arhdr)
22554 {
22555 if (got == 0)
22556 break;
22557 /* PR 24049 - we cannot use filedata->file_name as this will
22558 have already been freed. */
22559 error (_("%s: failed to read archive header\n"), arch.file_name);
22560
22561 ret = false;
22562 break;
22563 }
22564 if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
22565 {
22566 error (_("%s: did not find a valid archive header\n"),
22567 arch.file_name);
22568 ret = false;
22569 break;
22570 }
22571
22572 arch.next_arhdr_offset += sizeof arch.arhdr;
22573
22574 filedata->archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
22575
22576 name = get_archive_member_name (&arch, &nested_arch);
22577 if (name == NULL)
22578 {
22579 error (_("%s: bad archive file name\n"), arch.file_name);
22580 ret = false;
22581 break;
22582 }
22583 namelen = strlen (name);
22584
22585 qualified_name = make_qualified_name (&arch, &nested_arch, name);
22586 if (qualified_name == NULL)
22587 {
22588 error (_("%s: bad archive file name\n"), arch.file_name);
22589 free (name);
22590 ret = false;
22591 break;
22592 }
22593
22594 if (is_thin_archive && arch.nested_member_origin == 0)
22595 {
22596 /* This is a proxy for an external member of a thin archive. */
22597 Filedata * member_filedata;
22598 char * member_file_name = adjust_relative_path
22599 (filedata->file_name, name, namelen);
22600
22601 free (name);
22602 if (member_file_name == NULL)
22603 {
22604 free (qualified_name);
22605 ret = false;
22606 break;
22607 }
22608
22609 member_filedata = open_file (member_file_name, false);
22610 if (member_filedata == NULL)
22611 {
22612 error (_("Input file '%s' is not readable.\n"), member_file_name);
22613 free (member_file_name);
22614 free (qualified_name);
22615 ret = false;
22616 break;
22617 }
22618
22619 filedata->archive_file_offset = arch.nested_member_origin;
22620 member_filedata->file_name = qualified_name;
22621
22622 /* The call to process_object() expects the file to be at the beginning. */
22623 rewind (member_filedata->handle);
22624
22625 if (! process_object (member_filedata))
22626 ret = false;
22627
22628 close_file (member_filedata);
22629 free (member_file_name);
22630 }
22631 else if (is_thin_archive)
22632 {
22633 Filedata thin_filedata;
22634
22635 memset (&thin_filedata, 0, sizeof (thin_filedata));
22636
22637 /* PR 15140: Allow for corrupt thin archives. */
22638 if (nested_arch.file == NULL)
22639 {
22640 error (_("%s: contains corrupt thin archive: %s\n"),
22641 qualified_name, name);
22642 free (qualified_name);
22643 free (name);
22644 ret = false;
22645 break;
22646 }
22647 free (name);
22648
22649 /* This is a proxy for a member of a nested archive. */
22650 filedata->archive_file_offset
22651 = arch.nested_member_origin + sizeof arch.arhdr;
22652
22653 /* The nested archive file will have been opened and setup by
22654 get_archive_member_name. */
22655 if (fseek (nested_arch.file, filedata->archive_file_offset,
22656 SEEK_SET) != 0)
22657 {
22658 error (_("%s: failed to seek to archive member.\n"),
22659 nested_arch.file_name);
22660 free (qualified_name);
22661 ret = false;
22662 break;
22663 }
22664
22665 thin_filedata.handle = nested_arch.file;
22666 thin_filedata.file_name = qualified_name;
22667
22668 if (! process_object (& thin_filedata))
22669 ret = false;
22670 }
22671 else
22672 {
22673 free (name);
22674 filedata->archive_file_offset = arch.next_arhdr_offset;
22675 filedata->file_name = qualified_name;
22676 if (! process_object (filedata))
22677 ret = false;
22678 arch.next_arhdr_offset += (filedata->archive_file_size + 1) & -2;
22679 /* Stop looping with "negative" archive_file_size. */
22680 if (arch.next_arhdr_offset < filedata->archive_file_size)
22681 arch.next_arhdr_offset = -1ul;
22682 }
22683
22684 free (qualified_name);
22685 }
22686
22687 out:
22688 if (nested_arch.file != NULL)
22689 fclose (nested_arch.file);
22690 release_archive (&nested_arch);
22691 release_archive (&arch);
22692
22693 return ret;
22694 }
22695
22696 static bool
22697 process_file (char * file_name)
22698 {
22699 Filedata * filedata = NULL;
22700 struct stat statbuf;
22701 char armag[SARMAG];
22702 bool ret = true;
22703
22704 if (stat (file_name, &statbuf) < 0)
22705 {
22706 if (errno == ENOENT)
22707 error (_("'%s': No such file\n"), file_name);
22708 else
22709 error (_("Could not locate '%s'. System error message: %s\n"),
22710 file_name, strerror (errno));
22711 return false;
22712 }
22713
22714 if (! S_ISREG (statbuf.st_mode))
22715 {
22716 error (_("'%s' is not an ordinary file\n"), file_name);
22717 return false;
22718 }
22719
22720 filedata = calloc (1, sizeof * filedata);
22721 if (filedata == NULL)
22722 {
22723 error (_("Out of memory allocating file data structure\n"));
22724 return false;
22725 }
22726
22727 filedata->file_name = file_name;
22728 filedata->handle = fopen (file_name, "rb");
22729 if (filedata->handle == NULL)
22730 {
22731 error (_("Input file '%s' is not readable.\n"), file_name);
22732 free (filedata);
22733 return false;
22734 }
22735
22736 if (fread (armag, SARMAG, 1, filedata->handle) != 1)
22737 {
22738 error (_("%s: Failed to read file's magic number\n"), file_name);
22739 fclose (filedata->handle);
22740 free (filedata);
22741 return false;
22742 }
22743
22744 filedata->file_size = (bfd_size_type) statbuf.st_size;
22745 filedata->is_separate = false;
22746
22747 if (memcmp (armag, ARMAG, SARMAG) == 0)
22748 {
22749 if (! process_archive (filedata, false))
22750 ret = false;
22751 }
22752 else if (memcmp (armag, ARMAGT, SARMAG) == 0)
22753 {
22754 if ( ! process_archive (filedata, true))
22755 ret = false;
22756 }
22757 else
22758 {
22759 if (do_archive_index && !check_all)
22760 error (_("File %s is not an archive so its index cannot be displayed.\n"),
22761 file_name);
22762
22763 rewind (filedata->handle);
22764 filedata->archive_file_size = filedata->archive_file_offset = 0;
22765
22766 if (! process_object (filedata))
22767 ret = false;
22768 }
22769
22770 fclose (filedata->handle);
22771 free (filedata->section_headers);
22772 free (filedata->program_headers);
22773 free (filedata->string_table);
22774 free (filedata->dump.dump_sects);
22775 free (filedata);
22776
22777 free (ba_cache.strtab);
22778 ba_cache.strtab = NULL;
22779 free (ba_cache.symtab);
22780 ba_cache.symtab = NULL;
22781 ba_cache.filedata = NULL;
22782
22783 return ret;
22784 }
22785
22786 #ifdef SUPPORT_DISASSEMBLY
22787 /* Needed by the i386 disassembler. For extra credit, someone could
22788 fix this so that we insert symbolic addresses here, esp for GOT/PLT
22789 symbols. */
22790
22791 void
22792 print_address (unsigned int addr, FILE * outfile)
22793 {
22794 fprintf (outfile,"0x%8.8x", addr);
22795 }
22796
22797 /* Needed by the i386 disassembler. */
22798
22799 void
22800 db_task_printsym (unsigned int addr)
22801 {
22802 print_address (addr, stderr);
22803 }
22804 #endif
22805
22806 int
22807 main (int argc, char ** argv)
22808 {
22809 int err;
22810
22811 #ifdef HAVE_LC_MESSAGES
22812 setlocale (LC_MESSAGES, "");
22813 #endif
22814 setlocale (LC_CTYPE, "");
22815 bindtextdomain (PACKAGE, LOCALEDIR);
22816 textdomain (PACKAGE);
22817
22818 expandargv (&argc, &argv);
22819
22820 parse_args (& cmdline, argc, argv);
22821
22822 if (optind < (argc - 1))
22823 /* When displaying information for more than one file,
22824 prefix the information with the file name. */
22825 show_name = true;
22826 else if (optind >= argc)
22827 {
22828 /* Ensure that the warning is always displayed. */
22829 do_checks = true;
22830
22831 warn (_("Nothing to do.\n"));
22832 usage (stderr);
22833 }
22834
22835 err = false;
22836 while (optind < argc)
22837 if (! process_file (argv[optind++]))
22838 err = true;
22839
22840 free (cmdline.dump_sects);
22841
22842 free (dump_ctf_symtab_name);
22843 free (dump_ctf_strtab_name);
22844 free (dump_ctf_parent_name);
22845
22846 return err ? EXIT_FAILURE : EXIT_SUCCESS;
22847 }