]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - binutils/readelf.c
Get rid of fprintf_vma and sprintf_vma
[thirdparty/binutils-gdb.git] / binutils / readelf.c
1 /* readelf.c -- display contents of an ELF format file
2 Copyright (C) 1998-2022 Free Software Foundation, Inc.
3
4 Originally developed by Eric Youngdale <eric@andante.jic.com>
5 Modifications by Nick Clifton <nickc@redhat.com>
6
7 This file is part of GNU Binutils.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
22 02110-1301, USA. */
23 \f
24 /* The difference between readelf and objdump:
25
26 Both programs are capable of displaying the contents of ELF format files,
27 so why does the binutils project have two file dumpers ?
28
29 The reason is that objdump sees an ELF file through a BFD filter of the
30 world; if BFD has a bug where, say, it disagrees about a machine constant
31 in e_flags, then the odds are good that it will remain internally
32 consistent. The linker sees it the BFD way, objdump sees it the BFD way,
33 GAS sees it the BFD way. There was need for a tool to go find out what
34 the file actually says.
35
36 This is why the readelf program does not link against the BFD library - it
37 exists as an independent program to help verify the correct working of BFD.
38
39 There is also the case that readelf can provide more information about an
40 ELF file than is provided by objdump. In particular it can display DWARF
41 debugging information which (at the moment) objdump cannot. */
42 \f
43 #include "sysdep.h"
44 #include <assert.h>
45 #include <time.h>
46 #include <zlib.h>
47 #include <wchar.h>
48
49 #if defined HAVE_MSGPACK
50 #include <msgpack.h>
51 #endif
52
53 #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/xgate.h"
167 #include "elf/xstormy16.h"
168 #include "elf/xtensa.h"
169 #include "elf/z80.h"
170 #include "elf/loongarch.h"
171
172 #include "getopt.h"
173 #include "libiberty.h"
174 #include "safe-ctype.h"
175 #include "filenames.h"
176
177 #ifndef offsetof
178 #define offsetof(TYPE, MEMBER) ((size_t) &(((TYPE *) 0)->MEMBER))
179 #endif
180
181 typedef struct elf_section_list
182 {
183 Elf_Internal_Shdr * hdr;
184 struct elf_section_list * next;
185 } elf_section_list;
186
187 /* Flag bits indicating particular types of dump. */
188 #define HEX_DUMP (1 << 0) /* The -x command line switch. */
189 #define DISASS_DUMP (1 << 1) /* The -i command line switch. */
190 #define DEBUG_DUMP (1 << 2) /* The -w command line switch. */
191 #define STRING_DUMP (1 << 3) /* The -p command line switch. */
192 #define RELOC_DUMP (1 << 4) /* The -R command line switch. */
193 #define CTF_DUMP (1 << 5) /* The --ctf command line switch. */
194
195 typedef unsigned char dump_type;
196
197 /* A linked list of the section names for which dumps were requested. */
198 struct dump_list_entry
199 {
200 char * name;
201 dump_type type;
202 struct dump_list_entry * next;
203 };
204
205 /* A dynamic array of flags indicating for which sections a dump
206 has been requested via command line switches. */
207 struct dump_data
208 {
209 dump_type * dump_sects;
210 unsigned int num_dump_sects;
211 };
212
213 static struct dump_data cmdline;
214
215 static struct dump_list_entry * dump_sects_byname;
216
217 char * program_name = "readelf";
218
219 static bool show_name = false;
220 static bool do_dynamic = false;
221 static bool do_syms = false;
222 static bool do_dyn_syms = false;
223 static bool do_lto_syms = false;
224 static bool do_reloc = false;
225 static bool do_sections = false;
226 static bool do_section_groups = false;
227 static bool do_section_details = false;
228 static bool do_segments = false;
229 static bool do_unwind = false;
230 static bool do_using_dynamic = false;
231 static bool do_header = false;
232 static bool do_dump = false;
233 static bool do_version = false;
234 static bool do_histogram = false;
235 static bool do_debugging = false;
236 static bool do_ctf = false;
237 static bool do_arch = false;
238 static bool do_notes = false;
239 static bool do_archive_index = false;
240 static bool check_all = false;
241 static bool is_32bit_elf = false;
242 static bool decompress_dumps = false;
243 static bool do_not_show_symbol_truncation = false;
244 static bool do_demangle = false; /* Pretty print C++ symbol names. */
245 static bool process_links = false;
246 static bool dump_any_debugging = false;
247 static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
248 static int sym_base = 0;
249
250 static char *dump_ctf_parent_name;
251 static char *dump_ctf_symtab_name;
252 static char *dump_ctf_strtab_name;
253
254 struct group_list
255 {
256 struct group_list * next;
257 unsigned int section_index;
258 };
259
260 struct group
261 {
262 struct group_list * root;
263 unsigned int group_index;
264 };
265
266 typedef struct filedata
267 {
268 const char * file_name;
269 bool is_separate;
270 FILE * handle;
271 bfd_size_type file_size;
272 Elf_Internal_Ehdr file_header;
273 unsigned long archive_file_offset;
274 unsigned long archive_file_size;
275 /* Everything below this point is cleared out by free_filedata. */
276 Elf_Internal_Shdr * section_headers;
277 Elf_Internal_Phdr * program_headers;
278 char * string_table;
279 unsigned long string_table_length;
280 unsigned long dynamic_addr;
281 bfd_size_type dynamic_size;
282 size_t dynamic_nent;
283 Elf_Internal_Dyn * dynamic_section;
284 Elf_Internal_Shdr * dynamic_strtab_section;
285 char * dynamic_strings;
286 unsigned long dynamic_strings_length;
287 Elf_Internal_Shdr * dynamic_symtab_section;
288 unsigned long num_dynamic_syms;
289 Elf_Internal_Sym * dynamic_symbols;
290 bfd_vma version_info[16];
291 unsigned int dynamic_syminfo_nent;
292 Elf_Internal_Syminfo * dynamic_syminfo;
293 unsigned long dynamic_syminfo_offset;
294 bfd_size_type nbuckets;
295 bfd_size_type nchains;
296 bfd_vma * buckets;
297 bfd_vma * chains;
298 bfd_size_type ngnubuckets;
299 bfd_size_type ngnuchains;
300 bfd_vma * gnubuckets;
301 bfd_vma * gnuchains;
302 bfd_vma * mipsxlat;
303 bfd_vma gnusymidx;
304 char * program_interpreter;
305 bfd_vma dynamic_info[DT_ENCODING];
306 bfd_vma dynamic_info_DT_GNU_HASH;
307 bfd_vma dynamic_info_DT_MIPS_XHASH;
308 elf_section_list * symtab_shndx_list;
309 size_t group_count;
310 struct group * section_groups;
311 struct group ** section_headers_groups;
312 /* A dynamic array of flags indicating for which sections a dump of
313 some kind has been requested. It is reset on a per-object file
314 basis and then initialised from the cmdline_dump_sects array,
315 the results of interpreting the -w switch, and the
316 dump_sects_byname list. */
317 struct dump_data dump;
318 } Filedata;
319
320 /* How to print a vma value. */
321 typedef enum print_mode
322 {
323 HEX,
324 HEX_5,
325 DEC,
326 DEC_5,
327 UNSIGNED,
328 UNSIGNED_5,
329 PREFIX_HEX,
330 PREFIX_HEX_5,
331 FULL_HEX,
332 LONG_HEX,
333 OCTAL,
334 OCTAL_5
335 }
336 print_mode;
337
338 typedef enum unicode_display_type
339 {
340 unicode_default = 0,
341 unicode_locale,
342 unicode_escape,
343 unicode_hex,
344 unicode_highlight,
345 unicode_invalid
346 } unicode_display_type;
347
348 static unicode_display_type unicode_display = unicode_default;
349
350 typedef enum
351 {
352 reltype_unknown,
353 reltype_rel,
354 reltype_rela,
355 reltype_relr
356 } relocation_type;
357
358 /* Versioned symbol info. */
359 enum versioned_symbol_info
360 {
361 symbol_undefined,
362 symbol_hidden,
363 symbol_public
364 };
365
366 static const char * get_symbol_version_string
367 (Filedata *, bool, const char *, unsigned long, unsigned,
368 Elf_Internal_Sym *, enum versioned_symbol_info *, unsigned short *);
369
370 #define UNKNOWN -1
371
372 static inline const char *
373 section_name (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
374 {
375 return filedata->string_table + hdr->sh_name;
376 }
377
378 static inline bool
379 section_name_valid (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
380 {
381 return (hdr != NULL
382 && filedata->string_table != NULL
383 && hdr->sh_name < filedata->string_table_length);
384 }
385
386 static inline const char *
387 section_name_print (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
388 {
389 if (hdr == NULL)
390 return _("<none>");
391 if (filedata->string_table == NULL)
392 return _("<no-strings>");
393 if (hdr->sh_name >= filedata->string_table_length)
394 return _("<corrupt>");
395 return section_name (filedata, hdr);
396 }
397
398 #define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */
399
400 static inline bool
401 valid_symbol_name (const char *strtab, size_t strtab_size, uint64_t offset)
402 {
403 return strtab != NULL && offset < strtab_size;
404 }
405
406 static inline bool
407 valid_dynamic_name (const Filedata *filedata, uint64_t offset)
408 {
409 return valid_symbol_name (filedata->dynamic_strings,
410 filedata->dynamic_strings_length, offset);
411 }
412
413 /* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
414 already been called and verified that the string exists. */
415 static inline const char *
416 get_dynamic_name (const Filedata *filedata, size_t offset)
417 {
418 return filedata->dynamic_strings + offset;
419 }
420
421 #define REMOVE_ARCH_BITS(ADDR) \
422 do \
423 { \
424 if (filedata->file_header.e_machine == EM_ARM) \
425 (ADDR) &= ~1; \
426 } \
427 while (0)
428
429 /* Get the correct GNU hash section name. */
430 #define GNU_HASH_SECTION_NAME(filedata) \
431 filedata->dynamic_info_DT_MIPS_XHASH ? ".MIPS.xhash" : ".gnu.hash"
432 \f
433 /* Print a BFD_VMA to an internal buffer, for use in error messages.
434 BFD_FMA_FMT can't be used in translated strings. */
435
436 static const char *
437 bfd_vmatoa (char *fmtch, bfd_vma value)
438 {
439 /* bfd_vmatoa is used more then once in a printf call for output.
440 Cycle through an array of buffers. */
441 static int buf_pos = 0;
442 static struct bfd_vmatoa_buf
443 {
444 char place[64];
445 } buf[4];
446 char *ret;
447 char fmt[32];
448
449 ret = buf[buf_pos++].place;
450 buf_pos %= ARRAY_SIZE (buf);
451
452 sprintf (fmt, "%%%s%s", BFD_VMA_FMT, fmtch);
453 snprintf (ret, sizeof (buf[0].place), fmt, value);
454 return ret;
455 }
456
457 /* Retrieve NMEMB structures, each SIZE bytes long from FILEDATA starting at
458 OFFSET + the offset of the current archive member, if we are examining an
459 archive. Put the retrieved data into VAR, if it is not NULL. Otherwise
460 allocate a buffer using malloc and fill that. In either case return the
461 pointer to the start of the retrieved data or NULL if something went wrong.
462 If something does go wrong and REASON is not NULL then emit an error
463 message using REASON as part of the context. */
464
465 static void *
466 get_data (void * var,
467 Filedata * filedata,
468 unsigned long offset,
469 bfd_size_type size,
470 bfd_size_type nmemb,
471 const char * reason)
472 {
473 void * mvar;
474 bfd_size_type amt = size * nmemb;
475
476 if (size == 0 || nmemb == 0)
477 return NULL;
478
479 /* If the size_t type is smaller than the bfd_size_type, eg because
480 you are building a 32-bit tool on a 64-bit host, then make sure
481 that when the sizes are cast to (size_t) no information is lost. */
482 if ((size_t) size != size
483 || (size_t) nmemb != nmemb
484 || (size_t) amt != amt)
485 {
486 if (reason)
487 error (_("Size truncation prevents reading %s"
488 " elements of size %s for %s\n"),
489 bfd_vmatoa ("u", nmemb), bfd_vmatoa ("u", size), reason);
490 return NULL;
491 }
492
493 /* Check for size overflow. */
494 if (amt / size != nmemb || (size_t) amt + 1 == 0)
495 {
496 if (reason)
497 error (_("Size overflow prevents reading %s"
498 " elements of size %s for %s\n"),
499 bfd_vmatoa ("u", nmemb), bfd_vmatoa ("u", size), reason);
500 return NULL;
501 }
502
503 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
504 attempting to allocate memory when the read is bound to fail. */
505 if (filedata->archive_file_offset > filedata->file_size
506 || offset > filedata->file_size - filedata->archive_file_offset
507 || amt > filedata->file_size - filedata->archive_file_offset - offset)
508 {
509 if (reason)
510 error (_("Reading %s bytes extends past end of file for %s\n"),
511 bfd_vmatoa ("u", amt), reason);
512 return NULL;
513 }
514
515 if (fseek (filedata->handle, filedata->archive_file_offset + offset,
516 SEEK_SET))
517 {
518 if (reason)
519 error (_("Unable to seek to 0x%lx for %s\n"),
520 filedata->archive_file_offset + offset, reason);
521 return NULL;
522 }
523
524 mvar = var;
525 if (mvar == NULL)
526 {
527 /* + 1 so that we can '\0' terminate invalid string table sections. */
528 mvar = malloc ((size_t) amt + 1);
529
530 if (mvar == NULL)
531 {
532 if (reason)
533 error (_("Out of memory allocating %s bytes for %s\n"),
534 bfd_vmatoa ("u", amt), reason);
535 return NULL;
536 }
537
538 ((char *) mvar)[amt] = '\0';
539 }
540
541 if (fread (mvar, (size_t) size, (size_t) nmemb, filedata->handle) != nmemb)
542 {
543 if (reason)
544 error (_("Unable to read in %s bytes of %s\n"),
545 bfd_vmatoa ("u", amt), reason);
546 if (mvar != var)
547 free (mvar);
548 return NULL;
549 }
550
551 return mvar;
552 }
553
554 /* Print a VMA value in the MODE specified.
555 Returns the number of characters displayed. */
556
557 static unsigned int
558 print_vma (bfd_vma vma, print_mode mode)
559 {
560 unsigned int nc = 0;
561
562 switch (mode)
563 {
564 case FULL_HEX:
565 nc = printf ("0x");
566 /* Fall through. */
567 case LONG_HEX:
568 #ifdef BFD64
569 if (!is_32bit_elf)
570 return nc + printf ("%16.16" PRIx64, (uint64_t) vma);
571 #endif
572 return nc + printf ("%8.8" PRIx64, (uint64_t) vma);
573
574 case DEC_5:
575 if (vma <= 99999)
576 return printf ("%5" BFD_VMA_FMT "d", vma);
577 /* Fall through. */
578 case PREFIX_HEX:
579 nc = printf ("0x");
580 /* Fall through. */
581 case HEX:
582 return nc + printf ("%" BFD_VMA_FMT "x", vma);
583
584 case PREFIX_HEX_5:
585 nc = printf ("0x");
586 /* Fall through. */
587 case HEX_5:
588 return nc + printf ("%05" BFD_VMA_FMT "x", vma);
589
590 case DEC:
591 return printf ("%" BFD_VMA_FMT "d", vma);
592
593 case UNSIGNED:
594 return printf ("%" BFD_VMA_FMT "u", vma);
595
596 case UNSIGNED_5:
597 return printf ("%5" BFD_VMA_FMT "u", vma);
598
599 case OCTAL:
600 return printf ("%" BFD_VMA_FMT "o", vma);
601
602 case OCTAL_5:
603 return printf ("%5" BFD_VMA_FMT "o", vma);
604
605 default:
606 /* FIXME: Report unrecognised mode ? */
607 return 0;
608 }
609 }
610
611
612 /* Display a symbol on stdout. Handles the display of control characters and
613 multibye characters (assuming the host environment supports them).
614
615 Display at most abs(WIDTH) characters, truncating as necessary, unless do_wide is true.
616
617 If truncation will happen and do_not_show_symbol_truncation is FALSE then display
618 abs(WIDTH) - 5 characters followed by "[...]".
619
620 If WIDTH is negative then ensure that the output is at least (- WIDTH) characters,
621 padding as necessary.
622
623 Returns the number of emitted characters. */
624
625 static unsigned int
626 print_symbol (signed int width, const char * symbol)
627 {
628 bool extra_padding = false;
629 bool do_dots = false;
630 signed int num_printed = 0;
631 #ifdef HAVE_MBSTATE_T
632 mbstate_t state;
633 #endif
634 unsigned int width_remaining;
635 const void * alloced_symbol = NULL;
636
637 if (width < 0)
638 {
639 /* Keep the width positive. This helps the code below. */
640 width = - width;
641 extra_padding = true;
642 }
643 else if (width == 0)
644 return 0;
645
646 if (do_wide)
647 /* Set the remaining width to a very large value.
648 This simplifies the code below. */
649 width_remaining = INT_MAX;
650 else
651 {
652 width_remaining = width;
653 if (! do_not_show_symbol_truncation
654 && (int) strlen (symbol) > width)
655 {
656 width_remaining -= 5;
657 if ((int) width_remaining < 0)
658 width_remaining = 0;
659 do_dots = true;
660 }
661 }
662
663 #ifdef HAVE_MBSTATE_T
664 /* Initialise the multibyte conversion state. */
665 memset (& state, 0, sizeof (state));
666 #endif
667
668 if (do_demangle && *symbol)
669 {
670 const char * res = cplus_demangle (symbol, demangle_flags);
671
672 if (res != NULL)
673 alloced_symbol = symbol = res;
674 }
675
676 while (width_remaining)
677 {
678 size_t n;
679 const char c = *symbol++;
680
681 if (c == 0)
682 break;
683
684 if (ISPRINT (c))
685 {
686 putchar (c);
687 width_remaining --;
688 num_printed ++;
689 }
690 else if (ISCNTRL (c))
691 {
692 /* Do not print control characters directly as they can affect terminal
693 settings. Such characters usually appear in the names generated
694 by the assembler for local labels. */
695
696 if (width_remaining < 2)
697 break;
698
699 printf ("^%c", c + 0x40);
700 width_remaining -= 2;
701 num_printed += 2;
702 }
703 else if (c == 0x7f)
704 {
705 if (width_remaining < 5)
706 break;
707 printf ("<DEL>");
708 width_remaining -= 5;
709 num_printed += 5;
710 }
711 else if (unicode_display != unicode_locale
712 && unicode_display != unicode_default)
713 {
714 /* Display unicode characters as something else. */
715 unsigned char bytes[4];
716 bool is_utf8;
717 unsigned int nbytes;
718
719 bytes[0] = c;
720
721 if (bytes[0] < 0xc0)
722 {
723 nbytes = 1;
724 is_utf8 = false;
725 }
726 else
727 {
728 bytes[1] = *symbol++;
729
730 if ((bytes[1] & 0xc0) != 0x80)
731 {
732 is_utf8 = false;
733 /* Do not consume this character. It may only
734 be the first byte in the sequence that was
735 corrupt. */
736 --symbol;
737 nbytes = 1;
738 }
739 else if ((bytes[0] & 0x20) == 0)
740 {
741 is_utf8 = true;
742 nbytes = 2;
743 }
744 else
745 {
746 bytes[2] = *symbol++;
747
748 if ((bytes[2] & 0xc0) != 0x80)
749 {
750 is_utf8 = false;
751 symbol -= 2;
752 nbytes = 1;
753 }
754 else if ((bytes[0] & 0x10) == 0)
755 {
756 is_utf8 = true;
757 nbytes = 3;
758 }
759 else
760 {
761 bytes[3] = *symbol++;
762
763 nbytes = 4;
764
765 if ((bytes[3] & 0xc0) != 0x80)
766 {
767 is_utf8 = false;
768 symbol -= 3;
769 nbytes = 1;
770 }
771 else
772 is_utf8 = true;
773 }
774 }
775 }
776
777 if (unicode_display == unicode_invalid)
778 is_utf8 = false;
779
780 if (unicode_display == unicode_hex || ! is_utf8)
781 {
782 unsigned int i;
783
784 if (width_remaining < (nbytes * 2) + 2)
785 break;
786
787 putchar (is_utf8 ? '<' : '{');
788 printf ("0x");
789 for (i = 0; i < nbytes; i++)
790 printf ("%02x", bytes[i]);
791 putchar (is_utf8 ? '>' : '}');
792 }
793 else
794 {
795 if (unicode_display == unicode_highlight && isatty (1))
796 printf ("\x1B[31;47m"); /* Red. */
797
798 switch (nbytes)
799 {
800 case 2:
801 if (width_remaining < 6)
802 break;
803 printf ("\\u%02x%02x",
804 (bytes[0] & 0x1c) >> 2,
805 ((bytes[0] & 0x03) << 6) | (bytes[1] & 0x3f));
806 break;
807 case 3:
808 if (width_remaining < 6)
809 break;
810 printf ("\\u%02x%02x",
811 ((bytes[0] & 0x0f) << 4) | ((bytes[1] & 0x3c) >> 2),
812 ((bytes[1] & 0x03) << 6) | (bytes[2] & 0x3f));
813 break;
814 case 4:
815 if (width_remaining < 8)
816 break;
817 printf ("\\u%02x%02x%02x",
818 ((bytes[0] & 0x07) << 6) | ((bytes[1] & 0x3c) >> 2),
819 ((bytes[1] & 0x03) << 6) | ((bytes[2] & 0x3c) >> 2),
820 ((bytes[2] & 0x03) << 6) | (bytes[3] & 0x3f));
821
822 break;
823 default:
824 /* URG. */
825 break;
826 }
827
828 if (unicode_display == unicode_highlight && isatty (1))
829 printf ("\033[0m"); /* Default colour. */
830 }
831
832 if (bytes[nbytes - 1] == 0)
833 break;
834 }
835 else
836 {
837 #ifdef HAVE_MBSTATE_T
838 wchar_t w;
839 #endif
840 /* Let printf do the hard work of displaying multibyte characters. */
841 printf ("%.1s", symbol - 1);
842 width_remaining --;
843 num_printed ++;
844
845 #ifdef HAVE_MBSTATE_T
846 /* Try to find out how many bytes made up the character that was
847 just printed. Advance the symbol pointer past the bytes that
848 were displayed. */
849 n = mbrtowc (& w, symbol - 1, MB_CUR_MAX, & state);
850 #else
851 n = 1;
852 #endif
853 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
854 symbol += (n - 1);
855 }
856 }
857
858 if (do_dots)
859 num_printed += printf ("[...]");
860
861 if (extra_padding && num_printed < width)
862 {
863 /* Fill in the remaining spaces. */
864 printf ("%-*s", width - num_printed, " ");
865 num_printed = width;
866 }
867
868 free ((void *) alloced_symbol);
869 return num_printed;
870 }
871
872 /* Returns a pointer to a static buffer containing a printable version of
873 the given section's name. Like print_symbol, except that it does not try
874 to print multibyte characters, it just interprets them as hex values. */
875
876 static const char *
877 printable_section_name (Filedata * filedata, const Elf_Internal_Shdr * sec)
878 {
879 #define MAX_PRINT_SEC_NAME_LEN 256
880 static char sec_name_buf [MAX_PRINT_SEC_NAME_LEN + 1];
881 const char * name = section_name_print (filedata, sec);
882 char * buf = sec_name_buf;
883 char c;
884 unsigned int remaining = MAX_PRINT_SEC_NAME_LEN;
885
886 while ((c = * name ++) != 0)
887 {
888 if (ISCNTRL (c))
889 {
890 if (remaining < 2)
891 break;
892
893 * buf ++ = '^';
894 * buf ++ = c + 0x40;
895 remaining -= 2;
896 }
897 else if (ISPRINT (c))
898 {
899 * buf ++ = c;
900 remaining -= 1;
901 }
902 else
903 {
904 static char hex[17] = "0123456789ABCDEF";
905
906 if (remaining < 4)
907 break;
908 * buf ++ = '<';
909 * buf ++ = hex[(c & 0xf0) >> 4];
910 * buf ++ = hex[c & 0x0f];
911 * buf ++ = '>';
912 remaining -= 4;
913 }
914
915 if (remaining == 0)
916 break;
917 }
918
919 * buf = 0;
920 return sec_name_buf;
921 }
922
923 static const char *
924 printable_section_name_from_index (Filedata * filedata, unsigned long ndx)
925 {
926 if (ndx >= filedata->file_header.e_shnum)
927 return _("<corrupt>");
928
929 return printable_section_name (filedata, filedata->section_headers + ndx);
930 }
931
932 /* Return a pointer to section NAME, or NULL if no such section exists. */
933
934 static Elf_Internal_Shdr *
935 find_section (Filedata * filedata, const char * name)
936 {
937 unsigned int i;
938
939 if (filedata->section_headers == NULL)
940 return NULL;
941
942 for (i = 0; i < filedata->file_header.e_shnum; i++)
943 if (section_name_valid (filedata, filedata->section_headers + i)
944 && streq (section_name (filedata, filedata->section_headers + i),
945 name))
946 return filedata->section_headers + i;
947
948 return NULL;
949 }
950
951 /* Return a pointer to a section containing ADDR, or NULL if no such
952 section exists. */
953
954 static Elf_Internal_Shdr *
955 find_section_by_address (Filedata * filedata, bfd_vma addr)
956 {
957 unsigned int i;
958
959 if (filedata->section_headers == NULL)
960 return NULL;
961
962 for (i = 0; i < filedata->file_header.e_shnum; i++)
963 {
964 Elf_Internal_Shdr *sec = filedata->section_headers + i;
965
966 if (addr >= sec->sh_addr && addr < sec->sh_addr + sec->sh_size)
967 return sec;
968 }
969
970 return NULL;
971 }
972
973 static Elf_Internal_Shdr *
974 find_section_by_type (Filedata * filedata, unsigned int type)
975 {
976 unsigned int i;
977
978 if (filedata->section_headers == NULL)
979 return NULL;
980
981 for (i = 0; i < filedata->file_header.e_shnum; i++)
982 {
983 Elf_Internal_Shdr *sec = filedata->section_headers + i;
984
985 if (sec->sh_type == type)
986 return sec;
987 }
988
989 return NULL;
990 }
991
992 /* Return a pointer to section NAME, or NULL if no such section exists,
993 restricted to the list of sections given in SET. */
994
995 static Elf_Internal_Shdr *
996 find_section_in_set (Filedata * filedata, const char * name, unsigned int * set)
997 {
998 unsigned int i;
999
1000 if (filedata->section_headers == NULL)
1001 return NULL;
1002
1003 if (set != NULL)
1004 {
1005 while ((i = *set++) > 0)
1006 {
1007 /* See PR 21156 for a reproducer. */
1008 if (i >= filedata->file_header.e_shnum)
1009 continue; /* FIXME: Should we issue an error message ? */
1010
1011 if (section_name_valid (filedata, filedata->section_headers + i)
1012 && streq (section_name (filedata, filedata->section_headers + i),
1013 name))
1014 return filedata->section_headers + i;
1015 }
1016 }
1017
1018 return find_section (filedata, name);
1019 }
1020
1021 /* Return TRUE if the current file is for IA-64 machine and OpenVMS ABI.
1022 This OS has so many departures from the ELF standard that we test it at
1023 many places. */
1024
1025 static inline bool
1026 is_ia64_vms (Filedata * filedata)
1027 {
1028 return filedata->file_header.e_machine == EM_IA_64
1029 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS;
1030 }
1031
1032 /* Guess the relocation size commonly used by the specific machines. */
1033
1034 static bool
1035 guess_is_rela (unsigned int e_machine)
1036 {
1037 switch (e_machine)
1038 {
1039 /* Targets that use REL relocations. */
1040 case EM_386:
1041 case EM_IAMCU:
1042 case EM_960:
1043 case EM_ARM:
1044 case EM_D10V:
1045 case EM_CYGNUS_D10V:
1046 case EM_DLX:
1047 case EM_MIPS:
1048 case EM_MIPS_RS3_LE:
1049 case EM_CYGNUS_M32R:
1050 case EM_SCORE:
1051 case EM_XGATE:
1052 case EM_NFP:
1053 case EM_BPF:
1054 return false;
1055
1056 /* Targets that use RELA relocations. */
1057 case EM_68K:
1058 case EM_860:
1059 case EM_AARCH64:
1060 case EM_ADAPTEVA_EPIPHANY:
1061 case EM_ALPHA:
1062 case EM_ALTERA_NIOS2:
1063 case EM_ARC:
1064 case EM_ARC_COMPACT:
1065 case EM_ARC_COMPACT2:
1066 case EM_AVR:
1067 case EM_AVR_OLD:
1068 case EM_BLACKFIN:
1069 case EM_CR16:
1070 case EM_CRIS:
1071 case EM_CRX:
1072 case EM_CSKY:
1073 case EM_D30V:
1074 case EM_CYGNUS_D30V:
1075 case EM_FR30:
1076 case EM_FT32:
1077 case EM_CYGNUS_FR30:
1078 case EM_CYGNUS_FRV:
1079 case EM_H8S:
1080 case EM_H8_300:
1081 case EM_H8_300H:
1082 case EM_IA_64:
1083 case EM_IP2K:
1084 case EM_IP2K_OLD:
1085 case EM_IQ2000:
1086 case EM_LATTICEMICO32:
1087 case EM_M32C_OLD:
1088 case EM_M32C:
1089 case EM_M32R:
1090 case EM_MCORE:
1091 case EM_CYGNUS_MEP:
1092 case EM_METAG:
1093 case EM_MMIX:
1094 case EM_MN10200:
1095 case EM_CYGNUS_MN10200:
1096 case EM_MN10300:
1097 case EM_CYGNUS_MN10300:
1098 case EM_MOXIE:
1099 case EM_MSP430:
1100 case EM_MSP430_OLD:
1101 case EM_MT:
1102 case EM_NDS32:
1103 case EM_NIOS32:
1104 case EM_OR1K:
1105 case EM_PPC64:
1106 case EM_PPC:
1107 case EM_TI_PRU:
1108 case EM_RISCV:
1109 case EM_RL78:
1110 case EM_RX:
1111 case EM_S390:
1112 case EM_S390_OLD:
1113 case EM_SH:
1114 case EM_SPARC:
1115 case EM_SPARC32PLUS:
1116 case EM_SPARCV9:
1117 case EM_SPU:
1118 case EM_TI_C6000:
1119 case EM_TILEGX:
1120 case EM_TILEPRO:
1121 case EM_V800:
1122 case EM_V850:
1123 case EM_CYGNUS_V850:
1124 case EM_VAX:
1125 case EM_VISIUM:
1126 case EM_X86_64:
1127 case EM_L1OM:
1128 case EM_K1OM:
1129 case EM_XSTORMY16:
1130 case EM_XTENSA:
1131 case EM_XTENSA_OLD:
1132 case EM_MICROBLAZE:
1133 case EM_MICROBLAZE_OLD:
1134 case EM_WEBASSEMBLY:
1135 return true;
1136
1137 case EM_68HC05:
1138 case EM_68HC08:
1139 case EM_68HC11:
1140 case EM_68HC16:
1141 case EM_FX66:
1142 case EM_ME16:
1143 case EM_MMA:
1144 case EM_NCPU:
1145 case EM_NDR1:
1146 case EM_PCP:
1147 case EM_ST100:
1148 case EM_ST19:
1149 case EM_ST7:
1150 case EM_ST9PLUS:
1151 case EM_STARCORE:
1152 case EM_SVX:
1153 case EM_TINYJ:
1154 default:
1155 warn (_("Don't know about relocations on this machine architecture\n"));
1156 return false;
1157 }
1158 }
1159
1160 /* Load RELA type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1161 Returns TRUE upon success, FALSE otherwise. If successful then a
1162 pointer to a malloc'ed buffer containing the relocs is placed in *RELASP,
1163 and the number of relocs loaded is placed in *NRELASP. It is the caller's
1164 responsibility to free the allocated buffer. */
1165
1166 static bool
1167 slurp_rela_relocs (Filedata * filedata,
1168 unsigned long rel_offset,
1169 unsigned long rel_size,
1170 Elf_Internal_Rela ** relasp,
1171 unsigned long * nrelasp)
1172 {
1173 Elf_Internal_Rela * relas;
1174 size_t nrelas;
1175 unsigned int i;
1176
1177 if (is_32bit_elf)
1178 {
1179 Elf32_External_Rela * erelas;
1180
1181 erelas = (Elf32_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1182 rel_size, _("32-bit relocation data"));
1183 if (!erelas)
1184 return false;
1185
1186 nrelas = rel_size / sizeof (Elf32_External_Rela);
1187
1188 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1189 sizeof (Elf_Internal_Rela));
1190
1191 if (relas == NULL)
1192 {
1193 free (erelas);
1194 error (_("out of memory parsing relocs\n"));
1195 return false;
1196 }
1197
1198 for (i = 0; i < nrelas; i++)
1199 {
1200 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1201 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1202 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1203 }
1204
1205 free (erelas);
1206 }
1207 else
1208 {
1209 Elf64_External_Rela * erelas;
1210
1211 erelas = (Elf64_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1212 rel_size, _("64-bit relocation data"));
1213 if (!erelas)
1214 return false;
1215
1216 nrelas = rel_size / sizeof (Elf64_External_Rela);
1217
1218 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1219 sizeof (Elf_Internal_Rela));
1220
1221 if (relas == NULL)
1222 {
1223 free (erelas);
1224 error (_("out of memory parsing relocs\n"));
1225 return false;
1226 }
1227
1228 for (i = 0; i < nrelas; i++)
1229 {
1230 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1231 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1232 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1233
1234 /* The #ifdef BFD64 below is to prevent a compile time
1235 warning. We know that if we do not have a 64 bit data
1236 type that we will never execute this code anyway. */
1237 #ifdef BFD64
1238 if (filedata->file_header.e_machine == EM_MIPS
1239 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1240 {
1241 /* In little-endian objects, r_info isn't really a
1242 64-bit little-endian value: it has a 32-bit
1243 little-endian symbol index followed by four
1244 individual byte fields. Reorder INFO
1245 accordingly. */
1246 bfd_vma inf = relas[i].r_info;
1247 inf = (((inf & 0xffffffff) << 32)
1248 | ((inf >> 56) & 0xff)
1249 | ((inf >> 40) & 0xff00)
1250 | ((inf >> 24) & 0xff0000)
1251 | ((inf >> 8) & 0xff000000));
1252 relas[i].r_info = inf;
1253 }
1254 #endif /* BFD64 */
1255 }
1256
1257 free (erelas);
1258 }
1259
1260 *relasp = relas;
1261 *nrelasp = nrelas;
1262 return true;
1263 }
1264
1265 /* Load REL type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1266 Returns TRUE upon success, FALSE otherwise. If successful then a
1267 pointer to a malloc'ed buffer containing the relocs is placed in *RELSP,
1268 and the number of relocs loaded is placed in *NRELSP. It is the caller's
1269 responsibility to free the allocated buffer. */
1270
1271 static bool
1272 slurp_rel_relocs (Filedata * filedata,
1273 unsigned long rel_offset,
1274 unsigned long rel_size,
1275 Elf_Internal_Rela ** relsp,
1276 unsigned long * nrelsp)
1277 {
1278 Elf_Internal_Rela * rels;
1279 size_t nrels;
1280 unsigned int i;
1281
1282 if (is_32bit_elf)
1283 {
1284 Elf32_External_Rel * erels;
1285
1286 erels = (Elf32_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1287 rel_size, _("32-bit relocation data"));
1288 if (!erels)
1289 return false;
1290
1291 nrels = rel_size / sizeof (Elf32_External_Rel);
1292
1293 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1294
1295 if (rels == NULL)
1296 {
1297 free (erels);
1298 error (_("out of memory parsing relocs\n"));
1299 return false;
1300 }
1301
1302 for (i = 0; i < nrels; i++)
1303 {
1304 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1305 rels[i].r_info = BYTE_GET (erels[i].r_info);
1306 rels[i].r_addend = 0;
1307 }
1308
1309 free (erels);
1310 }
1311 else
1312 {
1313 Elf64_External_Rel * erels;
1314
1315 erels = (Elf64_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1316 rel_size, _("64-bit relocation data"));
1317 if (!erels)
1318 return false;
1319
1320 nrels = rel_size / sizeof (Elf64_External_Rel);
1321
1322 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1323
1324 if (rels == NULL)
1325 {
1326 free (erels);
1327 error (_("out of memory parsing relocs\n"));
1328 return false;
1329 }
1330
1331 for (i = 0; i < nrels; i++)
1332 {
1333 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1334 rels[i].r_info = BYTE_GET (erels[i].r_info);
1335 rels[i].r_addend = 0;
1336
1337 /* The #ifdef BFD64 below is to prevent a compile time
1338 warning. We know that if we do not have a 64 bit data
1339 type that we will never execute this code anyway. */
1340 #ifdef BFD64
1341 if (filedata->file_header.e_machine == EM_MIPS
1342 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1343 {
1344 /* In little-endian objects, r_info isn't really a
1345 64-bit little-endian value: it has a 32-bit
1346 little-endian symbol index followed by four
1347 individual byte fields. Reorder INFO
1348 accordingly. */
1349 bfd_vma inf = rels[i].r_info;
1350 inf = (((inf & 0xffffffff) << 32)
1351 | ((inf >> 56) & 0xff)
1352 | ((inf >> 40) & 0xff00)
1353 | ((inf >> 24) & 0xff0000)
1354 | ((inf >> 8) & 0xff000000));
1355 rels[i].r_info = inf;
1356 }
1357 #endif /* BFD64 */
1358 }
1359
1360 free (erels);
1361 }
1362
1363 *relsp = rels;
1364 *nrelsp = nrels;
1365 return true;
1366 }
1367
1368 static bool
1369 slurp_relr_relocs (Filedata * filedata,
1370 unsigned long relr_offset,
1371 unsigned long relr_size,
1372 bfd_vma ** relrsp,
1373 unsigned long * nrelrsp)
1374 {
1375 void *relrs;
1376 size_t size = 0, nentries, i;
1377 bfd_vma base = 0, addr, entry;
1378
1379 relrs = get_data (NULL, filedata, relr_offset, 1, relr_size,
1380 _("RELR relocation data"));
1381 if (!relrs)
1382 return false;
1383
1384 if (is_32bit_elf)
1385 nentries = relr_size / sizeof (Elf32_External_Relr);
1386 else
1387 nentries = relr_size / sizeof (Elf64_External_Relr);
1388 for (i = 0; i < nentries; i++)
1389 {
1390 if (is_32bit_elf)
1391 entry = BYTE_GET (((Elf32_External_Relr *)relrs)[i].r_data);
1392 else
1393 entry = BYTE_GET (((Elf64_External_Relr *)relrs)[i].r_data);
1394 if ((entry & 1) == 0)
1395 size++;
1396 else
1397 while ((entry >>= 1) != 0)
1398 if ((entry & 1) == 1)
1399 size++;
1400 }
1401
1402 *relrsp = (bfd_vma *) malloc (size * sizeof (bfd_vma));
1403 if (*relrsp == NULL)
1404 {
1405 free (relrs);
1406 error (_("out of memory parsing relocs\n"));
1407 return false;
1408 }
1409
1410 size = 0;
1411 for (i = 0; i < nentries; i++)
1412 {
1413 const bfd_vma entry_bytes = is_32bit_elf ? 4 : 8;
1414
1415 if (is_32bit_elf)
1416 entry = BYTE_GET (((Elf32_External_Relr *)relrs)[i].r_data);
1417 else
1418 entry = BYTE_GET (((Elf64_External_Relr *)relrs)[i].r_data);
1419 if ((entry & 1) == 0)
1420 {
1421 (*relrsp)[size++] = entry;
1422 base = entry + entry_bytes;
1423 }
1424 else
1425 {
1426 for (addr = base; (entry >>= 1) != 0; addr += entry_bytes)
1427 if ((entry & 1) != 0)
1428 (*relrsp)[size++] = addr;
1429 base += entry_bytes * (entry_bytes * CHAR_BIT - 1);
1430 }
1431 }
1432
1433 *nrelrsp = size;
1434 free (relrs);
1435 return true;
1436 }
1437
1438 /* Returns the reloc type extracted from the reloc info field. */
1439
1440 static unsigned int
1441 get_reloc_type (Filedata * filedata, bfd_vma reloc_info)
1442 {
1443 if (is_32bit_elf)
1444 return ELF32_R_TYPE (reloc_info);
1445
1446 switch (filedata->file_header.e_machine)
1447 {
1448 case EM_MIPS:
1449 /* Note: We assume that reloc_info has already been adjusted for us. */
1450 return ELF64_MIPS_R_TYPE (reloc_info);
1451
1452 case EM_SPARCV9:
1453 return ELF64_R_TYPE_ID (reloc_info);
1454
1455 default:
1456 return ELF64_R_TYPE (reloc_info);
1457 }
1458 }
1459
1460 /* Return the symbol index extracted from the reloc info field. */
1461
1462 static bfd_vma
1463 get_reloc_symindex (bfd_vma reloc_info)
1464 {
1465 return is_32bit_elf ? ELF32_R_SYM (reloc_info) : ELF64_R_SYM (reloc_info);
1466 }
1467
1468 static inline bool
1469 uses_msp430x_relocs (Filedata * filedata)
1470 {
1471 return
1472 filedata->file_header.e_machine == EM_MSP430 /* Paranoia. */
1473 /* GCC uses osabi == ELFOSBI_STANDALONE. */
1474 && (((filedata->file_header.e_flags & EF_MSP430_MACH) == E_MSP430_MACH_MSP430X)
1475 /* TI compiler uses ELFOSABI_NONE. */
1476 || (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_NONE));
1477 }
1478
1479 /* Display the contents of the relocation data found at the specified
1480 offset. */
1481
1482 static bool
1483 dump_relocations (Filedata * filedata,
1484 unsigned long rel_offset,
1485 unsigned long rel_size,
1486 Elf_Internal_Sym * symtab,
1487 unsigned long nsyms,
1488 char * strtab,
1489 unsigned long strtablen,
1490 relocation_type rel_type,
1491 bool is_dynsym)
1492 {
1493 unsigned long i;
1494 Elf_Internal_Rela * rels;
1495 bool res = true;
1496
1497 if (rel_type == reltype_unknown)
1498 rel_type = guess_is_rela (filedata->file_header.e_machine) ? reltype_rela : reltype_rel;
1499
1500 if (rel_type == reltype_rela)
1501 {
1502 if (!slurp_rela_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1503 return false;
1504 }
1505 else if (rel_type == reltype_rel)
1506 {
1507 if (!slurp_rel_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1508 return false;
1509 }
1510 else if (rel_type == reltype_relr)
1511 {
1512 bfd_vma * relrs;
1513 const char *format
1514 = is_32bit_elf ? "%08" BFD_VMA_FMT "x\n" : "%016" BFD_VMA_FMT "x\n";
1515
1516 if (!slurp_relr_relocs (filedata, rel_offset, rel_size, &relrs,
1517 &rel_size))
1518 return false;
1519
1520 printf (ngettext (" %lu offset\n", " %lu offsets\n", rel_size), rel_size);
1521 for (i = 0; i < rel_size; i++)
1522 printf (format, relrs[i]);
1523 free (relrs);
1524 return true;
1525 }
1526
1527 if (is_32bit_elf)
1528 {
1529 if (rel_type == reltype_rela)
1530 {
1531 if (do_wide)
1532 printf (_(" Offset Info Type Sym. Value Symbol's Name + Addend\n"));
1533 else
1534 printf (_(" Offset Info Type Sym.Value Sym. Name + Addend\n"));
1535 }
1536 else
1537 {
1538 if (do_wide)
1539 printf (_(" Offset Info Type Sym. Value Symbol's Name\n"));
1540 else
1541 printf (_(" Offset Info Type Sym.Value Sym. Name\n"));
1542 }
1543 }
1544 else
1545 {
1546 if (rel_type == reltype_rela)
1547 {
1548 if (do_wide)
1549 printf (_(" Offset Info Type Symbol's Value Symbol's Name + Addend\n"));
1550 else
1551 printf (_(" Offset Info Type Sym. Value Sym. Name + Addend\n"));
1552 }
1553 else
1554 {
1555 if (do_wide)
1556 printf (_(" Offset Info Type Symbol's Value Symbol's Name\n"));
1557 else
1558 printf (_(" Offset Info Type Sym. Value Sym. Name\n"));
1559 }
1560 }
1561
1562 for (i = 0; i < rel_size; i++)
1563 {
1564 const char * rtype;
1565 bfd_vma offset;
1566 bfd_vma inf;
1567 bfd_vma symtab_index;
1568 bfd_vma type;
1569
1570 offset = rels[i].r_offset;
1571 inf = rels[i].r_info;
1572
1573 type = get_reloc_type (filedata, inf);
1574 symtab_index = get_reloc_symindex (inf);
1575
1576 if (is_32bit_elf)
1577 {
1578 printf ("%8.8lx %8.8lx ",
1579 (unsigned long) offset & 0xffffffff,
1580 (unsigned long) inf & 0xffffffff);
1581 }
1582 else
1583 {
1584 printf (do_wide
1585 ? "%16.16" BFD_VMA_FMT "x %16.16" BFD_VMA_FMT "x "
1586 : "%12.12" BFD_VMA_FMT "x %12.12" BFD_VMA_FMT "x ",
1587 offset, inf);
1588 }
1589
1590 switch (filedata->file_header.e_machine)
1591 {
1592 default:
1593 rtype = NULL;
1594 break;
1595
1596 case EM_AARCH64:
1597 rtype = elf_aarch64_reloc_type (type);
1598 break;
1599
1600 case EM_M32R:
1601 case EM_CYGNUS_M32R:
1602 rtype = elf_m32r_reloc_type (type);
1603 break;
1604
1605 case EM_386:
1606 case EM_IAMCU:
1607 rtype = elf_i386_reloc_type (type);
1608 break;
1609
1610 case EM_68HC11:
1611 case EM_68HC12:
1612 rtype = elf_m68hc11_reloc_type (type);
1613 break;
1614
1615 case EM_S12Z:
1616 rtype = elf_s12z_reloc_type (type);
1617 break;
1618
1619 case EM_68K:
1620 rtype = elf_m68k_reloc_type (type);
1621 break;
1622
1623 case EM_960:
1624 rtype = elf_i960_reloc_type (type);
1625 break;
1626
1627 case EM_AVR:
1628 case EM_AVR_OLD:
1629 rtype = elf_avr_reloc_type (type);
1630 break;
1631
1632 case EM_OLD_SPARCV9:
1633 case EM_SPARC32PLUS:
1634 case EM_SPARCV9:
1635 case EM_SPARC:
1636 rtype = elf_sparc_reloc_type (type);
1637 break;
1638
1639 case EM_SPU:
1640 rtype = elf_spu_reloc_type (type);
1641 break;
1642
1643 case EM_V800:
1644 rtype = v800_reloc_type (type);
1645 break;
1646 case EM_V850:
1647 case EM_CYGNUS_V850:
1648 rtype = v850_reloc_type (type);
1649 break;
1650
1651 case EM_D10V:
1652 case EM_CYGNUS_D10V:
1653 rtype = elf_d10v_reloc_type (type);
1654 break;
1655
1656 case EM_D30V:
1657 case EM_CYGNUS_D30V:
1658 rtype = elf_d30v_reloc_type (type);
1659 break;
1660
1661 case EM_DLX:
1662 rtype = elf_dlx_reloc_type (type);
1663 break;
1664
1665 case EM_SH:
1666 rtype = elf_sh_reloc_type (type);
1667 break;
1668
1669 case EM_MN10300:
1670 case EM_CYGNUS_MN10300:
1671 rtype = elf_mn10300_reloc_type (type);
1672 break;
1673
1674 case EM_MN10200:
1675 case EM_CYGNUS_MN10200:
1676 rtype = elf_mn10200_reloc_type (type);
1677 break;
1678
1679 case EM_FR30:
1680 case EM_CYGNUS_FR30:
1681 rtype = elf_fr30_reloc_type (type);
1682 break;
1683
1684 case EM_CYGNUS_FRV:
1685 rtype = elf_frv_reloc_type (type);
1686 break;
1687
1688 case EM_CSKY:
1689 rtype = elf_csky_reloc_type (type);
1690 break;
1691
1692 case EM_FT32:
1693 rtype = elf_ft32_reloc_type (type);
1694 break;
1695
1696 case EM_MCORE:
1697 rtype = elf_mcore_reloc_type (type);
1698 break;
1699
1700 case EM_MMIX:
1701 rtype = elf_mmix_reloc_type (type);
1702 break;
1703
1704 case EM_MOXIE:
1705 rtype = elf_moxie_reloc_type (type);
1706 break;
1707
1708 case EM_MSP430:
1709 if (uses_msp430x_relocs (filedata))
1710 {
1711 rtype = elf_msp430x_reloc_type (type);
1712 break;
1713 }
1714 /* Fall through. */
1715 case EM_MSP430_OLD:
1716 rtype = elf_msp430_reloc_type (type);
1717 break;
1718
1719 case EM_NDS32:
1720 rtype = elf_nds32_reloc_type (type);
1721 break;
1722
1723 case EM_PPC:
1724 rtype = elf_ppc_reloc_type (type);
1725 break;
1726
1727 case EM_PPC64:
1728 rtype = elf_ppc64_reloc_type (type);
1729 break;
1730
1731 case EM_MIPS:
1732 case EM_MIPS_RS3_LE:
1733 rtype = elf_mips_reloc_type (type);
1734 break;
1735
1736 case EM_RISCV:
1737 rtype = elf_riscv_reloc_type (type);
1738 break;
1739
1740 case EM_ALPHA:
1741 rtype = elf_alpha_reloc_type (type);
1742 break;
1743
1744 case EM_ARM:
1745 rtype = elf_arm_reloc_type (type);
1746 break;
1747
1748 case EM_ARC:
1749 case EM_ARC_COMPACT:
1750 case EM_ARC_COMPACT2:
1751 rtype = elf_arc_reloc_type (type);
1752 break;
1753
1754 case EM_PARISC:
1755 rtype = elf_hppa_reloc_type (type);
1756 break;
1757
1758 case EM_H8_300:
1759 case EM_H8_300H:
1760 case EM_H8S:
1761 rtype = elf_h8_reloc_type (type);
1762 break;
1763
1764 case EM_OR1K:
1765 rtype = elf_or1k_reloc_type (type);
1766 break;
1767
1768 case EM_PJ:
1769 case EM_PJ_OLD:
1770 rtype = elf_pj_reloc_type (type);
1771 break;
1772 case EM_IA_64:
1773 rtype = elf_ia64_reloc_type (type);
1774 break;
1775
1776 case EM_CRIS:
1777 rtype = elf_cris_reloc_type (type);
1778 break;
1779
1780 case EM_860:
1781 rtype = elf_i860_reloc_type (type);
1782 break;
1783
1784 case EM_X86_64:
1785 case EM_L1OM:
1786 case EM_K1OM:
1787 rtype = elf_x86_64_reloc_type (type);
1788 break;
1789
1790 case EM_S370:
1791 rtype = i370_reloc_type (type);
1792 break;
1793
1794 case EM_S390_OLD:
1795 case EM_S390:
1796 rtype = elf_s390_reloc_type (type);
1797 break;
1798
1799 case EM_SCORE:
1800 rtype = elf_score_reloc_type (type);
1801 break;
1802
1803 case EM_XSTORMY16:
1804 rtype = elf_xstormy16_reloc_type (type);
1805 break;
1806
1807 case EM_CRX:
1808 rtype = elf_crx_reloc_type (type);
1809 break;
1810
1811 case EM_VAX:
1812 rtype = elf_vax_reloc_type (type);
1813 break;
1814
1815 case EM_VISIUM:
1816 rtype = elf_visium_reloc_type (type);
1817 break;
1818
1819 case EM_BPF:
1820 rtype = elf_bpf_reloc_type (type);
1821 break;
1822
1823 case EM_ADAPTEVA_EPIPHANY:
1824 rtype = elf_epiphany_reloc_type (type);
1825 break;
1826
1827 case EM_IP2K:
1828 case EM_IP2K_OLD:
1829 rtype = elf_ip2k_reloc_type (type);
1830 break;
1831
1832 case EM_IQ2000:
1833 rtype = elf_iq2000_reloc_type (type);
1834 break;
1835
1836 case EM_XTENSA_OLD:
1837 case EM_XTENSA:
1838 rtype = elf_xtensa_reloc_type (type);
1839 break;
1840
1841 case EM_LATTICEMICO32:
1842 rtype = elf_lm32_reloc_type (type);
1843 break;
1844
1845 case EM_M32C_OLD:
1846 case EM_M32C:
1847 rtype = elf_m32c_reloc_type (type);
1848 break;
1849
1850 case EM_MT:
1851 rtype = elf_mt_reloc_type (type);
1852 break;
1853
1854 case EM_BLACKFIN:
1855 rtype = elf_bfin_reloc_type (type);
1856 break;
1857
1858 case EM_CYGNUS_MEP:
1859 rtype = elf_mep_reloc_type (type);
1860 break;
1861
1862 case EM_CR16:
1863 rtype = elf_cr16_reloc_type (type);
1864 break;
1865
1866 case EM_MICROBLAZE:
1867 case EM_MICROBLAZE_OLD:
1868 rtype = elf_microblaze_reloc_type (type);
1869 break;
1870
1871 case EM_RL78:
1872 rtype = elf_rl78_reloc_type (type);
1873 break;
1874
1875 case EM_RX:
1876 rtype = elf_rx_reloc_type (type);
1877 break;
1878
1879 case EM_METAG:
1880 rtype = elf_metag_reloc_type (type);
1881 break;
1882
1883 case EM_TI_C6000:
1884 rtype = elf_tic6x_reloc_type (type);
1885 break;
1886
1887 case EM_TILEGX:
1888 rtype = elf_tilegx_reloc_type (type);
1889 break;
1890
1891 case EM_TILEPRO:
1892 rtype = elf_tilepro_reloc_type (type);
1893 break;
1894
1895 case EM_WEBASSEMBLY:
1896 rtype = elf_wasm32_reloc_type (type);
1897 break;
1898
1899 case EM_XGATE:
1900 rtype = elf_xgate_reloc_type (type);
1901 break;
1902
1903 case EM_ALTERA_NIOS2:
1904 rtype = elf_nios2_reloc_type (type);
1905 break;
1906
1907 case EM_TI_PRU:
1908 rtype = elf_pru_reloc_type (type);
1909 break;
1910
1911 case EM_NFP:
1912 if (EF_NFP_MACH (filedata->file_header.e_flags) == E_NFP_MACH_3200)
1913 rtype = elf_nfp3200_reloc_type (type);
1914 else
1915 rtype = elf_nfp_reloc_type (type);
1916 break;
1917
1918 case EM_Z80:
1919 rtype = elf_z80_reloc_type (type);
1920 break;
1921
1922 case EM_LOONGARCH:
1923 rtype = elf_loongarch_reloc_type (type);
1924 break;
1925
1926 case EM_AMDGPU:
1927 rtype = elf_amdgpu_reloc_type (type);
1928 break;
1929 }
1930
1931 if (rtype == NULL)
1932 printf (_("unrecognized: %-7lx"), (unsigned long) type & 0xffffffff);
1933 else
1934 printf (do_wide ? "%-22s" : "%-17.17s", rtype);
1935
1936 if (filedata->file_header.e_machine == EM_ALPHA
1937 && rtype != NULL
1938 && streq (rtype, "R_ALPHA_LITUSE")
1939 && rel_type == reltype_rela)
1940 {
1941 switch (rels[i].r_addend)
1942 {
1943 case LITUSE_ALPHA_ADDR: rtype = "ADDR"; break;
1944 case LITUSE_ALPHA_BASE: rtype = "BASE"; break;
1945 case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
1946 case LITUSE_ALPHA_JSR: rtype = "JSR"; break;
1947 case LITUSE_ALPHA_TLSGD: rtype = "TLSGD"; break;
1948 case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
1949 case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
1950 default: rtype = NULL;
1951 }
1952
1953 if (rtype)
1954 printf (" (%s)", rtype);
1955 else
1956 {
1957 putchar (' ');
1958 printf (_("<unknown addend: %lx>"),
1959 (unsigned long) rels[i].r_addend);
1960 res = false;
1961 }
1962 }
1963 else if (symtab_index)
1964 {
1965 if (symtab == NULL || symtab_index >= nsyms)
1966 {
1967 error (_(" bad symbol index: %08lx in reloc\n"),
1968 (unsigned long) symtab_index);
1969 res = false;
1970 }
1971 else
1972 {
1973 Elf_Internal_Sym * psym;
1974 const char * version_string;
1975 enum versioned_symbol_info sym_info;
1976 unsigned short vna_other;
1977
1978 psym = symtab + symtab_index;
1979
1980 version_string
1981 = get_symbol_version_string (filedata, is_dynsym,
1982 strtab, strtablen,
1983 symtab_index,
1984 psym,
1985 &sym_info,
1986 &vna_other);
1987
1988 printf (" ");
1989
1990 if (ELF_ST_TYPE (psym->st_info) == STT_GNU_IFUNC)
1991 {
1992 const char * name;
1993 unsigned int len;
1994 unsigned int width = is_32bit_elf ? 8 : 14;
1995
1996 /* Relocations against GNU_IFUNC symbols do not use the value
1997 of the symbol as the address to relocate against. Instead
1998 they invoke the function named by the symbol and use its
1999 result as the address for relocation.
2000
2001 To indicate this to the user, do not display the value of
2002 the symbol in the "Symbols's Value" field. Instead show
2003 its name followed by () as a hint that the symbol is
2004 invoked. */
2005
2006 if (strtab == NULL
2007 || psym->st_name == 0
2008 || psym->st_name >= strtablen)
2009 name = "??";
2010 else
2011 name = strtab + psym->st_name;
2012
2013 len = print_symbol (width, name);
2014 if (version_string)
2015 printf (sym_info == symbol_public ? "@@%s" : "@%s",
2016 version_string);
2017 printf ("()%-*s", len <= width ? (width + 1) - len : 1, " ");
2018 }
2019 else
2020 {
2021 print_vma (psym->st_value, LONG_HEX);
2022
2023 printf (is_32bit_elf ? " " : " ");
2024 }
2025
2026 if (psym->st_name == 0)
2027 {
2028 const char * sec_name = "<null>";
2029 char name_buf[40];
2030
2031 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
2032 {
2033 if (psym->st_shndx < filedata->file_header.e_shnum
2034 && filedata->section_headers != NULL)
2035 sec_name = section_name_print (filedata,
2036 filedata->section_headers
2037 + psym->st_shndx);
2038 else if (psym->st_shndx == SHN_ABS)
2039 sec_name = "ABS";
2040 else if (psym->st_shndx == SHN_COMMON)
2041 sec_name = "COMMON";
2042 else if ((filedata->file_header.e_machine == EM_MIPS
2043 && psym->st_shndx == SHN_MIPS_SCOMMON)
2044 || (filedata->file_header.e_machine == EM_TI_C6000
2045 && psym->st_shndx == SHN_TIC6X_SCOMMON))
2046 sec_name = "SCOMMON";
2047 else if (filedata->file_header.e_machine == EM_MIPS
2048 && psym->st_shndx == SHN_MIPS_SUNDEFINED)
2049 sec_name = "SUNDEF";
2050 else if ((filedata->file_header.e_machine == EM_X86_64
2051 || filedata->file_header.e_machine == EM_L1OM
2052 || filedata->file_header.e_machine == EM_K1OM)
2053 && psym->st_shndx == SHN_X86_64_LCOMMON)
2054 sec_name = "LARGE_COMMON";
2055 else if (filedata->file_header.e_machine == EM_IA_64
2056 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
2057 && psym->st_shndx == SHN_IA_64_ANSI_COMMON)
2058 sec_name = "ANSI_COM";
2059 else if (is_ia64_vms (filedata)
2060 && psym->st_shndx == SHN_IA_64_VMS_SYMVEC)
2061 sec_name = "VMS_SYMVEC";
2062 else
2063 {
2064 sprintf (name_buf, "<section 0x%x>",
2065 (unsigned int) psym->st_shndx);
2066 sec_name = name_buf;
2067 }
2068 }
2069 print_symbol (22, sec_name);
2070 }
2071 else if (strtab == NULL)
2072 printf (_("<string table index: %3ld>"), psym->st_name);
2073 else if (psym->st_name >= strtablen)
2074 {
2075 error (_("<corrupt string table index: %3ld>\n"),
2076 psym->st_name);
2077 res = false;
2078 }
2079 else
2080 {
2081 print_symbol (22, strtab + psym->st_name);
2082 if (version_string)
2083 printf (sym_info == symbol_public ? "@@%s" : "@%s",
2084 version_string);
2085 }
2086
2087 if (rel_type == reltype_rela)
2088 {
2089 bfd_vma off = rels[i].r_addend;
2090
2091 if ((bfd_signed_vma) off < 0)
2092 printf (" - %" BFD_VMA_FMT "x", - off);
2093 else
2094 printf (" + %" BFD_VMA_FMT "x", off);
2095 }
2096 }
2097 }
2098 else if (rel_type == reltype_rela)
2099 {
2100 bfd_vma off = rels[i].r_addend;
2101
2102 printf ("%*c", is_32bit_elf ? 12 : 20, ' ');
2103 if ((bfd_signed_vma) off < 0)
2104 printf ("-%" BFD_VMA_FMT "x", - off);
2105 else
2106 printf ("%" BFD_VMA_FMT "x", off);
2107 }
2108
2109 if (filedata->file_header.e_machine == EM_SPARCV9
2110 && rtype != NULL
2111 && streq (rtype, "R_SPARC_OLO10"))
2112 printf (" + %lx", (unsigned long) ELF64_R_TYPE_DATA (inf));
2113
2114 putchar ('\n');
2115
2116 #ifdef BFD64
2117 if (! is_32bit_elf && filedata->file_header.e_machine == EM_MIPS)
2118 {
2119 bfd_vma type2 = ELF64_MIPS_R_TYPE2 (inf);
2120 bfd_vma type3 = ELF64_MIPS_R_TYPE3 (inf);
2121 const char * rtype2 = elf_mips_reloc_type (type2);
2122 const char * rtype3 = elf_mips_reloc_type (type3);
2123
2124 printf (" Type2: ");
2125
2126 if (rtype2 == NULL)
2127 printf (_("unrecognized: %-7lx"),
2128 (unsigned long) type2 & 0xffffffff);
2129 else
2130 printf ("%-17.17s", rtype2);
2131
2132 printf ("\n Type3: ");
2133
2134 if (rtype3 == NULL)
2135 printf (_("unrecognized: %-7lx"),
2136 (unsigned long) type3 & 0xffffffff);
2137 else
2138 printf ("%-17.17s", rtype3);
2139
2140 putchar ('\n');
2141 }
2142 #endif /* BFD64 */
2143 }
2144
2145 free (rels);
2146
2147 return res;
2148 }
2149
2150 static const char *
2151 get_aarch64_dynamic_type (unsigned long type)
2152 {
2153 switch (type)
2154 {
2155 case DT_AARCH64_BTI_PLT: return "AARCH64_BTI_PLT";
2156 case DT_AARCH64_PAC_PLT: return "AARCH64_PAC_PLT";
2157 case DT_AARCH64_VARIANT_PCS: return "AARCH64_VARIANT_PCS";
2158 default:
2159 return NULL;
2160 }
2161 }
2162
2163 static const char *
2164 get_mips_dynamic_type (unsigned long type)
2165 {
2166 switch (type)
2167 {
2168 case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
2169 case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
2170 case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
2171 case DT_MIPS_IVERSION: return "MIPS_IVERSION";
2172 case DT_MIPS_FLAGS: return "MIPS_FLAGS";
2173 case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
2174 case DT_MIPS_MSYM: return "MIPS_MSYM";
2175 case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
2176 case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
2177 case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
2178 case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
2179 case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
2180 case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
2181 case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
2182 case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
2183 case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
2184 case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
2185 case DT_MIPS_RLD_MAP_REL: return "MIPS_RLD_MAP_REL";
2186 case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
2187 case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
2188 case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
2189 case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
2190 case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
2191 case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
2192 case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
2193 case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
2194 case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
2195 case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
2196 case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
2197 case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
2198 case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
2199 case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
2200 case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
2201 case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
2202 case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
2203 case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
2204 case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
2205 case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
2206 case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
2207 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
2208 case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
2209 case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
2210 case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
2211 case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
2212 case DT_MIPS_PLTGOT: return "MIPS_PLTGOT";
2213 case DT_MIPS_RWPLT: return "MIPS_RWPLT";
2214 case DT_MIPS_XHASH: return "MIPS_XHASH";
2215 default:
2216 return NULL;
2217 }
2218 }
2219
2220 static const char *
2221 get_sparc64_dynamic_type (unsigned long type)
2222 {
2223 switch (type)
2224 {
2225 case DT_SPARC_REGISTER: return "SPARC_REGISTER";
2226 default:
2227 return NULL;
2228 }
2229 }
2230
2231 static const char *
2232 get_ppc_dynamic_type (unsigned long type)
2233 {
2234 switch (type)
2235 {
2236 case DT_PPC_GOT: return "PPC_GOT";
2237 case DT_PPC_OPT: return "PPC_OPT";
2238 default:
2239 return NULL;
2240 }
2241 }
2242
2243 static const char *
2244 get_ppc64_dynamic_type (unsigned long type)
2245 {
2246 switch (type)
2247 {
2248 case DT_PPC64_GLINK: return "PPC64_GLINK";
2249 case DT_PPC64_OPD: return "PPC64_OPD";
2250 case DT_PPC64_OPDSZ: return "PPC64_OPDSZ";
2251 case DT_PPC64_OPT: return "PPC64_OPT";
2252 default:
2253 return NULL;
2254 }
2255 }
2256
2257 static const char *
2258 get_parisc_dynamic_type (unsigned long type)
2259 {
2260 switch (type)
2261 {
2262 case DT_HP_LOAD_MAP: return "HP_LOAD_MAP";
2263 case DT_HP_DLD_FLAGS: return "HP_DLD_FLAGS";
2264 case DT_HP_DLD_HOOK: return "HP_DLD_HOOK";
2265 case DT_HP_UX10_INIT: return "HP_UX10_INIT";
2266 case DT_HP_UX10_INITSZ: return "HP_UX10_INITSZ";
2267 case DT_HP_PREINIT: return "HP_PREINIT";
2268 case DT_HP_PREINITSZ: return "HP_PREINITSZ";
2269 case DT_HP_NEEDED: return "HP_NEEDED";
2270 case DT_HP_TIME_STAMP: return "HP_TIME_STAMP";
2271 case DT_HP_CHECKSUM: return "HP_CHECKSUM";
2272 case DT_HP_GST_SIZE: return "HP_GST_SIZE";
2273 case DT_HP_GST_VERSION: return "HP_GST_VERSION";
2274 case DT_HP_GST_HASHVAL: return "HP_GST_HASHVAL";
2275 case DT_HP_EPLTREL: return "HP_GST_EPLTREL";
2276 case DT_HP_EPLTRELSZ: return "HP_GST_EPLTRELSZ";
2277 case DT_HP_FILTERED: return "HP_FILTERED";
2278 case DT_HP_FILTER_TLS: return "HP_FILTER_TLS";
2279 case DT_HP_COMPAT_FILTERED: return "HP_COMPAT_FILTERED";
2280 case DT_HP_LAZYLOAD: return "HP_LAZYLOAD";
2281 case DT_HP_BIND_NOW_COUNT: return "HP_BIND_NOW_COUNT";
2282 case DT_PLT: return "PLT";
2283 case DT_PLT_SIZE: return "PLT_SIZE";
2284 case DT_DLT: return "DLT";
2285 case DT_DLT_SIZE: return "DLT_SIZE";
2286 default:
2287 return NULL;
2288 }
2289 }
2290
2291 static const char *
2292 get_ia64_dynamic_type (unsigned long type)
2293 {
2294 switch (type)
2295 {
2296 case DT_IA_64_PLT_RESERVE: return "IA_64_PLT_RESERVE";
2297 case DT_IA_64_VMS_SUBTYPE: return "VMS_SUBTYPE";
2298 case DT_IA_64_VMS_IMGIOCNT: return "VMS_IMGIOCNT";
2299 case DT_IA_64_VMS_LNKFLAGS: return "VMS_LNKFLAGS";
2300 case DT_IA_64_VMS_VIR_MEM_BLK_SIZ: return "VMS_VIR_MEM_BLK_SIZ";
2301 case DT_IA_64_VMS_IDENT: return "VMS_IDENT";
2302 case DT_IA_64_VMS_NEEDED_IDENT: return "VMS_NEEDED_IDENT";
2303 case DT_IA_64_VMS_IMG_RELA_CNT: return "VMS_IMG_RELA_CNT";
2304 case DT_IA_64_VMS_SEG_RELA_CNT: return "VMS_SEG_RELA_CNT";
2305 case DT_IA_64_VMS_FIXUP_RELA_CNT: return "VMS_FIXUP_RELA_CNT";
2306 case DT_IA_64_VMS_FIXUP_NEEDED: return "VMS_FIXUP_NEEDED";
2307 case DT_IA_64_VMS_SYMVEC_CNT: return "VMS_SYMVEC_CNT";
2308 case DT_IA_64_VMS_XLATED: return "VMS_XLATED";
2309 case DT_IA_64_VMS_STACKSIZE: return "VMS_STACKSIZE";
2310 case DT_IA_64_VMS_UNWINDSZ: return "VMS_UNWINDSZ";
2311 case DT_IA_64_VMS_UNWIND_CODSEG: return "VMS_UNWIND_CODSEG";
2312 case DT_IA_64_VMS_UNWIND_INFOSEG: return "VMS_UNWIND_INFOSEG";
2313 case DT_IA_64_VMS_LINKTIME: return "VMS_LINKTIME";
2314 case DT_IA_64_VMS_SEG_NO: return "VMS_SEG_NO";
2315 case DT_IA_64_VMS_SYMVEC_OFFSET: return "VMS_SYMVEC_OFFSET";
2316 case DT_IA_64_VMS_SYMVEC_SEG: return "VMS_SYMVEC_SEG";
2317 case DT_IA_64_VMS_UNWIND_OFFSET: return "VMS_UNWIND_OFFSET";
2318 case DT_IA_64_VMS_UNWIND_SEG: return "VMS_UNWIND_SEG";
2319 case DT_IA_64_VMS_STRTAB_OFFSET: return "VMS_STRTAB_OFFSET";
2320 case DT_IA_64_VMS_SYSVER_OFFSET: return "VMS_SYSVER_OFFSET";
2321 case DT_IA_64_VMS_IMG_RELA_OFF: return "VMS_IMG_RELA_OFF";
2322 case DT_IA_64_VMS_SEG_RELA_OFF: return "VMS_SEG_RELA_OFF";
2323 case DT_IA_64_VMS_FIXUP_RELA_OFF: return "VMS_FIXUP_RELA_OFF";
2324 case DT_IA_64_VMS_PLTGOT_OFFSET: return "VMS_PLTGOT_OFFSET";
2325 case DT_IA_64_VMS_PLTGOT_SEG: return "VMS_PLTGOT_SEG";
2326 case DT_IA_64_VMS_FPMODE: return "VMS_FPMODE";
2327 default:
2328 return NULL;
2329 }
2330 }
2331
2332 static const char *
2333 get_solaris_section_type (unsigned long type)
2334 {
2335 switch (type)
2336 {
2337 case 0x6fffffee: return "SUNW_ancillary";
2338 case 0x6fffffef: return "SUNW_capchain";
2339 case 0x6ffffff0: return "SUNW_capinfo";
2340 case 0x6ffffff1: return "SUNW_symsort";
2341 case 0x6ffffff2: return "SUNW_tlssort";
2342 case 0x6ffffff3: return "SUNW_LDYNSYM";
2343 case 0x6ffffff4: return "SUNW_dof";
2344 case 0x6ffffff5: return "SUNW_cap";
2345 case 0x6ffffff6: return "SUNW_SIGNATURE";
2346 case 0x6ffffff7: return "SUNW_ANNOTATE";
2347 case 0x6ffffff8: return "SUNW_DEBUGSTR";
2348 case 0x6ffffff9: return "SUNW_DEBUG";
2349 case 0x6ffffffa: return "SUNW_move";
2350 case 0x6ffffffb: return "SUNW_COMDAT";
2351 case 0x6ffffffc: return "SUNW_syminfo";
2352 case 0x6ffffffd: return "SUNW_verdef";
2353 case 0x6ffffffe: return "SUNW_verneed";
2354 case 0x6fffffff: return "SUNW_versym";
2355 case 0x70000000: return "SPARC_GOTDATA";
2356 default: return NULL;
2357 }
2358 }
2359
2360 static const char *
2361 get_alpha_dynamic_type (unsigned long type)
2362 {
2363 switch (type)
2364 {
2365 case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
2366 default: return NULL;
2367 }
2368 }
2369
2370 static const char *
2371 get_score_dynamic_type (unsigned long type)
2372 {
2373 switch (type)
2374 {
2375 case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
2376 case DT_SCORE_LOCAL_GOTNO: return "SCORE_LOCAL_GOTNO";
2377 case DT_SCORE_SYMTABNO: return "SCORE_SYMTABNO";
2378 case DT_SCORE_GOTSYM: return "SCORE_GOTSYM";
2379 case DT_SCORE_UNREFEXTNO: return "SCORE_UNREFEXTNO";
2380 case DT_SCORE_HIPAGENO: return "SCORE_HIPAGENO";
2381 default: return NULL;
2382 }
2383 }
2384
2385 static const char *
2386 get_tic6x_dynamic_type (unsigned long type)
2387 {
2388 switch (type)
2389 {
2390 case DT_C6000_GSYM_OFFSET: return "C6000_GSYM_OFFSET";
2391 case DT_C6000_GSTR_OFFSET: return "C6000_GSTR_OFFSET";
2392 case DT_C6000_DSBT_BASE: return "C6000_DSBT_BASE";
2393 case DT_C6000_DSBT_SIZE: return "C6000_DSBT_SIZE";
2394 case DT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
2395 case DT_C6000_DSBT_INDEX: return "C6000_DSBT_INDEX";
2396 default: return NULL;
2397 }
2398 }
2399
2400 static const char *
2401 get_nios2_dynamic_type (unsigned long type)
2402 {
2403 switch (type)
2404 {
2405 case DT_NIOS2_GP: return "NIOS2_GP";
2406 default: return NULL;
2407 }
2408 }
2409
2410 static const char *
2411 get_solaris_dynamic_type (unsigned long type)
2412 {
2413 switch (type)
2414 {
2415 case 0x6000000d: return "SUNW_AUXILIARY";
2416 case 0x6000000e: return "SUNW_RTLDINF";
2417 case 0x6000000f: return "SUNW_FILTER";
2418 case 0x60000010: return "SUNW_CAP";
2419 case 0x60000011: return "SUNW_SYMTAB";
2420 case 0x60000012: return "SUNW_SYMSZ";
2421 case 0x60000013: return "SUNW_SORTENT";
2422 case 0x60000014: return "SUNW_SYMSORT";
2423 case 0x60000015: return "SUNW_SYMSORTSZ";
2424 case 0x60000016: return "SUNW_TLSSORT";
2425 case 0x60000017: return "SUNW_TLSSORTSZ";
2426 case 0x60000018: return "SUNW_CAPINFO";
2427 case 0x60000019: return "SUNW_STRPAD";
2428 case 0x6000001a: return "SUNW_CAPCHAIN";
2429 case 0x6000001b: return "SUNW_LDMACH";
2430 case 0x6000001d: return "SUNW_CAPCHAINENT";
2431 case 0x6000001f: return "SUNW_CAPCHAINSZ";
2432 case 0x60000021: return "SUNW_PARENT";
2433 case 0x60000023: return "SUNW_ASLR";
2434 case 0x60000025: return "SUNW_RELAX";
2435 case 0x60000029: return "SUNW_NXHEAP";
2436 case 0x6000002b: return "SUNW_NXSTACK";
2437
2438 case 0x70000001: return "SPARC_REGISTER";
2439 case 0x7ffffffd: return "AUXILIARY";
2440 case 0x7ffffffe: return "USED";
2441 case 0x7fffffff: return "FILTER";
2442
2443 default: return NULL;
2444 }
2445 }
2446
2447 static const char *
2448 get_riscv_dynamic_type (unsigned long type)
2449 {
2450 switch (type)
2451 {
2452 case DT_RISCV_VARIANT_CC: return "RISCV_VARIANT_CC";
2453 default:
2454 return NULL;
2455 }
2456 }
2457
2458 static const char *
2459 get_dynamic_type (Filedata * filedata, unsigned long type)
2460 {
2461 static char buff[64];
2462
2463 switch (type)
2464 {
2465 case DT_NULL: return "NULL";
2466 case DT_NEEDED: return "NEEDED";
2467 case DT_PLTRELSZ: return "PLTRELSZ";
2468 case DT_PLTGOT: return "PLTGOT";
2469 case DT_HASH: return "HASH";
2470 case DT_STRTAB: return "STRTAB";
2471 case DT_SYMTAB: return "SYMTAB";
2472 case DT_RELA: return "RELA";
2473 case DT_RELASZ: return "RELASZ";
2474 case DT_RELAENT: return "RELAENT";
2475 case DT_STRSZ: return "STRSZ";
2476 case DT_SYMENT: return "SYMENT";
2477 case DT_INIT: return "INIT";
2478 case DT_FINI: return "FINI";
2479 case DT_SONAME: return "SONAME";
2480 case DT_RPATH: return "RPATH";
2481 case DT_SYMBOLIC: return "SYMBOLIC";
2482 case DT_REL: return "REL";
2483 case DT_RELSZ: return "RELSZ";
2484 case DT_RELENT: return "RELENT";
2485 case DT_RELR: return "RELR";
2486 case DT_RELRSZ: return "RELRSZ";
2487 case DT_RELRENT: return "RELRENT";
2488 case DT_PLTREL: return "PLTREL";
2489 case DT_DEBUG: return "DEBUG";
2490 case DT_TEXTREL: return "TEXTREL";
2491 case DT_JMPREL: return "JMPREL";
2492 case DT_BIND_NOW: return "BIND_NOW";
2493 case DT_INIT_ARRAY: return "INIT_ARRAY";
2494 case DT_FINI_ARRAY: return "FINI_ARRAY";
2495 case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
2496 case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
2497 case DT_RUNPATH: return "RUNPATH";
2498 case DT_FLAGS: return "FLAGS";
2499
2500 case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
2501 case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
2502 case DT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
2503
2504 case DT_CHECKSUM: return "CHECKSUM";
2505 case DT_PLTPADSZ: return "PLTPADSZ";
2506 case DT_MOVEENT: return "MOVEENT";
2507 case DT_MOVESZ: return "MOVESZ";
2508 case DT_FEATURE: return "FEATURE";
2509 case DT_POSFLAG_1: return "POSFLAG_1";
2510 case DT_SYMINSZ: return "SYMINSZ";
2511 case DT_SYMINENT: return "SYMINENT"; /* aka VALRNGHI */
2512
2513 case DT_ADDRRNGLO: return "ADDRRNGLO";
2514 case DT_CONFIG: return "CONFIG";
2515 case DT_DEPAUDIT: return "DEPAUDIT";
2516 case DT_AUDIT: return "AUDIT";
2517 case DT_PLTPAD: return "PLTPAD";
2518 case DT_MOVETAB: return "MOVETAB";
2519 case DT_SYMINFO: return "SYMINFO"; /* aka ADDRRNGHI */
2520
2521 case DT_VERSYM: return "VERSYM";
2522
2523 case DT_TLSDESC_GOT: return "TLSDESC_GOT";
2524 case DT_TLSDESC_PLT: return "TLSDESC_PLT";
2525 case DT_RELACOUNT: return "RELACOUNT";
2526 case DT_RELCOUNT: return "RELCOUNT";
2527 case DT_FLAGS_1: return "FLAGS_1";
2528 case DT_VERDEF: return "VERDEF";
2529 case DT_VERDEFNUM: return "VERDEFNUM";
2530 case DT_VERNEED: return "VERNEED";
2531 case DT_VERNEEDNUM: return "VERNEEDNUM";
2532
2533 case DT_AUXILIARY: return "AUXILIARY";
2534 case DT_USED: return "USED";
2535 case DT_FILTER: return "FILTER";
2536
2537 case DT_GNU_PRELINKED: return "GNU_PRELINKED";
2538 case DT_GNU_CONFLICT: return "GNU_CONFLICT";
2539 case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
2540 case DT_GNU_LIBLIST: return "GNU_LIBLIST";
2541 case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
2542 case DT_GNU_HASH: return "GNU_HASH";
2543 case DT_GNU_FLAGS_1: return "GNU_FLAGS_1";
2544
2545 default:
2546 if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
2547 {
2548 const char * result;
2549
2550 switch (filedata->file_header.e_machine)
2551 {
2552 case EM_AARCH64:
2553 result = get_aarch64_dynamic_type (type);
2554 break;
2555 case EM_MIPS:
2556 case EM_MIPS_RS3_LE:
2557 result = get_mips_dynamic_type (type);
2558 break;
2559 case EM_SPARCV9:
2560 result = get_sparc64_dynamic_type (type);
2561 break;
2562 case EM_PPC:
2563 result = get_ppc_dynamic_type (type);
2564 break;
2565 case EM_PPC64:
2566 result = get_ppc64_dynamic_type (type);
2567 break;
2568 case EM_IA_64:
2569 result = get_ia64_dynamic_type (type);
2570 break;
2571 case EM_ALPHA:
2572 result = get_alpha_dynamic_type (type);
2573 break;
2574 case EM_SCORE:
2575 result = get_score_dynamic_type (type);
2576 break;
2577 case EM_TI_C6000:
2578 result = get_tic6x_dynamic_type (type);
2579 break;
2580 case EM_ALTERA_NIOS2:
2581 result = get_nios2_dynamic_type (type);
2582 break;
2583 case EM_RISCV:
2584 result = get_riscv_dynamic_type (type);
2585 break;
2586 default:
2587 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2588 result = get_solaris_dynamic_type (type);
2589 else
2590 result = NULL;
2591 break;
2592 }
2593
2594 if (result != NULL)
2595 return result;
2596
2597 snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
2598 }
2599 else if (((type >= DT_LOOS) && (type <= DT_HIOS))
2600 || (filedata->file_header.e_machine == EM_PARISC
2601 && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
2602 {
2603 const char * result;
2604
2605 switch (filedata->file_header.e_machine)
2606 {
2607 case EM_PARISC:
2608 result = get_parisc_dynamic_type (type);
2609 break;
2610 case EM_IA_64:
2611 result = get_ia64_dynamic_type (type);
2612 break;
2613 default:
2614 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2615 result = get_solaris_dynamic_type (type);
2616 else
2617 result = NULL;
2618 break;
2619 }
2620
2621 if (result != NULL)
2622 return result;
2623
2624 snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
2625 type);
2626 }
2627 else
2628 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
2629
2630 return buff;
2631 }
2632 }
2633
2634 static bool get_program_headers (Filedata *);
2635 static bool get_dynamic_section (Filedata *);
2636
2637 static void
2638 locate_dynamic_section (Filedata *filedata)
2639 {
2640 unsigned long dynamic_addr = 0;
2641 bfd_size_type dynamic_size = 0;
2642
2643 if (filedata->file_header.e_phnum != 0
2644 && get_program_headers (filedata))
2645 {
2646 Elf_Internal_Phdr *segment;
2647 unsigned int i;
2648
2649 for (i = 0, segment = filedata->program_headers;
2650 i < filedata->file_header.e_phnum;
2651 i++, segment++)
2652 {
2653 if (segment->p_type == PT_DYNAMIC)
2654 {
2655 dynamic_addr = segment->p_offset;
2656 dynamic_size = segment->p_filesz;
2657
2658 if (filedata->section_headers != NULL)
2659 {
2660 Elf_Internal_Shdr *sec;
2661
2662 sec = find_section (filedata, ".dynamic");
2663 if (sec != NULL)
2664 {
2665 if (sec->sh_size == 0
2666 || sec->sh_type == SHT_NOBITS)
2667 {
2668 dynamic_addr = 0;
2669 dynamic_size = 0;
2670 }
2671 else
2672 {
2673 dynamic_addr = sec->sh_offset;
2674 dynamic_size = sec->sh_size;
2675 }
2676 }
2677 }
2678
2679 if (dynamic_addr > filedata->file_size
2680 || (dynamic_size > filedata->file_size - dynamic_addr))
2681 {
2682 dynamic_addr = 0;
2683 dynamic_size = 0;
2684 }
2685 break;
2686 }
2687 }
2688 }
2689 filedata->dynamic_addr = dynamic_addr;
2690 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
2691 }
2692
2693 static bool
2694 is_pie (Filedata *filedata)
2695 {
2696 Elf_Internal_Dyn *entry;
2697
2698 if (filedata->dynamic_size == 0)
2699 locate_dynamic_section (filedata);
2700 if (filedata->dynamic_size <= 1)
2701 return false;
2702
2703 if (!get_dynamic_section (filedata))
2704 return false;
2705
2706 for (entry = filedata->dynamic_section;
2707 entry < filedata->dynamic_section + filedata->dynamic_nent;
2708 entry++)
2709 {
2710 if (entry->d_tag == DT_FLAGS_1)
2711 {
2712 if ((entry->d_un.d_val & DF_1_PIE) != 0)
2713 return true;
2714 break;
2715 }
2716 }
2717 return false;
2718 }
2719
2720 static char *
2721 get_file_type (Filedata *filedata)
2722 {
2723 unsigned e_type = filedata->file_header.e_type;
2724 static char buff[64];
2725
2726 switch (e_type)
2727 {
2728 case ET_NONE: return _("NONE (None)");
2729 case ET_REL: return _("REL (Relocatable file)");
2730 case ET_EXEC: return _("EXEC (Executable file)");
2731 case ET_DYN:
2732 if (is_pie (filedata))
2733 return _("DYN (Position-Independent Executable file)");
2734 else
2735 return _("DYN (Shared object file)");
2736 case ET_CORE: return _("CORE (Core file)");
2737
2738 default:
2739 if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
2740 snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
2741 else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
2742 snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
2743 else
2744 snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
2745 return buff;
2746 }
2747 }
2748
2749 static char *
2750 get_machine_name (unsigned e_machine)
2751 {
2752 static char buff[64]; /* XXX */
2753
2754 switch (e_machine)
2755 {
2756 /* Please keep this switch table sorted by increasing EM_ value. */
2757 /* 0 */
2758 case EM_NONE: return _("None");
2759 case EM_M32: return "WE32100";
2760 case EM_SPARC: return "Sparc";
2761 case EM_386: return "Intel 80386";
2762 case EM_68K: return "MC68000";
2763 case EM_88K: return "MC88000";
2764 case EM_IAMCU: return "Intel MCU";
2765 case EM_860: return "Intel 80860";
2766 case EM_MIPS: return "MIPS R3000";
2767 case EM_S370: return "IBM System/370";
2768 /* 10 */
2769 case EM_MIPS_RS3_LE: return "MIPS R4000 big-endian";
2770 case EM_OLD_SPARCV9: return "Sparc v9 (old)";
2771 case EM_PARISC: return "HPPA";
2772 case EM_VPP550: return "Fujitsu VPP500";
2773 case EM_SPARC32PLUS: return "Sparc v8+" ;
2774 case EM_960: return "Intel 80960";
2775 case EM_PPC: return "PowerPC";
2776 /* 20 */
2777 case EM_PPC64: return "PowerPC64";
2778 case EM_S390_OLD:
2779 case EM_S390: return "IBM S/390";
2780 case EM_SPU: return "SPU";
2781 /* 30 */
2782 case EM_V800: return "Renesas V850 (using RH850 ABI)";
2783 case EM_FR20: return "Fujitsu FR20";
2784 case EM_RH32: return "TRW RH32";
2785 case EM_MCORE: return "MCORE";
2786 /* 40 */
2787 case EM_ARM: return "ARM";
2788 case EM_OLD_ALPHA: return "Digital Alpha (old)";
2789 case EM_SH: return "Renesas / SuperH SH";
2790 case EM_SPARCV9: return "Sparc v9";
2791 case EM_TRICORE: return "Siemens Tricore";
2792 case EM_ARC: return "ARC";
2793 case EM_H8_300: return "Renesas H8/300";
2794 case EM_H8_300H: return "Renesas H8/300H";
2795 case EM_H8S: return "Renesas H8S";
2796 case EM_H8_500: return "Renesas H8/500";
2797 /* 50 */
2798 case EM_IA_64: return "Intel IA-64";
2799 case EM_MIPS_X: return "Stanford MIPS-X";
2800 case EM_COLDFIRE: return "Motorola Coldfire";
2801 case EM_68HC12: return "Motorola MC68HC12 Microcontroller";
2802 case EM_MMA: return "Fujitsu Multimedia Accelerator";
2803 case EM_PCP: return "Siemens PCP";
2804 case EM_NCPU: return "Sony nCPU embedded RISC processor";
2805 case EM_NDR1: return "Denso NDR1 microprocesspr";
2806 case EM_STARCORE: return "Motorola Star*Core processor";
2807 case EM_ME16: return "Toyota ME16 processor";
2808 /* 60 */
2809 case EM_ST100: return "STMicroelectronics ST100 processor";
2810 case EM_TINYJ: return "Advanced Logic Corp. TinyJ embedded processor";
2811 case EM_X86_64: return "Advanced Micro Devices X86-64";
2812 case EM_PDSP: return "Sony DSP processor";
2813 case EM_PDP10: return "Digital Equipment Corp. PDP-10";
2814 case EM_PDP11: return "Digital Equipment Corp. PDP-11";
2815 case EM_FX66: return "Siemens FX66 microcontroller";
2816 case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 bit microcontroller";
2817 case EM_ST7: return "STMicroelectronics ST7 8-bit microcontroller";
2818 case EM_68HC16: return "Motorola MC68HC16 Microcontroller";
2819 /* 70 */
2820 case EM_68HC11: return "Motorola MC68HC11 Microcontroller";
2821 case EM_68HC08: return "Motorola MC68HC08 Microcontroller";
2822 case EM_68HC05: return "Motorola MC68HC05 Microcontroller";
2823 case EM_SVX: return "Silicon Graphics SVx";
2824 case EM_ST19: return "STMicroelectronics ST19 8-bit microcontroller";
2825 case EM_VAX: return "Digital VAX";
2826 case EM_CRIS: return "Axis Communications 32-bit embedded processor";
2827 case EM_JAVELIN: return "Infineon Technologies 32-bit embedded cpu";
2828 case EM_FIREPATH: return "Element 14 64-bit DSP processor";
2829 case EM_ZSP: return "LSI Logic's 16-bit DSP processor";
2830 /* 80 */
2831 case EM_MMIX: return "Donald Knuth's educational 64-bit processor";
2832 case EM_HUANY: return "Harvard Universitys's machine-independent object format";
2833 case EM_PRISM: return "Vitesse Prism";
2834 case EM_AVR_OLD:
2835 case EM_AVR: return "Atmel AVR 8-bit microcontroller";
2836 case EM_CYGNUS_FR30:
2837 case EM_FR30: return "Fujitsu FR30";
2838 case EM_CYGNUS_D10V:
2839 case EM_D10V: return "d10v";
2840 case EM_CYGNUS_D30V:
2841 case EM_D30V: return "d30v";
2842 case EM_CYGNUS_V850:
2843 case EM_V850: return "Renesas V850";
2844 case EM_CYGNUS_M32R:
2845 case EM_M32R: return "Renesas M32R (formerly Mitsubishi M32r)";
2846 case EM_CYGNUS_MN10300:
2847 case EM_MN10300: return "mn10300";
2848 /* 90 */
2849 case EM_CYGNUS_MN10200:
2850 case EM_MN10200: return "mn10200";
2851 case EM_PJ: return "picoJava";
2852 case EM_OR1K: return "OpenRISC 1000";
2853 case EM_ARC_COMPACT: return "ARCompact";
2854 case EM_XTENSA_OLD:
2855 case EM_XTENSA: return "Tensilica Xtensa Processor";
2856 case EM_VIDEOCORE: return "Alphamosaic VideoCore processor";
2857 case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor";
2858 case EM_NS32K: return "National Semiconductor 32000 series";
2859 case EM_TPC: return "Tenor Network TPC processor";
2860 case EM_SNP1K: return "Trebia SNP 1000 processor";
2861 /* 100 */
2862 case EM_ST200: return "STMicroelectronics ST200 microcontroller";
2863 case EM_IP2K_OLD:
2864 case EM_IP2K: return "Ubicom IP2xxx 8-bit microcontrollers";
2865 case EM_MAX: return "MAX Processor";
2866 case EM_CR: return "National Semiconductor CompactRISC";
2867 case EM_F2MC16: return "Fujitsu F2MC16";
2868 case EM_MSP430: return "Texas Instruments msp430 microcontroller";
2869 case EM_BLACKFIN: return "Analog Devices Blackfin";
2870 case EM_SE_C33: return "S1C33 Family of Seiko Epson processors";
2871 case EM_SEP: return "Sharp embedded microprocessor";
2872 case EM_ARCA: return "Arca RISC microprocessor";
2873 /* 110 */
2874 case EM_UNICORE: return "Unicore";
2875 case EM_EXCESS: return "eXcess 16/32/64-bit configurable embedded CPU";
2876 case EM_DXP: return "Icera Semiconductor Inc. Deep Execution Processor";
2877 case EM_ALTERA_NIOS2: return "Altera Nios II";
2878 case EM_CRX: return "National Semiconductor CRX microprocessor";
2879 case EM_XGATE: return "Motorola XGATE embedded processor";
2880 case EM_C166:
2881 case EM_XC16X: return "Infineon Technologies xc16x";
2882 case EM_M16C: return "Renesas M16C series microprocessors";
2883 case EM_DSPIC30F: return "Microchip Technology dsPIC30F Digital Signal Controller";
2884 case EM_CE: return "Freescale Communication Engine RISC core";
2885 /* 120 */
2886 case EM_M32C: return "Renesas M32c";
2887 /* 130 */
2888 case EM_TSK3000: return "Altium TSK3000 core";
2889 case EM_RS08: return "Freescale RS08 embedded processor";
2890 case EM_ECOG2: return "Cyan Technology eCOG2 microprocessor";
2891 case EM_SCORE: return "SUNPLUS S+Core";
2892 case EM_DSP24: return "New Japan Radio (NJR) 24-bit DSP Processor";
2893 case EM_VIDEOCORE3: return "Broadcom VideoCore III processor";
2894 case EM_LATTICEMICO32: return "Lattice Mico32";
2895 case EM_SE_C17: return "Seiko Epson C17 family";
2896 /* 140 */
2897 case EM_TI_C6000: return "Texas Instruments TMS320C6000 DSP family";
2898 case EM_TI_C2000: return "Texas Instruments TMS320C2000 DSP family";
2899 case EM_TI_C5500: return "Texas Instruments TMS320C55x DSP family";
2900 case EM_TI_PRU: return "TI PRU I/O processor";
2901 /* 160 */
2902 case EM_MMDSP_PLUS: return "STMicroelectronics 64bit VLIW Data Signal Processor";
2903 case EM_CYPRESS_M8C: return "Cypress M8C microprocessor";
2904 case EM_R32C: return "Renesas R32C series microprocessors";
2905 case EM_TRIMEDIA: return "NXP Semiconductors TriMedia architecture family";
2906 case EM_QDSP6: return "QUALCOMM DSP6 Processor";
2907 case EM_8051: return "Intel 8051 and variants";
2908 case EM_STXP7X: return "STMicroelectronics STxP7x family";
2909 case EM_NDS32: return "Andes Technology compact code size embedded RISC processor family";
2910 case EM_ECOG1X: return "Cyan Technology eCOG1X family";
2911 case EM_MAXQ30: return "Dallas Semiconductor MAXQ30 Core microcontrollers";
2912 /* 170 */
2913 case EM_XIMO16: return "New Japan Radio (NJR) 16-bit DSP Processor";
2914 case EM_MANIK: return "M2000 Reconfigurable RISC Microprocessor";
2915 case EM_CRAYNV2: return "Cray Inc. NV2 vector architecture";
2916 case EM_RX: return "Renesas RX";
2917 case EM_METAG: return "Imagination Technologies Meta processor architecture";
2918 case EM_MCST_ELBRUS: return "MCST Elbrus general purpose hardware architecture";
2919 case EM_ECOG16: return "Cyan Technology eCOG16 family";
2920 case EM_CR16:
2921 case EM_MICROBLAZE:
2922 case EM_MICROBLAZE_OLD: return "Xilinx MicroBlaze";
2923 case EM_ETPU: return "Freescale Extended Time Processing Unit";
2924 case EM_SLE9X: return "Infineon Technologies SLE9X core";
2925 /* 180 */
2926 case EM_L1OM: return "Intel L1OM";
2927 case EM_K1OM: return "Intel K1OM";
2928 case EM_INTEL182: return "Intel (reserved)";
2929 case EM_AARCH64: return "AArch64";
2930 case EM_ARM184: return "ARM (reserved)";
2931 case EM_AVR32: return "Atmel Corporation 32-bit microprocessor";
2932 case EM_STM8: return "STMicroeletronics STM8 8-bit microcontroller";
2933 case EM_TILE64: return "Tilera TILE64 multicore architecture family";
2934 case EM_TILEPRO: return "Tilera TILEPro multicore architecture family";
2935 /* 190 */
2936 case EM_CUDA: return "NVIDIA CUDA architecture";
2937 case EM_TILEGX: return "Tilera TILE-Gx multicore architecture family";
2938 case EM_CLOUDSHIELD: return "CloudShield architecture family";
2939 case EM_COREA_1ST: return "KIPO-KAIST Core-A 1st generation processor family";
2940 case EM_COREA_2ND: return "KIPO-KAIST Core-A 2nd generation processor family";
2941 case EM_ARC_COMPACT2: return "ARCv2";
2942 case EM_OPEN8: return "Open8 8-bit RISC soft processor core";
2943 case EM_RL78: return "Renesas RL78";
2944 case EM_VIDEOCORE5: return "Broadcom VideoCore V processor";
2945 case EM_78K0R: return "Renesas 78K0R";
2946 /* 200 */
2947 case EM_56800EX: return "Freescale 56800EX Digital Signal Controller (DSC)";
2948 case EM_BA1: return "Beyond BA1 CPU architecture";
2949 case EM_BA2: return "Beyond BA2 CPU architecture";
2950 case EM_XCORE: return "XMOS xCORE processor family";
2951 case EM_MCHP_PIC: return "Microchip 8-bit PIC(r) family";
2952 case EM_INTELGT: return "Intel Graphics Technology";
2953 /* 210 */
2954 case EM_KM32: return "KM211 KM32 32-bit processor";
2955 case EM_KMX32: return "KM211 KMX32 32-bit processor";
2956 case EM_KMX16: return "KM211 KMX16 16-bit processor";
2957 case EM_KMX8: return "KM211 KMX8 8-bit processor";
2958 case EM_KVARC: return "KM211 KVARC processor";
2959 case EM_CDP: return "Paneve CDP architecture family";
2960 case EM_COGE: return "Cognitive Smart Memory Processor";
2961 case EM_COOL: return "Bluechip Systems CoolEngine";
2962 case EM_NORC: return "Nanoradio Optimized RISC";
2963 case EM_CSR_KALIMBA: return "CSR Kalimba architecture family";
2964 /* 220 */
2965 case EM_Z80: return "Zilog Z80";
2966 case EM_VISIUM: return "CDS VISIUMcore processor";
2967 case EM_FT32: return "FTDI Chip FT32";
2968 case EM_MOXIE: return "Moxie";
2969 case EM_AMDGPU: return "AMD GPU";
2970 /* 230 (all reserved) */
2971 /* 240 */
2972 case EM_RISCV: return "RISC-V";
2973 case EM_LANAI: return "Lanai 32-bit processor";
2974 case EM_CEVA: return "CEVA Processor Architecture Family";
2975 case EM_CEVA_X2: return "CEVA X2 Processor Family";
2976 case EM_BPF: return "Linux BPF";
2977 case EM_GRAPHCORE_IPU: return "Graphcore Intelligent Processing Unit";
2978 case EM_IMG1: return "Imagination Technologies";
2979 /* 250 */
2980 case EM_NFP: return "Netronome Flow Processor";
2981 case EM_VE: return "NEC Vector Engine";
2982 case EM_CSKY: return "C-SKY";
2983 case EM_ARC_COMPACT3_64: return "Synopsys ARCv2.3 64-bit";
2984 case EM_MCS6502: return "MOS Technology MCS 6502 processor";
2985 case EM_ARC_COMPACT3: return "Synopsys ARCv2.3 32-bit";
2986 case EM_KVX: return "Kalray VLIW core of the MPPA processor family";
2987 case EM_65816: return "WDC 65816/65C816";
2988 case EM_LOONGARCH: return "LoongArch";
2989 case EM_KF32: return "ChipON KungFu32";
2990
2991 /* Large numbers... */
2992 case EM_MT: return "Morpho Techologies MT processor";
2993 case EM_ALPHA: return "Alpha";
2994 case EM_WEBASSEMBLY: return "Web Assembly";
2995 case EM_DLX: return "OpenDLX";
2996 case EM_XSTORMY16: return "Sanyo XStormy16 CPU core";
2997 case EM_IQ2000: return "Vitesse IQ2000";
2998 case EM_M32C_OLD:
2999 case EM_NIOS32: return "Altera Nios";
3000 case EM_CYGNUS_MEP: return "Toshiba MeP Media Engine";
3001 case EM_ADAPTEVA_EPIPHANY: return "Adapteva EPIPHANY";
3002 case EM_CYGNUS_FRV: return "Fujitsu FR-V";
3003 case EM_S12Z: return "Freescale S12Z";
3004
3005 default:
3006 snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
3007 return buff;
3008 }
3009 }
3010
3011 static void
3012 decode_ARC_machine_flags (unsigned e_flags, unsigned e_machine, char buf[])
3013 {
3014 /* ARC has two machine types EM_ARC_COMPACT and EM_ARC_COMPACT2. Some
3015 other compilers don't specify an architecture type in the e_flags, and
3016 instead use EM_ARC_COMPACT for old ARC600, ARC601, and ARC700
3017 architectures, and switch to EM_ARC_COMPACT2 for newer ARCEM and ARCHS
3018 architectures.
3019
3020 Th GNU tools follows this use of EM_ARC_COMPACT and EM_ARC_COMPACT2,
3021 but also sets a specific architecture type in the e_flags field.
3022
3023 However, when decoding the flags we don't worry if we see an
3024 unexpected pairing, for example EM_ARC_COMPACT machine type, with
3025 ARCEM architecture type. */
3026
3027 switch (e_flags & EF_ARC_MACH_MSK)
3028 {
3029 /* We only expect these to occur for EM_ARC_COMPACT2. */
3030 case EF_ARC_CPU_ARCV2EM:
3031 strcat (buf, ", ARC EM");
3032 break;
3033 case EF_ARC_CPU_ARCV2HS:
3034 strcat (buf, ", ARC HS");
3035 break;
3036
3037 /* We only expect these to occur for EM_ARC_COMPACT. */
3038 case E_ARC_MACH_ARC600:
3039 strcat (buf, ", ARC600");
3040 break;
3041 case E_ARC_MACH_ARC601:
3042 strcat (buf, ", ARC601");
3043 break;
3044 case E_ARC_MACH_ARC700:
3045 strcat (buf, ", ARC700");
3046 break;
3047
3048 /* The only times we should end up here are (a) A corrupt ELF, (b) A
3049 new ELF with new architecture being read by an old version of
3050 readelf, or (c) An ELF built with non-GNU compiler that does not
3051 set the architecture in the e_flags. */
3052 default:
3053 if (e_machine == EM_ARC_COMPACT)
3054 strcat (buf, ", Unknown ARCompact");
3055 else
3056 strcat (buf, ", Unknown ARC");
3057 break;
3058 }
3059
3060 switch (e_flags & EF_ARC_OSABI_MSK)
3061 {
3062 case E_ARC_OSABI_ORIG:
3063 strcat (buf, ", (ABI:legacy)");
3064 break;
3065 case E_ARC_OSABI_V2:
3066 strcat (buf, ", (ABI:v2)");
3067 break;
3068 /* Only upstream 3.9+ kernels will support ARCv2 ISA. */
3069 case E_ARC_OSABI_V3:
3070 strcat (buf, ", v3 no-legacy-syscalls ABI");
3071 break;
3072 case E_ARC_OSABI_V4:
3073 strcat (buf, ", v4 ABI");
3074 break;
3075 default:
3076 strcat (buf, ", unrecognised ARC OSABI flag");
3077 break;
3078 }
3079 }
3080
3081 static void
3082 decode_ARM_machine_flags (unsigned e_flags, char buf[])
3083 {
3084 unsigned eabi;
3085 bool unknown = false;
3086
3087 eabi = EF_ARM_EABI_VERSION (e_flags);
3088 e_flags &= ~ EF_ARM_EABIMASK;
3089
3090 /* Handle "generic" ARM flags. */
3091 if (e_flags & EF_ARM_RELEXEC)
3092 {
3093 strcat (buf, ", relocatable executable");
3094 e_flags &= ~ EF_ARM_RELEXEC;
3095 }
3096
3097 if (e_flags & EF_ARM_PIC)
3098 {
3099 strcat (buf, ", position independent");
3100 e_flags &= ~ EF_ARM_PIC;
3101 }
3102
3103 /* Now handle EABI specific flags. */
3104 switch (eabi)
3105 {
3106 default:
3107 strcat (buf, ", <unrecognized EABI>");
3108 if (e_flags)
3109 unknown = true;
3110 break;
3111
3112 case EF_ARM_EABI_VER1:
3113 strcat (buf, ", Version1 EABI");
3114 while (e_flags)
3115 {
3116 unsigned flag;
3117
3118 /* Process flags one bit at a time. */
3119 flag = e_flags & - e_flags;
3120 e_flags &= ~ flag;
3121
3122 switch (flag)
3123 {
3124 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
3125 strcat (buf, ", sorted symbol tables");
3126 break;
3127
3128 default:
3129 unknown = true;
3130 break;
3131 }
3132 }
3133 break;
3134
3135 case EF_ARM_EABI_VER2:
3136 strcat (buf, ", Version2 EABI");
3137 while (e_flags)
3138 {
3139 unsigned flag;
3140
3141 /* Process flags one bit at a time. */
3142 flag = e_flags & - e_flags;
3143 e_flags &= ~ flag;
3144
3145 switch (flag)
3146 {
3147 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
3148 strcat (buf, ", sorted symbol tables");
3149 break;
3150
3151 case EF_ARM_DYNSYMSUSESEGIDX:
3152 strcat (buf, ", dynamic symbols use segment index");
3153 break;
3154
3155 case EF_ARM_MAPSYMSFIRST:
3156 strcat (buf, ", mapping symbols precede others");
3157 break;
3158
3159 default:
3160 unknown = true;
3161 break;
3162 }
3163 }
3164 break;
3165
3166 case EF_ARM_EABI_VER3:
3167 strcat (buf, ", Version3 EABI");
3168 break;
3169
3170 case EF_ARM_EABI_VER4:
3171 strcat (buf, ", Version4 EABI");
3172 while (e_flags)
3173 {
3174 unsigned flag;
3175
3176 /* Process flags one bit at a time. */
3177 flag = e_flags & - e_flags;
3178 e_flags &= ~ flag;
3179
3180 switch (flag)
3181 {
3182 case EF_ARM_BE8:
3183 strcat (buf, ", BE8");
3184 break;
3185
3186 case EF_ARM_LE8:
3187 strcat (buf, ", LE8");
3188 break;
3189
3190 default:
3191 unknown = true;
3192 break;
3193 }
3194 }
3195 break;
3196
3197 case EF_ARM_EABI_VER5:
3198 strcat (buf, ", Version5 EABI");
3199 while (e_flags)
3200 {
3201 unsigned flag;
3202
3203 /* Process flags one bit at a time. */
3204 flag = e_flags & - e_flags;
3205 e_flags &= ~ flag;
3206
3207 switch (flag)
3208 {
3209 case EF_ARM_BE8:
3210 strcat (buf, ", BE8");
3211 break;
3212
3213 case EF_ARM_LE8:
3214 strcat (buf, ", LE8");
3215 break;
3216
3217 case EF_ARM_ABI_FLOAT_SOFT: /* Conflicts with EF_ARM_SOFT_FLOAT. */
3218 strcat (buf, ", soft-float ABI");
3219 break;
3220
3221 case EF_ARM_ABI_FLOAT_HARD: /* Conflicts with EF_ARM_VFP_FLOAT. */
3222 strcat (buf, ", hard-float ABI");
3223 break;
3224
3225 default:
3226 unknown = true;
3227 break;
3228 }
3229 }
3230 break;
3231
3232 case EF_ARM_EABI_UNKNOWN:
3233 strcat (buf, ", GNU EABI");
3234 while (e_flags)
3235 {
3236 unsigned flag;
3237
3238 /* Process flags one bit at a time. */
3239 flag = e_flags & - e_flags;
3240 e_flags &= ~ flag;
3241
3242 switch (flag)
3243 {
3244 case EF_ARM_INTERWORK:
3245 strcat (buf, ", interworking enabled");
3246 break;
3247
3248 case EF_ARM_APCS_26:
3249 strcat (buf, ", uses APCS/26");
3250 break;
3251
3252 case EF_ARM_APCS_FLOAT:
3253 strcat (buf, ", uses APCS/float");
3254 break;
3255
3256 case EF_ARM_PIC:
3257 strcat (buf, ", position independent");
3258 break;
3259
3260 case EF_ARM_ALIGN8:
3261 strcat (buf, ", 8 bit structure alignment");
3262 break;
3263
3264 case EF_ARM_NEW_ABI:
3265 strcat (buf, ", uses new ABI");
3266 break;
3267
3268 case EF_ARM_OLD_ABI:
3269 strcat (buf, ", uses old ABI");
3270 break;
3271
3272 case EF_ARM_SOFT_FLOAT:
3273 strcat (buf, ", software FP");
3274 break;
3275
3276 case EF_ARM_VFP_FLOAT:
3277 strcat (buf, ", VFP");
3278 break;
3279
3280 case EF_ARM_MAVERICK_FLOAT:
3281 strcat (buf, ", Maverick FP");
3282 break;
3283
3284 default:
3285 unknown = true;
3286 break;
3287 }
3288 }
3289 }
3290
3291 if (unknown)
3292 strcat (buf,_(", <unknown>"));
3293 }
3294
3295 static void
3296 decode_AVR_machine_flags (unsigned e_flags, char buf[], size_t size)
3297 {
3298 --size; /* Leave space for null terminator. */
3299
3300 switch (e_flags & EF_AVR_MACH)
3301 {
3302 case E_AVR_MACH_AVR1:
3303 strncat (buf, ", avr:1", size);
3304 break;
3305 case E_AVR_MACH_AVR2:
3306 strncat (buf, ", avr:2", size);
3307 break;
3308 case E_AVR_MACH_AVR25:
3309 strncat (buf, ", avr:25", size);
3310 break;
3311 case E_AVR_MACH_AVR3:
3312 strncat (buf, ", avr:3", size);
3313 break;
3314 case E_AVR_MACH_AVR31:
3315 strncat (buf, ", avr:31", size);
3316 break;
3317 case E_AVR_MACH_AVR35:
3318 strncat (buf, ", avr:35", size);
3319 break;
3320 case E_AVR_MACH_AVR4:
3321 strncat (buf, ", avr:4", size);
3322 break;
3323 case E_AVR_MACH_AVR5:
3324 strncat (buf, ", avr:5", size);
3325 break;
3326 case E_AVR_MACH_AVR51:
3327 strncat (buf, ", avr:51", size);
3328 break;
3329 case E_AVR_MACH_AVR6:
3330 strncat (buf, ", avr:6", size);
3331 break;
3332 case E_AVR_MACH_AVRTINY:
3333 strncat (buf, ", avr:100", size);
3334 break;
3335 case E_AVR_MACH_XMEGA1:
3336 strncat (buf, ", avr:101", size);
3337 break;
3338 case E_AVR_MACH_XMEGA2:
3339 strncat (buf, ", avr:102", size);
3340 break;
3341 case E_AVR_MACH_XMEGA3:
3342 strncat (buf, ", avr:103", size);
3343 break;
3344 case E_AVR_MACH_XMEGA4:
3345 strncat (buf, ", avr:104", size);
3346 break;
3347 case E_AVR_MACH_XMEGA5:
3348 strncat (buf, ", avr:105", size);
3349 break;
3350 case E_AVR_MACH_XMEGA6:
3351 strncat (buf, ", avr:106", size);
3352 break;
3353 case E_AVR_MACH_XMEGA7:
3354 strncat (buf, ", avr:107", size);
3355 break;
3356 default:
3357 strncat (buf, ", avr:<unknown>", size);
3358 break;
3359 }
3360
3361 size -= strlen (buf);
3362 if (e_flags & EF_AVR_LINKRELAX_PREPARED)
3363 strncat (buf, ", link-relax", size);
3364 }
3365
3366 static void
3367 decode_NDS32_machine_flags (unsigned e_flags, char buf[], size_t size)
3368 {
3369 unsigned abi;
3370 unsigned arch;
3371 unsigned config;
3372 unsigned version;
3373 bool has_fpu = false;
3374 unsigned int r = 0;
3375
3376 static const char *ABI_STRINGS[] =
3377 {
3378 "ABI v0", /* use r5 as return register; only used in N1213HC */
3379 "ABI v1", /* use r0 as return register */
3380 "ABI v2", /* use r0 as return register and don't reserve 24 bytes for arguments */
3381 "ABI v2fp", /* for FPU */
3382 "AABI",
3383 "ABI2 FP+"
3384 };
3385 static const char *VER_STRINGS[] =
3386 {
3387 "Andes ELF V1.3 or older",
3388 "Andes ELF V1.3.1",
3389 "Andes ELF V1.4"
3390 };
3391 static const char *ARCH_STRINGS[] =
3392 {
3393 "",
3394 "Andes Star v1.0",
3395 "Andes Star v2.0",
3396 "Andes Star v3.0",
3397 "Andes Star v3.0m"
3398 };
3399
3400 abi = EF_NDS_ABI & e_flags;
3401 arch = EF_NDS_ARCH & e_flags;
3402 config = EF_NDS_INST & e_flags;
3403 version = EF_NDS32_ELF_VERSION & e_flags;
3404
3405 memset (buf, 0, size);
3406
3407 switch (abi)
3408 {
3409 case E_NDS_ABI_V0:
3410 case E_NDS_ABI_V1:
3411 case E_NDS_ABI_V2:
3412 case E_NDS_ABI_V2FP:
3413 case E_NDS_ABI_AABI:
3414 case E_NDS_ABI_V2FP_PLUS:
3415 /* In case there are holes in the array. */
3416 r += snprintf (buf + r, size - r, ", %s", ABI_STRINGS[abi >> EF_NDS_ABI_SHIFT]);
3417 break;
3418
3419 default:
3420 r += snprintf (buf + r, size - r, ", <unrecognized ABI>");
3421 break;
3422 }
3423
3424 switch (version)
3425 {
3426 case E_NDS32_ELF_VER_1_2:
3427 case E_NDS32_ELF_VER_1_3:
3428 case E_NDS32_ELF_VER_1_4:
3429 r += snprintf (buf + r, size - r, ", %s", VER_STRINGS[version >> EF_NDS32_ELF_VERSION_SHIFT]);
3430 break;
3431
3432 default:
3433 r += snprintf (buf + r, size - r, ", <unrecognized ELF version number>");
3434 break;
3435 }
3436
3437 if (E_NDS_ABI_V0 == abi)
3438 {
3439 /* OLD ABI; only used in N1213HC, has performance extension 1. */
3440 r += snprintf (buf + r, size - r, ", Andes Star v1.0, N1213HC, MAC, PERF1");
3441 if (arch == E_NDS_ARCH_STAR_V1_0)
3442 r += snprintf (buf + r, size -r, ", 16b"); /* has 16-bit instructions */
3443 return;
3444 }
3445
3446 switch (arch)
3447 {
3448 case E_NDS_ARCH_STAR_V1_0:
3449 case E_NDS_ARCH_STAR_V2_0:
3450 case E_NDS_ARCH_STAR_V3_0:
3451 case E_NDS_ARCH_STAR_V3_M:
3452 r += snprintf (buf + r, size - r, ", %s", ARCH_STRINGS[arch >> EF_NDS_ARCH_SHIFT]);
3453 break;
3454
3455 default:
3456 r += snprintf (buf + r, size - r, ", <unrecognized architecture>");
3457 /* ARCH version determines how the e_flags are interpreted.
3458 If it is unknown, we cannot proceed. */
3459 return;
3460 }
3461
3462 /* Newer ABI; Now handle architecture specific flags. */
3463 if (arch == E_NDS_ARCH_STAR_V1_0)
3464 {
3465 if (config & E_NDS32_HAS_MFUSR_PC_INST)
3466 r += snprintf (buf + r, size -r, ", MFUSR_PC");
3467
3468 if (!(config & E_NDS32_HAS_NO_MAC_INST))
3469 r += snprintf (buf + r, size -r, ", MAC");
3470
3471 if (config & E_NDS32_HAS_DIV_INST)
3472 r += snprintf (buf + r, size -r, ", DIV");
3473
3474 if (config & E_NDS32_HAS_16BIT_INST)
3475 r += snprintf (buf + r, size -r, ", 16b");
3476 }
3477 else
3478 {
3479 if (config & E_NDS32_HAS_MFUSR_PC_INST)
3480 {
3481 if (version <= E_NDS32_ELF_VER_1_3)
3482 r += snprintf (buf + r, size -r, ", [B8]");
3483 else
3484 r += snprintf (buf + r, size -r, ", EX9");
3485 }
3486
3487 if (config & E_NDS32_HAS_MAC_DX_INST)
3488 r += snprintf (buf + r, size -r, ", MAC_DX");
3489
3490 if (config & E_NDS32_HAS_DIV_DX_INST)
3491 r += snprintf (buf + r, size -r, ", DIV_DX");
3492
3493 if (config & E_NDS32_HAS_16BIT_INST)
3494 {
3495 if (version <= E_NDS32_ELF_VER_1_3)
3496 r += snprintf (buf + r, size -r, ", 16b");
3497 else
3498 r += snprintf (buf + r, size -r, ", IFC");
3499 }
3500 }
3501
3502 if (config & E_NDS32_HAS_EXT_INST)
3503 r += snprintf (buf + r, size -r, ", PERF1");
3504
3505 if (config & E_NDS32_HAS_EXT2_INST)
3506 r += snprintf (buf + r, size -r, ", PERF2");
3507
3508 if (config & E_NDS32_HAS_FPU_INST)
3509 {
3510 has_fpu = true;
3511 r += snprintf (buf + r, size -r, ", FPU_SP");
3512 }
3513
3514 if (config & E_NDS32_HAS_FPU_DP_INST)
3515 {
3516 has_fpu = true;
3517 r += snprintf (buf + r, size -r, ", FPU_DP");
3518 }
3519
3520 if (config & E_NDS32_HAS_FPU_MAC_INST)
3521 {
3522 has_fpu = true;
3523 r += snprintf (buf + r, size -r, ", FPU_MAC");
3524 }
3525
3526 if (has_fpu)
3527 {
3528 switch ((config & E_NDS32_FPU_REG_CONF) >> E_NDS32_FPU_REG_CONF_SHIFT)
3529 {
3530 case E_NDS32_FPU_REG_8SP_4DP:
3531 r += snprintf (buf + r, size -r, ", FPU_REG:8/4");
3532 break;
3533 case E_NDS32_FPU_REG_16SP_8DP:
3534 r += snprintf (buf + r, size -r, ", FPU_REG:16/8");
3535 break;
3536 case E_NDS32_FPU_REG_32SP_16DP:
3537 r += snprintf (buf + r, size -r, ", FPU_REG:32/16");
3538 break;
3539 case E_NDS32_FPU_REG_32SP_32DP:
3540 r += snprintf (buf + r, size -r, ", FPU_REG:32/32");
3541 break;
3542 }
3543 }
3544
3545 if (config & E_NDS32_HAS_AUDIO_INST)
3546 r += snprintf (buf + r, size -r, ", AUDIO");
3547
3548 if (config & E_NDS32_HAS_STRING_INST)
3549 r += snprintf (buf + r, size -r, ", STR");
3550
3551 if (config & E_NDS32_HAS_REDUCED_REGS)
3552 r += snprintf (buf + r, size -r, ", 16REG");
3553
3554 if (config & E_NDS32_HAS_VIDEO_INST)
3555 {
3556 if (version <= E_NDS32_ELF_VER_1_3)
3557 r += snprintf (buf + r, size -r, ", VIDEO");
3558 else
3559 r += snprintf (buf + r, size -r, ", SATURATION");
3560 }
3561
3562 if (config & E_NDS32_HAS_ENCRIPT_INST)
3563 r += snprintf (buf + r, size -r, ", ENCRP");
3564
3565 if (config & E_NDS32_HAS_L2C_INST)
3566 r += snprintf (buf + r, size -r, ", L2C");
3567 }
3568
3569 static void
3570 decode_AMDGPU_machine_flags (Filedata *filedata, unsigned int e_flags,
3571 char *buf)
3572 {
3573 unsigned char *e_ident = filedata->file_header.e_ident;
3574 unsigned char osabi = e_ident[EI_OSABI];
3575 unsigned char abiversion = e_ident[EI_ABIVERSION];
3576 unsigned int mach;
3577
3578 /* HSA OS ABI v2 used a different encoding, but we don't need to support it,
3579 it has been deprecated for a while.
3580
3581 The PAL, MESA3D and NONE OS ABIs are not properly versioned, at the time
3582 of writing, they use the same flags as HSA v3, so the code below uses that
3583 assumption. */
3584 if (osabi == ELFOSABI_AMDGPU_HSA && abiversion < ELFABIVERSION_AMDGPU_HSA_V3)
3585 return;
3586
3587 mach = e_flags & EF_AMDGPU_MACH;
3588 switch (mach)
3589 {
3590 #define AMDGPU_CASE(code, string) \
3591 case code: strcat (buf, ", " string); break;
3592 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX600, "gfx600")
3593 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX601, "gfx601")
3594 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX700, "gfx700")
3595 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX701, "gfx701")
3596 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX702, "gfx702")
3597 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX703, "gfx703")
3598 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX704, "gfx704")
3599 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX801, "gfx801")
3600 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX802, "gfx802")
3601 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX803, "gfx803")
3602 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX810, "gfx810")
3603 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX900, "gfx900")
3604 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX902, "gfx902")
3605 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX904, "gfx904")
3606 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX906, "gfx906")
3607 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX908, "gfx908")
3608 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX909, "gfx909")
3609 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX90C, "gfx90c")
3610 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1010, "gfx1010")
3611 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1011, "gfx1011")
3612 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1012, "gfx1012")
3613 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1030, "gfx1030")
3614 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1031, "gfx1031")
3615 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1032, "gfx1032")
3616 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1033, "gfx1033")
3617 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX602, "gfx602")
3618 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX705, "gfx705")
3619 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX805, "gfx805")
3620 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1035, "gfx1035")
3621 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1034, "gfx1034")
3622 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX90A, "gfx90a")
3623 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX940, "gfx940")
3624 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1013, "gfx1013")
3625 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1036, "gfx1036")
3626 default:
3627 sprintf (buf, _(", <unknown AMDGPU GPU type: %#x>"), mach);
3628 break;
3629 #undef AMDGPU_CASE
3630 }
3631
3632 buf += strlen (buf);
3633 e_flags &= ~EF_AMDGPU_MACH;
3634
3635 if ((osabi == ELFOSABI_AMDGPU_HSA
3636 && abiversion == ELFABIVERSION_AMDGPU_HSA_V3)
3637 || osabi != ELFOSABI_AMDGPU_HSA)
3638 {
3639 /* For HSA v3 and other OS ABIs. */
3640 if (e_flags & EF_AMDGPU_FEATURE_XNACK_V3)
3641 {
3642 strcat (buf, ", xnack on");
3643 buf += strlen (buf);
3644 e_flags &= ~EF_AMDGPU_FEATURE_XNACK_V3;
3645 }
3646
3647 if (e_flags & EF_AMDGPU_FEATURE_SRAMECC_V3)
3648 {
3649 strcat (buf, ", sramecc on");
3650 buf += strlen (buf);
3651 e_flags &= ~EF_AMDGPU_FEATURE_SRAMECC_V3;
3652 }
3653 }
3654 else
3655 {
3656 /* For HSA v4+. */
3657 int xnack, sramecc;
3658
3659 xnack = e_flags & EF_AMDGPU_FEATURE_XNACK_V4;
3660 switch (xnack)
3661 {
3662 case EF_AMDGPU_FEATURE_XNACK_UNSUPPORTED_V4:
3663 break;
3664
3665 case EF_AMDGPU_FEATURE_XNACK_ANY_V4:
3666 strcat (buf, ", xnack any");
3667 break;
3668
3669 case EF_AMDGPU_FEATURE_XNACK_OFF_V4:
3670 strcat (buf, ", xnack off");
3671 break;
3672
3673 case EF_AMDGPU_FEATURE_XNACK_ON_V4:
3674 strcat (buf, ", xnack on");
3675 break;
3676
3677 default:
3678 sprintf (buf, _(", <unknown xnack value: %#x>"), xnack);
3679 break;
3680 }
3681
3682 buf += strlen (buf);
3683 e_flags &= ~EF_AMDGPU_FEATURE_XNACK_V4;
3684
3685 sramecc = e_flags & EF_AMDGPU_FEATURE_SRAMECC_V4;
3686 switch (sramecc)
3687 {
3688 case EF_AMDGPU_FEATURE_SRAMECC_UNSUPPORTED_V4:
3689 break;
3690
3691 case EF_AMDGPU_FEATURE_SRAMECC_ANY_V4:
3692 strcat (buf, ", sramecc any");
3693 break;
3694
3695 case EF_AMDGPU_FEATURE_SRAMECC_OFF_V4:
3696 strcat (buf, ", sramecc off");
3697 break;
3698
3699 case EF_AMDGPU_FEATURE_SRAMECC_ON_V4:
3700 strcat (buf, ", sramecc on");
3701 break;
3702
3703 default:
3704 sprintf (buf, _(", <unknown sramecc value: %#x>"), sramecc);
3705 break;
3706 }
3707
3708 buf += strlen (buf);
3709 e_flags &= ~EF_AMDGPU_FEATURE_SRAMECC_V4;
3710 }
3711
3712 if (e_flags != 0)
3713 sprintf (buf, _(", unknown flags bits: %#x"), e_flags);
3714 }
3715
3716 static char *
3717 get_machine_flags (Filedata * filedata, unsigned e_flags, unsigned e_machine)
3718 {
3719 static char buf[1024];
3720
3721 buf[0] = '\0';
3722
3723 if (e_flags)
3724 {
3725 switch (e_machine)
3726 {
3727 default:
3728 break;
3729
3730 case EM_ARC_COMPACT2:
3731 case EM_ARC_COMPACT:
3732 decode_ARC_machine_flags (e_flags, e_machine, buf);
3733 break;
3734
3735 case EM_ARM:
3736 decode_ARM_machine_flags (e_flags, buf);
3737 break;
3738
3739 case EM_AVR:
3740 decode_AVR_machine_flags (e_flags, buf, sizeof buf);
3741 break;
3742
3743 case EM_BLACKFIN:
3744 if (e_flags & EF_BFIN_PIC)
3745 strcat (buf, ", PIC");
3746
3747 if (e_flags & EF_BFIN_FDPIC)
3748 strcat (buf, ", FDPIC");
3749
3750 if (e_flags & EF_BFIN_CODE_IN_L1)
3751 strcat (buf, ", code in L1");
3752
3753 if (e_flags & EF_BFIN_DATA_IN_L1)
3754 strcat (buf, ", data in L1");
3755
3756 break;
3757
3758 case EM_CYGNUS_FRV:
3759 switch (e_flags & EF_FRV_CPU_MASK)
3760 {
3761 case EF_FRV_CPU_GENERIC:
3762 break;
3763
3764 default:
3765 strcat (buf, ", fr???");
3766 break;
3767
3768 case EF_FRV_CPU_FR300:
3769 strcat (buf, ", fr300");
3770 break;
3771
3772 case EF_FRV_CPU_FR400:
3773 strcat (buf, ", fr400");
3774 break;
3775 case EF_FRV_CPU_FR405:
3776 strcat (buf, ", fr405");
3777 break;
3778
3779 case EF_FRV_CPU_FR450:
3780 strcat (buf, ", fr450");
3781 break;
3782
3783 case EF_FRV_CPU_FR500:
3784 strcat (buf, ", fr500");
3785 break;
3786 case EF_FRV_CPU_FR550:
3787 strcat (buf, ", fr550");
3788 break;
3789
3790 case EF_FRV_CPU_SIMPLE:
3791 strcat (buf, ", simple");
3792 break;
3793 case EF_FRV_CPU_TOMCAT:
3794 strcat (buf, ", tomcat");
3795 break;
3796 }
3797 break;
3798
3799 case EM_68K:
3800 if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
3801 strcat (buf, ", m68000");
3802 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
3803 strcat (buf, ", cpu32");
3804 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
3805 strcat (buf, ", fido_a");
3806 else
3807 {
3808 char const * isa = _("unknown");
3809 char const * mac = _("unknown mac");
3810 char const * additional = NULL;
3811
3812 switch (e_flags & EF_M68K_CF_ISA_MASK)
3813 {
3814 case EF_M68K_CF_ISA_A_NODIV:
3815 isa = "A";
3816 additional = ", nodiv";
3817 break;
3818 case EF_M68K_CF_ISA_A:
3819 isa = "A";
3820 break;
3821 case EF_M68K_CF_ISA_A_PLUS:
3822 isa = "A+";
3823 break;
3824 case EF_M68K_CF_ISA_B_NOUSP:
3825 isa = "B";
3826 additional = ", nousp";
3827 break;
3828 case EF_M68K_CF_ISA_B:
3829 isa = "B";
3830 break;
3831 case EF_M68K_CF_ISA_C:
3832 isa = "C";
3833 break;
3834 case EF_M68K_CF_ISA_C_NODIV:
3835 isa = "C";
3836 additional = ", nodiv";
3837 break;
3838 }
3839 strcat (buf, ", cf, isa ");
3840 strcat (buf, isa);
3841 if (additional)
3842 strcat (buf, additional);
3843 if (e_flags & EF_M68K_CF_FLOAT)
3844 strcat (buf, ", float");
3845 switch (e_flags & EF_M68K_CF_MAC_MASK)
3846 {
3847 case 0:
3848 mac = NULL;
3849 break;
3850 case EF_M68K_CF_MAC:
3851 mac = "mac";
3852 break;
3853 case EF_M68K_CF_EMAC:
3854 mac = "emac";
3855 break;
3856 case EF_M68K_CF_EMAC_B:
3857 mac = "emac_b";
3858 break;
3859 }
3860 if (mac)
3861 {
3862 strcat (buf, ", ");
3863 strcat (buf, mac);
3864 }
3865 }
3866 break;
3867
3868 case EM_AMDGPU:
3869 decode_AMDGPU_machine_flags (filedata, e_flags, buf);
3870 break;
3871
3872 case EM_CYGNUS_MEP:
3873 switch (e_flags & EF_MEP_CPU_MASK)
3874 {
3875 case EF_MEP_CPU_MEP: strcat (buf, ", generic MeP"); break;
3876 case EF_MEP_CPU_C2: strcat (buf, ", MeP C2"); break;
3877 case EF_MEP_CPU_C3: strcat (buf, ", MeP C3"); break;
3878 case EF_MEP_CPU_C4: strcat (buf, ", MeP C4"); break;
3879 case EF_MEP_CPU_C5: strcat (buf, ", MeP C5"); break;
3880 case EF_MEP_CPU_H1: strcat (buf, ", MeP H1"); break;
3881 default: strcat (buf, _(", <unknown MeP cpu type>")); break;
3882 }
3883
3884 switch (e_flags & EF_MEP_COP_MASK)
3885 {
3886 case EF_MEP_COP_NONE: break;
3887 case EF_MEP_COP_AVC: strcat (buf, ", AVC coprocessor"); break;
3888 case EF_MEP_COP_AVC2: strcat (buf, ", AVC2 coprocessor"); break;
3889 case EF_MEP_COP_FMAX: strcat (buf, ", FMAX coprocessor"); break;
3890 case EF_MEP_COP_IVC2: strcat (buf, ", IVC2 coprocessor"); break;
3891 default: strcat (buf, _("<unknown MeP copro type>")); break;
3892 }
3893
3894 if (e_flags & EF_MEP_LIBRARY)
3895 strcat (buf, ", Built for Library");
3896
3897 if (e_flags & EF_MEP_INDEX_MASK)
3898 sprintf (buf + strlen (buf), ", Configuration Index: %#x",
3899 e_flags & EF_MEP_INDEX_MASK);
3900
3901 if (e_flags & ~ EF_MEP_ALL_FLAGS)
3902 sprintf (buf + strlen (buf), _(", unknown flags bits: %#x"),
3903 e_flags & ~ EF_MEP_ALL_FLAGS);
3904 break;
3905
3906 case EM_PPC:
3907 if (e_flags & EF_PPC_EMB)
3908 strcat (buf, ", emb");
3909
3910 if (e_flags & EF_PPC_RELOCATABLE)
3911 strcat (buf, _(", relocatable"));
3912
3913 if (e_flags & EF_PPC_RELOCATABLE_LIB)
3914 strcat (buf, _(", relocatable-lib"));
3915 break;
3916
3917 case EM_PPC64:
3918 if (e_flags & EF_PPC64_ABI)
3919 {
3920 char abi[] = ", abiv0";
3921
3922 abi[6] += e_flags & EF_PPC64_ABI;
3923 strcat (buf, abi);
3924 }
3925 break;
3926
3927 case EM_V800:
3928 if ((e_flags & EF_RH850_ABI) == EF_RH850_ABI)
3929 strcat (buf, ", RH850 ABI");
3930
3931 if (e_flags & EF_V800_850E3)
3932 strcat (buf, ", V3 architecture");
3933
3934 if ((e_flags & (EF_RH850_FPU_DOUBLE | EF_RH850_FPU_SINGLE)) == 0)
3935 strcat (buf, ", FPU not used");
3936
3937 if ((e_flags & (EF_RH850_REGMODE22 | EF_RH850_REGMODE32)) == 0)
3938 strcat (buf, ", regmode: COMMON");
3939
3940 if ((e_flags & (EF_RH850_GP_FIX | EF_RH850_GP_NOFIX)) == 0)
3941 strcat (buf, ", r4 not used");
3942
3943 if ((e_flags & (EF_RH850_EP_FIX | EF_RH850_EP_NOFIX)) == 0)
3944 strcat (buf, ", r30 not used");
3945
3946 if ((e_flags & (EF_RH850_TP_FIX | EF_RH850_TP_NOFIX)) == 0)
3947 strcat (buf, ", r5 not used");
3948
3949 if ((e_flags & (EF_RH850_REG2_RESERVE | EF_RH850_REG2_NORESERVE)) == 0)
3950 strcat (buf, ", r2 not used");
3951
3952 for (e_flags &= 0xFFFF; e_flags; e_flags &= ~ (e_flags & - e_flags))
3953 {
3954 switch (e_flags & - e_flags)
3955 {
3956 case EF_RH850_FPU_DOUBLE: strcat (buf, ", double precision FPU"); break;
3957 case EF_RH850_FPU_SINGLE: strcat (buf, ", single precision FPU"); break;
3958 case EF_RH850_REGMODE22: strcat (buf, ", regmode:22"); break;
3959 case EF_RH850_REGMODE32: strcat (buf, ", regmode:23"); break;
3960 case EF_RH850_GP_FIX: strcat (buf, ", r4 fixed"); break;
3961 case EF_RH850_GP_NOFIX: strcat (buf, ", r4 free"); break;
3962 case EF_RH850_EP_FIX: strcat (buf, ", r30 fixed"); break;
3963 case EF_RH850_EP_NOFIX: strcat (buf, ", r30 free"); break;
3964 case EF_RH850_TP_FIX: strcat (buf, ", r5 fixed"); break;
3965 case EF_RH850_TP_NOFIX: strcat (buf, ", r5 free"); break;
3966 case EF_RH850_REG2_RESERVE: strcat (buf, ", r2 fixed"); break;
3967 case EF_RH850_REG2_NORESERVE: strcat (buf, ", r2 free"); break;
3968 default: break;
3969 }
3970 }
3971 break;
3972
3973 case EM_V850:
3974 case EM_CYGNUS_V850:
3975 switch (e_flags & EF_V850_ARCH)
3976 {
3977 case E_V850E3V5_ARCH:
3978 strcat (buf, ", v850e3v5");
3979 break;
3980 case E_V850E2V3_ARCH:
3981 strcat (buf, ", v850e2v3");
3982 break;
3983 case E_V850E2_ARCH:
3984 strcat (buf, ", v850e2");
3985 break;
3986 case E_V850E1_ARCH:
3987 strcat (buf, ", v850e1");
3988 break;
3989 case E_V850E_ARCH:
3990 strcat (buf, ", v850e");
3991 break;
3992 case E_V850_ARCH:
3993 strcat (buf, ", v850");
3994 break;
3995 default:
3996 strcat (buf, _(", unknown v850 architecture variant"));
3997 break;
3998 }
3999 break;
4000
4001 case EM_M32R:
4002 case EM_CYGNUS_M32R:
4003 if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
4004 strcat (buf, ", m32r");
4005 break;
4006
4007 case EM_MIPS:
4008 case EM_MIPS_RS3_LE:
4009 if (e_flags & EF_MIPS_NOREORDER)
4010 strcat (buf, ", noreorder");
4011
4012 if (e_flags & EF_MIPS_PIC)
4013 strcat (buf, ", pic");
4014
4015 if (e_flags & EF_MIPS_CPIC)
4016 strcat (buf, ", cpic");
4017
4018 if (e_flags & EF_MIPS_UCODE)
4019 strcat (buf, ", ugen_reserved");
4020
4021 if (e_flags & EF_MIPS_ABI2)
4022 strcat (buf, ", abi2");
4023
4024 if (e_flags & EF_MIPS_OPTIONS_FIRST)
4025 strcat (buf, ", odk first");
4026
4027 if (e_flags & EF_MIPS_32BITMODE)
4028 strcat (buf, ", 32bitmode");
4029
4030 if (e_flags & EF_MIPS_NAN2008)
4031 strcat (buf, ", nan2008");
4032
4033 if (e_flags & EF_MIPS_FP64)
4034 strcat (buf, ", fp64");
4035
4036 switch ((e_flags & EF_MIPS_MACH))
4037 {
4038 case E_MIPS_MACH_3900: strcat (buf, ", 3900"); break;
4039 case E_MIPS_MACH_4010: strcat (buf, ", 4010"); break;
4040 case E_MIPS_MACH_4100: strcat (buf, ", 4100"); break;
4041 case E_MIPS_MACH_4111: strcat (buf, ", 4111"); break;
4042 case E_MIPS_MACH_4120: strcat (buf, ", 4120"); break;
4043 case E_MIPS_MACH_4650: strcat (buf, ", 4650"); break;
4044 case E_MIPS_MACH_5400: strcat (buf, ", 5400"); break;
4045 case E_MIPS_MACH_5500: strcat (buf, ", 5500"); break;
4046 case E_MIPS_MACH_5900: strcat (buf, ", 5900"); break;
4047 case E_MIPS_MACH_SB1: strcat (buf, ", sb1"); break;
4048 case E_MIPS_MACH_9000: strcat (buf, ", 9000"); break;
4049 case E_MIPS_MACH_LS2E: strcat (buf, ", loongson-2e"); break;
4050 case E_MIPS_MACH_LS2F: strcat (buf, ", loongson-2f"); break;
4051 case E_MIPS_MACH_GS464: strcat (buf, ", gs464"); break;
4052 case E_MIPS_MACH_GS464E: strcat (buf, ", gs464e"); break;
4053 case E_MIPS_MACH_GS264E: strcat (buf, ", gs264e"); break;
4054 case E_MIPS_MACH_OCTEON: strcat (buf, ", octeon"); break;
4055 case E_MIPS_MACH_OCTEON2: strcat (buf, ", octeon2"); break;
4056 case E_MIPS_MACH_OCTEON3: strcat (buf, ", octeon3"); break;
4057 case E_MIPS_MACH_XLR: strcat (buf, ", xlr"); break;
4058 case E_MIPS_MACH_IAMR2: strcat (buf, ", interaptiv-mr2"); break;
4059 case 0:
4060 /* We simply ignore the field in this case to avoid confusion:
4061 MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
4062 extension. */
4063 break;
4064 default: strcat (buf, _(", unknown CPU")); break;
4065 }
4066
4067 switch ((e_flags & EF_MIPS_ABI))
4068 {
4069 case E_MIPS_ABI_O32: strcat (buf, ", o32"); break;
4070 case E_MIPS_ABI_O64: strcat (buf, ", o64"); break;
4071 case E_MIPS_ABI_EABI32: strcat (buf, ", eabi32"); break;
4072 case E_MIPS_ABI_EABI64: strcat (buf, ", eabi64"); break;
4073 case 0:
4074 /* We simply ignore the field in this case to avoid confusion:
4075 MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
4076 This means it is likely to be an o32 file, but not for
4077 sure. */
4078 break;
4079 default: strcat (buf, _(", unknown ABI")); break;
4080 }
4081
4082 if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
4083 strcat (buf, ", mdmx");
4084
4085 if (e_flags & EF_MIPS_ARCH_ASE_M16)
4086 strcat (buf, ", mips16");
4087
4088 if (e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
4089 strcat (buf, ", micromips");
4090
4091 switch ((e_flags & EF_MIPS_ARCH))
4092 {
4093 case E_MIPS_ARCH_1: strcat (buf, ", mips1"); break;
4094 case E_MIPS_ARCH_2: strcat (buf, ", mips2"); break;
4095 case E_MIPS_ARCH_3: strcat (buf, ", mips3"); break;
4096 case E_MIPS_ARCH_4: strcat (buf, ", mips4"); break;
4097 case E_MIPS_ARCH_5: strcat (buf, ", mips5"); break;
4098 case E_MIPS_ARCH_32: strcat (buf, ", mips32"); break;
4099 case E_MIPS_ARCH_32R2: strcat (buf, ", mips32r2"); break;
4100 case E_MIPS_ARCH_32R6: strcat (buf, ", mips32r6"); break;
4101 case E_MIPS_ARCH_64: strcat (buf, ", mips64"); break;
4102 case E_MIPS_ARCH_64R2: strcat (buf, ", mips64r2"); break;
4103 case E_MIPS_ARCH_64R6: strcat (buf, ", mips64r6"); break;
4104 default: strcat (buf, _(", unknown ISA")); break;
4105 }
4106 break;
4107
4108 case EM_NDS32:
4109 decode_NDS32_machine_flags (e_flags, buf, sizeof buf);
4110 break;
4111
4112 case EM_NFP:
4113 switch (EF_NFP_MACH (e_flags))
4114 {
4115 case E_NFP_MACH_3200:
4116 strcat (buf, ", NFP-32xx");
4117 break;
4118 case E_NFP_MACH_6000:
4119 strcat (buf, ", NFP-6xxx");
4120 break;
4121 }
4122 break;
4123
4124 case EM_RISCV:
4125 if (e_flags & EF_RISCV_RVC)
4126 strcat (buf, ", RVC");
4127
4128 if (e_flags & EF_RISCV_RVE)
4129 strcat (buf, ", RVE");
4130
4131 switch (e_flags & EF_RISCV_FLOAT_ABI)
4132 {
4133 case EF_RISCV_FLOAT_ABI_SOFT:
4134 strcat (buf, ", soft-float ABI");
4135 break;
4136
4137 case EF_RISCV_FLOAT_ABI_SINGLE:
4138 strcat (buf, ", single-float ABI");
4139 break;
4140
4141 case EF_RISCV_FLOAT_ABI_DOUBLE:
4142 strcat (buf, ", double-float ABI");
4143 break;
4144
4145 case EF_RISCV_FLOAT_ABI_QUAD:
4146 strcat (buf, ", quad-float ABI");
4147 break;
4148 }
4149 break;
4150
4151 case EM_SH:
4152 switch ((e_flags & EF_SH_MACH_MASK))
4153 {
4154 case EF_SH1: strcat (buf, ", sh1"); break;
4155 case EF_SH2: strcat (buf, ", sh2"); break;
4156 case EF_SH3: strcat (buf, ", sh3"); break;
4157 case EF_SH_DSP: strcat (buf, ", sh-dsp"); break;
4158 case EF_SH3_DSP: strcat (buf, ", sh3-dsp"); break;
4159 case EF_SH4AL_DSP: strcat (buf, ", sh4al-dsp"); break;
4160 case EF_SH3E: strcat (buf, ", sh3e"); break;
4161 case EF_SH4: strcat (buf, ", sh4"); break;
4162 case EF_SH5: strcat (buf, ", sh5"); break;
4163 case EF_SH2E: strcat (buf, ", sh2e"); break;
4164 case EF_SH4A: strcat (buf, ", sh4a"); break;
4165 case EF_SH2A: strcat (buf, ", sh2a"); break;
4166 case EF_SH4_NOFPU: strcat (buf, ", sh4-nofpu"); break;
4167 case EF_SH4A_NOFPU: strcat (buf, ", sh4a-nofpu"); break;
4168 case EF_SH2A_NOFPU: strcat (buf, ", sh2a-nofpu"); break;
4169 case EF_SH3_NOMMU: strcat (buf, ", sh3-nommu"); break;
4170 case EF_SH4_NOMMU_NOFPU: strcat (buf, ", sh4-nommu-nofpu"); break;
4171 case EF_SH2A_SH4_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh4-nommu-nofpu"); break;
4172 case EF_SH2A_SH3_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh3-nommu"); break;
4173 case EF_SH2A_SH4: strcat (buf, ", sh2a-or-sh4"); break;
4174 case EF_SH2A_SH3E: strcat (buf, ", sh2a-or-sh3e"); break;
4175 default: strcat (buf, _(", unknown ISA")); break;
4176 }
4177
4178 if (e_flags & EF_SH_PIC)
4179 strcat (buf, ", pic");
4180
4181 if (e_flags & EF_SH_FDPIC)
4182 strcat (buf, ", fdpic");
4183 break;
4184
4185 case EM_OR1K:
4186 if (e_flags & EF_OR1K_NODELAY)
4187 strcat (buf, ", no delay");
4188 break;
4189
4190 case EM_SPARCV9:
4191 if (e_flags & EF_SPARC_32PLUS)
4192 strcat (buf, ", v8+");
4193
4194 if (e_flags & EF_SPARC_SUN_US1)
4195 strcat (buf, ", ultrasparcI");
4196
4197 if (e_flags & EF_SPARC_SUN_US3)
4198 strcat (buf, ", ultrasparcIII");
4199
4200 if (e_flags & EF_SPARC_HAL_R1)
4201 strcat (buf, ", halr1");
4202
4203 if (e_flags & EF_SPARC_LEDATA)
4204 strcat (buf, ", ledata");
4205
4206 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
4207 strcat (buf, ", tso");
4208
4209 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
4210 strcat (buf, ", pso");
4211
4212 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
4213 strcat (buf, ", rmo");
4214 break;
4215
4216 case EM_PARISC:
4217 switch (e_flags & EF_PARISC_ARCH)
4218 {
4219 case EFA_PARISC_1_0:
4220 strcpy (buf, ", PA-RISC 1.0");
4221 break;
4222 case EFA_PARISC_1_1:
4223 strcpy (buf, ", PA-RISC 1.1");
4224 break;
4225 case EFA_PARISC_2_0:
4226 strcpy (buf, ", PA-RISC 2.0");
4227 break;
4228 default:
4229 break;
4230 }
4231 if (e_flags & EF_PARISC_TRAPNIL)
4232 strcat (buf, ", trapnil");
4233 if (e_flags & EF_PARISC_EXT)
4234 strcat (buf, ", ext");
4235 if (e_flags & EF_PARISC_LSB)
4236 strcat (buf, ", lsb");
4237 if (e_flags & EF_PARISC_WIDE)
4238 strcat (buf, ", wide");
4239 if (e_flags & EF_PARISC_NO_KABP)
4240 strcat (buf, ", no kabp");
4241 if (e_flags & EF_PARISC_LAZYSWAP)
4242 strcat (buf, ", lazyswap");
4243 break;
4244
4245 case EM_PJ:
4246 case EM_PJ_OLD:
4247 if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
4248 strcat (buf, ", new calling convention");
4249
4250 if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
4251 strcat (buf, ", gnu calling convention");
4252 break;
4253
4254 case EM_IA_64:
4255 if ((e_flags & EF_IA_64_ABI64))
4256 strcat (buf, ", 64-bit");
4257 else
4258 strcat (buf, ", 32-bit");
4259 if ((e_flags & EF_IA_64_REDUCEDFP))
4260 strcat (buf, ", reduced fp model");
4261 if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
4262 strcat (buf, ", no function descriptors, constant gp");
4263 else if ((e_flags & EF_IA_64_CONS_GP))
4264 strcat (buf, ", constant gp");
4265 if ((e_flags & EF_IA_64_ABSOLUTE))
4266 strcat (buf, ", absolute");
4267 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
4268 {
4269 if ((e_flags & EF_IA_64_VMS_LINKAGES))
4270 strcat (buf, ", vms_linkages");
4271 switch ((e_flags & EF_IA_64_VMS_COMCOD))
4272 {
4273 case EF_IA_64_VMS_COMCOD_SUCCESS:
4274 break;
4275 case EF_IA_64_VMS_COMCOD_WARNING:
4276 strcat (buf, ", warning");
4277 break;
4278 case EF_IA_64_VMS_COMCOD_ERROR:
4279 strcat (buf, ", error");
4280 break;
4281 case EF_IA_64_VMS_COMCOD_ABORT:
4282 strcat (buf, ", abort");
4283 break;
4284 default:
4285 warn (_("Unrecognised IA64 VMS Command Code: %x\n"),
4286 e_flags & EF_IA_64_VMS_COMCOD);
4287 strcat (buf, ", <unknown>");
4288 }
4289 }
4290 break;
4291
4292 case EM_VAX:
4293 if ((e_flags & EF_VAX_NONPIC))
4294 strcat (buf, ", non-PIC");
4295 if ((e_flags & EF_VAX_DFLOAT))
4296 strcat (buf, ", D-Float");
4297 if ((e_flags & EF_VAX_GFLOAT))
4298 strcat (buf, ", G-Float");
4299 break;
4300
4301 case EM_VISIUM:
4302 if (e_flags & EF_VISIUM_ARCH_MCM)
4303 strcat (buf, ", mcm");
4304 else if (e_flags & EF_VISIUM_ARCH_MCM24)
4305 strcat (buf, ", mcm24");
4306 if (e_flags & EF_VISIUM_ARCH_GR6)
4307 strcat (buf, ", gr6");
4308 break;
4309
4310 case EM_RL78:
4311 switch (e_flags & E_FLAG_RL78_CPU_MASK)
4312 {
4313 case E_FLAG_RL78_ANY_CPU: break;
4314 case E_FLAG_RL78_G10: strcat (buf, ", G10"); break;
4315 case E_FLAG_RL78_G13: strcat (buf, ", G13"); break;
4316 case E_FLAG_RL78_G14: strcat (buf, ", G14"); break;
4317 }
4318 if (e_flags & E_FLAG_RL78_64BIT_DOUBLES)
4319 strcat (buf, ", 64-bit doubles");
4320 break;
4321
4322 case EM_RX:
4323 if (e_flags & E_FLAG_RX_64BIT_DOUBLES)
4324 strcat (buf, ", 64-bit doubles");
4325 if (e_flags & E_FLAG_RX_DSP)
4326 strcat (buf, ", dsp");
4327 if (e_flags & E_FLAG_RX_PID)
4328 strcat (buf, ", pid");
4329 if (e_flags & E_FLAG_RX_ABI)
4330 strcat (buf, ", RX ABI");
4331 if (e_flags & E_FLAG_RX_SINSNS_SET)
4332 strcat (buf, e_flags & E_FLAG_RX_SINSNS_YES
4333 ? ", uses String instructions" : ", bans String instructions");
4334 if (e_flags & E_FLAG_RX_V2)
4335 strcat (buf, ", V2");
4336 if (e_flags & E_FLAG_RX_V3)
4337 strcat (buf, ", V3");
4338 break;
4339
4340 case EM_S390:
4341 if (e_flags & EF_S390_HIGH_GPRS)
4342 strcat (buf, ", highgprs");
4343 break;
4344
4345 case EM_TI_C6000:
4346 if ((e_flags & EF_C6000_REL))
4347 strcat (buf, ", relocatable module");
4348 break;
4349
4350 case EM_MSP430:
4351 strcat (buf, _(": architecture variant: "));
4352 switch (e_flags & EF_MSP430_MACH)
4353 {
4354 case E_MSP430_MACH_MSP430x11: strcat (buf, "MSP430x11"); break;
4355 case E_MSP430_MACH_MSP430x11x1 : strcat (buf, "MSP430x11x1 "); break;
4356 case E_MSP430_MACH_MSP430x12: strcat (buf, "MSP430x12"); break;
4357 case E_MSP430_MACH_MSP430x13: strcat (buf, "MSP430x13"); break;
4358 case E_MSP430_MACH_MSP430x14: strcat (buf, "MSP430x14"); break;
4359 case E_MSP430_MACH_MSP430x15: strcat (buf, "MSP430x15"); break;
4360 case E_MSP430_MACH_MSP430x16: strcat (buf, "MSP430x16"); break;
4361 case E_MSP430_MACH_MSP430x31: strcat (buf, "MSP430x31"); break;
4362 case E_MSP430_MACH_MSP430x32: strcat (buf, "MSP430x32"); break;
4363 case E_MSP430_MACH_MSP430x33: strcat (buf, "MSP430x33"); break;
4364 case E_MSP430_MACH_MSP430x41: strcat (buf, "MSP430x41"); break;
4365 case E_MSP430_MACH_MSP430x42: strcat (buf, "MSP430x42"); break;
4366 case E_MSP430_MACH_MSP430x43: strcat (buf, "MSP430x43"); break;
4367 case E_MSP430_MACH_MSP430x44: strcat (buf, "MSP430x44"); break;
4368 case E_MSP430_MACH_MSP430X : strcat (buf, "MSP430X"); break;
4369 default:
4370 strcat (buf, _(": unknown")); break;
4371 }
4372
4373 if (e_flags & ~ EF_MSP430_MACH)
4374 strcat (buf, _(": unknown extra flag bits also present"));
4375 break;
4376
4377 case EM_Z80:
4378 switch (e_flags & EF_Z80_MACH_MSK)
4379 {
4380 case EF_Z80_MACH_Z80: strcat (buf, ", Z80"); break;
4381 case EF_Z80_MACH_Z180: strcat (buf, ", Z180"); break;
4382 case EF_Z80_MACH_R800: strcat (buf, ", R800"); break;
4383 case EF_Z80_MACH_EZ80_Z80: strcat (buf, ", EZ80"); break;
4384 case EF_Z80_MACH_EZ80_ADL: strcat (buf, ", EZ80, ADL"); break;
4385 case EF_Z80_MACH_GBZ80: strcat (buf, ", GBZ80"); break;
4386 case EF_Z80_MACH_Z80N: strcat (buf, ", Z80N"); break;
4387 default:
4388 strcat (buf, _(", unknown")); break;
4389 }
4390 break;
4391 case EM_LOONGARCH:
4392 if (EF_LOONGARCH_IS_LP64 (e_flags))
4393 strcat (buf, ", LP64");
4394 else if (EF_LOONGARCH_IS_ILP32 (e_flags))
4395 strcat (buf, ", ILP32");
4396
4397 if (EF_LOONGARCH_IS_SOFT_FLOAT (e_flags))
4398 strcat (buf, ", SOFT-FLOAT");
4399 else if (EF_LOONGARCH_IS_SINGLE_FLOAT (e_flags))
4400 strcat (buf, ", SINGLE-FLOAT");
4401 else if (EF_LOONGARCH_IS_DOUBLE_FLOAT (e_flags))
4402 strcat (buf, ", DOUBLE-FLOAT");
4403
4404 break;
4405 }
4406 }
4407
4408 return buf;
4409 }
4410
4411 static const char *
4412 get_osabi_name (Filedata * filedata, unsigned int osabi)
4413 {
4414 static char buff[32];
4415
4416 switch (osabi)
4417 {
4418 case ELFOSABI_NONE: return "UNIX - System V";
4419 case ELFOSABI_HPUX: return "UNIX - HP-UX";
4420 case ELFOSABI_NETBSD: return "UNIX - NetBSD";
4421 case ELFOSABI_GNU: return "UNIX - GNU";
4422 case ELFOSABI_SOLARIS: return "UNIX - Solaris";
4423 case ELFOSABI_AIX: return "UNIX - AIX";
4424 case ELFOSABI_IRIX: return "UNIX - IRIX";
4425 case ELFOSABI_FREEBSD: return "UNIX - FreeBSD";
4426 case ELFOSABI_TRU64: return "UNIX - TRU64";
4427 case ELFOSABI_MODESTO: return "Novell - Modesto";
4428 case ELFOSABI_OPENBSD: return "UNIX - OpenBSD";
4429 case ELFOSABI_OPENVMS: return "VMS - OpenVMS";
4430 case ELFOSABI_NSK: return "HP - Non-Stop Kernel";
4431 case ELFOSABI_AROS: return "AROS";
4432 case ELFOSABI_FENIXOS: return "FenixOS";
4433 case ELFOSABI_CLOUDABI: return "Nuxi CloudABI";
4434 case ELFOSABI_OPENVOS: return "Stratus Technologies OpenVOS";
4435 default:
4436 if (osabi >= 64)
4437 switch (filedata->file_header.e_machine)
4438 {
4439 case EM_AMDGPU:
4440 switch (osabi)
4441 {
4442 case ELFOSABI_AMDGPU_HSA: return "AMD HSA";
4443 case ELFOSABI_AMDGPU_PAL: return "AMD PAL";
4444 case ELFOSABI_AMDGPU_MESA3D: return "AMD Mesa3D";
4445 default:
4446 break;
4447 }
4448 break;
4449
4450 case EM_ARM:
4451 switch (osabi)
4452 {
4453 case ELFOSABI_ARM: return "ARM";
4454 case ELFOSABI_ARM_FDPIC: return "ARM FDPIC";
4455 default:
4456 break;
4457 }
4458 break;
4459
4460 case EM_MSP430:
4461 case EM_MSP430_OLD:
4462 case EM_VISIUM:
4463 switch (osabi)
4464 {
4465 case ELFOSABI_STANDALONE: return _("Standalone App");
4466 default:
4467 break;
4468 }
4469 break;
4470
4471 case EM_TI_C6000:
4472 switch (osabi)
4473 {
4474 case ELFOSABI_C6000_ELFABI: return _("Bare-metal C6000");
4475 case ELFOSABI_C6000_LINUX: return "Linux C6000";
4476 default:
4477 break;
4478 }
4479 break;
4480
4481 default:
4482 break;
4483 }
4484 snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
4485 return buff;
4486 }
4487 }
4488
4489 static const char *
4490 get_aarch64_segment_type (unsigned long type)
4491 {
4492 switch (type)
4493 {
4494 case PT_AARCH64_ARCHEXT: return "AARCH64_ARCHEXT";
4495 case PT_AARCH64_MEMTAG_MTE: return "AARCH64_MEMTAG_MTE";
4496 default: return NULL;
4497 }
4498 }
4499
4500 static const char *
4501 get_arm_segment_type (unsigned long type)
4502 {
4503 switch (type)
4504 {
4505 case PT_ARM_EXIDX: return "EXIDX";
4506 default: return NULL;
4507 }
4508 }
4509
4510 static const char *
4511 get_s390_segment_type (unsigned long type)
4512 {
4513 switch (type)
4514 {
4515 case PT_S390_PGSTE: return "S390_PGSTE";
4516 default: return NULL;
4517 }
4518 }
4519
4520 static const char *
4521 get_mips_segment_type (unsigned long type)
4522 {
4523 switch (type)
4524 {
4525 case PT_MIPS_REGINFO: return "REGINFO";
4526 case PT_MIPS_RTPROC: return "RTPROC";
4527 case PT_MIPS_OPTIONS: return "OPTIONS";
4528 case PT_MIPS_ABIFLAGS: return "ABIFLAGS";
4529 default: return NULL;
4530 }
4531 }
4532
4533 static const char *
4534 get_parisc_segment_type (unsigned long type)
4535 {
4536 switch (type)
4537 {
4538 case PT_PARISC_ARCHEXT: return "PARISC_ARCHEXT";
4539 case PT_PARISC_UNWIND: return "PARISC_UNWIND";
4540 case PT_PARISC_WEAKORDER: return "PARISC_WEAKORDER";
4541 default: return NULL;
4542 }
4543 }
4544
4545 static const char *
4546 get_ia64_segment_type (unsigned long type)
4547 {
4548 switch (type)
4549 {
4550 case PT_IA_64_ARCHEXT: return "IA_64_ARCHEXT";
4551 case PT_IA_64_UNWIND: return "IA_64_UNWIND";
4552 default: return NULL;
4553 }
4554 }
4555
4556 static const char *
4557 get_tic6x_segment_type (unsigned long type)
4558 {
4559 switch (type)
4560 {
4561 case PT_C6000_PHATTR: return "C6000_PHATTR";
4562 default: return NULL;
4563 }
4564 }
4565
4566 static const char *
4567 get_riscv_segment_type (unsigned long type)
4568 {
4569 switch (type)
4570 {
4571 case PT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
4572 default: return NULL;
4573 }
4574 }
4575
4576 static const char *
4577 get_hpux_segment_type (unsigned long type, unsigned e_machine)
4578 {
4579 if (e_machine == EM_PARISC)
4580 switch (type)
4581 {
4582 case PT_HP_TLS: return "HP_TLS";
4583 case PT_HP_CORE_NONE: return "HP_CORE_NONE";
4584 case PT_HP_CORE_VERSION: return "HP_CORE_VERSION";
4585 case PT_HP_CORE_KERNEL: return "HP_CORE_KERNEL";
4586 case PT_HP_CORE_COMM: return "HP_CORE_COMM";
4587 case PT_HP_CORE_PROC: return "HP_CORE_PROC";
4588 case PT_HP_CORE_LOADABLE: return "HP_CORE_LOADABLE";
4589 case PT_HP_CORE_STACK: return "HP_CORE_STACK";
4590 case PT_HP_CORE_SHM: return "HP_CORE_SHM";
4591 case PT_HP_CORE_MMF: return "HP_CORE_MMF";
4592 case PT_HP_PARALLEL: return "HP_PARALLEL";
4593 case PT_HP_FASTBIND: return "HP_FASTBIND";
4594 case PT_HP_OPT_ANNOT: return "HP_OPT_ANNOT";
4595 case PT_HP_HSL_ANNOT: return "HP_HSL_ANNOT";
4596 case PT_HP_STACK: return "HP_STACK";
4597 case PT_HP_CORE_UTSNAME: return "HP_CORE_UTSNAME";
4598 default: return NULL;
4599 }
4600
4601 if (e_machine == EM_IA_64)
4602 switch (type)
4603 {
4604 case PT_HP_TLS: return "HP_TLS";
4605 case PT_IA_64_HP_OPT_ANOT: return "HP_OPT_ANNOT";
4606 case PT_IA_64_HP_HSL_ANOT: return "HP_HSL_ANNOT";
4607 case PT_IA_64_HP_STACK: return "HP_STACK";
4608 default: return NULL;
4609 }
4610
4611 return NULL;
4612 }
4613
4614 static const char *
4615 get_solaris_segment_type (unsigned long type)
4616 {
4617 switch (type)
4618 {
4619 case 0x6464e550: return "PT_SUNW_UNWIND";
4620 case 0x6474e550: return "PT_SUNW_EH_FRAME";
4621 case 0x6ffffff7: return "PT_LOSUNW";
4622 case 0x6ffffffa: return "PT_SUNWBSS";
4623 case 0x6ffffffb: return "PT_SUNWSTACK";
4624 case 0x6ffffffc: return "PT_SUNWDTRACE";
4625 case 0x6ffffffd: return "PT_SUNWCAP";
4626 case 0x6fffffff: return "PT_HISUNW";
4627 default: return NULL;
4628 }
4629 }
4630
4631 static const char *
4632 get_segment_type (Filedata * filedata, unsigned long p_type)
4633 {
4634 static char buff[32];
4635
4636 switch (p_type)
4637 {
4638 case PT_NULL: return "NULL";
4639 case PT_LOAD: return "LOAD";
4640 case PT_DYNAMIC: return "DYNAMIC";
4641 case PT_INTERP: return "INTERP";
4642 case PT_NOTE: return "NOTE";
4643 case PT_SHLIB: return "SHLIB";
4644 case PT_PHDR: return "PHDR";
4645 case PT_TLS: return "TLS";
4646 case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
4647 case PT_GNU_STACK: return "GNU_STACK";
4648 case PT_GNU_RELRO: return "GNU_RELRO";
4649 case PT_GNU_PROPERTY: return "GNU_PROPERTY";
4650
4651 case PT_OPENBSD_RANDOMIZE: return "OPENBSD_RANDOMIZE";
4652 case PT_OPENBSD_WXNEEDED: return "OPENBSD_WXNEEDED";
4653 case PT_OPENBSD_BOOTDATA: return "OPENBSD_BOOTDATA";
4654
4655 default:
4656 if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
4657 {
4658 const char * result;
4659
4660 switch (filedata->file_header.e_machine)
4661 {
4662 case EM_AARCH64:
4663 result = get_aarch64_segment_type (p_type);
4664 break;
4665 case EM_ARM:
4666 result = get_arm_segment_type (p_type);
4667 break;
4668 case EM_MIPS:
4669 case EM_MIPS_RS3_LE:
4670 result = get_mips_segment_type (p_type);
4671 break;
4672 case EM_PARISC:
4673 result = get_parisc_segment_type (p_type);
4674 break;
4675 case EM_IA_64:
4676 result = get_ia64_segment_type (p_type);
4677 break;
4678 case EM_TI_C6000:
4679 result = get_tic6x_segment_type (p_type);
4680 break;
4681 case EM_S390:
4682 case EM_S390_OLD:
4683 result = get_s390_segment_type (p_type);
4684 break;
4685 case EM_RISCV:
4686 result = get_riscv_segment_type (p_type);
4687 break;
4688 default:
4689 result = NULL;
4690 break;
4691 }
4692
4693 if (result != NULL)
4694 return result;
4695
4696 sprintf (buff, "LOPROC+%#lx", p_type - PT_LOPROC);
4697 }
4698 else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
4699 {
4700 const char * result = NULL;
4701
4702 switch (filedata->file_header.e_ident[EI_OSABI])
4703 {
4704 case ELFOSABI_GNU:
4705 case ELFOSABI_FREEBSD:
4706 if (p_type >= PT_GNU_MBIND_LO && p_type <= PT_GNU_MBIND_HI)
4707 {
4708 sprintf (buff, "GNU_MBIND+%#lx", p_type - PT_GNU_MBIND_LO);
4709 result = buff;
4710 }
4711 break;
4712 case ELFOSABI_HPUX:
4713 result = get_hpux_segment_type (p_type,
4714 filedata->file_header.e_machine);
4715 break;
4716 case ELFOSABI_SOLARIS:
4717 result = get_solaris_segment_type (p_type);
4718 break;
4719 default:
4720 break;
4721 }
4722 if (result != NULL)
4723 return result;
4724
4725 sprintf (buff, "LOOS+%#lx", p_type - PT_LOOS);
4726 }
4727 else
4728 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
4729
4730 return buff;
4731 }
4732 }
4733
4734 static const char *
4735 get_arc_section_type_name (unsigned int sh_type)
4736 {
4737 switch (sh_type)
4738 {
4739 case SHT_ARC_ATTRIBUTES: return "ARC_ATTRIBUTES";
4740 default:
4741 break;
4742 }
4743 return NULL;
4744 }
4745
4746 static const char *
4747 get_mips_section_type_name (unsigned int sh_type)
4748 {
4749 switch (sh_type)
4750 {
4751 case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
4752 case SHT_MIPS_MSYM: return "MIPS_MSYM";
4753 case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
4754 case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
4755 case SHT_MIPS_UCODE: return "MIPS_UCODE";
4756 case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
4757 case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
4758 case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
4759 case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
4760 case SHT_MIPS_RELD: return "MIPS_RELD";
4761 case SHT_MIPS_IFACE: return "MIPS_IFACE";
4762 case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
4763 case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
4764 case SHT_MIPS_SHDR: return "MIPS_SHDR";
4765 case SHT_MIPS_FDESC: return "MIPS_FDESC";
4766 case SHT_MIPS_EXTSYM: return "MIPS_EXTSYM";
4767 case SHT_MIPS_DENSE: return "MIPS_DENSE";
4768 case SHT_MIPS_PDESC: return "MIPS_PDESC";
4769 case SHT_MIPS_LOCSYM: return "MIPS_LOCSYM";
4770 case SHT_MIPS_AUXSYM: return "MIPS_AUXSYM";
4771 case SHT_MIPS_OPTSYM: return "MIPS_OPTSYM";
4772 case SHT_MIPS_LOCSTR: return "MIPS_LOCSTR";
4773 case SHT_MIPS_LINE: return "MIPS_LINE";
4774 case SHT_MIPS_RFDESC: return "MIPS_RFDESC";
4775 case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
4776 case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
4777 case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
4778 case SHT_MIPS_DWARF: return "MIPS_DWARF";
4779 case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
4780 case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
4781 case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
4782 case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
4783 case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
4784 case SHT_MIPS_XLATE: return "MIPS_XLATE";
4785 case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
4786 case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
4787 case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
4788 case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
4789 case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
4790 case SHT_MIPS_ABIFLAGS: return "MIPS_ABIFLAGS";
4791 case SHT_MIPS_XHASH: return "MIPS_XHASH";
4792 default:
4793 break;
4794 }
4795 return NULL;
4796 }
4797
4798 static const char *
4799 get_parisc_section_type_name (unsigned int sh_type)
4800 {
4801 switch (sh_type)
4802 {
4803 case SHT_PARISC_EXT: return "PARISC_EXT";
4804 case SHT_PARISC_UNWIND: return "PARISC_UNWIND";
4805 case SHT_PARISC_DOC: return "PARISC_DOC";
4806 case SHT_PARISC_ANNOT: return "PARISC_ANNOT";
4807 case SHT_PARISC_SYMEXTN: return "PARISC_SYMEXTN";
4808 case SHT_PARISC_STUBS: return "PARISC_STUBS";
4809 case SHT_PARISC_DLKM: return "PARISC_DLKM";
4810 default: return NULL;
4811 }
4812 }
4813
4814 static const char *
4815 get_ia64_section_type_name (Filedata * filedata, unsigned int sh_type)
4816 {
4817 /* If the top 8 bits are 0x78 the next 8 are the os/abi ID. */
4818 if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
4819 return get_osabi_name (filedata, (sh_type & 0x00FF0000) >> 16);
4820
4821 switch (sh_type)
4822 {
4823 case SHT_IA_64_EXT: return "IA_64_EXT";
4824 case SHT_IA_64_UNWIND: return "IA_64_UNWIND";
4825 case SHT_IA_64_PRIORITY_INIT: return "IA_64_PRIORITY_INIT";
4826 case SHT_IA_64_VMS_TRACE: return "VMS_TRACE";
4827 case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
4828 case SHT_IA_64_VMS_DEBUG: return "VMS_DEBUG";
4829 case SHT_IA_64_VMS_DEBUG_STR: return "VMS_DEBUG_STR";
4830 case SHT_IA_64_VMS_LINKAGES: return "VMS_LINKAGES";
4831 case SHT_IA_64_VMS_SYMBOL_VECTOR: return "VMS_SYMBOL_VECTOR";
4832 case SHT_IA_64_VMS_FIXUP: return "VMS_FIXUP";
4833 default:
4834 break;
4835 }
4836 return NULL;
4837 }
4838
4839 static const char *
4840 get_x86_64_section_type_name (unsigned int sh_type)
4841 {
4842 switch (sh_type)
4843 {
4844 case SHT_X86_64_UNWIND: return "X86_64_UNWIND";
4845 default: return NULL;
4846 }
4847 }
4848
4849 static const char *
4850 get_aarch64_section_type_name (unsigned int sh_type)
4851 {
4852 switch (sh_type)
4853 {
4854 case SHT_AARCH64_ATTRIBUTES: return "AARCH64_ATTRIBUTES";
4855 default: return NULL;
4856 }
4857 }
4858
4859 static const char *
4860 get_arm_section_type_name (unsigned int sh_type)
4861 {
4862 switch (sh_type)
4863 {
4864 case SHT_ARM_EXIDX: return "ARM_EXIDX";
4865 case SHT_ARM_PREEMPTMAP: return "ARM_PREEMPTMAP";
4866 case SHT_ARM_ATTRIBUTES: return "ARM_ATTRIBUTES";
4867 case SHT_ARM_DEBUGOVERLAY: return "ARM_DEBUGOVERLAY";
4868 case SHT_ARM_OVERLAYSECTION: return "ARM_OVERLAYSECTION";
4869 default: return NULL;
4870 }
4871 }
4872
4873 static const char *
4874 get_tic6x_section_type_name (unsigned int sh_type)
4875 {
4876 switch (sh_type)
4877 {
4878 case SHT_C6000_UNWIND: return "C6000_UNWIND";
4879 case SHT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
4880 case SHT_C6000_ATTRIBUTES: return "C6000_ATTRIBUTES";
4881 case SHT_TI_ICODE: return "TI_ICODE";
4882 case SHT_TI_XREF: return "TI_XREF";
4883 case SHT_TI_HANDLER: return "TI_HANDLER";
4884 case SHT_TI_INITINFO: return "TI_INITINFO";
4885 case SHT_TI_PHATTRS: return "TI_PHATTRS";
4886 default: return NULL;
4887 }
4888 }
4889
4890 static const char *
4891 get_msp430_section_type_name (unsigned int sh_type)
4892 {
4893 switch (sh_type)
4894 {
4895 case SHT_MSP430_SEC_FLAGS: return "MSP430_SEC_FLAGS";
4896 case SHT_MSP430_SYM_ALIASES: return "MSP430_SYM_ALIASES";
4897 case SHT_MSP430_ATTRIBUTES: return "MSP430_ATTRIBUTES";
4898 default: return NULL;
4899 }
4900 }
4901
4902 static const char *
4903 get_nfp_section_type_name (unsigned int sh_type)
4904 {
4905 switch (sh_type)
4906 {
4907 case SHT_NFP_MECONFIG: return "NFP_MECONFIG";
4908 case SHT_NFP_INITREG: return "NFP_INITREG";
4909 case SHT_NFP_UDEBUG: return "NFP_UDEBUG";
4910 default: return NULL;
4911 }
4912 }
4913
4914 static const char *
4915 get_v850_section_type_name (unsigned int sh_type)
4916 {
4917 switch (sh_type)
4918 {
4919 case SHT_V850_SCOMMON: return "V850 Small Common";
4920 case SHT_V850_TCOMMON: return "V850 Tiny Common";
4921 case SHT_V850_ZCOMMON: return "V850 Zero Common";
4922 case SHT_RENESAS_IOP: return "RENESAS IOP";
4923 case SHT_RENESAS_INFO: return "RENESAS INFO";
4924 default: return NULL;
4925 }
4926 }
4927
4928 static const char *
4929 get_riscv_section_type_name (unsigned int sh_type)
4930 {
4931 switch (sh_type)
4932 {
4933 case SHT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
4934 default: return NULL;
4935 }
4936 }
4937
4938 static const char *
4939 get_csky_section_type_name (unsigned int sh_type)
4940 {
4941 switch (sh_type)
4942 {
4943 case SHT_CSKY_ATTRIBUTES: return "CSKY_ATTRIBUTES";
4944 default: return NULL;
4945 }
4946 }
4947
4948 static const char *
4949 get_section_type_name (Filedata * filedata, unsigned int sh_type)
4950 {
4951 static char buff[32];
4952 const char * result;
4953
4954 switch (sh_type)
4955 {
4956 case SHT_NULL: return "NULL";
4957 case SHT_PROGBITS: return "PROGBITS";
4958 case SHT_SYMTAB: return "SYMTAB";
4959 case SHT_STRTAB: return "STRTAB";
4960 case SHT_RELA: return "RELA";
4961 case SHT_RELR: return "RELR";
4962 case SHT_HASH: return "HASH";
4963 case SHT_DYNAMIC: return "DYNAMIC";
4964 case SHT_NOTE: return "NOTE";
4965 case SHT_NOBITS: return "NOBITS";
4966 case SHT_REL: return "REL";
4967 case SHT_SHLIB: return "SHLIB";
4968 case SHT_DYNSYM: return "DYNSYM";
4969 case SHT_INIT_ARRAY: return "INIT_ARRAY";
4970 case SHT_FINI_ARRAY: return "FINI_ARRAY";
4971 case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
4972 case SHT_GNU_HASH: return "GNU_HASH";
4973 case SHT_GROUP: return "GROUP";
4974 case SHT_SYMTAB_SHNDX: return "SYMTAB SECTION INDICES";
4975 case SHT_GNU_verdef: return "VERDEF";
4976 case SHT_GNU_verneed: return "VERNEED";
4977 case SHT_GNU_versym: return "VERSYM";
4978 case 0x6ffffff0: return "VERSYM";
4979 case 0x6ffffffc: return "VERDEF";
4980 case 0x7ffffffd: return "AUXILIARY";
4981 case 0x7fffffff: return "FILTER";
4982 case SHT_GNU_LIBLIST: return "GNU_LIBLIST";
4983
4984 default:
4985 if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
4986 {
4987 switch (filedata->file_header.e_machine)
4988 {
4989 case EM_ARC:
4990 case EM_ARC_COMPACT:
4991 case EM_ARC_COMPACT2:
4992 result = get_arc_section_type_name (sh_type);
4993 break;
4994 case EM_MIPS:
4995 case EM_MIPS_RS3_LE:
4996 result = get_mips_section_type_name (sh_type);
4997 break;
4998 case EM_PARISC:
4999 result = get_parisc_section_type_name (sh_type);
5000 break;
5001 case EM_IA_64:
5002 result = get_ia64_section_type_name (filedata, sh_type);
5003 break;
5004 case EM_X86_64:
5005 case EM_L1OM:
5006 case EM_K1OM:
5007 result = get_x86_64_section_type_name (sh_type);
5008 break;
5009 case EM_AARCH64:
5010 result = get_aarch64_section_type_name (sh_type);
5011 break;
5012 case EM_ARM:
5013 result = get_arm_section_type_name (sh_type);
5014 break;
5015 case EM_TI_C6000:
5016 result = get_tic6x_section_type_name (sh_type);
5017 break;
5018 case EM_MSP430:
5019 result = get_msp430_section_type_name (sh_type);
5020 break;
5021 case EM_NFP:
5022 result = get_nfp_section_type_name (sh_type);
5023 break;
5024 case EM_V800:
5025 case EM_V850:
5026 case EM_CYGNUS_V850:
5027 result = get_v850_section_type_name (sh_type);
5028 break;
5029 case EM_RISCV:
5030 result = get_riscv_section_type_name (sh_type);
5031 break;
5032 case EM_CSKY:
5033 result = get_csky_section_type_name (sh_type);
5034 break;
5035 default:
5036 result = NULL;
5037 break;
5038 }
5039
5040 if (result != NULL)
5041 return result;
5042
5043 sprintf (buff, "LOPROC+%#x", sh_type - SHT_LOPROC);
5044 }
5045 else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
5046 {
5047 switch (filedata->file_header.e_machine)
5048 {
5049 case EM_IA_64:
5050 result = get_ia64_section_type_name (filedata, sh_type);
5051 break;
5052 default:
5053 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
5054 result = get_solaris_section_type (sh_type);
5055 else
5056 {
5057 switch (sh_type)
5058 {
5059 case SHT_GNU_INCREMENTAL_INPUTS: result = "GNU_INCREMENTAL_INPUTS"; break;
5060 case SHT_GNU_ATTRIBUTES: result = "GNU_ATTRIBUTES"; break;
5061 case SHT_GNU_HASH: result = "GNU_HASH"; break;
5062 case SHT_GNU_LIBLIST: result = "GNU_LIBLIST"; break;
5063 default:
5064 result = NULL;
5065 break;
5066 }
5067 }
5068 break;
5069 }
5070
5071 if (result != NULL)
5072 return result;
5073
5074 sprintf (buff, "LOOS+%#x", sh_type - SHT_LOOS);
5075 }
5076 else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
5077 {
5078 switch (filedata->file_header.e_machine)
5079 {
5080 case EM_V800:
5081 case EM_V850:
5082 case EM_CYGNUS_V850:
5083 result = get_v850_section_type_name (sh_type);
5084 break;
5085 default:
5086 result = NULL;
5087 break;
5088 }
5089
5090 if (result != NULL)
5091 return result;
5092
5093 sprintf (buff, "LOUSER+%#x", sh_type - SHT_LOUSER);
5094 }
5095 else
5096 /* This message is probably going to be displayed in a 15
5097 character wide field, so put the hex value first. */
5098 snprintf (buff, sizeof (buff), _("%08x: <unknown>"), sh_type);
5099
5100 return buff;
5101 }
5102 }
5103
5104 enum long_option_values
5105 {
5106 OPTION_DEBUG_DUMP = 512,
5107 OPTION_DYN_SYMS,
5108 OPTION_LTO_SYMS,
5109 OPTION_DWARF_DEPTH,
5110 OPTION_DWARF_START,
5111 OPTION_DWARF_CHECK,
5112 OPTION_CTF_DUMP,
5113 OPTION_CTF_PARENT,
5114 OPTION_CTF_SYMBOLS,
5115 OPTION_CTF_STRINGS,
5116 OPTION_WITH_SYMBOL_VERSIONS,
5117 OPTION_RECURSE_LIMIT,
5118 OPTION_NO_RECURSE_LIMIT,
5119 OPTION_NO_DEMANGLING,
5120 OPTION_SYM_BASE
5121 };
5122
5123 static struct option options[] =
5124 {
5125 /* Note - This table is alpha-sorted on the 'val'
5126 field in order to make adding new options easier. */
5127 {"arch-specific", no_argument, 0, 'A'},
5128 {"all", no_argument, 0, 'a'},
5129 {"demangle", optional_argument, 0, 'C'},
5130 {"archive-index", no_argument, 0, 'c'},
5131 {"use-dynamic", no_argument, 0, 'D'},
5132 {"dynamic", no_argument, 0, 'd'},
5133 {"headers", no_argument, 0, 'e'},
5134 {"section-groups", no_argument, 0, 'g'},
5135 {"help", no_argument, 0, 'H'},
5136 {"file-header", no_argument, 0, 'h'},
5137 {"histogram", no_argument, 0, 'I'},
5138 {"lint", no_argument, 0, 'L'},
5139 {"enable-checks", no_argument, 0, 'L'},
5140 {"program-headers", no_argument, 0, 'l'},
5141 {"segments", no_argument, 0, 'l'},
5142 {"full-section-name",no_argument, 0, 'N'},
5143 {"notes", no_argument, 0, 'n'},
5144 {"process-links", no_argument, 0, 'P'},
5145 {"string-dump", required_argument, 0, 'p'},
5146 {"relocated-dump", required_argument, 0, 'R'},
5147 {"relocs", no_argument, 0, 'r'},
5148 {"section-headers", no_argument, 0, 'S'},
5149 {"sections", no_argument, 0, 'S'},
5150 {"symbols", no_argument, 0, 's'},
5151 {"syms", no_argument, 0, 's'},
5152 {"silent-truncation",no_argument, 0, 'T'},
5153 {"section-details", no_argument, 0, 't'},
5154 {"unicode", required_argument, NULL, 'U'},
5155 {"unwind", no_argument, 0, 'u'},
5156 {"version-info", no_argument, 0, 'V'},
5157 {"version", no_argument, 0, 'v'},
5158 {"wide", no_argument, 0, 'W'},
5159 {"hex-dump", required_argument, 0, 'x'},
5160 {"decompress", no_argument, 0, 'z'},
5161
5162 {"no-demangle", no_argument, 0, OPTION_NO_DEMANGLING},
5163 {"recurse-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
5164 {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
5165 {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
5166 {"dyn-syms", no_argument, 0, OPTION_DYN_SYMS},
5167 {"lto-syms", no_argument, 0, OPTION_LTO_SYMS},
5168 {"debug-dump", optional_argument, 0, OPTION_DEBUG_DUMP},
5169 {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
5170 {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
5171 {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
5172 #ifdef ENABLE_LIBCTF
5173 {"ctf", required_argument, 0, OPTION_CTF_DUMP},
5174 {"ctf-symbols", required_argument, 0, OPTION_CTF_SYMBOLS},
5175 {"ctf-strings", required_argument, 0, OPTION_CTF_STRINGS},
5176 {"ctf-parent", required_argument, 0, OPTION_CTF_PARENT},
5177 #endif
5178 {"sym-base", optional_argument, 0, OPTION_SYM_BASE},
5179
5180 {0, no_argument, 0, 0}
5181 };
5182
5183 static void
5184 usage (FILE * stream)
5185 {
5186 fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
5187 fprintf (stream, _(" Display information about the contents of ELF format files\n"));
5188 fprintf (stream, _(" Options are:\n"));
5189 fprintf (stream, _("\
5190 -a --all Equivalent to: -h -l -S -s -r -d -V -A -I\n"));
5191 fprintf (stream, _("\
5192 -h --file-header Display the ELF file header\n"));
5193 fprintf (stream, _("\
5194 -l --program-headers Display the program headers\n"));
5195 fprintf (stream, _("\
5196 --segments An alias for --program-headers\n"));
5197 fprintf (stream, _("\
5198 -S --section-headers Display the sections' header\n"));
5199 fprintf (stream, _("\
5200 --sections An alias for --section-headers\n"));
5201 fprintf (stream, _("\
5202 -g --section-groups Display the section groups\n"));
5203 fprintf (stream, _("\
5204 -t --section-details Display the section details\n"));
5205 fprintf (stream, _("\
5206 -e --headers Equivalent to: -h -l -S\n"));
5207 fprintf (stream, _("\
5208 -s --syms Display the symbol table\n"));
5209 fprintf (stream, _("\
5210 --symbols An alias for --syms\n"));
5211 fprintf (stream, _("\
5212 --dyn-syms Display the dynamic symbol table\n"));
5213 fprintf (stream, _("\
5214 --lto-syms Display LTO symbol tables\n"));
5215 fprintf (stream, _("\
5216 --sym-base=[0|8|10|16] \n\
5217 Force base for symbol sizes. The options are \n\
5218 mixed (the default), octal, decimal, hexadecimal.\n"));
5219 fprintf (stream, _("\
5220 -C --demangle[=STYLE] Decode mangled/processed symbol names\n"));
5221 display_demangler_styles (stream, _("\
5222 STYLE can be "));
5223 fprintf (stream, _("\
5224 --no-demangle Do not demangle low-level symbol names. (default)\n"));
5225 fprintf (stream, _("\
5226 --recurse-limit Enable a demangling recursion limit. (default)\n"));
5227 fprintf (stream, _("\
5228 --no-recurse-limit Disable a demangling recursion limit\n"));
5229 fprintf (stream, _("\
5230 -U[dlexhi] --unicode=[default|locale|escape|hex|highlight|invalid]\n\
5231 Display unicode characters as determined by the current locale\n\
5232 (default), escape sequences, \"<hex sequences>\", highlighted\n\
5233 escape sequences, or treat them as invalid and display as\n\
5234 \"{hex sequences}\"\n"));
5235 fprintf (stream, _("\
5236 -n --notes Display the core notes (if present)\n"));
5237 fprintf (stream, _("\
5238 -r --relocs Display the relocations (if present)\n"));
5239 fprintf (stream, _("\
5240 -u --unwind Display the unwind info (if present)\n"));
5241 fprintf (stream, _("\
5242 -d --dynamic Display the dynamic section (if present)\n"));
5243 fprintf (stream, _("\
5244 -V --version-info Display the version sections (if present)\n"));
5245 fprintf (stream, _("\
5246 -A --arch-specific Display architecture specific information (if any)\n"));
5247 fprintf (stream, _("\
5248 -c --archive-index Display the symbol/file index in an archive\n"));
5249 fprintf (stream, _("\
5250 -D --use-dynamic Use the dynamic section info when displaying symbols\n"));
5251 fprintf (stream, _("\
5252 -L --lint|--enable-checks\n\
5253 Display warning messages for possible problems\n"));
5254 fprintf (stream, _("\
5255 -x --hex-dump=<number|name>\n\
5256 Dump the contents of section <number|name> as bytes\n"));
5257 fprintf (stream, _("\
5258 -p --string-dump=<number|name>\n\
5259 Dump the contents of section <number|name> as strings\n"));
5260 fprintf (stream, _("\
5261 -R --relocated-dump=<number|name>\n\
5262 Dump the relocated contents of section <number|name>\n"));
5263 fprintf (stream, _("\
5264 -z --decompress Decompress section before dumping it\n"));
5265 fprintf (stream, _("\
5266 -w --debug-dump[a/=abbrev, A/=addr, r/=aranges, c/=cu_index, L/=decodedline,\n\
5267 f/=frames, F/=frames-interp, g/=gdb_index, i/=info, o/=loc,\n\
5268 m/=macro, p/=pubnames, t/=pubtypes, R/=Ranges, l/=rawline,\n\
5269 s/=str, O/=str-offsets, u/=trace_abbrev, T/=trace_aranges,\n\
5270 U/=trace_info]\n\
5271 Display the contents of DWARF debug sections\n"));
5272 fprintf (stream, _("\
5273 -wk --debug-dump=links Display the contents of sections that link to separate\n\
5274 debuginfo files\n"));
5275 fprintf (stream, _("\
5276 -P --process-links Display the contents of non-debug sections in separate\n\
5277 debuginfo files. (Implies -wK)\n"));
5278 #if DEFAULT_FOR_FOLLOW_LINKS
5279 fprintf (stream, _("\
5280 -wK --debug-dump=follow-links\n\
5281 Follow links to separate debug info files (default)\n"));
5282 fprintf (stream, _("\
5283 -wN --debug-dump=no-follow-links\n\
5284 Do not follow links to separate debug info files\n"));
5285 #else
5286 fprintf (stream, _("\
5287 -wK --debug-dump=follow-links\n\
5288 Follow links to separate debug info files\n"));
5289 fprintf (stream, _("\
5290 -wN --debug-dump=no-follow-links\n\
5291 Do not follow links to separate debug info files\n\
5292 (default)\n"));
5293 #endif
5294 #if HAVE_LIBDEBUGINFOD
5295 fprintf (stream, _("\
5296 -wD --debug-dump=use-debuginfod\n\
5297 When following links, also query debuginfod servers (default)\n"));
5298 fprintf (stream, _("\
5299 -wE --debug-dump=do-not-use-debuginfod\n\
5300 When following links, do not query debuginfod servers\n"));
5301 #endif
5302 fprintf (stream, _("\
5303 --dwarf-depth=N Do not display DIEs at depth N or greater\n"));
5304 fprintf (stream, _("\
5305 --dwarf-start=N Display DIEs starting at offset N\n"));
5306 #ifdef ENABLE_LIBCTF
5307 fprintf (stream, _("\
5308 --ctf=<number|name> Display CTF info from section <number|name>\n"));
5309 fprintf (stream, _("\
5310 --ctf-parent=<name> Use CTF archive member <name> as the CTF parent\n"));
5311 fprintf (stream, _("\
5312 --ctf-symbols=<number|name>\n\
5313 Use section <number|name> as the CTF external symtab\n"));
5314 fprintf (stream, _("\
5315 --ctf-strings=<number|name>\n\
5316 Use section <number|name> as the CTF external strtab\n"));
5317 #endif
5318
5319 #ifdef SUPPORT_DISASSEMBLY
5320 fprintf (stream, _("\
5321 -i --instruction-dump=<number|name>\n\
5322 Disassemble the contents of section <number|name>\n"));
5323 #endif
5324 fprintf (stream, _("\
5325 -I --histogram Display histogram of bucket list lengths\n"));
5326 fprintf (stream, _("\
5327 -W --wide Allow output width to exceed 80 characters\n"));
5328 fprintf (stream, _("\
5329 -T --silent-truncation If a symbol name is truncated, do not add [...] suffix\n"));
5330 fprintf (stream, _("\
5331 @<file> Read options from <file>\n"));
5332 fprintf (stream, _("\
5333 -H --help Display this information\n"));
5334 fprintf (stream, _("\
5335 -v --version Display the version number of readelf\n"));
5336
5337 if (REPORT_BUGS_TO[0] && stream == stdout)
5338 fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
5339
5340 exit (stream == stdout ? 0 : 1);
5341 }
5342
5343 /* Record the fact that the user wants the contents of section number
5344 SECTION to be displayed using the method(s) encoded as flags bits
5345 in TYPE. Note, TYPE can be zero if we are creating the array for
5346 the first time. */
5347
5348 static void
5349 request_dump_bynumber (struct dump_data *dumpdata,
5350 unsigned int section, dump_type type)
5351 {
5352 if (section >= dumpdata->num_dump_sects)
5353 {
5354 dump_type * new_dump_sects;
5355
5356 new_dump_sects = (dump_type *) calloc (section + 1,
5357 sizeof (* new_dump_sects));
5358
5359 if (new_dump_sects == NULL)
5360 error (_("Out of memory allocating dump request table.\n"));
5361 else
5362 {
5363 if (dumpdata->dump_sects)
5364 {
5365 /* Copy current flag settings. */
5366 memcpy (new_dump_sects, dumpdata->dump_sects,
5367 dumpdata->num_dump_sects * sizeof (* new_dump_sects));
5368
5369 free (dumpdata->dump_sects);
5370 }
5371
5372 dumpdata->dump_sects = new_dump_sects;
5373 dumpdata->num_dump_sects = section + 1;
5374 }
5375 }
5376
5377 if (dumpdata->dump_sects)
5378 dumpdata->dump_sects[section] |= type;
5379 }
5380
5381 /* Request a dump by section name. */
5382
5383 static void
5384 request_dump_byname (const char * section, dump_type type)
5385 {
5386 struct dump_list_entry * new_request;
5387
5388 new_request = (struct dump_list_entry *)
5389 malloc (sizeof (struct dump_list_entry));
5390 if (!new_request)
5391 error (_("Out of memory allocating dump request table.\n"));
5392
5393 new_request->name = strdup (section);
5394 if (!new_request->name)
5395 error (_("Out of memory allocating dump request table.\n"));
5396
5397 new_request->type = type;
5398
5399 new_request->next = dump_sects_byname;
5400 dump_sects_byname = new_request;
5401 }
5402
5403 static inline void
5404 request_dump (struct dump_data *dumpdata, dump_type type)
5405 {
5406 int section;
5407 char * cp;
5408
5409 do_dump = true;
5410 section = strtoul (optarg, & cp, 0);
5411
5412 if (! *cp && section >= 0)
5413 request_dump_bynumber (dumpdata, section, type);
5414 else
5415 request_dump_byname (optarg, type);
5416 }
5417
5418 static void
5419 parse_args (struct dump_data *dumpdata, int argc, char ** argv)
5420 {
5421 int c;
5422
5423 if (argc < 2)
5424 usage (stderr);
5425
5426 while ((c = getopt_long
5427 (argc, argv, "ACDHILNPR:STU:VWacdeghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
5428 {
5429 switch (c)
5430 {
5431 case 0:
5432 /* Long options. */
5433 break;
5434 case 'H':
5435 usage (stdout);
5436 break;
5437
5438 case 'a':
5439 do_syms = true;
5440 do_reloc = true;
5441 do_unwind = true;
5442 do_dynamic = true;
5443 do_header = true;
5444 do_sections = true;
5445 do_section_groups = true;
5446 do_segments = true;
5447 do_version = true;
5448 do_histogram = true;
5449 do_arch = true;
5450 do_notes = true;
5451 break;
5452
5453 case 'g':
5454 do_section_groups = true;
5455 break;
5456 case 't':
5457 case 'N':
5458 do_sections = true;
5459 do_section_details = true;
5460 break;
5461 case 'e':
5462 do_header = true;
5463 do_sections = true;
5464 do_segments = true;
5465 break;
5466 case 'A':
5467 do_arch = true;
5468 break;
5469 case 'D':
5470 do_using_dynamic = true;
5471 break;
5472 case 'r':
5473 do_reloc = true;
5474 break;
5475 case 'u':
5476 do_unwind = true;
5477 break;
5478 case 'h':
5479 do_header = true;
5480 break;
5481 case 'l':
5482 do_segments = true;
5483 break;
5484 case 's':
5485 do_syms = true;
5486 break;
5487 case 'S':
5488 do_sections = true;
5489 break;
5490 case 'd':
5491 do_dynamic = true;
5492 break;
5493 case 'I':
5494 do_histogram = true;
5495 break;
5496 case 'n':
5497 do_notes = true;
5498 break;
5499 case 'c':
5500 do_archive_index = true;
5501 break;
5502 case 'L':
5503 do_checks = true;
5504 break;
5505 case 'P':
5506 process_links = true;
5507 do_follow_links = true;
5508 dump_any_debugging = true;
5509 break;
5510 case 'x':
5511 request_dump (dumpdata, HEX_DUMP);
5512 break;
5513 case 'p':
5514 request_dump (dumpdata, STRING_DUMP);
5515 break;
5516 case 'R':
5517 request_dump (dumpdata, RELOC_DUMP);
5518 break;
5519 case 'z':
5520 decompress_dumps = true;
5521 break;
5522 case 'w':
5523 if (optarg == NULL)
5524 {
5525 do_debugging = true;
5526 do_dump = true;
5527 dump_any_debugging = true;
5528 dwarf_select_sections_all ();
5529 }
5530 else
5531 {
5532 do_debugging = false;
5533 if (dwarf_select_sections_by_letters (optarg))
5534 {
5535 do_dump = true;
5536 dump_any_debugging = true;
5537 }
5538 }
5539 break;
5540 case OPTION_DEBUG_DUMP:
5541 if (optarg == NULL)
5542 {
5543 do_dump = true;
5544 do_debugging = true;
5545 dump_any_debugging = true;
5546 dwarf_select_sections_all ();
5547 }
5548 else
5549 {
5550 do_debugging = false;
5551 if (dwarf_select_sections_by_names (optarg))
5552 {
5553 do_dump = true;
5554 dump_any_debugging = true;
5555 }
5556 }
5557 break;
5558 case OPTION_DWARF_DEPTH:
5559 {
5560 char *cp;
5561
5562 dwarf_cutoff_level = strtoul (optarg, & cp, 0);
5563 }
5564 break;
5565 case OPTION_DWARF_START:
5566 {
5567 char *cp;
5568
5569 dwarf_start_die = strtoul (optarg, & cp, 0);
5570 }
5571 break;
5572 case OPTION_DWARF_CHECK:
5573 dwarf_check = true;
5574 break;
5575 case OPTION_CTF_DUMP:
5576 do_ctf = true;
5577 request_dump (dumpdata, CTF_DUMP);
5578 break;
5579 case OPTION_CTF_SYMBOLS:
5580 free (dump_ctf_symtab_name);
5581 dump_ctf_symtab_name = strdup (optarg);
5582 break;
5583 case OPTION_CTF_STRINGS:
5584 free (dump_ctf_strtab_name);
5585 dump_ctf_strtab_name = strdup (optarg);
5586 break;
5587 case OPTION_CTF_PARENT:
5588 free (dump_ctf_parent_name);
5589 dump_ctf_parent_name = strdup (optarg);
5590 break;
5591 case OPTION_DYN_SYMS:
5592 do_dyn_syms = true;
5593 break;
5594 case OPTION_LTO_SYMS:
5595 do_lto_syms = true;
5596 break;
5597 #ifdef SUPPORT_DISASSEMBLY
5598 case 'i':
5599 request_dump (dumpdata, DISASS_DUMP);
5600 break;
5601 #endif
5602 case 'v':
5603 print_version (program_name);
5604 break;
5605 case 'V':
5606 do_version = true;
5607 break;
5608 case 'W':
5609 do_wide = true;
5610 break;
5611 case 'T':
5612 do_not_show_symbol_truncation = true;
5613 break;
5614 case 'C':
5615 do_demangle = true;
5616 if (optarg != NULL)
5617 {
5618 enum demangling_styles style;
5619
5620 style = cplus_demangle_name_to_style (optarg);
5621 if (style == unknown_demangling)
5622 error (_("unknown demangling style `%s'"), optarg);
5623
5624 cplus_demangle_set_style (style);
5625 }
5626 break;
5627 case OPTION_NO_DEMANGLING:
5628 do_demangle = false;
5629 break;
5630 case OPTION_RECURSE_LIMIT:
5631 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
5632 break;
5633 case OPTION_NO_RECURSE_LIMIT:
5634 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
5635 break;
5636 case OPTION_WITH_SYMBOL_VERSIONS:
5637 /* Ignored for backward compatibility. */
5638 break;
5639
5640 case 'U':
5641 if (optarg == NULL)
5642 error (_("Missing arg to -U/--unicode")); /* Can this happen ? */
5643 else if (streq (optarg, "default") || streq (optarg, "d"))
5644 unicode_display = unicode_default;
5645 else if (streq (optarg, "locale") || streq (optarg, "l"))
5646 unicode_display = unicode_locale;
5647 else if (streq (optarg, "escape") || streq (optarg, "e"))
5648 unicode_display = unicode_escape;
5649 else if (streq (optarg, "invalid") || streq (optarg, "i"))
5650 unicode_display = unicode_invalid;
5651 else if (streq (optarg, "hex") || streq (optarg, "x"))
5652 unicode_display = unicode_hex;
5653 else if (streq (optarg, "highlight") || streq (optarg, "h"))
5654 unicode_display = unicode_highlight;
5655 else
5656 error (_("invalid argument to -U/--unicode: %s"), optarg);
5657 break;
5658
5659 case OPTION_SYM_BASE:
5660 sym_base = 0;
5661 if (optarg != NULL)
5662 {
5663 sym_base = strtoul (optarg, NULL, 0);
5664 switch (sym_base)
5665 {
5666 case 0:
5667 case 8:
5668 case 10:
5669 case 16:
5670 break;
5671
5672 default:
5673 sym_base = 0;
5674 break;
5675 }
5676 }
5677 break;
5678
5679 default:
5680 /* xgettext:c-format */
5681 error (_("Invalid option '-%c'\n"), c);
5682 /* Fall through. */
5683 case '?':
5684 usage (stderr);
5685 }
5686 }
5687
5688 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
5689 && !do_segments && !do_header && !do_dump && !do_version
5690 && !do_histogram && !do_debugging && !do_arch && !do_notes
5691 && !do_section_groups && !do_archive_index
5692 && !do_dyn_syms && !do_lto_syms)
5693 {
5694 if (do_checks)
5695 {
5696 check_all = true;
5697 do_dynamic = do_syms = do_reloc = do_unwind = do_sections = true;
5698 do_segments = do_header = do_dump = do_version = true;
5699 do_histogram = do_debugging = do_arch = do_notes = true;
5700 do_section_groups = do_archive_index = do_dyn_syms = true;
5701 do_lto_syms = true;
5702 }
5703 else
5704 usage (stderr);
5705 }
5706 }
5707
5708 static const char *
5709 get_elf_class (unsigned int elf_class)
5710 {
5711 static char buff[32];
5712
5713 switch (elf_class)
5714 {
5715 case ELFCLASSNONE: return _("none");
5716 case ELFCLASS32: return "ELF32";
5717 case ELFCLASS64: return "ELF64";
5718 default:
5719 snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
5720 return buff;
5721 }
5722 }
5723
5724 static const char *
5725 get_data_encoding (unsigned int encoding)
5726 {
5727 static char buff[32];
5728
5729 switch (encoding)
5730 {
5731 case ELFDATANONE: return _("none");
5732 case ELFDATA2LSB: return _("2's complement, little endian");
5733 case ELFDATA2MSB: return _("2's complement, big endian");
5734 default:
5735 snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
5736 return buff;
5737 }
5738 }
5739
5740 /* Decode the data held in 'filedata->file_header'. */
5741
5742 static bool
5743 process_file_header (Filedata * filedata)
5744 {
5745 Elf_Internal_Ehdr * header = & filedata->file_header;
5746
5747 if ( header->e_ident[EI_MAG0] != ELFMAG0
5748 || header->e_ident[EI_MAG1] != ELFMAG1
5749 || header->e_ident[EI_MAG2] != ELFMAG2
5750 || header->e_ident[EI_MAG3] != ELFMAG3)
5751 {
5752 error
5753 (_("Not an ELF file - it has the wrong magic bytes at the start\n"));
5754 return false;
5755 }
5756
5757 if (! filedata->is_separate)
5758 init_dwarf_regnames_by_elf_machine_code (header->e_machine);
5759
5760 if (do_header)
5761 {
5762 unsigned i;
5763
5764 if (filedata->is_separate)
5765 printf (_("ELF Header in linked file '%s':\n"), filedata->file_name);
5766 else
5767 printf (_("ELF Header:\n"));
5768 printf (_(" Magic: "));
5769 for (i = 0; i < EI_NIDENT; i++)
5770 printf ("%2.2x ", header->e_ident[i]);
5771 printf ("\n");
5772 printf (_(" Class: %s\n"),
5773 get_elf_class (header->e_ident[EI_CLASS]));
5774 printf (_(" Data: %s\n"),
5775 get_data_encoding (header->e_ident[EI_DATA]));
5776 printf (_(" Version: %d%s\n"),
5777 header->e_ident[EI_VERSION],
5778 (header->e_ident[EI_VERSION] == EV_CURRENT
5779 ? _(" (current)")
5780 : (header->e_ident[EI_VERSION] != EV_NONE
5781 ? _(" <unknown>")
5782 : "")));
5783 printf (_(" OS/ABI: %s\n"),
5784 get_osabi_name (filedata, header->e_ident[EI_OSABI]));
5785 printf (_(" ABI Version: %d\n"),
5786 header->e_ident[EI_ABIVERSION]);
5787 printf (_(" Type: %s\n"),
5788 get_file_type (filedata));
5789 printf (_(" Machine: %s\n"),
5790 get_machine_name (header->e_machine));
5791 printf (_(" Version: 0x%lx\n"),
5792 header->e_version);
5793
5794 printf (_(" Entry point address: "));
5795 print_vma (header->e_entry, PREFIX_HEX);
5796 printf (_("\n Start of program headers: "));
5797 print_vma (header->e_phoff, DEC);
5798 printf (_(" (bytes into file)\n Start of section headers: "));
5799 print_vma (header->e_shoff, DEC);
5800 printf (_(" (bytes into file)\n"));
5801
5802 printf (_(" Flags: 0x%lx%s\n"),
5803 header->e_flags,
5804 get_machine_flags (filedata, header->e_flags, header->e_machine));
5805 printf (_(" Size of this header: %u (bytes)\n"),
5806 header->e_ehsize);
5807 printf (_(" Size of program headers: %u (bytes)\n"),
5808 header->e_phentsize);
5809 printf (_(" Number of program headers: %u"),
5810 header->e_phnum);
5811 if (filedata->section_headers != NULL
5812 && header->e_phnum == PN_XNUM
5813 && filedata->section_headers[0].sh_info != 0)
5814 printf (" (%u)", filedata->section_headers[0].sh_info);
5815 putc ('\n', stdout);
5816 printf (_(" Size of section headers: %u (bytes)\n"),
5817 header->e_shentsize);
5818 printf (_(" Number of section headers: %u"),
5819 header->e_shnum);
5820 if (filedata->section_headers != NULL && header->e_shnum == SHN_UNDEF)
5821 {
5822 header->e_shnum = filedata->section_headers[0].sh_size;
5823 printf (" (%u)", header->e_shnum);
5824 }
5825 putc ('\n', stdout);
5826 printf (_(" Section header string table index: %u"),
5827 header->e_shstrndx);
5828 if (filedata->section_headers != NULL
5829 && header->e_shstrndx == (SHN_XINDEX & 0xffff))
5830 {
5831 header->e_shstrndx = filedata->section_headers[0].sh_link;
5832 printf (" (%u)", header->e_shstrndx);
5833 }
5834 if (header->e_shstrndx != SHN_UNDEF
5835 && header->e_shstrndx >= header->e_shnum)
5836 {
5837 header->e_shstrndx = SHN_UNDEF;
5838 printf (_(" <corrupt: out of range>"));
5839 }
5840 putc ('\n', stdout);
5841 }
5842
5843 if (filedata->section_headers != NULL)
5844 {
5845 if (header->e_phnum == PN_XNUM
5846 && filedata->section_headers[0].sh_info != 0)
5847 {
5848 /* Throw away any cached read of PN_XNUM headers. */
5849 free (filedata->program_headers);
5850 filedata->program_headers = NULL;
5851 header->e_phnum = filedata->section_headers[0].sh_info;
5852 }
5853 if (header->e_shnum == SHN_UNDEF)
5854 header->e_shnum = filedata->section_headers[0].sh_size;
5855 if (header->e_shstrndx == (SHN_XINDEX & 0xffff))
5856 header->e_shstrndx = filedata->section_headers[0].sh_link;
5857 if (header->e_shstrndx >= header->e_shnum)
5858 header->e_shstrndx = SHN_UNDEF;
5859 }
5860
5861 return true;
5862 }
5863
5864 /* Read in the program headers from FILEDATA and store them in PHEADERS.
5865 Returns TRUE upon success, FALSE otherwise. Loads 32-bit headers. */
5866
5867 static bool
5868 get_32bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
5869 {
5870 Elf32_External_Phdr * phdrs;
5871 Elf32_External_Phdr * external;
5872 Elf_Internal_Phdr * internal;
5873 unsigned int i;
5874 unsigned int size = filedata->file_header.e_phentsize;
5875 unsigned int num = filedata->file_header.e_phnum;
5876
5877 /* PR binutils/17531: Cope with unexpected section header sizes. */
5878 if (size == 0 || num == 0)
5879 return false;
5880 if (size < sizeof * phdrs)
5881 {
5882 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
5883 return false;
5884 }
5885 if (size > sizeof * phdrs)
5886 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
5887
5888 phdrs = (Elf32_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
5889 size, num, _("program headers"));
5890 if (phdrs == NULL)
5891 return false;
5892
5893 for (i = 0, internal = pheaders, external = phdrs;
5894 i < filedata->file_header.e_phnum;
5895 i++, internal++, external++)
5896 {
5897 internal->p_type = BYTE_GET (external->p_type);
5898 internal->p_offset = BYTE_GET (external->p_offset);
5899 internal->p_vaddr = BYTE_GET (external->p_vaddr);
5900 internal->p_paddr = BYTE_GET (external->p_paddr);
5901 internal->p_filesz = BYTE_GET (external->p_filesz);
5902 internal->p_memsz = BYTE_GET (external->p_memsz);
5903 internal->p_flags = BYTE_GET (external->p_flags);
5904 internal->p_align = BYTE_GET (external->p_align);
5905 }
5906
5907 free (phdrs);
5908 return true;
5909 }
5910
5911 /* Read in the program headers from FILEDATA and store them in PHEADERS.
5912 Returns TRUE upon success, FALSE otherwise. Loads 64-bit headers. */
5913
5914 static bool
5915 get_64bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
5916 {
5917 Elf64_External_Phdr * phdrs;
5918 Elf64_External_Phdr * external;
5919 Elf_Internal_Phdr * internal;
5920 unsigned int i;
5921 unsigned int size = filedata->file_header.e_phentsize;
5922 unsigned int num = filedata->file_header.e_phnum;
5923
5924 /* PR binutils/17531: Cope with unexpected section header sizes. */
5925 if (size == 0 || num == 0)
5926 return false;
5927 if (size < sizeof * phdrs)
5928 {
5929 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
5930 return false;
5931 }
5932 if (size > sizeof * phdrs)
5933 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
5934
5935 phdrs = (Elf64_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
5936 size, num, _("program headers"));
5937 if (!phdrs)
5938 return false;
5939
5940 for (i = 0, internal = pheaders, external = phdrs;
5941 i < filedata->file_header.e_phnum;
5942 i++, internal++, external++)
5943 {
5944 internal->p_type = BYTE_GET (external->p_type);
5945 internal->p_flags = BYTE_GET (external->p_flags);
5946 internal->p_offset = BYTE_GET (external->p_offset);
5947 internal->p_vaddr = BYTE_GET (external->p_vaddr);
5948 internal->p_paddr = BYTE_GET (external->p_paddr);
5949 internal->p_filesz = BYTE_GET (external->p_filesz);
5950 internal->p_memsz = BYTE_GET (external->p_memsz);
5951 internal->p_align = BYTE_GET (external->p_align);
5952 }
5953
5954 free (phdrs);
5955 return true;
5956 }
5957
5958 /* Returns TRUE if the program headers were read into `program_headers'. */
5959
5960 static bool
5961 get_program_headers (Filedata * filedata)
5962 {
5963 Elf_Internal_Phdr * phdrs;
5964
5965 /* Check cache of prior read. */
5966 if (filedata->program_headers != NULL)
5967 return true;
5968
5969 /* Be kind to memory checkers by looking for
5970 e_phnum values which we know must be invalid. */
5971 if (filedata->file_header.e_phnum
5972 * (is_32bit_elf ? sizeof (Elf32_External_Phdr) : sizeof (Elf64_External_Phdr))
5973 >= filedata->file_size)
5974 {
5975 error (_("Too many program headers - %#x - the file is not that big\n"),
5976 filedata->file_header.e_phnum);
5977 return false;
5978 }
5979
5980 phdrs = (Elf_Internal_Phdr *) cmalloc (filedata->file_header.e_phnum,
5981 sizeof (Elf_Internal_Phdr));
5982 if (phdrs == NULL)
5983 {
5984 error (_("Out of memory reading %u program headers\n"),
5985 filedata->file_header.e_phnum);
5986 return false;
5987 }
5988
5989 if (is_32bit_elf
5990 ? get_32bit_program_headers (filedata, phdrs)
5991 : get_64bit_program_headers (filedata, phdrs))
5992 {
5993 filedata->program_headers = phdrs;
5994 return true;
5995 }
5996
5997 free (phdrs);
5998 return false;
5999 }
6000
6001 /* Print program header info and locate dynamic section. */
6002
6003 static void
6004 process_program_headers (Filedata * filedata)
6005 {
6006 Elf_Internal_Phdr * segment;
6007 unsigned int i;
6008 Elf_Internal_Phdr * previous_load = NULL;
6009
6010 if (filedata->file_header.e_phnum == 0)
6011 {
6012 /* PR binutils/12467. */
6013 if (filedata->file_header.e_phoff != 0)
6014 warn (_("possibly corrupt ELF header - it has a non-zero program"
6015 " header offset, but no program headers\n"));
6016 else if (do_segments)
6017 {
6018 if (filedata->is_separate)
6019 printf (_("\nThere are no program headers in linked file '%s'.\n"),
6020 filedata->file_name);
6021 else
6022 printf (_("\nThere are no program headers in this file.\n"));
6023 }
6024 goto no_headers;
6025 }
6026
6027 if (do_segments && !do_header)
6028 {
6029 if (filedata->is_separate)
6030 printf ("\nIn linked file '%s' the ELF file type is %s\n",
6031 filedata->file_name, get_file_type (filedata));
6032 else
6033 printf (_("\nElf file type is %s\n"), get_file_type (filedata));
6034 printf (_("Entry point 0x%s\n"), bfd_vmatoa ("x", filedata->file_header.e_entry));
6035 printf (ngettext ("There is %d program header, starting at offset %s\n",
6036 "There are %d program headers, starting at offset %s\n",
6037 filedata->file_header.e_phnum),
6038 filedata->file_header.e_phnum,
6039 bfd_vmatoa ("u", filedata->file_header.e_phoff));
6040 }
6041
6042 if (! get_program_headers (filedata))
6043 goto no_headers;
6044
6045 if (do_segments)
6046 {
6047 if (filedata->file_header.e_phnum > 1)
6048 printf (_("\nProgram Headers:\n"));
6049 else
6050 printf (_("\nProgram Headers:\n"));
6051
6052 if (is_32bit_elf)
6053 printf
6054 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
6055 else if (do_wide)
6056 printf
6057 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
6058 else
6059 {
6060 printf
6061 (_(" Type Offset VirtAddr PhysAddr\n"));
6062 printf
6063 (_(" FileSiz MemSiz Flags Align\n"));
6064 }
6065 }
6066
6067 unsigned long dynamic_addr = 0;
6068 bfd_size_type dynamic_size = 0;
6069 for (i = 0, segment = filedata->program_headers;
6070 i < filedata->file_header.e_phnum;
6071 i++, segment++)
6072 {
6073 if (do_segments)
6074 {
6075 printf (" %-14.14s ", get_segment_type (filedata, segment->p_type));
6076
6077 if (is_32bit_elf)
6078 {
6079 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
6080 printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
6081 printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
6082 printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
6083 printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
6084 printf ("%c%c%c ",
6085 (segment->p_flags & PF_R ? 'R' : ' '),
6086 (segment->p_flags & PF_W ? 'W' : ' '),
6087 (segment->p_flags & PF_X ? 'E' : ' '));
6088 printf ("%#lx", (unsigned long) segment->p_align);
6089 }
6090 else if (do_wide)
6091 {
6092 if ((unsigned long) segment->p_offset == segment->p_offset)
6093 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
6094 else
6095 {
6096 print_vma (segment->p_offset, FULL_HEX);
6097 putchar (' ');
6098 }
6099
6100 print_vma (segment->p_vaddr, FULL_HEX);
6101 putchar (' ');
6102 print_vma (segment->p_paddr, FULL_HEX);
6103 putchar (' ');
6104
6105 if ((unsigned long) segment->p_filesz == segment->p_filesz)
6106 printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
6107 else
6108 {
6109 print_vma (segment->p_filesz, FULL_HEX);
6110 putchar (' ');
6111 }
6112
6113 if ((unsigned long) segment->p_memsz == segment->p_memsz)
6114 printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
6115 else
6116 {
6117 print_vma (segment->p_memsz, FULL_HEX);
6118 }
6119
6120 printf (" %c%c%c ",
6121 (segment->p_flags & PF_R ? 'R' : ' '),
6122 (segment->p_flags & PF_W ? 'W' : ' '),
6123 (segment->p_flags & PF_X ? 'E' : ' '));
6124
6125 if ((unsigned long) segment->p_align == segment->p_align)
6126 printf ("%#lx", (unsigned long) segment->p_align);
6127 else
6128 {
6129 print_vma (segment->p_align, PREFIX_HEX);
6130 }
6131 }
6132 else
6133 {
6134 print_vma (segment->p_offset, FULL_HEX);
6135 putchar (' ');
6136 print_vma (segment->p_vaddr, FULL_HEX);
6137 putchar (' ');
6138 print_vma (segment->p_paddr, FULL_HEX);
6139 printf ("\n ");
6140 print_vma (segment->p_filesz, FULL_HEX);
6141 putchar (' ');
6142 print_vma (segment->p_memsz, FULL_HEX);
6143 printf (" %c%c%c ",
6144 (segment->p_flags & PF_R ? 'R' : ' '),
6145 (segment->p_flags & PF_W ? 'W' : ' '),
6146 (segment->p_flags & PF_X ? 'E' : ' '));
6147 print_vma (segment->p_align, PREFIX_HEX);
6148 }
6149
6150 putc ('\n', stdout);
6151 }
6152
6153 switch (segment->p_type)
6154 {
6155 case PT_LOAD:
6156 #if 0 /* Do not warn about out of order PT_LOAD segments. Although officially
6157 required by the ELF standard, several programs, including the Linux
6158 kernel, make use of non-ordered segments. */
6159 if (previous_load
6160 && previous_load->p_vaddr > segment->p_vaddr)
6161 error (_("LOAD segments must be sorted in order of increasing VirtAddr\n"));
6162 #endif
6163 if (segment->p_memsz < segment->p_filesz)
6164 error (_("the segment's file size is larger than its memory size\n"));
6165 previous_load = segment;
6166 break;
6167
6168 case PT_PHDR:
6169 /* PR 20815 - Verify that the program header is loaded into memory. */
6170 if (i > 0 && previous_load != NULL)
6171 error (_("the PHDR segment must occur before any LOAD segment\n"));
6172 if (filedata->file_header.e_machine != EM_PARISC)
6173 {
6174 unsigned int j;
6175
6176 for (j = 1; j < filedata->file_header.e_phnum; j++)
6177 {
6178 Elf_Internal_Phdr *load = filedata->program_headers + j;
6179 if (load->p_type == PT_LOAD
6180 && load->p_offset <= segment->p_offset
6181 && (load->p_offset + load->p_filesz
6182 >= segment->p_offset + segment->p_filesz)
6183 && load->p_vaddr <= segment->p_vaddr
6184 && (load->p_vaddr + load->p_filesz
6185 >= segment->p_vaddr + segment->p_filesz))
6186 break;
6187 }
6188 if (j == filedata->file_header.e_phnum)
6189 error (_("the PHDR segment is not covered by a LOAD segment\n"));
6190 }
6191 break;
6192
6193 case PT_DYNAMIC:
6194 if (dynamic_addr)
6195 error (_("more than one dynamic segment\n"));
6196
6197 /* By default, assume that the .dynamic section is the first
6198 section in the DYNAMIC segment. */
6199 dynamic_addr = segment->p_offset;
6200 dynamic_size = segment->p_filesz;
6201
6202 /* Try to locate the .dynamic section. If there is
6203 a section header table, we can easily locate it. */
6204 if (filedata->section_headers != NULL)
6205 {
6206 Elf_Internal_Shdr * sec;
6207
6208 sec = find_section (filedata, ".dynamic");
6209 if (sec == NULL || sec->sh_size == 0)
6210 {
6211 /* A corresponding .dynamic section is expected, but on
6212 IA-64/OpenVMS it is OK for it to be missing. */
6213 if (!is_ia64_vms (filedata))
6214 error (_("no .dynamic section in the dynamic segment\n"));
6215 break;
6216 }
6217
6218 if (sec->sh_type == SHT_NOBITS)
6219 {
6220 dynamic_addr = 0;
6221 dynamic_size = 0;
6222 break;
6223 }
6224
6225 dynamic_addr = sec->sh_offset;
6226 dynamic_size = sec->sh_size;
6227
6228 /* The PT_DYNAMIC segment, which is used by the run-time
6229 loader, should exactly match the .dynamic section. */
6230 if (do_checks
6231 && (dynamic_addr != segment->p_offset
6232 || dynamic_size != segment->p_filesz))
6233 warn (_("\
6234 the .dynamic section is not the same as the dynamic segment\n"));
6235 }
6236
6237 /* PR binutils/17512: Avoid corrupt dynamic section info in the
6238 segment. Check this after matching against the section headers
6239 so we don't warn on debuginfo file (which have NOBITS .dynamic
6240 sections). */
6241 if (dynamic_addr > filedata->file_size
6242 || (dynamic_size > filedata->file_size - dynamic_addr))
6243 {
6244 error (_("the dynamic segment offset + size exceeds the size of the file\n"));
6245 dynamic_addr = 0;
6246 dynamic_size = 0;
6247 }
6248 break;
6249
6250 case PT_INTERP:
6251 if (segment->p_offset >= filedata->file_size
6252 || segment->p_filesz > filedata->file_size - segment->p_offset
6253 || segment->p_filesz - 1 >= (size_t) -2
6254 || fseek (filedata->handle,
6255 filedata->archive_file_offset + (long) segment->p_offset,
6256 SEEK_SET))
6257 error (_("Unable to find program interpreter name\n"));
6258 else
6259 {
6260 size_t len = segment->p_filesz;
6261 free (filedata->program_interpreter);
6262 filedata->program_interpreter = xmalloc (len + 1);
6263 len = fread (filedata->program_interpreter, 1, len,
6264 filedata->handle);
6265 filedata->program_interpreter[len] = 0;
6266
6267 if (do_segments)
6268 printf (_(" [Requesting program interpreter: %s]\n"),
6269 filedata->program_interpreter);
6270 }
6271 break;
6272 }
6273 }
6274
6275 if (do_segments
6276 && filedata->section_headers != NULL
6277 && filedata->string_table != NULL)
6278 {
6279 printf (_("\n Section to Segment mapping:\n"));
6280 printf (_(" Segment Sections...\n"));
6281
6282 for (i = 0; i < filedata->file_header.e_phnum; i++)
6283 {
6284 unsigned int j;
6285 Elf_Internal_Shdr * section;
6286
6287 segment = filedata->program_headers + i;
6288 section = filedata->section_headers + 1;
6289
6290 printf (" %2.2d ", i);
6291
6292 for (j = 1; j < filedata->file_header.e_shnum; j++, section++)
6293 {
6294 if (!ELF_TBSS_SPECIAL (section, segment)
6295 && ELF_SECTION_IN_SEGMENT_STRICT (section, segment))
6296 printf ("%s ", printable_section_name (filedata, section));
6297 }
6298
6299 putc ('\n',stdout);
6300 }
6301 }
6302
6303 filedata->dynamic_addr = dynamic_addr;
6304 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
6305 return;
6306
6307 no_headers:
6308 filedata->dynamic_addr = 0;
6309 filedata->dynamic_size = 1;
6310 }
6311
6312
6313 /* Find the file offset corresponding to VMA by using the program headers. */
6314
6315 static long
6316 offset_from_vma (Filedata * filedata, bfd_vma vma, bfd_size_type size)
6317 {
6318 Elf_Internal_Phdr * seg;
6319
6320 if (! get_program_headers (filedata))
6321 {
6322 warn (_("Cannot interpret virtual addresses without program headers.\n"));
6323 return (long) vma;
6324 }
6325
6326 for (seg = filedata->program_headers;
6327 seg < filedata->program_headers + filedata->file_header.e_phnum;
6328 ++seg)
6329 {
6330 if (seg->p_type != PT_LOAD)
6331 continue;
6332
6333 if (vma >= (seg->p_vaddr & -seg->p_align)
6334 && vma + size <= seg->p_vaddr + seg->p_filesz)
6335 return vma - seg->p_vaddr + seg->p_offset;
6336 }
6337
6338 warn (_("Virtual address 0x%lx not located in any PT_LOAD segment.\n"),
6339 (unsigned long) vma);
6340 return (long) vma;
6341 }
6342
6343
6344 /* Allocate memory and load the sections headers into FILEDATA->filedata->section_headers.
6345 If PROBE is true, this is just a probe and we do not generate any error
6346 messages if the load fails. */
6347
6348 static bool
6349 get_32bit_section_headers (Filedata * filedata, bool probe)
6350 {
6351 Elf32_External_Shdr * shdrs;
6352 Elf_Internal_Shdr * internal;
6353 unsigned int i;
6354 unsigned int size = filedata->file_header.e_shentsize;
6355 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
6356
6357 /* PR binutils/17531: Cope with unexpected section header sizes. */
6358 if (size == 0 || num == 0)
6359 return false;
6360 if (size < sizeof * shdrs)
6361 {
6362 if (! probe)
6363 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
6364 return false;
6365 }
6366 if (!probe && size > sizeof * shdrs)
6367 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
6368
6369 shdrs = (Elf32_External_Shdr *) get_data (NULL, filedata, filedata->file_header.e_shoff,
6370 size, num,
6371 probe ? NULL : _("section headers"));
6372 if (shdrs == NULL)
6373 return false;
6374
6375 filedata->section_headers = (Elf_Internal_Shdr *)
6376 cmalloc (num, sizeof (Elf_Internal_Shdr));
6377 if (filedata->section_headers == NULL)
6378 {
6379 if (!probe)
6380 error (_("Out of memory reading %u section headers\n"), num);
6381 free (shdrs);
6382 return false;
6383 }
6384
6385 for (i = 0, internal = filedata->section_headers;
6386 i < num;
6387 i++, internal++)
6388 {
6389 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
6390 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
6391 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
6392 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
6393 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
6394 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
6395 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
6396 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
6397 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
6398 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
6399 if (!probe && internal->sh_link > num)
6400 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
6401 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
6402 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
6403 }
6404
6405 free (shdrs);
6406 return true;
6407 }
6408
6409 /* Like get_32bit_section_headers, except that it fetches 64-bit headers. */
6410
6411 static bool
6412 get_64bit_section_headers (Filedata * filedata, bool probe)
6413 {
6414 Elf64_External_Shdr * shdrs;
6415 Elf_Internal_Shdr * internal;
6416 unsigned int i;
6417 unsigned int size = filedata->file_header.e_shentsize;
6418 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
6419
6420 /* PR binutils/17531: Cope with unexpected section header sizes. */
6421 if (size == 0 || num == 0)
6422 return false;
6423
6424 if (size < sizeof * shdrs)
6425 {
6426 if (! probe)
6427 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
6428 return false;
6429 }
6430
6431 if (! probe && size > sizeof * shdrs)
6432 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
6433
6434 shdrs = (Elf64_External_Shdr *) get_data (NULL, filedata,
6435 filedata->file_header.e_shoff,
6436 size, num,
6437 probe ? NULL : _("section headers"));
6438 if (shdrs == NULL)
6439 return false;
6440
6441 filedata->section_headers = (Elf_Internal_Shdr *)
6442 cmalloc (num, sizeof (Elf_Internal_Shdr));
6443 if (filedata->section_headers == NULL)
6444 {
6445 if (! probe)
6446 error (_("Out of memory reading %u section headers\n"), num);
6447 free (shdrs);
6448 return false;
6449 }
6450
6451 for (i = 0, internal = filedata->section_headers;
6452 i < num;
6453 i++, internal++)
6454 {
6455 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
6456 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
6457 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
6458 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
6459 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
6460 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
6461 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
6462 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
6463 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
6464 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
6465 if (!probe && internal->sh_link > num)
6466 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
6467 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
6468 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
6469 }
6470
6471 free (shdrs);
6472 return true;
6473 }
6474
6475 static bool
6476 get_section_headers (Filedata *filedata, bool probe)
6477 {
6478 if (filedata->section_headers != NULL)
6479 return true;
6480
6481 if (is_32bit_elf)
6482 return get_32bit_section_headers (filedata, probe);
6483 else
6484 return get_64bit_section_headers (filedata, probe);
6485 }
6486
6487 static Elf_Internal_Sym *
6488 get_32bit_elf_symbols (Filedata * filedata,
6489 Elf_Internal_Shdr * section,
6490 unsigned long * num_syms_return)
6491 {
6492 unsigned long number = 0;
6493 Elf32_External_Sym * esyms = NULL;
6494 Elf_External_Sym_Shndx * shndx = NULL;
6495 Elf_Internal_Sym * isyms = NULL;
6496 Elf_Internal_Sym * psym;
6497 unsigned int j;
6498 elf_section_list * entry;
6499
6500 if (section->sh_size == 0)
6501 {
6502 if (num_syms_return != NULL)
6503 * num_syms_return = 0;
6504 return NULL;
6505 }
6506
6507 /* Run some sanity checks first. */
6508 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
6509 {
6510 error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
6511 printable_section_name (filedata, section),
6512 (unsigned long) section->sh_entsize);
6513 goto exit_point;
6514 }
6515
6516 if (section->sh_size > filedata->file_size)
6517 {
6518 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
6519 printable_section_name (filedata, section),
6520 (unsigned long) section->sh_size);
6521 goto exit_point;
6522 }
6523
6524 number = section->sh_size / section->sh_entsize;
6525
6526 if (number * sizeof (Elf32_External_Sym) > section->sh_size + 1)
6527 {
6528 error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
6529 (unsigned long) section->sh_size,
6530 printable_section_name (filedata, section),
6531 (unsigned long) section->sh_entsize);
6532 goto exit_point;
6533 }
6534
6535 esyms = (Elf32_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
6536 section->sh_size, _("symbols"));
6537 if (esyms == NULL)
6538 goto exit_point;
6539
6540 shndx = NULL;
6541 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
6542 {
6543 if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
6544 continue;
6545
6546 if (shndx != NULL)
6547 {
6548 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
6549 free (shndx);
6550 }
6551
6552 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
6553 entry->hdr->sh_offset,
6554 1, entry->hdr->sh_size,
6555 _("symbol table section indices"));
6556 if (shndx == NULL)
6557 goto exit_point;
6558
6559 /* PR17531: file: heap-buffer-overflow */
6560 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
6561 {
6562 error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
6563 printable_section_name (filedata, entry->hdr),
6564 (unsigned long) entry->hdr->sh_size,
6565 (unsigned long) section->sh_size);
6566 goto exit_point;
6567 }
6568 }
6569
6570 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
6571
6572 if (isyms == NULL)
6573 {
6574 error (_("Out of memory reading %lu symbols\n"),
6575 (unsigned long) number);
6576 goto exit_point;
6577 }
6578
6579 for (j = 0, psym = isyms; j < number; j++, psym++)
6580 {
6581 psym->st_name = BYTE_GET (esyms[j].st_name);
6582 psym->st_value = BYTE_GET (esyms[j].st_value);
6583 psym->st_size = BYTE_GET (esyms[j].st_size);
6584 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
6585 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
6586 psym->st_shndx
6587 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
6588 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
6589 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
6590 psym->st_info = BYTE_GET (esyms[j].st_info);
6591 psym->st_other = BYTE_GET (esyms[j].st_other);
6592 }
6593
6594 exit_point:
6595 free (shndx);
6596 free (esyms);
6597
6598 if (num_syms_return != NULL)
6599 * num_syms_return = isyms == NULL ? 0 : number;
6600
6601 return isyms;
6602 }
6603
6604 static Elf_Internal_Sym *
6605 get_64bit_elf_symbols (Filedata * filedata,
6606 Elf_Internal_Shdr * section,
6607 unsigned long * num_syms_return)
6608 {
6609 unsigned long number = 0;
6610 Elf64_External_Sym * esyms = NULL;
6611 Elf_External_Sym_Shndx * shndx = NULL;
6612 Elf_Internal_Sym * isyms = NULL;
6613 Elf_Internal_Sym * psym;
6614 unsigned int j;
6615 elf_section_list * entry;
6616
6617 if (section->sh_size == 0)
6618 {
6619 if (num_syms_return != NULL)
6620 * num_syms_return = 0;
6621 return NULL;
6622 }
6623
6624 /* Run some sanity checks first. */
6625 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
6626 {
6627 error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
6628 printable_section_name (filedata, section),
6629 (unsigned long) section->sh_entsize);
6630 goto exit_point;
6631 }
6632
6633 if (section->sh_size > filedata->file_size)
6634 {
6635 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
6636 printable_section_name (filedata, section),
6637 (unsigned long) section->sh_size);
6638 goto exit_point;
6639 }
6640
6641 number = section->sh_size / section->sh_entsize;
6642
6643 if (number * sizeof (Elf64_External_Sym) > section->sh_size + 1)
6644 {
6645 error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
6646 (unsigned long) section->sh_size,
6647 printable_section_name (filedata, section),
6648 (unsigned long) section->sh_entsize);
6649 goto exit_point;
6650 }
6651
6652 esyms = (Elf64_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
6653 section->sh_size, _("symbols"));
6654 if (!esyms)
6655 goto exit_point;
6656
6657 shndx = NULL;
6658 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
6659 {
6660 if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
6661 continue;
6662
6663 if (shndx != NULL)
6664 {
6665 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
6666 free (shndx);
6667 }
6668
6669 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
6670 entry->hdr->sh_offset,
6671 1, entry->hdr->sh_size,
6672 _("symbol table section indices"));
6673 if (shndx == NULL)
6674 goto exit_point;
6675
6676 /* PR17531: file: heap-buffer-overflow */
6677 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
6678 {
6679 error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
6680 printable_section_name (filedata, entry->hdr),
6681 (unsigned long) entry->hdr->sh_size,
6682 (unsigned long) section->sh_size);
6683 goto exit_point;
6684 }
6685 }
6686
6687 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
6688
6689 if (isyms == NULL)
6690 {
6691 error (_("Out of memory reading %lu symbols\n"),
6692 (unsigned long) number);
6693 goto exit_point;
6694 }
6695
6696 for (j = 0, psym = isyms; j < number; j++, psym++)
6697 {
6698 psym->st_name = BYTE_GET (esyms[j].st_name);
6699 psym->st_info = BYTE_GET (esyms[j].st_info);
6700 psym->st_other = BYTE_GET (esyms[j].st_other);
6701 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
6702
6703 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
6704 psym->st_shndx
6705 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
6706 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
6707 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
6708
6709 psym->st_value = BYTE_GET (esyms[j].st_value);
6710 psym->st_size = BYTE_GET (esyms[j].st_size);
6711 }
6712
6713 exit_point:
6714 free (shndx);
6715 free (esyms);
6716
6717 if (num_syms_return != NULL)
6718 * num_syms_return = isyms == NULL ? 0 : number;
6719
6720 return isyms;
6721 }
6722
6723 static Elf_Internal_Sym *
6724 get_elf_symbols (Filedata *filedata,
6725 Elf_Internal_Shdr *section,
6726 unsigned long *num_syms_return)
6727 {
6728 if (is_32bit_elf)
6729 return get_32bit_elf_symbols (filedata, section, num_syms_return);
6730 else
6731 return get_64bit_elf_symbols (filedata, section, num_syms_return);
6732 }
6733
6734 static const char *
6735 get_elf_section_flags (Filedata * filedata, bfd_vma sh_flags)
6736 {
6737 static char buff[1024];
6738 char * p = buff;
6739 unsigned int field_size = is_32bit_elf ? 8 : 16;
6740 signed int sindex;
6741 unsigned int size = sizeof (buff) - (field_size + 4 + 1);
6742 bfd_vma os_flags = 0;
6743 bfd_vma proc_flags = 0;
6744 bfd_vma unknown_flags = 0;
6745 static const struct
6746 {
6747 const char * str;
6748 unsigned int len;
6749 }
6750 flags [] =
6751 {
6752 /* 0 */ { STRING_COMMA_LEN ("WRITE") },
6753 /* 1 */ { STRING_COMMA_LEN ("ALLOC") },
6754 /* 2 */ { STRING_COMMA_LEN ("EXEC") },
6755 /* 3 */ { STRING_COMMA_LEN ("MERGE") },
6756 /* 4 */ { STRING_COMMA_LEN ("STRINGS") },
6757 /* 5 */ { STRING_COMMA_LEN ("INFO LINK") },
6758 /* 6 */ { STRING_COMMA_LEN ("LINK ORDER") },
6759 /* 7 */ { STRING_COMMA_LEN ("OS NONCONF") },
6760 /* 8 */ { STRING_COMMA_LEN ("GROUP") },
6761 /* 9 */ { STRING_COMMA_LEN ("TLS") },
6762 /* IA-64 specific. */
6763 /* 10 */ { STRING_COMMA_LEN ("SHORT") },
6764 /* 11 */ { STRING_COMMA_LEN ("NORECOV") },
6765 /* IA-64 OpenVMS specific. */
6766 /* 12 */ { STRING_COMMA_LEN ("VMS_GLOBAL") },
6767 /* 13 */ { STRING_COMMA_LEN ("VMS_OVERLAID") },
6768 /* 14 */ { STRING_COMMA_LEN ("VMS_SHARED") },
6769 /* 15 */ { STRING_COMMA_LEN ("VMS_VECTOR") },
6770 /* 16 */ { STRING_COMMA_LEN ("VMS_ALLOC_64BIT") },
6771 /* 17 */ { STRING_COMMA_LEN ("VMS_PROTECTED") },
6772 /* Generic. */
6773 /* 18 */ { STRING_COMMA_LEN ("EXCLUDE") },
6774 /* SPARC specific. */
6775 /* 19 */ { STRING_COMMA_LEN ("ORDERED") },
6776 /* 20 */ { STRING_COMMA_LEN ("COMPRESSED") },
6777 /* ARM specific. */
6778 /* 21 */ { STRING_COMMA_LEN ("ENTRYSECT") },
6779 /* 22 */ { STRING_COMMA_LEN ("ARM_PURECODE") },
6780 /* 23 */ { STRING_COMMA_LEN ("COMDEF") },
6781 /* GNU specific. */
6782 /* 24 */ { STRING_COMMA_LEN ("GNU_MBIND") },
6783 /* VLE specific. */
6784 /* 25 */ { STRING_COMMA_LEN ("VLE") },
6785 /* GNU specific. */
6786 /* 26 */ { STRING_COMMA_LEN ("GNU_RETAIN") },
6787 };
6788
6789 if (do_section_details)
6790 {
6791 sprintf (buff, "[%*.*lx]: ",
6792 field_size, field_size, (unsigned long) sh_flags);
6793 p += field_size + 4;
6794 }
6795
6796 while (sh_flags)
6797 {
6798 bfd_vma flag;
6799
6800 flag = sh_flags & - sh_flags;
6801 sh_flags &= ~ flag;
6802
6803 if (do_section_details)
6804 {
6805 switch (flag)
6806 {
6807 case SHF_WRITE: sindex = 0; break;
6808 case SHF_ALLOC: sindex = 1; break;
6809 case SHF_EXECINSTR: sindex = 2; break;
6810 case SHF_MERGE: sindex = 3; break;
6811 case SHF_STRINGS: sindex = 4; break;
6812 case SHF_INFO_LINK: sindex = 5; break;
6813 case SHF_LINK_ORDER: sindex = 6; break;
6814 case SHF_OS_NONCONFORMING: sindex = 7; break;
6815 case SHF_GROUP: sindex = 8; break;
6816 case SHF_TLS: sindex = 9; break;
6817 case SHF_EXCLUDE: sindex = 18; break;
6818 case SHF_COMPRESSED: sindex = 20; break;
6819
6820 default:
6821 sindex = -1;
6822 switch (filedata->file_header.e_machine)
6823 {
6824 case EM_IA_64:
6825 if (flag == SHF_IA_64_SHORT)
6826 sindex = 10;
6827 else if (flag == SHF_IA_64_NORECOV)
6828 sindex = 11;
6829 #ifdef BFD64
6830 else if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
6831 switch (flag)
6832 {
6833 case SHF_IA_64_VMS_GLOBAL: sindex = 12; break;
6834 case SHF_IA_64_VMS_OVERLAID: sindex = 13; break;
6835 case SHF_IA_64_VMS_SHARED: sindex = 14; break;
6836 case SHF_IA_64_VMS_VECTOR: sindex = 15; break;
6837 case SHF_IA_64_VMS_ALLOC_64BIT: sindex = 16; break;
6838 case SHF_IA_64_VMS_PROTECTED: sindex = 17; break;
6839 default: break;
6840 }
6841 #endif
6842 break;
6843
6844 case EM_386:
6845 case EM_IAMCU:
6846 case EM_X86_64:
6847 case EM_L1OM:
6848 case EM_K1OM:
6849 case EM_OLD_SPARCV9:
6850 case EM_SPARC32PLUS:
6851 case EM_SPARCV9:
6852 case EM_SPARC:
6853 if (flag == SHF_ORDERED)
6854 sindex = 19;
6855 break;
6856
6857 case EM_ARM:
6858 switch (flag)
6859 {
6860 case SHF_ENTRYSECT: sindex = 21; break;
6861 case SHF_ARM_PURECODE: sindex = 22; break;
6862 case SHF_COMDEF: sindex = 23; break;
6863 default: break;
6864 }
6865 break;
6866 case EM_PPC:
6867 if (flag == SHF_PPC_VLE)
6868 sindex = 25;
6869 break;
6870 default:
6871 break;
6872 }
6873
6874 switch (filedata->file_header.e_ident[EI_OSABI])
6875 {
6876 case ELFOSABI_GNU:
6877 case ELFOSABI_FREEBSD:
6878 if (flag == SHF_GNU_RETAIN)
6879 sindex = 26;
6880 /* Fall through */
6881 case ELFOSABI_NONE:
6882 if (flag == SHF_GNU_MBIND)
6883 /* We should not recognize SHF_GNU_MBIND for
6884 ELFOSABI_NONE, but binutils as of 2019-07-23 did
6885 not set the EI_OSABI header byte. */
6886 sindex = 24;
6887 break;
6888 default:
6889 break;
6890 }
6891 break;
6892 }
6893
6894 if (sindex != -1)
6895 {
6896 if (p != buff + field_size + 4)
6897 {
6898 if (size < (10 + 2))
6899 {
6900 warn (_("Internal error: not enough buffer room for section flag info"));
6901 return _("<unknown>");
6902 }
6903 size -= 2;
6904 *p++ = ',';
6905 *p++ = ' ';
6906 }
6907
6908 size -= flags [sindex].len;
6909 p = stpcpy (p, flags [sindex].str);
6910 }
6911 else if (flag & SHF_MASKOS)
6912 os_flags |= flag;
6913 else if (flag & SHF_MASKPROC)
6914 proc_flags |= flag;
6915 else
6916 unknown_flags |= flag;
6917 }
6918 else
6919 {
6920 switch (flag)
6921 {
6922 case SHF_WRITE: *p = 'W'; break;
6923 case SHF_ALLOC: *p = 'A'; break;
6924 case SHF_EXECINSTR: *p = 'X'; break;
6925 case SHF_MERGE: *p = 'M'; break;
6926 case SHF_STRINGS: *p = 'S'; break;
6927 case SHF_INFO_LINK: *p = 'I'; break;
6928 case SHF_LINK_ORDER: *p = 'L'; break;
6929 case SHF_OS_NONCONFORMING: *p = 'O'; break;
6930 case SHF_GROUP: *p = 'G'; break;
6931 case SHF_TLS: *p = 'T'; break;
6932 case SHF_EXCLUDE: *p = 'E'; break;
6933 case SHF_COMPRESSED: *p = 'C'; break;
6934
6935 default:
6936 if ((filedata->file_header.e_machine == EM_X86_64
6937 || filedata->file_header.e_machine == EM_L1OM
6938 || filedata->file_header.e_machine == EM_K1OM)
6939 && flag == SHF_X86_64_LARGE)
6940 *p = 'l';
6941 else if (filedata->file_header.e_machine == EM_ARM
6942 && flag == SHF_ARM_PURECODE)
6943 *p = 'y';
6944 else if (filedata->file_header.e_machine == EM_PPC
6945 && flag == SHF_PPC_VLE)
6946 *p = 'v';
6947 else if (flag & SHF_MASKOS)
6948 {
6949 switch (filedata->file_header.e_ident[EI_OSABI])
6950 {
6951 case ELFOSABI_GNU:
6952 case ELFOSABI_FREEBSD:
6953 if (flag == SHF_GNU_RETAIN)
6954 {
6955 *p = 'R';
6956 break;
6957 }
6958 /* Fall through */
6959 case ELFOSABI_NONE:
6960 if (flag == SHF_GNU_MBIND)
6961 {
6962 /* We should not recognize SHF_GNU_MBIND for
6963 ELFOSABI_NONE, but binutils as of 2019-07-23 did
6964 not set the EI_OSABI header byte. */
6965 *p = 'D';
6966 break;
6967 }
6968 /* Fall through */
6969 default:
6970 *p = 'o';
6971 sh_flags &= ~SHF_MASKOS;
6972 break;
6973 }
6974 }
6975 else if (flag & SHF_MASKPROC)
6976 {
6977 *p = 'p';
6978 sh_flags &= ~ SHF_MASKPROC;
6979 }
6980 else
6981 *p = 'x';
6982 break;
6983 }
6984 p++;
6985 }
6986 }
6987
6988 if (do_section_details)
6989 {
6990 if (os_flags)
6991 {
6992 size -= 5 + field_size;
6993 if (p != buff + field_size + 4)
6994 {
6995 if (size < (2 + 1))
6996 {
6997 warn (_("Internal error: not enough buffer room for section flag info"));
6998 return _("<unknown>");
6999 }
7000 size -= 2;
7001 *p++ = ',';
7002 *p++ = ' ';
7003 }
7004 sprintf (p, "OS (%*.*lx)", field_size, field_size,
7005 (unsigned long) os_flags);
7006 p += 5 + field_size;
7007 }
7008 if (proc_flags)
7009 {
7010 size -= 7 + field_size;
7011 if (p != buff + field_size + 4)
7012 {
7013 if (size < (2 + 1))
7014 {
7015 warn (_("Internal error: not enough buffer room for section flag info"));
7016 return _("<unknown>");
7017 }
7018 size -= 2;
7019 *p++ = ',';
7020 *p++ = ' ';
7021 }
7022 sprintf (p, "PROC (%*.*lx)", field_size, field_size,
7023 (unsigned long) proc_flags);
7024 p += 7 + field_size;
7025 }
7026 if (unknown_flags)
7027 {
7028 size -= 10 + field_size;
7029 if (p != buff + field_size + 4)
7030 {
7031 if (size < (2 + 1))
7032 {
7033 warn (_("Internal error: not enough buffer room for section flag info"));
7034 return _("<unknown>");
7035 }
7036 size -= 2;
7037 *p++ = ',';
7038 *p++ = ' ';
7039 }
7040 sprintf (p, _("UNKNOWN (%*.*lx)"), field_size, field_size,
7041 (unsigned long) unknown_flags);
7042 p += 10 + field_size;
7043 }
7044 }
7045
7046 *p = '\0';
7047 return buff;
7048 }
7049
7050 static unsigned int ATTRIBUTE_WARN_UNUSED_RESULT
7051 get_compression_header (Elf_Internal_Chdr *chdr, unsigned char *buf, bfd_size_type size)
7052 {
7053 if (is_32bit_elf)
7054 {
7055 Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) buf;
7056
7057 if (size < sizeof (* echdr))
7058 {
7059 error (_("Compressed section is too small even for a compression header\n"));
7060 return 0;
7061 }
7062
7063 chdr->ch_type = BYTE_GET (echdr->ch_type);
7064 chdr->ch_size = BYTE_GET (echdr->ch_size);
7065 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
7066 return sizeof (*echdr);
7067 }
7068 else
7069 {
7070 Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) buf;
7071
7072 if (size < sizeof (* echdr))
7073 {
7074 error (_("Compressed section is too small even for a compression header\n"));
7075 return 0;
7076 }
7077
7078 chdr->ch_type = BYTE_GET (echdr->ch_type);
7079 chdr->ch_size = BYTE_GET (echdr->ch_size);
7080 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
7081 return sizeof (*echdr);
7082 }
7083 }
7084
7085 static bool
7086 process_section_headers (Filedata * filedata)
7087 {
7088 Elf_Internal_Shdr * section;
7089 unsigned int i;
7090
7091 if (filedata->file_header.e_shnum == 0)
7092 {
7093 /* PR binutils/12467. */
7094 if (filedata->file_header.e_shoff != 0)
7095 {
7096 warn (_("possibly corrupt ELF file header - it has a non-zero"
7097 " section header offset, but no section headers\n"));
7098 return false;
7099 }
7100 else if (do_sections)
7101 printf (_("\nThere are no sections in this file.\n"));
7102
7103 return true;
7104 }
7105
7106 if (do_sections && !do_header)
7107 {
7108 if (filedata->is_separate && process_links)
7109 printf (_("In linked file '%s': "), filedata->file_name);
7110 if (! filedata->is_separate || process_links)
7111 printf (ngettext ("There is %d section header, "
7112 "starting at offset 0x%lx:\n",
7113 "There are %d section headers, "
7114 "starting at offset 0x%lx:\n",
7115 filedata->file_header.e_shnum),
7116 filedata->file_header.e_shnum,
7117 (unsigned long) filedata->file_header.e_shoff);
7118 }
7119
7120 if (!get_section_headers (filedata, false))
7121 return false;
7122
7123 /* Read in the string table, so that we have names to display. */
7124 if (filedata->file_header.e_shstrndx != SHN_UNDEF
7125 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
7126 {
7127 section = filedata->section_headers + filedata->file_header.e_shstrndx;
7128
7129 if (section->sh_size != 0)
7130 {
7131 filedata->string_table = (char *) get_data (NULL, filedata, section->sh_offset,
7132 1, section->sh_size,
7133 _("string table"));
7134
7135 filedata->string_table_length = filedata->string_table != NULL ? section->sh_size : 0;
7136 }
7137 }
7138
7139 /* Scan the sections for the dynamic symbol table
7140 and dynamic string table and debug sections. */
7141 eh_addr_size = is_32bit_elf ? 4 : 8;
7142 switch (filedata->file_header.e_machine)
7143 {
7144 case EM_MIPS:
7145 case EM_MIPS_RS3_LE:
7146 /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
7147 FDE addresses. However, the ABI also has a semi-official ILP32
7148 variant for which the normal FDE address size rules apply.
7149
7150 GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
7151 section, where XX is the size of longs in bits. Unfortunately,
7152 earlier compilers provided no way of distinguishing ILP32 objects
7153 from LP64 objects, so if there's any doubt, we should assume that
7154 the official LP64 form is being used. */
7155 if ((filedata->file_header.e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64
7156 && find_section (filedata, ".gcc_compiled_long32") == NULL)
7157 eh_addr_size = 8;
7158 break;
7159
7160 case EM_H8_300:
7161 case EM_H8_300H:
7162 switch (filedata->file_header.e_flags & EF_H8_MACH)
7163 {
7164 case E_H8_MACH_H8300:
7165 case E_H8_MACH_H8300HN:
7166 case E_H8_MACH_H8300SN:
7167 case E_H8_MACH_H8300SXN:
7168 eh_addr_size = 2;
7169 break;
7170 case E_H8_MACH_H8300H:
7171 case E_H8_MACH_H8300S:
7172 case E_H8_MACH_H8300SX:
7173 eh_addr_size = 4;
7174 break;
7175 }
7176 break;
7177
7178 case EM_M32C_OLD:
7179 case EM_M32C:
7180 switch (filedata->file_header.e_flags & EF_M32C_CPU_MASK)
7181 {
7182 case EF_M32C_CPU_M16C:
7183 eh_addr_size = 2;
7184 break;
7185 }
7186 break;
7187 }
7188
7189 #define CHECK_ENTSIZE_VALUES(section, i, size32, size64) \
7190 do \
7191 { \
7192 bfd_size_type expected_entsize = is_32bit_elf ? size32 : size64; \
7193 if (section->sh_entsize != expected_entsize) \
7194 { \
7195 error (_("Section %d has invalid sh_entsize of %" PRIx64 "\n"), \
7196 i, (uint64_t) section->sh_entsize); \
7197 error (_("(Using the expected size of %" PRIx64 " for the rest of this dump)\n"), \
7198 (uint64_t) expected_entsize); \
7199 section->sh_entsize = expected_entsize; \
7200 } \
7201 } \
7202 while (0)
7203
7204 #define CHECK_ENTSIZE(section, i, type) \
7205 CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type), \
7206 sizeof (Elf64_External_##type))
7207
7208 for (i = 0, section = filedata->section_headers;
7209 i < filedata->file_header.e_shnum;
7210 i++, section++)
7211 {
7212 const char *name = section_name_print (filedata, section);
7213
7214 /* Run some sanity checks on the headers and
7215 possibly fill in some file data as well. */
7216 switch (section->sh_type)
7217 {
7218 case SHT_DYNSYM:
7219 if (filedata->dynamic_symbols != NULL)
7220 {
7221 error (_("File contains multiple dynamic symbol tables\n"));
7222 continue;
7223 }
7224
7225 CHECK_ENTSIZE (section, i, Sym);
7226 filedata->dynamic_symbols
7227 = get_elf_symbols (filedata, section, &filedata->num_dynamic_syms);
7228 filedata->dynamic_symtab_section = section;
7229 break;
7230
7231 case SHT_STRTAB:
7232 if (streq (name, ".dynstr"))
7233 {
7234 if (filedata->dynamic_strings != NULL)
7235 {
7236 error (_("File contains multiple dynamic string tables\n"));
7237 continue;
7238 }
7239
7240 filedata->dynamic_strings
7241 = (char *) get_data (NULL, filedata, section->sh_offset,
7242 1, section->sh_size, _("dynamic strings"));
7243 filedata->dynamic_strings_length
7244 = filedata->dynamic_strings == NULL ? 0 : section->sh_size;
7245 filedata->dynamic_strtab_section = section;
7246 }
7247 break;
7248
7249 case SHT_SYMTAB_SHNDX:
7250 {
7251 elf_section_list * entry = xmalloc (sizeof * entry);
7252
7253 entry->hdr = section;
7254 entry->next = filedata->symtab_shndx_list;
7255 filedata->symtab_shndx_list = entry;
7256 }
7257 break;
7258
7259 case SHT_SYMTAB:
7260 CHECK_ENTSIZE (section, i, Sym);
7261 break;
7262
7263 case SHT_GROUP:
7264 CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
7265 break;
7266
7267 case SHT_REL:
7268 CHECK_ENTSIZE (section, i, Rel);
7269 if (do_checks && section->sh_size == 0)
7270 warn (_("Section '%s': zero-sized relocation section\n"), name);
7271 break;
7272
7273 case SHT_RELA:
7274 CHECK_ENTSIZE (section, i, Rela);
7275 if (do_checks && section->sh_size == 0)
7276 warn (_("Section '%s': zero-sized relocation section\n"), name);
7277 break;
7278
7279 case SHT_RELR:
7280 CHECK_ENTSIZE (section, i, Relr);
7281 break;
7282
7283 case SHT_NOTE:
7284 case SHT_PROGBITS:
7285 /* Having a zero sized section is not illegal according to the
7286 ELF standard, but it might be an indication that something
7287 is wrong. So issue a warning if we are running in lint mode. */
7288 if (do_checks && section->sh_size == 0)
7289 warn (_("Section '%s': has a size of zero - is this intended ?\n"), name);
7290 break;
7291
7292 default:
7293 break;
7294 }
7295
7296 if ((do_debugging || do_debug_info || do_debug_abbrevs
7297 || do_debug_lines || do_debug_pubnames || do_debug_pubtypes
7298 || do_debug_aranges || do_debug_frames || do_debug_macinfo
7299 || do_debug_str || do_debug_str_offsets || do_debug_loc
7300 || do_debug_ranges
7301 || do_debug_addr || do_debug_cu_index || do_debug_links)
7302 && (startswith (name, ".debug_")
7303 || startswith (name, ".zdebug_")))
7304 {
7305 if (name[1] == 'z')
7306 name += sizeof (".zdebug_") - 1;
7307 else
7308 name += sizeof (".debug_") - 1;
7309
7310 if (do_debugging
7311 || (do_debug_info && startswith (name, "info"))
7312 || (do_debug_info && startswith (name, "types"))
7313 || (do_debug_abbrevs && startswith (name, "abbrev"))
7314 || (do_debug_lines && strcmp (name, "line") == 0)
7315 || (do_debug_lines && startswith (name, "line."))
7316 || (do_debug_pubnames && startswith (name, "pubnames"))
7317 || (do_debug_pubtypes && startswith (name, "pubtypes"))
7318 || (do_debug_pubnames && startswith (name, "gnu_pubnames"))
7319 || (do_debug_pubtypes && startswith (name, "gnu_pubtypes"))
7320 || (do_debug_aranges && startswith (name, "aranges"))
7321 || (do_debug_ranges && startswith (name, "ranges"))
7322 || (do_debug_ranges && startswith (name, "rnglists"))
7323 || (do_debug_frames && startswith (name, "frame"))
7324 || (do_debug_macinfo && startswith (name, "macinfo"))
7325 || (do_debug_macinfo && startswith (name, "macro"))
7326 || (do_debug_str && startswith (name, "str"))
7327 || (do_debug_links && startswith (name, "sup"))
7328 || (do_debug_str_offsets && startswith (name, "str_offsets"))
7329 || (do_debug_loc && startswith (name, "loc"))
7330 || (do_debug_loc && startswith (name, "loclists"))
7331 || (do_debug_addr && startswith (name, "addr"))
7332 || (do_debug_cu_index && startswith (name, "cu_index"))
7333 || (do_debug_cu_index && startswith (name, "tu_index"))
7334 )
7335 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7336 }
7337 /* Linkonce section to be combined with .debug_info at link time. */
7338 else if ((do_debugging || do_debug_info)
7339 && startswith (name, ".gnu.linkonce.wi."))
7340 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7341 else if (do_debug_frames && streq (name, ".eh_frame"))
7342 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7343 else if (do_gdb_index && (streq (name, ".gdb_index")
7344 || streq (name, ".debug_names")))
7345 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7346 /* Trace sections for Itanium VMS. */
7347 else if ((do_debugging || do_trace_info || do_trace_abbrevs
7348 || do_trace_aranges)
7349 && startswith (name, ".trace_"))
7350 {
7351 name += sizeof (".trace_") - 1;
7352
7353 if (do_debugging
7354 || (do_trace_info && streq (name, "info"))
7355 || (do_trace_abbrevs && streq (name, "abbrev"))
7356 || (do_trace_aranges && streq (name, "aranges"))
7357 )
7358 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7359 }
7360 else if ((do_debugging || do_debug_links)
7361 && (startswith (name, ".gnu_debuglink")
7362 || startswith (name, ".gnu_debugaltlink")))
7363 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7364 }
7365
7366 if (! do_sections)
7367 return true;
7368
7369 if (filedata->is_separate && ! process_links)
7370 return true;
7371
7372 if (filedata->is_separate)
7373 printf (_("\nSection Headers in linked file '%s':\n"), filedata->file_name);
7374 else if (filedata->file_header.e_shnum > 1)
7375 printf (_("\nSection Headers:\n"));
7376 else
7377 printf (_("\nSection Header:\n"));
7378
7379 if (is_32bit_elf)
7380 {
7381 if (do_section_details)
7382 {
7383 printf (_(" [Nr] Name\n"));
7384 printf (_(" Type Addr Off Size ES Lk Inf Al\n"));
7385 }
7386 else
7387 printf
7388 (_(" [Nr] Name Type Addr Off Size ES Flg Lk Inf Al\n"));
7389 }
7390 else if (do_wide)
7391 {
7392 if (do_section_details)
7393 {
7394 printf (_(" [Nr] Name\n"));
7395 printf (_(" Type Address Off Size ES Lk Inf Al\n"));
7396 }
7397 else
7398 printf
7399 (_(" [Nr] Name Type Address Off Size ES Flg Lk Inf Al\n"));
7400 }
7401 else
7402 {
7403 if (do_section_details)
7404 {
7405 printf (_(" [Nr] Name\n"));
7406 printf (_(" Type Address Offset Link\n"));
7407 printf (_(" Size EntSize Info Align\n"));
7408 }
7409 else
7410 {
7411 printf (_(" [Nr] Name Type Address Offset\n"));
7412 printf (_(" Size EntSize Flags Link Info Align\n"));
7413 }
7414 }
7415
7416 if (do_section_details)
7417 printf (_(" Flags\n"));
7418
7419 for (i = 0, section = filedata->section_headers;
7420 i < filedata->file_header.e_shnum;
7421 i++, section++)
7422 {
7423 /* Run some sanity checks on the section header. */
7424
7425 /* Check the sh_link field. */
7426 switch (section->sh_type)
7427 {
7428 case SHT_REL:
7429 case SHT_RELA:
7430 if (section->sh_link == 0
7431 && (filedata->file_header.e_type == ET_EXEC
7432 || filedata->file_header.e_type == ET_DYN))
7433 /* A dynamic relocation section where all entries use a
7434 zero symbol index need not specify a symtab section. */
7435 break;
7436 /* Fall through. */
7437 case SHT_SYMTAB_SHNDX:
7438 case SHT_GROUP:
7439 case SHT_HASH:
7440 case SHT_GNU_HASH:
7441 case SHT_GNU_versym:
7442 if (section->sh_link == 0
7443 || section->sh_link >= filedata->file_header.e_shnum
7444 || (filedata->section_headers[section->sh_link].sh_type != SHT_SYMTAB
7445 && filedata->section_headers[section->sh_link].sh_type != SHT_DYNSYM))
7446 warn (_("[%2u]: Link field (%u) should index a symtab section.\n"),
7447 i, section->sh_link);
7448 break;
7449
7450 case SHT_DYNAMIC:
7451 case SHT_SYMTAB:
7452 case SHT_DYNSYM:
7453 case SHT_GNU_verneed:
7454 case SHT_GNU_verdef:
7455 case SHT_GNU_LIBLIST:
7456 if (section->sh_link == 0
7457 || section->sh_link >= filedata->file_header.e_shnum
7458 || filedata->section_headers[section->sh_link].sh_type != SHT_STRTAB)
7459 warn (_("[%2u]: Link field (%u) should index a string section.\n"),
7460 i, section->sh_link);
7461 break;
7462
7463 case SHT_INIT_ARRAY:
7464 case SHT_FINI_ARRAY:
7465 case SHT_PREINIT_ARRAY:
7466 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
7467 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
7468 i, section->sh_link);
7469 break;
7470
7471 default:
7472 /* FIXME: Add support for target specific section types. */
7473 #if 0 /* Currently we do not check other section types as there are too
7474 many special cases. Stab sections for example have a type
7475 of SHT_PROGBITS but an sh_link field that links to the .stabstr
7476 section. */
7477 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
7478 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
7479 i, section->sh_link);
7480 #endif
7481 break;
7482 }
7483
7484 /* Check the sh_info field. */
7485 switch (section->sh_type)
7486 {
7487 case SHT_REL:
7488 case SHT_RELA:
7489 if (section->sh_info == 0
7490 && (filedata->file_header.e_type == ET_EXEC
7491 || filedata->file_header.e_type == ET_DYN))
7492 /* Dynamic relocations apply to segments, so they do not
7493 need to specify the section they relocate. */
7494 break;
7495 if (section->sh_info == 0
7496 || section->sh_info >= filedata->file_header.e_shnum
7497 || (filedata->section_headers[section->sh_info].sh_type != SHT_PROGBITS
7498 && filedata->section_headers[section->sh_info].sh_type != SHT_NOBITS
7499 && filedata->section_headers[section->sh_info].sh_type != SHT_NOTE
7500 && filedata->section_headers[section->sh_info].sh_type != SHT_INIT_ARRAY
7501 && filedata->section_headers[section->sh_info].sh_type != SHT_FINI_ARRAY
7502 && filedata->section_headers[section->sh_info].sh_type != SHT_PREINIT_ARRAY
7503 /* FIXME: Are other section types valid ? */
7504 && filedata->section_headers[section->sh_info].sh_type < SHT_LOOS))
7505 warn (_("[%2u]: Info field (%u) should index a relocatable section.\n"),
7506 i, section->sh_info);
7507 break;
7508
7509 case SHT_DYNAMIC:
7510 case SHT_HASH:
7511 case SHT_SYMTAB_SHNDX:
7512 case SHT_INIT_ARRAY:
7513 case SHT_FINI_ARRAY:
7514 case SHT_PREINIT_ARRAY:
7515 if (section->sh_info != 0)
7516 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
7517 i, section->sh_info);
7518 break;
7519
7520 case SHT_GROUP:
7521 case SHT_SYMTAB:
7522 case SHT_DYNSYM:
7523 /* A symbol index - we assume that it is valid. */
7524 break;
7525
7526 default:
7527 /* FIXME: Add support for target specific section types. */
7528 if (section->sh_type == SHT_NOBITS)
7529 /* NOBITS section headers with non-zero sh_info fields can be
7530 created when a binary is stripped of everything but its debug
7531 information. The stripped sections have their headers
7532 preserved but their types set to SHT_NOBITS. So do not check
7533 this type of section. */
7534 ;
7535 else if (section->sh_flags & SHF_INFO_LINK)
7536 {
7537 if (section->sh_info < 1 || section->sh_info >= filedata->file_header.e_shnum)
7538 warn (_("[%2u]: Expected link to another section in info field"), i);
7539 }
7540 else if (section->sh_type < SHT_LOOS
7541 && (section->sh_flags & SHF_GNU_MBIND) == 0
7542 && section->sh_info != 0)
7543 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
7544 i, section->sh_info);
7545 break;
7546 }
7547
7548 /* Check the sh_size field. */
7549 if (section->sh_size > filedata->file_size
7550 && section->sh_type != SHT_NOBITS
7551 && section->sh_type != SHT_NULL
7552 && section->sh_type < SHT_LOOS)
7553 warn (_("Size of section %u is larger than the entire file!\n"), i);
7554
7555 printf (" [%2u] ", i);
7556 if (do_section_details)
7557 printf ("%s\n ", printable_section_name (filedata, section));
7558 else
7559 print_symbol (-17, section_name_print (filedata, section));
7560
7561 printf (do_wide ? " %-15s " : " %-15.15s ",
7562 get_section_type_name (filedata, section->sh_type));
7563
7564 if (is_32bit_elf)
7565 {
7566 const char * link_too_big = NULL;
7567
7568 print_vma (section->sh_addr, LONG_HEX);
7569
7570 printf ( " %6.6lx %6.6lx %2.2lx",
7571 (unsigned long) section->sh_offset,
7572 (unsigned long) section->sh_size,
7573 (unsigned long) section->sh_entsize);
7574
7575 if (do_section_details)
7576 fputs (" ", stdout);
7577 else
7578 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7579
7580 if (section->sh_link >= filedata->file_header.e_shnum)
7581 {
7582 link_too_big = "";
7583 /* The sh_link value is out of range. Normally this indicates
7584 an error but it can have special values in Solaris binaries. */
7585 switch (filedata->file_header.e_machine)
7586 {
7587 case EM_386:
7588 case EM_IAMCU:
7589 case EM_X86_64:
7590 case EM_L1OM:
7591 case EM_K1OM:
7592 case EM_OLD_SPARCV9:
7593 case EM_SPARC32PLUS:
7594 case EM_SPARCV9:
7595 case EM_SPARC:
7596 if (section->sh_link == (SHN_BEFORE & 0xffff))
7597 link_too_big = "BEFORE";
7598 else if (section->sh_link == (SHN_AFTER & 0xffff))
7599 link_too_big = "AFTER";
7600 break;
7601 default:
7602 break;
7603 }
7604 }
7605
7606 if (do_section_details)
7607 {
7608 if (link_too_big != NULL && * link_too_big)
7609 printf ("<%s> ", link_too_big);
7610 else
7611 printf ("%2u ", section->sh_link);
7612 printf ("%3u %2lu\n", section->sh_info,
7613 (unsigned long) section->sh_addralign);
7614 }
7615 else
7616 printf ("%2u %3u %2lu\n",
7617 section->sh_link,
7618 section->sh_info,
7619 (unsigned long) section->sh_addralign);
7620
7621 if (link_too_big && ! * link_too_big)
7622 warn (_("section %u: sh_link value of %u is larger than the number of sections\n"),
7623 i, section->sh_link);
7624 }
7625 else if (do_wide)
7626 {
7627 print_vma (section->sh_addr, LONG_HEX);
7628
7629 if ((long) section->sh_offset == section->sh_offset)
7630 printf (" %6.6lx", (unsigned long) section->sh_offset);
7631 else
7632 {
7633 putchar (' ');
7634 print_vma (section->sh_offset, LONG_HEX);
7635 }
7636
7637 if ((unsigned long) section->sh_size == section->sh_size)
7638 printf (" %6.6lx", (unsigned long) section->sh_size);
7639 else
7640 {
7641 putchar (' ');
7642 print_vma (section->sh_size, LONG_HEX);
7643 }
7644
7645 if ((unsigned long) section->sh_entsize == section->sh_entsize)
7646 printf (" %2.2lx", (unsigned long) section->sh_entsize);
7647 else
7648 {
7649 putchar (' ');
7650 print_vma (section->sh_entsize, LONG_HEX);
7651 }
7652
7653 if (do_section_details)
7654 fputs (" ", stdout);
7655 else
7656 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7657
7658 printf ("%2u %3u ", section->sh_link, section->sh_info);
7659
7660 if ((unsigned long) section->sh_addralign == section->sh_addralign)
7661 printf ("%2lu\n", (unsigned long) section->sh_addralign);
7662 else
7663 {
7664 print_vma (section->sh_addralign, DEC);
7665 putchar ('\n');
7666 }
7667 }
7668 else if (do_section_details)
7669 {
7670 putchar (' ');
7671 print_vma (section->sh_addr, LONG_HEX);
7672 if ((long) section->sh_offset == section->sh_offset)
7673 printf (" %16.16lx", (unsigned long) section->sh_offset);
7674 else
7675 {
7676 printf (" ");
7677 print_vma (section->sh_offset, LONG_HEX);
7678 }
7679 printf (" %u\n ", section->sh_link);
7680 print_vma (section->sh_size, LONG_HEX);
7681 putchar (' ');
7682 print_vma (section->sh_entsize, LONG_HEX);
7683
7684 printf (" %-16u %lu\n",
7685 section->sh_info,
7686 (unsigned long) section->sh_addralign);
7687 }
7688 else
7689 {
7690 putchar (' ');
7691 print_vma (section->sh_addr, LONG_HEX);
7692 if ((long) section->sh_offset == section->sh_offset)
7693 printf (" %8.8lx", (unsigned long) section->sh_offset);
7694 else
7695 {
7696 printf (" ");
7697 print_vma (section->sh_offset, LONG_HEX);
7698 }
7699 printf ("\n ");
7700 print_vma (section->sh_size, LONG_HEX);
7701 printf (" ");
7702 print_vma (section->sh_entsize, LONG_HEX);
7703
7704 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7705
7706 printf (" %2u %3u %lu\n",
7707 section->sh_link,
7708 section->sh_info,
7709 (unsigned long) section->sh_addralign);
7710 }
7711
7712 if (do_section_details)
7713 {
7714 printf (" %s\n", get_elf_section_flags (filedata, section->sh_flags));
7715 if ((section->sh_flags & SHF_COMPRESSED) != 0)
7716 {
7717 /* Minimum section size is 12 bytes for 32-bit compression
7718 header + 12 bytes for compressed data header. */
7719 unsigned char buf[24];
7720
7721 assert (sizeof (buf) >= sizeof (Elf64_External_Chdr));
7722 if (get_data (&buf, filedata, section->sh_offset, 1,
7723 sizeof (buf), _("compression header")))
7724 {
7725 Elf_Internal_Chdr chdr;
7726
7727 if (get_compression_header (&chdr, buf, sizeof (buf)) == 0)
7728 printf (_(" [<corrupt>]\n"));
7729 else
7730 {
7731 if (chdr.ch_type == ELFCOMPRESS_ZLIB)
7732 printf (" ZLIB, ");
7733 else
7734 printf (_(" [<unknown>: 0x%x], "),
7735 chdr.ch_type);
7736 print_vma (chdr.ch_size, LONG_HEX);
7737 printf (", %lu\n", (unsigned long) chdr.ch_addralign);
7738 }
7739 }
7740 }
7741 }
7742 }
7743
7744 if (!do_section_details)
7745 {
7746 /* The ordering of the letters shown here matches the ordering of the
7747 corresponding SHF_xxx values, and hence the order in which these
7748 letters will be displayed to the user. */
7749 printf (_("Key to Flags:\n\
7750 W (write), A (alloc), X (execute), M (merge), S (strings), I (info),\n\
7751 L (link order), O (extra OS processing required), G (group), T (TLS),\n\
7752 C (compressed), x (unknown), o (OS specific), E (exclude),\n "));
7753 switch (filedata->file_header.e_ident[EI_OSABI])
7754 {
7755 case ELFOSABI_GNU:
7756 case ELFOSABI_FREEBSD:
7757 printf (_("R (retain), "));
7758 /* Fall through */
7759 case ELFOSABI_NONE:
7760 printf (_("D (mbind), "));
7761 break;
7762 default:
7763 break;
7764 }
7765 if (filedata->file_header.e_machine == EM_X86_64
7766 || filedata->file_header.e_machine == EM_L1OM
7767 || filedata->file_header.e_machine == EM_K1OM)
7768 printf (_("l (large), "));
7769 else if (filedata->file_header.e_machine == EM_ARM)
7770 printf (_("y (purecode), "));
7771 else if (filedata->file_header.e_machine == EM_PPC)
7772 printf (_("v (VLE), "));
7773 printf ("p (processor specific)\n");
7774 }
7775
7776 return true;
7777 }
7778
7779 static bool
7780 get_symtab (Filedata *filedata, Elf_Internal_Shdr *symsec,
7781 Elf_Internal_Sym **symtab, unsigned long *nsyms,
7782 char **strtab, unsigned long *strtablen)
7783 {
7784 *strtab = NULL;
7785 *strtablen = 0;
7786 *symtab = get_elf_symbols (filedata, symsec, nsyms);
7787
7788 if (*symtab == NULL)
7789 return false;
7790
7791 if (symsec->sh_link != 0)
7792 {
7793 Elf_Internal_Shdr *strsec;
7794
7795 if (symsec->sh_link >= filedata->file_header.e_shnum)
7796 {
7797 error (_("Bad sh_link in symbol table section\n"));
7798 free (*symtab);
7799 *symtab = NULL;
7800 *nsyms = 0;
7801 return false;
7802 }
7803
7804 strsec = filedata->section_headers + symsec->sh_link;
7805
7806 *strtab = (char *) get_data (NULL, filedata, strsec->sh_offset,
7807 1, strsec->sh_size, _("string table"));
7808 if (*strtab == NULL)
7809 {
7810 free (*symtab);
7811 *symtab = NULL;
7812 *nsyms = 0;
7813 return false;
7814 }
7815 *strtablen = strsec->sh_size;
7816 }
7817 return true;
7818 }
7819
7820 static const char *
7821 get_group_flags (unsigned int flags)
7822 {
7823 static char buff[128];
7824
7825 if (flags == 0)
7826 return "";
7827 else if (flags == GRP_COMDAT)
7828 return "COMDAT ";
7829
7830 snprintf (buff, sizeof buff, "[0x%x: %s%s%s]",
7831 flags,
7832 flags & GRP_MASKOS ? _("<OS specific>") : "",
7833 flags & GRP_MASKPROC ? _("<PROC specific>") : "",
7834 (flags & ~(GRP_COMDAT | GRP_MASKOS | GRP_MASKPROC)
7835 ? _("<unknown>") : ""));
7836
7837 return buff;
7838 }
7839
7840 static bool
7841 process_section_groups (Filedata * filedata)
7842 {
7843 Elf_Internal_Shdr * section;
7844 unsigned int i;
7845 struct group * group;
7846 Elf_Internal_Shdr * symtab_sec;
7847 Elf_Internal_Shdr * strtab_sec;
7848 Elf_Internal_Sym * symtab;
7849 unsigned long num_syms;
7850 char * strtab;
7851 size_t strtab_size;
7852
7853 /* Don't process section groups unless needed. */
7854 if (!do_unwind && !do_section_groups)
7855 return true;
7856
7857 if (filedata->file_header.e_shnum == 0)
7858 {
7859 if (do_section_groups)
7860 {
7861 if (filedata->is_separate)
7862 printf (_("\nThere are no sections group in linked file '%s'.\n"),
7863 filedata->file_name);
7864 else
7865 printf (_("\nThere are no section groups in this file.\n"));
7866 }
7867 return true;
7868 }
7869
7870 if (filedata->section_headers == NULL)
7871 {
7872 error (_("Section headers are not available!\n"));
7873 /* PR 13622: This can happen with a corrupt ELF header. */
7874 return false;
7875 }
7876
7877 filedata->section_headers_groups
7878 = (struct group **) calloc (filedata->file_header.e_shnum,
7879 sizeof (struct group *));
7880
7881 if (filedata->section_headers_groups == NULL)
7882 {
7883 error (_("Out of memory reading %u section group headers\n"),
7884 filedata->file_header.e_shnum);
7885 return false;
7886 }
7887
7888 /* Scan the sections for the group section. */
7889 filedata->group_count = 0;
7890 for (i = 0, section = filedata->section_headers;
7891 i < filedata->file_header.e_shnum;
7892 i++, section++)
7893 if (section->sh_type == SHT_GROUP)
7894 filedata->group_count++;
7895
7896 if (filedata->group_count == 0)
7897 {
7898 if (do_section_groups)
7899 {
7900 if (filedata->is_separate)
7901 printf (_("\nThere are no section groups in linked file '%s'.\n"),
7902 filedata->file_name);
7903 else
7904 printf (_("\nThere are no section groups in this file.\n"));
7905 }
7906
7907 return true;
7908 }
7909
7910 filedata->section_groups = (struct group *) calloc (filedata->group_count,
7911 sizeof (struct group));
7912
7913 if (filedata->section_groups == NULL)
7914 {
7915 error (_("Out of memory reading %lu groups\n"),
7916 (unsigned long) filedata->group_count);
7917 return false;
7918 }
7919
7920 symtab_sec = NULL;
7921 strtab_sec = NULL;
7922 symtab = NULL;
7923 num_syms = 0;
7924 strtab = NULL;
7925 strtab_size = 0;
7926
7927 if (filedata->is_separate)
7928 printf (_("Section groups in linked file '%s'\n"), filedata->file_name);
7929
7930 for (i = 0, section = filedata->section_headers, group = filedata->section_groups;
7931 i < filedata->file_header.e_shnum;
7932 i++, section++)
7933 {
7934 if (section->sh_type == SHT_GROUP)
7935 {
7936 const char * name = printable_section_name (filedata, section);
7937 const char * group_name;
7938 unsigned char * start;
7939 unsigned char * indices;
7940 unsigned int entry, j, size;
7941 Elf_Internal_Shdr * sec;
7942 Elf_Internal_Sym * sym;
7943
7944 /* Get the symbol table. */
7945 if (section->sh_link >= filedata->file_header.e_shnum
7946 || ((sec = filedata->section_headers + section->sh_link)->sh_type
7947 != SHT_SYMTAB))
7948 {
7949 error (_("Bad sh_link in group section `%s'\n"), name);
7950 continue;
7951 }
7952
7953 if (symtab_sec != sec)
7954 {
7955 symtab_sec = sec;
7956 free (symtab);
7957 symtab = get_elf_symbols (filedata, symtab_sec, & num_syms);
7958 }
7959
7960 if (symtab == NULL)
7961 {
7962 error (_("Corrupt header in group section `%s'\n"), name);
7963 continue;
7964 }
7965
7966 if (section->sh_info >= num_syms)
7967 {
7968 error (_("Bad sh_info in group section `%s'\n"), name);
7969 continue;
7970 }
7971
7972 sym = symtab + section->sh_info;
7973
7974 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
7975 {
7976 if (sym->st_shndx == 0
7977 || sym->st_shndx >= filedata->file_header.e_shnum)
7978 {
7979 error (_("Bad sh_info in group section `%s'\n"), name);
7980 continue;
7981 }
7982
7983 group_name = section_name_print (filedata,
7984 filedata->section_headers
7985 + sym->st_shndx);
7986 strtab_sec = NULL;
7987 free (strtab);
7988 strtab = NULL;
7989 strtab_size = 0;
7990 }
7991 else
7992 {
7993 /* Get the string table. */
7994 if (symtab_sec->sh_link >= filedata->file_header.e_shnum)
7995 {
7996 strtab_sec = NULL;
7997 free (strtab);
7998 strtab = NULL;
7999 strtab_size = 0;
8000 }
8001 else if (strtab_sec
8002 != (sec = filedata->section_headers + symtab_sec->sh_link))
8003 {
8004 strtab_sec = sec;
8005 free (strtab);
8006
8007 strtab = (char *) get_data (NULL, filedata, strtab_sec->sh_offset,
8008 1, strtab_sec->sh_size,
8009 _("string table"));
8010 strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
8011 }
8012 group_name = sym->st_name < strtab_size
8013 ? strtab + sym->st_name : _("<corrupt>");
8014 }
8015
8016 /* PR 17531: file: loop. */
8017 if (section->sh_entsize > section->sh_size)
8018 {
8019 error (_("Section %s has sh_entsize (0x%lx) which is larger than its size (0x%lx)\n"),
8020 printable_section_name (filedata, section),
8021 (unsigned long) section->sh_entsize,
8022 (unsigned long) section->sh_size);
8023 continue;
8024 }
8025
8026 start = (unsigned char *) get_data (NULL, filedata, section->sh_offset,
8027 1, section->sh_size,
8028 _("section data"));
8029 if (start == NULL)
8030 continue;
8031
8032 indices = start;
8033 size = (section->sh_size / section->sh_entsize) - 1;
8034 entry = byte_get (indices, 4);
8035 indices += 4;
8036
8037 if (do_section_groups)
8038 {
8039 printf (_("\n%sgroup section [%5u] `%s' [%s] contains %u sections:\n"),
8040 get_group_flags (entry), i, name, group_name, size);
8041
8042 printf (_(" [Index] Name\n"));
8043 }
8044
8045 group->group_index = i;
8046
8047 for (j = 0; j < size; j++)
8048 {
8049 struct group_list * g;
8050
8051 entry = byte_get (indices, 4);
8052 indices += 4;
8053
8054 if (entry >= filedata->file_header.e_shnum)
8055 {
8056 static unsigned num_group_errors = 0;
8057
8058 if (num_group_errors ++ < 10)
8059 {
8060 error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
8061 entry, i, filedata->file_header.e_shnum - 1);
8062 if (num_group_errors == 10)
8063 warn (_("Further error messages about overlarge group section indices suppressed\n"));
8064 }
8065 continue;
8066 }
8067
8068 if (filedata->section_headers_groups [entry] != NULL)
8069 {
8070 if (entry)
8071 {
8072 static unsigned num_errs = 0;
8073
8074 if (num_errs ++ < 10)
8075 {
8076 error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
8077 entry, i,
8078 filedata->section_headers_groups [entry]->group_index);
8079 if (num_errs == 10)
8080 warn (_("Further error messages about already contained group sections suppressed\n"));
8081 }
8082 continue;
8083 }
8084 else
8085 {
8086 /* Intel C/C++ compiler may put section 0 in a
8087 section group. We just warn it the first time
8088 and ignore it afterwards. */
8089 static bool warned = false;
8090 if (!warned)
8091 {
8092 error (_("section 0 in group section [%5u]\n"),
8093 filedata->section_headers_groups [entry]->group_index);
8094 warned = true;
8095 }
8096 }
8097 }
8098
8099 filedata->section_headers_groups [entry] = group;
8100
8101 if (do_section_groups)
8102 {
8103 sec = filedata->section_headers + entry;
8104 printf (" [%5u] %s\n", entry, printable_section_name (filedata, sec));
8105 }
8106
8107 g = (struct group_list *) xmalloc (sizeof (struct group_list));
8108 g->section_index = entry;
8109 g->next = group->root;
8110 group->root = g;
8111 }
8112
8113 free (start);
8114
8115 group++;
8116 }
8117 }
8118
8119 free (symtab);
8120 free (strtab);
8121 return true;
8122 }
8123
8124 /* Data used to display dynamic fixups. */
8125
8126 struct ia64_vms_dynfixup
8127 {
8128 bfd_vma needed_ident; /* Library ident number. */
8129 bfd_vma needed; /* Index in the dstrtab of the library name. */
8130 bfd_vma fixup_needed; /* Index of the library. */
8131 bfd_vma fixup_rela_cnt; /* Number of fixups. */
8132 bfd_vma fixup_rela_off; /* Fixups offset in the dynamic segment. */
8133 };
8134
8135 /* Data used to display dynamic relocations. */
8136
8137 struct ia64_vms_dynimgrela
8138 {
8139 bfd_vma img_rela_cnt; /* Number of relocations. */
8140 bfd_vma img_rela_off; /* Reloc offset in the dynamic segment. */
8141 };
8142
8143 /* Display IA-64 OpenVMS dynamic fixups (used to dynamically link a shared
8144 library). */
8145
8146 static bool
8147 dump_ia64_vms_dynamic_fixups (Filedata * filedata,
8148 struct ia64_vms_dynfixup * fixup,
8149 const char * strtab,
8150 unsigned int strtab_sz)
8151 {
8152 Elf64_External_VMS_IMAGE_FIXUP * imfs;
8153 long i;
8154 const char * lib_name;
8155
8156 imfs = get_data (NULL, filedata,
8157 filedata->dynamic_addr + fixup->fixup_rela_off,
8158 sizeof (*imfs), fixup->fixup_rela_cnt,
8159 _("dynamic section image fixups"));
8160 if (!imfs)
8161 return false;
8162
8163 if (fixup->needed < strtab_sz)
8164 lib_name = strtab + fixup->needed;
8165 else
8166 {
8167 warn (_("corrupt library name index of 0x%lx found in dynamic entry"),
8168 (unsigned long) fixup->needed);
8169 lib_name = "???";
8170 }
8171
8172 printf (_("\nImage fixups for needed library #%d: %s - ident: %lx\n"),
8173 (int) fixup->fixup_needed, lib_name, (long) fixup->needed_ident);
8174 printf
8175 (_("Seg Offset Type SymVec DataType\n"));
8176
8177 for (i = 0; i < (long) fixup->fixup_rela_cnt; i++)
8178 {
8179 unsigned int type;
8180 const char *rtype;
8181
8182 printf ("%3u ", (unsigned) BYTE_GET (imfs [i].fixup_seg));
8183 printf ("%016" PRIx64 " ", (uint64_t) BYTE_GET (imfs [i].fixup_offset));
8184 type = BYTE_GET (imfs [i].type);
8185 rtype = elf_ia64_reloc_type (type);
8186 if (rtype == NULL)
8187 printf ("0x%08x ", type);
8188 else
8189 printf ("%-32s ", rtype);
8190 printf ("%6u ", (unsigned) BYTE_GET (imfs [i].symvec_index));
8191 printf ("0x%08x\n", (unsigned) BYTE_GET (imfs [i].data_type));
8192 }
8193
8194 free (imfs);
8195 return true;
8196 }
8197
8198 /* Display IA-64 OpenVMS dynamic relocations (used to relocate an image). */
8199
8200 static bool
8201 dump_ia64_vms_dynamic_relocs (Filedata * filedata, struct ia64_vms_dynimgrela *imgrela)
8202 {
8203 Elf64_External_VMS_IMAGE_RELA *imrs;
8204 long i;
8205
8206 imrs = get_data (NULL, filedata,
8207 filedata->dynamic_addr + imgrela->img_rela_off,
8208 sizeof (*imrs), imgrela->img_rela_cnt,
8209 _("dynamic section image relocations"));
8210 if (!imrs)
8211 return false;
8212
8213 printf (_("\nImage relocs\n"));
8214 printf
8215 (_("Seg Offset Type Addend Seg Sym Off\n"));
8216
8217 for (i = 0; i < (long) imgrela->img_rela_cnt; i++)
8218 {
8219 unsigned int type;
8220 const char *rtype;
8221
8222 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].rela_seg));
8223 printf ("%08" BFD_VMA_FMT "x ",
8224 (bfd_vma) BYTE_GET (imrs [i].rela_offset));
8225 type = BYTE_GET (imrs [i].type);
8226 rtype = elf_ia64_reloc_type (type);
8227 if (rtype == NULL)
8228 printf ("0x%08x ", type);
8229 else
8230 printf ("%-31s ", rtype);
8231 print_vma (BYTE_GET (imrs [i].addend), FULL_HEX);
8232 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].sym_seg));
8233 printf ("%08" BFD_VMA_FMT "x\n",
8234 (bfd_vma) BYTE_GET (imrs [i].sym_offset));
8235 }
8236
8237 free (imrs);
8238 return true;
8239 }
8240
8241 /* Display IA-64 OpenVMS dynamic relocations and fixups. */
8242
8243 static bool
8244 process_ia64_vms_dynamic_relocs (Filedata * filedata)
8245 {
8246 struct ia64_vms_dynfixup fixup;
8247 struct ia64_vms_dynimgrela imgrela;
8248 Elf_Internal_Dyn *entry;
8249 bfd_vma strtab_off = 0;
8250 bfd_vma strtab_sz = 0;
8251 char *strtab = NULL;
8252 bool res = true;
8253
8254 memset (&fixup, 0, sizeof (fixup));
8255 memset (&imgrela, 0, sizeof (imgrela));
8256
8257 /* Note: the order of the entries is specified by the OpenVMS specs. */
8258 for (entry = filedata->dynamic_section;
8259 entry < filedata->dynamic_section + filedata->dynamic_nent;
8260 entry++)
8261 {
8262 switch (entry->d_tag)
8263 {
8264 case DT_IA_64_VMS_STRTAB_OFFSET:
8265 strtab_off = entry->d_un.d_val;
8266 break;
8267 case DT_STRSZ:
8268 strtab_sz = entry->d_un.d_val;
8269 if (strtab == NULL)
8270 strtab = get_data (NULL, filedata,
8271 filedata->dynamic_addr + strtab_off,
8272 1, strtab_sz, _("dynamic string section"));
8273 if (strtab == NULL)
8274 strtab_sz = 0;
8275 break;
8276
8277 case DT_IA_64_VMS_NEEDED_IDENT:
8278 fixup.needed_ident = entry->d_un.d_val;
8279 break;
8280 case DT_NEEDED:
8281 fixup.needed = entry->d_un.d_val;
8282 break;
8283 case DT_IA_64_VMS_FIXUP_NEEDED:
8284 fixup.fixup_needed = entry->d_un.d_val;
8285 break;
8286 case DT_IA_64_VMS_FIXUP_RELA_CNT:
8287 fixup.fixup_rela_cnt = entry->d_un.d_val;
8288 break;
8289 case DT_IA_64_VMS_FIXUP_RELA_OFF:
8290 fixup.fixup_rela_off = entry->d_un.d_val;
8291 if (! dump_ia64_vms_dynamic_fixups (filedata, &fixup, strtab, strtab_sz))
8292 res = false;
8293 break;
8294 case DT_IA_64_VMS_IMG_RELA_CNT:
8295 imgrela.img_rela_cnt = entry->d_un.d_val;
8296 break;
8297 case DT_IA_64_VMS_IMG_RELA_OFF:
8298 imgrela.img_rela_off = entry->d_un.d_val;
8299 if (! dump_ia64_vms_dynamic_relocs (filedata, &imgrela))
8300 res = false;
8301 break;
8302
8303 default:
8304 break;
8305 }
8306 }
8307
8308 free (strtab);
8309
8310 return res;
8311 }
8312
8313 static struct
8314 {
8315 const char * name;
8316 int reloc;
8317 int size;
8318 relocation_type rel_type;
8319 }
8320 dynamic_relocations [] =
8321 {
8322 { "REL", DT_REL, DT_RELSZ, reltype_rel },
8323 { "RELA", DT_RELA, DT_RELASZ, reltype_rela },
8324 { "RELR", DT_RELR, DT_RELRSZ, reltype_relr },
8325 { "PLT", DT_JMPREL, DT_PLTRELSZ, reltype_unknown }
8326 };
8327
8328 /* Process the reloc section. */
8329
8330 static bool
8331 process_relocs (Filedata * filedata)
8332 {
8333 unsigned long rel_size;
8334 unsigned long rel_offset;
8335
8336 if (!do_reloc)
8337 return true;
8338
8339 if (do_using_dynamic)
8340 {
8341 relocation_type rel_type;
8342 const char * name;
8343 bool has_dynamic_reloc;
8344 unsigned int i;
8345
8346 has_dynamic_reloc = false;
8347
8348 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
8349 {
8350 rel_type = dynamic_relocations [i].rel_type;
8351 name = dynamic_relocations [i].name;
8352 rel_size = filedata->dynamic_info[dynamic_relocations [i].size];
8353 rel_offset = filedata->dynamic_info[dynamic_relocations [i].reloc];
8354
8355 if (rel_size)
8356 has_dynamic_reloc = true;
8357
8358 if (rel_type == reltype_unknown)
8359 {
8360 if (dynamic_relocations [i].reloc == DT_JMPREL)
8361 switch (filedata->dynamic_info[DT_PLTREL])
8362 {
8363 case DT_REL:
8364 rel_type = reltype_rel;
8365 break;
8366 case DT_RELA:
8367 rel_type = reltype_rela;
8368 break;
8369 }
8370 }
8371
8372 if (rel_size)
8373 {
8374 if (filedata->is_separate)
8375 printf
8376 (_("\nIn linked file '%s' section '%s' at offset 0x%lx contains %ld bytes:\n"),
8377 filedata->file_name, name, rel_offset, rel_size);
8378 else
8379 printf
8380 (_("\n'%s' relocation section at offset 0x%lx contains %ld bytes:\n"),
8381 name, rel_offset, rel_size);
8382
8383 dump_relocations (filedata,
8384 offset_from_vma (filedata, rel_offset, rel_size),
8385 rel_size,
8386 filedata->dynamic_symbols,
8387 filedata->num_dynamic_syms,
8388 filedata->dynamic_strings,
8389 filedata->dynamic_strings_length,
8390 rel_type, true /* is_dynamic */);
8391 }
8392 }
8393
8394 if (is_ia64_vms (filedata))
8395 if (process_ia64_vms_dynamic_relocs (filedata))
8396 has_dynamic_reloc = true;
8397
8398 if (! has_dynamic_reloc)
8399 {
8400 if (filedata->is_separate)
8401 printf (_("\nThere are no dynamic relocations in linked file '%s'.\n"),
8402 filedata->file_name);
8403 else
8404 printf (_("\nThere are no dynamic relocations in this file.\n"));
8405 }
8406 }
8407 else
8408 {
8409 Elf_Internal_Shdr * section;
8410 unsigned long i;
8411 bool found = false;
8412
8413 for (i = 0, section = filedata->section_headers;
8414 i < filedata->file_header.e_shnum;
8415 i++, section++)
8416 {
8417 if ( section->sh_type != SHT_RELA
8418 && section->sh_type != SHT_REL
8419 && section->sh_type != SHT_RELR)
8420 continue;
8421
8422 rel_offset = section->sh_offset;
8423 rel_size = section->sh_size;
8424
8425 if (rel_size)
8426 {
8427 relocation_type rel_type;
8428 unsigned long num_rela;
8429
8430 if (filedata->is_separate)
8431 printf (_("\nIn linked file '%s' relocation section "),
8432 filedata->file_name);
8433 else
8434 printf (_("\nRelocation section "));
8435
8436 if (filedata->string_table == NULL)
8437 printf ("%d", section->sh_name);
8438 else
8439 printf ("'%s'", printable_section_name (filedata, section));
8440
8441 num_rela = rel_size / section->sh_entsize;
8442 printf (ngettext (" at offset 0x%lx contains %lu entry:\n",
8443 " at offset 0x%lx contains %lu entries:\n",
8444 num_rela),
8445 rel_offset, num_rela);
8446
8447 rel_type = section->sh_type == SHT_RELA ? reltype_rela :
8448 section->sh_type == SHT_REL ? reltype_rel : reltype_relr;
8449
8450 if (section->sh_link != 0
8451 && section->sh_link < filedata->file_header.e_shnum)
8452 {
8453 Elf_Internal_Shdr * symsec;
8454 Elf_Internal_Sym * symtab;
8455 unsigned long nsyms;
8456 unsigned long strtablen = 0;
8457 char * strtab = NULL;
8458
8459 symsec = filedata->section_headers + section->sh_link;
8460 if (symsec->sh_type != SHT_SYMTAB
8461 && symsec->sh_type != SHT_DYNSYM)
8462 continue;
8463
8464 if (!get_symtab (filedata, symsec,
8465 &symtab, &nsyms, &strtab, &strtablen))
8466 continue;
8467
8468 dump_relocations (filedata, rel_offset, rel_size,
8469 symtab, nsyms, strtab, strtablen,
8470 rel_type,
8471 symsec->sh_type == SHT_DYNSYM);
8472 free (strtab);
8473 free (symtab);
8474 }
8475 else
8476 dump_relocations (filedata, rel_offset, rel_size,
8477 NULL, 0, NULL, 0, rel_type, false /* is_dynamic */);
8478
8479 found = true;
8480 }
8481 }
8482
8483 if (! found)
8484 {
8485 /* Users sometimes forget the -D option, so try to be helpful. */
8486 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
8487 {
8488 if (filedata->dynamic_info[dynamic_relocations [i].size])
8489 {
8490 if (filedata->is_separate)
8491 printf (_("\nThere are no static relocations in linked file '%s'."),
8492 filedata->file_name);
8493 else
8494 printf (_("\nThere are no static relocations in this file."));
8495 printf (_("\nTo see the dynamic relocations add --use-dynamic to the command line.\n"));
8496
8497 break;
8498 }
8499 }
8500 if (i == ARRAY_SIZE (dynamic_relocations))
8501 {
8502 if (filedata->is_separate)
8503 printf (_("\nThere are no relocations in linked file '%s'.\n"),
8504 filedata->file_name);
8505 else
8506 printf (_("\nThere are no relocations in this file.\n"));
8507 }
8508 }
8509 }
8510
8511 return true;
8512 }
8513
8514 /* An absolute address consists of a section and an offset. If the
8515 section is NULL, the offset itself is the address, otherwise, the
8516 address equals to LOAD_ADDRESS(section) + offset. */
8517
8518 struct absaddr
8519 {
8520 unsigned short section;
8521 bfd_vma offset;
8522 };
8523
8524 /* Find the nearest symbol at or below ADDR. Returns the symbol
8525 name, if found, and the offset from the symbol to ADDR. */
8526
8527 static void
8528 find_symbol_for_address (Filedata * filedata,
8529 Elf_Internal_Sym * symtab,
8530 unsigned long nsyms,
8531 const char * strtab,
8532 unsigned long strtab_size,
8533 struct absaddr addr,
8534 const char ** symname,
8535 bfd_vma * offset)
8536 {
8537 bfd_vma dist = 0x100000;
8538 Elf_Internal_Sym * sym;
8539 Elf_Internal_Sym * beg;
8540 Elf_Internal_Sym * end;
8541 Elf_Internal_Sym * best = NULL;
8542
8543 REMOVE_ARCH_BITS (addr.offset);
8544 beg = symtab;
8545 end = symtab + nsyms;
8546
8547 while (beg < end)
8548 {
8549 bfd_vma value;
8550
8551 sym = beg + (end - beg) / 2;
8552
8553 value = sym->st_value;
8554 REMOVE_ARCH_BITS (value);
8555
8556 if (sym->st_name != 0
8557 && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
8558 && addr.offset >= value
8559 && addr.offset - value < dist)
8560 {
8561 best = sym;
8562 dist = addr.offset - value;
8563 if (!dist)
8564 break;
8565 }
8566
8567 if (addr.offset < value)
8568 end = sym;
8569 else
8570 beg = sym + 1;
8571 }
8572
8573 if (best)
8574 {
8575 *symname = (best->st_name >= strtab_size
8576 ? _("<corrupt>") : strtab + best->st_name);
8577 *offset = dist;
8578 return;
8579 }
8580
8581 *symname = NULL;
8582 *offset = addr.offset;
8583 }
8584
8585 static /* signed */ int
8586 symcmp (const void *p, const void *q)
8587 {
8588 Elf_Internal_Sym *sp = (Elf_Internal_Sym *) p;
8589 Elf_Internal_Sym *sq = (Elf_Internal_Sym *) q;
8590
8591 return sp->st_value > sq->st_value ? 1 : (sp->st_value < sq->st_value ? -1 : 0);
8592 }
8593
8594 /* Process the unwind section. */
8595
8596 #include "unwind-ia64.h"
8597
8598 struct ia64_unw_table_entry
8599 {
8600 struct absaddr start;
8601 struct absaddr end;
8602 struct absaddr info;
8603 };
8604
8605 struct ia64_unw_aux_info
8606 {
8607 struct ia64_unw_table_entry * table; /* Unwind table. */
8608 unsigned long table_len; /* Length of unwind table. */
8609 unsigned char * info; /* Unwind info. */
8610 unsigned long info_size; /* Size of unwind info. */
8611 bfd_vma info_addr; /* Starting address of unwind info. */
8612 bfd_vma seg_base; /* Starting address of segment. */
8613 Elf_Internal_Sym * symtab; /* The symbol table. */
8614 unsigned long nsyms; /* Number of symbols. */
8615 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
8616 unsigned long nfuns; /* Number of entries in funtab. */
8617 char * strtab; /* The string table. */
8618 unsigned long strtab_size; /* Size of string table. */
8619 };
8620
8621 static bool
8622 dump_ia64_unwind (Filedata * filedata, struct ia64_unw_aux_info * aux)
8623 {
8624 struct ia64_unw_table_entry * tp;
8625 unsigned long j, nfuns;
8626 int in_body;
8627 bool res = true;
8628
8629 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
8630 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
8631 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
8632 aux->funtab[nfuns++] = aux->symtab[j];
8633 aux->nfuns = nfuns;
8634 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
8635
8636 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
8637 {
8638 bfd_vma stamp;
8639 bfd_vma offset;
8640 const unsigned char * dp;
8641 const unsigned char * head;
8642 const unsigned char * end;
8643 const char * procname;
8644
8645 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
8646 aux->strtab_size, tp->start, &procname, &offset);
8647
8648 fputs ("\n<", stdout);
8649
8650 if (procname)
8651 {
8652 fputs (procname, stdout);
8653
8654 if (offset)
8655 printf ("+%lx", (unsigned long) offset);
8656 }
8657
8658 fputs (">: [", stdout);
8659 print_vma (tp->start.offset, PREFIX_HEX);
8660 fputc ('-', stdout);
8661 print_vma (tp->end.offset, PREFIX_HEX);
8662 printf ("], info at +0x%lx\n",
8663 (unsigned long) (tp->info.offset - aux->seg_base));
8664
8665 /* PR 17531: file: 86232b32. */
8666 if (aux->info == NULL)
8667 continue;
8668
8669 offset = tp->info.offset;
8670 if (tp->info.section)
8671 {
8672 if (tp->info.section >= filedata->file_header.e_shnum)
8673 {
8674 warn (_("Invalid section %u in table entry %ld\n"),
8675 tp->info.section, (long) (tp - aux->table));
8676 res = false;
8677 continue;
8678 }
8679 offset += filedata->section_headers[tp->info.section].sh_addr;
8680 }
8681 offset -= aux->info_addr;
8682 /* PR 17531: file: 0997b4d1. */
8683 if (offset >= aux->info_size
8684 || aux->info_size - offset < 8)
8685 {
8686 warn (_("Invalid offset %lx in table entry %ld\n"),
8687 (long) tp->info.offset, (long) (tp - aux->table));
8688 res = false;
8689 continue;
8690 }
8691
8692 head = aux->info + offset;
8693 stamp = byte_get ((unsigned char *) head, sizeof (stamp));
8694
8695 printf (" v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
8696 (unsigned) UNW_VER (stamp),
8697 (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
8698 UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
8699 UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
8700 (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
8701
8702 if (UNW_VER (stamp) != 1)
8703 {
8704 printf (_("\tUnknown version.\n"));
8705 continue;
8706 }
8707
8708 in_body = 0;
8709 end = head + 8 + eh_addr_size * UNW_LENGTH (stamp);
8710 /* PR 17531: file: 16ceda89. */
8711 if (end > aux->info + aux->info_size)
8712 end = aux->info + aux->info_size;
8713 for (dp = head + 8; dp < end;)
8714 dp = unw_decode (dp, in_body, & in_body, end);
8715 }
8716
8717 free (aux->funtab);
8718
8719 return res;
8720 }
8721
8722 static bool
8723 slurp_ia64_unwind_table (Filedata * filedata,
8724 struct ia64_unw_aux_info * aux,
8725 Elf_Internal_Shdr * sec)
8726 {
8727 unsigned long size, nrelas, i;
8728 Elf_Internal_Phdr * seg;
8729 struct ia64_unw_table_entry * tep;
8730 Elf_Internal_Shdr * relsec;
8731 Elf_Internal_Rela * rela;
8732 Elf_Internal_Rela * rp;
8733 unsigned char * table;
8734 unsigned char * tp;
8735 Elf_Internal_Sym * sym;
8736 const char * relname;
8737
8738 aux->table_len = 0;
8739
8740 /* First, find the starting address of the segment that includes
8741 this section: */
8742
8743 if (filedata->file_header.e_phnum)
8744 {
8745 if (! get_program_headers (filedata))
8746 return false;
8747
8748 for (seg = filedata->program_headers;
8749 seg < filedata->program_headers + filedata->file_header.e_phnum;
8750 ++seg)
8751 {
8752 if (seg->p_type != PT_LOAD)
8753 continue;
8754
8755 if (sec->sh_addr >= seg->p_vaddr
8756 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
8757 {
8758 aux->seg_base = seg->p_vaddr;
8759 break;
8760 }
8761 }
8762 }
8763
8764 /* Second, build the unwind table from the contents of the unwind section: */
8765 size = sec->sh_size;
8766 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
8767 _("unwind table"));
8768 if (!table)
8769 return false;
8770
8771 aux->table_len = size / (3 * eh_addr_size);
8772 aux->table = (struct ia64_unw_table_entry *)
8773 xcmalloc (aux->table_len, sizeof (aux->table[0]));
8774 tep = aux->table;
8775
8776 for (tp = table; tp <= table + size - (3 * eh_addr_size); ++tep)
8777 {
8778 tep->start.section = SHN_UNDEF;
8779 tep->end.section = SHN_UNDEF;
8780 tep->info.section = SHN_UNDEF;
8781 tep->start.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8782 tep->end.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8783 tep->info.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8784 tep->start.offset += aux->seg_base;
8785 tep->end.offset += aux->seg_base;
8786 tep->info.offset += aux->seg_base;
8787 }
8788 free (table);
8789
8790 /* Third, apply any relocations to the unwind table: */
8791 for (relsec = filedata->section_headers;
8792 relsec < filedata->section_headers + filedata->file_header.e_shnum;
8793 ++relsec)
8794 {
8795 if (relsec->sh_type != SHT_RELA
8796 || relsec->sh_info >= filedata->file_header.e_shnum
8797 || filedata->section_headers + relsec->sh_info != sec)
8798 continue;
8799
8800 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
8801 & rela, & nrelas))
8802 {
8803 free (aux->table);
8804 aux->table = NULL;
8805 aux->table_len = 0;
8806 return false;
8807 }
8808
8809 for (rp = rela; rp < rela + nrelas; ++rp)
8810 {
8811 unsigned int sym_ndx;
8812 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
8813 relname = elf_ia64_reloc_type (r_type);
8814
8815 /* PR 17531: file: 9fa67536. */
8816 if (relname == NULL)
8817 {
8818 warn (_("Skipping unknown relocation type: %u\n"), r_type);
8819 continue;
8820 }
8821
8822 if (! startswith (relname, "R_IA64_SEGREL"))
8823 {
8824 warn (_("Skipping unexpected relocation type: %s\n"), relname);
8825 continue;
8826 }
8827
8828 i = rp->r_offset / (3 * eh_addr_size);
8829
8830 /* PR 17531: file: 5bc8d9bf. */
8831 if (i >= aux->table_len)
8832 {
8833 warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
8834 continue;
8835 }
8836
8837 sym_ndx = get_reloc_symindex (rp->r_info);
8838 if (sym_ndx >= aux->nsyms)
8839 {
8840 warn (_("Skipping reloc with invalid symbol index: %u\n"),
8841 sym_ndx);
8842 continue;
8843 }
8844 sym = aux->symtab + sym_ndx;
8845
8846 switch (rp->r_offset / eh_addr_size % 3)
8847 {
8848 case 0:
8849 aux->table[i].start.section = sym->st_shndx;
8850 aux->table[i].start.offset = rp->r_addend + sym->st_value;
8851 break;
8852 case 1:
8853 aux->table[i].end.section = sym->st_shndx;
8854 aux->table[i].end.offset = rp->r_addend + sym->st_value;
8855 break;
8856 case 2:
8857 aux->table[i].info.section = sym->st_shndx;
8858 aux->table[i].info.offset = rp->r_addend + sym->st_value;
8859 break;
8860 default:
8861 break;
8862 }
8863 }
8864
8865 free (rela);
8866 }
8867
8868 return true;
8869 }
8870
8871 static bool
8872 ia64_process_unwind (Filedata * filedata)
8873 {
8874 Elf_Internal_Shdr * sec;
8875 Elf_Internal_Shdr * unwsec = NULL;
8876 unsigned long i, unwcount = 0, unwstart = 0;
8877 struct ia64_unw_aux_info aux;
8878 bool res = true;
8879
8880 memset (& aux, 0, sizeof (aux));
8881
8882 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
8883 {
8884 if (sec->sh_type == SHT_SYMTAB)
8885 {
8886 if (aux.symtab)
8887 {
8888 error (_("Multiple symbol tables encountered\n"));
8889 free (aux.symtab);
8890 aux.symtab = NULL;
8891 free (aux.strtab);
8892 aux.strtab = NULL;
8893 }
8894 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
8895 &aux.strtab, &aux.strtab_size))
8896 return false;
8897 }
8898 else if (sec->sh_type == SHT_IA_64_UNWIND)
8899 unwcount++;
8900 }
8901
8902 if (!unwcount)
8903 printf (_("\nThere are no unwind sections in this file.\n"));
8904
8905 while (unwcount-- > 0)
8906 {
8907 const char *suffix;
8908 size_t len, len2;
8909
8910 for (i = unwstart, sec = filedata->section_headers + unwstart, unwsec = NULL;
8911 i < filedata->file_header.e_shnum; ++i, ++sec)
8912 if (sec->sh_type == SHT_IA_64_UNWIND)
8913 {
8914 unwsec = sec;
8915 break;
8916 }
8917 /* We have already counted the number of SHT_IA64_UNWIND
8918 sections so the loop above should never fail. */
8919 assert (unwsec != NULL);
8920
8921 unwstart = i + 1;
8922 len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
8923
8924 if ((unwsec->sh_flags & SHF_GROUP) != 0)
8925 {
8926 /* We need to find which section group it is in. */
8927 struct group_list * g;
8928
8929 if (filedata->section_headers_groups == NULL
8930 || filedata->section_headers_groups[i] == NULL)
8931 i = filedata->file_header.e_shnum;
8932 else
8933 {
8934 g = filedata->section_headers_groups[i]->root;
8935
8936 for (; g != NULL; g = g->next)
8937 {
8938 sec = filedata->section_headers + g->section_index;
8939
8940 if (section_name_valid (filedata, sec)
8941 && streq (section_name (filedata, sec),
8942 ELF_STRING_ia64_unwind_info))
8943 break;
8944 }
8945
8946 if (g == NULL)
8947 i = filedata->file_header.e_shnum;
8948 }
8949 }
8950 else if (section_name_valid (filedata, unwsec)
8951 && startswith (section_name (filedata, unwsec),
8952 ELF_STRING_ia64_unwind_once))
8953 {
8954 /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO. */
8955 len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
8956 suffix = section_name (filedata, unwsec) + len;
8957 for (i = 0, sec = filedata->section_headers;
8958 i < filedata->file_header.e_shnum;
8959 ++i, ++sec)
8960 if (section_name_valid (filedata, sec)
8961 && startswith (section_name (filedata, sec),
8962 ELF_STRING_ia64_unwind_info_once)
8963 && streq (section_name (filedata, sec) + len2, suffix))
8964 break;
8965 }
8966 else
8967 {
8968 /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
8969 .IA_64.unwind or BAR -> .IA_64.unwind_info. */
8970 len = sizeof (ELF_STRING_ia64_unwind) - 1;
8971 len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
8972 suffix = "";
8973 if (section_name_valid (filedata, unwsec)
8974 && startswith (section_name (filedata, unwsec),
8975 ELF_STRING_ia64_unwind))
8976 suffix = section_name (filedata, unwsec) + len;
8977 for (i = 0, sec = filedata->section_headers;
8978 i < filedata->file_header.e_shnum;
8979 ++i, ++sec)
8980 if (section_name_valid (filedata, sec)
8981 && startswith (section_name (filedata, sec),
8982 ELF_STRING_ia64_unwind_info)
8983 && streq (section_name (filedata, sec) + len2, suffix))
8984 break;
8985 }
8986
8987 if (i == filedata->file_header.e_shnum)
8988 {
8989 printf (_("\nCould not find unwind info section for "));
8990
8991 if (filedata->string_table == NULL)
8992 printf ("%d", unwsec->sh_name);
8993 else
8994 printf ("'%s'", printable_section_name (filedata, unwsec));
8995 }
8996 else
8997 {
8998 aux.info_addr = sec->sh_addr;
8999 aux.info = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1,
9000 sec->sh_size,
9001 _("unwind info"));
9002 aux.info_size = aux.info == NULL ? 0 : sec->sh_size;
9003
9004 printf (_("\nUnwind section "));
9005
9006 if (filedata->string_table == NULL)
9007 printf ("%d", unwsec->sh_name);
9008 else
9009 printf ("'%s'", printable_section_name (filedata, unwsec));
9010
9011 printf (_(" at offset 0x%lx contains %lu entries:\n"),
9012 (unsigned long) unwsec->sh_offset,
9013 (unsigned long) (unwsec->sh_size / (3 * eh_addr_size)));
9014
9015 if (slurp_ia64_unwind_table (filedata, & aux, unwsec)
9016 && aux.table_len > 0)
9017 dump_ia64_unwind (filedata, & aux);
9018
9019 free ((char *) aux.table);
9020 free ((char *) aux.info);
9021 aux.table = NULL;
9022 aux.info = NULL;
9023 }
9024 }
9025
9026 free (aux.symtab);
9027 free ((char *) aux.strtab);
9028
9029 return res;
9030 }
9031
9032 struct hppa_unw_table_entry
9033 {
9034 struct absaddr start;
9035 struct absaddr end;
9036 unsigned int Cannot_unwind:1; /* 0 */
9037 unsigned int Millicode:1; /* 1 */
9038 unsigned int Millicode_save_sr0:1; /* 2 */
9039 unsigned int Region_description:2; /* 3..4 */
9040 unsigned int reserved1:1; /* 5 */
9041 unsigned int Entry_SR:1; /* 6 */
9042 unsigned int Entry_FR:4; /* Number saved 7..10 */
9043 unsigned int Entry_GR:5; /* Number saved 11..15 */
9044 unsigned int Args_stored:1; /* 16 */
9045 unsigned int Variable_Frame:1; /* 17 */
9046 unsigned int Separate_Package_Body:1; /* 18 */
9047 unsigned int Frame_Extension_Millicode:1; /* 19 */
9048 unsigned int Stack_Overflow_Check:1; /* 20 */
9049 unsigned int Two_Instruction_SP_Increment:1; /* 21 */
9050 unsigned int Ada_Region:1; /* 22 */
9051 unsigned int cxx_info:1; /* 23 */
9052 unsigned int cxx_try_catch:1; /* 24 */
9053 unsigned int sched_entry_seq:1; /* 25 */
9054 unsigned int reserved2:1; /* 26 */
9055 unsigned int Save_SP:1; /* 27 */
9056 unsigned int Save_RP:1; /* 28 */
9057 unsigned int Save_MRP_in_frame:1; /* 29 */
9058 unsigned int extn_ptr_defined:1; /* 30 */
9059 unsigned int Cleanup_defined:1; /* 31 */
9060
9061 unsigned int MPE_XL_interrupt_marker:1; /* 0 */
9062 unsigned int HP_UX_interrupt_marker:1; /* 1 */
9063 unsigned int Large_frame:1; /* 2 */
9064 unsigned int Pseudo_SP_Set:1; /* 3 */
9065 unsigned int reserved4:1; /* 4 */
9066 unsigned int Total_frame_size:27; /* 5..31 */
9067 };
9068
9069 struct hppa_unw_aux_info
9070 {
9071 struct hppa_unw_table_entry * table; /* Unwind table. */
9072 unsigned long table_len; /* Length of unwind table. */
9073 bfd_vma seg_base; /* Starting address of segment. */
9074 Elf_Internal_Sym * symtab; /* The symbol table. */
9075 unsigned long nsyms; /* Number of symbols. */
9076 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
9077 unsigned long nfuns; /* Number of entries in funtab. */
9078 char * strtab; /* The string table. */
9079 unsigned long strtab_size; /* Size of string table. */
9080 };
9081
9082 static bool
9083 dump_hppa_unwind (Filedata * filedata, struct hppa_unw_aux_info * aux)
9084 {
9085 struct hppa_unw_table_entry * tp;
9086 unsigned long j, nfuns;
9087 bool res = true;
9088
9089 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
9090 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
9091 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
9092 aux->funtab[nfuns++] = aux->symtab[j];
9093 aux->nfuns = nfuns;
9094 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
9095
9096 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
9097 {
9098 bfd_vma offset;
9099 const char * procname;
9100
9101 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
9102 aux->strtab_size, tp->start, &procname,
9103 &offset);
9104
9105 fputs ("\n<", stdout);
9106
9107 if (procname)
9108 {
9109 fputs (procname, stdout);
9110
9111 if (offset)
9112 printf ("+%lx", (unsigned long) offset);
9113 }
9114
9115 fputs (">: [", stdout);
9116 print_vma (tp->start.offset, PREFIX_HEX);
9117 fputc ('-', stdout);
9118 print_vma (tp->end.offset, PREFIX_HEX);
9119 printf ("]\n\t");
9120
9121 #define PF(_m) if (tp->_m) printf (#_m " ");
9122 #define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
9123 PF(Cannot_unwind);
9124 PF(Millicode);
9125 PF(Millicode_save_sr0);
9126 /* PV(Region_description); */
9127 PF(Entry_SR);
9128 PV(Entry_FR);
9129 PV(Entry_GR);
9130 PF(Args_stored);
9131 PF(Variable_Frame);
9132 PF(Separate_Package_Body);
9133 PF(Frame_Extension_Millicode);
9134 PF(Stack_Overflow_Check);
9135 PF(Two_Instruction_SP_Increment);
9136 PF(Ada_Region);
9137 PF(cxx_info);
9138 PF(cxx_try_catch);
9139 PF(sched_entry_seq);
9140 PF(Save_SP);
9141 PF(Save_RP);
9142 PF(Save_MRP_in_frame);
9143 PF(extn_ptr_defined);
9144 PF(Cleanup_defined);
9145 PF(MPE_XL_interrupt_marker);
9146 PF(HP_UX_interrupt_marker);
9147 PF(Large_frame);
9148 PF(Pseudo_SP_Set);
9149 PV(Total_frame_size);
9150 #undef PF
9151 #undef PV
9152 }
9153
9154 printf ("\n");
9155
9156 free (aux->funtab);
9157
9158 return res;
9159 }
9160
9161 static bool
9162 slurp_hppa_unwind_table (Filedata * filedata,
9163 struct hppa_unw_aux_info * aux,
9164 Elf_Internal_Shdr * sec)
9165 {
9166 unsigned long size, unw_ent_size, nentries, nrelas, i;
9167 Elf_Internal_Phdr * seg;
9168 struct hppa_unw_table_entry * tep;
9169 Elf_Internal_Shdr * relsec;
9170 Elf_Internal_Rela * rela;
9171 Elf_Internal_Rela * rp;
9172 unsigned char * table;
9173 unsigned char * tp;
9174 Elf_Internal_Sym * sym;
9175 const char * relname;
9176
9177 /* First, find the starting address of the segment that includes
9178 this section. */
9179 if (filedata->file_header.e_phnum)
9180 {
9181 if (! get_program_headers (filedata))
9182 return false;
9183
9184 for (seg = filedata->program_headers;
9185 seg < filedata->program_headers + filedata->file_header.e_phnum;
9186 ++seg)
9187 {
9188 if (seg->p_type != PT_LOAD)
9189 continue;
9190
9191 if (sec->sh_addr >= seg->p_vaddr
9192 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
9193 {
9194 aux->seg_base = seg->p_vaddr;
9195 break;
9196 }
9197 }
9198 }
9199
9200 /* Second, build the unwind table from the contents of the unwind
9201 section. */
9202 size = sec->sh_size;
9203 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
9204 _("unwind table"));
9205 if (!table)
9206 return false;
9207
9208 unw_ent_size = 16;
9209 nentries = size / unw_ent_size;
9210 size = unw_ent_size * nentries;
9211
9212 aux->table_len = nentries;
9213 tep = aux->table = (struct hppa_unw_table_entry *)
9214 xcmalloc (nentries, sizeof (aux->table[0]));
9215
9216 for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
9217 {
9218 unsigned int tmp1, tmp2;
9219
9220 tep->start.section = SHN_UNDEF;
9221 tep->end.section = SHN_UNDEF;
9222
9223 tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
9224 tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
9225 tmp1 = byte_get ((unsigned char *) tp + 8, 4);
9226 tmp2 = byte_get ((unsigned char *) tp + 12, 4);
9227
9228 tep->start.offset += aux->seg_base;
9229 tep->end.offset += aux->seg_base;
9230
9231 tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
9232 tep->Millicode = (tmp1 >> 30) & 0x1;
9233 tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
9234 tep->Region_description = (tmp1 >> 27) & 0x3;
9235 tep->reserved1 = (tmp1 >> 26) & 0x1;
9236 tep->Entry_SR = (tmp1 >> 25) & 0x1;
9237 tep->Entry_FR = (tmp1 >> 21) & 0xf;
9238 tep->Entry_GR = (tmp1 >> 16) & 0x1f;
9239 tep->Args_stored = (tmp1 >> 15) & 0x1;
9240 tep->Variable_Frame = (tmp1 >> 14) & 0x1;
9241 tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
9242 tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
9243 tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
9244 tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
9245 tep->Ada_Region = (tmp1 >> 9) & 0x1;
9246 tep->cxx_info = (tmp1 >> 8) & 0x1;
9247 tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
9248 tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
9249 tep->reserved2 = (tmp1 >> 5) & 0x1;
9250 tep->Save_SP = (tmp1 >> 4) & 0x1;
9251 tep->Save_RP = (tmp1 >> 3) & 0x1;
9252 tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
9253 tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
9254 tep->Cleanup_defined = tmp1 & 0x1;
9255
9256 tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
9257 tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
9258 tep->Large_frame = (tmp2 >> 29) & 0x1;
9259 tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
9260 tep->reserved4 = (tmp2 >> 27) & 0x1;
9261 tep->Total_frame_size = tmp2 & 0x7ffffff;
9262 }
9263 free (table);
9264
9265 /* Third, apply any relocations to the unwind table. */
9266 for (relsec = filedata->section_headers;
9267 relsec < filedata->section_headers + filedata->file_header.e_shnum;
9268 ++relsec)
9269 {
9270 if (relsec->sh_type != SHT_RELA
9271 || relsec->sh_info >= filedata->file_header.e_shnum
9272 || filedata->section_headers + relsec->sh_info != sec)
9273 continue;
9274
9275 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
9276 & rela, & nrelas))
9277 return false;
9278
9279 for (rp = rela; rp < rela + nrelas; ++rp)
9280 {
9281 unsigned int sym_ndx;
9282 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
9283 relname = elf_hppa_reloc_type (r_type);
9284
9285 if (relname == NULL)
9286 {
9287 warn (_("Skipping unknown relocation type: %u\n"), r_type);
9288 continue;
9289 }
9290
9291 /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64. */
9292 if (! startswith (relname, "R_PARISC_SEGREL"))
9293 {
9294 warn (_("Skipping unexpected relocation type: %s\n"), relname);
9295 continue;
9296 }
9297
9298 i = rp->r_offset / unw_ent_size;
9299 if (i >= aux->table_len)
9300 {
9301 warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
9302 continue;
9303 }
9304
9305 sym_ndx = get_reloc_symindex (rp->r_info);
9306 if (sym_ndx >= aux->nsyms)
9307 {
9308 warn (_("Skipping reloc with invalid symbol index: %u\n"),
9309 sym_ndx);
9310 continue;
9311 }
9312 sym = aux->symtab + sym_ndx;
9313
9314 switch ((rp->r_offset % unw_ent_size) / 4)
9315 {
9316 case 0:
9317 aux->table[i].start.section = sym->st_shndx;
9318 aux->table[i].start.offset = sym->st_value + rp->r_addend;
9319 break;
9320 case 1:
9321 aux->table[i].end.section = sym->st_shndx;
9322 aux->table[i].end.offset = sym->st_value + rp->r_addend;
9323 break;
9324 default:
9325 break;
9326 }
9327 }
9328
9329 free (rela);
9330 }
9331
9332 return true;
9333 }
9334
9335 static bool
9336 hppa_process_unwind (Filedata * filedata)
9337 {
9338 struct hppa_unw_aux_info aux;
9339 Elf_Internal_Shdr * unwsec = NULL;
9340 Elf_Internal_Shdr * sec;
9341 unsigned long i;
9342 bool res = true;
9343
9344 if (filedata->string_table == NULL)
9345 return false;
9346
9347 memset (& aux, 0, sizeof (aux));
9348
9349 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9350 {
9351 if (sec->sh_type == SHT_SYMTAB)
9352 {
9353 if (aux.symtab)
9354 {
9355 error (_("Multiple symbol tables encountered\n"));
9356 free (aux.symtab);
9357 aux.symtab = NULL;
9358 free (aux.strtab);
9359 aux.strtab = NULL;
9360 }
9361 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
9362 &aux.strtab, &aux.strtab_size))
9363 return false;
9364 }
9365 else if (section_name_valid (filedata, sec)
9366 && streq (section_name (filedata, sec), ".PARISC.unwind"))
9367 unwsec = sec;
9368 }
9369
9370 if (!unwsec)
9371 printf (_("\nThere are no unwind sections in this file.\n"));
9372
9373 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9374 {
9375 if (section_name_valid (filedata, sec)
9376 && streq (section_name (filedata, sec), ".PARISC.unwind"))
9377 {
9378 unsigned long num_unwind = sec->sh_size / 16;
9379
9380 printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
9381 "contains %lu entry:\n",
9382 "\nUnwind section '%s' at offset 0x%lx "
9383 "contains %lu entries:\n",
9384 num_unwind),
9385 printable_section_name (filedata, sec),
9386 (unsigned long) sec->sh_offset,
9387 num_unwind);
9388
9389 if (! slurp_hppa_unwind_table (filedata, &aux, sec))
9390 res = false;
9391
9392 if (res && aux.table_len > 0)
9393 {
9394 if (! dump_hppa_unwind (filedata, &aux))
9395 res = false;
9396 }
9397
9398 free ((char *) aux.table);
9399 aux.table = NULL;
9400 }
9401 }
9402
9403 free (aux.symtab);
9404 free ((char *) aux.strtab);
9405
9406 return res;
9407 }
9408
9409 struct arm_section
9410 {
9411 unsigned char * data; /* The unwind data. */
9412 Elf_Internal_Shdr * sec; /* The cached unwind section header. */
9413 Elf_Internal_Rela * rela; /* The cached relocations for this section. */
9414 unsigned long nrelas; /* The number of relocations. */
9415 unsigned int rel_type; /* REL or RELA ? */
9416 Elf_Internal_Rela * next_rela; /* Cyclic pointer to the next reloc to process. */
9417 };
9418
9419 struct arm_unw_aux_info
9420 {
9421 Filedata * filedata; /* The file containing the unwind sections. */
9422 Elf_Internal_Sym * symtab; /* The file's symbol table. */
9423 unsigned long nsyms; /* Number of symbols. */
9424 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
9425 unsigned long nfuns; /* Number of these symbols. */
9426 char * strtab; /* The file's string table. */
9427 unsigned long strtab_size; /* Size of string table. */
9428 };
9429
9430 static const char *
9431 arm_print_vma_and_name (Filedata * filedata,
9432 struct arm_unw_aux_info * aux,
9433 bfd_vma fn,
9434 struct absaddr addr)
9435 {
9436 const char *procname;
9437 bfd_vma sym_offset;
9438
9439 if (addr.section == SHN_UNDEF)
9440 addr.offset = fn;
9441
9442 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
9443 aux->strtab_size, addr, &procname,
9444 &sym_offset);
9445
9446 print_vma (fn, PREFIX_HEX);
9447
9448 if (procname)
9449 {
9450 fputs (" <", stdout);
9451 fputs (procname, stdout);
9452
9453 if (sym_offset)
9454 printf ("+0x%lx", (unsigned long) sym_offset);
9455 fputc ('>', stdout);
9456 }
9457
9458 return procname;
9459 }
9460
9461 static void
9462 arm_free_section (struct arm_section *arm_sec)
9463 {
9464 free (arm_sec->data);
9465 free (arm_sec->rela);
9466 }
9467
9468 /* 1) If SEC does not match the one cached in ARM_SEC, then free the current
9469 cached section and install SEC instead.
9470 2) Locate the 32-bit word at WORD_OFFSET in unwind section SEC
9471 and return its valued in * WORDP, relocating if necessary.
9472 3) Update the NEXT_RELA field in ARM_SEC and store the section index and
9473 relocation's offset in ADDR.
9474 4) If SYM_NAME is non-NULL and a relocation was applied, record the offset
9475 into the string table of the symbol associated with the reloc. If no
9476 reloc was applied store -1 there.
9477 5) Return TRUE upon success, FALSE otherwise. */
9478
9479 static bool
9480 get_unwind_section_word (Filedata * filedata,
9481 struct arm_unw_aux_info * aux,
9482 struct arm_section * arm_sec,
9483 Elf_Internal_Shdr * sec,
9484 bfd_vma word_offset,
9485 unsigned int * wordp,
9486 struct absaddr * addr,
9487 bfd_vma * sym_name)
9488 {
9489 Elf_Internal_Rela *rp;
9490 Elf_Internal_Sym *sym;
9491 const char * relname;
9492 unsigned int word;
9493 bool wrapped;
9494
9495 if (sec == NULL || arm_sec == NULL)
9496 return false;
9497
9498 addr->section = SHN_UNDEF;
9499 addr->offset = 0;
9500
9501 if (sym_name != NULL)
9502 *sym_name = (bfd_vma) -1;
9503
9504 /* If necessary, update the section cache. */
9505 if (sec != arm_sec->sec)
9506 {
9507 Elf_Internal_Shdr *relsec;
9508
9509 arm_free_section (arm_sec);
9510
9511 arm_sec->sec = sec;
9512 arm_sec->data = get_data (NULL, aux->filedata, sec->sh_offset, 1,
9513 sec->sh_size, _("unwind data"));
9514 arm_sec->rela = NULL;
9515 arm_sec->nrelas = 0;
9516
9517 for (relsec = filedata->section_headers;
9518 relsec < filedata->section_headers + filedata->file_header.e_shnum;
9519 ++relsec)
9520 {
9521 if (relsec->sh_info >= filedata->file_header.e_shnum
9522 || filedata->section_headers + relsec->sh_info != sec
9523 /* PR 15745: Check the section type as well. */
9524 || (relsec->sh_type != SHT_REL
9525 && relsec->sh_type != SHT_RELA))
9526 continue;
9527
9528 arm_sec->rel_type = relsec->sh_type;
9529 if (relsec->sh_type == SHT_REL)
9530 {
9531 if (!slurp_rel_relocs (aux->filedata, relsec->sh_offset,
9532 relsec->sh_size,
9533 & arm_sec->rela, & arm_sec->nrelas))
9534 return false;
9535 }
9536 else /* relsec->sh_type == SHT_RELA */
9537 {
9538 if (!slurp_rela_relocs (aux->filedata, relsec->sh_offset,
9539 relsec->sh_size,
9540 & arm_sec->rela, & arm_sec->nrelas))
9541 return false;
9542 }
9543 break;
9544 }
9545
9546 arm_sec->next_rela = arm_sec->rela;
9547 }
9548
9549 /* If there is no unwind data we can do nothing. */
9550 if (arm_sec->data == NULL)
9551 return false;
9552
9553 /* If the offset is invalid then fail. */
9554 if (/* PR 21343 *//* PR 18879 */
9555 sec->sh_size < 4
9556 || word_offset > (sec->sh_size - 4)
9557 || ((bfd_signed_vma) word_offset) < 0)
9558 return false;
9559
9560 /* Get the word at the required offset. */
9561 word = byte_get (arm_sec->data + word_offset, 4);
9562
9563 /* PR 17531: file: id:000001,src:001266+003044,op:splice,rep:128. */
9564 if (arm_sec->rela == NULL)
9565 {
9566 * wordp = word;
9567 return true;
9568 }
9569
9570 /* Look through the relocs to find the one that applies to the provided offset. */
9571 wrapped = false;
9572 for (rp = arm_sec->next_rela; rp != arm_sec->rela + arm_sec->nrelas; rp++)
9573 {
9574 bfd_vma prelval, offset;
9575
9576 if (rp->r_offset > word_offset && !wrapped)
9577 {
9578 rp = arm_sec->rela;
9579 wrapped = true;
9580 }
9581 if (rp->r_offset > word_offset)
9582 break;
9583
9584 if (rp->r_offset & 3)
9585 {
9586 warn (_("Skipping unexpected relocation at offset 0x%lx\n"),
9587 (unsigned long) rp->r_offset);
9588 continue;
9589 }
9590
9591 if (rp->r_offset < word_offset)
9592 continue;
9593
9594 /* PR 17531: file: 027-161405-0.004 */
9595 if (aux->symtab == NULL)
9596 continue;
9597
9598 if (arm_sec->rel_type == SHT_REL)
9599 {
9600 offset = word & 0x7fffffff;
9601 if (offset & 0x40000000)
9602 offset |= ~ (bfd_vma) 0x7fffffff;
9603 }
9604 else if (arm_sec->rel_type == SHT_RELA)
9605 offset = rp->r_addend;
9606 else
9607 {
9608 error (_("Unknown section relocation type %d encountered\n"),
9609 arm_sec->rel_type);
9610 break;
9611 }
9612
9613 /* PR 17531 file: 027-1241568-0.004. */
9614 if (ELF32_R_SYM (rp->r_info) >= aux->nsyms)
9615 {
9616 error (_("Bad symbol index in unwind relocation (%lu > %lu)\n"),
9617 (unsigned long) ELF32_R_SYM (rp->r_info), aux->nsyms);
9618 break;
9619 }
9620
9621 sym = aux->symtab + ELF32_R_SYM (rp->r_info);
9622 offset += sym->st_value;
9623 prelval = offset - (arm_sec->sec->sh_addr + rp->r_offset);
9624
9625 /* Check that we are processing the expected reloc type. */
9626 if (filedata->file_header.e_machine == EM_ARM)
9627 {
9628 relname = elf_arm_reloc_type (ELF32_R_TYPE (rp->r_info));
9629 if (relname == NULL)
9630 {
9631 warn (_("Skipping unknown ARM relocation type: %d\n"),
9632 (int) ELF32_R_TYPE (rp->r_info));
9633 continue;
9634 }
9635
9636 if (streq (relname, "R_ARM_NONE"))
9637 continue;
9638
9639 if (! streq (relname, "R_ARM_PREL31"))
9640 {
9641 warn (_("Skipping unexpected ARM relocation type %s\n"), relname);
9642 continue;
9643 }
9644 }
9645 else if (filedata->file_header.e_machine == EM_TI_C6000)
9646 {
9647 relname = elf_tic6x_reloc_type (ELF32_R_TYPE (rp->r_info));
9648 if (relname == NULL)
9649 {
9650 warn (_("Skipping unknown C6000 relocation type: %d\n"),
9651 (int) ELF32_R_TYPE (rp->r_info));
9652 continue;
9653 }
9654
9655 if (streq (relname, "R_C6000_NONE"))
9656 continue;
9657
9658 if (! streq (relname, "R_C6000_PREL31"))
9659 {
9660 warn (_("Skipping unexpected C6000 relocation type %s\n"), relname);
9661 continue;
9662 }
9663
9664 prelval >>= 1;
9665 }
9666 else
9667 {
9668 /* This function currently only supports ARM and TI unwinders. */
9669 warn (_("Only TI and ARM unwinders are currently supported\n"));
9670 break;
9671 }
9672
9673 word = (word & ~ (bfd_vma) 0x7fffffff) | (prelval & 0x7fffffff);
9674 addr->section = sym->st_shndx;
9675 addr->offset = offset;
9676
9677 if (sym_name)
9678 * sym_name = sym->st_name;
9679 break;
9680 }
9681
9682 *wordp = word;
9683 arm_sec->next_rela = rp;
9684
9685 return true;
9686 }
9687
9688 static const char *tic6x_unwind_regnames[16] =
9689 {
9690 "A15", "B15", "B14", "B13", "B12", "B11", "B10", "B3",
9691 "A14", "A13", "A12", "A11", "A10",
9692 "[invalid reg 13]", "[invalid reg 14]", "[invalid reg 15]"
9693 };
9694
9695 static void
9696 decode_tic6x_unwind_regmask (unsigned int mask)
9697 {
9698 int i;
9699
9700 for (i = 12; mask; mask >>= 1, i--)
9701 {
9702 if (mask & 1)
9703 {
9704 fputs (tic6x_unwind_regnames[i], stdout);
9705 if (mask > 1)
9706 fputs (", ", stdout);
9707 }
9708 }
9709 }
9710
9711 #define ADVANCE \
9712 if (remaining == 0 && more_words) \
9713 { \
9714 data_offset += 4; \
9715 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, \
9716 data_offset, & word, & addr, NULL)) \
9717 return false; \
9718 remaining = 4; \
9719 more_words--; \
9720 } \
9721
9722 #define GET_OP(OP) \
9723 ADVANCE; \
9724 if (remaining) \
9725 { \
9726 remaining--; \
9727 (OP) = word >> 24; \
9728 word <<= 8; \
9729 } \
9730 else \
9731 { \
9732 printf (_("[Truncated opcode]\n")); \
9733 return false; \
9734 } \
9735 printf ("0x%02x ", OP)
9736
9737 static bool
9738 decode_arm_unwind_bytecode (Filedata * filedata,
9739 struct arm_unw_aux_info * aux,
9740 unsigned int word,
9741 unsigned int remaining,
9742 unsigned int more_words,
9743 bfd_vma data_offset,
9744 Elf_Internal_Shdr * data_sec,
9745 struct arm_section * data_arm_sec)
9746 {
9747 struct absaddr addr;
9748 bool res = true;
9749
9750 /* Decode the unwinding instructions. */
9751 while (1)
9752 {
9753 unsigned int op, op2;
9754
9755 ADVANCE;
9756 if (remaining == 0)
9757 break;
9758 remaining--;
9759 op = word >> 24;
9760 word <<= 8;
9761
9762 printf (" 0x%02x ", op);
9763
9764 if ((op & 0xc0) == 0x00)
9765 {
9766 int offset = ((op & 0x3f) << 2) + 4;
9767
9768 printf (" vsp = vsp + %d", offset);
9769 }
9770 else if ((op & 0xc0) == 0x40)
9771 {
9772 int offset = ((op & 0x3f) << 2) + 4;
9773
9774 printf (" vsp = vsp - %d", offset);
9775 }
9776 else if ((op & 0xf0) == 0x80)
9777 {
9778 GET_OP (op2);
9779 if (op == 0x80 && op2 == 0)
9780 printf (_("Refuse to unwind"));
9781 else
9782 {
9783 unsigned int mask = ((op & 0x0f) << 8) | op2;
9784 bool first = true;
9785 int i;
9786
9787 printf ("pop {");
9788 for (i = 0; i < 12; i++)
9789 if (mask & (1 << i))
9790 {
9791 if (first)
9792 first = false;
9793 else
9794 printf (", ");
9795 printf ("r%d", 4 + i);
9796 }
9797 printf ("}");
9798 }
9799 }
9800 else if ((op & 0xf0) == 0x90)
9801 {
9802 if (op == 0x9d || op == 0x9f)
9803 printf (_(" [Reserved]"));
9804 else
9805 printf (" vsp = r%d", op & 0x0f);
9806 }
9807 else if ((op & 0xf0) == 0xa0)
9808 {
9809 int end = 4 + (op & 0x07);
9810 bool first = true;
9811 int i;
9812
9813 printf (" pop {");
9814 for (i = 4; i <= end; i++)
9815 {
9816 if (first)
9817 first = false;
9818 else
9819 printf (", ");
9820 printf ("r%d", i);
9821 }
9822 if (op & 0x08)
9823 {
9824 if (!first)
9825 printf (", ");
9826 printf ("r14");
9827 }
9828 printf ("}");
9829 }
9830 else if (op == 0xb0)
9831 printf (_(" finish"));
9832 else if (op == 0xb1)
9833 {
9834 GET_OP (op2);
9835 if (op2 == 0 || (op2 & 0xf0) != 0)
9836 printf (_("[Spare]"));
9837 else
9838 {
9839 unsigned int mask = op2 & 0x0f;
9840 bool first = true;
9841 int i;
9842
9843 printf ("pop {");
9844 for (i = 0; i < 12; i++)
9845 if (mask & (1 << i))
9846 {
9847 if (first)
9848 first = false;
9849 else
9850 printf (", ");
9851 printf ("r%d", i);
9852 }
9853 printf ("}");
9854 }
9855 }
9856 else if (op == 0xb2)
9857 {
9858 unsigned char buf[9];
9859 unsigned int i, len;
9860 unsigned long offset;
9861
9862 for (i = 0; i < sizeof (buf); i++)
9863 {
9864 GET_OP (buf[i]);
9865 if ((buf[i] & 0x80) == 0)
9866 break;
9867 }
9868 if (i == sizeof (buf))
9869 {
9870 error (_("corrupt change to vsp\n"));
9871 res = false;
9872 }
9873 else
9874 {
9875 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
9876 assert (len == i + 1);
9877 offset = offset * 4 + 0x204;
9878 printf ("vsp = vsp + %ld", offset);
9879 }
9880 }
9881 else if (op == 0xb3 || op == 0xc8 || op == 0xc9)
9882 {
9883 unsigned int first, last;
9884
9885 GET_OP (op2);
9886 first = op2 >> 4;
9887 last = op2 & 0x0f;
9888 if (op == 0xc8)
9889 first = first + 16;
9890 printf ("pop {D%d", first);
9891 if (last)
9892 printf ("-D%d", first + last);
9893 printf ("}");
9894 }
9895 else if (op == 0xb4)
9896 printf (_(" pop {ra_auth_code}"));
9897 else if ((op & 0xf8) == 0xb8 || (op & 0xf8) == 0xd0)
9898 {
9899 unsigned int count = op & 0x07;
9900
9901 printf ("pop {D8");
9902 if (count)
9903 printf ("-D%d", 8 + count);
9904 printf ("}");
9905 }
9906 else if (op >= 0xc0 && op <= 0xc5)
9907 {
9908 unsigned int count = op & 0x07;
9909
9910 printf (" pop {wR10");
9911 if (count)
9912 printf ("-wR%d", 10 + count);
9913 printf ("}");
9914 }
9915 else if (op == 0xc6)
9916 {
9917 unsigned int first, last;
9918
9919 GET_OP (op2);
9920 first = op2 >> 4;
9921 last = op2 & 0x0f;
9922 printf ("pop {wR%d", first);
9923 if (last)
9924 printf ("-wR%d", first + last);
9925 printf ("}");
9926 }
9927 else if (op == 0xc7)
9928 {
9929 GET_OP (op2);
9930 if (op2 == 0 || (op2 & 0xf0) != 0)
9931 printf (_("[Spare]"));
9932 else
9933 {
9934 unsigned int mask = op2 & 0x0f;
9935 bool first = true;
9936 int i;
9937
9938 printf ("pop {");
9939 for (i = 0; i < 4; i++)
9940 if (mask & (1 << i))
9941 {
9942 if (first)
9943 first = false;
9944 else
9945 printf (", ");
9946 printf ("wCGR%d", i);
9947 }
9948 printf ("}");
9949 }
9950 }
9951 else
9952 {
9953 printf (_(" [unsupported opcode]"));
9954 res = false;
9955 }
9956
9957 printf ("\n");
9958 }
9959
9960 return res;
9961 }
9962
9963 static bool
9964 decode_tic6x_unwind_bytecode (Filedata * filedata,
9965 struct arm_unw_aux_info * aux,
9966 unsigned int word,
9967 unsigned int remaining,
9968 unsigned int more_words,
9969 bfd_vma data_offset,
9970 Elf_Internal_Shdr * data_sec,
9971 struct arm_section * data_arm_sec)
9972 {
9973 struct absaddr addr;
9974
9975 /* Decode the unwinding instructions. */
9976 while (1)
9977 {
9978 unsigned int op, op2;
9979
9980 ADVANCE;
9981 if (remaining == 0)
9982 break;
9983 remaining--;
9984 op = word >> 24;
9985 word <<= 8;
9986
9987 printf (" 0x%02x ", op);
9988
9989 if ((op & 0xc0) == 0x00)
9990 {
9991 int offset = ((op & 0x3f) << 3) + 8;
9992 printf (" sp = sp + %d", offset);
9993 }
9994 else if ((op & 0xc0) == 0x80)
9995 {
9996 GET_OP (op2);
9997 if (op == 0x80 && op2 == 0)
9998 printf (_("Refuse to unwind"));
9999 else
10000 {
10001 unsigned int mask = ((op & 0x1f) << 8) | op2;
10002 if (op & 0x20)
10003 printf ("pop compact {");
10004 else
10005 printf ("pop {");
10006
10007 decode_tic6x_unwind_regmask (mask);
10008 printf("}");
10009 }
10010 }
10011 else if ((op & 0xf0) == 0xc0)
10012 {
10013 unsigned int reg;
10014 unsigned int nregs;
10015 unsigned int i;
10016 const char *name;
10017 struct
10018 {
10019 unsigned int offset;
10020 unsigned int reg;
10021 } regpos[16];
10022
10023 /* Scan entire instruction first so that GET_OP output is not
10024 interleaved with disassembly. */
10025 nregs = 0;
10026 for (i = 0; nregs < (op & 0xf); i++)
10027 {
10028 GET_OP (op2);
10029 reg = op2 >> 4;
10030 if (reg != 0xf)
10031 {
10032 regpos[nregs].offset = i * 2;
10033 regpos[nregs].reg = reg;
10034 nregs++;
10035 }
10036
10037 reg = op2 & 0xf;
10038 if (reg != 0xf)
10039 {
10040 regpos[nregs].offset = i * 2 + 1;
10041 regpos[nregs].reg = reg;
10042 nregs++;
10043 }
10044 }
10045
10046 printf (_("pop frame {"));
10047 if (nregs == 0)
10048 {
10049 printf (_("*corrupt* - no registers specified"));
10050 }
10051 else
10052 {
10053 reg = nregs - 1;
10054 for (i = i * 2; i > 0; i--)
10055 {
10056 if (regpos[reg].offset == i - 1)
10057 {
10058 name = tic6x_unwind_regnames[regpos[reg].reg];
10059 if (reg > 0)
10060 reg--;
10061 }
10062 else
10063 name = _("[pad]");
10064
10065 fputs (name, stdout);
10066 if (i > 1)
10067 printf (", ");
10068 }
10069 }
10070
10071 printf ("}");
10072 }
10073 else if (op == 0xd0)
10074 printf (" MOV FP, SP");
10075 else if (op == 0xd1)
10076 printf (" __c6xabi_pop_rts");
10077 else if (op == 0xd2)
10078 {
10079 unsigned char buf[9];
10080 unsigned int i, len;
10081 unsigned long offset;
10082
10083 for (i = 0; i < sizeof (buf); i++)
10084 {
10085 GET_OP (buf[i]);
10086 if ((buf[i] & 0x80) == 0)
10087 break;
10088 }
10089 /* PR 17531: file: id:000001,src:001906+004739,op:splice,rep:2. */
10090 if (i == sizeof (buf))
10091 {
10092 warn (_("Corrupt stack pointer adjustment detected\n"));
10093 return false;
10094 }
10095
10096 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
10097 assert (len == i + 1);
10098 offset = offset * 8 + 0x408;
10099 printf (_("sp = sp + %ld"), offset);
10100 }
10101 else if ((op & 0xf0) == 0xe0)
10102 {
10103 if ((op & 0x0f) == 7)
10104 printf (" RETURN");
10105 else
10106 printf (" MV %s, B3", tic6x_unwind_regnames[op & 0x0f]);
10107 }
10108 else
10109 {
10110 printf (_(" [unsupported opcode]"));
10111 }
10112 putchar ('\n');
10113 }
10114
10115 return true;
10116 }
10117
10118 static bfd_vma
10119 arm_expand_prel31 (Filedata * filedata, bfd_vma word, bfd_vma where)
10120 {
10121 bfd_vma offset;
10122
10123 offset = word & 0x7fffffff;
10124 if (offset & 0x40000000)
10125 offset |= ~ (bfd_vma) 0x7fffffff;
10126
10127 if (filedata->file_header.e_machine == EM_TI_C6000)
10128 offset <<= 1;
10129
10130 return offset + where;
10131 }
10132
10133 static bool
10134 decode_arm_unwind (Filedata * filedata,
10135 struct arm_unw_aux_info * aux,
10136 unsigned int word,
10137 unsigned int remaining,
10138 bfd_vma data_offset,
10139 Elf_Internal_Shdr * data_sec,
10140 struct arm_section * data_arm_sec)
10141 {
10142 int per_index;
10143 unsigned int more_words = 0;
10144 struct absaddr addr;
10145 bfd_vma sym_name = (bfd_vma) -1;
10146 bool res = true;
10147
10148 if (remaining == 0)
10149 {
10150 /* Fetch the first word.
10151 Note - when decoding an object file the address extracted
10152 here will always be 0. So we also pass in the sym_name
10153 parameter so that we can find the symbol associated with
10154 the personality routine. */
10155 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, data_offset,
10156 & word, & addr, & sym_name))
10157 return false;
10158
10159 remaining = 4;
10160 }
10161 else
10162 {
10163 addr.section = SHN_UNDEF;
10164 addr.offset = 0;
10165 }
10166
10167 if ((word & 0x80000000) == 0)
10168 {
10169 /* Expand prel31 for personality routine. */
10170 bfd_vma fn;
10171 const char *procname;
10172
10173 fn = arm_expand_prel31 (filedata, word, data_sec->sh_addr + data_offset);
10174 printf (_(" Personality routine: "));
10175 if (fn == 0
10176 && addr.section == SHN_UNDEF && addr.offset == 0
10177 && sym_name != (bfd_vma) -1 && sym_name < aux->strtab_size)
10178 {
10179 procname = aux->strtab + sym_name;
10180 print_vma (fn, PREFIX_HEX);
10181 if (procname)
10182 {
10183 fputs (" <", stdout);
10184 fputs (procname, stdout);
10185 fputc ('>', stdout);
10186 }
10187 }
10188 else
10189 procname = arm_print_vma_and_name (filedata, aux, fn, addr);
10190 fputc ('\n', stdout);
10191
10192 /* The GCC personality routines use the standard compact
10193 encoding, starting with one byte giving the number of
10194 words. */
10195 if (procname != NULL
10196 && (startswith (procname, "__gcc_personality_v0")
10197 || startswith (procname, "__gxx_personality_v0")
10198 || startswith (procname, "__gcj_personality_v0")
10199 || startswith (procname, "__gnu_objc_personality_v0")))
10200 {
10201 remaining = 0;
10202 more_words = 1;
10203 ADVANCE;
10204 if (!remaining)
10205 {
10206 printf (_(" [Truncated data]\n"));
10207 return false;
10208 }
10209 more_words = word >> 24;
10210 word <<= 8;
10211 remaining--;
10212 per_index = -1;
10213 }
10214 else
10215 return true;
10216 }
10217 else
10218 {
10219 /* ARM EHABI Section 6.3:
10220
10221 An exception-handling table entry for the compact model looks like:
10222
10223 31 30-28 27-24 23-0
10224 -- ----- ----- ----
10225 1 0 index Data for personalityRoutine[index] */
10226
10227 if (filedata->file_header.e_machine == EM_ARM
10228 && (word & 0x70000000))
10229 {
10230 warn (_("Corrupt ARM compact model table entry: %x \n"), word);
10231 res = false;
10232 }
10233
10234 per_index = (word >> 24) & 0x7f;
10235 printf (_(" Compact model index: %d\n"), per_index);
10236 if (per_index == 0)
10237 {
10238 more_words = 0;
10239 word <<= 8;
10240 remaining--;
10241 }
10242 else if (per_index < 3)
10243 {
10244 more_words = (word >> 16) & 0xff;
10245 word <<= 16;
10246 remaining -= 2;
10247 }
10248 }
10249
10250 switch (filedata->file_header.e_machine)
10251 {
10252 case EM_ARM:
10253 if (per_index < 3)
10254 {
10255 if (! decode_arm_unwind_bytecode (filedata, aux, word, remaining, more_words,
10256 data_offset, data_sec, data_arm_sec))
10257 res = false;
10258 }
10259 else
10260 {
10261 warn (_("Unknown ARM compact model index encountered\n"));
10262 printf (_(" [reserved]\n"));
10263 res = false;
10264 }
10265 break;
10266
10267 case EM_TI_C6000:
10268 if (per_index < 3)
10269 {
10270 if (! decode_tic6x_unwind_bytecode (filedata, aux, word, remaining, more_words,
10271 data_offset, data_sec, data_arm_sec))
10272 res = false;
10273 }
10274 else if (per_index < 5)
10275 {
10276 if (((word >> 17) & 0x7f) == 0x7f)
10277 printf (_(" Restore stack from frame pointer\n"));
10278 else
10279 printf (_(" Stack increment %d\n"), (word >> 14) & 0x1fc);
10280 printf (_(" Registers restored: "));
10281 if (per_index == 4)
10282 printf (" (compact) ");
10283 decode_tic6x_unwind_regmask ((word >> 4) & 0x1fff);
10284 putchar ('\n');
10285 printf (_(" Return register: %s\n"),
10286 tic6x_unwind_regnames[word & 0xf]);
10287 }
10288 else
10289 printf (_(" [reserved (%d)]\n"), per_index);
10290 break;
10291
10292 default:
10293 error (_("Unsupported architecture type %d encountered when decoding unwind table\n"),
10294 filedata->file_header.e_machine);
10295 res = false;
10296 }
10297
10298 /* Decode the descriptors. Not implemented. */
10299
10300 return res;
10301 }
10302
10303 static bool
10304 dump_arm_unwind (Filedata * filedata,
10305 struct arm_unw_aux_info * aux,
10306 Elf_Internal_Shdr * exidx_sec)
10307 {
10308 struct arm_section exidx_arm_sec, extab_arm_sec;
10309 unsigned int i, exidx_len;
10310 unsigned long j, nfuns;
10311 bool res = true;
10312
10313 memset (&exidx_arm_sec, 0, sizeof (exidx_arm_sec));
10314 memset (&extab_arm_sec, 0, sizeof (extab_arm_sec));
10315 exidx_len = exidx_sec->sh_size / 8;
10316
10317 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
10318 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
10319 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
10320 aux->funtab[nfuns++] = aux->symtab[j];
10321 aux->nfuns = nfuns;
10322 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
10323
10324 for (i = 0; i < exidx_len; i++)
10325 {
10326 unsigned int exidx_fn, exidx_entry;
10327 struct absaddr fn_addr, entry_addr;
10328 bfd_vma fn;
10329
10330 fputc ('\n', stdout);
10331
10332 if (! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
10333 8 * i, & exidx_fn, & fn_addr, NULL)
10334 || ! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
10335 8 * i + 4, & exidx_entry, & entry_addr, NULL))
10336 {
10337 free (aux->funtab);
10338 arm_free_section (& exidx_arm_sec);
10339 arm_free_section (& extab_arm_sec);
10340 return false;
10341 }
10342
10343 /* ARM EHABI, Section 5:
10344 An index table entry consists of 2 words.
10345 The first word contains a prel31 offset to the start of a function, with bit 31 clear. */
10346 if (exidx_fn & 0x80000000)
10347 {
10348 warn (_("corrupt index table entry: %x\n"), exidx_fn);
10349 res = false;
10350 }
10351
10352 fn = arm_expand_prel31 (filedata, exidx_fn, exidx_sec->sh_addr + 8 * i);
10353
10354 arm_print_vma_and_name (filedata, aux, fn, fn_addr);
10355 fputs (": ", stdout);
10356
10357 if (exidx_entry == 1)
10358 {
10359 print_vma (exidx_entry, PREFIX_HEX);
10360 fputs (" [cantunwind]\n", stdout);
10361 }
10362 else if (exidx_entry & 0x80000000)
10363 {
10364 print_vma (exidx_entry, PREFIX_HEX);
10365 fputc ('\n', stdout);
10366 decode_arm_unwind (filedata, aux, exidx_entry, 4, 0, NULL, NULL);
10367 }
10368 else
10369 {
10370 bfd_vma table, table_offset = 0;
10371 Elf_Internal_Shdr *table_sec;
10372
10373 fputs ("@", stdout);
10374 table = arm_expand_prel31 (filedata, exidx_entry, exidx_sec->sh_addr + 8 * i + 4);
10375 print_vma (table, PREFIX_HEX);
10376 printf ("\n");
10377
10378 /* Locate the matching .ARM.extab. */
10379 if (entry_addr.section != SHN_UNDEF
10380 && entry_addr.section < filedata->file_header.e_shnum)
10381 {
10382 table_sec = filedata->section_headers + entry_addr.section;
10383 table_offset = entry_addr.offset;
10384 /* PR 18879 */
10385 if (table_offset > table_sec->sh_size
10386 || ((bfd_signed_vma) table_offset) < 0)
10387 {
10388 warn (_("Unwind entry contains corrupt offset (0x%lx) into section %s\n"),
10389 (unsigned long) table_offset,
10390 printable_section_name (filedata, table_sec));
10391 res = false;
10392 continue;
10393 }
10394 }
10395 else
10396 {
10397 table_sec = find_section_by_address (filedata, table);
10398 if (table_sec != NULL)
10399 table_offset = table - table_sec->sh_addr;
10400 }
10401
10402 if (table_sec == NULL)
10403 {
10404 warn (_("Could not locate .ARM.extab section containing 0x%lx.\n"),
10405 (unsigned long) table);
10406 res = false;
10407 continue;
10408 }
10409
10410 if (! decode_arm_unwind (filedata, aux, 0, 0, table_offset, table_sec,
10411 &extab_arm_sec))
10412 res = false;
10413 }
10414 }
10415
10416 printf ("\n");
10417
10418 free (aux->funtab);
10419 arm_free_section (&exidx_arm_sec);
10420 arm_free_section (&extab_arm_sec);
10421
10422 return res;
10423 }
10424
10425 /* Used for both ARM and C6X unwinding tables. */
10426
10427 static bool
10428 arm_process_unwind (Filedata * filedata)
10429 {
10430 struct arm_unw_aux_info aux;
10431 Elf_Internal_Shdr *unwsec = NULL;
10432 Elf_Internal_Shdr *sec;
10433 unsigned long i;
10434 unsigned int sec_type;
10435 bool res = true;
10436
10437 switch (filedata->file_header.e_machine)
10438 {
10439 case EM_ARM:
10440 sec_type = SHT_ARM_EXIDX;
10441 break;
10442
10443 case EM_TI_C6000:
10444 sec_type = SHT_C6000_UNWIND;
10445 break;
10446
10447 default:
10448 error (_("Unsupported architecture type %d encountered when processing unwind table\n"),
10449 filedata->file_header.e_machine);
10450 return false;
10451 }
10452
10453 if (filedata->string_table == NULL)
10454 return false;
10455
10456 memset (& aux, 0, sizeof (aux));
10457 aux.filedata = filedata;
10458
10459 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
10460 {
10461 if (sec->sh_type == SHT_SYMTAB)
10462 {
10463 if (aux.symtab)
10464 {
10465 error (_("Multiple symbol tables encountered\n"));
10466 free (aux.symtab);
10467 aux.symtab = NULL;
10468 free (aux.strtab);
10469 aux.strtab = NULL;
10470 }
10471 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
10472 &aux.strtab, &aux.strtab_size))
10473 return false;
10474 }
10475 else if (sec->sh_type == sec_type)
10476 unwsec = sec;
10477 }
10478
10479 if (unwsec == NULL)
10480 printf (_("\nThere are no unwind sections in this file.\n"));
10481 else
10482 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
10483 {
10484 if (sec->sh_type == sec_type)
10485 {
10486 unsigned long num_unwind = sec->sh_size / (2 * eh_addr_size);
10487 printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
10488 "contains %lu entry:\n",
10489 "\nUnwind section '%s' at offset 0x%lx "
10490 "contains %lu entries:\n",
10491 num_unwind),
10492 printable_section_name (filedata, sec),
10493 (unsigned long) sec->sh_offset,
10494 num_unwind);
10495
10496 if (! dump_arm_unwind (filedata, &aux, sec))
10497 res = false;
10498 }
10499 }
10500
10501 free (aux.symtab);
10502 free ((char *) aux.strtab);
10503
10504 return res;
10505 }
10506
10507 static bool
10508 no_processor_specific_unwind (Filedata * filedata ATTRIBUTE_UNUSED)
10509 {
10510 printf (_("No processor specific unwind information to decode\n"));
10511 return true;
10512 }
10513
10514 static bool
10515 process_unwind (Filedata * filedata)
10516 {
10517 struct unwind_handler
10518 {
10519 unsigned int machtype;
10520 bool (* handler)(Filedata *);
10521 } handlers[] =
10522 {
10523 { EM_ARM, arm_process_unwind },
10524 { EM_IA_64, ia64_process_unwind },
10525 { EM_PARISC, hppa_process_unwind },
10526 { EM_TI_C6000, arm_process_unwind },
10527 { EM_386, no_processor_specific_unwind },
10528 { EM_X86_64, no_processor_specific_unwind },
10529 { 0, NULL }
10530 };
10531 int i;
10532
10533 if (!do_unwind)
10534 return true;
10535
10536 for (i = 0; handlers[i].handler != NULL; i++)
10537 if (filedata->file_header.e_machine == handlers[i].machtype)
10538 return handlers[i].handler (filedata);
10539
10540 printf (_("\nThe decoding of unwind sections for machine type %s is not currently supported.\n"),
10541 get_machine_name (filedata->file_header.e_machine));
10542 return true;
10543 }
10544
10545 static void
10546 dynamic_section_aarch64_val (Elf_Internal_Dyn * entry)
10547 {
10548 switch (entry->d_tag)
10549 {
10550 case DT_AARCH64_BTI_PLT:
10551 case DT_AARCH64_PAC_PLT:
10552 break;
10553 default:
10554 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10555 break;
10556 }
10557 putchar ('\n');
10558 }
10559
10560 static void
10561 dynamic_section_mips_val (Filedata * filedata, Elf_Internal_Dyn * entry)
10562 {
10563 switch (entry->d_tag)
10564 {
10565 case DT_MIPS_FLAGS:
10566 if (entry->d_un.d_val == 0)
10567 printf (_("NONE"));
10568 else
10569 {
10570 static const char * opts[] =
10571 {
10572 "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
10573 "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
10574 "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
10575 "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
10576 "RLD_ORDER_SAFE"
10577 };
10578 unsigned int cnt;
10579 bool first = true;
10580
10581 for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
10582 if (entry->d_un.d_val & (1 << cnt))
10583 {
10584 printf ("%s%s", first ? "" : " ", opts[cnt]);
10585 first = false;
10586 }
10587 }
10588 break;
10589
10590 case DT_MIPS_IVERSION:
10591 if (valid_dynamic_name (filedata, entry->d_un.d_val))
10592 printf (_("Interface Version: %s"),
10593 get_dynamic_name (filedata, entry->d_un.d_val));
10594 else
10595 printf (_("Interface Version: <corrupt: %" PRIx64 ">"),
10596 (uint64_t) entry->d_un.d_ptr);
10597 break;
10598
10599 case DT_MIPS_TIME_STAMP:
10600 {
10601 char timebuf[128];
10602 struct tm * tmp;
10603 time_t atime = entry->d_un.d_val;
10604
10605 tmp = gmtime (&atime);
10606 /* PR 17531: file: 6accc532. */
10607 if (tmp == NULL)
10608 snprintf (timebuf, sizeof (timebuf), _("<corrupt>"));
10609 else
10610 snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
10611 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
10612 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
10613 printf (_("Time Stamp: %s"), timebuf);
10614 }
10615 break;
10616
10617 case DT_MIPS_RLD_VERSION:
10618 case DT_MIPS_LOCAL_GOTNO:
10619 case DT_MIPS_CONFLICTNO:
10620 case DT_MIPS_LIBLISTNO:
10621 case DT_MIPS_SYMTABNO:
10622 case DT_MIPS_UNREFEXTNO:
10623 case DT_MIPS_HIPAGENO:
10624 case DT_MIPS_DELTA_CLASS_NO:
10625 case DT_MIPS_DELTA_INSTANCE_NO:
10626 case DT_MIPS_DELTA_RELOC_NO:
10627 case DT_MIPS_DELTA_SYM_NO:
10628 case DT_MIPS_DELTA_CLASSSYM_NO:
10629 case DT_MIPS_COMPACT_SIZE:
10630 print_vma (entry->d_un.d_val, DEC);
10631 break;
10632
10633 case DT_MIPS_XHASH:
10634 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
10635 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10636 /* Falls through. */
10637
10638 default:
10639 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10640 }
10641 putchar ('\n');
10642 }
10643
10644 static void
10645 dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
10646 {
10647 switch (entry->d_tag)
10648 {
10649 case DT_HP_DLD_FLAGS:
10650 {
10651 static struct
10652 {
10653 long int bit;
10654 const char * str;
10655 }
10656 flags[] =
10657 {
10658 { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
10659 { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
10660 { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
10661 { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
10662 { DT_HP_BIND_NOW, "HP_BIND_NOW" },
10663 { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
10664 { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
10665 { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
10666 { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
10667 { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
10668 { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
10669 { DT_HP_GST, "HP_GST" },
10670 { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
10671 { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
10672 { DT_HP_NODELETE, "HP_NODELETE" },
10673 { DT_HP_GROUP, "HP_GROUP" },
10674 { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
10675 };
10676 bool first = true;
10677 size_t cnt;
10678 bfd_vma val = entry->d_un.d_val;
10679
10680 for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
10681 if (val & flags[cnt].bit)
10682 {
10683 if (! first)
10684 putchar (' ');
10685 fputs (flags[cnt].str, stdout);
10686 first = false;
10687 val ^= flags[cnt].bit;
10688 }
10689
10690 if (val != 0 || first)
10691 {
10692 if (! first)
10693 putchar (' ');
10694 print_vma (val, HEX);
10695 }
10696 }
10697 break;
10698
10699 default:
10700 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10701 break;
10702 }
10703 putchar ('\n');
10704 }
10705
10706 #ifdef BFD64
10707
10708 /* VMS vs Unix time offset and factor. */
10709
10710 #define VMS_EPOCH_OFFSET 35067168000000000LL
10711 #define VMS_GRANULARITY_FACTOR 10000000
10712 #ifndef INT64_MIN
10713 #define INT64_MIN (-9223372036854775807LL - 1)
10714 #endif
10715
10716 /* Display a VMS time in a human readable format. */
10717
10718 static void
10719 print_vms_time (int64_t vmstime)
10720 {
10721 struct tm *tm = NULL;
10722 time_t unxtime;
10723
10724 if (vmstime >= INT64_MIN + VMS_EPOCH_OFFSET)
10725 {
10726 vmstime = (vmstime - VMS_EPOCH_OFFSET) / VMS_GRANULARITY_FACTOR;
10727 unxtime = vmstime;
10728 if (unxtime == vmstime)
10729 tm = gmtime (&unxtime);
10730 }
10731 if (tm != NULL)
10732 printf ("%04u-%02u-%02uT%02u:%02u:%02u",
10733 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
10734 tm->tm_hour, tm->tm_min, tm->tm_sec);
10735 }
10736 #endif /* BFD64 */
10737
10738 static void
10739 dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
10740 {
10741 switch (entry->d_tag)
10742 {
10743 case DT_IA_64_PLT_RESERVE:
10744 /* First 3 slots reserved. */
10745 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10746 printf (" -- ");
10747 print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
10748 break;
10749
10750 case DT_IA_64_VMS_LINKTIME:
10751 #ifdef BFD64
10752 print_vms_time (entry->d_un.d_val);
10753 #endif
10754 break;
10755
10756 case DT_IA_64_VMS_LNKFLAGS:
10757 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10758 if (entry->d_un.d_val & VMS_LF_CALL_DEBUG)
10759 printf (" CALL_DEBUG");
10760 if (entry->d_un.d_val & VMS_LF_NOP0BUFS)
10761 printf (" NOP0BUFS");
10762 if (entry->d_un.d_val & VMS_LF_P0IMAGE)
10763 printf (" P0IMAGE");
10764 if (entry->d_un.d_val & VMS_LF_MKTHREADS)
10765 printf (" MKTHREADS");
10766 if (entry->d_un.d_val & VMS_LF_UPCALLS)
10767 printf (" UPCALLS");
10768 if (entry->d_un.d_val & VMS_LF_IMGSTA)
10769 printf (" IMGSTA");
10770 if (entry->d_un.d_val & VMS_LF_INITIALIZE)
10771 printf (" INITIALIZE");
10772 if (entry->d_un.d_val & VMS_LF_MAIN)
10773 printf (" MAIN");
10774 if (entry->d_un.d_val & VMS_LF_EXE_INIT)
10775 printf (" EXE_INIT");
10776 if (entry->d_un.d_val & VMS_LF_TBK_IN_IMG)
10777 printf (" TBK_IN_IMG");
10778 if (entry->d_un.d_val & VMS_LF_DBG_IN_IMG)
10779 printf (" DBG_IN_IMG");
10780 if (entry->d_un.d_val & VMS_LF_TBK_IN_DSF)
10781 printf (" TBK_IN_DSF");
10782 if (entry->d_un.d_val & VMS_LF_DBG_IN_DSF)
10783 printf (" DBG_IN_DSF");
10784 if (entry->d_un.d_val & VMS_LF_SIGNATURES)
10785 printf (" SIGNATURES");
10786 if (entry->d_un.d_val & VMS_LF_REL_SEG_OFF)
10787 printf (" REL_SEG_OFF");
10788 break;
10789
10790 default:
10791 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10792 break;
10793 }
10794 putchar ('\n');
10795 }
10796
10797 static bool
10798 get_32bit_dynamic_section (Filedata * filedata)
10799 {
10800 Elf32_External_Dyn * edyn;
10801 Elf32_External_Dyn * ext;
10802 Elf_Internal_Dyn * entry;
10803
10804 edyn = (Elf32_External_Dyn *) get_data (NULL, filedata,
10805 filedata->dynamic_addr, 1,
10806 filedata->dynamic_size,
10807 _("dynamic section"));
10808 if (!edyn)
10809 return false;
10810
10811 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
10812 might not have the luxury of section headers. Look for the DT_NULL
10813 terminator to determine the number of entries. */
10814 for (ext = edyn, filedata->dynamic_nent = 0;
10815 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
10816 ext++)
10817 {
10818 filedata->dynamic_nent++;
10819 if (BYTE_GET (ext->d_tag) == DT_NULL)
10820 break;
10821 }
10822
10823 filedata->dynamic_section
10824 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
10825 if (filedata->dynamic_section == NULL)
10826 {
10827 error (_("Out of memory allocating space for %lu dynamic entries\n"),
10828 (unsigned long) filedata->dynamic_nent);
10829 free (edyn);
10830 return false;
10831 }
10832
10833 for (ext = edyn, entry = filedata->dynamic_section;
10834 entry < filedata->dynamic_section + filedata->dynamic_nent;
10835 ext++, entry++)
10836 {
10837 entry->d_tag = BYTE_GET (ext->d_tag);
10838 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
10839 }
10840
10841 free (edyn);
10842
10843 return true;
10844 }
10845
10846 static bool
10847 get_64bit_dynamic_section (Filedata * filedata)
10848 {
10849 Elf64_External_Dyn * edyn;
10850 Elf64_External_Dyn * ext;
10851 Elf_Internal_Dyn * entry;
10852
10853 /* Read in the data. */
10854 edyn = (Elf64_External_Dyn *) get_data (NULL, filedata,
10855 filedata->dynamic_addr, 1,
10856 filedata->dynamic_size,
10857 _("dynamic section"));
10858 if (!edyn)
10859 return false;
10860
10861 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
10862 might not have the luxury of section headers. Look for the DT_NULL
10863 terminator to determine the number of entries. */
10864 for (ext = edyn, filedata->dynamic_nent = 0;
10865 /* PR 17533 file: 033-67080-0.004 - do not read past end of buffer. */
10866 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
10867 ext++)
10868 {
10869 filedata->dynamic_nent++;
10870 if (BYTE_GET (ext->d_tag) == DT_NULL)
10871 break;
10872 }
10873
10874 filedata->dynamic_section
10875 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
10876 if (filedata->dynamic_section == NULL)
10877 {
10878 error (_("Out of memory allocating space for %lu dynamic entries\n"),
10879 (unsigned long) filedata->dynamic_nent);
10880 free (edyn);
10881 return false;
10882 }
10883
10884 /* Convert from external to internal formats. */
10885 for (ext = edyn, entry = filedata->dynamic_section;
10886 entry < filedata->dynamic_section + filedata->dynamic_nent;
10887 ext++, entry++)
10888 {
10889 entry->d_tag = BYTE_GET (ext->d_tag);
10890 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
10891 }
10892
10893 free (edyn);
10894
10895 return true;
10896 }
10897
10898 static bool
10899 get_dynamic_section (Filedata *filedata)
10900 {
10901 if (filedata->dynamic_section)
10902 return true;
10903
10904 if (is_32bit_elf)
10905 return get_32bit_dynamic_section (filedata);
10906 else
10907 return get_64bit_dynamic_section (filedata);
10908 }
10909
10910 static void
10911 print_dynamic_flags (bfd_vma flags)
10912 {
10913 bool first = true;
10914
10915 while (flags)
10916 {
10917 bfd_vma flag;
10918
10919 flag = flags & - flags;
10920 flags &= ~ flag;
10921
10922 if (first)
10923 first = false;
10924 else
10925 putc (' ', stdout);
10926
10927 switch (flag)
10928 {
10929 case DF_ORIGIN: fputs ("ORIGIN", stdout); break;
10930 case DF_SYMBOLIC: fputs ("SYMBOLIC", stdout); break;
10931 case DF_TEXTREL: fputs ("TEXTREL", stdout); break;
10932 case DF_BIND_NOW: fputs ("BIND_NOW", stdout); break;
10933 case DF_STATIC_TLS: fputs ("STATIC_TLS", stdout); break;
10934 default: fputs (_("unknown"), stdout); break;
10935 }
10936 }
10937 puts ("");
10938 }
10939
10940 static bfd_vma *
10941 get_dynamic_data (Filedata * filedata, bfd_size_type number, unsigned int ent_size)
10942 {
10943 unsigned char * e_data;
10944 bfd_vma * i_data;
10945
10946 /* If the size_t type is smaller than the bfd_size_type, eg because
10947 you are building a 32-bit tool on a 64-bit host, then make sure
10948 that when (number) is cast to (size_t) no information is lost. */
10949 if (sizeof (size_t) < sizeof (bfd_size_type)
10950 && (bfd_size_type) ((size_t) number) != number)
10951 {
10952 error (_("Size truncation prevents reading %s elements of size %u\n"),
10953 bfd_vmatoa ("u", number), ent_size);
10954 return NULL;
10955 }
10956
10957 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
10958 attempting to allocate memory when the read is bound to fail. */
10959 if (ent_size * number > filedata->file_size)
10960 {
10961 error (_("Invalid number of dynamic entries: %s\n"),
10962 bfd_vmatoa ("u", number));
10963 return NULL;
10964 }
10965
10966 e_data = (unsigned char *) cmalloc ((size_t) number, ent_size);
10967 if (e_data == NULL)
10968 {
10969 error (_("Out of memory reading %s dynamic entries\n"),
10970 bfd_vmatoa ("u", number));
10971 return NULL;
10972 }
10973
10974 if (fread (e_data, ent_size, (size_t) number, filedata->handle) != number)
10975 {
10976 error (_("Unable to read in %s bytes of dynamic data\n"),
10977 bfd_vmatoa ("u", number * ent_size));
10978 free (e_data);
10979 return NULL;
10980 }
10981
10982 i_data = (bfd_vma *) cmalloc ((size_t) number, sizeof (*i_data));
10983 if (i_data == NULL)
10984 {
10985 error (_("Out of memory allocating space for %s dynamic entries\n"),
10986 bfd_vmatoa ("u", number));
10987 free (e_data);
10988 return NULL;
10989 }
10990
10991 while (number--)
10992 i_data[number] = byte_get (e_data + number * ent_size, ent_size);
10993
10994 free (e_data);
10995
10996 return i_data;
10997 }
10998
10999 static unsigned long
11000 get_num_dynamic_syms (Filedata * filedata)
11001 {
11002 unsigned long num_of_syms = 0;
11003
11004 if (!do_histogram && (!do_using_dynamic || do_dyn_syms))
11005 return num_of_syms;
11006
11007 if (filedata->dynamic_info[DT_HASH])
11008 {
11009 unsigned char nb[8];
11010 unsigned char nc[8];
11011 unsigned int hash_ent_size = 4;
11012
11013 if ((filedata->file_header.e_machine == EM_ALPHA
11014 || filedata->file_header.e_machine == EM_S390
11015 || filedata->file_header.e_machine == EM_S390_OLD)
11016 && filedata->file_header.e_ident[EI_CLASS] == ELFCLASS64)
11017 hash_ent_size = 8;
11018
11019 if (fseek (filedata->handle,
11020 (filedata->archive_file_offset
11021 + offset_from_vma (filedata, filedata->dynamic_info[DT_HASH],
11022 sizeof nb + sizeof nc)),
11023 SEEK_SET))
11024 {
11025 error (_("Unable to seek to start of dynamic information\n"));
11026 goto no_hash;
11027 }
11028
11029 if (fread (nb, hash_ent_size, 1, filedata->handle) != 1)
11030 {
11031 error (_("Failed to read in number of buckets\n"));
11032 goto no_hash;
11033 }
11034
11035 if (fread (nc, hash_ent_size, 1, filedata->handle) != 1)
11036 {
11037 error (_("Failed to read in number of chains\n"));
11038 goto no_hash;
11039 }
11040
11041 filedata->nbuckets = byte_get (nb, hash_ent_size);
11042 filedata->nchains = byte_get (nc, hash_ent_size);
11043
11044 if (filedata->nbuckets != 0 && filedata->nchains != 0)
11045 {
11046 filedata->buckets = get_dynamic_data (filedata, filedata->nbuckets,
11047 hash_ent_size);
11048 filedata->chains = get_dynamic_data (filedata, filedata->nchains,
11049 hash_ent_size);
11050
11051 if (filedata->buckets != NULL && filedata->chains != NULL)
11052 num_of_syms = filedata->nchains;
11053 }
11054 no_hash:
11055 if (num_of_syms == 0)
11056 {
11057 free (filedata->buckets);
11058 filedata->buckets = NULL;
11059 free (filedata->chains);
11060 filedata->chains = NULL;
11061 filedata->nbuckets = 0;
11062 }
11063 }
11064
11065 if (filedata->dynamic_info_DT_GNU_HASH)
11066 {
11067 unsigned char nb[16];
11068 bfd_vma i, maxchain = 0xffffffff, bitmaskwords;
11069 bfd_vma buckets_vma;
11070 unsigned long hn;
11071
11072 if (fseek (filedata->handle,
11073 (filedata->archive_file_offset
11074 + offset_from_vma (filedata,
11075 filedata->dynamic_info_DT_GNU_HASH,
11076 sizeof nb)),
11077 SEEK_SET))
11078 {
11079 error (_("Unable to seek to start of dynamic information\n"));
11080 goto no_gnu_hash;
11081 }
11082
11083 if (fread (nb, 16, 1, filedata->handle) != 1)
11084 {
11085 error (_("Failed to read in number of buckets\n"));
11086 goto no_gnu_hash;
11087 }
11088
11089 filedata->ngnubuckets = byte_get (nb, 4);
11090 filedata->gnusymidx = byte_get (nb + 4, 4);
11091 bitmaskwords = byte_get (nb + 8, 4);
11092 buckets_vma = filedata->dynamic_info_DT_GNU_HASH + 16;
11093 if (is_32bit_elf)
11094 buckets_vma += bitmaskwords * 4;
11095 else
11096 buckets_vma += bitmaskwords * 8;
11097
11098 if (fseek (filedata->handle,
11099 (filedata->archive_file_offset
11100 + offset_from_vma (filedata, buckets_vma, 4)),
11101 SEEK_SET))
11102 {
11103 error (_("Unable to seek to start of dynamic information\n"));
11104 goto no_gnu_hash;
11105 }
11106
11107 filedata->gnubuckets
11108 = get_dynamic_data (filedata, filedata->ngnubuckets, 4);
11109
11110 if (filedata->gnubuckets == NULL)
11111 goto no_gnu_hash;
11112
11113 for (i = 0; i < filedata->ngnubuckets; i++)
11114 if (filedata->gnubuckets[i] != 0)
11115 {
11116 if (filedata->gnubuckets[i] < filedata->gnusymidx)
11117 goto no_gnu_hash;
11118
11119 if (maxchain == 0xffffffff || filedata->gnubuckets[i] > maxchain)
11120 maxchain = filedata->gnubuckets[i];
11121 }
11122
11123 if (maxchain == 0xffffffff)
11124 goto no_gnu_hash;
11125
11126 maxchain -= filedata->gnusymidx;
11127
11128 if (fseek (filedata->handle,
11129 (filedata->archive_file_offset
11130 + offset_from_vma (filedata,
11131 buckets_vma + 4 * (filedata->ngnubuckets
11132 + maxchain),
11133 4)),
11134 SEEK_SET))
11135 {
11136 error (_("Unable to seek to start of dynamic information\n"));
11137 goto no_gnu_hash;
11138 }
11139
11140 do
11141 {
11142 if (fread (nb, 4, 1, filedata->handle) != 1)
11143 {
11144 error (_("Failed to determine last chain length\n"));
11145 goto no_gnu_hash;
11146 }
11147
11148 if (maxchain + 1 == 0)
11149 goto no_gnu_hash;
11150
11151 ++maxchain;
11152 }
11153 while ((byte_get (nb, 4) & 1) == 0);
11154
11155 if (fseek (filedata->handle,
11156 (filedata->archive_file_offset
11157 + offset_from_vma (filedata, (buckets_vma
11158 + 4 * filedata->ngnubuckets),
11159 4)),
11160 SEEK_SET))
11161 {
11162 error (_("Unable to seek to start of dynamic information\n"));
11163 goto no_gnu_hash;
11164 }
11165
11166 filedata->gnuchains = get_dynamic_data (filedata, maxchain, 4);
11167 filedata->ngnuchains = maxchain;
11168
11169 if (filedata->gnuchains == NULL)
11170 goto no_gnu_hash;
11171
11172 if (filedata->dynamic_info_DT_MIPS_XHASH)
11173 {
11174 if (fseek (filedata->handle,
11175 (filedata->archive_file_offset
11176 + offset_from_vma (filedata, (buckets_vma
11177 + 4 * (filedata->ngnubuckets
11178 + maxchain)), 4)),
11179 SEEK_SET))
11180 {
11181 error (_("Unable to seek to start of dynamic information\n"));
11182 goto no_gnu_hash;
11183 }
11184
11185 filedata->mipsxlat = get_dynamic_data (filedata, maxchain, 4);
11186 if (filedata->mipsxlat == NULL)
11187 goto no_gnu_hash;
11188 }
11189
11190 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
11191 if (filedata->gnubuckets[hn] != 0)
11192 {
11193 bfd_vma si = filedata->gnubuckets[hn];
11194 bfd_vma off = si - filedata->gnusymidx;
11195
11196 do
11197 {
11198 if (filedata->dynamic_info_DT_MIPS_XHASH)
11199 {
11200 if (off < filedata->ngnuchains
11201 && filedata->mipsxlat[off] >= num_of_syms)
11202 num_of_syms = filedata->mipsxlat[off] + 1;
11203 }
11204 else
11205 {
11206 if (si >= num_of_syms)
11207 num_of_syms = si + 1;
11208 }
11209 si++;
11210 }
11211 while (off < filedata->ngnuchains
11212 && (filedata->gnuchains[off++] & 1) == 0);
11213 }
11214
11215 if (num_of_syms == 0)
11216 {
11217 no_gnu_hash:
11218 free (filedata->mipsxlat);
11219 filedata->mipsxlat = NULL;
11220 free (filedata->gnuchains);
11221 filedata->gnuchains = NULL;
11222 free (filedata->gnubuckets);
11223 filedata->gnubuckets = NULL;
11224 filedata->ngnubuckets = 0;
11225 filedata->ngnuchains = 0;
11226 }
11227 }
11228
11229 return num_of_syms;
11230 }
11231
11232 /* Parse and display the contents of the dynamic section. */
11233
11234 static bool
11235 process_dynamic_section (Filedata * filedata)
11236 {
11237 Elf_Internal_Dyn * entry;
11238
11239 if (filedata->dynamic_size <= 1)
11240 {
11241 if (do_dynamic)
11242 {
11243 if (filedata->is_separate)
11244 printf (_("\nThere is no dynamic section in linked file '%s'.\n"),
11245 filedata->file_name);
11246 else
11247 printf (_("\nThere is no dynamic section in this file.\n"));
11248 }
11249
11250 return true;
11251 }
11252
11253 if (!get_dynamic_section (filedata))
11254 return false;
11255
11256 /* Find the appropriate symbol table. */
11257 if (filedata->dynamic_symbols == NULL || do_histogram)
11258 {
11259 unsigned long num_of_syms;
11260
11261 for (entry = filedata->dynamic_section;
11262 entry < filedata->dynamic_section + filedata->dynamic_nent;
11263 ++entry)
11264 if (entry->d_tag == DT_SYMTAB)
11265 filedata->dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
11266 else if (entry->d_tag == DT_SYMENT)
11267 filedata->dynamic_info[DT_SYMENT] = entry->d_un.d_val;
11268 else if (entry->d_tag == DT_HASH)
11269 filedata->dynamic_info[DT_HASH] = entry->d_un.d_val;
11270 else if (entry->d_tag == DT_GNU_HASH)
11271 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11272 else if ((filedata->file_header.e_machine == EM_MIPS
11273 || filedata->file_header.e_machine == EM_MIPS_RS3_LE)
11274 && entry->d_tag == DT_MIPS_XHASH)
11275 {
11276 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
11277 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11278 }
11279
11280 num_of_syms = get_num_dynamic_syms (filedata);
11281
11282 if (num_of_syms != 0
11283 && filedata->dynamic_symbols == NULL
11284 && filedata->dynamic_info[DT_SYMTAB]
11285 && filedata->dynamic_info[DT_SYMENT])
11286 {
11287 Elf_Internal_Phdr *seg;
11288 bfd_vma vma = filedata->dynamic_info[DT_SYMTAB];
11289
11290 if (! get_program_headers (filedata))
11291 {
11292 error (_("Cannot interpret virtual addresses "
11293 "without program headers.\n"));
11294 return false;
11295 }
11296
11297 for (seg = filedata->program_headers;
11298 seg < filedata->program_headers + filedata->file_header.e_phnum;
11299 ++seg)
11300 {
11301 if (seg->p_type != PT_LOAD)
11302 continue;
11303
11304 if (seg->p_offset + seg->p_filesz > filedata->file_size)
11305 {
11306 /* See PR 21379 for a reproducer. */
11307 error (_("Invalid PT_LOAD entry\n"));
11308 return false;
11309 }
11310
11311 if (vma >= (seg->p_vaddr & -seg->p_align)
11312 && vma < seg->p_vaddr + seg->p_filesz)
11313 {
11314 /* Since we do not know how big the symbol table is,
11315 we default to reading in up to the end of PT_LOAD
11316 segment and processing that. This is overkill, I
11317 know, but it should work. */
11318 Elf_Internal_Shdr section;
11319 section.sh_offset = (vma - seg->p_vaddr
11320 + seg->p_offset);
11321 section.sh_size = (num_of_syms
11322 * filedata->dynamic_info[DT_SYMENT]);
11323 section.sh_entsize = filedata->dynamic_info[DT_SYMENT];
11324
11325 if (do_checks
11326 && filedata->dynamic_symtab_section != NULL
11327 && ((filedata->dynamic_symtab_section->sh_offset
11328 != section.sh_offset)
11329 || (filedata->dynamic_symtab_section->sh_size
11330 != section.sh_size)
11331 || (filedata->dynamic_symtab_section->sh_entsize
11332 != section.sh_entsize)))
11333 warn (_("\
11334 the .dynsym section doesn't match the DT_SYMTAB and DT_SYMENT tags\n"));
11335
11336 section.sh_name = filedata->string_table_length;
11337 filedata->dynamic_symbols
11338 = get_elf_symbols (filedata, &section,
11339 &filedata->num_dynamic_syms);
11340 if (filedata->dynamic_symbols == NULL
11341 || filedata->num_dynamic_syms != num_of_syms)
11342 {
11343 error (_("Corrupt DT_SYMTAB dynamic entry\n"));
11344 return false;
11345 }
11346 break;
11347 }
11348 }
11349 }
11350 }
11351
11352 /* Similarly find a string table. */
11353 if (filedata->dynamic_strings == NULL)
11354 for (entry = filedata->dynamic_section;
11355 entry < filedata->dynamic_section + filedata->dynamic_nent;
11356 ++entry)
11357 {
11358 if (entry->d_tag == DT_STRTAB)
11359 filedata->dynamic_info[DT_STRTAB] = entry->d_un.d_val;
11360
11361 if (entry->d_tag == DT_STRSZ)
11362 filedata->dynamic_info[DT_STRSZ] = entry->d_un.d_val;
11363
11364 if (filedata->dynamic_info[DT_STRTAB]
11365 && filedata->dynamic_info[DT_STRSZ])
11366 {
11367 unsigned long offset;
11368 bfd_size_type str_tab_len = filedata->dynamic_info[DT_STRSZ];
11369
11370 offset = offset_from_vma (filedata,
11371 filedata->dynamic_info[DT_STRTAB],
11372 str_tab_len);
11373 if (do_checks
11374 && filedata->dynamic_strtab_section
11375 && ((filedata->dynamic_strtab_section->sh_offset
11376 != (file_ptr) offset)
11377 || (filedata->dynamic_strtab_section->sh_size
11378 != str_tab_len)))
11379 warn (_("\
11380 the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
11381
11382 filedata->dynamic_strings
11383 = (char *) get_data (NULL, filedata, offset, 1, str_tab_len,
11384 _("dynamic string table"));
11385 if (filedata->dynamic_strings == NULL)
11386 {
11387 error (_("Corrupt DT_STRTAB dynamic entry\n"));
11388 break;
11389 }
11390
11391 filedata->dynamic_strings_length = str_tab_len;
11392 break;
11393 }
11394 }
11395
11396 /* And find the syminfo section if available. */
11397 if (filedata->dynamic_syminfo == NULL)
11398 {
11399 unsigned long syminsz = 0;
11400
11401 for (entry = filedata->dynamic_section;
11402 entry < filedata->dynamic_section + filedata->dynamic_nent;
11403 ++entry)
11404 {
11405 if (entry->d_tag == DT_SYMINENT)
11406 {
11407 /* Note: these braces are necessary to avoid a syntax
11408 error from the SunOS4 C compiler. */
11409 /* PR binutils/17531: A corrupt file can trigger this test.
11410 So do not use an assert, instead generate an error message. */
11411 if (sizeof (Elf_External_Syminfo) != entry->d_un.d_val)
11412 error (_("Bad value (%d) for SYMINENT entry\n"),
11413 (int) entry->d_un.d_val);
11414 }
11415 else if (entry->d_tag == DT_SYMINSZ)
11416 syminsz = entry->d_un.d_val;
11417 else if (entry->d_tag == DT_SYMINFO)
11418 filedata->dynamic_syminfo_offset
11419 = offset_from_vma (filedata, entry->d_un.d_val, syminsz);
11420 }
11421
11422 if (filedata->dynamic_syminfo_offset != 0 && syminsz != 0)
11423 {
11424 Elf_External_Syminfo * extsyminfo;
11425 Elf_External_Syminfo * extsym;
11426 Elf_Internal_Syminfo * syminfo;
11427
11428 /* There is a syminfo section. Read the data. */
11429 extsyminfo = (Elf_External_Syminfo *)
11430 get_data (NULL, filedata, filedata->dynamic_syminfo_offset,
11431 1, syminsz, _("symbol information"));
11432 if (!extsyminfo)
11433 return false;
11434
11435 if (filedata->dynamic_syminfo != NULL)
11436 {
11437 error (_("Multiple dynamic symbol information sections found\n"));
11438 free (filedata->dynamic_syminfo);
11439 }
11440 filedata->dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
11441 if (filedata->dynamic_syminfo == NULL)
11442 {
11443 error (_("Out of memory allocating %lu bytes "
11444 "for dynamic symbol info\n"),
11445 (unsigned long) syminsz);
11446 return false;
11447 }
11448
11449 filedata->dynamic_syminfo_nent
11450 = syminsz / sizeof (Elf_External_Syminfo);
11451 for (syminfo = filedata->dynamic_syminfo, extsym = extsyminfo;
11452 syminfo < (filedata->dynamic_syminfo
11453 + filedata->dynamic_syminfo_nent);
11454 ++syminfo, ++extsym)
11455 {
11456 syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
11457 syminfo->si_flags = BYTE_GET (extsym->si_flags);
11458 }
11459
11460 free (extsyminfo);
11461 }
11462 }
11463
11464 if (do_dynamic && filedata->dynamic_addr)
11465 {
11466 if (filedata->is_separate)
11467 printf (ngettext ("\nIn linked file '%s' the dynamic section at offset 0x%lx contains %lu entry:\n",
11468 "\nIn linked file '%s' the dynamic section at offset 0x%lx contains %lu entries:\n",
11469 (unsigned long) filedata->dynamic_nent),
11470 filedata->file_name,
11471 filedata->dynamic_addr,
11472 (unsigned long) filedata->dynamic_nent);
11473 else
11474 printf (ngettext ("\nDynamic section at offset 0x%lx contains %lu entry:\n",
11475 "\nDynamic section at offset 0x%lx contains %lu entries:\n",
11476 (unsigned long) filedata->dynamic_nent),
11477 filedata->dynamic_addr,
11478 (unsigned long) filedata->dynamic_nent);
11479 }
11480 if (do_dynamic)
11481 printf (_(" Tag Type Name/Value\n"));
11482
11483 for (entry = filedata->dynamic_section;
11484 entry < filedata->dynamic_section + filedata->dynamic_nent;
11485 entry++)
11486 {
11487 if (do_dynamic)
11488 {
11489 const char * dtype;
11490
11491 putchar (' ');
11492 print_vma (entry->d_tag, FULL_HEX);
11493 dtype = get_dynamic_type (filedata, entry->d_tag);
11494 printf (" (%s)%*s", dtype,
11495 ((is_32bit_elf ? 27 : 19) - (int) strlen (dtype)), " ");
11496 }
11497
11498 switch (entry->d_tag)
11499 {
11500 case DT_FLAGS:
11501 if (do_dynamic)
11502 print_dynamic_flags (entry->d_un.d_val);
11503 break;
11504
11505 case DT_AUXILIARY:
11506 case DT_FILTER:
11507 case DT_CONFIG:
11508 case DT_DEPAUDIT:
11509 case DT_AUDIT:
11510 if (do_dynamic)
11511 {
11512 switch (entry->d_tag)
11513 {
11514 case DT_AUXILIARY:
11515 printf (_("Auxiliary library"));
11516 break;
11517
11518 case DT_FILTER:
11519 printf (_("Filter library"));
11520 break;
11521
11522 case DT_CONFIG:
11523 printf (_("Configuration file"));
11524 break;
11525
11526 case DT_DEPAUDIT:
11527 printf (_("Dependency audit library"));
11528 break;
11529
11530 case DT_AUDIT:
11531 printf (_("Audit library"));
11532 break;
11533 }
11534
11535 if (valid_dynamic_name (filedata, entry->d_un.d_val))
11536 printf (": [%s]\n",
11537 get_dynamic_name (filedata, entry->d_un.d_val));
11538 else
11539 {
11540 printf (": ");
11541 print_vma (entry->d_un.d_val, PREFIX_HEX);
11542 putchar ('\n');
11543 }
11544 }
11545 break;
11546
11547 case DT_FEATURE:
11548 if (do_dynamic)
11549 {
11550 printf (_("Flags:"));
11551
11552 if (entry->d_un.d_val == 0)
11553 printf (_(" None\n"));
11554 else
11555 {
11556 unsigned long int val = entry->d_un.d_val;
11557
11558 if (val & DTF_1_PARINIT)
11559 {
11560 printf (" PARINIT");
11561 val ^= DTF_1_PARINIT;
11562 }
11563 if (val & DTF_1_CONFEXP)
11564 {
11565 printf (" CONFEXP");
11566 val ^= DTF_1_CONFEXP;
11567 }
11568 if (val != 0)
11569 printf (" %lx", val);
11570 puts ("");
11571 }
11572 }
11573 break;
11574
11575 case DT_POSFLAG_1:
11576 if (do_dynamic)
11577 {
11578 printf (_("Flags:"));
11579
11580 if (entry->d_un.d_val == 0)
11581 printf (_(" None\n"));
11582 else
11583 {
11584 unsigned long int val = entry->d_un.d_val;
11585
11586 if (val & DF_P1_LAZYLOAD)
11587 {
11588 printf (" LAZYLOAD");
11589 val ^= DF_P1_LAZYLOAD;
11590 }
11591 if (val & DF_P1_GROUPPERM)
11592 {
11593 printf (" GROUPPERM");
11594 val ^= DF_P1_GROUPPERM;
11595 }
11596 if (val != 0)
11597 printf (" %lx", val);
11598 puts ("");
11599 }
11600 }
11601 break;
11602
11603 case DT_FLAGS_1:
11604 if (do_dynamic)
11605 {
11606 printf (_("Flags:"));
11607 if (entry->d_un.d_val == 0)
11608 printf (_(" None\n"));
11609 else
11610 {
11611 unsigned long int val = entry->d_un.d_val;
11612
11613 if (val & DF_1_NOW)
11614 {
11615 printf (" NOW");
11616 val ^= DF_1_NOW;
11617 }
11618 if (val & DF_1_GLOBAL)
11619 {
11620 printf (" GLOBAL");
11621 val ^= DF_1_GLOBAL;
11622 }
11623 if (val & DF_1_GROUP)
11624 {
11625 printf (" GROUP");
11626 val ^= DF_1_GROUP;
11627 }
11628 if (val & DF_1_NODELETE)
11629 {
11630 printf (" NODELETE");
11631 val ^= DF_1_NODELETE;
11632 }
11633 if (val & DF_1_LOADFLTR)
11634 {
11635 printf (" LOADFLTR");
11636 val ^= DF_1_LOADFLTR;
11637 }
11638 if (val & DF_1_INITFIRST)
11639 {
11640 printf (" INITFIRST");
11641 val ^= DF_1_INITFIRST;
11642 }
11643 if (val & DF_1_NOOPEN)
11644 {
11645 printf (" NOOPEN");
11646 val ^= DF_1_NOOPEN;
11647 }
11648 if (val & DF_1_ORIGIN)
11649 {
11650 printf (" ORIGIN");
11651 val ^= DF_1_ORIGIN;
11652 }
11653 if (val & DF_1_DIRECT)
11654 {
11655 printf (" DIRECT");
11656 val ^= DF_1_DIRECT;
11657 }
11658 if (val & DF_1_TRANS)
11659 {
11660 printf (" TRANS");
11661 val ^= DF_1_TRANS;
11662 }
11663 if (val & DF_1_INTERPOSE)
11664 {
11665 printf (" INTERPOSE");
11666 val ^= DF_1_INTERPOSE;
11667 }
11668 if (val & DF_1_NODEFLIB)
11669 {
11670 printf (" NODEFLIB");
11671 val ^= DF_1_NODEFLIB;
11672 }
11673 if (val & DF_1_NODUMP)
11674 {
11675 printf (" NODUMP");
11676 val ^= DF_1_NODUMP;
11677 }
11678 if (val & DF_1_CONFALT)
11679 {
11680 printf (" CONFALT");
11681 val ^= DF_1_CONFALT;
11682 }
11683 if (val & DF_1_ENDFILTEE)
11684 {
11685 printf (" ENDFILTEE");
11686 val ^= DF_1_ENDFILTEE;
11687 }
11688 if (val & DF_1_DISPRELDNE)
11689 {
11690 printf (" DISPRELDNE");
11691 val ^= DF_1_DISPRELDNE;
11692 }
11693 if (val & DF_1_DISPRELPND)
11694 {
11695 printf (" DISPRELPND");
11696 val ^= DF_1_DISPRELPND;
11697 }
11698 if (val & DF_1_NODIRECT)
11699 {
11700 printf (" NODIRECT");
11701 val ^= DF_1_NODIRECT;
11702 }
11703 if (val & DF_1_IGNMULDEF)
11704 {
11705 printf (" IGNMULDEF");
11706 val ^= DF_1_IGNMULDEF;
11707 }
11708 if (val & DF_1_NOKSYMS)
11709 {
11710 printf (" NOKSYMS");
11711 val ^= DF_1_NOKSYMS;
11712 }
11713 if (val & DF_1_NOHDR)
11714 {
11715 printf (" NOHDR");
11716 val ^= DF_1_NOHDR;
11717 }
11718 if (val & DF_1_EDITED)
11719 {
11720 printf (" EDITED");
11721 val ^= DF_1_EDITED;
11722 }
11723 if (val & DF_1_NORELOC)
11724 {
11725 printf (" NORELOC");
11726 val ^= DF_1_NORELOC;
11727 }
11728 if (val & DF_1_SYMINTPOSE)
11729 {
11730 printf (" SYMINTPOSE");
11731 val ^= DF_1_SYMINTPOSE;
11732 }
11733 if (val & DF_1_GLOBAUDIT)
11734 {
11735 printf (" GLOBAUDIT");
11736 val ^= DF_1_GLOBAUDIT;
11737 }
11738 if (val & DF_1_SINGLETON)
11739 {
11740 printf (" SINGLETON");
11741 val ^= DF_1_SINGLETON;
11742 }
11743 if (val & DF_1_STUB)
11744 {
11745 printf (" STUB");
11746 val ^= DF_1_STUB;
11747 }
11748 if (val & DF_1_PIE)
11749 {
11750 printf (" PIE");
11751 val ^= DF_1_PIE;
11752 }
11753 if (val & DF_1_KMOD)
11754 {
11755 printf (" KMOD");
11756 val ^= DF_1_KMOD;
11757 }
11758 if (val & DF_1_WEAKFILTER)
11759 {
11760 printf (" WEAKFILTER");
11761 val ^= DF_1_WEAKFILTER;
11762 }
11763 if (val & DF_1_NOCOMMON)
11764 {
11765 printf (" NOCOMMON");
11766 val ^= DF_1_NOCOMMON;
11767 }
11768 if (val != 0)
11769 printf (" %lx", val);
11770 puts ("");
11771 }
11772 }
11773 break;
11774
11775 case DT_PLTREL:
11776 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11777 if (do_dynamic)
11778 puts (get_dynamic_type (filedata, entry->d_un.d_val));
11779 break;
11780
11781 case DT_NULL :
11782 case DT_NEEDED :
11783 case DT_PLTGOT :
11784 case DT_HASH :
11785 case DT_STRTAB :
11786 case DT_SYMTAB :
11787 case DT_RELA :
11788 case DT_INIT :
11789 case DT_FINI :
11790 case DT_SONAME :
11791 case DT_RPATH :
11792 case DT_SYMBOLIC:
11793 case DT_REL :
11794 case DT_RELR :
11795 case DT_DEBUG :
11796 case DT_TEXTREL :
11797 case DT_JMPREL :
11798 case DT_RUNPATH :
11799 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11800
11801 if (do_dynamic)
11802 {
11803 const char *name;
11804
11805 if (valid_dynamic_name (filedata, entry->d_un.d_val))
11806 name = get_dynamic_name (filedata, entry->d_un.d_val);
11807 else
11808 name = NULL;
11809
11810 if (name)
11811 {
11812 switch (entry->d_tag)
11813 {
11814 case DT_NEEDED:
11815 printf (_("Shared library: [%s]"), name);
11816
11817 if (filedata->program_interpreter
11818 && streq (name, filedata->program_interpreter))
11819 printf (_(" program interpreter"));
11820 break;
11821
11822 case DT_SONAME:
11823 printf (_("Library soname: [%s]"), name);
11824 break;
11825
11826 case DT_RPATH:
11827 printf (_("Library rpath: [%s]"), name);
11828 break;
11829
11830 case DT_RUNPATH:
11831 printf (_("Library runpath: [%s]"), name);
11832 break;
11833
11834 default:
11835 print_vma (entry->d_un.d_val, PREFIX_HEX);
11836 break;
11837 }
11838 }
11839 else
11840 print_vma (entry->d_un.d_val, PREFIX_HEX);
11841
11842 putchar ('\n');
11843 }
11844 break;
11845
11846 case DT_PLTRELSZ:
11847 case DT_RELASZ :
11848 case DT_STRSZ :
11849 case DT_RELSZ :
11850 case DT_RELAENT :
11851 case DT_RELRENT :
11852 case DT_RELRSZ :
11853 case DT_SYMENT :
11854 case DT_RELENT :
11855 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11856 /* Fall through. */
11857 case DT_PLTPADSZ:
11858 case DT_MOVEENT :
11859 case DT_MOVESZ :
11860 case DT_PREINIT_ARRAYSZ:
11861 case DT_INIT_ARRAYSZ:
11862 case DT_FINI_ARRAYSZ:
11863 case DT_GNU_CONFLICTSZ:
11864 case DT_GNU_LIBLISTSZ:
11865 if (do_dynamic)
11866 {
11867 print_vma (entry->d_un.d_val, UNSIGNED);
11868 printf (_(" (bytes)\n"));
11869 }
11870 break;
11871
11872 case DT_VERDEFNUM:
11873 case DT_VERNEEDNUM:
11874 case DT_RELACOUNT:
11875 case DT_RELCOUNT:
11876 if (do_dynamic)
11877 {
11878 print_vma (entry->d_un.d_val, UNSIGNED);
11879 putchar ('\n');
11880 }
11881 break;
11882
11883 case DT_SYMINSZ:
11884 case DT_SYMINENT:
11885 case DT_SYMINFO:
11886 case DT_USED:
11887 case DT_INIT_ARRAY:
11888 case DT_FINI_ARRAY:
11889 if (do_dynamic)
11890 {
11891 if (entry->d_tag == DT_USED
11892 && valid_dynamic_name (filedata, entry->d_un.d_val))
11893 {
11894 const char *name
11895 = get_dynamic_name (filedata, entry->d_un.d_val);
11896
11897 if (*name)
11898 {
11899 printf (_("Not needed object: [%s]\n"), name);
11900 break;
11901 }
11902 }
11903
11904 print_vma (entry->d_un.d_val, PREFIX_HEX);
11905 putchar ('\n');
11906 }
11907 break;
11908
11909 case DT_BIND_NOW:
11910 /* The value of this entry is ignored. */
11911 if (do_dynamic)
11912 putchar ('\n');
11913 break;
11914
11915 case DT_GNU_PRELINKED:
11916 if (do_dynamic)
11917 {
11918 struct tm * tmp;
11919 time_t atime = entry->d_un.d_val;
11920
11921 tmp = gmtime (&atime);
11922 /* PR 17533 file: 041-1244816-0.004. */
11923 if (tmp == NULL)
11924 printf (_("<corrupt time val: %lx"),
11925 (unsigned long) atime);
11926 else
11927 printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
11928 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
11929 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
11930
11931 }
11932 break;
11933
11934 case DT_GNU_HASH:
11935 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11936 if (do_dynamic)
11937 {
11938 print_vma (entry->d_un.d_val, PREFIX_HEX);
11939 putchar ('\n');
11940 }
11941 break;
11942
11943 case DT_GNU_FLAGS_1:
11944 if (do_dynamic)
11945 {
11946 printf (_("Flags:"));
11947 if (entry->d_un.d_val == 0)
11948 printf (_(" None\n"));
11949 else
11950 {
11951 unsigned long int val = entry->d_un.d_val;
11952
11953 if (val & DF_GNU_1_UNIQUE)
11954 {
11955 printf (" UNIQUE");
11956 val ^= DF_GNU_1_UNIQUE;
11957 }
11958 if (val != 0)
11959 printf (" %lx", val);
11960 puts ("");
11961 }
11962 }
11963 break;
11964
11965 default:
11966 if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
11967 filedata->version_info[DT_VERSIONTAGIDX (entry->d_tag)]
11968 = entry->d_un.d_val;
11969
11970 if (do_dynamic)
11971 {
11972 switch (filedata->file_header.e_machine)
11973 {
11974 case EM_AARCH64:
11975 dynamic_section_aarch64_val (entry);
11976 break;
11977 case EM_MIPS:
11978 case EM_MIPS_RS3_LE:
11979 dynamic_section_mips_val (filedata, entry);
11980 break;
11981 case EM_PARISC:
11982 dynamic_section_parisc_val (entry);
11983 break;
11984 case EM_IA_64:
11985 dynamic_section_ia64_val (entry);
11986 break;
11987 default:
11988 print_vma (entry->d_un.d_val, PREFIX_HEX);
11989 putchar ('\n');
11990 }
11991 }
11992 break;
11993 }
11994 }
11995
11996 return true;
11997 }
11998
11999 static char *
12000 get_ver_flags (unsigned int flags)
12001 {
12002 static char buff[128];
12003
12004 buff[0] = 0;
12005
12006 if (flags == 0)
12007 return _("none");
12008
12009 if (flags & VER_FLG_BASE)
12010 strcat (buff, "BASE");
12011
12012 if (flags & VER_FLG_WEAK)
12013 {
12014 if (flags & VER_FLG_BASE)
12015 strcat (buff, " | ");
12016
12017 strcat (buff, "WEAK");
12018 }
12019
12020 if (flags & VER_FLG_INFO)
12021 {
12022 if (flags & (VER_FLG_BASE|VER_FLG_WEAK))
12023 strcat (buff, " | ");
12024
12025 strcat (buff, "INFO");
12026 }
12027
12028 if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
12029 {
12030 if (flags & (VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
12031 strcat (buff, " | ");
12032
12033 strcat (buff, _("<unknown>"));
12034 }
12035
12036 return buff;
12037 }
12038
12039 /* Display the contents of the version sections. */
12040
12041 static bool
12042 process_version_sections (Filedata * filedata)
12043 {
12044 Elf_Internal_Shdr * section;
12045 unsigned i;
12046 bool found = false;
12047
12048 if (! do_version)
12049 return true;
12050
12051 for (i = 0, section = filedata->section_headers;
12052 i < filedata->file_header.e_shnum;
12053 i++, section++)
12054 {
12055 switch (section->sh_type)
12056 {
12057 case SHT_GNU_verdef:
12058 {
12059 Elf_External_Verdef * edefs;
12060 unsigned long idx;
12061 unsigned long cnt;
12062 char * endbuf;
12063
12064 found = true;
12065
12066 if (filedata->is_separate)
12067 printf (ngettext ("\nIn linked file '%s' the version definition section '%s' contains %u entry:\n",
12068 "\nIn linked file '%s' the version definition section '%s' contains %u entries:\n",
12069 section->sh_info),
12070 filedata->file_name,
12071 printable_section_name (filedata, section),
12072 section->sh_info);
12073 else
12074 printf (ngettext ("\nVersion definition section '%s' "
12075 "contains %u entry:\n",
12076 "\nVersion definition section '%s' "
12077 "contains %u entries:\n",
12078 section->sh_info),
12079 printable_section_name (filedata, section),
12080 section->sh_info);
12081
12082 printf (_(" Addr: 0x%016" PRIx64), (uint64_t) section->sh_addr);
12083 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
12084 (unsigned long) section->sh_offset, section->sh_link,
12085 printable_section_name_from_index (filedata, section->sh_link));
12086
12087 edefs = (Elf_External_Verdef *)
12088 get_data (NULL, filedata, section->sh_offset, 1,section->sh_size,
12089 _("version definition section"));
12090 if (!edefs)
12091 break;
12092 endbuf = (char *) edefs + section->sh_size;
12093
12094 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
12095 {
12096 char * vstart;
12097 Elf_External_Verdef * edef;
12098 Elf_Internal_Verdef ent;
12099 Elf_External_Verdaux * eaux;
12100 Elf_Internal_Verdaux aux;
12101 unsigned long isum;
12102 int j;
12103
12104 vstart = ((char *) edefs) + idx;
12105 if (vstart + sizeof (*edef) > endbuf)
12106 break;
12107
12108 edef = (Elf_External_Verdef *) vstart;
12109
12110 ent.vd_version = BYTE_GET (edef->vd_version);
12111 ent.vd_flags = BYTE_GET (edef->vd_flags);
12112 ent.vd_ndx = BYTE_GET (edef->vd_ndx);
12113 ent.vd_cnt = BYTE_GET (edef->vd_cnt);
12114 ent.vd_hash = BYTE_GET (edef->vd_hash);
12115 ent.vd_aux = BYTE_GET (edef->vd_aux);
12116 ent.vd_next = BYTE_GET (edef->vd_next);
12117
12118 printf (_(" %#06lx: Rev: %d Flags: %s"),
12119 idx, ent.vd_version, get_ver_flags (ent.vd_flags));
12120
12121 printf (_(" Index: %d Cnt: %d "),
12122 ent.vd_ndx, ent.vd_cnt);
12123
12124 /* Check for overflow. */
12125 if (ent.vd_aux > (size_t) (endbuf - vstart))
12126 break;
12127
12128 vstart += ent.vd_aux;
12129
12130 if (vstart + sizeof (*eaux) > endbuf)
12131 break;
12132 eaux = (Elf_External_Verdaux *) vstart;
12133
12134 aux.vda_name = BYTE_GET (eaux->vda_name);
12135 aux.vda_next = BYTE_GET (eaux->vda_next);
12136
12137 if (valid_dynamic_name (filedata, aux.vda_name))
12138 printf (_("Name: %s\n"),
12139 get_dynamic_name (filedata, aux.vda_name));
12140 else
12141 printf (_("Name index: %ld\n"), aux.vda_name);
12142
12143 isum = idx + ent.vd_aux;
12144
12145 for (j = 1; j < ent.vd_cnt; j++)
12146 {
12147 if (aux.vda_next < sizeof (*eaux)
12148 && !(j == ent.vd_cnt - 1 && aux.vda_next == 0))
12149 {
12150 warn (_("Invalid vda_next field of %lx\n"),
12151 aux.vda_next);
12152 j = ent.vd_cnt;
12153 break;
12154 }
12155 /* Check for overflow. */
12156 if (aux.vda_next > (size_t) (endbuf - vstart))
12157 break;
12158
12159 isum += aux.vda_next;
12160 vstart += aux.vda_next;
12161
12162 if (vstart + sizeof (*eaux) > endbuf)
12163 break;
12164 eaux = (Elf_External_Verdaux *) vstart;
12165
12166 aux.vda_name = BYTE_GET (eaux->vda_name);
12167 aux.vda_next = BYTE_GET (eaux->vda_next);
12168
12169 if (valid_dynamic_name (filedata, aux.vda_name))
12170 printf (_(" %#06lx: Parent %d: %s\n"),
12171 isum, j,
12172 get_dynamic_name (filedata, aux.vda_name));
12173 else
12174 printf (_(" %#06lx: Parent %d, name index: %ld\n"),
12175 isum, j, aux.vda_name);
12176 }
12177
12178 if (j < ent.vd_cnt)
12179 printf (_(" Version def aux past end of section\n"));
12180
12181 /* PR 17531:
12182 file: id:000001,src:000172+005151,op:splice,rep:2. */
12183 if (ent.vd_next < sizeof (*edef)
12184 && !(cnt == section->sh_info - 1 && ent.vd_next == 0))
12185 {
12186 warn (_("Invalid vd_next field of %lx\n"), ent.vd_next);
12187 cnt = section->sh_info;
12188 break;
12189 }
12190 if (ent.vd_next > (size_t) (endbuf - ((char *) edefs + idx)))
12191 break;
12192
12193 idx += ent.vd_next;
12194 }
12195
12196 if (cnt < section->sh_info)
12197 printf (_(" Version definition past end of section\n"));
12198
12199 free (edefs);
12200 }
12201 break;
12202
12203 case SHT_GNU_verneed:
12204 {
12205 Elf_External_Verneed * eneed;
12206 unsigned long idx;
12207 unsigned long cnt;
12208 char * endbuf;
12209
12210 found = true;
12211
12212 if (filedata->is_separate)
12213 printf (ngettext ("\nIn linked file '%s' the version needs section '%s' contains %u entry:\n",
12214 "\nIn linked file '%s' the version needs section '%s' contains %u entries:\n",
12215 section->sh_info),
12216 filedata->file_name,
12217 printable_section_name (filedata, section),
12218 section->sh_info);
12219 else
12220 printf (ngettext ("\nVersion needs section '%s' "
12221 "contains %u entry:\n",
12222 "\nVersion needs section '%s' "
12223 "contains %u entries:\n",
12224 section->sh_info),
12225 printable_section_name (filedata, section),
12226 section->sh_info);
12227
12228 printf (_(" Addr: 0x%016" PRIx64), (uint64_t) section->sh_addr);
12229 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
12230 (unsigned long) section->sh_offset, section->sh_link,
12231 printable_section_name_from_index (filedata, section->sh_link));
12232
12233 eneed = (Elf_External_Verneed *) get_data (NULL, filedata,
12234 section->sh_offset, 1,
12235 section->sh_size,
12236 _("Version Needs section"));
12237 if (!eneed)
12238 break;
12239 endbuf = (char *) eneed + section->sh_size;
12240
12241 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
12242 {
12243 Elf_External_Verneed * entry;
12244 Elf_Internal_Verneed ent;
12245 unsigned long isum;
12246 int j;
12247 char * vstart;
12248
12249 vstart = ((char *) eneed) + idx;
12250 if (vstart + sizeof (*entry) > endbuf)
12251 break;
12252
12253 entry = (Elf_External_Verneed *) vstart;
12254
12255 ent.vn_version = BYTE_GET (entry->vn_version);
12256 ent.vn_cnt = BYTE_GET (entry->vn_cnt);
12257 ent.vn_file = BYTE_GET (entry->vn_file);
12258 ent.vn_aux = BYTE_GET (entry->vn_aux);
12259 ent.vn_next = BYTE_GET (entry->vn_next);
12260
12261 printf (_(" %#06lx: Version: %d"), idx, ent.vn_version);
12262
12263 if (valid_dynamic_name (filedata, ent.vn_file))
12264 printf (_(" File: %s"),
12265 get_dynamic_name (filedata, ent.vn_file));
12266 else
12267 printf (_(" File: %lx"), ent.vn_file);
12268
12269 printf (_(" Cnt: %d\n"), ent.vn_cnt);
12270
12271 /* Check for overflow. */
12272 if (ent.vn_aux > (size_t) (endbuf - vstart))
12273 break;
12274 vstart += ent.vn_aux;
12275
12276 for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
12277 {
12278 Elf_External_Vernaux * eaux;
12279 Elf_Internal_Vernaux aux;
12280
12281 if (vstart + sizeof (*eaux) > endbuf)
12282 break;
12283 eaux = (Elf_External_Vernaux *) vstart;
12284
12285 aux.vna_hash = BYTE_GET (eaux->vna_hash);
12286 aux.vna_flags = BYTE_GET (eaux->vna_flags);
12287 aux.vna_other = BYTE_GET (eaux->vna_other);
12288 aux.vna_name = BYTE_GET (eaux->vna_name);
12289 aux.vna_next = BYTE_GET (eaux->vna_next);
12290
12291 if (valid_dynamic_name (filedata, aux.vna_name))
12292 printf (_(" %#06lx: Name: %s"),
12293 isum, get_dynamic_name (filedata, aux.vna_name));
12294 else
12295 printf (_(" %#06lx: Name index: %lx"),
12296 isum, aux.vna_name);
12297
12298 printf (_(" Flags: %s Version: %d\n"),
12299 get_ver_flags (aux.vna_flags), aux.vna_other);
12300
12301 if (aux.vna_next < sizeof (*eaux)
12302 && !(j == ent.vn_cnt - 1 && aux.vna_next == 0))
12303 {
12304 warn (_("Invalid vna_next field of %lx\n"),
12305 aux.vna_next);
12306 j = ent.vn_cnt;
12307 break;
12308 }
12309 /* Check for overflow. */
12310 if (aux.vna_next > (size_t) (endbuf - vstart))
12311 break;
12312 isum += aux.vna_next;
12313 vstart += aux.vna_next;
12314 }
12315
12316 if (j < ent.vn_cnt)
12317 warn (_("Missing Version Needs auxiliary information\n"));
12318
12319 if (ent.vn_next < sizeof (*entry)
12320 && !(cnt == section->sh_info - 1 && ent.vn_next == 0))
12321 {
12322 warn (_("Invalid vn_next field of %lx\n"), ent.vn_next);
12323 cnt = section->sh_info;
12324 break;
12325 }
12326 if (ent.vn_next > (size_t) (endbuf - ((char *) eneed + idx)))
12327 break;
12328 idx += ent.vn_next;
12329 }
12330
12331 if (cnt < section->sh_info)
12332 warn (_("Missing Version Needs information\n"));
12333
12334 free (eneed);
12335 }
12336 break;
12337
12338 case SHT_GNU_versym:
12339 {
12340 Elf_Internal_Shdr * link_section;
12341 size_t total;
12342 unsigned int cnt;
12343 unsigned char * edata;
12344 unsigned short * data;
12345 char * strtab;
12346 Elf_Internal_Sym * symbols;
12347 Elf_Internal_Shdr * string_sec;
12348 unsigned long num_syms;
12349 long off;
12350
12351 if (section->sh_link >= filedata->file_header.e_shnum)
12352 break;
12353
12354 link_section = filedata->section_headers + section->sh_link;
12355 total = section->sh_size / sizeof (Elf_External_Versym);
12356
12357 if (link_section->sh_link >= filedata->file_header.e_shnum)
12358 break;
12359
12360 found = true;
12361
12362 symbols = get_elf_symbols (filedata, link_section, & num_syms);
12363 if (symbols == NULL)
12364 break;
12365
12366 string_sec = filedata->section_headers + link_section->sh_link;
12367
12368 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
12369 string_sec->sh_size,
12370 _("version string table"));
12371 if (!strtab)
12372 {
12373 free (symbols);
12374 break;
12375 }
12376
12377 if (filedata->is_separate)
12378 printf (ngettext ("\nIn linked file '%s' the version symbols section '%s' contains %lu entry:\n",
12379 "\nIn linked file '%s' the version symbols section '%s' contains %lu entries:\n",
12380 total),
12381 filedata->file_name,
12382 printable_section_name (filedata, section),
12383 (unsigned long) total);
12384 else
12385 printf (ngettext ("\nVersion symbols section '%s' "
12386 "contains %lu entry:\n",
12387 "\nVersion symbols section '%s' "
12388 "contains %lu entries:\n",
12389 total),
12390 printable_section_name (filedata, section),
12391 (unsigned long) total);
12392
12393 printf (_(" Addr: 0x%016" PRIx64), (uint64_t) section->sh_addr);
12394 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
12395 (unsigned long) section->sh_offset, section->sh_link,
12396 printable_section_name (filedata, link_section));
12397
12398 off = offset_from_vma (filedata,
12399 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
12400 total * sizeof (short));
12401 edata = (unsigned char *) get_data (NULL, filedata, off,
12402 sizeof (short), total,
12403 _("version symbol data"));
12404 if (!edata)
12405 {
12406 free (strtab);
12407 free (symbols);
12408 break;
12409 }
12410
12411 data = (short unsigned int *) cmalloc (total, sizeof (short));
12412
12413 for (cnt = total; cnt --;)
12414 data[cnt] = byte_get (edata + cnt * sizeof (short),
12415 sizeof (short));
12416
12417 free (edata);
12418
12419 for (cnt = 0; cnt < total; cnt += 4)
12420 {
12421 int j, nn;
12422 char *name;
12423 char *invalid = _("*invalid*");
12424
12425 printf (" %03x:", cnt);
12426
12427 for (j = 0; (j < 4) && (cnt + j) < total; ++j)
12428 switch (data[cnt + j])
12429 {
12430 case 0:
12431 fputs (_(" 0 (*local*) "), stdout);
12432 break;
12433
12434 case 1:
12435 fputs (_(" 1 (*global*) "), stdout);
12436 break;
12437
12438 default:
12439 nn = printf ("%4x%c", data[cnt + j] & VERSYM_VERSION,
12440 data[cnt + j] & VERSYM_HIDDEN ? 'h' : ' ');
12441
12442 /* If this index value is greater than the size of the symbols
12443 array, break to avoid an out-of-bounds read. */
12444 if ((unsigned long)(cnt + j) >= num_syms)
12445 {
12446 warn (_("invalid index into symbol array\n"));
12447 break;
12448 }
12449
12450 name = NULL;
12451 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
12452 {
12453 Elf_Internal_Verneed ivn;
12454 unsigned long offset;
12455
12456 offset = offset_from_vma
12457 (filedata,
12458 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
12459 sizeof (Elf_External_Verneed));
12460
12461 do
12462 {
12463 Elf_Internal_Vernaux ivna;
12464 Elf_External_Verneed evn;
12465 Elf_External_Vernaux evna;
12466 unsigned long a_off;
12467
12468 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
12469 _("version need")) == NULL)
12470 break;
12471
12472 ivn.vn_aux = BYTE_GET (evn.vn_aux);
12473 ivn.vn_next = BYTE_GET (evn.vn_next);
12474
12475 a_off = offset + ivn.vn_aux;
12476
12477 do
12478 {
12479 if (get_data (&evna, filedata, a_off, sizeof (evna),
12480 1, _("version need aux (2)")) == NULL)
12481 {
12482 ivna.vna_next = 0;
12483 ivna.vna_other = 0;
12484 }
12485 else
12486 {
12487 ivna.vna_next = BYTE_GET (evna.vna_next);
12488 ivna.vna_other = BYTE_GET (evna.vna_other);
12489 }
12490
12491 a_off += ivna.vna_next;
12492 }
12493 while (ivna.vna_other != data[cnt + j]
12494 && ivna.vna_next != 0);
12495
12496 if (ivna.vna_other == data[cnt + j])
12497 {
12498 ivna.vna_name = BYTE_GET (evna.vna_name);
12499
12500 if (ivna.vna_name >= string_sec->sh_size)
12501 name = invalid;
12502 else
12503 name = strtab + ivna.vna_name;
12504 break;
12505 }
12506
12507 offset += ivn.vn_next;
12508 }
12509 while (ivn.vn_next);
12510 }
12511
12512 if (data[cnt + j] != 0x8001
12513 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
12514 {
12515 Elf_Internal_Verdef ivd;
12516 Elf_External_Verdef evd;
12517 unsigned long offset;
12518
12519 offset = offset_from_vma
12520 (filedata,
12521 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
12522 sizeof evd);
12523
12524 do
12525 {
12526 if (get_data (&evd, filedata, offset, sizeof (evd), 1,
12527 _("version def")) == NULL)
12528 {
12529 ivd.vd_next = 0;
12530 /* PR 17531: file: 046-1082287-0.004. */
12531 ivd.vd_ndx = (data[cnt + j] & VERSYM_VERSION) + 1;
12532 break;
12533 }
12534 else
12535 {
12536 ivd.vd_next = BYTE_GET (evd.vd_next);
12537 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
12538 }
12539
12540 offset += ivd.vd_next;
12541 }
12542 while (ivd.vd_ndx != (data[cnt + j] & VERSYM_VERSION)
12543 && ivd.vd_next != 0);
12544
12545 if (ivd.vd_ndx == (data[cnt + j] & VERSYM_VERSION))
12546 {
12547 Elf_External_Verdaux evda;
12548 Elf_Internal_Verdaux ivda;
12549
12550 ivd.vd_aux = BYTE_GET (evd.vd_aux);
12551
12552 if (get_data (&evda, filedata,
12553 offset - ivd.vd_next + ivd.vd_aux,
12554 sizeof (evda), 1,
12555 _("version def aux")) == NULL)
12556 break;
12557
12558 ivda.vda_name = BYTE_GET (evda.vda_name);
12559
12560 if (ivda.vda_name >= string_sec->sh_size)
12561 name = invalid;
12562 else if (name != NULL && name != invalid)
12563 name = _("*both*");
12564 else
12565 name = strtab + ivda.vda_name;
12566 }
12567 }
12568 if (name != NULL)
12569 nn += printf ("(%s%-*s",
12570 name,
12571 12 - (int) strlen (name),
12572 ")");
12573
12574 if (nn < 18)
12575 printf ("%*c", 18 - nn, ' ');
12576 }
12577
12578 putchar ('\n');
12579 }
12580
12581 free (data);
12582 free (strtab);
12583 free (symbols);
12584 }
12585 break;
12586
12587 default:
12588 break;
12589 }
12590 }
12591
12592 if (! found)
12593 {
12594 if (filedata->is_separate)
12595 printf (_("\nNo version information found in linked file '%s'.\n"),
12596 filedata->file_name);
12597 else
12598 printf (_("\nNo version information found in this file.\n"));
12599 }
12600
12601 return true;
12602 }
12603
12604 static const char *
12605 get_symbol_binding (Filedata * filedata, unsigned int binding)
12606 {
12607 static char buff[64];
12608
12609 switch (binding)
12610 {
12611 case STB_LOCAL: return "LOCAL";
12612 case STB_GLOBAL: return "GLOBAL";
12613 case STB_WEAK: return "WEAK";
12614 default:
12615 if (binding >= STB_LOPROC && binding <= STB_HIPROC)
12616 snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
12617 binding);
12618 else if (binding >= STB_LOOS && binding <= STB_HIOS)
12619 {
12620 if (binding == STB_GNU_UNIQUE
12621 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU)
12622 return "UNIQUE";
12623 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
12624 }
12625 else
12626 snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
12627 return buff;
12628 }
12629 }
12630
12631 static const char *
12632 get_symbol_type (Filedata * filedata, unsigned int type)
12633 {
12634 static char buff[64];
12635
12636 switch (type)
12637 {
12638 case STT_NOTYPE: return "NOTYPE";
12639 case STT_OBJECT: return "OBJECT";
12640 case STT_FUNC: return "FUNC";
12641 case STT_SECTION: return "SECTION";
12642 case STT_FILE: return "FILE";
12643 case STT_COMMON: return "COMMON";
12644 case STT_TLS: return "TLS";
12645 case STT_RELC: return "RELC";
12646 case STT_SRELC: return "SRELC";
12647 default:
12648 if (type >= STT_LOPROC && type <= STT_HIPROC)
12649 {
12650 if (filedata->file_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
12651 return "THUMB_FUNC";
12652
12653 if (filedata->file_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
12654 return "REGISTER";
12655
12656 if (filedata->file_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
12657 return "PARISC_MILLI";
12658
12659 snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
12660 }
12661 else if (type >= STT_LOOS && type <= STT_HIOS)
12662 {
12663 if (filedata->file_header.e_machine == EM_PARISC)
12664 {
12665 if (type == STT_HP_OPAQUE)
12666 return "HP_OPAQUE";
12667 if (type == STT_HP_STUB)
12668 return "HP_STUB";
12669 }
12670
12671 if (type == STT_GNU_IFUNC
12672 && (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU
12673 || filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_FREEBSD))
12674 return "IFUNC";
12675
12676 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
12677 }
12678 else
12679 snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
12680 return buff;
12681 }
12682 }
12683
12684 static const char *
12685 get_symbol_visibility (unsigned int visibility)
12686 {
12687 switch (visibility)
12688 {
12689 case STV_DEFAULT: return "DEFAULT";
12690 case STV_INTERNAL: return "INTERNAL";
12691 case STV_HIDDEN: return "HIDDEN";
12692 case STV_PROTECTED: return "PROTECTED";
12693 default:
12694 error (_("Unrecognized visibility value: %u\n"), visibility);
12695 return _("<unknown>");
12696 }
12697 }
12698
12699 static const char *
12700 get_alpha_symbol_other (unsigned int other)
12701 {
12702 switch (other)
12703 {
12704 case STO_ALPHA_NOPV: return "NOPV";
12705 case STO_ALPHA_STD_GPLOAD: return "STD GPLOAD";
12706 default:
12707 error (_("Unrecognized alpha specific other value: %u\n"), other);
12708 return _("<unknown>");
12709 }
12710 }
12711
12712 static const char *
12713 get_solaris_symbol_visibility (unsigned int visibility)
12714 {
12715 switch (visibility)
12716 {
12717 case 4: return "EXPORTED";
12718 case 5: return "SINGLETON";
12719 case 6: return "ELIMINATE";
12720 default: return get_symbol_visibility (visibility);
12721 }
12722 }
12723
12724 static const char *
12725 get_aarch64_symbol_other (unsigned int other)
12726 {
12727 static char buf[32];
12728
12729 if (other & STO_AARCH64_VARIANT_PCS)
12730 {
12731 other &= ~STO_AARCH64_VARIANT_PCS;
12732 if (other == 0)
12733 return "VARIANT_PCS";
12734 snprintf (buf, sizeof buf, "VARIANT_PCS | %x", other);
12735 return buf;
12736 }
12737 return NULL;
12738 }
12739
12740 static const char *
12741 get_mips_symbol_other (unsigned int other)
12742 {
12743 switch (other)
12744 {
12745 case STO_OPTIONAL: return "OPTIONAL";
12746 case STO_MIPS_PLT: return "MIPS PLT";
12747 case STO_MIPS_PIC: return "MIPS PIC";
12748 case STO_MICROMIPS: return "MICROMIPS";
12749 case STO_MICROMIPS | STO_MIPS_PIC: return "MICROMIPS, MIPS PIC";
12750 case STO_MIPS16: return "MIPS16";
12751 default: return NULL;
12752 }
12753 }
12754
12755 static const char *
12756 get_ia64_symbol_other (Filedata * filedata, unsigned int other)
12757 {
12758 if (is_ia64_vms (filedata))
12759 {
12760 static char res[32];
12761
12762 res[0] = 0;
12763
12764 /* Function types is for images and .STB files only. */
12765 switch (filedata->file_header.e_type)
12766 {
12767 case ET_DYN:
12768 case ET_EXEC:
12769 switch (VMS_ST_FUNC_TYPE (other))
12770 {
12771 case VMS_SFT_CODE_ADDR:
12772 strcat (res, " CA");
12773 break;
12774 case VMS_SFT_SYMV_IDX:
12775 strcat (res, " VEC");
12776 break;
12777 case VMS_SFT_FD:
12778 strcat (res, " FD");
12779 break;
12780 case VMS_SFT_RESERVE:
12781 strcat (res, " RSV");
12782 break;
12783 default:
12784 warn (_("Unrecognized IA64 VMS ST Function type: %d\n"),
12785 VMS_ST_FUNC_TYPE (other));
12786 strcat (res, " <unknown>");
12787 break;
12788 }
12789 break;
12790 default:
12791 break;
12792 }
12793 switch (VMS_ST_LINKAGE (other))
12794 {
12795 case VMS_STL_IGNORE:
12796 strcat (res, " IGN");
12797 break;
12798 case VMS_STL_RESERVE:
12799 strcat (res, " RSV");
12800 break;
12801 case VMS_STL_STD:
12802 strcat (res, " STD");
12803 break;
12804 case VMS_STL_LNK:
12805 strcat (res, " LNK");
12806 break;
12807 default:
12808 warn (_("Unrecognized IA64 VMS ST Linkage: %d\n"),
12809 VMS_ST_LINKAGE (other));
12810 strcat (res, " <unknown>");
12811 break;
12812 }
12813
12814 if (res[0] != 0)
12815 return res + 1;
12816 else
12817 return res;
12818 }
12819 return NULL;
12820 }
12821
12822 static const char *
12823 get_ppc64_symbol_other (unsigned int other)
12824 {
12825 if ((other & ~STO_PPC64_LOCAL_MASK) != 0)
12826 return NULL;
12827
12828 other >>= STO_PPC64_LOCAL_BIT;
12829 if (other <= 6)
12830 {
12831 static char buf[64];
12832 if (other >= 2)
12833 other = ppc64_decode_local_entry (other);
12834 snprintf (buf, sizeof buf, _("<localentry>: %d"), other);
12835 return buf;
12836 }
12837 return NULL;
12838 }
12839
12840 static const char *
12841 get_riscv_symbol_other (unsigned int other)
12842 {
12843 static char buf[32];
12844 buf[0] = 0;
12845
12846 if (other & STO_RISCV_VARIANT_CC)
12847 {
12848 strcat (buf, _(" VARIANT_CC"));
12849 other &= ~STO_RISCV_VARIANT_CC;
12850 }
12851
12852 if (other != 0)
12853 snprintf (buf, sizeof buf, " %x", other);
12854
12855
12856 if (buf[0] != 0)
12857 return buf + 1;
12858 else
12859 return buf;
12860 }
12861
12862 static const char *
12863 get_symbol_other (Filedata * filedata, unsigned int other)
12864 {
12865 const char * result = NULL;
12866 static char buff [64];
12867
12868 if (other == 0)
12869 return "";
12870
12871 switch (filedata->file_header.e_machine)
12872 {
12873 case EM_ALPHA:
12874 result = get_alpha_symbol_other (other);
12875 break;
12876 case EM_AARCH64:
12877 result = get_aarch64_symbol_other (other);
12878 break;
12879 case EM_MIPS:
12880 result = get_mips_symbol_other (other);
12881 break;
12882 case EM_IA_64:
12883 result = get_ia64_symbol_other (filedata, other);
12884 break;
12885 case EM_PPC64:
12886 result = get_ppc64_symbol_other (other);
12887 break;
12888 case EM_RISCV:
12889 result = get_riscv_symbol_other (other);
12890 break;
12891 default:
12892 result = NULL;
12893 break;
12894 }
12895
12896 if (result)
12897 return result;
12898
12899 snprintf (buff, sizeof buff, _("<other>: %x"), other);
12900 return buff;
12901 }
12902
12903 static const char *
12904 get_symbol_index_type (Filedata * filedata, unsigned int type)
12905 {
12906 static char buff[32];
12907
12908 switch (type)
12909 {
12910 case SHN_UNDEF: return "UND";
12911 case SHN_ABS: return "ABS";
12912 case SHN_COMMON: return "COM";
12913 default:
12914 if (type == SHN_IA_64_ANSI_COMMON
12915 && filedata->file_header.e_machine == EM_IA_64
12916 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX)
12917 return "ANSI_COM";
12918 else if ((filedata->file_header.e_machine == EM_X86_64
12919 || filedata->file_header.e_machine == EM_L1OM
12920 || filedata->file_header.e_machine == EM_K1OM)
12921 && type == SHN_X86_64_LCOMMON)
12922 return "LARGE_COM";
12923 else if ((type == SHN_MIPS_SCOMMON
12924 && filedata->file_header.e_machine == EM_MIPS)
12925 || (type == SHN_TIC6X_SCOMMON
12926 && filedata->file_header.e_machine == EM_TI_C6000))
12927 return "SCOM";
12928 else if (type == SHN_MIPS_SUNDEFINED
12929 && filedata->file_header.e_machine == EM_MIPS)
12930 return "SUND";
12931 else if (type >= SHN_LOPROC && type <= SHN_HIPROC)
12932 sprintf (buff, "PRC[0x%04x]", type & 0xffff);
12933 else if (type >= SHN_LOOS && type <= SHN_HIOS)
12934 sprintf (buff, "OS [0x%04x]", type & 0xffff);
12935 else if (type >= SHN_LORESERVE)
12936 sprintf (buff, "RSV[0x%04x]", type & 0xffff);
12937 else if (filedata->file_header.e_shnum != 0
12938 && type >= filedata->file_header.e_shnum)
12939 sprintf (buff, _("bad section index[%3d]"), type);
12940 else
12941 sprintf (buff, "%3d", type);
12942 break;
12943 }
12944
12945 return buff;
12946 }
12947
12948 static const char *
12949 get_symbol_version_string (Filedata * filedata,
12950 bool is_dynsym,
12951 const char * strtab,
12952 unsigned long int strtab_size,
12953 unsigned int si,
12954 Elf_Internal_Sym * psym,
12955 enum versioned_symbol_info * sym_info,
12956 unsigned short * vna_other)
12957 {
12958 unsigned char data[2];
12959 unsigned short vers_data;
12960 unsigned long offset;
12961 unsigned short max_vd_ndx;
12962
12963 if (!is_dynsym
12964 || filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)] == 0)
12965 return NULL;
12966
12967 offset = offset_from_vma (filedata,
12968 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
12969 sizeof data + si * sizeof (vers_data));
12970
12971 if (get_data (&data, filedata, offset + si * sizeof (vers_data),
12972 sizeof (data), 1, _("version data")) == NULL)
12973 return NULL;
12974
12975 vers_data = byte_get (data, 2);
12976
12977 if ((vers_data & VERSYM_HIDDEN) == 0 && vers_data == 0)
12978 return NULL;
12979
12980 *sym_info = (vers_data & VERSYM_HIDDEN) != 0 ? symbol_hidden : symbol_public;
12981 max_vd_ndx = 0;
12982
12983 /* Usually we'd only see verdef for defined symbols, and verneed for
12984 undefined symbols. However, symbols defined by the linker in
12985 .dynbss for variables copied from a shared library in order to
12986 avoid text relocations are defined yet have verneed. We could
12987 use a heuristic to detect the special case, for example, check
12988 for verneed first on symbols defined in SHT_NOBITS sections, but
12989 it is simpler and more reliable to just look for both verdef and
12990 verneed. .dynbss might not be mapped to a SHT_NOBITS section. */
12991
12992 if (psym->st_shndx != SHN_UNDEF
12993 && vers_data != 0x8001
12994 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
12995 {
12996 Elf_Internal_Verdef ivd;
12997 Elf_Internal_Verdaux ivda;
12998 Elf_External_Verdaux evda;
12999 unsigned long off;
13000
13001 off = offset_from_vma (filedata,
13002 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
13003 sizeof (Elf_External_Verdef));
13004
13005 do
13006 {
13007 Elf_External_Verdef evd;
13008
13009 if (get_data (&evd, filedata, off, sizeof (evd), 1,
13010 _("version def")) == NULL)
13011 {
13012 ivd.vd_ndx = 0;
13013 ivd.vd_aux = 0;
13014 ivd.vd_next = 0;
13015 ivd.vd_flags = 0;
13016 }
13017 else
13018 {
13019 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
13020 ivd.vd_aux = BYTE_GET (evd.vd_aux);
13021 ivd.vd_next = BYTE_GET (evd.vd_next);
13022 ivd.vd_flags = BYTE_GET (evd.vd_flags);
13023 }
13024
13025 if ((ivd.vd_ndx & VERSYM_VERSION) > max_vd_ndx)
13026 max_vd_ndx = ivd.vd_ndx & VERSYM_VERSION;
13027
13028 off += ivd.vd_next;
13029 }
13030 while (ivd.vd_ndx != (vers_data & VERSYM_VERSION) && ivd.vd_next != 0);
13031
13032 if (ivd.vd_ndx == (vers_data & VERSYM_VERSION))
13033 {
13034 if (ivd.vd_ndx == 1 && ivd.vd_flags == VER_FLG_BASE)
13035 return NULL;
13036
13037 off -= ivd.vd_next;
13038 off += ivd.vd_aux;
13039
13040 if (get_data (&evda, filedata, off, sizeof (evda), 1,
13041 _("version def aux")) != NULL)
13042 {
13043 ivda.vda_name = BYTE_GET (evda.vda_name);
13044
13045 if (psym->st_name != ivda.vda_name)
13046 return (ivda.vda_name < strtab_size
13047 ? strtab + ivda.vda_name : _("<corrupt>"));
13048 }
13049 }
13050 }
13051
13052 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
13053 {
13054 Elf_External_Verneed evn;
13055 Elf_Internal_Verneed ivn;
13056 Elf_Internal_Vernaux ivna;
13057
13058 offset = offset_from_vma (filedata,
13059 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
13060 sizeof evn);
13061 do
13062 {
13063 unsigned long vna_off;
13064
13065 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
13066 _("version need")) == NULL)
13067 {
13068 ivna.vna_next = 0;
13069 ivna.vna_other = 0;
13070 ivna.vna_name = 0;
13071 break;
13072 }
13073
13074 ivn.vn_aux = BYTE_GET (evn.vn_aux);
13075 ivn.vn_next = BYTE_GET (evn.vn_next);
13076
13077 vna_off = offset + ivn.vn_aux;
13078
13079 do
13080 {
13081 Elf_External_Vernaux evna;
13082
13083 if (get_data (&evna, filedata, vna_off, sizeof (evna), 1,
13084 _("version need aux (3)")) == NULL)
13085 {
13086 ivna.vna_next = 0;
13087 ivna.vna_other = 0;
13088 ivna.vna_name = 0;
13089 }
13090 else
13091 {
13092 ivna.vna_other = BYTE_GET (evna.vna_other);
13093 ivna.vna_next = BYTE_GET (evna.vna_next);
13094 ivna.vna_name = BYTE_GET (evna.vna_name);
13095 }
13096
13097 vna_off += ivna.vna_next;
13098 }
13099 while (ivna.vna_other != vers_data && ivna.vna_next != 0);
13100
13101 if (ivna.vna_other == vers_data)
13102 break;
13103
13104 offset += ivn.vn_next;
13105 }
13106 while (ivn.vn_next != 0);
13107
13108 if (ivna.vna_other == vers_data)
13109 {
13110 *sym_info = symbol_undefined;
13111 *vna_other = ivna.vna_other;
13112 return (ivna.vna_name < strtab_size
13113 ? strtab + ivna.vna_name : _("<corrupt>"));
13114 }
13115 else if ((max_vd_ndx || (vers_data & VERSYM_VERSION) != 1)
13116 && (vers_data & VERSYM_VERSION) > max_vd_ndx)
13117 return _("<corrupt>");
13118 }
13119 return NULL;
13120 }
13121
13122 /* Display a symbol size on stdout. Format is based on --sym-base setting. */
13123
13124 static unsigned int
13125 print_dynamic_symbol_size (bfd_vma vma, int base)
13126 {
13127 switch (base)
13128 {
13129 case 8:
13130 return print_vma (vma, OCTAL_5);
13131
13132 case 10:
13133 return print_vma (vma, UNSIGNED_5);
13134
13135 case 16:
13136 return print_vma (vma, PREFIX_HEX_5);
13137
13138 case 0:
13139 default:
13140 return print_vma (vma, DEC_5);
13141 }
13142 }
13143
13144 static void
13145 print_dynamic_symbol (Filedata *filedata, unsigned long si,
13146 Elf_Internal_Sym *symtab,
13147 Elf_Internal_Shdr *section,
13148 char *strtab, size_t strtab_size)
13149 {
13150 const char *version_string;
13151 enum versioned_symbol_info sym_info;
13152 unsigned short vna_other;
13153 bool is_valid;
13154 const char * sstr;
13155 Elf_Internal_Sym *psym = symtab + si;
13156
13157 printf ("%6ld: ", si);
13158 print_vma (psym->st_value, LONG_HEX);
13159 putchar (' ');
13160 print_dynamic_symbol_size (psym->st_size, sym_base);
13161 printf (" %-7s", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
13162 printf (" %-6s", get_symbol_binding (filedata, ELF_ST_BIND (psym->st_info)));
13163 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
13164 printf (" %-7s", get_solaris_symbol_visibility (psym->st_other));
13165 else
13166 {
13167 unsigned int vis = ELF_ST_VISIBILITY (psym->st_other);
13168
13169 printf (" %-7s", get_symbol_visibility (vis));
13170 /* Check to see if any other bits in the st_other field are set.
13171 Note - displaying this information disrupts the layout of the
13172 table being generated, but for the moment this case is very rare. */
13173 if (psym->st_other ^ vis)
13174 printf (" [%s] ", get_symbol_other (filedata, psym->st_other ^ vis));
13175 }
13176 printf (" %4s ", get_symbol_index_type (filedata, psym->st_shndx));
13177
13178 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION
13179 && psym->st_shndx < filedata->file_header.e_shnum
13180 && filedata->section_headers != NULL
13181 && psym->st_name == 0)
13182 {
13183 is_valid
13184 = section_name_valid (filedata,
13185 filedata->section_headers + psym->st_shndx);
13186 sstr = is_valid ?
13187 section_name_print (filedata,
13188 filedata->section_headers + psym->st_shndx)
13189 : _("<corrupt>");
13190 }
13191 else
13192 {
13193 is_valid = valid_symbol_name (strtab, strtab_size, psym->st_name);
13194 sstr = is_valid ? strtab + psym->st_name : _("<corrupt>");
13195 }
13196
13197 version_string
13198 = get_symbol_version_string (filedata,
13199 (section == NULL
13200 || section->sh_type == SHT_DYNSYM),
13201 strtab, strtab_size, si,
13202 psym, &sym_info, &vna_other);
13203
13204 int len_avail = 21;
13205 if (! do_wide && version_string != NULL)
13206 {
13207 char buffer[16];
13208
13209 len_avail -= 1 + strlen (version_string);
13210
13211 if (sym_info == symbol_undefined)
13212 len_avail -= sprintf (buffer," (%d)", vna_other);
13213 else if (sym_info != symbol_hidden)
13214 len_avail -= 1;
13215 }
13216
13217 print_symbol (len_avail, sstr);
13218
13219 if (version_string)
13220 {
13221 if (sym_info == symbol_undefined)
13222 printf ("@%s (%d)", version_string, vna_other);
13223 else
13224 printf (sym_info == symbol_hidden ? "@%s" : "@@%s",
13225 version_string);
13226 }
13227
13228 putchar ('\n');
13229
13230 if (ELF_ST_BIND (psym->st_info) == STB_LOCAL
13231 && section != NULL
13232 && si >= section->sh_info
13233 /* Irix 5 and 6 MIPS binaries are known to ignore this requirement. */
13234 && filedata->file_header.e_machine != EM_MIPS
13235 /* Solaris binaries have been found to violate this requirement as
13236 well. Not sure if this is a bug or an ABI requirement. */
13237 && filedata->file_header.e_ident[EI_OSABI] != ELFOSABI_SOLARIS)
13238 warn (_("local symbol %lu found at index >= %s's sh_info value of %u\n"),
13239 si, printable_section_name (filedata, section), section->sh_info);
13240 }
13241
13242 static const char *
13243 get_lto_kind (unsigned int kind)
13244 {
13245 switch (kind)
13246 {
13247 case 0: return "DEF";
13248 case 1: return "WEAKDEF";
13249 case 2: return "UNDEF";
13250 case 3: return "WEAKUNDEF";
13251 case 4: return "COMMON";
13252 default:
13253 break;
13254 }
13255
13256 static char buffer[30];
13257 error (_("Unknown LTO symbol definition encountered: %u\n"), kind);
13258 sprintf (buffer, "<unknown: %u>", kind);
13259 return buffer;
13260 }
13261
13262 static const char *
13263 get_lto_visibility (unsigned int visibility)
13264 {
13265 switch (visibility)
13266 {
13267 case 0: return "DEFAULT";
13268 case 1: return "PROTECTED";
13269 case 2: return "INTERNAL";
13270 case 3: return "HIDDEN";
13271 default:
13272 break;
13273 }
13274
13275 static char buffer[30];
13276 error (_("Unknown LTO symbol visibility encountered: %u\n"), visibility);
13277 sprintf (buffer, "<unknown: %u>", visibility);
13278 return buffer;
13279 }
13280
13281 static const char *
13282 get_lto_sym_type (unsigned int sym_type)
13283 {
13284 switch (sym_type)
13285 {
13286 case 0: return "UNKNOWN";
13287 case 1: return "FUNCTION";
13288 case 2: return "VARIABLE";
13289 default:
13290 break;
13291 }
13292
13293 static char buffer[30];
13294 error (_("Unknown LTO symbol type encountered: %u\n"), sym_type);
13295 sprintf (buffer, "<unknown: %u>", sym_type);
13296 return buffer;
13297 }
13298
13299 /* Display an LTO format symbol table.
13300 FIXME: The format of LTO symbol tables is not formalized.
13301 So this code could need changing in the future. */
13302
13303 static bool
13304 display_lto_symtab (Filedata * filedata,
13305 Elf_Internal_Shdr * section)
13306 {
13307 if (section->sh_size == 0)
13308 {
13309 if (filedata->is_separate)
13310 printf (_("\nThe LTO Symbol table section '%s' in linked file '%s' is empty!\n"),
13311 printable_section_name (filedata, section),
13312 filedata->file_name);
13313 else
13314 printf (_("\nLTO Symbol table '%s' is empty!\n"),
13315 printable_section_name (filedata, section));
13316
13317 return true;
13318 }
13319
13320 if (section->sh_size > filedata->file_size)
13321 {
13322 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
13323 printable_section_name (filedata, section),
13324 (unsigned long) section->sh_size);
13325 return false;
13326 }
13327
13328 void * alloced_data = get_data (NULL, filedata, section->sh_offset,
13329 section->sh_size, 1, _("LTO symbols"));
13330 if (alloced_data == NULL)
13331 return false;
13332
13333 /* Look for extended data for the symbol table. */
13334 Elf_Internal_Shdr * ext;
13335 void * ext_data_orig = NULL;
13336 char * ext_data = NULL;
13337 char * ext_data_end = NULL;
13338 char * ext_name = NULL;
13339
13340 if (asprintf (& ext_name, ".gnu.lto_.ext_symtab.%s",
13341 (section_name (filedata, section)
13342 + sizeof (".gnu.lto_.symtab.") - 1)) > 0
13343 && ext_name != NULL /* Paranoia. */
13344 && (ext = find_section (filedata, ext_name)) != NULL)
13345 {
13346 if (ext->sh_size < 3)
13347 error (_("LTO Symbol extension table '%s' is empty!\n"),
13348 printable_section_name (filedata, ext));
13349 else
13350 {
13351 ext_data_orig = ext_data = get_data (NULL, filedata, ext->sh_offset,
13352 ext->sh_size, 1,
13353 _("LTO ext symbol data"));
13354 if (ext_data != NULL)
13355 {
13356 ext_data_end = ext_data + ext->sh_size;
13357 if (* ext_data++ != 1)
13358 error (_("Unexpected version number in symbol extension table\n"));
13359 }
13360 }
13361 }
13362
13363 const unsigned char * data = (const unsigned char *) alloced_data;
13364 const unsigned char * end = data + section->sh_size;
13365
13366 if (filedata->is_separate)
13367 printf (_("\nIn linked file '%s': "), filedata->file_name);
13368 else
13369 printf ("\n");
13370
13371 if (ext_data_orig != NULL)
13372 {
13373 if (do_wide)
13374 printf (_("LTO Symbol table '%s' and extension table '%s' contain:\n"),
13375 printable_section_name (filedata, section),
13376 printable_section_name (filedata, ext));
13377 else
13378 {
13379 printf (_("LTO Symbol table '%s'\n"),
13380 printable_section_name (filedata, section));
13381 printf (_(" and extension table '%s' contain:\n"),
13382 printable_section_name (filedata, ext));
13383 }
13384 }
13385 else
13386 printf (_("LTO Symbol table '%s' contains:\n"),
13387 printable_section_name (filedata, section));
13388
13389 /* FIXME: Add a wide version. */
13390 if (ext_data_orig != NULL)
13391 printf (_(" Comdat_Key Kind Visibility Size Slot Type Section Name\n"));
13392 else
13393 printf (_(" Comdat_Key Kind Visibility Size Slot Name\n"));
13394
13395 /* FIXME: We do not handle style prefixes. */
13396
13397 while (data < end)
13398 {
13399 const unsigned char * sym_name = data;
13400 data += strnlen ((const char *) sym_name, end - data) + 1;
13401 if (data >= end)
13402 goto fail;
13403
13404 const unsigned char * comdat_key = data;
13405 data += strnlen ((const char *) comdat_key, end - data) + 1;
13406 if (data >= end)
13407 goto fail;
13408
13409 if (data + 2 + 8 + 4 > end)
13410 goto fail;
13411
13412 unsigned int kind = *data++;
13413 unsigned int visibility = *data++;
13414
13415 elf_vma size = byte_get (data, 8);
13416 data += 8;
13417
13418 elf_vma slot = byte_get (data, 4);
13419 data += 4;
13420
13421 if (ext_data != NULL)
13422 {
13423 if (ext_data < (ext_data_end - 1))
13424 {
13425 unsigned int sym_type = * ext_data ++;
13426 unsigned int sec_kind = * ext_data ++;
13427
13428 printf (" %10s %10s %11s %08lx %08lx %9s %08lx _",
13429 * comdat_key == 0 ? "-" : (char *) comdat_key,
13430 get_lto_kind (kind),
13431 get_lto_visibility (visibility),
13432 (long) size,
13433 (long) slot,
13434 get_lto_sym_type (sym_type),
13435 (long) sec_kind);
13436 print_symbol (6, (const char *) sym_name);
13437 }
13438 else
13439 {
13440 error (_("Ran out of LTO symbol extension data\n"));
13441 ext_data = NULL;
13442 /* FIXME: return FAIL result ? */
13443 }
13444 }
13445 else
13446 {
13447 printf (" %10s %10s %11s %08lx %08lx _",
13448 * comdat_key == 0 ? "-" : (char *) comdat_key,
13449 get_lto_kind (kind),
13450 get_lto_visibility (visibility),
13451 (long) size,
13452 (long) slot);
13453 print_symbol (21, (const char *) sym_name);
13454 }
13455 putchar ('\n');
13456 }
13457
13458 if (ext_data != NULL && ext_data < ext_data_end)
13459 {
13460 error (_("Data remains in the LTO symbol extension table\n"));
13461 goto fail;
13462 }
13463
13464 free (alloced_data);
13465 free (ext_data_orig);
13466 free (ext_name);
13467 return true;
13468
13469 fail:
13470 error (_("Buffer overrun encountered whilst decoding LTO symbol table\n"));
13471 free (alloced_data);
13472 free (ext_data_orig);
13473 free (ext_name);
13474 return false;
13475 }
13476
13477 /* Display LTO symbol tables. */
13478
13479 static bool
13480 process_lto_symbol_tables (Filedata * filedata)
13481 {
13482 Elf_Internal_Shdr * section;
13483 unsigned int i;
13484 bool res = true;
13485
13486 if (!do_lto_syms)
13487 return true;
13488
13489 if (filedata->section_headers == NULL)
13490 return true;
13491
13492 for (i = 0, section = filedata->section_headers;
13493 i < filedata->file_header.e_shnum;
13494 i++, section++)
13495 if (section_name_valid (filedata, section)
13496 && startswith (section_name (filedata, section), ".gnu.lto_.symtab."))
13497 res &= display_lto_symtab (filedata, section);
13498
13499 return res;
13500 }
13501
13502 /* Dump the symbol table. */
13503
13504 static bool
13505 process_symbol_table (Filedata * filedata)
13506 {
13507 Elf_Internal_Shdr * section;
13508
13509 if (!do_syms && !do_dyn_syms && !do_histogram)
13510 return true;
13511
13512 if ((filedata->dynamic_info[DT_HASH] || filedata->dynamic_info_DT_GNU_HASH)
13513 && do_syms
13514 && do_using_dynamic
13515 && filedata->dynamic_strings != NULL
13516 && filedata->dynamic_symbols != NULL)
13517 {
13518 unsigned long si;
13519
13520 if (filedata->is_separate)
13521 {
13522 printf (ngettext ("\nIn linked file '%s' the dynamic symbol table contains %lu entry:\n",
13523 "\nIn linked file '%s' the dynamic symbol table contains %lu entries:\n",
13524 filedata->num_dynamic_syms),
13525 filedata->file_name,
13526 filedata->num_dynamic_syms);
13527 }
13528 else
13529 {
13530 printf (ngettext ("\nSymbol table for image contains %lu entry:\n",
13531 "\nSymbol table for image contains %lu entries:\n",
13532 filedata->num_dynamic_syms),
13533 filedata->num_dynamic_syms);
13534 }
13535 if (is_32bit_elf)
13536 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13537 else
13538 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13539
13540 for (si = 0; si < filedata->num_dynamic_syms; si++)
13541 print_dynamic_symbol (filedata, si, filedata->dynamic_symbols, NULL,
13542 filedata->dynamic_strings,
13543 filedata->dynamic_strings_length);
13544 }
13545 else if ((do_dyn_syms || (do_syms && !do_using_dynamic))
13546 && filedata->section_headers != NULL)
13547 {
13548 unsigned int i;
13549
13550 for (i = 0, section = filedata->section_headers;
13551 i < filedata->file_header.e_shnum;
13552 i++, section++)
13553 {
13554 char * strtab = NULL;
13555 unsigned long int strtab_size = 0;
13556 Elf_Internal_Sym * symtab;
13557 unsigned long si, num_syms;
13558
13559 if ((section->sh_type != SHT_SYMTAB
13560 && section->sh_type != SHT_DYNSYM)
13561 || (!do_syms
13562 && section->sh_type == SHT_SYMTAB))
13563 continue;
13564
13565 if (section->sh_entsize == 0)
13566 {
13567 printf (_("\nSymbol table '%s' has a sh_entsize of zero!\n"),
13568 printable_section_name (filedata, section));
13569 continue;
13570 }
13571
13572 num_syms = section->sh_size / section->sh_entsize;
13573
13574 if (filedata->is_separate)
13575 printf (ngettext ("\nIn linked file '%s' symbol section '%s' contains %lu entry:\n",
13576 "\nIn linked file '%s' symbol section '%s' contains %lu entries:\n",
13577 num_syms),
13578 filedata->file_name,
13579 printable_section_name (filedata, section),
13580 num_syms);
13581 else
13582 printf (ngettext ("\nSymbol table '%s' contains %lu entry:\n",
13583 "\nSymbol table '%s' contains %lu entries:\n",
13584 num_syms),
13585 printable_section_name (filedata, section),
13586 num_syms);
13587
13588 if (is_32bit_elf)
13589 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13590 else
13591 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13592
13593 symtab = get_elf_symbols (filedata, section, & num_syms);
13594 if (symtab == NULL)
13595 continue;
13596
13597 if (section->sh_link == filedata->file_header.e_shstrndx)
13598 {
13599 strtab = filedata->string_table;
13600 strtab_size = filedata->string_table_length;
13601 }
13602 else if (section->sh_link < filedata->file_header.e_shnum)
13603 {
13604 Elf_Internal_Shdr * string_sec;
13605
13606 string_sec = filedata->section_headers + section->sh_link;
13607
13608 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset,
13609 1, string_sec->sh_size,
13610 _("string table"));
13611 strtab_size = strtab != NULL ? string_sec->sh_size : 0;
13612 }
13613
13614 for (si = 0; si < num_syms; si++)
13615 print_dynamic_symbol (filedata, si, symtab, section,
13616 strtab, strtab_size);
13617
13618 free (symtab);
13619 if (strtab != filedata->string_table)
13620 free (strtab);
13621 }
13622 }
13623 else if (do_syms)
13624 printf
13625 (_("\nDynamic symbol information is not available for displaying symbols.\n"));
13626
13627 if (do_histogram && filedata->buckets != NULL)
13628 {
13629 unsigned long * lengths;
13630 unsigned long * counts;
13631 unsigned long hn;
13632 bfd_vma si;
13633 unsigned long maxlength = 0;
13634 unsigned long nzero_counts = 0;
13635 unsigned long nsyms = 0;
13636 char *visited;
13637
13638 printf (ngettext ("\nHistogram for bucket list length "
13639 "(total of %lu bucket):\n",
13640 "\nHistogram for bucket list length "
13641 "(total of %lu buckets):\n",
13642 (unsigned long) filedata->nbuckets),
13643 (unsigned long) filedata->nbuckets);
13644
13645 lengths = (unsigned long *) calloc (filedata->nbuckets,
13646 sizeof (*lengths));
13647 if (lengths == NULL)
13648 {
13649 error (_("Out of memory allocating space for histogram buckets\n"));
13650 goto err_out;
13651 }
13652 visited = xcmalloc (filedata->nchains, 1);
13653 memset (visited, 0, filedata->nchains);
13654
13655 printf (_(" Length Number %% of total Coverage\n"));
13656 for (hn = 0; hn < filedata->nbuckets; ++hn)
13657 {
13658 for (si = filedata->buckets[hn]; si > 0; si = filedata->chains[si])
13659 {
13660 ++nsyms;
13661 if (maxlength < ++lengths[hn])
13662 ++maxlength;
13663 if (si >= filedata->nchains || visited[si])
13664 {
13665 error (_("histogram chain is corrupt\n"));
13666 break;
13667 }
13668 visited[si] = 1;
13669 }
13670 }
13671 free (visited);
13672
13673 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
13674 if (counts == NULL)
13675 {
13676 free (lengths);
13677 error (_("Out of memory allocating space for histogram counts\n"));
13678 goto err_out;
13679 }
13680
13681 for (hn = 0; hn < filedata->nbuckets; ++hn)
13682 ++counts[lengths[hn]];
13683
13684 if (filedata->nbuckets > 0)
13685 {
13686 unsigned long i;
13687 printf (" 0 %-10lu (%5.1f%%)\n",
13688 counts[0], (counts[0] * 100.0) / filedata->nbuckets);
13689 for (i = 1; i <= maxlength; ++i)
13690 {
13691 nzero_counts += counts[i] * i;
13692 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
13693 i, counts[i], (counts[i] * 100.0) / filedata->nbuckets,
13694 (nzero_counts * 100.0) / nsyms);
13695 }
13696 }
13697
13698 free (counts);
13699 free (lengths);
13700 }
13701
13702 free (filedata->buckets);
13703 filedata->buckets = NULL;
13704 filedata->nbuckets = 0;
13705 free (filedata->chains);
13706 filedata->chains = NULL;
13707
13708 if (do_histogram && filedata->gnubuckets != NULL)
13709 {
13710 unsigned long * lengths;
13711 unsigned long * counts;
13712 unsigned long hn;
13713 unsigned long maxlength = 0;
13714 unsigned long nzero_counts = 0;
13715 unsigned long nsyms = 0;
13716
13717 printf (ngettext ("\nHistogram for `%s' bucket list length "
13718 "(total of %lu bucket):\n",
13719 "\nHistogram for `%s' bucket list length "
13720 "(total of %lu buckets):\n",
13721 (unsigned long) filedata->ngnubuckets),
13722 GNU_HASH_SECTION_NAME (filedata),
13723 (unsigned long) filedata->ngnubuckets);
13724
13725 lengths = (unsigned long *) calloc (filedata->ngnubuckets,
13726 sizeof (*lengths));
13727 if (lengths == NULL)
13728 {
13729 error (_("Out of memory allocating space for gnu histogram buckets\n"));
13730 goto err_out;
13731 }
13732
13733 printf (_(" Length Number %% of total Coverage\n"));
13734
13735 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
13736 if (filedata->gnubuckets[hn] != 0)
13737 {
13738 bfd_vma off, length = 1;
13739
13740 for (off = filedata->gnubuckets[hn] - filedata->gnusymidx;
13741 /* PR 17531 file: 010-77222-0.004. */
13742 off < filedata->ngnuchains
13743 && (filedata->gnuchains[off] & 1) == 0;
13744 ++off)
13745 ++length;
13746 lengths[hn] = length;
13747 if (length > maxlength)
13748 maxlength = length;
13749 nsyms += length;
13750 }
13751
13752 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
13753 if (counts == NULL)
13754 {
13755 free (lengths);
13756 error (_("Out of memory allocating space for gnu histogram counts\n"));
13757 goto err_out;
13758 }
13759
13760 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
13761 ++counts[lengths[hn]];
13762
13763 if (filedata->ngnubuckets > 0)
13764 {
13765 unsigned long j;
13766 printf (" 0 %-10lu (%5.1f%%)\n",
13767 counts[0], (counts[0] * 100.0) / filedata->ngnubuckets);
13768 for (j = 1; j <= maxlength; ++j)
13769 {
13770 nzero_counts += counts[j] * j;
13771 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
13772 j, counts[j], (counts[j] * 100.0) / filedata->ngnubuckets,
13773 (nzero_counts * 100.0) / nsyms);
13774 }
13775 }
13776
13777 free (counts);
13778 free (lengths);
13779 }
13780 free (filedata->gnubuckets);
13781 filedata->gnubuckets = NULL;
13782 filedata->ngnubuckets = 0;
13783 free (filedata->gnuchains);
13784 filedata->gnuchains = NULL;
13785 filedata->ngnuchains = 0;
13786 free (filedata->mipsxlat);
13787 filedata->mipsxlat = NULL;
13788 return true;
13789
13790 err_out:
13791 free (filedata->gnubuckets);
13792 filedata->gnubuckets = NULL;
13793 filedata->ngnubuckets = 0;
13794 free (filedata->gnuchains);
13795 filedata->gnuchains = NULL;
13796 filedata->ngnuchains = 0;
13797 free (filedata->mipsxlat);
13798 filedata->mipsxlat = NULL;
13799 free (filedata->buckets);
13800 filedata->buckets = NULL;
13801 filedata->nbuckets = 0;
13802 free (filedata->chains);
13803 filedata->chains = NULL;
13804 return false;
13805 }
13806
13807 static bool
13808 process_syminfo (Filedata * filedata)
13809 {
13810 unsigned int i;
13811
13812 if (filedata->dynamic_syminfo == NULL
13813 || !do_dynamic)
13814 /* No syminfo, this is ok. */
13815 return true;
13816
13817 /* There better should be a dynamic symbol section. */
13818 if (filedata->dynamic_symbols == NULL || filedata->dynamic_strings == NULL)
13819 return false;
13820
13821 if (filedata->is_separate)
13822 printf (ngettext ("\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entry:\n",
13823 "\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entries:\n",
13824 filedata->dynamic_syminfo_nent),
13825 filedata->file_name,
13826 filedata->dynamic_syminfo_offset,
13827 filedata->dynamic_syminfo_nent);
13828 else
13829 printf (ngettext ("\nDynamic info segment at offset 0x%lx "
13830 "contains %d entry:\n",
13831 "\nDynamic info segment at offset 0x%lx "
13832 "contains %d entries:\n",
13833 filedata->dynamic_syminfo_nent),
13834 filedata->dynamic_syminfo_offset,
13835 filedata->dynamic_syminfo_nent);
13836
13837 printf (_(" Num: Name BoundTo Flags\n"));
13838 for (i = 0; i < filedata->dynamic_syminfo_nent; ++i)
13839 {
13840 unsigned short int flags = filedata->dynamic_syminfo[i].si_flags;
13841
13842 printf ("%4d: ", i);
13843 if (i >= filedata->num_dynamic_syms)
13844 printf (_("<corrupt index>"));
13845 else if (valid_dynamic_name (filedata, filedata->dynamic_symbols[i].st_name))
13846 print_symbol (30, get_dynamic_name (filedata,
13847 filedata->dynamic_symbols[i].st_name));
13848 else
13849 printf (_("<corrupt: %19ld>"), filedata->dynamic_symbols[i].st_name);
13850 putchar (' ');
13851
13852 switch (filedata->dynamic_syminfo[i].si_boundto)
13853 {
13854 case SYMINFO_BT_SELF:
13855 fputs ("SELF ", stdout);
13856 break;
13857 case SYMINFO_BT_PARENT:
13858 fputs ("PARENT ", stdout);
13859 break;
13860 default:
13861 if (filedata->dynamic_syminfo[i].si_boundto > 0
13862 && filedata->dynamic_syminfo[i].si_boundto < filedata->dynamic_nent
13863 && valid_dynamic_name (filedata,
13864 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val))
13865 {
13866 print_symbol (10, get_dynamic_name (filedata,
13867 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val));
13868 putchar (' ' );
13869 }
13870 else
13871 printf ("%-10d ", filedata->dynamic_syminfo[i].si_boundto);
13872 break;
13873 }
13874
13875 if (flags & SYMINFO_FLG_DIRECT)
13876 printf (" DIRECT");
13877 if (flags & SYMINFO_FLG_PASSTHRU)
13878 printf (" PASSTHRU");
13879 if (flags & SYMINFO_FLG_COPY)
13880 printf (" COPY");
13881 if (flags & SYMINFO_FLG_LAZYLOAD)
13882 printf (" LAZYLOAD");
13883
13884 puts ("");
13885 }
13886
13887 return true;
13888 }
13889
13890 /* A macro which evaluates to TRUE if the region ADDR .. ADDR + NELEM
13891 is contained by the region START .. END. The types of ADDR, START
13892 and END should all be the same. Note both ADDR + NELEM and END
13893 point to just beyond the end of the regions that are being tested. */
13894 #define IN_RANGE(START,END,ADDR,NELEM) \
13895 (((ADDR) >= (START)) && ((ADDR) < (END)) && ((ADDR) + (NELEM) <= (END)))
13896
13897 /* Check to see if the given reloc needs to be handled in a target specific
13898 manner. If so then process the reloc and return TRUE otherwise return
13899 FALSE.
13900
13901 If called with reloc == NULL, then this is a signal that reloc processing
13902 for the current section has finished, and any saved state should be
13903 discarded. */
13904
13905 static bool
13906 target_specific_reloc_handling (Filedata * filedata,
13907 Elf_Internal_Rela * reloc,
13908 unsigned char * start,
13909 unsigned char * end,
13910 Elf_Internal_Sym * symtab,
13911 unsigned long num_syms)
13912 {
13913 unsigned int reloc_type = 0;
13914 unsigned long sym_index = 0;
13915
13916 if (reloc)
13917 {
13918 reloc_type = get_reloc_type (filedata, reloc->r_info);
13919 sym_index = get_reloc_symindex (reloc->r_info);
13920 }
13921
13922 switch (filedata->file_header.e_machine)
13923 {
13924 case EM_MSP430:
13925 case EM_MSP430_OLD:
13926 {
13927 static Elf_Internal_Sym * saved_sym = NULL;
13928
13929 if (reloc == NULL)
13930 {
13931 saved_sym = NULL;
13932 return true;
13933 }
13934
13935 switch (reloc_type)
13936 {
13937 case 10: /* R_MSP430_SYM_DIFF */
13938 case 12: /* R_MSP430_GNU_SUB_ULEB128 */
13939 if (uses_msp430x_relocs (filedata))
13940 break;
13941 /* Fall through. */
13942 case 21: /* R_MSP430X_SYM_DIFF */
13943 case 23: /* R_MSP430X_GNU_SUB_ULEB128 */
13944 /* PR 21139. */
13945 if (sym_index >= num_syms)
13946 error (_("MSP430 SYM_DIFF reloc contains invalid symbol index %lu\n"),
13947 sym_index);
13948 else
13949 saved_sym = symtab + sym_index;
13950 return true;
13951
13952 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
13953 case 3: /* R_MSP430_16 or R_MSP430_ABS8 */
13954 goto handle_sym_diff;
13955
13956 case 5: /* R_MSP430_16_BYTE */
13957 case 9: /* R_MSP430_8 */
13958 case 11: /* R_MSP430_GNU_SET_ULEB128 */
13959 if (uses_msp430x_relocs (filedata))
13960 break;
13961 goto handle_sym_diff;
13962
13963 case 2: /* R_MSP430_ABS16 */
13964 case 15: /* R_MSP430X_ABS16 */
13965 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
13966 if (! uses_msp430x_relocs (filedata))
13967 break;
13968 goto handle_sym_diff;
13969
13970 handle_sym_diff:
13971 if (saved_sym != NULL)
13972 {
13973 bfd_vma value;
13974 unsigned int reloc_size = 0;
13975 int leb_ret = 0;
13976 switch (reloc_type)
13977 {
13978 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
13979 reloc_size = 4;
13980 break;
13981 case 11: /* R_MSP430_GNU_SET_ULEB128 */
13982 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
13983 if (reloc->r_offset < (size_t) (end - start))
13984 read_leb128 (start + reloc->r_offset, end, false,
13985 &reloc_size, &leb_ret);
13986 break;
13987 default:
13988 reloc_size = 2;
13989 break;
13990 }
13991
13992 if (leb_ret != 0 || reloc_size == 0 || reloc_size > 8)
13993 error (_("MSP430 ULEB128 field at 0x%lx contains invalid "
13994 "ULEB128 value\n"),
13995 (long) reloc->r_offset);
13996 else if (sym_index >= num_syms)
13997 error (_("MSP430 reloc contains invalid symbol index %lu\n"),
13998 sym_index);
13999 else
14000 {
14001 value = reloc->r_addend + (symtab[sym_index].st_value
14002 - saved_sym->st_value);
14003
14004 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
14005 byte_put (start + reloc->r_offset, value, reloc_size);
14006 else
14007 /* PR 21137 */
14008 error (_("MSP430 sym diff reloc contains invalid offset: 0x%lx\n"),
14009 (long) reloc->r_offset);
14010 }
14011
14012 saved_sym = NULL;
14013 return true;
14014 }
14015 break;
14016
14017 default:
14018 if (saved_sym != NULL)
14019 error (_("Unhandled MSP430 reloc type found after SYM_DIFF reloc\n"));
14020 break;
14021 }
14022 break;
14023 }
14024
14025 case EM_MN10300:
14026 case EM_CYGNUS_MN10300:
14027 {
14028 static Elf_Internal_Sym * saved_sym = NULL;
14029
14030 if (reloc == NULL)
14031 {
14032 saved_sym = NULL;
14033 return true;
14034 }
14035
14036 switch (reloc_type)
14037 {
14038 case 34: /* R_MN10300_ALIGN */
14039 return true;
14040 case 33: /* R_MN10300_SYM_DIFF */
14041 if (sym_index >= num_syms)
14042 error (_("MN10300_SYM_DIFF reloc contains invalid symbol index %lu\n"),
14043 sym_index);
14044 else
14045 saved_sym = symtab + sym_index;
14046 return true;
14047
14048 case 1: /* R_MN10300_32 */
14049 case 2: /* R_MN10300_16 */
14050 if (saved_sym != NULL)
14051 {
14052 int reloc_size = reloc_type == 1 ? 4 : 2;
14053 bfd_vma value;
14054
14055 if (sym_index >= num_syms)
14056 error (_("MN10300 reloc contains invalid symbol index %lu\n"),
14057 sym_index);
14058 else
14059 {
14060 value = reloc->r_addend + (symtab[sym_index].st_value
14061 - saved_sym->st_value);
14062
14063 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
14064 byte_put (start + reloc->r_offset, value, reloc_size);
14065 else
14066 error (_("MN10300 sym diff reloc contains invalid offset: 0x%lx\n"),
14067 (long) reloc->r_offset);
14068 }
14069
14070 saved_sym = NULL;
14071 return true;
14072 }
14073 break;
14074 default:
14075 if (saved_sym != NULL)
14076 error (_("Unhandled MN10300 reloc type found after SYM_DIFF reloc\n"));
14077 break;
14078 }
14079 break;
14080 }
14081
14082 case EM_RL78:
14083 {
14084 static bfd_vma saved_sym1 = 0;
14085 static bfd_vma saved_sym2 = 0;
14086 static bfd_vma value;
14087
14088 if (reloc == NULL)
14089 {
14090 saved_sym1 = saved_sym2 = 0;
14091 return true;
14092 }
14093
14094 switch (reloc_type)
14095 {
14096 case 0x80: /* R_RL78_SYM. */
14097 saved_sym1 = saved_sym2;
14098 if (sym_index >= num_syms)
14099 error (_("RL78_SYM reloc contains invalid symbol index %lu\n"),
14100 sym_index);
14101 else
14102 {
14103 saved_sym2 = symtab[sym_index].st_value;
14104 saved_sym2 += reloc->r_addend;
14105 }
14106 return true;
14107
14108 case 0x83: /* R_RL78_OPsub. */
14109 value = saved_sym1 - saved_sym2;
14110 saved_sym2 = saved_sym1 = 0;
14111 return true;
14112 break;
14113
14114 case 0x41: /* R_RL78_ABS32. */
14115 if (IN_RANGE (start, end, start + reloc->r_offset, 4))
14116 byte_put (start + reloc->r_offset, value, 4);
14117 else
14118 error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
14119 (long) reloc->r_offset);
14120 value = 0;
14121 return true;
14122
14123 case 0x43: /* R_RL78_ABS16. */
14124 if (IN_RANGE (start, end, start + reloc->r_offset, 2))
14125 byte_put (start + reloc->r_offset, value, 2);
14126 else
14127 error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
14128 (long) reloc->r_offset);
14129 value = 0;
14130 return true;
14131
14132 default:
14133 break;
14134 }
14135 break;
14136 }
14137 }
14138
14139 return false;
14140 }
14141
14142 /* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
14143 DWARF debug sections. This is a target specific test. Note - we do not
14144 go through the whole including-target-headers-multiple-times route, (as
14145 we have already done with <elf/h8.h>) because this would become very
14146 messy and even then this function would have to contain target specific
14147 information (the names of the relocs instead of their numeric values).
14148 FIXME: This is not the correct way to solve this problem. The proper way
14149 is to have target specific reloc sizing and typing functions created by
14150 the reloc-macros.h header, in the same way that it already creates the
14151 reloc naming functions. */
14152
14153 static bool
14154 is_32bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14155 {
14156 /* Please keep this table alpha-sorted for ease of visual lookup. */
14157 switch (filedata->file_header.e_machine)
14158 {
14159 case EM_386:
14160 case EM_IAMCU:
14161 return reloc_type == 1; /* R_386_32. */
14162 case EM_68K:
14163 return reloc_type == 1; /* R_68K_32. */
14164 case EM_860:
14165 return reloc_type == 1; /* R_860_32. */
14166 case EM_960:
14167 return reloc_type == 2; /* R_960_32. */
14168 case EM_AARCH64:
14169 return (reloc_type == 258
14170 || reloc_type == 1); /* R_AARCH64_ABS32 || R_AARCH64_P32_ABS32 */
14171 case EM_BPF:
14172 return reloc_type == 11; /* R_BPF_DATA_32 */
14173 case EM_ADAPTEVA_EPIPHANY:
14174 return reloc_type == 3;
14175 case EM_ALPHA:
14176 return reloc_type == 1; /* R_ALPHA_REFLONG. */
14177 case EM_ARC:
14178 return reloc_type == 1; /* R_ARC_32. */
14179 case EM_ARC_COMPACT:
14180 case EM_ARC_COMPACT2:
14181 return reloc_type == 4; /* R_ARC_32. */
14182 case EM_ARM:
14183 return reloc_type == 2; /* R_ARM_ABS32 */
14184 case EM_AVR_OLD:
14185 case EM_AVR:
14186 return reloc_type == 1;
14187 case EM_BLACKFIN:
14188 return reloc_type == 0x12; /* R_byte4_data. */
14189 case EM_CRIS:
14190 return reloc_type == 3; /* R_CRIS_32. */
14191 case EM_CR16:
14192 return reloc_type == 3; /* R_CR16_NUM32. */
14193 case EM_CRX:
14194 return reloc_type == 15; /* R_CRX_NUM32. */
14195 case EM_CSKY:
14196 return reloc_type == 1; /* R_CKCORE_ADDR32. */
14197 case EM_CYGNUS_FRV:
14198 return reloc_type == 1;
14199 case EM_CYGNUS_D10V:
14200 case EM_D10V:
14201 return reloc_type == 6; /* R_D10V_32. */
14202 case EM_CYGNUS_D30V:
14203 case EM_D30V:
14204 return reloc_type == 12; /* R_D30V_32_NORMAL. */
14205 case EM_DLX:
14206 return reloc_type == 3; /* R_DLX_RELOC_32. */
14207 case EM_CYGNUS_FR30:
14208 case EM_FR30:
14209 return reloc_type == 3; /* R_FR30_32. */
14210 case EM_FT32:
14211 return reloc_type == 1; /* R_FT32_32. */
14212 case EM_H8S:
14213 case EM_H8_300:
14214 case EM_H8_300H:
14215 return reloc_type == 1; /* R_H8_DIR32. */
14216 case EM_IA_64:
14217 return (reloc_type == 0x64 /* R_IA64_SECREL32MSB. */
14218 || reloc_type == 0x65 /* R_IA64_SECREL32LSB. */
14219 || reloc_type == 0x24 /* R_IA64_DIR32MSB. */
14220 || reloc_type == 0x25 /* R_IA64_DIR32LSB. */);
14221 case EM_IP2K_OLD:
14222 case EM_IP2K:
14223 return reloc_type == 2; /* R_IP2K_32. */
14224 case EM_IQ2000:
14225 return reloc_type == 2; /* R_IQ2000_32. */
14226 case EM_LATTICEMICO32:
14227 return reloc_type == 3; /* R_LM32_32. */
14228 case EM_LOONGARCH:
14229 return reloc_type == 1; /* R_LARCH_32. */
14230 case EM_M32C_OLD:
14231 case EM_M32C:
14232 return reloc_type == 3; /* R_M32C_32. */
14233 case EM_M32R:
14234 return reloc_type == 34; /* R_M32R_32_RELA. */
14235 case EM_68HC11:
14236 case EM_68HC12:
14237 return reloc_type == 6; /* R_M68HC11_32. */
14238 case EM_S12Z:
14239 return reloc_type == 7 || /* R_S12Z_EXT32 */
14240 reloc_type == 6; /* R_S12Z_CW32. */
14241 case EM_MCORE:
14242 return reloc_type == 1; /* R_MCORE_ADDR32. */
14243 case EM_CYGNUS_MEP:
14244 return reloc_type == 4; /* R_MEP_32. */
14245 case EM_METAG:
14246 return reloc_type == 2; /* R_METAG_ADDR32. */
14247 case EM_MICROBLAZE:
14248 return reloc_type == 1; /* R_MICROBLAZE_32. */
14249 case EM_MIPS:
14250 return reloc_type == 2; /* R_MIPS_32. */
14251 case EM_MMIX:
14252 return reloc_type == 4; /* R_MMIX_32. */
14253 case EM_CYGNUS_MN10200:
14254 case EM_MN10200:
14255 return reloc_type == 1; /* R_MN10200_32. */
14256 case EM_CYGNUS_MN10300:
14257 case EM_MN10300:
14258 return reloc_type == 1; /* R_MN10300_32. */
14259 case EM_MOXIE:
14260 return reloc_type == 1; /* R_MOXIE_32. */
14261 case EM_MSP430_OLD:
14262 case EM_MSP430:
14263 return reloc_type == 1; /* R_MSP430_32 or R_MSP320_ABS32. */
14264 case EM_MT:
14265 return reloc_type == 2; /* R_MT_32. */
14266 case EM_NDS32:
14267 return reloc_type == 20; /* R_NDS32_32_RELA. */
14268 case EM_ALTERA_NIOS2:
14269 return reloc_type == 12; /* R_NIOS2_BFD_RELOC_32. */
14270 case EM_NIOS32:
14271 return reloc_type == 1; /* R_NIOS_32. */
14272 case EM_OR1K:
14273 return reloc_type == 1; /* R_OR1K_32. */
14274 case EM_PARISC:
14275 return (reloc_type == 1 /* R_PARISC_DIR32. */
14276 || reloc_type == 2 /* R_PARISC_DIR21L. */
14277 || reloc_type == 41); /* R_PARISC_SECREL32. */
14278 case EM_PJ:
14279 case EM_PJ_OLD:
14280 return reloc_type == 1; /* R_PJ_DATA_DIR32. */
14281 case EM_PPC64:
14282 return reloc_type == 1; /* R_PPC64_ADDR32. */
14283 case EM_PPC:
14284 return reloc_type == 1; /* R_PPC_ADDR32. */
14285 case EM_TI_PRU:
14286 return reloc_type == 11; /* R_PRU_BFD_RELOC_32. */
14287 case EM_RISCV:
14288 return reloc_type == 1; /* R_RISCV_32. */
14289 case EM_RL78:
14290 return reloc_type == 1; /* R_RL78_DIR32. */
14291 case EM_RX:
14292 return reloc_type == 1; /* R_RX_DIR32. */
14293 case EM_S370:
14294 return reloc_type == 1; /* R_I370_ADDR31. */
14295 case EM_S390_OLD:
14296 case EM_S390:
14297 return reloc_type == 4; /* R_S390_32. */
14298 case EM_SCORE:
14299 return reloc_type == 8; /* R_SCORE_ABS32. */
14300 case EM_SH:
14301 return reloc_type == 1; /* R_SH_DIR32. */
14302 case EM_SPARC32PLUS:
14303 case EM_SPARCV9:
14304 case EM_SPARC:
14305 return reloc_type == 3 /* R_SPARC_32. */
14306 || reloc_type == 23; /* R_SPARC_UA32. */
14307 case EM_SPU:
14308 return reloc_type == 6; /* R_SPU_ADDR32 */
14309 case EM_TI_C6000:
14310 return reloc_type == 1; /* R_C6000_ABS32. */
14311 case EM_TILEGX:
14312 return reloc_type == 2; /* R_TILEGX_32. */
14313 case EM_TILEPRO:
14314 return reloc_type == 1; /* R_TILEPRO_32. */
14315 case EM_CYGNUS_V850:
14316 case EM_V850:
14317 return reloc_type == 6; /* R_V850_ABS32. */
14318 case EM_V800:
14319 return reloc_type == 0x33; /* R_V810_WORD. */
14320 case EM_VAX:
14321 return reloc_type == 1; /* R_VAX_32. */
14322 case EM_VISIUM:
14323 return reloc_type == 3; /* R_VISIUM_32. */
14324 case EM_WEBASSEMBLY:
14325 return reloc_type == 1; /* R_WASM32_32. */
14326 case EM_X86_64:
14327 case EM_L1OM:
14328 case EM_K1OM:
14329 return reloc_type == 10; /* R_X86_64_32. */
14330 case EM_XGATE:
14331 return reloc_type == 4; /* R_XGATE_32. */
14332 case EM_XSTORMY16:
14333 return reloc_type == 1; /* R_XSTROMY16_32. */
14334 case EM_XTENSA_OLD:
14335 case EM_XTENSA:
14336 return reloc_type == 1; /* R_XTENSA_32. */
14337 case EM_Z80:
14338 return reloc_type == 6; /* R_Z80_32. */
14339 default:
14340 {
14341 static unsigned int prev_warn = 0;
14342
14343 /* Avoid repeating the same warning multiple times. */
14344 if (prev_warn != filedata->file_header.e_machine)
14345 error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
14346 filedata->file_header.e_machine);
14347 prev_warn = filedata->file_header.e_machine;
14348 return false;
14349 }
14350 }
14351 }
14352
14353 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14354 a 32-bit pc-relative RELA relocation used in DWARF debug sections. */
14355
14356 static bool
14357 is_32bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
14358 {
14359 switch (filedata->file_header.e_machine)
14360 /* Please keep this table alpha-sorted for ease of visual lookup. */
14361 {
14362 case EM_386:
14363 case EM_IAMCU:
14364 return reloc_type == 2; /* R_386_PC32. */
14365 case EM_68K:
14366 return reloc_type == 4; /* R_68K_PC32. */
14367 case EM_AARCH64:
14368 return reloc_type == 261; /* R_AARCH64_PREL32 */
14369 case EM_ADAPTEVA_EPIPHANY:
14370 return reloc_type == 6;
14371 case EM_ALPHA:
14372 return reloc_type == 10; /* R_ALPHA_SREL32. */
14373 case EM_ARC_COMPACT:
14374 case EM_ARC_COMPACT2:
14375 return reloc_type == 49; /* R_ARC_32_PCREL. */
14376 case EM_ARM:
14377 return reloc_type == 3; /* R_ARM_REL32 */
14378 case EM_AVR_OLD:
14379 case EM_AVR:
14380 return reloc_type == 36; /* R_AVR_32_PCREL. */
14381 case EM_LOONGARCH:
14382 return reloc_type == 99; /* R_LARCH_32_PCREL. */
14383 case EM_MICROBLAZE:
14384 return reloc_type == 2; /* R_MICROBLAZE_32_PCREL. */
14385 case EM_OR1K:
14386 return reloc_type == 9; /* R_OR1K_32_PCREL. */
14387 case EM_PARISC:
14388 return reloc_type == 9; /* R_PARISC_PCREL32. */
14389 case EM_PPC:
14390 return reloc_type == 26; /* R_PPC_REL32. */
14391 case EM_PPC64:
14392 return reloc_type == 26; /* R_PPC64_REL32. */
14393 case EM_RISCV:
14394 return reloc_type == 57; /* R_RISCV_32_PCREL. */
14395 case EM_S390_OLD:
14396 case EM_S390:
14397 return reloc_type == 5; /* R_390_PC32. */
14398 case EM_SH:
14399 return reloc_type == 2; /* R_SH_REL32. */
14400 case EM_SPARC32PLUS:
14401 case EM_SPARCV9:
14402 case EM_SPARC:
14403 return reloc_type == 6; /* R_SPARC_DISP32. */
14404 case EM_SPU:
14405 return reloc_type == 13; /* R_SPU_REL32. */
14406 case EM_TILEGX:
14407 return reloc_type == 6; /* R_TILEGX_32_PCREL. */
14408 case EM_TILEPRO:
14409 return reloc_type == 4; /* R_TILEPRO_32_PCREL. */
14410 case EM_VISIUM:
14411 return reloc_type == 6; /* R_VISIUM_32_PCREL */
14412 case EM_X86_64:
14413 case EM_L1OM:
14414 case EM_K1OM:
14415 return reloc_type == 2; /* R_X86_64_PC32. */
14416 case EM_VAX:
14417 return reloc_type == 4; /* R_VAX_PCREL32. */
14418 case EM_XTENSA_OLD:
14419 case EM_XTENSA:
14420 return reloc_type == 14; /* R_XTENSA_32_PCREL. */
14421 default:
14422 /* Do not abort or issue an error message here. Not all targets use
14423 pc-relative 32-bit relocs in their DWARF debug information and we
14424 have already tested for target coverage in is_32bit_abs_reloc. A
14425 more helpful warning message will be generated by apply_relocations
14426 anyway, so just return. */
14427 return false;
14428 }
14429 }
14430
14431 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14432 a 64-bit absolute RELA relocation used in DWARF debug sections. */
14433
14434 static bool
14435 is_64bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14436 {
14437 switch (filedata->file_header.e_machine)
14438 {
14439 case EM_AARCH64:
14440 return reloc_type == 257; /* R_AARCH64_ABS64. */
14441 case EM_ALPHA:
14442 return reloc_type == 2; /* R_ALPHA_REFQUAD. */
14443 case EM_IA_64:
14444 return (reloc_type == 0x26 /* R_IA64_DIR64MSB. */
14445 || reloc_type == 0x27 /* R_IA64_DIR64LSB. */);
14446 case EM_LOONGARCH:
14447 return reloc_type == 2; /* R_LARCH_64 */
14448 case EM_PARISC:
14449 return reloc_type == 80; /* R_PARISC_DIR64. */
14450 case EM_PPC64:
14451 return reloc_type == 38; /* R_PPC64_ADDR64. */
14452 case EM_RISCV:
14453 return reloc_type == 2; /* R_RISCV_64. */
14454 case EM_SPARC32PLUS:
14455 case EM_SPARCV9:
14456 case EM_SPARC:
14457 return reloc_type == 32 /* R_SPARC_64. */
14458 || reloc_type == 54; /* R_SPARC_UA64. */
14459 case EM_X86_64:
14460 case EM_L1OM:
14461 case EM_K1OM:
14462 return reloc_type == 1; /* R_X86_64_64. */
14463 case EM_S390_OLD:
14464 case EM_S390:
14465 return reloc_type == 22; /* R_S390_64. */
14466 case EM_TILEGX:
14467 return reloc_type == 1; /* R_TILEGX_64. */
14468 case EM_MIPS:
14469 return reloc_type == 18; /* R_MIPS_64. */
14470 default:
14471 return false;
14472 }
14473 }
14474
14475 /* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
14476 a 64-bit pc-relative RELA relocation used in DWARF debug sections. */
14477
14478 static bool
14479 is_64bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
14480 {
14481 switch (filedata->file_header.e_machine)
14482 {
14483 case EM_AARCH64:
14484 return reloc_type == 260; /* R_AARCH64_PREL64. */
14485 case EM_ALPHA:
14486 return reloc_type == 11; /* R_ALPHA_SREL64. */
14487 case EM_IA_64:
14488 return (reloc_type == 0x4e /* R_IA64_PCREL64MSB. */
14489 || reloc_type == 0x4f /* R_IA64_PCREL64LSB. */);
14490 case EM_PARISC:
14491 return reloc_type == 72; /* R_PARISC_PCREL64. */
14492 case EM_PPC64:
14493 return reloc_type == 44; /* R_PPC64_REL64. */
14494 case EM_SPARC32PLUS:
14495 case EM_SPARCV9:
14496 case EM_SPARC:
14497 return reloc_type == 46; /* R_SPARC_DISP64. */
14498 case EM_X86_64:
14499 case EM_L1OM:
14500 case EM_K1OM:
14501 return reloc_type == 24; /* R_X86_64_PC64. */
14502 case EM_S390_OLD:
14503 case EM_S390:
14504 return reloc_type == 23; /* R_S390_PC64. */
14505 case EM_TILEGX:
14506 return reloc_type == 5; /* R_TILEGX_64_PCREL. */
14507 default:
14508 return false;
14509 }
14510 }
14511
14512 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14513 a 24-bit absolute RELA relocation used in DWARF debug sections. */
14514
14515 static bool
14516 is_24bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14517 {
14518 switch (filedata->file_header.e_machine)
14519 {
14520 case EM_CYGNUS_MN10200:
14521 case EM_MN10200:
14522 return reloc_type == 4; /* R_MN10200_24. */
14523 case EM_FT32:
14524 return reloc_type == 5; /* R_FT32_20. */
14525 case EM_Z80:
14526 return reloc_type == 5; /* R_Z80_24. */
14527 default:
14528 return false;
14529 }
14530 }
14531
14532 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14533 a 16-bit absolute RELA relocation used in DWARF debug sections. */
14534
14535 static bool
14536 is_16bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14537 {
14538 /* Please keep this table alpha-sorted for ease of visual lookup. */
14539 switch (filedata->file_header.e_machine)
14540 {
14541 case EM_ARC:
14542 case EM_ARC_COMPACT:
14543 case EM_ARC_COMPACT2:
14544 return reloc_type == 2; /* R_ARC_16. */
14545 case EM_ADAPTEVA_EPIPHANY:
14546 return reloc_type == 5;
14547 case EM_AVR_OLD:
14548 case EM_AVR:
14549 return reloc_type == 4; /* R_AVR_16. */
14550 case EM_CYGNUS_D10V:
14551 case EM_D10V:
14552 return reloc_type == 3; /* R_D10V_16. */
14553 case EM_FT32:
14554 return reloc_type == 2; /* R_FT32_16. */
14555 case EM_H8S:
14556 case EM_H8_300:
14557 case EM_H8_300H:
14558 return reloc_type == R_H8_DIR16;
14559 case EM_IP2K_OLD:
14560 case EM_IP2K:
14561 return reloc_type == 1; /* R_IP2K_16. */
14562 case EM_M32C_OLD:
14563 case EM_M32C:
14564 return reloc_type == 1; /* R_M32C_16 */
14565 case EM_CYGNUS_MN10200:
14566 case EM_MN10200:
14567 return reloc_type == 2; /* R_MN10200_16. */
14568 case EM_CYGNUS_MN10300:
14569 case EM_MN10300:
14570 return reloc_type == 2; /* R_MN10300_16. */
14571 case EM_MSP430:
14572 if (uses_msp430x_relocs (filedata))
14573 return reloc_type == 2; /* R_MSP430_ABS16. */
14574 /* Fall through. */
14575 case EM_MSP430_OLD:
14576 return reloc_type == 5; /* R_MSP430_16_BYTE. */
14577 case EM_NDS32:
14578 return reloc_type == 19; /* R_NDS32_16_RELA. */
14579 case EM_ALTERA_NIOS2:
14580 return reloc_type == 13; /* R_NIOS2_BFD_RELOC_16. */
14581 case EM_NIOS32:
14582 return reloc_type == 9; /* R_NIOS_16. */
14583 case EM_OR1K:
14584 return reloc_type == 2; /* R_OR1K_16. */
14585 case EM_RISCV:
14586 return reloc_type == 55; /* R_RISCV_SET16. */
14587 case EM_TI_PRU:
14588 return reloc_type == 8; /* R_PRU_BFD_RELOC_16. */
14589 case EM_TI_C6000:
14590 return reloc_type == 2; /* R_C6000_ABS16. */
14591 case EM_VISIUM:
14592 return reloc_type == 2; /* R_VISIUM_16. */
14593 case EM_XGATE:
14594 return reloc_type == 3; /* R_XGATE_16. */
14595 case EM_Z80:
14596 return reloc_type == 4; /* R_Z80_16. */
14597 default:
14598 return false;
14599 }
14600 }
14601
14602 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14603 a 8-bit absolute RELA relocation used in DWARF debug sections. */
14604
14605 static bool
14606 is_8bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14607 {
14608 switch (filedata->file_header.e_machine)
14609 {
14610 case EM_RISCV:
14611 return reloc_type == 54; /* R_RISCV_SET8. */
14612 case EM_Z80:
14613 return reloc_type == 1; /* R_Z80_8. */
14614 default:
14615 return false;
14616 }
14617 }
14618
14619 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14620 a 6-bit absolute RELA relocation used in DWARF debug sections. */
14621
14622 static bool
14623 is_6bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14624 {
14625 switch (filedata->file_header.e_machine)
14626 {
14627 case EM_RISCV:
14628 return reloc_type == 53; /* R_RISCV_SET6. */
14629 default:
14630 return false;
14631 }
14632 }
14633
14634 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14635 a 32-bit inplace add RELA relocation used in DWARF debug sections. */
14636
14637 static bool
14638 is_32bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14639 {
14640 /* Please keep this table alpha-sorted for ease of visual lookup. */
14641 switch (filedata->file_header.e_machine)
14642 {
14643 case EM_RISCV:
14644 return reloc_type == 35; /* R_RISCV_ADD32. */
14645 default:
14646 return false;
14647 }
14648 }
14649
14650 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14651 a 32-bit inplace sub RELA relocation used in DWARF debug sections. */
14652
14653 static bool
14654 is_32bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14655 {
14656 /* Please keep this table alpha-sorted for ease of visual lookup. */
14657 switch (filedata->file_header.e_machine)
14658 {
14659 case EM_RISCV:
14660 return reloc_type == 39; /* R_RISCV_SUB32. */
14661 default:
14662 return false;
14663 }
14664 }
14665
14666 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14667 a 64-bit inplace add RELA relocation used in DWARF debug sections. */
14668
14669 static bool
14670 is_64bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14671 {
14672 /* Please keep this table alpha-sorted for ease of visual lookup. */
14673 switch (filedata->file_header.e_machine)
14674 {
14675 case EM_RISCV:
14676 return reloc_type == 36; /* R_RISCV_ADD64. */
14677 default:
14678 return false;
14679 }
14680 }
14681
14682 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14683 a 64-bit inplace sub RELA relocation used in DWARF debug sections. */
14684
14685 static bool
14686 is_64bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14687 {
14688 /* Please keep this table alpha-sorted for ease of visual lookup. */
14689 switch (filedata->file_header.e_machine)
14690 {
14691 case EM_RISCV:
14692 return reloc_type == 40; /* R_RISCV_SUB64. */
14693 default:
14694 return false;
14695 }
14696 }
14697
14698 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14699 a 16-bit inplace add RELA relocation used in DWARF debug sections. */
14700
14701 static bool
14702 is_16bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14703 {
14704 /* Please keep this table alpha-sorted for ease of visual lookup. */
14705 switch (filedata->file_header.e_machine)
14706 {
14707 case EM_RISCV:
14708 return reloc_type == 34; /* R_RISCV_ADD16. */
14709 default:
14710 return false;
14711 }
14712 }
14713
14714 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14715 a 16-bit inplace sub RELA relocation used in DWARF debug sections. */
14716
14717 static bool
14718 is_16bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14719 {
14720 /* Please keep this table alpha-sorted for ease of visual lookup. */
14721 switch (filedata->file_header.e_machine)
14722 {
14723 case EM_RISCV:
14724 return reloc_type == 38; /* R_RISCV_SUB16. */
14725 default:
14726 return false;
14727 }
14728 }
14729
14730 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14731 a 8-bit inplace add RELA relocation used in DWARF debug sections. */
14732
14733 static bool
14734 is_8bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14735 {
14736 /* Please keep this table alpha-sorted for ease of visual lookup. */
14737 switch (filedata->file_header.e_machine)
14738 {
14739 case EM_RISCV:
14740 return reloc_type == 33; /* R_RISCV_ADD8. */
14741 default:
14742 return false;
14743 }
14744 }
14745
14746 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14747 a 8-bit inplace sub RELA relocation used in DWARF debug sections. */
14748
14749 static bool
14750 is_8bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14751 {
14752 /* Please keep this table alpha-sorted for ease of visual lookup. */
14753 switch (filedata->file_header.e_machine)
14754 {
14755 case EM_RISCV:
14756 return reloc_type == 37; /* R_RISCV_SUB8. */
14757 default:
14758 return false;
14759 }
14760 }
14761
14762 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14763 a 6-bit inplace sub RELA relocation used in DWARF debug sections. */
14764
14765 static bool
14766 is_6bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14767 {
14768 switch (filedata->file_header.e_machine)
14769 {
14770 case EM_RISCV:
14771 return reloc_type == 52; /* R_RISCV_SUB6. */
14772 default:
14773 return false;
14774 }
14775 }
14776
14777 /* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
14778 relocation entries (possibly formerly used for SHT_GROUP sections). */
14779
14780 static bool
14781 is_none_reloc (Filedata * filedata, unsigned int reloc_type)
14782 {
14783 switch (filedata->file_header.e_machine)
14784 {
14785 case EM_386: /* R_386_NONE. */
14786 case EM_68K: /* R_68K_NONE. */
14787 case EM_ADAPTEVA_EPIPHANY:
14788 case EM_ALPHA: /* R_ALPHA_NONE. */
14789 case EM_ALTERA_NIOS2: /* R_NIOS2_NONE. */
14790 case EM_ARC: /* R_ARC_NONE. */
14791 case EM_ARC_COMPACT2: /* R_ARC_NONE. */
14792 case EM_ARC_COMPACT: /* R_ARC_NONE. */
14793 case EM_ARM: /* R_ARM_NONE. */
14794 case EM_CRIS: /* R_CRIS_NONE. */
14795 case EM_FT32: /* R_FT32_NONE. */
14796 case EM_IA_64: /* R_IA64_NONE. */
14797 case EM_K1OM: /* R_X86_64_NONE. */
14798 case EM_L1OM: /* R_X86_64_NONE. */
14799 case EM_M32R: /* R_M32R_NONE. */
14800 case EM_MIPS: /* R_MIPS_NONE. */
14801 case EM_MN10300: /* R_MN10300_NONE. */
14802 case EM_MOXIE: /* R_MOXIE_NONE. */
14803 case EM_NIOS32: /* R_NIOS_NONE. */
14804 case EM_OR1K: /* R_OR1K_NONE. */
14805 case EM_PARISC: /* R_PARISC_NONE. */
14806 case EM_PPC64: /* R_PPC64_NONE. */
14807 case EM_PPC: /* R_PPC_NONE. */
14808 case EM_RISCV: /* R_RISCV_NONE. */
14809 case EM_S390: /* R_390_NONE. */
14810 case EM_S390_OLD:
14811 case EM_SH: /* R_SH_NONE. */
14812 case EM_SPARC32PLUS:
14813 case EM_SPARC: /* R_SPARC_NONE. */
14814 case EM_SPARCV9:
14815 case EM_TILEGX: /* R_TILEGX_NONE. */
14816 case EM_TILEPRO: /* R_TILEPRO_NONE. */
14817 case EM_TI_C6000:/* R_C6000_NONE. */
14818 case EM_X86_64: /* R_X86_64_NONE. */
14819 case EM_Z80: /* R_Z80_NONE. */
14820 case EM_WEBASSEMBLY: /* R_WASM32_NONE. */
14821 return reloc_type == 0;
14822
14823 case EM_AARCH64:
14824 return reloc_type == 0 || reloc_type == 256;
14825 case EM_AVR_OLD:
14826 case EM_AVR:
14827 return (reloc_type == 0 /* R_AVR_NONE. */
14828 || reloc_type == 30 /* R_AVR_DIFF8. */
14829 || reloc_type == 31 /* R_AVR_DIFF16. */
14830 || reloc_type == 32 /* R_AVR_DIFF32. */);
14831 case EM_METAG:
14832 return reloc_type == 3; /* R_METAG_NONE. */
14833 case EM_NDS32:
14834 return (reloc_type == 0 /* R_NDS32_NONE. */
14835 || reloc_type == 205 /* R_NDS32_DIFF8. */
14836 || reloc_type == 206 /* R_NDS32_DIFF16. */
14837 || reloc_type == 207 /* R_NDS32_DIFF32. */
14838 || reloc_type == 208 /* R_NDS32_DIFF_ULEB128. */);
14839 case EM_TI_PRU:
14840 return (reloc_type == 0 /* R_PRU_NONE. */
14841 || reloc_type == 65 /* R_PRU_DIFF8. */
14842 || reloc_type == 66 /* R_PRU_DIFF16. */
14843 || reloc_type == 67 /* R_PRU_DIFF32. */);
14844 case EM_XTENSA_OLD:
14845 case EM_XTENSA:
14846 return (reloc_type == 0 /* R_XTENSA_NONE. */
14847 || reloc_type == 17 /* R_XTENSA_DIFF8. */
14848 || reloc_type == 18 /* R_XTENSA_DIFF16. */
14849 || reloc_type == 19 /* R_XTENSA_DIFF32. */
14850 || reloc_type == 57 /* R_XTENSA_PDIFF8. */
14851 || reloc_type == 58 /* R_XTENSA_PDIFF16. */
14852 || reloc_type == 59 /* R_XTENSA_PDIFF32. */
14853 || reloc_type == 60 /* R_XTENSA_NDIFF8. */
14854 || reloc_type == 61 /* R_XTENSA_NDIFF16. */
14855 || reloc_type == 62 /* R_XTENSA_NDIFF32. */);
14856 }
14857 return false;
14858 }
14859
14860 /* Returns TRUE if there is a relocation against
14861 section NAME at OFFSET bytes. */
14862
14863 bool
14864 reloc_at (struct dwarf_section * dsec, dwarf_vma offset)
14865 {
14866 Elf_Internal_Rela * relocs;
14867 Elf_Internal_Rela * rp;
14868
14869 if (dsec == NULL || dsec->reloc_info == NULL)
14870 return false;
14871
14872 relocs = (Elf_Internal_Rela *) dsec->reloc_info;
14873
14874 for (rp = relocs; rp < relocs + dsec->num_relocs; ++rp)
14875 if (rp->r_offset == offset)
14876 return true;
14877
14878 return false;
14879 }
14880
14881 /* Apply relocations to a section.
14882 Returns TRUE upon success, FALSE otherwise.
14883 If RELOCS_RETURN is non-NULL then it is set to point to the loaded relocs.
14884 It is then the caller's responsibility to free them. NUM_RELOCS_RETURN
14885 will be set to the number of relocs loaded.
14886
14887 Note: So far support has been added only for those relocations
14888 which can be found in debug sections. FIXME: Add support for
14889 more relocations ? */
14890
14891 static bool
14892 apply_relocations (Filedata * filedata,
14893 const Elf_Internal_Shdr * section,
14894 unsigned char * start,
14895 bfd_size_type size,
14896 void ** relocs_return,
14897 unsigned long * num_relocs_return)
14898 {
14899 Elf_Internal_Shdr * relsec;
14900 unsigned char * end = start + size;
14901
14902 if (relocs_return != NULL)
14903 {
14904 * (Elf_Internal_Rela **) relocs_return = NULL;
14905 * num_relocs_return = 0;
14906 }
14907
14908 if (filedata->file_header.e_type != ET_REL)
14909 /* No relocs to apply. */
14910 return true;
14911
14912 /* Find the reloc section associated with the section. */
14913 for (relsec = filedata->section_headers;
14914 relsec < filedata->section_headers + filedata->file_header.e_shnum;
14915 ++relsec)
14916 {
14917 bool is_rela;
14918 unsigned long num_relocs;
14919 Elf_Internal_Rela * relocs;
14920 Elf_Internal_Rela * rp;
14921 Elf_Internal_Shdr * symsec;
14922 Elf_Internal_Sym * symtab;
14923 unsigned long num_syms;
14924 Elf_Internal_Sym * sym;
14925
14926 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
14927 || relsec->sh_info >= filedata->file_header.e_shnum
14928 || filedata->section_headers + relsec->sh_info != section
14929 || relsec->sh_size == 0
14930 || relsec->sh_link >= filedata->file_header.e_shnum)
14931 continue;
14932
14933 symsec = filedata->section_headers + relsec->sh_link;
14934 if (symsec->sh_type != SHT_SYMTAB
14935 && symsec->sh_type != SHT_DYNSYM)
14936 return false;
14937
14938 is_rela = relsec->sh_type == SHT_RELA;
14939
14940 if (is_rela)
14941 {
14942 if (!slurp_rela_relocs (filedata, relsec->sh_offset,
14943 relsec->sh_size, & relocs, & num_relocs))
14944 return false;
14945 }
14946 else
14947 {
14948 if (!slurp_rel_relocs (filedata, relsec->sh_offset,
14949 relsec->sh_size, & relocs, & num_relocs))
14950 return false;
14951 }
14952
14953 /* SH uses RELA but uses in place value instead of the addend field. */
14954 if (filedata->file_header.e_machine == EM_SH)
14955 is_rela = false;
14956
14957 symtab = get_elf_symbols (filedata, symsec, & num_syms);
14958
14959 for (rp = relocs; rp < relocs + num_relocs; ++rp)
14960 {
14961 bfd_vma addend;
14962 unsigned int reloc_type;
14963 unsigned int reloc_size;
14964 bool reloc_inplace = false;
14965 bool reloc_subtract = false;
14966 unsigned char *rloc;
14967 unsigned long sym_index;
14968
14969 reloc_type = get_reloc_type (filedata, rp->r_info);
14970
14971 if (target_specific_reloc_handling (filedata, rp, start, end, symtab, num_syms))
14972 continue;
14973 else if (is_none_reloc (filedata, reloc_type))
14974 continue;
14975 else if (is_32bit_abs_reloc (filedata, reloc_type)
14976 || is_32bit_pcrel_reloc (filedata, reloc_type))
14977 reloc_size = 4;
14978 else if (is_64bit_abs_reloc (filedata, reloc_type)
14979 || is_64bit_pcrel_reloc (filedata, reloc_type))
14980 reloc_size = 8;
14981 else if (is_24bit_abs_reloc (filedata, reloc_type))
14982 reloc_size = 3;
14983 else if (is_16bit_abs_reloc (filedata, reloc_type))
14984 reloc_size = 2;
14985 else if (is_8bit_abs_reloc (filedata, reloc_type)
14986 || is_6bit_abs_reloc (filedata, reloc_type))
14987 reloc_size = 1;
14988 else if ((reloc_subtract = is_32bit_inplace_sub_reloc (filedata,
14989 reloc_type))
14990 || is_32bit_inplace_add_reloc (filedata, reloc_type))
14991 {
14992 reloc_size = 4;
14993 reloc_inplace = true;
14994 }
14995 else if ((reloc_subtract = is_64bit_inplace_sub_reloc (filedata,
14996 reloc_type))
14997 || is_64bit_inplace_add_reloc (filedata, reloc_type))
14998 {
14999 reloc_size = 8;
15000 reloc_inplace = true;
15001 }
15002 else if ((reloc_subtract = is_16bit_inplace_sub_reloc (filedata,
15003 reloc_type))
15004 || is_16bit_inplace_add_reloc (filedata, reloc_type))
15005 {
15006 reloc_size = 2;
15007 reloc_inplace = true;
15008 }
15009 else if ((reloc_subtract = is_8bit_inplace_sub_reloc (filedata,
15010 reloc_type))
15011 || is_8bit_inplace_add_reloc (filedata, reloc_type))
15012 {
15013 reloc_size = 1;
15014 reloc_inplace = true;
15015 }
15016 else if ((reloc_subtract = is_6bit_inplace_sub_reloc (filedata,
15017 reloc_type)))
15018 {
15019 reloc_size = 1;
15020 reloc_inplace = true;
15021 }
15022 else
15023 {
15024 static unsigned int prev_reloc = 0;
15025
15026 if (reloc_type != prev_reloc)
15027 warn (_("unable to apply unsupported reloc type %d to section %s\n"),
15028 reloc_type, printable_section_name (filedata, section));
15029 prev_reloc = reloc_type;
15030 continue;
15031 }
15032
15033 rloc = start + rp->r_offset;
15034 if (!IN_RANGE (start, end, rloc, reloc_size))
15035 {
15036 warn (_("skipping invalid relocation offset 0x%lx in section %s\n"),
15037 (unsigned long) rp->r_offset,
15038 printable_section_name (filedata, section));
15039 continue;
15040 }
15041
15042 sym_index = (unsigned long) get_reloc_symindex (rp->r_info);
15043 if (sym_index >= num_syms)
15044 {
15045 warn (_("skipping invalid relocation symbol index 0x%lx in section %s\n"),
15046 sym_index, printable_section_name (filedata, section));
15047 continue;
15048 }
15049 sym = symtab + sym_index;
15050
15051 /* If the reloc has a symbol associated with it,
15052 make sure that it is of an appropriate type.
15053
15054 Relocations against symbols without type can happen.
15055 Gcc -feliminate-dwarf2-dups may generate symbols
15056 without type for debug info.
15057
15058 Icc generates relocations against function symbols
15059 instead of local labels.
15060
15061 Relocations against object symbols can happen, eg when
15062 referencing a global array. For an example of this see
15063 the _clz.o binary in libgcc.a. */
15064 if (sym != symtab
15065 && ELF_ST_TYPE (sym->st_info) != STT_COMMON
15066 && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
15067 {
15068 warn (_("skipping unexpected symbol type %s in section %s relocation %ld\n"),
15069 get_symbol_type (filedata, ELF_ST_TYPE (sym->st_info)),
15070 printable_section_name (filedata, relsec),
15071 (long int)(rp - relocs));
15072 continue;
15073 }
15074
15075 addend = 0;
15076 if (is_rela)
15077 addend += rp->r_addend;
15078 /* R_XTENSA_32, R_PJ_DATA_DIR32 and R_D30V_32_NORMAL are
15079 partial_inplace. */
15080 if (!is_rela
15081 || (filedata->file_header.e_machine == EM_XTENSA
15082 && reloc_type == 1)
15083 || ((filedata->file_header.e_machine == EM_PJ
15084 || filedata->file_header.e_machine == EM_PJ_OLD)
15085 && reloc_type == 1)
15086 || ((filedata->file_header.e_machine == EM_D30V
15087 || filedata->file_header.e_machine == EM_CYGNUS_D30V)
15088 && reloc_type == 12)
15089 || reloc_inplace)
15090 {
15091 if (is_6bit_inplace_sub_reloc (filedata, reloc_type))
15092 addend += byte_get (rloc, reloc_size) & 0x3f;
15093 else
15094 addend += byte_get (rloc, reloc_size);
15095 }
15096
15097 if (is_32bit_pcrel_reloc (filedata, reloc_type)
15098 || is_64bit_pcrel_reloc (filedata, reloc_type))
15099 {
15100 /* On HPPA, all pc-relative relocations are biased by 8. */
15101 if (filedata->file_header.e_machine == EM_PARISC)
15102 addend -= 8;
15103 byte_put (rloc, (addend + sym->st_value) - rp->r_offset,
15104 reloc_size);
15105 }
15106 else if (is_6bit_abs_reloc (filedata, reloc_type)
15107 || is_6bit_inplace_sub_reloc (filedata, reloc_type))
15108 {
15109 if (reloc_subtract)
15110 addend -= sym->st_value;
15111 else
15112 addend += sym->st_value;
15113 addend = (addend & 0x3f) | (byte_get (rloc, reloc_size) & 0xc0);
15114 byte_put (rloc, addend, reloc_size);
15115 }
15116 else if (reloc_subtract)
15117 byte_put (rloc, addend - sym->st_value, reloc_size);
15118 else
15119 byte_put (rloc, addend + sym->st_value, reloc_size);
15120 }
15121
15122 free (symtab);
15123 /* Let the target specific reloc processing code know that
15124 we have finished with these relocs. */
15125 target_specific_reloc_handling (filedata, NULL, NULL, NULL, NULL, 0);
15126
15127 if (relocs_return)
15128 {
15129 * (Elf_Internal_Rela **) relocs_return = relocs;
15130 * num_relocs_return = num_relocs;
15131 }
15132 else
15133 free (relocs);
15134
15135 break;
15136 }
15137
15138 return true;
15139 }
15140
15141 #ifdef SUPPORT_DISASSEMBLY
15142 static bool
15143 disassemble_section (Elf_Internal_Shdr * section, Filedata * filedata)
15144 {
15145 printf (_("\nAssembly dump of section %s\n"), printable_section_name (filedata, section));
15146
15147 /* FIXME: XXX -- to be done --- XXX */
15148
15149 return true;
15150 }
15151 #endif
15152
15153 /* Reads in the contents of SECTION from FILE, returning a pointer
15154 to a malloc'ed buffer or NULL if something went wrong. */
15155
15156 static char *
15157 get_section_contents (Elf_Internal_Shdr * section, Filedata * filedata)
15158 {
15159 bfd_size_type num_bytes = section->sh_size;
15160
15161 if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
15162 {
15163 printf (_("Section '%s' has no data to dump.\n"),
15164 printable_section_name (filedata, section));
15165 return NULL;
15166 }
15167
15168 return (char *) get_data (NULL, filedata, section->sh_offset, 1, num_bytes,
15169 _("section contents"));
15170 }
15171
15172 /* Uncompresses a section that was compressed using zlib, in place. */
15173
15174 static bool
15175 uncompress_section_contents (unsigned char ** buffer,
15176 dwarf_size_type uncompressed_size,
15177 dwarf_size_type * size)
15178 {
15179 dwarf_size_type compressed_size = *size;
15180 unsigned char * compressed_buffer = *buffer;
15181 unsigned char * uncompressed_buffer;
15182 z_stream strm;
15183 int rc;
15184
15185 /* It is possible the section consists of several compressed
15186 buffers concatenated together, so we uncompress in a loop. */
15187 /* PR 18313: The state field in the z_stream structure is supposed
15188 to be invisible to the user (ie us), but some compilers will
15189 still complain about it being used without initialisation. So
15190 we first zero the entire z_stream structure and then set the fields
15191 that we need. */
15192 memset (& strm, 0, sizeof strm);
15193 strm.avail_in = compressed_size;
15194 strm.next_in = (Bytef *) compressed_buffer;
15195 strm.avail_out = uncompressed_size;
15196 uncompressed_buffer = (unsigned char *) xmalloc (uncompressed_size);
15197
15198 rc = inflateInit (& strm);
15199 while (strm.avail_in > 0)
15200 {
15201 if (rc != Z_OK)
15202 break;
15203 strm.next_out = ((Bytef *) uncompressed_buffer
15204 + (uncompressed_size - strm.avail_out));
15205 rc = inflate (&strm, Z_FINISH);
15206 if (rc != Z_STREAM_END)
15207 break;
15208 rc = inflateReset (& strm);
15209 }
15210 if (inflateEnd (& strm) != Z_OK
15211 || rc != Z_OK
15212 || strm.avail_out != 0)
15213 goto fail;
15214
15215 *buffer = uncompressed_buffer;
15216 *size = uncompressed_size;
15217 return true;
15218
15219 fail:
15220 free (uncompressed_buffer);
15221 /* Indicate decompression failure. */
15222 *buffer = NULL;
15223 return false;
15224 }
15225
15226 static bool
15227 dump_section_as_strings (Elf_Internal_Shdr * section, Filedata * filedata)
15228 {
15229 Elf_Internal_Shdr *relsec;
15230 bfd_size_type num_bytes;
15231 unsigned char *data;
15232 unsigned char *end;
15233 unsigned char *real_start;
15234 unsigned char *start;
15235 bool some_strings_shown;
15236
15237 real_start = start = (unsigned char *) get_section_contents (section, filedata);
15238 if (start == NULL)
15239 /* PR 21820: Do not fail if the section was empty. */
15240 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
15241
15242 num_bytes = section->sh_size;
15243
15244 if (filedata->is_separate)
15245 printf (_("\nString dump of section '%s' in linked file %s:\n"),
15246 printable_section_name (filedata, section),
15247 filedata->file_name);
15248 else
15249 printf (_("\nString dump of section '%s':\n"),
15250 printable_section_name (filedata, section));
15251
15252 if (decompress_dumps)
15253 {
15254 dwarf_size_type new_size = num_bytes;
15255 dwarf_size_type uncompressed_size = 0;
15256
15257 if ((section->sh_flags & SHF_COMPRESSED) != 0)
15258 {
15259 Elf_Internal_Chdr chdr;
15260 unsigned int compression_header_size
15261 = get_compression_header (& chdr, (unsigned char *) start,
15262 num_bytes);
15263 if (compression_header_size == 0)
15264 /* An error message will have already been generated
15265 by get_compression_header. */
15266 goto error_out;
15267
15268 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
15269 {
15270 warn (_("section '%s' has unsupported compress type: %d\n"),
15271 printable_section_name (filedata, section), chdr.ch_type);
15272 goto error_out;
15273 }
15274 uncompressed_size = chdr.ch_size;
15275 start += compression_header_size;
15276 new_size -= compression_header_size;
15277 }
15278 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
15279 {
15280 /* Read the zlib header. In this case, it should be "ZLIB"
15281 followed by the uncompressed section size, 8 bytes in
15282 big-endian order. */
15283 uncompressed_size = start[4]; uncompressed_size <<= 8;
15284 uncompressed_size += start[5]; uncompressed_size <<= 8;
15285 uncompressed_size += start[6]; uncompressed_size <<= 8;
15286 uncompressed_size += start[7]; uncompressed_size <<= 8;
15287 uncompressed_size += start[8]; uncompressed_size <<= 8;
15288 uncompressed_size += start[9]; uncompressed_size <<= 8;
15289 uncompressed_size += start[10]; uncompressed_size <<= 8;
15290 uncompressed_size += start[11];
15291 start += 12;
15292 new_size -= 12;
15293 }
15294
15295 if (uncompressed_size)
15296 {
15297 if (uncompress_section_contents (& start,
15298 uncompressed_size, & new_size))
15299 num_bytes = new_size;
15300 else
15301 {
15302 error (_("Unable to decompress section %s\n"),
15303 printable_section_name (filedata, section));
15304 goto error_out;
15305 }
15306 }
15307 else
15308 start = real_start;
15309 }
15310
15311 /* If the section being dumped has relocations against it the user might
15312 be expecting these relocations to have been applied. Check for this
15313 case and issue a warning message in order to avoid confusion.
15314 FIXME: Maybe we ought to have an option that dumps a section with
15315 relocs applied ? */
15316 for (relsec = filedata->section_headers;
15317 relsec < filedata->section_headers + filedata->file_header.e_shnum;
15318 ++relsec)
15319 {
15320 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
15321 || relsec->sh_info >= filedata->file_header.e_shnum
15322 || filedata->section_headers + relsec->sh_info != section
15323 || relsec->sh_size == 0
15324 || relsec->sh_link >= filedata->file_header.e_shnum)
15325 continue;
15326
15327 printf (_(" Note: This section has relocations against it, but these have NOT been applied to this dump.\n"));
15328 break;
15329 }
15330
15331 data = start;
15332 end = start + num_bytes;
15333 some_strings_shown = false;
15334
15335 #ifdef HAVE_MBSTATE_T
15336 mbstate_t state;
15337 /* Initialise the multibyte conversion state. */
15338 memset (& state, 0, sizeof (state));
15339 #endif
15340
15341 bool continuing = false;
15342
15343 while (data < end)
15344 {
15345 while (!ISPRINT (* data))
15346 if (++ data >= end)
15347 break;
15348
15349 if (data < end)
15350 {
15351 size_t maxlen = end - data;
15352
15353 if (continuing)
15354 {
15355 printf (" ");
15356 continuing = false;
15357 }
15358 else
15359 {
15360 printf (" [%6lx] ", (unsigned long) (data - start));
15361 }
15362
15363 if (maxlen > 0)
15364 {
15365 char c = 0;
15366
15367 while (maxlen)
15368 {
15369 c = *data++;
15370
15371 if (c == 0)
15372 break;
15373
15374 /* PR 25543: Treat new-lines as string-ending characters. */
15375 if (c == '\n')
15376 {
15377 printf ("\\n\n");
15378 if (*data != 0)
15379 continuing = true;
15380 break;
15381 }
15382
15383 /* Do not print control characters directly as they can affect terminal
15384 settings. Such characters usually appear in the names generated
15385 by the assembler for local labels. */
15386 if (ISCNTRL (c))
15387 {
15388 printf ("^%c", c + 0x40);
15389 }
15390 else if (ISPRINT (c))
15391 {
15392 putchar (c);
15393 }
15394 else
15395 {
15396 size_t n;
15397 #ifdef HAVE_MBSTATE_T
15398 wchar_t w;
15399 #endif
15400 /* Let printf do the hard work of displaying multibyte characters. */
15401 printf ("%.1s", data - 1);
15402 #ifdef HAVE_MBSTATE_T
15403 /* Try to find out how many bytes made up the character that was
15404 just printed. Advance the symbol pointer past the bytes that
15405 were displayed. */
15406 n = mbrtowc (& w, (char *)(data - 1), MB_CUR_MAX, & state);
15407 #else
15408 n = 1;
15409 #endif
15410 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
15411 data += (n - 1);
15412 }
15413 }
15414
15415 if (c != '\n')
15416 putchar ('\n');
15417 }
15418 else
15419 {
15420 printf (_("<corrupt>\n"));
15421 data = end;
15422 }
15423 some_strings_shown = true;
15424 }
15425 }
15426
15427 if (! some_strings_shown)
15428 printf (_(" No strings found in this section."));
15429
15430 free (real_start);
15431
15432 putchar ('\n');
15433 return true;
15434
15435 error_out:
15436 free (real_start);
15437 return false;
15438 }
15439
15440 static bool
15441 dump_section_as_bytes (Elf_Internal_Shdr *section,
15442 Filedata *filedata,
15443 bool relocate)
15444 {
15445 Elf_Internal_Shdr * relsec;
15446 bfd_size_type bytes;
15447 bfd_size_type section_size;
15448 bfd_vma addr;
15449 unsigned char * data;
15450 unsigned char * real_start;
15451 unsigned char * start;
15452
15453 real_start = start = (unsigned char *) get_section_contents (section, filedata);
15454 if (start == NULL)
15455 /* PR 21820: Do not fail if the section was empty. */
15456 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
15457
15458 section_size = section->sh_size;
15459
15460 if (filedata->is_separate)
15461 printf (_("\nHex dump of section '%s' in linked file %s:\n"),
15462 printable_section_name (filedata, section),
15463 filedata->file_name);
15464 else
15465 printf (_("\nHex dump of section '%s':\n"),
15466 printable_section_name (filedata, section));
15467
15468 if (decompress_dumps)
15469 {
15470 dwarf_size_type new_size = section_size;
15471 dwarf_size_type uncompressed_size = 0;
15472
15473 if ((section->sh_flags & SHF_COMPRESSED) != 0)
15474 {
15475 Elf_Internal_Chdr chdr;
15476 unsigned int compression_header_size
15477 = get_compression_header (& chdr, start, section_size);
15478
15479 if (compression_header_size == 0)
15480 /* An error message will have already been generated
15481 by get_compression_header. */
15482 goto error_out;
15483
15484 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
15485 {
15486 warn (_("section '%s' has unsupported compress type: %d\n"),
15487 printable_section_name (filedata, section), chdr.ch_type);
15488 goto error_out;
15489 }
15490 uncompressed_size = chdr.ch_size;
15491 start += compression_header_size;
15492 new_size -= compression_header_size;
15493 }
15494 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
15495 {
15496 /* Read the zlib header. In this case, it should be "ZLIB"
15497 followed by the uncompressed section size, 8 bytes in
15498 big-endian order. */
15499 uncompressed_size = start[4]; uncompressed_size <<= 8;
15500 uncompressed_size += start[5]; uncompressed_size <<= 8;
15501 uncompressed_size += start[6]; uncompressed_size <<= 8;
15502 uncompressed_size += start[7]; uncompressed_size <<= 8;
15503 uncompressed_size += start[8]; uncompressed_size <<= 8;
15504 uncompressed_size += start[9]; uncompressed_size <<= 8;
15505 uncompressed_size += start[10]; uncompressed_size <<= 8;
15506 uncompressed_size += start[11];
15507 start += 12;
15508 new_size -= 12;
15509 }
15510
15511 if (uncompressed_size)
15512 {
15513 if (uncompress_section_contents (& start, uncompressed_size,
15514 & new_size))
15515 {
15516 section_size = new_size;
15517 }
15518 else
15519 {
15520 error (_("Unable to decompress section %s\n"),
15521 printable_section_name (filedata, section));
15522 /* FIXME: Print the section anyway ? */
15523 goto error_out;
15524 }
15525 }
15526 else
15527 start = real_start;
15528 }
15529
15530 if (relocate)
15531 {
15532 if (! apply_relocations (filedata, section, start, section_size, NULL, NULL))
15533 goto error_out;
15534 }
15535 else
15536 {
15537 /* If the section being dumped has relocations against it the user might
15538 be expecting these relocations to have been applied. Check for this
15539 case and issue a warning message in order to avoid confusion.
15540 FIXME: Maybe we ought to have an option that dumps a section with
15541 relocs applied ? */
15542 for (relsec = filedata->section_headers;
15543 relsec < filedata->section_headers + filedata->file_header.e_shnum;
15544 ++relsec)
15545 {
15546 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
15547 || relsec->sh_info >= filedata->file_header.e_shnum
15548 || filedata->section_headers + relsec->sh_info != section
15549 || relsec->sh_size == 0
15550 || relsec->sh_link >= filedata->file_header.e_shnum)
15551 continue;
15552
15553 printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
15554 break;
15555 }
15556 }
15557
15558 addr = section->sh_addr;
15559 bytes = section_size;
15560 data = start;
15561
15562 while (bytes)
15563 {
15564 int j;
15565 int k;
15566 int lbytes;
15567
15568 lbytes = (bytes > 16 ? 16 : bytes);
15569
15570 printf (" 0x%8.8lx ", (unsigned long) addr);
15571
15572 for (j = 0; j < 16; j++)
15573 {
15574 if (j < lbytes)
15575 printf ("%2.2x", data[j]);
15576 else
15577 printf (" ");
15578
15579 if ((j & 3) == 3)
15580 printf (" ");
15581 }
15582
15583 for (j = 0; j < lbytes; j++)
15584 {
15585 k = data[j];
15586 if (k >= ' ' && k < 0x7f)
15587 printf ("%c", k);
15588 else
15589 printf (".");
15590 }
15591
15592 putchar ('\n');
15593
15594 data += lbytes;
15595 addr += lbytes;
15596 bytes -= lbytes;
15597 }
15598
15599 free (real_start);
15600
15601 putchar ('\n');
15602 return true;
15603
15604 error_out:
15605 free (real_start);
15606 return false;
15607 }
15608
15609 #ifdef ENABLE_LIBCTF
15610 static ctf_sect_t *
15611 shdr_to_ctf_sect (ctf_sect_t *buf, Elf_Internal_Shdr *shdr, Filedata *filedata)
15612 {
15613 buf->cts_name = section_name_print (filedata, shdr);
15614 buf->cts_size = shdr->sh_size;
15615 buf->cts_entsize = shdr->sh_entsize;
15616
15617 return buf;
15618 }
15619
15620 /* Formatting callback function passed to ctf_dump. Returns either the pointer
15621 it is passed, or a pointer to newly-allocated storage, in which case
15622 dump_ctf() will free it when it no longer needs it. */
15623
15624 static char *
15625 dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
15626 char *s, void *arg)
15627 {
15628 const char *blanks = arg;
15629 char *new_s;
15630
15631 if (asprintf (&new_s, "%s%s", blanks, s) < 0)
15632 return s;
15633 return new_s;
15634 }
15635
15636 /* Dump CTF errors/warnings. */
15637 static void
15638 dump_ctf_errs (ctf_dict_t *fp)
15639 {
15640 ctf_next_t *it = NULL;
15641 char *errtext;
15642 int is_warning;
15643 int err;
15644
15645 /* Dump accumulated errors and warnings. */
15646 while ((errtext = ctf_errwarning_next (fp, &it, &is_warning, &err)) != NULL)
15647 {
15648 error (_("%s: %s"), is_warning ? _("warning"): _("error"),
15649 errtext);
15650 free (errtext);
15651 }
15652 if (err != ECTF_NEXT_END)
15653 error (_("CTF error: cannot get CTF errors: `%s'"), ctf_errmsg (err));
15654 }
15655
15656 /* Dump one CTF archive member. */
15657
15658 static void
15659 dump_ctf_archive_member (ctf_dict_t *ctf, const char *name, ctf_dict_t *parent,
15660 size_t member)
15661 {
15662 const char *things[] = {"Header", "Labels", "Data objects",
15663 "Function objects", "Variables", "Types", "Strings",
15664 ""};
15665 const char **thing;
15666 size_t i;
15667
15668 /* Don't print out the name of the default-named archive member if it appears
15669 first in the list. The name .ctf appears everywhere, even for things that
15670 aren't really archives, so printing it out is liable to be confusing; also,
15671 the common case by far is for only one archive member to exist, and hiding
15672 it in that case seems worthwhile. */
15673
15674 if (strcmp (name, ".ctf") != 0 || member != 0)
15675 printf (_("\nCTF archive member: %s:\n"), name);
15676
15677 if (ctf_parent_name (ctf) != NULL)
15678 ctf_import (ctf, parent);
15679
15680 for (i = 0, thing = things; *thing[0]; thing++, i++)
15681 {
15682 ctf_dump_state_t *s = NULL;
15683 char *item;
15684
15685 printf ("\n %s:\n", *thing);
15686 while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
15687 (void *) " ")) != NULL)
15688 {
15689 printf ("%s\n", item);
15690 free (item);
15691 }
15692
15693 if (ctf_errno (ctf))
15694 {
15695 error (_("Iteration failed: %s, %s\n"), *thing,
15696 ctf_errmsg (ctf_errno (ctf)));
15697 break;
15698 }
15699 }
15700
15701 dump_ctf_errs (ctf);
15702 }
15703
15704 static bool
15705 dump_section_as_ctf (Elf_Internal_Shdr * section, Filedata * filedata)
15706 {
15707 Elf_Internal_Shdr * symtab_sec = NULL;
15708 Elf_Internal_Shdr * strtab_sec = NULL;
15709 void * data = NULL;
15710 void * symdata = NULL;
15711 void * strdata = NULL;
15712 ctf_sect_t ctfsect, symsect, strsect;
15713 ctf_sect_t * symsectp = NULL;
15714 ctf_sect_t * strsectp = NULL;
15715 ctf_archive_t * ctfa = NULL;
15716 ctf_dict_t * parent = NULL;
15717 ctf_dict_t * fp;
15718
15719 ctf_next_t *i = NULL;
15720 const char *name;
15721 size_t member = 0;
15722 int err;
15723 bool ret = false;
15724
15725 shdr_to_ctf_sect (&ctfsect, section, filedata);
15726 data = get_section_contents (section, filedata);
15727 ctfsect.cts_data = data;
15728
15729 if (!dump_ctf_symtab_name)
15730 dump_ctf_symtab_name = strdup (".dynsym");
15731
15732 if (!dump_ctf_strtab_name)
15733 dump_ctf_strtab_name = strdup (".dynstr");
15734
15735 if (dump_ctf_symtab_name && dump_ctf_symtab_name[0] != 0)
15736 {
15737 if ((symtab_sec = find_section (filedata, dump_ctf_symtab_name)) == NULL)
15738 {
15739 error (_("No symbol section named %s\n"), dump_ctf_symtab_name);
15740 goto fail;
15741 }
15742 if ((symdata = (void *) get_data (NULL, filedata,
15743 symtab_sec->sh_offset, 1,
15744 symtab_sec->sh_size,
15745 _("symbols"))) == NULL)
15746 goto fail;
15747 symsectp = shdr_to_ctf_sect (&symsect, symtab_sec, filedata);
15748 symsect.cts_data = symdata;
15749 }
15750
15751 if (dump_ctf_strtab_name && dump_ctf_strtab_name[0] != 0)
15752 {
15753 if ((strtab_sec = find_section (filedata, dump_ctf_strtab_name)) == NULL)
15754 {
15755 error (_("No string table section named %s\n"),
15756 dump_ctf_strtab_name);
15757 goto fail;
15758 }
15759 if ((strdata = (void *) get_data (NULL, filedata,
15760 strtab_sec->sh_offset, 1,
15761 strtab_sec->sh_size,
15762 _("strings"))) == NULL)
15763 goto fail;
15764 strsectp = shdr_to_ctf_sect (&strsect, strtab_sec, filedata);
15765 strsect.cts_data = strdata;
15766 }
15767
15768 /* Load the CTF file and dump it. It may be a raw CTF section, or an archive:
15769 libctf papers over the difference, so we can pretend it is always an
15770 archive. */
15771
15772 if ((ctfa = ctf_arc_bufopen (&ctfsect, symsectp, strsectp, &err)) == NULL)
15773 {
15774 dump_ctf_errs (NULL);
15775 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15776 goto fail;
15777 }
15778
15779 ctf_arc_symsect_endianness (ctfa, filedata->file_header.e_ident[EI_DATA]
15780 != ELFDATA2MSB);
15781
15782 /* Preload the parent dict, since it will need to be imported into every
15783 child in turn. */
15784 if ((parent = ctf_dict_open (ctfa, dump_ctf_parent_name, &err)) == NULL)
15785 {
15786 dump_ctf_errs (NULL);
15787 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15788 goto fail;
15789 }
15790
15791 ret = true;
15792
15793 if (filedata->is_separate)
15794 printf (_("\nDump of CTF section '%s' in linked file %s:\n"),
15795 printable_section_name (filedata, section),
15796 filedata->file_name);
15797 else
15798 printf (_("\nDump of CTF section '%s':\n"),
15799 printable_section_name (filedata, section));
15800
15801 while ((fp = ctf_archive_next (ctfa, &i, &name, 0, &err)) != NULL)
15802 dump_ctf_archive_member (fp, name, parent, member++);
15803 if (err != ECTF_NEXT_END)
15804 {
15805 dump_ctf_errs (NULL);
15806 error (_("CTF member open failure: %s\n"), ctf_errmsg (err));
15807 ret = false;
15808 }
15809
15810 fail:
15811 ctf_dict_close (parent);
15812 ctf_close (ctfa);
15813 free (data);
15814 free (symdata);
15815 free (strdata);
15816 return ret;
15817 }
15818 #endif
15819
15820 static bool
15821 load_specific_debug_section (enum dwarf_section_display_enum debug,
15822 const Elf_Internal_Shdr * sec,
15823 void * data)
15824 {
15825 struct dwarf_section * section = &debug_displays [debug].section;
15826 char buf [64];
15827 Filedata * filedata = (Filedata *) data;
15828
15829 if (section->start != NULL)
15830 {
15831 /* If it is already loaded, do nothing. */
15832 if (streq (section->filename, filedata->file_name))
15833 return true;
15834 free (section->start);
15835 }
15836
15837 snprintf (buf, sizeof (buf), _("%s section data"), section->name);
15838 section->address = sec->sh_addr;
15839 section->filename = filedata->file_name;
15840 section->start = (unsigned char *) get_data (NULL, filedata,
15841 sec->sh_offset, 1,
15842 sec->sh_size, buf);
15843 if (section->start == NULL)
15844 section->size = 0;
15845 else
15846 {
15847 unsigned char *start = section->start;
15848 dwarf_size_type size = sec->sh_size;
15849 dwarf_size_type uncompressed_size = 0;
15850
15851 if ((sec->sh_flags & SHF_COMPRESSED) != 0)
15852 {
15853 Elf_Internal_Chdr chdr;
15854 unsigned int compression_header_size;
15855
15856 if (size < (is_32bit_elf
15857 ? sizeof (Elf32_External_Chdr)
15858 : sizeof (Elf64_External_Chdr)))
15859 {
15860 warn (_("compressed section %s is too small to contain a compression header\n"),
15861 section->name);
15862 return false;
15863 }
15864
15865 compression_header_size = get_compression_header (&chdr, start, size);
15866 if (compression_header_size == 0)
15867 /* An error message will have already been generated
15868 by get_compression_header. */
15869 return false;
15870
15871 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
15872 {
15873 warn (_("section '%s' has unsupported compress type: %d\n"),
15874 section->name, chdr.ch_type);
15875 return false;
15876 }
15877 uncompressed_size = chdr.ch_size;
15878 start += compression_header_size;
15879 size -= compression_header_size;
15880 }
15881 else if (size > 12 && streq ((char *) start, "ZLIB"))
15882 {
15883 /* Read the zlib header. In this case, it should be "ZLIB"
15884 followed by the uncompressed section size, 8 bytes in
15885 big-endian order. */
15886 uncompressed_size = start[4]; uncompressed_size <<= 8;
15887 uncompressed_size += start[5]; uncompressed_size <<= 8;
15888 uncompressed_size += start[6]; uncompressed_size <<= 8;
15889 uncompressed_size += start[7]; uncompressed_size <<= 8;
15890 uncompressed_size += start[8]; uncompressed_size <<= 8;
15891 uncompressed_size += start[9]; uncompressed_size <<= 8;
15892 uncompressed_size += start[10]; uncompressed_size <<= 8;
15893 uncompressed_size += start[11];
15894 start += 12;
15895 size -= 12;
15896 }
15897
15898 if (uncompressed_size)
15899 {
15900 if (uncompress_section_contents (&start, uncompressed_size,
15901 &size))
15902 {
15903 /* Free the compressed buffer, update the section buffer
15904 and the section size if uncompress is successful. */
15905 free (section->start);
15906 section->start = start;
15907 }
15908 else
15909 {
15910 error (_("Unable to decompress section %s\n"),
15911 printable_section_name (filedata, sec));
15912 return false;
15913 }
15914 }
15915
15916 section->size = size;
15917 }
15918
15919 if (section->start == NULL)
15920 return false;
15921
15922 if (debug_displays [debug].relocate)
15923 {
15924 if (! apply_relocations (filedata, sec, section->start, section->size,
15925 & section->reloc_info, & section->num_relocs))
15926 return false;
15927 }
15928 else
15929 {
15930 section->reloc_info = NULL;
15931 section->num_relocs = 0;
15932 }
15933
15934 return true;
15935 }
15936
15937 #if HAVE_LIBDEBUGINFOD
15938 /* Return a hex string representation of the build-id. */
15939 unsigned char *
15940 get_build_id (void * data)
15941 {
15942 Filedata * filedata = (Filedata *) data;
15943 Elf_Internal_Shdr * shdr;
15944 unsigned long i;
15945
15946 /* Iterate through notes to find note.gnu.build-id.
15947 FIXME: Only the first note in any note section is examined. */
15948 for (i = 0, shdr = filedata->section_headers;
15949 i < filedata->file_header.e_shnum && shdr != NULL;
15950 i++, shdr++)
15951 {
15952 if (shdr->sh_type != SHT_NOTE)
15953 continue;
15954
15955 char * next;
15956 char * end;
15957 size_t data_remaining;
15958 size_t min_notesz;
15959 Elf_External_Note * enote;
15960 Elf_Internal_Note inote;
15961
15962 bfd_vma offset = shdr->sh_offset;
15963 bfd_vma align = shdr->sh_addralign;
15964 bfd_vma length = shdr->sh_size;
15965
15966 enote = (Elf_External_Note *) get_section_contents (shdr, filedata);
15967 if (enote == NULL)
15968 continue;
15969
15970 if (align < 4)
15971 align = 4;
15972 else if (align != 4 && align != 8)
15973 {
15974 free (enote);
15975 continue;
15976 }
15977
15978 end = (char *) enote + length;
15979 data_remaining = end - (char *) enote;
15980
15981 if (!is_ia64_vms (filedata))
15982 {
15983 min_notesz = offsetof (Elf_External_Note, name);
15984 if (data_remaining < min_notesz)
15985 {
15986 warn (_("\
15987 malformed note encountered in section %s whilst scanning for build-id note\n"),
15988 printable_section_name (filedata, shdr));
15989 free (enote);
15990 continue;
15991 }
15992 data_remaining -= min_notesz;
15993
15994 inote.type = BYTE_GET (enote->type);
15995 inote.namesz = BYTE_GET (enote->namesz);
15996 inote.namedata = enote->name;
15997 inote.descsz = BYTE_GET (enote->descsz);
15998 inote.descdata = ((char *) enote
15999 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
16000 inote.descpos = offset + (inote.descdata - (char *) enote);
16001 next = ((char *) enote
16002 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
16003 }
16004 else
16005 {
16006 Elf64_External_VMS_Note *vms_enote;
16007
16008 /* PR binutils/15191
16009 Make sure that there is enough data to read. */
16010 min_notesz = offsetof (Elf64_External_VMS_Note, name);
16011 if (data_remaining < min_notesz)
16012 {
16013 warn (_("\
16014 malformed note encountered in section %s whilst scanning for build-id note\n"),
16015 printable_section_name (filedata, shdr));
16016 free (enote);
16017 continue;
16018 }
16019 data_remaining -= min_notesz;
16020
16021 vms_enote = (Elf64_External_VMS_Note *) enote;
16022 inote.type = BYTE_GET (vms_enote->type);
16023 inote.namesz = BYTE_GET (vms_enote->namesz);
16024 inote.namedata = vms_enote->name;
16025 inote.descsz = BYTE_GET (vms_enote->descsz);
16026 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
16027 inote.descpos = offset + (inote.descdata - (char *) enote);
16028 next = inote.descdata + align_power (inote.descsz, 3);
16029 }
16030
16031 /* Skip malformed notes. */
16032 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
16033 || (size_t) (inote.descdata - inote.namedata) > data_remaining
16034 || (size_t) (next - inote.descdata) < inote.descsz
16035 || ((size_t) (next - inote.descdata)
16036 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
16037 {
16038 warn (_("\
16039 malformed note encountered in section %s whilst scanning for build-id note\n"),
16040 printable_section_name (filedata, shdr));
16041 free (enote);
16042 continue;
16043 }
16044
16045 /* Check if this is the build-id note. If so then convert the build-id
16046 bytes to a hex string. */
16047 if (inote.namesz > 0
16048 && startswith (inote.namedata, "GNU")
16049 && inote.type == NT_GNU_BUILD_ID)
16050 {
16051 unsigned long j;
16052 char * build_id;
16053
16054 build_id = malloc (inote.descsz * 2 + 1);
16055 if (build_id == NULL)
16056 {
16057 free (enote);
16058 return NULL;
16059 }
16060
16061 for (j = 0; j < inote.descsz; ++j)
16062 sprintf (build_id + (j * 2), "%02x", inote.descdata[j] & 0xff);
16063 build_id[inote.descsz * 2] = '\0';
16064 free (enote);
16065
16066 return (unsigned char *) build_id;
16067 }
16068 free (enote);
16069 }
16070
16071 return NULL;
16072 }
16073 #endif /* HAVE_LIBDEBUGINFOD */
16074
16075 /* If this is not NULL, load_debug_section will only look for sections
16076 within the list of sections given here. */
16077 static unsigned int * section_subset = NULL;
16078
16079 bool
16080 load_debug_section (enum dwarf_section_display_enum debug, void * data)
16081 {
16082 struct dwarf_section * section = &debug_displays [debug].section;
16083 Elf_Internal_Shdr * sec;
16084 Filedata * filedata = (Filedata *) data;
16085
16086 if (!dump_any_debugging)
16087 return false;
16088
16089 /* Without section headers we cannot find any sections. */
16090 if (filedata->section_headers == NULL)
16091 return false;
16092
16093 if (filedata->string_table == NULL
16094 && filedata->file_header.e_shstrndx != SHN_UNDEF
16095 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
16096 {
16097 Elf_Internal_Shdr * strs;
16098
16099 /* Read in the string table, so that we have section names to scan. */
16100 strs = filedata->section_headers + filedata->file_header.e_shstrndx;
16101
16102 if (strs != NULL && strs->sh_size != 0)
16103 {
16104 filedata->string_table
16105 = (char *) get_data (NULL, filedata, strs->sh_offset,
16106 1, strs->sh_size, _("string table"));
16107
16108 filedata->string_table_length
16109 = filedata->string_table != NULL ? strs->sh_size : 0;
16110 }
16111 }
16112
16113 /* Locate the debug section. */
16114 sec = find_section_in_set (filedata, section->uncompressed_name, section_subset);
16115 if (sec != NULL)
16116 section->name = section->uncompressed_name;
16117 else
16118 {
16119 sec = find_section_in_set (filedata, section->compressed_name, section_subset);
16120 if (sec != NULL)
16121 section->name = section->compressed_name;
16122 }
16123 if (sec == NULL)
16124 return false;
16125
16126 /* If we're loading from a subset of sections, and we've loaded
16127 a section matching this name before, it's likely that it's a
16128 different one. */
16129 if (section_subset != NULL)
16130 free_debug_section (debug);
16131
16132 return load_specific_debug_section (debug, sec, data);
16133 }
16134
16135 void
16136 free_debug_section (enum dwarf_section_display_enum debug)
16137 {
16138 struct dwarf_section * section = &debug_displays [debug].section;
16139
16140 if (section->start == NULL)
16141 return;
16142
16143 free ((char *) section->start);
16144 section->start = NULL;
16145 section->address = 0;
16146 section->size = 0;
16147
16148 free (section->reloc_info);
16149 section->reloc_info = NULL;
16150 section->num_relocs = 0;
16151 }
16152
16153 static bool
16154 display_debug_section (int shndx, Elf_Internal_Shdr * section, Filedata * filedata)
16155 {
16156 const char *name = (section_name_valid (filedata, section)
16157 ? section_name (filedata, section) : "");
16158 const char *print_name = printable_section_name (filedata, section);
16159 bfd_size_type length;
16160 bool result = true;
16161 int i;
16162
16163 length = section->sh_size;
16164 if (length == 0)
16165 {
16166 printf (_("\nSection '%s' has no debugging data.\n"), print_name);
16167 return true;
16168 }
16169 if (section->sh_type == SHT_NOBITS)
16170 {
16171 /* There is no point in dumping the contents of a debugging section
16172 which has the NOBITS type - the bits in the file will be random.
16173 This can happen when a file containing a .eh_frame section is
16174 stripped with the --only-keep-debug command line option. */
16175 printf (_("section '%s' has the NOBITS type - its contents are unreliable.\n"),
16176 print_name);
16177 return false;
16178 }
16179
16180 if (startswith (name, ".gnu.linkonce.wi."))
16181 name = ".debug_info";
16182
16183 /* See if we know how to display the contents of this section. */
16184 for (i = 0; i < max; i++)
16185 {
16186 enum dwarf_section_display_enum id = (enum dwarf_section_display_enum) i;
16187 struct dwarf_section_display * display = debug_displays + i;
16188 struct dwarf_section * sec = & display->section;
16189
16190 if (streq (sec->uncompressed_name, name)
16191 || (id == line && startswith (name, ".debug_line."))
16192 || streq (sec->compressed_name, name))
16193 {
16194 bool secondary = (section != find_section (filedata, name));
16195
16196 if (secondary)
16197 free_debug_section (id);
16198
16199 if (i == line && startswith (name, ".debug_line."))
16200 sec->name = name;
16201 else if (streq (sec->uncompressed_name, name))
16202 sec->name = sec->uncompressed_name;
16203 else
16204 sec->name = sec->compressed_name;
16205
16206 if (load_specific_debug_section (id, section, filedata))
16207 {
16208 /* If this debug section is part of a CU/TU set in a .dwp file,
16209 restrict load_debug_section to the sections in that set. */
16210 section_subset = find_cu_tu_set (filedata, shndx);
16211
16212 result &= display->display (sec, filedata);
16213
16214 section_subset = NULL;
16215
16216 if (secondary || (id != info && id != abbrev && id != debug_addr))
16217 free_debug_section (id);
16218 }
16219 break;
16220 }
16221 }
16222
16223 if (i == max)
16224 {
16225 printf (_("Unrecognized debug section: %s\n"), print_name);
16226 result = false;
16227 }
16228
16229 return result;
16230 }
16231
16232 /* Set DUMP_SECTS for all sections where dumps were requested
16233 based on section name. */
16234
16235 static void
16236 initialise_dumps_byname (Filedata * filedata)
16237 {
16238 struct dump_list_entry * cur;
16239
16240 for (cur = dump_sects_byname; cur; cur = cur->next)
16241 {
16242 unsigned int i;
16243 bool any = false;
16244
16245 for (i = 0; i < filedata->file_header.e_shnum; i++)
16246 if (section_name_valid (filedata, filedata->section_headers + i)
16247 && streq (section_name (filedata, filedata->section_headers + i),
16248 cur->name))
16249 {
16250 request_dump_bynumber (&filedata->dump, i, cur->type);
16251 any = true;
16252 }
16253
16254 if (!any && !filedata->is_separate)
16255 warn (_("Section '%s' was not dumped because it does not exist\n"),
16256 cur->name);
16257 }
16258 }
16259
16260 static bool
16261 process_section_contents (Filedata * filedata)
16262 {
16263 Elf_Internal_Shdr * section;
16264 unsigned int i;
16265 bool res = true;
16266
16267 if (! do_dump)
16268 return true;
16269
16270 initialise_dumps_byname (filedata);
16271
16272 for (i = 0, section = filedata->section_headers;
16273 i < filedata->file_header.e_shnum && i < filedata->dump.num_dump_sects;
16274 i++, section++)
16275 {
16276 dump_type dump = filedata->dump.dump_sects[i];
16277
16278 if (filedata->is_separate && ! process_links)
16279 dump &= DEBUG_DUMP;
16280
16281 #ifdef SUPPORT_DISASSEMBLY
16282 if (dump & DISASS_DUMP)
16283 {
16284 if (! disassemble_section (section, filedata))
16285 res = false;
16286 }
16287 #endif
16288 if (dump & HEX_DUMP)
16289 {
16290 if (! dump_section_as_bytes (section, filedata, false))
16291 res = false;
16292 }
16293
16294 if (dump & RELOC_DUMP)
16295 {
16296 if (! dump_section_as_bytes (section, filedata, true))
16297 res = false;
16298 }
16299
16300 if (dump & STRING_DUMP)
16301 {
16302 if (! dump_section_as_strings (section, filedata))
16303 res = false;
16304 }
16305
16306 if (dump & DEBUG_DUMP)
16307 {
16308 if (! display_debug_section (i, section, filedata))
16309 res = false;
16310 }
16311
16312 #ifdef ENABLE_LIBCTF
16313 if (dump & CTF_DUMP)
16314 {
16315 if (! dump_section_as_ctf (section, filedata))
16316 res = false;
16317 }
16318 #endif
16319 }
16320
16321 if (! filedata->is_separate)
16322 {
16323 /* Check to see if the user requested a
16324 dump of a section that does not exist. */
16325 for (; i < filedata->dump.num_dump_sects; i++)
16326 if (filedata->dump.dump_sects[i])
16327 {
16328 warn (_("Section %d was not dumped because it does not exist!\n"), i);
16329 res = false;
16330 }
16331 }
16332
16333 return res;
16334 }
16335
16336 static void
16337 process_mips_fpe_exception (int mask)
16338 {
16339 if (mask)
16340 {
16341 bool first = true;
16342
16343 if (mask & OEX_FPU_INEX)
16344 fputs ("INEX", stdout), first = false;
16345 if (mask & OEX_FPU_UFLO)
16346 printf ("%sUFLO", first ? "" : "|"), first = false;
16347 if (mask & OEX_FPU_OFLO)
16348 printf ("%sOFLO", first ? "" : "|"), first = false;
16349 if (mask & OEX_FPU_DIV0)
16350 printf ("%sDIV0", first ? "" : "|"), first = false;
16351 if (mask & OEX_FPU_INVAL)
16352 printf ("%sINVAL", first ? "" : "|");
16353 }
16354 else
16355 fputs ("0", stdout);
16356 }
16357
16358 /* Display's the value of TAG at location P. If TAG is
16359 greater than 0 it is assumed to be an unknown tag, and
16360 a message is printed to this effect. Otherwise it is
16361 assumed that a message has already been printed.
16362
16363 If the bottom bit of TAG is set it assumed to have a
16364 string value, otherwise it is assumed to have an integer
16365 value.
16366
16367 Returns an updated P pointing to the first unread byte
16368 beyond the end of TAG's value.
16369
16370 Reads at or beyond END will not be made. */
16371
16372 static unsigned char *
16373 display_tag_value (signed int tag,
16374 unsigned char * p,
16375 const unsigned char * const end)
16376 {
16377 unsigned long val;
16378
16379 if (tag > 0)
16380 printf (" Tag_unknown_%d: ", tag);
16381
16382 if (p >= end)
16383 {
16384 warn (_("<corrupt tag>\n"));
16385 }
16386 else if (tag & 1)
16387 {
16388 /* PR 17531 file: 027-19978-0.004. */
16389 size_t maxlen = (end - p) - 1;
16390
16391 putchar ('"');
16392 if (maxlen > 0)
16393 {
16394 print_symbol ((int) maxlen, (const char *) p);
16395 p += strnlen ((char *) p, maxlen) + 1;
16396 }
16397 else
16398 {
16399 printf (_("<corrupt string tag>"));
16400 p = (unsigned char *) end;
16401 }
16402 printf ("\"\n");
16403 }
16404 else
16405 {
16406 READ_ULEB (val, p, end);
16407 printf ("%ld (0x%lx)\n", val, val);
16408 }
16409
16410 assert (p <= end);
16411 return p;
16412 }
16413
16414 /* ARC ABI attributes section. */
16415
16416 static unsigned char *
16417 display_arc_attribute (unsigned char * p,
16418 const unsigned char * const end)
16419 {
16420 unsigned int tag;
16421 unsigned int val;
16422
16423 READ_ULEB (tag, p, end);
16424
16425 switch (tag)
16426 {
16427 case Tag_ARC_PCS_config:
16428 READ_ULEB (val, p, end);
16429 printf (" Tag_ARC_PCS_config: ");
16430 switch (val)
16431 {
16432 case 0:
16433 printf (_("Absent/Non standard\n"));
16434 break;
16435 case 1:
16436 printf (_("Bare metal/mwdt\n"));
16437 break;
16438 case 2:
16439 printf (_("Bare metal/newlib\n"));
16440 break;
16441 case 3:
16442 printf (_("Linux/uclibc\n"));
16443 break;
16444 case 4:
16445 printf (_("Linux/glibc\n"));
16446 break;
16447 default:
16448 printf (_("Unknown\n"));
16449 break;
16450 }
16451 break;
16452
16453 case Tag_ARC_CPU_base:
16454 READ_ULEB (val, p, end);
16455 printf (" Tag_ARC_CPU_base: ");
16456 switch (val)
16457 {
16458 default:
16459 case TAG_CPU_NONE:
16460 printf (_("Absent\n"));
16461 break;
16462 case TAG_CPU_ARC6xx:
16463 printf ("ARC6xx\n");
16464 break;
16465 case TAG_CPU_ARC7xx:
16466 printf ("ARC7xx\n");
16467 break;
16468 case TAG_CPU_ARCEM:
16469 printf ("ARCEM\n");
16470 break;
16471 case TAG_CPU_ARCHS:
16472 printf ("ARCHS\n");
16473 break;
16474 }
16475 break;
16476
16477 case Tag_ARC_CPU_variation:
16478 READ_ULEB (val, p, end);
16479 printf (" Tag_ARC_CPU_variation: ");
16480 switch (val)
16481 {
16482 default:
16483 if (val > 0 && val < 16)
16484 printf ("Core%d\n", val);
16485 else
16486 printf ("Unknown\n");
16487 break;
16488
16489 case 0:
16490 printf (_("Absent\n"));
16491 break;
16492 }
16493 break;
16494
16495 case Tag_ARC_CPU_name:
16496 printf (" Tag_ARC_CPU_name: ");
16497 p = display_tag_value (-1, p, end);
16498 break;
16499
16500 case Tag_ARC_ABI_rf16:
16501 READ_ULEB (val, p, end);
16502 printf (" Tag_ARC_ABI_rf16: %s\n", val ? _("yes") : _("no"));
16503 break;
16504
16505 case Tag_ARC_ABI_osver:
16506 READ_ULEB (val, p, end);
16507 printf (" Tag_ARC_ABI_osver: v%d\n", val);
16508 break;
16509
16510 case Tag_ARC_ABI_pic:
16511 case Tag_ARC_ABI_sda:
16512 READ_ULEB (val, p, end);
16513 printf (tag == Tag_ARC_ABI_sda ? " Tag_ARC_ABI_sda: "
16514 : " Tag_ARC_ABI_pic: ");
16515 switch (val)
16516 {
16517 case 0:
16518 printf (_("Absent\n"));
16519 break;
16520 case 1:
16521 printf ("MWDT\n");
16522 break;
16523 case 2:
16524 printf ("GNU\n");
16525 break;
16526 default:
16527 printf (_("Unknown\n"));
16528 break;
16529 }
16530 break;
16531
16532 case Tag_ARC_ABI_tls:
16533 READ_ULEB (val, p, end);
16534 printf (" Tag_ARC_ABI_tls: %s\n", val ? "r25": "none");
16535 break;
16536
16537 case Tag_ARC_ABI_enumsize:
16538 READ_ULEB (val, p, end);
16539 printf (" Tag_ARC_ABI_enumsize: %s\n", val ? _("default") :
16540 _("smallest"));
16541 break;
16542
16543 case Tag_ARC_ABI_exceptions:
16544 READ_ULEB (val, p, end);
16545 printf (" Tag_ARC_ABI_exceptions: %s\n", val ? _("OPTFP")
16546 : _("default"));
16547 break;
16548
16549 case Tag_ARC_ABI_double_size:
16550 READ_ULEB (val, p, end);
16551 printf (" Tag_ARC_ABI_double_size: %d\n", val);
16552 break;
16553
16554 case Tag_ARC_ISA_config:
16555 printf (" Tag_ARC_ISA_config: ");
16556 p = display_tag_value (-1, p, end);
16557 break;
16558
16559 case Tag_ARC_ISA_apex:
16560 printf (" Tag_ARC_ISA_apex: ");
16561 p = display_tag_value (-1, p, end);
16562 break;
16563
16564 case Tag_ARC_ISA_mpy_option:
16565 READ_ULEB (val, p, end);
16566 printf (" Tag_ARC_ISA_mpy_option: %d\n", val);
16567 break;
16568
16569 case Tag_ARC_ATR_version:
16570 READ_ULEB (val, p, end);
16571 printf (" Tag_ARC_ATR_version: %d\n", val);
16572 break;
16573
16574 default:
16575 return display_tag_value (tag & 1, p, end);
16576 }
16577
16578 return p;
16579 }
16580
16581 /* ARM EABI attributes section. */
16582 typedef struct
16583 {
16584 unsigned int tag;
16585 const char * name;
16586 /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup. */
16587 unsigned int type;
16588 const char *const *table;
16589 } arm_attr_public_tag;
16590
16591 static const char *const arm_attr_tag_CPU_arch[] =
16592 {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
16593 "v6K", "v7", "v6-M", "v6S-M", "v7E-M", "v8", "v8-R", "v8-M.baseline",
16594 "v8-M.mainline", "v8.1-A", "v8.2-A", "v8.3-A",
16595 "v8.1-M.mainline", "v9"};
16596 static const char *const arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
16597 static const char *const arm_attr_tag_THUMB_ISA_use[] =
16598 {"No", "Thumb-1", "Thumb-2", "Yes"};
16599 static const char *const arm_attr_tag_FP_arch[] =
16600 {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16", "VFPv4", "VFPv4-D16",
16601 "FP for ARMv8", "FPv5/FP-D16 for ARMv8"};
16602 static const char *const arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
16603 static const char *const arm_attr_tag_Advanced_SIMD_arch[] =
16604 {"No", "NEONv1", "NEONv1 with Fused-MAC", "NEON for ARMv8",
16605 "NEON for ARMv8.1"};
16606 static const char *const arm_attr_tag_PCS_config[] =
16607 {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
16608 "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
16609 static const char *const arm_attr_tag_ABI_PCS_R9_use[] =
16610 {"V6", "SB", "TLS", "Unused"};
16611 static const char *const arm_attr_tag_ABI_PCS_RW_data[] =
16612 {"Absolute", "PC-relative", "SB-relative", "None"};
16613 static const char *const arm_attr_tag_ABI_PCS_RO_data[] =
16614 {"Absolute", "PC-relative", "None"};
16615 static const char *const arm_attr_tag_ABI_PCS_GOT_use[] =
16616 {"None", "direct", "GOT-indirect"};
16617 static const char *const arm_attr_tag_ABI_PCS_wchar_t[] =
16618 {"None", "??? 1", "2", "??? 3", "4"};
16619 static const char *const arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
16620 static const char *const arm_attr_tag_ABI_FP_denormal[] =
16621 {"Unused", "Needed", "Sign only"};
16622 static const char *const arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
16623 static const char *const arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
16624 static const char *const arm_attr_tag_ABI_FP_number_model[] =
16625 {"Unused", "Finite", "RTABI", "IEEE 754"};
16626 static const char *const arm_attr_tag_ABI_enum_size[] =
16627 {"Unused", "small", "int", "forced to int"};
16628 static const char *const arm_attr_tag_ABI_HardFP_use[] =
16629 {"As Tag_FP_arch", "SP only", "Reserved", "Deprecated"};
16630 static const char *const arm_attr_tag_ABI_VFP_args[] =
16631 {"AAPCS", "VFP registers", "custom", "compatible"};
16632 static const char *const arm_attr_tag_ABI_WMMX_args[] =
16633 {"AAPCS", "WMMX registers", "custom"};
16634 static const char *const arm_attr_tag_ABI_optimization_goals[] =
16635 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
16636 "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
16637 static const char *const arm_attr_tag_ABI_FP_optimization_goals[] =
16638 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
16639 "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
16640 static const char *const arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
16641 static const char *const arm_attr_tag_FP_HP_extension[] =
16642 {"Not Allowed", "Allowed"};
16643 static const char *const arm_attr_tag_ABI_FP_16bit_format[] =
16644 {"None", "IEEE 754", "Alternative Format"};
16645 static const char *const arm_attr_tag_DSP_extension[] =
16646 {"Follow architecture", "Allowed"};
16647 static const char *const arm_attr_tag_MPextension_use[] =
16648 {"Not Allowed", "Allowed"};
16649 static const char *const arm_attr_tag_DIV_use[] =
16650 {"Allowed in Thumb-ISA, v7-R or v7-M", "Not allowed",
16651 "Allowed in v7-A with integer division extension"};
16652 static const char *const arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
16653 static const char *const arm_attr_tag_Virtualization_use[] =
16654 {"Not Allowed", "TrustZone", "Virtualization Extensions",
16655 "TrustZone and Virtualization Extensions"};
16656 static const char *const arm_attr_tag_MPextension_use_legacy[] =
16657 {"Not Allowed", "Allowed"};
16658
16659 static const char *const arm_attr_tag_MVE_arch[] =
16660 {"No MVE", "MVE Integer only", "MVE Integer and FP"};
16661
16662 static const char * arm_attr_tag_PAC_extension[] =
16663 {"No PAC/AUT instructions",
16664 "PAC/AUT instructions permitted in the NOP space",
16665 "PAC/AUT instructions permitted in the NOP and in the non-NOP space"};
16666
16667 static const char * arm_attr_tag_BTI_extension[] =
16668 {"BTI instructions not permitted",
16669 "BTI instructions permitted in the NOP space",
16670 "BTI instructions permitted in the NOP and in the non-NOP space"};
16671
16672 static const char * arm_attr_tag_BTI_use[] =
16673 {"Compiled without branch target enforcement",
16674 "Compiled with branch target enforcement"};
16675
16676 static const char * arm_attr_tag_PACRET_use[] =
16677 {"Compiled without return address signing and authentication",
16678 "Compiled with return address signing and authentication"};
16679
16680 #define LOOKUP(id, name) \
16681 {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
16682 static arm_attr_public_tag arm_attr_public_tags[] =
16683 {
16684 {4, "CPU_raw_name", 1, NULL},
16685 {5, "CPU_name", 1, NULL},
16686 LOOKUP(6, CPU_arch),
16687 {7, "CPU_arch_profile", 0, NULL},
16688 LOOKUP(8, ARM_ISA_use),
16689 LOOKUP(9, THUMB_ISA_use),
16690 LOOKUP(10, FP_arch),
16691 LOOKUP(11, WMMX_arch),
16692 LOOKUP(12, Advanced_SIMD_arch),
16693 LOOKUP(13, PCS_config),
16694 LOOKUP(14, ABI_PCS_R9_use),
16695 LOOKUP(15, ABI_PCS_RW_data),
16696 LOOKUP(16, ABI_PCS_RO_data),
16697 LOOKUP(17, ABI_PCS_GOT_use),
16698 LOOKUP(18, ABI_PCS_wchar_t),
16699 LOOKUP(19, ABI_FP_rounding),
16700 LOOKUP(20, ABI_FP_denormal),
16701 LOOKUP(21, ABI_FP_exceptions),
16702 LOOKUP(22, ABI_FP_user_exceptions),
16703 LOOKUP(23, ABI_FP_number_model),
16704 {24, "ABI_align_needed", 0, NULL},
16705 {25, "ABI_align_preserved", 0, NULL},
16706 LOOKUP(26, ABI_enum_size),
16707 LOOKUP(27, ABI_HardFP_use),
16708 LOOKUP(28, ABI_VFP_args),
16709 LOOKUP(29, ABI_WMMX_args),
16710 LOOKUP(30, ABI_optimization_goals),
16711 LOOKUP(31, ABI_FP_optimization_goals),
16712 {32, "compatibility", 0, NULL},
16713 LOOKUP(34, CPU_unaligned_access),
16714 LOOKUP(36, FP_HP_extension),
16715 LOOKUP(38, ABI_FP_16bit_format),
16716 LOOKUP(42, MPextension_use),
16717 LOOKUP(44, DIV_use),
16718 LOOKUP(46, DSP_extension),
16719 LOOKUP(48, MVE_arch),
16720 LOOKUP(50, PAC_extension),
16721 LOOKUP(52, BTI_extension),
16722 LOOKUP(74, BTI_use),
16723 LOOKUP(76, PACRET_use),
16724 {64, "nodefaults", 0, NULL},
16725 {65, "also_compatible_with", 0, NULL},
16726 LOOKUP(66, T2EE_use),
16727 {67, "conformance", 1, NULL},
16728 LOOKUP(68, Virtualization_use),
16729 LOOKUP(70, MPextension_use_legacy)
16730 };
16731 #undef LOOKUP
16732
16733 static unsigned char *
16734 display_arm_attribute (unsigned char * p,
16735 const unsigned char * const end)
16736 {
16737 unsigned int tag;
16738 unsigned int val;
16739 arm_attr_public_tag * attr;
16740 unsigned i;
16741 unsigned int type;
16742
16743 READ_ULEB (tag, p, end);
16744 attr = NULL;
16745 for (i = 0; i < ARRAY_SIZE (arm_attr_public_tags); i++)
16746 {
16747 if (arm_attr_public_tags[i].tag == tag)
16748 {
16749 attr = &arm_attr_public_tags[i];
16750 break;
16751 }
16752 }
16753
16754 if (attr)
16755 {
16756 printf (" Tag_%s: ", attr->name);
16757 switch (attr->type)
16758 {
16759 case 0:
16760 switch (tag)
16761 {
16762 case 7: /* Tag_CPU_arch_profile. */
16763 READ_ULEB (val, p, end);
16764 switch (val)
16765 {
16766 case 0: printf (_("None\n")); break;
16767 case 'A': printf (_("Application\n")); break;
16768 case 'R': printf (_("Realtime\n")); break;
16769 case 'M': printf (_("Microcontroller\n")); break;
16770 case 'S': printf (_("Application or Realtime\n")); break;
16771 default: printf ("??? (%d)\n", val); break;
16772 }
16773 break;
16774
16775 case 24: /* Tag_align_needed. */
16776 READ_ULEB (val, p, end);
16777 switch (val)
16778 {
16779 case 0: printf (_("None\n")); break;
16780 case 1: printf (_("8-byte\n")); break;
16781 case 2: printf (_("4-byte\n")); break;
16782 case 3: printf ("??? 3\n"); break;
16783 default:
16784 if (val <= 12)
16785 printf (_("8-byte and up to %d-byte extended\n"),
16786 1 << val);
16787 else
16788 printf ("??? (%d)\n", val);
16789 break;
16790 }
16791 break;
16792
16793 case 25: /* Tag_align_preserved. */
16794 READ_ULEB (val, p, end);
16795 switch (val)
16796 {
16797 case 0: printf (_("None\n")); break;
16798 case 1: printf (_("8-byte, except leaf SP\n")); break;
16799 case 2: printf (_("8-byte\n")); break;
16800 case 3: printf ("??? 3\n"); break;
16801 default:
16802 if (val <= 12)
16803 printf (_("8-byte and up to %d-byte extended\n"),
16804 1 << val);
16805 else
16806 printf ("??? (%d)\n", val);
16807 break;
16808 }
16809 break;
16810
16811 case 32: /* Tag_compatibility. */
16812 {
16813 READ_ULEB (val, p, end);
16814 printf (_("flag = %d, vendor = "), val);
16815 if (p < end - 1)
16816 {
16817 size_t maxlen = (end - p) - 1;
16818
16819 print_symbol ((int) maxlen, (const char *) p);
16820 p += strnlen ((char *) p, maxlen) + 1;
16821 }
16822 else
16823 {
16824 printf (_("<corrupt>"));
16825 p = (unsigned char *) end;
16826 }
16827 putchar ('\n');
16828 }
16829 break;
16830
16831 case 64: /* Tag_nodefaults. */
16832 /* PR 17531: file: 001-505008-0.01. */
16833 if (p < end)
16834 p++;
16835 printf (_("True\n"));
16836 break;
16837
16838 case 65: /* Tag_also_compatible_with. */
16839 READ_ULEB (val, p, end);
16840 if (val == 6 /* Tag_CPU_arch. */)
16841 {
16842 READ_ULEB (val, p, end);
16843 if ((unsigned int) val >= ARRAY_SIZE (arm_attr_tag_CPU_arch))
16844 printf ("??? (%d)\n", val);
16845 else
16846 printf ("%s\n", arm_attr_tag_CPU_arch[val]);
16847 }
16848 else
16849 printf ("???\n");
16850 while (p < end && *(p++) != '\0' /* NUL terminator. */)
16851 ;
16852 break;
16853
16854 default:
16855 printf (_("<unknown: %d>\n"), tag);
16856 break;
16857 }
16858 return p;
16859
16860 case 1:
16861 return display_tag_value (-1, p, end);
16862 case 2:
16863 return display_tag_value (0, p, end);
16864
16865 default:
16866 assert (attr->type & 0x80);
16867 READ_ULEB (val, p, end);
16868 type = attr->type & 0x7f;
16869 if (val >= type)
16870 printf ("??? (%d)\n", val);
16871 else
16872 printf ("%s\n", attr->table[val]);
16873 return p;
16874 }
16875 }
16876
16877 return display_tag_value (tag, p, end);
16878 }
16879
16880 static unsigned char *
16881 display_gnu_attribute (unsigned char * p,
16882 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const),
16883 const unsigned char * const end)
16884 {
16885 unsigned int tag;
16886 unsigned int val;
16887
16888 READ_ULEB (tag, p, end);
16889
16890 /* Tag_compatibility is the only generic GNU attribute defined at
16891 present. */
16892 if (tag == 32)
16893 {
16894 READ_ULEB (val, p, end);
16895
16896 printf (_("flag = %d, vendor = "), val);
16897 if (p == end)
16898 {
16899 printf (_("<corrupt>\n"));
16900 warn (_("corrupt vendor attribute\n"));
16901 }
16902 else
16903 {
16904 if (p < end - 1)
16905 {
16906 size_t maxlen = (end - p) - 1;
16907
16908 print_symbol ((int) maxlen, (const char *) p);
16909 p += strnlen ((char *) p, maxlen) + 1;
16910 }
16911 else
16912 {
16913 printf (_("<corrupt>"));
16914 p = (unsigned char *) end;
16915 }
16916 putchar ('\n');
16917 }
16918 return p;
16919 }
16920
16921 if ((tag & 2) == 0 && display_proc_gnu_attribute)
16922 return display_proc_gnu_attribute (p, tag, end);
16923
16924 return display_tag_value (tag, p, end);
16925 }
16926
16927 static unsigned char *
16928 display_m68k_gnu_attribute (unsigned char * p,
16929 unsigned int tag,
16930 const unsigned char * const end)
16931 {
16932 unsigned int val;
16933
16934 if (tag == Tag_GNU_M68K_ABI_FP)
16935 {
16936 printf (" Tag_GNU_M68K_ABI_FP: ");
16937 if (p == end)
16938 {
16939 printf (_("<corrupt>\n"));
16940 return p;
16941 }
16942 READ_ULEB (val, p, end);
16943
16944 if (val > 3)
16945 printf ("(%#x), ", val);
16946
16947 switch (val & 3)
16948 {
16949 case 0:
16950 printf (_("unspecified hard/soft float\n"));
16951 break;
16952 case 1:
16953 printf (_("hard float\n"));
16954 break;
16955 case 2:
16956 printf (_("soft float\n"));
16957 break;
16958 }
16959 return p;
16960 }
16961
16962 return display_tag_value (tag & 1, p, end);
16963 }
16964
16965 static unsigned char *
16966 display_power_gnu_attribute (unsigned char * p,
16967 unsigned int tag,
16968 const unsigned char * const end)
16969 {
16970 unsigned int val;
16971
16972 if (tag == Tag_GNU_Power_ABI_FP)
16973 {
16974 printf (" Tag_GNU_Power_ABI_FP: ");
16975 if (p == end)
16976 {
16977 printf (_("<corrupt>\n"));
16978 return p;
16979 }
16980 READ_ULEB (val, p, end);
16981
16982 if (val > 15)
16983 printf ("(%#x), ", val);
16984
16985 switch (val & 3)
16986 {
16987 case 0:
16988 printf (_("unspecified hard/soft float, "));
16989 break;
16990 case 1:
16991 printf (_("hard float, "));
16992 break;
16993 case 2:
16994 printf (_("soft float, "));
16995 break;
16996 case 3:
16997 printf (_("single-precision hard float, "));
16998 break;
16999 }
17000
17001 switch (val & 0xC)
17002 {
17003 case 0:
17004 printf (_("unspecified long double\n"));
17005 break;
17006 case 4:
17007 printf (_("128-bit IBM long double\n"));
17008 break;
17009 case 8:
17010 printf (_("64-bit long double\n"));
17011 break;
17012 case 12:
17013 printf (_("128-bit IEEE long double\n"));
17014 break;
17015 }
17016 return p;
17017 }
17018
17019 if (tag == Tag_GNU_Power_ABI_Vector)
17020 {
17021 printf (" Tag_GNU_Power_ABI_Vector: ");
17022 if (p == end)
17023 {
17024 printf (_("<corrupt>\n"));
17025 return p;
17026 }
17027 READ_ULEB (val, p, end);
17028
17029 if (val > 3)
17030 printf ("(%#x), ", val);
17031
17032 switch (val & 3)
17033 {
17034 case 0:
17035 printf (_("unspecified\n"));
17036 break;
17037 case 1:
17038 printf (_("generic\n"));
17039 break;
17040 case 2:
17041 printf ("AltiVec\n");
17042 break;
17043 case 3:
17044 printf ("SPE\n");
17045 break;
17046 }
17047 return p;
17048 }
17049
17050 if (tag == Tag_GNU_Power_ABI_Struct_Return)
17051 {
17052 printf (" Tag_GNU_Power_ABI_Struct_Return: ");
17053 if (p == end)
17054 {
17055 printf (_("<corrupt>\n"));
17056 return p;
17057 }
17058 READ_ULEB (val, p, end);
17059
17060 if (val > 2)
17061 printf ("(%#x), ", val);
17062
17063 switch (val & 3)
17064 {
17065 case 0:
17066 printf (_("unspecified\n"));
17067 break;
17068 case 1:
17069 printf ("r3/r4\n");
17070 break;
17071 case 2:
17072 printf (_("memory\n"));
17073 break;
17074 case 3:
17075 printf ("???\n");
17076 break;
17077 }
17078 return p;
17079 }
17080
17081 return display_tag_value (tag & 1, p, end);
17082 }
17083
17084 static unsigned char *
17085 display_s390_gnu_attribute (unsigned char * p,
17086 unsigned int tag,
17087 const unsigned char * const end)
17088 {
17089 unsigned int val;
17090
17091 if (tag == Tag_GNU_S390_ABI_Vector)
17092 {
17093 printf (" Tag_GNU_S390_ABI_Vector: ");
17094 READ_ULEB (val, p, end);
17095
17096 switch (val)
17097 {
17098 case 0:
17099 printf (_("any\n"));
17100 break;
17101 case 1:
17102 printf (_("software\n"));
17103 break;
17104 case 2:
17105 printf (_("hardware\n"));
17106 break;
17107 default:
17108 printf ("??? (%d)\n", val);
17109 break;
17110 }
17111 return p;
17112 }
17113
17114 return display_tag_value (tag & 1, p, end);
17115 }
17116
17117 static void
17118 display_sparc_hwcaps (unsigned int mask)
17119 {
17120 if (mask)
17121 {
17122 bool first = true;
17123
17124 if (mask & ELF_SPARC_HWCAP_MUL32)
17125 fputs ("mul32", stdout), first = false;
17126 if (mask & ELF_SPARC_HWCAP_DIV32)
17127 printf ("%sdiv32", first ? "" : "|"), first = false;
17128 if (mask & ELF_SPARC_HWCAP_FSMULD)
17129 printf ("%sfsmuld", first ? "" : "|"), first = false;
17130 if (mask & ELF_SPARC_HWCAP_V8PLUS)
17131 printf ("%sv8plus", first ? "" : "|"), first = false;
17132 if (mask & ELF_SPARC_HWCAP_POPC)
17133 printf ("%spopc", first ? "" : "|"), first = false;
17134 if (mask & ELF_SPARC_HWCAP_VIS)
17135 printf ("%svis", first ? "" : "|"), first = false;
17136 if (mask & ELF_SPARC_HWCAP_VIS2)
17137 printf ("%svis2", first ? "" : "|"), first = false;
17138 if (mask & ELF_SPARC_HWCAP_ASI_BLK_INIT)
17139 printf ("%sASIBlkInit", first ? "" : "|"), first = false;
17140 if (mask & ELF_SPARC_HWCAP_FMAF)
17141 printf ("%sfmaf", first ? "" : "|"), first = false;
17142 if (mask & ELF_SPARC_HWCAP_VIS3)
17143 printf ("%svis3", first ? "" : "|"), first = false;
17144 if (mask & ELF_SPARC_HWCAP_HPC)
17145 printf ("%shpc", first ? "" : "|"), first = false;
17146 if (mask & ELF_SPARC_HWCAP_RANDOM)
17147 printf ("%srandom", first ? "" : "|"), first = false;
17148 if (mask & ELF_SPARC_HWCAP_TRANS)
17149 printf ("%strans", first ? "" : "|"), first = false;
17150 if (mask & ELF_SPARC_HWCAP_FJFMAU)
17151 printf ("%sfjfmau", first ? "" : "|"), first = false;
17152 if (mask & ELF_SPARC_HWCAP_IMA)
17153 printf ("%sima", first ? "" : "|"), first = false;
17154 if (mask & ELF_SPARC_HWCAP_ASI_CACHE_SPARING)
17155 printf ("%scspare", first ? "" : "|"), first = false;
17156 }
17157 else
17158 fputc ('0', stdout);
17159 fputc ('\n', stdout);
17160 }
17161
17162 static void
17163 display_sparc_hwcaps2 (unsigned int mask)
17164 {
17165 if (mask)
17166 {
17167 bool first = true;
17168
17169 if (mask & ELF_SPARC_HWCAP2_FJATHPLUS)
17170 fputs ("fjathplus", stdout), first = false;
17171 if (mask & ELF_SPARC_HWCAP2_VIS3B)
17172 printf ("%svis3b", first ? "" : "|"), first = false;
17173 if (mask & ELF_SPARC_HWCAP2_ADP)
17174 printf ("%sadp", first ? "" : "|"), first = false;
17175 if (mask & ELF_SPARC_HWCAP2_SPARC5)
17176 printf ("%ssparc5", first ? "" : "|"), first = false;
17177 if (mask & ELF_SPARC_HWCAP2_MWAIT)
17178 printf ("%smwait", first ? "" : "|"), first = false;
17179 if (mask & ELF_SPARC_HWCAP2_XMPMUL)
17180 printf ("%sxmpmul", first ? "" : "|"), first = false;
17181 if (mask & ELF_SPARC_HWCAP2_XMONT)
17182 printf ("%sxmont2", first ? "" : "|"), first = false;
17183 if (mask & ELF_SPARC_HWCAP2_NSEC)
17184 printf ("%snsec", first ? "" : "|"), first = false;
17185 if (mask & ELF_SPARC_HWCAP2_FJATHHPC)
17186 printf ("%sfjathhpc", first ? "" : "|"), first = false;
17187 if (mask & ELF_SPARC_HWCAP2_FJDES)
17188 printf ("%sfjdes", first ? "" : "|"), first = false;
17189 if (mask & ELF_SPARC_HWCAP2_FJAES)
17190 printf ("%sfjaes", first ? "" : "|"), first = false;
17191 }
17192 else
17193 fputc ('0', stdout);
17194 fputc ('\n', stdout);
17195 }
17196
17197 static unsigned char *
17198 display_sparc_gnu_attribute (unsigned char * p,
17199 unsigned int tag,
17200 const unsigned char * const end)
17201 {
17202 unsigned int val;
17203
17204 if (tag == Tag_GNU_Sparc_HWCAPS)
17205 {
17206 READ_ULEB (val, p, end);
17207 printf (" Tag_GNU_Sparc_HWCAPS: ");
17208 display_sparc_hwcaps (val);
17209 return p;
17210 }
17211 if (tag == Tag_GNU_Sparc_HWCAPS2)
17212 {
17213 READ_ULEB (val, p, end);
17214 printf (" Tag_GNU_Sparc_HWCAPS2: ");
17215 display_sparc_hwcaps2 (val);
17216 return p;
17217 }
17218
17219 return display_tag_value (tag, p, end);
17220 }
17221
17222 static void
17223 print_mips_fp_abi_value (unsigned int val)
17224 {
17225 switch (val)
17226 {
17227 case Val_GNU_MIPS_ABI_FP_ANY:
17228 printf (_("Hard or soft float\n"));
17229 break;
17230 case Val_GNU_MIPS_ABI_FP_DOUBLE:
17231 printf (_("Hard float (double precision)\n"));
17232 break;
17233 case Val_GNU_MIPS_ABI_FP_SINGLE:
17234 printf (_("Hard float (single precision)\n"));
17235 break;
17236 case Val_GNU_MIPS_ABI_FP_SOFT:
17237 printf (_("Soft float\n"));
17238 break;
17239 case Val_GNU_MIPS_ABI_FP_OLD_64:
17240 printf (_("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
17241 break;
17242 case Val_GNU_MIPS_ABI_FP_XX:
17243 printf (_("Hard float (32-bit CPU, Any FPU)\n"));
17244 break;
17245 case Val_GNU_MIPS_ABI_FP_64:
17246 printf (_("Hard float (32-bit CPU, 64-bit FPU)\n"));
17247 break;
17248 case Val_GNU_MIPS_ABI_FP_64A:
17249 printf (_("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
17250 break;
17251 case Val_GNU_MIPS_ABI_FP_NAN2008:
17252 printf (_("NaN 2008 compatibility\n"));
17253 break;
17254 default:
17255 printf ("??? (%d)\n", val);
17256 break;
17257 }
17258 }
17259
17260 static unsigned char *
17261 display_mips_gnu_attribute (unsigned char * p,
17262 unsigned int tag,
17263 const unsigned char * const end)
17264 {
17265 if (tag == Tag_GNU_MIPS_ABI_FP)
17266 {
17267 unsigned int val;
17268
17269 printf (" Tag_GNU_MIPS_ABI_FP: ");
17270 READ_ULEB (val, p, end);
17271 print_mips_fp_abi_value (val);
17272 return p;
17273 }
17274
17275 if (tag == Tag_GNU_MIPS_ABI_MSA)
17276 {
17277 unsigned int val;
17278
17279 printf (" Tag_GNU_MIPS_ABI_MSA: ");
17280 READ_ULEB (val, p, end);
17281
17282 switch (val)
17283 {
17284 case Val_GNU_MIPS_ABI_MSA_ANY:
17285 printf (_("Any MSA or not\n"));
17286 break;
17287 case Val_GNU_MIPS_ABI_MSA_128:
17288 printf (_("128-bit MSA\n"));
17289 break;
17290 default:
17291 printf ("??? (%d)\n", val);
17292 break;
17293 }
17294 return p;
17295 }
17296
17297 return display_tag_value (tag & 1, p, end);
17298 }
17299
17300 static unsigned char *
17301 display_tic6x_attribute (unsigned char * p,
17302 const unsigned char * const end)
17303 {
17304 unsigned int tag;
17305 unsigned int val;
17306
17307 READ_ULEB (tag, p, end);
17308
17309 switch (tag)
17310 {
17311 case Tag_ISA:
17312 printf (" Tag_ISA: ");
17313 READ_ULEB (val, p, end);
17314
17315 switch (val)
17316 {
17317 case C6XABI_Tag_ISA_none:
17318 printf (_("None\n"));
17319 break;
17320 case C6XABI_Tag_ISA_C62X:
17321 printf ("C62x\n");
17322 break;
17323 case C6XABI_Tag_ISA_C67X:
17324 printf ("C67x\n");
17325 break;
17326 case C6XABI_Tag_ISA_C67XP:
17327 printf ("C67x+\n");
17328 break;
17329 case C6XABI_Tag_ISA_C64X:
17330 printf ("C64x\n");
17331 break;
17332 case C6XABI_Tag_ISA_C64XP:
17333 printf ("C64x+\n");
17334 break;
17335 case C6XABI_Tag_ISA_C674X:
17336 printf ("C674x\n");
17337 break;
17338 default:
17339 printf ("??? (%d)\n", val);
17340 break;
17341 }
17342 return p;
17343
17344 case Tag_ABI_wchar_t:
17345 printf (" Tag_ABI_wchar_t: ");
17346 READ_ULEB (val, p, end);
17347 switch (val)
17348 {
17349 case 0:
17350 printf (_("Not used\n"));
17351 break;
17352 case 1:
17353 printf (_("2 bytes\n"));
17354 break;
17355 case 2:
17356 printf (_("4 bytes\n"));
17357 break;
17358 default:
17359 printf ("??? (%d)\n", val);
17360 break;
17361 }
17362 return p;
17363
17364 case Tag_ABI_stack_align_needed:
17365 printf (" Tag_ABI_stack_align_needed: ");
17366 READ_ULEB (val, p, end);
17367 switch (val)
17368 {
17369 case 0:
17370 printf (_("8-byte\n"));
17371 break;
17372 case 1:
17373 printf (_("16-byte\n"));
17374 break;
17375 default:
17376 printf ("??? (%d)\n", val);
17377 break;
17378 }
17379 return p;
17380
17381 case Tag_ABI_stack_align_preserved:
17382 READ_ULEB (val, p, end);
17383 printf (" Tag_ABI_stack_align_preserved: ");
17384 switch (val)
17385 {
17386 case 0:
17387 printf (_("8-byte\n"));
17388 break;
17389 case 1:
17390 printf (_("16-byte\n"));
17391 break;
17392 default:
17393 printf ("??? (%d)\n", val);
17394 break;
17395 }
17396 return p;
17397
17398 case Tag_ABI_DSBT:
17399 READ_ULEB (val, p, end);
17400 printf (" Tag_ABI_DSBT: ");
17401 switch (val)
17402 {
17403 case 0:
17404 printf (_("DSBT addressing not used\n"));
17405 break;
17406 case 1:
17407 printf (_("DSBT addressing used\n"));
17408 break;
17409 default:
17410 printf ("??? (%d)\n", val);
17411 break;
17412 }
17413 return p;
17414
17415 case Tag_ABI_PID:
17416 READ_ULEB (val, p, end);
17417 printf (" Tag_ABI_PID: ");
17418 switch (val)
17419 {
17420 case 0:
17421 printf (_("Data addressing position-dependent\n"));
17422 break;
17423 case 1:
17424 printf (_("Data addressing position-independent, GOT near DP\n"));
17425 break;
17426 case 2:
17427 printf (_("Data addressing position-independent, GOT far from DP\n"));
17428 break;
17429 default:
17430 printf ("??? (%d)\n", val);
17431 break;
17432 }
17433 return p;
17434
17435 case Tag_ABI_PIC:
17436 READ_ULEB (val, p, end);
17437 printf (" Tag_ABI_PIC: ");
17438 switch (val)
17439 {
17440 case 0:
17441 printf (_("Code addressing position-dependent\n"));
17442 break;
17443 case 1:
17444 printf (_("Code addressing position-independent\n"));
17445 break;
17446 default:
17447 printf ("??? (%d)\n", val);
17448 break;
17449 }
17450 return p;
17451
17452 case Tag_ABI_array_object_alignment:
17453 READ_ULEB (val, p, end);
17454 printf (" Tag_ABI_array_object_alignment: ");
17455 switch (val)
17456 {
17457 case 0:
17458 printf (_("8-byte\n"));
17459 break;
17460 case 1:
17461 printf (_("4-byte\n"));
17462 break;
17463 case 2:
17464 printf (_("16-byte\n"));
17465 break;
17466 default:
17467 printf ("??? (%d)\n", val);
17468 break;
17469 }
17470 return p;
17471
17472 case Tag_ABI_array_object_align_expected:
17473 READ_ULEB (val, p, end);
17474 printf (" Tag_ABI_array_object_align_expected: ");
17475 switch (val)
17476 {
17477 case 0:
17478 printf (_("8-byte\n"));
17479 break;
17480 case 1:
17481 printf (_("4-byte\n"));
17482 break;
17483 case 2:
17484 printf (_("16-byte\n"));
17485 break;
17486 default:
17487 printf ("??? (%d)\n", val);
17488 break;
17489 }
17490 return p;
17491
17492 case Tag_ABI_compatibility:
17493 {
17494 READ_ULEB (val, p, end);
17495 printf (" Tag_ABI_compatibility: ");
17496 printf (_("flag = %d, vendor = "), val);
17497 if (p < end - 1)
17498 {
17499 size_t maxlen = (end - p) - 1;
17500
17501 print_symbol ((int) maxlen, (const char *) p);
17502 p += strnlen ((char *) p, maxlen) + 1;
17503 }
17504 else
17505 {
17506 printf (_("<corrupt>"));
17507 p = (unsigned char *) end;
17508 }
17509 putchar ('\n');
17510 return p;
17511 }
17512
17513 case Tag_ABI_conformance:
17514 {
17515 printf (" Tag_ABI_conformance: \"");
17516 if (p < end - 1)
17517 {
17518 size_t maxlen = (end - p) - 1;
17519
17520 print_symbol ((int) maxlen, (const char *) p);
17521 p += strnlen ((char *) p, maxlen) + 1;
17522 }
17523 else
17524 {
17525 printf (_("<corrupt>"));
17526 p = (unsigned char *) end;
17527 }
17528 printf ("\"\n");
17529 return p;
17530 }
17531 }
17532
17533 return display_tag_value (tag, p, end);
17534 }
17535
17536 static void
17537 display_raw_attribute (unsigned char * p, unsigned char const * const end)
17538 {
17539 unsigned long addr = 0;
17540 size_t bytes = end - p;
17541
17542 assert (end >= p);
17543 while (bytes)
17544 {
17545 int j;
17546 int k;
17547 int lbytes = (bytes > 16 ? 16 : bytes);
17548
17549 printf (" 0x%8.8lx ", addr);
17550
17551 for (j = 0; j < 16; j++)
17552 {
17553 if (j < lbytes)
17554 printf ("%2.2x", p[j]);
17555 else
17556 printf (" ");
17557
17558 if ((j & 3) == 3)
17559 printf (" ");
17560 }
17561
17562 for (j = 0; j < lbytes; j++)
17563 {
17564 k = p[j];
17565 if (k >= ' ' && k < 0x7f)
17566 printf ("%c", k);
17567 else
17568 printf (".");
17569 }
17570
17571 putchar ('\n');
17572
17573 p += lbytes;
17574 bytes -= lbytes;
17575 addr += lbytes;
17576 }
17577
17578 putchar ('\n');
17579 }
17580
17581 static unsigned char *
17582 display_msp430_attribute (unsigned char * p,
17583 const unsigned char * const end)
17584 {
17585 unsigned int val;
17586 unsigned int tag;
17587
17588 READ_ULEB (tag, p, end);
17589
17590 switch (tag)
17591 {
17592 case OFBA_MSPABI_Tag_ISA:
17593 printf (" Tag_ISA: ");
17594 READ_ULEB (val, p, end);
17595 switch (val)
17596 {
17597 case 0: printf (_("None\n")); break;
17598 case 1: printf (_("MSP430\n")); break;
17599 case 2: printf (_("MSP430X\n")); break;
17600 default: printf ("??? (%d)\n", val); break;
17601 }
17602 break;
17603
17604 case OFBA_MSPABI_Tag_Code_Model:
17605 printf (" Tag_Code_Model: ");
17606 READ_ULEB (val, p, end);
17607 switch (val)
17608 {
17609 case 0: printf (_("None\n")); break;
17610 case 1: printf (_("Small\n")); break;
17611 case 2: printf (_("Large\n")); break;
17612 default: printf ("??? (%d)\n", val); break;
17613 }
17614 break;
17615
17616 case OFBA_MSPABI_Tag_Data_Model:
17617 printf (" Tag_Data_Model: ");
17618 READ_ULEB (val, p, end);
17619 switch (val)
17620 {
17621 case 0: printf (_("None\n")); break;
17622 case 1: printf (_("Small\n")); break;
17623 case 2: printf (_("Large\n")); break;
17624 case 3: printf (_("Restricted Large\n")); break;
17625 default: printf ("??? (%d)\n", val); break;
17626 }
17627 break;
17628
17629 default:
17630 printf (_(" <unknown tag %d>: "), tag);
17631
17632 if (tag & 1)
17633 {
17634 putchar ('"');
17635 if (p < end - 1)
17636 {
17637 size_t maxlen = (end - p) - 1;
17638
17639 print_symbol ((int) maxlen, (const char *) p);
17640 p += strnlen ((char *) p, maxlen) + 1;
17641 }
17642 else
17643 {
17644 printf (_("<corrupt>"));
17645 p = (unsigned char *) end;
17646 }
17647 printf ("\"\n");
17648 }
17649 else
17650 {
17651 READ_ULEB (val, p, end);
17652 printf ("%d (0x%x)\n", val, val);
17653 }
17654 break;
17655 }
17656
17657 assert (p <= end);
17658 return p;
17659 }
17660
17661 static unsigned char *
17662 display_msp430_gnu_attribute (unsigned char * p,
17663 unsigned int tag,
17664 const unsigned char * const end)
17665 {
17666 if (tag == Tag_GNU_MSP430_Data_Region)
17667 {
17668 unsigned int val;
17669
17670 printf (" Tag_GNU_MSP430_Data_Region: ");
17671 READ_ULEB (val, p, end);
17672
17673 switch (val)
17674 {
17675 case Val_GNU_MSP430_Data_Region_Any:
17676 printf (_("Any Region\n"));
17677 break;
17678 case Val_GNU_MSP430_Data_Region_Lower:
17679 printf (_("Lower Region Only\n"));
17680 break;
17681 default:
17682 printf ("??? (%u)\n", val);
17683 }
17684 return p;
17685 }
17686 return display_tag_value (tag & 1, p, end);
17687 }
17688
17689 struct riscv_attr_tag_t {
17690 const char *name;
17691 unsigned int tag;
17692 };
17693
17694 static struct riscv_attr_tag_t riscv_attr_tag[] =
17695 {
17696 #define T(tag) {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
17697 T(arch),
17698 T(priv_spec),
17699 T(priv_spec_minor),
17700 T(priv_spec_revision),
17701 T(unaligned_access),
17702 T(stack_align),
17703 #undef T
17704 };
17705
17706 static unsigned char *
17707 display_riscv_attribute (unsigned char *p,
17708 const unsigned char * const end)
17709 {
17710 unsigned int val;
17711 unsigned int tag;
17712 struct riscv_attr_tag_t *attr = NULL;
17713 unsigned i;
17714
17715 READ_ULEB (tag, p, end);
17716
17717 /* Find the name of attribute. */
17718 for (i = 0; i < ARRAY_SIZE (riscv_attr_tag); i++)
17719 {
17720 if (riscv_attr_tag[i].tag == tag)
17721 {
17722 attr = &riscv_attr_tag[i];
17723 break;
17724 }
17725 }
17726
17727 if (attr)
17728 printf (" %s: ", attr->name);
17729 else
17730 return display_tag_value (tag, p, end);
17731
17732 switch (tag)
17733 {
17734 case Tag_RISCV_priv_spec:
17735 case Tag_RISCV_priv_spec_minor:
17736 case Tag_RISCV_priv_spec_revision:
17737 READ_ULEB (val, p, end);
17738 printf (_("%u\n"), val);
17739 break;
17740 case Tag_RISCV_unaligned_access:
17741 READ_ULEB (val, p, end);
17742 switch (val)
17743 {
17744 case 0:
17745 printf (_("No unaligned access\n"));
17746 break;
17747 case 1:
17748 printf (_("Unaligned access\n"));
17749 break;
17750 }
17751 break;
17752 case Tag_RISCV_stack_align:
17753 READ_ULEB (val, p, end);
17754 printf (_("%u-bytes\n"), val);
17755 break;
17756 case Tag_RISCV_arch:
17757 p = display_tag_value (-1, p, end);
17758 break;
17759 default:
17760 return display_tag_value (tag, p, end);
17761 }
17762
17763 return p;
17764 }
17765
17766 static unsigned char *
17767 display_csky_attribute (unsigned char * p,
17768 const unsigned char * const end)
17769 {
17770 unsigned int tag;
17771 unsigned int val;
17772 READ_ULEB (tag, p, end);
17773
17774 if (tag >= Tag_CSKY_MAX)
17775 {
17776 return display_tag_value (-1, p, end);
17777 }
17778
17779 switch (tag)
17780 {
17781 case Tag_CSKY_ARCH_NAME:
17782 printf (" Tag_CSKY_ARCH_NAME:\t\t");
17783 return display_tag_value (-1, p, end);
17784 case Tag_CSKY_CPU_NAME:
17785 printf (" Tag_CSKY_CPU_NAME:\t\t");
17786 return display_tag_value (-1, p, end);
17787
17788 case Tag_CSKY_ISA_FLAGS:
17789 printf (" Tag_CSKY_ISA_FLAGS:\t\t");
17790 return display_tag_value (0, p, end);
17791 case Tag_CSKY_ISA_EXT_FLAGS:
17792 printf (" Tag_CSKY_ISA_EXT_FLAGS:\t");
17793 return display_tag_value (0, p, end);
17794
17795 case Tag_CSKY_DSP_VERSION:
17796 printf (" Tag_CSKY_DSP_VERSION:\t\t");
17797 READ_ULEB (val, p, end);
17798 if (val == VAL_CSKY_DSP_VERSION_EXTENSION)
17799 printf ("DSP Extension\n");
17800 else if (val == VAL_CSKY_DSP_VERSION_2)
17801 printf ("DSP 2.0\n");
17802 break;
17803
17804 case Tag_CSKY_VDSP_VERSION:
17805 printf (" Tag_CSKY_VDSP_VERSION:\t");
17806 READ_ULEB (val, p, end);
17807 printf ("VDSP Version %d\n", val);
17808 break;
17809
17810 case Tag_CSKY_FPU_VERSION:
17811 printf (" Tag_CSKY_FPU_VERSION:\t\t");
17812 READ_ULEB (val, p, end);
17813 if (val == VAL_CSKY_FPU_VERSION_1)
17814 printf ("ABIV1 FPU Version 1\n");
17815 else if (val == VAL_CSKY_FPU_VERSION_2)
17816 printf ("FPU Version 2\n");
17817 break;
17818
17819 case Tag_CSKY_FPU_ABI:
17820 printf (" Tag_CSKY_FPU_ABI:\t\t");
17821 READ_ULEB (val, p, end);
17822 if (val == VAL_CSKY_FPU_ABI_HARD)
17823 printf ("Hard\n");
17824 else if (val == VAL_CSKY_FPU_ABI_SOFTFP)
17825 printf ("SoftFP\n");
17826 else if (val == VAL_CSKY_FPU_ABI_SOFT)
17827 printf ("Soft\n");
17828 break;
17829 case Tag_CSKY_FPU_ROUNDING:
17830 READ_ULEB (val, p, end);
17831 if (val == 1)
17832 {
17833 printf (" Tag_CSKY_FPU_ROUNDING:\t");
17834 printf ("Needed\n");
17835 }
17836 break;
17837 case Tag_CSKY_FPU_DENORMAL:
17838 READ_ULEB (val, p, end);
17839 if (val == 1)
17840 {
17841 printf (" Tag_CSKY_FPU_DENORMAL:\t");
17842 printf ("Needed\n");
17843 }
17844 break;
17845 case Tag_CSKY_FPU_Exception:
17846 READ_ULEB (val, p, end);
17847 if (val == 1)
17848 {
17849 printf (" Tag_CSKY_FPU_Exception:\t");
17850 printf ("Needed\n");
17851 }
17852 break;
17853 case Tag_CSKY_FPU_NUMBER_MODULE:
17854 printf (" Tag_CSKY_FPU_NUMBER_MODULE:\t");
17855 return display_tag_value (-1, p, end);
17856 case Tag_CSKY_FPU_HARDFP:
17857 printf (" Tag_CSKY_FPU_HARDFP:\t\t");
17858 READ_ULEB (val, p, end);
17859 if (val & VAL_CSKY_FPU_HARDFP_HALF)
17860 printf (" Half");
17861 if (val & VAL_CSKY_FPU_HARDFP_SINGLE)
17862 printf (" Single");
17863 if (val & VAL_CSKY_FPU_HARDFP_DOUBLE)
17864 printf (" Double");
17865 printf ("\n");
17866 break;
17867 default:
17868 return display_tag_value (tag, p, end);
17869 }
17870 return p;
17871 }
17872
17873 static bool
17874 process_attributes (Filedata * filedata,
17875 const char * public_name,
17876 unsigned int proc_type,
17877 unsigned char * (* display_pub_attribute) (unsigned char *, const unsigned char * const),
17878 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const))
17879 {
17880 Elf_Internal_Shdr * sect;
17881 unsigned i;
17882 bool res = true;
17883
17884 /* Find the section header so that we get the size. */
17885 for (i = 0, sect = filedata->section_headers;
17886 i < filedata->file_header.e_shnum;
17887 i++, sect++)
17888 {
17889 unsigned char * contents;
17890 unsigned char * p;
17891
17892 if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
17893 continue;
17894
17895 contents = (unsigned char *) get_data (NULL, filedata, sect->sh_offset, 1,
17896 sect->sh_size, _("attributes"));
17897 if (contents == NULL)
17898 {
17899 res = false;
17900 continue;
17901 }
17902
17903 p = contents;
17904 /* The first character is the version of the attributes.
17905 Currently only version 1, (aka 'A') is recognised here. */
17906 if (*p != 'A')
17907 {
17908 printf (_("Unknown attributes version '%c'(%d) - expecting 'A'\n"), *p, *p);
17909 res = false;
17910 }
17911 else
17912 {
17913 bfd_vma section_len;
17914
17915 section_len = sect->sh_size - 1;
17916 p++;
17917
17918 while (section_len > 0)
17919 {
17920 bfd_vma attr_len;
17921 unsigned int namelen;
17922 bool public_section;
17923 bool gnu_section;
17924
17925 if (section_len <= 4)
17926 {
17927 error (_("Tag section ends prematurely\n"));
17928 res = false;
17929 break;
17930 }
17931 attr_len = byte_get (p, 4);
17932 p += 4;
17933
17934 if (attr_len > section_len)
17935 {
17936 error (_("Bad attribute length (%u > %u)\n"),
17937 (unsigned) attr_len, (unsigned) section_len);
17938 attr_len = section_len;
17939 res = false;
17940 }
17941 /* PR 17531: file: 001-101425-0.004 */
17942 else if (attr_len < 5)
17943 {
17944 error (_("Attribute length of %u is too small\n"), (unsigned) attr_len);
17945 res = false;
17946 break;
17947 }
17948
17949 section_len -= attr_len;
17950 attr_len -= 4;
17951
17952 namelen = strnlen ((char *) p, attr_len) + 1;
17953 if (namelen == 0 || namelen >= attr_len)
17954 {
17955 error (_("Corrupt attribute section name\n"));
17956 res = false;
17957 break;
17958 }
17959
17960 printf (_("Attribute Section: "));
17961 print_symbol (INT_MAX, (const char *) p);
17962 putchar ('\n');
17963
17964 if (public_name && streq ((char *) p, public_name))
17965 public_section = true;
17966 else
17967 public_section = false;
17968
17969 if (streq ((char *) p, "gnu"))
17970 gnu_section = true;
17971 else
17972 gnu_section = false;
17973
17974 p += namelen;
17975 attr_len -= namelen;
17976
17977 while (attr_len > 0 && p < contents + sect->sh_size)
17978 {
17979 int tag;
17980 unsigned int val;
17981 bfd_vma size;
17982 unsigned char * end;
17983
17984 /* PR binutils/17531: Safe handling of corrupt files. */
17985 if (attr_len < 6)
17986 {
17987 error (_("Unused bytes at end of section\n"));
17988 res = false;
17989 section_len = 0;
17990 break;
17991 }
17992
17993 tag = *(p++);
17994 size = byte_get (p, 4);
17995 if (size > attr_len)
17996 {
17997 error (_("Bad subsection length (%u > %u)\n"),
17998 (unsigned) size, (unsigned) attr_len);
17999 res = false;
18000 size = attr_len;
18001 }
18002 /* PR binutils/17531: Safe handling of corrupt files. */
18003 if (size < 6)
18004 {
18005 error (_("Bad subsection length (%u < 6)\n"),
18006 (unsigned) size);
18007 res = false;
18008 section_len = 0;
18009 break;
18010 }
18011
18012 attr_len -= size;
18013 end = p + size - 1;
18014 assert (end <= contents + sect->sh_size);
18015 p += 4;
18016
18017 switch (tag)
18018 {
18019 case 1:
18020 printf (_("File Attributes\n"));
18021 break;
18022 case 2:
18023 printf (_("Section Attributes:"));
18024 goto do_numlist;
18025 case 3:
18026 printf (_("Symbol Attributes:"));
18027 /* Fall through. */
18028 do_numlist:
18029 for (;;)
18030 {
18031 READ_ULEB (val, p, end);
18032 if (val == 0)
18033 break;
18034 printf (" %d", val);
18035 }
18036 printf ("\n");
18037 break;
18038 default:
18039 printf (_("Unknown tag: %d\n"), tag);
18040 public_section = false;
18041 break;
18042 }
18043
18044 if (public_section && display_pub_attribute != NULL)
18045 {
18046 while (p < end)
18047 p = display_pub_attribute (p, end);
18048 assert (p == end);
18049 }
18050 else if (gnu_section && display_proc_gnu_attribute != NULL)
18051 {
18052 while (p < end)
18053 p = display_gnu_attribute (p,
18054 display_proc_gnu_attribute,
18055 end);
18056 assert (p == end);
18057 }
18058 else if (p < end)
18059 {
18060 printf (_(" Unknown attribute:\n"));
18061 display_raw_attribute (p, end);
18062 p = end;
18063 }
18064 else
18065 attr_len = 0;
18066 }
18067 }
18068 }
18069
18070 free (contents);
18071 }
18072
18073 return res;
18074 }
18075
18076 /* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
18077 Print the Address, Access and Initial fields of an entry at VMA ADDR
18078 and return the VMA of the next entry, or -1 if there was a problem.
18079 Does not read from DATA_END or beyond. */
18080
18081 static bfd_vma
18082 print_mips_got_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr,
18083 unsigned char * data_end)
18084 {
18085 printf (" ");
18086 print_vma (addr, LONG_HEX);
18087 printf (" ");
18088 if (addr < pltgot + 0xfff0)
18089 printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
18090 else
18091 printf ("%10s", "");
18092 printf (" ");
18093 if (data == NULL)
18094 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
18095 else
18096 {
18097 bfd_vma entry;
18098 unsigned char * from = data + addr - pltgot;
18099
18100 if (from + (is_32bit_elf ? 4 : 8) > data_end)
18101 {
18102 warn (_("MIPS GOT entry extends beyond the end of available data\n"));
18103 printf ("%*s", is_32bit_elf ? 8 : 16, _("<corrupt>"));
18104 return (bfd_vma) -1;
18105 }
18106 else
18107 {
18108 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
18109 print_vma (entry, LONG_HEX);
18110 }
18111 }
18112 return addr + (is_32bit_elf ? 4 : 8);
18113 }
18114
18115 /* DATA points to the contents of a MIPS PLT GOT that starts at VMA
18116 PLTGOT. Print the Address and Initial fields of an entry at VMA
18117 ADDR and return the VMA of the next entry. */
18118
18119 static bfd_vma
18120 print_mips_pltgot_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr)
18121 {
18122 printf (" ");
18123 print_vma (addr, LONG_HEX);
18124 printf (" ");
18125 if (data == NULL)
18126 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
18127 else
18128 {
18129 bfd_vma entry;
18130
18131 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
18132 print_vma (entry, LONG_HEX);
18133 }
18134 return addr + (is_32bit_elf ? 4 : 8);
18135 }
18136
18137 static void
18138 print_mips_ases (unsigned int mask)
18139 {
18140 if (mask & AFL_ASE_DSP)
18141 fputs ("\n\tDSP ASE", stdout);
18142 if (mask & AFL_ASE_DSPR2)
18143 fputs ("\n\tDSP R2 ASE", stdout);
18144 if (mask & AFL_ASE_DSPR3)
18145 fputs ("\n\tDSP R3 ASE", stdout);
18146 if (mask & AFL_ASE_EVA)
18147 fputs ("\n\tEnhanced VA Scheme", stdout);
18148 if (mask & AFL_ASE_MCU)
18149 fputs ("\n\tMCU (MicroController) ASE", stdout);
18150 if (mask & AFL_ASE_MDMX)
18151 fputs ("\n\tMDMX ASE", stdout);
18152 if (mask & AFL_ASE_MIPS3D)
18153 fputs ("\n\tMIPS-3D ASE", stdout);
18154 if (mask & AFL_ASE_MT)
18155 fputs ("\n\tMT ASE", stdout);
18156 if (mask & AFL_ASE_SMARTMIPS)
18157 fputs ("\n\tSmartMIPS ASE", stdout);
18158 if (mask & AFL_ASE_VIRT)
18159 fputs ("\n\tVZ ASE", stdout);
18160 if (mask & AFL_ASE_MSA)
18161 fputs ("\n\tMSA ASE", stdout);
18162 if (mask & AFL_ASE_MIPS16)
18163 fputs ("\n\tMIPS16 ASE", stdout);
18164 if (mask & AFL_ASE_MICROMIPS)
18165 fputs ("\n\tMICROMIPS ASE", stdout);
18166 if (mask & AFL_ASE_XPA)
18167 fputs ("\n\tXPA ASE", stdout);
18168 if (mask & AFL_ASE_MIPS16E2)
18169 fputs ("\n\tMIPS16e2 ASE", stdout);
18170 if (mask & AFL_ASE_CRC)
18171 fputs ("\n\tCRC ASE", stdout);
18172 if (mask & AFL_ASE_GINV)
18173 fputs ("\n\tGINV ASE", stdout);
18174 if (mask & AFL_ASE_LOONGSON_MMI)
18175 fputs ("\n\tLoongson MMI ASE", stdout);
18176 if (mask & AFL_ASE_LOONGSON_CAM)
18177 fputs ("\n\tLoongson CAM ASE", stdout);
18178 if (mask & AFL_ASE_LOONGSON_EXT)
18179 fputs ("\n\tLoongson EXT ASE", stdout);
18180 if (mask & AFL_ASE_LOONGSON_EXT2)
18181 fputs ("\n\tLoongson EXT2 ASE", stdout);
18182 if (mask == 0)
18183 fprintf (stdout, "\n\t%s", _("None"));
18184 else if ((mask & ~AFL_ASE_MASK) != 0)
18185 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
18186 }
18187
18188 static void
18189 print_mips_isa_ext (unsigned int isa_ext)
18190 {
18191 switch (isa_ext)
18192 {
18193 case 0:
18194 fputs (_("None"), stdout);
18195 break;
18196 case AFL_EXT_XLR:
18197 fputs ("RMI XLR", stdout);
18198 break;
18199 case AFL_EXT_OCTEON3:
18200 fputs ("Cavium Networks Octeon3", stdout);
18201 break;
18202 case AFL_EXT_OCTEON2:
18203 fputs ("Cavium Networks Octeon2", stdout);
18204 break;
18205 case AFL_EXT_OCTEONP:
18206 fputs ("Cavium Networks OcteonP", stdout);
18207 break;
18208 case AFL_EXT_OCTEON:
18209 fputs ("Cavium Networks Octeon", stdout);
18210 break;
18211 case AFL_EXT_5900:
18212 fputs ("Toshiba R5900", stdout);
18213 break;
18214 case AFL_EXT_4650:
18215 fputs ("MIPS R4650", stdout);
18216 break;
18217 case AFL_EXT_4010:
18218 fputs ("LSI R4010", stdout);
18219 break;
18220 case AFL_EXT_4100:
18221 fputs ("NEC VR4100", stdout);
18222 break;
18223 case AFL_EXT_3900:
18224 fputs ("Toshiba R3900", stdout);
18225 break;
18226 case AFL_EXT_10000:
18227 fputs ("MIPS R10000", stdout);
18228 break;
18229 case AFL_EXT_SB1:
18230 fputs ("Broadcom SB-1", stdout);
18231 break;
18232 case AFL_EXT_4111:
18233 fputs ("NEC VR4111/VR4181", stdout);
18234 break;
18235 case AFL_EXT_4120:
18236 fputs ("NEC VR4120", stdout);
18237 break;
18238 case AFL_EXT_5400:
18239 fputs ("NEC VR5400", stdout);
18240 break;
18241 case AFL_EXT_5500:
18242 fputs ("NEC VR5500", stdout);
18243 break;
18244 case AFL_EXT_LOONGSON_2E:
18245 fputs ("ST Microelectronics Loongson 2E", stdout);
18246 break;
18247 case AFL_EXT_LOONGSON_2F:
18248 fputs ("ST Microelectronics Loongson 2F", stdout);
18249 break;
18250 case AFL_EXT_INTERAPTIV_MR2:
18251 fputs ("Imagination interAptiv MR2", stdout);
18252 break;
18253 default:
18254 fprintf (stdout, "%s (%d)", _("Unknown"), isa_ext);
18255 }
18256 }
18257
18258 static signed int
18259 get_mips_reg_size (int reg_size)
18260 {
18261 return (reg_size == AFL_REG_NONE) ? 0
18262 : (reg_size == AFL_REG_32) ? 32
18263 : (reg_size == AFL_REG_64) ? 64
18264 : (reg_size == AFL_REG_128) ? 128
18265 : -1;
18266 }
18267
18268 static bool
18269 process_mips_specific (Filedata * filedata)
18270 {
18271 Elf_Internal_Dyn * entry;
18272 Elf_Internal_Shdr *sect = NULL;
18273 size_t liblist_offset = 0;
18274 size_t liblistno = 0;
18275 size_t conflictsno = 0;
18276 size_t options_offset = 0;
18277 size_t conflicts_offset = 0;
18278 size_t pltrelsz = 0;
18279 size_t pltrel = 0;
18280 bfd_vma pltgot = 0;
18281 bfd_vma mips_pltgot = 0;
18282 bfd_vma jmprel = 0;
18283 bfd_vma local_gotno = 0;
18284 bfd_vma gotsym = 0;
18285 bfd_vma symtabno = 0;
18286 bool res = true;
18287
18288 if (! process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
18289 display_mips_gnu_attribute))
18290 res = false;
18291
18292 sect = find_section (filedata, ".MIPS.abiflags");
18293
18294 if (sect != NULL)
18295 {
18296 Elf_External_ABIFlags_v0 *abiflags_ext;
18297 Elf_Internal_ABIFlags_v0 abiflags_in;
18298
18299 if (sizeof (Elf_External_ABIFlags_v0) != sect->sh_size)
18300 {
18301 error (_("Corrupt MIPS ABI Flags section.\n"));
18302 res = false;
18303 }
18304 else
18305 {
18306 abiflags_ext = get_data (NULL, filedata, sect->sh_offset, 1,
18307 sect->sh_size, _("MIPS ABI Flags section"));
18308 if (abiflags_ext)
18309 {
18310 abiflags_in.version = BYTE_GET (abiflags_ext->version);
18311 abiflags_in.isa_level = BYTE_GET (abiflags_ext->isa_level);
18312 abiflags_in.isa_rev = BYTE_GET (abiflags_ext->isa_rev);
18313 abiflags_in.gpr_size = BYTE_GET (abiflags_ext->gpr_size);
18314 abiflags_in.cpr1_size = BYTE_GET (abiflags_ext->cpr1_size);
18315 abiflags_in.cpr2_size = BYTE_GET (abiflags_ext->cpr2_size);
18316 abiflags_in.fp_abi = BYTE_GET (abiflags_ext->fp_abi);
18317 abiflags_in.isa_ext = BYTE_GET (abiflags_ext->isa_ext);
18318 abiflags_in.ases = BYTE_GET (abiflags_ext->ases);
18319 abiflags_in.flags1 = BYTE_GET (abiflags_ext->flags1);
18320 abiflags_in.flags2 = BYTE_GET (abiflags_ext->flags2);
18321
18322 printf ("\nMIPS ABI Flags Version: %d\n", abiflags_in.version);
18323 printf ("\nISA: MIPS%d", abiflags_in.isa_level);
18324 if (abiflags_in.isa_rev > 1)
18325 printf ("r%d", abiflags_in.isa_rev);
18326 printf ("\nGPR size: %d",
18327 get_mips_reg_size (abiflags_in.gpr_size));
18328 printf ("\nCPR1 size: %d",
18329 get_mips_reg_size (abiflags_in.cpr1_size));
18330 printf ("\nCPR2 size: %d",
18331 get_mips_reg_size (abiflags_in.cpr2_size));
18332 fputs ("\nFP ABI: ", stdout);
18333 print_mips_fp_abi_value (abiflags_in.fp_abi);
18334 fputs ("ISA Extension: ", stdout);
18335 print_mips_isa_ext (abiflags_in.isa_ext);
18336 fputs ("\nASEs:", stdout);
18337 print_mips_ases (abiflags_in.ases);
18338 printf ("\nFLAGS 1: %8.8lx", abiflags_in.flags1);
18339 printf ("\nFLAGS 2: %8.8lx", abiflags_in.flags2);
18340 fputc ('\n', stdout);
18341 free (abiflags_ext);
18342 }
18343 }
18344 }
18345
18346 /* We have a lot of special sections. Thanks SGI! */
18347 if (filedata->dynamic_section == NULL)
18348 {
18349 /* No dynamic information available. See if there is static GOT. */
18350 sect = find_section (filedata, ".got");
18351 if (sect != NULL)
18352 {
18353 unsigned char *data_end;
18354 unsigned char *data;
18355 bfd_vma ent, end;
18356 int addr_size;
18357
18358 pltgot = sect->sh_addr;
18359
18360 ent = pltgot;
18361 addr_size = (is_32bit_elf ? 4 : 8);
18362 end = pltgot + sect->sh_size;
18363
18364 data = (unsigned char *) get_data (NULL, filedata, sect->sh_offset,
18365 end - pltgot, 1,
18366 _("Global Offset Table data"));
18367 /* PR 12855: Null data is handled gracefully throughout. */
18368 data_end = data + (end - pltgot);
18369
18370 printf (_("\nStatic GOT:\n"));
18371 printf (_(" Canonical gp value: "));
18372 print_vma (ent + 0x7ff0, LONG_HEX);
18373 printf ("\n\n");
18374
18375 /* In a dynamic binary GOT[0] is reserved for the dynamic
18376 loader to store the lazy resolver pointer, however in
18377 a static binary it may well have been omitted and GOT
18378 reduced to a table of addresses.
18379 PR 21344: Check for the entry being fully available
18380 before fetching it. */
18381 if (data
18382 && data + ent - pltgot + addr_size <= data_end
18383 && byte_get (data + ent - pltgot, addr_size) == 0)
18384 {
18385 printf (_(" Reserved entries:\n"));
18386 printf (_(" %*s %10s %*s\n"),
18387 addr_size * 2, _("Address"), _("Access"),
18388 addr_size * 2, _("Value"));
18389 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18390 printf ("\n");
18391 if (ent == (bfd_vma) -1)
18392 goto sgot_print_fail;
18393
18394 /* Check for the MSB of GOT[1] being set, identifying a
18395 GNU object. This entry will be used by some runtime
18396 loaders, to store the module pointer. Otherwise this
18397 is an ordinary local entry.
18398 PR 21344: Check for the entry being fully available
18399 before fetching it. */
18400 if (data
18401 && data + ent - pltgot + addr_size <= data_end
18402 && (byte_get (data + ent - pltgot, addr_size)
18403 >> (addr_size * 8 - 1)) != 0)
18404 {
18405 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18406 printf ("\n");
18407 if (ent == (bfd_vma) -1)
18408 goto sgot_print_fail;
18409 }
18410 printf ("\n");
18411 }
18412
18413 if (data != NULL && ent < end)
18414 {
18415 printf (_(" Local entries:\n"));
18416 printf (" %*s %10s %*s\n",
18417 addr_size * 2, _("Address"), _("Access"),
18418 addr_size * 2, _("Value"));
18419 while (ent < end)
18420 {
18421 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18422 printf ("\n");
18423 if (ent == (bfd_vma) -1)
18424 goto sgot_print_fail;
18425 }
18426 printf ("\n");
18427 }
18428
18429 sgot_print_fail:
18430 free (data);
18431 }
18432 return res;
18433 }
18434
18435 for (entry = filedata->dynamic_section;
18436 /* PR 17531 file: 012-50589-0.004. */
18437 (entry < filedata->dynamic_section + filedata->dynamic_nent
18438 && entry->d_tag != DT_NULL);
18439 ++entry)
18440 switch (entry->d_tag)
18441 {
18442 case DT_MIPS_LIBLIST:
18443 liblist_offset
18444 = offset_from_vma (filedata, entry->d_un.d_val,
18445 liblistno * sizeof (Elf32_External_Lib));
18446 break;
18447 case DT_MIPS_LIBLISTNO:
18448 liblistno = entry->d_un.d_val;
18449 break;
18450 case DT_MIPS_OPTIONS:
18451 options_offset = offset_from_vma (filedata, entry->d_un.d_val, 0);
18452 break;
18453 case DT_MIPS_CONFLICT:
18454 conflicts_offset
18455 = offset_from_vma (filedata, entry->d_un.d_val,
18456 conflictsno * sizeof (Elf32_External_Conflict));
18457 break;
18458 case DT_MIPS_CONFLICTNO:
18459 conflictsno = entry->d_un.d_val;
18460 break;
18461 case DT_PLTGOT:
18462 pltgot = entry->d_un.d_ptr;
18463 break;
18464 case DT_MIPS_LOCAL_GOTNO:
18465 local_gotno = entry->d_un.d_val;
18466 break;
18467 case DT_MIPS_GOTSYM:
18468 gotsym = entry->d_un.d_val;
18469 break;
18470 case DT_MIPS_SYMTABNO:
18471 symtabno = entry->d_un.d_val;
18472 break;
18473 case DT_MIPS_PLTGOT:
18474 mips_pltgot = entry->d_un.d_ptr;
18475 break;
18476 case DT_PLTREL:
18477 pltrel = entry->d_un.d_val;
18478 break;
18479 case DT_PLTRELSZ:
18480 pltrelsz = entry->d_un.d_val;
18481 break;
18482 case DT_JMPREL:
18483 jmprel = entry->d_un.d_ptr;
18484 break;
18485 default:
18486 break;
18487 }
18488
18489 if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
18490 {
18491 Elf32_External_Lib * elib;
18492 size_t cnt;
18493
18494 elib = (Elf32_External_Lib *) get_data (NULL, filedata, liblist_offset,
18495 sizeof (Elf32_External_Lib),
18496 liblistno,
18497 _("liblist section data"));
18498 if (elib)
18499 {
18500 printf (ngettext ("\nSection '.liblist' contains %lu entry:\n",
18501 "\nSection '.liblist' contains %lu entries:\n",
18502 (unsigned long) liblistno),
18503 (unsigned long) liblistno);
18504 fputs (_(" Library Time Stamp Checksum Version Flags\n"),
18505 stdout);
18506
18507 for (cnt = 0; cnt < liblistno; ++cnt)
18508 {
18509 Elf32_Lib liblist;
18510 time_t atime;
18511 char timebuf[128];
18512 struct tm * tmp;
18513
18514 liblist.l_name = BYTE_GET (elib[cnt].l_name);
18515 atime = BYTE_GET (elib[cnt].l_time_stamp);
18516 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
18517 liblist.l_version = BYTE_GET (elib[cnt].l_version);
18518 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
18519
18520 tmp = gmtime (&atime);
18521 snprintf (timebuf, sizeof (timebuf),
18522 "%04u-%02u-%02uT%02u:%02u:%02u",
18523 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
18524 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
18525
18526 printf ("%3lu: ", (unsigned long) cnt);
18527 if (valid_dynamic_name (filedata, liblist.l_name))
18528 print_symbol (20, get_dynamic_name (filedata, liblist.l_name));
18529 else
18530 printf (_("<corrupt: %9ld>"), liblist.l_name);
18531 printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
18532 liblist.l_version);
18533
18534 if (liblist.l_flags == 0)
18535 puts (_(" NONE"));
18536 else
18537 {
18538 static const struct
18539 {
18540 const char * name;
18541 int bit;
18542 }
18543 l_flags_vals[] =
18544 {
18545 { " EXACT_MATCH", LL_EXACT_MATCH },
18546 { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
18547 { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
18548 { " EXPORTS", LL_EXPORTS },
18549 { " DELAY_LOAD", LL_DELAY_LOAD },
18550 { " DELTA", LL_DELTA }
18551 };
18552 int flags = liblist.l_flags;
18553 size_t fcnt;
18554
18555 for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
18556 if ((flags & l_flags_vals[fcnt].bit) != 0)
18557 {
18558 fputs (l_flags_vals[fcnt].name, stdout);
18559 flags ^= l_flags_vals[fcnt].bit;
18560 }
18561 if (flags != 0)
18562 printf (" %#x", (unsigned int) flags);
18563
18564 puts ("");
18565 }
18566 }
18567
18568 free (elib);
18569 }
18570 else
18571 res = false;
18572 }
18573
18574 if (options_offset != 0)
18575 {
18576 Elf_External_Options * eopt;
18577 size_t offset;
18578 int cnt;
18579
18580 /* Find the section header so that we get the size. */
18581 sect = find_section_by_type (filedata, SHT_MIPS_OPTIONS);
18582 /* PR 17533 file: 012-277276-0.004. */
18583 if (sect == NULL)
18584 {
18585 error (_("No MIPS_OPTIONS header found\n"));
18586 return false;
18587 }
18588 /* PR 24243 */
18589 if (sect->sh_size < sizeof (* eopt))
18590 {
18591 error (_("The MIPS options section is too small.\n"));
18592 return false;
18593 }
18594
18595 eopt = (Elf_External_Options *) get_data (NULL, filedata, options_offset, 1,
18596 sect->sh_size, _("options"));
18597 if (eopt)
18598 {
18599 Elf_Internal_Options option;
18600
18601 offset = cnt = 0;
18602 while (offset <= sect->sh_size - sizeof (* eopt))
18603 {
18604 Elf_External_Options * eoption;
18605 unsigned int optsize;
18606
18607 eoption = (Elf_External_Options *) ((char *) eopt + offset);
18608
18609 optsize = BYTE_GET (eoption->size);
18610
18611 /* PR 17531: file: ffa0fa3b. */
18612 if (optsize < sizeof (* eopt)
18613 || optsize > sect->sh_size - offset)
18614 {
18615 error (_("Invalid size (%u) for MIPS option\n"),
18616 optsize);
18617 free (eopt);
18618 return false;
18619 }
18620 offset += optsize;
18621 ++cnt;
18622 }
18623
18624 printf (ngettext ("\nSection '%s' contains %d entry:\n",
18625 "\nSection '%s' contains %d entries:\n",
18626 cnt),
18627 printable_section_name (filedata, sect), cnt);
18628
18629 offset = 0;
18630 while (cnt-- > 0)
18631 {
18632 size_t len;
18633 Elf_External_Options * eoption;
18634
18635 eoption = (Elf_External_Options *) ((char *) eopt + offset);
18636
18637 option.kind = BYTE_GET (eoption->kind);
18638 option.size = BYTE_GET (eoption->size);
18639 option.section = BYTE_GET (eoption->section);
18640 option.info = BYTE_GET (eoption->info);
18641
18642 switch (option.kind)
18643 {
18644 case ODK_NULL:
18645 /* This shouldn't happen. */
18646 printf (" NULL %" PRId16 " %" PRIx32,
18647 option.section, option.info);
18648 break;
18649
18650 case ODK_REGINFO:
18651 printf (" REGINFO ");
18652 if (filedata->file_header.e_machine == EM_MIPS)
18653 {
18654 Elf32_External_RegInfo * ereg;
18655 Elf32_RegInfo reginfo;
18656
18657 /* 32bit form. */
18658 if (option.size < (sizeof (Elf_External_Options)
18659 + sizeof (Elf32_External_RegInfo)))
18660 {
18661 printf (_("<corrupt>\n"));
18662 error (_("Truncated MIPS REGINFO option\n"));
18663 cnt = 0;
18664 break;
18665 }
18666
18667 ereg = (Elf32_External_RegInfo *) (eoption + 1);
18668
18669 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
18670 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
18671 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
18672 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
18673 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
18674 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
18675
18676 printf ("GPR %08" PRIx32 " GP 0x%" PRIx32 "\n",
18677 reginfo.ri_gprmask, reginfo.ri_gp_value);
18678 printf (" "
18679 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
18680 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
18681 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
18682 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
18683 }
18684 else
18685 {
18686 /* 64 bit form. */
18687 Elf64_External_RegInfo * ereg;
18688 Elf64_Internal_RegInfo reginfo;
18689
18690 if (option.size < (sizeof (Elf_External_Options)
18691 + sizeof (Elf64_External_RegInfo)))
18692 {
18693 printf (_("<corrupt>\n"));
18694 error (_("Truncated MIPS REGINFO option\n"));
18695 cnt = 0;
18696 break;
18697 }
18698
18699 ereg = (Elf64_External_RegInfo *) (eoption + 1);
18700 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
18701 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
18702 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
18703 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
18704 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
18705 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
18706
18707 printf ("GPR %08" PRIx32 " GP 0x%" PRIx64 "\n",
18708 reginfo.ri_gprmask, reginfo.ri_gp_value);
18709 printf (" "
18710 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
18711 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
18712 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
18713 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
18714 }
18715 offset += option.size;
18716 continue;
18717
18718 case ODK_EXCEPTIONS:
18719 fputs (" EXCEPTIONS fpe_min(", stdout);
18720 process_mips_fpe_exception (option.info & OEX_FPU_MIN);
18721 fputs (") fpe_max(", stdout);
18722 process_mips_fpe_exception ((option.info & OEX_FPU_MAX) >> 8);
18723 fputs (")", stdout);
18724
18725 if (option.info & OEX_PAGE0)
18726 fputs (" PAGE0", stdout);
18727 if (option.info & OEX_SMM)
18728 fputs (" SMM", stdout);
18729 if (option.info & OEX_FPDBUG)
18730 fputs (" FPDBUG", stdout);
18731 if (option.info & OEX_DISMISS)
18732 fputs (" DISMISS", stdout);
18733 break;
18734
18735 case ODK_PAD:
18736 fputs (" PAD ", stdout);
18737 if (option.info & OPAD_PREFIX)
18738 fputs (" PREFIX", stdout);
18739 if (option.info & OPAD_POSTFIX)
18740 fputs (" POSTFIX", stdout);
18741 if (option.info & OPAD_SYMBOL)
18742 fputs (" SYMBOL", stdout);
18743 break;
18744
18745 case ODK_HWPATCH:
18746 fputs (" HWPATCH ", stdout);
18747 if (option.info & OHW_R4KEOP)
18748 fputs (" R4KEOP", stdout);
18749 if (option.info & OHW_R8KPFETCH)
18750 fputs (" R8KPFETCH", stdout);
18751 if (option.info & OHW_R5KEOP)
18752 fputs (" R5KEOP", stdout);
18753 if (option.info & OHW_R5KCVTL)
18754 fputs (" R5KCVTL", stdout);
18755 break;
18756
18757 case ODK_FILL:
18758 fputs (" FILL ", stdout);
18759 /* XXX Print content of info word? */
18760 break;
18761
18762 case ODK_TAGS:
18763 fputs (" TAGS ", stdout);
18764 /* XXX Print content of info word? */
18765 break;
18766
18767 case ODK_HWAND:
18768 fputs (" HWAND ", stdout);
18769 if (option.info & OHWA0_R4KEOP_CHECKED)
18770 fputs (" R4KEOP_CHECKED", stdout);
18771 if (option.info & OHWA0_R4KEOP_CLEAN)
18772 fputs (" R4KEOP_CLEAN", stdout);
18773 break;
18774
18775 case ODK_HWOR:
18776 fputs (" HWOR ", stdout);
18777 if (option.info & OHWA0_R4KEOP_CHECKED)
18778 fputs (" R4KEOP_CHECKED", stdout);
18779 if (option.info & OHWA0_R4KEOP_CLEAN)
18780 fputs (" R4KEOP_CLEAN", stdout);
18781 break;
18782
18783 case ODK_GP_GROUP:
18784 printf (" GP_GROUP %#06x self-contained %#06x",
18785 option.info & OGP_GROUP,
18786 (option.info & OGP_SELF) >> 16);
18787 break;
18788
18789 case ODK_IDENT:
18790 printf (" IDENT %#06x self-contained %#06x",
18791 option.info & OGP_GROUP,
18792 (option.info & OGP_SELF) >> 16);
18793 break;
18794
18795 default:
18796 /* This shouldn't happen. */
18797 printf (" %3d ??? %" PRId16 " %" PRIx32,
18798 option.kind, option.section, option.info);
18799 break;
18800 }
18801
18802 len = sizeof (* eopt);
18803 while (len < option.size)
18804 {
18805 unsigned char datum = *((unsigned char *) eoption + len);
18806
18807 if (ISPRINT (datum))
18808 printf ("%c", datum);
18809 else
18810 printf ("\\%03o", datum);
18811 len ++;
18812 }
18813 fputs ("\n", stdout);
18814
18815 offset += option.size;
18816 }
18817 free (eopt);
18818 }
18819 else
18820 res = false;
18821 }
18822
18823 if (conflicts_offset != 0 && conflictsno != 0)
18824 {
18825 Elf32_Conflict * iconf;
18826 size_t cnt;
18827
18828 if (filedata->dynamic_symbols == NULL)
18829 {
18830 error (_("conflict list found without a dynamic symbol table\n"));
18831 return false;
18832 }
18833
18834 /* PR 21345 - print a slightly more helpful error message
18835 if we are sure that the cmalloc will fail. */
18836 if (conflictsno > filedata->file_size / sizeof (* iconf))
18837 {
18838 error (_("Overlarge number of conflicts detected: %lx\n"),
18839 (long) conflictsno);
18840 return false;
18841 }
18842
18843 iconf = (Elf32_Conflict *) cmalloc (conflictsno, sizeof (* iconf));
18844 if (iconf == NULL)
18845 {
18846 error (_("Out of memory allocating space for dynamic conflicts\n"));
18847 return false;
18848 }
18849
18850 if (is_32bit_elf)
18851 {
18852 Elf32_External_Conflict * econf32;
18853
18854 econf32 = (Elf32_External_Conflict *)
18855 get_data (NULL, filedata, conflicts_offset,
18856 sizeof (*econf32), conflictsno, _("conflict"));
18857 if (!econf32)
18858 {
18859 free (iconf);
18860 return false;
18861 }
18862
18863 for (cnt = 0; cnt < conflictsno; ++cnt)
18864 iconf[cnt] = BYTE_GET (econf32[cnt]);
18865
18866 free (econf32);
18867 }
18868 else
18869 {
18870 Elf64_External_Conflict * econf64;
18871
18872 econf64 = (Elf64_External_Conflict *)
18873 get_data (NULL, filedata, conflicts_offset,
18874 sizeof (*econf64), conflictsno, _("conflict"));
18875 if (!econf64)
18876 {
18877 free (iconf);
18878 return false;
18879 }
18880
18881 for (cnt = 0; cnt < conflictsno; ++cnt)
18882 iconf[cnt] = BYTE_GET (econf64[cnt]);
18883
18884 free (econf64);
18885 }
18886
18887 printf (ngettext ("\nSection '.conflict' contains %lu entry:\n",
18888 "\nSection '.conflict' contains %lu entries:\n",
18889 (unsigned long) conflictsno),
18890 (unsigned long) conflictsno);
18891 puts (_(" Num: Index Value Name"));
18892
18893 for (cnt = 0; cnt < conflictsno; ++cnt)
18894 {
18895 printf ("%5lu: %8lu ", (unsigned long) cnt, iconf[cnt]);
18896
18897 if (iconf[cnt] >= filedata->num_dynamic_syms)
18898 printf (_("<corrupt symbol index>"));
18899 else
18900 {
18901 Elf_Internal_Sym * psym;
18902
18903 psym = & filedata->dynamic_symbols[iconf[cnt]];
18904 print_vma (psym->st_value, FULL_HEX);
18905 putchar (' ');
18906 if (valid_dynamic_name (filedata, psym->st_name))
18907 print_symbol (25, get_dynamic_name (filedata, psym->st_name));
18908 else
18909 printf (_("<corrupt: %14ld>"), psym->st_name);
18910 }
18911 putchar ('\n');
18912 }
18913
18914 free (iconf);
18915 }
18916
18917 if (pltgot != 0 && local_gotno != 0)
18918 {
18919 bfd_vma ent, local_end, global_end;
18920 size_t i, offset;
18921 unsigned char * data;
18922 unsigned char * data_end;
18923 int addr_size;
18924
18925 ent = pltgot;
18926 addr_size = (is_32bit_elf ? 4 : 8);
18927 local_end = pltgot + local_gotno * addr_size;
18928
18929 /* PR binutils/17533 file: 012-111227-0.004 */
18930 if (symtabno < gotsym)
18931 {
18932 error (_("The GOT symbol offset (%lu) is greater than the symbol table size (%lu)\n"),
18933 (unsigned long) gotsym, (unsigned long) symtabno);
18934 return false;
18935 }
18936
18937 global_end = local_end + (symtabno - gotsym) * addr_size;
18938 /* PR 17531: file: 54c91a34. */
18939 if (global_end < local_end)
18940 {
18941 error (_("Too many GOT symbols: %lu\n"), (unsigned long) symtabno);
18942 return false;
18943 }
18944
18945 offset = offset_from_vma (filedata, pltgot, global_end - pltgot);
18946 data = (unsigned char *) get_data (NULL, filedata, offset,
18947 global_end - pltgot, 1,
18948 _("Global Offset Table data"));
18949 /* PR 12855: Null data is handled gracefully throughout. */
18950 data_end = data + (global_end - pltgot);
18951
18952 printf (_("\nPrimary GOT:\n"));
18953 printf (_(" Canonical gp value: "));
18954 print_vma (pltgot + 0x7ff0, LONG_HEX);
18955 printf ("\n\n");
18956
18957 printf (_(" Reserved entries:\n"));
18958 printf (_(" %*s %10s %*s Purpose\n"),
18959 addr_size * 2, _("Address"), _("Access"),
18960 addr_size * 2, _("Initial"));
18961 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18962 printf (_(" Lazy resolver\n"));
18963 if (ent == (bfd_vma) -1)
18964 goto got_print_fail;
18965
18966 /* Check for the MSB of GOT[1] being set, denoting a GNU object.
18967 This entry will be used by some runtime loaders, to store the
18968 module pointer. Otherwise this is an ordinary local entry.
18969 PR 21344: Check for the entry being fully available before
18970 fetching it. */
18971 if (data
18972 && data + ent - pltgot + addr_size <= data_end
18973 && (byte_get (data + ent - pltgot, addr_size)
18974 >> (addr_size * 8 - 1)) != 0)
18975 {
18976 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18977 printf (_(" Module pointer (GNU extension)\n"));
18978 if (ent == (bfd_vma) -1)
18979 goto got_print_fail;
18980 }
18981 printf ("\n");
18982
18983 if (data != NULL && ent < local_end)
18984 {
18985 printf (_(" Local entries:\n"));
18986 printf (" %*s %10s %*s\n",
18987 addr_size * 2, _("Address"), _("Access"),
18988 addr_size * 2, _("Initial"));
18989 while (ent < local_end)
18990 {
18991 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18992 printf ("\n");
18993 if (ent == (bfd_vma) -1)
18994 goto got_print_fail;
18995 }
18996 printf ("\n");
18997 }
18998
18999 if (data != NULL && gotsym < symtabno)
19000 {
19001 int sym_width;
19002
19003 printf (_(" Global entries:\n"));
19004 printf (" %*s %10s %*s %*s %-7s %3s %s\n",
19005 addr_size * 2, _("Address"),
19006 _("Access"),
19007 addr_size * 2, _("Initial"),
19008 addr_size * 2, _("Sym.Val."),
19009 _("Type"),
19010 /* Note for translators: "Ndx" = abbreviated form of "Index". */
19011 _("Ndx"), _("Name"));
19012
19013 sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
19014
19015 for (i = gotsym; i < symtabno; i++)
19016 {
19017 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19018 printf (" ");
19019
19020 if (filedata->dynamic_symbols == NULL)
19021 printf (_("<no dynamic symbols>"));
19022 else if (i < filedata->num_dynamic_syms)
19023 {
19024 Elf_Internal_Sym * psym = filedata->dynamic_symbols + i;
19025
19026 print_vma (psym->st_value, LONG_HEX);
19027 printf (" %-7s %3s ",
19028 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
19029 get_symbol_index_type (filedata, psym->st_shndx));
19030
19031 if (valid_dynamic_name (filedata, psym->st_name))
19032 print_symbol (sym_width,
19033 get_dynamic_name (filedata, psym->st_name));
19034 else
19035 printf (_("<corrupt: %14ld>"), psym->st_name);
19036 }
19037 else
19038 printf (_("<symbol index %lu exceeds number of dynamic symbols>"),
19039 (unsigned long) i);
19040
19041 printf ("\n");
19042 if (ent == (bfd_vma) -1)
19043 break;
19044 }
19045 printf ("\n");
19046 }
19047
19048 got_print_fail:
19049 free (data);
19050 }
19051
19052 if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
19053 {
19054 bfd_vma ent, end;
19055 size_t offset, rel_offset;
19056 unsigned long count, i;
19057 unsigned char * data;
19058 int addr_size, sym_width;
19059 Elf_Internal_Rela * rels;
19060
19061 rel_offset = offset_from_vma (filedata, jmprel, pltrelsz);
19062 if (pltrel == DT_RELA)
19063 {
19064 if (!slurp_rela_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
19065 return false;
19066 }
19067 else
19068 {
19069 if (!slurp_rel_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
19070 return false;
19071 }
19072
19073 ent = mips_pltgot;
19074 addr_size = (is_32bit_elf ? 4 : 8);
19075 end = mips_pltgot + (2 + count) * addr_size;
19076
19077 offset = offset_from_vma (filedata, mips_pltgot, end - mips_pltgot);
19078 data = (unsigned char *) get_data (NULL, filedata, offset, end - mips_pltgot,
19079 1, _("Procedure Linkage Table data"));
19080 if (data == NULL)
19081 {
19082 free (rels);
19083 return false;
19084 }
19085
19086 printf ("\nPLT GOT:\n\n");
19087 printf (_(" Reserved entries:\n"));
19088 printf (_(" %*s %*s Purpose\n"),
19089 addr_size * 2, _("Address"), addr_size * 2, _("Initial"));
19090 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
19091 printf (_(" PLT lazy resolver\n"));
19092 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
19093 printf (_(" Module pointer\n"));
19094 printf ("\n");
19095
19096 printf (_(" Entries:\n"));
19097 printf (" %*s %*s %*s %-7s %3s %s\n",
19098 addr_size * 2, _("Address"),
19099 addr_size * 2, _("Initial"),
19100 addr_size * 2, _("Sym.Val."), _("Type"), _("Ndx"), _("Name"));
19101 sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
19102 for (i = 0; i < count; i++)
19103 {
19104 unsigned long idx = get_reloc_symindex (rels[i].r_info);
19105
19106 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
19107 printf (" ");
19108
19109 if (idx >= filedata->num_dynamic_syms)
19110 printf (_("<corrupt symbol index: %lu>"), idx);
19111 else
19112 {
19113 Elf_Internal_Sym * psym = filedata->dynamic_symbols + idx;
19114
19115 print_vma (psym->st_value, LONG_HEX);
19116 printf (" %-7s %3s ",
19117 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
19118 get_symbol_index_type (filedata, psym->st_shndx));
19119 if (valid_dynamic_name (filedata, psym->st_name))
19120 print_symbol (sym_width,
19121 get_dynamic_name (filedata, psym->st_name));
19122 else
19123 printf (_("<corrupt: %14ld>"), psym->st_name);
19124 }
19125 printf ("\n");
19126 }
19127 printf ("\n");
19128
19129 free (data);
19130 free (rels);
19131 }
19132
19133 return res;
19134 }
19135
19136 static bool
19137 process_nds32_specific (Filedata * filedata)
19138 {
19139 Elf_Internal_Shdr *sect = NULL;
19140
19141 sect = find_section (filedata, ".nds32_e_flags");
19142 if (sect != NULL && sect->sh_size >= 4)
19143 {
19144 unsigned char *buf;
19145 unsigned int flag;
19146
19147 printf ("\nNDS32 elf flags section:\n");
19148 buf = get_data (NULL, filedata, sect->sh_offset, 1, 4,
19149 _("NDS32 elf flags section"));
19150
19151 if (buf == NULL)
19152 return false;
19153
19154 flag = byte_get (buf, 4);
19155 free (buf);
19156 switch (flag & 0x3)
19157 {
19158 case 0:
19159 printf ("(VEC_SIZE):\tNo entry.\n");
19160 break;
19161 case 1:
19162 printf ("(VEC_SIZE):\t4 bytes\n");
19163 break;
19164 case 2:
19165 printf ("(VEC_SIZE):\t16 bytes\n");
19166 break;
19167 case 3:
19168 printf ("(VEC_SIZE):\treserved\n");
19169 break;
19170 }
19171 }
19172
19173 return true;
19174 }
19175
19176 static bool
19177 process_gnu_liblist (Filedata * filedata)
19178 {
19179 Elf_Internal_Shdr * section;
19180 Elf_Internal_Shdr * string_sec;
19181 Elf32_External_Lib * elib;
19182 char * strtab;
19183 size_t strtab_size;
19184 size_t cnt;
19185 unsigned long num_liblist;
19186 unsigned i;
19187 bool res = true;
19188
19189 if (! do_arch)
19190 return true;
19191
19192 for (i = 0, section = filedata->section_headers;
19193 i < filedata->file_header.e_shnum;
19194 i++, section++)
19195 {
19196 switch (section->sh_type)
19197 {
19198 case SHT_GNU_LIBLIST:
19199 if (section->sh_link >= filedata->file_header.e_shnum)
19200 break;
19201
19202 elib = (Elf32_External_Lib *)
19203 get_data (NULL, filedata, section->sh_offset, 1, section->sh_size,
19204 _("liblist section data"));
19205
19206 if (elib == NULL)
19207 {
19208 res = false;
19209 break;
19210 }
19211
19212 string_sec = filedata->section_headers + section->sh_link;
19213 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
19214 string_sec->sh_size,
19215 _("liblist string table"));
19216 if (strtab == NULL
19217 || section->sh_entsize != sizeof (Elf32_External_Lib))
19218 {
19219 free (elib);
19220 free (strtab);
19221 res = false;
19222 break;
19223 }
19224 strtab_size = string_sec->sh_size;
19225
19226 num_liblist = section->sh_size / sizeof (Elf32_External_Lib);
19227 printf (ngettext ("\nLibrary list section '%s' contains %lu entries:\n",
19228 "\nLibrary list section '%s' contains %lu entries:\n",
19229 num_liblist),
19230 printable_section_name (filedata, section),
19231 num_liblist);
19232
19233 puts (_(" Library Time Stamp Checksum Version Flags"));
19234
19235 for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
19236 ++cnt)
19237 {
19238 Elf32_Lib liblist;
19239 time_t atime;
19240 char timebuf[128];
19241 struct tm * tmp;
19242
19243 liblist.l_name = BYTE_GET (elib[cnt].l_name);
19244 atime = BYTE_GET (elib[cnt].l_time_stamp);
19245 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
19246 liblist.l_version = BYTE_GET (elib[cnt].l_version);
19247 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
19248
19249 tmp = gmtime (&atime);
19250 snprintf (timebuf, sizeof (timebuf),
19251 "%04u-%02u-%02uT%02u:%02u:%02u",
19252 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
19253 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
19254
19255 printf ("%3lu: ", (unsigned long) cnt);
19256 if (do_wide)
19257 printf ("%-20s", liblist.l_name < strtab_size
19258 ? strtab + liblist.l_name : _("<corrupt>"));
19259 else
19260 printf ("%-20.20s", liblist.l_name < strtab_size
19261 ? strtab + liblist.l_name : _("<corrupt>"));
19262 printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
19263 liblist.l_version, liblist.l_flags);
19264 }
19265
19266 free (elib);
19267 free (strtab);
19268 }
19269 }
19270
19271 return res;
19272 }
19273
19274 static const char *
19275 get_note_type (Filedata * filedata, unsigned e_type)
19276 {
19277 static char buff[64];
19278
19279 if (filedata->file_header.e_type == ET_CORE)
19280 switch (e_type)
19281 {
19282 case NT_AUXV:
19283 return _("NT_AUXV (auxiliary vector)");
19284 case NT_PRSTATUS:
19285 return _("NT_PRSTATUS (prstatus structure)");
19286 case NT_FPREGSET:
19287 return _("NT_FPREGSET (floating point registers)");
19288 case NT_PRPSINFO:
19289 return _("NT_PRPSINFO (prpsinfo structure)");
19290 case NT_TASKSTRUCT:
19291 return _("NT_TASKSTRUCT (task structure)");
19292 case NT_GDB_TDESC:
19293 return _("NT_GDB_TDESC (GDB XML target description)");
19294 case NT_PRXFPREG:
19295 return _("NT_PRXFPREG (user_xfpregs structure)");
19296 case NT_PPC_VMX:
19297 return _("NT_PPC_VMX (ppc Altivec registers)");
19298 case NT_PPC_VSX:
19299 return _("NT_PPC_VSX (ppc VSX registers)");
19300 case NT_PPC_TAR:
19301 return _("NT_PPC_TAR (ppc TAR register)");
19302 case NT_PPC_PPR:
19303 return _("NT_PPC_PPR (ppc PPR register)");
19304 case NT_PPC_DSCR:
19305 return _("NT_PPC_DSCR (ppc DSCR register)");
19306 case NT_PPC_EBB:
19307 return _("NT_PPC_EBB (ppc EBB registers)");
19308 case NT_PPC_PMU:
19309 return _("NT_PPC_PMU (ppc PMU registers)");
19310 case NT_PPC_TM_CGPR:
19311 return _("NT_PPC_TM_CGPR (ppc checkpointed GPR registers)");
19312 case NT_PPC_TM_CFPR:
19313 return _("NT_PPC_TM_CFPR (ppc checkpointed floating point registers)");
19314 case NT_PPC_TM_CVMX:
19315 return _("NT_PPC_TM_CVMX (ppc checkpointed Altivec registers)");
19316 case NT_PPC_TM_CVSX:
19317 return _("NT_PPC_TM_CVSX (ppc checkpointed VSX registers)");
19318 case NT_PPC_TM_SPR:
19319 return _("NT_PPC_TM_SPR (ppc TM special purpose registers)");
19320 case NT_PPC_TM_CTAR:
19321 return _("NT_PPC_TM_CTAR (ppc checkpointed TAR register)");
19322 case NT_PPC_TM_CPPR:
19323 return _("NT_PPC_TM_CPPR (ppc checkpointed PPR register)");
19324 case NT_PPC_TM_CDSCR:
19325 return _("NT_PPC_TM_CDSCR (ppc checkpointed DSCR register)");
19326 case NT_386_TLS:
19327 return _("NT_386_TLS (x86 TLS information)");
19328 case NT_386_IOPERM:
19329 return _("NT_386_IOPERM (x86 I/O permissions)");
19330 case NT_X86_XSTATE:
19331 return _("NT_X86_XSTATE (x86 XSAVE extended state)");
19332 case NT_X86_CET:
19333 return _("NT_X86_CET (x86 CET state)");
19334 case NT_S390_HIGH_GPRS:
19335 return _("NT_S390_HIGH_GPRS (s390 upper register halves)");
19336 case NT_S390_TIMER:
19337 return _("NT_S390_TIMER (s390 timer register)");
19338 case NT_S390_TODCMP:
19339 return _("NT_S390_TODCMP (s390 TOD comparator register)");
19340 case NT_S390_TODPREG:
19341 return _("NT_S390_TODPREG (s390 TOD programmable register)");
19342 case NT_S390_CTRS:
19343 return _("NT_S390_CTRS (s390 control registers)");
19344 case NT_S390_PREFIX:
19345 return _("NT_S390_PREFIX (s390 prefix register)");
19346 case NT_S390_LAST_BREAK:
19347 return _("NT_S390_LAST_BREAK (s390 last breaking event address)");
19348 case NT_S390_SYSTEM_CALL:
19349 return _("NT_S390_SYSTEM_CALL (s390 system call restart data)");
19350 case NT_S390_TDB:
19351 return _("NT_S390_TDB (s390 transaction diagnostic block)");
19352 case NT_S390_VXRS_LOW:
19353 return _("NT_S390_VXRS_LOW (s390 vector registers 0-15 upper half)");
19354 case NT_S390_VXRS_HIGH:
19355 return _("NT_S390_VXRS_HIGH (s390 vector registers 16-31)");
19356 case NT_S390_GS_CB:
19357 return _("NT_S390_GS_CB (s390 guarded-storage registers)");
19358 case NT_S390_GS_BC:
19359 return _("NT_S390_GS_BC (s390 guarded-storage broadcast control)");
19360 case NT_ARM_VFP:
19361 return _("NT_ARM_VFP (arm VFP registers)");
19362 case NT_ARM_TLS:
19363 return _("NT_ARM_TLS (AArch TLS registers)");
19364 case NT_ARM_HW_BREAK:
19365 return _("NT_ARM_HW_BREAK (AArch hardware breakpoint registers)");
19366 case NT_ARM_HW_WATCH:
19367 return _("NT_ARM_HW_WATCH (AArch hardware watchpoint registers)");
19368 case NT_ARM_SYSTEM_CALL:
19369 return _("NT_ARM_SYSTEM_CALL (AArch system call number)");
19370 case NT_ARM_SVE:
19371 return _("NT_ARM_SVE (AArch SVE registers)");
19372 case NT_ARM_PAC_MASK:
19373 return _("NT_ARM_PAC_MASK (AArch pointer authentication code masks)");
19374 case NT_ARM_PACA_KEYS:
19375 return _("NT_ARM_PACA_KEYS (ARM pointer authentication address keys)");
19376 case NT_ARM_PACG_KEYS:
19377 return _("NT_ARM_PACG_KEYS (ARM pointer authentication generic keys)");
19378 case NT_ARM_TAGGED_ADDR_CTRL:
19379 return _("NT_ARM_TAGGED_ADDR_CTRL (AArch tagged address control)");
19380 case NT_ARM_PAC_ENABLED_KEYS:
19381 return _("NT_ARM_PAC_ENABLED_KEYS (AArch64 pointer authentication enabled keys)");
19382 case NT_ARC_V2:
19383 return _("NT_ARC_V2 (ARC HS accumulator/extra registers)");
19384 case NT_RISCV_CSR:
19385 return _("NT_RISCV_CSR (RISC-V control and status registers)");
19386 case NT_PSTATUS:
19387 return _("NT_PSTATUS (pstatus structure)");
19388 case NT_FPREGS:
19389 return _("NT_FPREGS (floating point registers)");
19390 case NT_PSINFO:
19391 return _("NT_PSINFO (psinfo structure)");
19392 case NT_LWPSTATUS:
19393 return _("NT_LWPSTATUS (lwpstatus_t structure)");
19394 case NT_LWPSINFO:
19395 return _("NT_LWPSINFO (lwpsinfo_t structure)");
19396 case NT_WIN32PSTATUS:
19397 return _("NT_WIN32PSTATUS (win32_pstatus structure)");
19398 case NT_SIGINFO:
19399 return _("NT_SIGINFO (siginfo_t data)");
19400 case NT_FILE:
19401 return _("NT_FILE (mapped files)");
19402 default:
19403 break;
19404 }
19405 else
19406 switch (e_type)
19407 {
19408 case NT_VERSION:
19409 return _("NT_VERSION (version)");
19410 case NT_ARCH:
19411 return _("NT_ARCH (architecture)");
19412 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
19413 return _("OPEN");
19414 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
19415 return _("func");
19416 case NT_GO_BUILDID:
19417 return _("GO BUILDID");
19418 case FDO_PACKAGING_METADATA:
19419 return _("FDO_PACKAGING_METADATA");
19420 default:
19421 break;
19422 }
19423
19424 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19425 return buff;
19426 }
19427
19428 static bool
19429 print_core_note (Elf_Internal_Note *pnote)
19430 {
19431 unsigned int addr_size = is_32bit_elf ? 4 : 8;
19432 bfd_vma count, page_size;
19433 unsigned char *descdata, *filenames, *descend;
19434
19435 if (pnote->type != NT_FILE)
19436 {
19437 if (do_wide)
19438 printf ("\n");
19439 return true;
19440 }
19441
19442 #ifndef BFD64
19443 if (!is_32bit_elf)
19444 {
19445 printf (_(" Cannot decode 64-bit note in 32-bit build\n"));
19446 /* Still "successful". */
19447 return true;
19448 }
19449 #endif
19450
19451 if (pnote->descsz < 2 * addr_size)
19452 {
19453 error (_(" Malformed note - too short for header\n"));
19454 return false;
19455 }
19456
19457 descdata = (unsigned char *) pnote->descdata;
19458 descend = descdata + pnote->descsz;
19459
19460 if (descdata[pnote->descsz - 1] != '\0')
19461 {
19462 error (_(" Malformed note - does not end with \\0\n"));
19463 return false;
19464 }
19465
19466 count = byte_get (descdata, addr_size);
19467 descdata += addr_size;
19468
19469 page_size = byte_get (descdata, addr_size);
19470 descdata += addr_size;
19471
19472 if (count > ((bfd_vma) -1 - 2 * addr_size) / (3 * addr_size)
19473 || pnote->descsz < 2 * addr_size + count * 3 * addr_size)
19474 {
19475 error (_(" Malformed note - too short for supplied file count\n"));
19476 return false;
19477 }
19478
19479 printf (_(" Page size: "));
19480 print_vma (page_size, DEC);
19481 printf ("\n");
19482
19483 printf (_(" %*s%*s%*s\n"),
19484 (int) (2 + 2 * addr_size), _("Start"),
19485 (int) (4 + 2 * addr_size), _("End"),
19486 (int) (4 + 2 * addr_size), _("Page Offset"));
19487 filenames = descdata + count * 3 * addr_size;
19488 while (count-- > 0)
19489 {
19490 bfd_vma start, end, file_ofs;
19491
19492 if (filenames == descend)
19493 {
19494 error (_(" Malformed note - filenames end too early\n"));
19495 return false;
19496 }
19497
19498 start = byte_get (descdata, addr_size);
19499 descdata += addr_size;
19500 end = byte_get (descdata, addr_size);
19501 descdata += addr_size;
19502 file_ofs = byte_get (descdata, addr_size);
19503 descdata += addr_size;
19504
19505 printf (" ");
19506 print_vma (start, FULL_HEX);
19507 printf (" ");
19508 print_vma (end, FULL_HEX);
19509 printf (" ");
19510 print_vma (file_ofs, FULL_HEX);
19511 printf ("\n %s\n", filenames);
19512
19513 filenames += 1 + strlen ((char *) filenames);
19514 }
19515
19516 return true;
19517 }
19518
19519 static const char *
19520 get_gnu_elf_note_type (unsigned e_type)
19521 {
19522 /* NB/ Keep this switch statement in sync with print_gnu_note (). */
19523 switch (e_type)
19524 {
19525 case NT_GNU_ABI_TAG:
19526 return _("NT_GNU_ABI_TAG (ABI version tag)");
19527 case NT_GNU_HWCAP:
19528 return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
19529 case NT_GNU_BUILD_ID:
19530 return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
19531 case NT_GNU_GOLD_VERSION:
19532 return _("NT_GNU_GOLD_VERSION (gold version)");
19533 case NT_GNU_PROPERTY_TYPE_0:
19534 return _("NT_GNU_PROPERTY_TYPE_0");
19535 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
19536 return _("NT_GNU_BUILD_ATTRIBUTE_OPEN");
19537 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
19538 return _("NT_GNU_BUILD_ATTRIBUTE_FUNC");
19539 default:
19540 {
19541 static char buff[64];
19542
19543 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19544 return buff;
19545 }
19546 }
19547 }
19548
19549 static void
19550 decode_x86_compat_isa (unsigned int bitmask)
19551 {
19552 while (bitmask)
19553 {
19554 unsigned int bit = bitmask & (- bitmask);
19555
19556 bitmask &= ~ bit;
19557 switch (bit)
19558 {
19559 case GNU_PROPERTY_X86_COMPAT_ISA_1_486:
19560 printf ("i486");
19561 break;
19562 case GNU_PROPERTY_X86_COMPAT_ISA_1_586:
19563 printf ("586");
19564 break;
19565 case GNU_PROPERTY_X86_COMPAT_ISA_1_686:
19566 printf ("686");
19567 break;
19568 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE:
19569 printf ("SSE");
19570 break;
19571 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE2:
19572 printf ("SSE2");
19573 break;
19574 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE3:
19575 printf ("SSE3");
19576 break;
19577 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSSE3:
19578 printf ("SSSE3");
19579 break;
19580 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_1:
19581 printf ("SSE4_1");
19582 break;
19583 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_2:
19584 printf ("SSE4_2");
19585 break;
19586 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX:
19587 printf ("AVX");
19588 break;
19589 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX2:
19590 printf ("AVX2");
19591 break;
19592 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512F:
19593 printf ("AVX512F");
19594 break;
19595 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512CD:
19596 printf ("AVX512CD");
19597 break;
19598 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512ER:
19599 printf ("AVX512ER");
19600 break;
19601 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512PF:
19602 printf ("AVX512PF");
19603 break;
19604 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512VL:
19605 printf ("AVX512VL");
19606 break;
19607 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512DQ:
19608 printf ("AVX512DQ");
19609 break;
19610 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512BW:
19611 printf ("AVX512BW");
19612 break;
19613 default:
19614 printf (_("<unknown: %x>"), bit);
19615 break;
19616 }
19617 if (bitmask)
19618 printf (", ");
19619 }
19620 }
19621
19622 static void
19623 decode_x86_compat_2_isa (unsigned int bitmask)
19624 {
19625 if (!bitmask)
19626 {
19627 printf (_("<None>"));
19628 return;
19629 }
19630
19631 while (bitmask)
19632 {
19633 unsigned int bit = bitmask & (- bitmask);
19634
19635 bitmask &= ~ bit;
19636 switch (bit)
19637 {
19638 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_CMOV:
19639 printf ("CMOV");
19640 break;
19641 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE:
19642 printf ("SSE");
19643 break;
19644 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE2:
19645 printf ("SSE2");
19646 break;
19647 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE3:
19648 printf ("SSE3");
19649 break;
19650 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSSE3:
19651 printf ("SSSE3");
19652 break;
19653 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_1:
19654 printf ("SSE4_1");
19655 break;
19656 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_2:
19657 printf ("SSE4_2");
19658 break;
19659 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX:
19660 printf ("AVX");
19661 break;
19662 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX2:
19663 printf ("AVX2");
19664 break;
19665 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_FMA:
19666 printf ("FMA");
19667 break;
19668 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512F:
19669 printf ("AVX512F");
19670 break;
19671 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512CD:
19672 printf ("AVX512CD");
19673 break;
19674 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512ER:
19675 printf ("AVX512ER");
19676 break;
19677 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512PF:
19678 printf ("AVX512PF");
19679 break;
19680 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512VL:
19681 printf ("AVX512VL");
19682 break;
19683 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512DQ:
19684 printf ("AVX512DQ");
19685 break;
19686 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512BW:
19687 printf ("AVX512BW");
19688 break;
19689 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4FMAPS:
19690 printf ("AVX512_4FMAPS");
19691 break;
19692 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4VNNIW:
19693 printf ("AVX512_4VNNIW");
19694 break;
19695 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BITALG:
19696 printf ("AVX512_BITALG");
19697 break;
19698 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_IFMA:
19699 printf ("AVX512_IFMA");
19700 break;
19701 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI:
19702 printf ("AVX512_VBMI");
19703 break;
19704 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI2:
19705 printf ("AVX512_VBMI2");
19706 break;
19707 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VNNI:
19708 printf ("AVX512_VNNI");
19709 break;
19710 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BF16:
19711 printf ("AVX512_BF16");
19712 break;
19713 default:
19714 printf (_("<unknown: %x>"), bit);
19715 break;
19716 }
19717 if (bitmask)
19718 printf (", ");
19719 }
19720 }
19721
19722 static const char *
19723 get_amdgpu_elf_note_type (unsigned int e_type)
19724 {
19725 switch (e_type)
19726 {
19727 case NT_AMDGPU_METADATA:
19728 return _("NT_AMDGPU_METADATA (code object metadata)");
19729 default:
19730 {
19731 static char buf[64];
19732 snprintf (buf, sizeof (buf), _("Unknown note type: (0x%08x)"), e_type);
19733 return buf;
19734 }
19735 }
19736 }
19737
19738 static void
19739 decode_x86_isa (unsigned int bitmask)
19740 {
19741 while (bitmask)
19742 {
19743 unsigned int bit = bitmask & (- bitmask);
19744
19745 bitmask &= ~ bit;
19746 switch (bit)
19747 {
19748 case GNU_PROPERTY_X86_ISA_1_BASELINE:
19749 printf ("x86-64-baseline");
19750 break;
19751 case GNU_PROPERTY_X86_ISA_1_V2:
19752 printf ("x86-64-v2");
19753 break;
19754 case GNU_PROPERTY_X86_ISA_1_V3:
19755 printf ("x86-64-v3");
19756 break;
19757 case GNU_PROPERTY_X86_ISA_1_V4:
19758 printf ("x86-64-v4");
19759 break;
19760 default:
19761 printf (_("<unknown: %x>"), bit);
19762 break;
19763 }
19764 if (bitmask)
19765 printf (", ");
19766 }
19767 }
19768
19769 static void
19770 decode_x86_feature_1 (unsigned int bitmask)
19771 {
19772 if (!bitmask)
19773 {
19774 printf (_("<None>"));
19775 return;
19776 }
19777
19778 while (bitmask)
19779 {
19780 unsigned int bit = bitmask & (- bitmask);
19781
19782 bitmask &= ~ bit;
19783 switch (bit)
19784 {
19785 case GNU_PROPERTY_X86_FEATURE_1_IBT:
19786 printf ("IBT");
19787 break;
19788 case GNU_PROPERTY_X86_FEATURE_1_SHSTK:
19789 printf ("SHSTK");
19790 break;
19791 case GNU_PROPERTY_X86_FEATURE_1_LAM_U48:
19792 printf ("LAM_U48");
19793 break;
19794 case GNU_PROPERTY_X86_FEATURE_1_LAM_U57:
19795 printf ("LAM_U57");
19796 break;
19797 default:
19798 printf (_("<unknown: %x>"), bit);
19799 break;
19800 }
19801 if (bitmask)
19802 printf (", ");
19803 }
19804 }
19805
19806 static void
19807 decode_x86_feature_2 (unsigned int bitmask)
19808 {
19809 if (!bitmask)
19810 {
19811 printf (_("<None>"));
19812 return;
19813 }
19814
19815 while (bitmask)
19816 {
19817 unsigned int bit = bitmask & (- bitmask);
19818
19819 bitmask &= ~ bit;
19820 switch (bit)
19821 {
19822 case GNU_PROPERTY_X86_FEATURE_2_X86:
19823 printf ("x86");
19824 break;
19825 case GNU_PROPERTY_X86_FEATURE_2_X87:
19826 printf ("x87");
19827 break;
19828 case GNU_PROPERTY_X86_FEATURE_2_MMX:
19829 printf ("MMX");
19830 break;
19831 case GNU_PROPERTY_X86_FEATURE_2_XMM:
19832 printf ("XMM");
19833 break;
19834 case GNU_PROPERTY_X86_FEATURE_2_YMM:
19835 printf ("YMM");
19836 break;
19837 case GNU_PROPERTY_X86_FEATURE_2_ZMM:
19838 printf ("ZMM");
19839 break;
19840 case GNU_PROPERTY_X86_FEATURE_2_TMM:
19841 printf ("TMM");
19842 break;
19843 case GNU_PROPERTY_X86_FEATURE_2_MASK:
19844 printf ("MASK");
19845 break;
19846 case GNU_PROPERTY_X86_FEATURE_2_FXSR:
19847 printf ("FXSR");
19848 break;
19849 case GNU_PROPERTY_X86_FEATURE_2_XSAVE:
19850 printf ("XSAVE");
19851 break;
19852 case GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT:
19853 printf ("XSAVEOPT");
19854 break;
19855 case GNU_PROPERTY_X86_FEATURE_2_XSAVEC:
19856 printf ("XSAVEC");
19857 break;
19858 default:
19859 printf (_("<unknown: %x>"), bit);
19860 break;
19861 }
19862 if (bitmask)
19863 printf (", ");
19864 }
19865 }
19866
19867 static void
19868 decode_aarch64_feature_1_and (unsigned int bitmask)
19869 {
19870 while (bitmask)
19871 {
19872 unsigned int bit = bitmask & (- bitmask);
19873
19874 bitmask &= ~ bit;
19875 switch (bit)
19876 {
19877 case GNU_PROPERTY_AARCH64_FEATURE_1_BTI:
19878 printf ("BTI");
19879 break;
19880
19881 case GNU_PROPERTY_AARCH64_FEATURE_1_PAC:
19882 printf ("PAC");
19883 break;
19884
19885 default:
19886 printf (_("<unknown: %x>"), bit);
19887 break;
19888 }
19889 if (bitmask)
19890 printf (", ");
19891 }
19892 }
19893
19894 static void
19895 decode_1_needed (unsigned int bitmask)
19896 {
19897 while (bitmask)
19898 {
19899 unsigned int bit = bitmask & (- bitmask);
19900
19901 bitmask &= ~ bit;
19902 switch (bit)
19903 {
19904 case GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS:
19905 printf ("indirect external access");
19906 break;
19907 default:
19908 printf (_("<unknown: %x>"), bit);
19909 break;
19910 }
19911 if (bitmask)
19912 printf (", ");
19913 }
19914 }
19915
19916 static void
19917 print_gnu_property_note (Filedata * filedata, Elf_Internal_Note * pnote)
19918 {
19919 unsigned char * ptr = (unsigned char *) pnote->descdata;
19920 unsigned char * ptr_end = ptr + pnote->descsz;
19921 unsigned int size = is_32bit_elf ? 4 : 8;
19922
19923 printf (_(" Properties: "));
19924
19925 if (pnote->descsz < 8 || (pnote->descsz % size) != 0)
19926 {
19927 printf (_("<corrupt GNU_PROPERTY_TYPE, size = %#lx>\n"), pnote->descsz);
19928 return;
19929 }
19930
19931 while (ptr < ptr_end)
19932 {
19933 unsigned int j;
19934 unsigned int type;
19935 unsigned int datasz;
19936
19937 if ((size_t) (ptr_end - ptr) < 8)
19938 {
19939 printf (_("<corrupt descsz: %#lx>\n"), pnote->descsz);
19940 break;
19941 }
19942
19943 type = byte_get (ptr, 4);
19944 datasz = byte_get (ptr + 4, 4);
19945
19946 ptr += 8;
19947
19948 if (datasz > (size_t) (ptr_end - ptr))
19949 {
19950 printf (_("<corrupt type (%#x) datasz: %#x>\n"),
19951 type, datasz);
19952 break;
19953 }
19954
19955 if (type >= GNU_PROPERTY_LOPROC && type <= GNU_PROPERTY_HIPROC)
19956 {
19957 if (filedata->file_header.e_machine == EM_X86_64
19958 || filedata->file_header.e_machine == EM_IAMCU
19959 || filedata->file_header.e_machine == EM_386)
19960 {
19961 unsigned int bitmask;
19962
19963 if (datasz == 4)
19964 bitmask = byte_get (ptr, 4);
19965 else
19966 bitmask = 0;
19967
19968 switch (type)
19969 {
19970 case GNU_PROPERTY_X86_ISA_1_USED:
19971 if (datasz != 4)
19972 printf (_("x86 ISA used: <corrupt length: %#x> "),
19973 datasz);
19974 else
19975 {
19976 printf ("x86 ISA used: ");
19977 decode_x86_isa (bitmask);
19978 }
19979 goto next;
19980
19981 case GNU_PROPERTY_X86_ISA_1_NEEDED:
19982 if (datasz != 4)
19983 printf (_("x86 ISA needed: <corrupt length: %#x> "),
19984 datasz);
19985 else
19986 {
19987 printf ("x86 ISA needed: ");
19988 decode_x86_isa (bitmask);
19989 }
19990 goto next;
19991
19992 case GNU_PROPERTY_X86_FEATURE_1_AND:
19993 if (datasz != 4)
19994 printf (_("x86 feature: <corrupt length: %#x> "),
19995 datasz);
19996 else
19997 {
19998 printf ("x86 feature: ");
19999 decode_x86_feature_1 (bitmask);
20000 }
20001 goto next;
20002
20003 case GNU_PROPERTY_X86_FEATURE_2_USED:
20004 if (datasz != 4)
20005 printf (_("x86 feature used: <corrupt length: %#x> "),
20006 datasz);
20007 else
20008 {
20009 printf ("x86 feature used: ");
20010 decode_x86_feature_2 (bitmask);
20011 }
20012 goto next;
20013
20014 case GNU_PROPERTY_X86_FEATURE_2_NEEDED:
20015 if (datasz != 4)
20016 printf (_("x86 feature needed: <corrupt length: %#x> "), datasz);
20017 else
20018 {
20019 printf ("x86 feature needed: ");
20020 decode_x86_feature_2 (bitmask);
20021 }
20022 goto next;
20023
20024 case GNU_PROPERTY_X86_COMPAT_ISA_1_USED:
20025 if (datasz != 4)
20026 printf (_("x86 ISA used: <corrupt length: %#x> "),
20027 datasz);
20028 else
20029 {
20030 printf ("x86 ISA used: ");
20031 decode_x86_compat_isa (bitmask);
20032 }
20033 goto next;
20034
20035 case GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED:
20036 if (datasz != 4)
20037 printf (_("x86 ISA needed: <corrupt length: %#x> "),
20038 datasz);
20039 else
20040 {
20041 printf ("x86 ISA needed: ");
20042 decode_x86_compat_isa (bitmask);
20043 }
20044 goto next;
20045
20046 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_USED:
20047 if (datasz != 4)
20048 printf (_("x86 ISA used: <corrupt length: %#x> "),
20049 datasz);
20050 else
20051 {
20052 printf ("x86 ISA used: ");
20053 decode_x86_compat_2_isa (bitmask);
20054 }
20055 goto next;
20056
20057 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_NEEDED:
20058 if (datasz != 4)
20059 printf (_("x86 ISA needed: <corrupt length: %#x> "),
20060 datasz);
20061 else
20062 {
20063 printf ("x86 ISA needed: ");
20064 decode_x86_compat_2_isa (bitmask);
20065 }
20066 goto next;
20067
20068 default:
20069 break;
20070 }
20071 }
20072 else if (filedata->file_header.e_machine == EM_AARCH64)
20073 {
20074 if (type == GNU_PROPERTY_AARCH64_FEATURE_1_AND)
20075 {
20076 printf ("AArch64 feature: ");
20077 if (datasz != 4)
20078 printf (_("<corrupt length: %#x> "), datasz);
20079 else
20080 decode_aarch64_feature_1_and (byte_get (ptr, 4));
20081 goto next;
20082 }
20083 }
20084 }
20085 else
20086 {
20087 switch (type)
20088 {
20089 case GNU_PROPERTY_STACK_SIZE:
20090 printf (_("stack size: "));
20091 if (datasz != size)
20092 printf (_("<corrupt length: %#x> "), datasz);
20093 else
20094 printf ("%#lx", (unsigned long) byte_get (ptr, size));
20095 goto next;
20096
20097 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
20098 printf ("no copy on protected ");
20099 if (datasz)
20100 printf (_("<corrupt length: %#x> "), datasz);
20101 goto next;
20102
20103 default:
20104 if ((type >= GNU_PROPERTY_UINT32_AND_LO
20105 && type <= GNU_PROPERTY_UINT32_AND_HI)
20106 || (type >= GNU_PROPERTY_UINT32_OR_LO
20107 && type <= GNU_PROPERTY_UINT32_OR_HI))
20108 {
20109 switch (type)
20110 {
20111 case GNU_PROPERTY_1_NEEDED:
20112 if (datasz != 4)
20113 printf (_("1_needed: <corrupt length: %#x> "),
20114 datasz);
20115 else
20116 {
20117 unsigned int bitmask = byte_get (ptr, 4);
20118 printf ("1_needed: ");
20119 decode_1_needed (bitmask);
20120 }
20121 goto next;
20122
20123 default:
20124 break;
20125 }
20126 if (type <= GNU_PROPERTY_UINT32_AND_HI)
20127 printf (_("UINT32_AND (%#x): "), type);
20128 else
20129 printf (_("UINT32_OR (%#x): "), type);
20130 if (datasz != 4)
20131 printf (_("<corrupt length: %#x> "), datasz);
20132 else
20133 printf ("%#x", (unsigned int) byte_get (ptr, 4));
20134 goto next;
20135 }
20136 break;
20137 }
20138 }
20139
20140 if (type < GNU_PROPERTY_LOPROC)
20141 printf (_("<unknown type %#x data: "), type);
20142 else if (type < GNU_PROPERTY_LOUSER)
20143 printf (_("<processor-specific type %#x data: "), type);
20144 else
20145 printf (_("<application-specific type %#x data: "), type);
20146 for (j = 0; j < datasz; ++j)
20147 printf ("%02x ", ptr[j] & 0xff);
20148 printf (">");
20149
20150 next:
20151 ptr += ((datasz + (size - 1)) & ~ (size - 1));
20152 if (ptr == ptr_end)
20153 break;
20154
20155 if (do_wide)
20156 printf (", ");
20157 else
20158 printf ("\n\t");
20159 }
20160
20161 printf ("\n");
20162 }
20163
20164 static bool
20165 print_gnu_note (Filedata * filedata, Elf_Internal_Note *pnote)
20166 {
20167 /* NB/ Keep this switch statement in sync with get_gnu_elf_note_type (). */
20168 switch (pnote->type)
20169 {
20170 case NT_GNU_BUILD_ID:
20171 {
20172 unsigned long i;
20173
20174 printf (_(" Build ID: "));
20175 for (i = 0; i < pnote->descsz; ++i)
20176 printf ("%02x", pnote->descdata[i] & 0xff);
20177 printf ("\n");
20178 }
20179 break;
20180
20181 case NT_GNU_ABI_TAG:
20182 {
20183 unsigned long os, major, minor, subminor;
20184 const char *osname;
20185
20186 /* PR 17531: file: 030-599401-0.004. */
20187 if (pnote->descsz < 16)
20188 {
20189 printf (_(" <corrupt GNU_ABI_TAG>\n"));
20190 break;
20191 }
20192
20193 os = byte_get ((unsigned char *) pnote->descdata, 4);
20194 major = byte_get ((unsigned char *) pnote->descdata + 4, 4);
20195 minor = byte_get ((unsigned char *) pnote->descdata + 8, 4);
20196 subminor = byte_get ((unsigned char *) pnote->descdata + 12, 4);
20197
20198 switch (os)
20199 {
20200 case GNU_ABI_TAG_LINUX:
20201 osname = "Linux";
20202 break;
20203 case GNU_ABI_TAG_HURD:
20204 osname = "Hurd";
20205 break;
20206 case GNU_ABI_TAG_SOLARIS:
20207 osname = "Solaris";
20208 break;
20209 case GNU_ABI_TAG_FREEBSD:
20210 osname = "FreeBSD";
20211 break;
20212 case GNU_ABI_TAG_NETBSD:
20213 osname = "NetBSD";
20214 break;
20215 case GNU_ABI_TAG_SYLLABLE:
20216 osname = "Syllable";
20217 break;
20218 case GNU_ABI_TAG_NACL:
20219 osname = "NaCl";
20220 break;
20221 default:
20222 osname = "Unknown";
20223 break;
20224 }
20225
20226 printf (_(" OS: %s, ABI: %ld.%ld.%ld\n"), osname,
20227 major, minor, subminor);
20228 }
20229 break;
20230
20231 case NT_GNU_GOLD_VERSION:
20232 {
20233 unsigned long i;
20234
20235 printf (_(" Version: "));
20236 for (i = 0; i < pnote->descsz && pnote->descdata[i] != '\0'; ++i)
20237 printf ("%c", pnote->descdata[i]);
20238 printf ("\n");
20239 }
20240 break;
20241
20242 case NT_GNU_HWCAP:
20243 {
20244 unsigned long num_entries, mask;
20245
20246 /* Hardware capabilities information. Word 0 is the number of entries.
20247 Word 1 is a bitmask of enabled entries. The rest of the descriptor
20248 is a series of entries, where each entry is a single byte followed
20249 by a nul terminated string. The byte gives the bit number to test
20250 if enabled in the bitmask. */
20251 printf (_(" Hardware Capabilities: "));
20252 if (pnote->descsz < 8)
20253 {
20254 error (_("<corrupt GNU_HWCAP>\n"));
20255 return false;
20256 }
20257 num_entries = byte_get ((unsigned char *) pnote->descdata, 4);
20258 mask = byte_get ((unsigned char *) pnote->descdata + 4, 4);
20259 printf (_("num entries: %ld, enabled mask: %lx\n"), num_entries, mask);
20260 /* FIXME: Add code to display the entries... */
20261 }
20262 break;
20263
20264 case NT_GNU_PROPERTY_TYPE_0:
20265 print_gnu_property_note (filedata, pnote);
20266 break;
20267
20268 default:
20269 /* Handle unrecognised types. An error message should have already been
20270 created by get_gnu_elf_note_type(), so all that we need to do is to
20271 display the data. */
20272 {
20273 unsigned long i;
20274
20275 printf (_(" Description data: "));
20276 for (i = 0; i < pnote->descsz; ++i)
20277 printf ("%02x ", pnote->descdata[i] & 0xff);
20278 printf ("\n");
20279 }
20280 break;
20281 }
20282
20283 return true;
20284 }
20285
20286 static const char *
20287 get_v850_elf_note_type (enum v850_notes n_type)
20288 {
20289 static char buff[64];
20290
20291 switch (n_type)
20292 {
20293 case V850_NOTE_ALIGNMENT: return _("Alignment of 8-byte objects");
20294 case V850_NOTE_DATA_SIZE: return _("Sizeof double and long double");
20295 case V850_NOTE_FPU_INFO: return _("Type of FPU support needed");
20296 case V850_NOTE_SIMD_INFO: return _("Use of SIMD instructions");
20297 case V850_NOTE_CACHE_INFO: return _("Use of cache");
20298 case V850_NOTE_MMU_INFO: return _("Use of MMU");
20299 default:
20300 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), n_type);
20301 return buff;
20302 }
20303 }
20304
20305 static bool
20306 print_v850_note (Elf_Internal_Note * pnote)
20307 {
20308 unsigned int val;
20309
20310 if (pnote->descsz != 4)
20311 return false;
20312
20313 val = byte_get ((unsigned char *) pnote->descdata, pnote->descsz);
20314
20315 if (val == 0)
20316 {
20317 printf (_("not set\n"));
20318 return true;
20319 }
20320
20321 switch (pnote->type)
20322 {
20323 case V850_NOTE_ALIGNMENT:
20324 switch (val)
20325 {
20326 case EF_RH850_DATA_ALIGN4: printf (_("4-byte\n")); return true;
20327 case EF_RH850_DATA_ALIGN8: printf (_("8-byte\n")); return true;
20328 }
20329 break;
20330
20331 case V850_NOTE_DATA_SIZE:
20332 switch (val)
20333 {
20334 case EF_RH850_DOUBLE32: printf (_("4-bytes\n")); return true;
20335 case EF_RH850_DOUBLE64: printf (_("8-bytes\n")); return true;
20336 }
20337 break;
20338
20339 case V850_NOTE_FPU_INFO:
20340 switch (val)
20341 {
20342 case EF_RH850_FPU20: printf (_("FPU-2.0\n")); return true;
20343 case EF_RH850_FPU30: printf (_("FPU-3.0\n")); return true;
20344 }
20345 break;
20346
20347 case V850_NOTE_MMU_INFO:
20348 case V850_NOTE_CACHE_INFO:
20349 case V850_NOTE_SIMD_INFO:
20350 if (val == EF_RH850_SIMD)
20351 {
20352 printf (_("yes\n"));
20353 return true;
20354 }
20355 break;
20356
20357 default:
20358 /* An 'unknown note type' message will already have been displayed. */
20359 break;
20360 }
20361
20362 printf (_("unknown value: %x\n"), val);
20363 return false;
20364 }
20365
20366 static bool
20367 process_netbsd_elf_note (Elf_Internal_Note * pnote)
20368 {
20369 unsigned int version;
20370
20371 switch (pnote->type)
20372 {
20373 case NT_NETBSD_IDENT:
20374 if (pnote->descsz < 1)
20375 break;
20376 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
20377 if ((version / 10000) % 100)
20378 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u%s%c)\n", pnote->descsz,
20379 version, version / 100000000, (version / 1000000) % 100,
20380 (version / 10000) % 100 > 26 ? "Z" : "",
20381 'A' + (version / 10000) % 26);
20382 else
20383 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u.%u)\n", pnote->descsz,
20384 version, version / 100000000, (version / 1000000) % 100,
20385 (version / 100) % 100);
20386 return true;
20387
20388 case NT_NETBSD_MARCH:
20389 printf (" NetBSD\t\t0x%08lx\tMARCH <%s>\n", pnote->descsz,
20390 pnote->descdata);
20391 return true;
20392
20393 case NT_NETBSD_PAX:
20394 if (pnote->descsz < 1)
20395 break;
20396 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
20397 printf (" NetBSD\t\t0x%08lx\tPaX <%s%s%s%s%s%s>\n", pnote->descsz,
20398 ((version & NT_NETBSD_PAX_MPROTECT) ? "+mprotect" : ""),
20399 ((version & NT_NETBSD_PAX_NOMPROTECT) ? "-mprotect" : ""),
20400 ((version & NT_NETBSD_PAX_GUARD) ? "+guard" : ""),
20401 ((version & NT_NETBSD_PAX_NOGUARD) ? "-guard" : ""),
20402 ((version & NT_NETBSD_PAX_ASLR) ? "+ASLR" : ""),
20403 ((version & NT_NETBSD_PAX_NOASLR) ? "-ASLR" : ""));
20404 return true;
20405 }
20406
20407 printf (" NetBSD\t0x%08lx\tUnknown note type: (0x%08lx)\n",
20408 pnote->descsz, pnote->type);
20409 return false;
20410 }
20411
20412 static const char *
20413 get_freebsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
20414 {
20415 switch (e_type)
20416 {
20417 case NT_FREEBSD_THRMISC:
20418 return _("NT_THRMISC (thrmisc structure)");
20419 case NT_FREEBSD_PROCSTAT_PROC:
20420 return _("NT_PROCSTAT_PROC (proc data)");
20421 case NT_FREEBSD_PROCSTAT_FILES:
20422 return _("NT_PROCSTAT_FILES (files data)");
20423 case NT_FREEBSD_PROCSTAT_VMMAP:
20424 return _("NT_PROCSTAT_VMMAP (vmmap data)");
20425 case NT_FREEBSD_PROCSTAT_GROUPS:
20426 return _("NT_PROCSTAT_GROUPS (groups data)");
20427 case NT_FREEBSD_PROCSTAT_UMASK:
20428 return _("NT_PROCSTAT_UMASK (umask data)");
20429 case NT_FREEBSD_PROCSTAT_RLIMIT:
20430 return _("NT_PROCSTAT_RLIMIT (rlimit data)");
20431 case NT_FREEBSD_PROCSTAT_OSREL:
20432 return _("NT_PROCSTAT_OSREL (osreldate data)");
20433 case NT_FREEBSD_PROCSTAT_PSSTRINGS:
20434 return _("NT_PROCSTAT_PSSTRINGS (ps_strings data)");
20435 case NT_FREEBSD_PROCSTAT_AUXV:
20436 return _("NT_PROCSTAT_AUXV (auxv data)");
20437 case NT_FREEBSD_PTLWPINFO:
20438 return _("NT_PTLWPINFO (ptrace_lwpinfo structure)");
20439 case NT_FREEBSD_X86_SEGBASES:
20440 return _("NT_X86_SEGBASES (x86 segment base registers)");
20441 }
20442 return get_note_type (filedata, e_type);
20443 }
20444
20445 static const char *
20446 get_netbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
20447 {
20448 static char buff[64];
20449
20450 switch (e_type)
20451 {
20452 case NT_NETBSDCORE_PROCINFO:
20453 /* NetBSD core "procinfo" structure. */
20454 return _("NetBSD procinfo structure");
20455
20456 case NT_NETBSDCORE_AUXV:
20457 return _("NetBSD ELF auxiliary vector data");
20458
20459 case NT_NETBSDCORE_LWPSTATUS:
20460 return _("PT_LWPSTATUS (ptrace_lwpstatus structure)");
20461
20462 default:
20463 /* As of Jan 2020 there are no other machine-independent notes
20464 defined for NetBSD core files. If the note type is less
20465 than the start of the machine-dependent note types, we don't
20466 understand it. */
20467
20468 if (e_type < NT_NETBSDCORE_FIRSTMACH)
20469 {
20470 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20471 return buff;
20472 }
20473 break;
20474 }
20475
20476 switch (filedata->file_header.e_machine)
20477 {
20478 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
20479 and PT_GETFPREGS == mach+2. */
20480
20481 case EM_OLD_ALPHA:
20482 case EM_ALPHA:
20483 case EM_SPARC:
20484 case EM_SPARC32PLUS:
20485 case EM_SPARCV9:
20486 switch (e_type)
20487 {
20488 case NT_NETBSDCORE_FIRSTMACH + 0:
20489 return _("PT_GETREGS (reg structure)");
20490 case NT_NETBSDCORE_FIRSTMACH + 2:
20491 return _("PT_GETFPREGS (fpreg structure)");
20492 default:
20493 break;
20494 }
20495 break;
20496
20497 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
20498 There's also old PT___GETREGS40 == mach + 1 for old reg
20499 structure which lacks GBR. */
20500 case EM_SH:
20501 switch (e_type)
20502 {
20503 case NT_NETBSDCORE_FIRSTMACH + 1:
20504 return _("PT___GETREGS40 (old reg structure)");
20505 case NT_NETBSDCORE_FIRSTMACH + 3:
20506 return _("PT_GETREGS (reg structure)");
20507 case NT_NETBSDCORE_FIRSTMACH + 5:
20508 return _("PT_GETFPREGS (fpreg structure)");
20509 default:
20510 break;
20511 }
20512 break;
20513
20514 /* On all other arch's, PT_GETREGS == mach+1 and
20515 PT_GETFPREGS == mach+3. */
20516 default:
20517 switch (e_type)
20518 {
20519 case NT_NETBSDCORE_FIRSTMACH + 1:
20520 return _("PT_GETREGS (reg structure)");
20521 case NT_NETBSDCORE_FIRSTMACH + 3:
20522 return _("PT_GETFPREGS (fpreg structure)");
20523 default:
20524 break;
20525 }
20526 }
20527
20528 snprintf (buff, sizeof (buff), "PT_FIRSTMACH+%d",
20529 e_type - NT_NETBSDCORE_FIRSTMACH);
20530 return buff;
20531 }
20532
20533 static const char *
20534 get_openbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
20535 {
20536 switch (e_type)
20537 {
20538 case NT_OPENBSD_PROCINFO:
20539 return _("OpenBSD procinfo structure");
20540 case NT_OPENBSD_AUXV:
20541 return _("OpenBSD ELF auxiliary vector data");
20542 case NT_OPENBSD_REGS:
20543 return _("OpenBSD regular registers");
20544 case NT_OPENBSD_FPREGS:
20545 return _("OpenBSD floating point registers");
20546 case NT_OPENBSD_WCOOKIE:
20547 return _("OpenBSD window cookie");
20548 }
20549
20550 return get_note_type (filedata, e_type);
20551 }
20552
20553 static const char *
20554 get_stapsdt_note_type (unsigned e_type)
20555 {
20556 static char buff[64];
20557
20558 switch (e_type)
20559 {
20560 case NT_STAPSDT:
20561 return _("NT_STAPSDT (SystemTap probe descriptors)");
20562
20563 default:
20564 break;
20565 }
20566
20567 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20568 return buff;
20569 }
20570
20571 static bool
20572 print_stapsdt_note (Elf_Internal_Note *pnote)
20573 {
20574 size_t len, maxlen;
20575 unsigned long addr_size = is_32bit_elf ? 4 : 8;
20576 char *data = pnote->descdata;
20577 char *data_end = pnote->descdata + pnote->descsz;
20578 bfd_vma pc, base_addr, semaphore;
20579 char *provider, *probe, *arg_fmt;
20580
20581 if (pnote->descsz < (addr_size * 3))
20582 goto stapdt_note_too_small;
20583
20584 pc = byte_get ((unsigned char *) data, addr_size);
20585 data += addr_size;
20586
20587 base_addr = byte_get ((unsigned char *) data, addr_size);
20588 data += addr_size;
20589
20590 semaphore = byte_get ((unsigned char *) data, addr_size);
20591 data += addr_size;
20592
20593 if (data >= data_end)
20594 goto stapdt_note_too_small;
20595 maxlen = data_end - data;
20596 len = strnlen (data, maxlen);
20597 if (len < maxlen)
20598 {
20599 provider = data;
20600 data += len + 1;
20601 }
20602 else
20603 goto stapdt_note_too_small;
20604
20605 if (data >= data_end)
20606 goto stapdt_note_too_small;
20607 maxlen = data_end - data;
20608 len = strnlen (data, maxlen);
20609 if (len < maxlen)
20610 {
20611 probe = data;
20612 data += len + 1;
20613 }
20614 else
20615 goto stapdt_note_too_small;
20616
20617 if (data >= data_end)
20618 goto stapdt_note_too_small;
20619 maxlen = data_end - data;
20620 len = strnlen (data, maxlen);
20621 if (len < maxlen)
20622 {
20623 arg_fmt = data;
20624 data += len + 1;
20625 }
20626 else
20627 goto stapdt_note_too_small;
20628
20629 printf (_(" Provider: %s\n"), provider);
20630 printf (_(" Name: %s\n"), probe);
20631 printf (_(" Location: "));
20632 print_vma (pc, FULL_HEX);
20633 printf (_(", Base: "));
20634 print_vma (base_addr, FULL_HEX);
20635 printf (_(", Semaphore: "));
20636 print_vma (semaphore, FULL_HEX);
20637 printf ("\n");
20638 printf (_(" Arguments: %s\n"), arg_fmt);
20639
20640 return data == data_end;
20641
20642 stapdt_note_too_small:
20643 printf (_(" <corrupt - note is too small>\n"));
20644 error (_("corrupt stapdt note - the data size is too small\n"));
20645 return false;
20646 }
20647
20648 static bool
20649 print_fdo_note (Elf_Internal_Note * pnote)
20650 {
20651 if (pnote->descsz > 0 && pnote->type == FDO_PACKAGING_METADATA)
20652 {
20653 printf (_(" Packaging Metadata: %.*s\n"), (int) pnote->descsz, pnote->descdata);
20654 return true;
20655 }
20656 return false;
20657 }
20658
20659 static const char *
20660 get_ia64_vms_note_type (unsigned e_type)
20661 {
20662 static char buff[64];
20663
20664 switch (e_type)
20665 {
20666 case NT_VMS_MHD:
20667 return _("NT_VMS_MHD (module header)");
20668 case NT_VMS_LNM:
20669 return _("NT_VMS_LNM (language name)");
20670 case NT_VMS_SRC:
20671 return _("NT_VMS_SRC (source files)");
20672 case NT_VMS_TITLE:
20673 return "NT_VMS_TITLE";
20674 case NT_VMS_EIDC:
20675 return _("NT_VMS_EIDC (consistency check)");
20676 case NT_VMS_FPMODE:
20677 return _("NT_VMS_FPMODE (FP mode)");
20678 case NT_VMS_LINKTIME:
20679 return "NT_VMS_LINKTIME";
20680 case NT_VMS_IMGNAM:
20681 return _("NT_VMS_IMGNAM (image name)");
20682 case NT_VMS_IMGID:
20683 return _("NT_VMS_IMGID (image id)");
20684 case NT_VMS_LINKID:
20685 return _("NT_VMS_LINKID (link id)");
20686 case NT_VMS_IMGBID:
20687 return _("NT_VMS_IMGBID (build id)");
20688 case NT_VMS_GSTNAM:
20689 return _("NT_VMS_GSTNAM (sym table name)");
20690 case NT_VMS_ORIG_DYN:
20691 return "NT_VMS_ORIG_DYN";
20692 case NT_VMS_PATCHTIME:
20693 return "NT_VMS_PATCHTIME";
20694 default:
20695 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20696 return buff;
20697 }
20698 }
20699
20700 static bool
20701 print_ia64_vms_note (Elf_Internal_Note * pnote)
20702 {
20703 int maxlen = pnote->descsz;
20704
20705 if (maxlen < 2 || (unsigned long) maxlen != pnote->descsz)
20706 goto desc_size_fail;
20707
20708 switch (pnote->type)
20709 {
20710 case NT_VMS_MHD:
20711 if (maxlen <= 36)
20712 goto desc_size_fail;
20713
20714 int l = (int) strnlen (pnote->descdata + 34, maxlen - 34);
20715
20716 printf (_(" Creation date : %.17s\n"), pnote->descdata);
20717 printf (_(" Last patch date: %.17s\n"), pnote->descdata + 17);
20718 if (l + 34 < maxlen)
20719 {
20720 printf (_(" Module name : %s\n"), pnote->descdata + 34);
20721 if (l + 35 < maxlen)
20722 printf (_(" Module version : %s\n"), pnote->descdata + 34 + l + 1);
20723 else
20724 printf (_(" Module version : <missing>\n"));
20725 }
20726 else
20727 {
20728 printf (_(" Module name : <missing>\n"));
20729 printf (_(" Module version : <missing>\n"));
20730 }
20731 break;
20732
20733 case NT_VMS_LNM:
20734 printf (_(" Language: %.*s\n"), maxlen, pnote->descdata);
20735 break;
20736
20737 #ifdef BFD64
20738 case NT_VMS_FPMODE:
20739 printf (_(" Floating Point mode: "));
20740 if (maxlen < 8)
20741 goto desc_size_fail;
20742 /* FIXME: Generate an error if descsz > 8 ? */
20743
20744 printf ("0x%016" BFD_VMA_FMT "x\n",
20745 (bfd_vma) byte_get ((unsigned char *) pnote->descdata, 8));
20746 break;
20747
20748 case NT_VMS_LINKTIME:
20749 printf (_(" Link time: "));
20750 if (maxlen < 8)
20751 goto desc_size_fail;
20752 /* FIXME: Generate an error if descsz > 8 ? */
20753
20754 print_vms_time (byte_get ((unsigned char *) pnote->descdata, 8));
20755 printf ("\n");
20756 break;
20757
20758 case NT_VMS_PATCHTIME:
20759 printf (_(" Patch time: "));
20760 if (maxlen < 8)
20761 goto desc_size_fail;
20762 /* FIXME: Generate an error if descsz > 8 ? */
20763
20764 print_vms_time (byte_get ((unsigned char *) pnote->descdata, 8));
20765 printf ("\n");
20766 break;
20767
20768 case NT_VMS_ORIG_DYN:
20769 if (maxlen < 34)
20770 goto desc_size_fail;
20771
20772 printf (_(" Major id: %u, minor id: %u\n"),
20773 (unsigned) byte_get ((unsigned char *) pnote->descdata, 4),
20774 (unsigned) byte_get ((unsigned char *) pnote->descdata + 4, 4));
20775 printf (_(" Last modified : "));
20776 print_vms_time (byte_get ((unsigned char *) pnote->descdata + 8, 8));
20777 printf (_("\n Link flags : "));
20778 printf ("0x%016" BFD_VMA_FMT "x\n",
20779 (bfd_vma) byte_get ((unsigned char *) pnote->descdata + 16, 8));
20780 printf (_(" Header flags: 0x%08x\n"),
20781 (unsigned) byte_get ((unsigned char *) pnote->descdata + 24, 4));
20782 printf (_(" Image id : %.*s\n"), maxlen - 32, pnote->descdata + 32);
20783 break;
20784 #endif
20785
20786 case NT_VMS_IMGNAM:
20787 printf (_(" Image name: %.*s\n"), maxlen, pnote->descdata);
20788 break;
20789
20790 case NT_VMS_GSTNAM:
20791 printf (_(" Global symbol table name: %.*s\n"), maxlen, pnote->descdata);
20792 break;
20793
20794 case NT_VMS_IMGID:
20795 printf (_(" Image id: %.*s\n"), maxlen, pnote->descdata);
20796 break;
20797
20798 case NT_VMS_LINKID:
20799 printf (_(" Linker id: %.*s\n"), maxlen, pnote->descdata);
20800 break;
20801
20802 default:
20803 return false;
20804 }
20805
20806 return true;
20807
20808 desc_size_fail:
20809 printf (_(" <corrupt - data size is too small>\n"));
20810 error (_("corrupt IA64 note: data size is too small\n"));
20811 return false;
20812 }
20813
20814 struct build_attr_cache {
20815 Filedata *filedata;
20816 char *strtab;
20817 unsigned long strtablen;
20818 Elf_Internal_Sym *symtab;
20819 unsigned long nsyms;
20820 } ba_cache;
20821
20822 /* Find the symbol associated with a build attribute that is attached
20823 to address OFFSET. If PNAME is non-NULL then store the name of
20824 the symbol (if found) in the provided pointer, Returns NULL if a
20825 symbol could not be found. */
20826
20827 static Elf_Internal_Sym *
20828 get_symbol_for_build_attribute (Filedata *filedata,
20829 unsigned long offset,
20830 bool is_open_attr,
20831 const char **pname)
20832 {
20833 Elf_Internal_Sym *saved_sym = NULL;
20834 Elf_Internal_Sym *sym;
20835
20836 if (filedata->section_headers != NULL
20837 && (ba_cache.filedata == NULL || filedata != ba_cache.filedata))
20838 {
20839 Elf_Internal_Shdr * symsec;
20840
20841 free (ba_cache.strtab);
20842 ba_cache.strtab = NULL;
20843 free (ba_cache.symtab);
20844 ba_cache.symtab = NULL;
20845
20846 /* Load the symbol and string sections. */
20847 for (symsec = filedata->section_headers;
20848 symsec < filedata->section_headers + filedata->file_header.e_shnum;
20849 symsec ++)
20850 {
20851 if (symsec->sh_type == SHT_SYMTAB
20852 && get_symtab (filedata, symsec,
20853 &ba_cache.symtab, &ba_cache.nsyms,
20854 &ba_cache.strtab, &ba_cache.strtablen))
20855 break;
20856 }
20857 ba_cache.filedata = filedata;
20858 }
20859
20860 if (ba_cache.symtab == NULL)
20861 return NULL;
20862
20863 /* Find a symbol whose value matches offset. */
20864 for (sym = ba_cache.symtab; sym < ba_cache.symtab + ba_cache.nsyms; sym ++)
20865 if (sym->st_value == offset)
20866 {
20867 if (sym->st_name >= ba_cache.strtablen)
20868 /* Huh ? This should not happen. */
20869 continue;
20870
20871 if (ba_cache.strtab[sym->st_name] == 0)
20872 continue;
20873
20874 /* The AArch64, ARM and RISC-V architectures define mapping symbols
20875 (eg $d, $x, $t) which we want to ignore. */
20876 if (ba_cache.strtab[sym->st_name] == '$'
20877 && ba_cache.strtab[sym->st_name + 1] != 0
20878 && ba_cache.strtab[sym->st_name + 2] == 0)
20879 continue;
20880
20881 if (is_open_attr)
20882 {
20883 /* For OPEN attributes we prefer GLOBAL over LOCAL symbols
20884 and FILE or OBJECT symbols over NOTYPE symbols. We skip
20885 FUNC symbols entirely. */
20886 switch (ELF_ST_TYPE (sym->st_info))
20887 {
20888 case STT_OBJECT:
20889 case STT_FILE:
20890 saved_sym = sym;
20891 if (sym->st_size)
20892 {
20893 /* If the symbol has a size associated
20894 with it then we can stop searching. */
20895 sym = ba_cache.symtab + ba_cache.nsyms;
20896 }
20897 continue;
20898
20899 case STT_FUNC:
20900 /* Ignore function symbols. */
20901 continue;
20902
20903 default:
20904 break;
20905 }
20906
20907 switch (ELF_ST_BIND (sym->st_info))
20908 {
20909 case STB_GLOBAL:
20910 if (saved_sym == NULL
20911 || ELF_ST_TYPE (saved_sym->st_info) != STT_OBJECT)
20912 saved_sym = sym;
20913 break;
20914
20915 case STB_LOCAL:
20916 if (saved_sym == NULL)
20917 saved_sym = sym;
20918 break;
20919
20920 default:
20921 break;
20922 }
20923 }
20924 else
20925 {
20926 if (ELF_ST_TYPE (sym->st_info) != STT_FUNC)
20927 continue;
20928
20929 saved_sym = sym;
20930 break;
20931 }
20932 }
20933
20934 if (saved_sym && pname)
20935 * pname = ba_cache.strtab + saved_sym->st_name;
20936
20937 return saved_sym;
20938 }
20939
20940 /* Returns true iff addr1 and addr2 are in the same section. */
20941
20942 static bool
20943 same_section (Filedata * filedata, unsigned long addr1, unsigned long addr2)
20944 {
20945 Elf_Internal_Shdr * a1;
20946 Elf_Internal_Shdr * a2;
20947
20948 a1 = find_section_by_address (filedata, addr1);
20949 a2 = find_section_by_address (filedata, addr2);
20950
20951 return a1 == a2 && a1 != NULL;
20952 }
20953
20954 static bool
20955 print_gnu_build_attribute_description (Elf_Internal_Note * pnote,
20956 Filedata * filedata)
20957 {
20958 static unsigned long global_offset = 0;
20959 static unsigned long global_end = 0;
20960 static unsigned long func_offset = 0;
20961 static unsigned long func_end = 0;
20962
20963 Elf_Internal_Sym *sym;
20964 const char *name;
20965 unsigned long start;
20966 unsigned long end;
20967 bool is_open_attr = pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN;
20968
20969 switch (pnote->descsz)
20970 {
20971 case 0:
20972 /* A zero-length description means that the range of
20973 the previous note of the same type should be used. */
20974 if (is_open_attr)
20975 {
20976 if (global_end > global_offset)
20977 printf (_(" Applies to region from %#lx to %#lx\n"),
20978 global_offset, global_end);
20979 else
20980 printf (_(" Applies to region from %#lx\n"), global_offset);
20981 }
20982 else
20983 {
20984 if (func_end > func_offset)
20985 printf (_(" Applies to region from %#lx to %#lx\n"), func_offset, func_end);
20986 else
20987 printf (_(" Applies to region from %#lx\n"), func_offset);
20988 }
20989 return true;
20990
20991 case 4:
20992 start = byte_get ((unsigned char *) pnote->descdata, 4);
20993 end = 0;
20994 break;
20995
20996 case 8:
20997 start = byte_get ((unsigned char *) pnote->descdata, 4);
20998 end = byte_get ((unsigned char *) pnote->descdata + 4, 4);
20999 break;
21000
21001 case 16:
21002 start = byte_get ((unsigned char *) pnote->descdata, 8);
21003 end = byte_get ((unsigned char *) pnote->descdata + 8, 8);
21004 break;
21005
21006 default:
21007 error (_(" <invalid description size: %lx>\n"), pnote->descsz);
21008 printf (_(" <invalid descsz>"));
21009 return false;
21010 }
21011
21012 name = NULL;
21013 sym = get_symbol_for_build_attribute (filedata, start, is_open_attr, & name);
21014 /* As of version 5 of the annobin plugin, filename symbols are biased by 2
21015 in order to avoid them being confused with the start address of the
21016 first function in the file... */
21017 if (sym == NULL && is_open_attr)
21018 sym = get_symbol_for_build_attribute (filedata, start + 2, is_open_attr,
21019 & name);
21020
21021 if (end == 0 && sym != NULL && sym->st_size > 0)
21022 end = start + sym->st_size;
21023
21024 if (is_open_attr)
21025 {
21026 /* FIXME: Need to properly allow for section alignment.
21027 16 is just the alignment used on x86_64. */
21028 if (global_end > 0
21029 && start > BFD_ALIGN (global_end, 16)
21030 /* Build notes are not guaranteed to be organised in order of
21031 increasing address, but we should find the all of the notes
21032 for one section in the same place. */
21033 && same_section (filedata, start, global_end))
21034 warn (_("Gap in build notes detected from %#lx to %#lx\n"),
21035 global_end + 1, start - 1);
21036
21037 printf (_(" Applies to region from %#lx"), start);
21038 global_offset = start;
21039
21040 if (end)
21041 {
21042 printf (_(" to %#lx"), end);
21043 global_end = end;
21044 }
21045 }
21046 else
21047 {
21048 printf (_(" Applies to region from %#lx"), start);
21049 func_offset = start;
21050
21051 if (end)
21052 {
21053 printf (_(" to %#lx"), end);
21054 func_end = end;
21055 }
21056 }
21057
21058 if (sym && name)
21059 printf (_(" (%s)"), name);
21060
21061 printf ("\n");
21062 return true;
21063 }
21064
21065 static bool
21066 print_gnu_build_attribute_name (Elf_Internal_Note * pnote)
21067 {
21068 static const char string_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_STRING, 0 };
21069 static const char number_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC, 0 };
21070 static const char bool_expected [3] = { GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE, GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE, 0 };
21071 char name_type;
21072 char name_attribute;
21073 const char * expected_types;
21074 const char * name = pnote->namedata;
21075 const char * text;
21076 signed int left;
21077
21078 if (name == NULL || pnote->namesz < 2)
21079 {
21080 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
21081 print_symbol (-20, _(" <corrupt name>"));
21082 return false;
21083 }
21084
21085 if (do_wide)
21086 left = 28;
21087 else
21088 left = 20;
21089
21090 /* Version 2 of the spec adds a "GA" prefix to the name field. */
21091 if (name[0] == 'G' && name[1] == 'A')
21092 {
21093 if (pnote->namesz < 4)
21094 {
21095 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
21096 print_symbol (-20, _(" <corrupt name>"));
21097 return false;
21098 }
21099
21100 printf ("GA");
21101 name += 2;
21102 left -= 2;
21103 }
21104
21105 switch ((name_type = * name))
21106 {
21107 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
21108 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
21109 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
21110 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
21111 printf ("%c", * name);
21112 left --;
21113 break;
21114 default:
21115 error (_("unrecognised attribute type in name field: %d\n"), name_type);
21116 print_symbol (-20, _("<unknown name type>"));
21117 return false;
21118 }
21119
21120 ++ name;
21121 text = NULL;
21122
21123 switch ((name_attribute = * name))
21124 {
21125 case GNU_BUILD_ATTRIBUTE_VERSION:
21126 text = _("<version>");
21127 expected_types = string_expected;
21128 ++ name;
21129 break;
21130 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
21131 text = _("<stack prot>");
21132 expected_types = "!+*";
21133 ++ name;
21134 break;
21135 case GNU_BUILD_ATTRIBUTE_RELRO:
21136 text = _("<relro>");
21137 expected_types = bool_expected;
21138 ++ name;
21139 break;
21140 case GNU_BUILD_ATTRIBUTE_STACK_SIZE:
21141 text = _("<stack size>");
21142 expected_types = number_expected;
21143 ++ name;
21144 break;
21145 case GNU_BUILD_ATTRIBUTE_TOOL:
21146 text = _("<tool>");
21147 expected_types = string_expected;
21148 ++ name;
21149 break;
21150 case GNU_BUILD_ATTRIBUTE_ABI:
21151 text = _("<ABI>");
21152 expected_types = "$*";
21153 ++ name;
21154 break;
21155 case GNU_BUILD_ATTRIBUTE_PIC:
21156 text = _("<PIC>");
21157 expected_types = number_expected;
21158 ++ name;
21159 break;
21160 case GNU_BUILD_ATTRIBUTE_SHORT_ENUM:
21161 text = _("<short enum>");
21162 expected_types = bool_expected;
21163 ++ name;
21164 break;
21165 default:
21166 if (ISPRINT (* name))
21167 {
21168 int len = strnlen (name, pnote->namesz - (name - pnote->namedata)) + 1;
21169
21170 if (len > left && ! do_wide)
21171 len = left;
21172 printf ("%.*s:", len, name);
21173 left -= len;
21174 name += len;
21175 }
21176 else
21177 {
21178 static char tmpbuf [128];
21179
21180 error (_("unrecognised byte in name field: %d\n"), * name);
21181 sprintf (tmpbuf, _("<unknown:_%d>"), * name);
21182 text = tmpbuf;
21183 name ++;
21184 }
21185 expected_types = "*$!+";
21186 break;
21187 }
21188
21189 if (text)
21190 left -= printf ("%s", text);
21191
21192 if (strchr (expected_types, name_type) == NULL)
21193 warn (_("attribute does not have an expected type (%c)\n"), name_type);
21194
21195 if ((unsigned long)(name - pnote->namedata) > pnote->namesz)
21196 {
21197 error (_("corrupt name field: namesz: %lu but parsing gets to %ld\n"),
21198 (unsigned long) pnote->namesz,
21199 (long) (name - pnote->namedata));
21200 return false;
21201 }
21202
21203 if (left < 1 && ! do_wide)
21204 return true;
21205
21206 switch (name_type)
21207 {
21208 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
21209 {
21210 unsigned int bytes;
21211 unsigned long long val = 0;
21212 unsigned int shift = 0;
21213 char * decoded = NULL;
21214
21215 bytes = pnote->namesz - (name - pnote->namedata);
21216 if (bytes > 0)
21217 /* The -1 is because the name field is always 0 terminated, and we
21218 want to be able to ensure that the shift in the while loop below
21219 will not overflow. */
21220 -- bytes;
21221
21222 if (bytes > sizeof (val))
21223 {
21224 error (_("corrupt numeric name field: too many bytes in the value: %x\n"),
21225 bytes);
21226 bytes = sizeof (val);
21227 }
21228 /* We do not bother to warn if bytes == 0 as this can
21229 happen with some early versions of the gcc plugin. */
21230
21231 while (bytes --)
21232 {
21233 unsigned long long byte = *name++ & 0xff;
21234
21235 val |= byte << shift;
21236 shift += 8;
21237 }
21238
21239 switch (name_attribute)
21240 {
21241 case GNU_BUILD_ATTRIBUTE_PIC:
21242 switch (val)
21243 {
21244 case 0: decoded = "static"; break;
21245 case 1: decoded = "pic"; break;
21246 case 2: decoded = "PIC"; break;
21247 case 3: decoded = "pie"; break;
21248 case 4: decoded = "PIE"; break;
21249 default: break;
21250 }
21251 break;
21252 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
21253 switch (val)
21254 {
21255 /* Based upon the SPCT_FLAG_xxx enum values in gcc/cfgexpand.c. */
21256 case 0: decoded = "off"; break;
21257 case 1: decoded = "on"; break;
21258 case 2: decoded = "all"; break;
21259 case 3: decoded = "strong"; break;
21260 case 4: decoded = "explicit"; break;
21261 default: break;
21262 }
21263 break;
21264 default:
21265 break;
21266 }
21267
21268 if (decoded != NULL)
21269 {
21270 print_symbol (-left, decoded);
21271 left = 0;
21272 }
21273 else if (val == 0)
21274 {
21275 printf ("0x0");
21276 left -= 3;
21277 }
21278 else
21279 {
21280 if (do_wide)
21281 left -= printf ("0x%llx", val);
21282 else
21283 left -= printf ("0x%-.*llx", left, val);
21284 }
21285 }
21286 break;
21287 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
21288 left -= print_symbol (- left, name);
21289 break;
21290 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
21291 left -= print_symbol (- left, "true");
21292 break;
21293 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
21294 left -= print_symbol (- left, "false");
21295 break;
21296 }
21297
21298 if (do_wide && left > 0)
21299 printf ("%-*s", left, " ");
21300
21301 return true;
21302 }
21303
21304 /* Print the contents of PNOTE as hex. */
21305
21306 static void
21307 print_note_contents_hex (Elf_Internal_Note *pnote)
21308 {
21309 if (pnote->descsz)
21310 {
21311 unsigned long i;
21312
21313 printf (_(" description data: "));
21314 for (i = 0; i < pnote->descsz; i++)
21315 printf ("%02x ", pnote->descdata[i] & 0xff);
21316 if (!do_wide)
21317 printf ("\n");
21318 }
21319
21320 if (do_wide)
21321 printf ("\n");
21322 }
21323
21324 #if defined HAVE_MSGPACK
21325
21326 static void
21327 print_indents (int n)
21328 {
21329 printf (" ");
21330
21331 for (int i = 0; i < n; i++)
21332 printf (" ");
21333 }
21334
21335 /* Print OBJ in human-readable form. */
21336
21337 static void
21338 dump_msgpack_obj (const msgpack_object *obj, int indent)
21339 {
21340 switch (obj->type)
21341 {
21342 case MSGPACK_OBJECT_NIL:
21343 printf ("(nil)");
21344 break;
21345
21346 case MSGPACK_OBJECT_BOOLEAN:
21347 printf ("%s", obj->via.boolean ? "true" : "false");
21348 break;
21349
21350 case MSGPACK_OBJECT_POSITIVE_INTEGER:
21351 printf ("%" PRIu64, obj->via.u64);
21352 break;
21353
21354 case MSGPACK_OBJECT_NEGATIVE_INTEGER:
21355 printf ("%" PRIi64, obj->via.i64);
21356 break;
21357
21358 case MSGPACK_OBJECT_FLOAT32:
21359 case MSGPACK_OBJECT_FLOAT64:
21360 printf ("%f", obj->via.f64);
21361 break;
21362
21363 case MSGPACK_OBJECT_STR:
21364 printf ("\"%.*s\"", obj->via.str.size, obj->via.str.ptr);
21365 break;
21366
21367 case MSGPACK_OBJECT_ARRAY:
21368 {
21369 const msgpack_object_array *array = &obj->via.array;
21370
21371 printf ("[\n");
21372 ++indent;
21373
21374 for (uint32_t i = 0; i < array->size; ++i)
21375 {
21376 const msgpack_object *item = &array->ptr[i];
21377
21378 print_indents (indent);
21379 dump_msgpack_obj (item, indent);
21380 printf (",\n");
21381 }
21382
21383 --indent;
21384 print_indents (indent);
21385 printf ("]");
21386 break;
21387 }
21388 break;
21389
21390 case MSGPACK_OBJECT_MAP:
21391 {
21392 const msgpack_object_map *map = &obj->via.map;
21393
21394 printf ("{\n");
21395 ++indent;
21396
21397 for (uint32_t i = 0; i < map->size; ++i)
21398 {
21399 const msgpack_object_kv *kv = &map->ptr[i];
21400 const msgpack_object *key = &kv->key;
21401 const msgpack_object *val = &kv->val;
21402
21403 print_indents (indent);
21404 dump_msgpack_obj (key, indent);
21405 printf (": ");
21406 dump_msgpack_obj (val, indent);
21407
21408 printf (",\n");
21409 }
21410
21411 --indent;
21412 print_indents (indent);
21413 printf ("}");
21414
21415 break;
21416 }
21417
21418 case MSGPACK_OBJECT_BIN:
21419 printf ("(bin)");
21420 break;
21421
21422 case MSGPACK_OBJECT_EXT:
21423 printf ("(ext)");
21424 break;
21425 }
21426 }
21427
21428 static void
21429 dump_msgpack (const msgpack_unpacked *msg)
21430 {
21431 print_indents (0);
21432 dump_msgpack_obj (&msg->data, 0);
21433 printf ("\n");
21434 }
21435
21436 #endif /* defined HAVE_MSGPACK */
21437
21438 static bool
21439 print_amdgpu_note (Elf_Internal_Note *pnote)
21440 {
21441 #if defined HAVE_MSGPACK
21442 /* If msgpack is available, decode and dump the note's content. */
21443 bool ret;
21444 msgpack_unpacked msg;
21445 msgpack_unpack_return msgpack_ret;
21446
21447 assert (pnote->type == NT_AMDGPU_METADATA);
21448
21449 msgpack_unpacked_init (&msg);
21450 msgpack_ret = msgpack_unpack_next (&msg, pnote->descdata, pnote->descsz,
21451 NULL);
21452
21453 switch (msgpack_ret)
21454 {
21455 case MSGPACK_UNPACK_SUCCESS:
21456 dump_msgpack (&msg);
21457 ret = true;
21458 break;
21459
21460 default:
21461 error (_("failed to unpack msgpack contents in NT_AMDGPU_METADATA note"));
21462 ret = false;
21463 break;
21464 }
21465
21466 msgpack_unpacked_destroy (&msg);
21467 return ret;
21468 #else
21469 /* msgpack is not available, dump contents as hex. */
21470 print_note_contents_hex (pnote);
21471 return true;
21472 #endif
21473 }
21474
21475 /* Note that by the ELF standard, the name field is already null byte
21476 terminated, and namesz includes the terminating null byte.
21477 I.E. the value of namesz for the name "FSF" is 4.
21478
21479 If the value of namesz is zero, there is no name present. */
21480
21481 static bool
21482 process_note (Elf_Internal_Note * pnote,
21483 Filedata * filedata)
21484 {
21485 const char * name = pnote->namesz ? pnote->namedata : "(NONE)";
21486 const char * nt;
21487
21488 if (pnote->namesz == 0)
21489 /* If there is no note name, then use the default set of
21490 note type strings. */
21491 nt = get_note_type (filedata, pnote->type);
21492
21493 else if (startswith (pnote->namedata, "GNU"))
21494 /* GNU-specific object file notes. */
21495 nt = get_gnu_elf_note_type (pnote->type);
21496
21497 else if (startswith (pnote->namedata, "AMDGPU"))
21498 /* AMDGPU-specific object file notes. */
21499 nt = get_amdgpu_elf_note_type (pnote->type);
21500
21501 else if (startswith (pnote->namedata, "FreeBSD"))
21502 /* FreeBSD-specific core file notes. */
21503 nt = get_freebsd_elfcore_note_type (filedata, pnote->type);
21504
21505 else if (startswith (pnote->namedata, "NetBSD-CORE"))
21506 /* NetBSD-specific core file notes. */
21507 nt = get_netbsd_elfcore_note_type (filedata, pnote->type);
21508
21509 else if (startswith (pnote->namedata, "NetBSD"))
21510 /* NetBSD-specific core file notes. */
21511 return process_netbsd_elf_note (pnote);
21512
21513 else if (startswith (pnote->namedata, "PaX"))
21514 /* NetBSD-specific core file notes. */
21515 return process_netbsd_elf_note (pnote);
21516
21517 else if (startswith (pnote->namedata, "OpenBSD"))
21518 /* OpenBSD-specific core file notes. */
21519 nt = get_openbsd_elfcore_note_type (filedata, pnote->type);
21520
21521 else if (startswith (pnote->namedata, "SPU/"))
21522 {
21523 /* SPU-specific core file notes. */
21524 nt = pnote->namedata + 4;
21525 name = "SPU";
21526 }
21527
21528 else if (startswith (pnote->namedata, "IPF/VMS"))
21529 /* VMS/ia64-specific file notes. */
21530 nt = get_ia64_vms_note_type (pnote->type);
21531
21532 else if (startswith (pnote->namedata, "stapsdt"))
21533 nt = get_stapsdt_note_type (pnote->type);
21534
21535 else
21536 /* Don't recognize this note name; just use the default set of
21537 note type strings. */
21538 nt = get_note_type (filedata, pnote->type);
21539
21540 printf (" ");
21541
21542 if (((startswith (pnote->namedata, "GA")
21543 && strchr ("*$!+", pnote->namedata[2]) != NULL)
21544 || strchr ("*$!+", pnote->namedata[0]) != NULL)
21545 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
21546 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
21547 print_gnu_build_attribute_name (pnote);
21548 else
21549 print_symbol (-20, name);
21550
21551 if (do_wide)
21552 printf (" 0x%08lx\t%s\t", pnote->descsz, nt);
21553 else
21554 printf (" 0x%08lx\t%s\n", pnote->descsz, nt);
21555
21556 if (startswith (pnote->namedata, "IPF/VMS"))
21557 return print_ia64_vms_note (pnote);
21558 else if (startswith (pnote->namedata, "GNU"))
21559 return print_gnu_note (filedata, pnote);
21560 else if (startswith (pnote->namedata, "stapsdt"))
21561 return print_stapsdt_note (pnote);
21562 else if (startswith (pnote->namedata, "CORE"))
21563 return print_core_note (pnote);
21564 else if (startswith (pnote->namedata, "FDO"))
21565 return print_fdo_note (pnote);
21566 else if (((startswith (pnote->namedata, "GA")
21567 && strchr ("*$!+", pnote->namedata[2]) != NULL)
21568 || strchr ("*$!+", pnote->namedata[0]) != NULL)
21569 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
21570 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
21571 return print_gnu_build_attribute_description (pnote, filedata);
21572 else if (startswith (pnote->namedata, "AMDGPU")
21573 && pnote->type == NT_AMDGPU_METADATA)
21574 return print_amdgpu_note (pnote);
21575
21576 print_note_contents_hex (pnote);
21577 return true;
21578 }
21579
21580 static bool
21581 process_notes_at (Filedata * filedata,
21582 Elf_Internal_Shdr * section,
21583 bfd_vma offset,
21584 bfd_vma length,
21585 bfd_vma align)
21586 {
21587 Elf_External_Note *pnotes;
21588 Elf_External_Note *external;
21589 char *end;
21590 bool res = true;
21591
21592 if (length <= 0)
21593 return false;
21594
21595 if (section)
21596 {
21597 pnotes = (Elf_External_Note *) get_section_contents (section, filedata);
21598 if (pnotes)
21599 {
21600 if (! apply_relocations (filedata, section, (unsigned char *) pnotes, length, NULL, NULL))
21601 {
21602 free (pnotes);
21603 return false;
21604 }
21605 }
21606 }
21607 else
21608 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
21609 _("notes"));
21610
21611 if (pnotes == NULL)
21612 return false;
21613
21614 external = pnotes;
21615
21616 if (filedata->is_separate)
21617 printf (_("In linked file '%s': "), filedata->file_name);
21618 else
21619 printf ("\n");
21620 if (section)
21621 printf (_("Displaying notes found in: %s\n"), printable_section_name (filedata, section));
21622 else
21623 printf (_("Displaying notes found at file offset 0x%08lx with length 0x%08lx:\n"),
21624 (unsigned long) offset, (unsigned long) length);
21625
21626 /* NB: Some note sections may have alignment value of 0 or 1. gABI
21627 specifies that notes should be aligned to 4 bytes in 32-bit
21628 objects and to 8 bytes in 64-bit objects. As a Linux extension,
21629 we also support 4 byte alignment in 64-bit objects. If section
21630 alignment is less than 4, we treate alignment as 4 bytes. */
21631 if (align < 4)
21632 align = 4;
21633 else if (align != 4 && align != 8)
21634 {
21635 warn (_("Corrupt note: alignment %ld, expecting 4 or 8\n"),
21636 (long) align);
21637 free (pnotes);
21638 return false;
21639 }
21640
21641 printf (_(" %-20s %-10s\tDescription\n"), _("Owner"), _("Data size"));
21642
21643 end = (char *) pnotes + length;
21644 while ((char *) external < end)
21645 {
21646 Elf_Internal_Note inote;
21647 size_t min_notesz;
21648 char * next;
21649 char * temp = NULL;
21650 size_t data_remaining = end - (char *) external;
21651
21652 if (!is_ia64_vms (filedata))
21653 {
21654 /* PR binutils/15191
21655 Make sure that there is enough data to read. */
21656 min_notesz = offsetof (Elf_External_Note, name);
21657 if (data_remaining < min_notesz)
21658 {
21659 warn (ngettext ("Corrupt note: only %ld byte remains, "
21660 "not enough for a full note\n",
21661 "Corrupt note: only %ld bytes remain, "
21662 "not enough for a full note\n",
21663 data_remaining),
21664 (long) data_remaining);
21665 break;
21666 }
21667 data_remaining -= min_notesz;
21668
21669 inote.type = BYTE_GET (external->type);
21670 inote.namesz = BYTE_GET (external->namesz);
21671 inote.namedata = external->name;
21672 inote.descsz = BYTE_GET (external->descsz);
21673 inote.descdata = ((char *) external
21674 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
21675 inote.descpos = offset + (inote.descdata - (char *) pnotes);
21676 next = ((char *) external
21677 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
21678 }
21679 else
21680 {
21681 Elf64_External_VMS_Note *vms_external;
21682
21683 /* PR binutils/15191
21684 Make sure that there is enough data to read. */
21685 min_notesz = offsetof (Elf64_External_VMS_Note, name);
21686 if (data_remaining < min_notesz)
21687 {
21688 warn (ngettext ("Corrupt note: only %ld byte remains, "
21689 "not enough for a full note\n",
21690 "Corrupt note: only %ld bytes remain, "
21691 "not enough for a full note\n",
21692 data_remaining),
21693 (long) data_remaining);
21694 break;
21695 }
21696 data_remaining -= min_notesz;
21697
21698 vms_external = (Elf64_External_VMS_Note *) external;
21699 inote.type = BYTE_GET (vms_external->type);
21700 inote.namesz = BYTE_GET (vms_external->namesz);
21701 inote.namedata = vms_external->name;
21702 inote.descsz = BYTE_GET (vms_external->descsz);
21703 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
21704 inote.descpos = offset + (inote.descdata - (char *) pnotes);
21705 next = inote.descdata + align_power (inote.descsz, 3);
21706 }
21707
21708 /* PR 17531: file: 3443835e. */
21709 /* PR 17531: file: id:000000,sig:11,src:006986,op:havoc,rep:4. */
21710 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
21711 || (size_t) (inote.descdata - inote.namedata) > data_remaining
21712 || (size_t) (next - inote.descdata) < inote.descsz
21713 || ((size_t) (next - inote.descdata)
21714 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
21715 {
21716 warn (_("note with invalid namesz and/or descsz found at offset 0x%lx\n"),
21717 (unsigned long) ((char *) external - (char *) pnotes));
21718 warn (_(" type: 0x%lx, namesize: 0x%08lx, descsize: 0x%08lx, alignment: %u\n"),
21719 inote.type, inote.namesz, inote.descsz, (int) align);
21720 break;
21721 }
21722
21723 external = (Elf_External_Note *) next;
21724
21725 /* Verify that name is null terminated. It appears that at least
21726 one version of Linux (RedHat 6.0) generates corefiles that don't
21727 comply with the ELF spec by failing to include the null byte in
21728 namesz. */
21729 if (inote.namesz > 0 && inote.namedata[inote.namesz - 1] != '\0')
21730 {
21731 if ((size_t) (inote.descdata - inote.namedata) == inote.namesz)
21732 {
21733 temp = (char *) malloc (inote.namesz + 1);
21734 if (temp == NULL)
21735 {
21736 error (_("Out of memory allocating space for inote name\n"));
21737 res = false;
21738 break;
21739 }
21740
21741 memcpy (temp, inote.namedata, inote.namesz);
21742 inote.namedata = temp;
21743 }
21744 inote.namedata[inote.namesz] = 0;
21745 }
21746
21747 if (! process_note (& inote, filedata))
21748 res = false;
21749
21750 free (temp);
21751 temp = NULL;
21752 }
21753
21754 free (pnotes);
21755
21756 return res;
21757 }
21758
21759 static bool
21760 process_corefile_note_segments (Filedata * filedata)
21761 {
21762 Elf_Internal_Phdr *segment;
21763 unsigned int i;
21764 bool res = true;
21765
21766 if (! get_program_headers (filedata))
21767 return true;
21768
21769 for (i = 0, segment = filedata->program_headers;
21770 i < filedata->file_header.e_phnum;
21771 i++, segment++)
21772 {
21773 if (segment->p_type == PT_NOTE)
21774 if (! process_notes_at (filedata, NULL,
21775 (bfd_vma) segment->p_offset,
21776 (bfd_vma) segment->p_filesz,
21777 (bfd_vma) segment->p_align))
21778 res = false;
21779 }
21780
21781 return res;
21782 }
21783
21784 static bool
21785 process_v850_notes (Filedata * filedata, bfd_vma offset, bfd_vma length)
21786 {
21787 Elf_External_Note * pnotes;
21788 Elf_External_Note * external;
21789 char * end;
21790 bool res = true;
21791
21792 if (length <= 0)
21793 return false;
21794
21795 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
21796 _("v850 notes"));
21797 if (pnotes == NULL)
21798 return false;
21799
21800 external = pnotes;
21801 end = (char*) pnotes + length;
21802
21803 printf (_("\nDisplaying contents of Renesas V850 notes section at offset 0x%lx with length 0x%lx:\n"),
21804 (unsigned long) offset, (unsigned long) length);
21805
21806 while ((char *) external + sizeof (Elf_External_Note) < end)
21807 {
21808 Elf_External_Note * next;
21809 Elf_Internal_Note inote;
21810
21811 inote.type = BYTE_GET (external->type);
21812 inote.namesz = BYTE_GET (external->namesz);
21813 inote.namedata = external->name;
21814 inote.descsz = BYTE_GET (external->descsz);
21815 inote.descdata = inote.namedata + align_power (inote.namesz, 2);
21816 inote.descpos = offset + (inote.descdata - (char *) pnotes);
21817
21818 if (inote.descdata < (char *) pnotes || inote.descdata >= end)
21819 {
21820 warn (_("Corrupt note: name size is too big: %lx\n"), inote.namesz);
21821 inote.descdata = inote.namedata;
21822 inote.namesz = 0;
21823 }
21824
21825 next = (Elf_External_Note *) (inote.descdata + align_power (inote.descsz, 2));
21826
21827 if ( ((char *) next > end)
21828 || ((char *) next < (char *) pnotes))
21829 {
21830 warn (_("corrupt descsz found in note at offset 0x%lx\n"),
21831 (unsigned long) ((char *) external - (char *) pnotes));
21832 warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
21833 inote.type, inote.namesz, inote.descsz);
21834 break;
21835 }
21836
21837 external = next;
21838
21839 /* Prevent out-of-bounds indexing. */
21840 if ( inote.namedata + inote.namesz > end
21841 || inote.namedata + inote.namesz < inote.namedata)
21842 {
21843 warn (_("corrupt namesz found in note at offset 0x%lx\n"),
21844 (unsigned long) ((char *) external - (char *) pnotes));
21845 warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
21846 inote.type, inote.namesz, inote.descsz);
21847 break;
21848 }
21849
21850 printf (" %s: ", get_v850_elf_note_type (inote.type));
21851
21852 if (! print_v850_note (& inote))
21853 {
21854 res = false;
21855 printf ("<corrupt sizes: namesz: %lx, descsz: %lx>\n",
21856 inote.namesz, inote.descsz);
21857 }
21858 }
21859
21860 free (pnotes);
21861
21862 return res;
21863 }
21864
21865 static bool
21866 process_note_sections (Filedata * filedata)
21867 {
21868 Elf_Internal_Shdr *section;
21869 unsigned long i;
21870 unsigned int n = 0;
21871 bool res = true;
21872
21873 for (i = 0, section = filedata->section_headers;
21874 i < filedata->file_header.e_shnum && section != NULL;
21875 i++, section++)
21876 {
21877 if (section->sh_type == SHT_NOTE)
21878 {
21879 if (! process_notes_at (filedata, section,
21880 (bfd_vma) section->sh_offset,
21881 (bfd_vma) section->sh_size,
21882 (bfd_vma) section->sh_addralign))
21883 res = false;
21884 n++;
21885 }
21886
21887 if (( filedata->file_header.e_machine == EM_V800
21888 || filedata->file_header.e_machine == EM_V850
21889 || filedata->file_header.e_machine == EM_CYGNUS_V850)
21890 && section->sh_type == SHT_RENESAS_INFO)
21891 {
21892 if (! process_v850_notes (filedata,
21893 (bfd_vma) section->sh_offset,
21894 (bfd_vma) section->sh_size))
21895 res = false;
21896 n++;
21897 }
21898 }
21899
21900 if (n == 0)
21901 /* Try processing NOTE segments instead. */
21902 return process_corefile_note_segments (filedata);
21903
21904 return res;
21905 }
21906
21907 static bool
21908 process_notes (Filedata * filedata)
21909 {
21910 /* If we have not been asked to display the notes then do nothing. */
21911 if (! do_notes)
21912 return true;
21913
21914 if (filedata->file_header.e_type != ET_CORE)
21915 return process_note_sections (filedata);
21916
21917 /* No program headers means no NOTE segment. */
21918 if (filedata->file_header.e_phnum > 0)
21919 return process_corefile_note_segments (filedata);
21920
21921 if (filedata->is_separate)
21922 printf (_("No notes found in linked file '%s'.\n"),
21923 filedata->file_name);
21924 else
21925 printf (_("No notes found file.\n"));
21926
21927 return true;
21928 }
21929
21930 static unsigned char *
21931 display_public_gnu_attributes (unsigned char * start,
21932 const unsigned char * const end)
21933 {
21934 printf (_(" Unknown GNU attribute: %s\n"), start);
21935
21936 start += strnlen ((char *) start, end - start);
21937 display_raw_attribute (start, end);
21938
21939 return (unsigned char *) end;
21940 }
21941
21942 static unsigned char *
21943 display_generic_attribute (unsigned char * start,
21944 unsigned int tag,
21945 const unsigned char * const end)
21946 {
21947 if (tag == 0)
21948 return (unsigned char *) end;
21949
21950 return display_tag_value (tag, start, end);
21951 }
21952
21953 static bool
21954 process_arch_specific (Filedata * filedata)
21955 {
21956 if (! do_arch)
21957 return true;
21958
21959 switch (filedata->file_header.e_machine)
21960 {
21961 case EM_ARC:
21962 case EM_ARC_COMPACT:
21963 case EM_ARC_COMPACT2:
21964 return process_attributes (filedata, "ARC", SHT_ARC_ATTRIBUTES,
21965 display_arc_attribute,
21966 display_generic_attribute);
21967 case EM_ARM:
21968 return process_attributes (filedata, "aeabi", SHT_ARM_ATTRIBUTES,
21969 display_arm_attribute,
21970 display_generic_attribute);
21971
21972 case EM_MIPS:
21973 case EM_MIPS_RS3_LE:
21974 return process_mips_specific (filedata);
21975
21976 case EM_MSP430:
21977 return process_attributes (filedata, "mspabi", SHT_MSP430_ATTRIBUTES,
21978 display_msp430_attribute,
21979 display_msp430_gnu_attribute);
21980
21981 case EM_RISCV:
21982 return process_attributes (filedata, "riscv", SHT_RISCV_ATTRIBUTES,
21983 display_riscv_attribute,
21984 display_generic_attribute);
21985
21986 case EM_NDS32:
21987 return process_nds32_specific (filedata);
21988
21989 case EM_68K:
21990 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
21991 display_m68k_gnu_attribute);
21992
21993 case EM_PPC:
21994 case EM_PPC64:
21995 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
21996 display_power_gnu_attribute);
21997
21998 case EM_S390:
21999 case EM_S390_OLD:
22000 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22001 display_s390_gnu_attribute);
22002
22003 case EM_SPARC:
22004 case EM_SPARC32PLUS:
22005 case EM_SPARCV9:
22006 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22007 display_sparc_gnu_attribute);
22008
22009 case EM_TI_C6000:
22010 return process_attributes (filedata, "c6xabi", SHT_C6000_ATTRIBUTES,
22011 display_tic6x_attribute,
22012 display_generic_attribute);
22013
22014 case EM_CSKY:
22015 return process_attributes (filedata, "csky", SHT_CSKY_ATTRIBUTES,
22016 display_csky_attribute, NULL);
22017
22018 default:
22019 return process_attributes (filedata, "gnu", SHT_GNU_ATTRIBUTES,
22020 display_public_gnu_attributes,
22021 display_generic_attribute);
22022 }
22023 }
22024
22025 static bool
22026 get_file_header (Filedata * filedata)
22027 {
22028 /* Read in the identity array. */
22029 if (fread (filedata->file_header.e_ident, EI_NIDENT, 1, filedata->handle) != 1)
22030 return false;
22031
22032 /* Determine how to read the rest of the header. */
22033 switch (filedata->file_header.e_ident[EI_DATA])
22034 {
22035 default:
22036 case ELFDATANONE:
22037 case ELFDATA2LSB:
22038 byte_get = byte_get_little_endian;
22039 byte_put = byte_put_little_endian;
22040 break;
22041 case ELFDATA2MSB:
22042 byte_get = byte_get_big_endian;
22043 byte_put = byte_put_big_endian;
22044 break;
22045 }
22046
22047 /* For now we only support 32 bit and 64 bit ELF files. */
22048 is_32bit_elf = (filedata->file_header.e_ident[EI_CLASS] != ELFCLASS64);
22049
22050 /* Read in the rest of the header. */
22051 if (is_32bit_elf)
22052 {
22053 Elf32_External_Ehdr ehdr32;
22054
22055 if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, filedata->handle) != 1)
22056 return false;
22057
22058 filedata->file_header.e_type = BYTE_GET (ehdr32.e_type);
22059 filedata->file_header.e_machine = BYTE_GET (ehdr32.e_machine);
22060 filedata->file_header.e_version = BYTE_GET (ehdr32.e_version);
22061 filedata->file_header.e_entry = BYTE_GET (ehdr32.e_entry);
22062 filedata->file_header.e_phoff = BYTE_GET (ehdr32.e_phoff);
22063 filedata->file_header.e_shoff = BYTE_GET (ehdr32.e_shoff);
22064 filedata->file_header.e_flags = BYTE_GET (ehdr32.e_flags);
22065 filedata->file_header.e_ehsize = BYTE_GET (ehdr32.e_ehsize);
22066 filedata->file_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
22067 filedata->file_header.e_phnum = BYTE_GET (ehdr32.e_phnum);
22068 filedata->file_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
22069 filedata->file_header.e_shnum = BYTE_GET (ehdr32.e_shnum);
22070 filedata->file_header.e_shstrndx = BYTE_GET (ehdr32.e_shstrndx);
22071 }
22072 else
22073 {
22074 Elf64_External_Ehdr ehdr64;
22075
22076 /* If we have been compiled with sizeof (bfd_vma) == 4, then
22077 we will not be able to cope with the 64bit data found in
22078 64 ELF files. Detect this now and abort before we start
22079 overwriting things. */
22080 if (sizeof (bfd_vma) < 8)
22081 {
22082 error (_("This instance of readelf has been built without support for a\n\
22083 64 bit data type and so it cannot read 64 bit ELF files.\n"));
22084 return false;
22085 }
22086
22087 if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, filedata->handle) != 1)
22088 return false;
22089
22090 filedata->file_header.e_type = BYTE_GET (ehdr64.e_type);
22091 filedata->file_header.e_machine = BYTE_GET (ehdr64.e_machine);
22092 filedata->file_header.e_version = BYTE_GET (ehdr64.e_version);
22093 filedata->file_header.e_entry = BYTE_GET (ehdr64.e_entry);
22094 filedata->file_header.e_phoff = BYTE_GET (ehdr64.e_phoff);
22095 filedata->file_header.e_shoff = BYTE_GET (ehdr64.e_shoff);
22096 filedata->file_header.e_flags = BYTE_GET (ehdr64.e_flags);
22097 filedata->file_header.e_ehsize = BYTE_GET (ehdr64.e_ehsize);
22098 filedata->file_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
22099 filedata->file_header.e_phnum = BYTE_GET (ehdr64.e_phnum);
22100 filedata->file_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
22101 filedata->file_header.e_shnum = BYTE_GET (ehdr64.e_shnum);
22102 filedata->file_header.e_shstrndx = BYTE_GET (ehdr64.e_shstrndx);
22103 }
22104
22105 return true;
22106 }
22107
22108 static void
22109 free_filedata (Filedata *filedata)
22110 {
22111 free (filedata->program_interpreter);
22112 free (filedata->program_headers);
22113 free (filedata->section_headers);
22114 free (filedata->string_table);
22115 free (filedata->dump.dump_sects);
22116 free (filedata->dynamic_strings);
22117 free (filedata->dynamic_symbols);
22118 free (filedata->dynamic_syminfo);
22119 free (filedata->dynamic_section);
22120
22121 while (filedata->symtab_shndx_list != NULL)
22122 {
22123 elf_section_list *next = filedata->symtab_shndx_list->next;
22124 free (filedata->symtab_shndx_list);
22125 filedata->symtab_shndx_list = next;
22126 }
22127
22128 free (filedata->section_headers_groups);
22129
22130 if (filedata->section_groups)
22131 {
22132 size_t i;
22133 struct group_list * g;
22134 struct group_list * next;
22135
22136 for (i = 0; i < filedata->group_count; i++)
22137 {
22138 for (g = filedata->section_groups [i].root; g != NULL; g = next)
22139 {
22140 next = g->next;
22141 free (g);
22142 }
22143 }
22144
22145 free (filedata->section_groups);
22146 }
22147 memset (&filedata->section_headers, 0,
22148 sizeof (Filedata) - offsetof (Filedata, section_headers));
22149 }
22150
22151 static void
22152 close_file (Filedata * filedata)
22153 {
22154 if (filedata)
22155 {
22156 if (filedata->handle)
22157 fclose (filedata->handle);
22158 free (filedata);
22159 }
22160 }
22161
22162 void
22163 close_debug_file (void * data)
22164 {
22165 free_filedata ((Filedata *) data);
22166 close_file ((Filedata *) data);
22167 }
22168
22169 static Filedata *
22170 open_file (const char * pathname, bool is_separate)
22171 {
22172 struct stat statbuf;
22173 Filedata * filedata = NULL;
22174
22175 if (stat (pathname, & statbuf) < 0
22176 || ! S_ISREG (statbuf.st_mode))
22177 goto fail;
22178
22179 filedata = calloc (1, sizeof * filedata);
22180 if (filedata == NULL)
22181 goto fail;
22182
22183 filedata->handle = fopen (pathname, "rb");
22184 if (filedata->handle == NULL)
22185 goto fail;
22186
22187 filedata->file_size = (bfd_size_type) statbuf.st_size;
22188 filedata->file_name = pathname;
22189 filedata->is_separate = is_separate;
22190
22191 if (! get_file_header (filedata))
22192 goto fail;
22193
22194 if (!get_section_headers (filedata, false))
22195 goto fail;
22196
22197 return filedata;
22198
22199 fail:
22200 if (filedata)
22201 {
22202 if (filedata->handle)
22203 fclose (filedata->handle);
22204 free (filedata);
22205 }
22206 return NULL;
22207 }
22208
22209 void *
22210 open_debug_file (const char * pathname)
22211 {
22212 return open_file (pathname, true);
22213 }
22214
22215 static void
22216 initialise_dump_sects (Filedata * filedata)
22217 {
22218 /* Initialise the dump_sects array from the cmdline_dump_sects array.
22219 Note we do this even if cmdline_dump_sects is empty because we
22220 must make sure that the dump_sets array is zeroed out before each
22221 object file is processed. */
22222 if (filedata->dump.num_dump_sects > cmdline.num_dump_sects)
22223 memset (filedata->dump.dump_sects, 0,
22224 filedata->dump.num_dump_sects * sizeof (*filedata->dump.dump_sects));
22225
22226 if (cmdline.num_dump_sects > 0)
22227 {
22228 if (filedata->dump.num_dump_sects == 0)
22229 /* A sneaky way of allocating the dump_sects array. */
22230 request_dump_bynumber (&filedata->dump, cmdline.num_dump_sects, 0);
22231
22232 assert (filedata->dump.num_dump_sects >= cmdline.num_dump_sects);
22233 memcpy (filedata->dump.dump_sects, cmdline.dump_sects,
22234 cmdline.num_dump_sects * sizeof (*filedata->dump.dump_sects));
22235 }
22236 }
22237
22238 static bool
22239 might_need_separate_debug_info (Filedata * filedata)
22240 {
22241 /* Debuginfo files do not need further separate file loading. */
22242 if (filedata->file_header.e_shstrndx == SHN_UNDEF)
22243 return false;
22244
22245 /* Since do_follow_links might be enabled by default, only treat it as an
22246 indication that separate files should be loaded if setting it was a
22247 deliberate user action. */
22248 if (DEFAULT_FOR_FOLLOW_LINKS == 0 && do_follow_links)
22249 return true;
22250
22251 if (process_links || do_syms || do_unwind
22252 || dump_any_debugging || do_dump || do_debugging)
22253 return true;
22254
22255 return false;
22256 }
22257
22258 /* Process one ELF object file according to the command line options.
22259 This file may actually be stored in an archive. The file is
22260 positioned at the start of the ELF object. Returns TRUE if no
22261 problems were encountered, FALSE otherwise. */
22262
22263 static bool
22264 process_object (Filedata * filedata)
22265 {
22266 bool have_separate_files;
22267 unsigned int i;
22268 bool res;
22269
22270 if (! get_file_header (filedata))
22271 {
22272 error (_("%s: Failed to read file header\n"), filedata->file_name);
22273 return false;
22274 }
22275
22276 /* Initialise per file variables. */
22277 for (i = ARRAY_SIZE (filedata->version_info); i--;)
22278 filedata->version_info[i] = 0;
22279
22280 for (i = ARRAY_SIZE (filedata->dynamic_info); i--;)
22281 filedata->dynamic_info[i] = 0;
22282 filedata->dynamic_info_DT_GNU_HASH = 0;
22283 filedata->dynamic_info_DT_MIPS_XHASH = 0;
22284
22285 /* Process the file. */
22286 if (show_name)
22287 printf (_("\nFile: %s\n"), filedata->file_name);
22288
22289 initialise_dump_sects (filedata);
22290
22291 /* There may be some extensions in the first section header. Don't
22292 bomb if we can't read it. */
22293 get_section_headers (filedata, true);
22294
22295 if (! process_file_header (filedata))
22296 {
22297 res = false;
22298 goto out;
22299 }
22300
22301 /* Throw away the single section header read above, so that we
22302 re-read the entire set. */
22303 free (filedata->section_headers);
22304 filedata->section_headers = NULL;
22305
22306 if (! process_section_headers (filedata))
22307 {
22308 /* Without loaded section headers we cannot process lots of things. */
22309 do_unwind = do_version = do_dump = do_arch = false;
22310
22311 if (! do_using_dynamic)
22312 do_syms = do_dyn_syms = do_reloc = false;
22313 }
22314
22315 if (! process_section_groups (filedata))
22316 /* Without loaded section groups we cannot process unwind. */
22317 do_unwind = false;
22318
22319 process_program_headers (filedata);
22320
22321 res = process_dynamic_section (filedata);
22322
22323 if (! process_relocs (filedata))
22324 res = false;
22325
22326 if (! process_unwind (filedata))
22327 res = false;
22328
22329 if (! process_symbol_table (filedata))
22330 res = false;
22331
22332 if (! process_lto_symbol_tables (filedata))
22333 res = false;
22334
22335 if (! process_syminfo (filedata))
22336 res = false;
22337
22338 if (! process_version_sections (filedata))
22339 res = false;
22340
22341 if (might_need_separate_debug_info (filedata))
22342 have_separate_files = load_separate_debug_files (filedata, filedata->file_name);
22343 else
22344 have_separate_files = false;
22345
22346 if (! process_section_contents (filedata))
22347 res = false;
22348
22349 if (have_separate_files)
22350 {
22351 separate_info * d;
22352
22353 for (d = first_separate_info; d != NULL; d = d->next)
22354 {
22355 initialise_dump_sects (d->handle);
22356
22357 if (process_links && ! process_file_header (d->handle))
22358 res = false;
22359 else if (! process_section_headers (d->handle))
22360 res = false;
22361 else if (! process_section_contents (d->handle))
22362 res = false;
22363 else if (process_links)
22364 {
22365 if (! process_section_groups (d->handle))
22366 res = false;
22367 process_program_headers (d->handle);
22368 if (! process_dynamic_section (d->handle))
22369 res = false;
22370 if (! process_relocs (d->handle))
22371 res = false;
22372 if (! process_unwind (d->handle))
22373 res = false;
22374 if (! process_symbol_table (d->handle))
22375 res = false;
22376 if (! process_lto_symbol_tables (d->handle))
22377 res = false;
22378 if (! process_syminfo (d->handle))
22379 res = false;
22380 if (! process_version_sections (d->handle))
22381 res = false;
22382 if (! process_notes (d->handle))
22383 res = false;
22384 }
22385 }
22386
22387 /* The file handles are closed by the call to free_debug_memory() below. */
22388 }
22389
22390 if (! process_notes (filedata))
22391 res = false;
22392
22393 if (! process_gnu_liblist (filedata))
22394 res = false;
22395
22396 if (! process_arch_specific (filedata))
22397 res = false;
22398
22399 out:
22400 free_filedata (filedata);
22401
22402 free_debug_memory ();
22403
22404 return res;
22405 }
22406
22407 /* Process an ELF archive.
22408 On entry the file is positioned just after the ARMAG string.
22409 Returns TRUE upon success, FALSE otherwise. */
22410
22411 static bool
22412 process_archive (Filedata * filedata, bool is_thin_archive)
22413 {
22414 struct archive_info arch;
22415 struct archive_info nested_arch;
22416 size_t got;
22417 bool ret = true;
22418
22419 show_name = true;
22420
22421 /* The ARCH structure is used to hold information about this archive. */
22422 arch.file_name = NULL;
22423 arch.file = NULL;
22424 arch.index_array = NULL;
22425 arch.sym_table = NULL;
22426 arch.longnames = NULL;
22427
22428 /* The NESTED_ARCH structure is used as a single-item cache of information
22429 about a nested archive (when members of a thin archive reside within
22430 another regular archive file). */
22431 nested_arch.file_name = NULL;
22432 nested_arch.file = NULL;
22433 nested_arch.index_array = NULL;
22434 nested_arch.sym_table = NULL;
22435 nested_arch.longnames = NULL;
22436
22437 if (setup_archive (&arch, filedata->file_name, filedata->handle,
22438 filedata->file_size, is_thin_archive,
22439 do_archive_index) != 0)
22440 {
22441 ret = false;
22442 goto out;
22443 }
22444
22445 if (do_archive_index)
22446 {
22447 if (arch.sym_table == NULL)
22448 error (_("%s: unable to dump the index as none was found\n"),
22449 filedata->file_name);
22450 else
22451 {
22452 unsigned long i, l;
22453 unsigned long current_pos;
22454
22455 printf (_("Index of archive %s: (%lu entries, 0x%lx bytes "
22456 "in the symbol table)\n"),
22457 filedata->file_name, (unsigned long) arch.index_num,
22458 arch.sym_size);
22459
22460 current_pos = ftell (filedata->handle);
22461
22462 for (i = l = 0; i < arch.index_num; i++)
22463 {
22464 if (i == 0
22465 || (i > 0 && arch.index_array[i] != arch.index_array[i - 1]))
22466 {
22467 char * member_name
22468 = get_archive_member_name_at (&arch, arch.index_array[i],
22469 &nested_arch);
22470
22471 if (member_name != NULL)
22472 {
22473 char * qualified_name
22474 = make_qualified_name (&arch, &nested_arch,
22475 member_name);
22476
22477 if (qualified_name != NULL)
22478 {
22479 printf (_("Contents of binary %s at offset "),
22480 qualified_name);
22481 (void) print_vma (arch.index_array[i], PREFIX_HEX);
22482 putchar ('\n');
22483 free (qualified_name);
22484 }
22485 free (member_name);
22486 }
22487 }
22488
22489 if (l >= arch.sym_size)
22490 {
22491 error (_("%s: end of the symbol table reached "
22492 "before the end of the index\n"),
22493 filedata->file_name);
22494 ret = false;
22495 break;
22496 }
22497 /* PR 17531: file: 0b6630b2. */
22498 printf ("\t%.*s\n",
22499 (int) (arch.sym_size - l), arch.sym_table + l);
22500 l += strnlen (arch.sym_table + l, arch.sym_size - l) + 1;
22501 }
22502
22503 if (arch.uses_64bit_indices)
22504 l = (l + 7) & ~ 7;
22505 else
22506 l += l & 1;
22507
22508 if (l < arch.sym_size)
22509 {
22510 error (ngettext ("%s: %ld byte remains in the symbol table, "
22511 "but without corresponding entries in "
22512 "the index table\n",
22513 "%s: %ld bytes remain in the symbol table, "
22514 "but without corresponding entries in "
22515 "the index table\n",
22516 arch.sym_size - l),
22517 filedata->file_name, arch.sym_size - l);
22518 ret = false;
22519 }
22520
22521 if (fseek (filedata->handle, current_pos, SEEK_SET) != 0)
22522 {
22523 error (_("%s: failed to seek back to start of object files "
22524 "in the archive\n"),
22525 filedata->file_name);
22526 ret = false;
22527 goto out;
22528 }
22529 }
22530
22531 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
22532 && !do_segments && !do_header && !do_dump && !do_version
22533 && !do_histogram && !do_debugging && !do_arch && !do_notes
22534 && !do_section_groups && !do_dyn_syms)
22535 {
22536 ret = true; /* Archive index only. */
22537 goto out;
22538 }
22539 }
22540
22541 while (1)
22542 {
22543 char * name;
22544 size_t namelen;
22545 char * qualified_name;
22546
22547 /* Read the next archive header. */
22548 if (fseek (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0)
22549 {
22550 error (_("%s: failed to seek to next archive header\n"),
22551 arch.file_name);
22552 ret = false;
22553 break;
22554 }
22555 got = fread (&arch.arhdr, 1, sizeof arch.arhdr, filedata->handle);
22556 if (got != sizeof arch.arhdr)
22557 {
22558 if (got == 0)
22559 break;
22560 /* PR 24049 - we cannot use filedata->file_name as this will
22561 have already been freed. */
22562 error (_("%s: failed to read archive header\n"), arch.file_name);
22563
22564 ret = false;
22565 break;
22566 }
22567 if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
22568 {
22569 error (_("%s: did not find a valid archive header\n"),
22570 arch.file_name);
22571 ret = false;
22572 break;
22573 }
22574
22575 arch.next_arhdr_offset += sizeof arch.arhdr;
22576
22577 filedata->archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
22578
22579 name = get_archive_member_name (&arch, &nested_arch);
22580 if (name == NULL)
22581 {
22582 error (_("%s: bad archive file name\n"), arch.file_name);
22583 ret = false;
22584 break;
22585 }
22586 namelen = strlen (name);
22587
22588 qualified_name = make_qualified_name (&arch, &nested_arch, name);
22589 if (qualified_name == NULL)
22590 {
22591 error (_("%s: bad archive file name\n"), arch.file_name);
22592 free (name);
22593 ret = false;
22594 break;
22595 }
22596
22597 if (is_thin_archive && arch.nested_member_origin == 0)
22598 {
22599 /* This is a proxy for an external member of a thin archive. */
22600 Filedata * member_filedata;
22601 char * member_file_name = adjust_relative_path
22602 (filedata->file_name, name, namelen);
22603
22604 free (name);
22605 if (member_file_name == NULL)
22606 {
22607 free (qualified_name);
22608 ret = false;
22609 break;
22610 }
22611
22612 member_filedata = open_file (member_file_name, false);
22613 if (member_filedata == NULL)
22614 {
22615 error (_("Input file '%s' is not readable.\n"), member_file_name);
22616 free (member_file_name);
22617 free (qualified_name);
22618 ret = false;
22619 break;
22620 }
22621
22622 filedata->archive_file_offset = arch.nested_member_origin;
22623 member_filedata->file_name = qualified_name;
22624
22625 /* The call to process_object() expects the file to be at the beginning. */
22626 rewind (member_filedata->handle);
22627
22628 if (! process_object (member_filedata))
22629 ret = false;
22630
22631 close_file (member_filedata);
22632 free (member_file_name);
22633 }
22634 else if (is_thin_archive)
22635 {
22636 Filedata thin_filedata;
22637
22638 memset (&thin_filedata, 0, sizeof (thin_filedata));
22639
22640 /* PR 15140: Allow for corrupt thin archives. */
22641 if (nested_arch.file == NULL)
22642 {
22643 error (_("%s: contains corrupt thin archive: %s\n"),
22644 qualified_name, name);
22645 free (qualified_name);
22646 free (name);
22647 ret = false;
22648 break;
22649 }
22650 free (name);
22651
22652 /* This is a proxy for a member of a nested archive. */
22653 filedata->archive_file_offset
22654 = arch.nested_member_origin + sizeof arch.arhdr;
22655
22656 /* The nested archive file will have been opened and setup by
22657 get_archive_member_name. */
22658 if (fseek (nested_arch.file, filedata->archive_file_offset,
22659 SEEK_SET) != 0)
22660 {
22661 error (_("%s: failed to seek to archive member.\n"),
22662 nested_arch.file_name);
22663 free (qualified_name);
22664 ret = false;
22665 break;
22666 }
22667
22668 thin_filedata.handle = nested_arch.file;
22669 thin_filedata.file_name = qualified_name;
22670
22671 if (! process_object (& thin_filedata))
22672 ret = false;
22673 }
22674 else
22675 {
22676 free (name);
22677 filedata->archive_file_offset = arch.next_arhdr_offset;
22678 filedata->file_name = qualified_name;
22679 if (! process_object (filedata))
22680 ret = false;
22681 arch.next_arhdr_offset += (filedata->archive_file_size + 1) & -2;
22682 /* Stop looping with "negative" archive_file_size. */
22683 if (arch.next_arhdr_offset < filedata->archive_file_size)
22684 arch.next_arhdr_offset = -1ul;
22685 }
22686
22687 free (qualified_name);
22688 }
22689
22690 out:
22691 if (nested_arch.file != NULL)
22692 fclose (nested_arch.file);
22693 release_archive (&nested_arch);
22694 release_archive (&arch);
22695
22696 return ret;
22697 }
22698
22699 static bool
22700 process_file (char * file_name)
22701 {
22702 Filedata * filedata = NULL;
22703 struct stat statbuf;
22704 char armag[SARMAG];
22705 bool ret = true;
22706
22707 if (stat (file_name, &statbuf) < 0)
22708 {
22709 if (errno == ENOENT)
22710 error (_("'%s': No such file\n"), file_name);
22711 else
22712 error (_("Could not locate '%s'. System error message: %s\n"),
22713 file_name, strerror (errno));
22714 return false;
22715 }
22716
22717 if (! S_ISREG (statbuf.st_mode))
22718 {
22719 error (_("'%s' is not an ordinary file\n"), file_name);
22720 return false;
22721 }
22722
22723 filedata = calloc (1, sizeof * filedata);
22724 if (filedata == NULL)
22725 {
22726 error (_("Out of memory allocating file data structure\n"));
22727 return false;
22728 }
22729
22730 filedata->file_name = file_name;
22731 filedata->handle = fopen (file_name, "rb");
22732 if (filedata->handle == NULL)
22733 {
22734 error (_("Input file '%s' is not readable.\n"), file_name);
22735 free (filedata);
22736 return false;
22737 }
22738
22739 if (fread (armag, SARMAG, 1, filedata->handle) != 1)
22740 {
22741 error (_("%s: Failed to read file's magic number\n"), file_name);
22742 fclose (filedata->handle);
22743 free (filedata);
22744 return false;
22745 }
22746
22747 filedata->file_size = (bfd_size_type) statbuf.st_size;
22748 filedata->is_separate = false;
22749
22750 if (memcmp (armag, ARMAG, SARMAG) == 0)
22751 {
22752 if (! process_archive (filedata, false))
22753 ret = false;
22754 }
22755 else if (memcmp (armag, ARMAGT, SARMAG) == 0)
22756 {
22757 if ( ! process_archive (filedata, true))
22758 ret = false;
22759 }
22760 else
22761 {
22762 if (do_archive_index && !check_all)
22763 error (_("File %s is not an archive so its index cannot be displayed.\n"),
22764 file_name);
22765
22766 rewind (filedata->handle);
22767 filedata->archive_file_size = filedata->archive_file_offset = 0;
22768
22769 if (! process_object (filedata))
22770 ret = false;
22771 }
22772
22773 fclose (filedata->handle);
22774 free (filedata->section_headers);
22775 free (filedata->program_headers);
22776 free (filedata->string_table);
22777 free (filedata->dump.dump_sects);
22778 free (filedata);
22779
22780 free (ba_cache.strtab);
22781 ba_cache.strtab = NULL;
22782 free (ba_cache.symtab);
22783 ba_cache.symtab = NULL;
22784 ba_cache.filedata = NULL;
22785
22786 return ret;
22787 }
22788
22789 #ifdef SUPPORT_DISASSEMBLY
22790 /* Needed by the i386 disassembler. For extra credit, someone could
22791 fix this so that we insert symbolic addresses here, esp for GOT/PLT
22792 symbols. */
22793
22794 void
22795 print_address (unsigned int addr, FILE * outfile)
22796 {
22797 fprintf (outfile,"0x%8.8x", addr);
22798 }
22799
22800 /* Needed by the i386 disassembler. */
22801
22802 void
22803 db_task_printsym (unsigned int addr)
22804 {
22805 print_address (addr, stderr);
22806 }
22807 #endif
22808
22809 int
22810 main (int argc, char ** argv)
22811 {
22812 int err;
22813
22814 #ifdef HAVE_LC_MESSAGES
22815 setlocale (LC_MESSAGES, "");
22816 #endif
22817 setlocale (LC_CTYPE, "");
22818 bindtextdomain (PACKAGE, LOCALEDIR);
22819 textdomain (PACKAGE);
22820
22821 expandargv (&argc, &argv);
22822
22823 parse_args (& cmdline, argc, argv);
22824
22825 if (optind < (argc - 1))
22826 /* When displaying information for more than one file,
22827 prefix the information with the file name. */
22828 show_name = true;
22829 else if (optind >= argc)
22830 {
22831 /* Ensure that the warning is always displayed. */
22832 do_checks = true;
22833
22834 warn (_("Nothing to do.\n"));
22835 usage (stderr);
22836 }
22837
22838 err = false;
22839 while (optind < argc)
22840 if (! process_file (argv[optind++]))
22841 err = true;
22842
22843 free (cmdline.dump_sects);
22844
22845 free (dump_ctf_symtab_name);
22846 free (dump_ctf_strtab_name);
22847 free (dump_ctf_parent_name);
22848
22849 return err ? EXIT_FAILURE : EXIT_SUCCESS;
22850 }