]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - binutils/readelf.c
arm: add armv9-a architecture to -march
[thirdparty/binutils-gdb.git] / binutils / readelf.c
1 /* readelf.c -- display contents of an ELF format file
2 Copyright (C) 1998-2021 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 __GNUC__ >= 2
50 /* Define BFD64 here, even if our default architecture is 32 bit ELF
51 as this will allow us to read in and parse 64bit and 32bit ELF files.
52 Only do this if we believe that the compiler can support a 64 bit
53 data type. For now we only rely on GCC being able to do this. */
54 #define BFD64
55 #endif
56
57 #include "bfd.h"
58 #include "bucomm.h"
59 #include "elfcomm.h"
60 #include "dwarf.h"
61 #include "ctf-api.h"
62 #include "demangle.h"
63
64 #include "elf/common.h"
65 #include "elf/external.h"
66 #include "elf/internal.h"
67
68
69 /* Included here, before RELOC_MACROS_GEN_FUNC is defined, so that
70 we can obtain the H8 reloc numbers. We need these for the
71 get_reloc_size() function. We include h8.h again after defining
72 RELOC_MACROS_GEN_FUNC so that we get the naming function as well. */
73
74 #include "elf/h8.h"
75 #undef _ELF_H8_H
76
77 /* Undo the effects of #including reloc-macros.h. */
78
79 #undef START_RELOC_NUMBERS
80 #undef RELOC_NUMBER
81 #undef FAKE_RELOC
82 #undef EMPTY_RELOC
83 #undef END_RELOC_NUMBERS
84 #undef _RELOC_MACROS_H
85
86 /* The following headers use the elf/reloc-macros.h file to
87 automatically generate relocation recognition functions
88 such as elf_mips_reloc_type() */
89
90 #define RELOC_MACROS_GEN_FUNC
91
92 #include "elf/aarch64.h"
93 #include "elf/alpha.h"
94 #include "elf/arc.h"
95 #include "elf/arm.h"
96 #include "elf/avr.h"
97 #include "elf/bfin.h"
98 #include "elf/cr16.h"
99 #include "elf/cris.h"
100 #include "elf/crx.h"
101 #include "elf/csky.h"
102 #include "elf/d10v.h"
103 #include "elf/d30v.h"
104 #include "elf/dlx.h"
105 #include "elf/bpf.h"
106 #include "elf/epiphany.h"
107 #include "elf/fr30.h"
108 #include "elf/frv.h"
109 #include "elf/ft32.h"
110 #include "elf/h8.h"
111 #include "elf/hppa.h"
112 #include "elf/i386.h"
113 #include "elf/i370.h"
114 #include "elf/i860.h"
115 #include "elf/i960.h"
116 #include "elf/ia64.h"
117 #include "elf/ip2k.h"
118 #include "elf/lm32.h"
119 #include "elf/iq2000.h"
120 #include "elf/m32c.h"
121 #include "elf/m32r.h"
122 #include "elf/m68k.h"
123 #include "elf/m68hc11.h"
124 #include "elf/s12z.h"
125 #include "elf/mcore.h"
126 #include "elf/mep.h"
127 #include "elf/metag.h"
128 #include "elf/microblaze.h"
129 #include "elf/mips.h"
130 #include "elf/mmix.h"
131 #include "elf/mn10200.h"
132 #include "elf/mn10300.h"
133 #include "elf/moxie.h"
134 #include "elf/mt.h"
135 #include "elf/msp430.h"
136 #include "elf/nds32.h"
137 #include "elf/nfp.h"
138 #include "elf/nios2.h"
139 #include "elf/or1k.h"
140 #include "elf/pj.h"
141 #include "elf/ppc.h"
142 #include "elf/ppc64.h"
143 #include "elf/pru.h"
144 #include "elf/riscv.h"
145 #include "elf/rl78.h"
146 #include "elf/rx.h"
147 #include "elf/s390.h"
148 #include "elf/score.h"
149 #include "elf/sh.h"
150 #include "elf/sparc.h"
151 #include "elf/spu.h"
152 #include "elf/tic6x.h"
153 #include "elf/tilegx.h"
154 #include "elf/tilepro.h"
155 #include "elf/v850.h"
156 #include "elf/vax.h"
157 #include "elf/visium.h"
158 #include "elf/wasm32.h"
159 #include "elf/x86-64.h"
160 #include "elf/xc16x.h"
161 #include "elf/xgate.h"
162 #include "elf/xstormy16.h"
163 #include "elf/xtensa.h"
164 #include "elf/z80.h"
165 #include "elf/loongarch.h"
166
167 #include "getopt.h"
168 #include "libiberty.h"
169 #include "safe-ctype.h"
170 #include "filenames.h"
171
172 #ifndef offsetof
173 #define offsetof(TYPE, MEMBER) ((size_t) &(((TYPE *) 0)->MEMBER))
174 #endif
175
176 typedef struct elf_section_list
177 {
178 Elf_Internal_Shdr * hdr;
179 struct elf_section_list * next;
180 } elf_section_list;
181
182 /* Flag bits indicating particular types of dump. */
183 #define HEX_DUMP (1 << 0) /* The -x command line switch. */
184 #define DISASS_DUMP (1 << 1) /* The -i command line switch. */
185 #define DEBUG_DUMP (1 << 2) /* The -w command line switch. */
186 #define STRING_DUMP (1 << 3) /* The -p command line switch. */
187 #define RELOC_DUMP (1 << 4) /* The -R command line switch. */
188 #define CTF_DUMP (1 << 5) /* The --ctf command line switch. */
189
190 typedef unsigned char dump_type;
191
192 /* A linked list of the section names for which dumps were requested. */
193 struct dump_list_entry
194 {
195 char * name;
196 dump_type type;
197 struct dump_list_entry * next;
198 };
199
200 /* A dynamic array of flags indicating for which sections a dump
201 has been requested via command line switches. */
202 struct dump_data
203 {
204 dump_type * dump_sects;
205 unsigned int num_dump_sects;
206 };
207
208 static struct dump_data cmdline;
209
210 static struct dump_list_entry * dump_sects_byname;
211
212 char * program_name = "readelf";
213
214 static bool show_name = false;
215 static bool do_dynamic = false;
216 static bool do_syms = false;
217 static bool do_dyn_syms = false;
218 static bool do_lto_syms = false;
219 static bool do_reloc = false;
220 static bool do_sections = false;
221 static bool do_section_groups = false;
222 static bool do_section_details = false;
223 static bool do_segments = false;
224 static bool do_unwind = false;
225 static bool do_using_dynamic = false;
226 static bool do_header = false;
227 static bool do_dump = false;
228 static bool do_version = false;
229 static bool do_histogram = false;
230 static bool do_debugging = false;
231 static bool do_ctf = false;
232 static bool do_arch = false;
233 static bool do_notes = false;
234 static bool do_archive_index = false;
235 static bool check_all = false;
236 static bool is_32bit_elf = false;
237 static bool decompress_dumps = false;
238 static bool do_not_show_symbol_truncation = false;
239 static bool do_demangle = false; /* Pretty print C++ symbol names. */
240 static bool process_links = false;
241 static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
242 static int sym_base = 0;
243
244 static char *dump_ctf_parent_name;
245 static char *dump_ctf_symtab_name;
246 static char *dump_ctf_strtab_name;
247
248 struct group_list
249 {
250 struct group_list * next;
251 unsigned int section_index;
252 };
253
254 struct group
255 {
256 struct group_list * root;
257 unsigned int group_index;
258 };
259
260 typedef struct filedata
261 {
262 const char * file_name;
263 bool is_separate;
264 FILE * handle;
265 bfd_size_type file_size;
266 Elf_Internal_Ehdr file_header;
267 unsigned long archive_file_offset;
268 unsigned long archive_file_size;
269 /* Everything below this point is cleared out by free_filedata. */
270 Elf_Internal_Shdr * section_headers;
271 Elf_Internal_Phdr * program_headers;
272 char * string_table;
273 unsigned long string_table_length;
274 unsigned long dynamic_addr;
275 bfd_size_type dynamic_size;
276 size_t dynamic_nent;
277 Elf_Internal_Dyn * dynamic_section;
278 Elf_Internal_Shdr * dynamic_strtab_section;
279 char * dynamic_strings;
280 unsigned long dynamic_strings_length;
281 Elf_Internal_Shdr * dynamic_symtab_section;
282 unsigned long num_dynamic_syms;
283 Elf_Internal_Sym * dynamic_symbols;
284 bfd_vma version_info[16];
285 unsigned int dynamic_syminfo_nent;
286 Elf_Internal_Syminfo * dynamic_syminfo;
287 unsigned long dynamic_syminfo_offset;
288 bfd_size_type nbuckets;
289 bfd_size_type nchains;
290 bfd_vma * buckets;
291 bfd_vma * chains;
292 bfd_size_type ngnubuckets;
293 bfd_size_type ngnuchains;
294 bfd_vma * gnubuckets;
295 bfd_vma * gnuchains;
296 bfd_vma * mipsxlat;
297 bfd_vma gnusymidx;
298 char * program_interpreter;
299 bfd_vma dynamic_info[DT_ENCODING];
300 bfd_vma dynamic_info_DT_GNU_HASH;
301 bfd_vma dynamic_info_DT_MIPS_XHASH;
302 elf_section_list * symtab_shndx_list;
303 size_t group_count;
304 struct group * section_groups;
305 struct group ** section_headers_groups;
306 /* A dynamic array of flags indicating for which sections a dump of
307 some kind has been requested. It is reset on a per-object file
308 basis and then initialised from the cmdline_dump_sects array,
309 the results of interpreting the -w switch, and the
310 dump_sects_byname list. */
311 struct dump_data dump;
312 } Filedata;
313
314 /* How to print a vma value. */
315 typedef enum print_mode
316 {
317 HEX,
318 HEX_5,
319 DEC,
320 DEC_5,
321 UNSIGNED,
322 UNSIGNED_5,
323 PREFIX_HEX,
324 PREFIX_HEX_5,
325 FULL_HEX,
326 LONG_HEX,
327 OCTAL,
328 OCTAL_5
329 }
330 print_mode;
331
332 /* Versioned symbol info. */
333 enum versioned_symbol_info
334 {
335 symbol_undefined,
336 symbol_hidden,
337 symbol_public
338 };
339
340 static const char * get_symbol_version_string
341 (Filedata *, bool, const char *, unsigned long, unsigned,
342 Elf_Internal_Sym *, enum versioned_symbol_info *, unsigned short *);
343
344 #define UNKNOWN -1
345
346 static inline const char *
347 section_name (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
348 {
349 return filedata->string_table + hdr->sh_name;
350 }
351
352 static inline bool
353 section_name_valid (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
354 {
355 return (hdr != NULL
356 && filedata->string_table != NULL
357 && hdr->sh_name < filedata->string_table_length);
358 }
359
360 static inline const char *
361 section_name_print (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
362 {
363 if (hdr == NULL)
364 return _("<none>");
365 if (filedata->string_table == NULL)
366 return _("<no-strings>");
367 if (hdr->sh_name >= filedata->string_table_length)
368 return _("<corrupt>");
369 return section_name (filedata, hdr);
370 }
371
372 #define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */
373
374 static inline bool
375 valid_symbol_name (const char *strtab, size_t strtab_size, uint64_t offset)
376 {
377 return strtab != NULL && offset < strtab_size;
378 }
379
380 static inline bool
381 valid_dynamic_name (const Filedata *filedata, uint64_t offset)
382 {
383 return valid_symbol_name (filedata->dynamic_strings,
384 filedata->dynamic_strings_length, offset);
385 }
386
387 /* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
388 already been called and verified that the string exists. */
389 static inline const char *
390 get_dynamic_name (const Filedata *filedata, size_t offset)
391 {
392 return filedata->dynamic_strings + offset;
393 }
394
395 #define REMOVE_ARCH_BITS(ADDR) \
396 do \
397 { \
398 if (filedata->file_header.e_machine == EM_ARM) \
399 (ADDR) &= ~1; \
400 } \
401 while (0)
402
403 /* Get the correct GNU hash section name. */
404 #define GNU_HASH_SECTION_NAME(filedata) \
405 filedata->dynamic_info_DT_MIPS_XHASH ? ".MIPS.xhash" : ".gnu.hash"
406 \f
407 /* Print a BFD_VMA to an internal buffer, for use in error messages.
408 BFD_FMA_FMT can't be used in translated strings. */
409
410 static const char *
411 bfd_vmatoa (char *fmtch, bfd_vma value)
412 {
413 /* bfd_vmatoa is used more then once in a printf call for output.
414 Cycle through an array of buffers. */
415 static int buf_pos = 0;
416 static struct bfd_vmatoa_buf
417 {
418 char place[64];
419 } buf[4];
420 char *ret;
421 char fmt[32];
422
423 ret = buf[buf_pos++].place;
424 buf_pos %= ARRAY_SIZE (buf);
425
426 sprintf (fmt, "%%%s%s", BFD_VMA_FMT, fmtch);
427 snprintf (ret, sizeof (buf[0].place), fmt, value);
428 return ret;
429 }
430
431 /* Retrieve NMEMB structures, each SIZE bytes long from FILEDATA starting at
432 OFFSET + the offset of the current archive member, if we are examining an
433 archive. Put the retrieved data into VAR, if it is not NULL. Otherwise
434 allocate a buffer using malloc and fill that. In either case return the
435 pointer to the start of the retrieved data or NULL if something went wrong.
436 If something does go wrong and REASON is not NULL then emit an error
437 message using REASON as part of the context. */
438
439 static void *
440 get_data (void * var,
441 Filedata * filedata,
442 unsigned long offset,
443 bfd_size_type size,
444 bfd_size_type nmemb,
445 const char * reason)
446 {
447 void * mvar;
448 bfd_size_type amt = size * nmemb;
449
450 if (size == 0 || nmemb == 0)
451 return NULL;
452
453 /* If the size_t type is smaller than the bfd_size_type, eg because
454 you are building a 32-bit tool on a 64-bit host, then make sure
455 that when the sizes are cast to (size_t) no information is lost. */
456 if ((size_t) size != size
457 || (size_t) nmemb != nmemb
458 || (size_t) amt != amt)
459 {
460 if (reason)
461 error (_("Size truncation prevents reading %s"
462 " elements of size %s for %s\n"),
463 bfd_vmatoa ("u", nmemb), bfd_vmatoa ("u", size), reason);
464 return NULL;
465 }
466
467 /* Check for size overflow. */
468 if (amt / size != nmemb || (size_t) amt + 1 == 0)
469 {
470 if (reason)
471 error (_("Size overflow prevents reading %s"
472 " elements of size %s for %s\n"),
473 bfd_vmatoa ("u", nmemb), bfd_vmatoa ("u", size), reason);
474 return NULL;
475 }
476
477 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
478 attempting to allocate memory when the read is bound to fail. */
479 if (filedata->archive_file_offset > filedata->file_size
480 || offset > filedata->file_size - filedata->archive_file_offset
481 || amt > filedata->file_size - filedata->archive_file_offset - offset)
482 {
483 if (reason)
484 error (_("Reading %s bytes extends past end of file for %s\n"),
485 bfd_vmatoa ("u", amt), reason);
486 return NULL;
487 }
488
489 if (fseek (filedata->handle, filedata->archive_file_offset + offset,
490 SEEK_SET))
491 {
492 if (reason)
493 error (_("Unable to seek to 0x%lx for %s\n"),
494 filedata->archive_file_offset + offset, reason);
495 return NULL;
496 }
497
498 mvar = var;
499 if (mvar == NULL)
500 {
501 /* + 1 so that we can '\0' terminate invalid string table sections. */
502 mvar = malloc ((size_t) amt + 1);
503
504 if (mvar == NULL)
505 {
506 if (reason)
507 error (_("Out of memory allocating %s bytes for %s\n"),
508 bfd_vmatoa ("u", amt), reason);
509 return NULL;
510 }
511
512 ((char *) mvar)[amt] = '\0';
513 }
514
515 if (fread (mvar, (size_t) size, (size_t) nmemb, filedata->handle) != nmemb)
516 {
517 if (reason)
518 error (_("Unable to read in %s bytes of %s\n"),
519 bfd_vmatoa ("u", amt), reason);
520 if (mvar != var)
521 free (mvar);
522 return NULL;
523 }
524
525 return mvar;
526 }
527
528 /* Print a VMA value in the MODE specified.
529 Returns the number of characters displayed. */
530
531 static unsigned int
532 print_vma (bfd_vma vma, print_mode mode)
533 {
534 unsigned int nc = 0;
535
536 switch (mode)
537 {
538 case FULL_HEX:
539 nc = printf ("0x");
540 /* Fall through. */
541 case LONG_HEX:
542 #ifdef BFD64
543 if (is_32bit_elf)
544 return nc + printf ("%8.8" BFD_VMA_FMT "x", vma);
545 #endif
546 printf_vma (vma);
547 return nc + 16;
548
549 case DEC_5:
550 if (vma <= 99999)
551 return printf ("%5" BFD_VMA_FMT "d", vma);
552 /* Fall through. */
553 case PREFIX_HEX:
554 nc = printf ("0x");
555 /* Fall through. */
556 case HEX:
557 return nc + printf ("%" BFD_VMA_FMT "x", vma);
558
559 case PREFIX_HEX_5:
560 nc = printf ("0x");
561 /* Fall through. */
562 case HEX_5:
563 return nc + printf ("%05" BFD_VMA_FMT "x", vma);
564
565 case DEC:
566 return printf ("%" BFD_VMA_FMT "d", vma);
567
568 case UNSIGNED:
569 return printf ("%" BFD_VMA_FMT "u", vma);
570
571 case UNSIGNED_5:
572 return printf ("%5" BFD_VMA_FMT "u", vma);
573
574 case OCTAL:
575 return printf ("%" BFD_VMA_FMT "o", vma);
576
577 case OCTAL_5:
578 return printf ("%5" BFD_VMA_FMT "o", vma);
579
580 default:
581 /* FIXME: Report unrecognised mode ? */
582 return 0;
583 }
584 }
585
586
587 /* Display a symbol on stdout. Handles the display of control characters and
588 multibye characters (assuming the host environment supports them).
589
590 Display at most abs(WIDTH) characters, truncating as necessary, unless do_wide is true.
591
592 If truncation will happen and do_not_show_symbol_truncation is FALSE then display
593 abs(WIDTH) - 5 characters followed by "[...]".
594
595 If WIDTH is negative then ensure that the output is at least (- WIDTH) characters,
596 padding as necessary.
597
598 Returns the number of emitted characters. */
599
600 static unsigned int
601 print_symbol (signed int width, const char * symbol)
602 {
603 bool extra_padding = false;
604 bool do_dots = false;
605 signed int num_printed = 0;
606 #ifdef HAVE_MBSTATE_T
607 mbstate_t state;
608 #endif
609 unsigned int width_remaining;
610 const void * alloced_symbol = NULL;
611
612 if (width < 0)
613 {
614 /* Keep the width positive. This helps the code below. */
615 width = - width;
616 extra_padding = true;
617 }
618 else if (width == 0)
619 return 0;
620
621 if (do_wide)
622 /* Set the remaining width to a very large value.
623 This simplifies the code below. */
624 width_remaining = INT_MAX;
625 else
626 {
627 width_remaining = width;
628 if (! do_not_show_symbol_truncation
629 && (int) strlen (symbol) > width)
630 {
631 width_remaining -= 5;
632 if ((int) width_remaining < 0)
633 width_remaining = 0;
634 do_dots = true;
635 }
636 }
637
638 #ifdef HAVE_MBSTATE_T
639 /* Initialise the multibyte conversion state. */
640 memset (& state, 0, sizeof (state));
641 #endif
642
643 if (do_demangle && *symbol)
644 {
645 const char * res = cplus_demangle (symbol, demangle_flags);
646
647 if (res != NULL)
648 alloced_symbol = symbol = res;
649 }
650
651 while (width_remaining)
652 {
653 size_t n;
654 const char c = *symbol++;
655
656 if (c == 0)
657 break;
658
659 /* Do not print control characters directly as they can affect terminal
660 settings. Such characters usually appear in the names generated
661 by the assembler for local labels. */
662 if (ISCNTRL (c))
663 {
664 if (width_remaining < 2)
665 break;
666
667 printf ("^%c", c + 0x40);
668 width_remaining -= 2;
669 num_printed += 2;
670 }
671 else if (ISPRINT (c))
672 {
673 putchar (c);
674 width_remaining --;
675 num_printed ++;
676 }
677 else
678 {
679 #ifdef HAVE_MBSTATE_T
680 wchar_t w;
681 #endif
682 /* Let printf do the hard work of displaying multibyte characters. */
683 printf ("%.1s", symbol - 1);
684 width_remaining --;
685 num_printed ++;
686
687 #ifdef HAVE_MBSTATE_T
688 /* Try to find out how many bytes made up the character that was
689 just printed. Advance the symbol pointer past the bytes that
690 were displayed. */
691 n = mbrtowc (& w, symbol - 1, MB_CUR_MAX, & state);
692 #else
693 n = 1;
694 #endif
695 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
696 symbol += (n - 1);
697 }
698 }
699
700 if (do_dots)
701 num_printed += printf ("[...]");
702
703 if (extra_padding && num_printed < width)
704 {
705 /* Fill in the remaining spaces. */
706 printf ("%-*s", width - num_printed, " ");
707 num_printed = width;
708 }
709
710 free ((void *) alloced_symbol);
711 return num_printed;
712 }
713
714 /* Returns a pointer to a static buffer containing a printable version of
715 the given section's name. Like print_symbol, except that it does not try
716 to print multibyte characters, it just interprets them as hex values. */
717
718 static const char *
719 printable_section_name (Filedata * filedata, const Elf_Internal_Shdr * sec)
720 {
721 #define MAX_PRINT_SEC_NAME_LEN 256
722 static char sec_name_buf [MAX_PRINT_SEC_NAME_LEN + 1];
723 const char * name = section_name_print (filedata, sec);
724 char * buf = sec_name_buf;
725 char c;
726 unsigned int remaining = MAX_PRINT_SEC_NAME_LEN;
727
728 while ((c = * name ++) != 0)
729 {
730 if (ISCNTRL (c))
731 {
732 if (remaining < 2)
733 break;
734
735 * buf ++ = '^';
736 * buf ++ = c + 0x40;
737 remaining -= 2;
738 }
739 else if (ISPRINT (c))
740 {
741 * buf ++ = c;
742 remaining -= 1;
743 }
744 else
745 {
746 static char hex[17] = "0123456789ABCDEF";
747
748 if (remaining < 4)
749 break;
750 * buf ++ = '<';
751 * buf ++ = hex[(c & 0xf0) >> 4];
752 * buf ++ = hex[c & 0x0f];
753 * buf ++ = '>';
754 remaining -= 4;
755 }
756
757 if (remaining == 0)
758 break;
759 }
760
761 * buf = 0;
762 return sec_name_buf;
763 }
764
765 static const char *
766 printable_section_name_from_index (Filedata * filedata, unsigned long ndx)
767 {
768 if (ndx >= filedata->file_header.e_shnum)
769 return _("<corrupt>");
770
771 return printable_section_name (filedata, filedata->section_headers + ndx);
772 }
773
774 /* Return a pointer to section NAME, or NULL if no such section exists. */
775
776 static Elf_Internal_Shdr *
777 find_section (Filedata * filedata, const char * name)
778 {
779 unsigned int i;
780
781 if (filedata->section_headers == NULL)
782 return NULL;
783
784 for (i = 0; i < filedata->file_header.e_shnum; i++)
785 if (section_name_valid (filedata, filedata->section_headers + i)
786 && streq (section_name (filedata, filedata->section_headers + i),
787 name))
788 return filedata->section_headers + i;
789
790 return NULL;
791 }
792
793 /* Return a pointer to a section containing ADDR, or NULL if no such
794 section exists. */
795
796 static Elf_Internal_Shdr *
797 find_section_by_address (Filedata * filedata, bfd_vma addr)
798 {
799 unsigned int i;
800
801 if (filedata->section_headers == NULL)
802 return NULL;
803
804 for (i = 0; i < filedata->file_header.e_shnum; i++)
805 {
806 Elf_Internal_Shdr *sec = filedata->section_headers + i;
807
808 if (addr >= sec->sh_addr && addr < sec->sh_addr + sec->sh_size)
809 return sec;
810 }
811
812 return NULL;
813 }
814
815 static Elf_Internal_Shdr *
816 find_section_by_type (Filedata * filedata, unsigned int type)
817 {
818 unsigned int i;
819
820 if (filedata->section_headers == NULL)
821 return NULL;
822
823 for (i = 0; i < filedata->file_header.e_shnum; i++)
824 {
825 Elf_Internal_Shdr *sec = filedata->section_headers + i;
826
827 if (sec->sh_type == type)
828 return sec;
829 }
830
831 return NULL;
832 }
833
834 /* Return a pointer to section NAME, or NULL if no such section exists,
835 restricted to the list of sections given in SET. */
836
837 static Elf_Internal_Shdr *
838 find_section_in_set (Filedata * filedata, const char * name, unsigned int * set)
839 {
840 unsigned int i;
841
842 if (filedata->section_headers == NULL)
843 return NULL;
844
845 if (set != NULL)
846 {
847 while ((i = *set++) > 0)
848 {
849 /* See PR 21156 for a reproducer. */
850 if (i >= filedata->file_header.e_shnum)
851 continue; /* FIXME: Should we issue an error message ? */
852
853 if (section_name_valid (filedata, filedata->section_headers + i)
854 && streq (section_name (filedata, filedata->section_headers + i),
855 name))
856 return filedata->section_headers + i;
857 }
858 }
859
860 return find_section (filedata, name);
861 }
862
863 /* Return TRUE if the current file is for IA-64 machine and OpenVMS ABI.
864 This OS has so many departures from the ELF standard that we test it at
865 many places. */
866
867 static inline bool
868 is_ia64_vms (Filedata * filedata)
869 {
870 return filedata->file_header.e_machine == EM_IA_64
871 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS;
872 }
873
874 /* Guess the relocation size commonly used by the specific machines. */
875
876 static bool
877 guess_is_rela (unsigned int e_machine)
878 {
879 switch (e_machine)
880 {
881 /* Targets that use REL relocations. */
882 case EM_386:
883 case EM_IAMCU:
884 case EM_960:
885 case EM_ARM:
886 case EM_D10V:
887 case EM_CYGNUS_D10V:
888 case EM_DLX:
889 case EM_MIPS:
890 case EM_MIPS_RS3_LE:
891 case EM_CYGNUS_M32R:
892 case EM_SCORE:
893 case EM_XGATE:
894 case EM_NFP:
895 case EM_BPF:
896 return false;
897
898 /* Targets that use RELA relocations. */
899 case EM_68K:
900 case EM_860:
901 case EM_AARCH64:
902 case EM_ADAPTEVA_EPIPHANY:
903 case EM_ALPHA:
904 case EM_ALTERA_NIOS2:
905 case EM_ARC:
906 case EM_ARC_COMPACT:
907 case EM_ARC_COMPACT2:
908 case EM_AVR:
909 case EM_AVR_OLD:
910 case EM_BLACKFIN:
911 case EM_CR16:
912 case EM_CRIS:
913 case EM_CRX:
914 case EM_CSKY:
915 case EM_D30V:
916 case EM_CYGNUS_D30V:
917 case EM_FR30:
918 case EM_FT32:
919 case EM_CYGNUS_FR30:
920 case EM_CYGNUS_FRV:
921 case EM_H8S:
922 case EM_H8_300:
923 case EM_H8_300H:
924 case EM_IA_64:
925 case EM_IP2K:
926 case EM_IP2K_OLD:
927 case EM_IQ2000:
928 case EM_LATTICEMICO32:
929 case EM_M32C_OLD:
930 case EM_M32C:
931 case EM_M32R:
932 case EM_MCORE:
933 case EM_CYGNUS_MEP:
934 case EM_METAG:
935 case EM_MMIX:
936 case EM_MN10200:
937 case EM_CYGNUS_MN10200:
938 case EM_MN10300:
939 case EM_CYGNUS_MN10300:
940 case EM_MOXIE:
941 case EM_MSP430:
942 case EM_MSP430_OLD:
943 case EM_MT:
944 case EM_NDS32:
945 case EM_NIOS32:
946 case EM_OR1K:
947 case EM_PPC64:
948 case EM_PPC:
949 case EM_TI_PRU:
950 case EM_RISCV:
951 case EM_RL78:
952 case EM_RX:
953 case EM_S390:
954 case EM_S390_OLD:
955 case EM_SH:
956 case EM_SPARC:
957 case EM_SPARC32PLUS:
958 case EM_SPARCV9:
959 case EM_SPU:
960 case EM_TI_C6000:
961 case EM_TILEGX:
962 case EM_TILEPRO:
963 case EM_V800:
964 case EM_V850:
965 case EM_CYGNUS_V850:
966 case EM_VAX:
967 case EM_VISIUM:
968 case EM_X86_64:
969 case EM_L1OM:
970 case EM_K1OM:
971 case EM_XSTORMY16:
972 case EM_XTENSA:
973 case EM_XTENSA_OLD:
974 case EM_MICROBLAZE:
975 case EM_MICROBLAZE_OLD:
976 case EM_WEBASSEMBLY:
977 return true;
978
979 case EM_68HC05:
980 case EM_68HC08:
981 case EM_68HC11:
982 case EM_68HC16:
983 case EM_FX66:
984 case EM_ME16:
985 case EM_MMA:
986 case EM_NCPU:
987 case EM_NDR1:
988 case EM_PCP:
989 case EM_ST100:
990 case EM_ST19:
991 case EM_ST7:
992 case EM_ST9PLUS:
993 case EM_STARCORE:
994 case EM_SVX:
995 case EM_TINYJ:
996 default:
997 warn (_("Don't know about relocations on this machine architecture\n"));
998 return false;
999 }
1000 }
1001
1002 /* Load RELA type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1003 Returns TRUE upon success, FALSE otherwise. If successful then a
1004 pointer to a malloc'ed buffer containing the relocs is placed in *RELASP,
1005 and the number of relocs loaded is placed in *NRELASP. It is the caller's
1006 responsibility to free the allocated buffer. */
1007
1008 static bool
1009 slurp_rela_relocs (Filedata * filedata,
1010 unsigned long rel_offset,
1011 unsigned long rel_size,
1012 Elf_Internal_Rela ** relasp,
1013 unsigned long * nrelasp)
1014 {
1015 Elf_Internal_Rela * relas;
1016 size_t nrelas;
1017 unsigned int i;
1018
1019 if (is_32bit_elf)
1020 {
1021 Elf32_External_Rela * erelas;
1022
1023 erelas = (Elf32_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1024 rel_size, _("32-bit relocation data"));
1025 if (!erelas)
1026 return false;
1027
1028 nrelas = rel_size / sizeof (Elf32_External_Rela);
1029
1030 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1031 sizeof (Elf_Internal_Rela));
1032
1033 if (relas == NULL)
1034 {
1035 free (erelas);
1036 error (_("out of memory parsing relocs\n"));
1037 return false;
1038 }
1039
1040 for (i = 0; i < nrelas; i++)
1041 {
1042 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1043 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1044 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1045 }
1046
1047 free (erelas);
1048 }
1049 else
1050 {
1051 Elf64_External_Rela * erelas;
1052
1053 erelas = (Elf64_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1054 rel_size, _("64-bit relocation data"));
1055 if (!erelas)
1056 return false;
1057
1058 nrelas = rel_size / sizeof (Elf64_External_Rela);
1059
1060 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1061 sizeof (Elf_Internal_Rela));
1062
1063 if (relas == NULL)
1064 {
1065 free (erelas);
1066 error (_("out of memory parsing relocs\n"));
1067 return false;
1068 }
1069
1070 for (i = 0; i < nrelas; i++)
1071 {
1072 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1073 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1074 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1075
1076 /* The #ifdef BFD64 below is to prevent a compile time
1077 warning. We know that if we do not have a 64 bit data
1078 type that we will never execute this code anyway. */
1079 #ifdef BFD64
1080 if (filedata->file_header.e_machine == EM_MIPS
1081 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1082 {
1083 /* In little-endian objects, r_info isn't really a
1084 64-bit little-endian value: it has a 32-bit
1085 little-endian symbol index followed by four
1086 individual byte fields. Reorder INFO
1087 accordingly. */
1088 bfd_vma inf = relas[i].r_info;
1089 inf = (((inf & 0xffffffff) << 32)
1090 | ((inf >> 56) & 0xff)
1091 | ((inf >> 40) & 0xff00)
1092 | ((inf >> 24) & 0xff0000)
1093 | ((inf >> 8) & 0xff000000));
1094 relas[i].r_info = inf;
1095 }
1096 #endif /* BFD64 */
1097 }
1098
1099 free (erelas);
1100 }
1101
1102 *relasp = relas;
1103 *nrelasp = nrelas;
1104 return true;
1105 }
1106
1107 /* Load REL type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1108 Returns TRUE upon success, FALSE otherwise. If successful then a
1109 pointer to a malloc'ed buffer containing the relocs is placed in *RELSP,
1110 and the number of relocs loaded is placed in *NRELSP. It is the caller's
1111 responsibility to free the allocated buffer. */
1112
1113 static bool
1114 slurp_rel_relocs (Filedata * filedata,
1115 unsigned long rel_offset,
1116 unsigned long rel_size,
1117 Elf_Internal_Rela ** relsp,
1118 unsigned long * nrelsp)
1119 {
1120 Elf_Internal_Rela * rels;
1121 size_t nrels;
1122 unsigned int i;
1123
1124 if (is_32bit_elf)
1125 {
1126 Elf32_External_Rel * erels;
1127
1128 erels = (Elf32_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1129 rel_size, _("32-bit relocation data"));
1130 if (!erels)
1131 return false;
1132
1133 nrels = rel_size / sizeof (Elf32_External_Rel);
1134
1135 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1136
1137 if (rels == NULL)
1138 {
1139 free (erels);
1140 error (_("out of memory parsing relocs\n"));
1141 return false;
1142 }
1143
1144 for (i = 0; i < nrels; i++)
1145 {
1146 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1147 rels[i].r_info = BYTE_GET (erels[i].r_info);
1148 rels[i].r_addend = 0;
1149 }
1150
1151 free (erels);
1152 }
1153 else
1154 {
1155 Elf64_External_Rel * erels;
1156
1157 erels = (Elf64_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1158 rel_size, _("64-bit relocation data"));
1159 if (!erels)
1160 return false;
1161
1162 nrels = rel_size / sizeof (Elf64_External_Rel);
1163
1164 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1165
1166 if (rels == NULL)
1167 {
1168 free (erels);
1169 error (_("out of memory parsing relocs\n"));
1170 return false;
1171 }
1172
1173 for (i = 0; i < nrels; i++)
1174 {
1175 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1176 rels[i].r_info = BYTE_GET (erels[i].r_info);
1177 rels[i].r_addend = 0;
1178
1179 /* The #ifdef BFD64 below is to prevent a compile time
1180 warning. We know that if we do not have a 64 bit data
1181 type that we will never execute this code anyway. */
1182 #ifdef BFD64
1183 if (filedata->file_header.e_machine == EM_MIPS
1184 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1185 {
1186 /* In little-endian objects, r_info isn't really a
1187 64-bit little-endian value: it has a 32-bit
1188 little-endian symbol index followed by four
1189 individual byte fields. Reorder INFO
1190 accordingly. */
1191 bfd_vma inf = rels[i].r_info;
1192 inf = (((inf & 0xffffffff) << 32)
1193 | ((inf >> 56) & 0xff)
1194 | ((inf >> 40) & 0xff00)
1195 | ((inf >> 24) & 0xff0000)
1196 | ((inf >> 8) & 0xff000000));
1197 rels[i].r_info = inf;
1198 }
1199 #endif /* BFD64 */
1200 }
1201
1202 free (erels);
1203 }
1204
1205 *relsp = rels;
1206 *nrelsp = nrels;
1207 return true;
1208 }
1209
1210 /* Returns the reloc type extracted from the reloc info field. */
1211
1212 static unsigned int
1213 get_reloc_type (Filedata * filedata, bfd_vma reloc_info)
1214 {
1215 if (is_32bit_elf)
1216 return ELF32_R_TYPE (reloc_info);
1217
1218 switch (filedata->file_header.e_machine)
1219 {
1220 case EM_MIPS:
1221 /* Note: We assume that reloc_info has already been adjusted for us. */
1222 return ELF64_MIPS_R_TYPE (reloc_info);
1223
1224 case EM_SPARCV9:
1225 return ELF64_R_TYPE_ID (reloc_info);
1226
1227 default:
1228 return ELF64_R_TYPE (reloc_info);
1229 }
1230 }
1231
1232 /* Return the symbol index extracted from the reloc info field. */
1233
1234 static bfd_vma
1235 get_reloc_symindex (bfd_vma reloc_info)
1236 {
1237 return is_32bit_elf ? ELF32_R_SYM (reloc_info) : ELF64_R_SYM (reloc_info);
1238 }
1239
1240 static inline bool
1241 uses_msp430x_relocs (Filedata * filedata)
1242 {
1243 return
1244 filedata->file_header.e_machine == EM_MSP430 /* Paranoia. */
1245 /* GCC uses osabi == ELFOSBI_STANDALONE. */
1246 && (((filedata->file_header.e_flags & EF_MSP430_MACH) == E_MSP430_MACH_MSP430X)
1247 /* TI compiler uses ELFOSABI_NONE. */
1248 || (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_NONE));
1249 }
1250
1251 /* Display the contents of the relocation data found at the specified
1252 offset. */
1253
1254 static bool
1255 dump_relocations (Filedata * filedata,
1256 unsigned long rel_offset,
1257 unsigned long rel_size,
1258 Elf_Internal_Sym * symtab,
1259 unsigned long nsyms,
1260 char * strtab,
1261 unsigned long strtablen,
1262 int is_rela,
1263 bool is_dynsym)
1264 {
1265 unsigned long i;
1266 Elf_Internal_Rela * rels;
1267 bool res = true;
1268
1269 if (is_rela == UNKNOWN)
1270 is_rela = guess_is_rela (filedata->file_header.e_machine);
1271
1272 if (is_rela)
1273 {
1274 if (!slurp_rela_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1275 return false;
1276 }
1277 else
1278 {
1279 if (!slurp_rel_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1280 return false;
1281 }
1282
1283 if (is_32bit_elf)
1284 {
1285 if (is_rela)
1286 {
1287 if (do_wide)
1288 printf (_(" Offset Info Type Sym. Value Symbol's Name + Addend\n"));
1289 else
1290 printf (_(" Offset Info Type Sym.Value Sym. Name + Addend\n"));
1291 }
1292 else
1293 {
1294 if (do_wide)
1295 printf (_(" Offset Info Type Sym. Value Symbol's Name\n"));
1296 else
1297 printf (_(" Offset Info Type Sym.Value Sym. Name\n"));
1298 }
1299 }
1300 else
1301 {
1302 if (is_rela)
1303 {
1304 if (do_wide)
1305 printf (_(" Offset Info Type Symbol's Value Symbol's Name + Addend\n"));
1306 else
1307 printf (_(" Offset Info Type Sym. Value Sym. Name + Addend\n"));
1308 }
1309 else
1310 {
1311 if (do_wide)
1312 printf (_(" Offset Info Type Symbol's Value Symbol's Name\n"));
1313 else
1314 printf (_(" Offset Info Type Sym. Value Sym. Name\n"));
1315 }
1316 }
1317
1318 for (i = 0; i < rel_size; i++)
1319 {
1320 const char * rtype;
1321 bfd_vma offset;
1322 bfd_vma inf;
1323 bfd_vma symtab_index;
1324 bfd_vma type;
1325
1326 offset = rels[i].r_offset;
1327 inf = rels[i].r_info;
1328
1329 type = get_reloc_type (filedata, inf);
1330 symtab_index = get_reloc_symindex (inf);
1331
1332 if (is_32bit_elf)
1333 {
1334 printf ("%8.8lx %8.8lx ",
1335 (unsigned long) offset & 0xffffffff,
1336 (unsigned long) inf & 0xffffffff);
1337 }
1338 else
1339 {
1340 printf (do_wide
1341 ? "%16.16" BFD_VMA_FMT "x %16.16" BFD_VMA_FMT "x "
1342 : "%12.12" BFD_VMA_FMT "x %12.12" BFD_VMA_FMT "x ",
1343 offset, inf);
1344 }
1345
1346 switch (filedata->file_header.e_machine)
1347 {
1348 default:
1349 rtype = NULL;
1350 break;
1351
1352 case EM_AARCH64:
1353 rtype = elf_aarch64_reloc_type (type);
1354 break;
1355
1356 case EM_M32R:
1357 case EM_CYGNUS_M32R:
1358 rtype = elf_m32r_reloc_type (type);
1359 break;
1360
1361 case EM_386:
1362 case EM_IAMCU:
1363 rtype = elf_i386_reloc_type (type);
1364 break;
1365
1366 case EM_68HC11:
1367 case EM_68HC12:
1368 rtype = elf_m68hc11_reloc_type (type);
1369 break;
1370
1371 case EM_S12Z:
1372 rtype = elf_s12z_reloc_type (type);
1373 break;
1374
1375 case EM_68K:
1376 rtype = elf_m68k_reloc_type (type);
1377 break;
1378
1379 case EM_960:
1380 rtype = elf_i960_reloc_type (type);
1381 break;
1382
1383 case EM_AVR:
1384 case EM_AVR_OLD:
1385 rtype = elf_avr_reloc_type (type);
1386 break;
1387
1388 case EM_OLD_SPARCV9:
1389 case EM_SPARC32PLUS:
1390 case EM_SPARCV9:
1391 case EM_SPARC:
1392 rtype = elf_sparc_reloc_type (type);
1393 break;
1394
1395 case EM_SPU:
1396 rtype = elf_spu_reloc_type (type);
1397 break;
1398
1399 case EM_V800:
1400 rtype = v800_reloc_type (type);
1401 break;
1402 case EM_V850:
1403 case EM_CYGNUS_V850:
1404 rtype = v850_reloc_type (type);
1405 break;
1406
1407 case EM_D10V:
1408 case EM_CYGNUS_D10V:
1409 rtype = elf_d10v_reloc_type (type);
1410 break;
1411
1412 case EM_D30V:
1413 case EM_CYGNUS_D30V:
1414 rtype = elf_d30v_reloc_type (type);
1415 break;
1416
1417 case EM_DLX:
1418 rtype = elf_dlx_reloc_type (type);
1419 break;
1420
1421 case EM_SH:
1422 rtype = elf_sh_reloc_type (type);
1423 break;
1424
1425 case EM_MN10300:
1426 case EM_CYGNUS_MN10300:
1427 rtype = elf_mn10300_reloc_type (type);
1428 break;
1429
1430 case EM_MN10200:
1431 case EM_CYGNUS_MN10200:
1432 rtype = elf_mn10200_reloc_type (type);
1433 break;
1434
1435 case EM_FR30:
1436 case EM_CYGNUS_FR30:
1437 rtype = elf_fr30_reloc_type (type);
1438 break;
1439
1440 case EM_CYGNUS_FRV:
1441 rtype = elf_frv_reloc_type (type);
1442 break;
1443
1444 case EM_CSKY:
1445 rtype = elf_csky_reloc_type (type);
1446 break;
1447
1448 case EM_FT32:
1449 rtype = elf_ft32_reloc_type (type);
1450 break;
1451
1452 case EM_MCORE:
1453 rtype = elf_mcore_reloc_type (type);
1454 break;
1455
1456 case EM_MMIX:
1457 rtype = elf_mmix_reloc_type (type);
1458 break;
1459
1460 case EM_MOXIE:
1461 rtype = elf_moxie_reloc_type (type);
1462 break;
1463
1464 case EM_MSP430:
1465 if (uses_msp430x_relocs (filedata))
1466 {
1467 rtype = elf_msp430x_reloc_type (type);
1468 break;
1469 }
1470 /* Fall through. */
1471 case EM_MSP430_OLD:
1472 rtype = elf_msp430_reloc_type (type);
1473 break;
1474
1475 case EM_NDS32:
1476 rtype = elf_nds32_reloc_type (type);
1477 break;
1478
1479 case EM_PPC:
1480 rtype = elf_ppc_reloc_type (type);
1481 break;
1482
1483 case EM_PPC64:
1484 rtype = elf_ppc64_reloc_type (type);
1485 break;
1486
1487 case EM_MIPS:
1488 case EM_MIPS_RS3_LE:
1489 rtype = elf_mips_reloc_type (type);
1490 break;
1491
1492 case EM_RISCV:
1493 rtype = elf_riscv_reloc_type (type);
1494 break;
1495
1496 case EM_ALPHA:
1497 rtype = elf_alpha_reloc_type (type);
1498 break;
1499
1500 case EM_ARM:
1501 rtype = elf_arm_reloc_type (type);
1502 break;
1503
1504 case EM_ARC:
1505 case EM_ARC_COMPACT:
1506 case EM_ARC_COMPACT2:
1507 rtype = elf_arc_reloc_type (type);
1508 break;
1509
1510 case EM_PARISC:
1511 rtype = elf_hppa_reloc_type (type);
1512 break;
1513
1514 case EM_H8_300:
1515 case EM_H8_300H:
1516 case EM_H8S:
1517 rtype = elf_h8_reloc_type (type);
1518 break;
1519
1520 case EM_OR1K:
1521 rtype = elf_or1k_reloc_type (type);
1522 break;
1523
1524 case EM_PJ:
1525 case EM_PJ_OLD:
1526 rtype = elf_pj_reloc_type (type);
1527 break;
1528 case EM_IA_64:
1529 rtype = elf_ia64_reloc_type (type);
1530 break;
1531
1532 case EM_CRIS:
1533 rtype = elf_cris_reloc_type (type);
1534 break;
1535
1536 case EM_860:
1537 rtype = elf_i860_reloc_type (type);
1538 break;
1539
1540 case EM_X86_64:
1541 case EM_L1OM:
1542 case EM_K1OM:
1543 rtype = elf_x86_64_reloc_type (type);
1544 break;
1545
1546 case EM_S370:
1547 rtype = i370_reloc_type (type);
1548 break;
1549
1550 case EM_S390_OLD:
1551 case EM_S390:
1552 rtype = elf_s390_reloc_type (type);
1553 break;
1554
1555 case EM_SCORE:
1556 rtype = elf_score_reloc_type (type);
1557 break;
1558
1559 case EM_XSTORMY16:
1560 rtype = elf_xstormy16_reloc_type (type);
1561 break;
1562
1563 case EM_CRX:
1564 rtype = elf_crx_reloc_type (type);
1565 break;
1566
1567 case EM_VAX:
1568 rtype = elf_vax_reloc_type (type);
1569 break;
1570
1571 case EM_VISIUM:
1572 rtype = elf_visium_reloc_type (type);
1573 break;
1574
1575 case EM_BPF:
1576 rtype = elf_bpf_reloc_type (type);
1577 break;
1578
1579 case EM_ADAPTEVA_EPIPHANY:
1580 rtype = elf_epiphany_reloc_type (type);
1581 break;
1582
1583 case EM_IP2K:
1584 case EM_IP2K_OLD:
1585 rtype = elf_ip2k_reloc_type (type);
1586 break;
1587
1588 case EM_IQ2000:
1589 rtype = elf_iq2000_reloc_type (type);
1590 break;
1591
1592 case EM_XTENSA_OLD:
1593 case EM_XTENSA:
1594 rtype = elf_xtensa_reloc_type (type);
1595 break;
1596
1597 case EM_LATTICEMICO32:
1598 rtype = elf_lm32_reloc_type (type);
1599 break;
1600
1601 case EM_M32C_OLD:
1602 case EM_M32C:
1603 rtype = elf_m32c_reloc_type (type);
1604 break;
1605
1606 case EM_MT:
1607 rtype = elf_mt_reloc_type (type);
1608 break;
1609
1610 case EM_BLACKFIN:
1611 rtype = elf_bfin_reloc_type (type);
1612 break;
1613
1614 case EM_CYGNUS_MEP:
1615 rtype = elf_mep_reloc_type (type);
1616 break;
1617
1618 case EM_CR16:
1619 rtype = elf_cr16_reloc_type (type);
1620 break;
1621
1622 case EM_MICROBLAZE:
1623 case EM_MICROBLAZE_OLD:
1624 rtype = elf_microblaze_reloc_type (type);
1625 break;
1626
1627 case EM_RL78:
1628 rtype = elf_rl78_reloc_type (type);
1629 break;
1630
1631 case EM_RX:
1632 rtype = elf_rx_reloc_type (type);
1633 break;
1634
1635 case EM_METAG:
1636 rtype = elf_metag_reloc_type (type);
1637 break;
1638
1639 case EM_XC16X:
1640 case EM_C166:
1641 rtype = elf_xc16x_reloc_type (type);
1642 break;
1643
1644 case EM_TI_C6000:
1645 rtype = elf_tic6x_reloc_type (type);
1646 break;
1647
1648 case EM_TILEGX:
1649 rtype = elf_tilegx_reloc_type (type);
1650 break;
1651
1652 case EM_TILEPRO:
1653 rtype = elf_tilepro_reloc_type (type);
1654 break;
1655
1656 case EM_WEBASSEMBLY:
1657 rtype = elf_wasm32_reloc_type (type);
1658 break;
1659
1660 case EM_XGATE:
1661 rtype = elf_xgate_reloc_type (type);
1662 break;
1663
1664 case EM_ALTERA_NIOS2:
1665 rtype = elf_nios2_reloc_type (type);
1666 break;
1667
1668 case EM_TI_PRU:
1669 rtype = elf_pru_reloc_type (type);
1670 break;
1671
1672 case EM_NFP:
1673 if (EF_NFP_MACH (filedata->file_header.e_flags) == E_NFP_MACH_3200)
1674 rtype = elf_nfp3200_reloc_type (type);
1675 else
1676 rtype = elf_nfp_reloc_type (type);
1677 break;
1678
1679 case EM_Z80:
1680 rtype = elf_z80_reloc_type (type);
1681 break;
1682
1683 case EM_LOONGARCH:
1684 rtype = elf_loongarch_reloc_type (type);
1685 break;
1686
1687 }
1688
1689 if (rtype == NULL)
1690 printf (_("unrecognized: %-7lx"), (unsigned long) type & 0xffffffff);
1691 else
1692 printf (do_wide ? "%-22s" : "%-17.17s", rtype);
1693
1694 if (filedata->file_header.e_machine == EM_ALPHA
1695 && rtype != NULL
1696 && streq (rtype, "R_ALPHA_LITUSE")
1697 && is_rela)
1698 {
1699 switch (rels[i].r_addend)
1700 {
1701 case LITUSE_ALPHA_ADDR: rtype = "ADDR"; break;
1702 case LITUSE_ALPHA_BASE: rtype = "BASE"; break;
1703 case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
1704 case LITUSE_ALPHA_JSR: rtype = "JSR"; break;
1705 case LITUSE_ALPHA_TLSGD: rtype = "TLSGD"; break;
1706 case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
1707 case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
1708 default: rtype = NULL;
1709 }
1710
1711 if (rtype)
1712 printf (" (%s)", rtype);
1713 else
1714 {
1715 putchar (' ');
1716 printf (_("<unknown addend: %lx>"),
1717 (unsigned long) rels[i].r_addend);
1718 res = false;
1719 }
1720 }
1721 else if (symtab_index)
1722 {
1723 if (symtab == NULL || symtab_index >= nsyms)
1724 {
1725 error (_(" bad symbol index: %08lx in reloc\n"),
1726 (unsigned long) symtab_index);
1727 res = false;
1728 }
1729 else
1730 {
1731 Elf_Internal_Sym * psym;
1732 const char * version_string;
1733 enum versioned_symbol_info sym_info;
1734 unsigned short vna_other;
1735
1736 psym = symtab + symtab_index;
1737
1738 version_string
1739 = get_symbol_version_string (filedata, is_dynsym,
1740 strtab, strtablen,
1741 symtab_index,
1742 psym,
1743 &sym_info,
1744 &vna_other);
1745
1746 printf (" ");
1747
1748 if (ELF_ST_TYPE (psym->st_info) == STT_GNU_IFUNC)
1749 {
1750 const char * name;
1751 unsigned int len;
1752 unsigned int width = is_32bit_elf ? 8 : 14;
1753
1754 /* Relocations against GNU_IFUNC symbols do not use the value
1755 of the symbol as the address to relocate against. Instead
1756 they invoke the function named by the symbol and use its
1757 result as the address for relocation.
1758
1759 To indicate this to the user, do not display the value of
1760 the symbol in the "Symbols's Value" field. Instead show
1761 its name followed by () as a hint that the symbol is
1762 invoked. */
1763
1764 if (strtab == NULL
1765 || psym->st_name == 0
1766 || psym->st_name >= strtablen)
1767 name = "??";
1768 else
1769 name = strtab + psym->st_name;
1770
1771 len = print_symbol (width, name);
1772 if (version_string)
1773 printf (sym_info == symbol_public ? "@@%s" : "@%s",
1774 version_string);
1775 printf ("()%-*s", len <= width ? (width + 1) - len : 1, " ");
1776 }
1777 else
1778 {
1779 print_vma (psym->st_value, LONG_HEX);
1780
1781 printf (is_32bit_elf ? " " : " ");
1782 }
1783
1784 if (psym->st_name == 0)
1785 {
1786 const char * sec_name = "<null>";
1787 char name_buf[40];
1788
1789 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
1790 {
1791 if (psym->st_shndx < filedata->file_header.e_shnum)
1792 sec_name = section_name_print (filedata,
1793 filedata->section_headers
1794 + psym->st_shndx);
1795 else if (psym->st_shndx == SHN_ABS)
1796 sec_name = "ABS";
1797 else if (psym->st_shndx == SHN_COMMON)
1798 sec_name = "COMMON";
1799 else if ((filedata->file_header.e_machine == EM_MIPS
1800 && psym->st_shndx == SHN_MIPS_SCOMMON)
1801 || (filedata->file_header.e_machine == EM_TI_C6000
1802 && psym->st_shndx == SHN_TIC6X_SCOMMON))
1803 sec_name = "SCOMMON";
1804 else if (filedata->file_header.e_machine == EM_MIPS
1805 && psym->st_shndx == SHN_MIPS_SUNDEFINED)
1806 sec_name = "SUNDEF";
1807 else if ((filedata->file_header.e_machine == EM_X86_64
1808 || filedata->file_header.e_machine == EM_L1OM
1809 || filedata->file_header.e_machine == EM_K1OM)
1810 && psym->st_shndx == SHN_X86_64_LCOMMON)
1811 sec_name = "LARGE_COMMON";
1812 else if (filedata->file_header.e_machine == EM_IA_64
1813 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
1814 && psym->st_shndx == SHN_IA_64_ANSI_COMMON)
1815 sec_name = "ANSI_COM";
1816 else if (is_ia64_vms (filedata)
1817 && psym->st_shndx == SHN_IA_64_VMS_SYMVEC)
1818 sec_name = "VMS_SYMVEC";
1819 else
1820 {
1821 sprintf (name_buf, "<section 0x%x>",
1822 (unsigned int) psym->st_shndx);
1823 sec_name = name_buf;
1824 }
1825 }
1826 print_symbol (22, sec_name);
1827 }
1828 else if (strtab == NULL)
1829 printf (_("<string table index: %3ld>"), psym->st_name);
1830 else if (psym->st_name >= strtablen)
1831 {
1832 error (_("<corrupt string table index: %3ld>\n"),
1833 psym->st_name);
1834 res = false;
1835 }
1836 else
1837 {
1838 print_symbol (22, strtab + psym->st_name);
1839 if (version_string)
1840 printf (sym_info == symbol_public ? "@@%s" : "@%s",
1841 version_string);
1842 }
1843
1844 if (is_rela)
1845 {
1846 bfd_vma off = rels[i].r_addend;
1847
1848 if ((bfd_signed_vma) off < 0)
1849 printf (" - %" BFD_VMA_FMT "x", - off);
1850 else
1851 printf (" + %" BFD_VMA_FMT "x", off);
1852 }
1853 }
1854 }
1855 else if (is_rela)
1856 {
1857 bfd_vma off = rels[i].r_addend;
1858
1859 printf ("%*c", is_32bit_elf ? 12 : 20, ' ');
1860 if ((bfd_signed_vma) off < 0)
1861 printf ("-%" BFD_VMA_FMT "x", - off);
1862 else
1863 printf ("%" BFD_VMA_FMT "x", off);
1864 }
1865
1866 if (filedata->file_header.e_machine == EM_SPARCV9
1867 && rtype != NULL
1868 && streq (rtype, "R_SPARC_OLO10"))
1869 printf (" + %lx", (unsigned long) ELF64_R_TYPE_DATA (inf));
1870
1871 putchar ('\n');
1872
1873 #ifdef BFD64
1874 if (! is_32bit_elf && filedata->file_header.e_machine == EM_MIPS)
1875 {
1876 bfd_vma type2 = ELF64_MIPS_R_TYPE2 (inf);
1877 bfd_vma type3 = ELF64_MIPS_R_TYPE3 (inf);
1878 const char * rtype2 = elf_mips_reloc_type (type2);
1879 const char * rtype3 = elf_mips_reloc_type (type3);
1880
1881 printf (" Type2: ");
1882
1883 if (rtype2 == NULL)
1884 printf (_("unrecognized: %-7lx"),
1885 (unsigned long) type2 & 0xffffffff);
1886 else
1887 printf ("%-17.17s", rtype2);
1888
1889 printf ("\n Type3: ");
1890
1891 if (rtype3 == NULL)
1892 printf (_("unrecognized: %-7lx"),
1893 (unsigned long) type3 & 0xffffffff);
1894 else
1895 printf ("%-17.17s", rtype3);
1896
1897 putchar ('\n');
1898 }
1899 #endif /* BFD64 */
1900 }
1901
1902 free (rels);
1903
1904 return res;
1905 }
1906
1907 static const char *
1908 get_aarch64_dynamic_type (unsigned long type)
1909 {
1910 switch (type)
1911 {
1912 case DT_AARCH64_BTI_PLT: return "AARCH64_BTI_PLT";
1913 case DT_AARCH64_PAC_PLT: return "AARCH64_PAC_PLT";
1914 case DT_AARCH64_VARIANT_PCS: return "AARCH64_VARIANT_PCS";
1915 default:
1916 return NULL;
1917 }
1918 }
1919
1920 static const char *
1921 get_mips_dynamic_type (unsigned long type)
1922 {
1923 switch (type)
1924 {
1925 case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
1926 case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
1927 case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
1928 case DT_MIPS_IVERSION: return "MIPS_IVERSION";
1929 case DT_MIPS_FLAGS: return "MIPS_FLAGS";
1930 case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
1931 case DT_MIPS_MSYM: return "MIPS_MSYM";
1932 case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
1933 case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
1934 case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
1935 case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
1936 case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
1937 case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
1938 case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
1939 case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
1940 case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
1941 case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
1942 case DT_MIPS_RLD_MAP_REL: return "MIPS_RLD_MAP_REL";
1943 case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
1944 case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
1945 case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
1946 case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
1947 case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
1948 case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
1949 case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
1950 case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
1951 case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
1952 case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
1953 case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
1954 case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
1955 case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
1956 case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
1957 case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
1958 case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
1959 case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
1960 case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
1961 case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
1962 case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
1963 case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
1964 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
1965 case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
1966 case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
1967 case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
1968 case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
1969 case DT_MIPS_PLTGOT: return "MIPS_PLTGOT";
1970 case DT_MIPS_RWPLT: return "MIPS_RWPLT";
1971 case DT_MIPS_XHASH: return "MIPS_XHASH";
1972 default:
1973 return NULL;
1974 }
1975 }
1976
1977 static const char *
1978 get_sparc64_dynamic_type (unsigned long type)
1979 {
1980 switch (type)
1981 {
1982 case DT_SPARC_REGISTER: return "SPARC_REGISTER";
1983 default:
1984 return NULL;
1985 }
1986 }
1987
1988 static const char *
1989 get_ppc_dynamic_type (unsigned long type)
1990 {
1991 switch (type)
1992 {
1993 case DT_PPC_GOT: return "PPC_GOT";
1994 case DT_PPC_OPT: return "PPC_OPT";
1995 default:
1996 return NULL;
1997 }
1998 }
1999
2000 static const char *
2001 get_ppc64_dynamic_type (unsigned long type)
2002 {
2003 switch (type)
2004 {
2005 case DT_PPC64_GLINK: return "PPC64_GLINK";
2006 case DT_PPC64_OPD: return "PPC64_OPD";
2007 case DT_PPC64_OPDSZ: return "PPC64_OPDSZ";
2008 case DT_PPC64_OPT: return "PPC64_OPT";
2009 default:
2010 return NULL;
2011 }
2012 }
2013
2014 static const char *
2015 get_parisc_dynamic_type (unsigned long type)
2016 {
2017 switch (type)
2018 {
2019 case DT_HP_LOAD_MAP: return "HP_LOAD_MAP";
2020 case DT_HP_DLD_FLAGS: return "HP_DLD_FLAGS";
2021 case DT_HP_DLD_HOOK: return "HP_DLD_HOOK";
2022 case DT_HP_UX10_INIT: return "HP_UX10_INIT";
2023 case DT_HP_UX10_INITSZ: return "HP_UX10_INITSZ";
2024 case DT_HP_PREINIT: return "HP_PREINIT";
2025 case DT_HP_PREINITSZ: return "HP_PREINITSZ";
2026 case DT_HP_NEEDED: return "HP_NEEDED";
2027 case DT_HP_TIME_STAMP: return "HP_TIME_STAMP";
2028 case DT_HP_CHECKSUM: return "HP_CHECKSUM";
2029 case DT_HP_GST_SIZE: return "HP_GST_SIZE";
2030 case DT_HP_GST_VERSION: return "HP_GST_VERSION";
2031 case DT_HP_GST_HASHVAL: return "HP_GST_HASHVAL";
2032 case DT_HP_EPLTREL: return "HP_GST_EPLTREL";
2033 case DT_HP_EPLTRELSZ: return "HP_GST_EPLTRELSZ";
2034 case DT_HP_FILTERED: return "HP_FILTERED";
2035 case DT_HP_FILTER_TLS: return "HP_FILTER_TLS";
2036 case DT_HP_COMPAT_FILTERED: return "HP_COMPAT_FILTERED";
2037 case DT_HP_LAZYLOAD: return "HP_LAZYLOAD";
2038 case DT_HP_BIND_NOW_COUNT: return "HP_BIND_NOW_COUNT";
2039 case DT_PLT: return "PLT";
2040 case DT_PLT_SIZE: return "PLT_SIZE";
2041 case DT_DLT: return "DLT";
2042 case DT_DLT_SIZE: return "DLT_SIZE";
2043 default:
2044 return NULL;
2045 }
2046 }
2047
2048 static const char *
2049 get_ia64_dynamic_type (unsigned long type)
2050 {
2051 switch (type)
2052 {
2053 case DT_IA_64_PLT_RESERVE: return "IA_64_PLT_RESERVE";
2054 case DT_IA_64_VMS_SUBTYPE: return "VMS_SUBTYPE";
2055 case DT_IA_64_VMS_IMGIOCNT: return "VMS_IMGIOCNT";
2056 case DT_IA_64_VMS_LNKFLAGS: return "VMS_LNKFLAGS";
2057 case DT_IA_64_VMS_VIR_MEM_BLK_SIZ: return "VMS_VIR_MEM_BLK_SIZ";
2058 case DT_IA_64_VMS_IDENT: return "VMS_IDENT";
2059 case DT_IA_64_VMS_NEEDED_IDENT: return "VMS_NEEDED_IDENT";
2060 case DT_IA_64_VMS_IMG_RELA_CNT: return "VMS_IMG_RELA_CNT";
2061 case DT_IA_64_VMS_SEG_RELA_CNT: return "VMS_SEG_RELA_CNT";
2062 case DT_IA_64_VMS_FIXUP_RELA_CNT: return "VMS_FIXUP_RELA_CNT";
2063 case DT_IA_64_VMS_FIXUP_NEEDED: return "VMS_FIXUP_NEEDED";
2064 case DT_IA_64_VMS_SYMVEC_CNT: return "VMS_SYMVEC_CNT";
2065 case DT_IA_64_VMS_XLATED: return "VMS_XLATED";
2066 case DT_IA_64_VMS_STACKSIZE: return "VMS_STACKSIZE";
2067 case DT_IA_64_VMS_UNWINDSZ: return "VMS_UNWINDSZ";
2068 case DT_IA_64_VMS_UNWIND_CODSEG: return "VMS_UNWIND_CODSEG";
2069 case DT_IA_64_VMS_UNWIND_INFOSEG: return "VMS_UNWIND_INFOSEG";
2070 case DT_IA_64_VMS_LINKTIME: return "VMS_LINKTIME";
2071 case DT_IA_64_VMS_SEG_NO: return "VMS_SEG_NO";
2072 case DT_IA_64_VMS_SYMVEC_OFFSET: return "VMS_SYMVEC_OFFSET";
2073 case DT_IA_64_VMS_SYMVEC_SEG: return "VMS_SYMVEC_SEG";
2074 case DT_IA_64_VMS_UNWIND_OFFSET: return "VMS_UNWIND_OFFSET";
2075 case DT_IA_64_VMS_UNWIND_SEG: return "VMS_UNWIND_SEG";
2076 case DT_IA_64_VMS_STRTAB_OFFSET: return "VMS_STRTAB_OFFSET";
2077 case DT_IA_64_VMS_SYSVER_OFFSET: return "VMS_SYSVER_OFFSET";
2078 case DT_IA_64_VMS_IMG_RELA_OFF: return "VMS_IMG_RELA_OFF";
2079 case DT_IA_64_VMS_SEG_RELA_OFF: return "VMS_SEG_RELA_OFF";
2080 case DT_IA_64_VMS_FIXUP_RELA_OFF: return "VMS_FIXUP_RELA_OFF";
2081 case DT_IA_64_VMS_PLTGOT_OFFSET: return "VMS_PLTGOT_OFFSET";
2082 case DT_IA_64_VMS_PLTGOT_SEG: return "VMS_PLTGOT_SEG";
2083 case DT_IA_64_VMS_FPMODE: return "VMS_FPMODE";
2084 default:
2085 return NULL;
2086 }
2087 }
2088
2089 static const char *
2090 get_solaris_section_type (unsigned long type)
2091 {
2092 switch (type)
2093 {
2094 case 0x6fffffee: return "SUNW_ancillary";
2095 case 0x6fffffef: return "SUNW_capchain";
2096 case 0x6ffffff0: return "SUNW_capinfo";
2097 case 0x6ffffff1: return "SUNW_symsort";
2098 case 0x6ffffff2: return "SUNW_tlssort";
2099 case 0x6ffffff3: return "SUNW_LDYNSYM";
2100 case 0x6ffffff4: return "SUNW_dof";
2101 case 0x6ffffff5: return "SUNW_cap";
2102 case 0x6ffffff6: return "SUNW_SIGNATURE";
2103 case 0x6ffffff7: return "SUNW_ANNOTATE";
2104 case 0x6ffffff8: return "SUNW_DEBUGSTR";
2105 case 0x6ffffff9: return "SUNW_DEBUG";
2106 case 0x6ffffffa: return "SUNW_move";
2107 case 0x6ffffffb: return "SUNW_COMDAT";
2108 case 0x6ffffffc: return "SUNW_syminfo";
2109 case 0x6ffffffd: return "SUNW_verdef";
2110 case 0x6ffffffe: return "SUNW_verneed";
2111 case 0x6fffffff: return "SUNW_versym";
2112 case 0x70000000: return "SPARC_GOTDATA";
2113 default: return NULL;
2114 }
2115 }
2116
2117 static const char *
2118 get_alpha_dynamic_type (unsigned long type)
2119 {
2120 switch (type)
2121 {
2122 case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
2123 default: return NULL;
2124 }
2125 }
2126
2127 static const char *
2128 get_score_dynamic_type (unsigned long type)
2129 {
2130 switch (type)
2131 {
2132 case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
2133 case DT_SCORE_LOCAL_GOTNO: return "SCORE_LOCAL_GOTNO";
2134 case DT_SCORE_SYMTABNO: return "SCORE_SYMTABNO";
2135 case DT_SCORE_GOTSYM: return "SCORE_GOTSYM";
2136 case DT_SCORE_UNREFEXTNO: return "SCORE_UNREFEXTNO";
2137 case DT_SCORE_HIPAGENO: return "SCORE_HIPAGENO";
2138 default: return NULL;
2139 }
2140 }
2141
2142 static const char *
2143 get_tic6x_dynamic_type (unsigned long type)
2144 {
2145 switch (type)
2146 {
2147 case DT_C6000_GSYM_OFFSET: return "C6000_GSYM_OFFSET";
2148 case DT_C6000_GSTR_OFFSET: return "C6000_GSTR_OFFSET";
2149 case DT_C6000_DSBT_BASE: return "C6000_DSBT_BASE";
2150 case DT_C6000_DSBT_SIZE: return "C6000_DSBT_SIZE";
2151 case DT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
2152 case DT_C6000_DSBT_INDEX: return "C6000_DSBT_INDEX";
2153 default: return NULL;
2154 }
2155 }
2156
2157 static const char *
2158 get_nios2_dynamic_type (unsigned long type)
2159 {
2160 switch (type)
2161 {
2162 case DT_NIOS2_GP: return "NIOS2_GP";
2163 default: return NULL;
2164 }
2165 }
2166
2167 static const char *
2168 get_solaris_dynamic_type (unsigned long type)
2169 {
2170 switch (type)
2171 {
2172 case 0x6000000d: return "SUNW_AUXILIARY";
2173 case 0x6000000e: return "SUNW_RTLDINF";
2174 case 0x6000000f: return "SUNW_FILTER";
2175 case 0x60000010: return "SUNW_CAP";
2176 case 0x60000011: return "SUNW_SYMTAB";
2177 case 0x60000012: return "SUNW_SYMSZ";
2178 case 0x60000013: return "SUNW_SORTENT";
2179 case 0x60000014: return "SUNW_SYMSORT";
2180 case 0x60000015: return "SUNW_SYMSORTSZ";
2181 case 0x60000016: return "SUNW_TLSSORT";
2182 case 0x60000017: return "SUNW_TLSSORTSZ";
2183 case 0x60000018: return "SUNW_CAPINFO";
2184 case 0x60000019: return "SUNW_STRPAD";
2185 case 0x6000001a: return "SUNW_CAPCHAIN";
2186 case 0x6000001b: return "SUNW_LDMACH";
2187 case 0x6000001d: return "SUNW_CAPCHAINENT";
2188 case 0x6000001f: return "SUNW_CAPCHAINSZ";
2189 case 0x60000021: return "SUNW_PARENT";
2190 case 0x60000023: return "SUNW_ASLR";
2191 case 0x60000025: return "SUNW_RELAX";
2192 case 0x60000029: return "SUNW_NXHEAP";
2193 case 0x6000002b: return "SUNW_NXSTACK";
2194
2195 case 0x70000001: return "SPARC_REGISTER";
2196 case 0x7ffffffd: return "AUXILIARY";
2197 case 0x7ffffffe: return "USED";
2198 case 0x7fffffff: return "FILTER";
2199
2200 default: return NULL;
2201 }
2202 }
2203
2204 static const char *
2205 get_dynamic_type (Filedata * filedata, unsigned long type)
2206 {
2207 static char buff[64];
2208
2209 switch (type)
2210 {
2211 case DT_NULL: return "NULL";
2212 case DT_NEEDED: return "NEEDED";
2213 case DT_PLTRELSZ: return "PLTRELSZ";
2214 case DT_PLTGOT: return "PLTGOT";
2215 case DT_HASH: return "HASH";
2216 case DT_STRTAB: return "STRTAB";
2217 case DT_SYMTAB: return "SYMTAB";
2218 case DT_RELA: return "RELA";
2219 case DT_RELASZ: return "RELASZ";
2220 case DT_RELAENT: return "RELAENT";
2221 case DT_STRSZ: return "STRSZ";
2222 case DT_SYMENT: return "SYMENT";
2223 case DT_INIT: return "INIT";
2224 case DT_FINI: return "FINI";
2225 case DT_SONAME: return "SONAME";
2226 case DT_RPATH: return "RPATH";
2227 case DT_SYMBOLIC: return "SYMBOLIC";
2228 case DT_REL: return "REL";
2229 case DT_RELSZ: return "RELSZ";
2230 case DT_RELENT: return "RELENT";
2231 case DT_PLTREL: return "PLTREL";
2232 case DT_DEBUG: return "DEBUG";
2233 case DT_TEXTREL: return "TEXTREL";
2234 case DT_JMPREL: return "JMPREL";
2235 case DT_BIND_NOW: return "BIND_NOW";
2236 case DT_INIT_ARRAY: return "INIT_ARRAY";
2237 case DT_FINI_ARRAY: return "FINI_ARRAY";
2238 case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
2239 case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
2240 case DT_RUNPATH: return "RUNPATH";
2241 case DT_FLAGS: return "FLAGS";
2242
2243 case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
2244 case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
2245 case DT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
2246
2247 case DT_CHECKSUM: return "CHECKSUM";
2248 case DT_PLTPADSZ: return "PLTPADSZ";
2249 case DT_MOVEENT: return "MOVEENT";
2250 case DT_MOVESZ: return "MOVESZ";
2251 case DT_FEATURE: return "FEATURE";
2252 case DT_POSFLAG_1: return "POSFLAG_1";
2253 case DT_SYMINSZ: return "SYMINSZ";
2254 case DT_SYMINENT: return "SYMINENT"; /* aka VALRNGHI */
2255
2256 case DT_ADDRRNGLO: return "ADDRRNGLO";
2257 case DT_CONFIG: return "CONFIG";
2258 case DT_DEPAUDIT: return "DEPAUDIT";
2259 case DT_AUDIT: return "AUDIT";
2260 case DT_PLTPAD: return "PLTPAD";
2261 case DT_MOVETAB: return "MOVETAB";
2262 case DT_SYMINFO: return "SYMINFO"; /* aka ADDRRNGHI */
2263
2264 case DT_VERSYM: return "VERSYM";
2265
2266 case DT_TLSDESC_GOT: return "TLSDESC_GOT";
2267 case DT_TLSDESC_PLT: return "TLSDESC_PLT";
2268 case DT_RELACOUNT: return "RELACOUNT";
2269 case DT_RELCOUNT: return "RELCOUNT";
2270 case DT_FLAGS_1: return "FLAGS_1";
2271 case DT_VERDEF: return "VERDEF";
2272 case DT_VERDEFNUM: return "VERDEFNUM";
2273 case DT_VERNEED: return "VERNEED";
2274 case DT_VERNEEDNUM: return "VERNEEDNUM";
2275
2276 case DT_AUXILIARY: return "AUXILIARY";
2277 case DT_USED: return "USED";
2278 case DT_FILTER: return "FILTER";
2279
2280 case DT_GNU_PRELINKED: return "GNU_PRELINKED";
2281 case DT_GNU_CONFLICT: return "GNU_CONFLICT";
2282 case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
2283 case DT_GNU_LIBLIST: return "GNU_LIBLIST";
2284 case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
2285 case DT_GNU_HASH: return "GNU_HASH";
2286 case DT_GNU_FLAGS_1: return "GNU_FLAGS_1";
2287
2288 default:
2289 if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
2290 {
2291 const char * result;
2292
2293 switch (filedata->file_header.e_machine)
2294 {
2295 case EM_AARCH64:
2296 result = get_aarch64_dynamic_type (type);
2297 break;
2298 case EM_MIPS:
2299 case EM_MIPS_RS3_LE:
2300 result = get_mips_dynamic_type (type);
2301 break;
2302 case EM_SPARCV9:
2303 result = get_sparc64_dynamic_type (type);
2304 break;
2305 case EM_PPC:
2306 result = get_ppc_dynamic_type (type);
2307 break;
2308 case EM_PPC64:
2309 result = get_ppc64_dynamic_type (type);
2310 break;
2311 case EM_IA_64:
2312 result = get_ia64_dynamic_type (type);
2313 break;
2314 case EM_ALPHA:
2315 result = get_alpha_dynamic_type (type);
2316 break;
2317 case EM_SCORE:
2318 result = get_score_dynamic_type (type);
2319 break;
2320 case EM_TI_C6000:
2321 result = get_tic6x_dynamic_type (type);
2322 break;
2323 case EM_ALTERA_NIOS2:
2324 result = get_nios2_dynamic_type (type);
2325 break;
2326 default:
2327 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2328 result = get_solaris_dynamic_type (type);
2329 else
2330 result = NULL;
2331 break;
2332 }
2333
2334 if (result != NULL)
2335 return result;
2336
2337 snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
2338 }
2339 else if (((type >= DT_LOOS) && (type <= DT_HIOS))
2340 || (filedata->file_header.e_machine == EM_PARISC
2341 && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
2342 {
2343 const char * result;
2344
2345 switch (filedata->file_header.e_machine)
2346 {
2347 case EM_PARISC:
2348 result = get_parisc_dynamic_type (type);
2349 break;
2350 case EM_IA_64:
2351 result = get_ia64_dynamic_type (type);
2352 break;
2353 default:
2354 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2355 result = get_solaris_dynamic_type (type);
2356 else
2357 result = NULL;
2358 break;
2359 }
2360
2361 if (result != NULL)
2362 return result;
2363
2364 snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
2365 type);
2366 }
2367 else
2368 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
2369
2370 return buff;
2371 }
2372 }
2373
2374 static bool get_program_headers (Filedata *);
2375 static bool get_dynamic_section (Filedata *);
2376
2377 static void
2378 locate_dynamic_section (Filedata *filedata)
2379 {
2380 unsigned long dynamic_addr = 0;
2381 bfd_size_type dynamic_size = 0;
2382
2383 if (filedata->file_header.e_phnum != 0
2384 && get_program_headers (filedata))
2385 {
2386 Elf_Internal_Phdr *segment;
2387 unsigned int i;
2388
2389 for (i = 0, segment = filedata->program_headers;
2390 i < filedata->file_header.e_phnum;
2391 i++, segment++)
2392 {
2393 if (segment->p_type == PT_DYNAMIC)
2394 {
2395 dynamic_addr = segment->p_offset;
2396 dynamic_size = segment->p_filesz;
2397
2398 if (filedata->section_headers != NULL)
2399 {
2400 Elf_Internal_Shdr *sec;
2401
2402 sec = find_section (filedata, ".dynamic");
2403 if (sec != NULL)
2404 {
2405 if (sec->sh_size == 0
2406 || sec->sh_type == SHT_NOBITS)
2407 {
2408 dynamic_addr = 0;
2409 dynamic_size = 0;
2410 }
2411 else
2412 {
2413 dynamic_addr = sec->sh_offset;
2414 dynamic_size = sec->sh_size;
2415 }
2416 }
2417 }
2418
2419 if (dynamic_addr > filedata->file_size
2420 || (dynamic_size > filedata->file_size - dynamic_addr))
2421 {
2422 dynamic_addr = 0;
2423 dynamic_size = 0;
2424 }
2425 break;
2426 }
2427 }
2428 }
2429 filedata->dynamic_addr = dynamic_addr;
2430 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
2431 }
2432
2433 static bool
2434 is_pie (Filedata *filedata)
2435 {
2436 Elf_Internal_Dyn *entry;
2437
2438 if (filedata->dynamic_size == 0)
2439 locate_dynamic_section (filedata);
2440 if (filedata->dynamic_size <= 1)
2441 return false;
2442
2443 if (!get_dynamic_section (filedata))
2444 return false;
2445
2446 for (entry = filedata->dynamic_section;
2447 entry < filedata->dynamic_section + filedata->dynamic_nent;
2448 entry++)
2449 {
2450 if (entry->d_tag == DT_FLAGS_1)
2451 {
2452 if ((entry->d_un.d_val & DF_1_PIE) != 0)
2453 return true;
2454 break;
2455 }
2456 }
2457 return false;
2458 }
2459
2460 static char *
2461 get_file_type (Filedata *filedata)
2462 {
2463 unsigned e_type = filedata->file_header.e_type;
2464 static char buff[64];
2465
2466 switch (e_type)
2467 {
2468 case ET_NONE: return _("NONE (None)");
2469 case ET_REL: return _("REL (Relocatable file)");
2470 case ET_EXEC: return _("EXEC (Executable file)");
2471 case ET_DYN:
2472 if (is_pie (filedata))
2473 return _("DYN (Position-Independent Executable file)");
2474 else
2475 return _("DYN (Shared object file)");
2476 case ET_CORE: return _("CORE (Core file)");
2477
2478 default:
2479 if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
2480 snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
2481 else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
2482 snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
2483 else
2484 snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
2485 return buff;
2486 }
2487 }
2488
2489 static char *
2490 get_machine_name (unsigned e_machine)
2491 {
2492 static char buff[64]; /* XXX */
2493
2494 switch (e_machine)
2495 {
2496 /* Please keep this switch table sorted by increasing EM_ value. */
2497 /* 0 */
2498 case EM_NONE: return _("None");
2499 case EM_M32: return "WE32100";
2500 case EM_SPARC: return "Sparc";
2501 case EM_386: return "Intel 80386";
2502 case EM_68K: return "MC68000";
2503 case EM_88K: return "MC88000";
2504 case EM_IAMCU: return "Intel MCU";
2505 case EM_860: return "Intel 80860";
2506 case EM_MIPS: return "MIPS R3000";
2507 case EM_S370: return "IBM System/370";
2508 /* 10 */
2509 case EM_MIPS_RS3_LE: return "MIPS R4000 big-endian";
2510 case EM_OLD_SPARCV9: return "Sparc v9 (old)";
2511 case EM_PARISC: return "HPPA";
2512 case EM_VPP550: return "Fujitsu VPP500";
2513 case EM_SPARC32PLUS: return "Sparc v8+" ;
2514 case EM_960: return "Intel 80960";
2515 case EM_PPC: return "PowerPC";
2516 /* 20 */
2517 case EM_PPC64: return "PowerPC64";
2518 case EM_S390_OLD:
2519 case EM_S390: return "IBM S/390";
2520 case EM_SPU: return "SPU";
2521 /* 30 */
2522 case EM_V800: return "Renesas V850 (using RH850 ABI)";
2523 case EM_FR20: return "Fujitsu FR20";
2524 case EM_RH32: return "TRW RH32";
2525 case EM_MCORE: return "MCORE";
2526 /* 40 */
2527 case EM_ARM: return "ARM";
2528 case EM_OLD_ALPHA: return "Digital Alpha (old)";
2529 case EM_SH: return "Renesas / SuperH SH";
2530 case EM_SPARCV9: return "Sparc v9";
2531 case EM_TRICORE: return "Siemens Tricore";
2532 case EM_ARC: return "ARC";
2533 case EM_H8_300: return "Renesas H8/300";
2534 case EM_H8_300H: return "Renesas H8/300H";
2535 case EM_H8S: return "Renesas H8S";
2536 case EM_H8_500: return "Renesas H8/500";
2537 /* 50 */
2538 case EM_IA_64: return "Intel IA-64";
2539 case EM_MIPS_X: return "Stanford MIPS-X";
2540 case EM_COLDFIRE: return "Motorola Coldfire";
2541 case EM_68HC12: return "Motorola MC68HC12 Microcontroller";
2542 case EM_MMA: return "Fujitsu Multimedia Accelerator";
2543 case EM_PCP: return "Siemens PCP";
2544 case EM_NCPU: return "Sony nCPU embedded RISC processor";
2545 case EM_NDR1: return "Denso NDR1 microprocesspr";
2546 case EM_STARCORE: return "Motorola Star*Core processor";
2547 case EM_ME16: return "Toyota ME16 processor";
2548 /* 60 */
2549 case EM_ST100: return "STMicroelectronics ST100 processor";
2550 case EM_TINYJ: return "Advanced Logic Corp. TinyJ embedded processor";
2551 case EM_X86_64: return "Advanced Micro Devices X86-64";
2552 case EM_PDSP: return "Sony DSP processor";
2553 case EM_PDP10: return "Digital Equipment Corp. PDP-10";
2554 case EM_PDP11: return "Digital Equipment Corp. PDP-11";
2555 case EM_FX66: return "Siemens FX66 microcontroller";
2556 case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 bit microcontroller";
2557 case EM_ST7: return "STMicroelectronics ST7 8-bit microcontroller";
2558 case EM_68HC16: return "Motorola MC68HC16 Microcontroller";
2559 /* 70 */
2560 case EM_68HC11: return "Motorola MC68HC11 Microcontroller";
2561 case EM_68HC08: return "Motorola MC68HC08 Microcontroller";
2562 case EM_68HC05: return "Motorola MC68HC05 Microcontroller";
2563 case EM_SVX: return "Silicon Graphics SVx";
2564 case EM_ST19: return "STMicroelectronics ST19 8-bit microcontroller";
2565 case EM_VAX: return "Digital VAX";
2566 case EM_CRIS: return "Axis Communications 32-bit embedded processor";
2567 case EM_JAVELIN: return "Infineon Technologies 32-bit embedded cpu";
2568 case EM_FIREPATH: return "Element 14 64-bit DSP processor";
2569 case EM_ZSP: return "LSI Logic's 16-bit DSP processor";
2570 /* 80 */
2571 case EM_MMIX: return "Donald Knuth's educational 64-bit processor";
2572 case EM_HUANY: return "Harvard Universitys's machine-independent object format";
2573 case EM_PRISM: return "Vitesse Prism";
2574 case EM_AVR_OLD:
2575 case EM_AVR: return "Atmel AVR 8-bit microcontroller";
2576 case EM_CYGNUS_FR30:
2577 case EM_FR30: return "Fujitsu FR30";
2578 case EM_CYGNUS_D10V:
2579 case EM_D10V: return "d10v";
2580 case EM_CYGNUS_D30V:
2581 case EM_D30V: return "d30v";
2582 case EM_CYGNUS_V850:
2583 case EM_V850: return "Renesas V850";
2584 case EM_CYGNUS_M32R:
2585 case EM_M32R: return "Renesas M32R (formerly Mitsubishi M32r)";
2586 case EM_CYGNUS_MN10300:
2587 case EM_MN10300: return "mn10300";
2588 /* 90 */
2589 case EM_CYGNUS_MN10200:
2590 case EM_MN10200: return "mn10200";
2591 case EM_PJ: return "picoJava";
2592 case EM_OR1K: return "OpenRISC 1000";
2593 case EM_ARC_COMPACT: return "ARCompact";
2594 case EM_XTENSA_OLD:
2595 case EM_XTENSA: return "Tensilica Xtensa Processor";
2596 case EM_VIDEOCORE: return "Alphamosaic VideoCore processor";
2597 case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor";
2598 case EM_NS32K: return "National Semiconductor 32000 series";
2599 case EM_TPC: return "Tenor Network TPC processor";
2600 case EM_SNP1K: return "Trebia SNP 1000 processor";
2601 /* 100 */
2602 case EM_ST200: return "STMicroelectronics ST200 microcontroller";
2603 case EM_IP2K_OLD:
2604 case EM_IP2K: return "Ubicom IP2xxx 8-bit microcontrollers";
2605 case EM_MAX: return "MAX Processor";
2606 case EM_CR: return "National Semiconductor CompactRISC";
2607 case EM_F2MC16: return "Fujitsu F2MC16";
2608 case EM_MSP430: return "Texas Instruments msp430 microcontroller";
2609 case EM_BLACKFIN: return "Analog Devices Blackfin";
2610 case EM_SE_C33: return "S1C33 Family of Seiko Epson processors";
2611 case EM_SEP: return "Sharp embedded microprocessor";
2612 case EM_ARCA: return "Arca RISC microprocessor";
2613 /* 110 */
2614 case EM_UNICORE: return "Unicore";
2615 case EM_EXCESS: return "eXcess 16/32/64-bit configurable embedded CPU";
2616 case EM_DXP: return "Icera Semiconductor Inc. Deep Execution Processor";
2617 case EM_ALTERA_NIOS2: return "Altera Nios II";
2618 case EM_CRX: return "National Semiconductor CRX microprocessor";
2619 case EM_XGATE: return "Motorola XGATE embedded processor";
2620 case EM_C166:
2621 case EM_XC16X: return "Infineon Technologies xc16x";
2622 case EM_M16C: return "Renesas M16C series microprocessors";
2623 case EM_DSPIC30F: return "Microchip Technology dsPIC30F Digital Signal Controller";
2624 case EM_CE: return "Freescale Communication Engine RISC core";
2625 /* 120 */
2626 case EM_M32C: return "Renesas M32c";
2627 /* 130 */
2628 case EM_TSK3000: return "Altium TSK3000 core";
2629 case EM_RS08: return "Freescale RS08 embedded processor";
2630 case EM_ECOG2: return "Cyan Technology eCOG2 microprocessor";
2631 case EM_SCORE: return "SUNPLUS S+Core";
2632 case EM_DSP24: return "New Japan Radio (NJR) 24-bit DSP Processor";
2633 case EM_VIDEOCORE3: return "Broadcom VideoCore III processor";
2634 case EM_LATTICEMICO32: return "Lattice Mico32";
2635 case EM_SE_C17: return "Seiko Epson C17 family";
2636 /* 140 */
2637 case EM_TI_C6000: return "Texas Instruments TMS320C6000 DSP family";
2638 case EM_TI_C2000: return "Texas Instruments TMS320C2000 DSP family";
2639 case EM_TI_C5500: return "Texas Instruments TMS320C55x DSP family";
2640 case EM_TI_PRU: return "TI PRU I/O processor";
2641 /* 160 */
2642 case EM_MMDSP_PLUS: return "STMicroelectronics 64bit VLIW Data Signal Processor";
2643 case EM_CYPRESS_M8C: return "Cypress M8C microprocessor";
2644 case EM_R32C: return "Renesas R32C series microprocessors";
2645 case EM_TRIMEDIA: return "NXP Semiconductors TriMedia architecture family";
2646 case EM_QDSP6: return "QUALCOMM DSP6 Processor";
2647 case EM_8051: return "Intel 8051 and variants";
2648 case EM_STXP7X: return "STMicroelectronics STxP7x family";
2649 case EM_NDS32: return "Andes Technology compact code size embedded RISC processor family";
2650 case EM_ECOG1X: return "Cyan Technology eCOG1X family";
2651 case EM_MAXQ30: return "Dallas Semiconductor MAXQ30 Core microcontrollers";
2652 /* 170 */
2653 case EM_XIMO16: return "New Japan Radio (NJR) 16-bit DSP Processor";
2654 case EM_MANIK: return "M2000 Reconfigurable RISC Microprocessor";
2655 case EM_CRAYNV2: return "Cray Inc. NV2 vector architecture";
2656 case EM_RX: return "Renesas RX";
2657 case EM_METAG: return "Imagination Technologies Meta processor architecture";
2658 case EM_MCST_ELBRUS: return "MCST Elbrus general purpose hardware architecture";
2659 case EM_ECOG16: return "Cyan Technology eCOG16 family";
2660 case EM_CR16:
2661 case EM_MICROBLAZE:
2662 case EM_MICROBLAZE_OLD: return "Xilinx MicroBlaze";
2663 case EM_ETPU: return "Freescale Extended Time Processing Unit";
2664 case EM_SLE9X: return "Infineon Technologies SLE9X core";
2665 /* 180 */
2666 case EM_L1OM: return "Intel L1OM";
2667 case EM_K1OM: return "Intel K1OM";
2668 case EM_INTEL182: return "Intel (reserved)";
2669 case EM_AARCH64: return "AArch64";
2670 case EM_ARM184: return "ARM (reserved)";
2671 case EM_AVR32: return "Atmel Corporation 32-bit microprocessor";
2672 case EM_STM8: return "STMicroeletronics STM8 8-bit microcontroller";
2673 case EM_TILE64: return "Tilera TILE64 multicore architecture family";
2674 case EM_TILEPRO: return "Tilera TILEPro multicore architecture family";
2675 /* 190 */
2676 case EM_CUDA: return "NVIDIA CUDA architecture";
2677 case EM_TILEGX: return "Tilera TILE-Gx multicore architecture family";
2678 case EM_CLOUDSHIELD: return "CloudShield architecture family";
2679 case EM_COREA_1ST: return "KIPO-KAIST Core-A 1st generation processor family";
2680 case EM_COREA_2ND: return "KIPO-KAIST Core-A 2nd generation processor family";
2681 case EM_ARC_COMPACT2: return "ARCv2";
2682 case EM_OPEN8: return "Open8 8-bit RISC soft processor core";
2683 case EM_RL78: return "Renesas RL78";
2684 case EM_VIDEOCORE5: return "Broadcom VideoCore V processor";
2685 case EM_78K0R: return "Renesas 78K0R";
2686 /* 200 */
2687 case EM_56800EX: return "Freescale 56800EX Digital Signal Controller (DSC)";
2688 case EM_BA1: return "Beyond BA1 CPU architecture";
2689 case EM_BA2: return "Beyond BA2 CPU architecture";
2690 case EM_XCORE: return "XMOS xCORE processor family";
2691 case EM_MCHP_PIC: return "Microchip 8-bit PIC(r) family";
2692 case EM_INTELGT: return "Intel Graphics Technology";
2693 /* 210 */
2694 case EM_KM32: return "KM211 KM32 32-bit processor";
2695 case EM_KMX32: return "KM211 KMX32 32-bit processor";
2696 case EM_KMX16: return "KM211 KMX16 16-bit processor";
2697 case EM_KMX8: return "KM211 KMX8 8-bit processor";
2698 case EM_KVARC: return "KM211 KVARC processor";
2699 case EM_CDP: return "Paneve CDP architecture family";
2700 case EM_COGE: return "Cognitive Smart Memory Processor";
2701 case EM_COOL: return "Bluechip Systems CoolEngine";
2702 case EM_NORC: return "Nanoradio Optimized RISC";
2703 case EM_CSR_KALIMBA: return "CSR Kalimba architecture family";
2704 /* 220 */
2705 case EM_Z80: return "Zilog Z80";
2706 case EM_VISIUM: return "CDS VISIUMcore processor";
2707 case EM_FT32: return "FTDI Chip FT32";
2708 case EM_MOXIE: return "Moxie";
2709 case EM_AMDGPU: return "AMD GPU";
2710 /* 230 (all reserved) */
2711 /* 240 */
2712 case EM_RISCV: return "RISC-V";
2713 case EM_LANAI: return "Lanai 32-bit processor";
2714 case EM_CEVA: return "CEVA Processor Architecture Family";
2715 case EM_CEVA_X2: return "CEVA X2 Processor Family";
2716 case EM_BPF: return "Linux BPF";
2717 case EM_GRAPHCORE_IPU: return "Graphcore Intelligent Processing Unit";
2718 case EM_IMG1: return "Imagination Technologies";
2719 /* 250 */
2720 case EM_NFP: return "Netronome Flow Processor";
2721 case EM_VE: return "NEC Vector Engine";
2722 case EM_CSKY: return "C-SKY";
2723 case EM_ARC_COMPACT3_64: return "Synopsys ARCv2.3 64-bit";
2724 case EM_MCS6502: return "MOS Technology MCS 6502 processor";
2725 case EM_ARC_COMPACT3: return "Synopsys ARCv2.3 32-bit";
2726 case EM_KVX: return "Kalray VLIW core of the MPPA processor family";
2727 case EM_65816: return "WDC 65816/65C816";
2728 case EM_LOONGARCH: return "LoongArch";
2729 case EM_KF32: return "ChipON KungFu32";
2730
2731 /* Large numbers... */
2732 case EM_MT: return "Morpho Techologies MT processor";
2733 case EM_ALPHA: return "Alpha";
2734 case EM_WEBASSEMBLY: return "Web Assembly";
2735 case EM_DLX: return "OpenDLX";
2736 case EM_XSTORMY16: return "Sanyo XStormy16 CPU core";
2737 case EM_IQ2000: return "Vitesse IQ2000";
2738 case EM_M32C_OLD:
2739 case EM_NIOS32: return "Altera Nios";
2740 case EM_CYGNUS_MEP: return "Toshiba MeP Media Engine";
2741 case EM_ADAPTEVA_EPIPHANY: return "Adapteva EPIPHANY";
2742 case EM_CYGNUS_FRV: return "Fujitsu FR-V";
2743 case EM_S12Z: return "Freescale S12Z";
2744
2745 default:
2746 snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
2747 return buff;
2748 }
2749 }
2750
2751 static void
2752 decode_ARC_machine_flags (unsigned e_flags, unsigned e_machine, char buf[])
2753 {
2754 /* ARC has two machine types EM_ARC_COMPACT and EM_ARC_COMPACT2. Some
2755 other compilers don't specify an architecture type in the e_flags, and
2756 instead use EM_ARC_COMPACT for old ARC600, ARC601, and ARC700
2757 architectures, and switch to EM_ARC_COMPACT2 for newer ARCEM and ARCHS
2758 architectures.
2759
2760 Th GNU tools follows this use of EM_ARC_COMPACT and EM_ARC_COMPACT2,
2761 but also sets a specific architecture type in the e_flags field.
2762
2763 However, when decoding the flags we don't worry if we see an
2764 unexpected pairing, for example EM_ARC_COMPACT machine type, with
2765 ARCEM architecture type. */
2766
2767 switch (e_flags & EF_ARC_MACH_MSK)
2768 {
2769 /* We only expect these to occur for EM_ARC_COMPACT2. */
2770 case EF_ARC_CPU_ARCV2EM:
2771 strcat (buf, ", ARC EM");
2772 break;
2773 case EF_ARC_CPU_ARCV2HS:
2774 strcat (buf, ", ARC HS");
2775 break;
2776
2777 /* We only expect these to occur for EM_ARC_COMPACT. */
2778 case E_ARC_MACH_ARC600:
2779 strcat (buf, ", ARC600");
2780 break;
2781 case E_ARC_MACH_ARC601:
2782 strcat (buf, ", ARC601");
2783 break;
2784 case E_ARC_MACH_ARC700:
2785 strcat (buf, ", ARC700");
2786 break;
2787
2788 /* The only times we should end up here are (a) A corrupt ELF, (b) A
2789 new ELF with new architecture being read by an old version of
2790 readelf, or (c) An ELF built with non-GNU compiler that does not
2791 set the architecture in the e_flags. */
2792 default:
2793 if (e_machine == EM_ARC_COMPACT)
2794 strcat (buf, ", Unknown ARCompact");
2795 else
2796 strcat (buf, ", Unknown ARC");
2797 break;
2798 }
2799
2800 switch (e_flags & EF_ARC_OSABI_MSK)
2801 {
2802 case E_ARC_OSABI_ORIG:
2803 strcat (buf, ", (ABI:legacy)");
2804 break;
2805 case E_ARC_OSABI_V2:
2806 strcat (buf, ", (ABI:v2)");
2807 break;
2808 /* Only upstream 3.9+ kernels will support ARCv2 ISA. */
2809 case E_ARC_OSABI_V3:
2810 strcat (buf, ", v3 no-legacy-syscalls ABI");
2811 break;
2812 case E_ARC_OSABI_V4:
2813 strcat (buf, ", v4 ABI");
2814 break;
2815 default:
2816 strcat (buf, ", unrecognised ARC OSABI flag");
2817 break;
2818 }
2819 }
2820
2821 static void
2822 decode_ARM_machine_flags (unsigned e_flags, char buf[])
2823 {
2824 unsigned eabi;
2825 bool unknown = false;
2826
2827 eabi = EF_ARM_EABI_VERSION (e_flags);
2828 e_flags &= ~ EF_ARM_EABIMASK;
2829
2830 /* Handle "generic" ARM flags. */
2831 if (e_flags & EF_ARM_RELEXEC)
2832 {
2833 strcat (buf, ", relocatable executable");
2834 e_flags &= ~ EF_ARM_RELEXEC;
2835 }
2836
2837 if (e_flags & EF_ARM_PIC)
2838 {
2839 strcat (buf, ", position independent");
2840 e_flags &= ~ EF_ARM_PIC;
2841 }
2842
2843 /* Now handle EABI specific flags. */
2844 switch (eabi)
2845 {
2846 default:
2847 strcat (buf, ", <unrecognized EABI>");
2848 if (e_flags)
2849 unknown = true;
2850 break;
2851
2852 case EF_ARM_EABI_VER1:
2853 strcat (buf, ", Version1 EABI");
2854 while (e_flags)
2855 {
2856 unsigned flag;
2857
2858 /* Process flags one bit at a time. */
2859 flag = e_flags & - e_flags;
2860 e_flags &= ~ flag;
2861
2862 switch (flag)
2863 {
2864 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
2865 strcat (buf, ", sorted symbol tables");
2866 break;
2867
2868 default:
2869 unknown = true;
2870 break;
2871 }
2872 }
2873 break;
2874
2875 case EF_ARM_EABI_VER2:
2876 strcat (buf, ", Version2 EABI");
2877 while (e_flags)
2878 {
2879 unsigned flag;
2880
2881 /* Process flags one bit at a time. */
2882 flag = e_flags & - e_flags;
2883 e_flags &= ~ flag;
2884
2885 switch (flag)
2886 {
2887 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
2888 strcat (buf, ", sorted symbol tables");
2889 break;
2890
2891 case EF_ARM_DYNSYMSUSESEGIDX:
2892 strcat (buf, ", dynamic symbols use segment index");
2893 break;
2894
2895 case EF_ARM_MAPSYMSFIRST:
2896 strcat (buf, ", mapping symbols precede others");
2897 break;
2898
2899 default:
2900 unknown = true;
2901 break;
2902 }
2903 }
2904 break;
2905
2906 case EF_ARM_EABI_VER3:
2907 strcat (buf, ", Version3 EABI");
2908 break;
2909
2910 case EF_ARM_EABI_VER4:
2911 strcat (buf, ", Version4 EABI");
2912 while (e_flags)
2913 {
2914 unsigned flag;
2915
2916 /* Process flags one bit at a time. */
2917 flag = e_flags & - e_flags;
2918 e_flags &= ~ flag;
2919
2920 switch (flag)
2921 {
2922 case EF_ARM_BE8:
2923 strcat (buf, ", BE8");
2924 break;
2925
2926 case EF_ARM_LE8:
2927 strcat (buf, ", LE8");
2928 break;
2929
2930 default:
2931 unknown = true;
2932 break;
2933 }
2934 }
2935 break;
2936
2937 case EF_ARM_EABI_VER5:
2938 strcat (buf, ", Version5 EABI");
2939 while (e_flags)
2940 {
2941 unsigned flag;
2942
2943 /* Process flags one bit at a time. */
2944 flag = e_flags & - e_flags;
2945 e_flags &= ~ flag;
2946
2947 switch (flag)
2948 {
2949 case EF_ARM_BE8:
2950 strcat (buf, ", BE8");
2951 break;
2952
2953 case EF_ARM_LE8:
2954 strcat (buf, ", LE8");
2955 break;
2956
2957 case EF_ARM_ABI_FLOAT_SOFT: /* Conflicts with EF_ARM_SOFT_FLOAT. */
2958 strcat (buf, ", soft-float ABI");
2959 break;
2960
2961 case EF_ARM_ABI_FLOAT_HARD: /* Conflicts with EF_ARM_VFP_FLOAT. */
2962 strcat (buf, ", hard-float ABI");
2963 break;
2964
2965 default:
2966 unknown = true;
2967 break;
2968 }
2969 }
2970 break;
2971
2972 case EF_ARM_EABI_UNKNOWN:
2973 strcat (buf, ", GNU EABI");
2974 while (e_flags)
2975 {
2976 unsigned flag;
2977
2978 /* Process flags one bit at a time. */
2979 flag = e_flags & - e_flags;
2980 e_flags &= ~ flag;
2981
2982 switch (flag)
2983 {
2984 case EF_ARM_INTERWORK:
2985 strcat (buf, ", interworking enabled");
2986 break;
2987
2988 case EF_ARM_APCS_26:
2989 strcat (buf, ", uses APCS/26");
2990 break;
2991
2992 case EF_ARM_APCS_FLOAT:
2993 strcat (buf, ", uses APCS/float");
2994 break;
2995
2996 case EF_ARM_PIC:
2997 strcat (buf, ", position independent");
2998 break;
2999
3000 case EF_ARM_ALIGN8:
3001 strcat (buf, ", 8 bit structure alignment");
3002 break;
3003
3004 case EF_ARM_NEW_ABI:
3005 strcat (buf, ", uses new ABI");
3006 break;
3007
3008 case EF_ARM_OLD_ABI:
3009 strcat (buf, ", uses old ABI");
3010 break;
3011
3012 case EF_ARM_SOFT_FLOAT:
3013 strcat (buf, ", software FP");
3014 break;
3015
3016 case EF_ARM_VFP_FLOAT:
3017 strcat (buf, ", VFP");
3018 break;
3019
3020 case EF_ARM_MAVERICK_FLOAT:
3021 strcat (buf, ", Maverick FP");
3022 break;
3023
3024 default:
3025 unknown = true;
3026 break;
3027 }
3028 }
3029 }
3030
3031 if (unknown)
3032 strcat (buf,_(", <unknown>"));
3033 }
3034
3035 static void
3036 decode_AVR_machine_flags (unsigned e_flags, char buf[], size_t size)
3037 {
3038 --size; /* Leave space for null terminator. */
3039
3040 switch (e_flags & EF_AVR_MACH)
3041 {
3042 case E_AVR_MACH_AVR1:
3043 strncat (buf, ", avr:1", size);
3044 break;
3045 case E_AVR_MACH_AVR2:
3046 strncat (buf, ", avr:2", size);
3047 break;
3048 case E_AVR_MACH_AVR25:
3049 strncat (buf, ", avr:25", size);
3050 break;
3051 case E_AVR_MACH_AVR3:
3052 strncat (buf, ", avr:3", size);
3053 break;
3054 case E_AVR_MACH_AVR31:
3055 strncat (buf, ", avr:31", size);
3056 break;
3057 case E_AVR_MACH_AVR35:
3058 strncat (buf, ", avr:35", size);
3059 break;
3060 case E_AVR_MACH_AVR4:
3061 strncat (buf, ", avr:4", size);
3062 break;
3063 case E_AVR_MACH_AVR5:
3064 strncat (buf, ", avr:5", size);
3065 break;
3066 case E_AVR_MACH_AVR51:
3067 strncat (buf, ", avr:51", size);
3068 break;
3069 case E_AVR_MACH_AVR6:
3070 strncat (buf, ", avr:6", size);
3071 break;
3072 case E_AVR_MACH_AVRTINY:
3073 strncat (buf, ", avr:100", size);
3074 break;
3075 case E_AVR_MACH_XMEGA1:
3076 strncat (buf, ", avr:101", size);
3077 break;
3078 case E_AVR_MACH_XMEGA2:
3079 strncat (buf, ", avr:102", size);
3080 break;
3081 case E_AVR_MACH_XMEGA3:
3082 strncat (buf, ", avr:103", size);
3083 break;
3084 case E_AVR_MACH_XMEGA4:
3085 strncat (buf, ", avr:104", size);
3086 break;
3087 case E_AVR_MACH_XMEGA5:
3088 strncat (buf, ", avr:105", size);
3089 break;
3090 case E_AVR_MACH_XMEGA6:
3091 strncat (buf, ", avr:106", size);
3092 break;
3093 case E_AVR_MACH_XMEGA7:
3094 strncat (buf, ", avr:107", size);
3095 break;
3096 default:
3097 strncat (buf, ", avr:<unknown>", size);
3098 break;
3099 }
3100
3101 size -= strlen (buf);
3102 if (e_flags & EF_AVR_LINKRELAX_PREPARED)
3103 strncat (buf, ", link-relax", size);
3104 }
3105
3106 static void
3107 decode_NDS32_machine_flags (unsigned e_flags, char buf[], size_t size)
3108 {
3109 unsigned abi;
3110 unsigned arch;
3111 unsigned config;
3112 unsigned version;
3113 bool has_fpu = false;
3114 unsigned int r = 0;
3115
3116 static const char *ABI_STRINGS[] =
3117 {
3118 "ABI v0", /* use r5 as return register; only used in N1213HC */
3119 "ABI v1", /* use r0 as return register */
3120 "ABI v2", /* use r0 as return register and don't reserve 24 bytes for arguments */
3121 "ABI v2fp", /* for FPU */
3122 "AABI",
3123 "ABI2 FP+"
3124 };
3125 static const char *VER_STRINGS[] =
3126 {
3127 "Andes ELF V1.3 or older",
3128 "Andes ELF V1.3.1",
3129 "Andes ELF V1.4"
3130 };
3131 static const char *ARCH_STRINGS[] =
3132 {
3133 "",
3134 "Andes Star v1.0",
3135 "Andes Star v2.0",
3136 "Andes Star v3.0",
3137 "Andes Star v3.0m"
3138 };
3139
3140 abi = EF_NDS_ABI & e_flags;
3141 arch = EF_NDS_ARCH & e_flags;
3142 config = EF_NDS_INST & e_flags;
3143 version = EF_NDS32_ELF_VERSION & e_flags;
3144
3145 memset (buf, 0, size);
3146
3147 switch (abi)
3148 {
3149 case E_NDS_ABI_V0:
3150 case E_NDS_ABI_V1:
3151 case E_NDS_ABI_V2:
3152 case E_NDS_ABI_V2FP:
3153 case E_NDS_ABI_AABI:
3154 case E_NDS_ABI_V2FP_PLUS:
3155 /* In case there are holes in the array. */
3156 r += snprintf (buf + r, size - r, ", %s", ABI_STRINGS[abi >> EF_NDS_ABI_SHIFT]);
3157 break;
3158
3159 default:
3160 r += snprintf (buf + r, size - r, ", <unrecognized ABI>");
3161 break;
3162 }
3163
3164 switch (version)
3165 {
3166 case E_NDS32_ELF_VER_1_2:
3167 case E_NDS32_ELF_VER_1_3:
3168 case E_NDS32_ELF_VER_1_4:
3169 r += snprintf (buf + r, size - r, ", %s", VER_STRINGS[version >> EF_NDS32_ELF_VERSION_SHIFT]);
3170 break;
3171
3172 default:
3173 r += snprintf (buf + r, size - r, ", <unrecognized ELF version number>");
3174 break;
3175 }
3176
3177 if (E_NDS_ABI_V0 == abi)
3178 {
3179 /* OLD ABI; only used in N1213HC, has performance extension 1. */
3180 r += snprintf (buf + r, size - r, ", Andes Star v1.0, N1213HC, MAC, PERF1");
3181 if (arch == E_NDS_ARCH_STAR_V1_0)
3182 r += snprintf (buf + r, size -r, ", 16b"); /* has 16-bit instructions */
3183 return;
3184 }
3185
3186 switch (arch)
3187 {
3188 case E_NDS_ARCH_STAR_V1_0:
3189 case E_NDS_ARCH_STAR_V2_0:
3190 case E_NDS_ARCH_STAR_V3_0:
3191 case E_NDS_ARCH_STAR_V3_M:
3192 r += snprintf (buf + r, size - r, ", %s", ARCH_STRINGS[arch >> EF_NDS_ARCH_SHIFT]);
3193 break;
3194
3195 default:
3196 r += snprintf (buf + r, size - r, ", <unrecognized architecture>");
3197 /* ARCH version determines how the e_flags are interpreted.
3198 If it is unknown, we cannot proceed. */
3199 return;
3200 }
3201
3202 /* Newer ABI; Now handle architecture specific flags. */
3203 if (arch == E_NDS_ARCH_STAR_V1_0)
3204 {
3205 if (config & E_NDS32_HAS_MFUSR_PC_INST)
3206 r += snprintf (buf + r, size -r, ", MFUSR_PC");
3207
3208 if (!(config & E_NDS32_HAS_NO_MAC_INST))
3209 r += snprintf (buf + r, size -r, ", MAC");
3210
3211 if (config & E_NDS32_HAS_DIV_INST)
3212 r += snprintf (buf + r, size -r, ", DIV");
3213
3214 if (config & E_NDS32_HAS_16BIT_INST)
3215 r += snprintf (buf + r, size -r, ", 16b");
3216 }
3217 else
3218 {
3219 if (config & E_NDS32_HAS_MFUSR_PC_INST)
3220 {
3221 if (version <= E_NDS32_ELF_VER_1_3)
3222 r += snprintf (buf + r, size -r, ", [B8]");
3223 else
3224 r += snprintf (buf + r, size -r, ", EX9");
3225 }
3226
3227 if (config & E_NDS32_HAS_MAC_DX_INST)
3228 r += snprintf (buf + r, size -r, ", MAC_DX");
3229
3230 if (config & E_NDS32_HAS_DIV_DX_INST)
3231 r += snprintf (buf + r, size -r, ", DIV_DX");
3232
3233 if (config & E_NDS32_HAS_16BIT_INST)
3234 {
3235 if (version <= E_NDS32_ELF_VER_1_3)
3236 r += snprintf (buf + r, size -r, ", 16b");
3237 else
3238 r += snprintf (buf + r, size -r, ", IFC");
3239 }
3240 }
3241
3242 if (config & E_NDS32_HAS_EXT_INST)
3243 r += snprintf (buf + r, size -r, ", PERF1");
3244
3245 if (config & E_NDS32_HAS_EXT2_INST)
3246 r += snprintf (buf + r, size -r, ", PERF2");
3247
3248 if (config & E_NDS32_HAS_FPU_INST)
3249 {
3250 has_fpu = true;
3251 r += snprintf (buf + r, size -r, ", FPU_SP");
3252 }
3253
3254 if (config & E_NDS32_HAS_FPU_DP_INST)
3255 {
3256 has_fpu = true;
3257 r += snprintf (buf + r, size -r, ", FPU_DP");
3258 }
3259
3260 if (config & E_NDS32_HAS_FPU_MAC_INST)
3261 {
3262 has_fpu = true;
3263 r += snprintf (buf + r, size -r, ", FPU_MAC");
3264 }
3265
3266 if (has_fpu)
3267 {
3268 switch ((config & E_NDS32_FPU_REG_CONF) >> E_NDS32_FPU_REG_CONF_SHIFT)
3269 {
3270 case E_NDS32_FPU_REG_8SP_4DP:
3271 r += snprintf (buf + r, size -r, ", FPU_REG:8/4");
3272 break;
3273 case E_NDS32_FPU_REG_16SP_8DP:
3274 r += snprintf (buf + r, size -r, ", FPU_REG:16/8");
3275 break;
3276 case E_NDS32_FPU_REG_32SP_16DP:
3277 r += snprintf (buf + r, size -r, ", FPU_REG:32/16");
3278 break;
3279 case E_NDS32_FPU_REG_32SP_32DP:
3280 r += snprintf (buf + r, size -r, ", FPU_REG:32/32");
3281 break;
3282 }
3283 }
3284
3285 if (config & E_NDS32_HAS_AUDIO_INST)
3286 r += snprintf (buf + r, size -r, ", AUDIO");
3287
3288 if (config & E_NDS32_HAS_STRING_INST)
3289 r += snprintf (buf + r, size -r, ", STR");
3290
3291 if (config & E_NDS32_HAS_REDUCED_REGS)
3292 r += snprintf (buf + r, size -r, ", 16REG");
3293
3294 if (config & E_NDS32_HAS_VIDEO_INST)
3295 {
3296 if (version <= E_NDS32_ELF_VER_1_3)
3297 r += snprintf (buf + r, size -r, ", VIDEO");
3298 else
3299 r += snprintf (buf + r, size -r, ", SATURATION");
3300 }
3301
3302 if (config & E_NDS32_HAS_ENCRIPT_INST)
3303 r += snprintf (buf + r, size -r, ", ENCRP");
3304
3305 if (config & E_NDS32_HAS_L2C_INST)
3306 r += snprintf (buf + r, size -r, ", L2C");
3307 }
3308
3309 static char *
3310 get_machine_flags (Filedata * filedata, unsigned e_flags, unsigned e_machine)
3311 {
3312 static char buf[1024];
3313
3314 buf[0] = '\0';
3315
3316 if (e_flags)
3317 {
3318 switch (e_machine)
3319 {
3320 default:
3321 break;
3322
3323 case EM_ARC_COMPACT2:
3324 case EM_ARC_COMPACT:
3325 decode_ARC_machine_flags (e_flags, e_machine, buf);
3326 break;
3327
3328 case EM_ARM:
3329 decode_ARM_machine_flags (e_flags, buf);
3330 break;
3331
3332 case EM_AVR:
3333 decode_AVR_machine_flags (e_flags, buf, sizeof buf);
3334 break;
3335
3336 case EM_BLACKFIN:
3337 if (e_flags & EF_BFIN_PIC)
3338 strcat (buf, ", PIC");
3339
3340 if (e_flags & EF_BFIN_FDPIC)
3341 strcat (buf, ", FDPIC");
3342
3343 if (e_flags & EF_BFIN_CODE_IN_L1)
3344 strcat (buf, ", code in L1");
3345
3346 if (e_flags & EF_BFIN_DATA_IN_L1)
3347 strcat (buf, ", data in L1");
3348
3349 break;
3350
3351 case EM_CYGNUS_FRV:
3352 switch (e_flags & EF_FRV_CPU_MASK)
3353 {
3354 case EF_FRV_CPU_GENERIC:
3355 break;
3356
3357 default:
3358 strcat (buf, ", fr???");
3359 break;
3360
3361 case EF_FRV_CPU_FR300:
3362 strcat (buf, ", fr300");
3363 break;
3364
3365 case EF_FRV_CPU_FR400:
3366 strcat (buf, ", fr400");
3367 break;
3368 case EF_FRV_CPU_FR405:
3369 strcat (buf, ", fr405");
3370 break;
3371
3372 case EF_FRV_CPU_FR450:
3373 strcat (buf, ", fr450");
3374 break;
3375
3376 case EF_FRV_CPU_FR500:
3377 strcat (buf, ", fr500");
3378 break;
3379 case EF_FRV_CPU_FR550:
3380 strcat (buf, ", fr550");
3381 break;
3382
3383 case EF_FRV_CPU_SIMPLE:
3384 strcat (buf, ", simple");
3385 break;
3386 case EF_FRV_CPU_TOMCAT:
3387 strcat (buf, ", tomcat");
3388 break;
3389 }
3390 break;
3391
3392 case EM_68K:
3393 if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
3394 strcat (buf, ", m68000");
3395 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
3396 strcat (buf, ", cpu32");
3397 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
3398 strcat (buf, ", fido_a");
3399 else
3400 {
3401 char const * isa = _("unknown");
3402 char const * mac = _("unknown mac");
3403 char const * additional = NULL;
3404
3405 switch (e_flags & EF_M68K_CF_ISA_MASK)
3406 {
3407 case EF_M68K_CF_ISA_A_NODIV:
3408 isa = "A";
3409 additional = ", nodiv";
3410 break;
3411 case EF_M68K_CF_ISA_A:
3412 isa = "A";
3413 break;
3414 case EF_M68K_CF_ISA_A_PLUS:
3415 isa = "A+";
3416 break;
3417 case EF_M68K_CF_ISA_B_NOUSP:
3418 isa = "B";
3419 additional = ", nousp";
3420 break;
3421 case EF_M68K_CF_ISA_B:
3422 isa = "B";
3423 break;
3424 case EF_M68K_CF_ISA_C:
3425 isa = "C";
3426 break;
3427 case EF_M68K_CF_ISA_C_NODIV:
3428 isa = "C";
3429 additional = ", nodiv";
3430 break;
3431 }
3432 strcat (buf, ", cf, isa ");
3433 strcat (buf, isa);
3434 if (additional)
3435 strcat (buf, additional);
3436 if (e_flags & EF_M68K_CF_FLOAT)
3437 strcat (buf, ", float");
3438 switch (e_flags & EF_M68K_CF_MAC_MASK)
3439 {
3440 case 0:
3441 mac = NULL;
3442 break;
3443 case EF_M68K_CF_MAC:
3444 mac = "mac";
3445 break;
3446 case EF_M68K_CF_EMAC:
3447 mac = "emac";
3448 break;
3449 case EF_M68K_CF_EMAC_B:
3450 mac = "emac_b";
3451 break;
3452 }
3453 if (mac)
3454 {
3455 strcat (buf, ", ");
3456 strcat (buf, mac);
3457 }
3458 }
3459 break;
3460
3461 case EM_CYGNUS_MEP:
3462 switch (e_flags & EF_MEP_CPU_MASK)
3463 {
3464 case EF_MEP_CPU_MEP: strcat (buf, ", generic MeP"); break;
3465 case EF_MEP_CPU_C2: strcat (buf, ", MeP C2"); break;
3466 case EF_MEP_CPU_C3: strcat (buf, ", MeP C3"); break;
3467 case EF_MEP_CPU_C4: strcat (buf, ", MeP C4"); break;
3468 case EF_MEP_CPU_C5: strcat (buf, ", MeP C5"); break;
3469 case EF_MEP_CPU_H1: strcat (buf, ", MeP H1"); break;
3470 default: strcat (buf, _(", <unknown MeP cpu type>")); break;
3471 }
3472
3473 switch (e_flags & EF_MEP_COP_MASK)
3474 {
3475 case EF_MEP_COP_NONE: break;
3476 case EF_MEP_COP_AVC: strcat (buf, ", AVC coprocessor"); break;
3477 case EF_MEP_COP_AVC2: strcat (buf, ", AVC2 coprocessor"); break;
3478 case EF_MEP_COP_FMAX: strcat (buf, ", FMAX coprocessor"); break;
3479 case EF_MEP_COP_IVC2: strcat (buf, ", IVC2 coprocessor"); break;
3480 default: strcat (buf, _("<unknown MeP copro type>")); break;
3481 }
3482
3483 if (e_flags & EF_MEP_LIBRARY)
3484 strcat (buf, ", Built for Library");
3485
3486 if (e_flags & EF_MEP_INDEX_MASK)
3487 sprintf (buf + strlen (buf), ", Configuration Index: %#x",
3488 e_flags & EF_MEP_INDEX_MASK);
3489
3490 if (e_flags & ~ EF_MEP_ALL_FLAGS)
3491 sprintf (buf + strlen (buf), _(", unknown flags bits: %#x"),
3492 e_flags & ~ EF_MEP_ALL_FLAGS);
3493 break;
3494
3495 case EM_PPC:
3496 if (e_flags & EF_PPC_EMB)
3497 strcat (buf, ", emb");
3498
3499 if (e_flags & EF_PPC_RELOCATABLE)
3500 strcat (buf, _(", relocatable"));
3501
3502 if (e_flags & EF_PPC_RELOCATABLE_LIB)
3503 strcat (buf, _(", relocatable-lib"));
3504 break;
3505
3506 case EM_PPC64:
3507 if (e_flags & EF_PPC64_ABI)
3508 {
3509 char abi[] = ", abiv0";
3510
3511 abi[6] += e_flags & EF_PPC64_ABI;
3512 strcat (buf, abi);
3513 }
3514 break;
3515
3516 case EM_V800:
3517 if ((e_flags & EF_RH850_ABI) == EF_RH850_ABI)
3518 strcat (buf, ", RH850 ABI");
3519
3520 if (e_flags & EF_V800_850E3)
3521 strcat (buf, ", V3 architecture");
3522
3523 if ((e_flags & (EF_RH850_FPU_DOUBLE | EF_RH850_FPU_SINGLE)) == 0)
3524 strcat (buf, ", FPU not used");
3525
3526 if ((e_flags & (EF_RH850_REGMODE22 | EF_RH850_REGMODE32)) == 0)
3527 strcat (buf, ", regmode: COMMON");
3528
3529 if ((e_flags & (EF_RH850_GP_FIX | EF_RH850_GP_NOFIX)) == 0)
3530 strcat (buf, ", r4 not used");
3531
3532 if ((e_flags & (EF_RH850_EP_FIX | EF_RH850_EP_NOFIX)) == 0)
3533 strcat (buf, ", r30 not used");
3534
3535 if ((e_flags & (EF_RH850_TP_FIX | EF_RH850_TP_NOFIX)) == 0)
3536 strcat (buf, ", r5 not used");
3537
3538 if ((e_flags & (EF_RH850_REG2_RESERVE | EF_RH850_REG2_NORESERVE)) == 0)
3539 strcat (buf, ", r2 not used");
3540
3541 for (e_flags &= 0xFFFF; e_flags; e_flags &= ~ (e_flags & - e_flags))
3542 {
3543 switch (e_flags & - e_flags)
3544 {
3545 case EF_RH850_FPU_DOUBLE: strcat (buf, ", double precision FPU"); break;
3546 case EF_RH850_FPU_SINGLE: strcat (buf, ", single precision FPU"); break;
3547 case EF_RH850_REGMODE22: strcat (buf, ", regmode:22"); break;
3548 case EF_RH850_REGMODE32: strcat (buf, ", regmode:23"); break;
3549 case EF_RH850_GP_FIX: strcat (buf, ", r4 fixed"); break;
3550 case EF_RH850_GP_NOFIX: strcat (buf, ", r4 free"); break;
3551 case EF_RH850_EP_FIX: strcat (buf, ", r30 fixed"); break;
3552 case EF_RH850_EP_NOFIX: strcat (buf, ", r30 free"); break;
3553 case EF_RH850_TP_FIX: strcat (buf, ", r5 fixed"); break;
3554 case EF_RH850_TP_NOFIX: strcat (buf, ", r5 free"); break;
3555 case EF_RH850_REG2_RESERVE: strcat (buf, ", r2 fixed"); break;
3556 case EF_RH850_REG2_NORESERVE: strcat (buf, ", r2 free"); break;
3557 default: break;
3558 }
3559 }
3560 break;
3561
3562 case EM_V850:
3563 case EM_CYGNUS_V850:
3564 switch (e_flags & EF_V850_ARCH)
3565 {
3566 case E_V850E3V5_ARCH:
3567 strcat (buf, ", v850e3v5");
3568 break;
3569 case E_V850E2V3_ARCH:
3570 strcat (buf, ", v850e2v3");
3571 break;
3572 case E_V850E2_ARCH:
3573 strcat (buf, ", v850e2");
3574 break;
3575 case E_V850E1_ARCH:
3576 strcat (buf, ", v850e1");
3577 break;
3578 case E_V850E_ARCH:
3579 strcat (buf, ", v850e");
3580 break;
3581 case E_V850_ARCH:
3582 strcat (buf, ", v850");
3583 break;
3584 default:
3585 strcat (buf, _(", unknown v850 architecture variant"));
3586 break;
3587 }
3588 break;
3589
3590 case EM_M32R:
3591 case EM_CYGNUS_M32R:
3592 if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
3593 strcat (buf, ", m32r");
3594 break;
3595
3596 case EM_MIPS:
3597 case EM_MIPS_RS3_LE:
3598 if (e_flags & EF_MIPS_NOREORDER)
3599 strcat (buf, ", noreorder");
3600
3601 if (e_flags & EF_MIPS_PIC)
3602 strcat (buf, ", pic");
3603
3604 if (e_flags & EF_MIPS_CPIC)
3605 strcat (buf, ", cpic");
3606
3607 if (e_flags & EF_MIPS_UCODE)
3608 strcat (buf, ", ugen_reserved");
3609
3610 if (e_flags & EF_MIPS_ABI2)
3611 strcat (buf, ", abi2");
3612
3613 if (e_flags & EF_MIPS_OPTIONS_FIRST)
3614 strcat (buf, ", odk first");
3615
3616 if (e_flags & EF_MIPS_32BITMODE)
3617 strcat (buf, ", 32bitmode");
3618
3619 if (e_flags & EF_MIPS_NAN2008)
3620 strcat (buf, ", nan2008");
3621
3622 if (e_flags & EF_MIPS_FP64)
3623 strcat (buf, ", fp64");
3624
3625 switch ((e_flags & EF_MIPS_MACH))
3626 {
3627 case E_MIPS_MACH_3900: strcat (buf, ", 3900"); break;
3628 case E_MIPS_MACH_4010: strcat (buf, ", 4010"); break;
3629 case E_MIPS_MACH_4100: strcat (buf, ", 4100"); break;
3630 case E_MIPS_MACH_4111: strcat (buf, ", 4111"); break;
3631 case E_MIPS_MACH_4120: strcat (buf, ", 4120"); break;
3632 case E_MIPS_MACH_4650: strcat (buf, ", 4650"); break;
3633 case E_MIPS_MACH_5400: strcat (buf, ", 5400"); break;
3634 case E_MIPS_MACH_5500: strcat (buf, ", 5500"); break;
3635 case E_MIPS_MACH_5900: strcat (buf, ", 5900"); break;
3636 case E_MIPS_MACH_SB1: strcat (buf, ", sb1"); break;
3637 case E_MIPS_MACH_9000: strcat (buf, ", 9000"); break;
3638 case E_MIPS_MACH_LS2E: strcat (buf, ", loongson-2e"); break;
3639 case E_MIPS_MACH_LS2F: strcat (buf, ", loongson-2f"); break;
3640 case E_MIPS_MACH_GS464: strcat (buf, ", gs464"); break;
3641 case E_MIPS_MACH_GS464E: strcat (buf, ", gs464e"); break;
3642 case E_MIPS_MACH_GS264E: strcat (buf, ", gs264e"); break;
3643 case E_MIPS_MACH_OCTEON: strcat (buf, ", octeon"); break;
3644 case E_MIPS_MACH_OCTEON2: strcat (buf, ", octeon2"); break;
3645 case E_MIPS_MACH_OCTEON3: strcat (buf, ", octeon3"); break;
3646 case E_MIPS_MACH_XLR: strcat (buf, ", xlr"); break;
3647 case E_MIPS_MACH_IAMR2: strcat (buf, ", interaptiv-mr2"); break;
3648 case 0:
3649 /* We simply ignore the field in this case to avoid confusion:
3650 MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
3651 extension. */
3652 break;
3653 default: strcat (buf, _(", unknown CPU")); break;
3654 }
3655
3656 switch ((e_flags & EF_MIPS_ABI))
3657 {
3658 case E_MIPS_ABI_O32: strcat (buf, ", o32"); break;
3659 case E_MIPS_ABI_O64: strcat (buf, ", o64"); break;
3660 case E_MIPS_ABI_EABI32: strcat (buf, ", eabi32"); break;
3661 case E_MIPS_ABI_EABI64: strcat (buf, ", eabi64"); break;
3662 case 0:
3663 /* We simply ignore the field in this case to avoid confusion:
3664 MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
3665 This means it is likely to be an o32 file, but not for
3666 sure. */
3667 break;
3668 default: strcat (buf, _(", unknown ABI")); break;
3669 }
3670
3671 if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
3672 strcat (buf, ", mdmx");
3673
3674 if (e_flags & EF_MIPS_ARCH_ASE_M16)
3675 strcat (buf, ", mips16");
3676
3677 if (e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
3678 strcat (buf, ", micromips");
3679
3680 switch ((e_flags & EF_MIPS_ARCH))
3681 {
3682 case E_MIPS_ARCH_1: strcat (buf, ", mips1"); break;
3683 case E_MIPS_ARCH_2: strcat (buf, ", mips2"); break;
3684 case E_MIPS_ARCH_3: strcat (buf, ", mips3"); break;
3685 case E_MIPS_ARCH_4: strcat (buf, ", mips4"); break;
3686 case E_MIPS_ARCH_5: strcat (buf, ", mips5"); break;
3687 case E_MIPS_ARCH_32: strcat (buf, ", mips32"); break;
3688 case E_MIPS_ARCH_32R2: strcat (buf, ", mips32r2"); break;
3689 case E_MIPS_ARCH_32R6: strcat (buf, ", mips32r6"); break;
3690 case E_MIPS_ARCH_64: strcat (buf, ", mips64"); break;
3691 case E_MIPS_ARCH_64R2: strcat (buf, ", mips64r2"); break;
3692 case E_MIPS_ARCH_64R6: strcat (buf, ", mips64r6"); break;
3693 default: strcat (buf, _(", unknown ISA")); break;
3694 }
3695 break;
3696
3697 case EM_NDS32:
3698 decode_NDS32_machine_flags (e_flags, buf, sizeof buf);
3699 break;
3700
3701 case EM_NFP:
3702 switch (EF_NFP_MACH (e_flags))
3703 {
3704 case E_NFP_MACH_3200:
3705 strcat (buf, ", NFP-32xx");
3706 break;
3707 case E_NFP_MACH_6000:
3708 strcat (buf, ", NFP-6xxx");
3709 break;
3710 }
3711 break;
3712
3713 case EM_RISCV:
3714 if (e_flags & EF_RISCV_RVC)
3715 strcat (buf, ", RVC");
3716
3717 if (e_flags & EF_RISCV_RVE)
3718 strcat (buf, ", RVE");
3719
3720 switch (e_flags & EF_RISCV_FLOAT_ABI)
3721 {
3722 case EF_RISCV_FLOAT_ABI_SOFT:
3723 strcat (buf, ", soft-float ABI");
3724 break;
3725
3726 case EF_RISCV_FLOAT_ABI_SINGLE:
3727 strcat (buf, ", single-float ABI");
3728 break;
3729
3730 case EF_RISCV_FLOAT_ABI_DOUBLE:
3731 strcat (buf, ", double-float ABI");
3732 break;
3733
3734 case EF_RISCV_FLOAT_ABI_QUAD:
3735 strcat (buf, ", quad-float ABI");
3736 break;
3737 }
3738 break;
3739
3740 case EM_SH:
3741 switch ((e_flags & EF_SH_MACH_MASK))
3742 {
3743 case EF_SH1: strcat (buf, ", sh1"); break;
3744 case EF_SH2: strcat (buf, ", sh2"); break;
3745 case EF_SH3: strcat (buf, ", sh3"); break;
3746 case EF_SH_DSP: strcat (buf, ", sh-dsp"); break;
3747 case EF_SH3_DSP: strcat (buf, ", sh3-dsp"); break;
3748 case EF_SH4AL_DSP: strcat (buf, ", sh4al-dsp"); break;
3749 case EF_SH3E: strcat (buf, ", sh3e"); break;
3750 case EF_SH4: strcat (buf, ", sh4"); break;
3751 case EF_SH5: strcat (buf, ", sh5"); break;
3752 case EF_SH2E: strcat (buf, ", sh2e"); break;
3753 case EF_SH4A: strcat (buf, ", sh4a"); break;
3754 case EF_SH2A: strcat (buf, ", sh2a"); break;
3755 case EF_SH4_NOFPU: strcat (buf, ", sh4-nofpu"); break;
3756 case EF_SH4A_NOFPU: strcat (buf, ", sh4a-nofpu"); break;
3757 case EF_SH2A_NOFPU: strcat (buf, ", sh2a-nofpu"); break;
3758 case EF_SH3_NOMMU: strcat (buf, ", sh3-nommu"); break;
3759 case EF_SH4_NOMMU_NOFPU: strcat (buf, ", sh4-nommu-nofpu"); break;
3760 case EF_SH2A_SH4_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh4-nommu-nofpu"); break;
3761 case EF_SH2A_SH3_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh3-nommu"); break;
3762 case EF_SH2A_SH4: strcat (buf, ", sh2a-or-sh4"); break;
3763 case EF_SH2A_SH3E: strcat (buf, ", sh2a-or-sh3e"); break;
3764 default: strcat (buf, _(", unknown ISA")); break;
3765 }
3766
3767 if (e_flags & EF_SH_PIC)
3768 strcat (buf, ", pic");
3769
3770 if (e_flags & EF_SH_FDPIC)
3771 strcat (buf, ", fdpic");
3772 break;
3773
3774 case EM_OR1K:
3775 if (e_flags & EF_OR1K_NODELAY)
3776 strcat (buf, ", no delay");
3777 break;
3778
3779 case EM_SPARCV9:
3780 if (e_flags & EF_SPARC_32PLUS)
3781 strcat (buf, ", v8+");
3782
3783 if (e_flags & EF_SPARC_SUN_US1)
3784 strcat (buf, ", ultrasparcI");
3785
3786 if (e_flags & EF_SPARC_SUN_US3)
3787 strcat (buf, ", ultrasparcIII");
3788
3789 if (e_flags & EF_SPARC_HAL_R1)
3790 strcat (buf, ", halr1");
3791
3792 if (e_flags & EF_SPARC_LEDATA)
3793 strcat (buf, ", ledata");
3794
3795 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
3796 strcat (buf, ", tso");
3797
3798 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
3799 strcat (buf, ", pso");
3800
3801 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
3802 strcat (buf, ", rmo");
3803 break;
3804
3805 case EM_PARISC:
3806 switch (e_flags & EF_PARISC_ARCH)
3807 {
3808 case EFA_PARISC_1_0:
3809 strcpy (buf, ", PA-RISC 1.0");
3810 break;
3811 case EFA_PARISC_1_1:
3812 strcpy (buf, ", PA-RISC 1.1");
3813 break;
3814 case EFA_PARISC_2_0:
3815 strcpy (buf, ", PA-RISC 2.0");
3816 break;
3817 default:
3818 break;
3819 }
3820 if (e_flags & EF_PARISC_TRAPNIL)
3821 strcat (buf, ", trapnil");
3822 if (e_flags & EF_PARISC_EXT)
3823 strcat (buf, ", ext");
3824 if (e_flags & EF_PARISC_LSB)
3825 strcat (buf, ", lsb");
3826 if (e_flags & EF_PARISC_WIDE)
3827 strcat (buf, ", wide");
3828 if (e_flags & EF_PARISC_NO_KABP)
3829 strcat (buf, ", no kabp");
3830 if (e_flags & EF_PARISC_LAZYSWAP)
3831 strcat (buf, ", lazyswap");
3832 break;
3833
3834 case EM_PJ:
3835 case EM_PJ_OLD:
3836 if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
3837 strcat (buf, ", new calling convention");
3838
3839 if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
3840 strcat (buf, ", gnu calling convention");
3841 break;
3842
3843 case EM_IA_64:
3844 if ((e_flags & EF_IA_64_ABI64))
3845 strcat (buf, ", 64-bit");
3846 else
3847 strcat (buf, ", 32-bit");
3848 if ((e_flags & EF_IA_64_REDUCEDFP))
3849 strcat (buf, ", reduced fp model");
3850 if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
3851 strcat (buf, ", no function descriptors, constant gp");
3852 else if ((e_flags & EF_IA_64_CONS_GP))
3853 strcat (buf, ", constant gp");
3854 if ((e_flags & EF_IA_64_ABSOLUTE))
3855 strcat (buf, ", absolute");
3856 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
3857 {
3858 if ((e_flags & EF_IA_64_VMS_LINKAGES))
3859 strcat (buf, ", vms_linkages");
3860 switch ((e_flags & EF_IA_64_VMS_COMCOD))
3861 {
3862 case EF_IA_64_VMS_COMCOD_SUCCESS:
3863 break;
3864 case EF_IA_64_VMS_COMCOD_WARNING:
3865 strcat (buf, ", warning");
3866 break;
3867 case EF_IA_64_VMS_COMCOD_ERROR:
3868 strcat (buf, ", error");
3869 break;
3870 case EF_IA_64_VMS_COMCOD_ABORT:
3871 strcat (buf, ", abort");
3872 break;
3873 default:
3874 warn (_("Unrecognised IA64 VMS Command Code: %x\n"),
3875 e_flags & EF_IA_64_VMS_COMCOD);
3876 strcat (buf, ", <unknown>");
3877 }
3878 }
3879 break;
3880
3881 case EM_VAX:
3882 if ((e_flags & EF_VAX_NONPIC))
3883 strcat (buf, ", non-PIC");
3884 if ((e_flags & EF_VAX_DFLOAT))
3885 strcat (buf, ", D-Float");
3886 if ((e_flags & EF_VAX_GFLOAT))
3887 strcat (buf, ", G-Float");
3888 break;
3889
3890 case EM_VISIUM:
3891 if (e_flags & EF_VISIUM_ARCH_MCM)
3892 strcat (buf, ", mcm");
3893 else if (e_flags & EF_VISIUM_ARCH_MCM24)
3894 strcat (buf, ", mcm24");
3895 if (e_flags & EF_VISIUM_ARCH_GR6)
3896 strcat (buf, ", gr6");
3897 break;
3898
3899 case EM_RL78:
3900 switch (e_flags & E_FLAG_RL78_CPU_MASK)
3901 {
3902 case E_FLAG_RL78_ANY_CPU: break;
3903 case E_FLAG_RL78_G10: strcat (buf, ", G10"); break;
3904 case E_FLAG_RL78_G13: strcat (buf, ", G13"); break;
3905 case E_FLAG_RL78_G14: strcat (buf, ", G14"); break;
3906 }
3907 if (e_flags & E_FLAG_RL78_64BIT_DOUBLES)
3908 strcat (buf, ", 64-bit doubles");
3909 break;
3910
3911 case EM_RX:
3912 if (e_flags & E_FLAG_RX_64BIT_DOUBLES)
3913 strcat (buf, ", 64-bit doubles");
3914 if (e_flags & E_FLAG_RX_DSP)
3915 strcat (buf, ", dsp");
3916 if (e_flags & E_FLAG_RX_PID)
3917 strcat (buf, ", pid");
3918 if (e_flags & E_FLAG_RX_ABI)
3919 strcat (buf, ", RX ABI");
3920 if (e_flags & E_FLAG_RX_SINSNS_SET)
3921 strcat (buf, e_flags & E_FLAG_RX_SINSNS_YES
3922 ? ", uses String instructions" : ", bans String instructions");
3923 if (e_flags & E_FLAG_RX_V2)
3924 strcat (buf, ", V2");
3925 if (e_flags & E_FLAG_RX_V3)
3926 strcat (buf, ", V3");
3927 break;
3928
3929 case EM_S390:
3930 if (e_flags & EF_S390_HIGH_GPRS)
3931 strcat (buf, ", highgprs");
3932 break;
3933
3934 case EM_TI_C6000:
3935 if ((e_flags & EF_C6000_REL))
3936 strcat (buf, ", relocatable module");
3937 break;
3938
3939 case EM_MSP430:
3940 strcat (buf, _(": architecture variant: "));
3941 switch (e_flags & EF_MSP430_MACH)
3942 {
3943 case E_MSP430_MACH_MSP430x11: strcat (buf, "MSP430x11"); break;
3944 case E_MSP430_MACH_MSP430x11x1 : strcat (buf, "MSP430x11x1 "); break;
3945 case E_MSP430_MACH_MSP430x12: strcat (buf, "MSP430x12"); break;
3946 case E_MSP430_MACH_MSP430x13: strcat (buf, "MSP430x13"); break;
3947 case E_MSP430_MACH_MSP430x14: strcat (buf, "MSP430x14"); break;
3948 case E_MSP430_MACH_MSP430x15: strcat (buf, "MSP430x15"); break;
3949 case E_MSP430_MACH_MSP430x16: strcat (buf, "MSP430x16"); break;
3950 case E_MSP430_MACH_MSP430x31: strcat (buf, "MSP430x31"); break;
3951 case E_MSP430_MACH_MSP430x32: strcat (buf, "MSP430x32"); break;
3952 case E_MSP430_MACH_MSP430x33: strcat (buf, "MSP430x33"); break;
3953 case E_MSP430_MACH_MSP430x41: strcat (buf, "MSP430x41"); break;
3954 case E_MSP430_MACH_MSP430x42: strcat (buf, "MSP430x42"); break;
3955 case E_MSP430_MACH_MSP430x43: strcat (buf, "MSP430x43"); break;
3956 case E_MSP430_MACH_MSP430x44: strcat (buf, "MSP430x44"); break;
3957 case E_MSP430_MACH_MSP430X : strcat (buf, "MSP430X"); break;
3958 default:
3959 strcat (buf, _(": unknown")); break;
3960 }
3961
3962 if (e_flags & ~ EF_MSP430_MACH)
3963 strcat (buf, _(": unknown extra flag bits also present"));
3964 break;
3965
3966 case EM_Z80:
3967 switch (e_flags & EF_Z80_MACH_MSK)
3968 {
3969 case EF_Z80_MACH_Z80: strcat (buf, ", Z80"); break;
3970 case EF_Z80_MACH_Z180: strcat (buf, ", Z180"); break;
3971 case EF_Z80_MACH_R800: strcat (buf, ", R800"); break;
3972 case EF_Z80_MACH_EZ80_Z80: strcat (buf, ", EZ80"); break;
3973 case EF_Z80_MACH_EZ80_ADL: strcat (buf, ", EZ80, ADL"); break;
3974 case EF_Z80_MACH_GBZ80: strcat (buf, ", GBZ80"); break;
3975 case EF_Z80_MACH_Z80N: strcat (buf, ", Z80N"); break;
3976 default:
3977 strcat (buf, _(", unknown")); break;
3978 }
3979 break;
3980 case EM_LOONGARCH:
3981 if (EF_LOONGARCH_IS_LP64 (e_flags))
3982 strcat (buf, ", LP64");
3983 else if (EF_LOONGARCH_IS_ILP32 (e_flags))
3984 strcat (buf, ", ILP32");
3985
3986 if (EF_LOONGARCH_IS_SOFT_FLOAT (e_flags))
3987 strcat (buf, ", SOFT-FLOAT");
3988 else if (EF_LOONGARCH_IS_SINGLE_FLOAT (e_flags))
3989 strcat (buf, ", SINGLE-FLOAT");
3990 else if (EF_LOONGARCH_IS_DOUBLE_FLOAT (e_flags))
3991 strcat (buf, ", DOUBLE-FLOAT");
3992
3993 break;
3994 }
3995 }
3996
3997 return buf;
3998 }
3999
4000 static const char *
4001 get_osabi_name (Filedata * filedata, unsigned int osabi)
4002 {
4003 static char buff[32];
4004
4005 switch (osabi)
4006 {
4007 case ELFOSABI_NONE: return "UNIX - System V";
4008 case ELFOSABI_HPUX: return "UNIX - HP-UX";
4009 case ELFOSABI_NETBSD: return "UNIX - NetBSD";
4010 case ELFOSABI_GNU: return "UNIX - GNU";
4011 case ELFOSABI_SOLARIS: return "UNIX - Solaris";
4012 case ELFOSABI_AIX: return "UNIX - AIX";
4013 case ELFOSABI_IRIX: return "UNIX - IRIX";
4014 case ELFOSABI_FREEBSD: return "UNIX - FreeBSD";
4015 case ELFOSABI_TRU64: return "UNIX - TRU64";
4016 case ELFOSABI_MODESTO: return "Novell - Modesto";
4017 case ELFOSABI_OPENBSD: return "UNIX - OpenBSD";
4018 case ELFOSABI_OPENVMS: return "VMS - OpenVMS";
4019 case ELFOSABI_NSK: return "HP - Non-Stop Kernel";
4020 case ELFOSABI_AROS: return "AROS";
4021 case ELFOSABI_FENIXOS: return "FenixOS";
4022 case ELFOSABI_CLOUDABI: return "Nuxi CloudABI";
4023 case ELFOSABI_OPENVOS: return "Stratus Technologies OpenVOS";
4024 default:
4025 if (osabi >= 64)
4026 switch (filedata->file_header.e_machine)
4027 {
4028 case EM_ARM:
4029 switch (osabi)
4030 {
4031 case ELFOSABI_ARM: return "ARM";
4032 case ELFOSABI_ARM_FDPIC: return "ARM FDPIC";
4033 default:
4034 break;
4035 }
4036 break;
4037
4038 case EM_MSP430:
4039 case EM_MSP430_OLD:
4040 case EM_VISIUM:
4041 switch (osabi)
4042 {
4043 case ELFOSABI_STANDALONE: return _("Standalone App");
4044 default:
4045 break;
4046 }
4047 break;
4048
4049 case EM_TI_C6000:
4050 switch (osabi)
4051 {
4052 case ELFOSABI_C6000_ELFABI: return _("Bare-metal C6000");
4053 case ELFOSABI_C6000_LINUX: return "Linux C6000";
4054 default:
4055 break;
4056 }
4057 break;
4058
4059 default:
4060 break;
4061 }
4062 snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
4063 return buff;
4064 }
4065 }
4066
4067 static const char *
4068 get_aarch64_segment_type (unsigned long type)
4069 {
4070 switch (type)
4071 {
4072 case PT_AARCH64_ARCHEXT: return "AARCH64_ARCHEXT";
4073 default: return NULL;
4074 }
4075 }
4076
4077 static const char *
4078 get_arm_segment_type (unsigned long type)
4079 {
4080 switch (type)
4081 {
4082 case PT_ARM_EXIDX: return "EXIDX";
4083 default: return NULL;
4084 }
4085 }
4086
4087 static const char *
4088 get_s390_segment_type (unsigned long type)
4089 {
4090 switch (type)
4091 {
4092 case PT_S390_PGSTE: return "S390_PGSTE";
4093 default: return NULL;
4094 }
4095 }
4096
4097 static const char *
4098 get_mips_segment_type (unsigned long type)
4099 {
4100 switch (type)
4101 {
4102 case PT_MIPS_REGINFO: return "REGINFO";
4103 case PT_MIPS_RTPROC: return "RTPROC";
4104 case PT_MIPS_OPTIONS: return "OPTIONS";
4105 case PT_MIPS_ABIFLAGS: return "ABIFLAGS";
4106 default: return NULL;
4107 }
4108 }
4109
4110 static const char *
4111 get_parisc_segment_type (unsigned long type)
4112 {
4113 switch (type)
4114 {
4115 case PT_PARISC_ARCHEXT: return "PARISC_ARCHEXT";
4116 case PT_PARISC_UNWIND: return "PARISC_UNWIND";
4117 case PT_PARISC_WEAKORDER: return "PARISC_WEAKORDER";
4118 default: return NULL;
4119 }
4120 }
4121
4122 static const char *
4123 get_ia64_segment_type (unsigned long type)
4124 {
4125 switch (type)
4126 {
4127 case PT_IA_64_ARCHEXT: return "IA_64_ARCHEXT";
4128 case PT_IA_64_UNWIND: return "IA_64_UNWIND";
4129 default: return NULL;
4130 }
4131 }
4132
4133 static const char *
4134 get_tic6x_segment_type (unsigned long type)
4135 {
4136 switch (type)
4137 {
4138 case PT_C6000_PHATTR: return "C6000_PHATTR";
4139 default: return NULL;
4140 }
4141 }
4142
4143 static const char *
4144 get_riscv_segment_type (unsigned long type)
4145 {
4146 switch (type)
4147 {
4148 case PT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
4149 default: return NULL;
4150 }
4151 }
4152
4153 static const char *
4154 get_hpux_segment_type (unsigned long type, unsigned e_machine)
4155 {
4156 if (e_machine == EM_PARISC)
4157 switch (type)
4158 {
4159 case PT_HP_TLS: return "HP_TLS";
4160 case PT_HP_CORE_NONE: return "HP_CORE_NONE";
4161 case PT_HP_CORE_VERSION: return "HP_CORE_VERSION";
4162 case PT_HP_CORE_KERNEL: return "HP_CORE_KERNEL";
4163 case PT_HP_CORE_COMM: return "HP_CORE_COMM";
4164 case PT_HP_CORE_PROC: return "HP_CORE_PROC";
4165 case PT_HP_CORE_LOADABLE: return "HP_CORE_LOADABLE";
4166 case PT_HP_CORE_STACK: return "HP_CORE_STACK";
4167 case PT_HP_CORE_SHM: return "HP_CORE_SHM";
4168 case PT_HP_CORE_MMF: return "HP_CORE_MMF";
4169 case PT_HP_PARALLEL: return "HP_PARALLEL";
4170 case PT_HP_FASTBIND: return "HP_FASTBIND";
4171 case PT_HP_OPT_ANNOT: return "HP_OPT_ANNOT";
4172 case PT_HP_HSL_ANNOT: return "HP_HSL_ANNOT";
4173 case PT_HP_STACK: return "HP_STACK";
4174 case PT_HP_CORE_UTSNAME: return "HP_CORE_UTSNAME";
4175 default: return NULL;
4176 }
4177
4178 if (e_machine == EM_IA_64)
4179 switch (type)
4180 {
4181 case PT_HP_TLS: return "HP_TLS";
4182 case PT_IA_64_HP_OPT_ANOT: return "HP_OPT_ANNOT";
4183 case PT_IA_64_HP_HSL_ANOT: return "HP_HSL_ANNOT";
4184 case PT_IA_64_HP_STACK: return "HP_STACK";
4185 default: return NULL;
4186 }
4187
4188 return NULL;
4189 }
4190
4191 static const char *
4192 get_solaris_segment_type (unsigned long type)
4193 {
4194 switch (type)
4195 {
4196 case 0x6464e550: return "PT_SUNW_UNWIND";
4197 case 0x6474e550: return "PT_SUNW_EH_FRAME";
4198 case 0x6ffffff7: return "PT_LOSUNW";
4199 case 0x6ffffffa: return "PT_SUNWBSS";
4200 case 0x6ffffffb: return "PT_SUNWSTACK";
4201 case 0x6ffffffc: return "PT_SUNWDTRACE";
4202 case 0x6ffffffd: return "PT_SUNWCAP";
4203 case 0x6fffffff: return "PT_HISUNW";
4204 default: return NULL;
4205 }
4206 }
4207
4208 static const char *
4209 get_segment_type (Filedata * filedata, unsigned long p_type)
4210 {
4211 static char buff[32];
4212
4213 switch (p_type)
4214 {
4215 case PT_NULL: return "NULL";
4216 case PT_LOAD: return "LOAD";
4217 case PT_DYNAMIC: return "DYNAMIC";
4218 case PT_INTERP: return "INTERP";
4219 case PT_NOTE: return "NOTE";
4220 case PT_SHLIB: return "SHLIB";
4221 case PT_PHDR: return "PHDR";
4222 case PT_TLS: return "TLS";
4223 case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
4224 case PT_GNU_STACK: return "GNU_STACK";
4225 case PT_GNU_RELRO: return "GNU_RELRO";
4226 case PT_GNU_PROPERTY: return "GNU_PROPERTY";
4227
4228 case PT_OPENBSD_RANDOMIZE: return "OPENBSD_RANDOMIZE";
4229 case PT_OPENBSD_WXNEEDED: return "OPENBSD_WXNEEDED";
4230 case PT_OPENBSD_BOOTDATA: return "OPENBSD_BOOTDATA";
4231
4232 default:
4233 if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
4234 {
4235 const char * result;
4236
4237 switch (filedata->file_header.e_machine)
4238 {
4239 case EM_AARCH64:
4240 result = get_aarch64_segment_type (p_type);
4241 break;
4242 case EM_ARM:
4243 result = get_arm_segment_type (p_type);
4244 break;
4245 case EM_MIPS:
4246 case EM_MIPS_RS3_LE:
4247 result = get_mips_segment_type (p_type);
4248 break;
4249 case EM_PARISC:
4250 result = get_parisc_segment_type (p_type);
4251 break;
4252 case EM_IA_64:
4253 result = get_ia64_segment_type (p_type);
4254 break;
4255 case EM_TI_C6000:
4256 result = get_tic6x_segment_type (p_type);
4257 break;
4258 case EM_S390:
4259 case EM_S390_OLD:
4260 result = get_s390_segment_type (p_type);
4261 break;
4262 case EM_RISCV:
4263 result = get_riscv_segment_type (p_type);
4264 break;
4265 default:
4266 result = NULL;
4267 break;
4268 }
4269
4270 if (result != NULL)
4271 return result;
4272
4273 sprintf (buff, "LOPROC+%#lx", p_type - PT_LOPROC);
4274 }
4275 else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
4276 {
4277 const char * result = NULL;
4278
4279 switch (filedata->file_header.e_ident[EI_OSABI])
4280 {
4281 case ELFOSABI_GNU:
4282 case ELFOSABI_FREEBSD:
4283 if (p_type >= PT_GNU_MBIND_LO && p_type <= PT_GNU_MBIND_HI)
4284 {
4285 sprintf (buff, "GNU_MBIND+%#lx", p_type - PT_GNU_MBIND_LO);
4286 result = buff;
4287 }
4288 break;
4289 case ELFOSABI_HPUX:
4290 result = get_hpux_segment_type (p_type,
4291 filedata->file_header.e_machine);
4292 break;
4293 case ELFOSABI_SOLARIS:
4294 result = get_solaris_segment_type (p_type);
4295 break;
4296 default:
4297 break;
4298 }
4299 if (result != NULL)
4300 return result;
4301
4302 sprintf (buff, "LOOS+%#lx", p_type - PT_LOOS);
4303 }
4304 else
4305 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
4306
4307 return buff;
4308 }
4309 }
4310
4311 static const char *
4312 get_arc_section_type_name (unsigned int sh_type)
4313 {
4314 switch (sh_type)
4315 {
4316 case SHT_ARC_ATTRIBUTES: return "ARC_ATTRIBUTES";
4317 default:
4318 break;
4319 }
4320 return NULL;
4321 }
4322
4323 static const char *
4324 get_mips_section_type_name (unsigned int sh_type)
4325 {
4326 switch (sh_type)
4327 {
4328 case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
4329 case SHT_MIPS_MSYM: return "MIPS_MSYM";
4330 case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
4331 case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
4332 case SHT_MIPS_UCODE: return "MIPS_UCODE";
4333 case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
4334 case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
4335 case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
4336 case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
4337 case SHT_MIPS_RELD: return "MIPS_RELD";
4338 case SHT_MIPS_IFACE: return "MIPS_IFACE";
4339 case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
4340 case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
4341 case SHT_MIPS_SHDR: return "MIPS_SHDR";
4342 case SHT_MIPS_FDESC: return "MIPS_FDESC";
4343 case SHT_MIPS_EXTSYM: return "MIPS_EXTSYM";
4344 case SHT_MIPS_DENSE: return "MIPS_DENSE";
4345 case SHT_MIPS_PDESC: return "MIPS_PDESC";
4346 case SHT_MIPS_LOCSYM: return "MIPS_LOCSYM";
4347 case SHT_MIPS_AUXSYM: return "MIPS_AUXSYM";
4348 case SHT_MIPS_OPTSYM: return "MIPS_OPTSYM";
4349 case SHT_MIPS_LOCSTR: return "MIPS_LOCSTR";
4350 case SHT_MIPS_LINE: return "MIPS_LINE";
4351 case SHT_MIPS_RFDESC: return "MIPS_RFDESC";
4352 case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
4353 case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
4354 case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
4355 case SHT_MIPS_DWARF: return "MIPS_DWARF";
4356 case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
4357 case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
4358 case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
4359 case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
4360 case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
4361 case SHT_MIPS_XLATE: return "MIPS_XLATE";
4362 case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
4363 case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
4364 case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
4365 case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
4366 case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
4367 case SHT_MIPS_ABIFLAGS: return "MIPS_ABIFLAGS";
4368 case SHT_MIPS_XHASH: return "MIPS_XHASH";
4369 default:
4370 break;
4371 }
4372 return NULL;
4373 }
4374
4375 static const char *
4376 get_parisc_section_type_name (unsigned int sh_type)
4377 {
4378 switch (sh_type)
4379 {
4380 case SHT_PARISC_EXT: return "PARISC_EXT";
4381 case SHT_PARISC_UNWIND: return "PARISC_UNWIND";
4382 case SHT_PARISC_DOC: return "PARISC_DOC";
4383 case SHT_PARISC_ANNOT: return "PARISC_ANNOT";
4384 case SHT_PARISC_SYMEXTN: return "PARISC_SYMEXTN";
4385 case SHT_PARISC_STUBS: return "PARISC_STUBS";
4386 case SHT_PARISC_DLKM: return "PARISC_DLKM";
4387 default: return NULL;
4388 }
4389 }
4390
4391 static const char *
4392 get_ia64_section_type_name (Filedata * filedata, unsigned int sh_type)
4393 {
4394 /* If the top 8 bits are 0x78 the next 8 are the os/abi ID. */
4395 if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
4396 return get_osabi_name (filedata, (sh_type & 0x00FF0000) >> 16);
4397
4398 switch (sh_type)
4399 {
4400 case SHT_IA_64_EXT: return "IA_64_EXT";
4401 case SHT_IA_64_UNWIND: return "IA_64_UNWIND";
4402 case SHT_IA_64_PRIORITY_INIT: return "IA_64_PRIORITY_INIT";
4403 case SHT_IA_64_VMS_TRACE: return "VMS_TRACE";
4404 case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
4405 case SHT_IA_64_VMS_DEBUG: return "VMS_DEBUG";
4406 case SHT_IA_64_VMS_DEBUG_STR: return "VMS_DEBUG_STR";
4407 case SHT_IA_64_VMS_LINKAGES: return "VMS_LINKAGES";
4408 case SHT_IA_64_VMS_SYMBOL_VECTOR: return "VMS_SYMBOL_VECTOR";
4409 case SHT_IA_64_VMS_FIXUP: return "VMS_FIXUP";
4410 default:
4411 break;
4412 }
4413 return NULL;
4414 }
4415
4416 static const char *
4417 get_x86_64_section_type_name (unsigned int sh_type)
4418 {
4419 switch (sh_type)
4420 {
4421 case SHT_X86_64_UNWIND: return "X86_64_UNWIND";
4422 default: return NULL;
4423 }
4424 }
4425
4426 static const char *
4427 get_aarch64_section_type_name (unsigned int sh_type)
4428 {
4429 switch (sh_type)
4430 {
4431 case SHT_AARCH64_ATTRIBUTES: return "AARCH64_ATTRIBUTES";
4432 default: return NULL;
4433 }
4434 }
4435
4436 static const char *
4437 get_arm_section_type_name (unsigned int sh_type)
4438 {
4439 switch (sh_type)
4440 {
4441 case SHT_ARM_EXIDX: return "ARM_EXIDX";
4442 case SHT_ARM_PREEMPTMAP: return "ARM_PREEMPTMAP";
4443 case SHT_ARM_ATTRIBUTES: return "ARM_ATTRIBUTES";
4444 case SHT_ARM_DEBUGOVERLAY: return "ARM_DEBUGOVERLAY";
4445 case SHT_ARM_OVERLAYSECTION: return "ARM_OVERLAYSECTION";
4446 default: return NULL;
4447 }
4448 }
4449
4450 static const char *
4451 get_tic6x_section_type_name (unsigned int sh_type)
4452 {
4453 switch (sh_type)
4454 {
4455 case SHT_C6000_UNWIND: return "C6000_UNWIND";
4456 case SHT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
4457 case SHT_C6000_ATTRIBUTES: return "C6000_ATTRIBUTES";
4458 case SHT_TI_ICODE: return "TI_ICODE";
4459 case SHT_TI_XREF: return "TI_XREF";
4460 case SHT_TI_HANDLER: return "TI_HANDLER";
4461 case SHT_TI_INITINFO: return "TI_INITINFO";
4462 case SHT_TI_PHATTRS: return "TI_PHATTRS";
4463 default: return NULL;
4464 }
4465 }
4466
4467 static const char *
4468 get_msp430_section_type_name (unsigned int sh_type)
4469 {
4470 switch (sh_type)
4471 {
4472 case SHT_MSP430_SEC_FLAGS: return "MSP430_SEC_FLAGS";
4473 case SHT_MSP430_SYM_ALIASES: return "MSP430_SYM_ALIASES";
4474 case SHT_MSP430_ATTRIBUTES: return "MSP430_ATTRIBUTES";
4475 default: return NULL;
4476 }
4477 }
4478
4479 static const char *
4480 get_nfp_section_type_name (unsigned int sh_type)
4481 {
4482 switch (sh_type)
4483 {
4484 case SHT_NFP_MECONFIG: return "NFP_MECONFIG";
4485 case SHT_NFP_INITREG: return "NFP_INITREG";
4486 case SHT_NFP_UDEBUG: return "NFP_UDEBUG";
4487 default: return NULL;
4488 }
4489 }
4490
4491 static const char *
4492 get_v850_section_type_name (unsigned int sh_type)
4493 {
4494 switch (sh_type)
4495 {
4496 case SHT_V850_SCOMMON: return "V850 Small Common";
4497 case SHT_V850_TCOMMON: return "V850 Tiny Common";
4498 case SHT_V850_ZCOMMON: return "V850 Zero Common";
4499 case SHT_RENESAS_IOP: return "RENESAS IOP";
4500 case SHT_RENESAS_INFO: return "RENESAS INFO";
4501 default: return NULL;
4502 }
4503 }
4504
4505 static const char *
4506 get_riscv_section_type_name (unsigned int sh_type)
4507 {
4508 switch (sh_type)
4509 {
4510 case SHT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
4511 default: return NULL;
4512 }
4513 }
4514
4515 static const char *
4516 get_csky_section_type_name (unsigned int sh_type)
4517 {
4518 switch (sh_type)
4519 {
4520 case SHT_CSKY_ATTRIBUTES: return "CSKY_ATTRIBUTES";
4521 default: return NULL;
4522 }
4523 }
4524
4525 static const char *
4526 get_section_type_name (Filedata * filedata, unsigned int sh_type)
4527 {
4528 static char buff[32];
4529 const char * result;
4530
4531 switch (sh_type)
4532 {
4533 case SHT_NULL: return "NULL";
4534 case SHT_PROGBITS: return "PROGBITS";
4535 case SHT_SYMTAB: return "SYMTAB";
4536 case SHT_STRTAB: return "STRTAB";
4537 case SHT_RELA: return "RELA";
4538 case SHT_HASH: return "HASH";
4539 case SHT_DYNAMIC: return "DYNAMIC";
4540 case SHT_NOTE: return "NOTE";
4541 case SHT_NOBITS: return "NOBITS";
4542 case SHT_REL: return "REL";
4543 case SHT_SHLIB: return "SHLIB";
4544 case SHT_DYNSYM: return "DYNSYM";
4545 case SHT_INIT_ARRAY: return "INIT_ARRAY";
4546 case SHT_FINI_ARRAY: return "FINI_ARRAY";
4547 case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
4548 case SHT_GNU_HASH: return "GNU_HASH";
4549 case SHT_GROUP: return "GROUP";
4550 case SHT_SYMTAB_SHNDX: return "SYMTAB SECTION INDICES";
4551 case SHT_GNU_verdef: return "VERDEF";
4552 case SHT_GNU_verneed: return "VERNEED";
4553 case SHT_GNU_versym: return "VERSYM";
4554 case 0x6ffffff0: return "VERSYM";
4555 case 0x6ffffffc: return "VERDEF";
4556 case 0x7ffffffd: return "AUXILIARY";
4557 case 0x7fffffff: return "FILTER";
4558 case SHT_GNU_LIBLIST: return "GNU_LIBLIST";
4559
4560 default:
4561 if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
4562 {
4563 switch (filedata->file_header.e_machine)
4564 {
4565 case EM_ARC:
4566 case EM_ARC_COMPACT:
4567 case EM_ARC_COMPACT2:
4568 result = get_arc_section_type_name (sh_type);
4569 break;
4570 case EM_MIPS:
4571 case EM_MIPS_RS3_LE:
4572 result = get_mips_section_type_name (sh_type);
4573 break;
4574 case EM_PARISC:
4575 result = get_parisc_section_type_name (sh_type);
4576 break;
4577 case EM_IA_64:
4578 result = get_ia64_section_type_name (filedata, sh_type);
4579 break;
4580 case EM_X86_64:
4581 case EM_L1OM:
4582 case EM_K1OM:
4583 result = get_x86_64_section_type_name (sh_type);
4584 break;
4585 case EM_AARCH64:
4586 result = get_aarch64_section_type_name (sh_type);
4587 break;
4588 case EM_ARM:
4589 result = get_arm_section_type_name (sh_type);
4590 break;
4591 case EM_TI_C6000:
4592 result = get_tic6x_section_type_name (sh_type);
4593 break;
4594 case EM_MSP430:
4595 result = get_msp430_section_type_name (sh_type);
4596 break;
4597 case EM_NFP:
4598 result = get_nfp_section_type_name (sh_type);
4599 break;
4600 case EM_V800:
4601 case EM_V850:
4602 case EM_CYGNUS_V850:
4603 result = get_v850_section_type_name (sh_type);
4604 break;
4605 case EM_RISCV:
4606 result = get_riscv_section_type_name (sh_type);
4607 break;
4608 case EM_CSKY:
4609 result = get_csky_section_type_name (sh_type);
4610 break;
4611 default:
4612 result = NULL;
4613 break;
4614 }
4615
4616 if (result != NULL)
4617 return result;
4618
4619 sprintf (buff, "LOPROC+%#x", sh_type - SHT_LOPROC);
4620 }
4621 else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
4622 {
4623 switch (filedata->file_header.e_machine)
4624 {
4625 case EM_IA_64:
4626 result = get_ia64_section_type_name (filedata, sh_type);
4627 break;
4628 default:
4629 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
4630 result = get_solaris_section_type (sh_type);
4631 else
4632 {
4633 switch (sh_type)
4634 {
4635 case SHT_GNU_INCREMENTAL_INPUTS: result = "GNU_INCREMENTAL_INPUTS"; break;
4636 case SHT_GNU_ATTRIBUTES: result = "GNU_ATTRIBUTES"; break;
4637 case SHT_GNU_HASH: result = "GNU_HASH"; break;
4638 case SHT_GNU_LIBLIST: result = "GNU_LIBLIST"; break;
4639 default:
4640 result = NULL;
4641 break;
4642 }
4643 }
4644 break;
4645 }
4646
4647 if (result != NULL)
4648 return result;
4649
4650 sprintf (buff, "LOOS+%#x", sh_type - SHT_LOOS);
4651 }
4652 else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
4653 {
4654 switch (filedata->file_header.e_machine)
4655 {
4656 case EM_V800:
4657 case EM_V850:
4658 case EM_CYGNUS_V850:
4659 result = get_v850_section_type_name (sh_type);
4660 break;
4661 default:
4662 result = NULL;
4663 break;
4664 }
4665
4666 if (result != NULL)
4667 return result;
4668
4669 sprintf (buff, "LOUSER+%#x", sh_type - SHT_LOUSER);
4670 }
4671 else
4672 /* This message is probably going to be displayed in a 15
4673 character wide field, so put the hex value first. */
4674 snprintf (buff, sizeof (buff), _("%08x: <unknown>"), sh_type);
4675
4676 return buff;
4677 }
4678 }
4679
4680 enum long_option_values
4681 {
4682 OPTION_DEBUG_DUMP = 512,
4683 OPTION_DYN_SYMS,
4684 OPTION_LTO_SYMS,
4685 OPTION_DWARF_DEPTH,
4686 OPTION_DWARF_START,
4687 OPTION_DWARF_CHECK,
4688 OPTION_CTF_DUMP,
4689 OPTION_CTF_PARENT,
4690 OPTION_CTF_SYMBOLS,
4691 OPTION_CTF_STRINGS,
4692 OPTION_WITH_SYMBOL_VERSIONS,
4693 OPTION_RECURSE_LIMIT,
4694 OPTION_NO_RECURSE_LIMIT,
4695 OPTION_NO_DEMANGLING,
4696 OPTION_SYM_BASE
4697 };
4698
4699 static struct option options[] =
4700 {
4701 /* Note - This table is alpha-sorted on the 'val'
4702 field in order to make adding new options easier. */
4703 {"arch-specific", no_argument, 0, 'A'},
4704 {"all", no_argument, 0, 'a'},
4705 {"demangle", optional_argument, 0, 'C'},
4706 {"archive-index", no_argument, 0, 'c'},
4707 {"use-dynamic", no_argument, 0, 'D'},
4708 {"dynamic", no_argument, 0, 'd'},
4709 {"headers", no_argument, 0, 'e'},
4710 {"section-groups", no_argument, 0, 'g'},
4711 {"help", no_argument, 0, 'H'},
4712 {"file-header", no_argument, 0, 'h'},
4713 {"histogram", no_argument, 0, 'I'},
4714 {"lint", no_argument, 0, 'L'},
4715 {"enable-checks", no_argument, 0, 'L'},
4716 {"program-headers", no_argument, 0, 'l'},
4717 {"segments", no_argument, 0, 'l'},
4718 {"full-section-name",no_argument, 0, 'N'},
4719 {"notes", no_argument, 0, 'n'},
4720 {"process-links", no_argument, 0, 'P'},
4721 {"string-dump", required_argument, 0, 'p'},
4722 {"relocated-dump", required_argument, 0, 'R'},
4723 {"relocs", no_argument, 0, 'r'},
4724 {"section-headers", no_argument, 0, 'S'},
4725 {"sections", no_argument, 0, 'S'},
4726 {"symbols", no_argument, 0, 's'},
4727 {"syms", no_argument, 0, 's'},
4728 {"silent-truncation",no_argument, 0, 'T'},
4729 {"section-details", no_argument, 0, 't'},
4730 {"unwind", no_argument, 0, 'u'},
4731 {"version-info", no_argument, 0, 'V'},
4732 {"version", no_argument, 0, 'v'},
4733 {"wide", no_argument, 0, 'W'},
4734 {"hex-dump", required_argument, 0, 'x'},
4735 {"decompress", no_argument, 0, 'z'},
4736
4737 {"no-demangle", no_argument, 0, OPTION_NO_DEMANGLING},
4738 {"recurse-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
4739 {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
4740 {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
4741 {"dyn-syms", no_argument, 0, OPTION_DYN_SYMS},
4742 {"lto-syms", no_argument, 0, OPTION_LTO_SYMS},
4743 {"debug-dump", optional_argument, 0, OPTION_DEBUG_DUMP},
4744 {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
4745 {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
4746 {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
4747 #ifdef ENABLE_LIBCTF
4748 {"ctf", required_argument, 0, OPTION_CTF_DUMP},
4749 {"ctf-symbols", required_argument, 0, OPTION_CTF_SYMBOLS},
4750 {"ctf-strings", required_argument, 0, OPTION_CTF_STRINGS},
4751 {"ctf-parent", required_argument, 0, OPTION_CTF_PARENT},
4752 #endif
4753 {"sym-base", optional_argument, 0, OPTION_SYM_BASE},
4754
4755 {0, no_argument, 0, 0}
4756 };
4757
4758 static void
4759 usage (FILE * stream)
4760 {
4761 fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
4762 fprintf (stream, _(" Display information about the contents of ELF format files\n"));
4763 fprintf (stream, _(" Options are:\n"));
4764 fprintf (stream, _("\
4765 -a --all Equivalent to: -h -l -S -s -r -d -V -A -I\n"));
4766 fprintf (stream, _("\
4767 -h --file-header Display the ELF file header\n"));
4768 fprintf (stream, _("\
4769 -l --program-headers Display the program headers\n"));
4770 fprintf (stream, _("\
4771 --segments An alias for --program-headers\n"));
4772 fprintf (stream, _("\
4773 -S --section-headers Display the sections' header\n"));
4774 fprintf (stream, _("\
4775 --sections An alias for --section-headers\n"));
4776 fprintf (stream, _("\
4777 -g --section-groups Display the section groups\n"));
4778 fprintf (stream, _("\
4779 -t --section-details Display the section details\n"));
4780 fprintf (stream, _("\
4781 -e --headers Equivalent to: -h -l -S\n"));
4782 fprintf (stream, _("\
4783 -s --syms Display the symbol table\n"));
4784 fprintf (stream, _("\
4785 --symbols An alias for --syms\n"));
4786 fprintf (stream, _("\
4787 --dyn-syms Display the dynamic symbol table\n"));
4788 fprintf (stream, _("\
4789 --lto-syms Display LTO symbol tables\n"));
4790 fprintf (stream, _("\
4791 --sym-base=[0|8|10|16] \n\
4792 Force base for symbol sizes. The options are \n\
4793 mixed (the default), octal, decimal, hexadecimal.\n"));
4794 fprintf (stream, _("\
4795 -C --demangle[=STYLE] Decode low-level symbol names into user-level names\n\
4796 The STYLE, if specified, can be `auto' (the default),\n\
4797 `gnu', `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
4798 or `gnat'\n"));
4799 fprintf (stream, _("\
4800 --no-demangle Do not demangle low-level symbol names. (default)\n"));
4801 fprintf (stream, _("\
4802 --recurse-limit Enable a demangling recursion limit. (default)\n"));
4803 fprintf (stream, _("\
4804 --no-recurse-limit Disable a demangling recursion limit\n"));
4805 fprintf (stream, _("\
4806 -n --notes Display the core notes (if present)\n"));
4807 fprintf (stream, _("\
4808 -r --relocs Display the relocations (if present)\n"));
4809 fprintf (stream, _("\
4810 -u --unwind Display the unwind info (if present)\n"));
4811 fprintf (stream, _("\
4812 -d --dynamic Display the dynamic section (if present)\n"));
4813 fprintf (stream, _("\
4814 -V --version-info Display the version sections (if present)\n"));
4815 fprintf (stream, _("\
4816 -A --arch-specific Display architecture specific information (if any)\n"));
4817 fprintf (stream, _("\
4818 -c --archive-index Display the symbol/file index in an archive\n"));
4819 fprintf (stream, _("\
4820 -D --use-dynamic Use the dynamic section info when displaying symbols\n"));
4821 fprintf (stream, _("\
4822 -L --lint|--enable-checks\n\
4823 Display warning messages for possible problems\n"));
4824 fprintf (stream, _("\
4825 -x --hex-dump=<number|name>\n\
4826 Dump the contents of section <number|name> as bytes\n"));
4827 fprintf (stream, _("\
4828 -p --string-dump=<number|name>\n\
4829 Dump the contents of section <number|name> as strings\n"));
4830 fprintf (stream, _("\
4831 -R --relocated-dump=<number|name>\n\
4832 Dump the relocated contents of section <number|name>\n"));
4833 fprintf (stream, _("\
4834 -z --decompress Decompress section before dumping it\n"));
4835 fprintf (stream, _("\
4836 -w --debug-dump[a/=abbrev, A/=addr, r/=aranges, c/=cu_index, L/=decodedline,\n\
4837 f/=frames, F/=frames-interp, g/=gdb_index, i/=info, o/=loc,\n\
4838 m/=macro, p/=pubnames, t/=pubtypes, R/=Ranges, l/=rawline,\n\
4839 s/=str, O/=str-offsets, u/=trace_abbrev, T/=trace_aranges,\n\
4840 U/=trace_info]\n\
4841 Display the contents of DWARF debug sections\n"));
4842 fprintf (stream, _("\
4843 -wk --debug-dump=links Display the contents of sections that link to separate\n\
4844 debuginfo files\n"));
4845 fprintf (stream, _("\
4846 -P --process-links Display the contents of non-debug sections in separate\n\
4847 debuginfo files. (Implies -wK)\n"));
4848 #if DEFAULT_FOR_FOLLOW_LINKS
4849 fprintf (stream, _("\
4850 -wK --debug-dump=follow-links\n\
4851 Follow links to separate debug info files (default)\n"));
4852 fprintf (stream, _("\
4853 -wN --debug-dump=no-follow-links\n\
4854 Do not follow links to separate debug info files\n"));
4855 #else
4856 fprintf (stream, _("\
4857 -wK --debug-dump=follow-links\n\
4858 Follow links to separate debug info files\n"));
4859 fprintf (stream, _("\
4860 -wN --debug-dump=no-follow-links\n\
4861 Do not follow links to separate debug info files\n\
4862 (default)\n"));
4863 #endif
4864 fprintf (stream, _("\
4865 --dwarf-depth=N Do not display DIEs at depth N or greater\n"));
4866 fprintf (stream, _("\
4867 --dwarf-start=N Display DIEs starting at offset N\n"));
4868 #ifdef ENABLE_LIBCTF
4869 fprintf (stream, _("\
4870 --ctf=<number|name> Display CTF info from section <number|name>\n"));
4871 fprintf (stream, _("\
4872 --ctf-parent=<name> Use CTF archive member <name> as the CTF parent\n"));
4873 fprintf (stream, _("\
4874 --ctf-symbols=<number|name>\n\
4875 Use section <number|name> as the CTF external symtab\n"));
4876 fprintf (stream, _("\
4877 --ctf-strings=<number|name>\n\
4878 Use section <number|name> as the CTF external strtab\n"));
4879 #endif
4880
4881 #ifdef SUPPORT_DISASSEMBLY
4882 fprintf (stream, _("\
4883 -i --instruction-dump=<number|name>\n\
4884 Disassemble the contents of section <number|name>\n"));
4885 #endif
4886 fprintf (stream, _("\
4887 -I --histogram Display histogram of bucket list lengths\n"));
4888 fprintf (stream, _("\
4889 -W --wide Allow output width to exceed 80 characters\n"));
4890 fprintf (stream, _("\
4891 -T --silent-truncation If a symbol name is truncated, do not add [...] suffix\n"));
4892 fprintf (stream, _("\
4893 @<file> Read options from <file>\n"));
4894 fprintf (stream, _("\
4895 -H --help Display this information\n"));
4896 fprintf (stream, _("\
4897 -v --version Display the version number of readelf\n"));
4898
4899 if (REPORT_BUGS_TO[0] && stream == stdout)
4900 fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
4901
4902 exit (stream == stdout ? 0 : 1);
4903 }
4904
4905 /* Record the fact that the user wants the contents of section number
4906 SECTION to be displayed using the method(s) encoded as flags bits
4907 in TYPE. Note, TYPE can be zero if we are creating the array for
4908 the first time. */
4909
4910 static void
4911 request_dump_bynumber (struct dump_data *dumpdata,
4912 unsigned int section, dump_type type)
4913 {
4914 if (section >= dumpdata->num_dump_sects)
4915 {
4916 dump_type * new_dump_sects;
4917
4918 new_dump_sects = (dump_type *) calloc (section + 1,
4919 sizeof (* new_dump_sects));
4920
4921 if (new_dump_sects == NULL)
4922 error (_("Out of memory allocating dump request table.\n"));
4923 else
4924 {
4925 if (dumpdata->dump_sects)
4926 {
4927 /* Copy current flag settings. */
4928 memcpy (new_dump_sects, dumpdata->dump_sects,
4929 dumpdata->num_dump_sects * sizeof (* new_dump_sects));
4930
4931 free (dumpdata->dump_sects);
4932 }
4933
4934 dumpdata->dump_sects = new_dump_sects;
4935 dumpdata->num_dump_sects = section + 1;
4936 }
4937 }
4938
4939 if (dumpdata->dump_sects)
4940 dumpdata->dump_sects[section] |= type;
4941 }
4942
4943 /* Request a dump by section name. */
4944
4945 static void
4946 request_dump_byname (const char * section, dump_type type)
4947 {
4948 struct dump_list_entry * new_request;
4949
4950 new_request = (struct dump_list_entry *)
4951 malloc (sizeof (struct dump_list_entry));
4952 if (!new_request)
4953 error (_("Out of memory allocating dump request table.\n"));
4954
4955 new_request->name = strdup (section);
4956 if (!new_request->name)
4957 error (_("Out of memory allocating dump request table.\n"));
4958
4959 new_request->type = type;
4960
4961 new_request->next = dump_sects_byname;
4962 dump_sects_byname = new_request;
4963 }
4964
4965 static inline void
4966 request_dump (struct dump_data *dumpdata, dump_type type)
4967 {
4968 int section;
4969 char * cp;
4970
4971 do_dump = true;
4972 section = strtoul (optarg, & cp, 0);
4973
4974 if (! *cp && section >= 0)
4975 request_dump_bynumber (dumpdata, section, type);
4976 else
4977 request_dump_byname (optarg, type);
4978 }
4979
4980 static void
4981 parse_args (struct dump_data *dumpdata, int argc, char ** argv)
4982 {
4983 int c;
4984
4985 if (argc < 2)
4986 usage (stderr);
4987
4988 while ((c = getopt_long
4989 (argc, argv, "ACDHILNPR:STVWacdeghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
4990 {
4991 switch (c)
4992 {
4993 case 0:
4994 /* Long options. */
4995 break;
4996 case 'H':
4997 usage (stdout);
4998 break;
4999
5000 case 'a':
5001 do_syms = true;
5002 do_reloc = true;
5003 do_unwind = true;
5004 do_dynamic = true;
5005 do_header = true;
5006 do_sections = true;
5007 do_section_groups = true;
5008 do_segments = true;
5009 do_version = true;
5010 do_histogram = true;
5011 do_arch = true;
5012 do_notes = true;
5013 break;
5014
5015 case 'g':
5016 do_section_groups = true;
5017 break;
5018 case 't':
5019 case 'N':
5020 do_sections = true;
5021 do_section_details = true;
5022 break;
5023 case 'e':
5024 do_header = true;
5025 do_sections = true;
5026 do_segments = true;
5027 break;
5028 case 'A':
5029 do_arch = true;
5030 break;
5031 case 'D':
5032 do_using_dynamic = true;
5033 break;
5034 case 'r':
5035 do_reloc = true;
5036 break;
5037 case 'u':
5038 do_unwind = true;
5039 break;
5040 case 'h':
5041 do_header = true;
5042 break;
5043 case 'l':
5044 do_segments = true;
5045 break;
5046 case 's':
5047 do_syms = true;
5048 break;
5049 case 'S':
5050 do_sections = true;
5051 break;
5052 case 'd':
5053 do_dynamic = true;
5054 break;
5055 case 'I':
5056 do_histogram = true;
5057 break;
5058 case 'n':
5059 do_notes = true;
5060 break;
5061 case 'c':
5062 do_archive_index = true;
5063 break;
5064 case 'L':
5065 do_checks = true;
5066 break;
5067 case 'P':
5068 process_links = true;
5069 do_follow_links = true;
5070 break;
5071 case 'x':
5072 request_dump (dumpdata, HEX_DUMP);
5073 break;
5074 case 'p':
5075 request_dump (dumpdata, STRING_DUMP);
5076 break;
5077 case 'R':
5078 request_dump (dumpdata, RELOC_DUMP);
5079 break;
5080 case 'z':
5081 decompress_dumps = true;
5082 break;
5083 case 'w':
5084 do_dump = true;
5085 if (optarg == NULL)
5086 {
5087 do_debugging = true;
5088 dwarf_select_sections_all ();
5089 }
5090 else
5091 {
5092 do_debugging = false;
5093 dwarf_select_sections_by_letters (optarg);
5094 }
5095 break;
5096 case OPTION_DEBUG_DUMP:
5097 do_dump = true;
5098 if (optarg == NULL)
5099 {
5100 do_debugging = true;
5101 dwarf_select_sections_all ();
5102 }
5103 else
5104 {
5105 do_debugging = false;
5106 dwarf_select_sections_by_names (optarg);
5107 }
5108 break;
5109 case OPTION_DWARF_DEPTH:
5110 {
5111 char *cp;
5112
5113 dwarf_cutoff_level = strtoul (optarg, & cp, 0);
5114 }
5115 break;
5116 case OPTION_DWARF_START:
5117 {
5118 char *cp;
5119
5120 dwarf_start_die = strtoul (optarg, & cp, 0);
5121 }
5122 break;
5123 case OPTION_DWARF_CHECK:
5124 dwarf_check = true;
5125 break;
5126 case OPTION_CTF_DUMP:
5127 do_ctf = true;
5128 request_dump (dumpdata, CTF_DUMP);
5129 break;
5130 case OPTION_CTF_SYMBOLS:
5131 free (dump_ctf_symtab_name);
5132 dump_ctf_symtab_name = strdup (optarg);
5133 break;
5134 case OPTION_CTF_STRINGS:
5135 free (dump_ctf_strtab_name);
5136 dump_ctf_strtab_name = strdup (optarg);
5137 break;
5138 case OPTION_CTF_PARENT:
5139 free (dump_ctf_parent_name);
5140 dump_ctf_parent_name = strdup (optarg);
5141 break;
5142 case OPTION_DYN_SYMS:
5143 do_dyn_syms = true;
5144 break;
5145 case OPTION_LTO_SYMS:
5146 do_lto_syms = true;
5147 break;
5148 #ifdef SUPPORT_DISASSEMBLY
5149 case 'i':
5150 request_dump (dumpdata, DISASS_DUMP);
5151 break;
5152 #endif
5153 case 'v':
5154 print_version (program_name);
5155 break;
5156 case 'V':
5157 do_version = true;
5158 break;
5159 case 'W':
5160 do_wide = true;
5161 break;
5162 case 'T':
5163 do_not_show_symbol_truncation = true;
5164 break;
5165 case 'C':
5166 do_demangle = true;
5167 if (optarg != NULL)
5168 {
5169 enum demangling_styles style;
5170
5171 style = cplus_demangle_name_to_style (optarg);
5172 if (style == unknown_demangling)
5173 error (_("unknown demangling style `%s'"), optarg);
5174
5175 cplus_demangle_set_style (style);
5176 }
5177 break;
5178 case OPTION_NO_DEMANGLING:
5179 do_demangle = false;
5180 break;
5181 case OPTION_RECURSE_LIMIT:
5182 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
5183 break;
5184 case OPTION_NO_RECURSE_LIMIT:
5185 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
5186 break;
5187 case OPTION_WITH_SYMBOL_VERSIONS:
5188 /* Ignored for backward compatibility. */
5189 break;
5190
5191 case OPTION_SYM_BASE:
5192 sym_base = 0;
5193 if (optarg != NULL)
5194 {
5195 sym_base = strtoul (optarg, NULL, 0);
5196 switch (sym_base)
5197 {
5198 case 0:
5199 case 8:
5200 case 10:
5201 case 16:
5202 break;
5203
5204 default:
5205 sym_base = 0;
5206 break;
5207 }
5208 }
5209 break;
5210
5211 default:
5212 /* xgettext:c-format */
5213 error (_("Invalid option '-%c'\n"), c);
5214 /* Fall through. */
5215 case '?':
5216 usage (stderr);
5217 }
5218 }
5219
5220 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
5221 && !do_segments && !do_header && !do_dump && !do_version
5222 && !do_histogram && !do_debugging && !do_arch && !do_notes
5223 && !do_section_groups && !do_archive_index
5224 && !do_dyn_syms && !do_lto_syms)
5225 {
5226 if (do_checks)
5227 {
5228 check_all = true;
5229 do_dynamic = do_syms = do_reloc = do_unwind = do_sections = true;
5230 do_segments = do_header = do_dump = do_version = true;
5231 do_histogram = do_debugging = do_arch = do_notes = true;
5232 do_section_groups = do_archive_index = do_dyn_syms = true;
5233 do_lto_syms = true;
5234 }
5235 else
5236 usage (stderr);
5237 }
5238 }
5239
5240 static const char *
5241 get_elf_class (unsigned int elf_class)
5242 {
5243 static char buff[32];
5244
5245 switch (elf_class)
5246 {
5247 case ELFCLASSNONE: return _("none");
5248 case ELFCLASS32: return "ELF32";
5249 case ELFCLASS64: return "ELF64";
5250 default:
5251 snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
5252 return buff;
5253 }
5254 }
5255
5256 static const char *
5257 get_data_encoding (unsigned int encoding)
5258 {
5259 static char buff[32];
5260
5261 switch (encoding)
5262 {
5263 case ELFDATANONE: return _("none");
5264 case ELFDATA2LSB: return _("2's complement, little endian");
5265 case ELFDATA2MSB: return _("2's complement, big endian");
5266 default:
5267 snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
5268 return buff;
5269 }
5270 }
5271
5272 /* Decode the data held in 'filedata->file_header'. */
5273
5274 static bool
5275 process_file_header (Filedata * filedata)
5276 {
5277 Elf_Internal_Ehdr * header = & filedata->file_header;
5278
5279 if ( header->e_ident[EI_MAG0] != ELFMAG0
5280 || header->e_ident[EI_MAG1] != ELFMAG1
5281 || header->e_ident[EI_MAG2] != ELFMAG2
5282 || header->e_ident[EI_MAG3] != ELFMAG3)
5283 {
5284 error
5285 (_("Not an ELF file - it has the wrong magic bytes at the start\n"));
5286 return false;
5287 }
5288
5289 if (! filedata->is_separate)
5290 init_dwarf_regnames_by_elf_machine_code (header->e_machine);
5291
5292 if (do_header)
5293 {
5294 unsigned i;
5295
5296 if (filedata->is_separate)
5297 printf (_("ELF Header in linked file '%s':\n"), filedata->file_name);
5298 else
5299 printf (_("ELF Header:\n"));
5300 printf (_(" Magic: "));
5301 for (i = 0; i < EI_NIDENT; i++)
5302 printf ("%2.2x ", header->e_ident[i]);
5303 printf ("\n");
5304 printf (_(" Class: %s\n"),
5305 get_elf_class (header->e_ident[EI_CLASS]));
5306 printf (_(" Data: %s\n"),
5307 get_data_encoding (header->e_ident[EI_DATA]));
5308 printf (_(" Version: %d%s\n"),
5309 header->e_ident[EI_VERSION],
5310 (header->e_ident[EI_VERSION] == EV_CURRENT
5311 ? _(" (current)")
5312 : (header->e_ident[EI_VERSION] != EV_NONE
5313 ? _(" <unknown>")
5314 : "")));
5315 printf (_(" OS/ABI: %s\n"),
5316 get_osabi_name (filedata, header->e_ident[EI_OSABI]));
5317 printf (_(" ABI Version: %d\n"),
5318 header->e_ident[EI_ABIVERSION]);
5319 printf (_(" Type: %s\n"),
5320 get_file_type (filedata));
5321 printf (_(" Machine: %s\n"),
5322 get_machine_name (header->e_machine));
5323 printf (_(" Version: 0x%lx\n"),
5324 header->e_version);
5325
5326 printf (_(" Entry point address: "));
5327 print_vma (header->e_entry, PREFIX_HEX);
5328 printf (_("\n Start of program headers: "));
5329 print_vma (header->e_phoff, DEC);
5330 printf (_(" (bytes into file)\n Start of section headers: "));
5331 print_vma (header->e_shoff, DEC);
5332 printf (_(" (bytes into file)\n"));
5333
5334 printf (_(" Flags: 0x%lx%s\n"),
5335 header->e_flags,
5336 get_machine_flags (filedata, header->e_flags, header->e_machine));
5337 printf (_(" Size of this header: %u (bytes)\n"),
5338 header->e_ehsize);
5339 printf (_(" Size of program headers: %u (bytes)\n"),
5340 header->e_phentsize);
5341 printf (_(" Number of program headers: %u"),
5342 header->e_phnum);
5343 if (filedata->section_headers != NULL
5344 && header->e_phnum == PN_XNUM
5345 && filedata->section_headers[0].sh_info != 0)
5346 {
5347 header->e_phnum = filedata->section_headers[0].sh_info;
5348 printf (" (%u)", header->e_phnum);
5349 }
5350 putc ('\n', stdout);
5351 printf (_(" Size of section headers: %u (bytes)\n"),
5352 header->e_shentsize);
5353 printf (_(" Number of section headers: %u"),
5354 header->e_shnum);
5355 if (filedata->section_headers != NULL && header->e_shnum == SHN_UNDEF)
5356 {
5357 header->e_shnum = filedata->section_headers[0].sh_size;
5358 printf (" (%u)", header->e_shnum);
5359 }
5360 putc ('\n', stdout);
5361 printf (_(" Section header string table index: %u"),
5362 header->e_shstrndx);
5363 if (filedata->section_headers != NULL
5364 && header->e_shstrndx == (SHN_XINDEX & 0xffff))
5365 {
5366 header->e_shstrndx = filedata->section_headers[0].sh_link;
5367 printf (" (%u)", header->e_shstrndx);
5368 }
5369 if (header->e_shstrndx != SHN_UNDEF
5370 && header->e_shstrndx >= header->e_shnum)
5371 {
5372 header->e_shstrndx = SHN_UNDEF;
5373 printf (_(" <corrupt: out of range>"));
5374 }
5375 putc ('\n', stdout);
5376 }
5377
5378 if (filedata->section_headers != NULL)
5379 {
5380 if (header->e_phnum == PN_XNUM
5381 && filedata->section_headers[0].sh_info != 0)
5382 header->e_phnum = filedata->section_headers[0].sh_info;
5383 if (header->e_shnum == SHN_UNDEF)
5384 header->e_shnum = filedata->section_headers[0].sh_size;
5385 if (header->e_shstrndx == (SHN_XINDEX & 0xffff))
5386 header->e_shstrndx = filedata->section_headers[0].sh_link;
5387 if (header->e_shstrndx >= header->e_shnum)
5388 header->e_shstrndx = SHN_UNDEF;
5389 }
5390
5391 return true;
5392 }
5393
5394 /* Read in the program headers from FILEDATA and store them in PHEADERS.
5395 Returns TRUE upon success, FALSE otherwise. Loads 32-bit headers. */
5396
5397 static bool
5398 get_32bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
5399 {
5400 Elf32_External_Phdr * phdrs;
5401 Elf32_External_Phdr * external;
5402 Elf_Internal_Phdr * internal;
5403 unsigned int i;
5404 unsigned int size = filedata->file_header.e_phentsize;
5405 unsigned int num = filedata->file_header.e_phnum;
5406
5407 /* PR binutils/17531: Cope with unexpected section header sizes. */
5408 if (size == 0 || num == 0)
5409 return false;
5410 if (size < sizeof * phdrs)
5411 {
5412 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
5413 return false;
5414 }
5415 if (size > sizeof * phdrs)
5416 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
5417
5418 phdrs = (Elf32_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
5419 size, num, _("program headers"));
5420 if (phdrs == NULL)
5421 return false;
5422
5423 for (i = 0, internal = pheaders, external = phdrs;
5424 i < filedata->file_header.e_phnum;
5425 i++, internal++, external++)
5426 {
5427 internal->p_type = BYTE_GET (external->p_type);
5428 internal->p_offset = BYTE_GET (external->p_offset);
5429 internal->p_vaddr = BYTE_GET (external->p_vaddr);
5430 internal->p_paddr = BYTE_GET (external->p_paddr);
5431 internal->p_filesz = BYTE_GET (external->p_filesz);
5432 internal->p_memsz = BYTE_GET (external->p_memsz);
5433 internal->p_flags = BYTE_GET (external->p_flags);
5434 internal->p_align = BYTE_GET (external->p_align);
5435 }
5436
5437 free (phdrs);
5438 return true;
5439 }
5440
5441 /* Read in the program headers from FILEDATA and store them in PHEADERS.
5442 Returns TRUE upon success, FALSE otherwise. Loads 64-bit headers. */
5443
5444 static bool
5445 get_64bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
5446 {
5447 Elf64_External_Phdr * phdrs;
5448 Elf64_External_Phdr * external;
5449 Elf_Internal_Phdr * internal;
5450 unsigned int i;
5451 unsigned int size = filedata->file_header.e_phentsize;
5452 unsigned int num = filedata->file_header.e_phnum;
5453
5454 /* PR binutils/17531: Cope with unexpected section header sizes. */
5455 if (size == 0 || num == 0)
5456 return false;
5457 if (size < sizeof * phdrs)
5458 {
5459 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
5460 return false;
5461 }
5462 if (size > sizeof * phdrs)
5463 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
5464
5465 phdrs = (Elf64_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
5466 size, num, _("program headers"));
5467 if (!phdrs)
5468 return false;
5469
5470 for (i = 0, internal = pheaders, external = phdrs;
5471 i < filedata->file_header.e_phnum;
5472 i++, internal++, external++)
5473 {
5474 internal->p_type = BYTE_GET (external->p_type);
5475 internal->p_flags = BYTE_GET (external->p_flags);
5476 internal->p_offset = BYTE_GET (external->p_offset);
5477 internal->p_vaddr = BYTE_GET (external->p_vaddr);
5478 internal->p_paddr = BYTE_GET (external->p_paddr);
5479 internal->p_filesz = BYTE_GET (external->p_filesz);
5480 internal->p_memsz = BYTE_GET (external->p_memsz);
5481 internal->p_align = BYTE_GET (external->p_align);
5482 }
5483
5484 free (phdrs);
5485 return true;
5486 }
5487
5488 /* Returns TRUE if the program headers were read into `program_headers'. */
5489
5490 static bool
5491 get_program_headers (Filedata * filedata)
5492 {
5493 Elf_Internal_Phdr * phdrs;
5494
5495 /* Check cache of prior read. */
5496 if (filedata->program_headers != NULL)
5497 return true;
5498
5499 /* Be kind to memory checkers by looking for
5500 e_phnum values which we know must be invalid. */
5501 if (filedata->file_header.e_phnum
5502 * (is_32bit_elf ? sizeof (Elf32_External_Phdr) : sizeof (Elf64_External_Phdr))
5503 >= filedata->file_size)
5504 {
5505 error (_("Too many program headers - %#x - the file is not that big\n"),
5506 filedata->file_header.e_phnum);
5507 return false;
5508 }
5509
5510 phdrs = (Elf_Internal_Phdr *) cmalloc (filedata->file_header.e_phnum,
5511 sizeof (Elf_Internal_Phdr));
5512 if (phdrs == NULL)
5513 {
5514 error (_("Out of memory reading %u program headers\n"),
5515 filedata->file_header.e_phnum);
5516 return false;
5517 }
5518
5519 if (is_32bit_elf
5520 ? get_32bit_program_headers (filedata, phdrs)
5521 : get_64bit_program_headers (filedata, phdrs))
5522 {
5523 filedata->program_headers = phdrs;
5524 return true;
5525 }
5526
5527 free (phdrs);
5528 return false;
5529 }
5530
5531 /* Print program header info and locate dynamic section. */
5532
5533 static void
5534 process_program_headers (Filedata * filedata)
5535 {
5536 Elf_Internal_Phdr * segment;
5537 unsigned int i;
5538 Elf_Internal_Phdr * previous_load = NULL;
5539
5540 if (filedata->file_header.e_phnum == 0)
5541 {
5542 /* PR binutils/12467. */
5543 if (filedata->file_header.e_phoff != 0)
5544 warn (_("possibly corrupt ELF header - it has a non-zero program"
5545 " header offset, but no program headers\n"));
5546 else if (do_segments)
5547 {
5548 if (filedata->is_separate)
5549 printf (_("\nThere are no program headers in linked file '%s'.\n"),
5550 filedata->file_name);
5551 else
5552 printf (_("\nThere are no program headers in this file.\n"));
5553 }
5554 goto no_headers;
5555 }
5556
5557 if (do_segments && !do_header)
5558 {
5559 if (filedata->is_separate)
5560 printf ("\nIn linked file '%s' the ELF file type is %s\n",
5561 filedata->file_name, get_file_type (filedata));
5562 else
5563 printf (_("\nElf file type is %s\n"), get_file_type (filedata));
5564 printf (_("Entry point 0x%s\n"), bfd_vmatoa ("x", filedata->file_header.e_entry));
5565 printf (ngettext ("There is %d program header, starting at offset %s\n",
5566 "There are %d program headers, starting at offset %s\n",
5567 filedata->file_header.e_phnum),
5568 filedata->file_header.e_phnum,
5569 bfd_vmatoa ("u", filedata->file_header.e_phoff));
5570 }
5571
5572 if (! get_program_headers (filedata))
5573 goto no_headers;
5574
5575 if (do_segments)
5576 {
5577 if (filedata->file_header.e_phnum > 1)
5578 printf (_("\nProgram Headers:\n"));
5579 else
5580 printf (_("\nProgram Headers:\n"));
5581
5582 if (is_32bit_elf)
5583 printf
5584 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
5585 else if (do_wide)
5586 printf
5587 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
5588 else
5589 {
5590 printf
5591 (_(" Type Offset VirtAddr PhysAddr\n"));
5592 printf
5593 (_(" FileSiz MemSiz Flags Align\n"));
5594 }
5595 }
5596
5597 unsigned long dynamic_addr = 0;
5598 bfd_size_type dynamic_size = 0;
5599 for (i = 0, segment = filedata->program_headers;
5600 i < filedata->file_header.e_phnum;
5601 i++, segment++)
5602 {
5603 if (do_segments)
5604 {
5605 printf (" %-14.14s ", get_segment_type (filedata, segment->p_type));
5606
5607 if (is_32bit_elf)
5608 {
5609 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
5610 printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
5611 printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
5612 printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
5613 printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
5614 printf ("%c%c%c ",
5615 (segment->p_flags & PF_R ? 'R' : ' '),
5616 (segment->p_flags & PF_W ? 'W' : ' '),
5617 (segment->p_flags & PF_X ? 'E' : ' '));
5618 printf ("%#lx", (unsigned long) segment->p_align);
5619 }
5620 else if (do_wide)
5621 {
5622 if ((unsigned long) segment->p_offset == segment->p_offset)
5623 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
5624 else
5625 {
5626 print_vma (segment->p_offset, FULL_HEX);
5627 putchar (' ');
5628 }
5629
5630 print_vma (segment->p_vaddr, FULL_HEX);
5631 putchar (' ');
5632 print_vma (segment->p_paddr, FULL_HEX);
5633 putchar (' ');
5634
5635 if ((unsigned long) segment->p_filesz == segment->p_filesz)
5636 printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
5637 else
5638 {
5639 print_vma (segment->p_filesz, FULL_HEX);
5640 putchar (' ');
5641 }
5642
5643 if ((unsigned long) segment->p_memsz == segment->p_memsz)
5644 printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
5645 else
5646 {
5647 print_vma (segment->p_memsz, FULL_HEX);
5648 }
5649
5650 printf (" %c%c%c ",
5651 (segment->p_flags & PF_R ? 'R' : ' '),
5652 (segment->p_flags & PF_W ? 'W' : ' '),
5653 (segment->p_flags & PF_X ? 'E' : ' '));
5654
5655 if ((unsigned long) segment->p_align == segment->p_align)
5656 printf ("%#lx", (unsigned long) segment->p_align);
5657 else
5658 {
5659 print_vma (segment->p_align, PREFIX_HEX);
5660 }
5661 }
5662 else
5663 {
5664 print_vma (segment->p_offset, FULL_HEX);
5665 putchar (' ');
5666 print_vma (segment->p_vaddr, FULL_HEX);
5667 putchar (' ');
5668 print_vma (segment->p_paddr, FULL_HEX);
5669 printf ("\n ");
5670 print_vma (segment->p_filesz, FULL_HEX);
5671 putchar (' ');
5672 print_vma (segment->p_memsz, FULL_HEX);
5673 printf (" %c%c%c ",
5674 (segment->p_flags & PF_R ? 'R' : ' '),
5675 (segment->p_flags & PF_W ? 'W' : ' '),
5676 (segment->p_flags & PF_X ? 'E' : ' '));
5677 print_vma (segment->p_align, PREFIX_HEX);
5678 }
5679
5680 putc ('\n', stdout);
5681 }
5682
5683 switch (segment->p_type)
5684 {
5685 case PT_LOAD:
5686 #if 0 /* Do not warn about out of order PT_LOAD segments. Although officially
5687 required by the ELF standard, several programs, including the Linux
5688 kernel, make use of non-ordered segments. */
5689 if (previous_load
5690 && previous_load->p_vaddr > segment->p_vaddr)
5691 error (_("LOAD segments must be sorted in order of increasing VirtAddr\n"));
5692 #endif
5693 if (segment->p_memsz < segment->p_filesz)
5694 error (_("the segment's file size is larger than its memory size\n"));
5695 previous_load = segment;
5696 break;
5697
5698 case PT_PHDR:
5699 /* PR 20815 - Verify that the program header is loaded into memory. */
5700 if (i > 0 && previous_load != NULL)
5701 error (_("the PHDR segment must occur before any LOAD segment\n"));
5702 if (filedata->file_header.e_machine != EM_PARISC)
5703 {
5704 unsigned int j;
5705
5706 for (j = 1; j < filedata->file_header.e_phnum; j++)
5707 {
5708 Elf_Internal_Phdr *load = filedata->program_headers + j;
5709 if (load->p_type == PT_LOAD
5710 && load->p_offset <= segment->p_offset
5711 && (load->p_offset + load->p_filesz
5712 >= segment->p_offset + segment->p_filesz)
5713 && load->p_vaddr <= segment->p_vaddr
5714 && (load->p_vaddr + load->p_filesz
5715 >= segment->p_vaddr + segment->p_filesz))
5716 break;
5717 }
5718 if (j == filedata->file_header.e_phnum)
5719 error (_("the PHDR segment is not covered by a LOAD segment\n"));
5720 }
5721 break;
5722
5723 case PT_DYNAMIC:
5724 if (dynamic_addr)
5725 error (_("more than one dynamic segment\n"));
5726
5727 /* By default, assume that the .dynamic section is the first
5728 section in the DYNAMIC segment. */
5729 dynamic_addr = segment->p_offset;
5730 dynamic_size = segment->p_filesz;
5731
5732 /* Try to locate the .dynamic section. If there is
5733 a section header table, we can easily locate it. */
5734 if (filedata->section_headers != NULL)
5735 {
5736 Elf_Internal_Shdr * sec;
5737
5738 sec = find_section (filedata, ".dynamic");
5739 if (sec == NULL || sec->sh_size == 0)
5740 {
5741 /* A corresponding .dynamic section is expected, but on
5742 IA-64/OpenVMS it is OK for it to be missing. */
5743 if (!is_ia64_vms (filedata))
5744 error (_("no .dynamic section in the dynamic segment\n"));
5745 break;
5746 }
5747
5748 if (sec->sh_type == SHT_NOBITS)
5749 {
5750 dynamic_addr = 0;
5751 dynamic_size = 0;
5752 break;
5753 }
5754
5755 dynamic_addr = sec->sh_offset;
5756 dynamic_size = sec->sh_size;
5757
5758 /* The PT_DYNAMIC segment, which is used by the run-time
5759 loader, should exactly match the .dynamic section. */
5760 if (do_checks
5761 && (dynamic_addr != segment->p_offset
5762 || dynamic_size != segment->p_filesz))
5763 warn (_("\
5764 the .dynamic section is not the same as the dynamic segment\n"));
5765 }
5766
5767 /* PR binutils/17512: Avoid corrupt dynamic section info in the
5768 segment. Check this after matching against the section headers
5769 so we don't warn on debuginfo file (which have NOBITS .dynamic
5770 sections). */
5771 if (dynamic_addr > filedata->file_size
5772 || (dynamic_size > filedata->file_size - dynamic_addr))
5773 {
5774 error (_("the dynamic segment offset + size exceeds the size of the file\n"));
5775 dynamic_addr = 0;
5776 dynamic_size = 0;
5777 }
5778 break;
5779
5780 case PT_INTERP:
5781 if (segment->p_offset >= filedata->file_size
5782 || segment->p_filesz > filedata->file_size - segment->p_offset
5783 || segment->p_filesz - 1 >= (size_t) -2
5784 || fseek (filedata->handle,
5785 filedata->archive_file_offset + (long) segment->p_offset,
5786 SEEK_SET))
5787 error (_("Unable to find program interpreter name\n"));
5788 else
5789 {
5790 size_t len = segment->p_filesz;
5791 free (filedata->program_interpreter);
5792 filedata->program_interpreter = xmalloc (len + 1);
5793 len = fread (filedata->program_interpreter, 1, len,
5794 filedata->handle);
5795 filedata->program_interpreter[len] = 0;
5796
5797 if (do_segments)
5798 printf (_(" [Requesting program interpreter: %s]\n"),
5799 filedata->program_interpreter);
5800 }
5801 break;
5802 }
5803 }
5804
5805 if (do_segments
5806 && filedata->section_headers != NULL
5807 && filedata->string_table != NULL)
5808 {
5809 printf (_("\n Section to Segment mapping:\n"));
5810 printf (_(" Segment Sections...\n"));
5811
5812 for (i = 0; i < filedata->file_header.e_phnum; i++)
5813 {
5814 unsigned int j;
5815 Elf_Internal_Shdr * section;
5816
5817 segment = filedata->program_headers + i;
5818 section = filedata->section_headers + 1;
5819
5820 printf (" %2.2d ", i);
5821
5822 for (j = 1; j < filedata->file_header.e_shnum; j++, section++)
5823 {
5824 if (!ELF_TBSS_SPECIAL (section, segment)
5825 && ELF_SECTION_IN_SEGMENT_STRICT (section, segment))
5826 printf ("%s ", printable_section_name (filedata, section));
5827 }
5828
5829 putc ('\n',stdout);
5830 }
5831 }
5832
5833 filedata->dynamic_addr = dynamic_addr;
5834 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
5835 return;
5836
5837 no_headers:
5838 filedata->dynamic_addr = 0;
5839 filedata->dynamic_size = 1;
5840 }
5841
5842
5843 /* Find the file offset corresponding to VMA by using the program headers. */
5844
5845 static long
5846 offset_from_vma (Filedata * filedata, bfd_vma vma, bfd_size_type size)
5847 {
5848 Elf_Internal_Phdr * seg;
5849
5850 if (! get_program_headers (filedata))
5851 {
5852 warn (_("Cannot interpret virtual addresses without program headers.\n"));
5853 return (long) vma;
5854 }
5855
5856 for (seg = filedata->program_headers;
5857 seg < filedata->program_headers + filedata->file_header.e_phnum;
5858 ++seg)
5859 {
5860 if (seg->p_type != PT_LOAD)
5861 continue;
5862
5863 if (vma >= (seg->p_vaddr & -seg->p_align)
5864 && vma + size <= seg->p_vaddr + seg->p_filesz)
5865 return vma - seg->p_vaddr + seg->p_offset;
5866 }
5867
5868 warn (_("Virtual address 0x%lx not located in any PT_LOAD segment.\n"),
5869 (unsigned long) vma);
5870 return (long) vma;
5871 }
5872
5873
5874 /* Allocate memory and load the sections headers into FILEDATA->filedata->section_headers.
5875 If PROBE is true, this is just a probe and we do not generate any error
5876 messages if the load fails. */
5877
5878 static bool
5879 get_32bit_section_headers (Filedata * filedata, bool probe)
5880 {
5881 Elf32_External_Shdr * shdrs;
5882 Elf_Internal_Shdr * internal;
5883 unsigned int i;
5884 unsigned int size = filedata->file_header.e_shentsize;
5885 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
5886
5887 /* PR binutils/17531: Cope with unexpected section header sizes. */
5888 if (size == 0 || num == 0)
5889 return false;
5890 if (size < sizeof * shdrs)
5891 {
5892 if (! probe)
5893 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
5894 return false;
5895 }
5896 if (!probe && size > sizeof * shdrs)
5897 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
5898
5899 shdrs = (Elf32_External_Shdr *) get_data (NULL, filedata, filedata->file_header.e_shoff,
5900 size, num,
5901 probe ? NULL : _("section headers"));
5902 if (shdrs == NULL)
5903 return false;
5904
5905 filedata->section_headers = (Elf_Internal_Shdr *)
5906 cmalloc (num, sizeof (Elf_Internal_Shdr));
5907 if (filedata->section_headers == NULL)
5908 {
5909 if (!probe)
5910 error (_("Out of memory reading %u section headers\n"), num);
5911 free (shdrs);
5912 return false;
5913 }
5914
5915 for (i = 0, internal = filedata->section_headers;
5916 i < num;
5917 i++, internal++)
5918 {
5919 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
5920 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
5921 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
5922 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
5923 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
5924 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
5925 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
5926 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
5927 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
5928 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
5929 if (!probe && internal->sh_link > num)
5930 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
5931 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
5932 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
5933 }
5934
5935 free (shdrs);
5936 return true;
5937 }
5938
5939 /* Like get_32bit_section_headers, except that it fetches 64-bit headers. */
5940
5941 static bool
5942 get_64bit_section_headers (Filedata * filedata, bool probe)
5943 {
5944 Elf64_External_Shdr * shdrs;
5945 Elf_Internal_Shdr * internal;
5946 unsigned int i;
5947 unsigned int size = filedata->file_header.e_shentsize;
5948 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
5949
5950 /* PR binutils/17531: Cope with unexpected section header sizes. */
5951 if (size == 0 || num == 0)
5952 return false;
5953
5954 if (size < sizeof * shdrs)
5955 {
5956 if (! probe)
5957 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
5958 return false;
5959 }
5960
5961 if (! probe && size > sizeof * shdrs)
5962 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
5963
5964 shdrs = (Elf64_External_Shdr *) get_data (NULL, filedata,
5965 filedata->file_header.e_shoff,
5966 size, num,
5967 probe ? NULL : _("section headers"));
5968 if (shdrs == NULL)
5969 return false;
5970
5971 filedata->section_headers = (Elf_Internal_Shdr *)
5972 cmalloc (num, sizeof (Elf_Internal_Shdr));
5973 if (filedata->section_headers == NULL)
5974 {
5975 if (! probe)
5976 error (_("Out of memory reading %u section headers\n"), num);
5977 free (shdrs);
5978 return false;
5979 }
5980
5981 for (i = 0, internal = filedata->section_headers;
5982 i < num;
5983 i++, internal++)
5984 {
5985 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
5986 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
5987 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
5988 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
5989 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
5990 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
5991 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
5992 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
5993 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
5994 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
5995 if (!probe && internal->sh_link > num)
5996 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
5997 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
5998 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
5999 }
6000
6001 free (shdrs);
6002 return true;
6003 }
6004
6005 static bool
6006 get_section_headers (Filedata *filedata, bool probe)
6007 {
6008 if (filedata->section_headers != NULL)
6009 return true;
6010
6011 if (is_32bit_elf)
6012 return get_32bit_section_headers (filedata, probe);
6013 else
6014 return get_64bit_section_headers (filedata, probe);
6015 }
6016
6017 static Elf_Internal_Sym *
6018 get_32bit_elf_symbols (Filedata * filedata,
6019 Elf_Internal_Shdr * section,
6020 unsigned long * num_syms_return)
6021 {
6022 unsigned long number = 0;
6023 Elf32_External_Sym * esyms = NULL;
6024 Elf_External_Sym_Shndx * shndx = NULL;
6025 Elf_Internal_Sym * isyms = NULL;
6026 Elf_Internal_Sym * psym;
6027 unsigned int j;
6028 elf_section_list * entry;
6029
6030 if (section->sh_size == 0)
6031 {
6032 if (num_syms_return != NULL)
6033 * num_syms_return = 0;
6034 return NULL;
6035 }
6036
6037 /* Run some sanity checks first. */
6038 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
6039 {
6040 error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
6041 printable_section_name (filedata, section),
6042 (unsigned long) section->sh_entsize);
6043 goto exit_point;
6044 }
6045
6046 if (section->sh_size > filedata->file_size)
6047 {
6048 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
6049 printable_section_name (filedata, section),
6050 (unsigned long) section->sh_size);
6051 goto exit_point;
6052 }
6053
6054 number = section->sh_size / section->sh_entsize;
6055
6056 if (number * sizeof (Elf32_External_Sym) > section->sh_size + 1)
6057 {
6058 error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
6059 (unsigned long) section->sh_size,
6060 printable_section_name (filedata, section),
6061 (unsigned long) section->sh_entsize);
6062 goto exit_point;
6063 }
6064
6065 esyms = (Elf32_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
6066 section->sh_size, _("symbols"));
6067 if (esyms == NULL)
6068 goto exit_point;
6069
6070 shndx = NULL;
6071 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
6072 {
6073 if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
6074 continue;
6075
6076 if (shndx != NULL)
6077 {
6078 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
6079 free (shndx);
6080 }
6081
6082 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
6083 entry->hdr->sh_offset,
6084 1, entry->hdr->sh_size,
6085 _("symbol table section indices"));
6086 if (shndx == NULL)
6087 goto exit_point;
6088
6089 /* PR17531: file: heap-buffer-overflow */
6090 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
6091 {
6092 error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
6093 printable_section_name (filedata, entry->hdr),
6094 (unsigned long) entry->hdr->sh_size,
6095 (unsigned long) section->sh_size);
6096 goto exit_point;
6097 }
6098 }
6099
6100 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
6101
6102 if (isyms == NULL)
6103 {
6104 error (_("Out of memory reading %lu symbols\n"),
6105 (unsigned long) number);
6106 goto exit_point;
6107 }
6108
6109 for (j = 0, psym = isyms; j < number; j++, psym++)
6110 {
6111 psym->st_name = BYTE_GET (esyms[j].st_name);
6112 psym->st_value = BYTE_GET (esyms[j].st_value);
6113 psym->st_size = BYTE_GET (esyms[j].st_size);
6114 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
6115 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
6116 psym->st_shndx
6117 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
6118 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
6119 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
6120 psym->st_info = BYTE_GET (esyms[j].st_info);
6121 psym->st_other = BYTE_GET (esyms[j].st_other);
6122 }
6123
6124 exit_point:
6125 free (shndx);
6126 free (esyms);
6127
6128 if (num_syms_return != NULL)
6129 * num_syms_return = isyms == NULL ? 0 : number;
6130
6131 return isyms;
6132 }
6133
6134 static Elf_Internal_Sym *
6135 get_64bit_elf_symbols (Filedata * filedata,
6136 Elf_Internal_Shdr * section,
6137 unsigned long * num_syms_return)
6138 {
6139 unsigned long number = 0;
6140 Elf64_External_Sym * esyms = NULL;
6141 Elf_External_Sym_Shndx * shndx = NULL;
6142 Elf_Internal_Sym * isyms = NULL;
6143 Elf_Internal_Sym * psym;
6144 unsigned int j;
6145 elf_section_list * entry;
6146
6147 if (section->sh_size == 0)
6148 {
6149 if (num_syms_return != NULL)
6150 * num_syms_return = 0;
6151 return NULL;
6152 }
6153
6154 /* Run some sanity checks first. */
6155 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
6156 {
6157 error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
6158 printable_section_name (filedata, section),
6159 (unsigned long) section->sh_entsize);
6160 goto exit_point;
6161 }
6162
6163 if (section->sh_size > filedata->file_size)
6164 {
6165 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
6166 printable_section_name (filedata, section),
6167 (unsigned long) section->sh_size);
6168 goto exit_point;
6169 }
6170
6171 number = section->sh_size / section->sh_entsize;
6172
6173 if (number * sizeof (Elf64_External_Sym) > section->sh_size + 1)
6174 {
6175 error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
6176 (unsigned long) section->sh_size,
6177 printable_section_name (filedata, section),
6178 (unsigned long) section->sh_entsize);
6179 goto exit_point;
6180 }
6181
6182 esyms = (Elf64_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
6183 section->sh_size, _("symbols"));
6184 if (!esyms)
6185 goto exit_point;
6186
6187 shndx = NULL;
6188 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
6189 {
6190 if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
6191 continue;
6192
6193 if (shndx != NULL)
6194 {
6195 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
6196 free (shndx);
6197 }
6198
6199 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
6200 entry->hdr->sh_offset,
6201 1, entry->hdr->sh_size,
6202 _("symbol table section indices"));
6203 if (shndx == NULL)
6204 goto exit_point;
6205
6206 /* PR17531: file: heap-buffer-overflow */
6207 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
6208 {
6209 error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
6210 printable_section_name (filedata, entry->hdr),
6211 (unsigned long) entry->hdr->sh_size,
6212 (unsigned long) section->sh_size);
6213 goto exit_point;
6214 }
6215 }
6216
6217 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
6218
6219 if (isyms == NULL)
6220 {
6221 error (_("Out of memory reading %lu symbols\n"),
6222 (unsigned long) number);
6223 goto exit_point;
6224 }
6225
6226 for (j = 0, psym = isyms; j < number; j++, psym++)
6227 {
6228 psym->st_name = BYTE_GET (esyms[j].st_name);
6229 psym->st_info = BYTE_GET (esyms[j].st_info);
6230 psym->st_other = BYTE_GET (esyms[j].st_other);
6231 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
6232
6233 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
6234 psym->st_shndx
6235 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
6236 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
6237 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
6238
6239 psym->st_value = BYTE_GET (esyms[j].st_value);
6240 psym->st_size = BYTE_GET (esyms[j].st_size);
6241 }
6242
6243 exit_point:
6244 free (shndx);
6245 free (esyms);
6246
6247 if (num_syms_return != NULL)
6248 * num_syms_return = isyms == NULL ? 0 : number;
6249
6250 return isyms;
6251 }
6252
6253 static Elf_Internal_Sym *
6254 get_elf_symbols (Filedata *filedata,
6255 Elf_Internal_Shdr *section,
6256 unsigned long *num_syms_return)
6257 {
6258 if (is_32bit_elf)
6259 return get_32bit_elf_symbols (filedata, section, num_syms_return);
6260 else
6261 return get_64bit_elf_symbols (filedata, section, num_syms_return);
6262 }
6263
6264 static const char *
6265 get_elf_section_flags (Filedata * filedata, bfd_vma sh_flags)
6266 {
6267 static char buff[1024];
6268 char * p = buff;
6269 unsigned int field_size = is_32bit_elf ? 8 : 16;
6270 signed int sindex;
6271 unsigned int size = sizeof (buff) - (field_size + 4 + 1);
6272 bfd_vma os_flags = 0;
6273 bfd_vma proc_flags = 0;
6274 bfd_vma unknown_flags = 0;
6275 static const struct
6276 {
6277 const char * str;
6278 unsigned int len;
6279 }
6280 flags [] =
6281 {
6282 /* 0 */ { STRING_COMMA_LEN ("WRITE") },
6283 /* 1 */ { STRING_COMMA_LEN ("ALLOC") },
6284 /* 2 */ { STRING_COMMA_LEN ("EXEC") },
6285 /* 3 */ { STRING_COMMA_LEN ("MERGE") },
6286 /* 4 */ { STRING_COMMA_LEN ("STRINGS") },
6287 /* 5 */ { STRING_COMMA_LEN ("INFO LINK") },
6288 /* 6 */ { STRING_COMMA_LEN ("LINK ORDER") },
6289 /* 7 */ { STRING_COMMA_LEN ("OS NONCONF") },
6290 /* 8 */ { STRING_COMMA_LEN ("GROUP") },
6291 /* 9 */ { STRING_COMMA_LEN ("TLS") },
6292 /* IA-64 specific. */
6293 /* 10 */ { STRING_COMMA_LEN ("SHORT") },
6294 /* 11 */ { STRING_COMMA_LEN ("NORECOV") },
6295 /* IA-64 OpenVMS specific. */
6296 /* 12 */ { STRING_COMMA_LEN ("VMS_GLOBAL") },
6297 /* 13 */ { STRING_COMMA_LEN ("VMS_OVERLAID") },
6298 /* 14 */ { STRING_COMMA_LEN ("VMS_SHARED") },
6299 /* 15 */ { STRING_COMMA_LEN ("VMS_VECTOR") },
6300 /* 16 */ { STRING_COMMA_LEN ("VMS_ALLOC_64BIT") },
6301 /* 17 */ { STRING_COMMA_LEN ("VMS_PROTECTED") },
6302 /* Generic. */
6303 /* 18 */ { STRING_COMMA_LEN ("EXCLUDE") },
6304 /* SPARC specific. */
6305 /* 19 */ { STRING_COMMA_LEN ("ORDERED") },
6306 /* 20 */ { STRING_COMMA_LEN ("COMPRESSED") },
6307 /* ARM specific. */
6308 /* 21 */ { STRING_COMMA_LEN ("ENTRYSECT") },
6309 /* 22 */ { STRING_COMMA_LEN ("ARM_PURECODE") },
6310 /* 23 */ { STRING_COMMA_LEN ("COMDEF") },
6311 /* GNU specific. */
6312 /* 24 */ { STRING_COMMA_LEN ("GNU_MBIND") },
6313 /* VLE specific. */
6314 /* 25 */ { STRING_COMMA_LEN ("VLE") },
6315 /* GNU specific. */
6316 /* 26 */ { STRING_COMMA_LEN ("GNU_RETAIN") },
6317 };
6318
6319 if (do_section_details)
6320 {
6321 sprintf (buff, "[%*.*lx]: ",
6322 field_size, field_size, (unsigned long) sh_flags);
6323 p += field_size + 4;
6324 }
6325
6326 while (sh_flags)
6327 {
6328 bfd_vma flag;
6329
6330 flag = sh_flags & - sh_flags;
6331 sh_flags &= ~ flag;
6332
6333 if (do_section_details)
6334 {
6335 switch (flag)
6336 {
6337 case SHF_WRITE: sindex = 0; break;
6338 case SHF_ALLOC: sindex = 1; break;
6339 case SHF_EXECINSTR: sindex = 2; break;
6340 case SHF_MERGE: sindex = 3; break;
6341 case SHF_STRINGS: sindex = 4; break;
6342 case SHF_INFO_LINK: sindex = 5; break;
6343 case SHF_LINK_ORDER: sindex = 6; break;
6344 case SHF_OS_NONCONFORMING: sindex = 7; break;
6345 case SHF_GROUP: sindex = 8; break;
6346 case SHF_TLS: sindex = 9; break;
6347 case SHF_EXCLUDE: sindex = 18; break;
6348 case SHF_COMPRESSED: sindex = 20; break;
6349
6350 default:
6351 sindex = -1;
6352 switch (filedata->file_header.e_machine)
6353 {
6354 case EM_IA_64:
6355 if (flag == SHF_IA_64_SHORT)
6356 sindex = 10;
6357 else if (flag == SHF_IA_64_NORECOV)
6358 sindex = 11;
6359 #ifdef BFD64
6360 else if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
6361 switch (flag)
6362 {
6363 case SHF_IA_64_VMS_GLOBAL: sindex = 12; break;
6364 case SHF_IA_64_VMS_OVERLAID: sindex = 13; break;
6365 case SHF_IA_64_VMS_SHARED: sindex = 14; break;
6366 case SHF_IA_64_VMS_VECTOR: sindex = 15; break;
6367 case SHF_IA_64_VMS_ALLOC_64BIT: sindex = 16; break;
6368 case SHF_IA_64_VMS_PROTECTED: sindex = 17; break;
6369 default: break;
6370 }
6371 #endif
6372 break;
6373
6374 case EM_386:
6375 case EM_IAMCU:
6376 case EM_X86_64:
6377 case EM_L1OM:
6378 case EM_K1OM:
6379 case EM_OLD_SPARCV9:
6380 case EM_SPARC32PLUS:
6381 case EM_SPARCV9:
6382 case EM_SPARC:
6383 if (flag == SHF_ORDERED)
6384 sindex = 19;
6385 break;
6386
6387 case EM_ARM:
6388 switch (flag)
6389 {
6390 case SHF_ENTRYSECT: sindex = 21; break;
6391 case SHF_ARM_PURECODE: sindex = 22; break;
6392 case SHF_COMDEF: sindex = 23; break;
6393 default: break;
6394 }
6395 break;
6396 case EM_PPC:
6397 if (flag == SHF_PPC_VLE)
6398 sindex = 25;
6399 break;
6400 default:
6401 break;
6402 }
6403
6404 switch (filedata->file_header.e_ident[EI_OSABI])
6405 {
6406 case ELFOSABI_GNU:
6407 case ELFOSABI_FREEBSD:
6408 if (flag == SHF_GNU_RETAIN)
6409 sindex = 26;
6410 /* Fall through */
6411 case ELFOSABI_NONE:
6412 if (flag == SHF_GNU_MBIND)
6413 /* We should not recognize SHF_GNU_MBIND for
6414 ELFOSABI_NONE, but binutils as of 2019-07-23 did
6415 not set the EI_OSABI header byte. */
6416 sindex = 24;
6417 break;
6418 default:
6419 break;
6420 }
6421 break;
6422 }
6423
6424 if (sindex != -1)
6425 {
6426 if (p != buff + field_size + 4)
6427 {
6428 if (size < (10 + 2))
6429 {
6430 warn (_("Internal error: not enough buffer room for section flag info"));
6431 return _("<unknown>");
6432 }
6433 size -= 2;
6434 *p++ = ',';
6435 *p++ = ' ';
6436 }
6437
6438 size -= flags [sindex].len;
6439 p = stpcpy (p, flags [sindex].str);
6440 }
6441 else if (flag & SHF_MASKOS)
6442 os_flags |= flag;
6443 else if (flag & SHF_MASKPROC)
6444 proc_flags |= flag;
6445 else
6446 unknown_flags |= flag;
6447 }
6448 else
6449 {
6450 switch (flag)
6451 {
6452 case SHF_WRITE: *p = 'W'; break;
6453 case SHF_ALLOC: *p = 'A'; break;
6454 case SHF_EXECINSTR: *p = 'X'; break;
6455 case SHF_MERGE: *p = 'M'; break;
6456 case SHF_STRINGS: *p = 'S'; break;
6457 case SHF_INFO_LINK: *p = 'I'; break;
6458 case SHF_LINK_ORDER: *p = 'L'; break;
6459 case SHF_OS_NONCONFORMING: *p = 'O'; break;
6460 case SHF_GROUP: *p = 'G'; break;
6461 case SHF_TLS: *p = 'T'; break;
6462 case SHF_EXCLUDE: *p = 'E'; break;
6463 case SHF_COMPRESSED: *p = 'C'; break;
6464
6465 default:
6466 if ((filedata->file_header.e_machine == EM_X86_64
6467 || filedata->file_header.e_machine == EM_L1OM
6468 || filedata->file_header.e_machine == EM_K1OM)
6469 && flag == SHF_X86_64_LARGE)
6470 *p = 'l';
6471 else if (filedata->file_header.e_machine == EM_ARM
6472 && flag == SHF_ARM_PURECODE)
6473 *p = 'y';
6474 else if (filedata->file_header.e_machine == EM_PPC
6475 && flag == SHF_PPC_VLE)
6476 *p = 'v';
6477 else if (flag & SHF_MASKOS)
6478 {
6479 switch (filedata->file_header.e_ident[EI_OSABI])
6480 {
6481 case ELFOSABI_GNU:
6482 case ELFOSABI_FREEBSD:
6483 if (flag == SHF_GNU_RETAIN)
6484 {
6485 *p = 'R';
6486 break;
6487 }
6488 /* Fall through */
6489 case ELFOSABI_NONE:
6490 if (flag == SHF_GNU_MBIND)
6491 {
6492 /* We should not recognize SHF_GNU_MBIND for
6493 ELFOSABI_NONE, but binutils as of 2019-07-23 did
6494 not set the EI_OSABI header byte. */
6495 *p = 'D';
6496 break;
6497 }
6498 /* Fall through */
6499 default:
6500 *p = 'o';
6501 sh_flags &= ~SHF_MASKOS;
6502 break;
6503 }
6504 }
6505 else if (flag & SHF_MASKPROC)
6506 {
6507 *p = 'p';
6508 sh_flags &= ~ SHF_MASKPROC;
6509 }
6510 else
6511 *p = 'x';
6512 break;
6513 }
6514 p++;
6515 }
6516 }
6517
6518 if (do_section_details)
6519 {
6520 if (os_flags)
6521 {
6522 size -= 5 + field_size;
6523 if (p != buff + field_size + 4)
6524 {
6525 if (size < (2 + 1))
6526 {
6527 warn (_("Internal error: not enough buffer room for section flag info"));
6528 return _("<unknown>");
6529 }
6530 size -= 2;
6531 *p++ = ',';
6532 *p++ = ' ';
6533 }
6534 sprintf (p, "OS (%*.*lx)", field_size, field_size,
6535 (unsigned long) os_flags);
6536 p += 5 + field_size;
6537 }
6538 if (proc_flags)
6539 {
6540 size -= 7 + field_size;
6541 if (p != buff + field_size + 4)
6542 {
6543 if (size < (2 + 1))
6544 {
6545 warn (_("Internal error: not enough buffer room for section flag info"));
6546 return _("<unknown>");
6547 }
6548 size -= 2;
6549 *p++ = ',';
6550 *p++ = ' ';
6551 }
6552 sprintf (p, "PROC (%*.*lx)", field_size, field_size,
6553 (unsigned long) proc_flags);
6554 p += 7 + field_size;
6555 }
6556 if (unknown_flags)
6557 {
6558 size -= 10 + field_size;
6559 if (p != buff + field_size + 4)
6560 {
6561 if (size < (2 + 1))
6562 {
6563 warn (_("Internal error: not enough buffer room for section flag info"));
6564 return _("<unknown>");
6565 }
6566 size -= 2;
6567 *p++ = ',';
6568 *p++ = ' ';
6569 }
6570 sprintf (p, _("UNKNOWN (%*.*lx)"), field_size, field_size,
6571 (unsigned long) unknown_flags);
6572 p += 10 + field_size;
6573 }
6574 }
6575
6576 *p = '\0';
6577 return buff;
6578 }
6579
6580 static unsigned int ATTRIBUTE_WARN_UNUSED_RESULT
6581 get_compression_header (Elf_Internal_Chdr *chdr, unsigned char *buf, bfd_size_type size)
6582 {
6583 if (is_32bit_elf)
6584 {
6585 Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) buf;
6586
6587 if (size < sizeof (* echdr))
6588 {
6589 error (_("Compressed section is too small even for a compression header\n"));
6590 return 0;
6591 }
6592
6593 chdr->ch_type = BYTE_GET (echdr->ch_type);
6594 chdr->ch_size = BYTE_GET (echdr->ch_size);
6595 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
6596 return sizeof (*echdr);
6597 }
6598 else
6599 {
6600 Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) buf;
6601
6602 if (size < sizeof (* echdr))
6603 {
6604 error (_("Compressed section is too small even for a compression header\n"));
6605 return 0;
6606 }
6607
6608 chdr->ch_type = BYTE_GET (echdr->ch_type);
6609 chdr->ch_size = BYTE_GET (echdr->ch_size);
6610 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
6611 return sizeof (*echdr);
6612 }
6613 }
6614
6615 static bool
6616 process_section_headers (Filedata * filedata)
6617 {
6618 Elf_Internal_Shdr * section;
6619 unsigned int i;
6620
6621 if (filedata->file_header.e_shnum == 0)
6622 {
6623 /* PR binutils/12467. */
6624 if (filedata->file_header.e_shoff != 0)
6625 {
6626 warn (_("possibly corrupt ELF file header - it has a non-zero"
6627 " section header offset, but no section headers\n"));
6628 return false;
6629 }
6630 else if (do_sections)
6631 printf (_("\nThere are no sections in this file.\n"));
6632
6633 return true;
6634 }
6635
6636 if (do_sections && !do_header)
6637 {
6638 if (filedata->is_separate && process_links)
6639 printf (_("In linked file '%s': "), filedata->file_name);
6640 if (! filedata->is_separate || process_links)
6641 printf (ngettext ("There is %d section header, "
6642 "starting at offset 0x%lx:\n",
6643 "There are %d section headers, "
6644 "starting at offset 0x%lx:\n",
6645 filedata->file_header.e_shnum),
6646 filedata->file_header.e_shnum,
6647 (unsigned long) filedata->file_header.e_shoff);
6648 }
6649
6650 if (!get_section_headers (filedata, false))
6651 return false;
6652
6653 /* Read in the string table, so that we have names to display. */
6654 if (filedata->file_header.e_shstrndx != SHN_UNDEF
6655 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
6656 {
6657 section = filedata->section_headers + filedata->file_header.e_shstrndx;
6658
6659 if (section->sh_size != 0)
6660 {
6661 filedata->string_table = (char *) get_data (NULL, filedata, section->sh_offset,
6662 1, section->sh_size,
6663 _("string table"));
6664
6665 filedata->string_table_length = filedata->string_table != NULL ? section->sh_size : 0;
6666 }
6667 }
6668
6669 /* Scan the sections for the dynamic symbol table
6670 and dynamic string table and debug sections. */
6671 eh_addr_size = is_32bit_elf ? 4 : 8;
6672 switch (filedata->file_header.e_machine)
6673 {
6674 case EM_MIPS:
6675 case EM_MIPS_RS3_LE:
6676 /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
6677 FDE addresses. However, the ABI also has a semi-official ILP32
6678 variant for which the normal FDE address size rules apply.
6679
6680 GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
6681 section, where XX is the size of longs in bits. Unfortunately,
6682 earlier compilers provided no way of distinguishing ILP32 objects
6683 from LP64 objects, so if there's any doubt, we should assume that
6684 the official LP64 form is being used. */
6685 if ((filedata->file_header.e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64
6686 && find_section (filedata, ".gcc_compiled_long32") == NULL)
6687 eh_addr_size = 8;
6688 break;
6689
6690 case EM_H8_300:
6691 case EM_H8_300H:
6692 switch (filedata->file_header.e_flags & EF_H8_MACH)
6693 {
6694 case E_H8_MACH_H8300:
6695 case E_H8_MACH_H8300HN:
6696 case E_H8_MACH_H8300SN:
6697 case E_H8_MACH_H8300SXN:
6698 eh_addr_size = 2;
6699 break;
6700 case E_H8_MACH_H8300H:
6701 case E_H8_MACH_H8300S:
6702 case E_H8_MACH_H8300SX:
6703 eh_addr_size = 4;
6704 break;
6705 }
6706 break;
6707
6708 case EM_M32C_OLD:
6709 case EM_M32C:
6710 switch (filedata->file_header.e_flags & EF_M32C_CPU_MASK)
6711 {
6712 case EF_M32C_CPU_M16C:
6713 eh_addr_size = 2;
6714 break;
6715 }
6716 break;
6717 }
6718
6719 #define CHECK_ENTSIZE_VALUES(section, i, size32, size64) \
6720 do \
6721 { \
6722 bfd_size_type expected_entsize = is_32bit_elf ? size32 : size64; \
6723 if (section->sh_entsize != expected_entsize) \
6724 { \
6725 char buf[40]; \
6726 sprintf_vma (buf, section->sh_entsize); \
6727 /* Note: coded this way so that there is a single string for \
6728 translation. */ \
6729 error (_("Section %d has invalid sh_entsize of %s\n"), i, buf); \
6730 error (_("(Using the expected size of %u for the rest of this dump)\n"), \
6731 (unsigned) expected_entsize); \
6732 section->sh_entsize = expected_entsize; \
6733 } \
6734 } \
6735 while (0)
6736
6737 #define CHECK_ENTSIZE(section, i, type) \
6738 CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type), \
6739 sizeof (Elf64_External_##type))
6740
6741 for (i = 0, section = filedata->section_headers;
6742 i < filedata->file_header.e_shnum;
6743 i++, section++)
6744 {
6745 const char *name = section_name_print (filedata, section);
6746
6747 /* Run some sanity checks on the headers and
6748 possibly fill in some file data as well. */
6749 switch (section->sh_type)
6750 {
6751 case SHT_DYNSYM:
6752 if (filedata->dynamic_symbols != NULL)
6753 {
6754 error (_("File contains multiple dynamic symbol tables\n"));
6755 continue;
6756 }
6757
6758 CHECK_ENTSIZE (section, i, Sym);
6759 filedata->dynamic_symbols
6760 = get_elf_symbols (filedata, section, &filedata->num_dynamic_syms);
6761 filedata->dynamic_symtab_section = section;
6762 break;
6763
6764 case SHT_STRTAB:
6765 if (streq (name, ".dynstr"))
6766 {
6767 if (filedata->dynamic_strings != NULL)
6768 {
6769 error (_("File contains multiple dynamic string tables\n"));
6770 continue;
6771 }
6772
6773 filedata->dynamic_strings
6774 = (char *) get_data (NULL, filedata, section->sh_offset,
6775 1, section->sh_size, _("dynamic strings"));
6776 filedata->dynamic_strings_length
6777 = filedata->dynamic_strings == NULL ? 0 : section->sh_size;
6778 filedata->dynamic_strtab_section = section;
6779 }
6780 break;
6781
6782 case SHT_SYMTAB_SHNDX:
6783 {
6784 elf_section_list * entry = xmalloc (sizeof * entry);
6785
6786 entry->hdr = section;
6787 entry->next = filedata->symtab_shndx_list;
6788 filedata->symtab_shndx_list = entry;
6789 }
6790 break;
6791
6792 case SHT_SYMTAB:
6793 CHECK_ENTSIZE (section, i, Sym);
6794 break;
6795
6796 case SHT_GROUP:
6797 CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
6798 break;
6799
6800 case SHT_REL:
6801 CHECK_ENTSIZE (section, i, Rel);
6802 if (do_checks && section->sh_size == 0)
6803 warn (_("Section '%s': zero-sized relocation section\n"), name);
6804 break;
6805
6806 case SHT_RELA:
6807 CHECK_ENTSIZE (section, i, Rela);
6808 if (do_checks && section->sh_size == 0)
6809 warn (_("Section '%s': zero-sized relocation section\n"), name);
6810 break;
6811
6812 case SHT_NOTE:
6813 case SHT_PROGBITS:
6814 /* Having a zero sized section is not illegal according to the
6815 ELF standard, but it might be an indication that something
6816 is wrong. So issue a warning if we are running in lint mode. */
6817 if (do_checks && section->sh_size == 0)
6818 warn (_("Section '%s': has a size of zero - is this intended ?\n"), name);
6819 break;
6820
6821 default:
6822 break;
6823 }
6824
6825 if ((do_debugging || do_debug_info || do_debug_abbrevs
6826 || do_debug_lines || do_debug_pubnames || do_debug_pubtypes
6827 || do_debug_aranges || do_debug_frames || do_debug_macinfo
6828 || do_debug_str || do_debug_str_offsets || do_debug_loc
6829 || do_debug_ranges
6830 || do_debug_addr || do_debug_cu_index || do_debug_links)
6831 && (startswith (name, ".debug_")
6832 || startswith (name, ".zdebug_")))
6833 {
6834 if (name[1] == 'z')
6835 name += sizeof (".zdebug_") - 1;
6836 else
6837 name += sizeof (".debug_") - 1;
6838
6839 if (do_debugging
6840 || (do_debug_info && startswith (name, "info"))
6841 || (do_debug_info && startswith (name, "types"))
6842 || (do_debug_abbrevs && startswith (name, "abbrev"))
6843 || (do_debug_lines && strcmp (name, "line") == 0)
6844 || (do_debug_lines && startswith (name, "line."))
6845 || (do_debug_pubnames && startswith (name, "pubnames"))
6846 || (do_debug_pubtypes && startswith (name, "pubtypes"))
6847 || (do_debug_pubnames && startswith (name, "gnu_pubnames"))
6848 || (do_debug_pubtypes && startswith (name, "gnu_pubtypes"))
6849 || (do_debug_aranges && startswith (name, "aranges"))
6850 || (do_debug_ranges && startswith (name, "ranges"))
6851 || (do_debug_ranges && startswith (name, "rnglists"))
6852 || (do_debug_frames && startswith (name, "frame"))
6853 || (do_debug_macinfo && startswith (name, "macinfo"))
6854 || (do_debug_macinfo && startswith (name, "macro"))
6855 || (do_debug_str && startswith (name, "str"))
6856 || (do_debug_links && startswith (name, "sup"))
6857 || (do_debug_str_offsets && startswith (name, "str_offsets"))
6858 || (do_debug_loc && startswith (name, "loc"))
6859 || (do_debug_loc && startswith (name, "loclists"))
6860 || (do_debug_addr && startswith (name, "addr"))
6861 || (do_debug_cu_index && startswith (name, "cu_index"))
6862 || (do_debug_cu_index && startswith (name, "tu_index"))
6863 )
6864 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6865 }
6866 /* Linkonce section to be combined with .debug_info at link time. */
6867 else if ((do_debugging || do_debug_info)
6868 && startswith (name, ".gnu.linkonce.wi."))
6869 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6870 else if (do_debug_frames && streq (name, ".eh_frame"))
6871 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6872 else if (do_gdb_index && (streq (name, ".gdb_index")
6873 || streq (name, ".debug_names")))
6874 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6875 /* Trace sections for Itanium VMS. */
6876 else if ((do_debugging || do_trace_info || do_trace_abbrevs
6877 || do_trace_aranges)
6878 && startswith (name, ".trace_"))
6879 {
6880 name += sizeof (".trace_") - 1;
6881
6882 if (do_debugging
6883 || (do_trace_info && streq (name, "info"))
6884 || (do_trace_abbrevs && streq (name, "abbrev"))
6885 || (do_trace_aranges && streq (name, "aranges"))
6886 )
6887 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6888 }
6889 else if ((do_debugging || do_debug_links)
6890 && (startswith (name, ".gnu_debuglink")
6891 || startswith (name, ".gnu_debugaltlink")))
6892 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6893 }
6894
6895 if (! do_sections)
6896 return true;
6897
6898 if (filedata->is_separate && ! process_links)
6899 return true;
6900
6901 if (filedata->is_separate)
6902 printf (_("\nSection Headers in linked file '%s':\n"), filedata->file_name);
6903 else if (filedata->file_header.e_shnum > 1)
6904 printf (_("\nSection Headers:\n"));
6905 else
6906 printf (_("\nSection Header:\n"));
6907
6908 if (is_32bit_elf)
6909 {
6910 if (do_section_details)
6911 {
6912 printf (_(" [Nr] Name\n"));
6913 printf (_(" Type Addr Off Size ES Lk Inf Al\n"));
6914 }
6915 else
6916 printf
6917 (_(" [Nr] Name Type Addr Off Size ES Flg Lk Inf Al\n"));
6918 }
6919 else if (do_wide)
6920 {
6921 if (do_section_details)
6922 {
6923 printf (_(" [Nr] Name\n"));
6924 printf (_(" Type Address Off Size ES Lk Inf Al\n"));
6925 }
6926 else
6927 printf
6928 (_(" [Nr] Name Type Address Off Size ES Flg Lk Inf Al\n"));
6929 }
6930 else
6931 {
6932 if (do_section_details)
6933 {
6934 printf (_(" [Nr] Name\n"));
6935 printf (_(" Type Address Offset Link\n"));
6936 printf (_(" Size EntSize Info Align\n"));
6937 }
6938 else
6939 {
6940 printf (_(" [Nr] Name Type Address Offset\n"));
6941 printf (_(" Size EntSize Flags Link Info Align\n"));
6942 }
6943 }
6944
6945 if (do_section_details)
6946 printf (_(" Flags\n"));
6947
6948 for (i = 0, section = filedata->section_headers;
6949 i < filedata->file_header.e_shnum;
6950 i++, section++)
6951 {
6952 /* Run some sanity checks on the section header. */
6953
6954 /* Check the sh_link field. */
6955 switch (section->sh_type)
6956 {
6957 case SHT_REL:
6958 case SHT_RELA:
6959 if (section->sh_link == 0
6960 && (filedata->file_header.e_type == ET_EXEC
6961 || filedata->file_header.e_type == ET_DYN))
6962 /* A dynamic relocation section where all entries use a
6963 zero symbol index need not specify a symtab section. */
6964 break;
6965 /* Fall through. */
6966 case SHT_SYMTAB_SHNDX:
6967 case SHT_GROUP:
6968 case SHT_HASH:
6969 case SHT_GNU_HASH:
6970 case SHT_GNU_versym:
6971 if (section->sh_link == 0
6972 || section->sh_link >= filedata->file_header.e_shnum
6973 || (filedata->section_headers[section->sh_link].sh_type != SHT_SYMTAB
6974 && filedata->section_headers[section->sh_link].sh_type != SHT_DYNSYM))
6975 warn (_("[%2u]: Link field (%u) should index a symtab section.\n"),
6976 i, section->sh_link);
6977 break;
6978
6979 case SHT_DYNAMIC:
6980 case SHT_SYMTAB:
6981 case SHT_DYNSYM:
6982 case SHT_GNU_verneed:
6983 case SHT_GNU_verdef:
6984 case SHT_GNU_LIBLIST:
6985 if (section->sh_link == 0
6986 || section->sh_link >= filedata->file_header.e_shnum
6987 || filedata->section_headers[section->sh_link].sh_type != SHT_STRTAB)
6988 warn (_("[%2u]: Link field (%u) should index a string section.\n"),
6989 i, section->sh_link);
6990 break;
6991
6992 case SHT_INIT_ARRAY:
6993 case SHT_FINI_ARRAY:
6994 case SHT_PREINIT_ARRAY:
6995 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
6996 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
6997 i, section->sh_link);
6998 break;
6999
7000 default:
7001 /* FIXME: Add support for target specific section types. */
7002 #if 0 /* Currently we do not check other section types as there are too
7003 many special cases. Stab sections for example have a type
7004 of SHT_PROGBITS but an sh_link field that links to the .stabstr
7005 section. */
7006 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
7007 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
7008 i, section->sh_link);
7009 #endif
7010 break;
7011 }
7012
7013 /* Check the sh_info field. */
7014 switch (section->sh_type)
7015 {
7016 case SHT_REL:
7017 case SHT_RELA:
7018 if (section->sh_info == 0
7019 && (filedata->file_header.e_type == ET_EXEC
7020 || filedata->file_header.e_type == ET_DYN))
7021 /* Dynamic relocations apply to segments, so they do not
7022 need to specify the section they relocate. */
7023 break;
7024 if (section->sh_info == 0
7025 || section->sh_info >= filedata->file_header.e_shnum
7026 || (filedata->section_headers[section->sh_info].sh_type != SHT_PROGBITS
7027 && filedata->section_headers[section->sh_info].sh_type != SHT_NOBITS
7028 && filedata->section_headers[section->sh_info].sh_type != SHT_NOTE
7029 && filedata->section_headers[section->sh_info].sh_type != SHT_INIT_ARRAY
7030 && filedata->section_headers[section->sh_info].sh_type != SHT_FINI_ARRAY
7031 && filedata->section_headers[section->sh_info].sh_type != SHT_PREINIT_ARRAY
7032 /* FIXME: Are other section types valid ? */
7033 && filedata->section_headers[section->sh_info].sh_type < SHT_LOOS))
7034 warn (_("[%2u]: Info field (%u) should index a relocatable section.\n"),
7035 i, section->sh_info);
7036 break;
7037
7038 case SHT_DYNAMIC:
7039 case SHT_HASH:
7040 case SHT_SYMTAB_SHNDX:
7041 case SHT_INIT_ARRAY:
7042 case SHT_FINI_ARRAY:
7043 case SHT_PREINIT_ARRAY:
7044 if (section->sh_info != 0)
7045 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
7046 i, section->sh_info);
7047 break;
7048
7049 case SHT_GROUP:
7050 case SHT_SYMTAB:
7051 case SHT_DYNSYM:
7052 /* A symbol index - we assume that it is valid. */
7053 break;
7054
7055 default:
7056 /* FIXME: Add support for target specific section types. */
7057 if (section->sh_type == SHT_NOBITS)
7058 /* NOBITS section headers with non-zero sh_info fields can be
7059 created when a binary is stripped of everything but its debug
7060 information. The stripped sections have their headers
7061 preserved but their types set to SHT_NOBITS. So do not check
7062 this type of section. */
7063 ;
7064 else if (section->sh_flags & SHF_INFO_LINK)
7065 {
7066 if (section->sh_info < 1 || section->sh_info >= filedata->file_header.e_shnum)
7067 warn (_("[%2u]: Expected link to another section in info field"), i);
7068 }
7069 else if (section->sh_type < SHT_LOOS
7070 && (section->sh_flags & SHF_GNU_MBIND) == 0
7071 && section->sh_info != 0)
7072 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
7073 i, section->sh_info);
7074 break;
7075 }
7076
7077 /* Check the sh_size field. */
7078 if (section->sh_size > filedata->file_size
7079 && section->sh_type != SHT_NOBITS
7080 && section->sh_type != SHT_NULL
7081 && section->sh_type < SHT_LOOS)
7082 warn (_("Size of section %u is larger than the entire file!\n"), i);
7083
7084 printf (" [%2u] ", i);
7085 if (do_section_details)
7086 printf ("%s\n ", printable_section_name (filedata, section));
7087 else
7088 print_symbol (-17, section_name_print (filedata, section));
7089
7090 printf (do_wide ? " %-15s " : " %-15.15s ",
7091 get_section_type_name (filedata, section->sh_type));
7092
7093 if (is_32bit_elf)
7094 {
7095 const char * link_too_big = NULL;
7096
7097 print_vma (section->sh_addr, LONG_HEX);
7098
7099 printf ( " %6.6lx %6.6lx %2.2lx",
7100 (unsigned long) section->sh_offset,
7101 (unsigned long) section->sh_size,
7102 (unsigned long) section->sh_entsize);
7103
7104 if (do_section_details)
7105 fputs (" ", stdout);
7106 else
7107 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7108
7109 if (section->sh_link >= filedata->file_header.e_shnum)
7110 {
7111 link_too_big = "";
7112 /* The sh_link value is out of range. Normally this indicates
7113 an error but it can have special values in Solaris binaries. */
7114 switch (filedata->file_header.e_machine)
7115 {
7116 case EM_386:
7117 case EM_IAMCU:
7118 case EM_X86_64:
7119 case EM_L1OM:
7120 case EM_K1OM:
7121 case EM_OLD_SPARCV9:
7122 case EM_SPARC32PLUS:
7123 case EM_SPARCV9:
7124 case EM_SPARC:
7125 if (section->sh_link == (SHN_BEFORE & 0xffff))
7126 link_too_big = "BEFORE";
7127 else if (section->sh_link == (SHN_AFTER & 0xffff))
7128 link_too_big = "AFTER";
7129 break;
7130 default:
7131 break;
7132 }
7133 }
7134
7135 if (do_section_details)
7136 {
7137 if (link_too_big != NULL && * link_too_big)
7138 printf ("<%s> ", link_too_big);
7139 else
7140 printf ("%2u ", section->sh_link);
7141 printf ("%3u %2lu\n", section->sh_info,
7142 (unsigned long) section->sh_addralign);
7143 }
7144 else
7145 printf ("%2u %3u %2lu\n",
7146 section->sh_link,
7147 section->sh_info,
7148 (unsigned long) section->sh_addralign);
7149
7150 if (link_too_big && ! * link_too_big)
7151 warn (_("section %u: sh_link value of %u is larger than the number of sections\n"),
7152 i, section->sh_link);
7153 }
7154 else if (do_wide)
7155 {
7156 print_vma (section->sh_addr, LONG_HEX);
7157
7158 if ((long) section->sh_offset == section->sh_offset)
7159 printf (" %6.6lx", (unsigned long) section->sh_offset);
7160 else
7161 {
7162 putchar (' ');
7163 print_vma (section->sh_offset, LONG_HEX);
7164 }
7165
7166 if ((unsigned long) section->sh_size == section->sh_size)
7167 printf (" %6.6lx", (unsigned long) section->sh_size);
7168 else
7169 {
7170 putchar (' ');
7171 print_vma (section->sh_size, LONG_HEX);
7172 }
7173
7174 if ((unsigned long) section->sh_entsize == section->sh_entsize)
7175 printf (" %2.2lx", (unsigned long) section->sh_entsize);
7176 else
7177 {
7178 putchar (' ');
7179 print_vma (section->sh_entsize, LONG_HEX);
7180 }
7181
7182 if (do_section_details)
7183 fputs (" ", stdout);
7184 else
7185 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7186
7187 printf ("%2u %3u ", section->sh_link, section->sh_info);
7188
7189 if ((unsigned long) section->sh_addralign == section->sh_addralign)
7190 printf ("%2lu\n", (unsigned long) section->sh_addralign);
7191 else
7192 {
7193 print_vma (section->sh_addralign, DEC);
7194 putchar ('\n');
7195 }
7196 }
7197 else if (do_section_details)
7198 {
7199 putchar (' ');
7200 print_vma (section->sh_addr, LONG_HEX);
7201 if ((long) section->sh_offset == section->sh_offset)
7202 printf (" %16.16lx", (unsigned long) section->sh_offset);
7203 else
7204 {
7205 printf (" ");
7206 print_vma (section->sh_offset, LONG_HEX);
7207 }
7208 printf (" %u\n ", section->sh_link);
7209 print_vma (section->sh_size, LONG_HEX);
7210 putchar (' ');
7211 print_vma (section->sh_entsize, LONG_HEX);
7212
7213 printf (" %-16u %lu\n",
7214 section->sh_info,
7215 (unsigned long) section->sh_addralign);
7216 }
7217 else
7218 {
7219 putchar (' ');
7220 print_vma (section->sh_addr, LONG_HEX);
7221 if ((long) section->sh_offset == section->sh_offset)
7222 printf (" %8.8lx", (unsigned long) section->sh_offset);
7223 else
7224 {
7225 printf (" ");
7226 print_vma (section->sh_offset, LONG_HEX);
7227 }
7228 printf ("\n ");
7229 print_vma (section->sh_size, LONG_HEX);
7230 printf (" ");
7231 print_vma (section->sh_entsize, LONG_HEX);
7232
7233 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7234
7235 printf (" %2u %3u %lu\n",
7236 section->sh_link,
7237 section->sh_info,
7238 (unsigned long) section->sh_addralign);
7239 }
7240
7241 if (do_section_details)
7242 {
7243 printf (" %s\n", get_elf_section_flags (filedata, section->sh_flags));
7244 if ((section->sh_flags & SHF_COMPRESSED) != 0)
7245 {
7246 /* Minimum section size is 12 bytes for 32-bit compression
7247 header + 12 bytes for compressed data header. */
7248 unsigned char buf[24];
7249
7250 assert (sizeof (buf) >= sizeof (Elf64_External_Chdr));
7251 if (get_data (&buf, filedata, section->sh_offset, 1,
7252 sizeof (buf), _("compression header")))
7253 {
7254 Elf_Internal_Chdr chdr;
7255
7256 if (get_compression_header (&chdr, buf, sizeof (buf)) == 0)
7257 printf (_(" [<corrupt>]\n"));
7258 else
7259 {
7260 if (chdr.ch_type == ELFCOMPRESS_ZLIB)
7261 printf (" ZLIB, ");
7262 else
7263 printf (_(" [<unknown>: 0x%x], "),
7264 chdr.ch_type);
7265 print_vma (chdr.ch_size, LONG_HEX);
7266 printf (", %lu\n", (unsigned long) chdr.ch_addralign);
7267 }
7268 }
7269 }
7270 }
7271 }
7272
7273 if (!do_section_details)
7274 {
7275 /* The ordering of the letters shown here matches the ordering of the
7276 corresponding SHF_xxx values, and hence the order in which these
7277 letters will be displayed to the user. */
7278 printf (_("Key to Flags:\n\
7279 W (write), A (alloc), X (execute), M (merge), S (strings), I (info),\n\
7280 L (link order), O (extra OS processing required), G (group), T (TLS),\n\
7281 C (compressed), x (unknown), o (OS specific), E (exclude),\n "));
7282 switch (filedata->file_header.e_ident[EI_OSABI])
7283 {
7284 case ELFOSABI_GNU:
7285 case ELFOSABI_FREEBSD:
7286 printf (_("R (retain), "));
7287 /* Fall through */
7288 case ELFOSABI_NONE:
7289 printf (_("D (mbind), "));
7290 break;
7291 default:
7292 break;
7293 }
7294 if (filedata->file_header.e_machine == EM_X86_64
7295 || filedata->file_header.e_machine == EM_L1OM
7296 || filedata->file_header.e_machine == EM_K1OM)
7297 printf (_("l (large), "));
7298 else if (filedata->file_header.e_machine == EM_ARM)
7299 printf (_("y (purecode), "));
7300 else if (filedata->file_header.e_machine == EM_PPC)
7301 printf (_("v (VLE), "));
7302 printf ("p (processor specific)\n");
7303 }
7304
7305 return true;
7306 }
7307
7308 static bool
7309 get_symtab (Filedata *filedata, Elf_Internal_Shdr *symsec,
7310 Elf_Internal_Sym **symtab, unsigned long *nsyms,
7311 char **strtab, unsigned long *strtablen)
7312 {
7313 *strtab = NULL;
7314 *strtablen = 0;
7315 *symtab = get_elf_symbols (filedata, symsec, nsyms);
7316
7317 if (*symtab == NULL)
7318 return false;
7319
7320 if (symsec->sh_link != 0)
7321 {
7322 Elf_Internal_Shdr *strsec;
7323
7324 if (symsec->sh_link >= filedata->file_header.e_shnum)
7325 {
7326 error (_("Bad sh_link in symbol table section\n"));
7327 free (*symtab);
7328 *symtab = NULL;
7329 *nsyms = 0;
7330 return false;
7331 }
7332
7333 strsec = filedata->section_headers + symsec->sh_link;
7334
7335 *strtab = (char *) get_data (NULL, filedata, strsec->sh_offset,
7336 1, strsec->sh_size, _("string table"));
7337 if (*strtab == NULL)
7338 {
7339 free (*symtab);
7340 *symtab = NULL;
7341 *nsyms = 0;
7342 return false;
7343 }
7344 *strtablen = strsec->sh_size;
7345 }
7346 return true;
7347 }
7348
7349 static const char *
7350 get_group_flags (unsigned int flags)
7351 {
7352 static char buff[128];
7353
7354 if (flags == 0)
7355 return "";
7356 else if (flags == GRP_COMDAT)
7357 return "COMDAT ";
7358
7359 snprintf (buff, sizeof buff, "[0x%x: %s%s%s]",
7360 flags,
7361 flags & GRP_MASKOS ? _("<OS specific>") : "",
7362 flags & GRP_MASKPROC ? _("<PROC specific>") : "",
7363 (flags & ~(GRP_COMDAT | GRP_MASKOS | GRP_MASKPROC)
7364 ? _("<unknown>") : ""));
7365
7366 return buff;
7367 }
7368
7369 static bool
7370 process_section_groups (Filedata * filedata)
7371 {
7372 Elf_Internal_Shdr * section;
7373 unsigned int i;
7374 struct group * group;
7375 Elf_Internal_Shdr * symtab_sec;
7376 Elf_Internal_Shdr * strtab_sec;
7377 Elf_Internal_Sym * symtab;
7378 unsigned long num_syms;
7379 char * strtab;
7380 size_t strtab_size;
7381
7382 /* Don't process section groups unless needed. */
7383 if (!do_unwind && !do_section_groups)
7384 return true;
7385
7386 if (filedata->file_header.e_shnum == 0)
7387 {
7388 if (do_section_groups)
7389 {
7390 if (filedata->is_separate)
7391 printf (_("\nThere are no sections group in linked file '%s'.\n"),
7392 filedata->file_name);
7393 else
7394 printf (_("\nThere are no section groups in this file.\n"));
7395 }
7396 return true;
7397 }
7398
7399 if (filedata->section_headers == NULL)
7400 {
7401 error (_("Section headers are not available!\n"));
7402 /* PR 13622: This can happen with a corrupt ELF header. */
7403 return false;
7404 }
7405
7406 filedata->section_headers_groups
7407 = (struct group **) calloc (filedata->file_header.e_shnum,
7408 sizeof (struct group *));
7409
7410 if (filedata->section_headers_groups == NULL)
7411 {
7412 error (_("Out of memory reading %u section group headers\n"),
7413 filedata->file_header.e_shnum);
7414 return false;
7415 }
7416
7417 /* Scan the sections for the group section. */
7418 filedata->group_count = 0;
7419 for (i = 0, section = filedata->section_headers;
7420 i < filedata->file_header.e_shnum;
7421 i++, section++)
7422 if (section->sh_type == SHT_GROUP)
7423 filedata->group_count++;
7424
7425 if (filedata->group_count == 0)
7426 {
7427 if (do_section_groups)
7428 {
7429 if (filedata->is_separate)
7430 printf (_("\nThere are no section groups in linked file '%s'.\n"),
7431 filedata->file_name);
7432 else
7433 printf (_("\nThere are no section groups in this file.\n"));
7434 }
7435
7436 return true;
7437 }
7438
7439 filedata->section_groups = (struct group *) calloc (filedata->group_count,
7440 sizeof (struct group));
7441
7442 if (filedata->section_groups == NULL)
7443 {
7444 error (_("Out of memory reading %lu groups\n"),
7445 (unsigned long) filedata->group_count);
7446 return false;
7447 }
7448
7449 symtab_sec = NULL;
7450 strtab_sec = NULL;
7451 symtab = NULL;
7452 num_syms = 0;
7453 strtab = NULL;
7454 strtab_size = 0;
7455
7456 if (filedata->is_separate)
7457 printf (_("Section groups in linked file '%s'\n"), filedata->file_name);
7458
7459 for (i = 0, section = filedata->section_headers, group = filedata->section_groups;
7460 i < filedata->file_header.e_shnum;
7461 i++, section++)
7462 {
7463 if (section->sh_type == SHT_GROUP)
7464 {
7465 const char * name = printable_section_name (filedata, section);
7466 const char * group_name;
7467 unsigned char * start;
7468 unsigned char * indices;
7469 unsigned int entry, j, size;
7470 Elf_Internal_Shdr * sec;
7471 Elf_Internal_Sym * sym;
7472
7473 /* Get the symbol table. */
7474 if (section->sh_link >= filedata->file_header.e_shnum
7475 || ((sec = filedata->section_headers + section->sh_link)->sh_type
7476 != SHT_SYMTAB))
7477 {
7478 error (_("Bad sh_link in group section `%s'\n"), name);
7479 continue;
7480 }
7481
7482 if (symtab_sec != sec)
7483 {
7484 symtab_sec = sec;
7485 free (symtab);
7486 symtab = get_elf_symbols (filedata, symtab_sec, & num_syms);
7487 }
7488
7489 if (symtab == NULL)
7490 {
7491 error (_("Corrupt header in group section `%s'\n"), name);
7492 continue;
7493 }
7494
7495 if (section->sh_info >= num_syms)
7496 {
7497 error (_("Bad sh_info in group section `%s'\n"), name);
7498 continue;
7499 }
7500
7501 sym = symtab + section->sh_info;
7502
7503 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
7504 {
7505 if (sym->st_shndx == 0
7506 || sym->st_shndx >= filedata->file_header.e_shnum)
7507 {
7508 error (_("Bad sh_info in group section `%s'\n"), name);
7509 continue;
7510 }
7511
7512 group_name = section_name_print (filedata,
7513 filedata->section_headers
7514 + sym->st_shndx);
7515 strtab_sec = NULL;
7516 free (strtab);
7517 strtab = NULL;
7518 strtab_size = 0;
7519 }
7520 else
7521 {
7522 /* Get the string table. */
7523 if (symtab_sec->sh_link >= filedata->file_header.e_shnum)
7524 {
7525 strtab_sec = NULL;
7526 free (strtab);
7527 strtab = NULL;
7528 strtab_size = 0;
7529 }
7530 else if (strtab_sec
7531 != (sec = filedata->section_headers + symtab_sec->sh_link))
7532 {
7533 strtab_sec = sec;
7534 free (strtab);
7535
7536 strtab = (char *) get_data (NULL, filedata, strtab_sec->sh_offset,
7537 1, strtab_sec->sh_size,
7538 _("string table"));
7539 strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
7540 }
7541 group_name = sym->st_name < strtab_size
7542 ? strtab + sym->st_name : _("<corrupt>");
7543 }
7544
7545 /* PR 17531: file: loop. */
7546 if (section->sh_entsize > section->sh_size)
7547 {
7548 error (_("Section %s has sh_entsize (0x%lx) which is larger than its size (0x%lx)\n"),
7549 printable_section_name (filedata, section),
7550 (unsigned long) section->sh_entsize,
7551 (unsigned long) section->sh_size);
7552 continue;
7553 }
7554
7555 start = (unsigned char *) get_data (NULL, filedata, section->sh_offset,
7556 1, section->sh_size,
7557 _("section data"));
7558 if (start == NULL)
7559 continue;
7560
7561 indices = start;
7562 size = (section->sh_size / section->sh_entsize) - 1;
7563 entry = byte_get (indices, 4);
7564 indices += 4;
7565
7566 if (do_section_groups)
7567 {
7568 printf (_("\n%sgroup section [%5u] `%s' [%s] contains %u sections:\n"),
7569 get_group_flags (entry), i, name, group_name, size);
7570
7571 printf (_(" [Index] Name\n"));
7572 }
7573
7574 group->group_index = i;
7575
7576 for (j = 0; j < size; j++)
7577 {
7578 struct group_list * g;
7579
7580 entry = byte_get (indices, 4);
7581 indices += 4;
7582
7583 if (entry >= filedata->file_header.e_shnum)
7584 {
7585 static unsigned num_group_errors = 0;
7586
7587 if (num_group_errors ++ < 10)
7588 {
7589 error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
7590 entry, i, filedata->file_header.e_shnum - 1);
7591 if (num_group_errors == 10)
7592 warn (_("Further error messages about overlarge group section indices suppressed\n"));
7593 }
7594 continue;
7595 }
7596
7597 if (filedata->section_headers_groups [entry] != NULL)
7598 {
7599 if (entry)
7600 {
7601 static unsigned num_errs = 0;
7602
7603 if (num_errs ++ < 10)
7604 {
7605 error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
7606 entry, i,
7607 filedata->section_headers_groups [entry]->group_index);
7608 if (num_errs == 10)
7609 warn (_("Further error messages about already contained group sections suppressed\n"));
7610 }
7611 continue;
7612 }
7613 else
7614 {
7615 /* Intel C/C++ compiler may put section 0 in a
7616 section group. We just warn it the first time
7617 and ignore it afterwards. */
7618 static bool warned = false;
7619 if (!warned)
7620 {
7621 error (_("section 0 in group section [%5u]\n"),
7622 filedata->section_headers_groups [entry]->group_index);
7623 warned = true;
7624 }
7625 }
7626 }
7627
7628 filedata->section_headers_groups [entry] = group;
7629
7630 if (do_section_groups)
7631 {
7632 sec = filedata->section_headers + entry;
7633 printf (" [%5u] %s\n", entry, printable_section_name (filedata, sec));
7634 }
7635
7636 g = (struct group_list *) xmalloc (sizeof (struct group_list));
7637 g->section_index = entry;
7638 g->next = group->root;
7639 group->root = g;
7640 }
7641
7642 free (start);
7643
7644 group++;
7645 }
7646 }
7647
7648 free (symtab);
7649 free (strtab);
7650 return true;
7651 }
7652
7653 /* Data used to display dynamic fixups. */
7654
7655 struct ia64_vms_dynfixup
7656 {
7657 bfd_vma needed_ident; /* Library ident number. */
7658 bfd_vma needed; /* Index in the dstrtab of the library name. */
7659 bfd_vma fixup_needed; /* Index of the library. */
7660 bfd_vma fixup_rela_cnt; /* Number of fixups. */
7661 bfd_vma fixup_rela_off; /* Fixups offset in the dynamic segment. */
7662 };
7663
7664 /* Data used to display dynamic relocations. */
7665
7666 struct ia64_vms_dynimgrela
7667 {
7668 bfd_vma img_rela_cnt; /* Number of relocations. */
7669 bfd_vma img_rela_off; /* Reloc offset in the dynamic segment. */
7670 };
7671
7672 /* Display IA-64 OpenVMS dynamic fixups (used to dynamically link a shared
7673 library). */
7674
7675 static bool
7676 dump_ia64_vms_dynamic_fixups (Filedata * filedata,
7677 struct ia64_vms_dynfixup * fixup,
7678 const char * strtab,
7679 unsigned int strtab_sz)
7680 {
7681 Elf64_External_VMS_IMAGE_FIXUP * imfs;
7682 long i;
7683 const char * lib_name;
7684
7685 imfs = get_data (NULL, filedata,
7686 filedata->dynamic_addr + fixup->fixup_rela_off,
7687 sizeof (*imfs), fixup->fixup_rela_cnt,
7688 _("dynamic section image fixups"));
7689 if (!imfs)
7690 return false;
7691
7692 if (fixup->needed < strtab_sz)
7693 lib_name = strtab + fixup->needed;
7694 else
7695 {
7696 warn (_("corrupt library name index of 0x%lx found in dynamic entry"),
7697 (unsigned long) fixup->needed);
7698 lib_name = "???";
7699 }
7700
7701 printf (_("\nImage fixups for needed library #%d: %s - ident: %lx\n"),
7702 (int) fixup->fixup_needed, lib_name, (long) fixup->needed_ident);
7703 printf
7704 (_("Seg Offset Type SymVec DataType\n"));
7705
7706 for (i = 0; i < (long) fixup->fixup_rela_cnt; i++)
7707 {
7708 unsigned int type;
7709 const char *rtype;
7710
7711 printf ("%3u ", (unsigned) BYTE_GET (imfs [i].fixup_seg));
7712 printf_vma ((bfd_vma) BYTE_GET (imfs [i].fixup_offset));
7713 type = BYTE_GET (imfs [i].type);
7714 rtype = elf_ia64_reloc_type (type);
7715 if (rtype == NULL)
7716 printf (" 0x%08x ", type);
7717 else
7718 printf (" %-32s ", rtype);
7719 printf ("%6u ", (unsigned) BYTE_GET (imfs [i].symvec_index));
7720 printf ("0x%08x\n", (unsigned) BYTE_GET (imfs [i].data_type));
7721 }
7722
7723 free (imfs);
7724 return true;
7725 }
7726
7727 /* Display IA-64 OpenVMS dynamic relocations (used to relocate an image). */
7728
7729 static bool
7730 dump_ia64_vms_dynamic_relocs (Filedata * filedata, struct ia64_vms_dynimgrela *imgrela)
7731 {
7732 Elf64_External_VMS_IMAGE_RELA *imrs;
7733 long i;
7734
7735 imrs = get_data (NULL, filedata,
7736 filedata->dynamic_addr + imgrela->img_rela_off,
7737 sizeof (*imrs), imgrela->img_rela_cnt,
7738 _("dynamic section image relocations"));
7739 if (!imrs)
7740 return false;
7741
7742 printf (_("\nImage relocs\n"));
7743 printf
7744 (_("Seg Offset Type Addend Seg Sym Off\n"));
7745
7746 for (i = 0; i < (long) imgrela->img_rela_cnt; i++)
7747 {
7748 unsigned int type;
7749 const char *rtype;
7750
7751 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].rela_seg));
7752 printf ("%08" BFD_VMA_FMT "x ",
7753 (bfd_vma) BYTE_GET (imrs [i].rela_offset));
7754 type = BYTE_GET (imrs [i].type);
7755 rtype = elf_ia64_reloc_type (type);
7756 if (rtype == NULL)
7757 printf ("0x%08x ", type);
7758 else
7759 printf ("%-31s ", rtype);
7760 print_vma (BYTE_GET (imrs [i].addend), FULL_HEX);
7761 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].sym_seg));
7762 printf ("%08" BFD_VMA_FMT "x\n",
7763 (bfd_vma) BYTE_GET (imrs [i].sym_offset));
7764 }
7765
7766 free (imrs);
7767 return true;
7768 }
7769
7770 /* Display IA-64 OpenVMS dynamic relocations and fixups. */
7771
7772 static bool
7773 process_ia64_vms_dynamic_relocs (Filedata * filedata)
7774 {
7775 struct ia64_vms_dynfixup fixup;
7776 struct ia64_vms_dynimgrela imgrela;
7777 Elf_Internal_Dyn *entry;
7778 bfd_vma strtab_off = 0;
7779 bfd_vma strtab_sz = 0;
7780 char *strtab = NULL;
7781 bool res = true;
7782
7783 memset (&fixup, 0, sizeof (fixup));
7784 memset (&imgrela, 0, sizeof (imgrela));
7785
7786 /* Note: the order of the entries is specified by the OpenVMS specs. */
7787 for (entry = filedata->dynamic_section;
7788 entry < filedata->dynamic_section + filedata->dynamic_nent;
7789 entry++)
7790 {
7791 switch (entry->d_tag)
7792 {
7793 case DT_IA_64_VMS_STRTAB_OFFSET:
7794 strtab_off = entry->d_un.d_val;
7795 break;
7796 case DT_STRSZ:
7797 strtab_sz = entry->d_un.d_val;
7798 if (strtab == NULL)
7799 strtab = get_data (NULL, filedata,
7800 filedata->dynamic_addr + strtab_off,
7801 1, strtab_sz, _("dynamic string section"));
7802 if (strtab == NULL)
7803 strtab_sz = 0;
7804 break;
7805
7806 case DT_IA_64_VMS_NEEDED_IDENT:
7807 fixup.needed_ident = entry->d_un.d_val;
7808 break;
7809 case DT_NEEDED:
7810 fixup.needed = entry->d_un.d_val;
7811 break;
7812 case DT_IA_64_VMS_FIXUP_NEEDED:
7813 fixup.fixup_needed = entry->d_un.d_val;
7814 break;
7815 case DT_IA_64_VMS_FIXUP_RELA_CNT:
7816 fixup.fixup_rela_cnt = entry->d_un.d_val;
7817 break;
7818 case DT_IA_64_VMS_FIXUP_RELA_OFF:
7819 fixup.fixup_rela_off = entry->d_un.d_val;
7820 if (! dump_ia64_vms_dynamic_fixups (filedata, &fixup, strtab, strtab_sz))
7821 res = false;
7822 break;
7823 case DT_IA_64_VMS_IMG_RELA_CNT:
7824 imgrela.img_rela_cnt = entry->d_un.d_val;
7825 break;
7826 case DT_IA_64_VMS_IMG_RELA_OFF:
7827 imgrela.img_rela_off = entry->d_un.d_val;
7828 if (! dump_ia64_vms_dynamic_relocs (filedata, &imgrela))
7829 res = false;
7830 break;
7831
7832 default:
7833 break;
7834 }
7835 }
7836
7837 free (strtab);
7838
7839 return res;
7840 }
7841
7842 static struct
7843 {
7844 const char * name;
7845 int reloc;
7846 int size;
7847 int rela;
7848 }
7849 dynamic_relocations [] =
7850 {
7851 { "REL", DT_REL, DT_RELSZ, false },
7852 { "RELA", DT_RELA, DT_RELASZ, true },
7853 { "PLT", DT_JMPREL, DT_PLTRELSZ, UNKNOWN }
7854 };
7855
7856 /* Process the reloc section. */
7857
7858 static bool
7859 process_relocs (Filedata * filedata)
7860 {
7861 unsigned long rel_size;
7862 unsigned long rel_offset;
7863
7864 if (!do_reloc)
7865 return true;
7866
7867 if (do_using_dynamic)
7868 {
7869 int is_rela;
7870 const char * name;
7871 bool has_dynamic_reloc;
7872 unsigned int i;
7873
7874 has_dynamic_reloc = false;
7875
7876 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
7877 {
7878 is_rela = dynamic_relocations [i].rela;
7879 name = dynamic_relocations [i].name;
7880 rel_size = filedata->dynamic_info[dynamic_relocations [i].size];
7881 rel_offset = filedata->dynamic_info[dynamic_relocations [i].reloc];
7882
7883 if (rel_size)
7884 has_dynamic_reloc = true;
7885
7886 if (is_rela == UNKNOWN)
7887 {
7888 if (dynamic_relocations [i].reloc == DT_JMPREL)
7889 switch (filedata->dynamic_info[DT_PLTREL])
7890 {
7891 case DT_REL:
7892 is_rela = false;
7893 break;
7894 case DT_RELA:
7895 is_rela = true;
7896 break;
7897 }
7898 }
7899
7900 if (rel_size)
7901 {
7902 if (filedata->is_separate)
7903 printf
7904 (_("\nIn linked file '%s' section '%s' at offset 0x%lx contains %ld bytes:\n"),
7905 filedata->file_name, name, rel_offset, rel_size);
7906 else
7907 printf
7908 (_("\n'%s' relocation section at offset 0x%lx contains %ld bytes:\n"),
7909 name, rel_offset, rel_size);
7910
7911 dump_relocations (filedata,
7912 offset_from_vma (filedata, rel_offset, rel_size),
7913 rel_size,
7914 filedata->dynamic_symbols,
7915 filedata->num_dynamic_syms,
7916 filedata->dynamic_strings,
7917 filedata->dynamic_strings_length,
7918 is_rela, true /* is_dynamic */);
7919 }
7920 }
7921
7922 if (is_ia64_vms (filedata))
7923 if (process_ia64_vms_dynamic_relocs (filedata))
7924 has_dynamic_reloc = true;
7925
7926 if (! has_dynamic_reloc)
7927 {
7928 if (filedata->is_separate)
7929 printf (_("\nThere are no dynamic relocations in linked file '%s'.\n"),
7930 filedata->file_name);
7931 else
7932 printf (_("\nThere are no dynamic relocations in this file.\n"));
7933 }
7934 }
7935 else
7936 {
7937 Elf_Internal_Shdr * section;
7938 unsigned long i;
7939 bool found = false;
7940
7941 for (i = 0, section = filedata->section_headers;
7942 i < filedata->file_header.e_shnum;
7943 i++, section++)
7944 {
7945 if ( section->sh_type != SHT_RELA
7946 && section->sh_type != SHT_REL)
7947 continue;
7948
7949 rel_offset = section->sh_offset;
7950 rel_size = section->sh_size;
7951
7952 if (rel_size)
7953 {
7954 int is_rela;
7955 unsigned long num_rela;
7956
7957 if (filedata->is_separate)
7958 printf (_("\nIn linked file '%s' relocation section "),
7959 filedata->file_name);
7960 else
7961 printf (_("\nRelocation section "));
7962
7963 if (filedata->string_table == NULL)
7964 printf ("%d", section->sh_name);
7965 else
7966 printf ("'%s'", printable_section_name (filedata, section));
7967
7968 num_rela = rel_size / section->sh_entsize;
7969 printf (ngettext (" at offset 0x%lx contains %lu entry:\n",
7970 " at offset 0x%lx contains %lu entries:\n",
7971 num_rela),
7972 rel_offset, num_rela);
7973
7974 is_rela = section->sh_type == SHT_RELA;
7975
7976 if (section->sh_link != 0
7977 && section->sh_link < filedata->file_header.e_shnum)
7978 {
7979 Elf_Internal_Shdr * symsec;
7980 Elf_Internal_Sym * symtab;
7981 unsigned long nsyms;
7982 unsigned long strtablen = 0;
7983 char * strtab = NULL;
7984
7985 symsec = filedata->section_headers + section->sh_link;
7986 if (symsec->sh_type != SHT_SYMTAB
7987 && symsec->sh_type != SHT_DYNSYM)
7988 continue;
7989
7990 if (!get_symtab (filedata, symsec,
7991 &symtab, &nsyms, &strtab, &strtablen))
7992 continue;
7993
7994 dump_relocations (filedata, rel_offset, rel_size,
7995 symtab, nsyms, strtab, strtablen,
7996 is_rela,
7997 symsec->sh_type == SHT_DYNSYM);
7998 free (strtab);
7999 free (symtab);
8000 }
8001 else
8002 dump_relocations (filedata, rel_offset, rel_size,
8003 NULL, 0, NULL, 0, is_rela,
8004 false /* is_dynamic */);
8005
8006 found = true;
8007 }
8008 }
8009
8010 if (! found)
8011 {
8012 /* Users sometimes forget the -D option, so try to be helpful. */
8013 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
8014 {
8015 if (filedata->dynamic_info[dynamic_relocations [i].size])
8016 {
8017 if (filedata->is_separate)
8018 printf (_("\nThere are no static relocations in linked file '%s'."),
8019 filedata->file_name);
8020 else
8021 printf (_("\nThere are no static relocations in this file."));
8022 printf (_("\nTo see the dynamic relocations add --use-dynamic to the command line.\n"));
8023
8024 break;
8025 }
8026 }
8027 if (i == ARRAY_SIZE (dynamic_relocations))
8028 {
8029 if (filedata->is_separate)
8030 printf (_("\nThere are no relocations in linked file '%s'.\n"),
8031 filedata->file_name);
8032 else
8033 printf (_("\nThere are no relocations in this file.\n"));
8034 }
8035 }
8036 }
8037
8038 return true;
8039 }
8040
8041 /* An absolute address consists of a section and an offset. If the
8042 section is NULL, the offset itself is the address, otherwise, the
8043 address equals to LOAD_ADDRESS(section) + offset. */
8044
8045 struct absaddr
8046 {
8047 unsigned short section;
8048 bfd_vma offset;
8049 };
8050
8051 /* Find the nearest symbol at or below ADDR. Returns the symbol
8052 name, if found, and the offset from the symbol to ADDR. */
8053
8054 static void
8055 find_symbol_for_address (Filedata * filedata,
8056 Elf_Internal_Sym * symtab,
8057 unsigned long nsyms,
8058 const char * strtab,
8059 unsigned long strtab_size,
8060 struct absaddr addr,
8061 const char ** symname,
8062 bfd_vma * offset)
8063 {
8064 bfd_vma dist = 0x100000;
8065 Elf_Internal_Sym * sym;
8066 Elf_Internal_Sym * beg;
8067 Elf_Internal_Sym * end;
8068 Elf_Internal_Sym * best = NULL;
8069
8070 REMOVE_ARCH_BITS (addr.offset);
8071 beg = symtab;
8072 end = symtab + nsyms;
8073
8074 while (beg < end)
8075 {
8076 bfd_vma value;
8077
8078 sym = beg + (end - beg) / 2;
8079
8080 value = sym->st_value;
8081 REMOVE_ARCH_BITS (value);
8082
8083 if (sym->st_name != 0
8084 && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
8085 && addr.offset >= value
8086 && addr.offset - value < dist)
8087 {
8088 best = sym;
8089 dist = addr.offset - value;
8090 if (!dist)
8091 break;
8092 }
8093
8094 if (addr.offset < value)
8095 end = sym;
8096 else
8097 beg = sym + 1;
8098 }
8099
8100 if (best)
8101 {
8102 *symname = (best->st_name >= strtab_size
8103 ? _("<corrupt>") : strtab + best->st_name);
8104 *offset = dist;
8105 return;
8106 }
8107
8108 *symname = NULL;
8109 *offset = addr.offset;
8110 }
8111
8112 static /* signed */ int
8113 symcmp (const void *p, const void *q)
8114 {
8115 Elf_Internal_Sym *sp = (Elf_Internal_Sym *) p;
8116 Elf_Internal_Sym *sq = (Elf_Internal_Sym *) q;
8117
8118 return sp->st_value > sq->st_value ? 1 : (sp->st_value < sq->st_value ? -1 : 0);
8119 }
8120
8121 /* Process the unwind section. */
8122
8123 #include "unwind-ia64.h"
8124
8125 struct ia64_unw_table_entry
8126 {
8127 struct absaddr start;
8128 struct absaddr end;
8129 struct absaddr info;
8130 };
8131
8132 struct ia64_unw_aux_info
8133 {
8134 struct ia64_unw_table_entry * table; /* Unwind table. */
8135 unsigned long table_len; /* Length of unwind table. */
8136 unsigned char * info; /* Unwind info. */
8137 unsigned long info_size; /* Size of unwind info. */
8138 bfd_vma info_addr; /* Starting address of unwind info. */
8139 bfd_vma seg_base; /* Starting address of segment. */
8140 Elf_Internal_Sym * symtab; /* The symbol table. */
8141 unsigned long nsyms; /* Number of symbols. */
8142 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
8143 unsigned long nfuns; /* Number of entries in funtab. */
8144 char * strtab; /* The string table. */
8145 unsigned long strtab_size; /* Size of string table. */
8146 };
8147
8148 static bool
8149 dump_ia64_unwind (Filedata * filedata, struct ia64_unw_aux_info * aux)
8150 {
8151 struct ia64_unw_table_entry * tp;
8152 unsigned long j, nfuns;
8153 int in_body;
8154 bool res = true;
8155
8156 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
8157 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
8158 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
8159 aux->funtab[nfuns++] = aux->symtab[j];
8160 aux->nfuns = nfuns;
8161 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
8162
8163 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
8164 {
8165 bfd_vma stamp;
8166 bfd_vma offset;
8167 const unsigned char * dp;
8168 const unsigned char * head;
8169 const unsigned char * end;
8170 const char * procname;
8171
8172 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
8173 aux->strtab_size, tp->start, &procname, &offset);
8174
8175 fputs ("\n<", stdout);
8176
8177 if (procname)
8178 {
8179 fputs (procname, stdout);
8180
8181 if (offset)
8182 printf ("+%lx", (unsigned long) offset);
8183 }
8184
8185 fputs (">: [", stdout);
8186 print_vma (tp->start.offset, PREFIX_HEX);
8187 fputc ('-', stdout);
8188 print_vma (tp->end.offset, PREFIX_HEX);
8189 printf ("], info at +0x%lx\n",
8190 (unsigned long) (tp->info.offset - aux->seg_base));
8191
8192 /* PR 17531: file: 86232b32. */
8193 if (aux->info == NULL)
8194 continue;
8195
8196 offset = tp->info.offset;
8197 if (tp->info.section)
8198 {
8199 if (tp->info.section >= filedata->file_header.e_shnum)
8200 {
8201 warn (_("Invalid section %u in table entry %ld\n"),
8202 tp->info.section, (long) (tp - aux->table));
8203 res = false;
8204 continue;
8205 }
8206 offset += filedata->section_headers[tp->info.section].sh_addr;
8207 }
8208 offset -= aux->info_addr;
8209 /* PR 17531: file: 0997b4d1. */
8210 if (offset >= aux->info_size
8211 || aux->info_size - offset < 8)
8212 {
8213 warn (_("Invalid offset %lx in table entry %ld\n"),
8214 (long) tp->info.offset, (long) (tp - aux->table));
8215 res = false;
8216 continue;
8217 }
8218
8219 head = aux->info + offset;
8220 stamp = byte_get ((unsigned char *) head, sizeof (stamp));
8221
8222 printf (" v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
8223 (unsigned) UNW_VER (stamp),
8224 (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
8225 UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
8226 UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
8227 (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
8228
8229 if (UNW_VER (stamp) != 1)
8230 {
8231 printf (_("\tUnknown version.\n"));
8232 continue;
8233 }
8234
8235 in_body = 0;
8236 end = head + 8 + eh_addr_size * UNW_LENGTH (stamp);
8237 /* PR 17531: file: 16ceda89. */
8238 if (end > aux->info + aux->info_size)
8239 end = aux->info + aux->info_size;
8240 for (dp = head + 8; dp < end;)
8241 dp = unw_decode (dp, in_body, & in_body, end);
8242 }
8243
8244 free (aux->funtab);
8245
8246 return res;
8247 }
8248
8249 static bool
8250 slurp_ia64_unwind_table (Filedata * filedata,
8251 struct ia64_unw_aux_info * aux,
8252 Elf_Internal_Shdr * sec)
8253 {
8254 unsigned long size, nrelas, i;
8255 Elf_Internal_Phdr * seg;
8256 struct ia64_unw_table_entry * tep;
8257 Elf_Internal_Shdr * relsec;
8258 Elf_Internal_Rela * rela;
8259 Elf_Internal_Rela * rp;
8260 unsigned char * table;
8261 unsigned char * tp;
8262 Elf_Internal_Sym * sym;
8263 const char * relname;
8264
8265 aux->table_len = 0;
8266
8267 /* First, find the starting address of the segment that includes
8268 this section: */
8269
8270 if (filedata->file_header.e_phnum)
8271 {
8272 if (! get_program_headers (filedata))
8273 return false;
8274
8275 for (seg = filedata->program_headers;
8276 seg < filedata->program_headers + filedata->file_header.e_phnum;
8277 ++seg)
8278 {
8279 if (seg->p_type != PT_LOAD)
8280 continue;
8281
8282 if (sec->sh_addr >= seg->p_vaddr
8283 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
8284 {
8285 aux->seg_base = seg->p_vaddr;
8286 break;
8287 }
8288 }
8289 }
8290
8291 /* Second, build the unwind table from the contents of the unwind section: */
8292 size = sec->sh_size;
8293 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
8294 _("unwind table"));
8295 if (!table)
8296 return false;
8297
8298 aux->table_len = size / (3 * eh_addr_size);
8299 aux->table = (struct ia64_unw_table_entry *)
8300 xcmalloc (aux->table_len, sizeof (aux->table[0]));
8301 tep = aux->table;
8302
8303 for (tp = table; tp <= table + size - (3 * eh_addr_size); ++tep)
8304 {
8305 tep->start.section = SHN_UNDEF;
8306 tep->end.section = SHN_UNDEF;
8307 tep->info.section = SHN_UNDEF;
8308 tep->start.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8309 tep->end.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8310 tep->info.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8311 tep->start.offset += aux->seg_base;
8312 tep->end.offset += aux->seg_base;
8313 tep->info.offset += aux->seg_base;
8314 }
8315 free (table);
8316
8317 /* Third, apply any relocations to the unwind table: */
8318 for (relsec = filedata->section_headers;
8319 relsec < filedata->section_headers + filedata->file_header.e_shnum;
8320 ++relsec)
8321 {
8322 if (relsec->sh_type != SHT_RELA
8323 || relsec->sh_info >= filedata->file_header.e_shnum
8324 || filedata->section_headers + relsec->sh_info != sec)
8325 continue;
8326
8327 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
8328 & rela, & nrelas))
8329 {
8330 free (aux->table);
8331 aux->table = NULL;
8332 aux->table_len = 0;
8333 return false;
8334 }
8335
8336 for (rp = rela; rp < rela + nrelas; ++rp)
8337 {
8338 unsigned int sym_ndx;
8339 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
8340 relname = elf_ia64_reloc_type (r_type);
8341
8342 /* PR 17531: file: 9fa67536. */
8343 if (relname == NULL)
8344 {
8345 warn (_("Skipping unknown relocation type: %u\n"), r_type);
8346 continue;
8347 }
8348
8349 if (! startswith (relname, "R_IA64_SEGREL"))
8350 {
8351 warn (_("Skipping unexpected relocation type: %s\n"), relname);
8352 continue;
8353 }
8354
8355 i = rp->r_offset / (3 * eh_addr_size);
8356
8357 /* PR 17531: file: 5bc8d9bf. */
8358 if (i >= aux->table_len)
8359 {
8360 warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
8361 continue;
8362 }
8363
8364 sym_ndx = get_reloc_symindex (rp->r_info);
8365 if (sym_ndx >= aux->nsyms)
8366 {
8367 warn (_("Skipping reloc with invalid symbol index: %u\n"),
8368 sym_ndx);
8369 continue;
8370 }
8371 sym = aux->symtab + sym_ndx;
8372
8373 switch (rp->r_offset / eh_addr_size % 3)
8374 {
8375 case 0:
8376 aux->table[i].start.section = sym->st_shndx;
8377 aux->table[i].start.offset = rp->r_addend + sym->st_value;
8378 break;
8379 case 1:
8380 aux->table[i].end.section = sym->st_shndx;
8381 aux->table[i].end.offset = rp->r_addend + sym->st_value;
8382 break;
8383 case 2:
8384 aux->table[i].info.section = sym->st_shndx;
8385 aux->table[i].info.offset = rp->r_addend + sym->st_value;
8386 break;
8387 default:
8388 break;
8389 }
8390 }
8391
8392 free (rela);
8393 }
8394
8395 return true;
8396 }
8397
8398 static bool
8399 ia64_process_unwind (Filedata * filedata)
8400 {
8401 Elf_Internal_Shdr * sec;
8402 Elf_Internal_Shdr * unwsec = NULL;
8403 unsigned long i, unwcount = 0, unwstart = 0;
8404 struct ia64_unw_aux_info aux;
8405 bool res = true;
8406
8407 memset (& aux, 0, sizeof (aux));
8408
8409 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
8410 {
8411 if (sec->sh_type == SHT_SYMTAB)
8412 {
8413 if (aux.symtab)
8414 {
8415 error (_("Multiple symbol tables encountered\n"));
8416 free (aux.symtab);
8417 aux.symtab = NULL;
8418 free (aux.strtab);
8419 aux.strtab = NULL;
8420 }
8421 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
8422 &aux.strtab, &aux.strtab_size))
8423 return false;
8424 }
8425 else if (sec->sh_type == SHT_IA_64_UNWIND)
8426 unwcount++;
8427 }
8428
8429 if (!unwcount)
8430 printf (_("\nThere are no unwind sections in this file.\n"));
8431
8432 while (unwcount-- > 0)
8433 {
8434 const char *suffix;
8435 size_t len, len2;
8436
8437 for (i = unwstart, sec = filedata->section_headers + unwstart, unwsec = NULL;
8438 i < filedata->file_header.e_shnum; ++i, ++sec)
8439 if (sec->sh_type == SHT_IA_64_UNWIND)
8440 {
8441 unwsec = sec;
8442 break;
8443 }
8444 /* We have already counted the number of SHT_IA64_UNWIND
8445 sections so the loop above should never fail. */
8446 assert (unwsec != NULL);
8447
8448 unwstart = i + 1;
8449 len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
8450
8451 if ((unwsec->sh_flags & SHF_GROUP) != 0)
8452 {
8453 /* We need to find which section group it is in. */
8454 struct group_list * g;
8455
8456 if (filedata->section_headers_groups == NULL
8457 || filedata->section_headers_groups[i] == NULL)
8458 i = filedata->file_header.e_shnum;
8459 else
8460 {
8461 g = filedata->section_headers_groups[i]->root;
8462
8463 for (; g != NULL; g = g->next)
8464 {
8465 sec = filedata->section_headers + g->section_index;
8466
8467 if (section_name_valid (filedata, sec)
8468 && streq (section_name (filedata, sec),
8469 ELF_STRING_ia64_unwind_info))
8470 break;
8471 }
8472
8473 if (g == NULL)
8474 i = filedata->file_header.e_shnum;
8475 }
8476 }
8477 else if (section_name_valid (filedata, unwsec)
8478 && startswith (section_name (filedata, unwsec),
8479 ELF_STRING_ia64_unwind_once))
8480 {
8481 /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO. */
8482 len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
8483 suffix = section_name (filedata, unwsec) + len;
8484 for (i = 0, sec = filedata->section_headers;
8485 i < filedata->file_header.e_shnum;
8486 ++i, ++sec)
8487 if (section_name_valid (filedata, sec)
8488 && startswith (section_name (filedata, sec),
8489 ELF_STRING_ia64_unwind_info_once)
8490 && streq (section_name (filedata, sec) + len2, suffix))
8491 break;
8492 }
8493 else
8494 {
8495 /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
8496 .IA_64.unwind or BAR -> .IA_64.unwind_info. */
8497 len = sizeof (ELF_STRING_ia64_unwind) - 1;
8498 len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
8499 suffix = "";
8500 if (section_name_valid (filedata, unwsec)
8501 && startswith (section_name (filedata, unwsec),
8502 ELF_STRING_ia64_unwind))
8503 suffix = section_name (filedata, unwsec) + len;
8504 for (i = 0, sec = filedata->section_headers;
8505 i < filedata->file_header.e_shnum;
8506 ++i, ++sec)
8507 if (section_name_valid (filedata, sec)
8508 && startswith (section_name (filedata, sec),
8509 ELF_STRING_ia64_unwind_info)
8510 && streq (section_name (filedata, sec) + len2, suffix))
8511 break;
8512 }
8513
8514 if (i == filedata->file_header.e_shnum)
8515 {
8516 printf (_("\nCould not find unwind info section for "));
8517
8518 if (filedata->string_table == NULL)
8519 printf ("%d", unwsec->sh_name);
8520 else
8521 printf ("'%s'", printable_section_name (filedata, unwsec));
8522 }
8523 else
8524 {
8525 aux.info_addr = sec->sh_addr;
8526 aux.info = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1,
8527 sec->sh_size,
8528 _("unwind info"));
8529 aux.info_size = aux.info == NULL ? 0 : sec->sh_size;
8530
8531 printf (_("\nUnwind section "));
8532
8533 if (filedata->string_table == NULL)
8534 printf ("%d", unwsec->sh_name);
8535 else
8536 printf ("'%s'", printable_section_name (filedata, unwsec));
8537
8538 printf (_(" at offset 0x%lx contains %lu entries:\n"),
8539 (unsigned long) unwsec->sh_offset,
8540 (unsigned long) (unwsec->sh_size / (3 * eh_addr_size)));
8541
8542 if (slurp_ia64_unwind_table (filedata, & aux, unwsec)
8543 && aux.table_len > 0)
8544 dump_ia64_unwind (filedata, & aux);
8545
8546 free ((char *) aux.table);
8547 free ((char *) aux.info);
8548 aux.table = NULL;
8549 aux.info = NULL;
8550 }
8551 }
8552
8553 free (aux.symtab);
8554 free ((char *) aux.strtab);
8555
8556 return res;
8557 }
8558
8559 struct hppa_unw_table_entry
8560 {
8561 struct absaddr start;
8562 struct absaddr end;
8563 unsigned int Cannot_unwind:1; /* 0 */
8564 unsigned int Millicode:1; /* 1 */
8565 unsigned int Millicode_save_sr0:1; /* 2 */
8566 unsigned int Region_description:2; /* 3..4 */
8567 unsigned int reserved1:1; /* 5 */
8568 unsigned int Entry_SR:1; /* 6 */
8569 unsigned int Entry_FR:4; /* Number saved 7..10 */
8570 unsigned int Entry_GR:5; /* Number saved 11..15 */
8571 unsigned int Args_stored:1; /* 16 */
8572 unsigned int Variable_Frame:1; /* 17 */
8573 unsigned int Separate_Package_Body:1; /* 18 */
8574 unsigned int Frame_Extension_Millicode:1; /* 19 */
8575 unsigned int Stack_Overflow_Check:1; /* 20 */
8576 unsigned int Two_Instruction_SP_Increment:1; /* 21 */
8577 unsigned int Ada_Region:1; /* 22 */
8578 unsigned int cxx_info:1; /* 23 */
8579 unsigned int cxx_try_catch:1; /* 24 */
8580 unsigned int sched_entry_seq:1; /* 25 */
8581 unsigned int reserved2:1; /* 26 */
8582 unsigned int Save_SP:1; /* 27 */
8583 unsigned int Save_RP:1; /* 28 */
8584 unsigned int Save_MRP_in_frame:1; /* 29 */
8585 unsigned int extn_ptr_defined:1; /* 30 */
8586 unsigned int Cleanup_defined:1; /* 31 */
8587
8588 unsigned int MPE_XL_interrupt_marker:1; /* 0 */
8589 unsigned int HP_UX_interrupt_marker:1; /* 1 */
8590 unsigned int Large_frame:1; /* 2 */
8591 unsigned int Pseudo_SP_Set:1; /* 3 */
8592 unsigned int reserved4:1; /* 4 */
8593 unsigned int Total_frame_size:27; /* 5..31 */
8594 };
8595
8596 struct hppa_unw_aux_info
8597 {
8598 struct hppa_unw_table_entry * table; /* Unwind table. */
8599 unsigned long table_len; /* Length of unwind table. */
8600 bfd_vma seg_base; /* Starting address of segment. */
8601 Elf_Internal_Sym * symtab; /* The symbol table. */
8602 unsigned long nsyms; /* Number of symbols. */
8603 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
8604 unsigned long nfuns; /* Number of entries in funtab. */
8605 char * strtab; /* The string table. */
8606 unsigned long strtab_size; /* Size of string table. */
8607 };
8608
8609 static bool
8610 dump_hppa_unwind (Filedata * filedata, struct hppa_unw_aux_info * aux)
8611 {
8612 struct hppa_unw_table_entry * tp;
8613 unsigned long j, nfuns;
8614 bool res = true;
8615
8616 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
8617 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
8618 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
8619 aux->funtab[nfuns++] = aux->symtab[j];
8620 aux->nfuns = nfuns;
8621 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
8622
8623 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
8624 {
8625 bfd_vma offset;
8626 const char * procname;
8627
8628 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
8629 aux->strtab_size, tp->start, &procname,
8630 &offset);
8631
8632 fputs ("\n<", stdout);
8633
8634 if (procname)
8635 {
8636 fputs (procname, stdout);
8637
8638 if (offset)
8639 printf ("+%lx", (unsigned long) offset);
8640 }
8641
8642 fputs (">: [", stdout);
8643 print_vma (tp->start.offset, PREFIX_HEX);
8644 fputc ('-', stdout);
8645 print_vma (tp->end.offset, PREFIX_HEX);
8646 printf ("]\n\t");
8647
8648 #define PF(_m) if (tp->_m) printf (#_m " ");
8649 #define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
8650 PF(Cannot_unwind);
8651 PF(Millicode);
8652 PF(Millicode_save_sr0);
8653 /* PV(Region_description); */
8654 PF(Entry_SR);
8655 PV(Entry_FR);
8656 PV(Entry_GR);
8657 PF(Args_stored);
8658 PF(Variable_Frame);
8659 PF(Separate_Package_Body);
8660 PF(Frame_Extension_Millicode);
8661 PF(Stack_Overflow_Check);
8662 PF(Two_Instruction_SP_Increment);
8663 PF(Ada_Region);
8664 PF(cxx_info);
8665 PF(cxx_try_catch);
8666 PF(sched_entry_seq);
8667 PF(Save_SP);
8668 PF(Save_RP);
8669 PF(Save_MRP_in_frame);
8670 PF(extn_ptr_defined);
8671 PF(Cleanup_defined);
8672 PF(MPE_XL_interrupt_marker);
8673 PF(HP_UX_interrupt_marker);
8674 PF(Large_frame);
8675 PF(Pseudo_SP_Set);
8676 PV(Total_frame_size);
8677 #undef PF
8678 #undef PV
8679 }
8680
8681 printf ("\n");
8682
8683 free (aux->funtab);
8684
8685 return res;
8686 }
8687
8688 static bool
8689 slurp_hppa_unwind_table (Filedata * filedata,
8690 struct hppa_unw_aux_info * aux,
8691 Elf_Internal_Shdr * sec)
8692 {
8693 unsigned long size, unw_ent_size, nentries, nrelas, i;
8694 Elf_Internal_Phdr * seg;
8695 struct hppa_unw_table_entry * tep;
8696 Elf_Internal_Shdr * relsec;
8697 Elf_Internal_Rela * rela;
8698 Elf_Internal_Rela * rp;
8699 unsigned char * table;
8700 unsigned char * tp;
8701 Elf_Internal_Sym * sym;
8702 const char * relname;
8703
8704 /* First, find the starting address of the segment that includes
8705 this section. */
8706 if (filedata->file_header.e_phnum)
8707 {
8708 if (! get_program_headers (filedata))
8709 return false;
8710
8711 for (seg = filedata->program_headers;
8712 seg < filedata->program_headers + filedata->file_header.e_phnum;
8713 ++seg)
8714 {
8715 if (seg->p_type != PT_LOAD)
8716 continue;
8717
8718 if (sec->sh_addr >= seg->p_vaddr
8719 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
8720 {
8721 aux->seg_base = seg->p_vaddr;
8722 break;
8723 }
8724 }
8725 }
8726
8727 /* Second, build the unwind table from the contents of the unwind
8728 section. */
8729 size = sec->sh_size;
8730 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
8731 _("unwind table"));
8732 if (!table)
8733 return false;
8734
8735 unw_ent_size = 16;
8736 nentries = size / unw_ent_size;
8737 size = unw_ent_size * nentries;
8738
8739 aux->table_len = nentries;
8740 tep = aux->table = (struct hppa_unw_table_entry *)
8741 xcmalloc (nentries, sizeof (aux->table[0]));
8742
8743 for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
8744 {
8745 unsigned int tmp1, tmp2;
8746
8747 tep->start.section = SHN_UNDEF;
8748 tep->end.section = SHN_UNDEF;
8749
8750 tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
8751 tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
8752 tmp1 = byte_get ((unsigned char *) tp + 8, 4);
8753 tmp2 = byte_get ((unsigned char *) tp + 12, 4);
8754
8755 tep->start.offset += aux->seg_base;
8756 tep->end.offset += aux->seg_base;
8757
8758 tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
8759 tep->Millicode = (tmp1 >> 30) & 0x1;
8760 tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
8761 tep->Region_description = (tmp1 >> 27) & 0x3;
8762 tep->reserved1 = (tmp1 >> 26) & 0x1;
8763 tep->Entry_SR = (tmp1 >> 25) & 0x1;
8764 tep->Entry_FR = (tmp1 >> 21) & 0xf;
8765 tep->Entry_GR = (tmp1 >> 16) & 0x1f;
8766 tep->Args_stored = (tmp1 >> 15) & 0x1;
8767 tep->Variable_Frame = (tmp1 >> 14) & 0x1;
8768 tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
8769 tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
8770 tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
8771 tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
8772 tep->Ada_Region = (tmp1 >> 9) & 0x1;
8773 tep->cxx_info = (tmp1 >> 8) & 0x1;
8774 tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
8775 tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
8776 tep->reserved2 = (tmp1 >> 5) & 0x1;
8777 tep->Save_SP = (tmp1 >> 4) & 0x1;
8778 tep->Save_RP = (tmp1 >> 3) & 0x1;
8779 tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
8780 tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
8781 tep->Cleanup_defined = tmp1 & 0x1;
8782
8783 tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
8784 tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
8785 tep->Large_frame = (tmp2 >> 29) & 0x1;
8786 tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
8787 tep->reserved4 = (tmp2 >> 27) & 0x1;
8788 tep->Total_frame_size = tmp2 & 0x7ffffff;
8789 }
8790 free (table);
8791
8792 /* Third, apply any relocations to the unwind table. */
8793 for (relsec = filedata->section_headers;
8794 relsec < filedata->section_headers + filedata->file_header.e_shnum;
8795 ++relsec)
8796 {
8797 if (relsec->sh_type != SHT_RELA
8798 || relsec->sh_info >= filedata->file_header.e_shnum
8799 || filedata->section_headers + relsec->sh_info != sec)
8800 continue;
8801
8802 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
8803 & rela, & nrelas))
8804 return false;
8805
8806 for (rp = rela; rp < rela + nrelas; ++rp)
8807 {
8808 unsigned int sym_ndx;
8809 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
8810 relname = elf_hppa_reloc_type (r_type);
8811
8812 if (relname == NULL)
8813 {
8814 warn (_("Skipping unknown relocation type: %u\n"), r_type);
8815 continue;
8816 }
8817
8818 /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64. */
8819 if (! startswith (relname, "R_PARISC_SEGREL"))
8820 {
8821 warn (_("Skipping unexpected relocation type: %s\n"), relname);
8822 continue;
8823 }
8824
8825 i = rp->r_offset / unw_ent_size;
8826 if (i >= aux->table_len)
8827 {
8828 warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
8829 continue;
8830 }
8831
8832 sym_ndx = get_reloc_symindex (rp->r_info);
8833 if (sym_ndx >= aux->nsyms)
8834 {
8835 warn (_("Skipping reloc with invalid symbol index: %u\n"),
8836 sym_ndx);
8837 continue;
8838 }
8839 sym = aux->symtab + sym_ndx;
8840
8841 switch ((rp->r_offset % unw_ent_size) / 4)
8842 {
8843 case 0:
8844 aux->table[i].start.section = sym->st_shndx;
8845 aux->table[i].start.offset = sym->st_value + rp->r_addend;
8846 break;
8847 case 1:
8848 aux->table[i].end.section = sym->st_shndx;
8849 aux->table[i].end.offset = sym->st_value + rp->r_addend;
8850 break;
8851 default:
8852 break;
8853 }
8854 }
8855
8856 free (rela);
8857 }
8858
8859 return true;
8860 }
8861
8862 static bool
8863 hppa_process_unwind (Filedata * filedata)
8864 {
8865 struct hppa_unw_aux_info aux;
8866 Elf_Internal_Shdr * unwsec = NULL;
8867 Elf_Internal_Shdr * sec;
8868 unsigned long i;
8869 bool res = true;
8870
8871 if (filedata->string_table == NULL)
8872 return false;
8873
8874 memset (& aux, 0, sizeof (aux));
8875
8876 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
8877 {
8878 if (sec->sh_type == SHT_SYMTAB)
8879 {
8880 if (aux.symtab)
8881 {
8882 error (_("Multiple symbol tables encountered\n"));
8883 free (aux.symtab);
8884 aux.symtab = NULL;
8885 free (aux.strtab);
8886 aux.strtab = NULL;
8887 }
8888 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
8889 &aux.strtab, &aux.strtab_size))
8890 return false;
8891 }
8892 else if (section_name_valid (filedata, sec)
8893 && streq (section_name (filedata, sec), ".PARISC.unwind"))
8894 unwsec = sec;
8895 }
8896
8897 if (!unwsec)
8898 printf (_("\nThere are no unwind sections in this file.\n"));
8899
8900 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
8901 {
8902 if (section_name_valid (filedata, sec)
8903 && streq (section_name (filedata, sec), ".PARISC.unwind"))
8904 {
8905 unsigned long num_unwind = sec->sh_size / 16;
8906
8907 printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
8908 "contains %lu entry:\n",
8909 "\nUnwind section '%s' at offset 0x%lx "
8910 "contains %lu entries:\n",
8911 num_unwind),
8912 printable_section_name (filedata, sec),
8913 (unsigned long) sec->sh_offset,
8914 num_unwind);
8915
8916 if (! slurp_hppa_unwind_table (filedata, &aux, sec))
8917 res = false;
8918
8919 if (res && aux.table_len > 0)
8920 {
8921 if (! dump_hppa_unwind (filedata, &aux))
8922 res = false;
8923 }
8924
8925 free ((char *) aux.table);
8926 aux.table = NULL;
8927 }
8928 }
8929
8930 free (aux.symtab);
8931 free ((char *) aux.strtab);
8932
8933 return res;
8934 }
8935
8936 struct arm_section
8937 {
8938 unsigned char * data; /* The unwind data. */
8939 Elf_Internal_Shdr * sec; /* The cached unwind section header. */
8940 Elf_Internal_Rela * rela; /* The cached relocations for this section. */
8941 unsigned long nrelas; /* The number of relocations. */
8942 unsigned int rel_type; /* REL or RELA ? */
8943 Elf_Internal_Rela * next_rela; /* Cyclic pointer to the next reloc to process. */
8944 };
8945
8946 struct arm_unw_aux_info
8947 {
8948 Filedata * filedata; /* The file containing the unwind sections. */
8949 Elf_Internal_Sym * symtab; /* The file's symbol table. */
8950 unsigned long nsyms; /* Number of symbols. */
8951 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
8952 unsigned long nfuns; /* Number of these symbols. */
8953 char * strtab; /* The file's string table. */
8954 unsigned long strtab_size; /* Size of string table. */
8955 };
8956
8957 static const char *
8958 arm_print_vma_and_name (Filedata * filedata,
8959 struct arm_unw_aux_info * aux,
8960 bfd_vma fn,
8961 struct absaddr addr)
8962 {
8963 const char *procname;
8964 bfd_vma sym_offset;
8965
8966 if (addr.section == SHN_UNDEF)
8967 addr.offset = fn;
8968
8969 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
8970 aux->strtab_size, addr, &procname,
8971 &sym_offset);
8972
8973 print_vma (fn, PREFIX_HEX);
8974
8975 if (procname)
8976 {
8977 fputs (" <", stdout);
8978 fputs (procname, stdout);
8979
8980 if (sym_offset)
8981 printf ("+0x%lx", (unsigned long) sym_offset);
8982 fputc ('>', stdout);
8983 }
8984
8985 return procname;
8986 }
8987
8988 static void
8989 arm_free_section (struct arm_section *arm_sec)
8990 {
8991 free (arm_sec->data);
8992 free (arm_sec->rela);
8993 }
8994
8995 /* 1) If SEC does not match the one cached in ARM_SEC, then free the current
8996 cached section and install SEC instead.
8997 2) Locate the 32-bit word at WORD_OFFSET in unwind section SEC
8998 and return its valued in * WORDP, relocating if necessary.
8999 3) Update the NEXT_RELA field in ARM_SEC and store the section index and
9000 relocation's offset in ADDR.
9001 4) If SYM_NAME is non-NULL and a relocation was applied, record the offset
9002 into the string table of the symbol associated with the reloc. If no
9003 reloc was applied store -1 there.
9004 5) Return TRUE upon success, FALSE otherwise. */
9005
9006 static bool
9007 get_unwind_section_word (Filedata * filedata,
9008 struct arm_unw_aux_info * aux,
9009 struct arm_section * arm_sec,
9010 Elf_Internal_Shdr * sec,
9011 bfd_vma word_offset,
9012 unsigned int * wordp,
9013 struct absaddr * addr,
9014 bfd_vma * sym_name)
9015 {
9016 Elf_Internal_Rela *rp;
9017 Elf_Internal_Sym *sym;
9018 const char * relname;
9019 unsigned int word;
9020 bool wrapped;
9021
9022 if (sec == NULL || arm_sec == NULL)
9023 return false;
9024
9025 addr->section = SHN_UNDEF;
9026 addr->offset = 0;
9027
9028 if (sym_name != NULL)
9029 *sym_name = (bfd_vma) -1;
9030
9031 /* If necessary, update the section cache. */
9032 if (sec != arm_sec->sec)
9033 {
9034 Elf_Internal_Shdr *relsec;
9035
9036 arm_free_section (arm_sec);
9037
9038 arm_sec->sec = sec;
9039 arm_sec->data = get_data (NULL, aux->filedata, sec->sh_offset, 1,
9040 sec->sh_size, _("unwind data"));
9041 arm_sec->rela = NULL;
9042 arm_sec->nrelas = 0;
9043
9044 for (relsec = filedata->section_headers;
9045 relsec < filedata->section_headers + filedata->file_header.e_shnum;
9046 ++relsec)
9047 {
9048 if (relsec->sh_info >= filedata->file_header.e_shnum
9049 || filedata->section_headers + relsec->sh_info != sec
9050 /* PR 15745: Check the section type as well. */
9051 || (relsec->sh_type != SHT_REL
9052 && relsec->sh_type != SHT_RELA))
9053 continue;
9054
9055 arm_sec->rel_type = relsec->sh_type;
9056 if (relsec->sh_type == SHT_REL)
9057 {
9058 if (!slurp_rel_relocs (aux->filedata, relsec->sh_offset,
9059 relsec->sh_size,
9060 & arm_sec->rela, & arm_sec->nrelas))
9061 return false;
9062 }
9063 else /* relsec->sh_type == SHT_RELA */
9064 {
9065 if (!slurp_rela_relocs (aux->filedata, relsec->sh_offset,
9066 relsec->sh_size,
9067 & arm_sec->rela, & arm_sec->nrelas))
9068 return false;
9069 }
9070 break;
9071 }
9072
9073 arm_sec->next_rela = arm_sec->rela;
9074 }
9075
9076 /* If there is no unwind data we can do nothing. */
9077 if (arm_sec->data == NULL)
9078 return false;
9079
9080 /* If the offset is invalid then fail. */
9081 if (/* PR 21343 *//* PR 18879 */
9082 sec->sh_size < 4
9083 || word_offset > (sec->sh_size - 4)
9084 || ((bfd_signed_vma) word_offset) < 0)
9085 return false;
9086
9087 /* Get the word at the required offset. */
9088 word = byte_get (arm_sec->data + word_offset, 4);
9089
9090 /* PR 17531: file: id:000001,src:001266+003044,op:splice,rep:128. */
9091 if (arm_sec->rela == NULL)
9092 {
9093 * wordp = word;
9094 return true;
9095 }
9096
9097 /* Look through the relocs to find the one that applies to the provided offset. */
9098 wrapped = false;
9099 for (rp = arm_sec->next_rela; rp != arm_sec->rela + arm_sec->nrelas; rp++)
9100 {
9101 bfd_vma prelval, offset;
9102
9103 if (rp->r_offset > word_offset && !wrapped)
9104 {
9105 rp = arm_sec->rela;
9106 wrapped = true;
9107 }
9108 if (rp->r_offset > word_offset)
9109 break;
9110
9111 if (rp->r_offset & 3)
9112 {
9113 warn (_("Skipping unexpected relocation at offset 0x%lx\n"),
9114 (unsigned long) rp->r_offset);
9115 continue;
9116 }
9117
9118 if (rp->r_offset < word_offset)
9119 continue;
9120
9121 /* PR 17531: file: 027-161405-0.004 */
9122 if (aux->symtab == NULL)
9123 continue;
9124
9125 if (arm_sec->rel_type == SHT_REL)
9126 {
9127 offset = word & 0x7fffffff;
9128 if (offset & 0x40000000)
9129 offset |= ~ (bfd_vma) 0x7fffffff;
9130 }
9131 else if (arm_sec->rel_type == SHT_RELA)
9132 offset = rp->r_addend;
9133 else
9134 {
9135 error (_("Unknown section relocation type %d encountered\n"),
9136 arm_sec->rel_type);
9137 break;
9138 }
9139
9140 /* PR 17531 file: 027-1241568-0.004. */
9141 if (ELF32_R_SYM (rp->r_info) >= aux->nsyms)
9142 {
9143 error (_("Bad symbol index in unwind relocation (%lu > %lu)\n"),
9144 (unsigned long) ELF32_R_SYM (rp->r_info), aux->nsyms);
9145 break;
9146 }
9147
9148 sym = aux->symtab + ELF32_R_SYM (rp->r_info);
9149 offset += sym->st_value;
9150 prelval = offset - (arm_sec->sec->sh_addr + rp->r_offset);
9151
9152 /* Check that we are processing the expected reloc type. */
9153 if (filedata->file_header.e_machine == EM_ARM)
9154 {
9155 relname = elf_arm_reloc_type (ELF32_R_TYPE (rp->r_info));
9156 if (relname == NULL)
9157 {
9158 warn (_("Skipping unknown ARM relocation type: %d\n"),
9159 (int) ELF32_R_TYPE (rp->r_info));
9160 continue;
9161 }
9162
9163 if (streq (relname, "R_ARM_NONE"))
9164 continue;
9165
9166 if (! streq (relname, "R_ARM_PREL31"))
9167 {
9168 warn (_("Skipping unexpected ARM relocation type %s\n"), relname);
9169 continue;
9170 }
9171 }
9172 else if (filedata->file_header.e_machine == EM_TI_C6000)
9173 {
9174 relname = elf_tic6x_reloc_type (ELF32_R_TYPE (rp->r_info));
9175 if (relname == NULL)
9176 {
9177 warn (_("Skipping unknown C6000 relocation type: %d\n"),
9178 (int) ELF32_R_TYPE (rp->r_info));
9179 continue;
9180 }
9181
9182 if (streq (relname, "R_C6000_NONE"))
9183 continue;
9184
9185 if (! streq (relname, "R_C6000_PREL31"))
9186 {
9187 warn (_("Skipping unexpected C6000 relocation type %s\n"), relname);
9188 continue;
9189 }
9190
9191 prelval >>= 1;
9192 }
9193 else
9194 {
9195 /* This function currently only supports ARM and TI unwinders. */
9196 warn (_("Only TI and ARM unwinders are currently supported\n"));
9197 break;
9198 }
9199
9200 word = (word & ~ (bfd_vma) 0x7fffffff) | (prelval & 0x7fffffff);
9201 addr->section = sym->st_shndx;
9202 addr->offset = offset;
9203
9204 if (sym_name)
9205 * sym_name = sym->st_name;
9206 break;
9207 }
9208
9209 *wordp = word;
9210 arm_sec->next_rela = rp;
9211
9212 return true;
9213 }
9214
9215 static const char *tic6x_unwind_regnames[16] =
9216 {
9217 "A15", "B15", "B14", "B13", "B12", "B11", "B10", "B3",
9218 "A14", "A13", "A12", "A11", "A10",
9219 "[invalid reg 13]", "[invalid reg 14]", "[invalid reg 15]"
9220 };
9221
9222 static void
9223 decode_tic6x_unwind_regmask (unsigned int mask)
9224 {
9225 int i;
9226
9227 for (i = 12; mask; mask >>= 1, i--)
9228 {
9229 if (mask & 1)
9230 {
9231 fputs (tic6x_unwind_regnames[i], stdout);
9232 if (mask > 1)
9233 fputs (", ", stdout);
9234 }
9235 }
9236 }
9237
9238 #define ADVANCE \
9239 if (remaining == 0 && more_words) \
9240 { \
9241 data_offset += 4; \
9242 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, \
9243 data_offset, & word, & addr, NULL)) \
9244 return false; \
9245 remaining = 4; \
9246 more_words--; \
9247 } \
9248
9249 #define GET_OP(OP) \
9250 ADVANCE; \
9251 if (remaining) \
9252 { \
9253 remaining--; \
9254 (OP) = word >> 24; \
9255 word <<= 8; \
9256 } \
9257 else \
9258 { \
9259 printf (_("[Truncated opcode]\n")); \
9260 return false; \
9261 } \
9262 printf ("0x%02x ", OP)
9263
9264 static bool
9265 decode_arm_unwind_bytecode (Filedata * filedata,
9266 struct arm_unw_aux_info * aux,
9267 unsigned int word,
9268 unsigned int remaining,
9269 unsigned int more_words,
9270 bfd_vma data_offset,
9271 Elf_Internal_Shdr * data_sec,
9272 struct arm_section * data_arm_sec)
9273 {
9274 struct absaddr addr;
9275 bool res = true;
9276
9277 /* Decode the unwinding instructions. */
9278 while (1)
9279 {
9280 unsigned int op, op2;
9281
9282 ADVANCE;
9283 if (remaining == 0)
9284 break;
9285 remaining--;
9286 op = word >> 24;
9287 word <<= 8;
9288
9289 printf (" 0x%02x ", op);
9290
9291 if ((op & 0xc0) == 0x00)
9292 {
9293 int offset = ((op & 0x3f) << 2) + 4;
9294
9295 printf (" vsp = vsp + %d", offset);
9296 }
9297 else if ((op & 0xc0) == 0x40)
9298 {
9299 int offset = ((op & 0x3f) << 2) + 4;
9300
9301 printf (" vsp = vsp - %d", offset);
9302 }
9303 else if ((op & 0xf0) == 0x80)
9304 {
9305 GET_OP (op2);
9306 if (op == 0x80 && op2 == 0)
9307 printf (_("Refuse to unwind"));
9308 else
9309 {
9310 unsigned int mask = ((op & 0x0f) << 8) | op2;
9311 bool first = true;
9312 int i;
9313
9314 printf ("pop {");
9315 for (i = 0; i < 12; i++)
9316 if (mask & (1 << i))
9317 {
9318 if (first)
9319 first = false;
9320 else
9321 printf (", ");
9322 printf ("r%d", 4 + i);
9323 }
9324 printf ("}");
9325 }
9326 }
9327 else if ((op & 0xf0) == 0x90)
9328 {
9329 if (op == 0x9d || op == 0x9f)
9330 printf (_(" [Reserved]"));
9331 else
9332 printf (" vsp = r%d", op & 0x0f);
9333 }
9334 else if ((op & 0xf0) == 0xa0)
9335 {
9336 int end = 4 + (op & 0x07);
9337 bool first = true;
9338 int i;
9339
9340 printf (" pop {");
9341 for (i = 4; i <= end; i++)
9342 {
9343 if (first)
9344 first = false;
9345 else
9346 printf (", ");
9347 printf ("r%d", i);
9348 }
9349 if (op & 0x08)
9350 {
9351 if (!first)
9352 printf (", ");
9353 printf ("r14");
9354 }
9355 printf ("}");
9356 }
9357 else if (op == 0xb0)
9358 printf (_(" finish"));
9359 else if (op == 0xb1)
9360 {
9361 GET_OP (op2);
9362 if (op2 == 0 || (op2 & 0xf0) != 0)
9363 printf (_("[Spare]"));
9364 else
9365 {
9366 unsigned int mask = op2 & 0x0f;
9367 bool first = true;
9368 int i;
9369
9370 printf ("pop {");
9371 for (i = 0; i < 12; i++)
9372 if (mask & (1 << i))
9373 {
9374 if (first)
9375 first = false;
9376 else
9377 printf (", ");
9378 printf ("r%d", i);
9379 }
9380 printf ("}");
9381 }
9382 }
9383 else if (op == 0xb2)
9384 {
9385 unsigned char buf[9];
9386 unsigned int i, len;
9387 unsigned long offset;
9388
9389 for (i = 0; i < sizeof (buf); i++)
9390 {
9391 GET_OP (buf[i]);
9392 if ((buf[i] & 0x80) == 0)
9393 break;
9394 }
9395 if (i == sizeof (buf))
9396 {
9397 error (_("corrupt change to vsp\n"));
9398 res = false;
9399 }
9400 else
9401 {
9402 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
9403 assert (len == i + 1);
9404 offset = offset * 4 + 0x204;
9405 printf ("vsp = vsp + %ld", offset);
9406 }
9407 }
9408 else if (op == 0xb3 || op == 0xc8 || op == 0xc9)
9409 {
9410 unsigned int first, last;
9411
9412 GET_OP (op2);
9413 first = op2 >> 4;
9414 last = op2 & 0x0f;
9415 if (op == 0xc8)
9416 first = first + 16;
9417 printf ("pop {D%d", first);
9418 if (last)
9419 printf ("-D%d", first + last);
9420 printf ("}");
9421 }
9422 else if (op == 0xb4)
9423 printf (_(" pop {ra_auth_code}"));
9424 else if ((op & 0xf8) == 0xb8 || (op & 0xf8) == 0xd0)
9425 {
9426 unsigned int count = op & 0x07;
9427
9428 printf ("pop {D8");
9429 if (count)
9430 printf ("-D%d", 8 + count);
9431 printf ("}");
9432 }
9433 else if (op >= 0xc0 && op <= 0xc5)
9434 {
9435 unsigned int count = op & 0x07;
9436
9437 printf (" pop {wR10");
9438 if (count)
9439 printf ("-wR%d", 10 + count);
9440 printf ("}");
9441 }
9442 else if (op == 0xc6)
9443 {
9444 unsigned int first, last;
9445
9446 GET_OP (op2);
9447 first = op2 >> 4;
9448 last = op2 & 0x0f;
9449 printf ("pop {wR%d", first);
9450 if (last)
9451 printf ("-wR%d", first + last);
9452 printf ("}");
9453 }
9454 else if (op == 0xc7)
9455 {
9456 GET_OP (op2);
9457 if (op2 == 0 || (op2 & 0xf0) != 0)
9458 printf (_("[Spare]"));
9459 else
9460 {
9461 unsigned int mask = op2 & 0x0f;
9462 bool first = true;
9463 int i;
9464
9465 printf ("pop {");
9466 for (i = 0; i < 4; i++)
9467 if (mask & (1 << i))
9468 {
9469 if (first)
9470 first = false;
9471 else
9472 printf (", ");
9473 printf ("wCGR%d", i);
9474 }
9475 printf ("}");
9476 }
9477 }
9478 else
9479 {
9480 printf (_(" [unsupported opcode]"));
9481 res = false;
9482 }
9483
9484 printf ("\n");
9485 }
9486
9487 return res;
9488 }
9489
9490 static bool
9491 decode_tic6x_unwind_bytecode (Filedata * filedata,
9492 struct arm_unw_aux_info * aux,
9493 unsigned int word,
9494 unsigned int remaining,
9495 unsigned int more_words,
9496 bfd_vma data_offset,
9497 Elf_Internal_Shdr * data_sec,
9498 struct arm_section * data_arm_sec)
9499 {
9500 struct absaddr addr;
9501
9502 /* Decode the unwinding instructions. */
9503 while (1)
9504 {
9505 unsigned int op, op2;
9506
9507 ADVANCE;
9508 if (remaining == 0)
9509 break;
9510 remaining--;
9511 op = word >> 24;
9512 word <<= 8;
9513
9514 printf (" 0x%02x ", op);
9515
9516 if ((op & 0xc0) == 0x00)
9517 {
9518 int offset = ((op & 0x3f) << 3) + 8;
9519 printf (" sp = sp + %d", offset);
9520 }
9521 else if ((op & 0xc0) == 0x80)
9522 {
9523 GET_OP (op2);
9524 if (op == 0x80 && op2 == 0)
9525 printf (_("Refuse to unwind"));
9526 else
9527 {
9528 unsigned int mask = ((op & 0x1f) << 8) | op2;
9529 if (op & 0x20)
9530 printf ("pop compact {");
9531 else
9532 printf ("pop {");
9533
9534 decode_tic6x_unwind_regmask (mask);
9535 printf("}");
9536 }
9537 }
9538 else if ((op & 0xf0) == 0xc0)
9539 {
9540 unsigned int reg;
9541 unsigned int nregs;
9542 unsigned int i;
9543 const char *name;
9544 struct
9545 {
9546 unsigned int offset;
9547 unsigned int reg;
9548 } regpos[16];
9549
9550 /* Scan entire instruction first so that GET_OP output is not
9551 interleaved with disassembly. */
9552 nregs = 0;
9553 for (i = 0; nregs < (op & 0xf); i++)
9554 {
9555 GET_OP (op2);
9556 reg = op2 >> 4;
9557 if (reg != 0xf)
9558 {
9559 regpos[nregs].offset = i * 2;
9560 regpos[nregs].reg = reg;
9561 nregs++;
9562 }
9563
9564 reg = op2 & 0xf;
9565 if (reg != 0xf)
9566 {
9567 regpos[nregs].offset = i * 2 + 1;
9568 regpos[nregs].reg = reg;
9569 nregs++;
9570 }
9571 }
9572
9573 printf (_("pop frame {"));
9574 if (nregs == 0)
9575 {
9576 printf (_("*corrupt* - no registers specified"));
9577 }
9578 else
9579 {
9580 reg = nregs - 1;
9581 for (i = i * 2; i > 0; i--)
9582 {
9583 if (regpos[reg].offset == i - 1)
9584 {
9585 name = tic6x_unwind_regnames[regpos[reg].reg];
9586 if (reg > 0)
9587 reg--;
9588 }
9589 else
9590 name = _("[pad]");
9591
9592 fputs (name, stdout);
9593 if (i > 1)
9594 printf (", ");
9595 }
9596 }
9597
9598 printf ("}");
9599 }
9600 else if (op == 0xd0)
9601 printf (" MOV FP, SP");
9602 else if (op == 0xd1)
9603 printf (" __c6xabi_pop_rts");
9604 else if (op == 0xd2)
9605 {
9606 unsigned char buf[9];
9607 unsigned int i, len;
9608 unsigned long offset;
9609
9610 for (i = 0; i < sizeof (buf); i++)
9611 {
9612 GET_OP (buf[i]);
9613 if ((buf[i] & 0x80) == 0)
9614 break;
9615 }
9616 /* PR 17531: file: id:000001,src:001906+004739,op:splice,rep:2. */
9617 if (i == sizeof (buf))
9618 {
9619 warn (_("Corrupt stack pointer adjustment detected\n"));
9620 return false;
9621 }
9622
9623 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
9624 assert (len == i + 1);
9625 offset = offset * 8 + 0x408;
9626 printf (_("sp = sp + %ld"), offset);
9627 }
9628 else if ((op & 0xf0) == 0xe0)
9629 {
9630 if ((op & 0x0f) == 7)
9631 printf (" RETURN");
9632 else
9633 printf (" MV %s, B3", tic6x_unwind_regnames[op & 0x0f]);
9634 }
9635 else
9636 {
9637 printf (_(" [unsupported opcode]"));
9638 }
9639 putchar ('\n');
9640 }
9641
9642 return true;
9643 }
9644
9645 static bfd_vma
9646 arm_expand_prel31 (Filedata * filedata, bfd_vma word, bfd_vma where)
9647 {
9648 bfd_vma offset;
9649
9650 offset = word & 0x7fffffff;
9651 if (offset & 0x40000000)
9652 offset |= ~ (bfd_vma) 0x7fffffff;
9653
9654 if (filedata->file_header.e_machine == EM_TI_C6000)
9655 offset <<= 1;
9656
9657 return offset + where;
9658 }
9659
9660 static bool
9661 decode_arm_unwind (Filedata * filedata,
9662 struct arm_unw_aux_info * aux,
9663 unsigned int word,
9664 unsigned int remaining,
9665 bfd_vma data_offset,
9666 Elf_Internal_Shdr * data_sec,
9667 struct arm_section * data_arm_sec)
9668 {
9669 int per_index;
9670 unsigned int more_words = 0;
9671 struct absaddr addr;
9672 bfd_vma sym_name = (bfd_vma) -1;
9673 bool res = true;
9674
9675 if (remaining == 0)
9676 {
9677 /* Fetch the first word.
9678 Note - when decoding an object file the address extracted
9679 here will always be 0. So we also pass in the sym_name
9680 parameter so that we can find the symbol associated with
9681 the personality routine. */
9682 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, data_offset,
9683 & word, & addr, & sym_name))
9684 return false;
9685
9686 remaining = 4;
9687 }
9688 else
9689 {
9690 addr.section = SHN_UNDEF;
9691 addr.offset = 0;
9692 }
9693
9694 if ((word & 0x80000000) == 0)
9695 {
9696 /* Expand prel31 for personality routine. */
9697 bfd_vma fn;
9698 const char *procname;
9699
9700 fn = arm_expand_prel31 (filedata, word, data_sec->sh_addr + data_offset);
9701 printf (_(" Personality routine: "));
9702 if (fn == 0
9703 && addr.section == SHN_UNDEF && addr.offset == 0
9704 && sym_name != (bfd_vma) -1 && sym_name < aux->strtab_size)
9705 {
9706 procname = aux->strtab + sym_name;
9707 print_vma (fn, PREFIX_HEX);
9708 if (procname)
9709 {
9710 fputs (" <", stdout);
9711 fputs (procname, stdout);
9712 fputc ('>', stdout);
9713 }
9714 }
9715 else
9716 procname = arm_print_vma_and_name (filedata, aux, fn, addr);
9717 fputc ('\n', stdout);
9718
9719 /* The GCC personality routines use the standard compact
9720 encoding, starting with one byte giving the number of
9721 words. */
9722 if (procname != NULL
9723 && (startswith (procname, "__gcc_personality_v0")
9724 || startswith (procname, "__gxx_personality_v0")
9725 || startswith (procname, "__gcj_personality_v0")
9726 || startswith (procname, "__gnu_objc_personality_v0")))
9727 {
9728 remaining = 0;
9729 more_words = 1;
9730 ADVANCE;
9731 if (!remaining)
9732 {
9733 printf (_(" [Truncated data]\n"));
9734 return false;
9735 }
9736 more_words = word >> 24;
9737 word <<= 8;
9738 remaining--;
9739 per_index = -1;
9740 }
9741 else
9742 return true;
9743 }
9744 else
9745 {
9746 /* ARM EHABI Section 6.3:
9747
9748 An exception-handling table entry for the compact model looks like:
9749
9750 31 30-28 27-24 23-0
9751 -- ----- ----- ----
9752 1 0 index Data for personalityRoutine[index] */
9753
9754 if (filedata->file_header.e_machine == EM_ARM
9755 && (word & 0x70000000))
9756 {
9757 warn (_("Corrupt ARM compact model table entry: %x \n"), word);
9758 res = false;
9759 }
9760
9761 per_index = (word >> 24) & 0x7f;
9762 printf (_(" Compact model index: %d\n"), per_index);
9763 if (per_index == 0)
9764 {
9765 more_words = 0;
9766 word <<= 8;
9767 remaining--;
9768 }
9769 else if (per_index < 3)
9770 {
9771 more_words = (word >> 16) & 0xff;
9772 word <<= 16;
9773 remaining -= 2;
9774 }
9775 }
9776
9777 switch (filedata->file_header.e_machine)
9778 {
9779 case EM_ARM:
9780 if (per_index < 3)
9781 {
9782 if (! decode_arm_unwind_bytecode (filedata, aux, word, remaining, more_words,
9783 data_offset, data_sec, data_arm_sec))
9784 res = false;
9785 }
9786 else
9787 {
9788 warn (_("Unknown ARM compact model index encountered\n"));
9789 printf (_(" [reserved]\n"));
9790 res = false;
9791 }
9792 break;
9793
9794 case EM_TI_C6000:
9795 if (per_index < 3)
9796 {
9797 if (! decode_tic6x_unwind_bytecode (filedata, aux, word, remaining, more_words,
9798 data_offset, data_sec, data_arm_sec))
9799 res = false;
9800 }
9801 else if (per_index < 5)
9802 {
9803 if (((word >> 17) & 0x7f) == 0x7f)
9804 printf (_(" Restore stack from frame pointer\n"));
9805 else
9806 printf (_(" Stack increment %d\n"), (word >> 14) & 0x1fc);
9807 printf (_(" Registers restored: "));
9808 if (per_index == 4)
9809 printf (" (compact) ");
9810 decode_tic6x_unwind_regmask ((word >> 4) & 0x1fff);
9811 putchar ('\n');
9812 printf (_(" Return register: %s\n"),
9813 tic6x_unwind_regnames[word & 0xf]);
9814 }
9815 else
9816 printf (_(" [reserved (%d)]\n"), per_index);
9817 break;
9818
9819 default:
9820 error (_("Unsupported architecture type %d encountered when decoding unwind table\n"),
9821 filedata->file_header.e_machine);
9822 res = false;
9823 }
9824
9825 /* Decode the descriptors. Not implemented. */
9826
9827 return res;
9828 }
9829
9830 static bool
9831 dump_arm_unwind (Filedata * filedata,
9832 struct arm_unw_aux_info * aux,
9833 Elf_Internal_Shdr * exidx_sec)
9834 {
9835 struct arm_section exidx_arm_sec, extab_arm_sec;
9836 unsigned int i, exidx_len;
9837 unsigned long j, nfuns;
9838 bool res = true;
9839
9840 memset (&exidx_arm_sec, 0, sizeof (exidx_arm_sec));
9841 memset (&extab_arm_sec, 0, sizeof (extab_arm_sec));
9842 exidx_len = exidx_sec->sh_size / 8;
9843
9844 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
9845 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
9846 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
9847 aux->funtab[nfuns++] = aux->symtab[j];
9848 aux->nfuns = nfuns;
9849 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
9850
9851 for (i = 0; i < exidx_len; i++)
9852 {
9853 unsigned int exidx_fn, exidx_entry;
9854 struct absaddr fn_addr, entry_addr;
9855 bfd_vma fn;
9856
9857 fputc ('\n', stdout);
9858
9859 if (! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
9860 8 * i, & exidx_fn, & fn_addr, NULL)
9861 || ! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
9862 8 * i + 4, & exidx_entry, & entry_addr, NULL))
9863 {
9864 free (aux->funtab);
9865 arm_free_section (& exidx_arm_sec);
9866 arm_free_section (& extab_arm_sec);
9867 return false;
9868 }
9869
9870 /* ARM EHABI, Section 5:
9871 An index table entry consists of 2 words.
9872 The first word contains a prel31 offset to the start of a function, with bit 31 clear. */
9873 if (exidx_fn & 0x80000000)
9874 {
9875 warn (_("corrupt index table entry: %x\n"), exidx_fn);
9876 res = false;
9877 }
9878
9879 fn = arm_expand_prel31 (filedata, exidx_fn, exidx_sec->sh_addr + 8 * i);
9880
9881 arm_print_vma_and_name (filedata, aux, fn, fn_addr);
9882 fputs (": ", stdout);
9883
9884 if (exidx_entry == 1)
9885 {
9886 print_vma (exidx_entry, PREFIX_HEX);
9887 fputs (" [cantunwind]\n", stdout);
9888 }
9889 else if (exidx_entry & 0x80000000)
9890 {
9891 print_vma (exidx_entry, PREFIX_HEX);
9892 fputc ('\n', stdout);
9893 decode_arm_unwind (filedata, aux, exidx_entry, 4, 0, NULL, NULL);
9894 }
9895 else
9896 {
9897 bfd_vma table, table_offset = 0;
9898 Elf_Internal_Shdr *table_sec;
9899
9900 fputs ("@", stdout);
9901 table = arm_expand_prel31 (filedata, exidx_entry, exidx_sec->sh_addr + 8 * i + 4);
9902 print_vma (table, PREFIX_HEX);
9903 printf ("\n");
9904
9905 /* Locate the matching .ARM.extab. */
9906 if (entry_addr.section != SHN_UNDEF
9907 && entry_addr.section < filedata->file_header.e_shnum)
9908 {
9909 table_sec = filedata->section_headers + entry_addr.section;
9910 table_offset = entry_addr.offset;
9911 /* PR 18879 */
9912 if (table_offset > table_sec->sh_size
9913 || ((bfd_signed_vma) table_offset) < 0)
9914 {
9915 warn (_("Unwind entry contains corrupt offset (0x%lx) into section %s\n"),
9916 (unsigned long) table_offset,
9917 printable_section_name (filedata, table_sec));
9918 res = false;
9919 continue;
9920 }
9921 }
9922 else
9923 {
9924 table_sec = find_section_by_address (filedata, table);
9925 if (table_sec != NULL)
9926 table_offset = table - table_sec->sh_addr;
9927 }
9928
9929 if (table_sec == NULL)
9930 {
9931 warn (_("Could not locate .ARM.extab section containing 0x%lx.\n"),
9932 (unsigned long) table);
9933 res = false;
9934 continue;
9935 }
9936
9937 if (! decode_arm_unwind (filedata, aux, 0, 0, table_offset, table_sec,
9938 &extab_arm_sec))
9939 res = false;
9940 }
9941 }
9942
9943 printf ("\n");
9944
9945 free (aux->funtab);
9946 arm_free_section (&exidx_arm_sec);
9947 arm_free_section (&extab_arm_sec);
9948
9949 return res;
9950 }
9951
9952 /* Used for both ARM and C6X unwinding tables. */
9953
9954 static bool
9955 arm_process_unwind (Filedata * filedata)
9956 {
9957 struct arm_unw_aux_info aux;
9958 Elf_Internal_Shdr *unwsec = NULL;
9959 Elf_Internal_Shdr *sec;
9960 unsigned long i;
9961 unsigned int sec_type;
9962 bool res = true;
9963
9964 switch (filedata->file_header.e_machine)
9965 {
9966 case EM_ARM:
9967 sec_type = SHT_ARM_EXIDX;
9968 break;
9969
9970 case EM_TI_C6000:
9971 sec_type = SHT_C6000_UNWIND;
9972 break;
9973
9974 default:
9975 error (_("Unsupported architecture type %d encountered when processing unwind table\n"),
9976 filedata->file_header.e_machine);
9977 return false;
9978 }
9979
9980 if (filedata->string_table == NULL)
9981 return false;
9982
9983 memset (& aux, 0, sizeof (aux));
9984 aux.filedata = filedata;
9985
9986 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9987 {
9988 if (sec->sh_type == SHT_SYMTAB)
9989 {
9990 if (aux.symtab)
9991 {
9992 error (_("Multiple symbol tables encountered\n"));
9993 free (aux.symtab);
9994 aux.symtab = NULL;
9995 free (aux.strtab);
9996 aux.strtab = NULL;
9997 }
9998 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
9999 &aux.strtab, &aux.strtab_size))
10000 return false;
10001 }
10002 else if (sec->sh_type == sec_type)
10003 unwsec = sec;
10004 }
10005
10006 if (unwsec == NULL)
10007 printf (_("\nThere are no unwind sections in this file.\n"));
10008 else
10009 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
10010 {
10011 if (sec->sh_type == sec_type)
10012 {
10013 unsigned long num_unwind = sec->sh_size / (2 * eh_addr_size);
10014 printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
10015 "contains %lu entry:\n",
10016 "\nUnwind section '%s' at offset 0x%lx "
10017 "contains %lu entries:\n",
10018 num_unwind),
10019 printable_section_name (filedata, sec),
10020 (unsigned long) sec->sh_offset,
10021 num_unwind);
10022
10023 if (! dump_arm_unwind (filedata, &aux, sec))
10024 res = false;
10025 }
10026 }
10027
10028 free (aux.symtab);
10029 free ((char *) aux.strtab);
10030
10031 return res;
10032 }
10033
10034 static bool
10035 no_processor_specific_unwind (Filedata * filedata ATTRIBUTE_UNUSED)
10036 {
10037 printf (_("No processor specific unwind information to decode\n"));
10038 return true;
10039 }
10040
10041 static bool
10042 process_unwind (Filedata * filedata)
10043 {
10044 struct unwind_handler
10045 {
10046 unsigned int machtype;
10047 bool (* handler)(Filedata *);
10048 } handlers[] =
10049 {
10050 { EM_ARM, arm_process_unwind },
10051 { EM_IA_64, ia64_process_unwind },
10052 { EM_PARISC, hppa_process_unwind },
10053 { EM_TI_C6000, arm_process_unwind },
10054 { EM_386, no_processor_specific_unwind },
10055 { EM_X86_64, no_processor_specific_unwind },
10056 { 0, NULL }
10057 };
10058 int i;
10059
10060 if (!do_unwind)
10061 return true;
10062
10063 for (i = 0; handlers[i].handler != NULL; i++)
10064 if (filedata->file_header.e_machine == handlers[i].machtype)
10065 return handlers[i].handler (filedata);
10066
10067 printf (_("\nThe decoding of unwind sections for machine type %s is not currently supported.\n"),
10068 get_machine_name (filedata->file_header.e_machine));
10069 return true;
10070 }
10071
10072 static void
10073 dynamic_section_aarch64_val (Elf_Internal_Dyn * entry)
10074 {
10075 switch (entry->d_tag)
10076 {
10077 case DT_AARCH64_BTI_PLT:
10078 case DT_AARCH64_PAC_PLT:
10079 break;
10080 default:
10081 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10082 break;
10083 }
10084 putchar ('\n');
10085 }
10086
10087 static void
10088 dynamic_section_mips_val (Filedata * filedata, Elf_Internal_Dyn * entry)
10089 {
10090 switch (entry->d_tag)
10091 {
10092 case DT_MIPS_FLAGS:
10093 if (entry->d_un.d_val == 0)
10094 printf (_("NONE"));
10095 else
10096 {
10097 static const char * opts[] =
10098 {
10099 "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
10100 "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
10101 "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
10102 "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
10103 "RLD_ORDER_SAFE"
10104 };
10105 unsigned int cnt;
10106 bool first = true;
10107
10108 for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
10109 if (entry->d_un.d_val & (1 << cnt))
10110 {
10111 printf ("%s%s", first ? "" : " ", opts[cnt]);
10112 first = false;
10113 }
10114 }
10115 break;
10116
10117 case DT_MIPS_IVERSION:
10118 if (valid_dynamic_name (filedata, entry->d_un.d_val))
10119 printf (_("Interface Version: %s"),
10120 get_dynamic_name (filedata, entry->d_un.d_val));
10121 else
10122 {
10123 char buf[40];
10124 sprintf_vma (buf, entry->d_un.d_ptr);
10125 /* Note: coded this way so that there is a single string for translation. */
10126 printf (_("<corrupt: %s>"), buf);
10127 }
10128 break;
10129
10130 case DT_MIPS_TIME_STAMP:
10131 {
10132 char timebuf[128];
10133 struct tm * tmp;
10134 time_t atime = entry->d_un.d_val;
10135
10136 tmp = gmtime (&atime);
10137 /* PR 17531: file: 6accc532. */
10138 if (tmp == NULL)
10139 snprintf (timebuf, sizeof (timebuf), _("<corrupt>"));
10140 else
10141 snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
10142 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
10143 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
10144 printf (_("Time Stamp: %s"), timebuf);
10145 }
10146 break;
10147
10148 case DT_MIPS_RLD_VERSION:
10149 case DT_MIPS_LOCAL_GOTNO:
10150 case DT_MIPS_CONFLICTNO:
10151 case DT_MIPS_LIBLISTNO:
10152 case DT_MIPS_SYMTABNO:
10153 case DT_MIPS_UNREFEXTNO:
10154 case DT_MIPS_HIPAGENO:
10155 case DT_MIPS_DELTA_CLASS_NO:
10156 case DT_MIPS_DELTA_INSTANCE_NO:
10157 case DT_MIPS_DELTA_RELOC_NO:
10158 case DT_MIPS_DELTA_SYM_NO:
10159 case DT_MIPS_DELTA_CLASSSYM_NO:
10160 case DT_MIPS_COMPACT_SIZE:
10161 print_vma (entry->d_un.d_val, DEC);
10162 break;
10163
10164 case DT_MIPS_XHASH:
10165 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
10166 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10167 /* Falls through. */
10168
10169 default:
10170 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10171 }
10172 putchar ('\n');
10173 }
10174
10175 static void
10176 dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
10177 {
10178 switch (entry->d_tag)
10179 {
10180 case DT_HP_DLD_FLAGS:
10181 {
10182 static struct
10183 {
10184 long int bit;
10185 const char * str;
10186 }
10187 flags[] =
10188 {
10189 { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
10190 { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
10191 { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
10192 { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
10193 { DT_HP_BIND_NOW, "HP_BIND_NOW" },
10194 { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
10195 { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
10196 { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
10197 { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
10198 { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
10199 { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
10200 { DT_HP_GST, "HP_GST" },
10201 { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
10202 { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
10203 { DT_HP_NODELETE, "HP_NODELETE" },
10204 { DT_HP_GROUP, "HP_GROUP" },
10205 { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
10206 };
10207 bool first = true;
10208 size_t cnt;
10209 bfd_vma val = entry->d_un.d_val;
10210
10211 for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
10212 if (val & flags[cnt].bit)
10213 {
10214 if (! first)
10215 putchar (' ');
10216 fputs (flags[cnt].str, stdout);
10217 first = false;
10218 val ^= flags[cnt].bit;
10219 }
10220
10221 if (val != 0 || first)
10222 {
10223 if (! first)
10224 putchar (' ');
10225 print_vma (val, HEX);
10226 }
10227 }
10228 break;
10229
10230 default:
10231 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10232 break;
10233 }
10234 putchar ('\n');
10235 }
10236
10237 #ifdef BFD64
10238
10239 /* VMS vs Unix time offset and factor. */
10240
10241 #define VMS_EPOCH_OFFSET 35067168000000000LL
10242 #define VMS_GRANULARITY_FACTOR 10000000
10243 #ifndef INT64_MIN
10244 #define INT64_MIN (-9223372036854775807LL - 1)
10245 #endif
10246
10247 /* Display a VMS time in a human readable format. */
10248
10249 static void
10250 print_vms_time (bfd_int64_t vmstime)
10251 {
10252 struct tm *tm = NULL;
10253 time_t unxtime;
10254
10255 if (vmstime >= INT64_MIN + VMS_EPOCH_OFFSET)
10256 {
10257 vmstime = (vmstime - VMS_EPOCH_OFFSET) / VMS_GRANULARITY_FACTOR;
10258 unxtime = vmstime;
10259 if (unxtime == vmstime)
10260 tm = gmtime (&unxtime);
10261 }
10262 if (tm != NULL)
10263 printf ("%04u-%02u-%02uT%02u:%02u:%02u",
10264 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
10265 tm->tm_hour, tm->tm_min, tm->tm_sec);
10266 }
10267 #endif /* BFD64 */
10268
10269 static void
10270 dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
10271 {
10272 switch (entry->d_tag)
10273 {
10274 case DT_IA_64_PLT_RESERVE:
10275 /* First 3 slots reserved. */
10276 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10277 printf (" -- ");
10278 print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
10279 break;
10280
10281 case DT_IA_64_VMS_LINKTIME:
10282 #ifdef BFD64
10283 print_vms_time (entry->d_un.d_val);
10284 #endif
10285 break;
10286
10287 case DT_IA_64_VMS_LNKFLAGS:
10288 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10289 if (entry->d_un.d_val & VMS_LF_CALL_DEBUG)
10290 printf (" CALL_DEBUG");
10291 if (entry->d_un.d_val & VMS_LF_NOP0BUFS)
10292 printf (" NOP0BUFS");
10293 if (entry->d_un.d_val & VMS_LF_P0IMAGE)
10294 printf (" P0IMAGE");
10295 if (entry->d_un.d_val & VMS_LF_MKTHREADS)
10296 printf (" MKTHREADS");
10297 if (entry->d_un.d_val & VMS_LF_UPCALLS)
10298 printf (" UPCALLS");
10299 if (entry->d_un.d_val & VMS_LF_IMGSTA)
10300 printf (" IMGSTA");
10301 if (entry->d_un.d_val & VMS_LF_INITIALIZE)
10302 printf (" INITIALIZE");
10303 if (entry->d_un.d_val & VMS_LF_MAIN)
10304 printf (" MAIN");
10305 if (entry->d_un.d_val & VMS_LF_EXE_INIT)
10306 printf (" EXE_INIT");
10307 if (entry->d_un.d_val & VMS_LF_TBK_IN_IMG)
10308 printf (" TBK_IN_IMG");
10309 if (entry->d_un.d_val & VMS_LF_DBG_IN_IMG)
10310 printf (" DBG_IN_IMG");
10311 if (entry->d_un.d_val & VMS_LF_TBK_IN_DSF)
10312 printf (" TBK_IN_DSF");
10313 if (entry->d_un.d_val & VMS_LF_DBG_IN_DSF)
10314 printf (" DBG_IN_DSF");
10315 if (entry->d_un.d_val & VMS_LF_SIGNATURES)
10316 printf (" SIGNATURES");
10317 if (entry->d_un.d_val & VMS_LF_REL_SEG_OFF)
10318 printf (" REL_SEG_OFF");
10319 break;
10320
10321 default:
10322 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10323 break;
10324 }
10325 putchar ('\n');
10326 }
10327
10328 static bool
10329 get_32bit_dynamic_section (Filedata * filedata)
10330 {
10331 Elf32_External_Dyn * edyn;
10332 Elf32_External_Dyn * ext;
10333 Elf_Internal_Dyn * entry;
10334
10335 edyn = (Elf32_External_Dyn *) get_data (NULL, filedata,
10336 filedata->dynamic_addr, 1,
10337 filedata->dynamic_size,
10338 _("dynamic section"));
10339 if (!edyn)
10340 return false;
10341
10342 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
10343 might not have the luxury of section headers. Look for the DT_NULL
10344 terminator to determine the number of entries. */
10345 for (ext = edyn, filedata->dynamic_nent = 0;
10346 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
10347 ext++)
10348 {
10349 filedata->dynamic_nent++;
10350 if (BYTE_GET (ext->d_tag) == DT_NULL)
10351 break;
10352 }
10353
10354 filedata->dynamic_section
10355 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
10356 if (filedata->dynamic_section == NULL)
10357 {
10358 error (_("Out of memory allocating space for %lu dynamic entries\n"),
10359 (unsigned long) filedata->dynamic_nent);
10360 free (edyn);
10361 return false;
10362 }
10363
10364 for (ext = edyn, entry = filedata->dynamic_section;
10365 entry < filedata->dynamic_section + filedata->dynamic_nent;
10366 ext++, entry++)
10367 {
10368 entry->d_tag = BYTE_GET (ext->d_tag);
10369 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
10370 }
10371
10372 free (edyn);
10373
10374 return true;
10375 }
10376
10377 static bool
10378 get_64bit_dynamic_section (Filedata * filedata)
10379 {
10380 Elf64_External_Dyn * edyn;
10381 Elf64_External_Dyn * ext;
10382 Elf_Internal_Dyn * entry;
10383
10384 /* Read in the data. */
10385 edyn = (Elf64_External_Dyn *) get_data (NULL, filedata,
10386 filedata->dynamic_addr, 1,
10387 filedata->dynamic_size,
10388 _("dynamic section"));
10389 if (!edyn)
10390 return false;
10391
10392 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
10393 might not have the luxury of section headers. Look for the DT_NULL
10394 terminator to determine the number of entries. */
10395 for (ext = edyn, filedata->dynamic_nent = 0;
10396 /* PR 17533 file: 033-67080-0.004 - do not read past end of buffer. */
10397 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
10398 ext++)
10399 {
10400 filedata->dynamic_nent++;
10401 if (BYTE_GET (ext->d_tag) == DT_NULL)
10402 break;
10403 }
10404
10405 filedata->dynamic_section
10406 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
10407 if (filedata->dynamic_section == NULL)
10408 {
10409 error (_("Out of memory allocating space for %lu dynamic entries\n"),
10410 (unsigned long) filedata->dynamic_nent);
10411 free (edyn);
10412 return false;
10413 }
10414
10415 /* Convert from external to internal formats. */
10416 for (ext = edyn, entry = filedata->dynamic_section;
10417 entry < filedata->dynamic_section + filedata->dynamic_nent;
10418 ext++, entry++)
10419 {
10420 entry->d_tag = BYTE_GET (ext->d_tag);
10421 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
10422 }
10423
10424 free (edyn);
10425
10426 return true;
10427 }
10428
10429 static bool
10430 get_dynamic_section (Filedata *filedata)
10431 {
10432 if (filedata->dynamic_section)
10433 return true;
10434
10435 if (is_32bit_elf)
10436 return get_32bit_dynamic_section (filedata);
10437 else
10438 return get_64bit_dynamic_section (filedata);
10439 }
10440
10441 static void
10442 print_dynamic_flags (bfd_vma flags)
10443 {
10444 bool first = true;
10445
10446 while (flags)
10447 {
10448 bfd_vma flag;
10449
10450 flag = flags & - flags;
10451 flags &= ~ flag;
10452
10453 if (first)
10454 first = false;
10455 else
10456 putc (' ', stdout);
10457
10458 switch (flag)
10459 {
10460 case DF_ORIGIN: fputs ("ORIGIN", stdout); break;
10461 case DF_SYMBOLIC: fputs ("SYMBOLIC", stdout); break;
10462 case DF_TEXTREL: fputs ("TEXTREL", stdout); break;
10463 case DF_BIND_NOW: fputs ("BIND_NOW", stdout); break;
10464 case DF_STATIC_TLS: fputs ("STATIC_TLS", stdout); break;
10465 default: fputs (_("unknown"), stdout); break;
10466 }
10467 }
10468 puts ("");
10469 }
10470
10471 static bfd_vma *
10472 get_dynamic_data (Filedata * filedata, bfd_size_type number, unsigned int ent_size)
10473 {
10474 unsigned char * e_data;
10475 bfd_vma * i_data;
10476
10477 /* If the size_t type is smaller than the bfd_size_type, eg because
10478 you are building a 32-bit tool on a 64-bit host, then make sure
10479 that when (number) is cast to (size_t) no information is lost. */
10480 if (sizeof (size_t) < sizeof (bfd_size_type)
10481 && (bfd_size_type) ((size_t) number) != number)
10482 {
10483 error (_("Size truncation prevents reading %s elements of size %u\n"),
10484 bfd_vmatoa ("u", number), ent_size);
10485 return NULL;
10486 }
10487
10488 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
10489 attempting to allocate memory when the read is bound to fail. */
10490 if (ent_size * number > filedata->file_size)
10491 {
10492 error (_("Invalid number of dynamic entries: %s\n"),
10493 bfd_vmatoa ("u", number));
10494 return NULL;
10495 }
10496
10497 e_data = (unsigned char *) cmalloc ((size_t) number, ent_size);
10498 if (e_data == NULL)
10499 {
10500 error (_("Out of memory reading %s dynamic entries\n"),
10501 bfd_vmatoa ("u", number));
10502 return NULL;
10503 }
10504
10505 if (fread (e_data, ent_size, (size_t) number, filedata->handle) != number)
10506 {
10507 error (_("Unable to read in %s bytes of dynamic data\n"),
10508 bfd_vmatoa ("u", number * ent_size));
10509 free (e_data);
10510 return NULL;
10511 }
10512
10513 i_data = (bfd_vma *) cmalloc ((size_t) number, sizeof (*i_data));
10514 if (i_data == NULL)
10515 {
10516 error (_("Out of memory allocating space for %s dynamic entries\n"),
10517 bfd_vmatoa ("u", number));
10518 free (e_data);
10519 return NULL;
10520 }
10521
10522 while (number--)
10523 i_data[number] = byte_get (e_data + number * ent_size, ent_size);
10524
10525 free (e_data);
10526
10527 return i_data;
10528 }
10529
10530 static unsigned long
10531 get_num_dynamic_syms (Filedata * filedata)
10532 {
10533 unsigned long num_of_syms = 0;
10534
10535 if (!do_histogram && (!do_using_dynamic || do_dyn_syms))
10536 return num_of_syms;
10537
10538 if (filedata->dynamic_info[DT_HASH])
10539 {
10540 unsigned char nb[8];
10541 unsigned char nc[8];
10542 unsigned int hash_ent_size = 4;
10543
10544 if ((filedata->file_header.e_machine == EM_ALPHA
10545 || filedata->file_header.e_machine == EM_S390
10546 || filedata->file_header.e_machine == EM_S390_OLD)
10547 && filedata->file_header.e_ident[EI_CLASS] == ELFCLASS64)
10548 hash_ent_size = 8;
10549
10550 if (fseek (filedata->handle,
10551 (filedata->archive_file_offset
10552 + offset_from_vma (filedata, filedata->dynamic_info[DT_HASH],
10553 sizeof nb + sizeof nc)),
10554 SEEK_SET))
10555 {
10556 error (_("Unable to seek to start of dynamic information\n"));
10557 goto no_hash;
10558 }
10559
10560 if (fread (nb, hash_ent_size, 1, filedata->handle) != 1)
10561 {
10562 error (_("Failed to read in number of buckets\n"));
10563 goto no_hash;
10564 }
10565
10566 if (fread (nc, hash_ent_size, 1, filedata->handle) != 1)
10567 {
10568 error (_("Failed to read in number of chains\n"));
10569 goto no_hash;
10570 }
10571
10572 filedata->nbuckets = byte_get (nb, hash_ent_size);
10573 filedata->nchains = byte_get (nc, hash_ent_size);
10574
10575 if (filedata->nbuckets != 0 && filedata->nchains != 0)
10576 {
10577 filedata->buckets = get_dynamic_data (filedata, filedata->nbuckets,
10578 hash_ent_size);
10579 filedata->chains = get_dynamic_data (filedata, filedata->nchains,
10580 hash_ent_size);
10581
10582 if (filedata->buckets != NULL && filedata->chains != NULL)
10583 num_of_syms = filedata->nchains;
10584 }
10585 no_hash:
10586 if (num_of_syms == 0)
10587 {
10588 free (filedata->buckets);
10589 filedata->buckets = NULL;
10590 free (filedata->chains);
10591 filedata->chains = NULL;
10592 filedata->nbuckets = 0;
10593 }
10594 }
10595
10596 if (filedata->dynamic_info_DT_GNU_HASH)
10597 {
10598 unsigned char nb[16];
10599 bfd_vma i, maxchain = 0xffffffff, bitmaskwords;
10600 bfd_vma buckets_vma;
10601 unsigned long hn;
10602
10603 if (fseek (filedata->handle,
10604 (filedata->archive_file_offset
10605 + offset_from_vma (filedata,
10606 filedata->dynamic_info_DT_GNU_HASH,
10607 sizeof nb)),
10608 SEEK_SET))
10609 {
10610 error (_("Unable to seek to start of dynamic information\n"));
10611 goto no_gnu_hash;
10612 }
10613
10614 if (fread (nb, 16, 1, filedata->handle) != 1)
10615 {
10616 error (_("Failed to read in number of buckets\n"));
10617 goto no_gnu_hash;
10618 }
10619
10620 filedata->ngnubuckets = byte_get (nb, 4);
10621 filedata->gnusymidx = byte_get (nb + 4, 4);
10622 bitmaskwords = byte_get (nb + 8, 4);
10623 buckets_vma = filedata->dynamic_info_DT_GNU_HASH + 16;
10624 if (is_32bit_elf)
10625 buckets_vma += bitmaskwords * 4;
10626 else
10627 buckets_vma += bitmaskwords * 8;
10628
10629 if (fseek (filedata->handle,
10630 (filedata->archive_file_offset
10631 + offset_from_vma (filedata, buckets_vma, 4)),
10632 SEEK_SET))
10633 {
10634 error (_("Unable to seek to start of dynamic information\n"));
10635 goto no_gnu_hash;
10636 }
10637
10638 filedata->gnubuckets
10639 = get_dynamic_data (filedata, filedata->ngnubuckets, 4);
10640
10641 if (filedata->gnubuckets == NULL)
10642 goto no_gnu_hash;
10643
10644 for (i = 0; i < filedata->ngnubuckets; i++)
10645 if (filedata->gnubuckets[i] != 0)
10646 {
10647 if (filedata->gnubuckets[i] < filedata->gnusymidx)
10648 goto no_gnu_hash;
10649
10650 if (maxchain == 0xffffffff || filedata->gnubuckets[i] > maxchain)
10651 maxchain = filedata->gnubuckets[i];
10652 }
10653
10654 if (maxchain == 0xffffffff)
10655 goto no_gnu_hash;
10656
10657 maxchain -= filedata->gnusymidx;
10658
10659 if (fseek (filedata->handle,
10660 (filedata->archive_file_offset
10661 + offset_from_vma (filedata,
10662 buckets_vma + 4 * (filedata->ngnubuckets
10663 + maxchain),
10664 4)),
10665 SEEK_SET))
10666 {
10667 error (_("Unable to seek to start of dynamic information\n"));
10668 goto no_gnu_hash;
10669 }
10670
10671 do
10672 {
10673 if (fread (nb, 4, 1, filedata->handle) != 1)
10674 {
10675 error (_("Failed to determine last chain length\n"));
10676 goto no_gnu_hash;
10677 }
10678
10679 if (maxchain + 1 == 0)
10680 goto no_gnu_hash;
10681
10682 ++maxchain;
10683 }
10684 while ((byte_get (nb, 4) & 1) == 0);
10685
10686 if (fseek (filedata->handle,
10687 (filedata->archive_file_offset
10688 + offset_from_vma (filedata, (buckets_vma
10689 + 4 * filedata->ngnubuckets),
10690 4)),
10691 SEEK_SET))
10692 {
10693 error (_("Unable to seek to start of dynamic information\n"));
10694 goto no_gnu_hash;
10695 }
10696
10697 filedata->gnuchains = get_dynamic_data (filedata, maxchain, 4);
10698 filedata->ngnuchains = maxchain;
10699
10700 if (filedata->gnuchains == NULL)
10701 goto no_gnu_hash;
10702
10703 if (filedata->dynamic_info_DT_MIPS_XHASH)
10704 {
10705 if (fseek (filedata->handle,
10706 (filedata->archive_file_offset
10707 + offset_from_vma (filedata, (buckets_vma
10708 + 4 * (filedata->ngnubuckets
10709 + maxchain)), 4)),
10710 SEEK_SET))
10711 {
10712 error (_("Unable to seek to start of dynamic information\n"));
10713 goto no_gnu_hash;
10714 }
10715
10716 filedata->mipsxlat = get_dynamic_data (filedata, maxchain, 4);
10717 if (filedata->mipsxlat == NULL)
10718 goto no_gnu_hash;
10719 }
10720
10721 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
10722 if (filedata->gnubuckets[hn] != 0)
10723 {
10724 bfd_vma si = filedata->gnubuckets[hn];
10725 bfd_vma off = si - filedata->gnusymidx;
10726
10727 do
10728 {
10729 if (filedata->dynamic_info_DT_MIPS_XHASH)
10730 {
10731 if (off < filedata->ngnuchains
10732 && filedata->mipsxlat[off] >= num_of_syms)
10733 num_of_syms = filedata->mipsxlat[off] + 1;
10734 }
10735 else
10736 {
10737 if (si >= num_of_syms)
10738 num_of_syms = si + 1;
10739 }
10740 si++;
10741 }
10742 while (off < filedata->ngnuchains
10743 && (filedata->gnuchains[off++] & 1) == 0);
10744 }
10745
10746 if (num_of_syms == 0)
10747 {
10748 no_gnu_hash:
10749 free (filedata->mipsxlat);
10750 filedata->mipsxlat = NULL;
10751 free (filedata->gnuchains);
10752 filedata->gnuchains = NULL;
10753 free (filedata->gnubuckets);
10754 filedata->gnubuckets = NULL;
10755 filedata->ngnubuckets = 0;
10756 filedata->ngnuchains = 0;
10757 }
10758 }
10759
10760 return num_of_syms;
10761 }
10762
10763 /* Parse and display the contents of the dynamic section. */
10764
10765 static bool
10766 process_dynamic_section (Filedata * filedata)
10767 {
10768 Elf_Internal_Dyn * entry;
10769
10770 if (filedata->dynamic_size <= 1)
10771 {
10772 if (do_dynamic)
10773 {
10774 if (filedata->is_separate)
10775 printf (_("\nThere is no dynamic section in linked file '%s'.\n"),
10776 filedata->file_name);
10777 else
10778 printf (_("\nThere is no dynamic section in this file.\n"));
10779 }
10780
10781 return true;
10782 }
10783
10784 if (!get_dynamic_section (filedata))
10785 return false;
10786
10787 /* Find the appropriate symbol table. */
10788 if (filedata->dynamic_symbols == NULL || do_histogram)
10789 {
10790 unsigned long num_of_syms;
10791
10792 for (entry = filedata->dynamic_section;
10793 entry < filedata->dynamic_section + filedata->dynamic_nent;
10794 ++entry)
10795 if (entry->d_tag == DT_SYMTAB)
10796 filedata->dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
10797 else if (entry->d_tag == DT_SYMENT)
10798 filedata->dynamic_info[DT_SYMENT] = entry->d_un.d_val;
10799 else if (entry->d_tag == DT_HASH)
10800 filedata->dynamic_info[DT_HASH] = entry->d_un.d_val;
10801 else if (entry->d_tag == DT_GNU_HASH)
10802 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10803 else if ((filedata->file_header.e_machine == EM_MIPS
10804 || filedata->file_header.e_machine == EM_MIPS_RS3_LE)
10805 && entry->d_tag == DT_MIPS_XHASH)
10806 {
10807 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
10808 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10809 }
10810
10811 num_of_syms = get_num_dynamic_syms (filedata);
10812
10813 if (num_of_syms != 0
10814 && filedata->dynamic_symbols == NULL
10815 && filedata->dynamic_info[DT_SYMTAB]
10816 && filedata->dynamic_info[DT_SYMENT])
10817 {
10818 Elf_Internal_Phdr *seg;
10819 bfd_vma vma = filedata->dynamic_info[DT_SYMTAB];
10820
10821 if (! get_program_headers (filedata))
10822 {
10823 error (_("Cannot interpret virtual addresses "
10824 "without program headers.\n"));
10825 return false;
10826 }
10827
10828 for (seg = filedata->program_headers;
10829 seg < filedata->program_headers + filedata->file_header.e_phnum;
10830 ++seg)
10831 {
10832 if (seg->p_type != PT_LOAD)
10833 continue;
10834
10835 if (seg->p_offset + seg->p_filesz > filedata->file_size)
10836 {
10837 /* See PR 21379 for a reproducer. */
10838 error (_("Invalid PT_LOAD entry\n"));
10839 return false;
10840 }
10841
10842 if (vma >= (seg->p_vaddr & -seg->p_align)
10843 && vma < seg->p_vaddr + seg->p_filesz)
10844 {
10845 /* Since we do not know how big the symbol table is,
10846 we default to reading in up to the end of PT_LOAD
10847 segment and processing that. This is overkill, I
10848 know, but it should work. */
10849 Elf_Internal_Shdr section;
10850 section.sh_offset = (vma - seg->p_vaddr
10851 + seg->p_offset);
10852 section.sh_size = (num_of_syms
10853 * filedata->dynamic_info[DT_SYMENT]);
10854 section.sh_entsize = filedata->dynamic_info[DT_SYMENT];
10855
10856 if (do_checks
10857 && filedata->dynamic_symtab_section != NULL
10858 && ((filedata->dynamic_symtab_section->sh_offset
10859 != section.sh_offset)
10860 || (filedata->dynamic_symtab_section->sh_size
10861 != section.sh_size)
10862 || (filedata->dynamic_symtab_section->sh_entsize
10863 != section.sh_entsize)))
10864 warn (_("\
10865 the .dynsym section doesn't match the DT_SYMTAB and DT_SYMENT tags\n"));
10866
10867 section.sh_name = filedata->string_table_length;
10868 filedata->dynamic_symbols
10869 = get_elf_symbols (filedata, &section,
10870 &filedata->num_dynamic_syms);
10871 if (filedata->dynamic_symbols == NULL
10872 || filedata->num_dynamic_syms != num_of_syms)
10873 {
10874 error (_("Corrupt DT_SYMTAB dynamic entry\n"));
10875 return false;
10876 }
10877 break;
10878 }
10879 }
10880 }
10881 }
10882
10883 /* Similarly find a string table. */
10884 if (filedata->dynamic_strings == NULL)
10885 for (entry = filedata->dynamic_section;
10886 entry < filedata->dynamic_section + filedata->dynamic_nent;
10887 ++entry)
10888 {
10889 if (entry->d_tag == DT_STRTAB)
10890 filedata->dynamic_info[DT_STRTAB] = entry->d_un.d_val;
10891
10892 if (entry->d_tag == DT_STRSZ)
10893 filedata->dynamic_info[DT_STRSZ] = entry->d_un.d_val;
10894
10895 if (filedata->dynamic_info[DT_STRTAB]
10896 && filedata->dynamic_info[DT_STRSZ])
10897 {
10898 unsigned long offset;
10899 bfd_size_type str_tab_len = filedata->dynamic_info[DT_STRSZ];
10900
10901 offset = offset_from_vma (filedata,
10902 filedata->dynamic_info[DT_STRTAB],
10903 str_tab_len);
10904 if (do_checks
10905 && filedata->dynamic_strtab_section
10906 && ((filedata->dynamic_strtab_section->sh_offset
10907 != (file_ptr) offset)
10908 || (filedata->dynamic_strtab_section->sh_size
10909 != str_tab_len)))
10910 warn (_("\
10911 the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
10912
10913 filedata->dynamic_strings
10914 = (char *) get_data (NULL, filedata, offset, 1, str_tab_len,
10915 _("dynamic string table"));
10916 if (filedata->dynamic_strings == NULL)
10917 {
10918 error (_("Corrupt DT_STRTAB dynamic entry\n"));
10919 break;
10920 }
10921
10922 filedata->dynamic_strings_length = str_tab_len;
10923 break;
10924 }
10925 }
10926
10927 /* And find the syminfo section if available. */
10928 if (filedata->dynamic_syminfo == NULL)
10929 {
10930 unsigned long syminsz = 0;
10931
10932 for (entry = filedata->dynamic_section;
10933 entry < filedata->dynamic_section + filedata->dynamic_nent;
10934 ++entry)
10935 {
10936 if (entry->d_tag == DT_SYMINENT)
10937 {
10938 /* Note: these braces are necessary to avoid a syntax
10939 error from the SunOS4 C compiler. */
10940 /* PR binutils/17531: A corrupt file can trigger this test.
10941 So do not use an assert, instead generate an error message. */
10942 if (sizeof (Elf_External_Syminfo) != entry->d_un.d_val)
10943 error (_("Bad value (%d) for SYMINENT entry\n"),
10944 (int) entry->d_un.d_val);
10945 }
10946 else if (entry->d_tag == DT_SYMINSZ)
10947 syminsz = entry->d_un.d_val;
10948 else if (entry->d_tag == DT_SYMINFO)
10949 filedata->dynamic_syminfo_offset
10950 = offset_from_vma (filedata, entry->d_un.d_val, syminsz);
10951 }
10952
10953 if (filedata->dynamic_syminfo_offset != 0 && syminsz != 0)
10954 {
10955 Elf_External_Syminfo * extsyminfo;
10956 Elf_External_Syminfo * extsym;
10957 Elf_Internal_Syminfo * syminfo;
10958
10959 /* There is a syminfo section. Read the data. */
10960 extsyminfo = (Elf_External_Syminfo *)
10961 get_data (NULL, filedata, filedata->dynamic_syminfo_offset,
10962 1, syminsz, _("symbol information"));
10963 if (!extsyminfo)
10964 return false;
10965
10966 if (filedata->dynamic_syminfo != NULL)
10967 {
10968 error (_("Multiple dynamic symbol information sections found\n"));
10969 free (filedata->dynamic_syminfo);
10970 }
10971 filedata->dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
10972 if (filedata->dynamic_syminfo == NULL)
10973 {
10974 error (_("Out of memory allocating %lu bytes "
10975 "for dynamic symbol info\n"),
10976 (unsigned long) syminsz);
10977 return false;
10978 }
10979
10980 filedata->dynamic_syminfo_nent
10981 = syminsz / sizeof (Elf_External_Syminfo);
10982 for (syminfo = filedata->dynamic_syminfo, extsym = extsyminfo;
10983 syminfo < (filedata->dynamic_syminfo
10984 + filedata->dynamic_syminfo_nent);
10985 ++syminfo, ++extsym)
10986 {
10987 syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
10988 syminfo->si_flags = BYTE_GET (extsym->si_flags);
10989 }
10990
10991 free (extsyminfo);
10992 }
10993 }
10994
10995 if (do_dynamic && filedata->dynamic_addr)
10996 {
10997 if (filedata->is_separate)
10998 printf (ngettext ("\nIn linked file '%s' the dynamic section at offset 0x%lx contains %lu entry:\n",
10999 "\nIn linked file '%s' the dynamic section at offset 0x%lx contains %lu entries:\n",
11000 (unsigned long) filedata->dynamic_nent),
11001 filedata->file_name,
11002 filedata->dynamic_addr,
11003 (unsigned long) filedata->dynamic_nent);
11004 else
11005 printf (ngettext ("\nDynamic section at offset 0x%lx contains %lu entry:\n",
11006 "\nDynamic section at offset 0x%lx contains %lu entries:\n",
11007 (unsigned long) filedata->dynamic_nent),
11008 filedata->dynamic_addr,
11009 (unsigned long) filedata->dynamic_nent);
11010 }
11011 if (do_dynamic)
11012 printf (_(" Tag Type Name/Value\n"));
11013
11014 for (entry = filedata->dynamic_section;
11015 entry < filedata->dynamic_section + filedata->dynamic_nent;
11016 entry++)
11017 {
11018 if (do_dynamic)
11019 {
11020 const char * dtype;
11021
11022 putchar (' ');
11023 print_vma (entry->d_tag, FULL_HEX);
11024 dtype = get_dynamic_type (filedata, entry->d_tag);
11025 printf (" (%s)%*s", dtype,
11026 ((is_32bit_elf ? 27 : 19) - (int) strlen (dtype)), " ");
11027 }
11028
11029 switch (entry->d_tag)
11030 {
11031 case DT_FLAGS:
11032 if (do_dynamic)
11033 print_dynamic_flags (entry->d_un.d_val);
11034 break;
11035
11036 case DT_AUXILIARY:
11037 case DT_FILTER:
11038 case DT_CONFIG:
11039 case DT_DEPAUDIT:
11040 case DT_AUDIT:
11041 if (do_dynamic)
11042 {
11043 switch (entry->d_tag)
11044 {
11045 case DT_AUXILIARY:
11046 printf (_("Auxiliary library"));
11047 break;
11048
11049 case DT_FILTER:
11050 printf (_("Filter library"));
11051 break;
11052
11053 case DT_CONFIG:
11054 printf (_("Configuration file"));
11055 break;
11056
11057 case DT_DEPAUDIT:
11058 printf (_("Dependency audit library"));
11059 break;
11060
11061 case DT_AUDIT:
11062 printf (_("Audit library"));
11063 break;
11064 }
11065
11066 if (valid_dynamic_name (filedata, entry->d_un.d_val))
11067 printf (": [%s]\n",
11068 get_dynamic_name (filedata, entry->d_un.d_val));
11069 else
11070 {
11071 printf (": ");
11072 print_vma (entry->d_un.d_val, PREFIX_HEX);
11073 putchar ('\n');
11074 }
11075 }
11076 break;
11077
11078 case DT_FEATURE:
11079 if (do_dynamic)
11080 {
11081 printf (_("Flags:"));
11082
11083 if (entry->d_un.d_val == 0)
11084 printf (_(" None\n"));
11085 else
11086 {
11087 unsigned long int val = entry->d_un.d_val;
11088
11089 if (val & DTF_1_PARINIT)
11090 {
11091 printf (" PARINIT");
11092 val ^= DTF_1_PARINIT;
11093 }
11094 if (val & DTF_1_CONFEXP)
11095 {
11096 printf (" CONFEXP");
11097 val ^= DTF_1_CONFEXP;
11098 }
11099 if (val != 0)
11100 printf (" %lx", val);
11101 puts ("");
11102 }
11103 }
11104 break;
11105
11106 case DT_POSFLAG_1:
11107 if (do_dynamic)
11108 {
11109 printf (_("Flags:"));
11110
11111 if (entry->d_un.d_val == 0)
11112 printf (_(" None\n"));
11113 else
11114 {
11115 unsigned long int val = entry->d_un.d_val;
11116
11117 if (val & DF_P1_LAZYLOAD)
11118 {
11119 printf (" LAZYLOAD");
11120 val ^= DF_P1_LAZYLOAD;
11121 }
11122 if (val & DF_P1_GROUPPERM)
11123 {
11124 printf (" GROUPPERM");
11125 val ^= DF_P1_GROUPPERM;
11126 }
11127 if (val != 0)
11128 printf (" %lx", val);
11129 puts ("");
11130 }
11131 }
11132 break;
11133
11134 case DT_FLAGS_1:
11135 if (do_dynamic)
11136 {
11137 printf (_("Flags:"));
11138 if (entry->d_un.d_val == 0)
11139 printf (_(" None\n"));
11140 else
11141 {
11142 unsigned long int val = entry->d_un.d_val;
11143
11144 if (val & DF_1_NOW)
11145 {
11146 printf (" NOW");
11147 val ^= DF_1_NOW;
11148 }
11149 if (val & DF_1_GLOBAL)
11150 {
11151 printf (" GLOBAL");
11152 val ^= DF_1_GLOBAL;
11153 }
11154 if (val & DF_1_GROUP)
11155 {
11156 printf (" GROUP");
11157 val ^= DF_1_GROUP;
11158 }
11159 if (val & DF_1_NODELETE)
11160 {
11161 printf (" NODELETE");
11162 val ^= DF_1_NODELETE;
11163 }
11164 if (val & DF_1_LOADFLTR)
11165 {
11166 printf (" LOADFLTR");
11167 val ^= DF_1_LOADFLTR;
11168 }
11169 if (val & DF_1_INITFIRST)
11170 {
11171 printf (" INITFIRST");
11172 val ^= DF_1_INITFIRST;
11173 }
11174 if (val & DF_1_NOOPEN)
11175 {
11176 printf (" NOOPEN");
11177 val ^= DF_1_NOOPEN;
11178 }
11179 if (val & DF_1_ORIGIN)
11180 {
11181 printf (" ORIGIN");
11182 val ^= DF_1_ORIGIN;
11183 }
11184 if (val & DF_1_DIRECT)
11185 {
11186 printf (" DIRECT");
11187 val ^= DF_1_DIRECT;
11188 }
11189 if (val & DF_1_TRANS)
11190 {
11191 printf (" TRANS");
11192 val ^= DF_1_TRANS;
11193 }
11194 if (val & DF_1_INTERPOSE)
11195 {
11196 printf (" INTERPOSE");
11197 val ^= DF_1_INTERPOSE;
11198 }
11199 if (val & DF_1_NODEFLIB)
11200 {
11201 printf (" NODEFLIB");
11202 val ^= DF_1_NODEFLIB;
11203 }
11204 if (val & DF_1_NODUMP)
11205 {
11206 printf (" NODUMP");
11207 val ^= DF_1_NODUMP;
11208 }
11209 if (val & DF_1_CONFALT)
11210 {
11211 printf (" CONFALT");
11212 val ^= DF_1_CONFALT;
11213 }
11214 if (val & DF_1_ENDFILTEE)
11215 {
11216 printf (" ENDFILTEE");
11217 val ^= DF_1_ENDFILTEE;
11218 }
11219 if (val & DF_1_DISPRELDNE)
11220 {
11221 printf (" DISPRELDNE");
11222 val ^= DF_1_DISPRELDNE;
11223 }
11224 if (val & DF_1_DISPRELPND)
11225 {
11226 printf (" DISPRELPND");
11227 val ^= DF_1_DISPRELPND;
11228 }
11229 if (val & DF_1_NODIRECT)
11230 {
11231 printf (" NODIRECT");
11232 val ^= DF_1_NODIRECT;
11233 }
11234 if (val & DF_1_IGNMULDEF)
11235 {
11236 printf (" IGNMULDEF");
11237 val ^= DF_1_IGNMULDEF;
11238 }
11239 if (val & DF_1_NOKSYMS)
11240 {
11241 printf (" NOKSYMS");
11242 val ^= DF_1_NOKSYMS;
11243 }
11244 if (val & DF_1_NOHDR)
11245 {
11246 printf (" NOHDR");
11247 val ^= DF_1_NOHDR;
11248 }
11249 if (val & DF_1_EDITED)
11250 {
11251 printf (" EDITED");
11252 val ^= DF_1_EDITED;
11253 }
11254 if (val & DF_1_NORELOC)
11255 {
11256 printf (" NORELOC");
11257 val ^= DF_1_NORELOC;
11258 }
11259 if (val & DF_1_SYMINTPOSE)
11260 {
11261 printf (" SYMINTPOSE");
11262 val ^= DF_1_SYMINTPOSE;
11263 }
11264 if (val & DF_1_GLOBAUDIT)
11265 {
11266 printf (" GLOBAUDIT");
11267 val ^= DF_1_GLOBAUDIT;
11268 }
11269 if (val & DF_1_SINGLETON)
11270 {
11271 printf (" SINGLETON");
11272 val ^= DF_1_SINGLETON;
11273 }
11274 if (val & DF_1_STUB)
11275 {
11276 printf (" STUB");
11277 val ^= DF_1_STUB;
11278 }
11279 if (val & DF_1_PIE)
11280 {
11281 printf (" PIE");
11282 val ^= DF_1_PIE;
11283 }
11284 if (val & DF_1_KMOD)
11285 {
11286 printf (" KMOD");
11287 val ^= DF_1_KMOD;
11288 }
11289 if (val & DF_1_WEAKFILTER)
11290 {
11291 printf (" WEAKFILTER");
11292 val ^= DF_1_WEAKFILTER;
11293 }
11294 if (val & DF_1_NOCOMMON)
11295 {
11296 printf (" NOCOMMON");
11297 val ^= DF_1_NOCOMMON;
11298 }
11299 if (val != 0)
11300 printf (" %lx", val);
11301 puts ("");
11302 }
11303 }
11304 break;
11305
11306 case DT_PLTREL:
11307 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11308 if (do_dynamic)
11309 puts (get_dynamic_type (filedata, entry->d_un.d_val));
11310 break;
11311
11312 case DT_NULL :
11313 case DT_NEEDED :
11314 case DT_PLTGOT :
11315 case DT_HASH :
11316 case DT_STRTAB :
11317 case DT_SYMTAB :
11318 case DT_RELA :
11319 case DT_INIT :
11320 case DT_FINI :
11321 case DT_SONAME :
11322 case DT_RPATH :
11323 case DT_SYMBOLIC:
11324 case DT_REL :
11325 case DT_DEBUG :
11326 case DT_TEXTREL :
11327 case DT_JMPREL :
11328 case DT_RUNPATH :
11329 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11330
11331 if (do_dynamic)
11332 {
11333 const char *name;
11334
11335 if (valid_dynamic_name (filedata, entry->d_un.d_val))
11336 name = get_dynamic_name (filedata, entry->d_un.d_val);
11337 else
11338 name = NULL;
11339
11340 if (name)
11341 {
11342 switch (entry->d_tag)
11343 {
11344 case DT_NEEDED:
11345 printf (_("Shared library: [%s]"), name);
11346
11347 if (filedata->program_interpreter
11348 && streq (name, filedata->program_interpreter))
11349 printf (_(" program interpreter"));
11350 break;
11351
11352 case DT_SONAME:
11353 printf (_("Library soname: [%s]"), name);
11354 break;
11355
11356 case DT_RPATH:
11357 printf (_("Library rpath: [%s]"), name);
11358 break;
11359
11360 case DT_RUNPATH:
11361 printf (_("Library runpath: [%s]"), name);
11362 break;
11363
11364 default:
11365 print_vma (entry->d_un.d_val, PREFIX_HEX);
11366 break;
11367 }
11368 }
11369 else
11370 print_vma (entry->d_un.d_val, PREFIX_HEX);
11371
11372 putchar ('\n');
11373 }
11374 break;
11375
11376 case DT_PLTRELSZ:
11377 case DT_RELASZ :
11378 case DT_STRSZ :
11379 case DT_RELSZ :
11380 case DT_RELAENT :
11381 case DT_SYMENT :
11382 case DT_RELENT :
11383 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11384 /* Fall through. */
11385 case DT_PLTPADSZ:
11386 case DT_MOVEENT :
11387 case DT_MOVESZ :
11388 case DT_INIT_ARRAYSZ:
11389 case DT_FINI_ARRAYSZ:
11390 case DT_GNU_CONFLICTSZ:
11391 case DT_GNU_LIBLISTSZ:
11392 if (do_dynamic)
11393 {
11394 print_vma (entry->d_un.d_val, UNSIGNED);
11395 printf (_(" (bytes)\n"));
11396 }
11397 break;
11398
11399 case DT_VERDEFNUM:
11400 case DT_VERNEEDNUM:
11401 case DT_RELACOUNT:
11402 case DT_RELCOUNT:
11403 if (do_dynamic)
11404 {
11405 print_vma (entry->d_un.d_val, UNSIGNED);
11406 putchar ('\n');
11407 }
11408 break;
11409
11410 case DT_SYMINSZ:
11411 case DT_SYMINENT:
11412 case DT_SYMINFO:
11413 case DT_USED:
11414 case DT_INIT_ARRAY:
11415 case DT_FINI_ARRAY:
11416 if (do_dynamic)
11417 {
11418 if (entry->d_tag == DT_USED
11419 && valid_dynamic_name (filedata, entry->d_un.d_val))
11420 {
11421 const char *name
11422 = get_dynamic_name (filedata, entry->d_un.d_val);
11423
11424 if (*name)
11425 {
11426 printf (_("Not needed object: [%s]\n"), name);
11427 break;
11428 }
11429 }
11430
11431 print_vma (entry->d_un.d_val, PREFIX_HEX);
11432 putchar ('\n');
11433 }
11434 break;
11435
11436 case DT_BIND_NOW:
11437 /* The value of this entry is ignored. */
11438 if (do_dynamic)
11439 putchar ('\n');
11440 break;
11441
11442 case DT_GNU_PRELINKED:
11443 if (do_dynamic)
11444 {
11445 struct tm * tmp;
11446 time_t atime = entry->d_un.d_val;
11447
11448 tmp = gmtime (&atime);
11449 /* PR 17533 file: 041-1244816-0.004. */
11450 if (tmp == NULL)
11451 printf (_("<corrupt time val: %lx"),
11452 (unsigned long) atime);
11453 else
11454 printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
11455 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
11456 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
11457
11458 }
11459 break;
11460
11461 case DT_GNU_HASH:
11462 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11463 if (do_dynamic)
11464 {
11465 print_vma (entry->d_un.d_val, PREFIX_HEX);
11466 putchar ('\n');
11467 }
11468 break;
11469
11470 case DT_GNU_FLAGS_1:
11471 if (do_dynamic)
11472 {
11473 printf (_("Flags:"));
11474 if (entry->d_un.d_val == 0)
11475 printf (_(" None\n"));
11476 else
11477 {
11478 unsigned long int val = entry->d_un.d_val;
11479
11480 if (val & DF_GNU_1_UNIQUE)
11481 {
11482 printf (" UNIQUE");
11483 val ^= DF_GNU_1_UNIQUE;
11484 }
11485 if (val != 0)
11486 printf (" %lx", val);
11487 puts ("");
11488 }
11489 }
11490 break;
11491
11492 default:
11493 if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
11494 filedata->version_info[DT_VERSIONTAGIDX (entry->d_tag)]
11495 = entry->d_un.d_val;
11496
11497 if (do_dynamic)
11498 {
11499 switch (filedata->file_header.e_machine)
11500 {
11501 case EM_AARCH64:
11502 dynamic_section_aarch64_val (entry);
11503 break;
11504 case EM_MIPS:
11505 case EM_MIPS_RS3_LE:
11506 dynamic_section_mips_val (filedata, entry);
11507 break;
11508 case EM_PARISC:
11509 dynamic_section_parisc_val (entry);
11510 break;
11511 case EM_IA_64:
11512 dynamic_section_ia64_val (entry);
11513 break;
11514 default:
11515 print_vma (entry->d_un.d_val, PREFIX_HEX);
11516 putchar ('\n');
11517 }
11518 }
11519 break;
11520 }
11521 }
11522
11523 return true;
11524 }
11525
11526 static char *
11527 get_ver_flags (unsigned int flags)
11528 {
11529 static char buff[128];
11530
11531 buff[0] = 0;
11532
11533 if (flags == 0)
11534 return _("none");
11535
11536 if (flags & VER_FLG_BASE)
11537 strcat (buff, "BASE");
11538
11539 if (flags & VER_FLG_WEAK)
11540 {
11541 if (flags & VER_FLG_BASE)
11542 strcat (buff, " | ");
11543
11544 strcat (buff, "WEAK");
11545 }
11546
11547 if (flags & VER_FLG_INFO)
11548 {
11549 if (flags & (VER_FLG_BASE|VER_FLG_WEAK))
11550 strcat (buff, " | ");
11551
11552 strcat (buff, "INFO");
11553 }
11554
11555 if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
11556 {
11557 if (flags & (VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
11558 strcat (buff, " | ");
11559
11560 strcat (buff, _("<unknown>"));
11561 }
11562
11563 return buff;
11564 }
11565
11566 /* Display the contents of the version sections. */
11567
11568 static bool
11569 process_version_sections (Filedata * filedata)
11570 {
11571 Elf_Internal_Shdr * section;
11572 unsigned i;
11573 bool found = false;
11574
11575 if (! do_version)
11576 return true;
11577
11578 for (i = 0, section = filedata->section_headers;
11579 i < filedata->file_header.e_shnum;
11580 i++, section++)
11581 {
11582 switch (section->sh_type)
11583 {
11584 case SHT_GNU_verdef:
11585 {
11586 Elf_External_Verdef * edefs;
11587 unsigned long idx;
11588 unsigned long cnt;
11589 char * endbuf;
11590
11591 found = true;
11592
11593 if (filedata->is_separate)
11594 printf (ngettext ("\nIn linked file '%s' the version definition section '%s' contains %u entry:\n",
11595 "\nIn linked file '%s' the version definition section '%s' contains %u entries:\n",
11596 section->sh_info),
11597 filedata->file_name,
11598 printable_section_name (filedata, section),
11599 section->sh_info);
11600 else
11601 printf (ngettext ("\nVersion definition section '%s' "
11602 "contains %u entry:\n",
11603 "\nVersion definition section '%s' "
11604 "contains %u entries:\n",
11605 section->sh_info),
11606 printable_section_name (filedata, section),
11607 section->sh_info);
11608
11609 printf (_(" Addr: 0x"));
11610 printf_vma (section->sh_addr);
11611 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
11612 (unsigned long) section->sh_offset, section->sh_link,
11613 printable_section_name_from_index (filedata, section->sh_link));
11614
11615 edefs = (Elf_External_Verdef *)
11616 get_data (NULL, filedata, section->sh_offset, 1,section->sh_size,
11617 _("version definition section"));
11618 if (!edefs)
11619 break;
11620 endbuf = (char *) edefs + section->sh_size;
11621
11622 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
11623 {
11624 char * vstart;
11625 Elf_External_Verdef * edef;
11626 Elf_Internal_Verdef ent;
11627 Elf_External_Verdaux * eaux;
11628 Elf_Internal_Verdaux aux;
11629 unsigned long isum;
11630 int j;
11631
11632 vstart = ((char *) edefs) + idx;
11633 if (vstart + sizeof (*edef) > endbuf)
11634 break;
11635
11636 edef = (Elf_External_Verdef *) vstart;
11637
11638 ent.vd_version = BYTE_GET (edef->vd_version);
11639 ent.vd_flags = BYTE_GET (edef->vd_flags);
11640 ent.vd_ndx = BYTE_GET (edef->vd_ndx);
11641 ent.vd_cnt = BYTE_GET (edef->vd_cnt);
11642 ent.vd_hash = BYTE_GET (edef->vd_hash);
11643 ent.vd_aux = BYTE_GET (edef->vd_aux);
11644 ent.vd_next = BYTE_GET (edef->vd_next);
11645
11646 printf (_(" %#06lx: Rev: %d Flags: %s"),
11647 idx, ent.vd_version, get_ver_flags (ent.vd_flags));
11648
11649 printf (_(" Index: %d Cnt: %d "),
11650 ent.vd_ndx, ent.vd_cnt);
11651
11652 /* Check for overflow. */
11653 if (ent.vd_aux > (size_t) (endbuf - vstart))
11654 break;
11655
11656 vstart += ent.vd_aux;
11657
11658 if (vstart + sizeof (*eaux) > endbuf)
11659 break;
11660 eaux = (Elf_External_Verdaux *) vstart;
11661
11662 aux.vda_name = BYTE_GET (eaux->vda_name);
11663 aux.vda_next = BYTE_GET (eaux->vda_next);
11664
11665 if (valid_dynamic_name (filedata, aux.vda_name))
11666 printf (_("Name: %s\n"),
11667 get_dynamic_name (filedata, aux.vda_name));
11668 else
11669 printf (_("Name index: %ld\n"), aux.vda_name);
11670
11671 isum = idx + ent.vd_aux;
11672
11673 for (j = 1; j < ent.vd_cnt; j++)
11674 {
11675 if (aux.vda_next < sizeof (*eaux)
11676 && !(j == ent.vd_cnt - 1 && aux.vda_next == 0))
11677 {
11678 warn (_("Invalid vda_next field of %lx\n"),
11679 aux.vda_next);
11680 j = ent.vd_cnt;
11681 break;
11682 }
11683 /* Check for overflow. */
11684 if (aux.vda_next > (size_t) (endbuf - vstart))
11685 break;
11686
11687 isum += aux.vda_next;
11688 vstart += aux.vda_next;
11689
11690 if (vstart + sizeof (*eaux) > endbuf)
11691 break;
11692 eaux = (Elf_External_Verdaux *) vstart;
11693
11694 aux.vda_name = BYTE_GET (eaux->vda_name);
11695 aux.vda_next = BYTE_GET (eaux->vda_next);
11696
11697 if (valid_dynamic_name (filedata, aux.vda_name))
11698 printf (_(" %#06lx: Parent %d: %s\n"),
11699 isum, j,
11700 get_dynamic_name (filedata, aux.vda_name));
11701 else
11702 printf (_(" %#06lx: Parent %d, name index: %ld\n"),
11703 isum, j, aux.vda_name);
11704 }
11705
11706 if (j < ent.vd_cnt)
11707 printf (_(" Version def aux past end of section\n"));
11708
11709 /* PR 17531:
11710 file: id:000001,src:000172+005151,op:splice,rep:2. */
11711 if (ent.vd_next < sizeof (*edef)
11712 && !(cnt == section->sh_info - 1 && ent.vd_next == 0))
11713 {
11714 warn (_("Invalid vd_next field of %lx\n"), ent.vd_next);
11715 cnt = section->sh_info;
11716 break;
11717 }
11718 if (ent.vd_next > (size_t) (endbuf - ((char *) edefs + idx)))
11719 break;
11720
11721 idx += ent.vd_next;
11722 }
11723
11724 if (cnt < section->sh_info)
11725 printf (_(" Version definition past end of section\n"));
11726
11727 free (edefs);
11728 }
11729 break;
11730
11731 case SHT_GNU_verneed:
11732 {
11733 Elf_External_Verneed * eneed;
11734 unsigned long idx;
11735 unsigned long cnt;
11736 char * endbuf;
11737
11738 found = true;
11739
11740 if (filedata->is_separate)
11741 printf (ngettext ("\nIn linked file '%s' the version needs section '%s' contains %u entry:\n",
11742 "\nIn linked file '%s' the version needs section '%s' contains %u entries:\n",
11743 section->sh_info),
11744 filedata->file_name,
11745 printable_section_name (filedata, section),
11746 section->sh_info);
11747 else
11748 printf (ngettext ("\nVersion needs section '%s' "
11749 "contains %u entry:\n",
11750 "\nVersion needs section '%s' "
11751 "contains %u entries:\n",
11752 section->sh_info),
11753 printable_section_name (filedata, section),
11754 section->sh_info);
11755
11756 printf (_(" Addr: 0x"));
11757 printf_vma (section->sh_addr);
11758 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
11759 (unsigned long) section->sh_offset, section->sh_link,
11760 printable_section_name_from_index (filedata, section->sh_link));
11761
11762 eneed = (Elf_External_Verneed *) get_data (NULL, filedata,
11763 section->sh_offset, 1,
11764 section->sh_size,
11765 _("Version Needs section"));
11766 if (!eneed)
11767 break;
11768 endbuf = (char *) eneed + section->sh_size;
11769
11770 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
11771 {
11772 Elf_External_Verneed * entry;
11773 Elf_Internal_Verneed ent;
11774 unsigned long isum;
11775 int j;
11776 char * vstart;
11777
11778 vstart = ((char *) eneed) + idx;
11779 if (vstart + sizeof (*entry) > endbuf)
11780 break;
11781
11782 entry = (Elf_External_Verneed *) vstart;
11783
11784 ent.vn_version = BYTE_GET (entry->vn_version);
11785 ent.vn_cnt = BYTE_GET (entry->vn_cnt);
11786 ent.vn_file = BYTE_GET (entry->vn_file);
11787 ent.vn_aux = BYTE_GET (entry->vn_aux);
11788 ent.vn_next = BYTE_GET (entry->vn_next);
11789
11790 printf (_(" %#06lx: Version: %d"), idx, ent.vn_version);
11791
11792 if (valid_dynamic_name (filedata, ent.vn_file))
11793 printf (_(" File: %s"),
11794 get_dynamic_name (filedata, ent.vn_file));
11795 else
11796 printf (_(" File: %lx"), ent.vn_file);
11797
11798 printf (_(" Cnt: %d\n"), ent.vn_cnt);
11799
11800 /* Check for overflow. */
11801 if (ent.vn_aux > (size_t) (endbuf - vstart))
11802 break;
11803 vstart += ent.vn_aux;
11804
11805 for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
11806 {
11807 Elf_External_Vernaux * eaux;
11808 Elf_Internal_Vernaux aux;
11809
11810 if (vstart + sizeof (*eaux) > endbuf)
11811 break;
11812 eaux = (Elf_External_Vernaux *) vstart;
11813
11814 aux.vna_hash = BYTE_GET (eaux->vna_hash);
11815 aux.vna_flags = BYTE_GET (eaux->vna_flags);
11816 aux.vna_other = BYTE_GET (eaux->vna_other);
11817 aux.vna_name = BYTE_GET (eaux->vna_name);
11818 aux.vna_next = BYTE_GET (eaux->vna_next);
11819
11820 if (valid_dynamic_name (filedata, aux.vna_name))
11821 printf (_(" %#06lx: Name: %s"),
11822 isum, get_dynamic_name (filedata, aux.vna_name));
11823 else
11824 printf (_(" %#06lx: Name index: %lx"),
11825 isum, aux.vna_name);
11826
11827 printf (_(" Flags: %s Version: %d\n"),
11828 get_ver_flags (aux.vna_flags), aux.vna_other);
11829
11830 if (aux.vna_next < sizeof (*eaux)
11831 && !(j == ent.vn_cnt - 1 && aux.vna_next == 0))
11832 {
11833 warn (_("Invalid vna_next field of %lx\n"),
11834 aux.vna_next);
11835 j = ent.vn_cnt;
11836 break;
11837 }
11838 /* Check for overflow. */
11839 if (aux.vna_next > (size_t) (endbuf - vstart))
11840 break;
11841 isum += aux.vna_next;
11842 vstart += aux.vna_next;
11843 }
11844
11845 if (j < ent.vn_cnt)
11846 warn (_("Missing Version Needs auxiliary information\n"));
11847
11848 if (ent.vn_next < sizeof (*entry)
11849 && !(cnt == section->sh_info - 1 && ent.vn_next == 0))
11850 {
11851 warn (_("Invalid vn_next field of %lx\n"), ent.vn_next);
11852 cnt = section->sh_info;
11853 break;
11854 }
11855 if (ent.vn_next > (size_t) (endbuf - ((char *) eneed + idx)))
11856 break;
11857 idx += ent.vn_next;
11858 }
11859
11860 if (cnt < section->sh_info)
11861 warn (_("Missing Version Needs information\n"));
11862
11863 free (eneed);
11864 }
11865 break;
11866
11867 case SHT_GNU_versym:
11868 {
11869 Elf_Internal_Shdr * link_section;
11870 size_t total;
11871 unsigned int cnt;
11872 unsigned char * edata;
11873 unsigned short * data;
11874 char * strtab;
11875 Elf_Internal_Sym * symbols;
11876 Elf_Internal_Shdr * string_sec;
11877 unsigned long num_syms;
11878 long off;
11879
11880 if (section->sh_link >= filedata->file_header.e_shnum)
11881 break;
11882
11883 link_section = filedata->section_headers + section->sh_link;
11884 total = section->sh_size / sizeof (Elf_External_Versym);
11885
11886 if (link_section->sh_link >= filedata->file_header.e_shnum)
11887 break;
11888
11889 found = true;
11890
11891 symbols = get_elf_symbols (filedata, link_section, & num_syms);
11892 if (symbols == NULL)
11893 break;
11894
11895 string_sec = filedata->section_headers + link_section->sh_link;
11896
11897 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
11898 string_sec->sh_size,
11899 _("version string table"));
11900 if (!strtab)
11901 {
11902 free (symbols);
11903 break;
11904 }
11905
11906 if (filedata->is_separate)
11907 printf (ngettext ("\nIn linked file '%s' the version symbols section '%s' contains %lu entry:\n",
11908 "\nIn linked file '%s' the version symbols section '%s' contains %lu entries:\n",
11909 total),
11910 filedata->file_name,
11911 printable_section_name (filedata, section),
11912 (unsigned long) total);
11913 else
11914 printf (ngettext ("\nVersion symbols section '%s' "
11915 "contains %lu entry:\n",
11916 "\nVersion symbols section '%s' "
11917 "contains %lu entries:\n",
11918 total),
11919 printable_section_name (filedata, section),
11920 (unsigned long) total);
11921
11922 printf (_(" Addr: 0x"));
11923 printf_vma (section->sh_addr);
11924 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
11925 (unsigned long) section->sh_offset, section->sh_link,
11926 printable_section_name (filedata, link_section));
11927
11928 off = offset_from_vma (filedata,
11929 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
11930 total * sizeof (short));
11931 edata = (unsigned char *) get_data (NULL, filedata, off,
11932 sizeof (short), total,
11933 _("version symbol data"));
11934 if (!edata)
11935 {
11936 free (strtab);
11937 free (symbols);
11938 break;
11939 }
11940
11941 data = (short unsigned int *) cmalloc (total, sizeof (short));
11942
11943 for (cnt = total; cnt --;)
11944 data[cnt] = byte_get (edata + cnt * sizeof (short),
11945 sizeof (short));
11946
11947 free (edata);
11948
11949 for (cnt = 0; cnt < total; cnt += 4)
11950 {
11951 int j, nn;
11952 char *name;
11953 char *invalid = _("*invalid*");
11954
11955 printf (" %03x:", cnt);
11956
11957 for (j = 0; (j < 4) && (cnt + j) < total; ++j)
11958 switch (data[cnt + j])
11959 {
11960 case 0:
11961 fputs (_(" 0 (*local*) "), stdout);
11962 break;
11963
11964 case 1:
11965 fputs (_(" 1 (*global*) "), stdout);
11966 break;
11967
11968 default:
11969 nn = printf ("%4x%c", data[cnt + j] & VERSYM_VERSION,
11970 data[cnt + j] & VERSYM_HIDDEN ? 'h' : ' ');
11971
11972 /* If this index value is greater than the size of the symbols
11973 array, break to avoid an out-of-bounds read. */
11974 if ((unsigned long)(cnt + j) >= num_syms)
11975 {
11976 warn (_("invalid index into symbol array\n"));
11977 break;
11978 }
11979
11980 name = NULL;
11981 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
11982 {
11983 Elf_Internal_Verneed ivn;
11984 unsigned long offset;
11985
11986 offset = offset_from_vma
11987 (filedata,
11988 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
11989 sizeof (Elf_External_Verneed));
11990
11991 do
11992 {
11993 Elf_Internal_Vernaux ivna;
11994 Elf_External_Verneed evn;
11995 Elf_External_Vernaux evna;
11996 unsigned long a_off;
11997
11998 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
11999 _("version need")) == NULL)
12000 break;
12001
12002 ivn.vn_aux = BYTE_GET (evn.vn_aux);
12003 ivn.vn_next = BYTE_GET (evn.vn_next);
12004
12005 a_off = offset + ivn.vn_aux;
12006
12007 do
12008 {
12009 if (get_data (&evna, filedata, a_off, sizeof (evna),
12010 1, _("version need aux (2)")) == NULL)
12011 {
12012 ivna.vna_next = 0;
12013 ivna.vna_other = 0;
12014 }
12015 else
12016 {
12017 ivna.vna_next = BYTE_GET (evna.vna_next);
12018 ivna.vna_other = BYTE_GET (evna.vna_other);
12019 }
12020
12021 a_off += ivna.vna_next;
12022 }
12023 while (ivna.vna_other != data[cnt + j]
12024 && ivna.vna_next != 0);
12025
12026 if (ivna.vna_other == data[cnt + j])
12027 {
12028 ivna.vna_name = BYTE_GET (evna.vna_name);
12029
12030 if (ivna.vna_name >= string_sec->sh_size)
12031 name = invalid;
12032 else
12033 name = strtab + ivna.vna_name;
12034 break;
12035 }
12036
12037 offset += ivn.vn_next;
12038 }
12039 while (ivn.vn_next);
12040 }
12041
12042 if (data[cnt + j] != 0x8001
12043 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
12044 {
12045 Elf_Internal_Verdef ivd;
12046 Elf_External_Verdef evd;
12047 unsigned long offset;
12048
12049 offset = offset_from_vma
12050 (filedata,
12051 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
12052 sizeof evd);
12053
12054 do
12055 {
12056 if (get_data (&evd, filedata, offset, sizeof (evd), 1,
12057 _("version def")) == NULL)
12058 {
12059 ivd.vd_next = 0;
12060 /* PR 17531: file: 046-1082287-0.004. */
12061 ivd.vd_ndx = (data[cnt + j] & VERSYM_VERSION) + 1;
12062 break;
12063 }
12064 else
12065 {
12066 ivd.vd_next = BYTE_GET (evd.vd_next);
12067 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
12068 }
12069
12070 offset += ivd.vd_next;
12071 }
12072 while (ivd.vd_ndx != (data[cnt + j] & VERSYM_VERSION)
12073 && ivd.vd_next != 0);
12074
12075 if (ivd.vd_ndx == (data[cnt + j] & VERSYM_VERSION))
12076 {
12077 Elf_External_Verdaux evda;
12078 Elf_Internal_Verdaux ivda;
12079
12080 ivd.vd_aux = BYTE_GET (evd.vd_aux);
12081
12082 if (get_data (&evda, filedata,
12083 offset - ivd.vd_next + ivd.vd_aux,
12084 sizeof (evda), 1,
12085 _("version def aux")) == NULL)
12086 break;
12087
12088 ivda.vda_name = BYTE_GET (evda.vda_name);
12089
12090 if (ivda.vda_name >= string_sec->sh_size)
12091 name = invalid;
12092 else if (name != NULL && name != invalid)
12093 name = _("*both*");
12094 else
12095 name = strtab + ivda.vda_name;
12096 }
12097 }
12098 if (name != NULL)
12099 nn += printf ("(%s%-*s",
12100 name,
12101 12 - (int) strlen (name),
12102 ")");
12103
12104 if (nn < 18)
12105 printf ("%*c", 18 - nn, ' ');
12106 }
12107
12108 putchar ('\n');
12109 }
12110
12111 free (data);
12112 free (strtab);
12113 free (symbols);
12114 }
12115 break;
12116
12117 default:
12118 break;
12119 }
12120 }
12121
12122 if (! found)
12123 {
12124 if (filedata->is_separate)
12125 printf (_("\nNo version information found in linked file '%s'.\n"),
12126 filedata->file_name);
12127 else
12128 printf (_("\nNo version information found in this file.\n"));
12129 }
12130
12131 return true;
12132 }
12133
12134 static const char *
12135 get_symbol_binding (Filedata * filedata, unsigned int binding)
12136 {
12137 static char buff[64];
12138
12139 switch (binding)
12140 {
12141 case STB_LOCAL: return "LOCAL";
12142 case STB_GLOBAL: return "GLOBAL";
12143 case STB_WEAK: return "WEAK";
12144 default:
12145 if (binding >= STB_LOPROC && binding <= STB_HIPROC)
12146 snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
12147 binding);
12148 else if (binding >= STB_LOOS && binding <= STB_HIOS)
12149 {
12150 if (binding == STB_GNU_UNIQUE
12151 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU)
12152 return "UNIQUE";
12153 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
12154 }
12155 else
12156 snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
12157 return buff;
12158 }
12159 }
12160
12161 static const char *
12162 get_symbol_type (Filedata * filedata, unsigned int type)
12163 {
12164 static char buff[64];
12165
12166 switch (type)
12167 {
12168 case STT_NOTYPE: return "NOTYPE";
12169 case STT_OBJECT: return "OBJECT";
12170 case STT_FUNC: return "FUNC";
12171 case STT_SECTION: return "SECTION";
12172 case STT_FILE: return "FILE";
12173 case STT_COMMON: return "COMMON";
12174 case STT_TLS: return "TLS";
12175 case STT_RELC: return "RELC";
12176 case STT_SRELC: return "SRELC";
12177 default:
12178 if (type >= STT_LOPROC && type <= STT_HIPROC)
12179 {
12180 if (filedata->file_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
12181 return "THUMB_FUNC";
12182
12183 if (filedata->file_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
12184 return "REGISTER";
12185
12186 if (filedata->file_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
12187 return "PARISC_MILLI";
12188
12189 snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
12190 }
12191 else if (type >= STT_LOOS && type <= STT_HIOS)
12192 {
12193 if (filedata->file_header.e_machine == EM_PARISC)
12194 {
12195 if (type == STT_HP_OPAQUE)
12196 return "HP_OPAQUE";
12197 if (type == STT_HP_STUB)
12198 return "HP_STUB";
12199 }
12200
12201 if (type == STT_GNU_IFUNC
12202 && (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU
12203 || filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_FREEBSD))
12204 return "IFUNC";
12205
12206 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
12207 }
12208 else
12209 snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
12210 return buff;
12211 }
12212 }
12213
12214 static const char *
12215 get_symbol_visibility (unsigned int visibility)
12216 {
12217 switch (visibility)
12218 {
12219 case STV_DEFAULT: return "DEFAULT";
12220 case STV_INTERNAL: return "INTERNAL";
12221 case STV_HIDDEN: return "HIDDEN";
12222 case STV_PROTECTED: return "PROTECTED";
12223 default:
12224 error (_("Unrecognized visibility value: %u\n"), visibility);
12225 return _("<unknown>");
12226 }
12227 }
12228
12229 static const char *
12230 get_alpha_symbol_other (unsigned int other)
12231 {
12232 switch (other)
12233 {
12234 case STO_ALPHA_NOPV: return "NOPV";
12235 case STO_ALPHA_STD_GPLOAD: return "STD GPLOAD";
12236 default:
12237 error (_("Unrecognized alpha specific other value: %u\n"), other);
12238 return _("<unknown>");
12239 }
12240 }
12241
12242 static const char *
12243 get_solaris_symbol_visibility (unsigned int visibility)
12244 {
12245 switch (visibility)
12246 {
12247 case 4: return "EXPORTED";
12248 case 5: return "SINGLETON";
12249 case 6: return "ELIMINATE";
12250 default: return get_symbol_visibility (visibility);
12251 }
12252 }
12253
12254 static const char *
12255 get_aarch64_symbol_other (unsigned int other)
12256 {
12257 static char buf[32];
12258
12259 if (other & STO_AARCH64_VARIANT_PCS)
12260 {
12261 other &= ~STO_AARCH64_VARIANT_PCS;
12262 if (other == 0)
12263 return "VARIANT_PCS";
12264 snprintf (buf, sizeof buf, "VARIANT_PCS | %x", other);
12265 return buf;
12266 }
12267 return NULL;
12268 }
12269
12270 static const char *
12271 get_mips_symbol_other (unsigned int other)
12272 {
12273 switch (other)
12274 {
12275 case STO_OPTIONAL: return "OPTIONAL";
12276 case STO_MIPS_PLT: return "MIPS PLT";
12277 case STO_MIPS_PIC: return "MIPS PIC";
12278 case STO_MICROMIPS: return "MICROMIPS";
12279 case STO_MICROMIPS | STO_MIPS_PIC: return "MICROMIPS, MIPS PIC";
12280 case STO_MIPS16: return "MIPS16";
12281 default: return NULL;
12282 }
12283 }
12284
12285 static const char *
12286 get_ia64_symbol_other (Filedata * filedata, unsigned int other)
12287 {
12288 if (is_ia64_vms (filedata))
12289 {
12290 static char res[32];
12291
12292 res[0] = 0;
12293
12294 /* Function types is for images and .STB files only. */
12295 switch (filedata->file_header.e_type)
12296 {
12297 case ET_DYN:
12298 case ET_EXEC:
12299 switch (VMS_ST_FUNC_TYPE (other))
12300 {
12301 case VMS_SFT_CODE_ADDR:
12302 strcat (res, " CA");
12303 break;
12304 case VMS_SFT_SYMV_IDX:
12305 strcat (res, " VEC");
12306 break;
12307 case VMS_SFT_FD:
12308 strcat (res, " FD");
12309 break;
12310 case VMS_SFT_RESERVE:
12311 strcat (res, " RSV");
12312 break;
12313 default:
12314 warn (_("Unrecognized IA64 VMS ST Function type: %d\n"),
12315 VMS_ST_FUNC_TYPE (other));
12316 strcat (res, " <unknown>");
12317 break;
12318 }
12319 break;
12320 default:
12321 break;
12322 }
12323 switch (VMS_ST_LINKAGE (other))
12324 {
12325 case VMS_STL_IGNORE:
12326 strcat (res, " IGN");
12327 break;
12328 case VMS_STL_RESERVE:
12329 strcat (res, " RSV");
12330 break;
12331 case VMS_STL_STD:
12332 strcat (res, " STD");
12333 break;
12334 case VMS_STL_LNK:
12335 strcat (res, " LNK");
12336 break;
12337 default:
12338 warn (_("Unrecognized IA64 VMS ST Linkage: %d\n"),
12339 VMS_ST_LINKAGE (other));
12340 strcat (res, " <unknown>");
12341 break;
12342 }
12343
12344 if (res[0] != 0)
12345 return res + 1;
12346 else
12347 return res;
12348 }
12349 return NULL;
12350 }
12351
12352 static const char *
12353 get_ppc64_symbol_other (unsigned int other)
12354 {
12355 if ((other & ~STO_PPC64_LOCAL_MASK) != 0)
12356 return NULL;
12357
12358 other >>= STO_PPC64_LOCAL_BIT;
12359 if (other <= 6)
12360 {
12361 static char buf[64];
12362 if (other >= 2)
12363 other = ppc64_decode_local_entry (other);
12364 snprintf (buf, sizeof buf, _("<localentry>: %d"), other);
12365 return buf;
12366 }
12367 return NULL;
12368 }
12369
12370 static const char *
12371 get_symbol_other (Filedata * filedata, unsigned int other)
12372 {
12373 const char * result = NULL;
12374 static char buff [64];
12375
12376 if (other == 0)
12377 return "";
12378
12379 switch (filedata->file_header.e_machine)
12380 {
12381 case EM_ALPHA:
12382 result = get_alpha_symbol_other (other);
12383 break;
12384 case EM_AARCH64:
12385 result = get_aarch64_symbol_other (other);
12386 break;
12387 case EM_MIPS:
12388 result = get_mips_symbol_other (other);
12389 break;
12390 case EM_IA_64:
12391 result = get_ia64_symbol_other (filedata, other);
12392 break;
12393 case EM_PPC64:
12394 result = get_ppc64_symbol_other (other);
12395 break;
12396 default:
12397 result = NULL;
12398 break;
12399 }
12400
12401 if (result)
12402 return result;
12403
12404 snprintf (buff, sizeof buff, _("<other>: %x"), other);
12405 return buff;
12406 }
12407
12408 static const char *
12409 get_symbol_index_type (Filedata * filedata, unsigned int type)
12410 {
12411 static char buff[32];
12412
12413 switch (type)
12414 {
12415 case SHN_UNDEF: return "UND";
12416 case SHN_ABS: return "ABS";
12417 case SHN_COMMON: return "COM";
12418 default:
12419 if (type == SHN_IA_64_ANSI_COMMON
12420 && filedata->file_header.e_machine == EM_IA_64
12421 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX)
12422 return "ANSI_COM";
12423 else if ((filedata->file_header.e_machine == EM_X86_64
12424 || filedata->file_header.e_machine == EM_L1OM
12425 || filedata->file_header.e_machine == EM_K1OM)
12426 && type == SHN_X86_64_LCOMMON)
12427 return "LARGE_COM";
12428 else if ((type == SHN_MIPS_SCOMMON
12429 && filedata->file_header.e_machine == EM_MIPS)
12430 || (type == SHN_TIC6X_SCOMMON
12431 && filedata->file_header.e_machine == EM_TI_C6000))
12432 return "SCOM";
12433 else if (type == SHN_MIPS_SUNDEFINED
12434 && filedata->file_header.e_machine == EM_MIPS)
12435 return "SUND";
12436 else if (type >= SHN_LOPROC && type <= SHN_HIPROC)
12437 sprintf (buff, "PRC[0x%04x]", type & 0xffff);
12438 else if (type >= SHN_LOOS && type <= SHN_HIOS)
12439 sprintf (buff, "OS [0x%04x]", type & 0xffff);
12440 else if (type >= SHN_LORESERVE)
12441 sprintf (buff, "RSV[0x%04x]", type & 0xffff);
12442 else if (filedata->file_header.e_shnum != 0
12443 && type >= filedata->file_header.e_shnum)
12444 sprintf (buff, _("bad section index[%3d]"), type);
12445 else
12446 sprintf (buff, "%3d", type);
12447 break;
12448 }
12449
12450 return buff;
12451 }
12452
12453 static const char *
12454 get_symbol_version_string (Filedata * filedata,
12455 bool is_dynsym,
12456 const char * strtab,
12457 unsigned long int strtab_size,
12458 unsigned int si,
12459 Elf_Internal_Sym * psym,
12460 enum versioned_symbol_info * sym_info,
12461 unsigned short * vna_other)
12462 {
12463 unsigned char data[2];
12464 unsigned short vers_data;
12465 unsigned long offset;
12466 unsigned short max_vd_ndx;
12467
12468 if (!is_dynsym
12469 || filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)] == 0)
12470 return NULL;
12471
12472 offset = offset_from_vma (filedata,
12473 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
12474 sizeof data + si * sizeof (vers_data));
12475
12476 if (get_data (&data, filedata, offset + si * sizeof (vers_data),
12477 sizeof (data), 1, _("version data")) == NULL)
12478 return NULL;
12479
12480 vers_data = byte_get (data, 2);
12481
12482 if ((vers_data & VERSYM_HIDDEN) == 0 && vers_data == 0)
12483 return NULL;
12484
12485 *sym_info = (vers_data & VERSYM_HIDDEN) != 0 ? symbol_hidden : symbol_public;
12486 max_vd_ndx = 0;
12487
12488 /* Usually we'd only see verdef for defined symbols, and verneed for
12489 undefined symbols. However, symbols defined by the linker in
12490 .dynbss for variables copied from a shared library in order to
12491 avoid text relocations are defined yet have verneed. We could
12492 use a heuristic to detect the special case, for example, check
12493 for verneed first on symbols defined in SHT_NOBITS sections, but
12494 it is simpler and more reliable to just look for both verdef and
12495 verneed. .dynbss might not be mapped to a SHT_NOBITS section. */
12496
12497 if (psym->st_shndx != SHN_UNDEF
12498 && vers_data != 0x8001
12499 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
12500 {
12501 Elf_Internal_Verdef ivd;
12502 Elf_Internal_Verdaux ivda;
12503 Elf_External_Verdaux evda;
12504 unsigned long off;
12505
12506 off = offset_from_vma (filedata,
12507 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
12508 sizeof (Elf_External_Verdef));
12509
12510 do
12511 {
12512 Elf_External_Verdef evd;
12513
12514 if (get_data (&evd, filedata, off, sizeof (evd), 1,
12515 _("version def")) == NULL)
12516 {
12517 ivd.vd_ndx = 0;
12518 ivd.vd_aux = 0;
12519 ivd.vd_next = 0;
12520 ivd.vd_flags = 0;
12521 }
12522 else
12523 {
12524 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
12525 ivd.vd_aux = BYTE_GET (evd.vd_aux);
12526 ivd.vd_next = BYTE_GET (evd.vd_next);
12527 ivd.vd_flags = BYTE_GET (evd.vd_flags);
12528 }
12529
12530 if ((ivd.vd_ndx & VERSYM_VERSION) > max_vd_ndx)
12531 max_vd_ndx = ivd.vd_ndx & VERSYM_VERSION;
12532
12533 off += ivd.vd_next;
12534 }
12535 while (ivd.vd_ndx != (vers_data & VERSYM_VERSION) && ivd.vd_next != 0);
12536
12537 if (ivd.vd_ndx == (vers_data & VERSYM_VERSION))
12538 {
12539 if (ivd.vd_ndx == 1 && ivd.vd_flags == VER_FLG_BASE)
12540 return NULL;
12541
12542 off -= ivd.vd_next;
12543 off += ivd.vd_aux;
12544
12545 if (get_data (&evda, filedata, off, sizeof (evda), 1,
12546 _("version def aux")) != NULL)
12547 {
12548 ivda.vda_name = BYTE_GET (evda.vda_name);
12549
12550 if (psym->st_name != ivda.vda_name)
12551 return (ivda.vda_name < strtab_size
12552 ? strtab + ivda.vda_name : _("<corrupt>"));
12553 }
12554 }
12555 }
12556
12557 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
12558 {
12559 Elf_External_Verneed evn;
12560 Elf_Internal_Verneed ivn;
12561 Elf_Internal_Vernaux ivna;
12562
12563 offset = offset_from_vma (filedata,
12564 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
12565 sizeof evn);
12566 do
12567 {
12568 unsigned long vna_off;
12569
12570 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
12571 _("version need")) == NULL)
12572 {
12573 ivna.vna_next = 0;
12574 ivna.vna_other = 0;
12575 ivna.vna_name = 0;
12576 break;
12577 }
12578
12579 ivn.vn_aux = BYTE_GET (evn.vn_aux);
12580 ivn.vn_next = BYTE_GET (evn.vn_next);
12581
12582 vna_off = offset + ivn.vn_aux;
12583
12584 do
12585 {
12586 Elf_External_Vernaux evna;
12587
12588 if (get_data (&evna, filedata, vna_off, sizeof (evna), 1,
12589 _("version need aux (3)")) == NULL)
12590 {
12591 ivna.vna_next = 0;
12592 ivna.vna_other = 0;
12593 ivna.vna_name = 0;
12594 }
12595 else
12596 {
12597 ivna.vna_other = BYTE_GET (evna.vna_other);
12598 ivna.vna_next = BYTE_GET (evna.vna_next);
12599 ivna.vna_name = BYTE_GET (evna.vna_name);
12600 }
12601
12602 vna_off += ivna.vna_next;
12603 }
12604 while (ivna.vna_other != vers_data && ivna.vna_next != 0);
12605
12606 if (ivna.vna_other == vers_data)
12607 break;
12608
12609 offset += ivn.vn_next;
12610 }
12611 while (ivn.vn_next != 0);
12612
12613 if (ivna.vna_other == vers_data)
12614 {
12615 *sym_info = symbol_undefined;
12616 *vna_other = ivna.vna_other;
12617 return (ivna.vna_name < strtab_size
12618 ? strtab + ivna.vna_name : _("<corrupt>"));
12619 }
12620 else if ((max_vd_ndx || (vers_data & VERSYM_VERSION) != 1)
12621 && (vers_data & VERSYM_VERSION) > max_vd_ndx)
12622 return _("<corrupt>");
12623 }
12624 return NULL;
12625 }
12626
12627 /* Display a symbol size on stdout. Format is based on --sym-base setting. */
12628
12629 static unsigned int
12630 print_dynamic_symbol_size (bfd_vma vma, int base)
12631 {
12632 switch (base)
12633 {
12634 case 8:
12635 return print_vma (vma, OCTAL_5);
12636
12637 case 10:
12638 return print_vma (vma, UNSIGNED_5);
12639
12640 case 16:
12641 return print_vma (vma, PREFIX_HEX_5);
12642
12643 case 0:
12644 default:
12645 return print_vma (vma, DEC_5);
12646 }
12647 }
12648
12649 static void
12650 print_dynamic_symbol (Filedata *filedata, unsigned long si,
12651 Elf_Internal_Sym *symtab,
12652 Elf_Internal_Shdr *section,
12653 char *strtab, size_t strtab_size)
12654 {
12655 const char *version_string;
12656 enum versioned_symbol_info sym_info;
12657 unsigned short vna_other;
12658 bool is_valid;
12659 const char * sstr;
12660 Elf_Internal_Sym *psym = symtab + si;
12661
12662 printf ("%6ld: ", si);
12663 print_vma (psym->st_value, LONG_HEX);
12664 putchar (' ');
12665 print_dynamic_symbol_size (psym->st_size, sym_base);
12666 printf (" %-7s", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
12667 printf (" %-6s", get_symbol_binding (filedata, ELF_ST_BIND (psym->st_info)));
12668 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
12669 printf (" %-7s", get_solaris_symbol_visibility (psym->st_other));
12670 else
12671 {
12672 unsigned int vis = ELF_ST_VISIBILITY (psym->st_other);
12673
12674 printf (" %-7s", get_symbol_visibility (vis));
12675 /* Check to see if any other bits in the st_other field are set.
12676 Note - displaying this information disrupts the layout of the
12677 table being generated, but for the moment this case is very rare. */
12678 if (psym->st_other ^ vis)
12679 printf (" [%s] ", get_symbol_other (filedata, psym->st_other ^ vis));
12680 }
12681 printf (" %4s ", get_symbol_index_type (filedata, psym->st_shndx));
12682
12683 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION
12684 && psym->st_shndx < filedata->file_header.e_shnum
12685 && psym->st_name == 0)
12686 {
12687 is_valid
12688 = section_name_valid (filedata,
12689 filedata->section_headers + psym->st_shndx);
12690 sstr = is_valid ?
12691 section_name_print (filedata,
12692 filedata->section_headers + psym->st_shndx)
12693 : _("<corrupt>");
12694 }
12695 else
12696 {
12697 is_valid = valid_symbol_name (strtab, strtab_size, psym->st_name);
12698 sstr = is_valid ? strtab + psym->st_name : _("<corrupt>");
12699 }
12700
12701 version_string
12702 = get_symbol_version_string (filedata,
12703 (section == NULL
12704 || section->sh_type == SHT_DYNSYM),
12705 strtab, strtab_size, si,
12706 psym, &sym_info, &vna_other);
12707
12708 int len_avail = 21;
12709 if (! do_wide && version_string != NULL)
12710 {
12711 char buffer[16];
12712
12713 len_avail -= 1 + strlen (version_string);
12714
12715 if (sym_info == symbol_undefined)
12716 len_avail -= sprintf (buffer," (%d)", vna_other);
12717 else if (sym_info != symbol_hidden)
12718 len_avail -= 1;
12719 }
12720
12721 print_symbol (len_avail, sstr);
12722
12723 if (version_string)
12724 {
12725 if (sym_info == symbol_undefined)
12726 printf ("@%s (%d)", version_string, vna_other);
12727 else
12728 printf (sym_info == symbol_hidden ? "@%s" : "@@%s",
12729 version_string);
12730 }
12731
12732 putchar ('\n');
12733
12734 if (ELF_ST_BIND (psym->st_info) == STB_LOCAL
12735 && section != NULL
12736 && si >= section->sh_info
12737 /* Irix 5 and 6 MIPS binaries are known to ignore this requirement. */
12738 && filedata->file_header.e_machine != EM_MIPS
12739 /* Solaris binaries have been found to violate this requirement as
12740 well. Not sure if this is a bug or an ABI requirement. */
12741 && filedata->file_header.e_ident[EI_OSABI] != ELFOSABI_SOLARIS)
12742 warn (_("local symbol %lu found at index >= %s's sh_info value of %u\n"),
12743 si, printable_section_name (filedata, section), section->sh_info);
12744 }
12745
12746 static const char *
12747 get_lto_kind (unsigned int kind)
12748 {
12749 switch (kind)
12750 {
12751 case 0: return "DEF";
12752 case 1: return "WEAKDEF";
12753 case 2: return "UNDEF";
12754 case 3: return "WEAKUNDEF";
12755 case 4: return "COMMON";
12756 default:
12757 break;
12758 }
12759
12760 static char buffer[30];
12761 error (_("Unknown LTO symbol definition encountered: %u\n"), kind);
12762 sprintf (buffer, "<unknown: %u>", kind);
12763 return buffer;
12764 }
12765
12766 static const char *
12767 get_lto_visibility (unsigned int visibility)
12768 {
12769 switch (visibility)
12770 {
12771 case 0: return "DEFAULT";
12772 case 1: return "PROTECTED";
12773 case 2: return "INTERNAL";
12774 case 3: return "HIDDEN";
12775 default:
12776 break;
12777 }
12778
12779 static char buffer[30];
12780 error (_("Unknown LTO symbol visibility encountered: %u\n"), visibility);
12781 sprintf (buffer, "<unknown: %u>", visibility);
12782 return buffer;
12783 }
12784
12785 static const char *
12786 get_lto_sym_type (unsigned int sym_type)
12787 {
12788 switch (sym_type)
12789 {
12790 case 0: return "UNKNOWN";
12791 case 1: return "FUNCTION";
12792 case 2: return "VARIABLE";
12793 default:
12794 break;
12795 }
12796
12797 static char buffer[30];
12798 error (_("Unknown LTO symbol type encountered: %u\n"), sym_type);
12799 sprintf (buffer, "<unknown: %u>", sym_type);
12800 return buffer;
12801 }
12802
12803 /* Display an LTO format symbol table.
12804 FIXME: The format of LTO symbol tables is not formalized.
12805 So this code could need changing in the future. */
12806
12807 static bool
12808 display_lto_symtab (Filedata * filedata,
12809 Elf_Internal_Shdr * section)
12810 {
12811 if (section->sh_size == 0)
12812 {
12813 if (filedata->is_separate)
12814 printf (_("\nThe LTO Symbol table section '%s' in linked file '%s' is empty!\n"),
12815 printable_section_name (filedata, section),
12816 filedata->file_name);
12817 else
12818 printf (_("\nLTO Symbol table '%s' is empty!\n"),
12819 printable_section_name (filedata, section));
12820
12821 return true;
12822 }
12823
12824 if (section->sh_size > filedata->file_size)
12825 {
12826 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
12827 printable_section_name (filedata, section),
12828 (unsigned long) section->sh_size);
12829 return false;
12830 }
12831
12832 void * alloced_data = get_data (NULL, filedata, section->sh_offset,
12833 section->sh_size, 1, _("LTO symbols"));
12834 if (alloced_data == NULL)
12835 return false;
12836
12837 /* Look for extended data for the symbol table. */
12838 Elf_Internal_Shdr * ext;
12839 void * ext_data_orig = NULL;
12840 char * ext_data = NULL;
12841 char * ext_data_end = NULL;
12842 char * ext_name = NULL;
12843
12844 if (asprintf (& ext_name, ".gnu.lto_.ext_symtab.%s",
12845 (section_name (filedata, section)
12846 + sizeof (".gnu.lto_.symtab.") - 1)) > 0
12847 && ext_name != NULL /* Paranoia. */
12848 && (ext = find_section (filedata, ext_name)) != NULL)
12849 {
12850 if (ext->sh_size < 3)
12851 error (_("LTO Symbol extension table '%s' is empty!\n"),
12852 printable_section_name (filedata, ext));
12853 else
12854 {
12855 ext_data_orig = ext_data = get_data (NULL, filedata, ext->sh_offset,
12856 ext->sh_size, 1,
12857 _("LTO ext symbol data"));
12858 if (ext_data != NULL)
12859 {
12860 ext_data_end = ext_data + ext->sh_size;
12861 if (* ext_data++ != 1)
12862 error (_("Unexpected version number in symbol extension table\n"));
12863 }
12864 }
12865 }
12866
12867 const unsigned char * data = (const unsigned char *) alloced_data;
12868 const unsigned char * end = data + section->sh_size;
12869
12870 if (filedata->is_separate)
12871 printf (_("\nIn linked file '%s': "), filedata->file_name);
12872 else
12873 printf ("\n");
12874
12875 if (ext_data_orig != NULL)
12876 {
12877 if (do_wide)
12878 printf (_("LTO Symbol table '%s' and extension table '%s' contain:\n"),
12879 printable_section_name (filedata, section),
12880 printable_section_name (filedata, ext));
12881 else
12882 {
12883 printf (_("LTO Symbol table '%s'\n"),
12884 printable_section_name (filedata, section));
12885 printf (_(" and extension table '%s' contain:\n"),
12886 printable_section_name (filedata, ext));
12887 }
12888 }
12889 else
12890 printf (_("LTO Symbol table '%s' contains:\n"),
12891 printable_section_name (filedata, section));
12892
12893 /* FIXME: Add a wide version. */
12894 if (ext_data_orig != NULL)
12895 printf (_(" Comdat_Key Kind Visibility Size Slot Type Section Name\n"));
12896 else
12897 printf (_(" Comdat_Key Kind Visibility Size Slot Name\n"));
12898
12899 /* FIXME: We do not handle style prefixes. */
12900
12901 while (data < end)
12902 {
12903 const unsigned char * sym_name = data;
12904 data += strnlen ((const char *) sym_name, end - data) + 1;
12905 if (data >= end)
12906 goto fail;
12907
12908 const unsigned char * comdat_key = data;
12909 data += strnlen ((const char *) comdat_key, end - data) + 1;
12910 if (data >= end)
12911 goto fail;
12912
12913 if (data + 2 + 8 + 4 > end)
12914 goto fail;
12915
12916 unsigned int kind = *data++;
12917 unsigned int visibility = *data++;
12918
12919 elf_vma size = byte_get (data, 8);
12920 data += 8;
12921
12922 elf_vma slot = byte_get (data, 4);
12923 data += 4;
12924
12925 if (ext_data != NULL)
12926 {
12927 if (ext_data < (ext_data_end - 1))
12928 {
12929 unsigned int sym_type = * ext_data ++;
12930 unsigned int sec_kind = * ext_data ++;
12931
12932 printf (" %10s %10s %11s %08lx %08lx %9s %08lx _",
12933 * comdat_key == 0 ? "-" : (char *) comdat_key,
12934 get_lto_kind (kind),
12935 get_lto_visibility (visibility),
12936 (long) size,
12937 (long) slot,
12938 get_lto_sym_type (sym_type),
12939 (long) sec_kind);
12940 print_symbol (6, (const char *) sym_name);
12941 }
12942 else
12943 {
12944 error (_("Ran out of LTO symbol extension data\n"));
12945 ext_data = NULL;
12946 /* FIXME: return FAIL result ? */
12947 }
12948 }
12949 else
12950 {
12951 printf (" %10s %10s %11s %08lx %08lx _",
12952 * comdat_key == 0 ? "-" : (char *) comdat_key,
12953 get_lto_kind (kind),
12954 get_lto_visibility (visibility),
12955 (long) size,
12956 (long) slot);
12957 print_symbol (21, (const char *) sym_name);
12958 }
12959 putchar ('\n');
12960 }
12961
12962 if (ext_data != NULL && ext_data < ext_data_end)
12963 {
12964 error (_("Data remains in the LTO symbol extension table\n"));
12965 goto fail;
12966 }
12967
12968 free (alloced_data);
12969 free (ext_data_orig);
12970 free (ext_name);
12971 return true;
12972
12973 fail:
12974 error (_("Buffer overrun encountered whilst decoding LTO symbol table\n"));
12975 free (alloced_data);
12976 free (ext_data_orig);
12977 free (ext_name);
12978 return false;
12979 }
12980
12981 /* Display LTO symbol tables. */
12982
12983 static bool
12984 process_lto_symbol_tables (Filedata * filedata)
12985 {
12986 Elf_Internal_Shdr * section;
12987 unsigned int i;
12988 bool res = true;
12989
12990 if (!do_lto_syms)
12991 return true;
12992
12993 if (filedata->section_headers == NULL)
12994 return true;
12995
12996 for (i = 0, section = filedata->section_headers;
12997 i < filedata->file_header.e_shnum;
12998 i++, section++)
12999 if (section_name_valid (filedata, section)
13000 && startswith (section_name (filedata, section), ".gnu.lto_.symtab."))
13001 res &= display_lto_symtab (filedata, section);
13002
13003 return res;
13004 }
13005
13006 /* Dump the symbol table. */
13007
13008 static bool
13009 process_symbol_table (Filedata * filedata)
13010 {
13011 Elf_Internal_Shdr * section;
13012
13013 if (!do_syms && !do_dyn_syms && !do_histogram)
13014 return true;
13015
13016 if ((filedata->dynamic_info[DT_HASH] || filedata->dynamic_info_DT_GNU_HASH)
13017 && do_syms
13018 && do_using_dynamic
13019 && filedata->dynamic_strings != NULL
13020 && filedata->dynamic_symbols != NULL)
13021 {
13022 unsigned long si;
13023
13024 if (filedata->is_separate)
13025 {
13026 printf (ngettext ("\nIn linked file '%s' the dynamic symbol table contains %lu entry:\n",
13027 "\nIn linked file '%s' the dynamic symbol table contains %lu entries:\n",
13028 filedata->num_dynamic_syms),
13029 filedata->file_name,
13030 filedata->num_dynamic_syms);
13031 }
13032 else
13033 {
13034 printf (ngettext ("\nSymbol table for image contains %lu entry:\n",
13035 "\nSymbol table for image contains %lu entries:\n",
13036 filedata->num_dynamic_syms),
13037 filedata->num_dynamic_syms);
13038 }
13039 if (is_32bit_elf)
13040 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13041 else
13042 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13043
13044 for (si = 0; si < filedata->num_dynamic_syms; si++)
13045 print_dynamic_symbol (filedata, si, filedata->dynamic_symbols, NULL,
13046 filedata->dynamic_strings,
13047 filedata->dynamic_strings_length);
13048 }
13049 else if ((do_dyn_syms || (do_syms && !do_using_dynamic))
13050 && filedata->section_headers != NULL)
13051 {
13052 unsigned int i;
13053
13054 for (i = 0, section = filedata->section_headers;
13055 i < filedata->file_header.e_shnum;
13056 i++, section++)
13057 {
13058 char * strtab = NULL;
13059 unsigned long int strtab_size = 0;
13060 Elf_Internal_Sym * symtab;
13061 unsigned long si, num_syms;
13062
13063 if ((section->sh_type != SHT_SYMTAB
13064 && section->sh_type != SHT_DYNSYM)
13065 || (!do_syms
13066 && section->sh_type == SHT_SYMTAB))
13067 continue;
13068
13069 if (section->sh_entsize == 0)
13070 {
13071 printf (_("\nSymbol table '%s' has a sh_entsize of zero!\n"),
13072 printable_section_name (filedata, section));
13073 continue;
13074 }
13075
13076 num_syms = section->sh_size / section->sh_entsize;
13077
13078 if (filedata->is_separate)
13079 printf (ngettext ("\nIn linked file '%s' symbol section '%s' contains %lu entry:\n",
13080 "\nIn linked file '%s' symbol section '%s' contains %lu entries:\n",
13081 num_syms),
13082 filedata->file_name,
13083 printable_section_name (filedata, section),
13084 num_syms);
13085 else
13086 printf (ngettext ("\nSymbol table '%s' contains %lu entry:\n",
13087 "\nSymbol table '%s' contains %lu entries:\n",
13088 num_syms),
13089 printable_section_name (filedata, section),
13090 num_syms);
13091
13092 if (is_32bit_elf)
13093 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13094 else
13095 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13096
13097 symtab = get_elf_symbols (filedata, section, & num_syms);
13098 if (symtab == NULL)
13099 continue;
13100
13101 if (section->sh_link == filedata->file_header.e_shstrndx)
13102 {
13103 strtab = filedata->string_table;
13104 strtab_size = filedata->string_table_length;
13105 }
13106 else if (section->sh_link < filedata->file_header.e_shnum)
13107 {
13108 Elf_Internal_Shdr * string_sec;
13109
13110 string_sec = filedata->section_headers + section->sh_link;
13111
13112 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset,
13113 1, string_sec->sh_size,
13114 _("string table"));
13115 strtab_size = strtab != NULL ? string_sec->sh_size : 0;
13116 }
13117
13118 for (si = 0; si < num_syms; si++)
13119 print_dynamic_symbol (filedata, si, symtab, section,
13120 strtab, strtab_size);
13121
13122 free (symtab);
13123 if (strtab != filedata->string_table)
13124 free (strtab);
13125 }
13126 }
13127 else if (do_syms)
13128 printf
13129 (_("\nDynamic symbol information is not available for displaying symbols.\n"));
13130
13131 if (do_histogram && filedata->buckets != NULL)
13132 {
13133 unsigned long * lengths;
13134 unsigned long * counts;
13135 unsigned long hn;
13136 bfd_vma si;
13137 unsigned long maxlength = 0;
13138 unsigned long nzero_counts = 0;
13139 unsigned long nsyms = 0;
13140 char *visited;
13141
13142 printf (ngettext ("\nHistogram for bucket list length "
13143 "(total of %lu bucket):\n",
13144 "\nHistogram for bucket list length "
13145 "(total of %lu buckets):\n",
13146 (unsigned long) filedata->nbuckets),
13147 (unsigned long) filedata->nbuckets);
13148
13149 lengths = (unsigned long *) calloc (filedata->nbuckets,
13150 sizeof (*lengths));
13151 if (lengths == NULL)
13152 {
13153 error (_("Out of memory allocating space for histogram buckets\n"));
13154 goto err_out;
13155 }
13156 visited = xcmalloc (filedata->nchains, 1);
13157 memset (visited, 0, filedata->nchains);
13158
13159 printf (_(" Length Number %% of total Coverage\n"));
13160 for (hn = 0; hn < filedata->nbuckets; ++hn)
13161 {
13162 for (si = filedata->buckets[hn]; si > 0; si = filedata->chains[si])
13163 {
13164 ++nsyms;
13165 if (maxlength < ++lengths[hn])
13166 ++maxlength;
13167 if (si >= filedata->nchains || visited[si])
13168 {
13169 error (_("histogram chain is corrupt\n"));
13170 break;
13171 }
13172 visited[si] = 1;
13173 }
13174 }
13175 free (visited);
13176
13177 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
13178 if (counts == NULL)
13179 {
13180 free (lengths);
13181 error (_("Out of memory allocating space for histogram counts\n"));
13182 goto err_out;
13183 }
13184
13185 for (hn = 0; hn < filedata->nbuckets; ++hn)
13186 ++counts[lengths[hn]];
13187
13188 if (filedata->nbuckets > 0)
13189 {
13190 unsigned long i;
13191 printf (" 0 %-10lu (%5.1f%%)\n",
13192 counts[0], (counts[0] * 100.0) / filedata->nbuckets);
13193 for (i = 1; i <= maxlength; ++i)
13194 {
13195 nzero_counts += counts[i] * i;
13196 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
13197 i, counts[i], (counts[i] * 100.0) / filedata->nbuckets,
13198 (nzero_counts * 100.0) / nsyms);
13199 }
13200 }
13201
13202 free (counts);
13203 free (lengths);
13204 }
13205
13206 free (filedata->buckets);
13207 filedata->buckets = NULL;
13208 filedata->nbuckets = 0;
13209 free (filedata->chains);
13210 filedata->chains = NULL;
13211
13212 if (do_histogram && filedata->gnubuckets != NULL)
13213 {
13214 unsigned long * lengths;
13215 unsigned long * counts;
13216 unsigned long hn;
13217 unsigned long maxlength = 0;
13218 unsigned long nzero_counts = 0;
13219 unsigned long nsyms = 0;
13220
13221 printf (ngettext ("\nHistogram for `%s' bucket list length "
13222 "(total of %lu bucket):\n",
13223 "\nHistogram for `%s' bucket list length "
13224 "(total of %lu buckets):\n",
13225 (unsigned long) filedata->ngnubuckets),
13226 GNU_HASH_SECTION_NAME (filedata),
13227 (unsigned long) filedata->ngnubuckets);
13228
13229 lengths = (unsigned long *) calloc (filedata->ngnubuckets,
13230 sizeof (*lengths));
13231 if (lengths == NULL)
13232 {
13233 error (_("Out of memory allocating space for gnu histogram buckets\n"));
13234 goto err_out;
13235 }
13236
13237 printf (_(" Length Number %% of total Coverage\n"));
13238
13239 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
13240 if (filedata->gnubuckets[hn] != 0)
13241 {
13242 bfd_vma off, length = 1;
13243
13244 for (off = filedata->gnubuckets[hn] - filedata->gnusymidx;
13245 /* PR 17531 file: 010-77222-0.004. */
13246 off < filedata->ngnuchains
13247 && (filedata->gnuchains[off] & 1) == 0;
13248 ++off)
13249 ++length;
13250 lengths[hn] = length;
13251 if (length > maxlength)
13252 maxlength = length;
13253 nsyms += length;
13254 }
13255
13256 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
13257 if (counts == NULL)
13258 {
13259 free (lengths);
13260 error (_("Out of memory allocating space for gnu histogram counts\n"));
13261 goto err_out;
13262 }
13263
13264 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
13265 ++counts[lengths[hn]];
13266
13267 if (filedata->ngnubuckets > 0)
13268 {
13269 unsigned long j;
13270 printf (" 0 %-10lu (%5.1f%%)\n",
13271 counts[0], (counts[0] * 100.0) / filedata->ngnubuckets);
13272 for (j = 1; j <= maxlength; ++j)
13273 {
13274 nzero_counts += counts[j] * j;
13275 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
13276 j, counts[j], (counts[j] * 100.0) / filedata->ngnubuckets,
13277 (nzero_counts * 100.0) / nsyms);
13278 }
13279 }
13280
13281 free (counts);
13282 free (lengths);
13283 }
13284 free (filedata->gnubuckets);
13285 filedata->gnubuckets = NULL;
13286 filedata->ngnubuckets = 0;
13287 free (filedata->gnuchains);
13288 filedata->gnuchains = NULL;
13289 filedata->ngnuchains = 0;
13290 free (filedata->mipsxlat);
13291 filedata->mipsxlat = NULL;
13292 return true;
13293
13294 err_out:
13295 free (filedata->gnubuckets);
13296 filedata->gnubuckets = NULL;
13297 filedata->ngnubuckets = 0;
13298 free (filedata->gnuchains);
13299 filedata->gnuchains = NULL;
13300 filedata->ngnuchains = 0;
13301 free (filedata->mipsxlat);
13302 filedata->mipsxlat = NULL;
13303 free (filedata->buckets);
13304 filedata->buckets = NULL;
13305 filedata->nbuckets = 0;
13306 free (filedata->chains);
13307 filedata->chains = NULL;
13308 return false;
13309 }
13310
13311 static bool
13312 process_syminfo (Filedata * filedata)
13313 {
13314 unsigned int i;
13315
13316 if (filedata->dynamic_syminfo == NULL
13317 || !do_dynamic)
13318 /* No syminfo, this is ok. */
13319 return true;
13320
13321 /* There better should be a dynamic symbol section. */
13322 if (filedata->dynamic_symbols == NULL || filedata->dynamic_strings == NULL)
13323 return false;
13324
13325 if (filedata->is_separate)
13326 printf (ngettext ("\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entry:\n",
13327 "\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entries:\n",
13328 filedata->dynamic_syminfo_nent),
13329 filedata->file_name,
13330 filedata->dynamic_syminfo_offset,
13331 filedata->dynamic_syminfo_nent);
13332 else
13333 printf (ngettext ("\nDynamic info segment at offset 0x%lx "
13334 "contains %d entry:\n",
13335 "\nDynamic info segment at offset 0x%lx "
13336 "contains %d entries:\n",
13337 filedata->dynamic_syminfo_nent),
13338 filedata->dynamic_syminfo_offset,
13339 filedata->dynamic_syminfo_nent);
13340
13341 printf (_(" Num: Name BoundTo Flags\n"));
13342 for (i = 0; i < filedata->dynamic_syminfo_nent; ++i)
13343 {
13344 unsigned short int flags = filedata->dynamic_syminfo[i].si_flags;
13345
13346 printf ("%4d: ", i);
13347 if (i >= filedata->num_dynamic_syms)
13348 printf (_("<corrupt index>"));
13349 else if (valid_dynamic_name (filedata, filedata->dynamic_symbols[i].st_name))
13350 print_symbol (30, get_dynamic_name (filedata,
13351 filedata->dynamic_symbols[i].st_name));
13352 else
13353 printf (_("<corrupt: %19ld>"), filedata->dynamic_symbols[i].st_name);
13354 putchar (' ');
13355
13356 switch (filedata->dynamic_syminfo[i].si_boundto)
13357 {
13358 case SYMINFO_BT_SELF:
13359 fputs ("SELF ", stdout);
13360 break;
13361 case SYMINFO_BT_PARENT:
13362 fputs ("PARENT ", stdout);
13363 break;
13364 default:
13365 if (filedata->dynamic_syminfo[i].si_boundto > 0
13366 && filedata->dynamic_syminfo[i].si_boundto < filedata->dynamic_nent
13367 && valid_dynamic_name (filedata,
13368 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val))
13369 {
13370 print_symbol (10, get_dynamic_name (filedata,
13371 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val));
13372 putchar (' ' );
13373 }
13374 else
13375 printf ("%-10d ", filedata->dynamic_syminfo[i].si_boundto);
13376 break;
13377 }
13378
13379 if (flags & SYMINFO_FLG_DIRECT)
13380 printf (" DIRECT");
13381 if (flags & SYMINFO_FLG_PASSTHRU)
13382 printf (" PASSTHRU");
13383 if (flags & SYMINFO_FLG_COPY)
13384 printf (" COPY");
13385 if (flags & SYMINFO_FLG_LAZYLOAD)
13386 printf (" LAZYLOAD");
13387
13388 puts ("");
13389 }
13390
13391 return true;
13392 }
13393
13394 /* A macro which evaluates to TRUE if the region ADDR .. ADDR + NELEM
13395 is contained by the region START .. END. The types of ADDR, START
13396 and END should all be the same. Note both ADDR + NELEM and END
13397 point to just beyond the end of the regions that are being tested. */
13398 #define IN_RANGE(START,END,ADDR,NELEM) \
13399 (((ADDR) >= (START)) && ((ADDR) < (END)) && ((ADDR) + (NELEM) <= (END)))
13400
13401 /* Check to see if the given reloc needs to be handled in a target specific
13402 manner. If so then process the reloc and return TRUE otherwise return
13403 FALSE.
13404
13405 If called with reloc == NULL, then this is a signal that reloc processing
13406 for the current section has finished, and any saved state should be
13407 discarded. */
13408
13409 static bool
13410 target_specific_reloc_handling (Filedata * filedata,
13411 Elf_Internal_Rela * reloc,
13412 unsigned char * start,
13413 unsigned char * end,
13414 Elf_Internal_Sym * symtab,
13415 unsigned long num_syms)
13416 {
13417 unsigned int reloc_type = 0;
13418 unsigned long sym_index = 0;
13419
13420 if (reloc)
13421 {
13422 reloc_type = get_reloc_type (filedata, reloc->r_info);
13423 sym_index = get_reloc_symindex (reloc->r_info);
13424 }
13425
13426 switch (filedata->file_header.e_machine)
13427 {
13428 case EM_MSP430:
13429 case EM_MSP430_OLD:
13430 {
13431 static Elf_Internal_Sym * saved_sym = NULL;
13432
13433 if (reloc == NULL)
13434 {
13435 saved_sym = NULL;
13436 return true;
13437 }
13438
13439 switch (reloc_type)
13440 {
13441 case 10: /* R_MSP430_SYM_DIFF */
13442 case 12: /* R_MSP430_GNU_SUB_ULEB128 */
13443 if (uses_msp430x_relocs (filedata))
13444 break;
13445 /* Fall through. */
13446 case 21: /* R_MSP430X_SYM_DIFF */
13447 case 23: /* R_MSP430X_GNU_SUB_ULEB128 */
13448 /* PR 21139. */
13449 if (sym_index >= num_syms)
13450 error (_("MSP430 SYM_DIFF reloc contains invalid symbol index %lu\n"),
13451 sym_index);
13452 else
13453 saved_sym = symtab + sym_index;
13454 return true;
13455
13456 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
13457 case 3: /* R_MSP430_16 or R_MSP430_ABS8 */
13458 goto handle_sym_diff;
13459
13460 case 5: /* R_MSP430_16_BYTE */
13461 case 9: /* R_MSP430_8 */
13462 case 11: /* R_MSP430_GNU_SET_ULEB128 */
13463 if (uses_msp430x_relocs (filedata))
13464 break;
13465 goto handle_sym_diff;
13466
13467 case 2: /* R_MSP430_ABS16 */
13468 case 15: /* R_MSP430X_ABS16 */
13469 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
13470 if (! uses_msp430x_relocs (filedata))
13471 break;
13472 goto handle_sym_diff;
13473
13474 handle_sym_diff:
13475 if (saved_sym != NULL)
13476 {
13477 bfd_vma value;
13478 unsigned int reloc_size = 0;
13479 int leb_ret = 0;
13480 switch (reloc_type)
13481 {
13482 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
13483 reloc_size = 4;
13484 break;
13485 case 11: /* R_MSP430_GNU_SET_ULEB128 */
13486 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
13487 if (reloc->r_offset < (size_t) (end - start))
13488 read_leb128 (start + reloc->r_offset, end, false,
13489 &reloc_size, &leb_ret);
13490 break;
13491 default:
13492 reloc_size = 2;
13493 break;
13494 }
13495
13496 if (leb_ret != 0 || reloc_size == 0 || reloc_size > 8)
13497 error (_("MSP430 ULEB128 field at 0x%lx contains invalid "
13498 "ULEB128 value\n"),
13499 (long) reloc->r_offset);
13500 else if (sym_index >= num_syms)
13501 error (_("MSP430 reloc contains invalid symbol index %lu\n"),
13502 sym_index);
13503 else
13504 {
13505 value = reloc->r_addend + (symtab[sym_index].st_value
13506 - saved_sym->st_value);
13507
13508 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
13509 byte_put (start + reloc->r_offset, value, reloc_size);
13510 else
13511 /* PR 21137 */
13512 error (_("MSP430 sym diff reloc contains invalid offset: 0x%lx\n"),
13513 (long) reloc->r_offset);
13514 }
13515
13516 saved_sym = NULL;
13517 return true;
13518 }
13519 break;
13520
13521 default:
13522 if (saved_sym != NULL)
13523 error (_("Unhandled MSP430 reloc type found after SYM_DIFF reloc\n"));
13524 break;
13525 }
13526 break;
13527 }
13528
13529 case EM_MN10300:
13530 case EM_CYGNUS_MN10300:
13531 {
13532 static Elf_Internal_Sym * saved_sym = NULL;
13533
13534 if (reloc == NULL)
13535 {
13536 saved_sym = NULL;
13537 return true;
13538 }
13539
13540 switch (reloc_type)
13541 {
13542 case 34: /* R_MN10300_ALIGN */
13543 return true;
13544 case 33: /* R_MN10300_SYM_DIFF */
13545 if (sym_index >= num_syms)
13546 error (_("MN10300_SYM_DIFF reloc contains invalid symbol index %lu\n"),
13547 sym_index);
13548 else
13549 saved_sym = symtab + sym_index;
13550 return true;
13551
13552 case 1: /* R_MN10300_32 */
13553 case 2: /* R_MN10300_16 */
13554 if (saved_sym != NULL)
13555 {
13556 int reloc_size = reloc_type == 1 ? 4 : 2;
13557 bfd_vma value;
13558
13559 if (sym_index >= num_syms)
13560 error (_("MN10300 reloc contains invalid symbol index %lu\n"),
13561 sym_index);
13562 else
13563 {
13564 value = reloc->r_addend + (symtab[sym_index].st_value
13565 - saved_sym->st_value);
13566
13567 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
13568 byte_put (start + reloc->r_offset, value, reloc_size);
13569 else
13570 error (_("MN10300 sym diff reloc contains invalid offset: 0x%lx\n"),
13571 (long) reloc->r_offset);
13572 }
13573
13574 saved_sym = NULL;
13575 return true;
13576 }
13577 break;
13578 default:
13579 if (saved_sym != NULL)
13580 error (_("Unhandled MN10300 reloc type found after SYM_DIFF reloc\n"));
13581 break;
13582 }
13583 break;
13584 }
13585
13586 case EM_RL78:
13587 {
13588 static bfd_vma saved_sym1 = 0;
13589 static bfd_vma saved_sym2 = 0;
13590 static bfd_vma value;
13591
13592 if (reloc == NULL)
13593 {
13594 saved_sym1 = saved_sym2 = 0;
13595 return true;
13596 }
13597
13598 switch (reloc_type)
13599 {
13600 case 0x80: /* R_RL78_SYM. */
13601 saved_sym1 = saved_sym2;
13602 if (sym_index >= num_syms)
13603 error (_("RL78_SYM reloc contains invalid symbol index %lu\n"),
13604 sym_index);
13605 else
13606 {
13607 saved_sym2 = symtab[sym_index].st_value;
13608 saved_sym2 += reloc->r_addend;
13609 }
13610 return true;
13611
13612 case 0x83: /* R_RL78_OPsub. */
13613 value = saved_sym1 - saved_sym2;
13614 saved_sym2 = saved_sym1 = 0;
13615 return true;
13616 break;
13617
13618 case 0x41: /* R_RL78_ABS32. */
13619 if (IN_RANGE (start, end, start + reloc->r_offset, 4))
13620 byte_put (start + reloc->r_offset, value, 4);
13621 else
13622 error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
13623 (long) reloc->r_offset);
13624 value = 0;
13625 return true;
13626
13627 case 0x43: /* R_RL78_ABS16. */
13628 if (IN_RANGE (start, end, start + reloc->r_offset, 2))
13629 byte_put (start + reloc->r_offset, value, 2);
13630 else
13631 error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
13632 (long) reloc->r_offset);
13633 value = 0;
13634 return true;
13635
13636 default:
13637 break;
13638 }
13639 break;
13640 }
13641 }
13642
13643 return false;
13644 }
13645
13646 /* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
13647 DWARF debug sections. This is a target specific test. Note - we do not
13648 go through the whole including-target-headers-multiple-times route, (as
13649 we have already done with <elf/h8.h>) because this would become very
13650 messy and even then this function would have to contain target specific
13651 information (the names of the relocs instead of their numeric values).
13652 FIXME: This is not the correct way to solve this problem. The proper way
13653 is to have target specific reloc sizing and typing functions created by
13654 the reloc-macros.h header, in the same way that it already creates the
13655 reloc naming functions. */
13656
13657 static bool
13658 is_32bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13659 {
13660 /* Please keep this table alpha-sorted for ease of visual lookup. */
13661 switch (filedata->file_header.e_machine)
13662 {
13663 case EM_386:
13664 case EM_IAMCU:
13665 return reloc_type == 1; /* R_386_32. */
13666 case EM_68K:
13667 return reloc_type == 1; /* R_68K_32. */
13668 case EM_860:
13669 return reloc_type == 1; /* R_860_32. */
13670 case EM_960:
13671 return reloc_type == 2; /* R_960_32. */
13672 case EM_AARCH64:
13673 return (reloc_type == 258
13674 || reloc_type == 1); /* R_AARCH64_ABS32 || R_AARCH64_P32_ABS32 */
13675 case EM_BPF:
13676 return reloc_type == 11; /* R_BPF_DATA_32 */
13677 case EM_ADAPTEVA_EPIPHANY:
13678 return reloc_type == 3;
13679 case EM_ALPHA:
13680 return reloc_type == 1; /* R_ALPHA_REFLONG. */
13681 case EM_ARC:
13682 return reloc_type == 1; /* R_ARC_32. */
13683 case EM_ARC_COMPACT:
13684 case EM_ARC_COMPACT2:
13685 return reloc_type == 4; /* R_ARC_32. */
13686 case EM_ARM:
13687 return reloc_type == 2; /* R_ARM_ABS32 */
13688 case EM_AVR_OLD:
13689 case EM_AVR:
13690 return reloc_type == 1;
13691 case EM_BLACKFIN:
13692 return reloc_type == 0x12; /* R_byte4_data. */
13693 case EM_CRIS:
13694 return reloc_type == 3; /* R_CRIS_32. */
13695 case EM_CR16:
13696 return reloc_type == 3; /* R_CR16_NUM32. */
13697 case EM_CRX:
13698 return reloc_type == 15; /* R_CRX_NUM32. */
13699 case EM_CSKY:
13700 return reloc_type == 1; /* R_CKCORE_ADDR32. */
13701 case EM_CYGNUS_FRV:
13702 return reloc_type == 1;
13703 case EM_CYGNUS_D10V:
13704 case EM_D10V:
13705 return reloc_type == 6; /* R_D10V_32. */
13706 case EM_CYGNUS_D30V:
13707 case EM_D30V:
13708 return reloc_type == 12; /* R_D30V_32_NORMAL. */
13709 case EM_DLX:
13710 return reloc_type == 3; /* R_DLX_RELOC_32. */
13711 case EM_CYGNUS_FR30:
13712 case EM_FR30:
13713 return reloc_type == 3; /* R_FR30_32. */
13714 case EM_FT32:
13715 return reloc_type == 1; /* R_FT32_32. */
13716 case EM_H8S:
13717 case EM_H8_300:
13718 case EM_H8_300H:
13719 return reloc_type == 1; /* R_H8_DIR32. */
13720 case EM_IA_64:
13721 return (reloc_type == 0x64 /* R_IA64_SECREL32MSB. */
13722 || reloc_type == 0x65 /* R_IA64_SECREL32LSB. */
13723 || reloc_type == 0x24 /* R_IA64_DIR32MSB. */
13724 || reloc_type == 0x25 /* R_IA64_DIR32LSB. */);
13725 case EM_IP2K_OLD:
13726 case EM_IP2K:
13727 return reloc_type == 2; /* R_IP2K_32. */
13728 case EM_IQ2000:
13729 return reloc_type == 2; /* R_IQ2000_32. */
13730 case EM_LATTICEMICO32:
13731 return reloc_type == 3; /* R_LM32_32. */
13732 case EM_LOONGARCH:
13733 return reloc_type == 1; /* R_LARCH_32. */
13734 case EM_M32C_OLD:
13735 case EM_M32C:
13736 return reloc_type == 3; /* R_M32C_32. */
13737 case EM_M32R:
13738 return reloc_type == 34; /* R_M32R_32_RELA. */
13739 case EM_68HC11:
13740 case EM_68HC12:
13741 return reloc_type == 6; /* R_M68HC11_32. */
13742 case EM_S12Z:
13743 return reloc_type == 7 || /* R_S12Z_EXT32 */
13744 reloc_type == 6; /* R_S12Z_CW32. */
13745 case EM_MCORE:
13746 return reloc_type == 1; /* R_MCORE_ADDR32. */
13747 case EM_CYGNUS_MEP:
13748 return reloc_type == 4; /* R_MEP_32. */
13749 case EM_METAG:
13750 return reloc_type == 2; /* R_METAG_ADDR32. */
13751 case EM_MICROBLAZE:
13752 return reloc_type == 1; /* R_MICROBLAZE_32. */
13753 case EM_MIPS:
13754 return reloc_type == 2; /* R_MIPS_32. */
13755 case EM_MMIX:
13756 return reloc_type == 4; /* R_MMIX_32. */
13757 case EM_CYGNUS_MN10200:
13758 case EM_MN10200:
13759 return reloc_type == 1; /* R_MN10200_32. */
13760 case EM_CYGNUS_MN10300:
13761 case EM_MN10300:
13762 return reloc_type == 1; /* R_MN10300_32. */
13763 case EM_MOXIE:
13764 return reloc_type == 1; /* R_MOXIE_32. */
13765 case EM_MSP430_OLD:
13766 case EM_MSP430:
13767 return reloc_type == 1; /* R_MSP430_32 or R_MSP320_ABS32. */
13768 case EM_MT:
13769 return reloc_type == 2; /* R_MT_32. */
13770 case EM_NDS32:
13771 return reloc_type == 20; /* R_NDS32_RELA. */
13772 case EM_ALTERA_NIOS2:
13773 return reloc_type == 12; /* R_NIOS2_BFD_RELOC_32. */
13774 case EM_NIOS32:
13775 return reloc_type == 1; /* R_NIOS_32. */
13776 case EM_OR1K:
13777 return reloc_type == 1; /* R_OR1K_32. */
13778 case EM_PARISC:
13779 return (reloc_type == 1 /* R_PARISC_DIR32. */
13780 || reloc_type == 2 /* R_PARISC_DIR21L. */
13781 || reloc_type == 41); /* R_PARISC_SECREL32. */
13782 case EM_PJ:
13783 case EM_PJ_OLD:
13784 return reloc_type == 1; /* R_PJ_DATA_DIR32. */
13785 case EM_PPC64:
13786 return reloc_type == 1; /* R_PPC64_ADDR32. */
13787 case EM_PPC:
13788 return reloc_type == 1; /* R_PPC_ADDR32. */
13789 case EM_TI_PRU:
13790 return reloc_type == 11; /* R_PRU_BFD_RELOC_32. */
13791 case EM_RISCV:
13792 return reloc_type == 1; /* R_RISCV_32. */
13793 case EM_RL78:
13794 return reloc_type == 1; /* R_RL78_DIR32. */
13795 case EM_RX:
13796 return reloc_type == 1; /* R_RX_DIR32. */
13797 case EM_S370:
13798 return reloc_type == 1; /* R_I370_ADDR31. */
13799 case EM_S390_OLD:
13800 case EM_S390:
13801 return reloc_type == 4; /* R_S390_32. */
13802 case EM_SCORE:
13803 return reloc_type == 8; /* R_SCORE_ABS32. */
13804 case EM_SH:
13805 return reloc_type == 1; /* R_SH_DIR32. */
13806 case EM_SPARC32PLUS:
13807 case EM_SPARCV9:
13808 case EM_SPARC:
13809 return reloc_type == 3 /* R_SPARC_32. */
13810 || reloc_type == 23; /* R_SPARC_UA32. */
13811 case EM_SPU:
13812 return reloc_type == 6; /* R_SPU_ADDR32 */
13813 case EM_TI_C6000:
13814 return reloc_type == 1; /* R_C6000_ABS32. */
13815 case EM_TILEGX:
13816 return reloc_type == 2; /* R_TILEGX_32. */
13817 case EM_TILEPRO:
13818 return reloc_type == 1; /* R_TILEPRO_32. */
13819 case EM_CYGNUS_V850:
13820 case EM_V850:
13821 return reloc_type == 6; /* R_V850_ABS32. */
13822 case EM_V800:
13823 return reloc_type == 0x33; /* R_V810_WORD. */
13824 case EM_VAX:
13825 return reloc_type == 1; /* R_VAX_32. */
13826 case EM_VISIUM:
13827 return reloc_type == 3; /* R_VISIUM_32. */
13828 case EM_WEBASSEMBLY:
13829 return reloc_type == 1; /* R_WASM32_32. */
13830 case EM_X86_64:
13831 case EM_L1OM:
13832 case EM_K1OM:
13833 return reloc_type == 10; /* R_X86_64_32. */
13834 case EM_XC16X:
13835 case EM_C166:
13836 return reloc_type == 3; /* R_XC16C_ABS_32. */
13837 case EM_XGATE:
13838 return reloc_type == 4; /* R_XGATE_32. */
13839 case EM_XSTORMY16:
13840 return reloc_type == 1; /* R_XSTROMY16_32. */
13841 case EM_XTENSA_OLD:
13842 case EM_XTENSA:
13843 return reloc_type == 1; /* R_XTENSA_32. */
13844 case EM_Z80:
13845 return reloc_type == 6; /* R_Z80_32. */
13846 default:
13847 {
13848 static unsigned int prev_warn = 0;
13849
13850 /* Avoid repeating the same warning multiple times. */
13851 if (prev_warn != filedata->file_header.e_machine)
13852 error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
13853 filedata->file_header.e_machine);
13854 prev_warn = filedata->file_header.e_machine;
13855 return false;
13856 }
13857 }
13858 }
13859
13860 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13861 a 32-bit pc-relative RELA relocation used in DWARF debug sections. */
13862
13863 static bool
13864 is_32bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
13865 {
13866 switch (filedata->file_header.e_machine)
13867 /* Please keep this table alpha-sorted for ease of visual lookup. */
13868 {
13869 case EM_386:
13870 case EM_IAMCU:
13871 return reloc_type == 2; /* R_386_PC32. */
13872 case EM_68K:
13873 return reloc_type == 4; /* R_68K_PC32. */
13874 case EM_AARCH64:
13875 return reloc_type == 261; /* R_AARCH64_PREL32 */
13876 case EM_ADAPTEVA_EPIPHANY:
13877 return reloc_type == 6;
13878 case EM_ALPHA:
13879 return reloc_type == 10; /* R_ALPHA_SREL32. */
13880 case EM_ARC_COMPACT:
13881 case EM_ARC_COMPACT2:
13882 return reloc_type == 49; /* R_ARC_32_PCREL. */
13883 case EM_ARM:
13884 return reloc_type == 3; /* R_ARM_REL32 */
13885 case EM_AVR_OLD:
13886 case EM_AVR:
13887 return reloc_type == 36; /* R_AVR_32_PCREL. */
13888 case EM_MICROBLAZE:
13889 return reloc_type == 2; /* R_MICROBLAZE_32_PCREL. */
13890 case EM_OR1K:
13891 return reloc_type == 9; /* R_OR1K_32_PCREL. */
13892 case EM_PARISC:
13893 return reloc_type == 9; /* R_PARISC_PCREL32. */
13894 case EM_PPC:
13895 return reloc_type == 26; /* R_PPC_REL32. */
13896 case EM_PPC64:
13897 return reloc_type == 26; /* R_PPC64_REL32. */
13898 case EM_RISCV:
13899 return reloc_type == 57; /* R_RISCV_32_PCREL. */
13900 case EM_S390_OLD:
13901 case EM_S390:
13902 return reloc_type == 5; /* R_390_PC32. */
13903 case EM_SH:
13904 return reloc_type == 2; /* R_SH_REL32. */
13905 case EM_SPARC32PLUS:
13906 case EM_SPARCV9:
13907 case EM_SPARC:
13908 return reloc_type == 6; /* R_SPARC_DISP32. */
13909 case EM_SPU:
13910 return reloc_type == 13; /* R_SPU_REL32. */
13911 case EM_TILEGX:
13912 return reloc_type == 6; /* R_TILEGX_32_PCREL. */
13913 case EM_TILEPRO:
13914 return reloc_type == 4; /* R_TILEPRO_32_PCREL. */
13915 case EM_VISIUM:
13916 return reloc_type == 6; /* R_VISIUM_32_PCREL */
13917 case EM_X86_64:
13918 case EM_L1OM:
13919 case EM_K1OM:
13920 return reloc_type == 2; /* R_X86_64_PC32. */
13921 case EM_VAX:
13922 return reloc_type == 4; /* R_VAX_PCREL32. */
13923 case EM_XTENSA_OLD:
13924 case EM_XTENSA:
13925 return reloc_type == 14; /* R_XTENSA_32_PCREL. */
13926 default:
13927 /* Do not abort or issue an error message here. Not all targets use
13928 pc-relative 32-bit relocs in their DWARF debug information and we
13929 have already tested for target coverage in is_32bit_abs_reloc. A
13930 more helpful warning message will be generated by apply_relocations
13931 anyway, so just return. */
13932 return false;
13933 }
13934 }
13935
13936 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13937 a 64-bit absolute RELA relocation used in DWARF debug sections. */
13938
13939 static bool
13940 is_64bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13941 {
13942 switch (filedata->file_header.e_machine)
13943 {
13944 case EM_AARCH64:
13945 return reloc_type == 257; /* R_AARCH64_ABS64. */
13946 case EM_ALPHA:
13947 return reloc_type == 2; /* R_ALPHA_REFQUAD. */
13948 case EM_IA_64:
13949 return (reloc_type == 0x26 /* R_IA64_DIR64MSB. */
13950 || reloc_type == 0x27 /* R_IA64_DIR64LSB. */);
13951 case EM_LOONGARCH:
13952 return reloc_type == 2; /* R_LARCH_64 */
13953 case EM_PARISC:
13954 return reloc_type == 80; /* R_PARISC_DIR64. */
13955 case EM_PPC64:
13956 return reloc_type == 38; /* R_PPC64_ADDR64. */
13957 case EM_RISCV:
13958 return reloc_type == 2; /* R_RISCV_64. */
13959 case EM_SPARC32PLUS:
13960 case EM_SPARCV9:
13961 case EM_SPARC:
13962 return reloc_type == 32 /* R_SPARC_64. */
13963 || reloc_type == 54; /* R_SPARC_UA64. */
13964 case EM_X86_64:
13965 case EM_L1OM:
13966 case EM_K1OM:
13967 return reloc_type == 1; /* R_X86_64_64. */
13968 case EM_S390_OLD:
13969 case EM_S390:
13970 return reloc_type == 22; /* R_S390_64. */
13971 case EM_TILEGX:
13972 return reloc_type == 1; /* R_TILEGX_64. */
13973 case EM_MIPS:
13974 return reloc_type == 18; /* R_MIPS_64. */
13975 default:
13976 return false;
13977 }
13978 }
13979
13980 /* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
13981 a 64-bit pc-relative RELA relocation used in DWARF debug sections. */
13982
13983 static bool
13984 is_64bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
13985 {
13986 switch (filedata->file_header.e_machine)
13987 {
13988 case EM_AARCH64:
13989 return reloc_type == 260; /* R_AARCH64_PREL64. */
13990 case EM_ALPHA:
13991 return reloc_type == 11; /* R_ALPHA_SREL64. */
13992 case EM_IA_64:
13993 return (reloc_type == 0x4e /* R_IA64_PCREL64MSB. */
13994 || reloc_type == 0x4f /* R_IA64_PCREL64LSB. */);
13995 case EM_PARISC:
13996 return reloc_type == 72; /* R_PARISC_PCREL64. */
13997 case EM_PPC64:
13998 return reloc_type == 44; /* R_PPC64_REL64. */
13999 case EM_SPARC32PLUS:
14000 case EM_SPARCV9:
14001 case EM_SPARC:
14002 return reloc_type == 46; /* R_SPARC_DISP64. */
14003 case EM_X86_64:
14004 case EM_L1OM:
14005 case EM_K1OM:
14006 return reloc_type == 24; /* R_X86_64_PC64. */
14007 case EM_S390_OLD:
14008 case EM_S390:
14009 return reloc_type == 23; /* R_S390_PC64. */
14010 case EM_TILEGX:
14011 return reloc_type == 5; /* R_TILEGX_64_PCREL. */
14012 default:
14013 return false;
14014 }
14015 }
14016
14017 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14018 a 24-bit absolute RELA relocation used in DWARF debug sections. */
14019
14020 static bool
14021 is_24bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14022 {
14023 switch (filedata->file_header.e_machine)
14024 {
14025 case EM_CYGNUS_MN10200:
14026 case EM_MN10200:
14027 return reloc_type == 4; /* R_MN10200_24. */
14028 case EM_FT32:
14029 return reloc_type == 5; /* R_FT32_20. */
14030 case EM_Z80:
14031 return reloc_type == 5; /* R_Z80_24. */
14032 default:
14033 return false;
14034 }
14035 }
14036
14037 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14038 a 16-bit absolute RELA relocation used in DWARF debug sections. */
14039
14040 static bool
14041 is_16bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14042 {
14043 /* Please keep this table alpha-sorted for ease of visual lookup. */
14044 switch (filedata->file_header.e_machine)
14045 {
14046 case EM_ARC:
14047 case EM_ARC_COMPACT:
14048 case EM_ARC_COMPACT2:
14049 return reloc_type == 2; /* R_ARC_16. */
14050 case EM_ADAPTEVA_EPIPHANY:
14051 return reloc_type == 5;
14052 case EM_AVR_OLD:
14053 case EM_AVR:
14054 return reloc_type == 4; /* R_AVR_16. */
14055 case EM_CYGNUS_D10V:
14056 case EM_D10V:
14057 return reloc_type == 3; /* R_D10V_16. */
14058 case EM_FT32:
14059 return reloc_type == 2; /* R_FT32_16. */
14060 case EM_H8S:
14061 case EM_H8_300:
14062 case EM_H8_300H:
14063 return reloc_type == R_H8_DIR16;
14064 case EM_IP2K_OLD:
14065 case EM_IP2K:
14066 return reloc_type == 1; /* R_IP2K_16. */
14067 case EM_M32C_OLD:
14068 case EM_M32C:
14069 return reloc_type == 1; /* R_M32C_16 */
14070 case EM_CYGNUS_MN10200:
14071 case EM_MN10200:
14072 return reloc_type == 2; /* R_MN10200_16. */
14073 case EM_CYGNUS_MN10300:
14074 case EM_MN10300:
14075 return reloc_type == 2; /* R_MN10300_16. */
14076 case EM_MSP430:
14077 if (uses_msp430x_relocs (filedata))
14078 return reloc_type == 2; /* R_MSP430_ABS16. */
14079 /* Fall through. */
14080 case EM_MSP430_OLD:
14081 return reloc_type == 5; /* R_MSP430_16_BYTE. */
14082 case EM_NDS32:
14083 return reloc_type == 19; /* R_NDS32_RELA. */
14084 case EM_ALTERA_NIOS2:
14085 return reloc_type == 13; /* R_NIOS2_BFD_RELOC_16. */
14086 case EM_NIOS32:
14087 return reloc_type == 9; /* R_NIOS_16. */
14088 case EM_OR1K:
14089 return reloc_type == 2; /* R_OR1K_16. */
14090 case EM_RISCV:
14091 return reloc_type == 55; /* R_RISCV_SET16. */
14092 case EM_TI_PRU:
14093 return reloc_type == 8; /* R_PRU_BFD_RELOC_16. */
14094 case EM_TI_C6000:
14095 return reloc_type == 2; /* R_C6000_ABS16. */
14096 case EM_VISIUM:
14097 return reloc_type == 2; /* R_VISIUM_16. */
14098 case EM_XC16X:
14099 case EM_C166:
14100 return reloc_type == 2; /* R_XC16C_ABS_16. */
14101 case EM_XGATE:
14102 return reloc_type == 3; /* R_XGATE_16. */
14103 case EM_Z80:
14104 return reloc_type == 4; /* R_Z80_16. */
14105 default:
14106 return false;
14107 }
14108 }
14109
14110 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14111 a 8-bit absolute RELA relocation used in DWARF debug sections. */
14112
14113 static bool
14114 is_8bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14115 {
14116 switch (filedata->file_header.e_machine)
14117 {
14118 case EM_RISCV:
14119 return reloc_type == 54; /* R_RISCV_SET8. */
14120 case EM_Z80:
14121 return reloc_type == 1; /* R_Z80_8. */
14122 default:
14123 return false;
14124 }
14125 }
14126
14127 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14128 a 6-bit absolute RELA relocation used in DWARF debug sections. */
14129
14130 static bool
14131 is_6bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14132 {
14133 switch (filedata->file_header.e_machine)
14134 {
14135 case EM_RISCV:
14136 return reloc_type == 53; /* R_RISCV_SET6. */
14137 default:
14138 return false;
14139 }
14140 }
14141
14142 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14143 a 32-bit inplace add RELA relocation used in DWARF debug sections. */
14144
14145 static bool
14146 is_32bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14147 {
14148 /* Please keep this table alpha-sorted for ease of visual lookup. */
14149 switch (filedata->file_header.e_machine)
14150 {
14151 case EM_RISCV:
14152 return reloc_type == 35; /* R_RISCV_ADD32. */
14153 default:
14154 return false;
14155 }
14156 }
14157
14158 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14159 a 32-bit inplace sub RELA relocation used in DWARF debug sections. */
14160
14161 static bool
14162 is_32bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14163 {
14164 /* Please keep this table alpha-sorted for ease of visual lookup. */
14165 switch (filedata->file_header.e_machine)
14166 {
14167 case EM_RISCV:
14168 return reloc_type == 39; /* R_RISCV_SUB32. */
14169 default:
14170 return false;
14171 }
14172 }
14173
14174 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14175 a 64-bit inplace add RELA relocation used in DWARF debug sections. */
14176
14177 static bool
14178 is_64bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14179 {
14180 /* Please keep this table alpha-sorted for ease of visual lookup. */
14181 switch (filedata->file_header.e_machine)
14182 {
14183 case EM_RISCV:
14184 return reloc_type == 36; /* R_RISCV_ADD64. */
14185 default:
14186 return false;
14187 }
14188 }
14189
14190 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14191 a 64-bit inplace sub RELA relocation used in DWARF debug sections. */
14192
14193 static bool
14194 is_64bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14195 {
14196 /* Please keep this table alpha-sorted for ease of visual lookup. */
14197 switch (filedata->file_header.e_machine)
14198 {
14199 case EM_RISCV:
14200 return reloc_type == 40; /* R_RISCV_SUB64. */
14201 default:
14202 return false;
14203 }
14204 }
14205
14206 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14207 a 16-bit inplace add RELA relocation used in DWARF debug sections. */
14208
14209 static bool
14210 is_16bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14211 {
14212 /* Please keep this table alpha-sorted for ease of visual lookup. */
14213 switch (filedata->file_header.e_machine)
14214 {
14215 case EM_RISCV:
14216 return reloc_type == 34; /* R_RISCV_ADD16. */
14217 default:
14218 return false;
14219 }
14220 }
14221
14222 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14223 a 16-bit inplace sub RELA relocation used in DWARF debug sections. */
14224
14225 static bool
14226 is_16bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14227 {
14228 /* Please keep this table alpha-sorted for ease of visual lookup. */
14229 switch (filedata->file_header.e_machine)
14230 {
14231 case EM_RISCV:
14232 return reloc_type == 38; /* R_RISCV_SUB16. */
14233 default:
14234 return false;
14235 }
14236 }
14237
14238 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14239 a 8-bit inplace add RELA relocation used in DWARF debug sections. */
14240
14241 static bool
14242 is_8bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14243 {
14244 /* Please keep this table alpha-sorted for ease of visual lookup. */
14245 switch (filedata->file_header.e_machine)
14246 {
14247 case EM_RISCV:
14248 return reloc_type == 33; /* R_RISCV_ADD8. */
14249 default:
14250 return false;
14251 }
14252 }
14253
14254 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14255 a 8-bit inplace sub RELA relocation used in DWARF debug sections. */
14256
14257 static bool
14258 is_8bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14259 {
14260 /* Please keep this table alpha-sorted for ease of visual lookup. */
14261 switch (filedata->file_header.e_machine)
14262 {
14263 case EM_RISCV:
14264 return reloc_type == 37; /* R_RISCV_SUB8. */
14265 default:
14266 return false;
14267 }
14268 }
14269
14270 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14271 a 6-bit inplace sub RELA relocation used in DWARF debug sections. */
14272
14273 static bool
14274 is_6bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14275 {
14276 switch (filedata->file_header.e_machine)
14277 {
14278 case EM_RISCV:
14279 return reloc_type == 52; /* R_RISCV_SUB6. */
14280 default:
14281 return false;
14282 }
14283 }
14284
14285 /* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
14286 relocation entries (possibly formerly used for SHT_GROUP sections). */
14287
14288 static bool
14289 is_none_reloc (Filedata * filedata, unsigned int reloc_type)
14290 {
14291 switch (filedata->file_header.e_machine)
14292 {
14293 case EM_386: /* R_386_NONE. */
14294 case EM_68K: /* R_68K_NONE. */
14295 case EM_ADAPTEVA_EPIPHANY:
14296 case EM_ALPHA: /* R_ALPHA_NONE. */
14297 case EM_ALTERA_NIOS2: /* R_NIOS2_NONE. */
14298 case EM_ARC: /* R_ARC_NONE. */
14299 case EM_ARC_COMPACT2: /* R_ARC_NONE. */
14300 case EM_ARC_COMPACT: /* R_ARC_NONE. */
14301 case EM_ARM: /* R_ARM_NONE. */
14302 case EM_C166: /* R_XC16X_NONE. */
14303 case EM_CRIS: /* R_CRIS_NONE. */
14304 case EM_FT32: /* R_FT32_NONE. */
14305 case EM_IA_64: /* R_IA64_NONE. */
14306 case EM_K1OM: /* R_X86_64_NONE. */
14307 case EM_L1OM: /* R_X86_64_NONE. */
14308 case EM_M32R: /* R_M32R_NONE. */
14309 case EM_MIPS: /* R_MIPS_NONE. */
14310 case EM_MN10300: /* R_MN10300_NONE. */
14311 case EM_MOXIE: /* R_MOXIE_NONE. */
14312 case EM_NIOS32: /* R_NIOS_NONE. */
14313 case EM_OR1K: /* R_OR1K_NONE. */
14314 case EM_PARISC: /* R_PARISC_NONE. */
14315 case EM_PPC64: /* R_PPC64_NONE. */
14316 case EM_PPC: /* R_PPC_NONE. */
14317 case EM_RISCV: /* R_RISCV_NONE. */
14318 case EM_S390: /* R_390_NONE. */
14319 case EM_S390_OLD:
14320 case EM_SH: /* R_SH_NONE. */
14321 case EM_SPARC32PLUS:
14322 case EM_SPARC: /* R_SPARC_NONE. */
14323 case EM_SPARCV9:
14324 case EM_TILEGX: /* R_TILEGX_NONE. */
14325 case EM_TILEPRO: /* R_TILEPRO_NONE. */
14326 case EM_TI_C6000:/* R_C6000_NONE. */
14327 case EM_X86_64: /* R_X86_64_NONE. */
14328 case EM_XC16X:
14329 case EM_Z80: /* R_Z80_NONE. */
14330 case EM_WEBASSEMBLY: /* R_WASM32_NONE. */
14331 return reloc_type == 0;
14332
14333 case EM_AARCH64:
14334 return reloc_type == 0 || reloc_type == 256;
14335 case EM_AVR_OLD:
14336 case EM_AVR:
14337 return (reloc_type == 0 /* R_AVR_NONE. */
14338 || reloc_type == 30 /* R_AVR_DIFF8. */
14339 || reloc_type == 31 /* R_AVR_DIFF16. */
14340 || reloc_type == 32 /* R_AVR_DIFF32. */);
14341 case EM_METAG:
14342 return reloc_type == 3; /* R_METAG_NONE. */
14343 case EM_NDS32:
14344 return (reloc_type == 0 /* R_XTENSA_NONE. */
14345 || reloc_type == 204 /* R_NDS32_DIFF8. */
14346 || reloc_type == 205 /* R_NDS32_DIFF16. */
14347 || reloc_type == 206 /* R_NDS32_DIFF32. */
14348 || reloc_type == 207 /* R_NDS32_ULEB128. */);
14349 case EM_TI_PRU:
14350 return (reloc_type == 0 /* R_PRU_NONE. */
14351 || reloc_type == 65 /* R_PRU_DIFF8. */
14352 || reloc_type == 66 /* R_PRU_DIFF16. */
14353 || reloc_type == 67 /* R_PRU_DIFF32. */);
14354 case EM_XTENSA_OLD:
14355 case EM_XTENSA:
14356 return (reloc_type == 0 /* R_XTENSA_NONE. */
14357 || reloc_type == 17 /* R_XTENSA_DIFF8. */
14358 || reloc_type == 18 /* R_XTENSA_DIFF16. */
14359 || reloc_type == 19 /* R_XTENSA_DIFF32. */
14360 || reloc_type == 57 /* R_XTENSA_PDIFF8. */
14361 || reloc_type == 58 /* R_XTENSA_PDIFF16. */
14362 || reloc_type == 59 /* R_XTENSA_PDIFF32. */
14363 || reloc_type == 60 /* R_XTENSA_NDIFF8. */
14364 || reloc_type == 61 /* R_XTENSA_NDIFF16. */
14365 || reloc_type == 62 /* R_XTENSA_NDIFF32. */);
14366 }
14367 return false;
14368 }
14369
14370 /* Returns TRUE if there is a relocation against
14371 section NAME at OFFSET bytes. */
14372
14373 bool
14374 reloc_at (struct dwarf_section * dsec, dwarf_vma offset)
14375 {
14376 Elf_Internal_Rela * relocs;
14377 Elf_Internal_Rela * rp;
14378
14379 if (dsec == NULL || dsec->reloc_info == NULL)
14380 return false;
14381
14382 relocs = (Elf_Internal_Rela *) dsec->reloc_info;
14383
14384 for (rp = relocs; rp < relocs + dsec->num_relocs; ++rp)
14385 if (rp->r_offset == offset)
14386 return true;
14387
14388 return false;
14389 }
14390
14391 /* Apply relocations to a section.
14392 Returns TRUE upon success, FALSE otherwise.
14393 If RELOCS_RETURN is non-NULL then it is set to point to the loaded relocs.
14394 It is then the caller's responsibility to free them. NUM_RELOCS_RETURN
14395 will be set to the number of relocs loaded.
14396
14397 Note: So far support has been added only for those relocations
14398 which can be found in debug sections. FIXME: Add support for
14399 more relocations ? */
14400
14401 static bool
14402 apply_relocations (Filedata * filedata,
14403 const Elf_Internal_Shdr * section,
14404 unsigned char * start,
14405 bfd_size_type size,
14406 void ** relocs_return,
14407 unsigned long * num_relocs_return)
14408 {
14409 Elf_Internal_Shdr * relsec;
14410 unsigned char * end = start + size;
14411
14412 if (relocs_return != NULL)
14413 {
14414 * (Elf_Internal_Rela **) relocs_return = NULL;
14415 * num_relocs_return = 0;
14416 }
14417
14418 if (filedata->file_header.e_type != ET_REL)
14419 /* No relocs to apply. */
14420 return true;
14421
14422 /* Find the reloc section associated with the section. */
14423 for (relsec = filedata->section_headers;
14424 relsec < filedata->section_headers + filedata->file_header.e_shnum;
14425 ++relsec)
14426 {
14427 bool is_rela;
14428 unsigned long num_relocs;
14429 Elf_Internal_Rela * relocs;
14430 Elf_Internal_Rela * rp;
14431 Elf_Internal_Shdr * symsec;
14432 Elf_Internal_Sym * symtab;
14433 unsigned long num_syms;
14434 Elf_Internal_Sym * sym;
14435
14436 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
14437 || relsec->sh_info >= filedata->file_header.e_shnum
14438 || filedata->section_headers + relsec->sh_info != section
14439 || relsec->sh_size == 0
14440 || relsec->sh_link >= filedata->file_header.e_shnum)
14441 continue;
14442
14443 symsec = filedata->section_headers + relsec->sh_link;
14444 if (symsec->sh_type != SHT_SYMTAB
14445 && symsec->sh_type != SHT_DYNSYM)
14446 return false;
14447
14448 is_rela = relsec->sh_type == SHT_RELA;
14449
14450 if (is_rela)
14451 {
14452 if (!slurp_rela_relocs (filedata, relsec->sh_offset,
14453 relsec->sh_size, & relocs, & num_relocs))
14454 return false;
14455 }
14456 else
14457 {
14458 if (!slurp_rel_relocs (filedata, relsec->sh_offset,
14459 relsec->sh_size, & relocs, & num_relocs))
14460 return false;
14461 }
14462
14463 /* SH uses RELA but uses in place value instead of the addend field. */
14464 if (filedata->file_header.e_machine == EM_SH)
14465 is_rela = false;
14466
14467 symtab = get_elf_symbols (filedata, symsec, & num_syms);
14468
14469 for (rp = relocs; rp < relocs + num_relocs; ++rp)
14470 {
14471 bfd_vma addend;
14472 unsigned int reloc_type;
14473 unsigned int reloc_size;
14474 bool reloc_inplace = false;
14475 bool reloc_subtract = false;
14476 unsigned char *rloc;
14477 unsigned long sym_index;
14478
14479 reloc_type = get_reloc_type (filedata, rp->r_info);
14480
14481 if (target_specific_reloc_handling (filedata, rp, start, end, symtab, num_syms))
14482 continue;
14483 else if (is_none_reloc (filedata, reloc_type))
14484 continue;
14485 else if (is_32bit_abs_reloc (filedata, reloc_type)
14486 || is_32bit_pcrel_reloc (filedata, reloc_type))
14487 reloc_size = 4;
14488 else if (is_64bit_abs_reloc (filedata, reloc_type)
14489 || is_64bit_pcrel_reloc (filedata, reloc_type))
14490 reloc_size = 8;
14491 else if (is_24bit_abs_reloc (filedata, reloc_type))
14492 reloc_size = 3;
14493 else if (is_16bit_abs_reloc (filedata, reloc_type))
14494 reloc_size = 2;
14495 else if (is_8bit_abs_reloc (filedata, reloc_type)
14496 || is_6bit_abs_reloc (filedata, reloc_type))
14497 reloc_size = 1;
14498 else if ((reloc_subtract = is_32bit_inplace_sub_reloc (filedata,
14499 reloc_type))
14500 || is_32bit_inplace_add_reloc (filedata, reloc_type))
14501 {
14502 reloc_size = 4;
14503 reloc_inplace = true;
14504 }
14505 else if ((reloc_subtract = is_64bit_inplace_sub_reloc (filedata,
14506 reloc_type))
14507 || is_64bit_inplace_add_reloc (filedata, reloc_type))
14508 {
14509 reloc_size = 8;
14510 reloc_inplace = true;
14511 }
14512 else if ((reloc_subtract = is_16bit_inplace_sub_reloc (filedata,
14513 reloc_type))
14514 || is_16bit_inplace_add_reloc (filedata, reloc_type))
14515 {
14516 reloc_size = 2;
14517 reloc_inplace = true;
14518 }
14519 else if ((reloc_subtract = is_8bit_inplace_sub_reloc (filedata,
14520 reloc_type))
14521 || is_8bit_inplace_add_reloc (filedata, reloc_type))
14522 {
14523 reloc_size = 1;
14524 reloc_inplace = true;
14525 }
14526 else if ((reloc_subtract = is_6bit_inplace_sub_reloc (filedata,
14527 reloc_type)))
14528 {
14529 reloc_size = 1;
14530 reloc_inplace = true;
14531 }
14532 else
14533 {
14534 static unsigned int prev_reloc = 0;
14535
14536 if (reloc_type != prev_reloc)
14537 warn (_("unable to apply unsupported reloc type %d to section %s\n"),
14538 reloc_type, printable_section_name (filedata, section));
14539 prev_reloc = reloc_type;
14540 continue;
14541 }
14542
14543 rloc = start + rp->r_offset;
14544 if (!IN_RANGE (start, end, rloc, reloc_size))
14545 {
14546 warn (_("skipping invalid relocation offset 0x%lx in section %s\n"),
14547 (unsigned long) rp->r_offset,
14548 printable_section_name (filedata, section));
14549 continue;
14550 }
14551
14552 sym_index = (unsigned long) get_reloc_symindex (rp->r_info);
14553 if (sym_index >= num_syms)
14554 {
14555 warn (_("skipping invalid relocation symbol index 0x%lx in section %s\n"),
14556 sym_index, printable_section_name (filedata, section));
14557 continue;
14558 }
14559 sym = symtab + sym_index;
14560
14561 /* If the reloc has a symbol associated with it,
14562 make sure that it is of an appropriate type.
14563
14564 Relocations against symbols without type can happen.
14565 Gcc -feliminate-dwarf2-dups may generate symbols
14566 without type for debug info.
14567
14568 Icc generates relocations against function symbols
14569 instead of local labels.
14570
14571 Relocations against object symbols can happen, eg when
14572 referencing a global array. For an example of this see
14573 the _clz.o binary in libgcc.a. */
14574 if (sym != symtab
14575 && ELF_ST_TYPE (sym->st_info) != STT_COMMON
14576 && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
14577 {
14578 warn (_("skipping unexpected symbol type %s in section %s relocation %ld\n"),
14579 get_symbol_type (filedata, ELF_ST_TYPE (sym->st_info)),
14580 printable_section_name (filedata, relsec),
14581 (long int)(rp - relocs));
14582 continue;
14583 }
14584
14585 addend = 0;
14586 if (is_rela)
14587 addend += rp->r_addend;
14588 /* R_XTENSA_32, R_PJ_DATA_DIR32 and R_D30V_32_NORMAL are
14589 partial_inplace. */
14590 if (!is_rela
14591 || (filedata->file_header.e_machine == EM_XTENSA
14592 && reloc_type == 1)
14593 || ((filedata->file_header.e_machine == EM_PJ
14594 || filedata->file_header.e_machine == EM_PJ_OLD)
14595 && reloc_type == 1)
14596 || ((filedata->file_header.e_machine == EM_D30V
14597 || filedata->file_header.e_machine == EM_CYGNUS_D30V)
14598 && reloc_type == 12)
14599 || reloc_inplace)
14600 {
14601 if (is_6bit_inplace_sub_reloc (filedata, reloc_type))
14602 addend += byte_get (rloc, reloc_size) & 0x3f;
14603 else
14604 addend += byte_get (rloc, reloc_size);
14605 }
14606
14607 if (is_32bit_pcrel_reloc (filedata, reloc_type)
14608 || is_64bit_pcrel_reloc (filedata, reloc_type))
14609 {
14610 /* On HPPA, all pc-relative relocations are biased by 8. */
14611 if (filedata->file_header.e_machine == EM_PARISC)
14612 addend -= 8;
14613 byte_put (rloc, (addend + sym->st_value) - rp->r_offset,
14614 reloc_size);
14615 }
14616 else if (is_6bit_abs_reloc (filedata, reloc_type)
14617 || is_6bit_inplace_sub_reloc (filedata, reloc_type))
14618 {
14619 if (reloc_subtract)
14620 addend -= sym->st_value;
14621 else
14622 addend += sym->st_value;
14623 addend = (addend & 0x3f) | (byte_get (rloc, reloc_size) & 0xc0);
14624 byte_put (rloc, addend, reloc_size);
14625 }
14626 else if (reloc_subtract)
14627 byte_put (rloc, addend - sym->st_value, reloc_size);
14628 else
14629 byte_put (rloc, addend + sym->st_value, reloc_size);
14630 }
14631
14632 free (symtab);
14633 /* Let the target specific reloc processing code know that
14634 we have finished with these relocs. */
14635 target_specific_reloc_handling (filedata, NULL, NULL, NULL, NULL, 0);
14636
14637 if (relocs_return)
14638 {
14639 * (Elf_Internal_Rela **) relocs_return = relocs;
14640 * num_relocs_return = num_relocs;
14641 }
14642 else
14643 free (relocs);
14644
14645 break;
14646 }
14647
14648 return true;
14649 }
14650
14651 #ifdef SUPPORT_DISASSEMBLY
14652 static bool
14653 disassemble_section (Elf_Internal_Shdr * section, Filedata * filedata)
14654 {
14655 printf (_("\nAssembly dump of section %s\n"), printable_section_name (filedata, section));
14656
14657 /* FIXME: XXX -- to be done --- XXX */
14658
14659 return true;
14660 }
14661 #endif
14662
14663 /* Reads in the contents of SECTION from FILE, returning a pointer
14664 to a malloc'ed buffer or NULL if something went wrong. */
14665
14666 static char *
14667 get_section_contents (Elf_Internal_Shdr * section, Filedata * filedata)
14668 {
14669 bfd_size_type num_bytes = section->sh_size;
14670
14671 if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
14672 {
14673 printf (_("Section '%s' has no data to dump.\n"),
14674 printable_section_name (filedata, section));
14675 return NULL;
14676 }
14677
14678 return (char *) get_data (NULL, filedata, section->sh_offset, 1, num_bytes,
14679 _("section contents"));
14680 }
14681
14682 /* Uncompresses a section that was compressed using zlib, in place. */
14683
14684 static bool
14685 uncompress_section_contents (unsigned char ** buffer,
14686 dwarf_size_type uncompressed_size,
14687 dwarf_size_type * size)
14688 {
14689 dwarf_size_type compressed_size = *size;
14690 unsigned char * compressed_buffer = *buffer;
14691 unsigned char * uncompressed_buffer;
14692 z_stream strm;
14693 int rc;
14694
14695 /* It is possible the section consists of several compressed
14696 buffers concatenated together, so we uncompress in a loop. */
14697 /* PR 18313: The state field in the z_stream structure is supposed
14698 to be invisible to the user (ie us), but some compilers will
14699 still complain about it being used without initialisation. So
14700 we first zero the entire z_stream structure and then set the fields
14701 that we need. */
14702 memset (& strm, 0, sizeof strm);
14703 strm.avail_in = compressed_size;
14704 strm.next_in = (Bytef *) compressed_buffer;
14705 strm.avail_out = uncompressed_size;
14706 uncompressed_buffer = (unsigned char *) xmalloc (uncompressed_size);
14707
14708 rc = inflateInit (& strm);
14709 while (strm.avail_in > 0)
14710 {
14711 if (rc != Z_OK)
14712 break;
14713 strm.next_out = ((Bytef *) uncompressed_buffer
14714 + (uncompressed_size - strm.avail_out));
14715 rc = inflate (&strm, Z_FINISH);
14716 if (rc != Z_STREAM_END)
14717 break;
14718 rc = inflateReset (& strm);
14719 }
14720 if (inflateEnd (& strm) != Z_OK
14721 || rc != Z_OK
14722 || strm.avail_out != 0)
14723 goto fail;
14724
14725 *buffer = uncompressed_buffer;
14726 *size = uncompressed_size;
14727 return true;
14728
14729 fail:
14730 free (uncompressed_buffer);
14731 /* Indicate decompression failure. */
14732 *buffer = NULL;
14733 return false;
14734 }
14735
14736 static bool
14737 dump_section_as_strings (Elf_Internal_Shdr * section, Filedata * filedata)
14738 {
14739 Elf_Internal_Shdr *relsec;
14740 bfd_size_type num_bytes;
14741 unsigned char *data;
14742 unsigned char *end;
14743 unsigned char *real_start;
14744 unsigned char *start;
14745 bool some_strings_shown;
14746
14747 real_start = start = (unsigned char *) get_section_contents (section, filedata);
14748 if (start == NULL)
14749 /* PR 21820: Do not fail if the section was empty. */
14750 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
14751
14752 num_bytes = section->sh_size;
14753
14754 if (filedata->is_separate)
14755 printf (_("\nString dump of section '%s' in linked file %s:\n"),
14756 printable_section_name (filedata, section),
14757 filedata->file_name);
14758 else
14759 printf (_("\nString dump of section '%s':\n"),
14760 printable_section_name (filedata, section));
14761
14762 if (decompress_dumps)
14763 {
14764 dwarf_size_type new_size = num_bytes;
14765 dwarf_size_type uncompressed_size = 0;
14766
14767 if ((section->sh_flags & SHF_COMPRESSED) != 0)
14768 {
14769 Elf_Internal_Chdr chdr;
14770 unsigned int compression_header_size
14771 = get_compression_header (& chdr, (unsigned char *) start,
14772 num_bytes);
14773 if (compression_header_size == 0)
14774 /* An error message will have already been generated
14775 by get_compression_header. */
14776 goto error_out;
14777
14778 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
14779 {
14780 warn (_("section '%s' has unsupported compress type: %d\n"),
14781 printable_section_name (filedata, section), chdr.ch_type);
14782 goto error_out;
14783 }
14784 uncompressed_size = chdr.ch_size;
14785 start += compression_header_size;
14786 new_size -= compression_header_size;
14787 }
14788 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
14789 {
14790 /* Read the zlib header. In this case, it should be "ZLIB"
14791 followed by the uncompressed section size, 8 bytes in
14792 big-endian order. */
14793 uncompressed_size = start[4]; uncompressed_size <<= 8;
14794 uncompressed_size += start[5]; uncompressed_size <<= 8;
14795 uncompressed_size += start[6]; uncompressed_size <<= 8;
14796 uncompressed_size += start[7]; uncompressed_size <<= 8;
14797 uncompressed_size += start[8]; uncompressed_size <<= 8;
14798 uncompressed_size += start[9]; uncompressed_size <<= 8;
14799 uncompressed_size += start[10]; uncompressed_size <<= 8;
14800 uncompressed_size += start[11];
14801 start += 12;
14802 new_size -= 12;
14803 }
14804
14805 if (uncompressed_size)
14806 {
14807 if (uncompress_section_contents (& start,
14808 uncompressed_size, & new_size))
14809 num_bytes = new_size;
14810 else
14811 {
14812 error (_("Unable to decompress section %s\n"),
14813 printable_section_name (filedata, section));
14814 goto error_out;
14815 }
14816 }
14817 else
14818 start = real_start;
14819 }
14820
14821 /* If the section being dumped has relocations against it the user might
14822 be expecting these relocations to have been applied. Check for this
14823 case and issue a warning message in order to avoid confusion.
14824 FIXME: Maybe we ought to have an option that dumps a section with
14825 relocs applied ? */
14826 for (relsec = filedata->section_headers;
14827 relsec < filedata->section_headers + filedata->file_header.e_shnum;
14828 ++relsec)
14829 {
14830 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
14831 || relsec->sh_info >= filedata->file_header.e_shnum
14832 || filedata->section_headers + relsec->sh_info != section
14833 || relsec->sh_size == 0
14834 || relsec->sh_link >= filedata->file_header.e_shnum)
14835 continue;
14836
14837 printf (_(" Note: This section has relocations against it, but these have NOT been applied to this dump.\n"));
14838 break;
14839 }
14840
14841 data = start;
14842 end = start + num_bytes;
14843 some_strings_shown = false;
14844
14845 #ifdef HAVE_MBSTATE_T
14846 mbstate_t state;
14847 /* Initialise the multibyte conversion state. */
14848 memset (& state, 0, sizeof (state));
14849 #endif
14850
14851 bool continuing = false;
14852
14853 while (data < end)
14854 {
14855 while (!ISPRINT (* data))
14856 if (++ data >= end)
14857 break;
14858
14859 if (data < end)
14860 {
14861 size_t maxlen = end - data;
14862
14863 if (continuing)
14864 {
14865 printf (" ");
14866 continuing = false;
14867 }
14868 else
14869 {
14870 printf (" [%6lx] ", (unsigned long) (data - start));
14871 }
14872
14873 if (maxlen > 0)
14874 {
14875 char c = 0;
14876
14877 while (maxlen)
14878 {
14879 c = *data++;
14880
14881 if (c == 0)
14882 break;
14883
14884 /* PR 25543: Treat new-lines as string-ending characters. */
14885 if (c == '\n')
14886 {
14887 printf ("\\n\n");
14888 if (*data != 0)
14889 continuing = true;
14890 break;
14891 }
14892
14893 /* Do not print control characters directly as they can affect terminal
14894 settings. Such characters usually appear in the names generated
14895 by the assembler for local labels. */
14896 if (ISCNTRL (c))
14897 {
14898 printf ("^%c", c + 0x40);
14899 }
14900 else if (ISPRINT (c))
14901 {
14902 putchar (c);
14903 }
14904 else
14905 {
14906 size_t n;
14907 #ifdef HAVE_MBSTATE_T
14908 wchar_t w;
14909 #endif
14910 /* Let printf do the hard work of displaying multibyte characters. */
14911 printf ("%.1s", data - 1);
14912 #ifdef HAVE_MBSTATE_T
14913 /* Try to find out how many bytes made up the character that was
14914 just printed. Advance the symbol pointer past the bytes that
14915 were displayed. */
14916 n = mbrtowc (& w, (char *)(data - 1), MB_CUR_MAX, & state);
14917 #else
14918 n = 1;
14919 #endif
14920 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
14921 data += (n - 1);
14922 }
14923 }
14924
14925 if (c != '\n')
14926 putchar ('\n');
14927 }
14928 else
14929 {
14930 printf (_("<corrupt>\n"));
14931 data = end;
14932 }
14933 some_strings_shown = true;
14934 }
14935 }
14936
14937 if (! some_strings_shown)
14938 printf (_(" No strings found in this section."));
14939
14940 free (real_start);
14941
14942 putchar ('\n');
14943 return true;
14944
14945 error_out:
14946 free (real_start);
14947 return false;
14948 }
14949
14950 static bool
14951 dump_section_as_bytes (Elf_Internal_Shdr *section,
14952 Filedata *filedata,
14953 bool relocate)
14954 {
14955 Elf_Internal_Shdr * relsec;
14956 bfd_size_type bytes;
14957 bfd_size_type section_size;
14958 bfd_vma addr;
14959 unsigned char * data;
14960 unsigned char * real_start;
14961 unsigned char * start;
14962
14963 real_start = start = (unsigned char *) get_section_contents (section, filedata);
14964 if (start == NULL)
14965 /* PR 21820: Do not fail if the section was empty. */
14966 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
14967
14968 section_size = section->sh_size;
14969
14970 if (filedata->is_separate)
14971 printf (_("\nHex dump of section '%s' in linked file %s:\n"),
14972 printable_section_name (filedata, section),
14973 filedata->file_name);
14974 else
14975 printf (_("\nHex dump of section '%s':\n"),
14976 printable_section_name (filedata, section));
14977
14978 if (decompress_dumps)
14979 {
14980 dwarf_size_type new_size = section_size;
14981 dwarf_size_type uncompressed_size = 0;
14982
14983 if ((section->sh_flags & SHF_COMPRESSED) != 0)
14984 {
14985 Elf_Internal_Chdr chdr;
14986 unsigned int compression_header_size
14987 = get_compression_header (& chdr, start, section_size);
14988
14989 if (compression_header_size == 0)
14990 /* An error message will have already been generated
14991 by get_compression_header. */
14992 goto error_out;
14993
14994 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
14995 {
14996 warn (_("section '%s' has unsupported compress type: %d\n"),
14997 printable_section_name (filedata, section), chdr.ch_type);
14998 goto error_out;
14999 }
15000 uncompressed_size = chdr.ch_size;
15001 start += compression_header_size;
15002 new_size -= compression_header_size;
15003 }
15004 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
15005 {
15006 /* Read the zlib header. In this case, it should be "ZLIB"
15007 followed by the uncompressed section size, 8 bytes in
15008 big-endian order. */
15009 uncompressed_size = start[4]; uncompressed_size <<= 8;
15010 uncompressed_size += start[5]; uncompressed_size <<= 8;
15011 uncompressed_size += start[6]; uncompressed_size <<= 8;
15012 uncompressed_size += start[7]; uncompressed_size <<= 8;
15013 uncompressed_size += start[8]; uncompressed_size <<= 8;
15014 uncompressed_size += start[9]; uncompressed_size <<= 8;
15015 uncompressed_size += start[10]; uncompressed_size <<= 8;
15016 uncompressed_size += start[11];
15017 start += 12;
15018 new_size -= 12;
15019 }
15020
15021 if (uncompressed_size)
15022 {
15023 if (uncompress_section_contents (& start, uncompressed_size,
15024 & new_size))
15025 {
15026 section_size = new_size;
15027 }
15028 else
15029 {
15030 error (_("Unable to decompress section %s\n"),
15031 printable_section_name (filedata, section));
15032 /* FIXME: Print the section anyway ? */
15033 goto error_out;
15034 }
15035 }
15036 else
15037 start = real_start;
15038 }
15039
15040 if (relocate)
15041 {
15042 if (! apply_relocations (filedata, section, start, section_size, NULL, NULL))
15043 goto error_out;
15044 }
15045 else
15046 {
15047 /* If the section being dumped has relocations against it the user might
15048 be expecting these relocations to have been applied. Check for this
15049 case and issue a warning message in order to avoid confusion.
15050 FIXME: Maybe we ought to have an option that dumps a section with
15051 relocs applied ? */
15052 for (relsec = filedata->section_headers;
15053 relsec < filedata->section_headers + filedata->file_header.e_shnum;
15054 ++relsec)
15055 {
15056 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
15057 || relsec->sh_info >= filedata->file_header.e_shnum
15058 || filedata->section_headers + relsec->sh_info != section
15059 || relsec->sh_size == 0
15060 || relsec->sh_link >= filedata->file_header.e_shnum)
15061 continue;
15062
15063 printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
15064 break;
15065 }
15066 }
15067
15068 addr = section->sh_addr;
15069 bytes = section_size;
15070 data = start;
15071
15072 while (bytes)
15073 {
15074 int j;
15075 int k;
15076 int lbytes;
15077
15078 lbytes = (bytes > 16 ? 16 : bytes);
15079
15080 printf (" 0x%8.8lx ", (unsigned long) addr);
15081
15082 for (j = 0; j < 16; j++)
15083 {
15084 if (j < lbytes)
15085 printf ("%2.2x", data[j]);
15086 else
15087 printf (" ");
15088
15089 if ((j & 3) == 3)
15090 printf (" ");
15091 }
15092
15093 for (j = 0; j < lbytes; j++)
15094 {
15095 k = data[j];
15096 if (k >= ' ' && k < 0x7f)
15097 printf ("%c", k);
15098 else
15099 printf (".");
15100 }
15101
15102 putchar ('\n');
15103
15104 data += lbytes;
15105 addr += lbytes;
15106 bytes -= lbytes;
15107 }
15108
15109 free (real_start);
15110
15111 putchar ('\n');
15112 return true;
15113
15114 error_out:
15115 free (real_start);
15116 return false;
15117 }
15118
15119 #ifdef ENABLE_LIBCTF
15120 static ctf_sect_t *
15121 shdr_to_ctf_sect (ctf_sect_t *buf, Elf_Internal_Shdr *shdr, Filedata *filedata)
15122 {
15123 buf->cts_name = section_name_print (filedata, shdr);
15124 buf->cts_size = shdr->sh_size;
15125 buf->cts_entsize = shdr->sh_entsize;
15126
15127 return buf;
15128 }
15129
15130 /* Formatting callback function passed to ctf_dump. Returns either the pointer
15131 it is passed, or a pointer to newly-allocated storage, in which case
15132 dump_ctf() will free it when it no longer needs it. */
15133
15134 static char *
15135 dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
15136 char *s, void *arg)
15137 {
15138 const char *blanks = arg;
15139 char *new_s;
15140
15141 if (asprintf (&new_s, "%s%s", blanks, s) < 0)
15142 return s;
15143 return new_s;
15144 }
15145
15146 /* Dump CTF errors/warnings. */
15147 static void
15148 dump_ctf_errs (ctf_dict_t *fp)
15149 {
15150 ctf_next_t *it = NULL;
15151 char *errtext;
15152 int is_warning;
15153 int err;
15154
15155 /* Dump accumulated errors and warnings. */
15156 while ((errtext = ctf_errwarning_next (fp, &it, &is_warning, &err)) != NULL)
15157 {
15158 error (_("%s: %s"), is_warning ? _("warning"): _("error"),
15159 errtext);
15160 free (errtext);
15161 }
15162 if (err != ECTF_NEXT_END)
15163 error (_("CTF error: cannot get CTF errors: `%s'"), ctf_errmsg (err));
15164 }
15165
15166 /* Dump one CTF archive member. */
15167
15168 static void
15169 dump_ctf_archive_member (ctf_dict_t *ctf, const char *name, ctf_dict_t *parent,
15170 size_t member)
15171 {
15172 const char *things[] = {"Header", "Labels", "Data objects",
15173 "Function objects", "Variables", "Types", "Strings",
15174 ""};
15175 const char **thing;
15176 size_t i;
15177
15178 /* Don't print out the name of the default-named archive member if it appears
15179 first in the list. The name .ctf appears everywhere, even for things that
15180 aren't really archives, so printing it out is liable to be confusing; also,
15181 the common case by far is for only one archive member to exist, and hiding
15182 it in that case seems worthwhile. */
15183
15184 if (strcmp (name, ".ctf") != 0 || member != 0)
15185 printf (_("\nCTF archive member: %s:\n"), name);
15186
15187 if (ctf_parent_name (ctf) != NULL)
15188 ctf_import (ctf, parent);
15189
15190 for (i = 0, thing = things; *thing[0]; thing++, i++)
15191 {
15192 ctf_dump_state_t *s = NULL;
15193 char *item;
15194
15195 printf ("\n %s:\n", *thing);
15196 while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
15197 (void *) " ")) != NULL)
15198 {
15199 printf ("%s\n", item);
15200 free (item);
15201 }
15202
15203 if (ctf_errno (ctf))
15204 {
15205 error (_("Iteration failed: %s, %s\n"), *thing,
15206 ctf_errmsg (ctf_errno (ctf)));
15207 break;
15208 }
15209 }
15210
15211 dump_ctf_errs (ctf);
15212 }
15213
15214 static bool
15215 dump_section_as_ctf (Elf_Internal_Shdr * section, Filedata * filedata)
15216 {
15217 Elf_Internal_Shdr * symtab_sec = NULL;
15218 Elf_Internal_Shdr * strtab_sec = NULL;
15219 void * data = NULL;
15220 void * symdata = NULL;
15221 void * strdata = NULL;
15222 ctf_sect_t ctfsect, symsect, strsect;
15223 ctf_sect_t * symsectp = NULL;
15224 ctf_sect_t * strsectp = NULL;
15225 ctf_archive_t * ctfa = NULL;
15226 ctf_dict_t * parent = NULL;
15227 ctf_dict_t * fp;
15228
15229 ctf_next_t *i = NULL;
15230 const char *name;
15231 size_t member = 0;
15232 int err;
15233 bool ret = false;
15234
15235 shdr_to_ctf_sect (&ctfsect, section, filedata);
15236 data = get_section_contents (section, filedata);
15237 ctfsect.cts_data = data;
15238
15239 if (!dump_ctf_symtab_name)
15240 dump_ctf_symtab_name = strdup (".dynsym");
15241
15242 if (!dump_ctf_strtab_name)
15243 dump_ctf_strtab_name = strdup (".dynstr");
15244
15245 if (dump_ctf_symtab_name && dump_ctf_symtab_name[0] != 0)
15246 {
15247 if ((symtab_sec = find_section (filedata, dump_ctf_symtab_name)) == NULL)
15248 {
15249 error (_("No symbol section named %s\n"), dump_ctf_symtab_name);
15250 goto fail;
15251 }
15252 if ((symdata = (void *) get_data (NULL, filedata,
15253 symtab_sec->sh_offset, 1,
15254 symtab_sec->sh_size,
15255 _("symbols"))) == NULL)
15256 goto fail;
15257 symsectp = shdr_to_ctf_sect (&symsect, symtab_sec, filedata);
15258 symsect.cts_data = symdata;
15259 }
15260
15261 if (dump_ctf_strtab_name && dump_ctf_strtab_name[0] != 0)
15262 {
15263 if ((strtab_sec = find_section (filedata, dump_ctf_strtab_name)) == NULL)
15264 {
15265 error (_("No string table section named %s\n"),
15266 dump_ctf_strtab_name);
15267 goto fail;
15268 }
15269 if ((strdata = (void *) get_data (NULL, filedata,
15270 strtab_sec->sh_offset, 1,
15271 strtab_sec->sh_size,
15272 _("strings"))) == NULL)
15273 goto fail;
15274 strsectp = shdr_to_ctf_sect (&strsect, strtab_sec, filedata);
15275 strsect.cts_data = strdata;
15276 }
15277
15278 /* Load the CTF file and dump it. It may be a raw CTF section, or an archive:
15279 libctf papers over the difference, so we can pretend it is always an
15280 archive. */
15281
15282 if ((ctfa = ctf_arc_bufopen (&ctfsect, symsectp, strsectp, &err)) == NULL)
15283 {
15284 dump_ctf_errs (NULL);
15285 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15286 goto fail;
15287 }
15288
15289 ctf_arc_symsect_endianness (ctfa, filedata->file_header.e_ident[EI_DATA]
15290 != ELFDATA2MSB);
15291
15292 /* Preload the parent dict, since it will need to be imported into every
15293 child in turn. */
15294 if ((parent = ctf_dict_open (ctfa, dump_ctf_parent_name, &err)) == NULL)
15295 {
15296 dump_ctf_errs (NULL);
15297 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15298 goto fail;
15299 }
15300
15301 ret = true;
15302
15303 if (filedata->is_separate)
15304 printf (_("\nDump of CTF section '%s' in linked file %s:\n"),
15305 printable_section_name (filedata, section),
15306 filedata->file_name);
15307 else
15308 printf (_("\nDump of CTF section '%s':\n"),
15309 printable_section_name (filedata, section));
15310
15311 while ((fp = ctf_archive_next (ctfa, &i, &name, 0, &err)) != NULL)
15312 dump_ctf_archive_member (fp, name, parent, member++);
15313 if (err != ECTF_NEXT_END)
15314 {
15315 dump_ctf_errs (NULL);
15316 error (_("CTF member open failure: %s\n"), ctf_errmsg (err));
15317 ret = false;
15318 }
15319
15320 fail:
15321 ctf_dict_close (parent);
15322 ctf_close (ctfa);
15323 free (data);
15324 free (symdata);
15325 free (strdata);
15326 return ret;
15327 }
15328 #endif
15329
15330 static bool
15331 load_specific_debug_section (enum dwarf_section_display_enum debug,
15332 const Elf_Internal_Shdr * sec,
15333 void * data)
15334 {
15335 struct dwarf_section * section = &debug_displays [debug].section;
15336 char buf [64];
15337 Filedata * filedata = (Filedata *) data;
15338
15339 if (section->start != NULL)
15340 {
15341 /* If it is already loaded, do nothing. */
15342 if (streq (section->filename, filedata->file_name))
15343 return true;
15344 free (section->start);
15345 }
15346
15347 snprintf (buf, sizeof (buf), _("%s section data"), section->name);
15348 section->address = sec->sh_addr;
15349 section->filename = filedata->file_name;
15350 section->start = (unsigned char *) get_data (NULL, filedata,
15351 sec->sh_offset, 1,
15352 sec->sh_size, buf);
15353 if (section->start == NULL)
15354 section->size = 0;
15355 else
15356 {
15357 unsigned char *start = section->start;
15358 dwarf_size_type size = sec->sh_size;
15359 dwarf_size_type uncompressed_size = 0;
15360
15361 if ((sec->sh_flags & SHF_COMPRESSED) != 0)
15362 {
15363 Elf_Internal_Chdr chdr;
15364 unsigned int compression_header_size;
15365
15366 if (size < (is_32bit_elf
15367 ? sizeof (Elf32_External_Chdr)
15368 : sizeof (Elf64_External_Chdr)))
15369 {
15370 warn (_("compressed section %s is too small to contain a compression header\n"),
15371 section->name);
15372 return false;
15373 }
15374
15375 compression_header_size = get_compression_header (&chdr, start, size);
15376 if (compression_header_size == 0)
15377 /* An error message will have already been generated
15378 by get_compression_header. */
15379 return false;
15380
15381 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
15382 {
15383 warn (_("section '%s' has unsupported compress type: %d\n"),
15384 section->name, chdr.ch_type);
15385 return false;
15386 }
15387 uncompressed_size = chdr.ch_size;
15388 start += compression_header_size;
15389 size -= compression_header_size;
15390 }
15391 else if (size > 12 && streq ((char *) start, "ZLIB"))
15392 {
15393 /* Read the zlib header. In this case, it should be "ZLIB"
15394 followed by the uncompressed section size, 8 bytes in
15395 big-endian order. */
15396 uncompressed_size = start[4]; uncompressed_size <<= 8;
15397 uncompressed_size += start[5]; uncompressed_size <<= 8;
15398 uncompressed_size += start[6]; uncompressed_size <<= 8;
15399 uncompressed_size += start[7]; uncompressed_size <<= 8;
15400 uncompressed_size += start[8]; uncompressed_size <<= 8;
15401 uncompressed_size += start[9]; uncompressed_size <<= 8;
15402 uncompressed_size += start[10]; uncompressed_size <<= 8;
15403 uncompressed_size += start[11];
15404 start += 12;
15405 size -= 12;
15406 }
15407
15408 if (uncompressed_size)
15409 {
15410 if (uncompress_section_contents (&start, uncompressed_size,
15411 &size))
15412 {
15413 /* Free the compressed buffer, update the section buffer
15414 and the section size if uncompress is successful. */
15415 free (section->start);
15416 section->start = start;
15417 }
15418 else
15419 {
15420 error (_("Unable to decompress section %s\n"),
15421 printable_section_name (filedata, sec));
15422 return false;
15423 }
15424 }
15425
15426 section->size = size;
15427 }
15428
15429 if (section->start == NULL)
15430 return false;
15431
15432 if (debug_displays [debug].relocate)
15433 {
15434 if (! apply_relocations (filedata, sec, section->start, section->size,
15435 & section->reloc_info, & section->num_relocs))
15436 return false;
15437 }
15438 else
15439 {
15440 section->reloc_info = NULL;
15441 section->num_relocs = 0;
15442 }
15443
15444 return true;
15445 }
15446
15447 #if HAVE_LIBDEBUGINFOD
15448 /* Return a hex string representation of the build-id. */
15449 unsigned char *
15450 get_build_id (void * data)
15451 {
15452 Filedata * filedata = (Filedata *) data;
15453 Elf_Internal_Shdr * shdr;
15454 unsigned long i;
15455
15456 /* Iterate through notes to find note.gnu.build-id.
15457 FIXME: Only the first note in any note section is examined. */
15458 for (i = 0, shdr = filedata->section_headers;
15459 i < filedata->file_header.e_shnum && shdr != NULL;
15460 i++, shdr++)
15461 {
15462 if (shdr->sh_type != SHT_NOTE)
15463 continue;
15464
15465 char * next;
15466 char * end;
15467 size_t data_remaining;
15468 size_t min_notesz;
15469 Elf_External_Note * enote;
15470 Elf_Internal_Note inote;
15471
15472 bfd_vma offset = shdr->sh_offset;
15473 bfd_vma align = shdr->sh_addralign;
15474 bfd_vma length = shdr->sh_size;
15475
15476 enote = (Elf_External_Note *) get_section_contents (shdr, filedata);
15477 if (enote == NULL)
15478 continue;
15479
15480 if (align < 4)
15481 align = 4;
15482 else if (align != 4 && align != 8)
15483 {
15484 free (enote);
15485 continue;
15486 }
15487
15488 end = (char *) enote + length;
15489 data_remaining = end - (char *) enote;
15490
15491 if (!is_ia64_vms (filedata))
15492 {
15493 min_notesz = offsetof (Elf_External_Note, name);
15494 if (data_remaining < min_notesz)
15495 {
15496 warn (_("\
15497 malformed note encountered in section %s whilst scanning for build-id note\n"),
15498 printable_section_name (filedata, shdr));
15499 free (enote);
15500 continue;
15501 }
15502 data_remaining -= min_notesz;
15503
15504 inote.type = BYTE_GET (enote->type);
15505 inote.namesz = BYTE_GET (enote->namesz);
15506 inote.namedata = enote->name;
15507 inote.descsz = BYTE_GET (enote->descsz);
15508 inote.descdata = ((char *) enote
15509 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
15510 inote.descpos = offset + (inote.descdata - (char *) enote);
15511 next = ((char *) enote
15512 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
15513 }
15514 else
15515 {
15516 Elf64_External_VMS_Note *vms_enote;
15517
15518 /* PR binutils/15191
15519 Make sure that there is enough data to read. */
15520 min_notesz = offsetof (Elf64_External_VMS_Note, name);
15521 if (data_remaining < min_notesz)
15522 {
15523 warn (_("\
15524 malformed note encountered in section %s whilst scanning for build-id note\n"),
15525 printable_section_name (filedata, shdr));
15526 free (enote);
15527 continue;
15528 }
15529 data_remaining -= min_notesz;
15530
15531 vms_enote = (Elf64_External_VMS_Note *) enote;
15532 inote.type = BYTE_GET (vms_enote->type);
15533 inote.namesz = BYTE_GET (vms_enote->namesz);
15534 inote.namedata = vms_enote->name;
15535 inote.descsz = BYTE_GET (vms_enote->descsz);
15536 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
15537 inote.descpos = offset + (inote.descdata - (char *) enote);
15538 next = inote.descdata + align_power (inote.descsz, 3);
15539 }
15540
15541 /* Skip malformed notes. */
15542 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
15543 || (size_t) (inote.descdata - inote.namedata) > data_remaining
15544 || (size_t) (next - inote.descdata) < inote.descsz
15545 || ((size_t) (next - inote.descdata)
15546 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
15547 {
15548 warn (_("\
15549 malformed note encountered in section %s whilst scanning for build-id note\n"),
15550 printable_section_name (filedata, shdr));
15551 free (enote);
15552 continue;
15553 }
15554
15555 /* Check if this is the build-id note. If so then convert the build-id
15556 bytes to a hex string. */
15557 if (inote.namesz > 0
15558 && startswith (inote.namedata, "GNU")
15559 && inote.type == NT_GNU_BUILD_ID)
15560 {
15561 unsigned long j;
15562 char * build_id;
15563
15564 build_id = malloc (inote.descsz * 2 + 1);
15565 if (build_id == NULL)
15566 {
15567 free (enote);
15568 return NULL;
15569 }
15570
15571 for (j = 0; j < inote.descsz; ++j)
15572 sprintf (build_id + (j * 2), "%02x", inote.descdata[j] & 0xff);
15573 build_id[inote.descsz * 2] = '\0';
15574 free (enote);
15575
15576 return (unsigned char *) build_id;
15577 }
15578 free (enote);
15579 }
15580
15581 return NULL;
15582 }
15583 #endif /* HAVE_LIBDEBUGINFOD */
15584
15585 /* If this is not NULL, load_debug_section will only look for sections
15586 within the list of sections given here. */
15587 static unsigned int * section_subset = NULL;
15588
15589 bool
15590 load_debug_section (enum dwarf_section_display_enum debug, void * data)
15591 {
15592 struct dwarf_section * section = &debug_displays [debug].section;
15593 Elf_Internal_Shdr * sec;
15594 Filedata * filedata = (Filedata *) data;
15595
15596 /* Without section headers we cannot find any sections. */
15597 if (filedata->section_headers == NULL)
15598 return false;
15599
15600 if (filedata->string_table == NULL
15601 && filedata->file_header.e_shstrndx != SHN_UNDEF
15602 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
15603 {
15604 Elf_Internal_Shdr * strs;
15605
15606 /* Read in the string table, so that we have section names to scan. */
15607 strs = filedata->section_headers + filedata->file_header.e_shstrndx;
15608
15609 if (strs != NULL && strs->sh_size != 0)
15610 {
15611 filedata->string_table
15612 = (char *) get_data (NULL, filedata, strs->sh_offset,
15613 1, strs->sh_size, _("string table"));
15614
15615 filedata->string_table_length
15616 = filedata->string_table != NULL ? strs->sh_size : 0;
15617 }
15618 }
15619
15620 /* Locate the debug section. */
15621 sec = find_section_in_set (filedata, section->uncompressed_name, section_subset);
15622 if (sec != NULL)
15623 section->name = section->uncompressed_name;
15624 else
15625 {
15626 sec = find_section_in_set (filedata, section->compressed_name, section_subset);
15627 if (sec != NULL)
15628 section->name = section->compressed_name;
15629 }
15630 if (sec == NULL)
15631 return false;
15632
15633 /* If we're loading from a subset of sections, and we've loaded
15634 a section matching this name before, it's likely that it's a
15635 different one. */
15636 if (section_subset != NULL)
15637 free_debug_section (debug);
15638
15639 return load_specific_debug_section (debug, sec, data);
15640 }
15641
15642 void
15643 free_debug_section (enum dwarf_section_display_enum debug)
15644 {
15645 struct dwarf_section * section = &debug_displays [debug].section;
15646
15647 if (section->start == NULL)
15648 return;
15649
15650 free ((char *) section->start);
15651 section->start = NULL;
15652 section->address = 0;
15653 section->size = 0;
15654
15655 free (section->reloc_info);
15656 section->reloc_info = NULL;
15657 section->num_relocs = 0;
15658 }
15659
15660 static bool
15661 display_debug_section (int shndx, Elf_Internal_Shdr * section, Filedata * filedata)
15662 {
15663 const char *name = (section_name_valid (filedata, section)
15664 ? section_name (filedata, section) : "");
15665 const char *print_name = printable_section_name (filedata, section);
15666 bfd_size_type length;
15667 bool result = true;
15668 int i;
15669
15670 length = section->sh_size;
15671 if (length == 0)
15672 {
15673 printf (_("\nSection '%s' has no debugging data.\n"), print_name);
15674 return true;
15675 }
15676 if (section->sh_type == SHT_NOBITS)
15677 {
15678 /* There is no point in dumping the contents of a debugging section
15679 which has the NOBITS type - the bits in the file will be random.
15680 This can happen when a file containing a .eh_frame section is
15681 stripped with the --only-keep-debug command line option. */
15682 printf (_("section '%s' has the NOBITS type - its contents are unreliable.\n"),
15683 print_name);
15684 return false;
15685 }
15686
15687 if (startswith (name, ".gnu.linkonce.wi."))
15688 name = ".debug_info";
15689
15690 /* See if we know how to display the contents of this section. */
15691 for (i = 0; i < max; i++)
15692 {
15693 enum dwarf_section_display_enum id = (enum dwarf_section_display_enum) i;
15694 struct dwarf_section_display * display = debug_displays + i;
15695 struct dwarf_section * sec = & display->section;
15696
15697 if (streq (sec->uncompressed_name, name)
15698 || (id == line && startswith (name, ".debug_line."))
15699 || streq (sec->compressed_name, name))
15700 {
15701 bool secondary = (section != find_section (filedata, name));
15702
15703 if (secondary)
15704 free_debug_section (id);
15705
15706 if (i == line && startswith (name, ".debug_line."))
15707 sec->name = name;
15708 else if (streq (sec->uncompressed_name, name))
15709 sec->name = sec->uncompressed_name;
15710 else
15711 sec->name = sec->compressed_name;
15712
15713 if (load_specific_debug_section (id, section, filedata))
15714 {
15715 /* If this debug section is part of a CU/TU set in a .dwp file,
15716 restrict load_debug_section to the sections in that set. */
15717 section_subset = find_cu_tu_set (filedata, shndx);
15718
15719 result &= display->display (sec, filedata);
15720
15721 section_subset = NULL;
15722
15723 if (secondary || (id != info && id != abbrev && id != debug_addr))
15724 free_debug_section (id);
15725 }
15726 break;
15727 }
15728 }
15729
15730 if (i == max)
15731 {
15732 printf (_("Unrecognized debug section: %s\n"), print_name);
15733 result = false;
15734 }
15735
15736 return result;
15737 }
15738
15739 /* Set DUMP_SECTS for all sections where dumps were requested
15740 based on section name. */
15741
15742 static void
15743 initialise_dumps_byname (Filedata * filedata)
15744 {
15745 struct dump_list_entry * cur;
15746
15747 for (cur = dump_sects_byname; cur; cur = cur->next)
15748 {
15749 unsigned int i;
15750 bool any = false;
15751
15752 for (i = 0; i < filedata->file_header.e_shnum; i++)
15753 if (section_name_valid (filedata, filedata->section_headers + i)
15754 && streq (section_name (filedata, filedata->section_headers + i),
15755 cur->name))
15756 {
15757 request_dump_bynumber (&filedata->dump, i, cur->type);
15758 any = true;
15759 }
15760
15761 if (!any && !filedata->is_separate)
15762 warn (_("Section '%s' was not dumped because it does not exist\n"),
15763 cur->name);
15764 }
15765 }
15766
15767 static bool
15768 process_section_contents (Filedata * filedata)
15769 {
15770 Elf_Internal_Shdr * section;
15771 unsigned int i;
15772 bool res = true;
15773
15774 if (! do_dump)
15775 return true;
15776
15777 initialise_dumps_byname (filedata);
15778
15779 for (i = 0, section = filedata->section_headers;
15780 i < filedata->file_header.e_shnum && i < filedata->dump.num_dump_sects;
15781 i++, section++)
15782 {
15783 dump_type dump = filedata->dump.dump_sects[i];
15784
15785 if (filedata->is_separate && ! process_links)
15786 dump &= DEBUG_DUMP;
15787
15788 #ifdef SUPPORT_DISASSEMBLY
15789 if (dump & DISASS_DUMP)
15790 {
15791 if (! disassemble_section (section, filedata))
15792 res = false;
15793 }
15794 #endif
15795 if (dump & HEX_DUMP)
15796 {
15797 if (! dump_section_as_bytes (section, filedata, false))
15798 res = false;
15799 }
15800
15801 if (dump & RELOC_DUMP)
15802 {
15803 if (! dump_section_as_bytes (section, filedata, true))
15804 res = false;
15805 }
15806
15807 if (dump & STRING_DUMP)
15808 {
15809 if (! dump_section_as_strings (section, filedata))
15810 res = false;
15811 }
15812
15813 if (dump & DEBUG_DUMP)
15814 {
15815 if (! display_debug_section (i, section, filedata))
15816 res = false;
15817 }
15818
15819 #ifdef ENABLE_LIBCTF
15820 if (dump & CTF_DUMP)
15821 {
15822 if (! dump_section_as_ctf (section, filedata))
15823 res = false;
15824 }
15825 #endif
15826 }
15827
15828 if (! filedata->is_separate)
15829 {
15830 /* Check to see if the user requested a
15831 dump of a section that does not exist. */
15832 for (; i < filedata->dump.num_dump_sects; i++)
15833 if (filedata->dump.dump_sects[i])
15834 {
15835 warn (_("Section %d was not dumped because it does not exist!\n"), i);
15836 res = false;
15837 }
15838 }
15839
15840 return res;
15841 }
15842
15843 static void
15844 process_mips_fpe_exception (int mask)
15845 {
15846 if (mask)
15847 {
15848 bool first = true;
15849
15850 if (mask & OEX_FPU_INEX)
15851 fputs ("INEX", stdout), first = false;
15852 if (mask & OEX_FPU_UFLO)
15853 printf ("%sUFLO", first ? "" : "|"), first = false;
15854 if (mask & OEX_FPU_OFLO)
15855 printf ("%sOFLO", first ? "" : "|"), first = false;
15856 if (mask & OEX_FPU_DIV0)
15857 printf ("%sDIV0", first ? "" : "|"), first = false;
15858 if (mask & OEX_FPU_INVAL)
15859 printf ("%sINVAL", first ? "" : "|");
15860 }
15861 else
15862 fputs ("0", stdout);
15863 }
15864
15865 /* Display's the value of TAG at location P. If TAG is
15866 greater than 0 it is assumed to be an unknown tag, and
15867 a message is printed to this effect. Otherwise it is
15868 assumed that a message has already been printed.
15869
15870 If the bottom bit of TAG is set it assumed to have a
15871 string value, otherwise it is assumed to have an integer
15872 value.
15873
15874 Returns an updated P pointing to the first unread byte
15875 beyond the end of TAG's value.
15876
15877 Reads at or beyond END will not be made. */
15878
15879 static unsigned char *
15880 display_tag_value (signed int tag,
15881 unsigned char * p,
15882 const unsigned char * const end)
15883 {
15884 unsigned long val;
15885
15886 if (tag > 0)
15887 printf (" Tag_unknown_%d: ", tag);
15888
15889 if (p >= end)
15890 {
15891 warn (_("<corrupt tag>\n"));
15892 }
15893 else if (tag & 1)
15894 {
15895 /* PR 17531 file: 027-19978-0.004. */
15896 size_t maxlen = (end - p) - 1;
15897
15898 putchar ('"');
15899 if (maxlen > 0)
15900 {
15901 print_symbol ((int) maxlen, (const char *) p);
15902 p += strnlen ((char *) p, maxlen) + 1;
15903 }
15904 else
15905 {
15906 printf (_("<corrupt string tag>"));
15907 p = (unsigned char *) end;
15908 }
15909 printf ("\"\n");
15910 }
15911 else
15912 {
15913 READ_ULEB (val, p, end);
15914 printf ("%ld (0x%lx)\n", val, val);
15915 }
15916
15917 assert (p <= end);
15918 return p;
15919 }
15920
15921 /* ARC ABI attributes section. */
15922
15923 static unsigned char *
15924 display_arc_attribute (unsigned char * p,
15925 const unsigned char * const end)
15926 {
15927 unsigned int tag;
15928 unsigned int val;
15929
15930 READ_ULEB (tag, p, end);
15931
15932 switch (tag)
15933 {
15934 case Tag_ARC_PCS_config:
15935 READ_ULEB (val, p, end);
15936 printf (" Tag_ARC_PCS_config: ");
15937 switch (val)
15938 {
15939 case 0:
15940 printf (_("Absent/Non standard\n"));
15941 break;
15942 case 1:
15943 printf (_("Bare metal/mwdt\n"));
15944 break;
15945 case 2:
15946 printf (_("Bare metal/newlib\n"));
15947 break;
15948 case 3:
15949 printf (_("Linux/uclibc\n"));
15950 break;
15951 case 4:
15952 printf (_("Linux/glibc\n"));
15953 break;
15954 default:
15955 printf (_("Unknown\n"));
15956 break;
15957 }
15958 break;
15959
15960 case Tag_ARC_CPU_base:
15961 READ_ULEB (val, p, end);
15962 printf (" Tag_ARC_CPU_base: ");
15963 switch (val)
15964 {
15965 default:
15966 case TAG_CPU_NONE:
15967 printf (_("Absent\n"));
15968 break;
15969 case TAG_CPU_ARC6xx:
15970 printf ("ARC6xx\n");
15971 break;
15972 case TAG_CPU_ARC7xx:
15973 printf ("ARC7xx\n");
15974 break;
15975 case TAG_CPU_ARCEM:
15976 printf ("ARCEM\n");
15977 break;
15978 case TAG_CPU_ARCHS:
15979 printf ("ARCHS\n");
15980 break;
15981 }
15982 break;
15983
15984 case Tag_ARC_CPU_variation:
15985 READ_ULEB (val, p, end);
15986 printf (" Tag_ARC_CPU_variation: ");
15987 switch (val)
15988 {
15989 default:
15990 if (val > 0 && val < 16)
15991 printf ("Core%d\n", val);
15992 else
15993 printf ("Unknown\n");
15994 break;
15995
15996 case 0:
15997 printf (_("Absent\n"));
15998 break;
15999 }
16000 break;
16001
16002 case Tag_ARC_CPU_name:
16003 printf (" Tag_ARC_CPU_name: ");
16004 p = display_tag_value (-1, p, end);
16005 break;
16006
16007 case Tag_ARC_ABI_rf16:
16008 READ_ULEB (val, p, end);
16009 printf (" Tag_ARC_ABI_rf16: %s\n", val ? _("yes") : _("no"));
16010 break;
16011
16012 case Tag_ARC_ABI_osver:
16013 READ_ULEB (val, p, end);
16014 printf (" Tag_ARC_ABI_osver: v%d\n", val);
16015 break;
16016
16017 case Tag_ARC_ABI_pic:
16018 case Tag_ARC_ABI_sda:
16019 READ_ULEB (val, p, end);
16020 printf (tag == Tag_ARC_ABI_sda ? " Tag_ARC_ABI_sda: "
16021 : " Tag_ARC_ABI_pic: ");
16022 switch (val)
16023 {
16024 case 0:
16025 printf (_("Absent\n"));
16026 break;
16027 case 1:
16028 printf ("MWDT\n");
16029 break;
16030 case 2:
16031 printf ("GNU\n");
16032 break;
16033 default:
16034 printf (_("Unknown\n"));
16035 break;
16036 }
16037 break;
16038
16039 case Tag_ARC_ABI_tls:
16040 READ_ULEB (val, p, end);
16041 printf (" Tag_ARC_ABI_tls: %s\n", val ? "r25": "none");
16042 break;
16043
16044 case Tag_ARC_ABI_enumsize:
16045 READ_ULEB (val, p, end);
16046 printf (" Tag_ARC_ABI_enumsize: %s\n", val ? _("default") :
16047 _("smallest"));
16048 break;
16049
16050 case Tag_ARC_ABI_exceptions:
16051 READ_ULEB (val, p, end);
16052 printf (" Tag_ARC_ABI_exceptions: %s\n", val ? _("OPTFP")
16053 : _("default"));
16054 break;
16055
16056 case Tag_ARC_ABI_double_size:
16057 READ_ULEB (val, p, end);
16058 printf (" Tag_ARC_ABI_double_size: %d\n", val);
16059 break;
16060
16061 case Tag_ARC_ISA_config:
16062 printf (" Tag_ARC_ISA_config: ");
16063 p = display_tag_value (-1, p, end);
16064 break;
16065
16066 case Tag_ARC_ISA_apex:
16067 printf (" Tag_ARC_ISA_apex: ");
16068 p = display_tag_value (-1, p, end);
16069 break;
16070
16071 case Tag_ARC_ISA_mpy_option:
16072 READ_ULEB (val, p, end);
16073 printf (" Tag_ARC_ISA_mpy_option: %d\n", val);
16074 break;
16075
16076 case Tag_ARC_ATR_version:
16077 READ_ULEB (val, p, end);
16078 printf (" Tag_ARC_ATR_version: %d\n", val);
16079 break;
16080
16081 default:
16082 return display_tag_value (tag & 1, p, end);
16083 }
16084
16085 return p;
16086 }
16087
16088 /* ARM EABI attributes section. */
16089 typedef struct
16090 {
16091 unsigned int tag;
16092 const char * name;
16093 /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup. */
16094 unsigned int type;
16095 const char *const *table;
16096 } arm_attr_public_tag;
16097
16098 static const char *const arm_attr_tag_CPU_arch[] =
16099 {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
16100 "v6K", "v7", "v6-M", "v6S-M", "v7E-M", "v8", "v8-R", "v8-M.baseline",
16101 "v8-M.mainline", "v8.1-A", "v8.2-A", "v8.3-A",
16102 "v8.1-M.mainline", "v9"};
16103 static const char *const arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
16104 static const char *const arm_attr_tag_THUMB_ISA_use[] =
16105 {"No", "Thumb-1", "Thumb-2", "Yes"};
16106 static const char *const arm_attr_tag_FP_arch[] =
16107 {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16", "VFPv4", "VFPv4-D16",
16108 "FP for ARMv8", "FPv5/FP-D16 for ARMv8"};
16109 static const char *const arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
16110 static const char *const arm_attr_tag_Advanced_SIMD_arch[] =
16111 {"No", "NEONv1", "NEONv1 with Fused-MAC", "NEON for ARMv8",
16112 "NEON for ARMv8.1"};
16113 static const char *const arm_attr_tag_PCS_config[] =
16114 {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
16115 "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
16116 static const char *const arm_attr_tag_ABI_PCS_R9_use[] =
16117 {"V6", "SB", "TLS", "Unused"};
16118 static const char *const arm_attr_tag_ABI_PCS_RW_data[] =
16119 {"Absolute", "PC-relative", "SB-relative", "None"};
16120 static const char *const arm_attr_tag_ABI_PCS_RO_data[] =
16121 {"Absolute", "PC-relative", "None"};
16122 static const char *const arm_attr_tag_ABI_PCS_GOT_use[] =
16123 {"None", "direct", "GOT-indirect"};
16124 static const char *const arm_attr_tag_ABI_PCS_wchar_t[] =
16125 {"None", "??? 1", "2", "??? 3", "4"};
16126 static const char *const arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
16127 static const char *const arm_attr_tag_ABI_FP_denormal[] =
16128 {"Unused", "Needed", "Sign only"};
16129 static const char *const arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
16130 static const char *const arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
16131 static const char *const arm_attr_tag_ABI_FP_number_model[] =
16132 {"Unused", "Finite", "RTABI", "IEEE 754"};
16133 static const char *const arm_attr_tag_ABI_enum_size[] =
16134 {"Unused", "small", "int", "forced to int"};
16135 static const char *const arm_attr_tag_ABI_HardFP_use[] =
16136 {"As Tag_FP_arch", "SP only", "Reserved", "Deprecated"};
16137 static const char *const arm_attr_tag_ABI_VFP_args[] =
16138 {"AAPCS", "VFP registers", "custom", "compatible"};
16139 static const char *const arm_attr_tag_ABI_WMMX_args[] =
16140 {"AAPCS", "WMMX registers", "custom"};
16141 static const char *const arm_attr_tag_ABI_optimization_goals[] =
16142 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
16143 "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
16144 static const char *const arm_attr_tag_ABI_FP_optimization_goals[] =
16145 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
16146 "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
16147 static const char *const arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
16148 static const char *const arm_attr_tag_FP_HP_extension[] =
16149 {"Not Allowed", "Allowed"};
16150 static const char *const arm_attr_tag_ABI_FP_16bit_format[] =
16151 {"None", "IEEE 754", "Alternative Format"};
16152 static const char *const arm_attr_tag_DSP_extension[] =
16153 {"Follow architecture", "Allowed"};
16154 static const char *const arm_attr_tag_MPextension_use[] =
16155 {"Not Allowed", "Allowed"};
16156 static const char *const arm_attr_tag_DIV_use[] =
16157 {"Allowed in Thumb-ISA, v7-R or v7-M", "Not allowed",
16158 "Allowed in v7-A with integer division extension"};
16159 static const char *const arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
16160 static const char *const arm_attr_tag_Virtualization_use[] =
16161 {"Not Allowed", "TrustZone", "Virtualization Extensions",
16162 "TrustZone and Virtualization Extensions"};
16163 static const char *const arm_attr_tag_MPextension_use_legacy[] =
16164 {"Not Allowed", "Allowed"};
16165
16166 static const char *const arm_attr_tag_MVE_arch[] =
16167 {"No MVE", "MVE Integer only", "MVE Integer and FP"};
16168
16169 static const char * arm_attr_tag_PAC_extension[] =
16170 {"No PAC/AUT instructions",
16171 "PAC/AUT instructions permitted in the NOP space",
16172 "PAC/AUT instructions permitted in the NOP and in the non-NOP space"};
16173
16174 static const char * arm_attr_tag_BTI_extension[] =
16175 {"BTI instructions not permitted",
16176 "BTI instructions permitted in the NOP space",
16177 "BTI instructions permitted in the NOP and in the non-NOP space"};
16178
16179 static const char * arm_attr_tag_BTI_use[] =
16180 {"Compiled without branch target enforcement",
16181 "Compiled with branch target enforcement"};
16182
16183 static const char * arm_attr_tag_PACRET_use[] =
16184 {"Compiled without return address signing and authentication",
16185 "Compiled with return address signing and authentication"};
16186
16187 #define LOOKUP(id, name) \
16188 {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
16189 static arm_attr_public_tag arm_attr_public_tags[] =
16190 {
16191 {4, "CPU_raw_name", 1, NULL},
16192 {5, "CPU_name", 1, NULL},
16193 LOOKUP(6, CPU_arch),
16194 {7, "CPU_arch_profile", 0, NULL},
16195 LOOKUP(8, ARM_ISA_use),
16196 LOOKUP(9, THUMB_ISA_use),
16197 LOOKUP(10, FP_arch),
16198 LOOKUP(11, WMMX_arch),
16199 LOOKUP(12, Advanced_SIMD_arch),
16200 LOOKUP(13, PCS_config),
16201 LOOKUP(14, ABI_PCS_R9_use),
16202 LOOKUP(15, ABI_PCS_RW_data),
16203 LOOKUP(16, ABI_PCS_RO_data),
16204 LOOKUP(17, ABI_PCS_GOT_use),
16205 LOOKUP(18, ABI_PCS_wchar_t),
16206 LOOKUP(19, ABI_FP_rounding),
16207 LOOKUP(20, ABI_FP_denormal),
16208 LOOKUP(21, ABI_FP_exceptions),
16209 LOOKUP(22, ABI_FP_user_exceptions),
16210 LOOKUP(23, ABI_FP_number_model),
16211 {24, "ABI_align_needed", 0, NULL},
16212 {25, "ABI_align_preserved", 0, NULL},
16213 LOOKUP(26, ABI_enum_size),
16214 LOOKUP(27, ABI_HardFP_use),
16215 LOOKUP(28, ABI_VFP_args),
16216 LOOKUP(29, ABI_WMMX_args),
16217 LOOKUP(30, ABI_optimization_goals),
16218 LOOKUP(31, ABI_FP_optimization_goals),
16219 {32, "compatibility", 0, NULL},
16220 LOOKUP(34, CPU_unaligned_access),
16221 LOOKUP(36, FP_HP_extension),
16222 LOOKUP(38, ABI_FP_16bit_format),
16223 LOOKUP(42, MPextension_use),
16224 LOOKUP(44, DIV_use),
16225 LOOKUP(46, DSP_extension),
16226 LOOKUP(48, MVE_arch),
16227 LOOKUP(50, PAC_extension),
16228 LOOKUP(52, BTI_extension),
16229 LOOKUP(74, BTI_use),
16230 LOOKUP(76, PACRET_use),
16231 {64, "nodefaults", 0, NULL},
16232 {65, "also_compatible_with", 0, NULL},
16233 LOOKUP(66, T2EE_use),
16234 {67, "conformance", 1, NULL},
16235 LOOKUP(68, Virtualization_use),
16236 LOOKUP(70, MPextension_use_legacy)
16237 };
16238 #undef LOOKUP
16239
16240 static unsigned char *
16241 display_arm_attribute (unsigned char * p,
16242 const unsigned char * const end)
16243 {
16244 unsigned int tag;
16245 unsigned int val;
16246 arm_attr_public_tag * attr;
16247 unsigned i;
16248 unsigned int type;
16249
16250 READ_ULEB (tag, p, end);
16251 attr = NULL;
16252 for (i = 0; i < ARRAY_SIZE (arm_attr_public_tags); i++)
16253 {
16254 if (arm_attr_public_tags[i].tag == tag)
16255 {
16256 attr = &arm_attr_public_tags[i];
16257 break;
16258 }
16259 }
16260
16261 if (attr)
16262 {
16263 printf (" Tag_%s: ", attr->name);
16264 switch (attr->type)
16265 {
16266 case 0:
16267 switch (tag)
16268 {
16269 case 7: /* Tag_CPU_arch_profile. */
16270 READ_ULEB (val, p, end);
16271 switch (val)
16272 {
16273 case 0: printf (_("None\n")); break;
16274 case 'A': printf (_("Application\n")); break;
16275 case 'R': printf (_("Realtime\n")); break;
16276 case 'M': printf (_("Microcontroller\n")); break;
16277 case 'S': printf (_("Application or Realtime\n")); break;
16278 default: printf ("??? (%d)\n", val); break;
16279 }
16280 break;
16281
16282 case 24: /* Tag_align_needed. */
16283 READ_ULEB (val, p, end);
16284 switch (val)
16285 {
16286 case 0: printf (_("None\n")); break;
16287 case 1: printf (_("8-byte\n")); break;
16288 case 2: printf (_("4-byte\n")); break;
16289 case 3: printf ("??? 3\n"); break;
16290 default:
16291 if (val <= 12)
16292 printf (_("8-byte and up to %d-byte extended\n"),
16293 1 << val);
16294 else
16295 printf ("??? (%d)\n", val);
16296 break;
16297 }
16298 break;
16299
16300 case 25: /* Tag_align_preserved. */
16301 READ_ULEB (val, p, end);
16302 switch (val)
16303 {
16304 case 0: printf (_("None\n")); break;
16305 case 1: printf (_("8-byte, except leaf SP\n")); break;
16306 case 2: printf (_("8-byte\n")); break;
16307 case 3: printf ("??? 3\n"); break;
16308 default:
16309 if (val <= 12)
16310 printf (_("8-byte and up to %d-byte extended\n"),
16311 1 << val);
16312 else
16313 printf ("??? (%d)\n", val);
16314 break;
16315 }
16316 break;
16317
16318 case 32: /* Tag_compatibility. */
16319 {
16320 READ_ULEB (val, p, end);
16321 printf (_("flag = %d, vendor = "), val);
16322 if (p < end - 1)
16323 {
16324 size_t maxlen = (end - p) - 1;
16325
16326 print_symbol ((int) maxlen, (const char *) p);
16327 p += strnlen ((char *) p, maxlen) + 1;
16328 }
16329 else
16330 {
16331 printf (_("<corrupt>"));
16332 p = (unsigned char *) end;
16333 }
16334 putchar ('\n');
16335 }
16336 break;
16337
16338 case 64: /* Tag_nodefaults. */
16339 /* PR 17531: file: 001-505008-0.01. */
16340 if (p < end)
16341 p++;
16342 printf (_("True\n"));
16343 break;
16344
16345 case 65: /* Tag_also_compatible_with. */
16346 READ_ULEB (val, p, end);
16347 if (val == 6 /* Tag_CPU_arch. */)
16348 {
16349 READ_ULEB (val, p, end);
16350 if ((unsigned int) val >= ARRAY_SIZE (arm_attr_tag_CPU_arch))
16351 printf ("??? (%d)\n", val);
16352 else
16353 printf ("%s\n", arm_attr_tag_CPU_arch[val]);
16354 }
16355 else
16356 printf ("???\n");
16357 while (p < end && *(p++) != '\0' /* NUL terminator. */)
16358 ;
16359 break;
16360
16361 default:
16362 printf (_("<unknown: %d>\n"), tag);
16363 break;
16364 }
16365 return p;
16366
16367 case 1:
16368 return display_tag_value (-1, p, end);
16369 case 2:
16370 return display_tag_value (0, p, end);
16371
16372 default:
16373 assert (attr->type & 0x80);
16374 READ_ULEB (val, p, end);
16375 type = attr->type & 0x7f;
16376 if (val >= type)
16377 printf ("??? (%d)\n", val);
16378 else
16379 printf ("%s\n", attr->table[val]);
16380 return p;
16381 }
16382 }
16383
16384 return display_tag_value (tag, p, end);
16385 }
16386
16387 static unsigned char *
16388 display_gnu_attribute (unsigned char * p,
16389 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const),
16390 const unsigned char * const end)
16391 {
16392 unsigned int tag;
16393 unsigned int val;
16394
16395 READ_ULEB (tag, p, end);
16396
16397 /* Tag_compatibility is the only generic GNU attribute defined at
16398 present. */
16399 if (tag == 32)
16400 {
16401 READ_ULEB (val, p, end);
16402
16403 printf (_("flag = %d, vendor = "), val);
16404 if (p == end)
16405 {
16406 printf (_("<corrupt>\n"));
16407 warn (_("corrupt vendor attribute\n"));
16408 }
16409 else
16410 {
16411 if (p < end - 1)
16412 {
16413 size_t maxlen = (end - p) - 1;
16414
16415 print_symbol ((int) maxlen, (const char *) p);
16416 p += strnlen ((char *) p, maxlen) + 1;
16417 }
16418 else
16419 {
16420 printf (_("<corrupt>"));
16421 p = (unsigned char *) end;
16422 }
16423 putchar ('\n');
16424 }
16425 return p;
16426 }
16427
16428 if ((tag & 2) == 0 && display_proc_gnu_attribute)
16429 return display_proc_gnu_attribute (p, tag, end);
16430
16431 return display_tag_value (tag, p, end);
16432 }
16433
16434 static unsigned char *
16435 display_m68k_gnu_attribute (unsigned char * p,
16436 unsigned int tag,
16437 const unsigned char * const end)
16438 {
16439 unsigned int val;
16440
16441 if (tag == Tag_GNU_M68K_ABI_FP)
16442 {
16443 printf (" Tag_GNU_M68K_ABI_FP: ");
16444 if (p == end)
16445 {
16446 printf (_("<corrupt>\n"));
16447 return p;
16448 }
16449 READ_ULEB (val, p, end);
16450
16451 if (val > 3)
16452 printf ("(%#x), ", val);
16453
16454 switch (val & 3)
16455 {
16456 case 0:
16457 printf (_("unspecified hard/soft float\n"));
16458 break;
16459 case 1:
16460 printf (_("hard float\n"));
16461 break;
16462 case 2:
16463 printf (_("soft float\n"));
16464 break;
16465 }
16466 return p;
16467 }
16468
16469 return display_tag_value (tag & 1, p, end);
16470 }
16471
16472 static unsigned char *
16473 display_power_gnu_attribute (unsigned char * p,
16474 unsigned int tag,
16475 const unsigned char * const end)
16476 {
16477 unsigned int val;
16478
16479 if (tag == Tag_GNU_Power_ABI_FP)
16480 {
16481 printf (" Tag_GNU_Power_ABI_FP: ");
16482 if (p == end)
16483 {
16484 printf (_("<corrupt>\n"));
16485 return p;
16486 }
16487 READ_ULEB (val, p, end);
16488
16489 if (val > 15)
16490 printf ("(%#x), ", val);
16491
16492 switch (val & 3)
16493 {
16494 case 0:
16495 printf (_("unspecified hard/soft float, "));
16496 break;
16497 case 1:
16498 printf (_("hard float, "));
16499 break;
16500 case 2:
16501 printf (_("soft float, "));
16502 break;
16503 case 3:
16504 printf (_("single-precision hard float, "));
16505 break;
16506 }
16507
16508 switch (val & 0xC)
16509 {
16510 case 0:
16511 printf (_("unspecified long double\n"));
16512 break;
16513 case 4:
16514 printf (_("128-bit IBM long double\n"));
16515 break;
16516 case 8:
16517 printf (_("64-bit long double\n"));
16518 break;
16519 case 12:
16520 printf (_("128-bit IEEE long double\n"));
16521 break;
16522 }
16523 return p;
16524 }
16525
16526 if (tag == Tag_GNU_Power_ABI_Vector)
16527 {
16528 printf (" Tag_GNU_Power_ABI_Vector: ");
16529 if (p == end)
16530 {
16531 printf (_("<corrupt>\n"));
16532 return p;
16533 }
16534 READ_ULEB (val, p, end);
16535
16536 if (val > 3)
16537 printf ("(%#x), ", val);
16538
16539 switch (val & 3)
16540 {
16541 case 0:
16542 printf (_("unspecified\n"));
16543 break;
16544 case 1:
16545 printf (_("generic\n"));
16546 break;
16547 case 2:
16548 printf ("AltiVec\n");
16549 break;
16550 case 3:
16551 printf ("SPE\n");
16552 break;
16553 }
16554 return p;
16555 }
16556
16557 if (tag == Tag_GNU_Power_ABI_Struct_Return)
16558 {
16559 printf (" Tag_GNU_Power_ABI_Struct_Return: ");
16560 if (p == end)
16561 {
16562 printf (_("<corrupt>\n"));
16563 return p;
16564 }
16565 READ_ULEB (val, p, end);
16566
16567 if (val > 2)
16568 printf ("(%#x), ", val);
16569
16570 switch (val & 3)
16571 {
16572 case 0:
16573 printf (_("unspecified\n"));
16574 break;
16575 case 1:
16576 printf ("r3/r4\n");
16577 break;
16578 case 2:
16579 printf (_("memory\n"));
16580 break;
16581 case 3:
16582 printf ("???\n");
16583 break;
16584 }
16585 return p;
16586 }
16587
16588 return display_tag_value (tag & 1, p, end);
16589 }
16590
16591 static unsigned char *
16592 display_s390_gnu_attribute (unsigned char * p,
16593 unsigned int tag,
16594 const unsigned char * const end)
16595 {
16596 unsigned int val;
16597
16598 if (tag == Tag_GNU_S390_ABI_Vector)
16599 {
16600 printf (" Tag_GNU_S390_ABI_Vector: ");
16601 READ_ULEB (val, p, end);
16602
16603 switch (val)
16604 {
16605 case 0:
16606 printf (_("any\n"));
16607 break;
16608 case 1:
16609 printf (_("software\n"));
16610 break;
16611 case 2:
16612 printf (_("hardware\n"));
16613 break;
16614 default:
16615 printf ("??? (%d)\n", val);
16616 break;
16617 }
16618 return p;
16619 }
16620
16621 return display_tag_value (tag & 1, p, end);
16622 }
16623
16624 static void
16625 display_sparc_hwcaps (unsigned int mask)
16626 {
16627 if (mask)
16628 {
16629 bool first = true;
16630
16631 if (mask & ELF_SPARC_HWCAP_MUL32)
16632 fputs ("mul32", stdout), first = false;
16633 if (mask & ELF_SPARC_HWCAP_DIV32)
16634 printf ("%sdiv32", first ? "" : "|"), first = false;
16635 if (mask & ELF_SPARC_HWCAP_FSMULD)
16636 printf ("%sfsmuld", first ? "" : "|"), first = false;
16637 if (mask & ELF_SPARC_HWCAP_V8PLUS)
16638 printf ("%sv8plus", first ? "" : "|"), first = false;
16639 if (mask & ELF_SPARC_HWCAP_POPC)
16640 printf ("%spopc", first ? "" : "|"), first = false;
16641 if (mask & ELF_SPARC_HWCAP_VIS)
16642 printf ("%svis", first ? "" : "|"), first = false;
16643 if (mask & ELF_SPARC_HWCAP_VIS2)
16644 printf ("%svis2", first ? "" : "|"), first = false;
16645 if (mask & ELF_SPARC_HWCAP_ASI_BLK_INIT)
16646 printf ("%sASIBlkInit", first ? "" : "|"), first = false;
16647 if (mask & ELF_SPARC_HWCAP_FMAF)
16648 printf ("%sfmaf", first ? "" : "|"), first = false;
16649 if (mask & ELF_SPARC_HWCAP_VIS3)
16650 printf ("%svis3", first ? "" : "|"), first = false;
16651 if (mask & ELF_SPARC_HWCAP_HPC)
16652 printf ("%shpc", first ? "" : "|"), first = false;
16653 if (mask & ELF_SPARC_HWCAP_RANDOM)
16654 printf ("%srandom", first ? "" : "|"), first = false;
16655 if (mask & ELF_SPARC_HWCAP_TRANS)
16656 printf ("%strans", first ? "" : "|"), first = false;
16657 if (mask & ELF_SPARC_HWCAP_FJFMAU)
16658 printf ("%sfjfmau", first ? "" : "|"), first = false;
16659 if (mask & ELF_SPARC_HWCAP_IMA)
16660 printf ("%sima", first ? "" : "|"), first = false;
16661 if (mask & ELF_SPARC_HWCAP_ASI_CACHE_SPARING)
16662 printf ("%scspare", first ? "" : "|"), first = false;
16663 }
16664 else
16665 fputc ('0', stdout);
16666 fputc ('\n', stdout);
16667 }
16668
16669 static void
16670 display_sparc_hwcaps2 (unsigned int mask)
16671 {
16672 if (mask)
16673 {
16674 bool first = true;
16675
16676 if (mask & ELF_SPARC_HWCAP2_FJATHPLUS)
16677 fputs ("fjathplus", stdout), first = false;
16678 if (mask & ELF_SPARC_HWCAP2_VIS3B)
16679 printf ("%svis3b", first ? "" : "|"), first = false;
16680 if (mask & ELF_SPARC_HWCAP2_ADP)
16681 printf ("%sadp", first ? "" : "|"), first = false;
16682 if (mask & ELF_SPARC_HWCAP2_SPARC5)
16683 printf ("%ssparc5", first ? "" : "|"), first = false;
16684 if (mask & ELF_SPARC_HWCAP2_MWAIT)
16685 printf ("%smwait", first ? "" : "|"), first = false;
16686 if (mask & ELF_SPARC_HWCAP2_XMPMUL)
16687 printf ("%sxmpmul", first ? "" : "|"), first = false;
16688 if (mask & ELF_SPARC_HWCAP2_XMONT)
16689 printf ("%sxmont2", first ? "" : "|"), first = false;
16690 if (mask & ELF_SPARC_HWCAP2_NSEC)
16691 printf ("%snsec", first ? "" : "|"), first = false;
16692 if (mask & ELF_SPARC_HWCAP2_FJATHHPC)
16693 printf ("%sfjathhpc", first ? "" : "|"), first = false;
16694 if (mask & ELF_SPARC_HWCAP2_FJDES)
16695 printf ("%sfjdes", first ? "" : "|"), first = false;
16696 if (mask & ELF_SPARC_HWCAP2_FJAES)
16697 printf ("%sfjaes", first ? "" : "|"), first = false;
16698 }
16699 else
16700 fputc ('0', stdout);
16701 fputc ('\n', stdout);
16702 }
16703
16704 static unsigned char *
16705 display_sparc_gnu_attribute (unsigned char * p,
16706 unsigned int tag,
16707 const unsigned char * const end)
16708 {
16709 unsigned int val;
16710
16711 if (tag == Tag_GNU_Sparc_HWCAPS)
16712 {
16713 READ_ULEB (val, p, end);
16714 printf (" Tag_GNU_Sparc_HWCAPS: ");
16715 display_sparc_hwcaps (val);
16716 return p;
16717 }
16718 if (tag == Tag_GNU_Sparc_HWCAPS2)
16719 {
16720 READ_ULEB (val, p, end);
16721 printf (" Tag_GNU_Sparc_HWCAPS2: ");
16722 display_sparc_hwcaps2 (val);
16723 return p;
16724 }
16725
16726 return display_tag_value (tag, p, end);
16727 }
16728
16729 static void
16730 print_mips_fp_abi_value (unsigned int val)
16731 {
16732 switch (val)
16733 {
16734 case Val_GNU_MIPS_ABI_FP_ANY:
16735 printf (_("Hard or soft float\n"));
16736 break;
16737 case Val_GNU_MIPS_ABI_FP_DOUBLE:
16738 printf (_("Hard float (double precision)\n"));
16739 break;
16740 case Val_GNU_MIPS_ABI_FP_SINGLE:
16741 printf (_("Hard float (single precision)\n"));
16742 break;
16743 case Val_GNU_MIPS_ABI_FP_SOFT:
16744 printf (_("Soft float\n"));
16745 break;
16746 case Val_GNU_MIPS_ABI_FP_OLD_64:
16747 printf (_("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
16748 break;
16749 case Val_GNU_MIPS_ABI_FP_XX:
16750 printf (_("Hard float (32-bit CPU, Any FPU)\n"));
16751 break;
16752 case Val_GNU_MIPS_ABI_FP_64:
16753 printf (_("Hard float (32-bit CPU, 64-bit FPU)\n"));
16754 break;
16755 case Val_GNU_MIPS_ABI_FP_64A:
16756 printf (_("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
16757 break;
16758 case Val_GNU_MIPS_ABI_FP_NAN2008:
16759 printf (_("NaN 2008 compatibility\n"));
16760 break;
16761 default:
16762 printf ("??? (%d)\n", val);
16763 break;
16764 }
16765 }
16766
16767 static unsigned char *
16768 display_mips_gnu_attribute (unsigned char * p,
16769 unsigned int tag,
16770 const unsigned char * const end)
16771 {
16772 if (tag == Tag_GNU_MIPS_ABI_FP)
16773 {
16774 unsigned int val;
16775
16776 printf (" Tag_GNU_MIPS_ABI_FP: ");
16777 READ_ULEB (val, p, end);
16778 print_mips_fp_abi_value (val);
16779 return p;
16780 }
16781
16782 if (tag == Tag_GNU_MIPS_ABI_MSA)
16783 {
16784 unsigned int val;
16785
16786 printf (" Tag_GNU_MIPS_ABI_MSA: ");
16787 READ_ULEB (val, p, end);
16788
16789 switch (val)
16790 {
16791 case Val_GNU_MIPS_ABI_MSA_ANY:
16792 printf (_("Any MSA or not\n"));
16793 break;
16794 case Val_GNU_MIPS_ABI_MSA_128:
16795 printf (_("128-bit MSA\n"));
16796 break;
16797 default:
16798 printf ("??? (%d)\n", val);
16799 break;
16800 }
16801 return p;
16802 }
16803
16804 return display_tag_value (tag & 1, p, end);
16805 }
16806
16807 static unsigned char *
16808 display_tic6x_attribute (unsigned char * p,
16809 const unsigned char * const end)
16810 {
16811 unsigned int tag;
16812 unsigned int val;
16813
16814 READ_ULEB (tag, p, end);
16815
16816 switch (tag)
16817 {
16818 case Tag_ISA:
16819 printf (" Tag_ISA: ");
16820 READ_ULEB (val, p, end);
16821
16822 switch (val)
16823 {
16824 case C6XABI_Tag_ISA_none:
16825 printf (_("None\n"));
16826 break;
16827 case C6XABI_Tag_ISA_C62X:
16828 printf ("C62x\n");
16829 break;
16830 case C6XABI_Tag_ISA_C67X:
16831 printf ("C67x\n");
16832 break;
16833 case C6XABI_Tag_ISA_C67XP:
16834 printf ("C67x+\n");
16835 break;
16836 case C6XABI_Tag_ISA_C64X:
16837 printf ("C64x\n");
16838 break;
16839 case C6XABI_Tag_ISA_C64XP:
16840 printf ("C64x+\n");
16841 break;
16842 case C6XABI_Tag_ISA_C674X:
16843 printf ("C674x\n");
16844 break;
16845 default:
16846 printf ("??? (%d)\n", val);
16847 break;
16848 }
16849 return p;
16850
16851 case Tag_ABI_wchar_t:
16852 printf (" Tag_ABI_wchar_t: ");
16853 READ_ULEB (val, p, end);
16854 switch (val)
16855 {
16856 case 0:
16857 printf (_("Not used\n"));
16858 break;
16859 case 1:
16860 printf (_("2 bytes\n"));
16861 break;
16862 case 2:
16863 printf (_("4 bytes\n"));
16864 break;
16865 default:
16866 printf ("??? (%d)\n", val);
16867 break;
16868 }
16869 return p;
16870
16871 case Tag_ABI_stack_align_needed:
16872 printf (" Tag_ABI_stack_align_needed: ");
16873 READ_ULEB (val, p, end);
16874 switch (val)
16875 {
16876 case 0:
16877 printf (_("8-byte\n"));
16878 break;
16879 case 1:
16880 printf (_("16-byte\n"));
16881 break;
16882 default:
16883 printf ("??? (%d)\n", val);
16884 break;
16885 }
16886 return p;
16887
16888 case Tag_ABI_stack_align_preserved:
16889 READ_ULEB (val, p, end);
16890 printf (" Tag_ABI_stack_align_preserved: ");
16891 switch (val)
16892 {
16893 case 0:
16894 printf (_("8-byte\n"));
16895 break;
16896 case 1:
16897 printf (_("16-byte\n"));
16898 break;
16899 default:
16900 printf ("??? (%d)\n", val);
16901 break;
16902 }
16903 return p;
16904
16905 case Tag_ABI_DSBT:
16906 READ_ULEB (val, p, end);
16907 printf (" Tag_ABI_DSBT: ");
16908 switch (val)
16909 {
16910 case 0:
16911 printf (_("DSBT addressing not used\n"));
16912 break;
16913 case 1:
16914 printf (_("DSBT addressing used\n"));
16915 break;
16916 default:
16917 printf ("??? (%d)\n", val);
16918 break;
16919 }
16920 return p;
16921
16922 case Tag_ABI_PID:
16923 READ_ULEB (val, p, end);
16924 printf (" Tag_ABI_PID: ");
16925 switch (val)
16926 {
16927 case 0:
16928 printf (_("Data addressing position-dependent\n"));
16929 break;
16930 case 1:
16931 printf (_("Data addressing position-independent, GOT near DP\n"));
16932 break;
16933 case 2:
16934 printf (_("Data addressing position-independent, GOT far from DP\n"));
16935 break;
16936 default:
16937 printf ("??? (%d)\n", val);
16938 break;
16939 }
16940 return p;
16941
16942 case Tag_ABI_PIC:
16943 READ_ULEB (val, p, end);
16944 printf (" Tag_ABI_PIC: ");
16945 switch (val)
16946 {
16947 case 0:
16948 printf (_("Code addressing position-dependent\n"));
16949 break;
16950 case 1:
16951 printf (_("Code addressing position-independent\n"));
16952 break;
16953 default:
16954 printf ("??? (%d)\n", val);
16955 break;
16956 }
16957 return p;
16958
16959 case Tag_ABI_array_object_alignment:
16960 READ_ULEB (val, p, end);
16961 printf (" Tag_ABI_array_object_alignment: ");
16962 switch (val)
16963 {
16964 case 0:
16965 printf (_("8-byte\n"));
16966 break;
16967 case 1:
16968 printf (_("4-byte\n"));
16969 break;
16970 case 2:
16971 printf (_("16-byte\n"));
16972 break;
16973 default:
16974 printf ("??? (%d)\n", val);
16975 break;
16976 }
16977 return p;
16978
16979 case Tag_ABI_array_object_align_expected:
16980 READ_ULEB (val, p, end);
16981 printf (" Tag_ABI_array_object_align_expected: ");
16982 switch (val)
16983 {
16984 case 0:
16985 printf (_("8-byte\n"));
16986 break;
16987 case 1:
16988 printf (_("4-byte\n"));
16989 break;
16990 case 2:
16991 printf (_("16-byte\n"));
16992 break;
16993 default:
16994 printf ("??? (%d)\n", val);
16995 break;
16996 }
16997 return p;
16998
16999 case Tag_ABI_compatibility:
17000 {
17001 READ_ULEB (val, p, end);
17002 printf (" Tag_ABI_compatibility: ");
17003 printf (_("flag = %d, vendor = "), val);
17004 if (p < end - 1)
17005 {
17006 size_t maxlen = (end - p) - 1;
17007
17008 print_symbol ((int) maxlen, (const char *) p);
17009 p += strnlen ((char *) p, maxlen) + 1;
17010 }
17011 else
17012 {
17013 printf (_("<corrupt>"));
17014 p = (unsigned char *) end;
17015 }
17016 putchar ('\n');
17017 return p;
17018 }
17019
17020 case Tag_ABI_conformance:
17021 {
17022 printf (" Tag_ABI_conformance: \"");
17023 if (p < end - 1)
17024 {
17025 size_t maxlen = (end - p) - 1;
17026
17027 print_symbol ((int) maxlen, (const char *) p);
17028 p += strnlen ((char *) p, maxlen) + 1;
17029 }
17030 else
17031 {
17032 printf (_("<corrupt>"));
17033 p = (unsigned char *) end;
17034 }
17035 printf ("\"\n");
17036 return p;
17037 }
17038 }
17039
17040 return display_tag_value (tag, p, end);
17041 }
17042
17043 static void
17044 display_raw_attribute (unsigned char * p, unsigned char const * const end)
17045 {
17046 unsigned long addr = 0;
17047 size_t bytes = end - p;
17048
17049 assert (end >= p);
17050 while (bytes)
17051 {
17052 int j;
17053 int k;
17054 int lbytes = (bytes > 16 ? 16 : bytes);
17055
17056 printf (" 0x%8.8lx ", addr);
17057
17058 for (j = 0; j < 16; j++)
17059 {
17060 if (j < lbytes)
17061 printf ("%2.2x", p[j]);
17062 else
17063 printf (" ");
17064
17065 if ((j & 3) == 3)
17066 printf (" ");
17067 }
17068
17069 for (j = 0; j < lbytes; j++)
17070 {
17071 k = p[j];
17072 if (k >= ' ' && k < 0x7f)
17073 printf ("%c", k);
17074 else
17075 printf (".");
17076 }
17077
17078 putchar ('\n');
17079
17080 p += lbytes;
17081 bytes -= lbytes;
17082 addr += lbytes;
17083 }
17084
17085 putchar ('\n');
17086 }
17087
17088 static unsigned char *
17089 display_msp430_attribute (unsigned char * p,
17090 const unsigned char * const end)
17091 {
17092 unsigned int val;
17093 unsigned int tag;
17094
17095 READ_ULEB (tag, p, end);
17096
17097 switch (tag)
17098 {
17099 case OFBA_MSPABI_Tag_ISA:
17100 printf (" Tag_ISA: ");
17101 READ_ULEB (val, p, end);
17102 switch (val)
17103 {
17104 case 0: printf (_("None\n")); break;
17105 case 1: printf (_("MSP430\n")); break;
17106 case 2: printf (_("MSP430X\n")); break;
17107 default: printf ("??? (%d)\n", val); break;
17108 }
17109 break;
17110
17111 case OFBA_MSPABI_Tag_Code_Model:
17112 printf (" Tag_Code_Model: ");
17113 READ_ULEB (val, p, end);
17114 switch (val)
17115 {
17116 case 0: printf (_("None\n")); break;
17117 case 1: printf (_("Small\n")); break;
17118 case 2: printf (_("Large\n")); break;
17119 default: printf ("??? (%d)\n", val); break;
17120 }
17121 break;
17122
17123 case OFBA_MSPABI_Tag_Data_Model:
17124 printf (" Tag_Data_Model: ");
17125 READ_ULEB (val, p, end);
17126 switch (val)
17127 {
17128 case 0: printf (_("None\n")); break;
17129 case 1: printf (_("Small\n")); break;
17130 case 2: printf (_("Large\n")); break;
17131 case 3: printf (_("Restricted Large\n")); break;
17132 default: printf ("??? (%d)\n", val); break;
17133 }
17134 break;
17135
17136 default:
17137 printf (_(" <unknown tag %d>: "), tag);
17138
17139 if (tag & 1)
17140 {
17141 putchar ('"');
17142 if (p < end - 1)
17143 {
17144 size_t maxlen = (end - p) - 1;
17145
17146 print_symbol ((int) maxlen, (const char *) p);
17147 p += strnlen ((char *) p, maxlen) + 1;
17148 }
17149 else
17150 {
17151 printf (_("<corrupt>"));
17152 p = (unsigned char *) end;
17153 }
17154 printf ("\"\n");
17155 }
17156 else
17157 {
17158 READ_ULEB (val, p, end);
17159 printf ("%d (0x%x)\n", val, val);
17160 }
17161 break;
17162 }
17163
17164 assert (p <= end);
17165 return p;
17166 }
17167
17168 static unsigned char *
17169 display_msp430_gnu_attribute (unsigned char * p,
17170 unsigned int tag,
17171 const unsigned char * const end)
17172 {
17173 if (tag == Tag_GNU_MSP430_Data_Region)
17174 {
17175 unsigned int val;
17176
17177 printf (" Tag_GNU_MSP430_Data_Region: ");
17178 READ_ULEB (val, p, end);
17179
17180 switch (val)
17181 {
17182 case Val_GNU_MSP430_Data_Region_Any:
17183 printf (_("Any Region\n"));
17184 break;
17185 case Val_GNU_MSP430_Data_Region_Lower:
17186 printf (_("Lower Region Only\n"));
17187 break;
17188 default:
17189 printf ("??? (%u)\n", val);
17190 }
17191 return p;
17192 }
17193 return display_tag_value (tag & 1, p, end);
17194 }
17195
17196 struct riscv_attr_tag_t {
17197 const char *name;
17198 unsigned int tag;
17199 };
17200
17201 static struct riscv_attr_tag_t riscv_attr_tag[] =
17202 {
17203 #define T(tag) {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
17204 T(arch),
17205 T(priv_spec),
17206 T(priv_spec_minor),
17207 T(priv_spec_revision),
17208 T(unaligned_access),
17209 T(stack_align),
17210 #undef T
17211 };
17212
17213 static unsigned char *
17214 display_riscv_attribute (unsigned char *p,
17215 const unsigned char * const end)
17216 {
17217 unsigned int val;
17218 unsigned int tag;
17219 struct riscv_attr_tag_t *attr = NULL;
17220 unsigned i;
17221
17222 READ_ULEB (tag, p, end);
17223
17224 /* Find the name of attribute. */
17225 for (i = 0; i < ARRAY_SIZE (riscv_attr_tag); i++)
17226 {
17227 if (riscv_attr_tag[i].tag == tag)
17228 {
17229 attr = &riscv_attr_tag[i];
17230 break;
17231 }
17232 }
17233
17234 if (attr)
17235 printf (" %s: ", attr->name);
17236 else
17237 return display_tag_value (tag, p, end);
17238
17239 switch (tag)
17240 {
17241 case Tag_RISCV_priv_spec:
17242 case Tag_RISCV_priv_spec_minor:
17243 case Tag_RISCV_priv_spec_revision:
17244 READ_ULEB (val, p, end);
17245 printf (_("%u\n"), val);
17246 break;
17247 case Tag_RISCV_unaligned_access:
17248 READ_ULEB (val, p, end);
17249 switch (val)
17250 {
17251 case 0:
17252 printf (_("No unaligned access\n"));
17253 break;
17254 case 1:
17255 printf (_("Unaligned access\n"));
17256 break;
17257 }
17258 break;
17259 case Tag_RISCV_stack_align:
17260 READ_ULEB (val, p, end);
17261 printf (_("%u-bytes\n"), val);
17262 break;
17263 case Tag_RISCV_arch:
17264 p = display_tag_value (-1, p, end);
17265 break;
17266 default:
17267 return display_tag_value (tag, p, end);
17268 }
17269
17270 return p;
17271 }
17272
17273 static unsigned char *
17274 display_csky_attribute (unsigned char * p,
17275 const unsigned char * const end)
17276 {
17277 unsigned int tag;
17278 unsigned int val;
17279 READ_ULEB (tag, p, end);
17280
17281 if (tag >= Tag_CSKY_MAX)
17282 {
17283 return display_tag_value (-1, p, end);
17284 }
17285
17286 switch (tag)
17287 {
17288 case Tag_CSKY_ARCH_NAME:
17289 printf (" Tag_CSKY_ARCH_NAME:\t\t");
17290 return display_tag_value (-1, p, end);
17291 case Tag_CSKY_CPU_NAME:
17292 printf (" Tag_CSKY_CPU_NAME:\t\t");
17293 return display_tag_value (-1, p, end);
17294
17295 case Tag_CSKY_ISA_FLAGS:
17296 printf (" Tag_CSKY_ISA_FLAGS:\t\t");
17297 return display_tag_value (0, p, end);
17298 case Tag_CSKY_ISA_EXT_FLAGS:
17299 printf (" Tag_CSKY_ISA_EXT_FLAGS:\t");
17300 return display_tag_value (0, p, end);
17301
17302 case Tag_CSKY_DSP_VERSION:
17303 printf (" Tag_CSKY_DSP_VERSION:\t\t");
17304 READ_ULEB (val, p, end);
17305 if (val == VAL_CSKY_DSP_VERSION_EXTENSION)
17306 printf ("DSP Extension\n");
17307 else if (val == VAL_CSKY_DSP_VERSION_2)
17308 printf ("DSP 2.0\n");
17309 break;
17310
17311 case Tag_CSKY_VDSP_VERSION:
17312 printf (" Tag_CSKY_VDSP_VERSION:\t");
17313 READ_ULEB (val, p, end);
17314 printf ("VDSP Version %d\n", val);
17315 break;
17316
17317 case Tag_CSKY_FPU_VERSION:
17318 printf (" Tag_CSKY_FPU_VERSION:\t\t");
17319 READ_ULEB (val, p, end);
17320 if (val == VAL_CSKY_FPU_VERSION_1)
17321 printf ("ABIV1 FPU Version 1\n");
17322 else if (val == VAL_CSKY_FPU_VERSION_2)
17323 printf ("FPU Version 2\n");
17324 break;
17325
17326 case Tag_CSKY_FPU_ABI:
17327 printf (" Tag_CSKY_FPU_ABI:\t\t");
17328 READ_ULEB (val, p, end);
17329 if (val == VAL_CSKY_FPU_ABI_HARD)
17330 printf ("Hard\n");
17331 else if (val == VAL_CSKY_FPU_ABI_SOFTFP)
17332 printf ("SoftFP\n");
17333 else if (val == VAL_CSKY_FPU_ABI_SOFT)
17334 printf ("Soft\n");
17335 break;
17336 case Tag_CSKY_FPU_ROUNDING:
17337 READ_ULEB (val, p, end);
17338 if (val == 1)
17339 {
17340 printf (" Tag_CSKY_FPU_ROUNDING:\t");
17341 printf ("Needed\n");
17342 }
17343 break;
17344 case Tag_CSKY_FPU_DENORMAL:
17345 READ_ULEB (val, p, end);
17346 if (val == 1)
17347 {
17348 printf (" Tag_CSKY_FPU_DENORMAL:\t");
17349 printf ("Needed\n");
17350 }
17351 break;
17352 case Tag_CSKY_FPU_Exception:
17353 READ_ULEB (val, p, end);
17354 if (val == 1)
17355 {
17356 printf (" Tag_CSKY_FPU_Exception:\t");
17357 printf ("Needed\n");
17358 }
17359 break;
17360 case Tag_CSKY_FPU_NUMBER_MODULE:
17361 printf (" Tag_CSKY_FPU_NUMBER_MODULE:\t");
17362 return display_tag_value (-1, p, end);
17363 case Tag_CSKY_FPU_HARDFP:
17364 printf (" Tag_CSKY_FPU_HARDFP:\t\t");
17365 READ_ULEB (val, p, end);
17366 if (val & VAL_CSKY_FPU_HARDFP_HALF)
17367 printf (" Half");
17368 if (val & VAL_CSKY_FPU_HARDFP_SINGLE)
17369 printf (" Single");
17370 if (val & VAL_CSKY_FPU_HARDFP_DOUBLE)
17371 printf (" Double");
17372 printf ("\n");
17373 break;
17374 default:
17375 return display_tag_value (tag, p, end);
17376 }
17377 return p;
17378 }
17379
17380 static bool
17381 process_attributes (Filedata * filedata,
17382 const char * public_name,
17383 unsigned int proc_type,
17384 unsigned char * (* display_pub_attribute) (unsigned char *, const unsigned char * const),
17385 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const))
17386 {
17387 Elf_Internal_Shdr * sect;
17388 unsigned i;
17389 bool res = true;
17390
17391 /* Find the section header so that we get the size. */
17392 for (i = 0, sect = filedata->section_headers;
17393 i < filedata->file_header.e_shnum;
17394 i++, sect++)
17395 {
17396 unsigned char * contents;
17397 unsigned char * p;
17398
17399 if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
17400 continue;
17401
17402 contents = (unsigned char *) get_data (NULL, filedata, sect->sh_offset, 1,
17403 sect->sh_size, _("attributes"));
17404 if (contents == NULL)
17405 {
17406 res = false;
17407 continue;
17408 }
17409
17410 p = contents;
17411 /* The first character is the version of the attributes.
17412 Currently only version 1, (aka 'A') is recognised here. */
17413 if (*p != 'A')
17414 {
17415 printf (_("Unknown attributes version '%c'(%d) - expecting 'A'\n"), *p, *p);
17416 res = false;
17417 }
17418 else
17419 {
17420 bfd_vma section_len;
17421
17422 section_len = sect->sh_size - 1;
17423 p++;
17424
17425 while (section_len > 0)
17426 {
17427 bfd_vma attr_len;
17428 unsigned int namelen;
17429 bool public_section;
17430 bool gnu_section;
17431
17432 if (section_len <= 4)
17433 {
17434 error (_("Tag section ends prematurely\n"));
17435 res = false;
17436 break;
17437 }
17438 attr_len = byte_get (p, 4);
17439 p += 4;
17440
17441 if (attr_len > section_len)
17442 {
17443 error (_("Bad attribute length (%u > %u)\n"),
17444 (unsigned) attr_len, (unsigned) section_len);
17445 attr_len = section_len;
17446 res = false;
17447 }
17448 /* PR 17531: file: 001-101425-0.004 */
17449 else if (attr_len < 5)
17450 {
17451 error (_("Attribute length of %u is too small\n"), (unsigned) attr_len);
17452 res = false;
17453 break;
17454 }
17455
17456 section_len -= attr_len;
17457 attr_len -= 4;
17458
17459 namelen = strnlen ((char *) p, attr_len) + 1;
17460 if (namelen == 0 || namelen >= attr_len)
17461 {
17462 error (_("Corrupt attribute section name\n"));
17463 res = false;
17464 break;
17465 }
17466
17467 printf (_("Attribute Section: "));
17468 print_symbol (INT_MAX, (const char *) p);
17469 putchar ('\n');
17470
17471 if (public_name && streq ((char *) p, public_name))
17472 public_section = true;
17473 else
17474 public_section = false;
17475
17476 if (streq ((char *) p, "gnu"))
17477 gnu_section = true;
17478 else
17479 gnu_section = false;
17480
17481 p += namelen;
17482 attr_len -= namelen;
17483
17484 while (attr_len > 0 && p < contents + sect->sh_size)
17485 {
17486 int tag;
17487 unsigned int val;
17488 bfd_vma size;
17489 unsigned char * end;
17490
17491 /* PR binutils/17531: Safe handling of corrupt files. */
17492 if (attr_len < 6)
17493 {
17494 error (_("Unused bytes at end of section\n"));
17495 res = false;
17496 section_len = 0;
17497 break;
17498 }
17499
17500 tag = *(p++);
17501 size = byte_get (p, 4);
17502 if (size > attr_len)
17503 {
17504 error (_("Bad subsection length (%u > %u)\n"),
17505 (unsigned) size, (unsigned) attr_len);
17506 res = false;
17507 size = attr_len;
17508 }
17509 /* PR binutils/17531: Safe handling of corrupt files. */
17510 if (size < 6)
17511 {
17512 error (_("Bad subsection length (%u < 6)\n"),
17513 (unsigned) size);
17514 res = false;
17515 section_len = 0;
17516 break;
17517 }
17518
17519 attr_len -= size;
17520 end = p + size - 1;
17521 assert (end <= contents + sect->sh_size);
17522 p += 4;
17523
17524 switch (tag)
17525 {
17526 case 1:
17527 printf (_("File Attributes\n"));
17528 break;
17529 case 2:
17530 printf (_("Section Attributes:"));
17531 goto do_numlist;
17532 case 3:
17533 printf (_("Symbol Attributes:"));
17534 /* Fall through. */
17535 do_numlist:
17536 for (;;)
17537 {
17538 READ_ULEB (val, p, end);
17539 if (val == 0)
17540 break;
17541 printf (" %d", val);
17542 }
17543 printf ("\n");
17544 break;
17545 default:
17546 printf (_("Unknown tag: %d\n"), tag);
17547 public_section = false;
17548 break;
17549 }
17550
17551 if (public_section && display_pub_attribute != NULL)
17552 {
17553 while (p < end)
17554 p = display_pub_attribute (p, end);
17555 assert (p == end);
17556 }
17557 else if (gnu_section && display_proc_gnu_attribute != NULL)
17558 {
17559 while (p < end)
17560 p = display_gnu_attribute (p,
17561 display_proc_gnu_attribute,
17562 end);
17563 assert (p == end);
17564 }
17565 else if (p < end)
17566 {
17567 printf (_(" Unknown attribute:\n"));
17568 display_raw_attribute (p, end);
17569 p = end;
17570 }
17571 else
17572 attr_len = 0;
17573 }
17574 }
17575 }
17576
17577 free (contents);
17578 }
17579
17580 return res;
17581 }
17582
17583 /* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
17584 Print the Address, Access and Initial fields of an entry at VMA ADDR
17585 and return the VMA of the next entry, or -1 if there was a problem.
17586 Does not read from DATA_END or beyond. */
17587
17588 static bfd_vma
17589 print_mips_got_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr,
17590 unsigned char * data_end)
17591 {
17592 printf (" ");
17593 print_vma (addr, LONG_HEX);
17594 printf (" ");
17595 if (addr < pltgot + 0xfff0)
17596 printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
17597 else
17598 printf ("%10s", "");
17599 printf (" ");
17600 if (data == NULL)
17601 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
17602 else
17603 {
17604 bfd_vma entry;
17605 unsigned char * from = data + addr - pltgot;
17606
17607 if (from + (is_32bit_elf ? 4 : 8) > data_end)
17608 {
17609 warn (_("MIPS GOT entry extends beyond the end of available data\n"));
17610 printf ("%*s", is_32bit_elf ? 8 : 16, _("<corrupt>"));
17611 return (bfd_vma) -1;
17612 }
17613 else
17614 {
17615 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
17616 print_vma (entry, LONG_HEX);
17617 }
17618 }
17619 return addr + (is_32bit_elf ? 4 : 8);
17620 }
17621
17622 /* DATA points to the contents of a MIPS PLT GOT that starts at VMA
17623 PLTGOT. Print the Address and Initial fields of an entry at VMA
17624 ADDR and return the VMA of the next entry. */
17625
17626 static bfd_vma
17627 print_mips_pltgot_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr)
17628 {
17629 printf (" ");
17630 print_vma (addr, LONG_HEX);
17631 printf (" ");
17632 if (data == NULL)
17633 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
17634 else
17635 {
17636 bfd_vma entry;
17637
17638 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
17639 print_vma (entry, LONG_HEX);
17640 }
17641 return addr + (is_32bit_elf ? 4 : 8);
17642 }
17643
17644 static void
17645 print_mips_ases (unsigned int mask)
17646 {
17647 if (mask & AFL_ASE_DSP)
17648 fputs ("\n\tDSP ASE", stdout);
17649 if (mask & AFL_ASE_DSPR2)
17650 fputs ("\n\tDSP R2 ASE", stdout);
17651 if (mask & AFL_ASE_DSPR3)
17652 fputs ("\n\tDSP R3 ASE", stdout);
17653 if (mask & AFL_ASE_EVA)
17654 fputs ("\n\tEnhanced VA Scheme", stdout);
17655 if (mask & AFL_ASE_MCU)
17656 fputs ("\n\tMCU (MicroController) ASE", stdout);
17657 if (mask & AFL_ASE_MDMX)
17658 fputs ("\n\tMDMX ASE", stdout);
17659 if (mask & AFL_ASE_MIPS3D)
17660 fputs ("\n\tMIPS-3D ASE", stdout);
17661 if (mask & AFL_ASE_MT)
17662 fputs ("\n\tMT ASE", stdout);
17663 if (mask & AFL_ASE_SMARTMIPS)
17664 fputs ("\n\tSmartMIPS ASE", stdout);
17665 if (mask & AFL_ASE_VIRT)
17666 fputs ("\n\tVZ ASE", stdout);
17667 if (mask & AFL_ASE_MSA)
17668 fputs ("\n\tMSA ASE", stdout);
17669 if (mask & AFL_ASE_MIPS16)
17670 fputs ("\n\tMIPS16 ASE", stdout);
17671 if (mask & AFL_ASE_MICROMIPS)
17672 fputs ("\n\tMICROMIPS ASE", stdout);
17673 if (mask & AFL_ASE_XPA)
17674 fputs ("\n\tXPA ASE", stdout);
17675 if (mask & AFL_ASE_MIPS16E2)
17676 fputs ("\n\tMIPS16e2 ASE", stdout);
17677 if (mask & AFL_ASE_CRC)
17678 fputs ("\n\tCRC ASE", stdout);
17679 if (mask & AFL_ASE_GINV)
17680 fputs ("\n\tGINV ASE", stdout);
17681 if (mask & AFL_ASE_LOONGSON_MMI)
17682 fputs ("\n\tLoongson MMI ASE", stdout);
17683 if (mask & AFL_ASE_LOONGSON_CAM)
17684 fputs ("\n\tLoongson CAM ASE", stdout);
17685 if (mask & AFL_ASE_LOONGSON_EXT)
17686 fputs ("\n\tLoongson EXT ASE", stdout);
17687 if (mask & AFL_ASE_LOONGSON_EXT2)
17688 fputs ("\n\tLoongson EXT2 ASE", stdout);
17689 if (mask == 0)
17690 fprintf (stdout, "\n\t%s", _("None"));
17691 else if ((mask & ~AFL_ASE_MASK) != 0)
17692 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
17693 }
17694
17695 static void
17696 print_mips_isa_ext (unsigned int isa_ext)
17697 {
17698 switch (isa_ext)
17699 {
17700 case 0:
17701 fputs (_("None"), stdout);
17702 break;
17703 case AFL_EXT_XLR:
17704 fputs ("RMI XLR", stdout);
17705 break;
17706 case AFL_EXT_OCTEON3:
17707 fputs ("Cavium Networks Octeon3", stdout);
17708 break;
17709 case AFL_EXT_OCTEON2:
17710 fputs ("Cavium Networks Octeon2", stdout);
17711 break;
17712 case AFL_EXT_OCTEONP:
17713 fputs ("Cavium Networks OcteonP", stdout);
17714 break;
17715 case AFL_EXT_OCTEON:
17716 fputs ("Cavium Networks Octeon", stdout);
17717 break;
17718 case AFL_EXT_5900:
17719 fputs ("Toshiba R5900", stdout);
17720 break;
17721 case AFL_EXT_4650:
17722 fputs ("MIPS R4650", stdout);
17723 break;
17724 case AFL_EXT_4010:
17725 fputs ("LSI R4010", stdout);
17726 break;
17727 case AFL_EXT_4100:
17728 fputs ("NEC VR4100", stdout);
17729 break;
17730 case AFL_EXT_3900:
17731 fputs ("Toshiba R3900", stdout);
17732 break;
17733 case AFL_EXT_10000:
17734 fputs ("MIPS R10000", stdout);
17735 break;
17736 case AFL_EXT_SB1:
17737 fputs ("Broadcom SB-1", stdout);
17738 break;
17739 case AFL_EXT_4111:
17740 fputs ("NEC VR4111/VR4181", stdout);
17741 break;
17742 case AFL_EXT_4120:
17743 fputs ("NEC VR4120", stdout);
17744 break;
17745 case AFL_EXT_5400:
17746 fputs ("NEC VR5400", stdout);
17747 break;
17748 case AFL_EXT_5500:
17749 fputs ("NEC VR5500", stdout);
17750 break;
17751 case AFL_EXT_LOONGSON_2E:
17752 fputs ("ST Microelectronics Loongson 2E", stdout);
17753 break;
17754 case AFL_EXT_LOONGSON_2F:
17755 fputs ("ST Microelectronics Loongson 2F", stdout);
17756 break;
17757 case AFL_EXT_INTERAPTIV_MR2:
17758 fputs ("Imagination interAptiv MR2", stdout);
17759 break;
17760 default:
17761 fprintf (stdout, "%s (%d)", _("Unknown"), isa_ext);
17762 }
17763 }
17764
17765 static signed int
17766 get_mips_reg_size (int reg_size)
17767 {
17768 return (reg_size == AFL_REG_NONE) ? 0
17769 : (reg_size == AFL_REG_32) ? 32
17770 : (reg_size == AFL_REG_64) ? 64
17771 : (reg_size == AFL_REG_128) ? 128
17772 : -1;
17773 }
17774
17775 static bool
17776 process_mips_specific (Filedata * filedata)
17777 {
17778 Elf_Internal_Dyn * entry;
17779 Elf_Internal_Shdr *sect = NULL;
17780 size_t liblist_offset = 0;
17781 size_t liblistno = 0;
17782 size_t conflictsno = 0;
17783 size_t options_offset = 0;
17784 size_t conflicts_offset = 0;
17785 size_t pltrelsz = 0;
17786 size_t pltrel = 0;
17787 bfd_vma pltgot = 0;
17788 bfd_vma mips_pltgot = 0;
17789 bfd_vma jmprel = 0;
17790 bfd_vma local_gotno = 0;
17791 bfd_vma gotsym = 0;
17792 bfd_vma symtabno = 0;
17793 bool res = true;
17794
17795 if (! process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
17796 display_mips_gnu_attribute))
17797 res = false;
17798
17799 sect = find_section (filedata, ".MIPS.abiflags");
17800
17801 if (sect != NULL)
17802 {
17803 Elf_External_ABIFlags_v0 *abiflags_ext;
17804 Elf_Internal_ABIFlags_v0 abiflags_in;
17805
17806 if (sizeof (Elf_External_ABIFlags_v0) != sect->sh_size)
17807 {
17808 error (_("Corrupt MIPS ABI Flags section.\n"));
17809 res = false;
17810 }
17811 else
17812 {
17813 abiflags_ext = get_data (NULL, filedata, sect->sh_offset, 1,
17814 sect->sh_size, _("MIPS ABI Flags section"));
17815 if (abiflags_ext)
17816 {
17817 abiflags_in.version = BYTE_GET (abiflags_ext->version);
17818 abiflags_in.isa_level = BYTE_GET (abiflags_ext->isa_level);
17819 abiflags_in.isa_rev = BYTE_GET (abiflags_ext->isa_rev);
17820 abiflags_in.gpr_size = BYTE_GET (abiflags_ext->gpr_size);
17821 abiflags_in.cpr1_size = BYTE_GET (abiflags_ext->cpr1_size);
17822 abiflags_in.cpr2_size = BYTE_GET (abiflags_ext->cpr2_size);
17823 abiflags_in.fp_abi = BYTE_GET (abiflags_ext->fp_abi);
17824 abiflags_in.isa_ext = BYTE_GET (abiflags_ext->isa_ext);
17825 abiflags_in.ases = BYTE_GET (abiflags_ext->ases);
17826 abiflags_in.flags1 = BYTE_GET (abiflags_ext->flags1);
17827 abiflags_in.flags2 = BYTE_GET (abiflags_ext->flags2);
17828
17829 printf ("\nMIPS ABI Flags Version: %d\n", abiflags_in.version);
17830 printf ("\nISA: MIPS%d", abiflags_in.isa_level);
17831 if (abiflags_in.isa_rev > 1)
17832 printf ("r%d", abiflags_in.isa_rev);
17833 printf ("\nGPR size: %d",
17834 get_mips_reg_size (abiflags_in.gpr_size));
17835 printf ("\nCPR1 size: %d",
17836 get_mips_reg_size (abiflags_in.cpr1_size));
17837 printf ("\nCPR2 size: %d",
17838 get_mips_reg_size (abiflags_in.cpr2_size));
17839 fputs ("\nFP ABI: ", stdout);
17840 print_mips_fp_abi_value (abiflags_in.fp_abi);
17841 fputs ("ISA Extension: ", stdout);
17842 print_mips_isa_ext (abiflags_in.isa_ext);
17843 fputs ("\nASEs:", stdout);
17844 print_mips_ases (abiflags_in.ases);
17845 printf ("\nFLAGS 1: %8.8lx", abiflags_in.flags1);
17846 printf ("\nFLAGS 2: %8.8lx", abiflags_in.flags2);
17847 fputc ('\n', stdout);
17848 free (abiflags_ext);
17849 }
17850 }
17851 }
17852
17853 /* We have a lot of special sections. Thanks SGI! */
17854 if (filedata->dynamic_section == NULL)
17855 {
17856 /* No dynamic information available. See if there is static GOT. */
17857 sect = find_section (filedata, ".got");
17858 if (sect != NULL)
17859 {
17860 unsigned char *data_end;
17861 unsigned char *data;
17862 bfd_vma ent, end;
17863 int addr_size;
17864
17865 pltgot = sect->sh_addr;
17866
17867 ent = pltgot;
17868 addr_size = (is_32bit_elf ? 4 : 8);
17869 end = pltgot + sect->sh_size;
17870
17871 data = (unsigned char *) get_data (NULL, filedata, sect->sh_offset,
17872 end - pltgot, 1,
17873 _("Global Offset Table data"));
17874 /* PR 12855: Null data is handled gracefully throughout. */
17875 data_end = data + (end - pltgot);
17876
17877 printf (_("\nStatic GOT:\n"));
17878 printf (_(" Canonical gp value: "));
17879 print_vma (ent + 0x7ff0, LONG_HEX);
17880 printf ("\n\n");
17881
17882 /* In a dynamic binary GOT[0] is reserved for the dynamic
17883 loader to store the lazy resolver pointer, however in
17884 a static binary it may well have been omitted and GOT
17885 reduced to a table of addresses.
17886 PR 21344: Check for the entry being fully available
17887 before fetching it. */
17888 if (data
17889 && data + ent - pltgot + addr_size <= data_end
17890 && byte_get (data + ent - pltgot, addr_size) == 0)
17891 {
17892 printf (_(" Reserved entries:\n"));
17893 printf (_(" %*s %10s %*s\n"),
17894 addr_size * 2, _("Address"), _("Access"),
17895 addr_size * 2, _("Value"));
17896 ent = print_mips_got_entry (data, pltgot, ent, data_end);
17897 printf ("\n");
17898 if (ent == (bfd_vma) -1)
17899 goto sgot_print_fail;
17900
17901 /* Check for the MSB of GOT[1] being set, identifying a
17902 GNU object. This entry will be used by some runtime
17903 loaders, to store the module pointer. Otherwise this
17904 is an ordinary local entry.
17905 PR 21344: Check for the entry being fully available
17906 before fetching it. */
17907 if (data
17908 && data + ent - pltgot + addr_size <= data_end
17909 && (byte_get (data + ent - pltgot, addr_size)
17910 >> (addr_size * 8 - 1)) != 0)
17911 {
17912 ent = print_mips_got_entry (data, pltgot, ent, data_end);
17913 printf ("\n");
17914 if (ent == (bfd_vma) -1)
17915 goto sgot_print_fail;
17916 }
17917 printf ("\n");
17918 }
17919
17920 if (data != NULL && ent < end)
17921 {
17922 printf (_(" Local entries:\n"));
17923 printf (" %*s %10s %*s\n",
17924 addr_size * 2, _("Address"), _("Access"),
17925 addr_size * 2, _("Value"));
17926 while (ent < end)
17927 {
17928 ent = print_mips_got_entry (data, pltgot, ent, data_end);
17929 printf ("\n");
17930 if (ent == (bfd_vma) -1)
17931 goto sgot_print_fail;
17932 }
17933 printf ("\n");
17934 }
17935
17936 sgot_print_fail:
17937 free (data);
17938 }
17939 return res;
17940 }
17941
17942 for (entry = filedata->dynamic_section;
17943 /* PR 17531 file: 012-50589-0.004. */
17944 (entry < filedata->dynamic_section + filedata->dynamic_nent
17945 && entry->d_tag != DT_NULL);
17946 ++entry)
17947 switch (entry->d_tag)
17948 {
17949 case DT_MIPS_LIBLIST:
17950 liblist_offset
17951 = offset_from_vma (filedata, entry->d_un.d_val,
17952 liblistno * sizeof (Elf32_External_Lib));
17953 break;
17954 case DT_MIPS_LIBLISTNO:
17955 liblistno = entry->d_un.d_val;
17956 break;
17957 case DT_MIPS_OPTIONS:
17958 options_offset = offset_from_vma (filedata, entry->d_un.d_val, 0);
17959 break;
17960 case DT_MIPS_CONFLICT:
17961 conflicts_offset
17962 = offset_from_vma (filedata, entry->d_un.d_val,
17963 conflictsno * sizeof (Elf32_External_Conflict));
17964 break;
17965 case DT_MIPS_CONFLICTNO:
17966 conflictsno = entry->d_un.d_val;
17967 break;
17968 case DT_PLTGOT:
17969 pltgot = entry->d_un.d_ptr;
17970 break;
17971 case DT_MIPS_LOCAL_GOTNO:
17972 local_gotno = entry->d_un.d_val;
17973 break;
17974 case DT_MIPS_GOTSYM:
17975 gotsym = entry->d_un.d_val;
17976 break;
17977 case DT_MIPS_SYMTABNO:
17978 symtabno = entry->d_un.d_val;
17979 break;
17980 case DT_MIPS_PLTGOT:
17981 mips_pltgot = entry->d_un.d_ptr;
17982 break;
17983 case DT_PLTREL:
17984 pltrel = entry->d_un.d_val;
17985 break;
17986 case DT_PLTRELSZ:
17987 pltrelsz = entry->d_un.d_val;
17988 break;
17989 case DT_JMPREL:
17990 jmprel = entry->d_un.d_ptr;
17991 break;
17992 default:
17993 break;
17994 }
17995
17996 if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
17997 {
17998 Elf32_External_Lib * elib;
17999 size_t cnt;
18000
18001 elib = (Elf32_External_Lib *) get_data (NULL, filedata, liblist_offset,
18002 sizeof (Elf32_External_Lib),
18003 liblistno,
18004 _("liblist section data"));
18005 if (elib)
18006 {
18007 printf (ngettext ("\nSection '.liblist' contains %lu entry:\n",
18008 "\nSection '.liblist' contains %lu entries:\n",
18009 (unsigned long) liblistno),
18010 (unsigned long) liblistno);
18011 fputs (_(" Library Time Stamp Checksum Version Flags\n"),
18012 stdout);
18013
18014 for (cnt = 0; cnt < liblistno; ++cnt)
18015 {
18016 Elf32_Lib liblist;
18017 time_t atime;
18018 char timebuf[128];
18019 struct tm * tmp;
18020
18021 liblist.l_name = BYTE_GET (elib[cnt].l_name);
18022 atime = BYTE_GET (elib[cnt].l_time_stamp);
18023 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
18024 liblist.l_version = BYTE_GET (elib[cnt].l_version);
18025 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
18026
18027 tmp = gmtime (&atime);
18028 snprintf (timebuf, sizeof (timebuf),
18029 "%04u-%02u-%02uT%02u:%02u:%02u",
18030 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
18031 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
18032
18033 printf ("%3lu: ", (unsigned long) cnt);
18034 if (valid_dynamic_name (filedata, liblist.l_name))
18035 print_symbol (20, get_dynamic_name (filedata, liblist.l_name));
18036 else
18037 printf (_("<corrupt: %9ld>"), liblist.l_name);
18038 printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
18039 liblist.l_version);
18040
18041 if (liblist.l_flags == 0)
18042 puts (_(" NONE"));
18043 else
18044 {
18045 static const struct
18046 {
18047 const char * name;
18048 int bit;
18049 }
18050 l_flags_vals[] =
18051 {
18052 { " EXACT_MATCH", LL_EXACT_MATCH },
18053 { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
18054 { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
18055 { " EXPORTS", LL_EXPORTS },
18056 { " DELAY_LOAD", LL_DELAY_LOAD },
18057 { " DELTA", LL_DELTA }
18058 };
18059 int flags = liblist.l_flags;
18060 size_t fcnt;
18061
18062 for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
18063 if ((flags & l_flags_vals[fcnt].bit) != 0)
18064 {
18065 fputs (l_flags_vals[fcnt].name, stdout);
18066 flags ^= l_flags_vals[fcnt].bit;
18067 }
18068 if (flags != 0)
18069 printf (" %#x", (unsigned int) flags);
18070
18071 puts ("");
18072 }
18073 }
18074
18075 free (elib);
18076 }
18077 else
18078 res = false;
18079 }
18080
18081 if (options_offset != 0)
18082 {
18083 Elf_External_Options * eopt;
18084 size_t offset;
18085 int cnt;
18086 sect = filedata->section_headers;
18087
18088 /* Find the section header so that we get the size. */
18089 sect = find_section_by_type (filedata, SHT_MIPS_OPTIONS);
18090 /* PR 17533 file: 012-277276-0.004. */
18091 if (sect == NULL)
18092 {
18093 error (_("No MIPS_OPTIONS header found\n"));
18094 return false;
18095 }
18096 /* PR 24243 */
18097 if (sect->sh_size < sizeof (* eopt))
18098 {
18099 error (_("The MIPS options section is too small.\n"));
18100 return false;
18101 }
18102
18103 eopt = (Elf_External_Options *) get_data (NULL, filedata, options_offset, 1,
18104 sect->sh_size, _("options"));
18105 if (eopt)
18106 {
18107 Elf_Internal_Options option;
18108
18109 offset = cnt = 0;
18110 while (offset <= sect->sh_size - sizeof (* eopt))
18111 {
18112 Elf_External_Options * eoption;
18113 unsigned int optsize;
18114
18115 eoption = (Elf_External_Options *) ((char *) eopt + offset);
18116
18117 optsize = BYTE_GET (eoption->size);
18118
18119 /* PR 17531: file: ffa0fa3b. */
18120 if (optsize < sizeof (* eopt)
18121 || optsize > sect->sh_size - offset)
18122 {
18123 error (_("Invalid size (%u) for MIPS option\n"),
18124 optsize);
18125 free (eopt);
18126 return false;
18127 }
18128 offset += optsize;
18129 ++cnt;
18130 }
18131
18132 printf (ngettext ("\nSection '%s' contains %d entry:\n",
18133 "\nSection '%s' contains %d entries:\n",
18134 cnt),
18135 printable_section_name (filedata, sect), cnt);
18136
18137 offset = 0;
18138 while (cnt-- > 0)
18139 {
18140 size_t len;
18141 Elf_External_Options * eoption;
18142
18143 eoption = (Elf_External_Options *) ((char *) eopt + offset);
18144
18145 option.kind = BYTE_GET (eoption->kind);
18146 option.size = BYTE_GET (eoption->size);
18147 option.section = BYTE_GET (eoption->section);
18148 option.info = BYTE_GET (eoption->info);
18149
18150 switch (option.kind)
18151 {
18152 case ODK_NULL:
18153 /* This shouldn't happen. */
18154 printf (" NULL %" PRId16 " %" PRIx32,
18155 option.section, option.info);
18156 break;
18157
18158 case ODK_REGINFO:
18159 printf (" REGINFO ");
18160 if (filedata->file_header.e_machine == EM_MIPS)
18161 {
18162 Elf32_External_RegInfo * ereg;
18163 Elf32_RegInfo reginfo;
18164
18165 /* 32bit form. */
18166 if (option.size < (sizeof (Elf_External_Options)
18167 + sizeof (Elf32_External_RegInfo)))
18168 {
18169 printf (_("<corrupt>\n"));
18170 error (_("Truncated MIPS REGINFO option\n"));
18171 cnt = 0;
18172 break;
18173 }
18174
18175 ereg = (Elf32_External_RegInfo *) (eoption + 1);
18176
18177 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
18178 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
18179 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
18180 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
18181 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
18182 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
18183
18184 printf ("GPR %08" PRIx32 " GP 0x%" PRIx32 "\n",
18185 reginfo.ri_gprmask, reginfo.ri_gp_value);
18186 printf (" "
18187 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
18188 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
18189 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
18190 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
18191 }
18192 else
18193 {
18194 /* 64 bit form. */
18195 Elf64_External_RegInfo * ereg;
18196 Elf64_Internal_RegInfo reginfo;
18197
18198 if (option.size < (sizeof (Elf_External_Options)
18199 + sizeof (Elf64_External_RegInfo)))
18200 {
18201 printf (_("<corrupt>\n"));
18202 error (_("Truncated MIPS REGINFO option\n"));
18203 cnt = 0;
18204 break;
18205 }
18206
18207 ereg = (Elf64_External_RegInfo *) (eoption + 1);
18208 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
18209 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
18210 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
18211 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
18212 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
18213 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
18214
18215 printf ("GPR %08" PRIx32 " GP 0x%" PRIx64 "\n",
18216 reginfo.ri_gprmask, reginfo.ri_gp_value);
18217 printf (" "
18218 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
18219 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
18220 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
18221 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
18222 }
18223 offset += option.size;
18224 continue;
18225
18226 case ODK_EXCEPTIONS:
18227 fputs (" EXCEPTIONS fpe_min(", stdout);
18228 process_mips_fpe_exception (option.info & OEX_FPU_MIN);
18229 fputs (") fpe_max(", stdout);
18230 process_mips_fpe_exception ((option.info & OEX_FPU_MAX) >> 8);
18231 fputs (")", stdout);
18232
18233 if (option.info & OEX_PAGE0)
18234 fputs (" PAGE0", stdout);
18235 if (option.info & OEX_SMM)
18236 fputs (" SMM", stdout);
18237 if (option.info & OEX_FPDBUG)
18238 fputs (" FPDBUG", stdout);
18239 if (option.info & OEX_DISMISS)
18240 fputs (" DISMISS", stdout);
18241 break;
18242
18243 case ODK_PAD:
18244 fputs (" PAD ", stdout);
18245 if (option.info & OPAD_PREFIX)
18246 fputs (" PREFIX", stdout);
18247 if (option.info & OPAD_POSTFIX)
18248 fputs (" POSTFIX", stdout);
18249 if (option.info & OPAD_SYMBOL)
18250 fputs (" SYMBOL", stdout);
18251 break;
18252
18253 case ODK_HWPATCH:
18254 fputs (" HWPATCH ", stdout);
18255 if (option.info & OHW_R4KEOP)
18256 fputs (" R4KEOP", stdout);
18257 if (option.info & OHW_R8KPFETCH)
18258 fputs (" R8KPFETCH", stdout);
18259 if (option.info & OHW_R5KEOP)
18260 fputs (" R5KEOP", stdout);
18261 if (option.info & OHW_R5KCVTL)
18262 fputs (" R5KCVTL", stdout);
18263 break;
18264
18265 case ODK_FILL:
18266 fputs (" FILL ", stdout);
18267 /* XXX Print content of info word? */
18268 break;
18269
18270 case ODK_TAGS:
18271 fputs (" TAGS ", stdout);
18272 /* XXX Print content of info word? */
18273 break;
18274
18275 case ODK_HWAND:
18276 fputs (" HWAND ", stdout);
18277 if (option.info & OHWA0_R4KEOP_CHECKED)
18278 fputs (" R4KEOP_CHECKED", stdout);
18279 if (option.info & OHWA0_R4KEOP_CLEAN)
18280 fputs (" R4KEOP_CLEAN", stdout);
18281 break;
18282
18283 case ODK_HWOR:
18284 fputs (" HWOR ", stdout);
18285 if (option.info & OHWA0_R4KEOP_CHECKED)
18286 fputs (" R4KEOP_CHECKED", stdout);
18287 if (option.info & OHWA0_R4KEOP_CLEAN)
18288 fputs (" R4KEOP_CLEAN", stdout);
18289 break;
18290
18291 case ODK_GP_GROUP:
18292 printf (" GP_GROUP %#06x self-contained %#06x",
18293 option.info & OGP_GROUP,
18294 (option.info & OGP_SELF) >> 16);
18295 break;
18296
18297 case ODK_IDENT:
18298 printf (" IDENT %#06x self-contained %#06x",
18299 option.info & OGP_GROUP,
18300 (option.info & OGP_SELF) >> 16);
18301 break;
18302
18303 default:
18304 /* This shouldn't happen. */
18305 printf (" %3d ??? %" PRId16 " %" PRIx32,
18306 option.kind, option.section, option.info);
18307 break;
18308 }
18309
18310 len = sizeof (* eopt);
18311 while (len < option.size)
18312 {
18313 unsigned char datum = *((unsigned char *) eoption + len);
18314
18315 if (ISPRINT (datum))
18316 printf ("%c", datum);
18317 else
18318 printf ("\\%03o", datum);
18319 len ++;
18320 }
18321 fputs ("\n", stdout);
18322
18323 offset += option.size;
18324 }
18325 free (eopt);
18326 }
18327 else
18328 res = false;
18329 }
18330
18331 if (conflicts_offset != 0 && conflictsno != 0)
18332 {
18333 Elf32_Conflict * iconf;
18334 size_t cnt;
18335
18336 if (filedata->dynamic_symbols == NULL)
18337 {
18338 error (_("conflict list found without a dynamic symbol table\n"));
18339 return false;
18340 }
18341
18342 /* PR 21345 - print a slightly more helpful error message
18343 if we are sure that the cmalloc will fail. */
18344 if (conflictsno > filedata->file_size / sizeof (* iconf))
18345 {
18346 error (_("Overlarge number of conflicts detected: %lx\n"),
18347 (long) conflictsno);
18348 return false;
18349 }
18350
18351 iconf = (Elf32_Conflict *) cmalloc (conflictsno, sizeof (* iconf));
18352 if (iconf == NULL)
18353 {
18354 error (_("Out of memory allocating space for dynamic conflicts\n"));
18355 return false;
18356 }
18357
18358 if (is_32bit_elf)
18359 {
18360 Elf32_External_Conflict * econf32;
18361
18362 econf32 = (Elf32_External_Conflict *)
18363 get_data (NULL, filedata, conflicts_offset,
18364 sizeof (*econf32), conflictsno, _("conflict"));
18365 if (!econf32)
18366 {
18367 free (iconf);
18368 return false;
18369 }
18370
18371 for (cnt = 0; cnt < conflictsno; ++cnt)
18372 iconf[cnt] = BYTE_GET (econf32[cnt]);
18373
18374 free (econf32);
18375 }
18376 else
18377 {
18378 Elf64_External_Conflict * econf64;
18379
18380 econf64 = (Elf64_External_Conflict *)
18381 get_data (NULL, filedata, conflicts_offset,
18382 sizeof (*econf64), conflictsno, _("conflict"));
18383 if (!econf64)
18384 {
18385 free (iconf);
18386 return false;
18387 }
18388
18389 for (cnt = 0; cnt < conflictsno; ++cnt)
18390 iconf[cnt] = BYTE_GET (econf64[cnt]);
18391
18392 free (econf64);
18393 }
18394
18395 printf (ngettext ("\nSection '.conflict' contains %lu entry:\n",
18396 "\nSection '.conflict' contains %lu entries:\n",
18397 (unsigned long) conflictsno),
18398 (unsigned long) conflictsno);
18399 puts (_(" Num: Index Value Name"));
18400
18401 for (cnt = 0; cnt < conflictsno; ++cnt)
18402 {
18403 printf ("%5lu: %8lu ", (unsigned long) cnt, iconf[cnt]);
18404
18405 if (iconf[cnt] >= filedata->num_dynamic_syms)
18406 printf (_("<corrupt symbol index>"));
18407 else
18408 {
18409 Elf_Internal_Sym * psym;
18410
18411 psym = & filedata->dynamic_symbols[iconf[cnt]];
18412 print_vma (psym->st_value, FULL_HEX);
18413 putchar (' ');
18414 if (valid_dynamic_name (filedata, psym->st_name))
18415 print_symbol (25, get_dynamic_name (filedata, psym->st_name));
18416 else
18417 printf (_("<corrupt: %14ld>"), psym->st_name);
18418 }
18419 putchar ('\n');
18420 }
18421
18422 free (iconf);
18423 }
18424
18425 if (pltgot != 0 && local_gotno != 0)
18426 {
18427 bfd_vma ent, local_end, global_end;
18428 size_t i, offset;
18429 unsigned char * data;
18430 unsigned char * data_end;
18431 int addr_size;
18432
18433 ent = pltgot;
18434 addr_size = (is_32bit_elf ? 4 : 8);
18435 local_end = pltgot + local_gotno * addr_size;
18436
18437 /* PR binutils/17533 file: 012-111227-0.004 */
18438 if (symtabno < gotsym)
18439 {
18440 error (_("The GOT symbol offset (%lu) is greater than the symbol table size (%lu)\n"),
18441 (unsigned long) gotsym, (unsigned long) symtabno);
18442 return false;
18443 }
18444
18445 global_end = local_end + (symtabno - gotsym) * addr_size;
18446 /* PR 17531: file: 54c91a34. */
18447 if (global_end < local_end)
18448 {
18449 error (_("Too many GOT symbols: %lu\n"), (unsigned long) symtabno);
18450 return false;
18451 }
18452
18453 offset = offset_from_vma (filedata, pltgot, global_end - pltgot);
18454 data = (unsigned char *) get_data (NULL, filedata, offset,
18455 global_end - pltgot, 1,
18456 _("Global Offset Table data"));
18457 /* PR 12855: Null data is handled gracefully throughout. */
18458 data_end = data + (global_end - pltgot);
18459
18460 printf (_("\nPrimary GOT:\n"));
18461 printf (_(" Canonical gp value: "));
18462 print_vma (pltgot + 0x7ff0, LONG_HEX);
18463 printf ("\n\n");
18464
18465 printf (_(" Reserved entries:\n"));
18466 printf (_(" %*s %10s %*s Purpose\n"),
18467 addr_size * 2, _("Address"), _("Access"),
18468 addr_size * 2, _("Initial"));
18469 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18470 printf (_(" Lazy resolver\n"));
18471 if (ent == (bfd_vma) -1)
18472 goto got_print_fail;
18473
18474 /* Check for the MSB of GOT[1] being set, denoting a GNU object.
18475 This entry will be used by some runtime loaders, to store the
18476 module pointer. Otherwise this is an ordinary local entry.
18477 PR 21344: Check for the entry being fully available before
18478 fetching it. */
18479 if (data
18480 && data + ent - pltgot + addr_size <= data_end
18481 && (byte_get (data + ent - pltgot, addr_size)
18482 >> (addr_size * 8 - 1)) != 0)
18483 {
18484 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18485 printf (_(" Module pointer (GNU extension)\n"));
18486 if (ent == (bfd_vma) -1)
18487 goto got_print_fail;
18488 }
18489 printf ("\n");
18490
18491 if (data != NULL && ent < local_end)
18492 {
18493 printf (_(" Local entries:\n"));
18494 printf (" %*s %10s %*s\n",
18495 addr_size * 2, _("Address"), _("Access"),
18496 addr_size * 2, _("Initial"));
18497 while (ent < local_end)
18498 {
18499 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18500 printf ("\n");
18501 if (ent == (bfd_vma) -1)
18502 goto got_print_fail;
18503 }
18504 printf ("\n");
18505 }
18506
18507 if (data != NULL && gotsym < symtabno)
18508 {
18509 int sym_width;
18510
18511 printf (_(" Global entries:\n"));
18512 printf (" %*s %10s %*s %*s %-7s %3s %s\n",
18513 addr_size * 2, _("Address"),
18514 _("Access"),
18515 addr_size * 2, _("Initial"),
18516 addr_size * 2, _("Sym.Val."),
18517 _("Type"),
18518 /* Note for translators: "Ndx" = abbreviated form of "Index". */
18519 _("Ndx"), _("Name"));
18520
18521 sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
18522
18523 for (i = gotsym; i < symtabno; i++)
18524 {
18525 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18526 printf (" ");
18527
18528 if (filedata->dynamic_symbols == NULL)
18529 printf (_("<no dynamic symbols>"));
18530 else if (i < filedata->num_dynamic_syms)
18531 {
18532 Elf_Internal_Sym * psym = filedata->dynamic_symbols + i;
18533
18534 print_vma (psym->st_value, LONG_HEX);
18535 printf (" %-7s %3s ",
18536 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
18537 get_symbol_index_type (filedata, psym->st_shndx));
18538
18539 if (valid_dynamic_name (filedata, psym->st_name))
18540 print_symbol (sym_width,
18541 get_dynamic_name (filedata, psym->st_name));
18542 else
18543 printf (_("<corrupt: %14ld>"), psym->st_name);
18544 }
18545 else
18546 printf (_("<symbol index %lu exceeds number of dynamic symbols>"),
18547 (unsigned long) i);
18548
18549 printf ("\n");
18550 if (ent == (bfd_vma) -1)
18551 break;
18552 }
18553 printf ("\n");
18554 }
18555
18556 got_print_fail:
18557 free (data);
18558 }
18559
18560 if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
18561 {
18562 bfd_vma ent, end;
18563 size_t offset, rel_offset;
18564 unsigned long count, i;
18565 unsigned char * data;
18566 int addr_size, sym_width;
18567 Elf_Internal_Rela * rels;
18568
18569 rel_offset = offset_from_vma (filedata, jmprel, pltrelsz);
18570 if (pltrel == DT_RELA)
18571 {
18572 if (!slurp_rela_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
18573 return false;
18574 }
18575 else
18576 {
18577 if (!slurp_rel_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
18578 return false;
18579 }
18580
18581 ent = mips_pltgot;
18582 addr_size = (is_32bit_elf ? 4 : 8);
18583 end = mips_pltgot + (2 + count) * addr_size;
18584
18585 offset = offset_from_vma (filedata, mips_pltgot, end - mips_pltgot);
18586 data = (unsigned char *) get_data (NULL, filedata, offset, end - mips_pltgot,
18587 1, _("Procedure Linkage Table data"));
18588 if (data == NULL)
18589 {
18590 free (rels);
18591 return false;
18592 }
18593
18594 printf ("\nPLT GOT:\n\n");
18595 printf (_(" Reserved entries:\n"));
18596 printf (_(" %*s %*s Purpose\n"),
18597 addr_size * 2, _("Address"), addr_size * 2, _("Initial"));
18598 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
18599 printf (_(" PLT lazy resolver\n"));
18600 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
18601 printf (_(" Module pointer\n"));
18602 printf ("\n");
18603
18604 printf (_(" Entries:\n"));
18605 printf (" %*s %*s %*s %-7s %3s %s\n",
18606 addr_size * 2, _("Address"),
18607 addr_size * 2, _("Initial"),
18608 addr_size * 2, _("Sym.Val."), _("Type"), _("Ndx"), _("Name"));
18609 sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
18610 for (i = 0; i < count; i++)
18611 {
18612 unsigned long idx = get_reloc_symindex (rels[i].r_info);
18613
18614 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
18615 printf (" ");
18616
18617 if (idx >= filedata->num_dynamic_syms)
18618 printf (_("<corrupt symbol index: %lu>"), idx);
18619 else
18620 {
18621 Elf_Internal_Sym * psym = filedata->dynamic_symbols + idx;
18622
18623 print_vma (psym->st_value, LONG_HEX);
18624 printf (" %-7s %3s ",
18625 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
18626 get_symbol_index_type (filedata, psym->st_shndx));
18627 if (valid_dynamic_name (filedata, psym->st_name))
18628 print_symbol (sym_width,
18629 get_dynamic_name (filedata, psym->st_name));
18630 else
18631 printf (_("<corrupt: %14ld>"), psym->st_name);
18632 }
18633 printf ("\n");
18634 }
18635 printf ("\n");
18636
18637 free (data);
18638 free (rels);
18639 }
18640
18641 return res;
18642 }
18643
18644 static bool
18645 process_nds32_specific (Filedata * filedata)
18646 {
18647 Elf_Internal_Shdr *sect = NULL;
18648
18649 sect = find_section (filedata, ".nds32_e_flags");
18650 if (sect != NULL && sect->sh_size >= 4)
18651 {
18652 unsigned char *buf;
18653 unsigned int flag;
18654
18655 printf ("\nNDS32 elf flags section:\n");
18656 buf = get_data (NULL, filedata, sect->sh_offset, 1, 4,
18657 _("NDS32 elf flags section"));
18658
18659 if (buf == NULL)
18660 return false;
18661
18662 flag = byte_get (buf, 4);
18663 free (buf);
18664 switch (flag & 0x3)
18665 {
18666 case 0:
18667 printf ("(VEC_SIZE):\tNo entry.\n");
18668 break;
18669 case 1:
18670 printf ("(VEC_SIZE):\t4 bytes\n");
18671 break;
18672 case 2:
18673 printf ("(VEC_SIZE):\t16 bytes\n");
18674 break;
18675 case 3:
18676 printf ("(VEC_SIZE):\treserved\n");
18677 break;
18678 }
18679 }
18680
18681 return true;
18682 }
18683
18684 static bool
18685 process_gnu_liblist (Filedata * filedata)
18686 {
18687 Elf_Internal_Shdr * section;
18688 Elf_Internal_Shdr * string_sec;
18689 Elf32_External_Lib * elib;
18690 char * strtab;
18691 size_t strtab_size;
18692 size_t cnt;
18693 unsigned long num_liblist;
18694 unsigned i;
18695 bool res = true;
18696
18697 if (! do_arch)
18698 return true;
18699
18700 for (i = 0, section = filedata->section_headers;
18701 i < filedata->file_header.e_shnum;
18702 i++, section++)
18703 {
18704 switch (section->sh_type)
18705 {
18706 case SHT_GNU_LIBLIST:
18707 if (section->sh_link >= filedata->file_header.e_shnum)
18708 break;
18709
18710 elib = (Elf32_External_Lib *)
18711 get_data (NULL, filedata, section->sh_offset, 1, section->sh_size,
18712 _("liblist section data"));
18713
18714 if (elib == NULL)
18715 {
18716 res = false;
18717 break;
18718 }
18719
18720 string_sec = filedata->section_headers + section->sh_link;
18721 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
18722 string_sec->sh_size,
18723 _("liblist string table"));
18724 if (strtab == NULL
18725 || section->sh_entsize != sizeof (Elf32_External_Lib))
18726 {
18727 free (elib);
18728 free (strtab);
18729 res = false;
18730 break;
18731 }
18732 strtab_size = string_sec->sh_size;
18733
18734 num_liblist = section->sh_size / sizeof (Elf32_External_Lib);
18735 printf (ngettext ("\nLibrary list section '%s' contains %lu entries:\n",
18736 "\nLibrary list section '%s' contains %lu entries:\n",
18737 num_liblist),
18738 printable_section_name (filedata, section),
18739 num_liblist);
18740
18741 puts (_(" Library Time Stamp Checksum Version Flags"));
18742
18743 for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
18744 ++cnt)
18745 {
18746 Elf32_Lib liblist;
18747 time_t atime;
18748 char timebuf[128];
18749 struct tm * tmp;
18750
18751 liblist.l_name = BYTE_GET (elib[cnt].l_name);
18752 atime = BYTE_GET (elib[cnt].l_time_stamp);
18753 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
18754 liblist.l_version = BYTE_GET (elib[cnt].l_version);
18755 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
18756
18757 tmp = gmtime (&atime);
18758 snprintf (timebuf, sizeof (timebuf),
18759 "%04u-%02u-%02uT%02u:%02u:%02u",
18760 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
18761 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
18762
18763 printf ("%3lu: ", (unsigned long) cnt);
18764 if (do_wide)
18765 printf ("%-20s", liblist.l_name < strtab_size
18766 ? strtab + liblist.l_name : _("<corrupt>"));
18767 else
18768 printf ("%-20.20s", liblist.l_name < strtab_size
18769 ? strtab + liblist.l_name : _("<corrupt>"));
18770 printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
18771 liblist.l_version, liblist.l_flags);
18772 }
18773
18774 free (elib);
18775 free (strtab);
18776 }
18777 }
18778
18779 return res;
18780 }
18781
18782 static const char *
18783 get_note_type (Filedata * filedata, unsigned e_type)
18784 {
18785 static char buff[64];
18786
18787 if (filedata->file_header.e_type == ET_CORE)
18788 switch (e_type)
18789 {
18790 case NT_AUXV:
18791 return _("NT_AUXV (auxiliary vector)");
18792 case NT_PRSTATUS:
18793 return _("NT_PRSTATUS (prstatus structure)");
18794 case NT_FPREGSET:
18795 return _("NT_FPREGSET (floating point registers)");
18796 case NT_PRPSINFO:
18797 return _("NT_PRPSINFO (prpsinfo structure)");
18798 case NT_TASKSTRUCT:
18799 return _("NT_TASKSTRUCT (task structure)");
18800 case NT_GDB_TDESC:
18801 return _("NT_GDB_TDESC (GDB XML target description)");
18802 case NT_PRXFPREG:
18803 return _("NT_PRXFPREG (user_xfpregs structure)");
18804 case NT_PPC_VMX:
18805 return _("NT_PPC_VMX (ppc Altivec registers)");
18806 case NT_PPC_VSX:
18807 return _("NT_PPC_VSX (ppc VSX registers)");
18808 case NT_PPC_TAR:
18809 return _("NT_PPC_TAR (ppc TAR register)");
18810 case NT_PPC_PPR:
18811 return _("NT_PPC_PPR (ppc PPR register)");
18812 case NT_PPC_DSCR:
18813 return _("NT_PPC_DSCR (ppc DSCR register)");
18814 case NT_PPC_EBB:
18815 return _("NT_PPC_EBB (ppc EBB registers)");
18816 case NT_PPC_PMU:
18817 return _("NT_PPC_PMU (ppc PMU registers)");
18818 case NT_PPC_TM_CGPR:
18819 return _("NT_PPC_TM_CGPR (ppc checkpointed GPR registers)");
18820 case NT_PPC_TM_CFPR:
18821 return _("NT_PPC_TM_CFPR (ppc checkpointed floating point registers)");
18822 case NT_PPC_TM_CVMX:
18823 return _("NT_PPC_TM_CVMX (ppc checkpointed Altivec registers)");
18824 case NT_PPC_TM_CVSX:
18825 return _("NT_PPC_TM_CVSX (ppc checkpointed VSX registers)");
18826 case NT_PPC_TM_SPR:
18827 return _("NT_PPC_TM_SPR (ppc TM special purpose registers)");
18828 case NT_PPC_TM_CTAR:
18829 return _("NT_PPC_TM_CTAR (ppc checkpointed TAR register)");
18830 case NT_PPC_TM_CPPR:
18831 return _("NT_PPC_TM_CPPR (ppc checkpointed PPR register)");
18832 case NT_PPC_TM_CDSCR:
18833 return _("NT_PPC_TM_CDSCR (ppc checkpointed DSCR register)");
18834 case NT_386_TLS:
18835 return _("NT_386_TLS (x86 TLS information)");
18836 case NT_386_IOPERM:
18837 return _("NT_386_IOPERM (x86 I/O permissions)");
18838 case NT_X86_XSTATE:
18839 return _("NT_X86_XSTATE (x86 XSAVE extended state)");
18840 case NT_X86_CET:
18841 return _("NT_X86_CET (x86 CET state)");
18842 case NT_S390_HIGH_GPRS:
18843 return _("NT_S390_HIGH_GPRS (s390 upper register halves)");
18844 case NT_S390_TIMER:
18845 return _("NT_S390_TIMER (s390 timer register)");
18846 case NT_S390_TODCMP:
18847 return _("NT_S390_TODCMP (s390 TOD comparator register)");
18848 case NT_S390_TODPREG:
18849 return _("NT_S390_TODPREG (s390 TOD programmable register)");
18850 case NT_S390_CTRS:
18851 return _("NT_S390_CTRS (s390 control registers)");
18852 case NT_S390_PREFIX:
18853 return _("NT_S390_PREFIX (s390 prefix register)");
18854 case NT_S390_LAST_BREAK:
18855 return _("NT_S390_LAST_BREAK (s390 last breaking event address)");
18856 case NT_S390_SYSTEM_CALL:
18857 return _("NT_S390_SYSTEM_CALL (s390 system call restart data)");
18858 case NT_S390_TDB:
18859 return _("NT_S390_TDB (s390 transaction diagnostic block)");
18860 case NT_S390_VXRS_LOW:
18861 return _("NT_S390_VXRS_LOW (s390 vector registers 0-15 upper half)");
18862 case NT_S390_VXRS_HIGH:
18863 return _("NT_S390_VXRS_HIGH (s390 vector registers 16-31)");
18864 case NT_S390_GS_CB:
18865 return _("NT_S390_GS_CB (s390 guarded-storage registers)");
18866 case NT_S390_GS_BC:
18867 return _("NT_S390_GS_BC (s390 guarded-storage broadcast control)");
18868 case NT_ARM_VFP:
18869 return _("NT_ARM_VFP (arm VFP registers)");
18870 case NT_ARM_TLS:
18871 return _("NT_ARM_TLS (AArch TLS registers)");
18872 case NT_ARM_HW_BREAK:
18873 return _("NT_ARM_HW_BREAK (AArch hardware breakpoint registers)");
18874 case NT_ARM_HW_WATCH:
18875 return _("NT_ARM_HW_WATCH (AArch hardware watchpoint registers)");
18876 case NT_ARM_SVE:
18877 return _("NT_ARM_SVE (AArch SVE registers)");
18878 case NT_ARM_PAC_MASK:
18879 return _("NT_ARM_PAC_MASK (AArch pointer authentication code masks)");
18880 case NT_ARM_PACA_KEYS:
18881 return _("NT_ARM_PACA_KEYS (ARM pointer authentication address keys)");
18882 case NT_ARM_PACG_KEYS:
18883 return _("NT_ARM_PACG_KEYS (ARM pointer authentication generic keys)");
18884 case NT_ARM_TAGGED_ADDR_CTRL:
18885 return _("NT_ARM_TAGGED_ADDR_CTRL (AArch tagged address control)");
18886 case NT_ARM_PAC_ENABLED_KEYS:
18887 return _("NT_ARM_PAC_ENABLED_KEYS (AArch64 pointer authentication enabled keys)");
18888 case NT_ARC_V2:
18889 return _("NT_ARC_V2 (ARC HS accumulator/extra registers)");
18890 case NT_RISCV_CSR:
18891 return _("NT_RISCV_CSR (RISC-V control and status registers)");
18892 case NT_PSTATUS:
18893 return _("NT_PSTATUS (pstatus structure)");
18894 case NT_FPREGS:
18895 return _("NT_FPREGS (floating point registers)");
18896 case NT_PSINFO:
18897 return _("NT_PSINFO (psinfo structure)");
18898 case NT_LWPSTATUS:
18899 return _("NT_LWPSTATUS (lwpstatus_t structure)");
18900 case NT_LWPSINFO:
18901 return _("NT_LWPSINFO (lwpsinfo_t structure)");
18902 case NT_WIN32PSTATUS:
18903 return _("NT_WIN32PSTATUS (win32_pstatus structure)");
18904 case NT_SIGINFO:
18905 return _("NT_SIGINFO (siginfo_t data)");
18906 case NT_FILE:
18907 return _("NT_FILE (mapped files)");
18908 default:
18909 break;
18910 }
18911 else
18912 switch (e_type)
18913 {
18914 case NT_VERSION:
18915 return _("NT_VERSION (version)");
18916 case NT_ARCH:
18917 return _("NT_ARCH (architecture)");
18918 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
18919 return _("OPEN");
18920 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
18921 return _("func");
18922 case NT_GO_BUILDID:
18923 return _("GO BUILDID");
18924 default:
18925 break;
18926 }
18927
18928 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
18929 return buff;
18930 }
18931
18932 static bool
18933 print_core_note (Elf_Internal_Note *pnote)
18934 {
18935 unsigned int addr_size = is_32bit_elf ? 4 : 8;
18936 bfd_vma count, page_size;
18937 unsigned char *descdata, *filenames, *descend;
18938
18939 if (pnote->type != NT_FILE)
18940 {
18941 if (do_wide)
18942 printf ("\n");
18943 return true;
18944 }
18945
18946 #ifndef BFD64
18947 if (!is_32bit_elf)
18948 {
18949 printf (_(" Cannot decode 64-bit note in 32-bit build\n"));
18950 /* Still "successful". */
18951 return true;
18952 }
18953 #endif
18954
18955 if (pnote->descsz < 2 * addr_size)
18956 {
18957 error (_(" Malformed note - too short for header\n"));
18958 return false;
18959 }
18960
18961 descdata = (unsigned char *) pnote->descdata;
18962 descend = descdata + pnote->descsz;
18963
18964 if (descdata[pnote->descsz - 1] != '\0')
18965 {
18966 error (_(" Malformed note - does not end with \\0\n"));
18967 return false;
18968 }
18969
18970 count = byte_get (descdata, addr_size);
18971 descdata += addr_size;
18972
18973 page_size = byte_get (descdata, addr_size);
18974 descdata += addr_size;
18975
18976 if (count > ((bfd_vma) -1 - 2 * addr_size) / (3 * addr_size)
18977 || pnote->descsz < 2 * addr_size + count * 3 * addr_size)
18978 {
18979 error (_(" Malformed note - too short for supplied file count\n"));
18980 return false;
18981 }
18982
18983 printf (_(" Page size: "));
18984 print_vma (page_size, DEC);
18985 printf ("\n");
18986
18987 printf (_(" %*s%*s%*s\n"),
18988 (int) (2 + 2 * addr_size), _("Start"),
18989 (int) (4 + 2 * addr_size), _("End"),
18990 (int) (4 + 2 * addr_size), _("Page Offset"));
18991 filenames = descdata + count * 3 * addr_size;
18992 while (count-- > 0)
18993 {
18994 bfd_vma start, end, file_ofs;
18995
18996 if (filenames == descend)
18997 {
18998 error (_(" Malformed note - filenames end too early\n"));
18999 return false;
19000 }
19001
19002 start = byte_get (descdata, addr_size);
19003 descdata += addr_size;
19004 end = byte_get (descdata, addr_size);
19005 descdata += addr_size;
19006 file_ofs = byte_get (descdata, addr_size);
19007 descdata += addr_size;
19008
19009 printf (" ");
19010 print_vma (start, FULL_HEX);
19011 printf (" ");
19012 print_vma (end, FULL_HEX);
19013 printf (" ");
19014 print_vma (file_ofs, FULL_HEX);
19015 printf ("\n %s\n", filenames);
19016
19017 filenames += 1 + strlen ((char *) filenames);
19018 }
19019
19020 return true;
19021 }
19022
19023 static const char *
19024 get_gnu_elf_note_type (unsigned e_type)
19025 {
19026 /* NB/ Keep this switch statement in sync with print_gnu_note (). */
19027 switch (e_type)
19028 {
19029 case NT_GNU_ABI_TAG:
19030 return _("NT_GNU_ABI_TAG (ABI version tag)");
19031 case NT_GNU_HWCAP:
19032 return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
19033 case NT_GNU_BUILD_ID:
19034 return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
19035 case NT_GNU_GOLD_VERSION:
19036 return _("NT_GNU_GOLD_VERSION (gold version)");
19037 case NT_GNU_PROPERTY_TYPE_0:
19038 return _("NT_GNU_PROPERTY_TYPE_0");
19039 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
19040 return _("NT_GNU_BUILD_ATTRIBUTE_OPEN");
19041 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
19042 return _("NT_GNU_BUILD_ATTRIBUTE_FUNC");
19043 default:
19044 {
19045 static char buff[64];
19046
19047 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19048 return buff;
19049 }
19050 }
19051 }
19052
19053 static void
19054 decode_x86_compat_isa (unsigned int bitmask)
19055 {
19056 while (bitmask)
19057 {
19058 unsigned int bit = bitmask & (- bitmask);
19059
19060 bitmask &= ~ bit;
19061 switch (bit)
19062 {
19063 case GNU_PROPERTY_X86_COMPAT_ISA_1_486:
19064 printf ("i486");
19065 break;
19066 case GNU_PROPERTY_X86_COMPAT_ISA_1_586:
19067 printf ("586");
19068 break;
19069 case GNU_PROPERTY_X86_COMPAT_ISA_1_686:
19070 printf ("686");
19071 break;
19072 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE:
19073 printf ("SSE");
19074 break;
19075 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE2:
19076 printf ("SSE2");
19077 break;
19078 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE3:
19079 printf ("SSE3");
19080 break;
19081 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSSE3:
19082 printf ("SSSE3");
19083 break;
19084 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_1:
19085 printf ("SSE4_1");
19086 break;
19087 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_2:
19088 printf ("SSE4_2");
19089 break;
19090 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX:
19091 printf ("AVX");
19092 break;
19093 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX2:
19094 printf ("AVX2");
19095 break;
19096 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512F:
19097 printf ("AVX512F");
19098 break;
19099 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512CD:
19100 printf ("AVX512CD");
19101 break;
19102 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512ER:
19103 printf ("AVX512ER");
19104 break;
19105 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512PF:
19106 printf ("AVX512PF");
19107 break;
19108 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512VL:
19109 printf ("AVX512VL");
19110 break;
19111 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512DQ:
19112 printf ("AVX512DQ");
19113 break;
19114 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512BW:
19115 printf ("AVX512BW");
19116 break;
19117 default:
19118 printf (_("<unknown: %x>"), bit);
19119 break;
19120 }
19121 if (bitmask)
19122 printf (", ");
19123 }
19124 }
19125
19126 static void
19127 decode_x86_compat_2_isa (unsigned int bitmask)
19128 {
19129 if (!bitmask)
19130 {
19131 printf (_("<None>"));
19132 return;
19133 }
19134
19135 while (bitmask)
19136 {
19137 unsigned int bit = bitmask & (- bitmask);
19138
19139 bitmask &= ~ bit;
19140 switch (bit)
19141 {
19142 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_CMOV:
19143 printf ("CMOV");
19144 break;
19145 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE:
19146 printf ("SSE");
19147 break;
19148 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE2:
19149 printf ("SSE2");
19150 break;
19151 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE3:
19152 printf ("SSE3");
19153 break;
19154 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSSE3:
19155 printf ("SSSE3");
19156 break;
19157 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_1:
19158 printf ("SSE4_1");
19159 break;
19160 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_2:
19161 printf ("SSE4_2");
19162 break;
19163 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX:
19164 printf ("AVX");
19165 break;
19166 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX2:
19167 printf ("AVX2");
19168 break;
19169 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_FMA:
19170 printf ("FMA");
19171 break;
19172 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512F:
19173 printf ("AVX512F");
19174 break;
19175 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512CD:
19176 printf ("AVX512CD");
19177 break;
19178 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512ER:
19179 printf ("AVX512ER");
19180 break;
19181 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512PF:
19182 printf ("AVX512PF");
19183 break;
19184 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512VL:
19185 printf ("AVX512VL");
19186 break;
19187 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512DQ:
19188 printf ("AVX512DQ");
19189 break;
19190 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512BW:
19191 printf ("AVX512BW");
19192 break;
19193 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4FMAPS:
19194 printf ("AVX512_4FMAPS");
19195 break;
19196 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4VNNIW:
19197 printf ("AVX512_4VNNIW");
19198 break;
19199 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BITALG:
19200 printf ("AVX512_BITALG");
19201 break;
19202 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_IFMA:
19203 printf ("AVX512_IFMA");
19204 break;
19205 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI:
19206 printf ("AVX512_VBMI");
19207 break;
19208 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI2:
19209 printf ("AVX512_VBMI2");
19210 break;
19211 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VNNI:
19212 printf ("AVX512_VNNI");
19213 break;
19214 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BF16:
19215 printf ("AVX512_BF16");
19216 break;
19217 default:
19218 printf (_("<unknown: %x>"), bit);
19219 break;
19220 }
19221 if (bitmask)
19222 printf (", ");
19223 }
19224 }
19225
19226 static void
19227 decode_x86_isa (unsigned int bitmask)
19228 {
19229 while (bitmask)
19230 {
19231 unsigned int bit = bitmask & (- bitmask);
19232
19233 bitmask &= ~ bit;
19234 switch (bit)
19235 {
19236 case GNU_PROPERTY_X86_ISA_1_BASELINE:
19237 printf ("x86-64-baseline");
19238 break;
19239 case GNU_PROPERTY_X86_ISA_1_V2:
19240 printf ("x86-64-v2");
19241 break;
19242 case GNU_PROPERTY_X86_ISA_1_V3:
19243 printf ("x86-64-v3");
19244 break;
19245 case GNU_PROPERTY_X86_ISA_1_V4:
19246 printf ("x86-64-v4");
19247 break;
19248 default:
19249 printf (_("<unknown: %x>"), bit);
19250 break;
19251 }
19252 if (bitmask)
19253 printf (", ");
19254 }
19255 }
19256
19257 static void
19258 decode_x86_feature_1 (unsigned int bitmask)
19259 {
19260 if (!bitmask)
19261 {
19262 printf (_("<None>"));
19263 return;
19264 }
19265
19266 while (bitmask)
19267 {
19268 unsigned int bit = bitmask & (- bitmask);
19269
19270 bitmask &= ~ bit;
19271 switch (bit)
19272 {
19273 case GNU_PROPERTY_X86_FEATURE_1_IBT:
19274 printf ("IBT");
19275 break;
19276 case GNU_PROPERTY_X86_FEATURE_1_SHSTK:
19277 printf ("SHSTK");
19278 break;
19279 case GNU_PROPERTY_X86_FEATURE_1_LAM_U48:
19280 printf ("LAM_U48");
19281 break;
19282 case GNU_PROPERTY_X86_FEATURE_1_LAM_U57:
19283 printf ("LAM_U57");
19284 break;
19285 default:
19286 printf (_("<unknown: %x>"), bit);
19287 break;
19288 }
19289 if (bitmask)
19290 printf (", ");
19291 }
19292 }
19293
19294 static void
19295 decode_x86_feature_2 (unsigned int bitmask)
19296 {
19297 if (!bitmask)
19298 {
19299 printf (_("<None>"));
19300 return;
19301 }
19302
19303 while (bitmask)
19304 {
19305 unsigned int bit = bitmask & (- bitmask);
19306
19307 bitmask &= ~ bit;
19308 switch (bit)
19309 {
19310 case GNU_PROPERTY_X86_FEATURE_2_X86:
19311 printf ("x86");
19312 break;
19313 case GNU_PROPERTY_X86_FEATURE_2_X87:
19314 printf ("x87");
19315 break;
19316 case GNU_PROPERTY_X86_FEATURE_2_MMX:
19317 printf ("MMX");
19318 break;
19319 case GNU_PROPERTY_X86_FEATURE_2_XMM:
19320 printf ("XMM");
19321 break;
19322 case GNU_PROPERTY_X86_FEATURE_2_YMM:
19323 printf ("YMM");
19324 break;
19325 case GNU_PROPERTY_X86_FEATURE_2_ZMM:
19326 printf ("ZMM");
19327 break;
19328 case GNU_PROPERTY_X86_FEATURE_2_TMM:
19329 printf ("TMM");
19330 break;
19331 case GNU_PROPERTY_X86_FEATURE_2_MASK:
19332 printf ("MASK");
19333 break;
19334 case GNU_PROPERTY_X86_FEATURE_2_FXSR:
19335 printf ("FXSR");
19336 break;
19337 case GNU_PROPERTY_X86_FEATURE_2_XSAVE:
19338 printf ("XSAVE");
19339 break;
19340 case GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT:
19341 printf ("XSAVEOPT");
19342 break;
19343 case GNU_PROPERTY_X86_FEATURE_2_XSAVEC:
19344 printf ("XSAVEC");
19345 break;
19346 default:
19347 printf (_("<unknown: %x>"), bit);
19348 break;
19349 }
19350 if (bitmask)
19351 printf (", ");
19352 }
19353 }
19354
19355 static void
19356 decode_aarch64_feature_1_and (unsigned int bitmask)
19357 {
19358 while (bitmask)
19359 {
19360 unsigned int bit = bitmask & (- bitmask);
19361
19362 bitmask &= ~ bit;
19363 switch (bit)
19364 {
19365 case GNU_PROPERTY_AARCH64_FEATURE_1_BTI:
19366 printf ("BTI");
19367 break;
19368
19369 case GNU_PROPERTY_AARCH64_FEATURE_1_PAC:
19370 printf ("PAC");
19371 break;
19372
19373 default:
19374 printf (_("<unknown: %x>"), bit);
19375 break;
19376 }
19377 if (bitmask)
19378 printf (", ");
19379 }
19380 }
19381
19382 static void
19383 decode_1_needed (unsigned int bitmask)
19384 {
19385 while (bitmask)
19386 {
19387 unsigned int bit = bitmask & (- bitmask);
19388
19389 bitmask &= ~ bit;
19390 switch (bit)
19391 {
19392 case GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS:
19393 printf ("indirect external access");
19394 break;
19395 default:
19396 printf (_("<unknown: %x>"), bit);
19397 break;
19398 }
19399 if (bitmask)
19400 printf (", ");
19401 }
19402 }
19403
19404 static void
19405 print_gnu_property_note (Filedata * filedata, Elf_Internal_Note * pnote)
19406 {
19407 unsigned char * ptr = (unsigned char *) pnote->descdata;
19408 unsigned char * ptr_end = ptr + pnote->descsz;
19409 unsigned int size = is_32bit_elf ? 4 : 8;
19410
19411 printf (_(" Properties: "));
19412
19413 if (pnote->descsz < 8 || (pnote->descsz % size) != 0)
19414 {
19415 printf (_("<corrupt GNU_PROPERTY_TYPE, size = %#lx>\n"), pnote->descsz);
19416 return;
19417 }
19418
19419 while (ptr < ptr_end)
19420 {
19421 unsigned int j;
19422 unsigned int type;
19423 unsigned int datasz;
19424
19425 if ((size_t) (ptr_end - ptr) < 8)
19426 {
19427 printf (_("<corrupt descsz: %#lx>\n"), pnote->descsz);
19428 break;
19429 }
19430
19431 type = byte_get (ptr, 4);
19432 datasz = byte_get (ptr + 4, 4);
19433
19434 ptr += 8;
19435
19436 if (datasz > (size_t) (ptr_end - ptr))
19437 {
19438 printf (_("<corrupt type (%#x) datasz: %#x>\n"),
19439 type, datasz);
19440 break;
19441 }
19442
19443 if (type >= GNU_PROPERTY_LOPROC && type <= GNU_PROPERTY_HIPROC)
19444 {
19445 if (filedata->file_header.e_machine == EM_X86_64
19446 || filedata->file_header.e_machine == EM_IAMCU
19447 || filedata->file_header.e_machine == EM_386)
19448 {
19449 unsigned int bitmask;
19450
19451 if (datasz == 4)
19452 bitmask = byte_get (ptr, 4);
19453 else
19454 bitmask = 0;
19455
19456 switch (type)
19457 {
19458 case GNU_PROPERTY_X86_ISA_1_USED:
19459 if (datasz != 4)
19460 printf (_("x86 ISA used: <corrupt length: %#x> "),
19461 datasz);
19462 else
19463 {
19464 printf ("x86 ISA used: ");
19465 decode_x86_isa (bitmask);
19466 }
19467 goto next;
19468
19469 case GNU_PROPERTY_X86_ISA_1_NEEDED:
19470 if (datasz != 4)
19471 printf (_("x86 ISA needed: <corrupt length: %#x> "),
19472 datasz);
19473 else
19474 {
19475 printf ("x86 ISA needed: ");
19476 decode_x86_isa (bitmask);
19477 }
19478 goto next;
19479
19480 case GNU_PROPERTY_X86_FEATURE_1_AND:
19481 if (datasz != 4)
19482 printf (_("x86 feature: <corrupt length: %#x> "),
19483 datasz);
19484 else
19485 {
19486 printf ("x86 feature: ");
19487 decode_x86_feature_1 (bitmask);
19488 }
19489 goto next;
19490
19491 case GNU_PROPERTY_X86_FEATURE_2_USED:
19492 if (datasz != 4)
19493 printf (_("x86 feature used: <corrupt length: %#x> "),
19494 datasz);
19495 else
19496 {
19497 printf ("x86 feature used: ");
19498 decode_x86_feature_2 (bitmask);
19499 }
19500 goto next;
19501
19502 case GNU_PROPERTY_X86_FEATURE_2_NEEDED:
19503 if (datasz != 4)
19504 printf (_("x86 feature needed: <corrupt length: %#x> "), datasz);
19505 else
19506 {
19507 printf ("x86 feature needed: ");
19508 decode_x86_feature_2 (bitmask);
19509 }
19510 goto next;
19511
19512 case GNU_PROPERTY_X86_COMPAT_ISA_1_USED:
19513 if (datasz != 4)
19514 printf (_("x86 ISA used: <corrupt length: %#x> "),
19515 datasz);
19516 else
19517 {
19518 printf ("x86 ISA used: ");
19519 decode_x86_compat_isa (bitmask);
19520 }
19521 goto next;
19522
19523 case GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED:
19524 if (datasz != 4)
19525 printf (_("x86 ISA needed: <corrupt length: %#x> "),
19526 datasz);
19527 else
19528 {
19529 printf ("x86 ISA needed: ");
19530 decode_x86_compat_isa (bitmask);
19531 }
19532 goto next;
19533
19534 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_USED:
19535 if (datasz != 4)
19536 printf (_("x86 ISA used: <corrupt length: %#x> "),
19537 datasz);
19538 else
19539 {
19540 printf ("x86 ISA used: ");
19541 decode_x86_compat_2_isa (bitmask);
19542 }
19543 goto next;
19544
19545 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_NEEDED:
19546 if (datasz != 4)
19547 printf (_("x86 ISA needed: <corrupt length: %#x> "),
19548 datasz);
19549 else
19550 {
19551 printf ("x86 ISA needed: ");
19552 decode_x86_compat_2_isa (bitmask);
19553 }
19554 goto next;
19555
19556 default:
19557 break;
19558 }
19559 }
19560 else if (filedata->file_header.e_machine == EM_AARCH64)
19561 {
19562 if (type == GNU_PROPERTY_AARCH64_FEATURE_1_AND)
19563 {
19564 printf ("AArch64 feature: ");
19565 if (datasz != 4)
19566 printf (_("<corrupt length: %#x> "), datasz);
19567 else
19568 decode_aarch64_feature_1_and (byte_get (ptr, 4));
19569 goto next;
19570 }
19571 }
19572 }
19573 else
19574 {
19575 switch (type)
19576 {
19577 case GNU_PROPERTY_STACK_SIZE:
19578 printf (_("stack size: "));
19579 if (datasz != size)
19580 printf (_("<corrupt length: %#x> "), datasz);
19581 else
19582 printf ("%#lx", (unsigned long) byte_get (ptr, size));
19583 goto next;
19584
19585 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
19586 printf ("no copy on protected ");
19587 if (datasz)
19588 printf (_("<corrupt length: %#x> "), datasz);
19589 goto next;
19590
19591 default:
19592 if ((type >= GNU_PROPERTY_UINT32_AND_LO
19593 && type <= GNU_PROPERTY_UINT32_AND_HI)
19594 || (type >= GNU_PROPERTY_UINT32_OR_LO
19595 && type <= GNU_PROPERTY_UINT32_OR_HI))
19596 {
19597 switch (type)
19598 {
19599 case GNU_PROPERTY_1_NEEDED:
19600 if (datasz != 4)
19601 printf (_("1_needed: <corrupt length: %#x> "),
19602 datasz);
19603 else
19604 {
19605 unsigned int bitmask = byte_get (ptr, 4);
19606 printf ("1_needed: ");
19607 decode_1_needed (bitmask);
19608 }
19609 goto next;
19610
19611 default:
19612 break;
19613 }
19614 if (type <= GNU_PROPERTY_UINT32_AND_HI)
19615 printf (_("UINT32_AND (%#x): "), type);
19616 else
19617 printf (_("UINT32_OR (%#x): "), type);
19618 if (datasz != 4)
19619 printf (_("<corrupt length: %#x> "), datasz);
19620 else
19621 printf ("%#x", (unsigned int) byte_get (ptr, 4));
19622 goto next;
19623 }
19624 break;
19625 }
19626 }
19627
19628 if (type < GNU_PROPERTY_LOPROC)
19629 printf (_("<unknown type %#x data: "), type);
19630 else if (type < GNU_PROPERTY_LOUSER)
19631 printf (_("<processor-specific type %#x data: "), type);
19632 else
19633 printf (_("<application-specific type %#x data: "), type);
19634 for (j = 0; j < datasz; ++j)
19635 printf ("%02x ", ptr[j] & 0xff);
19636 printf (">");
19637
19638 next:
19639 ptr += ((datasz + (size - 1)) & ~ (size - 1));
19640 if (ptr == ptr_end)
19641 break;
19642
19643 if (do_wide)
19644 printf (", ");
19645 else
19646 printf ("\n\t");
19647 }
19648
19649 printf ("\n");
19650 }
19651
19652 static bool
19653 print_gnu_note (Filedata * filedata, Elf_Internal_Note *pnote)
19654 {
19655 /* NB/ Keep this switch statement in sync with get_gnu_elf_note_type (). */
19656 switch (pnote->type)
19657 {
19658 case NT_GNU_BUILD_ID:
19659 {
19660 unsigned long i;
19661
19662 printf (_(" Build ID: "));
19663 for (i = 0; i < pnote->descsz; ++i)
19664 printf ("%02x", pnote->descdata[i] & 0xff);
19665 printf ("\n");
19666 }
19667 break;
19668
19669 case NT_GNU_ABI_TAG:
19670 {
19671 unsigned long os, major, minor, subminor;
19672 const char *osname;
19673
19674 /* PR 17531: file: 030-599401-0.004. */
19675 if (pnote->descsz < 16)
19676 {
19677 printf (_(" <corrupt GNU_ABI_TAG>\n"));
19678 break;
19679 }
19680
19681 os = byte_get ((unsigned char *) pnote->descdata, 4);
19682 major = byte_get ((unsigned char *) pnote->descdata + 4, 4);
19683 minor = byte_get ((unsigned char *) pnote->descdata + 8, 4);
19684 subminor = byte_get ((unsigned char *) pnote->descdata + 12, 4);
19685
19686 switch (os)
19687 {
19688 case GNU_ABI_TAG_LINUX:
19689 osname = "Linux";
19690 break;
19691 case GNU_ABI_TAG_HURD:
19692 osname = "Hurd";
19693 break;
19694 case GNU_ABI_TAG_SOLARIS:
19695 osname = "Solaris";
19696 break;
19697 case GNU_ABI_TAG_FREEBSD:
19698 osname = "FreeBSD";
19699 break;
19700 case GNU_ABI_TAG_NETBSD:
19701 osname = "NetBSD";
19702 break;
19703 case GNU_ABI_TAG_SYLLABLE:
19704 osname = "Syllable";
19705 break;
19706 case GNU_ABI_TAG_NACL:
19707 osname = "NaCl";
19708 break;
19709 default:
19710 osname = "Unknown";
19711 break;
19712 }
19713
19714 printf (_(" OS: %s, ABI: %ld.%ld.%ld\n"), osname,
19715 major, minor, subminor);
19716 }
19717 break;
19718
19719 case NT_GNU_GOLD_VERSION:
19720 {
19721 unsigned long i;
19722
19723 printf (_(" Version: "));
19724 for (i = 0; i < pnote->descsz && pnote->descdata[i] != '\0'; ++i)
19725 printf ("%c", pnote->descdata[i]);
19726 printf ("\n");
19727 }
19728 break;
19729
19730 case NT_GNU_HWCAP:
19731 {
19732 unsigned long num_entries, mask;
19733
19734 /* Hardware capabilities information. Word 0 is the number of entries.
19735 Word 1 is a bitmask of enabled entries. The rest of the descriptor
19736 is a series of entries, where each entry is a single byte followed
19737 by a nul terminated string. The byte gives the bit number to test
19738 if enabled in the bitmask. */
19739 printf (_(" Hardware Capabilities: "));
19740 if (pnote->descsz < 8)
19741 {
19742 error (_("<corrupt GNU_HWCAP>\n"));
19743 return false;
19744 }
19745 num_entries = byte_get ((unsigned char *) pnote->descdata, 4);
19746 mask = byte_get ((unsigned char *) pnote->descdata + 4, 4);
19747 printf (_("num entries: %ld, enabled mask: %lx\n"), num_entries, mask);
19748 /* FIXME: Add code to display the entries... */
19749 }
19750 break;
19751
19752 case NT_GNU_PROPERTY_TYPE_0:
19753 print_gnu_property_note (filedata, pnote);
19754 break;
19755
19756 default:
19757 /* Handle unrecognised types. An error message should have already been
19758 created by get_gnu_elf_note_type(), so all that we need to do is to
19759 display the data. */
19760 {
19761 unsigned long i;
19762
19763 printf (_(" Description data: "));
19764 for (i = 0; i < pnote->descsz; ++i)
19765 printf ("%02x ", pnote->descdata[i] & 0xff);
19766 printf ("\n");
19767 }
19768 break;
19769 }
19770
19771 return true;
19772 }
19773
19774 static const char *
19775 get_v850_elf_note_type (enum v850_notes n_type)
19776 {
19777 static char buff[64];
19778
19779 switch (n_type)
19780 {
19781 case V850_NOTE_ALIGNMENT: return _("Alignment of 8-byte objects");
19782 case V850_NOTE_DATA_SIZE: return _("Sizeof double and long double");
19783 case V850_NOTE_FPU_INFO: return _("Type of FPU support needed");
19784 case V850_NOTE_SIMD_INFO: return _("Use of SIMD instructions");
19785 case V850_NOTE_CACHE_INFO: return _("Use of cache");
19786 case V850_NOTE_MMU_INFO: return _("Use of MMU");
19787 default:
19788 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), n_type);
19789 return buff;
19790 }
19791 }
19792
19793 static bool
19794 print_v850_note (Elf_Internal_Note * pnote)
19795 {
19796 unsigned int val;
19797
19798 if (pnote->descsz != 4)
19799 return false;
19800
19801 val = byte_get ((unsigned char *) pnote->descdata, pnote->descsz);
19802
19803 if (val == 0)
19804 {
19805 printf (_("not set\n"));
19806 return true;
19807 }
19808
19809 switch (pnote->type)
19810 {
19811 case V850_NOTE_ALIGNMENT:
19812 switch (val)
19813 {
19814 case EF_RH850_DATA_ALIGN4: printf (_("4-byte\n")); return true;
19815 case EF_RH850_DATA_ALIGN8: printf (_("8-byte\n")); return true;
19816 }
19817 break;
19818
19819 case V850_NOTE_DATA_SIZE:
19820 switch (val)
19821 {
19822 case EF_RH850_DOUBLE32: printf (_("4-bytes\n")); return true;
19823 case EF_RH850_DOUBLE64: printf (_("8-bytes\n")); return true;
19824 }
19825 break;
19826
19827 case V850_NOTE_FPU_INFO:
19828 switch (val)
19829 {
19830 case EF_RH850_FPU20: printf (_("FPU-2.0\n")); return true;
19831 case EF_RH850_FPU30: printf (_("FPU-3.0\n")); return true;
19832 }
19833 break;
19834
19835 case V850_NOTE_MMU_INFO:
19836 case V850_NOTE_CACHE_INFO:
19837 case V850_NOTE_SIMD_INFO:
19838 if (val == EF_RH850_SIMD)
19839 {
19840 printf (_("yes\n"));
19841 return true;
19842 }
19843 break;
19844
19845 default:
19846 /* An 'unknown note type' message will already have been displayed. */
19847 break;
19848 }
19849
19850 printf (_("unknown value: %x\n"), val);
19851 return false;
19852 }
19853
19854 static bool
19855 process_netbsd_elf_note (Elf_Internal_Note * pnote)
19856 {
19857 unsigned int version;
19858
19859 switch (pnote->type)
19860 {
19861 case NT_NETBSD_IDENT:
19862 if (pnote->descsz < 1)
19863 break;
19864 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
19865 if ((version / 10000) % 100)
19866 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u%s%c)\n", pnote->descsz,
19867 version, version / 100000000, (version / 1000000) % 100,
19868 (version / 10000) % 100 > 26 ? "Z" : "",
19869 'A' + (version / 10000) % 26);
19870 else
19871 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u.%u)\n", pnote->descsz,
19872 version, version / 100000000, (version / 1000000) % 100,
19873 (version / 100) % 100);
19874 return true;
19875
19876 case NT_NETBSD_MARCH:
19877 printf (" NetBSD\t\t0x%08lx\tMARCH <%s>\n", pnote->descsz,
19878 pnote->descdata);
19879 return true;
19880
19881 case NT_NETBSD_PAX:
19882 if (pnote->descsz < 1)
19883 break;
19884 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
19885 printf (" NetBSD\t\t0x%08lx\tPaX <%s%s%s%s%s%s>\n", pnote->descsz,
19886 ((version & NT_NETBSD_PAX_MPROTECT) ? "+mprotect" : ""),
19887 ((version & NT_NETBSD_PAX_NOMPROTECT) ? "-mprotect" : ""),
19888 ((version & NT_NETBSD_PAX_GUARD) ? "+guard" : ""),
19889 ((version & NT_NETBSD_PAX_NOGUARD) ? "-guard" : ""),
19890 ((version & NT_NETBSD_PAX_ASLR) ? "+ASLR" : ""),
19891 ((version & NT_NETBSD_PAX_NOASLR) ? "-ASLR" : ""));
19892 return true;
19893 }
19894
19895 printf (" NetBSD\t0x%08lx\tUnknown note type: (0x%08lx)\n",
19896 pnote->descsz, pnote->type);
19897 return false;
19898 }
19899
19900 static const char *
19901 get_freebsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
19902 {
19903 switch (e_type)
19904 {
19905 case NT_FREEBSD_THRMISC:
19906 return _("NT_THRMISC (thrmisc structure)");
19907 case NT_FREEBSD_PROCSTAT_PROC:
19908 return _("NT_PROCSTAT_PROC (proc data)");
19909 case NT_FREEBSD_PROCSTAT_FILES:
19910 return _("NT_PROCSTAT_FILES (files data)");
19911 case NT_FREEBSD_PROCSTAT_VMMAP:
19912 return _("NT_PROCSTAT_VMMAP (vmmap data)");
19913 case NT_FREEBSD_PROCSTAT_GROUPS:
19914 return _("NT_PROCSTAT_GROUPS (groups data)");
19915 case NT_FREEBSD_PROCSTAT_UMASK:
19916 return _("NT_PROCSTAT_UMASK (umask data)");
19917 case NT_FREEBSD_PROCSTAT_RLIMIT:
19918 return _("NT_PROCSTAT_RLIMIT (rlimit data)");
19919 case NT_FREEBSD_PROCSTAT_OSREL:
19920 return _("NT_PROCSTAT_OSREL (osreldate data)");
19921 case NT_FREEBSD_PROCSTAT_PSSTRINGS:
19922 return _("NT_PROCSTAT_PSSTRINGS (ps_strings data)");
19923 case NT_FREEBSD_PROCSTAT_AUXV:
19924 return _("NT_PROCSTAT_AUXV (auxv data)");
19925 case NT_FREEBSD_PTLWPINFO:
19926 return _("NT_PTLWPINFO (ptrace_lwpinfo structure)");
19927 }
19928 return get_note_type (filedata, e_type);
19929 }
19930
19931 static const char *
19932 get_netbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
19933 {
19934 static char buff[64];
19935
19936 switch (e_type)
19937 {
19938 case NT_NETBSDCORE_PROCINFO:
19939 /* NetBSD core "procinfo" structure. */
19940 return _("NetBSD procinfo structure");
19941
19942 case NT_NETBSDCORE_AUXV:
19943 return _("NetBSD ELF auxiliary vector data");
19944
19945 case NT_NETBSDCORE_LWPSTATUS:
19946 return _("PT_LWPSTATUS (ptrace_lwpstatus structure)");
19947
19948 default:
19949 /* As of Jan 2020 there are no other machine-independent notes
19950 defined for NetBSD core files. If the note type is less
19951 than the start of the machine-dependent note types, we don't
19952 understand it. */
19953
19954 if (e_type < NT_NETBSDCORE_FIRSTMACH)
19955 {
19956 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19957 return buff;
19958 }
19959 break;
19960 }
19961
19962 switch (filedata->file_header.e_machine)
19963 {
19964 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
19965 and PT_GETFPREGS == mach+2. */
19966
19967 case EM_OLD_ALPHA:
19968 case EM_ALPHA:
19969 case EM_SPARC:
19970 case EM_SPARC32PLUS:
19971 case EM_SPARCV9:
19972 switch (e_type)
19973 {
19974 case NT_NETBSDCORE_FIRSTMACH + 0:
19975 return _("PT_GETREGS (reg structure)");
19976 case NT_NETBSDCORE_FIRSTMACH + 2:
19977 return _("PT_GETFPREGS (fpreg structure)");
19978 default:
19979 break;
19980 }
19981 break;
19982
19983 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
19984 There's also old PT___GETREGS40 == mach + 1 for old reg
19985 structure which lacks GBR. */
19986 case EM_SH:
19987 switch (e_type)
19988 {
19989 case NT_NETBSDCORE_FIRSTMACH + 1:
19990 return _("PT___GETREGS40 (old reg structure)");
19991 case NT_NETBSDCORE_FIRSTMACH + 3:
19992 return _("PT_GETREGS (reg structure)");
19993 case NT_NETBSDCORE_FIRSTMACH + 5:
19994 return _("PT_GETFPREGS (fpreg structure)");
19995 default:
19996 break;
19997 }
19998 break;
19999
20000 /* On all other arch's, PT_GETREGS == mach+1 and
20001 PT_GETFPREGS == mach+3. */
20002 default:
20003 switch (e_type)
20004 {
20005 case NT_NETBSDCORE_FIRSTMACH + 1:
20006 return _("PT_GETREGS (reg structure)");
20007 case NT_NETBSDCORE_FIRSTMACH + 3:
20008 return _("PT_GETFPREGS (fpreg structure)");
20009 default:
20010 break;
20011 }
20012 }
20013
20014 snprintf (buff, sizeof (buff), "PT_FIRSTMACH+%d",
20015 e_type - NT_NETBSDCORE_FIRSTMACH);
20016 return buff;
20017 }
20018
20019 static const char *
20020 get_openbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
20021 {
20022 switch (e_type)
20023 {
20024 case NT_OPENBSD_PROCINFO:
20025 return _("OpenBSD procinfo structure");
20026 case NT_OPENBSD_AUXV:
20027 return _("OpenBSD ELF auxiliary vector data");
20028 case NT_OPENBSD_REGS:
20029 return _("OpenBSD regular registers");
20030 case NT_OPENBSD_FPREGS:
20031 return _("OpenBSD floating point registers");
20032 case NT_OPENBSD_WCOOKIE:
20033 return _("OpenBSD window cookie");
20034 }
20035
20036 return get_note_type (filedata, e_type);
20037 }
20038
20039 static const char *
20040 get_stapsdt_note_type (unsigned e_type)
20041 {
20042 static char buff[64];
20043
20044 switch (e_type)
20045 {
20046 case NT_STAPSDT:
20047 return _("NT_STAPSDT (SystemTap probe descriptors)");
20048
20049 default:
20050 break;
20051 }
20052
20053 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20054 return buff;
20055 }
20056
20057 static bool
20058 print_stapsdt_note (Elf_Internal_Note *pnote)
20059 {
20060 size_t len, maxlen;
20061 unsigned long addr_size = is_32bit_elf ? 4 : 8;
20062 char *data = pnote->descdata;
20063 char *data_end = pnote->descdata + pnote->descsz;
20064 bfd_vma pc, base_addr, semaphore;
20065 char *provider, *probe, *arg_fmt;
20066
20067 if (pnote->descsz < (addr_size * 3))
20068 goto stapdt_note_too_small;
20069
20070 pc = byte_get ((unsigned char *) data, addr_size);
20071 data += addr_size;
20072
20073 base_addr = byte_get ((unsigned char *) data, addr_size);
20074 data += addr_size;
20075
20076 semaphore = byte_get ((unsigned char *) data, addr_size);
20077 data += addr_size;
20078
20079 if (data >= data_end)
20080 goto stapdt_note_too_small;
20081 maxlen = data_end - data;
20082 len = strnlen (data, maxlen);
20083 if (len < maxlen)
20084 {
20085 provider = data;
20086 data += len + 1;
20087 }
20088 else
20089 goto stapdt_note_too_small;
20090
20091 if (data >= data_end)
20092 goto stapdt_note_too_small;
20093 maxlen = data_end - data;
20094 len = strnlen (data, maxlen);
20095 if (len < maxlen)
20096 {
20097 probe = data;
20098 data += len + 1;
20099 }
20100 else
20101 goto stapdt_note_too_small;
20102
20103 if (data >= data_end)
20104 goto stapdt_note_too_small;
20105 maxlen = data_end - data;
20106 len = strnlen (data, maxlen);
20107 if (len < maxlen)
20108 {
20109 arg_fmt = data;
20110 data += len + 1;
20111 }
20112 else
20113 goto stapdt_note_too_small;
20114
20115 printf (_(" Provider: %s\n"), provider);
20116 printf (_(" Name: %s\n"), probe);
20117 printf (_(" Location: "));
20118 print_vma (pc, FULL_HEX);
20119 printf (_(", Base: "));
20120 print_vma (base_addr, FULL_HEX);
20121 printf (_(", Semaphore: "));
20122 print_vma (semaphore, FULL_HEX);
20123 printf ("\n");
20124 printf (_(" Arguments: %s\n"), arg_fmt);
20125
20126 return data == data_end;
20127
20128 stapdt_note_too_small:
20129 printf (_(" <corrupt - note is too small>\n"));
20130 error (_("corrupt stapdt note - the data size is too small\n"));
20131 return false;
20132 }
20133
20134 static const char *
20135 get_ia64_vms_note_type (unsigned e_type)
20136 {
20137 static char buff[64];
20138
20139 switch (e_type)
20140 {
20141 case NT_VMS_MHD:
20142 return _("NT_VMS_MHD (module header)");
20143 case NT_VMS_LNM:
20144 return _("NT_VMS_LNM (language name)");
20145 case NT_VMS_SRC:
20146 return _("NT_VMS_SRC (source files)");
20147 case NT_VMS_TITLE:
20148 return "NT_VMS_TITLE";
20149 case NT_VMS_EIDC:
20150 return _("NT_VMS_EIDC (consistency check)");
20151 case NT_VMS_FPMODE:
20152 return _("NT_VMS_FPMODE (FP mode)");
20153 case NT_VMS_LINKTIME:
20154 return "NT_VMS_LINKTIME";
20155 case NT_VMS_IMGNAM:
20156 return _("NT_VMS_IMGNAM (image name)");
20157 case NT_VMS_IMGID:
20158 return _("NT_VMS_IMGID (image id)");
20159 case NT_VMS_LINKID:
20160 return _("NT_VMS_LINKID (link id)");
20161 case NT_VMS_IMGBID:
20162 return _("NT_VMS_IMGBID (build id)");
20163 case NT_VMS_GSTNAM:
20164 return _("NT_VMS_GSTNAM (sym table name)");
20165 case NT_VMS_ORIG_DYN:
20166 return "NT_VMS_ORIG_DYN";
20167 case NT_VMS_PATCHTIME:
20168 return "NT_VMS_PATCHTIME";
20169 default:
20170 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20171 return buff;
20172 }
20173 }
20174
20175 static bool
20176 print_ia64_vms_note (Elf_Internal_Note * pnote)
20177 {
20178 int maxlen = pnote->descsz;
20179
20180 if (maxlen < 2 || (unsigned long) maxlen != pnote->descsz)
20181 goto desc_size_fail;
20182
20183 switch (pnote->type)
20184 {
20185 case NT_VMS_MHD:
20186 if (maxlen <= 36)
20187 goto desc_size_fail;
20188
20189 int l = (int) strnlen (pnote->descdata + 34, maxlen - 34);
20190
20191 printf (_(" Creation date : %.17s\n"), pnote->descdata);
20192 printf (_(" Last patch date: %.17s\n"), pnote->descdata + 17);
20193 if (l + 34 < maxlen)
20194 {
20195 printf (_(" Module name : %s\n"), pnote->descdata + 34);
20196 if (l + 35 < maxlen)
20197 printf (_(" Module version : %s\n"), pnote->descdata + 34 + l + 1);
20198 else
20199 printf (_(" Module version : <missing>\n"));
20200 }
20201 else
20202 {
20203 printf (_(" Module name : <missing>\n"));
20204 printf (_(" Module version : <missing>\n"));
20205 }
20206 break;
20207
20208 case NT_VMS_LNM:
20209 printf (_(" Language: %.*s\n"), maxlen, pnote->descdata);
20210 break;
20211
20212 #ifdef BFD64
20213 case NT_VMS_FPMODE:
20214 printf (_(" Floating Point mode: "));
20215 if (maxlen < 8)
20216 goto desc_size_fail;
20217 /* FIXME: Generate an error if descsz > 8 ? */
20218
20219 printf ("0x%016" BFD_VMA_FMT "x\n",
20220 (bfd_vma) byte_get ((unsigned char *)pnote->descdata, 8));
20221 break;
20222
20223 case NT_VMS_LINKTIME:
20224 printf (_(" Link time: "));
20225 if (maxlen < 8)
20226 goto desc_size_fail;
20227 /* FIXME: Generate an error if descsz > 8 ? */
20228
20229 print_vms_time
20230 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
20231 printf ("\n");
20232 break;
20233
20234 case NT_VMS_PATCHTIME:
20235 printf (_(" Patch time: "));
20236 if (maxlen < 8)
20237 goto desc_size_fail;
20238 /* FIXME: Generate an error if descsz > 8 ? */
20239
20240 print_vms_time
20241 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
20242 printf ("\n");
20243 break;
20244
20245 case NT_VMS_ORIG_DYN:
20246 if (maxlen < 34)
20247 goto desc_size_fail;
20248
20249 printf (_(" Major id: %u, minor id: %u\n"),
20250 (unsigned) byte_get ((unsigned char *)pnote->descdata, 4),
20251 (unsigned) byte_get ((unsigned char *)pnote->descdata + 4, 4));
20252 printf (_(" Last modified : "));
20253 print_vms_time
20254 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata + 8, 8));
20255 printf (_("\n Link flags : "));
20256 printf ("0x%016" BFD_VMA_FMT "x\n",
20257 (bfd_vma) byte_get ((unsigned char *)pnote->descdata + 16, 8));
20258 printf (_(" Header flags: 0x%08x\n"),
20259 (unsigned) byte_get ((unsigned char *)pnote->descdata + 24, 4));
20260 printf (_(" Image id : %.*s\n"), maxlen - 32, pnote->descdata + 32);
20261 break;
20262 #endif
20263
20264 case NT_VMS_IMGNAM:
20265 printf (_(" Image name: %.*s\n"), maxlen, pnote->descdata);
20266 break;
20267
20268 case NT_VMS_GSTNAM:
20269 printf (_(" Global symbol table name: %.*s\n"), maxlen, pnote->descdata);
20270 break;
20271
20272 case NT_VMS_IMGID:
20273 printf (_(" Image id: %.*s\n"), maxlen, pnote->descdata);
20274 break;
20275
20276 case NT_VMS_LINKID:
20277 printf (_(" Linker id: %.*s\n"), maxlen, pnote->descdata);
20278 break;
20279
20280 default:
20281 return false;
20282 }
20283
20284 return true;
20285
20286 desc_size_fail:
20287 printf (_(" <corrupt - data size is too small>\n"));
20288 error (_("corrupt IA64 note: data size is too small\n"));
20289 return false;
20290 }
20291
20292 struct build_attr_cache {
20293 Filedata *filedata;
20294 char *strtab;
20295 unsigned long strtablen;
20296 Elf_Internal_Sym *symtab;
20297 unsigned long nsyms;
20298 } ba_cache;
20299
20300 /* Find the symbol associated with a build attribute that is attached
20301 to address OFFSET. If PNAME is non-NULL then store the name of
20302 the symbol (if found) in the provided pointer, Returns NULL if a
20303 symbol could not be found. */
20304
20305 static Elf_Internal_Sym *
20306 get_symbol_for_build_attribute (Filedata *filedata,
20307 unsigned long offset,
20308 bool is_open_attr,
20309 const char **pname)
20310 {
20311 Elf_Internal_Sym *saved_sym = NULL;
20312 Elf_Internal_Sym *sym;
20313
20314 if (filedata->section_headers != NULL
20315 && (ba_cache.filedata == NULL || filedata != ba_cache.filedata))
20316 {
20317 Elf_Internal_Shdr * symsec;
20318
20319 free (ba_cache.strtab);
20320 ba_cache.strtab = NULL;
20321 free (ba_cache.symtab);
20322 ba_cache.symtab = NULL;
20323
20324 /* Load the symbol and string sections. */
20325 for (symsec = filedata->section_headers;
20326 symsec < filedata->section_headers + filedata->file_header.e_shnum;
20327 symsec ++)
20328 {
20329 if (symsec->sh_type == SHT_SYMTAB
20330 && get_symtab (filedata, symsec,
20331 &ba_cache.symtab, &ba_cache.nsyms,
20332 &ba_cache.strtab, &ba_cache.strtablen))
20333 break;
20334 }
20335 ba_cache.filedata = filedata;
20336 }
20337
20338 if (ba_cache.symtab == NULL)
20339 return NULL;
20340
20341 /* Find a symbol whose value matches offset. */
20342 for (sym = ba_cache.symtab; sym < ba_cache.symtab + ba_cache.nsyms; sym ++)
20343 if (sym->st_value == offset)
20344 {
20345 if (sym->st_name >= ba_cache.strtablen)
20346 /* Huh ? This should not happen. */
20347 continue;
20348
20349 if (ba_cache.strtab[sym->st_name] == 0)
20350 continue;
20351
20352 /* The AArch64, ARM and RISC-V architectures define mapping symbols
20353 (eg $d, $x, $t) which we want to ignore. */
20354 if (ba_cache.strtab[sym->st_name] == '$'
20355 && ba_cache.strtab[sym->st_name + 1] != 0
20356 && ba_cache.strtab[sym->st_name + 2] == 0)
20357 continue;
20358
20359 if (is_open_attr)
20360 {
20361 /* For OPEN attributes we prefer GLOBAL over LOCAL symbols
20362 and FILE or OBJECT symbols over NOTYPE symbols. We skip
20363 FUNC symbols entirely. */
20364 switch (ELF_ST_TYPE (sym->st_info))
20365 {
20366 case STT_OBJECT:
20367 case STT_FILE:
20368 saved_sym = sym;
20369 if (sym->st_size)
20370 {
20371 /* If the symbol has a size associated
20372 with it then we can stop searching. */
20373 sym = ba_cache.symtab + ba_cache.nsyms;
20374 }
20375 continue;
20376
20377 case STT_FUNC:
20378 /* Ignore function symbols. */
20379 continue;
20380
20381 default:
20382 break;
20383 }
20384
20385 switch (ELF_ST_BIND (sym->st_info))
20386 {
20387 case STB_GLOBAL:
20388 if (saved_sym == NULL
20389 || ELF_ST_TYPE (saved_sym->st_info) != STT_OBJECT)
20390 saved_sym = sym;
20391 break;
20392
20393 case STB_LOCAL:
20394 if (saved_sym == NULL)
20395 saved_sym = sym;
20396 break;
20397
20398 default:
20399 break;
20400 }
20401 }
20402 else
20403 {
20404 if (ELF_ST_TYPE (sym->st_info) != STT_FUNC)
20405 continue;
20406
20407 saved_sym = sym;
20408 break;
20409 }
20410 }
20411
20412 if (saved_sym && pname)
20413 * pname = ba_cache.strtab + saved_sym->st_name;
20414
20415 return saved_sym;
20416 }
20417
20418 /* Returns true iff addr1 and addr2 are in the same section. */
20419
20420 static bool
20421 same_section (Filedata * filedata, unsigned long addr1, unsigned long addr2)
20422 {
20423 Elf_Internal_Shdr * a1;
20424 Elf_Internal_Shdr * a2;
20425
20426 a1 = find_section_by_address (filedata, addr1);
20427 a2 = find_section_by_address (filedata, addr2);
20428
20429 return a1 == a2 && a1 != NULL;
20430 }
20431
20432 static bool
20433 print_gnu_build_attribute_description (Elf_Internal_Note * pnote,
20434 Filedata * filedata)
20435 {
20436 static unsigned long global_offset = 0;
20437 static unsigned long global_end = 0;
20438 static unsigned long func_offset = 0;
20439 static unsigned long func_end = 0;
20440
20441 Elf_Internal_Sym *sym;
20442 const char *name;
20443 unsigned long start;
20444 unsigned long end;
20445 bool is_open_attr = pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN;
20446
20447 switch (pnote->descsz)
20448 {
20449 case 0:
20450 /* A zero-length description means that the range of
20451 the previous note of the same type should be used. */
20452 if (is_open_attr)
20453 {
20454 if (global_end > global_offset)
20455 printf (_(" Applies to region from %#lx to %#lx\n"),
20456 global_offset, global_end);
20457 else
20458 printf (_(" Applies to region from %#lx\n"), global_offset);
20459 }
20460 else
20461 {
20462 if (func_end > func_offset)
20463 printf (_(" Applies to region from %#lx to %#lx\n"), func_offset, func_end);
20464 else
20465 printf (_(" Applies to region from %#lx\n"), func_offset);
20466 }
20467 return true;
20468
20469 case 4:
20470 start = byte_get ((unsigned char *) pnote->descdata, 4);
20471 end = 0;
20472 break;
20473
20474 case 8:
20475 start = byte_get ((unsigned char *) pnote->descdata, 4);
20476 end = byte_get ((unsigned char *) pnote->descdata + 4, 4);
20477 break;
20478
20479 case 16:
20480 start = byte_get ((unsigned char *) pnote->descdata, 8);
20481 end = byte_get ((unsigned char *) pnote->descdata + 8, 8);
20482 break;
20483
20484 default:
20485 error (_(" <invalid description size: %lx>\n"), pnote->descsz);
20486 printf (_(" <invalid descsz>"));
20487 return false;
20488 }
20489
20490 name = NULL;
20491 sym = get_symbol_for_build_attribute (filedata, start, is_open_attr, & name);
20492 /* As of version 5 of the annobin plugin, filename symbols are biased by 2
20493 in order to avoid them being confused with the start address of the
20494 first function in the file... */
20495 if (sym == NULL && is_open_attr)
20496 sym = get_symbol_for_build_attribute (filedata, start + 2, is_open_attr,
20497 & name);
20498
20499 if (end == 0 && sym != NULL && sym->st_size > 0)
20500 end = start + sym->st_size;
20501
20502 if (is_open_attr)
20503 {
20504 /* FIXME: Need to properly allow for section alignment.
20505 16 is just the alignment used on x86_64. */
20506 if (global_end > 0
20507 && start > BFD_ALIGN (global_end, 16)
20508 /* Build notes are not guaranteed to be organised in order of
20509 increasing address, but we should find the all of the notes
20510 for one section in the same place. */
20511 && same_section (filedata, start, global_end))
20512 warn (_("Gap in build notes detected from %#lx to %#lx\n"),
20513 global_end + 1, start - 1);
20514
20515 printf (_(" Applies to region from %#lx"), start);
20516 global_offset = start;
20517
20518 if (end)
20519 {
20520 printf (_(" to %#lx"), end);
20521 global_end = end;
20522 }
20523 }
20524 else
20525 {
20526 printf (_(" Applies to region from %#lx"), start);
20527 func_offset = start;
20528
20529 if (end)
20530 {
20531 printf (_(" to %#lx"), end);
20532 func_end = end;
20533 }
20534 }
20535
20536 if (sym && name)
20537 printf (_(" (%s)"), name);
20538
20539 printf ("\n");
20540 return true;
20541 }
20542
20543 static bool
20544 print_gnu_build_attribute_name (Elf_Internal_Note * pnote)
20545 {
20546 static const char string_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_STRING, 0 };
20547 static const char number_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC, 0 };
20548 static const char bool_expected [3] = { GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE, GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE, 0 };
20549 char name_type;
20550 char name_attribute;
20551 const char * expected_types;
20552 const char * name = pnote->namedata;
20553 const char * text;
20554 signed int left;
20555
20556 if (name == NULL || pnote->namesz < 2)
20557 {
20558 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
20559 print_symbol (-20, _(" <corrupt name>"));
20560 return false;
20561 }
20562
20563 if (do_wide)
20564 left = 28;
20565 else
20566 left = 20;
20567
20568 /* Version 2 of the spec adds a "GA" prefix to the name field. */
20569 if (name[0] == 'G' && name[1] == 'A')
20570 {
20571 if (pnote->namesz < 4)
20572 {
20573 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
20574 print_symbol (-20, _(" <corrupt name>"));
20575 return false;
20576 }
20577
20578 printf ("GA");
20579 name += 2;
20580 left -= 2;
20581 }
20582
20583 switch ((name_type = * name))
20584 {
20585 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
20586 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
20587 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
20588 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
20589 printf ("%c", * name);
20590 left --;
20591 break;
20592 default:
20593 error (_("unrecognised attribute type in name field: %d\n"), name_type);
20594 print_symbol (-20, _("<unknown name type>"));
20595 return false;
20596 }
20597
20598 ++ name;
20599 text = NULL;
20600
20601 switch ((name_attribute = * name))
20602 {
20603 case GNU_BUILD_ATTRIBUTE_VERSION:
20604 text = _("<version>");
20605 expected_types = string_expected;
20606 ++ name;
20607 break;
20608 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
20609 text = _("<stack prot>");
20610 expected_types = "!+*";
20611 ++ name;
20612 break;
20613 case GNU_BUILD_ATTRIBUTE_RELRO:
20614 text = _("<relro>");
20615 expected_types = bool_expected;
20616 ++ name;
20617 break;
20618 case GNU_BUILD_ATTRIBUTE_STACK_SIZE:
20619 text = _("<stack size>");
20620 expected_types = number_expected;
20621 ++ name;
20622 break;
20623 case GNU_BUILD_ATTRIBUTE_TOOL:
20624 text = _("<tool>");
20625 expected_types = string_expected;
20626 ++ name;
20627 break;
20628 case GNU_BUILD_ATTRIBUTE_ABI:
20629 text = _("<ABI>");
20630 expected_types = "$*";
20631 ++ name;
20632 break;
20633 case GNU_BUILD_ATTRIBUTE_PIC:
20634 text = _("<PIC>");
20635 expected_types = number_expected;
20636 ++ name;
20637 break;
20638 case GNU_BUILD_ATTRIBUTE_SHORT_ENUM:
20639 text = _("<short enum>");
20640 expected_types = bool_expected;
20641 ++ name;
20642 break;
20643 default:
20644 if (ISPRINT (* name))
20645 {
20646 int len = strnlen (name, pnote->namesz - (name - pnote->namedata)) + 1;
20647
20648 if (len > left && ! do_wide)
20649 len = left;
20650 printf ("%.*s:", len, name);
20651 left -= len;
20652 name += len;
20653 }
20654 else
20655 {
20656 static char tmpbuf [128];
20657
20658 error (_("unrecognised byte in name field: %d\n"), * name);
20659 sprintf (tmpbuf, _("<unknown:_%d>"), * name);
20660 text = tmpbuf;
20661 name ++;
20662 }
20663 expected_types = "*$!+";
20664 break;
20665 }
20666
20667 if (text)
20668 left -= printf ("%s", text);
20669
20670 if (strchr (expected_types, name_type) == NULL)
20671 warn (_("attribute does not have an expected type (%c)\n"), name_type);
20672
20673 if ((unsigned long)(name - pnote->namedata) > pnote->namesz)
20674 {
20675 error (_("corrupt name field: namesz: %lu but parsing gets to %ld\n"),
20676 (unsigned long) pnote->namesz,
20677 (long) (name - pnote->namedata));
20678 return false;
20679 }
20680
20681 if (left < 1 && ! do_wide)
20682 return true;
20683
20684 switch (name_type)
20685 {
20686 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
20687 {
20688 unsigned int bytes;
20689 unsigned long long val = 0;
20690 unsigned int shift = 0;
20691 char * decoded = NULL;
20692
20693 bytes = pnote->namesz - (name - pnote->namedata);
20694 if (bytes > 0)
20695 /* The -1 is because the name field is always 0 terminated, and we
20696 want to be able to ensure that the shift in the while loop below
20697 will not overflow. */
20698 -- bytes;
20699
20700 if (bytes > sizeof (val))
20701 {
20702 error (_("corrupt numeric name field: too many bytes in the value: %x\n"),
20703 bytes);
20704 bytes = sizeof (val);
20705 }
20706 /* We do not bother to warn if bytes == 0 as this can
20707 happen with some early versions of the gcc plugin. */
20708
20709 while (bytes --)
20710 {
20711 unsigned long long byte = *name++ & 0xff;
20712
20713 val |= byte << shift;
20714 shift += 8;
20715 }
20716
20717 switch (name_attribute)
20718 {
20719 case GNU_BUILD_ATTRIBUTE_PIC:
20720 switch (val)
20721 {
20722 case 0: decoded = "static"; break;
20723 case 1: decoded = "pic"; break;
20724 case 2: decoded = "PIC"; break;
20725 case 3: decoded = "pie"; break;
20726 case 4: decoded = "PIE"; break;
20727 default: break;
20728 }
20729 break;
20730 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
20731 switch (val)
20732 {
20733 /* Based upon the SPCT_FLAG_xxx enum values in gcc/cfgexpand.c. */
20734 case 0: decoded = "off"; break;
20735 case 1: decoded = "on"; break;
20736 case 2: decoded = "all"; break;
20737 case 3: decoded = "strong"; break;
20738 case 4: decoded = "explicit"; break;
20739 default: break;
20740 }
20741 break;
20742 default:
20743 break;
20744 }
20745
20746 if (decoded != NULL)
20747 {
20748 print_symbol (-left, decoded);
20749 left = 0;
20750 }
20751 else if (val == 0)
20752 {
20753 printf ("0x0");
20754 left -= 3;
20755 }
20756 else
20757 {
20758 if (do_wide)
20759 left -= printf ("0x%llx", val);
20760 else
20761 left -= printf ("0x%-.*llx", left, val);
20762 }
20763 }
20764 break;
20765 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
20766 left -= print_symbol (- left, name);
20767 break;
20768 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
20769 left -= print_symbol (- left, "true");
20770 break;
20771 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
20772 left -= print_symbol (- left, "false");
20773 break;
20774 }
20775
20776 if (do_wide && left > 0)
20777 printf ("%-*s", left, " ");
20778
20779 return true;
20780 }
20781
20782 /* Note that by the ELF standard, the name field is already null byte
20783 terminated, and namesz includes the terminating null byte.
20784 I.E. the value of namesz for the name "FSF" is 4.
20785
20786 If the value of namesz is zero, there is no name present. */
20787
20788 static bool
20789 process_note (Elf_Internal_Note * pnote,
20790 Filedata * filedata)
20791 {
20792 const char * name = pnote->namesz ? pnote->namedata : "(NONE)";
20793 const char * nt;
20794
20795 if (pnote->namesz == 0)
20796 /* If there is no note name, then use the default set of
20797 note type strings. */
20798 nt = get_note_type (filedata, pnote->type);
20799
20800 else if (startswith (pnote->namedata, "GNU"))
20801 /* GNU-specific object file notes. */
20802 nt = get_gnu_elf_note_type (pnote->type);
20803
20804 else if (startswith (pnote->namedata, "FreeBSD"))
20805 /* FreeBSD-specific core file notes. */
20806 nt = get_freebsd_elfcore_note_type (filedata, pnote->type);
20807
20808 else if (startswith (pnote->namedata, "NetBSD-CORE"))
20809 /* NetBSD-specific core file notes. */
20810 nt = get_netbsd_elfcore_note_type (filedata, pnote->type);
20811
20812 else if (startswith (pnote->namedata, "NetBSD"))
20813 /* NetBSD-specific core file notes. */
20814 return process_netbsd_elf_note (pnote);
20815
20816 else if (startswith (pnote->namedata, "PaX"))
20817 /* NetBSD-specific core file notes. */
20818 return process_netbsd_elf_note (pnote);
20819
20820 else if (startswith (pnote->namedata, "OpenBSD"))
20821 /* OpenBSD-specific core file notes. */
20822 nt = get_openbsd_elfcore_note_type (filedata, pnote->type);
20823
20824 else if (startswith (pnote->namedata, "SPU/"))
20825 {
20826 /* SPU-specific core file notes. */
20827 nt = pnote->namedata + 4;
20828 name = "SPU";
20829 }
20830
20831 else if (startswith (pnote->namedata, "IPF/VMS"))
20832 /* VMS/ia64-specific file notes. */
20833 nt = get_ia64_vms_note_type (pnote->type);
20834
20835 else if (startswith (pnote->namedata, "stapsdt"))
20836 nt = get_stapsdt_note_type (pnote->type);
20837
20838 else
20839 /* Don't recognize this note name; just use the default set of
20840 note type strings. */
20841 nt = get_note_type (filedata, pnote->type);
20842
20843 printf (" ");
20844
20845 if (((startswith (pnote->namedata, "GA")
20846 && strchr ("*$!+", pnote->namedata[2]) != NULL)
20847 || strchr ("*$!+", pnote->namedata[0]) != NULL)
20848 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
20849 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
20850 print_gnu_build_attribute_name (pnote);
20851 else
20852 print_symbol (-20, name);
20853
20854 if (do_wide)
20855 printf (" 0x%08lx\t%s\t", pnote->descsz, nt);
20856 else
20857 printf (" 0x%08lx\t%s\n", pnote->descsz, nt);
20858
20859 if (startswith (pnote->namedata, "IPF/VMS"))
20860 return print_ia64_vms_note (pnote);
20861 else if (startswith (pnote->namedata, "GNU"))
20862 return print_gnu_note (filedata, pnote);
20863 else if (startswith (pnote->namedata, "stapsdt"))
20864 return print_stapsdt_note (pnote);
20865 else if (startswith (pnote->namedata, "CORE"))
20866 return print_core_note (pnote);
20867 else if (((startswith (pnote->namedata, "GA")
20868 && strchr ("*$!+", pnote->namedata[2]) != NULL)
20869 || strchr ("*$!+", pnote->namedata[0]) != NULL)
20870 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
20871 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
20872 return print_gnu_build_attribute_description (pnote, filedata);
20873
20874 if (pnote->descsz)
20875 {
20876 unsigned long i;
20877
20878 printf (_(" description data: "));
20879 for (i = 0; i < pnote->descsz; i++)
20880 printf ("%02x ", pnote->descdata[i] & 0xff);
20881 if (!do_wide)
20882 printf ("\n");
20883 }
20884
20885 if (do_wide)
20886 printf ("\n");
20887
20888 return true;
20889 }
20890
20891 static bool
20892 process_notes_at (Filedata * filedata,
20893 Elf_Internal_Shdr * section,
20894 bfd_vma offset,
20895 bfd_vma length,
20896 bfd_vma align)
20897 {
20898 Elf_External_Note *pnotes;
20899 Elf_External_Note *external;
20900 char *end;
20901 bool res = true;
20902
20903 if (length <= 0)
20904 return false;
20905
20906 if (section)
20907 {
20908 pnotes = (Elf_External_Note *) get_section_contents (section, filedata);
20909 if (pnotes)
20910 {
20911 if (! apply_relocations (filedata, section, (unsigned char *) pnotes, length, NULL, NULL))
20912 {
20913 free (pnotes);
20914 return false;
20915 }
20916 }
20917 }
20918 else
20919 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
20920 _("notes"));
20921
20922 if (pnotes == NULL)
20923 return false;
20924
20925 external = pnotes;
20926
20927 if (filedata->is_separate)
20928 printf (_("In linked file '%s': "), filedata->file_name);
20929 else
20930 printf ("\n");
20931 if (section)
20932 printf (_("Displaying notes found in: %s\n"), printable_section_name (filedata, section));
20933 else
20934 printf (_("Displaying notes found at file offset 0x%08lx with length 0x%08lx:\n"),
20935 (unsigned long) offset, (unsigned long) length);
20936
20937 /* NB: Some note sections may have alignment value of 0 or 1. gABI
20938 specifies that notes should be aligned to 4 bytes in 32-bit
20939 objects and to 8 bytes in 64-bit objects. As a Linux extension,
20940 we also support 4 byte alignment in 64-bit objects. If section
20941 alignment is less than 4, we treate alignment as 4 bytes. */
20942 if (align < 4)
20943 align = 4;
20944 else if (align != 4 && align != 8)
20945 {
20946 warn (_("Corrupt note: alignment %ld, expecting 4 or 8\n"),
20947 (long) align);
20948 free (pnotes);
20949 return false;
20950 }
20951
20952 printf (_(" %-20s %-10s\tDescription\n"), _("Owner"), _("Data size"));
20953
20954 end = (char *) pnotes + length;
20955 while ((char *) external < end)
20956 {
20957 Elf_Internal_Note inote;
20958 size_t min_notesz;
20959 char * next;
20960 char * temp = NULL;
20961 size_t data_remaining = end - (char *) external;
20962
20963 if (!is_ia64_vms (filedata))
20964 {
20965 /* PR binutils/15191
20966 Make sure that there is enough data to read. */
20967 min_notesz = offsetof (Elf_External_Note, name);
20968 if (data_remaining < min_notesz)
20969 {
20970 warn (ngettext ("Corrupt note: only %ld byte remains, "
20971 "not enough for a full note\n",
20972 "Corrupt note: only %ld bytes remain, "
20973 "not enough for a full note\n",
20974 data_remaining),
20975 (long) data_remaining);
20976 break;
20977 }
20978 data_remaining -= min_notesz;
20979
20980 inote.type = BYTE_GET (external->type);
20981 inote.namesz = BYTE_GET (external->namesz);
20982 inote.namedata = external->name;
20983 inote.descsz = BYTE_GET (external->descsz);
20984 inote.descdata = ((char *) external
20985 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
20986 inote.descpos = offset + (inote.descdata - (char *) pnotes);
20987 next = ((char *) external
20988 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
20989 }
20990 else
20991 {
20992 Elf64_External_VMS_Note *vms_external;
20993
20994 /* PR binutils/15191
20995 Make sure that there is enough data to read. */
20996 min_notesz = offsetof (Elf64_External_VMS_Note, name);
20997 if (data_remaining < min_notesz)
20998 {
20999 warn (ngettext ("Corrupt note: only %ld byte remains, "
21000 "not enough for a full note\n",
21001 "Corrupt note: only %ld bytes remain, "
21002 "not enough for a full note\n",
21003 data_remaining),
21004 (long) data_remaining);
21005 break;
21006 }
21007 data_remaining -= min_notesz;
21008
21009 vms_external = (Elf64_External_VMS_Note *) external;
21010 inote.type = BYTE_GET (vms_external->type);
21011 inote.namesz = BYTE_GET (vms_external->namesz);
21012 inote.namedata = vms_external->name;
21013 inote.descsz = BYTE_GET (vms_external->descsz);
21014 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
21015 inote.descpos = offset + (inote.descdata - (char *) pnotes);
21016 next = inote.descdata + align_power (inote.descsz, 3);
21017 }
21018
21019 /* PR 17531: file: 3443835e. */
21020 /* PR 17531: file: id:000000,sig:11,src:006986,op:havoc,rep:4. */
21021 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
21022 || (size_t) (inote.descdata - inote.namedata) > data_remaining
21023 || (size_t) (next - inote.descdata) < inote.descsz
21024 || ((size_t) (next - inote.descdata)
21025 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
21026 {
21027 warn (_("note with invalid namesz and/or descsz found at offset 0x%lx\n"),
21028 (unsigned long) ((char *) external - (char *) pnotes));
21029 warn (_(" type: 0x%lx, namesize: 0x%08lx, descsize: 0x%08lx, alignment: %u\n"),
21030 inote.type, inote.namesz, inote.descsz, (int) align);
21031 break;
21032 }
21033
21034 external = (Elf_External_Note *) next;
21035
21036 /* Verify that name is null terminated. It appears that at least
21037 one version of Linux (RedHat 6.0) generates corefiles that don't
21038 comply with the ELF spec by failing to include the null byte in
21039 namesz. */
21040 if (inote.namesz > 0 && inote.namedata[inote.namesz - 1] != '\0')
21041 {
21042 if ((size_t) (inote.descdata - inote.namedata) == inote.namesz)
21043 {
21044 temp = (char *) malloc (inote.namesz + 1);
21045 if (temp == NULL)
21046 {
21047 error (_("Out of memory allocating space for inote name\n"));
21048 res = false;
21049 break;
21050 }
21051
21052 memcpy (temp, inote.namedata, inote.namesz);
21053 inote.namedata = temp;
21054 }
21055 inote.namedata[inote.namesz] = 0;
21056 }
21057
21058 if (! process_note (& inote, filedata))
21059 res = false;
21060
21061 free (temp);
21062 temp = NULL;
21063 }
21064
21065 free (pnotes);
21066
21067 return res;
21068 }
21069
21070 static bool
21071 process_corefile_note_segments (Filedata * filedata)
21072 {
21073 Elf_Internal_Phdr *segment;
21074 unsigned int i;
21075 bool res = true;
21076
21077 if (! get_program_headers (filedata))
21078 return true;
21079
21080 for (i = 0, segment = filedata->program_headers;
21081 i < filedata->file_header.e_phnum;
21082 i++, segment++)
21083 {
21084 if (segment->p_type == PT_NOTE)
21085 if (! process_notes_at (filedata, NULL,
21086 (bfd_vma) segment->p_offset,
21087 (bfd_vma) segment->p_filesz,
21088 (bfd_vma) segment->p_align))
21089 res = false;
21090 }
21091
21092 return res;
21093 }
21094
21095 static bool
21096 process_v850_notes (Filedata * filedata, bfd_vma offset, bfd_vma length)
21097 {
21098 Elf_External_Note * pnotes;
21099 Elf_External_Note * external;
21100 char * end;
21101 bool res = true;
21102
21103 if (length <= 0)
21104 return false;
21105
21106 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
21107 _("v850 notes"));
21108 if (pnotes == NULL)
21109 return false;
21110
21111 external = pnotes;
21112 end = (char*) pnotes + length;
21113
21114 printf (_("\nDisplaying contents of Renesas V850 notes section at offset 0x%lx with length 0x%lx:\n"),
21115 (unsigned long) offset, (unsigned long) length);
21116
21117 while ((char *) external + sizeof (Elf_External_Note) < end)
21118 {
21119 Elf_External_Note * next;
21120 Elf_Internal_Note inote;
21121
21122 inote.type = BYTE_GET (external->type);
21123 inote.namesz = BYTE_GET (external->namesz);
21124 inote.namedata = external->name;
21125 inote.descsz = BYTE_GET (external->descsz);
21126 inote.descdata = inote.namedata + align_power (inote.namesz, 2);
21127 inote.descpos = offset + (inote.descdata - (char *) pnotes);
21128
21129 if (inote.descdata < (char *) pnotes || inote.descdata >= end)
21130 {
21131 warn (_("Corrupt note: name size is too big: %lx\n"), inote.namesz);
21132 inote.descdata = inote.namedata;
21133 inote.namesz = 0;
21134 }
21135
21136 next = (Elf_External_Note *) (inote.descdata + align_power (inote.descsz, 2));
21137
21138 if ( ((char *) next > end)
21139 || ((char *) next < (char *) pnotes))
21140 {
21141 warn (_("corrupt descsz found in note at offset 0x%lx\n"),
21142 (unsigned long) ((char *) external - (char *) pnotes));
21143 warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
21144 inote.type, inote.namesz, inote.descsz);
21145 break;
21146 }
21147
21148 external = next;
21149
21150 /* Prevent out-of-bounds indexing. */
21151 if ( inote.namedata + inote.namesz > end
21152 || inote.namedata + inote.namesz < inote.namedata)
21153 {
21154 warn (_("corrupt namesz found in note at offset 0x%lx\n"),
21155 (unsigned long) ((char *) external - (char *) pnotes));
21156 warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
21157 inote.type, inote.namesz, inote.descsz);
21158 break;
21159 }
21160
21161 printf (" %s: ", get_v850_elf_note_type (inote.type));
21162
21163 if (! print_v850_note (& inote))
21164 {
21165 res = false;
21166 printf ("<corrupt sizes: namesz: %lx, descsz: %lx>\n",
21167 inote.namesz, inote.descsz);
21168 }
21169 }
21170
21171 free (pnotes);
21172
21173 return res;
21174 }
21175
21176 static bool
21177 process_note_sections (Filedata * filedata)
21178 {
21179 Elf_Internal_Shdr *section;
21180 unsigned long i;
21181 unsigned int n = 0;
21182 bool res = true;
21183
21184 for (i = 0, section = filedata->section_headers;
21185 i < filedata->file_header.e_shnum && section != NULL;
21186 i++, section++)
21187 {
21188 if (section->sh_type == SHT_NOTE)
21189 {
21190 if (! process_notes_at (filedata, section,
21191 (bfd_vma) section->sh_offset,
21192 (bfd_vma) section->sh_size,
21193 (bfd_vma) section->sh_addralign))
21194 res = false;
21195 n++;
21196 }
21197
21198 if (( filedata->file_header.e_machine == EM_V800
21199 || filedata->file_header.e_machine == EM_V850
21200 || filedata->file_header.e_machine == EM_CYGNUS_V850)
21201 && section->sh_type == SHT_RENESAS_INFO)
21202 {
21203 if (! process_v850_notes (filedata,
21204 (bfd_vma) section->sh_offset,
21205 (bfd_vma) section->sh_size))
21206 res = false;
21207 n++;
21208 }
21209 }
21210
21211 if (n == 0)
21212 /* Try processing NOTE segments instead. */
21213 return process_corefile_note_segments (filedata);
21214
21215 return res;
21216 }
21217
21218 static bool
21219 process_notes (Filedata * filedata)
21220 {
21221 /* If we have not been asked to display the notes then do nothing. */
21222 if (! do_notes)
21223 return true;
21224
21225 if (filedata->file_header.e_type != ET_CORE)
21226 return process_note_sections (filedata);
21227
21228 /* No program headers means no NOTE segment. */
21229 if (filedata->file_header.e_phnum > 0)
21230 return process_corefile_note_segments (filedata);
21231
21232 if (filedata->is_separate)
21233 printf (_("No notes found in linked file '%s'.\n"),
21234 filedata->file_name);
21235 else
21236 printf (_("No notes found file.\n"));
21237
21238 return true;
21239 }
21240
21241 static unsigned char *
21242 display_public_gnu_attributes (unsigned char * start,
21243 const unsigned char * const end)
21244 {
21245 printf (_(" Unknown GNU attribute: %s\n"), start);
21246
21247 start += strnlen ((char *) start, end - start);
21248 display_raw_attribute (start, end);
21249
21250 return (unsigned char *) end;
21251 }
21252
21253 static unsigned char *
21254 display_generic_attribute (unsigned char * start,
21255 unsigned int tag,
21256 const unsigned char * const end)
21257 {
21258 if (tag == 0)
21259 return (unsigned char *) end;
21260
21261 return display_tag_value (tag, start, end);
21262 }
21263
21264 static bool
21265 process_arch_specific (Filedata * filedata)
21266 {
21267 if (! do_arch)
21268 return true;
21269
21270 switch (filedata->file_header.e_machine)
21271 {
21272 case EM_ARC:
21273 case EM_ARC_COMPACT:
21274 case EM_ARC_COMPACT2:
21275 return process_attributes (filedata, "ARC", SHT_ARC_ATTRIBUTES,
21276 display_arc_attribute,
21277 display_generic_attribute);
21278 case EM_ARM:
21279 return process_attributes (filedata, "aeabi", SHT_ARM_ATTRIBUTES,
21280 display_arm_attribute,
21281 display_generic_attribute);
21282
21283 case EM_MIPS:
21284 case EM_MIPS_RS3_LE:
21285 return process_mips_specific (filedata);
21286
21287 case EM_MSP430:
21288 return process_attributes (filedata, "mspabi", SHT_MSP430_ATTRIBUTES,
21289 display_msp430_attribute,
21290 display_msp430_gnu_attribute);
21291
21292 case EM_RISCV:
21293 return process_attributes (filedata, "riscv", SHT_RISCV_ATTRIBUTES,
21294 display_riscv_attribute,
21295 display_generic_attribute);
21296
21297 case EM_NDS32:
21298 return process_nds32_specific (filedata);
21299
21300 case EM_68K:
21301 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
21302 display_m68k_gnu_attribute);
21303
21304 case EM_PPC:
21305 case EM_PPC64:
21306 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
21307 display_power_gnu_attribute);
21308
21309 case EM_S390:
21310 case EM_S390_OLD:
21311 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
21312 display_s390_gnu_attribute);
21313
21314 case EM_SPARC:
21315 case EM_SPARC32PLUS:
21316 case EM_SPARCV9:
21317 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
21318 display_sparc_gnu_attribute);
21319
21320 case EM_TI_C6000:
21321 return process_attributes (filedata, "c6xabi", SHT_C6000_ATTRIBUTES,
21322 display_tic6x_attribute,
21323 display_generic_attribute);
21324
21325 case EM_CSKY:
21326 return process_attributes (filedata, "csky", SHT_CSKY_ATTRIBUTES,
21327 display_csky_attribute, NULL);
21328
21329 default:
21330 return process_attributes (filedata, "gnu", SHT_GNU_ATTRIBUTES,
21331 display_public_gnu_attributes,
21332 display_generic_attribute);
21333 }
21334 }
21335
21336 static bool
21337 get_file_header (Filedata * filedata)
21338 {
21339 /* Read in the identity array. */
21340 if (fread (filedata->file_header.e_ident, EI_NIDENT, 1, filedata->handle) != 1)
21341 return false;
21342
21343 /* Determine how to read the rest of the header. */
21344 switch (filedata->file_header.e_ident[EI_DATA])
21345 {
21346 default:
21347 case ELFDATANONE:
21348 case ELFDATA2LSB:
21349 byte_get = byte_get_little_endian;
21350 byte_put = byte_put_little_endian;
21351 break;
21352 case ELFDATA2MSB:
21353 byte_get = byte_get_big_endian;
21354 byte_put = byte_put_big_endian;
21355 break;
21356 }
21357
21358 /* For now we only support 32 bit and 64 bit ELF files. */
21359 is_32bit_elf = (filedata->file_header.e_ident[EI_CLASS] != ELFCLASS64);
21360
21361 /* Read in the rest of the header. */
21362 if (is_32bit_elf)
21363 {
21364 Elf32_External_Ehdr ehdr32;
21365
21366 if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, filedata->handle) != 1)
21367 return false;
21368
21369 filedata->file_header.e_type = BYTE_GET (ehdr32.e_type);
21370 filedata->file_header.e_machine = BYTE_GET (ehdr32.e_machine);
21371 filedata->file_header.e_version = BYTE_GET (ehdr32.e_version);
21372 filedata->file_header.e_entry = BYTE_GET (ehdr32.e_entry);
21373 filedata->file_header.e_phoff = BYTE_GET (ehdr32.e_phoff);
21374 filedata->file_header.e_shoff = BYTE_GET (ehdr32.e_shoff);
21375 filedata->file_header.e_flags = BYTE_GET (ehdr32.e_flags);
21376 filedata->file_header.e_ehsize = BYTE_GET (ehdr32.e_ehsize);
21377 filedata->file_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
21378 filedata->file_header.e_phnum = BYTE_GET (ehdr32.e_phnum);
21379 filedata->file_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
21380 filedata->file_header.e_shnum = BYTE_GET (ehdr32.e_shnum);
21381 filedata->file_header.e_shstrndx = BYTE_GET (ehdr32.e_shstrndx);
21382 }
21383 else
21384 {
21385 Elf64_External_Ehdr ehdr64;
21386
21387 /* If we have been compiled with sizeof (bfd_vma) == 4, then
21388 we will not be able to cope with the 64bit data found in
21389 64 ELF files. Detect this now and abort before we start
21390 overwriting things. */
21391 if (sizeof (bfd_vma) < 8)
21392 {
21393 error (_("This instance of readelf has been built without support for a\n\
21394 64 bit data type and so it cannot read 64 bit ELF files.\n"));
21395 return false;
21396 }
21397
21398 if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, filedata->handle) != 1)
21399 return false;
21400
21401 filedata->file_header.e_type = BYTE_GET (ehdr64.e_type);
21402 filedata->file_header.e_machine = BYTE_GET (ehdr64.e_machine);
21403 filedata->file_header.e_version = BYTE_GET (ehdr64.e_version);
21404 filedata->file_header.e_entry = BYTE_GET (ehdr64.e_entry);
21405 filedata->file_header.e_phoff = BYTE_GET (ehdr64.e_phoff);
21406 filedata->file_header.e_shoff = BYTE_GET (ehdr64.e_shoff);
21407 filedata->file_header.e_flags = BYTE_GET (ehdr64.e_flags);
21408 filedata->file_header.e_ehsize = BYTE_GET (ehdr64.e_ehsize);
21409 filedata->file_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
21410 filedata->file_header.e_phnum = BYTE_GET (ehdr64.e_phnum);
21411 filedata->file_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
21412 filedata->file_header.e_shnum = BYTE_GET (ehdr64.e_shnum);
21413 filedata->file_header.e_shstrndx = BYTE_GET (ehdr64.e_shstrndx);
21414 }
21415
21416 return true;
21417 }
21418
21419 static void
21420 free_filedata (Filedata *filedata)
21421 {
21422 free (filedata->program_interpreter);
21423 free (filedata->program_headers);
21424 free (filedata->section_headers);
21425 free (filedata->string_table);
21426 free (filedata->dump.dump_sects);
21427 free (filedata->dynamic_strings);
21428 free (filedata->dynamic_symbols);
21429 free (filedata->dynamic_syminfo);
21430 free (filedata->dynamic_section);
21431
21432 while (filedata->symtab_shndx_list != NULL)
21433 {
21434 elf_section_list *next = filedata->symtab_shndx_list->next;
21435 free (filedata->symtab_shndx_list);
21436 filedata->symtab_shndx_list = next;
21437 }
21438
21439 free (filedata->section_headers_groups);
21440
21441 if (filedata->section_groups)
21442 {
21443 size_t i;
21444 struct group_list * g;
21445 struct group_list * next;
21446
21447 for (i = 0; i < filedata->group_count; i++)
21448 {
21449 for (g = filedata->section_groups [i].root; g != NULL; g = next)
21450 {
21451 next = g->next;
21452 free (g);
21453 }
21454 }
21455
21456 free (filedata->section_groups);
21457 }
21458 memset (&filedata->section_headers, 0,
21459 sizeof (Filedata) - offsetof (Filedata, section_headers));
21460 }
21461
21462 static void
21463 close_file (Filedata * filedata)
21464 {
21465 if (filedata)
21466 {
21467 if (filedata->handle)
21468 fclose (filedata->handle);
21469 free (filedata);
21470 }
21471 }
21472
21473 void
21474 close_debug_file (void * data)
21475 {
21476 free_filedata ((Filedata *) data);
21477 close_file ((Filedata *) data);
21478 }
21479
21480 static Filedata *
21481 open_file (const char * pathname, bool is_separate)
21482 {
21483 struct stat statbuf;
21484 Filedata * filedata = NULL;
21485
21486 if (stat (pathname, & statbuf) < 0
21487 || ! S_ISREG (statbuf.st_mode))
21488 goto fail;
21489
21490 filedata = calloc (1, sizeof * filedata);
21491 if (filedata == NULL)
21492 goto fail;
21493
21494 filedata->handle = fopen (pathname, "rb");
21495 if (filedata->handle == NULL)
21496 goto fail;
21497
21498 filedata->file_size = (bfd_size_type) statbuf.st_size;
21499 filedata->file_name = pathname;
21500 filedata->is_separate = is_separate;
21501
21502 if (! get_file_header (filedata))
21503 goto fail;
21504
21505 if (!get_section_headers (filedata, false))
21506 goto fail;
21507
21508 return filedata;
21509
21510 fail:
21511 if (filedata)
21512 {
21513 if (filedata->handle)
21514 fclose (filedata->handle);
21515 free (filedata);
21516 }
21517 return NULL;
21518 }
21519
21520 void *
21521 open_debug_file (const char * pathname)
21522 {
21523 return open_file (pathname, true);
21524 }
21525
21526 static void
21527 initialise_dump_sects (Filedata * filedata)
21528 {
21529 /* Initialise the dump_sects array from the cmdline_dump_sects array.
21530 Note we do this even if cmdline_dump_sects is empty because we
21531 must make sure that the dump_sets array is zeroed out before each
21532 object file is processed. */
21533 if (filedata->dump.num_dump_sects > cmdline.num_dump_sects)
21534 memset (filedata->dump.dump_sects, 0,
21535 filedata->dump.num_dump_sects * sizeof (*filedata->dump.dump_sects));
21536
21537 if (cmdline.num_dump_sects > 0)
21538 {
21539 if (filedata->dump.num_dump_sects == 0)
21540 /* A sneaky way of allocating the dump_sects array. */
21541 request_dump_bynumber (&filedata->dump, cmdline.num_dump_sects, 0);
21542
21543 assert (filedata->dump.num_dump_sects >= cmdline.num_dump_sects);
21544 memcpy (filedata->dump.dump_sects, cmdline.dump_sects,
21545 cmdline.num_dump_sects * sizeof (*filedata->dump.dump_sects));
21546 }
21547 }
21548
21549 /* Process one ELF object file according to the command line options.
21550 This file may actually be stored in an archive. The file is
21551 positioned at the start of the ELF object. Returns TRUE if no
21552 problems were encountered, FALSE otherwise. */
21553
21554 static bool
21555 process_object (Filedata * filedata)
21556 {
21557 bool have_separate_files;
21558 unsigned int i;
21559 bool res;
21560
21561 if (! get_file_header (filedata))
21562 {
21563 error (_("%s: Failed to read file header\n"), filedata->file_name);
21564 return false;
21565 }
21566
21567 /* Initialise per file variables. */
21568 for (i = ARRAY_SIZE (filedata->version_info); i--;)
21569 filedata->version_info[i] = 0;
21570
21571 for (i = ARRAY_SIZE (filedata->dynamic_info); i--;)
21572 filedata->dynamic_info[i] = 0;
21573 filedata->dynamic_info_DT_GNU_HASH = 0;
21574 filedata->dynamic_info_DT_MIPS_XHASH = 0;
21575
21576 /* Process the file. */
21577 if (show_name)
21578 printf (_("\nFile: %s\n"), filedata->file_name);
21579
21580 initialise_dump_sects (filedata);
21581
21582 /* There may be some extensions in the first section header. Don't
21583 bomb if we can't read it. */
21584 get_section_headers (filedata, true);
21585
21586 if (! process_file_header (filedata))
21587 {
21588 res = false;
21589 goto out;
21590 }
21591
21592 /* Throw away the single section header read above, so that we
21593 re-read the entire set. */
21594 free (filedata->section_headers);
21595 filedata->section_headers = NULL;
21596
21597 if (! process_section_headers (filedata))
21598 {
21599 /* Without loaded section headers we cannot process lots of things. */
21600 do_unwind = do_version = do_dump = do_arch = false;
21601
21602 if (! do_using_dynamic)
21603 do_syms = do_dyn_syms = do_reloc = false;
21604 }
21605
21606 if (! process_section_groups (filedata))
21607 /* Without loaded section groups we cannot process unwind. */
21608 do_unwind = false;
21609
21610 process_program_headers (filedata);
21611
21612 res = process_dynamic_section (filedata);
21613
21614 if (! process_relocs (filedata))
21615 res = false;
21616
21617 if (! process_unwind (filedata))
21618 res = false;
21619
21620 if (! process_symbol_table (filedata))
21621 res = false;
21622
21623 if (! process_lto_symbol_tables (filedata))
21624 res = false;
21625
21626 if (! process_syminfo (filedata))
21627 res = false;
21628
21629 if (! process_version_sections (filedata))
21630 res = false;
21631
21632 if (filedata->file_header.e_shstrndx != SHN_UNDEF)
21633 have_separate_files = load_separate_debug_files (filedata, filedata->file_name);
21634 else
21635 have_separate_files = false;
21636
21637 if (! process_section_contents (filedata))
21638 res = false;
21639
21640 if (have_separate_files)
21641 {
21642 separate_info * d;
21643
21644 for (d = first_separate_info; d != NULL; d = d->next)
21645 {
21646 initialise_dump_sects (d->handle);
21647
21648 if (process_links && ! process_file_header (d->handle))
21649 res = false;
21650 else if (! process_section_headers (d->handle))
21651 res = false;
21652 else if (! process_section_contents (d->handle))
21653 res = false;
21654 else if (process_links)
21655 {
21656 if (! process_section_groups (d->handle))
21657 res = false;
21658 process_program_headers (d->handle);
21659 if (! process_dynamic_section (d->handle))
21660 res = false;
21661 if (! process_relocs (d->handle))
21662 res = false;
21663 if (! process_unwind (d->handle))
21664 res = false;
21665 if (! process_symbol_table (d->handle))
21666 res = false;
21667 if (! process_lto_symbol_tables (d->handle))
21668 res = false;
21669 if (! process_syminfo (d->handle))
21670 res = false;
21671 if (! process_version_sections (d->handle))
21672 res = false;
21673 if (! process_notes (d->handle))
21674 res = false;
21675 }
21676 }
21677
21678 /* The file handles are closed by the call to free_debug_memory() below. */
21679 }
21680
21681 if (! process_notes (filedata))
21682 res = false;
21683
21684 if (! process_gnu_liblist (filedata))
21685 res = false;
21686
21687 if (! process_arch_specific (filedata))
21688 res = false;
21689
21690 out:
21691 free_filedata (filedata);
21692
21693 free_debug_memory ();
21694
21695 return res;
21696 }
21697
21698 /* Process an ELF archive.
21699 On entry the file is positioned just after the ARMAG string.
21700 Returns TRUE upon success, FALSE otherwise. */
21701
21702 static bool
21703 process_archive (Filedata * filedata, bool is_thin_archive)
21704 {
21705 struct archive_info arch;
21706 struct archive_info nested_arch;
21707 size_t got;
21708 bool ret = true;
21709
21710 show_name = true;
21711
21712 /* The ARCH structure is used to hold information about this archive. */
21713 arch.file_name = NULL;
21714 arch.file = NULL;
21715 arch.index_array = NULL;
21716 arch.sym_table = NULL;
21717 arch.longnames = NULL;
21718
21719 /* The NESTED_ARCH structure is used as a single-item cache of information
21720 about a nested archive (when members of a thin archive reside within
21721 another regular archive file). */
21722 nested_arch.file_name = NULL;
21723 nested_arch.file = NULL;
21724 nested_arch.index_array = NULL;
21725 nested_arch.sym_table = NULL;
21726 nested_arch.longnames = NULL;
21727
21728 if (setup_archive (&arch, filedata->file_name, filedata->handle,
21729 filedata->file_size, is_thin_archive,
21730 do_archive_index) != 0)
21731 {
21732 ret = false;
21733 goto out;
21734 }
21735
21736 if (do_archive_index)
21737 {
21738 if (arch.sym_table == NULL)
21739 error (_("%s: unable to dump the index as none was found\n"),
21740 filedata->file_name);
21741 else
21742 {
21743 unsigned long i, l;
21744 unsigned long current_pos;
21745
21746 printf (_("Index of archive %s: (%lu entries, 0x%lx bytes "
21747 "in the symbol table)\n"),
21748 filedata->file_name, (unsigned long) arch.index_num,
21749 arch.sym_size);
21750
21751 current_pos = ftell (filedata->handle);
21752
21753 for (i = l = 0; i < arch.index_num; i++)
21754 {
21755 if (i == 0
21756 || (i > 0 && arch.index_array[i] != arch.index_array[i - 1]))
21757 {
21758 char * member_name
21759 = get_archive_member_name_at (&arch, arch.index_array[i],
21760 &nested_arch);
21761
21762 if (member_name != NULL)
21763 {
21764 char * qualified_name
21765 = make_qualified_name (&arch, &nested_arch,
21766 member_name);
21767
21768 if (qualified_name != NULL)
21769 {
21770 printf (_("Contents of binary %s at offset "),
21771 qualified_name);
21772 (void) print_vma (arch.index_array[i], PREFIX_HEX);
21773 putchar ('\n');
21774 free (qualified_name);
21775 }
21776 free (member_name);
21777 }
21778 }
21779
21780 if (l >= arch.sym_size)
21781 {
21782 error (_("%s: end of the symbol table reached "
21783 "before the end of the index\n"),
21784 filedata->file_name);
21785 ret = false;
21786 break;
21787 }
21788 /* PR 17531: file: 0b6630b2. */
21789 printf ("\t%.*s\n",
21790 (int) (arch.sym_size - l), arch.sym_table + l);
21791 l += strnlen (arch.sym_table + l, arch.sym_size - l) + 1;
21792 }
21793
21794 if (arch.uses_64bit_indices)
21795 l = (l + 7) & ~ 7;
21796 else
21797 l += l & 1;
21798
21799 if (l < arch.sym_size)
21800 {
21801 error (ngettext ("%s: %ld byte remains in the symbol table, "
21802 "but without corresponding entries in "
21803 "the index table\n",
21804 "%s: %ld bytes remain in the symbol table, "
21805 "but without corresponding entries in "
21806 "the index table\n",
21807 arch.sym_size - l),
21808 filedata->file_name, arch.sym_size - l);
21809 ret = false;
21810 }
21811
21812 if (fseek (filedata->handle, current_pos, SEEK_SET) != 0)
21813 {
21814 error (_("%s: failed to seek back to start of object files "
21815 "in the archive\n"),
21816 filedata->file_name);
21817 ret = false;
21818 goto out;
21819 }
21820 }
21821
21822 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
21823 && !do_segments && !do_header && !do_dump && !do_version
21824 && !do_histogram && !do_debugging && !do_arch && !do_notes
21825 && !do_section_groups && !do_dyn_syms)
21826 {
21827 ret = true; /* Archive index only. */
21828 goto out;
21829 }
21830 }
21831
21832 while (1)
21833 {
21834 char * name;
21835 size_t namelen;
21836 char * qualified_name;
21837
21838 /* Read the next archive header. */
21839 if (fseek (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0)
21840 {
21841 error (_("%s: failed to seek to next archive header\n"),
21842 arch.file_name);
21843 ret = false;
21844 break;
21845 }
21846 got = fread (&arch.arhdr, 1, sizeof arch.arhdr, filedata->handle);
21847 if (got != sizeof arch.arhdr)
21848 {
21849 if (got == 0)
21850 break;
21851 /* PR 24049 - we cannot use filedata->file_name as this will
21852 have already been freed. */
21853 error (_("%s: failed to read archive header\n"), arch.file_name);
21854
21855 ret = false;
21856 break;
21857 }
21858 if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
21859 {
21860 error (_("%s: did not find a valid archive header\n"),
21861 arch.file_name);
21862 ret = false;
21863 break;
21864 }
21865
21866 arch.next_arhdr_offset += sizeof arch.arhdr;
21867
21868 filedata->archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
21869
21870 name = get_archive_member_name (&arch, &nested_arch);
21871 if (name == NULL)
21872 {
21873 error (_("%s: bad archive file name\n"), arch.file_name);
21874 ret = false;
21875 break;
21876 }
21877 namelen = strlen (name);
21878
21879 qualified_name = make_qualified_name (&arch, &nested_arch, name);
21880 if (qualified_name == NULL)
21881 {
21882 error (_("%s: bad archive file name\n"), arch.file_name);
21883 free (name);
21884 ret = false;
21885 break;
21886 }
21887
21888 if (is_thin_archive && arch.nested_member_origin == 0)
21889 {
21890 /* This is a proxy for an external member of a thin archive. */
21891 Filedata * member_filedata;
21892 char * member_file_name = adjust_relative_path
21893 (filedata->file_name, name, namelen);
21894
21895 free (name);
21896 if (member_file_name == NULL)
21897 {
21898 free (qualified_name);
21899 ret = false;
21900 break;
21901 }
21902
21903 member_filedata = open_file (member_file_name, false);
21904 if (member_filedata == NULL)
21905 {
21906 error (_("Input file '%s' is not readable.\n"), member_file_name);
21907 free (member_file_name);
21908 free (qualified_name);
21909 ret = false;
21910 break;
21911 }
21912
21913 filedata->archive_file_offset = arch.nested_member_origin;
21914 member_filedata->file_name = qualified_name;
21915
21916 /* The call to process_object() expects the file to be at the beginning. */
21917 rewind (member_filedata->handle);
21918
21919 if (! process_object (member_filedata))
21920 ret = false;
21921
21922 close_file (member_filedata);
21923 free (member_file_name);
21924 }
21925 else if (is_thin_archive)
21926 {
21927 Filedata thin_filedata;
21928
21929 memset (&thin_filedata, 0, sizeof (thin_filedata));
21930
21931 /* PR 15140: Allow for corrupt thin archives. */
21932 if (nested_arch.file == NULL)
21933 {
21934 error (_("%s: contains corrupt thin archive: %s\n"),
21935 qualified_name, name);
21936 free (qualified_name);
21937 free (name);
21938 ret = false;
21939 break;
21940 }
21941 free (name);
21942
21943 /* This is a proxy for a member of a nested archive. */
21944 filedata->archive_file_offset
21945 = arch.nested_member_origin + sizeof arch.arhdr;
21946
21947 /* The nested archive file will have been opened and setup by
21948 get_archive_member_name. */
21949 if (fseek (nested_arch.file, filedata->archive_file_offset,
21950 SEEK_SET) != 0)
21951 {
21952 error (_("%s: failed to seek to archive member.\n"),
21953 nested_arch.file_name);
21954 free (qualified_name);
21955 ret = false;
21956 break;
21957 }
21958
21959 thin_filedata.handle = nested_arch.file;
21960 thin_filedata.file_name = qualified_name;
21961
21962 if (! process_object (& thin_filedata))
21963 ret = false;
21964 }
21965 else
21966 {
21967 free (name);
21968 filedata->archive_file_offset = arch.next_arhdr_offset;
21969 filedata->file_name = qualified_name;
21970 if (! process_object (filedata))
21971 ret = false;
21972 arch.next_arhdr_offset += (filedata->archive_file_size + 1) & -2;
21973 /* Stop looping with "negative" archive_file_size. */
21974 if (arch.next_arhdr_offset < filedata->archive_file_size)
21975 arch.next_arhdr_offset = -1ul;
21976 }
21977
21978 free (qualified_name);
21979 }
21980
21981 out:
21982 if (nested_arch.file != NULL)
21983 fclose (nested_arch.file);
21984 release_archive (&nested_arch);
21985 release_archive (&arch);
21986
21987 return ret;
21988 }
21989
21990 static bool
21991 process_file (char * file_name)
21992 {
21993 Filedata * filedata = NULL;
21994 struct stat statbuf;
21995 char armag[SARMAG];
21996 bool ret = true;
21997
21998 if (stat (file_name, &statbuf) < 0)
21999 {
22000 if (errno == ENOENT)
22001 error (_("'%s': No such file\n"), file_name);
22002 else
22003 error (_("Could not locate '%s'. System error message: %s\n"),
22004 file_name, strerror (errno));
22005 return false;
22006 }
22007
22008 if (! S_ISREG (statbuf.st_mode))
22009 {
22010 error (_("'%s' is not an ordinary file\n"), file_name);
22011 return false;
22012 }
22013
22014 filedata = calloc (1, sizeof * filedata);
22015 if (filedata == NULL)
22016 {
22017 error (_("Out of memory allocating file data structure\n"));
22018 return false;
22019 }
22020
22021 filedata->file_name = file_name;
22022 filedata->handle = fopen (file_name, "rb");
22023 if (filedata->handle == NULL)
22024 {
22025 error (_("Input file '%s' is not readable.\n"), file_name);
22026 free (filedata);
22027 return false;
22028 }
22029
22030 if (fread (armag, SARMAG, 1, filedata->handle) != 1)
22031 {
22032 error (_("%s: Failed to read file's magic number\n"), file_name);
22033 fclose (filedata->handle);
22034 free (filedata);
22035 return false;
22036 }
22037
22038 filedata->file_size = (bfd_size_type) statbuf.st_size;
22039 filedata->is_separate = false;
22040
22041 if (memcmp (armag, ARMAG, SARMAG) == 0)
22042 {
22043 if (! process_archive (filedata, false))
22044 ret = false;
22045 }
22046 else if (memcmp (armag, ARMAGT, SARMAG) == 0)
22047 {
22048 if ( ! process_archive (filedata, true))
22049 ret = false;
22050 }
22051 else
22052 {
22053 if (do_archive_index && !check_all)
22054 error (_("File %s is not an archive so its index cannot be displayed.\n"),
22055 file_name);
22056
22057 rewind (filedata->handle);
22058 filedata->archive_file_size = filedata->archive_file_offset = 0;
22059
22060 if (! process_object (filedata))
22061 ret = false;
22062 }
22063
22064 fclose (filedata->handle);
22065 free (filedata->section_headers);
22066 free (filedata->program_headers);
22067 free (filedata->string_table);
22068 free (filedata->dump.dump_sects);
22069 free (filedata);
22070
22071 free (ba_cache.strtab);
22072 ba_cache.strtab = NULL;
22073 free (ba_cache.symtab);
22074 ba_cache.symtab = NULL;
22075 ba_cache.filedata = NULL;
22076
22077 return ret;
22078 }
22079
22080 #ifdef SUPPORT_DISASSEMBLY
22081 /* Needed by the i386 disassembler. For extra credit, someone could
22082 fix this so that we insert symbolic addresses here, esp for GOT/PLT
22083 symbols. */
22084
22085 void
22086 print_address (unsigned int addr, FILE * outfile)
22087 {
22088 fprintf (outfile,"0x%8.8x", addr);
22089 }
22090
22091 /* Needed by the i386 disassembler. */
22092
22093 void
22094 db_task_printsym (unsigned int addr)
22095 {
22096 print_address (addr, stderr);
22097 }
22098 #endif
22099
22100 int
22101 main (int argc, char ** argv)
22102 {
22103 int err;
22104
22105 #ifdef HAVE_LC_MESSAGES
22106 setlocale (LC_MESSAGES, "");
22107 #endif
22108 setlocale (LC_CTYPE, "");
22109 bindtextdomain (PACKAGE, LOCALEDIR);
22110 textdomain (PACKAGE);
22111
22112 expandargv (&argc, &argv);
22113
22114 parse_args (& cmdline, argc, argv);
22115
22116 if (optind < (argc - 1))
22117 /* When displaying information for more than one file,
22118 prefix the information with the file name. */
22119 show_name = true;
22120 else if (optind >= argc)
22121 {
22122 /* Ensure that the warning is always displayed. */
22123 do_checks = true;
22124
22125 warn (_("Nothing to do.\n"));
22126 usage (stderr);
22127 }
22128
22129 err = false;
22130 while (optind < argc)
22131 if (! process_file (argv[optind++]))
22132 err = true;
22133
22134 free (cmdline.dump_sects);
22135
22136 free (dump_ctf_symtab_name);
22137 free (dump_ctf_strtab_name);
22138 free (dump_ctf_parent_name);
22139
22140 return err ? EXIT_FAILURE : EXIT_SUCCESS;
22141 }