]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - binutils/readelf.c
elf: Add GNU_PROPERTY_UINT32_AND_XXX/GNU_PROPERTY_UINT32_OR_XXX
[thirdparty/binutils-gdb.git] / binutils / readelf.c
1 /* readelf.c -- display contents of an ELF format file
2 Copyright (C) 1998-2021 Free Software Foundation, Inc.
3
4 Originally developed by Eric Youngdale <eric@andante.jic.com>
5 Modifications by Nick Clifton <nickc@redhat.com>
6
7 This file is part of GNU Binutils.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
22 02110-1301, USA. */
23 \f
24 /* The difference between readelf and objdump:
25
26 Both programs are capable of displaying the contents of ELF format files,
27 so why does the binutils project have two file dumpers ?
28
29 The reason is that objdump sees an ELF file through a BFD filter of the
30 world; if BFD has a bug where, say, it disagrees about a machine constant
31 in e_flags, then the odds are good that it will remain internally
32 consistent. The linker sees it the BFD way, objdump sees it the BFD way,
33 GAS sees it the BFD way. There was need for a tool to go find out what
34 the file actually says.
35
36 This is why the readelf program does not link against the BFD library - it
37 exists as an independent program to help verify the correct working of BFD.
38
39 There is also the case that readelf can provide more information about an
40 ELF file than is provided by objdump. In particular it can display DWARF
41 debugging information which (at the moment) objdump cannot. */
42 \f
43 #include "sysdep.h"
44 #include <assert.h>
45 #include <time.h>
46 #include <zlib.h>
47 #include <wchar.h>
48
49 #if __GNUC__ >= 2
50 /* Define BFD64 here, even if our default architecture is 32 bit ELF
51 as this will allow us to read in and parse 64bit and 32bit ELF files.
52 Only do this if we believe that the compiler can support a 64 bit
53 data type. For now we only rely on GCC being able to do this. */
54 #define BFD64
55 #endif
56
57 #include "bfd.h"
58 #include "bucomm.h"
59 #include "elfcomm.h"
60 #include "dwarf.h"
61 #include "ctf-api.h"
62 #include "demangle.h"
63
64 #include "elf/common.h"
65 #include "elf/external.h"
66 #include "elf/internal.h"
67
68
69 /* Included here, before RELOC_MACROS_GEN_FUNC is defined, so that
70 we can obtain the H8 reloc numbers. We need these for the
71 get_reloc_size() function. We include h8.h again after defining
72 RELOC_MACROS_GEN_FUNC so that we get the naming function as well. */
73
74 #include "elf/h8.h"
75 #undef _ELF_H8_H
76
77 /* Undo the effects of #including reloc-macros.h. */
78
79 #undef START_RELOC_NUMBERS
80 #undef RELOC_NUMBER
81 #undef FAKE_RELOC
82 #undef EMPTY_RELOC
83 #undef END_RELOC_NUMBERS
84 #undef _RELOC_MACROS_H
85
86 /* The following headers use the elf/reloc-macros.h file to
87 automatically generate relocation recognition functions
88 such as elf_mips_reloc_type() */
89
90 #define RELOC_MACROS_GEN_FUNC
91
92 #include "elf/aarch64.h"
93 #include "elf/alpha.h"
94 #include "elf/arc.h"
95 #include "elf/arm.h"
96 #include "elf/avr.h"
97 #include "elf/bfin.h"
98 #include "elf/cr16.h"
99 #include "elf/cris.h"
100 #include "elf/crx.h"
101 #include "elf/csky.h"
102 #include "elf/d10v.h"
103 #include "elf/d30v.h"
104 #include "elf/dlx.h"
105 #include "elf/bpf.h"
106 #include "elf/epiphany.h"
107 #include "elf/fr30.h"
108 #include "elf/frv.h"
109 #include "elf/ft32.h"
110 #include "elf/h8.h"
111 #include "elf/hppa.h"
112 #include "elf/i386.h"
113 #include "elf/i370.h"
114 #include "elf/i860.h"
115 #include "elf/i960.h"
116 #include "elf/ia64.h"
117 #include "elf/ip2k.h"
118 #include "elf/lm32.h"
119 #include "elf/iq2000.h"
120 #include "elf/m32c.h"
121 #include "elf/m32r.h"
122 #include "elf/m68k.h"
123 #include "elf/m68hc11.h"
124 #include "elf/s12z.h"
125 #include "elf/mcore.h"
126 #include "elf/mep.h"
127 #include "elf/metag.h"
128 #include "elf/microblaze.h"
129 #include "elf/mips.h"
130 #include "elf/mmix.h"
131 #include "elf/mn10200.h"
132 #include "elf/mn10300.h"
133 #include "elf/moxie.h"
134 #include "elf/mt.h"
135 #include "elf/msp430.h"
136 #include "elf/nds32.h"
137 #include "elf/nfp.h"
138 #include "elf/nios2.h"
139 #include "elf/or1k.h"
140 #include "elf/pj.h"
141 #include "elf/ppc.h"
142 #include "elf/ppc64.h"
143 #include "elf/pru.h"
144 #include "elf/riscv.h"
145 #include "elf/rl78.h"
146 #include "elf/rx.h"
147 #include "elf/s390.h"
148 #include "elf/score.h"
149 #include "elf/sh.h"
150 #include "elf/sparc.h"
151 #include "elf/spu.h"
152 #include "elf/tic6x.h"
153 #include "elf/tilegx.h"
154 #include "elf/tilepro.h"
155 #include "elf/v850.h"
156 #include "elf/vax.h"
157 #include "elf/visium.h"
158 #include "elf/wasm32.h"
159 #include "elf/x86-64.h"
160 #include "elf/xc16x.h"
161 #include "elf/xgate.h"
162 #include "elf/xstormy16.h"
163 #include "elf/xtensa.h"
164 #include "elf/z80.h"
165
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 bool get_program_headers (Filedata *);
2343 static bool get_dynamic_section (Filedata *);
2344
2345 static void
2346 locate_dynamic_section (Filedata *filedata)
2347 {
2348 unsigned long dynamic_addr = 0;
2349 bfd_size_type dynamic_size = 0;
2350
2351 if (filedata->file_header.e_phnum != 0
2352 && get_program_headers (filedata))
2353 {
2354 Elf_Internal_Phdr *segment;
2355 unsigned int i;
2356
2357 for (i = 0, segment = filedata->program_headers;
2358 i < filedata->file_header.e_phnum;
2359 i++, segment++)
2360 {
2361 if (segment->p_type == PT_DYNAMIC)
2362 {
2363 dynamic_addr = segment->p_offset;
2364 dynamic_size = segment->p_filesz;
2365
2366 if (filedata->section_headers != NULL)
2367 {
2368 Elf_Internal_Shdr *sec;
2369
2370 sec = find_section (filedata, ".dynamic");
2371 if (sec != NULL)
2372 {
2373 if (sec->sh_size == 0
2374 || sec->sh_type == SHT_NOBITS)
2375 {
2376 dynamic_addr = 0;
2377 dynamic_size = 0;
2378 }
2379 else
2380 {
2381 dynamic_addr = sec->sh_offset;
2382 dynamic_size = sec->sh_size;
2383 }
2384 }
2385 }
2386
2387 if (dynamic_addr > filedata->file_size
2388 || (dynamic_size > filedata->file_size - dynamic_addr))
2389 {
2390 dynamic_addr = 0;
2391 dynamic_size = 0;
2392 }
2393 break;
2394 }
2395 }
2396 }
2397 filedata->dynamic_addr = dynamic_addr;
2398 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
2399 }
2400
2401 static bool
2402 is_pie (Filedata *filedata)
2403 {
2404 Elf_Internal_Dyn *entry;
2405
2406 if (filedata->dynamic_size == 0)
2407 locate_dynamic_section (filedata);
2408 if (filedata->dynamic_size <= 1)
2409 return false;
2410
2411 if (!get_dynamic_section (filedata))
2412 return false;
2413
2414 for (entry = filedata->dynamic_section;
2415 entry < filedata->dynamic_section + filedata->dynamic_nent;
2416 entry++)
2417 {
2418 if (entry->d_tag == DT_FLAGS_1)
2419 {
2420 if ((entry->d_un.d_val & DF_1_PIE) != 0)
2421 return true;
2422 break;
2423 }
2424 }
2425 return false;
2426 }
2427
2428 static char *
2429 get_file_type (Filedata *filedata)
2430 {
2431 unsigned e_type = filedata->file_header.e_type;
2432 static char buff[64];
2433
2434 switch (e_type)
2435 {
2436 case ET_NONE: return _("NONE (None)");
2437 case ET_REL: return _("REL (Relocatable file)");
2438 case ET_EXEC: return _("EXEC (Executable file)");
2439 case ET_DYN:
2440 if (is_pie (filedata))
2441 return _("DYN (Position-Independent Executable file)");
2442 else
2443 return _("DYN (Shared object file)");
2444 case ET_CORE: return _("CORE (Core file)");
2445
2446 default:
2447 if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
2448 snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
2449 else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
2450 snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
2451 else
2452 snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
2453 return buff;
2454 }
2455 }
2456
2457 static char *
2458 get_machine_name (unsigned e_machine)
2459 {
2460 static char buff[64]; /* XXX */
2461
2462 switch (e_machine)
2463 {
2464 /* Please keep this switch table sorted by increasing EM_ value. */
2465 /* 0 */
2466 case EM_NONE: return _("None");
2467 case EM_M32: return "WE32100";
2468 case EM_SPARC: return "Sparc";
2469 case EM_386: return "Intel 80386";
2470 case EM_68K: return "MC68000";
2471 case EM_88K: return "MC88000";
2472 case EM_IAMCU: return "Intel MCU";
2473 case EM_860: return "Intel 80860";
2474 case EM_MIPS: return "MIPS R3000";
2475 case EM_S370: return "IBM System/370";
2476 /* 10 */
2477 case EM_MIPS_RS3_LE: return "MIPS R4000 big-endian";
2478 case EM_OLD_SPARCV9: return "Sparc v9 (old)";
2479 case EM_PARISC: return "HPPA";
2480 case EM_VPP550: return "Fujitsu VPP500";
2481 case EM_SPARC32PLUS: return "Sparc v8+" ;
2482 case EM_960: return "Intel 80960";
2483 case EM_PPC: return "PowerPC";
2484 /* 20 */
2485 case EM_PPC64: return "PowerPC64";
2486 case EM_S390_OLD:
2487 case EM_S390: return "IBM S/390";
2488 case EM_SPU: return "SPU";
2489 /* 30 */
2490 case EM_V800: return "Renesas V850 (using RH850 ABI)";
2491 case EM_FR20: return "Fujitsu FR20";
2492 case EM_RH32: return "TRW RH32";
2493 case EM_MCORE: return "MCORE";
2494 /* 40 */
2495 case EM_ARM: return "ARM";
2496 case EM_OLD_ALPHA: return "Digital Alpha (old)";
2497 case EM_SH: return "Renesas / SuperH SH";
2498 case EM_SPARCV9: return "Sparc v9";
2499 case EM_TRICORE: return "Siemens Tricore";
2500 case EM_ARC: return "ARC";
2501 case EM_H8_300: return "Renesas H8/300";
2502 case EM_H8_300H: return "Renesas H8/300H";
2503 case EM_H8S: return "Renesas H8S";
2504 case EM_H8_500: return "Renesas H8/500";
2505 /* 50 */
2506 case EM_IA_64: return "Intel IA-64";
2507 case EM_MIPS_X: return "Stanford MIPS-X";
2508 case EM_COLDFIRE: return "Motorola Coldfire";
2509 case EM_68HC12: return "Motorola MC68HC12 Microcontroller";
2510 case EM_MMA: return "Fujitsu Multimedia Accelerator";
2511 case EM_PCP: return "Siemens PCP";
2512 case EM_NCPU: return "Sony nCPU embedded RISC processor";
2513 case EM_NDR1: return "Denso NDR1 microprocesspr";
2514 case EM_STARCORE: return "Motorola Star*Core processor";
2515 case EM_ME16: return "Toyota ME16 processor";
2516 /* 60 */
2517 case EM_ST100: return "STMicroelectronics ST100 processor";
2518 case EM_TINYJ: return "Advanced Logic Corp. TinyJ embedded processor";
2519 case EM_X86_64: return "Advanced Micro Devices X86-64";
2520 case EM_PDSP: return "Sony DSP processor";
2521 case EM_PDP10: return "Digital Equipment Corp. PDP-10";
2522 case EM_PDP11: return "Digital Equipment Corp. PDP-11";
2523 case EM_FX66: return "Siemens FX66 microcontroller";
2524 case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 bit microcontroller";
2525 case EM_ST7: return "STMicroelectronics ST7 8-bit microcontroller";
2526 case EM_68HC16: return "Motorola MC68HC16 Microcontroller";
2527 /* 70 */
2528 case EM_68HC11: return "Motorola MC68HC11 Microcontroller";
2529 case EM_68HC08: return "Motorola MC68HC08 Microcontroller";
2530 case EM_68HC05: return "Motorola MC68HC05 Microcontroller";
2531 case EM_SVX: return "Silicon Graphics SVx";
2532 case EM_ST19: return "STMicroelectronics ST19 8-bit microcontroller";
2533 case EM_VAX: return "Digital VAX";
2534 case EM_CRIS: return "Axis Communications 32-bit embedded processor";
2535 case EM_JAVELIN: return "Infineon Technologies 32-bit embedded cpu";
2536 case EM_FIREPATH: return "Element 14 64-bit DSP processor";
2537 case EM_ZSP: return "LSI Logic's 16-bit DSP processor";
2538 /* 80 */
2539 case EM_MMIX: return "Donald Knuth's educational 64-bit processor";
2540 case EM_HUANY: return "Harvard Universitys's machine-independent object format";
2541 case EM_PRISM: return "Vitesse Prism";
2542 case EM_AVR_OLD:
2543 case EM_AVR: return "Atmel AVR 8-bit microcontroller";
2544 case EM_CYGNUS_FR30:
2545 case EM_FR30: return "Fujitsu FR30";
2546 case EM_CYGNUS_D10V:
2547 case EM_D10V: return "d10v";
2548 case EM_CYGNUS_D30V:
2549 case EM_D30V: return "d30v";
2550 case EM_CYGNUS_V850:
2551 case EM_V850: return "Renesas V850";
2552 case EM_CYGNUS_M32R:
2553 case EM_M32R: return "Renesas M32R (formerly Mitsubishi M32r)";
2554 case EM_CYGNUS_MN10300:
2555 case EM_MN10300: return "mn10300";
2556 /* 90 */
2557 case EM_CYGNUS_MN10200:
2558 case EM_MN10200: return "mn10200";
2559 case EM_PJ: return "picoJava";
2560 case EM_OR1K: return "OpenRISC 1000";
2561 case EM_ARC_COMPACT: return "ARCompact";
2562 case EM_XTENSA_OLD:
2563 case EM_XTENSA: return "Tensilica Xtensa Processor";
2564 case EM_VIDEOCORE: return "Alphamosaic VideoCore processor";
2565 case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor";
2566 case EM_NS32K: return "National Semiconductor 32000 series";
2567 case EM_TPC: return "Tenor Network TPC processor";
2568 case EM_SNP1K: return "Trebia SNP 1000 processor";
2569 /* 100 */
2570 case EM_ST200: return "STMicroelectronics ST200 microcontroller";
2571 case EM_IP2K_OLD:
2572 case EM_IP2K: return "Ubicom IP2xxx 8-bit microcontrollers";
2573 case EM_MAX: return "MAX Processor";
2574 case EM_CR: return "National Semiconductor CompactRISC";
2575 case EM_F2MC16: return "Fujitsu F2MC16";
2576 case EM_MSP430: return "Texas Instruments msp430 microcontroller";
2577 case EM_BLACKFIN: return "Analog Devices Blackfin";
2578 case EM_SE_C33: return "S1C33 Family of Seiko Epson processors";
2579 case EM_SEP: return "Sharp embedded microprocessor";
2580 case EM_ARCA: return "Arca RISC microprocessor";
2581 /* 110 */
2582 case EM_UNICORE: return "Unicore";
2583 case EM_EXCESS: return "eXcess 16/32/64-bit configurable embedded CPU";
2584 case EM_DXP: return "Icera Semiconductor Inc. Deep Execution Processor";
2585 case EM_ALTERA_NIOS2: return "Altera Nios II";
2586 case EM_CRX: return "National Semiconductor CRX microprocessor";
2587 case EM_XGATE: return "Motorola XGATE embedded processor";
2588 case EM_C166:
2589 case EM_XC16X: return "Infineon Technologies xc16x";
2590 case EM_M16C: return "Renesas M16C series microprocessors";
2591 case EM_DSPIC30F: return "Microchip Technology dsPIC30F Digital Signal Controller";
2592 case EM_CE: return "Freescale Communication Engine RISC core";
2593 /* 120 */
2594 case EM_M32C: return "Renesas M32c";
2595 /* 130 */
2596 case EM_TSK3000: return "Altium TSK3000 core";
2597 case EM_RS08: return "Freescale RS08 embedded processor";
2598 case EM_ECOG2: return "Cyan Technology eCOG2 microprocessor";
2599 case EM_SCORE: return "SUNPLUS S+Core";
2600 case EM_DSP24: return "New Japan Radio (NJR) 24-bit DSP Processor";
2601 case EM_VIDEOCORE3: return "Broadcom VideoCore III processor";
2602 case EM_LATTICEMICO32: return "Lattice Mico32";
2603 case EM_SE_C17: return "Seiko Epson C17 family";
2604 /* 140 */
2605 case EM_TI_C6000: return "Texas Instruments TMS320C6000 DSP family";
2606 case EM_TI_C2000: return "Texas Instruments TMS320C2000 DSP family";
2607 case EM_TI_C5500: return "Texas Instruments TMS320C55x DSP family";
2608 case EM_TI_PRU: return "TI PRU I/O processor";
2609 /* 160 */
2610 case EM_MMDSP_PLUS: return "STMicroelectronics 64bit VLIW Data Signal Processor";
2611 case EM_CYPRESS_M8C: return "Cypress M8C microprocessor";
2612 case EM_R32C: return "Renesas R32C series microprocessors";
2613 case EM_TRIMEDIA: return "NXP Semiconductors TriMedia architecture family";
2614 case EM_QDSP6: return "QUALCOMM DSP6 Processor";
2615 case EM_8051: return "Intel 8051 and variants";
2616 case EM_STXP7X: return "STMicroelectronics STxP7x family";
2617 case EM_NDS32: return "Andes Technology compact code size embedded RISC processor family";
2618 case EM_ECOG1X: return "Cyan Technology eCOG1X family";
2619 case EM_MAXQ30: return "Dallas Semiconductor MAXQ30 Core microcontrollers";
2620 /* 170 */
2621 case EM_XIMO16: return "New Japan Radio (NJR) 16-bit DSP Processor";
2622 case EM_MANIK: return "M2000 Reconfigurable RISC Microprocessor";
2623 case EM_CRAYNV2: return "Cray Inc. NV2 vector architecture";
2624 case EM_RX: return "Renesas RX";
2625 case EM_METAG: return "Imagination Technologies Meta processor architecture";
2626 case EM_MCST_ELBRUS: return "MCST Elbrus general purpose hardware architecture";
2627 case EM_ECOG16: return "Cyan Technology eCOG16 family";
2628 case EM_CR16:
2629 case EM_MICROBLAZE:
2630 case EM_MICROBLAZE_OLD: return "Xilinx MicroBlaze";
2631 case EM_ETPU: return "Freescale Extended Time Processing Unit";
2632 case EM_SLE9X: return "Infineon Technologies SLE9X core";
2633 /* 180 */
2634 case EM_L1OM: return "Intel L1OM";
2635 case EM_K1OM: return "Intel K1OM";
2636 case EM_INTEL182: return "Intel (reserved)";
2637 case EM_AARCH64: return "AArch64";
2638 case EM_ARM184: return "ARM (reserved)";
2639 case EM_AVR32: return "Atmel Corporation 32-bit microprocessor";
2640 case EM_STM8: return "STMicroeletronics STM8 8-bit microcontroller";
2641 case EM_TILE64: return "Tilera TILE64 multicore architecture family";
2642 case EM_TILEPRO: return "Tilera TILEPro multicore architecture family";
2643 /* 190 */
2644 case EM_CUDA: return "NVIDIA CUDA architecture";
2645 case EM_TILEGX: return "Tilera TILE-Gx multicore architecture family";
2646 case EM_CLOUDSHIELD: return "CloudShield architecture family";
2647 case EM_COREA_1ST: return "KIPO-KAIST Core-A 1st generation processor family";
2648 case EM_COREA_2ND: return "KIPO-KAIST Core-A 2nd generation processor family";
2649 case EM_ARC_COMPACT2: return "ARCv2";
2650 case EM_OPEN8: return "Open8 8-bit RISC soft processor core";
2651 case EM_RL78: return "Renesas RL78";
2652 case EM_VIDEOCORE5: return "Broadcom VideoCore V processor";
2653 case EM_78K0R: return "Renesas 78K0R";
2654 /* 200 */
2655 case EM_56800EX: return "Freescale 56800EX Digital Signal Controller (DSC)";
2656 case EM_BA1: return "Beyond BA1 CPU architecture";
2657 case EM_BA2: return "Beyond BA2 CPU architecture";
2658 case EM_XCORE: return "XMOS xCORE processor family";
2659 case EM_MCHP_PIC: return "Microchip 8-bit PIC(r) family";
2660 case EM_INTELGT: return "Intel Graphics Technology";
2661 /* 210 */
2662 case EM_KM32: return "KM211 KM32 32-bit processor";
2663 case EM_KMX32: return "KM211 KMX32 32-bit processor";
2664 case EM_KMX16: return "KM211 KMX16 16-bit processor";
2665 case EM_KMX8: return "KM211 KMX8 8-bit processor";
2666 case EM_KVARC: return "KM211 KVARC processor";
2667 case EM_CDP: return "Paneve CDP architecture family";
2668 case EM_COGE: return "Cognitive Smart Memory Processor";
2669 case EM_COOL: return "Bluechip Systems CoolEngine";
2670 case EM_NORC: return "Nanoradio Optimized RISC";
2671 case EM_CSR_KALIMBA: return "CSR Kalimba architecture family";
2672 /* 220 */
2673 case EM_Z80: return "Zilog Z80";
2674 case EM_VISIUM: return "CDS VISIUMcore processor";
2675 case EM_FT32: return "FTDI Chip FT32";
2676 case EM_MOXIE: return "Moxie";
2677 case EM_AMDGPU: return "AMD GPU";
2678 /* 230 (all reserved) */
2679 /* 240 */
2680 case EM_RISCV: return "RISC-V";
2681 case EM_LANAI: return "Lanai 32-bit processor";
2682 case EM_CEVA: return "CEVA Processor Architecture Family";
2683 case EM_CEVA_X2: return "CEVA X2 Processor Family";
2684 case EM_BPF: return "Linux BPF";
2685 case EM_GRAPHCORE_IPU: return "Graphcore Intelligent Processing Unit";
2686 case EM_IMG1: return "Imagination Technologies";
2687 /* 250 */
2688 case EM_NFP: return "Netronome Flow Processor";
2689 case EM_VE: return "NEC Vector Engine";
2690 case EM_CSKY: return "C-SKY";
2691 case EM_ARC_COMPACT3_64: return "Synopsys ARCv2.3 64-bit";
2692 case EM_MCS6502: return "MOS Technology MCS 6502 processor";
2693 case EM_ARC_COMPACT3: return "Synopsys ARCv2.3 32-bit";
2694 case EM_KVX: return "Kalray VLIW core of the MPPA processor family";
2695 case EM_65816: return "WDC 65816/65C816";
2696 case EM_LOONGARCH: return "LoongArch";
2697 case EM_KF32: return "ChipON KungFu32";
2698
2699 /* Large numbers... */
2700 case EM_MT: return "Morpho Techologies MT processor";
2701 case EM_ALPHA: return "Alpha";
2702 case EM_WEBASSEMBLY: return "Web Assembly";
2703 case EM_DLX: return "OpenDLX";
2704 case EM_XSTORMY16: return "Sanyo XStormy16 CPU core";
2705 case EM_IQ2000: return "Vitesse IQ2000";
2706 case EM_M32C_OLD:
2707 case EM_NIOS32: return "Altera Nios";
2708 case EM_CYGNUS_MEP: return "Toshiba MeP Media Engine";
2709 case EM_ADAPTEVA_EPIPHANY: return "Adapteva EPIPHANY";
2710 case EM_CYGNUS_FRV: return "Fujitsu FR-V";
2711 case EM_S12Z: return "Freescale S12Z";
2712
2713 default:
2714 snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
2715 return buff;
2716 }
2717 }
2718
2719 static void
2720 decode_ARC_machine_flags (unsigned e_flags, unsigned e_machine, char buf[])
2721 {
2722 /* ARC has two machine types EM_ARC_COMPACT and EM_ARC_COMPACT2. Some
2723 other compilers don't specify an architecture type in the e_flags, and
2724 instead use EM_ARC_COMPACT for old ARC600, ARC601, and ARC700
2725 architectures, and switch to EM_ARC_COMPACT2 for newer ARCEM and ARCHS
2726 architectures.
2727
2728 Th GNU tools follows this use of EM_ARC_COMPACT and EM_ARC_COMPACT2,
2729 but also sets a specific architecture type in the e_flags field.
2730
2731 However, when decoding the flags we don't worry if we see an
2732 unexpected pairing, for example EM_ARC_COMPACT machine type, with
2733 ARCEM architecture type. */
2734
2735 switch (e_flags & EF_ARC_MACH_MSK)
2736 {
2737 /* We only expect these to occur for EM_ARC_COMPACT2. */
2738 case EF_ARC_CPU_ARCV2EM:
2739 strcat (buf, ", ARC EM");
2740 break;
2741 case EF_ARC_CPU_ARCV2HS:
2742 strcat (buf, ", ARC HS");
2743 break;
2744
2745 /* We only expect these to occur for EM_ARC_COMPACT. */
2746 case E_ARC_MACH_ARC600:
2747 strcat (buf, ", ARC600");
2748 break;
2749 case E_ARC_MACH_ARC601:
2750 strcat (buf, ", ARC601");
2751 break;
2752 case E_ARC_MACH_ARC700:
2753 strcat (buf, ", ARC700");
2754 break;
2755
2756 /* The only times we should end up here are (a) A corrupt ELF, (b) A
2757 new ELF with new architecture being read by an old version of
2758 readelf, or (c) An ELF built with non-GNU compiler that does not
2759 set the architecture in the e_flags. */
2760 default:
2761 if (e_machine == EM_ARC_COMPACT)
2762 strcat (buf, ", Unknown ARCompact");
2763 else
2764 strcat (buf, ", Unknown ARC");
2765 break;
2766 }
2767
2768 switch (e_flags & EF_ARC_OSABI_MSK)
2769 {
2770 case E_ARC_OSABI_ORIG:
2771 strcat (buf, ", (ABI:legacy)");
2772 break;
2773 case E_ARC_OSABI_V2:
2774 strcat (buf, ", (ABI:v2)");
2775 break;
2776 /* Only upstream 3.9+ kernels will support ARCv2 ISA. */
2777 case E_ARC_OSABI_V3:
2778 strcat (buf, ", v3 no-legacy-syscalls ABI");
2779 break;
2780 case E_ARC_OSABI_V4:
2781 strcat (buf, ", v4 ABI");
2782 break;
2783 default:
2784 strcat (buf, ", unrecognised ARC OSABI flag");
2785 break;
2786 }
2787 }
2788
2789 static void
2790 decode_ARM_machine_flags (unsigned e_flags, char buf[])
2791 {
2792 unsigned eabi;
2793 bool unknown = false;
2794
2795 eabi = EF_ARM_EABI_VERSION (e_flags);
2796 e_flags &= ~ EF_ARM_EABIMASK;
2797
2798 /* Handle "generic" ARM flags. */
2799 if (e_flags & EF_ARM_RELEXEC)
2800 {
2801 strcat (buf, ", relocatable executable");
2802 e_flags &= ~ EF_ARM_RELEXEC;
2803 }
2804
2805 if (e_flags & EF_ARM_PIC)
2806 {
2807 strcat (buf, ", position independent");
2808 e_flags &= ~ EF_ARM_PIC;
2809 }
2810
2811 /* Now handle EABI specific flags. */
2812 switch (eabi)
2813 {
2814 default:
2815 strcat (buf, ", <unrecognized EABI>");
2816 if (e_flags)
2817 unknown = true;
2818 break;
2819
2820 case EF_ARM_EABI_VER1:
2821 strcat (buf, ", Version1 EABI");
2822 while (e_flags)
2823 {
2824 unsigned flag;
2825
2826 /* Process flags one bit at a time. */
2827 flag = e_flags & - e_flags;
2828 e_flags &= ~ flag;
2829
2830 switch (flag)
2831 {
2832 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
2833 strcat (buf, ", sorted symbol tables");
2834 break;
2835
2836 default:
2837 unknown = true;
2838 break;
2839 }
2840 }
2841 break;
2842
2843 case EF_ARM_EABI_VER2:
2844 strcat (buf, ", Version2 EABI");
2845 while (e_flags)
2846 {
2847 unsigned flag;
2848
2849 /* Process flags one bit at a time. */
2850 flag = e_flags & - e_flags;
2851 e_flags &= ~ flag;
2852
2853 switch (flag)
2854 {
2855 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
2856 strcat (buf, ", sorted symbol tables");
2857 break;
2858
2859 case EF_ARM_DYNSYMSUSESEGIDX:
2860 strcat (buf, ", dynamic symbols use segment index");
2861 break;
2862
2863 case EF_ARM_MAPSYMSFIRST:
2864 strcat (buf, ", mapping symbols precede others");
2865 break;
2866
2867 default:
2868 unknown = true;
2869 break;
2870 }
2871 }
2872 break;
2873
2874 case EF_ARM_EABI_VER3:
2875 strcat (buf, ", Version3 EABI");
2876 break;
2877
2878 case EF_ARM_EABI_VER4:
2879 strcat (buf, ", Version4 EABI");
2880 while (e_flags)
2881 {
2882 unsigned flag;
2883
2884 /* Process flags one bit at a time. */
2885 flag = e_flags & - e_flags;
2886 e_flags &= ~ flag;
2887
2888 switch (flag)
2889 {
2890 case EF_ARM_BE8:
2891 strcat (buf, ", BE8");
2892 break;
2893
2894 case EF_ARM_LE8:
2895 strcat (buf, ", LE8");
2896 break;
2897
2898 default:
2899 unknown = true;
2900 break;
2901 }
2902 }
2903 break;
2904
2905 case EF_ARM_EABI_VER5:
2906 strcat (buf, ", Version5 EABI");
2907 while (e_flags)
2908 {
2909 unsigned flag;
2910
2911 /* Process flags one bit at a time. */
2912 flag = e_flags & - e_flags;
2913 e_flags &= ~ flag;
2914
2915 switch (flag)
2916 {
2917 case EF_ARM_BE8:
2918 strcat (buf, ", BE8");
2919 break;
2920
2921 case EF_ARM_LE8:
2922 strcat (buf, ", LE8");
2923 break;
2924
2925 case EF_ARM_ABI_FLOAT_SOFT: /* Conflicts with EF_ARM_SOFT_FLOAT. */
2926 strcat (buf, ", soft-float ABI");
2927 break;
2928
2929 case EF_ARM_ABI_FLOAT_HARD: /* Conflicts with EF_ARM_VFP_FLOAT. */
2930 strcat (buf, ", hard-float ABI");
2931 break;
2932
2933 default:
2934 unknown = true;
2935 break;
2936 }
2937 }
2938 break;
2939
2940 case EF_ARM_EABI_UNKNOWN:
2941 strcat (buf, ", GNU EABI");
2942 while (e_flags)
2943 {
2944 unsigned flag;
2945
2946 /* Process flags one bit at a time. */
2947 flag = e_flags & - e_flags;
2948 e_flags &= ~ flag;
2949
2950 switch (flag)
2951 {
2952 case EF_ARM_INTERWORK:
2953 strcat (buf, ", interworking enabled");
2954 break;
2955
2956 case EF_ARM_APCS_26:
2957 strcat (buf, ", uses APCS/26");
2958 break;
2959
2960 case EF_ARM_APCS_FLOAT:
2961 strcat (buf, ", uses APCS/float");
2962 break;
2963
2964 case EF_ARM_PIC:
2965 strcat (buf, ", position independent");
2966 break;
2967
2968 case EF_ARM_ALIGN8:
2969 strcat (buf, ", 8 bit structure alignment");
2970 break;
2971
2972 case EF_ARM_NEW_ABI:
2973 strcat (buf, ", uses new ABI");
2974 break;
2975
2976 case EF_ARM_OLD_ABI:
2977 strcat (buf, ", uses old ABI");
2978 break;
2979
2980 case EF_ARM_SOFT_FLOAT:
2981 strcat (buf, ", software FP");
2982 break;
2983
2984 case EF_ARM_VFP_FLOAT:
2985 strcat (buf, ", VFP");
2986 break;
2987
2988 case EF_ARM_MAVERICK_FLOAT:
2989 strcat (buf, ", Maverick FP");
2990 break;
2991
2992 default:
2993 unknown = true;
2994 break;
2995 }
2996 }
2997 }
2998
2999 if (unknown)
3000 strcat (buf,_(", <unknown>"));
3001 }
3002
3003 static void
3004 decode_AVR_machine_flags (unsigned e_flags, char buf[], size_t size)
3005 {
3006 --size; /* Leave space for null terminator. */
3007
3008 switch (e_flags & EF_AVR_MACH)
3009 {
3010 case E_AVR_MACH_AVR1:
3011 strncat (buf, ", avr:1", size);
3012 break;
3013 case E_AVR_MACH_AVR2:
3014 strncat (buf, ", avr:2", size);
3015 break;
3016 case E_AVR_MACH_AVR25:
3017 strncat (buf, ", avr:25", size);
3018 break;
3019 case E_AVR_MACH_AVR3:
3020 strncat (buf, ", avr:3", size);
3021 break;
3022 case E_AVR_MACH_AVR31:
3023 strncat (buf, ", avr:31", size);
3024 break;
3025 case E_AVR_MACH_AVR35:
3026 strncat (buf, ", avr:35", size);
3027 break;
3028 case E_AVR_MACH_AVR4:
3029 strncat (buf, ", avr:4", size);
3030 break;
3031 case E_AVR_MACH_AVR5:
3032 strncat (buf, ", avr:5", size);
3033 break;
3034 case E_AVR_MACH_AVR51:
3035 strncat (buf, ", avr:51", size);
3036 break;
3037 case E_AVR_MACH_AVR6:
3038 strncat (buf, ", avr:6", size);
3039 break;
3040 case E_AVR_MACH_AVRTINY:
3041 strncat (buf, ", avr:100", size);
3042 break;
3043 case E_AVR_MACH_XMEGA1:
3044 strncat (buf, ", avr:101", size);
3045 break;
3046 case E_AVR_MACH_XMEGA2:
3047 strncat (buf, ", avr:102", size);
3048 break;
3049 case E_AVR_MACH_XMEGA3:
3050 strncat (buf, ", avr:103", size);
3051 break;
3052 case E_AVR_MACH_XMEGA4:
3053 strncat (buf, ", avr:104", size);
3054 break;
3055 case E_AVR_MACH_XMEGA5:
3056 strncat (buf, ", avr:105", size);
3057 break;
3058 case E_AVR_MACH_XMEGA6:
3059 strncat (buf, ", avr:106", size);
3060 break;
3061 case E_AVR_MACH_XMEGA7:
3062 strncat (buf, ", avr:107", size);
3063 break;
3064 default:
3065 strncat (buf, ", avr:<unknown>", size);
3066 break;
3067 }
3068
3069 size -= strlen (buf);
3070 if (e_flags & EF_AVR_LINKRELAX_PREPARED)
3071 strncat (buf, ", link-relax", size);
3072 }
3073
3074 static void
3075 decode_NDS32_machine_flags (unsigned e_flags, char buf[], size_t size)
3076 {
3077 unsigned abi;
3078 unsigned arch;
3079 unsigned config;
3080 unsigned version;
3081 bool has_fpu = false;
3082 unsigned int r = 0;
3083
3084 static const char *ABI_STRINGS[] =
3085 {
3086 "ABI v0", /* use r5 as return register; only used in N1213HC */
3087 "ABI v1", /* use r0 as return register */
3088 "ABI v2", /* use r0 as return register and don't reserve 24 bytes for arguments */
3089 "ABI v2fp", /* for FPU */
3090 "AABI",
3091 "ABI2 FP+"
3092 };
3093 static const char *VER_STRINGS[] =
3094 {
3095 "Andes ELF V1.3 or older",
3096 "Andes ELF V1.3.1",
3097 "Andes ELF V1.4"
3098 };
3099 static const char *ARCH_STRINGS[] =
3100 {
3101 "",
3102 "Andes Star v1.0",
3103 "Andes Star v2.0",
3104 "Andes Star v3.0",
3105 "Andes Star v3.0m"
3106 };
3107
3108 abi = EF_NDS_ABI & e_flags;
3109 arch = EF_NDS_ARCH & e_flags;
3110 config = EF_NDS_INST & e_flags;
3111 version = EF_NDS32_ELF_VERSION & e_flags;
3112
3113 memset (buf, 0, size);
3114
3115 switch (abi)
3116 {
3117 case E_NDS_ABI_V0:
3118 case E_NDS_ABI_V1:
3119 case E_NDS_ABI_V2:
3120 case E_NDS_ABI_V2FP:
3121 case E_NDS_ABI_AABI:
3122 case E_NDS_ABI_V2FP_PLUS:
3123 /* In case there are holes in the array. */
3124 r += snprintf (buf + r, size - r, ", %s", ABI_STRINGS[abi >> EF_NDS_ABI_SHIFT]);
3125 break;
3126
3127 default:
3128 r += snprintf (buf + r, size - r, ", <unrecognized ABI>");
3129 break;
3130 }
3131
3132 switch (version)
3133 {
3134 case E_NDS32_ELF_VER_1_2:
3135 case E_NDS32_ELF_VER_1_3:
3136 case E_NDS32_ELF_VER_1_4:
3137 r += snprintf (buf + r, size - r, ", %s", VER_STRINGS[version >> EF_NDS32_ELF_VERSION_SHIFT]);
3138 break;
3139
3140 default:
3141 r += snprintf (buf + r, size - r, ", <unrecognized ELF version number>");
3142 break;
3143 }
3144
3145 if (E_NDS_ABI_V0 == abi)
3146 {
3147 /* OLD ABI; only used in N1213HC, has performance extension 1. */
3148 r += snprintf (buf + r, size - r, ", Andes Star v1.0, N1213HC, MAC, PERF1");
3149 if (arch == E_NDS_ARCH_STAR_V1_0)
3150 r += snprintf (buf + r, size -r, ", 16b"); /* has 16-bit instructions */
3151 return;
3152 }
3153
3154 switch (arch)
3155 {
3156 case E_NDS_ARCH_STAR_V1_0:
3157 case E_NDS_ARCH_STAR_V2_0:
3158 case E_NDS_ARCH_STAR_V3_0:
3159 case E_NDS_ARCH_STAR_V3_M:
3160 r += snprintf (buf + r, size - r, ", %s", ARCH_STRINGS[arch >> EF_NDS_ARCH_SHIFT]);
3161 break;
3162
3163 default:
3164 r += snprintf (buf + r, size - r, ", <unrecognized architecture>");
3165 /* ARCH version determines how the e_flags are interpreted.
3166 If it is unknown, we cannot proceed. */
3167 return;
3168 }
3169
3170 /* Newer ABI; Now handle architecture specific flags. */
3171 if (arch == E_NDS_ARCH_STAR_V1_0)
3172 {
3173 if (config & E_NDS32_HAS_MFUSR_PC_INST)
3174 r += snprintf (buf + r, size -r, ", MFUSR_PC");
3175
3176 if (!(config & E_NDS32_HAS_NO_MAC_INST))
3177 r += snprintf (buf + r, size -r, ", MAC");
3178
3179 if (config & E_NDS32_HAS_DIV_INST)
3180 r += snprintf (buf + r, size -r, ", DIV");
3181
3182 if (config & E_NDS32_HAS_16BIT_INST)
3183 r += snprintf (buf + r, size -r, ", 16b");
3184 }
3185 else
3186 {
3187 if (config & E_NDS32_HAS_MFUSR_PC_INST)
3188 {
3189 if (version <= E_NDS32_ELF_VER_1_3)
3190 r += snprintf (buf + r, size -r, ", [B8]");
3191 else
3192 r += snprintf (buf + r, size -r, ", EX9");
3193 }
3194
3195 if (config & E_NDS32_HAS_MAC_DX_INST)
3196 r += snprintf (buf + r, size -r, ", MAC_DX");
3197
3198 if (config & E_NDS32_HAS_DIV_DX_INST)
3199 r += snprintf (buf + r, size -r, ", DIV_DX");
3200
3201 if (config & E_NDS32_HAS_16BIT_INST)
3202 {
3203 if (version <= E_NDS32_ELF_VER_1_3)
3204 r += snprintf (buf + r, size -r, ", 16b");
3205 else
3206 r += snprintf (buf + r, size -r, ", IFC");
3207 }
3208 }
3209
3210 if (config & E_NDS32_HAS_EXT_INST)
3211 r += snprintf (buf + r, size -r, ", PERF1");
3212
3213 if (config & E_NDS32_HAS_EXT2_INST)
3214 r += snprintf (buf + r, size -r, ", PERF2");
3215
3216 if (config & E_NDS32_HAS_FPU_INST)
3217 {
3218 has_fpu = true;
3219 r += snprintf (buf + r, size -r, ", FPU_SP");
3220 }
3221
3222 if (config & E_NDS32_HAS_FPU_DP_INST)
3223 {
3224 has_fpu = true;
3225 r += snprintf (buf + r, size -r, ", FPU_DP");
3226 }
3227
3228 if (config & E_NDS32_HAS_FPU_MAC_INST)
3229 {
3230 has_fpu = true;
3231 r += snprintf (buf + r, size -r, ", FPU_MAC");
3232 }
3233
3234 if (has_fpu)
3235 {
3236 switch ((config & E_NDS32_FPU_REG_CONF) >> E_NDS32_FPU_REG_CONF_SHIFT)
3237 {
3238 case E_NDS32_FPU_REG_8SP_4DP:
3239 r += snprintf (buf + r, size -r, ", FPU_REG:8/4");
3240 break;
3241 case E_NDS32_FPU_REG_16SP_8DP:
3242 r += snprintf (buf + r, size -r, ", FPU_REG:16/8");
3243 break;
3244 case E_NDS32_FPU_REG_32SP_16DP:
3245 r += snprintf (buf + r, size -r, ", FPU_REG:32/16");
3246 break;
3247 case E_NDS32_FPU_REG_32SP_32DP:
3248 r += snprintf (buf + r, size -r, ", FPU_REG:32/32");
3249 break;
3250 }
3251 }
3252
3253 if (config & E_NDS32_HAS_AUDIO_INST)
3254 r += snprintf (buf + r, size -r, ", AUDIO");
3255
3256 if (config & E_NDS32_HAS_STRING_INST)
3257 r += snprintf (buf + r, size -r, ", STR");
3258
3259 if (config & E_NDS32_HAS_REDUCED_REGS)
3260 r += snprintf (buf + r, size -r, ", 16REG");
3261
3262 if (config & E_NDS32_HAS_VIDEO_INST)
3263 {
3264 if (version <= E_NDS32_ELF_VER_1_3)
3265 r += snprintf (buf + r, size -r, ", VIDEO");
3266 else
3267 r += snprintf (buf + r, size -r, ", SATURATION");
3268 }
3269
3270 if (config & E_NDS32_HAS_ENCRIPT_INST)
3271 r += snprintf (buf + r, size -r, ", ENCRP");
3272
3273 if (config & E_NDS32_HAS_L2C_INST)
3274 r += snprintf (buf + r, size -r, ", L2C");
3275 }
3276
3277 static char *
3278 get_machine_flags (Filedata * filedata, unsigned e_flags, unsigned e_machine)
3279 {
3280 static char buf[1024];
3281
3282 buf[0] = '\0';
3283
3284 if (e_flags)
3285 {
3286 switch (e_machine)
3287 {
3288 default:
3289 break;
3290
3291 case EM_ARC_COMPACT2:
3292 case EM_ARC_COMPACT:
3293 decode_ARC_machine_flags (e_flags, e_machine, buf);
3294 break;
3295
3296 case EM_ARM:
3297 decode_ARM_machine_flags (e_flags, buf);
3298 break;
3299
3300 case EM_AVR:
3301 decode_AVR_machine_flags (e_flags, buf, sizeof buf);
3302 break;
3303
3304 case EM_BLACKFIN:
3305 if (e_flags & EF_BFIN_PIC)
3306 strcat (buf, ", PIC");
3307
3308 if (e_flags & EF_BFIN_FDPIC)
3309 strcat (buf, ", FDPIC");
3310
3311 if (e_flags & EF_BFIN_CODE_IN_L1)
3312 strcat (buf, ", code in L1");
3313
3314 if (e_flags & EF_BFIN_DATA_IN_L1)
3315 strcat (buf, ", data in L1");
3316
3317 break;
3318
3319 case EM_CYGNUS_FRV:
3320 switch (e_flags & EF_FRV_CPU_MASK)
3321 {
3322 case EF_FRV_CPU_GENERIC:
3323 break;
3324
3325 default:
3326 strcat (buf, ", fr???");
3327 break;
3328
3329 case EF_FRV_CPU_FR300:
3330 strcat (buf, ", fr300");
3331 break;
3332
3333 case EF_FRV_CPU_FR400:
3334 strcat (buf, ", fr400");
3335 break;
3336 case EF_FRV_CPU_FR405:
3337 strcat (buf, ", fr405");
3338 break;
3339
3340 case EF_FRV_CPU_FR450:
3341 strcat (buf, ", fr450");
3342 break;
3343
3344 case EF_FRV_CPU_FR500:
3345 strcat (buf, ", fr500");
3346 break;
3347 case EF_FRV_CPU_FR550:
3348 strcat (buf, ", fr550");
3349 break;
3350
3351 case EF_FRV_CPU_SIMPLE:
3352 strcat (buf, ", simple");
3353 break;
3354 case EF_FRV_CPU_TOMCAT:
3355 strcat (buf, ", tomcat");
3356 break;
3357 }
3358 break;
3359
3360 case EM_68K:
3361 if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
3362 strcat (buf, ", m68000");
3363 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
3364 strcat (buf, ", cpu32");
3365 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
3366 strcat (buf, ", fido_a");
3367 else
3368 {
3369 char const * isa = _("unknown");
3370 char const * mac = _("unknown mac");
3371 char const * additional = NULL;
3372
3373 switch (e_flags & EF_M68K_CF_ISA_MASK)
3374 {
3375 case EF_M68K_CF_ISA_A_NODIV:
3376 isa = "A";
3377 additional = ", nodiv";
3378 break;
3379 case EF_M68K_CF_ISA_A:
3380 isa = "A";
3381 break;
3382 case EF_M68K_CF_ISA_A_PLUS:
3383 isa = "A+";
3384 break;
3385 case EF_M68K_CF_ISA_B_NOUSP:
3386 isa = "B";
3387 additional = ", nousp";
3388 break;
3389 case EF_M68K_CF_ISA_B:
3390 isa = "B";
3391 break;
3392 case EF_M68K_CF_ISA_C:
3393 isa = "C";
3394 break;
3395 case EF_M68K_CF_ISA_C_NODIV:
3396 isa = "C";
3397 additional = ", nodiv";
3398 break;
3399 }
3400 strcat (buf, ", cf, isa ");
3401 strcat (buf, isa);
3402 if (additional)
3403 strcat (buf, additional);
3404 if (e_flags & EF_M68K_CF_FLOAT)
3405 strcat (buf, ", float");
3406 switch (e_flags & EF_M68K_CF_MAC_MASK)
3407 {
3408 case 0:
3409 mac = NULL;
3410 break;
3411 case EF_M68K_CF_MAC:
3412 mac = "mac";
3413 break;
3414 case EF_M68K_CF_EMAC:
3415 mac = "emac";
3416 break;
3417 case EF_M68K_CF_EMAC_B:
3418 mac = "emac_b";
3419 break;
3420 }
3421 if (mac)
3422 {
3423 strcat (buf, ", ");
3424 strcat (buf, mac);
3425 }
3426 }
3427 break;
3428
3429 case EM_CYGNUS_MEP:
3430 switch (e_flags & EF_MEP_CPU_MASK)
3431 {
3432 case EF_MEP_CPU_MEP: strcat (buf, ", generic MeP"); break;
3433 case EF_MEP_CPU_C2: strcat (buf, ", MeP C2"); break;
3434 case EF_MEP_CPU_C3: strcat (buf, ", MeP C3"); break;
3435 case EF_MEP_CPU_C4: strcat (buf, ", MeP C4"); break;
3436 case EF_MEP_CPU_C5: strcat (buf, ", MeP C5"); break;
3437 case EF_MEP_CPU_H1: strcat (buf, ", MeP H1"); break;
3438 default: strcat (buf, _(", <unknown MeP cpu type>")); break;
3439 }
3440
3441 switch (e_flags & EF_MEP_COP_MASK)
3442 {
3443 case EF_MEP_COP_NONE: break;
3444 case EF_MEP_COP_AVC: strcat (buf, ", AVC coprocessor"); break;
3445 case EF_MEP_COP_AVC2: strcat (buf, ", AVC2 coprocessor"); break;
3446 case EF_MEP_COP_FMAX: strcat (buf, ", FMAX coprocessor"); break;
3447 case EF_MEP_COP_IVC2: strcat (buf, ", IVC2 coprocessor"); break;
3448 default: strcat (buf, _("<unknown MeP copro type>")); break;
3449 }
3450
3451 if (e_flags & EF_MEP_LIBRARY)
3452 strcat (buf, ", Built for Library");
3453
3454 if (e_flags & EF_MEP_INDEX_MASK)
3455 sprintf (buf + strlen (buf), ", Configuration Index: %#x",
3456 e_flags & EF_MEP_INDEX_MASK);
3457
3458 if (e_flags & ~ EF_MEP_ALL_FLAGS)
3459 sprintf (buf + strlen (buf), _(", unknown flags bits: %#x"),
3460 e_flags & ~ EF_MEP_ALL_FLAGS);
3461 break;
3462
3463 case EM_PPC:
3464 if (e_flags & EF_PPC_EMB)
3465 strcat (buf, ", emb");
3466
3467 if (e_flags & EF_PPC_RELOCATABLE)
3468 strcat (buf, _(", relocatable"));
3469
3470 if (e_flags & EF_PPC_RELOCATABLE_LIB)
3471 strcat (buf, _(", relocatable-lib"));
3472 break;
3473
3474 case EM_PPC64:
3475 if (e_flags & EF_PPC64_ABI)
3476 {
3477 char abi[] = ", abiv0";
3478
3479 abi[6] += e_flags & EF_PPC64_ABI;
3480 strcat (buf, abi);
3481 }
3482 break;
3483
3484 case EM_V800:
3485 if ((e_flags & EF_RH850_ABI) == EF_RH850_ABI)
3486 strcat (buf, ", RH850 ABI");
3487
3488 if (e_flags & EF_V800_850E3)
3489 strcat (buf, ", V3 architecture");
3490
3491 if ((e_flags & (EF_RH850_FPU_DOUBLE | EF_RH850_FPU_SINGLE)) == 0)
3492 strcat (buf, ", FPU not used");
3493
3494 if ((e_flags & (EF_RH850_REGMODE22 | EF_RH850_REGMODE32)) == 0)
3495 strcat (buf, ", regmode: COMMON");
3496
3497 if ((e_flags & (EF_RH850_GP_FIX | EF_RH850_GP_NOFIX)) == 0)
3498 strcat (buf, ", r4 not used");
3499
3500 if ((e_flags & (EF_RH850_EP_FIX | EF_RH850_EP_NOFIX)) == 0)
3501 strcat (buf, ", r30 not used");
3502
3503 if ((e_flags & (EF_RH850_TP_FIX | EF_RH850_TP_NOFIX)) == 0)
3504 strcat (buf, ", r5 not used");
3505
3506 if ((e_flags & (EF_RH850_REG2_RESERVE | EF_RH850_REG2_NORESERVE)) == 0)
3507 strcat (buf, ", r2 not used");
3508
3509 for (e_flags &= 0xFFFF; e_flags; e_flags &= ~ (e_flags & - e_flags))
3510 {
3511 switch (e_flags & - e_flags)
3512 {
3513 case EF_RH850_FPU_DOUBLE: strcat (buf, ", double precision FPU"); break;
3514 case EF_RH850_FPU_SINGLE: strcat (buf, ", single precision FPU"); break;
3515 case EF_RH850_REGMODE22: strcat (buf, ", regmode:22"); break;
3516 case EF_RH850_REGMODE32: strcat (buf, ", regmode:23"); break;
3517 case EF_RH850_GP_FIX: strcat (buf, ", r4 fixed"); break;
3518 case EF_RH850_GP_NOFIX: strcat (buf, ", r4 free"); break;
3519 case EF_RH850_EP_FIX: strcat (buf, ", r30 fixed"); break;
3520 case EF_RH850_EP_NOFIX: strcat (buf, ", r30 free"); break;
3521 case EF_RH850_TP_FIX: strcat (buf, ", r5 fixed"); break;
3522 case EF_RH850_TP_NOFIX: strcat (buf, ", r5 free"); break;
3523 case EF_RH850_REG2_RESERVE: strcat (buf, ", r2 fixed"); break;
3524 case EF_RH850_REG2_NORESERVE: strcat (buf, ", r2 free"); break;
3525 default: break;
3526 }
3527 }
3528 break;
3529
3530 case EM_V850:
3531 case EM_CYGNUS_V850:
3532 switch (e_flags & EF_V850_ARCH)
3533 {
3534 case E_V850E3V5_ARCH:
3535 strcat (buf, ", v850e3v5");
3536 break;
3537 case E_V850E2V3_ARCH:
3538 strcat (buf, ", v850e2v3");
3539 break;
3540 case E_V850E2_ARCH:
3541 strcat (buf, ", v850e2");
3542 break;
3543 case E_V850E1_ARCH:
3544 strcat (buf, ", v850e1");
3545 break;
3546 case E_V850E_ARCH:
3547 strcat (buf, ", v850e");
3548 break;
3549 case E_V850_ARCH:
3550 strcat (buf, ", v850");
3551 break;
3552 default:
3553 strcat (buf, _(", unknown v850 architecture variant"));
3554 break;
3555 }
3556 break;
3557
3558 case EM_M32R:
3559 case EM_CYGNUS_M32R:
3560 if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
3561 strcat (buf, ", m32r");
3562 break;
3563
3564 case EM_MIPS:
3565 case EM_MIPS_RS3_LE:
3566 if (e_flags & EF_MIPS_NOREORDER)
3567 strcat (buf, ", noreorder");
3568
3569 if (e_flags & EF_MIPS_PIC)
3570 strcat (buf, ", pic");
3571
3572 if (e_flags & EF_MIPS_CPIC)
3573 strcat (buf, ", cpic");
3574
3575 if (e_flags & EF_MIPS_UCODE)
3576 strcat (buf, ", ugen_reserved");
3577
3578 if (e_flags & EF_MIPS_ABI2)
3579 strcat (buf, ", abi2");
3580
3581 if (e_flags & EF_MIPS_OPTIONS_FIRST)
3582 strcat (buf, ", odk first");
3583
3584 if (e_flags & EF_MIPS_32BITMODE)
3585 strcat (buf, ", 32bitmode");
3586
3587 if (e_flags & EF_MIPS_NAN2008)
3588 strcat (buf, ", nan2008");
3589
3590 if (e_flags & EF_MIPS_FP64)
3591 strcat (buf, ", fp64");
3592
3593 switch ((e_flags & EF_MIPS_MACH))
3594 {
3595 case E_MIPS_MACH_3900: strcat (buf, ", 3900"); break;
3596 case E_MIPS_MACH_4010: strcat (buf, ", 4010"); break;
3597 case E_MIPS_MACH_4100: strcat (buf, ", 4100"); break;
3598 case E_MIPS_MACH_4111: strcat (buf, ", 4111"); break;
3599 case E_MIPS_MACH_4120: strcat (buf, ", 4120"); break;
3600 case E_MIPS_MACH_4650: strcat (buf, ", 4650"); break;
3601 case E_MIPS_MACH_5400: strcat (buf, ", 5400"); break;
3602 case E_MIPS_MACH_5500: strcat (buf, ", 5500"); break;
3603 case E_MIPS_MACH_5900: strcat (buf, ", 5900"); break;
3604 case E_MIPS_MACH_SB1: strcat (buf, ", sb1"); break;
3605 case E_MIPS_MACH_9000: strcat (buf, ", 9000"); break;
3606 case E_MIPS_MACH_LS2E: strcat (buf, ", loongson-2e"); break;
3607 case E_MIPS_MACH_LS2F: strcat (buf, ", loongson-2f"); break;
3608 case E_MIPS_MACH_GS464: strcat (buf, ", gs464"); break;
3609 case E_MIPS_MACH_GS464E: strcat (buf, ", gs464e"); break;
3610 case E_MIPS_MACH_GS264E: strcat (buf, ", gs264e"); break;
3611 case E_MIPS_MACH_OCTEON: strcat (buf, ", octeon"); break;
3612 case E_MIPS_MACH_OCTEON2: strcat (buf, ", octeon2"); break;
3613 case E_MIPS_MACH_OCTEON3: strcat (buf, ", octeon3"); break;
3614 case E_MIPS_MACH_XLR: strcat (buf, ", xlr"); break;
3615 case E_MIPS_MACH_IAMR2: strcat (buf, ", interaptiv-mr2"); break;
3616 case 0:
3617 /* We simply ignore the field in this case to avoid confusion:
3618 MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
3619 extension. */
3620 break;
3621 default: strcat (buf, _(", unknown CPU")); break;
3622 }
3623
3624 switch ((e_flags & EF_MIPS_ABI))
3625 {
3626 case E_MIPS_ABI_O32: strcat (buf, ", o32"); break;
3627 case E_MIPS_ABI_O64: strcat (buf, ", o64"); break;
3628 case E_MIPS_ABI_EABI32: strcat (buf, ", eabi32"); break;
3629 case E_MIPS_ABI_EABI64: strcat (buf, ", eabi64"); break;
3630 case 0:
3631 /* We simply ignore the field in this case to avoid confusion:
3632 MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
3633 This means it is likely to be an o32 file, but not for
3634 sure. */
3635 break;
3636 default: strcat (buf, _(", unknown ABI")); break;
3637 }
3638
3639 if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
3640 strcat (buf, ", mdmx");
3641
3642 if (e_flags & EF_MIPS_ARCH_ASE_M16)
3643 strcat (buf, ", mips16");
3644
3645 if (e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
3646 strcat (buf, ", micromips");
3647
3648 switch ((e_flags & EF_MIPS_ARCH))
3649 {
3650 case E_MIPS_ARCH_1: strcat (buf, ", mips1"); break;
3651 case E_MIPS_ARCH_2: strcat (buf, ", mips2"); break;
3652 case E_MIPS_ARCH_3: strcat (buf, ", mips3"); break;
3653 case E_MIPS_ARCH_4: strcat (buf, ", mips4"); break;
3654 case E_MIPS_ARCH_5: strcat (buf, ", mips5"); break;
3655 case E_MIPS_ARCH_32: strcat (buf, ", mips32"); break;
3656 case E_MIPS_ARCH_32R2: strcat (buf, ", mips32r2"); break;
3657 case E_MIPS_ARCH_32R6: strcat (buf, ", mips32r6"); break;
3658 case E_MIPS_ARCH_64: strcat (buf, ", mips64"); break;
3659 case E_MIPS_ARCH_64R2: strcat (buf, ", mips64r2"); break;
3660 case E_MIPS_ARCH_64R6: strcat (buf, ", mips64r6"); break;
3661 default: strcat (buf, _(", unknown ISA")); break;
3662 }
3663 break;
3664
3665 case EM_NDS32:
3666 decode_NDS32_machine_flags (e_flags, buf, sizeof buf);
3667 break;
3668
3669 case EM_NFP:
3670 switch (EF_NFP_MACH (e_flags))
3671 {
3672 case E_NFP_MACH_3200:
3673 strcat (buf, ", NFP-32xx");
3674 break;
3675 case E_NFP_MACH_6000:
3676 strcat (buf, ", NFP-6xxx");
3677 break;
3678 }
3679 break;
3680
3681 case EM_RISCV:
3682 if (e_flags & EF_RISCV_RVC)
3683 strcat (buf, ", RVC");
3684
3685 if (e_flags & EF_RISCV_RVE)
3686 strcat (buf, ", RVE");
3687
3688 switch (e_flags & EF_RISCV_FLOAT_ABI)
3689 {
3690 case EF_RISCV_FLOAT_ABI_SOFT:
3691 strcat (buf, ", soft-float ABI");
3692 break;
3693
3694 case EF_RISCV_FLOAT_ABI_SINGLE:
3695 strcat (buf, ", single-float ABI");
3696 break;
3697
3698 case EF_RISCV_FLOAT_ABI_DOUBLE:
3699 strcat (buf, ", double-float ABI");
3700 break;
3701
3702 case EF_RISCV_FLOAT_ABI_QUAD:
3703 strcat (buf, ", quad-float ABI");
3704 break;
3705 }
3706 break;
3707
3708 case EM_SH:
3709 switch ((e_flags & EF_SH_MACH_MASK))
3710 {
3711 case EF_SH1: strcat (buf, ", sh1"); break;
3712 case EF_SH2: strcat (buf, ", sh2"); break;
3713 case EF_SH3: strcat (buf, ", sh3"); break;
3714 case EF_SH_DSP: strcat (buf, ", sh-dsp"); break;
3715 case EF_SH3_DSP: strcat (buf, ", sh3-dsp"); break;
3716 case EF_SH4AL_DSP: strcat (buf, ", sh4al-dsp"); break;
3717 case EF_SH3E: strcat (buf, ", sh3e"); break;
3718 case EF_SH4: strcat (buf, ", sh4"); break;
3719 case EF_SH5: strcat (buf, ", sh5"); break;
3720 case EF_SH2E: strcat (buf, ", sh2e"); break;
3721 case EF_SH4A: strcat (buf, ", sh4a"); break;
3722 case EF_SH2A: strcat (buf, ", sh2a"); break;
3723 case EF_SH4_NOFPU: strcat (buf, ", sh4-nofpu"); break;
3724 case EF_SH4A_NOFPU: strcat (buf, ", sh4a-nofpu"); break;
3725 case EF_SH2A_NOFPU: strcat (buf, ", sh2a-nofpu"); break;
3726 case EF_SH3_NOMMU: strcat (buf, ", sh3-nommu"); break;
3727 case EF_SH4_NOMMU_NOFPU: strcat (buf, ", sh4-nommu-nofpu"); break;
3728 case EF_SH2A_SH4_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh4-nommu-nofpu"); break;
3729 case EF_SH2A_SH3_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh3-nommu"); break;
3730 case EF_SH2A_SH4: strcat (buf, ", sh2a-or-sh4"); break;
3731 case EF_SH2A_SH3E: strcat (buf, ", sh2a-or-sh3e"); break;
3732 default: strcat (buf, _(", unknown ISA")); break;
3733 }
3734
3735 if (e_flags & EF_SH_PIC)
3736 strcat (buf, ", pic");
3737
3738 if (e_flags & EF_SH_FDPIC)
3739 strcat (buf, ", fdpic");
3740 break;
3741
3742 case EM_OR1K:
3743 if (e_flags & EF_OR1K_NODELAY)
3744 strcat (buf, ", no delay");
3745 break;
3746
3747 case EM_SPARCV9:
3748 if (e_flags & EF_SPARC_32PLUS)
3749 strcat (buf, ", v8+");
3750
3751 if (e_flags & EF_SPARC_SUN_US1)
3752 strcat (buf, ", ultrasparcI");
3753
3754 if (e_flags & EF_SPARC_SUN_US3)
3755 strcat (buf, ", ultrasparcIII");
3756
3757 if (e_flags & EF_SPARC_HAL_R1)
3758 strcat (buf, ", halr1");
3759
3760 if (e_flags & EF_SPARC_LEDATA)
3761 strcat (buf, ", ledata");
3762
3763 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
3764 strcat (buf, ", tso");
3765
3766 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
3767 strcat (buf, ", pso");
3768
3769 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
3770 strcat (buf, ", rmo");
3771 break;
3772
3773 case EM_PARISC:
3774 switch (e_flags & EF_PARISC_ARCH)
3775 {
3776 case EFA_PARISC_1_0:
3777 strcpy (buf, ", PA-RISC 1.0");
3778 break;
3779 case EFA_PARISC_1_1:
3780 strcpy (buf, ", PA-RISC 1.1");
3781 break;
3782 case EFA_PARISC_2_0:
3783 strcpy (buf, ", PA-RISC 2.0");
3784 break;
3785 default:
3786 break;
3787 }
3788 if (e_flags & EF_PARISC_TRAPNIL)
3789 strcat (buf, ", trapnil");
3790 if (e_flags & EF_PARISC_EXT)
3791 strcat (buf, ", ext");
3792 if (e_flags & EF_PARISC_LSB)
3793 strcat (buf, ", lsb");
3794 if (e_flags & EF_PARISC_WIDE)
3795 strcat (buf, ", wide");
3796 if (e_flags & EF_PARISC_NO_KABP)
3797 strcat (buf, ", no kabp");
3798 if (e_flags & EF_PARISC_LAZYSWAP)
3799 strcat (buf, ", lazyswap");
3800 break;
3801
3802 case EM_PJ:
3803 case EM_PJ_OLD:
3804 if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
3805 strcat (buf, ", new calling convention");
3806
3807 if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
3808 strcat (buf, ", gnu calling convention");
3809 break;
3810
3811 case EM_IA_64:
3812 if ((e_flags & EF_IA_64_ABI64))
3813 strcat (buf, ", 64-bit");
3814 else
3815 strcat (buf, ", 32-bit");
3816 if ((e_flags & EF_IA_64_REDUCEDFP))
3817 strcat (buf, ", reduced fp model");
3818 if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
3819 strcat (buf, ", no function descriptors, constant gp");
3820 else if ((e_flags & EF_IA_64_CONS_GP))
3821 strcat (buf, ", constant gp");
3822 if ((e_flags & EF_IA_64_ABSOLUTE))
3823 strcat (buf, ", absolute");
3824 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
3825 {
3826 if ((e_flags & EF_IA_64_VMS_LINKAGES))
3827 strcat (buf, ", vms_linkages");
3828 switch ((e_flags & EF_IA_64_VMS_COMCOD))
3829 {
3830 case EF_IA_64_VMS_COMCOD_SUCCESS:
3831 break;
3832 case EF_IA_64_VMS_COMCOD_WARNING:
3833 strcat (buf, ", warning");
3834 break;
3835 case EF_IA_64_VMS_COMCOD_ERROR:
3836 strcat (buf, ", error");
3837 break;
3838 case EF_IA_64_VMS_COMCOD_ABORT:
3839 strcat (buf, ", abort");
3840 break;
3841 default:
3842 warn (_("Unrecognised IA64 VMS Command Code: %x\n"),
3843 e_flags & EF_IA_64_VMS_COMCOD);
3844 strcat (buf, ", <unknown>");
3845 }
3846 }
3847 break;
3848
3849 case EM_VAX:
3850 if ((e_flags & EF_VAX_NONPIC))
3851 strcat (buf, ", non-PIC");
3852 if ((e_flags & EF_VAX_DFLOAT))
3853 strcat (buf, ", D-Float");
3854 if ((e_flags & EF_VAX_GFLOAT))
3855 strcat (buf, ", G-Float");
3856 break;
3857
3858 case EM_VISIUM:
3859 if (e_flags & EF_VISIUM_ARCH_MCM)
3860 strcat (buf, ", mcm");
3861 else if (e_flags & EF_VISIUM_ARCH_MCM24)
3862 strcat (buf, ", mcm24");
3863 if (e_flags & EF_VISIUM_ARCH_GR6)
3864 strcat (buf, ", gr6");
3865 break;
3866
3867 case EM_RL78:
3868 switch (e_flags & E_FLAG_RL78_CPU_MASK)
3869 {
3870 case E_FLAG_RL78_ANY_CPU: break;
3871 case E_FLAG_RL78_G10: strcat (buf, ", G10"); break;
3872 case E_FLAG_RL78_G13: strcat (buf, ", G13"); break;
3873 case E_FLAG_RL78_G14: strcat (buf, ", G14"); break;
3874 }
3875 if (e_flags & E_FLAG_RL78_64BIT_DOUBLES)
3876 strcat (buf, ", 64-bit doubles");
3877 break;
3878
3879 case EM_RX:
3880 if (e_flags & E_FLAG_RX_64BIT_DOUBLES)
3881 strcat (buf, ", 64-bit doubles");
3882 if (e_flags & E_FLAG_RX_DSP)
3883 strcat (buf, ", dsp");
3884 if (e_flags & E_FLAG_RX_PID)
3885 strcat (buf, ", pid");
3886 if (e_flags & E_FLAG_RX_ABI)
3887 strcat (buf, ", RX ABI");
3888 if (e_flags & E_FLAG_RX_SINSNS_SET)
3889 strcat (buf, e_flags & E_FLAG_RX_SINSNS_YES
3890 ? ", uses String instructions" : ", bans String instructions");
3891 if (e_flags & E_FLAG_RX_V2)
3892 strcat (buf, ", V2");
3893 if (e_flags & E_FLAG_RX_V3)
3894 strcat (buf, ", V3");
3895 break;
3896
3897 case EM_S390:
3898 if (e_flags & EF_S390_HIGH_GPRS)
3899 strcat (buf, ", highgprs");
3900 break;
3901
3902 case EM_TI_C6000:
3903 if ((e_flags & EF_C6000_REL))
3904 strcat (buf, ", relocatable module");
3905 break;
3906
3907 case EM_MSP430:
3908 strcat (buf, _(": architecture variant: "));
3909 switch (e_flags & EF_MSP430_MACH)
3910 {
3911 case E_MSP430_MACH_MSP430x11: strcat (buf, "MSP430x11"); break;
3912 case E_MSP430_MACH_MSP430x11x1 : strcat (buf, "MSP430x11x1 "); break;
3913 case E_MSP430_MACH_MSP430x12: strcat (buf, "MSP430x12"); break;
3914 case E_MSP430_MACH_MSP430x13: strcat (buf, "MSP430x13"); break;
3915 case E_MSP430_MACH_MSP430x14: strcat (buf, "MSP430x14"); break;
3916 case E_MSP430_MACH_MSP430x15: strcat (buf, "MSP430x15"); break;
3917 case E_MSP430_MACH_MSP430x16: strcat (buf, "MSP430x16"); break;
3918 case E_MSP430_MACH_MSP430x31: strcat (buf, "MSP430x31"); break;
3919 case E_MSP430_MACH_MSP430x32: strcat (buf, "MSP430x32"); break;
3920 case E_MSP430_MACH_MSP430x33: strcat (buf, "MSP430x33"); break;
3921 case E_MSP430_MACH_MSP430x41: strcat (buf, "MSP430x41"); break;
3922 case E_MSP430_MACH_MSP430x42: strcat (buf, "MSP430x42"); break;
3923 case E_MSP430_MACH_MSP430x43: strcat (buf, "MSP430x43"); break;
3924 case E_MSP430_MACH_MSP430x44: strcat (buf, "MSP430x44"); break;
3925 case E_MSP430_MACH_MSP430X : strcat (buf, "MSP430X"); break;
3926 default:
3927 strcat (buf, _(": unknown")); break;
3928 }
3929
3930 if (e_flags & ~ EF_MSP430_MACH)
3931 strcat (buf, _(": unknown extra flag bits also present"));
3932 break;
3933
3934 case EM_Z80:
3935 switch (e_flags & EF_Z80_MACH_MSK)
3936 {
3937 case EF_Z80_MACH_Z80: strcat (buf, ", Z80"); break;
3938 case EF_Z80_MACH_Z180: strcat (buf, ", Z180"); break;
3939 case EF_Z80_MACH_R800: strcat (buf, ", R800"); break;
3940 case EF_Z80_MACH_EZ80_Z80: strcat (buf, ", EZ80"); break;
3941 case EF_Z80_MACH_EZ80_ADL: strcat (buf, ", EZ80, ADL"); break;
3942 case EF_Z80_MACH_GBZ80: strcat (buf, ", GBZ80"); break;
3943 case EF_Z80_MACH_Z80N: strcat (buf, ", Z80N"); break;
3944 default:
3945 strcat (buf, _(", unknown")); break;
3946 }
3947 break;
3948 }
3949 }
3950
3951 return buf;
3952 }
3953
3954 static const char *
3955 get_osabi_name (Filedata * filedata, unsigned int osabi)
3956 {
3957 static char buff[32];
3958
3959 switch (osabi)
3960 {
3961 case ELFOSABI_NONE: return "UNIX - System V";
3962 case ELFOSABI_HPUX: return "UNIX - HP-UX";
3963 case ELFOSABI_NETBSD: return "UNIX - NetBSD";
3964 case ELFOSABI_GNU: return "UNIX - GNU";
3965 case ELFOSABI_SOLARIS: return "UNIX - Solaris";
3966 case ELFOSABI_AIX: return "UNIX - AIX";
3967 case ELFOSABI_IRIX: return "UNIX - IRIX";
3968 case ELFOSABI_FREEBSD: return "UNIX - FreeBSD";
3969 case ELFOSABI_TRU64: return "UNIX - TRU64";
3970 case ELFOSABI_MODESTO: return "Novell - Modesto";
3971 case ELFOSABI_OPENBSD: return "UNIX - OpenBSD";
3972 case ELFOSABI_OPENVMS: return "VMS - OpenVMS";
3973 case ELFOSABI_NSK: return "HP - Non-Stop Kernel";
3974 case ELFOSABI_AROS: return "AROS";
3975 case ELFOSABI_FENIXOS: return "FenixOS";
3976 case ELFOSABI_CLOUDABI: return "Nuxi CloudABI";
3977 case ELFOSABI_OPENVOS: return "Stratus Technologies OpenVOS";
3978 default:
3979 if (osabi >= 64)
3980 switch (filedata->file_header.e_machine)
3981 {
3982 case EM_ARM:
3983 switch (osabi)
3984 {
3985 case ELFOSABI_ARM: return "ARM";
3986 case ELFOSABI_ARM_FDPIC: return "ARM FDPIC";
3987 default:
3988 break;
3989 }
3990 break;
3991
3992 case EM_MSP430:
3993 case EM_MSP430_OLD:
3994 case EM_VISIUM:
3995 switch (osabi)
3996 {
3997 case ELFOSABI_STANDALONE: return _("Standalone App");
3998 default:
3999 break;
4000 }
4001 break;
4002
4003 case EM_TI_C6000:
4004 switch (osabi)
4005 {
4006 case ELFOSABI_C6000_ELFABI: return _("Bare-metal C6000");
4007 case ELFOSABI_C6000_LINUX: return "Linux C6000";
4008 default:
4009 break;
4010 }
4011 break;
4012
4013 default:
4014 break;
4015 }
4016 snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
4017 return buff;
4018 }
4019 }
4020
4021 static const char *
4022 get_aarch64_segment_type (unsigned long type)
4023 {
4024 switch (type)
4025 {
4026 case PT_AARCH64_ARCHEXT: return "AARCH64_ARCHEXT";
4027 default: return NULL;
4028 }
4029 }
4030
4031 static const char *
4032 get_arm_segment_type (unsigned long type)
4033 {
4034 switch (type)
4035 {
4036 case PT_ARM_EXIDX: return "EXIDX";
4037 default: return NULL;
4038 }
4039 }
4040
4041 static const char *
4042 get_s390_segment_type (unsigned long type)
4043 {
4044 switch (type)
4045 {
4046 case PT_S390_PGSTE: return "S390_PGSTE";
4047 default: return NULL;
4048 }
4049 }
4050
4051 static const char *
4052 get_mips_segment_type (unsigned long type)
4053 {
4054 switch (type)
4055 {
4056 case PT_MIPS_REGINFO: return "REGINFO";
4057 case PT_MIPS_RTPROC: return "RTPROC";
4058 case PT_MIPS_OPTIONS: return "OPTIONS";
4059 case PT_MIPS_ABIFLAGS: return "ABIFLAGS";
4060 default: return NULL;
4061 }
4062 }
4063
4064 static const char *
4065 get_parisc_segment_type (unsigned long type)
4066 {
4067 switch (type)
4068 {
4069 case PT_PARISC_ARCHEXT: return "PARISC_ARCHEXT";
4070 case PT_PARISC_UNWIND: return "PARISC_UNWIND";
4071 case PT_PARISC_WEAKORDER: return "PARISC_WEAKORDER";
4072 default: return NULL;
4073 }
4074 }
4075
4076 static const char *
4077 get_ia64_segment_type (unsigned long type)
4078 {
4079 switch (type)
4080 {
4081 case PT_IA_64_ARCHEXT: return "IA_64_ARCHEXT";
4082 case PT_IA_64_UNWIND: return "IA_64_UNWIND";
4083 default: return NULL;
4084 }
4085 }
4086
4087 static const char *
4088 get_tic6x_segment_type (unsigned long type)
4089 {
4090 switch (type)
4091 {
4092 case PT_C6000_PHATTR: return "C6000_PHATTR";
4093 default: return NULL;
4094 }
4095 }
4096
4097 static const char *
4098 get_hpux_segment_type (unsigned long type, unsigned e_machine)
4099 {
4100 if (e_machine == EM_PARISC)
4101 switch (type)
4102 {
4103 case PT_HP_TLS: return "HP_TLS";
4104 case PT_HP_CORE_NONE: return "HP_CORE_NONE";
4105 case PT_HP_CORE_VERSION: return "HP_CORE_VERSION";
4106 case PT_HP_CORE_KERNEL: return "HP_CORE_KERNEL";
4107 case PT_HP_CORE_COMM: return "HP_CORE_COMM";
4108 case PT_HP_CORE_PROC: return "HP_CORE_PROC";
4109 case PT_HP_CORE_LOADABLE: return "HP_CORE_LOADABLE";
4110 case PT_HP_CORE_STACK: return "HP_CORE_STACK";
4111 case PT_HP_CORE_SHM: return "HP_CORE_SHM";
4112 case PT_HP_CORE_MMF: return "HP_CORE_MMF";
4113 case PT_HP_PARALLEL: return "HP_PARALLEL";
4114 case PT_HP_FASTBIND: return "HP_FASTBIND";
4115 case PT_HP_OPT_ANNOT: return "HP_OPT_ANNOT";
4116 case PT_HP_HSL_ANNOT: return "HP_HSL_ANNOT";
4117 case PT_HP_STACK: return "HP_STACK";
4118 case PT_HP_CORE_UTSNAME: return "HP_CORE_UTSNAME";
4119 default: return NULL;
4120 }
4121
4122 if (e_machine == EM_IA_64)
4123 switch (type)
4124 {
4125 case PT_HP_TLS: return "HP_TLS";
4126 case PT_IA_64_HP_OPT_ANOT: return "HP_OPT_ANNOT";
4127 case PT_IA_64_HP_HSL_ANOT: return "HP_HSL_ANNOT";
4128 case PT_IA_64_HP_STACK: return "HP_STACK";
4129 default: return NULL;
4130 }
4131
4132 return NULL;
4133 }
4134
4135 static const char *
4136 get_solaris_segment_type (unsigned long type)
4137 {
4138 switch (type)
4139 {
4140 case 0x6464e550: return "PT_SUNW_UNWIND";
4141 case 0x6474e550: return "PT_SUNW_EH_FRAME";
4142 case 0x6ffffff7: return "PT_LOSUNW";
4143 case 0x6ffffffa: return "PT_SUNWBSS";
4144 case 0x6ffffffb: return "PT_SUNWSTACK";
4145 case 0x6ffffffc: return "PT_SUNWDTRACE";
4146 case 0x6ffffffd: return "PT_SUNWCAP";
4147 case 0x6fffffff: return "PT_HISUNW";
4148 default: return NULL;
4149 }
4150 }
4151
4152 static const char *
4153 get_segment_type (Filedata * filedata, unsigned long p_type)
4154 {
4155 static char buff[32];
4156
4157 switch (p_type)
4158 {
4159 case PT_NULL: return "NULL";
4160 case PT_LOAD: return "LOAD";
4161 case PT_DYNAMIC: return "DYNAMIC";
4162 case PT_INTERP: return "INTERP";
4163 case PT_NOTE: return "NOTE";
4164 case PT_SHLIB: return "SHLIB";
4165 case PT_PHDR: return "PHDR";
4166 case PT_TLS: return "TLS";
4167 case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
4168 case PT_GNU_STACK: return "GNU_STACK";
4169 case PT_GNU_RELRO: return "GNU_RELRO";
4170 case PT_GNU_PROPERTY: return "GNU_PROPERTY";
4171
4172 case PT_OPENBSD_RANDOMIZE: return "OPENBSD_RANDOMIZE";
4173 case PT_OPENBSD_WXNEEDED: return "OPENBSD_WXNEEDED";
4174 case PT_OPENBSD_BOOTDATA: return "OPENBSD_BOOTDATA";
4175
4176 default:
4177 if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
4178 {
4179 const char * result;
4180
4181 switch (filedata->file_header.e_machine)
4182 {
4183 case EM_AARCH64:
4184 result = get_aarch64_segment_type (p_type);
4185 break;
4186 case EM_ARM:
4187 result = get_arm_segment_type (p_type);
4188 break;
4189 case EM_MIPS:
4190 case EM_MIPS_RS3_LE:
4191 result = get_mips_segment_type (p_type);
4192 break;
4193 case EM_PARISC:
4194 result = get_parisc_segment_type (p_type);
4195 break;
4196 case EM_IA_64:
4197 result = get_ia64_segment_type (p_type);
4198 break;
4199 case EM_TI_C6000:
4200 result = get_tic6x_segment_type (p_type);
4201 break;
4202 case EM_S390:
4203 case EM_S390_OLD:
4204 result = get_s390_segment_type (p_type);
4205 break;
4206 default:
4207 result = NULL;
4208 break;
4209 }
4210
4211 if (result != NULL)
4212 return result;
4213
4214 sprintf (buff, "LOPROC+%#lx", p_type - PT_LOPROC);
4215 }
4216 else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
4217 {
4218 const char * result = NULL;
4219
4220 switch (filedata->file_header.e_ident[EI_OSABI])
4221 {
4222 case ELFOSABI_GNU:
4223 case ELFOSABI_FREEBSD:
4224 if (p_type >= PT_GNU_MBIND_LO && p_type <= PT_GNU_MBIND_HI)
4225 {
4226 sprintf (buff, "GNU_MBIND+%#lx", p_type - PT_GNU_MBIND_LO);
4227 result = buff;
4228 }
4229 break;
4230 case ELFOSABI_HPUX:
4231 result = get_hpux_segment_type (p_type,
4232 filedata->file_header.e_machine);
4233 break;
4234 case ELFOSABI_SOLARIS:
4235 result = get_solaris_segment_type (p_type);
4236 break;
4237 default:
4238 break;
4239 }
4240 if (result != NULL)
4241 return result;
4242
4243 sprintf (buff, "LOOS+%#lx", p_type - PT_LOOS);
4244 }
4245 else
4246 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
4247
4248 return buff;
4249 }
4250 }
4251
4252 static const char *
4253 get_arc_section_type_name (unsigned int sh_type)
4254 {
4255 switch (sh_type)
4256 {
4257 case SHT_ARC_ATTRIBUTES: return "ARC_ATTRIBUTES";
4258 default:
4259 break;
4260 }
4261 return NULL;
4262 }
4263
4264 static const char *
4265 get_mips_section_type_name (unsigned int sh_type)
4266 {
4267 switch (sh_type)
4268 {
4269 case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
4270 case SHT_MIPS_MSYM: return "MIPS_MSYM";
4271 case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
4272 case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
4273 case SHT_MIPS_UCODE: return "MIPS_UCODE";
4274 case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
4275 case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
4276 case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
4277 case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
4278 case SHT_MIPS_RELD: return "MIPS_RELD";
4279 case SHT_MIPS_IFACE: return "MIPS_IFACE";
4280 case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
4281 case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
4282 case SHT_MIPS_SHDR: return "MIPS_SHDR";
4283 case SHT_MIPS_FDESC: return "MIPS_FDESC";
4284 case SHT_MIPS_EXTSYM: return "MIPS_EXTSYM";
4285 case SHT_MIPS_DENSE: return "MIPS_DENSE";
4286 case SHT_MIPS_PDESC: return "MIPS_PDESC";
4287 case SHT_MIPS_LOCSYM: return "MIPS_LOCSYM";
4288 case SHT_MIPS_AUXSYM: return "MIPS_AUXSYM";
4289 case SHT_MIPS_OPTSYM: return "MIPS_OPTSYM";
4290 case SHT_MIPS_LOCSTR: return "MIPS_LOCSTR";
4291 case SHT_MIPS_LINE: return "MIPS_LINE";
4292 case SHT_MIPS_RFDESC: return "MIPS_RFDESC";
4293 case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
4294 case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
4295 case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
4296 case SHT_MIPS_DWARF: return "MIPS_DWARF";
4297 case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
4298 case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
4299 case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
4300 case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
4301 case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
4302 case SHT_MIPS_XLATE: return "MIPS_XLATE";
4303 case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
4304 case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
4305 case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
4306 case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
4307 case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
4308 case SHT_MIPS_ABIFLAGS: return "MIPS_ABIFLAGS";
4309 case SHT_MIPS_XHASH: return "MIPS_XHASH";
4310 default:
4311 break;
4312 }
4313 return NULL;
4314 }
4315
4316 static const char *
4317 get_parisc_section_type_name (unsigned int sh_type)
4318 {
4319 switch (sh_type)
4320 {
4321 case SHT_PARISC_EXT: return "PARISC_EXT";
4322 case SHT_PARISC_UNWIND: return "PARISC_UNWIND";
4323 case SHT_PARISC_DOC: return "PARISC_DOC";
4324 case SHT_PARISC_ANNOT: return "PARISC_ANNOT";
4325 case SHT_PARISC_SYMEXTN: return "PARISC_SYMEXTN";
4326 case SHT_PARISC_STUBS: return "PARISC_STUBS";
4327 case SHT_PARISC_DLKM: return "PARISC_DLKM";
4328 default: return NULL;
4329 }
4330 }
4331
4332 static const char *
4333 get_ia64_section_type_name (Filedata * filedata, unsigned int sh_type)
4334 {
4335 /* If the top 8 bits are 0x78 the next 8 are the os/abi ID. */
4336 if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
4337 return get_osabi_name (filedata, (sh_type & 0x00FF0000) >> 16);
4338
4339 switch (sh_type)
4340 {
4341 case SHT_IA_64_EXT: return "IA_64_EXT";
4342 case SHT_IA_64_UNWIND: return "IA_64_UNWIND";
4343 case SHT_IA_64_PRIORITY_INIT: return "IA_64_PRIORITY_INIT";
4344 case SHT_IA_64_VMS_TRACE: return "VMS_TRACE";
4345 case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
4346 case SHT_IA_64_VMS_DEBUG: return "VMS_DEBUG";
4347 case SHT_IA_64_VMS_DEBUG_STR: return "VMS_DEBUG_STR";
4348 case SHT_IA_64_VMS_LINKAGES: return "VMS_LINKAGES";
4349 case SHT_IA_64_VMS_SYMBOL_VECTOR: return "VMS_SYMBOL_VECTOR";
4350 case SHT_IA_64_VMS_FIXUP: return "VMS_FIXUP";
4351 default:
4352 break;
4353 }
4354 return NULL;
4355 }
4356
4357 static const char *
4358 get_x86_64_section_type_name (unsigned int sh_type)
4359 {
4360 switch (sh_type)
4361 {
4362 case SHT_X86_64_UNWIND: return "X86_64_UNWIND";
4363 default: return NULL;
4364 }
4365 }
4366
4367 static const char *
4368 get_aarch64_section_type_name (unsigned int sh_type)
4369 {
4370 switch (sh_type)
4371 {
4372 case SHT_AARCH64_ATTRIBUTES: return "AARCH64_ATTRIBUTES";
4373 default: return NULL;
4374 }
4375 }
4376
4377 static const char *
4378 get_arm_section_type_name (unsigned int sh_type)
4379 {
4380 switch (sh_type)
4381 {
4382 case SHT_ARM_EXIDX: return "ARM_EXIDX";
4383 case SHT_ARM_PREEMPTMAP: return "ARM_PREEMPTMAP";
4384 case SHT_ARM_ATTRIBUTES: return "ARM_ATTRIBUTES";
4385 case SHT_ARM_DEBUGOVERLAY: return "ARM_DEBUGOVERLAY";
4386 case SHT_ARM_OVERLAYSECTION: return "ARM_OVERLAYSECTION";
4387 default: return NULL;
4388 }
4389 }
4390
4391 static const char *
4392 get_tic6x_section_type_name (unsigned int sh_type)
4393 {
4394 switch (sh_type)
4395 {
4396 case SHT_C6000_UNWIND: return "C6000_UNWIND";
4397 case SHT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
4398 case SHT_C6000_ATTRIBUTES: return "C6000_ATTRIBUTES";
4399 case SHT_TI_ICODE: return "TI_ICODE";
4400 case SHT_TI_XREF: return "TI_XREF";
4401 case SHT_TI_HANDLER: return "TI_HANDLER";
4402 case SHT_TI_INITINFO: return "TI_INITINFO";
4403 case SHT_TI_PHATTRS: return "TI_PHATTRS";
4404 default: return NULL;
4405 }
4406 }
4407
4408 static const char *
4409 get_msp430_section_type_name (unsigned int sh_type)
4410 {
4411 switch (sh_type)
4412 {
4413 case SHT_MSP430_SEC_FLAGS: return "MSP430_SEC_FLAGS";
4414 case SHT_MSP430_SYM_ALIASES: return "MSP430_SYM_ALIASES";
4415 case SHT_MSP430_ATTRIBUTES: return "MSP430_ATTRIBUTES";
4416 default: return NULL;
4417 }
4418 }
4419
4420 static const char *
4421 get_nfp_section_type_name (unsigned int sh_type)
4422 {
4423 switch (sh_type)
4424 {
4425 case SHT_NFP_MECONFIG: return "NFP_MECONFIG";
4426 case SHT_NFP_INITREG: return "NFP_INITREG";
4427 case SHT_NFP_UDEBUG: return "NFP_UDEBUG";
4428 default: return NULL;
4429 }
4430 }
4431
4432 static const char *
4433 get_v850_section_type_name (unsigned int sh_type)
4434 {
4435 switch (sh_type)
4436 {
4437 case SHT_V850_SCOMMON: return "V850 Small Common";
4438 case SHT_V850_TCOMMON: return "V850 Tiny Common";
4439 case SHT_V850_ZCOMMON: return "V850 Zero Common";
4440 case SHT_RENESAS_IOP: return "RENESAS IOP";
4441 case SHT_RENESAS_INFO: return "RENESAS INFO";
4442 default: return NULL;
4443 }
4444 }
4445
4446 static const char *
4447 get_riscv_section_type_name (unsigned int sh_type)
4448 {
4449 switch (sh_type)
4450 {
4451 case SHT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
4452 default: return NULL;
4453 }
4454 }
4455
4456 static const char *
4457 get_csky_section_type_name (unsigned int sh_type)
4458 {
4459 switch (sh_type)
4460 {
4461 case SHT_CSKY_ATTRIBUTES: return "CSKY_ATTRIBUTES";
4462 default: return NULL;
4463 }
4464 }
4465
4466 static const char *
4467 get_section_type_name (Filedata * filedata, unsigned int sh_type)
4468 {
4469 static char buff[32];
4470 const char * result;
4471
4472 switch (sh_type)
4473 {
4474 case SHT_NULL: return "NULL";
4475 case SHT_PROGBITS: return "PROGBITS";
4476 case SHT_SYMTAB: return "SYMTAB";
4477 case SHT_STRTAB: return "STRTAB";
4478 case SHT_RELA: return "RELA";
4479 case SHT_HASH: return "HASH";
4480 case SHT_DYNAMIC: return "DYNAMIC";
4481 case SHT_NOTE: return "NOTE";
4482 case SHT_NOBITS: return "NOBITS";
4483 case SHT_REL: return "REL";
4484 case SHT_SHLIB: return "SHLIB";
4485 case SHT_DYNSYM: return "DYNSYM";
4486 case SHT_INIT_ARRAY: return "INIT_ARRAY";
4487 case SHT_FINI_ARRAY: return "FINI_ARRAY";
4488 case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
4489 case SHT_GNU_HASH: return "GNU_HASH";
4490 case SHT_GROUP: return "GROUP";
4491 case SHT_SYMTAB_SHNDX: return "SYMTAB SECTION INDICES";
4492 case SHT_GNU_verdef: return "VERDEF";
4493 case SHT_GNU_verneed: return "VERNEED";
4494 case SHT_GNU_versym: return "VERSYM";
4495 case 0x6ffffff0: return "VERSYM";
4496 case 0x6ffffffc: return "VERDEF";
4497 case 0x7ffffffd: return "AUXILIARY";
4498 case 0x7fffffff: return "FILTER";
4499 case SHT_GNU_LIBLIST: return "GNU_LIBLIST";
4500
4501 default:
4502 if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
4503 {
4504 switch (filedata->file_header.e_machine)
4505 {
4506 case EM_ARC:
4507 case EM_ARC_COMPACT:
4508 case EM_ARC_COMPACT2:
4509 result = get_arc_section_type_name (sh_type);
4510 break;
4511 case EM_MIPS:
4512 case EM_MIPS_RS3_LE:
4513 result = get_mips_section_type_name (sh_type);
4514 break;
4515 case EM_PARISC:
4516 result = get_parisc_section_type_name (sh_type);
4517 break;
4518 case EM_IA_64:
4519 result = get_ia64_section_type_name (filedata, sh_type);
4520 break;
4521 case EM_X86_64:
4522 case EM_L1OM:
4523 case EM_K1OM:
4524 result = get_x86_64_section_type_name (sh_type);
4525 break;
4526 case EM_AARCH64:
4527 result = get_aarch64_section_type_name (sh_type);
4528 break;
4529 case EM_ARM:
4530 result = get_arm_section_type_name (sh_type);
4531 break;
4532 case EM_TI_C6000:
4533 result = get_tic6x_section_type_name (sh_type);
4534 break;
4535 case EM_MSP430:
4536 result = get_msp430_section_type_name (sh_type);
4537 break;
4538 case EM_NFP:
4539 result = get_nfp_section_type_name (sh_type);
4540 break;
4541 case EM_V800:
4542 case EM_V850:
4543 case EM_CYGNUS_V850:
4544 result = get_v850_section_type_name (sh_type);
4545 break;
4546 case EM_RISCV:
4547 result = get_riscv_section_type_name (sh_type);
4548 break;
4549 case EM_CSKY:
4550 result = get_csky_section_type_name (sh_type);
4551 break;
4552 default:
4553 result = NULL;
4554 break;
4555 }
4556
4557 if (result != NULL)
4558 return result;
4559
4560 sprintf (buff, "LOPROC+%#x", sh_type - SHT_LOPROC);
4561 }
4562 else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
4563 {
4564 switch (filedata->file_header.e_machine)
4565 {
4566 case EM_IA_64:
4567 result = get_ia64_section_type_name (filedata, sh_type);
4568 break;
4569 default:
4570 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
4571 result = get_solaris_section_type (sh_type);
4572 else
4573 {
4574 switch (sh_type)
4575 {
4576 case SHT_GNU_INCREMENTAL_INPUTS: result = "GNU_INCREMENTAL_INPUTS"; break;
4577 case SHT_GNU_ATTRIBUTES: result = "GNU_ATTRIBUTES"; break;
4578 case SHT_GNU_HASH: result = "GNU_HASH"; break;
4579 case SHT_GNU_LIBLIST: result = "GNU_LIBLIST"; break;
4580 default:
4581 result = NULL;
4582 break;
4583 }
4584 }
4585 break;
4586 }
4587
4588 if (result != NULL)
4589 return result;
4590
4591 sprintf (buff, "LOOS+%#x", sh_type - SHT_LOOS);
4592 }
4593 else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
4594 {
4595 switch (filedata->file_header.e_machine)
4596 {
4597 case EM_V800:
4598 case EM_V850:
4599 case EM_CYGNUS_V850:
4600 result = get_v850_section_type_name (sh_type);
4601 break;
4602 default:
4603 result = NULL;
4604 break;
4605 }
4606
4607 if (result != NULL)
4608 return result;
4609
4610 sprintf (buff, "LOUSER+%#x", sh_type - SHT_LOUSER);
4611 }
4612 else
4613 /* This message is probably going to be displayed in a 15
4614 character wide field, so put the hex value first. */
4615 snprintf (buff, sizeof (buff), _("%08x: <unknown>"), sh_type);
4616
4617 return buff;
4618 }
4619 }
4620
4621 enum long_option_values
4622 {
4623 OPTION_DEBUG_DUMP = 512,
4624 OPTION_DYN_SYMS,
4625 OPTION_LTO_SYMS,
4626 OPTION_DWARF_DEPTH,
4627 OPTION_DWARF_START,
4628 OPTION_DWARF_CHECK,
4629 OPTION_CTF_DUMP,
4630 OPTION_CTF_PARENT,
4631 OPTION_CTF_SYMBOLS,
4632 OPTION_CTF_STRINGS,
4633 OPTION_WITH_SYMBOL_VERSIONS,
4634 OPTION_RECURSE_LIMIT,
4635 OPTION_NO_RECURSE_LIMIT,
4636 OPTION_NO_DEMANGLING,
4637 OPTION_SYM_BASE
4638 };
4639
4640 static struct option options[] =
4641 {
4642 /* Note - This table is alpha-sorted on the 'val'
4643 field in order to make adding new options easier. */
4644 {"arch-specific", no_argument, 0, 'A'},
4645 {"all", no_argument, 0, 'a'},
4646 {"demangle", optional_argument, 0, 'C'},
4647 {"archive-index", no_argument, 0, 'c'},
4648 {"use-dynamic", no_argument, 0, 'D'},
4649 {"dynamic", no_argument, 0, 'd'},
4650 {"headers", no_argument, 0, 'e'},
4651 {"section-groups", no_argument, 0, 'g'},
4652 {"help", no_argument, 0, 'H'},
4653 {"file-header", no_argument, 0, 'h'},
4654 {"histogram", no_argument, 0, 'I'},
4655 {"lint", no_argument, 0, 'L'},
4656 {"enable-checks", no_argument, 0, 'L'},
4657 {"program-headers", no_argument, 0, 'l'},
4658 {"segments", no_argument, 0, 'l'},
4659 {"full-section-name",no_argument, 0, 'N'},
4660 {"notes", no_argument, 0, 'n'},
4661 {"process-links", no_argument, 0, 'P'},
4662 {"string-dump", required_argument, 0, 'p'},
4663 {"relocated-dump", required_argument, 0, 'R'},
4664 {"relocs", no_argument, 0, 'r'},
4665 {"section-headers", no_argument, 0, 'S'},
4666 {"sections", no_argument, 0, 'S'},
4667 {"symbols", no_argument, 0, 's'},
4668 {"syms", no_argument, 0, 's'},
4669 {"silent-truncation",no_argument, 0, 'T'},
4670 {"section-details", no_argument, 0, 't'},
4671 {"unwind", no_argument, 0, 'u'},
4672 {"version-info", no_argument, 0, 'V'},
4673 {"version", no_argument, 0, 'v'},
4674 {"wide", no_argument, 0, 'W'},
4675 {"hex-dump", required_argument, 0, 'x'},
4676 {"decompress", no_argument, 0, 'z'},
4677
4678 {"no-demangle", no_argument, 0, OPTION_NO_DEMANGLING},
4679 {"recurse-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
4680 {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
4681 {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
4682 {"dyn-syms", no_argument, 0, OPTION_DYN_SYMS},
4683 {"lto-syms", no_argument, 0, OPTION_LTO_SYMS},
4684 {"debug-dump", optional_argument, 0, OPTION_DEBUG_DUMP},
4685 {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
4686 {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
4687 {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
4688 #ifdef ENABLE_LIBCTF
4689 {"ctf", required_argument, 0, OPTION_CTF_DUMP},
4690 {"ctf-symbols", required_argument, 0, OPTION_CTF_SYMBOLS},
4691 {"ctf-strings", required_argument, 0, OPTION_CTF_STRINGS},
4692 {"ctf-parent", required_argument, 0, OPTION_CTF_PARENT},
4693 #endif
4694 {"sym-base", optional_argument, 0, OPTION_SYM_BASE},
4695
4696 {0, no_argument, 0, 0}
4697 };
4698
4699 static void
4700 usage (FILE * stream)
4701 {
4702 fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
4703 fprintf (stream, _(" Display information about the contents of ELF format files\n"));
4704 fprintf (stream, _(" Options are:\n"));
4705 fprintf (stream, _("\
4706 -a --all Equivalent to: -h -l -S -s -r -d -V -A -I\n"));
4707 fprintf (stream, _("\
4708 -h --file-header Display the ELF file header\n"));
4709 fprintf (stream, _("\
4710 -l --program-headers Display the program headers\n"));
4711 fprintf (stream, _("\
4712 --segments An alias for --program-headers\n"));
4713 fprintf (stream, _("\
4714 -S --section-headers Display the sections' header\n"));
4715 fprintf (stream, _("\
4716 --sections An alias for --section-headers\n"));
4717 fprintf (stream, _("\
4718 -g --section-groups Display the section groups\n"));
4719 fprintf (stream, _("\
4720 -t --section-details Display the section details\n"));
4721 fprintf (stream, _("\
4722 -e --headers Equivalent to: -h -l -S\n"));
4723 fprintf (stream, _("\
4724 -s --syms Display the symbol table\n"));
4725 fprintf (stream, _("\
4726 --symbols An alias for --syms\n"));
4727 fprintf (stream, _("\
4728 --dyn-syms Display the dynamic symbol table\n"));
4729 fprintf (stream, _("\
4730 --lto-syms Display LTO symbol tables\n"));
4731 fprintf (stream, _("\
4732 --sym-base=[0|8|10|16] \n\
4733 Force base for symbol sizes. The options are \n\
4734 mixed (the default), octal, decimal, hexadecimal.\n"));
4735 fprintf (stream, _("\
4736 -C --demangle[=STYLE] Decode low-level symbol names into user-level names\n\
4737 The STYLE, if specified, can be `auto' (the default),\n\
4738 `gnu', `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
4739 or `gnat'\n"));
4740 fprintf (stream, _("\
4741 --no-demangle Do not demangle low-level symbol names. (default)\n"));
4742 fprintf (stream, _("\
4743 --recurse-limit Enable a demangling recursion limit. (default)\n"));
4744 fprintf (stream, _("\
4745 --no-recurse-limit Disable a demangling recursion limit\n"));
4746 fprintf (stream, _("\
4747 -n --notes Display the core notes (if present)\n"));
4748 fprintf (stream, _("\
4749 -r --relocs Display the relocations (if present)\n"));
4750 fprintf (stream, _("\
4751 -u --unwind Display the unwind info (if present)\n"));
4752 fprintf (stream, _("\
4753 -d --dynamic Display the dynamic section (if present)\n"));
4754 fprintf (stream, _("\
4755 -V --version-info Display the version sections (if present)\n"));
4756 fprintf (stream, _("\
4757 -A --arch-specific Display architecture specific information (if any)\n"));
4758 fprintf (stream, _("\
4759 -c --archive-index Display the symbol/file index in an archive\n"));
4760 fprintf (stream, _("\
4761 -D --use-dynamic Use the dynamic section info when displaying symbols\n"));
4762 fprintf (stream, _("\
4763 -L --lint|--enable-checks\n\
4764 Display warning messages for possible problems\n"));
4765 fprintf (stream, _("\
4766 -x --hex-dump=<number|name>\n\
4767 Dump the contents of section <number|name> as bytes\n"));
4768 fprintf (stream, _("\
4769 -p --string-dump=<number|name>\n\
4770 Dump the contents of section <number|name> as strings\n"));
4771 fprintf (stream, _("\
4772 -R --relocated-dump=<number|name>\n\
4773 Dump the relocated contents of section <number|name>\n"));
4774 fprintf (stream, _("\
4775 -z --decompress Decompress section before dumping it\n"));
4776 fprintf (stream, _("\
4777 -w --debug-dump[a/=abbrev, A/=addr, r/=aranges, c/=cu_index, L/=decodedline,\n\
4778 f/=frames, F/=frames-interp, g/=gdb_index, i/=info, o/=loc,\n\
4779 m/=macro, p/=pubnames, t/=pubtypes, R/=Ranges, l/=rawline,\n\
4780 s/=str, O/=str-offsets, u/=trace_abbrev, T/=trace_aranges,\n\
4781 U/=trace_info]\n\
4782 Display the contents of DWARF debug sections\n"));
4783 fprintf (stream, _("\
4784 -wk --debug-dump=links Display the contents of sections that link to separate\n\
4785 debuginfo files\n"));
4786 fprintf (stream, _("\
4787 -P --process-links Display the contents of non-debug sections in separate\n\
4788 debuginfo files. (Implies -wK)\n"));
4789 #if DEFAULT_FOR_FOLLOW_LINKS
4790 fprintf (stream, _("\
4791 -wK --debug-dump=follow-links\n\
4792 Follow links to separate debug info files (default)\n"));
4793 fprintf (stream, _("\
4794 -wN --debug-dump=no-follow-links\n\
4795 Do not follow links to separate debug info files\n"));
4796 #else
4797 fprintf (stream, _("\
4798 -wK --debug-dump=follow-links\n\
4799 Follow links to separate debug info files\n"));
4800 fprintf (stream, _("\
4801 -wN --debug-dump=no-follow-links\n\
4802 Do not follow links to separate debug info files\n\
4803 (default)\n"));
4804 #endif
4805 fprintf (stream, _("\
4806 --dwarf-depth=N Do not display DIEs at depth N or greater\n"));
4807 fprintf (stream, _("\
4808 --dwarf-start=N Display DIEs starting at offset N\n"));
4809 #ifdef ENABLE_LIBCTF
4810 fprintf (stream, _("\
4811 --ctf=<number|name> Display CTF info from section <number|name>\n"));
4812 fprintf (stream, _("\
4813 --ctf-parent=<number|name>\n\
4814 Use section <number|name> as the CTF parent\n"));
4815 fprintf (stream, _("\
4816 --ctf-symbols=<number|name>\n\
4817 Use section <number|name> as the CTF external symtab\n"));
4818 fprintf (stream, _("\
4819 --ctf-strings=<number|name>\n\
4820 Use section <number|name> as the CTF external strtab\n"));
4821 #endif
4822
4823 #ifdef SUPPORT_DISASSEMBLY
4824 fprintf (stream, _("\
4825 -i --instruction-dump=<number|name>\n\
4826 Disassemble the contents of section <number|name>\n"));
4827 #endif
4828 fprintf (stream, _("\
4829 -I --histogram Display histogram of bucket list lengths\n"));
4830 fprintf (stream, _("\
4831 -W --wide Allow output width to exceed 80 characters\n"));
4832 fprintf (stream, _("\
4833 -T --silent-truncation If a symbol name is truncated, do not add [...] suffix\n"));
4834 fprintf (stream, _("\
4835 @<file> Read options from <file>\n"));
4836 fprintf (stream, _("\
4837 -H --help Display this information\n"));
4838 fprintf (stream, _("\
4839 -v --version Display the version number of readelf\n"));
4840
4841 if (REPORT_BUGS_TO[0] && stream == stdout)
4842 fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
4843
4844 exit (stream == stdout ? 0 : 1);
4845 }
4846
4847 /* Record the fact that the user wants the contents of section number
4848 SECTION to be displayed using the method(s) encoded as flags bits
4849 in TYPE. Note, TYPE can be zero if we are creating the array for
4850 the first time. */
4851
4852 static void
4853 request_dump_bynumber (struct dump_data *dumpdata,
4854 unsigned int section, dump_type type)
4855 {
4856 if (section >= dumpdata->num_dump_sects)
4857 {
4858 dump_type * new_dump_sects;
4859
4860 new_dump_sects = (dump_type *) calloc (section + 1,
4861 sizeof (* new_dump_sects));
4862
4863 if (new_dump_sects == NULL)
4864 error (_("Out of memory allocating dump request table.\n"));
4865 else
4866 {
4867 if (dumpdata->dump_sects)
4868 {
4869 /* Copy current flag settings. */
4870 memcpy (new_dump_sects, dumpdata->dump_sects,
4871 dumpdata->num_dump_sects * sizeof (* new_dump_sects));
4872
4873 free (dumpdata->dump_sects);
4874 }
4875
4876 dumpdata->dump_sects = new_dump_sects;
4877 dumpdata->num_dump_sects = section + 1;
4878 }
4879 }
4880
4881 if (dumpdata->dump_sects)
4882 dumpdata->dump_sects[section] |= type;
4883 }
4884
4885 /* Request a dump by section name. */
4886
4887 static void
4888 request_dump_byname (const char * section, dump_type type)
4889 {
4890 struct dump_list_entry * new_request;
4891
4892 new_request = (struct dump_list_entry *)
4893 malloc (sizeof (struct dump_list_entry));
4894 if (!new_request)
4895 error (_("Out of memory allocating dump request table.\n"));
4896
4897 new_request->name = strdup (section);
4898 if (!new_request->name)
4899 error (_("Out of memory allocating dump request table.\n"));
4900
4901 new_request->type = type;
4902
4903 new_request->next = dump_sects_byname;
4904 dump_sects_byname = new_request;
4905 }
4906
4907 static inline void
4908 request_dump (struct dump_data *dumpdata, dump_type type)
4909 {
4910 int section;
4911 char * cp;
4912
4913 do_dump = true;
4914 section = strtoul (optarg, & cp, 0);
4915
4916 if (! *cp && section >= 0)
4917 request_dump_bynumber (dumpdata, section, type);
4918 else
4919 request_dump_byname (optarg, type);
4920 }
4921
4922 static void
4923 parse_args (struct dump_data *dumpdata, int argc, char ** argv)
4924 {
4925 int c;
4926
4927 if (argc < 2)
4928 usage (stderr);
4929
4930 while ((c = getopt_long
4931 (argc, argv, "ACDHILNPR:STVWacdeghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
4932 {
4933 switch (c)
4934 {
4935 case 0:
4936 /* Long options. */
4937 break;
4938 case 'H':
4939 usage (stdout);
4940 break;
4941
4942 case 'a':
4943 do_syms = true;
4944 do_reloc = true;
4945 do_unwind = true;
4946 do_dynamic = true;
4947 do_header = true;
4948 do_sections = true;
4949 do_section_groups = true;
4950 do_segments = true;
4951 do_version = true;
4952 do_histogram = true;
4953 do_arch = true;
4954 do_notes = true;
4955 break;
4956
4957 case 'g':
4958 do_section_groups = true;
4959 break;
4960 case 't':
4961 case 'N':
4962 do_sections = true;
4963 do_section_details = true;
4964 break;
4965 case 'e':
4966 do_header = true;
4967 do_sections = true;
4968 do_segments = true;
4969 break;
4970 case 'A':
4971 do_arch = true;
4972 break;
4973 case 'D':
4974 do_using_dynamic = true;
4975 break;
4976 case 'r':
4977 do_reloc = true;
4978 break;
4979 case 'u':
4980 do_unwind = true;
4981 break;
4982 case 'h':
4983 do_header = true;
4984 break;
4985 case 'l':
4986 do_segments = true;
4987 break;
4988 case 's':
4989 do_syms = true;
4990 break;
4991 case 'S':
4992 do_sections = true;
4993 break;
4994 case 'd':
4995 do_dynamic = true;
4996 break;
4997 case 'I':
4998 do_histogram = true;
4999 break;
5000 case 'n':
5001 do_notes = true;
5002 break;
5003 case 'c':
5004 do_archive_index = true;
5005 break;
5006 case 'L':
5007 do_checks = true;
5008 break;
5009 case 'P':
5010 process_links = true;
5011 do_follow_links = true;
5012 break;
5013 case 'x':
5014 request_dump (dumpdata, HEX_DUMP);
5015 break;
5016 case 'p':
5017 request_dump (dumpdata, STRING_DUMP);
5018 break;
5019 case 'R':
5020 request_dump (dumpdata, RELOC_DUMP);
5021 break;
5022 case 'z':
5023 decompress_dumps = true;
5024 break;
5025 case 'w':
5026 do_dump = true;
5027 if (optarg == NULL)
5028 {
5029 do_debugging = true;
5030 dwarf_select_sections_all ();
5031 }
5032 else
5033 {
5034 do_debugging = false;
5035 dwarf_select_sections_by_letters (optarg);
5036 }
5037 break;
5038 case OPTION_DEBUG_DUMP:
5039 do_dump = true;
5040 if (optarg == NULL)
5041 {
5042 do_debugging = true;
5043 dwarf_select_sections_all ();
5044 }
5045 else
5046 {
5047 do_debugging = false;
5048 dwarf_select_sections_by_names (optarg);
5049 }
5050 break;
5051 case OPTION_DWARF_DEPTH:
5052 {
5053 char *cp;
5054
5055 dwarf_cutoff_level = strtoul (optarg, & cp, 0);
5056 }
5057 break;
5058 case OPTION_DWARF_START:
5059 {
5060 char *cp;
5061
5062 dwarf_start_die = strtoul (optarg, & cp, 0);
5063 }
5064 break;
5065 case OPTION_DWARF_CHECK:
5066 dwarf_check = true;
5067 break;
5068 case OPTION_CTF_DUMP:
5069 do_ctf = true;
5070 request_dump (dumpdata, CTF_DUMP);
5071 break;
5072 case OPTION_CTF_SYMBOLS:
5073 free (dump_ctf_symtab_name);
5074 dump_ctf_symtab_name = strdup (optarg);
5075 break;
5076 case OPTION_CTF_STRINGS:
5077 free (dump_ctf_strtab_name);
5078 dump_ctf_strtab_name = strdup (optarg);
5079 break;
5080 case OPTION_CTF_PARENT:
5081 free (dump_ctf_parent_name);
5082 dump_ctf_parent_name = strdup (optarg);
5083 break;
5084 case OPTION_DYN_SYMS:
5085 do_dyn_syms = true;
5086 break;
5087 case OPTION_LTO_SYMS:
5088 do_lto_syms = true;
5089 break;
5090 #ifdef SUPPORT_DISASSEMBLY
5091 case 'i':
5092 request_dump (dumpdata, DISASS_DUMP);
5093 break;
5094 #endif
5095 case 'v':
5096 print_version (program_name);
5097 break;
5098 case 'V':
5099 do_version = true;
5100 break;
5101 case 'W':
5102 do_wide = true;
5103 break;
5104 case 'T':
5105 do_not_show_symbol_truncation = true;
5106 break;
5107 case 'C':
5108 do_demangle = true;
5109 if (optarg != NULL)
5110 {
5111 enum demangling_styles style;
5112
5113 style = cplus_demangle_name_to_style (optarg);
5114 if (style == unknown_demangling)
5115 error (_("unknown demangling style `%s'"), optarg);
5116
5117 cplus_demangle_set_style (style);
5118 }
5119 break;
5120 case OPTION_NO_DEMANGLING:
5121 do_demangle = false;
5122 break;
5123 case OPTION_RECURSE_LIMIT:
5124 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
5125 break;
5126 case OPTION_NO_RECURSE_LIMIT:
5127 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
5128 break;
5129 case OPTION_WITH_SYMBOL_VERSIONS:
5130 /* Ignored for backward compatibility. */
5131 break;
5132
5133 case OPTION_SYM_BASE:
5134 sym_base = 0;
5135 if (optarg != NULL)
5136 {
5137 sym_base = strtoul (optarg, NULL, 0);
5138 switch (sym_base)
5139 {
5140 case 0:
5141 case 8:
5142 case 10:
5143 case 16:
5144 break;
5145
5146 default:
5147 sym_base = 0;
5148 break;
5149 }
5150 }
5151 break;
5152
5153 default:
5154 /* xgettext:c-format */
5155 error (_("Invalid option '-%c'\n"), c);
5156 /* Fall through. */
5157 case '?':
5158 usage (stderr);
5159 }
5160 }
5161
5162 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
5163 && !do_segments && !do_header && !do_dump && !do_version
5164 && !do_histogram && !do_debugging && !do_arch && !do_notes
5165 && !do_section_groups && !do_archive_index
5166 && !do_dyn_syms && !do_lto_syms)
5167 {
5168 if (do_checks)
5169 {
5170 check_all = true;
5171 do_dynamic = do_syms = do_reloc = do_unwind = do_sections = true;
5172 do_segments = do_header = do_dump = do_version = true;
5173 do_histogram = do_debugging = do_arch = do_notes = true;
5174 do_section_groups = do_archive_index = do_dyn_syms = true;
5175 do_lto_syms = true;
5176 }
5177 else
5178 usage (stderr);
5179 }
5180 }
5181
5182 static const char *
5183 get_elf_class (unsigned int elf_class)
5184 {
5185 static char buff[32];
5186
5187 switch (elf_class)
5188 {
5189 case ELFCLASSNONE: return _("none");
5190 case ELFCLASS32: return "ELF32";
5191 case ELFCLASS64: return "ELF64";
5192 default:
5193 snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
5194 return buff;
5195 }
5196 }
5197
5198 static const char *
5199 get_data_encoding (unsigned int encoding)
5200 {
5201 static char buff[32];
5202
5203 switch (encoding)
5204 {
5205 case ELFDATANONE: return _("none");
5206 case ELFDATA2LSB: return _("2's complement, little endian");
5207 case ELFDATA2MSB: return _("2's complement, big endian");
5208 default:
5209 snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
5210 return buff;
5211 }
5212 }
5213
5214 /* Decode the data held in 'filedata->file_header'. */
5215
5216 static bool
5217 process_file_header (Filedata * filedata)
5218 {
5219 Elf_Internal_Ehdr * header = & filedata->file_header;
5220
5221 if ( header->e_ident[EI_MAG0] != ELFMAG0
5222 || header->e_ident[EI_MAG1] != ELFMAG1
5223 || header->e_ident[EI_MAG2] != ELFMAG2
5224 || header->e_ident[EI_MAG3] != ELFMAG3)
5225 {
5226 error
5227 (_("Not an ELF file - it has the wrong magic bytes at the start\n"));
5228 return false;
5229 }
5230
5231 if (! filedata->is_separate)
5232 init_dwarf_regnames_by_elf_machine_code (header->e_machine);
5233
5234 if (do_header)
5235 {
5236 unsigned i;
5237
5238 if (filedata->is_separate)
5239 printf (_("ELF Header in linked file '%s':\n"), filedata->file_name);
5240 else
5241 printf (_("ELF Header:\n"));
5242 printf (_(" Magic: "));
5243 for (i = 0; i < EI_NIDENT; i++)
5244 printf ("%2.2x ", header->e_ident[i]);
5245 printf ("\n");
5246 printf (_(" Class: %s\n"),
5247 get_elf_class (header->e_ident[EI_CLASS]));
5248 printf (_(" Data: %s\n"),
5249 get_data_encoding (header->e_ident[EI_DATA]));
5250 printf (_(" Version: %d%s\n"),
5251 header->e_ident[EI_VERSION],
5252 (header->e_ident[EI_VERSION] == EV_CURRENT
5253 ? _(" (current)")
5254 : (header->e_ident[EI_VERSION] != EV_NONE
5255 ? _(" <unknown>")
5256 : "")));
5257 printf (_(" OS/ABI: %s\n"),
5258 get_osabi_name (filedata, header->e_ident[EI_OSABI]));
5259 printf (_(" ABI Version: %d\n"),
5260 header->e_ident[EI_ABIVERSION]);
5261 printf (_(" Type: %s\n"),
5262 get_file_type (filedata));
5263 printf (_(" Machine: %s\n"),
5264 get_machine_name (header->e_machine));
5265 printf (_(" Version: 0x%lx\n"),
5266 header->e_version);
5267
5268 printf (_(" Entry point address: "));
5269 print_vma (header->e_entry, PREFIX_HEX);
5270 printf (_("\n Start of program headers: "));
5271 print_vma (header->e_phoff, DEC);
5272 printf (_(" (bytes into file)\n Start of section headers: "));
5273 print_vma (header->e_shoff, DEC);
5274 printf (_(" (bytes into file)\n"));
5275
5276 printf (_(" Flags: 0x%lx%s\n"),
5277 header->e_flags,
5278 get_machine_flags (filedata, header->e_flags, header->e_machine));
5279 printf (_(" Size of this header: %u (bytes)\n"),
5280 header->e_ehsize);
5281 printf (_(" Size of program headers: %u (bytes)\n"),
5282 header->e_phentsize);
5283 printf (_(" Number of program headers: %u"),
5284 header->e_phnum);
5285 if (filedata->section_headers != NULL
5286 && header->e_phnum == PN_XNUM
5287 && filedata->section_headers[0].sh_info != 0)
5288 {
5289 header->e_phnum = filedata->section_headers[0].sh_info;
5290 printf (" (%u)", header->e_phnum);
5291 }
5292 putc ('\n', stdout);
5293 printf (_(" Size of section headers: %u (bytes)\n"),
5294 header->e_shentsize);
5295 printf (_(" Number of section headers: %u"),
5296 header->e_shnum);
5297 if (filedata->section_headers != NULL && header->e_shnum == SHN_UNDEF)
5298 {
5299 header->e_shnum = filedata->section_headers[0].sh_size;
5300 printf (" (%u)", header->e_shnum);
5301 }
5302 putc ('\n', stdout);
5303 printf (_(" Section header string table index: %u"),
5304 header->e_shstrndx);
5305 if (filedata->section_headers != NULL
5306 && header->e_shstrndx == (SHN_XINDEX & 0xffff))
5307 {
5308 header->e_shstrndx = filedata->section_headers[0].sh_link;
5309 printf (" (%u)", header->e_shstrndx);
5310 }
5311 if (header->e_shstrndx != SHN_UNDEF
5312 && header->e_shstrndx >= header->e_shnum)
5313 {
5314 header->e_shstrndx = SHN_UNDEF;
5315 printf (_(" <corrupt: out of range>"));
5316 }
5317 putc ('\n', stdout);
5318 }
5319
5320 if (filedata->section_headers != NULL)
5321 {
5322 if (header->e_phnum == PN_XNUM
5323 && filedata->section_headers[0].sh_info != 0)
5324 header->e_phnum = filedata->section_headers[0].sh_info;
5325 if (header->e_shnum == SHN_UNDEF)
5326 header->e_shnum = filedata->section_headers[0].sh_size;
5327 if (header->e_shstrndx == (SHN_XINDEX & 0xffff))
5328 header->e_shstrndx = filedata->section_headers[0].sh_link;
5329 if (header->e_shstrndx >= header->e_shnum)
5330 header->e_shstrndx = SHN_UNDEF;
5331 }
5332
5333 return true;
5334 }
5335
5336 /* Read in the program headers from FILEDATA and store them in PHEADERS.
5337 Returns TRUE upon success, FALSE otherwise. Loads 32-bit headers. */
5338
5339 static bool
5340 get_32bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
5341 {
5342 Elf32_External_Phdr * phdrs;
5343 Elf32_External_Phdr * external;
5344 Elf_Internal_Phdr * internal;
5345 unsigned int i;
5346 unsigned int size = filedata->file_header.e_phentsize;
5347 unsigned int num = filedata->file_header.e_phnum;
5348
5349 /* PR binutils/17531: Cope with unexpected section header sizes. */
5350 if (size == 0 || num == 0)
5351 return false;
5352 if (size < sizeof * phdrs)
5353 {
5354 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
5355 return false;
5356 }
5357 if (size > sizeof * phdrs)
5358 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
5359
5360 phdrs = (Elf32_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
5361 size, num, _("program headers"));
5362 if (phdrs == NULL)
5363 return false;
5364
5365 for (i = 0, internal = pheaders, external = phdrs;
5366 i < filedata->file_header.e_phnum;
5367 i++, internal++, external++)
5368 {
5369 internal->p_type = BYTE_GET (external->p_type);
5370 internal->p_offset = BYTE_GET (external->p_offset);
5371 internal->p_vaddr = BYTE_GET (external->p_vaddr);
5372 internal->p_paddr = BYTE_GET (external->p_paddr);
5373 internal->p_filesz = BYTE_GET (external->p_filesz);
5374 internal->p_memsz = BYTE_GET (external->p_memsz);
5375 internal->p_flags = BYTE_GET (external->p_flags);
5376 internal->p_align = BYTE_GET (external->p_align);
5377 }
5378
5379 free (phdrs);
5380 return true;
5381 }
5382
5383 /* Read in the program headers from FILEDATA and store them in PHEADERS.
5384 Returns TRUE upon success, FALSE otherwise. Loads 64-bit headers. */
5385
5386 static bool
5387 get_64bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
5388 {
5389 Elf64_External_Phdr * phdrs;
5390 Elf64_External_Phdr * external;
5391 Elf_Internal_Phdr * internal;
5392 unsigned int i;
5393 unsigned int size = filedata->file_header.e_phentsize;
5394 unsigned int num = filedata->file_header.e_phnum;
5395
5396 /* PR binutils/17531: Cope with unexpected section header sizes. */
5397 if (size == 0 || num == 0)
5398 return false;
5399 if (size < sizeof * phdrs)
5400 {
5401 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
5402 return false;
5403 }
5404 if (size > sizeof * phdrs)
5405 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
5406
5407 phdrs = (Elf64_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
5408 size, num, _("program headers"));
5409 if (!phdrs)
5410 return false;
5411
5412 for (i = 0, internal = pheaders, external = phdrs;
5413 i < filedata->file_header.e_phnum;
5414 i++, internal++, external++)
5415 {
5416 internal->p_type = BYTE_GET (external->p_type);
5417 internal->p_flags = BYTE_GET (external->p_flags);
5418 internal->p_offset = BYTE_GET (external->p_offset);
5419 internal->p_vaddr = BYTE_GET (external->p_vaddr);
5420 internal->p_paddr = BYTE_GET (external->p_paddr);
5421 internal->p_filesz = BYTE_GET (external->p_filesz);
5422 internal->p_memsz = BYTE_GET (external->p_memsz);
5423 internal->p_align = BYTE_GET (external->p_align);
5424 }
5425
5426 free (phdrs);
5427 return true;
5428 }
5429
5430 /* Returns TRUE if the program headers were read into `program_headers'. */
5431
5432 static bool
5433 get_program_headers (Filedata * filedata)
5434 {
5435 Elf_Internal_Phdr * phdrs;
5436
5437 /* Check cache of prior read. */
5438 if (filedata->program_headers != NULL)
5439 return true;
5440
5441 /* Be kind to memory checkers by looking for
5442 e_phnum values which we know must be invalid. */
5443 if (filedata->file_header.e_phnum
5444 * (is_32bit_elf ? sizeof (Elf32_External_Phdr) : sizeof (Elf64_External_Phdr))
5445 >= filedata->file_size)
5446 {
5447 error (_("Too many program headers - %#x - the file is not that big\n"),
5448 filedata->file_header.e_phnum);
5449 return false;
5450 }
5451
5452 phdrs = (Elf_Internal_Phdr *) cmalloc (filedata->file_header.e_phnum,
5453 sizeof (Elf_Internal_Phdr));
5454 if (phdrs == NULL)
5455 {
5456 error (_("Out of memory reading %u program headers\n"),
5457 filedata->file_header.e_phnum);
5458 return false;
5459 }
5460
5461 if (is_32bit_elf
5462 ? get_32bit_program_headers (filedata, phdrs)
5463 : get_64bit_program_headers (filedata, phdrs))
5464 {
5465 filedata->program_headers = phdrs;
5466 return true;
5467 }
5468
5469 free (phdrs);
5470 return false;
5471 }
5472
5473 /* Print program header info and locate dynamic section. */
5474
5475 static void
5476 process_program_headers (Filedata * filedata)
5477 {
5478 Elf_Internal_Phdr * segment;
5479 unsigned int i;
5480 Elf_Internal_Phdr * previous_load = NULL;
5481
5482 if (filedata->file_header.e_phnum == 0)
5483 {
5484 /* PR binutils/12467. */
5485 if (filedata->file_header.e_phoff != 0)
5486 warn (_("possibly corrupt ELF header - it has a non-zero program"
5487 " header offset, but no program headers\n"));
5488 else if (do_segments)
5489 {
5490 if (filedata->is_separate)
5491 printf (_("\nThere are no program headers in linked file '%s'.\n"),
5492 filedata->file_name);
5493 else
5494 printf (_("\nThere are no program headers in this file.\n"));
5495 }
5496 goto no_headers;
5497 }
5498
5499 if (do_segments && !do_header)
5500 {
5501 if (filedata->is_separate)
5502 printf ("\nIn linked file '%s' the ELF file type is %s\n",
5503 filedata->file_name, get_file_type (filedata));
5504 else
5505 printf (_("\nElf file type is %s\n"), get_file_type (filedata));
5506 printf (_("Entry point 0x%s\n"), bfd_vmatoa ("x", filedata->file_header.e_entry));
5507 printf (ngettext ("There is %d program header, starting at offset %s\n",
5508 "There are %d program headers, starting at offset %s\n",
5509 filedata->file_header.e_phnum),
5510 filedata->file_header.e_phnum,
5511 bfd_vmatoa ("u", filedata->file_header.e_phoff));
5512 }
5513
5514 if (! get_program_headers (filedata))
5515 goto no_headers;
5516
5517 if (do_segments)
5518 {
5519 if (filedata->file_header.e_phnum > 1)
5520 printf (_("\nProgram Headers:\n"));
5521 else
5522 printf (_("\nProgram Headers:\n"));
5523
5524 if (is_32bit_elf)
5525 printf
5526 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
5527 else if (do_wide)
5528 printf
5529 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
5530 else
5531 {
5532 printf
5533 (_(" Type Offset VirtAddr PhysAddr\n"));
5534 printf
5535 (_(" FileSiz MemSiz Flags Align\n"));
5536 }
5537 }
5538
5539 unsigned long dynamic_addr = 0;
5540 bfd_size_type dynamic_size = 0;
5541 for (i = 0, segment = filedata->program_headers;
5542 i < filedata->file_header.e_phnum;
5543 i++, segment++)
5544 {
5545 if (do_segments)
5546 {
5547 printf (" %-14.14s ", get_segment_type (filedata, segment->p_type));
5548
5549 if (is_32bit_elf)
5550 {
5551 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
5552 printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
5553 printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
5554 printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
5555 printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
5556 printf ("%c%c%c ",
5557 (segment->p_flags & PF_R ? 'R' : ' '),
5558 (segment->p_flags & PF_W ? 'W' : ' '),
5559 (segment->p_flags & PF_X ? 'E' : ' '));
5560 printf ("%#lx", (unsigned long) segment->p_align);
5561 }
5562 else if (do_wide)
5563 {
5564 if ((unsigned long) segment->p_offset == segment->p_offset)
5565 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
5566 else
5567 {
5568 print_vma (segment->p_offset, FULL_HEX);
5569 putchar (' ');
5570 }
5571
5572 print_vma (segment->p_vaddr, FULL_HEX);
5573 putchar (' ');
5574 print_vma (segment->p_paddr, FULL_HEX);
5575 putchar (' ');
5576
5577 if ((unsigned long) segment->p_filesz == segment->p_filesz)
5578 printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
5579 else
5580 {
5581 print_vma (segment->p_filesz, FULL_HEX);
5582 putchar (' ');
5583 }
5584
5585 if ((unsigned long) segment->p_memsz == segment->p_memsz)
5586 printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
5587 else
5588 {
5589 print_vma (segment->p_memsz, FULL_HEX);
5590 }
5591
5592 printf (" %c%c%c ",
5593 (segment->p_flags & PF_R ? 'R' : ' '),
5594 (segment->p_flags & PF_W ? 'W' : ' '),
5595 (segment->p_flags & PF_X ? 'E' : ' '));
5596
5597 if ((unsigned long) segment->p_align == segment->p_align)
5598 printf ("%#lx", (unsigned long) segment->p_align);
5599 else
5600 {
5601 print_vma (segment->p_align, PREFIX_HEX);
5602 }
5603 }
5604 else
5605 {
5606 print_vma (segment->p_offset, FULL_HEX);
5607 putchar (' ');
5608 print_vma (segment->p_vaddr, FULL_HEX);
5609 putchar (' ');
5610 print_vma (segment->p_paddr, FULL_HEX);
5611 printf ("\n ");
5612 print_vma (segment->p_filesz, FULL_HEX);
5613 putchar (' ');
5614 print_vma (segment->p_memsz, FULL_HEX);
5615 printf (" %c%c%c ",
5616 (segment->p_flags & PF_R ? 'R' : ' '),
5617 (segment->p_flags & PF_W ? 'W' : ' '),
5618 (segment->p_flags & PF_X ? 'E' : ' '));
5619 print_vma (segment->p_align, PREFIX_HEX);
5620 }
5621
5622 putc ('\n', stdout);
5623 }
5624
5625 switch (segment->p_type)
5626 {
5627 case PT_LOAD:
5628 #if 0 /* Do not warn about out of order PT_LOAD segments. Although officially
5629 required by the ELF standard, several programs, including the Linux
5630 kernel, make use of non-ordered segments. */
5631 if (previous_load
5632 && previous_load->p_vaddr > segment->p_vaddr)
5633 error (_("LOAD segments must be sorted in order of increasing VirtAddr\n"));
5634 #endif
5635 if (segment->p_memsz < segment->p_filesz)
5636 error (_("the segment's file size is larger than its memory size\n"));
5637 previous_load = segment;
5638 break;
5639
5640 case PT_PHDR:
5641 /* PR 20815 - Verify that the program header is loaded into memory. */
5642 if (i > 0 && previous_load != NULL)
5643 error (_("the PHDR segment must occur before any LOAD segment\n"));
5644 if (filedata->file_header.e_machine != EM_PARISC)
5645 {
5646 unsigned int j;
5647
5648 for (j = 1; j < filedata->file_header.e_phnum; j++)
5649 {
5650 Elf_Internal_Phdr *load = filedata->program_headers + j;
5651 if (load->p_type == PT_LOAD
5652 && load->p_offset <= segment->p_offset
5653 && (load->p_offset + load->p_filesz
5654 >= segment->p_offset + segment->p_filesz)
5655 && load->p_vaddr <= segment->p_vaddr
5656 && (load->p_vaddr + load->p_filesz
5657 >= segment->p_vaddr + segment->p_filesz))
5658 break;
5659 }
5660 if (j == filedata->file_header.e_phnum)
5661 error (_("the PHDR segment is not covered by a LOAD segment\n"));
5662 }
5663 break;
5664
5665 case PT_DYNAMIC:
5666 if (dynamic_addr)
5667 error (_("more than one dynamic segment\n"));
5668
5669 /* By default, assume that the .dynamic section is the first
5670 section in the DYNAMIC segment. */
5671 dynamic_addr = segment->p_offset;
5672 dynamic_size = segment->p_filesz;
5673
5674 /* Try to locate the .dynamic section. If there is
5675 a section header table, we can easily locate it. */
5676 if (filedata->section_headers != NULL)
5677 {
5678 Elf_Internal_Shdr * sec;
5679
5680 sec = find_section (filedata, ".dynamic");
5681 if (sec == NULL || sec->sh_size == 0)
5682 {
5683 /* A corresponding .dynamic section is expected, but on
5684 IA-64/OpenVMS it is OK for it to be missing. */
5685 if (!is_ia64_vms (filedata))
5686 error (_("no .dynamic section in the dynamic segment\n"));
5687 break;
5688 }
5689
5690 if (sec->sh_type == SHT_NOBITS)
5691 {
5692 dynamic_addr = 0;
5693 dynamic_size = 0;
5694 break;
5695 }
5696
5697 dynamic_addr = sec->sh_offset;
5698 dynamic_size = sec->sh_size;
5699
5700 /* The PT_DYNAMIC segment, which is used by the run-time
5701 loader, should exactly match the .dynamic section. */
5702 if (do_checks
5703 && (dynamic_addr != segment->p_offset
5704 || dynamic_size != segment->p_filesz))
5705 warn (_("\
5706 the .dynamic section is not the same as the dynamic segment\n"));
5707 }
5708
5709 /* PR binutils/17512: Avoid corrupt dynamic section info in the
5710 segment. Check this after matching against the section headers
5711 so we don't warn on debuginfo file (which have NOBITS .dynamic
5712 sections). */
5713 if (dynamic_addr > filedata->file_size
5714 || (dynamic_size > filedata->file_size - dynamic_addr))
5715 {
5716 error (_("the dynamic segment offset + size exceeds the size of the file\n"));
5717 dynamic_addr = 0;
5718 dynamic_size = 0;
5719 }
5720 break;
5721
5722 case PT_INTERP:
5723 if (segment->p_offset >= filedata->file_size
5724 || segment->p_filesz > filedata->file_size - segment->p_offset
5725 || segment->p_filesz - 1 >= (size_t) -2
5726 || fseek (filedata->handle,
5727 filedata->archive_file_offset + (long) segment->p_offset,
5728 SEEK_SET))
5729 error (_("Unable to find program interpreter name\n"));
5730 else
5731 {
5732 size_t len = segment->p_filesz;
5733 free (filedata->program_interpreter);
5734 filedata->program_interpreter = xmalloc (len + 1);
5735 len = fread (filedata->program_interpreter, 1, len,
5736 filedata->handle);
5737 filedata->program_interpreter[len] = 0;
5738
5739 if (do_segments)
5740 printf (_(" [Requesting program interpreter: %s]\n"),
5741 filedata->program_interpreter);
5742 }
5743 break;
5744 }
5745 }
5746
5747 if (do_segments
5748 && filedata->section_headers != NULL
5749 && filedata->string_table != NULL)
5750 {
5751 printf (_("\n Section to Segment mapping:\n"));
5752 printf (_(" Segment Sections...\n"));
5753
5754 for (i = 0; i < filedata->file_header.e_phnum; i++)
5755 {
5756 unsigned int j;
5757 Elf_Internal_Shdr * section;
5758
5759 segment = filedata->program_headers + i;
5760 section = filedata->section_headers + 1;
5761
5762 printf (" %2.2d ", i);
5763
5764 for (j = 1; j < filedata->file_header.e_shnum; j++, section++)
5765 {
5766 if (!ELF_TBSS_SPECIAL (section, segment)
5767 && ELF_SECTION_IN_SEGMENT_STRICT (section, segment))
5768 printf ("%s ", printable_section_name (filedata, section));
5769 }
5770
5771 putc ('\n',stdout);
5772 }
5773 }
5774
5775 filedata->dynamic_addr = dynamic_addr;
5776 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
5777 return;
5778
5779 no_headers:
5780 filedata->dynamic_addr = 0;
5781 filedata->dynamic_size = 1;
5782 }
5783
5784
5785 /* Find the file offset corresponding to VMA by using the program headers. */
5786
5787 static long
5788 offset_from_vma (Filedata * filedata, bfd_vma vma, bfd_size_type size)
5789 {
5790 Elf_Internal_Phdr * seg;
5791
5792 if (! get_program_headers (filedata))
5793 {
5794 warn (_("Cannot interpret virtual addresses without program headers.\n"));
5795 return (long) vma;
5796 }
5797
5798 for (seg = filedata->program_headers;
5799 seg < filedata->program_headers + filedata->file_header.e_phnum;
5800 ++seg)
5801 {
5802 if (seg->p_type != PT_LOAD)
5803 continue;
5804
5805 if (vma >= (seg->p_vaddr & -seg->p_align)
5806 && vma + size <= seg->p_vaddr + seg->p_filesz)
5807 return vma - seg->p_vaddr + seg->p_offset;
5808 }
5809
5810 warn (_("Virtual address 0x%lx not located in any PT_LOAD segment.\n"),
5811 (unsigned long) vma);
5812 return (long) vma;
5813 }
5814
5815
5816 /* Allocate memory and load the sections headers into FILEDATA->filedata->section_headers.
5817 If PROBE is true, this is just a probe and we do not generate any error
5818 messages if the load fails. */
5819
5820 static bool
5821 get_32bit_section_headers (Filedata * filedata, bool probe)
5822 {
5823 Elf32_External_Shdr * shdrs;
5824 Elf_Internal_Shdr * internal;
5825 unsigned int i;
5826 unsigned int size = filedata->file_header.e_shentsize;
5827 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
5828
5829 /* PR binutils/17531: Cope with unexpected section header sizes. */
5830 if (size == 0 || num == 0)
5831 return false;
5832 if (size < sizeof * shdrs)
5833 {
5834 if (! probe)
5835 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
5836 return false;
5837 }
5838 if (!probe && size > sizeof * shdrs)
5839 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
5840
5841 shdrs = (Elf32_External_Shdr *) get_data (NULL, filedata, filedata->file_header.e_shoff,
5842 size, num,
5843 probe ? NULL : _("section headers"));
5844 if (shdrs == NULL)
5845 return false;
5846
5847 filedata->section_headers = (Elf_Internal_Shdr *)
5848 cmalloc (num, sizeof (Elf_Internal_Shdr));
5849 if (filedata->section_headers == NULL)
5850 {
5851 if (!probe)
5852 error (_("Out of memory reading %u section headers\n"), num);
5853 free (shdrs);
5854 return false;
5855 }
5856
5857 for (i = 0, internal = filedata->section_headers;
5858 i < num;
5859 i++, internal++)
5860 {
5861 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
5862 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
5863 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
5864 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
5865 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
5866 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
5867 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
5868 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
5869 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
5870 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
5871 if (!probe && internal->sh_link > num)
5872 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
5873 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
5874 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
5875 }
5876
5877 free (shdrs);
5878 return true;
5879 }
5880
5881 /* Like get_32bit_section_headers, except that it fetches 64-bit headers. */
5882
5883 static bool
5884 get_64bit_section_headers (Filedata * filedata, bool probe)
5885 {
5886 Elf64_External_Shdr * shdrs;
5887 Elf_Internal_Shdr * internal;
5888 unsigned int i;
5889 unsigned int size = filedata->file_header.e_shentsize;
5890 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
5891
5892 /* PR binutils/17531: Cope with unexpected section header sizes. */
5893 if (size == 0 || num == 0)
5894 return false;
5895
5896 if (size < sizeof * shdrs)
5897 {
5898 if (! probe)
5899 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
5900 return false;
5901 }
5902
5903 if (! probe && size > sizeof * shdrs)
5904 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
5905
5906 shdrs = (Elf64_External_Shdr *) get_data (NULL, filedata,
5907 filedata->file_header.e_shoff,
5908 size, num,
5909 probe ? NULL : _("section headers"));
5910 if (shdrs == NULL)
5911 return false;
5912
5913 filedata->section_headers = (Elf_Internal_Shdr *)
5914 cmalloc (num, sizeof (Elf_Internal_Shdr));
5915 if (filedata->section_headers == NULL)
5916 {
5917 if (! probe)
5918 error (_("Out of memory reading %u section headers\n"), num);
5919 free (shdrs);
5920 return false;
5921 }
5922
5923 for (i = 0, internal = filedata->section_headers;
5924 i < num;
5925 i++, internal++)
5926 {
5927 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
5928 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
5929 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
5930 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
5931 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
5932 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
5933 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
5934 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
5935 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
5936 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
5937 if (!probe && internal->sh_link > num)
5938 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
5939 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
5940 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
5941 }
5942
5943 free (shdrs);
5944 return true;
5945 }
5946
5947 static bool
5948 get_section_headers (Filedata *filedata, bool probe)
5949 {
5950 if (filedata->section_headers != NULL)
5951 return true;
5952
5953 if (is_32bit_elf)
5954 return get_32bit_section_headers (filedata, probe);
5955 else
5956 return get_64bit_section_headers (filedata, probe);
5957 }
5958
5959 static Elf_Internal_Sym *
5960 get_32bit_elf_symbols (Filedata * filedata,
5961 Elf_Internal_Shdr * section,
5962 unsigned long * num_syms_return)
5963 {
5964 unsigned long number = 0;
5965 Elf32_External_Sym * esyms = NULL;
5966 Elf_External_Sym_Shndx * shndx = NULL;
5967 Elf_Internal_Sym * isyms = NULL;
5968 Elf_Internal_Sym * psym;
5969 unsigned int j;
5970 elf_section_list * entry;
5971
5972 if (section->sh_size == 0)
5973 {
5974 if (num_syms_return != NULL)
5975 * num_syms_return = 0;
5976 return NULL;
5977 }
5978
5979 /* Run some sanity checks first. */
5980 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
5981 {
5982 error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
5983 printable_section_name (filedata, section),
5984 (unsigned long) section->sh_entsize);
5985 goto exit_point;
5986 }
5987
5988 if (section->sh_size > filedata->file_size)
5989 {
5990 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
5991 printable_section_name (filedata, section),
5992 (unsigned long) section->sh_size);
5993 goto exit_point;
5994 }
5995
5996 number = section->sh_size / section->sh_entsize;
5997
5998 if (number * sizeof (Elf32_External_Sym) > section->sh_size + 1)
5999 {
6000 error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
6001 (unsigned long) section->sh_size,
6002 printable_section_name (filedata, section),
6003 (unsigned long) section->sh_entsize);
6004 goto exit_point;
6005 }
6006
6007 esyms = (Elf32_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
6008 section->sh_size, _("symbols"));
6009 if (esyms == NULL)
6010 goto exit_point;
6011
6012 shndx = NULL;
6013 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
6014 {
6015 if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
6016 continue;
6017
6018 if (shndx != NULL)
6019 {
6020 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
6021 free (shndx);
6022 }
6023
6024 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
6025 entry->hdr->sh_offset,
6026 1, entry->hdr->sh_size,
6027 _("symbol table section indices"));
6028 if (shndx == NULL)
6029 goto exit_point;
6030
6031 /* PR17531: file: heap-buffer-overflow */
6032 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
6033 {
6034 error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
6035 printable_section_name (filedata, entry->hdr),
6036 (unsigned long) entry->hdr->sh_size,
6037 (unsigned long) section->sh_size);
6038 goto exit_point;
6039 }
6040 }
6041
6042 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
6043
6044 if (isyms == NULL)
6045 {
6046 error (_("Out of memory reading %lu symbols\n"),
6047 (unsigned long) number);
6048 goto exit_point;
6049 }
6050
6051 for (j = 0, psym = isyms; j < number; j++, psym++)
6052 {
6053 psym->st_name = BYTE_GET (esyms[j].st_name);
6054 psym->st_value = BYTE_GET (esyms[j].st_value);
6055 psym->st_size = BYTE_GET (esyms[j].st_size);
6056 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
6057 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
6058 psym->st_shndx
6059 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
6060 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
6061 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
6062 psym->st_info = BYTE_GET (esyms[j].st_info);
6063 psym->st_other = BYTE_GET (esyms[j].st_other);
6064 }
6065
6066 exit_point:
6067 free (shndx);
6068 free (esyms);
6069
6070 if (num_syms_return != NULL)
6071 * num_syms_return = isyms == NULL ? 0 : number;
6072
6073 return isyms;
6074 }
6075
6076 static Elf_Internal_Sym *
6077 get_64bit_elf_symbols (Filedata * filedata,
6078 Elf_Internal_Shdr * section,
6079 unsigned long * num_syms_return)
6080 {
6081 unsigned long number = 0;
6082 Elf64_External_Sym * esyms = NULL;
6083 Elf_External_Sym_Shndx * shndx = NULL;
6084 Elf_Internal_Sym * isyms = NULL;
6085 Elf_Internal_Sym * psym;
6086 unsigned int j;
6087 elf_section_list * entry;
6088
6089 if (section->sh_size == 0)
6090 {
6091 if (num_syms_return != NULL)
6092 * num_syms_return = 0;
6093 return NULL;
6094 }
6095
6096 /* Run some sanity checks first. */
6097 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
6098 {
6099 error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
6100 printable_section_name (filedata, section),
6101 (unsigned long) section->sh_entsize);
6102 goto exit_point;
6103 }
6104
6105 if (section->sh_size > filedata->file_size)
6106 {
6107 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
6108 printable_section_name (filedata, section),
6109 (unsigned long) section->sh_size);
6110 goto exit_point;
6111 }
6112
6113 number = section->sh_size / section->sh_entsize;
6114
6115 if (number * sizeof (Elf64_External_Sym) > section->sh_size + 1)
6116 {
6117 error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
6118 (unsigned long) section->sh_size,
6119 printable_section_name (filedata, section),
6120 (unsigned long) section->sh_entsize);
6121 goto exit_point;
6122 }
6123
6124 esyms = (Elf64_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
6125 section->sh_size, _("symbols"));
6126 if (!esyms)
6127 goto exit_point;
6128
6129 shndx = NULL;
6130 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
6131 {
6132 if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
6133 continue;
6134
6135 if (shndx != NULL)
6136 {
6137 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
6138 free (shndx);
6139 }
6140
6141 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
6142 entry->hdr->sh_offset,
6143 1, entry->hdr->sh_size,
6144 _("symbol table section indices"));
6145 if (shndx == NULL)
6146 goto exit_point;
6147
6148 /* PR17531: file: heap-buffer-overflow */
6149 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
6150 {
6151 error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
6152 printable_section_name (filedata, entry->hdr),
6153 (unsigned long) entry->hdr->sh_size,
6154 (unsigned long) section->sh_size);
6155 goto exit_point;
6156 }
6157 }
6158
6159 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
6160
6161 if (isyms == NULL)
6162 {
6163 error (_("Out of memory reading %lu symbols\n"),
6164 (unsigned long) number);
6165 goto exit_point;
6166 }
6167
6168 for (j = 0, psym = isyms; j < number; j++, psym++)
6169 {
6170 psym->st_name = BYTE_GET (esyms[j].st_name);
6171 psym->st_info = BYTE_GET (esyms[j].st_info);
6172 psym->st_other = BYTE_GET (esyms[j].st_other);
6173 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
6174
6175 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
6176 psym->st_shndx
6177 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
6178 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
6179 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
6180
6181 psym->st_value = BYTE_GET (esyms[j].st_value);
6182 psym->st_size = BYTE_GET (esyms[j].st_size);
6183 }
6184
6185 exit_point:
6186 free (shndx);
6187 free (esyms);
6188
6189 if (num_syms_return != NULL)
6190 * num_syms_return = isyms == NULL ? 0 : number;
6191
6192 return isyms;
6193 }
6194
6195 static Elf_Internal_Sym *
6196 get_elf_symbols (Filedata *filedata,
6197 Elf_Internal_Shdr *section,
6198 unsigned long *num_syms_return)
6199 {
6200 if (is_32bit_elf)
6201 return get_32bit_elf_symbols (filedata, section, num_syms_return);
6202 else
6203 return get_64bit_elf_symbols (filedata, section, num_syms_return);
6204 }
6205
6206 static const char *
6207 get_elf_section_flags (Filedata * filedata, bfd_vma sh_flags)
6208 {
6209 static char buff[1024];
6210 char * p = buff;
6211 unsigned int field_size = is_32bit_elf ? 8 : 16;
6212 signed int sindex;
6213 unsigned int size = sizeof (buff) - (field_size + 4 + 1);
6214 bfd_vma os_flags = 0;
6215 bfd_vma proc_flags = 0;
6216 bfd_vma unknown_flags = 0;
6217 static const struct
6218 {
6219 const char * str;
6220 unsigned int len;
6221 }
6222 flags [] =
6223 {
6224 /* 0 */ { STRING_COMMA_LEN ("WRITE") },
6225 /* 1 */ { STRING_COMMA_LEN ("ALLOC") },
6226 /* 2 */ { STRING_COMMA_LEN ("EXEC") },
6227 /* 3 */ { STRING_COMMA_LEN ("MERGE") },
6228 /* 4 */ { STRING_COMMA_LEN ("STRINGS") },
6229 /* 5 */ { STRING_COMMA_LEN ("INFO LINK") },
6230 /* 6 */ { STRING_COMMA_LEN ("LINK ORDER") },
6231 /* 7 */ { STRING_COMMA_LEN ("OS NONCONF") },
6232 /* 8 */ { STRING_COMMA_LEN ("GROUP") },
6233 /* 9 */ { STRING_COMMA_LEN ("TLS") },
6234 /* IA-64 specific. */
6235 /* 10 */ { STRING_COMMA_LEN ("SHORT") },
6236 /* 11 */ { STRING_COMMA_LEN ("NORECOV") },
6237 /* IA-64 OpenVMS specific. */
6238 /* 12 */ { STRING_COMMA_LEN ("VMS_GLOBAL") },
6239 /* 13 */ { STRING_COMMA_LEN ("VMS_OVERLAID") },
6240 /* 14 */ { STRING_COMMA_LEN ("VMS_SHARED") },
6241 /* 15 */ { STRING_COMMA_LEN ("VMS_VECTOR") },
6242 /* 16 */ { STRING_COMMA_LEN ("VMS_ALLOC_64BIT") },
6243 /* 17 */ { STRING_COMMA_LEN ("VMS_PROTECTED") },
6244 /* Generic. */
6245 /* 18 */ { STRING_COMMA_LEN ("EXCLUDE") },
6246 /* SPARC specific. */
6247 /* 19 */ { STRING_COMMA_LEN ("ORDERED") },
6248 /* 20 */ { STRING_COMMA_LEN ("COMPRESSED") },
6249 /* ARM specific. */
6250 /* 21 */ { STRING_COMMA_LEN ("ENTRYSECT") },
6251 /* 22 */ { STRING_COMMA_LEN ("ARM_PURECODE") },
6252 /* 23 */ { STRING_COMMA_LEN ("COMDEF") },
6253 /* GNU specific. */
6254 /* 24 */ { STRING_COMMA_LEN ("GNU_MBIND") },
6255 /* VLE specific. */
6256 /* 25 */ { STRING_COMMA_LEN ("VLE") },
6257 /* GNU specific. */
6258 /* 26 */ { STRING_COMMA_LEN ("GNU_RETAIN") },
6259 };
6260
6261 if (do_section_details)
6262 {
6263 sprintf (buff, "[%*.*lx]: ",
6264 field_size, field_size, (unsigned long) sh_flags);
6265 p += field_size + 4;
6266 }
6267
6268 while (sh_flags)
6269 {
6270 bfd_vma flag;
6271
6272 flag = sh_flags & - sh_flags;
6273 sh_flags &= ~ flag;
6274
6275 if (do_section_details)
6276 {
6277 switch (flag)
6278 {
6279 case SHF_WRITE: sindex = 0; break;
6280 case SHF_ALLOC: sindex = 1; break;
6281 case SHF_EXECINSTR: sindex = 2; break;
6282 case SHF_MERGE: sindex = 3; break;
6283 case SHF_STRINGS: sindex = 4; break;
6284 case SHF_INFO_LINK: sindex = 5; break;
6285 case SHF_LINK_ORDER: sindex = 6; break;
6286 case SHF_OS_NONCONFORMING: sindex = 7; break;
6287 case SHF_GROUP: sindex = 8; break;
6288 case SHF_TLS: sindex = 9; break;
6289 case SHF_EXCLUDE: sindex = 18; break;
6290 case SHF_COMPRESSED: sindex = 20; break;
6291
6292 default:
6293 sindex = -1;
6294 switch (filedata->file_header.e_machine)
6295 {
6296 case EM_IA_64:
6297 if (flag == SHF_IA_64_SHORT)
6298 sindex = 10;
6299 else if (flag == SHF_IA_64_NORECOV)
6300 sindex = 11;
6301 #ifdef BFD64
6302 else if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
6303 switch (flag)
6304 {
6305 case SHF_IA_64_VMS_GLOBAL: sindex = 12; break;
6306 case SHF_IA_64_VMS_OVERLAID: sindex = 13; break;
6307 case SHF_IA_64_VMS_SHARED: sindex = 14; break;
6308 case SHF_IA_64_VMS_VECTOR: sindex = 15; break;
6309 case SHF_IA_64_VMS_ALLOC_64BIT: sindex = 16; break;
6310 case SHF_IA_64_VMS_PROTECTED: sindex = 17; break;
6311 default: break;
6312 }
6313 #endif
6314 break;
6315
6316 case EM_386:
6317 case EM_IAMCU:
6318 case EM_X86_64:
6319 case EM_L1OM:
6320 case EM_K1OM:
6321 case EM_OLD_SPARCV9:
6322 case EM_SPARC32PLUS:
6323 case EM_SPARCV9:
6324 case EM_SPARC:
6325 if (flag == SHF_ORDERED)
6326 sindex = 19;
6327 break;
6328
6329 case EM_ARM:
6330 switch (flag)
6331 {
6332 case SHF_ENTRYSECT: sindex = 21; break;
6333 case SHF_ARM_PURECODE: sindex = 22; break;
6334 case SHF_COMDEF: sindex = 23; break;
6335 default: break;
6336 }
6337 break;
6338 case EM_PPC:
6339 if (flag == SHF_PPC_VLE)
6340 sindex = 25;
6341 break;
6342 default:
6343 break;
6344 }
6345
6346 switch (filedata->file_header.e_ident[EI_OSABI])
6347 {
6348 case ELFOSABI_GNU:
6349 case ELFOSABI_FREEBSD:
6350 if (flag == SHF_GNU_RETAIN)
6351 sindex = 26;
6352 /* Fall through */
6353 case ELFOSABI_NONE:
6354 if (flag == SHF_GNU_MBIND)
6355 /* We should not recognize SHF_GNU_MBIND for
6356 ELFOSABI_NONE, but binutils as of 2019-07-23 did
6357 not set the EI_OSABI header byte. */
6358 sindex = 24;
6359 break;
6360 default:
6361 break;
6362 }
6363 break;
6364 }
6365
6366 if (sindex != -1)
6367 {
6368 if (p != buff + field_size + 4)
6369 {
6370 if (size < (10 + 2))
6371 {
6372 warn (_("Internal error: not enough buffer room for section flag info"));
6373 return _("<unknown>");
6374 }
6375 size -= 2;
6376 *p++ = ',';
6377 *p++ = ' ';
6378 }
6379
6380 size -= flags [sindex].len;
6381 p = stpcpy (p, flags [sindex].str);
6382 }
6383 else if (flag & SHF_MASKOS)
6384 os_flags |= flag;
6385 else if (flag & SHF_MASKPROC)
6386 proc_flags |= flag;
6387 else
6388 unknown_flags |= flag;
6389 }
6390 else
6391 {
6392 switch (flag)
6393 {
6394 case SHF_WRITE: *p = 'W'; break;
6395 case SHF_ALLOC: *p = 'A'; break;
6396 case SHF_EXECINSTR: *p = 'X'; break;
6397 case SHF_MERGE: *p = 'M'; break;
6398 case SHF_STRINGS: *p = 'S'; break;
6399 case SHF_INFO_LINK: *p = 'I'; break;
6400 case SHF_LINK_ORDER: *p = 'L'; break;
6401 case SHF_OS_NONCONFORMING: *p = 'O'; break;
6402 case SHF_GROUP: *p = 'G'; break;
6403 case SHF_TLS: *p = 'T'; break;
6404 case SHF_EXCLUDE: *p = 'E'; break;
6405 case SHF_COMPRESSED: *p = 'C'; break;
6406
6407 default:
6408 if ((filedata->file_header.e_machine == EM_X86_64
6409 || filedata->file_header.e_machine == EM_L1OM
6410 || filedata->file_header.e_machine == EM_K1OM)
6411 && flag == SHF_X86_64_LARGE)
6412 *p = 'l';
6413 else if (filedata->file_header.e_machine == EM_ARM
6414 && flag == SHF_ARM_PURECODE)
6415 *p = 'y';
6416 else if (filedata->file_header.e_machine == EM_PPC
6417 && flag == SHF_PPC_VLE)
6418 *p = 'v';
6419 else if (flag & SHF_MASKOS)
6420 {
6421 switch (filedata->file_header.e_ident[EI_OSABI])
6422 {
6423 case ELFOSABI_GNU:
6424 case ELFOSABI_FREEBSD:
6425 if (flag == SHF_GNU_RETAIN)
6426 {
6427 *p = 'R';
6428 break;
6429 }
6430 /* Fall through */
6431 case ELFOSABI_NONE:
6432 if (flag == SHF_GNU_MBIND)
6433 {
6434 /* We should not recognize SHF_GNU_MBIND for
6435 ELFOSABI_NONE, but binutils as of 2019-07-23 did
6436 not set the EI_OSABI header byte. */
6437 *p = 'D';
6438 break;
6439 }
6440 /* Fall through */
6441 default:
6442 *p = 'o';
6443 sh_flags &= ~SHF_MASKOS;
6444 break;
6445 }
6446 }
6447 else if (flag & SHF_MASKPROC)
6448 {
6449 *p = 'p';
6450 sh_flags &= ~ SHF_MASKPROC;
6451 }
6452 else
6453 *p = 'x';
6454 break;
6455 }
6456 p++;
6457 }
6458 }
6459
6460 if (do_section_details)
6461 {
6462 if (os_flags)
6463 {
6464 size -= 5 + field_size;
6465 if (p != buff + field_size + 4)
6466 {
6467 if (size < (2 + 1))
6468 {
6469 warn (_("Internal error: not enough buffer room for section flag info"));
6470 return _("<unknown>");
6471 }
6472 size -= 2;
6473 *p++ = ',';
6474 *p++ = ' ';
6475 }
6476 sprintf (p, "OS (%*.*lx)", field_size, field_size,
6477 (unsigned long) os_flags);
6478 p += 5 + field_size;
6479 }
6480 if (proc_flags)
6481 {
6482 size -= 7 + field_size;
6483 if (p != buff + field_size + 4)
6484 {
6485 if (size < (2 + 1))
6486 {
6487 warn (_("Internal error: not enough buffer room for section flag info"));
6488 return _("<unknown>");
6489 }
6490 size -= 2;
6491 *p++ = ',';
6492 *p++ = ' ';
6493 }
6494 sprintf (p, "PROC (%*.*lx)", field_size, field_size,
6495 (unsigned long) proc_flags);
6496 p += 7 + field_size;
6497 }
6498 if (unknown_flags)
6499 {
6500 size -= 10 + field_size;
6501 if (p != buff + field_size + 4)
6502 {
6503 if (size < (2 + 1))
6504 {
6505 warn (_("Internal error: not enough buffer room for section flag info"));
6506 return _("<unknown>");
6507 }
6508 size -= 2;
6509 *p++ = ',';
6510 *p++ = ' ';
6511 }
6512 sprintf (p, _("UNKNOWN (%*.*lx)"), field_size, field_size,
6513 (unsigned long) unknown_flags);
6514 p += 10 + field_size;
6515 }
6516 }
6517
6518 *p = '\0';
6519 return buff;
6520 }
6521
6522 static unsigned int ATTRIBUTE_WARN_UNUSED_RESULT
6523 get_compression_header (Elf_Internal_Chdr *chdr, unsigned char *buf, bfd_size_type size)
6524 {
6525 if (is_32bit_elf)
6526 {
6527 Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) buf;
6528
6529 if (size < sizeof (* echdr))
6530 {
6531 error (_("Compressed section is too small even for a compression header\n"));
6532 return 0;
6533 }
6534
6535 chdr->ch_type = BYTE_GET (echdr->ch_type);
6536 chdr->ch_size = BYTE_GET (echdr->ch_size);
6537 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
6538 return sizeof (*echdr);
6539 }
6540 else
6541 {
6542 Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) buf;
6543
6544 if (size < sizeof (* echdr))
6545 {
6546 error (_("Compressed section is too small even for a compression header\n"));
6547 return 0;
6548 }
6549
6550 chdr->ch_type = BYTE_GET (echdr->ch_type);
6551 chdr->ch_size = BYTE_GET (echdr->ch_size);
6552 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
6553 return sizeof (*echdr);
6554 }
6555 }
6556
6557 static bool
6558 process_section_headers (Filedata * filedata)
6559 {
6560 Elf_Internal_Shdr * section;
6561 unsigned int i;
6562
6563 if (filedata->file_header.e_shnum == 0)
6564 {
6565 /* PR binutils/12467. */
6566 if (filedata->file_header.e_shoff != 0)
6567 {
6568 warn (_("possibly corrupt ELF file header - it has a non-zero"
6569 " section header offset, but no section headers\n"));
6570 return false;
6571 }
6572 else if (do_sections)
6573 printf (_("\nThere are no sections in this file.\n"));
6574
6575 return true;
6576 }
6577
6578 if (do_sections && !do_header)
6579 {
6580 if (filedata->is_separate && process_links)
6581 printf (_("In linked file '%s': "), filedata->file_name);
6582 if (! filedata->is_separate || process_links)
6583 printf (ngettext ("There is %d section header, "
6584 "starting at offset 0x%lx:\n",
6585 "There are %d section headers, "
6586 "starting at offset 0x%lx:\n",
6587 filedata->file_header.e_shnum),
6588 filedata->file_header.e_shnum,
6589 (unsigned long) filedata->file_header.e_shoff);
6590 }
6591
6592 if (!get_section_headers (filedata, false))
6593 return false;
6594
6595 /* Read in the string table, so that we have names to display. */
6596 if (filedata->file_header.e_shstrndx != SHN_UNDEF
6597 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
6598 {
6599 section = filedata->section_headers + filedata->file_header.e_shstrndx;
6600
6601 if (section->sh_size != 0)
6602 {
6603 filedata->string_table = (char *) get_data (NULL, filedata, section->sh_offset,
6604 1, section->sh_size,
6605 _("string table"));
6606
6607 filedata->string_table_length = filedata->string_table != NULL ? section->sh_size : 0;
6608 }
6609 }
6610
6611 /* Scan the sections for the dynamic symbol table
6612 and dynamic string table and debug sections. */
6613 eh_addr_size = is_32bit_elf ? 4 : 8;
6614 switch (filedata->file_header.e_machine)
6615 {
6616 case EM_MIPS:
6617 case EM_MIPS_RS3_LE:
6618 /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
6619 FDE addresses. However, the ABI also has a semi-official ILP32
6620 variant for which the normal FDE address size rules apply.
6621
6622 GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
6623 section, where XX is the size of longs in bits. Unfortunately,
6624 earlier compilers provided no way of distinguishing ILP32 objects
6625 from LP64 objects, so if there's any doubt, we should assume that
6626 the official LP64 form is being used. */
6627 if ((filedata->file_header.e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64
6628 && find_section (filedata, ".gcc_compiled_long32") == NULL)
6629 eh_addr_size = 8;
6630 break;
6631
6632 case EM_H8_300:
6633 case EM_H8_300H:
6634 switch (filedata->file_header.e_flags & EF_H8_MACH)
6635 {
6636 case E_H8_MACH_H8300:
6637 case E_H8_MACH_H8300HN:
6638 case E_H8_MACH_H8300SN:
6639 case E_H8_MACH_H8300SXN:
6640 eh_addr_size = 2;
6641 break;
6642 case E_H8_MACH_H8300H:
6643 case E_H8_MACH_H8300S:
6644 case E_H8_MACH_H8300SX:
6645 eh_addr_size = 4;
6646 break;
6647 }
6648 break;
6649
6650 case EM_M32C_OLD:
6651 case EM_M32C:
6652 switch (filedata->file_header.e_flags & EF_M32C_CPU_MASK)
6653 {
6654 case EF_M32C_CPU_M16C:
6655 eh_addr_size = 2;
6656 break;
6657 }
6658 break;
6659 }
6660
6661 #define CHECK_ENTSIZE_VALUES(section, i, size32, size64) \
6662 do \
6663 { \
6664 bfd_size_type expected_entsize = is_32bit_elf ? size32 : size64; \
6665 if (section->sh_entsize != expected_entsize) \
6666 { \
6667 char buf[40]; \
6668 sprintf_vma (buf, section->sh_entsize); \
6669 /* Note: coded this way so that there is a single string for \
6670 translation. */ \
6671 error (_("Section %d has invalid sh_entsize of %s\n"), i, buf); \
6672 error (_("(Using the expected size of %u for the rest of this dump)\n"), \
6673 (unsigned) expected_entsize); \
6674 section->sh_entsize = expected_entsize; \
6675 } \
6676 } \
6677 while (0)
6678
6679 #define CHECK_ENTSIZE(section, i, type) \
6680 CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type), \
6681 sizeof (Elf64_External_##type))
6682
6683 for (i = 0, section = filedata->section_headers;
6684 i < filedata->file_header.e_shnum;
6685 i++, section++)
6686 {
6687 char * name = SECTION_NAME_PRINT (section);
6688
6689 /* Run some sanity checks on the headers and
6690 possibly fill in some file data as well. */
6691 switch (section->sh_type)
6692 {
6693 case SHT_DYNSYM:
6694 if (filedata->dynamic_symbols != NULL)
6695 {
6696 error (_("File contains multiple dynamic symbol tables\n"));
6697 continue;
6698 }
6699
6700 CHECK_ENTSIZE (section, i, Sym);
6701 filedata->dynamic_symbols
6702 = get_elf_symbols (filedata, section, &filedata->num_dynamic_syms);
6703 filedata->dynamic_symtab_section = section;
6704 break;
6705
6706 case SHT_STRTAB:
6707 if (streq (name, ".dynstr"))
6708 {
6709 if (filedata->dynamic_strings != NULL)
6710 {
6711 error (_("File contains multiple dynamic string tables\n"));
6712 continue;
6713 }
6714
6715 filedata->dynamic_strings
6716 = (char *) get_data (NULL, filedata, section->sh_offset,
6717 1, section->sh_size, _("dynamic strings"));
6718 filedata->dynamic_strings_length
6719 = filedata->dynamic_strings == NULL ? 0 : section->sh_size;
6720 filedata->dynamic_strtab_section = section;
6721 }
6722 break;
6723
6724 case SHT_SYMTAB_SHNDX:
6725 {
6726 elf_section_list * entry = xmalloc (sizeof * entry);
6727
6728 entry->hdr = section;
6729 entry->next = filedata->symtab_shndx_list;
6730 filedata->symtab_shndx_list = entry;
6731 }
6732 break;
6733
6734 case SHT_SYMTAB:
6735 CHECK_ENTSIZE (section, i, Sym);
6736 break;
6737
6738 case SHT_GROUP:
6739 CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
6740 break;
6741
6742 case SHT_REL:
6743 CHECK_ENTSIZE (section, i, Rel);
6744 if (do_checks && section->sh_size == 0)
6745 warn (_("Section '%s': zero-sized relocation section\n"), name);
6746 break;
6747
6748 case SHT_RELA:
6749 CHECK_ENTSIZE (section, i, Rela);
6750 if (do_checks && section->sh_size == 0)
6751 warn (_("Section '%s': zero-sized relocation section\n"), name);
6752 break;
6753
6754 case SHT_NOTE:
6755 case SHT_PROGBITS:
6756 /* Having a zero sized section is not illegal according to the
6757 ELF standard, but it might be an indication that something
6758 is wrong. So issue a warning if we are running in lint mode. */
6759 if (do_checks && section->sh_size == 0)
6760 warn (_("Section '%s': has a size of zero - is this intended ?\n"), name);
6761 break;
6762
6763 default:
6764 break;
6765 }
6766
6767 if ((do_debugging || do_debug_info || do_debug_abbrevs
6768 || do_debug_lines || do_debug_pubnames || do_debug_pubtypes
6769 || do_debug_aranges || do_debug_frames || do_debug_macinfo
6770 || do_debug_str || do_debug_str_offsets || do_debug_loc
6771 || do_debug_ranges
6772 || do_debug_addr || do_debug_cu_index || do_debug_links)
6773 && (startswith (name, ".debug_")
6774 || startswith (name, ".zdebug_")))
6775 {
6776 if (name[1] == 'z')
6777 name += sizeof (".zdebug_") - 1;
6778 else
6779 name += sizeof (".debug_") - 1;
6780
6781 if (do_debugging
6782 || (do_debug_info && startswith (name, "info"))
6783 || (do_debug_info && startswith (name, "types"))
6784 || (do_debug_abbrevs && startswith (name, "abbrev"))
6785 || (do_debug_lines && strcmp (name, "line") == 0)
6786 || (do_debug_lines && startswith (name, "line."))
6787 || (do_debug_pubnames && startswith (name, "pubnames"))
6788 || (do_debug_pubtypes && startswith (name, "pubtypes"))
6789 || (do_debug_pubnames && startswith (name, "gnu_pubnames"))
6790 || (do_debug_pubtypes && startswith (name, "gnu_pubtypes"))
6791 || (do_debug_aranges && startswith (name, "aranges"))
6792 || (do_debug_ranges && startswith (name, "ranges"))
6793 || (do_debug_ranges && startswith (name, "rnglists"))
6794 || (do_debug_frames && startswith (name, "frame"))
6795 || (do_debug_macinfo && startswith (name, "macinfo"))
6796 || (do_debug_macinfo && startswith (name, "macro"))
6797 || (do_debug_str && startswith (name, "str"))
6798 || (do_debug_links && startswith (name, "sup"))
6799 || (do_debug_str_offsets && startswith (name, "str_offsets"))
6800 || (do_debug_loc && startswith (name, "loc"))
6801 || (do_debug_loc && startswith (name, "loclists"))
6802 || (do_debug_addr && startswith (name, "addr"))
6803 || (do_debug_cu_index && startswith (name, "cu_index"))
6804 || (do_debug_cu_index && startswith (name, "tu_index"))
6805 )
6806 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6807 }
6808 /* Linkonce section to be combined with .debug_info at link time. */
6809 else if ((do_debugging || do_debug_info)
6810 && startswith (name, ".gnu.linkonce.wi."))
6811 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6812 else if (do_debug_frames && streq (name, ".eh_frame"))
6813 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6814 else if (do_gdb_index && (streq (name, ".gdb_index")
6815 || streq (name, ".debug_names")))
6816 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6817 /* Trace sections for Itanium VMS. */
6818 else if ((do_debugging || do_trace_info || do_trace_abbrevs
6819 || do_trace_aranges)
6820 && startswith (name, ".trace_"))
6821 {
6822 name += sizeof (".trace_") - 1;
6823
6824 if (do_debugging
6825 || (do_trace_info && streq (name, "info"))
6826 || (do_trace_abbrevs && streq (name, "abbrev"))
6827 || (do_trace_aranges && streq (name, "aranges"))
6828 )
6829 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6830 }
6831 else if ((do_debugging || do_debug_links)
6832 && (startswith (name, ".gnu_debuglink")
6833 || startswith (name, ".gnu_debugaltlink")))
6834 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6835 }
6836
6837 if (! do_sections)
6838 return true;
6839
6840 if (filedata->is_separate && ! process_links)
6841 return true;
6842
6843 if (filedata->is_separate)
6844 printf (_("\nSection Headers in linked file '%s':\n"), filedata->file_name);
6845 else if (filedata->file_header.e_shnum > 1)
6846 printf (_("\nSection Headers:\n"));
6847 else
6848 printf (_("\nSection Header:\n"));
6849
6850 if (is_32bit_elf)
6851 {
6852 if (do_section_details)
6853 {
6854 printf (_(" [Nr] Name\n"));
6855 printf (_(" Type Addr Off Size ES Lk Inf Al\n"));
6856 }
6857 else
6858 printf
6859 (_(" [Nr] Name Type Addr Off Size ES Flg Lk Inf Al\n"));
6860 }
6861 else if (do_wide)
6862 {
6863 if (do_section_details)
6864 {
6865 printf (_(" [Nr] Name\n"));
6866 printf (_(" Type Address Off Size ES Lk Inf Al\n"));
6867 }
6868 else
6869 printf
6870 (_(" [Nr] Name Type Address Off Size ES Flg Lk Inf Al\n"));
6871 }
6872 else
6873 {
6874 if (do_section_details)
6875 {
6876 printf (_(" [Nr] Name\n"));
6877 printf (_(" Type Address Offset Link\n"));
6878 printf (_(" Size EntSize Info Align\n"));
6879 }
6880 else
6881 {
6882 printf (_(" [Nr] Name Type Address Offset\n"));
6883 printf (_(" Size EntSize Flags Link Info Align\n"));
6884 }
6885 }
6886
6887 if (do_section_details)
6888 printf (_(" Flags\n"));
6889
6890 for (i = 0, section = filedata->section_headers;
6891 i < filedata->file_header.e_shnum;
6892 i++, section++)
6893 {
6894 /* Run some sanity checks on the section header. */
6895
6896 /* Check the sh_link field. */
6897 switch (section->sh_type)
6898 {
6899 case SHT_REL:
6900 case SHT_RELA:
6901 if (section->sh_link == 0
6902 && (filedata->file_header.e_type == ET_EXEC
6903 || filedata->file_header.e_type == ET_DYN))
6904 /* A dynamic relocation section where all entries use a
6905 zero symbol index need not specify a symtab section. */
6906 break;
6907 /* Fall through. */
6908 case SHT_SYMTAB_SHNDX:
6909 case SHT_GROUP:
6910 case SHT_HASH:
6911 case SHT_GNU_HASH:
6912 case SHT_GNU_versym:
6913 if (section->sh_link == 0
6914 || section->sh_link >= filedata->file_header.e_shnum
6915 || (filedata->section_headers[section->sh_link].sh_type != SHT_SYMTAB
6916 && filedata->section_headers[section->sh_link].sh_type != SHT_DYNSYM))
6917 warn (_("[%2u]: Link field (%u) should index a symtab section.\n"),
6918 i, section->sh_link);
6919 break;
6920
6921 case SHT_DYNAMIC:
6922 case SHT_SYMTAB:
6923 case SHT_DYNSYM:
6924 case SHT_GNU_verneed:
6925 case SHT_GNU_verdef:
6926 case SHT_GNU_LIBLIST:
6927 if (section->sh_link == 0
6928 || section->sh_link >= filedata->file_header.e_shnum
6929 || filedata->section_headers[section->sh_link].sh_type != SHT_STRTAB)
6930 warn (_("[%2u]: Link field (%u) should index a string section.\n"),
6931 i, section->sh_link);
6932 break;
6933
6934 case SHT_INIT_ARRAY:
6935 case SHT_FINI_ARRAY:
6936 case SHT_PREINIT_ARRAY:
6937 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
6938 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
6939 i, section->sh_link);
6940 break;
6941
6942 default:
6943 /* FIXME: Add support for target specific section types. */
6944 #if 0 /* Currently we do not check other section types as there are too
6945 many special cases. Stab sections for example have a type
6946 of SHT_PROGBITS but an sh_link field that links to the .stabstr
6947 section. */
6948 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
6949 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
6950 i, section->sh_link);
6951 #endif
6952 break;
6953 }
6954
6955 /* Check the sh_info field. */
6956 switch (section->sh_type)
6957 {
6958 case SHT_REL:
6959 case SHT_RELA:
6960 if (section->sh_info == 0
6961 && (filedata->file_header.e_type == ET_EXEC
6962 || filedata->file_header.e_type == ET_DYN))
6963 /* Dynamic relocations apply to segments, so they do not
6964 need to specify the section they relocate. */
6965 break;
6966 if (section->sh_info == 0
6967 || section->sh_info >= filedata->file_header.e_shnum
6968 || (filedata->section_headers[section->sh_info].sh_type != SHT_PROGBITS
6969 && filedata->section_headers[section->sh_info].sh_type != SHT_NOBITS
6970 && filedata->section_headers[section->sh_info].sh_type != SHT_NOTE
6971 && filedata->section_headers[section->sh_info].sh_type != SHT_INIT_ARRAY
6972 && filedata->section_headers[section->sh_info].sh_type != SHT_FINI_ARRAY
6973 && filedata->section_headers[section->sh_info].sh_type != SHT_PREINIT_ARRAY
6974 /* FIXME: Are other section types valid ? */
6975 && filedata->section_headers[section->sh_info].sh_type < SHT_LOOS))
6976 warn (_("[%2u]: Info field (%u) should index a relocatable section.\n"),
6977 i, section->sh_info);
6978 break;
6979
6980 case SHT_DYNAMIC:
6981 case SHT_HASH:
6982 case SHT_SYMTAB_SHNDX:
6983 case SHT_INIT_ARRAY:
6984 case SHT_FINI_ARRAY:
6985 case SHT_PREINIT_ARRAY:
6986 if (section->sh_info != 0)
6987 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
6988 i, section->sh_info);
6989 break;
6990
6991 case SHT_GROUP:
6992 case SHT_SYMTAB:
6993 case SHT_DYNSYM:
6994 /* A symbol index - we assume that it is valid. */
6995 break;
6996
6997 default:
6998 /* FIXME: Add support for target specific section types. */
6999 if (section->sh_type == SHT_NOBITS)
7000 /* NOBITS section headers with non-zero sh_info fields can be
7001 created when a binary is stripped of everything but its debug
7002 information. The stripped sections have their headers
7003 preserved but their types set to SHT_NOBITS. So do not check
7004 this type of section. */
7005 ;
7006 else if (section->sh_flags & SHF_INFO_LINK)
7007 {
7008 if (section->sh_info < 1 || section->sh_info >= filedata->file_header.e_shnum)
7009 warn (_("[%2u]: Expected link to another section in info field"), i);
7010 }
7011 else if (section->sh_type < SHT_LOOS
7012 && (section->sh_flags & SHF_GNU_MBIND) == 0
7013 && section->sh_info != 0)
7014 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
7015 i, section->sh_info);
7016 break;
7017 }
7018
7019 /* Check the sh_size field. */
7020 if (section->sh_size > filedata->file_size
7021 && section->sh_type != SHT_NOBITS
7022 && section->sh_type != SHT_NULL
7023 && section->sh_type < SHT_LOOS)
7024 warn (_("Size of section %u is larger than the entire file!\n"), i);
7025
7026 printf (" [%2u] ", i);
7027 if (do_section_details)
7028 printf ("%s\n ", printable_section_name (filedata, section));
7029 else
7030 print_symbol (-17, SECTION_NAME_PRINT (section));
7031
7032 printf (do_wide ? " %-15s " : " %-15.15s ",
7033 get_section_type_name (filedata, section->sh_type));
7034
7035 if (is_32bit_elf)
7036 {
7037 const char * link_too_big = NULL;
7038
7039 print_vma (section->sh_addr, LONG_HEX);
7040
7041 printf ( " %6.6lx %6.6lx %2.2lx",
7042 (unsigned long) section->sh_offset,
7043 (unsigned long) section->sh_size,
7044 (unsigned long) section->sh_entsize);
7045
7046 if (do_section_details)
7047 fputs (" ", stdout);
7048 else
7049 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7050
7051 if (section->sh_link >= filedata->file_header.e_shnum)
7052 {
7053 link_too_big = "";
7054 /* The sh_link value is out of range. Normally this indicates
7055 an error but it can have special values in Solaris binaries. */
7056 switch (filedata->file_header.e_machine)
7057 {
7058 case EM_386:
7059 case EM_IAMCU:
7060 case EM_X86_64:
7061 case EM_L1OM:
7062 case EM_K1OM:
7063 case EM_OLD_SPARCV9:
7064 case EM_SPARC32PLUS:
7065 case EM_SPARCV9:
7066 case EM_SPARC:
7067 if (section->sh_link == (SHN_BEFORE & 0xffff))
7068 link_too_big = "BEFORE";
7069 else if (section->sh_link == (SHN_AFTER & 0xffff))
7070 link_too_big = "AFTER";
7071 break;
7072 default:
7073 break;
7074 }
7075 }
7076
7077 if (do_section_details)
7078 {
7079 if (link_too_big != NULL && * link_too_big)
7080 printf ("<%s> ", link_too_big);
7081 else
7082 printf ("%2u ", section->sh_link);
7083 printf ("%3u %2lu\n", section->sh_info,
7084 (unsigned long) section->sh_addralign);
7085 }
7086 else
7087 printf ("%2u %3u %2lu\n",
7088 section->sh_link,
7089 section->sh_info,
7090 (unsigned long) section->sh_addralign);
7091
7092 if (link_too_big && ! * link_too_big)
7093 warn (_("section %u: sh_link value of %u is larger than the number of sections\n"),
7094 i, section->sh_link);
7095 }
7096 else if (do_wide)
7097 {
7098 print_vma (section->sh_addr, LONG_HEX);
7099
7100 if ((long) section->sh_offset == section->sh_offset)
7101 printf (" %6.6lx", (unsigned long) section->sh_offset);
7102 else
7103 {
7104 putchar (' ');
7105 print_vma (section->sh_offset, LONG_HEX);
7106 }
7107
7108 if ((unsigned long) section->sh_size == section->sh_size)
7109 printf (" %6.6lx", (unsigned long) section->sh_size);
7110 else
7111 {
7112 putchar (' ');
7113 print_vma (section->sh_size, LONG_HEX);
7114 }
7115
7116 if ((unsigned long) section->sh_entsize == section->sh_entsize)
7117 printf (" %2.2lx", (unsigned long) section->sh_entsize);
7118 else
7119 {
7120 putchar (' ');
7121 print_vma (section->sh_entsize, LONG_HEX);
7122 }
7123
7124 if (do_section_details)
7125 fputs (" ", stdout);
7126 else
7127 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7128
7129 printf ("%2u %3u ", section->sh_link, section->sh_info);
7130
7131 if ((unsigned long) section->sh_addralign == section->sh_addralign)
7132 printf ("%2lu\n", (unsigned long) section->sh_addralign);
7133 else
7134 {
7135 print_vma (section->sh_addralign, DEC);
7136 putchar ('\n');
7137 }
7138 }
7139 else if (do_section_details)
7140 {
7141 putchar (' ');
7142 print_vma (section->sh_addr, LONG_HEX);
7143 if ((long) section->sh_offset == section->sh_offset)
7144 printf (" %16.16lx", (unsigned long) section->sh_offset);
7145 else
7146 {
7147 printf (" ");
7148 print_vma (section->sh_offset, LONG_HEX);
7149 }
7150 printf (" %u\n ", section->sh_link);
7151 print_vma (section->sh_size, LONG_HEX);
7152 putchar (' ');
7153 print_vma (section->sh_entsize, LONG_HEX);
7154
7155 printf (" %-16u %lu\n",
7156 section->sh_info,
7157 (unsigned long) section->sh_addralign);
7158 }
7159 else
7160 {
7161 putchar (' ');
7162 print_vma (section->sh_addr, LONG_HEX);
7163 if ((long) section->sh_offset == section->sh_offset)
7164 printf (" %8.8lx", (unsigned long) section->sh_offset);
7165 else
7166 {
7167 printf (" ");
7168 print_vma (section->sh_offset, LONG_HEX);
7169 }
7170 printf ("\n ");
7171 print_vma (section->sh_size, LONG_HEX);
7172 printf (" ");
7173 print_vma (section->sh_entsize, LONG_HEX);
7174
7175 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7176
7177 printf (" %2u %3u %lu\n",
7178 section->sh_link,
7179 section->sh_info,
7180 (unsigned long) section->sh_addralign);
7181 }
7182
7183 if (do_section_details)
7184 {
7185 printf (" %s\n", get_elf_section_flags (filedata, section->sh_flags));
7186 if ((section->sh_flags & SHF_COMPRESSED) != 0)
7187 {
7188 /* Minimum section size is 12 bytes for 32-bit compression
7189 header + 12 bytes for compressed data header. */
7190 unsigned char buf[24];
7191
7192 assert (sizeof (buf) >= sizeof (Elf64_External_Chdr));
7193 if (get_data (&buf, filedata, section->sh_offset, 1,
7194 sizeof (buf), _("compression header")))
7195 {
7196 Elf_Internal_Chdr chdr;
7197
7198 if (get_compression_header (&chdr, buf, sizeof (buf)) == 0)
7199 printf (_(" [<corrupt>]\n"));
7200 else
7201 {
7202 if (chdr.ch_type == ELFCOMPRESS_ZLIB)
7203 printf (" ZLIB, ");
7204 else
7205 printf (_(" [<unknown>: 0x%x], "),
7206 chdr.ch_type);
7207 print_vma (chdr.ch_size, LONG_HEX);
7208 printf (", %lu\n", (unsigned long) chdr.ch_addralign);
7209 }
7210 }
7211 }
7212 }
7213 }
7214
7215 if (!do_section_details)
7216 {
7217 /* The ordering of the letters shown here matches the ordering of the
7218 corresponding SHF_xxx values, and hence the order in which these
7219 letters will be displayed to the user. */
7220 printf (_("Key to Flags:\n\
7221 W (write), A (alloc), X (execute), M (merge), S (strings), I (info),\n\
7222 L (link order), O (extra OS processing required), G (group), T (TLS),\n\
7223 C (compressed), x (unknown), o (OS specific), E (exclude),\n "));
7224 switch (filedata->file_header.e_ident[EI_OSABI])
7225 {
7226 case ELFOSABI_GNU:
7227 case ELFOSABI_FREEBSD:
7228 printf (_("R (retain), "));
7229 /* Fall through */
7230 case ELFOSABI_NONE:
7231 printf (_("D (mbind), "));
7232 break;
7233 default:
7234 break;
7235 }
7236 if (filedata->file_header.e_machine == EM_X86_64
7237 || filedata->file_header.e_machine == EM_L1OM
7238 || filedata->file_header.e_machine == EM_K1OM)
7239 printf (_("l (large), "));
7240 else if (filedata->file_header.e_machine == EM_ARM)
7241 printf (_("y (purecode), "));
7242 else if (filedata->file_header.e_machine == EM_PPC)
7243 printf (_("v (VLE), "));
7244 printf ("p (processor specific)\n");
7245 }
7246
7247 return true;
7248 }
7249
7250 static bool
7251 get_symtab (Filedata *filedata, Elf_Internal_Shdr *symsec,
7252 Elf_Internal_Sym **symtab, unsigned long *nsyms,
7253 char **strtab, unsigned long *strtablen)
7254 {
7255 *strtab = NULL;
7256 *strtablen = 0;
7257 *symtab = get_elf_symbols (filedata, symsec, nsyms);
7258
7259 if (*symtab == NULL)
7260 return false;
7261
7262 if (symsec->sh_link != 0)
7263 {
7264 Elf_Internal_Shdr *strsec;
7265
7266 if (symsec->sh_link >= filedata->file_header.e_shnum)
7267 {
7268 error (_("Bad sh_link in symbol table section\n"));
7269 free (*symtab);
7270 *symtab = NULL;
7271 *nsyms = 0;
7272 return false;
7273 }
7274
7275 strsec = filedata->section_headers + symsec->sh_link;
7276
7277 *strtab = (char *) get_data (NULL, filedata, strsec->sh_offset,
7278 1, strsec->sh_size, _("string table"));
7279 if (*strtab == NULL)
7280 {
7281 free (*symtab);
7282 *symtab = NULL;
7283 *nsyms = 0;
7284 return false;
7285 }
7286 *strtablen = strsec->sh_size;
7287 }
7288 return true;
7289 }
7290
7291 static const char *
7292 get_group_flags (unsigned int flags)
7293 {
7294 static char buff[128];
7295
7296 if (flags == 0)
7297 return "";
7298 else if (flags == GRP_COMDAT)
7299 return "COMDAT ";
7300
7301 snprintf (buff, sizeof buff, "[0x%x: %s%s%s]",
7302 flags,
7303 flags & GRP_MASKOS ? _("<OS specific>") : "",
7304 flags & GRP_MASKPROC ? _("<PROC specific>") : "",
7305 (flags & ~(GRP_COMDAT | GRP_MASKOS | GRP_MASKPROC)
7306 ? _("<unknown>") : ""));
7307
7308 return buff;
7309 }
7310
7311 static bool
7312 process_section_groups (Filedata * filedata)
7313 {
7314 Elf_Internal_Shdr * section;
7315 unsigned int i;
7316 struct group * group;
7317 Elf_Internal_Shdr * symtab_sec;
7318 Elf_Internal_Shdr * strtab_sec;
7319 Elf_Internal_Sym * symtab;
7320 unsigned long num_syms;
7321 char * strtab;
7322 size_t strtab_size;
7323
7324 /* Don't process section groups unless needed. */
7325 if (!do_unwind && !do_section_groups)
7326 return true;
7327
7328 if (filedata->file_header.e_shnum == 0)
7329 {
7330 if (do_section_groups)
7331 {
7332 if (filedata->is_separate)
7333 printf (_("\nThere are no sections group in linked file '%s'.\n"),
7334 filedata->file_name);
7335 else
7336 printf (_("\nThere are no section groups in this file.\n"));
7337 }
7338 return true;
7339 }
7340
7341 if (filedata->section_headers == NULL)
7342 {
7343 error (_("Section headers are not available!\n"));
7344 /* PR 13622: This can happen with a corrupt ELF header. */
7345 return false;
7346 }
7347
7348 filedata->section_headers_groups
7349 = (struct group **) calloc (filedata->file_header.e_shnum,
7350 sizeof (struct group *));
7351
7352 if (filedata->section_headers_groups == NULL)
7353 {
7354 error (_("Out of memory reading %u section group headers\n"),
7355 filedata->file_header.e_shnum);
7356 return false;
7357 }
7358
7359 /* Scan the sections for the group section. */
7360 filedata->group_count = 0;
7361 for (i = 0, section = filedata->section_headers;
7362 i < filedata->file_header.e_shnum;
7363 i++, section++)
7364 if (section->sh_type == SHT_GROUP)
7365 filedata->group_count++;
7366
7367 if (filedata->group_count == 0)
7368 {
7369 if (do_section_groups)
7370 {
7371 if (filedata->is_separate)
7372 printf (_("\nThere are no section groups in linked file '%s'.\n"),
7373 filedata->file_name);
7374 else
7375 printf (_("\nThere are no section groups in this file.\n"));
7376 }
7377
7378 return true;
7379 }
7380
7381 filedata->section_groups = (struct group *) calloc (filedata->group_count,
7382 sizeof (struct group));
7383
7384 if (filedata->section_groups == NULL)
7385 {
7386 error (_("Out of memory reading %lu groups\n"),
7387 (unsigned long) filedata->group_count);
7388 return false;
7389 }
7390
7391 symtab_sec = NULL;
7392 strtab_sec = NULL;
7393 symtab = NULL;
7394 num_syms = 0;
7395 strtab = NULL;
7396 strtab_size = 0;
7397
7398 if (filedata->is_separate)
7399 printf (_("Section groups in linked file '%s'\n"), filedata->file_name);
7400
7401 for (i = 0, section = filedata->section_headers, group = filedata->section_groups;
7402 i < filedata->file_header.e_shnum;
7403 i++, section++)
7404 {
7405 if (section->sh_type == SHT_GROUP)
7406 {
7407 const char * name = printable_section_name (filedata, section);
7408 const char * group_name;
7409 unsigned char * start;
7410 unsigned char * indices;
7411 unsigned int entry, j, size;
7412 Elf_Internal_Shdr * sec;
7413 Elf_Internal_Sym * sym;
7414
7415 /* Get the symbol table. */
7416 if (section->sh_link >= filedata->file_header.e_shnum
7417 || ((sec = filedata->section_headers + section->sh_link)->sh_type
7418 != SHT_SYMTAB))
7419 {
7420 error (_("Bad sh_link in group section `%s'\n"), name);
7421 continue;
7422 }
7423
7424 if (symtab_sec != sec)
7425 {
7426 symtab_sec = sec;
7427 free (symtab);
7428 symtab = get_elf_symbols (filedata, symtab_sec, & num_syms);
7429 }
7430
7431 if (symtab == NULL)
7432 {
7433 error (_("Corrupt header in group section `%s'\n"), name);
7434 continue;
7435 }
7436
7437 if (section->sh_info >= num_syms)
7438 {
7439 error (_("Bad sh_info in group section `%s'\n"), name);
7440 continue;
7441 }
7442
7443 sym = symtab + section->sh_info;
7444
7445 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
7446 {
7447 if (sym->st_shndx == 0
7448 || sym->st_shndx >= filedata->file_header.e_shnum)
7449 {
7450 error (_("Bad sh_info in group section `%s'\n"), name);
7451 continue;
7452 }
7453
7454 group_name = SECTION_NAME_PRINT (filedata->section_headers
7455 + sym->st_shndx);
7456 strtab_sec = NULL;
7457 free (strtab);
7458 strtab = NULL;
7459 strtab_size = 0;
7460 }
7461 else
7462 {
7463 /* Get the string table. */
7464 if (symtab_sec->sh_link >= filedata->file_header.e_shnum)
7465 {
7466 strtab_sec = NULL;
7467 free (strtab);
7468 strtab = NULL;
7469 strtab_size = 0;
7470 }
7471 else if (strtab_sec
7472 != (sec = filedata->section_headers + symtab_sec->sh_link))
7473 {
7474 strtab_sec = sec;
7475 free (strtab);
7476
7477 strtab = (char *) get_data (NULL, filedata, strtab_sec->sh_offset,
7478 1, strtab_sec->sh_size,
7479 _("string table"));
7480 strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
7481 }
7482 group_name = sym->st_name < strtab_size
7483 ? strtab + sym->st_name : _("<corrupt>");
7484 }
7485
7486 /* PR 17531: file: loop. */
7487 if (section->sh_entsize > section->sh_size)
7488 {
7489 error (_("Section %s has sh_entsize (0x%lx) which is larger than its size (0x%lx)\n"),
7490 printable_section_name (filedata, section),
7491 (unsigned long) section->sh_entsize,
7492 (unsigned long) section->sh_size);
7493 continue;
7494 }
7495
7496 start = (unsigned char *) get_data (NULL, filedata, section->sh_offset,
7497 1, section->sh_size,
7498 _("section data"));
7499 if (start == NULL)
7500 continue;
7501
7502 indices = start;
7503 size = (section->sh_size / section->sh_entsize) - 1;
7504 entry = byte_get (indices, 4);
7505 indices += 4;
7506
7507 if (do_section_groups)
7508 {
7509 printf (_("\n%sgroup section [%5u] `%s' [%s] contains %u sections:\n"),
7510 get_group_flags (entry), i, name, group_name, size);
7511
7512 printf (_(" [Index] Name\n"));
7513 }
7514
7515 group->group_index = i;
7516
7517 for (j = 0; j < size; j++)
7518 {
7519 struct group_list * g;
7520
7521 entry = byte_get (indices, 4);
7522 indices += 4;
7523
7524 if (entry >= filedata->file_header.e_shnum)
7525 {
7526 static unsigned num_group_errors = 0;
7527
7528 if (num_group_errors ++ < 10)
7529 {
7530 error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
7531 entry, i, filedata->file_header.e_shnum - 1);
7532 if (num_group_errors == 10)
7533 warn (_("Further error messages about overlarge group section indices suppressed\n"));
7534 }
7535 continue;
7536 }
7537
7538 if (filedata->section_headers_groups [entry] != NULL)
7539 {
7540 if (entry)
7541 {
7542 static unsigned num_errs = 0;
7543
7544 if (num_errs ++ < 10)
7545 {
7546 error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
7547 entry, i,
7548 filedata->section_headers_groups [entry]->group_index);
7549 if (num_errs == 10)
7550 warn (_("Further error messages about already contained group sections suppressed\n"));
7551 }
7552 continue;
7553 }
7554 else
7555 {
7556 /* Intel C/C++ compiler may put section 0 in a
7557 section group. We just warn it the first time
7558 and ignore it afterwards. */
7559 static bool warned = false;
7560 if (!warned)
7561 {
7562 error (_("section 0 in group section [%5u]\n"),
7563 filedata->section_headers_groups [entry]->group_index);
7564 warned = true;
7565 }
7566 }
7567 }
7568
7569 filedata->section_headers_groups [entry] = group;
7570
7571 if (do_section_groups)
7572 {
7573 sec = filedata->section_headers + entry;
7574 printf (" [%5u] %s\n", entry, printable_section_name (filedata, sec));
7575 }
7576
7577 g = (struct group_list *) xmalloc (sizeof (struct group_list));
7578 g->section_index = entry;
7579 g->next = group->root;
7580 group->root = g;
7581 }
7582
7583 free (start);
7584
7585 group++;
7586 }
7587 }
7588
7589 free (symtab);
7590 free (strtab);
7591 return true;
7592 }
7593
7594 /* Data used to display dynamic fixups. */
7595
7596 struct ia64_vms_dynfixup
7597 {
7598 bfd_vma needed_ident; /* Library ident number. */
7599 bfd_vma needed; /* Index in the dstrtab of the library name. */
7600 bfd_vma fixup_needed; /* Index of the library. */
7601 bfd_vma fixup_rela_cnt; /* Number of fixups. */
7602 bfd_vma fixup_rela_off; /* Fixups offset in the dynamic segment. */
7603 };
7604
7605 /* Data used to display dynamic relocations. */
7606
7607 struct ia64_vms_dynimgrela
7608 {
7609 bfd_vma img_rela_cnt; /* Number of relocations. */
7610 bfd_vma img_rela_off; /* Reloc offset in the dynamic segment. */
7611 };
7612
7613 /* Display IA-64 OpenVMS dynamic fixups (used to dynamically link a shared
7614 library). */
7615
7616 static bool
7617 dump_ia64_vms_dynamic_fixups (Filedata * filedata,
7618 struct ia64_vms_dynfixup * fixup,
7619 const char * strtab,
7620 unsigned int strtab_sz)
7621 {
7622 Elf64_External_VMS_IMAGE_FIXUP * imfs;
7623 long i;
7624 const char * lib_name;
7625
7626 imfs = get_data (NULL, filedata,
7627 filedata->dynamic_addr + fixup->fixup_rela_off,
7628 sizeof (*imfs), fixup->fixup_rela_cnt,
7629 _("dynamic section image fixups"));
7630 if (!imfs)
7631 return false;
7632
7633 if (fixup->needed < strtab_sz)
7634 lib_name = strtab + fixup->needed;
7635 else
7636 {
7637 warn (_("corrupt library name index of 0x%lx found in dynamic entry"),
7638 (unsigned long) fixup->needed);
7639 lib_name = "???";
7640 }
7641
7642 printf (_("\nImage fixups for needed library #%d: %s - ident: %lx\n"),
7643 (int) fixup->fixup_needed, lib_name, (long) fixup->needed_ident);
7644 printf
7645 (_("Seg Offset Type SymVec DataType\n"));
7646
7647 for (i = 0; i < (long) fixup->fixup_rela_cnt; i++)
7648 {
7649 unsigned int type;
7650 const char *rtype;
7651
7652 printf ("%3u ", (unsigned) BYTE_GET (imfs [i].fixup_seg));
7653 printf_vma ((bfd_vma) BYTE_GET (imfs [i].fixup_offset));
7654 type = BYTE_GET (imfs [i].type);
7655 rtype = elf_ia64_reloc_type (type);
7656 if (rtype == NULL)
7657 printf (" 0x%08x ", type);
7658 else
7659 printf (" %-32s ", rtype);
7660 printf ("%6u ", (unsigned) BYTE_GET (imfs [i].symvec_index));
7661 printf ("0x%08x\n", (unsigned) BYTE_GET (imfs [i].data_type));
7662 }
7663
7664 free (imfs);
7665 return true;
7666 }
7667
7668 /* Display IA-64 OpenVMS dynamic relocations (used to relocate an image). */
7669
7670 static bool
7671 dump_ia64_vms_dynamic_relocs (Filedata * filedata, struct ia64_vms_dynimgrela *imgrela)
7672 {
7673 Elf64_External_VMS_IMAGE_RELA *imrs;
7674 long i;
7675
7676 imrs = get_data (NULL, filedata,
7677 filedata->dynamic_addr + imgrela->img_rela_off,
7678 sizeof (*imrs), imgrela->img_rela_cnt,
7679 _("dynamic section image relocations"));
7680 if (!imrs)
7681 return false;
7682
7683 printf (_("\nImage relocs\n"));
7684 printf
7685 (_("Seg Offset Type Addend Seg Sym Off\n"));
7686
7687 for (i = 0; i < (long) imgrela->img_rela_cnt; i++)
7688 {
7689 unsigned int type;
7690 const char *rtype;
7691
7692 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].rela_seg));
7693 printf ("%08" BFD_VMA_FMT "x ",
7694 (bfd_vma) BYTE_GET (imrs [i].rela_offset));
7695 type = BYTE_GET (imrs [i].type);
7696 rtype = elf_ia64_reloc_type (type);
7697 if (rtype == NULL)
7698 printf ("0x%08x ", type);
7699 else
7700 printf ("%-31s ", rtype);
7701 print_vma (BYTE_GET (imrs [i].addend), FULL_HEX);
7702 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].sym_seg));
7703 printf ("%08" BFD_VMA_FMT "x\n",
7704 (bfd_vma) BYTE_GET (imrs [i].sym_offset));
7705 }
7706
7707 free (imrs);
7708 return true;
7709 }
7710
7711 /* Display IA-64 OpenVMS dynamic relocations and fixups. */
7712
7713 static bool
7714 process_ia64_vms_dynamic_relocs (Filedata * filedata)
7715 {
7716 struct ia64_vms_dynfixup fixup;
7717 struct ia64_vms_dynimgrela imgrela;
7718 Elf_Internal_Dyn *entry;
7719 bfd_vma strtab_off = 0;
7720 bfd_vma strtab_sz = 0;
7721 char *strtab = NULL;
7722 bool res = true;
7723
7724 memset (&fixup, 0, sizeof (fixup));
7725 memset (&imgrela, 0, sizeof (imgrela));
7726
7727 /* Note: the order of the entries is specified by the OpenVMS specs. */
7728 for (entry = filedata->dynamic_section;
7729 entry < filedata->dynamic_section + filedata->dynamic_nent;
7730 entry++)
7731 {
7732 switch (entry->d_tag)
7733 {
7734 case DT_IA_64_VMS_STRTAB_OFFSET:
7735 strtab_off = entry->d_un.d_val;
7736 break;
7737 case DT_STRSZ:
7738 strtab_sz = entry->d_un.d_val;
7739 if (strtab == NULL)
7740 strtab = get_data (NULL, filedata,
7741 filedata->dynamic_addr + strtab_off,
7742 1, strtab_sz, _("dynamic string section"));
7743 if (strtab == NULL)
7744 strtab_sz = 0;
7745 break;
7746
7747 case DT_IA_64_VMS_NEEDED_IDENT:
7748 fixup.needed_ident = entry->d_un.d_val;
7749 break;
7750 case DT_NEEDED:
7751 fixup.needed = entry->d_un.d_val;
7752 break;
7753 case DT_IA_64_VMS_FIXUP_NEEDED:
7754 fixup.fixup_needed = entry->d_un.d_val;
7755 break;
7756 case DT_IA_64_VMS_FIXUP_RELA_CNT:
7757 fixup.fixup_rela_cnt = entry->d_un.d_val;
7758 break;
7759 case DT_IA_64_VMS_FIXUP_RELA_OFF:
7760 fixup.fixup_rela_off = entry->d_un.d_val;
7761 if (! dump_ia64_vms_dynamic_fixups (filedata, &fixup, strtab, strtab_sz))
7762 res = false;
7763 break;
7764 case DT_IA_64_VMS_IMG_RELA_CNT:
7765 imgrela.img_rela_cnt = entry->d_un.d_val;
7766 break;
7767 case DT_IA_64_VMS_IMG_RELA_OFF:
7768 imgrela.img_rela_off = entry->d_un.d_val;
7769 if (! dump_ia64_vms_dynamic_relocs (filedata, &imgrela))
7770 res = false;
7771 break;
7772
7773 default:
7774 break;
7775 }
7776 }
7777
7778 free (strtab);
7779
7780 return res;
7781 }
7782
7783 static struct
7784 {
7785 const char * name;
7786 int reloc;
7787 int size;
7788 int rela;
7789 }
7790 dynamic_relocations [] =
7791 {
7792 { "REL", DT_REL, DT_RELSZ, false },
7793 { "RELA", DT_RELA, DT_RELASZ, true },
7794 { "PLT", DT_JMPREL, DT_PLTRELSZ, UNKNOWN }
7795 };
7796
7797 /* Process the reloc section. */
7798
7799 static bool
7800 process_relocs (Filedata * filedata)
7801 {
7802 unsigned long rel_size;
7803 unsigned long rel_offset;
7804
7805 if (!do_reloc)
7806 return true;
7807
7808 if (do_using_dynamic)
7809 {
7810 int is_rela;
7811 const char * name;
7812 bool has_dynamic_reloc;
7813 unsigned int i;
7814
7815 has_dynamic_reloc = false;
7816
7817 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
7818 {
7819 is_rela = dynamic_relocations [i].rela;
7820 name = dynamic_relocations [i].name;
7821 rel_size = filedata->dynamic_info[dynamic_relocations [i].size];
7822 rel_offset = filedata->dynamic_info[dynamic_relocations [i].reloc];
7823
7824 if (rel_size)
7825 has_dynamic_reloc = true;
7826
7827 if (is_rela == UNKNOWN)
7828 {
7829 if (dynamic_relocations [i].reloc == DT_JMPREL)
7830 switch (filedata->dynamic_info[DT_PLTREL])
7831 {
7832 case DT_REL:
7833 is_rela = false;
7834 break;
7835 case DT_RELA:
7836 is_rela = true;
7837 break;
7838 }
7839 }
7840
7841 if (rel_size)
7842 {
7843 if (filedata->is_separate)
7844 printf
7845 (_("\nIn linked file '%s' section '%s' at offset 0x%lx contains %ld bytes:\n"),
7846 filedata->file_name, name, rel_offset, rel_size);
7847 else
7848 printf
7849 (_("\n'%s' relocation section at offset 0x%lx contains %ld bytes:\n"),
7850 name, rel_offset, rel_size);
7851
7852 dump_relocations (filedata,
7853 offset_from_vma (filedata, rel_offset, rel_size),
7854 rel_size,
7855 filedata->dynamic_symbols,
7856 filedata->num_dynamic_syms,
7857 filedata->dynamic_strings,
7858 filedata->dynamic_strings_length,
7859 is_rela, true /* is_dynamic */);
7860 }
7861 }
7862
7863 if (is_ia64_vms (filedata))
7864 if (process_ia64_vms_dynamic_relocs (filedata))
7865 has_dynamic_reloc = true;
7866
7867 if (! has_dynamic_reloc)
7868 {
7869 if (filedata->is_separate)
7870 printf (_("\nThere are no dynamic relocations in linked file '%s'.\n"),
7871 filedata->file_name);
7872 else
7873 printf (_("\nThere are no dynamic relocations in this file.\n"));
7874 }
7875 }
7876 else
7877 {
7878 Elf_Internal_Shdr * section;
7879 unsigned long i;
7880 bool found = false;
7881
7882 for (i = 0, section = filedata->section_headers;
7883 i < filedata->file_header.e_shnum;
7884 i++, section++)
7885 {
7886 if ( section->sh_type != SHT_RELA
7887 && section->sh_type != SHT_REL)
7888 continue;
7889
7890 rel_offset = section->sh_offset;
7891 rel_size = section->sh_size;
7892
7893 if (rel_size)
7894 {
7895 int is_rela;
7896 unsigned long num_rela;
7897
7898 if (filedata->is_separate)
7899 printf (_("\nIn linked file '%s' relocation section "),
7900 filedata->file_name);
7901 else
7902 printf (_("\nRelocation section "));
7903
7904 if (filedata->string_table == NULL)
7905 printf ("%d", section->sh_name);
7906 else
7907 printf ("'%s'", printable_section_name (filedata, section));
7908
7909 num_rela = rel_size / section->sh_entsize;
7910 printf (ngettext (" at offset 0x%lx contains %lu entry:\n",
7911 " at offset 0x%lx contains %lu entries:\n",
7912 num_rela),
7913 rel_offset, num_rela);
7914
7915 is_rela = section->sh_type == SHT_RELA;
7916
7917 if (section->sh_link != 0
7918 && section->sh_link < filedata->file_header.e_shnum)
7919 {
7920 Elf_Internal_Shdr * symsec;
7921 Elf_Internal_Sym * symtab;
7922 unsigned long nsyms;
7923 unsigned long strtablen = 0;
7924 char * strtab = NULL;
7925
7926 symsec = filedata->section_headers + section->sh_link;
7927 if (symsec->sh_type != SHT_SYMTAB
7928 && symsec->sh_type != SHT_DYNSYM)
7929 continue;
7930
7931 if (!get_symtab (filedata, symsec,
7932 &symtab, &nsyms, &strtab, &strtablen))
7933 continue;
7934
7935 dump_relocations (filedata, rel_offset, rel_size,
7936 symtab, nsyms, strtab, strtablen,
7937 is_rela,
7938 symsec->sh_type == SHT_DYNSYM);
7939 free (strtab);
7940 free (symtab);
7941 }
7942 else
7943 dump_relocations (filedata, rel_offset, rel_size,
7944 NULL, 0, NULL, 0, is_rela,
7945 false /* is_dynamic */);
7946
7947 found = true;
7948 }
7949 }
7950
7951 if (! found)
7952 {
7953 /* Users sometimes forget the -D option, so try to be helpful. */
7954 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
7955 {
7956 if (filedata->dynamic_info[dynamic_relocations [i].size])
7957 {
7958 if (filedata->is_separate)
7959 printf (_("\nThere are no static relocations in linked file '%s'."),
7960 filedata->file_name);
7961 else
7962 printf (_("\nThere are no static relocations in this file."));
7963 printf (_("\nTo see the dynamic relocations add --use-dynamic to the command line.\n"));
7964
7965 break;
7966 }
7967 }
7968 if (i == ARRAY_SIZE (dynamic_relocations))
7969 {
7970 if (filedata->is_separate)
7971 printf (_("\nThere are no relocations in linked file '%s'.\n"),
7972 filedata->file_name);
7973 else
7974 printf (_("\nThere are no relocations in this file.\n"));
7975 }
7976 }
7977 }
7978
7979 return true;
7980 }
7981
7982 /* An absolute address consists of a section and an offset. If the
7983 section is NULL, the offset itself is the address, otherwise, the
7984 address equals to LOAD_ADDRESS(section) + offset. */
7985
7986 struct absaddr
7987 {
7988 unsigned short section;
7989 bfd_vma offset;
7990 };
7991
7992 /* Find the nearest symbol at or below ADDR. Returns the symbol
7993 name, if found, and the offset from the symbol to ADDR. */
7994
7995 static void
7996 find_symbol_for_address (Filedata * filedata,
7997 Elf_Internal_Sym * symtab,
7998 unsigned long nsyms,
7999 const char * strtab,
8000 unsigned long strtab_size,
8001 struct absaddr addr,
8002 const char ** symname,
8003 bfd_vma * offset)
8004 {
8005 bfd_vma dist = 0x100000;
8006 Elf_Internal_Sym * sym;
8007 Elf_Internal_Sym * beg;
8008 Elf_Internal_Sym * end;
8009 Elf_Internal_Sym * best = NULL;
8010
8011 REMOVE_ARCH_BITS (addr.offset);
8012 beg = symtab;
8013 end = symtab + nsyms;
8014
8015 while (beg < end)
8016 {
8017 bfd_vma value;
8018
8019 sym = beg + (end - beg) / 2;
8020
8021 value = sym->st_value;
8022 REMOVE_ARCH_BITS (value);
8023
8024 if (sym->st_name != 0
8025 && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
8026 && addr.offset >= value
8027 && addr.offset - value < dist)
8028 {
8029 best = sym;
8030 dist = addr.offset - value;
8031 if (!dist)
8032 break;
8033 }
8034
8035 if (addr.offset < value)
8036 end = sym;
8037 else
8038 beg = sym + 1;
8039 }
8040
8041 if (best)
8042 {
8043 *symname = (best->st_name >= strtab_size
8044 ? _("<corrupt>") : strtab + best->st_name);
8045 *offset = dist;
8046 return;
8047 }
8048
8049 *symname = NULL;
8050 *offset = addr.offset;
8051 }
8052
8053 static /* signed */ int
8054 symcmp (const void *p, const void *q)
8055 {
8056 Elf_Internal_Sym *sp = (Elf_Internal_Sym *) p;
8057 Elf_Internal_Sym *sq = (Elf_Internal_Sym *) q;
8058
8059 return sp->st_value > sq->st_value ? 1 : (sp->st_value < sq->st_value ? -1 : 0);
8060 }
8061
8062 /* Process the unwind section. */
8063
8064 #include "unwind-ia64.h"
8065
8066 struct ia64_unw_table_entry
8067 {
8068 struct absaddr start;
8069 struct absaddr end;
8070 struct absaddr info;
8071 };
8072
8073 struct ia64_unw_aux_info
8074 {
8075 struct ia64_unw_table_entry * table; /* Unwind table. */
8076 unsigned long table_len; /* Length of unwind table. */
8077 unsigned char * info; /* Unwind info. */
8078 unsigned long info_size; /* Size of unwind info. */
8079 bfd_vma info_addr; /* Starting address of unwind info. */
8080 bfd_vma seg_base; /* Starting address of segment. */
8081 Elf_Internal_Sym * symtab; /* The symbol table. */
8082 unsigned long nsyms; /* Number of symbols. */
8083 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
8084 unsigned long nfuns; /* Number of entries in funtab. */
8085 char * strtab; /* The string table. */
8086 unsigned long strtab_size; /* Size of string table. */
8087 };
8088
8089 static bool
8090 dump_ia64_unwind (Filedata * filedata, struct ia64_unw_aux_info * aux)
8091 {
8092 struct ia64_unw_table_entry * tp;
8093 unsigned long j, nfuns;
8094 int in_body;
8095 bool res = true;
8096
8097 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
8098 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
8099 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
8100 aux->funtab[nfuns++] = aux->symtab[j];
8101 aux->nfuns = nfuns;
8102 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
8103
8104 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
8105 {
8106 bfd_vma stamp;
8107 bfd_vma offset;
8108 const unsigned char * dp;
8109 const unsigned char * head;
8110 const unsigned char * end;
8111 const char * procname;
8112
8113 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
8114 aux->strtab_size, tp->start, &procname, &offset);
8115
8116 fputs ("\n<", stdout);
8117
8118 if (procname)
8119 {
8120 fputs (procname, stdout);
8121
8122 if (offset)
8123 printf ("+%lx", (unsigned long) offset);
8124 }
8125
8126 fputs (">: [", stdout);
8127 print_vma (tp->start.offset, PREFIX_HEX);
8128 fputc ('-', stdout);
8129 print_vma (tp->end.offset, PREFIX_HEX);
8130 printf ("], info at +0x%lx\n",
8131 (unsigned long) (tp->info.offset - aux->seg_base));
8132
8133 /* PR 17531: file: 86232b32. */
8134 if (aux->info == NULL)
8135 continue;
8136
8137 offset = tp->info.offset;
8138 if (tp->info.section)
8139 {
8140 if (tp->info.section >= filedata->file_header.e_shnum)
8141 {
8142 warn (_("Invalid section %u in table entry %ld\n"),
8143 tp->info.section, (long) (tp - aux->table));
8144 res = false;
8145 continue;
8146 }
8147 offset += filedata->section_headers[tp->info.section].sh_addr;
8148 }
8149 offset -= aux->info_addr;
8150 /* PR 17531: file: 0997b4d1. */
8151 if (offset >= aux->info_size
8152 || aux->info_size - offset < 8)
8153 {
8154 warn (_("Invalid offset %lx in table entry %ld\n"),
8155 (long) tp->info.offset, (long) (tp - aux->table));
8156 res = false;
8157 continue;
8158 }
8159
8160 head = aux->info + offset;
8161 stamp = byte_get ((unsigned char *) head, sizeof (stamp));
8162
8163 printf (" v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
8164 (unsigned) UNW_VER (stamp),
8165 (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
8166 UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
8167 UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
8168 (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
8169
8170 if (UNW_VER (stamp) != 1)
8171 {
8172 printf (_("\tUnknown version.\n"));
8173 continue;
8174 }
8175
8176 in_body = 0;
8177 end = head + 8 + eh_addr_size * UNW_LENGTH (stamp);
8178 /* PR 17531: file: 16ceda89. */
8179 if (end > aux->info + aux->info_size)
8180 end = aux->info + aux->info_size;
8181 for (dp = head + 8; dp < end;)
8182 dp = unw_decode (dp, in_body, & in_body, end);
8183 }
8184
8185 free (aux->funtab);
8186
8187 return res;
8188 }
8189
8190 static bool
8191 slurp_ia64_unwind_table (Filedata * filedata,
8192 struct ia64_unw_aux_info * aux,
8193 Elf_Internal_Shdr * sec)
8194 {
8195 unsigned long size, nrelas, i;
8196 Elf_Internal_Phdr * seg;
8197 struct ia64_unw_table_entry * tep;
8198 Elf_Internal_Shdr * relsec;
8199 Elf_Internal_Rela * rela;
8200 Elf_Internal_Rela * rp;
8201 unsigned char * table;
8202 unsigned char * tp;
8203 Elf_Internal_Sym * sym;
8204 const char * relname;
8205
8206 aux->table_len = 0;
8207
8208 /* First, find the starting address of the segment that includes
8209 this section: */
8210
8211 if (filedata->file_header.e_phnum)
8212 {
8213 if (! get_program_headers (filedata))
8214 return false;
8215
8216 for (seg = filedata->program_headers;
8217 seg < filedata->program_headers + filedata->file_header.e_phnum;
8218 ++seg)
8219 {
8220 if (seg->p_type != PT_LOAD)
8221 continue;
8222
8223 if (sec->sh_addr >= seg->p_vaddr
8224 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
8225 {
8226 aux->seg_base = seg->p_vaddr;
8227 break;
8228 }
8229 }
8230 }
8231
8232 /* Second, build the unwind table from the contents of the unwind section: */
8233 size = sec->sh_size;
8234 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
8235 _("unwind table"));
8236 if (!table)
8237 return false;
8238
8239 aux->table_len = size / (3 * eh_addr_size);
8240 aux->table = (struct ia64_unw_table_entry *)
8241 xcmalloc (aux->table_len, sizeof (aux->table[0]));
8242 tep = aux->table;
8243
8244 for (tp = table; tp <= table + size - (3 * eh_addr_size); ++tep)
8245 {
8246 tep->start.section = SHN_UNDEF;
8247 tep->end.section = SHN_UNDEF;
8248 tep->info.section = SHN_UNDEF;
8249 tep->start.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8250 tep->end.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8251 tep->info.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8252 tep->start.offset += aux->seg_base;
8253 tep->end.offset += aux->seg_base;
8254 tep->info.offset += aux->seg_base;
8255 }
8256 free (table);
8257
8258 /* Third, apply any relocations to the unwind table: */
8259 for (relsec = filedata->section_headers;
8260 relsec < filedata->section_headers + filedata->file_header.e_shnum;
8261 ++relsec)
8262 {
8263 if (relsec->sh_type != SHT_RELA
8264 || relsec->sh_info >= filedata->file_header.e_shnum
8265 || filedata->section_headers + relsec->sh_info != sec)
8266 continue;
8267
8268 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
8269 & rela, & nrelas))
8270 {
8271 free (aux->table);
8272 aux->table = NULL;
8273 aux->table_len = 0;
8274 return false;
8275 }
8276
8277 for (rp = rela; rp < rela + nrelas; ++rp)
8278 {
8279 unsigned int sym_ndx;
8280 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
8281 relname = elf_ia64_reloc_type (r_type);
8282
8283 /* PR 17531: file: 9fa67536. */
8284 if (relname == NULL)
8285 {
8286 warn (_("Skipping unknown relocation type: %u\n"), r_type);
8287 continue;
8288 }
8289
8290 if (! startswith (relname, "R_IA64_SEGREL"))
8291 {
8292 warn (_("Skipping unexpected relocation type: %s\n"), relname);
8293 continue;
8294 }
8295
8296 i = rp->r_offset / (3 * eh_addr_size);
8297
8298 /* PR 17531: file: 5bc8d9bf. */
8299 if (i >= aux->table_len)
8300 {
8301 warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
8302 continue;
8303 }
8304
8305 sym_ndx = get_reloc_symindex (rp->r_info);
8306 if (sym_ndx >= aux->nsyms)
8307 {
8308 warn (_("Skipping reloc with invalid symbol index: %u\n"),
8309 sym_ndx);
8310 continue;
8311 }
8312 sym = aux->symtab + sym_ndx;
8313
8314 switch (rp->r_offset / eh_addr_size % 3)
8315 {
8316 case 0:
8317 aux->table[i].start.section = sym->st_shndx;
8318 aux->table[i].start.offset = rp->r_addend + sym->st_value;
8319 break;
8320 case 1:
8321 aux->table[i].end.section = sym->st_shndx;
8322 aux->table[i].end.offset = rp->r_addend + sym->st_value;
8323 break;
8324 case 2:
8325 aux->table[i].info.section = sym->st_shndx;
8326 aux->table[i].info.offset = rp->r_addend + sym->st_value;
8327 break;
8328 default:
8329 break;
8330 }
8331 }
8332
8333 free (rela);
8334 }
8335
8336 return true;
8337 }
8338
8339 static bool
8340 ia64_process_unwind (Filedata * filedata)
8341 {
8342 Elf_Internal_Shdr * sec;
8343 Elf_Internal_Shdr * unwsec = NULL;
8344 unsigned long i, unwcount = 0, unwstart = 0;
8345 struct ia64_unw_aux_info aux;
8346 bool res = true;
8347
8348 memset (& aux, 0, sizeof (aux));
8349
8350 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
8351 {
8352 if (sec->sh_type == SHT_SYMTAB)
8353 {
8354 if (aux.symtab)
8355 {
8356 error (_("Multiple symbol tables encountered\n"));
8357 free (aux.symtab);
8358 aux.symtab = NULL;
8359 free (aux.strtab);
8360 aux.strtab = NULL;
8361 }
8362 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
8363 &aux.strtab, &aux.strtab_size))
8364 return false;
8365 }
8366 else if (sec->sh_type == SHT_IA_64_UNWIND)
8367 unwcount++;
8368 }
8369
8370 if (!unwcount)
8371 printf (_("\nThere are no unwind sections in this file.\n"));
8372
8373 while (unwcount-- > 0)
8374 {
8375 char * suffix;
8376 size_t len, len2;
8377
8378 for (i = unwstart, sec = filedata->section_headers + unwstart, unwsec = NULL;
8379 i < filedata->file_header.e_shnum; ++i, ++sec)
8380 if (sec->sh_type == SHT_IA_64_UNWIND)
8381 {
8382 unwsec = sec;
8383 break;
8384 }
8385 /* We have already counted the number of SHT_IA64_UNWIND
8386 sections so the loop above should never fail. */
8387 assert (unwsec != NULL);
8388
8389 unwstart = i + 1;
8390 len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
8391
8392 if ((unwsec->sh_flags & SHF_GROUP) != 0)
8393 {
8394 /* We need to find which section group it is in. */
8395 struct group_list * g;
8396
8397 if (filedata->section_headers_groups == NULL
8398 || filedata->section_headers_groups[i] == NULL)
8399 i = filedata->file_header.e_shnum;
8400 else
8401 {
8402 g = filedata->section_headers_groups[i]->root;
8403
8404 for (; g != NULL; g = g->next)
8405 {
8406 sec = filedata->section_headers + g->section_index;
8407
8408 if (SECTION_NAME_VALID (sec)
8409 && streq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info))
8410 break;
8411 }
8412
8413 if (g == NULL)
8414 i = filedata->file_header.e_shnum;
8415 }
8416 }
8417 else if (SECTION_NAME_VALID (unwsec)
8418 && startswith (SECTION_NAME (unwsec),
8419 ELF_STRING_ia64_unwind_once))
8420 {
8421 /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO. */
8422 len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
8423 suffix = SECTION_NAME (unwsec) + len;
8424 for (i = 0, sec = filedata->section_headers;
8425 i < filedata->file_header.e_shnum;
8426 ++i, ++sec)
8427 if (SECTION_NAME_VALID (sec)
8428 && startswith (SECTION_NAME (sec),
8429 ELF_STRING_ia64_unwind_info_once)
8430 && streq (SECTION_NAME (sec) + len2, suffix))
8431 break;
8432 }
8433 else
8434 {
8435 /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
8436 .IA_64.unwind or BAR -> .IA_64.unwind_info. */
8437 len = sizeof (ELF_STRING_ia64_unwind) - 1;
8438 len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
8439 suffix = "";
8440 if (SECTION_NAME_VALID (unwsec)
8441 && startswith (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind))
8442 suffix = SECTION_NAME (unwsec) + len;
8443 for (i = 0, sec = filedata->section_headers;
8444 i < filedata->file_header.e_shnum;
8445 ++i, ++sec)
8446 if (SECTION_NAME_VALID (sec)
8447 && startswith (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info)
8448 && streq (SECTION_NAME (sec) + len2, suffix))
8449 break;
8450 }
8451
8452 if (i == filedata->file_header.e_shnum)
8453 {
8454 printf (_("\nCould not find unwind info section for "));
8455
8456 if (filedata->string_table == NULL)
8457 printf ("%d", unwsec->sh_name);
8458 else
8459 printf ("'%s'", printable_section_name (filedata, unwsec));
8460 }
8461 else
8462 {
8463 aux.info_addr = sec->sh_addr;
8464 aux.info = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1,
8465 sec->sh_size,
8466 _("unwind info"));
8467 aux.info_size = aux.info == NULL ? 0 : sec->sh_size;
8468
8469 printf (_("\nUnwind section "));
8470
8471 if (filedata->string_table == NULL)
8472 printf ("%d", unwsec->sh_name);
8473 else
8474 printf ("'%s'", printable_section_name (filedata, unwsec));
8475
8476 printf (_(" at offset 0x%lx contains %lu entries:\n"),
8477 (unsigned long) unwsec->sh_offset,
8478 (unsigned long) (unwsec->sh_size / (3 * eh_addr_size)));
8479
8480 if (slurp_ia64_unwind_table (filedata, & aux, unwsec)
8481 && aux.table_len > 0)
8482 dump_ia64_unwind (filedata, & aux);
8483
8484 free ((char *) aux.table);
8485 free ((char *) aux.info);
8486 aux.table = NULL;
8487 aux.info = NULL;
8488 }
8489 }
8490
8491 free (aux.symtab);
8492 free ((char *) aux.strtab);
8493
8494 return res;
8495 }
8496
8497 struct hppa_unw_table_entry
8498 {
8499 struct absaddr start;
8500 struct absaddr end;
8501 unsigned int Cannot_unwind:1; /* 0 */
8502 unsigned int Millicode:1; /* 1 */
8503 unsigned int Millicode_save_sr0:1; /* 2 */
8504 unsigned int Region_description:2; /* 3..4 */
8505 unsigned int reserved1:1; /* 5 */
8506 unsigned int Entry_SR:1; /* 6 */
8507 unsigned int Entry_FR:4; /* Number saved 7..10 */
8508 unsigned int Entry_GR:5; /* Number saved 11..15 */
8509 unsigned int Args_stored:1; /* 16 */
8510 unsigned int Variable_Frame:1; /* 17 */
8511 unsigned int Separate_Package_Body:1; /* 18 */
8512 unsigned int Frame_Extension_Millicode:1; /* 19 */
8513 unsigned int Stack_Overflow_Check:1; /* 20 */
8514 unsigned int Two_Instruction_SP_Increment:1; /* 21 */
8515 unsigned int Ada_Region:1; /* 22 */
8516 unsigned int cxx_info:1; /* 23 */
8517 unsigned int cxx_try_catch:1; /* 24 */
8518 unsigned int sched_entry_seq:1; /* 25 */
8519 unsigned int reserved2:1; /* 26 */
8520 unsigned int Save_SP:1; /* 27 */
8521 unsigned int Save_RP:1; /* 28 */
8522 unsigned int Save_MRP_in_frame:1; /* 29 */
8523 unsigned int extn_ptr_defined:1; /* 30 */
8524 unsigned int Cleanup_defined:1; /* 31 */
8525
8526 unsigned int MPE_XL_interrupt_marker:1; /* 0 */
8527 unsigned int HP_UX_interrupt_marker:1; /* 1 */
8528 unsigned int Large_frame:1; /* 2 */
8529 unsigned int Pseudo_SP_Set:1; /* 3 */
8530 unsigned int reserved4:1; /* 4 */
8531 unsigned int Total_frame_size:27; /* 5..31 */
8532 };
8533
8534 struct hppa_unw_aux_info
8535 {
8536 struct hppa_unw_table_entry * table; /* Unwind table. */
8537 unsigned long table_len; /* Length of unwind table. */
8538 bfd_vma seg_base; /* Starting address of segment. */
8539 Elf_Internal_Sym * symtab; /* The symbol table. */
8540 unsigned long nsyms; /* Number of symbols. */
8541 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
8542 unsigned long nfuns; /* Number of entries in funtab. */
8543 char * strtab; /* The string table. */
8544 unsigned long strtab_size; /* Size of string table. */
8545 };
8546
8547 static bool
8548 dump_hppa_unwind (Filedata * filedata, struct hppa_unw_aux_info * aux)
8549 {
8550 struct hppa_unw_table_entry * tp;
8551 unsigned long j, nfuns;
8552 bool res = true;
8553
8554 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
8555 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
8556 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
8557 aux->funtab[nfuns++] = aux->symtab[j];
8558 aux->nfuns = nfuns;
8559 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
8560
8561 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
8562 {
8563 bfd_vma offset;
8564 const char * procname;
8565
8566 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
8567 aux->strtab_size, tp->start, &procname,
8568 &offset);
8569
8570 fputs ("\n<", stdout);
8571
8572 if (procname)
8573 {
8574 fputs (procname, stdout);
8575
8576 if (offset)
8577 printf ("+%lx", (unsigned long) offset);
8578 }
8579
8580 fputs (">: [", stdout);
8581 print_vma (tp->start.offset, PREFIX_HEX);
8582 fputc ('-', stdout);
8583 print_vma (tp->end.offset, PREFIX_HEX);
8584 printf ("]\n\t");
8585
8586 #define PF(_m) if (tp->_m) printf (#_m " ");
8587 #define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
8588 PF(Cannot_unwind);
8589 PF(Millicode);
8590 PF(Millicode_save_sr0);
8591 /* PV(Region_description); */
8592 PF(Entry_SR);
8593 PV(Entry_FR);
8594 PV(Entry_GR);
8595 PF(Args_stored);
8596 PF(Variable_Frame);
8597 PF(Separate_Package_Body);
8598 PF(Frame_Extension_Millicode);
8599 PF(Stack_Overflow_Check);
8600 PF(Two_Instruction_SP_Increment);
8601 PF(Ada_Region);
8602 PF(cxx_info);
8603 PF(cxx_try_catch);
8604 PF(sched_entry_seq);
8605 PF(Save_SP);
8606 PF(Save_RP);
8607 PF(Save_MRP_in_frame);
8608 PF(extn_ptr_defined);
8609 PF(Cleanup_defined);
8610 PF(MPE_XL_interrupt_marker);
8611 PF(HP_UX_interrupt_marker);
8612 PF(Large_frame);
8613 PF(Pseudo_SP_Set);
8614 PV(Total_frame_size);
8615 #undef PF
8616 #undef PV
8617 }
8618
8619 printf ("\n");
8620
8621 free (aux->funtab);
8622
8623 return res;
8624 }
8625
8626 static bool
8627 slurp_hppa_unwind_table (Filedata * filedata,
8628 struct hppa_unw_aux_info * aux,
8629 Elf_Internal_Shdr * sec)
8630 {
8631 unsigned long size, unw_ent_size, nentries, nrelas, i;
8632 Elf_Internal_Phdr * seg;
8633 struct hppa_unw_table_entry * tep;
8634 Elf_Internal_Shdr * relsec;
8635 Elf_Internal_Rela * rela;
8636 Elf_Internal_Rela * rp;
8637 unsigned char * table;
8638 unsigned char * tp;
8639 Elf_Internal_Sym * sym;
8640 const char * relname;
8641
8642 /* First, find the starting address of the segment that includes
8643 this section. */
8644 if (filedata->file_header.e_phnum)
8645 {
8646 if (! get_program_headers (filedata))
8647 return false;
8648
8649 for (seg = filedata->program_headers;
8650 seg < filedata->program_headers + filedata->file_header.e_phnum;
8651 ++seg)
8652 {
8653 if (seg->p_type != PT_LOAD)
8654 continue;
8655
8656 if (sec->sh_addr >= seg->p_vaddr
8657 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
8658 {
8659 aux->seg_base = seg->p_vaddr;
8660 break;
8661 }
8662 }
8663 }
8664
8665 /* Second, build the unwind table from the contents of the unwind
8666 section. */
8667 size = sec->sh_size;
8668 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
8669 _("unwind table"));
8670 if (!table)
8671 return false;
8672
8673 unw_ent_size = 16;
8674 nentries = size / unw_ent_size;
8675 size = unw_ent_size * nentries;
8676
8677 aux->table_len = nentries;
8678 tep = aux->table = (struct hppa_unw_table_entry *)
8679 xcmalloc (nentries, sizeof (aux->table[0]));
8680
8681 for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
8682 {
8683 unsigned int tmp1, tmp2;
8684
8685 tep->start.section = SHN_UNDEF;
8686 tep->end.section = SHN_UNDEF;
8687
8688 tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
8689 tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
8690 tmp1 = byte_get ((unsigned char *) tp + 8, 4);
8691 tmp2 = byte_get ((unsigned char *) tp + 12, 4);
8692
8693 tep->start.offset += aux->seg_base;
8694 tep->end.offset += aux->seg_base;
8695
8696 tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
8697 tep->Millicode = (tmp1 >> 30) & 0x1;
8698 tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
8699 tep->Region_description = (tmp1 >> 27) & 0x3;
8700 tep->reserved1 = (tmp1 >> 26) & 0x1;
8701 tep->Entry_SR = (tmp1 >> 25) & 0x1;
8702 tep->Entry_FR = (tmp1 >> 21) & 0xf;
8703 tep->Entry_GR = (tmp1 >> 16) & 0x1f;
8704 tep->Args_stored = (tmp1 >> 15) & 0x1;
8705 tep->Variable_Frame = (tmp1 >> 14) & 0x1;
8706 tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
8707 tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
8708 tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
8709 tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
8710 tep->Ada_Region = (tmp1 >> 9) & 0x1;
8711 tep->cxx_info = (tmp1 >> 8) & 0x1;
8712 tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
8713 tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
8714 tep->reserved2 = (tmp1 >> 5) & 0x1;
8715 tep->Save_SP = (tmp1 >> 4) & 0x1;
8716 tep->Save_RP = (tmp1 >> 3) & 0x1;
8717 tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
8718 tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
8719 tep->Cleanup_defined = tmp1 & 0x1;
8720
8721 tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
8722 tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
8723 tep->Large_frame = (tmp2 >> 29) & 0x1;
8724 tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
8725 tep->reserved4 = (tmp2 >> 27) & 0x1;
8726 tep->Total_frame_size = tmp2 & 0x7ffffff;
8727 }
8728 free (table);
8729
8730 /* Third, apply any relocations to the unwind table. */
8731 for (relsec = filedata->section_headers;
8732 relsec < filedata->section_headers + filedata->file_header.e_shnum;
8733 ++relsec)
8734 {
8735 if (relsec->sh_type != SHT_RELA
8736 || relsec->sh_info >= filedata->file_header.e_shnum
8737 || filedata->section_headers + relsec->sh_info != sec)
8738 continue;
8739
8740 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
8741 & rela, & nrelas))
8742 return false;
8743
8744 for (rp = rela; rp < rela + nrelas; ++rp)
8745 {
8746 unsigned int sym_ndx;
8747 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
8748 relname = elf_hppa_reloc_type (r_type);
8749
8750 if (relname == NULL)
8751 {
8752 warn (_("Skipping unknown relocation type: %u\n"), r_type);
8753 continue;
8754 }
8755
8756 /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64. */
8757 if (! startswith (relname, "R_PARISC_SEGREL"))
8758 {
8759 warn (_("Skipping unexpected relocation type: %s\n"), relname);
8760 continue;
8761 }
8762
8763 i = rp->r_offset / unw_ent_size;
8764 if (i >= aux->table_len)
8765 {
8766 warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
8767 continue;
8768 }
8769
8770 sym_ndx = get_reloc_symindex (rp->r_info);
8771 if (sym_ndx >= aux->nsyms)
8772 {
8773 warn (_("Skipping reloc with invalid symbol index: %u\n"),
8774 sym_ndx);
8775 continue;
8776 }
8777 sym = aux->symtab + sym_ndx;
8778
8779 switch ((rp->r_offset % unw_ent_size) / 4)
8780 {
8781 case 0:
8782 aux->table[i].start.section = sym->st_shndx;
8783 aux->table[i].start.offset = sym->st_value + rp->r_addend;
8784 break;
8785 case 1:
8786 aux->table[i].end.section = sym->st_shndx;
8787 aux->table[i].end.offset = sym->st_value + rp->r_addend;
8788 break;
8789 default:
8790 break;
8791 }
8792 }
8793
8794 free (rela);
8795 }
8796
8797 return true;
8798 }
8799
8800 static bool
8801 hppa_process_unwind (Filedata * filedata)
8802 {
8803 struct hppa_unw_aux_info aux;
8804 Elf_Internal_Shdr * unwsec = NULL;
8805 Elf_Internal_Shdr * sec;
8806 unsigned long i;
8807 bool res = true;
8808
8809 if (filedata->string_table == NULL)
8810 return false;
8811
8812 memset (& aux, 0, sizeof (aux));
8813
8814 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
8815 {
8816 if (sec->sh_type == SHT_SYMTAB)
8817 {
8818 if (aux.symtab)
8819 {
8820 error (_("Multiple symbol tables encountered\n"));
8821 free (aux.symtab);
8822 aux.symtab = NULL;
8823 free (aux.strtab);
8824 aux.strtab = NULL;
8825 }
8826 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
8827 &aux.strtab, &aux.strtab_size))
8828 return false;
8829 }
8830 else if (SECTION_NAME_VALID (sec)
8831 && streq (SECTION_NAME (sec), ".PARISC.unwind"))
8832 unwsec = sec;
8833 }
8834
8835 if (!unwsec)
8836 printf (_("\nThere are no unwind sections in this file.\n"));
8837
8838 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
8839 {
8840 if (SECTION_NAME_VALID (sec)
8841 && streq (SECTION_NAME (sec), ".PARISC.unwind"))
8842 {
8843 unsigned long num_unwind = sec->sh_size / 16;
8844
8845 printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
8846 "contains %lu entry:\n",
8847 "\nUnwind section '%s' at offset 0x%lx "
8848 "contains %lu entries:\n",
8849 num_unwind),
8850 printable_section_name (filedata, sec),
8851 (unsigned long) sec->sh_offset,
8852 num_unwind);
8853
8854 if (! slurp_hppa_unwind_table (filedata, &aux, sec))
8855 res = false;
8856
8857 if (res && aux.table_len > 0)
8858 {
8859 if (! dump_hppa_unwind (filedata, &aux))
8860 res = false;
8861 }
8862
8863 free ((char *) aux.table);
8864 aux.table = NULL;
8865 }
8866 }
8867
8868 free (aux.symtab);
8869 free ((char *) aux.strtab);
8870
8871 return res;
8872 }
8873
8874 struct arm_section
8875 {
8876 unsigned char * data; /* The unwind data. */
8877 Elf_Internal_Shdr * sec; /* The cached unwind section header. */
8878 Elf_Internal_Rela * rela; /* The cached relocations for this section. */
8879 unsigned long nrelas; /* The number of relocations. */
8880 unsigned int rel_type; /* REL or RELA ? */
8881 Elf_Internal_Rela * next_rela; /* Cyclic pointer to the next reloc to process. */
8882 };
8883
8884 struct arm_unw_aux_info
8885 {
8886 Filedata * filedata; /* The file containing the unwind sections. */
8887 Elf_Internal_Sym * symtab; /* The file's symbol table. */
8888 unsigned long nsyms; /* Number of symbols. */
8889 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
8890 unsigned long nfuns; /* Number of these symbols. */
8891 char * strtab; /* The file's string table. */
8892 unsigned long strtab_size; /* Size of string table. */
8893 };
8894
8895 static const char *
8896 arm_print_vma_and_name (Filedata * filedata,
8897 struct arm_unw_aux_info * aux,
8898 bfd_vma fn,
8899 struct absaddr addr)
8900 {
8901 const char *procname;
8902 bfd_vma sym_offset;
8903
8904 if (addr.section == SHN_UNDEF)
8905 addr.offset = fn;
8906
8907 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
8908 aux->strtab_size, addr, &procname,
8909 &sym_offset);
8910
8911 print_vma (fn, PREFIX_HEX);
8912
8913 if (procname)
8914 {
8915 fputs (" <", stdout);
8916 fputs (procname, stdout);
8917
8918 if (sym_offset)
8919 printf ("+0x%lx", (unsigned long) sym_offset);
8920 fputc ('>', stdout);
8921 }
8922
8923 return procname;
8924 }
8925
8926 static void
8927 arm_free_section (struct arm_section *arm_sec)
8928 {
8929 free (arm_sec->data);
8930 free (arm_sec->rela);
8931 }
8932
8933 /* 1) If SEC does not match the one cached in ARM_SEC, then free the current
8934 cached section and install SEC instead.
8935 2) Locate the 32-bit word at WORD_OFFSET in unwind section SEC
8936 and return its valued in * WORDP, relocating if necessary.
8937 3) Update the NEXT_RELA field in ARM_SEC and store the section index and
8938 relocation's offset in ADDR.
8939 4) If SYM_NAME is non-NULL and a relocation was applied, record the offset
8940 into the string table of the symbol associated with the reloc. If no
8941 reloc was applied store -1 there.
8942 5) Return TRUE upon success, FALSE otherwise. */
8943
8944 static bool
8945 get_unwind_section_word (Filedata * filedata,
8946 struct arm_unw_aux_info * aux,
8947 struct arm_section * arm_sec,
8948 Elf_Internal_Shdr * sec,
8949 bfd_vma word_offset,
8950 unsigned int * wordp,
8951 struct absaddr * addr,
8952 bfd_vma * sym_name)
8953 {
8954 Elf_Internal_Rela *rp;
8955 Elf_Internal_Sym *sym;
8956 const char * relname;
8957 unsigned int word;
8958 bool wrapped;
8959
8960 if (sec == NULL || arm_sec == NULL)
8961 return false;
8962
8963 addr->section = SHN_UNDEF;
8964 addr->offset = 0;
8965
8966 if (sym_name != NULL)
8967 *sym_name = (bfd_vma) -1;
8968
8969 /* If necessary, update the section cache. */
8970 if (sec != arm_sec->sec)
8971 {
8972 Elf_Internal_Shdr *relsec;
8973
8974 arm_free_section (arm_sec);
8975
8976 arm_sec->sec = sec;
8977 arm_sec->data = get_data (NULL, aux->filedata, sec->sh_offset, 1,
8978 sec->sh_size, _("unwind data"));
8979 arm_sec->rela = NULL;
8980 arm_sec->nrelas = 0;
8981
8982 for (relsec = filedata->section_headers;
8983 relsec < filedata->section_headers + filedata->file_header.e_shnum;
8984 ++relsec)
8985 {
8986 if (relsec->sh_info >= filedata->file_header.e_shnum
8987 || filedata->section_headers + relsec->sh_info != sec
8988 /* PR 15745: Check the section type as well. */
8989 || (relsec->sh_type != SHT_REL
8990 && relsec->sh_type != SHT_RELA))
8991 continue;
8992
8993 arm_sec->rel_type = relsec->sh_type;
8994 if (relsec->sh_type == SHT_REL)
8995 {
8996 if (!slurp_rel_relocs (aux->filedata, relsec->sh_offset,
8997 relsec->sh_size,
8998 & arm_sec->rela, & arm_sec->nrelas))
8999 return false;
9000 }
9001 else /* relsec->sh_type == SHT_RELA */
9002 {
9003 if (!slurp_rela_relocs (aux->filedata, relsec->sh_offset,
9004 relsec->sh_size,
9005 & arm_sec->rela, & arm_sec->nrelas))
9006 return false;
9007 }
9008 break;
9009 }
9010
9011 arm_sec->next_rela = arm_sec->rela;
9012 }
9013
9014 /* If there is no unwind data we can do nothing. */
9015 if (arm_sec->data == NULL)
9016 return false;
9017
9018 /* If the offset is invalid then fail. */
9019 if (/* PR 21343 *//* PR 18879 */
9020 sec->sh_size < 4
9021 || word_offset > (sec->sh_size - 4)
9022 || ((bfd_signed_vma) word_offset) < 0)
9023 return false;
9024
9025 /* Get the word at the required offset. */
9026 word = byte_get (arm_sec->data + word_offset, 4);
9027
9028 /* PR 17531: file: id:000001,src:001266+003044,op:splice,rep:128. */
9029 if (arm_sec->rela == NULL)
9030 {
9031 * wordp = word;
9032 return true;
9033 }
9034
9035 /* Look through the relocs to find the one that applies to the provided offset. */
9036 wrapped = false;
9037 for (rp = arm_sec->next_rela; rp != arm_sec->rela + arm_sec->nrelas; rp++)
9038 {
9039 bfd_vma prelval, offset;
9040
9041 if (rp->r_offset > word_offset && !wrapped)
9042 {
9043 rp = arm_sec->rela;
9044 wrapped = true;
9045 }
9046 if (rp->r_offset > word_offset)
9047 break;
9048
9049 if (rp->r_offset & 3)
9050 {
9051 warn (_("Skipping unexpected relocation at offset 0x%lx\n"),
9052 (unsigned long) rp->r_offset);
9053 continue;
9054 }
9055
9056 if (rp->r_offset < word_offset)
9057 continue;
9058
9059 /* PR 17531: file: 027-161405-0.004 */
9060 if (aux->symtab == NULL)
9061 continue;
9062
9063 if (arm_sec->rel_type == SHT_REL)
9064 {
9065 offset = word & 0x7fffffff;
9066 if (offset & 0x40000000)
9067 offset |= ~ (bfd_vma) 0x7fffffff;
9068 }
9069 else if (arm_sec->rel_type == SHT_RELA)
9070 offset = rp->r_addend;
9071 else
9072 {
9073 error (_("Unknown section relocation type %d encountered\n"),
9074 arm_sec->rel_type);
9075 break;
9076 }
9077
9078 /* PR 17531 file: 027-1241568-0.004. */
9079 if (ELF32_R_SYM (rp->r_info) >= aux->nsyms)
9080 {
9081 error (_("Bad symbol index in unwind relocation (%lu > %lu)\n"),
9082 (unsigned long) ELF32_R_SYM (rp->r_info), aux->nsyms);
9083 break;
9084 }
9085
9086 sym = aux->symtab + ELF32_R_SYM (rp->r_info);
9087 offset += sym->st_value;
9088 prelval = offset - (arm_sec->sec->sh_addr + rp->r_offset);
9089
9090 /* Check that we are processing the expected reloc type. */
9091 if (filedata->file_header.e_machine == EM_ARM)
9092 {
9093 relname = elf_arm_reloc_type (ELF32_R_TYPE (rp->r_info));
9094 if (relname == NULL)
9095 {
9096 warn (_("Skipping unknown ARM relocation type: %d\n"),
9097 (int) ELF32_R_TYPE (rp->r_info));
9098 continue;
9099 }
9100
9101 if (streq (relname, "R_ARM_NONE"))
9102 continue;
9103
9104 if (! streq (relname, "R_ARM_PREL31"))
9105 {
9106 warn (_("Skipping unexpected ARM relocation type %s\n"), relname);
9107 continue;
9108 }
9109 }
9110 else if (filedata->file_header.e_machine == EM_TI_C6000)
9111 {
9112 relname = elf_tic6x_reloc_type (ELF32_R_TYPE (rp->r_info));
9113 if (relname == NULL)
9114 {
9115 warn (_("Skipping unknown C6000 relocation type: %d\n"),
9116 (int) ELF32_R_TYPE (rp->r_info));
9117 continue;
9118 }
9119
9120 if (streq (relname, "R_C6000_NONE"))
9121 continue;
9122
9123 if (! streq (relname, "R_C6000_PREL31"))
9124 {
9125 warn (_("Skipping unexpected C6000 relocation type %s\n"), relname);
9126 continue;
9127 }
9128
9129 prelval >>= 1;
9130 }
9131 else
9132 {
9133 /* This function currently only supports ARM and TI unwinders. */
9134 warn (_("Only TI and ARM unwinders are currently supported\n"));
9135 break;
9136 }
9137
9138 word = (word & ~ (bfd_vma) 0x7fffffff) | (prelval & 0x7fffffff);
9139 addr->section = sym->st_shndx;
9140 addr->offset = offset;
9141
9142 if (sym_name)
9143 * sym_name = sym->st_name;
9144 break;
9145 }
9146
9147 *wordp = word;
9148 arm_sec->next_rela = rp;
9149
9150 return true;
9151 }
9152
9153 static const char *tic6x_unwind_regnames[16] =
9154 {
9155 "A15", "B15", "B14", "B13", "B12", "B11", "B10", "B3",
9156 "A14", "A13", "A12", "A11", "A10",
9157 "[invalid reg 13]", "[invalid reg 14]", "[invalid reg 15]"
9158 };
9159
9160 static void
9161 decode_tic6x_unwind_regmask (unsigned int mask)
9162 {
9163 int i;
9164
9165 for (i = 12; mask; mask >>= 1, i--)
9166 {
9167 if (mask & 1)
9168 {
9169 fputs (tic6x_unwind_regnames[i], stdout);
9170 if (mask > 1)
9171 fputs (", ", stdout);
9172 }
9173 }
9174 }
9175
9176 #define ADVANCE \
9177 if (remaining == 0 && more_words) \
9178 { \
9179 data_offset += 4; \
9180 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, \
9181 data_offset, & word, & addr, NULL)) \
9182 return false; \
9183 remaining = 4; \
9184 more_words--; \
9185 } \
9186
9187 #define GET_OP(OP) \
9188 ADVANCE; \
9189 if (remaining) \
9190 { \
9191 remaining--; \
9192 (OP) = word >> 24; \
9193 word <<= 8; \
9194 } \
9195 else \
9196 { \
9197 printf (_("[Truncated opcode]\n")); \
9198 return false; \
9199 } \
9200 printf ("0x%02x ", OP)
9201
9202 static bool
9203 decode_arm_unwind_bytecode (Filedata * filedata,
9204 struct arm_unw_aux_info * aux,
9205 unsigned int word,
9206 unsigned int remaining,
9207 unsigned int more_words,
9208 bfd_vma data_offset,
9209 Elf_Internal_Shdr * data_sec,
9210 struct arm_section * data_arm_sec)
9211 {
9212 struct absaddr addr;
9213 bool res = true;
9214
9215 /* Decode the unwinding instructions. */
9216 while (1)
9217 {
9218 unsigned int op, op2;
9219
9220 ADVANCE;
9221 if (remaining == 0)
9222 break;
9223 remaining--;
9224 op = word >> 24;
9225 word <<= 8;
9226
9227 printf (" 0x%02x ", op);
9228
9229 if ((op & 0xc0) == 0x00)
9230 {
9231 int offset = ((op & 0x3f) << 2) + 4;
9232
9233 printf (" vsp = vsp + %d", offset);
9234 }
9235 else if ((op & 0xc0) == 0x40)
9236 {
9237 int offset = ((op & 0x3f) << 2) + 4;
9238
9239 printf (" vsp = vsp - %d", offset);
9240 }
9241 else if ((op & 0xf0) == 0x80)
9242 {
9243 GET_OP (op2);
9244 if (op == 0x80 && op2 == 0)
9245 printf (_("Refuse to unwind"));
9246 else
9247 {
9248 unsigned int mask = ((op & 0x0f) << 8) | op2;
9249 bool first = true;
9250 int i;
9251
9252 printf ("pop {");
9253 for (i = 0; i < 12; i++)
9254 if (mask & (1 << i))
9255 {
9256 if (first)
9257 first = false;
9258 else
9259 printf (", ");
9260 printf ("r%d", 4 + i);
9261 }
9262 printf ("}");
9263 }
9264 }
9265 else if ((op & 0xf0) == 0x90)
9266 {
9267 if (op == 0x9d || op == 0x9f)
9268 printf (_(" [Reserved]"));
9269 else
9270 printf (" vsp = r%d", op & 0x0f);
9271 }
9272 else if ((op & 0xf0) == 0xa0)
9273 {
9274 int end = 4 + (op & 0x07);
9275 bool first = true;
9276 int i;
9277
9278 printf (" pop {");
9279 for (i = 4; i <= end; i++)
9280 {
9281 if (first)
9282 first = false;
9283 else
9284 printf (", ");
9285 printf ("r%d", i);
9286 }
9287 if (op & 0x08)
9288 {
9289 if (!first)
9290 printf (", ");
9291 printf ("r14");
9292 }
9293 printf ("}");
9294 }
9295 else if (op == 0xb0)
9296 printf (_(" finish"));
9297 else if (op == 0xb1)
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 < 12; i++)
9310 if (mask & (1 << i))
9311 {
9312 if (first)
9313 first = false;
9314 else
9315 printf (", ");
9316 printf ("r%d", i);
9317 }
9318 printf ("}");
9319 }
9320 }
9321 else if (op == 0xb2)
9322 {
9323 unsigned char buf[9];
9324 unsigned int i, len;
9325 unsigned long offset;
9326
9327 for (i = 0; i < sizeof (buf); i++)
9328 {
9329 GET_OP (buf[i]);
9330 if ((buf[i] & 0x80) == 0)
9331 break;
9332 }
9333 if (i == sizeof (buf))
9334 {
9335 error (_("corrupt change to vsp\n"));
9336 res = false;
9337 }
9338 else
9339 {
9340 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
9341 assert (len == i + 1);
9342 offset = offset * 4 + 0x204;
9343 printf ("vsp = vsp + %ld", offset);
9344 }
9345 }
9346 else if (op == 0xb3 || op == 0xc8 || op == 0xc9)
9347 {
9348 unsigned int first, last;
9349
9350 GET_OP (op2);
9351 first = op2 >> 4;
9352 last = op2 & 0x0f;
9353 if (op == 0xc8)
9354 first = first + 16;
9355 printf ("pop {D%d", first);
9356 if (last)
9357 printf ("-D%d", first + last);
9358 printf ("}");
9359 }
9360 else if ((op & 0xf8) == 0xb8 || (op & 0xf8) == 0xd0)
9361 {
9362 unsigned int count = op & 0x07;
9363
9364 printf ("pop {D8");
9365 if (count)
9366 printf ("-D%d", 8 + count);
9367 printf ("}");
9368 }
9369 else if (op >= 0xc0 && op <= 0xc5)
9370 {
9371 unsigned int count = op & 0x07;
9372
9373 printf (" pop {wR10");
9374 if (count)
9375 printf ("-wR%d", 10 + count);
9376 printf ("}");
9377 }
9378 else if (op == 0xc6)
9379 {
9380 unsigned int first, last;
9381
9382 GET_OP (op2);
9383 first = op2 >> 4;
9384 last = op2 & 0x0f;
9385 printf ("pop {wR%d", first);
9386 if (last)
9387 printf ("-wR%d", first + last);
9388 printf ("}");
9389 }
9390 else if (op == 0xc7)
9391 {
9392 GET_OP (op2);
9393 if (op2 == 0 || (op2 & 0xf0) != 0)
9394 printf (_("[Spare]"));
9395 else
9396 {
9397 unsigned int mask = op2 & 0x0f;
9398 bool first = true;
9399 int i;
9400
9401 printf ("pop {");
9402 for (i = 0; i < 4; i++)
9403 if (mask & (1 << i))
9404 {
9405 if (first)
9406 first = false;
9407 else
9408 printf (", ");
9409 printf ("wCGR%d", i);
9410 }
9411 printf ("}");
9412 }
9413 }
9414 else
9415 {
9416 printf (_(" [unsupported opcode]"));
9417 res = false;
9418 }
9419
9420 printf ("\n");
9421 }
9422
9423 return res;
9424 }
9425
9426 static bool
9427 decode_tic6x_unwind_bytecode (Filedata * filedata,
9428 struct arm_unw_aux_info * aux,
9429 unsigned int word,
9430 unsigned int remaining,
9431 unsigned int more_words,
9432 bfd_vma data_offset,
9433 Elf_Internal_Shdr * data_sec,
9434 struct arm_section * data_arm_sec)
9435 {
9436 struct absaddr addr;
9437
9438 /* Decode the unwinding instructions. */
9439 while (1)
9440 {
9441 unsigned int op, op2;
9442
9443 ADVANCE;
9444 if (remaining == 0)
9445 break;
9446 remaining--;
9447 op = word >> 24;
9448 word <<= 8;
9449
9450 printf (" 0x%02x ", op);
9451
9452 if ((op & 0xc0) == 0x00)
9453 {
9454 int offset = ((op & 0x3f) << 3) + 8;
9455 printf (" sp = sp + %d", offset);
9456 }
9457 else if ((op & 0xc0) == 0x80)
9458 {
9459 GET_OP (op2);
9460 if (op == 0x80 && op2 == 0)
9461 printf (_("Refuse to unwind"));
9462 else
9463 {
9464 unsigned int mask = ((op & 0x1f) << 8) | op2;
9465 if (op & 0x20)
9466 printf ("pop compact {");
9467 else
9468 printf ("pop {");
9469
9470 decode_tic6x_unwind_regmask (mask);
9471 printf("}");
9472 }
9473 }
9474 else if ((op & 0xf0) == 0xc0)
9475 {
9476 unsigned int reg;
9477 unsigned int nregs;
9478 unsigned int i;
9479 const char *name;
9480 struct
9481 {
9482 unsigned int offset;
9483 unsigned int reg;
9484 } regpos[16];
9485
9486 /* Scan entire instruction first so that GET_OP output is not
9487 interleaved with disassembly. */
9488 nregs = 0;
9489 for (i = 0; nregs < (op & 0xf); i++)
9490 {
9491 GET_OP (op2);
9492 reg = op2 >> 4;
9493 if (reg != 0xf)
9494 {
9495 regpos[nregs].offset = i * 2;
9496 regpos[nregs].reg = reg;
9497 nregs++;
9498 }
9499
9500 reg = op2 & 0xf;
9501 if (reg != 0xf)
9502 {
9503 regpos[nregs].offset = i * 2 + 1;
9504 regpos[nregs].reg = reg;
9505 nregs++;
9506 }
9507 }
9508
9509 printf (_("pop frame {"));
9510 if (nregs == 0)
9511 {
9512 printf (_("*corrupt* - no registers specified"));
9513 }
9514 else
9515 {
9516 reg = nregs - 1;
9517 for (i = i * 2; i > 0; i--)
9518 {
9519 if (regpos[reg].offset == i - 1)
9520 {
9521 name = tic6x_unwind_regnames[regpos[reg].reg];
9522 if (reg > 0)
9523 reg--;
9524 }
9525 else
9526 name = _("[pad]");
9527
9528 fputs (name, stdout);
9529 if (i > 1)
9530 printf (", ");
9531 }
9532 }
9533
9534 printf ("}");
9535 }
9536 else if (op == 0xd0)
9537 printf (" MOV FP, SP");
9538 else if (op == 0xd1)
9539 printf (" __c6xabi_pop_rts");
9540 else if (op == 0xd2)
9541 {
9542 unsigned char buf[9];
9543 unsigned int i, len;
9544 unsigned long offset;
9545
9546 for (i = 0; i < sizeof (buf); i++)
9547 {
9548 GET_OP (buf[i]);
9549 if ((buf[i] & 0x80) == 0)
9550 break;
9551 }
9552 /* PR 17531: file: id:000001,src:001906+004739,op:splice,rep:2. */
9553 if (i == sizeof (buf))
9554 {
9555 warn (_("Corrupt stack pointer adjustment detected\n"));
9556 return false;
9557 }
9558
9559 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
9560 assert (len == i + 1);
9561 offset = offset * 8 + 0x408;
9562 printf (_("sp = sp + %ld"), offset);
9563 }
9564 else if ((op & 0xf0) == 0xe0)
9565 {
9566 if ((op & 0x0f) == 7)
9567 printf (" RETURN");
9568 else
9569 printf (" MV %s, B3", tic6x_unwind_regnames[op & 0x0f]);
9570 }
9571 else
9572 {
9573 printf (_(" [unsupported opcode]"));
9574 }
9575 putchar ('\n');
9576 }
9577
9578 return true;
9579 }
9580
9581 static bfd_vma
9582 arm_expand_prel31 (Filedata * filedata, bfd_vma word, bfd_vma where)
9583 {
9584 bfd_vma offset;
9585
9586 offset = word & 0x7fffffff;
9587 if (offset & 0x40000000)
9588 offset |= ~ (bfd_vma) 0x7fffffff;
9589
9590 if (filedata->file_header.e_machine == EM_TI_C6000)
9591 offset <<= 1;
9592
9593 return offset + where;
9594 }
9595
9596 static bool
9597 decode_arm_unwind (Filedata * filedata,
9598 struct arm_unw_aux_info * aux,
9599 unsigned int word,
9600 unsigned int remaining,
9601 bfd_vma data_offset,
9602 Elf_Internal_Shdr * data_sec,
9603 struct arm_section * data_arm_sec)
9604 {
9605 int per_index;
9606 unsigned int more_words = 0;
9607 struct absaddr addr;
9608 bfd_vma sym_name = (bfd_vma) -1;
9609 bool res = true;
9610
9611 if (remaining == 0)
9612 {
9613 /* Fetch the first word.
9614 Note - when decoding an object file the address extracted
9615 here will always be 0. So we also pass in the sym_name
9616 parameter so that we can find the symbol associated with
9617 the personality routine. */
9618 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, data_offset,
9619 & word, & addr, & sym_name))
9620 return false;
9621
9622 remaining = 4;
9623 }
9624 else
9625 {
9626 addr.section = SHN_UNDEF;
9627 addr.offset = 0;
9628 }
9629
9630 if ((word & 0x80000000) == 0)
9631 {
9632 /* Expand prel31 for personality routine. */
9633 bfd_vma fn;
9634 const char *procname;
9635
9636 fn = arm_expand_prel31 (filedata, word, data_sec->sh_addr + data_offset);
9637 printf (_(" Personality routine: "));
9638 if (fn == 0
9639 && addr.section == SHN_UNDEF && addr.offset == 0
9640 && sym_name != (bfd_vma) -1 && sym_name < aux->strtab_size)
9641 {
9642 procname = aux->strtab + sym_name;
9643 print_vma (fn, PREFIX_HEX);
9644 if (procname)
9645 {
9646 fputs (" <", stdout);
9647 fputs (procname, stdout);
9648 fputc ('>', stdout);
9649 }
9650 }
9651 else
9652 procname = arm_print_vma_and_name (filedata, aux, fn, addr);
9653 fputc ('\n', stdout);
9654
9655 /* The GCC personality routines use the standard compact
9656 encoding, starting with one byte giving the number of
9657 words. */
9658 if (procname != NULL
9659 && (startswith (procname, "__gcc_personality_v0")
9660 || startswith (procname, "__gxx_personality_v0")
9661 || startswith (procname, "__gcj_personality_v0")
9662 || startswith (procname, "__gnu_objc_personality_v0")))
9663 {
9664 remaining = 0;
9665 more_words = 1;
9666 ADVANCE;
9667 if (!remaining)
9668 {
9669 printf (_(" [Truncated data]\n"));
9670 return false;
9671 }
9672 more_words = word >> 24;
9673 word <<= 8;
9674 remaining--;
9675 per_index = -1;
9676 }
9677 else
9678 return true;
9679 }
9680 else
9681 {
9682 /* ARM EHABI Section 6.3:
9683
9684 An exception-handling table entry for the compact model looks like:
9685
9686 31 30-28 27-24 23-0
9687 -- ----- ----- ----
9688 1 0 index Data for personalityRoutine[index] */
9689
9690 if (filedata->file_header.e_machine == EM_ARM
9691 && (word & 0x70000000))
9692 {
9693 warn (_("Corrupt ARM compact model table entry: %x \n"), word);
9694 res = false;
9695 }
9696
9697 per_index = (word >> 24) & 0x7f;
9698 printf (_(" Compact model index: %d\n"), per_index);
9699 if (per_index == 0)
9700 {
9701 more_words = 0;
9702 word <<= 8;
9703 remaining--;
9704 }
9705 else if (per_index < 3)
9706 {
9707 more_words = (word >> 16) & 0xff;
9708 word <<= 16;
9709 remaining -= 2;
9710 }
9711 }
9712
9713 switch (filedata->file_header.e_machine)
9714 {
9715 case EM_ARM:
9716 if (per_index < 3)
9717 {
9718 if (! decode_arm_unwind_bytecode (filedata, aux, word, remaining, more_words,
9719 data_offset, data_sec, data_arm_sec))
9720 res = false;
9721 }
9722 else
9723 {
9724 warn (_("Unknown ARM compact model index encountered\n"));
9725 printf (_(" [reserved]\n"));
9726 res = false;
9727 }
9728 break;
9729
9730 case EM_TI_C6000:
9731 if (per_index < 3)
9732 {
9733 if (! decode_tic6x_unwind_bytecode (filedata, aux, word, remaining, more_words,
9734 data_offset, data_sec, data_arm_sec))
9735 res = false;
9736 }
9737 else if (per_index < 5)
9738 {
9739 if (((word >> 17) & 0x7f) == 0x7f)
9740 printf (_(" Restore stack from frame pointer\n"));
9741 else
9742 printf (_(" Stack increment %d\n"), (word >> 14) & 0x1fc);
9743 printf (_(" Registers restored: "));
9744 if (per_index == 4)
9745 printf (" (compact) ");
9746 decode_tic6x_unwind_regmask ((word >> 4) & 0x1fff);
9747 putchar ('\n');
9748 printf (_(" Return register: %s\n"),
9749 tic6x_unwind_regnames[word & 0xf]);
9750 }
9751 else
9752 printf (_(" [reserved (%d)]\n"), per_index);
9753 break;
9754
9755 default:
9756 error (_("Unsupported architecture type %d encountered when decoding unwind table\n"),
9757 filedata->file_header.e_machine);
9758 res = false;
9759 }
9760
9761 /* Decode the descriptors. Not implemented. */
9762
9763 return res;
9764 }
9765
9766 static bool
9767 dump_arm_unwind (Filedata * filedata,
9768 struct arm_unw_aux_info * aux,
9769 Elf_Internal_Shdr * exidx_sec)
9770 {
9771 struct arm_section exidx_arm_sec, extab_arm_sec;
9772 unsigned int i, exidx_len;
9773 unsigned long j, nfuns;
9774 bool res = true;
9775
9776 memset (&exidx_arm_sec, 0, sizeof (exidx_arm_sec));
9777 memset (&extab_arm_sec, 0, sizeof (extab_arm_sec));
9778 exidx_len = exidx_sec->sh_size / 8;
9779
9780 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
9781 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
9782 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
9783 aux->funtab[nfuns++] = aux->symtab[j];
9784 aux->nfuns = nfuns;
9785 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
9786
9787 for (i = 0; i < exidx_len; i++)
9788 {
9789 unsigned int exidx_fn, exidx_entry;
9790 struct absaddr fn_addr, entry_addr;
9791 bfd_vma fn;
9792
9793 fputc ('\n', stdout);
9794
9795 if (! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
9796 8 * i, & exidx_fn, & fn_addr, NULL)
9797 || ! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
9798 8 * i + 4, & exidx_entry, & entry_addr, NULL))
9799 {
9800 free (aux->funtab);
9801 arm_free_section (& exidx_arm_sec);
9802 arm_free_section (& extab_arm_sec);
9803 return false;
9804 }
9805
9806 /* ARM EHABI, Section 5:
9807 An index table entry consists of 2 words.
9808 The first word contains a prel31 offset to the start of a function, with bit 31 clear. */
9809 if (exidx_fn & 0x80000000)
9810 {
9811 warn (_("corrupt index table entry: %x\n"), exidx_fn);
9812 res = false;
9813 }
9814
9815 fn = arm_expand_prel31 (filedata, exidx_fn, exidx_sec->sh_addr + 8 * i);
9816
9817 arm_print_vma_and_name (filedata, aux, fn, fn_addr);
9818 fputs (": ", stdout);
9819
9820 if (exidx_entry == 1)
9821 {
9822 print_vma (exidx_entry, PREFIX_HEX);
9823 fputs (" [cantunwind]\n", stdout);
9824 }
9825 else if (exidx_entry & 0x80000000)
9826 {
9827 print_vma (exidx_entry, PREFIX_HEX);
9828 fputc ('\n', stdout);
9829 decode_arm_unwind (filedata, aux, exidx_entry, 4, 0, NULL, NULL);
9830 }
9831 else
9832 {
9833 bfd_vma table, table_offset = 0;
9834 Elf_Internal_Shdr *table_sec;
9835
9836 fputs ("@", stdout);
9837 table = arm_expand_prel31 (filedata, exidx_entry, exidx_sec->sh_addr + 8 * i + 4);
9838 print_vma (table, PREFIX_HEX);
9839 printf ("\n");
9840
9841 /* Locate the matching .ARM.extab. */
9842 if (entry_addr.section != SHN_UNDEF
9843 && entry_addr.section < filedata->file_header.e_shnum)
9844 {
9845 table_sec = filedata->section_headers + entry_addr.section;
9846 table_offset = entry_addr.offset;
9847 /* PR 18879 */
9848 if (table_offset > table_sec->sh_size
9849 || ((bfd_signed_vma) table_offset) < 0)
9850 {
9851 warn (_("Unwind entry contains corrupt offset (0x%lx) into section %s\n"),
9852 (unsigned long) table_offset,
9853 printable_section_name (filedata, table_sec));
9854 res = false;
9855 continue;
9856 }
9857 }
9858 else
9859 {
9860 table_sec = find_section_by_address (filedata, table);
9861 if (table_sec != NULL)
9862 table_offset = table - table_sec->sh_addr;
9863 }
9864
9865 if (table_sec == NULL)
9866 {
9867 warn (_("Could not locate .ARM.extab section containing 0x%lx.\n"),
9868 (unsigned long) table);
9869 res = false;
9870 continue;
9871 }
9872
9873 if (! decode_arm_unwind (filedata, aux, 0, 0, table_offset, table_sec,
9874 &extab_arm_sec))
9875 res = false;
9876 }
9877 }
9878
9879 printf ("\n");
9880
9881 free (aux->funtab);
9882 arm_free_section (&exidx_arm_sec);
9883 arm_free_section (&extab_arm_sec);
9884
9885 return res;
9886 }
9887
9888 /* Used for both ARM and C6X unwinding tables. */
9889
9890 static bool
9891 arm_process_unwind (Filedata * filedata)
9892 {
9893 struct arm_unw_aux_info aux;
9894 Elf_Internal_Shdr *unwsec = NULL;
9895 Elf_Internal_Shdr *sec;
9896 unsigned long i;
9897 unsigned int sec_type;
9898 bool res = true;
9899
9900 switch (filedata->file_header.e_machine)
9901 {
9902 case EM_ARM:
9903 sec_type = SHT_ARM_EXIDX;
9904 break;
9905
9906 case EM_TI_C6000:
9907 sec_type = SHT_C6000_UNWIND;
9908 break;
9909
9910 default:
9911 error (_("Unsupported architecture type %d encountered when processing unwind table\n"),
9912 filedata->file_header.e_machine);
9913 return false;
9914 }
9915
9916 if (filedata->string_table == NULL)
9917 return false;
9918
9919 memset (& aux, 0, sizeof (aux));
9920 aux.filedata = filedata;
9921
9922 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9923 {
9924 if (sec->sh_type == SHT_SYMTAB)
9925 {
9926 if (aux.symtab)
9927 {
9928 error (_("Multiple symbol tables encountered\n"));
9929 free (aux.symtab);
9930 aux.symtab = NULL;
9931 free (aux.strtab);
9932 aux.strtab = NULL;
9933 }
9934 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
9935 &aux.strtab, &aux.strtab_size))
9936 return false;
9937 }
9938 else if (sec->sh_type == sec_type)
9939 unwsec = sec;
9940 }
9941
9942 if (unwsec == NULL)
9943 printf (_("\nThere are no unwind sections in this file.\n"));
9944 else
9945 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9946 {
9947 if (sec->sh_type == sec_type)
9948 {
9949 unsigned long num_unwind = sec->sh_size / (2 * eh_addr_size);
9950 printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
9951 "contains %lu entry:\n",
9952 "\nUnwind section '%s' at offset 0x%lx "
9953 "contains %lu entries:\n",
9954 num_unwind),
9955 printable_section_name (filedata, sec),
9956 (unsigned long) sec->sh_offset,
9957 num_unwind);
9958
9959 if (! dump_arm_unwind (filedata, &aux, sec))
9960 res = false;
9961 }
9962 }
9963
9964 free (aux.symtab);
9965 free ((char *) aux.strtab);
9966
9967 return res;
9968 }
9969
9970 static bool
9971 no_processor_specific_unwind (Filedata * filedata ATTRIBUTE_UNUSED)
9972 {
9973 printf (_("No processor specific unwind information to decode\n"));
9974 return true;
9975 }
9976
9977 static bool
9978 process_unwind (Filedata * filedata)
9979 {
9980 struct unwind_handler
9981 {
9982 unsigned int machtype;
9983 bool (* handler)(Filedata *);
9984 } handlers[] =
9985 {
9986 { EM_ARM, arm_process_unwind },
9987 { EM_IA_64, ia64_process_unwind },
9988 { EM_PARISC, hppa_process_unwind },
9989 { EM_TI_C6000, arm_process_unwind },
9990 { EM_386, no_processor_specific_unwind },
9991 { EM_X86_64, no_processor_specific_unwind },
9992 { 0, NULL }
9993 };
9994 int i;
9995
9996 if (!do_unwind)
9997 return true;
9998
9999 for (i = 0; handlers[i].handler != NULL; i++)
10000 if (filedata->file_header.e_machine == handlers[i].machtype)
10001 return handlers[i].handler (filedata);
10002
10003 printf (_("\nThe decoding of unwind sections for machine type %s is not currently supported.\n"),
10004 get_machine_name (filedata->file_header.e_machine));
10005 return true;
10006 }
10007
10008 static void
10009 dynamic_section_aarch64_val (Elf_Internal_Dyn * entry)
10010 {
10011 switch (entry->d_tag)
10012 {
10013 case DT_AARCH64_BTI_PLT:
10014 case DT_AARCH64_PAC_PLT:
10015 break;
10016 default:
10017 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10018 break;
10019 }
10020 putchar ('\n');
10021 }
10022
10023 static void
10024 dynamic_section_mips_val (Filedata * filedata, Elf_Internal_Dyn * entry)
10025 {
10026 switch (entry->d_tag)
10027 {
10028 case DT_MIPS_FLAGS:
10029 if (entry->d_un.d_val == 0)
10030 printf (_("NONE"));
10031 else
10032 {
10033 static const char * opts[] =
10034 {
10035 "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
10036 "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
10037 "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
10038 "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
10039 "RLD_ORDER_SAFE"
10040 };
10041 unsigned int cnt;
10042 bool first = true;
10043
10044 for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
10045 if (entry->d_un.d_val & (1 << cnt))
10046 {
10047 printf ("%s%s", first ? "" : " ", opts[cnt]);
10048 first = false;
10049 }
10050 }
10051 break;
10052
10053 case DT_MIPS_IVERSION:
10054 if (VALID_DYNAMIC_NAME (filedata, entry->d_un.d_val))
10055 printf (_("Interface Version: %s"),
10056 GET_DYNAMIC_NAME (filedata, entry->d_un.d_val));
10057 else
10058 {
10059 char buf[40];
10060 sprintf_vma (buf, entry->d_un.d_ptr);
10061 /* Note: coded this way so that there is a single string for translation. */
10062 printf (_("<corrupt: %s>"), buf);
10063 }
10064 break;
10065
10066 case DT_MIPS_TIME_STAMP:
10067 {
10068 char timebuf[128];
10069 struct tm * tmp;
10070 time_t atime = entry->d_un.d_val;
10071
10072 tmp = gmtime (&atime);
10073 /* PR 17531: file: 6accc532. */
10074 if (tmp == NULL)
10075 snprintf (timebuf, sizeof (timebuf), _("<corrupt>"));
10076 else
10077 snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
10078 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
10079 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
10080 printf (_("Time Stamp: %s"), timebuf);
10081 }
10082 break;
10083
10084 case DT_MIPS_RLD_VERSION:
10085 case DT_MIPS_LOCAL_GOTNO:
10086 case DT_MIPS_CONFLICTNO:
10087 case DT_MIPS_LIBLISTNO:
10088 case DT_MIPS_SYMTABNO:
10089 case DT_MIPS_UNREFEXTNO:
10090 case DT_MIPS_HIPAGENO:
10091 case DT_MIPS_DELTA_CLASS_NO:
10092 case DT_MIPS_DELTA_INSTANCE_NO:
10093 case DT_MIPS_DELTA_RELOC_NO:
10094 case DT_MIPS_DELTA_SYM_NO:
10095 case DT_MIPS_DELTA_CLASSSYM_NO:
10096 case DT_MIPS_COMPACT_SIZE:
10097 print_vma (entry->d_un.d_val, DEC);
10098 break;
10099
10100 case DT_MIPS_XHASH:
10101 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
10102 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10103 /* Falls through. */
10104
10105 default:
10106 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10107 }
10108 putchar ('\n');
10109 }
10110
10111 static void
10112 dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
10113 {
10114 switch (entry->d_tag)
10115 {
10116 case DT_HP_DLD_FLAGS:
10117 {
10118 static struct
10119 {
10120 long int bit;
10121 const char * str;
10122 }
10123 flags[] =
10124 {
10125 { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
10126 { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
10127 { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
10128 { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
10129 { DT_HP_BIND_NOW, "HP_BIND_NOW" },
10130 { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
10131 { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
10132 { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
10133 { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
10134 { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
10135 { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
10136 { DT_HP_GST, "HP_GST" },
10137 { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
10138 { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
10139 { DT_HP_NODELETE, "HP_NODELETE" },
10140 { DT_HP_GROUP, "HP_GROUP" },
10141 { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
10142 };
10143 bool first = true;
10144 size_t cnt;
10145 bfd_vma val = entry->d_un.d_val;
10146
10147 for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
10148 if (val & flags[cnt].bit)
10149 {
10150 if (! first)
10151 putchar (' ');
10152 fputs (flags[cnt].str, stdout);
10153 first = false;
10154 val ^= flags[cnt].bit;
10155 }
10156
10157 if (val != 0 || first)
10158 {
10159 if (! first)
10160 putchar (' ');
10161 print_vma (val, HEX);
10162 }
10163 }
10164 break;
10165
10166 default:
10167 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10168 break;
10169 }
10170 putchar ('\n');
10171 }
10172
10173 #ifdef BFD64
10174
10175 /* VMS vs Unix time offset and factor. */
10176
10177 #define VMS_EPOCH_OFFSET 35067168000000000LL
10178 #define VMS_GRANULARITY_FACTOR 10000000
10179 #ifndef INT64_MIN
10180 #define INT64_MIN (-9223372036854775807LL - 1)
10181 #endif
10182
10183 /* Display a VMS time in a human readable format. */
10184
10185 static void
10186 print_vms_time (bfd_int64_t vmstime)
10187 {
10188 struct tm *tm = NULL;
10189 time_t unxtime;
10190
10191 if (vmstime >= INT64_MIN + VMS_EPOCH_OFFSET)
10192 {
10193 vmstime = (vmstime - VMS_EPOCH_OFFSET) / VMS_GRANULARITY_FACTOR;
10194 unxtime = vmstime;
10195 if (unxtime == vmstime)
10196 tm = gmtime (&unxtime);
10197 }
10198 if (tm != NULL)
10199 printf ("%04u-%02u-%02uT%02u:%02u:%02u",
10200 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
10201 tm->tm_hour, tm->tm_min, tm->tm_sec);
10202 }
10203 #endif /* BFD64 */
10204
10205 static void
10206 dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
10207 {
10208 switch (entry->d_tag)
10209 {
10210 case DT_IA_64_PLT_RESERVE:
10211 /* First 3 slots reserved. */
10212 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10213 printf (" -- ");
10214 print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
10215 break;
10216
10217 case DT_IA_64_VMS_LINKTIME:
10218 #ifdef BFD64
10219 print_vms_time (entry->d_un.d_val);
10220 #endif
10221 break;
10222
10223 case DT_IA_64_VMS_LNKFLAGS:
10224 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10225 if (entry->d_un.d_val & VMS_LF_CALL_DEBUG)
10226 printf (" CALL_DEBUG");
10227 if (entry->d_un.d_val & VMS_LF_NOP0BUFS)
10228 printf (" NOP0BUFS");
10229 if (entry->d_un.d_val & VMS_LF_P0IMAGE)
10230 printf (" P0IMAGE");
10231 if (entry->d_un.d_val & VMS_LF_MKTHREADS)
10232 printf (" MKTHREADS");
10233 if (entry->d_un.d_val & VMS_LF_UPCALLS)
10234 printf (" UPCALLS");
10235 if (entry->d_un.d_val & VMS_LF_IMGSTA)
10236 printf (" IMGSTA");
10237 if (entry->d_un.d_val & VMS_LF_INITIALIZE)
10238 printf (" INITIALIZE");
10239 if (entry->d_un.d_val & VMS_LF_MAIN)
10240 printf (" MAIN");
10241 if (entry->d_un.d_val & VMS_LF_EXE_INIT)
10242 printf (" EXE_INIT");
10243 if (entry->d_un.d_val & VMS_LF_TBK_IN_IMG)
10244 printf (" TBK_IN_IMG");
10245 if (entry->d_un.d_val & VMS_LF_DBG_IN_IMG)
10246 printf (" DBG_IN_IMG");
10247 if (entry->d_un.d_val & VMS_LF_TBK_IN_DSF)
10248 printf (" TBK_IN_DSF");
10249 if (entry->d_un.d_val & VMS_LF_DBG_IN_DSF)
10250 printf (" DBG_IN_DSF");
10251 if (entry->d_un.d_val & VMS_LF_SIGNATURES)
10252 printf (" SIGNATURES");
10253 if (entry->d_un.d_val & VMS_LF_REL_SEG_OFF)
10254 printf (" REL_SEG_OFF");
10255 break;
10256
10257 default:
10258 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10259 break;
10260 }
10261 putchar ('\n');
10262 }
10263
10264 static bool
10265 get_32bit_dynamic_section (Filedata * filedata)
10266 {
10267 Elf32_External_Dyn * edyn;
10268 Elf32_External_Dyn * ext;
10269 Elf_Internal_Dyn * entry;
10270
10271 edyn = (Elf32_External_Dyn *) get_data (NULL, filedata,
10272 filedata->dynamic_addr, 1,
10273 filedata->dynamic_size,
10274 _("dynamic section"));
10275 if (!edyn)
10276 return false;
10277
10278 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
10279 might not have the luxury of section headers. Look for the DT_NULL
10280 terminator to determine the number of entries. */
10281 for (ext = edyn, filedata->dynamic_nent = 0;
10282 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
10283 ext++)
10284 {
10285 filedata->dynamic_nent++;
10286 if (BYTE_GET (ext->d_tag) == DT_NULL)
10287 break;
10288 }
10289
10290 filedata->dynamic_section
10291 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
10292 if (filedata->dynamic_section == NULL)
10293 {
10294 error (_("Out of memory allocating space for %lu dynamic entries\n"),
10295 (unsigned long) filedata->dynamic_nent);
10296 free (edyn);
10297 return false;
10298 }
10299
10300 for (ext = edyn, entry = filedata->dynamic_section;
10301 entry < filedata->dynamic_section + filedata->dynamic_nent;
10302 ext++, entry++)
10303 {
10304 entry->d_tag = BYTE_GET (ext->d_tag);
10305 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
10306 }
10307
10308 free (edyn);
10309
10310 return true;
10311 }
10312
10313 static bool
10314 get_64bit_dynamic_section (Filedata * filedata)
10315 {
10316 Elf64_External_Dyn * edyn;
10317 Elf64_External_Dyn * ext;
10318 Elf_Internal_Dyn * entry;
10319
10320 /* Read in the data. */
10321 edyn = (Elf64_External_Dyn *) get_data (NULL, filedata,
10322 filedata->dynamic_addr, 1,
10323 filedata->dynamic_size,
10324 _("dynamic section"));
10325 if (!edyn)
10326 return false;
10327
10328 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
10329 might not have the luxury of section headers. Look for the DT_NULL
10330 terminator to determine the number of entries. */
10331 for (ext = edyn, filedata->dynamic_nent = 0;
10332 /* PR 17533 file: 033-67080-0.004 - do not read past end of buffer. */
10333 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
10334 ext++)
10335 {
10336 filedata->dynamic_nent++;
10337 if (BYTE_GET (ext->d_tag) == DT_NULL)
10338 break;
10339 }
10340
10341 filedata->dynamic_section
10342 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
10343 if (filedata->dynamic_section == NULL)
10344 {
10345 error (_("Out of memory allocating space for %lu dynamic entries\n"),
10346 (unsigned long) filedata->dynamic_nent);
10347 free (edyn);
10348 return false;
10349 }
10350
10351 /* Convert from external to internal formats. */
10352 for (ext = edyn, entry = filedata->dynamic_section;
10353 entry < filedata->dynamic_section + filedata->dynamic_nent;
10354 ext++, entry++)
10355 {
10356 entry->d_tag = BYTE_GET (ext->d_tag);
10357 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
10358 }
10359
10360 free (edyn);
10361
10362 return true;
10363 }
10364
10365 static bool
10366 get_dynamic_section (Filedata *filedata)
10367 {
10368 if (filedata->dynamic_section)
10369 return true;
10370
10371 if (is_32bit_elf)
10372 return get_32bit_dynamic_section (filedata);
10373 else
10374 return get_64bit_dynamic_section (filedata);
10375 }
10376
10377 static void
10378 print_dynamic_flags (bfd_vma flags)
10379 {
10380 bool first = true;
10381
10382 while (flags)
10383 {
10384 bfd_vma flag;
10385
10386 flag = flags & - flags;
10387 flags &= ~ flag;
10388
10389 if (first)
10390 first = false;
10391 else
10392 putc (' ', stdout);
10393
10394 switch (flag)
10395 {
10396 case DF_ORIGIN: fputs ("ORIGIN", stdout); break;
10397 case DF_SYMBOLIC: fputs ("SYMBOLIC", stdout); break;
10398 case DF_TEXTREL: fputs ("TEXTREL", stdout); break;
10399 case DF_BIND_NOW: fputs ("BIND_NOW", stdout); break;
10400 case DF_STATIC_TLS: fputs ("STATIC_TLS", stdout); break;
10401 default: fputs (_("unknown"), stdout); break;
10402 }
10403 }
10404 puts ("");
10405 }
10406
10407 static bfd_vma *
10408 get_dynamic_data (Filedata * filedata, bfd_size_type number, unsigned int ent_size)
10409 {
10410 unsigned char * e_data;
10411 bfd_vma * i_data;
10412
10413 /* If the size_t type is smaller than the bfd_size_type, eg because
10414 you are building a 32-bit tool on a 64-bit host, then make sure
10415 that when (number) is cast to (size_t) no information is lost. */
10416 if (sizeof (size_t) < sizeof (bfd_size_type)
10417 && (bfd_size_type) ((size_t) number) != number)
10418 {
10419 error (_("Size truncation prevents reading %s elements of size %u\n"),
10420 bfd_vmatoa ("u", number), ent_size);
10421 return NULL;
10422 }
10423
10424 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
10425 attempting to allocate memory when the read is bound to fail. */
10426 if (ent_size * number > filedata->file_size)
10427 {
10428 error (_("Invalid number of dynamic entries: %s\n"),
10429 bfd_vmatoa ("u", number));
10430 return NULL;
10431 }
10432
10433 e_data = (unsigned char *) cmalloc ((size_t) number, ent_size);
10434 if (e_data == NULL)
10435 {
10436 error (_("Out of memory reading %s dynamic entries\n"),
10437 bfd_vmatoa ("u", number));
10438 return NULL;
10439 }
10440
10441 if (fread (e_data, ent_size, (size_t) number, filedata->handle) != number)
10442 {
10443 error (_("Unable to read in %s bytes of dynamic data\n"),
10444 bfd_vmatoa ("u", number * ent_size));
10445 free (e_data);
10446 return NULL;
10447 }
10448
10449 i_data = (bfd_vma *) cmalloc ((size_t) number, sizeof (*i_data));
10450 if (i_data == NULL)
10451 {
10452 error (_("Out of memory allocating space for %s dynamic entries\n"),
10453 bfd_vmatoa ("u", number));
10454 free (e_data);
10455 return NULL;
10456 }
10457
10458 while (number--)
10459 i_data[number] = byte_get (e_data + number * ent_size, ent_size);
10460
10461 free (e_data);
10462
10463 return i_data;
10464 }
10465
10466 static unsigned long
10467 get_num_dynamic_syms (Filedata * filedata)
10468 {
10469 unsigned long num_of_syms = 0;
10470
10471 if (!do_histogram && (!do_using_dynamic || do_dyn_syms))
10472 return num_of_syms;
10473
10474 if (filedata->dynamic_info[DT_HASH])
10475 {
10476 unsigned char nb[8];
10477 unsigned char nc[8];
10478 unsigned int hash_ent_size = 4;
10479
10480 if ((filedata->file_header.e_machine == EM_ALPHA
10481 || filedata->file_header.e_machine == EM_S390
10482 || filedata->file_header.e_machine == EM_S390_OLD)
10483 && filedata->file_header.e_ident[EI_CLASS] == ELFCLASS64)
10484 hash_ent_size = 8;
10485
10486 if (fseek (filedata->handle,
10487 (filedata->archive_file_offset
10488 + offset_from_vma (filedata, filedata->dynamic_info[DT_HASH],
10489 sizeof nb + sizeof nc)),
10490 SEEK_SET))
10491 {
10492 error (_("Unable to seek to start of dynamic information\n"));
10493 goto no_hash;
10494 }
10495
10496 if (fread (nb, hash_ent_size, 1, filedata->handle) != 1)
10497 {
10498 error (_("Failed to read in number of buckets\n"));
10499 goto no_hash;
10500 }
10501
10502 if (fread (nc, hash_ent_size, 1, filedata->handle) != 1)
10503 {
10504 error (_("Failed to read in number of chains\n"));
10505 goto no_hash;
10506 }
10507
10508 filedata->nbuckets = byte_get (nb, hash_ent_size);
10509 filedata->nchains = byte_get (nc, hash_ent_size);
10510
10511 if (filedata->nbuckets != 0 && filedata->nchains != 0)
10512 {
10513 filedata->buckets = get_dynamic_data (filedata, filedata->nbuckets,
10514 hash_ent_size);
10515 filedata->chains = get_dynamic_data (filedata, filedata->nchains,
10516 hash_ent_size);
10517
10518 if (filedata->buckets != NULL && filedata->chains != NULL)
10519 num_of_syms = filedata->nchains;
10520 }
10521 no_hash:
10522 if (num_of_syms == 0)
10523 {
10524 free (filedata->buckets);
10525 filedata->buckets = NULL;
10526 free (filedata->chains);
10527 filedata->chains = NULL;
10528 filedata->nbuckets = 0;
10529 }
10530 }
10531
10532 if (filedata->dynamic_info_DT_GNU_HASH)
10533 {
10534 unsigned char nb[16];
10535 bfd_vma i, maxchain = 0xffffffff, bitmaskwords;
10536 bfd_vma buckets_vma;
10537 unsigned long hn;
10538
10539 if (fseek (filedata->handle,
10540 (filedata->archive_file_offset
10541 + offset_from_vma (filedata,
10542 filedata->dynamic_info_DT_GNU_HASH,
10543 sizeof nb)),
10544 SEEK_SET))
10545 {
10546 error (_("Unable to seek to start of dynamic information\n"));
10547 goto no_gnu_hash;
10548 }
10549
10550 if (fread (nb, 16, 1, filedata->handle) != 1)
10551 {
10552 error (_("Failed to read in number of buckets\n"));
10553 goto no_gnu_hash;
10554 }
10555
10556 filedata->ngnubuckets = byte_get (nb, 4);
10557 filedata->gnusymidx = byte_get (nb + 4, 4);
10558 bitmaskwords = byte_get (nb + 8, 4);
10559 buckets_vma = filedata->dynamic_info_DT_GNU_HASH + 16;
10560 if (is_32bit_elf)
10561 buckets_vma += bitmaskwords * 4;
10562 else
10563 buckets_vma += bitmaskwords * 8;
10564
10565 if (fseek (filedata->handle,
10566 (filedata->archive_file_offset
10567 + offset_from_vma (filedata, buckets_vma, 4)),
10568 SEEK_SET))
10569 {
10570 error (_("Unable to seek to start of dynamic information\n"));
10571 goto no_gnu_hash;
10572 }
10573
10574 filedata->gnubuckets
10575 = get_dynamic_data (filedata, filedata->ngnubuckets, 4);
10576
10577 if (filedata->gnubuckets == NULL)
10578 goto no_gnu_hash;
10579
10580 for (i = 0; i < filedata->ngnubuckets; i++)
10581 if (filedata->gnubuckets[i] != 0)
10582 {
10583 if (filedata->gnubuckets[i] < filedata->gnusymidx)
10584 goto no_gnu_hash;
10585
10586 if (maxchain == 0xffffffff || filedata->gnubuckets[i] > maxchain)
10587 maxchain = filedata->gnubuckets[i];
10588 }
10589
10590 if (maxchain == 0xffffffff)
10591 goto no_gnu_hash;
10592
10593 maxchain -= filedata->gnusymidx;
10594
10595 if (fseek (filedata->handle,
10596 (filedata->archive_file_offset
10597 + offset_from_vma (filedata,
10598 buckets_vma + 4 * (filedata->ngnubuckets
10599 + maxchain),
10600 4)),
10601 SEEK_SET))
10602 {
10603 error (_("Unable to seek to start of dynamic information\n"));
10604 goto no_gnu_hash;
10605 }
10606
10607 do
10608 {
10609 if (fread (nb, 4, 1, filedata->handle) != 1)
10610 {
10611 error (_("Failed to determine last chain length\n"));
10612 goto no_gnu_hash;
10613 }
10614
10615 if (maxchain + 1 == 0)
10616 goto no_gnu_hash;
10617
10618 ++maxchain;
10619 }
10620 while ((byte_get (nb, 4) & 1) == 0);
10621
10622 if (fseek (filedata->handle,
10623 (filedata->archive_file_offset
10624 + offset_from_vma (filedata, (buckets_vma
10625 + 4 * filedata->ngnubuckets),
10626 4)),
10627 SEEK_SET))
10628 {
10629 error (_("Unable to seek to start of dynamic information\n"));
10630 goto no_gnu_hash;
10631 }
10632
10633 filedata->gnuchains = get_dynamic_data (filedata, maxchain, 4);
10634 filedata->ngnuchains = maxchain;
10635
10636 if (filedata->gnuchains == NULL)
10637 goto no_gnu_hash;
10638
10639 if (filedata->dynamic_info_DT_MIPS_XHASH)
10640 {
10641 if (fseek (filedata->handle,
10642 (filedata->archive_file_offset
10643 + offset_from_vma (filedata, (buckets_vma
10644 + 4 * (filedata->ngnubuckets
10645 + maxchain)), 4)),
10646 SEEK_SET))
10647 {
10648 error (_("Unable to seek to start of dynamic information\n"));
10649 goto no_gnu_hash;
10650 }
10651
10652 filedata->mipsxlat = get_dynamic_data (filedata, maxchain, 4);
10653 if (filedata->mipsxlat == NULL)
10654 goto no_gnu_hash;
10655 }
10656
10657 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
10658 if (filedata->gnubuckets[hn] != 0)
10659 {
10660 bfd_vma si = filedata->gnubuckets[hn];
10661 bfd_vma off = si - filedata->gnusymidx;
10662
10663 do
10664 {
10665 if (filedata->dynamic_info_DT_MIPS_XHASH)
10666 {
10667 if (off < filedata->ngnuchains
10668 && filedata->mipsxlat[off] >= num_of_syms)
10669 num_of_syms = filedata->mipsxlat[off] + 1;
10670 }
10671 else
10672 {
10673 if (si >= num_of_syms)
10674 num_of_syms = si + 1;
10675 }
10676 si++;
10677 }
10678 while (off < filedata->ngnuchains
10679 && (filedata->gnuchains[off++] & 1) == 0);
10680 }
10681
10682 if (num_of_syms == 0)
10683 {
10684 no_gnu_hash:
10685 free (filedata->mipsxlat);
10686 filedata->mipsxlat = NULL;
10687 free (filedata->gnuchains);
10688 filedata->gnuchains = NULL;
10689 free (filedata->gnubuckets);
10690 filedata->gnubuckets = NULL;
10691 filedata->ngnubuckets = 0;
10692 filedata->ngnuchains = 0;
10693 }
10694 }
10695
10696 return num_of_syms;
10697 }
10698
10699 /* Parse and display the contents of the dynamic section. */
10700
10701 static bool
10702 process_dynamic_section (Filedata * filedata)
10703 {
10704 Elf_Internal_Dyn * entry;
10705
10706 if (filedata->dynamic_size <= 1)
10707 {
10708 if (do_dynamic)
10709 {
10710 if (filedata->is_separate)
10711 printf (_("\nThere is no dynamic section in linked file '%s'.\n"),
10712 filedata->file_name);
10713 else
10714 printf (_("\nThere is no dynamic section in this file.\n"));
10715 }
10716
10717 return true;
10718 }
10719
10720 if (!get_dynamic_section (filedata))
10721 return false;
10722
10723 /* Find the appropriate symbol table. */
10724 if (filedata->dynamic_symbols == NULL || do_histogram)
10725 {
10726 unsigned long num_of_syms;
10727
10728 for (entry = filedata->dynamic_section;
10729 entry < filedata->dynamic_section + filedata->dynamic_nent;
10730 ++entry)
10731 if (entry->d_tag == DT_SYMTAB)
10732 filedata->dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
10733 else if (entry->d_tag == DT_SYMENT)
10734 filedata->dynamic_info[DT_SYMENT] = entry->d_un.d_val;
10735 else if (entry->d_tag == DT_HASH)
10736 filedata->dynamic_info[DT_HASH] = entry->d_un.d_val;
10737 else if (entry->d_tag == DT_GNU_HASH)
10738 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10739 else if ((filedata->file_header.e_machine == EM_MIPS
10740 || filedata->file_header.e_machine == EM_MIPS_RS3_LE)
10741 && entry->d_tag == DT_MIPS_XHASH)
10742 {
10743 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
10744 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10745 }
10746
10747 num_of_syms = get_num_dynamic_syms (filedata);
10748
10749 if (num_of_syms != 0
10750 && filedata->dynamic_symbols == NULL
10751 && filedata->dynamic_info[DT_SYMTAB]
10752 && filedata->dynamic_info[DT_SYMENT])
10753 {
10754 Elf_Internal_Phdr *seg;
10755 bfd_vma vma = filedata->dynamic_info[DT_SYMTAB];
10756
10757 if (! get_program_headers (filedata))
10758 {
10759 error (_("Cannot interpret virtual addresses "
10760 "without program headers.\n"));
10761 return false;
10762 }
10763
10764 for (seg = filedata->program_headers;
10765 seg < filedata->program_headers + filedata->file_header.e_phnum;
10766 ++seg)
10767 {
10768 if (seg->p_type != PT_LOAD)
10769 continue;
10770
10771 if (seg->p_offset + seg->p_filesz > filedata->file_size)
10772 {
10773 /* See PR 21379 for a reproducer. */
10774 error (_("Invalid PT_LOAD entry\n"));
10775 return false;
10776 }
10777
10778 if (vma >= (seg->p_vaddr & -seg->p_align)
10779 && vma < seg->p_vaddr + seg->p_filesz)
10780 {
10781 /* Since we do not know how big the symbol table is,
10782 we default to reading in up to the end of PT_LOAD
10783 segment and processing that. This is overkill, I
10784 know, but it should work. */
10785 Elf_Internal_Shdr section;
10786 section.sh_offset = (vma - seg->p_vaddr
10787 + seg->p_offset);
10788 section.sh_size = (num_of_syms
10789 * filedata->dynamic_info[DT_SYMENT]);
10790 section.sh_entsize = filedata->dynamic_info[DT_SYMENT];
10791
10792 if (do_checks
10793 && filedata->dynamic_symtab_section != NULL
10794 && ((filedata->dynamic_symtab_section->sh_offset
10795 != section.sh_offset)
10796 || (filedata->dynamic_symtab_section->sh_size
10797 != section.sh_size)
10798 || (filedata->dynamic_symtab_section->sh_entsize
10799 != section.sh_entsize)))
10800 warn (_("\
10801 the .dynsym section doesn't match the DT_SYMTAB and DT_SYMENT tags\n"));
10802
10803 section.sh_name = filedata->string_table_length;
10804 filedata->dynamic_symbols
10805 = get_elf_symbols (filedata, &section,
10806 &filedata->num_dynamic_syms);
10807 if (filedata->dynamic_symbols == NULL
10808 || filedata->num_dynamic_syms != num_of_syms)
10809 {
10810 error (_("Corrupt DT_SYMTAB dynamic entry\n"));
10811 return false;
10812 }
10813 break;
10814 }
10815 }
10816 }
10817 }
10818
10819 /* Similarly find a string table. */
10820 if (filedata->dynamic_strings == NULL)
10821 for (entry = filedata->dynamic_section;
10822 entry < filedata->dynamic_section + filedata->dynamic_nent;
10823 ++entry)
10824 {
10825 if (entry->d_tag == DT_STRTAB)
10826 filedata->dynamic_info[DT_STRTAB] = entry->d_un.d_val;
10827
10828 if (entry->d_tag == DT_STRSZ)
10829 filedata->dynamic_info[DT_STRSZ] = entry->d_un.d_val;
10830
10831 if (filedata->dynamic_info[DT_STRTAB]
10832 && filedata->dynamic_info[DT_STRSZ])
10833 {
10834 unsigned long offset;
10835 bfd_size_type str_tab_len = filedata->dynamic_info[DT_STRSZ];
10836
10837 offset = offset_from_vma (filedata,
10838 filedata->dynamic_info[DT_STRTAB],
10839 str_tab_len);
10840 if (do_checks
10841 && filedata->dynamic_strtab_section
10842 && ((filedata->dynamic_strtab_section->sh_offset
10843 != (file_ptr) offset)
10844 || (filedata->dynamic_strtab_section->sh_size
10845 != str_tab_len)))
10846 warn (_("\
10847 the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
10848
10849 filedata->dynamic_strings
10850 = (char *) get_data (NULL, filedata, offset, 1, str_tab_len,
10851 _("dynamic string table"));
10852 if (filedata->dynamic_strings == NULL)
10853 {
10854 error (_("Corrupt DT_STRTAB dynamic entry\n"));
10855 break;
10856 }
10857
10858 filedata->dynamic_strings_length = str_tab_len;
10859 break;
10860 }
10861 }
10862
10863 /* And find the syminfo section if available. */
10864 if (filedata->dynamic_syminfo == NULL)
10865 {
10866 unsigned long syminsz = 0;
10867
10868 for (entry = filedata->dynamic_section;
10869 entry < filedata->dynamic_section + filedata->dynamic_nent;
10870 ++entry)
10871 {
10872 if (entry->d_tag == DT_SYMINENT)
10873 {
10874 /* Note: these braces are necessary to avoid a syntax
10875 error from the SunOS4 C compiler. */
10876 /* PR binutils/17531: A corrupt file can trigger this test.
10877 So do not use an assert, instead generate an error message. */
10878 if (sizeof (Elf_External_Syminfo) != entry->d_un.d_val)
10879 error (_("Bad value (%d) for SYMINENT entry\n"),
10880 (int) entry->d_un.d_val);
10881 }
10882 else if (entry->d_tag == DT_SYMINSZ)
10883 syminsz = entry->d_un.d_val;
10884 else if (entry->d_tag == DT_SYMINFO)
10885 filedata->dynamic_syminfo_offset
10886 = offset_from_vma (filedata, entry->d_un.d_val, syminsz);
10887 }
10888
10889 if (filedata->dynamic_syminfo_offset != 0 && syminsz != 0)
10890 {
10891 Elf_External_Syminfo * extsyminfo;
10892 Elf_External_Syminfo * extsym;
10893 Elf_Internal_Syminfo * syminfo;
10894
10895 /* There is a syminfo section. Read the data. */
10896 extsyminfo = (Elf_External_Syminfo *)
10897 get_data (NULL, filedata, filedata->dynamic_syminfo_offset,
10898 1, syminsz, _("symbol information"));
10899 if (!extsyminfo)
10900 return false;
10901
10902 if (filedata->dynamic_syminfo != NULL)
10903 {
10904 error (_("Multiple dynamic symbol information sections found\n"));
10905 free (filedata->dynamic_syminfo);
10906 }
10907 filedata->dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
10908 if (filedata->dynamic_syminfo == NULL)
10909 {
10910 error (_("Out of memory allocating %lu bytes "
10911 "for dynamic symbol info\n"),
10912 (unsigned long) syminsz);
10913 return false;
10914 }
10915
10916 filedata->dynamic_syminfo_nent
10917 = syminsz / sizeof (Elf_External_Syminfo);
10918 for (syminfo = filedata->dynamic_syminfo, extsym = extsyminfo;
10919 syminfo < (filedata->dynamic_syminfo
10920 + filedata->dynamic_syminfo_nent);
10921 ++syminfo, ++extsym)
10922 {
10923 syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
10924 syminfo->si_flags = BYTE_GET (extsym->si_flags);
10925 }
10926
10927 free (extsyminfo);
10928 }
10929 }
10930
10931 if (do_dynamic && filedata->dynamic_addr)
10932 {
10933 if (filedata->dynamic_nent == 1)
10934 {
10935 if (filedata->is_separate)
10936 printf (_("\nIn linked file '%s' the dynamic section at offset 0x%lx contains 1 entry:\n"),
10937 filedata->file_name,
10938 filedata->dynamic_addr);
10939 else
10940 printf (_("\nDynamic section at offset 0x%lx contains 1 entry:\n"),
10941 filedata->dynamic_addr);
10942 }
10943 else
10944 {
10945 if (filedata->is_separate)
10946 printf (_("\nIn linked file '%s' the dynamic section at offset 0x%lx contains %lu entries:\n"),
10947 filedata->file_name,
10948 filedata->dynamic_addr,
10949 (unsigned long) filedata->dynamic_nent);
10950 else
10951 printf (_("\nDynamic section at offset 0x%lx contains %lu entries:\n"),
10952 filedata->dynamic_addr,
10953 (unsigned long) filedata->dynamic_nent);
10954 }
10955 }
10956 if (do_dynamic)
10957 printf (_(" Tag Type Name/Value\n"));
10958
10959 for (entry = filedata->dynamic_section;
10960 entry < filedata->dynamic_section + filedata->dynamic_nent;
10961 entry++)
10962 {
10963 if (do_dynamic)
10964 {
10965 const char * dtype;
10966
10967 putchar (' ');
10968 print_vma (entry->d_tag, FULL_HEX);
10969 dtype = get_dynamic_type (filedata, entry->d_tag);
10970 printf (" (%s)%*s", dtype,
10971 ((is_32bit_elf ? 27 : 19) - (int) strlen (dtype)), " ");
10972 }
10973
10974 switch (entry->d_tag)
10975 {
10976 case DT_FLAGS:
10977 if (do_dynamic)
10978 print_dynamic_flags (entry->d_un.d_val);
10979 break;
10980
10981 case DT_AUXILIARY:
10982 case DT_FILTER:
10983 case DT_CONFIG:
10984 case DT_DEPAUDIT:
10985 case DT_AUDIT:
10986 if (do_dynamic)
10987 {
10988 switch (entry->d_tag)
10989 {
10990 case DT_AUXILIARY:
10991 printf (_("Auxiliary library"));
10992 break;
10993
10994 case DT_FILTER:
10995 printf (_("Filter library"));
10996 break;
10997
10998 case DT_CONFIG:
10999 printf (_("Configuration file"));
11000 break;
11001
11002 case DT_DEPAUDIT:
11003 printf (_("Dependency audit library"));
11004 break;
11005
11006 case DT_AUDIT:
11007 printf (_("Audit library"));
11008 break;
11009 }
11010
11011 if (VALID_DYNAMIC_NAME (filedata, entry->d_un.d_val))
11012 printf (": [%s]\n",
11013 GET_DYNAMIC_NAME (filedata, entry->d_un.d_val));
11014 else
11015 {
11016 printf (": ");
11017 print_vma (entry->d_un.d_val, PREFIX_HEX);
11018 putchar ('\n');
11019 }
11020 }
11021 break;
11022
11023 case DT_FEATURE:
11024 if (do_dynamic)
11025 {
11026 printf (_("Flags:"));
11027
11028 if (entry->d_un.d_val == 0)
11029 printf (_(" None\n"));
11030 else
11031 {
11032 unsigned long int val = entry->d_un.d_val;
11033
11034 if (val & DTF_1_PARINIT)
11035 {
11036 printf (" PARINIT");
11037 val ^= DTF_1_PARINIT;
11038 }
11039 if (val & DTF_1_CONFEXP)
11040 {
11041 printf (" CONFEXP");
11042 val ^= DTF_1_CONFEXP;
11043 }
11044 if (val != 0)
11045 printf (" %lx", val);
11046 puts ("");
11047 }
11048 }
11049 break;
11050
11051 case DT_POSFLAG_1:
11052 if (do_dynamic)
11053 {
11054 printf (_("Flags:"));
11055
11056 if (entry->d_un.d_val == 0)
11057 printf (_(" None\n"));
11058 else
11059 {
11060 unsigned long int val = entry->d_un.d_val;
11061
11062 if (val & DF_P1_LAZYLOAD)
11063 {
11064 printf (" LAZYLOAD");
11065 val ^= DF_P1_LAZYLOAD;
11066 }
11067 if (val & DF_P1_GROUPPERM)
11068 {
11069 printf (" GROUPPERM");
11070 val ^= DF_P1_GROUPPERM;
11071 }
11072 if (val != 0)
11073 printf (" %lx", val);
11074 puts ("");
11075 }
11076 }
11077 break;
11078
11079 case DT_FLAGS_1:
11080 if (do_dynamic)
11081 {
11082 printf (_("Flags:"));
11083 if (entry->d_un.d_val == 0)
11084 printf (_(" None\n"));
11085 else
11086 {
11087 unsigned long int val = entry->d_un.d_val;
11088
11089 if (val & DF_1_NOW)
11090 {
11091 printf (" NOW");
11092 val ^= DF_1_NOW;
11093 }
11094 if (val & DF_1_GLOBAL)
11095 {
11096 printf (" GLOBAL");
11097 val ^= DF_1_GLOBAL;
11098 }
11099 if (val & DF_1_GROUP)
11100 {
11101 printf (" GROUP");
11102 val ^= DF_1_GROUP;
11103 }
11104 if (val & DF_1_NODELETE)
11105 {
11106 printf (" NODELETE");
11107 val ^= DF_1_NODELETE;
11108 }
11109 if (val & DF_1_LOADFLTR)
11110 {
11111 printf (" LOADFLTR");
11112 val ^= DF_1_LOADFLTR;
11113 }
11114 if (val & DF_1_INITFIRST)
11115 {
11116 printf (" INITFIRST");
11117 val ^= DF_1_INITFIRST;
11118 }
11119 if (val & DF_1_NOOPEN)
11120 {
11121 printf (" NOOPEN");
11122 val ^= DF_1_NOOPEN;
11123 }
11124 if (val & DF_1_ORIGIN)
11125 {
11126 printf (" ORIGIN");
11127 val ^= DF_1_ORIGIN;
11128 }
11129 if (val & DF_1_DIRECT)
11130 {
11131 printf (" DIRECT");
11132 val ^= DF_1_DIRECT;
11133 }
11134 if (val & DF_1_TRANS)
11135 {
11136 printf (" TRANS");
11137 val ^= DF_1_TRANS;
11138 }
11139 if (val & DF_1_INTERPOSE)
11140 {
11141 printf (" INTERPOSE");
11142 val ^= DF_1_INTERPOSE;
11143 }
11144 if (val & DF_1_NODEFLIB)
11145 {
11146 printf (" NODEFLIB");
11147 val ^= DF_1_NODEFLIB;
11148 }
11149 if (val & DF_1_NODUMP)
11150 {
11151 printf (" NODUMP");
11152 val ^= DF_1_NODUMP;
11153 }
11154 if (val & DF_1_CONFALT)
11155 {
11156 printf (" CONFALT");
11157 val ^= DF_1_CONFALT;
11158 }
11159 if (val & DF_1_ENDFILTEE)
11160 {
11161 printf (" ENDFILTEE");
11162 val ^= DF_1_ENDFILTEE;
11163 }
11164 if (val & DF_1_DISPRELDNE)
11165 {
11166 printf (" DISPRELDNE");
11167 val ^= DF_1_DISPRELDNE;
11168 }
11169 if (val & DF_1_DISPRELPND)
11170 {
11171 printf (" DISPRELPND");
11172 val ^= DF_1_DISPRELPND;
11173 }
11174 if (val & DF_1_NODIRECT)
11175 {
11176 printf (" NODIRECT");
11177 val ^= DF_1_NODIRECT;
11178 }
11179 if (val & DF_1_IGNMULDEF)
11180 {
11181 printf (" IGNMULDEF");
11182 val ^= DF_1_IGNMULDEF;
11183 }
11184 if (val & DF_1_NOKSYMS)
11185 {
11186 printf (" NOKSYMS");
11187 val ^= DF_1_NOKSYMS;
11188 }
11189 if (val & DF_1_NOHDR)
11190 {
11191 printf (" NOHDR");
11192 val ^= DF_1_NOHDR;
11193 }
11194 if (val & DF_1_EDITED)
11195 {
11196 printf (" EDITED");
11197 val ^= DF_1_EDITED;
11198 }
11199 if (val & DF_1_NORELOC)
11200 {
11201 printf (" NORELOC");
11202 val ^= DF_1_NORELOC;
11203 }
11204 if (val & DF_1_SYMINTPOSE)
11205 {
11206 printf (" SYMINTPOSE");
11207 val ^= DF_1_SYMINTPOSE;
11208 }
11209 if (val & DF_1_GLOBAUDIT)
11210 {
11211 printf (" GLOBAUDIT");
11212 val ^= DF_1_GLOBAUDIT;
11213 }
11214 if (val & DF_1_SINGLETON)
11215 {
11216 printf (" SINGLETON");
11217 val ^= DF_1_SINGLETON;
11218 }
11219 if (val & DF_1_STUB)
11220 {
11221 printf (" STUB");
11222 val ^= DF_1_STUB;
11223 }
11224 if (val & DF_1_PIE)
11225 {
11226 printf (" PIE");
11227 val ^= DF_1_PIE;
11228 }
11229 if (val & DF_1_KMOD)
11230 {
11231 printf (" KMOD");
11232 val ^= DF_1_KMOD;
11233 }
11234 if (val & DF_1_WEAKFILTER)
11235 {
11236 printf (" WEAKFILTER");
11237 val ^= DF_1_WEAKFILTER;
11238 }
11239 if (val & DF_1_NOCOMMON)
11240 {
11241 printf (" NOCOMMON");
11242 val ^= DF_1_NOCOMMON;
11243 }
11244 if (val != 0)
11245 printf (" %lx", val);
11246 puts ("");
11247 }
11248 }
11249 break;
11250
11251 case DT_PLTREL:
11252 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11253 if (do_dynamic)
11254 puts (get_dynamic_type (filedata, entry->d_un.d_val));
11255 break;
11256
11257 case DT_NULL :
11258 case DT_NEEDED :
11259 case DT_PLTGOT :
11260 case DT_HASH :
11261 case DT_STRTAB :
11262 case DT_SYMTAB :
11263 case DT_RELA :
11264 case DT_INIT :
11265 case DT_FINI :
11266 case DT_SONAME :
11267 case DT_RPATH :
11268 case DT_SYMBOLIC:
11269 case DT_REL :
11270 case DT_DEBUG :
11271 case DT_TEXTREL :
11272 case DT_JMPREL :
11273 case DT_RUNPATH :
11274 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11275
11276 if (do_dynamic)
11277 {
11278 char * name;
11279
11280 if (VALID_DYNAMIC_NAME (filedata, entry->d_un.d_val))
11281 name = GET_DYNAMIC_NAME (filedata, entry->d_un.d_val);
11282 else
11283 name = NULL;
11284
11285 if (name)
11286 {
11287 switch (entry->d_tag)
11288 {
11289 case DT_NEEDED:
11290 printf (_("Shared library: [%s]"), name);
11291
11292 if (filedata->program_interpreter
11293 && streq (name, filedata->program_interpreter))
11294 printf (_(" program interpreter"));
11295 break;
11296
11297 case DT_SONAME:
11298 printf (_("Library soname: [%s]"), name);
11299 break;
11300
11301 case DT_RPATH:
11302 printf (_("Library rpath: [%s]"), name);
11303 break;
11304
11305 case DT_RUNPATH:
11306 printf (_("Library runpath: [%s]"), name);
11307 break;
11308
11309 default:
11310 print_vma (entry->d_un.d_val, PREFIX_HEX);
11311 break;
11312 }
11313 }
11314 else
11315 print_vma (entry->d_un.d_val, PREFIX_HEX);
11316
11317 putchar ('\n');
11318 }
11319 break;
11320
11321 case DT_PLTRELSZ:
11322 case DT_RELASZ :
11323 case DT_STRSZ :
11324 case DT_RELSZ :
11325 case DT_RELAENT :
11326 case DT_SYMENT :
11327 case DT_RELENT :
11328 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11329 /* Fall through. */
11330 case DT_PLTPADSZ:
11331 case DT_MOVEENT :
11332 case DT_MOVESZ :
11333 case DT_INIT_ARRAYSZ:
11334 case DT_FINI_ARRAYSZ:
11335 case DT_GNU_CONFLICTSZ:
11336 case DT_GNU_LIBLISTSZ:
11337 if (do_dynamic)
11338 {
11339 print_vma (entry->d_un.d_val, UNSIGNED);
11340 printf (_(" (bytes)\n"));
11341 }
11342 break;
11343
11344 case DT_VERDEFNUM:
11345 case DT_VERNEEDNUM:
11346 case DT_RELACOUNT:
11347 case DT_RELCOUNT:
11348 if (do_dynamic)
11349 {
11350 print_vma (entry->d_un.d_val, UNSIGNED);
11351 putchar ('\n');
11352 }
11353 break;
11354
11355 case DT_SYMINSZ:
11356 case DT_SYMINENT:
11357 case DT_SYMINFO:
11358 case DT_USED:
11359 case DT_INIT_ARRAY:
11360 case DT_FINI_ARRAY:
11361 if (do_dynamic)
11362 {
11363 if (entry->d_tag == DT_USED
11364 && VALID_DYNAMIC_NAME (filedata, entry->d_un.d_val))
11365 {
11366 char * name = GET_DYNAMIC_NAME (filedata, entry->d_un.d_val);
11367
11368 if (*name)
11369 {
11370 printf (_("Not needed object: [%s]\n"), name);
11371 break;
11372 }
11373 }
11374
11375 print_vma (entry->d_un.d_val, PREFIX_HEX);
11376 putchar ('\n');
11377 }
11378 break;
11379
11380 case DT_BIND_NOW:
11381 /* The value of this entry is ignored. */
11382 if (do_dynamic)
11383 putchar ('\n');
11384 break;
11385
11386 case DT_GNU_PRELINKED:
11387 if (do_dynamic)
11388 {
11389 struct tm * tmp;
11390 time_t atime = entry->d_un.d_val;
11391
11392 tmp = gmtime (&atime);
11393 /* PR 17533 file: 041-1244816-0.004. */
11394 if (tmp == NULL)
11395 printf (_("<corrupt time val: %lx"),
11396 (unsigned long) atime);
11397 else
11398 printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
11399 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
11400 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
11401
11402 }
11403 break;
11404
11405 case DT_GNU_HASH:
11406 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11407 if (do_dynamic)
11408 {
11409 print_vma (entry->d_un.d_val, PREFIX_HEX);
11410 putchar ('\n');
11411 }
11412 break;
11413
11414 case DT_GNU_FLAGS_1:
11415 if (do_dynamic)
11416 {
11417 printf (_("Flags:"));
11418 if (entry->d_un.d_val == 0)
11419 printf (_(" None\n"));
11420 else
11421 {
11422 unsigned long int val = entry->d_un.d_val;
11423
11424 if (val & DF_GNU_1_UNIQUE)
11425 {
11426 printf (" UNIQUE");
11427 val ^= DF_GNU_1_UNIQUE;
11428 }
11429 if (val != 0)
11430 printf (" %lx", val);
11431 puts ("");
11432 }
11433 }
11434 break;
11435
11436 default:
11437 if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
11438 filedata->version_info[DT_VERSIONTAGIDX (entry->d_tag)]
11439 = entry->d_un.d_val;
11440
11441 if (do_dynamic)
11442 {
11443 switch (filedata->file_header.e_machine)
11444 {
11445 case EM_AARCH64:
11446 dynamic_section_aarch64_val (entry);
11447 break;
11448 case EM_MIPS:
11449 case EM_MIPS_RS3_LE:
11450 dynamic_section_mips_val (filedata, entry);
11451 break;
11452 case EM_PARISC:
11453 dynamic_section_parisc_val (entry);
11454 break;
11455 case EM_IA_64:
11456 dynamic_section_ia64_val (entry);
11457 break;
11458 default:
11459 print_vma (entry->d_un.d_val, PREFIX_HEX);
11460 putchar ('\n');
11461 }
11462 }
11463 break;
11464 }
11465 }
11466
11467 return true;
11468 }
11469
11470 static char *
11471 get_ver_flags (unsigned int flags)
11472 {
11473 static char buff[128];
11474
11475 buff[0] = 0;
11476
11477 if (flags == 0)
11478 return _("none");
11479
11480 if (flags & VER_FLG_BASE)
11481 strcat (buff, "BASE");
11482
11483 if (flags & VER_FLG_WEAK)
11484 {
11485 if (flags & VER_FLG_BASE)
11486 strcat (buff, " | ");
11487
11488 strcat (buff, "WEAK");
11489 }
11490
11491 if (flags & VER_FLG_INFO)
11492 {
11493 if (flags & (VER_FLG_BASE|VER_FLG_WEAK))
11494 strcat (buff, " | ");
11495
11496 strcat (buff, "INFO");
11497 }
11498
11499 if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
11500 {
11501 if (flags & (VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
11502 strcat (buff, " | ");
11503
11504 strcat (buff, _("<unknown>"));
11505 }
11506
11507 return buff;
11508 }
11509
11510 /* Display the contents of the version sections. */
11511
11512 static bool
11513 process_version_sections (Filedata * filedata)
11514 {
11515 Elf_Internal_Shdr * section;
11516 unsigned i;
11517 bool found = false;
11518
11519 if (! do_version)
11520 return true;
11521
11522 for (i = 0, section = filedata->section_headers;
11523 i < filedata->file_header.e_shnum;
11524 i++, section++)
11525 {
11526 switch (section->sh_type)
11527 {
11528 case SHT_GNU_verdef:
11529 {
11530 Elf_External_Verdef * edefs;
11531 unsigned long idx;
11532 unsigned long cnt;
11533 char * endbuf;
11534
11535 found = true;
11536
11537 if (filedata->is_separate)
11538 printf (ngettext ("\nIn linked file '%s' the version definition section '%s' contains %u entry:\n",
11539 "\nIn linked file '%s' the version definition section '%s' contains %u entries:\n",
11540 section->sh_info),
11541 filedata->file_name,
11542 printable_section_name (filedata, section),
11543 section->sh_info);
11544 else
11545 printf (ngettext ("\nVersion definition section '%s' "
11546 "contains %u entry:\n",
11547 "\nVersion definition section '%s' "
11548 "contains %u entries:\n",
11549 section->sh_info),
11550 printable_section_name (filedata, section),
11551 section->sh_info);
11552
11553 printf (_(" Addr: 0x"));
11554 printf_vma (section->sh_addr);
11555 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
11556 (unsigned long) section->sh_offset, section->sh_link,
11557 printable_section_name_from_index (filedata, section->sh_link));
11558
11559 edefs = (Elf_External_Verdef *)
11560 get_data (NULL, filedata, section->sh_offset, 1,section->sh_size,
11561 _("version definition section"));
11562 if (!edefs)
11563 break;
11564 endbuf = (char *) edefs + section->sh_size;
11565
11566 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
11567 {
11568 char * vstart;
11569 Elf_External_Verdef * edef;
11570 Elf_Internal_Verdef ent;
11571 Elf_External_Verdaux * eaux;
11572 Elf_Internal_Verdaux aux;
11573 unsigned long isum;
11574 int j;
11575
11576 vstart = ((char *) edefs) + idx;
11577 if (vstart + sizeof (*edef) > endbuf)
11578 break;
11579
11580 edef = (Elf_External_Verdef *) vstart;
11581
11582 ent.vd_version = BYTE_GET (edef->vd_version);
11583 ent.vd_flags = BYTE_GET (edef->vd_flags);
11584 ent.vd_ndx = BYTE_GET (edef->vd_ndx);
11585 ent.vd_cnt = BYTE_GET (edef->vd_cnt);
11586 ent.vd_hash = BYTE_GET (edef->vd_hash);
11587 ent.vd_aux = BYTE_GET (edef->vd_aux);
11588 ent.vd_next = BYTE_GET (edef->vd_next);
11589
11590 printf (_(" %#06lx: Rev: %d Flags: %s"),
11591 idx, ent.vd_version, get_ver_flags (ent.vd_flags));
11592
11593 printf (_(" Index: %d Cnt: %d "),
11594 ent.vd_ndx, ent.vd_cnt);
11595
11596 /* Check for overflow. */
11597 if (ent.vd_aux > (size_t) (endbuf - vstart))
11598 break;
11599
11600 vstart += ent.vd_aux;
11601
11602 if (vstart + sizeof (*eaux) > endbuf)
11603 break;
11604 eaux = (Elf_External_Verdaux *) vstart;
11605
11606 aux.vda_name = BYTE_GET (eaux->vda_name);
11607 aux.vda_next = BYTE_GET (eaux->vda_next);
11608
11609 if (VALID_DYNAMIC_NAME (filedata, aux.vda_name))
11610 printf (_("Name: %s\n"),
11611 GET_DYNAMIC_NAME (filedata, aux.vda_name));
11612 else
11613 printf (_("Name index: %ld\n"), aux.vda_name);
11614
11615 isum = idx + ent.vd_aux;
11616
11617 for (j = 1; j < ent.vd_cnt; j++)
11618 {
11619 if (aux.vda_next < sizeof (*eaux)
11620 && !(j == ent.vd_cnt - 1 && aux.vda_next == 0))
11621 {
11622 warn (_("Invalid vda_next field of %lx\n"),
11623 aux.vda_next);
11624 j = ent.vd_cnt;
11625 break;
11626 }
11627 /* Check for overflow. */
11628 if (aux.vda_next > (size_t) (endbuf - vstart))
11629 break;
11630
11631 isum += aux.vda_next;
11632 vstart += aux.vda_next;
11633
11634 if (vstart + sizeof (*eaux) > endbuf)
11635 break;
11636 eaux = (Elf_External_Verdaux *) vstart;
11637
11638 aux.vda_name = BYTE_GET (eaux->vda_name);
11639 aux.vda_next = BYTE_GET (eaux->vda_next);
11640
11641 if (VALID_DYNAMIC_NAME (filedata, aux.vda_name))
11642 printf (_(" %#06lx: Parent %d: %s\n"),
11643 isum, j,
11644 GET_DYNAMIC_NAME (filedata, aux.vda_name));
11645 else
11646 printf (_(" %#06lx: Parent %d, name index: %ld\n"),
11647 isum, j, aux.vda_name);
11648 }
11649
11650 if (j < ent.vd_cnt)
11651 printf (_(" Version def aux past end of section\n"));
11652
11653 /* PR 17531:
11654 file: id:000001,src:000172+005151,op:splice,rep:2. */
11655 if (ent.vd_next < sizeof (*edef)
11656 && !(cnt == section->sh_info - 1 && ent.vd_next == 0))
11657 {
11658 warn (_("Invalid vd_next field of %lx\n"), ent.vd_next);
11659 cnt = section->sh_info;
11660 break;
11661 }
11662 if (ent.vd_next > (size_t) (endbuf - ((char *) edefs + idx)))
11663 break;
11664
11665 idx += ent.vd_next;
11666 }
11667
11668 if (cnt < section->sh_info)
11669 printf (_(" Version definition past end of section\n"));
11670
11671 free (edefs);
11672 }
11673 break;
11674
11675 case SHT_GNU_verneed:
11676 {
11677 Elf_External_Verneed * eneed;
11678 unsigned long idx;
11679 unsigned long cnt;
11680 char * endbuf;
11681
11682 found = true;
11683
11684 if (filedata->is_separate)
11685 printf (ngettext ("\nIn linked file '%s' the version needs section '%s' contains %u entry:\n",
11686 "\nIn linked file '%s' the version needs section '%s' contains %u entries:\n",
11687 section->sh_info),
11688 filedata->file_name,
11689 printable_section_name (filedata, section),
11690 section->sh_info);
11691 else
11692 printf (ngettext ("\nVersion needs section '%s' "
11693 "contains %u entry:\n",
11694 "\nVersion needs section '%s' "
11695 "contains %u entries:\n",
11696 section->sh_info),
11697 printable_section_name (filedata, section),
11698 section->sh_info);
11699
11700 printf (_(" Addr: 0x"));
11701 printf_vma (section->sh_addr);
11702 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
11703 (unsigned long) section->sh_offset, section->sh_link,
11704 printable_section_name_from_index (filedata, section->sh_link));
11705
11706 eneed = (Elf_External_Verneed *) get_data (NULL, filedata,
11707 section->sh_offset, 1,
11708 section->sh_size,
11709 _("Version Needs section"));
11710 if (!eneed)
11711 break;
11712 endbuf = (char *) eneed + section->sh_size;
11713
11714 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
11715 {
11716 Elf_External_Verneed * entry;
11717 Elf_Internal_Verneed ent;
11718 unsigned long isum;
11719 int j;
11720 char * vstart;
11721
11722 vstart = ((char *) eneed) + idx;
11723 if (vstart + sizeof (*entry) > endbuf)
11724 break;
11725
11726 entry = (Elf_External_Verneed *) vstart;
11727
11728 ent.vn_version = BYTE_GET (entry->vn_version);
11729 ent.vn_cnt = BYTE_GET (entry->vn_cnt);
11730 ent.vn_file = BYTE_GET (entry->vn_file);
11731 ent.vn_aux = BYTE_GET (entry->vn_aux);
11732 ent.vn_next = BYTE_GET (entry->vn_next);
11733
11734 printf (_(" %#06lx: Version: %d"), idx, ent.vn_version);
11735
11736 if (VALID_DYNAMIC_NAME (filedata, ent.vn_file))
11737 printf (_(" File: %s"),
11738 GET_DYNAMIC_NAME (filedata, ent.vn_file));
11739 else
11740 printf (_(" File: %lx"), ent.vn_file);
11741
11742 printf (_(" Cnt: %d\n"), ent.vn_cnt);
11743
11744 /* Check for overflow. */
11745 if (ent.vn_aux > (size_t) (endbuf - vstart))
11746 break;
11747 vstart += ent.vn_aux;
11748
11749 for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
11750 {
11751 Elf_External_Vernaux * eaux;
11752 Elf_Internal_Vernaux aux;
11753
11754 if (vstart + sizeof (*eaux) > endbuf)
11755 break;
11756 eaux = (Elf_External_Vernaux *) vstart;
11757
11758 aux.vna_hash = BYTE_GET (eaux->vna_hash);
11759 aux.vna_flags = BYTE_GET (eaux->vna_flags);
11760 aux.vna_other = BYTE_GET (eaux->vna_other);
11761 aux.vna_name = BYTE_GET (eaux->vna_name);
11762 aux.vna_next = BYTE_GET (eaux->vna_next);
11763
11764 if (VALID_DYNAMIC_NAME (filedata, aux.vna_name))
11765 printf (_(" %#06lx: Name: %s"),
11766 isum, GET_DYNAMIC_NAME (filedata, aux.vna_name));
11767 else
11768 printf (_(" %#06lx: Name index: %lx"),
11769 isum, aux.vna_name);
11770
11771 printf (_(" Flags: %s Version: %d\n"),
11772 get_ver_flags (aux.vna_flags), aux.vna_other);
11773
11774 if (aux.vna_next < sizeof (*eaux)
11775 && !(j == ent.vn_cnt - 1 && aux.vna_next == 0))
11776 {
11777 warn (_("Invalid vna_next field of %lx\n"),
11778 aux.vna_next);
11779 j = ent.vn_cnt;
11780 break;
11781 }
11782 /* Check for overflow. */
11783 if (aux.vna_next > (size_t) (endbuf - vstart))
11784 break;
11785 isum += aux.vna_next;
11786 vstart += aux.vna_next;
11787 }
11788
11789 if (j < ent.vn_cnt)
11790 warn (_("Missing Version Needs auxiliary information\n"));
11791
11792 if (ent.vn_next < sizeof (*entry)
11793 && !(cnt == section->sh_info - 1 && ent.vn_next == 0))
11794 {
11795 warn (_("Invalid vn_next field of %lx\n"), ent.vn_next);
11796 cnt = section->sh_info;
11797 break;
11798 }
11799 if (ent.vn_next > (size_t) (endbuf - ((char *) eneed + idx)))
11800 break;
11801 idx += ent.vn_next;
11802 }
11803
11804 if (cnt < section->sh_info)
11805 warn (_("Missing Version Needs information\n"));
11806
11807 free (eneed);
11808 }
11809 break;
11810
11811 case SHT_GNU_versym:
11812 {
11813 Elf_Internal_Shdr * link_section;
11814 size_t total;
11815 unsigned int cnt;
11816 unsigned char * edata;
11817 unsigned short * data;
11818 char * strtab;
11819 Elf_Internal_Sym * symbols;
11820 Elf_Internal_Shdr * string_sec;
11821 unsigned long num_syms;
11822 long off;
11823
11824 if (section->sh_link >= filedata->file_header.e_shnum)
11825 break;
11826
11827 link_section = filedata->section_headers + section->sh_link;
11828 total = section->sh_size / sizeof (Elf_External_Versym);
11829
11830 if (link_section->sh_link >= filedata->file_header.e_shnum)
11831 break;
11832
11833 found = true;
11834
11835 symbols = get_elf_symbols (filedata, link_section, & num_syms);
11836 if (symbols == NULL)
11837 break;
11838
11839 string_sec = filedata->section_headers + link_section->sh_link;
11840
11841 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
11842 string_sec->sh_size,
11843 _("version string table"));
11844 if (!strtab)
11845 {
11846 free (symbols);
11847 break;
11848 }
11849
11850 if (filedata->is_separate)
11851 printf (ngettext ("\nIn linked file '%s' the version symbols section '%s' contains %lu entry:\n",
11852 "\nIn linked file '%s' the version symbols section '%s' contains %lu entries:\n",
11853 total),
11854 filedata->file_name,
11855 printable_section_name (filedata, section),
11856 (unsigned long) total);
11857 else
11858 printf (ngettext ("\nVersion symbols section '%s' "
11859 "contains %lu entry:\n",
11860 "\nVersion symbols section '%s' "
11861 "contains %lu entries:\n",
11862 total),
11863 printable_section_name (filedata, section),
11864 (unsigned long) total);
11865
11866 printf (_(" Addr: 0x"));
11867 printf_vma (section->sh_addr);
11868 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
11869 (unsigned long) section->sh_offset, section->sh_link,
11870 printable_section_name (filedata, link_section));
11871
11872 off = offset_from_vma (filedata,
11873 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
11874 total * sizeof (short));
11875 edata = (unsigned char *) get_data (NULL, filedata, off,
11876 sizeof (short), total,
11877 _("version symbol data"));
11878 if (!edata)
11879 {
11880 free (strtab);
11881 free (symbols);
11882 break;
11883 }
11884
11885 data = (short unsigned int *) cmalloc (total, sizeof (short));
11886
11887 for (cnt = total; cnt --;)
11888 data[cnt] = byte_get (edata + cnt * sizeof (short),
11889 sizeof (short));
11890
11891 free (edata);
11892
11893 for (cnt = 0; cnt < total; cnt += 4)
11894 {
11895 int j, nn;
11896 char *name;
11897 char *invalid = _("*invalid*");
11898
11899 printf (" %03x:", cnt);
11900
11901 for (j = 0; (j < 4) && (cnt + j) < total; ++j)
11902 switch (data[cnt + j])
11903 {
11904 case 0:
11905 fputs (_(" 0 (*local*) "), stdout);
11906 break;
11907
11908 case 1:
11909 fputs (_(" 1 (*global*) "), stdout);
11910 break;
11911
11912 default:
11913 nn = printf ("%4x%c", data[cnt + j] & VERSYM_VERSION,
11914 data[cnt + j] & VERSYM_HIDDEN ? 'h' : ' ');
11915
11916 /* If this index value is greater than the size of the symbols
11917 array, break to avoid an out-of-bounds read. */
11918 if ((unsigned long)(cnt + j) >= num_syms)
11919 {
11920 warn (_("invalid index into symbol array\n"));
11921 break;
11922 }
11923
11924 name = NULL;
11925 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
11926 {
11927 Elf_Internal_Verneed ivn;
11928 unsigned long offset;
11929
11930 offset = offset_from_vma
11931 (filedata,
11932 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
11933 sizeof (Elf_External_Verneed));
11934
11935 do
11936 {
11937 Elf_Internal_Vernaux ivna;
11938 Elf_External_Verneed evn;
11939 Elf_External_Vernaux evna;
11940 unsigned long a_off;
11941
11942 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
11943 _("version need")) == NULL)
11944 break;
11945
11946 ivn.vn_aux = BYTE_GET (evn.vn_aux);
11947 ivn.vn_next = BYTE_GET (evn.vn_next);
11948
11949 a_off = offset + ivn.vn_aux;
11950
11951 do
11952 {
11953 if (get_data (&evna, filedata, a_off, sizeof (evna),
11954 1, _("version need aux (2)")) == NULL)
11955 {
11956 ivna.vna_next = 0;
11957 ivna.vna_other = 0;
11958 }
11959 else
11960 {
11961 ivna.vna_next = BYTE_GET (evna.vna_next);
11962 ivna.vna_other = BYTE_GET (evna.vna_other);
11963 }
11964
11965 a_off += ivna.vna_next;
11966 }
11967 while (ivna.vna_other != data[cnt + j]
11968 && ivna.vna_next != 0);
11969
11970 if (ivna.vna_other == data[cnt + j])
11971 {
11972 ivna.vna_name = BYTE_GET (evna.vna_name);
11973
11974 if (ivna.vna_name >= string_sec->sh_size)
11975 name = invalid;
11976 else
11977 name = strtab + ivna.vna_name;
11978 break;
11979 }
11980
11981 offset += ivn.vn_next;
11982 }
11983 while (ivn.vn_next);
11984 }
11985
11986 if (data[cnt + j] != 0x8001
11987 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
11988 {
11989 Elf_Internal_Verdef ivd;
11990 Elf_External_Verdef evd;
11991 unsigned long offset;
11992
11993 offset = offset_from_vma
11994 (filedata,
11995 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
11996 sizeof evd);
11997
11998 do
11999 {
12000 if (get_data (&evd, filedata, offset, sizeof (evd), 1,
12001 _("version def")) == NULL)
12002 {
12003 ivd.vd_next = 0;
12004 /* PR 17531: file: 046-1082287-0.004. */
12005 ivd.vd_ndx = (data[cnt + j] & VERSYM_VERSION) + 1;
12006 break;
12007 }
12008 else
12009 {
12010 ivd.vd_next = BYTE_GET (evd.vd_next);
12011 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
12012 }
12013
12014 offset += ivd.vd_next;
12015 }
12016 while (ivd.vd_ndx != (data[cnt + j] & VERSYM_VERSION)
12017 && ivd.vd_next != 0);
12018
12019 if (ivd.vd_ndx == (data[cnt + j] & VERSYM_VERSION))
12020 {
12021 Elf_External_Verdaux evda;
12022 Elf_Internal_Verdaux ivda;
12023
12024 ivd.vd_aux = BYTE_GET (evd.vd_aux);
12025
12026 if (get_data (&evda, filedata,
12027 offset - ivd.vd_next + ivd.vd_aux,
12028 sizeof (evda), 1,
12029 _("version def aux")) == NULL)
12030 break;
12031
12032 ivda.vda_name = BYTE_GET (evda.vda_name);
12033
12034 if (ivda.vda_name >= string_sec->sh_size)
12035 name = invalid;
12036 else if (name != NULL && name != invalid)
12037 name = _("*both*");
12038 else
12039 name = strtab + ivda.vda_name;
12040 }
12041 }
12042 if (name != NULL)
12043 nn += printf ("(%s%-*s",
12044 name,
12045 12 - (int) strlen (name),
12046 ")");
12047
12048 if (nn < 18)
12049 printf ("%*c", 18 - nn, ' ');
12050 }
12051
12052 putchar ('\n');
12053 }
12054
12055 free (data);
12056 free (strtab);
12057 free (symbols);
12058 }
12059 break;
12060
12061 default:
12062 break;
12063 }
12064 }
12065
12066 if (! found)
12067 {
12068 if (filedata->is_separate)
12069 printf (_("\nNo version information found in linked file '%s'.\n"),
12070 filedata->file_name);
12071 else
12072 printf (_("\nNo version information found in this file.\n"));
12073 }
12074
12075 return true;
12076 }
12077
12078 static const char *
12079 get_symbol_binding (Filedata * filedata, unsigned int binding)
12080 {
12081 static char buff[64];
12082
12083 switch (binding)
12084 {
12085 case STB_LOCAL: return "LOCAL";
12086 case STB_GLOBAL: return "GLOBAL";
12087 case STB_WEAK: return "WEAK";
12088 default:
12089 if (binding >= STB_LOPROC && binding <= STB_HIPROC)
12090 snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
12091 binding);
12092 else if (binding >= STB_LOOS && binding <= STB_HIOS)
12093 {
12094 if (binding == STB_GNU_UNIQUE
12095 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU)
12096 return "UNIQUE";
12097 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
12098 }
12099 else
12100 snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
12101 return buff;
12102 }
12103 }
12104
12105 static const char *
12106 get_symbol_type (Filedata * filedata, unsigned int type)
12107 {
12108 static char buff[64];
12109
12110 switch (type)
12111 {
12112 case STT_NOTYPE: return "NOTYPE";
12113 case STT_OBJECT: return "OBJECT";
12114 case STT_FUNC: return "FUNC";
12115 case STT_SECTION: return "SECTION";
12116 case STT_FILE: return "FILE";
12117 case STT_COMMON: return "COMMON";
12118 case STT_TLS: return "TLS";
12119 case STT_RELC: return "RELC";
12120 case STT_SRELC: return "SRELC";
12121 default:
12122 if (type >= STT_LOPROC && type <= STT_HIPROC)
12123 {
12124 if (filedata->file_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
12125 return "THUMB_FUNC";
12126
12127 if (filedata->file_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
12128 return "REGISTER";
12129
12130 if (filedata->file_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
12131 return "PARISC_MILLI";
12132
12133 snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
12134 }
12135 else if (type >= STT_LOOS && type <= STT_HIOS)
12136 {
12137 if (filedata->file_header.e_machine == EM_PARISC)
12138 {
12139 if (type == STT_HP_OPAQUE)
12140 return "HP_OPAQUE";
12141 if (type == STT_HP_STUB)
12142 return "HP_STUB";
12143 }
12144
12145 if (type == STT_GNU_IFUNC
12146 && (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU
12147 || filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_FREEBSD))
12148 return "IFUNC";
12149
12150 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
12151 }
12152 else
12153 snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
12154 return buff;
12155 }
12156 }
12157
12158 static const char *
12159 get_symbol_visibility (unsigned int visibility)
12160 {
12161 switch (visibility)
12162 {
12163 case STV_DEFAULT: return "DEFAULT";
12164 case STV_INTERNAL: return "INTERNAL";
12165 case STV_HIDDEN: return "HIDDEN";
12166 case STV_PROTECTED: return "PROTECTED";
12167 default:
12168 error (_("Unrecognized visibility value: %u\n"), visibility);
12169 return _("<unknown>");
12170 }
12171 }
12172
12173 static const char *
12174 get_alpha_symbol_other (unsigned int other)
12175 {
12176 switch (other)
12177 {
12178 case STO_ALPHA_NOPV: return "NOPV";
12179 case STO_ALPHA_STD_GPLOAD: return "STD GPLOAD";
12180 default:
12181 error (_("Unrecognized alpha specific other value: %u\n"), other);
12182 return _("<unknown>");
12183 }
12184 }
12185
12186 static const char *
12187 get_solaris_symbol_visibility (unsigned int visibility)
12188 {
12189 switch (visibility)
12190 {
12191 case 4: return "EXPORTED";
12192 case 5: return "SINGLETON";
12193 case 6: return "ELIMINATE";
12194 default: return get_symbol_visibility (visibility);
12195 }
12196 }
12197
12198 static const char *
12199 get_aarch64_symbol_other (unsigned int other)
12200 {
12201 static char buf[32];
12202
12203 if (other & STO_AARCH64_VARIANT_PCS)
12204 {
12205 other &= ~STO_AARCH64_VARIANT_PCS;
12206 if (other == 0)
12207 return "VARIANT_PCS";
12208 snprintf (buf, sizeof buf, "VARIANT_PCS | %x", other);
12209 return buf;
12210 }
12211 return NULL;
12212 }
12213
12214 static const char *
12215 get_mips_symbol_other (unsigned int other)
12216 {
12217 switch (other)
12218 {
12219 case STO_OPTIONAL: return "OPTIONAL";
12220 case STO_MIPS_PLT: return "MIPS PLT";
12221 case STO_MIPS_PIC: return "MIPS PIC";
12222 case STO_MICROMIPS: return "MICROMIPS";
12223 case STO_MICROMIPS | STO_MIPS_PIC: return "MICROMIPS, MIPS PIC";
12224 case STO_MIPS16: return "MIPS16";
12225 default: return NULL;
12226 }
12227 }
12228
12229 static const char *
12230 get_ia64_symbol_other (Filedata * filedata, unsigned int other)
12231 {
12232 if (is_ia64_vms (filedata))
12233 {
12234 static char res[32];
12235
12236 res[0] = 0;
12237
12238 /* Function types is for images and .STB files only. */
12239 switch (filedata->file_header.e_type)
12240 {
12241 case ET_DYN:
12242 case ET_EXEC:
12243 switch (VMS_ST_FUNC_TYPE (other))
12244 {
12245 case VMS_SFT_CODE_ADDR:
12246 strcat (res, " CA");
12247 break;
12248 case VMS_SFT_SYMV_IDX:
12249 strcat (res, " VEC");
12250 break;
12251 case VMS_SFT_FD:
12252 strcat (res, " FD");
12253 break;
12254 case VMS_SFT_RESERVE:
12255 strcat (res, " RSV");
12256 break;
12257 default:
12258 warn (_("Unrecognized IA64 VMS ST Function type: %d\n"),
12259 VMS_ST_FUNC_TYPE (other));
12260 strcat (res, " <unknown>");
12261 break;
12262 }
12263 break;
12264 default:
12265 break;
12266 }
12267 switch (VMS_ST_LINKAGE (other))
12268 {
12269 case VMS_STL_IGNORE:
12270 strcat (res, " IGN");
12271 break;
12272 case VMS_STL_RESERVE:
12273 strcat (res, " RSV");
12274 break;
12275 case VMS_STL_STD:
12276 strcat (res, " STD");
12277 break;
12278 case VMS_STL_LNK:
12279 strcat (res, " LNK");
12280 break;
12281 default:
12282 warn (_("Unrecognized IA64 VMS ST Linkage: %d\n"),
12283 VMS_ST_LINKAGE (other));
12284 strcat (res, " <unknown>");
12285 break;
12286 }
12287
12288 if (res[0] != 0)
12289 return res + 1;
12290 else
12291 return res;
12292 }
12293 return NULL;
12294 }
12295
12296 static const char *
12297 get_ppc64_symbol_other (unsigned int other)
12298 {
12299 if ((other & ~STO_PPC64_LOCAL_MASK) != 0)
12300 return NULL;
12301
12302 other >>= STO_PPC64_LOCAL_BIT;
12303 if (other <= 6)
12304 {
12305 static char buf[64];
12306 if (other >= 2)
12307 other = ppc64_decode_local_entry (other);
12308 snprintf (buf, sizeof buf, _("<localentry>: %d"), other);
12309 return buf;
12310 }
12311 return NULL;
12312 }
12313
12314 static const char *
12315 get_symbol_other (Filedata * filedata, unsigned int other)
12316 {
12317 const char * result = NULL;
12318 static char buff [64];
12319
12320 if (other == 0)
12321 return "";
12322
12323 switch (filedata->file_header.e_machine)
12324 {
12325 case EM_ALPHA:
12326 result = get_alpha_symbol_other (other);
12327 break;
12328 case EM_AARCH64:
12329 result = get_aarch64_symbol_other (other);
12330 break;
12331 case EM_MIPS:
12332 result = get_mips_symbol_other (other);
12333 break;
12334 case EM_IA_64:
12335 result = get_ia64_symbol_other (filedata, other);
12336 break;
12337 case EM_PPC64:
12338 result = get_ppc64_symbol_other (other);
12339 break;
12340 default:
12341 result = NULL;
12342 break;
12343 }
12344
12345 if (result)
12346 return result;
12347
12348 snprintf (buff, sizeof buff, _("<other>: %x"), other);
12349 return buff;
12350 }
12351
12352 static const char *
12353 get_symbol_index_type (Filedata * filedata, unsigned int type)
12354 {
12355 static char buff[32];
12356
12357 switch (type)
12358 {
12359 case SHN_UNDEF: return "UND";
12360 case SHN_ABS: return "ABS";
12361 case SHN_COMMON: return "COM";
12362 default:
12363 if (type == SHN_IA_64_ANSI_COMMON
12364 && filedata->file_header.e_machine == EM_IA_64
12365 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX)
12366 return "ANSI_COM";
12367 else if ((filedata->file_header.e_machine == EM_X86_64
12368 || filedata->file_header.e_machine == EM_L1OM
12369 || filedata->file_header.e_machine == EM_K1OM)
12370 && type == SHN_X86_64_LCOMMON)
12371 return "LARGE_COM";
12372 else if ((type == SHN_MIPS_SCOMMON
12373 && filedata->file_header.e_machine == EM_MIPS)
12374 || (type == SHN_TIC6X_SCOMMON
12375 && filedata->file_header.e_machine == EM_TI_C6000))
12376 return "SCOM";
12377 else if (type == SHN_MIPS_SUNDEFINED
12378 && filedata->file_header.e_machine == EM_MIPS)
12379 return "SUND";
12380 else if (type >= SHN_LOPROC && type <= SHN_HIPROC)
12381 sprintf (buff, "PRC[0x%04x]", type & 0xffff);
12382 else if (type >= SHN_LOOS && type <= SHN_HIOS)
12383 sprintf (buff, "OS [0x%04x]", type & 0xffff);
12384 else if (type >= SHN_LORESERVE)
12385 sprintf (buff, "RSV[0x%04x]", type & 0xffff);
12386 else if (filedata->file_header.e_shnum != 0
12387 && type >= filedata->file_header.e_shnum)
12388 sprintf (buff, _("bad section index[%3d]"), type);
12389 else
12390 sprintf (buff, "%3d", type);
12391 break;
12392 }
12393
12394 return buff;
12395 }
12396
12397 static const char *
12398 get_symbol_version_string (Filedata * filedata,
12399 bool is_dynsym,
12400 const char * strtab,
12401 unsigned long int strtab_size,
12402 unsigned int si,
12403 Elf_Internal_Sym * psym,
12404 enum versioned_symbol_info * sym_info,
12405 unsigned short * vna_other)
12406 {
12407 unsigned char data[2];
12408 unsigned short vers_data;
12409 unsigned long offset;
12410 unsigned short max_vd_ndx;
12411
12412 if (!is_dynsym
12413 || filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)] == 0)
12414 return NULL;
12415
12416 offset = offset_from_vma (filedata,
12417 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
12418 sizeof data + si * sizeof (vers_data));
12419
12420 if (get_data (&data, filedata, offset + si * sizeof (vers_data),
12421 sizeof (data), 1, _("version data")) == NULL)
12422 return NULL;
12423
12424 vers_data = byte_get (data, 2);
12425
12426 if ((vers_data & VERSYM_HIDDEN) == 0 && vers_data == 0)
12427 return NULL;
12428
12429 *sym_info = (vers_data & VERSYM_HIDDEN) != 0 ? symbol_hidden : symbol_public;
12430 max_vd_ndx = 0;
12431
12432 /* Usually we'd only see verdef for defined symbols, and verneed for
12433 undefined symbols. However, symbols defined by the linker in
12434 .dynbss for variables copied from a shared library in order to
12435 avoid text relocations are defined yet have verneed. We could
12436 use a heuristic to detect the special case, for example, check
12437 for verneed first on symbols defined in SHT_NOBITS sections, but
12438 it is simpler and more reliable to just look for both verdef and
12439 verneed. .dynbss might not be mapped to a SHT_NOBITS section. */
12440
12441 if (psym->st_shndx != SHN_UNDEF
12442 && vers_data != 0x8001
12443 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
12444 {
12445 Elf_Internal_Verdef ivd;
12446 Elf_Internal_Verdaux ivda;
12447 Elf_External_Verdaux evda;
12448 unsigned long off;
12449
12450 off = offset_from_vma (filedata,
12451 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
12452 sizeof (Elf_External_Verdef));
12453
12454 do
12455 {
12456 Elf_External_Verdef evd;
12457
12458 if (get_data (&evd, filedata, off, sizeof (evd), 1,
12459 _("version def")) == NULL)
12460 {
12461 ivd.vd_ndx = 0;
12462 ivd.vd_aux = 0;
12463 ivd.vd_next = 0;
12464 ivd.vd_flags = 0;
12465 }
12466 else
12467 {
12468 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
12469 ivd.vd_aux = BYTE_GET (evd.vd_aux);
12470 ivd.vd_next = BYTE_GET (evd.vd_next);
12471 ivd.vd_flags = BYTE_GET (evd.vd_flags);
12472 }
12473
12474 if ((ivd.vd_ndx & VERSYM_VERSION) > max_vd_ndx)
12475 max_vd_ndx = ivd.vd_ndx & VERSYM_VERSION;
12476
12477 off += ivd.vd_next;
12478 }
12479 while (ivd.vd_ndx != (vers_data & VERSYM_VERSION) && ivd.vd_next != 0);
12480
12481 if (ivd.vd_ndx == (vers_data & VERSYM_VERSION))
12482 {
12483 if (ivd.vd_ndx == 1 && ivd.vd_flags == VER_FLG_BASE)
12484 return NULL;
12485
12486 off -= ivd.vd_next;
12487 off += ivd.vd_aux;
12488
12489 if (get_data (&evda, filedata, off, sizeof (evda), 1,
12490 _("version def aux")) != NULL)
12491 {
12492 ivda.vda_name = BYTE_GET (evda.vda_name);
12493
12494 if (psym->st_name != ivda.vda_name)
12495 return (ivda.vda_name < strtab_size
12496 ? strtab + ivda.vda_name : _("<corrupt>"));
12497 }
12498 }
12499 }
12500
12501 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
12502 {
12503 Elf_External_Verneed evn;
12504 Elf_Internal_Verneed ivn;
12505 Elf_Internal_Vernaux ivna;
12506
12507 offset = offset_from_vma (filedata,
12508 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
12509 sizeof evn);
12510 do
12511 {
12512 unsigned long vna_off;
12513
12514 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
12515 _("version need")) == NULL)
12516 {
12517 ivna.vna_next = 0;
12518 ivna.vna_other = 0;
12519 ivna.vna_name = 0;
12520 break;
12521 }
12522
12523 ivn.vn_aux = BYTE_GET (evn.vn_aux);
12524 ivn.vn_next = BYTE_GET (evn.vn_next);
12525
12526 vna_off = offset + ivn.vn_aux;
12527
12528 do
12529 {
12530 Elf_External_Vernaux evna;
12531
12532 if (get_data (&evna, filedata, vna_off, sizeof (evna), 1,
12533 _("version need aux (3)")) == NULL)
12534 {
12535 ivna.vna_next = 0;
12536 ivna.vna_other = 0;
12537 ivna.vna_name = 0;
12538 }
12539 else
12540 {
12541 ivna.vna_other = BYTE_GET (evna.vna_other);
12542 ivna.vna_next = BYTE_GET (evna.vna_next);
12543 ivna.vna_name = BYTE_GET (evna.vna_name);
12544 }
12545
12546 vna_off += ivna.vna_next;
12547 }
12548 while (ivna.vna_other != vers_data && ivna.vna_next != 0);
12549
12550 if (ivna.vna_other == vers_data)
12551 break;
12552
12553 offset += ivn.vn_next;
12554 }
12555 while (ivn.vn_next != 0);
12556
12557 if (ivna.vna_other == vers_data)
12558 {
12559 *sym_info = symbol_undefined;
12560 *vna_other = ivna.vna_other;
12561 return (ivna.vna_name < strtab_size
12562 ? strtab + ivna.vna_name : _("<corrupt>"));
12563 }
12564 else if ((max_vd_ndx || (vers_data & VERSYM_VERSION) != 1)
12565 && (vers_data & VERSYM_VERSION) > max_vd_ndx)
12566 return _("<corrupt>");
12567 }
12568 return NULL;
12569 }
12570
12571 /* Display a symbol size on stdout. Format is based on --sym-base setting. */
12572
12573 static unsigned int
12574 print_dynamic_symbol_size (bfd_vma vma, int base)
12575 {
12576 switch (base)
12577 {
12578 case 8:
12579 return print_vma (vma, OCTAL_5);
12580
12581 case 10:
12582 return print_vma (vma, UNSIGNED_5);
12583
12584 case 16:
12585 return print_vma (vma, PREFIX_HEX_5);
12586
12587 case 0:
12588 default:
12589 return print_vma (vma, DEC_5);
12590 }
12591 }
12592
12593 static void
12594 print_dynamic_symbol (Filedata *filedata, unsigned long si,
12595 Elf_Internal_Sym *symtab,
12596 Elf_Internal_Shdr *section,
12597 char *strtab, size_t strtab_size)
12598 {
12599 const char *version_string;
12600 enum versioned_symbol_info sym_info;
12601 unsigned short vna_other;
12602 bool is_valid;
12603 const char * sstr;
12604 Elf_Internal_Sym *psym = symtab + si;
12605
12606 printf ("%6ld: ", si);
12607 print_vma (psym->st_value, LONG_HEX);
12608 putchar (' ');
12609 print_dynamic_symbol_size (psym->st_size, sym_base);
12610 printf (" %-7s", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
12611 printf (" %-6s", get_symbol_binding (filedata, ELF_ST_BIND (psym->st_info)));
12612 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
12613 printf (" %-7s", get_solaris_symbol_visibility (psym->st_other));
12614 else
12615 {
12616 unsigned int vis = ELF_ST_VISIBILITY (psym->st_other);
12617
12618 printf (" %-7s", get_symbol_visibility (vis));
12619 /* Check to see if any other bits in the st_other field are set.
12620 Note - displaying this information disrupts the layout of the
12621 table being generated, but for the moment this case is very rare. */
12622 if (psym->st_other ^ vis)
12623 printf (" [%s] ", get_symbol_other (filedata, psym->st_other ^ vis));
12624 }
12625 printf (" %4s ", get_symbol_index_type (filedata, psym->st_shndx));
12626
12627 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION
12628 && psym->st_shndx < filedata->file_header.e_shnum
12629 && psym->st_name == 0)
12630 {
12631 is_valid = SECTION_NAME_VALID (filedata->section_headers + psym->st_shndx);
12632 sstr = is_valid ?
12633 SECTION_NAME_PRINT (filedata->section_headers + psym->st_shndx)
12634 : _("<corrupt>");
12635 }
12636 else
12637 {
12638 is_valid = VALID_SYMBOL_NAME (strtab, strtab_size, psym->st_name);
12639 sstr = is_valid ? strtab + psym->st_name : _("<corrupt>");
12640 }
12641
12642 version_string
12643 = get_symbol_version_string (filedata,
12644 (section == NULL
12645 || section->sh_type == SHT_DYNSYM),
12646 strtab, strtab_size, si,
12647 psym, &sym_info, &vna_other);
12648
12649 int len_avail = 21;
12650 if (! do_wide && version_string != NULL)
12651 {
12652 char buffer[16];
12653
12654 len_avail -= 1 + strlen (version_string);
12655
12656 if (sym_info == symbol_undefined)
12657 len_avail -= sprintf (buffer," (%d)", vna_other);
12658 else if (sym_info != symbol_hidden)
12659 len_avail -= 1;
12660 }
12661
12662 print_symbol (len_avail, sstr);
12663
12664 if (version_string)
12665 {
12666 if (sym_info == symbol_undefined)
12667 printf ("@%s (%d)", version_string, vna_other);
12668 else
12669 printf (sym_info == symbol_hidden ? "@%s" : "@@%s",
12670 version_string);
12671 }
12672
12673 putchar ('\n');
12674
12675 if (ELF_ST_BIND (psym->st_info) == STB_LOCAL
12676 && section != NULL
12677 && si >= section->sh_info
12678 /* Irix 5 and 6 MIPS binaries are known to ignore this requirement. */
12679 && filedata->file_header.e_machine != EM_MIPS
12680 /* Solaris binaries have been found to violate this requirement as
12681 well. Not sure if this is a bug or an ABI requirement. */
12682 && filedata->file_header.e_ident[EI_OSABI] != ELFOSABI_SOLARIS)
12683 warn (_("local symbol %lu found at index >= %s's sh_info value of %u\n"),
12684 si, printable_section_name (filedata, section), section->sh_info);
12685 }
12686
12687 static const char *
12688 get_lto_kind (unsigned int kind)
12689 {
12690 switch (kind)
12691 {
12692 case 0: return "DEF";
12693 case 1: return "WEAKDEF";
12694 case 2: return "UNDEF";
12695 case 3: return "WEAKUNDEF";
12696 case 4: return "COMMON";
12697 default:
12698 break;
12699 }
12700
12701 static char buffer[30];
12702 error (_("Unknown LTO symbol definition encountered: %u\n"), kind);
12703 sprintf (buffer, "<unknown: %u>", kind);
12704 return buffer;
12705 }
12706
12707 static const char *
12708 get_lto_visibility (unsigned int visibility)
12709 {
12710 switch (visibility)
12711 {
12712 case 0: return "DEFAULT";
12713 case 1: return "PROTECTED";
12714 case 2: return "INTERNAL";
12715 case 3: return "HIDDEN";
12716 default:
12717 break;
12718 }
12719
12720 static char buffer[30];
12721 error (_("Unknown LTO symbol visibility encountered: %u\n"), visibility);
12722 sprintf (buffer, "<unknown: %u>", visibility);
12723 return buffer;
12724 }
12725
12726 static const char *
12727 get_lto_sym_type (unsigned int sym_type)
12728 {
12729 switch (sym_type)
12730 {
12731 case 0: return "UNKNOWN";
12732 case 1: return "FUNCTION";
12733 case 2: return "VARIABLE";
12734 default:
12735 break;
12736 }
12737
12738 static char buffer[30];
12739 error (_("Unknown LTO symbol type encountered: %u\n"), sym_type);
12740 sprintf (buffer, "<unknown: %u>", sym_type);
12741 return buffer;
12742 }
12743
12744 /* Display an LTO format symbol table.
12745 FIXME: The format of LTO symbol tables is not formalized.
12746 So this code could need changing in the future. */
12747
12748 static bool
12749 display_lto_symtab (Filedata * filedata,
12750 Elf_Internal_Shdr * section)
12751 {
12752 if (section->sh_size == 0)
12753 {
12754 if (filedata->is_separate)
12755 printf (_("\nThe LTO Symbol table section '%s' in linked file '%s' is empty!\n"),
12756 printable_section_name (filedata, section),
12757 filedata->file_name);
12758 else
12759 printf (_("\nLTO Symbol table '%s' is empty!\n"),
12760 printable_section_name (filedata, section));
12761
12762 return true;
12763 }
12764
12765 if (section->sh_size > filedata->file_size)
12766 {
12767 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
12768 printable_section_name (filedata, section),
12769 (unsigned long) section->sh_size);
12770 return false;
12771 }
12772
12773 void * alloced_data = get_data (NULL, filedata, section->sh_offset,
12774 section->sh_size, 1, _("LTO symbols"));
12775 if (alloced_data == NULL)
12776 return false;
12777
12778 /* Look for extended data for the symbol table. */
12779 Elf_Internal_Shdr * ext;
12780 void * ext_data_orig = NULL;
12781 char * ext_data = NULL;
12782 char * ext_data_end = NULL;
12783 char * ext_name = NULL;
12784
12785 if (asprintf (& ext_name, ".gnu.lto_.ext_symtab.%s",
12786 SECTION_NAME (section) + sizeof (".gnu.lto_.symtab.") - 1) > 0
12787 && ext_name != NULL /* Paranoia. */
12788 && (ext = find_section (filedata, ext_name)) != NULL)
12789 {
12790 if (ext->sh_size < 3)
12791 error (_("LTO Symbol extension table '%s' is empty!\n"),
12792 printable_section_name (filedata, ext));
12793 else
12794 {
12795 ext_data_orig = ext_data = get_data (NULL, filedata, ext->sh_offset,
12796 ext->sh_size, 1,
12797 _("LTO ext symbol data"));
12798 if (ext_data != NULL)
12799 {
12800 ext_data_end = ext_data + ext->sh_size;
12801 if (* ext_data++ != 1)
12802 error (_("Unexpected version number in symbol extension table\n"));
12803 }
12804 }
12805 }
12806
12807 const unsigned char * data = (const unsigned char *) alloced_data;
12808 const unsigned char * end = data + section->sh_size;
12809
12810 if (filedata->is_separate)
12811 printf (_("\nIn linked file '%s': "), filedata->file_name);
12812 else
12813 printf ("\n");
12814
12815 if (ext_data_orig != NULL)
12816 {
12817 if (do_wide)
12818 printf (_("LTO Symbol table '%s' and extension table '%s' contain:\n"),
12819 printable_section_name (filedata, section),
12820 printable_section_name (filedata, ext));
12821 else
12822 {
12823 printf (_("LTO Symbol table '%s'\n"),
12824 printable_section_name (filedata, section));
12825 printf (_(" and extension table '%s' contain:\n"),
12826 printable_section_name (filedata, ext));
12827 }
12828 }
12829 else
12830 printf (_("LTO Symbol table '%s' contains:\n"),
12831 printable_section_name (filedata, section));
12832
12833 /* FIXME: Add a wide version. */
12834 if (ext_data_orig != NULL)
12835 printf (_(" Comdat_Key Kind Visibility Size Slot Type Section Name\n"));
12836 else
12837 printf (_(" Comdat_Key Kind Visibility Size Slot Name\n"));
12838
12839 /* FIXME: We do not handle style prefixes. */
12840
12841 while (data < end)
12842 {
12843 const unsigned char * sym_name = data;
12844 data += strnlen ((const char *) sym_name, end - data) + 1;
12845 if (data >= end)
12846 goto fail;
12847
12848 const unsigned char * comdat_key = data;
12849 data += strnlen ((const char *) comdat_key, end - data) + 1;
12850 if (data >= end)
12851 goto fail;
12852
12853 if (data + 2 + 8 + 4 > end)
12854 goto fail;
12855
12856 unsigned int kind = *data++;
12857 unsigned int visibility = *data++;
12858
12859 elf_vma size = byte_get (data, 8);
12860 data += 8;
12861
12862 elf_vma slot = byte_get (data, 4);
12863 data += 4;
12864
12865 if (ext_data != NULL)
12866 {
12867 if (ext_data < (ext_data_end - 1))
12868 {
12869 unsigned int sym_type = * ext_data ++;
12870 unsigned int sec_kind = * ext_data ++;
12871
12872 printf (" %10s %10s %11s %08lx %08lx %9s %08lx _",
12873 * comdat_key == 0 ? "-" : (char *) comdat_key,
12874 get_lto_kind (kind),
12875 get_lto_visibility (visibility),
12876 (long) size,
12877 (long) slot,
12878 get_lto_sym_type (sym_type),
12879 (long) sec_kind);
12880 print_symbol (6, (const char *) sym_name);
12881 }
12882 else
12883 {
12884 error (_("Ran out of LTO symbol extension data\n"));
12885 ext_data = NULL;
12886 /* FIXME: return FAIL result ? */
12887 }
12888 }
12889 else
12890 {
12891 printf (" %10s %10s %11s %08lx %08lx _",
12892 * comdat_key == 0 ? "-" : (char *) comdat_key,
12893 get_lto_kind (kind),
12894 get_lto_visibility (visibility),
12895 (long) size,
12896 (long) slot);
12897 print_symbol (21, (const char *) sym_name);
12898 }
12899 putchar ('\n');
12900 }
12901
12902 if (ext_data != NULL && ext_data < ext_data_end)
12903 {
12904 error (_("Data remains in the LTO symbol extension table\n"));
12905 goto fail;
12906 }
12907
12908 free (alloced_data);
12909 free (ext_data_orig);
12910 free (ext_name);
12911 return true;
12912
12913 fail:
12914 error (_("Buffer overrun encountered whilst decoding LTO symbol table\n"));
12915 free (alloced_data);
12916 free (ext_data_orig);
12917 free (ext_name);
12918 return false;
12919 }
12920
12921 /* Display LTO symbol tables. */
12922
12923 static bool
12924 process_lto_symbol_tables (Filedata * filedata)
12925 {
12926 Elf_Internal_Shdr * section;
12927 unsigned int i;
12928 bool res = true;
12929
12930 if (!do_lto_syms)
12931 return true;
12932
12933 if (filedata->section_headers == NULL)
12934 return true;
12935
12936 for (i = 0, section = filedata->section_headers;
12937 i < filedata->file_header.e_shnum;
12938 i++, section++)
12939 if (SECTION_NAME_VALID (section)
12940 && startswith (SECTION_NAME (section), ".gnu.lto_.symtab."))
12941 res &= display_lto_symtab (filedata, section);
12942
12943 return res;
12944 }
12945
12946 /* Dump the symbol table. */
12947
12948 static bool
12949 process_symbol_table (Filedata * filedata)
12950 {
12951 Elf_Internal_Shdr * section;
12952
12953 if (!do_syms && !do_dyn_syms && !do_histogram)
12954 return true;
12955
12956 if ((filedata->dynamic_info[DT_HASH] || filedata->dynamic_info_DT_GNU_HASH)
12957 && do_syms
12958 && do_using_dynamic
12959 && filedata->dynamic_strings != NULL
12960 && filedata->dynamic_symbols != NULL)
12961 {
12962 unsigned long si;
12963
12964 if (filedata->is_separate)
12965 {
12966 printf (ngettext ("\nIn linked file '%s' the dynamic symbol table contains %lu entry:\n",
12967 "\nIn linked file '%s' the dynamic symbol table contains %lu entries:\n",
12968 filedata->num_dynamic_syms),
12969 filedata->file_name,
12970 filedata->num_dynamic_syms);
12971 }
12972 else
12973 {
12974 printf (ngettext ("\nSymbol table for image contains %lu entry:\n",
12975 "\nSymbol table for image contains %lu entries:\n",
12976 filedata->num_dynamic_syms),
12977 filedata->num_dynamic_syms);
12978 }
12979 if (is_32bit_elf)
12980 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
12981 else
12982 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
12983
12984 for (si = 0; si < filedata->num_dynamic_syms; si++)
12985 print_dynamic_symbol (filedata, si, filedata->dynamic_symbols, NULL,
12986 filedata->dynamic_strings,
12987 filedata->dynamic_strings_length);
12988 }
12989 else if ((do_dyn_syms || (do_syms && !do_using_dynamic))
12990 && filedata->section_headers != NULL)
12991 {
12992 unsigned int i;
12993
12994 for (i = 0, section = filedata->section_headers;
12995 i < filedata->file_header.e_shnum;
12996 i++, section++)
12997 {
12998 char * strtab = NULL;
12999 unsigned long int strtab_size = 0;
13000 Elf_Internal_Sym * symtab;
13001 unsigned long si, num_syms;
13002
13003 if ((section->sh_type != SHT_SYMTAB
13004 && section->sh_type != SHT_DYNSYM)
13005 || (!do_syms
13006 && section->sh_type == SHT_SYMTAB))
13007 continue;
13008
13009 if (section->sh_entsize == 0)
13010 {
13011 printf (_("\nSymbol table '%s' has a sh_entsize of zero!\n"),
13012 printable_section_name (filedata, section));
13013 continue;
13014 }
13015
13016 num_syms = section->sh_size / section->sh_entsize;
13017
13018 if (filedata->is_separate)
13019 printf (ngettext ("\nIn linked file '%s' symbol section '%s' contains %lu entry:\n",
13020 "\nIn linked file '%s' symbol section '%s' contains %lu entries:\n",
13021 num_syms),
13022 filedata->file_name,
13023 printable_section_name (filedata, section),
13024 num_syms);
13025 else
13026 printf (ngettext ("\nSymbol table '%s' contains %lu entry:\n",
13027 "\nSymbol table '%s' contains %lu entries:\n",
13028 num_syms),
13029 printable_section_name (filedata, section),
13030 num_syms);
13031
13032 if (is_32bit_elf)
13033 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13034 else
13035 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13036
13037 symtab = get_elf_symbols (filedata, section, & num_syms);
13038 if (symtab == NULL)
13039 continue;
13040
13041 if (section->sh_link == filedata->file_header.e_shstrndx)
13042 {
13043 strtab = filedata->string_table;
13044 strtab_size = filedata->string_table_length;
13045 }
13046 else if (section->sh_link < filedata->file_header.e_shnum)
13047 {
13048 Elf_Internal_Shdr * string_sec;
13049
13050 string_sec = filedata->section_headers + section->sh_link;
13051
13052 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset,
13053 1, string_sec->sh_size,
13054 _("string table"));
13055 strtab_size = strtab != NULL ? string_sec->sh_size : 0;
13056 }
13057
13058 for (si = 0; si < num_syms; si++)
13059 print_dynamic_symbol (filedata, si, symtab, section,
13060 strtab, strtab_size);
13061
13062 free (symtab);
13063 if (strtab != filedata->string_table)
13064 free (strtab);
13065 }
13066 }
13067 else if (do_syms)
13068 printf
13069 (_("\nDynamic symbol information is not available for displaying symbols.\n"));
13070
13071 if (do_histogram && filedata->buckets != NULL)
13072 {
13073 unsigned long * lengths;
13074 unsigned long * counts;
13075 unsigned long hn;
13076 bfd_vma si;
13077 unsigned long maxlength = 0;
13078 unsigned long nzero_counts = 0;
13079 unsigned long nsyms = 0;
13080 char *visited;
13081
13082 printf (ngettext ("\nHistogram for bucket list length "
13083 "(total of %lu bucket):\n",
13084 "\nHistogram for bucket list length "
13085 "(total of %lu buckets):\n",
13086 (unsigned long) filedata->nbuckets),
13087 (unsigned long) filedata->nbuckets);
13088
13089 lengths = (unsigned long *) calloc (filedata->nbuckets,
13090 sizeof (*lengths));
13091 if (lengths == NULL)
13092 {
13093 error (_("Out of memory allocating space for histogram buckets\n"));
13094 goto err_out;
13095 }
13096 visited = xcmalloc (filedata->nchains, 1);
13097 memset (visited, 0, filedata->nchains);
13098
13099 printf (_(" Length Number %% of total Coverage\n"));
13100 for (hn = 0; hn < filedata->nbuckets; ++hn)
13101 {
13102 for (si = filedata->buckets[hn]; si > 0; si = filedata->chains[si])
13103 {
13104 ++nsyms;
13105 if (maxlength < ++lengths[hn])
13106 ++maxlength;
13107 if (si >= filedata->nchains || visited[si])
13108 {
13109 error (_("histogram chain is corrupt\n"));
13110 break;
13111 }
13112 visited[si] = 1;
13113 }
13114 }
13115 free (visited);
13116
13117 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
13118 if (counts == NULL)
13119 {
13120 free (lengths);
13121 error (_("Out of memory allocating space for histogram counts\n"));
13122 goto err_out;
13123 }
13124
13125 for (hn = 0; hn < filedata->nbuckets; ++hn)
13126 ++counts[lengths[hn]];
13127
13128 if (filedata->nbuckets > 0)
13129 {
13130 unsigned long i;
13131 printf (" 0 %-10lu (%5.1f%%)\n",
13132 counts[0], (counts[0] * 100.0) / filedata->nbuckets);
13133 for (i = 1; i <= maxlength; ++i)
13134 {
13135 nzero_counts += counts[i] * i;
13136 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
13137 i, counts[i], (counts[i] * 100.0) / filedata->nbuckets,
13138 (nzero_counts * 100.0) / nsyms);
13139 }
13140 }
13141
13142 free (counts);
13143 free (lengths);
13144 }
13145
13146 free (filedata->buckets);
13147 filedata->buckets = NULL;
13148 filedata->nbuckets = 0;
13149 free (filedata->chains);
13150 filedata->chains = NULL;
13151
13152 if (do_histogram && filedata->gnubuckets != NULL)
13153 {
13154 unsigned long * lengths;
13155 unsigned long * counts;
13156 unsigned long hn;
13157 unsigned long maxlength = 0;
13158 unsigned long nzero_counts = 0;
13159 unsigned long nsyms = 0;
13160
13161 printf (ngettext ("\nHistogram for `%s' bucket list length "
13162 "(total of %lu bucket):\n",
13163 "\nHistogram for `%s' bucket list length "
13164 "(total of %lu buckets):\n",
13165 (unsigned long) filedata->ngnubuckets),
13166 GNU_HASH_SECTION_NAME (filedata),
13167 (unsigned long) filedata->ngnubuckets);
13168
13169 lengths = (unsigned long *) calloc (filedata->ngnubuckets,
13170 sizeof (*lengths));
13171 if (lengths == NULL)
13172 {
13173 error (_("Out of memory allocating space for gnu histogram buckets\n"));
13174 goto err_out;
13175 }
13176
13177 printf (_(" Length Number %% of total Coverage\n"));
13178
13179 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
13180 if (filedata->gnubuckets[hn] != 0)
13181 {
13182 bfd_vma off, length = 1;
13183
13184 for (off = filedata->gnubuckets[hn] - filedata->gnusymidx;
13185 /* PR 17531 file: 010-77222-0.004. */
13186 off < filedata->ngnuchains
13187 && (filedata->gnuchains[off] & 1) == 0;
13188 ++off)
13189 ++length;
13190 lengths[hn] = length;
13191 if (length > maxlength)
13192 maxlength = length;
13193 nsyms += length;
13194 }
13195
13196 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
13197 if (counts == NULL)
13198 {
13199 free (lengths);
13200 error (_("Out of memory allocating space for gnu histogram counts\n"));
13201 goto err_out;
13202 }
13203
13204 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
13205 ++counts[lengths[hn]];
13206
13207 if (filedata->ngnubuckets > 0)
13208 {
13209 unsigned long j;
13210 printf (" 0 %-10lu (%5.1f%%)\n",
13211 counts[0], (counts[0] * 100.0) / filedata->ngnubuckets);
13212 for (j = 1; j <= maxlength; ++j)
13213 {
13214 nzero_counts += counts[j] * j;
13215 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
13216 j, counts[j], (counts[j] * 100.0) / filedata->ngnubuckets,
13217 (nzero_counts * 100.0) / nsyms);
13218 }
13219 }
13220
13221 free (counts);
13222 free (lengths);
13223 }
13224 free (filedata->gnubuckets);
13225 filedata->gnubuckets = NULL;
13226 filedata->ngnubuckets = 0;
13227 free (filedata->gnuchains);
13228 filedata->gnuchains = NULL;
13229 filedata->ngnuchains = 0;
13230 free (filedata->mipsxlat);
13231 filedata->mipsxlat = NULL;
13232 return true;
13233
13234 err_out:
13235 free (filedata->gnubuckets);
13236 filedata->gnubuckets = NULL;
13237 filedata->ngnubuckets = 0;
13238 free (filedata->gnuchains);
13239 filedata->gnuchains = NULL;
13240 filedata->ngnuchains = 0;
13241 free (filedata->mipsxlat);
13242 filedata->mipsxlat = NULL;
13243 free (filedata->buckets);
13244 filedata->buckets = NULL;
13245 filedata->nbuckets = 0;
13246 free (filedata->chains);
13247 filedata->chains = NULL;
13248 return false;
13249 }
13250
13251 static bool
13252 process_syminfo (Filedata * filedata)
13253 {
13254 unsigned int i;
13255
13256 if (filedata->dynamic_syminfo == NULL
13257 || !do_dynamic)
13258 /* No syminfo, this is ok. */
13259 return true;
13260
13261 /* There better should be a dynamic symbol section. */
13262 if (filedata->dynamic_symbols == NULL || filedata->dynamic_strings == NULL)
13263 return false;
13264
13265 if (filedata->is_separate)
13266 printf (ngettext ("\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entry:\n",
13267 "\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entries:\n",
13268 filedata->dynamic_syminfo_nent),
13269 filedata->file_name,
13270 filedata->dynamic_syminfo_offset,
13271 filedata->dynamic_syminfo_nent);
13272 else
13273 printf (ngettext ("\nDynamic info segment at offset 0x%lx "
13274 "contains %d entry:\n",
13275 "\nDynamic info segment at offset 0x%lx "
13276 "contains %d entries:\n",
13277 filedata->dynamic_syminfo_nent),
13278 filedata->dynamic_syminfo_offset,
13279 filedata->dynamic_syminfo_nent);
13280
13281 printf (_(" Num: Name BoundTo Flags\n"));
13282 for (i = 0; i < filedata->dynamic_syminfo_nent; ++i)
13283 {
13284 unsigned short int flags = filedata->dynamic_syminfo[i].si_flags;
13285
13286 printf ("%4d: ", i);
13287 if (i >= filedata->num_dynamic_syms)
13288 printf (_("<corrupt index>"));
13289 else if (VALID_DYNAMIC_NAME (filedata, filedata->dynamic_symbols[i].st_name))
13290 print_symbol (30, GET_DYNAMIC_NAME (filedata,
13291 filedata->dynamic_symbols[i].st_name));
13292 else
13293 printf (_("<corrupt: %19ld>"), filedata->dynamic_symbols[i].st_name);
13294 putchar (' ');
13295
13296 switch (filedata->dynamic_syminfo[i].si_boundto)
13297 {
13298 case SYMINFO_BT_SELF:
13299 fputs ("SELF ", stdout);
13300 break;
13301 case SYMINFO_BT_PARENT:
13302 fputs ("PARENT ", stdout);
13303 break;
13304 default:
13305 if (filedata->dynamic_syminfo[i].si_boundto > 0
13306 && filedata->dynamic_syminfo[i].si_boundto < filedata->dynamic_nent
13307 && VALID_DYNAMIC_NAME (filedata,
13308 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val))
13309 {
13310 print_symbol (10, GET_DYNAMIC_NAME (filedata,
13311 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val));
13312 putchar (' ' );
13313 }
13314 else
13315 printf ("%-10d ", filedata->dynamic_syminfo[i].si_boundto);
13316 break;
13317 }
13318
13319 if (flags & SYMINFO_FLG_DIRECT)
13320 printf (" DIRECT");
13321 if (flags & SYMINFO_FLG_PASSTHRU)
13322 printf (" PASSTHRU");
13323 if (flags & SYMINFO_FLG_COPY)
13324 printf (" COPY");
13325 if (flags & SYMINFO_FLG_LAZYLOAD)
13326 printf (" LAZYLOAD");
13327
13328 puts ("");
13329 }
13330
13331 return true;
13332 }
13333
13334 /* A macro which evaluates to TRUE if the region ADDR .. ADDR + NELEM
13335 is contained by the region START .. END. The types of ADDR, START
13336 and END should all be the same. Note both ADDR + NELEM and END
13337 point to just beyond the end of the regions that are being tested. */
13338 #define IN_RANGE(START,END,ADDR,NELEM) \
13339 (((ADDR) >= (START)) && ((ADDR) < (END)) && ((ADDR) + (NELEM) <= (END)))
13340
13341 /* Check to see if the given reloc needs to be handled in a target specific
13342 manner. If so then process the reloc and return TRUE otherwise return
13343 FALSE.
13344
13345 If called with reloc == NULL, then this is a signal that reloc processing
13346 for the current section has finished, and any saved state should be
13347 discarded. */
13348
13349 static bool
13350 target_specific_reloc_handling (Filedata * filedata,
13351 Elf_Internal_Rela * reloc,
13352 unsigned char * start,
13353 unsigned char * end,
13354 Elf_Internal_Sym * symtab,
13355 unsigned long num_syms)
13356 {
13357 unsigned int reloc_type = 0;
13358 unsigned long sym_index = 0;
13359
13360 if (reloc)
13361 {
13362 reloc_type = get_reloc_type (filedata, reloc->r_info);
13363 sym_index = get_reloc_symindex (reloc->r_info);
13364 }
13365
13366 switch (filedata->file_header.e_machine)
13367 {
13368 case EM_MSP430:
13369 case EM_MSP430_OLD:
13370 {
13371 static Elf_Internal_Sym * saved_sym = NULL;
13372
13373 if (reloc == NULL)
13374 {
13375 saved_sym = NULL;
13376 return true;
13377 }
13378
13379 switch (reloc_type)
13380 {
13381 case 10: /* R_MSP430_SYM_DIFF */
13382 case 12: /* R_MSP430_GNU_SUB_ULEB128 */
13383 if (uses_msp430x_relocs (filedata))
13384 break;
13385 /* Fall through. */
13386 case 21: /* R_MSP430X_SYM_DIFF */
13387 case 23: /* R_MSP430X_GNU_SUB_ULEB128 */
13388 /* PR 21139. */
13389 if (sym_index >= num_syms)
13390 error (_("MSP430 SYM_DIFF reloc contains invalid symbol index %lu\n"),
13391 sym_index);
13392 else
13393 saved_sym = symtab + sym_index;
13394 return true;
13395
13396 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
13397 case 3: /* R_MSP430_16 or R_MSP430_ABS8 */
13398 goto handle_sym_diff;
13399
13400 case 5: /* R_MSP430_16_BYTE */
13401 case 9: /* R_MSP430_8 */
13402 case 11: /* R_MSP430_GNU_SET_ULEB128 */
13403 if (uses_msp430x_relocs (filedata))
13404 break;
13405 goto handle_sym_diff;
13406
13407 case 2: /* R_MSP430_ABS16 */
13408 case 15: /* R_MSP430X_ABS16 */
13409 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
13410 if (! uses_msp430x_relocs (filedata))
13411 break;
13412 goto handle_sym_diff;
13413
13414 handle_sym_diff:
13415 if (saved_sym != NULL)
13416 {
13417 bfd_vma value;
13418 unsigned int reloc_size = 0;
13419 int leb_ret = 0;
13420 switch (reloc_type)
13421 {
13422 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
13423 reloc_size = 4;
13424 break;
13425 case 11: /* R_MSP430_GNU_SET_ULEB128 */
13426 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
13427 if (reloc->r_offset < (size_t) (end - start))
13428 read_leb128 (start + reloc->r_offset, end, false,
13429 &reloc_size, &leb_ret);
13430 break;
13431 default:
13432 reloc_size = 2;
13433 break;
13434 }
13435
13436 if (leb_ret != 0 || reloc_size == 0 || reloc_size > 8)
13437 error (_("MSP430 ULEB128 field at 0x%lx contains invalid "
13438 "ULEB128 value\n"),
13439 (long) reloc->r_offset);
13440 else if (sym_index >= num_syms)
13441 error (_("MSP430 reloc contains invalid symbol index %lu\n"),
13442 sym_index);
13443 else
13444 {
13445 value = reloc->r_addend + (symtab[sym_index].st_value
13446 - saved_sym->st_value);
13447
13448 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
13449 byte_put (start + reloc->r_offset, value, reloc_size);
13450 else
13451 /* PR 21137 */
13452 error (_("MSP430 sym diff reloc contains invalid offset: 0x%lx\n"),
13453 (long) reloc->r_offset);
13454 }
13455
13456 saved_sym = NULL;
13457 return true;
13458 }
13459 break;
13460
13461 default:
13462 if (saved_sym != NULL)
13463 error (_("Unhandled MSP430 reloc type found after SYM_DIFF reloc\n"));
13464 break;
13465 }
13466 break;
13467 }
13468
13469 case EM_MN10300:
13470 case EM_CYGNUS_MN10300:
13471 {
13472 static Elf_Internal_Sym * saved_sym = NULL;
13473
13474 if (reloc == NULL)
13475 {
13476 saved_sym = NULL;
13477 return true;
13478 }
13479
13480 switch (reloc_type)
13481 {
13482 case 34: /* R_MN10300_ALIGN */
13483 return true;
13484 case 33: /* R_MN10300_SYM_DIFF */
13485 if (sym_index >= num_syms)
13486 error (_("MN10300_SYM_DIFF reloc contains invalid symbol index %lu\n"),
13487 sym_index);
13488 else
13489 saved_sym = symtab + sym_index;
13490 return true;
13491
13492 case 1: /* R_MN10300_32 */
13493 case 2: /* R_MN10300_16 */
13494 if (saved_sym != NULL)
13495 {
13496 int reloc_size = reloc_type == 1 ? 4 : 2;
13497 bfd_vma value;
13498
13499 if (sym_index >= num_syms)
13500 error (_("MN10300 reloc contains invalid symbol index %lu\n"),
13501 sym_index);
13502 else
13503 {
13504 value = reloc->r_addend + (symtab[sym_index].st_value
13505 - saved_sym->st_value);
13506
13507 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
13508 byte_put (start + reloc->r_offset, value, reloc_size);
13509 else
13510 error (_("MN10300 sym diff reloc contains invalid offset: 0x%lx\n"),
13511 (long) reloc->r_offset);
13512 }
13513
13514 saved_sym = NULL;
13515 return true;
13516 }
13517 break;
13518 default:
13519 if (saved_sym != NULL)
13520 error (_("Unhandled MN10300 reloc type found after SYM_DIFF reloc\n"));
13521 break;
13522 }
13523 break;
13524 }
13525
13526 case EM_RL78:
13527 {
13528 static bfd_vma saved_sym1 = 0;
13529 static bfd_vma saved_sym2 = 0;
13530 static bfd_vma value;
13531
13532 if (reloc == NULL)
13533 {
13534 saved_sym1 = saved_sym2 = 0;
13535 return true;
13536 }
13537
13538 switch (reloc_type)
13539 {
13540 case 0x80: /* R_RL78_SYM. */
13541 saved_sym1 = saved_sym2;
13542 if (sym_index >= num_syms)
13543 error (_("RL78_SYM reloc contains invalid symbol index %lu\n"),
13544 sym_index);
13545 else
13546 {
13547 saved_sym2 = symtab[sym_index].st_value;
13548 saved_sym2 += reloc->r_addend;
13549 }
13550 return true;
13551
13552 case 0x83: /* R_RL78_OPsub. */
13553 value = saved_sym1 - saved_sym2;
13554 saved_sym2 = saved_sym1 = 0;
13555 return true;
13556 break;
13557
13558 case 0x41: /* R_RL78_ABS32. */
13559 if (IN_RANGE (start, end, start + reloc->r_offset, 4))
13560 byte_put (start + reloc->r_offset, value, 4);
13561 else
13562 error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
13563 (long) reloc->r_offset);
13564 value = 0;
13565 return true;
13566
13567 case 0x43: /* R_RL78_ABS16. */
13568 if (IN_RANGE (start, end, start + reloc->r_offset, 2))
13569 byte_put (start + reloc->r_offset, value, 2);
13570 else
13571 error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
13572 (long) reloc->r_offset);
13573 value = 0;
13574 return true;
13575
13576 default:
13577 break;
13578 }
13579 break;
13580 }
13581 }
13582
13583 return false;
13584 }
13585
13586 /* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
13587 DWARF debug sections. This is a target specific test. Note - we do not
13588 go through the whole including-target-headers-multiple-times route, (as
13589 we have already done with <elf/h8.h>) because this would become very
13590 messy and even then this function would have to contain target specific
13591 information (the names of the relocs instead of their numeric values).
13592 FIXME: This is not the correct way to solve this problem. The proper way
13593 is to have target specific reloc sizing and typing functions created by
13594 the reloc-macros.h header, in the same way that it already creates the
13595 reloc naming functions. */
13596
13597 static bool
13598 is_32bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13599 {
13600 /* Please keep this table alpha-sorted for ease of visual lookup. */
13601 switch (filedata->file_header.e_machine)
13602 {
13603 case EM_386:
13604 case EM_IAMCU:
13605 return reloc_type == 1; /* R_386_32. */
13606 case EM_68K:
13607 return reloc_type == 1; /* R_68K_32. */
13608 case EM_860:
13609 return reloc_type == 1; /* R_860_32. */
13610 case EM_960:
13611 return reloc_type == 2; /* R_960_32. */
13612 case EM_AARCH64:
13613 return (reloc_type == 258
13614 || reloc_type == 1); /* R_AARCH64_ABS32 || R_AARCH64_P32_ABS32 */
13615 case EM_BPF:
13616 return reloc_type == 11; /* R_BPF_DATA_32 */
13617 case EM_ADAPTEVA_EPIPHANY:
13618 return reloc_type == 3;
13619 case EM_ALPHA:
13620 return reloc_type == 1; /* R_ALPHA_REFLONG. */
13621 case EM_ARC:
13622 return reloc_type == 1; /* R_ARC_32. */
13623 case EM_ARC_COMPACT:
13624 case EM_ARC_COMPACT2:
13625 return reloc_type == 4; /* R_ARC_32. */
13626 case EM_ARM:
13627 return reloc_type == 2; /* R_ARM_ABS32 */
13628 case EM_AVR_OLD:
13629 case EM_AVR:
13630 return reloc_type == 1;
13631 case EM_BLACKFIN:
13632 return reloc_type == 0x12; /* R_byte4_data. */
13633 case EM_CRIS:
13634 return reloc_type == 3; /* R_CRIS_32. */
13635 case EM_CR16:
13636 return reloc_type == 3; /* R_CR16_NUM32. */
13637 case EM_CRX:
13638 return reloc_type == 15; /* R_CRX_NUM32. */
13639 case EM_CSKY:
13640 return reloc_type == 1; /* R_CKCORE_ADDR32. */
13641 case EM_CYGNUS_FRV:
13642 return reloc_type == 1;
13643 case EM_CYGNUS_D10V:
13644 case EM_D10V:
13645 return reloc_type == 6; /* R_D10V_32. */
13646 case EM_CYGNUS_D30V:
13647 case EM_D30V:
13648 return reloc_type == 12; /* R_D30V_32_NORMAL. */
13649 case EM_DLX:
13650 return reloc_type == 3; /* R_DLX_RELOC_32. */
13651 case EM_CYGNUS_FR30:
13652 case EM_FR30:
13653 return reloc_type == 3; /* R_FR30_32. */
13654 case EM_FT32:
13655 return reloc_type == 1; /* R_FT32_32. */
13656 case EM_H8S:
13657 case EM_H8_300:
13658 case EM_H8_300H:
13659 return reloc_type == 1; /* R_H8_DIR32. */
13660 case EM_IA_64:
13661 return (reloc_type == 0x64 /* R_IA64_SECREL32MSB. */
13662 || reloc_type == 0x65 /* R_IA64_SECREL32LSB. */
13663 || reloc_type == 0x24 /* R_IA64_DIR32MSB. */
13664 || reloc_type == 0x25 /* R_IA64_DIR32LSB. */);
13665 case EM_IP2K_OLD:
13666 case EM_IP2K:
13667 return reloc_type == 2; /* R_IP2K_32. */
13668 case EM_IQ2000:
13669 return reloc_type == 2; /* R_IQ2000_32. */
13670 case EM_LATTICEMICO32:
13671 return reloc_type == 3; /* R_LM32_32. */
13672 case EM_M32C_OLD:
13673 case EM_M32C:
13674 return reloc_type == 3; /* R_M32C_32. */
13675 case EM_M32R:
13676 return reloc_type == 34; /* R_M32R_32_RELA. */
13677 case EM_68HC11:
13678 case EM_68HC12:
13679 return reloc_type == 6; /* R_M68HC11_32. */
13680 case EM_S12Z:
13681 return reloc_type == 7 || /* R_S12Z_EXT32 */
13682 reloc_type == 6; /* R_S12Z_CW32. */
13683 case EM_MCORE:
13684 return reloc_type == 1; /* R_MCORE_ADDR32. */
13685 case EM_CYGNUS_MEP:
13686 return reloc_type == 4; /* R_MEP_32. */
13687 case EM_METAG:
13688 return reloc_type == 2; /* R_METAG_ADDR32. */
13689 case EM_MICROBLAZE:
13690 return reloc_type == 1; /* R_MICROBLAZE_32. */
13691 case EM_MIPS:
13692 return reloc_type == 2; /* R_MIPS_32. */
13693 case EM_MMIX:
13694 return reloc_type == 4; /* R_MMIX_32. */
13695 case EM_CYGNUS_MN10200:
13696 case EM_MN10200:
13697 return reloc_type == 1; /* R_MN10200_32. */
13698 case EM_CYGNUS_MN10300:
13699 case EM_MN10300:
13700 return reloc_type == 1; /* R_MN10300_32. */
13701 case EM_MOXIE:
13702 return reloc_type == 1; /* R_MOXIE_32. */
13703 case EM_MSP430_OLD:
13704 case EM_MSP430:
13705 return reloc_type == 1; /* R_MSP430_32 or R_MSP320_ABS32. */
13706 case EM_MT:
13707 return reloc_type == 2; /* R_MT_32. */
13708 case EM_NDS32:
13709 return reloc_type == 20; /* R_NDS32_RELA. */
13710 case EM_ALTERA_NIOS2:
13711 return reloc_type == 12; /* R_NIOS2_BFD_RELOC_32. */
13712 case EM_NIOS32:
13713 return reloc_type == 1; /* R_NIOS_32. */
13714 case EM_OR1K:
13715 return reloc_type == 1; /* R_OR1K_32. */
13716 case EM_PARISC:
13717 return (reloc_type == 1 /* R_PARISC_DIR32. */
13718 || reloc_type == 2 /* R_PARISC_DIR21L. */
13719 || reloc_type == 41); /* R_PARISC_SECREL32. */
13720 case EM_PJ:
13721 case EM_PJ_OLD:
13722 return reloc_type == 1; /* R_PJ_DATA_DIR32. */
13723 case EM_PPC64:
13724 return reloc_type == 1; /* R_PPC64_ADDR32. */
13725 case EM_PPC:
13726 return reloc_type == 1; /* R_PPC_ADDR32. */
13727 case EM_TI_PRU:
13728 return reloc_type == 11; /* R_PRU_BFD_RELOC_32. */
13729 case EM_RISCV:
13730 return reloc_type == 1; /* R_RISCV_32. */
13731 case EM_RL78:
13732 return reloc_type == 1; /* R_RL78_DIR32. */
13733 case EM_RX:
13734 return reloc_type == 1; /* R_RX_DIR32. */
13735 case EM_S370:
13736 return reloc_type == 1; /* R_I370_ADDR31. */
13737 case EM_S390_OLD:
13738 case EM_S390:
13739 return reloc_type == 4; /* R_S390_32. */
13740 case EM_SCORE:
13741 return reloc_type == 8; /* R_SCORE_ABS32. */
13742 case EM_SH:
13743 return reloc_type == 1; /* R_SH_DIR32. */
13744 case EM_SPARC32PLUS:
13745 case EM_SPARCV9:
13746 case EM_SPARC:
13747 return reloc_type == 3 /* R_SPARC_32. */
13748 || reloc_type == 23; /* R_SPARC_UA32. */
13749 case EM_SPU:
13750 return reloc_type == 6; /* R_SPU_ADDR32 */
13751 case EM_TI_C6000:
13752 return reloc_type == 1; /* R_C6000_ABS32. */
13753 case EM_TILEGX:
13754 return reloc_type == 2; /* R_TILEGX_32. */
13755 case EM_TILEPRO:
13756 return reloc_type == 1; /* R_TILEPRO_32. */
13757 case EM_CYGNUS_V850:
13758 case EM_V850:
13759 return reloc_type == 6; /* R_V850_ABS32. */
13760 case EM_V800:
13761 return reloc_type == 0x33; /* R_V810_WORD. */
13762 case EM_VAX:
13763 return reloc_type == 1; /* R_VAX_32. */
13764 case EM_VISIUM:
13765 return reloc_type == 3; /* R_VISIUM_32. */
13766 case EM_WEBASSEMBLY:
13767 return reloc_type == 1; /* R_WASM32_32. */
13768 case EM_X86_64:
13769 case EM_L1OM:
13770 case EM_K1OM:
13771 return reloc_type == 10; /* R_X86_64_32. */
13772 case EM_XC16X:
13773 case EM_C166:
13774 return reloc_type == 3; /* R_XC16C_ABS_32. */
13775 case EM_XGATE:
13776 return reloc_type == 4; /* R_XGATE_32. */
13777 case EM_XSTORMY16:
13778 return reloc_type == 1; /* R_XSTROMY16_32. */
13779 case EM_XTENSA_OLD:
13780 case EM_XTENSA:
13781 return reloc_type == 1; /* R_XTENSA_32. */
13782 case EM_Z80:
13783 return reloc_type == 6; /* R_Z80_32. */
13784 default:
13785 {
13786 static unsigned int prev_warn = 0;
13787
13788 /* Avoid repeating the same warning multiple times. */
13789 if (prev_warn != filedata->file_header.e_machine)
13790 error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
13791 filedata->file_header.e_machine);
13792 prev_warn = filedata->file_header.e_machine;
13793 return false;
13794 }
13795 }
13796 }
13797
13798 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13799 a 32-bit pc-relative RELA relocation used in DWARF debug sections. */
13800
13801 static bool
13802 is_32bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
13803 {
13804 switch (filedata->file_header.e_machine)
13805 /* Please keep this table alpha-sorted for ease of visual lookup. */
13806 {
13807 case EM_386:
13808 case EM_IAMCU:
13809 return reloc_type == 2; /* R_386_PC32. */
13810 case EM_68K:
13811 return reloc_type == 4; /* R_68K_PC32. */
13812 case EM_AARCH64:
13813 return reloc_type == 261; /* R_AARCH64_PREL32 */
13814 case EM_ADAPTEVA_EPIPHANY:
13815 return reloc_type == 6;
13816 case EM_ALPHA:
13817 return reloc_type == 10; /* R_ALPHA_SREL32. */
13818 case EM_ARC_COMPACT:
13819 case EM_ARC_COMPACT2:
13820 return reloc_type == 49; /* R_ARC_32_PCREL. */
13821 case EM_ARM:
13822 return reloc_type == 3; /* R_ARM_REL32 */
13823 case EM_AVR_OLD:
13824 case EM_AVR:
13825 return reloc_type == 36; /* R_AVR_32_PCREL. */
13826 case EM_MICROBLAZE:
13827 return reloc_type == 2; /* R_MICROBLAZE_32_PCREL. */
13828 case EM_OR1K:
13829 return reloc_type == 9; /* R_OR1K_32_PCREL. */
13830 case EM_PARISC:
13831 return reloc_type == 9; /* R_PARISC_PCREL32. */
13832 case EM_PPC:
13833 return reloc_type == 26; /* R_PPC_REL32. */
13834 case EM_PPC64:
13835 return reloc_type == 26; /* R_PPC64_REL32. */
13836 case EM_RISCV:
13837 return reloc_type == 57; /* R_RISCV_32_PCREL. */
13838 case EM_S390_OLD:
13839 case EM_S390:
13840 return reloc_type == 5; /* R_390_PC32. */
13841 case EM_SH:
13842 return reloc_type == 2; /* R_SH_REL32. */
13843 case EM_SPARC32PLUS:
13844 case EM_SPARCV9:
13845 case EM_SPARC:
13846 return reloc_type == 6; /* R_SPARC_DISP32. */
13847 case EM_SPU:
13848 return reloc_type == 13; /* R_SPU_REL32. */
13849 case EM_TILEGX:
13850 return reloc_type == 6; /* R_TILEGX_32_PCREL. */
13851 case EM_TILEPRO:
13852 return reloc_type == 4; /* R_TILEPRO_32_PCREL. */
13853 case EM_VISIUM:
13854 return reloc_type == 6; /* R_VISIUM_32_PCREL */
13855 case EM_X86_64:
13856 case EM_L1OM:
13857 case EM_K1OM:
13858 return reloc_type == 2; /* R_X86_64_PC32. */
13859 case EM_VAX:
13860 return reloc_type == 4; /* R_VAX_PCREL32. */
13861 case EM_XTENSA_OLD:
13862 case EM_XTENSA:
13863 return reloc_type == 14; /* R_XTENSA_32_PCREL. */
13864 default:
13865 /* Do not abort or issue an error message here. Not all targets use
13866 pc-relative 32-bit relocs in their DWARF debug information and we
13867 have already tested for target coverage in is_32bit_abs_reloc. A
13868 more helpful warning message will be generated by apply_relocations
13869 anyway, so just return. */
13870 return false;
13871 }
13872 }
13873
13874 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13875 a 64-bit absolute RELA relocation used in DWARF debug sections. */
13876
13877 static bool
13878 is_64bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13879 {
13880 switch (filedata->file_header.e_machine)
13881 {
13882 case EM_AARCH64:
13883 return reloc_type == 257; /* R_AARCH64_ABS64. */
13884 case EM_ALPHA:
13885 return reloc_type == 2; /* R_ALPHA_REFQUAD. */
13886 case EM_IA_64:
13887 return (reloc_type == 0x26 /* R_IA64_DIR64MSB. */
13888 || reloc_type == 0x27 /* R_IA64_DIR64LSB. */);
13889 case EM_PARISC:
13890 return reloc_type == 80; /* R_PARISC_DIR64. */
13891 case EM_PPC64:
13892 return reloc_type == 38; /* R_PPC64_ADDR64. */
13893 case EM_RISCV:
13894 return reloc_type == 2; /* R_RISCV_64. */
13895 case EM_SPARC32PLUS:
13896 case EM_SPARCV9:
13897 case EM_SPARC:
13898 return reloc_type == 32 /* R_SPARC_64. */
13899 || reloc_type == 54; /* R_SPARC_UA64. */
13900 case EM_X86_64:
13901 case EM_L1OM:
13902 case EM_K1OM:
13903 return reloc_type == 1; /* R_X86_64_64. */
13904 case EM_S390_OLD:
13905 case EM_S390:
13906 return reloc_type == 22; /* R_S390_64. */
13907 case EM_TILEGX:
13908 return reloc_type == 1; /* R_TILEGX_64. */
13909 case EM_MIPS:
13910 return reloc_type == 18; /* R_MIPS_64. */
13911 default:
13912 return false;
13913 }
13914 }
13915
13916 /* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
13917 a 64-bit pc-relative RELA relocation used in DWARF debug sections. */
13918
13919 static bool
13920 is_64bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
13921 {
13922 switch (filedata->file_header.e_machine)
13923 {
13924 case EM_AARCH64:
13925 return reloc_type == 260; /* R_AARCH64_PREL64. */
13926 case EM_ALPHA:
13927 return reloc_type == 11; /* R_ALPHA_SREL64. */
13928 case EM_IA_64:
13929 return (reloc_type == 0x4e /* R_IA64_PCREL64MSB. */
13930 || reloc_type == 0x4f /* R_IA64_PCREL64LSB. */);
13931 case EM_PARISC:
13932 return reloc_type == 72; /* R_PARISC_PCREL64. */
13933 case EM_PPC64:
13934 return reloc_type == 44; /* R_PPC64_REL64. */
13935 case EM_SPARC32PLUS:
13936 case EM_SPARCV9:
13937 case EM_SPARC:
13938 return reloc_type == 46; /* R_SPARC_DISP64. */
13939 case EM_X86_64:
13940 case EM_L1OM:
13941 case EM_K1OM:
13942 return reloc_type == 24; /* R_X86_64_PC64. */
13943 case EM_S390_OLD:
13944 case EM_S390:
13945 return reloc_type == 23; /* R_S390_PC64. */
13946 case EM_TILEGX:
13947 return reloc_type == 5; /* R_TILEGX_64_PCREL. */
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 24-bit absolute RELA relocation used in DWARF debug sections. */
13955
13956 static bool
13957 is_24bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13958 {
13959 switch (filedata->file_header.e_machine)
13960 {
13961 case EM_CYGNUS_MN10200:
13962 case EM_MN10200:
13963 return reloc_type == 4; /* R_MN10200_24. */
13964 case EM_FT32:
13965 return reloc_type == 5; /* R_FT32_20. */
13966 case EM_Z80:
13967 return reloc_type == 5; /* R_Z80_24. */
13968 default:
13969 return false;
13970 }
13971 }
13972
13973 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13974 a 16-bit absolute RELA relocation used in DWARF debug sections. */
13975
13976 static bool
13977 is_16bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13978 {
13979 /* Please keep this table alpha-sorted for ease of visual lookup. */
13980 switch (filedata->file_header.e_machine)
13981 {
13982 case EM_ARC:
13983 case EM_ARC_COMPACT:
13984 case EM_ARC_COMPACT2:
13985 return reloc_type == 2; /* R_ARC_16. */
13986 case EM_ADAPTEVA_EPIPHANY:
13987 return reloc_type == 5;
13988 case EM_AVR_OLD:
13989 case EM_AVR:
13990 return reloc_type == 4; /* R_AVR_16. */
13991 case EM_CYGNUS_D10V:
13992 case EM_D10V:
13993 return reloc_type == 3; /* R_D10V_16. */
13994 case EM_FT32:
13995 return reloc_type == 2; /* R_FT32_16. */
13996 case EM_H8S:
13997 case EM_H8_300:
13998 case EM_H8_300H:
13999 return reloc_type == R_H8_DIR16;
14000 case EM_IP2K_OLD:
14001 case EM_IP2K:
14002 return reloc_type == 1; /* R_IP2K_16. */
14003 case EM_M32C_OLD:
14004 case EM_M32C:
14005 return reloc_type == 1; /* R_M32C_16 */
14006 case EM_CYGNUS_MN10200:
14007 case EM_MN10200:
14008 return reloc_type == 2; /* R_MN10200_16. */
14009 case EM_CYGNUS_MN10300:
14010 case EM_MN10300:
14011 return reloc_type == 2; /* R_MN10300_16. */
14012 case EM_MSP430:
14013 if (uses_msp430x_relocs (filedata))
14014 return reloc_type == 2; /* R_MSP430_ABS16. */
14015 /* Fall through. */
14016 case EM_MSP430_OLD:
14017 return reloc_type == 5; /* R_MSP430_16_BYTE. */
14018 case EM_NDS32:
14019 return reloc_type == 19; /* R_NDS32_RELA. */
14020 case EM_ALTERA_NIOS2:
14021 return reloc_type == 13; /* R_NIOS2_BFD_RELOC_16. */
14022 case EM_NIOS32:
14023 return reloc_type == 9; /* R_NIOS_16. */
14024 case EM_OR1K:
14025 return reloc_type == 2; /* R_OR1K_16. */
14026 case EM_RISCV:
14027 return reloc_type == 55; /* R_RISCV_SET16. */
14028 case EM_TI_PRU:
14029 return reloc_type == 8; /* R_PRU_BFD_RELOC_16. */
14030 case EM_TI_C6000:
14031 return reloc_type == 2; /* R_C6000_ABS16. */
14032 case EM_VISIUM:
14033 return reloc_type == 2; /* R_VISIUM_16. */
14034 case EM_XC16X:
14035 case EM_C166:
14036 return reloc_type == 2; /* R_XC16C_ABS_16. */
14037 case EM_XGATE:
14038 return reloc_type == 3; /* R_XGATE_16. */
14039 case EM_Z80:
14040 return reloc_type == 4; /* R_Z80_16. */
14041 default:
14042 return false;
14043 }
14044 }
14045
14046 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14047 a 8-bit absolute RELA relocation used in DWARF debug sections. */
14048
14049 static bool
14050 is_8bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14051 {
14052 switch (filedata->file_header.e_machine)
14053 {
14054 case EM_RISCV:
14055 return reloc_type == 54; /* R_RISCV_SET8. */
14056 case EM_Z80:
14057 return reloc_type == 1; /* R_Z80_8. */
14058 default:
14059 return false;
14060 }
14061 }
14062
14063 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14064 a 6-bit absolute RELA relocation used in DWARF debug sections. */
14065
14066 static bool
14067 is_6bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14068 {
14069 switch (filedata->file_header.e_machine)
14070 {
14071 case EM_RISCV:
14072 return reloc_type == 53; /* R_RISCV_SET6. */
14073 default:
14074 return false;
14075 }
14076 }
14077
14078 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14079 a 32-bit inplace add RELA relocation used in DWARF debug sections. */
14080
14081 static bool
14082 is_32bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14083 {
14084 /* Please keep this table alpha-sorted for ease of visual lookup. */
14085 switch (filedata->file_header.e_machine)
14086 {
14087 case EM_RISCV:
14088 return reloc_type == 35; /* R_RISCV_ADD32. */
14089 default:
14090 return false;
14091 }
14092 }
14093
14094 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14095 a 32-bit inplace sub RELA relocation used in DWARF debug sections. */
14096
14097 static bool
14098 is_32bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14099 {
14100 /* Please keep this table alpha-sorted for ease of visual lookup. */
14101 switch (filedata->file_header.e_machine)
14102 {
14103 case EM_RISCV:
14104 return reloc_type == 39; /* R_RISCV_SUB32. */
14105 default:
14106 return false;
14107 }
14108 }
14109
14110 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14111 a 64-bit inplace add RELA relocation used in DWARF debug sections. */
14112
14113 static bool
14114 is_64bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14115 {
14116 /* Please keep this table alpha-sorted for ease of visual lookup. */
14117 switch (filedata->file_header.e_machine)
14118 {
14119 case EM_RISCV:
14120 return reloc_type == 36; /* R_RISCV_ADD64. */
14121 default:
14122 return false;
14123 }
14124 }
14125
14126 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14127 a 64-bit inplace sub RELA relocation used in DWARF debug sections. */
14128
14129 static bool
14130 is_64bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14131 {
14132 /* Please keep this table alpha-sorted for ease of visual lookup. */
14133 switch (filedata->file_header.e_machine)
14134 {
14135 case EM_RISCV:
14136 return reloc_type == 40; /* R_RISCV_SUB64. */
14137 default:
14138 return false;
14139 }
14140 }
14141
14142 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14143 a 16-bit inplace add RELA relocation used in DWARF debug sections. */
14144
14145 static bool
14146 is_16bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14147 {
14148 /* Please keep this table alpha-sorted for ease of visual lookup. */
14149 switch (filedata->file_header.e_machine)
14150 {
14151 case EM_RISCV:
14152 return reloc_type == 34; /* R_RISCV_ADD16. */
14153 default:
14154 return false;
14155 }
14156 }
14157
14158 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14159 a 16-bit inplace sub RELA relocation used in DWARF debug sections. */
14160
14161 static bool
14162 is_16bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14163 {
14164 /* Please keep this table alpha-sorted for ease of visual lookup. */
14165 switch (filedata->file_header.e_machine)
14166 {
14167 case EM_RISCV:
14168 return reloc_type == 38; /* R_RISCV_SUB16. */
14169 default:
14170 return false;
14171 }
14172 }
14173
14174 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14175 a 8-bit inplace add RELA relocation used in DWARF debug sections. */
14176
14177 static bool
14178 is_8bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14179 {
14180 /* Please keep this table alpha-sorted for ease of visual lookup. */
14181 switch (filedata->file_header.e_machine)
14182 {
14183 case EM_RISCV:
14184 return reloc_type == 33; /* R_RISCV_ADD8. */
14185 default:
14186 return false;
14187 }
14188 }
14189
14190 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14191 a 8-bit inplace sub RELA relocation used in DWARF debug sections. */
14192
14193 static bool
14194 is_8bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14195 {
14196 /* Please keep this table alpha-sorted for ease of visual lookup. */
14197 switch (filedata->file_header.e_machine)
14198 {
14199 case EM_RISCV:
14200 return reloc_type == 37; /* R_RISCV_SUB8. */
14201 default:
14202 return false;
14203 }
14204 }
14205
14206 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14207 a 6-bit inplace sub RELA relocation used in DWARF debug sections. */
14208
14209 static bool
14210 is_6bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14211 {
14212 switch (filedata->file_header.e_machine)
14213 {
14214 case EM_RISCV:
14215 return reloc_type == 52; /* R_RISCV_SUB6. */
14216 default:
14217 return false;
14218 }
14219 }
14220
14221 /* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
14222 relocation entries (possibly formerly used for SHT_GROUP sections). */
14223
14224 static bool
14225 is_none_reloc (Filedata * filedata, unsigned int reloc_type)
14226 {
14227 switch (filedata->file_header.e_machine)
14228 {
14229 case EM_386: /* R_386_NONE. */
14230 case EM_68K: /* R_68K_NONE. */
14231 case EM_ADAPTEVA_EPIPHANY:
14232 case EM_ALPHA: /* R_ALPHA_NONE. */
14233 case EM_ALTERA_NIOS2: /* R_NIOS2_NONE. */
14234 case EM_ARC: /* R_ARC_NONE. */
14235 case EM_ARC_COMPACT2: /* R_ARC_NONE. */
14236 case EM_ARC_COMPACT: /* R_ARC_NONE. */
14237 case EM_ARM: /* R_ARM_NONE. */
14238 case EM_C166: /* R_XC16X_NONE. */
14239 case EM_CRIS: /* R_CRIS_NONE. */
14240 case EM_FT32: /* R_FT32_NONE. */
14241 case EM_IA_64: /* R_IA64_NONE. */
14242 case EM_K1OM: /* R_X86_64_NONE. */
14243 case EM_L1OM: /* R_X86_64_NONE. */
14244 case EM_M32R: /* R_M32R_NONE. */
14245 case EM_MIPS: /* R_MIPS_NONE. */
14246 case EM_MN10300: /* R_MN10300_NONE. */
14247 case EM_MOXIE: /* R_MOXIE_NONE. */
14248 case EM_NIOS32: /* R_NIOS_NONE. */
14249 case EM_OR1K: /* R_OR1K_NONE. */
14250 case EM_PARISC: /* R_PARISC_NONE. */
14251 case EM_PPC64: /* R_PPC64_NONE. */
14252 case EM_PPC: /* R_PPC_NONE. */
14253 case EM_RISCV: /* R_RISCV_NONE. */
14254 case EM_S390: /* R_390_NONE. */
14255 case EM_S390_OLD:
14256 case EM_SH: /* R_SH_NONE. */
14257 case EM_SPARC32PLUS:
14258 case EM_SPARC: /* R_SPARC_NONE. */
14259 case EM_SPARCV9:
14260 case EM_TILEGX: /* R_TILEGX_NONE. */
14261 case EM_TILEPRO: /* R_TILEPRO_NONE. */
14262 case EM_TI_C6000:/* R_C6000_NONE. */
14263 case EM_X86_64: /* R_X86_64_NONE. */
14264 case EM_XC16X:
14265 case EM_Z80: /* R_Z80_NONE. */
14266 case EM_WEBASSEMBLY: /* R_WASM32_NONE. */
14267 return reloc_type == 0;
14268
14269 case EM_AARCH64:
14270 return reloc_type == 0 || reloc_type == 256;
14271 case EM_AVR_OLD:
14272 case EM_AVR:
14273 return (reloc_type == 0 /* R_AVR_NONE. */
14274 || reloc_type == 30 /* R_AVR_DIFF8. */
14275 || reloc_type == 31 /* R_AVR_DIFF16. */
14276 || reloc_type == 32 /* R_AVR_DIFF32. */);
14277 case EM_METAG:
14278 return reloc_type == 3; /* R_METAG_NONE. */
14279 case EM_NDS32:
14280 return (reloc_type == 0 /* R_XTENSA_NONE. */
14281 || reloc_type == 204 /* R_NDS32_DIFF8. */
14282 || reloc_type == 205 /* R_NDS32_DIFF16. */
14283 || reloc_type == 206 /* R_NDS32_DIFF32. */
14284 || reloc_type == 207 /* R_NDS32_ULEB128. */);
14285 case EM_TI_PRU:
14286 return (reloc_type == 0 /* R_PRU_NONE. */
14287 || reloc_type == 65 /* R_PRU_DIFF8. */
14288 || reloc_type == 66 /* R_PRU_DIFF16. */
14289 || reloc_type == 67 /* R_PRU_DIFF32. */);
14290 case EM_XTENSA_OLD:
14291 case EM_XTENSA:
14292 return (reloc_type == 0 /* R_XTENSA_NONE. */
14293 || reloc_type == 17 /* R_XTENSA_DIFF8. */
14294 || reloc_type == 18 /* R_XTENSA_DIFF16. */
14295 || reloc_type == 19 /* R_XTENSA_DIFF32. */
14296 || reloc_type == 57 /* R_XTENSA_PDIFF8. */
14297 || reloc_type == 58 /* R_XTENSA_PDIFF16. */
14298 || reloc_type == 59 /* R_XTENSA_PDIFF32. */
14299 || reloc_type == 60 /* R_XTENSA_NDIFF8. */
14300 || reloc_type == 61 /* R_XTENSA_NDIFF16. */
14301 || reloc_type == 62 /* R_XTENSA_NDIFF32. */);
14302 }
14303 return false;
14304 }
14305
14306 /* Returns TRUE if there is a relocation against
14307 section NAME at OFFSET bytes. */
14308
14309 bool
14310 reloc_at (struct dwarf_section * dsec, dwarf_vma offset)
14311 {
14312 Elf_Internal_Rela * relocs;
14313 Elf_Internal_Rela * rp;
14314
14315 if (dsec == NULL || dsec->reloc_info == NULL)
14316 return false;
14317
14318 relocs = (Elf_Internal_Rela *) dsec->reloc_info;
14319
14320 for (rp = relocs; rp < relocs + dsec->num_relocs; ++rp)
14321 if (rp->r_offset == offset)
14322 return true;
14323
14324 return false;
14325 }
14326
14327 /* Apply relocations to a section.
14328 Returns TRUE upon success, FALSE otherwise.
14329 If RELOCS_RETURN is non-NULL then it is set to point to the loaded relocs.
14330 It is then the caller's responsibility to free them. NUM_RELOCS_RETURN
14331 will be set to the number of relocs loaded.
14332
14333 Note: So far support has been added only for those relocations
14334 which can be found in debug sections. FIXME: Add support for
14335 more relocations ? */
14336
14337 static bool
14338 apply_relocations (Filedata * filedata,
14339 const Elf_Internal_Shdr * section,
14340 unsigned char * start,
14341 bfd_size_type size,
14342 void ** relocs_return,
14343 unsigned long * num_relocs_return)
14344 {
14345 Elf_Internal_Shdr * relsec;
14346 unsigned char * end = start + size;
14347
14348 if (relocs_return != NULL)
14349 {
14350 * (Elf_Internal_Rela **) relocs_return = NULL;
14351 * num_relocs_return = 0;
14352 }
14353
14354 if (filedata->file_header.e_type != ET_REL)
14355 /* No relocs to apply. */
14356 return true;
14357
14358 /* Find the reloc section associated with the section. */
14359 for (relsec = filedata->section_headers;
14360 relsec < filedata->section_headers + filedata->file_header.e_shnum;
14361 ++relsec)
14362 {
14363 bool is_rela;
14364 unsigned long num_relocs;
14365 Elf_Internal_Rela * relocs;
14366 Elf_Internal_Rela * rp;
14367 Elf_Internal_Shdr * symsec;
14368 Elf_Internal_Sym * symtab;
14369 unsigned long num_syms;
14370 Elf_Internal_Sym * sym;
14371
14372 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
14373 || relsec->sh_info >= filedata->file_header.e_shnum
14374 || filedata->section_headers + relsec->sh_info != section
14375 || relsec->sh_size == 0
14376 || relsec->sh_link >= filedata->file_header.e_shnum)
14377 continue;
14378
14379 symsec = filedata->section_headers + relsec->sh_link;
14380 if (symsec->sh_type != SHT_SYMTAB
14381 && symsec->sh_type != SHT_DYNSYM)
14382 return false;
14383
14384 is_rela = relsec->sh_type == SHT_RELA;
14385
14386 if (is_rela)
14387 {
14388 if (!slurp_rela_relocs (filedata, relsec->sh_offset,
14389 relsec->sh_size, & relocs, & num_relocs))
14390 return false;
14391 }
14392 else
14393 {
14394 if (!slurp_rel_relocs (filedata, relsec->sh_offset,
14395 relsec->sh_size, & relocs, & num_relocs))
14396 return false;
14397 }
14398
14399 /* SH uses RELA but uses in place value instead of the addend field. */
14400 if (filedata->file_header.e_machine == EM_SH)
14401 is_rela = false;
14402
14403 symtab = get_elf_symbols (filedata, symsec, & num_syms);
14404
14405 for (rp = relocs; rp < relocs + num_relocs; ++rp)
14406 {
14407 bfd_vma addend;
14408 unsigned int reloc_type;
14409 unsigned int reloc_size;
14410 bool reloc_inplace = false;
14411 bool reloc_subtract = false;
14412 unsigned char *rloc;
14413 unsigned long sym_index;
14414
14415 reloc_type = get_reloc_type (filedata, rp->r_info);
14416
14417 if (target_specific_reloc_handling (filedata, rp, start, end, symtab, num_syms))
14418 continue;
14419 else if (is_none_reloc (filedata, reloc_type))
14420 continue;
14421 else if (is_32bit_abs_reloc (filedata, reloc_type)
14422 || is_32bit_pcrel_reloc (filedata, reloc_type))
14423 reloc_size = 4;
14424 else if (is_64bit_abs_reloc (filedata, reloc_type)
14425 || is_64bit_pcrel_reloc (filedata, reloc_type))
14426 reloc_size = 8;
14427 else if (is_24bit_abs_reloc (filedata, reloc_type))
14428 reloc_size = 3;
14429 else if (is_16bit_abs_reloc (filedata, reloc_type))
14430 reloc_size = 2;
14431 else if (is_8bit_abs_reloc (filedata, reloc_type)
14432 || is_6bit_abs_reloc (filedata, reloc_type))
14433 reloc_size = 1;
14434 else if ((reloc_subtract = is_32bit_inplace_sub_reloc (filedata,
14435 reloc_type))
14436 || is_32bit_inplace_add_reloc (filedata, reloc_type))
14437 {
14438 reloc_size = 4;
14439 reloc_inplace = true;
14440 }
14441 else if ((reloc_subtract = is_64bit_inplace_sub_reloc (filedata,
14442 reloc_type))
14443 || is_64bit_inplace_add_reloc (filedata, reloc_type))
14444 {
14445 reloc_size = 8;
14446 reloc_inplace = true;
14447 }
14448 else if ((reloc_subtract = is_16bit_inplace_sub_reloc (filedata,
14449 reloc_type))
14450 || is_16bit_inplace_add_reloc (filedata, reloc_type))
14451 {
14452 reloc_size = 2;
14453 reloc_inplace = true;
14454 }
14455 else if ((reloc_subtract = is_8bit_inplace_sub_reloc (filedata,
14456 reloc_type))
14457 || is_8bit_inplace_add_reloc (filedata, reloc_type))
14458 {
14459 reloc_size = 1;
14460 reloc_inplace = true;
14461 }
14462 else if ((reloc_subtract = is_6bit_inplace_sub_reloc (filedata,
14463 reloc_type)))
14464 {
14465 reloc_size = 1;
14466 reloc_inplace = true;
14467 }
14468 else
14469 {
14470 static unsigned int prev_reloc = 0;
14471
14472 if (reloc_type != prev_reloc)
14473 warn (_("unable to apply unsupported reloc type %d to section %s\n"),
14474 reloc_type, printable_section_name (filedata, section));
14475 prev_reloc = reloc_type;
14476 continue;
14477 }
14478
14479 rloc = start + rp->r_offset;
14480 if (!IN_RANGE (start, end, rloc, reloc_size))
14481 {
14482 warn (_("skipping invalid relocation offset 0x%lx in section %s\n"),
14483 (unsigned long) rp->r_offset,
14484 printable_section_name (filedata, section));
14485 continue;
14486 }
14487
14488 sym_index = (unsigned long) get_reloc_symindex (rp->r_info);
14489 if (sym_index >= num_syms)
14490 {
14491 warn (_("skipping invalid relocation symbol index 0x%lx in section %s\n"),
14492 sym_index, printable_section_name (filedata, section));
14493 continue;
14494 }
14495 sym = symtab + sym_index;
14496
14497 /* If the reloc has a symbol associated with it,
14498 make sure that it is of an appropriate type.
14499
14500 Relocations against symbols without type can happen.
14501 Gcc -feliminate-dwarf2-dups may generate symbols
14502 without type for debug info.
14503
14504 Icc generates relocations against function symbols
14505 instead of local labels.
14506
14507 Relocations against object symbols can happen, eg when
14508 referencing a global array. For an example of this see
14509 the _clz.o binary in libgcc.a. */
14510 if (sym != symtab
14511 && ELF_ST_TYPE (sym->st_info) != STT_COMMON
14512 && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
14513 {
14514 warn (_("skipping unexpected symbol type %s in section %s relocation %ld\n"),
14515 get_symbol_type (filedata, ELF_ST_TYPE (sym->st_info)),
14516 printable_section_name (filedata, relsec),
14517 (long int)(rp - relocs));
14518 continue;
14519 }
14520
14521 addend = 0;
14522 if (is_rela)
14523 addend += rp->r_addend;
14524 /* R_XTENSA_32, R_PJ_DATA_DIR32 and R_D30V_32_NORMAL are
14525 partial_inplace. */
14526 if (!is_rela
14527 || (filedata->file_header.e_machine == EM_XTENSA
14528 && reloc_type == 1)
14529 || ((filedata->file_header.e_machine == EM_PJ
14530 || filedata->file_header.e_machine == EM_PJ_OLD)
14531 && reloc_type == 1)
14532 || ((filedata->file_header.e_machine == EM_D30V
14533 || filedata->file_header.e_machine == EM_CYGNUS_D30V)
14534 && reloc_type == 12)
14535 || reloc_inplace)
14536 {
14537 if (is_6bit_inplace_sub_reloc (filedata, reloc_type))
14538 addend += byte_get (rloc, reloc_size) & 0x3f;
14539 else
14540 addend += byte_get (rloc, reloc_size);
14541 }
14542
14543 if (is_32bit_pcrel_reloc (filedata, reloc_type)
14544 || is_64bit_pcrel_reloc (filedata, reloc_type))
14545 {
14546 /* On HPPA, all pc-relative relocations are biased by 8. */
14547 if (filedata->file_header.e_machine == EM_PARISC)
14548 addend -= 8;
14549 byte_put (rloc, (addend + sym->st_value) - rp->r_offset,
14550 reloc_size);
14551 }
14552 else if (is_6bit_abs_reloc (filedata, reloc_type)
14553 || is_6bit_inplace_sub_reloc (filedata, reloc_type))
14554 {
14555 if (reloc_subtract)
14556 addend -= sym->st_value;
14557 else
14558 addend += sym->st_value;
14559 addend = (addend & 0x3f) | (byte_get (rloc, reloc_size) & 0xc0);
14560 byte_put (rloc, addend, reloc_size);
14561 }
14562 else if (reloc_subtract)
14563 byte_put (rloc, addend - sym->st_value, reloc_size);
14564 else
14565 byte_put (rloc, addend + sym->st_value, reloc_size);
14566 }
14567
14568 free (symtab);
14569 /* Let the target specific reloc processing code know that
14570 we have finished with these relocs. */
14571 target_specific_reloc_handling (filedata, NULL, NULL, NULL, NULL, 0);
14572
14573 if (relocs_return)
14574 {
14575 * (Elf_Internal_Rela **) relocs_return = relocs;
14576 * num_relocs_return = num_relocs;
14577 }
14578 else
14579 free (relocs);
14580
14581 break;
14582 }
14583
14584 return true;
14585 }
14586
14587 #ifdef SUPPORT_DISASSEMBLY
14588 static bool
14589 disassemble_section (Elf_Internal_Shdr * section, Filedata * filedata)
14590 {
14591 printf (_("\nAssembly dump of section %s\n"), printable_section_name (filedata, section));
14592
14593 /* FIXME: XXX -- to be done --- XXX */
14594
14595 return true;
14596 }
14597 #endif
14598
14599 /* Reads in the contents of SECTION from FILE, returning a pointer
14600 to a malloc'ed buffer or NULL if something went wrong. */
14601
14602 static char *
14603 get_section_contents (Elf_Internal_Shdr * section, Filedata * filedata)
14604 {
14605 bfd_size_type num_bytes = section->sh_size;
14606
14607 if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
14608 {
14609 printf (_("Section '%s' has no data to dump.\n"),
14610 printable_section_name (filedata, section));
14611 return NULL;
14612 }
14613
14614 return (char *) get_data (NULL, filedata, section->sh_offset, 1, num_bytes,
14615 _("section contents"));
14616 }
14617
14618 /* Uncompresses a section that was compressed using zlib, in place. */
14619
14620 static bool
14621 uncompress_section_contents (unsigned char ** buffer,
14622 dwarf_size_type uncompressed_size,
14623 dwarf_size_type * size)
14624 {
14625 dwarf_size_type compressed_size = *size;
14626 unsigned char * compressed_buffer = *buffer;
14627 unsigned char * uncompressed_buffer;
14628 z_stream strm;
14629 int rc;
14630
14631 /* It is possible the section consists of several compressed
14632 buffers concatenated together, so we uncompress in a loop. */
14633 /* PR 18313: The state field in the z_stream structure is supposed
14634 to be invisible to the user (ie us), but some compilers will
14635 still complain about it being used without initialisation. So
14636 we first zero the entire z_stream structure and then set the fields
14637 that we need. */
14638 memset (& strm, 0, sizeof strm);
14639 strm.avail_in = compressed_size;
14640 strm.next_in = (Bytef *) compressed_buffer;
14641 strm.avail_out = uncompressed_size;
14642 uncompressed_buffer = (unsigned char *) xmalloc (uncompressed_size);
14643
14644 rc = inflateInit (& strm);
14645 while (strm.avail_in > 0)
14646 {
14647 if (rc != Z_OK)
14648 break;
14649 strm.next_out = ((Bytef *) uncompressed_buffer
14650 + (uncompressed_size - strm.avail_out));
14651 rc = inflate (&strm, Z_FINISH);
14652 if (rc != Z_STREAM_END)
14653 break;
14654 rc = inflateReset (& strm);
14655 }
14656 if (inflateEnd (& strm) != Z_OK
14657 || rc != Z_OK
14658 || strm.avail_out != 0)
14659 goto fail;
14660
14661 *buffer = uncompressed_buffer;
14662 *size = uncompressed_size;
14663 return true;
14664
14665 fail:
14666 free (uncompressed_buffer);
14667 /* Indicate decompression failure. */
14668 *buffer = NULL;
14669 return false;
14670 }
14671
14672 static bool
14673 dump_section_as_strings (Elf_Internal_Shdr * section, Filedata * filedata)
14674 {
14675 Elf_Internal_Shdr *relsec;
14676 bfd_size_type num_bytes;
14677 unsigned char *data;
14678 unsigned char *end;
14679 unsigned char *real_start;
14680 unsigned char *start;
14681 bool some_strings_shown;
14682
14683 real_start = start = (unsigned char *) get_section_contents (section, filedata);
14684 if (start == NULL)
14685 /* PR 21820: Do not fail if the section was empty. */
14686 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
14687
14688 num_bytes = section->sh_size;
14689
14690 if (filedata->is_separate)
14691 printf (_("\nString dump of section '%s' in linked file %s:\n"),
14692 printable_section_name (filedata, section),
14693 filedata->file_name);
14694 else
14695 printf (_("\nString dump of section '%s':\n"),
14696 printable_section_name (filedata, section));
14697
14698 if (decompress_dumps)
14699 {
14700 dwarf_size_type new_size = num_bytes;
14701 dwarf_size_type uncompressed_size = 0;
14702
14703 if ((section->sh_flags & SHF_COMPRESSED) != 0)
14704 {
14705 Elf_Internal_Chdr chdr;
14706 unsigned int compression_header_size
14707 = get_compression_header (& chdr, (unsigned char *) start,
14708 num_bytes);
14709 if (compression_header_size == 0)
14710 /* An error message will have already been generated
14711 by get_compression_header. */
14712 goto error_out;
14713
14714 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
14715 {
14716 warn (_("section '%s' has unsupported compress type: %d\n"),
14717 printable_section_name (filedata, section), chdr.ch_type);
14718 goto error_out;
14719 }
14720 uncompressed_size = chdr.ch_size;
14721 start += compression_header_size;
14722 new_size -= compression_header_size;
14723 }
14724 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
14725 {
14726 /* Read the zlib header. In this case, it should be "ZLIB"
14727 followed by the uncompressed section size, 8 bytes in
14728 big-endian order. */
14729 uncompressed_size = start[4]; uncompressed_size <<= 8;
14730 uncompressed_size += start[5]; uncompressed_size <<= 8;
14731 uncompressed_size += start[6]; uncompressed_size <<= 8;
14732 uncompressed_size += start[7]; uncompressed_size <<= 8;
14733 uncompressed_size += start[8]; uncompressed_size <<= 8;
14734 uncompressed_size += start[9]; uncompressed_size <<= 8;
14735 uncompressed_size += start[10]; uncompressed_size <<= 8;
14736 uncompressed_size += start[11];
14737 start += 12;
14738 new_size -= 12;
14739 }
14740
14741 if (uncompressed_size)
14742 {
14743 if (uncompress_section_contents (& start,
14744 uncompressed_size, & new_size))
14745 num_bytes = new_size;
14746 else
14747 {
14748 error (_("Unable to decompress section %s\n"),
14749 printable_section_name (filedata, section));
14750 goto error_out;
14751 }
14752 }
14753 else
14754 start = real_start;
14755 }
14756
14757 /* If the section being dumped has relocations against it the user might
14758 be expecting these relocations to have been applied. Check for this
14759 case and issue a warning message in order to avoid confusion.
14760 FIXME: Maybe we ought to have an option that dumps a section with
14761 relocs applied ? */
14762 for (relsec = filedata->section_headers;
14763 relsec < filedata->section_headers + filedata->file_header.e_shnum;
14764 ++relsec)
14765 {
14766 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
14767 || relsec->sh_info >= filedata->file_header.e_shnum
14768 || filedata->section_headers + relsec->sh_info != section
14769 || relsec->sh_size == 0
14770 || relsec->sh_link >= filedata->file_header.e_shnum)
14771 continue;
14772
14773 printf (_(" Note: This section has relocations against it, but these have NOT been applied to this dump.\n"));
14774 break;
14775 }
14776
14777 data = start;
14778 end = start + num_bytes;
14779 some_strings_shown = false;
14780
14781 #ifdef HAVE_MBSTATE_T
14782 mbstate_t state;
14783 /* Initialise the multibyte conversion state. */
14784 memset (& state, 0, sizeof (state));
14785 #endif
14786
14787 bool continuing = false;
14788
14789 while (data < end)
14790 {
14791 while (!ISPRINT (* data))
14792 if (++ data >= end)
14793 break;
14794
14795 if (data < end)
14796 {
14797 size_t maxlen = end - data;
14798
14799 if (continuing)
14800 {
14801 printf (" ");
14802 continuing = false;
14803 }
14804 else
14805 {
14806 printf (" [%6lx] ", (unsigned long) (data - start));
14807 }
14808
14809 if (maxlen > 0)
14810 {
14811 char c = 0;
14812
14813 while (maxlen)
14814 {
14815 c = *data++;
14816
14817 if (c == 0)
14818 break;
14819
14820 /* PR 25543: Treat new-lines as string-ending characters. */
14821 if (c == '\n')
14822 {
14823 printf ("\\n\n");
14824 if (*data != 0)
14825 continuing = true;
14826 break;
14827 }
14828
14829 /* Do not print control characters directly as they can affect terminal
14830 settings. Such characters usually appear in the names generated
14831 by the assembler for local labels. */
14832 if (ISCNTRL (c))
14833 {
14834 printf ("^%c", c + 0x40);
14835 }
14836 else if (ISPRINT (c))
14837 {
14838 putchar (c);
14839 }
14840 else
14841 {
14842 size_t n;
14843 #ifdef HAVE_MBSTATE_T
14844 wchar_t w;
14845 #endif
14846 /* Let printf do the hard work of displaying multibyte characters. */
14847 printf ("%.1s", data - 1);
14848 #ifdef HAVE_MBSTATE_T
14849 /* Try to find out how many bytes made up the character that was
14850 just printed. Advance the symbol pointer past the bytes that
14851 were displayed. */
14852 n = mbrtowc (& w, (char *)(data - 1), MB_CUR_MAX, & state);
14853 #else
14854 n = 1;
14855 #endif
14856 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
14857 data += (n - 1);
14858 }
14859 }
14860
14861 if (c != '\n')
14862 putchar ('\n');
14863 }
14864 else
14865 {
14866 printf (_("<corrupt>\n"));
14867 data = end;
14868 }
14869 some_strings_shown = true;
14870 }
14871 }
14872
14873 if (! some_strings_shown)
14874 printf (_(" No strings found in this section."));
14875
14876 free (real_start);
14877
14878 putchar ('\n');
14879 return true;
14880
14881 error_out:
14882 free (real_start);
14883 return false;
14884 }
14885
14886 static bool
14887 dump_section_as_bytes (Elf_Internal_Shdr *section,
14888 Filedata *filedata,
14889 bool relocate)
14890 {
14891 Elf_Internal_Shdr * relsec;
14892 bfd_size_type bytes;
14893 bfd_size_type section_size;
14894 bfd_vma addr;
14895 unsigned char * data;
14896 unsigned char * real_start;
14897 unsigned char * start;
14898
14899 real_start = start = (unsigned char *) get_section_contents (section, filedata);
14900 if (start == NULL)
14901 /* PR 21820: Do not fail if the section was empty. */
14902 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
14903
14904 section_size = section->sh_size;
14905
14906 if (filedata->is_separate)
14907 printf (_("\nHex dump of section '%s' in linked file %s:\n"),
14908 printable_section_name (filedata, section),
14909 filedata->file_name);
14910 else
14911 printf (_("\nHex dump of section '%s':\n"),
14912 printable_section_name (filedata, section));
14913
14914 if (decompress_dumps)
14915 {
14916 dwarf_size_type new_size = section_size;
14917 dwarf_size_type uncompressed_size = 0;
14918
14919 if ((section->sh_flags & SHF_COMPRESSED) != 0)
14920 {
14921 Elf_Internal_Chdr chdr;
14922 unsigned int compression_header_size
14923 = get_compression_header (& chdr, start, section_size);
14924
14925 if (compression_header_size == 0)
14926 /* An error message will have already been generated
14927 by get_compression_header. */
14928 goto error_out;
14929
14930 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
14931 {
14932 warn (_("section '%s' has unsupported compress type: %d\n"),
14933 printable_section_name (filedata, section), chdr.ch_type);
14934 goto error_out;
14935 }
14936 uncompressed_size = chdr.ch_size;
14937 start += compression_header_size;
14938 new_size -= compression_header_size;
14939 }
14940 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
14941 {
14942 /* Read the zlib header. In this case, it should be "ZLIB"
14943 followed by the uncompressed section size, 8 bytes in
14944 big-endian order. */
14945 uncompressed_size = start[4]; uncompressed_size <<= 8;
14946 uncompressed_size += start[5]; uncompressed_size <<= 8;
14947 uncompressed_size += start[6]; uncompressed_size <<= 8;
14948 uncompressed_size += start[7]; uncompressed_size <<= 8;
14949 uncompressed_size += start[8]; uncompressed_size <<= 8;
14950 uncompressed_size += start[9]; uncompressed_size <<= 8;
14951 uncompressed_size += start[10]; uncompressed_size <<= 8;
14952 uncompressed_size += start[11];
14953 start += 12;
14954 new_size -= 12;
14955 }
14956
14957 if (uncompressed_size)
14958 {
14959 if (uncompress_section_contents (& start, uncompressed_size,
14960 & new_size))
14961 {
14962 section_size = new_size;
14963 }
14964 else
14965 {
14966 error (_("Unable to decompress section %s\n"),
14967 printable_section_name (filedata, section));
14968 /* FIXME: Print the section anyway ? */
14969 goto error_out;
14970 }
14971 }
14972 else
14973 start = real_start;
14974 }
14975
14976 if (relocate)
14977 {
14978 if (! apply_relocations (filedata, section, start, section_size, NULL, NULL))
14979 goto error_out;
14980 }
14981 else
14982 {
14983 /* If the section being dumped has relocations against it the user might
14984 be expecting these relocations to have been applied. Check for this
14985 case and issue a warning message in order to avoid confusion.
14986 FIXME: Maybe we ought to have an option that dumps a section with
14987 relocs applied ? */
14988 for (relsec = filedata->section_headers;
14989 relsec < filedata->section_headers + filedata->file_header.e_shnum;
14990 ++relsec)
14991 {
14992 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
14993 || relsec->sh_info >= filedata->file_header.e_shnum
14994 || filedata->section_headers + relsec->sh_info != section
14995 || relsec->sh_size == 0
14996 || relsec->sh_link >= filedata->file_header.e_shnum)
14997 continue;
14998
14999 printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
15000 break;
15001 }
15002 }
15003
15004 addr = section->sh_addr;
15005 bytes = section_size;
15006 data = start;
15007
15008 while (bytes)
15009 {
15010 int j;
15011 int k;
15012 int lbytes;
15013
15014 lbytes = (bytes > 16 ? 16 : bytes);
15015
15016 printf (" 0x%8.8lx ", (unsigned long) addr);
15017
15018 for (j = 0; j < 16; j++)
15019 {
15020 if (j < lbytes)
15021 printf ("%2.2x", data[j]);
15022 else
15023 printf (" ");
15024
15025 if ((j & 3) == 3)
15026 printf (" ");
15027 }
15028
15029 for (j = 0; j < lbytes; j++)
15030 {
15031 k = data[j];
15032 if (k >= ' ' && k < 0x7f)
15033 printf ("%c", k);
15034 else
15035 printf (".");
15036 }
15037
15038 putchar ('\n');
15039
15040 data += lbytes;
15041 addr += lbytes;
15042 bytes -= lbytes;
15043 }
15044
15045 free (real_start);
15046
15047 putchar ('\n');
15048 return true;
15049
15050 error_out:
15051 free (real_start);
15052 return false;
15053 }
15054
15055 #ifdef ENABLE_LIBCTF
15056 static ctf_sect_t *
15057 shdr_to_ctf_sect (ctf_sect_t *buf, Elf_Internal_Shdr *shdr, Filedata *filedata)
15058 {
15059 buf->cts_name = SECTION_NAME_PRINT (shdr);
15060 buf->cts_size = shdr->sh_size;
15061 buf->cts_entsize = shdr->sh_entsize;
15062
15063 return buf;
15064 }
15065
15066 /* Formatting callback function passed to ctf_dump. Returns either the pointer
15067 it is passed, or a pointer to newly-allocated storage, in which case
15068 dump_ctf() will free it when it no longer needs it. */
15069
15070 static char *
15071 dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
15072 char *s, void *arg)
15073 {
15074 const char *blanks = arg;
15075 char *new_s;
15076
15077 if (asprintf (&new_s, "%s%s", blanks, s) < 0)
15078 return s;
15079 return new_s;
15080 }
15081
15082 /* Dump CTF errors/warnings. */
15083 static void
15084 dump_ctf_errs (ctf_dict_t *fp)
15085 {
15086 ctf_next_t *it = NULL;
15087 char *errtext;
15088 int is_warning;
15089 int err;
15090
15091 /* Dump accumulated errors and warnings. */
15092 while ((errtext = ctf_errwarning_next (fp, &it, &is_warning, &err)) != NULL)
15093 {
15094 error (_("%s: %s"), is_warning ? _("warning"): _("error"),
15095 errtext);
15096 free (errtext);
15097 }
15098 if (err != ECTF_NEXT_END)
15099 error (_("CTF error: cannot get CTF errors: `%s'"), ctf_errmsg (err));
15100 }
15101
15102 /* Dump one CTF archive member. */
15103
15104 static int
15105 dump_ctf_archive_member (ctf_dict_t *ctf, const char *name, void *arg)
15106 {
15107 ctf_dict_t *parent = (ctf_dict_t *) arg;
15108 const char *things[] = {"Header", "Labels", "Data objects",
15109 "Function objects", "Variables", "Types", "Strings",
15110 ""};
15111 const char **thing;
15112 size_t i;
15113 int err = 0;
15114
15115 /* Only print out the name of non-default-named archive members.
15116 The name .ctf appears everywhere, even for things that aren't
15117 really archives, so printing it out is liable to be confusing.
15118
15119 The parent, if there is one, is the default-owned archive member:
15120 avoid importing it into itself. (This does no harm, but looks
15121 confusing.) */
15122
15123 if (strcmp (name, ".ctf") != 0)
15124 {
15125 printf (_("\nCTF archive member: %s:\n"), name);
15126 ctf_import (ctf, parent);
15127 }
15128
15129 for (i = 0, thing = things; *thing[0]; thing++, i++)
15130 {
15131 ctf_dump_state_t *s = NULL;
15132 char *item;
15133
15134 printf ("\n %s:\n", *thing);
15135 while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
15136 (void *) " ")) != NULL)
15137 {
15138 printf ("%s\n", item);
15139 free (item);
15140 }
15141
15142 if (ctf_errno (ctf))
15143 {
15144 error (_("Iteration failed: %s, %s\n"), *thing,
15145 ctf_errmsg (ctf_errno (ctf)));
15146 err = 1;
15147 goto out;
15148 }
15149 }
15150
15151 out:
15152 dump_ctf_errs (ctf);
15153 return err;
15154 }
15155
15156 static bool
15157 dump_section_as_ctf (Elf_Internal_Shdr * section, Filedata * filedata)
15158 {
15159 Elf_Internal_Shdr * parent_sec = NULL;
15160 Elf_Internal_Shdr * symtab_sec = NULL;
15161 Elf_Internal_Shdr * strtab_sec = NULL;
15162 void * data = NULL;
15163 void * symdata = NULL;
15164 void * strdata = NULL;
15165 void * parentdata = NULL;
15166 ctf_sect_t ctfsect, symsect, strsect, parentsect;
15167 ctf_sect_t * symsectp = NULL;
15168 ctf_sect_t * strsectp = NULL;
15169 ctf_archive_t * ctfa = NULL;
15170 ctf_archive_t * parenta = NULL, *lookparent;
15171 ctf_dict_t * parent = NULL;
15172
15173 int err;
15174 bool ret = false;
15175
15176 shdr_to_ctf_sect (&ctfsect, section, filedata);
15177 data = get_section_contents (section, filedata);
15178 ctfsect.cts_data = data;
15179
15180 if (!dump_ctf_symtab_name)
15181 dump_ctf_symtab_name = strdup (".dynsym");
15182
15183 if (!dump_ctf_strtab_name)
15184 dump_ctf_strtab_name = strdup (".dynstr");
15185
15186 if (dump_ctf_symtab_name && dump_ctf_symtab_name[0] != 0)
15187 {
15188 if ((symtab_sec = find_section (filedata, dump_ctf_symtab_name)) == NULL)
15189 {
15190 error (_("No symbol section named %s\n"), dump_ctf_symtab_name);
15191 goto fail;
15192 }
15193 if ((symdata = (void *) get_data (NULL, filedata,
15194 symtab_sec->sh_offset, 1,
15195 symtab_sec->sh_size,
15196 _("symbols"))) == NULL)
15197 goto fail;
15198 symsectp = shdr_to_ctf_sect (&symsect, symtab_sec, filedata);
15199 symsect.cts_data = symdata;
15200 }
15201
15202 if (dump_ctf_strtab_name && dump_ctf_strtab_name[0] != 0)
15203 {
15204 if ((strtab_sec = find_section (filedata, dump_ctf_strtab_name)) == NULL)
15205 {
15206 error (_("No string table section named %s\n"),
15207 dump_ctf_strtab_name);
15208 goto fail;
15209 }
15210 if ((strdata = (void *) get_data (NULL, filedata,
15211 strtab_sec->sh_offset, 1,
15212 strtab_sec->sh_size,
15213 _("strings"))) == NULL)
15214 goto fail;
15215 strsectp = shdr_to_ctf_sect (&strsect, strtab_sec, filedata);
15216 strsect.cts_data = strdata;
15217 }
15218
15219 if (dump_ctf_parent_name)
15220 {
15221 if ((parent_sec = find_section (filedata, dump_ctf_parent_name)) == NULL)
15222 {
15223 error (_("No CTF parent section named %s\n"), dump_ctf_parent_name);
15224 goto fail;
15225 }
15226 if ((parentdata = (void *) get_data (NULL, filedata,
15227 parent_sec->sh_offset, 1,
15228 parent_sec->sh_size,
15229 _("CTF parent"))) == NULL)
15230 goto fail;
15231 shdr_to_ctf_sect (&parentsect, parent_sec, filedata);
15232 parentsect.cts_data = parentdata;
15233 }
15234
15235 /* Load the CTF file and dump it. It may be a raw CTF section, or an archive:
15236 libctf papers over the difference, so we can pretend it is always an
15237 archive. Possibly open the parent as well, if one was specified. */
15238
15239 if ((ctfa = ctf_arc_bufopen (&ctfsect, symsectp, strsectp, &err)) == NULL)
15240 {
15241 dump_ctf_errs (NULL);
15242 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15243 goto fail;
15244 }
15245
15246 ctf_arc_symsect_endianness (ctfa, filedata->file_header.e_ident[EI_DATA]
15247 != ELFDATA2MSB);
15248
15249 if (parentdata)
15250 {
15251 if ((parenta = ctf_arc_bufopen (&parentsect, symsectp, strsectp,
15252 &err)) == NULL)
15253 {
15254 dump_ctf_errs (NULL);
15255 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15256 goto fail;
15257 }
15258 lookparent = parenta;
15259 }
15260 else
15261 lookparent = ctfa;
15262
15263 /* Assume that the applicable parent archive member is the default one.
15264 (This is what all known implementations are expected to do, if they
15265 put CTFs and their parents in archives together.) */
15266 if ((parent = ctf_dict_open (lookparent, NULL, &err)) == NULL)
15267 {
15268 dump_ctf_errs (NULL);
15269 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15270 goto fail;
15271 }
15272
15273 ret = true;
15274
15275 if (filedata->is_separate)
15276 printf (_("\nDump of CTF section '%s' in linked file %s:\n"),
15277 printable_section_name (filedata, section),
15278 filedata->file_name);
15279 else
15280 printf (_("\nDump of CTF section '%s':\n"),
15281 printable_section_name (filedata, section));
15282
15283 if ((err = ctf_archive_iter (ctfa, dump_ctf_archive_member, parent)) != 0)
15284 {
15285 dump_ctf_errs (NULL);
15286 error (_("CTF member open failure: %s\n"), ctf_errmsg (err));
15287 ret = false;
15288 }
15289
15290 fail:
15291 ctf_dict_close (parent);
15292 ctf_close (ctfa);
15293 ctf_close (parenta);
15294 free (parentdata);
15295 free (data);
15296 free (symdata);
15297 free (strdata);
15298 return ret;
15299 }
15300 #endif
15301
15302 static bool
15303 load_specific_debug_section (enum dwarf_section_display_enum debug,
15304 const Elf_Internal_Shdr * sec,
15305 void * data)
15306 {
15307 struct dwarf_section * section = &debug_displays [debug].section;
15308 char buf [64];
15309 Filedata * filedata = (Filedata *) data;
15310
15311 if (section->start != NULL)
15312 {
15313 /* If it is already loaded, do nothing. */
15314 if (streq (section->filename, filedata->file_name))
15315 return true;
15316 free (section->start);
15317 }
15318
15319 snprintf (buf, sizeof (buf), _("%s section data"), section->name);
15320 section->address = sec->sh_addr;
15321 section->filename = filedata->file_name;
15322 section->start = (unsigned char *) get_data (NULL, filedata,
15323 sec->sh_offset, 1,
15324 sec->sh_size, buf);
15325 if (section->start == NULL)
15326 section->size = 0;
15327 else
15328 {
15329 unsigned char *start = section->start;
15330 dwarf_size_type size = sec->sh_size;
15331 dwarf_size_type uncompressed_size = 0;
15332
15333 if ((sec->sh_flags & SHF_COMPRESSED) != 0)
15334 {
15335 Elf_Internal_Chdr chdr;
15336 unsigned int compression_header_size;
15337
15338 if (size < (is_32bit_elf
15339 ? sizeof (Elf32_External_Chdr)
15340 : sizeof (Elf64_External_Chdr)))
15341 {
15342 warn (_("compressed section %s is too small to contain a compression header\n"),
15343 section->name);
15344 return false;
15345 }
15346
15347 compression_header_size = get_compression_header (&chdr, start, size);
15348 if (compression_header_size == 0)
15349 /* An error message will have already been generated
15350 by get_compression_header. */
15351 return false;
15352
15353 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
15354 {
15355 warn (_("section '%s' has unsupported compress type: %d\n"),
15356 section->name, chdr.ch_type);
15357 return false;
15358 }
15359 uncompressed_size = chdr.ch_size;
15360 start += compression_header_size;
15361 size -= compression_header_size;
15362 }
15363 else if (size > 12 && streq ((char *) start, "ZLIB"))
15364 {
15365 /* Read the zlib header. In this case, it should be "ZLIB"
15366 followed by the uncompressed section size, 8 bytes in
15367 big-endian order. */
15368 uncompressed_size = start[4]; uncompressed_size <<= 8;
15369 uncompressed_size += start[5]; uncompressed_size <<= 8;
15370 uncompressed_size += start[6]; uncompressed_size <<= 8;
15371 uncompressed_size += start[7]; uncompressed_size <<= 8;
15372 uncompressed_size += start[8]; uncompressed_size <<= 8;
15373 uncompressed_size += start[9]; uncompressed_size <<= 8;
15374 uncompressed_size += start[10]; uncompressed_size <<= 8;
15375 uncompressed_size += start[11];
15376 start += 12;
15377 size -= 12;
15378 }
15379
15380 if (uncompressed_size)
15381 {
15382 if (uncompress_section_contents (&start, uncompressed_size,
15383 &size))
15384 {
15385 /* Free the compressed buffer, update the section buffer
15386 and the section size if uncompress is successful. */
15387 free (section->start);
15388 section->start = start;
15389 }
15390 else
15391 {
15392 error (_("Unable to decompress section %s\n"),
15393 printable_section_name (filedata, sec));
15394 return false;
15395 }
15396 }
15397
15398 section->size = size;
15399 }
15400
15401 if (section->start == NULL)
15402 return false;
15403
15404 if (debug_displays [debug].relocate)
15405 {
15406 if (! apply_relocations (filedata, sec, section->start, section->size,
15407 & section->reloc_info, & section->num_relocs))
15408 return false;
15409 }
15410 else
15411 {
15412 section->reloc_info = NULL;
15413 section->num_relocs = 0;
15414 }
15415
15416 return true;
15417 }
15418
15419 #if HAVE_LIBDEBUGINFOD
15420 /* Return a hex string representation of the build-id. */
15421 unsigned char *
15422 get_build_id (void * data)
15423 {
15424 Filedata * filedata = (Filedata *) data;
15425 Elf_Internal_Shdr * shdr;
15426 unsigned long i;
15427
15428 /* Iterate through notes to find note.gnu.build-id.
15429 FIXME: Only the first note in any note section is examined. */
15430 for (i = 0, shdr = filedata->section_headers;
15431 i < filedata->file_header.e_shnum && shdr != NULL;
15432 i++, shdr++)
15433 {
15434 if (shdr->sh_type != SHT_NOTE)
15435 continue;
15436
15437 char * next;
15438 char * end;
15439 size_t data_remaining;
15440 size_t min_notesz;
15441 Elf_External_Note * enote;
15442 Elf_Internal_Note inote;
15443
15444 bfd_vma offset = shdr->sh_offset;
15445 bfd_vma align = shdr->sh_addralign;
15446 bfd_vma length = shdr->sh_size;
15447
15448 enote = (Elf_External_Note *) get_section_contents (shdr, filedata);
15449 if (enote == NULL)
15450 continue;
15451
15452 if (align < 4)
15453 align = 4;
15454 else if (align != 4 && align != 8)
15455 {
15456 free (enote);
15457 continue;
15458 }
15459
15460 end = (char *) enote + length;
15461 data_remaining = end - (char *) enote;
15462
15463 if (!is_ia64_vms (filedata))
15464 {
15465 min_notesz = offsetof (Elf_External_Note, name);
15466 if (data_remaining < min_notesz)
15467 {
15468 warn (_("\
15469 malformed note encountered in section %s whilst scanning for build-id note\n"),
15470 printable_section_name (filedata, shdr));
15471 free (enote);
15472 continue;
15473 }
15474 data_remaining -= min_notesz;
15475
15476 inote.type = BYTE_GET (enote->type);
15477 inote.namesz = BYTE_GET (enote->namesz);
15478 inote.namedata = enote->name;
15479 inote.descsz = BYTE_GET (enote->descsz);
15480 inote.descdata = ((char *) enote
15481 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
15482 inote.descpos = offset + (inote.descdata - (char *) enote);
15483 next = ((char *) enote
15484 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
15485 }
15486 else
15487 {
15488 Elf64_External_VMS_Note *vms_enote;
15489
15490 /* PR binutils/15191
15491 Make sure that there is enough data to read. */
15492 min_notesz = offsetof (Elf64_External_VMS_Note, name);
15493 if (data_remaining < min_notesz)
15494 {
15495 warn (_("\
15496 malformed note encountered in section %s whilst scanning for build-id note\n"),
15497 printable_section_name (filedata, shdr));
15498 free (enote);
15499 continue;
15500 }
15501 data_remaining -= min_notesz;
15502
15503 vms_enote = (Elf64_External_VMS_Note *) enote;
15504 inote.type = BYTE_GET (vms_enote->type);
15505 inote.namesz = BYTE_GET (vms_enote->namesz);
15506 inote.namedata = vms_enote->name;
15507 inote.descsz = BYTE_GET (vms_enote->descsz);
15508 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
15509 inote.descpos = offset + (inote.descdata - (char *) enote);
15510 next = inote.descdata + align_power (inote.descsz, 3);
15511 }
15512
15513 /* Skip malformed notes. */
15514 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
15515 || (size_t) (inote.descdata - inote.namedata) > data_remaining
15516 || (size_t) (next - inote.descdata) < inote.descsz
15517 || ((size_t) (next - inote.descdata)
15518 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
15519 {
15520 warn (_("\
15521 malformed note encountered in section %s whilst scanning for build-id note\n"),
15522 printable_section_name (filedata, shdr));
15523 free (enote);
15524 continue;
15525 }
15526
15527 /* Check if this is the build-id note. If so then convert the build-id
15528 bytes to a hex string. */
15529 if (inote.namesz > 0
15530 && startswith (inote.namedata, "GNU")
15531 && inote.type == NT_GNU_BUILD_ID)
15532 {
15533 unsigned long j;
15534 char * build_id;
15535
15536 build_id = malloc (inote.descsz * 2 + 1);
15537 if (build_id == NULL)
15538 {
15539 free (enote);
15540 return NULL;
15541 }
15542
15543 for (j = 0; j < inote.descsz; ++j)
15544 sprintf (build_id + (j * 2), "%02x", inote.descdata[j] & 0xff);
15545 build_id[inote.descsz * 2] = '\0';
15546 free (enote);
15547
15548 return (unsigned char *) build_id;
15549 }
15550 free (enote);
15551 }
15552
15553 return NULL;
15554 }
15555 #endif /* HAVE_LIBDEBUGINFOD */
15556
15557 /* If this is not NULL, load_debug_section will only look for sections
15558 within the list of sections given here. */
15559 static unsigned int * section_subset = NULL;
15560
15561 bool
15562 load_debug_section (enum dwarf_section_display_enum debug, void * data)
15563 {
15564 struct dwarf_section * section = &debug_displays [debug].section;
15565 Elf_Internal_Shdr * sec;
15566 Filedata * filedata = (Filedata *) data;
15567
15568 /* Without section headers we cannot find any sections. */
15569 if (filedata->section_headers == NULL)
15570 return false;
15571
15572 if (filedata->string_table == NULL
15573 && filedata->file_header.e_shstrndx != SHN_UNDEF
15574 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
15575 {
15576 Elf_Internal_Shdr * strs;
15577
15578 /* Read in the string table, so that we have section names to scan. */
15579 strs = filedata->section_headers + filedata->file_header.e_shstrndx;
15580
15581 if (strs != NULL && strs->sh_size != 0)
15582 {
15583 filedata->string_table
15584 = (char *) get_data (NULL, filedata, strs->sh_offset,
15585 1, strs->sh_size, _("string table"));
15586
15587 filedata->string_table_length
15588 = filedata->string_table != NULL ? strs->sh_size : 0;
15589 }
15590 }
15591
15592 /* Locate the debug section. */
15593 sec = find_section_in_set (filedata, section->uncompressed_name, section_subset);
15594 if (sec != NULL)
15595 section->name = section->uncompressed_name;
15596 else
15597 {
15598 sec = find_section_in_set (filedata, section->compressed_name, section_subset);
15599 if (sec != NULL)
15600 section->name = section->compressed_name;
15601 }
15602 if (sec == NULL)
15603 return false;
15604
15605 /* If we're loading from a subset of sections, and we've loaded
15606 a section matching this name before, it's likely that it's a
15607 different one. */
15608 if (section_subset != NULL)
15609 free_debug_section (debug);
15610
15611 return load_specific_debug_section (debug, sec, data);
15612 }
15613
15614 void
15615 free_debug_section (enum dwarf_section_display_enum debug)
15616 {
15617 struct dwarf_section * section = &debug_displays [debug].section;
15618
15619 if (section->start == NULL)
15620 return;
15621
15622 free ((char *) section->start);
15623 section->start = NULL;
15624 section->address = 0;
15625 section->size = 0;
15626
15627 free (section->reloc_info);
15628 section->reloc_info = NULL;
15629 section->num_relocs = 0;
15630 }
15631
15632 static bool
15633 display_debug_section (int shndx, Elf_Internal_Shdr * section, Filedata * filedata)
15634 {
15635 char * name = SECTION_NAME_VALID (section) ? SECTION_NAME (section) : "";
15636 const char * print_name = printable_section_name (filedata, section);
15637 bfd_size_type length;
15638 bool result = true;
15639 int i;
15640
15641 length = section->sh_size;
15642 if (length == 0)
15643 {
15644 printf (_("\nSection '%s' has no debugging data.\n"), print_name);
15645 return true;
15646 }
15647 if (section->sh_type == SHT_NOBITS)
15648 {
15649 /* There is no point in dumping the contents of a debugging section
15650 which has the NOBITS type - the bits in the file will be random.
15651 This can happen when a file containing a .eh_frame section is
15652 stripped with the --only-keep-debug command line option. */
15653 printf (_("section '%s' has the NOBITS type - its contents are unreliable.\n"),
15654 print_name);
15655 return false;
15656 }
15657
15658 if (startswith (name, ".gnu.linkonce.wi."))
15659 name = ".debug_info";
15660
15661 /* See if we know how to display the contents of this section. */
15662 for (i = 0; i < max; i++)
15663 {
15664 enum dwarf_section_display_enum id = (enum dwarf_section_display_enum) i;
15665 struct dwarf_section_display * display = debug_displays + i;
15666 struct dwarf_section * sec = & display->section;
15667
15668 if (streq (sec->uncompressed_name, name)
15669 || (id == line && startswith (name, ".debug_line."))
15670 || streq (sec->compressed_name, name))
15671 {
15672 bool secondary = (section != find_section (filedata, name));
15673
15674 if (secondary)
15675 free_debug_section (id);
15676
15677 if (i == line && startswith (name, ".debug_line."))
15678 sec->name = name;
15679 else if (streq (sec->uncompressed_name, name))
15680 sec->name = sec->uncompressed_name;
15681 else
15682 sec->name = sec->compressed_name;
15683
15684 if (load_specific_debug_section (id, section, filedata))
15685 {
15686 /* If this debug section is part of a CU/TU set in a .dwp file,
15687 restrict load_debug_section to the sections in that set. */
15688 section_subset = find_cu_tu_set (filedata, shndx);
15689
15690 result &= display->display (sec, filedata);
15691
15692 section_subset = NULL;
15693
15694 if (secondary || (id != info && id != abbrev && id != debug_addr))
15695 free_debug_section (id);
15696 }
15697 break;
15698 }
15699 }
15700
15701 if (i == max)
15702 {
15703 printf (_("Unrecognized debug section: %s\n"), print_name);
15704 result = false;
15705 }
15706
15707 return result;
15708 }
15709
15710 /* Set DUMP_SECTS for all sections where dumps were requested
15711 based on section name. */
15712
15713 static void
15714 initialise_dumps_byname (Filedata * filedata)
15715 {
15716 struct dump_list_entry * cur;
15717
15718 for (cur = dump_sects_byname; cur; cur = cur->next)
15719 {
15720 unsigned int i;
15721 bool any = false;
15722
15723 for (i = 0; i < filedata->file_header.e_shnum; i++)
15724 if (SECTION_NAME_VALID (filedata->section_headers + i)
15725 && streq (SECTION_NAME (filedata->section_headers + i), cur->name))
15726 {
15727 request_dump_bynumber (&filedata->dump, i, cur->type);
15728 any = true;
15729 }
15730
15731 if (!any && !filedata->is_separate)
15732 warn (_("Section '%s' was not dumped because it does not exist\n"),
15733 cur->name);
15734 }
15735 }
15736
15737 static bool
15738 process_section_contents (Filedata * filedata)
15739 {
15740 Elf_Internal_Shdr * section;
15741 unsigned int i;
15742 bool res = true;
15743
15744 if (! do_dump)
15745 return true;
15746
15747 initialise_dumps_byname (filedata);
15748
15749 for (i = 0, section = filedata->section_headers;
15750 i < filedata->file_header.e_shnum && i < filedata->dump.num_dump_sects;
15751 i++, section++)
15752 {
15753 dump_type dump = filedata->dump.dump_sects[i];
15754
15755 if (filedata->is_separate && ! process_links)
15756 dump &= DEBUG_DUMP;
15757
15758 #ifdef SUPPORT_DISASSEMBLY
15759 if (dump & DISASS_DUMP)
15760 {
15761 if (! disassemble_section (section, filedata))
15762 res = false;
15763 }
15764 #endif
15765 if (dump & HEX_DUMP)
15766 {
15767 if (! dump_section_as_bytes (section, filedata, false))
15768 res = false;
15769 }
15770
15771 if (dump & RELOC_DUMP)
15772 {
15773 if (! dump_section_as_bytes (section, filedata, true))
15774 res = false;
15775 }
15776
15777 if (dump & STRING_DUMP)
15778 {
15779 if (! dump_section_as_strings (section, filedata))
15780 res = false;
15781 }
15782
15783 if (dump & DEBUG_DUMP)
15784 {
15785 if (! display_debug_section (i, section, filedata))
15786 res = false;
15787 }
15788
15789 #ifdef ENABLE_LIBCTF
15790 if (dump & CTF_DUMP)
15791 {
15792 if (! dump_section_as_ctf (section, filedata))
15793 res = false;
15794 }
15795 #endif
15796 }
15797
15798 if (! filedata->is_separate)
15799 {
15800 /* Check to see if the user requested a
15801 dump of a section that does not exist. */
15802 for (; i < filedata->dump.num_dump_sects; i++)
15803 if (filedata->dump.dump_sects[i])
15804 {
15805 warn (_("Section %d was not dumped because it does not exist!\n"), i);
15806 res = false;
15807 }
15808 }
15809
15810 return res;
15811 }
15812
15813 static void
15814 process_mips_fpe_exception (int mask)
15815 {
15816 if (mask)
15817 {
15818 bool first = true;
15819
15820 if (mask & OEX_FPU_INEX)
15821 fputs ("INEX", stdout), first = false;
15822 if (mask & OEX_FPU_UFLO)
15823 printf ("%sUFLO", first ? "" : "|"), first = false;
15824 if (mask & OEX_FPU_OFLO)
15825 printf ("%sOFLO", first ? "" : "|"), first = false;
15826 if (mask & OEX_FPU_DIV0)
15827 printf ("%sDIV0", first ? "" : "|"), first = false;
15828 if (mask & OEX_FPU_INVAL)
15829 printf ("%sINVAL", first ? "" : "|");
15830 }
15831 else
15832 fputs ("0", stdout);
15833 }
15834
15835 /* Display's the value of TAG at location P. If TAG is
15836 greater than 0 it is assumed to be an unknown tag, and
15837 a message is printed to this effect. Otherwise it is
15838 assumed that a message has already been printed.
15839
15840 If the bottom bit of TAG is set it assumed to have a
15841 string value, otherwise it is assumed to have an integer
15842 value.
15843
15844 Returns an updated P pointing to the first unread byte
15845 beyond the end of TAG's value.
15846
15847 Reads at or beyond END will not be made. */
15848
15849 static unsigned char *
15850 display_tag_value (signed int tag,
15851 unsigned char * p,
15852 const unsigned char * const end)
15853 {
15854 unsigned long val;
15855
15856 if (tag > 0)
15857 printf (" Tag_unknown_%d: ", tag);
15858
15859 if (p >= end)
15860 {
15861 warn (_("<corrupt tag>\n"));
15862 }
15863 else if (tag & 1)
15864 {
15865 /* PR 17531 file: 027-19978-0.004. */
15866 size_t maxlen = (end - p) - 1;
15867
15868 putchar ('"');
15869 if (maxlen > 0)
15870 {
15871 print_symbol ((int) maxlen, (const char *) p);
15872 p += strnlen ((char *) p, maxlen) + 1;
15873 }
15874 else
15875 {
15876 printf (_("<corrupt string tag>"));
15877 p = (unsigned char *) end;
15878 }
15879 printf ("\"\n");
15880 }
15881 else
15882 {
15883 READ_ULEB (val, p, end);
15884 printf ("%ld (0x%lx)\n", val, val);
15885 }
15886
15887 assert (p <= end);
15888 return p;
15889 }
15890
15891 /* ARC ABI attributes section. */
15892
15893 static unsigned char *
15894 display_arc_attribute (unsigned char * p,
15895 const unsigned char * const end)
15896 {
15897 unsigned int tag;
15898 unsigned int val;
15899
15900 READ_ULEB (tag, p, end);
15901
15902 switch (tag)
15903 {
15904 case Tag_ARC_PCS_config:
15905 READ_ULEB (val, p, end);
15906 printf (" Tag_ARC_PCS_config: ");
15907 switch (val)
15908 {
15909 case 0:
15910 printf (_("Absent/Non standard\n"));
15911 break;
15912 case 1:
15913 printf (_("Bare metal/mwdt\n"));
15914 break;
15915 case 2:
15916 printf (_("Bare metal/newlib\n"));
15917 break;
15918 case 3:
15919 printf (_("Linux/uclibc\n"));
15920 break;
15921 case 4:
15922 printf (_("Linux/glibc\n"));
15923 break;
15924 default:
15925 printf (_("Unknown\n"));
15926 break;
15927 }
15928 break;
15929
15930 case Tag_ARC_CPU_base:
15931 READ_ULEB (val, p, end);
15932 printf (" Tag_ARC_CPU_base: ");
15933 switch (val)
15934 {
15935 default:
15936 case TAG_CPU_NONE:
15937 printf (_("Absent\n"));
15938 break;
15939 case TAG_CPU_ARC6xx:
15940 printf ("ARC6xx\n");
15941 break;
15942 case TAG_CPU_ARC7xx:
15943 printf ("ARC7xx\n");
15944 break;
15945 case TAG_CPU_ARCEM:
15946 printf ("ARCEM\n");
15947 break;
15948 case TAG_CPU_ARCHS:
15949 printf ("ARCHS\n");
15950 break;
15951 }
15952 break;
15953
15954 case Tag_ARC_CPU_variation:
15955 READ_ULEB (val, p, end);
15956 printf (" Tag_ARC_CPU_variation: ");
15957 switch (val)
15958 {
15959 default:
15960 if (val > 0 && val < 16)
15961 printf ("Core%d\n", val);
15962 else
15963 printf ("Unknown\n");
15964 break;
15965
15966 case 0:
15967 printf (_("Absent\n"));
15968 break;
15969 }
15970 break;
15971
15972 case Tag_ARC_CPU_name:
15973 printf (" Tag_ARC_CPU_name: ");
15974 p = display_tag_value (-1, p, end);
15975 break;
15976
15977 case Tag_ARC_ABI_rf16:
15978 READ_ULEB (val, p, end);
15979 printf (" Tag_ARC_ABI_rf16: %s\n", val ? _("yes") : _("no"));
15980 break;
15981
15982 case Tag_ARC_ABI_osver:
15983 READ_ULEB (val, p, end);
15984 printf (" Tag_ARC_ABI_osver: v%d\n", val);
15985 break;
15986
15987 case Tag_ARC_ABI_pic:
15988 case Tag_ARC_ABI_sda:
15989 READ_ULEB (val, p, end);
15990 printf (tag == Tag_ARC_ABI_sda ? " Tag_ARC_ABI_sda: "
15991 : " Tag_ARC_ABI_pic: ");
15992 switch (val)
15993 {
15994 case 0:
15995 printf (_("Absent\n"));
15996 break;
15997 case 1:
15998 printf ("MWDT\n");
15999 break;
16000 case 2:
16001 printf ("GNU\n");
16002 break;
16003 default:
16004 printf (_("Unknown\n"));
16005 break;
16006 }
16007 break;
16008
16009 case Tag_ARC_ABI_tls:
16010 READ_ULEB (val, p, end);
16011 printf (" Tag_ARC_ABI_tls: %s\n", val ? "r25": "none");
16012 break;
16013
16014 case Tag_ARC_ABI_enumsize:
16015 READ_ULEB (val, p, end);
16016 printf (" Tag_ARC_ABI_enumsize: %s\n", val ? _("default") :
16017 _("smallest"));
16018 break;
16019
16020 case Tag_ARC_ABI_exceptions:
16021 READ_ULEB (val, p, end);
16022 printf (" Tag_ARC_ABI_exceptions: %s\n", val ? _("OPTFP")
16023 : _("default"));
16024 break;
16025
16026 case Tag_ARC_ABI_double_size:
16027 READ_ULEB (val, p, end);
16028 printf (" Tag_ARC_ABI_double_size: %d\n", val);
16029 break;
16030
16031 case Tag_ARC_ISA_config:
16032 printf (" Tag_ARC_ISA_config: ");
16033 p = display_tag_value (-1, p, end);
16034 break;
16035
16036 case Tag_ARC_ISA_apex:
16037 printf (" Tag_ARC_ISA_apex: ");
16038 p = display_tag_value (-1, p, end);
16039 break;
16040
16041 case Tag_ARC_ISA_mpy_option:
16042 READ_ULEB (val, p, end);
16043 printf (" Tag_ARC_ISA_mpy_option: %d\n", val);
16044 break;
16045
16046 case Tag_ARC_ATR_version:
16047 READ_ULEB (val, p, end);
16048 printf (" Tag_ARC_ATR_version: %d\n", val);
16049 break;
16050
16051 default:
16052 return display_tag_value (tag & 1, p, end);
16053 }
16054
16055 return p;
16056 }
16057
16058 /* ARM EABI attributes section. */
16059 typedef struct
16060 {
16061 unsigned int tag;
16062 const char * name;
16063 /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup. */
16064 unsigned int type;
16065 const char *const *table;
16066 } arm_attr_public_tag;
16067
16068 static const char *const arm_attr_tag_CPU_arch[] =
16069 {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
16070 "v6K", "v7", "v6-M", "v6S-M", "v7E-M", "v8", "v8-R", "v8-M.baseline",
16071 "v8-M.mainline", "", "", "", "v8.1-M.mainline"};
16072 static const char *const arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
16073 static const char *const arm_attr_tag_THUMB_ISA_use[] =
16074 {"No", "Thumb-1", "Thumb-2", "Yes"};
16075 static const char *const arm_attr_tag_FP_arch[] =
16076 {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16", "VFPv4", "VFPv4-D16",
16077 "FP for ARMv8", "FPv5/FP-D16 for ARMv8"};
16078 static const char *const arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
16079 static const char *const arm_attr_tag_Advanced_SIMD_arch[] =
16080 {"No", "NEONv1", "NEONv1 with Fused-MAC", "NEON for ARMv8",
16081 "NEON for ARMv8.1"};
16082 static const char *const arm_attr_tag_PCS_config[] =
16083 {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
16084 "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
16085 static const char *const arm_attr_tag_ABI_PCS_R9_use[] =
16086 {"V6", "SB", "TLS", "Unused"};
16087 static const char *const arm_attr_tag_ABI_PCS_RW_data[] =
16088 {"Absolute", "PC-relative", "SB-relative", "None"};
16089 static const char *const arm_attr_tag_ABI_PCS_RO_data[] =
16090 {"Absolute", "PC-relative", "None"};
16091 static const char *const arm_attr_tag_ABI_PCS_GOT_use[] =
16092 {"None", "direct", "GOT-indirect"};
16093 static const char *const arm_attr_tag_ABI_PCS_wchar_t[] =
16094 {"None", "??? 1", "2", "??? 3", "4"};
16095 static const char *const arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
16096 static const char *const arm_attr_tag_ABI_FP_denormal[] =
16097 {"Unused", "Needed", "Sign only"};
16098 static const char *const arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
16099 static const char *const arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
16100 static const char *const arm_attr_tag_ABI_FP_number_model[] =
16101 {"Unused", "Finite", "RTABI", "IEEE 754"};
16102 static const char *const arm_attr_tag_ABI_enum_size[] =
16103 {"Unused", "small", "int", "forced to int"};
16104 static const char *const arm_attr_tag_ABI_HardFP_use[] =
16105 {"As Tag_FP_arch", "SP only", "Reserved", "Deprecated"};
16106 static const char *const arm_attr_tag_ABI_VFP_args[] =
16107 {"AAPCS", "VFP registers", "custom", "compatible"};
16108 static const char *const arm_attr_tag_ABI_WMMX_args[] =
16109 {"AAPCS", "WMMX registers", "custom"};
16110 static const char *const arm_attr_tag_ABI_optimization_goals[] =
16111 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
16112 "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
16113 static const char *const arm_attr_tag_ABI_FP_optimization_goals[] =
16114 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
16115 "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
16116 static const char *const arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
16117 static const char *const arm_attr_tag_FP_HP_extension[] =
16118 {"Not Allowed", "Allowed"};
16119 static const char *const arm_attr_tag_ABI_FP_16bit_format[] =
16120 {"None", "IEEE 754", "Alternative Format"};
16121 static const char *const arm_attr_tag_DSP_extension[] =
16122 {"Follow architecture", "Allowed"};
16123 static const char *const arm_attr_tag_MPextension_use[] =
16124 {"Not Allowed", "Allowed"};
16125 static const char *const arm_attr_tag_DIV_use[] =
16126 {"Allowed in Thumb-ISA, v7-R or v7-M", "Not allowed",
16127 "Allowed in v7-A with integer division extension"};
16128 static const char *const arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
16129 static const char *const arm_attr_tag_Virtualization_use[] =
16130 {"Not Allowed", "TrustZone", "Virtualization Extensions",
16131 "TrustZone and Virtualization Extensions"};
16132 static const char *const arm_attr_tag_MPextension_use_legacy[] =
16133 {"Not Allowed", "Allowed"};
16134
16135 static const char *const arm_attr_tag_MVE_arch[] =
16136 {"No MVE", "MVE Integer only", "MVE Integer and FP"};
16137
16138 #define LOOKUP(id, name) \
16139 {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
16140 static arm_attr_public_tag arm_attr_public_tags[] =
16141 {
16142 {4, "CPU_raw_name", 1, NULL},
16143 {5, "CPU_name", 1, NULL},
16144 LOOKUP(6, CPU_arch),
16145 {7, "CPU_arch_profile", 0, NULL},
16146 LOOKUP(8, ARM_ISA_use),
16147 LOOKUP(9, THUMB_ISA_use),
16148 LOOKUP(10, FP_arch),
16149 LOOKUP(11, WMMX_arch),
16150 LOOKUP(12, Advanced_SIMD_arch),
16151 LOOKUP(13, PCS_config),
16152 LOOKUP(14, ABI_PCS_R9_use),
16153 LOOKUP(15, ABI_PCS_RW_data),
16154 LOOKUP(16, ABI_PCS_RO_data),
16155 LOOKUP(17, ABI_PCS_GOT_use),
16156 LOOKUP(18, ABI_PCS_wchar_t),
16157 LOOKUP(19, ABI_FP_rounding),
16158 LOOKUP(20, ABI_FP_denormal),
16159 LOOKUP(21, ABI_FP_exceptions),
16160 LOOKUP(22, ABI_FP_user_exceptions),
16161 LOOKUP(23, ABI_FP_number_model),
16162 {24, "ABI_align_needed", 0, NULL},
16163 {25, "ABI_align_preserved", 0, NULL},
16164 LOOKUP(26, ABI_enum_size),
16165 LOOKUP(27, ABI_HardFP_use),
16166 LOOKUP(28, ABI_VFP_args),
16167 LOOKUP(29, ABI_WMMX_args),
16168 LOOKUP(30, ABI_optimization_goals),
16169 LOOKUP(31, ABI_FP_optimization_goals),
16170 {32, "compatibility", 0, NULL},
16171 LOOKUP(34, CPU_unaligned_access),
16172 LOOKUP(36, FP_HP_extension),
16173 LOOKUP(38, ABI_FP_16bit_format),
16174 LOOKUP(42, MPextension_use),
16175 LOOKUP(44, DIV_use),
16176 LOOKUP(46, DSP_extension),
16177 LOOKUP(48, MVE_arch),
16178 {64, "nodefaults", 0, NULL},
16179 {65, "also_compatible_with", 0, NULL},
16180 LOOKUP(66, T2EE_use),
16181 {67, "conformance", 1, NULL},
16182 LOOKUP(68, Virtualization_use),
16183 LOOKUP(70, MPextension_use_legacy)
16184 };
16185 #undef LOOKUP
16186
16187 static unsigned char *
16188 display_arm_attribute (unsigned char * p,
16189 const unsigned char * const end)
16190 {
16191 unsigned int tag;
16192 unsigned int val;
16193 arm_attr_public_tag * attr;
16194 unsigned i;
16195 unsigned int type;
16196
16197 READ_ULEB (tag, p, end);
16198 attr = NULL;
16199 for (i = 0; i < ARRAY_SIZE (arm_attr_public_tags); i++)
16200 {
16201 if (arm_attr_public_tags[i].tag == tag)
16202 {
16203 attr = &arm_attr_public_tags[i];
16204 break;
16205 }
16206 }
16207
16208 if (attr)
16209 {
16210 printf (" Tag_%s: ", attr->name);
16211 switch (attr->type)
16212 {
16213 case 0:
16214 switch (tag)
16215 {
16216 case 7: /* Tag_CPU_arch_profile. */
16217 READ_ULEB (val, p, end);
16218 switch (val)
16219 {
16220 case 0: printf (_("None\n")); break;
16221 case 'A': printf (_("Application\n")); break;
16222 case 'R': printf (_("Realtime\n")); break;
16223 case 'M': printf (_("Microcontroller\n")); break;
16224 case 'S': printf (_("Application or Realtime\n")); break;
16225 default: printf ("??? (%d)\n", val); break;
16226 }
16227 break;
16228
16229 case 24: /* Tag_align_needed. */
16230 READ_ULEB (val, p, end);
16231 switch (val)
16232 {
16233 case 0: printf (_("None\n")); break;
16234 case 1: printf (_("8-byte\n")); break;
16235 case 2: printf (_("4-byte\n")); break;
16236 case 3: printf ("??? 3\n"); break;
16237 default:
16238 if (val <= 12)
16239 printf (_("8-byte and up to %d-byte extended\n"),
16240 1 << val);
16241 else
16242 printf ("??? (%d)\n", val);
16243 break;
16244 }
16245 break;
16246
16247 case 25: /* Tag_align_preserved. */
16248 READ_ULEB (val, p, end);
16249 switch (val)
16250 {
16251 case 0: printf (_("None\n")); break;
16252 case 1: printf (_("8-byte, except leaf SP\n")); break;
16253 case 2: printf (_("8-byte\n")); break;
16254 case 3: printf ("??? 3\n"); break;
16255 default:
16256 if (val <= 12)
16257 printf (_("8-byte and up to %d-byte extended\n"),
16258 1 << val);
16259 else
16260 printf ("??? (%d)\n", val);
16261 break;
16262 }
16263 break;
16264
16265 case 32: /* Tag_compatibility. */
16266 {
16267 READ_ULEB (val, p, end);
16268 printf (_("flag = %d, vendor = "), val);
16269 if (p < end - 1)
16270 {
16271 size_t maxlen = (end - p) - 1;
16272
16273 print_symbol ((int) maxlen, (const char *) p);
16274 p += strnlen ((char *) p, maxlen) + 1;
16275 }
16276 else
16277 {
16278 printf (_("<corrupt>"));
16279 p = (unsigned char *) end;
16280 }
16281 putchar ('\n');
16282 }
16283 break;
16284
16285 case 64: /* Tag_nodefaults. */
16286 /* PR 17531: file: 001-505008-0.01. */
16287 if (p < end)
16288 p++;
16289 printf (_("True\n"));
16290 break;
16291
16292 case 65: /* Tag_also_compatible_with. */
16293 READ_ULEB (val, p, end);
16294 if (val == 6 /* Tag_CPU_arch. */)
16295 {
16296 READ_ULEB (val, p, end);
16297 if ((unsigned int) val >= ARRAY_SIZE (arm_attr_tag_CPU_arch))
16298 printf ("??? (%d)\n", val);
16299 else
16300 printf ("%s\n", arm_attr_tag_CPU_arch[val]);
16301 }
16302 else
16303 printf ("???\n");
16304 while (p < end && *(p++) != '\0' /* NUL terminator. */)
16305 ;
16306 break;
16307
16308 default:
16309 printf (_("<unknown: %d>\n"), tag);
16310 break;
16311 }
16312 return p;
16313
16314 case 1:
16315 return display_tag_value (-1, p, end);
16316 case 2:
16317 return display_tag_value (0, p, end);
16318
16319 default:
16320 assert (attr->type & 0x80);
16321 READ_ULEB (val, p, end);
16322 type = attr->type & 0x7f;
16323 if (val >= type)
16324 printf ("??? (%d)\n", val);
16325 else
16326 printf ("%s\n", attr->table[val]);
16327 return p;
16328 }
16329 }
16330
16331 return display_tag_value (tag, p, end);
16332 }
16333
16334 static unsigned char *
16335 display_gnu_attribute (unsigned char * p,
16336 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const),
16337 const unsigned char * const end)
16338 {
16339 unsigned int tag;
16340 unsigned int val;
16341
16342 READ_ULEB (tag, p, end);
16343
16344 /* Tag_compatibility is the only generic GNU attribute defined at
16345 present. */
16346 if (tag == 32)
16347 {
16348 READ_ULEB (val, p, end);
16349
16350 printf (_("flag = %d, vendor = "), val);
16351 if (p == end)
16352 {
16353 printf (_("<corrupt>\n"));
16354 warn (_("corrupt vendor attribute\n"));
16355 }
16356 else
16357 {
16358 if (p < end - 1)
16359 {
16360 size_t maxlen = (end - p) - 1;
16361
16362 print_symbol ((int) maxlen, (const char *) p);
16363 p += strnlen ((char *) p, maxlen) + 1;
16364 }
16365 else
16366 {
16367 printf (_("<corrupt>"));
16368 p = (unsigned char *) end;
16369 }
16370 putchar ('\n');
16371 }
16372 return p;
16373 }
16374
16375 if ((tag & 2) == 0 && display_proc_gnu_attribute)
16376 return display_proc_gnu_attribute (p, tag, end);
16377
16378 return display_tag_value (tag, p, end);
16379 }
16380
16381 static unsigned char *
16382 display_m68k_gnu_attribute (unsigned char * p,
16383 unsigned int tag,
16384 const unsigned char * const end)
16385 {
16386 unsigned int val;
16387
16388 if (tag == Tag_GNU_M68K_ABI_FP)
16389 {
16390 printf (" Tag_GNU_M68K_ABI_FP: ");
16391 if (p == end)
16392 {
16393 printf (_("<corrupt>\n"));
16394 return p;
16395 }
16396 READ_ULEB (val, p, end);
16397
16398 if (val > 3)
16399 printf ("(%#x), ", val);
16400
16401 switch (val & 3)
16402 {
16403 case 0:
16404 printf (_("unspecified hard/soft float\n"));
16405 break;
16406 case 1:
16407 printf (_("hard float\n"));
16408 break;
16409 case 2:
16410 printf (_("soft float\n"));
16411 break;
16412 }
16413 return p;
16414 }
16415
16416 return display_tag_value (tag & 1, p, end);
16417 }
16418
16419 static unsigned char *
16420 display_power_gnu_attribute (unsigned char * p,
16421 unsigned int tag,
16422 const unsigned char * const end)
16423 {
16424 unsigned int val;
16425
16426 if (tag == Tag_GNU_Power_ABI_FP)
16427 {
16428 printf (" Tag_GNU_Power_ABI_FP: ");
16429 if (p == end)
16430 {
16431 printf (_("<corrupt>\n"));
16432 return p;
16433 }
16434 READ_ULEB (val, p, end);
16435
16436 if (val > 15)
16437 printf ("(%#x), ", val);
16438
16439 switch (val & 3)
16440 {
16441 case 0:
16442 printf (_("unspecified hard/soft float, "));
16443 break;
16444 case 1:
16445 printf (_("hard float, "));
16446 break;
16447 case 2:
16448 printf (_("soft float, "));
16449 break;
16450 case 3:
16451 printf (_("single-precision hard float, "));
16452 break;
16453 }
16454
16455 switch (val & 0xC)
16456 {
16457 case 0:
16458 printf (_("unspecified long double\n"));
16459 break;
16460 case 4:
16461 printf (_("128-bit IBM long double\n"));
16462 break;
16463 case 8:
16464 printf (_("64-bit long double\n"));
16465 break;
16466 case 12:
16467 printf (_("128-bit IEEE long double\n"));
16468 break;
16469 }
16470 return p;
16471 }
16472
16473 if (tag == Tag_GNU_Power_ABI_Vector)
16474 {
16475 printf (" Tag_GNU_Power_ABI_Vector: ");
16476 if (p == end)
16477 {
16478 printf (_("<corrupt>\n"));
16479 return p;
16480 }
16481 READ_ULEB (val, p, end);
16482
16483 if (val > 3)
16484 printf ("(%#x), ", val);
16485
16486 switch (val & 3)
16487 {
16488 case 0:
16489 printf (_("unspecified\n"));
16490 break;
16491 case 1:
16492 printf (_("generic\n"));
16493 break;
16494 case 2:
16495 printf ("AltiVec\n");
16496 break;
16497 case 3:
16498 printf ("SPE\n");
16499 break;
16500 }
16501 return p;
16502 }
16503
16504 if (tag == Tag_GNU_Power_ABI_Struct_Return)
16505 {
16506 printf (" Tag_GNU_Power_ABI_Struct_Return: ");
16507 if (p == end)
16508 {
16509 printf (_("<corrupt>\n"));
16510 return p;
16511 }
16512 READ_ULEB (val, p, end);
16513
16514 if (val > 2)
16515 printf ("(%#x), ", val);
16516
16517 switch (val & 3)
16518 {
16519 case 0:
16520 printf (_("unspecified\n"));
16521 break;
16522 case 1:
16523 printf ("r3/r4\n");
16524 break;
16525 case 2:
16526 printf (_("memory\n"));
16527 break;
16528 case 3:
16529 printf ("???\n");
16530 break;
16531 }
16532 return p;
16533 }
16534
16535 return display_tag_value (tag & 1, p, end);
16536 }
16537
16538 static unsigned char *
16539 display_s390_gnu_attribute (unsigned char * p,
16540 unsigned int tag,
16541 const unsigned char * const end)
16542 {
16543 unsigned int val;
16544
16545 if (tag == Tag_GNU_S390_ABI_Vector)
16546 {
16547 printf (" Tag_GNU_S390_ABI_Vector: ");
16548 READ_ULEB (val, p, end);
16549
16550 switch (val)
16551 {
16552 case 0:
16553 printf (_("any\n"));
16554 break;
16555 case 1:
16556 printf (_("software\n"));
16557 break;
16558 case 2:
16559 printf (_("hardware\n"));
16560 break;
16561 default:
16562 printf ("??? (%d)\n", val);
16563 break;
16564 }
16565 return p;
16566 }
16567
16568 return display_tag_value (tag & 1, p, end);
16569 }
16570
16571 static void
16572 display_sparc_hwcaps (unsigned int mask)
16573 {
16574 if (mask)
16575 {
16576 bool first = true;
16577
16578 if (mask & ELF_SPARC_HWCAP_MUL32)
16579 fputs ("mul32", stdout), first = false;
16580 if (mask & ELF_SPARC_HWCAP_DIV32)
16581 printf ("%sdiv32", first ? "" : "|"), first = false;
16582 if (mask & ELF_SPARC_HWCAP_FSMULD)
16583 printf ("%sfsmuld", first ? "" : "|"), first = false;
16584 if (mask & ELF_SPARC_HWCAP_V8PLUS)
16585 printf ("%sv8plus", first ? "" : "|"), first = false;
16586 if (mask & ELF_SPARC_HWCAP_POPC)
16587 printf ("%spopc", first ? "" : "|"), first = false;
16588 if (mask & ELF_SPARC_HWCAP_VIS)
16589 printf ("%svis", first ? "" : "|"), first = false;
16590 if (mask & ELF_SPARC_HWCAP_VIS2)
16591 printf ("%svis2", first ? "" : "|"), first = false;
16592 if (mask & ELF_SPARC_HWCAP_ASI_BLK_INIT)
16593 printf ("%sASIBlkInit", first ? "" : "|"), first = false;
16594 if (mask & ELF_SPARC_HWCAP_FMAF)
16595 printf ("%sfmaf", first ? "" : "|"), first = false;
16596 if (mask & ELF_SPARC_HWCAP_VIS3)
16597 printf ("%svis3", first ? "" : "|"), first = false;
16598 if (mask & ELF_SPARC_HWCAP_HPC)
16599 printf ("%shpc", first ? "" : "|"), first = false;
16600 if (mask & ELF_SPARC_HWCAP_RANDOM)
16601 printf ("%srandom", first ? "" : "|"), first = false;
16602 if (mask & ELF_SPARC_HWCAP_TRANS)
16603 printf ("%strans", first ? "" : "|"), first = false;
16604 if (mask & ELF_SPARC_HWCAP_FJFMAU)
16605 printf ("%sfjfmau", first ? "" : "|"), first = false;
16606 if (mask & ELF_SPARC_HWCAP_IMA)
16607 printf ("%sima", first ? "" : "|"), first = false;
16608 if (mask & ELF_SPARC_HWCAP_ASI_CACHE_SPARING)
16609 printf ("%scspare", first ? "" : "|"), first = false;
16610 }
16611 else
16612 fputc ('0', stdout);
16613 fputc ('\n', stdout);
16614 }
16615
16616 static void
16617 display_sparc_hwcaps2 (unsigned int mask)
16618 {
16619 if (mask)
16620 {
16621 bool first = true;
16622
16623 if (mask & ELF_SPARC_HWCAP2_FJATHPLUS)
16624 fputs ("fjathplus", stdout), first = false;
16625 if (mask & ELF_SPARC_HWCAP2_VIS3B)
16626 printf ("%svis3b", first ? "" : "|"), first = false;
16627 if (mask & ELF_SPARC_HWCAP2_ADP)
16628 printf ("%sadp", first ? "" : "|"), first = false;
16629 if (mask & ELF_SPARC_HWCAP2_SPARC5)
16630 printf ("%ssparc5", first ? "" : "|"), first = false;
16631 if (mask & ELF_SPARC_HWCAP2_MWAIT)
16632 printf ("%smwait", first ? "" : "|"), first = false;
16633 if (mask & ELF_SPARC_HWCAP2_XMPMUL)
16634 printf ("%sxmpmul", first ? "" : "|"), first = false;
16635 if (mask & ELF_SPARC_HWCAP2_XMONT)
16636 printf ("%sxmont2", first ? "" : "|"), first = false;
16637 if (mask & ELF_SPARC_HWCAP2_NSEC)
16638 printf ("%snsec", first ? "" : "|"), first = false;
16639 if (mask & ELF_SPARC_HWCAP2_FJATHHPC)
16640 printf ("%sfjathhpc", first ? "" : "|"), first = false;
16641 if (mask & ELF_SPARC_HWCAP2_FJDES)
16642 printf ("%sfjdes", first ? "" : "|"), first = false;
16643 if (mask & ELF_SPARC_HWCAP2_FJAES)
16644 printf ("%sfjaes", first ? "" : "|"), first = false;
16645 }
16646 else
16647 fputc ('0', stdout);
16648 fputc ('\n', stdout);
16649 }
16650
16651 static unsigned char *
16652 display_sparc_gnu_attribute (unsigned char * p,
16653 unsigned int tag,
16654 const unsigned char * const end)
16655 {
16656 unsigned int val;
16657
16658 if (tag == Tag_GNU_Sparc_HWCAPS)
16659 {
16660 READ_ULEB (val, p, end);
16661 printf (" Tag_GNU_Sparc_HWCAPS: ");
16662 display_sparc_hwcaps (val);
16663 return p;
16664 }
16665 if (tag == Tag_GNU_Sparc_HWCAPS2)
16666 {
16667 READ_ULEB (val, p, end);
16668 printf (" Tag_GNU_Sparc_HWCAPS2: ");
16669 display_sparc_hwcaps2 (val);
16670 return p;
16671 }
16672
16673 return display_tag_value (tag, p, end);
16674 }
16675
16676 static void
16677 print_mips_fp_abi_value (unsigned int val)
16678 {
16679 switch (val)
16680 {
16681 case Val_GNU_MIPS_ABI_FP_ANY:
16682 printf (_("Hard or soft float\n"));
16683 break;
16684 case Val_GNU_MIPS_ABI_FP_DOUBLE:
16685 printf (_("Hard float (double precision)\n"));
16686 break;
16687 case Val_GNU_MIPS_ABI_FP_SINGLE:
16688 printf (_("Hard float (single precision)\n"));
16689 break;
16690 case Val_GNU_MIPS_ABI_FP_SOFT:
16691 printf (_("Soft float\n"));
16692 break;
16693 case Val_GNU_MIPS_ABI_FP_OLD_64:
16694 printf (_("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
16695 break;
16696 case Val_GNU_MIPS_ABI_FP_XX:
16697 printf (_("Hard float (32-bit CPU, Any FPU)\n"));
16698 break;
16699 case Val_GNU_MIPS_ABI_FP_64:
16700 printf (_("Hard float (32-bit CPU, 64-bit FPU)\n"));
16701 break;
16702 case Val_GNU_MIPS_ABI_FP_64A:
16703 printf (_("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
16704 break;
16705 case Val_GNU_MIPS_ABI_FP_NAN2008:
16706 printf (_("NaN 2008 compatibility\n"));
16707 break;
16708 default:
16709 printf ("??? (%d)\n", val);
16710 break;
16711 }
16712 }
16713
16714 static unsigned char *
16715 display_mips_gnu_attribute (unsigned char * p,
16716 unsigned int tag,
16717 const unsigned char * const end)
16718 {
16719 if (tag == Tag_GNU_MIPS_ABI_FP)
16720 {
16721 unsigned int val;
16722
16723 printf (" Tag_GNU_MIPS_ABI_FP: ");
16724 READ_ULEB (val, p, end);
16725 print_mips_fp_abi_value (val);
16726 return p;
16727 }
16728
16729 if (tag == Tag_GNU_MIPS_ABI_MSA)
16730 {
16731 unsigned int val;
16732
16733 printf (" Tag_GNU_MIPS_ABI_MSA: ");
16734 READ_ULEB (val, p, end);
16735
16736 switch (val)
16737 {
16738 case Val_GNU_MIPS_ABI_MSA_ANY:
16739 printf (_("Any MSA or not\n"));
16740 break;
16741 case Val_GNU_MIPS_ABI_MSA_128:
16742 printf (_("128-bit MSA\n"));
16743 break;
16744 default:
16745 printf ("??? (%d)\n", val);
16746 break;
16747 }
16748 return p;
16749 }
16750
16751 return display_tag_value (tag & 1, p, end);
16752 }
16753
16754 static unsigned char *
16755 display_tic6x_attribute (unsigned char * p,
16756 const unsigned char * const end)
16757 {
16758 unsigned int tag;
16759 unsigned int val;
16760
16761 READ_ULEB (tag, p, end);
16762
16763 switch (tag)
16764 {
16765 case Tag_ISA:
16766 printf (" Tag_ISA: ");
16767 READ_ULEB (val, p, end);
16768
16769 switch (val)
16770 {
16771 case C6XABI_Tag_ISA_none:
16772 printf (_("None\n"));
16773 break;
16774 case C6XABI_Tag_ISA_C62X:
16775 printf ("C62x\n");
16776 break;
16777 case C6XABI_Tag_ISA_C67X:
16778 printf ("C67x\n");
16779 break;
16780 case C6XABI_Tag_ISA_C67XP:
16781 printf ("C67x+\n");
16782 break;
16783 case C6XABI_Tag_ISA_C64X:
16784 printf ("C64x\n");
16785 break;
16786 case C6XABI_Tag_ISA_C64XP:
16787 printf ("C64x+\n");
16788 break;
16789 case C6XABI_Tag_ISA_C674X:
16790 printf ("C674x\n");
16791 break;
16792 default:
16793 printf ("??? (%d)\n", val);
16794 break;
16795 }
16796 return p;
16797
16798 case Tag_ABI_wchar_t:
16799 printf (" Tag_ABI_wchar_t: ");
16800 READ_ULEB (val, p, end);
16801 switch (val)
16802 {
16803 case 0:
16804 printf (_("Not used\n"));
16805 break;
16806 case 1:
16807 printf (_("2 bytes\n"));
16808 break;
16809 case 2:
16810 printf (_("4 bytes\n"));
16811 break;
16812 default:
16813 printf ("??? (%d)\n", val);
16814 break;
16815 }
16816 return p;
16817
16818 case Tag_ABI_stack_align_needed:
16819 printf (" Tag_ABI_stack_align_needed: ");
16820 READ_ULEB (val, p, end);
16821 switch (val)
16822 {
16823 case 0:
16824 printf (_("8-byte\n"));
16825 break;
16826 case 1:
16827 printf (_("16-byte\n"));
16828 break;
16829 default:
16830 printf ("??? (%d)\n", val);
16831 break;
16832 }
16833 return p;
16834
16835 case Tag_ABI_stack_align_preserved:
16836 READ_ULEB (val, p, end);
16837 printf (" Tag_ABI_stack_align_preserved: ");
16838 switch (val)
16839 {
16840 case 0:
16841 printf (_("8-byte\n"));
16842 break;
16843 case 1:
16844 printf (_("16-byte\n"));
16845 break;
16846 default:
16847 printf ("??? (%d)\n", val);
16848 break;
16849 }
16850 return p;
16851
16852 case Tag_ABI_DSBT:
16853 READ_ULEB (val, p, end);
16854 printf (" Tag_ABI_DSBT: ");
16855 switch (val)
16856 {
16857 case 0:
16858 printf (_("DSBT addressing not used\n"));
16859 break;
16860 case 1:
16861 printf (_("DSBT addressing used\n"));
16862 break;
16863 default:
16864 printf ("??? (%d)\n", val);
16865 break;
16866 }
16867 return p;
16868
16869 case Tag_ABI_PID:
16870 READ_ULEB (val, p, end);
16871 printf (" Tag_ABI_PID: ");
16872 switch (val)
16873 {
16874 case 0:
16875 printf (_("Data addressing position-dependent\n"));
16876 break;
16877 case 1:
16878 printf (_("Data addressing position-independent, GOT near DP\n"));
16879 break;
16880 case 2:
16881 printf (_("Data addressing position-independent, GOT far from DP\n"));
16882 break;
16883 default:
16884 printf ("??? (%d)\n", val);
16885 break;
16886 }
16887 return p;
16888
16889 case Tag_ABI_PIC:
16890 READ_ULEB (val, p, end);
16891 printf (" Tag_ABI_PIC: ");
16892 switch (val)
16893 {
16894 case 0:
16895 printf (_("Code addressing position-dependent\n"));
16896 break;
16897 case 1:
16898 printf (_("Code addressing position-independent\n"));
16899 break;
16900 default:
16901 printf ("??? (%d)\n", val);
16902 break;
16903 }
16904 return p;
16905
16906 case Tag_ABI_array_object_alignment:
16907 READ_ULEB (val, p, end);
16908 printf (" Tag_ABI_array_object_alignment: ");
16909 switch (val)
16910 {
16911 case 0:
16912 printf (_("8-byte\n"));
16913 break;
16914 case 1:
16915 printf (_("4-byte\n"));
16916 break;
16917 case 2:
16918 printf (_("16-byte\n"));
16919 break;
16920 default:
16921 printf ("??? (%d)\n", val);
16922 break;
16923 }
16924 return p;
16925
16926 case Tag_ABI_array_object_align_expected:
16927 READ_ULEB (val, p, end);
16928 printf (" Tag_ABI_array_object_align_expected: ");
16929 switch (val)
16930 {
16931 case 0:
16932 printf (_("8-byte\n"));
16933 break;
16934 case 1:
16935 printf (_("4-byte\n"));
16936 break;
16937 case 2:
16938 printf (_("16-byte\n"));
16939 break;
16940 default:
16941 printf ("??? (%d)\n", val);
16942 break;
16943 }
16944 return p;
16945
16946 case Tag_ABI_compatibility:
16947 {
16948 READ_ULEB (val, p, end);
16949 printf (" Tag_ABI_compatibility: ");
16950 printf (_("flag = %d, vendor = "), val);
16951 if (p < end - 1)
16952 {
16953 size_t maxlen = (end - p) - 1;
16954
16955 print_symbol ((int) maxlen, (const char *) p);
16956 p += strnlen ((char *) p, maxlen) + 1;
16957 }
16958 else
16959 {
16960 printf (_("<corrupt>"));
16961 p = (unsigned char *) end;
16962 }
16963 putchar ('\n');
16964 return p;
16965 }
16966
16967 case Tag_ABI_conformance:
16968 {
16969 printf (" Tag_ABI_conformance: \"");
16970 if (p < end - 1)
16971 {
16972 size_t maxlen = (end - p) - 1;
16973
16974 print_symbol ((int) maxlen, (const char *) p);
16975 p += strnlen ((char *) p, maxlen) + 1;
16976 }
16977 else
16978 {
16979 printf (_("<corrupt>"));
16980 p = (unsigned char *) end;
16981 }
16982 printf ("\"\n");
16983 return p;
16984 }
16985 }
16986
16987 return display_tag_value (tag, p, end);
16988 }
16989
16990 static void
16991 display_raw_attribute (unsigned char * p, unsigned char const * const end)
16992 {
16993 unsigned long addr = 0;
16994 size_t bytes = end - p;
16995
16996 assert (end >= p);
16997 while (bytes)
16998 {
16999 int j;
17000 int k;
17001 int lbytes = (bytes > 16 ? 16 : bytes);
17002
17003 printf (" 0x%8.8lx ", addr);
17004
17005 for (j = 0; j < 16; j++)
17006 {
17007 if (j < lbytes)
17008 printf ("%2.2x", p[j]);
17009 else
17010 printf (" ");
17011
17012 if ((j & 3) == 3)
17013 printf (" ");
17014 }
17015
17016 for (j = 0; j < lbytes; j++)
17017 {
17018 k = p[j];
17019 if (k >= ' ' && k < 0x7f)
17020 printf ("%c", k);
17021 else
17022 printf (".");
17023 }
17024
17025 putchar ('\n');
17026
17027 p += lbytes;
17028 bytes -= lbytes;
17029 addr += lbytes;
17030 }
17031
17032 putchar ('\n');
17033 }
17034
17035 static unsigned char *
17036 display_msp430_attribute (unsigned char * p,
17037 const unsigned char * const end)
17038 {
17039 unsigned int val;
17040 unsigned int tag;
17041
17042 READ_ULEB (tag, p, end);
17043
17044 switch (tag)
17045 {
17046 case OFBA_MSPABI_Tag_ISA:
17047 printf (" Tag_ISA: ");
17048 READ_ULEB (val, p, end);
17049 switch (val)
17050 {
17051 case 0: printf (_("None\n")); break;
17052 case 1: printf (_("MSP430\n")); break;
17053 case 2: printf (_("MSP430X\n")); break;
17054 default: printf ("??? (%d)\n", val); break;
17055 }
17056 break;
17057
17058 case OFBA_MSPABI_Tag_Code_Model:
17059 printf (" Tag_Code_Model: ");
17060 READ_ULEB (val, p, end);
17061 switch (val)
17062 {
17063 case 0: printf (_("None\n")); break;
17064 case 1: printf (_("Small\n")); break;
17065 case 2: printf (_("Large\n")); break;
17066 default: printf ("??? (%d)\n", val); break;
17067 }
17068 break;
17069
17070 case OFBA_MSPABI_Tag_Data_Model:
17071 printf (" Tag_Data_Model: ");
17072 READ_ULEB (val, p, end);
17073 switch (val)
17074 {
17075 case 0: printf (_("None\n")); break;
17076 case 1: printf (_("Small\n")); break;
17077 case 2: printf (_("Large\n")); break;
17078 case 3: printf (_("Restricted Large\n")); break;
17079 default: printf ("??? (%d)\n", val); break;
17080 }
17081 break;
17082
17083 default:
17084 printf (_(" <unknown tag %d>: "), tag);
17085
17086 if (tag & 1)
17087 {
17088 putchar ('"');
17089 if (p < end - 1)
17090 {
17091 size_t maxlen = (end - p) - 1;
17092
17093 print_symbol ((int) maxlen, (const char *) p);
17094 p += strnlen ((char *) p, maxlen) + 1;
17095 }
17096 else
17097 {
17098 printf (_("<corrupt>"));
17099 p = (unsigned char *) end;
17100 }
17101 printf ("\"\n");
17102 }
17103 else
17104 {
17105 READ_ULEB (val, p, end);
17106 printf ("%d (0x%x)\n", val, val);
17107 }
17108 break;
17109 }
17110
17111 assert (p <= end);
17112 return p;
17113 }
17114
17115 static unsigned char *
17116 display_msp430_gnu_attribute (unsigned char * p,
17117 unsigned int tag,
17118 const unsigned char * const end)
17119 {
17120 if (tag == Tag_GNU_MSP430_Data_Region)
17121 {
17122 unsigned int val;
17123
17124 printf (" Tag_GNU_MSP430_Data_Region: ");
17125 READ_ULEB (val, p, end);
17126
17127 switch (val)
17128 {
17129 case Val_GNU_MSP430_Data_Region_Any:
17130 printf (_("Any Region\n"));
17131 break;
17132 case Val_GNU_MSP430_Data_Region_Lower:
17133 printf (_("Lower Region Only\n"));
17134 break;
17135 default:
17136 printf ("??? (%u)\n", val);
17137 }
17138 return p;
17139 }
17140 return display_tag_value (tag & 1, p, end);
17141 }
17142
17143 struct riscv_attr_tag_t {
17144 const char *name;
17145 unsigned int tag;
17146 };
17147
17148 static struct riscv_attr_tag_t riscv_attr_tag[] =
17149 {
17150 #define T(tag) {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
17151 T(arch),
17152 T(priv_spec),
17153 T(priv_spec_minor),
17154 T(priv_spec_revision),
17155 T(unaligned_access),
17156 T(stack_align),
17157 #undef T
17158 };
17159
17160 static unsigned char *
17161 display_riscv_attribute (unsigned char *p,
17162 const unsigned char * const end)
17163 {
17164 unsigned int val;
17165 unsigned int tag;
17166 struct riscv_attr_tag_t *attr = NULL;
17167 unsigned i;
17168
17169 READ_ULEB (tag, p, end);
17170
17171 /* Find the name of attribute. */
17172 for (i = 0; i < ARRAY_SIZE (riscv_attr_tag); i++)
17173 {
17174 if (riscv_attr_tag[i].tag == tag)
17175 {
17176 attr = &riscv_attr_tag[i];
17177 break;
17178 }
17179 }
17180
17181 if (attr)
17182 printf (" %s: ", attr->name);
17183 else
17184 return display_tag_value (tag, p, end);
17185
17186 switch (tag)
17187 {
17188 case Tag_RISCV_priv_spec:
17189 case Tag_RISCV_priv_spec_minor:
17190 case Tag_RISCV_priv_spec_revision:
17191 READ_ULEB (val, p, end);
17192 printf (_("%u\n"), val);
17193 break;
17194 case Tag_RISCV_unaligned_access:
17195 READ_ULEB (val, p, end);
17196 switch (val)
17197 {
17198 case 0:
17199 printf (_("No unaligned access\n"));
17200 break;
17201 case 1:
17202 printf (_("Unaligned access\n"));
17203 break;
17204 }
17205 break;
17206 case Tag_RISCV_stack_align:
17207 READ_ULEB (val, p, end);
17208 printf (_("%u-bytes\n"), val);
17209 break;
17210 case Tag_RISCV_arch:
17211 p = display_tag_value (-1, p, end);
17212 break;
17213 default:
17214 return display_tag_value (tag, p, end);
17215 }
17216
17217 return p;
17218 }
17219
17220 static unsigned char *
17221 display_csky_attribute (unsigned char * p,
17222 const unsigned char * const end)
17223 {
17224 unsigned int tag;
17225 unsigned int val;
17226 READ_ULEB (tag, p, end);
17227
17228 if (tag >= Tag_CSKY_MAX)
17229 {
17230 return display_tag_value (-1, p, end);
17231 }
17232
17233 switch (tag)
17234 {
17235 case Tag_CSKY_ARCH_NAME:
17236 printf (" Tag_CSKY_ARCH_NAME:\t\t");
17237 return display_tag_value (-1, p, end);
17238 case Tag_CSKY_CPU_NAME:
17239 printf (" Tag_CSKY_CPU_NAME:\t\t");
17240 return display_tag_value (-1, p, end);
17241
17242 case Tag_CSKY_ISA_FLAGS:
17243 printf (" Tag_CSKY_ISA_FLAGS:\t\t");
17244 return display_tag_value (0, p, end);
17245 case Tag_CSKY_ISA_EXT_FLAGS:
17246 printf (" Tag_CSKY_ISA_EXT_FLAGS:\t");
17247 return display_tag_value (0, p, end);
17248
17249 case Tag_CSKY_DSP_VERSION:
17250 printf (" Tag_CSKY_DSP_VERSION:\t\t");
17251 READ_ULEB (val, p, end);
17252 if (val == VAL_CSKY_DSP_VERSION_EXTENSION)
17253 printf ("DSP Extension\n");
17254 else if (val == VAL_CSKY_DSP_VERSION_2)
17255 printf ("DSP 2.0\n");
17256 break;
17257
17258 case Tag_CSKY_VDSP_VERSION:
17259 printf (" Tag_CSKY_VDSP_VERSION:\t");
17260 READ_ULEB (val, p, end);
17261 printf ("VDSP Version %d\n", val);
17262 break;
17263
17264 case Tag_CSKY_FPU_VERSION:
17265 printf (" Tag_CSKY_FPU_VERSION:\t\t");
17266 READ_ULEB (val, p, end);
17267 if (val == VAL_CSKY_FPU_VERSION_1)
17268 printf ("ABIV1 FPU Version 1\n");
17269 else if (val == VAL_CSKY_FPU_VERSION_2)
17270 printf ("FPU Version 2\n");
17271 break;
17272
17273 case Tag_CSKY_FPU_ABI:
17274 printf (" Tag_CSKY_FPU_ABI:\t\t");
17275 READ_ULEB (val, p, end);
17276 if (val == VAL_CSKY_FPU_ABI_HARD)
17277 printf ("Hard\n");
17278 else if (val == VAL_CSKY_FPU_ABI_SOFTFP)
17279 printf ("SoftFP\n");
17280 else if (val == VAL_CSKY_FPU_ABI_SOFT)
17281 printf ("Soft\n");
17282 break;
17283 case Tag_CSKY_FPU_ROUNDING:
17284 READ_ULEB (val, p, end);
17285 if (val == 1) {
17286 printf (" Tag_CSKY_FPU_ROUNDING:\t");
17287 printf ("Needed\n");
17288 }
17289 break;
17290 case Tag_CSKY_FPU_DENORMAL:
17291 READ_ULEB (val, p, end);
17292 if (val == 1) {
17293 printf (" Tag_CSKY_FPU_DENORMAL:\t");
17294 printf ("Needed\n");
17295 }
17296 break;
17297 case Tag_CSKY_FPU_Exception:
17298 READ_ULEB (val, p, end);
17299 if (val == 1) {
17300 printf (" Tag_CSKY_FPU_Exception:\t");
17301 printf ("Needed\n");
17302 }
17303 break;
17304 case Tag_CSKY_FPU_NUMBER_MODULE:
17305 printf (" Tag_CSKY_FPU_NUMBER_MODULE:\t");
17306 return display_tag_value (-1, p, end);
17307 case Tag_CSKY_FPU_HARDFP:
17308 printf (" Tag_CSKY_FPU_HARDFP:\t\t");
17309 READ_ULEB (val, p, end);
17310 if (val & VAL_CSKY_FPU_HARDFP_HALF)
17311 printf (" Half");
17312 if (val & VAL_CSKY_FPU_HARDFP_SINGLE)
17313 printf (" Single");
17314 if (val & VAL_CSKY_FPU_HARDFP_DOUBLE)
17315 printf (" Double");
17316 printf ("\n");
17317 break;
17318 default:
17319 return display_tag_value (tag, p, end);
17320 }
17321 return p;
17322 }
17323
17324 static bool
17325 process_attributes (Filedata * filedata,
17326 const char * public_name,
17327 unsigned int proc_type,
17328 unsigned char * (* display_pub_attribute) (unsigned char *, const unsigned char * const),
17329 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const))
17330 {
17331 Elf_Internal_Shdr * sect;
17332 unsigned i;
17333 bool res = true;
17334
17335 /* Find the section header so that we get the size. */
17336 for (i = 0, sect = filedata->section_headers;
17337 i < filedata->file_header.e_shnum;
17338 i++, sect++)
17339 {
17340 unsigned char * contents;
17341 unsigned char * p;
17342
17343 if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
17344 continue;
17345
17346 contents = (unsigned char *) get_data (NULL, filedata, sect->sh_offset, 1,
17347 sect->sh_size, _("attributes"));
17348 if (contents == NULL)
17349 {
17350 res = false;
17351 continue;
17352 }
17353
17354 p = contents;
17355 /* The first character is the version of the attributes.
17356 Currently only version 1, (aka 'A') is recognised here. */
17357 if (*p != 'A')
17358 {
17359 printf (_("Unknown attributes version '%c'(%d) - expecting 'A'\n"), *p, *p);
17360 res = false;
17361 }
17362 else
17363 {
17364 bfd_vma section_len;
17365
17366 section_len = sect->sh_size - 1;
17367 p++;
17368
17369 while (section_len > 0)
17370 {
17371 bfd_vma attr_len;
17372 unsigned int namelen;
17373 bool public_section;
17374 bool gnu_section;
17375
17376 if (section_len <= 4)
17377 {
17378 error (_("Tag section ends prematurely\n"));
17379 res = false;
17380 break;
17381 }
17382 attr_len = byte_get (p, 4);
17383 p += 4;
17384
17385 if (attr_len > section_len)
17386 {
17387 error (_("Bad attribute length (%u > %u)\n"),
17388 (unsigned) attr_len, (unsigned) section_len);
17389 attr_len = section_len;
17390 res = false;
17391 }
17392 /* PR 17531: file: 001-101425-0.004 */
17393 else if (attr_len < 5)
17394 {
17395 error (_("Attribute length of %u is too small\n"), (unsigned) attr_len);
17396 res = false;
17397 break;
17398 }
17399
17400 section_len -= attr_len;
17401 attr_len -= 4;
17402
17403 namelen = strnlen ((char *) p, attr_len) + 1;
17404 if (namelen == 0 || namelen >= attr_len)
17405 {
17406 error (_("Corrupt attribute section name\n"));
17407 res = false;
17408 break;
17409 }
17410
17411 printf (_("Attribute Section: "));
17412 print_symbol (INT_MAX, (const char *) p);
17413 putchar ('\n');
17414
17415 if (public_name && streq ((char *) p, public_name))
17416 public_section = true;
17417 else
17418 public_section = false;
17419
17420 if (streq ((char *) p, "gnu"))
17421 gnu_section = true;
17422 else
17423 gnu_section = false;
17424
17425 p += namelen;
17426 attr_len -= namelen;
17427
17428 while (attr_len > 0 && p < contents + sect->sh_size)
17429 {
17430 int tag;
17431 unsigned int val;
17432 bfd_vma size;
17433 unsigned char * end;
17434
17435 /* PR binutils/17531: Safe handling of corrupt files. */
17436 if (attr_len < 6)
17437 {
17438 error (_("Unused bytes at end of section\n"));
17439 res = false;
17440 section_len = 0;
17441 break;
17442 }
17443
17444 tag = *(p++);
17445 size = byte_get (p, 4);
17446 if (size > attr_len)
17447 {
17448 error (_("Bad subsection length (%u > %u)\n"),
17449 (unsigned) size, (unsigned) attr_len);
17450 res = false;
17451 size = attr_len;
17452 }
17453 /* PR binutils/17531: Safe handling of corrupt files. */
17454 if (size < 6)
17455 {
17456 error (_("Bad subsection length (%u < 6)\n"),
17457 (unsigned) size);
17458 res = false;
17459 section_len = 0;
17460 break;
17461 }
17462
17463 attr_len -= size;
17464 end = p + size - 1;
17465 assert (end <= contents + sect->sh_size);
17466 p += 4;
17467
17468 switch (tag)
17469 {
17470 case 1:
17471 printf (_("File Attributes\n"));
17472 break;
17473 case 2:
17474 printf (_("Section Attributes:"));
17475 goto do_numlist;
17476 case 3:
17477 printf (_("Symbol Attributes:"));
17478 /* Fall through. */
17479 do_numlist:
17480 for (;;)
17481 {
17482 READ_ULEB (val, p, end);
17483 if (val == 0)
17484 break;
17485 printf (" %d", val);
17486 }
17487 printf ("\n");
17488 break;
17489 default:
17490 printf (_("Unknown tag: %d\n"), tag);
17491 public_section = false;
17492 break;
17493 }
17494
17495 if (public_section && display_pub_attribute != NULL)
17496 {
17497 while (p < end)
17498 p = display_pub_attribute (p, end);
17499 assert (p == end);
17500 }
17501 else if (gnu_section && display_proc_gnu_attribute != NULL)
17502 {
17503 while (p < end)
17504 p = display_gnu_attribute (p,
17505 display_proc_gnu_attribute,
17506 end);
17507 assert (p == end);
17508 }
17509 else if (p < end)
17510 {
17511 printf (_(" Unknown attribute:\n"));
17512 display_raw_attribute (p, end);
17513 p = end;
17514 }
17515 else
17516 attr_len = 0;
17517 }
17518 }
17519 }
17520
17521 free (contents);
17522 }
17523
17524 return res;
17525 }
17526
17527 /* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
17528 Print the Address, Access and Initial fields of an entry at VMA ADDR
17529 and return the VMA of the next entry, or -1 if there was a problem.
17530 Does not read from DATA_END or beyond. */
17531
17532 static bfd_vma
17533 print_mips_got_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr,
17534 unsigned char * data_end)
17535 {
17536 printf (" ");
17537 print_vma (addr, LONG_HEX);
17538 printf (" ");
17539 if (addr < pltgot + 0xfff0)
17540 printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
17541 else
17542 printf ("%10s", "");
17543 printf (" ");
17544 if (data == NULL)
17545 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
17546 else
17547 {
17548 bfd_vma entry;
17549 unsigned char * from = data + addr - pltgot;
17550
17551 if (from + (is_32bit_elf ? 4 : 8) > data_end)
17552 {
17553 warn (_("MIPS GOT entry extends beyond the end of available data\n"));
17554 printf ("%*s", is_32bit_elf ? 8 : 16, _("<corrupt>"));
17555 return (bfd_vma) -1;
17556 }
17557 else
17558 {
17559 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
17560 print_vma (entry, LONG_HEX);
17561 }
17562 }
17563 return addr + (is_32bit_elf ? 4 : 8);
17564 }
17565
17566 /* DATA points to the contents of a MIPS PLT GOT that starts at VMA
17567 PLTGOT. Print the Address and Initial fields of an entry at VMA
17568 ADDR and return the VMA of the next entry. */
17569
17570 static bfd_vma
17571 print_mips_pltgot_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr)
17572 {
17573 printf (" ");
17574 print_vma (addr, LONG_HEX);
17575 printf (" ");
17576 if (data == NULL)
17577 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
17578 else
17579 {
17580 bfd_vma entry;
17581
17582 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
17583 print_vma (entry, LONG_HEX);
17584 }
17585 return addr + (is_32bit_elf ? 4 : 8);
17586 }
17587
17588 static void
17589 print_mips_ases (unsigned int mask)
17590 {
17591 if (mask & AFL_ASE_DSP)
17592 fputs ("\n\tDSP ASE", stdout);
17593 if (mask & AFL_ASE_DSPR2)
17594 fputs ("\n\tDSP R2 ASE", stdout);
17595 if (mask & AFL_ASE_DSPR3)
17596 fputs ("\n\tDSP R3 ASE", stdout);
17597 if (mask & AFL_ASE_EVA)
17598 fputs ("\n\tEnhanced VA Scheme", stdout);
17599 if (mask & AFL_ASE_MCU)
17600 fputs ("\n\tMCU (MicroController) ASE", stdout);
17601 if (mask & AFL_ASE_MDMX)
17602 fputs ("\n\tMDMX ASE", stdout);
17603 if (mask & AFL_ASE_MIPS3D)
17604 fputs ("\n\tMIPS-3D ASE", stdout);
17605 if (mask & AFL_ASE_MT)
17606 fputs ("\n\tMT ASE", stdout);
17607 if (mask & AFL_ASE_SMARTMIPS)
17608 fputs ("\n\tSmartMIPS ASE", stdout);
17609 if (mask & AFL_ASE_VIRT)
17610 fputs ("\n\tVZ ASE", stdout);
17611 if (mask & AFL_ASE_MSA)
17612 fputs ("\n\tMSA ASE", stdout);
17613 if (mask & AFL_ASE_MIPS16)
17614 fputs ("\n\tMIPS16 ASE", stdout);
17615 if (mask & AFL_ASE_MICROMIPS)
17616 fputs ("\n\tMICROMIPS ASE", stdout);
17617 if (mask & AFL_ASE_XPA)
17618 fputs ("\n\tXPA ASE", stdout);
17619 if (mask & AFL_ASE_MIPS16E2)
17620 fputs ("\n\tMIPS16e2 ASE", stdout);
17621 if (mask & AFL_ASE_CRC)
17622 fputs ("\n\tCRC ASE", stdout);
17623 if (mask & AFL_ASE_GINV)
17624 fputs ("\n\tGINV ASE", stdout);
17625 if (mask & AFL_ASE_LOONGSON_MMI)
17626 fputs ("\n\tLoongson MMI ASE", stdout);
17627 if (mask & AFL_ASE_LOONGSON_CAM)
17628 fputs ("\n\tLoongson CAM ASE", stdout);
17629 if (mask & AFL_ASE_LOONGSON_EXT)
17630 fputs ("\n\tLoongson EXT ASE", stdout);
17631 if (mask & AFL_ASE_LOONGSON_EXT2)
17632 fputs ("\n\tLoongson EXT2 ASE", stdout);
17633 if (mask == 0)
17634 fprintf (stdout, "\n\t%s", _("None"));
17635 else if ((mask & ~AFL_ASE_MASK) != 0)
17636 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
17637 }
17638
17639 static void
17640 print_mips_isa_ext (unsigned int isa_ext)
17641 {
17642 switch (isa_ext)
17643 {
17644 case 0:
17645 fputs (_("None"), stdout);
17646 break;
17647 case AFL_EXT_XLR:
17648 fputs ("RMI XLR", stdout);
17649 break;
17650 case AFL_EXT_OCTEON3:
17651 fputs ("Cavium Networks Octeon3", stdout);
17652 break;
17653 case AFL_EXT_OCTEON2:
17654 fputs ("Cavium Networks Octeon2", stdout);
17655 break;
17656 case AFL_EXT_OCTEONP:
17657 fputs ("Cavium Networks OcteonP", stdout);
17658 break;
17659 case AFL_EXT_OCTEON:
17660 fputs ("Cavium Networks Octeon", stdout);
17661 break;
17662 case AFL_EXT_5900:
17663 fputs ("Toshiba R5900", stdout);
17664 break;
17665 case AFL_EXT_4650:
17666 fputs ("MIPS R4650", stdout);
17667 break;
17668 case AFL_EXT_4010:
17669 fputs ("LSI R4010", stdout);
17670 break;
17671 case AFL_EXT_4100:
17672 fputs ("NEC VR4100", stdout);
17673 break;
17674 case AFL_EXT_3900:
17675 fputs ("Toshiba R3900", stdout);
17676 break;
17677 case AFL_EXT_10000:
17678 fputs ("MIPS R10000", stdout);
17679 break;
17680 case AFL_EXT_SB1:
17681 fputs ("Broadcom SB-1", stdout);
17682 break;
17683 case AFL_EXT_4111:
17684 fputs ("NEC VR4111/VR4181", stdout);
17685 break;
17686 case AFL_EXT_4120:
17687 fputs ("NEC VR4120", stdout);
17688 break;
17689 case AFL_EXT_5400:
17690 fputs ("NEC VR5400", stdout);
17691 break;
17692 case AFL_EXT_5500:
17693 fputs ("NEC VR5500", stdout);
17694 break;
17695 case AFL_EXT_LOONGSON_2E:
17696 fputs ("ST Microelectronics Loongson 2E", stdout);
17697 break;
17698 case AFL_EXT_LOONGSON_2F:
17699 fputs ("ST Microelectronics Loongson 2F", stdout);
17700 break;
17701 case AFL_EXT_INTERAPTIV_MR2:
17702 fputs ("Imagination interAptiv MR2", stdout);
17703 break;
17704 default:
17705 fprintf (stdout, "%s (%d)", _("Unknown"), isa_ext);
17706 }
17707 }
17708
17709 static signed int
17710 get_mips_reg_size (int reg_size)
17711 {
17712 return (reg_size == AFL_REG_NONE) ? 0
17713 : (reg_size == AFL_REG_32) ? 32
17714 : (reg_size == AFL_REG_64) ? 64
17715 : (reg_size == AFL_REG_128) ? 128
17716 : -1;
17717 }
17718
17719 static bool
17720 process_mips_specific (Filedata * filedata)
17721 {
17722 Elf_Internal_Dyn * entry;
17723 Elf_Internal_Shdr *sect = NULL;
17724 size_t liblist_offset = 0;
17725 size_t liblistno = 0;
17726 size_t conflictsno = 0;
17727 size_t options_offset = 0;
17728 size_t conflicts_offset = 0;
17729 size_t pltrelsz = 0;
17730 size_t pltrel = 0;
17731 bfd_vma pltgot = 0;
17732 bfd_vma mips_pltgot = 0;
17733 bfd_vma jmprel = 0;
17734 bfd_vma local_gotno = 0;
17735 bfd_vma gotsym = 0;
17736 bfd_vma symtabno = 0;
17737 bool res = true;
17738
17739 if (! process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
17740 display_mips_gnu_attribute))
17741 res = false;
17742
17743 sect = find_section (filedata, ".MIPS.abiflags");
17744
17745 if (sect != NULL)
17746 {
17747 Elf_External_ABIFlags_v0 *abiflags_ext;
17748 Elf_Internal_ABIFlags_v0 abiflags_in;
17749
17750 if (sizeof (Elf_External_ABIFlags_v0) != sect->sh_size)
17751 {
17752 error (_("Corrupt MIPS ABI Flags section.\n"));
17753 res = false;
17754 }
17755 else
17756 {
17757 abiflags_ext = get_data (NULL, filedata, sect->sh_offset, 1,
17758 sect->sh_size, _("MIPS ABI Flags section"));
17759 if (abiflags_ext)
17760 {
17761 abiflags_in.version = BYTE_GET (abiflags_ext->version);
17762 abiflags_in.isa_level = BYTE_GET (abiflags_ext->isa_level);
17763 abiflags_in.isa_rev = BYTE_GET (abiflags_ext->isa_rev);
17764 abiflags_in.gpr_size = BYTE_GET (abiflags_ext->gpr_size);
17765 abiflags_in.cpr1_size = BYTE_GET (abiflags_ext->cpr1_size);
17766 abiflags_in.cpr2_size = BYTE_GET (abiflags_ext->cpr2_size);
17767 abiflags_in.fp_abi = BYTE_GET (abiflags_ext->fp_abi);
17768 abiflags_in.isa_ext = BYTE_GET (abiflags_ext->isa_ext);
17769 abiflags_in.ases = BYTE_GET (abiflags_ext->ases);
17770 abiflags_in.flags1 = BYTE_GET (abiflags_ext->flags1);
17771 abiflags_in.flags2 = BYTE_GET (abiflags_ext->flags2);
17772
17773 printf ("\nMIPS ABI Flags Version: %d\n", abiflags_in.version);
17774 printf ("\nISA: MIPS%d", abiflags_in.isa_level);
17775 if (abiflags_in.isa_rev > 1)
17776 printf ("r%d", abiflags_in.isa_rev);
17777 printf ("\nGPR size: %d",
17778 get_mips_reg_size (abiflags_in.gpr_size));
17779 printf ("\nCPR1 size: %d",
17780 get_mips_reg_size (abiflags_in.cpr1_size));
17781 printf ("\nCPR2 size: %d",
17782 get_mips_reg_size (abiflags_in.cpr2_size));
17783 fputs ("\nFP ABI: ", stdout);
17784 print_mips_fp_abi_value (abiflags_in.fp_abi);
17785 fputs ("ISA Extension: ", stdout);
17786 print_mips_isa_ext (abiflags_in.isa_ext);
17787 fputs ("\nASEs:", stdout);
17788 print_mips_ases (abiflags_in.ases);
17789 printf ("\nFLAGS 1: %8.8lx", abiflags_in.flags1);
17790 printf ("\nFLAGS 2: %8.8lx", abiflags_in.flags2);
17791 fputc ('\n', stdout);
17792 free (abiflags_ext);
17793 }
17794 }
17795 }
17796
17797 /* We have a lot of special sections. Thanks SGI! */
17798 if (filedata->dynamic_section == NULL)
17799 {
17800 /* No dynamic information available. See if there is static GOT. */
17801 sect = find_section (filedata, ".got");
17802 if (sect != NULL)
17803 {
17804 unsigned char *data_end;
17805 unsigned char *data;
17806 bfd_vma ent, end;
17807 int addr_size;
17808
17809 pltgot = sect->sh_addr;
17810
17811 ent = pltgot;
17812 addr_size = (is_32bit_elf ? 4 : 8);
17813 end = pltgot + sect->sh_size;
17814
17815 data = (unsigned char *) get_data (NULL, filedata, sect->sh_offset,
17816 end - pltgot, 1,
17817 _("Global Offset Table data"));
17818 /* PR 12855: Null data is handled gracefully throughout. */
17819 data_end = data + (end - pltgot);
17820
17821 printf (_("\nStatic GOT:\n"));
17822 printf (_(" Canonical gp value: "));
17823 print_vma (ent + 0x7ff0, LONG_HEX);
17824 printf ("\n\n");
17825
17826 /* In a dynamic binary GOT[0] is reserved for the dynamic
17827 loader to store the lazy resolver pointer, however in
17828 a static binary it may well have been omitted and GOT
17829 reduced to a table of addresses.
17830 PR 21344: Check for the entry being fully available
17831 before fetching it. */
17832 if (data
17833 && data + ent - pltgot + addr_size <= data_end
17834 && byte_get (data + ent - pltgot, addr_size) == 0)
17835 {
17836 printf (_(" Reserved entries:\n"));
17837 printf (_(" %*s %10s %*s\n"),
17838 addr_size * 2, _("Address"), _("Access"),
17839 addr_size * 2, _("Value"));
17840 ent = print_mips_got_entry (data, pltgot, ent, data_end);
17841 printf ("\n");
17842 if (ent == (bfd_vma) -1)
17843 goto sgot_print_fail;
17844
17845 /* Check for the MSB of GOT[1] being set, identifying a
17846 GNU object. This entry will be used by some runtime
17847 loaders, to store the module pointer. Otherwise this
17848 is an ordinary local entry.
17849 PR 21344: Check for the entry being fully available
17850 before fetching it. */
17851 if (data
17852 && data + ent - pltgot + addr_size <= data_end
17853 && (byte_get (data + ent - pltgot, addr_size)
17854 >> (addr_size * 8 - 1)) != 0)
17855 {
17856 ent = print_mips_got_entry (data, pltgot, ent, data_end);
17857 printf ("\n");
17858 if (ent == (bfd_vma) -1)
17859 goto sgot_print_fail;
17860 }
17861 printf ("\n");
17862 }
17863
17864 if (data != NULL && ent < end)
17865 {
17866 printf (_(" Local entries:\n"));
17867 printf (" %*s %10s %*s\n",
17868 addr_size * 2, _("Address"), _("Access"),
17869 addr_size * 2, _("Value"));
17870 while (ent < end)
17871 {
17872 ent = print_mips_got_entry (data, pltgot, ent, data_end);
17873 printf ("\n");
17874 if (ent == (bfd_vma) -1)
17875 goto sgot_print_fail;
17876 }
17877 printf ("\n");
17878 }
17879
17880 sgot_print_fail:
17881 free (data);
17882 }
17883 return res;
17884 }
17885
17886 for (entry = filedata->dynamic_section;
17887 /* PR 17531 file: 012-50589-0.004. */
17888 (entry < filedata->dynamic_section + filedata->dynamic_nent
17889 && entry->d_tag != DT_NULL);
17890 ++entry)
17891 switch (entry->d_tag)
17892 {
17893 case DT_MIPS_LIBLIST:
17894 liblist_offset
17895 = offset_from_vma (filedata, entry->d_un.d_val,
17896 liblistno * sizeof (Elf32_External_Lib));
17897 break;
17898 case DT_MIPS_LIBLISTNO:
17899 liblistno = entry->d_un.d_val;
17900 break;
17901 case DT_MIPS_OPTIONS:
17902 options_offset = offset_from_vma (filedata, entry->d_un.d_val, 0);
17903 break;
17904 case DT_MIPS_CONFLICT:
17905 conflicts_offset
17906 = offset_from_vma (filedata, entry->d_un.d_val,
17907 conflictsno * sizeof (Elf32_External_Conflict));
17908 break;
17909 case DT_MIPS_CONFLICTNO:
17910 conflictsno = entry->d_un.d_val;
17911 break;
17912 case DT_PLTGOT:
17913 pltgot = entry->d_un.d_ptr;
17914 break;
17915 case DT_MIPS_LOCAL_GOTNO:
17916 local_gotno = entry->d_un.d_val;
17917 break;
17918 case DT_MIPS_GOTSYM:
17919 gotsym = entry->d_un.d_val;
17920 break;
17921 case DT_MIPS_SYMTABNO:
17922 symtabno = entry->d_un.d_val;
17923 break;
17924 case DT_MIPS_PLTGOT:
17925 mips_pltgot = entry->d_un.d_ptr;
17926 break;
17927 case DT_PLTREL:
17928 pltrel = entry->d_un.d_val;
17929 break;
17930 case DT_PLTRELSZ:
17931 pltrelsz = entry->d_un.d_val;
17932 break;
17933 case DT_JMPREL:
17934 jmprel = entry->d_un.d_ptr;
17935 break;
17936 default:
17937 break;
17938 }
17939
17940 if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
17941 {
17942 Elf32_External_Lib * elib;
17943 size_t cnt;
17944
17945 elib = (Elf32_External_Lib *) get_data (NULL, filedata, liblist_offset,
17946 sizeof (Elf32_External_Lib),
17947 liblistno,
17948 _("liblist section data"));
17949 if (elib)
17950 {
17951 printf (ngettext ("\nSection '.liblist' contains %lu entry:\n",
17952 "\nSection '.liblist' contains %lu entries:\n",
17953 (unsigned long) liblistno),
17954 (unsigned long) liblistno);
17955 fputs (_(" Library Time Stamp Checksum Version Flags\n"),
17956 stdout);
17957
17958 for (cnt = 0; cnt < liblistno; ++cnt)
17959 {
17960 Elf32_Lib liblist;
17961 time_t atime;
17962 char timebuf[128];
17963 struct tm * tmp;
17964
17965 liblist.l_name = BYTE_GET (elib[cnt].l_name);
17966 atime = BYTE_GET (elib[cnt].l_time_stamp);
17967 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
17968 liblist.l_version = BYTE_GET (elib[cnt].l_version);
17969 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
17970
17971 tmp = gmtime (&atime);
17972 snprintf (timebuf, sizeof (timebuf),
17973 "%04u-%02u-%02uT%02u:%02u:%02u",
17974 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
17975 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
17976
17977 printf ("%3lu: ", (unsigned long) cnt);
17978 if (VALID_DYNAMIC_NAME (filedata, liblist.l_name))
17979 print_symbol (20, GET_DYNAMIC_NAME (filedata, liblist.l_name));
17980 else
17981 printf (_("<corrupt: %9ld>"), liblist.l_name);
17982 printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
17983 liblist.l_version);
17984
17985 if (liblist.l_flags == 0)
17986 puts (_(" NONE"));
17987 else
17988 {
17989 static const struct
17990 {
17991 const char * name;
17992 int bit;
17993 }
17994 l_flags_vals[] =
17995 {
17996 { " EXACT_MATCH", LL_EXACT_MATCH },
17997 { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
17998 { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
17999 { " EXPORTS", LL_EXPORTS },
18000 { " DELAY_LOAD", LL_DELAY_LOAD },
18001 { " DELTA", LL_DELTA }
18002 };
18003 int flags = liblist.l_flags;
18004 size_t fcnt;
18005
18006 for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
18007 if ((flags & l_flags_vals[fcnt].bit) != 0)
18008 {
18009 fputs (l_flags_vals[fcnt].name, stdout);
18010 flags ^= l_flags_vals[fcnt].bit;
18011 }
18012 if (flags != 0)
18013 printf (" %#x", (unsigned int) flags);
18014
18015 puts ("");
18016 }
18017 }
18018
18019 free (elib);
18020 }
18021 else
18022 res = false;
18023 }
18024
18025 if (options_offset != 0)
18026 {
18027 Elf_External_Options * eopt;
18028 size_t offset;
18029 int cnt;
18030 sect = filedata->section_headers;
18031
18032 /* Find the section header so that we get the size. */
18033 sect = find_section_by_type (filedata, SHT_MIPS_OPTIONS);
18034 /* PR 17533 file: 012-277276-0.004. */
18035 if (sect == NULL)
18036 {
18037 error (_("No MIPS_OPTIONS header found\n"));
18038 return false;
18039 }
18040 /* PR 24243 */
18041 if (sect->sh_size < sizeof (* eopt))
18042 {
18043 error (_("The MIPS options section is too small.\n"));
18044 return false;
18045 }
18046
18047 eopt = (Elf_External_Options *) get_data (NULL, filedata, options_offset, 1,
18048 sect->sh_size, _("options"));
18049 if (eopt)
18050 {
18051 Elf_Internal_Options option;
18052
18053 offset = cnt = 0;
18054 while (offset <= sect->sh_size - sizeof (* eopt))
18055 {
18056 Elf_External_Options * eoption;
18057 unsigned int optsize;
18058
18059 eoption = (Elf_External_Options *) ((char *) eopt + offset);
18060
18061 optsize = BYTE_GET (eoption->size);
18062
18063 /* PR 17531: file: ffa0fa3b. */
18064 if (optsize < sizeof (* eopt)
18065 || optsize > sect->sh_size - offset)
18066 {
18067 error (_("Invalid size (%u) for MIPS option\n"),
18068 optsize);
18069 free (eopt);
18070 return false;
18071 }
18072 offset += optsize;
18073 ++cnt;
18074 }
18075
18076 printf (ngettext ("\nSection '%s' contains %d entry:\n",
18077 "\nSection '%s' contains %d entries:\n",
18078 cnt),
18079 printable_section_name (filedata, sect), cnt);
18080
18081 offset = 0;
18082 while (cnt-- > 0)
18083 {
18084 size_t len;
18085 Elf_External_Options * eoption;
18086
18087 eoption = (Elf_External_Options *) ((char *) eopt + offset);
18088
18089 option.kind = BYTE_GET (eoption->kind);
18090 option.size = BYTE_GET (eoption->size);
18091 option.section = BYTE_GET (eoption->section);
18092 option.info = BYTE_GET (eoption->info);
18093
18094 switch (option.kind)
18095 {
18096 case ODK_NULL:
18097 /* This shouldn't happen. */
18098 printf (" NULL %" PRId16 " %" PRIx32,
18099 option.section, option.info);
18100 break;
18101
18102 case ODK_REGINFO:
18103 printf (" REGINFO ");
18104 if (filedata->file_header.e_machine == EM_MIPS)
18105 {
18106 Elf32_External_RegInfo * ereg;
18107 Elf32_RegInfo reginfo;
18108
18109 /* 32bit form. */
18110 if (option.size < (sizeof (Elf_External_Options)
18111 + sizeof (Elf32_External_RegInfo)))
18112 {
18113 printf (_("<corrupt>\n"));
18114 error (_("Truncated MIPS REGINFO option\n"));
18115 cnt = 0;
18116 break;
18117 }
18118
18119 ereg = (Elf32_External_RegInfo *) (eoption + 1);
18120
18121 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
18122 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
18123 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
18124 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
18125 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
18126 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
18127
18128 printf ("GPR %08" PRIx32 " GP 0x%" PRIx32 "\n",
18129 reginfo.ri_gprmask, reginfo.ri_gp_value);
18130 printf (" "
18131 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
18132 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
18133 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
18134 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
18135 }
18136 else
18137 {
18138 /* 64 bit form. */
18139 Elf64_External_RegInfo * ereg;
18140 Elf64_Internal_RegInfo reginfo;
18141
18142 if (option.size < (sizeof (Elf_External_Options)
18143 + sizeof (Elf64_External_RegInfo)))
18144 {
18145 printf (_("<corrupt>\n"));
18146 error (_("Truncated MIPS REGINFO option\n"));
18147 cnt = 0;
18148 break;
18149 }
18150
18151 ereg = (Elf64_External_RegInfo *) (eoption + 1);
18152 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
18153 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
18154 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
18155 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
18156 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
18157 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
18158
18159 printf ("GPR %08" PRIx32 " GP 0x%" PRIx64 "\n",
18160 reginfo.ri_gprmask, reginfo.ri_gp_value);
18161 printf (" "
18162 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
18163 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
18164 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
18165 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
18166 }
18167 offset += option.size;
18168 continue;
18169
18170 case ODK_EXCEPTIONS:
18171 fputs (" EXCEPTIONS fpe_min(", stdout);
18172 process_mips_fpe_exception (option.info & OEX_FPU_MIN);
18173 fputs (") fpe_max(", stdout);
18174 process_mips_fpe_exception ((option.info & OEX_FPU_MAX) >> 8);
18175 fputs (")", stdout);
18176
18177 if (option.info & OEX_PAGE0)
18178 fputs (" PAGE0", stdout);
18179 if (option.info & OEX_SMM)
18180 fputs (" SMM", stdout);
18181 if (option.info & OEX_FPDBUG)
18182 fputs (" FPDBUG", stdout);
18183 if (option.info & OEX_DISMISS)
18184 fputs (" DISMISS", stdout);
18185 break;
18186
18187 case ODK_PAD:
18188 fputs (" PAD ", stdout);
18189 if (option.info & OPAD_PREFIX)
18190 fputs (" PREFIX", stdout);
18191 if (option.info & OPAD_POSTFIX)
18192 fputs (" POSTFIX", stdout);
18193 if (option.info & OPAD_SYMBOL)
18194 fputs (" SYMBOL", stdout);
18195 break;
18196
18197 case ODK_HWPATCH:
18198 fputs (" HWPATCH ", stdout);
18199 if (option.info & OHW_R4KEOP)
18200 fputs (" R4KEOP", stdout);
18201 if (option.info & OHW_R8KPFETCH)
18202 fputs (" R8KPFETCH", stdout);
18203 if (option.info & OHW_R5KEOP)
18204 fputs (" R5KEOP", stdout);
18205 if (option.info & OHW_R5KCVTL)
18206 fputs (" R5KCVTL", stdout);
18207 break;
18208
18209 case ODK_FILL:
18210 fputs (" FILL ", stdout);
18211 /* XXX Print content of info word? */
18212 break;
18213
18214 case ODK_TAGS:
18215 fputs (" TAGS ", stdout);
18216 /* XXX Print content of info word? */
18217 break;
18218
18219 case ODK_HWAND:
18220 fputs (" HWAND ", stdout);
18221 if (option.info & OHWA0_R4KEOP_CHECKED)
18222 fputs (" R4KEOP_CHECKED", stdout);
18223 if (option.info & OHWA0_R4KEOP_CLEAN)
18224 fputs (" R4KEOP_CLEAN", stdout);
18225 break;
18226
18227 case ODK_HWOR:
18228 fputs (" HWOR ", stdout);
18229 if (option.info & OHWA0_R4KEOP_CHECKED)
18230 fputs (" R4KEOP_CHECKED", stdout);
18231 if (option.info & OHWA0_R4KEOP_CLEAN)
18232 fputs (" R4KEOP_CLEAN", stdout);
18233 break;
18234
18235 case ODK_GP_GROUP:
18236 printf (" GP_GROUP %#06x self-contained %#06x",
18237 option.info & OGP_GROUP,
18238 (option.info & OGP_SELF) >> 16);
18239 break;
18240
18241 case ODK_IDENT:
18242 printf (" IDENT %#06x self-contained %#06x",
18243 option.info & OGP_GROUP,
18244 (option.info & OGP_SELF) >> 16);
18245 break;
18246
18247 default:
18248 /* This shouldn't happen. */
18249 printf (" %3d ??? %" PRId16 " %" PRIx32,
18250 option.kind, option.section, option.info);
18251 break;
18252 }
18253
18254 len = sizeof (* eopt);
18255 while (len < option.size)
18256 {
18257 unsigned char datum = *((unsigned char *) eoption + len);
18258
18259 if (ISPRINT (datum))
18260 printf ("%c", datum);
18261 else
18262 printf ("\\%03o", datum);
18263 len ++;
18264 }
18265 fputs ("\n", stdout);
18266
18267 offset += option.size;
18268 }
18269 free (eopt);
18270 }
18271 else
18272 res = false;
18273 }
18274
18275 if (conflicts_offset != 0 && conflictsno != 0)
18276 {
18277 Elf32_Conflict * iconf;
18278 size_t cnt;
18279
18280 if (filedata->dynamic_symbols == NULL)
18281 {
18282 error (_("conflict list found without a dynamic symbol table\n"));
18283 return false;
18284 }
18285
18286 /* PR 21345 - print a slightly more helpful error message
18287 if we are sure that the cmalloc will fail. */
18288 if (conflictsno > filedata->file_size / sizeof (* iconf))
18289 {
18290 error (_("Overlarge number of conflicts detected: %lx\n"),
18291 (long) conflictsno);
18292 return false;
18293 }
18294
18295 iconf = (Elf32_Conflict *) cmalloc (conflictsno, sizeof (* iconf));
18296 if (iconf == NULL)
18297 {
18298 error (_("Out of memory allocating space for dynamic conflicts\n"));
18299 return false;
18300 }
18301
18302 if (is_32bit_elf)
18303 {
18304 Elf32_External_Conflict * econf32;
18305
18306 econf32 = (Elf32_External_Conflict *)
18307 get_data (NULL, filedata, conflicts_offset,
18308 sizeof (*econf32), conflictsno, _("conflict"));
18309 if (!econf32)
18310 {
18311 free (iconf);
18312 return false;
18313 }
18314
18315 for (cnt = 0; cnt < conflictsno; ++cnt)
18316 iconf[cnt] = BYTE_GET (econf32[cnt]);
18317
18318 free (econf32);
18319 }
18320 else
18321 {
18322 Elf64_External_Conflict * econf64;
18323
18324 econf64 = (Elf64_External_Conflict *)
18325 get_data (NULL, filedata, conflicts_offset,
18326 sizeof (*econf64), conflictsno, _("conflict"));
18327 if (!econf64)
18328 {
18329 free (iconf);
18330 return false;
18331 }
18332
18333 for (cnt = 0; cnt < conflictsno; ++cnt)
18334 iconf[cnt] = BYTE_GET (econf64[cnt]);
18335
18336 free (econf64);
18337 }
18338
18339 printf (ngettext ("\nSection '.conflict' contains %lu entry:\n",
18340 "\nSection '.conflict' contains %lu entries:\n",
18341 (unsigned long) conflictsno),
18342 (unsigned long) conflictsno);
18343 puts (_(" Num: Index Value Name"));
18344
18345 for (cnt = 0; cnt < conflictsno; ++cnt)
18346 {
18347 printf ("%5lu: %8lu ", (unsigned long) cnt, iconf[cnt]);
18348
18349 if (iconf[cnt] >= filedata->num_dynamic_syms)
18350 printf (_("<corrupt symbol index>"));
18351 else
18352 {
18353 Elf_Internal_Sym * psym;
18354
18355 psym = & filedata->dynamic_symbols[iconf[cnt]];
18356 print_vma (psym->st_value, FULL_HEX);
18357 putchar (' ');
18358 if (VALID_DYNAMIC_NAME (filedata, psym->st_name))
18359 print_symbol (25, GET_DYNAMIC_NAME (filedata, psym->st_name));
18360 else
18361 printf (_("<corrupt: %14ld>"), psym->st_name);
18362 }
18363 putchar ('\n');
18364 }
18365
18366 free (iconf);
18367 }
18368
18369 if (pltgot != 0 && local_gotno != 0)
18370 {
18371 bfd_vma ent, local_end, global_end;
18372 size_t i, offset;
18373 unsigned char * data;
18374 unsigned char * data_end;
18375 int addr_size;
18376
18377 ent = pltgot;
18378 addr_size = (is_32bit_elf ? 4 : 8);
18379 local_end = pltgot + local_gotno * addr_size;
18380
18381 /* PR binutils/17533 file: 012-111227-0.004 */
18382 if (symtabno < gotsym)
18383 {
18384 error (_("The GOT symbol offset (%lu) is greater than the symbol table size (%lu)\n"),
18385 (unsigned long) gotsym, (unsigned long) symtabno);
18386 return false;
18387 }
18388
18389 global_end = local_end + (symtabno - gotsym) * addr_size;
18390 /* PR 17531: file: 54c91a34. */
18391 if (global_end < local_end)
18392 {
18393 error (_("Too many GOT symbols: %lu\n"), (unsigned long) symtabno);
18394 return false;
18395 }
18396
18397 offset = offset_from_vma (filedata, pltgot, global_end - pltgot);
18398 data = (unsigned char *) get_data (NULL, filedata, offset,
18399 global_end - pltgot, 1,
18400 _("Global Offset Table data"));
18401 /* PR 12855: Null data is handled gracefully throughout. */
18402 data_end = data + (global_end - pltgot);
18403
18404 printf (_("\nPrimary GOT:\n"));
18405 printf (_(" Canonical gp value: "));
18406 print_vma (pltgot + 0x7ff0, LONG_HEX);
18407 printf ("\n\n");
18408
18409 printf (_(" Reserved entries:\n"));
18410 printf (_(" %*s %10s %*s Purpose\n"),
18411 addr_size * 2, _("Address"), _("Access"),
18412 addr_size * 2, _("Initial"));
18413 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18414 printf (_(" Lazy resolver\n"));
18415 if (ent == (bfd_vma) -1)
18416 goto got_print_fail;
18417
18418 /* Check for the MSB of GOT[1] being set, denoting a GNU object.
18419 This entry will be used by some runtime loaders, to store the
18420 module pointer. Otherwise this is an ordinary local entry.
18421 PR 21344: Check for the entry being fully available before
18422 fetching it. */
18423 if (data
18424 && data + ent - pltgot + addr_size <= data_end
18425 && (byte_get (data + ent - pltgot, addr_size)
18426 >> (addr_size * 8 - 1)) != 0)
18427 {
18428 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18429 printf (_(" Module pointer (GNU extension)\n"));
18430 if (ent == (bfd_vma) -1)
18431 goto got_print_fail;
18432 }
18433 printf ("\n");
18434
18435 if (data != NULL && ent < local_end)
18436 {
18437 printf (_(" Local entries:\n"));
18438 printf (" %*s %10s %*s\n",
18439 addr_size * 2, _("Address"), _("Access"),
18440 addr_size * 2, _("Initial"));
18441 while (ent < local_end)
18442 {
18443 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18444 printf ("\n");
18445 if (ent == (bfd_vma) -1)
18446 goto got_print_fail;
18447 }
18448 printf ("\n");
18449 }
18450
18451 if (data != NULL && gotsym < symtabno)
18452 {
18453 int sym_width;
18454
18455 printf (_(" Global entries:\n"));
18456 printf (" %*s %10s %*s %*s %-7s %3s %s\n",
18457 addr_size * 2, _("Address"),
18458 _("Access"),
18459 addr_size * 2, _("Initial"),
18460 addr_size * 2, _("Sym.Val."),
18461 _("Type"),
18462 /* Note for translators: "Ndx" = abbreviated form of "Index". */
18463 _("Ndx"), _("Name"));
18464
18465 sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
18466
18467 for (i = gotsym; i < symtabno; i++)
18468 {
18469 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18470 printf (" ");
18471
18472 if (filedata->dynamic_symbols == NULL)
18473 printf (_("<no dynamic symbols>"));
18474 else if (i < filedata->num_dynamic_syms)
18475 {
18476 Elf_Internal_Sym * psym = filedata->dynamic_symbols + i;
18477
18478 print_vma (psym->st_value, LONG_HEX);
18479 printf (" %-7s %3s ",
18480 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
18481 get_symbol_index_type (filedata, psym->st_shndx));
18482
18483 if (VALID_DYNAMIC_NAME (filedata, psym->st_name))
18484 print_symbol (sym_width,
18485 GET_DYNAMIC_NAME (filedata, psym->st_name));
18486 else
18487 printf (_("<corrupt: %14ld>"), psym->st_name);
18488 }
18489 else
18490 printf (_("<symbol index %lu exceeds number of dynamic symbols>"),
18491 (unsigned long) i);
18492
18493 printf ("\n");
18494 if (ent == (bfd_vma) -1)
18495 break;
18496 }
18497 printf ("\n");
18498 }
18499
18500 got_print_fail:
18501 free (data);
18502 }
18503
18504 if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
18505 {
18506 bfd_vma ent, end;
18507 size_t offset, rel_offset;
18508 unsigned long count, i;
18509 unsigned char * data;
18510 int addr_size, sym_width;
18511 Elf_Internal_Rela * rels;
18512
18513 rel_offset = offset_from_vma (filedata, jmprel, pltrelsz);
18514 if (pltrel == DT_RELA)
18515 {
18516 if (!slurp_rela_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
18517 return false;
18518 }
18519 else
18520 {
18521 if (!slurp_rel_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
18522 return false;
18523 }
18524
18525 ent = mips_pltgot;
18526 addr_size = (is_32bit_elf ? 4 : 8);
18527 end = mips_pltgot + (2 + count) * addr_size;
18528
18529 offset = offset_from_vma (filedata, mips_pltgot, end - mips_pltgot);
18530 data = (unsigned char *) get_data (NULL, filedata, offset, end - mips_pltgot,
18531 1, _("Procedure Linkage Table data"));
18532 if (data == NULL)
18533 {
18534 free (rels);
18535 return false;
18536 }
18537
18538 printf ("\nPLT GOT:\n\n");
18539 printf (_(" Reserved entries:\n"));
18540 printf (_(" %*s %*s Purpose\n"),
18541 addr_size * 2, _("Address"), addr_size * 2, _("Initial"));
18542 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
18543 printf (_(" PLT lazy resolver\n"));
18544 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
18545 printf (_(" Module pointer\n"));
18546 printf ("\n");
18547
18548 printf (_(" Entries:\n"));
18549 printf (" %*s %*s %*s %-7s %3s %s\n",
18550 addr_size * 2, _("Address"),
18551 addr_size * 2, _("Initial"),
18552 addr_size * 2, _("Sym.Val."), _("Type"), _("Ndx"), _("Name"));
18553 sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
18554 for (i = 0; i < count; i++)
18555 {
18556 unsigned long idx = get_reloc_symindex (rels[i].r_info);
18557
18558 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
18559 printf (" ");
18560
18561 if (idx >= filedata->num_dynamic_syms)
18562 printf (_("<corrupt symbol index: %lu>"), idx);
18563 else
18564 {
18565 Elf_Internal_Sym * psym = filedata->dynamic_symbols + idx;
18566
18567 print_vma (psym->st_value, LONG_HEX);
18568 printf (" %-7s %3s ",
18569 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
18570 get_symbol_index_type (filedata, psym->st_shndx));
18571 if (VALID_DYNAMIC_NAME (filedata, psym->st_name))
18572 print_symbol (sym_width,
18573 GET_DYNAMIC_NAME (filedata, psym->st_name));
18574 else
18575 printf (_("<corrupt: %14ld>"), psym->st_name);
18576 }
18577 printf ("\n");
18578 }
18579 printf ("\n");
18580
18581 free (data);
18582 free (rels);
18583 }
18584
18585 return res;
18586 }
18587
18588 static bool
18589 process_nds32_specific (Filedata * filedata)
18590 {
18591 Elf_Internal_Shdr *sect = NULL;
18592
18593 sect = find_section (filedata, ".nds32_e_flags");
18594 if (sect != NULL && sect->sh_size >= 4)
18595 {
18596 unsigned char *buf;
18597 unsigned int flag;
18598
18599 printf ("\nNDS32 elf flags section:\n");
18600 buf = get_data (NULL, filedata, sect->sh_offset, 1, 4,
18601 _("NDS32 elf flags section"));
18602
18603 if (buf == NULL)
18604 return false;
18605
18606 flag = byte_get (buf, 4);
18607 free (buf);
18608 switch (flag & 0x3)
18609 {
18610 case 0:
18611 printf ("(VEC_SIZE):\tNo entry.\n");
18612 break;
18613 case 1:
18614 printf ("(VEC_SIZE):\t4 bytes\n");
18615 break;
18616 case 2:
18617 printf ("(VEC_SIZE):\t16 bytes\n");
18618 break;
18619 case 3:
18620 printf ("(VEC_SIZE):\treserved\n");
18621 break;
18622 }
18623 }
18624
18625 return true;
18626 }
18627
18628 static bool
18629 process_gnu_liblist (Filedata * filedata)
18630 {
18631 Elf_Internal_Shdr * section;
18632 Elf_Internal_Shdr * string_sec;
18633 Elf32_External_Lib * elib;
18634 char * strtab;
18635 size_t strtab_size;
18636 size_t cnt;
18637 unsigned long num_liblist;
18638 unsigned i;
18639 bool res = true;
18640
18641 if (! do_arch)
18642 return true;
18643
18644 for (i = 0, section = filedata->section_headers;
18645 i < filedata->file_header.e_shnum;
18646 i++, section++)
18647 {
18648 switch (section->sh_type)
18649 {
18650 case SHT_GNU_LIBLIST:
18651 if (section->sh_link >= filedata->file_header.e_shnum)
18652 break;
18653
18654 elib = (Elf32_External_Lib *)
18655 get_data (NULL, filedata, section->sh_offset, 1, section->sh_size,
18656 _("liblist section data"));
18657
18658 if (elib == NULL)
18659 {
18660 res = false;
18661 break;
18662 }
18663
18664 string_sec = filedata->section_headers + section->sh_link;
18665 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
18666 string_sec->sh_size,
18667 _("liblist string table"));
18668 if (strtab == NULL
18669 || section->sh_entsize != sizeof (Elf32_External_Lib))
18670 {
18671 free (elib);
18672 free (strtab);
18673 res = false;
18674 break;
18675 }
18676 strtab_size = string_sec->sh_size;
18677
18678 num_liblist = section->sh_size / sizeof (Elf32_External_Lib);
18679 printf (ngettext ("\nLibrary list section '%s' contains %lu entries:\n",
18680 "\nLibrary list section '%s' contains %lu entries:\n",
18681 num_liblist),
18682 printable_section_name (filedata, section),
18683 num_liblist);
18684
18685 puts (_(" Library Time Stamp Checksum Version Flags"));
18686
18687 for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
18688 ++cnt)
18689 {
18690 Elf32_Lib liblist;
18691 time_t atime;
18692 char timebuf[128];
18693 struct tm * tmp;
18694
18695 liblist.l_name = BYTE_GET (elib[cnt].l_name);
18696 atime = BYTE_GET (elib[cnt].l_time_stamp);
18697 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
18698 liblist.l_version = BYTE_GET (elib[cnt].l_version);
18699 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
18700
18701 tmp = gmtime (&atime);
18702 snprintf (timebuf, sizeof (timebuf),
18703 "%04u-%02u-%02uT%02u:%02u:%02u",
18704 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
18705 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
18706
18707 printf ("%3lu: ", (unsigned long) cnt);
18708 if (do_wide)
18709 printf ("%-20s", liblist.l_name < strtab_size
18710 ? strtab + liblist.l_name : _("<corrupt>"));
18711 else
18712 printf ("%-20.20s", liblist.l_name < strtab_size
18713 ? strtab + liblist.l_name : _("<corrupt>"));
18714 printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
18715 liblist.l_version, liblist.l_flags);
18716 }
18717
18718 free (elib);
18719 free (strtab);
18720 }
18721 }
18722
18723 return res;
18724 }
18725
18726 static const char *
18727 get_note_type (Filedata * filedata, unsigned e_type)
18728 {
18729 static char buff[64];
18730
18731 if (filedata->file_header.e_type == ET_CORE)
18732 switch (e_type)
18733 {
18734 case NT_AUXV:
18735 return _("NT_AUXV (auxiliary vector)");
18736 case NT_PRSTATUS:
18737 return _("NT_PRSTATUS (prstatus structure)");
18738 case NT_FPREGSET:
18739 return _("NT_FPREGSET (floating point registers)");
18740 case NT_PRPSINFO:
18741 return _("NT_PRPSINFO (prpsinfo structure)");
18742 case NT_TASKSTRUCT:
18743 return _("NT_TASKSTRUCT (task structure)");
18744 case NT_GDB_TDESC:
18745 return _("NT_GDB_TDESC (GDB XML target description)");
18746 case NT_PRXFPREG:
18747 return _("NT_PRXFPREG (user_xfpregs structure)");
18748 case NT_PPC_VMX:
18749 return _("NT_PPC_VMX (ppc Altivec registers)");
18750 case NT_PPC_VSX:
18751 return _("NT_PPC_VSX (ppc VSX registers)");
18752 case NT_PPC_TAR:
18753 return _("NT_PPC_TAR (ppc TAR register)");
18754 case NT_PPC_PPR:
18755 return _("NT_PPC_PPR (ppc PPR register)");
18756 case NT_PPC_DSCR:
18757 return _("NT_PPC_DSCR (ppc DSCR register)");
18758 case NT_PPC_EBB:
18759 return _("NT_PPC_EBB (ppc EBB registers)");
18760 case NT_PPC_PMU:
18761 return _("NT_PPC_PMU (ppc PMU registers)");
18762 case NT_PPC_TM_CGPR:
18763 return _("NT_PPC_TM_CGPR (ppc checkpointed GPR registers)");
18764 case NT_PPC_TM_CFPR:
18765 return _("NT_PPC_TM_CFPR (ppc checkpointed floating point registers)");
18766 case NT_PPC_TM_CVMX:
18767 return _("NT_PPC_TM_CVMX (ppc checkpointed Altivec registers)");
18768 case NT_PPC_TM_CVSX:
18769 return _("NT_PPC_TM_CVSX (ppc checkpointed VSX registers)");
18770 case NT_PPC_TM_SPR:
18771 return _("NT_PPC_TM_SPR (ppc TM special purpose registers)");
18772 case NT_PPC_TM_CTAR:
18773 return _("NT_PPC_TM_CTAR (ppc checkpointed TAR register)");
18774 case NT_PPC_TM_CPPR:
18775 return _("NT_PPC_TM_CPPR (ppc checkpointed PPR register)");
18776 case NT_PPC_TM_CDSCR:
18777 return _("NT_PPC_TM_CDSCR (ppc checkpointed DSCR register)");
18778 case NT_386_TLS:
18779 return _("NT_386_TLS (x86 TLS information)");
18780 case NT_386_IOPERM:
18781 return _("NT_386_IOPERM (x86 I/O permissions)");
18782 case NT_X86_XSTATE:
18783 return _("NT_X86_XSTATE (x86 XSAVE extended state)");
18784 case NT_X86_CET:
18785 return _("NT_X86_CET (x86 CET state)");
18786 case NT_S390_HIGH_GPRS:
18787 return _("NT_S390_HIGH_GPRS (s390 upper register halves)");
18788 case NT_S390_TIMER:
18789 return _("NT_S390_TIMER (s390 timer register)");
18790 case NT_S390_TODCMP:
18791 return _("NT_S390_TODCMP (s390 TOD comparator register)");
18792 case NT_S390_TODPREG:
18793 return _("NT_S390_TODPREG (s390 TOD programmable register)");
18794 case NT_S390_CTRS:
18795 return _("NT_S390_CTRS (s390 control registers)");
18796 case NT_S390_PREFIX:
18797 return _("NT_S390_PREFIX (s390 prefix register)");
18798 case NT_S390_LAST_BREAK:
18799 return _("NT_S390_LAST_BREAK (s390 last breaking event address)");
18800 case NT_S390_SYSTEM_CALL:
18801 return _("NT_S390_SYSTEM_CALL (s390 system call restart data)");
18802 case NT_S390_TDB:
18803 return _("NT_S390_TDB (s390 transaction diagnostic block)");
18804 case NT_S390_VXRS_LOW:
18805 return _("NT_S390_VXRS_LOW (s390 vector registers 0-15 upper half)");
18806 case NT_S390_VXRS_HIGH:
18807 return _("NT_S390_VXRS_HIGH (s390 vector registers 16-31)");
18808 case NT_S390_GS_CB:
18809 return _("NT_S390_GS_CB (s390 guarded-storage registers)");
18810 case NT_S390_GS_BC:
18811 return _("NT_S390_GS_BC (s390 guarded-storage broadcast control)");
18812 case NT_ARM_VFP:
18813 return _("NT_ARM_VFP (arm VFP registers)");
18814 case NT_ARM_TLS:
18815 return _("NT_ARM_TLS (AArch TLS registers)");
18816 case NT_ARM_HW_BREAK:
18817 return _("NT_ARM_HW_BREAK (AArch hardware breakpoint registers)");
18818 case NT_ARM_HW_WATCH:
18819 return _("NT_ARM_HW_WATCH (AArch hardware watchpoint registers)");
18820 case NT_ARM_SVE:
18821 return _("NT_ARM_SVE (AArch SVE registers)");
18822 case NT_ARM_PAC_MASK:
18823 return _("NT_ARM_PAC_MASK (AArch pointer authentication code masks)");
18824 case NT_ARM_TAGGED_ADDR_CTRL:
18825 return _("NT_ARM_TAGGED_ADDR_CTRL (AArch tagged address control)");
18826 case NT_ARC_V2:
18827 return _("NT_ARC_V2 (ARC HS accumulator/extra registers)");
18828 case NT_RISCV_CSR:
18829 return _("NT_RISCV_CSR (RISC-V control and status registers)");
18830 case NT_PSTATUS:
18831 return _("NT_PSTATUS (pstatus structure)");
18832 case NT_FPREGS:
18833 return _("NT_FPREGS (floating point registers)");
18834 case NT_PSINFO:
18835 return _("NT_PSINFO (psinfo structure)");
18836 case NT_LWPSTATUS:
18837 return _("NT_LWPSTATUS (lwpstatus_t structure)");
18838 case NT_LWPSINFO:
18839 return _("NT_LWPSINFO (lwpsinfo_t structure)");
18840 case NT_WIN32PSTATUS:
18841 return _("NT_WIN32PSTATUS (win32_pstatus structure)");
18842 case NT_SIGINFO:
18843 return _("NT_SIGINFO (siginfo_t data)");
18844 case NT_FILE:
18845 return _("NT_FILE (mapped files)");
18846 case NT_MEMTAG:
18847 return _("NT_MEMTAG (memory tags)");
18848 default:
18849 break;
18850 }
18851 else
18852 switch (e_type)
18853 {
18854 case NT_VERSION:
18855 return _("NT_VERSION (version)");
18856 case NT_ARCH:
18857 return _("NT_ARCH (architecture)");
18858 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
18859 return _("OPEN");
18860 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
18861 return _("func");
18862 case NT_GO_BUILDID:
18863 return _("GO BUILDID");
18864 default:
18865 break;
18866 }
18867
18868 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
18869 return buff;
18870 }
18871
18872 static bool
18873 print_core_note (Elf_Internal_Note *pnote)
18874 {
18875 unsigned int addr_size = is_32bit_elf ? 4 : 8;
18876 bfd_vma count, page_size;
18877 unsigned char *descdata, *filenames, *descend;
18878
18879 if (pnote->type != NT_FILE)
18880 {
18881 if (do_wide)
18882 printf ("\n");
18883 return true;
18884 }
18885
18886 #ifndef BFD64
18887 if (!is_32bit_elf)
18888 {
18889 printf (_(" Cannot decode 64-bit note in 32-bit build\n"));
18890 /* Still "successful". */
18891 return true;
18892 }
18893 #endif
18894
18895 if (pnote->descsz < 2 * addr_size)
18896 {
18897 error (_(" Malformed note - too short for header\n"));
18898 return false;
18899 }
18900
18901 descdata = (unsigned char *) pnote->descdata;
18902 descend = descdata + pnote->descsz;
18903
18904 if (descdata[pnote->descsz - 1] != '\0')
18905 {
18906 error (_(" Malformed note - does not end with \\0\n"));
18907 return false;
18908 }
18909
18910 count = byte_get (descdata, addr_size);
18911 descdata += addr_size;
18912
18913 page_size = byte_get (descdata, addr_size);
18914 descdata += addr_size;
18915
18916 if (count > ((bfd_vma) -1 - 2 * addr_size) / (3 * addr_size)
18917 || pnote->descsz < 2 * addr_size + count * 3 * addr_size)
18918 {
18919 error (_(" Malformed note - too short for supplied file count\n"));
18920 return false;
18921 }
18922
18923 printf (_(" Page size: "));
18924 print_vma (page_size, DEC);
18925 printf ("\n");
18926
18927 printf (_(" %*s%*s%*s\n"),
18928 (int) (2 + 2 * addr_size), _("Start"),
18929 (int) (4 + 2 * addr_size), _("End"),
18930 (int) (4 + 2 * addr_size), _("Page Offset"));
18931 filenames = descdata + count * 3 * addr_size;
18932 while (count-- > 0)
18933 {
18934 bfd_vma start, end, file_ofs;
18935
18936 if (filenames == descend)
18937 {
18938 error (_(" Malformed note - filenames end too early\n"));
18939 return false;
18940 }
18941
18942 start = byte_get (descdata, addr_size);
18943 descdata += addr_size;
18944 end = byte_get (descdata, addr_size);
18945 descdata += addr_size;
18946 file_ofs = byte_get (descdata, addr_size);
18947 descdata += addr_size;
18948
18949 printf (" ");
18950 print_vma (start, FULL_HEX);
18951 printf (" ");
18952 print_vma (end, FULL_HEX);
18953 printf (" ");
18954 print_vma (file_ofs, FULL_HEX);
18955 printf ("\n %s\n", filenames);
18956
18957 filenames += 1 + strlen ((char *) filenames);
18958 }
18959
18960 return true;
18961 }
18962
18963 static const char *
18964 get_gnu_elf_note_type (unsigned e_type)
18965 {
18966 /* NB/ Keep this switch statement in sync with print_gnu_note (). */
18967 switch (e_type)
18968 {
18969 case NT_GNU_ABI_TAG:
18970 return _("NT_GNU_ABI_TAG (ABI version tag)");
18971 case NT_GNU_HWCAP:
18972 return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
18973 case NT_GNU_BUILD_ID:
18974 return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
18975 case NT_GNU_GOLD_VERSION:
18976 return _("NT_GNU_GOLD_VERSION (gold version)");
18977 case NT_GNU_PROPERTY_TYPE_0:
18978 return _("NT_GNU_PROPERTY_TYPE_0");
18979 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
18980 return _("NT_GNU_BUILD_ATTRIBUTE_OPEN");
18981 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
18982 return _("NT_GNU_BUILD_ATTRIBUTE_FUNC");
18983 default:
18984 {
18985 static char buff[64];
18986
18987 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
18988 return buff;
18989 }
18990 }
18991 }
18992
18993 static void
18994 decode_x86_compat_isa (unsigned int bitmask)
18995 {
18996 while (bitmask)
18997 {
18998 unsigned int bit = bitmask & (- bitmask);
18999
19000 bitmask &= ~ bit;
19001 switch (bit)
19002 {
19003 case GNU_PROPERTY_X86_COMPAT_ISA_1_486:
19004 printf ("i486");
19005 break;
19006 case GNU_PROPERTY_X86_COMPAT_ISA_1_586:
19007 printf ("586");
19008 break;
19009 case GNU_PROPERTY_X86_COMPAT_ISA_1_686:
19010 printf ("686");
19011 break;
19012 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE:
19013 printf ("SSE");
19014 break;
19015 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE2:
19016 printf ("SSE2");
19017 break;
19018 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE3:
19019 printf ("SSE3");
19020 break;
19021 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSSE3:
19022 printf ("SSSE3");
19023 break;
19024 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_1:
19025 printf ("SSE4_1");
19026 break;
19027 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_2:
19028 printf ("SSE4_2");
19029 break;
19030 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX:
19031 printf ("AVX");
19032 break;
19033 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX2:
19034 printf ("AVX2");
19035 break;
19036 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512F:
19037 printf ("AVX512F");
19038 break;
19039 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512CD:
19040 printf ("AVX512CD");
19041 break;
19042 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512ER:
19043 printf ("AVX512ER");
19044 break;
19045 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512PF:
19046 printf ("AVX512PF");
19047 break;
19048 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512VL:
19049 printf ("AVX512VL");
19050 break;
19051 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512DQ:
19052 printf ("AVX512DQ");
19053 break;
19054 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512BW:
19055 printf ("AVX512BW");
19056 break;
19057 default:
19058 printf (_("<unknown: %x>"), bit);
19059 break;
19060 }
19061 if (bitmask)
19062 printf (", ");
19063 }
19064 }
19065
19066 static void
19067 decode_x86_compat_2_isa (unsigned int bitmask)
19068 {
19069 if (!bitmask)
19070 {
19071 printf (_("<None>"));
19072 return;
19073 }
19074
19075 while (bitmask)
19076 {
19077 unsigned int bit = bitmask & (- bitmask);
19078
19079 bitmask &= ~ bit;
19080 switch (bit)
19081 {
19082 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_CMOV:
19083 printf ("CMOV");
19084 break;
19085 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE:
19086 printf ("SSE");
19087 break;
19088 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE2:
19089 printf ("SSE2");
19090 break;
19091 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE3:
19092 printf ("SSE3");
19093 break;
19094 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSSE3:
19095 printf ("SSSE3");
19096 break;
19097 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_1:
19098 printf ("SSE4_1");
19099 break;
19100 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_2:
19101 printf ("SSE4_2");
19102 break;
19103 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX:
19104 printf ("AVX");
19105 break;
19106 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX2:
19107 printf ("AVX2");
19108 break;
19109 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_FMA:
19110 printf ("FMA");
19111 break;
19112 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512F:
19113 printf ("AVX512F");
19114 break;
19115 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512CD:
19116 printf ("AVX512CD");
19117 break;
19118 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512ER:
19119 printf ("AVX512ER");
19120 break;
19121 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512PF:
19122 printf ("AVX512PF");
19123 break;
19124 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512VL:
19125 printf ("AVX512VL");
19126 break;
19127 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512DQ:
19128 printf ("AVX512DQ");
19129 break;
19130 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512BW:
19131 printf ("AVX512BW");
19132 break;
19133 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4FMAPS:
19134 printf ("AVX512_4FMAPS");
19135 break;
19136 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4VNNIW:
19137 printf ("AVX512_4VNNIW");
19138 break;
19139 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BITALG:
19140 printf ("AVX512_BITALG");
19141 break;
19142 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_IFMA:
19143 printf ("AVX512_IFMA");
19144 break;
19145 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI:
19146 printf ("AVX512_VBMI");
19147 break;
19148 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI2:
19149 printf ("AVX512_VBMI2");
19150 break;
19151 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VNNI:
19152 printf ("AVX512_VNNI");
19153 break;
19154 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BF16:
19155 printf ("AVX512_BF16");
19156 break;
19157 default:
19158 printf (_("<unknown: %x>"), bit);
19159 break;
19160 }
19161 if (bitmask)
19162 printf (", ");
19163 }
19164 }
19165
19166 static void
19167 decode_x86_isa (unsigned int bitmask)
19168 {
19169 while (bitmask)
19170 {
19171 unsigned int bit = bitmask & (- bitmask);
19172
19173 bitmask &= ~ bit;
19174 switch (bit)
19175 {
19176 case GNU_PROPERTY_X86_ISA_1_BASELINE:
19177 printf ("x86-64-baseline");
19178 break;
19179 case GNU_PROPERTY_X86_ISA_1_V2:
19180 printf ("x86-64-v2");
19181 break;
19182 case GNU_PROPERTY_X86_ISA_1_V3:
19183 printf ("x86-64-v3");
19184 break;
19185 case GNU_PROPERTY_X86_ISA_1_V4:
19186 printf ("x86-64-v4");
19187 break;
19188 default:
19189 printf (_("<unknown: %x>"), bit);
19190 break;
19191 }
19192 if (bitmask)
19193 printf (", ");
19194 }
19195 }
19196
19197 static void
19198 decode_x86_feature_1 (unsigned int bitmask)
19199 {
19200 if (!bitmask)
19201 {
19202 printf (_("<None>"));
19203 return;
19204 }
19205
19206 while (bitmask)
19207 {
19208 unsigned int bit = bitmask & (- bitmask);
19209
19210 bitmask &= ~ bit;
19211 switch (bit)
19212 {
19213 case GNU_PROPERTY_X86_FEATURE_1_IBT:
19214 printf ("IBT");
19215 break;
19216 case GNU_PROPERTY_X86_FEATURE_1_SHSTK:
19217 printf ("SHSTK");
19218 break;
19219 case GNU_PROPERTY_X86_FEATURE_1_LAM_U48:
19220 printf ("LAM_U48");
19221 break;
19222 case GNU_PROPERTY_X86_FEATURE_1_LAM_U57:
19223 printf ("LAM_U57");
19224 break;
19225 default:
19226 printf (_("<unknown: %x>"), bit);
19227 break;
19228 }
19229 if (bitmask)
19230 printf (", ");
19231 }
19232 }
19233
19234 static void
19235 decode_x86_feature_2 (unsigned int bitmask)
19236 {
19237 if (!bitmask)
19238 {
19239 printf (_("<None>"));
19240 return;
19241 }
19242
19243 while (bitmask)
19244 {
19245 unsigned int bit = bitmask & (- bitmask);
19246
19247 bitmask &= ~ bit;
19248 switch (bit)
19249 {
19250 case GNU_PROPERTY_X86_FEATURE_2_X86:
19251 printf ("x86");
19252 break;
19253 case GNU_PROPERTY_X86_FEATURE_2_X87:
19254 printf ("x87");
19255 break;
19256 case GNU_PROPERTY_X86_FEATURE_2_MMX:
19257 printf ("MMX");
19258 break;
19259 case GNU_PROPERTY_X86_FEATURE_2_XMM:
19260 printf ("XMM");
19261 break;
19262 case GNU_PROPERTY_X86_FEATURE_2_YMM:
19263 printf ("YMM");
19264 break;
19265 case GNU_PROPERTY_X86_FEATURE_2_ZMM:
19266 printf ("ZMM");
19267 break;
19268 case GNU_PROPERTY_X86_FEATURE_2_TMM:
19269 printf ("TMM");
19270 break;
19271 case GNU_PROPERTY_X86_FEATURE_2_MASK:
19272 printf ("MASK");
19273 break;
19274 case GNU_PROPERTY_X86_FEATURE_2_FXSR:
19275 printf ("FXSR");
19276 break;
19277 case GNU_PROPERTY_X86_FEATURE_2_XSAVE:
19278 printf ("XSAVE");
19279 break;
19280 case GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT:
19281 printf ("XSAVEOPT");
19282 break;
19283 case GNU_PROPERTY_X86_FEATURE_2_XSAVEC:
19284 printf ("XSAVEC");
19285 break;
19286 default:
19287 printf (_("<unknown: %x>"), bit);
19288 break;
19289 }
19290 if (bitmask)
19291 printf (", ");
19292 }
19293 }
19294
19295 static void
19296 decode_aarch64_feature_1_and (unsigned int bitmask)
19297 {
19298 while (bitmask)
19299 {
19300 unsigned int bit = bitmask & (- bitmask);
19301
19302 bitmask &= ~ bit;
19303 switch (bit)
19304 {
19305 case GNU_PROPERTY_AARCH64_FEATURE_1_BTI:
19306 printf ("BTI");
19307 break;
19308
19309 case GNU_PROPERTY_AARCH64_FEATURE_1_PAC:
19310 printf ("PAC");
19311 break;
19312
19313 default:
19314 printf (_("<unknown: %x>"), bit);
19315 break;
19316 }
19317 if (bitmask)
19318 printf (", ");
19319 }
19320 }
19321
19322 static void
19323 print_gnu_property_note (Filedata * filedata, Elf_Internal_Note * pnote)
19324 {
19325 unsigned char * ptr = (unsigned char *) pnote->descdata;
19326 unsigned char * ptr_end = ptr + pnote->descsz;
19327 unsigned int size = is_32bit_elf ? 4 : 8;
19328
19329 printf (_(" Properties: "));
19330
19331 if (pnote->descsz < 8 || (pnote->descsz % size) != 0)
19332 {
19333 printf (_("<corrupt GNU_PROPERTY_TYPE, size = %#lx>\n"), pnote->descsz);
19334 return;
19335 }
19336
19337 while (ptr < ptr_end)
19338 {
19339 unsigned int j;
19340 unsigned int type;
19341 unsigned int datasz;
19342
19343 if ((size_t) (ptr_end - ptr) < 8)
19344 {
19345 printf (_("<corrupt descsz: %#lx>\n"), pnote->descsz);
19346 break;
19347 }
19348
19349 type = byte_get (ptr, 4);
19350 datasz = byte_get (ptr + 4, 4);
19351
19352 ptr += 8;
19353
19354 if (datasz > (size_t) (ptr_end - ptr))
19355 {
19356 printf (_("<corrupt type (%#x) datasz: %#x>\n"),
19357 type, datasz);
19358 break;
19359 }
19360
19361 if (type >= GNU_PROPERTY_LOPROC && type <= GNU_PROPERTY_HIPROC)
19362 {
19363 if (filedata->file_header.e_machine == EM_X86_64
19364 || filedata->file_header.e_machine == EM_IAMCU
19365 || filedata->file_header.e_machine == EM_386)
19366 {
19367 unsigned int bitmask;
19368
19369 if (datasz == 4)
19370 bitmask = byte_get (ptr, 4);
19371 else
19372 bitmask = 0;
19373
19374 switch (type)
19375 {
19376 case GNU_PROPERTY_X86_ISA_1_USED:
19377 if (datasz != 4)
19378 printf (_("x86 ISA used: <corrupt length: %#x> "),
19379 datasz);
19380 else
19381 {
19382 printf ("x86 ISA used: ");
19383 decode_x86_isa (bitmask);
19384 }
19385 goto next;
19386
19387 case GNU_PROPERTY_X86_ISA_1_NEEDED:
19388 if (datasz != 4)
19389 printf (_("x86 ISA needed: <corrupt length: %#x> "),
19390 datasz);
19391 else
19392 {
19393 printf ("x86 ISA needed: ");
19394 decode_x86_isa (bitmask);
19395 }
19396 goto next;
19397
19398 case GNU_PROPERTY_X86_FEATURE_1_AND:
19399 if (datasz != 4)
19400 printf (_("x86 feature: <corrupt length: %#x> "),
19401 datasz);
19402 else
19403 {
19404 printf ("x86 feature: ");
19405 decode_x86_feature_1 (bitmask);
19406 }
19407 goto next;
19408
19409 case GNU_PROPERTY_X86_FEATURE_2_USED:
19410 if (datasz != 4)
19411 printf (_("x86 feature used: <corrupt length: %#x> "),
19412 datasz);
19413 else
19414 {
19415 printf ("x86 feature used: ");
19416 decode_x86_feature_2 (bitmask);
19417 }
19418 goto next;
19419
19420 case GNU_PROPERTY_X86_FEATURE_2_NEEDED:
19421 if (datasz != 4)
19422 printf (_("x86 feature needed: <corrupt length: %#x> "), datasz);
19423 else
19424 {
19425 printf ("x86 feature needed: ");
19426 decode_x86_feature_2 (bitmask);
19427 }
19428 goto next;
19429
19430 case GNU_PROPERTY_X86_COMPAT_ISA_1_USED:
19431 if (datasz != 4)
19432 printf (_("x86 ISA used: <corrupt length: %#x> "),
19433 datasz);
19434 else
19435 {
19436 printf ("x86 ISA used: ");
19437 decode_x86_compat_isa (bitmask);
19438 }
19439 goto next;
19440
19441 case GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED:
19442 if (datasz != 4)
19443 printf (_("x86 ISA needed: <corrupt length: %#x> "),
19444 datasz);
19445 else
19446 {
19447 printf ("x86 ISA needed: ");
19448 decode_x86_compat_isa (bitmask);
19449 }
19450 goto next;
19451
19452 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_USED:
19453 if (datasz != 4)
19454 printf (_("x86 ISA used: <corrupt length: %#x> "),
19455 datasz);
19456 else
19457 {
19458 printf ("x86 ISA used: ");
19459 decode_x86_compat_2_isa (bitmask);
19460 }
19461 goto next;
19462
19463 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_NEEDED:
19464 if (datasz != 4)
19465 printf (_("x86 ISA needed: <corrupt length: %#x> "),
19466 datasz);
19467 else
19468 {
19469 printf ("x86 ISA needed: ");
19470 decode_x86_compat_2_isa (bitmask);
19471 }
19472 goto next;
19473
19474 default:
19475 break;
19476 }
19477 }
19478 else if (filedata->file_header.e_machine == EM_AARCH64)
19479 {
19480 if (type == GNU_PROPERTY_AARCH64_FEATURE_1_AND)
19481 {
19482 printf ("AArch64 feature: ");
19483 if (datasz != 4)
19484 printf (_("<corrupt length: %#x> "), datasz);
19485 else
19486 decode_aarch64_feature_1_and (byte_get (ptr, 4));
19487 goto next;
19488 }
19489 }
19490 }
19491 else
19492 {
19493 switch (type)
19494 {
19495 case GNU_PROPERTY_STACK_SIZE:
19496 printf (_("stack size: "));
19497 if (datasz != size)
19498 printf (_("<corrupt length: %#x> "), datasz);
19499 else
19500 printf ("%#lx", (unsigned long) byte_get (ptr, size));
19501 goto next;
19502
19503 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
19504 printf ("no copy on protected ");
19505 if (datasz)
19506 printf (_("<corrupt length: %#x> "), datasz);
19507 goto next;
19508
19509 default:
19510 if ((type >= GNU_PROPERTY_UINT32_AND_LO
19511 && type <= GNU_PROPERTY_UINT32_AND_HI)
19512 || (type >= GNU_PROPERTY_UINT32_OR_LO
19513 && type <= GNU_PROPERTY_UINT32_OR_HI))
19514 {
19515 if (type <= GNU_PROPERTY_UINT32_AND_HI)
19516 printf (_("UINT32_AND (%#x): "), type);
19517 else
19518 printf (_("UINT32_OR (%#x): "), type);
19519 if (datasz != 4)
19520 printf (_("<corrupt length: %#x> "), datasz);
19521 else
19522 printf ("%#x", (unsigned int) byte_get (ptr, 4));
19523 goto next;
19524 }
19525 break;
19526 }
19527 }
19528
19529 if (type < GNU_PROPERTY_LOPROC)
19530 printf (_("<unknown type %#x data: "), type);
19531 else if (type < GNU_PROPERTY_LOUSER)
19532 printf (_("<processor-specific type %#x data: "), type);
19533 else
19534 printf (_("<application-specific type %#x data: "), type);
19535 for (j = 0; j < datasz; ++j)
19536 printf ("%02x ", ptr[j] & 0xff);
19537 printf (">");
19538
19539 next:
19540 ptr += ((datasz + (size - 1)) & ~ (size - 1));
19541 if (ptr == ptr_end)
19542 break;
19543
19544 if (do_wide)
19545 printf (", ");
19546 else
19547 printf ("\n\t");
19548 }
19549
19550 printf ("\n");
19551 }
19552
19553 static bool
19554 print_gnu_note (Filedata * filedata, Elf_Internal_Note *pnote)
19555 {
19556 /* NB/ Keep this switch statement in sync with get_gnu_elf_note_type (). */
19557 switch (pnote->type)
19558 {
19559 case NT_GNU_BUILD_ID:
19560 {
19561 unsigned long i;
19562
19563 printf (_(" Build ID: "));
19564 for (i = 0; i < pnote->descsz; ++i)
19565 printf ("%02x", pnote->descdata[i] & 0xff);
19566 printf ("\n");
19567 }
19568 break;
19569
19570 case NT_GNU_ABI_TAG:
19571 {
19572 unsigned long os, major, minor, subminor;
19573 const char *osname;
19574
19575 /* PR 17531: file: 030-599401-0.004. */
19576 if (pnote->descsz < 16)
19577 {
19578 printf (_(" <corrupt GNU_ABI_TAG>\n"));
19579 break;
19580 }
19581
19582 os = byte_get ((unsigned char *) pnote->descdata, 4);
19583 major = byte_get ((unsigned char *) pnote->descdata + 4, 4);
19584 minor = byte_get ((unsigned char *) pnote->descdata + 8, 4);
19585 subminor = byte_get ((unsigned char *) pnote->descdata + 12, 4);
19586
19587 switch (os)
19588 {
19589 case GNU_ABI_TAG_LINUX:
19590 osname = "Linux";
19591 break;
19592 case GNU_ABI_TAG_HURD:
19593 osname = "Hurd";
19594 break;
19595 case GNU_ABI_TAG_SOLARIS:
19596 osname = "Solaris";
19597 break;
19598 case GNU_ABI_TAG_FREEBSD:
19599 osname = "FreeBSD";
19600 break;
19601 case GNU_ABI_TAG_NETBSD:
19602 osname = "NetBSD";
19603 break;
19604 case GNU_ABI_TAG_SYLLABLE:
19605 osname = "Syllable";
19606 break;
19607 case GNU_ABI_TAG_NACL:
19608 osname = "NaCl";
19609 break;
19610 default:
19611 osname = "Unknown";
19612 break;
19613 }
19614
19615 printf (_(" OS: %s, ABI: %ld.%ld.%ld\n"), osname,
19616 major, minor, subminor);
19617 }
19618 break;
19619
19620 case NT_GNU_GOLD_VERSION:
19621 {
19622 unsigned long i;
19623
19624 printf (_(" Version: "));
19625 for (i = 0; i < pnote->descsz && pnote->descdata[i] != '\0'; ++i)
19626 printf ("%c", pnote->descdata[i]);
19627 printf ("\n");
19628 }
19629 break;
19630
19631 case NT_GNU_HWCAP:
19632 {
19633 unsigned long num_entries, mask;
19634
19635 /* Hardware capabilities information. Word 0 is the number of entries.
19636 Word 1 is a bitmask of enabled entries. The rest of the descriptor
19637 is a series of entries, where each entry is a single byte followed
19638 by a nul terminated string. The byte gives the bit number to test
19639 if enabled in the bitmask. */
19640 printf (_(" Hardware Capabilities: "));
19641 if (pnote->descsz < 8)
19642 {
19643 error (_("<corrupt GNU_HWCAP>\n"));
19644 return false;
19645 }
19646 num_entries = byte_get ((unsigned char *) pnote->descdata, 4);
19647 mask = byte_get ((unsigned char *) pnote->descdata + 4, 4);
19648 printf (_("num entries: %ld, enabled mask: %lx\n"), num_entries, mask);
19649 /* FIXME: Add code to display the entries... */
19650 }
19651 break;
19652
19653 case NT_GNU_PROPERTY_TYPE_0:
19654 print_gnu_property_note (filedata, pnote);
19655 break;
19656
19657 default:
19658 /* Handle unrecognised types. An error message should have already been
19659 created by get_gnu_elf_note_type(), so all that we need to do is to
19660 display the data. */
19661 {
19662 unsigned long i;
19663
19664 printf (_(" Description data: "));
19665 for (i = 0; i < pnote->descsz; ++i)
19666 printf ("%02x ", pnote->descdata[i] & 0xff);
19667 printf ("\n");
19668 }
19669 break;
19670 }
19671
19672 return true;
19673 }
19674
19675 static const char *
19676 get_v850_elf_note_type (enum v850_notes n_type)
19677 {
19678 static char buff[64];
19679
19680 switch (n_type)
19681 {
19682 case V850_NOTE_ALIGNMENT: return _("Alignment of 8-byte objects");
19683 case V850_NOTE_DATA_SIZE: return _("Sizeof double and long double");
19684 case V850_NOTE_FPU_INFO: return _("Type of FPU support needed");
19685 case V850_NOTE_SIMD_INFO: return _("Use of SIMD instructions");
19686 case V850_NOTE_CACHE_INFO: return _("Use of cache");
19687 case V850_NOTE_MMU_INFO: return _("Use of MMU");
19688 default:
19689 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), n_type);
19690 return buff;
19691 }
19692 }
19693
19694 static bool
19695 print_v850_note (Elf_Internal_Note * pnote)
19696 {
19697 unsigned int val;
19698
19699 if (pnote->descsz != 4)
19700 return false;
19701
19702 val = byte_get ((unsigned char *) pnote->descdata, pnote->descsz);
19703
19704 if (val == 0)
19705 {
19706 printf (_("not set\n"));
19707 return true;
19708 }
19709
19710 switch (pnote->type)
19711 {
19712 case V850_NOTE_ALIGNMENT:
19713 switch (val)
19714 {
19715 case EF_RH850_DATA_ALIGN4: printf (_("4-byte\n")); return true;
19716 case EF_RH850_DATA_ALIGN8: printf (_("8-byte\n")); return true;
19717 }
19718 break;
19719
19720 case V850_NOTE_DATA_SIZE:
19721 switch (val)
19722 {
19723 case EF_RH850_DOUBLE32: printf (_("4-bytes\n")); return true;
19724 case EF_RH850_DOUBLE64: printf (_("8-bytes\n")); return true;
19725 }
19726 break;
19727
19728 case V850_NOTE_FPU_INFO:
19729 switch (val)
19730 {
19731 case EF_RH850_FPU20: printf (_("FPU-2.0\n")); return true;
19732 case EF_RH850_FPU30: printf (_("FPU-3.0\n")); return true;
19733 }
19734 break;
19735
19736 case V850_NOTE_MMU_INFO:
19737 case V850_NOTE_CACHE_INFO:
19738 case V850_NOTE_SIMD_INFO:
19739 if (val == EF_RH850_SIMD)
19740 {
19741 printf (_("yes\n"));
19742 return true;
19743 }
19744 break;
19745
19746 default:
19747 /* An 'unknown note type' message will already have been displayed. */
19748 break;
19749 }
19750
19751 printf (_("unknown value: %x\n"), val);
19752 return false;
19753 }
19754
19755 static bool
19756 process_netbsd_elf_note (Elf_Internal_Note * pnote)
19757 {
19758 unsigned int version;
19759
19760 switch (pnote->type)
19761 {
19762 case NT_NETBSD_IDENT:
19763 if (pnote->descsz < 1)
19764 break;
19765 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
19766 if ((version / 10000) % 100)
19767 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u%s%c)\n", pnote->descsz,
19768 version, version / 100000000, (version / 1000000) % 100,
19769 (version / 10000) % 100 > 26 ? "Z" : "",
19770 'A' + (version / 10000) % 26);
19771 else
19772 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u.%u)\n", pnote->descsz,
19773 version, version / 100000000, (version / 1000000) % 100,
19774 (version / 100) % 100);
19775 return true;
19776
19777 case NT_NETBSD_MARCH:
19778 printf (" NetBSD\t\t0x%08lx\tMARCH <%s>\n", pnote->descsz,
19779 pnote->descdata);
19780 return true;
19781
19782 case NT_NETBSD_PAX:
19783 if (pnote->descsz < 1)
19784 break;
19785 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
19786 printf (" NetBSD\t\t0x%08lx\tPaX <%s%s%s%s%s%s>\n", pnote->descsz,
19787 ((version & NT_NETBSD_PAX_MPROTECT) ? "+mprotect" : ""),
19788 ((version & NT_NETBSD_PAX_NOMPROTECT) ? "-mprotect" : ""),
19789 ((version & NT_NETBSD_PAX_GUARD) ? "+guard" : ""),
19790 ((version & NT_NETBSD_PAX_NOGUARD) ? "-guard" : ""),
19791 ((version & NT_NETBSD_PAX_ASLR) ? "+ASLR" : ""),
19792 ((version & NT_NETBSD_PAX_NOASLR) ? "-ASLR" : ""));
19793 return true;
19794 }
19795
19796 printf (" NetBSD\t0x%08lx\tUnknown note type: (0x%08lx)\n",
19797 pnote->descsz, pnote->type);
19798 return false;
19799 }
19800
19801 static const char *
19802 get_freebsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
19803 {
19804 switch (e_type)
19805 {
19806 case NT_FREEBSD_THRMISC:
19807 return _("NT_THRMISC (thrmisc structure)");
19808 case NT_FREEBSD_PROCSTAT_PROC:
19809 return _("NT_PROCSTAT_PROC (proc data)");
19810 case NT_FREEBSD_PROCSTAT_FILES:
19811 return _("NT_PROCSTAT_FILES (files data)");
19812 case NT_FREEBSD_PROCSTAT_VMMAP:
19813 return _("NT_PROCSTAT_VMMAP (vmmap data)");
19814 case NT_FREEBSD_PROCSTAT_GROUPS:
19815 return _("NT_PROCSTAT_GROUPS (groups data)");
19816 case NT_FREEBSD_PROCSTAT_UMASK:
19817 return _("NT_PROCSTAT_UMASK (umask data)");
19818 case NT_FREEBSD_PROCSTAT_RLIMIT:
19819 return _("NT_PROCSTAT_RLIMIT (rlimit data)");
19820 case NT_FREEBSD_PROCSTAT_OSREL:
19821 return _("NT_PROCSTAT_OSREL (osreldate data)");
19822 case NT_FREEBSD_PROCSTAT_PSSTRINGS:
19823 return _("NT_PROCSTAT_PSSTRINGS (ps_strings data)");
19824 case NT_FREEBSD_PROCSTAT_AUXV:
19825 return _("NT_PROCSTAT_AUXV (auxv data)");
19826 case NT_FREEBSD_PTLWPINFO:
19827 return _("NT_PTLWPINFO (ptrace_lwpinfo structure)");
19828 }
19829 return get_note_type (filedata, e_type);
19830 }
19831
19832 static const char *
19833 get_netbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
19834 {
19835 static char buff[64];
19836
19837 switch (e_type)
19838 {
19839 case NT_NETBSDCORE_PROCINFO:
19840 /* NetBSD core "procinfo" structure. */
19841 return _("NetBSD procinfo structure");
19842
19843 case NT_NETBSDCORE_AUXV:
19844 return _("NetBSD ELF auxiliary vector data");
19845
19846 case NT_NETBSDCORE_LWPSTATUS:
19847 return _("PT_LWPSTATUS (ptrace_lwpstatus structure)");
19848
19849 default:
19850 /* As of Jan 2020 there are no other machine-independent notes
19851 defined for NetBSD core files. If the note type is less
19852 than the start of the machine-dependent note types, we don't
19853 understand it. */
19854
19855 if (e_type < NT_NETBSDCORE_FIRSTMACH)
19856 {
19857 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19858 return buff;
19859 }
19860 break;
19861 }
19862
19863 switch (filedata->file_header.e_machine)
19864 {
19865 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
19866 and PT_GETFPREGS == mach+2. */
19867
19868 case EM_OLD_ALPHA:
19869 case EM_ALPHA:
19870 case EM_SPARC:
19871 case EM_SPARC32PLUS:
19872 case EM_SPARCV9:
19873 switch (e_type)
19874 {
19875 case NT_NETBSDCORE_FIRSTMACH + 0:
19876 return _("PT_GETREGS (reg structure)");
19877 case NT_NETBSDCORE_FIRSTMACH + 2:
19878 return _("PT_GETFPREGS (fpreg structure)");
19879 default:
19880 break;
19881 }
19882 break;
19883
19884 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
19885 There's also old PT___GETREGS40 == mach + 1 for old reg
19886 structure which lacks GBR. */
19887 case EM_SH:
19888 switch (e_type)
19889 {
19890 case NT_NETBSDCORE_FIRSTMACH + 1:
19891 return _("PT___GETREGS40 (old reg structure)");
19892 case NT_NETBSDCORE_FIRSTMACH + 3:
19893 return _("PT_GETREGS (reg structure)");
19894 case NT_NETBSDCORE_FIRSTMACH + 5:
19895 return _("PT_GETFPREGS (fpreg structure)");
19896 default:
19897 break;
19898 }
19899 break;
19900
19901 /* On all other arch's, PT_GETREGS == mach+1 and
19902 PT_GETFPREGS == mach+3. */
19903 default:
19904 switch (e_type)
19905 {
19906 case NT_NETBSDCORE_FIRSTMACH + 1:
19907 return _("PT_GETREGS (reg structure)");
19908 case NT_NETBSDCORE_FIRSTMACH + 3:
19909 return _("PT_GETFPREGS (fpreg structure)");
19910 default:
19911 break;
19912 }
19913 }
19914
19915 snprintf (buff, sizeof (buff), "PT_FIRSTMACH+%d",
19916 e_type - NT_NETBSDCORE_FIRSTMACH);
19917 return buff;
19918 }
19919
19920 static const char *
19921 get_stapsdt_note_type (unsigned e_type)
19922 {
19923 static char buff[64];
19924
19925 switch (e_type)
19926 {
19927 case NT_STAPSDT:
19928 return _("NT_STAPSDT (SystemTap probe descriptors)");
19929
19930 default:
19931 break;
19932 }
19933
19934 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19935 return buff;
19936 }
19937
19938 static bool
19939 print_stapsdt_note (Elf_Internal_Note *pnote)
19940 {
19941 size_t len, maxlen;
19942 unsigned long addr_size = is_32bit_elf ? 4 : 8;
19943 char *data = pnote->descdata;
19944 char *data_end = pnote->descdata + pnote->descsz;
19945 bfd_vma pc, base_addr, semaphore;
19946 char *provider, *probe, *arg_fmt;
19947
19948 if (pnote->descsz < (addr_size * 3))
19949 goto stapdt_note_too_small;
19950
19951 pc = byte_get ((unsigned char *) data, addr_size);
19952 data += addr_size;
19953
19954 base_addr = byte_get ((unsigned char *) data, addr_size);
19955 data += addr_size;
19956
19957 semaphore = byte_get ((unsigned char *) data, addr_size);
19958 data += addr_size;
19959
19960 if (data >= data_end)
19961 goto stapdt_note_too_small;
19962 maxlen = data_end - data;
19963 len = strnlen (data, maxlen);
19964 if (len < maxlen)
19965 {
19966 provider = data;
19967 data += len + 1;
19968 }
19969 else
19970 goto stapdt_note_too_small;
19971
19972 if (data >= data_end)
19973 goto stapdt_note_too_small;
19974 maxlen = data_end - data;
19975 len = strnlen (data, maxlen);
19976 if (len < maxlen)
19977 {
19978 probe = data;
19979 data += len + 1;
19980 }
19981 else
19982 goto stapdt_note_too_small;
19983
19984 if (data >= data_end)
19985 goto stapdt_note_too_small;
19986 maxlen = data_end - data;
19987 len = strnlen (data, maxlen);
19988 if (len < maxlen)
19989 {
19990 arg_fmt = data;
19991 data += len + 1;
19992 }
19993 else
19994 goto stapdt_note_too_small;
19995
19996 printf (_(" Provider: %s\n"), provider);
19997 printf (_(" Name: %s\n"), probe);
19998 printf (_(" Location: "));
19999 print_vma (pc, FULL_HEX);
20000 printf (_(", Base: "));
20001 print_vma (base_addr, FULL_HEX);
20002 printf (_(", Semaphore: "));
20003 print_vma (semaphore, FULL_HEX);
20004 printf ("\n");
20005 printf (_(" Arguments: %s\n"), arg_fmt);
20006
20007 return data == data_end;
20008
20009 stapdt_note_too_small:
20010 printf (_(" <corrupt - note is too small>\n"));
20011 error (_("corrupt stapdt note - the data size is too small\n"));
20012 return false;
20013 }
20014
20015 static const char *
20016 get_ia64_vms_note_type (unsigned e_type)
20017 {
20018 static char buff[64];
20019
20020 switch (e_type)
20021 {
20022 case NT_VMS_MHD:
20023 return _("NT_VMS_MHD (module header)");
20024 case NT_VMS_LNM:
20025 return _("NT_VMS_LNM (language name)");
20026 case NT_VMS_SRC:
20027 return _("NT_VMS_SRC (source files)");
20028 case NT_VMS_TITLE:
20029 return "NT_VMS_TITLE";
20030 case NT_VMS_EIDC:
20031 return _("NT_VMS_EIDC (consistency check)");
20032 case NT_VMS_FPMODE:
20033 return _("NT_VMS_FPMODE (FP mode)");
20034 case NT_VMS_LINKTIME:
20035 return "NT_VMS_LINKTIME";
20036 case NT_VMS_IMGNAM:
20037 return _("NT_VMS_IMGNAM (image name)");
20038 case NT_VMS_IMGID:
20039 return _("NT_VMS_IMGID (image id)");
20040 case NT_VMS_LINKID:
20041 return _("NT_VMS_LINKID (link id)");
20042 case NT_VMS_IMGBID:
20043 return _("NT_VMS_IMGBID (build id)");
20044 case NT_VMS_GSTNAM:
20045 return _("NT_VMS_GSTNAM (sym table name)");
20046 case NT_VMS_ORIG_DYN:
20047 return "NT_VMS_ORIG_DYN";
20048 case NT_VMS_PATCHTIME:
20049 return "NT_VMS_PATCHTIME";
20050 default:
20051 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20052 return buff;
20053 }
20054 }
20055
20056 static bool
20057 print_ia64_vms_note (Elf_Internal_Note * pnote)
20058 {
20059 int maxlen = pnote->descsz;
20060
20061 if (maxlen < 2 || (unsigned long) maxlen != pnote->descsz)
20062 goto desc_size_fail;
20063
20064 switch (pnote->type)
20065 {
20066 case NT_VMS_MHD:
20067 if (maxlen <= 36)
20068 goto desc_size_fail;
20069
20070 int l = (int) strnlen (pnote->descdata + 34, maxlen - 34);
20071
20072 printf (_(" Creation date : %.17s\n"), pnote->descdata);
20073 printf (_(" Last patch date: %.17s\n"), pnote->descdata + 17);
20074 if (l + 34 < maxlen)
20075 {
20076 printf (_(" Module name : %s\n"), pnote->descdata + 34);
20077 if (l + 35 < maxlen)
20078 printf (_(" Module version : %s\n"), pnote->descdata + 34 + l + 1);
20079 else
20080 printf (_(" Module version : <missing>\n"));
20081 }
20082 else
20083 {
20084 printf (_(" Module name : <missing>\n"));
20085 printf (_(" Module version : <missing>\n"));
20086 }
20087 break;
20088
20089 case NT_VMS_LNM:
20090 printf (_(" Language: %.*s\n"), maxlen, pnote->descdata);
20091 break;
20092
20093 #ifdef BFD64
20094 case NT_VMS_FPMODE:
20095 printf (_(" Floating Point mode: "));
20096 if (maxlen < 8)
20097 goto desc_size_fail;
20098 /* FIXME: Generate an error if descsz > 8 ? */
20099
20100 printf ("0x%016" BFD_VMA_FMT "x\n",
20101 (bfd_vma) byte_get ((unsigned char *)pnote->descdata, 8));
20102 break;
20103
20104 case NT_VMS_LINKTIME:
20105 printf (_(" Link time: "));
20106 if (maxlen < 8)
20107 goto desc_size_fail;
20108 /* FIXME: Generate an error if descsz > 8 ? */
20109
20110 print_vms_time
20111 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
20112 printf ("\n");
20113 break;
20114
20115 case NT_VMS_PATCHTIME:
20116 printf (_(" Patch time: "));
20117 if (maxlen < 8)
20118 goto desc_size_fail;
20119 /* FIXME: Generate an error if descsz > 8 ? */
20120
20121 print_vms_time
20122 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
20123 printf ("\n");
20124 break;
20125
20126 case NT_VMS_ORIG_DYN:
20127 if (maxlen < 34)
20128 goto desc_size_fail;
20129
20130 printf (_(" Major id: %u, minor id: %u\n"),
20131 (unsigned) byte_get ((unsigned char *)pnote->descdata, 4),
20132 (unsigned) byte_get ((unsigned char *)pnote->descdata + 4, 4));
20133 printf (_(" Last modified : "));
20134 print_vms_time
20135 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata + 8, 8));
20136 printf (_("\n Link flags : "));
20137 printf ("0x%016" BFD_VMA_FMT "x\n",
20138 (bfd_vma) byte_get ((unsigned char *)pnote->descdata + 16, 8));
20139 printf (_(" Header flags: 0x%08x\n"),
20140 (unsigned) byte_get ((unsigned char *)pnote->descdata + 24, 4));
20141 printf (_(" Image id : %.*s\n"), maxlen - 32, pnote->descdata + 32);
20142 break;
20143 #endif
20144
20145 case NT_VMS_IMGNAM:
20146 printf (_(" Image name: %.*s\n"), maxlen, pnote->descdata);
20147 break;
20148
20149 case NT_VMS_GSTNAM:
20150 printf (_(" Global symbol table name: %.*s\n"), maxlen, pnote->descdata);
20151 break;
20152
20153 case NT_VMS_IMGID:
20154 printf (_(" Image id: %.*s\n"), maxlen, pnote->descdata);
20155 break;
20156
20157 case NT_VMS_LINKID:
20158 printf (_(" Linker id: %.*s\n"), maxlen, pnote->descdata);
20159 break;
20160
20161 default:
20162 return false;
20163 }
20164
20165 return true;
20166
20167 desc_size_fail:
20168 printf (_(" <corrupt - data size is too small>\n"));
20169 error (_("corrupt IA64 note: data size is too small\n"));
20170 return false;
20171 }
20172
20173 struct build_attr_cache {
20174 Filedata *filedata;
20175 char *strtab;
20176 unsigned long strtablen;
20177 Elf_Internal_Sym *symtab;
20178 unsigned long nsyms;
20179 } ba_cache;
20180
20181 /* Find the symbol associated with a build attribute that is attached
20182 to address OFFSET. If PNAME is non-NULL then store the name of
20183 the symbol (if found) in the provided pointer, Returns NULL if a
20184 symbol could not be found. */
20185
20186 static Elf_Internal_Sym *
20187 get_symbol_for_build_attribute (Filedata *filedata,
20188 unsigned long offset,
20189 bool is_open_attr,
20190 const char **pname)
20191 {
20192 Elf_Internal_Sym *saved_sym = NULL;
20193 Elf_Internal_Sym *sym;
20194
20195 if (filedata->section_headers != NULL
20196 && (ba_cache.filedata == NULL || filedata != ba_cache.filedata))
20197 {
20198 Elf_Internal_Shdr * symsec;
20199
20200 free (ba_cache.strtab);
20201 ba_cache.strtab = NULL;
20202 free (ba_cache.symtab);
20203 ba_cache.symtab = NULL;
20204
20205 /* Load the symbol and string sections. */
20206 for (symsec = filedata->section_headers;
20207 symsec < filedata->section_headers + filedata->file_header.e_shnum;
20208 symsec ++)
20209 {
20210 if (symsec->sh_type == SHT_SYMTAB
20211 && get_symtab (filedata, symsec,
20212 &ba_cache.symtab, &ba_cache.nsyms,
20213 &ba_cache.strtab, &ba_cache.strtablen))
20214 break;
20215 }
20216 ba_cache.filedata = filedata;
20217 }
20218
20219 if (ba_cache.symtab == NULL)
20220 return NULL;
20221
20222 /* Find a symbol whose value matches offset. */
20223 for (sym = ba_cache.symtab; sym < ba_cache.symtab + ba_cache.nsyms; sym ++)
20224 if (sym->st_value == offset)
20225 {
20226 if (sym->st_name >= ba_cache.strtablen)
20227 /* Huh ? This should not happen. */
20228 continue;
20229
20230 if (ba_cache.strtab[sym->st_name] == 0)
20231 continue;
20232
20233 /* The AArch64 and ARM architectures define mapping symbols
20234 (eg $d, $x, $t) which we want to ignore. */
20235 if (ba_cache.strtab[sym->st_name] == '$'
20236 && ba_cache.strtab[sym->st_name + 1] != 0
20237 && ba_cache.strtab[sym->st_name + 2] == 0)
20238 continue;
20239
20240 if (is_open_attr)
20241 {
20242 /* For OPEN attributes we prefer GLOBAL over LOCAL symbols
20243 and FILE or OBJECT symbols over NOTYPE symbols. We skip
20244 FUNC symbols entirely. */
20245 switch (ELF_ST_TYPE (sym->st_info))
20246 {
20247 case STT_OBJECT:
20248 case STT_FILE:
20249 saved_sym = sym;
20250 if (sym->st_size)
20251 {
20252 /* If the symbol has a size associated
20253 with it then we can stop searching. */
20254 sym = ba_cache.symtab + ba_cache.nsyms;
20255 }
20256 continue;
20257
20258 case STT_FUNC:
20259 /* Ignore function symbols. */
20260 continue;
20261
20262 default:
20263 break;
20264 }
20265
20266 switch (ELF_ST_BIND (sym->st_info))
20267 {
20268 case STB_GLOBAL:
20269 if (saved_sym == NULL
20270 || ELF_ST_TYPE (saved_sym->st_info) != STT_OBJECT)
20271 saved_sym = sym;
20272 break;
20273
20274 case STB_LOCAL:
20275 if (saved_sym == NULL)
20276 saved_sym = sym;
20277 break;
20278
20279 default:
20280 break;
20281 }
20282 }
20283 else
20284 {
20285 if (ELF_ST_TYPE (sym->st_info) != STT_FUNC)
20286 continue;
20287
20288 saved_sym = sym;
20289 break;
20290 }
20291 }
20292
20293 if (saved_sym && pname)
20294 * pname = ba_cache.strtab + saved_sym->st_name;
20295
20296 return saved_sym;
20297 }
20298
20299 /* Returns true iff addr1 and addr2 are in the same section. */
20300
20301 static bool
20302 same_section (Filedata * filedata, unsigned long addr1, unsigned long addr2)
20303 {
20304 Elf_Internal_Shdr * a1;
20305 Elf_Internal_Shdr * a2;
20306
20307 a1 = find_section_by_address (filedata, addr1);
20308 a2 = find_section_by_address (filedata, addr2);
20309
20310 return a1 == a2 && a1 != NULL;
20311 }
20312
20313 static bool
20314 print_gnu_build_attribute_description (Elf_Internal_Note * pnote,
20315 Filedata * filedata)
20316 {
20317 static unsigned long global_offset = 0;
20318 static unsigned long global_end = 0;
20319 static unsigned long func_offset = 0;
20320 static unsigned long func_end = 0;
20321
20322 Elf_Internal_Sym *sym;
20323 const char *name;
20324 unsigned long start;
20325 unsigned long end;
20326 bool is_open_attr = pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN;
20327
20328 switch (pnote->descsz)
20329 {
20330 case 0:
20331 /* A zero-length description means that the range of
20332 the previous note of the same type should be used. */
20333 if (is_open_attr)
20334 {
20335 if (global_end > global_offset)
20336 printf (_(" Applies to region from %#lx to %#lx\n"),
20337 global_offset, global_end);
20338 else
20339 printf (_(" Applies to region from %#lx\n"), global_offset);
20340 }
20341 else
20342 {
20343 if (func_end > func_offset)
20344 printf (_(" Applies to region from %#lx to %#lx\n"), func_offset, func_end);
20345 else
20346 printf (_(" Applies to region from %#lx\n"), func_offset);
20347 }
20348 return true;
20349
20350 case 4:
20351 start = byte_get ((unsigned char *) pnote->descdata, 4);
20352 end = 0;
20353 break;
20354
20355 case 8:
20356 start = byte_get ((unsigned char *) pnote->descdata, 4);
20357 end = byte_get ((unsigned char *) pnote->descdata + 4, 4);
20358 break;
20359
20360 case 16:
20361 start = byte_get ((unsigned char *) pnote->descdata, 8);
20362 end = byte_get ((unsigned char *) pnote->descdata + 8, 8);
20363 break;
20364
20365 default:
20366 error (_(" <invalid description size: %lx>\n"), pnote->descsz);
20367 printf (_(" <invalid descsz>"));
20368 return false;
20369 }
20370
20371 name = NULL;
20372 sym = get_symbol_for_build_attribute (filedata, start, is_open_attr, & name);
20373 /* As of version 5 of the annobin plugin, filename symbols are biased by 2
20374 in order to avoid them being confused with the start address of the
20375 first function in the file... */
20376 if (sym == NULL && is_open_attr)
20377 sym = get_symbol_for_build_attribute (filedata, start + 2, is_open_attr,
20378 & name);
20379
20380 if (end == 0 && sym != NULL && sym->st_size > 0)
20381 end = start + sym->st_size;
20382
20383 if (is_open_attr)
20384 {
20385 /* FIXME: Need to properly allow for section alignment.
20386 16 is just the alignment used on x86_64. */
20387 if (global_end > 0
20388 && start > BFD_ALIGN (global_end, 16)
20389 /* Build notes are not guaranteed to be organised in order of
20390 increasing address, but we should find the all of the notes
20391 for one section in the same place. */
20392 && same_section (filedata, start, global_end))
20393 warn (_("Gap in build notes detected from %#lx to %#lx\n"),
20394 global_end + 1, start - 1);
20395
20396 printf (_(" Applies to region from %#lx"), start);
20397 global_offset = start;
20398
20399 if (end)
20400 {
20401 printf (_(" to %#lx"), end);
20402 global_end = end;
20403 }
20404 }
20405 else
20406 {
20407 printf (_(" Applies to region from %#lx"), start);
20408 func_offset = start;
20409
20410 if (end)
20411 {
20412 printf (_(" to %#lx"), end);
20413 func_end = end;
20414 }
20415 }
20416
20417 if (sym && name)
20418 printf (_(" (%s)"), name);
20419
20420 printf ("\n");
20421 return true;
20422 }
20423
20424 static bool
20425 print_gnu_build_attribute_name (Elf_Internal_Note * pnote)
20426 {
20427 static const char string_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_STRING, 0 };
20428 static const char number_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC, 0 };
20429 static const char bool_expected [3] = { GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE, GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE, 0 };
20430 char name_type;
20431 char name_attribute;
20432 const char * expected_types;
20433 const char * name = pnote->namedata;
20434 const char * text;
20435 signed int left;
20436
20437 if (name == NULL || pnote->namesz < 2)
20438 {
20439 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
20440 print_symbol (-20, _(" <corrupt name>"));
20441 return false;
20442 }
20443
20444 if (do_wide)
20445 left = 28;
20446 else
20447 left = 20;
20448
20449 /* Version 2 of the spec adds a "GA" prefix to the name field. */
20450 if (name[0] == 'G' && name[1] == 'A')
20451 {
20452 if (pnote->namesz < 4)
20453 {
20454 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
20455 print_symbol (-20, _(" <corrupt name>"));
20456 return false;
20457 }
20458
20459 printf ("GA");
20460 name += 2;
20461 left -= 2;
20462 }
20463
20464 switch ((name_type = * name))
20465 {
20466 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
20467 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
20468 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
20469 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
20470 printf ("%c", * name);
20471 left --;
20472 break;
20473 default:
20474 error (_("unrecognised attribute type in name field: %d\n"), name_type);
20475 print_symbol (-20, _("<unknown name type>"));
20476 return false;
20477 }
20478
20479 ++ name;
20480 text = NULL;
20481
20482 switch ((name_attribute = * name))
20483 {
20484 case GNU_BUILD_ATTRIBUTE_VERSION:
20485 text = _("<version>");
20486 expected_types = string_expected;
20487 ++ name;
20488 break;
20489 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
20490 text = _("<stack prot>");
20491 expected_types = "!+*";
20492 ++ name;
20493 break;
20494 case GNU_BUILD_ATTRIBUTE_RELRO:
20495 text = _("<relro>");
20496 expected_types = bool_expected;
20497 ++ name;
20498 break;
20499 case GNU_BUILD_ATTRIBUTE_STACK_SIZE:
20500 text = _("<stack size>");
20501 expected_types = number_expected;
20502 ++ name;
20503 break;
20504 case GNU_BUILD_ATTRIBUTE_TOOL:
20505 text = _("<tool>");
20506 expected_types = string_expected;
20507 ++ name;
20508 break;
20509 case GNU_BUILD_ATTRIBUTE_ABI:
20510 text = _("<ABI>");
20511 expected_types = "$*";
20512 ++ name;
20513 break;
20514 case GNU_BUILD_ATTRIBUTE_PIC:
20515 text = _("<PIC>");
20516 expected_types = number_expected;
20517 ++ name;
20518 break;
20519 case GNU_BUILD_ATTRIBUTE_SHORT_ENUM:
20520 text = _("<short enum>");
20521 expected_types = bool_expected;
20522 ++ name;
20523 break;
20524 default:
20525 if (ISPRINT (* name))
20526 {
20527 int len = strnlen (name, pnote->namesz - (name - pnote->namedata)) + 1;
20528
20529 if (len > left && ! do_wide)
20530 len = left;
20531 printf ("%.*s:", len, name);
20532 left -= len;
20533 name += len;
20534 }
20535 else
20536 {
20537 static char tmpbuf [128];
20538
20539 error (_("unrecognised byte in name field: %d\n"), * name);
20540 sprintf (tmpbuf, _("<unknown:_%d>"), * name);
20541 text = tmpbuf;
20542 name ++;
20543 }
20544 expected_types = "*$!+";
20545 break;
20546 }
20547
20548 if (text)
20549 left -= printf ("%s", text);
20550
20551 if (strchr (expected_types, name_type) == NULL)
20552 warn (_("attribute does not have an expected type (%c)\n"), name_type);
20553
20554 if ((unsigned long)(name - pnote->namedata) > pnote->namesz)
20555 {
20556 error (_("corrupt name field: namesz: %lu but parsing gets to %ld\n"),
20557 (unsigned long) pnote->namesz,
20558 (long) (name - pnote->namedata));
20559 return false;
20560 }
20561
20562 if (left < 1 && ! do_wide)
20563 return true;
20564
20565 switch (name_type)
20566 {
20567 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
20568 {
20569 unsigned int bytes;
20570 unsigned long long val = 0;
20571 unsigned int shift = 0;
20572 char * decoded = NULL;
20573
20574 bytes = pnote->namesz - (name - pnote->namedata);
20575 if (bytes > 0)
20576 /* The -1 is because the name field is always 0 terminated, and we
20577 want to be able to ensure that the shift in the while loop below
20578 will not overflow. */
20579 -- bytes;
20580
20581 if (bytes > sizeof (val))
20582 {
20583 error (_("corrupt numeric name field: too many bytes in the value: %x\n"),
20584 bytes);
20585 bytes = sizeof (val);
20586 }
20587 /* We do not bother to warn if bytes == 0 as this can
20588 happen with some early versions of the gcc plugin. */
20589
20590 while (bytes --)
20591 {
20592 unsigned long long byte = *name++ & 0xff;
20593
20594 val |= byte << shift;
20595 shift += 8;
20596 }
20597
20598 switch (name_attribute)
20599 {
20600 case GNU_BUILD_ATTRIBUTE_PIC:
20601 switch (val)
20602 {
20603 case 0: decoded = "static"; break;
20604 case 1: decoded = "pic"; break;
20605 case 2: decoded = "PIC"; break;
20606 case 3: decoded = "pie"; break;
20607 case 4: decoded = "PIE"; break;
20608 default: break;
20609 }
20610 break;
20611 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
20612 switch (val)
20613 {
20614 /* Based upon the SPCT_FLAG_xxx enum values in gcc/cfgexpand.c. */
20615 case 0: decoded = "off"; break;
20616 case 1: decoded = "on"; break;
20617 case 2: decoded = "all"; break;
20618 case 3: decoded = "strong"; break;
20619 case 4: decoded = "explicit"; break;
20620 default: break;
20621 }
20622 break;
20623 default:
20624 break;
20625 }
20626
20627 if (decoded != NULL)
20628 {
20629 print_symbol (-left, decoded);
20630 left = 0;
20631 }
20632 else if (val == 0)
20633 {
20634 printf ("0x0");
20635 left -= 3;
20636 }
20637 else
20638 {
20639 if (do_wide)
20640 left -= printf ("0x%llx", val);
20641 else
20642 left -= printf ("0x%-.*llx", left, val);
20643 }
20644 }
20645 break;
20646 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
20647 left -= print_symbol (- left, name);
20648 break;
20649 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
20650 left -= print_symbol (- left, "true");
20651 break;
20652 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
20653 left -= print_symbol (- left, "false");
20654 break;
20655 }
20656
20657 if (do_wide && left > 0)
20658 printf ("%-*s", left, " ");
20659
20660 return true;
20661 }
20662
20663 /* Note that by the ELF standard, the name field is already null byte
20664 terminated, and namesz includes the terminating null byte.
20665 I.E. the value of namesz for the name "FSF" is 4.
20666
20667 If the value of namesz is zero, there is no name present. */
20668
20669 static bool
20670 process_note (Elf_Internal_Note * pnote,
20671 Filedata * filedata)
20672 {
20673 const char * name = pnote->namesz ? pnote->namedata : "(NONE)";
20674 const char * nt;
20675
20676 if (pnote->namesz == 0)
20677 /* If there is no note name, then use the default set of
20678 note type strings. */
20679 nt = get_note_type (filedata, pnote->type);
20680
20681 else if (startswith (pnote->namedata, "GNU"))
20682 /* GNU-specific object file notes. */
20683 nt = get_gnu_elf_note_type (pnote->type);
20684
20685 else if (startswith (pnote->namedata, "FreeBSD"))
20686 /* FreeBSD-specific core file notes. */
20687 nt = get_freebsd_elfcore_note_type (filedata, pnote->type);
20688
20689 else if (startswith (pnote->namedata, "NetBSD-CORE"))
20690 /* NetBSD-specific core file notes. */
20691 nt = get_netbsd_elfcore_note_type (filedata, pnote->type);
20692
20693 else if (startswith (pnote->namedata, "NetBSD"))
20694 /* NetBSD-specific core file notes. */
20695 return process_netbsd_elf_note (pnote);
20696
20697 else if (startswith (pnote->namedata, "PaX"))
20698 /* NetBSD-specific core file notes. */
20699 return process_netbsd_elf_note (pnote);
20700
20701 else if (startswith (pnote->namedata, "SPU/"))
20702 {
20703 /* SPU-specific core file notes. */
20704 nt = pnote->namedata + 4;
20705 name = "SPU";
20706 }
20707
20708 else if (startswith (pnote->namedata, "IPF/VMS"))
20709 /* VMS/ia64-specific file notes. */
20710 nt = get_ia64_vms_note_type (pnote->type);
20711
20712 else if (startswith (pnote->namedata, "stapsdt"))
20713 nt = get_stapsdt_note_type (pnote->type);
20714
20715 else
20716 /* Don't recognize this note name; just use the default set of
20717 note type strings. */
20718 nt = get_note_type (filedata, pnote->type);
20719
20720 printf (" ");
20721
20722 if (((startswith (pnote->namedata, "GA")
20723 && strchr ("*$!+", pnote->namedata[2]) != NULL)
20724 || strchr ("*$!+", pnote->namedata[0]) != NULL)
20725 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
20726 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
20727 print_gnu_build_attribute_name (pnote);
20728 else
20729 print_symbol (-20, name);
20730
20731 if (do_wide)
20732 printf (" 0x%08lx\t%s\t", pnote->descsz, nt);
20733 else
20734 printf (" 0x%08lx\t%s\n", pnote->descsz, nt);
20735
20736 if (startswith (pnote->namedata, "IPF/VMS"))
20737 return print_ia64_vms_note (pnote);
20738 else if (startswith (pnote->namedata, "GNU"))
20739 return print_gnu_note (filedata, pnote);
20740 else if (startswith (pnote->namedata, "stapsdt"))
20741 return print_stapsdt_note (pnote);
20742 else if (startswith (pnote->namedata, "CORE"))
20743 return print_core_note (pnote);
20744 else if (((startswith (pnote->namedata, "GA")
20745 && strchr ("*$!+", pnote->namedata[2]) != NULL)
20746 || strchr ("*$!+", pnote->namedata[0]) != NULL)
20747 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
20748 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
20749 return print_gnu_build_attribute_description (pnote, filedata);
20750
20751 if (pnote->descsz)
20752 {
20753 unsigned long i;
20754
20755 printf (_(" description data: "));
20756 for (i = 0; i < pnote->descsz; i++)
20757 printf ("%02x ", pnote->descdata[i] & 0xff);
20758 if (!do_wide)
20759 printf ("\n");
20760 }
20761
20762 if (do_wide)
20763 printf ("\n");
20764
20765 return true;
20766 }
20767
20768 static bool
20769 process_notes_at (Filedata * filedata,
20770 Elf_Internal_Shdr * section,
20771 bfd_vma offset,
20772 bfd_vma length,
20773 bfd_vma align)
20774 {
20775 Elf_External_Note *pnotes;
20776 Elf_External_Note *external;
20777 char *end;
20778 bool res = true;
20779
20780 if (length <= 0)
20781 return false;
20782
20783 if (section)
20784 {
20785 pnotes = (Elf_External_Note *) get_section_contents (section, filedata);
20786 if (pnotes)
20787 {
20788 if (! apply_relocations (filedata, section, (unsigned char *) pnotes, length, NULL, NULL))
20789 {
20790 free (pnotes);
20791 return false;
20792 }
20793 }
20794 }
20795 else
20796 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
20797 _("notes"));
20798
20799 if (pnotes == NULL)
20800 return false;
20801
20802 external = pnotes;
20803
20804 if (filedata->is_separate)
20805 printf (_("In linked file '%s': "), filedata->file_name);
20806 else
20807 printf ("\n");
20808 if (section)
20809 printf (_("Displaying notes found in: %s\n"), printable_section_name (filedata, section));
20810 else
20811 printf (_("Displaying notes found at file offset 0x%08lx with length 0x%08lx:\n"),
20812 (unsigned long) offset, (unsigned long) length);
20813
20814 /* NB: Some note sections may have alignment value of 0 or 1. gABI
20815 specifies that notes should be aligned to 4 bytes in 32-bit
20816 objects and to 8 bytes in 64-bit objects. As a Linux extension,
20817 we also support 4 byte alignment in 64-bit objects. If section
20818 alignment is less than 4, we treate alignment as 4 bytes. */
20819 if (align < 4)
20820 align = 4;
20821 else if (align != 4 && align != 8)
20822 {
20823 warn (_("Corrupt note: alignment %ld, expecting 4 or 8\n"),
20824 (long) align);
20825 free (pnotes);
20826 return false;
20827 }
20828
20829 printf (_(" %-20s %-10s\tDescription\n"), _("Owner"), _("Data size"));
20830
20831 end = (char *) pnotes + length;
20832 while ((char *) external < end)
20833 {
20834 Elf_Internal_Note inote;
20835 size_t min_notesz;
20836 char * next;
20837 char * temp = NULL;
20838 size_t data_remaining = end - (char *) external;
20839
20840 if (!is_ia64_vms (filedata))
20841 {
20842 /* PR binutils/15191
20843 Make sure that there is enough data to read. */
20844 min_notesz = offsetof (Elf_External_Note, name);
20845 if (data_remaining < min_notesz)
20846 {
20847 warn (ngettext ("Corrupt note: only %ld byte remains, "
20848 "not enough for a full note\n",
20849 "Corrupt note: only %ld bytes remain, "
20850 "not enough for a full note\n",
20851 data_remaining),
20852 (long) data_remaining);
20853 break;
20854 }
20855 data_remaining -= min_notesz;
20856
20857 inote.type = BYTE_GET (external->type);
20858 inote.namesz = BYTE_GET (external->namesz);
20859 inote.namedata = external->name;
20860 inote.descsz = BYTE_GET (external->descsz);
20861 inote.descdata = ((char *) external
20862 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
20863 inote.descpos = offset + (inote.descdata - (char *) pnotes);
20864 next = ((char *) external
20865 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
20866 }
20867 else
20868 {
20869 Elf64_External_VMS_Note *vms_external;
20870
20871 /* PR binutils/15191
20872 Make sure that there is enough data to read. */
20873 min_notesz = offsetof (Elf64_External_VMS_Note, name);
20874 if (data_remaining < min_notesz)
20875 {
20876 warn (ngettext ("Corrupt note: only %ld byte remains, "
20877 "not enough for a full note\n",
20878 "Corrupt note: only %ld bytes remain, "
20879 "not enough for a full note\n",
20880 data_remaining),
20881 (long) data_remaining);
20882 break;
20883 }
20884 data_remaining -= min_notesz;
20885
20886 vms_external = (Elf64_External_VMS_Note *) external;
20887 inote.type = BYTE_GET (vms_external->type);
20888 inote.namesz = BYTE_GET (vms_external->namesz);
20889 inote.namedata = vms_external->name;
20890 inote.descsz = BYTE_GET (vms_external->descsz);
20891 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
20892 inote.descpos = offset + (inote.descdata - (char *) pnotes);
20893 next = inote.descdata + align_power (inote.descsz, 3);
20894 }
20895
20896 /* PR 17531: file: 3443835e. */
20897 /* PR 17531: file: id:000000,sig:11,src:006986,op:havoc,rep:4. */
20898 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
20899 || (size_t) (inote.descdata - inote.namedata) > data_remaining
20900 || (size_t) (next - inote.descdata) < inote.descsz
20901 || ((size_t) (next - inote.descdata)
20902 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
20903 {
20904 warn (_("note with invalid namesz and/or descsz found at offset 0x%lx\n"),
20905 (unsigned long) ((char *) external - (char *) pnotes));
20906 warn (_(" type: 0x%lx, namesize: 0x%08lx, descsize: 0x%08lx, alignment: %u\n"),
20907 inote.type, inote.namesz, inote.descsz, (int) align);
20908 break;
20909 }
20910
20911 external = (Elf_External_Note *) next;
20912
20913 /* Verify that name is null terminated. It appears that at least
20914 one version of Linux (RedHat 6.0) generates corefiles that don't
20915 comply with the ELF spec by failing to include the null byte in
20916 namesz. */
20917 if (inote.namesz > 0 && inote.namedata[inote.namesz - 1] != '\0')
20918 {
20919 if ((size_t) (inote.descdata - inote.namedata) == inote.namesz)
20920 {
20921 temp = (char *) malloc (inote.namesz + 1);
20922 if (temp == NULL)
20923 {
20924 error (_("Out of memory allocating space for inote name\n"));
20925 res = false;
20926 break;
20927 }
20928
20929 memcpy (temp, inote.namedata, inote.namesz);
20930 inote.namedata = temp;
20931 }
20932 inote.namedata[inote.namesz] = 0;
20933 }
20934
20935 if (! process_note (& inote, filedata))
20936 res = false;
20937
20938 free (temp);
20939 temp = NULL;
20940 }
20941
20942 free (pnotes);
20943
20944 return res;
20945 }
20946
20947 static bool
20948 process_corefile_note_segments (Filedata * filedata)
20949 {
20950 Elf_Internal_Phdr *segment;
20951 unsigned int i;
20952 bool res = true;
20953
20954 if (! get_program_headers (filedata))
20955 return true;
20956
20957 for (i = 0, segment = filedata->program_headers;
20958 i < filedata->file_header.e_phnum;
20959 i++, segment++)
20960 {
20961 if (segment->p_type == PT_NOTE)
20962 if (! process_notes_at (filedata, NULL,
20963 (bfd_vma) segment->p_offset,
20964 (bfd_vma) segment->p_filesz,
20965 (bfd_vma) segment->p_align))
20966 res = false;
20967 }
20968
20969 return res;
20970 }
20971
20972 static bool
20973 process_v850_notes (Filedata * filedata, bfd_vma offset, bfd_vma length)
20974 {
20975 Elf_External_Note * pnotes;
20976 Elf_External_Note * external;
20977 char * end;
20978 bool res = true;
20979
20980 if (length <= 0)
20981 return false;
20982
20983 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
20984 _("v850 notes"));
20985 if (pnotes == NULL)
20986 return false;
20987
20988 external = pnotes;
20989 end = (char*) pnotes + length;
20990
20991 printf (_("\nDisplaying contents of Renesas V850 notes section at offset 0x%lx with length 0x%lx:\n"),
20992 (unsigned long) offset, (unsigned long) length);
20993
20994 while ((char *) external + sizeof (Elf_External_Note) < end)
20995 {
20996 Elf_External_Note * next;
20997 Elf_Internal_Note inote;
20998
20999 inote.type = BYTE_GET (external->type);
21000 inote.namesz = BYTE_GET (external->namesz);
21001 inote.namedata = external->name;
21002 inote.descsz = BYTE_GET (external->descsz);
21003 inote.descdata = inote.namedata + align_power (inote.namesz, 2);
21004 inote.descpos = offset + (inote.descdata - (char *) pnotes);
21005
21006 if (inote.descdata < (char *) pnotes || inote.descdata >= end)
21007 {
21008 warn (_("Corrupt note: name size is too big: %lx\n"), inote.namesz);
21009 inote.descdata = inote.namedata;
21010 inote.namesz = 0;
21011 }
21012
21013 next = (Elf_External_Note *) (inote.descdata + align_power (inote.descsz, 2));
21014
21015 if ( ((char *) next > end)
21016 || ((char *) next < (char *) pnotes))
21017 {
21018 warn (_("corrupt descsz found in note at offset 0x%lx\n"),
21019 (unsigned long) ((char *) external - (char *) pnotes));
21020 warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
21021 inote.type, inote.namesz, inote.descsz);
21022 break;
21023 }
21024
21025 external = next;
21026
21027 /* Prevent out-of-bounds indexing. */
21028 if ( inote.namedata + inote.namesz > end
21029 || inote.namedata + inote.namesz < inote.namedata)
21030 {
21031 warn (_("corrupt namesz found in note at offset 0x%lx\n"),
21032 (unsigned long) ((char *) external - (char *) pnotes));
21033 warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
21034 inote.type, inote.namesz, inote.descsz);
21035 break;
21036 }
21037
21038 printf (" %s: ", get_v850_elf_note_type (inote.type));
21039
21040 if (! print_v850_note (& inote))
21041 {
21042 res = false;
21043 printf ("<corrupt sizes: namesz: %lx, descsz: %lx>\n",
21044 inote.namesz, inote.descsz);
21045 }
21046 }
21047
21048 free (pnotes);
21049
21050 return res;
21051 }
21052
21053 static bool
21054 process_note_sections (Filedata * filedata)
21055 {
21056 Elf_Internal_Shdr *section;
21057 unsigned long i;
21058 unsigned int n = 0;
21059 bool res = true;
21060
21061 for (i = 0, section = filedata->section_headers;
21062 i < filedata->file_header.e_shnum && section != NULL;
21063 i++, section++)
21064 {
21065 if (section->sh_type == SHT_NOTE)
21066 {
21067 if (! process_notes_at (filedata, section,
21068 (bfd_vma) section->sh_offset,
21069 (bfd_vma) section->sh_size,
21070 (bfd_vma) section->sh_addralign))
21071 res = false;
21072 n++;
21073 }
21074
21075 if (( filedata->file_header.e_machine == EM_V800
21076 || filedata->file_header.e_machine == EM_V850
21077 || filedata->file_header.e_machine == EM_CYGNUS_V850)
21078 && section->sh_type == SHT_RENESAS_INFO)
21079 {
21080 if (! process_v850_notes (filedata,
21081 (bfd_vma) section->sh_offset,
21082 (bfd_vma) section->sh_size))
21083 res = false;
21084 n++;
21085 }
21086 }
21087
21088 if (n == 0)
21089 /* Try processing NOTE segments instead. */
21090 return process_corefile_note_segments (filedata);
21091
21092 return res;
21093 }
21094
21095 static bool
21096 process_notes (Filedata * filedata)
21097 {
21098 /* If we have not been asked to display the notes then do nothing. */
21099 if (! do_notes)
21100 return true;
21101
21102 if (filedata->file_header.e_type != ET_CORE)
21103 return process_note_sections (filedata);
21104
21105 /* No program headers means no NOTE segment. */
21106 if (filedata->file_header.e_phnum > 0)
21107 return process_corefile_note_segments (filedata);
21108
21109 if (filedata->is_separate)
21110 printf (_("No notes found in linked file '%s'.\n"),
21111 filedata->file_name);
21112 else
21113 printf (_("No notes found file.\n"));
21114
21115 return true;
21116 }
21117
21118 static unsigned char *
21119 display_public_gnu_attributes (unsigned char * start,
21120 const unsigned char * const end)
21121 {
21122 printf (_(" Unknown GNU attribute: %s\n"), start);
21123
21124 start += strnlen ((char *) start, end - start);
21125 display_raw_attribute (start, end);
21126
21127 return (unsigned char *) end;
21128 }
21129
21130 static unsigned char *
21131 display_generic_attribute (unsigned char * start,
21132 unsigned int tag,
21133 const unsigned char * const end)
21134 {
21135 if (tag == 0)
21136 return (unsigned char *) end;
21137
21138 return display_tag_value (tag, start, end);
21139 }
21140
21141 static bool
21142 process_arch_specific (Filedata * filedata)
21143 {
21144 if (! do_arch)
21145 return true;
21146
21147 switch (filedata->file_header.e_machine)
21148 {
21149 case EM_ARC:
21150 case EM_ARC_COMPACT:
21151 case EM_ARC_COMPACT2:
21152 return process_attributes (filedata, "ARC", SHT_ARC_ATTRIBUTES,
21153 display_arc_attribute,
21154 display_generic_attribute);
21155 case EM_ARM:
21156 return process_attributes (filedata, "aeabi", SHT_ARM_ATTRIBUTES,
21157 display_arm_attribute,
21158 display_generic_attribute);
21159
21160 case EM_MIPS:
21161 case EM_MIPS_RS3_LE:
21162 return process_mips_specific (filedata);
21163
21164 case EM_MSP430:
21165 return process_attributes (filedata, "mspabi", SHT_MSP430_ATTRIBUTES,
21166 display_msp430_attribute,
21167 display_msp430_gnu_attribute);
21168
21169 case EM_RISCV:
21170 return process_attributes (filedata, "riscv", SHT_RISCV_ATTRIBUTES,
21171 display_riscv_attribute,
21172 display_generic_attribute);
21173
21174 case EM_NDS32:
21175 return process_nds32_specific (filedata);
21176
21177 case EM_68K:
21178 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
21179 display_m68k_gnu_attribute);
21180
21181 case EM_PPC:
21182 case EM_PPC64:
21183 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
21184 display_power_gnu_attribute);
21185
21186 case EM_S390:
21187 case EM_S390_OLD:
21188 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
21189 display_s390_gnu_attribute);
21190
21191 case EM_SPARC:
21192 case EM_SPARC32PLUS:
21193 case EM_SPARCV9:
21194 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
21195 display_sparc_gnu_attribute);
21196
21197 case EM_TI_C6000:
21198 return process_attributes (filedata, "c6xabi", SHT_C6000_ATTRIBUTES,
21199 display_tic6x_attribute,
21200 display_generic_attribute);
21201
21202 case EM_CSKY:
21203 return process_attributes (filedata, "csky", SHT_CSKY_ATTRIBUTES,
21204 display_csky_attribute, NULL);
21205
21206 default:
21207 return process_attributes (filedata, "gnu", SHT_GNU_ATTRIBUTES,
21208 display_public_gnu_attributes,
21209 display_generic_attribute);
21210 }
21211 }
21212
21213 static bool
21214 get_file_header (Filedata * filedata)
21215 {
21216 /* Read in the identity array. */
21217 if (fread (filedata->file_header.e_ident, EI_NIDENT, 1, filedata->handle) != 1)
21218 return false;
21219
21220 /* Determine how to read the rest of the header. */
21221 switch (filedata->file_header.e_ident[EI_DATA])
21222 {
21223 default:
21224 case ELFDATANONE:
21225 case ELFDATA2LSB:
21226 byte_get = byte_get_little_endian;
21227 byte_put = byte_put_little_endian;
21228 break;
21229 case ELFDATA2MSB:
21230 byte_get = byte_get_big_endian;
21231 byte_put = byte_put_big_endian;
21232 break;
21233 }
21234
21235 /* For now we only support 32 bit and 64 bit ELF files. */
21236 is_32bit_elf = (filedata->file_header.e_ident[EI_CLASS] != ELFCLASS64);
21237
21238 /* Read in the rest of the header. */
21239 if (is_32bit_elf)
21240 {
21241 Elf32_External_Ehdr ehdr32;
21242
21243 if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, filedata->handle) != 1)
21244 return false;
21245
21246 filedata->file_header.e_type = BYTE_GET (ehdr32.e_type);
21247 filedata->file_header.e_machine = BYTE_GET (ehdr32.e_machine);
21248 filedata->file_header.e_version = BYTE_GET (ehdr32.e_version);
21249 filedata->file_header.e_entry = BYTE_GET (ehdr32.e_entry);
21250 filedata->file_header.e_phoff = BYTE_GET (ehdr32.e_phoff);
21251 filedata->file_header.e_shoff = BYTE_GET (ehdr32.e_shoff);
21252 filedata->file_header.e_flags = BYTE_GET (ehdr32.e_flags);
21253 filedata->file_header.e_ehsize = BYTE_GET (ehdr32.e_ehsize);
21254 filedata->file_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
21255 filedata->file_header.e_phnum = BYTE_GET (ehdr32.e_phnum);
21256 filedata->file_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
21257 filedata->file_header.e_shnum = BYTE_GET (ehdr32.e_shnum);
21258 filedata->file_header.e_shstrndx = BYTE_GET (ehdr32.e_shstrndx);
21259 }
21260 else
21261 {
21262 Elf64_External_Ehdr ehdr64;
21263
21264 /* If we have been compiled with sizeof (bfd_vma) == 4, then
21265 we will not be able to cope with the 64bit data found in
21266 64 ELF files. Detect this now and abort before we start
21267 overwriting things. */
21268 if (sizeof (bfd_vma) < 8)
21269 {
21270 error (_("This instance of readelf has been built without support for a\n\
21271 64 bit data type and so it cannot read 64 bit ELF files.\n"));
21272 return false;
21273 }
21274
21275 if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, filedata->handle) != 1)
21276 return false;
21277
21278 filedata->file_header.e_type = BYTE_GET (ehdr64.e_type);
21279 filedata->file_header.e_machine = BYTE_GET (ehdr64.e_machine);
21280 filedata->file_header.e_version = BYTE_GET (ehdr64.e_version);
21281 filedata->file_header.e_entry = BYTE_GET (ehdr64.e_entry);
21282 filedata->file_header.e_phoff = BYTE_GET (ehdr64.e_phoff);
21283 filedata->file_header.e_shoff = BYTE_GET (ehdr64.e_shoff);
21284 filedata->file_header.e_flags = BYTE_GET (ehdr64.e_flags);
21285 filedata->file_header.e_ehsize = BYTE_GET (ehdr64.e_ehsize);
21286 filedata->file_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
21287 filedata->file_header.e_phnum = BYTE_GET (ehdr64.e_phnum);
21288 filedata->file_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
21289 filedata->file_header.e_shnum = BYTE_GET (ehdr64.e_shnum);
21290 filedata->file_header.e_shstrndx = BYTE_GET (ehdr64.e_shstrndx);
21291 }
21292
21293 return true;
21294 }
21295
21296 static void
21297 free_filedata (Filedata *filedata)
21298 {
21299 free (filedata->program_interpreter);
21300 free (filedata->program_headers);
21301 free (filedata->section_headers);
21302 free (filedata->string_table);
21303 free (filedata->dump.dump_sects);
21304 free (filedata->dynamic_strings);
21305 free (filedata->dynamic_symbols);
21306 free (filedata->dynamic_syminfo);
21307 free (filedata->dynamic_section);
21308
21309 while (filedata->symtab_shndx_list != NULL)
21310 {
21311 elf_section_list *next = filedata->symtab_shndx_list->next;
21312 free (filedata->symtab_shndx_list);
21313 filedata->symtab_shndx_list = next;
21314 }
21315
21316 free (filedata->section_headers_groups);
21317
21318 if (filedata->section_groups)
21319 {
21320 size_t i;
21321 struct group_list * g;
21322 struct group_list * next;
21323
21324 for (i = 0; i < filedata->group_count; i++)
21325 {
21326 for (g = filedata->section_groups [i].root; g != NULL; g = next)
21327 {
21328 next = g->next;
21329 free (g);
21330 }
21331 }
21332
21333 free (filedata->section_groups);
21334 }
21335 memset (&filedata->section_headers, 0,
21336 sizeof (Filedata) - offsetof (Filedata, section_headers));
21337 }
21338
21339 static void
21340 close_file (Filedata * filedata)
21341 {
21342 if (filedata)
21343 {
21344 if (filedata->handle)
21345 fclose (filedata->handle);
21346 free (filedata);
21347 }
21348 }
21349
21350 void
21351 close_debug_file (void * data)
21352 {
21353 free_filedata ((Filedata *) data);
21354 close_file ((Filedata *) data);
21355 }
21356
21357 static Filedata *
21358 open_file (const char * pathname, bool is_separate)
21359 {
21360 struct stat statbuf;
21361 Filedata * filedata = NULL;
21362
21363 if (stat (pathname, & statbuf) < 0
21364 || ! S_ISREG (statbuf.st_mode))
21365 goto fail;
21366
21367 filedata = calloc (1, sizeof * filedata);
21368 if (filedata == NULL)
21369 goto fail;
21370
21371 filedata->handle = fopen (pathname, "rb");
21372 if (filedata->handle == NULL)
21373 goto fail;
21374
21375 filedata->file_size = (bfd_size_type) statbuf.st_size;
21376 filedata->file_name = pathname;
21377 filedata->is_separate = is_separate;
21378
21379 if (! get_file_header (filedata))
21380 goto fail;
21381
21382 if (!get_section_headers (filedata, false))
21383 goto fail;
21384
21385 return filedata;
21386
21387 fail:
21388 if (filedata)
21389 {
21390 if (filedata->handle)
21391 fclose (filedata->handle);
21392 free (filedata);
21393 }
21394 return NULL;
21395 }
21396
21397 void *
21398 open_debug_file (const char * pathname)
21399 {
21400 return open_file (pathname, true);
21401 }
21402
21403 static void
21404 initialise_dump_sects (Filedata * filedata)
21405 {
21406 /* Initialise the dump_sects array from the cmdline_dump_sects array.
21407 Note we do this even if cmdline_dump_sects is empty because we
21408 must make sure that the dump_sets array is zeroed out before each
21409 object file is processed. */
21410 if (filedata->dump.num_dump_sects > cmdline.num_dump_sects)
21411 memset (filedata->dump.dump_sects, 0,
21412 filedata->dump.num_dump_sects * sizeof (*filedata->dump.dump_sects));
21413
21414 if (cmdline.num_dump_sects > 0)
21415 {
21416 if (filedata->dump.num_dump_sects == 0)
21417 /* A sneaky way of allocating the dump_sects array. */
21418 request_dump_bynumber (&filedata->dump, cmdline.num_dump_sects, 0);
21419
21420 assert (filedata->dump.num_dump_sects >= cmdline.num_dump_sects);
21421 memcpy (filedata->dump.dump_sects, cmdline.dump_sects,
21422 cmdline.num_dump_sects * sizeof (*filedata->dump.dump_sects));
21423 }
21424 }
21425
21426 /* Process one ELF object file according to the command line options.
21427 This file may actually be stored in an archive. The file is
21428 positioned at the start of the ELF object. Returns TRUE if no
21429 problems were encountered, FALSE otherwise. */
21430
21431 static bool
21432 process_object (Filedata * filedata)
21433 {
21434 bool have_separate_files;
21435 unsigned int i;
21436 bool res;
21437
21438 if (! get_file_header (filedata))
21439 {
21440 error (_("%s: Failed to read file header\n"), filedata->file_name);
21441 return false;
21442 }
21443
21444 /* Initialise per file variables. */
21445 for (i = ARRAY_SIZE (filedata->version_info); i--;)
21446 filedata->version_info[i] = 0;
21447
21448 for (i = ARRAY_SIZE (filedata->dynamic_info); i--;)
21449 filedata->dynamic_info[i] = 0;
21450 filedata->dynamic_info_DT_GNU_HASH = 0;
21451 filedata->dynamic_info_DT_MIPS_XHASH = 0;
21452
21453 /* Process the file. */
21454 if (show_name)
21455 printf (_("\nFile: %s\n"), filedata->file_name);
21456
21457 initialise_dump_sects (filedata);
21458
21459 /* There may be some extensions in the first section header. Don't
21460 bomb if we can't read it. */
21461 get_section_headers (filedata, true);
21462
21463 if (! process_file_header (filedata))
21464 {
21465 res = false;
21466 goto out;
21467 }
21468
21469 /* Throw away the single section header read above, so that we
21470 re-read the entire set. */
21471 free (filedata->section_headers);
21472 filedata->section_headers = NULL;
21473
21474 if (! process_section_headers (filedata))
21475 {
21476 /* Without loaded section headers we cannot process lots of things. */
21477 do_unwind = do_version = do_dump = do_arch = false;
21478
21479 if (! do_using_dynamic)
21480 do_syms = do_dyn_syms = do_reloc = false;
21481 }
21482
21483 if (! process_section_groups (filedata))
21484 /* Without loaded section groups we cannot process unwind. */
21485 do_unwind = false;
21486
21487 process_program_headers (filedata);
21488
21489 res = process_dynamic_section (filedata);
21490
21491 if (! process_relocs (filedata))
21492 res = false;
21493
21494 if (! process_unwind (filedata))
21495 res = false;
21496
21497 if (! process_symbol_table (filedata))
21498 res = false;
21499
21500 if (! process_lto_symbol_tables (filedata))
21501 res = false;
21502
21503 if (! process_syminfo (filedata))
21504 res = false;
21505
21506 if (! process_version_sections (filedata))
21507 res = false;
21508
21509 if (filedata->file_header.e_shstrndx != SHN_UNDEF)
21510 have_separate_files = load_separate_debug_files (filedata, filedata->file_name);
21511 else
21512 have_separate_files = false;
21513
21514 if (! process_section_contents (filedata))
21515 res = false;
21516
21517 if (have_separate_files)
21518 {
21519 separate_info * d;
21520
21521 for (d = first_separate_info; d != NULL; d = d->next)
21522 {
21523 initialise_dump_sects (d->handle);
21524
21525 if (process_links && ! process_file_header (d->handle))
21526 res = false;
21527 else if (! process_section_headers (d->handle))
21528 res = false;
21529 else if (! process_section_contents (d->handle))
21530 res = false;
21531 else if (process_links)
21532 {
21533 if (! process_section_groups (d->handle))
21534 res = false;
21535 process_program_headers (d->handle);
21536 if (! process_dynamic_section (d->handle))
21537 res = false;
21538 if (! process_relocs (d->handle))
21539 res = false;
21540 if (! process_unwind (d->handle))
21541 res = false;
21542 if (! process_symbol_table (d->handle))
21543 res = false;
21544 if (! process_lto_symbol_tables (d->handle))
21545 res = false;
21546 if (! process_syminfo (d->handle))
21547 res = false;
21548 if (! process_version_sections (d->handle))
21549 res = false;
21550 if (! process_notes (d->handle))
21551 res = false;
21552 }
21553 }
21554
21555 /* The file handles are closed by the call to free_debug_memory() below. */
21556 }
21557
21558 if (! process_notes (filedata))
21559 res = false;
21560
21561 if (! process_gnu_liblist (filedata))
21562 res = false;
21563
21564 if (! process_arch_specific (filedata))
21565 res = false;
21566
21567 out:
21568 free_filedata (filedata);
21569
21570 free_debug_memory ();
21571
21572 return res;
21573 }
21574
21575 /* Process an ELF archive.
21576 On entry the file is positioned just after the ARMAG string.
21577 Returns TRUE upon success, FALSE otherwise. */
21578
21579 static bool
21580 process_archive (Filedata * filedata, bool is_thin_archive)
21581 {
21582 struct archive_info arch;
21583 struct archive_info nested_arch;
21584 size_t got;
21585 bool ret = true;
21586
21587 show_name = true;
21588
21589 /* The ARCH structure is used to hold information about this archive. */
21590 arch.file_name = NULL;
21591 arch.file = NULL;
21592 arch.index_array = NULL;
21593 arch.sym_table = NULL;
21594 arch.longnames = NULL;
21595
21596 /* The NESTED_ARCH structure is used as a single-item cache of information
21597 about a nested archive (when members of a thin archive reside within
21598 another regular archive file). */
21599 nested_arch.file_name = NULL;
21600 nested_arch.file = NULL;
21601 nested_arch.index_array = NULL;
21602 nested_arch.sym_table = NULL;
21603 nested_arch.longnames = NULL;
21604
21605 if (setup_archive (&arch, filedata->file_name, filedata->handle,
21606 filedata->file_size, is_thin_archive,
21607 do_archive_index) != 0)
21608 {
21609 ret = false;
21610 goto out;
21611 }
21612
21613 if (do_archive_index)
21614 {
21615 if (arch.sym_table == NULL)
21616 error (_("%s: unable to dump the index as none was found\n"),
21617 filedata->file_name);
21618 else
21619 {
21620 unsigned long i, l;
21621 unsigned long current_pos;
21622
21623 printf (_("Index of archive %s: (%lu entries, 0x%lx bytes "
21624 "in the symbol table)\n"),
21625 filedata->file_name, (unsigned long) arch.index_num,
21626 arch.sym_size);
21627
21628 current_pos = ftell (filedata->handle);
21629
21630 for (i = l = 0; i < arch.index_num; i++)
21631 {
21632 if (i == 0
21633 || (i > 0 && arch.index_array[i] != arch.index_array[i - 1]))
21634 {
21635 char * member_name
21636 = get_archive_member_name_at (&arch, arch.index_array[i],
21637 &nested_arch);
21638
21639 if (member_name != NULL)
21640 {
21641 char * qualified_name
21642 = make_qualified_name (&arch, &nested_arch,
21643 member_name);
21644
21645 if (qualified_name != NULL)
21646 {
21647 printf (_("Contents of binary %s at offset "),
21648 qualified_name);
21649 (void) print_vma (arch.index_array[i], PREFIX_HEX);
21650 putchar ('\n');
21651 free (qualified_name);
21652 }
21653 free (member_name);
21654 }
21655 }
21656
21657 if (l >= arch.sym_size)
21658 {
21659 error (_("%s: end of the symbol table reached "
21660 "before the end of the index\n"),
21661 filedata->file_name);
21662 ret = false;
21663 break;
21664 }
21665 /* PR 17531: file: 0b6630b2. */
21666 printf ("\t%.*s\n",
21667 (int) (arch.sym_size - l), arch.sym_table + l);
21668 l += strnlen (arch.sym_table + l, arch.sym_size - l) + 1;
21669 }
21670
21671 if (arch.uses_64bit_indices)
21672 l = (l + 7) & ~ 7;
21673 else
21674 l += l & 1;
21675
21676 if (l < arch.sym_size)
21677 {
21678 error (ngettext ("%s: %ld byte remains in the symbol table, "
21679 "but without corresponding entries in "
21680 "the index table\n",
21681 "%s: %ld bytes remain in the symbol table, "
21682 "but without corresponding entries in "
21683 "the index table\n",
21684 arch.sym_size - l),
21685 filedata->file_name, arch.sym_size - l);
21686 ret = false;
21687 }
21688
21689 if (fseek (filedata->handle, current_pos, SEEK_SET) != 0)
21690 {
21691 error (_("%s: failed to seek back to start of object files "
21692 "in the archive\n"),
21693 filedata->file_name);
21694 ret = false;
21695 goto out;
21696 }
21697 }
21698
21699 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
21700 && !do_segments && !do_header && !do_dump && !do_version
21701 && !do_histogram && !do_debugging && !do_arch && !do_notes
21702 && !do_section_groups && !do_dyn_syms)
21703 {
21704 ret = true; /* Archive index only. */
21705 goto out;
21706 }
21707 }
21708
21709 while (1)
21710 {
21711 char * name;
21712 size_t namelen;
21713 char * qualified_name;
21714
21715 /* Read the next archive header. */
21716 if (fseek (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0)
21717 {
21718 error (_("%s: failed to seek to next archive header\n"),
21719 arch.file_name);
21720 ret = false;
21721 break;
21722 }
21723 got = fread (&arch.arhdr, 1, sizeof arch.arhdr, filedata->handle);
21724 if (got != sizeof arch.arhdr)
21725 {
21726 if (got == 0)
21727 break;
21728 /* PR 24049 - we cannot use filedata->file_name as this will
21729 have already been freed. */
21730 error (_("%s: failed to read archive header\n"), arch.file_name);
21731
21732 ret = false;
21733 break;
21734 }
21735 if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
21736 {
21737 error (_("%s: did not find a valid archive header\n"),
21738 arch.file_name);
21739 ret = false;
21740 break;
21741 }
21742
21743 arch.next_arhdr_offset += sizeof arch.arhdr;
21744
21745 filedata->archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
21746 if (filedata->archive_file_size & 01)
21747 ++filedata->archive_file_size;
21748
21749 name = get_archive_member_name (&arch, &nested_arch);
21750 if (name == NULL)
21751 {
21752 error (_("%s: bad archive file name\n"), arch.file_name);
21753 ret = false;
21754 break;
21755 }
21756 namelen = strlen (name);
21757
21758 qualified_name = make_qualified_name (&arch, &nested_arch, name);
21759 if (qualified_name == NULL)
21760 {
21761 error (_("%s: bad archive file name\n"), arch.file_name);
21762 free (name);
21763 ret = false;
21764 break;
21765 }
21766
21767 if (is_thin_archive && arch.nested_member_origin == 0)
21768 {
21769 /* This is a proxy for an external member of a thin archive. */
21770 Filedata * member_filedata;
21771 char * member_file_name = adjust_relative_path
21772 (filedata->file_name, name, namelen);
21773
21774 free (name);
21775 if (member_file_name == NULL)
21776 {
21777 free (qualified_name);
21778 ret = false;
21779 break;
21780 }
21781
21782 member_filedata = open_file (member_file_name, false);
21783 if (member_filedata == NULL)
21784 {
21785 error (_("Input file '%s' is not readable.\n"), member_file_name);
21786 free (member_file_name);
21787 free (qualified_name);
21788 ret = false;
21789 break;
21790 }
21791
21792 filedata->archive_file_offset = arch.nested_member_origin;
21793 member_filedata->file_name = qualified_name;
21794
21795 if (! process_object (member_filedata))
21796 ret = false;
21797
21798 close_file (member_filedata);
21799 free (member_file_name);
21800 }
21801 else if (is_thin_archive)
21802 {
21803 Filedata thin_filedata;
21804
21805 memset (&thin_filedata, 0, sizeof (thin_filedata));
21806
21807 /* PR 15140: Allow for corrupt thin archives. */
21808 if (nested_arch.file == NULL)
21809 {
21810 error (_("%s: contains corrupt thin archive: %s\n"),
21811 qualified_name, name);
21812 free (qualified_name);
21813 free (name);
21814 ret = false;
21815 break;
21816 }
21817 free (name);
21818
21819 /* This is a proxy for a member of a nested archive. */
21820 filedata->archive_file_offset
21821 = arch.nested_member_origin + sizeof arch.arhdr;
21822
21823 /* The nested archive file will have been opened and setup by
21824 get_archive_member_name. */
21825 if (fseek (nested_arch.file, filedata->archive_file_offset,
21826 SEEK_SET) != 0)
21827 {
21828 error (_("%s: failed to seek to archive member.\n"),
21829 nested_arch.file_name);
21830 free (qualified_name);
21831 ret = false;
21832 break;
21833 }
21834
21835 thin_filedata.handle = nested_arch.file;
21836 thin_filedata.file_name = qualified_name;
21837
21838 if (! process_object (& thin_filedata))
21839 ret = false;
21840 }
21841 else
21842 {
21843 free (name);
21844 filedata->archive_file_offset = arch.next_arhdr_offset;
21845 filedata->file_name = qualified_name;
21846 if (! process_object (filedata))
21847 ret = false;
21848 arch.next_arhdr_offset += filedata->archive_file_size;
21849 /* Stop looping with "negative" archive_file_size. */
21850 if (arch.next_arhdr_offset < filedata->archive_file_size)
21851 arch.next_arhdr_offset = -1ul;
21852 }
21853
21854 free (qualified_name);
21855 }
21856
21857 out:
21858 if (nested_arch.file != NULL)
21859 fclose (nested_arch.file);
21860 release_archive (&nested_arch);
21861 release_archive (&arch);
21862
21863 return ret;
21864 }
21865
21866 static bool
21867 process_file (char * file_name)
21868 {
21869 Filedata * filedata = NULL;
21870 struct stat statbuf;
21871 char armag[SARMAG];
21872 bool ret = true;
21873
21874 if (stat (file_name, &statbuf) < 0)
21875 {
21876 if (errno == ENOENT)
21877 error (_("'%s': No such file\n"), file_name);
21878 else
21879 error (_("Could not locate '%s'. System error message: %s\n"),
21880 file_name, strerror (errno));
21881 return false;
21882 }
21883
21884 if (! S_ISREG (statbuf.st_mode))
21885 {
21886 error (_("'%s' is not an ordinary file\n"), file_name);
21887 return false;
21888 }
21889
21890 filedata = calloc (1, sizeof * filedata);
21891 if (filedata == NULL)
21892 {
21893 error (_("Out of memory allocating file data structure\n"));
21894 return false;
21895 }
21896
21897 filedata->file_name = file_name;
21898 filedata->handle = fopen (file_name, "rb");
21899 if (filedata->handle == NULL)
21900 {
21901 error (_("Input file '%s' is not readable.\n"), file_name);
21902 free (filedata);
21903 return false;
21904 }
21905
21906 if (fread (armag, SARMAG, 1, filedata->handle) != 1)
21907 {
21908 error (_("%s: Failed to read file's magic number\n"), file_name);
21909 fclose (filedata->handle);
21910 free (filedata);
21911 return false;
21912 }
21913
21914 filedata->file_size = (bfd_size_type) statbuf.st_size;
21915 filedata->is_separate = false;
21916
21917 if (memcmp (armag, ARMAG, SARMAG) == 0)
21918 {
21919 if (! process_archive (filedata, false))
21920 ret = false;
21921 }
21922 else if (memcmp (armag, ARMAGT, SARMAG) == 0)
21923 {
21924 if ( ! process_archive (filedata, true))
21925 ret = false;
21926 }
21927 else
21928 {
21929 if (do_archive_index && !check_all)
21930 error (_("File %s is not an archive so its index cannot be displayed.\n"),
21931 file_name);
21932
21933 rewind (filedata->handle);
21934 filedata->archive_file_size = filedata->archive_file_offset = 0;
21935
21936 if (! process_object (filedata))
21937 ret = false;
21938 }
21939
21940 fclose (filedata->handle);
21941 free (filedata->section_headers);
21942 free (filedata->program_headers);
21943 free (filedata->string_table);
21944 free (filedata->dump.dump_sects);
21945 free (filedata);
21946
21947 free (ba_cache.strtab);
21948 ba_cache.strtab = NULL;
21949 free (ba_cache.symtab);
21950 ba_cache.symtab = NULL;
21951 ba_cache.filedata = NULL;
21952
21953 return ret;
21954 }
21955
21956 #ifdef SUPPORT_DISASSEMBLY
21957 /* Needed by the i386 disassembler. For extra credit, someone could
21958 fix this so that we insert symbolic addresses here, esp for GOT/PLT
21959 symbols. */
21960
21961 void
21962 print_address (unsigned int addr, FILE * outfile)
21963 {
21964 fprintf (outfile,"0x%8.8x", addr);
21965 }
21966
21967 /* Needed by the i386 disassembler. */
21968
21969 void
21970 db_task_printsym (unsigned int addr)
21971 {
21972 print_address (addr, stderr);
21973 }
21974 #endif
21975
21976 int
21977 main (int argc, char ** argv)
21978 {
21979 int err;
21980
21981 #ifdef HAVE_LC_MESSAGES
21982 setlocale (LC_MESSAGES, "");
21983 #endif
21984 setlocale (LC_CTYPE, "");
21985 bindtextdomain (PACKAGE, LOCALEDIR);
21986 textdomain (PACKAGE);
21987
21988 expandargv (&argc, &argv);
21989
21990 parse_args (& cmdline, argc, argv);
21991
21992 if (optind < (argc - 1))
21993 /* When displaying information for more than one file,
21994 prefix the information with the file name. */
21995 show_name = true;
21996 else if (optind >= argc)
21997 {
21998 /* Ensure that the warning is always displayed. */
21999 do_checks = true;
22000
22001 warn (_("Nothing to do.\n"));
22002 usage (stderr);
22003 }
22004
22005 err = false;
22006 while (optind < argc)
22007 if (! process_file (argv[optind++]))
22008 err = true;
22009
22010 free (cmdline.dump_sects);
22011
22012 free (dump_ctf_symtab_name);
22013 free (dump_ctf_strtab_name);
22014 free (dump_ctf_parent_name);
22015
22016 return err ? EXIT_FAILURE : EXIT_SUCCESS;
22017 }