readelf: don't clear section_headers in process_file_header
[deliverable/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
166 #include "getopt.h"
167 #include "libiberty.h"
168 #include "safe-ctype.h"
169 #include "filenames.h"
170
171 #ifndef offsetof
172 #define offsetof(TYPE, MEMBER) ((size_t) &(((TYPE *) 0)->MEMBER))
173 #endif
174
175 typedef struct elf_section_list
176 {
177 Elf_Internal_Shdr * hdr;
178 struct elf_section_list * next;
179 } elf_section_list;
180
181 /* Flag bits indicating particular types of dump. */
182 #define HEX_DUMP (1 << 0) /* The -x command line switch. */
183 #define DISASS_DUMP (1 << 1) /* The -i command line switch. */
184 #define DEBUG_DUMP (1 << 2) /* The -w command line switch. */
185 #define STRING_DUMP (1 << 3) /* The -p command line switch. */
186 #define RELOC_DUMP (1 << 4) /* The -R command line switch. */
187 #define CTF_DUMP (1 << 5) /* The --ctf command line switch. */
188
189 typedef unsigned char dump_type;
190
191 /* A linked list of the section names for which dumps were requested. */
192 struct dump_list_entry
193 {
194 char * name;
195 dump_type type;
196 struct dump_list_entry * next;
197 };
198
199 /* A dynamic array of flags indicating for which sections a dump
200 has been requested via command line switches. */
201 struct dump_data
202 {
203 dump_type * dump_sects;
204 unsigned int num_dump_sects;
205 };
206
207 static struct dump_data cmdline;
208
209 static struct dump_list_entry * dump_sects_byname;
210
211 char * program_name = "readelf";
212
213 static bool show_name = false;
214 static bool do_dynamic = false;
215 static bool do_syms = false;
216 static bool do_dyn_syms = false;
217 static bool do_lto_syms = false;
218 static bool do_reloc = false;
219 static bool do_sections = false;
220 static bool do_section_groups = false;
221 static bool do_section_details = false;
222 static bool do_segments = false;
223 static bool do_unwind = false;
224 static bool do_using_dynamic = false;
225 static bool do_header = false;
226 static bool do_dump = false;
227 static bool do_version = false;
228 static bool do_histogram = false;
229 static bool do_debugging = false;
230 static bool do_ctf = false;
231 static bool do_arch = false;
232 static bool do_notes = false;
233 static bool do_archive_index = false;
234 static bool check_all = false;
235 static bool is_32bit_elf = false;
236 static bool decompress_dumps = false;
237 static bool do_not_show_symbol_truncation = false;
238 static bool do_demangle = false; /* Pretty print C++ symbol names. */
239 static bool process_links = false;
240 static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
241 static int sym_base = 0;
242
243 static char *dump_ctf_parent_name;
244 static char *dump_ctf_symtab_name;
245 static char *dump_ctf_strtab_name;
246
247 struct group_list
248 {
249 struct group_list * next;
250 unsigned int section_index;
251 };
252
253 struct group
254 {
255 struct group_list * root;
256 unsigned int group_index;
257 };
258
259 typedef struct filedata
260 {
261 const char * file_name;
262 bool is_separate;
263 FILE * handle;
264 bfd_size_type file_size;
265 Elf_Internal_Ehdr file_header;
266 unsigned long archive_file_offset;
267 unsigned long archive_file_size;
268 /* Everything below this point is cleared out by free_filedata. */
269 Elf_Internal_Shdr * section_headers;
270 Elf_Internal_Phdr * program_headers;
271 char * string_table;
272 unsigned long string_table_length;
273 unsigned long dynamic_addr;
274 bfd_size_type dynamic_size;
275 size_t dynamic_nent;
276 Elf_Internal_Dyn * dynamic_section;
277 Elf_Internal_Shdr * dynamic_strtab_section;
278 char * dynamic_strings;
279 unsigned long dynamic_strings_length;
280 Elf_Internal_Shdr * dynamic_symtab_section;
281 unsigned long num_dynamic_syms;
282 Elf_Internal_Sym * dynamic_symbols;
283 bfd_vma version_info[16];
284 unsigned int dynamic_syminfo_nent;
285 Elf_Internal_Syminfo * dynamic_syminfo;
286 unsigned long dynamic_syminfo_offset;
287 bfd_size_type nbuckets;
288 bfd_size_type nchains;
289 bfd_vma * buckets;
290 bfd_vma * chains;
291 bfd_size_type ngnubuckets;
292 bfd_size_type ngnuchains;
293 bfd_vma * gnubuckets;
294 bfd_vma * gnuchains;
295 bfd_vma * mipsxlat;
296 bfd_vma gnusymidx;
297 char * program_interpreter;
298 bfd_vma dynamic_info[DT_ENCODING];
299 bfd_vma dynamic_info_DT_GNU_HASH;
300 bfd_vma dynamic_info_DT_MIPS_XHASH;
301 elf_section_list * symtab_shndx_list;
302 size_t group_count;
303 struct group * section_groups;
304 struct group ** section_headers_groups;
305 /* A dynamic array of flags indicating for which sections a dump of
306 some kind has been requested. It is reset on a per-object file
307 basis and then initialised from the cmdline_dump_sects array,
308 the results of interpreting the -w switch, and the
309 dump_sects_byname list. */
310 struct dump_data dump;
311 } Filedata;
312
313 /* How to print a vma value. */
314 typedef enum print_mode
315 {
316 HEX,
317 HEX_5,
318 DEC,
319 DEC_5,
320 UNSIGNED,
321 UNSIGNED_5,
322 PREFIX_HEX,
323 PREFIX_HEX_5,
324 FULL_HEX,
325 LONG_HEX,
326 OCTAL,
327 OCTAL_5
328 }
329 print_mode;
330
331 /* Versioned symbol info. */
332 enum versioned_symbol_info
333 {
334 symbol_undefined,
335 symbol_hidden,
336 symbol_public
337 };
338
339 static const char * get_symbol_version_string
340 (Filedata *, bool, const char *, unsigned long, unsigned,
341 Elf_Internal_Sym *, enum versioned_symbol_info *, unsigned short *);
342
343 #define UNKNOWN -1
344
345 #define SECTION_NAME(X) \
346 (filedata->string_table + (X)->sh_name)
347
348 #define SECTION_NAME_VALID(X) \
349 ((X) != NULL \
350 && filedata->string_table != NULL \
351 && (X)->sh_name < filedata->string_table_length)
352
353 #define SECTION_NAME_PRINT(X) \
354 ((X) == NULL ? _("<none>") \
355 : filedata->string_table == NULL ? _("<no-strings>") \
356 : (X)->sh_name >= filedata->string_table_length ? _("<corrupt>") \
357 : filedata->string_table + (X)->sh_name)
358
359 #define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */
360
361 #define VALID_SYMBOL_NAME(strtab, strtab_size, offset) \
362 (strtab != NULL && offset < strtab_size)
363 #define VALID_DYNAMIC_NAME(filedata, offset) \
364 VALID_SYMBOL_NAME (filedata->dynamic_strings, \
365 filedata->dynamic_strings_length, offset)
366 /* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
367 already been called and verified that the string exists. */
368 #define GET_DYNAMIC_NAME(filedata, offset) \
369 (filedata->dynamic_strings + offset)
370
371 #define REMOVE_ARCH_BITS(ADDR) \
372 do \
373 { \
374 if (filedata->file_header.e_machine == EM_ARM) \
375 (ADDR) &= ~1; \
376 } \
377 while (0)
378
379 /* Get the correct GNU hash section name. */
380 #define GNU_HASH_SECTION_NAME(filedata) \
381 filedata->dynamic_info_DT_MIPS_XHASH ? ".MIPS.xhash" : ".gnu.hash"
382 \f
383 /* Print a BFD_VMA to an internal buffer, for use in error messages.
384 BFD_FMA_FMT can't be used in translated strings. */
385
386 static const char *
387 bfd_vmatoa (char *fmtch, bfd_vma value)
388 {
389 /* bfd_vmatoa is used more then once in a printf call for output.
390 Cycle through an array of buffers. */
391 static int buf_pos = 0;
392 static struct bfd_vmatoa_buf
393 {
394 char place[64];
395 } buf[4];
396 char *ret;
397 char fmt[32];
398
399 ret = buf[buf_pos++].place;
400 buf_pos %= ARRAY_SIZE (buf);
401
402 sprintf (fmt, "%%%s%s", BFD_VMA_FMT, fmtch);
403 snprintf (ret, sizeof (buf[0].place), fmt, value);
404 return ret;
405 }
406
407 /* Retrieve NMEMB structures, each SIZE bytes long from FILEDATA starting at
408 OFFSET + the offset of the current archive member, if we are examining an
409 archive. Put the retrieved data into VAR, if it is not NULL. Otherwise
410 allocate a buffer using malloc and fill that. In either case return the
411 pointer to the start of the retrieved data or NULL if something went wrong.
412 If something does go wrong and REASON is not NULL then emit an error
413 message using REASON as part of the context. */
414
415 static void *
416 get_data (void * var,
417 Filedata * filedata,
418 unsigned long offset,
419 bfd_size_type size,
420 bfd_size_type nmemb,
421 const char * reason)
422 {
423 void * mvar;
424 bfd_size_type amt = size * nmemb;
425
426 if (size == 0 || nmemb == 0)
427 return NULL;
428
429 /* If the size_t type is smaller than the bfd_size_type, eg because
430 you are building a 32-bit tool on a 64-bit host, then make sure
431 that when the sizes are cast to (size_t) no information is lost. */
432 if ((size_t) size != size
433 || (size_t) nmemb != nmemb
434 || (size_t) amt != amt)
435 {
436 if (reason)
437 error (_("Size truncation prevents reading %s"
438 " elements of size %s for %s\n"),
439 bfd_vmatoa ("u", nmemb), bfd_vmatoa ("u", size), reason);
440 return NULL;
441 }
442
443 /* Check for size overflow. */
444 if (amt / size != nmemb || (size_t) amt + 1 == 0)
445 {
446 if (reason)
447 error (_("Size overflow prevents reading %s"
448 " elements of size %s for %s\n"),
449 bfd_vmatoa ("u", nmemb), bfd_vmatoa ("u", size), reason);
450 return NULL;
451 }
452
453 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
454 attempting to allocate memory when the read is bound to fail. */
455 if (filedata->archive_file_offset > filedata->file_size
456 || offset > filedata->file_size - filedata->archive_file_offset
457 || amt > filedata->file_size - filedata->archive_file_offset - offset)
458 {
459 if (reason)
460 error (_("Reading %s bytes extends past end of file for %s\n"),
461 bfd_vmatoa ("u", amt), reason);
462 return NULL;
463 }
464
465 if (fseek (filedata->handle, filedata->archive_file_offset + offset,
466 SEEK_SET))
467 {
468 if (reason)
469 error (_("Unable to seek to 0x%lx for %s\n"),
470 filedata->archive_file_offset + offset, reason);
471 return NULL;
472 }
473
474 mvar = var;
475 if (mvar == NULL)
476 {
477 /* + 1 so that we can '\0' terminate invalid string table sections. */
478 mvar = malloc ((size_t) amt + 1);
479
480 if (mvar == NULL)
481 {
482 if (reason)
483 error (_("Out of memory allocating %s bytes for %s\n"),
484 bfd_vmatoa ("u", amt), reason);
485 return NULL;
486 }
487
488 ((char *) mvar)[amt] = '\0';
489 }
490
491 if (fread (mvar, (size_t) size, (size_t) nmemb, filedata->handle) != nmemb)
492 {
493 if (reason)
494 error (_("Unable to read in %s bytes of %s\n"),
495 bfd_vmatoa ("u", amt), reason);
496 if (mvar != var)
497 free (mvar);
498 return NULL;
499 }
500
501 return mvar;
502 }
503
504 /* Print a VMA value in the MODE specified.
505 Returns the number of characters displayed. */
506
507 static unsigned int
508 print_vma (bfd_vma vma, print_mode mode)
509 {
510 unsigned int nc = 0;
511
512 switch (mode)
513 {
514 case FULL_HEX:
515 nc = printf ("0x");
516 /* Fall through. */
517 case LONG_HEX:
518 #ifdef BFD64
519 if (is_32bit_elf)
520 return nc + printf ("%8.8" BFD_VMA_FMT "x", vma);
521 #endif
522 printf_vma (vma);
523 return nc + 16;
524
525 case DEC_5:
526 if (vma <= 99999)
527 return printf ("%5" BFD_VMA_FMT "d", vma);
528 /* Fall through. */
529 case PREFIX_HEX:
530 nc = printf ("0x");
531 /* Fall through. */
532 case HEX:
533 return nc + printf ("%" BFD_VMA_FMT "x", vma);
534
535 case PREFIX_HEX_5:
536 nc = printf ("0x");
537 /* Fall through. */
538 case HEX_5:
539 return nc + printf ("%05" BFD_VMA_FMT "x", vma);
540
541 case DEC:
542 return printf ("%" BFD_VMA_FMT "d", vma);
543
544 case UNSIGNED:
545 return printf ("%" BFD_VMA_FMT "u", vma);
546
547 case UNSIGNED_5:
548 return printf ("%5" BFD_VMA_FMT "u", vma);
549
550 case OCTAL:
551 return printf ("%" BFD_VMA_FMT "o", vma);
552
553 case OCTAL_5:
554 return printf ("%5" BFD_VMA_FMT "o", vma);
555
556 default:
557 /* FIXME: Report unrecognised mode ? */
558 return 0;
559 }
560 }
561
562
563 /* Display a symbol on stdout. Handles the display of control characters and
564 multibye characters (assuming the host environment supports them).
565
566 Display at most abs(WIDTH) characters, truncating as necessary, unless do_wide is true.
567
568 If truncation will happen and do_not_show_symbol_truncation is FALSE then display
569 abs(WIDTH) - 5 characters followed by "[...]".
570
571 If WIDTH is negative then ensure that the output is at least (- WIDTH) characters,
572 padding as necessary.
573
574 Returns the number of emitted characters. */
575
576 static unsigned int
577 print_symbol (signed int width, const char * symbol)
578 {
579 bool extra_padding = false;
580 bool do_dots = false;
581 signed int num_printed = 0;
582 #ifdef HAVE_MBSTATE_T
583 mbstate_t state;
584 #endif
585 unsigned int width_remaining;
586 const void * alloced_symbol = NULL;
587
588 if (width < 0)
589 {
590 /* Keep the width positive. This helps the code below. */
591 width = - width;
592 extra_padding = true;
593 }
594 else if (width == 0)
595 return 0;
596
597 if (do_wide)
598 /* Set the remaining width to a very large value.
599 This simplifies the code below. */
600 width_remaining = INT_MAX;
601 else
602 {
603 width_remaining = width;
604 if (! do_not_show_symbol_truncation
605 && (int) strlen (symbol) > width)
606 {
607 width_remaining -= 5;
608 if ((int) width_remaining < 0)
609 width_remaining = 0;
610 do_dots = true;
611 }
612 }
613
614 #ifdef HAVE_MBSTATE_T
615 /* Initialise the multibyte conversion state. */
616 memset (& state, 0, sizeof (state));
617 #endif
618
619 if (do_demangle && *symbol)
620 {
621 const char * res = cplus_demangle (symbol, demangle_flags);
622
623 if (res != NULL)
624 alloced_symbol = symbol = res;
625 }
626
627 while (width_remaining)
628 {
629 size_t n;
630 const char c = *symbol++;
631
632 if (c == 0)
633 break;
634
635 /* Do not print control characters directly as they can affect terminal
636 settings. Such characters usually appear in the names generated
637 by the assembler for local labels. */
638 if (ISCNTRL (c))
639 {
640 if (width_remaining < 2)
641 break;
642
643 printf ("^%c", c + 0x40);
644 width_remaining -= 2;
645 num_printed += 2;
646 }
647 else if (ISPRINT (c))
648 {
649 putchar (c);
650 width_remaining --;
651 num_printed ++;
652 }
653 else
654 {
655 #ifdef HAVE_MBSTATE_T
656 wchar_t w;
657 #endif
658 /* Let printf do the hard work of displaying multibyte characters. */
659 printf ("%.1s", symbol - 1);
660 width_remaining --;
661 num_printed ++;
662
663 #ifdef HAVE_MBSTATE_T
664 /* Try to find out how many bytes made up the character that was
665 just printed. Advance the symbol pointer past the bytes that
666 were displayed. */
667 n = mbrtowc (& w, symbol - 1, MB_CUR_MAX, & state);
668 #else
669 n = 1;
670 #endif
671 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
672 symbol += (n - 1);
673 }
674 }
675
676 if (do_dots)
677 num_printed += printf ("[...]");
678
679 if (extra_padding && num_printed < width)
680 {
681 /* Fill in the remaining spaces. */
682 printf ("%-*s", width - num_printed, " ");
683 num_printed = width;
684 }
685
686 free ((void *) alloced_symbol);
687 return num_printed;
688 }
689
690 /* Returns a pointer to a static buffer containing a printable version of
691 the given section's name. Like print_symbol, except that it does not try
692 to print multibyte characters, it just interprets them as hex values. */
693
694 static const char *
695 printable_section_name (Filedata * filedata, const Elf_Internal_Shdr * sec)
696 {
697 #define MAX_PRINT_SEC_NAME_LEN 256
698 static char sec_name_buf [MAX_PRINT_SEC_NAME_LEN + 1];
699 const char * name = SECTION_NAME_PRINT (sec);
700 char * buf = sec_name_buf;
701 char c;
702 unsigned int remaining = MAX_PRINT_SEC_NAME_LEN;
703
704 while ((c = * name ++) != 0)
705 {
706 if (ISCNTRL (c))
707 {
708 if (remaining < 2)
709 break;
710
711 * buf ++ = '^';
712 * buf ++ = c + 0x40;
713 remaining -= 2;
714 }
715 else if (ISPRINT (c))
716 {
717 * buf ++ = c;
718 remaining -= 1;
719 }
720 else
721 {
722 static char hex[17] = "0123456789ABCDEF";
723
724 if (remaining < 4)
725 break;
726 * buf ++ = '<';
727 * buf ++ = hex[(c & 0xf0) >> 4];
728 * buf ++ = hex[c & 0x0f];
729 * buf ++ = '>';
730 remaining -= 4;
731 }
732
733 if (remaining == 0)
734 break;
735 }
736
737 * buf = 0;
738 return sec_name_buf;
739 }
740
741 static const char *
742 printable_section_name_from_index (Filedata * filedata, unsigned long ndx)
743 {
744 if (ndx >= filedata->file_header.e_shnum)
745 return _("<corrupt>");
746
747 return printable_section_name (filedata, filedata->section_headers + ndx);
748 }
749
750 /* Return a pointer to section NAME, or NULL if no such section exists. */
751
752 static Elf_Internal_Shdr *
753 find_section (Filedata * filedata, const char * name)
754 {
755 unsigned int i;
756
757 if (filedata->section_headers == NULL)
758 return NULL;
759
760 for (i = 0; i < filedata->file_header.e_shnum; i++)
761 if (SECTION_NAME_VALID (filedata->section_headers + i)
762 && streq (SECTION_NAME (filedata->section_headers + i), name))
763 return filedata->section_headers + i;
764
765 return NULL;
766 }
767
768 /* Return a pointer to a section containing ADDR, or NULL if no such
769 section exists. */
770
771 static Elf_Internal_Shdr *
772 find_section_by_address (Filedata * filedata, bfd_vma addr)
773 {
774 unsigned int i;
775
776 if (filedata->section_headers == NULL)
777 return NULL;
778
779 for (i = 0; i < filedata->file_header.e_shnum; i++)
780 {
781 Elf_Internal_Shdr *sec = filedata->section_headers + i;
782
783 if (addr >= sec->sh_addr && addr < sec->sh_addr + sec->sh_size)
784 return sec;
785 }
786
787 return NULL;
788 }
789
790 static Elf_Internal_Shdr *
791 find_section_by_type (Filedata * filedata, unsigned int type)
792 {
793 unsigned int i;
794
795 if (filedata->section_headers == NULL)
796 return NULL;
797
798 for (i = 0; i < filedata->file_header.e_shnum; i++)
799 {
800 Elf_Internal_Shdr *sec = filedata->section_headers + i;
801
802 if (sec->sh_type == type)
803 return sec;
804 }
805
806 return NULL;
807 }
808
809 /* Return a pointer to section NAME, or NULL if no such section exists,
810 restricted to the list of sections given in SET. */
811
812 static Elf_Internal_Shdr *
813 find_section_in_set (Filedata * filedata, const char * name, unsigned int * set)
814 {
815 unsigned int i;
816
817 if (filedata->section_headers == NULL)
818 return NULL;
819
820 if (set != NULL)
821 {
822 while ((i = *set++) > 0)
823 {
824 /* See PR 21156 for a reproducer. */
825 if (i >= filedata->file_header.e_shnum)
826 continue; /* FIXME: Should we issue an error message ? */
827
828 if (SECTION_NAME_VALID (filedata->section_headers + i)
829 && streq (SECTION_NAME (filedata->section_headers + i), name))
830 return filedata->section_headers + i;
831 }
832 }
833
834 return find_section (filedata, name);
835 }
836
837 /* Return TRUE if the current file is for IA-64 machine and OpenVMS ABI.
838 This OS has so many departures from the ELF standard that we test it at
839 many places. */
840
841 static inline bool
842 is_ia64_vms (Filedata * filedata)
843 {
844 return filedata->file_header.e_machine == EM_IA_64
845 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS;
846 }
847
848 /* Guess the relocation size commonly used by the specific machines. */
849
850 static bool
851 guess_is_rela (unsigned int e_machine)
852 {
853 switch (e_machine)
854 {
855 /* Targets that use REL relocations. */
856 case EM_386:
857 case EM_IAMCU:
858 case EM_960:
859 case EM_ARM:
860 case EM_D10V:
861 case EM_CYGNUS_D10V:
862 case EM_DLX:
863 case EM_MIPS:
864 case EM_MIPS_RS3_LE:
865 case EM_CYGNUS_M32R:
866 case EM_SCORE:
867 case EM_XGATE:
868 case EM_NFP:
869 case EM_BPF:
870 return false;
871
872 /* Targets that use RELA relocations. */
873 case EM_68K:
874 case EM_860:
875 case EM_AARCH64:
876 case EM_ADAPTEVA_EPIPHANY:
877 case EM_ALPHA:
878 case EM_ALTERA_NIOS2:
879 case EM_ARC:
880 case EM_ARC_COMPACT:
881 case EM_ARC_COMPACT2:
882 case EM_AVR:
883 case EM_AVR_OLD:
884 case EM_BLACKFIN:
885 case EM_CR16:
886 case EM_CRIS:
887 case EM_CRX:
888 case EM_CSKY:
889 case EM_D30V:
890 case EM_CYGNUS_D30V:
891 case EM_FR30:
892 case EM_FT32:
893 case EM_CYGNUS_FR30:
894 case EM_CYGNUS_FRV:
895 case EM_H8S:
896 case EM_H8_300:
897 case EM_H8_300H:
898 case EM_IA_64:
899 case EM_IP2K:
900 case EM_IP2K_OLD:
901 case EM_IQ2000:
902 case EM_LATTICEMICO32:
903 case EM_M32C_OLD:
904 case EM_M32C:
905 case EM_M32R:
906 case EM_MCORE:
907 case EM_CYGNUS_MEP:
908 case EM_METAG:
909 case EM_MMIX:
910 case EM_MN10200:
911 case EM_CYGNUS_MN10200:
912 case EM_MN10300:
913 case EM_CYGNUS_MN10300:
914 case EM_MOXIE:
915 case EM_MSP430:
916 case EM_MSP430_OLD:
917 case EM_MT:
918 case EM_NDS32:
919 case EM_NIOS32:
920 case EM_OR1K:
921 case EM_PPC64:
922 case EM_PPC:
923 case EM_TI_PRU:
924 case EM_RISCV:
925 case EM_RL78:
926 case EM_RX:
927 case EM_S390:
928 case EM_S390_OLD:
929 case EM_SH:
930 case EM_SPARC:
931 case EM_SPARC32PLUS:
932 case EM_SPARCV9:
933 case EM_SPU:
934 case EM_TI_C6000:
935 case EM_TILEGX:
936 case EM_TILEPRO:
937 case EM_V800:
938 case EM_V850:
939 case EM_CYGNUS_V850:
940 case EM_VAX:
941 case EM_VISIUM:
942 case EM_X86_64:
943 case EM_L1OM:
944 case EM_K1OM:
945 case EM_XSTORMY16:
946 case EM_XTENSA:
947 case EM_XTENSA_OLD:
948 case EM_MICROBLAZE:
949 case EM_MICROBLAZE_OLD:
950 case EM_WEBASSEMBLY:
951 return true;
952
953 case EM_68HC05:
954 case EM_68HC08:
955 case EM_68HC11:
956 case EM_68HC16:
957 case EM_FX66:
958 case EM_ME16:
959 case EM_MMA:
960 case EM_NCPU:
961 case EM_NDR1:
962 case EM_PCP:
963 case EM_ST100:
964 case EM_ST19:
965 case EM_ST7:
966 case EM_ST9PLUS:
967 case EM_STARCORE:
968 case EM_SVX:
969 case EM_TINYJ:
970 default:
971 warn (_("Don't know about relocations on this machine architecture\n"));
972 return false;
973 }
974 }
975
976 /* Load RELA type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
977 Returns TRUE upon success, FALSE otherwise. If successful then a
978 pointer to a malloc'ed buffer containing the relocs is placed in *RELASP,
979 and the number of relocs loaded is placed in *NRELASP. It is the caller's
980 responsibility to free the allocated buffer. */
981
982 static bool
983 slurp_rela_relocs (Filedata * filedata,
984 unsigned long rel_offset,
985 unsigned long rel_size,
986 Elf_Internal_Rela ** relasp,
987 unsigned long * nrelasp)
988 {
989 Elf_Internal_Rela * relas;
990 size_t nrelas;
991 unsigned int i;
992
993 if (is_32bit_elf)
994 {
995 Elf32_External_Rela * erelas;
996
997 erelas = (Elf32_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
998 rel_size, _("32-bit relocation data"));
999 if (!erelas)
1000 return false;
1001
1002 nrelas = rel_size / sizeof (Elf32_External_Rela);
1003
1004 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1005 sizeof (Elf_Internal_Rela));
1006
1007 if (relas == NULL)
1008 {
1009 free (erelas);
1010 error (_("out of memory parsing relocs\n"));
1011 return false;
1012 }
1013
1014 for (i = 0; i < nrelas; i++)
1015 {
1016 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1017 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1018 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1019 }
1020
1021 free (erelas);
1022 }
1023 else
1024 {
1025 Elf64_External_Rela * erelas;
1026
1027 erelas = (Elf64_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1028 rel_size, _("64-bit relocation data"));
1029 if (!erelas)
1030 return false;
1031
1032 nrelas = rel_size / sizeof (Elf64_External_Rela);
1033
1034 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1035 sizeof (Elf_Internal_Rela));
1036
1037 if (relas == NULL)
1038 {
1039 free (erelas);
1040 error (_("out of memory parsing relocs\n"));
1041 return false;
1042 }
1043
1044 for (i = 0; i < nrelas; i++)
1045 {
1046 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1047 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1048 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1049
1050 /* The #ifdef BFD64 below is to prevent a compile time
1051 warning. We know that if we do not have a 64 bit data
1052 type that we will never execute this code anyway. */
1053 #ifdef BFD64
1054 if (filedata->file_header.e_machine == EM_MIPS
1055 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1056 {
1057 /* In little-endian objects, r_info isn't really a
1058 64-bit little-endian value: it has a 32-bit
1059 little-endian symbol index followed by four
1060 individual byte fields. Reorder INFO
1061 accordingly. */
1062 bfd_vma inf = relas[i].r_info;
1063 inf = (((inf & 0xffffffff) << 32)
1064 | ((inf >> 56) & 0xff)
1065 | ((inf >> 40) & 0xff00)
1066 | ((inf >> 24) & 0xff0000)
1067 | ((inf >> 8) & 0xff000000));
1068 relas[i].r_info = inf;
1069 }
1070 #endif /* BFD64 */
1071 }
1072
1073 free (erelas);
1074 }
1075
1076 *relasp = relas;
1077 *nrelasp = nrelas;
1078 return true;
1079 }
1080
1081 /* Load REL type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1082 Returns TRUE upon success, FALSE otherwise. If successful then a
1083 pointer to a malloc'ed buffer containing the relocs is placed in *RELSP,
1084 and the number of relocs loaded is placed in *NRELSP. It is the caller's
1085 responsibility to free the allocated buffer. */
1086
1087 static bool
1088 slurp_rel_relocs (Filedata * filedata,
1089 unsigned long rel_offset,
1090 unsigned long rel_size,
1091 Elf_Internal_Rela ** relsp,
1092 unsigned long * nrelsp)
1093 {
1094 Elf_Internal_Rela * rels;
1095 size_t nrels;
1096 unsigned int i;
1097
1098 if (is_32bit_elf)
1099 {
1100 Elf32_External_Rel * erels;
1101
1102 erels = (Elf32_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1103 rel_size, _("32-bit relocation data"));
1104 if (!erels)
1105 return false;
1106
1107 nrels = rel_size / sizeof (Elf32_External_Rel);
1108
1109 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1110
1111 if (rels == NULL)
1112 {
1113 free (erels);
1114 error (_("out of memory parsing relocs\n"));
1115 return false;
1116 }
1117
1118 for (i = 0; i < nrels; i++)
1119 {
1120 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1121 rels[i].r_info = BYTE_GET (erels[i].r_info);
1122 rels[i].r_addend = 0;
1123 }
1124
1125 free (erels);
1126 }
1127 else
1128 {
1129 Elf64_External_Rel * erels;
1130
1131 erels = (Elf64_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1132 rel_size, _("64-bit relocation data"));
1133 if (!erels)
1134 return false;
1135
1136 nrels = rel_size / sizeof (Elf64_External_Rel);
1137
1138 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1139
1140 if (rels == NULL)
1141 {
1142 free (erels);
1143 error (_("out of memory parsing relocs\n"));
1144 return false;
1145 }
1146
1147 for (i = 0; i < nrels; i++)
1148 {
1149 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1150 rels[i].r_info = BYTE_GET (erels[i].r_info);
1151 rels[i].r_addend = 0;
1152
1153 /* The #ifdef BFD64 below is to prevent a compile time
1154 warning. We know that if we do not have a 64 bit data
1155 type that we will never execute this code anyway. */
1156 #ifdef BFD64
1157 if (filedata->file_header.e_machine == EM_MIPS
1158 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1159 {
1160 /* In little-endian objects, r_info isn't really a
1161 64-bit little-endian value: it has a 32-bit
1162 little-endian symbol index followed by four
1163 individual byte fields. Reorder INFO
1164 accordingly. */
1165 bfd_vma inf = rels[i].r_info;
1166 inf = (((inf & 0xffffffff) << 32)
1167 | ((inf >> 56) & 0xff)
1168 | ((inf >> 40) & 0xff00)
1169 | ((inf >> 24) & 0xff0000)
1170 | ((inf >> 8) & 0xff000000));
1171 rels[i].r_info = inf;
1172 }
1173 #endif /* BFD64 */
1174 }
1175
1176 free (erels);
1177 }
1178
1179 *relsp = rels;
1180 *nrelsp = nrels;
1181 return true;
1182 }
1183
1184 /* Returns the reloc type extracted from the reloc info field. */
1185
1186 static unsigned int
1187 get_reloc_type (Filedata * filedata, bfd_vma reloc_info)
1188 {
1189 if (is_32bit_elf)
1190 return ELF32_R_TYPE (reloc_info);
1191
1192 switch (filedata->file_header.e_machine)
1193 {
1194 case EM_MIPS:
1195 /* Note: We assume that reloc_info has already been adjusted for us. */
1196 return ELF64_MIPS_R_TYPE (reloc_info);
1197
1198 case EM_SPARCV9:
1199 return ELF64_R_TYPE_ID (reloc_info);
1200
1201 default:
1202 return ELF64_R_TYPE (reloc_info);
1203 }
1204 }
1205
1206 /* Return the symbol index extracted from the reloc info field. */
1207
1208 static bfd_vma
1209 get_reloc_symindex (bfd_vma reloc_info)
1210 {
1211 return is_32bit_elf ? ELF32_R_SYM (reloc_info) : ELF64_R_SYM (reloc_info);
1212 }
1213
1214 static inline bool
1215 uses_msp430x_relocs (Filedata * filedata)
1216 {
1217 return
1218 filedata->file_header.e_machine == EM_MSP430 /* Paranoia. */
1219 /* GCC uses osabi == ELFOSBI_STANDALONE. */
1220 && (((filedata->file_header.e_flags & EF_MSP430_MACH) == E_MSP430_MACH_MSP430X)
1221 /* TI compiler uses ELFOSABI_NONE. */
1222 || (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_NONE));
1223 }
1224
1225 /* Display the contents of the relocation data found at the specified
1226 offset. */
1227
1228 static bool
1229 dump_relocations (Filedata * filedata,
1230 unsigned long rel_offset,
1231 unsigned long rel_size,
1232 Elf_Internal_Sym * symtab,
1233 unsigned long nsyms,
1234 char * strtab,
1235 unsigned long strtablen,
1236 int is_rela,
1237 bool is_dynsym)
1238 {
1239 unsigned long i;
1240 Elf_Internal_Rela * rels;
1241 bool res = true;
1242
1243 if (is_rela == UNKNOWN)
1244 is_rela = guess_is_rela (filedata->file_header.e_machine);
1245
1246 if (is_rela)
1247 {
1248 if (!slurp_rela_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1249 return false;
1250 }
1251 else
1252 {
1253 if (!slurp_rel_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1254 return false;
1255 }
1256
1257 if (is_32bit_elf)
1258 {
1259 if (is_rela)
1260 {
1261 if (do_wide)
1262 printf (_(" Offset Info Type Sym. Value Symbol's Name + Addend\n"));
1263 else
1264 printf (_(" Offset Info Type Sym.Value Sym. Name + Addend\n"));
1265 }
1266 else
1267 {
1268 if (do_wide)
1269 printf (_(" Offset Info Type Sym. Value Symbol's Name\n"));
1270 else
1271 printf (_(" Offset Info Type Sym.Value Sym. Name\n"));
1272 }
1273 }
1274 else
1275 {
1276 if (is_rela)
1277 {
1278 if (do_wide)
1279 printf (_(" Offset Info Type Symbol's Value Symbol's Name + Addend\n"));
1280 else
1281 printf (_(" Offset Info Type Sym. Value Sym. Name + Addend\n"));
1282 }
1283 else
1284 {
1285 if (do_wide)
1286 printf (_(" Offset Info Type Symbol's Value Symbol's Name\n"));
1287 else
1288 printf (_(" Offset Info Type Sym. Value Sym. Name\n"));
1289 }
1290 }
1291
1292 for (i = 0; i < rel_size; i++)
1293 {
1294 const char * rtype;
1295 bfd_vma offset;
1296 bfd_vma inf;
1297 bfd_vma symtab_index;
1298 bfd_vma type;
1299
1300 offset = rels[i].r_offset;
1301 inf = rels[i].r_info;
1302
1303 type = get_reloc_type (filedata, inf);
1304 symtab_index = get_reloc_symindex (inf);
1305
1306 if (is_32bit_elf)
1307 {
1308 printf ("%8.8lx %8.8lx ",
1309 (unsigned long) offset & 0xffffffff,
1310 (unsigned long) inf & 0xffffffff);
1311 }
1312 else
1313 {
1314 printf (do_wide
1315 ? "%16.16" BFD_VMA_FMT "x %16.16" BFD_VMA_FMT "x "
1316 : "%12.12" BFD_VMA_FMT "x %12.12" BFD_VMA_FMT "x ",
1317 offset, inf);
1318 }
1319
1320 switch (filedata->file_header.e_machine)
1321 {
1322 default:
1323 rtype = NULL;
1324 break;
1325
1326 case EM_AARCH64:
1327 rtype = elf_aarch64_reloc_type (type);
1328 break;
1329
1330 case EM_M32R:
1331 case EM_CYGNUS_M32R:
1332 rtype = elf_m32r_reloc_type (type);
1333 break;
1334
1335 case EM_386:
1336 case EM_IAMCU:
1337 rtype = elf_i386_reloc_type (type);
1338 break;
1339
1340 case EM_68HC11:
1341 case EM_68HC12:
1342 rtype = elf_m68hc11_reloc_type (type);
1343 break;
1344
1345 case EM_S12Z:
1346 rtype = elf_s12z_reloc_type (type);
1347 break;
1348
1349 case EM_68K:
1350 rtype = elf_m68k_reloc_type (type);
1351 break;
1352
1353 case EM_960:
1354 rtype = elf_i960_reloc_type (type);
1355 break;
1356
1357 case EM_AVR:
1358 case EM_AVR_OLD:
1359 rtype = elf_avr_reloc_type (type);
1360 break;
1361
1362 case EM_OLD_SPARCV9:
1363 case EM_SPARC32PLUS:
1364 case EM_SPARCV9:
1365 case EM_SPARC:
1366 rtype = elf_sparc_reloc_type (type);
1367 break;
1368
1369 case EM_SPU:
1370 rtype = elf_spu_reloc_type (type);
1371 break;
1372
1373 case EM_V800:
1374 rtype = v800_reloc_type (type);
1375 break;
1376 case EM_V850:
1377 case EM_CYGNUS_V850:
1378 rtype = v850_reloc_type (type);
1379 break;
1380
1381 case EM_D10V:
1382 case EM_CYGNUS_D10V:
1383 rtype = elf_d10v_reloc_type (type);
1384 break;
1385
1386 case EM_D30V:
1387 case EM_CYGNUS_D30V:
1388 rtype = elf_d30v_reloc_type (type);
1389 break;
1390
1391 case EM_DLX:
1392 rtype = elf_dlx_reloc_type (type);
1393 break;
1394
1395 case EM_SH:
1396 rtype = elf_sh_reloc_type (type);
1397 break;
1398
1399 case EM_MN10300:
1400 case EM_CYGNUS_MN10300:
1401 rtype = elf_mn10300_reloc_type (type);
1402 break;
1403
1404 case EM_MN10200:
1405 case EM_CYGNUS_MN10200:
1406 rtype = elf_mn10200_reloc_type (type);
1407 break;
1408
1409 case EM_FR30:
1410 case EM_CYGNUS_FR30:
1411 rtype = elf_fr30_reloc_type (type);
1412 break;
1413
1414 case EM_CYGNUS_FRV:
1415 rtype = elf_frv_reloc_type (type);
1416 break;
1417
1418 case EM_CSKY:
1419 rtype = elf_csky_reloc_type (type);
1420 break;
1421
1422 case EM_FT32:
1423 rtype = elf_ft32_reloc_type (type);
1424 break;
1425
1426 case EM_MCORE:
1427 rtype = elf_mcore_reloc_type (type);
1428 break;
1429
1430 case EM_MMIX:
1431 rtype = elf_mmix_reloc_type (type);
1432 break;
1433
1434 case EM_MOXIE:
1435 rtype = elf_moxie_reloc_type (type);
1436 break;
1437
1438 case EM_MSP430:
1439 if (uses_msp430x_relocs (filedata))
1440 {
1441 rtype = elf_msp430x_reloc_type (type);
1442 break;
1443 }
1444 /* Fall through. */
1445 case EM_MSP430_OLD:
1446 rtype = elf_msp430_reloc_type (type);
1447 break;
1448
1449 case EM_NDS32:
1450 rtype = elf_nds32_reloc_type (type);
1451 break;
1452
1453 case EM_PPC:
1454 rtype = elf_ppc_reloc_type (type);
1455 break;
1456
1457 case EM_PPC64:
1458 rtype = elf_ppc64_reloc_type (type);
1459 break;
1460
1461 case EM_MIPS:
1462 case EM_MIPS_RS3_LE:
1463 rtype = elf_mips_reloc_type (type);
1464 break;
1465
1466 case EM_RISCV:
1467 rtype = elf_riscv_reloc_type (type);
1468 break;
1469
1470 case EM_ALPHA:
1471 rtype = elf_alpha_reloc_type (type);
1472 break;
1473
1474 case EM_ARM:
1475 rtype = elf_arm_reloc_type (type);
1476 break;
1477
1478 case EM_ARC:
1479 case EM_ARC_COMPACT:
1480 case EM_ARC_COMPACT2:
1481 rtype = elf_arc_reloc_type (type);
1482 break;
1483
1484 case EM_PARISC:
1485 rtype = elf_hppa_reloc_type (type);
1486 break;
1487
1488 case EM_H8_300:
1489 case EM_H8_300H:
1490 case EM_H8S:
1491 rtype = elf_h8_reloc_type (type);
1492 break;
1493
1494 case EM_OR1K:
1495 rtype = elf_or1k_reloc_type (type);
1496 break;
1497
1498 case EM_PJ:
1499 case EM_PJ_OLD:
1500 rtype = elf_pj_reloc_type (type);
1501 break;
1502 case EM_IA_64:
1503 rtype = elf_ia64_reloc_type (type);
1504 break;
1505
1506 case EM_CRIS:
1507 rtype = elf_cris_reloc_type (type);
1508 break;
1509
1510 case EM_860:
1511 rtype = elf_i860_reloc_type (type);
1512 break;
1513
1514 case EM_X86_64:
1515 case EM_L1OM:
1516 case EM_K1OM:
1517 rtype = elf_x86_64_reloc_type (type);
1518 break;
1519
1520 case EM_S370:
1521 rtype = i370_reloc_type (type);
1522 break;
1523
1524 case EM_S390_OLD:
1525 case EM_S390:
1526 rtype = elf_s390_reloc_type (type);
1527 break;
1528
1529 case EM_SCORE:
1530 rtype = elf_score_reloc_type (type);
1531 break;
1532
1533 case EM_XSTORMY16:
1534 rtype = elf_xstormy16_reloc_type (type);
1535 break;
1536
1537 case EM_CRX:
1538 rtype = elf_crx_reloc_type (type);
1539 break;
1540
1541 case EM_VAX:
1542 rtype = elf_vax_reloc_type (type);
1543 break;
1544
1545 case EM_VISIUM:
1546 rtype = elf_visium_reloc_type (type);
1547 break;
1548
1549 case EM_BPF:
1550 rtype = elf_bpf_reloc_type (type);
1551 break;
1552
1553 case EM_ADAPTEVA_EPIPHANY:
1554 rtype = elf_epiphany_reloc_type (type);
1555 break;
1556
1557 case EM_IP2K:
1558 case EM_IP2K_OLD:
1559 rtype = elf_ip2k_reloc_type (type);
1560 break;
1561
1562 case EM_IQ2000:
1563 rtype = elf_iq2000_reloc_type (type);
1564 break;
1565
1566 case EM_XTENSA_OLD:
1567 case EM_XTENSA:
1568 rtype = elf_xtensa_reloc_type (type);
1569 break;
1570
1571 case EM_LATTICEMICO32:
1572 rtype = elf_lm32_reloc_type (type);
1573 break;
1574
1575 case EM_M32C_OLD:
1576 case EM_M32C:
1577 rtype = elf_m32c_reloc_type (type);
1578 break;
1579
1580 case EM_MT:
1581 rtype = elf_mt_reloc_type (type);
1582 break;
1583
1584 case EM_BLACKFIN:
1585 rtype = elf_bfin_reloc_type (type);
1586 break;
1587
1588 case EM_CYGNUS_MEP:
1589 rtype = elf_mep_reloc_type (type);
1590 break;
1591
1592 case EM_CR16:
1593 rtype = elf_cr16_reloc_type (type);
1594 break;
1595
1596 case EM_MICROBLAZE:
1597 case EM_MICROBLAZE_OLD:
1598 rtype = elf_microblaze_reloc_type (type);
1599 break;
1600
1601 case EM_RL78:
1602 rtype = elf_rl78_reloc_type (type);
1603 break;
1604
1605 case EM_RX:
1606 rtype = elf_rx_reloc_type (type);
1607 break;
1608
1609 case EM_METAG:
1610 rtype = elf_metag_reloc_type (type);
1611 break;
1612
1613 case EM_XC16X:
1614 case EM_C166:
1615 rtype = elf_xc16x_reloc_type (type);
1616 break;
1617
1618 case EM_TI_C6000:
1619 rtype = elf_tic6x_reloc_type (type);
1620 break;
1621
1622 case EM_TILEGX:
1623 rtype = elf_tilegx_reloc_type (type);
1624 break;
1625
1626 case EM_TILEPRO:
1627 rtype = elf_tilepro_reloc_type (type);
1628 break;
1629
1630 case EM_WEBASSEMBLY:
1631 rtype = elf_wasm32_reloc_type (type);
1632 break;
1633
1634 case EM_XGATE:
1635 rtype = elf_xgate_reloc_type (type);
1636 break;
1637
1638 case EM_ALTERA_NIOS2:
1639 rtype = elf_nios2_reloc_type (type);
1640 break;
1641
1642 case EM_TI_PRU:
1643 rtype = elf_pru_reloc_type (type);
1644 break;
1645
1646 case EM_NFP:
1647 if (EF_NFP_MACH (filedata->file_header.e_flags) == E_NFP_MACH_3200)
1648 rtype = elf_nfp3200_reloc_type (type);
1649 else
1650 rtype = elf_nfp_reloc_type (type);
1651 break;
1652
1653 case EM_Z80:
1654 rtype = elf_z80_reloc_type (type);
1655 break;
1656 }
1657
1658 if (rtype == NULL)
1659 printf (_("unrecognized: %-7lx"), (unsigned long) type & 0xffffffff);
1660 else
1661 printf (do_wide ? "%-22s" : "%-17.17s", rtype);
1662
1663 if (filedata->file_header.e_machine == EM_ALPHA
1664 && rtype != NULL
1665 && streq (rtype, "R_ALPHA_LITUSE")
1666 && is_rela)
1667 {
1668 switch (rels[i].r_addend)
1669 {
1670 case LITUSE_ALPHA_ADDR: rtype = "ADDR"; break;
1671 case LITUSE_ALPHA_BASE: rtype = "BASE"; break;
1672 case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
1673 case LITUSE_ALPHA_JSR: rtype = "JSR"; break;
1674 case LITUSE_ALPHA_TLSGD: rtype = "TLSGD"; break;
1675 case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
1676 case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
1677 default: rtype = NULL;
1678 }
1679
1680 if (rtype)
1681 printf (" (%s)", rtype);
1682 else
1683 {
1684 putchar (' ');
1685 printf (_("<unknown addend: %lx>"),
1686 (unsigned long) rels[i].r_addend);
1687 res = false;
1688 }
1689 }
1690 else if (symtab_index)
1691 {
1692 if (symtab == NULL || symtab_index >= nsyms)
1693 {
1694 error (_(" bad symbol index: %08lx in reloc\n"),
1695 (unsigned long) symtab_index);
1696 res = false;
1697 }
1698 else
1699 {
1700 Elf_Internal_Sym * psym;
1701 const char * version_string;
1702 enum versioned_symbol_info sym_info;
1703 unsigned short vna_other;
1704
1705 psym = symtab + symtab_index;
1706
1707 version_string
1708 = get_symbol_version_string (filedata, is_dynsym,
1709 strtab, strtablen,
1710 symtab_index,
1711 psym,
1712 &sym_info,
1713 &vna_other);
1714
1715 printf (" ");
1716
1717 if (ELF_ST_TYPE (psym->st_info) == STT_GNU_IFUNC)
1718 {
1719 const char * name;
1720 unsigned int len;
1721 unsigned int width = is_32bit_elf ? 8 : 14;
1722
1723 /* Relocations against GNU_IFUNC symbols do not use the value
1724 of the symbol as the address to relocate against. Instead
1725 they invoke the function named by the symbol and use its
1726 result as the address for relocation.
1727
1728 To indicate this to the user, do not display the value of
1729 the symbol in the "Symbols's Value" field. Instead show
1730 its name followed by () as a hint that the symbol is
1731 invoked. */
1732
1733 if (strtab == NULL
1734 || psym->st_name == 0
1735 || psym->st_name >= strtablen)
1736 name = "??";
1737 else
1738 name = strtab + psym->st_name;
1739
1740 len = print_symbol (width, name);
1741 if (version_string)
1742 printf (sym_info == symbol_public ? "@@%s" : "@%s",
1743 version_string);
1744 printf ("()%-*s", len <= width ? (width + 1) - len : 1, " ");
1745 }
1746 else
1747 {
1748 print_vma (psym->st_value, LONG_HEX);
1749
1750 printf (is_32bit_elf ? " " : " ");
1751 }
1752
1753 if (psym->st_name == 0)
1754 {
1755 const char * sec_name = "<null>";
1756 char name_buf[40];
1757
1758 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
1759 {
1760 if (psym->st_shndx < filedata->file_header.e_shnum)
1761 sec_name = SECTION_NAME_PRINT (filedata->section_headers
1762 + psym->st_shndx);
1763 else if (psym->st_shndx == SHN_ABS)
1764 sec_name = "ABS";
1765 else if (psym->st_shndx == SHN_COMMON)
1766 sec_name = "COMMON";
1767 else if ((filedata->file_header.e_machine == EM_MIPS
1768 && psym->st_shndx == SHN_MIPS_SCOMMON)
1769 || (filedata->file_header.e_machine == EM_TI_C6000
1770 && psym->st_shndx == SHN_TIC6X_SCOMMON))
1771 sec_name = "SCOMMON";
1772 else if (filedata->file_header.e_machine == EM_MIPS
1773 && psym->st_shndx == SHN_MIPS_SUNDEFINED)
1774 sec_name = "SUNDEF";
1775 else if ((filedata->file_header.e_machine == EM_X86_64
1776 || filedata->file_header.e_machine == EM_L1OM
1777 || filedata->file_header.e_machine == EM_K1OM)
1778 && psym->st_shndx == SHN_X86_64_LCOMMON)
1779 sec_name = "LARGE_COMMON";
1780 else if (filedata->file_header.e_machine == EM_IA_64
1781 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
1782 && psym->st_shndx == SHN_IA_64_ANSI_COMMON)
1783 sec_name = "ANSI_COM";
1784 else if (is_ia64_vms (filedata)
1785 && psym->st_shndx == SHN_IA_64_VMS_SYMVEC)
1786 sec_name = "VMS_SYMVEC";
1787 else
1788 {
1789 sprintf (name_buf, "<section 0x%x>",
1790 (unsigned int) psym->st_shndx);
1791 sec_name = name_buf;
1792 }
1793 }
1794 print_symbol (22, sec_name);
1795 }
1796 else if (strtab == NULL)
1797 printf (_("<string table index: %3ld>"), psym->st_name);
1798 else if (psym->st_name >= strtablen)
1799 {
1800 error (_("<corrupt string table index: %3ld>\n"),
1801 psym->st_name);
1802 res = false;
1803 }
1804 else
1805 {
1806 print_symbol (22, strtab + psym->st_name);
1807 if (version_string)
1808 printf (sym_info == symbol_public ? "@@%s" : "@%s",
1809 version_string);
1810 }
1811
1812 if (is_rela)
1813 {
1814 bfd_vma off = rels[i].r_addend;
1815
1816 if ((bfd_signed_vma) off < 0)
1817 printf (" - %" BFD_VMA_FMT "x", - off);
1818 else
1819 printf (" + %" BFD_VMA_FMT "x", off);
1820 }
1821 }
1822 }
1823 else if (is_rela)
1824 {
1825 bfd_vma off = rels[i].r_addend;
1826
1827 printf ("%*c", is_32bit_elf ? 12 : 20, ' ');
1828 if ((bfd_signed_vma) off < 0)
1829 printf ("-%" BFD_VMA_FMT "x", - off);
1830 else
1831 printf ("%" BFD_VMA_FMT "x", off);
1832 }
1833
1834 if (filedata->file_header.e_machine == EM_SPARCV9
1835 && rtype != NULL
1836 && streq (rtype, "R_SPARC_OLO10"))
1837 printf (" + %lx", (unsigned long) ELF64_R_TYPE_DATA (inf));
1838
1839 putchar ('\n');
1840
1841 #ifdef BFD64
1842 if (! is_32bit_elf && filedata->file_header.e_machine == EM_MIPS)
1843 {
1844 bfd_vma type2 = ELF64_MIPS_R_TYPE2 (inf);
1845 bfd_vma type3 = ELF64_MIPS_R_TYPE3 (inf);
1846 const char * rtype2 = elf_mips_reloc_type (type2);
1847 const char * rtype3 = elf_mips_reloc_type (type3);
1848
1849 printf (" Type2: ");
1850
1851 if (rtype2 == NULL)
1852 printf (_("unrecognized: %-7lx"),
1853 (unsigned long) type2 & 0xffffffff);
1854 else
1855 printf ("%-17.17s", rtype2);
1856
1857 printf ("\n Type3: ");
1858
1859 if (rtype3 == NULL)
1860 printf (_("unrecognized: %-7lx"),
1861 (unsigned long) type3 & 0xffffffff);
1862 else
1863 printf ("%-17.17s", rtype3);
1864
1865 putchar ('\n');
1866 }
1867 #endif /* BFD64 */
1868 }
1869
1870 free (rels);
1871
1872 return res;
1873 }
1874
1875 static const char *
1876 get_aarch64_dynamic_type (unsigned long type)
1877 {
1878 switch (type)
1879 {
1880 case DT_AARCH64_BTI_PLT: return "AARCH64_BTI_PLT";
1881 case DT_AARCH64_PAC_PLT: return "AARCH64_PAC_PLT";
1882 case DT_AARCH64_VARIANT_PCS: return "AARCH64_VARIANT_PCS";
1883 default:
1884 return NULL;
1885 }
1886 }
1887
1888 static const char *
1889 get_mips_dynamic_type (unsigned long type)
1890 {
1891 switch (type)
1892 {
1893 case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
1894 case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
1895 case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
1896 case DT_MIPS_IVERSION: return "MIPS_IVERSION";
1897 case DT_MIPS_FLAGS: return "MIPS_FLAGS";
1898 case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
1899 case DT_MIPS_MSYM: return "MIPS_MSYM";
1900 case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
1901 case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
1902 case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
1903 case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
1904 case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
1905 case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
1906 case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
1907 case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
1908 case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
1909 case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
1910 case DT_MIPS_RLD_MAP_REL: return "MIPS_RLD_MAP_REL";
1911 case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
1912 case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
1913 case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
1914 case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
1915 case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
1916 case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
1917 case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
1918 case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
1919 case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
1920 case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
1921 case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
1922 case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
1923 case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
1924 case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
1925 case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
1926 case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
1927 case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
1928 case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
1929 case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
1930 case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
1931 case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
1932 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
1933 case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
1934 case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
1935 case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
1936 case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
1937 case DT_MIPS_PLTGOT: return "MIPS_PLTGOT";
1938 case DT_MIPS_RWPLT: return "MIPS_RWPLT";
1939 case DT_MIPS_XHASH: return "MIPS_XHASH";
1940 default:
1941 return NULL;
1942 }
1943 }
1944
1945 static const char *
1946 get_sparc64_dynamic_type (unsigned long type)
1947 {
1948 switch (type)
1949 {
1950 case DT_SPARC_REGISTER: return "SPARC_REGISTER";
1951 default:
1952 return NULL;
1953 }
1954 }
1955
1956 static const char *
1957 get_ppc_dynamic_type (unsigned long type)
1958 {
1959 switch (type)
1960 {
1961 case DT_PPC_GOT: return "PPC_GOT";
1962 case DT_PPC_OPT: return "PPC_OPT";
1963 default:
1964 return NULL;
1965 }
1966 }
1967
1968 static const char *
1969 get_ppc64_dynamic_type (unsigned long type)
1970 {
1971 switch (type)
1972 {
1973 case DT_PPC64_GLINK: return "PPC64_GLINK";
1974 case DT_PPC64_OPD: return "PPC64_OPD";
1975 case DT_PPC64_OPDSZ: return "PPC64_OPDSZ";
1976 case DT_PPC64_OPT: return "PPC64_OPT";
1977 default:
1978 return NULL;
1979 }
1980 }
1981
1982 static const char *
1983 get_parisc_dynamic_type (unsigned long type)
1984 {
1985 switch (type)
1986 {
1987 case DT_HP_LOAD_MAP: return "HP_LOAD_MAP";
1988 case DT_HP_DLD_FLAGS: return "HP_DLD_FLAGS";
1989 case DT_HP_DLD_HOOK: return "HP_DLD_HOOK";
1990 case DT_HP_UX10_INIT: return "HP_UX10_INIT";
1991 case DT_HP_UX10_INITSZ: return "HP_UX10_INITSZ";
1992 case DT_HP_PREINIT: return "HP_PREINIT";
1993 case DT_HP_PREINITSZ: return "HP_PREINITSZ";
1994 case DT_HP_NEEDED: return "HP_NEEDED";
1995 case DT_HP_TIME_STAMP: return "HP_TIME_STAMP";
1996 case DT_HP_CHECKSUM: return "HP_CHECKSUM";
1997 case DT_HP_GST_SIZE: return "HP_GST_SIZE";
1998 case DT_HP_GST_VERSION: return "HP_GST_VERSION";
1999 case DT_HP_GST_HASHVAL: return "HP_GST_HASHVAL";
2000 case DT_HP_EPLTREL: return "HP_GST_EPLTREL";
2001 case DT_HP_EPLTRELSZ: return "HP_GST_EPLTRELSZ";
2002 case DT_HP_FILTERED: return "HP_FILTERED";
2003 case DT_HP_FILTER_TLS: return "HP_FILTER_TLS";
2004 case DT_HP_COMPAT_FILTERED: return "HP_COMPAT_FILTERED";
2005 case DT_HP_LAZYLOAD: return "HP_LAZYLOAD";
2006 case DT_HP_BIND_NOW_COUNT: return "HP_BIND_NOW_COUNT";
2007 case DT_PLT: return "PLT";
2008 case DT_PLT_SIZE: return "PLT_SIZE";
2009 case DT_DLT: return "DLT";
2010 case DT_DLT_SIZE: return "DLT_SIZE";
2011 default:
2012 return NULL;
2013 }
2014 }
2015
2016 static const char *
2017 get_ia64_dynamic_type (unsigned long type)
2018 {
2019 switch (type)
2020 {
2021 case DT_IA_64_PLT_RESERVE: return "IA_64_PLT_RESERVE";
2022 case DT_IA_64_VMS_SUBTYPE: return "VMS_SUBTYPE";
2023 case DT_IA_64_VMS_IMGIOCNT: return "VMS_IMGIOCNT";
2024 case DT_IA_64_VMS_LNKFLAGS: return "VMS_LNKFLAGS";
2025 case DT_IA_64_VMS_VIR_MEM_BLK_SIZ: return "VMS_VIR_MEM_BLK_SIZ";
2026 case DT_IA_64_VMS_IDENT: return "VMS_IDENT";
2027 case DT_IA_64_VMS_NEEDED_IDENT: return "VMS_NEEDED_IDENT";
2028 case DT_IA_64_VMS_IMG_RELA_CNT: return "VMS_IMG_RELA_CNT";
2029 case DT_IA_64_VMS_SEG_RELA_CNT: return "VMS_SEG_RELA_CNT";
2030 case DT_IA_64_VMS_FIXUP_RELA_CNT: return "VMS_FIXUP_RELA_CNT";
2031 case DT_IA_64_VMS_FIXUP_NEEDED: return "VMS_FIXUP_NEEDED";
2032 case DT_IA_64_VMS_SYMVEC_CNT: return "VMS_SYMVEC_CNT";
2033 case DT_IA_64_VMS_XLATED: return "VMS_XLATED";
2034 case DT_IA_64_VMS_STACKSIZE: return "VMS_STACKSIZE";
2035 case DT_IA_64_VMS_UNWINDSZ: return "VMS_UNWINDSZ";
2036 case DT_IA_64_VMS_UNWIND_CODSEG: return "VMS_UNWIND_CODSEG";
2037 case DT_IA_64_VMS_UNWIND_INFOSEG: return "VMS_UNWIND_INFOSEG";
2038 case DT_IA_64_VMS_LINKTIME: return "VMS_LINKTIME";
2039 case DT_IA_64_VMS_SEG_NO: return "VMS_SEG_NO";
2040 case DT_IA_64_VMS_SYMVEC_OFFSET: return "VMS_SYMVEC_OFFSET";
2041 case DT_IA_64_VMS_SYMVEC_SEG: return "VMS_SYMVEC_SEG";
2042 case DT_IA_64_VMS_UNWIND_OFFSET: return "VMS_UNWIND_OFFSET";
2043 case DT_IA_64_VMS_UNWIND_SEG: return "VMS_UNWIND_SEG";
2044 case DT_IA_64_VMS_STRTAB_OFFSET: return "VMS_STRTAB_OFFSET";
2045 case DT_IA_64_VMS_SYSVER_OFFSET: return "VMS_SYSVER_OFFSET";
2046 case DT_IA_64_VMS_IMG_RELA_OFF: return "VMS_IMG_RELA_OFF";
2047 case DT_IA_64_VMS_SEG_RELA_OFF: return "VMS_SEG_RELA_OFF";
2048 case DT_IA_64_VMS_FIXUP_RELA_OFF: return "VMS_FIXUP_RELA_OFF";
2049 case DT_IA_64_VMS_PLTGOT_OFFSET: return "VMS_PLTGOT_OFFSET";
2050 case DT_IA_64_VMS_PLTGOT_SEG: return "VMS_PLTGOT_SEG";
2051 case DT_IA_64_VMS_FPMODE: return "VMS_FPMODE";
2052 default:
2053 return NULL;
2054 }
2055 }
2056
2057 static const char *
2058 get_solaris_section_type (unsigned long type)
2059 {
2060 switch (type)
2061 {
2062 case 0x6fffffee: return "SUNW_ancillary";
2063 case 0x6fffffef: return "SUNW_capchain";
2064 case 0x6ffffff0: return "SUNW_capinfo";
2065 case 0x6ffffff1: return "SUNW_symsort";
2066 case 0x6ffffff2: return "SUNW_tlssort";
2067 case 0x6ffffff3: return "SUNW_LDYNSYM";
2068 case 0x6ffffff4: return "SUNW_dof";
2069 case 0x6ffffff5: return "SUNW_cap";
2070 case 0x6ffffff6: return "SUNW_SIGNATURE";
2071 case 0x6ffffff7: return "SUNW_ANNOTATE";
2072 case 0x6ffffff8: return "SUNW_DEBUGSTR";
2073 case 0x6ffffff9: return "SUNW_DEBUG";
2074 case 0x6ffffffa: return "SUNW_move";
2075 case 0x6ffffffb: return "SUNW_COMDAT";
2076 case 0x6ffffffc: return "SUNW_syminfo";
2077 case 0x6ffffffd: return "SUNW_verdef";
2078 case 0x6ffffffe: return "SUNW_verneed";
2079 case 0x6fffffff: return "SUNW_versym";
2080 case 0x70000000: return "SPARC_GOTDATA";
2081 default: return NULL;
2082 }
2083 }
2084
2085 static const char *
2086 get_alpha_dynamic_type (unsigned long type)
2087 {
2088 switch (type)
2089 {
2090 case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
2091 default: return NULL;
2092 }
2093 }
2094
2095 static const char *
2096 get_score_dynamic_type (unsigned long type)
2097 {
2098 switch (type)
2099 {
2100 case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
2101 case DT_SCORE_LOCAL_GOTNO: return "SCORE_LOCAL_GOTNO";
2102 case DT_SCORE_SYMTABNO: return "SCORE_SYMTABNO";
2103 case DT_SCORE_GOTSYM: return "SCORE_GOTSYM";
2104 case DT_SCORE_UNREFEXTNO: return "SCORE_UNREFEXTNO";
2105 case DT_SCORE_HIPAGENO: return "SCORE_HIPAGENO";
2106 default: return NULL;
2107 }
2108 }
2109
2110 static const char *
2111 get_tic6x_dynamic_type (unsigned long type)
2112 {
2113 switch (type)
2114 {
2115 case DT_C6000_GSYM_OFFSET: return "C6000_GSYM_OFFSET";
2116 case DT_C6000_GSTR_OFFSET: return "C6000_GSTR_OFFSET";
2117 case DT_C6000_DSBT_BASE: return "C6000_DSBT_BASE";
2118 case DT_C6000_DSBT_SIZE: return "C6000_DSBT_SIZE";
2119 case DT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
2120 case DT_C6000_DSBT_INDEX: return "C6000_DSBT_INDEX";
2121 default: return NULL;
2122 }
2123 }
2124
2125 static const char *
2126 get_nios2_dynamic_type (unsigned long type)
2127 {
2128 switch (type)
2129 {
2130 case DT_NIOS2_GP: return "NIOS2_GP";
2131 default: return NULL;
2132 }
2133 }
2134
2135 static const char *
2136 get_solaris_dynamic_type (unsigned long type)
2137 {
2138 switch (type)
2139 {
2140 case 0x6000000d: return "SUNW_AUXILIARY";
2141 case 0x6000000e: return "SUNW_RTLDINF";
2142 case 0x6000000f: return "SUNW_FILTER";
2143 case 0x60000010: return "SUNW_CAP";
2144 case 0x60000011: return "SUNW_SYMTAB";
2145 case 0x60000012: return "SUNW_SYMSZ";
2146 case 0x60000013: return "SUNW_SORTENT";
2147 case 0x60000014: return "SUNW_SYMSORT";
2148 case 0x60000015: return "SUNW_SYMSORTSZ";
2149 case 0x60000016: return "SUNW_TLSSORT";
2150 case 0x60000017: return "SUNW_TLSSORTSZ";
2151 case 0x60000018: return "SUNW_CAPINFO";
2152 case 0x60000019: return "SUNW_STRPAD";
2153 case 0x6000001a: return "SUNW_CAPCHAIN";
2154 case 0x6000001b: return "SUNW_LDMACH";
2155 case 0x6000001d: return "SUNW_CAPCHAINENT";
2156 case 0x6000001f: return "SUNW_CAPCHAINSZ";
2157 case 0x60000021: return "SUNW_PARENT";
2158 case 0x60000023: return "SUNW_ASLR";
2159 case 0x60000025: return "SUNW_RELAX";
2160 case 0x60000029: return "SUNW_NXHEAP";
2161 case 0x6000002b: return "SUNW_NXSTACK";
2162
2163 case 0x70000001: return "SPARC_REGISTER";
2164 case 0x7ffffffd: return "AUXILIARY";
2165 case 0x7ffffffe: return "USED";
2166 case 0x7fffffff: return "FILTER";
2167
2168 default: return NULL;
2169 }
2170 }
2171
2172 static const char *
2173 get_dynamic_type (Filedata * filedata, unsigned long type)
2174 {
2175 static char buff[64];
2176
2177 switch (type)
2178 {
2179 case DT_NULL: return "NULL";
2180 case DT_NEEDED: return "NEEDED";
2181 case DT_PLTRELSZ: return "PLTRELSZ";
2182 case DT_PLTGOT: return "PLTGOT";
2183 case DT_HASH: return "HASH";
2184 case DT_STRTAB: return "STRTAB";
2185 case DT_SYMTAB: return "SYMTAB";
2186 case DT_RELA: return "RELA";
2187 case DT_RELASZ: return "RELASZ";
2188 case DT_RELAENT: return "RELAENT";
2189 case DT_STRSZ: return "STRSZ";
2190 case DT_SYMENT: return "SYMENT";
2191 case DT_INIT: return "INIT";
2192 case DT_FINI: return "FINI";
2193 case DT_SONAME: return "SONAME";
2194 case DT_RPATH: return "RPATH";
2195 case DT_SYMBOLIC: return "SYMBOLIC";
2196 case DT_REL: return "REL";
2197 case DT_RELSZ: return "RELSZ";
2198 case DT_RELENT: return "RELENT";
2199 case DT_PLTREL: return "PLTREL";
2200 case DT_DEBUG: return "DEBUG";
2201 case DT_TEXTREL: return "TEXTREL";
2202 case DT_JMPREL: return "JMPREL";
2203 case DT_BIND_NOW: return "BIND_NOW";
2204 case DT_INIT_ARRAY: return "INIT_ARRAY";
2205 case DT_FINI_ARRAY: return "FINI_ARRAY";
2206 case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
2207 case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
2208 case DT_RUNPATH: return "RUNPATH";
2209 case DT_FLAGS: return "FLAGS";
2210
2211 case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
2212 case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
2213 case DT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
2214
2215 case DT_CHECKSUM: return "CHECKSUM";
2216 case DT_PLTPADSZ: return "PLTPADSZ";
2217 case DT_MOVEENT: return "MOVEENT";
2218 case DT_MOVESZ: return "MOVESZ";
2219 case DT_FEATURE: return "FEATURE";
2220 case DT_POSFLAG_1: return "POSFLAG_1";
2221 case DT_SYMINSZ: return "SYMINSZ";
2222 case DT_SYMINENT: return "SYMINENT"; /* aka VALRNGHI */
2223
2224 case DT_ADDRRNGLO: return "ADDRRNGLO";
2225 case DT_CONFIG: return "CONFIG";
2226 case DT_DEPAUDIT: return "DEPAUDIT";
2227 case DT_AUDIT: return "AUDIT";
2228 case DT_PLTPAD: return "PLTPAD";
2229 case DT_MOVETAB: return "MOVETAB";
2230 case DT_SYMINFO: return "SYMINFO"; /* aka ADDRRNGHI */
2231
2232 case DT_VERSYM: return "VERSYM";
2233
2234 case DT_TLSDESC_GOT: return "TLSDESC_GOT";
2235 case DT_TLSDESC_PLT: return "TLSDESC_PLT";
2236 case DT_RELACOUNT: return "RELACOUNT";
2237 case DT_RELCOUNT: return "RELCOUNT";
2238 case DT_FLAGS_1: return "FLAGS_1";
2239 case DT_VERDEF: return "VERDEF";
2240 case DT_VERDEFNUM: return "VERDEFNUM";
2241 case DT_VERNEED: return "VERNEED";
2242 case DT_VERNEEDNUM: return "VERNEEDNUM";
2243
2244 case DT_AUXILIARY: return "AUXILIARY";
2245 case DT_USED: return "USED";
2246 case DT_FILTER: return "FILTER";
2247
2248 case DT_GNU_PRELINKED: return "GNU_PRELINKED";
2249 case DT_GNU_CONFLICT: return "GNU_CONFLICT";
2250 case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
2251 case DT_GNU_LIBLIST: return "GNU_LIBLIST";
2252 case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
2253 case DT_GNU_HASH: return "GNU_HASH";
2254 case DT_GNU_FLAGS_1: return "GNU_FLAGS_1";
2255
2256 default:
2257 if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
2258 {
2259 const char * result;
2260
2261 switch (filedata->file_header.e_machine)
2262 {
2263 case EM_AARCH64:
2264 result = get_aarch64_dynamic_type (type);
2265 break;
2266 case EM_MIPS:
2267 case EM_MIPS_RS3_LE:
2268 result = get_mips_dynamic_type (type);
2269 break;
2270 case EM_SPARCV9:
2271 result = get_sparc64_dynamic_type (type);
2272 break;
2273 case EM_PPC:
2274 result = get_ppc_dynamic_type (type);
2275 break;
2276 case EM_PPC64:
2277 result = get_ppc64_dynamic_type (type);
2278 break;
2279 case EM_IA_64:
2280 result = get_ia64_dynamic_type (type);
2281 break;
2282 case EM_ALPHA:
2283 result = get_alpha_dynamic_type (type);
2284 break;
2285 case EM_SCORE:
2286 result = get_score_dynamic_type (type);
2287 break;
2288 case EM_TI_C6000:
2289 result = get_tic6x_dynamic_type (type);
2290 break;
2291 case EM_ALTERA_NIOS2:
2292 result = get_nios2_dynamic_type (type);
2293 break;
2294 default:
2295 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2296 result = get_solaris_dynamic_type (type);
2297 else
2298 result = NULL;
2299 break;
2300 }
2301
2302 if (result != NULL)
2303 return result;
2304
2305 snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
2306 }
2307 else if (((type >= DT_LOOS) && (type <= DT_HIOS))
2308 || (filedata->file_header.e_machine == EM_PARISC
2309 && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
2310 {
2311 const char * result;
2312
2313 switch (filedata->file_header.e_machine)
2314 {
2315 case EM_PARISC:
2316 result = get_parisc_dynamic_type (type);
2317 break;
2318 case EM_IA_64:
2319 result = get_ia64_dynamic_type (type);
2320 break;
2321 default:
2322 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2323 result = get_solaris_dynamic_type (type);
2324 else
2325 result = NULL;
2326 break;
2327 }
2328
2329 if (result != NULL)
2330 return result;
2331
2332 snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
2333 type);
2334 }
2335 else
2336 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
2337
2338 return buff;
2339 }
2340 }
2341
2342 static char *
2343 get_file_type (unsigned e_type)
2344 {
2345 static char buff[64];
2346
2347 switch (e_type)
2348 {
2349 case ET_NONE: return _("NONE (None)");
2350 case ET_REL: return _("REL (Relocatable file)");
2351 case ET_EXEC: return _("EXEC (Executable file)");
2352 case ET_DYN: return _("DYN (Shared object file)");
2353 case ET_CORE: return _("CORE (Core file)");
2354
2355 default:
2356 if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
2357 snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
2358 else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
2359 snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
2360 else
2361 snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
2362 return buff;
2363 }
2364 }
2365
2366 static char *
2367 get_machine_name (unsigned e_machine)
2368 {
2369 static char buff[64]; /* XXX */
2370
2371 switch (e_machine)
2372 {
2373 /* Please keep this switch table sorted by increasing EM_ value. */
2374 /* 0 */
2375 case EM_NONE: return _("None");
2376 case EM_M32: return "WE32100";
2377 case EM_SPARC: return "Sparc";
2378 case EM_386: return "Intel 80386";
2379 case EM_68K: return "MC68000";
2380 case EM_88K: return "MC88000";
2381 case EM_IAMCU: return "Intel MCU";
2382 case EM_860: return "Intel 80860";
2383 case EM_MIPS: return "MIPS R3000";
2384 case EM_S370: return "IBM System/370";
2385 /* 10 */
2386 case EM_MIPS_RS3_LE: return "MIPS R4000 big-endian";
2387 case EM_OLD_SPARCV9: return "Sparc v9 (old)";
2388 case EM_PARISC: return "HPPA";
2389 case EM_VPP550: return "Fujitsu VPP500";
2390 case EM_SPARC32PLUS: return "Sparc v8+" ;
2391 case EM_960: return "Intel 80960";
2392 case EM_PPC: return "PowerPC";
2393 /* 20 */
2394 case EM_PPC64: return "PowerPC64";
2395 case EM_S390_OLD:
2396 case EM_S390: return "IBM S/390";
2397 case EM_SPU: return "SPU";
2398 /* 30 */
2399 case EM_V800: return "Renesas V850 (using RH850 ABI)";
2400 case EM_FR20: return "Fujitsu FR20";
2401 case EM_RH32: return "TRW RH32";
2402 case EM_MCORE: return "MCORE";
2403 /* 40 */
2404 case EM_ARM: return "ARM";
2405 case EM_OLD_ALPHA: return "Digital Alpha (old)";
2406 case EM_SH: return "Renesas / SuperH SH";
2407 case EM_SPARCV9: return "Sparc v9";
2408 case EM_TRICORE: return "Siemens Tricore";
2409 case EM_ARC: return "ARC";
2410 case EM_H8_300: return "Renesas H8/300";
2411 case EM_H8_300H: return "Renesas H8/300H";
2412 case EM_H8S: return "Renesas H8S";
2413 case EM_H8_500: return "Renesas H8/500";
2414 /* 50 */
2415 case EM_IA_64: return "Intel IA-64";
2416 case EM_MIPS_X: return "Stanford MIPS-X";
2417 case EM_COLDFIRE: return "Motorola Coldfire";
2418 case EM_68HC12: return "Motorola MC68HC12 Microcontroller";
2419 case EM_MMA: return "Fujitsu Multimedia Accelerator";
2420 case EM_PCP: return "Siemens PCP";
2421 case EM_NCPU: return "Sony nCPU embedded RISC processor";
2422 case EM_NDR1: return "Denso NDR1 microprocesspr";
2423 case EM_STARCORE: return "Motorola Star*Core processor";
2424 case EM_ME16: return "Toyota ME16 processor";
2425 /* 60 */
2426 case EM_ST100: return "STMicroelectronics ST100 processor";
2427 case EM_TINYJ: return "Advanced Logic Corp. TinyJ embedded processor";
2428 case EM_X86_64: return "Advanced Micro Devices X86-64";
2429 case EM_PDSP: return "Sony DSP processor";
2430 case EM_PDP10: return "Digital Equipment Corp. PDP-10";
2431 case EM_PDP11: return "Digital Equipment Corp. PDP-11";
2432 case EM_FX66: return "Siemens FX66 microcontroller";
2433 case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 bit microcontroller";
2434 case EM_ST7: return "STMicroelectronics ST7 8-bit microcontroller";
2435 case EM_68HC16: return "Motorola MC68HC16 Microcontroller";
2436 /* 70 */
2437 case EM_68HC11: return "Motorola MC68HC11 Microcontroller";
2438 case EM_68HC08: return "Motorola MC68HC08 Microcontroller";
2439 case EM_68HC05: return "Motorola MC68HC05 Microcontroller";
2440 case EM_SVX: return "Silicon Graphics SVx";
2441 case EM_ST19: return "STMicroelectronics ST19 8-bit microcontroller";
2442 case EM_VAX: return "Digital VAX";
2443 case EM_CRIS: return "Axis Communications 32-bit embedded processor";
2444 case EM_JAVELIN: return "Infineon Technologies 32-bit embedded cpu";
2445 case EM_FIREPATH: return "Element 14 64-bit DSP processor";
2446 case EM_ZSP: return "LSI Logic's 16-bit DSP processor";
2447 /* 80 */
2448 case EM_MMIX: return "Donald Knuth's educational 64-bit processor";
2449 case EM_HUANY: return "Harvard Universitys's machine-independent object format";
2450 case EM_PRISM: return "Vitesse Prism";
2451 case EM_AVR_OLD:
2452 case EM_AVR: return "Atmel AVR 8-bit microcontroller";
2453 case EM_CYGNUS_FR30:
2454 case EM_FR30: return "Fujitsu FR30";
2455 case EM_CYGNUS_D10V:
2456 case EM_D10V: return "d10v";
2457 case EM_CYGNUS_D30V:
2458 case EM_D30V: return "d30v";
2459 case EM_CYGNUS_V850:
2460 case EM_V850: return "Renesas V850";
2461 case EM_CYGNUS_M32R:
2462 case EM_M32R: return "Renesas M32R (formerly Mitsubishi M32r)";
2463 case EM_CYGNUS_MN10300:
2464 case EM_MN10300: return "mn10300";
2465 /* 90 */
2466 case EM_CYGNUS_MN10200:
2467 case EM_MN10200: return "mn10200";
2468 case EM_PJ: return "picoJava";
2469 case EM_OR1K: return "OpenRISC 1000";
2470 case EM_ARC_COMPACT: return "ARCompact";
2471 case EM_XTENSA_OLD:
2472 case EM_XTENSA: return "Tensilica Xtensa Processor";
2473 case EM_VIDEOCORE: return "Alphamosaic VideoCore processor";
2474 case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor";
2475 case EM_NS32K: return "National Semiconductor 32000 series";
2476 case EM_TPC: return "Tenor Network TPC processor";
2477 case EM_SNP1K: return "Trebia SNP 1000 processor";
2478 /* 100 */
2479 case EM_ST200: return "STMicroelectronics ST200 microcontroller";
2480 case EM_IP2K_OLD:
2481 case EM_IP2K: return "Ubicom IP2xxx 8-bit microcontrollers";
2482 case EM_MAX: return "MAX Processor";
2483 case EM_CR: return "National Semiconductor CompactRISC";
2484 case EM_F2MC16: return "Fujitsu F2MC16";
2485 case EM_MSP430: return "Texas Instruments msp430 microcontroller";
2486 case EM_BLACKFIN: return "Analog Devices Blackfin";
2487 case EM_SE_C33: return "S1C33 Family of Seiko Epson processors";
2488 case EM_SEP: return "Sharp embedded microprocessor";
2489 case EM_ARCA: return "Arca RISC microprocessor";
2490 /* 110 */
2491 case EM_UNICORE: return "Unicore";
2492 case EM_EXCESS: return "eXcess 16/32/64-bit configurable embedded CPU";
2493 case EM_DXP: return "Icera Semiconductor Inc. Deep Execution Processor";
2494 case EM_ALTERA_NIOS2: return "Altera Nios II";
2495 case EM_CRX: return "National Semiconductor CRX microprocessor";
2496 case EM_XGATE: return "Motorola XGATE embedded processor";
2497 case EM_C166:
2498 case EM_XC16X: return "Infineon Technologies xc16x";
2499 case EM_M16C: return "Renesas M16C series microprocessors";
2500 case EM_DSPIC30F: return "Microchip Technology dsPIC30F Digital Signal Controller";
2501 case EM_CE: return "Freescale Communication Engine RISC core";
2502 /* 120 */
2503 case EM_M32C: return "Renesas M32c";
2504 /* 130 */
2505 case EM_TSK3000: return "Altium TSK3000 core";
2506 case EM_RS08: return "Freescale RS08 embedded processor";
2507 case EM_ECOG2: return "Cyan Technology eCOG2 microprocessor";
2508 case EM_SCORE: return "SUNPLUS S+Core";
2509 case EM_DSP24: return "New Japan Radio (NJR) 24-bit DSP Processor";
2510 case EM_VIDEOCORE3: return "Broadcom VideoCore III processor";
2511 case EM_LATTICEMICO32: return "Lattice Mico32";
2512 case EM_SE_C17: return "Seiko Epson C17 family";
2513 /* 140 */
2514 case EM_TI_C6000: return "Texas Instruments TMS320C6000 DSP family";
2515 case EM_TI_C2000: return "Texas Instruments TMS320C2000 DSP family";
2516 case EM_TI_C5500: return "Texas Instruments TMS320C55x DSP family";
2517 case EM_TI_PRU: return "TI PRU I/O processor";
2518 /* 160 */
2519 case EM_MMDSP_PLUS: return "STMicroelectronics 64bit VLIW Data Signal Processor";
2520 case EM_CYPRESS_M8C: return "Cypress M8C microprocessor";
2521 case EM_R32C: return "Renesas R32C series microprocessors";
2522 case EM_TRIMEDIA: return "NXP Semiconductors TriMedia architecture family";
2523 case EM_QDSP6: return "QUALCOMM DSP6 Processor";
2524 case EM_8051: return "Intel 8051 and variants";
2525 case EM_STXP7X: return "STMicroelectronics STxP7x family";
2526 case EM_NDS32: return "Andes Technology compact code size embedded RISC processor family";
2527 case EM_ECOG1X: return "Cyan Technology eCOG1X family";
2528 case EM_MAXQ30: return "Dallas Semiconductor MAXQ30 Core microcontrollers";
2529 /* 170 */
2530 case EM_XIMO16: return "New Japan Radio (NJR) 16-bit DSP Processor";
2531 case EM_MANIK: return "M2000 Reconfigurable RISC Microprocessor";
2532 case EM_CRAYNV2: return "Cray Inc. NV2 vector architecture";
2533 case EM_RX: return "Renesas RX";
2534 case EM_METAG: return "Imagination Technologies Meta processor architecture";
2535 case EM_MCST_ELBRUS: return "MCST Elbrus general purpose hardware architecture";
2536 case EM_ECOG16: return "Cyan Technology eCOG16 family";
2537 case EM_CR16:
2538 case EM_MICROBLAZE:
2539 case EM_MICROBLAZE_OLD: return "Xilinx MicroBlaze";
2540 case EM_ETPU: return "Freescale Extended Time Processing Unit";
2541 case EM_SLE9X: return "Infineon Technologies SLE9X core";
2542 /* 180 */
2543 case EM_L1OM: return "Intel L1OM";
2544 case EM_K1OM: return "Intel K1OM";
2545 case EM_INTEL182: return "Intel (reserved)";
2546 case EM_AARCH64: return "AArch64";
2547 case EM_ARM184: return "ARM (reserved)";
2548 case EM_AVR32: return "Atmel Corporation 32-bit microprocessor";
2549 case EM_STM8: return "STMicroeletronics STM8 8-bit microcontroller";
2550 case EM_TILE64: return "Tilera TILE64 multicore architecture family";
2551 case EM_TILEPRO: return "Tilera TILEPro multicore architecture family";
2552 /* 190 */
2553 case EM_CUDA: return "NVIDIA CUDA architecture";
2554 case EM_TILEGX: return "Tilera TILE-Gx multicore architecture family";
2555 case EM_CLOUDSHIELD: return "CloudShield architecture family";
2556 case EM_COREA_1ST: return "KIPO-KAIST Core-A 1st generation processor family";
2557 case EM_COREA_2ND: return "KIPO-KAIST Core-A 2nd generation processor family";
2558 case EM_ARC_COMPACT2: return "ARCv2";
2559 case EM_OPEN8: return "Open8 8-bit RISC soft processor core";
2560 case EM_RL78: return "Renesas RL78";
2561 case EM_VIDEOCORE5: return "Broadcom VideoCore V processor";
2562 case EM_78K0R: return "Renesas 78K0R";
2563 /* 200 */
2564 case EM_56800EX: return "Freescale 56800EX Digital Signal Controller (DSC)";
2565 case EM_BA1: return "Beyond BA1 CPU architecture";
2566 case EM_BA2: return "Beyond BA2 CPU architecture";
2567 case EM_XCORE: return "XMOS xCORE processor family";
2568 case EM_MCHP_PIC: return "Microchip 8-bit PIC(r) family";
2569 case EM_INTELGT: return "Intel Graphics Technology";
2570 /* 210 */
2571 case EM_KM32: return "KM211 KM32 32-bit processor";
2572 case EM_KMX32: return "KM211 KMX32 32-bit processor";
2573 case EM_KMX16: return "KM211 KMX16 16-bit processor";
2574 case EM_KMX8: return "KM211 KMX8 8-bit processor";
2575 case EM_KVARC: return "KM211 KVARC processor";
2576 case EM_CDP: return "Paneve CDP architecture family";
2577 case EM_COGE: return "Cognitive Smart Memory Processor";
2578 case EM_COOL: return "Bluechip Systems CoolEngine";
2579 case EM_NORC: return "Nanoradio Optimized RISC";
2580 case EM_CSR_KALIMBA: return "CSR Kalimba architecture family";
2581 /* 220 */
2582 case EM_Z80: return "Zilog Z80";
2583 case EM_VISIUM: return "CDS VISIUMcore processor";
2584 case EM_FT32: return "FTDI Chip FT32";
2585 case EM_MOXIE: return "Moxie";
2586 case EM_AMDGPU: return "AMD GPU";
2587 /* 230 (all reserved) */
2588 /* 240 */
2589 case EM_RISCV: return "RISC-V";
2590 case EM_LANAI: return "Lanai 32-bit processor";
2591 case EM_CEVA: return "CEVA Processor Architecture Family";
2592 case EM_CEVA_X2: return "CEVA X2 Processor Family";
2593 case EM_BPF: return "Linux BPF";
2594 case EM_GRAPHCORE_IPU: return "Graphcore Intelligent Processing Unit";
2595 case EM_IMG1: return "Imagination Technologies";
2596 /* 250 */
2597 case EM_NFP: return "Netronome Flow Processor";
2598 case EM_VE: return "NEC Vector Engine";
2599 case EM_CSKY: return "C-SKY";
2600 case EM_ARC_COMPACT3_64: return "Synopsys ARCv2.3 64-bit";
2601 case EM_MCS6502: return "MOS Technology MCS 6502 processor";
2602 case EM_ARC_COMPACT3: return "Synopsys ARCv2.3 32-bit";
2603 case EM_KVX: return "Kalray VLIW core of the MPPA processor family";
2604 case EM_65816: return "WDC 65816/65C816";
2605 case EM_LOONGARCH: return "LoongArch";
2606 case EM_KF32: return "ChipON KungFu32";
2607
2608 /* Large numbers... */
2609 case EM_MT: return "Morpho Techologies MT processor";
2610 case EM_ALPHA: return "Alpha";
2611 case EM_WEBASSEMBLY: return "Web Assembly";
2612 case EM_DLX: return "OpenDLX";
2613 case EM_XSTORMY16: return "Sanyo XStormy16 CPU core";
2614 case EM_IQ2000: return "Vitesse IQ2000";
2615 case EM_M32C_OLD:
2616 case EM_NIOS32: return "Altera Nios";
2617 case EM_CYGNUS_MEP: return "Toshiba MeP Media Engine";
2618 case EM_ADAPTEVA_EPIPHANY: return "Adapteva EPIPHANY";
2619 case EM_CYGNUS_FRV: return "Fujitsu FR-V";
2620 case EM_S12Z: return "Freescale S12Z";
2621
2622 default:
2623 snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
2624 return buff;
2625 }
2626 }
2627
2628 static void
2629 decode_ARC_machine_flags (unsigned e_flags, unsigned e_machine, char buf[])
2630 {
2631 /* ARC has two machine types EM_ARC_COMPACT and EM_ARC_COMPACT2. Some
2632 other compilers don't specify an architecture type in the e_flags, and
2633 instead use EM_ARC_COMPACT for old ARC600, ARC601, and ARC700
2634 architectures, and switch to EM_ARC_COMPACT2 for newer ARCEM and ARCHS
2635 architectures.
2636
2637 Th GNU tools follows this use of EM_ARC_COMPACT and EM_ARC_COMPACT2,
2638 but also sets a specific architecture type in the e_flags field.
2639
2640 However, when decoding the flags we don't worry if we see an
2641 unexpected pairing, for example EM_ARC_COMPACT machine type, with
2642 ARCEM architecture type. */
2643
2644 switch (e_flags & EF_ARC_MACH_MSK)
2645 {
2646 /* We only expect these to occur for EM_ARC_COMPACT2. */
2647 case EF_ARC_CPU_ARCV2EM:
2648 strcat (buf, ", ARC EM");
2649 break;
2650 case EF_ARC_CPU_ARCV2HS:
2651 strcat (buf, ", ARC HS");
2652 break;
2653
2654 /* We only expect these to occur for EM_ARC_COMPACT. */
2655 case E_ARC_MACH_ARC600:
2656 strcat (buf, ", ARC600");
2657 break;
2658 case E_ARC_MACH_ARC601:
2659 strcat (buf, ", ARC601");
2660 break;
2661 case E_ARC_MACH_ARC700:
2662 strcat (buf, ", ARC700");
2663 break;
2664
2665 /* The only times we should end up here are (a) A corrupt ELF, (b) A
2666 new ELF with new architecture being read by an old version of
2667 readelf, or (c) An ELF built with non-GNU compiler that does not
2668 set the architecture in the e_flags. */
2669 default:
2670 if (e_machine == EM_ARC_COMPACT)
2671 strcat (buf, ", Unknown ARCompact");
2672 else
2673 strcat (buf, ", Unknown ARC");
2674 break;
2675 }
2676
2677 switch (e_flags & EF_ARC_OSABI_MSK)
2678 {
2679 case E_ARC_OSABI_ORIG:
2680 strcat (buf, ", (ABI:legacy)");
2681 break;
2682 case E_ARC_OSABI_V2:
2683 strcat (buf, ", (ABI:v2)");
2684 break;
2685 /* Only upstream 3.9+ kernels will support ARCv2 ISA. */
2686 case E_ARC_OSABI_V3:
2687 strcat (buf, ", v3 no-legacy-syscalls ABI");
2688 break;
2689 case E_ARC_OSABI_V4:
2690 strcat (buf, ", v4 ABI");
2691 break;
2692 default:
2693 strcat (buf, ", unrecognised ARC OSABI flag");
2694 break;
2695 }
2696 }
2697
2698 static void
2699 decode_ARM_machine_flags (unsigned e_flags, char buf[])
2700 {
2701 unsigned eabi;
2702 bool unknown = false;
2703
2704 eabi = EF_ARM_EABI_VERSION (e_flags);
2705 e_flags &= ~ EF_ARM_EABIMASK;
2706
2707 /* Handle "generic" ARM flags. */
2708 if (e_flags & EF_ARM_RELEXEC)
2709 {
2710 strcat (buf, ", relocatable executable");
2711 e_flags &= ~ EF_ARM_RELEXEC;
2712 }
2713
2714 if (e_flags & EF_ARM_PIC)
2715 {
2716 strcat (buf, ", position independent");
2717 e_flags &= ~ EF_ARM_PIC;
2718 }
2719
2720 /* Now handle EABI specific flags. */
2721 switch (eabi)
2722 {
2723 default:
2724 strcat (buf, ", <unrecognized EABI>");
2725 if (e_flags)
2726 unknown = true;
2727 break;
2728
2729 case EF_ARM_EABI_VER1:
2730 strcat (buf, ", Version1 EABI");
2731 while (e_flags)
2732 {
2733 unsigned flag;
2734
2735 /* Process flags one bit at a time. */
2736 flag = e_flags & - e_flags;
2737 e_flags &= ~ flag;
2738
2739 switch (flag)
2740 {
2741 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
2742 strcat (buf, ", sorted symbol tables");
2743 break;
2744
2745 default:
2746 unknown = true;
2747 break;
2748 }
2749 }
2750 break;
2751
2752 case EF_ARM_EABI_VER2:
2753 strcat (buf, ", Version2 EABI");
2754 while (e_flags)
2755 {
2756 unsigned flag;
2757
2758 /* Process flags one bit at a time. */
2759 flag = e_flags & - e_flags;
2760 e_flags &= ~ flag;
2761
2762 switch (flag)
2763 {
2764 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
2765 strcat (buf, ", sorted symbol tables");
2766 break;
2767
2768 case EF_ARM_DYNSYMSUSESEGIDX:
2769 strcat (buf, ", dynamic symbols use segment index");
2770 break;
2771
2772 case EF_ARM_MAPSYMSFIRST:
2773 strcat (buf, ", mapping symbols precede others");
2774 break;
2775
2776 default:
2777 unknown = true;
2778 break;
2779 }
2780 }
2781 break;
2782
2783 case EF_ARM_EABI_VER3:
2784 strcat (buf, ", Version3 EABI");
2785 break;
2786
2787 case EF_ARM_EABI_VER4:
2788 strcat (buf, ", Version4 EABI");
2789 while (e_flags)
2790 {
2791 unsigned flag;
2792
2793 /* Process flags one bit at a time. */
2794 flag = e_flags & - e_flags;
2795 e_flags &= ~ flag;
2796
2797 switch (flag)
2798 {
2799 case EF_ARM_BE8:
2800 strcat (buf, ", BE8");
2801 break;
2802
2803 case EF_ARM_LE8:
2804 strcat (buf, ", LE8");
2805 break;
2806
2807 default:
2808 unknown = true;
2809 break;
2810 }
2811 }
2812 break;
2813
2814 case EF_ARM_EABI_VER5:
2815 strcat (buf, ", Version5 EABI");
2816 while (e_flags)
2817 {
2818 unsigned flag;
2819
2820 /* Process flags one bit at a time. */
2821 flag = e_flags & - e_flags;
2822 e_flags &= ~ flag;
2823
2824 switch (flag)
2825 {
2826 case EF_ARM_BE8:
2827 strcat (buf, ", BE8");
2828 break;
2829
2830 case EF_ARM_LE8:
2831 strcat (buf, ", LE8");
2832 break;
2833
2834 case EF_ARM_ABI_FLOAT_SOFT: /* Conflicts with EF_ARM_SOFT_FLOAT. */
2835 strcat (buf, ", soft-float ABI");
2836 break;
2837
2838 case EF_ARM_ABI_FLOAT_HARD: /* Conflicts with EF_ARM_VFP_FLOAT. */
2839 strcat (buf, ", hard-float ABI");
2840 break;
2841
2842 default:
2843 unknown = true;
2844 break;
2845 }
2846 }
2847 break;
2848
2849 case EF_ARM_EABI_UNKNOWN:
2850 strcat (buf, ", GNU EABI");
2851 while (e_flags)
2852 {
2853 unsigned flag;
2854
2855 /* Process flags one bit at a time. */
2856 flag = e_flags & - e_flags;
2857 e_flags &= ~ flag;
2858
2859 switch (flag)
2860 {
2861 case EF_ARM_INTERWORK:
2862 strcat (buf, ", interworking enabled");
2863 break;
2864
2865 case EF_ARM_APCS_26:
2866 strcat (buf, ", uses APCS/26");
2867 break;
2868
2869 case EF_ARM_APCS_FLOAT:
2870 strcat (buf, ", uses APCS/float");
2871 break;
2872
2873 case EF_ARM_PIC:
2874 strcat (buf, ", position independent");
2875 break;
2876
2877 case EF_ARM_ALIGN8:
2878 strcat (buf, ", 8 bit structure alignment");
2879 break;
2880
2881 case EF_ARM_NEW_ABI:
2882 strcat (buf, ", uses new ABI");
2883 break;
2884
2885 case EF_ARM_OLD_ABI:
2886 strcat (buf, ", uses old ABI");
2887 break;
2888
2889 case EF_ARM_SOFT_FLOAT:
2890 strcat (buf, ", software FP");
2891 break;
2892
2893 case EF_ARM_VFP_FLOAT:
2894 strcat (buf, ", VFP");
2895 break;
2896
2897 case EF_ARM_MAVERICK_FLOAT:
2898 strcat (buf, ", Maverick FP");
2899 break;
2900
2901 default:
2902 unknown = true;
2903 break;
2904 }
2905 }
2906 }
2907
2908 if (unknown)
2909 strcat (buf,_(", <unknown>"));
2910 }
2911
2912 static void
2913 decode_AVR_machine_flags (unsigned e_flags, char buf[], size_t size)
2914 {
2915 --size; /* Leave space for null terminator. */
2916
2917 switch (e_flags & EF_AVR_MACH)
2918 {
2919 case E_AVR_MACH_AVR1:
2920 strncat (buf, ", avr:1", size);
2921 break;
2922 case E_AVR_MACH_AVR2:
2923 strncat (buf, ", avr:2", size);
2924 break;
2925 case E_AVR_MACH_AVR25:
2926 strncat (buf, ", avr:25", size);
2927 break;
2928 case E_AVR_MACH_AVR3:
2929 strncat (buf, ", avr:3", size);
2930 break;
2931 case E_AVR_MACH_AVR31:
2932 strncat (buf, ", avr:31", size);
2933 break;
2934 case E_AVR_MACH_AVR35:
2935 strncat (buf, ", avr:35", size);
2936 break;
2937 case E_AVR_MACH_AVR4:
2938 strncat (buf, ", avr:4", size);
2939 break;
2940 case E_AVR_MACH_AVR5:
2941 strncat (buf, ", avr:5", size);
2942 break;
2943 case E_AVR_MACH_AVR51:
2944 strncat (buf, ", avr:51", size);
2945 break;
2946 case E_AVR_MACH_AVR6:
2947 strncat (buf, ", avr:6", size);
2948 break;
2949 case E_AVR_MACH_AVRTINY:
2950 strncat (buf, ", avr:100", size);
2951 break;
2952 case E_AVR_MACH_XMEGA1:
2953 strncat (buf, ", avr:101", size);
2954 break;
2955 case E_AVR_MACH_XMEGA2:
2956 strncat (buf, ", avr:102", size);
2957 break;
2958 case E_AVR_MACH_XMEGA3:
2959 strncat (buf, ", avr:103", size);
2960 break;
2961 case E_AVR_MACH_XMEGA4:
2962 strncat (buf, ", avr:104", size);
2963 break;
2964 case E_AVR_MACH_XMEGA5:
2965 strncat (buf, ", avr:105", size);
2966 break;
2967 case E_AVR_MACH_XMEGA6:
2968 strncat (buf, ", avr:106", size);
2969 break;
2970 case E_AVR_MACH_XMEGA7:
2971 strncat (buf, ", avr:107", size);
2972 break;
2973 default:
2974 strncat (buf, ", avr:<unknown>", size);
2975 break;
2976 }
2977
2978 size -= strlen (buf);
2979 if (e_flags & EF_AVR_LINKRELAX_PREPARED)
2980 strncat (buf, ", link-relax", size);
2981 }
2982
2983 static void
2984 decode_NDS32_machine_flags (unsigned e_flags, char buf[], size_t size)
2985 {
2986 unsigned abi;
2987 unsigned arch;
2988 unsigned config;
2989 unsigned version;
2990 bool has_fpu = false;
2991 unsigned int r = 0;
2992
2993 static const char *ABI_STRINGS[] =
2994 {
2995 "ABI v0", /* use r5 as return register; only used in N1213HC */
2996 "ABI v1", /* use r0 as return register */
2997 "ABI v2", /* use r0 as return register and don't reserve 24 bytes for arguments */
2998 "ABI v2fp", /* for FPU */
2999 "AABI",
3000 "ABI2 FP+"
3001 };
3002 static const char *VER_STRINGS[] =
3003 {
3004 "Andes ELF V1.3 or older",
3005 "Andes ELF V1.3.1",
3006 "Andes ELF V1.4"
3007 };
3008 static const char *ARCH_STRINGS[] =
3009 {
3010 "",
3011 "Andes Star v1.0",
3012 "Andes Star v2.0",
3013 "Andes Star v3.0",
3014 "Andes Star v3.0m"
3015 };
3016
3017 abi = EF_NDS_ABI & e_flags;
3018 arch = EF_NDS_ARCH & e_flags;
3019 config = EF_NDS_INST & e_flags;
3020 version = EF_NDS32_ELF_VERSION & e_flags;
3021
3022 memset (buf, 0, size);
3023
3024 switch (abi)
3025 {
3026 case E_NDS_ABI_V0:
3027 case E_NDS_ABI_V1:
3028 case E_NDS_ABI_V2:
3029 case E_NDS_ABI_V2FP:
3030 case E_NDS_ABI_AABI:
3031 case E_NDS_ABI_V2FP_PLUS:
3032 /* In case there are holes in the array. */
3033 r += snprintf (buf + r, size - r, ", %s", ABI_STRINGS[abi >> EF_NDS_ABI_SHIFT]);
3034 break;
3035
3036 default:
3037 r += snprintf (buf + r, size - r, ", <unrecognized ABI>");
3038 break;
3039 }
3040
3041 switch (version)
3042 {
3043 case E_NDS32_ELF_VER_1_2:
3044 case E_NDS32_ELF_VER_1_3:
3045 case E_NDS32_ELF_VER_1_4:
3046 r += snprintf (buf + r, size - r, ", %s", VER_STRINGS[version >> EF_NDS32_ELF_VERSION_SHIFT]);
3047 break;
3048
3049 default:
3050 r += snprintf (buf + r, size - r, ", <unrecognized ELF version number>");
3051 break;
3052 }
3053
3054 if (E_NDS_ABI_V0 == abi)
3055 {
3056 /* OLD ABI; only used in N1213HC, has performance extension 1. */
3057 r += snprintf (buf + r, size - r, ", Andes Star v1.0, N1213HC, MAC, PERF1");
3058 if (arch == E_NDS_ARCH_STAR_V1_0)
3059 r += snprintf (buf + r, size -r, ", 16b"); /* has 16-bit instructions */
3060 return;
3061 }
3062
3063 switch (arch)
3064 {
3065 case E_NDS_ARCH_STAR_V1_0:
3066 case E_NDS_ARCH_STAR_V2_0:
3067 case E_NDS_ARCH_STAR_V3_0:
3068 case E_NDS_ARCH_STAR_V3_M:
3069 r += snprintf (buf + r, size - r, ", %s", ARCH_STRINGS[arch >> EF_NDS_ARCH_SHIFT]);
3070 break;
3071
3072 default:
3073 r += snprintf (buf + r, size - r, ", <unrecognized architecture>");
3074 /* ARCH version determines how the e_flags are interpreted.
3075 If it is unknown, we cannot proceed. */
3076 return;
3077 }
3078
3079 /* Newer ABI; Now handle architecture specific flags. */
3080 if (arch == E_NDS_ARCH_STAR_V1_0)
3081 {
3082 if (config & E_NDS32_HAS_MFUSR_PC_INST)
3083 r += snprintf (buf + r, size -r, ", MFUSR_PC");
3084
3085 if (!(config & E_NDS32_HAS_NO_MAC_INST))
3086 r += snprintf (buf + r, size -r, ", MAC");
3087
3088 if (config & E_NDS32_HAS_DIV_INST)
3089 r += snprintf (buf + r, size -r, ", DIV");
3090
3091 if (config & E_NDS32_HAS_16BIT_INST)
3092 r += snprintf (buf + r, size -r, ", 16b");
3093 }
3094 else
3095 {
3096 if (config & E_NDS32_HAS_MFUSR_PC_INST)
3097 {
3098 if (version <= E_NDS32_ELF_VER_1_3)
3099 r += snprintf (buf + r, size -r, ", [B8]");
3100 else
3101 r += snprintf (buf + r, size -r, ", EX9");
3102 }
3103
3104 if (config & E_NDS32_HAS_MAC_DX_INST)
3105 r += snprintf (buf + r, size -r, ", MAC_DX");
3106
3107 if (config & E_NDS32_HAS_DIV_DX_INST)
3108 r += snprintf (buf + r, size -r, ", DIV_DX");
3109
3110 if (config & E_NDS32_HAS_16BIT_INST)
3111 {
3112 if (version <= E_NDS32_ELF_VER_1_3)
3113 r += snprintf (buf + r, size -r, ", 16b");
3114 else
3115 r += snprintf (buf + r, size -r, ", IFC");
3116 }
3117 }
3118
3119 if (config & E_NDS32_HAS_EXT_INST)
3120 r += snprintf (buf + r, size -r, ", PERF1");
3121
3122 if (config & E_NDS32_HAS_EXT2_INST)
3123 r += snprintf (buf + r, size -r, ", PERF2");
3124
3125 if (config & E_NDS32_HAS_FPU_INST)
3126 {
3127 has_fpu = true;
3128 r += snprintf (buf + r, size -r, ", FPU_SP");
3129 }
3130
3131 if (config & E_NDS32_HAS_FPU_DP_INST)
3132 {
3133 has_fpu = true;
3134 r += snprintf (buf + r, size -r, ", FPU_DP");
3135 }
3136
3137 if (config & E_NDS32_HAS_FPU_MAC_INST)
3138 {
3139 has_fpu = true;
3140 r += snprintf (buf + r, size -r, ", FPU_MAC");
3141 }
3142
3143 if (has_fpu)
3144 {
3145 switch ((config & E_NDS32_FPU_REG_CONF) >> E_NDS32_FPU_REG_CONF_SHIFT)
3146 {
3147 case E_NDS32_FPU_REG_8SP_4DP:
3148 r += snprintf (buf + r, size -r, ", FPU_REG:8/4");
3149 break;
3150 case E_NDS32_FPU_REG_16SP_8DP:
3151 r += snprintf (buf + r, size -r, ", FPU_REG:16/8");
3152 break;
3153 case E_NDS32_FPU_REG_32SP_16DP:
3154 r += snprintf (buf + r, size -r, ", FPU_REG:32/16");
3155 break;
3156 case E_NDS32_FPU_REG_32SP_32DP:
3157 r += snprintf (buf + r, size -r, ", FPU_REG:32/32");
3158 break;
3159 }
3160 }
3161
3162 if (config & E_NDS32_HAS_AUDIO_INST)
3163 r += snprintf (buf + r, size -r, ", AUDIO");
3164
3165 if (config & E_NDS32_HAS_STRING_INST)
3166 r += snprintf (buf + r, size -r, ", STR");
3167
3168 if (config & E_NDS32_HAS_REDUCED_REGS)
3169 r += snprintf (buf + r, size -r, ", 16REG");
3170
3171 if (config & E_NDS32_HAS_VIDEO_INST)
3172 {
3173 if (version <= E_NDS32_ELF_VER_1_3)
3174 r += snprintf (buf + r, size -r, ", VIDEO");
3175 else
3176 r += snprintf (buf + r, size -r, ", SATURATION");
3177 }
3178
3179 if (config & E_NDS32_HAS_ENCRIPT_INST)
3180 r += snprintf (buf + r, size -r, ", ENCRP");
3181
3182 if (config & E_NDS32_HAS_L2C_INST)
3183 r += snprintf (buf + r, size -r, ", L2C");
3184 }
3185
3186 static char *
3187 get_machine_flags (Filedata * filedata, unsigned e_flags, unsigned e_machine)
3188 {
3189 static char buf[1024];
3190
3191 buf[0] = '\0';
3192
3193 if (e_flags)
3194 {
3195 switch (e_machine)
3196 {
3197 default:
3198 break;
3199
3200 case EM_ARC_COMPACT2:
3201 case EM_ARC_COMPACT:
3202 decode_ARC_machine_flags (e_flags, e_machine, buf);
3203 break;
3204
3205 case EM_ARM:
3206 decode_ARM_machine_flags (e_flags, buf);
3207 break;
3208
3209 case EM_AVR:
3210 decode_AVR_machine_flags (e_flags, buf, sizeof buf);
3211 break;
3212
3213 case EM_BLACKFIN:
3214 if (e_flags & EF_BFIN_PIC)
3215 strcat (buf, ", PIC");
3216
3217 if (e_flags & EF_BFIN_FDPIC)
3218 strcat (buf, ", FDPIC");
3219
3220 if (e_flags & EF_BFIN_CODE_IN_L1)
3221 strcat (buf, ", code in L1");
3222
3223 if (e_flags & EF_BFIN_DATA_IN_L1)
3224 strcat (buf, ", data in L1");
3225
3226 break;
3227
3228 case EM_CYGNUS_FRV:
3229 switch (e_flags & EF_FRV_CPU_MASK)
3230 {
3231 case EF_FRV_CPU_GENERIC:
3232 break;
3233
3234 default:
3235 strcat (buf, ", fr???");
3236 break;
3237
3238 case EF_FRV_CPU_FR300:
3239 strcat (buf, ", fr300");
3240 break;
3241
3242 case EF_FRV_CPU_FR400:
3243 strcat (buf, ", fr400");
3244 break;
3245 case EF_FRV_CPU_FR405:
3246 strcat (buf, ", fr405");
3247 break;
3248
3249 case EF_FRV_CPU_FR450:
3250 strcat (buf, ", fr450");
3251 break;
3252
3253 case EF_FRV_CPU_FR500:
3254 strcat (buf, ", fr500");
3255 break;
3256 case EF_FRV_CPU_FR550:
3257 strcat (buf, ", fr550");
3258 break;
3259
3260 case EF_FRV_CPU_SIMPLE:
3261 strcat (buf, ", simple");
3262 break;
3263 case EF_FRV_CPU_TOMCAT:
3264 strcat (buf, ", tomcat");
3265 break;
3266 }
3267 break;
3268
3269 case EM_68K:
3270 if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
3271 strcat (buf, ", m68000");
3272 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
3273 strcat (buf, ", cpu32");
3274 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
3275 strcat (buf, ", fido_a");
3276 else
3277 {
3278 char const * isa = _("unknown");
3279 char const * mac = _("unknown mac");
3280 char const * additional = NULL;
3281
3282 switch (e_flags & EF_M68K_CF_ISA_MASK)
3283 {
3284 case EF_M68K_CF_ISA_A_NODIV:
3285 isa = "A";
3286 additional = ", nodiv";
3287 break;
3288 case EF_M68K_CF_ISA_A:
3289 isa = "A";
3290 break;
3291 case EF_M68K_CF_ISA_A_PLUS:
3292 isa = "A+";
3293 break;
3294 case EF_M68K_CF_ISA_B_NOUSP:
3295 isa = "B";
3296 additional = ", nousp";
3297 break;
3298 case EF_M68K_CF_ISA_B:
3299 isa = "B";
3300 break;
3301 case EF_M68K_CF_ISA_C:
3302 isa = "C";
3303 break;
3304 case EF_M68K_CF_ISA_C_NODIV:
3305 isa = "C";
3306 additional = ", nodiv";
3307 break;
3308 }
3309 strcat (buf, ", cf, isa ");
3310 strcat (buf, isa);
3311 if (additional)
3312 strcat (buf, additional);
3313 if (e_flags & EF_M68K_CF_FLOAT)
3314 strcat (buf, ", float");
3315 switch (e_flags & EF_M68K_CF_MAC_MASK)
3316 {
3317 case 0:
3318 mac = NULL;
3319 break;
3320 case EF_M68K_CF_MAC:
3321 mac = "mac";
3322 break;
3323 case EF_M68K_CF_EMAC:
3324 mac = "emac";
3325 break;
3326 case EF_M68K_CF_EMAC_B:
3327 mac = "emac_b";
3328 break;
3329 }
3330 if (mac)
3331 {
3332 strcat (buf, ", ");
3333 strcat (buf, mac);
3334 }
3335 }
3336 break;
3337
3338 case EM_CYGNUS_MEP:
3339 switch (e_flags & EF_MEP_CPU_MASK)
3340 {
3341 case EF_MEP_CPU_MEP: strcat (buf, ", generic MeP"); break;
3342 case EF_MEP_CPU_C2: strcat (buf, ", MeP C2"); break;
3343 case EF_MEP_CPU_C3: strcat (buf, ", MeP C3"); break;
3344 case EF_MEP_CPU_C4: strcat (buf, ", MeP C4"); break;
3345 case EF_MEP_CPU_C5: strcat (buf, ", MeP C5"); break;
3346 case EF_MEP_CPU_H1: strcat (buf, ", MeP H1"); break;
3347 default: strcat (buf, _(", <unknown MeP cpu type>")); break;
3348 }
3349
3350 switch (e_flags & EF_MEP_COP_MASK)
3351 {
3352 case EF_MEP_COP_NONE: break;
3353 case EF_MEP_COP_AVC: strcat (buf, ", AVC coprocessor"); break;
3354 case EF_MEP_COP_AVC2: strcat (buf, ", AVC2 coprocessor"); break;
3355 case EF_MEP_COP_FMAX: strcat (buf, ", FMAX coprocessor"); break;
3356 case EF_MEP_COP_IVC2: strcat (buf, ", IVC2 coprocessor"); break;
3357 default: strcat (buf, _("<unknown MeP copro type>")); break;
3358 }
3359
3360 if (e_flags & EF_MEP_LIBRARY)
3361 strcat (buf, ", Built for Library");
3362
3363 if (e_flags & EF_MEP_INDEX_MASK)
3364 sprintf (buf + strlen (buf), ", Configuration Index: %#x",
3365 e_flags & EF_MEP_INDEX_MASK);
3366
3367 if (e_flags & ~ EF_MEP_ALL_FLAGS)
3368 sprintf (buf + strlen (buf), _(", unknown flags bits: %#x"),
3369 e_flags & ~ EF_MEP_ALL_FLAGS);
3370 break;
3371
3372 case EM_PPC:
3373 if (e_flags & EF_PPC_EMB)
3374 strcat (buf, ", emb");
3375
3376 if (e_flags & EF_PPC_RELOCATABLE)
3377 strcat (buf, _(", relocatable"));
3378
3379 if (e_flags & EF_PPC_RELOCATABLE_LIB)
3380 strcat (buf, _(", relocatable-lib"));
3381 break;
3382
3383 case EM_PPC64:
3384 if (e_flags & EF_PPC64_ABI)
3385 {
3386 char abi[] = ", abiv0";
3387
3388 abi[6] += e_flags & EF_PPC64_ABI;
3389 strcat (buf, abi);
3390 }
3391 break;
3392
3393 case EM_V800:
3394 if ((e_flags & EF_RH850_ABI) == EF_RH850_ABI)
3395 strcat (buf, ", RH850 ABI");
3396
3397 if (e_flags & EF_V800_850E3)
3398 strcat (buf, ", V3 architecture");
3399
3400 if ((e_flags & (EF_RH850_FPU_DOUBLE | EF_RH850_FPU_SINGLE)) == 0)
3401 strcat (buf, ", FPU not used");
3402
3403 if ((e_flags & (EF_RH850_REGMODE22 | EF_RH850_REGMODE32)) == 0)
3404 strcat (buf, ", regmode: COMMON");
3405
3406 if ((e_flags & (EF_RH850_GP_FIX | EF_RH850_GP_NOFIX)) == 0)
3407 strcat (buf, ", r4 not used");
3408
3409 if ((e_flags & (EF_RH850_EP_FIX | EF_RH850_EP_NOFIX)) == 0)
3410 strcat (buf, ", r30 not used");
3411
3412 if ((e_flags & (EF_RH850_TP_FIX | EF_RH850_TP_NOFIX)) == 0)
3413 strcat (buf, ", r5 not used");
3414
3415 if ((e_flags & (EF_RH850_REG2_RESERVE | EF_RH850_REG2_NORESERVE)) == 0)
3416 strcat (buf, ", r2 not used");
3417
3418 for (e_flags &= 0xFFFF; e_flags; e_flags &= ~ (e_flags & - e_flags))
3419 {
3420 switch (e_flags & - e_flags)
3421 {
3422 case EF_RH850_FPU_DOUBLE: strcat (buf, ", double precision FPU"); break;
3423 case EF_RH850_FPU_SINGLE: strcat (buf, ", single precision FPU"); break;
3424 case EF_RH850_REGMODE22: strcat (buf, ", regmode:22"); break;
3425 case EF_RH850_REGMODE32: strcat (buf, ", regmode:23"); break;
3426 case EF_RH850_GP_FIX: strcat (buf, ", r4 fixed"); break;
3427 case EF_RH850_GP_NOFIX: strcat (buf, ", r4 free"); break;
3428 case EF_RH850_EP_FIX: strcat (buf, ", r30 fixed"); break;
3429 case EF_RH850_EP_NOFIX: strcat (buf, ", r30 free"); break;
3430 case EF_RH850_TP_FIX: strcat (buf, ", r5 fixed"); break;
3431 case EF_RH850_TP_NOFIX: strcat (buf, ", r5 free"); break;
3432 case EF_RH850_REG2_RESERVE: strcat (buf, ", r2 fixed"); break;
3433 case EF_RH850_REG2_NORESERVE: strcat (buf, ", r2 free"); break;
3434 default: break;
3435 }
3436 }
3437 break;
3438
3439 case EM_V850:
3440 case EM_CYGNUS_V850:
3441 switch (e_flags & EF_V850_ARCH)
3442 {
3443 case E_V850E3V5_ARCH:
3444 strcat (buf, ", v850e3v5");
3445 break;
3446 case E_V850E2V3_ARCH:
3447 strcat (buf, ", v850e2v3");
3448 break;
3449 case E_V850E2_ARCH:
3450 strcat (buf, ", v850e2");
3451 break;
3452 case E_V850E1_ARCH:
3453 strcat (buf, ", v850e1");
3454 break;
3455 case E_V850E_ARCH:
3456 strcat (buf, ", v850e");
3457 break;
3458 case E_V850_ARCH:
3459 strcat (buf, ", v850");
3460 break;
3461 default:
3462 strcat (buf, _(", unknown v850 architecture variant"));
3463 break;
3464 }
3465 break;
3466
3467 case EM_M32R:
3468 case EM_CYGNUS_M32R:
3469 if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
3470 strcat (buf, ", m32r");
3471 break;
3472
3473 case EM_MIPS:
3474 case EM_MIPS_RS3_LE:
3475 if (e_flags & EF_MIPS_NOREORDER)
3476 strcat (buf, ", noreorder");
3477
3478 if (e_flags & EF_MIPS_PIC)
3479 strcat (buf, ", pic");
3480
3481 if (e_flags & EF_MIPS_CPIC)
3482 strcat (buf, ", cpic");
3483
3484 if (e_flags & EF_MIPS_UCODE)
3485 strcat (buf, ", ugen_reserved");
3486
3487 if (e_flags & EF_MIPS_ABI2)
3488 strcat (buf, ", abi2");
3489
3490 if (e_flags & EF_MIPS_OPTIONS_FIRST)
3491 strcat (buf, ", odk first");
3492
3493 if (e_flags & EF_MIPS_32BITMODE)
3494 strcat (buf, ", 32bitmode");
3495
3496 if (e_flags & EF_MIPS_NAN2008)
3497 strcat (buf, ", nan2008");
3498
3499 if (e_flags & EF_MIPS_FP64)
3500 strcat (buf, ", fp64");
3501
3502 switch ((e_flags & EF_MIPS_MACH))
3503 {
3504 case E_MIPS_MACH_3900: strcat (buf, ", 3900"); break;
3505 case E_MIPS_MACH_4010: strcat (buf, ", 4010"); break;
3506 case E_MIPS_MACH_4100: strcat (buf, ", 4100"); break;
3507 case E_MIPS_MACH_4111: strcat (buf, ", 4111"); break;
3508 case E_MIPS_MACH_4120: strcat (buf, ", 4120"); break;
3509 case E_MIPS_MACH_4650: strcat (buf, ", 4650"); break;
3510 case E_MIPS_MACH_5400: strcat (buf, ", 5400"); break;
3511 case E_MIPS_MACH_5500: strcat (buf, ", 5500"); break;
3512 case E_MIPS_MACH_5900: strcat (buf, ", 5900"); break;
3513 case E_MIPS_MACH_SB1: strcat (buf, ", sb1"); break;
3514 case E_MIPS_MACH_9000: strcat (buf, ", 9000"); break;
3515 case E_MIPS_MACH_LS2E: strcat (buf, ", loongson-2e"); break;
3516 case E_MIPS_MACH_LS2F: strcat (buf, ", loongson-2f"); break;
3517 case E_MIPS_MACH_GS464: strcat (buf, ", gs464"); break;
3518 case E_MIPS_MACH_GS464E: strcat (buf, ", gs464e"); break;
3519 case E_MIPS_MACH_GS264E: strcat (buf, ", gs264e"); break;
3520 case E_MIPS_MACH_OCTEON: strcat (buf, ", octeon"); break;
3521 case E_MIPS_MACH_OCTEON2: strcat (buf, ", octeon2"); break;
3522 case E_MIPS_MACH_OCTEON3: strcat (buf, ", octeon3"); break;
3523 case E_MIPS_MACH_XLR: strcat (buf, ", xlr"); break;
3524 case E_MIPS_MACH_IAMR2: strcat (buf, ", interaptiv-mr2"); break;
3525 case 0:
3526 /* We simply ignore the field in this case to avoid confusion:
3527 MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
3528 extension. */
3529 break;
3530 default: strcat (buf, _(", unknown CPU")); break;
3531 }
3532
3533 switch ((e_flags & EF_MIPS_ABI))
3534 {
3535 case E_MIPS_ABI_O32: strcat (buf, ", o32"); break;
3536 case E_MIPS_ABI_O64: strcat (buf, ", o64"); break;
3537 case E_MIPS_ABI_EABI32: strcat (buf, ", eabi32"); break;
3538 case E_MIPS_ABI_EABI64: strcat (buf, ", eabi64"); break;
3539 case 0:
3540 /* We simply ignore the field in this case to avoid confusion:
3541 MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
3542 This means it is likely to be an o32 file, but not for
3543 sure. */
3544 break;
3545 default: strcat (buf, _(", unknown ABI")); break;
3546 }
3547
3548 if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
3549 strcat (buf, ", mdmx");
3550
3551 if (e_flags & EF_MIPS_ARCH_ASE_M16)
3552 strcat (buf, ", mips16");
3553
3554 if (e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
3555 strcat (buf, ", micromips");
3556
3557 switch ((e_flags & EF_MIPS_ARCH))
3558 {
3559 case E_MIPS_ARCH_1: strcat (buf, ", mips1"); break;
3560 case E_MIPS_ARCH_2: strcat (buf, ", mips2"); break;
3561 case E_MIPS_ARCH_3: strcat (buf, ", mips3"); break;
3562 case E_MIPS_ARCH_4: strcat (buf, ", mips4"); break;
3563 case E_MIPS_ARCH_5: strcat (buf, ", mips5"); break;
3564 case E_MIPS_ARCH_32: strcat (buf, ", mips32"); break;
3565 case E_MIPS_ARCH_32R2: strcat (buf, ", mips32r2"); break;
3566 case E_MIPS_ARCH_32R6: strcat (buf, ", mips32r6"); break;
3567 case E_MIPS_ARCH_64: strcat (buf, ", mips64"); break;
3568 case E_MIPS_ARCH_64R2: strcat (buf, ", mips64r2"); break;
3569 case E_MIPS_ARCH_64R6: strcat (buf, ", mips64r6"); break;
3570 default: strcat (buf, _(", unknown ISA")); break;
3571 }
3572 break;
3573
3574 case EM_NDS32:
3575 decode_NDS32_machine_flags (e_flags, buf, sizeof buf);
3576 break;
3577
3578 case EM_NFP:
3579 switch (EF_NFP_MACH (e_flags))
3580 {
3581 case E_NFP_MACH_3200:
3582 strcat (buf, ", NFP-32xx");
3583 break;
3584 case E_NFP_MACH_6000:
3585 strcat (buf, ", NFP-6xxx");
3586 break;
3587 }
3588 break;
3589
3590 case EM_RISCV:
3591 if (e_flags & EF_RISCV_RVC)
3592 strcat (buf, ", RVC");
3593
3594 if (e_flags & EF_RISCV_RVE)
3595 strcat (buf, ", RVE");
3596
3597 switch (e_flags & EF_RISCV_FLOAT_ABI)
3598 {
3599 case EF_RISCV_FLOAT_ABI_SOFT:
3600 strcat (buf, ", soft-float ABI");
3601 break;
3602
3603 case EF_RISCV_FLOAT_ABI_SINGLE:
3604 strcat (buf, ", single-float ABI");
3605 break;
3606
3607 case EF_RISCV_FLOAT_ABI_DOUBLE:
3608 strcat (buf, ", double-float ABI");
3609 break;
3610
3611 case EF_RISCV_FLOAT_ABI_QUAD:
3612 strcat (buf, ", quad-float ABI");
3613 break;
3614 }
3615 break;
3616
3617 case EM_SH:
3618 switch ((e_flags & EF_SH_MACH_MASK))
3619 {
3620 case EF_SH1: strcat (buf, ", sh1"); break;
3621 case EF_SH2: strcat (buf, ", sh2"); break;
3622 case EF_SH3: strcat (buf, ", sh3"); break;
3623 case EF_SH_DSP: strcat (buf, ", sh-dsp"); break;
3624 case EF_SH3_DSP: strcat (buf, ", sh3-dsp"); break;
3625 case EF_SH4AL_DSP: strcat (buf, ", sh4al-dsp"); break;
3626 case EF_SH3E: strcat (buf, ", sh3e"); break;
3627 case EF_SH4: strcat (buf, ", sh4"); break;
3628 case EF_SH5: strcat (buf, ", sh5"); break;
3629 case EF_SH2E: strcat (buf, ", sh2e"); break;
3630 case EF_SH4A: strcat (buf, ", sh4a"); break;
3631 case EF_SH2A: strcat (buf, ", sh2a"); break;
3632 case EF_SH4_NOFPU: strcat (buf, ", sh4-nofpu"); break;
3633 case EF_SH4A_NOFPU: strcat (buf, ", sh4a-nofpu"); break;
3634 case EF_SH2A_NOFPU: strcat (buf, ", sh2a-nofpu"); break;
3635 case EF_SH3_NOMMU: strcat (buf, ", sh3-nommu"); break;
3636 case EF_SH4_NOMMU_NOFPU: strcat (buf, ", sh4-nommu-nofpu"); break;
3637 case EF_SH2A_SH4_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh4-nommu-nofpu"); break;
3638 case EF_SH2A_SH3_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh3-nommu"); break;
3639 case EF_SH2A_SH4: strcat (buf, ", sh2a-or-sh4"); break;
3640 case EF_SH2A_SH3E: strcat (buf, ", sh2a-or-sh3e"); break;
3641 default: strcat (buf, _(", unknown ISA")); break;
3642 }
3643
3644 if (e_flags & EF_SH_PIC)
3645 strcat (buf, ", pic");
3646
3647 if (e_flags & EF_SH_FDPIC)
3648 strcat (buf, ", fdpic");
3649 break;
3650
3651 case EM_OR1K:
3652 if (e_flags & EF_OR1K_NODELAY)
3653 strcat (buf, ", no delay");
3654 break;
3655
3656 case EM_SPARCV9:
3657 if (e_flags & EF_SPARC_32PLUS)
3658 strcat (buf, ", v8+");
3659
3660 if (e_flags & EF_SPARC_SUN_US1)
3661 strcat (buf, ", ultrasparcI");
3662
3663 if (e_flags & EF_SPARC_SUN_US3)
3664 strcat (buf, ", ultrasparcIII");
3665
3666 if (e_flags & EF_SPARC_HAL_R1)
3667 strcat (buf, ", halr1");
3668
3669 if (e_flags & EF_SPARC_LEDATA)
3670 strcat (buf, ", ledata");
3671
3672 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
3673 strcat (buf, ", tso");
3674
3675 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
3676 strcat (buf, ", pso");
3677
3678 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
3679 strcat (buf, ", rmo");
3680 break;
3681
3682 case EM_PARISC:
3683 switch (e_flags & EF_PARISC_ARCH)
3684 {
3685 case EFA_PARISC_1_0:
3686 strcpy (buf, ", PA-RISC 1.0");
3687 break;
3688 case EFA_PARISC_1_1:
3689 strcpy (buf, ", PA-RISC 1.1");
3690 break;
3691 case EFA_PARISC_2_0:
3692 strcpy (buf, ", PA-RISC 2.0");
3693 break;
3694 default:
3695 break;
3696 }
3697 if (e_flags & EF_PARISC_TRAPNIL)
3698 strcat (buf, ", trapnil");
3699 if (e_flags & EF_PARISC_EXT)
3700 strcat (buf, ", ext");
3701 if (e_flags & EF_PARISC_LSB)
3702 strcat (buf, ", lsb");
3703 if (e_flags & EF_PARISC_WIDE)
3704 strcat (buf, ", wide");
3705 if (e_flags & EF_PARISC_NO_KABP)
3706 strcat (buf, ", no kabp");
3707 if (e_flags & EF_PARISC_LAZYSWAP)
3708 strcat (buf, ", lazyswap");
3709 break;
3710
3711 case EM_PJ:
3712 case EM_PJ_OLD:
3713 if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
3714 strcat (buf, ", new calling convention");
3715
3716 if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
3717 strcat (buf, ", gnu calling convention");
3718 break;
3719
3720 case EM_IA_64:
3721 if ((e_flags & EF_IA_64_ABI64))
3722 strcat (buf, ", 64-bit");
3723 else
3724 strcat (buf, ", 32-bit");
3725 if ((e_flags & EF_IA_64_REDUCEDFP))
3726 strcat (buf, ", reduced fp model");
3727 if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
3728 strcat (buf, ", no function descriptors, constant gp");
3729 else if ((e_flags & EF_IA_64_CONS_GP))
3730 strcat (buf, ", constant gp");
3731 if ((e_flags & EF_IA_64_ABSOLUTE))
3732 strcat (buf, ", absolute");
3733 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
3734 {
3735 if ((e_flags & EF_IA_64_VMS_LINKAGES))
3736 strcat (buf, ", vms_linkages");
3737 switch ((e_flags & EF_IA_64_VMS_COMCOD))
3738 {
3739 case EF_IA_64_VMS_COMCOD_SUCCESS:
3740 break;
3741 case EF_IA_64_VMS_COMCOD_WARNING:
3742 strcat (buf, ", warning");
3743 break;
3744 case EF_IA_64_VMS_COMCOD_ERROR:
3745 strcat (buf, ", error");
3746 break;
3747 case EF_IA_64_VMS_COMCOD_ABORT:
3748 strcat (buf, ", abort");
3749 break;
3750 default:
3751 warn (_("Unrecognised IA64 VMS Command Code: %x\n"),
3752 e_flags & EF_IA_64_VMS_COMCOD);
3753 strcat (buf, ", <unknown>");
3754 }
3755 }
3756 break;
3757
3758 case EM_VAX:
3759 if ((e_flags & EF_VAX_NONPIC))
3760 strcat (buf, ", non-PIC");
3761 if ((e_flags & EF_VAX_DFLOAT))
3762 strcat (buf, ", D-Float");
3763 if ((e_flags & EF_VAX_GFLOAT))
3764 strcat (buf, ", G-Float");
3765 break;
3766
3767 case EM_VISIUM:
3768 if (e_flags & EF_VISIUM_ARCH_MCM)
3769 strcat (buf, ", mcm");
3770 else if (e_flags & EF_VISIUM_ARCH_MCM24)
3771 strcat (buf, ", mcm24");
3772 if (e_flags & EF_VISIUM_ARCH_GR6)
3773 strcat (buf, ", gr6");
3774 break;
3775
3776 case EM_RL78:
3777 switch (e_flags & E_FLAG_RL78_CPU_MASK)
3778 {
3779 case E_FLAG_RL78_ANY_CPU: break;
3780 case E_FLAG_RL78_G10: strcat (buf, ", G10"); break;
3781 case E_FLAG_RL78_G13: strcat (buf, ", G13"); break;
3782 case E_FLAG_RL78_G14: strcat (buf, ", G14"); break;
3783 }
3784 if (e_flags & E_FLAG_RL78_64BIT_DOUBLES)
3785 strcat (buf, ", 64-bit doubles");
3786 break;
3787
3788 case EM_RX:
3789 if (e_flags & E_FLAG_RX_64BIT_DOUBLES)
3790 strcat (buf, ", 64-bit doubles");
3791 if (e_flags & E_FLAG_RX_DSP)
3792 strcat (buf, ", dsp");
3793 if (e_flags & E_FLAG_RX_PID)
3794 strcat (buf, ", pid");
3795 if (e_flags & E_FLAG_RX_ABI)
3796 strcat (buf, ", RX ABI");
3797 if (e_flags & E_FLAG_RX_SINSNS_SET)
3798 strcat (buf, e_flags & E_FLAG_RX_SINSNS_YES
3799 ? ", uses String instructions" : ", bans String instructions");
3800 if (e_flags & E_FLAG_RX_V2)
3801 strcat (buf, ", V2");
3802 if (e_flags & E_FLAG_RX_V3)
3803 strcat (buf, ", V3");
3804 break;
3805
3806 case EM_S390:
3807 if (e_flags & EF_S390_HIGH_GPRS)
3808 strcat (buf, ", highgprs");
3809 break;
3810
3811 case EM_TI_C6000:
3812 if ((e_flags & EF_C6000_REL))
3813 strcat (buf, ", relocatable module");
3814 break;
3815
3816 case EM_MSP430:
3817 strcat (buf, _(": architecture variant: "));
3818 switch (e_flags & EF_MSP430_MACH)
3819 {
3820 case E_MSP430_MACH_MSP430x11: strcat (buf, "MSP430x11"); break;
3821 case E_MSP430_MACH_MSP430x11x1 : strcat (buf, "MSP430x11x1 "); break;
3822 case E_MSP430_MACH_MSP430x12: strcat (buf, "MSP430x12"); break;
3823 case E_MSP430_MACH_MSP430x13: strcat (buf, "MSP430x13"); break;
3824 case E_MSP430_MACH_MSP430x14: strcat (buf, "MSP430x14"); break;
3825 case E_MSP430_MACH_MSP430x15: strcat (buf, "MSP430x15"); break;
3826 case E_MSP430_MACH_MSP430x16: strcat (buf, "MSP430x16"); break;
3827 case E_MSP430_MACH_MSP430x31: strcat (buf, "MSP430x31"); break;
3828 case E_MSP430_MACH_MSP430x32: strcat (buf, "MSP430x32"); break;
3829 case E_MSP430_MACH_MSP430x33: strcat (buf, "MSP430x33"); break;
3830 case E_MSP430_MACH_MSP430x41: strcat (buf, "MSP430x41"); break;
3831 case E_MSP430_MACH_MSP430x42: strcat (buf, "MSP430x42"); break;
3832 case E_MSP430_MACH_MSP430x43: strcat (buf, "MSP430x43"); break;
3833 case E_MSP430_MACH_MSP430x44: strcat (buf, "MSP430x44"); break;
3834 case E_MSP430_MACH_MSP430X : strcat (buf, "MSP430X"); break;
3835 default:
3836 strcat (buf, _(": unknown")); break;
3837 }
3838
3839 if (e_flags & ~ EF_MSP430_MACH)
3840 strcat (buf, _(": unknown extra flag bits also present"));
3841 break;
3842
3843 case EM_Z80:
3844 switch (e_flags & EF_Z80_MACH_MSK)
3845 {
3846 case EF_Z80_MACH_Z80: strcat (buf, ", Z80"); break;
3847 case EF_Z80_MACH_Z180: strcat (buf, ", Z180"); break;
3848 case EF_Z80_MACH_R800: strcat (buf, ", R800"); break;
3849 case EF_Z80_MACH_EZ80_Z80: strcat (buf, ", EZ80"); break;
3850 case EF_Z80_MACH_EZ80_ADL: strcat (buf, ", EZ80, ADL"); break;
3851 case EF_Z80_MACH_GBZ80: strcat (buf, ", GBZ80"); break;
3852 case EF_Z80_MACH_Z80N: strcat (buf, ", Z80N"); break;
3853 default:
3854 strcat (buf, _(", unknown")); break;
3855 }
3856 break;
3857 }
3858 }
3859
3860 return buf;
3861 }
3862
3863 static const char *
3864 get_osabi_name (Filedata * filedata, unsigned int osabi)
3865 {
3866 static char buff[32];
3867
3868 switch (osabi)
3869 {
3870 case ELFOSABI_NONE: return "UNIX - System V";
3871 case ELFOSABI_HPUX: return "UNIX - HP-UX";
3872 case ELFOSABI_NETBSD: return "UNIX - NetBSD";
3873 case ELFOSABI_GNU: return "UNIX - GNU";
3874 case ELFOSABI_SOLARIS: return "UNIX - Solaris";
3875 case ELFOSABI_AIX: return "UNIX - AIX";
3876 case ELFOSABI_IRIX: return "UNIX - IRIX";
3877 case ELFOSABI_FREEBSD: return "UNIX - FreeBSD";
3878 case ELFOSABI_TRU64: return "UNIX - TRU64";
3879 case ELFOSABI_MODESTO: return "Novell - Modesto";
3880 case ELFOSABI_OPENBSD: return "UNIX - OpenBSD";
3881 case ELFOSABI_OPENVMS: return "VMS - OpenVMS";
3882 case ELFOSABI_NSK: return "HP - Non-Stop Kernel";
3883 case ELFOSABI_AROS: return "AROS";
3884 case ELFOSABI_FENIXOS: return "FenixOS";
3885 case ELFOSABI_CLOUDABI: return "Nuxi CloudABI";
3886 case ELFOSABI_OPENVOS: return "Stratus Technologies OpenVOS";
3887 default:
3888 if (osabi >= 64)
3889 switch (filedata->file_header.e_machine)
3890 {
3891 case EM_ARM:
3892 switch (osabi)
3893 {
3894 case ELFOSABI_ARM: return "ARM";
3895 case ELFOSABI_ARM_FDPIC: return "ARM FDPIC";
3896 default:
3897 break;
3898 }
3899 break;
3900
3901 case EM_MSP430:
3902 case EM_MSP430_OLD:
3903 case EM_VISIUM:
3904 switch (osabi)
3905 {
3906 case ELFOSABI_STANDALONE: return _("Standalone App");
3907 default:
3908 break;
3909 }
3910 break;
3911
3912 case EM_TI_C6000:
3913 switch (osabi)
3914 {
3915 case ELFOSABI_C6000_ELFABI: return _("Bare-metal C6000");
3916 case ELFOSABI_C6000_LINUX: return "Linux C6000";
3917 default:
3918 break;
3919 }
3920 break;
3921
3922 default:
3923 break;
3924 }
3925 snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
3926 return buff;
3927 }
3928 }
3929
3930 static const char *
3931 get_aarch64_segment_type (unsigned long type)
3932 {
3933 switch (type)
3934 {
3935 case PT_AARCH64_ARCHEXT: return "AARCH64_ARCHEXT";
3936 default: return NULL;
3937 }
3938 }
3939
3940 static const char *
3941 get_arm_segment_type (unsigned long type)
3942 {
3943 switch (type)
3944 {
3945 case PT_ARM_EXIDX: return "EXIDX";
3946 default: return NULL;
3947 }
3948 }
3949
3950 static const char *
3951 get_s390_segment_type (unsigned long type)
3952 {
3953 switch (type)
3954 {
3955 case PT_S390_PGSTE: return "S390_PGSTE";
3956 default: return NULL;
3957 }
3958 }
3959
3960 static const char *
3961 get_mips_segment_type (unsigned long type)
3962 {
3963 switch (type)
3964 {
3965 case PT_MIPS_REGINFO: return "REGINFO";
3966 case PT_MIPS_RTPROC: return "RTPROC";
3967 case PT_MIPS_OPTIONS: return "OPTIONS";
3968 case PT_MIPS_ABIFLAGS: return "ABIFLAGS";
3969 default: return NULL;
3970 }
3971 }
3972
3973 static const char *
3974 get_parisc_segment_type (unsigned long type)
3975 {
3976 switch (type)
3977 {
3978 case PT_PARISC_ARCHEXT: return "PARISC_ARCHEXT";
3979 case PT_PARISC_UNWIND: return "PARISC_UNWIND";
3980 case PT_PARISC_WEAKORDER: return "PARISC_WEAKORDER";
3981 default: return NULL;
3982 }
3983 }
3984
3985 static const char *
3986 get_ia64_segment_type (unsigned long type)
3987 {
3988 switch (type)
3989 {
3990 case PT_IA_64_ARCHEXT: return "IA_64_ARCHEXT";
3991 case PT_IA_64_UNWIND: return "IA_64_UNWIND";
3992 default: return NULL;
3993 }
3994 }
3995
3996 static const char *
3997 get_tic6x_segment_type (unsigned long type)
3998 {
3999 switch (type)
4000 {
4001 case PT_C6000_PHATTR: return "C6000_PHATTR";
4002 default: return NULL;
4003 }
4004 }
4005
4006 static const char *
4007 get_hpux_segment_type (unsigned long type, unsigned e_machine)
4008 {
4009 if (e_machine == EM_PARISC)
4010 switch (type)
4011 {
4012 case PT_HP_TLS: return "HP_TLS";
4013 case PT_HP_CORE_NONE: return "HP_CORE_NONE";
4014 case PT_HP_CORE_VERSION: return "HP_CORE_VERSION";
4015 case PT_HP_CORE_KERNEL: return "HP_CORE_KERNEL";
4016 case PT_HP_CORE_COMM: return "HP_CORE_COMM";
4017 case PT_HP_CORE_PROC: return "HP_CORE_PROC";
4018 case PT_HP_CORE_LOADABLE: return "HP_CORE_LOADABLE";
4019 case PT_HP_CORE_STACK: return "HP_CORE_STACK";
4020 case PT_HP_CORE_SHM: return "HP_CORE_SHM";
4021 case PT_HP_CORE_MMF: return "HP_CORE_MMF";
4022 case PT_HP_PARALLEL: return "HP_PARALLEL";
4023 case PT_HP_FASTBIND: return "HP_FASTBIND";
4024 case PT_HP_OPT_ANNOT: return "HP_OPT_ANNOT";
4025 case PT_HP_HSL_ANNOT: return "HP_HSL_ANNOT";
4026 case PT_HP_STACK: return "HP_STACK";
4027 case PT_HP_CORE_UTSNAME: return "HP_CORE_UTSNAME";
4028 default: return NULL;
4029 }
4030
4031 if (e_machine == EM_IA_64)
4032 switch (type)
4033 {
4034 case PT_HP_TLS: return "HP_TLS";
4035 case PT_IA_64_HP_OPT_ANOT: return "HP_OPT_ANNOT";
4036 case PT_IA_64_HP_HSL_ANOT: return "HP_HSL_ANNOT";
4037 case PT_IA_64_HP_STACK: return "HP_STACK";
4038 default: return NULL;
4039 }
4040
4041 return NULL;
4042 }
4043
4044 static const char *
4045 get_solaris_segment_type (unsigned long type)
4046 {
4047 switch (type)
4048 {
4049 case 0x6464e550: return "PT_SUNW_UNWIND";
4050 case 0x6474e550: return "PT_SUNW_EH_FRAME";
4051 case 0x6ffffff7: return "PT_LOSUNW";
4052 case 0x6ffffffa: return "PT_SUNWBSS";
4053 case 0x6ffffffb: return "PT_SUNWSTACK";
4054 case 0x6ffffffc: return "PT_SUNWDTRACE";
4055 case 0x6ffffffd: return "PT_SUNWCAP";
4056 case 0x6fffffff: return "PT_HISUNW";
4057 default: return NULL;
4058 }
4059 }
4060
4061 static const char *
4062 get_segment_type (Filedata * filedata, unsigned long p_type)
4063 {
4064 static char buff[32];
4065
4066 switch (p_type)
4067 {
4068 case PT_NULL: return "NULL";
4069 case PT_LOAD: return "LOAD";
4070 case PT_DYNAMIC: return "DYNAMIC";
4071 case PT_INTERP: return "INTERP";
4072 case PT_NOTE: return "NOTE";
4073 case PT_SHLIB: return "SHLIB";
4074 case PT_PHDR: return "PHDR";
4075 case PT_TLS: return "TLS";
4076 case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
4077 case PT_GNU_STACK: return "GNU_STACK";
4078 case PT_GNU_RELRO: return "GNU_RELRO";
4079 case PT_GNU_PROPERTY: return "GNU_PROPERTY";
4080
4081 case PT_OPENBSD_RANDOMIZE: return "OPENBSD_RANDOMIZE";
4082 case PT_OPENBSD_WXNEEDED: return "OPENBSD_WXNEEDED";
4083 case PT_OPENBSD_BOOTDATA: return "OPENBSD_BOOTDATA";
4084
4085 default:
4086 if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
4087 {
4088 const char * result;
4089
4090 switch (filedata->file_header.e_machine)
4091 {
4092 case EM_AARCH64:
4093 result = get_aarch64_segment_type (p_type);
4094 break;
4095 case EM_ARM:
4096 result = get_arm_segment_type (p_type);
4097 break;
4098 case EM_MIPS:
4099 case EM_MIPS_RS3_LE:
4100 result = get_mips_segment_type (p_type);
4101 break;
4102 case EM_PARISC:
4103 result = get_parisc_segment_type (p_type);
4104 break;
4105 case EM_IA_64:
4106 result = get_ia64_segment_type (p_type);
4107 break;
4108 case EM_TI_C6000:
4109 result = get_tic6x_segment_type (p_type);
4110 break;
4111 case EM_S390:
4112 case EM_S390_OLD:
4113 result = get_s390_segment_type (p_type);
4114 break;
4115 default:
4116 result = NULL;
4117 break;
4118 }
4119
4120 if (result != NULL)
4121 return result;
4122
4123 sprintf (buff, "LOPROC+%#lx", p_type - PT_LOPROC);
4124 }
4125 else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
4126 {
4127 const char * result = NULL;
4128
4129 switch (filedata->file_header.e_ident[EI_OSABI])
4130 {
4131 case ELFOSABI_GNU:
4132 case ELFOSABI_FREEBSD:
4133 if (p_type >= PT_GNU_MBIND_LO && p_type <= PT_GNU_MBIND_HI)
4134 {
4135 sprintf (buff, "GNU_MBIND+%#lx", p_type - PT_GNU_MBIND_LO);
4136 result = buff;
4137 }
4138 break;
4139 case ELFOSABI_HPUX:
4140 result = get_hpux_segment_type (p_type,
4141 filedata->file_header.e_machine);
4142 break;
4143 case ELFOSABI_SOLARIS:
4144 result = get_solaris_segment_type (p_type);
4145 break;
4146 default:
4147 break;
4148 }
4149 if (result != NULL)
4150 return result;
4151
4152 sprintf (buff, "LOOS+%#lx", p_type - PT_LOOS);
4153 }
4154 else
4155 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
4156
4157 return buff;
4158 }
4159 }
4160
4161 static const char *
4162 get_arc_section_type_name (unsigned int sh_type)
4163 {
4164 switch (sh_type)
4165 {
4166 case SHT_ARC_ATTRIBUTES: return "ARC_ATTRIBUTES";
4167 default:
4168 break;
4169 }
4170 return NULL;
4171 }
4172
4173 static const char *
4174 get_mips_section_type_name (unsigned int sh_type)
4175 {
4176 switch (sh_type)
4177 {
4178 case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
4179 case SHT_MIPS_MSYM: return "MIPS_MSYM";
4180 case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
4181 case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
4182 case SHT_MIPS_UCODE: return "MIPS_UCODE";
4183 case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
4184 case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
4185 case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
4186 case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
4187 case SHT_MIPS_RELD: return "MIPS_RELD";
4188 case SHT_MIPS_IFACE: return "MIPS_IFACE";
4189 case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
4190 case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
4191 case SHT_MIPS_SHDR: return "MIPS_SHDR";
4192 case SHT_MIPS_FDESC: return "MIPS_FDESC";
4193 case SHT_MIPS_EXTSYM: return "MIPS_EXTSYM";
4194 case SHT_MIPS_DENSE: return "MIPS_DENSE";
4195 case SHT_MIPS_PDESC: return "MIPS_PDESC";
4196 case SHT_MIPS_LOCSYM: return "MIPS_LOCSYM";
4197 case SHT_MIPS_AUXSYM: return "MIPS_AUXSYM";
4198 case SHT_MIPS_OPTSYM: return "MIPS_OPTSYM";
4199 case SHT_MIPS_LOCSTR: return "MIPS_LOCSTR";
4200 case SHT_MIPS_LINE: return "MIPS_LINE";
4201 case SHT_MIPS_RFDESC: return "MIPS_RFDESC";
4202 case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
4203 case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
4204 case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
4205 case SHT_MIPS_DWARF: return "MIPS_DWARF";
4206 case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
4207 case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
4208 case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
4209 case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
4210 case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
4211 case SHT_MIPS_XLATE: return "MIPS_XLATE";
4212 case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
4213 case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
4214 case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
4215 case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
4216 case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
4217 case SHT_MIPS_ABIFLAGS: return "MIPS_ABIFLAGS";
4218 case SHT_MIPS_XHASH: return "MIPS_XHASH";
4219 default:
4220 break;
4221 }
4222 return NULL;
4223 }
4224
4225 static const char *
4226 get_parisc_section_type_name (unsigned int sh_type)
4227 {
4228 switch (sh_type)
4229 {
4230 case SHT_PARISC_EXT: return "PARISC_EXT";
4231 case SHT_PARISC_UNWIND: return "PARISC_UNWIND";
4232 case SHT_PARISC_DOC: return "PARISC_DOC";
4233 case SHT_PARISC_ANNOT: return "PARISC_ANNOT";
4234 case SHT_PARISC_SYMEXTN: return "PARISC_SYMEXTN";
4235 case SHT_PARISC_STUBS: return "PARISC_STUBS";
4236 case SHT_PARISC_DLKM: return "PARISC_DLKM";
4237 default: return NULL;
4238 }
4239 }
4240
4241 static const char *
4242 get_ia64_section_type_name (Filedata * filedata, unsigned int sh_type)
4243 {
4244 /* If the top 8 bits are 0x78 the next 8 are the os/abi ID. */
4245 if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
4246 return get_osabi_name (filedata, (sh_type & 0x00FF0000) >> 16);
4247
4248 switch (sh_type)
4249 {
4250 case SHT_IA_64_EXT: return "IA_64_EXT";
4251 case SHT_IA_64_UNWIND: return "IA_64_UNWIND";
4252 case SHT_IA_64_PRIORITY_INIT: return "IA_64_PRIORITY_INIT";
4253 case SHT_IA_64_VMS_TRACE: return "VMS_TRACE";
4254 case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
4255 case SHT_IA_64_VMS_DEBUG: return "VMS_DEBUG";
4256 case SHT_IA_64_VMS_DEBUG_STR: return "VMS_DEBUG_STR";
4257 case SHT_IA_64_VMS_LINKAGES: return "VMS_LINKAGES";
4258 case SHT_IA_64_VMS_SYMBOL_VECTOR: return "VMS_SYMBOL_VECTOR";
4259 case SHT_IA_64_VMS_FIXUP: return "VMS_FIXUP";
4260 default:
4261 break;
4262 }
4263 return NULL;
4264 }
4265
4266 static const char *
4267 get_x86_64_section_type_name (unsigned int sh_type)
4268 {
4269 switch (sh_type)
4270 {
4271 case SHT_X86_64_UNWIND: return "X86_64_UNWIND";
4272 default: return NULL;
4273 }
4274 }
4275
4276 static const char *
4277 get_aarch64_section_type_name (unsigned int sh_type)
4278 {
4279 switch (sh_type)
4280 {
4281 case SHT_AARCH64_ATTRIBUTES: return "AARCH64_ATTRIBUTES";
4282 default: return NULL;
4283 }
4284 }
4285
4286 static const char *
4287 get_arm_section_type_name (unsigned int sh_type)
4288 {
4289 switch (sh_type)
4290 {
4291 case SHT_ARM_EXIDX: return "ARM_EXIDX";
4292 case SHT_ARM_PREEMPTMAP: return "ARM_PREEMPTMAP";
4293 case SHT_ARM_ATTRIBUTES: return "ARM_ATTRIBUTES";
4294 case SHT_ARM_DEBUGOVERLAY: return "ARM_DEBUGOVERLAY";
4295 case SHT_ARM_OVERLAYSECTION: return "ARM_OVERLAYSECTION";
4296 default: return NULL;
4297 }
4298 }
4299
4300 static const char *
4301 get_tic6x_section_type_name (unsigned int sh_type)
4302 {
4303 switch (sh_type)
4304 {
4305 case SHT_C6000_UNWIND: return "C6000_UNWIND";
4306 case SHT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
4307 case SHT_C6000_ATTRIBUTES: return "C6000_ATTRIBUTES";
4308 case SHT_TI_ICODE: return "TI_ICODE";
4309 case SHT_TI_XREF: return "TI_XREF";
4310 case SHT_TI_HANDLER: return "TI_HANDLER";
4311 case SHT_TI_INITINFO: return "TI_INITINFO";
4312 case SHT_TI_PHATTRS: return "TI_PHATTRS";
4313 default: return NULL;
4314 }
4315 }
4316
4317 static const char *
4318 get_msp430_section_type_name (unsigned int sh_type)
4319 {
4320 switch (sh_type)
4321 {
4322 case SHT_MSP430_SEC_FLAGS: return "MSP430_SEC_FLAGS";
4323 case SHT_MSP430_SYM_ALIASES: return "MSP430_SYM_ALIASES";
4324 case SHT_MSP430_ATTRIBUTES: return "MSP430_ATTRIBUTES";
4325 default: return NULL;
4326 }
4327 }
4328
4329 static const char *
4330 get_nfp_section_type_name (unsigned int sh_type)
4331 {
4332 switch (sh_type)
4333 {
4334 case SHT_NFP_MECONFIG: return "NFP_MECONFIG";
4335 case SHT_NFP_INITREG: return "NFP_INITREG";
4336 case SHT_NFP_UDEBUG: return "NFP_UDEBUG";
4337 default: return NULL;
4338 }
4339 }
4340
4341 static const char *
4342 get_v850_section_type_name (unsigned int sh_type)
4343 {
4344 switch (sh_type)
4345 {
4346 case SHT_V850_SCOMMON: return "V850 Small Common";
4347 case SHT_V850_TCOMMON: return "V850 Tiny Common";
4348 case SHT_V850_ZCOMMON: return "V850 Zero Common";
4349 case SHT_RENESAS_IOP: return "RENESAS IOP";
4350 case SHT_RENESAS_INFO: return "RENESAS INFO";
4351 default: return NULL;
4352 }
4353 }
4354
4355 static const char *
4356 get_riscv_section_type_name (unsigned int sh_type)
4357 {
4358 switch (sh_type)
4359 {
4360 case SHT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
4361 default: return NULL;
4362 }
4363 }
4364
4365 static const char *
4366 get_csky_section_type_name (unsigned int sh_type)
4367 {
4368 switch (sh_type)
4369 {
4370 case SHT_CSKY_ATTRIBUTES: return "CSKY_ATTRIBUTES";
4371 default: return NULL;
4372 }
4373 }
4374
4375 static const char *
4376 get_section_type_name (Filedata * filedata, unsigned int sh_type)
4377 {
4378 static char buff[32];
4379 const char * result;
4380
4381 switch (sh_type)
4382 {
4383 case SHT_NULL: return "NULL";
4384 case SHT_PROGBITS: return "PROGBITS";
4385 case SHT_SYMTAB: return "SYMTAB";
4386 case SHT_STRTAB: return "STRTAB";
4387 case SHT_RELA: return "RELA";
4388 case SHT_HASH: return "HASH";
4389 case SHT_DYNAMIC: return "DYNAMIC";
4390 case SHT_NOTE: return "NOTE";
4391 case SHT_NOBITS: return "NOBITS";
4392 case SHT_REL: return "REL";
4393 case SHT_SHLIB: return "SHLIB";
4394 case SHT_DYNSYM: return "DYNSYM";
4395 case SHT_INIT_ARRAY: return "INIT_ARRAY";
4396 case SHT_FINI_ARRAY: return "FINI_ARRAY";
4397 case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
4398 case SHT_GNU_HASH: return "GNU_HASH";
4399 case SHT_GROUP: return "GROUP";
4400 case SHT_SYMTAB_SHNDX: return "SYMTAB SECTION INDICES";
4401 case SHT_GNU_verdef: return "VERDEF";
4402 case SHT_GNU_verneed: return "VERNEED";
4403 case SHT_GNU_versym: return "VERSYM";
4404 case 0x6ffffff0: return "VERSYM";
4405 case 0x6ffffffc: return "VERDEF";
4406 case 0x7ffffffd: return "AUXILIARY";
4407 case 0x7fffffff: return "FILTER";
4408 case SHT_GNU_LIBLIST: return "GNU_LIBLIST";
4409
4410 default:
4411 if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
4412 {
4413 switch (filedata->file_header.e_machine)
4414 {
4415 case EM_ARC:
4416 case EM_ARC_COMPACT:
4417 case EM_ARC_COMPACT2:
4418 result = get_arc_section_type_name (sh_type);
4419 break;
4420 case EM_MIPS:
4421 case EM_MIPS_RS3_LE:
4422 result = get_mips_section_type_name (sh_type);
4423 break;
4424 case EM_PARISC:
4425 result = get_parisc_section_type_name (sh_type);
4426 break;
4427 case EM_IA_64:
4428 result = get_ia64_section_type_name (filedata, sh_type);
4429 break;
4430 case EM_X86_64:
4431 case EM_L1OM:
4432 case EM_K1OM:
4433 result = get_x86_64_section_type_name (sh_type);
4434 break;
4435 case EM_AARCH64:
4436 result = get_aarch64_section_type_name (sh_type);
4437 break;
4438 case EM_ARM:
4439 result = get_arm_section_type_name (sh_type);
4440 break;
4441 case EM_TI_C6000:
4442 result = get_tic6x_section_type_name (sh_type);
4443 break;
4444 case EM_MSP430:
4445 result = get_msp430_section_type_name (sh_type);
4446 break;
4447 case EM_NFP:
4448 result = get_nfp_section_type_name (sh_type);
4449 break;
4450 case EM_V800:
4451 case EM_V850:
4452 case EM_CYGNUS_V850:
4453 result = get_v850_section_type_name (sh_type);
4454 break;
4455 case EM_RISCV:
4456 result = get_riscv_section_type_name (sh_type);
4457 break;
4458 case EM_CSKY:
4459 result = get_csky_section_type_name (sh_type);
4460 break;
4461 default:
4462 result = NULL;
4463 break;
4464 }
4465
4466 if (result != NULL)
4467 return result;
4468
4469 sprintf (buff, "LOPROC+%#x", sh_type - SHT_LOPROC);
4470 }
4471 else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
4472 {
4473 switch (filedata->file_header.e_machine)
4474 {
4475 case EM_IA_64:
4476 result = get_ia64_section_type_name (filedata, sh_type);
4477 break;
4478 default:
4479 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
4480 result = get_solaris_section_type (sh_type);
4481 else
4482 {
4483 switch (sh_type)
4484 {
4485 case SHT_GNU_INCREMENTAL_INPUTS: result = "GNU_INCREMENTAL_INPUTS"; break;
4486 case SHT_GNU_ATTRIBUTES: result = "GNU_ATTRIBUTES"; break;
4487 case SHT_GNU_HASH: result = "GNU_HASH"; break;
4488 case SHT_GNU_LIBLIST: result = "GNU_LIBLIST"; break;
4489 default:
4490 result = NULL;
4491 break;
4492 }
4493 }
4494 break;
4495 }
4496
4497 if (result != NULL)
4498 return result;
4499
4500 sprintf (buff, "LOOS+%#x", sh_type - SHT_LOOS);
4501 }
4502 else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
4503 {
4504 switch (filedata->file_header.e_machine)
4505 {
4506 case EM_V800:
4507 case EM_V850:
4508 case EM_CYGNUS_V850:
4509 result = get_v850_section_type_name (sh_type);
4510 break;
4511 default:
4512 result = NULL;
4513 break;
4514 }
4515
4516 if (result != NULL)
4517 return result;
4518
4519 sprintf (buff, "LOUSER+%#x", sh_type - SHT_LOUSER);
4520 }
4521 else
4522 /* This message is probably going to be displayed in a 15
4523 character wide field, so put the hex value first. */
4524 snprintf (buff, sizeof (buff), _("%08x: <unknown>"), sh_type);
4525
4526 return buff;
4527 }
4528 }
4529
4530 enum long_option_values
4531 {
4532 OPTION_DEBUG_DUMP = 512,
4533 OPTION_DYN_SYMS,
4534 OPTION_LTO_SYMS,
4535 OPTION_DWARF_DEPTH,
4536 OPTION_DWARF_START,
4537 OPTION_DWARF_CHECK,
4538 OPTION_CTF_DUMP,
4539 OPTION_CTF_PARENT,
4540 OPTION_CTF_SYMBOLS,
4541 OPTION_CTF_STRINGS,
4542 OPTION_WITH_SYMBOL_VERSIONS,
4543 OPTION_RECURSE_LIMIT,
4544 OPTION_NO_RECURSE_LIMIT,
4545 OPTION_NO_DEMANGLING,
4546 OPTION_SYM_BASE
4547 };
4548
4549 static struct option options[] =
4550 {
4551 /* Note - This table is alpha-sorted on the 'val'
4552 field in order to make adding new options easier. */
4553 {"arch-specific", no_argument, 0, 'A'},
4554 {"all", no_argument, 0, 'a'},
4555 {"demangle", optional_argument, 0, 'C'},
4556 {"archive-index", no_argument, 0, 'c'},
4557 {"use-dynamic", no_argument, 0, 'D'},
4558 {"dynamic", no_argument, 0, 'd'},
4559 {"headers", no_argument, 0, 'e'},
4560 {"section-groups", no_argument, 0, 'g'},
4561 {"help", no_argument, 0, 'H'},
4562 {"file-header", no_argument, 0, 'h'},
4563 {"histogram", no_argument, 0, 'I'},
4564 {"lint", no_argument, 0, 'L'},
4565 {"enable-checks", no_argument, 0, 'L'},
4566 {"program-headers", no_argument, 0, 'l'},
4567 {"segments", no_argument, 0, 'l'},
4568 {"full-section-name",no_argument, 0, 'N'},
4569 {"notes", no_argument, 0, 'n'},
4570 {"process-links", no_argument, 0, 'P'},
4571 {"string-dump", required_argument, 0, 'p'},
4572 {"relocated-dump", required_argument, 0, 'R'},
4573 {"relocs", no_argument, 0, 'r'},
4574 {"section-headers", no_argument, 0, 'S'},
4575 {"sections", no_argument, 0, 'S'},
4576 {"symbols", no_argument, 0, 's'},
4577 {"syms", no_argument, 0, 's'},
4578 {"silent-truncation",no_argument, 0, 'T'},
4579 {"section-details", no_argument, 0, 't'},
4580 {"unwind", no_argument, 0, 'u'},
4581 {"version-info", no_argument, 0, 'V'},
4582 {"version", no_argument, 0, 'v'},
4583 {"wide", no_argument, 0, 'W'},
4584 {"hex-dump", required_argument, 0, 'x'},
4585 {"decompress", no_argument, 0, 'z'},
4586
4587 {"no-demangle", no_argument, 0, OPTION_NO_DEMANGLING},
4588 {"recurse-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
4589 {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
4590 {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
4591 {"dyn-syms", no_argument, 0, OPTION_DYN_SYMS},
4592 {"lto-syms", no_argument, 0, OPTION_LTO_SYMS},
4593 {"debug-dump", optional_argument, 0, OPTION_DEBUG_DUMP},
4594 {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
4595 {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
4596 {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
4597 #ifdef ENABLE_LIBCTF
4598 {"ctf", required_argument, 0, OPTION_CTF_DUMP},
4599 {"ctf-symbols", required_argument, 0, OPTION_CTF_SYMBOLS},
4600 {"ctf-strings", required_argument, 0, OPTION_CTF_STRINGS},
4601 {"ctf-parent", required_argument, 0, OPTION_CTF_PARENT},
4602 #endif
4603 {"sym-base", optional_argument, 0, OPTION_SYM_BASE},
4604
4605 {0, no_argument, 0, 0}
4606 };
4607
4608 static void
4609 usage (FILE * stream)
4610 {
4611 fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
4612 fprintf (stream, _(" Display information about the contents of ELF format files\n"));
4613 fprintf (stream, _(" Options are:\n"));
4614 fprintf (stream, _("\
4615 -a --all Equivalent to: -h -l -S -s -r -d -V -A -I\n"));
4616 fprintf (stream, _("\
4617 -h --file-header Display the ELF file header\n"));
4618 fprintf (stream, _("\
4619 -l --program-headers Display the program headers\n"));
4620 fprintf (stream, _("\
4621 --segments An alias for --program-headers\n"));
4622 fprintf (stream, _("\
4623 -S --section-headers Display the sections' header\n"));
4624 fprintf (stream, _("\
4625 --sections An alias for --section-headers\n"));
4626 fprintf (stream, _("\
4627 -g --section-groups Display the section groups\n"));
4628 fprintf (stream, _("\
4629 -t --section-details Display the section details\n"));
4630 fprintf (stream, _("\
4631 -e --headers Equivalent to: -h -l -S\n"));
4632 fprintf (stream, _("\
4633 -s --syms Display the symbol table\n"));
4634 fprintf (stream, _("\
4635 --symbols An alias for --syms\n"));
4636 fprintf (stream, _("\
4637 --dyn-syms Display the dynamic symbol table\n"));
4638 fprintf (stream, _("\
4639 --lto-syms Display LTO symbol tables\n"));
4640 fprintf (stream, _("\
4641 --sym-base=[0|8|10|16] \n\
4642 Force base for symbol sizes. The options are \n\
4643 mixed (the default), octal, decimal, hexadecimal.\n"));
4644 fprintf (stream, _("\
4645 -C --demangle[=STYLE] Decode low-level symbol names into user-level names\n\
4646 The STYLE, if specified, can be `auto' (the default),\n\
4647 `gnu', `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
4648 or `gnat'\n"));
4649 fprintf (stream, _("\
4650 --no-demangle Do not demangle low-level symbol names. (default)\n"));
4651 fprintf (stream, _("\
4652 --recurse-limit Enable a demangling recursion limit. (default)\n"));
4653 fprintf (stream, _("\
4654 --no-recurse-limit Disable a demangling recursion limit\n"));
4655 fprintf (stream, _("\
4656 -n --notes Display the core notes (if present)\n"));
4657 fprintf (stream, _("\
4658 -r --relocs Display the relocations (if present)\n"));
4659 fprintf (stream, _("\
4660 -u --unwind Display the unwind info (if present)\n"));
4661 fprintf (stream, _("\
4662 -d --dynamic Display the dynamic section (if present)\n"));
4663 fprintf (stream, _("\
4664 -V --version-info Display the version sections (if present)\n"));
4665 fprintf (stream, _("\
4666 -A --arch-specific Display architecture specific information (if any)\n"));
4667 fprintf (stream, _("\
4668 -c --archive-index Display the symbol/file index in an archive\n"));
4669 fprintf (stream, _("\
4670 -D --use-dynamic Use the dynamic section info when displaying symbols\n"));
4671 fprintf (stream, _("\
4672 -L --lint|--enable-checks\n\
4673 Display warning messages for possible problems\n"));
4674 fprintf (stream, _("\
4675 -x --hex-dump=<number|name>\n\
4676 Dump the contents of section <number|name> as bytes\n"));
4677 fprintf (stream, _("\
4678 -p --string-dump=<number|name>\n\
4679 Dump the contents of section <number|name> as strings\n"));
4680 fprintf (stream, _("\
4681 -R --relocated-dump=<number|name>\n\
4682 Dump the relocated contents of section <number|name>\n"));
4683 fprintf (stream, _("\
4684 -z --decompress Decompress section before dumping it\n"));
4685 fprintf (stream, _("\
4686 -w --debug-dump[a/=abbrev, A/=addr, r/=aranges, c/=cu_index, L/=decodedline,\n\
4687 f/=frames, F/=frames-interp, g/=gdb_index, i/=info, o/=loc,\n\
4688 m/=macro, p/=pubnames, t/=pubtypes, R/=Ranges, l/=rawline,\n\
4689 s/=str, O/=str-offsets, u/=trace_abbrev, T/=trace_aranges,\n\
4690 U/=trace_info]\n\
4691 Display the contents of DWARF debug sections\n"));
4692 fprintf (stream, _("\
4693 -wk --debug-dump=links Display the contents of sections that link to separate\n\
4694 debuginfo files\n"));
4695 fprintf (stream, _("\
4696 -P --process-links Display the contents of non-debug sections in separate\n\
4697 debuginfo files. (Implies -wK)\n"));
4698 #if DEFAULT_FOR_FOLLOW_LINKS
4699 fprintf (stream, _("\
4700 -wK --debug-dump=follow-links\n\
4701 Follow links to separate debug info files (default)\n"));
4702 fprintf (stream, _("\
4703 -wN --debug-dump=no-follow-links\n\
4704 Do not follow links to separate debug info files\n"));
4705 #else
4706 fprintf (stream, _("\
4707 -wK --debug-dump=follow-links\n\
4708 Follow links to separate debug info files\n"));
4709 fprintf (stream, _("\
4710 -wN --debug-dump=no-follow-links\n\
4711 Do not follow links to separate debug info files\n\
4712 (default)\n"));
4713 #endif
4714 fprintf (stream, _("\
4715 --dwarf-depth=N Do not display DIEs at depth N or greater\n"));
4716 fprintf (stream, _("\
4717 --dwarf-start=N Display DIEs starting at offset N\n"));
4718 #ifdef ENABLE_LIBCTF
4719 fprintf (stream, _("\
4720 --ctf=<number|name> Display CTF info from section <number|name>\n"));
4721 fprintf (stream, _("\
4722 --ctf-parent=<number|name>\n\
4723 Use section <number|name> as the CTF parent\n"));
4724 fprintf (stream, _("\
4725 --ctf-symbols=<number|name>\n\
4726 Use section <number|name> as the CTF external symtab\n"));
4727 fprintf (stream, _("\
4728 --ctf-strings=<number|name>\n\
4729 Use section <number|name> as the CTF external strtab\n"));
4730 #endif
4731
4732 #ifdef SUPPORT_DISASSEMBLY
4733 fprintf (stream, _("\
4734 -i --instruction-dump=<number|name>\n\
4735 Disassemble the contents of section <number|name>\n"));
4736 #endif
4737 fprintf (stream, _("\
4738 -I --histogram Display histogram of bucket list lengths\n"));
4739 fprintf (stream, _("\
4740 -W --wide Allow output width to exceed 80 characters\n"));
4741 fprintf (stream, _("\
4742 -T --silent-truncation If a symbol name is truncated, do not add [...] suffix\n"));
4743 fprintf (stream, _("\
4744 @<file> Read options from <file>\n"));
4745 fprintf (stream, _("\
4746 -H --help Display this information\n"));
4747 fprintf (stream, _("\
4748 -v --version Display the version number of readelf\n"));
4749
4750 if (REPORT_BUGS_TO[0] && stream == stdout)
4751 fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
4752
4753 exit (stream == stdout ? 0 : 1);
4754 }
4755
4756 /* Record the fact that the user wants the contents of section number
4757 SECTION to be displayed using the method(s) encoded as flags bits
4758 in TYPE. Note, TYPE can be zero if we are creating the array for
4759 the first time. */
4760
4761 static void
4762 request_dump_bynumber (struct dump_data *dumpdata,
4763 unsigned int section, dump_type type)
4764 {
4765 if (section >= dumpdata->num_dump_sects)
4766 {
4767 dump_type * new_dump_sects;
4768
4769 new_dump_sects = (dump_type *) calloc (section + 1,
4770 sizeof (* new_dump_sects));
4771
4772 if (new_dump_sects == NULL)
4773 error (_("Out of memory allocating dump request table.\n"));
4774 else
4775 {
4776 if (dumpdata->dump_sects)
4777 {
4778 /* Copy current flag settings. */
4779 memcpy (new_dump_sects, dumpdata->dump_sects,
4780 dumpdata->num_dump_sects * sizeof (* new_dump_sects));
4781
4782 free (dumpdata->dump_sects);
4783 }
4784
4785 dumpdata->dump_sects = new_dump_sects;
4786 dumpdata->num_dump_sects = section + 1;
4787 }
4788 }
4789
4790 if (dumpdata->dump_sects)
4791 dumpdata->dump_sects[section] |= type;
4792 }
4793
4794 /* Request a dump by section name. */
4795
4796 static void
4797 request_dump_byname (const char * section, dump_type type)
4798 {
4799 struct dump_list_entry * new_request;
4800
4801 new_request = (struct dump_list_entry *)
4802 malloc (sizeof (struct dump_list_entry));
4803 if (!new_request)
4804 error (_("Out of memory allocating dump request table.\n"));
4805
4806 new_request->name = strdup (section);
4807 if (!new_request->name)
4808 error (_("Out of memory allocating dump request table.\n"));
4809
4810 new_request->type = type;
4811
4812 new_request->next = dump_sects_byname;
4813 dump_sects_byname = new_request;
4814 }
4815
4816 static inline void
4817 request_dump (struct dump_data *dumpdata, dump_type type)
4818 {
4819 int section;
4820 char * cp;
4821
4822 do_dump = true;
4823 section = strtoul (optarg, & cp, 0);
4824
4825 if (! *cp && section >= 0)
4826 request_dump_bynumber (dumpdata, section, type);
4827 else
4828 request_dump_byname (optarg, type);
4829 }
4830
4831 static void
4832 parse_args (struct dump_data *dumpdata, int argc, char ** argv)
4833 {
4834 int c;
4835
4836 if (argc < 2)
4837 usage (stderr);
4838
4839 while ((c = getopt_long
4840 (argc, argv, "ACDHILNPR:STVWacdeghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
4841 {
4842 switch (c)
4843 {
4844 case 0:
4845 /* Long options. */
4846 break;
4847 case 'H':
4848 usage (stdout);
4849 break;
4850
4851 case 'a':
4852 do_syms = true;
4853 do_reloc = true;
4854 do_unwind = true;
4855 do_dynamic = true;
4856 do_header = true;
4857 do_sections = true;
4858 do_section_groups = true;
4859 do_segments = true;
4860 do_version = true;
4861 do_histogram = true;
4862 do_arch = true;
4863 do_notes = true;
4864 break;
4865
4866 case 'g':
4867 do_section_groups = true;
4868 break;
4869 case 't':
4870 case 'N':
4871 do_sections = true;
4872 do_section_details = true;
4873 break;
4874 case 'e':
4875 do_header = true;
4876 do_sections = true;
4877 do_segments = true;
4878 break;
4879 case 'A':
4880 do_arch = true;
4881 break;
4882 case 'D':
4883 do_using_dynamic = true;
4884 break;
4885 case 'r':
4886 do_reloc = true;
4887 break;
4888 case 'u':
4889 do_unwind = true;
4890 break;
4891 case 'h':
4892 do_header = true;
4893 break;
4894 case 'l':
4895 do_segments = true;
4896 break;
4897 case 's':
4898 do_syms = true;
4899 break;
4900 case 'S':
4901 do_sections = true;
4902 break;
4903 case 'd':
4904 do_dynamic = true;
4905 break;
4906 case 'I':
4907 do_histogram = true;
4908 break;
4909 case 'n':
4910 do_notes = true;
4911 break;
4912 case 'c':
4913 do_archive_index = true;
4914 break;
4915 case 'L':
4916 do_checks = true;
4917 break;
4918 case 'P':
4919 process_links = true;
4920 do_follow_links = true;
4921 break;
4922 case 'x':
4923 request_dump (dumpdata, HEX_DUMP);
4924 break;
4925 case 'p':
4926 request_dump (dumpdata, STRING_DUMP);
4927 break;
4928 case 'R':
4929 request_dump (dumpdata, RELOC_DUMP);
4930 break;
4931 case 'z':
4932 decompress_dumps = true;
4933 break;
4934 case 'w':
4935 do_dump = true;
4936 if (optarg == NULL)
4937 {
4938 do_debugging = true;
4939 dwarf_select_sections_all ();
4940 }
4941 else
4942 {
4943 do_debugging = false;
4944 dwarf_select_sections_by_letters (optarg);
4945 }
4946 break;
4947 case OPTION_DEBUG_DUMP:
4948 do_dump = true;
4949 if (optarg == NULL)
4950 {
4951 do_debugging = true;
4952 dwarf_select_sections_all ();
4953 }
4954 else
4955 {
4956 do_debugging = false;
4957 dwarf_select_sections_by_names (optarg);
4958 }
4959 break;
4960 case OPTION_DWARF_DEPTH:
4961 {
4962 char *cp;
4963
4964 dwarf_cutoff_level = strtoul (optarg, & cp, 0);
4965 }
4966 break;
4967 case OPTION_DWARF_START:
4968 {
4969 char *cp;
4970
4971 dwarf_start_die = strtoul (optarg, & cp, 0);
4972 }
4973 break;
4974 case OPTION_DWARF_CHECK:
4975 dwarf_check = true;
4976 break;
4977 case OPTION_CTF_DUMP:
4978 do_ctf = true;
4979 request_dump (dumpdata, CTF_DUMP);
4980 break;
4981 case OPTION_CTF_SYMBOLS:
4982 free (dump_ctf_symtab_name);
4983 dump_ctf_symtab_name = strdup (optarg);
4984 break;
4985 case OPTION_CTF_STRINGS:
4986 free (dump_ctf_strtab_name);
4987 dump_ctf_strtab_name = strdup (optarg);
4988 break;
4989 case OPTION_CTF_PARENT:
4990 free (dump_ctf_parent_name);
4991 dump_ctf_parent_name = strdup (optarg);
4992 break;
4993 case OPTION_DYN_SYMS:
4994 do_dyn_syms = true;
4995 break;
4996 case OPTION_LTO_SYMS:
4997 do_lto_syms = true;
4998 break;
4999 #ifdef SUPPORT_DISASSEMBLY
5000 case 'i':
5001 request_dump (dumpdata, DISASS_DUMP);
5002 break;
5003 #endif
5004 case 'v':
5005 print_version (program_name);
5006 break;
5007 case 'V':
5008 do_version = true;
5009 break;
5010 case 'W':
5011 do_wide = true;
5012 break;
5013 case 'T':
5014 do_not_show_symbol_truncation = true;
5015 break;
5016 case 'C':
5017 do_demangle = true;
5018 if (optarg != NULL)
5019 {
5020 enum demangling_styles style;
5021
5022 style = cplus_demangle_name_to_style (optarg);
5023 if (style == unknown_demangling)
5024 error (_("unknown demangling style `%s'"), optarg);
5025
5026 cplus_demangle_set_style (style);
5027 }
5028 break;
5029 case OPTION_NO_DEMANGLING:
5030 do_demangle = false;
5031 break;
5032 case OPTION_RECURSE_LIMIT:
5033 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
5034 break;
5035 case OPTION_NO_RECURSE_LIMIT:
5036 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
5037 break;
5038 case OPTION_WITH_SYMBOL_VERSIONS:
5039 /* Ignored for backward compatibility. */
5040 break;
5041
5042 case OPTION_SYM_BASE:
5043 sym_base = 0;
5044 if (optarg != NULL)
5045 {
5046 sym_base = strtoul (optarg, NULL, 0);
5047 switch (sym_base)
5048 {
5049 case 0:
5050 case 8:
5051 case 10:
5052 case 16:
5053 break;
5054
5055 default:
5056 sym_base = 0;
5057 break;
5058 }
5059 }
5060 break;
5061
5062 default:
5063 /* xgettext:c-format */
5064 error (_("Invalid option '-%c'\n"), c);
5065 /* Fall through. */
5066 case '?':
5067 usage (stderr);
5068 }
5069 }
5070
5071 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
5072 && !do_segments && !do_header && !do_dump && !do_version
5073 && !do_histogram && !do_debugging && !do_arch && !do_notes
5074 && !do_section_groups && !do_archive_index
5075 && !do_dyn_syms && !do_lto_syms)
5076 {
5077 if (do_checks)
5078 {
5079 check_all = true;
5080 do_dynamic = do_syms = do_reloc = do_unwind = do_sections = true;
5081 do_segments = do_header = do_dump = do_version = true;
5082 do_histogram = do_debugging = do_arch = do_notes = true;
5083 do_section_groups = do_archive_index = do_dyn_syms = true;
5084 do_lto_syms = true;
5085 }
5086 else
5087 usage (stderr);
5088 }
5089 }
5090
5091 static const char *
5092 get_elf_class (unsigned int elf_class)
5093 {
5094 static char buff[32];
5095
5096 switch (elf_class)
5097 {
5098 case ELFCLASSNONE: return _("none");
5099 case ELFCLASS32: return "ELF32";
5100 case ELFCLASS64: return "ELF64";
5101 default:
5102 snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
5103 return buff;
5104 }
5105 }
5106
5107 static const char *
5108 get_data_encoding (unsigned int encoding)
5109 {
5110 static char buff[32];
5111
5112 switch (encoding)
5113 {
5114 case ELFDATANONE: return _("none");
5115 case ELFDATA2LSB: return _("2's complement, little endian");
5116 case ELFDATA2MSB: return _("2's complement, big endian");
5117 default:
5118 snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
5119 return buff;
5120 }
5121 }
5122
5123 /* Decode the data held in 'filedata->file_header'. */
5124
5125 static bool
5126 process_file_header (Filedata * filedata)
5127 {
5128 Elf_Internal_Ehdr * header = & filedata->file_header;
5129
5130 if ( header->e_ident[EI_MAG0] != ELFMAG0
5131 || header->e_ident[EI_MAG1] != ELFMAG1
5132 || header->e_ident[EI_MAG2] != ELFMAG2
5133 || header->e_ident[EI_MAG3] != ELFMAG3)
5134 {
5135 error
5136 (_("Not an ELF file - it has the wrong magic bytes at the start\n"));
5137 return false;
5138 }
5139
5140 if (! filedata->is_separate)
5141 init_dwarf_regnames_by_elf_machine_code (header->e_machine);
5142
5143 if (do_header)
5144 {
5145 unsigned i;
5146
5147 if (filedata->is_separate)
5148 printf (_("ELF Header in linked file '%s':\n"), filedata->file_name);
5149 else
5150 printf (_("ELF Header:\n"));
5151 printf (_(" Magic: "));
5152 for (i = 0; i < EI_NIDENT; i++)
5153 printf ("%2.2x ", header->e_ident[i]);
5154 printf ("\n");
5155 printf (_(" Class: %s\n"),
5156 get_elf_class (header->e_ident[EI_CLASS]));
5157 printf (_(" Data: %s\n"),
5158 get_data_encoding (header->e_ident[EI_DATA]));
5159 printf (_(" Version: %d%s\n"),
5160 header->e_ident[EI_VERSION],
5161 (header->e_ident[EI_VERSION] == EV_CURRENT
5162 ? _(" (current)")
5163 : (header->e_ident[EI_VERSION] != EV_NONE
5164 ? _(" <unknown>")
5165 : "")));
5166 printf (_(" OS/ABI: %s\n"),
5167 get_osabi_name (filedata, header->e_ident[EI_OSABI]));
5168 printf (_(" ABI Version: %d\n"),
5169 header->e_ident[EI_ABIVERSION]);
5170 printf (_(" Type: %s\n"),
5171 get_file_type (header->e_type));
5172 printf (_(" Machine: %s\n"),
5173 get_machine_name (header->e_machine));
5174 printf (_(" Version: 0x%lx\n"),
5175 header->e_version);
5176
5177 printf (_(" Entry point address: "));
5178 print_vma (header->e_entry, PREFIX_HEX);
5179 printf (_("\n Start of program headers: "));
5180 print_vma (header->e_phoff, DEC);
5181 printf (_(" (bytes into file)\n Start of section headers: "));
5182 print_vma (header->e_shoff, DEC);
5183 printf (_(" (bytes into file)\n"));
5184
5185 printf (_(" Flags: 0x%lx%s\n"),
5186 header->e_flags,
5187 get_machine_flags (filedata, header->e_flags, header->e_machine));
5188 printf (_(" Size of this header: %u (bytes)\n"),
5189 header->e_ehsize);
5190 printf (_(" Size of program headers: %u (bytes)\n"),
5191 header->e_phentsize);
5192 printf (_(" Number of program headers: %u"),
5193 header->e_phnum);
5194 if (filedata->section_headers != NULL
5195 && header->e_phnum == PN_XNUM
5196 && filedata->section_headers[0].sh_info != 0)
5197 {
5198 header->e_phnum = filedata->section_headers[0].sh_info;
5199 printf (" (%u)", header->e_phnum);
5200 }
5201 putc ('\n', stdout);
5202 printf (_(" Size of section headers: %u (bytes)\n"),
5203 header->e_shentsize);
5204 printf (_(" Number of section headers: %u"),
5205 header->e_shnum);
5206 if (filedata->section_headers != NULL && header->e_shnum == SHN_UNDEF)
5207 {
5208 header->e_shnum = filedata->section_headers[0].sh_size;
5209 printf (" (%u)", header->e_shnum);
5210 }
5211 putc ('\n', stdout);
5212 printf (_(" Section header string table index: %u"),
5213 header->e_shstrndx);
5214 if (filedata->section_headers != NULL
5215 && header->e_shstrndx == (SHN_XINDEX & 0xffff))
5216 {
5217 header->e_shstrndx = filedata->section_headers[0].sh_link;
5218 printf (" (%u)", header->e_shstrndx);
5219 }
5220 if (header->e_shstrndx != SHN_UNDEF
5221 && header->e_shstrndx >= header->e_shnum)
5222 {
5223 header->e_shstrndx = SHN_UNDEF;
5224 printf (_(" <corrupt: out of range>"));
5225 }
5226 putc ('\n', stdout);
5227 }
5228
5229 if (filedata->section_headers != NULL)
5230 {
5231 if (header->e_phnum == PN_XNUM
5232 && filedata->section_headers[0].sh_info != 0)
5233 header->e_phnum = filedata->section_headers[0].sh_info;
5234 if (header->e_shnum == SHN_UNDEF)
5235 header->e_shnum = filedata->section_headers[0].sh_size;
5236 if (header->e_shstrndx == (SHN_XINDEX & 0xffff))
5237 header->e_shstrndx = filedata->section_headers[0].sh_link;
5238 if (header->e_shstrndx >= header->e_shnum)
5239 header->e_shstrndx = SHN_UNDEF;
5240 }
5241
5242 return true;
5243 }
5244
5245 /* Read in the program headers from FILEDATA and store them in PHEADERS.
5246 Returns TRUE upon success, FALSE otherwise. Loads 32-bit headers. */
5247
5248 static bool
5249 get_32bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
5250 {
5251 Elf32_External_Phdr * phdrs;
5252 Elf32_External_Phdr * external;
5253 Elf_Internal_Phdr * internal;
5254 unsigned int i;
5255 unsigned int size = filedata->file_header.e_phentsize;
5256 unsigned int num = filedata->file_header.e_phnum;
5257
5258 /* PR binutils/17531: Cope with unexpected section header sizes. */
5259 if (size == 0 || num == 0)
5260 return false;
5261 if (size < sizeof * phdrs)
5262 {
5263 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
5264 return false;
5265 }
5266 if (size > sizeof * phdrs)
5267 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
5268
5269 phdrs = (Elf32_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
5270 size, num, _("program headers"));
5271 if (phdrs == NULL)
5272 return false;
5273
5274 for (i = 0, internal = pheaders, external = phdrs;
5275 i < filedata->file_header.e_phnum;
5276 i++, internal++, external++)
5277 {
5278 internal->p_type = BYTE_GET (external->p_type);
5279 internal->p_offset = BYTE_GET (external->p_offset);
5280 internal->p_vaddr = BYTE_GET (external->p_vaddr);
5281 internal->p_paddr = BYTE_GET (external->p_paddr);
5282 internal->p_filesz = BYTE_GET (external->p_filesz);
5283 internal->p_memsz = BYTE_GET (external->p_memsz);
5284 internal->p_flags = BYTE_GET (external->p_flags);
5285 internal->p_align = BYTE_GET (external->p_align);
5286 }
5287
5288 free (phdrs);
5289 return true;
5290 }
5291
5292 /* Read in the program headers from FILEDATA and store them in PHEADERS.
5293 Returns TRUE upon success, FALSE otherwise. Loads 64-bit headers. */
5294
5295 static bool
5296 get_64bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
5297 {
5298 Elf64_External_Phdr * phdrs;
5299 Elf64_External_Phdr * external;
5300 Elf_Internal_Phdr * internal;
5301 unsigned int i;
5302 unsigned int size = filedata->file_header.e_phentsize;
5303 unsigned int num = filedata->file_header.e_phnum;
5304
5305 /* PR binutils/17531: Cope with unexpected section header sizes. */
5306 if (size == 0 || num == 0)
5307 return false;
5308 if (size < sizeof * phdrs)
5309 {
5310 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
5311 return false;
5312 }
5313 if (size > sizeof * phdrs)
5314 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
5315
5316 phdrs = (Elf64_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
5317 size, num, _("program headers"));
5318 if (!phdrs)
5319 return false;
5320
5321 for (i = 0, internal = pheaders, external = phdrs;
5322 i < filedata->file_header.e_phnum;
5323 i++, internal++, external++)
5324 {
5325 internal->p_type = BYTE_GET (external->p_type);
5326 internal->p_flags = BYTE_GET (external->p_flags);
5327 internal->p_offset = BYTE_GET (external->p_offset);
5328 internal->p_vaddr = BYTE_GET (external->p_vaddr);
5329 internal->p_paddr = BYTE_GET (external->p_paddr);
5330 internal->p_filesz = BYTE_GET (external->p_filesz);
5331 internal->p_memsz = BYTE_GET (external->p_memsz);
5332 internal->p_align = BYTE_GET (external->p_align);
5333 }
5334
5335 free (phdrs);
5336 return true;
5337 }
5338
5339 /* Returns TRUE if the program headers were read into `program_headers'. */
5340
5341 static bool
5342 get_program_headers (Filedata * filedata)
5343 {
5344 Elf_Internal_Phdr * phdrs;
5345
5346 /* Check cache of prior read. */
5347 if (filedata->program_headers != NULL)
5348 return true;
5349
5350 /* Be kind to memory checkers by looking for
5351 e_phnum values which we know must be invalid. */
5352 if (filedata->file_header.e_phnum
5353 * (is_32bit_elf ? sizeof (Elf32_External_Phdr) : sizeof (Elf64_External_Phdr))
5354 >= filedata->file_size)
5355 {
5356 error (_("Too many program headers - %#x - the file is not that big\n"),
5357 filedata->file_header.e_phnum);
5358 return false;
5359 }
5360
5361 phdrs = (Elf_Internal_Phdr *) cmalloc (filedata->file_header.e_phnum,
5362 sizeof (Elf_Internal_Phdr));
5363 if (phdrs == NULL)
5364 {
5365 error (_("Out of memory reading %u program headers\n"),
5366 filedata->file_header.e_phnum);
5367 return false;
5368 }
5369
5370 if (is_32bit_elf
5371 ? get_32bit_program_headers (filedata, phdrs)
5372 : get_64bit_program_headers (filedata, phdrs))
5373 {
5374 filedata->program_headers = phdrs;
5375 return true;
5376 }
5377
5378 free (phdrs);
5379 return false;
5380 }
5381
5382 /* Returns TRUE if the program headers were loaded. */
5383
5384 static bool
5385 process_program_headers (Filedata * filedata)
5386 {
5387 Elf_Internal_Phdr * segment;
5388 unsigned int i;
5389 Elf_Internal_Phdr * previous_load = NULL;
5390
5391 filedata->dynamic_addr = 0;
5392 filedata->dynamic_size = 0;
5393
5394 if (filedata->file_header.e_phnum == 0)
5395 {
5396 /* PR binutils/12467. */
5397 if (filedata->file_header.e_phoff != 0)
5398 {
5399 warn (_("possibly corrupt ELF header - it has a non-zero program"
5400 " header offset, but no program headers\n"));
5401 return false;
5402 }
5403 else if (do_segments)
5404 {
5405 if (filedata->is_separate)
5406 printf (_("\nThere are no program headers in linked file '%s'.\n"),
5407 filedata->file_name);
5408 else
5409 printf (_("\nThere are no program headers in this file.\n"));
5410 }
5411 return true;
5412 }
5413
5414 if (do_segments && !do_header)
5415 {
5416 if (filedata->is_separate)
5417 printf ("\nIn linked file '%s' the ELF file type is %s\n",
5418 filedata->file_name,
5419 get_file_type (filedata->file_header.e_type));
5420 else
5421 printf (_("\nElf file type is %s\n"), get_file_type (filedata->file_header.e_type));
5422 printf (_("Entry point 0x%s\n"), bfd_vmatoa ("x", filedata->file_header.e_entry));
5423 printf (ngettext ("There is %d program header, starting at offset %s\n",
5424 "There are %d program headers, starting at offset %s\n",
5425 filedata->file_header.e_phnum),
5426 filedata->file_header.e_phnum,
5427 bfd_vmatoa ("u", filedata->file_header.e_phoff));
5428 }
5429
5430 if (! get_program_headers (filedata))
5431 return true;
5432
5433 if (do_segments)
5434 {
5435 if (filedata->file_header.e_phnum > 1)
5436 printf (_("\nProgram Headers:\n"));
5437 else
5438 printf (_("\nProgram Headers:\n"));
5439
5440 if (is_32bit_elf)
5441 printf
5442 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
5443 else if (do_wide)
5444 printf
5445 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
5446 else
5447 {
5448 printf
5449 (_(" Type Offset VirtAddr PhysAddr\n"));
5450 printf
5451 (_(" FileSiz MemSiz Flags Align\n"));
5452 }
5453 }
5454
5455 for (i = 0, segment = filedata->program_headers;
5456 i < filedata->file_header.e_phnum;
5457 i++, segment++)
5458 {
5459 if (do_segments)
5460 {
5461 printf (" %-14.14s ", get_segment_type (filedata, segment->p_type));
5462
5463 if (is_32bit_elf)
5464 {
5465 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
5466 printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
5467 printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
5468 printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
5469 printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
5470 printf ("%c%c%c ",
5471 (segment->p_flags & PF_R ? 'R' : ' '),
5472 (segment->p_flags & PF_W ? 'W' : ' '),
5473 (segment->p_flags & PF_X ? 'E' : ' '));
5474 printf ("%#lx", (unsigned long) segment->p_align);
5475 }
5476 else if (do_wide)
5477 {
5478 if ((unsigned long) segment->p_offset == segment->p_offset)
5479 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
5480 else
5481 {
5482 print_vma (segment->p_offset, FULL_HEX);
5483 putchar (' ');
5484 }
5485
5486 print_vma (segment->p_vaddr, FULL_HEX);
5487 putchar (' ');
5488 print_vma (segment->p_paddr, FULL_HEX);
5489 putchar (' ');
5490
5491 if ((unsigned long) segment->p_filesz == segment->p_filesz)
5492 printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
5493 else
5494 {
5495 print_vma (segment->p_filesz, FULL_HEX);
5496 putchar (' ');
5497 }
5498
5499 if ((unsigned long) segment->p_memsz == segment->p_memsz)
5500 printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
5501 else
5502 {
5503 print_vma (segment->p_memsz, FULL_HEX);
5504 }
5505
5506 printf (" %c%c%c ",
5507 (segment->p_flags & PF_R ? 'R' : ' '),
5508 (segment->p_flags & PF_W ? 'W' : ' '),
5509 (segment->p_flags & PF_X ? 'E' : ' '));
5510
5511 if ((unsigned long) segment->p_align == segment->p_align)
5512 printf ("%#lx", (unsigned long) segment->p_align);
5513 else
5514 {
5515 print_vma (segment->p_align, PREFIX_HEX);
5516 }
5517 }
5518 else
5519 {
5520 print_vma (segment->p_offset, FULL_HEX);
5521 putchar (' ');
5522 print_vma (segment->p_vaddr, FULL_HEX);
5523 putchar (' ');
5524 print_vma (segment->p_paddr, FULL_HEX);
5525 printf ("\n ");
5526 print_vma (segment->p_filesz, FULL_HEX);
5527 putchar (' ');
5528 print_vma (segment->p_memsz, FULL_HEX);
5529 printf (" %c%c%c ",
5530 (segment->p_flags & PF_R ? 'R' : ' '),
5531 (segment->p_flags & PF_W ? 'W' : ' '),
5532 (segment->p_flags & PF_X ? 'E' : ' '));
5533 print_vma (segment->p_align, PREFIX_HEX);
5534 }
5535
5536 putc ('\n', stdout);
5537 }
5538
5539 switch (segment->p_type)
5540 {
5541 case PT_LOAD:
5542 #if 0 /* Do not warn about out of order PT_LOAD segments. Although officially
5543 required by the ELF standard, several programs, including the Linux
5544 kernel, make use of non-ordered segments. */
5545 if (previous_load
5546 && previous_load->p_vaddr > segment->p_vaddr)
5547 error (_("LOAD segments must be sorted in order of increasing VirtAddr\n"));
5548 #endif
5549 if (segment->p_memsz < segment->p_filesz)
5550 error (_("the segment's file size is larger than its memory size\n"));
5551 previous_load = segment;
5552 break;
5553
5554 case PT_PHDR:
5555 /* PR 20815 - Verify that the program header is loaded into memory. */
5556 if (i > 0 && previous_load != NULL)
5557 error (_("the PHDR segment must occur before any LOAD segment\n"));
5558 if (filedata->file_header.e_machine != EM_PARISC)
5559 {
5560 unsigned int j;
5561
5562 for (j = 1; j < filedata->file_header.e_phnum; j++)
5563 {
5564 Elf_Internal_Phdr *load = filedata->program_headers + j;
5565 if (load->p_type == PT_LOAD
5566 && load->p_offset <= segment->p_offset
5567 && (load->p_offset + load->p_filesz
5568 >= segment->p_offset + segment->p_filesz)
5569 && load->p_vaddr <= segment->p_vaddr
5570 && (load->p_vaddr + load->p_filesz
5571 >= segment->p_vaddr + segment->p_filesz))
5572 break;
5573 }
5574 if (j == filedata->file_header.e_phnum)
5575 error (_("the PHDR segment is not covered by a LOAD segment\n"));
5576 }
5577 break;
5578
5579 case PT_DYNAMIC:
5580 if (filedata->dynamic_addr)
5581 error (_("more than one dynamic segment\n"));
5582
5583 /* By default, assume that the .dynamic section is the first
5584 section in the DYNAMIC segment. */
5585 filedata->dynamic_addr = segment->p_offset;
5586 filedata->dynamic_size = segment->p_filesz;
5587
5588 /* Try to locate the .dynamic section. If there is
5589 a section header table, we can easily locate it. */
5590 if (filedata->section_headers != NULL)
5591 {
5592 Elf_Internal_Shdr * sec;
5593
5594 sec = find_section (filedata, ".dynamic");
5595 if (sec == NULL || sec->sh_size == 0)
5596 {
5597 /* A corresponding .dynamic section is expected, but on
5598 IA-64/OpenVMS it is OK for it to be missing. */
5599 if (!is_ia64_vms (filedata))
5600 error (_("no .dynamic section in the dynamic segment\n"));
5601 break;
5602 }
5603
5604 if (sec->sh_type == SHT_NOBITS)
5605 {
5606 filedata->dynamic_size = 0;
5607 break;
5608 }
5609
5610 filedata->dynamic_addr = sec->sh_offset;
5611 filedata->dynamic_size = sec->sh_size;
5612
5613 /* The PT_DYNAMIC segment, which is used by the run-time
5614 loader, should exactly match the .dynamic section. */
5615 if (do_checks
5616 && (filedata->dynamic_addr != segment->p_offset
5617 || filedata->dynamic_size != segment->p_filesz))
5618 warn (_("\
5619 the .dynamic section is not the same as the dynamic segment\n"));
5620 }
5621
5622 /* PR binutils/17512: Avoid corrupt dynamic section info in the
5623 segment. Check this after matching against the section headers
5624 so we don't warn on debuginfo file (which have NOBITS .dynamic
5625 sections). */
5626 if (filedata->dynamic_addr > filedata->file_size
5627 || (filedata->dynamic_size
5628 > filedata->file_size - filedata->dynamic_addr))
5629 {
5630 error (_("the dynamic segment offset + size exceeds the size of the file\n"));
5631 filedata->dynamic_addr = filedata->dynamic_size = 0;
5632 }
5633 break;
5634
5635 case PT_INTERP:
5636 if (segment->p_offset >= filedata->file_size
5637 || segment->p_filesz > filedata->file_size - segment->p_offset
5638 || segment->p_filesz - 1 >= (size_t) -2
5639 || fseek (filedata->handle,
5640 filedata->archive_file_offset + (long) segment->p_offset,
5641 SEEK_SET))
5642 error (_("Unable to find program interpreter name\n"));
5643 else
5644 {
5645 size_t len = segment->p_filesz;
5646 free (filedata->program_interpreter);
5647 filedata->program_interpreter = xmalloc (len + 1);
5648 len = fread (filedata->program_interpreter, 1, len,
5649 filedata->handle);
5650 filedata->program_interpreter[len] = 0;
5651
5652 if (do_segments)
5653 printf (_(" [Requesting program interpreter: %s]\n"),
5654 filedata->program_interpreter);
5655 }
5656 break;
5657 }
5658 }
5659
5660 if (do_segments
5661 && filedata->section_headers != NULL
5662 && filedata->string_table != NULL)
5663 {
5664 printf (_("\n Section to Segment mapping:\n"));
5665 printf (_(" Segment Sections...\n"));
5666
5667 for (i = 0; i < filedata->file_header.e_phnum; i++)
5668 {
5669 unsigned int j;
5670 Elf_Internal_Shdr * section;
5671
5672 segment = filedata->program_headers + i;
5673 section = filedata->section_headers + 1;
5674
5675 printf (" %2.2d ", i);
5676
5677 for (j = 1; j < filedata->file_header.e_shnum; j++, section++)
5678 {
5679 if (!ELF_TBSS_SPECIAL (section, segment)
5680 && ELF_SECTION_IN_SEGMENT_STRICT (section, segment))
5681 printf ("%s ", printable_section_name (filedata, section));
5682 }
5683
5684 putc ('\n',stdout);
5685 }
5686 }
5687
5688 return true;
5689 }
5690
5691
5692 /* Find the file offset corresponding to VMA by using the program headers. */
5693
5694 static long
5695 offset_from_vma (Filedata * filedata, bfd_vma vma, bfd_size_type size)
5696 {
5697 Elf_Internal_Phdr * seg;
5698
5699 if (! get_program_headers (filedata))
5700 {
5701 warn (_("Cannot interpret virtual addresses without program headers.\n"));
5702 return (long) vma;
5703 }
5704
5705 for (seg = filedata->program_headers;
5706 seg < filedata->program_headers + filedata->file_header.e_phnum;
5707 ++seg)
5708 {
5709 if (seg->p_type != PT_LOAD)
5710 continue;
5711
5712 if (vma >= (seg->p_vaddr & -seg->p_align)
5713 && vma + size <= seg->p_vaddr + seg->p_filesz)
5714 return vma - seg->p_vaddr + seg->p_offset;
5715 }
5716
5717 warn (_("Virtual address 0x%lx not located in any PT_LOAD segment.\n"),
5718 (unsigned long) vma);
5719 return (long) vma;
5720 }
5721
5722
5723 /* Allocate memory and load the sections headers into FILEDATA->filedata->section_headers.
5724 If PROBE is true, this is just a probe and we do not generate any error
5725 messages if the load fails. */
5726
5727 static bool
5728 get_32bit_section_headers (Filedata * filedata, bool probe)
5729 {
5730 Elf32_External_Shdr * shdrs;
5731 Elf_Internal_Shdr * internal;
5732 unsigned int i;
5733 unsigned int size = filedata->file_header.e_shentsize;
5734 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
5735
5736 /* PR binutils/17531: Cope with unexpected section header sizes. */
5737 if (size == 0 || num == 0)
5738 return false;
5739 if (size < sizeof * shdrs)
5740 {
5741 if (! probe)
5742 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
5743 return false;
5744 }
5745 if (!probe && size > sizeof * shdrs)
5746 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
5747
5748 shdrs = (Elf32_External_Shdr *) get_data (NULL, filedata, filedata->file_header.e_shoff,
5749 size, num,
5750 probe ? NULL : _("section headers"));
5751 if (shdrs == NULL)
5752 return false;
5753
5754 filedata->section_headers = (Elf_Internal_Shdr *)
5755 cmalloc (num, sizeof (Elf_Internal_Shdr));
5756 if (filedata->section_headers == NULL)
5757 {
5758 if (!probe)
5759 error (_("Out of memory reading %u section headers\n"), num);
5760 free (shdrs);
5761 return false;
5762 }
5763
5764 for (i = 0, internal = filedata->section_headers;
5765 i < num;
5766 i++, internal++)
5767 {
5768 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
5769 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
5770 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
5771 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
5772 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
5773 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
5774 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
5775 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
5776 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
5777 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
5778 if (!probe && internal->sh_link > num)
5779 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
5780 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
5781 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
5782 }
5783
5784 free (shdrs);
5785 return true;
5786 }
5787
5788 /* Like get_32bit_section_headers, except that it fetches 64-bit headers. */
5789
5790 static bool
5791 get_64bit_section_headers (Filedata * filedata, bool probe)
5792 {
5793 Elf64_External_Shdr * shdrs;
5794 Elf_Internal_Shdr * internal;
5795 unsigned int i;
5796 unsigned int size = filedata->file_header.e_shentsize;
5797 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
5798
5799 /* PR binutils/17531: Cope with unexpected section header sizes. */
5800 if (size == 0 || num == 0)
5801 return false;
5802
5803 if (size < sizeof * shdrs)
5804 {
5805 if (! probe)
5806 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
5807 return false;
5808 }
5809
5810 if (! probe && size > sizeof * shdrs)
5811 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
5812
5813 shdrs = (Elf64_External_Shdr *) get_data (NULL, filedata,
5814 filedata->file_header.e_shoff,
5815 size, num,
5816 probe ? NULL : _("section headers"));
5817 if (shdrs == NULL)
5818 return false;
5819
5820 filedata->section_headers = (Elf_Internal_Shdr *)
5821 cmalloc (num, sizeof (Elf_Internal_Shdr));
5822 if (filedata->section_headers == NULL)
5823 {
5824 if (! probe)
5825 error (_("Out of memory reading %u section headers\n"), num);
5826 free (shdrs);
5827 return false;
5828 }
5829
5830 for (i = 0, internal = filedata->section_headers;
5831 i < num;
5832 i++, internal++)
5833 {
5834 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
5835 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
5836 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
5837 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
5838 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
5839 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
5840 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
5841 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
5842 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
5843 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
5844 if (!probe && internal->sh_link > num)
5845 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
5846 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
5847 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
5848 }
5849
5850 free (shdrs);
5851 return true;
5852 }
5853
5854 static bool
5855 get_section_headers (Filedata *filedata, bool probe)
5856 {
5857 if (filedata->section_headers != NULL)
5858 return true;
5859
5860 if (is_32bit_elf)
5861 return get_32bit_section_headers (filedata, probe);
5862 else
5863 return get_64bit_section_headers (filedata, probe);
5864 }
5865
5866 static Elf_Internal_Sym *
5867 get_32bit_elf_symbols (Filedata * filedata,
5868 Elf_Internal_Shdr * section,
5869 unsigned long * num_syms_return)
5870 {
5871 unsigned long number = 0;
5872 Elf32_External_Sym * esyms = NULL;
5873 Elf_External_Sym_Shndx * shndx = NULL;
5874 Elf_Internal_Sym * isyms = NULL;
5875 Elf_Internal_Sym * psym;
5876 unsigned int j;
5877 elf_section_list * entry;
5878
5879 if (section->sh_size == 0)
5880 {
5881 if (num_syms_return != NULL)
5882 * num_syms_return = 0;
5883 return NULL;
5884 }
5885
5886 /* Run some sanity checks first. */
5887 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
5888 {
5889 error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
5890 printable_section_name (filedata, section),
5891 (unsigned long) section->sh_entsize);
5892 goto exit_point;
5893 }
5894
5895 if (section->sh_size > filedata->file_size)
5896 {
5897 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
5898 printable_section_name (filedata, section),
5899 (unsigned long) section->sh_size);
5900 goto exit_point;
5901 }
5902
5903 number = section->sh_size / section->sh_entsize;
5904
5905 if (number * sizeof (Elf32_External_Sym) > section->sh_size + 1)
5906 {
5907 error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
5908 (unsigned long) section->sh_size,
5909 printable_section_name (filedata, section),
5910 (unsigned long) section->sh_entsize);
5911 goto exit_point;
5912 }
5913
5914 esyms = (Elf32_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
5915 section->sh_size, _("symbols"));
5916 if (esyms == NULL)
5917 goto exit_point;
5918
5919 shndx = NULL;
5920 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
5921 {
5922 if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
5923 continue;
5924
5925 if (shndx != NULL)
5926 {
5927 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
5928 free (shndx);
5929 }
5930
5931 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
5932 entry->hdr->sh_offset,
5933 1, entry->hdr->sh_size,
5934 _("symbol table section indices"));
5935 if (shndx == NULL)
5936 goto exit_point;
5937
5938 /* PR17531: file: heap-buffer-overflow */
5939 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
5940 {
5941 error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
5942 printable_section_name (filedata, entry->hdr),
5943 (unsigned long) entry->hdr->sh_size,
5944 (unsigned long) section->sh_size);
5945 goto exit_point;
5946 }
5947 }
5948
5949 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
5950
5951 if (isyms == NULL)
5952 {
5953 error (_("Out of memory reading %lu symbols\n"),
5954 (unsigned long) number);
5955 goto exit_point;
5956 }
5957
5958 for (j = 0, psym = isyms; j < number; j++, psym++)
5959 {
5960 psym->st_name = BYTE_GET (esyms[j].st_name);
5961 psym->st_value = BYTE_GET (esyms[j].st_value);
5962 psym->st_size = BYTE_GET (esyms[j].st_size);
5963 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
5964 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
5965 psym->st_shndx
5966 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
5967 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
5968 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
5969 psym->st_info = BYTE_GET (esyms[j].st_info);
5970 psym->st_other = BYTE_GET (esyms[j].st_other);
5971 }
5972
5973 exit_point:
5974 free (shndx);
5975 free (esyms);
5976
5977 if (num_syms_return != NULL)
5978 * num_syms_return = isyms == NULL ? 0 : number;
5979
5980 return isyms;
5981 }
5982
5983 static Elf_Internal_Sym *
5984 get_64bit_elf_symbols (Filedata * filedata,
5985 Elf_Internal_Shdr * section,
5986 unsigned long * num_syms_return)
5987 {
5988 unsigned long number = 0;
5989 Elf64_External_Sym * esyms = NULL;
5990 Elf_External_Sym_Shndx * shndx = NULL;
5991 Elf_Internal_Sym * isyms = NULL;
5992 Elf_Internal_Sym * psym;
5993 unsigned int j;
5994 elf_section_list * entry;
5995
5996 if (section->sh_size == 0)
5997 {
5998 if (num_syms_return != NULL)
5999 * num_syms_return = 0;
6000 return NULL;
6001 }
6002
6003 /* Run some sanity checks first. */
6004 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
6005 {
6006 error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
6007 printable_section_name (filedata, section),
6008 (unsigned long) section->sh_entsize);
6009 goto exit_point;
6010 }
6011
6012 if (section->sh_size > filedata->file_size)
6013 {
6014 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
6015 printable_section_name (filedata, section),
6016 (unsigned long) section->sh_size);
6017 goto exit_point;
6018 }
6019
6020 number = section->sh_size / section->sh_entsize;
6021
6022 if (number * sizeof (Elf64_External_Sym) > section->sh_size + 1)
6023 {
6024 error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
6025 (unsigned long) section->sh_size,
6026 printable_section_name (filedata, section),
6027 (unsigned long) section->sh_entsize);
6028 goto exit_point;
6029 }
6030
6031 esyms = (Elf64_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
6032 section->sh_size, _("symbols"));
6033 if (!esyms)
6034 goto exit_point;
6035
6036 shndx = NULL;
6037 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
6038 {
6039 if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
6040 continue;
6041
6042 if (shndx != NULL)
6043 {
6044 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
6045 free (shndx);
6046 }
6047
6048 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
6049 entry->hdr->sh_offset,
6050 1, entry->hdr->sh_size,
6051 _("symbol table section indices"));
6052 if (shndx == NULL)
6053 goto exit_point;
6054
6055 /* PR17531: file: heap-buffer-overflow */
6056 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
6057 {
6058 error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
6059 printable_section_name (filedata, entry->hdr),
6060 (unsigned long) entry->hdr->sh_size,
6061 (unsigned long) section->sh_size);
6062 goto exit_point;
6063 }
6064 }
6065
6066 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
6067
6068 if (isyms == NULL)
6069 {
6070 error (_("Out of memory reading %lu symbols\n"),
6071 (unsigned long) number);
6072 goto exit_point;
6073 }
6074
6075 for (j = 0, psym = isyms; j < number; j++, psym++)
6076 {
6077 psym->st_name = BYTE_GET (esyms[j].st_name);
6078 psym->st_info = BYTE_GET (esyms[j].st_info);
6079 psym->st_other = BYTE_GET (esyms[j].st_other);
6080 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
6081
6082 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
6083 psym->st_shndx
6084 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
6085 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
6086 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
6087
6088 psym->st_value = BYTE_GET (esyms[j].st_value);
6089 psym->st_size = BYTE_GET (esyms[j].st_size);
6090 }
6091
6092 exit_point:
6093 free (shndx);
6094 free (esyms);
6095
6096 if (num_syms_return != NULL)
6097 * num_syms_return = isyms == NULL ? 0 : number;
6098
6099 return isyms;
6100 }
6101
6102 static Elf_Internal_Sym *
6103 get_elf_symbols (Filedata *filedata,
6104 Elf_Internal_Shdr *section,
6105 unsigned long *num_syms_return)
6106 {
6107 if (is_32bit_elf)
6108 return get_32bit_elf_symbols (filedata, section, num_syms_return);
6109 else
6110 return get_64bit_elf_symbols (filedata, section, num_syms_return);
6111 }
6112
6113 static const char *
6114 get_elf_section_flags (Filedata * filedata, bfd_vma sh_flags)
6115 {
6116 static char buff[1024];
6117 char * p = buff;
6118 unsigned int field_size = is_32bit_elf ? 8 : 16;
6119 signed int sindex;
6120 unsigned int size = sizeof (buff) - (field_size + 4 + 1);
6121 bfd_vma os_flags = 0;
6122 bfd_vma proc_flags = 0;
6123 bfd_vma unknown_flags = 0;
6124 static const struct
6125 {
6126 const char * str;
6127 unsigned int len;
6128 }
6129 flags [] =
6130 {
6131 /* 0 */ { STRING_COMMA_LEN ("WRITE") },
6132 /* 1 */ { STRING_COMMA_LEN ("ALLOC") },
6133 /* 2 */ { STRING_COMMA_LEN ("EXEC") },
6134 /* 3 */ { STRING_COMMA_LEN ("MERGE") },
6135 /* 4 */ { STRING_COMMA_LEN ("STRINGS") },
6136 /* 5 */ { STRING_COMMA_LEN ("INFO LINK") },
6137 /* 6 */ { STRING_COMMA_LEN ("LINK ORDER") },
6138 /* 7 */ { STRING_COMMA_LEN ("OS NONCONF") },
6139 /* 8 */ { STRING_COMMA_LEN ("GROUP") },
6140 /* 9 */ { STRING_COMMA_LEN ("TLS") },
6141 /* IA-64 specific. */
6142 /* 10 */ { STRING_COMMA_LEN ("SHORT") },
6143 /* 11 */ { STRING_COMMA_LEN ("NORECOV") },
6144 /* IA-64 OpenVMS specific. */
6145 /* 12 */ { STRING_COMMA_LEN ("VMS_GLOBAL") },
6146 /* 13 */ { STRING_COMMA_LEN ("VMS_OVERLAID") },
6147 /* 14 */ { STRING_COMMA_LEN ("VMS_SHARED") },
6148 /* 15 */ { STRING_COMMA_LEN ("VMS_VECTOR") },
6149 /* 16 */ { STRING_COMMA_LEN ("VMS_ALLOC_64BIT") },
6150 /* 17 */ { STRING_COMMA_LEN ("VMS_PROTECTED") },
6151 /* Generic. */
6152 /* 18 */ { STRING_COMMA_LEN ("EXCLUDE") },
6153 /* SPARC specific. */
6154 /* 19 */ { STRING_COMMA_LEN ("ORDERED") },
6155 /* 20 */ { STRING_COMMA_LEN ("COMPRESSED") },
6156 /* ARM specific. */
6157 /* 21 */ { STRING_COMMA_LEN ("ENTRYSECT") },
6158 /* 22 */ { STRING_COMMA_LEN ("ARM_PURECODE") },
6159 /* 23 */ { STRING_COMMA_LEN ("COMDEF") },
6160 /* GNU specific. */
6161 /* 24 */ { STRING_COMMA_LEN ("GNU_MBIND") },
6162 /* VLE specific. */
6163 /* 25 */ { STRING_COMMA_LEN ("VLE") },
6164 /* GNU specific. */
6165 /* 26 */ { STRING_COMMA_LEN ("GNU_RETAIN") },
6166 };
6167
6168 if (do_section_details)
6169 {
6170 sprintf (buff, "[%*.*lx]: ",
6171 field_size, field_size, (unsigned long) sh_flags);
6172 p += field_size + 4;
6173 }
6174
6175 while (sh_flags)
6176 {
6177 bfd_vma flag;
6178
6179 flag = sh_flags & - sh_flags;
6180 sh_flags &= ~ flag;
6181
6182 if (do_section_details)
6183 {
6184 switch (flag)
6185 {
6186 case SHF_WRITE: sindex = 0; break;
6187 case SHF_ALLOC: sindex = 1; break;
6188 case SHF_EXECINSTR: sindex = 2; break;
6189 case SHF_MERGE: sindex = 3; break;
6190 case SHF_STRINGS: sindex = 4; break;
6191 case SHF_INFO_LINK: sindex = 5; break;
6192 case SHF_LINK_ORDER: sindex = 6; break;
6193 case SHF_OS_NONCONFORMING: sindex = 7; break;
6194 case SHF_GROUP: sindex = 8; break;
6195 case SHF_TLS: sindex = 9; break;
6196 case SHF_EXCLUDE: sindex = 18; break;
6197 case SHF_COMPRESSED: sindex = 20; break;
6198
6199 default:
6200 sindex = -1;
6201 switch (filedata->file_header.e_machine)
6202 {
6203 case EM_IA_64:
6204 if (flag == SHF_IA_64_SHORT)
6205 sindex = 10;
6206 else if (flag == SHF_IA_64_NORECOV)
6207 sindex = 11;
6208 #ifdef BFD64
6209 else if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
6210 switch (flag)
6211 {
6212 case SHF_IA_64_VMS_GLOBAL: sindex = 12; break;
6213 case SHF_IA_64_VMS_OVERLAID: sindex = 13; break;
6214 case SHF_IA_64_VMS_SHARED: sindex = 14; break;
6215 case SHF_IA_64_VMS_VECTOR: sindex = 15; break;
6216 case SHF_IA_64_VMS_ALLOC_64BIT: sindex = 16; break;
6217 case SHF_IA_64_VMS_PROTECTED: sindex = 17; break;
6218 default: break;
6219 }
6220 #endif
6221 break;
6222
6223 case EM_386:
6224 case EM_IAMCU:
6225 case EM_X86_64:
6226 case EM_L1OM:
6227 case EM_K1OM:
6228 case EM_OLD_SPARCV9:
6229 case EM_SPARC32PLUS:
6230 case EM_SPARCV9:
6231 case EM_SPARC:
6232 if (flag == SHF_ORDERED)
6233 sindex = 19;
6234 break;
6235
6236 case EM_ARM:
6237 switch (flag)
6238 {
6239 case SHF_ENTRYSECT: sindex = 21; break;
6240 case SHF_ARM_PURECODE: sindex = 22; break;
6241 case SHF_COMDEF: sindex = 23; break;
6242 default: break;
6243 }
6244 break;
6245 case EM_PPC:
6246 if (flag == SHF_PPC_VLE)
6247 sindex = 25;
6248 break;
6249 default:
6250 break;
6251 }
6252
6253 switch (filedata->file_header.e_ident[EI_OSABI])
6254 {
6255 case ELFOSABI_GNU:
6256 case ELFOSABI_FREEBSD:
6257 if (flag == SHF_GNU_RETAIN)
6258 sindex = 26;
6259 /* Fall through */
6260 case ELFOSABI_NONE:
6261 if (flag == SHF_GNU_MBIND)
6262 /* We should not recognize SHF_GNU_MBIND for
6263 ELFOSABI_NONE, but binutils as of 2019-07-23 did
6264 not set the EI_OSABI header byte. */
6265 sindex = 24;
6266 break;
6267 default:
6268 break;
6269 }
6270 break;
6271 }
6272
6273 if (sindex != -1)
6274 {
6275 if (p != buff + field_size + 4)
6276 {
6277 if (size < (10 + 2))
6278 {
6279 warn (_("Internal error: not enough buffer room for section flag info"));
6280 return _("<unknown>");
6281 }
6282 size -= 2;
6283 *p++ = ',';
6284 *p++ = ' ';
6285 }
6286
6287 size -= flags [sindex].len;
6288 p = stpcpy (p, flags [sindex].str);
6289 }
6290 else if (flag & SHF_MASKOS)
6291 os_flags |= flag;
6292 else if (flag & SHF_MASKPROC)
6293 proc_flags |= flag;
6294 else
6295 unknown_flags |= flag;
6296 }
6297 else
6298 {
6299 switch (flag)
6300 {
6301 case SHF_WRITE: *p = 'W'; break;
6302 case SHF_ALLOC: *p = 'A'; break;
6303 case SHF_EXECINSTR: *p = 'X'; break;
6304 case SHF_MERGE: *p = 'M'; break;
6305 case SHF_STRINGS: *p = 'S'; break;
6306 case SHF_INFO_LINK: *p = 'I'; break;
6307 case SHF_LINK_ORDER: *p = 'L'; break;
6308 case SHF_OS_NONCONFORMING: *p = 'O'; break;
6309 case SHF_GROUP: *p = 'G'; break;
6310 case SHF_TLS: *p = 'T'; break;
6311 case SHF_EXCLUDE: *p = 'E'; break;
6312 case SHF_COMPRESSED: *p = 'C'; break;
6313
6314 default:
6315 if ((filedata->file_header.e_machine == EM_X86_64
6316 || filedata->file_header.e_machine == EM_L1OM
6317 || filedata->file_header.e_machine == EM_K1OM)
6318 && flag == SHF_X86_64_LARGE)
6319 *p = 'l';
6320 else if (filedata->file_header.e_machine == EM_ARM
6321 && flag == SHF_ARM_PURECODE)
6322 *p = 'y';
6323 else if (filedata->file_header.e_machine == EM_PPC
6324 && flag == SHF_PPC_VLE)
6325 *p = 'v';
6326 else if (flag & SHF_MASKOS)
6327 {
6328 switch (filedata->file_header.e_ident[EI_OSABI])
6329 {
6330 case ELFOSABI_GNU:
6331 case ELFOSABI_FREEBSD:
6332 if (flag == SHF_GNU_RETAIN)
6333 {
6334 *p = 'R';
6335 break;
6336 }
6337 /* Fall through */
6338 case ELFOSABI_NONE:
6339 if (flag == SHF_GNU_MBIND)
6340 {
6341 /* We should not recognize SHF_GNU_MBIND for
6342 ELFOSABI_NONE, but binutils as of 2019-07-23 did
6343 not set the EI_OSABI header byte. */
6344 *p = 'D';
6345 break;
6346 }
6347 /* Fall through */
6348 default:
6349 *p = 'o';
6350 sh_flags &= ~SHF_MASKOS;
6351 break;
6352 }
6353 }
6354 else if (flag & SHF_MASKPROC)
6355 {
6356 *p = 'p';
6357 sh_flags &= ~ SHF_MASKPROC;
6358 }
6359 else
6360 *p = 'x';
6361 break;
6362 }
6363 p++;
6364 }
6365 }
6366
6367 if (do_section_details)
6368 {
6369 if (os_flags)
6370 {
6371 size -= 5 + field_size;
6372 if (p != buff + field_size + 4)
6373 {
6374 if (size < (2 + 1))
6375 {
6376 warn (_("Internal error: not enough buffer room for section flag info"));
6377 return _("<unknown>");
6378 }
6379 size -= 2;
6380 *p++ = ',';
6381 *p++ = ' ';
6382 }
6383 sprintf (p, "OS (%*.*lx)", field_size, field_size,
6384 (unsigned long) os_flags);
6385 p += 5 + field_size;
6386 }
6387 if (proc_flags)
6388 {
6389 size -= 7 + field_size;
6390 if (p != buff + field_size + 4)
6391 {
6392 if (size < (2 + 1))
6393 {
6394 warn (_("Internal error: not enough buffer room for section flag info"));
6395 return _("<unknown>");
6396 }
6397 size -= 2;
6398 *p++ = ',';
6399 *p++ = ' ';
6400 }
6401 sprintf (p, "PROC (%*.*lx)", field_size, field_size,
6402 (unsigned long) proc_flags);
6403 p += 7 + field_size;
6404 }
6405 if (unknown_flags)
6406 {
6407 size -= 10 + field_size;
6408 if (p != buff + field_size + 4)
6409 {
6410 if (size < (2 + 1))
6411 {
6412 warn (_("Internal error: not enough buffer room for section flag info"));
6413 return _("<unknown>");
6414 }
6415 size -= 2;
6416 *p++ = ',';
6417 *p++ = ' ';
6418 }
6419 sprintf (p, _("UNKNOWN (%*.*lx)"), field_size, field_size,
6420 (unsigned long) unknown_flags);
6421 p += 10 + field_size;
6422 }
6423 }
6424
6425 *p = '\0';
6426 return buff;
6427 }
6428
6429 static unsigned int ATTRIBUTE_WARN_UNUSED_RESULT
6430 get_compression_header (Elf_Internal_Chdr *chdr, unsigned char *buf, bfd_size_type size)
6431 {
6432 if (is_32bit_elf)
6433 {
6434 Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) buf;
6435
6436 if (size < sizeof (* echdr))
6437 {
6438 error (_("Compressed section is too small even for a compression header\n"));
6439 return 0;
6440 }
6441
6442 chdr->ch_type = BYTE_GET (echdr->ch_type);
6443 chdr->ch_size = BYTE_GET (echdr->ch_size);
6444 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
6445 return sizeof (*echdr);
6446 }
6447 else
6448 {
6449 Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) buf;
6450
6451 if (size < sizeof (* echdr))
6452 {
6453 error (_("Compressed section is too small even for a compression header\n"));
6454 return 0;
6455 }
6456
6457 chdr->ch_type = BYTE_GET (echdr->ch_type);
6458 chdr->ch_size = BYTE_GET (echdr->ch_size);
6459 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
6460 return sizeof (*echdr);
6461 }
6462 }
6463
6464 static bool
6465 process_section_headers (Filedata * filedata)
6466 {
6467 Elf_Internal_Shdr * section;
6468 unsigned int i;
6469
6470 if (filedata->file_header.e_shnum == 0)
6471 {
6472 /* PR binutils/12467. */
6473 if (filedata->file_header.e_shoff != 0)
6474 {
6475 warn (_("possibly corrupt ELF file header - it has a non-zero"
6476 " section header offset, but no section headers\n"));
6477 return false;
6478 }
6479 else if (do_sections)
6480 printf (_("\nThere are no sections in this file.\n"));
6481
6482 return true;
6483 }
6484
6485 if (do_sections && !do_header)
6486 {
6487 if (filedata->is_separate && process_links)
6488 printf (_("In linked file '%s': "), filedata->file_name);
6489 if (! filedata->is_separate || process_links)
6490 printf (ngettext ("There is %d section header, "
6491 "starting at offset 0x%lx:\n",
6492 "There are %d section headers, "
6493 "starting at offset 0x%lx:\n",
6494 filedata->file_header.e_shnum),
6495 filedata->file_header.e_shnum,
6496 (unsigned long) filedata->file_header.e_shoff);
6497 }
6498
6499 if (!get_section_headers (filedata, false))
6500 return false;
6501
6502 /* Read in the string table, so that we have names to display. */
6503 if (filedata->file_header.e_shstrndx != SHN_UNDEF
6504 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
6505 {
6506 section = filedata->section_headers + filedata->file_header.e_shstrndx;
6507
6508 if (section->sh_size != 0)
6509 {
6510 filedata->string_table = (char *) get_data (NULL, filedata, section->sh_offset,
6511 1, section->sh_size,
6512 _("string table"));
6513
6514 filedata->string_table_length = filedata->string_table != NULL ? section->sh_size : 0;
6515 }
6516 }
6517
6518 /* Scan the sections for the dynamic symbol table
6519 and dynamic string table and debug sections. */
6520 eh_addr_size = is_32bit_elf ? 4 : 8;
6521 switch (filedata->file_header.e_machine)
6522 {
6523 case EM_MIPS:
6524 case EM_MIPS_RS3_LE:
6525 /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
6526 FDE addresses. However, the ABI also has a semi-official ILP32
6527 variant for which the normal FDE address size rules apply.
6528
6529 GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
6530 section, where XX is the size of longs in bits. Unfortunately,
6531 earlier compilers provided no way of distinguishing ILP32 objects
6532 from LP64 objects, so if there's any doubt, we should assume that
6533 the official LP64 form is being used. */
6534 if ((filedata->file_header.e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64
6535 && find_section (filedata, ".gcc_compiled_long32") == NULL)
6536 eh_addr_size = 8;
6537 break;
6538
6539 case EM_H8_300:
6540 case EM_H8_300H:
6541 switch (filedata->file_header.e_flags & EF_H8_MACH)
6542 {
6543 case E_H8_MACH_H8300:
6544 case E_H8_MACH_H8300HN:
6545 case E_H8_MACH_H8300SN:
6546 case E_H8_MACH_H8300SXN:
6547 eh_addr_size = 2;
6548 break;
6549 case E_H8_MACH_H8300H:
6550 case E_H8_MACH_H8300S:
6551 case E_H8_MACH_H8300SX:
6552 eh_addr_size = 4;
6553 break;
6554 }
6555 break;
6556
6557 case EM_M32C_OLD:
6558 case EM_M32C:
6559 switch (filedata->file_header.e_flags & EF_M32C_CPU_MASK)
6560 {
6561 case EF_M32C_CPU_M16C:
6562 eh_addr_size = 2;
6563 break;
6564 }
6565 break;
6566 }
6567
6568 #define CHECK_ENTSIZE_VALUES(section, i, size32, size64) \
6569 do \
6570 { \
6571 bfd_size_type expected_entsize = is_32bit_elf ? size32 : size64; \
6572 if (section->sh_entsize != expected_entsize) \
6573 { \
6574 char buf[40]; \
6575 sprintf_vma (buf, section->sh_entsize); \
6576 /* Note: coded this way so that there is a single string for \
6577 translation. */ \
6578 error (_("Section %d has invalid sh_entsize of %s\n"), i, buf); \
6579 error (_("(Using the expected size of %u for the rest of this dump)\n"), \
6580 (unsigned) expected_entsize); \
6581 section->sh_entsize = expected_entsize; \
6582 } \
6583 } \
6584 while (0)
6585
6586 #define CHECK_ENTSIZE(section, i, type) \
6587 CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type), \
6588 sizeof (Elf64_External_##type))
6589
6590 for (i = 0, section = filedata->section_headers;
6591 i < filedata->file_header.e_shnum;
6592 i++, section++)
6593 {
6594 char * name = SECTION_NAME_PRINT (section);
6595
6596 /* Run some sanity checks on the headers and
6597 possibly fill in some file data as well. */
6598 switch (section->sh_type)
6599 {
6600 case SHT_DYNSYM:
6601 if (filedata->dynamic_symbols != NULL)
6602 {
6603 error (_("File contains multiple dynamic symbol tables\n"));
6604 continue;
6605 }
6606
6607 CHECK_ENTSIZE (section, i, Sym);
6608 filedata->dynamic_symbols
6609 = get_elf_symbols (filedata, section, &filedata->num_dynamic_syms);
6610 filedata->dynamic_symtab_section = section;
6611 break;
6612
6613 case SHT_STRTAB:
6614 if (streq (name, ".dynstr"))
6615 {
6616 if (filedata->dynamic_strings != NULL)
6617 {
6618 error (_("File contains multiple dynamic string tables\n"));
6619 continue;
6620 }
6621
6622 filedata->dynamic_strings
6623 = (char *) get_data (NULL, filedata, section->sh_offset,
6624 1, section->sh_size, _("dynamic strings"));
6625 filedata->dynamic_strings_length
6626 = filedata->dynamic_strings == NULL ? 0 : section->sh_size;
6627 filedata->dynamic_strtab_section = section;
6628 }
6629 break;
6630
6631 case SHT_SYMTAB_SHNDX:
6632 {
6633 elf_section_list * entry = xmalloc (sizeof * entry);
6634
6635 entry->hdr = section;
6636 entry->next = filedata->symtab_shndx_list;
6637 filedata->symtab_shndx_list = entry;
6638 }
6639 break;
6640
6641 case SHT_SYMTAB:
6642 CHECK_ENTSIZE (section, i, Sym);
6643 break;
6644
6645 case SHT_GROUP:
6646 CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
6647 break;
6648
6649 case SHT_REL:
6650 CHECK_ENTSIZE (section, i, Rel);
6651 if (do_checks && section->sh_size == 0)
6652 warn (_("Section '%s': zero-sized relocation section\n"), name);
6653 break;
6654
6655 case SHT_RELA:
6656 CHECK_ENTSIZE (section, i, Rela);
6657 if (do_checks && section->sh_size == 0)
6658 warn (_("Section '%s': zero-sized relocation section\n"), name);
6659 break;
6660
6661 case SHT_NOTE:
6662 case SHT_PROGBITS:
6663 /* Having a zero sized section is not illegal according to the
6664 ELF standard, but it might be an indication that something
6665 is wrong. So issue a warning if we are running in lint mode. */
6666 if (do_checks && section->sh_size == 0)
6667 warn (_("Section '%s': has a size of zero - is this intended ?\n"), name);
6668 break;
6669
6670 default:
6671 break;
6672 }
6673
6674 if ((do_debugging || do_debug_info || do_debug_abbrevs
6675 || do_debug_lines || do_debug_pubnames || do_debug_pubtypes
6676 || do_debug_aranges || do_debug_frames || do_debug_macinfo
6677 || do_debug_str || do_debug_str_offsets || do_debug_loc
6678 || do_debug_ranges
6679 || do_debug_addr || do_debug_cu_index || do_debug_links)
6680 && (startswith (name, ".debug_")
6681 || startswith (name, ".zdebug_")))
6682 {
6683 if (name[1] == 'z')
6684 name += sizeof (".zdebug_") - 1;
6685 else
6686 name += sizeof (".debug_") - 1;
6687
6688 if (do_debugging
6689 || (do_debug_info && startswith (name, "info"))
6690 || (do_debug_info && startswith (name, "types"))
6691 || (do_debug_abbrevs && startswith (name, "abbrev"))
6692 || (do_debug_lines && strcmp (name, "line") == 0)
6693 || (do_debug_lines && startswith (name, "line."))
6694 || (do_debug_pubnames && startswith (name, "pubnames"))
6695 || (do_debug_pubtypes && startswith (name, "pubtypes"))
6696 || (do_debug_pubnames && startswith (name, "gnu_pubnames"))
6697 || (do_debug_pubtypes && startswith (name, "gnu_pubtypes"))
6698 || (do_debug_aranges && startswith (name, "aranges"))
6699 || (do_debug_ranges && startswith (name, "ranges"))
6700 || (do_debug_ranges && startswith (name, "rnglists"))
6701 || (do_debug_frames && startswith (name, "frame"))
6702 || (do_debug_macinfo && startswith (name, "macinfo"))
6703 || (do_debug_macinfo && startswith (name, "macro"))
6704 || (do_debug_str && startswith (name, "str"))
6705 || (do_debug_links && startswith (name, "sup"))
6706 || (do_debug_str_offsets && startswith (name, "str_offsets"))
6707 || (do_debug_loc && startswith (name, "loc"))
6708 || (do_debug_loc && startswith (name, "loclists"))
6709 || (do_debug_addr && startswith (name, "addr"))
6710 || (do_debug_cu_index && startswith (name, "cu_index"))
6711 || (do_debug_cu_index && startswith (name, "tu_index"))
6712 )
6713 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6714 }
6715 /* Linkonce section to be combined with .debug_info at link time. */
6716 else if ((do_debugging || do_debug_info)
6717 && startswith (name, ".gnu.linkonce.wi."))
6718 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6719 else if (do_debug_frames && streq (name, ".eh_frame"))
6720 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6721 else if (do_gdb_index && (streq (name, ".gdb_index")
6722 || streq (name, ".debug_names")))
6723 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6724 /* Trace sections for Itanium VMS. */
6725 else if ((do_debugging || do_trace_info || do_trace_abbrevs
6726 || do_trace_aranges)
6727 && startswith (name, ".trace_"))
6728 {
6729 name += sizeof (".trace_") - 1;
6730
6731 if (do_debugging
6732 || (do_trace_info && streq (name, "info"))
6733 || (do_trace_abbrevs && streq (name, "abbrev"))
6734 || (do_trace_aranges && streq (name, "aranges"))
6735 )
6736 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6737 }
6738 else if ((do_debugging || do_debug_links)
6739 && (startswith (name, ".gnu_debuglink")
6740 || startswith (name, ".gnu_debugaltlink")))
6741 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6742 }
6743
6744 if (! do_sections)
6745 return true;
6746
6747 if (filedata->is_separate && ! process_links)
6748 return true;
6749
6750 if (filedata->is_separate)
6751 printf (_("\nSection Headers in linked file '%s':\n"), filedata->file_name);
6752 else if (filedata->file_header.e_shnum > 1)
6753 printf (_("\nSection Headers:\n"));
6754 else
6755 printf (_("\nSection Header:\n"));
6756
6757 if (is_32bit_elf)
6758 {
6759 if (do_section_details)
6760 {
6761 printf (_(" [Nr] Name\n"));
6762 printf (_(" Type Addr Off Size ES Lk Inf Al\n"));
6763 }
6764 else
6765 printf
6766 (_(" [Nr] Name Type Addr Off Size ES Flg Lk Inf Al\n"));
6767 }
6768 else if (do_wide)
6769 {
6770 if (do_section_details)
6771 {
6772 printf (_(" [Nr] Name\n"));
6773 printf (_(" Type Address Off Size ES Lk Inf Al\n"));
6774 }
6775 else
6776 printf
6777 (_(" [Nr] Name Type Address Off Size ES Flg Lk Inf Al\n"));
6778 }
6779 else
6780 {
6781 if (do_section_details)
6782 {
6783 printf (_(" [Nr] Name\n"));
6784 printf (_(" Type Address Offset Link\n"));
6785 printf (_(" Size EntSize Info Align\n"));
6786 }
6787 else
6788 {
6789 printf (_(" [Nr] Name Type Address Offset\n"));
6790 printf (_(" Size EntSize Flags Link Info Align\n"));
6791 }
6792 }
6793
6794 if (do_section_details)
6795 printf (_(" Flags\n"));
6796
6797 for (i = 0, section = filedata->section_headers;
6798 i < filedata->file_header.e_shnum;
6799 i++, section++)
6800 {
6801 /* Run some sanity checks on the section header. */
6802
6803 /* Check the sh_link field. */
6804 switch (section->sh_type)
6805 {
6806 case SHT_REL:
6807 case SHT_RELA:
6808 if (section->sh_link == 0
6809 && (filedata->file_header.e_type == ET_EXEC
6810 || filedata->file_header.e_type == ET_DYN))
6811 /* A dynamic relocation section where all entries use a
6812 zero symbol index need not specify a symtab section. */
6813 break;
6814 /* Fall through. */
6815 case SHT_SYMTAB_SHNDX:
6816 case SHT_GROUP:
6817 case SHT_HASH:
6818 case SHT_GNU_HASH:
6819 case SHT_GNU_versym:
6820 if (section->sh_link == 0
6821 || section->sh_link >= filedata->file_header.e_shnum
6822 || (filedata->section_headers[section->sh_link].sh_type != SHT_SYMTAB
6823 && filedata->section_headers[section->sh_link].sh_type != SHT_DYNSYM))
6824 warn (_("[%2u]: Link field (%u) should index a symtab section.\n"),
6825 i, section->sh_link);
6826 break;
6827
6828 case SHT_DYNAMIC:
6829 case SHT_SYMTAB:
6830 case SHT_DYNSYM:
6831 case SHT_GNU_verneed:
6832 case SHT_GNU_verdef:
6833 case SHT_GNU_LIBLIST:
6834 if (section->sh_link == 0
6835 || section->sh_link >= filedata->file_header.e_shnum
6836 || filedata->section_headers[section->sh_link].sh_type != SHT_STRTAB)
6837 warn (_("[%2u]: Link field (%u) should index a string section.\n"),
6838 i, section->sh_link);
6839 break;
6840
6841 case SHT_INIT_ARRAY:
6842 case SHT_FINI_ARRAY:
6843 case SHT_PREINIT_ARRAY:
6844 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
6845 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
6846 i, section->sh_link);
6847 break;
6848
6849 default:
6850 /* FIXME: Add support for target specific section types. */
6851 #if 0 /* Currently we do not check other section types as there are too
6852 many special cases. Stab sections for example have a type
6853 of SHT_PROGBITS but an sh_link field that links to the .stabstr
6854 section. */
6855 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
6856 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
6857 i, section->sh_link);
6858 #endif
6859 break;
6860 }
6861
6862 /* Check the sh_info field. */
6863 switch (section->sh_type)
6864 {
6865 case SHT_REL:
6866 case SHT_RELA:
6867 if (section->sh_info == 0
6868 && (filedata->file_header.e_type == ET_EXEC
6869 || filedata->file_header.e_type == ET_DYN))
6870 /* Dynamic relocations apply to segments, so they do not
6871 need to specify the section they relocate. */
6872 break;
6873 if (section->sh_info == 0
6874 || section->sh_info >= filedata->file_header.e_shnum
6875 || (filedata->section_headers[section->sh_info].sh_type != SHT_PROGBITS
6876 && filedata->section_headers[section->sh_info].sh_type != SHT_NOBITS
6877 && filedata->section_headers[section->sh_info].sh_type != SHT_NOTE
6878 && filedata->section_headers[section->sh_info].sh_type != SHT_INIT_ARRAY
6879 && filedata->section_headers[section->sh_info].sh_type != SHT_FINI_ARRAY
6880 && filedata->section_headers[section->sh_info].sh_type != SHT_PREINIT_ARRAY
6881 /* FIXME: Are other section types valid ? */
6882 && filedata->section_headers[section->sh_info].sh_type < SHT_LOOS))
6883 warn (_("[%2u]: Info field (%u) should index a relocatable section.\n"),
6884 i, section->sh_info);
6885 break;
6886
6887 case SHT_DYNAMIC:
6888 case SHT_HASH:
6889 case SHT_SYMTAB_SHNDX:
6890 case SHT_INIT_ARRAY:
6891 case SHT_FINI_ARRAY:
6892 case SHT_PREINIT_ARRAY:
6893 if (section->sh_info != 0)
6894 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
6895 i, section->sh_info);
6896 break;
6897
6898 case SHT_GROUP:
6899 case SHT_SYMTAB:
6900 case SHT_DYNSYM:
6901 /* A symbol index - we assume that it is valid. */
6902 break;
6903
6904 default:
6905 /* FIXME: Add support for target specific section types. */
6906 if (section->sh_type == SHT_NOBITS)
6907 /* NOBITS section headers with non-zero sh_info fields can be
6908 created when a binary is stripped of everything but its debug
6909 information. The stripped sections have their headers
6910 preserved but their types set to SHT_NOBITS. So do not check
6911 this type of section. */
6912 ;
6913 else if (section->sh_flags & SHF_INFO_LINK)
6914 {
6915 if (section->sh_info < 1 || section->sh_info >= filedata->file_header.e_shnum)
6916 warn (_("[%2u]: Expected link to another section in info field"), i);
6917 }
6918 else if (section->sh_type < SHT_LOOS
6919 && (section->sh_flags & SHF_GNU_MBIND) == 0
6920 && section->sh_info != 0)
6921 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
6922 i, section->sh_info);
6923 break;
6924 }
6925
6926 /* Check the sh_size field. */
6927 if (section->sh_size > filedata->file_size
6928 && section->sh_type != SHT_NOBITS
6929 && section->sh_type != SHT_NULL
6930 && section->sh_type < SHT_LOOS)
6931 warn (_("Size of section %u is larger than the entire file!\n"), i);
6932
6933 printf (" [%2u] ", i);
6934 if (do_section_details)
6935 printf ("%s\n ", printable_section_name (filedata, section));
6936 else
6937 print_symbol (-17, SECTION_NAME_PRINT (section));
6938
6939 printf (do_wide ? " %-15s " : " %-15.15s ",
6940 get_section_type_name (filedata, section->sh_type));
6941
6942 if (is_32bit_elf)
6943 {
6944 const char * link_too_big = NULL;
6945
6946 print_vma (section->sh_addr, LONG_HEX);
6947
6948 printf ( " %6.6lx %6.6lx %2.2lx",
6949 (unsigned long) section->sh_offset,
6950 (unsigned long) section->sh_size,
6951 (unsigned long) section->sh_entsize);
6952
6953 if (do_section_details)
6954 fputs (" ", stdout);
6955 else
6956 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
6957
6958 if (section->sh_link >= filedata->file_header.e_shnum)
6959 {
6960 link_too_big = "";
6961 /* The sh_link value is out of range. Normally this indicates
6962 an error but it can have special values in Solaris binaries. */
6963 switch (filedata->file_header.e_machine)
6964 {
6965 case EM_386:
6966 case EM_IAMCU:
6967 case EM_X86_64:
6968 case EM_L1OM:
6969 case EM_K1OM:
6970 case EM_OLD_SPARCV9:
6971 case EM_SPARC32PLUS:
6972 case EM_SPARCV9:
6973 case EM_SPARC:
6974 if (section->sh_link == (SHN_BEFORE & 0xffff))
6975 link_too_big = "BEFORE";
6976 else if (section->sh_link == (SHN_AFTER & 0xffff))
6977 link_too_big = "AFTER";
6978 break;
6979 default:
6980 break;
6981 }
6982 }
6983
6984 if (do_section_details)
6985 {
6986 if (link_too_big != NULL && * link_too_big)
6987 printf ("<%s> ", link_too_big);
6988 else
6989 printf ("%2u ", section->sh_link);
6990 printf ("%3u %2lu\n", section->sh_info,
6991 (unsigned long) section->sh_addralign);
6992 }
6993 else
6994 printf ("%2u %3u %2lu\n",
6995 section->sh_link,
6996 section->sh_info,
6997 (unsigned long) section->sh_addralign);
6998
6999 if (link_too_big && ! * link_too_big)
7000 warn (_("section %u: sh_link value of %u is larger than the number of sections\n"),
7001 i, section->sh_link);
7002 }
7003 else if (do_wide)
7004 {
7005 print_vma (section->sh_addr, LONG_HEX);
7006
7007 if ((long) section->sh_offset == section->sh_offset)
7008 printf (" %6.6lx", (unsigned long) section->sh_offset);
7009 else
7010 {
7011 putchar (' ');
7012 print_vma (section->sh_offset, LONG_HEX);
7013 }
7014
7015 if ((unsigned long) section->sh_size == section->sh_size)
7016 printf (" %6.6lx", (unsigned long) section->sh_size);
7017 else
7018 {
7019 putchar (' ');
7020 print_vma (section->sh_size, LONG_HEX);
7021 }
7022
7023 if ((unsigned long) section->sh_entsize == section->sh_entsize)
7024 printf (" %2.2lx", (unsigned long) section->sh_entsize);
7025 else
7026 {
7027 putchar (' ');
7028 print_vma (section->sh_entsize, LONG_HEX);
7029 }
7030
7031 if (do_section_details)
7032 fputs (" ", stdout);
7033 else
7034 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7035
7036 printf ("%2u %3u ", section->sh_link, section->sh_info);
7037
7038 if ((unsigned long) section->sh_addralign == section->sh_addralign)
7039 printf ("%2lu\n", (unsigned long) section->sh_addralign);
7040 else
7041 {
7042 print_vma (section->sh_addralign, DEC);
7043 putchar ('\n');
7044 }
7045 }
7046 else if (do_section_details)
7047 {
7048 putchar (' ');
7049 print_vma (section->sh_addr, LONG_HEX);
7050 if ((long) section->sh_offset == section->sh_offset)
7051 printf (" %16.16lx", (unsigned long) section->sh_offset);
7052 else
7053 {
7054 printf (" ");
7055 print_vma (section->sh_offset, LONG_HEX);
7056 }
7057 printf (" %u\n ", section->sh_link);
7058 print_vma (section->sh_size, LONG_HEX);
7059 putchar (' ');
7060 print_vma (section->sh_entsize, LONG_HEX);
7061
7062 printf (" %-16u %lu\n",
7063 section->sh_info,
7064 (unsigned long) section->sh_addralign);
7065 }
7066 else
7067 {
7068 putchar (' ');
7069 print_vma (section->sh_addr, LONG_HEX);
7070 if ((long) section->sh_offset == section->sh_offset)
7071 printf (" %8.8lx", (unsigned long) section->sh_offset);
7072 else
7073 {
7074 printf (" ");
7075 print_vma (section->sh_offset, LONG_HEX);
7076 }
7077 printf ("\n ");
7078 print_vma (section->sh_size, LONG_HEX);
7079 printf (" ");
7080 print_vma (section->sh_entsize, LONG_HEX);
7081
7082 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7083
7084 printf (" %2u %3u %lu\n",
7085 section->sh_link,
7086 section->sh_info,
7087 (unsigned long) section->sh_addralign);
7088 }
7089
7090 if (do_section_details)
7091 {
7092 printf (" %s\n", get_elf_section_flags (filedata, section->sh_flags));
7093 if ((section->sh_flags & SHF_COMPRESSED) != 0)
7094 {
7095 /* Minimum section size is 12 bytes for 32-bit compression
7096 header + 12 bytes for compressed data header. */
7097 unsigned char buf[24];
7098
7099 assert (sizeof (buf) >= sizeof (Elf64_External_Chdr));
7100 if (get_data (&buf, filedata, section->sh_offset, 1,
7101 sizeof (buf), _("compression header")))
7102 {
7103 Elf_Internal_Chdr chdr;
7104
7105 if (get_compression_header (&chdr, buf, sizeof (buf)) == 0)
7106 printf (_(" [<corrupt>]\n"));
7107 else
7108 {
7109 if (chdr.ch_type == ELFCOMPRESS_ZLIB)
7110 printf (" ZLIB, ");
7111 else
7112 printf (_(" [<unknown>: 0x%x], "),
7113 chdr.ch_type);
7114 print_vma (chdr.ch_size, LONG_HEX);
7115 printf (", %lu\n", (unsigned long) chdr.ch_addralign);
7116 }
7117 }
7118 }
7119 }
7120 }
7121
7122 if (!do_section_details)
7123 {
7124 /* The ordering of the letters shown here matches the ordering of the
7125 corresponding SHF_xxx values, and hence the order in which these
7126 letters will be displayed to the user. */
7127 printf (_("Key to Flags:\n\
7128 W (write), A (alloc), X (execute), M (merge), S (strings), I (info),\n\
7129 L (link order), O (extra OS processing required), G (group), T (TLS),\n\
7130 C (compressed), x (unknown), o (OS specific), E (exclude),\n "));
7131 switch (filedata->file_header.e_ident[EI_OSABI])
7132 {
7133 case ELFOSABI_GNU:
7134 case ELFOSABI_FREEBSD:
7135 printf (_("R (retain), "));
7136 /* Fall through */
7137 case ELFOSABI_NONE:
7138 printf (_("D (mbind), "));
7139 break;
7140 default:
7141 break;
7142 }
7143 if (filedata->file_header.e_machine == EM_X86_64
7144 || filedata->file_header.e_machine == EM_L1OM
7145 || filedata->file_header.e_machine == EM_K1OM)
7146 printf (_("l (large), "));
7147 else if (filedata->file_header.e_machine == EM_ARM)
7148 printf (_("y (purecode), "));
7149 else if (filedata->file_header.e_machine == EM_PPC)
7150 printf (_("v (VLE), "));
7151 printf ("p (processor specific)\n");
7152 }
7153
7154 return true;
7155 }
7156
7157 static bool
7158 get_symtab (Filedata *filedata, Elf_Internal_Shdr *symsec,
7159 Elf_Internal_Sym **symtab, unsigned long *nsyms,
7160 char **strtab, unsigned long *strtablen)
7161 {
7162 *strtab = NULL;
7163 *strtablen = 0;
7164 *symtab = get_elf_symbols (filedata, symsec, nsyms);
7165
7166 if (*symtab == NULL)
7167 return false;
7168
7169 if (symsec->sh_link != 0)
7170 {
7171 Elf_Internal_Shdr *strsec;
7172
7173 if (symsec->sh_link >= filedata->file_header.e_shnum)
7174 {
7175 error (_("Bad sh_link in symbol table section\n"));
7176 free (*symtab);
7177 *symtab = NULL;
7178 *nsyms = 0;
7179 return false;
7180 }
7181
7182 strsec = filedata->section_headers + symsec->sh_link;
7183
7184 *strtab = (char *) get_data (NULL, filedata, strsec->sh_offset,
7185 1, strsec->sh_size, _("string table"));
7186 if (*strtab == NULL)
7187 {
7188 free (*symtab);
7189 *symtab = NULL;
7190 *nsyms = 0;
7191 return false;
7192 }
7193 *strtablen = strsec->sh_size;
7194 }
7195 return true;
7196 }
7197
7198 static const char *
7199 get_group_flags (unsigned int flags)
7200 {
7201 static char buff[128];
7202
7203 if (flags == 0)
7204 return "";
7205 else if (flags == GRP_COMDAT)
7206 return "COMDAT ";
7207
7208 snprintf (buff, sizeof buff, "[0x%x: %s%s%s]",
7209 flags,
7210 flags & GRP_MASKOS ? _("<OS specific>") : "",
7211 flags & GRP_MASKPROC ? _("<PROC specific>") : "",
7212 (flags & ~(GRP_COMDAT | GRP_MASKOS | GRP_MASKPROC)
7213 ? _("<unknown>") : ""));
7214
7215 return buff;
7216 }
7217
7218 static bool
7219 process_section_groups (Filedata * filedata)
7220 {
7221 Elf_Internal_Shdr * section;
7222 unsigned int i;
7223 struct group * group;
7224 Elf_Internal_Shdr * symtab_sec;
7225 Elf_Internal_Shdr * strtab_sec;
7226 Elf_Internal_Sym * symtab;
7227 unsigned long num_syms;
7228 char * strtab;
7229 size_t strtab_size;
7230
7231 /* Don't process section groups unless needed. */
7232 if (!do_unwind && !do_section_groups)
7233 return true;
7234
7235 if (filedata->file_header.e_shnum == 0)
7236 {
7237 if (do_section_groups)
7238 {
7239 if (filedata->is_separate)
7240 printf (_("\nThere are no sections group in linked file '%s'.\n"),
7241 filedata->file_name);
7242 else
7243 printf (_("\nThere are no section groups in this file.\n"));
7244 }
7245 return true;
7246 }
7247
7248 if (filedata->section_headers == NULL)
7249 {
7250 error (_("Section headers are not available!\n"));
7251 /* PR 13622: This can happen with a corrupt ELF header. */
7252 return false;
7253 }
7254
7255 filedata->section_headers_groups
7256 = (struct group **) calloc (filedata->file_header.e_shnum,
7257 sizeof (struct group *));
7258
7259 if (filedata->section_headers_groups == NULL)
7260 {
7261 error (_("Out of memory reading %u section group headers\n"),
7262 filedata->file_header.e_shnum);
7263 return false;
7264 }
7265
7266 /* Scan the sections for the group section. */
7267 filedata->group_count = 0;
7268 for (i = 0, section = filedata->section_headers;
7269 i < filedata->file_header.e_shnum;
7270 i++, section++)
7271 if (section->sh_type == SHT_GROUP)
7272 filedata->group_count++;
7273
7274 if (filedata->group_count == 0)
7275 {
7276 if (do_section_groups)
7277 {
7278 if (filedata->is_separate)
7279 printf (_("\nThere are no section groups in linked file '%s'.\n"),
7280 filedata->file_name);
7281 else
7282 printf (_("\nThere are no section groups in this file.\n"));
7283 }
7284
7285 return true;
7286 }
7287
7288 filedata->section_groups = (struct group *) calloc (filedata->group_count,
7289 sizeof (struct group));
7290
7291 if (filedata->section_groups == NULL)
7292 {
7293 error (_("Out of memory reading %lu groups\n"),
7294 (unsigned long) filedata->group_count);
7295 return false;
7296 }
7297
7298 symtab_sec = NULL;
7299 strtab_sec = NULL;
7300 symtab = NULL;
7301 num_syms = 0;
7302 strtab = NULL;
7303 strtab_size = 0;
7304
7305 if (filedata->is_separate)
7306 printf (_("Section groups in linked file '%s'\n"), filedata->file_name);
7307
7308 for (i = 0, section = filedata->section_headers, group = filedata->section_groups;
7309 i < filedata->file_header.e_shnum;
7310 i++, section++)
7311 {
7312 if (section->sh_type == SHT_GROUP)
7313 {
7314 const char * name = printable_section_name (filedata, section);
7315 const char * group_name;
7316 unsigned char * start;
7317 unsigned char * indices;
7318 unsigned int entry, j, size;
7319 Elf_Internal_Shdr * sec;
7320 Elf_Internal_Sym * sym;
7321
7322 /* Get the symbol table. */
7323 if (section->sh_link >= filedata->file_header.e_shnum
7324 || ((sec = filedata->section_headers + section->sh_link)->sh_type
7325 != SHT_SYMTAB))
7326 {
7327 error (_("Bad sh_link in group section `%s'\n"), name);
7328 continue;
7329 }
7330
7331 if (symtab_sec != sec)
7332 {
7333 symtab_sec = sec;
7334 free (symtab);
7335 symtab = get_elf_symbols (filedata, symtab_sec, & num_syms);
7336 }
7337
7338 if (symtab == NULL)
7339 {
7340 error (_("Corrupt header in group section `%s'\n"), name);
7341 continue;
7342 }
7343
7344 if (section->sh_info >= num_syms)
7345 {
7346 error (_("Bad sh_info in group section `%s'\n"), name);
7347 continue;
7348 }
7349
7350 sym = symtab + section->sh_info;
7351
7352 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
7353 {
7354 if (sym->st_shndx == 0
7355 || sym->st_shndx >= filedata->file_header.e_shnum)
7356 {
7357 error (_("Bad sh_info in group section `%s'\n"), name);
7358 continue;
7359 }
7360
7361 group_name = SECTION_NAME_PRINT (filedata->section_headers
7362 + sym->st_shndx);
7363 strtab_sec = NULL;
7364 free (strtab);
7365 strtab = NULL;
7366 strtab_size = 0;
7367 }
7368 else
7369 {
7370 /* Get the string table. */
7371 if (symtab_sec->sh_link >= filedata->file_header.e_shnum)
7372 {
7373 strtab_sec = NULL;
7374 free (strtab);
7375 strtab = NULL;
7376 strtab_size = 0;
7377 }
7378 else if (strtab_sec
7379 != (sec = filedata->section_headers + symtab_sec->sh_link))
7380 {
7381 strtab_sec = sec;
7382 free (strtab);
7383
7384 strtab = (char *) get_data (NULL, filedata, strtab_sec->sh_offset,
7385 1, strtab_sec->sh_size,
7386 _("string table"));
7387 strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
7388 }
7389 group_name = sym->st_name < strtab_size
7390 ? strtab + sym->st_name : _("<corrupt>");
7391 }
7392
7393 /* PR 17531: file: loop. */
7394 if (section->sh_entsize > section->sh_size)
7395 {
7396 error (_("Section %s has sh_entsize (0x%lx) which is larger than its size (0x%lx)\n"),
7397 printable_section_name (filedata, section),
7398 (unsigned long) section->sh_entsize,
7399 (unsigned long) section->sh_size);
7400 continue;
7401 }
7402
7403 start = (unsigned char *) get_data (NULL, filedata, section->sh_offset,
7404 1, section->sh_size,
7405 _("section data"));
7406 if (start == NULL)
7407 continue;
7408
7409 indices = start;
7410 size = (section->sh_size / section->sh_entsize) - 1;
7411 entry = byte_get (indices, 4);
7412 indices += 4;
7413
7414 if (do_section_groups)
7415 {
7416 printf (_("\n%sgroup section [%5u] `%s' [%s] contains %u sections:\n"),
7417 get_group_flags (entry), i, name, group_name, size);
7418
7419 printf (_(" [Index] Name\n"));
7420 }
7421
7422 group->group_index = i;
7423
7424 for (j = 0; j < size; j++)
7425 {
7426 struct group_list * g;
7427
7428 entry = byte_get (indices, 4);
7429 indices += 4;
7430
7431 if (entry >= filedata->file_header.e_shnum)
7432 {
7433 static unsigned num_group_errors = 0;
7434
7435 if (num_group_errors ++ < 10)
7436 {
7437 error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
7438 entry, i, filedata->file_header.e_shnum - 1);
7439 if (num_group_errors == 10)
7440 warn (_("Further error messages about overlarge group section indices suppressed\n"));
7441 }
7442 continue;
7443 }
7444
7445 if (filedata->section_headers_groups [entry] != NULL)
7446 {
7447 if (entry)
7448 {
7449 static unsigned num_errs = 0;
7450
7451 if (num_errs ++ < 10)
7452 {
7453 error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
7454 entry, i,
7455 filedata->section_headers_groups [entry]->group_index);
7456 if (num_errs == 10)
7457 warn (_("Further error messages about already contained group sections suppressed\n"));
7458 }
7459 continue;
7460 }
7461 else
7462 {
7463 /* Intel C/C++ compiler may put section 0 in a
7464 section group. We just warn it the first time
7465 and ignore it afterwards. */
7466 static bool warned = false;
7467 if (!warned)
7468 {
7469 error (_("section 0 in group section [%5u]\n"),
7470 filedata->section_headers_groups [entry]->group_index);
7471 warned = true;
7472 }
7473 }
7474 }
7475
7476 filedata->section_headers_groups [entry] = group;
7477
7478 if (do_section_groups)
7479 {
7480 sec = filedata->section_headers + entry;
7481 printf (" [%5u] %s\n", entry, printable_section_name (filedata, sec));
7482 }
7483
7484 g = (struct group_list *) xmalloc (sizeof (struct group_list));
7485 g->section_index = entry;
7486 g->next = group->root;
7487 group->root = g;
7488 }
7489
7490 free (start);
7491
7492 group++;
7493 }
7494 }
7495
7496 free (symtab);
7497 free (strtab);
7498 return true;
7499 }
7500
7501 /* Data used to display dynamic fixups. */
7502
7503 struct ia64_vms_dynfixup
7504 {
7505 bfd_vma needed_ident; /* Library ident number. */
7506 bfd_vma needed; /* Index in the dstrtab of the library name. */
7507 bfd_vma fixup_needed; /* Index of the library. */
7508 bfd_vma fixup_rela_cnt; /* Number of fixups. */
7509 bfd_vma fixup_rela_off; /* Fixups offset in the dynamic segment. */
7510 };
7511
7512 /* Data used to display dynamic relocations. */
7513
7514 struct ia64_vms_dynimgrela
7515 {
7516 bfd_vma img_rela_cnt; /* Number of relocations. */
7517 bfd_vma img_rela_off; /* Reloc offset in the dynamic segment. */
7518 };
7519
7520 /* Display IA-64 OpenVMS dynamic fixups (used to dynamically link a shared
7521 library). */
7522
7523 static bool
7524 dump_ia64_vms_dynamic_fixups (Filedata * filedata,
7525 struct ia64_vms_dynfixup * fixup,
7526 const char * strtab,
7527 unsigned int strtab_sz)
7528 {
7529 Elf64_External_VMS_IMAGE_FIXUP * imfs;
7530 long i;
7531 const char * lib_name;
7532
7533 imfs = get_data (NULL, filedata,
7534 filedata->dynamic_addr + fixup->fixup_rela_off,
7535 sizeof (*imfs), fixup->fixup_rela_cnt,
7536 _("dynamic section image fixups"));
7537 if (!imfs)
7538 return false;
7539
7540 if (fixup->needed < strtab_sz)
7541 lib_name = strtab + fixup->needed;
7542 else
7543 {
7544 warn (_("corrupt library name index of 0x%lx found in dynamic entry"),
7545 (unsigned long) fixup->needed);
7546 lib_name = "???";
7547 }
7548
7549 printf (_("\nImage fixups for needed library #%d: %s - ident: %lx\n"),
7550 (int) fixup->fixup_needed, lib_name, (long) fixup->needed_ident);
7551 printf
7552 (_("Seg Offset Type SymVec DataType\n"));
7553
7554 for (i = 0; i < (long) fixup->fixup_rela_cnt; i++)
7555 {
7556 unsigned int type;
7557 const char *rtype;
7558
7559 printf ("%3u ", (unsigned) BYTE_GET (imfs [i].fixup_seg));
7560 printf_vma ((bfd_vma) BYTE_GET (imfs [i].fixup_offset));
7561 type = BYTE_GET (imfs [i].type);
7562 rtype = elf_ia64_reloc_type (type);
7563 if (rtype == NULL)
7564 printf (" 0x%08x ", type);
7565 else
7566 printf (" %-32s ", rtype);
7567 printf ("%6u ", (unsigned) BYTE_GET (imfs [i].symvec_index));
7568 printf ("0x%08x\n", (unsigned) BYTE_GET (imfs [i].data_type));
7569 }
7570
7571 free (imfs);
7572 return true;
7573 }
7574
7575 /* Display IA-64 OpenVMS dynamic relocations (used to relocate an image). */
7576
7577 static bool
7578 dump_ia64_vms_dynamic_relocs (Filedata * filedata, struct ia64_vms_dynimgrela *imgrela)
7579 {
7580 Elf64_External_VMS_IMAGE_RELA *imrs;
7581 long i;
7582
7583 imrs = get_data (NULL, filedata,
7584 filedata->dynamic_addr + imgrela->img_rela_off,
7585 sizeof (*imrs), imgrela->img_rela_cnt,
7586 _("dynamic section image relocations"));
7587 if (!imrs)
7588 return false;
7589
7590 printf (_("\nImage relocs\n"));
7591 printf
7592 (_("Seg Offset Type Addend Seg Sym Off\n"));
7593
7594 for (i = 0; i < (long) imgrela->img_rela_cnt; i++)
7595 {
7596 unsigned int type;
7597 const char *rtype;
7598
7599 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].rela_seg));
7600 printf ("%08" BFD_VMA_FMT "x ",
7601 (bfd_vma) BYTE_GET (imrs [i].rela_offset));
7602 type = BYTE_GET (imrs [i].type);
7603 rtype = elf_ia64_reloc_type (type);
7604 if (rtype == NULL)
7605 printf ("0x%08x ", type);
7606 else
7607 printf ("%-31s ", rtype);
7608 print_vma (BYTE_GET (imrs [i].addend), FULL_HEX);
7609 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].sym_seg));
7610 printf ("%08" BFD_VMA_FMT "x\n",
7611 (bfd_vma) BYTE_GET (imrs [i].sym_offset));
7612 }
7613
7614 free (imrs);
7615 return true;
7616 }
7617
7618 /* Display IA-64 OpenVMS dynamic relocations and fixups. */
7619
7620 static bool
7621 process_ia64_vms_dynamic_relocs (Filedata * filedata)
7622 {
7623 struct ia64_vms_dynfixup fixup;
7624 struct ia64_vms_dynimgrela imgrela;
7625 Elf_Internal_Dyn *entry;
7626 bfd_vma strtab_off = 0;
7627 bfd_vma strtab_sz = 0;
7628 char *strtab = NULL;
7629 bool res = true;
7630
7631 memset (&fixup, 0, sizeof (fixup));
7632 memset (&imgrela, 0, sizeof (imgrela));
7633
7634 /* Note: the order of the entries is specified by the OpenVMS specs. */
7635 for (entry = filedata->dynamic_section;
7636 entry < filedata->dynamic_section + filedata->dynamic_nent;
7637 entry++)
7638 {
7639 switch (entry->d_tag)
7640 {
7641 case DT_IA_64_VMS_STRTAB_OFFSET:
7642 strtab_off = entry->d_un.d_val;
7643 break;
7644 case DT_STRSZ:
7645 strtab_sz = entry->d_un.d_val;
7646 if (strtab == NULL)
7647 strtab = get_data (NULL, filedata,
7648 filedata->dynamic_addr + strtab_off,
7649 1, strtab_sz, _("dynamic string section"));
7650 if (strtab == NULL)
7651 strtab_sz = 0;
7652 break;
7653
7654 case DT_IA_64_VMS_NEEDED_IDENT:
7655 fixup.needed_ident = entry->d_un.d_val;
7656 break;
7657 case DT_NEEDED:
7658 fixup.needed = entry->d_un.d_val;
7659 break;
7660 case DT_IA_64_VMS_FIXUP_NEEDED:
7661 fixup.fixup_needed = entry->d_un.d_val;
7662 break;
7663 case DT_IA_64_VMS_FIXUP_RELA_CNT:
7664 fixup.fixup_rela_cnt = entry->d_un.d_val;
7665 break;
7666 case DT_IA_64_VMS_FIXUP_RELA_OFF:
7667 fixup.fixup_rela_off = entry->d_un.d_val;
7668 if (! dump_ia64_vms_dynamic_fixups (filedata, &fixup, strtab, strtab_sz))
7669 res = false;
7670 break;
7671 case DT_IA_64_VMS_IMG_RELA_CNT:
7672 imgrela.img_rela_cnt = entry->d_un.d_val;
7673 break;
7674 case DT_IA_64_VMS_IMG_RELA_OFF:
7675 imgrela.img_rela_off = entry->d_un.d_val;
7676 if (! dump_ia64_vms_dynamic_relocs (filedata, &imgrela))
7677 res = false;
7678 break;
7679
7680 default:
7681 break;
7682 }
7683 }
7684
7685 free (strtab);
7686
7687 return res;
7688 }
7689
7690 static struct
7691 {
7692 const char * name;
7693 int reloc;
7694 int size;
7695 int rela;
7696 }
7697 dynamic_relocations [] =
7698 {
7699 { "REL", DT_REL, DT_RELSZ, false },
7700 { "RELA", DT_RELA, DT_RELASZ, true },
7701 { "PLT", DT_JMPREL, DT_PLTRELSZ, UNKNOWN }
7702 };
7703
7704 /* Process the reloc section. */
7705
7706 static bool
7707 process_relocs (Filedata * filedata)
7708 {
7709 unsigned long rel_size;
7710 unsigned long rel_offset;
7711
7712 if (!do_reloc)
7713 return true;
7714
7715 if (do_using_dynamic)
7716 {
7717 int is_rela;
7718 const char * name;
7719 bool has_dynamic_reloc;
7720 unsigned int i;
7721
7722 has_dynamic_reloc = false;
7723
7724 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
7725 {
7726 is_rela = dynamic_relocations [i].rela;
7727 name = dynamic_relocations [i].name;
7728 rel_size = filedata->dynamic_info[dynamic_relocations [i].size];
7729 rel_offset = filedata->dynamic_info[dynamic_relocations [i].reloc];
7730
7731 if (rel_size)
7732 has_dynamic_reloc = true;
7733
7734 if (is_rela == UNKNOWN)
7735 {
7736 if (dynamic_relocations [i].reloc == DT_JMPREL)
7737 switch (filedata->dynamic_info[DT_PLTREL])
7738 {
7739 case DT_REL:
7740 is_rela = false;
7741 break;
7742 case DT_RELA:
7743 is_rela = true;
7744 break;
7745 }
7746 }
7747
7748 if (rel_size)
7749 {
7750 if (filedata->is_separate)
7751 printf
7752 (_("\nIn linked file '%s' section '%s' at offset 0x%lx contains %ld bytes:\n"),
7753 filedata->file_name, name, rel_offset, rel_size);
7754 else
7755 printf
7756 (_("\n'%s' relocation section at offset 0x%lx contains %ld bytes:\n"),
7757 name, rel_offset, rel_size);
7758
7759 dump_relocations (filedata,
7760 offset_from_vma (filedata, rel_offset, rel_size),
7761 rel_size,
7762 filedata->dynamic_symbols,
7763 filedata->num_dynamic_syms,
7764 filedata->dynamic_strings,
7765 filedata->dynamic_strings_length,
7766 is_rela, true /* is_dynamic */);
7767 }
7768 }
7769
7770 if (is_ia64_vms (filedata))
7771 if (process_ia64_vms_dynamic_relocs (filedata))
7772 has_dynamic_reloc = true;
7773
7774 if (! has_dynamic_reloc)
7775 {
7776 if (filedata->is_separate)
7777 printf (_("\nThere are no dynamic relocations in linked file '%s'.\n"),
7778 filedata->file_name);
7779 else
7780 printf (_("\nThere are no dynamic relocations in this file.\n"));
7781 }
7782 }
7783 else
7784 {
7785 Elf_Internal_Shdr * section;
7786 unsigned long i;
7787 bool found = false;
7788
7789 for (i = 0, section = filedata->section_headers;
7790 i < filedata->file_header.e_shnum;
7791 i++, section++)
7792 {
7793 if ( section->sh_type != SHT_RELA
7794 && section->sh_type != SHT_REL)
7795 continue;
7796
7797 rel_offset = section->sh_offset;
7798 rel_size = section->sh_size;
7799
7800 if (rel_size)
7801 {
7802 int is_rela;
7803 unsigned long num_rela;
7804
7805 if (filedata->is_separate)
7806 printf (_("\nIn linked file '%s' relocation section "),
7807 filedata->file_name);
7808 else
7809 printf (_("\nRelocation section "));
7810
7811 if (filedata->string_table == NULL)
7812 printf ("%d", section->sh_name);
7813 else
7814 printf ("'%s'", printable_section_name (filedata, section));
7815
7816 num_rela = rel_size / section->sh_entsize;
7817 printf (ngettext (" at offset 0x%lx contains %lu entry:\n",
7818 " at offset 0x%lx contains %lu entries:\n",
7819 num_rela),
7820 rel_offset, num_rela);
7821
7822 is_rela = section->sh_type == SHT_RELA;
7823
7824 if (section->sh_link != 0
7825 && section->sh_link < filedata->file_header.e_shnum)
7826 {
7827 Elf_Internal_Shdr * symsec;
7828 Elf_Internal_Sym * symtab;
7829 unsigned long nsyms;
7830 unsigned long strtablen = 0;
7831 char * strtab = NULL;
7832
7833 symsec = filedata->section_headers + section->sh_link;
7834 if (symsec->sh_type != SHT_SYMTAB
7835 && symsec->sh_type != SHT_DYNSYM)
7836 continue;
7837
7838 if (!get_symtab (filedata, symsec,
7839 &symtab, &nsyms, &strtab, &strtablen))
7840 continue;
7841
7842 dump_relocations (filedata, rel_offset, rel_size,
7843 symtab, nsyms, strtab, strtablen,
7844 is_rela,
7845 symsec->sh_type == SHT_DYNSYM);
7846 free (strtab);
7847 free (symtab);
7848 }
7849 else
7850 dump_relocations (filedata, rel_offset, rel_size,
7851 NULL, 0, NULL, 0, is_rela,
7852 false /* is_dynamic */);
7853
7854 found = true;
7855 }
7856 }
7857
7858 if (! found)
7859 {
7860 /* Users sometimes forget the -D option, so try to be helpful. */
7861 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
7862 {
7863 if (filedata->dynamic_info[dynamic_relocations [i].size])
7864 {
7865 if (filedata->is_separate)
7866 printf (_("\nThere are no static relocations in linked file '%s'."),
7867 filedata->file_name);
7868 else
7869 printf (_("\nThere are no static relocations in this file."));
7870 printf (_("\nTo see the dynamic relocations add --use-dynamic to the command line.\n"));
7871
7872 break;
7873 }
7874 }
7875 if (i == ARRAY_SIZE (dynamic_relocations))
7876 {
7877 if (filedata->is_separate)
7878 printf (_("\nThere are no relocations in linked file '%s'.\n"),
7879 filedata->file_name);
7880 else
7881 printf (_("\nThere are no relocations in this file.\n"));
7882 }
7883 }
7884 }
7885
7886 return true;
7887 }
7888
7889 /* An absolute address consists of a section and an offset. If the
7890 section is NULL, the offset itself is the address, otherwise, the
7891 address equals to LOAD_ADDRESS(section) + offset. */
7892
7893 struct absaddr
7894 {
7895 unsigned short section;
7896 bfd_vma offset;
7897 };
7898
7899 /* Find the nearest symbol at or below ADDR. Returns the symbol
7900 name, if found, and the offset from the symbol to ADDR. */
7901
7902 static void
7903 find_symbol_for_address (Filedata * filedata,
7904 Elf_Internal_Sym * symtab,
7905 unsigned long nsyms,
7906 const char * strtab,
7907 unsigned long strtab_size,
7908 struct absaddr addr,
7909 const char ** symname,
7910 bfd_vma * offset)
7911 {
7912 bfd_vma dist = 0x100000;
7913 Elf_Internal_Sym * sym;
7914 Elf_Internal_Sym * beg;
7915 Elf_Internal_Sym * end;
7916 Elf_Internal_Sym * best = NULL;
7917
7918 REMOVE_ARCH_BITS (addr.offset);
7919 beg = symtab;
7920 end = symtab + nsyms;
7921
7922 while (beg < end)
7923 {
7924 bfd_vma value;
7925
7926 sym = beg + (end - beg) / 2;
7927
7928 value = sym->st_value;
7929 REMOVE_ARCH_BITS (value);
7930
7931 if (sym->st_name != 0
7932 && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
7933 && addr.offset >= value
7934 && addr.offset - value < dist)
7935 {
7936 best = sym;
7937 dist = addr.offset - value;
7938 if (!dist)
7939 break;
7940 }
7941
7942 if (addr.offset < value)
7943 end = sym;
7944 else
7945 beg = sym + 1;
7946 }
7947
7948 if (best)
7949 {
7950 *symname = (best->st_name >= strtab_size
7951 ? _("<corrupt>") : strtab + best->st_name);
7952 *offset = dist;
7953 return;
7954 }
7955
7956 *symname = NULL;
7957 *offset = addr.offset;
7958 }
7959
7960 static /* signed */ int
7961 symcmp (const void *p, const void *q)
7962 {
7963 Elf_Internal_Sym *sp = (Elf_Internal_Sym *) p;
7964 Elf_Internal_Sym *sq = (Elf_Internal_Sym *) q;
7965
7966 return sp->st_value > sq->st_value ? 1 : (sp->st_value < sq->st_value ? -1 : 0);
7967 }
7968
7969 /* Process the unwind section. */
7970
7971 #include "unwind-ia64.h"
7972
7973 struct ia64_unw_table_entry
7974 {
7975 struct absaddr start;
7976 struct absaddr end;
7977 struct absaddr info;
7978 };
7979
7980 struct ia64_unw_aux_info
7981 {
7982 struct ia64_unw_table_entry * table; /* Unwind table. */
7983 unsigned long table_len; /* Length of unwind table. */
7984 unsigned char * info; /* Unwind info. */
7985 unsigned long info_size; /* Size of unwind info. */
7986 bfd_vma info_addr; /* Starting address of unwind info. */
7987 bfd_vma seg_base; /* Starting address of segment. */
7988 Elf_Internal_Sym * symtab; /* The symbol table. */
7989 unsigned long nsyms; /* Number of symbols. */
7990 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
7991 unsigned long nfuns; /* Number of entries in funtab. */
7992 char * strtab; /* The string table. */
7993 unsigned long strtab_size; /* Size of string table. */
7994 };
7995
7996 static bool
7997 dump_ia64_unwind (Filedata * filedata, struct ia64_unw_aux_info * aux)
7998 {
7999 struct ia64_unw_table_entry * tp;
8000 unsigned long j, nfuns;
8001 int in_body;
8002 bool res = true;
8003
8004 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
8005 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
8006 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
8007 aux->funtab[nfuns++] = aux->symtab[j];
8008 aux->nfuns = nfuns;
8009 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
8010
8011 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
8012 {
8013 bfd_vma stamp;
8014 bfd_vma offset;
8015 const unsigned char * dp;
8016 const unsigned char * head;
8017 const unsigned char * end;
8018 const char * procname;
8019
8020 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
8021 aux->strtab_size, tp->start, &procname, &offset);
8022
8023 fputs ("\n<", stdout);
8024
8025 if (procname)
8026 {
8027 fputs (procname, stdout);
8028
8029 if (offset)
8030 printf ("+%lx", (unsigned long) offset);
8031 }
8032
8033 fputs (">: [", stdout);
8034 print_vma (tp->start.offset, PREFIX_HEX);
8035 fputc ('-', stdout);
8036 print_vma (tp->end.offset, PREFIX_HEX);
8037 printf ("], info at +0x%lx\n",
8038 (unsigned long) (tp->info.offset - aux->seg_base));
8039
8040 /* PR 17531: file: 86232b32. */
8041 if (aux->info == NULL)
8042 continue;
8043
8044 offset = tp->info.offset;
8045 if (tp->info.section)
8046 {
8047 if (tp->info.section >= filedata->file_header.e_shnum)
8048 {
8049 warn (_("Invalid section %u in table entry %ld\n"),
8050 tp->info.section, (long) (tp - aux->table));
8051 res = false;
8052 continue;
8053 }
8054 offset += filedata->section_headers[tp->info.section].sh_addr;
8055 }
8056 offset -= aux->info_addr;
8057 /* PR 17531: file: 0997b4d1. */
8058 if (offset >= aux->info_size
8059 || aux->info_size - offset < 8)
8060 {
8061 warn (_("Invalid offset %lx in table entry %ld\n"),
8062 (long) tp->info.offset, (long) (tp - aux->table));
8063 res = false;
8064 continue;
8065 }
8066
8067 head = aux->info + offset;
8068 stamp = byte_get ((unsigned char *) head, sizeof (stamp));
8069
8070 printf (" v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
8071 (unsigned) UNW_VER (stamp),
8072 (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
8073 UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
8074 UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
8075 (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
8076
8077 if (UNW_VER (stamp) != 1)
8078 {
8079 printf (_("\tUnknown version.\n"));
8080 continue;
8081 }
8082
8083 in_body = 0;
8084 end = head + 8 + eh_addr_size * UNW_LENGTH (stamp);
8085 /* PR 17531: file: 16ceda89. */
8086 if (end > aux->info + aux->info_size)
8087 end = aux->info + aux->info_size;
8088 for (dp = head + 8; dp < end;)
8089 dp = unw_decode (dp, in_body, & in_body, end);
8090 }
8091
8092 free (aux->funtab);
8093
8094 return res;
8095 }
8096
8097 static bool
8098 slurp_ia64_unwind_table (Filedata * filedata,
8099 struct ia64_unw_aux_info * aux,
8100 Elf_Internal_Shdr * sec)
8101 {
8102 unsigned long size, nrelas, i;
8103 Elf_Internal_Phdr * seg;
8104 struct ia64_unw_table_entry * tep;
8105 Elf_Internal_Shdr * relsec;
8106 Elf_Internal_Rela * rela;
8107 Elf_Internal_Rela * rp;
8108 unsigned char * table;
8109 unsigned char * tp;
8110 Elf_Internal_Sym * sym;
8111 const char * relname;
8112
8113 aux->table_len = 0;
8114
8115 /* First, find the starting address of the segment that includes
8116 this section: */
8117
8118 if (filedata->file_header.e_phnum)
8119 {
8120 if (! get_program_headers (filedata))
8121 return false;
8122
8123 for (seg = filedata->program_headers;
8124 seg < filedata->program_headers + filedata->file_header.e_phnum;
8125 ++seg)
8126 {
8127 if (seg->p_type != PT_LOAD)
8128 continue;
8129
8130 if (sec->sh_addr >= seg->p_vaddr
8131 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
8132 {
8133 aux->seg_base = seg->p_vaddr;
8134 break;
8135 }
8136 }
8137 }
8138
8139 /* Second, build the unwind table from the contents of the unwind section: */
8140 size = sec->sh_size;
8141 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
8142 _("unwind table"));
8143 if (!table)
8144 return false;
8145
8146 aux->table_len = size / (3 * eh_addr_size);
8147 aux->table = (struct ia64_unw_table_entry *)
8148 xcmalloc (aux->table_len, sizeof (aux->table[0]));
8149 tep = aux->table;
8150
8151 for (tp = table; tp <= table + size - (3 * eh_addr_size); ++tep)
8152 {
8153 tep->start.section = SHN_UNDEF;
8154 tep->end.section = SHN_UNDEF;
8155 tep->info.section = SHN_UNDEF;
8156 tep->start.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8157 tep->end.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8158 tep->info.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8159 tep->start.offset += aux->seg_base;
8160 tep->end.offset += aux->seg_base;
8161 tep->info.offset += aux->seg_base;
8162 }
8163 free (table);
8164
8165 /* Third, apply any relocations to the unwind table: */
8166 for (relsec = filedata->section_headers;
8167 relsec < filedata->section_headers + filedata->file_header.e_shnum;
8168 ++relsec)
8169 {
8170 if (relsec->sh_type != SHT_RELA
8171 || relsec->sh_info >= filedata->file_header.e_shnum
8172 || filedata->section_headers + relsec->sh_info != sec)
8173 continue;
8174
8175 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
8176 & rela, & nrelas))
8177 {
8178 free (aux->table);
8179 aux->table = NULL;
8180 aux->table_len = 0;
8181 return false;
8182 }
8183
8184 for (rp = rela; rp < rela + nrelas; ++rp)
8185 {
8186 unsigned int sym_ndx;
8187 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
8188 relname = elf_ia64_reloc_type (r_type);
8189
8190 /* PR 17531: file: 9fa67536. */
8191 if (relname == NULL)
8192 {
8193 warn (_("Skipping unknown relocation type: %u\n"), r_type);
8194 continue;
8195 }
8196
8197 if (! startswith (relname, "R_IA64_SEGREL"))
8198 {
8199 warn (_("Skipping unexpected relocation type: %s\n"), relname);
8200 continue;
8201 }
8202
8203 i = rp->r_offset / (3 * eh_addr_size);
8204
8205 /* PR 17531: file: 5bc8d9bf. */
8206 if (i >= aux->table_len)
8207 {
8208 warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
8209 continue;
8210 }
8211
8212 sym_ndx = get_reloc_symindex (rp->r_info);
8213 if (sym_ndx >= aux->nsyms)
8214 {
8215 warn (_("Skipping reloc with invalid symbol index: %u\n"),
8216 sym_ndx);
8217 continue;
8218 }
8219 sym = aux->symtab + sym_ndx;
8220
8221 switch (rp->r_offset / eh_addr_size % 3)
8222 {
8223 case 0:
8224 aux->table[i].start.section = sym->st_shndx;
8225 aux->table[i].start.offset = rp->r_addend + sym->st_value;
8226 break;
8227 case 1:
8228 aux->table[i].end.section = sym->st_shndx;
8229 aux->table[i].end.offset = rp->r_addend + sym->st_value;
8230 break;
8231 case 2:
8232 aux->table[i].info.section = sym->st_shndx;
8233 aux->table[i].info.offset = rp->r_addend + sym->st_value;
8234 break;
8235 default:
8236 break;
8237 }
8238 }
8239
8240 free (rela);
8241 }
8242
8243 return true;
8244 }
8245
8246 static bool
8247 ia64_process_unwind (Filedata * filedata)
8248 {
8249 Elf_Internal_Shdr * sec;
8250 Elf_Internal_Shdr * unwsec = NULL;
8251 unsigned long i, unwcount = 0, unwstart = 0;
8252 struct ia64_unw_aux_info aux;
8253 bool res = true;
8254
8255 memset (& aux, 0, sizeof (aux));
8256
8257 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
8258 {
8259 if (sec->sh_type == SHT_SYMTAB)
8260 {
8261 if (aux.symtab)
8262 {
8263 error (_("Multiple symbol tables encountered\n"));
8264 free (aux.symtab);
8265 aux.symtab = NULL;
8266 free (aux.strtab);
8267 aux.strtab = NULL;
8268 }
8269 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
8270 &aux.strtab, &aux.strtab_size))
8271 return false;
8272 }
8273 else if (sec->sh_type == SHT_IA_64_UNWIND)
8274 unwcount++;
8275 }
8276
8277 if (!unwcount)
8278 printf (_("\nThere are no unwind sections in this file.\n"));
8279
8280 while (unwcount-- > 0)
8281 {
8282 char * suffix;
8283 size_t len, len2;
8284
8285 for (i = unwstart, sec = filedata->section_headers + unwstart, unwsec = NULL;
8286 i < filedata->file_header.e_shnum; ++i, ++sec)
8287 if (sec->sh_type == SHT_IA_64_UNWIND)
8288 {
8289 unwsec = sec;
8290 break;
8291 }
8292 /* We have already counted the number of SHT_IA64_UNWIND
8293 sections so the loop above should never fail. */
8294 assert (unwsec != NULL);
8295
8296 unwstart = i + 1;
8297 len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
8298
8299 if ((unwsec->sh_flags & SHF_GROUP) != 0)
8300 {
8301 /* We need to find which section group it is in. */
8302 struct group_list * g;
8303
8304 if (filedata->section_headers_groups == NULL
8305 || filedata->section_headers_groups[i] == NULL)
8306 i = filedata->file_header.e_shnum;
8307 else
8308 {
8309 g = filedata->section_headers_groups[i]->root;
8310
8311 for (; g != NULL; g = g->next)
8312 {
8313 sec = filedata->section_headers + g->section_index;
8314
8315 if (SECTION_NAME_VALID (sec)
8316 && streq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info))
8317 break;
8318 }
8319
8320 if (g == NULL)
8321 i = filedata->file_header.e_shnum;
8322 }
8323 }
8324 else if (SECTION_NAME_VALID (unwsec)
8325 && startswith (SECTION_NAME (unwsec),
8326 ELF_STRING_ia64_unwind_once))
8327 {
8328 /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO. */
8329 len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
8330 suffix = SECTION_NAME (unwsec) + len;
8331 for (i = 0, sec = filedata->section_headers;
8332 i < filedata->file_header.e_shnum;
8333 ++i, ++sec)
8334 if (SECTION_NAME_VALID (sec)
8335 && startswith (SECTION_NAME (sec),
8336 ELF_STRING_ia64_unwind_info_once)
8337 && streq (SECTION_NAME (sec) + len2, suffix))
8338 break;
8339 }
8340 else
8341 {
8342 /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
8343 .IA_64.unwind or BAR -> .IA_64.unwind_info. */
8344 len = sizeof (ELF_STRING_ia64_unwind) - 1;
8345 len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
8346 suffix = "";
8347 if (SECTION_NAME_VALID (unwsec)
8348 && startswith (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind))
8349 suffix = SECTION_NAME (unwsec) + len;
8350 for (i = 0, sec = filedata->section_headers;
8351 i < filedata->file_header.e_shnum;
8352 ++i, ++sec)
8353 if (SECTION_NAME_VALID (sec)
8354 && startswith (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info)
8355 && streq (SECTION_NAME (sec) + len2, suffix))
8356 break;
8357 }
8358
8359 if (i == filedata->file_header.e_shnum)
8360 {
8361 printf (_("\nCould not find unwind info section for "));
8362
8363 if (filedata->string_table == NULL)
8364 printf ("%d", unwsec->sh_name);
8365 else
8366 printf ("'%s'", printable_section_name (filedata, unwsec));
8367 }
8368 else
8369 {
8370 aux.info_addr = sec->sh_addr;
8371 aux.info = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1,
8372 sec->sh_size,
8373 _("unwind info"));
8374 aux.info_size = aux.info == NULL ? 0 : sec->sh_size;
8375
8376 printf (_("\nUnwind section "));
8377
8378 if (filedata->string_table == NULL)
8379 printf ("%d", unwsec->sh_name);
8380 else
8381 printf ("'%s'", printable_section_name (filedata, unwsec));
8382
8383 printf (_(" at offset 0x%lx contains %lu entries:\n"),
8384 (unsigned long) unwsec->sh_offset,
8385 (unsigned long) (unwsec->sh_size / (3 * eh_addr_size)));
8386
8387 if (slurp_ia64_unwind_table (filedata, & aux, unwsec)
8388 && aux.table_len > 0)
8389 dump_ia64_unwind (filedata, & aux);
8390
8391 free ((char *) aux.table);
8392 free ((char *) aux.info);
8393 aux.table = NULL;
8394 aux.info = NULL;
8395 }
8396 }
8397
8398 free (aux.symtab);
8399 free ((char *) aux.strtab);
8400
8401 return res;
8402 }
8403
8404 struct hppa_unw_table_entry
8405 {
8406 struct absaddr start;
8407 struct absaddr end;
8408 unsigned int Cannot_unwind:1; /* 0 */
8409 unsigned int Millicode:1; /* 1 */
8410 unsigned int Millicode_save_sr0:1; /* 2 */
8411 unsigned int Region_description:2; /* 3..4 */
8412 unsigned int reserved1:1; /* 5 */
8413 unsigned int Entry_SR:1; /* 6 */
8414 unsigned int Entry_FR:4; /* Number saved 7..10 */
8415 unsigned int Entry_GR:5; /* Number saved 11..15 */
8416 unsigned int Args_stored:1; /* 16 */
8417 unsigned int Variable_Frame:1; /* 17 */
8418 unsigned int Separate_Package_Body:1; /* 18 */
8419 unsigned int Frame_Extension_Millicode:1; /* 19 */
8420 unsigned int Stack_Overflow_Check:1; /* 20 */
8421 unsigned int Two_Instruction_SP_Increment:1; /* 21 */
8422 unsigned int Ada_Region:1; /* 22 */
8423 unsigned int cxx_info:1; /* 23 */
8424 unsigned int cxx_try_catch:1; /* 24 */
8425 unsigned int sched_entry_seq:1; /* 25 */
8426 unsigned int reserved2:1; /* 26 */
8427 unsigned int Save_SP:1; /* 27 */
8428 unsigned int Save_RP:1; /* 28 */
8429 unsigned int Save_MRP_in_frame:1; /* 29 */
8430 unsigned int extn_ptr_defined:1; /* 30 */
8431 unsigned int Cleanup_defined:1; /* 31 */
8432
8433 unsigned int MPE_XL_interrupt_marker:1; /* 0 */
8434 unsigned int HP_UX_interrupt_marker:1; /* 1 */
8435 unsigned int Large_frame:1; /* 2 */
8436 unsigned int Pseudo_SP_Set:1; /* 3 */
8437 unsigned int reserved4:1; /* 4 */
8438 unsigned int Total_frame_size:27; /* 5..31 */
8439 };
8440
8441 struct hppa_unw_aux_info
8442 {
8443 struct hppa_unw_table_entry * table; /* Unwind table. */
8444 unsigned long table_len; /* Length of unwind table. */
8445 bfd_vma seg_base; /* Starting address of segment. */
8446 Elf_Internal_Sym * symtab; /* The symbol table. */
8447 unsigned long nsyms; /* Number of symbols. */
8448 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
8449 unsigned long nfuns; /* Number of entries in funtab. */
8450 char * strtab; /* The string table. */
8451 unsigned long strtab_size; /* Size of string table. */
8452 };
8453
8454 static bool
8455 dump_hppa_unwind (Filedata * filedata, struct hppa_unw_aux_info * aux)
8456 {
8457 struct hppa_unw_table_entry * tp;
8458 unsigned long j, nfuns;
8459 bool res = true;
8460
8461 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
8462 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
8463 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
8464 aux->funtab[nfuns++] = aux->symtab[j];
8465 aux->nfuns = nfuns;
8466 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
8467
8468 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
8469 {
8470 bfd_vma offset;
8471 const char * procname;
8472
8473 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
8474 aux->strtab_size, tp->start, &procname,
8475 &offset);
8476
8477 fputs ("\n<", stdout);
8478
8479 if (procname)
8480 {
8481 fputs (procname, stdout);
8482
8483 if (offset)
8484 printf ("+%lx", (unsigned long) offset);
8485 }
8486
8487 fputs (">: [", stdout);
8488 print_vma (tp->start.offset, PREFIX_HEX);
8489 fputc ('-', stdout);
8490 print_vma (tp->end.offset, PREFIX_HEX);
8491 printf ("]\n\t");
8492
8493 #define PF(_m) if (tp->_m) printf (#_m " ");
8494 #define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
8495 PF(Cannot_unwind);
8496 PF(Millicode);
8497 PF(Millicode_save_sr0);
8498 /* PV(Region_description); */
8499 PF(Entry_SR);
8500 PV(Entry_FR);
8501 PV(Entry_GR);
8502 PF(Args_stored);
8503 PF(Variable_Frame);
8504 PF(Separate_Package_Body);
8505 PF(Frame_Extension_Millicode);
8506 PF(Stack_Overflow_Check);
8507 PF(Two_Instruction_SP_Increment);
8508 PF(Ada_Region);
8509 PF(cxx_info);
8510 PF(cxx_try_catch);
8511 PF(sched_entry_seq);
8512 PF(Save_SP);
8513 PF(Save_RP);
8514 PF(Save_MRP_in_frame);
8515 PF(extn_ptr_defined);
8516 PF(Cleanup_defined);
8517 PF(MPE_XL_interrupt_marker);
8518 PF(HP_UX_interrupt_marker);
8519 PF(Large_frame);
8520 PF(Pseudo_SP_Set);
8521 PV(Total_frame_size);
8522 #undef PF
8523 #undef PV
8524 }
8525
8526 printf ("\n");
8527
8528 free (aux->funtab);
8529
8530 return res;
8531 }
8532
8533 static bool
8534 slurp_hppa_unwind_table (Filedata * filedata,
8535 struct hppa_unw_aux_info * aux,
8536 Elf_Internal_Shdr * sec)
8537 {
8538 unsigned long size, unw_ent_size, nentries, nrelas, i;
8539 Elf_Internal_Phdr * seg;
8540 struct hppa_unw_table_entry * tep;
8541 Elf_Internal_Shdr * relsec;
8542 Elf_Internal_Rela * rela;
8543 Elf_Internal_Rela * rp;
8544 unsigned char * table;
8545 unsigned char * tp;
8546 Elf_Internal_Sym * sym;
8547 const char * relname;
8548
8549 /* First, find the starting address of the segment that includes
8550 this section. */
8551 if (filedata->file_header.e_phnum)
8552 {
8553 if (! get_program_headers (filedata))
8554 return false;
8555
8556 for (seg = filedata->program_headers;
8557 seg < filedata->program_headers + filedata->file_header.e_phnum;
8558 ++seg)
8559 {
8560 if (seg->p_type != PT_LOAD)
8561 continue;
8562
8563 if (sec->sh_addr >= seg->p_vaddr
8564 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
8565 {
8566 aux->seg_base = seg->p_vaddr;
8567 break;
8568 }
8569 }
8570 }
8571
8572 /* Second, build the unwind table from the contents of the unwind
8573 section. */
8574 size = sec->sh_size;
8575 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
8576 _("unwind table"));
8577 if (!table)
8578 return false;
8579
8580 unw_ent_size = 16;
8581 nentries = size / unw_ent_size;
8582 size = unw_ent_size * nentries;
8583
8584 aux->table_len = nentries;
8585 tep = aux->table = (struct hppa_unw_table_entry *)
8586 xcmalloc (nentries, sizeof (aux->table[0]));
8587
8588 for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
8589 {
8590 unsigned int tmp1, tmp2;
8591
8592 tep->start.section = SHN_UNDEF;
8593 tep->end.section = SHN_UNDEF;
8594
8595 tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
8596 tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
8597 tmp1 = byte_get ((unsigned char *) tp + 8, 4);
8598 tmp2 = byte_get ((unsigned char *) tp + 12, 4);
8599
8600 tep->start.offset += aux->seg_base;
8601 tep->end.offset += aux->seg_base;
8602
8603 tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
8604 tep->Millicode = (tmp1 >> 30) & 0x1;
8605 tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
8606 tep->Region_description = (tmp1 >> 27) & 0x3;
8607 tep->reserved1 = (tmp1 >> 26) & 0x1;
8608 tep->Entry_SR = (tmp1 >> 25) & 0x1;
8609 tep->Entry_FR = (tmp1 >> 21) & 0xf;
8610 tep->Entry_GR = (tmp1 >> 16) & 0x1f;
8611 tep->Args_stored = (tmp1 >> 15) & 0x1;
8612 tep->Variable_Frame = (tmp1 >> 14) & 0x1;
8613 tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
8614 tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
8615 tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
8616 tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
8617 tep->Ada_Region = (tmp1 >> 9) & 0x1;
8618 tep->cxx_info = (tmp1 >> 8) & 0x1;
8619 tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
8620 tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
8621 tep->reserved2 = (tmp1 >> 5) & 0x1;
8622 tep->Save_SP = (tmp1 >> 4) & 0x1;
8623 tep->Save_RP = (tmp1 >> 3) & 0x1;
8624 tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
8625 tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
8626 tep->Cleanup_defined = tmp1 & 0x1;
8627
8628 tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
8629 tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
8630 tep->Large_frame = (tmp2 >> 29) & 0x1;
8631 tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
8632 tep->reserved4 = (tmp2 >> 27) & 0x1;
8633 tep->Total_frame_size = tmp2 & 0x7ffffff;
8634 }
8635 free (table);
8636
8637 /* Third, apply any relocations to the unwind table. */
8638 for (relsec = filedata->section_headers;
8639 relsec < filedata->section_headers + filedata->file_header.e_shnum;
8640 ++relsec)
8641 {
8642 if (relsec->sh_type != SHT_RELA
8643 || relsec->sh_info >= filedata->file_header.e_shnum
8644 || filedata->section_headers + relsec->sh_info != sec)
8645 continue;
8646
8647 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
8648 & rela, & nrelas))
8649 return false;
8650
8651 for (rp = rela; rp < rela + nrelas; ++rp)
8652 {
8653 unsigned int sym_ndx;
8654 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
8655 relname = elf_hppa_reloc_type (r_type);
8656
8657 if (relname == NULL)
8658 {
8659 warn (_("Skipping unknown relocation type: %u\n"), r_type);
8660 continue;
8661 }
8662
8663 /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64. */
8664 if (! startswith (relname, "R_PARISC_SEGREL"))
8665 {
8666 warn (_("Skipping unexpected relocation type: %s\n"), relname);
8667 continue;
8668 }
8669
8670 i = rp->r_offset / unw_ent_size;
8671 if (i >= aux->table_len)
8672 {
8673 warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
8674 continue;
8675 }
8676
8677 sym_ndx = get_reloc_symindex (rp->r_info);
8678 if (sym_ndx >= aux->nsyms)
8679 {
8680 warn (_("Skipping reloc with invalid symbol index: %u\n"),
8681 sym_ndx);
8682 continue;
8683 }
8684 sym = aux->symtab + sym_ndx;
8685
8686 switch ((rp->r_offset % unw_ent_size) / 4)
8687 {
8688 case 0:
8689 aux->table[i].start.section = sym->st_shndx;
8690 aux->table[i].start.offset = sym->st_value + rp->r_addend;
8691 break;
8692 case 1:
8693 aux->table[i].end.section = sym->st_shndx;
8694 aux->table[i].end.offset = sym->st_value + rp->r_addend;
8695 break;
8696 default:
8697 break;
8698 }
8699 }
8700
8701 free (rela);
8702 }
8703
8704 return true;
8705 }
8706
8707 static bool
8708 hppa_process_unwind (Filedata * filedata)
8709 {
8710 struct hppa_unw_aux_info aux;
8711 Elf_Internal_Shdr * unwsec = NULL;
8712 Elf_Internal_Shdr * sec;
8713 unsigned long i;
8714 bool res = true;
8715
8716 if (filedata->string_table == NULL)
8717 return false;
8718
8719 memset (& aux, 0, sizeof (aux));
8720
8721 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
8722 {
8723 if (sec->sh_type == SHT_SYMTAB)
8724 {
8725 if (aux.symtab)
8726 {
8727 error (_("Multiple symbol tables encountered\n"));
8728 free (aux.symtab);
8729 aux.symtab = NULL;
8730 free (aux.strtab);
8731 aux.strtab = NULL;
8732 }
8733 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
8734 &aux.strtab, &aux.strtab_size))
8735 return false;
8736 }
8737 else if (SECTION_NAME_VALID (sec)
8738 && streq (SECTION_NAME (sec), ".PARISC.unwind"))
8739 unwsec = sec;
8740 }
8741
8742 if (!unwsec)
8743 printf (_("\nThere are no unwind sections in this file.\n"));
8744
8745 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
8746 {
8747 if (SECTION_NAME_VALID (sec)
8748 && streq (SECTION_NAME (sec), ".PARISC.unwind"))
8749 {
8750 unsigned long num_unwind = sec->sh_size / 16;
8751
8752 printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
8753 "contains %lu entry:\n",
8754 "\nUnwind section '%s' at offset 0x%lx "
8755 "contains %lu entries:\n",
8756 num_unwind),
8757 printable_section_name (filedata, sec),
8758 (unsigned long) sec->sh_offset,
8759 num_unwind);
8760
8761 if (! slurp_hppa_unwind_table (filedata, &aux, sec))
8762 res = false;
8763
8764 if (res && aux.table_len > 0)
8765 {
8766 if (! dump_hppa_unwind (filedata, &aux))
8767 res = false;
8768 }
8769
8770 free ((char *) aux.table);
8771 aux.table = NULL;
8772 }
8773 }
8774
8775 free (aux.symtab);
8776 free ((char *) aux.strtab);
8777
8778 return res;
8779 }
8780
8781 struct arm_section
8782 {
8783 unsigned char * data; /* The unwind data. */
8784 Elf_Internal_Shdr * sec; /* The cached unwind section header. */
8785 Elf_Internal_Rela * rela; /* The cached relocations for this section. */
8786 unsigned long nrelas; /* The number of relocations. */
8787 unsigned int rel_type; /* REL or RELA ? */
8788 Elf_Internal_Rela * next_rela; /* Cyclic pointer to the next reloc to process. */
8789 };
8790
8791 struct arm_unw_aux_info
8792 {
8793 Filedata * filedata; /* The file containing the unwind sections. */
8794 Elf_Internal_Sym * symtab; /* The file's symbol table. */
8795 unsigned long nsyms; /* Number of symbols. */
8796 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
8797 unsigned long nfuns; /* Number of these symbols. */
8798 char * strtab; /* The file's string table. */
8799 unsigned long strtab_size; /* Size of string table. */
8800 };
8801
8802 static const char *
8803 arm_print_vma_and_name (Filedata * filedata,
8804 struct arm_unw_aux_info * aux,
8805 bfd_vma fn,
8806 struct absaddr addr)
8807 {
8808 const char *procname;
8809 bfd_vma sym_offset;
8810
8811 if (addr.section == SHN_UNDEF)
8812 addr.offset = fn;
8813
8814 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
8815 aux->strtab_size, addr, &procname,
8816 &sym_offset);
8817
8818 print_vma (fn, PREFIX_HEX);
8819
8820 if (procname)
8821 {
8822 fputs (" <", stdout);
8823 fputs (procname, stdout);
8824
8825 if (sym_offset)
8826 printf ("+0x%lx", (unsigned long) sym_offset);
8827 fputc ('>', stdout);
8828 }
8829
8830 return procname;
8831 }
8832
8833 static void
8834 arm_free_section (struct arm_section *arm_sec)
8835 {
8836 free (arm_sec->data);
8837 free (arm_sec->rela);
8838 }
8839
8840 /* 1) If SEC does not match the one cached in ARM_SEC, then free the current
8841 cached section and install SEC instead.
8842 2) Locate the 32-bit word at WORD_OFFSET in unwind section SEC
8843 and return its valued in * WORDP, relocating if necessary.
8844 3) Update the NEXT_RELA field in ARM_SEC and store the section index and
8845 relocation's offset in ADDR.
8846 4) If SYM_NAME is non-NULL and a relocation was applied, record the offset
8847 into the string table of the symbol associated with the reloc. If no
8848 reloc was applied store -1 there.
8849 5) Return TRUE upon success, FALSE otherwise. */
8850
8851 static bool
8852 get_unwind_section_word (Filedata * filedata,
8853 struct arm_unw_aux_info * aux,
8854 struct arm_section * arm_sec,
8855 Elf_Internal_Shdr * sec,
8856 bfd_vma word_offset,
8857 unsigned int * wordp,
8858 struct absaddr * addr,
8859 bfd_vma * sym_name)
8860 {
8861 Elf_Internal_Rela *rp;
8862 Elf_Internal_Sym *sym;
8863 const char * relname;
8864 unsigned int word;
8865 bool wrapped;
8866
8867 if (sec == NULL || arm_sec == NULL)
8868 return false;
8869
8870 addr->section = SHN_UNDEF;
8871 addr->offset = 0;
8872
8873 if (sym_name != NULL)
8874 *sym_name = (bfd_vma) -1;
8875
8876 /* If necessary, update the section cache. */
8877 if (sec != arm_sec->sec)
8878 {
8879 Elf_Internal_Shdr *relsec;
8880
8881 arm_free_section (arm_sec);
8882
8883 arm_sec->sec = sec;
8884 arm_sec->data = get_data (NULL, aux->filedata, sec->sh_offset, 1,
8885 sec->sh_size, _("unwind data"));
8886 arm_sec->rela = NULL;
8887 arm_sec->nrelas = 0;
8888
8889 for (relsec = filedata->section_headers;
8890 relsec < filedata->section_headers + filedata->file_header.e_shnum;
8891 ++relsec)
8892 {
8893 if (relsec->sh_info >= filedata->file_header.e_shnum
8894 || filedata->section_headers + relsec->sh_info != sec
8895 /* PR 15745: Check the section type as well. */
8896 || (relsec->sh_type != SHT_REL
8897 && relsec->sh_type != SHT_RELA))
8898 continue;
8899
8900 arm_sec->rel_type = relsec->sh_type;
8901 if (relsec->sh_type == SHT_REL)
8902 {
8903 if (!slurp_rel_relocs (aux->filedata, relsec->sh_offset,
8904 relsec->sh_size,
8905 & arm_sec->rela, & arm_sec->nrelas))
8906 return false;
8907 }
8908 else /* relsec->sh_type == SHT_RELA */
8909 {
8910 if (!slurp_rela_relocs (aux->filedata, relsec->sh_offset,
8911 relsec->sh_size,
8912 & arm_sec->rela, & arm_sec->nrelas))
8913 return false;
8914 }
8915 break;
8916 }
8917
8918 arm_sec->next_rela = arm_sec->rela;
8919 }
8920
8921 /* If there is no unwind data we can do nothing. */
8922 if (arm_sec->data == NULL)
8923 return false;
8924
8925 /* If the offset is invalid then fail. */
8926 if (/* PR 21343 *//* PR 18879 */
8927 sec->sh_size < 4
8928 || word_offset > (sec->sh_size - 4)
8929 || ((bfd_signed_vma) word_offset) < 0)
8930 return false;
8931
8932 /* Get the word at the required offset. */
8933 word = byte_get (arm_sec->data + word_offset, 4);
8934
8935 /* PR 17531: file: id:000001,src:001266+003044,op:splice,rep:128. */
8936 if (arm_sec->rela == NULL)
8937 {
8938 * wordp = word;
8939 return true;
8940 }
8941
8942 /* Look through the relocs to find the one that applies to the provided offset. */
8943 wrapped = false;
8944 for (rp = arm_sec->next_rela; rp != arm_sec->rela + arm_sec->nrelas; rp++)
8945 {
8946 bfd_vma prelval, offset;
8947
8948 if (rp->r_offset > word_offset && !wrapped)
8949 {
8950 rp = arm_sec->rela;
8951 wrapped = true;
8952 }
8953 if (rp->r_offset > word_offset)
8954 break;
8955
8956 if (rp->r_offset & 3)
8957 {
8958 warn (_("Skipping unexpected relocation at offset 0x%lx\n"),
8959 (unsigned long) rp->r_offset);
8960 continue;
8961 }
8962
8963 if (rp->r_offset < word_offset)
8964 continue;
8965
8966 /* PR 17531: file: 027-161405-0.004 */
8967 if (aux->symtab == NULL)
8968 continue;
8969
8970 if (arm_sec->rel_type == SHT_REL)
8971 {
8972 offset = word & 0x7fffffff;
8973 if (offset & 0x40000000)
8974 offset |= ~ (bfd_vma) 0x7fffffff;
8975 }
8976 else if (arm_sec->rel_type == SHT_RELA)
8977 offset = rp->r_addend;
8978 else
8979 {
8980 error (_("Unknown section relocation type %d encountered\n"),
8981 arm_sec->rel_type);
8982 break;
8983 }
8984
8985 /* PR 17531 file: 027-1241568-0.004. */
8986 if (ELF32_R_SYM (rp->r_info) >= aux->nsyms)
8987 {
8988 error (_("Bad symbol index in unwind relocation (%lu > %lu)\n"),
8989 (unsigned long) ELF32_R_SYM (rp->r_info), aux->nsyms);
8990 break;
8991 }
8992
8993 sym = aux->symtab + ELF32_R_SYM (rp->r_info);
8994 offset += sym->st_value;
8995 prelval = offset - (arm_sec->sec->sh_addr + rp->r_offset);
8996
8997 /* Check that we are processing the expected reloc type. */
8998 if (filedata->file_header.e_machine == EM_ARM)
8999 {
9000 relname = elf_arm_reloc_type (ELF32_R_TYPE (rp->r_info));
9001 if (relname == NULL)
9002 {
9003 warn (_("Skipping unknown ARM relocation type: %d\n"),
9004 (int) ELF32_R_TYPE (rp->r_info));
9005 continue;
9006 }
9007
9008 if (streq (relname, "R_ARM_NONE"))
9009 continue;
9010
9011 if (! streq (relname, "R_ARM_PREL31"))
9012 {
9013 warn (_("Skipping unexpected ARM relocation type %s\n"), relname);
9014 continue;
9015 }
9016 }
9017 else if (filedata->file_header.e_machine == EM_TI_C6000)
9018 {
9019 relname = elf_tic6x_reloc_type (ELF32_R_TYPE (rp->r_info));
9020 if (relname == NULL)
9021 {
9022 warn (_("Skipping unknown C6000 relocation type: %d\n"),
9023 (int) ELF32_R_TYPE (rp->r_info));
9024 continue;
9025 }
9026
9027 if (streq (relname, "R_C6000_NONE"))
9028 continue;
9029
9030 if (! streq (relname, "R_C6000_PREL31"))
9031 {
9032 warn (_("Skipping unexpected C6000 relocation type %s\n"), relname);
9033 continue;
9034 }
9035
9036 prelval >>= 1;
9037 }
9038 else
9039 {
9040 /* This function currently only supports ARM and TI unwinders. */
9041 warn (_("Only TI and ARM unwinders are currently supported\n"));
9042 break;
9043 }
9044
9045 word = (word & ~ (bfd_vma) 0x7fffffff) | (prelval & 0x7fffffff);
9046 addr->section = sym->st_shndx;
9047 addr->offset = offset;
9048
9049 if (sym_name)
9050 * sym_name = sym->st_name;
9051 break;
9052 }
9053
9054 *wordp = word;
9055 arm_sec->next_rela = rp;
9056
9057 return true;
9058 }
9059
9060 static const char *tic6x_unwind_regnames[16] =
9061 {
9062 "A15", "B15", "B14", "B13", "B12", "B11", "B10", "B3",
9063 "A14", "A13", "A12", "A11", "A10",
9064 "[invalid reg 13]", "[invalid reg 14]", "[invalid reg 15]"
9065 };
9066
9067 static void
9068 decode_tic6x_unwind_regmask (unsigned int mask)
9069 {
9070 int i;
9071
9072 for (i = 12; mask; mask >>= 1, i--)
9073 {
9074 if (mask & 1)
9075 {
9076 fputs (tic6x_unwind_regnames[i], stdout);
9077 if (mask > 1)
9078 fputs (", ", stdout);
9079 }
9080 }
9081 }
9082
9083 #define ADVANCE \
9084 if (remaining == 0 && more_words) \
9085 { \
9086 data_offset += 4; \
9087 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, \
9088 data_offset, & word, & addr, NULL)) \
9089 return false; \
9090 remaining = 4; \
9091 more_words--; \
9092 } \
9093
9094 #define GET_OP(OP) \
9095 ADVANCE; \
9096 if (remaining) \
9097 { \
9098 remaining--; \
9099 (OP) = word >> 24; \
9100 word <<= 8; \
9101 } \
9102 else \
9103 { \
9104 printf (_("[Truncated opcode]\n")); \
9105 return false; \
9106 } \
9107 printf ("0x%02x ", OP)
9108
9109 static bool
9110 decode_arm_unwind_bytecode (Filedata * filedata,
9111 struct arm_unw_aux_info * aux,
9112 unsigned int word,
9113 unsigned int remaining,
9114 unsigned int more_words,
9115 bfd_vma data_offset,
9116 Elf_Internal_Shdr * data_sec,
9117 struct arm_section * data_arm_sec)
9118 {
9119 struct absaddr addr;
9120 bool res = true;
9121
9122 /* Decode the unwinding instructions. */
9123 while (1)
9124 {
9125 unsigned int op, op2;
9126
9127 ADVANCE;
9128 if (remaining == 0)
9129 break;
9130 remaining--;
9131 op = word >> 24;
9132 word <<= 8;
9133
9134 printf (" 0x%02x ", op);
9135
9136 if ((op & 0xc0) == 0x00)
9137 {
9138 int offset = ((op & 0x3f) << 2) + 4;
9139
9140 printf (" vsp = vsp + %d", offset);
9141 }
9142 else if ((op & 0xc0) == 0x40)
9143 {
9144 int offset = ((op & 0x3f) << 2) + 4;
9145
9146 printf (" vsp = vsp - %d", offset);
9147 }
9148 else if ((op & 0xf0) == 0x80)
9149 {
9150 GET_OP (op2);
9151 if (op == 0x80 && op2 == 0)
9152 printf (_("Refuse to unwind"));
9153 else
9154 {
9155 unsigned int mask = ((op & 0x0f) << 8) | op2;
9156 bool first = true;
9157 int i;
9158
9159 printf ("pop {");
9160 for (i = 0; i < 12; i++)
9161 if (mask & (1 << i))
9162 {
9163 if (first)
9164 first = false;
9165 else
9166 printf (", ");
9167 printf ("r%d", 4 + i);
9168 }
9169 printf ("}");
9170 }
9171 }
9172 else if ((op & 0xf0) == 0x90)
9173 {
9174 if (op == 0x9d || op == 0x9f)
9175 printf (_(" [Reserved]"));
9176 else
9177 printf (" vsp = r%d", op & 0x0f);
9178 }
9179 else if ((op & 0xf0) == 0xa0)
9180 {
9181 int end = 4 + (op & 0x07);
9182 bool first = true;
9183 int i;
9184
9185 printf (" pop {");
9186 for (i = 4; i <= end; i++)
9187 {
9188 if (first)
9189 first = false;
9190 else
9191 printf (", ");
9192 printf ("r%d", i);
9193 }
9194 if (op & 0x08)
9195 {
9196 if (!first)
9197 printf (", ");
9198 printf ("r14");
9199 }
9200 printf ("}");
9201 }
9202 else if (op == 0xb0)
9203 printf (_(" finish"));
9204 else if (op == 0xb1)
9205 {
9206 GET_OP (op2);
9207 if (op2 == 0 || (op2 & 0xf0) != 0)
9208 printf (_("[Spare]"));
9209 else
9210 {
9211 unsigned int mask = op2 & 0x0f;
9212 bool first = true;
9213 int i;
9214
9215 printf ("pop {");
9216 for (i = 0; i < 12; i++)
9217 if (mask & (1 << i))
9218 {
9219 if (first)
9220 first = false;
9221 else
9222 printf (", ");
9223 printf ("r%d", i);
9224 }
9225 printf ("}");
9226 }
9227 }
9228 else if (op == 0xb2)
9229 {
9230 unsigned char buf[9];
9231 unsigned int i, len;
9232 unsigned long offset;
9233
9234 for (i = 0; i < sizeof (buf); i++)
9235 {
9236 GET_OP (buf[i]);
9237 if ((buf[i] & 0x80) == 0)
9238 break;
9239 }
9240 if (i == sizeof (buf))
9241 {
9242 error (_("corrupt change to vsp\n"));
9243 res = false;
9244 }
9245 else
9246 {
9247 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
9248 assert (len == i + 1);
9249 offset = offset * 4 + 0x204;
9250 printf ("vsp = vsp + %ld", offset);
9251 }
9252 }
9253 else if (op == 0xb3 || op == 0xc8 || op == 0xc9)
9254 {
9255 unsigned int first, last;
9256
9257 GET_OP (op2);
9258 first = op2 >> 4;
9259 last = op2 & 0x0f;
9260 if (op == 0xc8)
9261 first = first + 16;
9262 printf ("pop {D%d", first);
9263 if (last)
9264 printf ("-D%d", first + last);
9265 printf ("}");
9266 }
9267 else if ((op & 0xf8) == 0xb8 || (op & 0xf8) == 0xd0)
9268 {
9269 unsigned int count = op & 0x07;
9270
9271 printf ("pop {D8");
9272 if (count)
9273 printf ("-D%d", 8 + count);
9274 printf ("}");
9275 }
9276 else if (op >= 0xc0 && op <= 0xc5)
9277 {
9278 unsigned int count = op & 0x07;
9279
9280 printf (" pop {wR10");
9281 if (count)
9282 printf ("-wR%d", 10 + count);
9283 printf ("}");
9284 }
9285 else if (op == 0xc6)
9286 {
9287 unsigned int first, last;
9288
9289 GET_OP (op2);
9290 first = op2 >> 4;
9291 last = op2 & 0x0f;
9292 printf ("pop {wR%d", first);
9293 if (last)
9294 printf ("-wR%d", first + last);
9295 printf ("}");
9296 }
9297 else if (op == 0xc7)
9298 {
9299 GET_OP (op2);
9300 if (op2 == 0 || (op2 & 0xf0) != 0)
9301 printf (_("[Spare]"));
9302 else
9303 {
9304 unsigned int mask = op2 & 0x0f;
9305 bool first = true;
9306 int i;
9307
9308 printf ("pop {");
9309 for (i = 0; i < 4; i++)
9310 if (mask & (1 << i))
9311 {
9312 if (first)
9313 first = false;
9314 else
9315 printf (", ");
9316 printf ("wCGR%d", i);
9317 }
9318 printf ("}");
9319 }
9320 }
9321 else
9322 {
9323 printf (_(" [unsupported opcode]"));
9324 res = false;
9325 }
9326
9327 printf ("\n");
9328 }
9329
9330 return res;
9331 }
9332
9333 static bool
9334 decode_tic6x_unwind_bytecode (Filedata * filedata,
9335 struct arm_unw_aux_info * aux,
9336 unsigned int word,
9337 unsigned int remaining,
9338 unsigned int more_words,
9339 bfd_vma data_offset,
9340 Elf_Internal_Shdr * data_sec,
9341 struct arm_section * data_arm_sec)
9342 {
9343 struct absaddr addr;
9344
9345 /* Decode the unwinding instructions. */
9346 while (1)
9347 {
9348 unsigned int op, op2;
9349
9350 ADVANCE;
9351 if (remaining == 0)
9352 break;
9353 remaining--;
9354 op = word >> 24;
9355 word <<= 8;
9356
9357 printf (" 0x%02x ", op);
9358
9359 if ((op & 0xc0) == 0x00)
9360 {
9361 int offset = ((op & 0x3f) << 3) + 8;
9362 printf (" sp = sp + %d", offset);
9363 }
9364 else if ((op & 0xc0) == 0x80)
9365 {
9366 GET_OP (op2);
9367 if (op == 0x80 && op2 == 0)
9368 printf (_("Refuse to unwind"));
9369 else
9370 {
9371 unsigned int mask = ((op & 0x1f) << 8) | op2;
9372 if (op & 0x20)
9373 printf ("pop compact {");
9374 else
9375 printf ("pop {");
9376
9377 decode_tic6x_unwind_regmask (mask);
9378 printf("}");
9379 }
9380 }
9381 else if ((op & 0xf0) == 0xc0)
9382 {
9383 unsigned int reg;
9384 unsigned int nregs;
9385 unsigned int i;
9386 const char *name;
9387 struct
9388 {
9389 unsigned int offset;
9390 unsigned int reg;
9391 } regpos[16];
9392
9393 /* Scan entire instruction first so that GET_OP output is not
9394 interleaved with disassembly. */
9395 nregs = 0;
9396 for (i = 0; nregs < (op & 0xf); i++)
9397 {
9398 GET_OP (op2);
9399 reg = op2 >> 4;
9400 if (reg != 0xf)
9401 {
9402 regpos[nregs].offset = i * 2;
9403 regpos[nregs].reg = reg;
9404 nregs++;
9405 }
9406
9407 reg = op2 & 0xf;
9408 if (reg != 0xf)
9409 {
9410 regpos[nregs].offset = i * 2 + 1;
9411 regpos[nregs].reg = reg;
9412 nregs++;
9413 }
9414 }
9415
9416 printf (_("pop frame {"));
9417 if (nregs == 0)
9418 {
9419 printf (_("*corrupt* - no registers specified"));
9420 }
9421 else
9422 {
9423 reg = nregs - 1;
9424 for (i = i * 2; i > 0; i--)
9425 {
9426 if (regpos[reg].offset == i - 1)
9427 {
9428 name = tic6x_unwind_regnames[regpos[reg].reg];
9429 if (reg > 0)
9430 reg--;
9431 }
9432 else
9433 name = _("[pad]");
9434
9435 fputs (name, stdout);
9436 if (i > 1)
9437 printf (", ");
9438 }
9439 }
9440
9441 printf ("}");
9442 }
9443 else if (op == 0xd0)
9444 printf (" MOV FP, SP");
9445 else if (op == 0xd1)
9446 printf (" __c6xabi_pop_rts");
9447 else if (op == 0xd2)
9448 {
9449 unsigned char buf[9];
9450 unsigned int i, len;
9451 unsigned long offset;
9452
9453 for (i = 0; i < sizeof (buf); i++)
9454 {
9455 GET_OP (buf[i]);
9456 if ((buf[i] & 0x80) == 0)
9457 break;
9458 }
9459 /* PR 17531: file: id:000001,src:001906+004739,op:splice,rep:2. */
9460 if (i == sizeof (buf))
9461 {
9462 warn (_("Corrupt stack pointer adjustment detected\n"));
9463 return false;
9464 }
9465
9466 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
9467 assert (len == i + 1);
9468 offset = offset * 8 + 0x408;
9469 printf (_("sp = sp + %ld"), offset);
9470 }
9471 else if ((op & 0xf0) == 0xe0)
9472 {
9473 if ((op & 0x0f) == 7)
9474 printf (" RETURN");
9475 else
9476 printf (" MV %s, B3", tic6x_unwind_regnames[op & 0x0f]);
9477 }
9478 else
9479 {
9480 printf (_(" [unsupported opcode]"));
9481 }
9482 putchar ('\n');
9483 }
9484
9485 return true;
9486 }
9487
9488 static bfd_vma
9489 arm_expand_prel31 (Filedata * filedata, bfd_vma word, bfd_vma where)
9490 {
9491 bfd_vma offset;
9492
9493 offset = word & 0x7fffffff;
9494 if (offset & 0x40000000)
9495 offset |= ~ (bfd_vma) 0x7fffffff;
9496
9497 if (filedata->file_header.e_machine == EM_TI_C6000)
9498 offset <<= 1;
9499
9500 return offset + where;
9501 }
9502
9503 static bool
9504 decode_arm_unwind (Filedata * filedata,
9505 struct arm_unw_aux_info * aux,
9506 unsigned int word,
9507 unsigned int remaining,
9508 bfd_vma data_offset,
9509 Elf_Internal_Shdr * data_sec,
9510 struct arm_section * data_arm_sec)
9511 {
9512 int per_index;
9513 unsigned int more_words = 0;
9514 struct absaddr addr;
9515 bfd_vma sym_name = (bfd_vma) -1;
9516 bool res = true;
9517
9518 if (remaining == 0)
9519 {
9520 /* Fetch the first word.
9521 Note - when decoding an object file the address extracted
9522 here will always be 0. So we also pass in the sym_name
9523 parameter so that we can find the symbol associated with
9524 the personality routine. */
9525 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, data_offset,
9526 & word, & addr, & sym_name))
9527 return false;
9528
9529 remaining = 4;
9530 }
9531 else
9532 {
9533 addr.section = SHN_UNDEF;
9534 addr.offset = 0;
9535 }
9536
9537 if ((word & 0x80000000) == 0)
9538 {
9539 /* Expand prel31 for personality routine. */
9540 bfd_vma fn;
9541 const char *procname;
9542
9543 fn = arm_expand_prel31 (filedata, word, data_sec->sh_addr + data_offset);
9544 printf (_(" Personality routine: "));
9545 if (fn == 0
9546 && addr.section == SHN_UNDEF && addr.offset == 0
9547 && sym_name != (bfd_vma) -1 && sym_name < aux->strtab_size)
9548 {
9549 procname = aux->strtab + sym_name;
9550 print_vma (fn, PREFIX_HEX);
9551 if (procname)
9552 {
9553 fputs (" <", stdout);
9554 fputs (procname, stdout);
9555 fputc ('>', stdout);
9556 }
9557 }
9558 else
9559 procname = arm_print_vma_and_name (filedata, aux, fn, addr);
9560 fputc ('\n', stdout);
9561
9562 /* The GCC personality routines use the standard compact
9563 encoding, starting with one byte giving the number of
9564 words. */
9565 if (procname != NULL
9566 && (startswith (procname, "__gcc_personality_v0")
9567 || startswith (procname, "__gxx_personality_v0")
9568 || startswith (procname, "__gcj_personality_v0")
9569 || startswith (procname, "__gnu_objc_personality_v0")))
9570 {
9571 remaining = 0;
9572 more_words = 1;
9573 ADVANCE;
9574 if (!remaining)
9575 {
9576 printf (_(" [Truncated data]\n"));
9577 return false;
9578 }
9579 more_words = word >> 24;
9580 word <<= 8;
9581 remaining--;
9582 per_index = -1;
9583 }
9584 else
9585 return true;
9586 }
9587 else
9588 {
9589 /* ARM EHABI Section 6.3:
9590
9591 An exception-handling table entry for the compact model looks like:
9592
9593 31 30-28 27-24 23-0
9594 -- ----- ----- ----
9595 1 0 index Data for personalityRoutine[index] */
9596
9597 if (filedata->file_header.e_machine == EM_ARM
9598 && (word & 0x70000000))
9599 {
9600 warn (_("Corrupt ARM compact model table entry: %x \n"), word);
9601 res = false;
9602 }
9603
9604 per_index = (word >> 24) & 0x7f;
9605 printf (_(" Compact model index: %d\n"), per_index);
9606 if (per_index == 0)
9607 {
9608 more_words = 0;
9609 word <<= 8;
9610 remaining--;
9611 }
9612 else if (per_index < 3)
9613 {
9614 more_words = (word >> 16) & 0xff;
9615 word <<= 16;
9616 remaining -= 2;
9617 }
9618 }
9619
9620 switch (filedata->file_header.e_machine)
9621 {
9622 case EM_ARM:
9623 if (per_index < 3)
9624 {
9625 if (! decode_arm_unwind_bytecode (filedata, aux, word, remaining, more_words,
9626 data_offset, data_sec, data_arm_sec))
9627 res = false;
9628 }
9629 else
9630 {
9631 warn (_("Unknown ARM compact model index encountered\n"));
9632 printf (_(" [reserved]\n"));
9633 res = false;
9634 }
9635 break;
9636
9637 case EM_TI_C6000:
9638 if (per_index < 3)
9639 {
9640 if (! decode_tic6x_unwind_bytecode (filedata, aux, word, remaining, more_words,
9641 data_offset, data_sec, data_arm_sec))
9642 res = false;
9643 }
9644 else if (per_index < 5)
9645 {
9646 if (((word >> 17) & 0x7f) == 0x7f)
9647 printf (_(" Restore stack from frame pointer\n"));
9648 else
9649 printf (_(" Stack increment %d\n"), (word >> 14) & 0x1fc);
9650 printf (_(" Registers restored: "));
9651 if (per_index == 4)
9652 printf (" (compact) ");
9653 decode_tic6x_unwind_regmask ((word >> 4) & 0x1fff);
9654 putchar ('\n');
9655 printf (_(" Return register: %s\n"),
9656 tic6x_unwind_regnames[word & 0xf]);
9657 }
9658 else
9659 printf (_(" [reserved (%d)]\n"), per_index);
9660 break;
9661
9662 default:
9663 error (_("Unsupported architecture type %d encountered when decoding unwind table\n"),
9664 filedata->file_header.e_machine);
9665 res = false;
9666 }
9667
9668 /* Decode the descriptors. Not implemented. */
9669
9670 return res;
9671 }
9672
9673 static bool
9674 dump_arm_unwind (Filedata * filedata,
9675 struct arm_unw_aux_info * aux,
9676 Elf_Internal_Shdr * exidx_sec)
9677 {
9678 struct arm_section exidx_arm_sec, extab_arm_sec;
9679 unsigned int i, exidx_len;
9680 unsigned long j, nfuns;
9681 bool res = true;
9682
9683 memset (&exidx_arm_sec, 0, sizeof (exidx_arm_sec));
9684 memset (&extab_arm_sec, 0, sizeof (extab_arm_sec));
9685 exidx_len = exidx_sec->sh_size / 8;
9686
9687 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
9688 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
9689 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
9690 aux->funtab[nfuns++] = aux->symtab[j];
9691 aux->nfuns = nfuns;
9692 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
9693
9694 for (i = 0; i < exidx_len; i++)
9695 {
9696 unsigned int exidx_fn, exidx_entry;
9697 struct absaddr fn_addr, entry_addr;
9698 bfd_vma fn;
9699
9700 fputc ('\n', stdout);
9701
9702 if (! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
9703 8 * i, & exidx_fn, & fn_addr, NULL)
9704 || ! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
9705 8 * i + 4, & exidx_entry, & entry_addr, NULL))
9706 {
9707 free (aux->funtab);
9708 arm_free_section (& exidx_arm_sec);
9709 arm_free_section (& extab_arm_sec);
9710 return false;
9711 }
9712
9713 /* ARM EHABI, Section 5:
9714 An index table entry consists of 2 words.
9715 The first word contains a prel31 offset to the start of a function, with bit 31 clear. */
9716 if (exidx_fn & 0x80000000)
9717 {
9718 warn (_("corrupt index table entry: %x\n"), exidx_fn);
9719 res = false;
9720 }
9721
9722 fn = arm_expand_prel31 (filedata, exidx_fn, exidx_sec->sh_addr + 8 * i);
9723
9724 arm_print_vma_and_name (filedata, aux, fn, fn_addr);
9725 fputs (": ", stdout);
9726
9727 if (exidx_entry == 1)
9728 {
9729 print_vma (exidx_entry, PREFIX_HEX);
9730 fputs (" [cantunwind]\n", stdout);
9731 }
9732 else if (exidx_entry & 0x80000000)
9733 {
9734 print_vma (exidx_entry, PREFIX_HEX);
9735 fputc ('\n', stdout);
9736 decode_arm_unwind (filedata, aux, exidx_entry, 4, 0, NULL, NULL);
9737 }
9738 else
9739 {
9740 bfd_vma table, table_offset = 0;
9741 Elf_Internal_Shdr *table_sec;
9742
9743 fputs ("@", stdout);
9744 table = arm_expand_prel31 (filedata, exidx_entry, exidx_sec->sh_addr + 8 * i + 4);
9745 print_vma (table, PREFIX_HEX);
9746 printf ("\n");
9747
9748 /* Locate the matching .ARM.extab. */
9749 if (entry_addr.section != SHN_UNDEF
9750 && entry_addr.section < filedata->file_header.e_shnum)
9751 {
9752 table_sec = filedata->section_headers + entry_addr.section;
9753 table_offset = entry_addr.offset;
9754 /* PR 18879 */
9755 if (table_offset > table_sec->sh_size
9756 || ((bfd_signed_vma) table_offset) < 0)
9757 {
9758 warn (_("Unwind entry contains corrupt offset (0x%lx) into section %s\n"),
9759 (unsigned long) table_offset,
9760 printable_section_name (filedata, table_sec));
9761 res = false;
9762 continue;
9763 }
9764 }
9765 else
9766 {
9767 table_sec = find_section_by_address (filedata, table);
9768 if (table_sec != NULL)
9769 table_offset = table - table_sec->sh_addr;
9770 }
9771
9772 if (table_sec == NULL)
9773 {
9774 warn (_("Could not locate .ARM.extab section containing 0x%lx.\n"),
9775 (unsigned long) table);
9776 res = false;
9777 continue;
9778 }
9779
9780 if (! decode_arm_unwind (filedata, aux, 0, 0, table_offset, table_sec,
9781 &extab_arm_sec))
9782 res = false;
9783 }
9784 }
9785
9786 printf ("\n");
9787
9788 free (aux->funtab);
9789 arm_free_section (&exidx_arm_sec);
9790 arm_free_section (&extab_arm_sec);
9791
9792 return res;
9793 }
9794
9795 /* Used for both ARM and C6X unwinding tables. */
9796
9797 static bool
9798 arm_process_unwind (Filedata * filedata)
9799 {
9800 struct arm_unw_aux_info aux;
9801 Elf_Internal_Shdr *unwsec = NULL;
9802 Elf_Internal_Shdr *sec;
9803 unsigned long i;
9804 unsigned int sec_type;
9805 bool res = true;
9806
9807 switch (filedata->file_header.e_machine)
9808 {
9809 case EM_ARM:
9810 sec_type = SHT_ARM_EXIDX;
9811 break;
9812
9813 case EM_TI_C6000:
9814 sec_type = SHT_C6000_UNWIND;
9815 break;
9816
9817 default:
9818 error (_("Unsupported architecture type %d encountered when processing unwind table\n"),
9819 filedata->file_header.e_machine);
9820 return false;
9821 }
9822
9823 if (filedata->string_table == NULL)
9824 return false;
9825
9826 memset (& aux, 0, sizeof (aux));
9827 aux.filedata = filedata;
9828
9829 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9830 {
9831 if (sec->sh_type == SHT_SYMTAB)
9832 {
9833 if (aux.symtab)
9834 {
9835 error (_("Multiple symbol tables encountered\n"));
9836 free (aux.symtab);
9837 aux.symtab = NULL;
9838 free (aux.strtab);
9839 aux.strtab = NULL;
9840 }
9841 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
9842 &aux.strtab, &aux.strtab_size))
9843 return false;
9844 }
9845 else if (sec->sh_type == sec_type)
9846 unwsec = sec;
9847 }
9848
9849 if (unwsec == NULL)
9850 printf (_("\nThere are no unwind sections in this file.\n"));
9851 else
9852 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9853 {
9854 if (sec->sh_type == sec_type)
9855 {
9856 unsigned long num_unwind = sec->sh_size / (2 * eh_addr_size);
9857 printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
9858 "contains %lu entry:\n",
9859 "\nUnwind section '%s' at offset 0x%lx "
9860 "contains %lu entries:\n",
9861 num_unwind),
9862 printable_section_name (filedata, sec),
9863 (unsigned long) sec->sh_offset,
9864 num_unwind);
9865
9866 if (! dump_arm_unwind (filedata, &aux, sec))
9867 res = false;
9868 }
9869 }
9870
9871 free (aux.symtab);
9872 free ((char *) aux.strtab);
9873
9874 return res;
9875 }
9876
9877 static bool
9878 no_processor_specific_unwind (Filedata * filedata ATTRIBUTE_UNUSED)
9879 {
9880 printf (_("No processor specific unwind information to decode\n"));
9881 return true;
9882 }
9883
9884 static bool
9885 process_unwind (Filedata * filedata)
9886 {
9887 struct unwind_handler
9888 {
9889 unsigned int machtype;
9890 bool (* handler)(Filedata *);
9891 } handlers[] =
9892 {
9893 { EM_ARM, arm_process_unwind },
9894 { EM_IA_64, ia64_process_unwind },
9895 { EM_PARISC, hppa_process_unwind },
9896 { EM_TI_C6000, arm_process_unwind },
9897 { EM_386, no_processor_specific_unwind },
9898 { EM_X86_64, no_processor_specific_unwind },
9899 { 0, NULL }
9900 };
9901 int i;
9902
9903 if (!do_unwind)
9904 return true;
9905
9906 for (i = 0; handlers[i].handler != NULL; i++)
9907 if (filedata->file_header.e_machine == handlers[i].machtype)
9908 return handlers[i].handler (filedata);
9909
9910 printf (_("\nThe decoding of unwind sections for machine type %s is not currently supported.\n"),
9911 get_machine_name (filedata->file_header.e_machine));
9912 return true;
9913 }
9914
9915 static void
9916 dynamic_section_aarch64_val (Elf_Internal_Dyn * entry)
9917 {
9918 switch (entry->d_tag)
9919 {
9920 case DT_AARCH64_BTI_PLT:
9921 case DT_AARCH64_PAC_PLT:
9922 break;
9923 default:
9924 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
9925 break;
9926 }
9927 putchar ('\n');
9928 }
9929
9930 static void
9931 dynamic_section_mips_val (Filedata * filedata, Elf_Internal_Dyn * entry)
9932 {
9933 switch (entry->d_tag)
9934 {
9935 case DT_MIPS_FLAGS:
9936 if (entry->d_un.d_val == 0)
9937 printf (_("NONE"));
9938 else
9939 {
9940 static const char * opts[] =
9941 {
9942 "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
9943 "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
9944 "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
9945 "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
9946 "RLD_ORDER_SAFE"
9947 };
9948 unsigned int cnt;
9949 bool first = true;
9950
9951 for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
9952 if (entry->d_un.d_val & (1 << cnt))
9953 {
9954 printf ("%s%s", first ? "" : " ", opts[cnt]);
9955 first = false;
9956 }
9957 }
9958 break;
9959
9960 case DT_MIPS_IVERSION:
9961 if (VALID_DYNAMIC_NAME (filedata, entry->d_un.d_val))
9962 printf (_("Interface Version: %s"),
9963 GET_DYNAMIC_NAME (filedata, entry->d_un.d_val));
9964 else
9965 {
9966 char buf[40];
9967 sprintf_vma (buf, entry->d_un.d_ptr);
9968 /* Note: coded this way so that there is a single string for translation. */
9969 printf (_("<corrupt: %s>"), buf);
9970 }
9971 break;
9972
9973 case DT_MIPS_TIME_STAMP:
9974 {
9975 char timebuf[128];
9976 struct tm * tmp;
9977 time_t atime = entry->d_un.d_val;
9978
9979 tmp = gmtime (&atime);
9980 /* PR 17531: file: 6accc532. */
9981 if (tmp == NULL)
9982 snprintf (timebuf, sizeof (timebuf), _("<corrupt>"));
9983 else
9984 snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
9985 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
9986 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
9987 printf (_("Time Stamp: %s"), timebuf);
9988 }
9989 break;
9990
9991 case DT_MIPS_RLD_VERSION:
9992 case DT_MIPS_LOCAL_GOTNO:
9993 case DT_MIPS_CONFLICTNO:
9994 case DT_MIPS_LIBLISTNO:
9995 case DT_MIPS_SYMTABNO:
9996 case DT_MIPS_UNREFEXTNO:
9997 case DT_MIPS_HIPAGENO:
9998 case DT_MIPS_DELTA_CLASS_NO:
9999 case DT_MIPS_DELTA_INSTANCE_NO:
10000 case DT_MIPS_DELTA_RELOC_NO:
10001 case DT_MIPS_DELTA_SYM_NO:
10002 case DT_MIPS_DELTA_CLASSSYM_NO:
10003 case DT_MIPS_COMPACT_SIZE:
10004 print_vma (entry->d_un.d_val, DEC);
10005 break;
10006
10007 case DT_MIPS_XHASH:
10008 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
10009 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10010 /* Falls through. */
10011
10012 default:
10013 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10014 }
10015 putchar ('\n');
10016 }
10017
10018 static void
10019 dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
10020 {
10021 switch (entry->d_tag)
10022 {
10023 case DT_HP_DLD_FLAGS:
10024 {
10025 static struct
10026 {
10027 long int bit;
10028 const char * str;
10029 }
10030 flags[] =
10031 {
10032 { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
10033 { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
10034 { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
10035 { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
10036 { DT_HP_BIND_NOW, "HP_BIND_NOW" },
10037 { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
10038 { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
10039 { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
10040 { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
10041 { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
10042 { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
10043 { DT_HP_GST, "HP_GST" },
10044 { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
10045 { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
10046 { DT_HP_NODELETE, "HP_NODELETE" },
10047 { DT_HP_GROUP, "HP_GROUP" },
10048 { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
10049 };
10050 bool first = true;
10051 size_t cnt;
10052 bfd_vma val = entry->d_un.d_val;
10053
10054 for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
10055 if (val & flags[cnt].bit)
10056 {
10057 if (! first)
10058 putchar (' ');
10059 fputs (flags[cnt].str, stdout);
10060 first = false;
10061 val ^= flags[cnt].bit;
10062 }
10063
10064 if (val != 0 || first)
10065 {
10066 if (! first)
10067 putchar (' ');
10068 print_vma (val, HEX);
10069 }
10070 }
10071 break;
10072
10073 default:
10074 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10075 break;
10076 }
10077 putchar ('\n');
10078 }
10079
10080 #ifdef BFD64
10081
10082 /* VMS vs Unix time offset and factor. */
10083
10084 #define VMS_EPOCH_OFFSET 35067168000000000LL
10085 #define VMS_GRANULARITY_FACTOR 10000000
10086 #ifndef INT64_MIN
10087 #define INT64_MIN (-9223372036854775807LL - 1)
10088 #endif
10089
10090 /* Display a VMS time in a human readable format. */
10091
10092 static void
10093 print_vms_time (bfd_int64_t vmstime)
10094 {
10095 struct tm *tm = NULL;
10096 time_t unxtime;
10097
10098 if (vmstime >= INT64_MIN + VMS_EPOCH_OFFSET)
10099 {
10100 vmstime = (vmstime - VMS_EPOCH_OFFSET) / VMS_GRANULARITY_FACTOR;
10101 unxtime = vmstime;
10102 if (unxtime == vmstime)
10103 tm = gmtime (&unxtime);
10104 }
10105 if (tm != NULL)
10106 printf ("%04u-%02u-%02uT%02u:%02u:%02u",
10107 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
10108 tm->tm_hour, tm->tm_min, tm->tm_sec);
10109 }
10110 #endif /* BFD64 */
10111
10112 static void
10113 dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
10114 {
10115 switch (entry->d_tag)
10116 {
10117 case DT_IA_64_PLT_RESERVE:
10118 /* First 3 slots reserved. */
10119 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10120 printf (" -- ");
10121 print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
10122 break;
10123
10124 case DT_IA_64_VMS_LINKTIME:
10125 #ifdef BFD64
10126 print_vms_time (entry->d_un.d_val);
10127 #endif
10128 break;
10129
10130 case DT_IA_64_VMS_LNKFLAGS:
10131 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10132 if (entry->d_un.d_val & VMS_LF_CALL_DEBUG)
10133 printf (" CALL_DEBUG");
10134 if (entry->d_un.d_val & VMS_LF_NOP0BUFS)
10135 printf (" NOP0BUFS");
10136 if (entry->d_un.d_val & VMS_LF_P0IMAGE)
10137 printf (" P0IMAGE");
10138 if (entry->d_un.d_val & VMS_LF_MKTHREADS)
10139 printf (" MKTHREADS");
10140 if (entry->d_un.d_val & VMS_LF_UPCALLS)
10141 printf (" UPCALLS");
10142 if (entry->d_un.d_val & VMS_LF_IMGSTA)
10143 printf (" IMGSTA");
10144 if (entry->d_un.d_val & VMS_LF_INITIALIZE)
10145 printf (" INITIALIZE");
10146 if (entry->d_un.d_val & VMS_LF_MAIN)
10147 printf (" MAIN");
10148 if (entry->d_un.d_val & VMS_LF_EXE_INIT)
10149 printf (" EXE_INIT");
10150 if (entry->d_un.d_val & VMS_LF_TBK_IN_IMG)
10151 printf (" TBK_IN_IMG");
10152 if (entry->d_un.d_val & VMS_LF_DBG_IN_IMG)
10153 printf (" DBG_IN_IMG");
10154 if (entry->d_un.d_val & VMS_LF_TBK_IN_DSF)
10155 printf (" TBK_IN_DSF");
10156 if (entry->d_un.d_val & VMS_LF_DBG_IN_DSF)
10157 printf (" DBG_IN_DSF");
10158 if (entry->d_un.d_val & VMS_LF_SIGNATURES)
10159 printf (" SIGNATURES");
10160 if (entry->d_un.d_val & VMS_LF_REL_SEG_OFF)
10161 printf (" REL_SEG_OFF");
10162 break;
10163
10164 default:
10165 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10166 break;
10167 }
10168 putchar ('\n');
10169 }
10170
10171 static bool
10172 get_32bit_dynamic_section (Filedata * filedata)
10173 {
10174 Elf32_External_Dyn * edyn;
10175 Elf32_External_Dyn * ext;
10176 Elf_Internal_Dyn * entry;
10177
10178 edyn = (Elf32_External_Dyn *) get_data (NULL, filedata,
10179 filedata->dynamic_addr, 1,
10180 filedata->dynamic_size,
10181 _("dynamic section"));
10182 if (!edyn)
10183 return false;
10184
10185 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
10186 might not have the luxury of section headers. Look for the DT_NULL
10187 terminator to determine the number of entries. */
10188 for (ext = edyn, filedata->dynamic_nent = 0;
10189 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
10190 ext++)
10191 {
10192 filedata->dynamic_nent++;
10193 if (BYTE_GET (ext->d_tag) == DT_NULL)
10194 break;
10195 }
10196
10197 filedata->dynamic_section
10198 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
10199 if (filedata->dynamic_section == NULL)
10200 {
10201 error (_("Out of memory allocating space for %lu dynamic entries\n"),
10202 (unsigned long) filedata->dynamic_nent);
10203 free (edyn);
10204 return false;
10205 }
10206
10207 for (ext = edyn, entry = filedata->dynamic_section;
10208 entry < filedata->dynamic_section + filedata->dynamic_nent;
10209 ext++, entry++)
10210 {
10211 entry->d_tag = BYTE_GET (ext->d_tag);
10212 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
10213 }
10214
10215 free (edyn);
10216
10217 return true;
10218 }
10219
10220 static bool
10221 get_64bit_dynamic_section (Filedata * filedata)
10222 {
10223 Elf64_External_Dyn * edyn;
10224 Elf64_External_Dyn * ext;
10225 Elf_Internal_Dyn * entry;
10226
10227 /* Read in the data. */
10228 edyn = (Elf64_External_Dyn *) get_data (NULL, filedata,
10229 filedata->dynamic_addr, 1,
10230 filedata->dynamic_size,
10231 _("dynamic section"));
10232 if (!edyn)
10233 return false;
10234
10235 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
10236 might not have the luxury of section headers. Look for the DT_NULL
10237 terminator to determine the number of entries. */
10238 for (ext = edyn, filedata->dynamic_nent = 0;
10239 /* PR 17533 file: 033-67080-0.004 - do not read past end of buffer. */
10240 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
10241 ext++)
10242 {
10243 filedata->dynamic_nent++;
10244 if (BYTE_GET (ext->d_tag) == DT_NULL)
10245 break;
10246 }
10247
10248 filedata->dynamic_section
10249 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
10250 if (filedata->dynamic_section == NULL)
10251 {
10252 error (_("Out of memory allocating space for %lu dynamic entries\n"),
10253 (unsigned long) filedata->dynamic_nent);
10254 free (edyn);
10255 return false;
10256 }
10257
10258 /* Convert from external to internal formats. */
10259 for (ext = edyn, entry = filedata->dynamic_section;
10260 entry < filedata->dynamic_section + filedata->dynamic_nent;
10261 ext++, entry++)
10262 {
10263 entry->d_tag = BYTE_GET (ext->d_tag);
10264 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
10265 }
10266
10267 free (edyn);
10268
10269 return true;
10270 }
10271
10272 static bool
10273 get_dynamic_section (Filedata *filedata)
10274 {
10275 if (filedata->dynamic_section)
10276 return true;
10277
10278 if (is_32bit_elf)
10279 return get_32bit_dynamic_section (filedata);
10280 else
10281 return get_64bit_dynamic_section (filedata);
10282 }
10283
10284 static void
10285 print_dynamic_flags (bfd_vma flags)
10286 {
10287 bool first = true;
10288
10289 while (flags)
10290 {
10291 bfd_vma flag;
10292
10293 flag = flags & - flags;
10294 flags &= ~ flag;
10295
10296 if (first)
10297 first = false;
10298 else
10299 putc (' ', stdout);
10300
10301 switch (flag)
10302 {
10303 case DF_ORIGIN: fputs ("ORIGIN", stdout); break;
10304 case DF_SYMBOLIC: fputs ("SYMBOLIC", stdout); break;
10305 case DF_TEXTREL: fputs ("TEXTREL", stdout); break;
10306 case DF_BIND_NOW: fputs ("BIND_NOW", stdout); break;
10307 case DF_STATIC_TLS: fputs ("STATIC_TLS", stdout); break;
10308 default: fputs (_("unknown"), stdout); break;
10309 }
10310 }
10311 puts ("");
10312 }
10313
10314 static bfd_vma *
10315 get_dynamic_data (Filedata * filedata, bfd_size_type number, unsigned int ent_size)
10316 {
10317 unsigned char * e_data;
10318 bfd_vma * i_data;
10319
10320 /* If the size_t type is smaller than the bfd_size_type, eg because
10321 you are building a 32-bit tool on a 64-bit host, then make sure
10322 that when (number) is cast to (size_t) no information is lost. */
10323 if (sizeof (size_t) < sizeof (bfd_size_type)
10324 && (bfd_size_type) ((size_t) number) != number)
10325 {
10326 error (_("Size truncation prevents reading %s elements of size %u\n"),
10327 bfd_vmatoa ("u", number), ent_size);
10328 return NULL;
10329 }
10330
10331 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
10332 attempting to allocate memory when the read is bound to fail. */
10333 if (ent_size * number > filedata->file_size)
10334 {
10335 error (_("Invalid number of dynamic entries: %s\n"),
10336 bfd_vmatoa ("u", number));
10337 return NULL;
10338 }
10339
10340 e_data = (unsigned char *) cmalloc ((size_t) number, ent_size);
10341 if (e_data == NULL)
10342 {
10343 error (_("Out of memory reading %s dynamic entries\n"),
10344 bfd_vmatoa ("u", number));
10345 return NULL;
10346 }
10347
10348 if (fread (e_data, ent_size, (size_t) number, filedata->handle) != number)
10349 {
10350 error (_("Unable to read in %s bytes of dynamic data\n"),
10351 bfd_vmatoa ("u", number * ent_size));
10352 free (e_data);
10353 return NULL;
10354 }
10355
10356 i_data = (bfd_vma *) cmalloc ((size_t) number, sizeof (*i_data));
10357 if (i_data == NULL)
10358 {
10359 error (_("Out of memory allocating space for %s dynamic entries\n"),
10360 bfd_vmatoa ("u", number));
10361 free (e_data);
10362 return NULL;
10363 }
10364
10365 while (number--)
10366 i_data[number] = byte_get (e_data + number * ent_size, ent_size);
10367
10368 free (e_data);
10369
10370 return i_data;
10371 }
10372
10373 static unsigned long
10374 get_num_dynamic_syms (Filedata * filedata)
10375 {
10376 unsigned long num_of_syms = 0;
10377
10378 if (!do_histogram && (!do_using_dynamic || do_dyn_syms))
10379 return num_of_syms;
10380
10381 if (filedata->dynamic_info[DT_HASH])
10382 {
10383 unsigned char nb[8];
10384 unsigned char nc[8];
10385 unsigned int hash_ent_size = 4;
10386
10387 if ((filedata->file_header.e_machine == EM_ALPHA
10388 || filedata->file_header.e_machine == EM_S390
10389 || filedata->file_header.e_machine == EM_S390_OLD)
10390 && filedata->file_header.e_ident[EI_CLASS] == ELFCLASS64)
10391 hash_ent_size = 8;
10392
10393 if (fseek (filedata->handle,
10394 (filedata->archive_file_offset
10395 + offset_from_vma (filedata, filedata->dynamic_info[DT_HASH],
10396 sizeof nb + sizeof nc)),
10397 SEEK_SET))
10398 {
10399 error (_("Unable to seek to start of dynamic information\n"));
10400 goto no_hash;
10401 }
10402
10403 if (fread (nb, hash_ent_size, 1, filedata->handle) != 1)
10404 {
10405 error (_("Failed to read in number of buckets\n"));
10406 goto no_hash;
10407 }
10408
10409 if (fread (nc, hash_ent_size, 1, filedata->handle) != 1)
10410 {
10411 error (_("Failed to read in number of chains\n"));
10412 goto no_hash;
10413 }
10414
10415 filedata->nbuckets = byte_get (nb, hash_ent_size);
10416 filedata->nchains = byte_get (nc, hash_ent_size);
10417
10418 if (filedata->nbuckets != 0 && filedata->nchains != 0)
10419 {
10420 filedata->buckets = get_dynamic_data (filedata, filedata->nbuckets,
10421 hash_ent_size);
10422 filedata->chains = get_dynamic_data (filedata, filedata->nchains,
10423 hash_ent_size);
10424
10425 if (filedata->buckets != NULL && filedata->chains != NULL)
10426 num_of_syms = filedata->nchains;
10427 }
10428 no_hash:
10429 if (num_of_syms == 0)
10430 {
10431 free (filedata->buckets);
10432 filedata->buckets = NULL;
10433 free (filedata->chains);
10434 filedata->chains = NULL;
10435 filedata->nbuckets = 0;
10436 }
10437 }
10438
10439 if (filedata->dynamic_info_DT_GNU_HASH)
10440 {
10441 unsigned char nb[16];
10442 bfd_vma i, maxchain = 0xffffffff, bitmaskwords;
10443 bfd_vma buckets_vma;
10444 unsigned long hn;
10445
10446 if (fseek (filedata->handle,
10447 (filedata->archive_file_offset
10448 + offset_from_vma (filedata,
10449 filedata->dynamic_info_DT_GNU_HASH,
10450 sizeof nb)),
10451 SEEK_SET))
10452 {
10453 error (_("Unable to seek to start of dynamic information\n"));
10454 goto no_gnu_hash;
10455 }
10456
10457 if (fread (nb, 16, 1, filedata->handle) != 1)
10458 {
10459 error (_("Failed to read in number of buckets\n"));
10460 goto no_gnu_hash;
10461 }
10462
10463 filedata->ngnubuckets = byte_get (nb, 4);
10464 filedata->gnusymidx = byte_get (nb + 4, 4);
10465 bitmaskwords = byte_get (nb + 8, 4);
10466 buckets_vma = filedata->dynamic_info_DT_GNU_HASH + 16;
10467 if (is_32bit_elf)
10468 buckets_vma += bitmaskwords * 4;
10469 else
10470 buckets_vma += bitmaskwords * 8;
10471
10472 if (fseek (filedata->handle,
10473 (filedata->archive_file_offset
10474 + offset_from_vma (filedata, buckets_vma, 4)),
10475 SEEK_SET))
10476 {
10477 error (_("Unable to seek to start of dynamic information\n"));
10478 goto no_gnu_hash;
10479 }
10480
10481 filedata->gnubuckets
10482 = get_dynamic_data (filedata, filedata->ngnubuckets, 4);
10483
10484 if (filedata->gnubuckets == NULL)
10485 goto no_gnu_hash;
10486
10487 for (i = 0; i < filedata->ngnubuckets; i++)
10488 if (filedata->gnubuckets[i] != 0)
10489 {
10490 if (filedata->gnubuckets[i] < filedata->gnusymidx)
10491 goto no_gnu_hash;
10492
10493 if (maxchain == 0xffffffff || filedata->gnubuckets[i] > maxchain)
10494 maxchain = filedata->gnubuckets[i];
10495 }
10496
10497 if (maxchain == 0xffffffff)
10498 goto no_gnu_hash;
10499
10500 maxchain -= filedata->gnusymidx;
10501
10502 if (fseek (filedata->handle,
10503 (filedata->archive_file_offset
10504 + offset_from_vma (filedata,
10505 buckets_vma + 4 * (filedata->ngnubuckets
10506 + maxchain),
10507 4)),
10508 SEEK_SET))
10509 {
10510 error (_("Unable to seek to start of dynamic information\n"));
10511 goto no_gnu_hash;
10512 }
10513
10514 do
10515 {
10516 if (fread (nb, 4, 1, filedata->handle) != 1)
10517 {
10518 error (_("Failed to determine last chain length\n"));
10519 goto no_gnu_hash;
10520 }
10521
10522 if (maxchain + 1 == 0)
10523 goto no_gnu_hash;
10524
10525 ++maxchain;
10526 }
10527 while ((byte_get (nb, 4) & 1) == 0);
10528
10529 if (fseek (filedata->handle,
10530 (filedata->archive_file_offset
10531 + offset_from_vma (filedata, (buckets_vma
10532 + 4 * filedata->ngnubuckets),
10533 4)),
10534 SEEK_SET))
10535 {
10536 error (_("Unable to seek to start of dynamic information\n"));
10537 goto no_gnu_hash;
10538 }
10539
10540 filedata->gnuchains = get_dynamic_data (filedata, maxchain, 4);
10541 filedata->ngnuchains = maxchain;
10542
10543 if (filedata->gnuchains == NULL)
10544 goto no_gnu_hash;
10545
10546 if (filedata->dynamic_info_DT_MIPS_XHASH)
10547 {
10548 if (fseek (filedata->handle,
10549 (filedata->archive_file_offset
10550 + offset_from_vma (filedata, (buckets_vma
10551 + 4 * (filedata->ngnubuckets
10552 + maxchain)), 4)),
10553 SEEK_SET))
10554 {
10555 error (_("Unable to seek to start of dynamic information\n"));
10556 goto no_gnu_hash;
10557 }
10558
10559 filedata->mipsxlat = get_dynamic_data (filedata, maxchain, 4);
10560 if (filedata->mipsxlat == NULL)
10561 goto no_gnu_hash;
10562 }
10563
10564 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
10565 if (filedata->gnubuckets[hn] != 0)
10566 {
10567 bfd_vma si = filedata->gnubuckets[hn];
10568 bfd_vma off = si - filedata->gnusymidx;
10569
10570 do
10571 {
10572 if (filedata->dynamic_info_DT_MIPS_XHASH)
10573 {
10574 if (off < filedata->ngnuchains
10575 && filedata->mipsxlat[off] >= num_of_syms)
10576 num_of_syms = filedata->mipsxlat[off] + 1;
10577 }
10578 else
10579 {
10580 if (si >= num_of_syms)
10581 num_of_syms = si + 1;
10582 }
10583 si++;
10584 }
10585 while (off < filedata->ngnuchains
10586 && (filedata->gnuchains[off++] & 1) == 0);
10587 }
10588
10589 if (num_of_syms == 0)
10590 {
10591 no_gnu_hash:
10592 free (filedata->mipsxlat);
10593 filedata->mipsxlat = NULL;
10594 free (filedata->gnuchains);
10595 filedata->gnuchains = NULL;
10596 free (filedata->gnubuckets);
10597 filedata->gnubuckets = NULL;
10598 filedata->ngnubuckets = 0;
10599 filedata->ngnuchains = 0;
10600 }
10601 }
10602
10603 return num_of_syms;
10604 }
10605
10606 /* Parse and display the contents of the dynamic section. */
10607
10608 static bool
10609 process_dynamic_section (Filedata * filedata)
10610 {
10611 Elf_Internal_Dyn * entry;
10612
10613 if (filedata->dynamic_size == 0)
10614 {
10615 if (do_dynamic)
10616 {
10617 if (filedata->is_separate)
10618 printf (_("\nThere is no dynamic section in linked file '%s'.\n"),
10619 filedata->file_name);
10620 else
10621 printf (_("\nThere is no dynamic section in this file.\n"));
10622 }
10623
10624 return true;
10625 }
10626
10627 if (!get_dynamic_section (filedata))
10628 return false;
10629
10630 /* Find the appropriate symbol table. */
10631 if (filedata->dynamic_symbols == NULL || do_histogram)
10632 {
10633 unsigned long num_of_syms;
10634
10635 for (entry = filedata->dynamic_section;
10636 entry < filedata->dynamic_section + filedata->dynamic_nent;
10637 ++entry)
10638 if (entry->d_tag == DT_SYMTAB)
10639 filedata->dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
10640 else if (entry->d_tag == DT_SYMENT)
10641 filedata->dynamic_info[DT_SYMENT] = entry->d_un.d_val;
10642 else if (entry->d_tag == DT_HASH)
10643 filedata->dynamic_info[DT_HASH] = entry->d_un.d_val;
10644 else if (entry->d_tag == DT_GNU_HASH)
10645 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10646 else if ((filedata->file_header.e_machine == EM_MIPS
10647 || filedata->file_header.e_machine == EM_MIPS_RS3_LE)
10648 && entry->d_tag == DT_MIPS_XHASH)
10649 {
10650 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
10651 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10652 }
10653
10654 num_of_syms = get_num_dynamic_syms (filedata);
10655
10656 if (num_of_syms != 0
10657 && filedata->dynamic_symbols == NULL
10658 && filedata->dynamic_info[DT_SYMTAB]
10659 && filedata->dynamic_info[DT_SYMENT])
10660 {
10661 Elf_Internal_Phdr *seg;
10662 bfd_vma vma = filedata->dynamic_info[DT_SYMTAB];
10663
10664 if (! get_program_headers (filedata))
10665 {
10666 error (_("Cannot interpret virtual addresses "
10667 "without program headers.\n"));
10668 return false;
10669 }
10670
10671 for (seg = filedata->program_headers;
10672 seg < filedata->program_headers + filedata->file_header.e_phnum;
10673 ++seg)
10674 {
10675 if (seg->p_type != PT_LOAD)
10676 continue;
10677
10678 if (seg->p_offset + seg->p_filesz > filedata->file_size)
10679 {
10680 /* See PR 21379 for a reproducer. */
10681 error (_("Invalid PT_LOAD entry\n"));
10682 return false;
10683 }
10684
10685 if (vma >= (seg->p_vaddr & -seg->p_align)
10686 && vma < seg->p_vaddr + seg->p_filesz)
10687 {
10688 /* Since we do not know how big the symbol table is,
10689 we default to reading in up to the end of PT_LOAD
10690 segment and processing that. This is overkill, I
10691 know, but it should work. */
10692 Elf_Internal_Shdr section;
10693 section.sh_offset = (vma - seg->p_vaddr
10694 + seg->p_offset);
10695 section.sh_size = (num_of_syms
10696 * filedata->dynamic_info[DT_SYMENT]);
10697 section.sh_entsize = filedata->dynamic_info[DT_SYMENT];
10698
10699 if (do_checks
10700 && filedata->dynamic_symtab_section != NULL
10701 && ((filedata->dynamic_symtab_section->sh_offset
10702 != section.sh_offset)
10703 || (filedata->dynamic_symtab_section->sh_size
10704 != section.sh_size)
10705 || (filedata->dynamic_symtab_section->sh_entsize
10706 != section.sh_entsize)))
10707 warn (_("\
10708 the .dynsym section doesn't match the DT_SYMTAB and DT_SYMENT tags\n"));
10709
10710 section.sh_name = filedata->string_table_length;
10711 filedata->dynamic_symbols
10712 = get_elf_symbols (filedata, &section,
10713 &filedata->num_dynamic_syms);
10714 if (filedata->dynamic_symbols == NULL
10715 || filedata->num_dynamic_syms != num_of_syms)
10716 {
10717 error (_("Corrupt DT_SYMTAB dynamic entry\n"));
10718 return false;
10719 }
10720 break;
10721 }
10722 }
10723 }
10724 }
10725
10726 /* Similarly find a string table. */
10727 if (filedata->dynamic_strings == NULL)
10728 for (entry = filedata->dynamic_section;
10729 entry < filedata->dynamic_section + filedata->dynamic_nent;
10730 ++entry)
10731 {
10732 if (entry->d_tag == DT_STRTAB)
10733 filedata->dynamic_info[DT_STRTAB] = entry->d_un.d_val;
10734
10735 if (entry->d_tag == DT_STRSZ)
10736 filedata->dynamic_info[DT_STRSZ] = entry->d_un.d_val;
10737
10738 if (filedata->dynamic_info[DT_STRTAB]
10739 && filedata->dynamic_info[DT_STRSZ])
10740 {
10741 unsigned long offset;
10742 bfd_size_type str_tab_len = filedata->dynamic_info[DT_STRSZ];
10743
10744 offset = offset_from_vma (filedata,
10745 filedata->dynamic_info[DT_STRTAB],
10746 str_tab_len);
10747 if (do_checks
10748 && filedata->dynamic_strtab_section
10749 && ((filedata->dynamic_strtab_section->sh_offset
10750 != (file_ptr) offset)
10751 || (filedata->dynamic_strtab_section->sh_size
10752 != str_tab_len)))
10753 warn (_("\
10754 the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
10755
10756 filedata->dynamic_strings
10757 = (char *) get_data (NULL, filedata, offset, 1, str_tab_len,
10758 _("dynamic string table"));
10759 if (filedata->dynamic_strings == NULL)
10760 {
10761 error (_("Corrupt DT_STRTAB dynamic entry\n"));
10762 break;
10763 }
10764
10765 filedata->dynamic_strings_length = str_tab_len;
10766 break;
10767 }
10768 }
10769
10770 /* And find the syminfo section if available. */
10771 if (filedata->dynamic_syminfo == NULL)
10772 {
10773 unsigned long syminsz = 0;
10774
10775 for (entry = filedata->dynamic_section;
10776 entry < filedata->dynamic_section + filedata->dynamic_nent;
10777 ++entry)
10778 {
10779 if (entry->d_tag == DT_SYMINENT)
10780 {
10781 /* Note: these braces are necessary to avoid a syntax
10782 error from the SunOS4 C compiler. */
10783 /* PR binutils/17531: A corrupt file can trigger this test.
10784 So do not use an assert, instead generate an error message. */
10785 if (sizeof (Elf_External_Syminfo) != entry->d_un.d_val)
10786 error (_("Bad value (%d) for SYMINENT entry\n"),
10787 (int) entry->d_un.d_val);
10788 }
10789 else if (entry->d_tag == DT_SYMINSZ)
10790 syminsz = entry->d_un.d_val;
10791 else if (entry->d_tag == DT_SYMINFO)
10792 filedata->dynamic_syminfo_offset
10793 = offset_from_vma (filedata, entry->d_un.d_val, syminsz);
10794 }
10795
10796 if (filedata->dynamic_syminfo_offset != 0 && syminsz != 0)
10797 {
10798 Elf_External_Syminfo * extsyminfo;
10799 Elf_External_Syminfo * extsym;
10800 Elf_Internal_Syminfo * syminfo;
10801
10802 /* There is a syminfo section. Read the data. */
10803 extsyminfo = (Elf_External_Syminfo *)
10804 get_data (NULL, filedata, filedata->dynamic_syminfo_offset,
10805 1, syminsz, _("symbol information"));
10806 if (!extsyminfo)
10807 return false;
10808
10809 if (filedata->dynamic_syminfo != NULL)
10810 {
10811 error (_("Multiple dynamic symbol information sections found\n"));
10812 free (filedata->dynamic_syminfo);
10813 }
10814 filedata->dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
10815 if (filedata->dynamic_syminfo == NULL)
10816 {
10817 error (_("Out of memory allocating %lu bytes "
10818 "for dynamic symbol info\n"),
10819 (unsigned long) syminsz);
10820 return false;
10821 }
10822
10823 filedata->dynamic_syminfo_nent
10824 = syminsz / sizeof (Elf_External_Syminfo);
10825 for (syminfo = filedata->dynamic_syminfo, extsym = extsyminfo;
10826 syminfo < (filedata->dynamic_syminfo
10827 + filedata->dynamic_syminfo_nent);
10828 ++syminfo, ++extsym)
10829 {
10830 syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
10831 syminfo->si_flags = BYTE_GET (extsym->si_flags);
10832 }
10833
10834 free (extsyminfo);
10835 }
10836 }
10837
10838 if (do_dynamic && filedata->dynamic_addr)
10839 {
10840 if (filedata->dynamic_nent == 1)
10841 {
10842 if (filedata->is_separate)
10843 printf (_("\nIn linked file '%s' the dynamic section at offset 0x%lx contains 1 entry:\n"),
10844 filedata->file_name,
10845 filedata->dynamic_addr);
10846 else
10847 printf (_("\nDynamic section at offset 0x%lx contains 1 entry:\n"),
10848 filedata->dynamic_addr);
10849 }
10850 else
10851 {
10852 if (filedata->is_separate)
10853 printf (_("\nIn linked file '%s' the dynamic section at offset 0x%lx contains %lu entries:\n"),
10854 filedata->file_name,
10855 filedata->dynamic_addr,
10856 (unsigned long) filedata->dynamic_nent);
10857 else
10858 printf (_("\nDynamic section at offset 0x%lx contains %lu entries:\n"),
10859 filedata->dynamic_addr,
10860 (unsigned long) filedata->dynamic_nent);
10861 }
10862 }
10863 if (do_dynamic)
10864 printf (_(" Tag Type Name/Value\n"));
10865
10866 for (entry = filedata->dynamic_section;
10867 entry < filedata->dynamic_section + filedata->dynamic_nent;
10868 entry++)
10869 {
10870 if (do_dynamic)
10871 {
10872 const char * dtype;
10873
10874 putchar (' ');
10875 print_vma (entry->d_tag, FULL_HEX);
10876 dtype = get_dynamic_type (filedata, entry->d_tag);
10877 printf (" (%s)%*s", dtype,
10878 ((is_32bit_elf ? 27 : 19) - (int) strlen (dtype)), " ");
10879 }
10880
10881 switch (entry->d_tag)
10882 {
10883 case DT_FLAGS:
10884 if (do_dynamic)
10885 print_dynamic_flags (entry->d_un.d_val);
10886 break;
10887
10888 case DT_AUXILIARY:
10889 case DT_FILTER:
10890 case DT_CONFIG:
10891 case DT_DEPAUDIT:
10892 case DT_AUDIT:
10893 if (do_dynamic)
10894 {
10895 switch (entry->d_tag)
10896 {
10897 case DT_AUXILIARY:
10898 printf (_("Auxiliary library"));
10899 break;
10900
10901 case DT_FILTER:
10902 printf (_("Filter library"));
10903 break;
10904
10905 case DT_CONFIG:
10906 printf (_("Configuration file"));
10907 break;
10908
10909 case DT_DEPAUDIT:
10910 printf (_("Dependency audit library"));
10911 break;
10912
10913 case DT_AUDIT:
10914 printf (_("Audit library"));
10915 break;
10916 }
10917
10918 if (VALID_DYNAMIC_NAME (filedata, entry->d_un.d_val))
10919 printf (": [%s]\n",
10920 GET_DYNAMIC_NAME (filedata, entry->d_un.d_val));
10921 else
10922 {
10923 printf (": ");
10924 print_vma (entry->d_un.d_val, PREFIX_HEX);
10925 putchar ('\n');
10926 }
10927 }
10928 break;
10929
10930 case DT_FEATURE:
10931 if (do_dynamic)
10932 {
10933 printf (_("Flags:"));
10934
10935 if (entry->d_un.d_val == 0)
10936 printf (_(" None\n"));
10937 else
10938 {
10939 unsigned long int val = entry->d_un.d_val;
10940
10941 if (val & DTF_1_PARINIT)
10942 {
10943 printf (" PARINIT");
10944 val ^= DTF_1_PARINIT;
10945 }
10946 if (val & DTF_1_CONFEXP)
10947 {
10948 printf (" CONFEXP");
10949 val ^= DTF_1_CONFEXP;
10950 }
10951 if (val != 0)
10952 printf (" %lx", val);
10953 puts ("");
10954 }
10955 }
10956 break;
10957
10958 case DT_POSFLAG_1:
10959 if (do_dynamic)
10960 {
10961 printf (_("Flags:"));
10962
10963 if (entry->d_un.d_val == 0)
10964 printf (_(" None\n"));
10965 else
10966 {
10967 unsigned long int val = entry->d_un.d_val;
10968
10969 if (val & DF_P1_LAZYLOAD)
10970 {
10971 printf (" LAZYLOAD");
10972 val ^= DF_P1_LAZYLOAD;
10973 }
10974 if (val & DF_P1_GROUPPERM)
10975 {
10976 printf (" GROUPPERM");
10977 val ^= DF_P1_GROUPPERM;
10978 }
10979 if (val != 0)
10980 printf (" %lx", val);
10981 puts ("");
10982 }
10983 }
10984 break;
10985
10986 case DT_FLAGS_1:
10987 if (do_dynamic)
10988 {
10989 printf (_("Flags:"));
10990 if (entry->d_un.d_val == 0)
10991 printf (_(" None\n"));
10992 else
10993 {
10994 unsigned long int val = entry->d_un.d_val;
10995
10996 if (val & DF_1_NOW)
10997 {
10998 printf (" NOW");
10999 val ^= DF_1_NOW;
11000 }
11001 if (val & DF_1_GLOBAL)
11002 {
11003 printf (" GLOBAL");
11004 val ^= DF_1_GLOBAL;
11005 }
11006 if (val & DF_1_GROUP)
11007 {
11008 printf (" GROUP");
11009 val ^= DF_1_GROUP;
11010 }
11011 if (val & DF_1_NODELETE)
11012 {
11013 printf (" NODELETE");
11014 val ^= DF_1_NODELETE;
11015 }
11016 if (val & DF_1_LOADFLTR)
11017 {
11018 printf (" LOADFLTR");
11019 val ^= DF_1_LOADFLTR;
11020 }
11021 if (val & DF_1_INITFIRST)
11022 {
11023 printf (" INITFIRST");
11024 val ^= DF_1_INITFIRST;
11025 }
11026 if (val & DF_1_NOOPEN)
11027 {
11028 printf (" NOOPEN");
11029 val ^= DF_1_NOOPEN;
11030 }
11031 if (val & DF_1_ORIGIN)
11032 {
11033 printf (" ORIGIN");
11034 val ^= DF_1_ORIGIN;
11035 }
11036 if (val & DF_1_DIRECT)
11037 {
11038 printf (" DIRECT");
11039 val ^= DF_1_DIRECT;
11040 }
11041 if (val & DF_1_TRANS)
11042 {
11043 printf (" TRANS");
11044 val ^= DF_1_TRANS;
11045 }
11046 if (val & DF_1_INTERPOSE)
11047 {
11048 printf (" INTERPOSE");
11049 val ^= DF_1_INTERPOSE;
11050 }
11051 if (val & DF_1_NODEFLIB)
11052 {
11053 printf (" NODEFLIB");
11054 val ^= DF_1_NODEFLIB;
11055 }
11056 if (val & DF_1_NODUMP)
11057 {
11058 printf (" NODUMP");
11059 val ^= DF_1_NODUMP;
11060 }
11061 if (val & DF_1_CONFALT)
11062 {
11063 printf (" CONFALT");
11064 val ^= DF_1_CONFALT;
11065 }
11066 if (val & DF_1_ENDFILTEE)
11067 {
11068 printf (" ENDFILTEE");
11069 val ^= DF_1_ENDFILTEE;
11070 }
11071 if (val & DF_1_DISPRELDNE)
11072 {
11073 printf (" DISPRELDNE");
11074 val ^= DF_1_DISPRELDNE;
11075 }
11076 if (val & DF_1_DISPRELPND)
11077 {
11078 printf (" DISPRELPND");
11079 val ^= DF_1_DISPRELPND;
11080 }
11081 if (val & DF_1_NODIRECT)
11082 {
11083 printf (" NODIRECT");
11084 val ^= DF_1_NODIRECT;
11085 }
11086 if (val & DF_1_IGNMULDEF)
11087 {
11088 printf (" IGNMULDEF");
11089 val ^= DF_1_IGNMULDEF;
11090 }
11091 if (val & DF_1_NOKSYMS)
11092 {
11093 printf (" NOKSYMS");
11094 val ^= DF_1_NOKSYMS;
11095 }
11096 if (val & DF_1_NOHDR)
11097 {
11098 printf (" NOHDR");
11099 val ^= DF_1_NOHDR;
11100 }
11101 if (val & DF_1_EDITED)
11102 {
11103 printf (" EDITED");
11104 val ^= DF_1_EDITED;
11105 }
11106 if (val & DF_1_NORELOC)
11107 {
11108 printf (" NORELOC");
11109 val ^= DF_1_NORELOC;
11110 }
11111 if (val & DF_1_SYMINTPOSE)
11112 {
11113 printf (" SYMINTPOSE");
11114 val ^= DF_1_SYMINTPOSE;
11115 }
11116 if (val & DF_1_GLOBAUDIT)
11117 {
11118 printf (" GLOBAUDIT");
11119 val ^= DF_1_GLOBAUDIT;
11120 }
11121 if (val & DF_1_SINGLETON)
11122 {
11123 printf (" SINGLETON");
11124 val ^= DF_1_SINGLETON;
11125 }
11126 if (val & DF_1_STUB)
11127 {
11128 printf (" STUB");
11129 val ^= DF_1_STUB;
11130 }
11131 if (val & DF_1_PIE)
11132 {
11133 printf (" PIE");
11134 val ^= DF_1_PIE;
11135 }
11136 if (val & DF_1_KMOD)
11137 {
11138 printf (" KMOD");
11139 val ^= DF_1_KMOD;
11140 }
11141 if (val & DF_1_WEAKFILTER)
11142 {
11143 printf (" WEAKFILTER");
11144 val ^= DF_1_WEAKFILTER;
11145 }
11146 if (val & DF_1_NOCOMMON)
11147 {
11148 printf (" NOCOMMON");
11149 val ^= DF_1_NOCOMMON;
11150 }
11151 if (val != 0)
11152 printf (" %lx", val);
11153 puts ("");
11154 }
11155 }
11156 break;
11157
11158 case DT_PLTREL:
11159 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11160 if (do_dynamic)
11161 puts (get_dynamic_type (filedata, entry->d_un.d_val));
11162 break;
11163
11164 case DT_NULL :
11165 case DT_NEEDED :
11166 case DT_PLTGOT :
11167 case DT_HASH :
11168 case DT_STRTAB :
11169 case DT_SYMTAB :
11170 case DT_RELA :
11171 case DT_INIT :
11172 case DT_FINI :
11173 case DT_SONAME :
11174 case DT_RPATH :
11175 case DT_SYMBOLIC:
11176 case DT_REL :
11177 case DT_DEBUG :
11178 case DT_TEXTREL :
11179 case DT_JMPREL :
11180 case DT_RUNPATH :
11181 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11182
11183 if (do_dynamic)
11184 {
11185 char * name;
11186
11187 if (VALID_DYNAMIC_NAME (filedata, entry->d_un.d_val))
11188 name = GET_DYNAMIC_NAME (filedata, entry->d_un.d_val);
11189 else
11190 name = NULL;
11191
11192 if (name)
11193 {
11194 switch (entry->d_tag)
11195 {
11196 case DT_NEEDED:
11197 printf (_("Shared library: [%s]"), name);
11198
11199 if (filedata->program_interpreter
11200 && streq (name, filedata->program_interpreter))
11201 printf (_(" program interpreter"));
11202 break;
11203
11204 case DT_SONAME:
11205 printf (_("Library soname: [%s]"), name);
11206 break;
11207
11208 case DT_RPATH:
11209 printf (_("Library rpath: [%s]"), name);
11210 break;
11211
11212 case DT_RUNPATH:
11213 printf (_("Library runpath: [%s]"), name);
11214 break;
11215
11216 default:
11217 print_vma (entry->d_un.d_val, PREFIX_HEX);
11218 break;
11219 }
11220 }
11221 else
11222 print_vma (entry->d_un.d_val, PREFIX_HEX);
11223
11224 putchar ('\n');
11225 }
11226 break;
11227
11228 case DT_PLTRELSZ:
11229 case DT_RELASZ :
11230 case DT_STRSZ :
11231 case DT_RELSZ :
11232 case DT_RELAENT :
11233 case DT_SYMENT :
11234 case DT_RELENT :
11235 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11236 /* Fall through. */
11237 case DT_PLTPADSZ:
11238 case DT_MOVEENT :
11239 case DT_MOVESZ :
11240 case DT_INIT_ARRAYSZ:
11241 case DT_FINI_ARRAYSZ:
11242 case DT_GNU_CONFLICTSZ:
11243 case DT_GNU_LIBLISTSZ:
11244 if (do_dynamic)
11245 {
11246 print_vma (entry->d_un.d_val, UNSIGNED);
11247 printf (_(" (bytes)\n"));
11248 }
11249 break;
11250
11251 case DT_VERDEFNUM:
11252 case DT_VERNEEDNUM:
11253 case DT_RELACOUNT:
11254 case DT_RELCOUNT:
11255 if (do_dynamic)
11256 {
11257 print_vma (entry->d_un.d_val, UNSIGNED);
11258 putchar ('\n');
11259 }
11260 break;
11261
11262 case DT_SYMINSZ:
11263 case DT_SYMINENT:
11264 case DT_SYMINFO:
11265 case DT_USED:
11266 case DT_INIT_ARRAY:
11267 case DT_FINI_ARRAY:
11268 if (do_dynamic)
11269 {
11270 if (entry->d_tag == DT_USED
11271 && VALID_DYNAMIC_NAME (filedata, entry->d_un.d_val))
11272 {
11273 char * name = GET_DYNAMIC_NAME (filedata, entry->d_un.d_val);
11274
11275 if (*name)
11276 {
11277 printf (_("Not needed object: [%s]\n"), name);
11278 break;
11279 }
11280 }
11281
11282 print_vma (entry->d_un.d_val, PREFIX_HEX);
11283 putchar ('\n');
11284 }
11285 break;
11286
11287 case DT_BIND_NOW:
11288 /* The value of this entry is ignored. */
11289 if (do_dynamic)
11290 putchar ('\n');
11291 break;
11292
11293 case DT_GNU_PRELINKED:
11294 if (do_dynamic)
11295 {
11296 struct tm * tmp;
11297 time_t atime = entry->d_un.d_val;
11298
11299 tmp = gmtime (&atime);
11300 /* PR 17533 file: 041-1244816-0.004. */
11301 if (tmp == NULL)
11302 printf (_("<corrupt time val: %lx"),
11303 (unsigned long) atime);
11304 else
11305 printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
11306 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
11307 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
11308
11309 }
11310 break;
11311
11312 case DT_GNU_HASH:
11313 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11314 if (do_dynamic)
11315 {
11316 print_vma (entry->d_un.d_val, PREFIX_HEX);
11317 putchar ('\n');
11318 }
11319 break;
11320
11321 case DT_GNU_FLAGS_1:
11322 if (do_dynamic)
11323 {
11324 printf (_("Flags:"));
11325 if (entry->d_un.d_val == 0)
11326 printf (_(" None\n"));
11327 else
11328 {
11329 unsigned long int val = entry->d_un.d_val;
11330
11331 if (val & DF_GNU_1_UNIQUE)
11332 {
11333 printf (" UNIQUE");
11334 val ^= DF_GNU_1_UNIQUE;
11335 }
11336 if (val != 0)
11337 printf (" %lx", val);
11338 puts ("");
11339 }
11340 }
11341 break;
11342
11343 default:
11344 if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
11345 filedata->version_info[DT_VERSIONTAGIDX (entry->d_tag)]
11346 = entry->d_un.d_val;
11347
11348 if (do_dynamic)
11349 {
11350 switch (filedata->file_header.e_machine)
11351 {
11352 case EM_AARCH64:
11353 dynamic_section_aarch64_val (entry);
11354 break;
11355 case EM_MIPS:
11356 case EM_MIPS_RS3_LE:
11357 dynamic_section_mips_val (filedata, entry);
11358 break;
11359 case EM_PARISC:
11360 dynamic_section_parisc_val (entry);
11361 break;
11362 case EM_IA_64:
11363 dynamic_section_ia64_val (entry);
11364 break;
11365 default:
11366 print_vma (entry->d_un.d_val, PREFIX_HEX);
11367 putchar ('\n');
11368 }
11369 }
11370 break;
11371 }
11372 }
11373
11374 return true;
11375 }
11376
11377 static char *
11378 get_ver_flags (unsigned int flags)
11379 {
11380 static char buff[128];
11381
11382 buff[0] = 0;
11383
11384 if (flags == 0)
11385 return _("none");
11386
11387 if (flags & VER_FLG_BASE)
11388 strcat (buff, "BASE");
11389
11390 if (flags & VER_FLG_WEAK)
11391 {
11392 if (flags & VER_FLG_BASE)
11393 strcat (buff, " | ");
11394
11395 strcat (buff, "WEAK");
11396 }
11397
11398 if (flags & VER_FLG_INFO)
11399 {
11400 if (flags & (VER_FLG_BASE|VER_FLG_WEAK))
11401 strcat (buff, " | ");
11402
11403 strcat (buff, "INFO");
11404 }
11405
11406 if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
11407 {
11408 if (flags & (VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
11409 strcat (buff, " | ");
11410
11411 strcat (buff, _("<unknown>"));
11412 }
11413
11414 return buff;
11415 }
11416
11417 /* Display the contents of the version sections. */
11418
11419 static bool
11420 process_version_sections (Filedata * filedata)
11421 {
11422 Elf_Internal_Shdr * section;
11423 unsigned i;
11424 bool found = false;
11425
11426 if (! do_version)
11427 return true;
11428
11429 for (i = 0, section = filedata->section_headers;
11430 i < filedata->file_header.e_shnum;
11431 i++, section++)
11432 {
11433 switch (section->sh_type)
11434 {
11435 case SHT_GNU_verdef:
11436 {
11437 Elf_External_Verdef * edefs;
11438 unsigned long idx;
11439 unsigned long cnt;
11440 char * endbuf;
11441
11442 found = true;
11443
11444 if (filedata->is_separate)
11445 printf (ngettext ("\nIn linked file '%s' the version definition section '%s' contains %u entry:\n",
11446 "\nIn linked file '%s' the version definition section '%s' contains %u entries:\n",
11447 section->sh_info),
11448 filedata->file_name,
11449 printable_section_name (filedata, section),
11450 section->sh_info);
11451 else
11452 printf (ngettext ("\nVersion definition section '%s' "
11453 "contains %u entry:\n",
11454 "\nVersion definition section '%s' "
11455 "contains %u entries:\n",
11456 section->sh_info),
11457 printable_section_name (filedata, section),
11458 section->sh_info);
11459
11460 printf (_(" Addr: 0x"));
11461 printf_vma (section->sh_addr);
11462 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
11463 (unsigned long) section->sh_offset, section->sh_link,
11464 printable_section_name_from_index (filedata, section->sh_link));
11465
11466 edefs = (Elf_External_Verdef *)
11467 get_data (NULL, filedata, section->sh_offset, 1,section->sh_size,
11468 _("version definition section"));
11469 if (!edefs)
11470 break;
11471 endbuf = (char *) edefs + section->sh_size;
11472
11473 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
11474 {
11475 char * vstart;
11476 Elf_External_Verdef * edef;
11477 Elf_Internal_Verdef ent;
11478 Elf_External_Verdaux * eaux;
11479 Elf_Internal_Verdaux aux;
11480 unsigned long isum;
11481 int j;
11482
11483 vstart = ((char *) edefs) + idx;
11484 if (vstart + sizeof (*edef) > endbuf)
11485 break;
11486
11487 edef = (Elf_External_Verdef *) vstart;
11488
11489 ent.vd_version = BYTE_GET (edef->vd_version);
11490 ent.vd_flags = BYTE_GET (edef->vd_flags);
11491 ent.vd_ndx = BYTE_GET (edef->vd_ndx);
11492 ent.vd_cnt = BYTE_GET (edef->vd_cnt);
11493 ent.vd_hash = BYTE_GET (edef->vd_hash);
11494 ent.vd_aux = BYTE_GET (edef->vd_aux);
11495 ent.vd_next = BYTE_GET (edef->vd_next);
11496
11497 printf (_(" %#06lx: Rev: %d Flags: %s"),
11498 idx, ent.vd_version, get_ver_flags (ent.vd_flags));
11499
11500 printf (_(" Index: %d Cnt: %d "),
11501 ent.vd_ndx, ent.vd_cnt);
11502
11503 /* Check for overflow. */
11504 if (ent.vd_aux > (size_t) (endbuf - vstart))
11505 break;
11506
11507 vstart += ent.vd_aux;
11508
11509 if (vstart + sizeof (*eaux) > endbuf)
11510 break;
11511 eaux = (Elf_External_Verdaux *) vstart;
11512
11513 aux.vda_name = BYTE_GET (eaux->vda_name);
11514 aux.vda_next = BYTE_GET (eaux->vda_next);
11515
11516 if (VALID_DYNAMIC_NAME (filedata, aux.vda_name))
11517 printf (_("Name: %s\n"),
11518 GET_DYNAMIC_NAME (filedata, aux.vda_name));
11519 else
11520 printf (_("Name index: %ld\n"), aux.vda_name);
11521
11522 isum = idx + ent.vd_aux;
11523
11524 for (j = 1; j < ent.vd_cnt; j++)
11525 {
11526 if (aux.vda_next < sizeof (*eaux)
11527 && !(j == ent.vd_cnt - 1 && aux.vda_next == 0))
11528 {
11529 warn (_("Invalid vda_next field of %lx\n"),
11530 aux.vda_next);
11531 j = ent.vd_cnt;
11532 break;
11533 }
11534 /* Check for overflow. */
11535 if (aux.vda_next > (size_t) (endbuf - vstart))
11536 break;
11537
11538 isum += aux.vda_next;
11539 vstart += aux.vda_next;
11540
11541 if (vstart + sizeof (*eaux) > endbuf)
11542 break;
11543 eaux = (Elf_External_Verdaux *) vstart;
11544
11545 aux.vda_name = BYTE_GET (eaux->vda_name);
11546 aux.vda_next = BYTE_GET (eaux->vda_next);
11547
11548 if (VALID_DYNAMIC_NAME (filedata, aux.vda_name))
11549 printf (_(" %#06lx: Parent %d: %s\n"),
11550 isum, j,
11551 GET_DYNAMIC_NAME (filedata, aux.vda_name));
11552 else
11553 printf (_(" %#06lx: Parent %d, name index: %ld\n"),
11554 isum, j, aux.vda_name);
11555 }
11556
11557 if (j < ent.vd_cnt)
11558 printf (_(" Version def aux past end of section\n"));
11559
11560 /* PR 17531:
11561 file: id:000001,src:000172+005151,op:splice,rep:2. */
11562 if (ent.vd_next < sizeof (*edef)
11563 && !(cnt == section->sh_info - 1 && ent.vd_next == 0))
11564 {
11565 warn (_("Invalid vd_next field of %lx\n"), ent.vd_next);
11566 cnt = section->sh_info;
11567 break;
11568 }
11569 if (ent.vd_next > (size_t) (endbuf - ((char *) edefs + idx)))
11570 break;
11571
11572 idx += ent.vd_next;
11573 }
11574
11575 if (cnt < section->sh_info)
11576 printf (_(" Version definition past end of section\n"));
11577
11578 free (edefs);
11579 }
11580 break;
11581
11582 case SHT_GNU_verneed:
11583 {
11584 Elf_External_Verneed * eneed;
11585 unsigned long idx;
11586 unsigned long cnt;
11587 char * endbuf;
11588
11589 found = true;
11590
11591 if (filedata->is_separate)
11592 printf (ngettext ("\nIn linked file '%s' the version needs section '%s' contains %u entry:\n",
11593 "\nIn linked file '%s' the version needs section '%s' contains %u entries:\n",
11594 section->sh_info),
11595 filedata->file_name,
11596 printable_section_name (filedata, section),
11597 section->sh_info);
11598 else
11599 printf (ngettext ("\nVersion needs section '%s' "
11600 "contains %u entry:\n",
11601 "\nVersion needs section '%s' "
11602 "contains %u entries:\n",
11603 section->sh_info),
11604 printable_section_name (filedata, section),
11605 section->sh_info);
11606
11607 printf (_(" Addr: 0x"));
11608 printf_vma (section->sh_addr);
11609 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
11610 (unsigned long) section->sh_offset, section->sh_link,
11611 printable_section_name_from_index (filedata, section->sh_link));
11612
11613 eneed = (Elf_External_Verneed *) get_data (NULL, filedata,
11614 section->sh_offset, 1,
11615 section->sh_size,
11616 _("Version Needs section"));
11617 if (!eneed)
11618 break;
11619 endbuf = (char *) eneed + section->sh_size;
11620
11621 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
11622 {
11623 Elf_External_Verneed * entry;
11624 Elf_Internal_Verneed ent;
11625 unsigned long isum;
11626 int j;
11627 char * vstart;
11628
11629 vstart = ((char *) eneed) + idx;
11630 if (vstart + sizeof (*entry) > endbuf)
11631 break;
11632
11633 entry = (Elf_External_Verneed *) vstart;
11634
11635 ent.vn_version = BYTE_GET (entry->vn_version);
11636 ent.vn_cnt = BYTE_GET (entry->vn_cnt);
11637 ent.vn_file = BYTE_GET (entry->vn_file);
11638 ent.vn_aux = BYTE_GET (entry->vn_aux);
11639 ent.vn_next = BYTE_GET (entry->vn_next);
11640
11641 printf (_(" %#06lx: Version: %d"), idx, ent.vn_version);
11642
11643 if (VALID_DYNAMIC_NAME (filedata, ent.vn_file))
11644 printf (_(" File: %s"),
11645 GET_DYNAMIC_NAME (filedata, ent.vn_file));
11646 else
11647 printf (_(" File: %lx"), ent.vn_file);
11648
11649 printf (_(" Cnt: %d\n"), ent.vn_cnt);
11650
11651 /* Check for overflow. */
11652 if (ent.vn_aux > (size_t) (endbuf - vstart))
11653 break;
11654 vstart += ent.vn_aux;
11655
11656 for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
11657 {
11658 Elf_External_Vernaux * eaux;
11659 Elf_Internal_Vernaux aux;
11660
11661 if (vstart + sizeof (*eaux) > endbuf)
11662 break;
11663 eaux = (Elf_External_Vernaux *) vstart;
11664
11665 aux.vna_hash = BYTE_GET (eaux->vna_hash);
11666 aux.vna_flags = BYTE_GET (eaux->vna_flags);
11667 aux.vna_other = BYTE_GET (eaux->vna_other);
11668 aux.vna_name = BYTE_GET (eaux->vna_name);
11669 aux.vna_next = BYTE_GET (eaux->vna_next);
11670
11671 if (VALID_DYNAMIC_NAME (filedata, aux.vna_name))
11672 printf (_(" %#06lx: Name: %s"),
11673 isum, GET_DYNAMIC_NAME (filedata, aux.vna_name));
11674 else
11675 printf (_(" %#06lx: Name index: %lx"),
11676 isum, aux.vna_name);
11677
11678 printf (_(" Flags: %s Version: %d\n"),
11679 get_ver_flags (aux.vna_flags), aux.vna_other);
11680
11681 if (aux.vna_next < sizeof (*eaux)
11682 && !(j == ent.vn_cnt - 1 && aux.vna_next == 0))
11683 {
11684 warn (_("Invalid vna_next field of %lx\n"),
11685 aux.vna_next);
11686 j = ent.vn_cnt;
11687 break;
11688 }
11689 /* Check for overflow. */
11690 if (aux.vna_next > (size_t) (endbuf - vstart))
11691 break;
11692 isum += aux.vna_next;
11693 vstart += aux.vna_next;
11694 }
11695
11696 if (j < ent.vn_cnt)
11697 warn (_("Missing Version Needs auxiliary information\n"));
11698
11699 if (ent.vn_next < sizeof (*entry)
11700 && !(cnt == section->sh_info - 1 && ent.vn_next == 0))
11701 {
11702 warn (_("Invalid vn_next field of %lx\n"), ent.vn_next);
11703 cnt = section->sh_info;
11704 break;
11705 }
11706 if (ent.vn_next > (size_t) (endbuf - ((char *) eneed + idx)))
11707 break;
11708 idx += ent.vn_next;
11709 }
11710
11711 if (cnt < section->sh_info)
11712 warn (_("Missing Version Needs information\n"));
11713
11714 free (eneed);
11715 }
11716 break;
11717
11718 case SHT_GNU_versym:
11719 {
11720 Elf_Internal_Shdr * link_section;
11721 size_t total;
11722 unsigned int cnt;
11723 unsigned char * edata;
11724 unsigned short * data;
11725 char * strtab;
11726 Elf_Internal_Sym * symbols;
11727 Elf_Internal_Shdr * string_sec;
11728 unsigned long num_syms;
11729 long off;
11730
11731 if (section->sh_link >= filedata->file_header.e_shnum)
11732 break;
11733
11734 link_section = filedata->section_headers + section->sh_link;
11735 total = section->sh_size / sizeof (Elf_External_Versym);
11736
11737 if (link_section->sh_link >= filedata->file_header.e_shnum)
11738 break;
11739
11740 found = true;
11741
11742 symbols = get_elf_symbols (filedata, link_section, & num_syms);
11743 if (symbols == NULL)
11744 break;
11745
11746 string_sec = filedata->section_headers + link_section->sh_link;
11747
11748 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
11749 string_sec->sh_size,
11750 _("version string table"));
11751 if (!strtab)
11752 {
11753 free (symbols);
11754 break;
11755 }
11756
11757 if (filedata->is_separate)
11758 printf (ngettext ("\nIn linked file '%s' the version symbols section '%s' contains %lu entry:\n",
11759 "\nIn linked file '%s' the version symbols section '%s' contains %lu entries:\n",
11760 total),
11761 filedata->file_name,
11762 printable_section_name (filedata, section),
11763 (unsigned long) total);
11764 else
11765 printf (ngettext ("\nVersion symbols section '%s' "
11766 "contains %lu entry:\n",
11767 "\nVersion symbols section '%s' "
11768 "contains %lu entries:\n",
11769 total),
11770 printable_section_name (filedata, section),
11771 (unsigned long) total);
11772
11773 printf (_(" Addr: 0x"));
11774 printf_vma (section->sh_addr);
11775 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
11776 (unsigned long) section->sh_offset, section->sh_link,
11777 printable_section_name (filedata, link_section));
11778
11779 off = offset_from_vma (filedata,
11780 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
11781 total * sizeof (short));
11782 edata = (unsigned char *) get_data (NULL, filedata, off,
11783 sizeof (short), total,
11784 _("version symbol data"));
11785 if (!edata)
11786 {
11787 free (strtab);
11788 free (symbols);
11789 break;
11790 }
11791
11792 data = (short unsigned int *) cmalloc (total, sizeof (short));
11793
11794 for (cnt = total; cnt --;)
11795 data[cnt] = byte_get (edata + cnt * sizeof (short),
11796 sizeof (short));
11797
11798 free (edata);
11799
11800 for (cnt = 0; cnt < total; cnt += 4)
11801 {
11802 int j, nn;
11803 char *name;
11804 char *invalid = _("*invalid*");
11805
11806 printf (" %03x:", cnt);
11807
11808 for (j = 0; (j < 4) && (cnt + j) < total; ++j)
11809 switch (data[cnt + j])
11810 {
11811 case 0:
11812 fputs (_(" 0 (*local*) "), stdout);
11813 break;
11814
11815 case 1:
11816 fputs (_(" 1 (*global*) "), stdout);
11817 break;
11818
11819 default:
11820 nn = printf ("%4x%c", data[cnt + j] & VERSYM_VERSION,
11821 data[cnt + j] & VERSYM_HIDDEN ? 'h' : ' ');
11822
11823 /* If this index value is greater than the size of the symbols
11824 array, break to avoid an out-of-bounds read. */
11825 if ((unsigned long)(cnt + j) >= num_syms)
11826 {
11827 warn (_("invalid index into symbol array\n"));
11828 break;
11829 }
11830
11831 name = NULL;
11832 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
11833 {
11834 Elf_Internal_Verneed ivn;
11835 unsigned long offset;
11836
11837 offset = offset_from_vma
11838 (filedata,
11839 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
11840 sizeof (Elf_External_Verneed));
11841
11842 do
11843 {
11844 Elf_Internal_Vernaux ivna;
11845 Elf_External_Verneed evn;
11846 Elf_External_Vernaux evna;
11847 unsigned long a_off;
11848
11849 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
11850 _("version need")) == NULL)
11851 break;
11852
11853 ivn.vn_aux = BYTE_GET (evn.vn_aux);
11854 ivn.vn_next = BYTE_GET (evn.vn_next);
11855
11856 a_off = offset + ivn.vn_aux;
11857
11858 do
11859 {
11860 if (get_data (&evna, filedata, a_off, sizeof (evna),
11861 1, _("version need aux (2)")) == NULL)
11862 {
11863 ivna.vna_next = 0;
11864 ivna.vna_other = 0;
11865 }
11866 else
11867 {
11868 ivna.vna_next = BYTE_GET (evna.vna_next);
11869 ivna.vna_other = BYTE_GET (evna.vna_other);
11870 }
11871
11872 a_off += ivna.vna_next;
11873 }
11874 while (ivna.vna_other != data[cnt + j]
11875 && ivna.vna_next != 0);
11876
11877 if (ivna.vna_other == data[cnt + j])
11878 {
11879 ivna.vna_name = BYTE_GET (evna.vna_name);
11880
11881 if (ivna.vna_name >= string_sec->sh_size)
11882 name = invalid;
11883 else
11884 name = strtab + ivna.vna_name;
11885 break;
11886 }
11887
11888 offset += ivn.vn_next;
11889 }
11890 while (ivn.vn_next);
11891 }
11892
11893 if (data[cnt + j] != 0x8001
11894 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
11895 {
11896 Elf_Internal_Verdef ivd;
11897 Elf_External_Verdef evd;
11898 unsigned long offset;
11899
11900 offset = offset_from_vma
11901 (filedata,
11902 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
11903 sizeof evd);
11904
11905 do
11906 {
11907 if (get_data (&evd, filedata, offset, sizeof (evd), 1,
11908 _("version def")) == NULL)
11909 {
11910 ivd.vd_next = 0;
11911 /* PR 17531: file: 046-1082287-0.004. */
11912 ivd.vd_ndx = (data[cnt + j] & VERSYM_VERSION) + 1;
11913 break;
11914 }
11915 else
11916 {
11917 ivd.vd_next = BYTE_GET (evd.vd_next);
11918 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
11919 }
11920
11921 offset += ivd.vd_next;
11922 }
11923 while (ivd.vd_ndx != (data[cnt + j] & VERSYM_VERSION)
11924 && ivd.vd_next != 0);
11925
11926 if (ivd.vd_ndx == (data[cnt + j] & VERSYM_VERSION))
11927 {
11928 Elf_External_Verdaux evda;
11929 Elf_Internal_Verdaux ivda;
11930
11931 ivd.vd_aux = BYTE_GET (evd.vd_aux);
11932
11933 if (get_data (&evda, filedata,
11934 offset - ivd.vd_next + ivd.vd_aux,
11935 sizeof (evda), 1,
11936 _("version def aux")) == NULL)
11937 break;
11938
11939 ivda.vda_name = BYTE_GET (evda.vda_name);
11940
11941 if (ivda.vda_name >= string_sec->sh_size)
11942 name = invalid;
11943 else if (name != NULL && name != invalid)
11944 name = _("*both*");
11945 else
11946 name = strtab + ivda.vda_name;
11947 }
11948 }
11949 if (name != NULL)
11950 nn += printf ("(%s%-*s",
11951 name,
11952 12 - (int) strlen (name),
11953 ")");
11954
11955 if (nn < 18)
11956 printf ("%*c", 18 - nn, ' ');
11957 }
11958
11959 putchar ('\n');
11960 }
11961
11962 free (data);
11963 free (strtab);
11964 free (symbols);
11965 }
11966 break;
11967
11968 default:
11969 break;
11970 }
11971 }
11972
11973 if (! found)
11974 {
11975 if (filedata->is_separate)
11976 printf (_("\nNo version information found in linked file '%s'.\n"),
11977 filedata->file_name);
11978 else
11979 printf (_("\nNo version information found in this file.\n"));
11980 }
11981
11982 return true;
11983 }
11984
11985 static const char *
11986 get_symbol_binding (Filedata * filedata, unsigned int binding)
11987 {
11988 static char buff[64];
11989
11990 switch (binding)
11991 {
11992 case STB_LOCAL: return "LOCAL";
11993 case STB_GLOBAL: return "GLOBAL";
11994 case STB_WEAK: return "WEAK";
11995 default:
11996 if (binding >= STB_LOPROC && binding <= STB_HIPROC)
11997 snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
11998 binding);
11999 else if (binding >= STB_LOOS && binding <= STB_HIOS)
12000 {
12001 if (binding == STB_GNU_UNIQUE
12002 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU)
12003 return "UNIQUE";
12004 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
12005 }
12006 else
12007 snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
12008 return buff;
12009 }
12010 }
12011
12012 static const char *
12013 get_symbol_type (Filedata * filedata, unsigned int type)
12014 {
12015 static char buff[64];
12016
12017 switch (type)
12018 {
12019 case STT_NOTYPE: return "NOTYPE";
12020 case STT_OBJECT: return "OBJECT";
12021 case STT_FUNC: return "FUNC";
12022 case STT_SECTION: return "SECTION";
12023 case STT_FILE: return "FILE";
12024 case STT_COMMON: return "COMMON";
12025 case STT_TLS: return "TLS";
12026 case STT_RELC: return "RELC";
12027 case STT_SRELC: return "SRELC";
12028 default:
12029 if (type >= STT_LOPROC && type <= STT_HIPROC)
12030 {
12031 if (filedata->file_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
12032 return "THUMB_FUNC";
12033
12034 if (filedata->file_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
12035 return "REGISTER";
12036
12037 if (filedata->file_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
12038 return "PARISC_MILLI";
12039
12040 snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
12041 }
12042 else if (type >= STT_LOOS && type <= STT_HIOS)
12043 {
12044 if (filedata->file_header.e_machine == EM_PARISC)
12045 {
12046 if (type == STT_HP_OPAQUE)
12047 return "HP_OPAQUE";
12048 if (type == STT_HP_STUB)
12049 return "HP_STUB";
12050 }
12051
12052 if (type == STT_GNU_IFUNC
12053 && (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU
12054 || filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_FREEBSD))
12055 return "IFUNC";
12056
12057 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
12058 }
12059 else
12060 snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
12061 return buff;
12062 }
12063 }
12064
12065 static const char *
12066 get_symbol_visibility (unsigned int visibility)
12067 {
12068 switch (visibility)
12069 {
12070 case STV_DEFAULT: return "DEFAULT";
12071 case STV_INTERNAL: return "INTERNAL";
12072 case STV_HIDDEN: return "HIDDEN";
12073 case STV_PROTECTED: return "PROTECTED";
12074 default:
12075 error (_("Unrecognized visibility value: %u\n"), visibility);
12076 return _("<unknown>");
12077 }
12078 }
12079
12080 static const char *
12081 get_alpha_symbol_other (unsigned int other)
12082 {
12083 switch (other)
12084 {
12085 case STO_ALPHA_NOPV: return "NOPV";
12086 case STO_ALPHA_STD_GPLOAD: return "STD GPLOAD";
12087 default:
12088 error (_("Unrecognized alpha specific other value: %u\n"), other);
12089 return _("<unknown>");
12090 }
12091 }
12092
12093 static const char *
12094 get_solaris_symbol_visibility (unsigned int visibility)
12095 {
12096 switch (visibility)
12097 {
12098 case 4: return "EXPORTED";
12099 case 5: return "SINGLETON";
12100 case 6: return "ELIMINATE";
12101 default: return get_symbol_visibility (visibility);
12102 }
12103 }
12104
12105 static const char *
12106 get_aarch64_symbol_other (unsigned int other)
12107 {
12108 static char buf[32];
12109
12110 if (other & STO_AARCH64_VARIANT_PCS)
12111 {
12112 other &= ~STO_AARCH64_VARIANT_PCS;
12113 if (other == 0)
12114 return "VARIANT_PCS";
12115 snprintf (buf, sizeof buf, "VARIANT_PCS | %x", other);
12116 return buf;
12117 }
12118 return NULL;
12119 }
12120
12121 static const char *
12122 get_mips_symbol_other (unsigned int other)
12123 {
12124 switch (other)
12125 {
12126 case STO_OPTIONAL: return "OPTIONAL";
12127 case STO_MIPS_PLT: return "MIPS PLT";
12128 case STO_MIPS_PIC: return "MIPS PIC";
12129 case STO_MICROMIPS: return "MICROMIPS";
12130 case STO_MICROMIPS | STO_MIPS_PIC: return "MICROMIPS, MIPS PIC";
12131 case STO_MIPS16: return "MIPS16";
12132 default: return NULL;
12133 }
12134 }
12135
12136 static const char *
12137 get_ia64_symbol_other (Filedata * filedata, unsigned int other)
12138 {
12139 if (is_ia64_vms (filedata))
12140 {
12141 static char res[32];
12142
12143 res[0] = 0;
12144
12145 /* Function types is for images and .STB files only. */
12146 switch (filedata->file_header.e_type)
12147 {
12148 case ET_DYN:
12149 case ET_EXEC:
12150 switch (VMS_ST_FUNC_TYPE (other))
12151 {
12152 case VMS_SFT_CODE_ADDR:
12153 strcat (res, " CA");
12154 break;
12155 case VMS_SFT_SYMV_IDX:
12156 strcat (res, " VEC");
12157 break;
12158 case VMS_SFT_FD:
12159 strcat (res, " FD");
12160 break;
12161 case VMS_SFT_RESERVE:
12162 strcat (res, " RSV");
12163 break;
12164 default:
12165 warn (_("Unrecognized IA64 VMS ST Function type: %d\n"),
12166 VMS_ST_FUNC_TYPE (other));
12167 strcat (res, " <unknown>");
12168 break;
12169 }
12170 break;
12171 default:
12172 break;
12173 }
12174 switch (VMS_ST_LINKAGE (other))
12175 {
12176 case VMS_STL_IGNORE:
12177 strcat (res, " IGN");
12178 break;
12179 case VMS_STL_RESERVE:
12180 strcat (res, " RSV");
12181 break;
12182 case VMS_STL_STD:
12183 strcat (res, " STD");
12184 break;
12185 case VMS_STL_LNK:
12186 strcat (res, " LNK");
12187 break;
12188 default:
12189 warn (_("Unrecognized IA64 VMS ST Linkage: %d\n"),
12190 VMS_ST_LINKAGE (other));
12191 strcat (res, " <unknown>");
12192 break;
12193 }
12194
12195 if (res[0] != 0)
12196 return res + 1;
12197 else
12198 return res;
12199 }
12200 return NULL;
12201 }
12202
12203 static const char *
12204 get_ppc64_symbol_other (unsigned int other)
12205 {
12206 if ((other & ~STO_PPC64_LOCAL_MASK) != 0)
12207 return NULL;
12208
12209 other >>= STO_PPC64_LOCAL_BIT;
12210 if (other <= 6)
12211 {
12212 static char buf[64];
12213 if (other >= 2)
12214 other = ppc64_decode_local_entry (other);
12215 snprintf (buf, sizeof buf, _("<localentry>: %d"), other);
12216 return buf;
12217 }
12218 return NULL;
12219 }
12220
12221 static const char *
12222 get_symbol_other (Filedata * filedata, unsigned int other)
12223 {
12224 const char * result = NULL;
12225 static char buff [64];
12226
12227 if (other == 0)
12228 return "";
12229
12230 switch (filedata->file_header.e_machine)
12231 {
12232 case EM_ALPHA:
12233 result = get_alpha_symbol_other (other);
12234 break;
12235 case EM_AARCH64:
12236 result = get_aarch64_symbol_other (other);
12237 break;
12238 case EM_MIPS:
12239 result = get_mips_symbol_other (other);
12240 break;
12241 case EM_IA_64:
12242 result = get_ia64_symbol_other (filedata, other);
12243 break;
12244 case EM_PPC64:
12245 result = get_ppc64_symbol_other (other);
12246 break;
12247 default:
12248 result = NULL;
12249 break;
12250 }
12251
12252 if (result)
12253 return result;
12254
12255 snprintf (buff, sizeof buff, _("<other>: %x"), other);
12256 return buff;
12257 }
12258
12259 static const char *
12260 get_symbol_index_type (Filedata * filedata, unsigned int type)
12261 {
12262 static char buff[32];
12263
12264 switch (type)
12265 {
12266 case SHN_UNDEF: return "UND";
12267 case SHN_ABS: return "ABS";
12268 case SHN_COMMON: return "COM";
12269 default:
12270 if (type == SHN_IA_64_ANSI_COMMON
12271 && filedata->file_header.e_machine == EM_IA_64
12272 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX)
12273 return "ANSI_COM";
12274 else if ((filedata->file_header.e_machine == EM_X86_64
12275 || filedata->file_header.e_machine == EM_L1OM
12276 || filedata->file_header.e_machine == EM_K1OM)
12277 && type == SHN_X86_64_LCOMMON)
12278 return "LARGE_COM";
12279 else if ((type == SHN_MIPS_SCOMMON
12280 && filedata->file_header.e_machine == EM_MIPS)
12281 || (type == SHN_TIC6X_SCOMMON
12282 && filedata->file_header.e_machine == EM_TI_C6000))
12283 return "SCOM";
12284 else if (type == SHN_MIPS_SUNDEFINED
12285 && filedata->file_header.e_machine == EM_MIPS)
12286 return "SUND";
12287 else if (type >= SHN_LOPROC && type <= SHN_HIPROC)
12288 sprintf (buff, "PRC[0x%04x]", type & 0xffff);
12289 else if (type >= SHN_LOOS && type <= SHN_HIOS)
12290 sprintf (buff, "OS [0x%04x]", type & 0xffff);
12291 else if (type >= SHN_LORESERVE)
12292 sprintf (buff, "RSV[0x%04x]", type & 0xffff);
12293 else if (filedata->file_header.e_shnum != 0
12294 && type >= filedata->file_header.e_shnum)
12295 sprintf (buff, _("bad section index[%3d]"), type);
12296 else
12297 sprintf (buff, "%3d", type);
12298 break;
12299 }
12300
12301 return buff;
12302 }
12303
12304 static const char *
12305 get_symbol_version_string (Filedata * filedata,
12306 bool is_dynsym,
12307 const char * strtab,
12308 unsigned long int strtab_size,
12309 unsigned int si,
12310 Elf_Internal_Sym * psym,
12311 enum versioned_symbol_info * sym_info,
12312 unsigned short * vna_other)
12313 {
12314 unsigned char data[2];
12315 unsigned short vers_data;
12316 unsigned long offset;
12317 unsigned short max_vd_ndx;
12318
12319 if (!is_dynsym
12320 || filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)] == 0)
12321 return NULL;
12322
12323 offset = offset_from_vma (filedata,
12324 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
12325 sizeof data + si * sizeof (vers_data));
12326
12327 if (get_data (&data, filedata, offset + si * sizeof (vers_data),
12328 sizeof (data), 1, _("version data")) == NULL)
12329 return NULL;
12330
12331 vers_data = byte_get (data, 2);
12332
12333 if ((vers_data & VERSYM_HIDDEN) == 0 && vers_data == 0)
12334 return NULL;
12335
12336 *sym_info = (vers_data & VERSYM_HIDDEN) != 0 ? symbol_hidden : symbol_public;
12337 max_vd_ndx = 0;
12338
12339 /* Usually we'd only see verdef for defined symbols, and verneed for
12340 undefined symbols. However, symbols defined by the linker in
12341 .dynbss for variables copied from a shared library in order to
12342 avoid text relocations are defined yet have verneed. We could
12343 use a heuristic to detect the special case, for example, check
12344 for verneed first on symbols defined in SHT_NOBITS sections, but
12345 it is simpler and more reliable to just look for both verdef and
12346 verneed. .dynbss might not be mapped to a SHT_NOBITS section. */
12347
12348 if (psym->st_shndx != SHN_UNDEF
12349 && vers_data != 0x8001
12350 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
12351 {
12352 Elf_Internal_Verdef ivd;
12353 Elf_Internal_Verdaux ivda;
12354 Elf_External_Verdaux evda;
12355 unsigned long off;
12356
12357 off = offset_from_vma (filedata,
12358 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
12359 sizeof (Elf_External_Verdef));
12360
12361 do
12362 {
12363 Elf_External_Verdef evd;
12364
12365 if (get_data (&evd, filedata, off, sizeof (evd), 1,
12366 _("version def")) == NULL)
12367 {
12368 ivd.vd_ndx = 0;
12369 ivd.vd_aux = 0;
12370 ivd.vd_next = 0;
12371 ivd.vd_flags = 0;
12372 }
12373 else
12374 {
12375 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
12376 ivd.vd_aux = BYTE_GET (evd.vd_aux);
12377 ivd.vd_next = BYTE_GET (evd.vd_next);
12378 ivd.vd_flags = BYTE_GET (evd.vd_flags);
12379 }
12380
12381 if ((ivd.vd_ndx & VERSYM_VERSION) > max_vd_ndx)
12382 max_vd_ndx = ivd.vd_ndx & VERSYM_VERSION;
12383
12384 off += ivd.vd_next;
12385 }
12386 while (ivd.vd_ndx != (vers_data & VERSYM_VERSION) && ivd.vd_next != 0);
12387
12388 if (ivd.vd_ndx == (vers_data & VERSYM_VERSION))
12389 {
12390 if (ivd.vd_ndx == 1 && ivd.vd_flags == VER_FLG_BASE)
12391 return NULL;
12392
12393 off -= ivd.vd_next;
12394 off += ivd.vd_aux;
12395
12396 if (get_data (&evda, filedata, off, sizeof (evda), 1,
12397 _("version def aux")) != NULL)
12398 {
12399 ivda.vda_name = BYTE_GET (evda.vda_name);
12400
12401 if (psym->st_name != ivda.vda_name)
12402 return (ivda.vda_name < strtab_size
12403 ? strtab + ivda.vda_name : _("<corrupt>"));
12404 }
12405 }
12406 }
12407
12408 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
12409 {
12410 Elf_External_Verneed evn;
12411 Elf_Internal_Verneed ivn;
12412 Elf_Internal_Vernaux ivna;
12413
12414 offset = offset_from_vma (filedata,
12415 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
12416 sizeof evn);
12417 do
12418 {
12419 unsigned long vna_off;
12420
12421 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
12422 _("version need")) == NULL)
12423 {
12424 ivna.vna_next = 0;
12425 ivna.vna_other = 0;
12426 ivna.vna_name = 0;
12427 break;
12428 }
12429
12430 ivn.vn_aux = BYTE_GET (evn.vn_aux);
12431 ivn.vn_next = BYTE_GET (evn.vn_next);
12432
12433 vna_off = offset + ivn.vn_aux;
12434
12435 do
12436 {
12437 Elf_External_Vernaux evna;
12438
12439 if (get_data (&evna, filedata, vna_off, sizeof (evna), 1,
12440 _("version need aux (3)")) == NULL)
12441 {
12442 ivna.vna_next = 0;
12443 ivna.vna_other = 0;
12444 ivna.vna_name = 0;
12445 }
12446 else
12447 {
12448 ivna.vna_other = BYTE_GET (evna.vna_other);
12449 ivna.vna_next = BYTE_GET (evna.vna_next);
12450 ivna.vna_name = BYTE_GET (evna.vna_name);
12451 }
12452
12453 vna_off += ivna.vna_next;
12454 }
12455 while (ivna.vna_other != vers_data && ivna.vna_next != 0);
12456
12457 if (ivna.vna_other == vers_data)
12458 break;
12459
12460 offset += ivn.vn_next;
12461 }
12462 while (ivn.vn_next != 0);
12463
12464 if (ivna.vna_other == vers_data)
12465 {
12466 *sym_info = symbol_undefined;
12467 *vna_other = ivna.vna_other;
12468 return (ivna.vna_name < strtab_size
12469 ? strtab + ivna.vna_name : _("<corrupt>"));
12470 }
12471 else if ((max_vd_ndx || (vers_data & VERSYM_VERSION) != 1)
12472 && (vers_data & VERSYM_VERSION) > max_vd_ndx)
12473 return _("<corrupt>");
12474 }
12475 return NULL;
12476 }
12477
12478 /* Display a symbol size on stdout. Format is based on --sym-base setting. */
12479
12480 static unsigned int
12481 print_dynamic_symbol_size (bfd_vma vma, int base)
12482 {
12483 switch (base)
12484 {
12485 case 8:
12486 return print_vma (vma, OCTAL_5);
12487
12488 case 10:
12489 return print_vma (vma, UNSIGNED_5);
12490
12491 case 16:
12492 return print_vma (vma, PREFIX_HEX_5);
12493
12494 case 0:
12495 default:
12496 return print_vma (vma, DEC_5);
12497 }
12498 }
12499
12500 static void
12501 print_dynamic_symbol (Filedata *filedata, unsigned long si,
12502 Elf_Internal_Sym *symtab,
12503 Elf_Internal_Shdr *section,
12504 char *strtab, size_t strtab_size)
12505 {
12506 const char *version_string;
12507 enum versioned_symbol_info sym_info;
12508 unsigned short vna_other;
12509 bool is_valid;
12510 const char * sstr;
12511 Elf_Internal_Sym *psym = symtab + si;
12512
12513 printf ("%6ld: ", si);
12514 print_vma (psym->st_value, LONG_HEX);
12515 putchar (' ');
12516 print_dynamic_symbol_size (psym->st_size, sym_base);
12517 printf (" %-7s", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
12518 printf (" %-6s", get_symbol_binding (filedata, ELF_ST_BIND (psym->st_info)));
12519 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
12520 printf (" %-7s", get_solaris_symbol_visibility (psym->st_other));
12521 else
12522 {
12523 unsigned int vis = ELF_ST_VISIBILITY (psym->st_other);
12524
12525 printf (" %-7s", get_symbol_visibility (vis));
12526 /* Check to see if any other bits in the st_other field are set.
12527 Note - displaying this information disrupts the layout of the
12528 table being generated, but for the moment this case is very rare. */
12529 if (psym->st_other ^ vis)
12530 printf (" [%s] ", get_symbol_other (filedata, psym->st_other ^ vis));
12531 }
12532 printf (" %4s ", get_symbol_index_type (filedata, psym->st_shndx));
12533
12534 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION
12535 && psym->st_shndx < filedata->file_header.e_shnum
12536 && psym->st_name == 0)
12537 {
12538 is_valid = SECTION_NAME_VALID (filedata->section_headers + psym->st_shndx);
12539 sstr = is_valid ?
12540 SECTION_NAME_PRINT (filedata->section_headers + psym->st_shndx)
12541 : _("<corrupt>");
12542 }
12543 else
12544 {
12545 is_valid = VALID_SYMBOL_NAME (strtab, strtab_size, psym->st_name);
12546 sstr = is_valid ? strtab + psym->st_name : _("<corrupt>");
12547 }
12548
12549 version_string
12550 = get_symbol_version_string (filedata,
12551 (section == NULL
12552 || section->sh_type == SHT_DYNSYM),
12553 strtab, strtab_size, si,
12554 psym, &sym_info, &vna_other);
12555
12556 int len_avail = 21;
12557 if (! do_wide && version_string != NULL)
12558 {
12559 char buffer[16];
12560
12561 len_avail -= 1 + strlen (version_string);
12562
12563 if (sym_info == symbol_undefined)
12564 len_avail -= sprintf (buffer," (%d)", vna_other);
12565 else if (sym_info != symbol_hidden)
12566 len_avail -= 1;
12567 }
12568
12569 print_symbol (len_avail, sstr);
12570
12571 if (version_string)
12572 {
12573 if (sym_info == symbol_undefined)
12574 printf ("@%s (%d)", version_string, vna_other);
12575 else
12576 printf (sym_info == symbol_hidden ? "@%s" : "@@%s",
12577 version_string);
12578 }
12579
12580 putchar ('\n');
12581
12582 if (ELF_ST_BIND (psym->st_info) == STB_LOCAL
12583 && section != NULL
12584 && si >= section->sh_info
12585 /* Irix 5 and 6 MIPS binaries are known to ignore this requirement. */
12586 && filedata->file_header.e_machine != EM_MIPS
12587 /* Solaris binaries have been found to violate this requirement as
12588 well. Not sure if this is a bug or an ABI requirement. */
12589 && filedata->file_header.e_ident[EI_OSABI] != ELFOSABI_SOLARIS)
12590 warn (_("local symbol %lu found at index >= %s's sh_info value of %u\n"),
12591 si, printable_section_name (filedata, section), section->sh_info);
12592 }
12593
12594 static const char *
12595 get_lto_kind (unsigned int kind)
12596 {
12597 switch (kind)
12598 {
12599 case 0: return "DEF";
12600 case 1: return "WEAKDEF";
12601 case 2: return "UNDEF";
12602 case 3: return "WEAKUNDEF";
12603 case 4: return "COMMON";
12604 default:
12605 break;
12606 }
12607
12608 static char buffer[30];
12609 error (_("Unknown LTO symbol definition encountered: %u\n"), kind);
12610 sprintf (buffer, "<unknown: %u>", kind);
12611 return buffer;
12612 }
12613
12614 static const char *
12615 get_lto_visibility (unsigned int visibility)
12616 {
12617 switch (visibility)
12618 {
12619 case 0: return "DEFAULT";
12620 case 1: return "PROTECTED";
12621 case 2: return "INTERNAL";
12622 case 3: return "HIDDEN";
12623 default:
12624 break;
12625 }
12626
12627 static char buffer[30];
12628 error (_("Unknown LTO symbol visibility encountered: %u\n"), visibility);
12629 sprintf (buffer, "<unknown: %u>", visibility);
12630 return buffer;
12631 }
12632
12633 static const char *
12634 get_lto_sym_type (unsigned int sym_type)
12635 {
12636 switch (sym_type)
12637 {
12638 case 0: return "UNKNOWN";
12639 case 1: return "FUNCTION";
12640 case 2: return "VARIABLE";
12641 default:
12642 break;
12643 }
12644
12645 static char buffer[30];
12646 error (_("Unknown LTO symbol type encountered: %u\n"), sym_type);
12647 sprintf (buffer, "<unknown: %u>", sym_type);
12648 return buffer;
12649 }
12650
12651 /* Display an LTO format symbol table.
12652 FIXME: The format of LTO symbol tables is not formalized.
12653 So this code could need changing in the future. */
12654
12655 static bool
12656 display_lto_symtab (Filedata * filedata,
12657 Elf_Internal_Shdr * section)
12658 {
12659 if (section->sh_size == 0)
12660 {
12661 if (filedata->is_separate)
12662 printf (_("\nThe LTO Symbol table section '%s' in linked file '%s' is empty!\n"),
12663 printable_section_name (filedata, section),
12664 filedata->file_name);
12665 else
12666 printf (_("\nLTO Symbol table '%s' is empty!\n"),
12667 printable_section_name (filedata, section));
12668
12669 return true;
12670 }
12671
12672 if (section->sh_size > filedata->file_size)
12673 {
12674 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
12675 printable_section_name (filedata, section),
12676 (unsigned long) section->sh_size);
12677 return false;
12678 }
12679
12680 void * alloced_data = get_data (NULL, filedata, section->sh_offset,
12681 section->sh_size, 1, _("LTO symbols"));
12682 if (alloced_data == NULL)
12683 return false;
12684
12685 /* Look for extended data for the symbol table. */
12686 Elf_Internal_Shdr * ext;
12687 void * ext_data_orig = NULL;
12688 char * ext_data = NULL;
12689 char * ext_data_end = NULL;
12690 char * ext_name = NULL;
12691
12692 if (asprintf (& ext_name, ".gnu.lto_.ext_symtab.%s",
12693 SECTION_NAME (section) + sizeof (".gnu.lto_.symtab.") - 1) > 0
12694 && ext_name != NULL /* Paranoia. */
12695 && (ext = find_section (filedata, ext_name)) != NULL)
12696 {
12697 if (ext->sh_size < 3)
12698 error (_("LTO Symbol extension table '%s' is empty!\n"),
12699 printable_section_name (filedata, ext));
12700 else
12701 {
12702 ext_data_orig = ext_data = get_data (NULL, filedata, ext->sh_offset,
12703 ext->sh_size, 1,
12704 _("LTO ext symbol data"));
12705 if (ext_data != NULL)
12706 {
12707 ext_data_end = ext_data + ext->sh_size;
12708 if (* ext_data++ != 1)
12709 error (_("Unexpected version number in symbol extension table\n"));
12710 }
12711 }
12712 }
12713
12714 const unsigned char * data = (const unsigned char *) alloced_data;
12715 const unsigned char * end = data + section->sh_size;
12716
12717 if (filedata->is_separate)
12718 printf (_("\nIn linked file '%s': "), filedata->file_name);
12719 else
12720 printf ("\n");
12721
12722 if (ext_data_orig != NULL)
12723 {
12724 if (do_wide)
12725 printf (_("LTO Symbol table '%s' and extension table '%s' contain:\n"),
12726 printable_section_name (filedata, section),
12727 printable_section_name (filedata, ext));
12728 else
12729 {
12730 printf (_("LTO Symbol table '%s'\n"),
12731 printable_section_name (filedata, section));
12732 printf (_(" and extension table '%s' contain:\n"),
12733 printable_section_name (filedata, ext));
12734 }
12735 }
12736 else
12737 printf (_("LTO Symbol table '%s' contains:\n"),
12738 printable_section_name (filedata, section));
12739
12740 /* FIXME: Add a wide version. */
12741 if (ext_data_orig != NULL)
12742 printf (_(" Comdat_Key Kind Visibility Size Slot Type Section Name\n"));
12743 else
12744 printf (_(" Comdat_Key Kind Visibility Size Slot Name\n"));
12745
12746 /* FIXME: We do not handle style prefixes. */
12747
12748 while (data < end)
12749 {
12750 const unsigned char * sym_name = data;
12751 data += strnlen ((const char *) sym_name, end - data) + 1;
12752 if (data >= end)
12753 goto fail;
12754
12755 const unsigned char * comdat_key = data;
12756 data += strnlen ((const char *) comdat_key, end - data) + 1;
12757 if (data >= end)
12758 goto fail;
12759
12760 if (data + 2 + 8 + 4 > end)
12761 goto fail;
12762
12763 unsigned int kind = *data++;
12764 unsigned int visibility = *data++;
12765
12766 elf_vma size = byte_get (data, 8);
12767 data += 8;
12768
12769 elf_vma slot = byte_get (data, 4);
12770 data += 4;
12771
12772 if (ext_data != NULL)
12773 {
12774 if (ext_data < (ext_data_end - 1))
12775 {
12776 unsigned int sym_type = * ext_data ++;
12777 unsigned int sec_kind = * ext_data ++;
12778
12779 printf (" %10s %10s %11s %08lx %08lx %9s %08lx _",
12780 * comdat_key == 0 ? "-" : (char *) comdat_key,
12781 get_lto_kind (kind),
12782 get_lto_visibility (visibility),
12783 (long) size,
12784 (long) slot,
12785 get_lto_sym_type (sym_type),
12786 (long) sec_kind);
12787 print_symbol (6, (const char *) sym_name);
12788 }
12789 else
12790 {
12791 error (_("Ran out of LTO symbol extension data\n"));
12792 ext_data = NULL;
12793 /* FIXME: return FAIL result ? */
12794 }
12795 }
12796 else
12797 {
12798 printf (" %10s %10s %11s %08lx %08lx _",
12799 * comdat_key == 0 ? "-" : (char *) comdat_key,
12800 get_lto_kind (kind),
12801 get_lto_visibility (visibility),
12802 (long) size,
12803 (long) slot);
12804 print_symbol (21, (const char *) sym_name);
12805 }
12806 putchar ('\n');
12807 }
12808
12809 if (ext_data != NULL && ext_data < ext_data_end)
12810 {
12811 error (_("Data remains in the LTO symbol extension table\n"));
12812 goto fail;
12813 }
12814
12815 free (alloced_data);
12816 free (ext_data_orig);
12817 free (ext_name);
12818 return true;
12819
12820 fail:
12821 error (_("Buffer overrun encountered whilst decoding LTO symbol table\n"));
12822 free (alloced_data);
12823 free (ext_data_orig);
12824 free (ext_name);
12825 return false;
12826 }
12827
12828 /* Display LTO symbol tables. */
12829
12830 static bool
12831 process_lto_symbol_tables (Filedata * filedata)
12832 {
12833 Elf_Internal_Shdr * section;
12834 unsigned int i;
12835 bool res = true;
12836
12837 if (!do_lto_syms)
12838 return true;
12839
12840 if (filedata->section_headers == NULL)
12841 return true;
12842
12843 for (i = 0, section = filedata->section_headers;
12844 i < filedata->file_header.e_shnum;
12845 i++, section++)
12846 if (SECTION_NAME_VALID (section)
12847 && startswith (SECTION_NAME (section), ".gnu.lto_.symtab."))
12848 res &= display_lto_symtab (filedata, section);
12849
12850 return res;
12851 }
12852
12853 /* Dump the symbol table. */
12854
12855 static bool
12856 process_symbol_table (Filedata * filedata)
12857 {
12858 Elf_Internal_Shdr * section;
12859
12860 if (!do_syms && !do_dyn_syms && !do_histogram)
12861 return true;
12862
12863 if ((filedata->dynamic_info[DT_HASH] || filedata->dynamic_info_DT_GNU_HASH)
12864 && do_syms
12865 && do_using_dynamic
12866 && filedata->dynamic_strings != NULL
12867 && filedata->dynamic_symbols != NULL)
12868 {
12869 unsigned long si;
12870
12871 if (filedata->is_separate)
12872 {
12873 printf (ngettext ("\nIn linked file '%s' the dynamic symbol table contains %lu entry:\n",
12874 "\nIn linked file '%s' the dynamic symbol table contains %lu entries:\n",
12875 filedata->num_dynamic_syms),
12876 filedata->file_name,
12877 filedata->num_dynamic_syms);
12878 }
12879 else
12880 {
12881 printf (ngettext ("\nSymbol table for image contains %lu entry:\n",
12882 "\nSymbol table for image contains %lu entries:\n",
12883 filedata->num_dynamic_syms),
12884 filedata->num_dynamic_syms);
12885 }
12886 if (is_32bit_elf)
12887 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
12888 else
12889 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
12890
12891 for (si = 0; si < filedata->num_dynamic_syms; si++)
12892 print_dynamic_symbol (filedata, si, filedata->dynamic_symbols, NULL,
12893 filedata->dynamic_strings,
12894 filedata->dynamic_strings_length);
12895 }
12896 else if ((do_dyn_syms || (do_syms && !do_using_dynamic))
12897 && filedata->section_headers != NULL)
12898 {
12899 unsigned int i;
12900
12901 for (i = 0, section = filedata->section_headers;
12902 i < filedata->file_header.e_shnum;
12903 i++, section++)
12904 {
12905 char * strtab = NULL;
12906 unsigned long int strtab_size = 0;
12907 Elf_Internal_Sym * symtab;
12908 unsigned long si, num_syms;
12909
12910 if ((section->sh_type != SHT_SYMTAB
12911 && section->sh_type != SHT_DYNSYM)
12912 || (!do_syms
12913 && section->sh_type == SHT_SYMTAB))
12914 continue;
12915
12916 if (section->sh_entsize == 0)
12917 {
12918 printf (_("\nSymbol table '%s' has a sh_entsize of zero!\n"),
12919 printable_section_name (filedata, section));
12920 continue;
12921 }
12922
12923 num_syms = section->sh_size / section->sh_entsize;
12924
12925 if (filedata->is_separate)
12926 printf (ngettext ("\nIn linked file '%s' symbol section '%s' contains %lu entry:\n",
12927 "\nIn linked file '%s' symbol section '%s' contains %lu entries:\n",
12928 num_syms),
12929 filedata->file_name,
12930 printable_section_name (filedata, section),
12931 num_syms);
12932 else
12933 printf (ngettext ("\nSymbol table '%s' contains %lu entry:\n",
12934 "\nSymbol table '%s' contains %lu entries:\n",
12935 num_syms),
12936 printable_section_name (filedata, section),
12937 num_syms);
12938
12939 if (is_32bit_elf)
12940 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
12941 else
12942 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
12943
12944 symtab = get_elf_symbols (filedata, section, & num_syms);
12945 if (symtab == NULL)
12946 continue;
12947
12948 if (section->sh_link == filedata->file_header.e_shstrndx)
12949 {
12950 strtab = filedata->string_table;
12951 strtab_size = filedata->string_table_length;
12952 }
12953 else if (section->sh_link < filedata->file_header.e_shnum)
12954 {
12955 Elf_Internal_Shdr * string_sec;
12956
12957 string_sec = filedata->section_headers + section->sh_link;
12958
12959 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset,
12960 1, string_sec->sh_size,
12961 _("string table"));
12962 strtab_size = strtab != NULL ? string_sec->sh_size : 0;
12963 }
12964
12965 for (si = 0; si < num_syms; si++)
12966 print_dynamic_symbol (filedata, si, symtab, section,
12967 strtab, strtab_size);
12968
12969 free (symtab);
12970 if (strtab != filedata->string_table)
12971 free (strtab);
12972 }
12973 }
12974 else if (do_syms)
12975 printf
12976 (_("\nDynamic symbol information is not available for displaying symbols.\n"));
12977
12978 if (do_histogram && filedata->buckets != NULL)
12979 {
12980 unsigned long * lengths;
12981 unsigned long * counts;
12982 unsigned long hn;
12983 bfd_vma si;
12984 unsigned long maxlength = 0;
12985 unsigned long nzero_counts = 0;
12986 unsigned long nsyms = 0;
12987 char *visited;
12988
12989 printf (ngettext ("\nHistogram for bucket list length "
12990 "(total of %lu bucket):\n",
12991 "\nHistogram for bucket list length "
12992 "(total of %lu buckets):\n",
12993 (unsigned long) filedata->nbuckets),
12994 (unsigned long) filedata->nbuckets);
12995
12996 lengths = (unsigned long *) calloc (filedata->nbuckets,
12997 sizeof (*lengths));
12998 if (lengths == NULL)
12999 {
13000 error (_("Out of memory allocating space for histogram buckets\n"));
13001 goto err_out;
13002 }
13003 visited = xcmalloc (filedata->nchains, 1);
13004 memset (visited, 0, filedata->nchains);
13005
13006 printf (_(" Length Number %% of total Coverage\n"));
13007 for (hn = 0; hn < filedata->nbuckets; ++hn)
13008 {
13009 for (si = filedata->buckets[hn]; si > 0; si = filedata->chains[si])
13010 {
13011 ++nsyms;
13012 if (maxlength < ++lengths[hn])
13013 ++maxlength;
13014 if (si >= filedata->nchains || visited[si])
13015 {
13016 error (_("histogram chain is corrupt\n"));
13017 break;
13018 }
13019 visited[si] = 1;
13020 }
13021 }
13022 free (visited);
13023
13024 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
13025 if (counts == NULL)
13026 {
13027 free (lengths);
13028 error (_("Out of memory allocating space for histogram counts\n"));
13029 goto err_out;
13030 }
13031
13032 for (hn = 0; hn < filedata->nbuckets; ++hn)
13033 ++counts[lengths[hn]];
13034
13035 if (filedata->nbuckets > 0)
13036 {
13037 unsigned long i;
13038 printf (" 0 %-10lu (%5.1f%%)\n",
13039 counts[0], (counts[0] * 100.0) / filedata->nbuckets);
13040 for (i = 1; i <= maxlength; ++i)
13041 {
13042 nzero_counts += counts[i] * i;
13043 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
13044 i, counts[i], (counts[i] * 100.0) / filedata->nbuckets,
13045 (nzero_counts * 100.0) / nsyms);
13046 }
13047 }
13048
13049 free (counts);
13050 free (lengths);
13051 }
13052
13053 free (filedata->buckets);
13054 filedata->buckets = NULL;
13055 filedata->nbuckets = 0;
13056 free (filedata->chains);
13057 filedata->chains = NULL;
13058
13059 if (do_histogram && filedata->gnubuckets != NULL)
13060 {
13061 unsigned long * lengths;
13062 unsigned long * counts;
13063 unsigned long hn;
13064 unsigned long maxlength = 0;
13065 unsigned long nzero_counts = 0;
13066 unsigned long nsyms = 0;
13067
13068 printf (ngettext ("\nHistogram for `%s' bucket list length "
13069 "(total of %lu bucket):\n",
13070 "\nHistogram for `%s' bucket list length "
13071 "(total of %lu buckets):\n",
13072 (unsigned long) filedata->ngnubuckets),
13073 GNU_HASH_SECTION_NAME (filedata),
13074 (unsigned long) filedata->ngnubuckets);
13075
13076 lengths = (unsigned long *) calloc (filedata->ngnubuckets,
13077 sizeof (*lengths));
13078 if (lengths == NULL)
13079 {
13080 error (_("Out of memory allocating space for gnu histogram buckets\n"));
13081 goto err_out;
13082 }
13083
13084 printf (_(" Length Number %% of total Coverage\n"));
13085
13086 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
13087 if (filedata->gnubuckets[hn] != 0)
13088 {
13089 bfd_vma off, length = 1;
13090
13091 for (off = filedata->gnubuckets[hn] - filedata->gnusymidx;
13092 /* PR 17531 file: 010-77222-0.004. */
13093 off < filedata->ngnuchains
13094 && (filedata->gnuchains[off] & 1) == 0;
13095 ++off)
13096 ++length;
13097 lengths[hn] = length;
13098 if (length > maxlength)
13099 maxlength = length;
13100 nsyms += length;
13101 }
13102
13103 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
13104 if (counts == NULL)
13105 {
13106 free (lengths);
13107 error (_("Out of memory allocating space for gnu histogram counts\n"));
13108 goto err_out;
13109 }
13110
13111 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
13112 ++counts[lengths[hn]];
13113
13114 if (filedata->ngnubuckets > 0)
13115 {
13116 unsigned long j;
13117 printf (" 0 %-10lu (%5.1f%%)\n",
13118 counts[0], (counts[0] * 100.0) / filedata->ngnubuckets);
13119 for (j = 1; j <= maxlength; ++j)
13120 {
13121 nzero_counts += counts[j] * j;
13122 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
13123 j, counts[j], (counts[j] * 100.0) / filedata->ngnubuckets,
13124 (nzero_counts * 100.0) / nsyms);
13125 }
13126 }
13127
13128 free (counts);
13129 free (lengths);
13130 }
13131 free (filedata->gnubuckets);
13132 filedata->gnubuckets = NULL;
13133 filedata->ngnubuckets = 0;
13134 free (filedata->gnuchains);
13135 filedata->gnuchains = NULL;
13136 filedata->ngnuchains = 0;
13137 free (filedata->mipsxlat);
13138 filedata->mipsxlat = NULL;
13139 return true;
13140
13141 err_out:
13142 free (filedata->gnubuckets);
13143 filedata->gnubuckets = NULL;
13144 filedata->ngnubuckets = 0;
13145 free (filedata->gnuchains);
13146 filedata->gnuchains = NULL;
13147 filedata->ngnuchains = 0;
13148 free (filedata->mipsxlat);
13149 filedata->mipsxlat = NULL;
13150 free (filedata->buckets);
13151 filedata->buckets = NULL;
13152 filedata->nbuckets = 0;
13153 free (filedata->chains);
13154 filedata->chains = NULL;
13155 return false;
13156 }
13157
13158 static bool
13159 process_syminfo (Filedata * filedata)
13160 {
13161 unsigned int i;
13162
13163 if (filedata->dynamic_syminfo == NULL
13164 || !do_dynamic)
13165 /* No syminfo, this is ok. */
13166 return true;
13167
13168 /* There better should be a dynamic symbol section. */
13169 if (filedata->dynamic_symbols == NULL || filedata->dynamic_strings == NULL)
13170 return false;
13171
13172 if (filedata->is_separate)
13173 printf (ngettext ("\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entry:\n",
13174 "\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entries:\n",
13175 filedata->dynamic_syminfo_nent),
13176 filedata->file_name,
13177 filedata->dynamic_syminfo_offset,
13178 filedata->dynamic_syminfo_nent);
13179 else
13180 printf (ngettext ("\nDynamic info segment at offset 0x%lx "
13181 "contains %d entry:\n",
13182 "\nDynamic info segment at offset 0x%lx "
13183 "contains %d entries:\n",
13184 filedata->dynamic_syminfo_nent),
13185 filedata->dynamic_syminfo_offset,
13186 filedata->dynamic_syminfo_nent);
13187
13188 printf (_(" Num: Name BoundTo Flags\n"));
13189 for (i = 0; i < filedata->dynamic_syminfo_nent; ++i)
13190 {
13191 unsigned short int flags = filedata->dynamic_syminfo[i].si_flags;
13192
13193 printf ("%4d: ", i);
13194 if (i >= filedata->num_dynamic_syms)
13195 printf (_("<corrupt index>"));
13196 else if (VALID_DYNAMIC_NAME (filedata, filedata->dynamic_symbols[i].st_name))
13197 print_symbol (30, GET_DYNAMIC_NAME (filedata,
13198 filedata->dynamic_symbols[i].st_name));
13199 else
13200 printf (_("<corrupt: %19ld>"), filedata->dynamic_symbols[i].st_name);
13201 putchar (' ');
13202
13203 switch (filedata->dynamic_syminfo[i].si_boundto)
13204 {
13205 case SYMINFO_BT_SELF:
13206 fputs ("SELF ", stdout);
13207 break;
13208 case SYMINFO_BT_PARENT:
13209 fputs ("PARENT ", stdout);
13210 break;
13211 default:
13212 if (filedata->dynamic_syminfo[i].si_boundto > 0
13213 && filedata->dynamic_syminfo[i].si_boundto < filedata->dynamic_nent
13214 && VALID_DYNAMIC_NAME (filedata,
13215 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val))
13216 {
13217 print_symbol (10, GET_DYNAMIC_NAME (filedata,
13218 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val));
13219 putchar (' ' );
13220 }
13221 else
13222 printf ("%-10d ", filedata->dynamic_syminfo[i].si_boundto);
13223 break;
13224 }
13225
13226 if (flags & SYMINFO_FLG_DIRECT)
13227 printf (" DIRECT");
13228 if (flags & SYMINFO_FLG_PASSTHRU)
13229 printf (" PASSTHRU");
13230 if (flags & SYMINFO_FLG_COPY)
13231 printf (" COPY");
13232 if (flags & SYMINFO_FLG_LAZYLOAD)
13233 printf (" LAZYLOAD");
13234
13235 puts ("");
13236 }
13237
13238 return true;
13239 }
13240
13241 /* A macro which evaluates to TRUE if the region ADDR .. ADDR + NELEM
13242 is contained by the region START .. END. The types of ADDR, START
13243 and END should all be the same. Note both ADDR + NELEM and END
13244 point to just beyond the end of the regions that are being tested. */
13245 #define IN_RANGE(START,END,ADDR,NELEM) \
13246 (((ADDR) >= (START)) && ((ADDR) < (END)) && ((ADDR) + (NELEM) <= (END)))
13247
13248 /* Check to see if the given reloc needs to be handled in a target specific
13249 manner. If so then process the reloc and return TRUE otherwise return
13250 FALSE.
13251
13252 If called with reloc == NULL, then this is a signal that reloc processing
13253 for the current section has finished, and any saved state should be
13254 discarded. */
13255
13256 static bool
13257 target_specific_reloc_handling (Filedata * filedata,
13258 Elf_Internal_Rela * reloc,
13259 unsigned char * start,
13260 unsigned char * end,
13261 Elf_Internal_Sym * symtab,
13262 unsigned long num_syms)
13263 {
13264 unsigned int reloc_type = 0;
13265 unsigned long sym_index = 0;
13266
13267 if (reloc)
13268 {
13269 reloc_type = get_reloc_type (filedata, reloc->r_info);
13270 sym_index = get_reloc_symindex (reloc->r_info);
13271 }
13272
13273 switch (filedata->file_header.e_machine)
13274 {
13275 case EM_MSP430:
13276 case EM_MSP430_OLD:
13277 {
13278 static Elf_Internal_Sym * saved_sym = NULL;
13279
13280 if (reloc == NULL)
13281 {
13282 saved_sym = NULL;
13283 return true;
13284 }
13285
13286 switch (reloc_type)
13287 {
13288 case 10: /* R_MSP430_SYM_DIFF */
13289 case 12: /* R_MSP430_GNU_SUB_ULEB128 */
13290 if (uses_msp430x_relocs (filedata))
13291 break;
13292 /* Fall through. */
13293 case 21: /* R_MSP430X_SYM_DIFF */
13294 case 23: /* R_MSP430X_GNU_SUB_ULEB128 */
13295 /* PR 21139. */
13296 if (sym_index >= num_syms)
13297 error (_("MSP430 SYM_DIFF reloc contains invalid symbol index %lu\n"),
13298 sym_index);
13299 else
13300 saved_sym = symtab + sym_index;
13301 return true;
13302
13303 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
13304 case 3: /* R_MSP430_16 or R_MSP430_ABS8 */
13305 goto handle_sym_diff;
13306
13307 case 5: /* R_MSP430_16_BYTE */
13308 case 9: /* R_MSP430_8 */
13309 case 11: /* R_MSP430_GNU_SET_ULEB128 */
13310 if (uses_msp430x_relocs (filedata))
13311 break;
13312 goto handle_sym_diff;
13313
13314 case 2: /* R_MSP430_ABS16 */
13315 case 15: /* R_MSP430X_ABS16 */
13316 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
13317 if (! uses_msp430x_relocs (filedata))
13318 break;
13319 goto handle_sym_diff;
13320
13321 handle_sym_diff:
13322 if (saved_sym != NULL)
13323 {
13324 bfd_vma value;
13325 unsigned int reloc_size = 0;
13326 int leb_ret = 0;
13327 switch (reloc_type)
13328 {
13329 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
13330 reloc_size = 4;
13331 break;
13332 case 11: /* R_MSP430_GNU_SET_ULEB128 */
13333 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
13334 if (reloc->r_offset < (size_t) (end - start))
13335 read_leb128 (start + reloc->r_offset, end, false,
13336 &reloc_size, &leb_ret);
13337 break;
13338 default:
13339 reloc_size = 2;
13340 break;
13341 }
13342
13343 if (leb_ret != 0 || reloc_size == 0 || reloc_size > 8)
13344 error (_("MSP430 ULEB128 field at 0x%lx contains invalid "
13345 "ULEB128 value\n"),
13346 (long) reloc->r_offset);
13347 else if (sym_index >= num_syms)
13348 error (_("MSP430 reloc contains invalid symbol index %lu\n"),
13349 sym_index);
13350 else
13351 {
13352 value = reloc->r_addend + (symtab[sym_index].st_value
13353 - saved_sym->st_value);
13354
13355 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
13356 byte_put (start + reloc->r_offset, value, reloc_size);
13357 else
13358 /* PR 21137 */
13359 error (_("MSP430 sym diff reloc contains invalid offset: 0x%lx\n"),
13360 (long) reloc->r_offset);
13361 }
13362
13363 saved_sym = NULL;
13364 return true;
13365 }
13366 break;
13367
13368 default:
13369 if (saved_sym != NULL)
13370 error (_("Unhandled MSP430 reloc type found after SYM_DIFF reloc\n"));
13371 break;
13372 }
13373 break;
13374 }
13375
13376 case EM_MN10300:
13377 case EM_CYGNUS_MN10300:
13378 {
13379 static Elf_Internal_Sym * saved_sym = NULL;
13380
13381 if (reloc == NULL)
13382 {
13383 saved_sym = NULL;
13384 return true;
13385 }
13386
13387 switch (reloc_type)
13388 {
13389 case 34: /* R_MN10300_ALIGN */
13390 return true;
13391 case 33: /* R_MN10300_SYM_DIFF */
13392 if (sym_index >= num_syms)
13393 error (_("MN10300_SYM_DIFF reloc contains invalid symbol index %lu\n"),
13394 sym_index);
13395 else
13396 saved_sym = symtab + sym_index;
13397 return true;
13398
13399 case 1: /* R_MN10300_32 */
13400 case 2: /* R_MN10300_16 */
13401 if (saved_sym != NULL)
13402 {
13403 int reloc_size = reloc_type == 1 ? 4 : 2;
13404 bfd_vma value;
13405
13406 if (sym_index >= num_syms)
13407 error (_("MN10300 reloc contains invalid symbol index %lu\n"),
13408 sym_index);
13409 else
13410 {
13411 value = reloc->r_addend + (symtab[sym_index].st_value
13412 - saved_sym->st_value);
13413
13414 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
13415 byte_put (start + reloc->r_offset, value, reloc_size);
13416 else
13417 error (_("MN10300 sym diff reloc contains invalid offset: 0x%lx\n"),
13418 (long) reloc->r_offset);
13419 }
13420
13421 saved_sym = NULL;
13422 return true;
13423 }
13424 break;
13425 default:
13426 if (saved_sym != NULL)
13427 error (_("Unhandled MN10300 reloc type found after SYM_DIFF reloc\n"));
13428 break;
13429 }
13430 break;
13431 }
13432
13433 case EM_RL78:
13434 {
13435 static bfd_vma saved_sym1 = 0;
13436 static bfd_vma saved_sym2 = 0;
13437 static bfd_vma value;
13438
13439 if (reloc == NULL)
13440 {
13441 saved_sym1 = saved_sym2 = 0;
13442 return true;
13443 }
13444
13445 switch (reloc_type)
13446 {
13447 case 0x80: /* R_RL78_SYM. */
13448 saved_sym1 = saved_sym2;
13449 if (sym_index >= num_syms)
13450 error (_("RL78_SYM reloc contains invalid symbol index %lu\n"),
13451 sym_index);
13452 else
13453 {
13454 saved_sym2 = symtab[sym_index].st_value;
13455 saved_sym2 += reloc->r_addend;
13456 }
13457 return true;
13458
13459 case 0x83: /* R_RL78_OPsub. */
13460 value = saved_sym1 - saved_sym2;
13461 saved_sym2 = saved_sym1 = 0;
13462 return true;
13463 break;
13464
13465 case 0x41: /* R_RL78_ABS32. */
13466 if (IN_RANGE (start, end, start + reloc->r_offset, 4))
13467 byte_put (start + reloc->r_offset, value, 4);
13468 else
13469 error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
13470 (long) reloc->r_offset);
13471 value = 0;
13472 return true;
13473
13474 case 0x43: /* R_RL78_ABS16. */
13475 if (IN_RANGE (start, end, start + reloc->r_offset, 2))
13476 byte_put (start + reloc->r_offset, value, 2);
13477 else
13478 error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
13479 (long) reloc->r_offset);
13480 value = 0;
13481 return true;
13482
13483 default:
13484 break;
13485 }
13486 break;
13487 }
13488 }
13489
13490 return false;
13491 }
13492
13493 /* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
13494 DWARF debug sections. This is a target specific test. Note - we do not
13495 go through the whole including-target-headers-multiple-times route, (as
13496 we have already done with <elf/h8.h>) because this would become very
13497 messy and even then this function would have to contain target specific
13498 information (the names of the relocs instead of their numeric values).
13499 FIXME: This is not the correct way to solve this problem. The proper way
13500 is to have target specific reloc sizing and typing functions created by
13501 the reloc-macros.h header, in the same way that it already creates the
13502 reloc naming functions. */
13503
13504 static bool
13505 is_32bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13506 {
13507 /* Please keep this table alpha-sorted for ease of visual lookup. */
13508 switch (filedata->file_header.e_machine)
13509 {
13510 case EM_386:
13511 case EM_IAMCU:
13512 return reloc_type == 1; /* R_386_32. */
13513 case EM_68K:
13514 return reloc_type == 1; /* R_68K_32. */
13515 case EM_860:
13516 return reloc_type == 1; /* R_860_32. */
13517 case EM_960:
13518 return reloc_type == 2; /* R_960_32. */
13519 case EM_AARCH64:
13520 return (reloc_type == 258
13521 || reloc_type == 1); /* R_AARCH64_ABS32 || R_AARCH64_P32_ABS32 */
13522 case EM_BPF:
13523 return reloc_type == 11; /* R_BPF_DATA_32 */
13524 case EM_ADAPTEVA_EPIPHANY:
13525 return reloc_type == 3;
13526 case EM_ALPHA:
13527 return reloc_type == 1; /* R_ALPHA_REFLONG. */
13528 case EM_ARC:
13529 return reloc_type == 1; /* R_ARC_32. */
13530 case EM_ARC_COMPACT:
13531 case EM_ARC_COMPACT2:
13532 return reloc_type == 4; /* R_ARC_32. */
13533 case EM_ARM:
13534 return reloc_type == 2; /* R_ARM_ABS32 */
13535 case EM_AVR_OLD:
13536 case EM_AVR:
13537 return reloc_type == 1;
13538 case EM_BLACKFIN:
13539 return reloc_type == 0x12; /* R_byte4_data. */
13540 case EM_CRIS:
13541 return reloc_type == 3; /* R_CRIS_32. */
13542 case EM_CR16:
13543 return reloc_type == 3; /* R_CR16_NUM32. */
13544 case EM_CRX:
13545 return reloc_type == 15; /* R_CRX_NUM32. */
13546 case EM_CSKY:
13547 return reloc_type == 1; /* R_CKCORE_ADDR32. */
13548 case EM_CYGNUS_FRV:
13549 return reloc_type == 1;
13550 case EM_CYGNUS_D10V:
13551 case EM_D10V:
13552 return reloc_type == 6; /* R_D10V_32. */
13553 case EM_CYGNUS_D30V:
13554 case EM_D30V:
13555 return reloc_type == 12; /* R_D30V_32_NORMAL. */
13556 case EM_DLX:
13557 return reloc_type == 3; /* R_DLX_RELOC_32. */
13558 case EM_CYGNUS_FR30:
13559 case EM_FR30:
13560 return reloc_type == 3; /* R_FR30_32. */
13561 case EM_FT32:
13562 return reloc_type == 1; /* R_FT32_32. */
13563 case EM_H8S:
13564 case EM_H8_300:
13565 case EM_H8_300H:
13566 return reloc_type == 1; /* R_H8_DIR32. */
13567 case EM_IA_64:
13568 return (reloc_type == 0x64 /* R_IA64_SECREL32MSB. */
13569 || reloc_type == 0x65 /* R_IA64_SECREL32LSB. */
13570 || reloc_type == 0x24 /* R_IA64_DIR32MSB. */
13571 || reloc_type == 0x25 /* R_IA64_DIR32LSB. */);
13572 case EM_IP2K_OLD:
13573 case EM_IP2K:
13574 return reloc_type == 2; /* R_IP2K_32. */
13575 case EM_IQ2000:
13576 return reloc_type == 2; /* R_IQ2000_32. */
13577 case EM_LATTICEMICO32:
13578 return reloc_type == 3; /* R_LM32_32. */
13579 case EM_M32C_OLD:
13580 case EM_M32C:
13581 return reloc_type == 3; /* R_M32C_32. */
13582 case EM_M32R:
13583 return reloc_type == 34; /* R_M32R_32_RELA. */
13584 case EM_68HC11:
13585 case EM_68HC12:
13586 return reloc_type == 6; /* R_M68HC11_32. */
13587 case EM_S12Z:
13588 return reloc_type == 7 || /* R_S12Z_EXT32 */
13589 reloc_type == 6; /* R_S12Z_CW32. */
13590 case EM_MCORE:
13591 return reloc_type == 1; /* R_MCORE_ADDR32. */
13592 case EM_CYGNUS_MEP:
13593 return reloc_type == 4; /* R_MEP_32. */
13594 case EM_METAG:
13595 return reloc_type == 2; /* R_METAG_ADDR32. */
13596 case EM_MICROBLAZE:
13597 return reloc_type == 1; /* R_MICROBLAZE_32. */
13598 case EM_MIPS:
13599 return reloc_type == 2; /* R_MIPS_32. */
13600 case EM_MMIX:
13601 return reloc_type == 4; /* R_MMIX_32. */
13602 case EM_CYGNUS_MN10200:
13603 case EM_MN10200:
13604 return reloc_type == 1; /* R_MN10200_32. */
13605 case EM_CYGNUS_MN10300:
13606 case EM_MN10300:
13607 return reloc_type == 1; /* R_MN10300_32. */
13608 case EM_MOXIE:
13609 return reloc_type == 1; /* R_MOXIE_32. */
13610 case EM_MSP430_OLD:
13611 case EM_MSP430:
13612 return reloc_type == 1; /* R_MSP430_32 or R_MSP320_ABS32. */
13613 case EM_MT:
13614 return reloc_type == 2; /* R_MT_32. */
13615 case EM_NDS32:
13616 return reloc_type == 20; /* R_NDS32_RELA. */
13617 case EM_ALTERA_NIOS2:
13618 return reloc_type == 12; /* R_NIOS2_BFD_RELOC_32. */
13619 case EM_NIOS32:
13620 return reloc_type == 1; /* R_NIOS_32. */
13621 case EM_OR1K:
13622 return reloc_type == 1; /* R_OR1K_32. */
13623 case EM_PARISC:
13624 return (reloc_type == 1 /* R_PARISC_DIR32. */
13625 || reloc_type == 2 /* R_PARISC_DIR21L. */
13626 || reloc_type == 41); /* R_PARISC_SECREL32. */
13627 case EM_PJ:
13628 case EM_PJ_OLD:
13629 return reloc_type == 1; /* R_PJ_DATA_DIR32. */
13630 case EM_PPC64:
13631 return reloc_type == 1; /* R_PPC64_ADDR32. */
13632 case EM_PPC:
13633 return reloc_type == 1; /* R_PPC_ADDR32. */
13634 case EM_TI_PRU:
13635 return reloc_type == 11; /* R_PRU_BFD_RELOC_32. */
13636 case EM_RISCV:
13637 return reloc_type == 1; /* R_RISCV_32. */
13638 case EM_RL78:
13639 return reloc_type == 1; /* R_RL78_DIR32. */
13640 case EM_RX:
13641 return reloc_type == 1; /* R_RX_DIR32. */
13642 case EM_S370:
13643 return reloc_type == 1; /* R_I370_ADDR31. */
13644 case EM_S390_OLD:
13645 case EM_S390:
13646 return reloc_type == 4; /* R_S390_32. */
13647 case EM_SCORE:
13648 return reloc_type == 8; /* R_SCORE_ABS32. */
13649 case EM_SH:
13650 return reloc_type == 1; /* R_SH_DIR32. */
13651 case EM_SPARC32PLUS:
13652 case EM_SPARCV9:
13653 case EM_SPARC:
13654 return reloc_type == 3 /* R_SPARC_32. */
13655 || reloc_type == 23; /* R_SPARC_UA32. */
13656 case EM_SPU:
13657 return reloc_type == 6; /* R_SPU_ADDR32 */
13658 case EM_TI_C6000:
13659 return reloc_type == 1; /* R_C6000_ABS32. */
13660 case EM_TILEGX:
13661 return reloc_type == 2; /* R_TILEGX_32. */
13662 case EM_TILEPRO:
13663 return reloc_type == 1; /* R_TILEPRO_32. */
13664 case EM_CYGNUS_V850:
13665 case EM_V850:
13666 return reloc_type == 6; /* R_V850_ABS32. */
13667 case EM_V800:
13668 return reloc_type == 0x33; /* R_V810_WORD. */
13669 case EM_VAX:
13670 return reloc_type == 1; /* R_VAX_32. */
13671 case EM_VISIUM:
13672 return reloc_type == 3; /* R_VISIUM_32. */
13673 case EM_WEBASSEMBLY:
13674 return reloc_type == 1; /* R_WASM32_32. */
13675 case EM_X86_64:
13676 case EM_L1OM:
13677 case EM_K1OM:
13678 return reloc_type == 10; /* R_X86_64_32. */
13679 case EM_XC16X:
13680 case EM_C166:
13681 return reloc_type == 3; /* R_XC16C_ABS_32. */
13682 case EM_XGATE:
13683 return reloc_type == 4; /* R_XGATE_32. */
13684 case EM_XSTORMY16:
13685 return reloc_type == 1; /* R_XSTROMY16_32. */
13686 case EM_XTENSA_OLD:
13687 case EM_XTENSA:
13688 return reloc_type == 1; /* R_XTENSA_32. */
13689 case EM_Z80:
13690 return reloc_type == 6; /* R_Z80_32. */
13691 default:
13692 {
13693 static unsigned int prev_warn = 0;
13694
13695 /* Avoid repeating the same warning multiple times. */
13696 if (prev_warn != filedata->file_header.e_machine)
13697 error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
13698 filedata->file_header.e_machine);
13699 prev_warn = filedata->file_header.e_machine;
13700 return false;
13701 }
13702 }
13703 }
13704
13705 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13706 a 32-bit pc-relative RELA relocation used in DWARF debug sections. */
13707
13708 static bool
13709 is_32bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
13710 {
13711 switch (filedata->file_header.e_machine)
13712 /* Please keep this table alpha-sorted for ease of visual lookup. */
13713 {
13714 case EM_386:
13715 case EM_IAMCU:
13716 return reloc_type == 2; /* R_386_PC32. */
13717 case EM_68K:
13718 return reloc_type == 4; /* R_68K_PC32. */
13719 case EM_AARCH64:
13720 return reloc_type == 261; /* R_AARCH64_PREL32 */
13721 case EM_ADAPTEVA_EPIPHANY:
13722 return reloc_type == 6;
13723 case EM_ALPHA:
13724 return reloc_type == 10; /* R_ALPHA_SREL32. */
13725 case EM_ARC_COMPACT:
13726 case EM_ARC_COMPACT2:
13727 return reloc_type == 49; /* R_ARC_32_PCREL. */
13728 case EM_ARM:
13729 return reloc_type == 3; /* R_ARM_REL32 */
13730 case EM_AVR_OLD:
13731 case EM_AVR:
13732 return reloc_type == 36; /* R_AVR_32_PCREL. */
13733 case EM_MICROBLAZE:
13734 return reloc_type == 2; /* R_MICROBLAZE_32_PCREL. */
13735 case EM_OR1K:
13736 return reloc_type == 9; /* R_OR1K_32_PCREL. */
13737 case EM_PARISC:
13738 return reloc_type == 9; /* R_PARISC_PCREL32. */
13739 case EM_PPC:
13740 return reloc_type == 26; /* R_PPC_REL32. */
13741 case EM_PPC64:
13742 return reloc_type == 26; /* R_PPC64_REL32. */
13743 case EM_RISCV:
13744 return reloc_type == 57; /* R_RISCV_32_PCREL. */
13745 case EM_S390_OLD:
13746 case EM_S390:
13747 return reloc_type == 5; /* R_390_PC32. */
13748 case EM_SH:
13749 return reloc_type == 2; /* R_SH_REL32. */
13750 case EM_SPARC32PLUS:
13751 case EM_SPARCV9:
13752 case EM_SPARC:
13753 return reloc_type == 6; /* R_SPARC_DISP32. */
13754 case EM_SPU:
13755 return reloc_type == 13; /* R_SPU_REL32. */
13756 case EM_TILEGX:
13757 return reloc_type == 6; /* R_TILEGX_32_PCREL. */
13758 case EM_TILEPRO:
13759 return reloc_type == 4; /* R_TILEPRO_32_PCREL. */
13760 case EM_VISIUM:
13761 return reloc_type == 6; /* R_VISIUM_32_PCREL */
13762 case EM_X86_64:
13763 case EM_L1OM:
13764 case EM_K1OM:
13765 return reloc_type == 2; /* R_X86_64_PC32. */
13766 case EM_VAX:
13767 return reloc_type == 4; /* R_VAX_PCREL32. */
13768 case EM_XTENSA_OLD:
13769 case EM_XTENSA:
13770 return reloc_type == 14; /* R_XTENSA_32_PCREL. */
13771 default:
13772 /* Do not abort or issue an error message here. Not all targets use
13773 pc-relative 32-bit relocs in their DWARF debug information and we
13774 have already tested for target coverage in is_32bit_abs_reloc. A
13775 more helpful warning message will be generated by apply_relocations
13776 anyway, so just return. */
13777 return false;
13778 }
13779 }
13780
13781 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13782 a 64-bit absolute RELA relocation used in DWARF debug sections. */
13783
13784 static bool
13785 is_64bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13786 {
13787 switch (filedata->file_header.e_machine)
13788 {
13789 case EM_AARCH64:
13790 return reloc_type == 257; /* R_AARCH64_ABS64. */
13791 case EM_ALPHA:
13792 return reloc_type == 2; /* R_ALPHA_REFQUAD. */
13793 case EM_IA_64:
13794 return (reloc_type == 0x26 /* R_IA64_DIR64MSB. */
13795 || reloc_type == 0x27 /* R_IA64_DIR64LSB. */);
13796 case EM_PARISC:
13797 return reloc_type == 80; /* R_PARISC_DIR64. */
13798 case EM_PPC64:
13799 return reloc_type == 38; /* R_PPC64_ADDR64. */
13800 case EM_RISCV:
13801 return reloc_type == 2; /* R_RISCV_64. */
13802 case EM_SPARC32PLUS:
13803 case EM_SPARCV9:
13804 case EM_SPARC:
13805 return reloc_type == 32 /* R_SPARC_64. */
13806 || reloc_type == 54; /* R_SPARC_UA64. */
13807 case EM_X86_64:
13808 case EM_L1OM:
13809 case EM_K1OM:
13810 return reloc_type == 1; /* R_X86_64_64. */
13811 case EM_S390_OLD:
13812 case EM_S390:
13813 return reloc_type == 22; /* R_S390_64. */
13814 case EM_TILEGX:
13815 return reloc_type == 1; /* R_TILEGX_64. */
13816 case EM_MIPS:
13817 return reloc_type == 18; /* R_MIPS_64. */
13818 default:
13819 return false;
13820 }
13821 }
13822
13823 /* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
13824 a 64-bit pc-relative RELA relocation used in DWARF debug sections. */
13825
13826 static bool
13827 is_64bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
13828 {
13829 switch (filedata->file_header.e_machine)
13830 {
13831 case EM_AARCH64:
13832 return reloc_type == 260; /* R_AARCH64_PREL64. */
13833 case EM_ALPHA:
13834 return reloc_type == 11; /* R_ALPHA_SREL64. */
13835 case EM_IA_64:
13836 return (reloc_type == 0x4e /* R_IA64_PCREL64MSB. */
13837 || reloc_type == 0x4f /* R_IA64_PCREL64LSB. */);
13838 case EM_PARISC:
13839 return reloc_type == 72; /* R_PARISC_PCREL64. */
13840 case EM_PPC64:
13841 return reloc_type == 44; /* R_PPC64_REL64. */
13842 case EM_SPARC32PLUS:
13843 case EM_SPARCV9:
13844 case EM_SPARC:
13845 return reloc_type == 46; /* R_SPARC_DISP64. */
13846 case EM_X86_64:
13847 case EM_L1OM:
13848 case EM_K1OM:
13849 return reloc_type == 24; /* R_X86_64_PC64. */
13850 case EM_S390_OLD:
13851 case EM_S390:
13852 return reloc_type == 23; /* R_S390_PC64. */
13853 case EM_TILEGX:
13854 return reloc_type == 5; /* R_TILEGX_64_PCREL. */
13855 default:
13856 return false;
13857 }
13858 }
13859
13860 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13861 a 24-bit absolute RELA relocation used in DWARF debug sections. */
13862
13863 static bool
13864 is_24bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13865 {
13866 switch (filedata->file_header.e_machine)
13867 {
13868 case EM_CYGNUS_MN10200:
13869 case EM_MN10200:
13870 return reloc_type == 4; /* R_MN10200_24. */
13871 case EM_FT32:
13872 return reloc_type == 5; /* R_FT32_20. */
13873 case EM_Z80:
13874 return reloc_type == 5; /* R_Z80_24. */
13875 default:
13876 return false;
13877 }
13878 }
13879
13880 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13881 a 16-bit absolute RELA relocation used in DWARF debug sections. */
13882
13883 static bool
13884 is_16bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13885 {
13886 /* Please keep this table alpha-sorted for ease of visual lookup. */
13887 switch (filedata->file_header.e_machine)
13888 {
13889 case EM_ARC:
13890 case EM_ARC_COMPACT:
13891 case EM_ARC_COMPACT2:
13892 return reloc_type == 2; /* R_ARC_16. */
13893 case EM_ADAPTEVA_EPIPHANY:
13894 return reloc_type == 5;
13895 case EM_AVR_OLD:
13896 case EM_AVR:
13897 return reloc_type == 4; /* R_AVR_16. */
13898 case EM_CYGNUS_D10V:
13899 case EM_D10V:
13900 return reloc_type == 3; /* R_D10V_16. */
13901 case EM_FT32:
13902 return reloc_type == 2; /* R_FT32_16. */
13903 case EM_H8S:
13904 case EM_H8_300:
13905 case EM_H8_300H:
13906 return reloc_type == R_H8_DIR16;
13907 case EM_IP2K_OLD:
13908 case EM_IP2K:
13909 return reloc_type == 1; /* R_IP2K_16. */
13910 case EM_M32C_OLD:
13911 case EM_M32C:
13912 return reloc_type == 1; /* R_M32C_16 */
13913 case EM_CYGNUS_MN10200:
13914 case EM_MN10200:
13915 return reloc_type == 2; /* R_MN10200_16. */
13916 case EM_CYGNUS_MN10300:
13917 case EM_MN10300:
13918 return reloc_type == 2; /* R_MN10300_16. */
13919 case EM_MSP430:
13920 if (uses_msp430x_relocs (filedata))
13921 return reloc_type == 2; /* R_MSP430_ABS16. */
13922 /* Fall through. */
13923 case EM_MSP430_OLD:
13924 return reloc_type == 5; /* R_MSP430_16_BYTE. */
13925 case EM_NDS32:
13926 return reloc_type == 19; /* R_NDS32_RELA. */
13927 case EM_ALTERA_NIOS2:
13928 return reloc_type == 13; /* R_NIOS2_BFD_RELOC_16. */
13929 case EM_NIOS32:
13930 return reloc_type == 9; /* R_NIOS_16. */
13931 case EM_OR1K:
13932 return reloc_type == 2; /* R_OR1K_16. */
13933 case EM_RISCV:
13934 return reloc_type == 55; /* R_RISCV_SET16. */
13935 case EM_TI_PRU:
13936 return reloc_type == 8; /* R_PRU_BFD_RELOC_16. */
13937 case EM_TI_C6000:
13938 return reloc_type == 2; /* R_C6000_ABS16. */
13939 case EM_VISIUM:
13940 return reloc_type == 2; /* R_VISIUM_16. */
13941 case EM_XC16X:
13942 case EM_C166:
13943 return reloc_type == 2; /* R_XC16C_ABS_16. */
13944 case EM_XGATE:
13945 return reloc_type == 3; /* R_XGATE_16. */
13946 case EM_Z80:
13947 return reloc_type == 4; /* R_Z80_16. */
13948 default:
13949 return false;
13950 }
13951 }
13952
13953 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13954 a 8-bit absolute RELA relocation used in DWARF debug sections. */
13955
13956 static bool
13957 is_8bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13958 {
13959 switch (filedata->file_header.e_machine)
13960 {
13961 case EM_RISCV:
13962 return reloc_type == 54; /* R_RISCV_SET8. */
13963 case EM_Z80:
13964 return reloc_type == 1; /* R_Z80_8. */
13965 default:
13966 return false;
13967 }
13968 }
13969
13970 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13971 a 6-bit absolute RELA relocation used in DWARF debug sections. */
13972
13973 static bool
13974 is_6bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13975 {
13976 switch (filedata->file_header.e_machine)
13977 {
13978 case EM_RISCV:
13979 return reloc_type == 53; /* R_RISCV_SET6. */
13980 default:
13981 return false;
13982 }
13983 }
13984
13985 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13986 a 32-bit inplace add RELA relocation used in DWARF debug sections. */
13987
13988 static bool
13989 is_32bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
13990 {
13991 /* Please keep this table alpha-sorted for ease of visual lookup. */
13992 switch (filedata->file_header.e_machine)
13993 {
13994 case EM_RISCV:
13995 return reloc_type == 35; /* R_RISCV_ADD32. */
13996 default:
13997 return false;
13998 }
13999 }
14000
14001 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14002 a 32-bit inplace sub RELA relocation used in DWARF debug sections. */
14003
14004 static bool
14005 is_32bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14006 {
14007 /* Please keep this table alpha-sorted for ease of visual lookup. */
14008 switch (filedata->file_header.e_machine)
14009 {
14010 case EM_RISCV:
14011 return reloc_type == 39; /* R_RISCV_SUB32. */
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 64-bit inplace add RELA relocation used in DWARF debug sections. */
14019
14020 static bool
14021 is_64bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14022 {
14023 /* Please keep this table alpha-sorted for ease of visual lookup. */
14024 switch (filedata->file_header.e_machine)
14025 {
14026 case EM_RISCV:
14027 return reloc_type == 36; /* R_RISCV_ADD64. */
14028 default:
14029 return false;
14030 }
14031 }
14032
14033 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14034 a 64-bit inplace sub RELA relocation used in DWARF debug sections. */
14035
14036 static bool
14037 is_64bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14038 {
14039 /* Please keep this table alpha-sorted for ease of visual lookup. */
14040 switch (filedata->file_header.e_machine)
14041 {
14042 case EM_RISCV:
14043 return reloc_type == 40; /* R_RISCV_SUB64. */
14044 default:
14045 return false;
14046 }
14047 }
14048
14049 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14050 a 16-bit inplace add RELA relocation used in DWARF debug sections. */
14051
14052 static bool
14053 is_16bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14054 {
14055 /* Please keep this table alpha-sorted for ease of visual lookup. */
14056 switch (filedata->file_header.e_machine)
14057 {
14058 case EM_RISCV:
14059 return reloc_type == 34; /* R_RISCV_ADD16. */
14060 default:
14061 return false;
14062 }
14063 }
14064
14065 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14066 a 16-bit inplace sub RELA relocation used in DWARF debug sections. */
14067
14068 static bool
14069 is_16bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14070 {
14071 /* Please keep this table alpha-sorted for ease of visual lookup. */
14072 switch (filedata->file_header.e_machine)
14073 {
14074 case EM_RISCV:
14075 return reloc_type == 38; /* R_RISCV_SUB16. */
14076 default:
14077 return false;
14078 }
14079 }
14080
14081 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14082 a 8-bit inplace add RELA relocation used in DWARF debug sections. */
14083
14084 static bool
14085 is_8bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14086 {
14087 /* Please keep this table alpha-sorted for ease of visual lookup. */
14088 switch (filedata->file_header.e_machine)
14089 {
14090 case EM_RISCV:
14091 return reloc_type == 33; /* R_RISCV_ADD8. */
14092 default:
14093 return false;
14094 }
14095 }
14096
14097 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14098 a 8-bit inplace sub RELA relocation used in DWARF debug sections. */
14099
14100 static bool
14101 is_8bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14102 {
14103 /* Please keep this table alpha-sorted for ease of visual lookup. */
14104 switch (filedata->file_header.e_machine)
14105 {
14106 case EM_RISCV:
14107 return reloc_type == 37; /* R_RISCV_SUB8. */
14108 default:
14109 return false;
14110 }
14111 }
14112
14113 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14114 a 6-bit inplace sub RELA relocation used in DWARF debug sections. */
14115
14116 static bool
14117 is_6bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14118 {
14119 switch (filedata->file_header.e_machine)
14120 {
14121 case EM_RISCV:
14122 return reloc_type == 52; /* R_RISCV_SUB6. */
14123 default:
14124 return false;
14125 }
14126 }
14127
14128 /* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
14129 relocation entries (possibly formerly used for SHT_GROUP sections). */
14130
14131 static bool
14132 is_none_reloc (Filedata * filedata, unsigned int reloc_type)
14133 {
14134 switch (filedata->file_header.e_machine)
14135 {
14136 case EM_386: /* R_386_NONE. */
14137 case EM_68K: /* R_68K_NONE. */
14138 case EM_ADAPTEVA_EPIPHANY:
14139 case EM_ALPHA: /* R_ALPHA_NONE. */
14140 case EM_ALTERA_NIOS2: /* R_NIOS2_NONE. */
14141 case EM_ARC: /* R_ARC_NONE. */
14142 case EM_ARC_COMPACT2: /* R_ARC_NONE. */
14143 case EM_ARC_COMPACT: /* R_ARC_NONE. */
14144 case EM_ARM: /* R_ARM_NONE. */
14145 case EM_C166: /* R_XC16X_NONE. */
14146 case EM_CRIS: /* R_CRIS_NONE. */
14147 case EM_FT32: /* R_FT32_NONE. */
14148 case EM_IA_64: /* R_IA64_NONE. */
14149 case EM_K1OM: /* R_X86_64_NONE. */
14150 case EM_L1OM: /* R_X86_64_NONE. */
14151 case EM_M32R: /* R_M32R_NONE. */
14152 case EM_MIPS: /* R_MIPS_NONE. */
14153 case EM_MN10300: /* R_MN10300_NONE. */
14154 case EM_MOXIE: /* R_MOXIE_NONE. */
14155 case EM_NIOS32: /* R_NIOS_NONE. */
14156 case EM_OR1K: /* R_OR1K_NONE. */
14157 case EM_PARISC: /* R_PARISC_NONE. */
14158 case EM_PPC64: /* R_PPC64_NONE. */
14159 case EM_PPC: /* R_PPC_NONE. */
14160 case EM_RISCV: /* R_RISCV_NONE. */
14161 case EM_S390: /* R_390_NONE. */
14162 case EM_S390_OLD:
14163 case EM_SH: /* R_SH_NONE. */
14164 case EM_SPARC32PLUS:
14165 case EM_SPARC: /* R_SPARC_NONE. */
14166 case EM_SPARCV9:
14167 case EM_TILEGX: /* R_TILEGX_NONE. */
14168 case EM_TILEPRO: /* R_TILEPRO_NONE. */
14169 case EM_TI_C6000:/* R_C6000_NONE. */
14170 case EM_X86_64: /* R_X86_64_NONE. */
14171 case EM_XC16X:
14172 case EM_Z80: /* R_Z80_NONE. */
14173 case EM_WEBASSEMBLY: /* R_WASM32_NONE. */
14174 return reloc_type == 0;
14175
14176 case EM_AARCH64:
14177 return reloc_type == 0 || reloc_type == 256;
14178 case EM_AVR_OLD:
14179 case EM_AVR:
14180 return (reloc_type == 0 /* R_AVR_NONE. */
14181 || reloc_type == 30 /* R_AVR_DIFF8. */
14182 || reloc_type == 31 /* R_AVR_DIFF16. */
14183 || reloc_type == 32 /* R_AVR_DIFF32. */);
14184 case EM_METAG:
14185 return reloc_type == 3; /* R_METAG_NONE. */
14186 case EM_NDS32:
14187 return (reloc_type == 0 /* R_XTENSA_NONE. */
14188 || reloc_type == 204 /* R_NDS32_DIFF8. */
14189 || reloc_type == 205 /* R_NDS32_DIFF16. */
14190 || reloc_type == 206 /* R_NDS32_DIFF32. */
14191 || reloc_type == 207 /* R_NDS32_ULEB128. */);
14192 case EM_TI_PRU:
14193 return (reloc_type == 0 /* R_PRU_NONE. */
14194 || reloc_type == 65 /* R_PRU_DIFF8. */
14195 || reloc_type == 66 /* R_PRU_DIFF16. */
14196 || reloc_type == 67 /* R_PRU_DIFF32. */);
14197 case EM_XTENSA_OLD:
14198 case EM_XTENSA:
14199 return (reloc_type == 0 /* R_XTENSA_NONE. */
14200 || reloc_type == 17 /* R_XTENSA_DIFF8. */
14201 || reloc_type == 18 /* R_XTENSA_DIFF16. */
14202 || reloc_type == 19 /* R_XTENSA_DIFF32. */
14203 || reloc_type == 57 /* R_XTENSA_PDIFF8. */
14204 || reloc_type == 58 /* R_XTENSA_PDIFF16. */
14205 || reloc_type == 59 /* R_XTENSA_PDIFF32. */
14206 || reloc_type == 60 /* R_XTENSA_NDIFF8. */
14207 || reloc_type == 61 /* R_XTENSA_NDIFF16. */
14208 || reloc_type == 62 /* R_XTENSA_NDIFF32. */);
14209 }
14210 return false;
14211 }
14212
14213 /* Returns TRUE if there is a relocation against
14214 section NAME at OFFSET bytes. */
14215
14216 bool
14217 reloc_at (struct dwarf_section * dsec, dwarf_vma offset)
14218 {
14219 Elf_Internal_Rela * relocs;
14220 Elf_Internal_Rela * rp;
14221
14222 if (dsec == NULL || dsec->reloc_info == NULL)
14223 return false;
14224
14225 relocs = (Elf_Internal_Rela *) dsec->reloc_info;
14226
14227 for (rp = relocs; rp < relocs + dsec->num_relocs; ++rp)
14228 if (rp->r_offset == offset)
14229 return true;
14230
14231 return false;
14232 }
14233
14234 /* Apply relocations to a section.
14235 Returns TRUE upon success, FALSE otherwise.
14236 If RELOCS_RETURN is non-NULL then it is set to point to the loaded relocs.
14237 It is then the caller's responsibility to free them. NUM_RELOCS_RETURN
14238 will be set to the number of relocs loaded.
14239
14240 Note: So far support has been added only for those relocations
14241 which can be found in debug sections. FIXME: Add support for
14242 more relocations ? */
14243
14244 static bool
14245 apply_relocations (Filedata * filedata,
14246 const Elf_Internal_Shdr * section,
14247 unsigned char * start,
14248 bfd_size_type size,
14249 void ** relocs_return,
14250 unsigned long * num_relocs_return)
14251 {
14252 Elf_Internal_Shdr * relsec;
14253 unsigned char * end = start + size;
14254
14255 if (relocs_return != NULL)
14256 {
14257 * (Elf_Internal_Rela **) relocs_return = NULL;
14258 * num_relocs_return = 0;
14259 }
14260
14261 if (filedata->file_header.e_type != ET_REL)
14262 /* No relocs to apply. */
14263 return true;
14264
14265 /* Find the reloc section associated with the section. */
14266 for (relsec = filedata->section_headers;
14267 relsec < filedata->section_headers + filedata->file_header.e_shnum;
14268 ++relsec)
14269 {
14270 bool is_rela;
14271 unsigned long num_relocs;
14272 Elf_Internal_Rela * relocs;
14273 Elf_Internal_Rela * rp;
14274 Elf_Internal_Shdr * symsec;
14275 Elf_Internal_Sym * symtab;
14276 unsigned long num_syms;
14277 Elf_Internal_Sym * sym;
14278
14279 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
14280 || relsec->sh_info >= filedata->file_header.e_shnum
14281 || filedata->section_headers + relsec->sh_info != section
14282 || relsec->sh_size == 0
14283 || relsec->sh_link >= filedata->file_header.e_shnum)
14284 continue;
14285
14286 symsec = filedata->section_headers + relsec->sh_link;
14287 if (symsec->sh_type != SHT_SYMTAB
14288 && symsec->sh_type != SHT_DYNSYM)
14289 return false;
14290
14291 is_rela = relsec->sh_type == SHT_RELA;
14292
14293 if (is_rela)
14294 {
14295 if (!slurp_rela_relocs (filedata, relsec->sh_offset,
14296 relsec->sh_size, & relocs, & num_relocs))
14297 return false;
14298 }
14299 else
14300 {
14301 if (!slurp_rel_relocs (filedata, relsec->sh_offset,
14302 relsec->sh_size, & relocs, & num_relocs))
14303 return false;
14304 }
14305
14306 /* SH uses RELA but uses in place value instead of the addend field. */
14307 if (filedata->file_header.e_machine == EM_SH)
14308 is_rela = false;
14309
14310 symtab = get_elf_symbols (filedata, symsec, & num_syms);
14311
14312 for (rp = relocs; rp < relocs + num_relocs; ++rp)
14313 {
14314 bfd_vma addend;
14315 unsigned int reloc_type;
14316 unsigned int reloc_size;
14317 bool reloc_inplace = false;
14318 bool reloc_subtract = false;
14319 unsigned char *rloc;
14320 unsigned long sym_index;
14321
14322 reloc_type = get_reloc_type (filedata, rp->r_info);
14323
14324 if (target_specific_reloc_handling (filedata, rp, start, end, symtab, num_syms))
14325 continue;
14326 else if (is_none_reloc (filedata, reloc_type))
14327 continue;
14328 else if (is_32bit_abs_reloc (filedata, reloc_type)
14329 || is_32bit_pcrel_reloc (filedata, reloc_type))
14330 reloc_size = 4;
14331 else if (is_64bit_abs_reloc (filedata, reloc_type)
14332 || is_64bit_pcrel_reloc (filedata, reloc_type))
14333 reloc_size = 8;
14334 else if (is_24bit_abs_reloc (filedata, reloc_type))
14335 reloc_size = 3;
14336 else if (is_16bit_abs_reloc (filedata, reloc_type))
14337 reloc_size = 2;
14338 else if (is_8bit_abs_reloc (filedata, reloc_type)
14339 || is_6bit_abs_reloc (filedata, reloc_type))
14340 reloc_size = 1;
14341 else if ((reloc_subtract = is_32bit_inplace_sub_reloc (filedata,
14342 reloc_type))
14343 || is_32bit_inplace_add_reloc (filedata, reloc_type))
14344 {
14345 reloc_size = 4;
14346 reloc_inplace = true;
14347 }
14348 else if ((reloc_subtract = is_64bit_inplace_sub_reloc (filedata,
14349 reloc_type))
14350 || is_64bit_inplace_add_reloc (filedata, reloc_type))
14351 {
14352 reloc_size = 8;
14353 reloc_inplace = true;
14354 }
14355 else if ((reloc_subtract = is_16bit_inplace_sub_reloc (filedata,
14356 reloc_type))
14357 || is_16bit_inplace_add_reloc (filedata, reloc_type))
14358 {
14359 reloc_size = 2;
14360 reloc_inplace = true;
14361 }
14362 else if ((reloc_subtract = is_8bit_inplace_sub_reloc (filedata,
14363 reloc_type))
14364 || is_8bit_inplace_add_reloc (filedata, reloc_type))
14365 {
14366 reloc_size = 1;
14367 reloc_inplace = true;
14368 }
14369 else if ((reloc_subtract = is_6bit_inplace_sub_reloc (filedata,
14370 reloc_type)))
14371 {
14372 reloc_size = 1;
14373 reloc_inplace = true;
14374 }
14375 else
14376 {
14377 static unsigned int prev_reloc = 0;
14378
14379 if (reloc_type != prev_reloc)
14380 warn (_("unable to apply unsupported reloc type %d to section %s\n"),
14381 reloc_type, printable_section_name (filedata, section));
14382 prev_reloc = reloc_type;
14383 continue;
14384 }
14385
14386 rloc = start + rp->r_offset;
14387 if (!IN_RANGE (start, end, rloc, reloc_size))
14388 {
14389 warn (_("skipping invalid relocation offset 0x%lx in section %s\n"),
14390 (unsigned long) rp->r_offset,
14391 printable_section_name (filedata, section));
14392 continue;
14393 }
14394
14395 sym_index = (unsigned long) get_reloc_symindex (rp->r_info);
14396 if (sym_index >= num_syms)
14397 {
14398 warn (_("skipping invalid relocation symbol index 0x%lx in section %s\n"),
14399 sym_index, printable_section_name (filedata, section));
14400 continue;
14401 }
14402 sym = symtab + sym_index;
14403
14404 /* If the reloc has a symbol associated with it,
14405 make sure that it is of an appropriate type.
14406
14407 Relocations against symbols without type can happen.
14408 Gcc -feliminate-dwarf2-dups may generate symbols
14409 without type for debug info.
14410
14411 Icc generates relocations against function symbols
14412 instead of local labels.
14413
14414 Relocations against object symbols can happen, eg when
14415 referencing a global array. For an example of this see
14416 the _clz.o binary in libgcc.a. */
14417 if (sym != symtab
14418 && ELF_ST_TYPE (sym->st_info) != STT_COMMON
14419 && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
14420 {
14421 warn (_("skipping unexpected symbol type %s in section %s relocation %ld\n"),
14422 get_symbol_type (filedata, ELF_ST_TYPE (sym->st_info)),
14423 printable_section_name (filedata, relsec),
14424 (long int)(rp - relocs));
14425 continue;
14426 }
14427
14428 addend = 0;
14429 if (is_rela)
14430 addend += rp->r_addend;
14431 /* R_XTENSA_32, R_PJ_DATA_DIR32 and R_D30V_32_NORMAL are
14432 partial_inplace. */
14433 if (!is_rela
14434 || (filedata->file_header.e_machine == EM_XTENSA
14435 && reloc_type == 1)
14436 || ((filedata->file_header.e_machine == EM_PJ
14437 || filedata->file_header.e_machine == EM_PJ_OLD)
14438 && reloc_type == 1)
14439 || ((filedata->file_header.e_machine == EM_D30V
14440 || filedata->file_header.e_machine == EM_CYGNUS_D30V)
14441 && reloc_type == 12)
14442 || reloc_inplace)
14443 {
14444 if (is_6bit_inplace_sub_reloc (filedata, reloc_type))
14445 addend += byte_get (rloc, reloc_size) & 0x3f;
14446 else
14447 addend += byte_get (rloc, reloc_size);
14448 }
14449
14450 if (is_32bit_pcrel_reloc (filedata, reloc_type)
14451 || is_64bit_pcrel_reloc (filedata, reloc_type))
14452 {
14453 /* On HPPA, all pc-relative relocations are biased by 8. */
14454 if (filedata->file_header.e_machine == EM_PARISC)
14455 addend -= 8;
14456 byte_put (rloc, (addend + sym->st_value) - rp->r_offset,
14457 reloc_size);
14458 }
14459 else if (is_6bit_abs_reloc (filedata, reloc_type)
14460 || is_6bit_inplace_sub_reloc (filedata, reloc_type))
14461 {
14462 if (reloc_subtract)
14463 addend -= sym->st_value;
14464 else
14465 addend += sym->st_value;
14466 addend = (addend & 0x3f) | (byte_get (rloc, reloc_size) & 0xc0);
14467 byte_put (rloc, addend, reloc_size);
14468 }
14469 else if (reloc_subtract)
14470 byte_put (rloc, addend - sym->st_value, reloc_size);
14471 else
14472 byte_put (rloc, addend + sym->st_value, reloc_size);
14473 }
14474
14475 free (symtab);
14476 /* Let the target specific reloc processing code know that
14477 we have finished with these relocs. */
14478 target_specific_reloc_handling (filedata, NULL, NULL, NULL, NULL, 0);
14479
14480 if (relocs_return)
14481 {
14482 * (Elf_Internal_Rela **) relocs_return = relocs;
14483 * num_relocs_return = num_relocs;
14484 }
14485 else
14486 free (relocs);
14487
14488 break;
14489 }
14490
14491 return true;
14492 }
14493
14494 #ifdef SUPPORT_DISASSEMBLY
14495 static bool
14496 disassemble_section (Elf_Internal_Shdr * section, Filedata * filedata)
14497 {
14498 printf (_("\nAssembly dump of section %s\n"), printable_section_name (filedata, section));
14499
14500 /* FIXME: XXX -- to be done --- XXX */
14501
14502 return true;
14503 }
14504 #endif
14505
14506 /* Reads in the contents of SECTION from FILE, returning a pointer
14507 to a malloc'ed buffer or NULL if something went wrong. */
14508
14509 static char *
14510 get_section_contents (Elf_Internal_Shdr * section, Filedata * filedata)
14511 {
14512 bfd_size_type num_bytes = section->sh_size;
14513
14514 if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
14515 {
14516 printf (_("Section '%s' has no data to dump.\n"),
14517 printable_section_name (filedata, section));
14518 return NULL;
14519 }
14520
14521 return (char *) get_data (NULL, filedata, section->sh_offset, 1, num_bytes,
14522 _("section contents"));
14523 }
14524
14525 /* Uncompresses a section that was compressed using zlib, in place. */
14526
14527 static bool
14528 uncompress_section_contents (unsigned char ** buffer,
14529 dwarf_size_type uncompressed_size,
14530 dwarf_size_type * size)
14531 {
14532 dwarf_size_type compressed_size = *size;
14533 unsigned char * compressed_buffer = *buffer;
14534 unsigned char * uncompressed_buffer;
14535 z_stream strm;
14536 int rc;
14537
14538 /* It is possible the section consists of several compressed
14539 buffers concatenated together, so we uncompress in a loop. */
14540 /* PR 18313: The state field in the z_stream structure is supposed
14541 to be invisible to the user (ie us), but some compilers will
14542 still complain about it being used without initialisation. So
14543 we first zero the entire z_stream structure and then set the fields
14544 that we need. */
14545 memset (& strm, 0, sizeof strm);
14546 strm.avail_in = compressed_size;
14547 strm.next_in = (Bytef *) compressed_buffer;
14548 strm.avail_out = uncompressed_size;
14549 uncompressed_buffer = (unsigned char *) xmalloc (uncompressed_size);
14550
14551 rc = inflateInit (& strm);
14552 while (strm.avail_in > 0)
14553 {
14554 if (rc != Z_OK)
14555 break;
14556 strm.next_out = ((Bytef *) uncompressed_buffer
14557 + (uncompressed_size - strm.avail_out));
14558 rc = inflate (&strm, Z_FINISH);
14559 if (rc != Z_STREAM_END)
14560 break;
14561 rc = inflateReset (& strm);
14562 }
14563 if (inflateEnd (& strm) != Z_OK
14564 || rc != Z_OK
14565 || strm.avail_out != 0)
14566 goto fail;
14567
14568 *buffer = uncompressed_buffer;
14569 *size = uncompressed_size;
14570 return true;
14571
14572 fail:
14573 free (uncompressed_buffer);
14574 /* Indicate decompression failure. */
14575 *buffer = NULL;
14576 return false;
14577 }
14578
14579 static bool
14580 dump_section_as_strings (Elf_Internal_Shdr * section, Filedata * filedata)
14581 {
14582 Elf_Internal_Shdr *relsec;
14583 bfd_size_type num_bytes;
14584 unsigned char *data;
14585 unsigned char *end;
14586 unsigned char *real_start;
14587 unsigned char *start;
14588 bool some_strings_shown;
14589
14590 real_start = start = (unsigned char *) get_section_contents (section, filedata);
14591 if (start == NULL)
14592 /* PR 21820: Do not fail if the section was empty. */
14593 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
14594
14595 num_bytes = section->sh_size;
14596
14597 if (filedata->is_separate)
14598 printf (_("\nString dump of section '%s' in linked file %s:\n"),
14599 printable_section_name (filedata, section),
14600 filedata->file_name);
14601 else
14602 printf (_("\nString dump of section '%s':\n"),
14603 printable_section_name (filedata, section));
14604
14605 if (decompress_dumps)
14606 {
14607 dwarf_size_type new_size = num_bytes;
14608 dwarf_size_type uncompressed_size = 0;
14609
14610 if ((section->sh_flags & SHF_COMPRESSED) != 0)
14611 {
14612 Elf_Internal_Chdr chdr;
14613 unsigned int compression_header_size
14614 = get_compression_header (& chdr, (unsigned char *) start,
14615 num_bytes);
14616 if (compression_header_size == 0)
14617 /* An error message will have already been generated
14618 by get_compression_header. */
14619 goto error_out;
14620
14621 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
14622 {
14623 warn (_("section '%s' has unsupported compress type: %d\n"),
14624 printable_section_name (filedata, section), chdr.ch_type);
14625 goto error_out;
14626 }
14627 uncompressed_size = chdr.ch_size;
14628 start += compression_header_size;
14629 new_size -= compression_header_size;
14630 }
14631 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
14632 {
14633 /* Read the zlib header. In this case, it should be "ZLIB"
14634 followed by the uncompressed section size, 8 bytes in
14635 big-endian order. */
14636 uncompressed_size = start[4]; uncompressed_size <<= 8;
14637 uncompressed_size += start[5]; uncompressed_size <<= 8;
14638 uncompressed_size += start[6]; uncompressed_size <<= 8;
14639 uncompressed_size += start[7]; uncompressed_size <<= 8;
14640 uncompressed_size += start[8]; uncompressed_size <<= 8;
14641 uncompressed_size += start[9]; uncompressed_size <<= 8;
14642 uncompressed_size += start[10]; uncompressed_size <<= 8;
14643 uncompressed_size += start[11];
14644 start += 12;
14645 new_size -= 12;
14646 }
14647
14648 if (uncompressed_size)
14649 {
14650 if (uncompress_section_contents (& start,
14651 uncompressed_size, & new_size))
14652 num_bytes = new_size;
14653 else
14654 {
14655 error (_("Unable to decompress section %s\n"),
14656 printable_section_name (filedata, section));
14657 goto error_out;
14658 }
14659 }
14660 else
14661 start = real_start;
14662 }
14663
14664 /* If the section being dumped has relocations against it the user might
14665 be expecting these relocations to have been applied. Check for this
14666 case and issue a warning message in order to avoid confusion.
14667 FIXME: Maybe we ought to have an option that dumps a section with
14668 relocs applied ? */
14669 for (relsec = filedata->section_headers;
14670 relsec < filedata->section_headers + filedata->file_header.e_shnum;
14671 ++relsec)
14672 {
14673 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
14674 || relsec->sh_info >= filedata->file_header.e_shnum
14675 || filedata->section_headers + relsec->sh_info != section
14676 || relsec->sh_size == 0
14677 || relsec->sh_link >= filedata->file_header.e_shnum)
14678 continue;
14679
14680 printf (_(" Note: This section has relocations against it, but these have NOT been applied to this dump.\n"));
14681 break;
14682 }
14683
14684 data = start;
14685 end = start + num_bytes;
14686 some_strings_shown = false;
14687
14688 #ifdef HAVE_MBSTATE_T
14689 mbstate_t state;
14690 /* Initialise the multibyte conversion state. */
14691 memset (& state, 0, sizeof (state));
14692 #endif
14693
14694 bool continuing = false;
14695
14696 while (data < end)
14697 {
14698 while (!ISPRINT (* data))
14699 if (++ data >= end)
14700 break;
14701
14702 if (data < end)
14703 {
14704 size_t maxlen = end - data;
14705
14706 if (continuing)
14707 {
14708 printf (" ");
14709 continuing = false;
14710 }
14711 else
14712 {
14713 printf (" [%6lx] ", (unsigned long) (data - start));
14714 }
14715
14716 if (maxlen > 0)
14717 {
14718 char c = 0;
14719
14720 while (maxlen)
14721 {
14722 c = *data++;
14723
14724 if (c == 0)
14725 break;
14726
14727 /* PR 25543: Treat new-lines as string-ending characters. */
14728 if (c == '\n')
14729 {
14730 printf ("\\n\n");
14731 if (*data != 0)
14732 continuing = true;
14733 break;
14734 }
14735
14736 /* Do not print control characters directly as they can affect terminal
14737 settings. Such characters usually appear in the names generated
14738 by the assembler for local labels. */
14739 if (ISCNTRL (c))
14740 {
14741 printf ("^%c", c + 0x40);
14742 }
14743 else if (ISPRINT (c))
14744 {
14745 putchar (c);
14746 }
14747 else
14748 {
14749 size_t n;
14750 #ifdef HAVE_MBSTATE_T
14751 wchar_t w;
14752 #endif
14753 /* Let printf do the hard work of displaying multibyte characters. */
14754 printf ("%.1s", data - 1);
14755 #ifdef HAVE_MBSTATE_T
14756 /* Try to find out how many bytes made up the character that was
14757 just printed. Advance the symbol pointer past the bytes that
14758 were displayed. */
14759 n = mbrtowc (& w, (char *)(data - 1), MB_CUR_MAX, & state);
14760 #else
14761 n = 1;
14762 #endif
14763 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
14764 data += (n - 1);
14765 }
14766 }
14767
14768 if (c != '\n')
14769 putchar ('\n');
14770 }
14771 else
14772 {
14773 printf (_("<corrupt>\n"));
14774 data = end;
14775 }
14776 some_strings_shown = true;
14777 }
14778 }
14779
14780 if (! some_strings_shown)
14781 printf (_(" No strings found in this section."));
14782
14783 free (real_start);
14784
14785 putchar ('\n');
14786 return true;
14787
14788 error_out:
14789 free (real_start);
14790 return false;
14791 }
14792
14793 static bool
14794 dump_section_as_bytes (Elf_Internal_Shdr *section,
14795 Filedata *filedata,
14796 bool relocate)
14797 {
14798 Elf_Internal_Shdr * relsec;
14799 bfd_size_type bytes;
14800 bfd_size_type section_size;
14801 bfd_vma addr;
14802 unsigned char * data;
14803 unsigned char * real_start;
14804 unsigned char * start;
14805
14806 real_start = start = (unsigned char *) get_section_contents (section, filedata);
14807 if (start == NULL)
14808 /* PR 21820: Do not fail if the section was empty. */
14809 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
14810
14811 section_size = section->sh_size;
14812
14813 if (filedata->is_separate)
14814 printf (_("\nHex dump of section '%s' in linked file %s:\n"),
14815 printable_section_name (filedata, section),
14816 filedata->file_name);
14817 else
14818 printf (_("\nHex dump of section '%s':\n"),
14819 printable_section_name (filedata, section));
14820
14821 if (decompress_dumps)
14822 {
14823 dwarf_size_type new_size = section_size;
14824 dwarf_size_type uncompressed_size = 0;
14825
14826 if ((section->sh_flags & SHF_COMPRESSED) != 0)
14827 {
14828 Elf_Internal_Chdr chdr;
14829 unsigned int compression_header_size
14830 = get_compression_header (& chdr, start, section_size);
14831
14832 if (compression_header_size == 0)
14833 /* An error message will have already been generated
14834 by get_compression_header. */
14835 goto error_out;
14836
14837 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
14838 {
14839 warn (_("section '%s' has unsupported compress type: %d\n"),
14840 printable_section_name (filedata, section), chdr.ch_type);
14841 goto error_out;
14842 }
14843 uncompressed_size = chdr.ch_size;
14844 start += compression_header_size;
14845 new_size -= compression_header_size;
14846 }
14847 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
14848 {
14849 /* Read the zlib header. In this case, it should be "ZLIB"
14850 followed by the uncompressed section size, 8 bytes in
14851 big-endian order. */
14852 uncompressed_size = start[4]; uncompressed_size <<= 8;
14853 uncompressed_size += start[5]; uncompressed_size <<= 8;
14854 uncompressed_size += start[6]; uncompressed_size <<= 8;
14855 uncompressed_size += start[7]; uncompressed_size <<= 8;
14856 uncompressed_size += start[8]; uncompressed_size <<= 8;
14857 uncompressed_size += start[9]; uncompressed_size <<= 8;
14858 uncompressed_size += start[10]; uncompressed_size <<= 8;
14859 uncompressed_size += start[11];
14860 start += 12;
14861 new_size -= 12;
14862 }
14863
14864 if (uncompressed_size)
14865 {
14866 if (uncompress_section_contents (& start, uncompressed_size,
14867 & new_size))
14868 {
14869 section_size = new_size;
14870 }
14871 else
14872 {
14873 error (_("Unable to decompress section %s\n"),
14874 printable_section_name (filedata, section));
14875 /* FIXME: Print the section anyway ? */
14876 goto error_out;
14877 }
14878 }
14879 else
14880 start = real_start;
14881 }
14882
14883 if (relocate)
14884 {
14885 if (! apply_relocations (filedata, section, start, section_size, NULL, NULL))
14886 goto error_out;
14887 }
14888 else
14889 {
14890 /* If the section being dumped has relocations against it the user might
14891 be expecting these relocations to have been applied. Check for this
14892 case and issue a warning message in order to avoid confusion.
14893 FIXME: Maybe we ought to have an option that dumps a section with
14894 relocs applied ? */
14895 for (relsec = filedata->section_headers;
14896 relsec < filedata->section_headers + filedata->file_header.e_shnum;
14897 ++relsec)
14898 {
14899 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
14900 || relsec->sh_info >= filedata->file_header.e_shnum
14901 || filedata->section_headers + relsec->sh_info != section
14902 || relsec->sh_size == 0
14903 || relsec->sh_link >= filedata->file_header.e_shnum)
14904 continue;
14905
14906 printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
14907 break;
14908 }
14909 }
14910
14911 addr = section->sh_addr;
14912 bytes = section_size;
14913 data = start;
14914
14915 while (bytes)
14916 {
14917 int j;
14918 int k;
14919 int lbytes;
14920
14921 lbytes = (bytes > 16 ? 16 : bytes);
14922
14923 printf (" 0x%8.8lx ", (unsigned long) addr);
14924
14925 for (j = 0; j < 16; j++)
14926 {
14927 if (j < lbytes)
14928 printf ("%2.2x", data[j]);
14929 else
14930 printf (" ");
14931
14932 if ((j & 3) == 3)
14933 printf (" ");
14934 }
14935
14936 for (j = 0; j < lbytes; j++)
14937 {
14938 k = data[j];
14939 if (k >= ' ' && k < 0x7f)
14940 printf ("%c", k);
14941 else
14942 printf (".");
14943 }
14944
14945 putchar ('\n');
14946
14947 data += lbytes;
14948 addr += lbytes;
14949 bytes -= lbytes;
14950 }
14951
14952 free (real_start);
14953
14954 putchar ('\n');
14955 return true;
14956
14957 error_out:
14958 free (real_start);
14959 return false;
14960 }
14961
14962 #ifdef ENABLE_LIBCTF
14963 static ctf_sect_t *
14964 shdr_to_ctf_sect (ctf_sect_t *buf, Elf_Internal_Shdr *shdr, Filedata *filedata)
14965 {
14966 buf->cts_name = SECTION_NAME_PRINT (shdr);
14967 buf->cts_size = shdr->sh_size;
14968 buf->cts_entsize = shdr->sh_entsize;
14969
14970 return buf;
14971 }
14972
14973 /* Formatting callback function passed to ctf_dump. Returns either the pointer
14974 it is passed, or a pointer to newly-allocated storage, in which case
14975 dump_ctf() will free it when it no longer needs it. */
14976
14977 static char *
14978 dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
14979 char *s, void *arg)
14980 {
14981 const char *blanks = arg;
14982 char *new_s;
14983
14984 if (asprintf (&new_s, "%s%s", blanks, s) < 0)
14985 return s;
14986 return new_s;
14987 }
14988
14989 /* Dump CTF errors/warnings. */
14990 static void
14991 dump_ctf_errs (ctf_dict_t *fp)
14992 {
14993 ctf_next_t *it = NULL;
14994 char *errtext;
14995 int is_warning;
14996 int err;
14997
14998 /* Dump accumulated errors and warnings. */
14999 while ((errtext = ctf_errwarning_next (fp, &it, &is_warning, &err)) != NULL)
15000 {
15001 error (_("%s: %s"), is_warning ? _("warning"): _("error"),
15002 errtext);
15003 free (errtext);
15004 }
15005 if (err != ECTF_NEXT_END)
15006 error (_("CTF error: cannot get CTF errors: `%s'"), ctf_errmsg (err));
15007 }
15008
15009 /* Dump one CTF archive member. */
15010
15011 static int
15012 dump_ctf_archive_member (ctf_dict_t *ctf, const char *name, void *arg)
15013 {
15014 ctf_dict_t *parent = (ctf_dict_t *) arg;
15015 const char *things[] = {"Header", "Labels", "Data objects",
15016 "Function objects", "Variables", "Types", "Strings",
15017 ""};
15018 const char **thing;
15019 size_t i;
15020 int err = 0;
15021
15022 /* Only print out the name of non-default-named archive members.
15023 The name .ctf appears everywhere, even for things that aren't
15024 really archives, so printing it out is liable to be confusing.
15025
15026 The parent, if there is one, is the default-owned archive member:
15027 avoid importing it into itself. (This does no harm, but looks
15028 confusing.) */
15029
15030 if (strcmp (name, ".ctf") != 0)
15031 {
15032 printf (_("\nCTF archive member: %s:\n"), name);
15033 ctf_import (ctf, parent);
15034 }
15035
15036 for (i = 0, thing = things; *thing[0]; thing++, i++)
15037 {
15038 ctf_dump_state_t *s = NULL;
15039 char *item;
15040
15041 printf ("\n %s:\n", *thing);
15042 while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
15043 (void *) " ")) != NULL)
15044 {
15045 printf ("%s\n", item);
15046 free (item);
15047 }
15048
15049 if (ctf_errno (ctf))
15050 {
15051 error (_("Iteration failed: %s, %s\n"), *thing,
15052 ctf_errmsg (ctf_errno (ctf)));
15053 err = 1;
15054 goto out;
15055 }
15056 }
15057
15058 out:
15059 dump_ctf_errs (ctf);
15060 return err;
15061 }
15062
15063 static bool
15064 dump_section_as_ctf (Elf_Internal_Shdr * section, Filedata * filedata)
15065 {
15066 Elf_Internal_Shdr * parent_sec = NULL;
15067 Elf_Internal_Shdr * symtab_sec = NULL;
15068 Elf_Internal_Shdr * strtab_sec = NULL;
15069 void * data = NULL;
15070 void * symdata = NULL;
15071 void * strdata = NULL;
15072 void * parentdata = NULL;
15073 ctf_sect_t ctfsect, symsect, strsect, parentsect;
15074 ctf_sect_t * symsectp = NULL;
15075 ctf_sect_t * strsectp = NULL;
15076 ctf_archive_t * ctfa = NULL;
15077 ctf_archive_t * parenta = NULL, *lookparent;
15078 ctf_dict_t * parent = NULL;
15079
15080 int err;
15081 bool ret = false;
15082
15083 shdr_to_ctf_sect (&ctfsect, section, filedata);
15084 data = get_section_contents (section, filedata);
15085 ctfsect.cts_data = data;
15086
15087 if (!dump_ctf_symtab_name)
15088 dump_ctf_symtab_name = strdup (".dynsym");
15089
15090 if (!dump_ctf_strtab_name)
15091 dump_ctf_strtab_name = strdup (".dynstr");
15092
15093 if (dump_ctf_symtab_name && dump_ctf_symtab_name[0] != 0)
15094 {
15095 if ((symtab_sec = find_section (filedata, dump_ctf_symtab_name)) == NULL)
15096 {
15097 error (_("No symbol section named %s\n"), dump_ctf_symtab_name);
15098 goto fail;
15099 }
15100 if ((symdata = (void *) get_data (NULL, filedata,
15101 symtab_sec->sh_offset, 1,
15102 symtab_sec->sh_size,
15103 _("symbols"))) == NULL)
15104 goto fail;
15105 symsectp = shdr_to_ctf_sect (&symsect, symtab_sec, filedata);
15106 symsect.cts_data = symdata;
15107 }
15108
15109 if (dump_ctf_strtab_name && dump_ctf_strtab_name[0] != 0)
15110 {
15111 if ((strtab_sec = find_section (filedata, dump_ctf_strtab_name)) == NULL)
15112 {
15113 error (_("No string table section named %s\n"),
15114 dump_ctf_strtab_name);
15115 goto fail;
15116 }
15117 if ((strdata = (void *) get_data (NULL, filedata,
15118 strtab_sec->sh_offset, 1,
15119 strtab_sec->sh_size,
15120 _("strings"))) == NULL)
15121 goto fail;
15122 strsectp = shdr_to_ctf_sect (&strsect, strtab_sec, filedata);
15123 strsect.cts_data = strdata;
15124 }
15125
15126 if (dump_ctf_parent_name)
15127 {
15128 if ((parent_sec = find_section (filedata, dump_ctf_parent_name)) == NULL)
15129 {
15130 error (_("No CTF parent section named %s\n"), dump_ctf_parent_name);
15131 goto fail;
15132 }
15133 if ((parentdata = (void *) get_data (NULL, filedata,
15134 parent_sec->sh_offset, 1,
15135 parent_sec->sh_size,
15136 _("CTF parent"))) == NULL)
15137 goto fail;
15138 shdr_to_ctf_sect (&parentsect, parent_sec, filedata);
15139 parentsect.cts_data = parentdata;
15140 }
15141
15142 /* Load the CTF file and dump it. It may be a raw CTF section, or an archive:
15143 libctf papers over the difference, so we can pretend it is always an
15144 archive. Possibly open the parent as well, if one was specified. */
15145
15146 if ((ctfa = ctf_arc_bufopen (&ctfsect, symsectp, strsectp, &err)) == NULL)
15147 {
15148 dump_ctf_errs (NULL);
15149 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15150 goto fail;
15151 }
15152
15153 ctf_arc_symsect_endianness (ctfa, filedata->file_header.e_ident[EI_DATA]
15154 != ELFDATA2MSB);
15155
15156 if (parentdata)
15157 {
15158 if ((parenta = ctf_arc_bufopen (&parentsect, symsectp, strsectp,
15159 &err)) == NULL)
15160 {
15161 dump_ctf_errs (NULL);
15162 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15163 goto fail;
15164 }
15165 lookparent = parenta;
15166 }
15167 else
15168 lookparent = ctfa;
15169
15170 /* Assume that the applicable parent archive member is the default one.
15171 (This is what all known implementations are expected to do, if they
15172 put CTFs and their parents in archives together.) */
15173 if ((parent = ctf_dict_open (lookparent, NULL, &err)) == NULL)
15174 {
15175 dump_ctf_errs (NULL);
15176 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15177 goto fail;
15178 }
15179
15180 ret = true;
15181
15182 if (filedata->is_separate)
15183 printf (_("\nDump of CTF section '%s' in linked file %s:\n"),
15184 printable_section_name (filedata, section),
15185 filedata->file_name);
15186 else
15187 printf (_("\nDump of CTF section '%s':\n"),
15188 printable_section_name (filedata, section));
15189
15190 if ((err = ctf_archive_iter (ctfa, dump_ctf_archive_member, parent)) != 0)
15191 {
15192 dump_ctf_errs (NULL);
15193 error (_("CTF member open failure: %s\n"), ctf_errmsg (err));
15194 ret = false;
15195 }
15196
15197 fail:
15198 ctf_dict_close (parent);
15199 ctf_close (ctfa);
15200 ctf_close (parenta);
15201 free (parentdata);
15202 free (data);
15203 free (symdata);
15204 free (strdata);
15205 return ret;
15206 }
15207 #endif
15208
15209 static bool
15210 load_specific_debug_section (enum dwarf_section_display_enum debug,
15211 const Elf_Internal_Shdr * sec,
15212 void * data)
15213 {
15214 struct dwarf_section * section = &debug_displays [debug].section;
15215 char buf [64];
15216 Filedata * filedata = (Filedata *) data;
15217
15218 if (section->start != NULL)
15219 {
15220 /* If it is already loaded, do nothing. */
15221 if (streq (section->filename, filedata->file_name))
15222 return true;
15223 free (section->start);
15224 }
15225
15226 snprintf (buf, sizeof (buf), _("%s section data"), section->name);
15227 section->address = sec->sh_addr;
15228 section->filename = filedata->file_name;
15229 section->start = (unsigned char *) get_data (NULL, filedata,
15230 sec->sh_offset, 1,
15231 sec->sh_size, buf);
15232 if (section->start == NULL)
15233 section->size = 0;
15234 else
15235 {
15236 unsigned char *start = section->start;
15237 dwarf_size_type size = sec->sh_size;
15238 dwarf_size_type uncompressed_size = 0;
15239
15240 if ((sec->sh_flags & SHF_COMPRESSED) != 0)
15241 {
15242 Elf_Internal_Chdr chdr;
15243 unsigned int compression_header_size;
15244
15245 if (size < (is_32bit_elf
15246 ? sizeof (Elf32_External_Chdr)
15247 : sizeof (Elf64_External_Chdr)))
15248 {
15249 warn (_("compressed section %s is too small to contain a compression header\n"),
15250 section->name);
15251 return false;
15252 }
15253
15254 compression_header_size = get_compression_header (&chdr, start, size);
15255 if (compression_header_size == 0)
15256 /* An error message will have already been generated
15257 by get_compression_header. */
15258 return false;
15259
15260 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
15261 {
15262 warn (_("section '%s' has unsupported compress type: %d\n"),
15263 section->name, chdr.ch_type);
15264 return false;
15265 }
15266 uncompressed_size = chdr.ch_size;
15267 start += compression_header_size;
15268 size -= compression_header_size;
15269 }
15270 else if (size > 12 && streq ((char *) start, "ZLIB"))
15271 {
15272 /* Read the zlib header. In this case, it should be "ZLIB"
15273 followed by the uncompressed section size, 8 bytes in
15274 big-endian order. */
15275 uncompressed_size = start[4]; uncompressed_size <<= 8;
15276 uncompressed_size += start[5]; uncompressed_size <<= 8;
15277 uncompressed_size += start[6]; uncompressed_size <<= 8;
15278 uncompressed_size += start[7]; uncompressed_size <<= 8;
15279 uncompressed_size += start[8]; uncompressed_size <<= 8;
15280 uncompressed_size += start[9]; uncompressed_size <<= 8;
15281 uncompressed_size += start[10]; uncompressed_size <<= 8;
15282 uncompressed_size += start[11];
15283 start += 12;
15284 size -= 12;
15285 }
15286
15287 if (uncompressed_size)
15288 {
15289 if (uncompress_section_contents (&start, uncompressed_size,
15290 &size))
15291 {
15292 /* Free the compressed buffer, update the section buffer
15293 and the section size if uncompress is successful. */
15294 free (section->start);
15295 section->start = start;
15296 }
15297 else
15298 {
15299 error (_("Unable to decompress section %s\n"),
15300 printable_section_name (filedata, sec));
15301 return false;
15302 }
15303 }
15304
15305 section->size = size;
15306 }
15307
15308 if (section->start == NULL)
15309 return false;
15310
15311 if (debug_displays [debug].relocate)
15312 {
15313 if (! apply_relocations (filedata, sec, section->start, section->size,
15314 & section->reloc_info, & section->num_relocs))
15315 return false;
15316 }
15317 else
15318 {
15319 section->reloc_info = NULL;
15320 section->num_relocs = 0;
15321 }
15322
15323 return true;
15324 }
15325
15326 #if HAVE_LIBDEBUGINFOD
15327 /* Return a hex string representation of the build-id. */
15328 unsigned char *
15329 get_build_id (void * data)
15330 {
15331 Filedata * filedata = (Filedata *) data;
15332 Elf_Internal_Shdr * shdr;
15333 unsigned long i;
15334
15335 /* Iterate through notes to find note.gnu.build-id.
15336 FIXME: Only the first note in any note section is examined. */
15337 for (i = 0, shdr = filedata->section_headers;
15338 i < filedata->file_header.e_shnum && shdr != NULL;
15339 i++, shdr++)
15340 {
15341 if (shdr->sh_type != SHT_NOTE)
15342 continue;
15343
15344 char * next;
15345 char * end;
15346 size_t data_remaining;
15347 size_t min_notesz;
15348 Elf_External_Note * enote;
15349 Elf_Internal_Note inote;
15350
15351 bfd_vma offset = shdr->sh_offset;
15352 bfd_vma align = shdr->sh_addralign;
15353 bfd_vma length = shdr->sh_size;
15354
15355 enote = (Elf_External_Note *) get_section_contents (shdr, filedata);
15356 if (enote == NULL)
15357 continue;
15358
15359 if (align < 4)
15360 align = 4;
15361 else if (align != 4 && align != 8)
15362 {
15363 free (enote);
15364 continue;
15365 }
15366
15367 end = (char *) enote + length;
15368 data_remaining = end - (char *) enote;
15369
15370 if (!is_ia64_vms (filedata))
15371 {
15372 min_notesz = offsetof (Elf_External_Note, name);
15373 if (data_remaining < min_notesz)
15374 {
15375 warn (_("\
15376 malformed note encountered in section %s whilst scanning for build-id note\n"),
15377 printable_section_name (filedata, shdr));
15378 free (enote);
15379 continue;
15380 }
15381 data_remaining -= min_notesz;
15382
15383 inote.type = BYTE_GET (enote->type);
15384 inote.namesz = BYTE_GET (enote->namesz);
15385 inote.namedata = enote->name;
15386 inote.descsz = BYTE_GET (enote->descsz);
15387 inote.descdata = ((char *) enote
15388 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
15389 inote.descpos = offset + (inote.descdata - (char *) enote);
15390 next = ((char *) enote
15391 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
15392 }
15393 else
15394 {
15395 Elf64_External_VMS_Note *vms_enote;
15396
15397 /* PR binutils/15191
15398 Make sure that there is enough data to read. */
15399 min_notesz = offsetof (Elf64_External_VMS_Note, name);
15400 if (data_remaining < min_notesz)
15401 {
15402 warn (_("\
15403 malformed note encountered in section %s whilst scanning for build-id note\n"),
15404 printable_section_name (filedata, shdr));
15405 free (enote);
15406 continue;
15407 }
15408 data_remaining -= min_notesz;
15409
15410 vms_enote = (Elf64_External_VMS_Note *) enote;
15411 inote.type = BYTE_GET (vms_enote->type);
15412 inote.namesz = BYTE_GET (vms_enote->namesz);
15413 inote.namedata = vms_enote->name;
15414 inote.descsz = BYTE_GET (vms_enote->descsz);
15415 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
15416 inote.descpos = offset + (inote.descdata - (char *) enote);
15417 next = inote.descdata + align_power (inote.descsz, 3);
15418 }
15419
15420 /* Skip malformed notes. */
15421 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
15422 || (size_t) (inote.descdata - inote.namedata) > data_remaining
15423 || (size_t) (next - inote.descdata) < inote.descsz
15424 || ((size_t) (next - inote.descdata)
15425 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
15426 {
15427 warn (_("\
15428 malformed note encountered in section %s whilst scanning for build-id note\n"),
15429 printable_section_name (filedata, shdr));
15430 free (enote);
15431 continue;
15432 }
15433
15434 /* Check if this is the build-id note. If so then convert the build-id
15435 bytes to a hex string. */
15436 if (inote.namesz > 0
15437 && startswith (inote.namedata, "GNU")
15438 && inote.type == NT_GNU_BUILD_ID)
15439 {
15440 unsigned long j;
15441 char * build_id;
15442
15443 build_id = malloc (inote.descsz * 2 + 1);
15444 if (build_id == NULL)
15445 {
15446 free (enote);
15447 return NULL;
15448 }
15449
15450 for (j = 0; j < inote.descsz; ++j)
15451 sprintf (build_id + (j * 2), "%02x", inote.descdata[j] & 0xff);
15452 build_id[inote.descsz * 2] = '\0';
15453 free (enote);
15454
15455 return (unsigned char *) build_id;
15456 }
15457 free (enote);
15458 }
15459
15460 return NULL;
15461 }
15462 #endif /* HAVE_LIBDEBUGINFOD */
15463
15464 /* If this is not NULL, load_debug_section will only look for sections
15465 within the list of sections given here. */
15466 static unsigned int * section_subset = NULL;
15467
15468 bool
15469 load_debug_section (enum dwarf_section_display_enum debug, void * data)
15470 {
15471 struct dwarf_section * section = &debug_displays [debug].section;
15472 Elf_Internal_Shdr * sec;
15473 Filedata * filedata = (Filedata *) data;
15474
15475 /* Without section headers we cannot find any sections. */
15476 if (filedata->section_headers == NULL)
15477 return false;
15478
15479 if (filedata->string_table == NULL
15480 && filedata->file_header.e_shstrndx != SHN_UNDEF
15481 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
15482 {
15483 Elf_Internal_Shdr * strs;
15484
15485 /* Read in the string table, so that we have section names to scan. */
15486 strs = filedata->section_headers + filedata->file_header.e_shstrndx;
15487
15488 if (strs != NULL && strs->sh_size != 0)
15489 {
15490 filedata->string_table
15491 = (char *) get_data (NULL, filedata, strs->sh_offset,
15492 1, strs->sh_size, _("string table"));
15493
15494 filedata->string_table_length
15495 = filedata->string_table != NULL ? strs->sh_size : 0;
15496 }
15497 }
15498
15499 /* Locate the debug section. */
15500 sec = find_section_in_set (filedata, section->uncompressed_name, section_subset);
15501 if (sec != NULL)
15502 section->name = section->uncompressed_name;
15503 else
15504 {
15505 sec = find_section_in_set (filedata, section->compressed_name, section_subset);
15506 if (sec != NULL)
15507 section->name = section->compressed_name;
15508 }
15509 if (sec == NULL)
15510 return false;
15511
15512 /* If we're loading from a subset of sections, and we've loaded
15513 a section matching this name before, it's likely that it's a
15514 different one. */
15515 if (section_subset != NULL)
15516 free_debug_section (debug);
15517
15518 return load_specific_debug_section (debug, sec, data);
15519 }
15520
15521 void
15522 free_debug_section (enum dwarf_section_display_enum debug)
15523 {
15524 struct dwarf_section * section = &debug_displays [debug].section;
15525
15526 if (section->start == NULL)
15527 return;
15528
15529 free ((char *) section->start);
15530 section->start = NULL;
15531 section->address = 0;
15532 section->size = 0;
15533
15534 free (section->reloc_info);
15535 section->reloc_info = NULL;
15536 section->num_relocs = 0;
15537 }
15538
15539 static bool
15540 display_debug_section (int shndx, Elf_Internal_Shdr * section, Filedata * filedata)
15541 {
15542 char * name = SECTION_NAME_VALID (section) ? SECTION_NAME (section) : "";
15543 const char * print_name = printable_section_name (filedata, section);
15544 bfd_size_type length;
15545 bool result = true;
15546 int i;
15547
15548 length = section->sh_size;
15549 if (length == 0)
15550 {
15551 printf (_("\nSection '%s' has no debugging data.\n"), print_name);
15552 return true;
15553 }
15554 if (section->sh_type == SHT_NOBITS)
15555 {
15556 /* There is no point in dumping the contents of a debugging section
15557 which has the NOBITS type - the bits in the file will be random.
15558 This can happen when a file containing a .eh_frame section is
15559 stripped with the --only-keep-debug command line option. */
15560 printf (_("section '%s' has the NOBITS type - its contents are unreliable.\n"),
15561 print_name);
15562 return false;
15563 }
15564
15565 if (startswith (name, ".gnu.linkonce.wi."))
15566 name = ".debug_info";
15567
15568 /* See if we know how to display the contents of this section. */
15569 for (i = 0; i < max; i++)
15570 {
15571 enum dwarf_section_display_enum id = (enum dwarf_section_display_enum) i;
15572 struct dwarf_section_display * display = debug_displays + i;
15573 struct dwarf_section * sec = & display->section;
15574
15575 if (streq (sec->uncompressed_name, name)
15576 || (id == line && startswith (name, ".debug_line."))
15577 || streq (sec->compressed_name, name))
15578 {
15579 bool secondary = (section != find_section (filedata, name));
15580
15581 if (secondary)
15582 free_debug_section (id);
15583
15584 if (i == line && startswith (name, ".debug_line."))
15585 sec->name = name;
15586 else if (streq (sec->uncompressed_name, name))
15587 sec->name = sec->uncompressed_name;
15588 else
15589 sec->name = sec->compressed_name;
15590
15591 if (load_specific_debug_section (id, section, filedata))
15592 {
15593 /* If this debug section is part of a CU/TU set in a .dwp file,
15594 restrict load_debug_section to the sections in that set. */
15595 section_subset = find_cu_tu_set (filedata, shndx);
15596
15597 result &= display->display (sec, filedata);
15598
15599 section_subset = NULL;
15600
15601 if (secondary || (id != info && id != abbrev && id != debug_addr))
15602 free_debug_section (id);
15603 }
15604 break;
15605 }
15606 }
15607
15608 if (i == max)
15609 {
15610 printf (_("Unrecognized debug section: %s\n"), print_name);
15611 result = false;
15612 }
15613
15614 return result;
15615 }
15616
15617 /* Set DUMP_SECTS for all sections where dumps were requested
15618 based on section name. */
15619
15620 static void
15621 initialise_dumps_byname (Filedata * filedata)
15622 {
15623 struct dump_list_entry * cur;
15624
15625 for (cur = dump_sects_byname; cur; cur = cur->next)
15626 {
15627 unsigned int i;
15628 bool any = false;
15629
15630 for (i = 0; i < filedata->file_header.e_shnum; i++)
15631 if (SECTION_NAME_VALID (filedata->section_headers + i)
15632 && streq (SECTION_NAME (filedata->section_headers + i), cur->name))
15633 {
15634 request_dump_bynumber (&filedata->dump, i, cur->type);
15635 any = true;
15636 }
15637
15638 if (!any && !filedata->is_separate)
15639 warn (_("Section '%s' was not dumped because it does not exist\n"),
15640 cur->name);
15641 }
15642 }
15643
15644 static bool
15645 process_section_contents (Filedata * filedata)
15646 {
15647 Elf_Internal_Shdr * section;
15648 unsigned int i;
15649 bool res = true;
15650
15651 if (! do_dump)
15652 return true;
15653
15654 initialise_dumps_byname (filedata);
15655
15656 for (i = 0, section = filedata->section_headers;
15657 i < filedata->file_header.e_shnum && i < filedata->dump.num_dump_sects;
15658 i++, section++)
15659 {
15660 dump_type dump = filedata->dump.dump_sects[i];
15661
15662 if (filedata->is_separate && ! process_links)
15663 dump &= DEBUG_DUMP;
15664
15665 #ifdef SUPPORT_DISASSEMBLY
15666 if (dump & DISASS_DUMP)
15667 {
15668 if (! disassemble_section (section, filedata))
15669 res = false;
15670 }
15671 #endif
15672 if (dump & HEX_DUMP)
15673 {
15674 if (! dump_section_as_bytes (section, filedata, false))
15675 res = false;
15676 }
15677
15678 if (dump & RELOC_DUMP)
15679 {
15680 if (! dump_section_as_bytes (section, filedata, true))
15681 res = false;
15682 }
15683
15684 if (dump & STRING_DUMP)
15685 {
15686 if (! dump_section_as_strings (section, filedata))
15687 res = false;
15688 }
15689
15690 if (dump & DEBUG_DUMP)
15691 {
15692 if (! display_debug_section (i, section, filedata))
15693 res = false;
15694 }
15695
15696 #ifdef ENABLE_LIBCTF
15697 if (dump & CTF_DUMP)
15698 {
15699 if (! dump_section_as_ctf (section, filedata))
15700 res = false;
15701 }
15702 #endif
15703 }
15704
15705 if (! filedata->is_separate)
15706 {
15707 /* Check to see if the user requested a
15708 dump of a section that does not exist. */
15709 for (; i < filedata->dump.num_dump_sects; i++)
15710 if (filedata->dump.dump_sects[i])
15711 {
15712 warn (_("Section %d was not dumped because it does not exist!\n"), i);
15713 res = false;
15714 }
15715 }
15716
15717 return res;
15718 }
15719
15720 static void
15721 process_mips_fpe_exception (int mask)
15722 {
15723 if (mask)
15724 {
15725 bool first = true;
15726
15727 if (mask & OEX_FPU_INEX)
15728 fputs ("INEX", stdout), first = false;
15729 if (mask & OEX_FPU_UFLO)
15730 printf ("%sUFLO", first ? "" : "|"), first = false;
15731 if (mask & OEX_FPU_OFLO)
15732 printf ("%sOFLO", first ? "" : "|"), first = false;
15733 if (mask & OEX_FPU_DIV0)
15734 printf ("%sDIV0", first ? "" : "|"), first = false;
15735 if (mask & OEX_FPU_INVAL)
15736 printf ("%sINVAL", first ? "" : "|");
15737 }
15738 else
15739 fputs ("0", stdout);
15740 }
15741
15742 /* Display's the value of TAG at location P. If TAG is
15743 greater than 0 it is assumed to be an unknown tag, and
15744 a message is printed to this effect. Otherwise it is
15745 assumed that a message has already been printed.
15746
15747 If the bottom bit of TAG is set it assumed to have a
15748 string value, otherwise it is assumed to have an integer
15749 value.
15750
15751 Returns an updated P pointing to the first unread byte
15752 beyond the end of TAG's value.
15753
15754 Reads at or beyond END will not be made. */
15755
15756 static unsigned char *
15757 display_tag_value (signed int tag,
15758 unsigned char * p,
15759 const unsigned char * const end)
15760 {
15761 unsigned long val;
15762
15763 if (tag > 0)
15764 printf (" Tag_unknown_%d: ", tag);
15765
15766 if (p >= end)
15767 {
15768 warn (_("<corrupt tag>\n"));
15769 }
15770 else if (tag & 1)
15771 {
15772 /* PR 17531 file: 027-19978-0.004. */
15773 size_t maxlen = (end - p) - 1;
15774
15775 putchar ('"');
15776 if (maxlen > 0)
15777 {
15778 print_symbol ((int) maxlen, (const char *) p);
15779 p += strnlen ((char *) p, maxlen) + 1;
15780 }
15781 else
15782 {
15783 printf (_("<corrupt string tag>"));
15784 p = (unsigned char *) end;
15785 }
15786 printf ("\"\n");
15787 }
15788 else
15789 {
15790 READ_ULEB (val, p, end);
15791 printf ("%ld (0x%lx)\n", val, val);
15792 }
15793
15794 assert (p <= end);
15795 return p;
15796 }
15797
15798 /* ARC ABI attributes section. */
15799
15800 static unsigned char *
15801 display_arc_attribute (unsigned char * p,
15802 const unsigned char * const end)
15803 {
15804 unsigned int tag;
15805 unsigned int val;
15806
15807 READ_ULEB (tag, p, end);
15808
15809 switch (tag)
15810 {
15811 case Tag_ARC_PCS_config:
15812 READ_ULEB (val, p, end);
15813 printf (" Tag_ARC_PCS_config: ");
15814 switch (val)
15815 {
15816 case 0:
15817 printf (_("Absent/Non standard\n"));
15818 break;
15819 case 1:
15820 printf (_("Bare metal/mwdt\n"));
15821 break;
15822 case 2:
15823 printf (_("Bare metal/newlib\n"));
15824 break;
15825 case 3:
15826 printf (_("Linux/uclibc\n"));
15827 break;
15828 case 4:
15829 printf (_("Linux/glibc\n"));
15830 break;
15831 default:
15832 printf (_("Unknown\n"));
15833 break;
15834 }
15835 break;
15836
15837 case Tag_ARC_CPU_base:
15838 READ_ULEB (val, p, end);
15839 printf (" Tag_ARC_CPU_base: ");
15840 switch (val)
15841 {
15842 default:
15843 case TAG_CPU_NONE:
15844 printf (_("Absent\n"));
15845 break;
15846 case TAG_CPU_ARC6xx:
15847 printf ("ARC6xx\n");
15848 break;
15849 case TAG_CPU_ARC7xx:
15850 printf ("ARC7xx\n");
15851 break;
15852 case TAG_CPU_ARCEM:
15853 printf ("ARCEM\n");
15854 break;
15855 case TAG_CPU_ARCHS:
15856 printf ("ARCHS\n");
15857 break;
15858 }
15859 break;
15860
15861 case Tag_ARC_CPU_variation:
15862 READ_ULEB (val, p, end);
15863 printf (" Tag_ARC_CPU_variation: ");
15864 switch (val)
15865 {
15866 default:
15867 if (val > 0 && val < 16)
15868 printf ("Core%d\n", val);
15869 else
15870 printf ("Unknown\n");
15871 break;
15872
15873 case 0:
15874 printf (_("Absent\n"));
15875 break;
15876 }
15877 break;
15878
15879 case Tag_ARC_CPU_name:
15880 printf (" Tag_ARC_CPU_name: ");
15881 p = display_tag_value (-1, p, end);
15882 break;
15883
15884 case Tag_ARC_ABI_rf16:
15885 READ_ULEB (val, p, end);
15886 printf (" Tag_ARC_ABI_rf16: %s\n", val ? _("yes") : _("no"));
15887 break;
15888
15889 case Tag_ARC_ABI_osver:
15890 READ_ULEB (val, p, end);
15891 printf (" Tag_ARC_ABI_osver: v%d\n", val);
15892 break;
15893
15894 case Tag_ARC_ABI_pic:
15895 case Tag_ARC_ABI_sda:
15896 READ_ULEB (val, p, end);
15897 printf (tag == Tag_ARC_ABI_sda ? " Tag_ARC_ABI_sda: "
15898 : " Tag_ARC_ABI_pic: ");
15899 switch (val)
15900 {
15901 case 0:
15902 printf (_("Absent\n"));
15903 break;
15904 case 1:
15905 printf ("MWDT\n");
15906 break;
15907 case 2:
15908 printf ("GNU\n");
15909 break;
15910 default:
15911 printf (_("Unknown\n"));
15912 break;
15913 }
15914 break;
15915
15916 case Tag_ARC_ABI_tls:
15917 READ_ULEB (val, p, end);
15918 printf (" Tag_ARC_ABI_tls: %s\n", val ? "r25": "none");
15919 break;
15920
15921 case Tag_ARC_ABI_enumsize:
15922 READ_ULEB (val, p, end);
15923 printf (" Tag_ARC_ABI_enumsize: %s\n", val ? _("default") :
15924 _("smallest"));
15925 break;
15926
15927 case Tag_ARC_ABI_exceptions:
15928 READ_ULEB (val, p, end);
15929 printf (" Tag_ARC_ABI_exceptions: %s\n", val ? _("OPTFP")
15930 : _("default"));
15931 break;
15932
15933 case Tag_ARC_ABI_double_size:
15934 READ_ULEB (val, p, end);
15935 printf (" Tag_ARC_ABI_double_size: %d\n", val);
15936 break;
15937
15938 case Tag_ARC_ISA_config:
15939 printf (" Tag_ARC_ISA_config: ");
15940 p = display_tag_value (-1, p, end);
15941 break;
15942
15943 case Tag_ARC_ISA_apex:
15944 printf (" Tag_ARC_ISA_apex: ");
15945 p = display_tag_value (-1, p, end);
15946 break;
15947
15948 case Tag_ARC_ISA_mpy_option:
15949 READ_ULEB (val, p, end);
15950 printf (" Tag_ARC_ISA_mpy_option: %d\n", val);
15951 break;
15952
15953 case Tag_ARC_ATR_version:
15954 READ_ULEB (val, p, end);
15955 printf (" Tag_ARC_ATR_version: %d\n", val);
15956 break;
15957
15958 default:
15959 return display_tag_value (tag & 1, p, end);
15960 }
15961
15962 return p;
15963 }
15964
15965 /* ARM EABI attributes section. */
15966 typedef struct
15967 {
15968 unsigned int tag;
15969 const char * name;
15970 /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup. */
15971 unsigned int type;
15972 const char *const *table;
15973 } arm_attr_public_tag;
15974
15975 static const char *const arm_attr_tag_CPU_arch[] =
15976 {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
15977 "v6K", "v7", "v6-M", "v6S-M", "v7E-M", "v8", "v8-R", "v8-M.baseline",
15978 "v8-M.mainline", "", "", "", "v8.1-M.mainline"};
15979 static const char *const arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
15980 static const char *const arm_attr_tag_THUMB_ISA_use[] =
15981 {"No", "Thumb-1", "Thumb-2", "Yes"};
15982 static const char *const arm_attr_tag_FP_arch[] =
15983 {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16", "VFPv4", "VFPv4-D16",
15984 "FP for ARMv8", "FPv5/FP-D16 for ARMv8"};
15985 static const char *const arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
15986 static const char *const arm_attr_tag_Advanced_SIMD_arch[] =
15987 {"No", "NEONv1", "NEONv1 with Fused-MAC", "NEON for ARMv8",
15988 "NEON for ARMv8.1"};
15989 static const char *const arm_attr_tag_PCS_config[] =
15990 {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
15991 "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
15992 static const char *const arm_attr_tag_ABI_PCS_R9_use[] =
15993 {"V6", "SB", "TLS", "Unused"};
15994 static const char *const arm_attr_tag_ABI_PCS_RW_data[] =
15995 {"Absolute", "PC-relative", "SB-relative", "None"};
15996 static const char *const arm_attr_tag_ABI_PCS_RO_data[] =
15997 {"Absolute", "PC-relative", "None"};
15998 static const char *const arm_attr_tag_ABI_PCS_GOT_use[] =
15999 {"None", "direct", "GOT-indirect"};
16000 static const char *const arm_attr_tag_ABI_PCS_wchar_t[] =
16001 {"None", "??? 1", "2", "??? 3", "4"};
16002 static const char *const arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
16003 static const char *const arm_attr_tag_ABI_FP_denormal[] =
16004 {"Unused", "Needed", "Sign only"};
16005 static const char *const arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
16006 static const char *const arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
16007 static const char *const arm_attr_tag_ABI_FP_number_model[] =
16008 {"Unused", "Finite", "RTABI", "IEEE 754"};
16009 static const char *const arm_attr_tag_ABI_enum_size[] =
16010 {"Unused", "small", "int", "forced to int"};
16011 static const char *const arm_attr_tag_ABI_HardFP_use[] =
16012 {"As Tag_FP_arch", "SP only", "Reserved", "Deprecated"};
16013 static const char *const arm_attr_tag_ABI_VFP_args[] =
16014 {"AAPCS", "VFP registers", "custom", "compatible"};
16015 static const char *const arm_attr_tag_ABI_WMMX_args[] =
16016 {"AAPCS", "WMMX registers", "custom"};
16017 static const char *const arm_attr_tag_ABI_optimization_goals[] =
16018 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
16019 "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
16020 static const char *const arm_attr_tag_ABI_FP_optimization_goals[] =
16021 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
16022 "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
16023 static const char *const arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
16024 static const char *const arm_attr_tag_FP_HP_extension[] =
16025 {"Not Allowed", "Allowed"};
16026 static const char *const arm_attr_tag_ABI_FP_16bit_format[] =
16027 {"None", "IEEE 754", "Alternative Format"};
16028 static const char *const arm_attr_tag_DSP_extension[] =
16029 {"Follow architecture", "Allowed"};
16030 static const char *const arm_attr_tag_MPextension_use[] =
16031 {"Not Allowed", "Allowed"};
16032 static const char *const arm_attr_tag_DIV_use[] =
16033 {"Allowed in Thumb-ISA, v7-R or v7-M", "Not allowed",
16034 "Allowed in v7-A with integer division extension"};
16035 static const char *const arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
16036 static const char *const arm_attr_tag_Virtualization_use[] =
16037 {"Not Allowed", "TrustZone", "Virtualization Extensions",
16038 "TrustZone and Virtualization Extensions"};
16039 static const char *const arm_attr_tag_MPextension_use_legacy[] =
16040 {"Not Allowed", "Allowed"};
16041
16042 static const char *const arm_attr_tag_MVE_arch[] =
16043 {"No MVE", "MVE Integer only", "MVE Integer and FP"};
16044
16045 #define LOOKUP(id, name) \
16046 {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
16047 static arm_attr_public_tag arm_attr_public_tags[] =
16048 {
16049 {4, "CPU_raw_name", 1, NULL},
16050 {5, "CPU_name", 1, NULL},
16051 LOOKUP(6, CPU_arch),
16052 {7, "CPU_arch_profile", 0, NULL},
16053 LOOKUP(8, ARM_ISA_use),
16054 LOOKUP(9, THUMB_ISA_use),
16055 LOOKUP(10, FP_arch),
16056 LOOKUP(11, WMMX_arch),
16057 LOOKUP(12, Advanced_SIMD_arch),
16058 LOOKUP(13, PCS_config),
16059 LOOKUP(14, ABI_PCS_R9_use),
16060 LOOKUP(15, ABI_PCS_RW_data),
16061 LOOKUP(16, ABI_PCS_RO_data),
16062 LOOKUP(17, ABI_PCS_GOT_use),
16063 LOOKUP(18, ABI_PCS_wchar_t),
16064 LOOKUP(19, ABI_FP_rounding),
16065 LOOKUP(20, ABI_FP_denormal),
16066 LOOKUP(21, ABI_FP_exceptions),
16067 LOOKUP(22, ABI_FP_user_exceptions),
16068 LOOKUP(23, ABI_FP_number_model),
16069 {24, "ABI_align_needed", 0, NULL},
16070 {25, "ABI_align_preserved", 0, NULL},
16071 LOOKUP(26, ABI_enum_size),
16072 LOOKUP(27, ABI_HardFP_use),
16073 LOOKUP(28, ABI_VFP_args),
16074 LOOKUP(29, ABI_WMMX_args),
16075 LOOKUP(30, ABI_optimization_goals),
16076 LOOKUP(31, ABI_FP_optimization_goals),
16077 {32, "compatibility", 0, NULL},
16078 LOOKUP(34, CPU_unaligned_access),
16079 LOOKUP(36, FP_HP_extension),
16080 LOOKUP(38, ABI_FP_16bit_format),
16081 LOOKUP(42, MPextension_use),
16082 LOOKUP(44, DIV_use),
16083 LOOKUP(46, DSP_extension),
16084 LOOKUP(48, MVE_arch),
16085 {64, "nodefaults", 0, NULL},
16086 {65, "also_compatible_with", 0, NULL},
16087 LOOKUP(66, T2EE_use),
16088 {67, "conformance", 1, NULL},
16089 LOOKUP(68, Virtualization_use),
16090 LOOKUP(70, MPextension_use_legacy)
16091 };
16092 #undef LOOKUP
16093
16094 static unsigned char *
16095 display_arm_attribute (unsigned char * p,
16096 const unsigned char * const end)
16097 {
16098 unsigned int tag;
16099 unsigned int val;
16100 arm_attr_public_tag * attr;
16101 unsigned i;
16102 unsigned int type;
16103
16104 READ_ULEB (tag, p, end);
16105 attr = NULL;
16106 for (i = 0; i < ARRAY_SIZE (arm_attr_public_tags); i++)
16107 {
16108 if (arm_attr_public_tags[i].tag == tag)
16109 {
16110 attr = &arm_attr_public_tags[i];
16111 break;
16112 }
16113 }
16114
16115 if (attr)
16116 {
16117 printf (" Tag_%s: ", attr->name);
16118 switch (attr->type)
16119 {
16120 case 0:
16121 switch (tag)
16122 {
16123 case 7: /* Tag_CPU_arch_profile. */
16124 READ_ULEB (val, p, end);
16125 switch (val)
16126 {
16127 case 0: printf (_("None\n")); break;
16128 case 'A': printf (_("Application\n")); break;
16129 case 'R': printf (_("Realtime\n")); break;
16130 case 'M': printf (_("Microcontroller\n")); break;
16131 case 'S': printf (_("Application or Realtime\n")); break;
16132 default: printf ("??? (%d)\n", val); break;
16133 }
16134 break;
16135
16136 case 24: /* Tag_align_needed. */
16137 READ_ULEB (val, p, end);
16138 switch (val)
16139 {
16140 case 0: printf (_("None\n")); break;
16141 case 1: printf (_("8-byte\n")); break;
16142 case 2: printf (_("4-byte\n")); break;
16143 case 3: printf ("??? 3\n"); break;
16144 default:
16145 if (val <= 12)
16146 printf (_("8-byte and up to %d-byte extended\n"),
16147 1 << val);
16148 else
16149 printf ("??? (%d)\n", val);
16150 break;
16151 }
16152 break;
16153
16154 case 25: /* Tag_align_preserved. */
16155 READ_ULEB (val, p, end);
16156 switch (val)
16157 {
16158 case 0: printf (_("None\n")); break;
16159 case 1: printf (_("8-byte, except leaf SP\n")); break;
16160 case 2: printf (_("8-byte\n")); break;
16161 case 3: printf ("??? 3\n"); break;
16162 default:
16163 if (val <= 12)
16164 printf (_("8-byte and up to %d-byte extended\n"),
16165 1 << val);
16166 else
16167 printf ("??? (%d)\n", val);
16168 break;
16169 }
16170 break;
16171
16172 case 32: /* Tag_compatibility. */
16173 {
16174 READ_ULEB (val, p, end);
16175 printf (_("flag = %d, vendor = "), val);
16176 if (p < end - 1)
16177 {
16178 size_t maxlen = (end - p) - 1;
16179
16180 print_symbol ((int) maxlen, (const char *) p);
16181 p += strnlen ((char *) p, maxlen) + 1;
16182 }
16183 else
16184 {
16185 printf (_("<corrupt>"));
16186 p = (unsigned char *) end;
16187 }
16188 putchar ('\n');
16189 }
16190 break;
16191
16192 case 64: /* Tag_nodefaults. */
16193 /* PR 17531: file: 001-505008-0.01. */
16194 if (p < end)
16195 p++;
16196 printf (_("True\n"));
16197 break;
16198
16199 case 65: /* Tag_also_compatible_with. */
16200 READ_ULEB (val, p, end);
16201 if (val == 6 /* Tag_CPU_arch. */)
16202 {
16203 READ_ULEB (val, p, end);
16204 if ((unsigned int) val >= ARRAY_SIZE (arm_attr_tag_CPU_arch))
16205 printf ("??? (%d)\n", val);
16206 else
16207 printf ("%s\n", arm_attr_tag_CPU_arch[val]);
16208 }
16209 else
16210 printf ("???\n");
16211 while (p < end && *(p++) != '\0' /* NUL terminator. */)
16212 ;
16213 break;
16214
16215 default:
16216 printf (_("<unknown: %d>\n"), tag);
16217 break;
16218 }
16219 return p;
16220
16221 case 1:
16222 return display_tag_value (-1, p, end);
16223 case 2:
16224 return display_tag_value (0, p, end);
16225
16226 default:
16227 assert (attr->type & 0x80);
16228 READ_ULEB (val, p, end);
16229 type = attr->type & 0x7f;
16230 if (val >= type)
16231 printf ("??? (%d)\n", val);
16232 else
16233 printf ("%s\n", attr->table[val]);
16234 return p;
16235 }
16236 }
16237
16238 return display_tag_value (tag, p, end);
16239 }
16240
16241 static unsigned char *
16242 display_gnu_attribute (unsigned char * p,
16243 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const),
16244 const unsigned char * const end)
16245 {
16246 unsigned int tag;
16247 unsigned int val;
16248
16249 READ_ULEB (tag, p, end);
16250
16251 /* Tag_compatibility is the only generic GNU attribute defined at
16252 present. */
16253 if (tag == 32)
16254 {
16255 READ_ULEB (val, p, end);
16256
16257 printf (_("flag = %d, vendor = "), val);
16258 if (p == end)
16259 {
16260 printf (_("<corrupt>\n"));
16261 warn (_("corrupt vendor attribute\n"));
16262 }
16263 else
16264 {
16265 if (p < end - 1)
16266 {
16267 size_t maxlen = (end - p) - 1;
16268
16269 print_symbol ((int) maxlen, (const char *) p);
16270 p += strnlen ((char *) p, maxlen) + 1;
16271 }
16272 else
16273 {
16274 printf (_("<corrupt>"));
16275 p = (unsigned char *) end;
16276 }
16277 putchar ('\n');
16278 }
16279 return p;
16280 }
16281
16282 if ((tag & 2) == 0 && display_proc_gnu_attribute)
16283 return display_proc_gnu_attribute (p, tag, end);
16284
16285 return display_tag_value (tag, p, end);
16286 }
16287
16288 static unsigned char *
16289 display_m68k_gnu_attribute (unsigned char * p,
16290 unsigned int tag,
16291 const unsigned char * const end)
16292 {
16293 unsigned int val;
16294
16295 if (tag == Tag_GNU_M68K_ABI_FP)
16296 {
16297 printf (" Tag_GNU_M68K_ABI_FP: ");
16298 if (p == end)
16299 {
16300 printf (_("<corrupt>\n"));
16301 return p;
16302 }
16303 READ_ULEB (val, p, end);
16304
16305 if (val > 3)
16306 printf ("(%#x), ", val);
16307
16308 switch (val & 3)
16309 {
16310 case 0:
16311 printf (_("unspecified hard/soft float\n"));
16312 break;
16313 case 1:
16314 printf (_("hard float\n"));
16315 break;
16316 case 2:
16317 printf (_("soft float\n"));
16318 break;
16319 }
16320 return p;
16321 }
16322
16323 return display_tag_value (tag & 1, p, end);
16324 }
16325
16326 static unsigned char *
16327 display_power_gnu_attribute (unsigned char * p,
16328 unsigned int tag,
16329 const unsigned char * const end)
16330 {
16331 unsigned int val;
16332
16333 if (tag == Tag_GNU_Power_ABI_FP)
16334 {
16335 printf (" Tag_GNU_Power_ABI_FP: ");
16336 if (p == end)
16337 {
16338 printf (_("<corrupt>\n"));
16339 return p;
16340 }
16341 READ_ULEB (val, p, end);
16342
16343 if (val > 15)
16344 printf ("(%#x), ", val);
16345
16346 switch (val & 3)
16347 {
16348 case 0:
16349 printf (_("unspecified hard/soft float, "));
16350 break;
16351 case 1:
16352 printf (_("hard float, "));
16353 break;
16354 case 2:
16355 printf (_("soft float, "));
16356 break;
16357 case 3:
16358 printf (_("single-precision hard float, "));
16359 break;
16360 }
16361
16362 switch (val & 0xC)
16363 {
16364 case 0:
16365 printf (_("unspecified long double\n"));
16366 break;
16367 case 4:
16368 printf (_("128-bit IBM long double\n"));
16369 break;
16370 case 8:
16371 printf (_("64-bit long double\n"));
16372 break;
16373 case 12:
16374 printf (_("128-bit IEEE long double\n"));
16375 break;
16376 }
16377 return p;
16378 }
16379
16380 if (tag == Tag_GNU_Power_ABI_Vector)
16381 {
16382 printf (" Tag_GNU_Power_ABI_Vector: ");
16383 if (p == end)
16384 {
16385 printf (_("<corrupt>\n"));
16386 return p;
16387 }
16388 READ_ULEB (val, p, end);
16389
16390 if (val > 3)
16391 printf ("(%#x), ", val);
16392
16393 switch (val & 3)
16394 {
16395 case 0:
16396 printf (_("unspecified\n"));
16397 break;
16398 case 1:
16399 printf (_("generic\n"));
16400 break;
16401 case 2:
16402 printf ("AltiVec\n");
16403 break;
16404 case 3:
16405 printf ("SPE\n");
16406 break;
16407 }
16408 return p;
16409 }
16410
16411 if (tag == Tag_GNU_Power_ABI_Struct_Return)
16412 {
16413 printf (" Tag_GNU_Power_ABI_Struct_Return: ");
16414 if (p == end)
16415 {
16416 printf (_("<corrupt>\n"));
16417 return p;
16418 }
16419 READ_ULEB (val, p, end);
16420
16421 if (val > 2)
16422 printf ("(%#x), ", val);
16423
16424 switch (val & 3)
16425 {
16426 case 0:
16427 printf (_("unspecified\n"));
16428 break;
16429 case 1:
16430 printf ("r3/r4\n");
16431 break;
16432 case 2:
16433 printf (_("memory\n"));
16434 break;
16435 case 3:
16436 printf ("???\n");
16437 break;
16438 }
16439 return p;
16440 }
16441
16442 return display_tag_value (tag & 1, p, end);
16443 }
16444
16445 static unsigned char *
16446 display_s390_gnu_attribute (unsigned char * p,
16447 unsigned int tag,
16448 const unsigned char * const end)
16449 {
16450 unsigned int val;
16451
16452 if (tag == Tag_GNU_S390_ABI_Vector)
16453 {
16454 printf (" Tag_GNU_S390_ABI_Vector: ");
16455 READ_ULEB (val, p, end);
16456
16457 switch (val)
16458 {
16459 case 0:
16460 printf (_("any\n"));
16461 break;
16462 case 1:
16463 printf (_("software\n"));
16464 break;
16465 case 2:
16466 printf (_("hardware\n"));
16467 break;
16468 default:
16469 printf ("??? (%d)\n", val);
16470 break;
16471 }
16472 return p;
16473 }
16474
16475 return display_tag_value (tag & 1, p, end);
16476 }
16477
16478 static void
16479 display_sparc_hwcaps (unsigned int mask)
16480 {
16481 if (mask)
16482 {
16483 bool first = true;
16484
16485 if (mask & ELF_SPARC_HWCAP_MUL32)
16486 fputs ("mul32", stdout), first = false;
16487 if (mask & ELF_SPARC_HWCAP_DIV32)
16488 printf ("%sdiv32", first ? "" : "|"), first = false;
16489 if (mask & ELF_SPARC_HWCAP_FSMULD)
16490 printf ("%sfsmuld", first ? "" : "|"), first = false;
16491 if (mask & ELF_SPARC_HWCAP_V8PLUS)
16492 printf ("%sv8plus", first ? "" : "|"), first = false;
16493 if (mask & ELF_SPARC_HWCAP_POPC)
16494 printf ("%spopc", first ? "" : "|"), first = false;
16495 if (mask & ELF_SPARC_HWCAP_VIS)
16496 printf ("%svis", first ? "" : "|"), first = false;
16497 if (mask & ELF_SPARC_HWCAP_VIS2)
16498 printf ("%svis2", first ? "" : "|"), first = false;
16499 if (mask & ELF_SPARC_HWCAP_ASI_BLK_INIT)
16500 printf ("%sASIBlkInit", first ? "" : "|"), first = false;
16501 if (mask & ELF_SPARC_HWCAP_FMAF)
16502 printf ("%sfmaf", first ? "" : "|"), first = false;
16503 if (mask & ELF_SPARC_HWCAP_VIS3)
16504 printf ("%svis3", first ? "" : "|"), first = false;
16505 if (mask & ELF_SPARC_HWCAP_HPC)
16506 printf ("%shpc", first ? "" : "|"), first = false;
16507 if (mask & ELF_SPARC_HWCAP_RANDOM)
16508 printf ("%srandom", first ? "" : "|"), first = false;
16509 if (mask & ELF_SPARC_HWCAP_TRANS)
16510 printf ("%strans", first ? "" : "|"), first = false;
16511 if (mask & ELF_SPARC_HWCAP_FJFMAU)
16512 printf ("%sfjfmau", first ? "" : "|"), first = false;
16513 if (mask & ELF_SPARC_HWCAP_IMA)
16514 printf ("%sima", first ? "" : "|"), first = false;
16515 if (mask & ELF_SPARC_HWCAP_ASI_CACHE_SPARING)
16516 printf ("%scspare", first ? "" : "|"), first = false;
16517 }
16518 else
16519 fputc ('0', stdout);
16520 fputc ('\n', stdout);
16521 }
16522
16523 static void
16524 display_sparc_hwcaps2 (unsigned int mask)
16525 {
16526 if (mask)
16527 {
16528 bool first = true;
16529
16530 if (mask & ELF_SPARC_HWCAP2_FJATHPLUS)
16531 fputs ("fjathplus", stdout), first = false;
16532 if (mask & ELF_SPARC_HWCAP2_VIS3B)
16533 printf ("%svis3b", first ? "" : "|"), first = false;
16534 if (mask & ELF_SPARC_HWCAP2_ADP)
16535 printf ("%sadp", first ? "" : "|"), first = false;
16536 if (mask & ELF_SPARC_HWCAP2_SPARC5)
16537 printf ("%ssparc5", first ? "" : "|"), first = false;
16538 if (mask & ELF_SPARC_HWCAP2_MWAIT)
16539 printf ("%smwait", first ? "" : "|"), first = false;
16540 if (mask & ELF_SPARC_HWCAP2_XMPMUL)
16541 printf ("%sxmpmul", first ? "" : "|"), first = false;
16542 if (mask & ELF_SPARC_HWCAP2_XMONT)
16543 printf ("%sxmont2", first ? "" : "|"), first = false;
16544 if (mask & ELF_SPARC_HWCAP2_NSEC)
16545 printf ("%snsec", first ? "" : "|"), first = false;
16546 if (mask & ELF_SPARC_HWCAP2_FJATHHPC)
16547 printf ("%sfjathhpc", first ? "" : "|"), first = false;
16548 if (mask & ELF_SPARC_HWCAP2_FJDES)
16549 printf ("%sfjdes", first ? "" : "|"), first = false;
16550 if (mask & ELF_SPARC_HWCAP2_FJAES)
16551 printf ("%sfjaes", first ? "" : "|"), first = false;
16552 }
16553 else
16554 fputc ('0', stdout);
16555 fputc ('\n', stdout);
16556 }
16557
16558 static unsigned char *
16559 display_sparc_gnu_attribute (unsigned char * p,
16560 unsigned int tag,
16561 const unsigned char * const end)
16562 {
16563 unsigned int val;
16564
16565 if (tag == Tag_GNU_Sparc_HWCAPS)
16566 {
16567 READ_ULEB (val, p, end);
16568 printf (" Tag_GNU_Sparc_HWCAPS: ");
16569 display_sparc_hwcaps (val);
16570 return p;
16571 }
16572 if (tag == Tag_GNU_Sparc_HWCAPS2)
16573 {
16574 READ_ULEB (val, p, end);
16575 printf (" Tag_GNU_Sparc_HWCAPS2: ");
16576 display_sparc_hwcaps2 (val);
16577 return p;
16578 }
16579
16580 return display_tag_value (tag, p, end);
16581 }
16582
16583 static void
16584 print_mips_fp_abi_value (unsigned int val)
16585 {
16586 switch (val)
16587 {
16588 case Val_GNU_MIPS_ABI_FP_ANY:
16589 printf (_("Hard or soft float\n"));
16590 break;
16591 case Val_GNU_MIPS_ABI_FP_DOUBLE:
16592 printf (_("Hard float (double precision)\n"));
16593 break;
16594 case Val_GNU_MIPS_ABI_FP_SINGLE:
16595 printf (_("Hard float (single precision)\n"));
16596 break;
16597 case Val_GNU_MIPS_ABI_FP_SOFT:
16598 printf (_("Soft float\n"));
16599 break;
16600 case Val_GNU_MIPS_ABI_FP_OLD_64:
16601 printf (_("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
16602 break;
16603 case Val_GNU_MIPS_ABI_FP_XX:
16604 printf (_("Hard float (32-bit CPU, Any FPU)\n"));
16605 break;
16606 case Val_GNU_MIPS_ABI_FP_64:
16607 printf (_("Hard float (32-bit CPU, 64-bit FPU)\n"));
16608 break;
16609 case Val_GNU_MIPS_ABI_FP_64A:
16610 printf (_("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
16611 break;
16612 case Val_GNU_MIPS_ABI_FP_NAN2008:
16613 printf (_("NaN 2008 compatibility\n"));
16614 break;
16615 default:
16616 printf ("??? (%d)\n", val);
16617 break;
16618 }
16619 }
16620
16621 static unsigned char *
16622 display_mips_gnu_attribute (unsigned char * p,
16623 unsigned int tag,
16624 const unsigned char * const end)
16625 {
16626 if (tag == Tag_GNU_MIPS_ABI_FP)
16627 {
16628 unsigned int val;
16629
16630 printf (" Tag_GNU_MIPS_ABI_FP: ");
16631 READ_ULEB (val, p, end);
16632 print_mips_fp_abi_value (val);
16633 return p;
16634 }
16635
16636 if (tag == Tag_GNU_MIPS_ABI_MSA)
16637 {
16638 unsigned int val;
16639
16640 printf (" Tag_GNU_MIPS_ABI_MSA: ");
16641 READ_ULEB (val, p, end);
16642
16643 switch (val)
16644 {
16645 case Val_GNU_MIPS_ABI_MSA_ANY:
16646 printf (_("Any MSA or not\n"));
16647 break;
16648 case Val_GNU_MIPS_ABI_MSA_128:
16649 printf (_("128-bit MSA\n"));
16650 break;
16651 default:
16652 printf ("??? (%d)\n", val);
16653 break;
16654 }
16655 return p;
16656 }
16657
16658 return display_tag_value (tag & 1, p, end);
16659 }
16660
16661 static unsigned char *
16662 display_tic6x_attribute (unsigned char * p,
16663 const unsigned char * const end)
16664 {
16665 unsigned int tag;
16666 unsigned int val;
16667
16668 READ_ULEB (tag, p, end);
16669
16670 switch (tag)
16671 {
16672 case Tag_ISA:
16673 printf (" Tag_ISA: ");
16674 READ_ULEB (val, p, end);
16675
16676 switch (val)
16677 {
16678 case C6XABI_Tag_ISA_none:
16679 printf (_("None\n"));
16680 break;
16681 case C6XABI_Tag_ISA_C62X:
16682 printf ("C62x\n");
16683 break;
16684 case C6XABI_Tag_ISA_C67X:
16685 printf ("C67x\n");
16686 break;
16687 case C6XABI_Tag_ISA_C67XP:
16688 printf ("C67x+\n");
16689 break;
16690 case C6XABI_Tag_ISA_C64X:
16691 printf ("C64x\n");
16692 break;
16693 case C6XABI_Tag_ISA_C64XP:
16694 printf ("C64x+\n");
16695 break;
16696 case C6XABI_Tag_ISA_C674X:
16697 printf ("C674x\n");
16698 break;
16699 default:
16700 printf ("??? (%d)\n", val);
16701 break;
16702 }
16703 return p;
16704
16705 case Tag_ABI_wchar_t:
16706 printf (" Tag_ABI_wchar_t: ");
16707 READ_ULEB (val, p, end);
16708 switch (val)
16709 {
16710 case 0:
16711 printf (_("Not used\n"));
16712 break;
16713 case 1:
16714 printf (_("2 bytes\n"));
16715 break;
16716 case 2:
16717 printf (_("4 bytes\n"));
16718 break;
16719 default:
16720 printf ("??? (%d)\n", val);
16721 break;
16722 }
16723 return p;
16724
16725 case Tag_ABI_stack_align_needed:
16726 printf (" Tag_ABI_stack_align_needed: ");
16727 READ_ULEB (val, p, end);
16728 switch (val)
16729 {
16730 case 0:
16731 printf (_("8-byte\n"));
16732 break;
16733 case 1:
16734 printf (_("16-byte\n"));
16735 break;
16736 default:
16737 printf ("??? (%d)\n", val);
16738 break;
16739 }
16740 return p;
16741
16742 case Tag_ABI_stack_align_preserved:
16743 READ_ULEB (val, p, end);
16744 printf (" Tag_ABI_stack_align_preserved: ");
16745 switch (val)
16746 {
16747 case 0:
16748 printf (_("8-byte\n"));
16749 break;
16750 case 1:
16751 printf (_("16-byte\n"));
16752 break;
16753 default:
16754 printf ("??? (%d)\n", val);
16755 break;
16756 }
16757 return p;
16758
16759 case Tag_ABI_DSBT:
16760 READ_ULEB (val, p, end);
16761 printf (" Tag_ABI_DSBT: ");
16762 switch (val)
16763 {
16764 case 0:
16765 printf (_("DSBT addressing not used\n"));
16766 break;
16767 case 1:
16768 printf (_("DSBT addressing used\n"));
16769 break;
16770 default:
16771 printf ("??? (%d)\n", val);
16772 break;
16773 }
16774 return p;
16775
16776 case Tag_ABI_PID:
16777 READ_ULEB (val, p, end);
16778 printf (" Tag_ABI_PID: ");
16779 switch (val)
16780 {
16781 case 0:
16782 printf (_("Data addressing position-dependent\n"));
16783 break;
16784 case 1:
16785 printf (_("Data addressing position-independent, GOT near DP\n"));
16786 break;
16787 case 2:
16788 printf (_("Data addressing position-independent, GOT far from DP\n"));
16789 break;
16790 default:
16791 printf ("??? (%d)\n", val);
16792 break;
16793 }
16794 return p;
16795
16796 case Tag_ABI_PIC:
16797 READ_ULEB (val, p, end);
16798 printf (" Tag_ABI_PIC: ");
16799 switch (val)
16800 {
16801 case 0:
16802 printf (_("Code addressing position-dependent\n"));
16803 break;
16804 case 1:
16805 printf (_("Code addressing position-independent\n"));
16806 break;
16807 default:
16808 printf ("??? (%d)\n", val);
16809 break;
16810 }
16811 return p;
16812
16813 case Tag_ABI_array_object_alignment:
16814 READ_ULEB (val, p, end);
16815 printf (" Tag_ABI_array_object_alignment: ");
16816 switch (val)
16817 {
16818 case 0:
16819 printf (_("8-byte\n"));
16820 break;
16821 case 1:
16822 printf (_("4-byte\n"));
16823 break;
16824 case 2:
16825 printf (_("16-byte\n"));
16826 break;
16827 default:
16828 printf ("??? (%d)\n", val);
16829 break;
16830 }
16831 return p;
16832
16833 case Tag_ABI_array_object_align_expected:
16834 READ_ULEB (val, p, end);
16835 printf (" Tag_ABI_array_object_align_expected: ");
16836 switch (val)
16837 {
16838 case 0:
16839 printf (_("8-byte\n"));
16840 break;
16841 case 1:
16842 printf (_("4-byte\n"));
16843 break;
16844 case 2:
16845 printf (_("16-byte\n"));
16846 break;
16847 default:
16848 printf ("??? (%d)\n", val);
16849 break;
16850 }
16851 return p;
16852
16853 case Tag_ABI_compatibility:
16854 {
16855 READ_ULEB (val, p, end);
16856 printf (" Tag_ABI_compatibility: ");
16857 printf (_("flag = %d, vendor = "), val);
16858 if (p < end - 1)
16859 {
16860 size_t maxlen = (end - p) - 1;
16861
16862 print_symbol ((int) maxlen, (const char *) p);
16863 p += strnlen ((char *) p, maxlen) + 1;
16864 }
16865 else
16866 {
16867 printf (_("<corrupt>"));
16868 p = (unsigned char *) end;
16869 }
16870 putchar ('\n');
16871 return p;
16872 }
16873
16874 case Tag_ABI_conformance:
16875 {
16876 printf (" Tag_ABI_conformance: \"");
16877 if (p < end - 1)
16878 {
16879 size_t maxlen = (end - p) - 1;
16880
16881 print_symbol ((int) maxlen, (const char *) p);
16882 p += strnlen ((char *) p, maxlen) + 1;
16883 }
16884 else
16885 {
16886 printf (_("<corrupt>"));
16887 p = (unsigned char *) end;
16888 }
16889 printf ("\"\n");
16890 return p;
16891 }
16892 }
16893
16894 return display_tag_value (tag, p, end);
16895 }
16896
16897 static void
16898 display_raw_attribute (unsigned char * p, unsigned char const * const end)
16899 {
16900 unsigned long addr = 0;
16901 size_t bytes = end - p;
16902
16903 assert (end >= p);
16904 while (bytes)
16905 {
16906 int j;
16907 int k;
16908 int lbytes = (bytes > 16 ? 16 : bytes);
16909
16910 printf (" 0x%8.8lx ", addr);
16911
16912 for (j = 0; j < 16; j++)
16913 {
16914 if (j < lbytes)
16915 printf ("%2.2x", p[j]);
16916 else
16917 printf (" ");
16918
16919 if ((j & 3) == 3)
16920 printf (" ");
16921 }
16922
16923 for (j = 0; j < lbytes; j++)
16924 {
16925 k = p[j];
16926 if (k >= ' ' && k < 0x7f)
16927 printf ("%c", k);
16928 else
16929 printf (".");
16930 }
16931
16932 putchar ('\n');
16933
16934 p += lbytes;
16935 bytes -= lbytes;
16936 addr += lbytes;
16937 }
16938
16939 putchar ('\n');
16940 }
16941
16942 static unsigned char *
16943 display_msp430_attribute (unsigned char * p,
16944 const unsigned char * const end)
16945 {
16946 unsigned int val;
16947 unsigned int tag;
16948
16949 READ_ULEB (tag, p, end);
16950
16951 switch (tag)
16952 {
16953 case OFBA_MSPABI_Tag_ISA:
16954 printf (" Tag_ISA: ");
16955 READ_ULEB (val, p, end);
16956 switch (val)
16957 {
16958 case 0: printf (_("None\n")); break;
16959 case 1: printf (_("MSP430\n")); break;
16960 case 2: printf (_("MSP430X\n")); break;
16961 default: printf ("??? (%d)\n", val); break;
16962 }
16963 break;
16964
16965 case OFBA_MSPABI_Tag_Code_Model:
16966 printf (" Tag_Code_Model: ");
16967 READ_ULEB (val, p, end);
16968 switch (val)
16969 {
16970 case 0: printf (_("None\n")); break;
16971 case 1: printf (_("Small\n")); break;
16972 case 2: printf (_("Large\n")); break;
16973 default: printf ("??? (%d)\n", val); break;
16974 }
16975 break;
16976
16977 case OFBA_MSPABI_Tag_Data_Model:
16978 printf (" Tag_Data_Model: ");
16979 READ_ULEB (val, p, end);
16980 switch (val)
16981 {
16982 case 0: printf (_("None\n")); break;
16983 case 1: printf (_("Small\n")); break;
16984 case 2: printf (_("Large\n")); break;
16985 case 3: printf (_("Restricted Large\n")); break;
16986 default: printf ("??? (%d)\n", val); break;
16987 }
16988 break;
16989
16990 default:
16991 printf (_(" <unknown tag %d>: "), tag);
16992
16993 if (tag & 1)
16994 {
16995 putchar ('"');
16996 if (p < end - 1)
16997 {
16998 size_t maxlen = (end - p) - 1;
16999
17000 print_symbol ((int) maxlen, (const char *) p);
17001 p += strnlen ((char *) p, maxlen) + 1;
17002 }
17003 else
17004 {
17005 printf (_("<corrupt>"));
17006 p = (unsigned char *) end;
17007 }
17008 printf ("\"\n");
17009 }
17010 else
17011 {
17012 READ_ULEB (val, p, end);
17013 printf ("%d (0x%x)\n", val, val);
17014 }
17015 break;
17016 }
17017
17018 assert (p <= end);
17019 return p;
17020 }
17021
17022 static unsigned char *
17023 display_msp430_gnu_attribute (unsigned char * p,
17024 unsigned int tag,
17025 const unsigned char * const end)
17026 {
17027 if (tag == Tag_GNU_MSP430_Data_Region)
17028 {
17029 unsigned int val;
17030
17031 printf (" Tag_GNU_MSP430_Data_Region: ");
17032 READ_ULEB (val, p, end);
17033
17034 switch (val)
17035 {
17036 case Val_GNU_MSP430_Data_Region_Any:
17037 printf (_("Any Region\n"));
17038 break;
17039 case Val_GNU_MSP430_Data_Region_Lower:
17040 printf (_("Lower Region Only\n"));
17041 break;
17042 default:
17043 printf ("??? (%u)\n", val);
17044 }
17045 return p;
17046 }
17047 return display_tag_value (tag & 1, p, end);
17048 }
17049
17050 struct riscv_attr_tag_t {
17051 const char *name;
17052 unsigned int tag;
17053 };
17054
17055 static struct riscv_attr_tag_t riscv_attr_tag[] =
17056 {
17057 #define T(tag) {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
17058 T(arch),
17059 T(priv_spec),
17060 T(priv_spec_minor),
17061 T(priv_spec_revision),
17062 T(unaligned_access),
17063 T(stack_align),
17064 #undef T
17065 };
17066
17067 static unsigned char *
17068 display_riscv_attribute (unsigned char *p,
17069 const unsigned char * const end)
17070 {
17071 unsigned int val;
17072 unsigned int tag;
17073 struct riscv_attr_tag_t *attr = NULL;
17074 unsigned i;
17075
17076 READ_ULEB (tag, p, end);
17077
17078 /* Find the name of attribute. */
17079 for (i = 0; i < ARRAY_SIZE (riscv_attr_tag); i++)
17080 {
17081 if (riscv_attr_tag[i].tag == tag)
17082 {
17083 attr = &riscv_attr_tag[i];
17084 break;
17085 }
17086 }
17087
17088 if (attr)
17089 printf (" %s: ", attr->name);
17090 else
17091 return display_tag_value (tag, p, end);
17092
17093 switch (tag)
17094 {
17095 case Tag_RISCV_priv_spec:
17096 case Tag_RISCV_priv_spec_minor:
17097 case Tag_RISCV_priv_spec_revision:
17098 READ_ULEB (val, p, end);
17099 printf (_("%u\n"), val);
17100 break;
17101 case Tag_RISCV_unaligned_access:
17102 READ_ULEB (val, p, end);
17103 switch (val)
17104 {
17105 case 0:
17106 printf (_("No unaligned access\n"));
17107 break;
17108 case 1:
17109 printf (_("Unaligned access\n"));
17110 break;
17111 }
17112 break;
17113 case Tag_RISCV_stack_align:
17114 READ_ULEB (val, p, end);
17115 printf (_("%u-bytes\n"), val);
17116 break;
17117 case Tag_RISCV_arch:
17118 p = display_tag_value (-1, p, end);
17119 break;
17120 default:
17121 return display_tag_value (tag, p, end);
17122 }
17123
17124 return p;
17125 }
17126
17127 static unsigned char *
17128 display_csky_attribute (unsigned char * p,
17129 const unsigned char * const end)
17130 {
17131 unsigned int tag;
17132 unsigned int val;
17133 READ_ULEB (tag, p, end);
17134
17135 if (tag >= Tag_CSKY_MAX)
17136 {
17137 return display_tag_value (-1, p, end);
17138 }
17139
17140 switch (tag)
17141 {
17142 case Tag_CSKY_ARCH_NAME:
17143 printf (" Tag_CSKY_ARCH_NAME:\t\t");
17144 return display_tag_value (-1, p, end);
17145 case Tag_CSKY_CPU_NAME:
17146 printf (" Tag_CSKY_CPU_NAME:\t\t");
17147 return display_tag_value (-1, p, end);
17148
17149 case Tag_CSKY_ISA_FLAGS:
17150 printf (" Tag_CSKY_ISA_FLAGS:\t\t");
17151 return display_tag_value (0, p, end);
17152 case Tag_CSKY_ISA_EXT_FLAGS:
17153 printf (" Tag_CSKY_ISA_EXT_FLAGS:\t");
17154 return display_tag_value (0, p, end);
17155
17156 case Tag_CSKY_DSP_VERSION:
17157 printf (" Tag_CSKY_DSP_VERSION:\t\t");
17158 READ_ULEB (val, p, end);
17159 if (val == VAL_CSKY_DSP_VERSION_EXTENSION)
17160 printf ("DSP Extension\n");
17161 else if (val == VAL_CSKY_DSP_VERSION_2)
17162 printf ("DSP 2.0\n");
17163 break;
17164
17165 case Tag_CSKY_VDSP_VERSION:
17166 printf (" Tag_CSKY_VDSP_VERSION:\t");
17167 READ_ULEB (val, p, end);
17168 printf ("VDSP Version %d\n", val);
17169 break;
17170
17171 case Tag_CSKY_FPU_VERSION:
17172 printf (" Tag_CSKY_FPU_VERSION:\t\t");
17173 READ_ULEB (val, p, end);
17174 if (val == VAL_CSKY_FPU_VERSION_1)
17175 printf ("ABIV1 FPU Version 1\n");
17176 else if (val == VAL_CSKY_FPU_VERSION_2)
17177 printf ("FPU Version 2\n");
17178 break;
17179
17180 case Tag_CSKY_FPU_ABI:
17181 printf (" Tag_CSKY_FPU_ABI:\t\t");
17182 READ_ULEB (val, p, end);
17183 if (val == VAL_CSKY_FPU_ABI_HARD)
17184 printf ("Hard\n");
17185 else if (val == VAL_CSKY_FPU_ABI_SOFTFP)
17186 printf ("SoftFP\n");
17187 else if (val == VAL_CSKY_FPU_ABI_SOFT)
17188 printf ("Soft\n");
17189 break;
17190 case Tag_CSKY_FPU_ROUNDING:
17191 READ_ULEB (val, p, end);
17192 if (val == 1) {
17193 printf (" Tag_CSKY_FPU_ROUNDING:\t");
17194 printf ("Needed\n");
17195 }
17196 break;
17197 case Tag_CSKY_FPU_DENORMAL:
17198 READ_ULEB (val, p, end);
17199 if (val == 1) {
17200 printf (" Tag_CSKY_FPU_DENORMAL:\t");
17201 printf ("Needed\n");
17202 }
17203 break;
17204 case Tag_CSKY_FPU_Exception:
17205 READ_ULEB (val, p, end);
17206 if (val == 1) {
17207 printf (" Tag_CSKY_FPU_Exception:\t");
17208 printf ("Needed\n");
17209 }
17210 break;
17211 case Tag_CSKY_FPU_NUMBER_MODULE:
17212 printf (" Tag_CSKY_FPU_NUMBER_MODULE:\t");
17213 return display_tag_value (-1, p, end);
17214 case Tag_CSKY_FPU_HARDFP:
17215 printf (" Tag_CSKY_FPU_HARDFP:\t\t");
17216 READ_ULEB (val, p, end);
17217 if (val & VAL_CSKY_FPU_HARDFP_HALF)
17218 printf (" Half");
17219 if (val & VAL_CSKY_FPU_HARDFP_SINGLE)
17220 printf (" Single");
17221 if (val & VAL_CSKY_FPU_HARDFP_DOUBLE)
17222 printf (" Double");
17223 printf ("\n");
17224 break;
17225 default:
17226 return display_tag_value (tag, p, end);
17227 }
17228 return p;
17229 }
17230
17231 static bool
17232 process_attributes (Filedata * filedata,
17233 const char * public_name,
17234 unsigned int proc_type,
17235 unsigned char * (* display_pub_attribute) (unsigned char *, const unsigned char * const),
17236 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const))
17237 {
17238 Elf_Internal_Shdr * sect;
17239 unsigned i;
17240 bool res = true;
17241
17242 /* Find the section header so that we get the size. */
17243 for (i = 0, sect = filedata->section_headers;
17244 i < filedata->file_header.e_shnum;
17245 i++, sect++)
17246 {
17247 unsigned char * contents;
17248 unsigned char * p;
17249
17250 if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
17251 continue;
17252
17253 contents = (unsigned char *) get_data (NULL, filedata, sect->sh_offset, 1,
17254 sect->sh_size, _("attributes"));
17255 if (contents == NULL)
17256 {
17257 res = false;
17258 continue;
17259 }
17260
17261 p = contents;
17262 /* The first character is the version of the attributes.
17263 Currently only version 1, (aka 'A') is recognised here. */
17264 if (*p != 'A')
17265 {
17266 printf (_("Unknown attributes version '%c'(%d) - expecting 'A'\n"), *p, *p);
17267 res = false;
17268 }
17269 else
17270 {
17271 bfd_vma section_len;
17272
17273 section_len = sect->sh_size - 1;
17274 p++;
17275
17276 while (section_len > 0)
17277 {
17278 bfd_vma attr_len;
17279 unsigned int namelen;
17280 bool public_section;
17281 bool gnu_section;
17282
17283 if (section_len <= 4)
17284 {
17285 error (_("Tag section ends prematurely\n"));
17286 res = false;
17287 break;
17288 }
17289 attr_len = byte_get (p, 4);
17290 p += 4;
17291
17292 if (attr_len > section_len)
17293 {
17294 error (_("Bad attribute length (%u > %u)\n"),
17295 (unsigned) attr_len, (unsigned) section_len);
17296 attr_len = section_len;
17297 res = false;
17298 }
17299 /* PR 17531: file: 001-101425-0.004 */
17300 else if (attr_len < 5)
17301 {
17302 error (_("Attribute length of %u is too small\n"), (unsigned) attr_len);
17303 res = false;
17304 break;
17305 }
17306
17307 section_len -= attr_len;
17308 attr_len -= 4;
17309
17310 namelen = strnlen ((char *) p, attr_len) + 1;
17311 if (namelen == 0 || namelen >= attr_len)
17312 {
17313 error (_("Corrupt attribute section name\n"));
17314 res = false;
17315 break;
17316 }
17317
17318 printf (_("Attribute Section: "));
17319 print_symbol (INT_MAX, (const char *) p);
17320 putchar ('\n');
17321
17322 if (public_name && streq ((char *) p, public_name))
17323 public_section = true;
17324 else
17325 public_section = false;
17326
17327 if (streq ((char *) p, "gnu"))
17328 gnu_section = true;
17329 else
17330 gnu_section = false;
17331
17332 p += namelen;
17333 attr_len -= namelen;
17334
17335 while (attr_len > 0 && p < contents + sect->sh_size)
17336 {
17337 int tag;
17338 unsigned int val;
17339 bfd_vma size;
17340 unsigned char * end;
17341
17342 /* PR binutils/17531: Safe handling of corrupt files. */
17343 if (attr_len < 6)
17344 {
17345 error (_("Unused bytes at end of section\n"));
17346 res = false;
17347 section_len = 0;
17348 break;
17349 }
17350
17351 tag = *(p++);
17352 size = byte_get (p, 4);
17353 if (size > attr_len)
17354 {
17355 error (_("Bad subsection length (%u > %u)\n"),
17356 (unsigned) size, (unsigned) attr_len);
17357 res = false;
17358 size = attr_len;
17359 }
17360 /* PR binutils/17531: Safe handling of corrupt files. */
17361 if (size < 6)
17362 {
17363 error (_("Bad subsection length (%u < 6)\n"),
17364 (unsigned) size);
17365 res = false;
17366 section_len = 0;
17367 break;
17368 }
17369
17370 attr_len -= size;
17371 end = p + size - 1;
17372 assert (end <= contents + sect->sh_size);
17373 p += 4;
17374
17375 switch (tag)
17376 {
17377 case 1:
17378 printf (_("File Attributes\n"));
17379 break;
17380 case 2:
17381 printf (_("Section Attributes:"));
17382 goto do_numlist;
17383 case 3:
17384 printf (_("Symbol Attributes:"));
17385 /* Fall through. */
17386 do_numlist:
17387 for (;;)
17388 {
17389 READ_ULEB (val, p, end);
17390 if (val == 0)
17391 break;
17392 printf (" %d", val);
17393 }
17394 printf ("\n");
17395 break;
17396 default:
17397 printf (_("Unknown tag: %d\n"), tag);
17398 public_section = false;
17399 break;
17400 }
17401
17402 if (public_section && display_pub_attribute != NULL)
17403 {
17404 while (p < end)
17405 p = display_pub_attribute (p, end);
17406 assert (p == end);
17407 }
17408 else if (gnu_section && display_proc_gnu_attribute != NULL)
17409 {
17410 while (p < end)
17411 p = display_gnu_attribute (p,
17412 display_proc_gnu_attribute,
17413 end);
17414 assert (p == end);
17415 }
17416 else if (p < end)
17417 {
17418 printf (_(" Unknown attribute:\n"));
17419 display_raw_attribute (p, end);
17420 p = end;
17421 }
17422 else
17423 attr_len = 0;
17424 }
17425 }
17426 }
17427
17428 free (contents);
17429 }
17430
17431 return res;
17432 }
17433
17434 /* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
17435 Print the Address, Access and Initial fields of an entry at VMA ADDR
17436 and return the VMA of the next entry, or -1 if there was a problem.
17437 Does not read from DATA_END or beyond. */
17438
17439 static bfd_vma
17440 print_mips_got_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr,
17441 unsigned char * data_end)
17442 {
17443 printf (" ");
17444 print_vma (addr, LONG_HEX);
17445 printf (" ");
17446 if (addr < pltgot + 0xfff0)
17447 printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
17448 else
17449 printf ("%10s", "");
17450 printf (" ");
17451 if (data == NULL)
17452 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
17453 else
17454 {
17455 bfd_vma entry;
17456 unsigned char * from = data + addr - pltgot;
17457
17458 if (from + (is_32bit_elf ? 4 : 8) > data_end)
17459 {
17460 warn (_("MIPS GOT entry extends beyond the end of available data\n"));
17461 printf ("%*s", is_32bit_elf ? 8 : 16, _("<corrupt>"));
17462 return (bfd_vma) -1;
17463 }
17464 else
17465 {
17466 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
17467 print_vma (entry, LONG_HEX);
17468 }
17469 }
17470 return addr + (is_32bit_elf ? 4 : 8);
17471 }
17472
17473 /* DATA points to the contents of a MIPS PLT GOT that starts at VMA
17474 PLTGOT. Print the Address and Initial fields of an entry at VMA
17475 ADDR and return the VMA of the next entry. */
17476
17477 static bfd_vma
17478 print_mips_pltgot_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr)
17479 {
17480 printf (" ");
17481 print_vma (addr, LONG_HEX);
17482 printf (" ");
17483 if (data == NULL)
17484 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
17485 else
17486 {
17487 bfd_vma entry;
17488
17489 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
17490 print_vma (entry, LONG_HEX);
17491 }
17492 return addr + (is_32bit_elf ? 4 : 8);
17493 }
17494
17495 static void
17496 print_mips_ases (unsigned int mask)
17497 {
17498 if (mask & AFL_ASE_DSP)
17499 fputs ("\n\tDSP ASE", stdout);
17500 if (mask & AFL_ASE_DSPR2)
17501 fputs ("\n\tDSP R2 ASE", stdout);
17502 if (mask & AFL_ASE_DSPR3)
17503 fputs ("\n\tDSP R3 ASE", stdout);
17504 if (mask & AFL_ASE_EVA)
17505 fputs ("\n\tEnhanced VA Scheme", stdout);
17506 if (mask & AFL_ASE_MCU)
17507 fputs ("\n\tMCU (MicroController) ASE", stdout);
17508 if (mask & AFL_ASE_MDMX)
17509 fputs ("\n\tMDMX ASE", stdout);
17510 if (mask & AFL_ASE_MIPS3D)
17511 fputs ("\n\tMIPS-3D ASE", stdout);
17512 if (mask & AFL_ASE_MT)
17513 fputs ("\n\tMT ASE", stdout);
17514 if (mask & AFL_ASE_SMARTMIPS)
17515 fputs ("\n\tSmartMIPS ASE", stdout);
17516 if (mask & AFL_ASE_VIRT)
17517 fputs ("\n\tVZ ASE", stdout);
17518 if (mask & AFL_ASE_MSA)
17519 fputs ("\n\tMSA ASE", stdout);
17520 if (mask & AFL_ASE_MIPS16)
17521 fputs ("\n\tMIPS16 ASE", stdout);
17522 if (mask & AFL_ASE_MICROMIPS)
17523 fputs ("\n\tMICROMIPS ASE", stdout);
17524 if (mask & AFL_ASE_XPA)
17525 fputs ("\n\tXPA ASE", stdout);
17526 if (mask & AFL_ASE_MIPS16E2)
17527 fputs ("\n\tMIPS16e2 ASE", stdout);
17528 if (mask & AFL_ASE_CRC)
17529 fputs ("\n\tCRC ASE", stdout);
17530 if (mask & AFL_ASE_GINV)
17531 fputs ("\n\tGINV ASE", stdout);
17532 if (mask & AFL_ASE_LOONGSON_MMI)
17533 fputs ("\n\tLoongson MMI ASE", stdout);
17534 if (mask & AFL_ASE_LOONGSON_CAM)
17535 fputs ("\n\tLoongson CAM ASE", stdout);
17536 if (mask & AFL_ASE_LOONGSON_EXT)
17537 fputs ("\n\tLoongson EXT ASE", stdout);
17538 if (mask & AFL_ASE_LOONGSON_EXT2)
17539 fputs ("\n\tLoongson EXT2 ASE", stdout);
17540 if (mask == 0)
17541 fprintf (stdout, "\n\t%s", _("None"));
17542 else if ((mask & ~AFL_ASE_MASK) != 0)
17543 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
17544 }
17545
17546 static void
17547 print_mips_isa_ext (unsigned int isa_ext)
17548 {
17549 switch (isa_ext)
17550 {
17551 case 0:
17552 fputs (_("None"), stdout);
17553 break;
17554 case AFL_EXT_XLR:
17555 fputs ("RMI XLR", stdout);
17556 break;
17557 case AFL_EXT_OCTEON3:
17558 fputs ("Cavium Networks Octeon3", stdout);
17559 break;
17560 case AFL_EXT_OCTEON2:
17561 fputs ("Cavium Networks Octeon2", stdout);
17562 break;
17563 case AFL_EXT_OCTEONP:
17564 fputs ("Cavium Networks OcteonP", stdout);
17565 break;
17566 case AFL_EXT_OCTEON:
17567 fputs ("Cavium Networks Octeon", stdout);
17568 break;
17569 case AFL_EXT_5900:
17570 fputs ("Toshiba R5900", stdout);
17571 break;
17572 case AFL_EXT_4650:
17573 fputs ("MIPS R4650", stdout);
17574 break;
17575 case AFL_EXT_4010:
17576 fputs ("LSI R4010", stdout);
17577 break;
17578 case AFL_EXT_4100:
17579 fputs ("NEC VR4100", stdout);
17580 break;
17581 case AFL_EXT_3900:
17582 fputs ("Toshiba R3900", stdout);
17583 break;
17584 case AFL_EXT_10000:
17585 fputs ("MIPS R10000", stdout);
17586 break;
17587 case AFL_EXT_SB1:
17588 fputs ("Broadcom SB-1", stdout);
17589 break;
17590 case AFL_EXT_4111:
17591 fputs ("NEC VR4111/VR4181", stdout);
17592 break;
17593 case AFL_EXT_4120:
17594 fputs ("NEC VR4120", stdout);
17595 break;
17596 case AFL_EXT_5400:
17597 fputs ("NEC VR5400", stdout);
17598 break;
17599 case AFL_EXT_5500:
17600 fputs ("NEC VR5500", stdout);
17601 break;
17602 case AFL_EXT_LOONGSON_2E:
17603 fputs ("ST Microelectronics Loongson 2E", stdout);
17604 break;
17605 case AFL_EXT_LOONGSON_2F:
17606 fputs ("ST Microelectronics Loongson 2F", stdout);
17607 break;
17608 case AFL_EXT_INTERAPTIV_MR2:
17609 fputs ("Imagination interAptiv MR2", stdout);
17610 break;
17611 default:
17612 fprintf (stdout, "%s (%d)", _("Unknown"), isa_ext);
17613 }
17614 }
17615
17616 static signed int
17617 get_mips_reg_size (int reg_size)
17618 {
17619 return (reg_size == AFL_REG_NONE) ? 0
17620 : (reg_size == AFL_REG_32) ? 32
17621 : (reg_size == AFL_REG_64) ? 64
17622 : (reg_size == AFL_REG_128) ? 128
17623 : -1;
17624 }
17625
17626 static bool
17627 process_mips_specific (Filedata * filedata)
17628 {
17629 Elf_Internal_Dyn * entry;
17630 Elf_Internal_Shdr *sect = NULL;
17631 size_t liblist_offset = 0;
17632 size_t liblistno = 0;
17633 size_t conflictsno = 0;
17634 size_t options_offset = 0;
17635 size_t conflicts_offset = 0;
17636 size_t pltrelsz = 0;
17637 size_t pltrel = 0;
17638 bfd_vma pltgot = 0;
17639 bfd_vma mips_pltgot = 0;
17640 bfd_vma jmprel = 0;
17641 bfd_vma local_gotno = 0;
17642 bfd_vma gotsym = 0;
17643 bfd_vma symtabno = 0;
17644 bool res = true;
17645
17646 if (! process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
17647 display_mips_gnu_attribute))
17648 res = false;
17649
17650 sect = find_section (filedata, ".MIPS.abiflags");
17651
17652 if (sect != NULL)
17653 {
17654 Elf_External_ABIFlags_v0 *abiflags_ext;
17655 Elf_Internal_ABIFlags_v0 abiflags_in;
17656
17657 if (sizeof (Elf_External_ABIFlags_v0) != sect->sh_size)
17658 {
17659 error (_("Corrupt MIPS ABI Flags section.\n"));
17660 res = false;
17661 }
17662 else
17663 {
17664 abiflags_ext = get_data (NULL, filedata, sect->sh_offset, 1,
17665 sect->sh_size, _("MIPS ABI Flags section"));
17666 if (abiflags_ext)
17667 {
17668 abiflags_in.version = BYTE_GET (abiflags_ext->version);
17669 abiflags_in.isa_level = BYTE_GET (abiflags_ext->isa_level);
17670 abiflags_in.isa_rev = BYTE_GET (abiflags_ext->isa_rev);
17671 abiflags_in.gpr_size = BYTE_GET (abiflags_ext->gpr_size);
17672 abiflags_in.cpr1_size = BYTE_GET (abiflags_ext->cpr1_size);
17673 abiflags_in.cpr2_size = BYTE_GET (abiflags_ext->cpr2_size);
17674 abiflags_in.fp_abi = BYTE_GET (abiflags_ext->fp_abi);
17675 abiflags_in.isa_ext = BYTE_GET (abiflags_ext->isa_ext);
17676 abiflags_in.ases = BYTE_GET (abiflags_ext->ases);
17677 abiflags_in.flags1 = BYTE_GET (abiflags_ext->flags1);
17678 abiflags_in.flags2 = BYTE_GET (abiflags_ext->flags2);
17679
17680 printf ("\nMIPS ABI Flags Version: %d\n", abiflags_in.version);
17681 printf ("\nISA: MIPS%d", abiflags_in.isa_level);
17682 if (abiflags_in.isa_rev > 1)
17683 printf ("r%d", abiflags_in.isa_rev);
17684 printf ("\nGPR size: %d",
17685 get_mips_reg_size (abiflags_in.gpr_size));
17686 printf ("\nCPR1 size: %d",
17687 get_mips_reg_size (abiflags_in.cpr1_size));
17688 printf ("\nCPR2 size: %d",
17689 get_mips_reg_size (abiflags_in.cpr2_size));
17690 fputs ("\nFP ABI: ", stdout);
17691 print_mips_fp_abi_value (abiflags_in.fp_abi);
17692 fputs ("ISA Extension: ", stdout);
17693 print_mips_isa_ext (abiflags_in.isa_ext);
17694 fputs ("\nASEs:", stdout);
17695 print_mips_ases (abiflags_in.ases);
17696 printf ("\nFLAGS 1: %8.8lx", abiflags_in.flags1);
17697 printf ("\nFLAGS 2: %8.8lx", abiflags_in.flags2);
17698 fputc ('\n', stdout);
17699 free (abiflags_ext);
17700 }
17701 }
17702 }
17703
17704 /* We have a lot of special sections. Thanks SGI! */
17705 if (filedata->dynamic_section == NULL)
17706 {
17707 /* No dynamic information available. See if there is static GOT. */
17708 sect = find_section (filedata, ".got");
17709 if (sect != NULL)
17710 {
17711 unsigned char *data_end;
17712 unsigned char *data;
17713 bfd_vma ent, end;
17714 int addr_size;
17715
17716 pltgot = sect->sh_addr;
17717
17718 ent = pltgot;
17719 addr_size = (is_32bit_elf ? 4 : 8);
17720 end = pltgot + sect->sh_size;
17721
17722 data = (unsigned char *) get_data (NULL, filedata, sect->sh_offset,
17723 end - pltgot, 1,
17724 _("Global Offset Table data"));
17725 /* PR 12855: Null data is handled gracefully throughout. */
17726 data_end = data + (end - pltgot);
17727
17728 printf (_("\nStatic GOT:\n"));
17729 printf (_(" Canonical gp value: "));
17730 print_vma (ent + 0x7ff0, LONG_HEX);
17731 printf ("\n\n");
17732
17733 /* In a dynamic binary GOT[0] is reserved for the dynamic
17734 loader to store the lazy resolver pointer, however in
17735 a static binary it may well have been omitted and GOT
17736 reduced to a table of addresses.
17737 PR 21344: Check for the entry being fully available
17738 before fetching it. */
17739 if (data
17740 && data + ent - pltgot + addr_size <= data_end
17741 && byte_get (data + ent - pltgot, addr_size) == 0)
17742 {
17743 printf (_(" Reserved entries:\n"));
17744 printf (_(" %*s %10s %*s\n"),
17745 addr_size * 2, _("Address"), _("Access"),
17746 addr_size * 2, _("Value"));
17747 ent = print_mips_got_entry (data, pltgot, ent, data_end);
17748 printf ("\n");
17749 if (ent == (bfd_vma) -1)
17750 goto sgot_print_fail;
17751
17752 /* Check for the MSB of GOT[1] being set, identifying a
17753 GNU object. This entry will be used by some runtime
17754 loaders, to store the module pointer. Otherwise this
17755 is an ordinary local entry.
17756 PR 21344: Check for the entry being fully available
17757 before fetching it. */
17758 if (data
17759 && data + ent - pltgot + addr_size <= data_end
17760 && (byte_get (data + ent - pltgot, addr_size)
17761 >> (addr_size * 8 - 1)) != 0)
17762 {
17763 ent = print_mips_got_entry (data, pltgot, ent, data_end);
17764 printf ("\n");
17765 if (ent == (bfd_vma) -1)
17766 goto sgot_print_fail;
17767 }
17768 printf ("\n");
17769 }
17770
17771 if (data != NULL && ent < end)
17772 {
17773 printf (_(" Local entries:\n"));
17774 printf (" %*s %10s %*s\n",
17775 addr_size * 2, _("Address"), _("Access"),
17776 addr_size * 2, _("Value"));
17777 while (ent < end)
17778 {
17779 ent = print_mips_got_entry (data, pltgot, ent, data_end);
17780 printf ("\n");
17781 if (ent == (bfd_vma) -1)
17782 goto sgot_print_fail;
17783 }
17784 printf ("\n");
17785 }
17786
17787 sgot_print_fail:
17788 free (data);
17789 }
17790 return res;
17791 }
17792
17793 for (entry = filedata->dynamic_section;
17794 /* PR 17531 file: 012-50589-0.004. */
17795 (entry < filedata->dynamic_section + filedata->dynamic_nent
17796 && entry->d_tag != DT_NULL);
17797 ++entry)
17798 switch (entry->d_tag)
17799 {
17800 case DT_MIPS_LIBLIST:
17801 liblist_offset
17802 = offset_from_vma (filedata, entry->d_un.d_val,
17803 liblistno * sizeof (Elf32_External_Lib));
17804 break;
17805 case DT_MIPS_LIBLISTNO:
17806 liblistno = entry->d_un.d_val;
17807 break;
17808 case DT_MIPS_OPTIONS:
17809 options_offset = offset_from_vma (filedata, entry->d_un.d_val, 0);
17810 break;
17811 case DT_MIPS_CONFLICT:
17812 conflicts_offset
17813 = offset_from_vma (filedata, entry->d_un.d_val,
17814 conflictsno * sizeof (Elf32_External_Conflict));
17815 break;
17816 case DT_MIPS_CONFLICTNO:
17817 conflictsno = entry->d_un.d_val;
17818 break;
17819 case DT_PLTGOT:
17820 pltgot = entry->d_un.d_ptr;
17821 break;
17822 case DT_MIPS_LOCAL_GOTNO:
17823 local_gotno = entry->d_un.d_val;
17824 break;
17825 case DT_MIPS_GOTSYM:
17826 gotsym = entry->d_un.d_val;
17827 break;
17828 case DT_MIPS_SYMTABNO:
17829 symtabno = entry->d_un.d_val;
17830 break;
17831 case DT_MIPS_PLTGOT:
17832 mips_pltgot = entry->d_un.d_ptr;
17833 break;
17834 case DT_PLTREL:
17835 pltrel = entry->d_un.d_val;
17836 break;
17837 case DT_PLTRELSZ:
17838 pltrelsz = entry->d_un.d_val;
17839 break;
17840 case DT_JMPREL:
17841 jmprel = entry->d_un.d_ptr;
17842 break;
17843 default:
17844 break;
17845 }
17846
17847 if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
17848 {
17849 Elf32_External_Lib * elib;
17850 size_t cnt;
17851
17852 elib = (Elf32_External_Lib *) get_data (NULL, filedata, liblist_offset,
17853 sizeof (Elf32_External_Lib),
17854 liblistno,
17855 _("liblist section data"));
17856 if (elib)
17857 {
17858 printf (ngettext ("\nSection '.liblist' contains %lu entry:\n",
17859 "\nSection '.liblist' contains %lu entries:\n",
17860 (unsigned long) liblistno),
17861 (unsigned long) liblistno);
17862 fputs (_(" Library Time Stamp Checksum Version Flags\n"),
17863 stdout);
17864
17865 for (cnt = 0; cnt < liblistno; ++cnt)
17866 {
17867 Elf32_Lib liblist;
17868 time_t atime;
17869 char timebuf[128];
17870 struct tm * tmp;
17871
17872 liblist.l_name = BYTE_GET (elib[cnt].l_name);
17873 atime = BYTE_GET (elib[cnt].l_time_stamp);
17874 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
17875 liblist.l_version = BYTE_GET (elib[cnt].l_version);
17876 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
17877
17878 tmp = gmtime (&atime);
17879 snprintf (timebuf, sizeof (timebuf),
17880 "%04u-%02u-%02uT%02u:%02u:%02u",
17881 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
17882 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
17883
17884 printf ("%3lu: ", (unsigned long) cnt);
17885 if (VALID_DYNAMIC_NAME (filedata, liblist.l_name))
17886 print_symbol (20, GET_DYNAMIC_NAME (filedata, liblist.l_name));
17887 else
17888 printf (_("<corrupt: %9ld>"), liblist.l_name);
17889 printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
17890 liblist.l_version);
17891
17892 if (liblist.l_flags == 0)
17893 puts (_(" NONE"));
17894 else
17895 {
17896 static const struct
17897 {
17898 const char * name;
17899 int bit;
17900 }
17901 l_flags_vals[] =
17902 {
17903 { " EXACT_MATCH", LL_EXACT_MATCH },
17904 { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
17905 { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
17906 { " EXPORTS", LL_EXPORTS },
17907 { " DELAY_LOAD", LL_DELAY_LOAD },
17908 { " DELTA", LL_DELTA }
17909 };
17910 int flags = liblist.l_flags;
17911 size_t fcnt;
17912
17913 for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
17914 if ((flags & l_flags_vals[fcnt].bit) != 0)
17915 {
17916 fputs (l_flags_vals[fcnt].name, stdout);
17917 flags ^= l_flags_vals[fcnt].bit;
17918 }
17919 if (flags != 0)
17920 printf (" %#x", (unsigned int) flags);
17921
17922 puts ("");
17923 }
17924 }
17925
17926 free (elib);
17927 }
17928 else
17929 res = false;
17930 }
17931
17932 if (options_offset != 0)
17933 {
17934 Elf_External_Options * eopt;
17935 size_t offset;
17936 int cnt;
17937 sect = filedata->section_headers;
17938
17939 /* Find the section header so that we get the size. */
17940 sect = find_section_by_type (filedata, SHT_MIPS_OPTIONS);
17941 /* PR 17533 file: 012-277276-0.004. */
17942 if (sect == NULL)
17943 {
17944 error (_("No MIPS_OPTIONS header found\n"));
17945 return false;
17946 }
17947 /* PR 24243 */
17948 if (sect->sh_size < sizeof (* eopt))
17949 {
17950 error (_("The MIPS options section is too small.\n"));
17951 return false;
17952 }
17953
17954 eopt = (Elf_External_Options *) get_data (NULL, filedata, options_offset, 1,
17955 sect->sh_size, _("options"));
17956 if (eopt)
17957 {
17958 Elf_Internal_Options option;
17959
17960 offset = cnt = 0;
17961 while (offset <= sect->sh_size - sizeof (* eopt))
17962 {
17963 Elf_External_Options * eoption;
17964 unsigned int optsize;
17965
17966 eoption = (Elf_External_Options *) ((char *) eopt + offset);
17967
17968 optsize = BYTE_GET (eoption->size);
17969
17970 /* PR 17531: file: ffa0fa3b. */
17971 if (optsize < sizeof (* eopt)
17972 || optsize > sect->sh_size - offset)
17973 {
17974 error (_("Invalid size (%u) for MIPS option\n"),
17975 optsize);
17976 free (eopt);
17977 return false;
17978 }
17979 offset += optsize;
17980 ++cnt;
17981 }
17982
17983 printf (ngettext ("\nSection '%s' contains %d entry:\n",
17984 "\nSection '%s' contains %d entries:\n",
17985 cnt),
17986 printable_section_name (filedata, sect), cnt);
17987
17988 offset = 0;
17989 while (cnt-- > 0)
17990 {
17991 size_t len;
17992 Elf_External_Options * eoption;
17993
17994 eoption = (Elf_External_Options *) ((char *) eopt + offset);
17995
17996 option.kind = BYTE_GET (eoption->kind);
17997 option.size = BYTE_GET (eoption->size);
17998 option.section = BYTE_GET (eoption->section);
17999 option.info = BYTE_GET (eoption->info);
18000
18001 switch (option.kind)
18002 {
18003 case ODK_NULL:
18004 /* This shouldn't happen. */
18005 printf (" NULL %" PRId16 " %" PRIx32,
18006 option.section, option.info);
18007 break;
18008
18009 case ODK_REGINFO:
18010 printf (" REGINFO ");
18011 if (filedata->file_header.e_machine == EM_MIPS)
18012 {
18013 Elf32_External_RegInfo * ereg;
18014 Elf32_RegInfo reginfo;
18015
18016 /* 32bit form. */
18017 if (option.size < (sizeof (Elf_External_Options)
18018 + sizeof (Elf32_External_RegInfo)))
18019 {
18020 printf (_("<corrupt>\n"));
18021 error (_("Truncated MIPS REGINFO option\n"));
18022 cnt = 0;
18023 break;
18024 }
18025
18026 ereg = (Elf32_External_RegInfo *) (eoption + 1);
18027
18028 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
18029 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
18030 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
18031 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
18032 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
18033 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
18034
18035 printf ("GPR %08" PRIx32 " GP 0x%" PRIx32 "\n",
18036 reginfo.ri_gprmask, reginfo.ri_gp_value);
18037 printf (" "
18038 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
18039 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
18040 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
18041 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
18042 }
18043 else
18044 {
18045 /* 64 bit form. */
18046 Elf64_External_RegInfo * ereg;
18047 Elf64_Internal_RegInfo reginfo;
18048
18049 if (option.size < (sizeof (Elf_External_Options)
18050 + sizeof (Elf64_External_RegInfo)))
18051 {
18052 printf (_("<corrupt>\n"));
18053 error (_("Truncated MIPS REGINFO option\n"));
18054 cnt = 0;
18055 break;
18056 }
18057
18058 ereg = (Elf64_External_RegInfo *) (eoption + 1);
18059 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
18060 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
18061 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
18062 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
18063 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
18064 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
18065
18066 printf ("GPR %08" PRIx32 " GP 0x%" PRIx64 "\n",
18067 reginfo.ri_gprmask, reginfo.ri_gp_value);
18068 printf (" "
18069 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
18070 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
18071 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
18072 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
18073 }
18074 offset += option.size;
18075 continue;
18076
18077 case ODK_EXCEPTIONS:
18078 fputs (" EXCEPTIONS fpe_min(", stdout);
18079 process_mips_fpe_exception (option.info & OEX_FPU_MIN);
18080 fputs (") fpe_max(", stdout);
18081 process_mips_fpe_exception ((option.info & OEX_FPU_MAX) >> 8);
18082 fputs (")", stdout);
18083
18084 if (option.info & OEX_PAGE0)
18085 fputs (" PAGE0", stdout);
18086 if (option.info & OEX_SMM)
18087 fputs (" SMM", stdout);
18088 if (option.info & OEX_FPDBUG)
18089 fputs (" FPDBUG", stdout);
18090 if (option.info & OEX_DISMISS)
18091 fputs (" DISMISS", stdout);
18092 break;
18093
18094 case ODK_PAD:
18095 fputs (" PAD ", stdout);
18096 if (option.info & OPAD_PREFIX)
18097 fputs (" PREFIX", stdout);
18098 if (option.info & OPAD_POSTFIX)
18099 fputs (" POSTFIX", stdout);
18100 if (option.info & OPAD_SYMBOL)
18101 fputs (" SYMBOL", stdout);
18102 break;
18103
18104 case ODK_HWPATCH:
18105 fputs (" HWPATCH ", stdout);
18106 if (option.info & OHW_R4KEOP)
18107 fputs (" R4KEOP", stdout);
18108 if (option.info & OHW_R8KPFETCH)
18109 fputs (" R8KPFETCH", stdout);
18110 if (option.info & OHW_R5KEOP)
18111 fputs (" R5KEOP", stdout);
18112 if (option.info & OHW_R5KCVTL)
18113 fputs (" R5KCVTL", stdout);
18114 break;
18115
18116 case ODK_FILL:
18117 fputs (" FILL ", stdout);
18118 /* XXX Print content of info word? */
18119 break;
18120
18121 case ODK_TAGS:
18122 fputs (" TAGS ", stdout);
18123 /* XXX Print content of info word? */
18124 break;
18125
18126 case ODK_HWAND:
18127 fputs (" HWAND ", stdout);
18128 if (option.info & OHWA0_R4KEOP_CHECKED)
18129 fputs (" R4KEOP_CHECKED", stdout);
18130 if (option.info & OHWA0_R4KEOP_CLEAN)
18131 fputs (" R4KEOP_CLEAN", stdout);
18132 break;
18133
18134 case ODK_HWOR:
18135 fputs (" HWOR ", stdout);
18136 if (option.info & OHWA0_R4KEOP_CHECKED)
18137 fputs (" R4KEOP_CHECKED", stdout);
18138 if (option.info & OHWA0_R4KEOP_CLEAN)
18139 fputs (" R4KEOP_CLEAN", stdout);
18140 break;
18141
18142 case ODK_GP_GROUP:
18143 printf (" GP_GROUP %#06x self-contained %#06x",
18144 option.info & OGP_GROUP,
18145 (option.info & OGP_SELF) >> 16);
18146 break;
18147
18148 case ODK_IDENT:
18149 printf (" IDENT %#06x self-contained %#06x",
18150 option.info & OGP_GROUP,
18151 (option.info & OGP_SELF) >> 16);
18152 break;
18153
18154 default:
18155 /* This shouldn't happen. */
18156 printf (" %3d ??? %" PRId16 " %" PRIx32,
18157 option.kind, option.section, option.info);
18158 break;
18159 }
18160
18161 len = sizeof (* eopt);
18162 while (len < option.size)
18163 {
18164 unsigned char datum = *((unsigned char *) eoption + len);
18165
18166 if (ISPRINT (datum))
18167 printf ("%c", datum);
18168 else
18169 printf ("\\%03o", datum);
18170 len ++;
18171 }
18172 fputs ("\n", stdout);
18173
18174 offset += option.size;
18175 }
18176 free (eopt);
18177 }
18178 else
18179 res = false;
18180 }
18181
18182 if (conflicts_offset != 0 && conflictsno != 0)
18183 {
18184 Elf32_Conflict * iconf;
18185 size_t cnt;
18186
18187 if (filedata->dynamic_symbols == NULL)
18188 {
18189 error (_("conflict list found without a dynamic symbol table\n"));
18190 return false;
18191 }
18192
18193 /* PR 21345 - print a slightly more helpful error message
18194 if we are sure that the cmalloc will fail. */
18195 if (conflictsno > filedata->file_size / sizeof (* iconf))
18196 {
18197 error (_("Overlarge number of conflicts detected: %lx\n"),
18198 (long) conflictsno);
18199 return false;
18200 }
18201
18202 iconf = (Elf32_Conflict *) cmalloc (conflictsno, sizeof (* iconf));
18203 if (iconf == NULL)
18204 {
18205 error (_("Out of memory allocating space for dynamic conflicts\n"));
18206 return false;
18207 }
18208
18209 if (is_32bit_elf)
18210 {
18211 Elf32_External_Conflict * econf32;
18212
18213 econf32 = (Elf32_External_Conflict *)
18214 get_data (NULL, filedata, conflicts_offset,
18215 sizeof (*econf32), conflictsno, _("conflict"));
18216 if (!econf32)
18217 {
18218 free (iconf);
18219 return false;
18220 }
18221
18222 for (cnt = 0; cnt < conflictsno; ++cnt)
18223 iconf[cnt] = BYTE_GET (econf32[cnt]);
18224
18225 free (econf32);
18226 }
18227 else
18228 {
18229 Elf64_External_Conflict * econf64;
18230
18231 econf64 = (Elf64_External_Conflict *)
18232 get_data (NULL, filedata, conflicts_offset,
18233 sizeof (*econf64), conflictsno, _("conflict"));
18234 if (!econf64)
18235 {
18236 free (iconf);
18237 return false;
18238 }
18239
18240 for (cnt = 0; cnt < conflictsno; ++cnt)
18241 iconf[cnt] = BYTE_GET (econf64[cnt]);
18242
18243 free (econf64);
18244 }
18245
18246 printf (ngettext ("\nSection '.conflict' contains %lu entry:\n",
18247 "\nSection '.conflict' contains %lu entries:\n",
18248 (unsigned long) conflictsno),
18249 (unsigned long) conflictsno);
18250 puts (_(" Num: Index Value Name"));
18251
18252 for (cnt = 0; cnt < conflictsno; ++cnt)
18253 {
18254 printf ("%5lu: %8lu ", (unsigned long) cnt, iconf[cnt]);
18255
18256 if (iconf[cnt] >= filedata->num_dynamic_syms)
18257 printf (_("<corrupt symbol index>"));
18258 else
18259 {
18260 Elf_Internal_Sym * psym;
18261
18262 psym = & filedata->dynamic_symbols[iconf[cnt]];
18263 print_vma (psym->st_value, FULL_HEX);
18264 putchar (' ');
18265 if (VALID_DYNAMIC_NAME (filedata, psym->st_name))
18266 print_symbol (25, GET_DYNAMIC_NAME (filedata, psym->st_name));
18267 else
18268 printf (_("<corrupt: %14ld>"), psym->st_name);
18269 }
18270 putchar ('\n');
18271 }
18272
18273 free (iconf);
18274 }
18275
18276 if (pltgot != 0 && local_gotno != 0)
18277 {
18278 bfd_vma ent, local_end, global_end;
18279 size_t i, offset;
18280 unsigned char * data;
18281 unsigned char * data_end;
18282 int addr_size;
18283
18284 ent = pltgot;
18285 addr_size = (is_32bit_elf ? 4 : 8);
18286 local_end = pltgot + local_gotno * addr_size;
18287
18288 /* PR binutils/17533 file: 012-111227-0.004 */
18289 if (symtabno < gotsym)
18290 {
18291 error (_("The GOT symbol offset (%lu) is greater than the symbol table size (%lu)\n"),
18292 (unsigned long) gotsym, (unsigned long) symtabno);
18293 return false;
18294 }
18295
18296 global_end = local_end + (symtabno - gotsym) * addr_size;
18297 /* PR 17531: file: 54c91a34. */
18298 if (global_end < local_end)
18299 {
18300 error (_("Too many GOT symbols: %lu\n"), (unsigned long) symtabno);
18301 return false;
18302 }
18303
18304 offset = offset_from_vma (filedata, pltgot, global_end - pltgot);
18305 data = (unsigned char *) get_data (NULL, filedata, offset,
18306 global_end - pltgot, 1,
18307 _("Global Offset Table data"));
18308 /* PR 12855: Null data is handled gracefully throughout. */
18309 data_end = data + (global_end - pltgot);
18310
18311 printf (_("\nPrimary GOT:\n"));
18312 printf (_(" Canonical gp value: "));
18313 print_vma (pltgot + 0x7ff0, LONG_HEX);
18314 printf ("\n\n");
18315
18316 printf (_(" Reserved entries:\n"));
18317 printf (_(" %*s %10s %*s Purpose\n"),
18318 addr_size * 2, _("Address"), _("Access"),
18319 addr_size * 2, _("Initial"));
18320 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18321 printf (_(" Lazy resolver\n"));
18322 if (ent == (bfd_vma) -1)
18323 goto got_print_fail;
18324
18325 /* Check for the MSB of GOT[1] being set, denoting a GNU object.
18326 This entry will be used by some runtime loaders, to store the
18327 module pointer. Otherwise this is an ordinary local entry.
18328 PR 21344: Check for the entry being fully available before
18329 fetching it. */
18330 if (data
18331 && data + ent - pltgot + addr_size <= data_end
18332 && (byte_get (data + ent - pltgot, addr_size)
18333 >> (addr_size * 8 - 1)) != 0)
18334 {
18335 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18336 printf (_(" Module pointer (GNU extension)\n"));
18337 if (ent == (bfd_vma) -1)
18338 goto got_print_fail;
18339 }
18340 printf ("\n");
18341
18342 if (data != NULL && ent < local_end)
18343 {
18344 printf (_(" Local entries:\n"));
18345 printf (" %*s %10s %*s\n",
18346 addr_size * 2, _("Address"), _("Access"),
18347 addr_size * 2, _("Initial"));
18348 while (ent < local_end)
18349 {
18350 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18351 printf ("\n");
18352 if (ent == (bfd_vma) -1)
18353 goto got_print_fail;
18354 }
18355 printf ("\n");
18356 }
18357
18358 if (data != NULL && gotsym < symtabno)
18359 {
18360 int sym_width;
18361
18362 printf (_(" Global entries:\n"));
18363 printf (" %*s %10s %*s %*s %-7s %3s %s\n",
18364 addr_size * 2, _("Address"),
18365 _("Access"),
18366 addr_size * 2, _("Initial"),
18367 addr_size * 2, _("Sym.Val."),
18368 _("Type"),
18369 /* Note for translators: "Ndx" = abbreviated form of "Index". */
18370 _("Ndx"), _("Name"));
18371
18372 sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
18373
18374 for (i = gotsym; i < symtabno; i++)
18375 {
18376 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18377 printf (" ");
18378
18379 if (filedata->dynamic_symbols == NULL)
18380 printf (_("<no dynamic symbols>"));
18381 else if (i < filedata->num_dynamic_syms)
18382 {
18383 Elf_Internal_Sym * psym = filedata->dynamic_symbols + i;
18384
18385 print_vma (psym->st_value, LONG_HEX);
18386 printf (" %-7s %3s ",
18387 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
18388 get_symbol_index_type (filedata, psym->st_shndx));
18389
18390 if (VALID_DYNAMIC_NAME (filedata, psym->st_name))
18391 print_symbol (sym_width,
18392 GET_DYNAMIC_NAME (filedata, psym->st_name));
18393 else
18394 printf (_("<corrupt: %14ld>"), psym->st_name);
18395 }
18396 else
18397 printf (_("<symbol index %lu exceeds number of dynamic symbols>"),
18398 (unsigned long) i);
18399
18400 printf ("\n");
18401 if (ent == (bfd_vma) -1)
18402 break;
18403 }
18404 printf ("\n");
18405 }
18406
18407 got_print_fail:
18408 free (data);
18409 }
18410
18411 if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
18412 {
18413 bfd_vma ent, end;
18414 size_t offset, rel_offset;
18415 unsigned long count, i;
18416 unsigned char * data;
18417 int addr_size, sym_width;
18418 Elf_Internal_Rela * rels;
18419
18420 rel_offset = offset_from_vma (filedata, jmprel, pltrelsz);
18421 if (pltrel == DT_RELA)
18422 {
18423 if (!slurp_rela_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
18424 return false;
18425 }
18426 else
18427 {
18428 if (!slurp_rel_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
18429 return false;
18430 }
18431
18432 ent = mips_pltgot;
18433 addr_size = (is_32bit_elf ? 4 : 8);
18434 end = mips_pltgot + (2 + count) * addr_size;
18435
18436 offset = offset_from_vma (filedata, mips_pltgot, end - mips_pltgot);
18437 data = (unsigned char *) get_data (NULL, filedata, offset, end - mips_pltgot,
18438 1, _("Procedure Linkage Table data"));
18439 if (data == NULL)
18440 {
18441 free (rels);
18442 return false;
18443 }
18444
18445 printf ("\nPLT GOT:\n\n");
18446 printf (_(" Reserved entries:\n"));
18447 printf (_(" %*s %*s Purpose\n"),
18448 addr_size * 2, _("Address"), addr_size * 2, _("Initial"));
18449 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
18450 printf (_(" PLT lazy resolver\n"));
18451 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
18452 printf (_(" Module pointer\n"));
18453 printf ("\n");
18454
18455 printf (_(" Entries:\n"));
18456 printf (" %*s %*s %*s %-7s %3s %s\n",
18457 addr_size * 2, _("Address"),
18458 addr_size * 2, _("Initial"),
18459 addr_size * 2, _("Sym.Val."), _("Type"), _("Ndx"), _("Name"));
18460 sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
18461 for (i = 0; i < count; i++)
18462 {
18463 unsigned long idx = get_reloc_symindex (rels[i].r_info);
18464
18465 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
18466 printf (" ");
18467
18468 if (idx >= filedata->num_dynamic_syms)
18469 printf (_("<corrupt symbol index: %lu>"), idx);
18470 else
18471 {
18472 Elf_Internal_Sym * psym = filedata->dynamic_symbols + idx;
18473
18474 print_vma (psym->st_value, LONG_HEX);
18475 printf (" %-7s %3s ",
18476 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
18477 get_symbol_index_type (filedata, psym->st_shndx));
18478 if (VALID_DYNAMIC_NAME (filedata, psym->st_name))
18479 print_symbol (sym_width,
18480 GET_DYNAMIC_NAME (filedata, psym->st_name));
18481 else
18482 printf (_("<corrupt: %14ld>"), psym->st_name);
18483 }
18484 printf ("\n");
18485 }
18486 printf ("\n");
18487
18488 free (data);
18489 free (rels);
18490 }
18491
18492 return res;
18493 }
18494
18495 static bool
18496 process_nds32_specific (Filedata * filedata)
18497 {
18498 Elf_Internal_Shdr *sect = NULL;
18499
18500 sect = find_section (filedata, ".nds32_e_flags");
18501 if (sect != NULL && sect->sh_size >= 4)
18502 {
18503 unsigned char *buf;
18504 unsigned int flag;
18505
18506 printf ("\nNDS32 elf flags section:\n");
18507 buf = get_data (NULL, filedata, sect->sh_offset, 1, 4,
18508 _("NDS32 elf flags section"));
18509
18510 if (buf == NULL)
18511 return false;
18512
18513 flag = byte_get (buf, 4);
18514 free (buf);
18515 switch (flag & 0x3)
18516 {
18517 case 0:
18518 printf ("(VEC_SIZE):\tNo entry.\n");
18519 break;
18520 case 1:
18521 printf ("(VEC_SIZE):\t4 bytes\n");
18522 break;
18523 case 2:
18524 printf ("(VEC_SIZE):\t16 bytes\n");
18525 break;
18526 case 3:
18527 printf ("(VEC_SIZE):\treserved\n");
18528 break;
18529 }
18530 }
18531
18532 return true;
18533 }
18534
18535 static bool
18536 process_gnu_liblist (Filedata * filedata)
18537 {
18538 Elf_Internal_Shdr * section;
18539 Elf_Internal_Shdr * string_sec;
18540 Elf32_External_Lib * elib;
18541 char * strtab;
18542 size_t strtab_size;
18543 size_t cnt;
18544 unsigned long num_liblist;
18545 unsigned i;
18546 bool res = true;
18547
18548 if (! do_arch)
18549 return true;
18550
18551 for (i = 0, section = filedata->section_headers;
18552 i < filedata->file_header.e_shnum;
18553 i++, section++)
18554 {
18555 switch (section->sh_type)
18556 {
18557 case SHT_GNU_LIBLIST:
18558 if (section->sh_link >= filedata->file_header.e_shnum)
18559 break;
18560
18561 elib = (Elf32_External_Lib *)
18562 get_data (NULL, filedata, section->sh_offset, 1, section->sh_size,
18563 _("liblist section data"));
18564
18565 if (elib == NULL)
18566 {
18567 res = false;
18568 break;
18569 }
18570
18571 string_sec = filedata->section_headers + section->sh_link;
18572 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
18573 string_sec->sh_size,
18574 _("liblist string table"));
18575 if (strtab == NULL
18576 || section->sh_entsize != sizeof (Elf32_External_Lib))
18577 {
18578 free (elib);
18579 free (strtab);
18580 res = false;
18581 break;
18582 }
18583 strtab_size = string_sec->sh_size;
18584
18585 num_liblist = section->sh_size / sizeof (Elf32_External_Lib);
18586 printf (ngettext ("\nLibrary list section '%s' contains %lu entries:\n",
18587 "\nLibrary list section '%s' contains %lu entries:\n",
18588 num_liblist),
18589 printable_section_name (filedata, section),
18590 num_liblist);
18591
18592 puts (_(" Library Time Stamp Checksum Version Flags"));
18593
18594 for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
18595 ++cnt)
18596 {
18597 Elf32_Lib liblist;
18598 time_t atime;
18599 char timebuf[128];
18600 struct tm * tmp;
18601
18602 liblist.l_name = BYTE_GET (elib[cnt].l_name);
18603 atime = BYTE_GET (elib[cnt].l_time_stamp);
18604 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
18605 liblist.l_version = BYTE_GET (elib[cnt].l_version);
18606 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
18607
18608 tmp = gmtime (&atime);
18609 snprintf (timebuf, sizeof (timebuf),
18610 "%04u-%02u-%02uT%02u:%02u:%02u",
18611 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
18612 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
18613
18614 printf ("%3lu: ", (unsigned long) cnt);
18615 if (do_wide)
18616 printf ("%-20s", liblist.l_name < strtab_size
18617 ? strtab + liblist.l_name : _("<corrupt>"));
18618 else
18619 printf ("%-20.20s", liblist.l_name < strtab_size
18620 ? strtab + liblist.l_name : _("<corrupt>"));
18621 printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
18622 liblist.l_version, liblist.l_flags);
18623 }
18624
18625 free (elib);
18626 free (strtab);
18627 }
18628 }
18629
18630 return res;
18631 }
18632
18633 static const char *
18634 get_note_type (Filedata * filedata, unsigned e_type)
18635 {
18636 static char buff[64];
18637
18638 if (filedata->file_header.e_type == ET_CORE)
18639 switch (e_type)
18640 {
18641 case NT_AUXV:
18642 return _("NT_AUXV (auxiliary vector)");
18643 case NT_PRSTATUS:
18644 return _("NT_PRSTATUS (prstatus structure)");
18645 case NT_FPREGSET:
18646 return _("NT_FPREGSET (floating point registers)");
18647 case NT_PRPSINFO:
18648 return _("NT_PRPSINFO (prpsinfo structure)");
18649 case NT_TASKSTRUCT:
18650 return _("NT_TASKSTRUCT (task structure)");
18651 case NT_GDB_TDESC:
18652 return _("NT_GDB_TDESC (GDB XML target description)");
18653 case NT_PRXFPREG:
18654 return _("NT_PRXFPREG (user_xfpregs structure)");
18655 case NT_PPC_VMX:
18656 return _("NT_PPC_VMX (ppc Altivec registers)");
18657 case NT_PPC_VSX:
18658 return _("NT_PPC_VSX (ppc VSX registers)");
18659 case NT_PPC_TAR:
18660 return _("NT_PPC_TAR (ppc TAR register)");
18661 case NT_PPC_PPR:
18662 return _("NT_PPC_PPR (ppc PPR register)");
18663 case NT_PPC_DSCR:
18664 return _("NT_PPC_DSCR (ppc DSCR register)");
18665 case NT_PPC_EBB:
18666 return _("NT_PPC_EBB (ppc EBB registers)");
18667 case NT_PPC_PMU:
18668 return _("NT_PPC_PMU (ppc PMU registers)");
18669 case NT_PPC_TM_CGPR:
18670 return _("NT_PPC_TM_CGPR (ppc checkpointed GPR registers)");
18671 case NT_PPC_TM_CFPR:
18672 return _("NT_PPC_TM_CFPR (ppc checkpointed floating point registers)");
18673 case NT_PPC_TM_CVMX:
18674 return _("NT_PPC_TM_CVMX (ppc checkpointed Altivec registers)");
18675 case NT_PPC_TM_CVSX:
18676 return _("NT_PPC_TM_CVSX (ppc checkpointed VSX registers)");
18677 case NT_PPC_TM_SPR:
18678 return _("NT_PPC_TM_SPR (ppc TM special purpose registers)");
18679 case NT_PPC_TM_CTAR:
18680 return _("NT_PPC_TM_CTAR (ppc checkpointed TAR register)");
18681 case NT_PPC_TM_CPPR:
18682 return _("NT_PPC_TM_CPPR (ppc checkpointed PPR register)");
18683 case NT_PPC_TM_CDSCR:
18684 return _("NT_PPC_TM_CDSCR (ppc checkpointed DSCR register)");
18685 case NT_386_TLS:
18686 return _("NT_386_TLS (x86 TLS information)");
18687 case NT_386_IOPERM:
18688 return _("NT_386_IOPERM (x86 I/O permissions)");
18689 case NT_X86_XSTATE:
18690 return _("NT_X86_XSTATE (x86 XSAVE extended state)");
18691 case NT_X86_CET:
18692 return _("NT_X86_CET (x86 CET state)");
18693 case NT_S390_HIGH_GPRS:
18694 return _("NT_S390_HIGH_GPRS (s390 upper register halves)");
18695 case NT_S390_TIMER:
18696 return _("NT_S390_TIMER (s390 timer register)");
18697 case NT_S390_TODCMP:
18698 return _("NT_S390_TODCMP (s390 TOD comparator register)");
18699 case NT_S390_TODPREG:
18700 return _("NT_S390_TODPREG (s390 TOD programmable register)");
18701 case NT_S390_CTRS:
18702 return _("NT_S390_CTRS (s390 control registers)");
18703 case NT_S390_PREFIX:
18704 return _("NT_S390_PREFIX (s390 prefix register)");
18705 case NT_S390_LAST_BREAK:
18706 return _("NT_S390_LAST_BREAK (s390 last breaking event address)");
18707 case NT_S390_SYSTEM_CALL:
18708 return _("NT_S390_SYSTEM_CALL (s390 system call restart data)");
18709 case NT_S390_TDB:
18710 return _("NT_S390_TDB (s390 transaction diagnostic block)");
18711 case NT_S390_VXRS_LOW:
18712 return _("NT_S390_VXRS_LOW (s390 vector registers 0-15 upper half)");
18713 case NT_S390_VXRS_HIGH:
18714 return _("NT_S390_VXRS_HIGH (s390 vector registers 16-31)");
18715 case NT_S390_GS_CB:
18716 return _("NT_S390_GS_CB (s390 guarded-storage registers)");
18717 case NT_S390_GS_BC:
18718 return _("NT_S390_GS_BC (s390 guarded-storage broadcast control)");
18719 case NT_ARM_VFP:
18720 return _("NT_ARM_VFP (arm VFP registers)");
18721 case NT_ARM_TLS:
18722 return _("NT_ARM_TLS (AArch TLS registers)");
18723 case NT_ARM_HW_BREAK:
18724 return _("NT_ARM_HW_BREAK (AArch hardware breakpoint registers)");
18725 case NT_ARM_HW_WATCH:
18726 return _("NT_ARM_HW_WATCH (AArch hardware watchpoint registers)");
18727 case NT_ARM_SVE:
18728 return _("NT_ARM_SVE (AArch SVE registers)");
18729 case NT_ARM_PAC_MASK:
18730 return _("NT_ARM_PAC_MASK (AArch pointer authentication code masks)");
18731 case NT_ARM_TAGGED_ADDR_CTRL:
18732 return _("NT_ARM_TAGGED_ADDR_CTRL (AArch tagged address control)");
18733 case NT_ARC_V2:
18734 return _("NT_ARC_V2 (ARC HS accumulator/extra registers)");
18735 case NT_RISCV_CSR:
18736 return _("NT_RISCV_CSR (RISC-V control and status registers)");
18737 case NT_PSTATUS:
18738 return _("NT_PSTATUS (pstatus structure)");
18739 case NT_FPREGS:
18740 return _("NT_FPREGS (floating point registers)");
18741 case NT_PSINFO:
18742 return _("NT_PSINFO (psinfo structure)");
18743 case NT_LWPSTATUS:
18744 return _("NT_LWPSTATUS (lwpstatus_t structure)");
18745 case NT_LWPSINFO:
18746 return _("NT_LWPSINFO (lwpsinfo_t structure)");
18747 case NT_WIN32PSTATUS:
18748 return _("NT_WIN32PSTATUS (win32_pstatus structure)");
18749 case NT_SIGINFO:
18750 return _("NT_SIGINFO (siginfo_t data)");
18751 case NT_FILE:
18752 return _("NT_FILE (mapped files)");
18753 case NT_MEMTAG:
18754 return _("NT_MEMTAG (memory tags)");
18755 default:
18756 break;
18757 }
18758 else
18759 switch (e_type)
18760 {
18761 case NT_VERSION:
18762 return _("NT_VERSION (version)");
18763 case NT_ARCH:
18764 return _("NT_ARCH (architecture)");
18765 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
18766 return _("OPEN");
18767 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
18768 return _("func");
18769 default:
18770 break;
18771 }
18772
18773 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
18774 return buff;
18775 }
18776
18777 static bool
18778 print_core_note (Elf_Internal_Note *pnote)
18779 {
18780 unsigned int addr_size = is_32bit_elf ? 4 : 8;
18781 bfd_vma count, page_size;
18782 unsigned char *descdata, *filenames, *descend;
18783
18784 if (pnote->type != NT_FILE)
18785 {
18786 if (do_wide)
18787 printf ("\n");
18788 return true;
18789 }
18790
18791 #ifndef BFD64
18792 if (!is_32bit_elf)
18793 {
18794 printf (_(" Cannot decode 64-bit note in 32-bit build\n"));
18795 /* Still "successful". */
18796 return true;
18797 }
18798 #endif
18799
18800 if (pnote->descsz < 2 * addr_size)
18801 {
18802 error (_(" Malformed note - too short for header\n"));
18803 return false;
18804 }
18805
18806 descdata = (unsigned char *) pnote->descdata;
18807 descend = descdata + pnote->descsz;
18808
18809 if (descdata[pnote->descsz - 1] != '\0')
18810 {
18811 error (_(" Malformed note - does not end with \\0\n"));
18812 return false;
18813 }
18814
18815 count = byte_get (descdata, addr_size);
18816 descdata += addr_size;
18817
18818 page_size = byte_get (descdata, addr_size);
18819 descdata += addr_size;
18820
18821 if (count > ((bfd_vma) -1 - 2 * addr_size) / (3 * addr_size)
18822 || pnote->descsz < 2 * addr_size + count * 3 * addr_size)
18823 {
18824 error (_(" Malformed note - too short for supplied file count\n"));
18825 return false;
18826 }
18827
18828 printf (_(" Page size: "));
18829 print_vma (page_size, DEC);
18830 printf ("\n");
18831
18832 printf (_(" %*s%*s%*s\n"),
18833 (int) (2 + 2 * addr_size), _("Start"),
18834 (int) (4 + 2 * addr_size), _("End"),
18835 (int) (4 + 2 * addr_size), _("Page Offset"));
18836 filenames = descdata + count * 3 * addr_size;
18837 while (count-- > 0)
18838 {
18839 bfd_vma start, end, file_ofs;
18840
18841 if (filenames == descend)
18842 {
18843 error (_(" Malformed note - filenames end too early\n"));
18844 return false;
18845 }
18846
18847 start = byte_get (descdata, addr_size);
18848 descdata += addr_size;
18849 end = byte_get (descdata, addr_size);
18850 descdata += addr_size;
18851 file_ofs = byte_get (descdata, addr_size);
18852 descdata += addr_size;
18853
18854 printf (" ");
18855 print_vma (start, FULL_HEX);
18856 printf (" ");
18857 print_vma (end, FULL_HEX);
18858 printf (" ");
18859 print_vma (file_ofs, FULL_HEX);
18860 printf ("\n %s\n", filenames);
18861
18862 filenames += 1 + strlen ((char *) filenames);
18863 }
18864
18865 return true;
18866 }
18867
18868 static const char *
18869 get_gnu_elf_note_type (unsigned e_type)
18870 {
18871 /* NB/ Keep this switch statement in sync with print_gnu_note (). */
18872 switch (e_type)
18873 {
18874 case NT_GNU_ABI_TAG:
18875 return _("NT_GNU_ABI_TAG (ABI version tag)");
18876 case NT_GNU_HWCAP:
18877 return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
18878 case NT_GNU_BUILD_ID:
18879 return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
18880 case NT_GNU_GOLD_VERSION:
18881 return _("NT_GNU_GOLD_VERSION (gold version)");
18882 case NT_GNU_PROPERTY_TYPE_0:
18883 return _("NT_GNU_PROPERTY_TYPE_0");
18884 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
18885 return _("NT_GNU_BUILD_ATTRIBUTE_OPEN");
18886 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
18887 return _("NT_GNU_BUILD_ATTRIBUTE_FUNC");
18888 default:
18889 {
18890 static char buff[64];
18891
18892 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
18893 return buff;
18894 }
18895 }
18896 }
18897
18898 static void
18899 decode_x86_compat_isa (unsigned int bitmask)
18900 {
18901 while (bitmask)
18902 {
18903 unsigned int bit = bitmask & (- bitmask);
18904
18905 bitmask &= ~ bit;
18906 switch (bit)
18907 {
18908 case GNU_PROPERTY_X86_COMPAT_ISA_1_486:
18909 printf ("i486");
18910 break;
18911 case GNU_PROPERTY_X86_COMPAT_ISA_1_586:
18912 printf ("586");
18913 break;
18914 case GNU_PROPERTY_X86_COMPAT_ISA_1_686:
18915 printf ("686");
18916 break;
18917 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE:
18918 printf ("SSE");
18919 break;
18920 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE2:
18921 printf ("SSE2");
18922 break;
18923 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE3:
18924 printf ("SSE3");
18925 break;
18926 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSSE3:
18927 printf ("SSSE3");
18928 break;
18929 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_1:
18930 printf ("SSE4_1");
18931 break;
18932 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_2:
18933 printf ("SSE4_2");
18934 break;
18935 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX:
18936 printf ("AVX");
18937 break;
18938 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX2:
18939 printf ("AVX2");
18940 break;
18941 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512F:
18942 printf ("AVX512F");
18943 break;
18944 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512CD:
18945 printf ("AVX512CD");
18946 break;
18947 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512ER:
18948 printf ("AVX512ER");
18949 break;
18950 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512PF:
18951 printf ("AVX512PF");
18952 break;
18953 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512VL:
18954 printf ("AVX512VL");
18955 break;
18956 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512DQ:
18957 printf ("AVX512DQ");
18958 break;
18959 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512BW:
18960 printf ("AVX512BW");
18961 break;
18962 default:
18963 printf (_("<unknown: %x>"), bit);
18964 break;
18965 }
18966 if (bitmask)
18967 printf (", ");
18968 }
18969 }
18970
18971 static void
18972 decode_x86_compat_2_isa (unsigned int bitmask)
18973 {
18974 if (!bitmask)
18975 {
18976 printf (_("<None>"));
18977 return;
18978 }
18979
18980 while (bitmask)
18981 {
18982 unsigned int bit = bitmask & (- bitmask);
18983
18984 bitmask &= ~ bit;
18985 switch (bit)
18986 {
18987 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_CMOV:
18988 printf ("CMOV");
18989 break;
18990 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE:
18991 printf ("SSE");
18992 break;
18993 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE2:
18994 printf ("SSE2");
18995 break;
18996 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE3:
18997 printf ("SSE3");
18998 break;
18999 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSSE3:
19000 printf ("SSSE3");
19001 break;
19002 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_1:
19003 printf ("SSE4_1");
19004 break;
19005 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_2:
19006 printf ("SSE4_2");
19007 break;
19008 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX:
19009 printf ("AVX");
19010 break;
19011 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX2:
19012 printf ("AVX2");
19013 break;
19014 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_FMA:
19015 printf ("FMA");
19016 break;
19017 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512F:
19018 printf ("AVX512F");
19019 break;
19020 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512CD:
19021 printf ("AVX512CD");
19022 break;
19023 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512ER:
19024 printf ("AVX512ER");
19025 break;
19026 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512PF:
19027 printf ("AVX512PF");
19028 break;
19029 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512VL:
19030 printf ("AVX512VL");
19031 break;
19032 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512DQ:
19033 printf ("AVX512DQ");
19034 break;
19035 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512BW:
19036 printf ("AVX512BW");
19037 break;
19038 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4FMAPS:
19039 printf ("AVX512_4FMAPS");
19040 break;
19041 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4VNNIW:
19042 printf ("AVX512_4VNNIW");
19043 break;
19044 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BITALG:
19045 printf ("AVX512_BITALG");
19046 break;
19047 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_IFMA:
19048 printf ("AVX512_IFMA");
19049 break;
19050 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI:
19051 printf ("AVX512_VBMI");
19052 break;
19053 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI2:
19054 printf ("AVX512_VBMI2");
19055 break;
19056 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VNNI:
19057 printf ("AVX512_VNNI");
19058 break;
19059 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BF16:
19060 printf ("AVX512_BF16");
19061 break;
19062 default:
19063 printf (_("<unknown: %x>"), bit);
19064 break;
19065 }
19066 if (bitmask)
19067 printf (", ");
19068 }
19069 }
19070
19071 static void
19072 decode_x86_isa (unsigned int bitmask)
19073 {
19074 while (bitmask)
19075 {
19076 unsigned int bit = bitmask & (- bitmask);
19077
19078 bitmask &= ~ bit;
19079 switch (bit)
19080 {
19081 case GNU_PROPERTY_X86_ISA_1_BASELINE:
19082 printf ("x86-64-baseline");
19083 break;
19084 case GNU_PROPERTY_X86_ISA_1_V2:
19085 printf ("x86-64-v2");
19086 break;
19087 case GNU_PROPERTY_X86_ISA_1_V3:
19088 printf ("x86-64-v3");
19089 break;
19090 case GNU_PROPERTY_X86_ISA_1_V4:
19091 printf ("x86-64-v4");
19092 break;
19093 default:
19094 printf (_("<unknown: %x>"), bit);
19095 break;
19096 }
19097 if (bitmask)
19098 printf (", ");
19099 }
19100 }
19101
19102 static void
19103 decode_x86_feature_1 (unsigned int bitmask)
19104 {
19105 if (!bitmask)
19106 {
19107 printf (_("<None>"));
19108 return;
19109 }
19110
19111 while (bitmask)
19112 {
19113 unsigned int bit = bitmask & (- bitmask);
19114
19115 bitmask &= ~ bit;
19116 switch (bit)
19117 {
19118 case GNU_PROPERTY_X86_FEATURE_1_IBT:
19119 printf ("IBT");
19120 break;
19121 case GNU_PROPERTY_X86_FEATURE_1_SHSTK:
19122 printf ("SHSTK");
19123 break;
19124 case GNU_PROPERTY_X86_FEATURE_1_LAM_U48:
19125 printf ("LAM_U48");
19126 break;
19127 case GNU_PROPERTY_X86_FEATURE_1_LAM_U57:
19128 printf ("LAM_U57");
19129 break;
19130 default:
19131 printf (_("<unknown: %x>"), bit);
19132 break;
19133 }
19134 if (bitmask)
19135 printf (", ");
19136 }
19137 }
19138
19139 static void
19140 decode_x86_feature_2 (unsigned int bitmask)
19141 {
19142 if (!bitmask)
19143 {
19144 printf (_("<None>"));
19145 return;
19146 }
19147
19148 while (bitmask)
19149 {
19150 unsigned int bit = bitmask & (- bitmask);
19151
19152 bitmask &= ~ bit;
19153 switch (bit)
19154 {
19155 case GNU_PROPERTY_X86_FEATURE_2_X86:
19156 printf ("x86");
19157 break;
19158 case GNU_PROPERTY_X86_FEATURE_2_X87:
19159 printf ("x87");
19160 break;
19161 case GNU_PROPERTY_X86_FEATURE_2_MMX:
19162 printf ("MMX");
19163 break;
19164 case GNU_PROPERTY_X86_FEATURE_2_XMM:
19165 printf ("XMM");
19166 break;
19167 case GNU_PROPERTY_X86_FEATURE_2_YMM:
19168 printf ("YMM");
19169 break;
19170 case GNU_PROPERTY_X86_FEATURE_2_ZMM:
19171 printf ("ZMM");
19172 break;
19173 case GNU_PROPERTY_X86_FEATURE_2_TMM:
19174 printf ("TMM");
19175 break;
19176 case GNU_PROPERTY_X86_FEATURE_2_MASK:
19177 printf ("MASK");
19178 break;
19179 case GNU_PROPERTY_X86_FEATURE_2_FXSR:
19180 printf ("FXSR");
19181 break;
19182 case GNU_PROPERTY_X86_FEATURE_2_XSAVE:
19183 printf ("XSAVE");
19184 break;
19185 case GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT:
19186 printf ("XSAVEOPT");
19187 break;
19188 case GNU_PROPERTY_X86_FEATURE_2_XSAVEC:
19189 printf ("XSAVEC");
19190 break;
19191 default:
19192 printf (_("<unknown: %x>"), bit);
19193 break;
19194 }
19195 if (bitmask)
19196 printf (", ");
19197 }
19198 }
19199
19200 static void
19201 decode_aarch64_feature_1_and (unsigned int bitmask)
19202 {
19203 while (bitmask)
19204 {
19205 unsigned int bit = bitmask & (- bitmask);
19206
19207 bitmask &= ~ bit;
19208 switch (bit)
19209 {
19210 case GNU_PROPERTY_AARCH64_FEATURE_1_BTI:
19211 printf ("BTI");
19212 break;
19213
19214 case GNU_PROPERTY_AARCH64_FEATURE_1_PAC:
19215 printf ("PAC");
19216 break;
19217
19218 default:
19219 printf (_("<unknown: %x>"), bit);
19220 break;
19221 }
19222 if (bitmask)
19223 printf (", ");
19224 }
19225 }
19226
19227 static void
19228 print_gnu_property_note (Filedata * filedata, Elf_Internal_Note * pnote)
19229 {
19230 unsigned char * ptr = (unsigned char *) pnote->descdata;
19231 unsigned char * ptr_end = ptr + pnote->descsz;
19232 unsigned int size = is_32bit_elf ? 4 : 8;
19233
19234 printf (_(" Properties: "));
19235
19236 if (pnote->descsz < 8 || (pnote->descsz % size) != 0)
19237 {
19238 printf (_("<corrupt GNU_PROPERTY_TYPE, size = %#lx>\n"), pnote->descsz);
19239 return;
19240 }
19241
19242 while (ptr < ptr_end)
19243 {
19244 unsigned int j;
19245 unsigned int type;
19246 unsigned int datasz;
19247
19248 if ((size_t) (ptr_end - ptr) < 8)
19249 {
19250 printf (_("<corrupt descsz: %#lx>\n"), pnote->descsz);
19251 break;
19252 }
19253
19254 type = byte_get (ptr, 4);
19255 datasz = byte_get (ptr + 4, 4);
19256
19257 ptr += 8;
19258
19259 if (datasz > (size_t) (ptr_end - ptr))
19260 {
19261 printf (_("<corrupt type (%#x) datasz: %#x>\n"),
19262 type, datasz);
19263 break;
19264 }
19265
19266 if (type >= GNU_PROPERTY_LOPROC && type <= GNU_PROPERTY_HIPROC)
19267 {
19268 if (filedata->file_header.e_machine == EM_X86_64
19269 || filedata->file_header.e_machine == EM_IAMCU
19270 || filedata->file_header.e_machine == EM_386)
19271 {
19272 unsigned int bitmask;
19273
19274 if (datasz == 4)
19275 bitmask = byte_get (ptr, 4);
19276 else
19277 bitmask = 0;
19278
19279 switch (type)
19280 {
19281 case GNU_PROPERTY_X86_ISA_1_USED:
19282 if (datasz != 4)
19283 printf (_("x86 ISA used: <corrupt length: %#x> "),
19284 datasz);
19285 else
19286 {
19287 printf ("x86 ISA used: ");
19288 decode_x86_isa (bitmask);
19289 }
19290 goto next;
19291
19292 case GNU_PROPERTY_X86_ISA_1_NEEDED:
19293 if (datasz != 4)
19294 printf (_("x86 ISA needed: <corrupt length: %#x> "),
19295 datasz);
19296 else
19297 {
19298 printf ("x86 ISA needed: ");
19299 decode_x86_isa (bitmask);
19300 }
19301 goto next;
19302
19303 case GNU_PROPERTY_X86_FEATURE_1_AND:
19304 if (datasz != 4)
19305 printf (_("x86 feature: <corrupt length: %#x> "),
19306 datasz);
19307 else
19308 {
19309 printf ("x86 feature: ");
19310 decode_x86_feature_1 (bitmask);
19311 }
19312 goto next;
19313
19314 case GNU_PROPERTY_X86_FEATURE_2_USED:
19315 if (datasz != 4)
19316 printf (_("x86 feature used: <corrupt length: %#x> "),
19317 datasz);
19318 else
19319 {
19320 printf ("x86 feature used: ");
19321 decode_x86_feature_2 (bitmask);
19322 }
19323 goto next;
19324
19325 case GNU_PROPERTY_X86_FEATURE_2_NEEDED:
19326 if (datasz != 4)
19327 printf (_("x86 feature needed: <corrupt length: %#x> "), datasz);
19328 else
19329 {
19330 printf ("x86 feature needed: ");
19331 decode_x86_feature_2 (bitmask);
19332 }
19333 goto next;
19334
19335 case GNU_PROPERTY_X86_COMPAT_ISA_1_USED:
19336 if (datasz != 4)
19337 printf (_("x86 ISA used: <corrupt length: %#x> "),
19338 datasz);
19339 else
19340 {
19341 printf ("x86 ISA used: ");
19342 decode_x86_compat_isa (bitmask);
19343 }
19344 goto next;
19345
19346 case GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED:
19347 if (datasz != 4)
19348 printf (_("x86 ISA needed: <corrupt length: %#x> "),
19349 datasz);
19350 else
19351 {
19352 printf ("x86 ISA needed: ");
19353 decode_x86_compat_isa (bitmask);
19354 }
19355 goto next;
19356
19357 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_USED:
19358 if (datasz != 4)
19359 printf (_("x86 ISA used: <corrupt length: %#x> "),
19360 datasz);
19361 else
19362 {
19363 printf ("x86 ISA used: ");
19364 decode_x86_compat_2_isa (bitmask);
19365 }
19366 goto next;
19367
19368 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_NEEDED:
19369 if (datasz != 4)
19370 printf (_("x86 ISA needed: <corrupt length: %#x> "),
19371 datasz);
19372 else
19373 {
19374 printf ("x86 ISA needed: ");
19375 decode_x86_compat_2_isa (bitmask);
19376 }
19377 goto next;
19378
19379 default:
19380 break;
19381 }
19382 }
19383 else if (filedata->file_header.e_machine == EM_AARCH64)
19384 {
19385 if (type == GNU_PROPERTY_AARCH64_FEATURE_1_AND)
19386 {
19387 printf ("AArch64 feature: ");
19388 if (datasz != 4)
19389 printf (_("<corrupt length: %#x> "), datasz);
19390 else
19391 decode_aarch64_feature_1_and (byte_get (ptr, 4));
19392 goto next;
19393 }
19394 }
19395 }
19396 else
19397 {
19398 switch (type)
19399 {
19400 case GNU_PROPERTY_STACK_SIZE:
19401 printf (_("stack size: "));
19402 if (datasz != size)
19403 printf (_("<corrupt length: %#x> "), datasz);
19404 else
19405 printf ("%#lx", (unsigned long) byte_get (ptr, size));
19406 goto next;
19407
19408 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
19409 printf ("no copy on protected ");
19410 if (datasz)
19411 printf (_("<corrupt length: %#x> "), datasz);
19412 goto next;
19413
19414 default:
19415 break;
19416 }
19417 }
19418
19419 if (type < GNU_PROPERTY_LOPROC)
19420 printf (_("<unknown type %#x data: "), type);
19421 else if (type < GNU_PROPERTY_LOUSER)
19422 printf (_("<processor-specific type %#x data: "), type);
19423 else
19424 printf (_("<application-specific type %#x data: "), type);
19425 for (j = 0; j < datasz; ++j)
19426 printf ("%02x ", ptr[j] & 0xff);
19427 printf (">");
19428
19429 next:
19430 ptr += ((datasz + (size - 1)) & ~ (size - 1));
19431 if (ptr == ptr_end)
19432 break;
19433
19434 if (do_wide)
19435 printf (", ");
19436 else
19437 printf ("\n\t");
19438 }
19439
19440 printf ("\n");
19441 }
19442
19443 static bool
19444 print_gnu_note (Filedata * filedata, Elf_Internal_Note *pnote)
19445 {
19446 /* NB/ Keep this switch statement in sync with get_gnu_elf_note_type (). */
19447 switch (pnote->type)
19448 {
19449 case NT_GNU_BUILD_ID:
19450 {
19451 unsigned long i;
19452
19453 printf (_(" Build ID: "));
19454 for (i = 0; i < pnote->descsz; ++i)
19455 printf ("%02x", pnote->descdata[i] & 0xff);
19456 printf ("\n");
19457 }
19458 break;
19459
19460 case NT_GNU_ABI_TAG:
19461 {
19462 unsigned long os, major, minor, subminor;
19463 const char *osname;
19464
19465 /* PR 17531: file: 030-599401-0.004. */
19466 if (pnote->descsz < 16)
19467 {
19468 printf (_(" <corrupt GNU_ABI_TAG>\n"));
19469 break;
19470 }
19471
19472 os = byte_get ((unsigned char *) pnote->descdata, 4);
19473 major = byte_get ((unsigned char *) pnote->descdata + 4, 4);
19474 minor = byte_get ((unsigned char *) pnote->descdata + 8, 4);
19475 subminor = byte_get ((unsigned char *) pnote->descdata + 12, 4);
19476
19477 switch (os)
19478 {
19479 case GNU_ABI_TAG_LINUX:
19480 osname = "Linux";
19481 break;
19482 case GNU_ABI_TAG_HURD:
19483 osname = "Hurd";
19484 break;
19485 case GNU_ABI_TAG_SOLARIS:
19486 osname = "Solaris";
19487 break;
19488 case GNU_ABI_TAG_FREEBSD:
19489 osname = "FreeBSD";
19490 break;
19491 case GNU_ABI_TAG_NETBSD:
19492 osname = "NetBSD";
19493 break;
19494 case GNU_ABI_TAG_SYLLABLE:
19495 osname = "Syllable";
19496 break;
19497 case GNU_ABI_TAG_NACL:
19498 osname = "NaCl";
19499 break;
19500 default:
19501 osname = "Unknown";
19502 break;
19503 }
19504
19505 printf (_(" OS: %s, ABI: %ld.%ld.%ld\n"), osname,
19506 major, minor, subminor);
19507 }
19508 break;
19509
19510 case NT_GNU_GOLD_VERSION:
19511 {
19512 unsigned long i;
19513
19514 printf (_(" Version: "));
19515 for (i = 0; i < pnote->descsz && pnote->descdata[i] != '\0'; ++i)
19516 printf ("%c", pnote->descdata[i]);
19517 printf ("\n");
19518 }
19519 break;
19520
19521 case NT_GNU_HWCAP:
19522 {
19523 unsigned long num_entries, mask;
19524
19525 /* Hardware capabilities information. Word 0 is the number of entries.
19526 Word 1 is a bitmask of enabled entries. The rest of the descriptor
19527 is a series of entries, where each entry is a single byte followed
19528 by a nul terminated string. The byte gives the bit number to test
19529 if enabled in the bitmask. */
19530 printf (_(" Hardware Capabilities: "));
19531 if (pnote->descsz < 8)
19532 {
19533 error (_("<corrupt GNU_HWCAP>\n"));
19534 return false;
19535 }
19536 num_entries = byte_get ((unsigned char *) pnote->descdata, 4);
19537 mask = byte_get ((unsigned char *) pnote->descdata + 4, 4);
19538 printf (_("num entries: %ld, enabled mask: %lx\n"), num_entries, mask);
19539 /* FIXME: Add code to display the entries... */
19540 }
19541 break;
19542
19543 case NT_GNU_PROPERTY_TYPE_0:
19544 print_gnu_property_note (filedata, pnote);
19545 break;
19546
19547 default:
19548 /* Handle unrecognised types. An error message should have already been
19549 created by get_gnu_elf_note_type(), so all that we need to do is to
19550 display the data. */
19551 {
19552 unsigned long i;
19553
19554 printf (_(" Description data: "));
19555 for (i = 0; i < pnote->descsz; ++i)
19556 printf ("%02x ", pnote->descdata[i] & 0xff);
19557 printf ("\n");
19558 }
19559 break;
19560 }
19561
19562 return true;
19563 }
19564
19565 static const char *
19566 get_v850_elf_note_type (enum v850_notes n_type)
19567 {
19568 static char buff[64];
19569
19570 switch (n_type)
19571 {
19572 case V850_NOTE_ALIGNMENT: return _("Alignment of 8-byte objects");
19573 case V850_NOTE_DATA_SIZE: return _("Sizeof double and long double");
19574 case V850_NOTE_FPU_INFO: return _("Type of FPU support needed");
19575 case V850_NOTE_SIMD_INFO: return _("Use of SIMD instructions");
19576 case V850_NOTE_CACHE_INFO: return _("Use of cache");
19577 case V850_NOTE_MMU_INFO: return _("Use of MMU");
19578 default:
19579 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), n_type);
19580 return buff;
19581 }
19582 }
19583
19584 static bool
19585 print_v850_note (Elf_Internal_Note * pnote)
19586 {
19587 unsigned int val;
19588
19589 if (pnote->descsz != 4)
19590 return false;
19591
19592 val = byte_get ((unsigned char *) pnote->descdata, pnote->descsz);
19593
19594 if (val == 0)
19595 {
19596 printf (_("not set\n"));
19597 return true;
19598 }
19599
19600 switch (pnote->type)
19601 {
19602 case V850_NOTE_ALIGNMENT:
19603 switch (val)
19604 {
19605 case EF_RH850_DATA_ALIGN4: printf (_("4-byte\n")); return true;
19606 case EF_RH850_DATA_ALIGN8: printf (_("8-byte\n")); return true;
19607 }
19608 break;
19609
19610 case V850_NOTE_DATA_SIZE:
19611 switch (val)
19612 {
19613 case EF_RH850_DOUBLE32: printf (_("4-bytes\n")); return true;
19614 case EF_RH850_DOUBLE64: printf (_("8-bytes\n")); return true;
19615 }
19616 break;
19617
19618 case V850_NOTE_FPU_INFO:
19619 switch (val)
19620 {
19621 case EF_RH850_FPU20: printf (_("FPU-2.0\n")); return true;
19622 case EF_RH850_FPU30: printf (_("FPU-3.0\n")); return true;
19623 }
19624 break;
19625
19626 case V850_NOTE_MMU_INFO:
19627 case V850_NOTE_CACHE_INFO:
19628 case V850_NOTE_SIMD_INFO:
19629 if (val == EF_RH850_SIMD)
19630 {
19631 printf (_("yes\n"));
19632 return true;
19633 }
19634 break;
19635
19636 default:
19637 /* An 'unknown note type' message will already have been displayed. */
19638 break;
19639 }
19640
19641 printf (_("unknown value: %x\n"), val);
19642 return false;
19643 }
19644
19645 static bool
19646 process_netbsd_elf_note (Elf_Internal_Note * pnote)
19647 {
19648 unsigned int version;
19649
19650 switch (pnote->type)
19651 {
19652 case NT_NETBSD_IDENT:
19653 if (pnote->descsz < 1)
19654 break;
19655 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
19656 if ((version / 10000) % 100)
19657 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u%s%c)\n", pnote->descsz,
19658 version, version / 100000000, (version / 1000000) % 100,
19659 (version / 10000) % 100 > 26 ? "Z" : "",
19660 'A' + (version / 10000) % 26);
19661 else
19662 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u.%u)\n", pnote->descsz,
19663 version, version / 100000000, (version / 1000000) % 100,
19664 (version / 100) % 100);
19665 return true;
19666
19667 case NT_NETBSD_MARCH:
19668 printf (" NetBSD\t\t0x%08lx\tMARCH <%s>\n", pnote->descsz,
19669 pnote->descdata);
19670 return true;
19671
19672 case NT_NETBSD_PAX:
19673 if (pnote->descsz < 1)
19674 break;
19675 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
19676 printf (" NetBSD\t\t0x%08lx\tPaX <%s%s%s%s%s%s>\n", pnote->descsz,
19677 ((version & NT_NETBSD_PAX_MPROTECT) ? "+mprotect" : ""),
19678 ((version & NT_NETBSD_PAX_NOMPROTECT) ? "-mprotect" : ""),
19679 ((version & NT_NETBSD_PAX_GUARD) ? "+guard" : ""),
19680 ((version & NT_NETBSD_PAX_NOGUARD) ? "-guard" : ""),
19681 ((version & NT_NETBSD_PAX_ASLR) ? "+ASLR" : ""),
19682 ((version & NT_NETBSD_PAX_NOASLR) ? "-ASLR" : ""));
19683 return true;
19684 }
19685
19686 printf (" NetBSD\t0x%08lx\tUnknown note type: (0x%08lx)\n",
19687 pnote->descsz, pnote->type);
19688 return false;
19689 }
19690
19691 static const char *
19692 get_freebsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
19693 {
19694 switch (e_type)
19695 {
19696 case NT_FREEBSD_THRMISC:
19697 return _("NT_THRMISC (thrmisc structure)");
19698 case NT_FREEBSD_PROCSTAT_PROC:
19699 return _("NT_PROCSTAT_PROC (proc data)");
19700 case NT_FREEBSD_PROCSTAT_FILES:
19701 return _("NT_PROCSTAT_FILES (files data)");
19702 case NT_FREEBSD_PROCSTAT_VMMAP:
19703 return _("NT_PROCSTAT_VMMAP (vmmap data)");
19704 case NT_FREEBSD_PROCSTAT_GROUPS:
19705 return _("NT_PROCSTAT_GROUPS (groups data)");
19706 case NT_FREEBSD_PROCSTAT_UMASK:
19707 return _("NT_PROCSTAT_UMASK (umask data)");
19708 case NT_FREEBSD_PROCSTAT_RLIMIT:
19709 return _("NT_PROCSTAT_RLIMIT (rlimit data)");
19710 case NT_FREEBSD_PROCSTAT_OSREL:
19711 return _("NT_PROCSTAT_OSREL (osreldate data)");
19712 case NT_FREEBSD_PROCSTAT_PSSTRINGS:
19713 return _("NT_PROCSTAT_PSSTRINGS (ps_strings data)");
19714 case NT_FREEBSD_PROCSTAT_AUXV:
19715 return _("NT_PROCSTAT_AUXV (auxv data)");
19716 case NT_FREEBSD_PTLWPINFO:
19717 return _("NT_PTLWPINFO (ptrace_lwpinfo structure)");
19718 }
19719 return get_note_type (filedata, e_type);
19720 }
19721
19722 static const char *
19723 get_netbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
19724 {
19725 static char buff[64];
19726
19727 switch (e_type)
19728 {
19729 case NT_NETBSDCORE_PROCINFO:
19730 /* NetBSD core "procinfo" structure. */
19731 return _("NetBSD procinfo structure");
19732
19733 case NT_NETBSDCORE_AUXV:
19734 return _("NetBSD ELF auxiliary vector data");
19735
19736 case NT_NETBSDCORE_LWPSTATUS:
19737 return _("PT_LWPSTATUS (ptrace_lwpstatus structure)");
19738
19739 default:
19740 /* As of Jan 2020 there are no other machine-independent notes
19741 defined for NetBSD core files. If the note type is less
19742 than the start of the machine-dependent note types, we don't
19743 understand it. */
19744
19745 if (e_type < NT_NETBSDCORE_FIRSTMACH)
19746 {
19747 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19748 return buff;
19749 }
19750 break;
19751 }
19752
19753 switch (filedata->file_header.e_machine)
19754 {
19755 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
19756 and PT_GETFPREGS == mach+2. */
19757
19758 case EM_OLD_ALPHA:
19759 case EM_ALPHA:
19760 case EM_SPARC:
19761 case EM_SPARC32PLUS:
19762 case EM_SPARCV9:
19763 switch (e_type)
19764 {
19765 case NT_NETBSDCORE_FIRSTMACH + 0:
19766 return _("PT_GETREGS (reg structure)");
19767 case NT_NETBSDCORE_FIRSTMACH + 2:
19768 return _("PT_GETFPREGS (fpreg structure)");
19769 default:
19770 break;
19771 }
19772 break;
19773
19774 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
19775 There's also old PT___GETREGS40 == mach + 1 for old reg
19776 structure which lacks GBR. */
19777 case EM_SH:
19778 switch (e_type)
19779 {
19780 case NT_NETBSDCORE_FIRSTMACH + 1:
19781 return _("PT___GETREGS40 (old reg structure)");
19782 case NT_NETBSDCORE_FIRSTMACH + 3:
19783 return _("PT_GETREGS (reg structure)");
19784 case NT_NETBSDCORE_FIRSTMACH + 5:
19785 return _("PT_GETFPREGS (fpreg structure)");
19786 default:
19787 break;
19788 }
19789 break;
19790
19791 /* On all other arch's, PT_GETREGS == mach+1 and
19792 PT_GETFPREGS == mach+3. */
19793 default:
19794 switch (e_type)
19795 {
19796 case NT_NETBSDCORE_FIRSTMACH + 1:
19797 return _("PT_GETREGS (reg structure)");
19798 case NT_NETBSDCORE_FIRSTMACH + 3:
19799 return _("PT_GETFPREGS (fpreg structure)");
19800 default:
19801 break;
19802 }
19803 }
19804
19805 snprintf (buff, sizeof (buff), "PT_FIRSTMACH+%d",
19806 e_type - NT_NETBSDCORE_FIRSTMACH);
19807 return buff;
19808 }
19809
19810 static const char *
19811 get_stapsdt_note_type (unsigned e_type)
19812 {
19813 static char buff[64];
19814
19815 switch (e_type)
19816 {
19817 case NT_STAPSDT:
19818 return _("NT_STAPSDT (SystemTap probe descriptors)");
19819
19820 default:
19821 break;
19822 }
19823
19824 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19825 return buff;
19826 }
19827
19828 static bool
19829 print_stapsdt_note (Elf_Internal_Note *pnote)
19830 {
19831 size_t len, maxlen;
19832 unsigned long addr_size = is_32bit_elf ? 4 : 8;
19833 char *data = pnote->descdata;
19834 char *data_end = pnote->descdata + pnote->descsz;
19835 bfd_vma pc, base_addr, semaphore;
19836 char *provider, *probe, *arg_fmt;
19837
19838 if (pnote->descsz < (addr_size * 3))
19839 goto stapdt_note_too_small;
19840
19841 pc = byte_get ((unsigned char *) data, addr_size);
19842 data += addr_size;
19843
19844 base_addr = byte_get ((unsigned char *) data, addr_size);
19845 data += addr_size;
19846
19847 semaphore = byte_get ((unsigned char *) data, addr_size);
19848 data += addr_size;
19849
19850 if (data >= data_end)
19851 goto stapdt_note_too_small;
19852 maxlen = data_end - data;
19853 len = strnlen (data, maxlen);
19854 if (len < maxlen)
19855 {
19856 provider = data;
19857 data += len + 1;
19858 }
19859 else
19860 goto stapdt_note_too_small;
19861
19862 if (data >= data_end)
19863 goto stapdt_note_too_small;
19864 maxlen = data_end - data;
19865 len = strnlen (data, maxlen);
19866 if (len < maxlen)
19867 {
19868 probe = data;
19869 data += len + 1;
19870 }
19871 else
19872 goto stapdt_note_too_small;
19873
19874 if (data >= data_end)
19875 goto stapdt_note_too_small;
19876 maxlen = data_end - data;
19877 len = strnlen (data, maxlen);
19878 if (len < maxlen)
19879 {
19880 arg_fmt = data;
19881 data += len + 1;
19882 }
19883 else
19884 goto stapdt_note_too_small;
19885
19886 printf (_(" Provider: %s\n"), provider);
19887 printf (_(" Name: %s\n"), probe);
19888 printf (_(" Location: "));
19889 print_vma (pc, FULL_HEX);
19890 printf (_(", Base: "));
19891 print_vma (base_addr, FULL_HEX);
19892 printf (_(", Semaphore: "));
19893 print_vma (semaphore, FULL_HEX);
19894 printf ("\n");
19895 printf (_(" Arguments: %s\n"), arg_fmt);
19896
19897 return data == data_end;
19898
19899 stapdt_note_too_small:
19900 printf (_(" <corrupt - note is too small>\n"));
19901 error (_("corrupt stapdt note - the data size is too small\n"));
19902 return false;
19903 }
19904
19905 static const char *
19906 get_ia64_vms_note_type (unsigned e_type)
19907 {
19908 static char buff[64];
19909
19910 switch (e_type)
19911 {
19912 case NT_VMS_MHD:
19913 return _("NT_VMS_MHD (module header)");
19914 case NT_VMS_LNM:
19915 return _("NT_VMS_LNM (language name)");
19916 case NT_VMS_SRC:
19917 return _("NT_VMS_SRC (source files)");
19918 case NT_VMS_TITLE:
19919 return "NT_VMS_TITLE";
19920 case NT_VMS_EIDC:
19921 return _("NT_VMS_EIDC (consistency check)");
19922 case NT_VMS_FPMODE:
19923 return _("NT_VMS_FPMODE (FP mode)");
19924 case NT_VMS_LINKTIME:
19925 return "NT_VMS_LINKTIME";
19926 case NT_VMS_IMGNAM:
19927 return _("NT_VMS_IMGNAM (image name)");
19928 case NT_VMS_IMGID:
19929 return _("NT_VMS_IMGID (image id)");
19930 case NT_VMS_LINKID:
19931 return _("NT_VMS_LINKID (link id)");
19932 case NT_VMS_IMGBID:
19933 return _("NT_VMS_IMGBID (build id)");
19934 case NT_VMS_GSTNAM:
19935 return _("NT_VMS_GSTNAM (sym table name)");
19936 case NT_VMS_ORIG_DYN:
19937 return "NT_VMS_ORIG_DYN";
19938 case NT_VMS_PATCHTIME:
19939 return "NT_VMS_PATCHTIME";
19940 default:
19941 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19942 return buff;
19943 }
19944 }
19945
19946 static bool
19947 print_ia64_vms_note (Elf_Internal_Note * pnote)
19948 {
19949 int maxlen = pnote->descsz;
19950
19951 if (maxlen < 2 || (unsigned long) maxlen != pnote->descsz)
19952 goto desc_size_fail;
19953
19954 switch (pnote->type)
19955 {
19956 case NT_VMS_MHD:
19957 if (maxlen <= 36)
19958 goto desc_size_fail;
19959
19960 int l = (int) strnlen (pnote->descdata + 34, maxlen - 34);
19961
19962 printf (_(" Creation date : %.17s\n"), pnote->descdata);
19963 printf (_(" Last patch date: %.17s\n"), pnote->descdata + 17);
19964 if (l + 34 < maxlen)
19965 {
19966 printf (_(" Module name : %s\n"), pnote->descdata + 34);
19967 if (l + 35 < maxlen)
19968 printf (_(" Module version : %s\n"), pnote->descdata + 34 + l + 1);
19969 else
19970 printf (_(" Module version : <missing>\n"));
19971 }
19972 else
19973 {
19974 printf (_(" Module name : <missing>\n"));
19975 printf (_(" Module version : <missing>\n"));
19976 }
19977 break;
19978
19979 case NT_VMS_LNM:
19980 printf (_(" Language: %.*s\n"), maxlen, pnote->descdata);
19981 break;
19982
19983 #ifdef BFD64
19984 case NT_VMS_FPMODE:
19985 printf (_(" Floating Point mode: "));
19986 if (maxlen < 8)
19987 goto desc_size_fail;
19988 /* FIXME: Generate an error if descsz > 8 ? */
19989
19990 printf ("0x%016" BFD_VMA_FMT "x\n",
19991 (bfd_vma) byte_get ((unsigned char *)pnote->descdata, 8));
19992 break;
19993
19994 case NT_VMS_LINKTIME:
19995 printf (_(" Link time: "));
19996 if (maxlen < 8)
19997 goto desc_size_fail;
19998 /* FIXME: Generate an error if descsz > 8 ? */
19999
20000 print_vms_time
20001 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
20002 printf ("\n");
20003 break;
20004
20005 case NT_VMS_PATCHTIME:
20006 printf (_(" Patch time: "));
20007 if (maxlen < 8)
20008 goto desc_size_fail;
20009 /* FIXME: Generate an error if descsz > 8 ? */
20010
20011 print_vms_time
20012 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
20013 printf ("\n");
20014 break;
20015
20016 case NT_VMS_ORIG_DYN:
20017 if (maxlen < 34)
20018 goto desc_size_fail;
20019
20020 printf (_(" Major id: %u, minor id: %u\n"),
20021 (unsigned) byte_get ((unsigned char *)pnote->descdata, 4),
20022 (unsigned) byte_get ((unsigned char *)pnote->descdata + 4, 4));
20023 printf (_(" Last modified : "));
20024 print_vms_time
20025 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata + 8, 8));
20026 printf (_("\n Link flags : "));
20027 printf ("0x%016" BFD_VMA_FMT "x\n",
20028 (bfd_vma) byte_get ((unsigned char *)pnote->descdata + 16, 8));
20029 printf (_(" Header flags: 0x%08x\n"),
20030 (unsigned) byte_get ((unsigned char *)pnote->descdata + 24, 4));
20031 printf (_(" Image id : %.*s\n"), maxlen - 32, pnote->descdata + 32);
20032 break;
20033 #endif
20034
20035 case NT_VMS_IMGNAM:
20036 printf (_(" Image name: %.*s\n"), maxlen, pnote->descdata);
20037 break;
20038
20039 case NT_VMS_GSTNAM:
20040 printf (_(" Global symbol table name: %.*s\n"), maxlen, pnote->descdata);
20041 break;
20042
20043 case NT_VMS_IMGID:
20044 printf (_(" Image id: %.*s\n"), maxlen, pnote->descdata);
20045 break;
20046
20047 case NT_VMS_LINKID:
20048 printf (_(" Linker id: %.*s\n"), maxlen, pnote->descdata);
20049 break;
20050
20051 default:
20052 return false;
20053 }
20054
20055 return true;
20056
20057 desc_size_fail:
20058 printf (_(" <corrupt - data size is too small>\n"));
20059 error (_("corrupt IA64 note: data size is too small\n"));
20060 return false;
20061 }
20062
20063 struct build_attr_cache {
20064 Filedata *filedata;
20065 char *strtab;
20066 unsigned long strtablen;
20067 Elf_Internal_Sym *symtab;
20068 unsigned long nsyms;
20069 } ba_cache;
20070
20071 /* Find the symbol associated with a build attribute that is attached
20072 to address OFFSET. If PNAME is non-NULL then store the name of
20073 the symbol (if found) in the provided pointer, Returns NULL if a
20074 symbol could not be found. */
20075
20076 static Elf_Internal_Sym *
20077 get_symbol_for_build_attribute (Filedata *filedata,
20078 unsigned long offset,
20079 bool is_open_attr,
20080 const char **pname)
20081 {
20082 Elf_Internal_Sym *saved_sym = NULL;
20083 Elf_Internal_Sym *sym;
20084
20085 if (filedata->section_headers != NULL
20086 && (ba_cache.filedata == NULL || filedata != ba_cache.filedata))
20087 {
20088 Elf_Internal_Shdr * symsec;
20089
20090 free (ba_cache.strtab);
20091 ba_cache.strtab = NULL;
20092 free (ba_cache.symtab);
20093 ba_cache.symtab = NULL;
20094
20095 /* Load the symbol and string sections. */
20096 for (symsec = filedata->section_headers;
20097 symsec < filedata->section_headers + filedata->file_header.e_shnum;
20098 symsec ++)
20099 {
20100 if (symsec->sh_type == SHT_SYMTAB
20101 && get_symtab (filedata, symsec,
20102 &ba_cache.symtab, &ba_cache.nsyms,
20103 &ba_cache.strtab, &ba_cache.strtablen))
20104 break;
20105 }
20106 ba_cache.filedata = filedata;
20107 }
20108
20109 if (ba_cache.symtab == NULL)
20110 return NULL;
20111
20112 /* Find a symbol whose value matches offset. */
20113 for (sym = ba_cache.symtab; sym < ba_cache.symtab + ba_cache.nsyms; sym ++)
20114 if (sym->st_value == offset)
20115 {
20116 if (sym->st_name >= ba_cache.strtablen)
20117 /* Huh ? This should not happen. */
20118 continue;
20119
20120 if (ba_cache.strtab[sym->st_name] == 0)
20121 continue;
20122
20123 /* The AArch64 and ARM architectures define mapping symbols
20124 (eg $d, $x, $t) which we want to ignore. */
20125 if (ba_cache.strtab[sym->st_name] == '$'
20126 && ba_cache.strtab[sym->st_name + 1] != 0
20127 && ba_cache.strtab[sym->st_name + 2] == 0)
20128 continue;
20129
20130 if (is_open_attr)
20131 {
20132 /* For OPEN attributes we prefer GLOBAL over LOCAL symbols
20133 and FILE or OBJECT symbols over NOTYPE symbols. We skip
20134 FUNC symbols entirely. */
20135 switch (ELF_ST_TYPE (sym->st_info))
20136 {
20137 case STT_OBJECT:
20138 case STT_FILE:
20139 saved_sym = sym;
20140 if (sym->st_size)
20141 {
20142 /* If the symbol has a size associated
20143 with it then we can stop searching. */
20144 sym = ba_cache.symtab + ba_cache.nsyms;
20145 }
20146 continue;
20147
20148 case STT_FUNC:
20149 /* Ignore function symbols. */
20150 continue;
20151
20152 default:
20153 break;
20154 }
20155
20156 switch (ELF_ST_BIND (sym->st_info))
20157 {
20158 case STB_GLOBAL:
20159 if (saved_sym == NULL
20160 || ELF_ST_TYPE (saved_sym->st_info) != STT_OBJECT)
20161 saved_sym = sym;
20162 break;
20163
20164 case STB_LOCAL:
20165 if (saved_sym == NULL)
20166 saved_sym = sym;
20167 break;
20168
20169 default:
20170 break;
20171 }
20172 }
20173 else
20174 {
20175 if (ELF_ST_TYPE (sym->st_info) != STT_FUNC)
20176 continue;
20177
20178 saved_sym = sym;
20179 break;
20180 }
20181 }
20182
20183 if (saved_sym && pname)
20184 * pname = ba_cache.strtab + saved_sym->st_name;
20185
20186 return saved_sym;
20187 }
20188
20189 /* Returns true iff addr1 and addr2 are in the same section. */
20190
20191 static bool
20192 same_section (Filedata * filedata, unsigned long addr1, unsigned long addr2)
20193 {
20194 Elf_Internal_Shdr * a1;
20195 Elf_Internal_Shdr * a2;
20196
20197 a1 = find_section_by_address (filedata, addr1);
20198 a2 = find_section_by_address (filedata, addr2);
20199
20200 return a1 == a2 && a1 != NULL;
20201 }
20202
20203 static bool
20204 print_gnu_build_attribute_description (Elf_Internal_Note * pnote,
20205 Filedata * filedata)
20206 {
20207 static unsigned long global_offset = 0;
20208 static unsigned long global_end = 0;
20209 static unsigned long func_offset = 0;
20210 static unsigned long func_end = 0;
20211
20212 Elf_Internal_Sym *sym;
20213 const char *name;
20214 unsigned long start;
20215 unsigned long end;
20216 bool is_open_attr = pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN;
20217
20218 switch (pnote->descsz)
20219 {
20220 case 0:
20221 /* A zero-length description means that the range of
20222 the previous note of the same type should be used. */
20223 if (is_open_attr)
20224 {
20225 if (global_end > global_offset)
20226 printf (_(" Applies to region from %#lx to %#lx\n"),
20227 global_offset, global_end);
20228 else
20229 printf (_(" Applies to region from %#lx\n"), global_offset);
20230 }
20231 else
20232 {
20233 if (func_end > func_offset)
20234 printf (_(" Applies to region from %#lx to %#lx\n"), func_offset, func_end);
20235 else
20236 printf (_(" Applies to region from %#lx\n"), func_offset);
20237 }
20238 return true;
20239
20240 case 4:
20241 start = byte_get ((unsigned char *) pnote->descdata, 4);
20242 end = 0;
20243 break;
20244
20245 case 8:
20246 start = byte_get ((unsigned char *) pnote->descdata, 4);
20247 end = byte_get ((unsigned char *) pnote->descdata + 4, 4);
20248 break;
20249
20250 case 16:
20251 start = byte_get ((unsigned char *) pnote->descdata, 8);
20252 end = byte_get ((unsigned char *) pnote->descdata + 8, 8);
20253 break;
20254
20255 default:
20256 error (_(" <invalid description size: %lx>\n"), pnote->descsz);
20257 printf (_(" <invalid descsz>"));
20258 return false;
20259 }
20260
20261 name = NULL;
20262 sym = get_symbol_for_build_attribute (filedata, start, is_open_attr, & name);
20263 /* As of version 5 of the annobin plugin, filename symbols are biased by 2
20264 in order to avoid them being confused with the start address of the
20265 first function in the file... */
20266 if (sym == NULL && is_open_attr)
20267 sym = get_symbol_for_build_attribute (filedata, start + 2, is_open_attr,
20268 & name);
20269
20270 if (end == 0 && sym != NULL && sym->st_size > 0)
20271 end = start + sym->st_size;
20272
20273 if (is_open_attr)
20274 {
20275 /* FIXME: Need to properly allow for section alignment.
20276 16 is just the alignment used on x86_64. */
20277 if (global_end > 0
20278 && start > BFD_ALIGN (global_end, 16)
20279 /* Build notes are not guaranteed to be organised in order of
20280 increasing address, but we should find the all of the notes
20281 for one section in the same place. */
20282 && same_section (filedata, start, global_end))
20283 warn (_("Gap in build notes detected from %#lx to %#lx\n"),
20284 global_end + 1, start - 1);
20285
20286 printf (_(" Applies to region from %#lx"), start);
20287 global_offset = start;
20288
20289 if (end)
20290 {
20291 printf (_(" to %#lx"), end);
20292 global_end = end;
20293 }
20294 }
20295 else
20296 {
20297 printf (_(" Applies to region from %#lx"), start);
20298 func_offset = start;
20299
20300 if (end)
20301 {
20302 printf (_(" to %#lx"), end);
20303 func_end = end;
20304 }
20305 }
20306
20307 if (sym && name)
20308 printf (_(" (%s)"), name);
20309
20310 printf ("\n");
20311 return true;
20312 }
20313
20314 static bool
20315 print_gnu_build_attribute_name (Elf_Internal_Note * pnote)
20316 {
20317 static const char string_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_STRING, 0 };
20318 static const char number_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC, 0 };
20319 static const char bool_expected [3] = { GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE, GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE, 0 };
20320 char name_type;
20321 char name_attribute;
20322 const char * expected_types;
20323 const char * name = pnote->namedata;
20324 const char * text;
20325 signed int left;
20326
20327 if (name == NULL || pnote->namesz < 2)
20328 {
20329 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
20330 print_symbol (-20, _(" <corrupt name>"));
20331 return false;
20332 }
20333
20334 if (do_wide)
20335 left = 28;
20336 else
20337 left = 20;
20338
20339 /* Version 2 of the spec adds a "GA" prefix to the name field. */
20340 if (name[0] == 'G' && name[1] == 'A')
20341 {
20342 if (pnote->namesz < 4)
20343 {
20344 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
20345 print_symbol (-20, _(" <corrupt name>"));
20346 return false;
20347 }
20348
20349 printf ("GA");
20350 name += 2;
20351 left -= 2;
20352 }
20353
20354 switch ((name_type = * name))
20355 {
20356 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
20357 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
20358 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
20359 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
20360 printf ("%c", * name);
20361 left --;
20362 break;
20363 default:
20364 error (_("unrecognised attribute type in name field: %d\n"), name_type);
20365 print_symbol (-20, _("<unknown name type>"));
20366 return false;
20367 }
20368
20369 ++ name;
20370 text = NULL;
20371
20372 switch ((name_attribute = * name))
20373 {
20374 case GNU_BUILD_ATTRIBUTE_VERSION:
20375 text = _("<version>");
20376 expected_types = string_expected;
20377 ++ name;
20378 break;
20379 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
20380 text = _("<stack prot>");
20381 expected_types = "!+*";
20382 ++ name;
20383 break;
20384 case GNU_BUILD_ATTRIBUTE_RELRO:
20385 text = _("<relro>");
20386 expected_types = bool_expected;
20387 ++ name;
20388 break;
20389 case GNU_BUILD_ATTRIBUTE_STACK_SIZE:
20390 text = _("<stack size>");
20391 expected_types = number_expected;
20392 ++ name;
20393 break;
20394 case GNU_BUILD_ATTRIBUTE_TOOL:
20395 text = _("<tool>");
20396 expected_types = string_expected;
20397 ++ name;
20398 break;
20399 case GNU_BUILD_ATTRIBUTE_ABI:
20400 text = _("<ABI>");
20401 expected_types = "$*";
20402 ++ name;
20403 break;
20404 case GNU_BUILD_ATTRIBUTE_PIC:
20405 text = _("<PIC>");
20406 expected_types = number_expected;
20407 ++ name;
20408 break;
20409 case GNU_BUILD_ATTRIBUTE_SHORT_ENUM:
20410 text = _("<short enum>");
20411 expected_types = bool_expected;
20412 ++ name;
20413 break;
20414 default:
20415 if (ISPRINT (* name))
20416 {
20417 int len = strnlen (name, pnote->namesz - (name - pnote->namedata)) + 1;
20418
20419 if (len > left && ! do_wide)
20420 len = left;
20421 printf ("%.*s:", len, name);
20422 left -= len;
20423 name += len;
20424 }
20425 else
20426 {
20427 static char tmpbuf [128];
20428
20429 error (_("unrecognised byte in name field: %d\n"), * name);
20430 sprintf (tmpbuf, _("<unknown:_%d>"), * name);
20431 text = tmpbuf;
20432 name ++;
20433 }
20434 expected_types = "*$!+";
20435 break;
20436 }
20437
20438 if (text)
20439 left -= printf ("%s", text);
20440
20441 if (strchr (expected_types, name_type) == NULL)
20442 warn (_("attribute does not have an expected type (%c)\n"), name_type);
20443
20444 if ((unsigned long)(name - pnote->namedata) > pnote->namesz)
20445 {
20446 error (_("corrupt name field: namesz: %lu but parsing gets to %ld\n"),
20447 (unsigned long) pnote->namesz,
20448 (long) (name - pnote->namedata));
20449 return false;
20450 }
20451
20452 if (left < 1 && ! do_wide)
20453 return true;
20454
20455 switch (name_type)
20456 {
20457 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
20458 {
20459 unsigned int bytes;
20460 unsigned long long val = 0;
20461 unsigned int shift = 0;
20462 char * decoded = NULL;
20463
20464 bytes = pnote->namesz - (name - pnote->namedata);
20465 if (bytes > 0)
20466 /* The -1 is because the name field is always 0 terminated, and we
20467 want to be able to ensure that the shift in the while loop below
20468 will not overflow. */
20469 -- bytes;
20470
20471 if (bytes > sizeof (val))
20472 {
20473 error (_("corrupt numeric name field: too many bytes in the value: %x\n"),
20474 bytes);
20475 bytes = sizeof (val);
20476 }
20477 /* We do not bother to warn if bytes == 0 as this can
20478 happen with some early versions of the gcc plugin. */
20479
20480 while (bytes --)
20481 {
20482 unsigned long long byte = *name++ & 0xff;
20483
20484 val |= byte << shift;
20485 shift += 8;
20486 }
20487
20488 switch (name_attribute)
20489 {
20490 case GNU_BUILD_ATTRIBUTE_PIC:
20491 switch (val)
20492 {
20493 case 0: decoded = "static"; break;
20494 case 1: decoded = "pic"; break;
20495 case 2: decoded = "PIC"; break;
20496 case 3: decoded = "pie"; break;
20497 case 4: decoded = "PIE"; break;
20498 default: break;
20499 }
20500 break;
20501 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
20502 switch (val)
20503 {
20504 /* Based upon the SPCT_FLAG_xxx enum values in gcc/cfgexpand.c. */
20505 case 0: decoded = "off"; break;
20506 case 1: decoded = "on"; break;
20507 case 2: decoded = "all"; break;
20508 case 3: decoded = "strong"; break;
20509 case 4: decoded = "explicit"; break;
20510 default: break;
20511 }
20512 break;
20513 default:
20514 break;
20515 }
20516
20517 if (decoded != NULL)
20518 {
20519 print_symbol (-left, decoded);
20520 left = 0;
20521 }
20522 else if (val == 0)
20523 {
20524 printf ("0x0");
20525 left -= 3;
20526 }
20527 else
20528 {
20529 if (do_wide)
20530 left -= printf ("0x%llx", val);
20531 else
20532 left -= printf ("0x%-.*llx", left, val);
20533 }
20534 }
20535 break;
20536 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
20537 left -= print_symbol (- left, name);
20538 break;
20539 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
20540 left -= print_symbol (- left, "true");
20541 break;
20542 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
20543 left -= print_symbol (- left, "false");
20544 break;
20545 }
20546
20547 if (do_wide && left > 0)
20548 printf ("%-*s", left, " ");
20549
20550 return true;
20551 }
20552
20553 /* Note that by the ELF standard, the name field is already null byte
20554 terminated, and namesz includes the terminating null byte.
20555 I.E. the value of namesz for the name "FSF" is 4.
20556
20557 If the value of namesz is zero, there is no name present. */
20558
20559 static bool
20560 process_note (Elf_Internal_Note * pnote,
20561 Filedata * filedata)
20562 {
20563 const char * name = pnote->namesz ? pnote->namedata : "(NONE)";
20564 const char * nt;
20565
20566 if (pnote->namesz == 0)
20567 /* If there is no note name, then use the default set of
20568 note type strings. */
20569 nt = get_note_type (filedata, pnote->type);
20570
20571 else if (startswith (pnote->namedata, "GNU"))
20572 /* GNU-specific object file notes. */
20573 nt = get_gnu_elf_note_type (pnote->type);
20574
20575 else if (startswith (pnote->namedata, "FreeBSD"))
20576 /* FreeBSD-specific core file notes. */
20577 nt = get_freebsd_elfcore_note_type (filedata, pnote->type);
20578
20579 else if (startswith (pnote->namedata, "NetBSD-CORE"))
20580 /* NetBSD-specific core file notes. */
20581 nt = get_netbsd_elfcore_note_type (filedata, pnote->type);
20582
20583 else if (startswith (pnote->namedata, "NetBSD"))
20584 /* NetBSD-specific core file notes. */
20585 return process_netbsd_elf_note (pnote);
20586
20587 else if (startswith (pnote->namedata, "PaX"))
20588 /* NetBSD-specific core file notes. */
20589 return process_netbsd_elf_note (pnote);
20590
20591 else if (startswith (pnote->namedata, "SPU/"))
20592 {
20593 /* SPU-specific core file notes. */
20594 nt = pnote->namedata + 4;
20595 name = "SPU";
20596 }
20597
20598 else if (startswith (pnote->namedata, "IPF/VMS"))
20599 /* VMS/ia64-specific file notes. */
20600 nt = get_ia64_vms_note_type (pnote->type);
20601
20602 else if (startswith (pnote->namedata, "stapsdt"))
20603 nt = get_stapsdt_note_type (pnote->type);
20604
20605 else
20606 /* Don't recognize this note name; just use the default set of
20607 note type strings. */
20608 nt = get_note_type (filedata, pnote->type);
20609
20610 printf (" ");
20611
20612 if (((startswith (pnote->namedata, "GA")
20613 && strchr ("*$!+", pnote->namedata[2]) != NULL)
20614 || strchr ("*$!+", pnote->namedata[0]) != NULL)
20615 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
20616 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
20617 print_gnu_build_attribute_name (pnote);
20618 else
20619 print_symbol (-20, name);
20620
20621 if (do_wide)
20622 printf (" 0x%08lx\t%s\t", pnote->descsz, nt);
20623 else
20624 printf (" 0x%08lx\t%s\n", pnote->descsz, nt);
20625
20626 if (startswith (pnote->namedata, "IPF/VMS"))
20627 return print_ia64_vms_note (pnote);
20628 else if (startswith (pnote->namedata, "GNU"))
20629 return print_gnu_note (filedata, pnote);
20630 else if (startswith (pnote->namedata, "stapsdt"))
20631 return print_stapsdt_note (pnote);
20632 else if (startswith (pnote->namedata, "CORE"))
20633 return print_core_note (pnote);
20634 else if (((startswith (pnote->namedata, "GA")
20635 && strchr ("*$!+", pnote->namedata[2]) != NULL)
20636 || strchr ("*$!+", pnote->namedata[0]) != NULL)
20637 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
20638 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
20639 return print_gnu_build_attribute_description (pnote, filedata);
20640
20641 if (pnote->descsz)
20642 {
20643 unsigned long i;
20644
20645 printf (_(" description data: "));
20646 for (i = 0; i < pnote->descsz; i++)
20647 printf ("%02x ", pnote->descdata[i] & 0xff);
20648 if (!do_wide)
20649 printf ("\n");
20650 }
20651
20652 if (do_wide)
20653 printf ("\n");
20654
20655 return true;
20656 }
20657
20658 static bool
20659 process_notes_at (Filedata * filedata,
20660 Elf_Internal_Shdr * section,
20661 bfd_vma offset,
20662 bfd_vma length,
20663 bfd_vma align)
20664 {
20665 Elf_External_Note *pnotes;
20666 Elf_External_Note *external;
20667 char *end;
20668 bool res = true;
20669
20670 if (length <= 0)
20671 return false;
20672
20673 if (section)
20674 {
20675 pnotes = (Elf_External_Note *) get_section_contents (section, filedata);
20676 if (pnotes)
20677 {
20678 if (! apply_relocations (filedata, section, (unsigned char *) pnotes, length, NULL, NULL))
20679 {
20680 free (pnotes);
20681 return false;
20682 }
20683 }
20684 }
20685 else
20686 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
20687 _("notes"));
20688
20689 if (pnotes == NULL)
20690 return false;
20691
20692 external = pnotes;
20693
20694 if (filedata->is_separate)
20695 printf (_("In linked file '%s': "), filedata->file_name);
20696 else
20697 printf ("\n");
20698 if (section)
20699 printf (_("Displaying notes found in: %s\n"), printable_section_name (filedata, section));
20700 else
20701 printf (_("Displaying notes found at file offset 0x%08lx with length 0x%08lx:\n"),
20702 (unsigned long) offset, (unsigned long) length);
20703
20704 /* NB: Some note sections may have alignment value of 0 or 1. gABI
20705 specifies that notes should be aligned to 4 bytes in 32-bit
20706 objects and to 8 bytes in 64-bit objects. As a Linux extension,
20707 we also support 4 byte alignment in 64-bit objects. If section
20708 alignment is less than 4, we treate alignment as 4 bytes. */
20709 if (align < 4)
20710 align = 4;
20711 else if (align != 4 && align != 8)
20712 {
20713 warn (_("Corrupt note: alignment %ld, expecting 4 or 8\n"),
20714 (long) align);
20715 free (pnotes);
20716 return false;
20717 }
20718
20719 printf (_(" %-20s %-10s\tDescription\n"), _("Owner"), _("Data size"));
20720
20721 end = (char *) pnotes + length;
20722 while ((char *) external < end)
20723 {
20724 Elf_Internal_Note inote;
20725 size_t min_notesz;
20726 char * next;
20727 char * temp = NULL;
20728 size_t data_remaining = end - (char *) external;
20729
20730 if (!is_ia64_vms (filedata))
20731 {
20732 /* PR binutils/15191
20733 Make sure that there is enough data to read. */
20734 min_notesz = offsetof (Elf_External_Note, name);
20735 if (data_remaining < min_notesz)
20736 {
20737 warn (ngettext ("Corrupt note: only %ld byte remains, "
20738 "not enough for a full note\n",
20739 "Corrupt note: only %ld bytes remain, "
20740 "not enough for a full note\n",
20741 data_remaining),
20742 (long) data_remaining);
20743 break;
20744 }
20745 data_remaining -= min_notesz;
20746
20747 inote.type = BYTE_GET (external->type);
20748 inote.namesz = BYTE_GET (external->namesz);
20749 inote.namedata = external->name;
20750 inote.descsz = BYTE_GET (external->descsz);
20751 inote.descdata = ((char *) external
20752 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
20753 inote.descpos = offset + (inote.descdata - (char *) pnotes);
20754 next = ((char *) external
20755 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
20756 }
20757 else
20758 {
20759 Elf64_External_VMS_Note *vms_external;
20760
20761 /* PR binutils/15191
20762 Make sure that there is enough data to read. */
20763 min_notesz = offsetof (Elf64_External_VMS_Note, name);
20764 if (data_remaining < min_notesz)
20765 {
20766 warn (ngettext ("Corrupt note: only %ld byte remains, "
20767 "not enough for a full note\n",
20768 "Corrupt note: only %ld bytes remain, "
20769 "not enough for a full note\n",
20770 data_remaining),
20771 (long) data_remaining);
20772 break;
20773 }
20774 data_remaining -= min_notesz;
20775
20776 vms_external = (Elf64_External_VMS_Note *) external;
20777 inote.type = BYTE_GET (vms_external->type);
20778 inote.namesz = BYTE_GET (vms_external->namesz);
20779 inote.namedata = vms_external->name;
20780 inote.descsz = BYTE_GET (vms_external->descsz);
20781 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
20782 inote.descpos = offset + (inote.descdata - (char *) pnotes);
20783 next = inote.descdata + align_power (inote.descsz, 3);
20784 }
20785
20786 /* PR 17531: file: 3443835e. */
20787 /* PR 17531: file: id:000000,sig:11,src:006986,op:havoc,rep:4. */
20788 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
20789 || (size_t) (inote.descdata - inote.namedata) > data_remaining
20790 || (size_t) (next - inote.descdata) < inote.descsz
20791 || ((size_t) (next - inote.descdata)
20792 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
20793 {
20794 warn (_("note with invalid namesz and/or descsz found at offset 0x%lx\n"),
20795 (unsigned long) ((char *) external - (char *) pnotes));
20796 warn (_(" type: 0x%lx, namesize: 0x%08lx, descsize: 0x%08lx, alignment: %u\n"),
20797 inote.type, inote.namesz, inote.descsz, (int) align);
20798 break;
20799 }
20800
20801 external = (Elf_External_Note *) next;
20802
20803 /* Verify that name is null terminated. It appears that at least
20804 one version of Linux (RedHat 6.0) generates corefiles that don't
20805 comply with the ELF spec by failing to include the null byte in
20806 namesz. */
20807 if (inote.namesz > 0 && inote.namedata[inote.namesz - 1] != '\0')
20808 {
20809 if ((size_t) (inote.descdata - inote.namedata) == inote.namesz)
20810 {
20811 temp = (char *) malloc (inote.namesz + 1);
20812 if (temp == NULL)
20813 {
20814 error (_("Out of memory allocating space for inote name\n"));
20815 res = false;
20816 break;
20817 }
20818
20819 memcpy (temp, inote.namedata, inote.namesz);
20820 inote.namedata = temp;
20821 }
20822 inote.namedata[inote.namesz] = 0;
20823 }
20824
20825 if (! process_note (& inote, filedata))
20826 res = false;
20827
20828 free (temp);
20829 temp = NULL;
20830 }
20831
20832 free (pnotes);
20833
20834 return res;
20835 }
20836
20837 static bool
20838 process_corefile_note_segments (Filedata * filedata)
20839 {
20840 Elf_Internal_Phdr *segment;
20841 unsigned int i;
20842 bool res = true;
20843
20844 if (! get_program_headers (filedata))
20845 return true;
20846
20847 for (i = 0, segment = filedata->program_headers;
20848 i < filedata->file_header.e_phnum;
20849 i++, segment++)
20850 {
20851 if (segment->p_type == PT_NOTE)
20852 if (! process_notes_at (filedata, NULL,
20853 (bfd_vma) segment->p_offset,
20854 (bfd_vma) segment->p_filesz,
20855 (bfd_vma) segment->p_align))
20856 res = false;
20857 }
20858
20859 return res;
20860 }
20861
20862 static bool
20863 process_v850_notes (Filedata * filedata, bfd_vma offset, bfd_vma length)
20864 {
20865 Elf_External_Note * pnotes;
20866 Elf_External_Note * external;
20867 char * end;
20868 bool res = true;
20869
20870 if (length <= 0)
20871 return false;
20872
20873 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
20874 _("v850 notes"));
20875 if (pnotes == NULL)
20876 return false;
20877
20878 external = pnotes;
20879 end = (char*) pnotes + length;
20880
20881 printf (_("\nDisplaying contents of Renesas V850 notes section at offset 0x%lx with length 0x%lx:\n"),
20882 (unsigned long) offset, (unsigned long) length);
20883
20884 while ((char *) external + sizeof (Elf_External_Note) < end)
20885 {
20886 Elf_External_Note * next;
20887 Elf_Internal_Note inote;
20888
20889 inote.type = BYTE_GET (external->type);
20890 inote.namesz = BYTE_GET (external->namesz);
20891 inote.namedata = external->name;
20892 inote.descsz = BYTE_GET (external->descsz);
20893 inote.descdata = inote.namedata + align_power (inote.namesz, 2);
20894 inote.descpos = offset + (inote.descdata - (char *) pnotes);
20895
20896 if (inote.descdata < (char *) pnotes || inote.descdata >= end)
20897 {
20898 warn (_("Corrupt note: name size is too big: %lx\n"), inote.namesz);
20899 inote.descdata = inote.namedata;
20900 inote.namesz = 0;
20901 }
20902
20903 next = (Elf_External_Note *) (inote.descdata + align_power (inote.descsz, 2));
20904
20905 if ( ((char *) next > end)
20906 || ((char *) next < (char *) pnotes))
20907 {
20908 warn (_("corrupt descsz found in note at offset 0x%lx\n"),
20909 (unsigned long) ((char *) external - (char *) pnotes));
20910 warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
20911 inote.type, inote.namesz, inote.descsz);
20912 break;
20913 }
20914
20915 external = next;
20916
20917 /* Prevent out-of-bounds indexing. */
20918 if ( inote.namedata + inote.namesz > end
20919 || inote.namedata + inote.namesz < inote.namedata)
20920 {
20921 warn (_("corrupt namesz found in note at offset 0x%lx\n"),
20922 (unsigned long) ((char *) external - (char *) pnotes));
20923 warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
20924 inote.type, inote.namesz, inote.descsz);
20925 break;
20926 }
20927
20928 printf (" %s: ", get_v850_elf_note_type (inote.type));
20929
20930 if (! print_v850_note (& inote))
20931 {
20932 res = false;
20933 printf ("<corrupt sizes: namesz: %lx, descsz: %lx>\n",
20934 inote.namesz, inote.descsz);
20935 }
20936 }
20937
20938 free (pnotes);
20939
20940 return res;
20941 }
20942
20943 static bool
20944 process_note_sections (Filedata * filedata)
20945 {
20946 Elf_Internal_Shdr *section;
20947 unsigned long i;
20948 unsigned int n = 0;
20949 bool res = true;
20950
20951 for (i = 0, section = filedata->section_headers;
20952 i < filedata->file_header.e_shnum && section != NULL;
20953 i++, section++)
20954 {
20955 if (section->sh_type == SHT_NOTE)
20956 {
20957 if (! process_notes_at (filedata, section,
20958 (bfd_vma) section->sh_offset,
20959 (bfd_vma) section->sh_size,
20960 (bfd_vma) section->sh_addralign))
20961 res = false;
20962 n++;
20963 }
20964
20965 if (( filedata->file_header.e_machine == EM_V800
20966 || filedata->file_header.e_machine == EM_V850
20967 || filedata->file_header.e_machine == EM_CYGNUS_V850)
20968 && section->sh_type == SHT_RENESAS_INFO)
20969 {
20970 if (! process_v850_notes (filedata,
20971 (bfd_vma) section->sh_offset,
20972 (bfd_vma) section->sh_size))
20973 res = false;
20974 n++;
20975 }
20976 }
20977
20978 if (n == 0)
20979 /* Try processing NOTE segments instead. */
20980 return process_corefile_note_segments (filedata);
20981
20982 return res;
20983 }
20984
20985 static bool
20986 process_notes (Filedata * filedata)
20987 {
20988 /* If we have not been asked to display the notes then do nothing. */
20989 if (! do_notes)
20990 return true;
20991
20992 if (filedata->file_header.e_type != ET_CORE)
20993 return process_note_sections (filedata);
20994
20995 /* No program headers means no NOTE segment. */
20996 if (filedata->file_header.e_phnum > 0)
20997 return process_corefile_note_segments (filedata);
20998
20999 if (filedata->is_separate)
21000 printf (_("No notes found in linked file '%s'.\n"),
21001 filedata->file_name);
21002 else
21003 printf (_("No notes found file.\n"));
21004
21005 return true;
21006 }
21007
21008 static unsigned char *
21009 display_public_gnu_attributes (unsigned char * start,
21010 const unsigned char * const end)
21011 {
21012 printf (_(" Unknown GNU attribute: %s\n"), start);
21013
21014 start += strnlen ((char *) start, end - start);
21015 display_raw_attribute (start, end);
21016
21017 return (unsigned char *) end;
21018 }
21019
21020 static unsigned char *
21021 display_generic_attribute (unsigned char * start,
21022 unsigned int tag,
21023 const unsigned char * const end)
21024 {
21025 if (tag == 0)
21026 return (unsigned char *) end;
21027
21028 return display_tag_value (tag, start, end);
21029 }
21030
21031 static bool
21032 process_arch_specific (Filedata * filedata)
21033 {
21034 if (! do_arch)
21035 return true;
21036
21037 switch (filedata->file_header.e_machine)
21038 {
21039 case EM_ARC:
21040 case EM_ARC_COMPACT:
21041 case EM_ARC_COMPACT2:
21042 return process_attributes (filedata, "ARC", SHT_ARC_ATTRIBUTES,
21043 display_arc_attribute,
21044 display_generic_attribute);
21045 case EM_ARM:
21046 return process_attributes (filedata, "aeabi", SHT_ARM_ATTRIBUTES,
21047 display_arm_attribute,
21048 display_generic_attribute);
21049
21050 case EM_MIPS:
21051 case EM_MIPS_RS3_LE:
21052 return process_mips_specific (filedata);
21053
21054 case EM_MSP430:
21055 return process_attributes (filedata, "mspabi", SHT_MSP430_ATTRIBUTES,
21056 display_msp430_attribute,
21057 display_msp430_gnu_attribute);
21058
21059 case EM_RISCV:
21060 return process_attributes (filedata, "riscv", SHT_RISCV_ATTRIBUTES,
21061 display_riscv_attribute,
21062 display_generic_attribute);
21063
21064 case EM_NDS32:
21065 return process_nds32_specific (filedata);
21066
21067 case EM_68K:
21068 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
21069 display_m68k_gnu_attribute);
21070
21071 case EM_PPC:
21072 case EM_PPC64:
21073 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
21074 display_power_gnu_attribute);
21075
21076 case EM_S390:
21077 case EM_S390_OLD:
21078 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
21079 display_s390_gnu_attribute);
21080
21081 case EM_SPARC:
21082 case EM_SPARC32PLUS:
21083 case EM_SPARCV9:
21084 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
21085 display_sparc_gnu_attribute);
21086
21087 case EM_TI_C6000:
21088 return process_attributes (filedata, "c6xabi", SHT_C6000_ATTRIBUTES,
21089 display_tic6x_attribute,
21090 display_generic_attribute);
21091
21092 case EM_CSKY:
21093 return process_attributes (filedata, "csky", SHT_CSKY_ATTRIBUTES,
21094 display_csky_attribute, NULL);
21095
21096 default:
21097 return process_attributes (filedata, "gnu", SHT_GNU_ATTRIBUTES,
21098 display_public_gnu_attributes,
21099 display_generic_attribute);
21100 }
21101 }
21102
21103 static bool
21104 get_file_header (Filedata * filedata)
21105 {
21106 /* Read in the identity array. */
21107 if (fread (filedata->file_header.e_ident, EI_NIDENT, 1, filedata->handle) != 1)
21108 return false;
21109
21110 /* Determine how to read the rest of the header. */
21111 switch (filedata->file_header.e_ident[EI_DATA])
21112 {
21113 default:
21114 case ELFDATANONE:
21115 case ELFDATA2LSB:
21116 byte_get = byte_get_little_endian;
21117 byte_put = byte_put_little_endian;
21118 break;
21119 case ELFDATA2MSB:
21120 byte_get = byte_get_big_endian;
21121 byte_put = byte_put_big_endian;
21122 break;
21123 }
21124
21125 /* For now we only support 32 bit and 64 bit ELF files. */
21126 is_32bit_elf = (filedata->file_header.e_ident[EI_CLASS] != ELFCLASS64);
21127
21128 /* Read in the rest of the header. */
21129 if (is_32bit_elf)
21130 {
21131 Elf32_External_Ehdr ehdr32;
21132
21133 if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, filedata->handle) != 1)
21134 return false;
21135
21136 filedata->file_header.e_type = BYTE_GET (ehdr32.e_type);
21137 filedata->file_header.e_machine = BYTE_GET (ehdr32.e_machine);
21138 filedata->file_header.e_version = BYTE_GET (ehdr32.e_version);
21139 filedata->file_header.e_entry = BYTE_GET (ehdr32.e_entry);
21140 filedata->file_header.e_phoff = BYTE_GET (ehdr32.e_phoff);
21141 filedata->file_header.e_shoff = BYTE_GET (ehdr32.e_shoff);
21142 filedata->file_header.e_flags = BYTE_GET (ehdr32.e_flags);
21143 filedata->file_header.e_ehsize = BYTE_GET (ehdr32.e_ehsize);
21144 filedata->file_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
21145 filedata->file_header.e_phnum = BYTE_GET (ehdr32.e_phnum);
21146 filedata->file_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
21147 filedata->file_header.e_shnum = BYTE_GET (ehdr32.e_shnum);
21148 filedata->file_header.e_shstrndx = BYTE_GET (ehdr32.e_shstrndx);
21149 }
21150 else
21151 {
21152 Elf64_External_Ehdr ehdr64;
21153
21154 /* If we have been compiled with sizeof (bfd_vma) == 4, then
21155 we will not be able to cope with the 64bit data found in
21156 64 ELF files. Detect this now and abort before we start
21157 overwriting things. */
21158 if (sizeof (bfd_vma) < 8)
21159 {
21160 error (_("This instance of readelf has been built without support for a\n\
21161 64 bit data type and so it cannot read 64 bit ELF files.\n"));
21162 return false;
21163 }
21164
21165 if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, filedata->handle) != 1)
21166 return false;
21167
21168 filedata->file_header.e_type = BYTE_GET (ehdr64.e_type);
21169 filedata->file_header.e_machine = BYTE_GET (ehdr64.e_machine);
21170 filedata->file_header.e_version = BYTE_GET (ehdr64.e_version);
21171 filedata->file_header.e_entry = BYTE_GET (ehdr64.e_entry);
21172 filedata->file_header.e_phoff = BYTE_GET (ehdr64.e_phoff);
21173 filedata->file_header.e_shoff = BYTE_GET (ehdr64.e_shoff);
21174 filedata->file_header.e_flags = BYTE_GET (ehdr64.e_flags);
21175 filedata->file_header.e_ehsize = BYTE_GET (ehdr64.e_ehsize);
21176 filedata->file_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
21177 filedata->file_header.e_phnum = BYTE_GET (ehdr64.e_phnum);
21178 filedata->file_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
21179 filedata->file_header.e_shnum = BYTE_GET (ehdr64.e_shnum);
21180 filedata->file_header.e_shstrndx = BYTE_GET (ehdr64.e_shstrndx);
21181 }
21182
21183 return true;
21184 }
21185
21186 static void
21187 free_filedata (Filedata *filedata)
21188 {
21189 free (filedata->program_interpreter);
21190 free (filedata->program_headers);
21191 free (filedata->section_headers);
21192 free (filedata->string_table);
21193 free (filedata->dump.dump_sects);
21194 free (filedata->dynamic_strings);
21195 free (filedata->dynamic_symbols);
21196 free (filedata->dynamic_syminfo);
21197 free (filedata->dynamic_section);
21198
21199 while (filedata->symtab_shndx_list != NULL)
21200 {
21201 elf_section_list *next = filedata->symtab_shndx_list->next;
21202 free (filedata->symtab_shndx_list);
21203 filedata->symtab_shndx_list = next;
21204 }
21205
21206 free (filedata->section_headers_groups);
21207
21208 if (filedata->section_groups)
21209 {
21210 size_t i;
21211 struct group_list * g;
21212 struct group_list * next;
21213
21214 for (i = 0; i < filedata->group_count; i++)
21215 {
21216 for (g = filedata->section_groups [i].root; g != NULL; g = next)
21217 {
21218 next = g->next;
21219 free (g);
21220 }
21221 }
21222
21223 free (filedata->section_groups);
21224 }
21225 memset (&filedata->section_headers, 0,
21226 sizeof (Filedata) - offsetof (Filedata, section_headers));
21227 }
21228
21229 static void
21230 close_file (Filedata * filedata)
21231 {
21232 if (filedata)
21233 {
21234 if (filedata->handle)
21235 fclose (filedata->handle);
21236 free (filedata);
21237 }
21238 }
21239
21240 void
21241 close_debug_file (void * data)
21242 {
21243 free_filedata ((Filedata *) data);
21244 close_file ((Filedata *) data);
21245 }
21246
21247 static Filedata *
21248 open_file (const char * pathname, bool is_separate)
21249 {
21250 struct stat statbuf;
21251 Filedata * filedata = NULL;
21252
21253 if (stat (pathname, & statbuf) < 0
21254 || ! S_ISREG (statbuf.st_mode))
21255 goto fail;
21256
21257 filedata = calloc (1, sizeof * filedata);
21258 if (filedata == NULL)
21259 goto fail;
21260
21261 filedata->handle = fopen (pathname, "rb");
21262 if (filedata->handle == NULL)
21263 goto fail;
21264
21265 filedata->file_size = (bfd_size_type) statbuf.st_size;
21266 filedata->file_name = pathname;
21267 filedata->is_separate = is_separate;
21268
21269 if (! get_file_header (filedata))
21270 goto fail;
21271
21272 if (!get_section_headers (filedata, false))
21273 goto fail;
21274
21275 return filedata;
21276
21277 fail:
21278 if (filedata)
21279 {
21280 if (filedata->handle)
21281 fclose (filedata->handle);
21282 free (filedata);
21283 }
21284 return NULL;
21285 }
21286
21287 void *
21288 open_debug_file (const char * pathname)
21289 {
21290 return open_file (pathname, true);
21291 }
21292
21293 static void
21294 initialise_dump_sects (Filedata * filedata)
21295 {
21296 /* Initialise the dump_sects array from the cmdline_dump_sects array.
21297 Note we do this even if cmdline_dump_sects is empty because we
21298 must make sure that the dump_sets array is zeroed out before each
21299 object file is processed. */
21300 if (filedata->dump.num_dump_sects > cmdline.num_dump_sects)
21301 memset (filedata->dump.dump_sects, 0,
21302 filedata->dump.num_dump_sects * sizeof (*filedata->dump.dump_sects));
21303
21304 if (cmdline.num_dump_sects > 0)
21305 {
21306 if (filedata->dump.num_dump_sects == 0)
21307 /* A sneaky way of allocating the dump_sects array. */
21308 request_dump_bynumber (&filedata->dump, cmdline.num_dump_sects, 0);
21309
21310 assert (filedata->dump.num_dump_sects >= cmdline.num_dump_sects);
21311 memcpy (filedata->dump.dump_sects, cmdline.dump_sects,
21312 cmdline.num_dump_sects * sizeof (*filedata->dump.dump_sects));
21313 }
21314 }
21315
21316 /* Process one ELF object file according to the command line options.
21317 This file may actually be stored in an archive. The file is
21318 positioned at the start of the ELF object. Returns TRUE if no
21319 problems were encountered, FALSE otherwise. */
21320
21321 static bool
21322 process_object (Filedata * filedata)
21323 {
21324 bool have_separate_files;
21325 unsigned int i;
21326 bool res;
21327
21328 if (! get_file_header (filedata))
21329 {
21330 error (_("%s: Failed to read file header\n"), filedata->file_name);
21331 return false;
21332 }
21333
21334 /* Initialise per file variables. */
21335 for (i = ARRAY_SIZE (filedata->version_info); i--;)
21336 filedata->version_info[i] = 0;
21337
21338 for (i = ARRAY_SIZE (filedata->dynamic_info); i--;)
21339 filedata->dynamic_info[i] = 0;
21340 filedata->dynamic_info_DT_GNU_HASH = 0;
21341 filedata->dynamic_info_DT_MIPS_XHASH = 0;
21342
21343 /* Process the file. */
21344 if (show_name)
21345 printf (_("\nFile: %s\n"), filedata->file_name);
21346
21347 initialise_dump_sects (filedata);
21348
21349 /* There may be some extensions in the first section header. Don't
21350 bomb if we can't read it. */
21351 get_section_headers (filedata, true);
21352
21353 if (! process_file_header (filedata))
21354 {
21355 res = false;
21356 goto out;
21357 }
21358
21359 /* Throw away the single section header read above, so that we
21360 re-read the entire set. */
21361 free (filedata->section_headers);
21362 filedata->section_headers = NULL;
21363
21364 if (! process_section_headers (filedata))
21365 {
21366 /* Without loaded section headers we cannot process lots of things. */
21367 do_unwind = do_version = do_dump = do_arch = false;
21368
21369 if (! do_using_dynamic)
21370 do_syms = do_dyn_syms = do_reloc = false;
21371 }
21372
21373 if (! process_section_groups (filedata))
21374 /* Without loaded section groups we cannot process unwind. */
21375 do_unwind = false;
21376
21377 res = process_program_headers (filedata);
21378 if (res)
21379 res = process_dynamic_section (filedata);
21380
21381 if (! process_relocs (filedata))
21382 res = false;
21383
21384 if (! process_unwind (filedata))
21385 res = false;
21386
21387 if (! process_symbol_table (filedata))
21388 res = false;
21389
21390 if (! process_lto_symbol_tables (filedata))
21391 res = false;
21392
21393 if (! process_syminfo (filedata))
21394 res = false;
21395
21396 if (! process_version_sections (filedata))
21397 res = false;
21398
21399 if (filedata->file_header.e_shstrndx != SHN_UNDEF)
21400 have_separate_files = load_separate_debug_files (filedata, filedata->file_name);
21401 else
21402 have_separate_files = false;
21403
21404 if (! process_section_contents (filedata))
21405 res = false;
21406
21407 if (have_separate_files)
21408 {
21409 separate_info * d;
21410
21411 for (d = first_separate_info; d != NULL; d = d->next)
21412 {
21413 initialise_dump_sects (d->handle);
21414
21415 if (process_links && ! process_file_header (d->handle))
21416 res = false;
21417 else if (! process_section_headers (d->handle))
21418 res = false;
21419 else if (! process_section_contents (d->handle))
21420 res = false;
21421 else if (process_links)
21422 {
21423 if (! process_section_groups (d->handle))
21424 res = false;
21425 if (! process_program_headers (d->handle))
21426 res = false;
21427 if (! process_dynamic_section (d->handle))
21428 res = false;
21429 if (! process_relocs (d->handle))
21430 res = false;
21431 if (! process_unwind (d->handle))
21432 res = false;
21433 if (! process_symbol_table (d->handle))
21434 res = false;
21435 if (! process_lto_symbol_tables (d->handle))
21436 res = false;
21437 if (! process_syminfo (d->handle))
21438 res = false;
21439 if (! process_version_sections (d->handle))
21440 res = false;
21441 if (! process_notes (d->handle))
21442 res = false;
21443 }
21444 }
21445
21446 /* The file handles are closed by the call to free_debug_memory() below. */
21447 }
21448
21449 if (! process_notes (filedata))
21450 res = false;
21451
21452 if (! process_gnu_liblist (filedata))
21453 res = false;
21454
21455 if (! process_arch_specific (filedata))
21456 res = false;
21457
21458 out:
21459 free_filedata (filedata);
21460
21461 free_debug_memory ();
21462
21463 return res;
21464 }
21465
21466 /* Process an ELF archive.
21467 On entry the file is positioned just after the ARMAG string.
21468 Returns TRUE upon success, FALSE otherwise. */
21469
21470 static bool
21471 process_archive (Filedata * filedata, bool is_thin_archive)
21472 {
21473 struct archive_info arch;
21474 struct archive_info nested_arch;
21475 size_t got;
21476 bool ret = true;
21477
21478 show_name = true;
21479
21480 /* The ARCH structure is used to hold information about this archive. */
21481 arch.file_name = NULL;
21482 arch.file = NULL;
21483 arch.index_array = NULL;
21484 arch.sym_table = NULL;
21485 arch.longnames = NULL;
21486
21487 /* The NESTED_ARCH structure is used as a single-item cache of information
21488 about a nested archive (when members of a thin archive reside within
21489 another regular archive file). */
21490 nested_arch.file_name = NULL;
21491 nested_arch.file = NULL;
21492 nested_arch.index_array = NULL;
21493 nested_arch.sym_table = NULL;
21494 nested_arch.longnames = NULL;
21495
21496 if (setup_archive (&arch, filedata->file_name, filedata->handle,
21497 filedata->file_size, is_thin_archive,
21498 do_archive_index) != 0)
21499 {
21500 ret = false;
21501 goto out;
21502 }
21503
21504 if (do_archive_index)
21505 {
21506 if (arch.sym_table == NULL)
21507 error (_("%s: unable to dump the index as none was found\n"),
21508 filedata->file_name);
21509 else
21510 {
21511 unsigned long i, l;
21512 unsigned long current_pos;
21513
21514 printf (_("Index of archive %s: (%lu entries, 0x%lx bytes "
21515 "in the symbol table)\n"),
21516 filedata->file_name, (unsigned long) arch.index_num,
21517 arch.sym_size);
21518
21519 current_pos = ftell (filedata->handle);
21520
21521 for (i = l = 0; i < arch.index_num; i++)
21522 {
21523 if (i == 0
21524 || (i > 0 && arch.index_array[i] != arch.index_array[i - 1]))
21525 {
21526 char * member_name
21527 = get_archive_member_name_at (&arch, arch.index_array[i],
21528 &nested_arch);
21529
21530 if (member_name != NULL)
21531 {
21532 char * qualified_name
21533 = make_qualified_name (&arch, &nested_arch,
21534 member_name);
21535
21536 if (qualified_name != NULL)
21537 {
21538 printf (_("Contents of binary %s at offset "),
21539 qualified_name);
21540 (void) print_vma (arch.index_array[i], PREFIX_HEX);
21541 putchar ('\n');
21542 free (qualified_name);
21543 }
21544 free (member_name);
21545 }
21546 }
21547
21548 if (l >= arch.sym_size)
21549 {
21550 error (_("%s: end of the symbol table reached "
21551 "before the end of the index\n"),
21552 filedata->file_name);
21553 ret = false;
21554 break;
21555 }
21556 /* PR 17531: file: 0b6630b2. */
21557 printf ("\t%.*s\n",
21558 (int) (arch.sym_size - l), arch.sym_table + l);
21559 l += strnlen (arch.sym_table + l, arch.sym_size - l) + 1;
21560 }
21561
21562 if (arch.uses_64bit_indices)
21563 l = (l + 7) & ~ 7;
21564 else
21565 l += l & 1;
21566
21567 if (l < arch.sym_size)
21568 {
21569 error (ngettext ("%s: %ld byte remains in the symbol table, "
21570 "but without corresponding entries in "
21571 "the index table\n",
21572 "%s: %ld bytes remain in the symbol table, "
21573 "but without corresponding entries in "
21574 "the index table\n",
21575 arch.sym_size - l),
21576 filedata->file_name, arch.sym_size - l);
21577 ret = false;
21578 }
21579
21580 if (fseek (filedata->handle, current_pos, SEEK_SET) != 0)
21581 {
21582 error (_("%s: failed to seek back to start of object files "
21583 "in the archive\n"),
21584 filedata->file_name);
21585 ret = false;
21586 goto out;
21587 }
21588 }
21589
21590 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
21591 && !do_segments && !do_header && !do_dump && !do_version
21592 && !do_histogram && !do_debugging && !do_arch && !do_notes
21593 && !do_section_groups && !do_dyn_syms)
21594 {
21595 ret = true; /* Archive index only. */
21596 goto out;
21597 }
21598 }
21599
21600 while (1)
21601 {
21602 char * name;
21603 size_t namelen;
21604 char * qualified_name;
21605
21606 /* Read the next archive header. */
21607 if (fseek (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0)
21608 {
21609 error (_("%s: failed to seek to next archive header\n"),
21610 arch.file_name);
21611 ret = false;
21612 break;
21613 }
21614 got = fread (&arch.arhdr, 1, sizeof arch.arhdr, filedata->handle);
21615 if (got != sizeof arch.arhdr)
21616 {
21617 if (got == 0)
21618 break;
21619 /* PR 24049 - we cannot use filedata->file_name as this will
21620 have already been freed. */
21621 error (_("%s: failed to read archive header\n"), arch.file_name);
21622
21623 ret = false;
21624 break;
21625 }
21626 if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
21627 {
21628 error (_("%s: did not find a valid archive header\n"),
21629 arch.file_name);
21630 ret = false;
21631 break;
21632 }
21633
21634 arch.next_arhdr_offset += sizeof arch.arhdr;
21635
21636 filedata->archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
21637 if (filedata->archive_file_size & 01)
21638 ++filedata->archive_file_size;
21639
21640 name = get_archive_member_name (&arch, &nested_arch);
21641 if (name == NULL)
21642 {
21643 error (_("%s: bad archive file name\n"), arch.file_name);
21644 ret = false;
21645 break;
21646 }
21647 namelen = strlen (name);
21648
21649 qualified_name = make_qualified_name (&arch, &nested_arch, name);
21650 if (qualified_name == NULL)
21651 {
21652 error (_("%s: bad archive file name\n"), arch.file_name);
21653 free (name);
21654 ret = false;
21655 break;
21656 }
21657
21658 if (is_thin_archive && arch.nested_member_origin == 0)
21659 {
21660 /* This is a proxy for an external member of a thin archive. */
21661 Filedata * member_filedata;
21662 char * member_file_name = adjust_relative_path
21663 (filedata->file_name, name, namelen);
21664
21665 free (name);
21666 if (member_file_name == NULL)
21667 {
21668 free (qualified_name);
21669 ret = false;
21670 break;
21671 }
21672
21673 member_filedata = open_file (member_file_name, false);
21674 if (member_filedata == NULL)
21675 {
21676 error (_("Input file '%s' is not readable.\n"), member_file_name);
21677 free (member_file_name);
21678 free (qualified_name);
21679 ret = false;
21680 break;
21681 }
21682
21683 filedata->archive_file_offset = arch.nested_member_origin;
21684 member_filedata->file_name = qualified_name;
21685
21686 if (! process_object (member_filedata))
21687 ret = false;
21688
21689 close_file (member_filedata);
21690 free (member_file_name);
21691 }
21692 else if (is_thin_archive)
21693 {
21694 Filedata thin_filedata;
21695
21696 memset (&thin_filedata, 0, sizeof (thin_filedata));
21697
21698 /* PR 15140: Allow for corrupt thin archives. */
21699 if (nested_arch.file == NULL)
21700 {
21701 error (_("%s: contains corrupt thin archive: %s\n"),
21702 qualified_name, name);
21703 free (qualified_name);
21704 free (name);
21705 ret = false;
21706 break;
21707 }
21708 free (name);
21709
21710 /* This is a proxy for a member of a nested archive. */
21711 filedata->archive_file_offset
21712 = arch.nested_member_origin + sizeof arch.arhdr;
21713
21714 /* The nested archive file will have been opened and setup by
21715 get_archive_member_name. */
21716 if (fseek (nested_arch.file, filedata->archive_file_offset,
21717 SEEK_SET) != 0)
21718 {
21719 error (_("%s: failed to seek to archive member.\n"),
21720 nested_arch.file_name);
21721 free (qualified_name);
21722 ret = false;
21723 break;
21724 }
21725
21726 thin_filedata.handle = nested_arch.file;
21727 thin_filedata.file_name = qualified_name;
21728
21729 if (! process_object (& thin_filedata))
21730 ret = false;
21731 }
21732 else
21733 {
21734 free (name);
21735 filedata->archive_file_offset = arch.next_arhdr_offset;
21736 filedata->file_name = qualified_name;
21737 if (! process_object (filedata))
21738 ret = false;
21739 arch.next_arhdr_offset += filedata->archive_file_size;
21740 /* Stop looping with "negative" archive_file_size. */
21741 if (arch.next_arhdr_offset < filedata->archive_file_size)
21742 arch.next_arhdr_offset = -1ul;
21743 }
21744
21745 free (qualified_name);
21746 }
21747
21748 out:
21749 if (nested_arch.file != NULL)
21750 fclose (nested_arch.file);
21751 release_archive (&nested_arch);
21752 release_archive (&arch);
21753
21754 return ret;
21755 }
21756
21757 static bool
21758 process_file (char * file_name)
21759 {
21760 Filedata * filedata = NULL;
21761 struct stat statbuf;
21762 char armag[SARMAG];
21763 bool ret = true;
21764
21765 if (stat (file_name, &statbuf) < 0)
21766 {
21767 if (errno == ENOENT)
21768 error (_("'%s': No such file\n"), file_name);
21769 else
21770 error (_("Could not locate '%s'. System error message: %s\n"),
21771 file_name, strerror (errno));
21772 return false;
21773 }
21774
21775 if (! S_ISREG (statbuf.st_mode))
21776 {
21777 error (_("'%s' is not an ordinary file\n"), file_name);
21778 return false;
21779 }
21780
21781 filedata = calloc (1, sizeof * filedata);
21782 if (filedata == NULL)
21783 {
21784 error (_("Out of memory allocating file data structure\n"));
21785 return false;
21786 }
21787
21788 filedata->file_name = file_name;
21789 filedata->handle = fopen (file_name, "rb");
21790 if (filedata->handle == NULL)
21791 {
21792 error (_("Input file '%s' is not readable.\n"), file_name);
21793 free (filedata);
21794 return false;
21795 }
21796
21797 if (fread (armag, SARMAG, 1, filedata->handle) != 1)
21798 {
21799 error (_("%s: Failed to read file's magic number\n"), file_name);
21800 fclose (filedata->handle);
21801 free (filedata);
21802 return false;
21803 }
21804
21805 filedata->file_size = (bfd_size_type) statbuf.st_size;
21806 filedata->is_separate = false;
21807
21808 if (memcmp (armag, ARMAG, SARMAG) == 0)
21809 {
21810 if (! process_archive (filedata, false))
21811 ret = false;
21812 }
21813 else if (memcmp (armag, ARMAGT, SARMAG) == 0)
21814 {
21815 if ( ! process_archive (filedata, true))
21816 ret = false;
21817 }
21818 else
21819 {
21820 if (do_archive_index && !check_all)
21821 error (_("File %s is not an archive so its index cannot be displayed.\n"),
21822 file_name);
21823
21824 rewind (filedata->handle);
21825 filedata->archive_file_size = filedata->archive_file_offset = 0;
21826
21827 if (! process_object (filedata))
21828 ret = false;
21829 }
21830
21831 fclose (filedata->handle);
21832 free (filedata->section_headers);
21833 free (filedata->program_headers);
21834 free (filedata->string_table);
21835 free (filedata->dump.dump_sects);
21836 free (filedata);
21837
21838 free (ba_cache.strtab);
21839 ba_cache.strtab = NULL;
21840 free (ba_cache.symtab);
21841 ba_cache.symtab = NULL;
21842 ba_cache.filedata = NULL;
21843
21844 return ret;
21845 }
21846
21847 #ifdef SUPPORT_DISASSEMBLY
21848 /* Needed by the i386 disassembler. For extra credit, someone could
21849 fix this so that we insert symbolic addresses here, esp for GOT/PLT
21850 symbols. */
21851
21852 void
21853 print_address (unsigned int addr, FILE * outfile)
21854 {
21855 fprintf (outfile,"0x%8.8x", addr);
21856 }
21857
21858 /* Needed by the i386 disassembler. */
21859
21860 void
21861 db_task_printsym (unsigned int addr)
21862 {
21863 print_address (addr, stderr);
21864 }
21865 #endif
21866
21867 int
21868 main (int argc, char ** argv)
21869 {
21870 int err;
21871
21872 #ifdef HAVE_LC_MESSAGES
21873 setlocale (LC_MESSAGES, "");
21874 #endif
21875 setlocale (LC_CTYPE, "");
21876 bindtextdomain (PACKAGE, LOCALEDIR);
21877 textdomain (PACKAGE);
21878
21879 expandargv (&argc, &argv);
21880
21881 parse_args (& cmdline, argc, argv);
21882
21883 if (optind < (argc - 1))
21884 /* When displaying information for more than one file,
21885 prefix the information with the file name. */
21886 show_name = true;
21887 else if (optind >= argc)
21888 {
21889 /* Ensure that the warning is always displayed. */
21890 do_checks = true;
21891
21892 warn (_("Nothing to do.\n"));
21893 usage (stderr);
21894 }
21895
21896 err = false;
21897 while (optind < argc)
21898 if (! process_file (argv[optind++]))
21899 err = true;
21900
21901 free (cmdline.dump_sects);
21902
21903 free (dump_ctf_symtab_name);
21904 free (dump_ctf_strtab_name);
21905 free (dump_ctf_parent_name);
21906
21907 return err ? EXIT_FAILURE : EXIT_SUCCESS;
21908 }
This page took 0.505518 seconds and 5 git commands to generate.